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