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