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