2 * This software is part of the SBCL system. See the README file for
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.
22 #include "interrupt.h"
24 #include "breakpoint.h"
27 #ifdef LISP_FEATURE_LINUX
28 extern int linux_sparc_siginfo_bug
;
36 os_vm_address_t
arch_get_bad_addr(int sig
, siginfo_t
*code
, os_context_t
*context
)
39 return (os_vm_address_t
)code
->si_addr
;
40 #else /* Old way, almost certainly predates sigaction(2)-style handlers */
45 pc
= (unsigned int *)(*os_context_pc_addr(context
));
47 /* On the sparc, we have to decode the instruction. */
49 /* Make sure it's not the pc thats bogus, and that it was lisp code */
50 /* that caused the fault. */
51 if ((unsigned long) pc
& 3) {
55 if ((pc
< READ_ONLY_SPACE_START
||
56 pc
>= READ_ONLY_SPACE_START
+READ_ONLY_SPACE_SIZE
) &&
57 (pc
< current_dynamic_space
||
58 pc
>= current_dynamic_space
+ dynamic_space_size
)) {
64 if ((badinst
>> 30) != 3)
65 /* All load/store instructions have op = 11 (binary) */
68 rs1
= (badinst
>>14)&0x1f;
70 if (badinst
& (1<<13)) {
71 /* r[rs1] + simm(13) */
72 int simm13
= badinst
& 0x1fff;
77 return (os_vm_address_t
)
78 (*os_context_register_addr(context
, rs1
)+simm13
);
82 int rs2
= badinst
& 0x1f;
84 return (os_vm_address_t
)
85 (*os_context_register_addr(context
, rs1
) +
86 *os_context_register_addr(context
, rs2
));
91 void arch_skip_instruction(os_context_t
*context
)
93 *os_context_pc_addr(context
) = *os_context_npc_addr(context
);
94 /* Note that we're doing integer arithmetic here, not pointer. So
95 * the value that the return value of os_context_npc_addr() points
96 * to will be incremented by 4, not 16.
98 *os_context_npc_addr(context
) += 4;
101 unsigned char *arch_internal_error_arguments(os_context_t
*context
)
103 return (unsigned char *)(*os_context_pc_addr(context
) + 4);
106 boolean
arch_pseudo_atomic_atomic(os_context_t
*context
)
108 /* FIXME: this foreign_function_call_active test is dubious at
109 * best. If a foreign call is made in a pseudo atomic section
110 * (?) or more likely a pseudo atomic section is in a foreign
111 * call then an interrupt is executed immediately. Maybe it
112 * has to do with C code not maintaining pseudo atomic
113 * properly. MG - 2005-08-10
115 * The foreign_function_call_active used to live at each call-site
116 * to arch_pseudo_atomic_atomic, but this seems clearer.
118 return (!foreign_function_call_active
)
119 && ((*os_context_register_addr(context
,reg_ALLOC
)) & 4);
122 void arch_set_pseudo_atomic_interrupted(os_context_t
*context
)
124 *os_context_register_addr(context
,reg_ALLOC
) |= 1;
127 void arch_clear_pseudo_atomic_interrupted(os_context_t
*context
)
129 *os_context_register_addr(context
,reg_ALLOC
) &= ~1;
132 unsigned int arch_install_breakpoint(void *pc
)
134 unsigned int *ptr
= (unsigned int *)pc
;
135 unsigned int result
= *ptr
;
136 *ptr
= trap_Breakpoint
;
138 os_flush_icache((os_vm_address_t
) pc
, sizeof(unsigned int));
143 void arch_remove_breakpoint(void *pc
, unsigned int orig_inst
)
145 *(unsigned int *)pc
= orig_inst
;
146 os_flush_icache((os_vm_address_t
) pc
, sizeof(unsigned int));
150 * Perform the instruction that we overwrote with a breakpoint. As we
151 * don't have a single-step facility, this means we have to:
152 * - put the instruction back
153 * - put a second breakpoint at the following instruction,
154 * set after_breakpoint and continue execution.
156 * When the second breakpoint is hit (very shortly thereafter, we hope)
157 * sigtrap_handler gets called again, but follows the AfterBreakpoint
159 * - puts a bpt back in the first breakpoint place (running across a
160 * breakpoint shouldn't cause it to be uninstalled)
161 * - replaces the second bpt with the instruction it was meant to be
166 static unsigned int *skipped_break_addr
, displaced_after_inst
;
167 static sigset_t orig_sigmask
;
169 void arch_do_displaced_inst(os_context_t
*context
, unsigned int orig_inst
)
171 unsigned int *pc
= (unsigned int *)(*os_context_pc_addr(context
));
172 unsigned int *npc
= (unsigned int *)(*os_context_npc_addr(context
));
174 /* orig_sigmask = context->sigmask;
175 sigemptyset(&context->sigmask); */
177 /* FILLBLOCKSET(&context->uc_sigmask);*/
180 os_flush_icache((os_vm_address_t
) pc
, sizeof(unsigned int));
181 skipped_break_addr
= pc
;
182 displaced_after_inst
= *npc
;
183 *npc
= trap_AfterBreakpoint
;
184 os_flush_icache((os_vm_address_t
) npc
, sizeof(unsigned int));
188 static int pseudo_atomic_trap_p(os_context_t
*context
)
191 unsigned int badinst
;
195 pc
= (unsigned int*) *os_context_pc_addr(context
);
199 /* Check to see if the current instruction is a pseudo-atomic-trap */
200 if (((badinst
>> 30) == 2) && (((badinst
>> 19) & 0x3f) == 0x3a)
201 && (((badinst
>> 13) & 1) == 1) && ((badinst
& 0x7f) == PSEUDO_ATOMIC_TRAP
))
203 unsigned int previnst
;
206 * Check to see if the previous instruction was an andcc alloc-tn,
207 * 3, zero-tn instruction.
209 if (((previnst
>> 30) == 2) && (((previnst
>> 19) & 0x3f) == 0x11)
210 && (((previnst
>> 14) & 0x1f) == reg_ALLOC
)
211 && (((previnst
>> 25) & 0x1f) == reg_ZERO
)
212 && (((previnst
>> 13) & 1) == 1)
213 && ((previnst
& 0x1fff) == 3))
219 fprintf(stderr
, "Oops! Got a PSEUDO-ATOMIC-TRAP without a preceeding andcc!\n");
226 arch_handle_breakpoint(os_context_t
*context
)
228 handle_breakpoint(context
);
232 arch_handle_fun_end_breakpoint(os_context_t
*context
)
234 *os_context_pc_addr(context
) = (int) handle_fun_end_breakpoint(context
);
235 *os_context_npc_addr(context
) = *os_context_pc_addr(context
) + 4;
239 arch_handle_after_breakpoint(os_context_t
*context
)
241 *skipped_break_addr
= trap_Breakpoint
;
242 os_flush_icache(skipped_break_addr
, sizeof(unsigned int));
243 skipped_break_addr
= NULL
;
244 *(unsigned long *) os_context_pc_addr(context
) = displaced_after_inst
;
245 /* context->sigmask = orig_sigmask; */
246 os_flush_icache((os_vm_address_t
) os_context_pc_addr(context
), sizeof(unsigned int));
250 arch_handle_single_step_trap(os_context_t
*context
, int trap
)
252 unsigned int code
= *((u32
*)(*os_context_pc_addr(context
)));
253 int register_offset
= code
>> 5 & 0x1f;
254 handle_single_step_trap(context
, trap
, register_offset
);
255 arch_skip_instruction(context
);
258 #ifdef LISP_FEATURE_GENCGC
260 arch_handle_allocation_trap(os_context_t
*context
)
263 unsigned int or_inst
;
268 boolean were_in_lisp
;
271 if (foreign_function_call_active
)
272 lose("Allocation trap inside foreign code.");
274 pc
= (unsigned int*) *os_context_pc_addr(context
);
278 * The instruction before this trap instruction had better be an OR
281 if (!(((or_inst
>> 30) == 2) && (((or_inst
>> 19) & 0x1f) == 2)))
282 lose("Allocation trap not preceded by an OR instruction: 0x%08x",
286 * An OR instruction. RS1 is the register we want to allocate to.
287 * RS2 (or an immediate) is the size.
289 rs1
= (or_inst
>> 14) & 0x1f;
290 immed
= (or_inst
>> 13) & 1;
293 size
= or_inst
& 0x1fff;
295 size
= or_inst
& 0x1f;
296 size
= *os_context_register_addr(context
, size
);
299 fake_foreign_function_call(context
);
302 * Allocate some memory, store the memory address in rs1.
305 struct interrupt_data
*data
=
306 arch_os_get_current_thread()->interrupt_data
;
307 data
->allocation_trap_context
= context
;
308 memory
= alloc(size
);
309 data
->allocation_trap_context
= 0;
311 *os_context_register_addr(context
, rs1
) = memory
;
313 undo_fake_foreign_function_call(context
);
317 static void sigill_handler(int signal
, siginfo_t
*siginfo
,
318 os_context_t
*context
)
320 if ((siginfo
->si_code
) == ILL_ILLOPC
321 #ifdef LISP_FEATURE_LINUX
322 || (linux_sparc_siginfo_bug
&& (siginfo
->si_code
== 2))
327 unsigned int* pc
= (unsigned int*) siginfo
->si_addr
;
331 handle_trap(context
,trap
);
333 else if ((siginfo
->si_code
) == ILL_ILLTRP
334 #ifdef LISP_FEATURE_LINUX
335 || (linux_sparc_siginfo_bug
&& (siginfo
->si_code
) == 192)
338 if (pseudo_atomic_trap_p(context
)) {
339 /* A trap instruction from a pseudo-atomic. We just need
340 to fixup up alloc-tn to remove the interrupted flag,
341 skip over the trap instruction, and then handle the
342 pending interrupt(s). */
343 arch_clear_pseudo_atomic_interrupted(context
);
344 arch_skip_instruction(context
);
345 interrupt_handle_pending(context
);
348 interrupt_internal_error(context
, 0);
352 interrupt_handle_now(signal
, siginfo
, context
);
356 void arch_install_interrupt_handlers()
358 undoably_install_low_level_interrupt_handler(SIGILL
, sigill_handler
);
362 #ifdef LISP_FEATURE_LINKAGE_TABLE
364 /* This a naive port from CMUCL/sparc, which was mostly stolen from the
365 * CMUCL/x86 version, with adjustments for sparc
367 * Linkage entry size is 16, because we need at least 3 instruction to
370 * sethi %hi(addr), %g4
371 * jmpl [%g4 + %lo(addr)], %g5
374 * The Sparc V9 ABI seems to use 8 words for its jump tables. Maybe
375 * we should do the same?
379 * Define the registers to use in the linkage jump table. Can be the
380 * same. Some care must be exercised when choosing these. It has to be
381 * a register that is not otherwise being used. reg_L0 is a good
382 * choice. call_into_c trashes reg_L0 without preserving it, so we can
383 * trash it in the linkage jump table.
385 #define LINKAGE_TEMP_REG reg_L0
386 #define LINKAGE_ADDR_REG reg_L0
389 * Insert the necessary jump instructions at the given address.
392 arch_write_linkage_table_jmp(void* reloc_addr
, void *target_addr
)
395 * Make JMP to function entry.
397 * The instruction sequence is:
399 * sethi %hi(addr), temp_reg
400 * jmp %temp_reg + %lo(addr), %addr_reg
406 unsigned long hi
; /* Top 22 bits of address */
407 unsigned long lo
; /* Low 10 bits of address */
410 inst_ptr
= (int*) reloc_addr
;
413 * Split the target address into hi and lo parts for the sethi
414 * instruction. hi is the top 22 bits. lo is the low 10 bits.
416 hi
= (unsigned long) target_addr
;
421 * sethi %hi(addr), temp_reg
424 inst
= (0 << 30) | (LINKAGE_TEMP_REG
<< 25) | (4 << 22) | hi
;
428 * jmpl [temp_reg + %lo(addr)], addr_reg
431 inst
= (2U << 30) | (LINKAGE_ADDR_REG
<< 25) | (0x38 << 19)
432 | (LINKAGE_TEMP_REG
<< 14) | (1 << 13) | lo
;
435 /* nop (really sethi 0, %g0) */
437 inst
= (0 << 30) | (0 << 25) | (4 << 22) | 0;
442 os_flush_icache((os_vm_address_t
) reloc_addr
, (char*) inst_ptr
- (char*) reloc_addr
);
446 arch_write_linkage_table_ref(void * reloc_addr
, void *target_addr
)
448 *(unsigned long *)reloc_addr
= (unsigned long)target_addr
;