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