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