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.
22 #include "interrupt.h"
24 #include "breakpoint.h"
26 #include "pseudo-atomic.h"
28 #include "genesis/static-symbols.h"
29 #include "genesis/symbol.h"
31 #define BREAKPOINT_INST 0xcc /* INT3 */
32 #define UD2_INST 0x0b0f /* UD2 */
34 #ifndef LISP_FEATURE_UD2_BREAKPOINTS
35 #define BREAKPOINT_WIDTH 1
37 #define BREAKPOINT_WIDTH 2
40 unsigned long fast_random_state = 1;
45 #ifndef LISP_FEATURE_WIN32
47 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
49 return (os_vm_address_t)code->si_addr;
55 * hacking signal contexts
57 * (This depends both on architecture, which determines what we might
58 * want to get to, and on OS, which determines how we get to it.)
62 context_eflags_addr(os_context_t *context)
64 #if defined __linux__ || defined __sun
65 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
66 * <sys/ucontext.h> file to define symbolic names for offsets into
67 * gregs[], but it's conditional on __USE_GNU and not defined, so
68 * we need to do this nasty absolute index magic number thing
70 return &context->uc_mcontext.gregs[16];
71 #elif defined __FreeBSD__
72 return &context->uc_mcontext.mc_eflags;
73 #elif defined __OpenBSD__
74 return &context->sc_eflags;
75 #elif defined LISP_FEATURE_DARWIN
76 return (int *)(&context->uc_mcontext->SS.EFLAGS);
77 #elif defined __NetBSD__
78 return &(context->uc_mcontext.__gregs[_REG_EFL]);
79 #elif defined LISP_FEATURE_WIN32
80 return (int *)&context->win32_context->EFlags;
86 void arch_skip_instruction(os_context_t *context)
88 /* Assuming we get here via an INT3 xxx instruction, the PC now
89 * points to the interrupt code (a Lisp value) so we just move
90 * past it. Skip the code; after that, if the code is an
91 * error-trap or cerror-trap then skip the data bytes that follow. */
97 /* Get and skip the Lisp interrupt code. */
98 code = *(char*)(*os_context_pc_addr(context))++;
103 /* Lisp error arg vector length */
104 vlen = *(char*)(*os_context_pc_addr(context))++;
105 /* Skip Lisp error arg data bytes. */
107 ++*os_context_pc_addr(context);
111 case trap_Breakpoint: /* not tested */
112 case trap_FunEndBreakpoint: /* not tested */
115 #ifdef LISP_FEATURE_SB_SAFEPOINT
116 case trap_GlobalSafepoint:
117 case trap_CspSafepoint:
119 case trap_PendingInterrupt:
121 case trap_SingleStepAround:
122 case trap_SingleStepBefore:
123 /* only needed to skip the Code */
127 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
132 "/[arch_skip_inst resuming at %x]\n",
133 *os_context_pc_addr(context)));
137 arch_internal_error_arguments(os_context_t *context)
139 return 1 + (unsigned char *)(*os_context_pc_addr(context));
143 arch_pseudo_atomic_atomic(os_context_t *context)
145 return get_pseudo_atomic_atomic(arch_os_get_current_thread());
149 arch_set_pseudo_atomic_interrupted(os_context_t *context)
151 struct thread *thread = arch_os_get_current_thread();
152 set_pseudo_atomic_interrupted(thread);
156 arch_clear_pseudo_atomic_interrupted(os_context_t *context)
158 struct thread *thread = arch_os_get_current_thread();
159 clear_pseudo_atomic_interrupted(thread);
163 * This stuff seems to get called for TRACE and debug activity.
167 arch_install_breakpoint(void *pc)
169 unsigned int result = *(unsigned int*)pc;
171 #ifndef LISP_FEATURE_UD2_BREAKPOINTS
172 *(char*)pc = BREAKPOINT_INST; /* x86 INT3 */
173 *((char*)pc+1) = trap_Breakpoint; /* Lisp trap code */
175 *(char*)pc = UD2_INST & 0xff;
176 *((char*)pc+1) = UD2_INST >> 8;
177 *((char*)pc+2) = trap_Breakpoint;
184 arch_remove_breakpoint(void *pc, unsigned int orig_inst)
186 *((char *)pc) = orig_inst & 0xff;
187 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
188 #if BREAKPOINT_WIDTH > 1
189 *((char *)pc + 2) = (orig_inst & 0xff0000) >> 16;
193 /* When single stepping, single_stepping holds the original instruction
195 unsigned int *single_stepping = NULL;
196 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
197 unsigned int single_step_save1;
198 unsigned int single_step_save2;
199 unsigned int single_step_save3;
203 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
205 unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
207 /* Put the original instruction back. */
208 arch_remove_breakpoint(pc, orig_inst);
210 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
211 /* Install helper instructions for the single step:
212 * pushf; or [esp],0x100; popf. */
213 single_step_save1 = *(pc-3);
214 single_step_save2 = *(pc-2);
215 single_step_save3 = *(pc-1);
216 *(pc-3) = 0x9c909090;
217 *(pc-2) = 0x00240c81;
218 *(pc-1) = 0x9d000001;
220 *context_eflags_addr(context) |= 0x100;
223 single_stepping = pc;
225 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
226 *os_context_pc_addr(context) = (os_context_register_t)((char *)pc - 9);
231 restore_breakpoint_from_single_step(os_context_t * context)
233 /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
234 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
235 /* Un-install single step helper instructions. */
236 *(single_stepping-3) = single_step_save1;
237 *(single_stepping-2) = single_step_save2;
238 *(single_stepping-1) = single_step_save3;
240 *context_eflags_addr(context) &= ~0x100;
242 /* Re-install the breakpoint if possible. */
243 if (((char *)*os_context_pc_addr(context) >
244 (char *)single_stepping) &&
245 ((char *)*os_context_pc_addr(context) <=
246 (char *)single_stepping + BREAKPOINT_WIDTH)) {
247 fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
249 arch_install_breakpoint(single_stepping);
252 single_stepping = NULL;
257 arch_handle_breakpoint(os_context_t *context)
259 *os_context_pc_addr(context) -= BREAKPOINT_WIDTH;
260 handle_breakpoint(context);
264 arch_handle_fun_end_breakpoint(os_context_t *context)
266 *os_context_pc_addr(context) -= BREAKPOINT_WIDTH;
267 *os_context_pc_addr(context) =
268 (int)handle_fun_end_breakpoint(context);
272 arch_handle_single_step_trap(os_context_t *context, int trap)
274 arch_skip_instruction(context);
275 /* On x86 the fdefn / function is always in EAX, so we pass 0
276 * as the register_offset. */
277 handle_single_step_trap(context, trap, 0);
280 #ifndef LISP_FEATURE_WIN32
282 sigtrap_handler(int signal, siginfo_t *info, os_context_t *context)
286 if (single_stepping) {
287 restore_breakpoint_from_single_step(context);
291 /* This is just for info in case the monitor wants to print an
293 access_control_stack_pointer(arch_os_get_current_thread()) =
294 (lispobj *)*os_context_sp_addr(context);
296 #ifdef LISP_FEATURE_SUNOS
297 /* For some reason the breakpoints that :ENCAPSULATE NIL tracing sets up
298 * cause a trace trap (i.e. processor single-stepping trap) on the following
299 * instruction on Solaris 10/x86. -- JES, 2006-04-07
301 if (info->si_code == TRAP_TRACE) {
307 /* On entry %eip points just after the INT3 byte and aims at the
308 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
309 * number of bytes will follow, the first is the length of the byte
310 * arguments to follow. */
311 trap = *(unsigned char *)(*os_context_pc_addr(context));
312 handle_trap(context, trap);
316 sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
317 /* Triggering SIGTRAP using int3 is unreliable on OS X/x86, so
318 * we need to use illegal instructions for traps.
320 #if defined(LISP_FEATURE_UD2_BREAKPOINTS) && !defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
321 if (*((unsigned short *)*os_context_pc_addr(context)) == UD2_INST) {
322 *os_context_pc_addr(context) += 2;
323 return sigtrap_handler(signal, siginfo, context);
326 fake_foreign_function_call(context);
327 lose("Unhandled SIGILL");
329 #endif /* not LISP_FEATURE_WIN32 */
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 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
347 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
348 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
350 SHOW("returning from arch_install_interrupt_handlers()");
353 #ifdef LISP_FEATURE_LINKAGE_TABLE
354 /* FIXME: It might be cleaner to generate these from the lisp side of
359 arch_write_linkage_table_jmp(char * reloc, void * fun)
361 /* Make JMP to function entry. JMP offset is calculated from next
364 long offset = (char *)fun - (reloc + 5);
367 *reloc++ = 0xe9; /* opcode for JMP rel32 */
368 for (i = 0; i < 4; i++) {
369 *reloc++ = offset & 0xff;
373 /* write a nop for good measure. */
378 arch_write_linkage_table_ref(void * reloc, void * data)
380 *(unsigned long *)reloc = (unsigned long)data;