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