Fix lose() invocation inside sparc-arch.c
[sbcl.git] / src / runtime / sparc-arch.c
index 9095f02..67aa7cf 100644 (file)
@@ -35,6 +35,9 @@ void arch_init(void)
 
 os_vm_address_t arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
 {
+#if 1 /* New way. */
+    return (os_vm_address_t)code->si_addr;
+#else /* Old way, almost certainly predates sigaction(2)-style handlers */
     unsigned int badinst;
     unsigned int *pc;
     int rs1;
@@ -82,6 +85,7 @@ os_vm_address_t arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *contex
             (*os_context_register_addr(context, rs1) +
              *os_context_register_addr(context, rs2));
     }
+#endif
 }
 
 void arch_skip_instruction(os_context_t *context)
@@ -101,7 +105,18 @@ unsigned char *arch_internal_error_arguments(os_context_t *context)
 
 boolean arch_pseudo_atomic_atomic(os_context_t *context)
 {
-    return ((*os_context_register_addr(context,reg_ALLOC)) & 4);
+    /* FIXME: this foreign_function_call_active test is dubious at
+     * best. If a foreign call is made in a pseudo atomic section
+     * (?) or more likely a pseudo atomic section is in a foreign
+     * call then an interrupt is executed immediately. Maybe it
+     * has to do with C code not maintaining pseudo atomic
+     * properly. MG - 2005-08-10
+     *
+     * The foreign_function_call_active used to live at each call-site
+     * to arch_pseudo_atomic_atomic, but this seems clearer.
+     * --NS 2007-05-15 */
+    return (!foreign_function_call_active)
+        && ((*os_context_register_addr(context,reg_ALLOC)) & 4);
 }
 
 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
@@ -240,14 +255,68 @@ arch_handle_single_step_trap(os_context_t *context, int trap)
     arch_skip_instruction(context);
 }
 
-static void sigill_handler(int signal, siginfo_t *siginfo, void *void_context)
+#ifdef LISP_FEATURE_GENCGC
+void
+arch_handle_allocation_trap(os_context_t *context)
 {
-    os_context_t *context = arch_os_get_context(&void_context);
-#ifdef LISP_FEATURE_LINUX
-    /* FIXME: Check that this is necessary -- CSR, 2002-07-15 */
-    os_restore_fp_control(context);
+    unsigned int* pc;
+    unsigned int or_inst;
+    int rs1;
+    int size;
+    int immed;
+    int context_index;
+    boolean were_in_lisp;
+    char* memory;
+
+    if (foreign_function_call_active)
+      lose("Allocation trap inside foreign code.");
+
+    pc = (unsigned int*) *os_context_pc_addr(context);
+    or_inst = pc[-1];
+
+    /*
+     * The instruction before this trap instruction had better be an OR
+     * instruction!
+     */
+    if (!(((or_inst >> 30) == 2) && (((or_inst >> 19) & 0x1f) == 2)))
+        lose("Allocation trap not preceded by an OR instruction: 0x%08x",
+             or_inst);
+
+    /*
+     * An OR instruction.  RS1 is the register we want to allocate to.
+     * RS2 (or an immediate) is the size.
+     */
+    rs1 = (or_inst >> 14) & 0x1f;
+    immed = (or_inst >> 13) & 1;
+
+    if (immed == 1)
+        size = or_inst & 0x1fff;
+    else {
+        size = or_inst & 0x1f;
+        size = *os_context_register_addr(context, size);
+    }
+
+    fake_foreign_function_call(context);
+
+    /*
+     * Allocate some memory, store the memory address in rs1.
+     */
+    {
+        struct interrupt_data *data =
+            arch_os_get_current_thread()->interrupt_data;
+        data->allocation_trap_context = context;
+        memory = alloc(size);
+        data->allocation_trap_context = 0;
+    }
+    *os_context_register_addr(context, rs1) = memory;
+
+    undo_fake_foreign_function_call(context);
+}
 #endif
 
+static void sigill_handler(int signal, siginfo_t *siginfo,
+                           os_context_t *context)
+{
     if ((siginfo->si_code) == ILL_ILLOPC
 #ifdef LISP_FEATURE_LINUX
         || (linux_sparc_siginfo_bug && (siginfo->si_code == 2))
@@ -284,15 +353,12 @@ static void sigill_handler(int signal, siginfo_t *siginfo, void *void_context)
     }
 }
 
-static void sigemt_handler(int signal, siginfo_t *siginfo, void *void_context)
+static void sigemt_handler(int signal, siginfo_t *siginfo,
+                           os_context_t *context)
 {
     unsigned int badinst;
     boolean subtract, immed;
     int rd, rs1, op1, rs2, op2, result;
-    os_context_t *context = arch_os_get_context(&void_context);
-#ifdef LISP_FEATURE_LINUX
-    os_restore_fp_control(context);
-#endif
 
     badinst = *(unsigned int *)os_context_pc_addr(context);
     if ((badinst >> 30) != 2 || ((badinst >> 20) & 0x1f) != 0x11) {