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