0.9.13.33: :SB-LDB in default build
[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
28 #include "genesis/static-symbols.h"
29 #include "genesis/symbol.h"
30
31 #define BREAKPOINT_INST 0xcc    /* INT3 */
32
33 unsigned long fast_random_state = 1;
34
35 void arch_init(void)
36 {}
37
38 #ifndef LISP_FEATURE_WIN32
39 os_vm_address_t
40 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
41 {
42     return (os_vm_address_t)code->si_addr;
43 }
44 #endif
45
46 \f
47 /*
48  * hacking signal contexts
49  *
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.)
52  */
53
54 int *
55 context_eflags_addr(os_context_t *context)
56 {
57 #if defined __linux__ || defined __sun
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
62      * instead. */
63     return &context->uc_mcontext.gregs[16];
64 #elif defined __FreeBSD__
65     return &context->uc_mcontext.mc_eflags;
66 #elif defined __OpenBSD__
67     return &context->sc_eflags;
68 #elif defined LISP_FEATURE_DARWIN
69     return &context->uc_mcontext->ss.eflags;
70 #elif defined __NetBSD__
71     return &(context->uc_mcontext.__gregs[_REG_EFL]);
72 #elif defined LISP_FEATURE_WIN32
73     return (int *)&context->EFlags;
74 #else
75 #error unsupported OS
76 #endif
77 }
78 \f
79 void arch_skip_instruction(os_context_t *context)
80 {
81     /* Assuming we get here via an INT3 xxx instruction, the PC now
82      * points to the interrupt code (a Lisp value) so we just move
83      * past it. Skip the code; after that, if the code is an
84      * error-trap or cerror-trap then skip the data bytes that follow. */
85
86     int vlen;
87     int code;
88
89
90     /* Get and skip the Lisp interrupt code. */
91     code = *(char*)(*os_context_pc_addr(context))++;
92     switch (code)
93         {
94         case trap_Error:
95         case trap_Cerror:
96             /* Lisp error arg vector length */
97             vlen = *(char*)(*os_context_pc_addr(context))++;
98             /* Skip Lisp error arg data bytes. */
99             while (vlen-- > 0) {
100                 ++*os_context_pc_addr(context);
101             }
102             break;
103
104         case trap_Breakpoint:           /* not tested */
105         case trap_FunEndBreakpoint: /* not tested */
106             break;
107
108         case trap_PendingInterrupt:
109         case trap_Halt:
110             /* only needed to skip the Code */
111             break;
112
113         default:
114             fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
115             break;
116         }
117
118     FSHOW((stderr,
119            "/[arch_skip_inst resuming at %x]\n",
120            *os_context_pc_addr(context)));
121 }
122
123 unsigned char *
124 arch_internal_error_arguments(os_context_t *context)
125 {
126     return 1 + (unsigned char *)(*os_context_pc_addr(context));
127 }
128
129 boolean
130 arch_pseudo_atomic_atomic(os_context_t *context)
131 {
132     return SymbolValue(PSEUDO_ATOMIC_ATOMIC,arch_os_get_current_thread());
133 }
134
135 void
136 arch_set_pseudo_atomic_interrupted(os_context_t *context)
137 {
138     SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1),
139                    arch_os_get_current_thread());
140 }
141
142 void
143 arch_clear_pseudo_atomic_interrupted(os_context_t *context)
144 {
145     SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0),
146                    arch_os_get_current_thread());
147 }
148 \f
149 /*
150  * This stuff seems to get called for TRACE and debug activity.
151  */
152
153 unsigned int
154 arch_install_breakpoint(void *pc)
155 {
156     unsigned int result = *(unsigned int*)pc;
157
158     *(char*)pc = BREAKPOINT_INST;               /* x86 INT3       */
159     *((char*)pc+1) = trap_Breakpoint;           /* Lisp trap code */
160
161     return result;
162 }
163
164 void
165 arch_remove_breakpoint(void *pc, unsigned int orig_inst)
166 {
167     *((char *)pc) = orig_inst & 0xff;
168     *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
169 }
170 \f
171 /* When single stepping, single_stepping holds the original instruction
172  * PC location. */
173 unsigned int *single_stepping = NULL;
174 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
175 unsigned int  single_step_save1;
176 unsigned int  single_step_save2;
177 unsigned int  single_step_save3;
178 #endif
179
180 void
181 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
182 {
183     unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
184
185     /* Put the original instruction back. */
186     *((char *)pc) = orig_inst & 0xff;
187     *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
188
189 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
190     /* Install helper instructions for the single step:
191      * pushf; or [esp],0x100; popf. */
192     single_step_save1 = *(pc-3);
193     single_step_save2 = *(pc-2);
194     single_step_save3 = *(pc-1);
195     *(pc-3) = 0x9c909090;
196     *(pc-2) = 0x00240c81;
197     *(pc-1) = 0x9d000001;
198 #else
199     *context_eflags_addr(context) |= 0x100;
200 #endif
201
202     single_stepping = pc;
203
204 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
205     *os_context_pc_addr(context) = (char *)pc - 9;
206 #endif
207 }
208 \f
209 void
210 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
211 {
212     os_context_t *context = (os_context_t*)void_context;
213     unsigned int trap;
214
215 #ifndef LISP_FEATURE_WIN32
216     if (single_stepping && (signal==SIGTRAP))
217     {
218         /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
219
220 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
221         /* Un-install single step helper instructions. */
222         *(single_stepping-3) = single_step_save1;
223         *(single_stepping-2) = single_step_save2;
224         *(single_stepping-1) = single_step_save3;
225 #else
226         *context_eflags_addr(context) &= ~0x100;
227 #endif
228         /* Re-install the breakpoint if possible. */
229         if (*os_context_pc_addr(context) == (int)single_stepping + 1) {
230             fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
231         } else {
232             *((char *)single_stepping) = BREAKPOINT_INST;       /* x86 INT3 */
233             *((char *)single_stepping+1) = trap_Breakpoint;
234         }
235
236         single_stepping = NULL;
237         return;
238     }
239 #endif
240
241     /* This is just for info in case the monitor wants to print an
242      * approximation. */
243     current_control_stack_pointer =
244         (lispobj *)*os_context_sp_addr(context);
245
246     /* FIXME: CMUCL puts the float control restoration code here.
247        Thus, it seems to me that single-stepping won't restore the
248        float control.  Since SBCL currently doesn't support
249        single-stepping (as far as I can tell) this is somewhat moot,
250        but it might be worth either moving this code up or deleting
251        the single-stepping code entirely.  -- CSR, 2002-07-15 */
252 #if defined(LISP_FEATURE_LINUX) || defined(RESTORE_FP_CONTROL_FROM_CONTEXT)
253     os_restore_fp_control(context);
254 #endif
255
256
257 #ifdef LISP_FEATURE_SUNOS
258     /* For some reason the breakpoints that :ENCAPSULATE NIL tracing sets up
259      * cause a trace trap (i.e. processor single-stepping trap) on the following
260      * instruction on Solaris 10/x86. -- JES, 2006-04-07
261      */
262     if (info->si_code == TRAP_TRACE) {
263         lose("foo");
264         return;
265     }
266 #endif
267
268     /* On entry %eip points just after the INT3 byte and aims at the
269      * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
270      * number of bytes will follow, the first is the length of the byte
271      * arguments to follow. */
272     trap = *(unsigned char *)(*os_context_pc_addr(context));
273     /* FSHOW((stderr, "/<sigtrap trap %d at pc_addr: %p>\n", trap, *os_context_pc_addr(context))); */
274     switch (trap) {
275
276     case trap_PendingInterrupt:
277         FSHOW((stderr, "/<trap pending interrupt>\n"));
278         arch_skip_instruction(context);
279         interrupt_handle_pending(context);
280         break;
281
282     case trap_Halt:
283         /* Note: the old CMU CL code tried to save FPU state
284          * here, and restore it after we do our thing, but there
285          * seems to be no point in doing that, since we're just
286          * going to lose(..) anyway. */
287         fake_foreign_function_call(context);
288         lose("%%PRIMITIVE HALT called; the party is over.\n");
289
290     case trap_Error:
291     case trap_Cerror:
292         FSHOW((stderr, "<trap error/cerror %d>\n", trap));
293         interrupt_internal_error(signal, info, context, trap==trap_Cerror);
294         break;
295
296     case trap_Breakpoint:
297         --*os_context_pc_addr(context);
298         handle_breakpoint(signal, info, context);
299         break;
300
301     case trap_FunEndBreakpoint:
302         --*os_context_pc_addr(context);
303         *os_context_pc_addr(context) =
304             (int)handle_fun_end_breakpoint(signal, info, context);
305         break;
306
307     default:
308         FSHOW((stderr,"/[C--trap default %d %d %x]\n",
309                signal, trap, context));
310         interrupt_handle_now(signal, info, context);
311         break;
312     }
313 }
314
315 static void
316 sigill_handler(int signal, siginfo_t *siginfo, void *void_context) {
317     os_context_t *context = (os_context_t*)void_context;
318
319     /* Triggering SIGTRAP using int3 is unreliable on OS X/x86, so
320      * we need to use illegal instructions for traps.
321      */
322 #if defined(LISP_FEATURE_DARWIN)
323     if (*((unsigned short *)*os_context_pc_addr(context)) == 0x0b0f) {
324         *os_context_pc_addr(context) += 2;
325         return sigtrap_handler(signal, siginfo, void_context);
326     }
327 #endif
328
329     fake_foreign_function_call(context);
330     lose("fake_foreign_call fell through");
331 }
332
333 void
334 arch_install_interrupt_handlers()
335 {
336     SHOW("entering arch_install_interrupt_handlers()");
337
338     /* Note: The old CMU CL code here used sigtrap_handler() to handle
339      * SIGILL as well as SIGTRAP. I couldn't see any reason to do
340      * things that way. So, I changed to separate handlers when
341      * debugging a problem on OpenBSD, where SBCL wasn't catching
342      * SIGILL properly, but was instead letting the process be
343      * terminated with an "Illegal instruction" output. If this change
344      * turns out to break something (maybe breakpoint handling on some
345      * OS I haven't tested on?) and we have to go back to the old CMU
346      * CL way, I hope there will at least be a comment to explain
347      * why.. -- WHN 2001-06-07 */
348 #ifndef LISP_FEATURE_WIN32
349     undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
350     undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
351 #endif
352
353     SHOW("returning from arch_install_interrupt_handlers()");
354 }
355 \f
356 /* This is implemented in assembly language and called from C: */
357 extern lispobj
358 call_into_lisp(lispobj fun, lispobj *args, int nargs);
359
360 /* These functions are an interface to the Lisp call-in facility.
361  * Since this is C we can know nothing about the calling environment.
362  * The control stack might be the C stack if called from the monitor
363  * or the Lisp stack if called as a result of an interrupt or maybe
364  * even a separate stack. The args are most likely on that stack but
365  * could be in registers depending on what the compiler likes. So we
366  * copy the args into a portable vector and let the assembly language
367  * call-in function figure it out. */
368
369 lispobj
370 funcall0(lispobj function)
371 {
372     lispobj *args = NULL;
373
374     FSHOW((stderr, "/entering funcall0(0x%lx)\n", (long)function));
375     return call_into_lisp(function, args, 0);
376 }
377 lispobj
378 funcall1(lispobj function, lispobj arg0)
379 {
380     lispobj args[1];
381     args[0] = arg0;
382     return call_into_lisp(function, args, 1);
383 }
384 lispobj
385 funcall2(lispobj function, lispobj arg0, lispobj arg1)
386 {
387     lispobj args[2];
388     args[0] = arg0;
389     args[1] = arg1;
390     return call_into_lisp(function, args, 2);
391 }
392 lispobj
393 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
394 {
395     lispobj args[3];
396     args[0] = arg0;
397     args[1] = arg1;
398     args[2] = arg2;
399     return call_into_lisp(function, args, 3);
400 }
401
402 #ifdef LISP_FEATURE_LINKAGE_TABLE
403 /* FIXME: It might be cleaner to generate these from the lisp side of
404  * things.
405  */
406
407 void
408 arch_write_linkage_table_jmp(char * reloc, void * fun)
409 {
410     /* Make JMP to function entry. JMP offset is calculated from next
411      * instruction.
412      */
413     long offset = (char *)fun - (reloc + 5);
414     int i;
415
416     *reloc++ = 0xe9;            /* opcode for JMP rel32 */
417     for (i = 0; i < 4; i++) {
418         *reloc++ = offset & 0xff;
419         offset >>= 8;
420     }
421
422     /* write a nop for good measure. */
423     *reloc = 0x90;
424 }
425
426 void
427 arch_write_linkage_table_ref(void * reloc, void * data)
428 {
429     *(unsigned long *)reloc = (unsigned long)data;
430 }
431
432 #endif