2 * This software is part of the SBCL system. See the README file for
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
12 /* Note that although superficially it appears that we use
13 * os_context_t like we ought to, we actually just assume its a
14 * ucontext in places. Naughty */
18 #include <asm/pal.h> /* for PAL_gentrap */
29 #include "interrupt.h"
31 #include "breakpoint.h"
34 extern char call_into_lisp_LRA[], call_into_lisp_end[];
35 extern size_t os_vm_page_size;
36 #define BREAKPOINT_INST 0x80
41 /* This must be called _after_ os_init, so we know what the page
43 if(mmap((os_vm_address_t) call_into_lisp_LRA_page,os_vm_page_size,
44 OS_VM_PROT_ALL,MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,-1,0)
45 == (os_vm_address_t) -1)
48 /* call_into_lisp_LRA is a collection of trampolines written in asm -
49 * see alpha-assem.S. We copy it to call_into_lisp_LRA_page where
50 * VOPs and things can find it. (I don't know why they can't find it
51 * where it was to start with.) */
52 bcopy(call_into_lisp_LRA,(void *)call_into_lisp_LRA_page,os_vm_page_size);
54 os_flush_icache((os_vm_address_t)call_into_lisp_LRA_page,
60 arch_get_bad_addr (int sig, siginfo_t *code, os_context_t *context)
64 /* Instructions are 32 bit quantities. */
66 /* fprintf(stderr,"arch_get_bad_addr %d %p %p\n",
67 sig, code, context); */
68 pc= (unsigned int *)(*os_context_pc_addr(context));
70 if(((unsigned long)pc) & 3) {
71 return NULL; /* In what case would pc be unaligned?? */
74 if( (pc < READ_ONLY_SPACE_START ||
75 pc >= READ_ONLY_SPACE_START+READ_ONLY_SPACE_SIZE) &&
76 (pc < current_dynamic_space ||
77 pc >= current_dynamic_space + DYNAMIC_SPACE_SIZE))
82 if(((badinst>>27)!=0x16) /* STL or STQ */
83 && ((badinst>>27)!=0x13)) /* STS or STT */
84 return NULL; /* Otherwise forget about address. */
86 return (os_vm_address_t)
87 (*os_context_register_addr(context,((badinst>>16)&0x1f))
92 arch_skip_instruction(os_context_t *context)
94 /* This may be complete rubbish, as (at least for traps) pc points
95 * _after_ the instruction that caused us to be here anyway.
97 ((char*)*os_context_pc_addr(context)) +=4; }
100 arch_internal_error_arguments(os_context_t *context)
102 return (unsigned char *)(*os_context_pc_addr(context)+4);
106 arch_pseudo_atomic_atomic(os_context_t *context)
108 return ((*os_context_register_addr(context,reg_ALLOC)) & 1);
111 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
113 /* On coming out of an atomic section, we subtract 1 from
114 * reg_Alloc, then try to store something at that address. On
115 * OSF/1 we add 1 to reg_Alloc here so that the end-of-atomic code
116 * will raise SIGTRAP for "unaligned access". Linux catches
117 * unaligned accesses in the kernel and fixes them up[1], so there
118 * we toggle bit 63 instead. The resulting address is somewhere
119 * out in no-man's land, so we get SIGSEGV when we try to access
120 * it. We catch whichever signal it is (see the appropriate
121 * *-os.c) and call interrupt_handle_pending() from it */
123 /* [1] This behaviour can be changed with osf_setsysinfo, but cmucl
127 *os_context_register_addr(context,reg_ALLOC) |= (1L<<63);
129 *os_context_register_addr(context,reg_ALLOC) |= 2;
133 /* XXX but is the caller of this storing all 64 bits? */
134 unsigned long arch_install_breakpoint(void *pc)
136 unsigned int *ptr = (unsigned int *)pc;
137 unsigned long result = (unsigned long) *ptr;
138 *ptr = BREAKPOINT_INST;
139 *(ptr+1)=trap_Breakpoint;
141 os_flush_icache((os_vm_address_t)ptr, sizeof(unsigned long));
146 void arch_remove_breakpoint(void *pc, unsigned long orig_inst)
148 /* was (unsigned int) but gcc complains. Changed to mirror
149 * install_breakpoint() above */
150 unsigned long *ptr=(unsigned long *)pc;
152 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
155 static unsigned int *skipped_break_addr, displaced_after_inst,
159 /* This returns a PC value. Lisp code is all in the 32-bit-addressable
160 * space,so we should be ok with an unsigned int. */
162 emulate_branch(os_context_t *context,unsigned long orig_inst)
164 int op = orig_inst >> 26;
165 int reg_a = (orig_inst >> 21) & 0x1f;
166 int reg_b = (orig_inst >> 16) & 0x1f;
167 int fn = orig_inst & 0xffff;
168 int disp = (orig_inst&(1<<20)) ? orig_inst | (-1 << 21) : orig_inst&0x1fffff;
169 int next_pc = *os_context_pc_addr(context);
170 int branch = 0; /* was NULL; */
173 case 0x1a: /* jmp, jsr, jsr_coroutine, ret */
174 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
175 *os_context_pc_addr(context)=*os_context_register_addr(context,reg_b)& ~3;
178 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
181 case 0x31: /* fbeq */
182 if(*(os_context_fpregister_addr(context,reg_a))==0) branch = 1;
184 case 0x32: /* fblt */
185 if(*os_context_fpregister_addr(context,reg_a)<0) branch = 1;
187 case 0x33: /* fble */
188 if(*os_context_fpregister_addr(context,reg_a)<=0) branch = 1;
191 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
194 case 0x35: /* fbne */
195 if(*os_context_register_addr(context,reg_a)!=0) branch = 1;
197 case 0x36: /* fbge */
198 if(*os_context_fpregister_addr(context,reg_a)>=0) branch = 1;
200 case 0x37: /* fbgt */
201 if(*os_context_fpregister_addr(context,reg_a)>0) branch = 1;
203 case 0x38: /* blbc */
204 if((*os_context_register_addr(context,reg_a)&1) == 0) branch = 1;
207 if(*os_context_register_addr(context,reg_a)==0) branch = 1;
210 if(*os_context_register_addr(context,reg_a)<0) branch = 1;
213 if(*os_context_register_addr(context,reg_a)<=0) branch = 1;
215 case 0x3c: /* blbs */
216 if((*os_context_register_addr(context,reg_a)&1)!=0) branch = 1;
219 if(*os_context_register_addr(context,reg_a)!=0) branch = 1;
222 if(*os_context_register_addr(context,reg_a)>=0) branch = 1;
225 if(*os_context_register_addr(context,reg_a)>0) branch = 1;
228 if(branch) next_pc += disp*4;
232 static sigset_t orig_sigmask;
234 void arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
236 unsigned int *pc=(unsigned int *)(*os_context_pc_addr(context));
237 unsigned int *next_pc;
238 unsigned int next_inst;
239 int op = orig_inst >> 26;;
240 fprintf(stderr,"arch_do_displaced_inst depends on sigreturn, which is not implemented and will\nalways fail\n");
241 orig_sigmask = *os_context_sigmask_addr(context);
242 sigaddset_blockable(os_context_sigmask_addr(context));
244 /* Figure out where the displaced inst is going. */
245 if(op == 0x1a || (op&0xf) == 0x30) /* branch...ugh */
246 /* The cast to long is just to shut gcc up. */
247 next_pc = (unsigned int *)((long)emulate_branch(context,orig_inst));
251 /* Put the original instruction back. */
253 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
254 skipped_break_addr = pc;
256 /* Set the after breakpoint. */
257 displaced_after_inst = *next_pc;
258 *next_pc = BREAKPOINT_INST;
260 os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned long));
266 #define AfterBreakpoint 100
269 sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
270 fake_foreign_function_call(context);
275 sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
277 /* Don't disallow recursive breakpoint traps. Otherwise, we can't */
278 /* use debugger breakpoints anywhere in here. */
280 sigset_t *mask=(os_context_sigmask_addr(context));
282 fprintf(stderr,"sigtrap_handler:signal %d context=%p ",signal,context);
285 /* this is different from how CMUCL does it. CMUCL used
286 * "call_pal PAL_gentrap", which doesn't do anything on Linux
287 * so screwed up our offsets in odd ways. We use "bpt" instead
290 /* probably we should
291 assert(*(unsigned int*)(*os_context_pc_addr(context)-4) == BREAKPOINT_INST)
292 but I've not decided a good way to handle it if it turns out not to be
294 code=*((u32 *)(*os_context_pc_addr(context)));
295 fprintf(stderr,"pc=%lx code=%d, inst=%x\n",
296 *os_context_pc_addr(context), code,
297 *(unsigned int*)(*os_context_pc_addr(context)-4));
299 case trap_PendingInterrupt:
300 arch_skip_instruction(context);
301 interrupt_handle_pending(context);
305 fake_foreign_function_call(context);
306 lose("%%primitive halt called; the party is over.\n");
310 interrupt_internal_error(signal, siginfo, context, code==trap_Cerror);
313 case trap_Breakpoint:
314 *os_context_pc_addr(context) -=4;
315 handle_breakpoint(signal, siginfo, context);
318 case trap_FunctionEndBreakpoint:
319 *os_context_pc_addr(context) -=4;
320 *os_context_pc_addr(context) = (int)handle_function_end_breakpoint(signal, siginfo, context);
323 case AfterBreakpoint:
324 *os_context_pc_addr(context) -=4;
325 *skipped_break_addr = BREAKPOINT_INST;
326 os_flush_icache((os_vm_address_t)skipped_break_addr,
327 sizeof(unsigned long));
328 skipped_break_addr = NULL;
329 *(unsigned int *)*os_context_pc_addr(context) = displaced_after_inst;
330 os_flush_icache((os_vm_address_t)*os_context_pc_addr(context), sizeof(unsigned long));
331 *os_context_sigmask_addr(context)= orig_sigmask;
332 after_breakpoint=0; /* NULL; */
336 interrupt_handle_now(signal, siginfo, context);
341 #define FIXNUM_VALUE(lispobj) (((int)lispobj)>>2)
343 static void sigfpe_handler(int signal, int code, os_context_t *context)
347 void arch_install_interrupt_handlers()
349 undoably_install_low_level_interrupt_handler(SIGILL, sigill_handler);
350 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
351 undoably_install_low_level_interrupt_handler(SIGFPE, sigfpe_handler);
354 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
356 lispobj funcall0(lispobj function)
358 lispobj *args = current_control_stack_pointer;
360 return call_into_lisp(function, args, 0);
363 lispobj funcall1(lispobj function, lispobj arg0)
365 lispobj *args = current_control_stack_pointer;
367 current_control_stack_pointer += 1;
370 return call_into_lisp(function, args, 1);
373 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
375 lispobj *args = current_control_stack_pointer;
377 current_control_stack_pointer += 2;
381 return call_into_lisp(function, args, 2);
384 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
386 lispobj *args = current_control_stack_pointer;
388 current_control_stack_pointer += 3;
393 return call_into_lisp(function, args, 3);
397 /* This is apparently called by emulate_branch, but isn't defined. So */
398 /* just do nothing and hope it works... */
399 void cacheflush(void)
401 /* hoping probably isn't _actually_ enough. we should call_pal imb,
402 according to the arch ref manual