1.0.26.16: fix gencgc on ppc
[sbcl.git] / src / runtime / ppc-arch.c
index 6554971..533293d 100644 (file)
@@ -1,3 +1,14 @@
+/*
+ * This software is part of the SBCL system. See the README file for
+ * more information.
+ *
+ * This software is derived from the CMU CL system, which was
+ * written at Carnegie Mellon University and released into the
+ * public domain. The software is in the public domain and is
+ * provided with absolutely no warranty. See the COPYING and CREDITS
+ * files for more information.
+ */
+
 #include <stdio.h>
 
 #include "sbcl.h"
 #include "globals.h"
 #include "validate.h"
 #include "os.h"
+#include "interrupt.h"
 #include "lispregs.h"
 #include "signal.h"
 #include "interrupt.h"
 #include "interr.h"
+#include "breakpoint.h"
+#include "alloc.h"
 
 #if defined(LISP_FEATURE_GENCGC)
 #include "gencgc-alloc-region.h"
@@ -27,6 +41,7 @@
 
      Caveat callers.  */
 
+#if defined (LISP_FEATURE_DARWIN) || defined(LISP_FEATURE_LINUX)
 #ifndef PT_DAR
 #define PT_DAR          41
 #endif
@@ -34,6 +49,7 @@
 #ifndef PT_DSISR
 #define PT_DSISR        42
 #endif
+#endif
 
 void arch_init() {
 }
