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