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 * hacking signal contexts
42 * (This depends both on architecture, which determines what we might
43 * want to get to, and on OS, which determines how we get to it.)
47 context_eflags_addr(os_context_t *context)
50 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
51 * <sys/ucontext.h> file to define symbolic names for offsets into
52 * gregs[], but it's conditional on __USE_GNU and not defined, so
53 * we need to do this nasty absolute index magic number thing
55 return &context->uc_mcontext.gregs[16];
56 #elif defined __FreeBSD__
57 return &context->uc_mcontext.mc_eflags;
58 #elif defined __OpenBSD__
59 return &context->sc_eflags;
60 #elif defined __NetBSD__
61 return &(context->uc_mcontext.__gregs[_REG_EFL]);
67 void arch_skip_instruction(os_context_t *context)
69 /* Assuming we get here via an INT3 xxx instruction, the PC now
70 * points to the interrupt code (a Lisp value) so we just move
71 * past it. Skip the code; after that, if the code is an
72 * error-trap or cerror-trap then skip the data bytes that follow. */
78 /* Get and skip the Lisp interrupt code. */
79 code = *(char*)(*os_context_pc_addr(context))++;
84 /* Lisp error arg vector length */
85 vlen = *(char*)(*os_context_pc_addr(context))++;
86 /* Skip Lisp error arg data bytes. */
88 ( (char*)(*os_context_pc_addr(context)) )++;
92 case trap_Breakpoint: /* not tested */
93 case trap_FunEndBreakpoint: /* not tested */
96 case trap_PendingInterrupt:
98 /* only needed to skip the Code */
102 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
107 "/[arch_skip_inst resuming at %x]\n",
108 *os_context_pc_addr(context)));
112 arch_internal_error_arguments(os_context_t *context)
114 return 1 + (unsigned char *)(*os_context_pc_addr(context));
118 arch_pseudo_atomic_atomic(os_context_t *context)
120 return SymbolValue(PSEUDO_ATOMIC_ATOMIC,arch_os_get_current_thread());
124 arch_set_pseudo_atomic_interrupted(os_context_t *context)
126 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1),
127 arch_os_get_current_thread());
131 * This stuff seems to get called for TRACE and debug activity.
135 arch_install_breakpoint(void *pc)
137 unsigned long result = *(unsigned long*)pc;
139 *(char*)pc = BREAKPOINT_INST; /* x86 INT3 */
140 *((char*)pc+1) = trap_Breakpoint; /* Lisp trap code */
146 arch_remove_breakpoint(void *pc, unsigned long orig_inst)
148 *((char *)pc) = orig_inst & 0xff;
149 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
152 /* When single stepping, single_stepping holds the original instruction
154 unsigned int *single_stepping = NULL;
155 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
156 unsigned int single_step_save1;
157 unsigned int single_step_save2;
158 unsigned int single_step_save3;
162 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
164 unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
166 /* Put the original instruction back. */
167 *((char *)pc) = orig_inst & 0xff;
168 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
170 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
171 /* Install helper instructions for the single step:
172 * pushf; or [esp],0x100; popf. */
173 single_step_save1 = *(pc-3);
174 single_step_save2 = *(pc-2);
175 single_step_save3 = *(pc-1);
176 *(pc-3) = 0x9c909090;
177 *(pc-2) = 0x00240c81;
178 *(pc-1) = 0x9d000001;
180 *context_eflags_addr(context) |= 0x100;
183 single_stepping = (unsigned int*)pc;
185 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
186 *os_context_pc_addr(context) = (char *)pc - 9;
191 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
193 int code = info->si_code;
194 os_context_t *context = (os_context_t*)void_context;
198 if (single_stepping && (signal==SIGTRAP))
200 /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
202 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
203 /* Un-install single step helper instructions. */
204 *(single_stepping-3) = single_step_save1;
205 *(single_stepping-2) = single_step_save2;
206 *(single_stepping-1) = single_step_save3;
208 *context_eflags_addr(context) ^= 0x100;
210 /* Re-install the breakpoint if possible. */
211 if (*os_context_pc_addr(context) == (int)single_stepping + 1) {
212 fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
214 *((char *)single_stepping) = BREAKPOINT_INST; /* x86 INT3 */
215 *((char *)single_stepping+1) = trap_Breakpoint;
218 single_stepping = NULL;
222 /* This is just for info in case the monitor wants to print an
224 current_control_stack_pointer =
225 (lispobj *)*os_context_sp_addr(context);
227 /* FIXME: CMUCL puts the float control restoration code here.
228 Thus, it seems to me that single-stepping won't restore the
229 float control. Since SBCL currently doesn't support
230 single-stepping (as far as I can tell) this is somewhat moot,
231 but it might be worth either moving this code up or deleting
232 the single-stepping code entirely. -- CSR, 2002-07-15 */
233 #ifdef LISP_FEATURE_LINUX
234 os_restore_fp_control(context);
237 /* On entry %eip points just after the INT3 byte and aims at the
238 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
239 * number of bytes will follow, the first is the length of the byte
240 * arguments to follow. */
241 trap = *(unsigned char *)(*os_context_pc_addr(context));
244 case trap_PendingInterrupt:
245 FSHOW((stderr, "/<trap pending interrupt>\n"));
246 arch_skip_instruction(context);
248 sigaddset(&ss,SIGTRAP);
249 sigprocmask(SIG_UNBLOCK,&ss,0);
250 interrupt_handle_pending(context);
254 /* Note: the old CMU CL code tried to save FPU state
255 * here, and restore it after we do our thing, but there
256 * seems to be no point in doing that, since we're just
257 * going to lose(..) anyway. */
258 fake_foreign_function_call(context);
259 lose("%%PRIMITIVE HALT called; the party is over.");
263 FSHOW((stderr, "<trap error/cerror %d>\n", code));
264 interrupt_internal_error(signal, info, context, code==trap_Cerror);
267 case trap_Breakpoint:
268 (char*)(*os_context_pc_addr(context)) -= 1;
269 handle_breakpoint(signal, info, context);
272 case trap_FunEndBreakpoint:
273 (char*)(*os_context_pc_addr(context)) -= 1;
274 *os_context_pc_addr(context) =
275 (int)handle_fun_end_breakpoint(signal, info, context);
279 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
280 signal, code, context));
281 interrupt_handle_now(signal, info, context);
287 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
288 os_context_t *context = (os_context_t*)void_context;
289 fake_foreign_function_call(context);
290 monitor_or_something();
294 arch_install_interrupt_handlers()
296 SHOW("entering arch_install_interrupt_handlers()");
298 /* Note: The old CMU CL code here used sigtrap_handler() to handle
299 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
300 * things that way. So, I changed to separate handlers when
301 * debugging a problem on OpenBSD, where SBCL wasn't catching
302 * SIGILL properly, but was instead letting the process be
303 * terminated with an "Illegal instruction" output. If this change
304 * turns out to break something (maybe breakpoint handling on some
305 * OS I haven't tested on?) and we have to go back to the old CMU
306 * CL way, I hope there will at least be a comment to explain
307 * why.. -- WHN 2001-06-07 */
308 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
309 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
311 SHOW("returning from arch_install_interrupt_handlers()");
314 /* This is implemented in assembly language and called from C: */
316 call_into_lisp(lispobj fun, lispobj *args, int nargs);
318 /* These functions are an interface to the Lisp call-in facility.
319 * Since this is C we can know nothing about the calling environment.
320 * The control stack might be the C stack if called from the monitor
321 * or the Lisp stack if called as a result of an interrupt or maybe
322 * even a separate stack. The args are most likely on that stack but
323 * could be in registers depending on what the compiler likes. So we
324 * copy the args into a portable vector and let the assembly language
325 * call-in function figure it out. */
328 funcall0(lispobj function)
330 lispobj *args = NULL;
332 FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
333 return call_into_lisp(function, args, 0);
336 funcall1(lispobj function, lispobj arg0)
340 return call_into_lisp(function, args, 1);
343 funcall2(lispobj function, lispobj arg0, lispobj arg1)
348 return call_into_lisp(function, args, 2);
351 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
357 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 /* Make JMP to function entry. JMP offset is calculated from next
371 long offset = (char *)fun - (reloc + 5);
374 *reloc++ = 0xe9; /* opcode for JMP rel32 */
375 for (i = 0; i < 4; i++) {
376 *reloc++ = offset & 0xff;
380 /* write a nop for good measure. */
385 arch_write_linkage_table_ref(void * reloc, void * data)
387 *(unsigned long *)reloc = (unsigned long)data;