1.0.13.27: commentary on BUG #420
[sbcl.git] / src / runtime / alpha-arch.c
blob3f3dafe7baeb257ad43787169012e4efc24b26e3
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
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.
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 */
16 #include <stdio.h>
17 #include <string.h>
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"
32 extern char call_into_lisp_LRA[], call_into_lisp_end[];
34 extern size_t os_vm_page_size;
35 #define BREAKPOINT_INST 0x80
38 void
39 arch_init(void)
41 /* This must be called _after_ os_init(), so that we know what the
42 * page size is. */
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");
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);
55 os_flush_icache((os_vm_address_t)call_into_lisp_LRA_page,
56 os_vm_page_size);
57 return;
60 os_vm_address_t
61 arch_get_bad_addr (int sig, siginfo_t *code, os_context_t *context)
63 unsigned int badinst;
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));
71 if (((unsigned long)pc) & 3) {
72 return NULL; /* In what case would pc be unaligned?? */
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;
81 return context->uc_mcontext.sc_traparg_a0;
84 void
85 arch_skip_instruction(os_context_t *context)
87 /* This may be complete rubbish, as (at least for traps) pc points
88 * _after_ the instruction that caused us to be here anyway.
90 ((char*)*os_context_pc_addr(context)) +=4; }
92 unsigned char *
93 arch_internal_error_arguments(os_context_t *context)
95 return (unsigned char *)(*os_context_pc_addr(context)+4);
98 boolean
99 arch_pseudo_atomic_atomic(os_context_t *context)
101 /* FIXME: this foreign_function_call_active test is dubious at
102 * best. If a foreign call is made in a pseudo atomic section
103 * (?) or more likely a pseudo atomic section is in a foreign
104 * call then an interrupt is executed immediately. Maybe it
105 * has to do with C code not maintaining pseudo atomic
106 * properly. MG - 2005-08-10
108 * The foreign_function_call_active used to live at each call-site
109 * to arch_pseudo_atomic_atomic, but this seems clearer.
110 * --NS 2007-05-15 */
111 return (!foreign_function_call_active)
112 && ((*os_context_register_addr(context,reg_ALLOC)) & 1);
115 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
117 /* On coming out of an atomic section, we subtract 1 from
118 * reg_Alloc, then try to store something at that address. So,
119 * to signal that it was interrupted and a signal should be handled,
120 * we set bit 63 of reg_ALLOC here so that the end-of-atomic code
121 * will raise SIGSEGV (no ram mapped there). We catch the signal
122 * (see the appropriate *-os.c) and call interrupt_handle_pending()
123 * for the saved signal instead */
125 *os_context_register_addr(context,reg_ALLOC) |= (1L<<63);
128 void arch_clear_pseudo_atomic_interrupted(os_context_t *context)
130 *os_context_register_addr(context, reg_ALLOC) &= ~(1L<<63);
133 unsigned int arch_install_breakpoint(void *pc)
135 unsigned int *ptr = (unsigned int *)pc;
136 unsigned int result = *ptr;
137 *ptr = BREAKPOINT_INST;
139 os_flush_icache((os_vm_address_t)ptr, sizeof(unsigned int));
141 return result;
144 void arch_remove_breakpoint(void *pc, unsigned int orig_inst)
146 unsigned int *ptr = (unsigned int *)pc;
147 *ptr = orig_inst;
148 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned int));
151 static unsigned int *skipped_break_addr, displaced_after_inst,
152 after_breakpoint;
155 /* This returns a PC value. Lisp code is all in the 32-bit-addressable
156 * space, so we should be ok with an unsigned int. */
157 unsigned int
158 emulate_branch(os_context_t *context, unsigned int orig_inst)
160 int op = orig_inst >> 26;
161 int reg_a = (orig_inst >> 21) & 0x1f;
162 int reg_b = (orig_inst >> 16) & 0x1f;
163 int disp =
164 (orig_inst&(1<<20)) ?
165 orig_inst | (-1 << 21) :
166 orig_inst&0x1fffff;
167 int next_pc = *os_context_pc_addr(context);
168 int branch = 0; /* was NULL; */
170 switch(op) {
171 case 0x1a: /* jmp, jsr, jsr_coroutine, ret */
172 *os_context_register_addr(context,reg_a) =
173 *os_context_pc_addr(context);
174 *os_context_pc_addr(context) =
175 *os_context_register_addr(context,reg_b)& ~3;
176 break;
177 case 0x30: /* br */
178 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
179 branch = 1;
180 break;
181 case 0x31: /* fbeq */
182 if (*(os_context_float_register_addr(context,reg_a))==0) branch = 1;
183 break;
184 case 0x32: /* fblt */
185 if (*os_context_float_register_addr(context,reg_a)<0) branch = 1;
186 break;
187 case 0x33: /* fble */
188 if (*os_context_float_register_addr(context,reg_a)<=0) branch = 1;
189 break;
190 case 0x34: /* bsr */
191 *os_context_register_addr(context,reg_a)=*os_context_pc_addr(context);
192 branch = 1;
193 break;
194 case 0x35: /* fbne */
195 if (*os_context_register_addr(context,reg_a)!=0) branch = 1;
196 break;
197 case 0x36: /* fbge */
198 if (*os_context_float_register_addr(context,reg_a)>=0) branch = 1;
199 break;
200 case 0x37: /* fbgt */
201 if (*os_context_float_register_addr(context,reg_a)>0) branch = 1;
202 break;
203 case 0x38: /* blbc */
204 if ((*os_context_register_addr(context,reg_a)&1) == 0) branch = 1;
205 break;
206 case 0x39: /* beq */
207 if (*os_context_register_addr(context,reg_a)==0) branch = 1;
208 break;
209 case 0x3a: /* blt */
210 if (*os_context_register_addr(context,reg_a)<0) branch = 1;
211 break;
212 case 0x3b: /* ble */
213 if (*os_context_register_addr(context,reg_a)<=0) branch = 1;
214 break;
215 case 0x3c: /* blbs */
216 if ((*os_context_register_addr(context,reg_a)&1)!=0) branch = 1;
217 break;
218 case 0x3d: /* bne */
219 if (*os_context_register_addr(context,reg_a)!=0) branch = 1;
220 break;
221 case 0x3e: /* bge */
222 if (*os_context_register_addr(context,reg_a)>=0) branch = 1;
223 break;
224 case 0x3f: /* bgt */
225 if (*os_context_register_addr(context,reg_a)>0) branch = 1;
226 break;
228 if (branch)
229 next_pc += disp*4;
230 return next_pc;
233 static sigset_t orig_sigmask;
235 /* Perform the instruction that we overwrote with a breakpoint. As we
236 * don't have a single-step facility, this means we have to:
237 * - put the instruction back
238 * - put a second breakpoint at the following instruction,
239 * set after_breakpoint and continue execution.
241 * When the second breakpoint is hit (very shortly thereafter, we hope)
242 * sigtrap_handler gets called again, but follows the AfterBreakpoint
243 * arm, which
244 * - puts a bpt back in the first breakpoint place (running across a
245 * breakpoint shouldn't cause it to be uninstalled)
246 * - replaces the second bpt with the instruction it was meant to be
247 * - carries on
249 * Clear?
252 void arch_do_displaced_inst(os_context_t *context,unsigned int orig_inst)
254 /* Apparent off-by-one errors ahoy. If you consult the Alpha ARM,
255 * it will tell you that after a BPT, the saved PC is the address
256 * of the instruction _after_ the instruction that caused the trap.
258 * However, we decremented PC by 4 before calling the Lisp-level
259 * handler that calls this routine (see alpha-arch.c line 322 and
260 * friends) so when we get to this point PC is actually pointing
261 * at the BPT instruction itself. This is good, because this is
262 * where we want to restart execution when we do that */
264 unsigned int *pc=(unsigned int *)(*os_context_pc_addr(context));
265 unsigned int *next_pc;
266 int op = orig_inst >> 26;;
268 orig_sigmask = *os_context_sigmask_addr(context);
269 sigaddset_blockable(os_context_sigmask_addr(context));
271 /* Put the original instruction back. */
272 *pc = orig_inst;
273 os_flush_icache((os_vm_address_t)pc, sizeof(unsigned int));
274 skipped_break_addr = pc;
276 /* Figure out where we will end up after running the displaced
277 * instruction */
278 if (op == 0x1a || (op&0xf) == 0x30) /* a branch */
279 /* The cast to long is just to shut gcc up. */
280 next_pc = (unsigned int *)((long)emulate_branch(context,orig_inst));
281 else
282 next_pc = pc+1;
284 /* Set the after breakpoint. */
285 displaced_after_inst = *next_pc;
286 *next_pc = BREAKPOINT_INST;
287 after_breakpoint=1;
288 os_flush_icache((os_vm_address_t)next_pc, sizeof(unsigned int));
291 void
292 arch_handle_breakpoint(os_context_t *context)
294 *os_context_pc_addr(context) -=4;
295 handle_breakpoint(context);
298 void
299 arch_handle_fun_end_breakpoint(os_context_t *context)
301 *os_context_pc_addr(context) -=4;
302 *os_context_pc_addr(context) =
303 (int)handle_fun_end_breakpoint(context);
306 static void
307 sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context)
309 unsigned int code;
310 #ifdef LISP_FEATURE_LINUX
311 os_restore_fp_control(context);
312 #endif
314 /* this is different from how CMUCL does it. CMUCL used "call_pal
315 * PAL_gentrap", which doesn't do anything on Linux (unless NL0
316 * contains certain specific values). We use "bugchk" instead.
317 * It's (for our purposes) just the same as bpt but has a
318 * different opcode so we can test whether we're dealing with a
319 * breakpoint or a "system service" */
321 if ((*(unsigned int*)(*os_context_pc_addr(context)-4))==BREAKPOINT_INST) {
322 if (after_breakpoint) {
323 /* see comments above arch_do_displaced_inst. This is where
324 * we reinsert the breakpoint that we removed earlier */
326 *os_context_pc_addr(context) -=4;
327 *skipped_break_addr = BREAKPOINT_INST;
328 os_flush_icache((os_vm_address_t)skipped_break_addr,
329 sizeof(unsigned int));
330 skipped_break_addr = NULL;
331 *(unsigned int *)*os_context_pc_addr(context) =
332 displaced_after_inst;
333 os_flush_icache((os_vm_address_t)*os_context_pc_addr(context), sizeof(unsigned int));
334 *os_context_sigmask_addr(context)= orig_sigmask;
335 after_breakpoint=0; /* false */
336 return;
337 } else
338 code = trap_Breakpoint;
339 } else
340 /* a "system service" */
341 code=*((u32 *)(*os_context_pc_addr(context)));
342 handle_trap(context, code);
345 unsigned long
346 arch_get_fp_control()
348 return ieee_get_fp_control();
351 void
352 arch_set_fp_control(unsigned long fp)
354 ieee_set_fp_control(fp);
358 void arch_install_interrupt_handlers()
360 undoably_install_low_level_interrupt_handler(SIGTRAP, sigtrap_handler);