0.9.3.63:
[sbcl/eslaughter.git] / src / runtime / x86-arch.c
blob8fba683461e041ea12a4b81dfda5d4e1db670acd
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
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.
12 #include <stdio.h>
14 #include "sbcl.h"
15 #include "runtime.h"
16 #include "globals.h"
17 #include "validate.h"
18 #include "os.h"
19 #include "arch.h"
20 #include "lispregs.h"
21 #include "signal.h"
22 #include "alloc.h"
23 #include "interrupt.h"
24 #include "interr.h"
25 #include "breakpoint.h"
26 #include "monitor.h"
27 #include "thread.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;
36 void arch_init(void)
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.)
46 int *
47 context_eflags_addr(os_context_t *context)
49 #if defined __linux__ || defined __sun
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
54 * instead. */
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]);
62 #else
63 #error unsupported OS
64 #endif
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. */
74 int vlen;
75 int code;
78 /* Get and skip the Lisp interrupt code. */
79 code = *(char*)(*os_context_pc_addr(context))++;
80 switch (code)
82 case trap_Error:
83 case trap_Cerror:
84 /* Lisp error arg vector length */
85 vlen = *(char*)(*os_context_pc_addr(context))++;
86 /* Skip Lisp error arg data bytes. */
87 while (vlen-- > 0) {
88 ++*os_context_pc_addr(context);
90 break;
92 case trap_Breakpoint: /* not tested */
93 case trap_FunEndBreakpoint: /* not tested */
94 break;
96 case trap_PendingInterrupt:
97 case trap_Halt:
98 /* only needed to skip the Code */
99 break;
101 default:
102 fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
103 break;
106 FSHOW((stderr,
107 "/[arch_skip_inst resuming at %x]\n",
108 *os_context_pc_addr(context)));
111 unsigned char *
112 arch_internal_error_arguments(os_context_t *context)
114 return 1 + (unsigned char *)(*os_context_pc_addr(context));
117 boolean
118 arch_pseudo_atomic_atomic(os_context_t *context)
120 return SymbolValue(PSEUDO_ATOMIC_ATOMIC,arch_os_get_current_thread());
123 void
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.
134 unsigned long
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 */
142 return result;
145 void
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
153 * PC location. */
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;
159 #endif
161 void
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;
179 #else
180 *context_eflags_addr(context) |= 0x100;
181 #endif
183 single_stepping = (unsigned int*)pc;
185 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
186 *os_context_pc_addr(context) = (char *)pc - 9;
187 #endif
190 void
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;
195 unsigned int trap;
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;
206 #else
207 *context_eflags_addr(context) ^= 0x100;
208 #endif
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");
212 } else {
213 *((char *)single_stepping) = BREAKPOINT_INST; /* x86 INT3 */
214 *((char *)single_stepping+1) = trap_Breakpoint;
217 single_stepping = NULL;
218 return;
221 /* This is just for info in case the monitor wants to print an
222 * approximation. */
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);
234 #endif
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));
241 switch (trap) {
243 case trap_PendingInterrupt:
244 FSHOW((stderr, "/<trap pending interrupt>\n"));
245 arch_skip_instruction(context);
246 interrupt_handle_pending(context);
247 break;
249 case trap_Halt:
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.");
257 case trap_Error:
258 case trap_Cerror:
259 FSHOW((stderr, "<trap error/cerror %d>\n", code));
260 interrupt_internal_error(signal, info, context, code==trap_Cerror);
261 break;
263 case trap_Breakpoint:
264 --*os_context_pc_addr(context);
265 handle_breakpoint(signal, info, context);
266 break;
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);
272 break;
274 default:
275 FSHOW((stderr,"/[C--trap default %d %d %x]\n",
276 signal, code, context));
277 interrupt_handle_now(signal, info, context);
278 break;
282 static void
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();
289 void
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: */
311 extern lispobj
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. */
323 lispobj
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);
331 lispobj
332 funcall1(lispobj function, lispobj arg0)
334 lispobj args[1];
335 args[0] = arg0;
336 return call_into_lisp(function, args, 1);
338 lispobj
339 funcall2(lispobj function, lispobj arg0, lispobj arg1)
341 lispobj args[2];
342 args[0] = arg0;
343 args[1] = arg1;
344 return call_into_lisp(function, args, 2);
346 lispobj
347 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
349 lispobj args[3];
350 args[0] = arg0;
351 args[1] = arg1;
352 args[2] = 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
358 * things.
361 void
362 arch_write_linkage_table_jmp(char * reloc, void * fun)
364 /* Make JMP to function entry. JMP offset is calculated from next
365 * instruction.
367 long offset = (char *)fun - (reloc + 5);
368 int i;
370 *reloc++ = 0xe9; /* opcode for JMP rel32 */
371 for (i = 0; i < 4; i++) {
372 *reloc++ = offset & 0xff;
373 offset >>= 8;
376 /* write a nop for good measure. */
377 *reloc = 0x90;
380 void
381 arch_write_linkage_table_ref(void * reloc, void * data)
383 *(unsigned long *)reloc = (unsigned long)data;
386 #endif