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 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
43 return (os_vm_address_t)code->si_addr;
48 * hacking signal contexts
50 * (This depends both on architecture, which determines what we might
51 * want to get to, and on OS, which determines how we get to it.)
55 context_eflags_addr(os_context_t *context)
58 /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
59 * <sys/ucontext.h> file to define symbolic names for offsets into
60 * gregs[], but it's conditional on __USE_GNU and not defined, so
61 * we need to do this nasty absolute index magic number thing
63 return &context->uc_mcontext.gregs[17];
64 #elif defined __FreeBSD__
65 return &context->uc_mcontext.mc_eflags;
66 #elif defined __OpenBSD__
67 return &context->sc_eflags;
73 void arch_skip_instruction(os_context_t *context)
75 /* Assuming we get here via an INT3 xxx instruction, the PC now
76 * points to the interrupt code (a Lisp value) so we just move
77 * past it. Skip the code; after that, if the code is an
78 * error-trap or cerror-trap then skip the data bytes that follow. */
84 /* Get and skip the Lisp interrupt code. */
85 code = *(char*)(*os_context_pc_addr(context))++;
90 /* Lisp error arg vector length */
91 vlen = *(char*)(*os_context_pc_addr(context))++;
92 /* Skip Lisp error arg data bytes. */
94 ++*os_context_pc_addr(context);
98 case trap_Breakpoint: /* not tested */
99 case trap_FunEndBreakpoint: /* not tested */
102 case trap_PendingInterrupt:
104 /* only needed to skip the Code */
108 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
113 "/[arch_skip_inst resuming at %x]\n",
114 *os_context_pc_addr(context)));
118 arch_internal_error_arguments(os_context_t *context)
120 return 1 + (unsigned char *)(*os_context_pc_addr(context));
124 arch_pseudo_atomic_atomic(os_context_t *context)
126 return SymbolValue(PSEUDO_ATOMIC_ATOMIC,arch_os_get_current_thread());
130 arch_set_pseudo_atomic_interrupted(os_context_t *context)
132 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1),
133 arch_os_get_current_thread());
137 arch_clear_pseudo_atomic_interrupted(os_context_t *context)
139 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0),
140 arch_os_get_current_thread());
144 * This stuff seems to get called for TRACE and debug activity.
148 arch_install_breakpoint(void *pc)
150 unsigned int result = *(unsigned int*)pc;
152 *(char*)pc = BREAKPOINT_INST; /* x86 INT3 */
153 *((char*)pc+1) = trap_Breakpoint; /* Lisp trap code */
159 arch_remove_breakpoint(void *pc, unsigned int orig_inst)
161 *((char *)pc) = orig_inst & 0xff;
162 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
167 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
169 unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
171 /* Put the original instruction back. */
172 *((char *)pc) = orig_inst & 0xff;
173 *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
177 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
179 int code = info->si_code;
180 os_context_t *context = (os_context_t*)void_context;
183 /* This is just for info in case the monitor wants to print an
185 current_control_stack_pointer =
186 (lispobj *)*os_context_sp_addr(context);
188 #ifdef LISP_FEATURE_LINUX
189 os_restore_fp_control(context);
192 /* On entry %eip points just after the INT3 byte and aims at the
193 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
194 * number of bytes will follow, the first is the length of the byte
195 * arguments to follow. */
196 trap = *(unsigned char *)(*os_context_pc_addr(context));
199 case trap_PendingInterrupt:
200 FSHOW((stderr, "/<trap pending interrupt>\n"));
201 arch_skip_instruction(context);
202 interrupt_handle_pending(context);
206 /* Note: the old CMU CL code tried to save FPU state
207 * here, and restore it after we do our thing, but there
208 * seems to be no point in doing that, since we're just
209 * going to lose(..) anyway. */
210 fake_foreign_function_call(context);
211 lose("%%PRIMITIVE HALT called; the party is over.\n");
215 FSHOW((stderr, "<trap error/cerror %d>\n", code));
216 interrupt_internal_error(signal, info, context, code==trap_Cerror);
219 case trap_Breakpoint:
220 --*os_context_pc_addr(context);
221 handle_breakpoint(signal, info, context);
224 case trap_FunEndBreakpoint:
225 --*os_context_pc_addr(context);
226 *os_context_pc_addr(context) =
227 (unsigned long)handle_fun_end_breakpoint(signal, info, context);
231 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
232 signal, code, context));
233 interrupt_handle_now(signal, info, context);
239 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
240 os_context_t *context = (os_context_t*)void_context;
241 fake_foreign_function_call(context);
242 monitor_or_something();
246 arch_install_interrupt_handlers()
248 SHOW("entering arch_install_interrupt_handlers()");
250 /* Note: The old CMU CL code here used sigtrap_handler() to handle
251 * SIGILL as well as SIGTRAP. I couldn't see any reason to do
252 * things that way. So, I changed to separate handlers when
253 * debugging a problem on OpenBSD, where SBCL wasn't catching
254 * SIGILL properly, but was instead letting the process be
255 * terminated with an "Illegal instruction" output. If this change
256 * turns out to break something (maybe breakpoint handling on some
257 * OS I haven't tested on?) and we have to go back to the old CMU
258 * CL way, I hope there will at least be a comment to explain
259 * why.. -- WHN 2001-06-07 */
260 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
261 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
263 SHOW("returning from arch_install_interrupt_handlers()");
266 /* This is implemented in assembly language and called from C: */
268 call_into_lisp(lispobj fun, lispobj *args, int nargs);
270 /* These functions are an interface to the Lisp call-in facility.
271 * Since this is C we can know nothing about the calling environment.
272 * The control stack might be the C stack if called from the monitor
273 * or the Lisp stack if called as a result of an interrupt or maybe
274 * even a separate stack. The args are most likely on that stack but
275 * could be in registers depending on what the compiler likes. So we
276 * copy the args into a portable vector and let the assembly language
277 * call-in function figure it out. */
280 funcall0(lispobj function)
282 lispobj *args = NULL;
284 FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
285 return call_into_lisp(function, args, 0);
288 funcall1(lispobj function, lispobj arg0)
292 return call_into_lisp(function, args, 1);
295 funcall2(lispobj function, lispobj arg0, lispobj arg1)
300 return call_into_lisp(function, args, 2);
303 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
309 return call_into_lisp(function, args, 3);
313 #ifdef LISP_FEATURE_LINKAGE_TABLE
314 /* FIXME: It might be cleaner to generate these from the lisp side of
319 arch_write_linkage_table_jmp(char * reloc, void * fun)
321 unsigned long addr = (unsigned long) fun;
324 *reloc++ = 0xFF; /* Opcode for near jump to absolute reg/mem64. */
325 *reloc++ = 0x25; /* ModRM #b00 100 101, i.e. RIP-relative. */
326 *reloc++ = 0x00; /* 32-bit displacement field = 0 */
327 *reloc++ = 0x00; /* ... */
328 *reloc++ = 0x00; /* ... */
329 *reloc++ = 0x00; /* ... */
331 for (i = 0; i < 8; i++) {
332 *reloc++ = addr & 0xff;
336 /* write a nop for good measure. */
341 arch_write_linkage_table_ref(void * reloc, void * data)
343 *(unsigned long *)reloc = (unsigned long)data;
348 /* These setup and check *both* the sse2 and x87 FPUs. While lisp code
349 only uses the sse2 FPU, other code (such as libc) may use the x87 FPU.
357 /* return the x87 exception flags ored in with the sse2
358 * control+status flags */
359 asm ("fnstsw %0" : "=m" (temp));
362 asm ("stmxcsr %0" : "=m" (temp));
364 /* flip exception mask bits */
365 return result ^ (0x3F << 7);
371 unsigned short unused1;
373 unsigned short unused2;
374 unsigned int other_regs[5];
378 arch_set_fp_modes(unsigned int mxcsr)
383 /* turn trap enable bits into exception mask */
387 asm ("fnstenv %0" : "=m" (f_env));
388 /* set control word: always long double precision
389 * get traps and rounding from mxcsr word */
390 f_env.cw = 0x300 | ((mxcsr >> 7) & 0x3F) | (((mxcsr >> 13) & 0x3) << 10);
391 /* set status word: only override exception flags, from mxcsr */
393 f_env.sw |= (mxcsr & 0x3F);
395 asm ("fldenv %0" : : "m" (f_env));
397 /* now, simply, load up the mxcsr register */
399 asm ("ldmxcsr %0" : : "m" (temp));