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(..)
348 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
350 os_context_t *context = (os_context_t*)void_context;
352 boolean were_in_lisp;
354 union interrupt_handler handler;
356 #ifdef LISP_FEATURE_LINUX
357 /* Under Linux, we appear to have to restore the fpu control word
358 from the context, as after the signal is delivered we appear to
359 have a null fpu control word. */
360 os_restore_fp_control(context);
362 handler = interrupt_handlers[signal];
364 if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
369 were_in_lisp = !foreign_function_call_active;
373 fake_foreign_function_call(context);
378 "/entering interrupt_handle_now(%d, info, context)\n",
382 if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
384 /* This can happen if someone tries to ignore or default one
385 * of the signals we need for runtime support, and the runtime
386 * support decides to pass on it. */
387 lose("no handler for signal %d in interrupt_handle_now(..)", signal);
389 } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
391 /* Allocate the SAPs while the interrupts are still disabled.
392 * (FIXME: Why? This is the way it was done in CMU CL, and it
393 * even had the comment noting that this is the way it was
394 * done, but no motivation..) */
395 lispobj info_sap,context_sap = alloc_sap(context);
396 info_sap = alloc_sap(info);
397 /* Allow signals again. */
398 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
401 SHOW("calling Lisp-level handler");
404 funcall3(handler.lisp,
411 SHOW("calling C-level handler");
414 /* Allow signals again. */
415 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
417 (*handler.c)(signal, info, void_context);
424 undo_fake_foreign_function_call(context);
429 "/returning from interrupt_handle_now(%d, info, context)\n",
435 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
437 os_context_t *context = (os_context_t*)void_context;
439 /* FIXME: See Debian cmucl 2.4.17, and mail from DTC on the CMU CL
440 * mailing list 23 Oct 1999, for changes in FPU handling at
441 * interrupt time which should be ported into SBCL. Also see the
442 * analogous logic at the head of interrupt_handle_now for
443 * more related FIXME stuff.
446 #ifdef LISP_FEATURE_LINUX
447 os_restore_fp_control(context);
450 /* see comments at top of code/signal.lisp for what's going on here
451 * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW
453 if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
455 /* FIXME: This code is exactly the same as the code in the
456 * other leg of the if(..), and should be factored out into
457 * a shared function. */
458 pending_signal = signal;
459 memcpy(&pending_info, info, sizeof(siginfo_t));
460 memcpy(&pending_mask,
461 os_context_sigmask_addr(context),
463 sigaddset_blockable(os_context_sigmask_addr(context));
464 SetSymbolValue(INTERRUPT_PENDING, T);
468 (!foreign_function_call_active) &&
470 arch_pseudo_atomic_atomic(context)) {
472 /* FIXME: It would probably be good to replace these bare
473 * memcpy(..) calls with calls to cpy_siginfo_t and
474 * cpy_sigset_t, so that we only have to get the sizeof
475 * expressions right in one place, and after that static type
476 * checking takes over. */
477 pending_signal = signal;
478 memcpy(&pending_info, info, sizeof(siginfo_t));
479 memcpy(&pending_mask,
480 os_context_sigmask_addr(context),
482 sigaddset_blockable(os_context_sigmask_addr(context));
484 arch_set_pseudo_atomic_interrupted(context);
487 interrupt_handle_now(signal, info, context);
492 * stuff to detect and handle hitting the GC trigger
495 #ifndef GENCGC /* since GENCGC has its own way to record trigger */
497 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
499 if (current_auto_gc_trigger == NULL)
502 void *badaddr=arch_get_bad_addr(signal,info,context);
503 return (badaddr >= (void *)current_auto_gc_trigger &&
504 badaddr <((void *)current_dynamic_space + DYNAMIC_SPACE_SIZE));
509 /* and similarly for the control stack guard page */
511 boolean handle_control_stack_guard_triggered(os_context_t *context,void *addr)
513 /* note the os_context hackery here. When the signal handler returns,
514 * it won't go back to what it was doing ... */
515 if(addr>=CONTROL_STACK_GUARD_PAGE &&
516 addr<(CONTROL_STACK_GUARD_PAGE+os_vm_page_size)) {
518 /* we hit the end of the control stack. disable protection
519 * temporarily so the error handler has some headroom */
520 protect_control_stack_guard_page(0);
523 &(((struct simple_fun *)
524 native_pointer(SymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR)))
527 /* Build a stack frame showing `interrupted' so that the
528 * user's backtrace makes (as much) sense (as usual) */
529 build_fake_control_stack_frames(context);
530 /* signal handler will "return" to this error-causing function */
531 *os_context_pc_addr(context)= function;
532 #ifndef LISP_FEATURE_X86
533 /* this much of the calling convention is common to all
535 *os_context_register_addr(context,reg_NARGS)=0;
536 *os_context_register_addr(context,reg_LIP)= function;
537 *os_context_register_addr(context,reg_CFP)=
538 current_control_frame_pointer;
539 #ifdef ARCH_HAS_NPC_REGISTER
540 *os_context_register_addr(context,reg_LIP)=
541 4+*os_context_pc_addr(context);
550 /* This function gets called from the SIGSEGV (for e.g. Linux or
551 * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
552 * whether the signal was due to treading on the mprotect()ed zone -
553 * and if so, arrange for a GC to happen. */
555 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
557 os_context_t *context=(os_context_t *) void_context;
559 if (!foreign_function_call_active
560 #ifndef GENCGC /* since GENCGC has its own way to record trigger */
561 && gc_trigger_hit(signal, info, context)
564 #ifndef GENCGC /* since GENCGC has its own way to record trigger */
565 clear_auto_gc_trigger();
568 if (arch_pseudo_atomic_atomic(context)) {
569 /* don't GC during an atomic operation. Instead, copy the
570 * signal mask somewhere safe. interrupt_handle_pending
571 * will detect pending_signal==0 and know to do a GC with the
572 * signal context instead of calling a Lisp-level handler */
573 maybe_gc_pending = 1;
574 if (pending_signal == 0) {
575 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
576 * idiom occurs over and over. It should be factored out
577 * into a function with a descriptive name. */
578 memcpy(&pending_mask,
579 os_context_sigmask_addr(context),
581 sigaddset_blockable(os_context_sigmask_addr(context));
583 arch_set_pseudo_atomic_interrupted(context);
586 lispobj *old_free_space=current_dynamic_space;
587 fake_foreign_function_call(context);
588 funcall0(SymbolFunction(MAYBE_GC));
589 undo_fake_foreign_function_call(context);
590 if(current_dynamic_space==old_free_space)
591 /* MAYBE-GC (as the name suggest) might not. If it
592 * doesn't, it won't reset the GC trigger either, so we
593 * have to do it ourselves. Put it near the end of
594 * dynamic space so we're not running into it continually
596 set_auto_gc_trigger(DYNAMIC_SPACE_SIZE
597 -(u32)os_vm_page_size);
607 * noise to install handlers
611 * what low-level signal handlers looked like before
612 * undoably_install_low_level_interrupt_handler() got involved
614 struct low_level_signal_handler_state {
616 void (*handler)(int, siginfo_t*, void*);
617 } old_low_level_signal_handler_states[NSIG];
620 uninstall_low_level_interrupt_handlers_atexit(void)
623 for (signal = 0; signal < NSIG; ++signal) {
624 struct low_level_signal_handler_state
625 *old_low_level_signal_handler_state =
626 old_low_level_signal_handler_states + signal;
627 if (old_low_level_signal_handler_state->was_modified) {
629 sa.sa_sigaction = old_low_level_signal_handler_state->handler;
630 sigemptyset(&sa.sa_mask);
631 sa.sa_flags = SA_SIGINFO | SA_RESTART;
632 sigaction(signal, &sa, NULL);
637 /* Undoably install a special low-level handler for signal; or if
638 * handler is SIG_DFL, remove any special handling for signal.
640 * The "undoably" aspect is because we also arrange with atexit() for
641 * the handler to be restored to its old value. This is for tidiness:
642 * it shouldn't matter much ordinarily, but it does remove a window
643 * where e.g. memory fault signals (SIGSEGV or SIGBUS, which in
644 * ordinary operation of SBCL are sent to the generational garbage
645 * collector, then possibly onward to Lisp code) or SIGINT (which is
646 * ordinarily passed to Lisp code) could otherwise be handled
647 * bizarrely/brokenly because the Lisp code would try to deal with
648 * them using machinery (like stream output buffers) which has already
649 * been dismantled. */
651 undoably_install_low_level_interrupt_handler (int signal,
657 struct low_level_signal_handler_state *old_low_level_signal_handler_state =
658 old_low_level_signal_handler_states + signal;
660 if (0 > signal || signal >= NSIG) {
661 lose("bad signal number %d", signal);
664 sa.sa_sigaction = handler;
665 sigemptyset(&sa.sa_mask);
666 sigaddset_blockable(&sa.sa_mask);
667 sa.sa_flags = SA_SIGINFO | SA_RESTART;
668 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
669 /* Signal handlers are run on the control stack, so if it is exhausted
670 * we had better use an alternate stack for whatever signal tells us
671 * we've exhausted it */
672 if(signal==SIG_MEMORY_FAULT) {
674 sigstack.ss_sp=(void *) ALTERNATE_SIGNAL_STACK_START;
676 sigstack.ss_size = SIGSTKSZ;
677 sigaltstack(&sigstack,0);
678 sa.sa_flags|=SA_ONSTACK;
682 /* In the case of interrupt handlers which are modified more than
683 * once, we only save the original unmodified copy. */
684 if (!old_low_level_signal_handler_state->was_modified) {
685 struct sigaction *old_handler =
686 (struct sigaction*) &old_low_level_signal_handler_state->handler;
687 old_low_level_signal_handler_state->was_modified = 1;
688 sigaction(signal, &sa, old_handler);
690 sigaction(signal, &sa, NULL);
693 interrupt_low_level_handlers[signal] =
694 (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
697 /* This is called from Lisp. */
699 install_handler(int signal, void handler(int, siginfo_t*, void*))
703 union interrupt_handler oldhandler;
705 FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
708 sigaddset(&new, signal);
709 sigprocmask(SIG_BLOCK, &new, &old);
712 sigaddset_blockable(&new);
714 FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%d\n",
715 interrupt_low_level_handlers[signal]));
716 if (interrupt_low_level_handlers[signal]==0) {
717 if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
718 ARE_SAME_HANDLER(handler, SIG_IGN)) {
719 sa.sa_sigaction = handler;
720 } else if (sigismember(&new, signal)) {
721 sa.sa_sigaction = maybe_now_maybe_later;
723 sa.sa_sigaction = interrupt_handle_now;
726 sigemptyset(&sa.sa_mask);
727 sigaddset_blockable(&sa.sa_mask);
728 sa.sa_flags = SA_SIGINFO | SA_RESTART;
730 sigaction(signal, &sa, NULL);
733 oldhandler = interrupt_handlers[signal];
734 interrupt_handlers[signal].c = handler;
736 sigprocmask(SIG_SETMASK, &old, 0);
738 FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
740 return (unsigned long)oldhandler.lisp;
748 SHOW("entering interrupt_init()");
750 /* Set up for recovery from any installed low-level handlers. */
751 atexit(&uninstall_low_level_interrupt_handlers_atexit);
753 /* Set up high level handler information. */
754 for (i = 0; i < NSIG; i++) {
755 interrupt_handlers[i].c =
756 /* (The cast here blasts away the distinction between
757 * SA_SIGACTION-style three-argument handlers and
758 * signal(..)-style one-argument handlers, which is OK
759 * because it works to call the 1-argument form where the
760 * 3-argument form is expected.) */
761 (void (*)(int, siginfo_t*, void*))SIG_DFL;
764 SHOW("returning from interrupt_init()");