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 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
42 return (os_vm_address_t)code->si_addr;
47 * hacking signal contexts
49 * (This depends both on architecture, which determines what we might
50 * want to get to, and on OS, which determines how we get to it.)
54 context_eflags_addr(os_context_t *context)
56 #if defined __linux__ || defined __sun
57 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
58 * <sys/ucontext.h> file to define symbolic names for offsets into
59 * gregs[], but it's conditional on __USE_GNU and not defined, so
60 * we need to do this nasty absolute index magic number thing
62 return &context->uc_mcontext.gregs[16];
63 #elif defined __FreeBSD__
64 return &context->uc_mcontext.mc_eflags;
65 #elif defined __OpenBSD__
66 return &context->sc_eflags;
67 #elif defined __NetBSD__
68 return &(context->uc_mcontext.__gregs[_REG_EFL]);
74 void arch_skip_instruction(os_context_t *context)
76 /* Assuming we get here via an INT3 xxx instruction, the PC now
77 * points to the interrupt code (a Lisp value) so we just move
78 * past it. Skip the code; after that, if the code is an
79 * error-trap or cerror-trap then skip the data bytes that follow. */
85 /* Get and skip the Lisp interrupt code. */
86 code = *(char*)(*os_context_pc_addr(context))++;
91 /* Lisp error arg vector length */
92 vlen = *(char*)(*os_context_pc_addr(context))++;
93 /* Skip Lisp error arg data bytes. */
95 ++*os_context_pc_addr(context);
99 case trap_Breakpoint: /* not tested */
100 case trap_FunEndBreakpoint: /* not tested */
103 case trap_PendingInterrupt:
105 /* only needed to skip the Code */
109 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
114 "/[arch_skip_inst resuming at %x]\n",
115 *os_context_pc_addr(context)));
119 arch_internal_error_arguments(os_context_t *context)
121 return 1 + (unsigned char *)(*os_context_pc_addr(context));
125 arch_pseudo_atomic_atomic(os_context_t *context)
127 return SymbolValue(PSEUDO_ATOMIC_ATOMIC,arch_os_get_current_thread());
131 arch_set_pseudo_atomic_interrupted(os_context_t *context)
133 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1),
134 arch_os_get_current_thread());
138 arch_clear_pseudo_atomic_interrupted(os_context_t *context)
140 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0),
141 arch_os_get_current_thread());
145 * This stuff seems to get called for TRACE and debug activity.
149 arch_install_breakpoint(void *pc)
151 unsigned int result = *(unsigned int*)pc;
153 *(char*)pc = BREAKPOINT_INST; /* x86 INT3 */
154 *((char*)pc+1) = trap_Breakpoint; /* Lisp trap code */
160 arch_remove_breakpoint(void *pc, unsigned int orig_inst)
162 *((char *)pc) = orig_inst & 0xff;
163 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
166 /* When single stepping, single_stepping holds the original instruction
168 unsigned int *single_stepping = NULL;
169 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
170 unsigned int single_step_save1;
171 unsigned int single_step_save2;
172 unsigned int single_step_save3;
176 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
178 unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
180 /* Put the original instruction back. */
181 *((char *)pc) = orig_inst & 0xff;
182 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
184 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
185 /* Install helper instructions for the single step:
186 * pushf; or [esp],0x100; popf. */
187 single_step_save1 = *(pc-3);
188 single_step_save2 = *(pc-2);
189 single_step_save3 = *(pc-1);
190 *(pc-3) = 0x9c909090;
191 *(pc-2) = 0x00240c81;
192 *(pc-1) = 0x9d000001;
194 *context_eflags_addr(context) |= 0x100;
197 single_stepping = pc;
199 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
200 *os_context_pc_addr(context) = (char *)pc - 9;
205 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
207 int code = info->si_code;
208 os_context_t *context = (os_context_t*)void_context;
211 if (single_stepping && (signal==SIGTRAP))
213 /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
215 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
216 /* Un-install single step helper instructions. */
217 *(single_stepping-3) = single_step_save1;
218 *(single_stepping-2) = single_step_save2;
219 *(single_stepping-1) = single_step_save3;
221 *context_eflags_addr(context) ^= 0x100;
223 /* Re-install the breakpoint if possible. */
224 if (*os_context_pc_addr(context) == (int)single_stepping + 1) {
225 fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
227 *((char *)single_stepping) = BREAKPOINT_INST; /* x86 INT3 */
228 *((char *)single_stepping+1) = trap_Breakpoint;
231 single_stepping = NULL;
235 /* This is just for info in case the monitor wants to print an
237 current_control_stack_pointer =
238 (lispobj *)*os_context_sp_addr(context);
240 /* FIXME: CMUCL puts the float control restoration code here.
241 Thus, it seems to me that single-stepping won't restore the
242 float control. Since SBCL currently doesn't support
243 single-stepping (as far as I can tell) this is somewhat moot,
244 but it might be worth either moving this code up or deleting
245 the single-stepping code entirely. -- CSR, 2002-07-15 */
246 #ifdef LISP_FEATURE_LINUX
247 os_restore_fp_control(context);
250 /* On entry %eip points just after the INT3 byte and aims at the
251 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
252 * number of bytes will follow, the first is the length of the byte
253 * arguments to follow. */
254 trap = *(unsigned char *)(*os_context_pc_addr(context));
257 case trap_PendingInterrupt:
258 FSHOW((stderr, "/<trap pending interrupt>\n"));
259 arch_skip_instruction(context);
260 interrupt_handle_pending(context);
264 /* Note: the old CMU CL code tried to save FPU state
265 * here, and restore it after we do our thing, but there
266 * seems to be no point in doing that, since we're just
267 * going to lose(..) anyway. */
268 fake_foreign_function_call(context);
269 lose("%%PRIMITIVE HALT called; the party is over.");
273 FSHOW((stderr, "<trap error/cerror %d>\n", code));
274 interrupt_internal_error(signal, info, context, code==trap_Cerror);
277 case trap_Breakpoint:
278 --*os_context_pc_addr(context);
279 handle_breakpoint(signal, info, context);
282 case trap_FunEndBreakpoint:
283 --*os_context_pc_addr(context);
284 *os_context_pc_addr(context) =
285 (int)handle_fun_end_breakpoint(signal, info, context);
289 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
290 signal, code, context));
291 interrupt_handle_now(signal, info, context);
297 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
298 os_context_t *context = (os_context_t*)void_context;
299 fake_foreign_function_call(context);
300 monitor_or_something();
304 arch_install_interrupt_handlers()
306 SHOW("entering arch_install_interrupt_handlers()");
308 /* Note: The old CMU CL code here used sigtrap_handler() to handle
309 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
310 * things that way. So, I changed to separate handlers when
311 * debugging a problem on OpenBSD, where SBCL wasn't catching
312 * SIGILL properly, but was instead letting the process be
313 * terminated with an "Illegal instruction" output. If this change
314 * turns out to break something (maybe breakpoint handling on some
315 * OS I haven't tested on?) and we have to go back to the old CMU
316 * CL way, I hope there will at least be a comment to explain
317 * why.. -- WHN 2001-06-07 */
318 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
319 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
321 SHOW("returning from arch_install_interrupt_handlers()");
324 /* This is implemented in assembly language and called from C: */
326 call_into_lisp(lispobj fun, lispobj *args, int nargs);
328 /* These functions are an interface to the Lisp call-in facility.
329 * Since this is C we can know nothing about the calling environment.
330 * The control stack might be the C stack if called from the monitor
331 * or the Lisp stack if called as a result of an interrupt or maybe
332 * even a separate stack. The args are most likely on that stack but
333 * could be in registers depending on what the compiler likes. So we
334 * copy the args into a portable vector and let the assembly language
335 * call-in function figure it out. */
338 funcall0(lispobj function)
340 lispobj *args = NULL;
342 FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
343 return call_into_lisp(function, args, 0);
346 funcall1(lispobj function, lispobj arg0)
350 return call_into_lisp(function, args, 1);
353 funcall2(lispobj function, lispobj arg0, lispobj arg1)
358 return call_into_lisp(function, args, 2);
361 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
367 return call_into_lisp(function, args, 3);
370 #ifdef LISP_FEATURE_LINKAGE_TABLE
371 /* FIXME: It might be cleaner to generate these from the lisp side of
376 arch_write_linkage_table_jmp(char * reloc, void * fun)
378 /* Make JMP to function entry. JMP offset is calculated from next
381 long offset = (char *)fun - (reloc + 5);
384 *reloc++ = 0xe9; /* opcode for JMP rel32 */
385 for (i = 0; i < 4; i++) {
386 *reloc++ = offset & 0xff;
390 /* write a nop for good measure. */
395 arch_write_linkage_table_ref(void * reloc, void * data)
397 *(unsigned long *)reloc = (unsigned long)data;