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 */
28 #include "interrupt.h"
30 #include "breakpoint.h"
33 extern char call_into_lisp_LRA[], call_into_lisp_end[];
35 extern size_t os_vm_page_size;
36 #define BREAKPOINT_INST 0x80
42 /* This must be called _after_ os_init(), so that we know what the
45 if (mmap((os_vm_address_t) call_into_lisp_LRA_page,os_vm_page_size,
46 OS_VM_PROT_ALL,MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,-1,0)
47 == (os_vm_address_t) -1)
50 /* call_into_lisp_LRA is a collection of trampolines written in asm -
51 * see alpha-assem.S. We copy it to call_into_lisp_LRA_page where
52 * VOPs and things can find it. (I don't know why they can't find it
53 * where it was to start with.) */
54 bcopy(call_into_lisp_LRA,(void *)call_into_lisp_LRA_page,os_vm_page_size);
56 os_flush_icache((os_vm_address_t)call_into_lisp_LRA_page,
62 arch_get_bad_addr (int sig, siginfo_t *code, os_context_t *context)
66 /* Instructions are 32 bit quantities. */
68 /* fprintf(stderr,"arch_get_bad_addr %d %p %p\n",
69 sig, code, context); */
70 pc= (unsigned int *)(*os_context_pc_addr(context));
72 if (((unsigned long)pc) & 3) {
73 return NULL; /* In what case would pc be unaligned?? */
76 if ( (pc < READ_ONLY_SPACE_START ||
77 pc >= READ_ONLY_SPACE_START+READ_ONLY_SPACE_SIZE) &&
78 (pc < current_dynamic_space ||
79 pc >= current_dynamic_space + DYNAMIC_SPACE_SIZE))
82 return context->uc_mcontext.sc_traparg_a0;
86 arch_skip_instruction(os_context_t *context)
88 /* This may be complete rubbish, as (at least for traps) pc points
89 * _after_ the instruction that caused us to be here anyway.
91 ((char*)*os_context_pc_addr(context)) +=4; }
94 arch_internal_error_arguments(os_context_t *context)
96 return (unsigned char *)(*os_context_pc_addr(context)+4);
100 arch_pseudo_atomic_atomic(os_context_t *context)
102 return ((*os_context_register_addr(context,reg_ALLOC)) & 1);
105 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
107 /* On coming out of an atomic section, we subtract 1 from
108 * reg_Alloc, then try to store something at that address. So,
109 * to signal that it was interrupted and a signal should be handled,
110 * we set bit 63 of reg_ALLOC here so that the end-of-atomic code
111 * will raise SIGSEGV (no ram mapped there). We catch the signal
112 * (see the appropriate *-os.c) and call interrupt_handle_pending()
113 * for the saved signal instead */
115 *os_context_register_addr(context,reg_ALLOC) |= (1L<<63);
118 unsigned int arch_install_breakpoint(void *pc)
120 unsigned int *ptr = (unsigned int *)pc;
121 unsigned int result = *ptr;
122 *ptr = BREAKPOINT_INST;
124 os_flush_icache((os_vm_address_t)ptr, sizeof(unsigned int));
129 void arch_remove_breakpoint(void *pc, unsigned int orig_inst)
131 unsigned int *ptr = (unsigned int *)pc;
133 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned int));
136 static unsigned int *skipped_break_addr, displaced_after_inst,
140 /* This returns a PC value. Lisp code is all in the 32-bit-addressable
141 * space, so we should be ok with an unsigned int. */
143 emulate_branch(os_context_t *context, unsigned int orig_inst)
145 int op = orig_inst >> 26;
146 int reg_a = (orig_inst >> 21) & 0x1f;
147 int reg_b = (orig_inst >> 16) & 0x1f;
149 (orig_inst&(1<<20)) ?
150 orig_inst | (-1 << 21) :
152 int next_pc = *os_context_pc_addr(context);
153 int branch = 0; /* was NULL; */
156 case 0x1a: /* jmp, jsr, jsr_coroutine, ret */
157 *os_context_register_addr(context,reg_a) =
158 *os_context_pc_addr(context);
159 *os_context_pc_addr(context) =
160 *os_context_register_addr(context,reg_b)& ~3;
163 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
166 case 0x31: /* fbeq */
167 if (*(os_context_float_register_addr(context,reg_a))==0) branch = 1;
169 case 0x32: /* fblt */
170 if (*os_context_float_register_addr(context,reg_a)<0) branch = 1;
172 case 0x33: /* fble */
173 if (*os_context_float_register_addr(context,reg_a)<=0) branch = 1;
176 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
179 case 0x35: /* fbne */
180 if (*os_context_register_addr(context,reg_a)!=0) branch = 1;
182 case 0x36: /* fbge */
183 if (*os_context_float_register_addr(context,reg_a)>=0) branch = 1;
185 case 0x37: /* fbgt */
186 if (*os_context_float_register_addr(context,reg_a)>0) branch = 1;
188 case 0x38: /* blbc */
189 if ((*os_context_register_addr(context,reg_a)&1) == 0) branch = 1;
192 if (*os_context_register_addr(context,reg_a)==0) branch = 1;
195 if (*os_context_register_addr(context,reg_a)<0) branch = 1;
198 if (*os_context_register_addr(context,reg_a)<=0) branch = 1;
200 case 0x3c: /* blbs */
201 if ((*os_context_register_addr(context,reg_a)&1)!=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;
210 if (*os_context_register_addr(context,reg_a)>0) branch = 1;
218 static sigset_t orig_sigmask;
220 /* Perform the instruction that we overwrote with a breakpoint. As we
221 * don't have a single-step facility, this means we have to:
222 * - put the instruction back
223 * - put a second breakpoint at the following instruction,
224 * set after_breakpoint and continue execution.
226 * When the second breakpoint is hit (very shortly thereafter, we hope)
227 * sigtrap_handler gets called again, but follows the AfterBreakpoint
229 * - puts a bpt back in the first breakpoint place (running across a
230 * breakpoint shouldn't cause it to be uninstalled)
231 * - replaces the second bpt with the instruction it was meant to be
237 void arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
239 /* Apparent off-by-one errors ahoy. If you consult the Alpha ARM,
240 * it will tell you that after a BPT, the saved PC is the address
241 * of the instruction _after_ the instruction that caused the trap.
243 * However, we decremented PC by 4 before calling the Lisp-level
244 * handler that calls this routine (see alpha-arch.c line 322 and
245 * friends) so when we get to this point PC is actually pointing
246 * at the BPT instruction itself. This is good, because this is
247 * where we want to restart execution when we do that */
249 unsigned int *pc=(unsigned int *)(*os_context_pc_addr(context));
250 unsigned int *next_pc;
251 int op = orig_inst >> 26;;
253 orig_sigmask = *os_context_sigmask_addr(context);
254 sigaddset_blockable(os_context_sigmask_addr(context));
256 /* Put the original instruction back. */
258 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned int));
259 skipped_break_addr = pc;
261 /* Figure out where we will end up after running the displaced
263 if (op == 0x1a || (op&0xf) == 0x30) /* a branch */
264 /* The cast to long is just to shut gcc up. */
265 next_pc = (unsigned int *)((long)emulate_branch(context,orig_inst));
269 /* Set the after breakpoint. */
270 displaced_after_inst = *next_pc;
271 *next_pc = BREAKPOINT_INST;
273 os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned int));
277 sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
280 #ifdef LISP_FEATURE_LINUX
281 os_restore_fp_control(context);
284 /* this is different from how CMUCL does it. CMUCL used "call_pal
285 * PAL_gentrap", which doesn't do anything on Linux (unless NL0
286 * contains certain specific values). We use "bugchk" instead.
287 * It's (for our purposes) just the same as bpt but has a
288 * different opcode so we can test whether we're dealing with a
289 * breakpoint or a "system service" */
291 if ((*(unsigned int*)(*os_context_pc_addr(context)-4))==BREAKPOINT_INST) {
292 if (after_breakpoint) {
293 /* see comments above arch_do_displaced_inst. This is where
294 * we reinsert the breakpoint that we removed earlier */
296 *os_context_pc_addr(context) -=4;
297 *skipped_break_addr = BREAKPOINT_INST;
298 os_flush_icache((os_vm_address_t)skipped_break_addr,
299 sizeof(unsigned int));
300 skipped_break_addr = NULL;
301 *(unsigned int *)*os_context_pc_addr(context) =
302 displaced_after_inst;
303 os_flush_icache((os_vm_address_t)*os_context_pc_addr(context), sizeof(unsigned int));
304 *os_context_sigmask_addr(context)= orig_sigmask;
305 after_breakpoint=0; /* false */
308 code = trap_Breakpoint;
310 /* a "system service" */
311 code=*((u32 *)(*os_context_pc_addr(context)));
314 case trap_PendingInterrupt:
315 arch_skip_instruction(context);
316 interrupt_handle_pending(context);
320 fake_foreign_function_call(context);
321 lose("%%primitive halt called; the party is over.\n");
325 interrupt_internal_error(signal, siginfo, context, code==trap_Cerror);
328 case trap_Breakpoint: /* call lisp-level handler */
329 *os_context_pc_addr(context) -=4;
330 handle_breakpoint(signal, siginfo, context);
333 case trap_FunEndBreakpoint:
334 *os_context_pc_addr(context) -=4;
335 *os_context_pc_addr(context) =
336 (int)handle_fun_end_breakpoint(signal, siginfo, context);
340 fprintf(stderr, "unidentified breakpoint/trap %d\n",code);
341 interrupt_handle_now(signal, siginfo, context);
347 arch_get_fp_control()
349 return ieee_get_fp_control();
353 arch_set_fp_control(unsigned long fp)
355 ieee_set_fp_control(fp);
359 void arch_install_interrupt_handlers()
361 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
364 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
366 lispobj funcall0(lispobj function)
368 lispobj *args = current_control_stack_pointer;
370 return call_into_lisp(function, args, 0);
373 lispobj funcall1(lispobj function, lispobj arg0)
375 lispobj *args = current_control_stack_pointer;
377 current_control_stack_pointer += 1;
380 return call_into_lisp(function, args, 1);
383 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
385 lispobj *args = current_control_stack_pointer;
387 current_control_stack_pointer += 2;
391 return call_into_lisp(function, args, 2);
394 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
396 lispobj *args = current_control_stack_pointer;
398 current_control_stack_pointer += 3;
403 return call_into_lisp(function, args, 3);