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