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 */
20 #include <asm/pal.h> /* for PAL_gentrap */
31 #include "interrupt.h"
33 #include "breakpoint.h"
35 extern char call_into_lisp_LRA[], call_into_lisp_end[];
36 extern size_t os_vm_page_size;
37 #define BREAKPOINT_INST 0x80
41 /* this must be called _after_ os_init, so we know what the page size is */
42 if(mmap((os_vm_address_t) call_into_lisp_LRA_page,os_vm_page_size,
43 OS_VM_PROT_ALL,MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,-1,0)
44 == (os_vm_address_t) -1)
47 /* call_into_lisp_LRA is a collection of trampolines written in asm -
48 * see alpha-assem.S. We copy it to call_into_lisp_LRA_page where
49 * VOPs and things can find it (I don't know why they can't find it
50 * where it was to start with). */
51 bcopy(call_into_lisp_LRA,(void *)call_into_lisp_LRA_page,os_vm_page_size);
53 os_flush_icache((os_vm_address_t)call_into_lisp_LRA_page,
59 arch_get_bad_addr (int sig, siginfo_t *code, os_context_t *context)
63 /* instructions are 32 bit quantities */
65 /* fprintf(stderr,"arch_get_bad_addr %d %p %p\n",
66 sig, code, context); */
67 pc= (unsigned int *)(*os_context_pc_addr(context));
69 if(((unsigned long)pc) & 3)
70 return NULL; /* in what case would pc be unaligned? */
72 if( (pc < READ_ONLY_SPACE_START ||
73 pc >= READ_ONLY_SPACE_START+READ_ONLY_SPACE_SIZE) &&
74 (pc < current_dynamic_space ||
75 pc >= current_dynamic_space + DYNAMIC_SPACE_SIZE))
80 if(((badinst>>27)!=0x16) /* STL or STQ */
81 && ((badinst>>27)!=0x13)) /* STS or STT */
82 return NULL; /* Otherwise forget about address */
84 return (os_vm_address_t)
85 (*os_context_register_addr(context,((badinst>>16)&0x1f))
89 void arch_skip_instruction(os_context_t *context)
91 /* this may be complete rubbish, as (at least for traps) pc points
92 * _after_ the instruction that caused us to be here anyway
94 ((char*)*os_context_pc_addr(context)) +=4; }
96 unsigned char *arch_internal_error_arguments(os_context_t *context)
98 return (unsigned char *)(*os_context_pc_addr(context)+4);
101 boolean arch_pseudo_atomic_atomic(os_context_t *context)
103 return ((*os_context_register_addr(context,reg_ALLOC)) & 1);
106 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
108 /* On coming out of an atomic section, we subtract 1 from
109 * reg_Alloc, then try to store something at that address. On
110 * OSF/1 we add 1 to reg_Alloc here so that the end-of-atomic code
111 * will raise SIGTRAP for "unaligned access". Linux catches
112 * unaligned accesses in the kernel and fixes them up[1], so there
113 * we toggle bit 63 instead. The resulting address is somewhere
114 * out in no-man's land, so we get SIGSEGV when we try to access
115 * it. We catch whichever signal it is (see the appropriate
116 * *-os.c) and call interrupt_handle_pending() from it */
118 /* [1] This behaviour can be changed with osf_setsysinfo, but cmucl
122 *os_context_register_addr(context,reg_ALLOC) |= (1L<<63);
124 *os_context_register_addr(context,reg_ALLOC) |= 2;
128 /* XXX but is the caller of this storing all 64 bits? */
129 unsigned long arch_install_breakpoint(void *pc)
131 unsigned int *ptr = (unsigned int *)pc;
132 unsigned long result = (unsigned long) *ptr;
133 *ptr = BREAKPOINT_INST;
134 *(ptr+1)=trap_Breakpoint;
136 os_flush_icache((os_vm_address_t)ptr, sizeof(unsigned long));
141 void arch_remove_breakpoint(void *pc, unsigned long orig_inst)
143 /* was (unsigned int) but gcc complains. Changed to mirror
144 install_breakpoint above */
145 unsigned long *ptr=(unsigned long *)pc;
147 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
150 static unsigned int *skipped_break_addr, displaced_after_inst,
154 /* Returns a PC value. Lisp code is all in the 32-bit-addressable
155 space,so we should be ok with an unsigned int */
158 emulate_branch(os_context_t *context,unsigned long orig_inst)
160 int op = orig_inst >> 26;
161 int reg_a = (orig_inst >> 21) & 0x1f;
162 int reg_b = (orig_inst >> 16) & 0x1f;
163 int fn = orig_inst & 0xffff;
164 int disp = (orig_inst&(1<<20)) ? orig_inst | (-1 << 21) : orig_inst&0x1fffff;
165 int next_pc = *os_context_pc_addr(context);
166 int branch = 0; /* was NULL; */
169 case 0x1a: /* jmp, jsr, jsr_coroutine, ret */
170 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
171 *os_context_pc_addr(context)=*os_context_register_addr(context,reg_b)& ~3;
174 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
177 case 0x31: /* fbeq */
178 if(*(os_context_fpregister_addr(context,reg_a))==0) branch = 1;
180 case 0x32: /* fblt */
181 if(*os_context_fpregister_addr(context,reg_a)<0) branch = 1;
183 case 0x33: /* fble */
184 if(*os_context_fpregister_addr(context,reg_a)<=0) branch = 1;
187 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
190 case 0x35: /* fbne */
191 if(*os_context_register_addr(context,reg_a)!=0) branch = 1;
193 case 0x36: /* fbge */
194 if(*os_context_fpregister_addr(context,reg_a)>=0) branch = 1;
196 case 0x37: /* fbgt */
197 if(*os_context_fpregister_addr(context,reg_a)>0) branch = 1;
199 case 0x38: /* blbc */
200 if((*os_context_register_addr(context,reg_a)&1) == 0) branch = 1;
203 if(*os_context_register_addr(context,reg_a)==0) branch = 1;
206 if(*os_context_register_addr(context,reg_a)<0) branch = 1;
209 if(*os_context_register_addr(context,reg_a)<=0) branch = 1;
211 case 0x3c: /* blbs */
212 if((*os_context_register_addr(context,reg_a)&1)!=0) branch = 1;
215 if(*os_context_register_addr(context,reg_a)!=0) branch = 1;
218 if(*os_context_register_addr(context,reg_a)>=0) branch = 1;
221 if(*os_context_register_addr(context,reg_a)>0) branch = 1;
224 if(branch) next_pc += disp*4;
228 static sigset_t orig_sigmask;
230 void arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
232 unsigned int *pc=(unsigned int *)(*os_context_pc_addr(context));
233 unsigned int *next_pc;
234 unsigned int next_inst;
235 int op = orig_inst >> 26;;
236 fprintf(stderr,"arch_do_displaced_inst depends on sigreturn, which is not implemented and will\nalways fail\n");
237 orig_sigmask = *os_context_sigmask_addr(context);
238 sigaddset_blockable(os_context_sigmask_addr(context));
240 /* Figure out where the displaced inst is going */
241 if(op == 0x1a || (op&0xf) == 0x30) /* branch...ugh */
242 /* the cast to long is just to shut gcc up */
243 next_pc = (unsigned int *)((long)emulate_branch(context,orig_inst));
247 /* Put the original instruction back. */
249 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
250 skipped_break_addr = pc;
252 /* set the after breakpoint */
253 displaced_after_inst = *next_pc;
254 *next_pc = BREAKPOINT_INST;
256 os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned long));
258 ldb_monitor("sigreturn is not implemented and just failed");
262 #define AfterBreakpoint 100
264 static void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
265 fake_foreign_function_call(context);
269 static void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
271 /* Don't disallow recursive breakpoint traps. Otherwise, we can't */
272 /* use debugger breakpoints anywhere in here. */
274 sigset_t *mask=(os_context_sigmask_addr(context));
276 fprintf(stderr,"sigtrap_handler:signal %d context=%p ",signal,context);
279 /* this is different from how CMUCL does it. CMUCL used
280 * "call_pal PAL_gentrap", which doesn't do anything on Linux
281 * so screwed up our offsets in odd ways. We use "bpt" instead
284 /* probably we should
285 assert(*(unsigned int*)(*os_context_pc_addr(context)-4) == BREAKPOINT_INST)
286 but I've not decided a good way to handle it if it turns out not to be
288 code=*((u32 *)(*os_context_pc_addr(context)));
289 fprintf(stderr,"pc=%lx code=%d, inst=%x\n",
290 *os_context_pc_addr(context), code,
291 *(unsigned int*)(*os_context_pc_addr(context)-4));
293 case trap_PendingInterrupt:
294 arch_skip_instruction(context);
295 interrupt_handle_pending(context);
299 fake_foreign_function_call(context);
300 lose("%%primitive halt called; the party is over.\n");
304 interrupt_internal_error(signal, siginfo, context, code==trap_Cerror);
307 case trap_Breakpoint:
308 *os_context_pc_addr(context) -=4;
309 handle_breakpoint(signal, siginfo, context);
312 case trap_FunctionEndBreakpoint:
313 *os_context_pc_addr(context) -=4;
314 *os_context_pc_addr(context) = (int)handle_function_end_breakpoint(signal, siginfo, context);
317 case AfterBreakpoint:
318 *os_context_pc_addr(context) -=4;
319 *skipped_break_addr = BREAKPOINT_INST;
320 os_flush_icache((os_vm_address_t)skipped_break_addr,
321 sizeof(unsigned long));
322 skipped_break_addr = NULL;
323 *(unsigned int *)*os_context_pc_addr(context) = displaced_after_inst;
324 os_flush_icache((os_vm_address_t)*os_context_pc_addr(context), sizeof(unsigned long));
325 *os_context_sigmask_addr(context)= orig_sigmask;
326 after_breakpoint=0; /* NULL; */
330 interrupt_handle_now(signal, siginfo, context);
335 #define FIXNUM_VALUE(lispobj) (((int)lispobj)>>2)
337 static void sigfpe_handler(int signal, int code, os_context_t *context)
341 void arch_install_interrupt_handlers()
343 interrupt_install_low_level_handler(SIGILL,sigill_handler);
344 interrupt_install_low_level_handler(SIGTRAP,sigtrap_handler);
345 interrupt_install_low_level_handler(SIGFPE,sigfpe_handler);
348 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
350 lispobj funcall0(lispobj function)
352 lispobj *args = current_control_stack_pointer;
354 return call_into_lisp(function, args, 0);
357 lispobj funcall1(lispobj function, lispobj arg0)
359 lispobj *args = current_control_stack_pointer;
361 current_control_stack_pointer += 1;
364 return call_into_lisp(function, args, 1);
367 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
369 lispobj *args = current_control_stack_pointer;
371 current_control_stack_pointer += 2;
375 return call_into_lisp(function, args, 2);
378 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
380 lispobj *args = current_control_stack_pointer;
382 current_control_stack_pointer += 3;
387 return call_into_lisp(function, args, 3);
391 /* This is apparently called by emulate_branch, but isn't defined. So */
392 /* just do nothing and hope it works... */
394 void cacheflush(void)
396 /* hoping probably isn't _actually_ enough. we should call_pal imb,
397 according to the arch ref manual