3 This code was written as part of the CMU Common Lisp project at
4 Carnegie Mellon University, and has been placed in the public domain.
15 #include "internals.h"
20 #include "interrupt.h"
22 #include "breakpoint.h"
24 #define BREAKPOINT_INST 0xcc /* INT3 */
26 unsigned long fast_random_state = 1;
30 * Use the /dev/cpu/self/cpuid interface on Solaris. We could use the
31 * same method below, but the Sun C compiler miscompiles the inline
35 #include <sys/types.h>
42 void cpuid(int level, unsigned int* a, unsigned int* b,
43 unsigned int* c, unsigned int* d)
47 static const char devname[] = "/dev/cpu/self/cpuid";
49 *a = *b = *c = *d = 0;
50 if ((device = open(devname, O_RDONLY)) == -1) {
55 if (pread(device, regs, sizeof(regs), 1) != sizeof(regs)) {
72 #define __cpuid(level, a, b, c, d) \
73 __asm__ ("xchgl\t%%ebx, %1\n\t" \
75 "xchgl\t%%ebx, %1\n\t" \
76 : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
79 void cpuid(int level, unsigned int* a, unsigned int* b,
80 unsigned int* c, unsigned int* d)
82 unsigned int eax, ebx, ecx, edx;
84 __cpuid(level, eax, ebx, ecx, edx);
94 arch_support_sse2(void)
96 unsigned int eax, ebx, ecx, edx;
98 cpuid(1, &eax, &ebx, &ecx, &edx);
100 /* Return non-zero if SSE2 is supported */
101 return edx & 0x4000000;
105 arch_init(fpu_mode_t mode)
109 have_sse2 = arch_support_sse2() && os_support_sse2();
114 return "lisp-sse2.core";
116 return "lisp-x87.core";
120 return "lisp-x87.core";
123 return "lisp-sse2.core";
133 * Assuming we get here via an INT3 xxx instruction, the PC now
134 * points to the interrupt code (lisp value) so we just move past
135 * it. Skip the code, then if the code is an error-trap or
136 * Cerror-trap then skip the data bytes that follow.
140 arch_skip_instruction(os_context_t * context)
144 DPRINTF(0, (stderr, "[arch_skip_inst at %lx>]\n", SC_PC(context)));
146 /* Get and skip the lisp error code. */
147 code = *(char *) SC_PC(context)++;
151 /* Lisp error arg vector length */
152 vlen = *(char *) SC_PC(context)++;
153 /* Skip lisp error arg data bytes */
158 case trap_Breakpoint:
159 case trap_FunctionEndBreakpoint:
162 case trap_PendingInterrupt:
164 /* Only needed to skip the Code. */
168 fprintf(stderr, "[arch_skip_inst invalid code %d\n]\n", code);
172 DPRINTF(0, (stderr, "[arch_skip_inst resuming at %lx>]\n", SC_PC(context)));
176 arch_internal_error_arguments(os_context_t * context)
178 return (unsigned char *) (SC_PC(context) + 1);
182 arch_pseudo_atomic_atomic(os_context_t * context)
184 return SymbolValue(PSEUDO_ATOMIC_ATOMIC);
188 arch_set_pseudo_atomic_interrupted(os_context_t * context)
190 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1));
196 arch_install_breakpoint(void *pc)
198 unsigned long result = *(unsigned long *) pc;
200 *(char *) pc = BREAKPOINT_INST; /* x86 INT3 */
201 *((char *) pc + 1) = trap_Breakpoint; /* Lisp trap code */
207 arch_remove_breakpoint(void *pc, unsigned long orig_inst)
209 *((char *) pc) = orig_inst & 0xff;
210 *((char *) pc + 1) = (orig_inst & 0xff00) >> 8;
216 * When single stepping single_stepping holds the original instruction
220 unsigned int *single_stepping = NULL;
223 unsigned int single_step_save1;
224 unsigned int single_step_save2;
225 unsigned int single_step_save3;
229 arch_do_displaced_inst(os_context_t * context, unsigned long orig_inst)
231 unsigned int *pc = (unsigned int *) SC_PC(context);
234 * Put the original instruction back.
237 *((char *) pc) = orig_inst & 0xff;
238 *((char *) pc + 1) = (orig_inst & 0xff00) >> 8;
241 /* Enable single-stepping */
242 SC_EFLAGS(context) |= 0x100;
246 * Install helper instructions for the single step:
247 * nop; nop; nop; pushf; or [esp],0x100; popf.
249 * The or instruction enables the trap flag which enables
250 * single-stepping. So when the popf instruction is run, we start
251 * single-stepping and stop on the next instruction.
254 DPRINTF(0, (stderr, "Installing helper instructions\n"));
256 single_step_save1 = *(pc - 3);
257 single_step_save2 = *(pc - 2);
258 single_step_save3 = *(pc - 1);
259 *(pc - 3) = 0x9c909090;
260 *(pc - 2) = 0x00240c81;
261 *(pc - 1) = 0x9d000001;
264 single_stepping = (unsigned int *) pc;
268 * pc - 9 points to the pushf instruction that we installed for
272 DPRINTF(0, (stderr, " Setting pc to pushf instruction at %p\n", (void*) ((char*) pc - 9)));
273 SC_PC(context) = (int)((char *) pc - 9);
279 sigtrap_handler(HANDLER_ARGS)
282 os_context_t* os_context = (os_context_t *) context;
284 fprintf(stderr, "x86sigtrap: %8x %x\n",
285 SC_PC(os_os_context), *(unsigned char *) (SC_PC(os_context) - 1));
286 fprintf(stderr, "sigtrap(%d %d %x)\n", signal, CODE(code), os_context);
289 if (single_stepping && (signal == SIGTRAP)) {
291 fprintf(stderr, "* Single step trap %p\n", single_stepping);
295 /* Disable single-stepping */
296 SC_EFLAGS(os_context) ^= 0x100;
298 /* Un-install single step helper instructions. */
299 *(single_stepping - 3) = single_step_save1;
300 *(single_stepping - 2) = single_step_save2;
301 *(single_stepping - 1) = single_step_save3;
302 DPRINTF(0, (stderr, "Uninstalling helper instructions\n"));
306 * Re-install the breakpoint if possible.
308 if ((int) SC_PC(os_context) == (int) single_stepping + 1)
309 fprintf(stderr, "* Breakpoint not re-install\n");
311 char *ptr = (char *) single_stepping;
313 ptr[0] = BREAKPOINT_INST; /* x86 INT3 */
314 ptr[1] = trap_Breakpoint;
317 single_stepping = NULL;
321 /* This is just for info in case monitor wants to print an approx */
322 current_control_stack_pointer = (unsigned long *) SC_SP(os_context);
324 RESTORE_FPU(os_context);
327 * On entry %eip points just after the INT3 byte and aims at the
328 * 'kind' value (eg trap_Cerror). For error-trap and Cerror-trap a
329 * number of bytes will follow, the first is the length of the byte
330 * arguments to follow.
333 trap = *(unsigned char *) SC_PC(os_context);
336 case trap_PendingInterrupt:
337 DPRINTF(0, (stderr, "<trap Pending Interrupt.>\n"));
338 arch_skip_instruction(os_context);
339 interrupt_handle_pending(os_context);
344 FPU_STATE(fpu_state);
345 save_fpu_state(fpu_state);
347 fake_foreign_function_call(os_context);
348 lose("%%primitive halt called; the party is over.\n");
349 undo_fake_foreign_function_call(os_context);
351 restore_fpu_state(fpu_state);
352 arch_skip_instruction(os_context);
358 DPRINTF(0, (stderr, "<trap Error %x>\n", CODE(code)));
359 interrupt_internal_error(signal, code, os_context, CODE(code) == trap_Cerror);
362 case trap_Breakpoint:
364 fprintf(stderr, "*C break\n");
366 SC_PC(os_context) -= 1;
368 handle_breakpoint(signal, CODE(code), os_context);
370 fprintf(stderr, "*C break return\n");
374 case trap_FunctionEndBreakpoint:
375 SC_PC(os_context) -= 1;
377 (int) handle_function_end_breakpoint(signal, CODE(code), os_context);
380 #ifdef trap_DynamicSpaceOverflowWarning
381 case trap_DynamicSpaceOverflowWarning:
382 interrupt_handle_space_overflow(SymbolFunction
383 (DYNAMIC_SPACE_OVERFLOW_WARNING_HIT),
387 #ifdef trap_DynamicSpaceOverflowError
388 case trap_DynamicSpaceOverflowError:
389 interrupt_handle_space_overflow(SymbolFunction
390 (DYNAMIC_SPACE_OVERFLOW_ERROR_HIT),
396 (stderr, "[C--trap default %d %d %p]\n", signal, CODE(code),
398 interrupt_handle_now(signal, code, os_context);
404 arch_install_interrupt_handlers(void)
406 interrupt_install_low_level_handler(SIGILL, sigtrap_handler);
407 interrupt_install_low_level_handler(SIGTRAP, sigtrap_handler);
411 extern lispobj call_into_lisp(lispobj fun, lispobj * args, int nargs);
413 /* These next four functions are an interface to the
414 * Lisp call-in facility. Since this is C we can know
415 * nothing about the calling environment. The control
416 * stack might be the C stack if called from the monitor
417 * or the Lisp stack if called as a result of an interrupt
418 * or maybe even a separate stack. The args are most likely
419 * on that stack but could be in registers depending on
420 * what the compiler likes. So I try to package up the
421 * args into a portable vector and let the assembly language
422 * call-in function figure it out.
426 funcall0(lispobj function)
428 lispobj *args = NULL;
430 return call_into_lisp(function, args, 0);
434 funcall1(lispobj function, lispobj arg0)
439 return call_into_lisp(function, args, 1);
443 funcall2(lispobj function, lispobj arg0, lispobj arg1)
449 return call_into_lisp(function, args, 2);
453 funcall3(lispobj function, lispobj arg0, lispobj arg1, lispobj arg2)
460 return call_into_lisp(function, args, 3);
465 #ifndef LinkageEntrySize
466 #define LinkageEntrySize 8
470 arch_make_linkage_entry(long linkage_entry, void *target_addr, long type)
472 char *reloc_addr = (char *) (FOREIGN_LINKAGE_SPACE_START
474 + linkage_entry * LinkageEntrySize);
476 if (type == 1) { /* code reference */
477 /* Make JMP to function entry. */
478 /* JMP offset is calculated from next instruction. */
479 long offset = (char *) target_addr - (reloc_addr + 5);
482 *reloc_addr++ = 0xe9; /* opcode for JMP rel32 */
483 for (i = 0; i < 4; i++) {
484 *reloc_addr++ = offset & 0xff;
487 /* write a nop for good measure. */
489 } else if (type == 2) {
490 *(unsigned long *) reloc_addr = (unsigned long) target_addr;
494 /* Make a call to the first function in the linkage table, which is
495 resolve_linkage_tramp. */
497 arch_make_lazy_linkage(long linkage_entry)
499 char *reloc_addr = (char *) (FOREIGN_LINKAGE_SPACE_START
501 + linkage_entry * LinkageEntrySize);
502 long offset = (char *) (FOREIGN_LINKAGE_SPACE_START) - (reloc_addr + 5);
505 *reloc_addr++ = 0xe8; /* opcode for CALL rel32 */
506 for (i = 0; i < 4; i++) {
507 *reloc_addr++ = offset & 0xff;
510 /* write a nop for good measure. */
514 /* Get linkage entry. The initial instruction in the linkage
515 entry is a CALL; the return address we're passed points to the next
519 arch_linkage_entry(unsigned long retaddr)
521 return ((retaddr - 5) - FOREIGN_LINKAGE_SPACE_START) / LinkageEntrySize;
523 #endif /* LINKAGE_TABLE */
525 int ieee754_rem_pio2(double x, double *y0, double *y1)
527 extern int __ieee754_rem_pio2(double x, double *y);
532 n = __ieee754_rem_pio2(x, y);