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)
38 unsigned long badinst;
42 pc = (unsigned long *)(*os_context_pc_addr(context));
44 /* On the sparc, we have to decode the instruction. */
46 /* Make sure it's not the pc thats bogus, and that it was lisp code */
47 /* that caused the fault. */
48 if ((unsigned long) pc & 3) {
52 if ((pc < READ_ONLY_SPACE_START ||
53 pc >= READ_ONLY_SPACE_START+READ_ONLY_SPACE_SIZE) &&
54 (pc < current_dynamic_space ||
55 pc >= current_dynamic_space + DYNAMIC_SPACE_SIZE)) {
61 if ((badinst >> 30) != 3)
62 /* All load/store instructions have op = 11 (binary) */
65 rs1 = (badinst>>14)&0x1f;
67 if (badinst & (1<<13)) {
68 /* r[rs1] + simm(13) */
69 int simm13 = badinst & 0x1fff;
74 return (os_vm_address_t)
75 (*os_context_register_addr(context, rs1)+simm13);
79 int rs2 = badinst & 0x1f;
81 return (os_vm_address_t)
82 (*os_context_register_addr(context, rs1) +
83 *os_context_register_addr(context, rs2));
87 void arch_skip_instruction(os_context_t *context)
89 ((char *) *os_context_pc_addr(context)) = ((char *) *os_context_npc_addr(context));
90 ((char *) *os_context_npc_addr(context)) += 4;
93 unsigned char *arch_internal_error_arguments(os_context_t *context)
95 return (unsigned char *)(*os_context_pc_addr(context) + 4);
98 boolean arch_pseudo_atomic_atomic(os_context_t *context)
100 return ((*os_context_register_addr(context,reg_ALLOC)) & 4);
103 void arch_set_pseudo_atomic_interrupted(os_context_t *context)
105 *os_context_register_addr(context,reg_ALLOC) |= 1;
108 unsigned long arch_install_breakpoint(void *pc)
110 unsigned long *ptr = (unsigned long *)pc;
111 unsigned long result = *ptr;
112 *ptr = trap_Breakpoint;
114 os_flush_icache((os_vm_address_t) pc, sizeof(unsigned long));
119 void arch_remove_breakpoint(void *pc, unsigned long orig_inst)
121 *(unsigned long *)pc = orig_inst;
122 os_flush_icache((os_vm_address_t) pc, sizeof(unsigned long));
125 static unsigned long *skipped_break_addr, displaced_after_inst;
126 static sigset_t orig_sigmask;
128 void arch_do_displaced_inst(os_context_t *context, unsigned int orig_inst)
130 unsigned long *pc = (unsigned long *)(*os_context_pc_addr(context));
131 unsigned long *npc = (unsigned long *)(*os_context_npc_addr(context));
133 /* orig_sigmask = context->sigmask;
134 sigemptyset(&context->sigmask); */
136 /* FILLBLOCKSET(&context->uc_sigmask);*/
139 os_flush_icache((os_vm_address_t) pc, sizeof(unsigned long));
140 skipped_break_addr = pc;
141 displaced_after_inst = *npc;
142 *npc = trap_AfterBreakpoint;
143 os_flush_icache((os_vm_address_t) npc, sizeof(unsigned long));
147 static int pseudo_atomic_trap_p(os_context_t *context)
150 unsigned int badinst;
154 pc = (unsigned int*) *os_context_pc_addr(context);
158 /* Check to see if the current instruction is a pseudo-atomic-trap */
159 if (((badinst >> 30) == 2) && (((badinst >> 19) & 0x3f) == 0x3a)
160 && (((badinst >> 13) & 1) == 1) && ((badinst & 0x7f) == PSEUDO_ATOMIC_TRAP))
162 unsigned int previnst;
165 * Check to see if the previous instruction was an andcc alloc-tn,
166 * 3, zero-tn instruction.
168 if (((previnst >> 30) == 2) && (((previnst >> 19) & 0x3f) == 0x11)
169 && (((previnst >> 14) & 0x1f) == reg_ALLOC)
170 && (((previnst >> 25) & 0x1f) == reg_ZERO)
171 && (((previnst >> 13) & 1) == 1)
172 && ((previnst & 0x1fff) == 3))
178 fprintf(stderr, "Oops! Got a PSEUDO-ATOMIC-TRAP without a preceeding andcc!\n");
184 static void sigill_handler(int signal, siginfo_t *siginfo, void *void_context)
186 os_context_t *context = arch_os_get_context(&void_context);
187 #ifdef LISP_FEATURE_LINUX
188 /* FIXME: Check that this is necessary -- CSR, 2002-07-15 */
189 os_restore_fp_control(context);
191 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
193 if ((siginfo->si_code) == ILL_ILLOPC
194 #ifdef LISP_FEATURE_LINUX
195 || (linux_sparc_siginfo_bug && (siginfo->si_code == 2))
200 unsigned int* pc = (unsigned int*) siginfo->si_addr;
203 trap = inst & 0x3fffff;
206 case trap_PendingInterrupt:
207 arch_skip_instruction(context);
208 interrupt_handle_pending(context);
212 fake_foreign_function_call(context);
213 lose("%%primitive halt called; the party is over.\n");
217 interrupt_internal_error(signal, siginfo, context, trap == trap_Cerror);
220 case trap_Breakpoint:
221 handle_breakpoint(signal, siginfo, context);
224 case trap_FunEndBreakpoint:
225 *os_context_pc_addr(context) = (int) handle_fun_end_breakpoint(signal, siginfo, context);
226 *os_context_npc_addr(context) = *os_context_pc_addr(context) + 4;
229 case trap_AfterBreakpoint:
230 *skipped_break_addr = trap_Breakpoint;
231 skipped_break_addr = NULL;
232 *(unsigned long *) os_context_pc_addr(context) = displaced_after_inst;
233 /* context->sigmask = orig_sigmask; */
234 os_flush_icache((os_vm_address_t) os_context_pc_addr(context), sizeof(unsigned long));
238 interrupt_handle_now(signal, siginfo, context);
242 else if ((siginfo->si_code) == ILL_ILLTRP
243 #ifdef LISP_FEATURE_LINUX
244 || (linux_sparc_siginfo_bug && (siginfo->si_code) == 192)
247 if (pseudo_atomic_trap_p(context)) {
248 /* A trap instruction from a pseudo-atomic. We just need
249 to fixup up alloc-tn to remove the interrupted flag,
250 skip over the trap instruction, and then handle the
251 pending interrupt(s). */
252 *os_context_register_addr(context, reg_ALLOC) &= ~7;
253 arch_skip_instruction(context);
254 interrupt_handle_pending(context);
257 interrupt_internal_error(signal, siginfo, context, 0);
261 interrupt_handle_now(signal, siginfo, context);
265 static void sigemt_handler(int signal, siginfo_t *siginfo, void *void_context)
267 unsigned long badinst;
268 boolean subtract, immed;
269 int rd, rs1, op1, rs2, op2, result;
270 os_context_t *context = arch_os_get_context(&void_context);
271 #ifdef LISP_FEATURE_LINUX
272 os_restore_fp_control(context);
275 badinst = *(unsigned long *)os_context_pc_addr(context);
276 if ((badinst >> 30) != 2 || ((badinst >> 20) & 0x1f) != 0x11) {
277 /* It wasn't a tagged add. Pass the signal into lisp. */
278 interrupt_handle_now(signal, siginfo, context);
282 fprintf(stderr, "SIGEMT trap handler with tagged op instruction!\n");
284 /* Extract the parts of the inst. */
285 subtract = badinst & (1<<19);
286 rs1 = (badinst>>14) & 0x1f;
287 op1 = *os_context_register_addr(context, rs1);
289 /* If the first arg is $ALLOC then it is really a signal-pending note */
290 /* for the pseudo-atomic noise. */
291 if (rs1 == reg_ALLOC) {
292 /* Perform the op anyway. */
293 op2 = badinst & 0x1fff;
300 *os_context_register_addr(context, reg_ALLOC) = result & ~7;
301 arch_skip_instruction(context);
302 interrupt_handle_pending(context);
306 if ((op1 & 3) != 0) {
307 /* The first arg wan't a fixnum. */
308 interrupt_internal_error(signal, siginfo, context, 0);
312 if (immed = badinst & (1<<13)) {
313 op2 = badinst & 0x1fff;
318 rs2 = badinst & 0x1f;
319 op2 = *os_context_register_addr(context, rs2);
322 if ((op2 & 3) != 0) {
323 /* The second arg wan't a fixnum. */
324 interrupt_internal_error(signal, siginfo, context, 0);
328 rd = (badinst>>25) & 0x1f;
330 /* Don't bother computing the result unless we are going to use it. */
332 result = (op1>>2) - (op2>>2);
334 result = (op1>>2) + (op2>>2);
336 dynamic_space_free_pointer =
337 (lispobj *) *os_context_register_addr(context, reg_ALLOC);
339 *os_context_register_addr(context, rd) = alloc_number(result);
341 *os_context_register_addr(context, reg_ALLOC) =
342 (unsigned long) dynamic_space_free_pointer;
345 arch_skip_instruction(context);
348 void arch_install_interrupt_handlers()
350 undoably_install_low_level_interrupt_handler(SIGILL , sigill_handler);
351 undoably_install_low_level_interrupt_handler(SIGEMT, sigemt_handler);
355 extern lispobj call_into_lisp(lispobj fun, lispobj *args, int nargs);
357 lispobj funcall0(lispobj function)
359 lispobj *args = current_control_stack_pointer;
361 return call_into_lisp(function, args, 0);
364 lispobj funcall1(lispobj function, lispobj arg0)
366 lispobj *args = current_control_stack_pointer;
368 current_control_stack_pointer += 1;
371 return call_into_lisp(function, args, 1);
374 lispobj funcall2(lispobj function, lispobj arg0, lispobj arg1)
376 lispobj *args = current_control_stack_pointer;
378 current_control_stack_pointer += 2;
382 return call_into_lisp(function, args, 2);
385 lispobj funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
387 lispobj *args = current_control_stack_pointer;
389 current_control_stack_pointer += 3;
394 return call_into_lisp(function, args, 3);
397 #ifdef LISP_FEATURE_LINKAGE_TABLE
399 /* This a naive port from CMUCL/sparc, which was mostly stolen from the
400 * CMUCL/x86 version, with adjustments for sparc
402 * Linkage entry size is 16, because we need at least 3 instruction to
405 * sethi %hi(addr), %g4
406 * jmpl [%g4 + %lo(addr)], %g5
409 * The Sparc V9 ABI seems to use 8 words for its jump tables. Maybe
410 * we should do the same?
414 * Define the registers to use in the linkage jump table. Can be the
415 * same. Some care must be exercised when choosing these. It has to be
416 * a register that is not otherwise being used. reg_L0 is a good
417 * choice. call_into_c trashes reg_L0 without preserving it, so we can
418 * trash it in the linkage jump table.
420 #define LINKAGE_TEMP_REG reg_L0
421 #define LINKAGE_ADDR_REG reg_L0
424 * Insert the necessary jump instructions at the given address.
427 arch_write_linkage_table_jmp(void* reloc_addr, void *target_addr)
430 * Make JMP to function entry.
432 * The instruction sequence is:
434 * sethi %hi(addr), temp_reg
435 * jmp %temp_reg + %lo(addr), %addr_reg
441 unsigned long hi; /* Top 22 bits of address */
442 unsigned long lo; /* Low 10 bits of address */
445 inst_ptr = (int*) reloc_addr;
448 * Split the target address into hi and lo parts for the sethi
449 * instruction. hi is the top 22 bits. lo is the low 10 bits.
451 hi = (unsigned long) target_addr;
456 * sethi %hi(addr), temp_reg
459 inst = (0 << 30) | (LINKAGE_TEMP_REG << 25) | (4 << 22) | hi;
463 * jmpl [temp_reg + %lo(addr)], addr_reg
466 inst = (2U << 30) | (LINKAGE_ADDR_REG << 25) | (0x38 << 19)
467 | (LINKAGE_TEMP_REG << 14) | (1 << 13) | lo;
470 /* nop (really sethi 0, %g0) */
472 inst = (0 << 30) | (0 << 25) | (4 << 22) | 0;
477 os_flush_icache((os_vm_address_t) reloc_addr, (char*) inst_ptr - (char*) reloc_addr);
481 arch_write_linkage_table_ref(void * reloc_addr, void *target_addr)
483 *(unsigned long *)reloc_addr = (unsigned long)target_addr;