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.
24 #include "interrupt.h"
26 #include "breakpoint.h"
30 #include "genesis/static-symbols.h"
31 #include "genesis/symbol.h"
33 #define BREAKPOINT_INST 0xcc /* INT3 */
35 unsigned long fast_random_state = 1;
41 * hacking signal contexts
43 * (This depends both on architecture, which determines what we might
44 * want to get to, and on OS, which determines how we get to it.)
48 context_eflags_addr(os_context_t *context)
51 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
52 * <sys/ucontext.h> file to define symbolic names for offsets into
53 * gregs[], but it's conditional on __USE_GNU and not defined, so
54 * we need to do this nasty absolute index magic number thing
56 return &context->uc_mcontext.gregs[16];
57 #elif defined __FreeBSD__
58 return &context->uc_mcontext.mc_eflags;
59 #elif defined __OpenBSD__
60 return &context->sc_eflags;
66 void arch_skip_instruction(os_context_t *context)
68 /* Assuming we get here via an INT3 xxx instruction, the PC now
69 * points to the interrupt code (a Lisp value) so we just move
70 * past it. Skip the code; after that, if the code is an
71 * error-trap or cerror-trap then skip the data bytes that follow. */
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 arch_remove_breakpoint(void *pc, unsigned long orig_inst)
147 *((char *)pc) = orig_inst & 0xff;
148 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
151 /* When single stepping, single_stepping holds the original instruction
153 unsigned long *single_stepping = NULL;
154 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
155 unsigned long single_step_save1;
156 unsigned long single_step_save2;
157 unsigned long single_step_save3;
161 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
163 unsigned long *pc = (unsigned long*)(*os_context_pc_addr(context));
165 /* Put the original instruction back. */
166 *((char *)pc) = orig_inst & 0xff;
167 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
169 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
170 /* Install helper instructions for the single step:
171 * pushf; or [esp],0x100; popf. */
172 single_step_save1 = *(pc-3);
173 single_step_save2 = *(pc-2);
174 single_step_save3 = *(pc-1);
175 *(pc-3) = 0x9c909090;
176 *(pc-2) = 0x00240c81;
177 *(pc-1) = 0x9d000001;
179 *context_eflags_addr(context) |= 0x100;
182 single_stepping = (unsigned int*)pc;
184 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
185 *os_context_pc_addr(context) = (char *)pc - 9;
190 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
192 int code = info->si_code;
193 os_context_t *context = (os_context_t*)void_context;
197 if (single_stepping && (signal==SIGTRAP))
199 /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
201 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
202 /* Un-install single step helper instructions. */
203 *(single_stepping-3) = single_step_save1;
204 *(single_stepping-2) = single_step_save2;
205 *(single_stepping-1) = single_step_save3;
207 *context_eflags_addr(context) ^= 0x100;
209 /* Re-install the breakpoint if possible. */
210 if (*os_context_pc_addr(context) == (int)single_stepping + 1) {
211 fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
213 *((char *)single_stepping) = BREAKPOINT_INST; /* x86 INT3 */
214 *((char *)single_stepping+1) = trap_Breakpoint;
217 single_stepping = NULL;
221 /* This is just for info in case the monitor wants to print an
223 current_control_stack_pointer =
224 (lispobj *)*os_context_sp_addr(context);
226 /* FIXME: CMUCL puts the float control restoration code here.
227 Thus, it seems to me that single-stepping won't restore the
228 float control. Since SBCL currently doesn't support
229 single-stepping (as far as I can tell) this is somewhat moot,
230 but it might be worth either moving this code up or deleting
231 the single-stepping code entirely. -- CSR, 2002-07-15 */
232 #ifdef LISP_FEATURE_LINUX
233 os_restore_fp_control(context);
236 /* On entry %eip points just after the INT3 byte and aims at the
237 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
238 * number of bytes will follow, the first is the length of the byte
239 * arguments to follow. */
240 trap = *(unsigned char *)(*os_context_pc_addr(context));
243 case trap_PendingInterrupt:
244 FSHOW((stderr, "/<trap pending interrupt>\n"));
245 arch_skip_instruction(context);
247 sigaddset(&ss,SIGTRAP);
248 sigprocmask(SIG_UNBLOCK,&ss,0);
249 interrupt_handle_pending(context);
253 /* Note: the old CMU CL code tried to save FPU state
254 * here, and restore it after we do our thing, but there
255 * seems to be no point in doing that, since we're just
256 * going to lose(..) anyway. */
257 fake_foreign_function_call(context);
258 lose("%%PRIMITIVE HALT called; the party is over.");
262 FSHOW((stderr, "<trap error/cerror %d>\n", code));
263 interrupt_internal_error(signal, info, context, code==trap_Cerror);
266 case trap_Breakpoint:
267 (char*)(*os_context_pc_addr(context)) -= 1;
268 handle_breakpoint(signal, info, context);
271 case trap_FunEndBreakpoint:
272 (char*)(*os_context_pc_addr(context)) -= 1;
273 *os_context_pc_addr(context) =
274 (int)handle_fun_end_breakpoint(signal, info, context);
278 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
279 signal, code, context));
280 interrupt_handle_now(signal, info, context);
286 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
287 os_context_t *context = (os_context_t*)void_context;
288 fake_foreign_function_call(context);
289 monitor_or_something();
293 arch_install_interrupt_handlers()
295 SHOW("entering arch_install_interrupt_handlers()");
297 /* Note: The old CMU CL code here used sigtrap_handler() to handle
298 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
299 * things that way. So, I changed to separate handlers when
300 * debugging a problem on OpenBSD, where SBCL wasn't catching
301 * SIGILL properly, but was instead letting the process be
302 * terminated with an "Illegal instruction" output. If this change
303 * turns out to break something (maybe breakpoint handling on some
304 * OS I haven't tested on?) and we have to go back to the old CMU
305 * CL way, I hope there will at least be a comment to explain
306 * why.. -- WHN 2001-06-07 */
307 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
308 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
310 SHOW("returning from arch_install_interrupt_handlers()");
313 /* This is implemented in assembly language and called from C: */
315 call_into_lisp(lispobj fun, lispobj *args, int nargs);
317 /* These functions are an interface to the Lisp call-in facility.
318 * Since this is C we can know nothing about the calling environment.
319 * The control stack might be the C stack if called from the monitor
320 * or the Lisp stack if called as a result of an interrupt or maybe
321 * even a separate stack. The args are most likely on that stack but
322 * could be in registers depending on what the compiler likes. So we
323 * copy the args into a portable vector and let the assembly language
324 * call-in function figure it out. */
327 funcall0(lispobj function)
329 lispobj *args = NULL;
331 FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
332 return call_into_lisp(function, args, 0);
335 funcall1(lispobj function, lispobj arg0)
339 return call_into_lisp(function, args, 1);
342 funcall2(lispobj function, lispobj arg0, lispobj arg1)
347 return call_into_lisp(function, args, 2);
350 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
356 return call_into_lisp(function, args, 3);
360 #ifdef LISP_FEATURE_LINKAGE_TABLE
361 /* FIXME: It might be cleaner to generate these from the lisp side of
366 arch_write_linkage_table_jmp(char * reloc, void * fun)
368 unsigned long addr = (unsigned long) fun;
371 *reloc++ = 0xFF; /* Opcode for near jump to absolute reg/mem64. */
372 *reloc++ = 0x25; /* ModRM #b00 100 101, i.e. RIP-relative. */
373 *reloc++ = 0x00; /* 32-bit displacement field = 0 */
374 *reloc++ = 0x00; /* ... */
375 *reloc++ = 0x00; /* ... */
376 *reloc++ = 0x00; /* ... */
378 for (i = 0; i < 8; i++) {
379 *reloc++ = addr & 0xff;
383 /* write a nop for good measure. */
388 arch_write_linkage_table_ref(void * reloc, void * data)
390 *(unsigned long *)reloc = (unsigned long)data;