Fix make-array transforms.
[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 "alloc.h"
22 #include "interrupt.h"
23 #include "interr.h"
24 #include "breakpoint.h"
25 #include "thread.h"
26 #include "pseudo-atomic.h"
27
28 #include "genesis/static-symbols.h"
29 #include "genesis/symbol.h"
30
31 #define BREAKPOINT_INST 0xcc    /* INT3 */
32 #define UD2_INST 0x0b0f         /* UD2 */
33
34 #ifndef LISP_FEATURE_UD2_BREAKPOINTS
35 #define BREAKPOINT_WIDTH 1
36 #else
37 #define BREAKPOINT_WIDTH 2
38 #endif
39
40 void arch_init(void)
41 {}
42
43 #ifndef LISP_FEATURE_WIN32
44 os_vm_address_t
45 arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
46 {
47     return (os_vm_address_t)code->si_addr;
48 }
49 #endif
50
51 \f
52 /*
53  * hacking signal contexts
54  *
55  * (This depends both on architecture, which determines what we might
56  * want to get to, and on OS, which determines how we get to it.)
57  */
58
59 int *
60 context_eflags_addr(os_context_t *context)
61 {
62 #if defined __linux__ || defined __sun
63     /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
64      * <sys/ucontext.h> file to define symbolic names for offsets into
65      * gregs[], but it's conditional on __USE_GNU and not defined, so
66      * we need to do this nasty absolute index magic number thing
67      * instead. */
68     return &context->uc_mcontext.gregs[16];
69 #elif defined __FreeBSD__
70     return &context->uc_mcontext.mc_eflags;
71 #elif defined __OpenBSD__
72     return &context->sc_eflags;
73 #elif defined LISP_FEATURE_DARWIN
74     return (int *)(&context->uc_mcontext->SS.EFLAGS);
75 #elif defined __NetBSD__
76     return &(context->uc_mcontext.__gregs[_REG_EFL]);
77 #elif defined LISP_FEATURE_WIN32
78     return (int *)&context->win32_context->EFlags;
79 #else
80 #error unsupported OS
81 #endif
82 }
83 \f
84 void arch_skip_instruction(os_context_t *context)
85 {
86     /* Assuming we get here via an INT3 xxx instruction, the PC now
87      * points to the interrupt code (a Lisp value) so we just move
88      * past it. Skip the code; after that, if the code is an
89      * error-trap or cerror-trap then skip the data bytes that follow. */
90
91     int vlen;
92     int code;
93
94
95     /* Get and skip the Lisp interrupt code. */
96     code = *(char*)(*os_context_pc_addr(context))++;
97     switch (code)
98         {
99         case trap_Error:
100         case trap_Cerror:
101             /* Lisp error arg vector length */
102             vlen = *(char*)(*os_context_pc_addr(context))++;
103             /* Skip Lisp error arg data bytes. */
104             while (vlen-- > 0) {
105                 ++*os_context_pc_addr(context);
106             }
107             break;
108
109         case trap_Breakpoint:           /* not tested */
110         case trap_FunEndBreakpoint: /* not tested */
111             break;
112
113 #ifdef LISP_FEATURE_SB_SAFEPOINT
114         case trap_GlobalSafepoint:
115         case trap_CspSafepoint:
116 #endif
117         case trap_PendingInterrupt:
118         case trap_Halt:
119         case trap_SingleStepAround:
120         case trap_SingleStepBefore:
121             /* only needed to skip the Code */
122             break;
123
124         default:
125             fprintf(stderr,"[arch_skip_inst invalid code %d\n]\n",code);
126             break;
127         }
128
129     FSHOW((stderr,
130            "/[arch_skip_inst resuming at %x]\n",
131            *os_context_pc_addr(context)));
132 }
133
134 unsigned char *
135 arch_internal_error_arguments(os_context_t *context)
136 {
137     return 1 + (unsigned char *)(*os_context_pc_addr(context));
138 }
139
140 boolean
141 arch_pseudo_atomic_atomic(os_context_t *context)
142 {
143     return get_pseudo_atomic_atomic(arch_os_get_current_thread());
144 }
145
146 void
147 arch_set_pseudo_atomic_interrupted(os_context_t *context)
148 {
149     struct thread *thread = arch_os_get_current_thread();
150     set_pseudo_atomic_interrupted(thread);
151 }
152
153 void
154 arch_clear_pseudo_atomic_interrupted(os_context_t *context)
155 {
156     struct thread *thread = arch_os_get_current_thread();
157     clear_pseudo_atomic_interrupted(thread);
158 }
159 \f
160 /*
161  * This stuff seems to get called for TRACE and debug activity.
162  */
163
164 unsigned int
165 arch_install_breakpoint(void *pc)
166 {
167     unsigned int result = *(unsigned int*)pc;
168
169 #ifndef LISP_FEATURE_UD2_BREAKPOINTS
170     *(char*)pc = BREAKPOINT_INST;               /* x86 INT3       */
171     *((char*)pc+1) = trap_Breakpoint;           /* Lisp trap code */
172 #else
173     *(char*)pc = UD2_INST & 0xff;
174     *((char*)pc+1) = UD2_INST >> 8;
175     *((char*)pc+2) = trap_Breakpoint;
176 #endif
177
178     return result;
179 }
180
181 void
182 arch_remove_breakpoint(void *pc, unsigned int orig_inst)
183 {
184     *((char *)pc) = orig_inst & 0xff;
185     *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
186 #if BREAKPOINT_WIDTH > 1
187     *((char *)pc + 2) = (orig_inst & 0xff0000) >> 16;
188 #endif
189 }
190 \f
191 /* When single stepping, single_stepping holds the original instruction
192  * PC location. */
193 unsigned int *single_stepping = NULL;
194 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
195 unsigned int  single_step_save1;
196 unsigned int  single_step_save2;
197 unsigned int  single_step_save3;
198 #endif
199
200 void
201 arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
202 {
203     unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
204
205     /* Put the original instruction back. */
206     arch_remove_breakpoint(pc, orig_inst);
207
208 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
209     /* Install helper instructions for the single step:
210      * pushf; or [esp],0x100; popf. */
211     single_step_save1 = *(pc-3);
212     single_step_save2 = *(pc-2);
213     single_step_save3 = *(pc-1);
214     *(pc-3) = 0x9c909090;
215     *(pc-2) = 0x00240c81;
216     *(pc-1) = 0x9d000001;
217 #else
218     *context_eflags_addr(context) |= 0x100;
219 #endif
220
221     single_stepping = pc;
222
223 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
224     *os_context_pc_addr(context) = (os_context_register_t)((char *)pc - 9);
225 #endif
226 }
227 \f
228 void
229 restore_breakpoint_from_single_step(os_context_t * context)
230 {
231     /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
232 #ifdef CANNOT_GET_TO_SINGLE_STEP_FLAG
233     /* Un-install single step helper instructions. */
234     *(single_stepping-3) = single_step_save1;
235     *(single_stepping-2) = single_step_save2;
236     *(single_stepping-1) = single_step_save3;
237 #else
238     *context_eflags_addr(context) &= ~0x100;
239 #endif
240     /* Re-install the breakpoint if possible. */
241     if (((char *)*os_context_pc_addr(context) >
242          (char *)single_stepping) &&
243         ((char *)*os_context_pc_addr(context) <=
244          (char *)single_stepping + BREAKPOINT_WIDTH)) {
245         fprintf(stderr, "warning: couldn't reinstall breakpoint\n");
246     } else {
247         arch_install_breakpoint(single_stepping);
248     }
249
250     single_stepping = NULL;
251     return;
252 }
253
254 void
255 arch_handle_breakpoint(os_context_t *context)
256 {
257     *os_context_pc_addr(context) -= BREAKPOINT_WIDTH;
258     handle_breakpoint(context);
259 }
260
261 void
262 arch_handle_fun_end_breakpoint(os_context_t *context)
263 {
264     *os_context_pc_addr(context) -= BREAKPOINT_WIDTH;
265     *os_context_pc_addr(context) =
266         (int)handle_fun_end_breakpoint(context);
267 }
268
269 void
270 arch_handle_single_step_trap(os_context_t *context, int trap)
271 {
272     arch_skip_instruction(context);
273     /* On x86 the fdefn / function is always in EAX, so we pass 0
274      * as the register_offset. */
275     handle_single_step_trap(context, trap, 0);
276 }
277
278 #ifndef LISP_FEATURE_WIN32
279 void
280 sigtrap_handler(int signal, siginfo_t *info, os_context_t *context)
281 {
282     unsigned int trap;
283
284     if (single_stepping) {
285         restore_breakpoint_from_single_step(context);
286         return;
287     }
288
289     /* This is just for info in case the monitor wants to print an
290      * approximation. */
291     access_control_stack_pointer(arch_os_get_current_thread()) =
292         (lispobj *)*os_context_sp_addr(context);
293
294 #ifdef LISP_FEATURE_SUNOS
295     /* For some reason the breakpoints that :ENCAPSULATE NIL tracing sets up
296      * cause a trace trap (i.e. processor single-stepping trap) on the following
297      * instruction on Solaris 10/x86. -- JES, 2006-04-07
298      */
299     if (info->si_code == TRAP_TRACE) {
300         lose("foo");
301         return;
302     }
303 #endif
304
305     /* On entry %eip points just after the INT3 byte and aims at the
306      * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
307      * number of bytes will follow, the first is the length of the byte
308      * arguments to follow. */
309     trap = *(unsigned char *)(*os_context_pc_addr(context));
310     handle_trap(context, trap);
311 }
312
313 void
314 sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
315     /* Triggering SIGTRAP using int3 is unreliable on OS X/x86, so
316      * we need to use illegal instructions for traps.
317      */
318 #if defined(LISP_FEATURE_UD2_BREAKPOINTS) && !defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
319     if (*((unsigned short *)*os_context_pc_addr(context)) == UD2_INST) {
320         *os_context_pc_addr(context) += 2;
321         return sigtrap_handler(signal, siginfo, context);
322     }
323 #endif
324     fake_foreign_function_call(context);
325     lose("Unhandled SIGILL");
326 }
327 #endif /* not LISP_FEATURE_WIN32 */
328
329 void
330 arch_install_interrupt_handlers()
331 {
332     SHOW("entering arch_install_interrupt_handlers()");
333
334     /* Note: The old CMU CL code here used sigtrap_handler() to handle
335      * SIGILL as well as SIGTRAP. I couldn't see any reason to do
336      * things that way. So, I changed to separate handlers when
337      * debugging a problem on OpenBSD, where SBCL wasn't catching
338      * SIGILL properly, but was instead letting the process be
339      * terminated with an "Illegal instruction" output. If this change
340      * turns out to break something (maybe breakpoint handling on some
341      * OS I haven't tested on?) and we have to go back to the old CMU
342      * CL way, I hope there will at least be a comment to explain
343      * why.. -- WHN 2001-06-07 */
344 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
345     undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
346     undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
347 #endif
348     SHOW("returning from arch_install_interrupt_handlers()");
349 }
350 \f
351 #ifdef LISP_FEATURE_LINKAGE_TABLE
352 /* FIXME: It might be cleaner to generate these from the lisp side of
353  * things.
354  */
355
356 void
357 arch_write_linkage_table_jmp(char * reloc, void * fun)
358 {
359     /* Make JMP to function entry. JMP offset is calculated from next
360      * instruction.
361      */
362     long offset = (char *)fun - (reloc + 5);
363     int i;
364
365     *reloc++ = 0xe9;            /* opcode for JMP rel32 */
366     for (i = 0; i < 4; i++) {
367         *reloc++ = offset & 0xff;
368         offset >>= 8;
369     }
370
371     /* write a nop for good measure. */
372     *reloc = 0x90;
373 }
374
375 void
376 arch_write_linkage_table_ref(void * reloc, void * data)
377 {
378     *(unsigned long *)reloc = (unsigned long)data;
379 }
380
381 #endif