0.6.12.5:
[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
33 extern char call_into_lisp_LRA[], call_into_lisp_end[];
34 extern size_t os_vm_page_size;
35 #define BREAKPOINT_INST 0x80
36
37 void arch_init(void)
38 {
39     /* this must be called _after_ os_init, so we know what the page size is */
40     if(mmap((os_vm_address_t) call_into_lisp_LRA_page,os_vm_page_size,
41             OS_VM_PROT_ALL,MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,-1,0)
42        == (os_vm_address_t) -1)
43         perror("mmap");
44     
45     /* call_into_lisp_LRA is a collection of trampolines written in asm -
46      * see alpha-assem.S.  We copy it to call_into_lisp_LRA_page where
47      * VOPs and things can find it (I don't know why they can't find it 
48      * where it was to start with). */
49     bcopy(call_into_lisp_LRA,(void *)call_into_lisp_LRA_page,os_vm_page_size);
50
51     os_flush_icache((os_vm_address_t)call_into_lisp_LRA_page,
52                     os_vm_page_size);
53     return;
54 }
55
56 os_vm_address_t 
57 arch_get_bad_addr (int sig, siginfo_t *code, os_context_t *context)
58 {
59   unsigned int badinst;
60
61   /* instructions are 32 bit quantities */
62   unsigned int *pc ;
63   /*  fprintf(stderr,"arch_get_bad_addr %d %p %p\n",
64       sig, code, context); */
65   pc= (unsigned int *)(*os_context_pc_addr(context));
66
67   if(((unsigned long)pc) & 3) 
68       return NULL;              /* in what case would pc be unaligned? */
69
70   if( (pc < READ_ONLY_SPACE_START ||
71        pc >= READ_ONLY_SPACE_START+READ_ONLY_SPACE_SIZE) && 
72       (pc < current_dynamic_space ||
73        pc >= current_dynamic_space + DYNAMIC_SPACE_SIZE))
74     return NULL;
75
76   badinst = *pc;
77
78   if(((badinst>>27)!=0x16)      /* STL or STQ */
79      && ((badinst>>27)!=0x13))  /* STS or STT */
80     return NULL;                /* Otherwise forget about address */
81   
82   return (os_vm_address_t)
83     (*os_context_register_addr(context,((badinst>>16)&0x1f))
84      +(badinst&0xffff));
85 }
86
87 void arch_skip_instruction(os_context_t *context)
88 {
89     /* this may be complete rubbish, as (at least for traps) pc points
90      * _after_ the instruction that caused us to be here anyway
91      */
92     ((char*)*os_context_pc_addr(context)) +=4; }
93
94 unsigned char *arch_internal_error_arguments(os_context_t *context)
95 {
96   return (unsigned char *)(*os_context_pc_addr(context)+4);
97 }
98
99 boolean arch_pseudo_atomic_atomic(os_context_t *context)
100 {
101   return ((*os_context_register_addr(context,reg_ALLOC)) & 1);
102 }
103
104 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
105 {
106     /* On coming out of an atomic section, we subtract 1 from
107      * reg_Alloc, then try to store something at that address.  On
108      * OSF/1 we add 1 to reg_Alloc here so that the end-of-atomic code
109      * will raise SIGTRAP for "unaligned access".  Linux catches
110      * unaligned accesses in the kernel and fixes them up[1], so there
111      * we toggle bit 63 instead.  The resulting address is somewhere
112      * out in no-man's land, so we get SIGSEGV when we try to access
113      * it.  We catch whichever signal it is (see the appropriate
114      * *-os.c) and call interrupt_handle_pending() from it */
115
116     /* [1] This behaviour can be changed with osf_setsysinfo, but cmucl
117      * didn't use that */
118
119 #ifdef linux
120   *os_context_register_addr(context,reg_ALLOC) |=  (1L<<63);
121 #else
122   *os_context_register_addr(context,reg_ALLOC) |=  2;
123 #endif
124 }
125
126 /* XXX but is the caller of this storing all 64 bits? */
127 unsigned long arch_install_breakpoint(void *pc)
128 {
129     unsigned int *ptr = (unsigned int *)pc;
130     unsigned long result = (unsigned long) *ptr;
131     *ptr = BREAKPOINT_INST;
132     *(ptr+1)=trap_Breakpoint;
133     
134     os_flush_icache((os_vm_address_t)ptr, sizeof(unsigned long));
135     
136     return result;
137 }
138
139 void arch_remove_breakpoint(void *pc, unsigned long orig_inst)
140 {
141   /* was (unsigned int) but gcc complains.  Changed to mirror
142      install_breakpoint above */
143   unsigned long *ptr=(unsigned long *)pc;
144   *ptr = orig_inst;
145   os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
146 }
147
148 static unsigned int *skipped_break_addr, displaced_after_inst,
149      after_breakpoint;
150
151
152 /* Returns a PC value.  Lisp code is all in the 32-bit-addressable
153    space,so we should be ok with an unsigned int */
154
155 unsigned int
156 emulate_branch(os_context_t *context,unsigned long orig_inst)
157 {
158   int op = orig_inst >> 26;
159   int reg_a = (orig_inst >> 21) & 0x1f;
160   int reg_b = (orig_inst >> 16) & 0x1f;
161   int fn = orig_inst & 0xffff;
162   int disp = (orig_inst&(1<<20)) ? orig_inst | (-1 << 21) : orig_inst&0x1fffff;
163   int next_pc = *os_context_pc_addr(context);
164   int branch = 0; /* was NULL;         */
165
166   switch(op) {
167   case 0x1a: /* jmp, jsr, jsr_coroutine, ret */
168     *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
169     *os_context_pc_addr(context)=*os_context_register_addr(context,reg_b)& ~3;
170     break;
171   case 0x30: /* br */
172     *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
173     branch = 1;
174     break;
175   case 0x31: /* fbeq */
176     if(*(os_context_fpregister_addr(context,reg_a))==0) branch = 1;
177     break;
178   case 0x32: /* fblt */
179     if(*os_context_fpregister_addr(context,reg_a)<0) branch = 1;
180     break;
181   case 0x33: /* fble */
182     if(*os_context_fpregister_addr(context,reg_a)<=0) branch = 1;
183     break;
184   case 0x34: /* bsr */
185     *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
186     branch = 1;
187     break;
188   case 0x35: /* fbne */
189     if(*os_context_register_addr(context,reg_a)!=0) branch = 1;
190     break;
191   case 0x36: /* fbge */
192     if(*os_context_fpregister_addr(context,reg_a)>=0) branch = 1;
193     break;
194   case 0x37: /* fbgt */
195     if(*os_context_fpregister_addr(context,reg_a)>0) branch = 1;
196     break;
197   case 0x38: /* blbc */
198     if((*os_context_register_addr(context,reg_a)&1) == 0) branch = 1;
199     break;
200   case 0x39: /* beq */
201     if(*os_context_register_addr(context,reg_a)==0) branch = 1;
202     break;
203   case 0x3a: /* blt */
204     if(*os_context_register_addr(context,reg_a)<0) branch = 1;
205     break;
206   case 0x3b: /* ble */
207     if(*os_context_register_addr(context,reg_a)<=0) branch = 1;
208     break;
209   case 0x3c: /* blbs */
210     if((*os_context_register_addr(context,reg_a)&1)!=0) branch = 1;
211     break;
212   case 0x3d: /* bne */
213     if(*os_context_register_addr(context,reg_a)!=0) branch = 1;
214     break;
215   case 0x3e: /* bge */
216     if(*os_context_register_addr(context,reg_a)>=0) branch = 1;
217     break;
218   case 0x3f: /* bgt */
219     if(*os_context_register_addr(context,reg_a)>0) branch = 1;
220     break;
221   }
222   if(branch) next_pc += disp*4;
223   return next_pc;
224 }
225
226 static sigset_t orig_sigmask;
227
228 void arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
229 {
230   unsigned int *pc=(unsigned int *)(*os_context_pc_addr(context));
231   unsigned int *next_pc;
232   unsigned int next_inst;
233   int op = orig_inst >> 26;;
234   fprintf(stderr,"arch_do_displaced_inst depends on sigreturn, which is not implemented and will\nalways fail\n");
235   orig_sigmask = *os_context_sigmask_addr(context);
236   sigaddset_blockable(os_context_sigmask_addr(context));
237
238   /* Figure out where the displaced inst is going */
239   if(op == 0x1a || (op&0xf) == 0x30) /* branch...ugh */
240     /* the cast to long is just to shut gcc up */
241     next_pc = (unsigned int *)((long)emulate_branch(context,orig_inst));
242   else
243     next_pc = pc+1;
244
245   /* Put the original instruction back. */
246   *pc = orig_inst;
247   os_flush_icache((os_vm_address_t)pc, sizeof(unsigned long));
248   skipped_break_addr = pc;
249
250   /* set the after breakpoint */
251   displaced_after_inst = *next_pc;
252   *next_pc = BREAKPOINT_INST;
253   after_breakpoint=1;
254   os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned long));
255
256   ldb_monitor("sigreturn is not implemented and just failed");
257   sigreturn(context);
258 }
259
260 #define AfterBreakpoint 100
261
262 static void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
263     fake_foreign_function_call(context);
264     ldb_monitor();
265 }
266
267 static void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
268 {
269     /* Don't disallow recursive breakpoint traps.  Otherwise, we can't */
270     /* use debugger breakpoints anywhere in here. */
271
272     sigset_t *mask=(os_context_sigmask_addr(context));
273     unsigned int code;
274     fprintf(stderr,"sigtrap_handler:signal %d context=%p ",signal,context);
275     sigsetmask(mask); 
276
277     /* this is different from how CMUCL does it.  CMUCL used
278      * "call_pal PAL_gentrap", which doesn't do anything on Linux
279      * so screwed up our offsets in odd ways.  We use "bpt" instead
280      */
281
282     /* probably we should
283     assert(*(unsigned int*)(*os_context_pc_addr(context)-4) == BREAKPOINT_INST)
284     but I've not decided a good way to handle it if it turns out not to be
285     */
286     code=*((u32 *)(*os_context_pc_addr(context)));
287     fprintf(stderr,"pc=%lx  code=%d, inst=%x\n",
288             *os_context_pc_addr(context),  code,
289             *(unsigned int*)(*os_context_pc_addr(context)-4));
290     switch (code) {
291       case trap_PendingInterrupt:
292         arch_skip_instruction(context);
293         interrupt_handle_pending(context);
294         break;
295
296       case trap_Halt:
297         fake_foreign_function_call(context);
298         lose("%%primitive halt called; the party is over.\n");
299
300       case trap_Error:
301       case trap_Cerror:
302         interrupt_internal_error(signal, siginfo, context, code==trap_Cerror);
303         break;
304
305       case trap_Breakpoint:
306         *os_context_pc_addr(context) -=4;
307         handle_breakpoint(signal, siginfo, context);
308         break;
309
310       case trap_FunctionEndBreakpoint:
311         *os_context_pc_addr(context) -=4;
312         *os_context_pc_addr(context) = (int)handle_function_end_breakpoint(signal, siginfo, context);
313         break;
314
315       case AfterBreakpoint:
316         *os_context_pc_addr(context) -=4;
317         *skipped_break_addr = BREAKPOINT_INST;
318         os_flush_icache((os_vm_address_t)skipped_break_addr,
319                         sizeof(unsigned long));
320         skipped_break_addr = NULL;
321         *(unsigned int *)*os_context_pc_addr(context) = displaced_after_inst;
322         os_flush_icache((os_vm_address_t)*os_context_pc_addr(context), sizeof(unsigned long));
323         *os_context_sigmask_addr(context)= orig_sigmask;
324         after_breakpoint=0; /* NULL; */
325         break;
326
327       default:
328         interrupt_handle_now(signal, siginfo, context);
329         break;
330     }
331 }
332
333 #define FIXNUM_VALUE(lispobj) (((int)lispobj)>>2)
334
335 static void sigfpe_handler(int signal, int code, os_context_t *context)
336 {
337 }
338
339 void arch_install_interrupt_handlers()
340 {
341     interrupt_install_low_level_handler(SIGILL,sigill_handler);
342     interrupt_install_low_level_handler(SIGTRAP,sigtrap_handler);
343     interrupt_install_low_level_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
388
389 /* This is apparently called by emulate_branch, but isn't defined.  So */
390 /* just do nothing and hope it works... */
391
392 void cacheflush(void)
393 {
394     /* hoping probably isn't _actually_ enough.  we should call_pal imb,
395        according to the arch ref manual
396     */
397 }