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"
27 #include "pseudo-atomic.h"
29 #include "genesis/static-symbols.h"
30 #include "genesis/symbol.h"
32 #define BREAKPOINT_INST 0xcc /* INT3 */
33 #define UD2_INST 0x0b0f /* UD2 */
35 #ifndef LISP_FEATURE_UD2_BREAKPOINTS
36 #define BREAKPOINT_WIDTH 1
38 #define BREAKPOINT_WIDTH 2
41 unsigned long fast_random_state = 1;
46 #ifndef LISP_FEATURE_WIN32
48 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
50 return (os_vm_address_t)code->si_addr;
56 * hacking signal contexts
58 * (This depends both on architecture, which determines what we might
59 * want to get to, and on OS, which determines how we get to it.)
63 context_eflags_addr(os_context_t *context)
65 #if defined __linux__ || defined __sun
66 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
67 * <sys/ucontext.h> file to define symbolic names for offsets into
68 * gregs[], but it's conditional on __USE_GNU and not defined, so
69 * we need to do this nasty absolute index magic number thing
71 return &context->uc_mcontext.gregs[16];
72 #elif defined __FreeBSD__
73 return &context->uc_mcontext.mc_eflags;
74 #elif defined __OpenBSD__
75 return &context->sc_eflags;
76 #elif defined LISP_FEATURE_DARWIN
77 return (int *)(&context->uc_mcontext->SS.EFLAGS);
78 #elif defined __NetBSD__
79 return &(context->uc_mcontext.__gregs[_REG_EFL]);
80 #elif defined LISP_FEATURE_WIN32
81 return (int *)&context->EFlags;
87 void arch_skip_instruction(os_context_t *context)
89 /* Assuming we get here via an INT3 xxx instruction, the PC now
90 * points to the interrupt code (a Lisp value) so we just move
91 * past it. Skip the code; after that, if the code is an
92 * error-trap or cerror-trap then skip the data bytes that follow. */
98 /* Get and skip the Lisp interrupt code. */
99 code = *(char*)(*os_context_pc_addr(context))++;
104 /* Lisp error arg vector length */
105 vlen = *(char*)(*os_context_pc_addr(context))++;
106 /* Skip Lisp error arg data bytes. */
108 ++*os_context_pc_addr(context);
112 case trap_Breakpoint: /* not tested */
113 case trap_FunEndBreakpoint: /* not tested */
116 case trap_PendingInterrupt:
118 case trap_SingleStepAround:
119 case trap_SingleStepBefore:
120 /* only needed to skip the Code */
124 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
129 "/[arch_skip_inst resuming at %x]\n",
130 *os_context_pc_addr(context)));
134 arch_internal_error_arguments(os_context_t *context)
136 return 1 + (unsigned char *)(*os_context_pc_addr(context));
140 arch_pseudo_atomic_atomic(os_context_t *context)
142 return get_pseudo_atomic_atomic(arch_os_get_current_thread());
146 arch_set_pseudo_atomic_interrupted(os_context_t *context)
148 struct thread *thread = arch_os_get_current_thread();
149 set_pseudo_atomic_interrupted(thread);
153 arch_clear_pseudo_atomic_interrupted(os_context_t *context)
155 struct thread *thread = arch_os_get_current_thread();
156 clear_pseudo_atomic_interrupted(thread);
160 * This stuff seems to get called for TRACE and debug activity.
164 arch_install_breakpoint(void *pc)
166 unsigned int result = *(unsigned int*)pc;
168 #ifndef LISP_FEATURE_UD2_BREAKPOINTS
169 *(char*)pc = BREAKPOINT_INST; /* x86 INT3 */
170 *((char*)pc+1) = trap_Breakpoint; /* Lisp trap code */
172 *(char*)pc = UD2_INST & 0xff;
173 *((char*)pc+1) = UD2_INST >> 8;
174 *((char*)pc+2) = trap_Breakpoint;
181 arch_remove_breakpoint(void *pc, unsigned int orig_inst)
183 *((char *)pc) = orig_inst & 0xff;
184 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
185 #if BREAKPOINT_WIDTH > 1
186 *((char *)pc + 2) = (orig_inst & 0xff0000) >> 16;
190 /* When single stepping, single_stepping holds the original instruction
192 unsigned int *single_stepping = NULL;
193 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
194 unsigned int single_step_save1;
195 unsigned int single_step_save2;
196 unsigned int single_step_save3;
200 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
202 unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
204 /* Put the original instruction back. */
205 arch_remove_breakpoint(pc, orig_inst);
207 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
208 /* Install helper instructions for the single step:
209 * pushf; or [esp],0x100; popf. */
210 single_step_save1 = *(pc-3);
211 single_step_save2 = *(pc-2);
212 single_step_save3 = *(pc-1);
213 *(pc-3) = 0x9c909090;
214 *(pc-2) = 0x00240c81;
215 *(pc-1) = 0x9d000001;
217 *context_eflags_addr(context) |= 0x100;
220 single_stepping = pc;
222 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
223 *os_context_pc_addr(context) = (os_context_register_t)((char *)pc - 9);
228 restore_breakpoint_from_single_step(os_context_t * context)
230 /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
231 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
232 /* Un-install single step helper instructions. */
233 *(single_stepping-3) = single_step_save1;
234 *(single_stepping-2) = single_step_save2;
235 *(single_stepping-1) = single_step_save3;
237 *context_eflags_addr(context) &= ~0x100;
239 /* Re-install the breakpoint if possible. */
240 if (((char *)*os_context_pc_addr(context) >
241 (char *)single_stepping) &&
242 ((char *)*os_context_pc_addr(context) <=
243 (char *)single_stepping + BREAKPOINT_WIDTH)) {
244 fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
246 arch_install_breakpoint(single_stepping);
249 single_stepping = NULL;
254 arch_handle_breakpoint(os_context_t *context)
256 *os_context_pc_addr(context) -= BREAKPOINT_WIDTH;
257 handle_breakpoint(context);
261 arch_handle_fun_end_breakpoint(os_context_t *context)
263 *os_context_pc_addr(context) -= BREAKPOINT_WIDTH;
264 *os_context_pc_addr(context) =
265 (int)handle_fun_end_breakpoint(context);
269 arch_handle_single_step_trap(os_context_t *context, int trap)
271 arch_skip_instruction(context);
272 /* On x86 the fdefn / function is always in EAX, so we pass 0
273 * as the register_offset. */
274 handle_single_step_trap(context, trap, 0);
277 #ifndef LISP_FEATURE_WIN32
279 sigtrap_handler(int signal, siginfo_t *info, os_context_t *context)
283 if (single_stepping) {
284 restore_breakpoint_from_single_step(context);
288 /* This is just for info in case the monitor wants to print an
290 access_control_stack_pointer(arch_os_get_current_thread()) =
291 (lispobj *)*os_context_sp_addr(context);
293 #ifdef LISP_FEATURE_SUNOS
294 /* For some reason the breakpoints that :ENCAPSULATE NIL tracing sets up
295 * cause a trace trap (i.e. processor single-stepping trap) on the following
296 * instruction on Solaris 10/x86. -- JES, 2006-04-07
298 if (info->si_code == TRAP_TRACE) {
304 /* On entry %eip points just after the INT3 byte and aims at the
305 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
306 * number of bytes will follow, the first is the length of the byte
307 * arguments to follow. */
308 trap = *(unsigned char *)(*os_context_pc_addr(context));
309 handle_trap(context, trap);
313 sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
314 /* Triggering SIGTRAP using int3 is unreliable on OS X/x86, so
315 * we need to use illegal instructions for traps.
317 #if defined(LISP_FEATURE_UD2_BREAKPOINTS) && !defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
318 if (*((unsigned short *)*os_context_pc_addr(context)) == UD2_INST) {
319 *os_context_pc_addr(context) += 2;
320 return sigtrap_handler(signal, siginfo, context);
323 fake_foreign_function_call(context);
324 lose("Unhandled SIGILL");
326 #endif /* not LISP_FEATURE_WIN32 */
329 arch_install_interrupt_handlers()
331 SHOW("entering arch_install_interrupt_handlers()");
333 /* Note: The old CMU CL code here used sigtrap_handler() to handle
334 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
335 * things that way. So, I changed to separate handlers when
336 * debugging a problem on OpenBSD, where SBCL wasn't catching
337 * SIGILL properly, but was instead letting the process be
338 * terminated with an "Illegal instruction" output. If this change
339 * turns out to break something (maybe breakpoint handling on some
340 * OS I haven't tested on?) and we have to go back to the old CMU
341 * CL way, I hope there will at least be a comment to explain
342 * why.. -- WHN 2001-06-07 */
343 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
344 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
345 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
347 SHOW("returning from arch_install_interrupt_handlers()");
350 #ifdef LISP_FEATURE_LINKAGE_TABLE
351 /* FIXME: It might be cleaner to generate these from the lisp side of
356 arch_write_linkage_table_jmp(char * reloc, void * fun)
358 /* Make JMP to function entry. JMP offset is calculated from next
361 long offset = (char *)fun - (reloc + 5);
364 *reloc++ = 0xe9; /* opcode for JMP rel32 */
365 for (i = 0; i < 4; i++) {
366 *reloc++ = offset & 0xff;
370 /* write a nop for good measure. */
375 arch_write_linkage_table_ref(void * reloc, void * data)
377 *(unsigned long *)reloc = (unsigned long)data;