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.
23 #include "interrupt.h"
25 #include "breakpoint.h"
29 #include "genesis/static-symbols.h"
30 #include "genesis/symbol.h"
32 #define BREAKPOINT_INST 0xcc /* INT3 */
34 unsigned long fast_random_state = 1;
40 * hacking signal contexts
42 * (This depends both on architecture, which determines what we might
43 * want to get to, and on OS, which determines how we get to it.)
47 context_eflags_addr(os_context_t *context)
50 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
51 * <sys/ucontext.h> file to define symbolic names for offsets into
52 * gregs[], but it's conditional on __USE_GNU and not defined, so
53 * we need to do this nasty absolute index magic number thing
55 return &context->uc_mcontext.gregs[16];
56 #elif defined __FreeBSD__
57 return &context->uc_mcontext.mc_eflags;
58 #elif defined __OpenBSD__
59 return &context->sc_eflags;
65 void arch_skip_instruction(os_context_t *context)
67 /* Assuming we get here via an INT3 xxx instruction, the PC now
68 * points to the interrupt code (a Lisp value) so we just move
69 * past it. Skip the code; after that, if the code is an
70 * error-trap or cerror-trap then skip the data bytes that follow. */
75 FSHOW((stderr, "/[arch_skip_inst at %x]\n", *os_context_pc_addr(context)));
77 /* Get and skip the Lisp interrupt code. */
78 code = *(char*)(*os_context_pc_addr(context))++;
83 /* Lisp error arg vector length */
84 vlen = *(char*)(*os_context_pc_addr(context))++;
85 /* Skip Lisp error arg data bytes. */
87 ( (char*)(*os_context_pc_addr(context)) )++;
91 case trap_Breakpoint: /* not tested */
92 case trap_FunEndBreakpoint: /* not tested */
95 case trap_PendingInterrupt:
97 /* only needed to skip the Code */
101 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
106 "/[arch_skip_inst resuming at %x]\n",
107 *os_context_pc_addr(context)));
111 arch_internal_error_arguments(os_context_t *context)
113 return 1 + (unsigned char *)(*os_context_pc_addr(context));
117 arch_pseudo_atomic_atomic(os_context_t *context)
119 return SymbolValue(PSEUDO_ATOMIC_ATOMIC,arch_os_get_current_thread());
123 arch_set_pseudo_atomic_interrupted(os_context_t *context)
125 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1),
126 arch_os_get_current_thread());
130 * This stuff seems to get called for TRACE and debug activity.
134 arch_install_breakpoint(void *pc)
136 unsigned long result = *(unsigned long*)pc;
138 *(char*)pc = BREAKPOINT_INST; /* x86 INT3 */
139 *((char*)pc+1) = trap_Breakpoint; /* Lisp trap code */
145 get_spinlock(lispobj *word,int value)
152 : "r" (value), "m" (*word)
158 arch_remove_breakpoint(void *pc, unsigned long orig_inst)
160 *((char *)pc) = orig_inst & 0xff;
161 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
164 /* When single stepping, single_stepping holds the original instruction
166 unsigned int *single_stepping = NULL;
167 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
168 unsigned int single_step_save1;
169 unsigned int single_step_save2;
170 unsigned int single_step_save3;
174 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
176 unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
178 /* Put the original instruction back. */
179 *((char *)pc) = orig_inst & 0xff;
180 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
182 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
183 /* Install helper instructions for the single step:
184 * pushf; or [esp],0x100; popf. */
185 single_step_save1 = *(pc-3);
186 single_step_save2 = *(pc-2);
187 single_step_save3 = *(pc-1);
188 *(pc-3) = 0x9c909090;
189 *(pc-2) = 0x00240c81;
190 *(pc-1) = 0x9d000001;
192 *context_eflags_addr(context) |= 0x100;
195 single_stepping = (unsigned int*)pc;
197 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
198 *os_context_pc_addr(context) = (char *)pc - 9;
203 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
205 int code = info->si_code;
206 os_context_t *context = (os_context_t*)void_context;
209 if (single_stepping && (signal==SIGTRAP))
211 /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
213 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
214 /* Un-install single step helper instructions. */
215 *(single_stepping-3) = single_step_save1;
216 *(single_stepping-2) = single_step_save2;
217 *(single_stepping-1) = single_step_save3;
219 *context_eflags_addr(context) ^= 0x100;
221 /* Re-install the breakpoint if possible. */
222 if (*os_context_pc_addr(context) == (int)single_stepping + 1) {
223 fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
225 *((char *)single_stepping) = BREAKPOINT_INST; /* x86 INT3 */
226 *((char *)single_stepping+1) = trap_Breakpoint;
229 single_stepping = NULL;
233 /* This is just for info in case the monitor wants to print an
235 current_control_stack_pointer =
236 (lispobj *)*os_context_sp_addr(context);
238 /* FIXME: CMUCL puts the float control restoration code here.
239 Thus, it seems to me that single-stepping won't restore the
240 float control. Since SBCL currently doesn't support
241 single-stepping (as far as I can tell) this is somewhat moot,
242 but it might be worth either moving this code up or deleting
243 the single-stepping code entirely. -- CSR, 2002-07-15 */
244 #ifdef LISP_FEATURE_LINUX
245 os_restore_fp_control(context);
248 /* On entry %eip points just after the INT3 byte and aims at the
249 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
250 * number of bytes will follow, the first is the length of the byte
251 * arguments to follow. */
252 trap = *(unsigned char *)(*os_context_pc_addr(context));
255 case trap_PendingInterrupt:
256 FSHOW((stderr, "/<trap pending interrupt>\n"));
257 arch_skip_instruction(context);
258 interrupt_handle_pending(context);
262 /* Note: the old CMU CL code tried to save FPU state
263 * here, and restore it after we do our thing, but there
264 * seems to be no point in doing that, since we're just
265 * going to lose(..) anyway. */
266 fake_foreign_function_call(context);
267 lose("%%PRIMITIVE HALT called; the party is over.");
271 FSHOW((stderr, "<trap error/cerror %d>\n", code));
272 interrupt_internal_error(signal, info, context, code==trap_Cerror);
275 case trap_Breakpoint:
276 (char*)(*os_context_pc_addr(context)) -= 1;
277 handle_breakpoint(signal, info, context);
280 case trap_FunEndBreakpoint:
281 (char*)(*os_context_pc_addr(context)) -= 1;
282 *os_context_pc_addr(context) =
283 (int)handle_fun_end_breakpoint(signal, info, context);
287 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
288 signal, code, context));
289 interrupt_handle_now(signal, info, context);
295 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
296 os_context_t *context = (os_context_t*)void_context;
297 fake_foreign_function_call(context);
298 monitor_or_something();
302 arch_install_interrupt_handlers()
304 SHOW("entering arch_install_interrupt_handlers()");
306 /* Note: The old CMU CL code here used sigtrap_handler() to handle
307 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
308 * things that way. So, I changed to separate handlers when
309 * debugging a problem on OpenBSD, where SBCL wasn't catching
310 * SIGILL properly, but was instead letting the process be
311 * terminated with an "Illegal instruction" output. If this change
312 * turns out to break something (maybe breakpoint handling on some
313 * OS I haven't tested on?) and we have to go back to the old CMU
314 * CL way, I hope there will at least be a comment to explain
315 * why.. -- WHN 2001-06-07 */
316 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
317 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
319 SHOW("returning from arch_install_interrupt_handlers()");
322 /* This is implemented in assembly language and called from C: */
324 call_into_lisp(lispobj fun, lispobj *args, int nargs);
326 /* These functions are an interface to the Lisp call-in facility.
327 * Since this is C we can know nothing about the calling environment.
328 * The control stack might be the C stack if called from the monitor
329 * or the Lisp stack if called as a result of an interrupt or maybe
330 * even a separate stack. The args are most likely on that stack but
331 * could be in registers depending on what the compiler likes. So we
332 * copy the args into a portable vector and let the assembly language
333 * call-in function figure it out. */
336 funcall0(lispobj function)
338 lispobj *args = NULL;
340 FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
341 return call_into_lisp(function, args, 0);
344 funcall1(lispobj function, lispobj arg0)
348 return call_into_lisp(function, args, 1);
351 funcall2(lispobj function, lispobj arg0, lispobj arg1)
356 return call_into_lisp(function, args, 2);
359 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
365 return call_into_lisp(function, args, 3);