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