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.
20 #ifdef mach /* KLUDGE: #ifdef on lowercase symbols? Ick. -- WHN 19990904 */
30 #include "interrupt.h"
40 void sigaddset_blockable(sigset_t *s)
44 sigaddset(s, SIGQUIT);
45 sigaddset(s, SIGPIPE);
46 sigaddset(s, SIGALRM);
48 sigaddset(s, SIGTSTP);
49 sigaddset(s, SIGCHLD);
51 sigaddset(s, SIGXCPU);
52 sigaddset(s, SIGXFSZ);
53 sigaddset(s, SIGVTALRM);
54 sigaddset(s, SIGPROF);
55 sigaddset(s, SIGWINCH);
56 sigaddset(s, SIGUSR1);
57 sigaddset(s, SIGUSR2);
60 /* When we catch an internal error, should we pass it back to Lisp to
61 * be handled in a high-level way? (Early in cold init, the answer is
62 * 'no', because Lisp is still too brain-dead to handle anything.
63 * After sufficient initialization has been completed, the answer
65 boolean internal_errors_enabled = 0;
67 os_context_t *lisp_interrupt_contexts[MAX_INTERRUPTS];
69 /* As far as I can tell, what's going on here is:
71 * In the case of most signals, when Lisp asks us to handle the
72 * signal, the outermost handler (the one actually passed to UNIX) is
73 * either interrupt_handle_now(..) or interrupt_handle_later(..).
74 * In that case, the Lisp-level handler is stored in interrupt_handlers[..]
75 * and interrupt_low_level_handlers[..] is cleared.
77 * However, some signals need special handling, e.g.
79 * o the SIGSEGV (for e.g. Linux) or SIGBUS (for e.g. FreeBSD) used by the
80 * garbage collector to detect violations of write protection,
81 * because some cases of such signals (e.g. GC-related violations of
82 * write protection) are handled at C level and never passed on to
83 * Lisp. For such signals, we still store any Lisp-level handler
84 * in interrupt_handlers[..], but for the outermost handle we use
85 * the value from interrupt_low_level_handlers[..], instead of the
86 * ordinary interrupt_handle_now(..) or interrupt_handle_later(..).
88 * o the SIGTRAP (Linux/Alpha) which Lisp code uses to handle breakpoints,
89 * pseudo-atomic sections, and some classes of error (e.g. "function
90 * not defined"). This never goes anywhere near the Lisp handlers at all.
91 * See runtime/alpha-arch.c and code/signal.lisp
93 * - WHN 20000728, dan 20010128 */
96 void (*interrupt_low_level_handlers[NSIG]) (int, siginfo_t*, void*) = {0};
97 union interrupt_handler interrupt_handlers[NSIG];
99 /* signal number, siginfo_t, and old mask information for pending signal
101 * pending_signal=0 when there is no pending signal. */
102 static int pending_signal = 0;
103 static siginfo_t pending_info;
104 static sigset_t pending_mask;
106 static boolean maybe_gc_pending = 0;
109 * utility routines used by various signal handlers
113 fake_foreign_function_call(os_context_t *context)
120 /* Get current Lisp state from context. */
122 dynamic_space_free_pointer =
123 (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
125 if ((long)dynamic_space_free_pointer & 1) {
126 lose("dead in fake_foreign_function_call, context = %x", context);
131 current_binding_stack_pointer =
132 (lispobj *)(*os_context_register_addr(context, reg_BSP));
136 /* Build a fake stack frame. */
137 current_control_frame_pointer =
138 (lispobj *)(*os_context_register_addr(context, reg_CSP));
139 if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
140 == current_control_frame_pointer) {
141 /* There is a small window during call where the callee's
142 * frame isn't built yet. */
143 if (LowtagOf(*os_context_register_addr(context, reg_CODE))
144 == type_FunctionPointer) {
145 /* We have called, but not built the new frame, so
146 * build it for them. */
147 current_control_frame_pointer[0] =
148 *os_context_register_addr(context, reg_OCFP);
149 current_control_frame_pointer[1] =
150 *os_context_register_addr(context, reg_LRA);
151 current_control_frame_pointer += 8;
152 /* Build our frame on top of it. */
153 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
156 /* We haven't yet called, build our frame as if the
157 * partial frame wasn't there. */
158 oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
161 /* ### We can't tell whether we are still in the caller if it had
162 * to reg_ALLOCate the stack frame due to stack arguments. */
163 /* ### Can anything strange happen during return? */
166 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
169 current_control_stack_pointer = current_control_frame_pointer + 8;
171 current_control_frame_pointer[0] = oldcont;
172 current_control_frame_pointer[1] = NIL;
173 current_control_frame_pointer[2] =
174 (lispobj)(*os_context_register_addr(context, reg_CODE));
177 /* Do dynamic binding of the active interrupt context index
178 * and save the context in the context array. */
179 context_index = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
180 /* FIXME: Ick! Why use abstract "make_fixnum" in some places if
181 * you're going to convert from fixnum by bare >>2 in other
182 * places? Use fixnum_value(..) here, and look for other places
183 * which do bare >> and << for fixnum_value and make_fixnum. */
185 if (context_index >= MAX_INTERRUPTS) {
186 lose("maximum interrupt nesting depth (%d) exceeded",
190 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
191 make_fixnum(context_index + 1));
193 lisp_interrupt_contexts[context_index] = context;
195 /* no longer in Lisp now */
196 foreign_function_call_active = 1;
200 undo_fake_foreign_function_call(os_context_t *context)
202 /* Block all blockable signals. */
205 sigaddset_blockable(&block);
206 sigprocmask(SIG_BLOCK, &block, 0);
208 /* going back into Lisp */
209 foreign_function_call_active = 0;
211 /* Undo dynamic binding. */
212 /* ### Do I really need to unbind_to_here()? */
213 /* FIXME: Is this to undo the binding of
214 * FREE_INTERRUPT_CONTEXT_INDEX? If so, we should say so. And
215 * perhaps yes, unbind_to_here() really would be clearer and less
217 /* dan (2001.08.10) thinks the above supposition is probably correct */
221 /* Put the dynamic space free pointer back into the context. */
222 *os_context_register_addr(context, reg_ALLOC) =
223 (unsigned long) dynamic_space_free_pointer;
227 /* a handler for the signal caused by execution of a trap opcode
228 * signalling an internal error */
230 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
233 lispobj context_sap = 0;
235 fake_foreign_function_call(context);
237 /* Allocate the SAP object while the interrupts are still
239 if (internal_errors_enabled) {
240 context_sap = alloc_sap(context);
243 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
245 if (internal_errors_enabled) {
246 SHOW("in interrupt_internal_error");
248 /* Display some rudimentary debugging information about the
249 * error, so that even if the Lisp error handler gets badly
250 * confused, we have a chance to determine what's going on. */
251 describe_internal_error(context);
253 funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
254 continuable ? T : NIL);
256 describe_internal_error(context);
257 /* There's no good way to recover from an internal error
258 * before the Lisp error handling mechanism is set up. */
259 lose("internal error too early in init, can't recover");
261 undo_fake_foreign_function_call(context);
263 arch_skip_instruction(context);
267 /* This function handles pending interrupts. Note that in C/kernel
268 * terms we dealt with the signal already; we just haven't decided
269 * whether to call a Lisp handler or do a GC or something like that.
270 * If it helps, you can think of pending_{signal,mask,info} as a
271 * one-element queue of signals that we have acknowledged but not
275 interrupt_handle_pending(os_context_t *context)
278 boolean were_in_lisp = !foreign_function_call_active;
281 SetSymbolValue(INTERRUPT_PENDING, NIL);
283 if (maybe_gc_pending) {
284 maybe_gc_pending = 0;
289 fake_foreign_function_call(context);
291 funcall0(SymbolFunction(MAYBE_GC));
296 undo_fake_foreign_function_call(context);
300 /* FIXME: This isn't very clear. It would be good to reverse
301 * engineer it and rewrite the code more clearly, or write a clear
302 * explanation of what's going on in the comments, or both.
304 * WHN's question 1a: How come we unconditionally copy from
305 * pending_mask into the context, and then test whether
306 * pending_signal is set?
308 * WHN's question 1b: If pending_signal wasn't set, how could
309 * pending_mask be valid?
311 * Dan Barlow's reply (sbcl-devel 2001-03-13): And the answer is -
312 * or appears to be - because interrupt_maybe_gc set it that way
313 * (look in the #ifndef __i386__ bit). We can't GC during a
314 * pseudo-atomic, so we set maybe_gc_pending=1 and
315 * arch_set_pseudo_atomic_interrupted(..) When we come out of
316 * pseudo_atomic we're marked as interrupted, so we call
317 * interrupt_handle_pending, which does the GC using the pending
318 * context (it needs a context so that it has registers to use as
319 * GC roots) then notices there's no actual interrupt handler to
320 * call, so doesn't. That's the second question [1b] answered,
321 * anyway. Why we still need to copy the pending_mask into the
322 * context given that we're now done with the context anyway, I
324 memcpy(os_context_sigmask_addr(context), &pending_mask, sizeof(sigset_t));
325 sigemptyset(&pending_mask);
326 if (pending_signal) {
327 int signal = pending_signal;
329 memcpy(&info, &pending_info, sizeof(siginfo_t));
331 interrupt_handle_now(signal, &info, context);
336 * the two main signal handlers:
337 * interrupt_handle_now(..)
338 * maybe_now_maybe_later(..)
342 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
344 os_context_t *context = (os_context_t*)void_context;
346 boolean were_in_lisp;
348 union interrupt_handler handler;
350 /* FIXME: The CMU CL we forked off of had this Linux-only
351 * operation here. Newer CMU CLs (e.g. 18c) have hairier
352 * Linux/i386-only logic here. SBCL seems to be more reliable
353 * without anything here. However, if we start supporting code
354 * which sets the rounding mode, then we may want to do something
355 * special to force the rounding mode back to some standard value
356 * here, so that ISRs can have a standard environment. (OTOH, if
357 * rounding modes are under user control, then perhaps we should
358 * leave this up to the user.)
360 * In the absence of a test case to show that this is really a
361 * problem, we just suppress this code completely (just like the
362 * parallel code in maybe_now_maybe_later).
364 * SET_FPU_CONTROL_WORD(context->__fpregs_mem.cw);
367 handler = interrupt_handlers[signal];
369 if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
374 were_in_lisp = !foreign_function_call_active;
378 fake_foreign_function_call(context);
383 "/entering interrupt_handle_now(%d, info, context)\n",
387 if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
389 /* This can happen if someone tries to ignore or default one
390 * of the signals we need for runtime support, and the runtime
391 * support decides to pass on it. */
392 lose("no handler for signal %d in interrupt_handle_now(..)", signal);
394 } else if (LowtagOf(handler.lisp) == type_FunctionPointer) {
396 /* Allocate the SAPs while the interrupts are still disabled.
397 * (FIXME: Why? This is the way it was done in CMU CL, and it
398 * even had the comment noting that this is the way it was
399 * done, but no motivation..) */
400 lispobj info_sap,context_sap = alloc_sap(context);
401 info_sap = alloc_sap(info);
402 /* Allow signals again. */
403 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
406 SHOW("calling Lisp-level handler");
409 funcall3(handler.lisp,
416 SHOW("calling C-level handler");
419 /* Allow signals again. */
420 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
422 (*handler.c)(signal, info, void_context);
429 undo_fake_foreign_function_call(context);
434 "/returning from interrupt_handle_now(%d, info, context)\n",
440 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
442 os_context_t *context = (os_context_t*)void_context;
444 /* FIXME: See Debian cmucl 2.4.17, and mail from DTC on the CMU CL
445 * mailing list 23 Oct 1999, for changes in FPU handling at
446 * interrupt time which should be ported into SBCL. Also see the
447 * analogous logic at the head of interrupt_handle_now for
448 * more related FIXME stuff.
450 * For now, we just suppress this code completely.
452 * SET_FPU_CONTROL_WORD(context->__fpregs_mem.cw);
455 /* see comments at top of code/signal.lisp for what's going on here
456 * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW
458 if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
460 /* FIXME: This code is exactly the same as the code in the
461 * other leg of the if(..), and should be factored out into
462 * a shared function. */
463 pending_signal = signal;
464 memcpy(&pending_info, info, sizeof(siginfo_t));
465 memcpy(&pending_mask,
466 os_context_sigmask_addr(context),
468 sigaddset_blockable(os_context_sigmask_addr(context));
469 SetSymbolValue(INTERRUPT_PENDING, T);
473 (!foreign_function_call_active) &&
475 arch_pseudo_atomic_atomic(context)) {
477 /* FIXME: It would probably be good to replace these bare
478 * memcpy(..) calls with calls to cpy_siginfo_t and
479 * cpy_sigset_t, so that we only have to get the sizeof
480 * expressions right in one place, and after that static type
481 * checking takes over. */
482 pending_signal = signal;
483 memcpy(&pending_info, info, sizeof(siginfo_t));
484 memcpy(&pending_mask,
485 os_context_sigmask_addr(context),
487 sigaddset_blockable(os_context_sigmask_addr(context));
489 arch_set_pseudo_atomic_interrupted(context);
492 interrupt_handle_now(signal, info, context);
497 * stuff to detect and handle hitting the GC trigger
500 #ifndef INTERNAL_GC_TRIGGER
502 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
504 if (current_auto_gc_trigger == NULL)
507 lispobj *badaddr=(lispobj *)arch_get_bad_addr(signal,
511 return (badaddr >= current_auto_gc_trigger &&
512 badaddr < current_dynamic_space + DYNAMIC_SPACE_SIZE);
518 /* This function gets called from the SIGSEGV (for e.g. Linux or
519 * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
520 * whether the signal was due to treading on the mprotect()ed zone -
521 * and if so, arrange for a GC to happen. */
523 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
525 os_context_t *context=(os_context_t *) void_context;
527 if (!foreign_function_call_active
528 #ifndef INTERNAL_GC_TRIGGER
529 && gc_trigger_hit(signal, info, context)
532 #ifndef INTERNAL_GC_TRIGGER
533 clear_auto_gc_trigger();
536 if (arch_pseudo_atomic_atomic(context)) {
537 /* don't GC during an atomic operation. Instead, copy the
538 * signal mask somewhere safe. interrupt_handle_pending
539 * will detect pending_signal==0 and know to do a GC with the
540 * signal context instead of calling a Lisp-level handler */
541 maybe_gc_pending = 1;
542 if (pending_signal == 0) {
543 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
544 * idiom occurs over and over. It should be factored out
545 * into a function with a descriptive name. */
546 memcpy(&pending_mask,
547 os_context_sigmask_addr(context),
549 sigaddset_blockable(os_context_sigmask_addr(context));
551 arch_set_pseudo_atomic_interrupted(context);
554 fake_foreign_function_call(context);
555 funcall0(SymbolFunction(MAYBE_GC));
556 undo_fake_foreign_function_call(context);
567 * noise to install handlers
571 * what low-level signal handlers looked like before
572 * undoably_install_low_level_interrupt_handler() got involved
574 struct low_level_signal_handler_state {
576 void (*handler)(int, siginfo_t*, void*);
577 } old_low_level_signal_handler_states[NSIG];
580 uninstall_low_level_interrupt_handlers_atexit(void)
583 for (signal = 0; signal < NSIG; ++signal) {
584 struct low_level_signal_handler_state
585 *old_low_level_signal_handler_state =
586 old_low_level_signal_handler_states + signal;
587 if (old_low_level_signal_handler_state->was_modified) {
589 sa.sa_sigaction = old_low_level_signal_handler_state->handler;
590 sigemptyset(&sa.sa_mask);
591 sa.sa_flags = SA_SIGINFO | SA_RESTART;
592 sigaction(signal, &sa, NULL);
597 /* Undoably install a special low-level handler for signal; or if
598 * handler is SIG_DFL, remove any special handling for signal.
600 * The "undoably" aspect is because we also arrange with atexit() for
601 * the handler to be restored to its old value. This is for tidiness:
602 * it shouldn't matter much ordinarily, but it does remove a window
603 * where e.g. memory fault signals (SIGSEGV or SIGBUS, which in
604 * ordinary operation of SBCL are sent to the generational garbage
605 * collector, then possibly onward to Lisp code) or SIGINT (which is
606 * ordinarily passed to Lisp code) could otherwise be handled
607 * bizarrely/brokenly because the Lisp code would try to deal with
608 * them using machinery (like stream output buffers) which has already
609 * been dismantled. */
611 undoably_install_low_level_interrupt_handler (int signal,
617 struct low_level_signal_handler_state *old_low_level_signal_handler_state =
618 old_low_level_signal_handler_states + signal;
620 if (0 > signal || signal >= NSIG) {
621 lose("bad signal number %d", signal);
624 sa.sa_sigaction = handler;
625 sigemptyset(&sa.sa_mask);
626 sigaddset_blockable(&sa.sa_mask);
627 sa.sa_flags = SA_SIGINFO | SA_RESTART;
629 /* In the case of interrupt handlers which are modified more than
630 * once, we only save the original unmodified copy. */
631 if (!old_low_level_signal_handler_state->was_modified) {
632 struct sigaction *old_handler =
633 (struct sigaction*) &old_low_level_signal_handler_state->handler;
634 old_low_level_signal_handler_state->was_modified = 1;
635 sigaction(signal, &sa, old_handler);
637 sigaction(signal, &sa, NULL);
640 interrupt_low_level_handlers[signal] =
641 (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
644 /* This is called from Lisp. */
646 install_handler(int signal, void handler(int, siginfo_t*, void*))
650 union interrupt_handler oldhandler;
652 FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
655 sigaddset(&new, signal);
656 sigprocmask(SIG_BLOCK, &new, &old);
659 sigaddset_blockable(&new);
661 FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%d\n",
662 interrupt_low_level_handlers[signal]));
663 if (interrupt_low_level_handlers[signal]==0) {
664 if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
665 ARE_SAME_HANDLER(handler, SIG_IGN)) {
666 sa.sa_sigaction = handler;
667 } else if (sigismember(&new, signal)) {
668 sa.sa_sigaction = maybe_now_maybe_later;
670 sa.sa_sigaction = interrupt_handle_now;
673 sigemptyset(&sa.sa_mask);
674 sigaddset_blockable(&sa.sa_mask);
675 sa.sa_flags = SA_SIGINFO | SA_RESTART;
677 sigaction(signal, &sa, NULL);
680 oldhandler = interrupt_handlers[signal];
681 interrupt_handlers[signal].c = handler;
683 sigprocmask(SIG_SETMASK, &old, 0);
685 FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
687 return (unsigned long)oldhandler.lisp;
695 SHOW("entering interrupt_init()");
697 /* Set up for recovery from any installed low-level handlers. */
698 atexit(&uninstall_low_level_interrupt_handlers_atexit);
700 /* Set up high level handler information. */
701 for (i = 0; i < NSIG; i++) {
702 interrupt_handlers[i].c =
703 /* (The cast here blasts away the distinction between
704 * SA_SIGACTION-style three-argument handlers and
705 * signal(..)-style one-argument handlers, which is OK
706 * because it works to call the 1-argument form where the
707 * 3-argument form is expected.) */
708 (void (*)(int, siginfo_t*, void*))SIG_DFL;
711 SHOW("returning from interrupt_init()");