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