2 * interrupt-handling magic
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
25 #include "interrupt.h"
35 void sigaddset_blockable(sigset_t *s)
39 sigaddset(s, SIGQUIT);
40 sigaddset(s, SIGPIPE);
41 sigaddset(s, SIGALRM);
44 sigaddset(s, SIGTSTP);
45 sigaddset(s, SIGCHLD);
47 sigaddset(s, SIGXCPU);
48 sigaddset(s, SIGXFSZ);
49 sigaddset(s, SIGVTALRM);
50 sigaddset(s, SIGPROF);
51 sigaddset(s, SIGWINCH);
52 sigaddset(s, SIGUSR1);
53 sigaddset(s, SIGUSR2);
56 /* When we catch an internal error, should we pass it back to Lisp to
57 * be handled in a high-level way? (Early in cold init, the answer is
58 * 'no', because Lisp is still too brain-dead to handle anything.
59 * After sufficient initialization has been completed, the answer
61 boolean internal_errors_enabled = 0;
63 os_context_t *lisp_interrupt_contexts[MAX_INTERRUPTS];
65 /* As far as I can tell, what's going on here is:
67 * In the case of most signals, when Lisp asks us to handle the
68 * signal, the outermost handler (the one actually passed to UNIX) is
69 * either interrupt_handle_now(..) or interrupt_handle_later(..).
70 * In that case, the Lisp-level handler is stored in interrupt_handlers[..]
71 * and interrupt_low_level_handlers[..] is cleared.
73 * However, some signals need special handling, e.g.
75 * o the SIGSEGV (for e.g. Linux) or SIGBUS (for e.g. FreeBSD) used by the
76 * garbage collector to detect violations of write protection,
77 * because some cases of such signals (e.g. GC-related violations of
78 * write protection) are handled at C level and never passed on to
79 * Lisp. For such signals, we still store any Lisp-level handler
80 * in interrupt_handlers[..], but for the outermost handle we use
81 * the value from interrupt_low_level_handlers[..], instead of the
82 * ordinary interrupt_handle_now(..) or interrupt_handle_later(..).
84 * o the SIGTRAP (Linux/Alpha) which Lisp code uses to handle breakpoints,
85 * pseudo-atomic sections, and some classes of error (e.g. "function
86 * not defined"). This never goes anywhere near the Lisp handlers at all.
87 * See runtime/alpha-arch.c and code/signal.lisp
89 * - WHN 20000728, dan 20010128 */
92 void (*interrupt_low_level_handlers[NSIG]) (int, siginfo_t*, void*) = {0};
93 union interrupt_handler interrupt_handlers[NSIG];
95 /* signal number, siginfo_t, and old mask information for pending signal
97 * pending_signal=0 when there is no pending signal. */
98 static int pending_signal = 0;
99 static siginfo_t pending_info;
100 static sigset_t pending_mask;
102 static boolean maybe_gc_pending = 0;
105 * utility routines used by various signal handlers
109 build_fake_control_stack_frames(os_context_t *context)
111 #ifndef LISP_FEATURE_X86
115 /* Build a fake stack frame or frames */
117 current_control_frame_pointer =
118 (lispobj *)(*os_context_register_addr(context, reg_CSP));
119 if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
120 == current_control_frame_pointer) {
121 /* There is a small window during call where the callee's
122 * frame isn't built yet. */
123 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
124 == FUN_POINTER_LOWTAG) {
125 /* We have called, but not built the new frame, so
126 * build it for them. */
127 current_control_frame_pointer[0] =
128 *os_context_register_addr(context, reg_OCFP);
129 current_control_frame_pointer[1] =
130 *os_context_register_addr(context, reg_LRA);
131 current_control_frame_pointer += 8;
132 /* Build our frame on top of it. */
133 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
136 /* We haven't yet called, build our frame as if the
137 * partial frame wasn't there. */
138 oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
141 /* We can't tell whether we are still in the caller if it had to
142 * allocate a stack frame due to stack arguments. */
143 /* This observation provoked some past CMUCL maintainer to ask
144 * "Can anything strange happen during return?" */
147 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
150 current_control_stack_pointer = current_control_frame_pointer + 8;
152 current_control_frame_pointer[0] = oldcont;
153 current_control_frame_pointer[1] = NIL;
154 current_control_frame_pointer[2] =
155 (lispobj)(*os_context_register_addr(context, reg_CODE));
160 fake_foreign_function_call(os_context_t *context)
164 /* Get current Lisp state from context. */
166 dynamic_space_free_pointer =
167 (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
169 if ((long)dynamic_space_free_pointer & 1) {
170 lose("dead in fake_foreign_function_call, context = %x", context);
175 current_binding_stack_pointer =
176 (lispobj *)(*os_context_register_addr(context, reg_BSP));
179 build_fake_control_stack_frames(context);
181 /* Do dynamic binding of the active interrupt context index
182 * and save the context in the context array. */
183 context_index = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
184 /* FIXME: Ick! Why use abstract "make_fixnum" in some places if
185 * you're going to convert from fixnum by bare >>2 in other
186 * places? Use fixnum_value(..) here, and look for other places
187 * which do bare >> and << for fixnum_value and make_fixnum. */
189 if (context_index >= MAX_INTERRUPTS) {
190 lose("maximum interrupt nesting depth (%d) exceeded", MAX_INTERRUPTS);
193 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
194 make_fixnum(context_index + 1));
196 lisp_interrupt_contexts[context_index] = context;
198 /* no longer in Lisp now */
199 foreign_function_call_active = 1;
203 undo_fake_foreign_function_call(os_context_t *context)
205 /* Block all blockable signals. */
208 sigaddset_blockable(&block);
209 sigprocmask(SIG_BLOCK, &block, 0);
211 /* going back into Lisp */
212 foreign_function_call_active = 0;
214 /* Undo dynamic binding. */
215 /* ### Do I really need to unbind_to_here()? */
216 /* FIXME: Is this to undo the binding of
217 * FREE_INTERRUPT_CONTEXT_INDEX? If so, we should say so. And
218 * perhaps yes, unbind_to_here() really would be clearer and less
220 /* dan (2001.08.10) thinks the above supposition is probably correct */
224 /* Put the dynamic space free pointer back into the context. */
225 *os_context_register_addr(context, reg_ALLOC) =
226 (unsigned long) dynamic_space_free_pointer;
230 /* a handler for the signal caused by execution of a trap opcode
231 * signalling an internal error */
233 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
236 lispobj context_sap = 0;
238 fake_foreign_function_call(context);
240 /* Allocate the SAP object while the interrupts are still
242 if (internal_errors_enabled) {
243 context_sap = alloc_sap(context);
246 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
248 if (internal_errors_enabled) {
249 SHOW("in interrupt_internal_error");
251 /* Display some rudimentary debugging information about the
252 * error, so that even if the Lisp error handler gets badly
253 * confused, we have a chance to determine what's going on. */
254 describe_internal_error(context);
256 funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
257 continuable ? T : NIL);
259 describe_internal_error(context);
260 /* There's no good way to recover from an internal error
261 * before the Lisp error handling mechanism is set up. */
262 lose("internal error too early in init, can't recover");
264 undo_fake_foreign_function_call(context);
266 arch_skip_instruction(context);
270 /* This function handles pending interrupts. Note that in C/kernel
271 * terms we dealt with the signal already; we just haven't decided
272 * whether to call a Lisp handler or do a GC or something like that.
273 * If it helps, you can think of pending_{signal,mask,info} as a
274 * one-element queue of signals that we have acknowledged but not
278 interrupt_handle_pending(os_context_t *context)
281 boolean were_in_lisp = !foreign_function_call_active;
284 SetSymbolValue(INTERRUPT_PENDING, NIL);
286 if (maybe_gc_pending) {
287 maybe_gc_pending = 0;
292 fake_foreign_function_call(context);
294 funcall0(SymbolFunction(MAYBE_GC));
299 undo_fake_foreign_function_call(context);
303 /* FIXME: This isn't very clear. It would be good to reverse
304 * engineer it and rewrite the code more clearly, or write a clear
305 * explanation of what's going on in the comments, or both.
307 * WHN's question 1a: How come we unconditionally copy from
308 * pending_mask into the context, and then test whether
309 * pending_signal is set?
311 * WHN's question 1b: If pending_signal wasn't set, how could
312 * pending_mask be valid?
314 * Dan Barlow's reply (sbcl-devel 2001-03-13): And the answer is -
315 * or appears to be - because interrupt_maybe_gc set it that way
316 * (look in the #ifndef __i386__ bit). We can't GC during a
317 * pseudo-atomic, so we set maybe_gc_pending=1 and
318 * arch_set_pseudo_atomic_interrupted(..) When we come out of
319 * pseudo_atomic we're marked as interrupted, so we call
320 * interrupt_handle_pending, which does the GC using the pending
321 * context (it needs a context so that it has registers to use as
322 * GC roots) then notices there's no actual interrupt handler to
323 * call, so doesn't. That's the second question [1b] answered,
324 * anyway. Why we still need to copy the pending_mask into the
325 * context given that we're now done with the context anyway, I
328 memcpy(os_context_sigmask_addr(context), &pending_mask,
329 4 /* sizeof(sigset_t) */ );
331 sigemptyset(&pending_mask);
332 if (pending_signal) {
333 int signal = pending_signal;
335 memcpy(&info, &pending_info, sizeof(siginfo_t));
337 interrupt_handle_now(signal, &info, context);
342 * the two main signal handlers:
343 * interrupt_handle_now(..)
344 * maybe_now_maybe_later(..)
346 * to which we have added interrupt_handle_now_handler(..). Why?
347 * Well, mostly because the SPARC/Linux platform doesn't quite do
348 * signals the way we want them done. The third argument in the
349 * handler isn't filled in by the kernel properly, so we fix it up
350 * ourselves in the arch_os_get_context(..) function; however, we only
351 * want to do this when we first hit the handler, and not when
352 * interrupt_handle_now(..) is being called from some other handler
353 * (when the fixup will already have been done). -- CSR, 2002-07-23
357 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
359 os_context_t *context = (os_context_t*)void_context;
361 boolean were_in_lisp;
363 union interrupt_handler handler;
365 #ifdef LISP_FEATURE_LINUX
366 /* Under Linux on some architectures, we appear to have to restore
367 the FPU control word from the context, as after the signal is
368 delivered we appear to have a null FPU control word. */
369 os_restore_fp_control(context);
371 handler = interrupt_handlers[signal];
373 if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
378 were_in_lisp = !foreign_function_call_active;
382 fake_foreign_function_call(context);
387 "/entering interrupt_handle_now(%d, info, context)\n",
391 if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
393 /* This can happen if someone tries to ignore or default one
394 * of the signals we need for runtime support, and the runtime
395 * support decides to pass on it. */
396 lose("no handler for signal %d in interrupt_handle_now(..)", signal);
398 } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
400 /* Allocate the SAPs while the interrupts are still disabled.
401 * (FIXME: Why? This is the way it was done in CMU CL, and it
402 * even had the comment noting that this is the way it was
403 * done, but no motivation..) */
404 lispobj info_sap,context_sap = alloc_sap(context);
405 info_sap = alloc_sap(info);
406 /* Allow signals again. */
407 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
410 SHOW("calling Lisp-level handler");
413 funcall3(handler.lisp,
420 SHOW("calling C-level handler");
423 /* Allow signals again. */
424 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
426 (*handler.c)(signal, info, void_context);
433 undo_fake_foreign_function_call(context);
438 "/returning from interrupt_handle_now(%d, info, context)\n",
444 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
446 os_context_t *context = arch_os_get_context(&void_context);
448 #ifdef LISP_FEATURE_LINUX
449 os_restore_fp_control(context);
452 /* see comments at top of code/signal.lisp for what's going on here
453 * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW
455 if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
457 /* FIXME: This code is exactly the same as the code in the
458 * other leg of the if(..), and should be factored out into
459 * a shared function. */
460 pending_signal = signal;
461 memcpy(&pending_info, info, sizeof(siginfo_t));
462 memcpy(&pending_mask,
463 os_context_sigmask_addr(context),
465 sigaddset_blockable(os_context_sigmask_addr(context));
466 SetSymbolValue(INTERRUPT_PENDING, T);
470 (!foreign_function_call_active) &&
472 arch_pseudo_atomic_atomic(context)) {
474 /* FIXME: It would probably be good to replace these bare
475 * memcpy(..) calls with calls to cpy_siginfo_t and
476 * cpy_sigset_t, so that we only have to get the sizeof
477 * expressions right in one place, and after that static type
478 * checking takes over. */
479 pending_signal = signal;
480 memcpy(&pending_info, info, sizeof(siginfo_t));
481 memcpy(&pending_mask,
482 os_context_sigmask_addr(context),
484 sigaddset_blockable(os_context_sigmask_addr(context));
486 arch_set_pseudo_atomic_interrupted(context);
489 interrupt_handle_now(signal, info, context);
495 interrupt_handle_now_handler(int signal, siginfo_t *info, void *void_context)
497 os_context_t *context = arch_os_get_context(&void_context);
498 interrupt_handle_now(signal, info, context);
502 * stuff to detect and handle hitting the GC trigger
505 #ifndef LISP_FEATURE_GENCGC
506 /* since GENCGC has its own way to record trigger */
508 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
510 if (current_auto_gc_trigger == NULL)
513 void *badaddr=arch_get_bad_addr(signal,info,context);
514 return (badaddr >= (void *)current_auto_gc_trigger &&
515 badaddr <((void *)current_dynamic_space + DYNAMIC_SPACE_SIZE));
520 /* and similarly for the control stack guard page */
522 boolean handle_control_stack_guard_triggered(os_context_t *context,void *addr)
524 /* note the os_context hackery here. When the signal handler returns,
525 * it won't go back to what it was doing ... */
526 if(addr>=(void *)CONTROL_STACK_GUARD_PAGE &&
527 addr<(void *)(CONTROL_STACK_GUARD_PAGE+os_vm_page_size)) {
531 /* we hit the end of the control stack. disable protection
532 * temporarily so the error handler has some headroom */
533 protect_control_stack_guard_page(0);
536 native_pointer((lispobj) SymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
537 code = &(((struct simple_fun *) fun)->code);
539 /* Build a stack frame showing `interrupted' so that the
540 * user's backtrace makes (as much) sense (as usual) */
541 build_fake_control_stack_frames(context);
542 /* signal handler will "return" to this error-causing function */
543 *os_context_pc_addr(context) = code;
544 #ifdef LISP_FEATURE_X86
545 *os_context_register_addr(context,reg_ECX) = 0;
547 /* this much of the calling convention is common to all
549 *os_context_register_addr(context,reg_NARGS) = 0;
550 *os_context_register_addr(context,reg_LIP) = code;
551 *os_context_register_addr(context,reg_CFP) =
552 current_control_frame_pointer;
554 #ifdef ARCH_HAS_NPC_REGISTER
555 *os_context_npc_addr(context) =
556 4 + *os_context_pc_addr(context);
558 #ifdef LISP_FEATURE_SPARC
559 /* Bletch. This is a feature of the SPARC calling convention,
560 which sadly I'm not going to go into in large detail here,
561 as I don't know it well enough. Suffice to say that if the
564 (INST MOVE CODE-TN FUNCTION)
566 in compiler/sparc/call.lisp is changed, then this bit can
567 probably go away. -- CSR, 2002-07-24 */
568 *os_context_register_addr(context,reg_CODE) =
569 fun + FUN_POINTER_LOWTAG;
576 #ifndef LISP_FEATURE_X86
577 /* This function gets called from the SIGSEGV (for e.g. Linux or
578 * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
579 * whether the signal was due to treading on the mprotect()ed zone -
580 * and if so, arrange for a GC to happen. */
582 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
584 os_context_t *context=(os_context_t *) void_context;
586 if (!foreign_function_call_active
587 #ifndef LISP_FEATURE_GENCGC
588 /* nb: GENCGC on non-x86? I really don't think so. This
589 * happens every time */
590 && gc_trigger_hit(signal, info, context)
593 #ifndef LISP_FEATURE_GENCGC
594 clear_auto_gc_trigger();
597 if (arch_pseudo_atomic_atomic(context)) {
598 /* don't GC during an atomic operation. Instead, copy the
599 * signal mask somewhere safe. interrupt_handle_pending
600 * will detect pending_signal==0 and know to do a GC with the
601 * signal context instead of calling a Lisp-level handler */
602 maybe_gc_pending = 1;
603 if (pending_signal == 0) {
604 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
605 * idiom occurs over and over. It should be factored out
606 * into a function with a descriptive name. */
607 memcpy(&pending_mask,
608 os_context_sigmask_addr(context),
610 sigaddset_blockable(os_context_sigmask_addr(context));
612 arch_set_pseudo_atomic_interrupted(context);
615 lispobj *old_free_space=current_dynamic_space;
616 fake_foreign_function_call(context);
617 funcall0(SymbolFunction(MAYBE_GC));
618 undo_fake_foreign_function_call(context);
619 if(current_dynamic_space==old_free_space)
620 /* MAYBE-GC (as the name suggest) might not. If it
621 * doesn't, it won't reset the GC trigger either, so we
622 * have to do it ourselves. Put it near the end of
623 * dynamic space so we're not running into it continually
625 set_auto_gc_trigger(DYNAMIC_SPACE_SIZE
626 -(u32)os_vm_page_size);
636 * noise to install handlers
640 * what low-level signal handlers looked like before
641 * undoably_install_low_level_interrupt_handler() got involved
643 struct low_level_signal_handler_state {
645 void (*handler)(int, siginfo_t*, void*);
646 } old_low_level_signal_handler_states[NSIG];
649 uninstall_low_level_interrupt_handlers_atexit(void)
652 for (signal = 0; signal < NSIG; ++signal) {
653 struct low_level_signal_handler_state
654 *old_low_level_signal_handler_state =
655 old_low_level_signal_handler_states + signal;
656 if (old_low_level_signal_handler_state->was_modified) {
658 sa.sa_sigaction = old_low_level_signal_handler_state->handler;
659 sigemptyset(&sa.sa_mask);
660 sa.sa_flags = SA_SIGINFO | SA_RESTART;
661 sigaction(signal, &sa, NULL);
666 /* Undoably install a special low-level handler for signal; or if
667 * handler is SIG_DFL, remove any special handling for signal.
669 * The "undoably" aspect is because we also arrange with atexit() for
670 * the handler to be restored to its old value. This is for tidiness:
671 * it shouldn't matter much ordinarily, but it does remove a window
672 * where e.g. memory fault signals (SIGSEGV or SIGBUS, which in
673 * ordinary operation of SBCL are sent to the generational garbage
674 * collector, then possibly onward to Lisp code) or SIGINT (which is
675 * ordinarily passed to Lisp code) could otherwise be handled
676 * bizarrely/brokenly because the Lisp code would try to deal with
677 * them using machinery (like stream output buffers) which has already
678 * been dismantled. */
680 undoably_install_low_level_interrupt_handler (int signal,
686 struct low_level_signal_handler_state *old_low_level_signal_handler_state =
687 old_low_level_signal_handler_states + signal;
689 if (0 > signal || signal >= NSIG) {
690 lose("bad signal number %d", signal);
693 sa.sa_sigaction = handler;
694 sigemptyset(&sa.sa_mask);
695 sigaddset_blockable(&sa.sa_mask);
696 sa.sa_flags = SA_SIGINFO | SA_RESTART;
697 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
698 /* Signal handlers are run on the control stack, so if it is exhausted
699 * we had better use an alternate stack for whatever signal tells us
700 * we've exhausted it */
701 if(signal==SIG_MEMORY_FAULT) {
703 sigstack.ss_sp=(void *) ALTERNATE_SIGNAL_STACK_START;
705 sigstack.ss_size = SIGSTKSZ;
706 sigaltstack(&sigstack,0);
707 sa.sa_flags|=SA_ONSTACK;
711 /* In the case of interrupt handlers which are modified more than
712 * once, we only save the original unmodified copy. */
713 if (!old_low_level_signal_handler_state->was_modified) {
714 struct sigaction *old_handler =
715 (struct sigaction*) &old_low_level_signal_handler_state->handler;
716 old_low_level_signal_handler_state->was_modified = 1;
717 sigaction(signal, &sa, old_handler);
719 sigaction(signal, &sa, NULL);
722 interrupt_low_level_handlers[signal] =
723 (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
726 /* This is called from Lisp. */
728 install_handler(int signal, void handler(int, siginfo_t*, void*))
732 union interrupt_handler oldhandler;
734 FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
737 sigaddset(&new, signal);
738 sigprocmask(SIG_BLOCK, &new, &old);
741 sigaddset_blockable(&new);
743 FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%d\n",
744 interrupt_low_level_handlers[signal]));
745 if (interrupt_low_level_handlers[signal]==0) {
746 if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
747 ARE_SAME_HANDLER(handler, SIG_IGN)) {
748 sa.sa_sigaction = handler;
749 } else if (sigismember(&new, signal)) {
750 sa.sa_sigaction = maybe_now_maybe_later;
752 sa.sa_sigaction = interrupt_handle_now_handler;
755 sigemptyset(&sa.sa_mask);
756 sigaddset_blockable(&sa.sa_mask);
757 sa.sa_flags = SA_SIGINFO | SA_RESTART;
759 sigaction(signal, &sa, NULL);
762 oldhandler = interrupt_handlers[signal];
763 interrupt_handlers[signal].c = handler;
765 sigprocmask(SIG_SETMASK, &old, 0);
767 FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
769 return (unsigned long)oldhandler.lisp;
777 SHOW("entering interrupt_init()");
779 /* Set up for recovery from any installed low-level handlers. */
780 atexit(&uninstall_low_level_interrupt_handlers_atexit);
782 /* Set up high level handler information. */
783 for (i = 0; i < NSIG; i++) {
784 interrupt_handlers[i].c =
785 /* (The cast here blasts away the distinction between
786 * SA_SIGACTION-style three-argument handlers and
787 * signal(..)-style one-argument handlers, which is OK
788 * because it works to call the 1-argument form where the
789 * 3-argument form is expected.) */
790 (void (*)(int, siginfo_t*, void*))SIG_DFL;
793 SHOW("returning from interrupt_init()");