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;
39 #ifndef LISP_FEATURE_WIN32
41 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
43 return (os_vm_address_t)code->si_addr;
49 * hacking signal contexts
51 * (This depends both on architecture, which determines what we might
52 * want to get to, and on OS, which determines how we get to it.)
56 context_eflags_addr(os_context_t *context)
58 #if defined __linux__ || defined __sun
59 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
60 * <sys/ucontext.h> file to define symbolic names for offsets into
61 * gregs[], but it's conditional on __USE_GNU and not defined, so
62 * we need to do this nasty absolute index magic number thing
64 return &context->uc_mcontext.gregs[16];
65 #elif defined __FreeBSD__
66 return &context->uc_mcontext.mc_eflags;
67 #elif defined __OpenBSD__
68 return &context->sc_eflags;
69 #elif defined LISP_FEATURE_DARWIN
70 return &context->uc_mcontext->ss.eflags;
71 #elif defined __NetBSD__
72 return &(context->uc_mcontext.__gregs[_REG_EFL]);
73 #elif defined LISP_FEATURE_WIN32
74 return (int *)&context->EFlags;
80 void arch_skip_instruction(os_context_t *context)
82 /* Assuming we get here via an INT3 xxx instruction, the PC now
83 * points to the interrupt code (a Lisp value) so we just move
84 * past it. Skip the code; after that, if the code is an
85 * error-trap or cerror-trap then skip the data bytes that follow. */
91 /* Get and skip the Lisp interrupt code. */
92 code = *(char*)(*os_context_pc_addr(context))++;
97 /* Lisp error arg vector length */
98 vlen = *(char*)(*os_context_pc_addr(context))++;
99 /* Skip Lisp error arg data bytes. */
101 ++*os_context_pc_addr(context);
105 case trap_Breakpoint: /* not tested */
106 case trap_FunEndBreakpoint: /* not tested */
109 case trap_PendingInterrupt:
111 /* only needed to skip the Code */
115 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
120 "/[arch_skip_inst resuming at %x]\n",
121 *os_context_pc_addr(context)));
125 arch_internal_error_arguments(os_context_t *context)
127 return 1 + (unsigned char *)(*os_context_pc_addr(context));
131 arch_pseudo_atomic_atomic(os_context_t *context)
133 return SymbolValue(PSEUDO_ATOMIC_ATOMIC,arch_os_get_current_thread());
137 arch_set_pseudo_atomic_interrupted(os_context_t *context)
139 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1),
140 arch_os_get_current_thread());
144 arch_clear_pseudo_atomic_interrupted(os_context_t *context)
146 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0),
147 arch_os_get_current_thread());
151 * This stuff seems to get called for TRACE and debug activity.
155 arch_install_breakpoint(void *pc)
157 unsigned int result = *(unsigned int*)pc;
159 *(char*)pc = BREAKPOINT_INST; /* x86 INT3 */
160 *((char*)pc+1) = trap_Breakpoint; /* Lisp trap code */
166 arch_remove_breakpoint(void *pc, unsigned int orig_inst)
168 *((char *)pc) = orig_inst & 0xff;
169 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
172 /* When single stepping, single_stepping holds the original instruction
174 unsigned int *single_stepping = NULL;
175 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
176 unsigned int single_step_save1;
177 unsigned int single_step_save2;
178 unsigned int single_step_save3;
182 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
184 unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
186 /* Put the original instruction back. */
187 *((char *)pc) = orig_inst & 0xff;
188 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
190 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
191 /* Install helper instructions for the single step:
192 * pushf; or [esp],0x100; popf. */
193 single_step_save1 = *(pc-3);
194 single_step_save2 = *(pc-2);
195 single_step_save3 = *(pc-1);
196 *(pc-3) = 0x9c909090;
197 *(pc-2) = 0x00240c81;
198 *(pc-1) = 0x9d000001;
200 *context_eflags_addr(context) |= 0x100;
203 single_stepping = pc;
205 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
206 *os_context_pc_addr(context) = (char *)pc - 9;
211 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
213 os_context_t *context = (os_context_t*)void_context;
216 #ifndef LISP_FEATURE_WIN32
217 if (single_stepping && (signal==SIGTRAP))
219 /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
221 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
222 /* Un-install single step helper instructions. */
223 *(single_stepping-3) = single_step_save1;
224 *(single_stepping-2) = single_step_save2;
225 *(single_stepping-1) = single_step_save3;
227 *context_eflags_addr(context) &= ~0x100;
229 /* Re-install the breakpoint if possible. */
230 if (*os_context_pc_addr(context) == (int)single_stepping + 1) {
231 fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
233 *((char *)single_stepping) = BREAKPOINT_INST; /* x86 INT3 */
234 *((char *)single_stepping+1) = trap_Breakpoint;
237 single_stepping = NULL;
242 /* This is just for info in case the monitor wants to print an
244 current_control_stack_pointer =
245 (lispobj *)*os_context_sp_addr(context);
247 /* FIXME: CMUCL puts the float control restoration code here.
248 Thus, it seems to me that single-stepping won't restore the
249 float control. Since SBCL currently doesn't support
250 single-stepping (as far as I can tell) this is somewhat moot,
251 but it might be worth either moving this code up or deleting
252 the single-stepping code entirely. -- CSR, 2002-07-15 */
253 #ifdef LISP_FEATURE_LINUX
254 os_restore_fp_control(context);
258 #ifdef LISP_FEATURE_SUNOS
259 /* For some reason the breakpoints that :ENCAPSULATE NIL tracing sets up
260 * cause a trace trap (i.e. processor single-stepping trap) on the following
261 * instruction on Solaris 10/x86. -- JES, 2006-04-07
263 if (info->si_code == TRAP_TRACE) {
269 /* On entry %eip points just after the INT3 byte and aims at the
270 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
271 * number of bytes will follow, the first is the length of the byte
272 * arguments to follow. */
273 trap = *(unsigned char *)(*os_context_pc_addr(context));
274 /* FSHOW((stderr, "/<sigtrap trap %d at pc_addr: %p>\n", trap, *os_context_pc_addr(context))); */
277 case trap_PendingInterrupt:
278 FSHOW((stderr, "/<trap pending interrupt>\n"));
279 arch_skip_instruction(context);
280 interrupt_handle_pending(context);
284 /* Note: the old CMU CL code tried to save FPU state
285 * here, and restore it after we do our thing, but there
286 * seems to be no point in doing that, since we're just
287 * going to lose(..) anyway. */
288 fake_foreign_function_call(context);
289 lose("%%PRIMITIVE HALT called; the party is over.\n");
293 FSHOW((stderr, "<trap error/cerror %d>\n", trap));
294 interrupt_internal_error(signal, info, context, trap==trap_Cerror);
297 case trap_Breakpoint:
298 --*os_context_pc_addr(context);
299 handle_breakpoint(signal, info, context);
302 case trap_FunEndBreakpoint:
303 --*os_context_pc_addr(context);
304 *os_context_pc_addr(context) =
305 (int)handle_fun_end_breakpoint(signal, info, context);
309 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
310 signal, trap, context));
311 interrupt_handle_now(signal, info, context);
317 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
318 os_context_t *context = (os_context_t*)void_context;
320 #if defined(LISP_FEATURE_DARWIN)
321 if (*((unsigned short *)*os_context_pc_addr(context)) == 0x0b0f) {
322 *os_context_pc_addr(context) += 2;
323 return sigtrap_handler(signal, siginfo, void_context);
327 fake_foreign_function_call(context);
328 monitor_or_something();
332 arch_install_interrupt_handlers()
334 SHOW("entering arch_install_interrupt_handlers()");
336 /* Note: The old CMU CL code here used sigtrap_handler() to handle
337 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
338 * things that way. So, I changed to separate handlers when
339 * debugging a problem on OpenBSD, where SBCL wasn't catching
340 * SIGILL properly, but was instead letting the process be
341 * terminated with an "Illegal instruction" output. If this change
342 * turns out to break something (maybe breakpoint handling on some
343 * OS I haven't tested on?) and we have to go back to the old CMU
344 * CL way, I hope there will at least be a comment to explain
345 * why.. -- WHN 2001-06-07 */
346 #ifndef LISP_FEATURE_WIN32
347 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
348 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
351 SHOW("returning from arch_install_interrupt_handlers()");
354 /* This is implemented in assembly language and called from C: */
356 call_into_lisp(lispobj fun, lispobj *args, int nargs);
358 /* These functions are an interface to the Lisp call-in facility.
359 * Since this is C we can know nothing about the calling environment.
360 * The control stack might be the C stack if called from the monitor
361 * or the Lisp stack if called as a result of an interrupt or maybe
362 * even a separate stack. The args are most likely on that stack but
363 * could be in registers depending on what the compiler likes. So we
364 * copy the args into a portable vector and let the assembly language
365 * call-in function figure it out. */
368 funcall0(lispobj function)
370 lispobj *args = NULL;
372 FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
373 return call_into_lisp(function, args, 0);
376 funcall1(lispobj function, lispobj arg0)
380 return call_into_lisp(function, args, 1);
383 funcall2(lispobj function, lispobj arg0, lispobj arg1)
388 return call_into_lisp(function, args, 2);
391 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
397 return call_into_lisp(function, args, 3);
400 #ifdef LISP_FEATURE_LINKAGE_TABLE
401 /* FIXME: It might be cleaner to generate these from the lisp side of
406 arch_write_linkage_table_jmp(char * reloc, void * fun)
408 /* Make JMP to function entry. JMP offset is calculated from next
411 long offset = (char *)fun - (reloc + 5);
414 *reloc++ = 0xe9; /* opcode for JMP rel32 */
415 for (i = 0; i < 4; i++) {
416 *reloc++ = offset & 0xff;
420 /* write a nop for good measure. */
425 arch_write_linkage_table_ref(void * reloc, void * data)
427 *(unsigned long *)reloc = (unsigned long)data;