Initial revision
[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 /*
13  * $Header$
14  */
15
16 #include <stdio.h>
17
18 #include "runtime.h"
19 #include "globals.h"
20 #include "validate.h"
21 #include "os.h"
22 #include "sbcl.h"
23 #include "arch.h"
24 #include "lispregs.h"
25 #include "signal.h"
26 #include "alloc.h"
27 #include "interrupt.h"
28 #include "interr.h"
29 #include "breakpoint.h"
30
31 #define BREAKPOINT_INST 0xcc    /* INT3 */
32
33 unsigned long fast_random_state = 1;
34
35 void arch_init(void)
36 {}
37 \f
38 /*
39  * hacking signal contexts
40  *
41  * (This depends both on architecture, which determines what we might
42  * want to get to, and on OS, which determines how we get to it.)
43  */
44
45 int *
46 context_eflags_addr(os_context_t *context)
47 {
48 #if defined __linux__
49     /* KLUDGE: As of kernel 2.2.14 on Red Hat 6.2, there's code in the
50      * <sys/ucontext.h> file to define symbolic names for offsets into
51      * gregs[], but it's conditional on __USE_GNU and not defined, so
52      * we need to do this nasty absolute index magic number thing
53      * instead. */
54     return &context->uc_mcontext.gregs[16];
55 #elif defined __FreeBSD__
56     return &context->uc_mcontext.mc_eflags;
57 #elif defined __OpenBSD__
58     return &context->sc_eflags;
59 #else
60 #error unsupported OS
61 #endif
62 }
63 \f
64 void arch_skip_instruction(os_context_t *context)
65 {
66     /* Assuming we get here via an INT3 xxx instruction, the PC now
67      * points to the interrupt code (a Lisp value) so we just move
68      * past it. Skip the code; after that, if the code is an
69      * error-trap or cerror-trap then skip the data bytes that follow. */
70
71     int vlen;
72     int code;
73
74     FSHOW((stderr, "[arch_skip_inst at %x]\n", *os_context_pc_addr(context)));
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_FunctionEndBreakpoint: /* 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);
119 }
120
121 void
122 arch_set_pseudo_atomic_interrupted(os_context_t *context)
123 {
124     SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1));
125 }
126 \f
127 /*
128  * This stuff seems to get called for TRACE and debug activity.
129  */
130
131 unsigned long
132 arch_install_breakpoint(void *pc)
133 {
134     unsigned long result = *(unsigned long*)pc;
135
136     *(char*)pc = BREAKPOINT_INST;               /* x86 INT3       */
137     *((char*)pc+1) = trap_Breakpoint;           /* Lisp trap code */
138
139     return result;
140 }
141
142 void
143 arch_remove_breakpoint(void *pc, unsigned long orig_inst)
144 {
145     *((char *)pc) = orig_inst & 0xff;
146     *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
147 }
148 \f
149 /* When single stepping, single_stepping holds the original instruction
150  * PC location. */
151 unsigned int *single_stepping=NULL;
152 #ifndef __linux__
153 unsigned int  single_step_save1;
154 unsigned int  single_step_save2;
155 unsigned int  single_step_save3;
156 #endif
157
158 void
159 arch_do_displaced_inst(os_context_t *context, unsigned long orig_inst)
160 {
161     unsigned int *pc = (unsigned int*)(*os_context_pc_addr(context));
162
163     /* Put the original instruction back. */
164     *((char *)pc) = orig_inst & 0xff;
165     *((char *)pc + 1) = (orig_inst & 0xff00) >> 8;
166
167 #ifdef __linux__
168     *context_eflags_addr(context) |= 0x100;
169 #else
170     /* Install helper instructions for the single step:
171      * pushf; or [esp],0x100; popf. */
172     single_step_save1 = *(pc-3);
173     single_step_save2 = *(pc-2);
174     single_step_save3 = *(pc-1);
175     *(pc-3) = 0x9c909090;
176     *(pc-2) = 0x00240c81;
177     *(pc-1) = 0x9d000001;
178 #endif
179
180     single_stepping = (unsigned int*)pc;
181
182 #ifndef __linux__
183     *os_context_pc_addr(context) = (char *)pc - 9;
184 #endif
185 }
186 \f
187 void
188 sigtrap_handler(int signal, siginfo_t *info, void *void_context)
189 {
190     int code = info->si_code;
191     os_context_t *context = (os_context_t*)void_context;
192     unsigned int trap;
193
194     SHOW("entering sigtrap_handler(..)"); /* REMOVEME */
195
196     if (single_stepping && (signal==SIGTRAP))
197     {
198         /* fprintf(stderr,"* single step trap %x\n", single_stepping); */
199
200 #ifndef __linux__
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 *ptr = (char*)single_stepping;
213             *((char *)single_stepping) = BREAKPOINT_INST;       /* x86 INT3 */
214             *((char *)single_stepping+1) = trap_Breakpoint;
215         }
216
217         single_stepping = NULL;
218         return;
219     }
220
221     /* This is just for info in case the monitor wants to print an
222      * approximation. */
223     current_control_stack_pointer =
224         (lispobj*)*os_context_sp_addr(context);
225
226     /* On entry %eip points just after the INT3 byte and aims at the
227      * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
228      * number of bytes will follow, the first is the length of the byte
229      * arguments to follow. */
230     trap = *(unsigned char *)(*os_context_pc_addr(context));
231     switch (trap) {
232
233     case trap_PendingInterrupt:
234         FSHOW((stderr, "<trap pending interrupt>\n"));
235         arch_skip_instruction(context);
236         interrupt_handle_pending(context);
237         break;
238
239     case trap_Halt:
240         /* Note: the old CMU CL code tried to save FPU state
241          * here, and restore it after we do our thing, but there
242          * seems to be no point in doing that, since we're just
243          * going to lose(..) anyway. */
244         SHOW("in trap_Halt case of sigtrap_handler(..)"); /* REMOVEME */
245         fake_foreign_function_call(context);
246         lose("%%primitive halt called; the party is over.");
247
248     case trap_Error:
249     case trap_Cerror:
250         FSHOW((stderr, "<trap error/cerror %d>\n", code));
251         interrupt_internal_error(signal, info, context, code==trap_Cerror);
252         break;
253
254     case trap_Breakpoint:
255         (char*)(*os_context_pc_addr(context)) -= 1;
256         handle_breakpoint(signal, info, context);
257         break;
258
259     case trap_FunctionEndBreakpoint:
260         (char*)(*os_context_pc_addr(context)) -= 1;
261         *os_context_pc_addr(context) =
262             (int)handle_function_end_breakpoint(signal, info, context);
263         break;
264
265     default:
266         FSHOW((stderr,"[C--trap default %d %d %x]\n",
267                signal, code, context));
268         interrupt_handle_now(signal, info, context);
269         break;
270     }
271     SHOW("leaving sigtrap_handler(..)"); /* REMOVEME */
272 }
273
274 void
275 arch_install_interrupt_handlers()
276 {
277     interrupt_install_low_level_handler(SIGILL , sigtrap_handler);
278     interrupt_install_low_level_handler(SIGTRAP, sigtrap_handler);
279 }
280 \f
281 /* This is implemented in assembly language and called from C: */
282 extern lispobj
283 call_into_lisp(lispobj fun, lispobj *args, int nargs);
284
285 /* These functions are an interface to the Lisp call-in facility.
286  * Since this is C we can know nothing about the calling environment.
287  * The control stack might be the C stack if called from the monitor
288  * or the Lisp stack if called as a result of an interrupt or maybe
289  * even a separate stack. The args are most likely on that stack but
290  * could be in registers depending on what the compiler likes. So we
291  * copy the args into a portable vector and let the assembly language
292  * call-in function figure it out. */
293 lispobj
294 funcall0(lispobj function)
295 {
296     lispobj *args = NULL;
297
298     return call_into_lisp(function, args, 0);
299 }
300 lispobj
301 funcall1(lispobj function, lispobj arg0)
302 {
303     lispobj args[1];
304     args[0] = arg0;
305     return call_into_lisp(function, args, 1);
306 }
307 lispobj
308 funcall2(lispobj function, lispobj arg0, lispobj arg1)
309 {
310     lispobj args[2];
311     args[0] = arg0;
312     args[1] = arg1;
313     return call_into_lisp(function, args, 2);
314 }
315 lispobj
316 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
317 {
318     lispobj args[3];
319     args[0] = arg0;
320     args[1] = arg1;
321     args[2] = arg2;
322     return call_into_lisp(function, args, 3);
323 }