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"
33 extern char call_into_lisp_LRA[], call_into_lisp_end[];
34 extern size_t os_vm_page_size;
35 #define BREAKPOINT_INST 0x80
39 /* this must be called _after_ os_init, so we know what the page size is */
40 if(mmap((os_vm_address_t) call_into_lisp_LRA_page,os_vm_page_size,
41 OS_VM_PROT_ALL,MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,-1,0)
42 == (os_vm_address_t) -1)
45 /* call_into_lisp_LRA is a collection of trampolines written in asm -
46 * see alpha-assem.S. We copy it to call_into_lisp_LRA_page where
47 * VOPs and things can find it (I don't know why they can't find it
48 * where it was to start with). */
49 bcopy(call_into_lisp_LRA,(void *)call_into_lisp_LRA_page,os_vm_page_size);
51 os_flush_icache((os_vm_address_t)call_into_lisp_LRA_page,
57 arch_get_bad_addr (int sig, siginfo_t *code, os_context_t *context)
61 /* instructions are 32 bit quantities */
63 /* fprintf(stderr,"arch_get_bad_addr %d %p %p\n",
64 sig, code, context); */
65 pc= (unsigned int *)(*os_context_pc_addr(context));
67 if(((unsigned long)pc) & 3)
68 return NULL; /* in what case would pc be unaligned? */
70 if( (pc < READ_ONLY_SPACE_START ||
71 pc >= READ_ONLY_SPACE_START+READ_ONLY_SPACE_SIZE) &&
72 (pc < current_dynamic_space ||
73 pc >= current_dynamic_space + DYNAMIC_SPACE_SIZE))
78 if(((badinst>>27)!=0x16) /* STL or STQ */
79 && ((badinst>>27)!=0x13)) /* STS or STT */
80 return NULL; /* Otherwise forget about address */
82 return (os_vm_address_t)
83 (*os_context_register_addr(context,((badinst>>16)&0x1f))
87 void arch_skip_instruction(os_context_t *context)
89 /* this may be complete rubbish, as (at least for traps) pc points
90 * _after_ the instruction that caused us to be here anyway
92 ((char*)*os_context_pc_addr(context)) +=4; }
94 unsigned char *arch_internal_error_arguments(os_context_t *context)
96 return (unsigned char *)(*os_context_pc_addr(context)+4);
99 boolean arch_pseudo_atomic_atomic(os_context_t *context)
101 return ((*os_context_register_addr(context,reg_ALLOC)) & 1);
104 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
106 /* On coming out of an atomic section, we subtract 1 from
107 * reg_Alloc, then try to store something at that address. On
108 * OSF/1 we add 1 to reg_Alloc here so that the end-of-atomic code
109 * will raise SIGTRAP for "unaligned access". Linux catches
110 * unaligned accesses in the kernel and fixes them up[1], so there
111 * we toggle bit 63 instead. The resulting address is somewhere
112 * out in no-man's land, so we get SIGSEGV when we try to access
113 * it. We catch whichever signal it is (see the appropriate
114 * *-os.c) and call interrupt_handle_pending() from it */
116 /* [1] This behaviour can be changed with osf_setsysinfo, but cmucl
120 *os_context_register_addr(context,reg_ALLOC) |= (1L<<63);
122 *os_context_register_addr(context,reg_ALLOC) |= 2;
126 /* XXX but is the caller of this storing all 64 bits? */
127 unsigned long arch_install_breakpoint(void *pc)
129 unsigned int *ptr = (unsigned int *)pc;
130 unsigned long result = (unsigned long) *ptr;
131 *ptr = BREAKPOINT_INST;
132 *(ptr+1)=trap_Breakpoint;
134 os_flush_icache((os_vm_address_t)ptr, sizeof(unsigned long));
139 void arch_remove_breakpoint(void *pc, unsigned long orig_inst)
141 /* was (unsigned int) but gcc complains. Changed to mirror
142 install_breakpoint above */
143 unsigned long *ptr=(unsigned long *)pc;
145 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
148 static unsigned int *skipped_break_addr, displaced_after_inst,
152 /* Returns a PC value. Lisp code is all in the 32-bit-addressable
153 space,so we should be ok with an unsigned int */
156 emulate_branch(os_context_t *context,unsigned long orig_inst)
158 int op = orig_inst >> 26;
159 int reg_a = (orig_inst >> 21) & 0x1f;
160 int reg_b = (orig_inst >> 16) & 0x1f;
161 int fn = orig_inst & 0xffff;
162 int disp = (orig_inst&(1<<20)) ? orig_inst | (-1 << 21) : orig_inst&0x1fffff;
163 int next_pc = *os_context_pc_addr(context);
164 int branch = 0; /* was NULL; */
167 case 0x1a: /* jmp, jsr, jsr_coroutine, ret */
168 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
169 *os_context_pc_addr(context)=*os_context_register_addr(context,reg_b)& ~3;
172 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
175 case 0x31: /* fbeq */
176 if(*(os_context_fpregister_addr(context,reg_a))==0) branch = 1;
178 case 0x32: /* fblt */
179 if(*os_context_fpregister_addr(context,reg_a)<0) branch = 1;
181 case 0x33: /* fble */
182 if(*os_context_fpregister_addr(context,reg_a)<=0) branch = 1;
185 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
188 case 0x35: /* fbne */
189 if(*os_context_register_addr(context,reg_a)!=0) branch = 1;
191 case 0x36: /* fbge */
192 if(*os_context_fpregister_addr(context,reg_a)>=0) branch = 1;
194 case 0x37: /* fbgt */
195 if(*os_context_fpregister_addr(context,reg_a)>0) branch = 1;
197 case 0x38: /* blbc */
198 if((*os_context_register_addr(context,reg_a)&1) == 0) branch = 1;
201 if(*os_context_register_addr(context,reg_a)==0) branch = 1;
204 if(*os_context_register_addr(context,reg_a)<0) branch = 1;
207 if(*os_context_register_addr(context,reg_a)<=0) branch = 1;
209 case 0x3c: /* blbs */
210 if((*os_context_register_addr(context,reg_a)&1)!=0) branch = 1;
213 if(*os_context_register_addr(context,reg_a)!=0) branch = 1;
216 if(*os_context_register_addr(context,reg_a)>=0) branch = 1;
219 if(*os_context_register_addr(context,reg_a)>0) branch = 1;
222 if(branch) next_pc += disp*4;
226 static sigset_t orig_sigmask;
228 void arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
230 unsigned int *pc=(unsigned int *)(*os_context_pc_addr(context));
231 unsigned int *next_pc;
232 unsigned int next_inst;
233 int op = orig_inst >> 26;;
234 fprintf(stderr,"arch_do_displaced_inst depends on sigreturn, which is not implemented and will\nalways fail\n");
235 orig_sigmask = *os_context_sigmask_addr(context);
236 sigaddset_blockable(os_context_sigmask_addr(context));
238 /* Figure out where the displaced inst is going */
239 if(op == 0x1a || (op&0xf) == 0x30) /* branch...ugh */
240 /* the cast to long is just to shut gcc up */
241 next_pc = (unsigned int *)((long)emulate_branch(context,orig_inst));
245 /* Put the original instruction back. */
247 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
248 skipped_break_addr = pc;
250 /* set the after breakpoint */
251 displaced_after_inst = *next_pc;
252 *next_pc = BREAKPOINT_INST;
254 os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned long));
256 ldb_monitor("sigreturn is not implemented and just failed");
260 #define AfterBreakpoint 100
262 static void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
263 fake_foreign_function_call(context);
267 static void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
269 /* Don't disallow recursive breakpoint traps. Otherwise, we can't */
270 /* use debugger breakpoints anywhere in here. */
272 sigset_t *mask=(os_context_sigmask_addr(context));
274 fprintf(stderr,"sigtrap_handler:signal %d context=%p ",signal,context);
277 /* this is different from how CMUCL does it. CMUCL used
278 * "call_pal PAL_gentrap", which doesn't do anything on Linux
279 * so screwed up our offsets in odd ways. We use "bpt" instead
282 /* probably we should
283 assert(*(unsigned int*)(*os_context_pc_addr(context)-4) == BREAKPOINT_INST)
284 but I've not decided a good way to handle it if it turns out not to be
286 code=*((u32 *)(*os_context_pc_addr(context)));
287 fprintf(stderr,"pc=%lx code=%d, inst=%x\n",
288 *os_context_pc_addr(context), code,
289 *(unsigned int*)(*os_context_pc_addr(context)-4));
291 case trap_PendingInterrupt:
292 arch_skip_instruction(context);
293 interrupt_handle_pending(context);
297 fake_foreign_function_call(context);
298 lose("%%primitive halt called; the party is over.\n");
302 interrupt_internal_error(signal, siginfo, context, code==trap_Cerror);
305 case trap_Breakpoint:
306 *os_context_pc_addr(context) -=4;
307 handle_breakpoint(signal, siginfo, context);
310 case trap_FunctionEndBreakpoint:
311 *os_context_pc_addr(context) -=4;
312 *os_context_pc_addr(context) = (int)handle_function_end_breakpoint(signal, siginfo, context);
315 case AfterBreakpoint:
316 *os_context_pc_addr(context) -=4;
317 *skipped_break_addr = BREAKPOINT_INST;
318 os_flush_icache((os_vm_address_t)skipped_break_addr,
319 sizeof(unsigned long));
320 skipped_break_addr = NULL;
321 *(unsigned int *)*os_context_pc_addr(context) = displaced_after_inst;
322 os_flush_icache((os_vm_address_t)*os_context_pc_addr(context), sizeof(unsigned long));
323 *os_context_sigmask_addr(context)= orig_sigmask;
324 after_breakpoint=0; /* NULL; */
328 interrupt_handle_now(signal, siginfo, context);
333 #define FIXNUM_VALUE(lispobj) (((int)lispobj)>>2)
335 static void sigfpe_handler(int signal, int code, os_context_t *context)
339 void arch_install_interrupt_handlers()
341 undoably_install_low_level_interrupt_handler(SIGILL, sigill_handler);
342 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
343 undoably_install_low_level_interrupt_handler(SIGFPE, sigfpe_handler);
346 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
348 lispobj funcall0(lispobj function)
350 lispobj *args = current_control_stack_pointer;
352 return call_into_lisp(function, args, 0);
355 lispobj funcall1(lispobj function, lispobj arg0)
357 lispobj *args = current_control_stack_pointer;
359 current_control_stack_pointer += 1;
362 return call_into_lisp(function, args, 1);
365 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
367 lispobj *args = current_control_stack_pointer;
369 current_control_stack_pointer += 2;
373 return call_into_lisp(function, args, 2);
376 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
378 lispobj *args = current_control_stack_pointer;
380 current_control_stack_pointer += 3;
385 return call_into_lisp(function, args, 3);
389 /* This is apparently called by emulate_branch, but isn't defined. So */
390 /* just do nothing and hope it works... */
392 void cacheflush(void)
394 /* hoping probably isn't _actually_ enough. we should call_pal imb,
395 according to the arch ref manual