@@ -41,10 +57,13 @@ void arch_init() {
 os_vm_address_t
 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
 {
-    unsigned long pc =  (unsigned long)(*os_context_pc_addr(context));
     os_vm_address_t addr;
 
+#if defined(LISP_FEATURE_NETBSD)
+    addr = (os_vm_address_t) (code->si_addr);
+#else
     addr = (os_vm_address_t) (*os_context_register_addr(context,PT_DAR));
+#endif
     return addr;
 }
 
@@ -67,7 +86,18 @@ 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
@@ -120,7 +150,7 @@ static unsigned int *skipped_break_addr, displaced_after_inst;
 static sigset_t orig_sigmask;
 
 void
-arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
+arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
 {
     /* not sure how we ensure that we get the breakpoint reinstalled
      * after doing this -dan */
@@ -132,6 +162,10 @@ arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
     *pc = orig_inst;
     os_flush_icache((os_vm_address_t) pc, sizeof(unsigned int));
     skipped_break_addr = pc;
+
+    /* FIXME: we should apparently be installing the after-breakpoint
+     * here, but would need to find the next instruction address for
+     * it first. alpha-arch.c shows how to do it. --NS 2007-04-02 */
 }
 
 #ifdef LISP_FEATURE_GENCGC
@@ -201,14 +235,11 @@ handle_allocation_trap(os_context_t * context)
 {
     unsigned int *pc;
     unsigned int inst;
-    unsigned int or_inst;
     unsigned int target, target_ptr, end_addr;
     unsigned int opcode;
     int size;
-    int immed;
     boolean were_in_lisp;
     char *memory;
-    sigset_t block;
 
     target = 0;
     size = 0;
@@ -217,10 +248,8 @@ handle_allocation_trap(os_context_t * context)
     fprintf(stderr, "In handle_allocation_trap\n");
 #endif
 
-    /*
-     * I don't think it's possible for us NOT to be in lisp when we get
-     * here.  Remove this later?
-     */
+    /* I don't think it's possible for us NOT to be in lisp when we get
+     * here.  Remove this later? */
     were_in_lisp = !foreign_function_call_active;
 
     if (were_in_lisp) {
@@ -328,7 +357,13 @@ handle_allocation_trap(os_context_t * context)
             dynamic_space_free_pointer);
 #endif
 
-    memory = (char *) alloc(size);
+    {
+        struct interrupt_data *data =
+            arch_os_get_current_thread()->interrupt_data;
+        data->allocation_trap_context = context;
+        memory = (char *) alloc(size);
+        data->allocation_trap_context = 0;
+    }
 
 #if 0
     fprintf(stderr, "alloc returned %p\n", memory);
@@ -360,15 +395,45 @@ handle_allocation_trap(os_context_t * context)
 }
 #endif
 
+void
+arch_handle_breakpoint(os_context_t *context)
+{
+    handle_breakpoint(context);
+}
+
+void
+arch_handle_fun_end_breakpoint(os_context_t *context)
+{
+    *os_context_pc_addr(context)
+        =(int)handle_fun_end_breakpoint(context);
+}
+
+void
+arch_handle_after_breakpoint(os_context_t *context)
+{
+    *skipped_break_addr = trap_Breakpoint;
+    skipped_break_addr = NULL;
+    *(unsigned int *)*os_context_pc_addr(context)
+        = displaced_after_inst;
+    *os_context_sigmask_addr(context)= orig_sigmask;
+    os_flush_icache((os_vm_address_t) *os_context_pc_addr(context),
+                    sizeof(unsigned int));
+}
+
+void
+arch_handle_single_step_trap(os_context_t *context, int trap)
+{
+    unsigned int code = *((u32 *)(*os_context_pc_addr(context)));
+    int register_offset = code >> 5 & 0x1f;
+    handle_single_step_trap(context, trap, register_offset);
+    arch_skip_instruction(context);
+}
 
 static void
 sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
 {
     unsigned int code;
 
-#ifdef LISP_FEATURE_LINUX
-    os_restore_fp_control(context);
-#endif
     code=*((u32 *)(*os_context_pc_addr(context)));
     if (code == ((3 << 26) | (0x18 << 21) | (reg_NL3 << 16))) {
         arch_clear_pseudo_atomic_interrupted(context);
@@ -384,9 +449,6 @@ sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
     if (allocation_trap_p(context)) {
         handle_allocation_trap(context);
         arch_skip_instruction(context);
-#ifdef LISP_FEATURE_DARWIN
-        DARWIN_FIX_CONTEXT(context);
-#endif
         return;
     }
 #endif
@@ -394,116 +456,22 @@ sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
     if ((code >> 16) == ((3 << 10) | (6 << 5))) {
         /* twllei reg_ZERO,N will always trap if reg_ZERO = 0 */
         int trap = code & 0x1f;
-
-        switch (trap) {
-        case trap_Halt:
-            fake_foreign_function_call(context);
-            lose("%%primitive halt called; the party is over.\n");
-
-        case trap_Error:
-        case trap_Cerror:
-            interrupt_internal_error(signal, code, context, trap == trap_Cerror);
-            break;
-
-        case trap_PendingInterrupt:
-            /* This is supposed run after WITHOUT-INTERRUPTS if there
-             * were pending signals. */
-            arch_skip_instruction(context);
-            interrupt_handle_pending(context);
-            break;
-
-        case trap_Breakpoint:
-            handle_breakpoint(signal, code, context);
-            break;
-
-        case trap_FunEndBreakpoint:
-            *os_context_pc_addr(context)
-                =(int)handle_fun_end_breakpoint(signal, code, context);
-            break;
-
-        case trap_AfterBreakpoint:
-            *skipped_break_addr = trap_Breakpoint;
-            skipped_break_addr = NULL;
-            *(unsigned int *)*os_context_pc_addr(context)
-                = displaced_after_inst;
-            *os_context_sigmask_addr(context)= orig_sigmask;
-
-            os_flush_icache((os_vm_address_t) *os_context_pc_addr(context),
-                            sizeof(unsigned int));
-            break;
-
-        default:
-            interrupt_handle_now(signal, code, context);
-            break;
-        }
-#ifdef LISP_FEATURE_DARWIN
-        DARWIN_FIX_CONTEXT(context);
-#endif
+        handle_trap(context,trap);
         return;
     }
     if (((code >> 26) == 3) && (((code >> 21) & 31) == 24)) {
-        interrupt_internal_error(signal, code, context, 0);
-#ifdef LISP_FEATURE_DARWIN
-        DARWIN_FIX_CONTEXT(context);
-#endif
+        interrupt_internal_error(context, 0);
         return;
     }
 
-    interrupt_handle_now(signal, code, context);
-#ifdef LISP_FEATURE_DARWIN
-    /* Work around G5 bug */
-    DARWIN_FIX_CONTEXT(context);
-#endif
+    interrupt_handle_now(signal, (siginfo_t *)code, context);
 }
 
 
 void arch_install_interrupt_handlers()
 {
-    undoably_install_low_level_interrupt_handler(SIGILL,sigtrap_handler);
-    undoably_install_low_level_interrupt_handler(SIGTRAP,sigtrap_handler);
-}
-
-
-extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
-
-lispobj funcall0(lispobj function)
-{
-    lispobj *args = current_control_stack_pointer;
-
-    return call_into_lisp(function, args, 0);
-}
-
-lispobj funcall1(lispobj function, lispobj arg0)
-{
-    lispobj *args = current_control_stack_pointer;
-
-    current_control_stack_pointer += 1;
-    args[0] = arg0;
-
-    return call_into_lisp(function, args, 1);
-}
-
-lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
-{
-    lispobj *args = current_control_stack_pointer;
-
-    current_control_stack_pointer += 2;
-    args[0] = arg0;
-    args[1] = arg1;
-
-    return call_into_lisp(function, args, 2);
-}
-
-lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
-{
-    lispobj *args = current_control_stack_pointer;
-
-    current_control_stack_pointer += 3;
-    args[0] = arg0;
-    args[1] = arg1;
-    args[2] = arg2;
-
-    return call_into_lisp(function, args, 3);
+    undoably_install_low_level_interrupt_handler(SIGILL, sigtrap_handler);
+    undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
 }
 
 void