1.0.4.13: refactor trap handling
[sbcl.git] / src / runtime / sparc-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 #include <stdio.h>
12
13 #include "sbcl.h"
14 #include "runtime.h"
15 #include "arch.h"
16 #include "globals.h"
17 #include "validate.h"
18 #include "os.h"
19 #include "lispregs.h"
20 #include "signal.h"
21 #include "alloc.h"
22 #include "interrupt.h"
23 #include "interr.h"
24 #include "breakpoint.h"
25 #include "monitor.h"
26
27 #ifdef LISP_FEATURE_LINUX
28 extern int linux_sparc_siginfo_bug;
29 #endif
30
31 void arch_init(void)
32 {
33     return;
34 }
35
36 os_vm_address_t arch_get_bad_addr(int sig, siginfo_t *code, os_context_t *context)
37 {
38     unsigned int badinst;
39     unsigned int *pc;
40     int rs1;
41
42     pc = (unsigned int *)(*os_context_pc_addr(context));
43
44     /* On the sparc, we have to decode the instruction. */
45
46     /* Make sure it's not the pc thats bogus, and that it was lisp code */
47     /* that caused the fault. */
48     if ((unsigned long) pc & 3) {
49       /* Unaligned */
50       return NULL;
51     }
52     if ((pc < READ_ONLY_SPACE_START ||
53          pc >= READ_ONLY_SPACE_START+READ_ONLY_SPACE_SIZE) &&
54         (pc < current_dynamic_space ||
55          pc >= current_dynamic_space + dynamic_space_size)) {
56       return NULL;
57     }
58
59     badinst = *pc;
60
61     if ((badinst >> 30) != 3)
62         /* All load/store instructions have op = 11 (binary) */
63         return 0;
64
65     rs1 = (badinst>>14)&0x1f;
66
67     if (badinst & (1<<13)) {
68         /* r[rs1] + simm(13) */
69         int simm13 = badinst & 0x1fff;
70
71         if (simm13 & (1<<12))
72             simm13 |= -1<<13;
73
74         return (os_vm_address_t)
75             (*os_context_register_addr(context, rs1)+simm13);
76     }
77     else {
78         /* r[rs1] + r[rs2] */
79         int rs2 = badinst & 0x1f;
80
81         return (os_vm_address_t)
82             (*os_context_register_addr(context, rs1) +
83              *os_context_register_addr(context, rs2));
84     }
85 }
86
87 void arch_skip_instruction(os_context_t *context)
88 {
89     *os_context_pc_addr(context) = *os_context_npc_addr(context);
90     /* Note that we're doing integer arithmetic here, not pointer. So
91      * the value that the return value of os_context_npc_addr() points
92      * to will be incremented by 4, not 16.
93      */
94     *os_context_npc_addr(context) += 4;
95 }
96
97 unsigned char *arch_internal_error_arguments(os_context_t *context)
98 {
99     return (unsigned char *)(*os_context_pc_addr(context) + 4);
100 }
101
102 boolean arch_pseudo_atomic_atomic(os_context_t *context)
103 {
104     return ((*os_context_register_addr(context,reg_ALLOC)) & 4);
105 }
106
107 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
108 {
109     *os_context_register_addr(context,reg_ALLOC) |=  1;
110 }
111
112 void arch_clear_pseudo_atomic_interrupted(os_context_t *context)
113 {
114     *os_context_register_addr(context,reg_ALLOC) &= ~1;
115 }
116
117 unsigned int arch_install_breakpoint(void *pc)
118 {
119     unsigned int *ptr = (unsigned int *)pc;
120     unsigned int result = *ptr;
121     *ptr = trap_Breakpoint;
122
123     os_flush_icache((os_vm_address_t) pc, sizeof(unsigned int));
124
125     return result;
126 }
127
128 void arch_remove_breakpoint(void *pc, unsigned int orig_inst)
129 {
130     *(unsigned int *)pc = orig_inst;
131     os_flush_icache((os_vm_address_t) pc, sizeof(unsigned int));
132 }
133
134 /*
135  * Perform the instruction that we overwrote with a breakpoint.  As we
136  * don't have a single-step facility, this means we have to:
137  * - put the instruction back
138  * - put a second breakpoint at the following instruction,
139  *   set after_breakpoint and continue execution.
140  *
141  * When the second breakpoint is hit (very shortly thereafter, we hope)
142  * sigtrap_handler gets called again, but follows the AfterBreakpoint
143  * arm, which
144  * - puts a bpt back in the first breakpoint place (running across a
145  *   breakpoint shouldn't cause it to be uninstalled)
146  * - replaces the second bpt with the instruction it was meant to be
147  * - carries on
148  *
149  * Clear?
150  */
151 static unsigned int *skipped_break_addr, displaced_after_inst;
152 static sigset_t orig_sigmask;
153
154 void arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
155 {
156     unsigned int *pc = (unsigned int *)(*os_context_pc_addr(context));
157     unsigned int *npc = (unsigned int *)(*os_context_npc_addr(context));
158
159   /*  orig_sigmask = context->sigmask;
160       sigemptyset(&context->sigmask); */
161   /* FIXME!!! */
162   /* FILLBLOCKSET(&context->uc_sigmask);*/
163
164     *pc = orig_inst;
165     os_flush_icache((os_vm_address_t) pc, sizeof(unsigned int));
166     skipped_break_addr = pc;
167     displaced_after_inst = *npc;
168     *npc = trap_AfterBreakpoint;
169     os_flush_icache((os_vm_address_t) npc, sizeof(unsigned int));
170
171 }
172
173 static int pseudo_atomic_trap_p(os_context_t *context)
174 {
175     unsigned int* pc;
176     unsigned int badinst;
177     int result;
178
179
180     pc = (unsigned int*) *os_context_pc_addr(context);
181     badinst = *pc;
182     result = 0;
183
184     /* Check to see if the current instruction is a pseudo-atomic-trap */
185     if (((badinst >> 30) == 2) && (((badinst >> 19) & 0x3f) == 0x3a)
186         && (((badinst >> 13) & 1) == 1) && ((badinst & 0x7f) == PSEUDO_ATOMIC_TRAP))
187         {
188             unsigned int previnst;
189             previnst = pc[-1];
190             /*
191              * Check to see if the previous instruction was an andcc alloc-tn,
192              * 3, zero-tn instruction.
193              */
194             if (((previnst >> 30) == 2) && (((previnst >> 19) & 0x3f) == 0x11)
195                 && (((previnst >> 14) & 0x1f) == reg_ALLOC)
196                 && (((previnst >> 25) & 0x1f) == reg_ZERO)
197                 && (((previnst >> 13) & 1) == 1)
198                 && ((previnst & 0x1fff) == 3))
199                 {
200                     result = 1;
201                 }
202             else
203                 {
204                     fprintf(stderr, "Oops!  Got a PSEUDO-ATOMIC-TRAP without a preceeding andcc!\n");
205                 }
206         }
207     return result;
208 }
209
210 void
211 arch_handle_breakpoint(os_context_t *context)
212 {
213     handle_breakpoint(context);
214 }
215
216 void
217 arch_handle_fun_end_breakpoint(os_context_t *context)
218 {
219     *os_context_pc_addr(context) = (int) handle_fun_end_breakpoint(context);
220     *os_context_npc_addr(context) = *os_context_pc_addr(context) + 4;
221 }
222
223 void
224 arch_handle_after_breakpoint(os_context_t *context)
225 {
226     *skipped_break_addr = trap_Breakpoint;
227     os_flush_icache(skipped_break_addr, sizeof(unsigned int));
228     skipped_break_addr = NULL;
229     *(unsigned long *) os_context_pc_addr(context) = displaced_after_inst;
230     /* context->sigmask = orig_sigmask; */
231     os_flush_icache((os_vm_address_t) os_context_pc_addr(context), sizeof(unsigned int));
232 }
233
234 static void sigill_handler(int signal, siginfo_t *siginfo, void *void_context)
235 {
236     os_context_t *context = arch_os_get_context(&void_context);
237 #ifdef LISP_FEATURE_LINUX
238     /* FIXME: Check that this is necessary -- CSR, 2002-07-15 */
239     os_restore_fp_control(context);
240 #endif
241
242     if ((siginfo->si_code) == ILL_ILLOPC
243 #ifdef LISP_FEATURE_LINUX
244         || (linux_sparc_siginfo_bug && (siginfo->si_code == 2))
245 #endif
246         ) {
247         int trap;
248         unsigned int inst;
249         unsigned int* pc = (unsigned int*) siginfo->si_addr;
250
251         inst = *pc;
252         trap = inst & 0x3fffff;
253         if (!maybe_handle_trap(context,trap))
254             interrupt_handle_now(signal, siginfo, context);
255     }
256     else if ((siginfo->si_code) == ILL_ILLTRP
257 #ifdef LISP_FEATURE_LINUX
258              || (linux_sparc_siginfo_bug && (siginfo->si_code) == 192)
259 #endif
260              ) {
261         if (pseudo_atomic_trap_p(context)) {
262             /* A trap instruction from a pseudo-atomic.  We just need
263                to fixup up alloc-tn to remove the interrupted flag,
264                skip over the trap instruction, and then handle the
265                pending interrupt(s). */
266             arch_clear_pseudo_atomic_interrupted(context);
267             arch_skip_instruction(context);
268             interrupt_handle_pending(context);
269         }
270         else {
271             interrupt_internal_error(context, 0);
272         }
273     }
274     else {
275         interrupt_handle_now(signal, siginfo, context);
276     }
277 }
278
279 static void sigemt_handler(int signal, siginfo_t *siginfo, void *void_context)
280 {
281     unsigned int badinst;
282     boolean subtract, immed;
283     int rd, rs1, op1, rs2, op2, result;
284     os_context_t *context = arch_os_get_context(&void_context);
285 #ifdef LISP_FEATURE_LINUX
286     os_restore_fp_control(context);
287 #endif
288
289     badinst = *(unsigned int *)os_context_pc_addr(context);
290     if ((badinst >> 30) != 2 || ((badinst >> 20) & 0x1f) != 0x11) {
291         /* It wasn't a tagged add.  Pass the signal into lisp. */
292         interrupt_handle_now(signal, siginfo, context);
293         return;
294     }
295
296     fprintf(stderr, "SIGEMT trap handler with tagged op instruction!\n");
297
298     /* Extract the parts of the inst. */
299     subtract = badinst & (1<<19);
300     rs1 = (badinst>>14) & 0x1f;
301     op1 = *os_context_register_addr(context, rs1);
302
303     /* If the first arg is $ALLOC then it is really a signal-pending note */
304     /* for the pseudo-atomic noise. */
305     if (rs1 == reg_ALLOC) {
306         /* Perform the op anyway. */
307         op2 = badinst & 0x1fff;
308         if (op2 & (1<<12))
309             op2 |= -1<<13;
310         if (subtract)
311             result = op1 - op2;
312         else
313             result = op1 + op2;
314         /* KLUDGE: this & ~7 is a little bit magical but basically
315            clears pseudo_atomic bits if any */
316         *os_context_register_addr(context, reg_ALLOC) = result & ~7;
317         arch_skip_instruction(context);
318         interrupt_handle_pending(context);
319         return;
320     }
321
322     if ((op1 & 3) != 0) {
323         /* The first arg wan't a fixnum. */
324         interrupt_internal_error(context, 0);
325         return;
326     }
327
328     if (immed = badinst & (1<<13)) {
329         op2 = badinst & 0x1fff;
330         if (op2 & (1<<12))
331             op2 |= -1<<13;
332     }
333     else {
334         rs2 = badinst & 0x1f;
335         op2 = *os_context_register_addr(context, rs2);
336     }
337
338     if ((op2 & 3) != 0) {
339         /* The second arg wan't a fixnum. */
340         interrupt_internal_error(context, 0);
341         return;
342     }
343
344     rd = (badinst>>25) & 0x1f;
345     if (rd != 0) {
346         /* Don't bother computing the result unless we are going to use it. */
347         if (subtract)
348             result = (op1>>2) - (op2>>2);
349         else
350             result = (op1>>2) + (op2>>2);
351
352         dynamic_space_free_pointer =
353             (lispobj *) *os_context_register_addr(context, reg_ALLOC);
354
355         *os_context_register_addr(context, rd) = alloc_number(result);
356
357         *os_context_register_addr(context, reg_ALLOC) =
358             (unsigned long) dynamic_space_free_pointer;
359     }
360
361     arch_skip_instruction(context);
362 }
363
364 void arch_install_interrupt_handlers()
365 {
366     undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
367     undoably_install_low_level_interrupt_handler(SIGEMT, sigemt_handler);
368 }
369
370 \f
371 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
372
373 lispobj funcall0(lispobj function)
374 {
375     lispobj *args = current_control_stack_pointer;
376
377     return call_into_lisp(function, args, 0);
378 }
379
380 lispobj funcall1(lispobj function, lispobj arg0)
381 {
382     lispobj *args = current_control_stack_pointer;
383
384     current_control_stack_pointer += 1;
385     args[0] = arg0;
386
387     return call_into_lisp(function, args, 1);
388 }
389
390 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
391 {
392     lispobj *args = current_control_stack_pointer;
393
394     current_control_stack_pointer += 2;
395     args[0] = arg0;
396     args[1] = arg1;
397
398     return call_into_lisp(function, args, 2);
399 }
400
401 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
402 {
403     lispobj *args = current_control_stack_pointer;
404
405     current_control_stack_pointer += 3;
406     args[0] = arg0;
407     args[1] = arg1;
408     args[2] = arg2;
409
410     return call_into_lisp(function, args, 3);
411 }
412
413 #ifdef LISP_FEATURE_LINKAGE_TABLE
414
415 /* This a naive port from CMUCL/sparc, which was mostly stolen from the
416  * CMUCL/x86 version, with adjustments for sparc
417  *
418  * Linkage entry size is 16, because we need at least 3 instruction to
419  * implement a jump:
420  *
421  *      sethi %hi(addr), %g4
422  *      jmpl  [%g4 + %lo(addr)], %g5
423  *      nop
424  *
425  * The Sparc V9 ABI seems to use 8 words for its jump tables.  Maybe
426  * we should do the same?
427  */
428
429 /*
430  * Define the registers to use in the linkage jump table. Can be the
431  * same. Some care must be exercised when choosing these. It has to be
432  * a register that is not otherwise being used. reg_L0 is a good
433  * choice. call_into_c trashes reg_L0 without preserving it, so we can
434  * trash it in the linkage jump table.
435  */
436 #define LINKAGE_TEMP_REG        reg_L0
437 #define LINKAGE_ADDR_REG        reg_L0
438
439 /*
440  * Insert the necessary jump instructions at the given address.
441  */
442 void
443 arch_write_linkage_table_jmp(void* reloc_addr, void *target_addr)
444 {
445   /*
446    * Make JMP to function entry.
447    *
448    * The instruction sequence is:
449    *
450    *        sethi %hi(addr), temp_reg
451    *        jmp   %temp_reg + %lo(addr), %addr_reg
452    *        nop
453    *        nop
454    *
455    */
456   int* inst_ptr;
457   unsigned long hi;                   /* Top 22 bits of address */
458   unsigned long lo;                   /* Low 10 bits of address */
459   unsigned int inst;
460
461   inst_ptr = (int*) reloc_addr;
462
463   /*
464    * Split the target address into hi and lo parts for the sethi
465    * instruction.  hi is the top 22 bits.  lo is the low 10 bits.
466    */
467   hi = (unsigned long) target_addr;
468   lo = hi & 0x3ff;
469   hi >>= 10;
470
471   /*
472    * sethi %hi(addr), temp_reg
473    */
474
475   inst = (0 << 30) | (LINKAGE_TEMP_REG << 25) | (4 << 22) | hi;
476   *inst_ptr++ = inst;
477
478   /*
479    * jmpl [temp_reg + %lo(addr)], addr_reg
480    */
481
482   inst = (2U << 30) | (LINKAGE_ADDR_REG << 25) | (0x38 << 19)
483     | (LINKAGE_TEMP_REG << 14) | (1 << 13) | lo;
484   *inst_ptr++ = inst;
485
486   /* nop (really sethi 0, %g0) */
487
488   inst = (0 << 30) | (0 << 25) | (4 << 22) | 0;
489
490   *inst_ptr++ = inst;
491   *inst_ptr++ = inst;
492
493   os_flush_icache((os_vm_address_t) reloc_addr, (char*) inst_ptr - (char*) reloc_addr);
494 }
495
496 void
497 arch_write_linkage_table_ref(void * reloc_addr, void *target_addr)
498 {
499     *(unsigned long *)reloc_addr = (unsigned long)target_addr;
500 }
501
502 #endif