X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fruntime%2Fppc-arch.c;h=bdd1a402359885b9c6b9421dfd9df94801bda907;hb=1bab7f87e5800ae7d0eed4e301de15125e72bd6f;hp=35dc8438222645c677b318ce0ed7964acef9fb0a;hpb=6c605fa4b46d3fee9304c4e40e0e605aa5a24f28;p=sbcl.git diff --git a/src/runtime/ppc-arch.c b/src/runtime/ppc-arch.c index 35dc843..bdd1a40 100644 --- a/src/runtime/ppc-arch.c +++ b/src/runtime/ppc-arch.c @@ -16,11 +16,13 @@ #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" @@ -49,6 +51,9 @@ #endif #endif +/* Magic encoding for the instruction used for traps. */ +#define TRAP_INSTRUCTION(trap) ((3<<26) | (6 << 21) | (trap)) + void arch_init() { } @@ -57,7 +62,7 @@ arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context) { os_vm_address_t addr; -#if defined(LISP_FEATURE_NETBSD) +#if defined(LISP_FEATURE_NETBSD) || defined(LISP_FEATURE_OPENBSD) addr = (os_vm_address_t) (code->si_addr); #else addr = (os_vm_address_t) (*os_context_register_addr(context,PT_DAR)); @@ -84,7 +89,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_p(arch_os_get_current_thread())) + && ((*os_context_register_addr(context,reg_ALLOC)) & 4); } void @@ -104,7 +120,7 @@ arch_install_breakpoint(void *pc) { unsigned int *ptr = (unsigned int *)pc; unsigned int result = *ptr; - *ptr = (3<<26) | (5 << 21) | trap_Breakpoint; + *ptr = TRAP_INSTRUCTION(trap_Breakpoint); os_flush_icache((os_vm_address_t) pc, sizeof(unsigned int)); return result; } @@ -136,12 +152,33 @@ arch_remove_breakpoint(void *pc, unsigned int orig_inst) static unsigned int *skipped_break_addr, displaced_after_inst; static sigset_t orig_sigmask; +static boolean +should_branch(os_context_t *context, unsigned int orig_inst) +{ + /* orig_inst is a conditional branch instruction. We need to + * know if the branch will be taken if executed in context. */ + int ctr = *os_context_ctr_addr(context); + int cr = *os_context_cr_addr(context); + int bo_field = (orig_inst >> 21) & 0x1f; + int bi_field = (orig_inst >> 16) & 0x1f; + int ctr_ok; + + if (!(bo_field & 4)) ctr--; /* Decrement CTR if necessary. */ + + ctr_ok = (bo_field & 4) || ((ctr == 0) == ((bo_field & 2) == 2)); + return ctr_ok && ((bo_field & 0x10) || + !(((cr >> (31-bi_field)) ^ (bo_field >> 3)) & 1)); +} + 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 */ unsigned int *pc = (unsigned int *)(*os_context_pc_addr(context)); + unsigned int *next_pc; + int op = orig_inst >> 26; + int sub_op = (orig_inst & 0x7fe) >> 1; /* XL-form sub-opcode */ orig_sigmask = *os_context_sigmask_addr(context); sigaddset_blockable(os_context_sigmask_addr(context)); @@ -149,6 +186,54 @@ 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; + + /* Figure out where we will end up after running the displaced + * instruction by defaulting to the next instruction in the stream + * and then checking for branch instructions. FIXME: This will + * probably screw up if it attempts to step a trap instruction. */ + next_pc = pc + 1; + + if (op == 18) { + /* Branch I-form */ + unsigned int displacement = orig_inst & 0x03fffffc; + /* Sign extend */ + if (displacement & 0x02000000) { + displacement |= 0xc0000000; + } + if (orig_inst & 2) { /* Absolute Address */ + next_pc = (unsigned int *)displacement; + } else { + next_pc = (unsigned int *)(((unsigned int)pc) + displacement); + } + } else if ((op == 16) + && should_branch(context, orig_inst)) { + /* Branch Conditional B-form */ + unsigned int displacement = orig_inst & 0x0000fffc; + /* Sign extend */ + if (displacement & 0x00008000) { + displacement |= 0xffff0000; + } + if (orig_inst & 2) { /* Absolute Address */ + next_pc = (unsigned int *)displacement; + } else { + next_pc = (unsigned int *)(((unsigned int)pc) + displacement); + } + } else if ((op == 19) && (sub_op == 16) + && should_branch(context, orig_inst)) { + /* Branch Conditional to Link Register XL-form */ + next_pc = (unsigned int *) + ((*os_context_lr_addr(context)) & ~3); + } else if ((op == 19) && (sub_op == 528) + && should_branch(context, orig_inst)) { + /* Branch Conditional to Count Register XL-form */ + next_pc = (unsigned int *) + ((*os_context_ctr_addr(context)) & ~3); + } + + /* Set the "after" breakpoint. */ + displaced_after_inst = *next_pc; + *next_pc = TRAP_INSTRUCTION(trap_AfterBreakpoint); + os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned int)); } #ifdef LISP_FEATURE_GENCGC @@ -231,11 +316,9 @@ 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? - */ - were_in_lisp = !foreign_function_call_active; + /* 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_p(arch_os_get_current_thread()); if (were_in_lisp) { fake_foreign_function_call(context); @@ -342,7 +425,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); @@ -387,13 +476,12 @@ arch_handle_fun_end_breakpoint(os_context_t *context) =(int)handle_fun_end_breakpoint(context); } -/* FIXME: AFTER-BREAKPOINT-TRAP is defined for PPC, but never - * emitted as far as I can see. Should it be emitted, do removed - * entirely? */ void arch_handle_after_breakpoint(os_context_t *context) { - *skipped_break_addr = trap_Breakpoint; + *skipped_break_addr = TRAP_INSTRUCTION(trap_Breakpoint); + os_flush_icache((os_vm_address_t) skipped_break_addr, + sizeof(unsigned int)); skipped_break_addr = NULL; *(unsigned int *)*os_context_pc_addr(context) = displaced_after_inst; @@ -416,9 +504,6 @@ 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); @@ -434,9 +519,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 @@ -444,78 +526,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; - - if (!maybe_handle_trap(context,trap)) - interrupt_handle_now(signal, siginfo, context); - -#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(context, 0); -#ifdef LISP_FEATURE_DARWIN - DARWIN_FIX_CONTEXT(context); -#endif 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 @@ -608,9 +634,6 @@ arch_write_linkage_table_jmp(void* reloc_addr, void *target_addr) inst = (19 << 26) | (20 << 21) | (528 << 1); *inst_ptr++ = inst; - - *inst_ptr++ = inst; - os_flush_icache((os_vm_address_t) reloc_addr, (char*) inst_ptr - (char*) reloc_addr); }