Add safepoint mechanism
[sbcl.git] / src / runtime / x86-arch.c
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
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.
10  */
11
12 #include <stdio.h>
13
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 "thread.h"
27 #include "pseudo-atomic.h"
28
29 #include "genesis/static-symbols.h"
30 #include "genesis/symbol.h"
31
32 #define BREAKPOINT_INST 0xcc    /* INT3 */
33 #define UD2_INST 0x0b0f         /* UD2 */
34
35 #ifndef LISP_FEATURE_UD2_BREAKPOINTS
36 #define BREAKPOINT_WIDTH 1
37 #else
38 #define BREAKPOINT_WIDTH 2
39 #endif
40
41 unsigned long fast_random_state = 1;
42
43 void arch_init(void)
44 {}
45
46 #ifndef LISP_FEATURE_WIN32
47 os_vm_address_t
48 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
49 {
50     return (os_vm_address_t)code->si_addr;
51 }
52 #endif
53
54 \f
55 /*
56  * hacking signal contexts
57  *
58  * (This depends both on architecture, which determines what we might
59  * want to get to, and on OS, which determines how we get to it.)
60  */
61
62 int *
63 context_eflags_addr(os_context_t *context)
64 {
65 #if defined __linux__ || defined __sun
66     /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
67      * <sys/ucontext.h> file to define symbolic names for offsets into
68      * gregs[], but it's conditional on __USE_GNU and not defined, so
69      * we need to do this nasty absolute index magic number thing
70      * instead. */
71     return &context->uc_mcontext.gregs[16];
72 #elif defined __FreeBSD__
73     return &context->uc_mcontext.mc_eflags;
74 #elif defined __OpenBSD__
75     return &context->sc_eflags;
76 #elif defined LISP_FEATURE_DARWIN
77     return (int *)(&context->uc_mcontext->SS.EFLAGS);
78 #elif defined __NetBSD__
79     return &(context->uc_mcontext.__gregs[_REG_EFL]);
80 #elif defined LISP_FEATURE_WIN32
81     return (int *)&context->EFlags;
82 #else
83 #error unsupported OS
84 #endif
85 }
86 \f
87 void arch_skip_instruction(os_context_t *context)
88 {
89     /* Assuming we get here via an INT3 xxx instruction, the PC now
90      * points to the interrupt code (a Lisp value) so we just move
91      * past it. Skip the code; after that, if the code is an
92      * error-trap or cerror-trap then skip the data bytes that follow. */
93
94     int vlen;
95     int code;
96
97
98     /* Get and skip the Lisp interrupt code. */
99     code = *(char*)(*os_context_pc_addr(context))++;
100     switch (code)
101         {
102         case trap_Error:
103         case trap_Cerror:
104             /* Lisp error arg vector length */
105             vlen = *(char*)(*os_context_pc_addr(context))++;
106             /* Skip Lisp error arg data bytes. */
107             while (vlen-- > 0) {
108                 ++*os_context_pc_addr(context);
109             }
110             break;
111
112         case trap_Breakpoint:           /* not tested */
113         case trap_FunEndBreakpoint: /* not tested */
114             break;
115
116 #ifdef LISP_FEATURE_SB_SAFEPOINT
117         case trap_GlobalSafepoint:
118         case trap_CspSafepoint:
119 #endif
120         case trap_PendingInterrupt:
121         case trap_Halt:
122         case trap_SingleStepAround:
123         case trap_SingleStepBefore:
124             /* only needed to skip the Code */
125             break;
126
127         default:
128             fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
129             break;
130         }
131
132     FSHOW((stderr,
133            "/[arch_skip_inst resuming at %x]\n",
134            *os_context_pc_addr(context)));
135 }
136
137 unsigned char *
138 arch_internal_error_arguments(os_context_t *context)
139 {
140     return 1 + (unsigned char *)(*os_context_pc_addr(context));
141 }
142
143 boolean
144 arch_pseudo_atomic_atomic(os_context_t *context)
145 {
146     return get_pseudo_atomic_atomic(arch_os_get_current_thread());
147 }
148
149 void
150 arch_set_pseudo_atomic_interrupted(os_context_t *context)
151 {
152     struct thread *thread = arch_os_get_current_thread();
153     set_pseudo_atomic_interrupted(thread);
154 }
155
156 void
157 arch_clear_pseudo_atomic_interrupted(os_context_t *context)
158 {
159     struct thread *thread = arch_os_get_current_thread();
160     clear_pseudo_atomic_interrupted(thread);
161 }
162 \f
163 /*
164  * This stuff seems to get called for TRACE and debug activity.
165  */
166
167 unsigned int
168 arch_install_breakpoint(void *pc)
169 {
170     unsigned int result = *(unsigned int*)pc;
171
172 #ifndef LISP_FEATURE_UD2_BREAKPOINTS
173     *(char*)pc = BREAKPOINT_INST;               /* x86 INT3       */
174     *((char*)pc+1) = trap_Breakpoint;           /* Lisp trap code */
175 #else
176     *(char*)pc = UD2_INST & 0xff;
177     *((char*)pc+1) = UD2_INST >> 8;
178     *((char*)pc+2) = trap_Breakpoint;
179 #endif
180
181     return result;
182 }
183
184 void
185 arch_remove_breakpoint(void *pc, unsigned int orig_inst)
186 {
187     *((char *)pc) = orig_inst & 0xff;
188     *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
189 #if BREAKPOINT_WIDTH > 1
190     *((char *)pc + 2) = (orig_inst & 0xff0000) >> 16;
191 #endif
192 }
193 \f
194 /* When single stepping, single_stepping holds the original instruction
195  * PC location. */
196 unsigned int *single_stepping = NULL;
197 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
198 unsigned int  single_step_save1;
199 unsigned int  single_step_save2;
200 unsigned int  single_step_save3;
201 #endif
202
203 void
204 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
205 {
206     unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
207
208     /* Put the original instruction back. */
209     arch_remove_breakpoint(pc, orig_inst);
210
211 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
212     /* Install helper instructions for the single step:
213      * pushf; or [esp],0x100; popf. */
214     single_step_save1 = *(pc-3);
215     single_step_save2 = *(pc-2);
216     single_step_save3 = *(pc-1);
217     *(pc-3) = 0x9c909090;
218     *(pc-2) = 0x00240c81;
219     *(pc-1) = 0x9d000001;
220 #else
221     *context_eflags_addr(context) |= 0x100;
222 #endif
223
224     single_stepping = pc;
225
226 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
227     *os_context_pc_addr(context) = (os_context_register_t)((char *)pc - 9);
228 #endif
229 }
230 \f
231 void
232 restore_breakpoint_from_single_step(os_context_t * context)
233 {
234     /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
235 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
236     /* Un-install single step helper instructions. */
237     *(single_stepping-3) = single_step_save1;
238     *(single_stepping-2) = single_step_save2;
239     *(single_stepping-1) = single_step_save3;
240 #else
241     *context_eflags_addr(context) &= ~0x100;
242 #endif
243     /* Re-install the breakpoint if possible. */
244     if (((char *)*os_context_pc_addr(context) >
245          (char *)single_stepping) &&
246         ((char *)*os_context_pc_addr(context) <=
247          (char *)single_stepping + BREAKPOINT_WIDTH)) {
248         fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
249     } else {
250         arch_install_breakpoint(single_stepping);
251     }
252
253     single_stepping = NULL;
254     return;
255 }
256
257 void
258 arch_handle_breakpoint(os_context_t *context)
259 {
260     *os_context_pc_addr(context) -= BREAKPOINT_WIDTH;
261     handle_breakpoint(context);
262 }
263
264 void
265 arch_handle_fun_end_breakpoint(os_context_t *context)
266 {
267     *os_context_pc_addr(context) -= BREAKPOINT_WIDTH;
268     *os_context_pc_addr(context) =
269         (int)handle_fun_end_breakpoint(context);
270 }
271
272 void
273 arch_handle_single_step_trap(os_context_t *context, int trap)
274 {
275     arch_skip_instruction(context);
276     /* On x86 the fdefn / function is always in EAX, so we pass 0
277      * as the register_offset. */
278     handle_single_step_trap(context, trap, 0);
279 }
280
281 #ifndef LISP_FEATURE_WIN32
282 void
283 sigtrap_handler(int signal, siginfo_t *info, os_context_t *context)
284 {
285     unsigned int trap;
286
287     if (single_stepping) {
288         restore_breakpoint_from_single_step(context);
289         return;
290     }
291
292     /* This is just for info in case the monitor wants to print an
293      * approximation. */
294     access_control_stack_pointer(arch_os_get_current_thread()) =
295         (lispobj *)*os_context_sp_addr(context);
296
297 #ifdef LISP_FEATURE_SUNOS
298     /* For some reason the breakpoints that :ENCAPSULATE NIL tracing sets up
299      * cause a trace trap (i.e. processor single-stepping trap) on the following
300      * instruction on Solaris 10/x86. -- JES, 2006-04-07
301      */
302     if (info->si_code == TRAP_TRACE) {
303         lose("foo");
304         return;
305     }
306 #endif
307
308     /* On entry %eip points just after the INT3 byte and aims at the
309      * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
310      * number of bytes will follow, the first is the length of the byte
311      * arguments to follow. */
312     trap = *(unsigned char *)(*os_context_pc_addr(context));
313     handle_trap(context, trap);
314 }
315
316 void
317 sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
318     /* Triggering SIGTRAP using int3 is unreliable on OS X/x86, so
319      * we need to use illegal instructions for traps.
320      */
321 #if defined(LISP_FEATURE_UD2_BREAKPOINTS) && !defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
322     if (*((unsigned short *)*os_context_pc_addr(context)) == UD2_INST) {
323         *os_context_pc_addr(context) += 2;
324         return sigtrap_handler(signal, siginfo, context);
325     }
326 #endif
327     fake_foreign_function_call(context);
328     lose("Unhandled SIGILL");
329 }
330 #endif /* not LISP_FEATURE_WIN32 */
331
332 void
333 arch_install_interrupt_handlers()
334 {
335     SHOW("entering arch_install_interrupt_handlers()");
336
337     /* Note: The old CMU CL code here used sigtrap_handler() to handle
338      * SIGILL as well as SIGTRAP. I couldn't see any reason to do
339      * things that way. So, I changed to separate handlers when
340      * debugging a problem on OpenBSD, where SBCL wasn't catching
341      * SIGILL properly, but was instead letting the process be
342      * terminated with an "Illegal instruction" output. If this change
343      * turns out to break something (maybe breakpoint handling on some
344      * OS I haven't tested on?) and we have to go back to the old CMU
345      * CL way, I hope there will at least be a comment to explain
346      * why.. -- WHN 2001-06-07 */
347 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
348     undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
349     undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
350 #endif
351     SHOW("returning from arch_install_interrupt_handlers()");
352 }
353 \f
354 #ifdef LISP_FEATURE_LINKAGE_TABLE
355 /* FIXME: It might be cleaner to generate these from the lisp side of
356  * things.
357  */
358
359 void
360 arch_write_linkage_table_jmp(char * reloc, void * fun)
361 {
362     /* Make JMP to function entry. JMP offset is calculated from next
363      * instruction.
364      */
365     long offset = (char *)fun - (reloc + 5);
366     int i;
367
368     *reloc++ = 0xe9;            /* opcode for JMP rel32 */
369     for (i = 0; i < 4; i++) {
370         *reloc++ = offset & 0xff;
371         offset >>= 8;
372     }
373
374     /* write a nop for good measure. */
375     *reloc = 0x90;
376 }
377
378 void
379 arch_write_linkage_table_ref(void * reloc, void * data)
380 {
381     *(unsigned long *)reloc = (unsigned long)data;
382 }
383
384 #endif