0.7.6.27:
[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 "runtime.h"
14 #include "arch.h"
15 #include "sbcl.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 early_kernel;
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 long badinst;
39     unsigned long *pc;
40     int rs1; 
41
42     pc = (unsigned long *)(*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 long arch_install_breakpoint(void *pc)
109 {
110     unsigned long *ptr = (unsigned long *)pc;
111     unsigned long result = *ptr;
112     *ptr = trap_Breakpoint;
113   
114     os_flush_icache((os_vm_address_t) pc, sizeof(unsigned long));
115     
116     return result;
117 }
118
119 void arch_remove_breakpoint(void *pc, unsigned long orig_inst)
120 {
121     *(unsigned long *)pc = orig_inst;
122     os_flush_icache((os_vm_address_t) pc, sizeof(unsigned long));
123 }
124
125 static unsigned long *skipped_break_addr, displaced_after_inst;
126 static sigset_t orig_sigmask;
127
128 void arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
129 {
130     unsigned long *pc = (unsigned long *)(*os_context_pc_addr(context));
131     unsigned long *npc = (unsigned long *)(*os_context_npc_addr(context));
132
133   /*  orig_sigmask = context->sigmask;
134       sigemptyset(&context->sigmask); */
135   /* FIXME!!! */
136   /* FILLBLOCKSET(&context->uc_sigmask);*/
137
138     *pc = orig_inst;
139     os_flush_icache((os_vm_address_t) pc, sizeof(unsigned long));
140     skipped_break_addr = pc;
141     displaced_after_inst = *npc;
142     *npc = trap_AfterBreakpoint;
143     os_flush_icache((os_vm_address_t) npc, sizeof(unsigned long));
144     
145 }
146
147 static int pseudo_atomic_trap_p(os_context_t *context)
148 {
149     unsigned int* pc;
150     unsigned int badinst;
151     int result;
152     
153     
154     pc = (unsigned int*) *os_context_pc_addr(context);
155     badinst = *pc;
156     result = 0;
157     
158     /* Check to see if the current instruction is a pseudo-atomic-trap */
159     if (((badinst >> 30) == 2) && (((badinst >> 19) & 0x3f) == 0x3a)
160         && (((badinst >> 13) & 1) == 1) && ((badinst & 0x7f) == PSEUDO_ATOMIC_TRAP))
161         {
162             unsigned int previnst;
163             previnst = pc[-1];
164             /*
165              * Check to see if the previous instruction was an andcc alloc-tn,
166              * 3, zero-tn instruction.
167              */
168             if (((previnst >> 30) == 2) && (((previnst >> 19) & 0x3f) == 0x11)
169                 && (((previnst >> 14) & 0x1f) == reg_ALLOC)
170                 && (((previnst >> 25) & 0x1f) == reg_ZERO)
171                 && (((previnst >> 13) & 1) == 1)
172                 && ((previnst & 0x1fff) == 3))
173                 {
174                     result = 1;
175                 }
176             else
177                 {
178                     fprintf(stderr, "Oops!  Got a PSEUDO-ATOMIC-TRAP without a preceeding andcc!\n");
179                 }
180         }
181     return result;
182 }
183
184 static void sigill_handler(int signal, siginfo_t *siginfo, void *void_context)
185 {
186     os_context_t *context = arch_os_get_context(&void_context);
187 #ifdef LISP_FEATURE_LINUX
188     /* FIXME: Check that this is necessary -- CSR, 2002-07-15 */
189     os_restore_fp_control(context);
190 #endif
191     sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
192     
193     if ((siginfo->si_code) == ILL_ILLOPC
194 #ifdef LISP_FEATURE_LINUX
195         || (early_kernel && (siginfo->si_code == 2))
196 #endif
197         ) {
198         int trap;
199         unsigned int inst;
200         unsigned int* pc = (unsigned int*) siginfo->si_addr;
201
202         inst = *pc;
203         trap = inst & 0x3fffff;
204         
205         switch (trap) {
206         case trap_PendingInterrupt:
207             arch_skip_instruction(context);
208             interrupt_handle_pending(context);
209             break;
210             
211         case trap_Halt:
212             fake_foreign_function_call(context);
213             lose("%%primitive halt called; the party is over.\n");
214             
215         case trap_Error:
216         case trap_Cerror:
217             interrupt_internal_error(signal, siginfo, context, trap == trap_Cerror);
218             break;
219             
220         case trap_Breakpoint:
221             handle_breakpoint(signal, siginfo, context);
222             break;
223             
224         case trap_FunEndBreakpoint:
225             *os_context_pc_addr(context) = (int) handle_fun_end_breakpoint(signal, siginfo, context);
226             *os_context_npc_addr(context) = *os_context_pc_addr(context) + 4;
227             break;
228             
229         case trap_AfterBreakpoint:
230             *skipped_break_addr = trap_Breakpoint;
231             skipped_break_addr = NULL;
232             *(unsigned long *) os_context_pc_addr(context) = displaced_after_inst;
233             /* context->sigmask = orig_sigmask; */
234             os_flush_icache((os_vm_address_t) os_context_pc_addr(context), sizeof(unsigned long));
235             break;
236             
237         default:
238             interrupt_handle_now(signal, siginfo, context);
239             break;
240         }
241     }
242     else if ((siginfo->si_code) == ILL_ILLTRP
243 #ifdef LISP_FEATURE_LINUX
244              || (early_kernel && (siginfo->si_code) == 192)
245 #endif
246              ) {
247         if (pseudo_atomic_trap_p(context)) {
248             /* A trap instruction from a pseudo-atomic.  We just need
249                to fixup up alloc-tn to remove the interrupted flag,
250                skip over the trap instruction, and then handle the
251                pending interrupt(s). */
252             *os_context_register_addr(context, reg_ALLOC) &= ~7;
253             arch_skip_instruction(context);
254             interrupt_handle_pending(context);
255         }
256         else {
257             interrupt_internal_error(signal, siginfo, context, 0);
258         }
259     }
260     else {
261         interrupt_handle_now(signal, siginfo, context);
262     }
263 }
264
265 static void sigemt_handler(int signal, siginfo_t *siginfo, void *void_context)
266 {
267     unsigned long badinst;
268     boolean subtract, immed;
269     int rd, rs1, op1, rs2, op2, result;
270     os_context_t *context = arch_os_get_context(&void_context);
271 #ifdef LISP_FEATURE_LINUX
272     os_restore_fp_control(context);
273 #endif
274     
275     badinst = *(unsigned long *)os_context_pc_addr(context);
276     if ((badinst >> 30) != 2 || ((badinst >> 20) & 0x1f) != 0x11) {
277         /* It wasn't a tagged add.  Pass the signal into lisp. */
278         interrupt_handle_now(signal, siginfo, context);
279         return;
280     }
281     
282     fprintf(stderr, "SIGEMT trap handler with tagged op instruction!\n");
283     
284     /* Extract the parts of the inst. */
285     subtract = badinst & (1<<19);
286     rs1 = (badinst>>14) & 0x1f;
287     op1 = *os_context_register_addr(context, rs1);
288     
289     /* If the first arg is $ALLOC then it is really a signal-pending note */
290     /* for the pseudo-atomic noise. */
291     if (rs1 == reg_ALLOC) {
292         /* Perform the op anyway. */
293         op2 = badinst & 0x1fff;
294         if (op2 & (1<<12))
295             op2 |= -1<<13;
296         if (subtract)
297             result = op1 - op2;
298         else
299             result = op1 + op2;
300         *os_context_register_addr(context, reg_ALLOC) = result & ~7;
301         arch_skip_instruction(context);
302         interrupt_handle_pending(context);
303         return;
304     }
305     
306     if ((op1 & 3) != 0) {
307         /* The first arg wan't a fixnum. */
308         interrupt_internal_error(signal, siginfo, context, 0);
309         return;
310     }
311     
312     if (immed = badinst & (1<<13)) {
313         op2 = badinst & 0x1fff;
314         if (op2 & (1<<12))
315             op2 |= -1<<13;
316     }
317     else {
318         rs2 = badinst & 0x1f;
319         op2 = *os_context_register_addr(context, rs2);
320     }
321     
322     if ((op2 & 3) != 0) {
323         /* The second arg wan't a fixnum. */
324         interrupt_internal_error(signal, siginfo, context, 0);
325         return;
326     }
327     
328     rd = (badinst>>25) & 0x1f;
329     if (rd != 0) {
330         /* Don't bother computing the result unless we are going to use it. */
331         if (subtract)
332             result = (op1>>2) - (op2>>2);
333         else
334             result = (op1>>2) + (op2>>2);
335         
336         dynamic_space_free_pointer =
337             (lispobj *) *os_context_register_addr(context, reg_ALLOC);
338         
339         *os_context_register_addr(context, rd) = alloc_number(result);
340         
341         *os_context_register_addr(context, reg_ALLOC) =
342             (unsigned long) dynamic_space_free_pointer;
343     }
344     
345     arch_skip_instruction(context);
346 }
347
348 void arch_install_interrupt_handlers()
349 {
350     undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
351     undoably_install_low_level_interrupt_handler(SIGEMT, sigemt_handler);
352 }
353
354 \f
355 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
356
357 lispobj funcall0(lispobj function)
358 {
359     lispobj *args = current_control_stack_pointer;
360
361     return call_into_lisp(function, args, 0);
362 }
363
364 lispobj funcall1(lispobj function, lispobj arg0)
365 {
366     lispobj *args = current_control_stack_pointer;
367
368     current_control_stack_pointer += 1;
369     args[0] = arg0;
370
371     return call_into_lisp(function, args, 1);
372 }
373
374 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
375 {
376     lispobj *args = current_control_stack_pointer;
377
378     current_control_stack_pointer += 2;
379     args[0] = arg0;
380     args[1] = arg1;
381
382     return call_into_lisp(function, args, 2);
383 }
384
385 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
386 {
387     lispobj *args = current_control_stack_pointer;
388
389     current_control_stack_pointer += 3;
390     args[0] = arg0;
391     args[1] = arg1;
392     args[2] = arg2;
393
394     return call_into_lisp(function, args, 3);
395 }
396