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"
28 #define BREAKPOINT_INST 0xcc /* INT3 */
30 unsigned long fast_random_state = 1;
36 * hacking signal contexts
38 * (This depends both on architecture, which determines what we might
39 * want to get to, and on OS, which determines how we get to it.)
43 context_eflags_addr(os_context_t *context)
46 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
47 * <sys/ucontext.h> file to define symbolic names for offsets into
48 * gregs[], but it's conditional on __USE_GNU and not defined, so
49 * we need to do this nasty absolute index magic number thing
51 return &context->uc_mcontext.gregs[16];
52 #elif defined __FreeBSD__
53 return &context->uc_mcontext.mc_eflags;
54 #elif defined __OpenBSD__
55 return &context->sc_eflags;
61 void arch_skip_instruction(os_context_t *context)
63 /* Assuming we get here via an INT3 xxx instruction, the PC now
64 * points to the interrupt code (a Lisp value) so we just move
65 * past it. Skip the code; after that, if the code is an
66 * error-trap or cerror-trap then skip the data bytes that follow. */
71 FSHOW((stderr, "/[arch_skip_inst at %x]\n", *os_context_pc_addr(context)));
73 /* Get and skip the Lisp interrupt code. */
74 code = *(char*)(*os_context_pc_addr(context))++;
79 /* Lisp error arg vector length */
80 vlen = *(char*)(*os_context_pc_addr(context))++;
81 /* Skip Lisp error arg data bytes. */
83 ( (char*)(*os_context_pc_addr(context)) )++;
87 case trap_Breakpoint: /* not tested */
88 case trap_FunEndBreakpoint: /* not tested */
91 case trap_PendingInterrupt:
93 /* only needed to skip the Code */
97 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
102 "/[arch_skip_inst resuming at %x]\n",
103 *os_context_pc_addr(context)));
107 arch_internal_error_arguments(os_context_t *context)
109 return 1 + (unsigned char *)(*os_context_pc_addr(context));
113 arch_pseudo_atomic_atomic(os_context_t *context)
115 return SymbolValue(PSEUDO_ATOMIC_ATOMIC);
119 arch_set_pseudo_atomic_interrupted(os_context_t *context)
121 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1));
125 * This stuff seems to get called for TRACE and debug activity.
129 arch_install_breakpoint(void *pc)
131 unsigned long result = *(unsigned long*)pc;
133 *(char*)pc = BREAKPOINT_INST; /* x86 INT3 */
134 *((char*)pc+1) = trap_Breakpoint; /* Lisp trap code */
140 arch_remove_breakpoint(void *pc, unsigned long orig_inst)
142 *((char *)pc) = orig_inst & 0xff;
143 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
146 /* When single stepping, single_stepping holds the original instruction
148 unsigned int *single_stepping = NULL;
149 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
150 unsigned int single_step_save1;
151 unsigned int single_step_save2;
152 unsigned int single_step_save3;
156 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
158 unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
160 /* Put the original instruction back. */
161 *((char *)pc) = orig_inst & 0xff;
162 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
164 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
165 /* Install helper instructions for the single step:
166 * pushf; or [esp],0x100; popf. */
167 single_step_save1 = *(pc-3);
168 single_step_save2 = *(pc-2);
169 single_step_save3 = *(pc-1);
170 *(pc-3) = 0x9c909090;
171 *(pc-2) = 0x00240c81;
172 *(pc-1) = 0x9d000001;
174 *context_eflags_addr(context) |= 0x100;
177 single_stepping = (unsigned int*)pc;
179 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
180 *os_context_pc_addr(context) = (char *)pc - 9;
185 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
187 int code = info->si_code;
188 os_context_t *context = (os_context_t*)void_context;
191 if (single_stepping && (signal==SIGTRAP))
193 /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
195 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
196 /* Un-install single step helper instructions. */
197 *(single_stepping-3) = single_step_save1;
198 *(single_stepping-2) = single_step_save2;
199 *(single_stepping-1) = single_step_save3;
201 *context_eflags_addr(context) ^= 0x100;
203 /* Re-install the breakpoint if possible. */
204 if (*os_context_pc_addr(context) == (int)single_stepping + 1) {
205 fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
207 *((char *)single_stepping) = BREAKPOINT_INST; /* x86 INT3 */
208 *((char *)single_stepping+1) = trap_Breakpoint;
211 single_stepping = NULL;
215 /* This is just for info in case the monitor wants to print an
217 current_control_stack_pointer =
218 (lispobj *)*os_context_sp_addr(context);
220 /* FIXME: CMUCL puts the float control restoration code here.
221 Thus, it seems to me that single-stepping won't restore the
222 float control. Since SBCL currently doesn't support
223 single-stepping (as far as I can tell) this is somewhat moot,
224 but it might be worth either moving this code up or deleting
225 the single-stepping code entirely. -- CSR, 2002-07-15 */
226 #ifdef LISP_FEATURE_LINUX
227 os_restore_fp_control(context);
230 /* On entry %eip points just after the INT3 byte and aims at the
231 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
232 * number of bytes will follow, the first is the length of the byte
233 * arguments to follow. */
234 trap = *(unsigned char *)(*os_context_pc_addr(context));
237 case trap_PendingInterrupt:
238 FSHOW((stderr, "/<trap pending interrupt>\n"));
239 arch_skip_instruction(context);
240 interrupt_handle_pending(context);
244 /* Note: the old CMU CL code tried to save FPU state
245 * here, and restore it after we do our thing, but there
246 * seems to be no point in doing that, since we're just
247 * going to lose(..) anyway. */
248 fake_foreign_function_call(context);
249 lose("%%PRIMITIVE HALT called; the party is over.");
253 FSHOW((stderr, "<trap error/cerror %d>\n", code));
254 interrupt_internal_error(signal, info, context, code==trap_Cerror);
257 case trap_Breakpoint:
258 (char*)(*os_context_pc_addr(context)) -= 1;
259 handle_breakpoint(signal, info, context);
262 case trap_FunEndBreakpoint:
263 (char*)(*os_context_pc_addr(context)) -= 1;
264 *os_context_pc_addr(context) =
265 (int)handle_fun_end_breakpoint(signal, info, context);
269 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
270 signal, code, context));
271 interrupt_handle_now(signal, info, context);
277 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
278 os_context_t *context = (os_context_t*)void_context;
279 fake_foreign_function_call(context);
280 monitor_or_something();
284 arch_install_interrupt_handlers()
286 SHOW("entering arch_install_interrupt_handlers()");
288 /* Note: The old CMU CL code here used sigtrap_handler() to handle
289 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
290 * things that way. So, I changed to separate handlers when
291 * debugging a problem on OpenBSD, where SBCL wasn't catching
292 * SIGILL properly, but was instead letting the process be
293 * terminated with an "Illegal instruction" output. If this change
294 * turns out to break something (maybe breakpoint handling on some
295 * OS I haven't tested on?) and we have to go back to the old CMU
296 * CL way, I hope there will at least be a comment to explain
297 * why.. -- WHN 2001-06-07 */
298 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
299 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
301 SHOW("returning from arch_install_interrupt_handlers()");
304 /* This is implemented in assembly language and called from C: */
306 call_into_lisp(lispobj fun, lispobj *args, int nargs);
308 /* These functions are an interface to the Lisp call-in facility.
309 * Since this is C we can know nothing about the calling environment.
310 * The control stack might be the C stack if called from the monitor
311 * or the Lisp stack if called as a result of an interrupt or maybe
312 * even a separate stack. The args are most likely on that stack but
313 * could be in registers depending on what the compiler likes. So we
314 * copy the args into a portable vector and let the assembly language
315 * call-in function figure it out. */
317 funcall0(lispobj function)
319 lispobj *args = NULL;
321 FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
322 return call_into_lisp(function, args, 0);
325 funcall1(lispobj function, lispobj arg0)
329 return call_into_lisp(function, args, 1);
332 funcall2(lispobj function, lispobj arg0, lispobj arg1)
337 return call_into_lisp(function, args, 2);
340 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
346 return call_into_lisp(function, args, 3);