0.7.7.9:
[sbcl.git] / src / runtime / mips-arch.c
1 /*
2
3  $Header$
4
5  This code was written as part of the CMU Common Lisp project at
6  Carnegie Mellon University, and has been placed in the public domain.
7
8 */
9
10 #include <stdio.h>
11
12 #include "runtime.h"
13 #include "arch.h"
14 #include "sbcl.h"
15 #include "globals.h"
16 #include "validate.h"
17 #include "os.h"
18 #include "lispregs.h"
19 #include "signal.h"
20 #include "alloc.h"
21 #include "interrupt.h"
22 #include "interr.h"
23 #include "breakpoint.h"
24 #include "monitor.h"
25
26 void arch_init()
27 {
28     return;
29 }
30
31 os_vm_address_t arch_get_bad_addr(int signam, siginfo_t *siginfo, os_context_t *context)
32 {
33     /* Classic CMUCL comment:
34
35        Finding the bad address on the mips is easy. */
36     return (os_vm_address_t) siginfo->si_addr;
37 }
38
39 unsigned long 
40 emulate_branch(os_context_t *context, unsigned long inst)
41 {
42     long opcode = inst >> 26;
43     long r1 = (inst >> 21) & 0x1f;
44     long r2 = (inst >> 16) & 0x1f;
45     long bdisp = (inst&(1<<15)) ? inst | (-1 << 16) : inst&0xffff;
46     long jdisp = (inst&(1<<25)) ? inst | (-1 << 26) : inst&0xffff;
47     long disp = 0;
48
49     switch(opcode) {
50     case 0x1: /* bltz, bgez, bltzal, bgezal */
51         switch((inst >> 16) & 0x1f) {
52         case 0x00: /* bltz */
53             if(*os_context_register_addr(context, r1) < 0)
54                 disp = bdisp;
55             break;
56         case 0x01: /* bgez */
57             if(*os_context_register_addr(context, r1) >= 0)
58                 disp = bdisp;
59             break;
60         case 0x10: /* bltzal */
61             if(*os_context_register_addr(context, r1) < 0)
62                 disp = bdisp;
63             *os_context_register_addr(context, 31) = *os_context_pc_addr(context) + 4;
64             break;
65         case 0x11: /* bgezal */
66             if(*os_context_register_addr(context, r1) >= 0)
67                 disp = bdisp;
68             *os_context_register_addr(context, 31) = *os_context_pc_addr(context) + 4;
69             break;
70         }
71         break;
72     case 0x4: /* beq */
73         if(*os_context_register_addr(context, r1)
74            == *os_context_register_addr(context, r2))
75             disp = bdisp;
76         break;
77     case 0x5: /* bne */
78         if(*os_context_register_addr(context, r1) 
79            != *os_context_register_addr(context, r2))
80             disp = bdisp;
81         break;
82     case 0x6: /* ble */
83         if(*os_context_register_addr(context, r1)
84            /* FIXME: One has to assume that the CMUCL gods of old have
85               got the sign issues right... but it might be worth
86               checking, someday */
87            <= *os_context_register_addr(context, r2))
88             disp = bdisp;
89         break;
90     case 0x7: /* bgtz */
91         if(*os_context_register_addr(context, r1)
92            >= *os_context_register_addr(context, r2))
93             disp = bdisp;
94         break;
95     case 0x2: /* j */
96         disp = jdisp;
97         break;
98     case 0x3: /* jal */
99         disp = jdisp;
100         *os_context_register_addr(context, 31) = *os_context_pc_addr(context) + 4;
101         break;
102     }
103     return (*os_context_pc_addr(context) + disp * 4);
104 }
105
106 void arch_skip_instruction(os_context_t *context)
107 {
108     /* Skip the offending instruction */
109     if (os_context_bd_cause(context))
110         *os_context_pc_addr(context) = 
111             emulate_branch(context, 
112                            *(unsigned long *) *os_context_pc_addr(context));
113     else
114         *os_context_pc_addr(context) += 4;
115
116     os_flush_icache((os_vm_address_t) *os_context_pc_addr(context), sizeof(unsigned long));
117 }
118
119 unsigned char *arch_internal_error_arguments(os_context_t *context)
120 {
121     if (os_context_bd_cause(context))
122         return (unsigned char *)(*os_context_pc_addr(context) + 8);
123     else
124         return (unsigned char *)(*os_context_pc_addr(context) + 4);
125 }
126
127 boolean arch_pseudo_atomic_atomic(os_context_t *context)
128 {
129     return *os_context_register_addr(context, reg_ALLOC) & 1;
130 }
131
132 #define PSEUDO_ATOMIC_INTERRUPTED_BIAS 0x7f000000
133
134 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
135 {
136     *os_context_register_addr(context, reg_NL4) |= 1<<31;
137 }
138
139 unsigned long arch_install_breakpoint(void *pc)
140 {
141     unsigned long *ptr = (unsigned long *)pc;
142     unsigned long result = *ptr;
143     *ptr = (trap_Breakpoint << 16) | 0xd;
144
145     os_flush_icache((os_vm_address_t)ptr, sizeof(unsigned long));
146
147     return result;
148 }
149
150 void arch_remove_breakpoint(void *pc, unsigned long orig_inst)
151 {
152     *(unsigned long *)pc = orig_inst;
153
154     os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
155 }
156
157 static unsigned long *skipped_break_addr, displaced_after_inst;
158 static sigset_t orig_sigmask;
159
160 void arch_do_displaced_inst(os_context_t *context,
161                             unsigned int orig_inst)
162 {
163     unsigned long *pc = (unsigned long *)*os_context_pc_addr(context);
164     unsigned long *break_pc, *next_pc;
165     unsigned long next_inst;
166     int opcode;
167
168     orig_sigmask = *os_context_sigmask_addr(context);
169     sigaddset_blockable(os_context_sigmask_addr(context));
170
171     /* Figure out where the breakpoint is, and what happens next. */
172     if (os_context_bd_cause(context)) {
173         break_pc = pc+1;
174         next_inst = *pc;
175     }
176     else {
177         break_pc = pc;
178         next_inst = orig_inst;
179     }
180
181     /* Put the original instruction back. */
182     *break_pc = orig_inst;
183     os_flush_icache((os_vm_address_t)break_pc, sizeof(unsigned long));
184     skipped_break_addr = break_pc;
185
186     /* Figure out where it goes. */
187     opcode = next_inst >> 26;
188     if (opcode == 1 || ((opcode & 0x3c) == 0x4) || ((next_inst & 0xf00e0000) == 0x80000000)) {
189         
190         next_pc = emulate_branch(context, next_inst);
191     }
192     else
193         next_pc = pc+1;
194
195     displaced_after_inst = *next_pc;
196     *next_pc = (trap_AfterBreakpoint << 16) | 0xd;
197     os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned long));
198 }
199
200 static void sigtrap_handler(int signal, siginfo_t *info, void *void_context)
201 {
202     os_context_t *context = arch_os_get_context(&void_context);
203     sigset_t *mask;
204     int code;
205     /* Don't disallow recursive breakpoint traps.  Otherwise, we can't */
206     /* use debugger breakpoints anywhere in here. */
207     mask = os_context_sigmask_addr(context);
208     sigsetmask(mask);
209     code = ((*(int *) (*os_context_pc_addr(context))) >> 16) & 0x1f;
210
211     switch (code) {
212     case trap_PendingInterrupt:
213         arch_skip_instruction(context);
214         interrupt_handle_pending(context);
215         break;
216         
217     case trap_Halt:
218         fake_foreign_function_call(context);
219         lose("%%primitive halt called; the party is over.\n");
220         
221     case trap_Error:
222     case trap_Cerror:
223         interrupt_internal_error(signal, info, context, code==trap_Cerror);
224         break;
225         
226     case trap_Breakpoint:
227         handle_breakpoint(signal, info, context);
228         break;
229         
230     case trap_FunEndBreakpoint:
231         *os_context_pc_addr(context) = (int)handle_fun_end_breakpoint(signal, info, context);
232         break;
233         
234     case trap_AfterBreakpoint:
235         *skipped_break_addr = (trap_Breakpoint << 16) | 0xd;
236         os_flush_icache((os_vm_address_t)skipped_break_addr,
237                         sizeof(unsigned long));
238         skipped_break_addr = NULL;
239         *(unsigned long *)(*os_context_pc_addr(context)) = displaced_after_inst;
240         os_flush_icache((os_vm_address_t) *os_context_pc_addr(context), sizeof(unsigned long));
241         *os_context_sigmask_addr(context) = orig_sigmask;
242         break;
243
244     case 0x10:
245         /* Clear the flag */
246         *os_context_register_addr(context, reg_NL4) &= 0x7fffffff;
247         arch_skip_instruction(context);
248         interrupt_handle_pending(context);
249         return;
250         
251     default:
252         interrupt_handle_now(signal, info, context);
253         break;
254     }
255 }
256
257 /* FIXME: We must have one of these somewhere. Also, export
258    N-FIXNUM-TAG-BITS from Lispland and use it rather than 2 here. */
259 #define FIXNUM_VALUE(lispobj) (((int)lispobj)>>2)
260
261 void sigfpe_handler(int signal, siginfo_t *info, void *void_context)
262 {
263     unsigned long bad_inst;
264     unsigned int op, rs, rt, rd, funct, dest;
265     int immed;
266     long result;
267     os_context_t *context = arch_os_get_context(&void_context);
268
269     if (os_context_bd_cause(context))
270         bad_inst = *(unsigned long *)(*os_context_pc_addr(context) + 4);
271     else
272         bad_inst = *(unsigned long *)(*os_context_pc_addr(context));
273
274     op = (bad_inst >> 26) & 0x3f;
275     rs = (bad_inst >> 21) & 0x1f;
276     rt = (bad_inst >> 16) & 0x1f;
277     rd = (bad_inst >> 11) & 0x1f;
278     funct = bad_inst & 0x3f;
279     immed = (((int)(bad_inst & 0xffff)) << 16) >> 16;
280
281     switch (op) {
282     case 0x0: /* SPECIAL */
283         switch (funct) {
284         case 0x20: /* ADD */
285             /* FIXME: Hopefully, this whole section can just go away,
286                with the rewrite of pseudo-atomic and the deletion of
287                overflow VOPs */
288             /* Check to see if this is really a pa_interrupted hit */
289             if (rs == reg_ALLOC && rt == reg_NL4) {
290                 *os_context_register_addr(context, reg_ALLOC)
291                     += (*os_context_register_addr(context, reg_NL4)
292                         - PSEUDO_ATOMIC_INTERRUPTED_BIAS);
293                 arch_skip_instruction(context);
294                 interrupt_handle_pending(context);
295                 return;
296             }
297             result = FIXNUM_VALUE(*os_context_register_addr(context, rs))
298                 + FIXNUM_VALUE(*os_context_register_addr(context, rt));
299             dest = rd;
300             break;
301             
302         case 0x22: /* SUB */
303             result = FIXNUM_VALUE(*os_context_register_addr(context, rs))
304                 - FIXNUM_VALUE(*os_context_register_addr(context, rt));
305             dest = rd;
306             break;
307             
308         default:
309             dest = 32;
310             break;
311         }
312         break;
313         
314     case 0x8: /* ADDI */
315         result = FIXNUM_VALUE(*os_context_register_addr(context,rs)) + (immed>>2);
316         dest = rt;
317         break;
318         
319     default:
320         dest = 32;
321         break;
322     }
323     
324     if (dest < 32) {
325         dynamic_space_free_pointer =
326             (lispobj *) *os_context_register_addr(context,reg_ALLOC);
327
328         *os_context_register_addr(context,dest) = alloc_number(result);
329
330         *os_context_register_addr(context, reg_ALLOC) =
331             (unsigned long) dynamic_space_free_pointer;
332         
333         arch_skip_instruction(context);
334         
335     }
336     else
337         interrupt_handle_now(signal, info, context);
338 }
339
340 void arch_install_interrupt_handlers()
341 {    
342     undoably_install_low_level_interrupt_handler(SIGTRAP,sigtrap_handler);
343     undoably_install_low_level_interrupt_handler(SIGFPE,sigfpe_handler);
344 }
345
346 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
347
348 lispobj funcall0(lispobj function)
349 {
350     lispobj *args = current_control_stack_pointer;
351
352     return call_into_lisp(function, args, 0);
353 }
354
355 lispobj funcall1(lispobj function, lispobj arg0)
356 {
357     lispobj *args = current_control_stack_pointer;
358
359     current_control_stack_pointer += 1;
360     args[0] = arg0;
361
362     return call_into_lisp(function, args, 1);
363 }
364
365 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
366 {
367     lispobj *args = current_control_stack_pointer;
368
369     current_control_stack_pointer += 2;
370     args[0] = arg0;
371     args[1] = arg1;
372
373     return call_into_lisp(function, args, 2);
374 }
375
376 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
377 {
378     lispobj *args = current_control_stack_pointer;
379
380     current_control_stack_pointer += 3;
381     args[0] = arg0;
382     args[1] = arg1;
383     args[2] = arg2;
384
385     return call_into_lisp(function, args, 3);
386 }
387