0.9.2.42:
[sbcl.git] / src / runtime / alpha-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 /* Note that although superficially it appears that we use
13  * os_context_t like we ought to, we actually just assume its a
14  * ucontext in places.  Naughty */
15
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "sbcl.h"
20 #include "runtime.h"
21 #include "globals.h"
22 #include "validate.h"
23 #include "os.h"
24 #include "arch.h"
25 #include "lispregs.h"
26 #include "signal.h"
27 #include "alloc.h"
28 #include "interrupt.h"
29 #include "interr.h"
30 #include "breakpoint.h"
31 #include "monitor.h"
32
33 extern char call_into_lisp_LRA[], call_into_lisp_end[];
34
35 extern size_t os_vm_page_size;
36 #define BREAKPOINT_INST 0x80
37
38
39 void
40 arch_init(void)
41 {
42     /* This must be called _after_ os_init(), so that we know what the
43      * page size is. */
44
45     if (mmap((os_vm_address_t) call_into_lisp_LRA_page,os_vm_page_size,
46              OS_VM_PROT_ALL,MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,-1,0)
47         == (os_vm_address_t) -1)
48         perror("mmap");
49
50     /* call_into_lisp_LRA is a collection of trampolines written in asm -
51      * see alpha-assem.S.  We copy it to call_into_lisp_LRA_page where
52      * VOPs and things can find it. (I don't know why they can't find it
53      * where it was to start with.) */
54     bcopy(call_into_lisp_LRA,(void *)call_into_lisp_LRA_page,os_vm_page_size);
55
56     os_flush_icache((os_vm_address_t)call_into_lisp_LRA_page,
57                     os_vm_page_size);
58     return;
59 }
60
61 os_vm_address_t
62 arch_get_bad_addr (int sig, siginfo_t *code, os_context_t *context)
63 {
64     unsigned int badinst;
65
66     /* Instructions are 32 bit quantities. */
67     unsigned int *pc ;
68     /*  fprintf(stderr,"arch_get_bad_addr %d %p %p\n",
69         sig, code, context); */
70     pc= (unsigned int *)(*os_context_pc_addr(context));
71
72     if (((unsigned long)pc) & 3) {
73         return NULL;            /* In what case would pc be unaligned?? */
74     }
75
76     if ( (pc < READ_ONLY_SPACE_START ||
77           pc >= READ_ONLY_SPACE_START+READ_ONLY_SPACE_SIZE) &&
78          (pc < current_dynamic_space ||
79           pc >= current_dynamic_space + DYNAMIC_SPACE_SIZE))
80         return NULL;
81
82     return context->uc_mcontext.sc_traparg_a0;
83 }
84
85 void
86 arch_skip_instruction(os_context_t *context)
87 {
88     /* This may be complete rubbish, as (at least for traps) pc points
89      * _after_ the instruction that caused us to be here anyway.
90      */
91     ((char*)*os_context_pc_addr(context)) +=4; }
92
93 unsigned char *
94 arch_internal_error_arguments(os_context_t *context)
95 {
96     return (unsigned char *)(*os_context_pc_addr(context)+4);
97 }
98
99 boolean
100 arch_pseudo_atomic_atomic(os_context_t *context)
101 {
102     return ((*os_context_register_addr(context,reg_ALLOC)) & 1);
103 }
104
105 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
106 {
107     /* On coming out of an atomic section, we subtract 1 from
108      * reg_Alloc, then try to store something at that address.  So,
109      * to signal that it was interrupted and a signal should be handled,
110      * we set bit 63 of reg_ALLOC here so that the end-of-atomic code
111      * will raise SIGSEGV (no ram mapped there).  We catch the signal
112      * (see the appropriate *-os.c) and call interrupt_handle_pending()
113      * for the saved signal instead */
114
115     *os_context_register_addr(context,reg_ALLOC) |=  (1L<<63);
116 }
117
118 unsigned long arch_install_breakpoint(void *pc)
119 {
120     unsigned int *ptr = (unsigned int *)pc;
121     unsigned long result = (unsigned long) *ptr;
122     *ptr = BREAKPOINT_INST;
123
124     os_flush_icache((os_vm_address_t)ptr, sizeof(unsigned long));
125
126     return result;
127 }
128
129 void arch_remove_breakpoint(void *pc, unsigned long orig_inst)
130 {
131     /* was (unsigned int) but gcc complains.  Changed to mirror
132      * install_breakpoint() above */
133     unsigned long *ptr=(unsigned long *)pc;
134     *ptr = orig_inst;
135     os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
136 }
137
138 static unsigned int *skipped_break_addr, displaced_after_inst,
139      after_breakpoint;
140
141
142 /* This returns a PC value.  Lisp code is all in the 32-bit-addressable
143  * space, so we should be ok with an unsigned int. */
144 unsigned int
145 emulate_branch(os_context_t *context,unsigned long orig_inst)
146 {
147     int op = orig_inst >> 26;
148     int reg_a = (orig_inst >> 21) & 0x1f;
149     int reg_b = (orig_inst >> 16) & 0x1f;
150     int disp =
151         (orig_inst&(1<<20)) ?
152         orig_inst | (-1 << 21) :
153         orig_inst&0x1fffff;
154     int next_pc = *os_context_pc_addr(context);
155     int branch = 0; /* was NULL;               */
156
157     switch(op) {
158     case 0x1a: /* jmp, jsr, jsr_coroutine, ret */
159         *os_context_register_addr(context,reg_a) =
160             *os_context_pc_addr(context);
161         *os_context_pc_addr(context) =
162             *os_context_register_addr(context,reg_b)& ~3;
163         break;
164     case 0x30: /* br */
165         *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
166         branch = 1;
167         break;
168     case 0x31: /* fbeq */
169         if (*(os_context_float_register_addr(context,reg_a))==0) branch = 1;
170         break;
171     case 0x32: /* fblt */
172         if (*os_context_float_register_addr(context,reg_a)<0) branch = 1;
173         break;
174     case 0x33: /* fble */
175         if (*os_context_float_register_addr(context,reg_a)<=0) branch = 1;
176         break;
177     case 0x34: /* bsr */
178         *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
179         branch = 1;
180         break;
181     case 0x35: /* fbne */
182         if (*os_context_register_addr(context,reg_a)!=0) branch = 1;
183         break;
184     case 0x36: /* fbge */
185         if (*os_context_float_register_addr(context,reg_a)>=0) branch = 1;
186         break;
187     case 0x37: /* fbgt */
188         if (*os_context_float_register_addr(context,reg_a)>0) branch = 1;
189         break;
190     case 0x38: /* blbc */
191         if ((*os_context_register_addr(context,reg_a)&1) == 0) branch = 1;
192         break;
193     case 0x39: /* beq */
194         if (*os_context_register_addr(context,reg_a)==0) branch = 1;
195         break;
196     case 0x3a: /* blt */
197         if (*os_context_register_addr(context,reg_a)<0) branch = 1;
198         break;
199     case 0x3b: /* ble */
200         if (*os_context_register_addr(context,reg_a)<=0) branch = 1;
201         break;
202     case 0x3c: /* blbs */
203         if ((*os_context_register_addr(context,reg_a)&1)!=0) branch = 1;
204         break;
205     case 0x3d: /* bne */
206         if (*os_context_register_addr(context,reg_a)!=0) branch = 1;
207         break;
208     case 0x3e: /* bge */
209         if (*os_context_register_addr(context,reg_a)>=0) branch = 1;
210         break;
211     case 0x3f: /* bgt */
212         if (*os_context_register_addr(context,reg_a)>0) branch = 1;
213         break;
214     }
215     if (branch)
216         next_pc += disp*4;
217     return next_pc;
218 }
219
220 static sigset_t orig_sigmask;
221
222 /* Perform the instruction that we overwrote with a breakpoint.  As we
223  * don't have a single-step facility, this means we have to:
224  * - put the instruction back
225  * - put a second breakpoint at the following instruction,
226  *   set after_breakpoint and continue execution.
227  *
228  * When the second breakpoint is hit (very shortly thereafter, we hope)
229  * sigtrap_handler gets called again, but follows the AfterBreakpoint
230  * arm, which
231  * - puts a bpt back in the first breakpoint place (running across a
232  *   breakpoint shouldn't cause it to be uninstalled)
233  * - replaces the second bpt with the instruction it was meant to be
234  * - carries on
235  *
236  * Clear?
237  */
238
239 void arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
240 {
241     /* Apparent off-by-one errors ahoy.  If you consult the Alpha ARM,
242      * it will tell you that after a BPT, the saved PC is the address
243      * of the instruction _after_ the instruction that caused the trap.
244      *
245      * However, we decremented PC by 4 before calling the Lisp-level
246      * handler that calls this routine (see alpha-arch.c line 322 and
247      * friends) so when we get to this point PC is actually pointing
248      * at the BPT instruction itself.  This is good, because this is
249      * where we want to restart execution when we do that */
250
251     unsigned int *pc=(unsigned int *)(*os_context_pc_addr(context));
252     unsigned int *next_pc;
253     int op = orig_inst >> 26;;
254
255     orig_sigmask = *os_context_sigmask_addr(context);
256     sigaddset_blockable(os_context_sigmask_addr(context));
257
258     /* Put the original instruction back. */
259     *pc = orig_inst;
260     os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
261     skipped_break_addr = pc;
262
263     /* Figure out where we will end up after running the displaced
264      * instruction */
265     if (op == 0x1a || (op&0xf) == 0x30) /* a branch */
266         /* The cast to long is just to shut gcc up. */
267         next_pc = (unsigned int *)((long)emulate_branch(context,orig_inst));
268     else
269         next_pc = pc+1;
270
271     /* Set the after breakpoint. */
272     displaced_after_inst = *next_pc;
273     *next_pc = BREAKPOINT_INST;
274     after_breakpoint=1;
275     os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned long));
276 }
277
278 static void
279 sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
280 {
281     unsigned int code;
282 #ifdef LISP_FEATURE_LINUX
283     os_restore_fp_control(context);
284 #endif
285
286     /* this is different from how CMUCL does it.  CMUCL used "call_pal
287      * PAL_gentrap", which doesn't do anything on Linux (unless NL0
288      * contains certain specific values).  We use "bugchk" instead.
289      * It's (for our purposes) just the same as bpt but has a
290      * different opcode so we can test whether we're dealing with a
291      * breakpoint or a "system service" */
292
293     if ((*(unsigned int*)(*os_context_pc_addr(context)-4))==BREAKPOINT_INST) {
294         if (after_breakpoint) {
295             /* see comments above arch_do_displaced_inst.  This is where
296              * we reinsert the breakpoint that we removed earlier */
297
298             *os_context_pc_addr(context) -=4;
299             *skipped_break_addr = BREAKPOINT_INST;
300             os_flush_icache((os_vm_address_t)skipped_break_addr,
301                             sizeof(unsigned long));
302             skipped_break_addr = NULL;
303             *(unsigned int *)*os_context_pc_addr(context) =
304                 displaced_after_inst;
305             os_flush_icache((os_vm_address_t)*os_context_pc_addr(context), sizeof(unsigned long));
306             *os_context_sigmask_addr(context)= orig_sigmask;
307             after_breakpoint=0; /* false */
308             return;
309         } else
310             code = trap_Breakpoint;
311     } else
312         /* a "system service" */
313     code=*((u32 *)(*os_context_pc_addr(context)));
314
315     switch (code) {
316       case trap_PendingInterrupt:
317         arch_skip_instruction(context);
318         interrupt_handle_pending(context);
319         break;
320
321       case trap_Halt:
322         fake_foreign_function_call(context);
323         lose("%%primitive halt called; the party is over.\n");
324
325       case trap_Error:
326       case trap_Cerror:
327         interrupt_internal_error(signal, siginfo, context, code==trap_Cerror);
328         break;
329
330     case trap_Breakpoint:        /* call lisp-level handler */
331         *os_context_pc_addr(context) -=4;
332         handle_breakpoint(signal, siginfo, context);
333         break;
334
335       case trap_FunEndBreakpoint:
336         *os_context_pc_addr(context) -=4;
337         *os_context_pc_addr(context) =
338             (int)handle_fun_end_breakpoint(signal, siginfo, context);
339         break;
340
341       default:
342         fprintf(stderr, "unidentified breakpoint/trap %d\n",code);
343         interrupt_handle_now(signal, siginfo, context);
344         break;
345     }
346 }
347
348 unsigned long
349 arch_get_fp_control()
350 {
351     return ieee_get_fp_control();
352 }
353
354 void
355 arch_set_fp_control(unsigned long fp)
356 {
357     ieee_set_fp_control(fp);
358 }
359
360
361 void arch_install_interrupt_handlers()
362 {
363     undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);
364 }
365
366 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
367
368 lispobj funcall0(lispobj function)
369 {
370     lispobj *args = current_control_stack_pointer;
371
372     return call_into_lisp(function, args, 0);
373 }
374
375 lispobj funcall1(lispobj function, lispobj arg0)
376 {
377     lispobj *args = current_control_stack_pointer;
378
379     current_control_stack_pointer += 1;
380     args[0] = arg0;
381
382     return call_into_lisp(function, args, 1);
383 }
384
385 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
386 {
387     lispobj *args = current_control_stack_pointer;
388
389     current_control_stack_pointer += 2;
390     args[0] = arg0;
391     args[1] = arg1;
392
393     return call_into_lisp(function, args, 2);
394 }
395
396 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
397 {
398     lispobj *args = current_control_stack_pointer;
399
400     current_control_stack_pointer += 3;
401     args[0] = arg0;
402     args[1] = arg1;
403     args[2] = arg2;
404
405     return call_into_lisp(function, args, 3);
406 }
407