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