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 ++*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;
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);
246 interrupt_handle_pending(context);
250 /* Note: the old CMU CL code tried to save FPU state
251 * here, and restore it after we do our thing, but there
252 * seems to be no point in doing that, since we're just
253 * going to lose(..) anyway. */
254 fake_foreign_function_call(context);
255 lose("%%PRIMITIVE HALT called; the party is over.");
259 FSHOW((stderr, "<trap error/cerror %d>\n", code));
260 interrupt_internal_error(signal, info, context, code==trap_Cerror);
263 case trap_Breakpoint:
264 --*os_context_pc_addr(context);
265 handle_breakpoint(signal, info, context);
268 case trap_FunEndBreakpoint:
269 --*os_context_pc_addr(context);
270 *os_context_pc_addr(context) =
271 (int)handle_fun_end_breakpoint(signal, info, context);
275 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
276 signal, code, context));
277 interrupt_handle_now(signal, info, context);
283 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
284 os_context_t *context = (os_context_t*)void_context;
285 fake_foreign_function_call(context);
286 monitor_or_something();
290 arch_install_interrupt_handlers()
292 SHOW("entering arch_install_interrupt_handlers()");
294 /* Note: The old CMU CL code here used sigtrap_handler() to handle
295 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
296 * things that way. So, I changed to separate handlers when
297 * debugging a problem on OpenBSD, where SBCL wasn't catching
298 * SIGILL properly, but was instead letting the process be
299 * terminated with an "Illegal instruction" output. If this change
300 * turns out to break something (maybe breakpoint handling on some
301 * OS I haven't tested on?) and we have to go back to the old CMU
302 * CL way, I hope there will at least be a comment to explain
303 * why.. -- WHN 2001-06-07 */
304 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
305 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
307 SHOW("returning from arch_install_interrupt_handlers()");
310 /* This is implemented in assembly language and called from C: */
312 call_into_lisp(lispobj fun, lispobj *args, int nargs);
314 /* These functions are an interface to the Lisp call-in facility.
315 * Since this is C we can know nothing about the calling environment.
316 * The control stack might be the C stack if called from the monitor
317 * or the Lisp stack if called as a result of an interrupt or maybe
318 * even a separate stack. The args are most likely on that stack but
319 * could be in registers depending on what the compiler likes. So we
320 * copy the args into a portable vector and let the assembly language
321 * call-in function figure it out. */
324 funcall0(lispobj function)
326 lispobj *args = NULL;
328 FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
329 return call_into_lisp(function, args, 0);
332 funcall1(lispobj function, lispobj arg0)
336 return call_into_lisp(function, args, 1);
339 funcall2(lispobj function, lispobj arg0, lispobj arg1)
344 return call_into_lisp(function, args, 2);
347 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
353 return call_into_lisp(function, args, 3);
356 #ifdef LISP_FEATURE_LINKAGE_TABLE
357 /* FIXME: It might be cleaner to generate these from the lisp side of
362 arch_write_linkage_table_jmp(char * reloc, void * fun)
364 /* Make JMP to function entry. JMP offset is calculated from next
367 long offset = (char *)fun - (reloc + 5);
370 *reloc++ = 0xe9; /* opcode for JMP rel32 */
371 for (i = 0; i < 4; i++) {
372 *reloc++ = offset & 0xff;
376 /* write a nop for good measure. */
381 arch_write_linkage_table_ref(void * reloc, void * data)
383 *(unsigned long *)reloc = (unsigned long)data;