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"
34 #include "genesis/fdefn.h"
35 #include "genesis/simple-fun.h"
37 void sigaddset_blockable(sigset_t *s)
41 sigaddset(s, SIGQUIT);
42 sigaddset(s, SIGPIPE);
43 sigaddset(s, SIGALRM);
46 sigaddset(s, SIGTSTP);
47 sigaddset(s, SIGCHLD);
49 sigaddset(s, SIGXCPU);
50 sigaddset(s, SIGXFSZ);
51 sigaddset(s, SIGVTALRM);
52 sigaddset(s, SIGPROF);
53 sigaddset(s, SIGWINCH);
54 sigaddset(s, SIGUSR1);
55 sigaddset(s, SIGUSR2);
58 /* When we catch an internal error, should we pass it back to Lisp to
59 * be handled in a high-level way? (Early in cold init, the answer is
60 * 'no', because Lisp is still too brain-dead to handle anything.
61 * After sufficient initialization has been completed, the answer
63 boolean internal_errors_enabled = 0;
65 struct interrupt_data * global_interrupt_data;
67 /* As far as I can tell, what's going on here is:
69 * In the case of most signals, when Lisp asks us to handle the
70 * signal, the outermost handler (the one actually passed to UNIX) is
71 * either interrupt_handle_now(..) or interrupt_handle_later(..).
72 * In that case, the Lisp-level handler is stored in interrupt_handlers[..]
73 * and interrupt_low_level_handlers[..] is cleared.
75 * However, some signals need special handling, e.g.
77 * o the SIGSEGV (for e.g. Linux) or SIGBUS (for e.g. FreeBSD) used by the
78 * garbage collector to detect violations of write protection,
79 * because some cases of such signals (e.g. GC-related violations of
80 * write protection) are handled at C level and never passed on to
81 * Lisp. For such signals, we still store any Lisp-level handler
82 * in interrupt_handlers[..], but for the outermost handle we use
83 * the value from interrupt_low_level_handlers[..], instead of the
84 * ordinary interrupt_handle_now(..) or interrupt_handle_later(..).
86 * o the SIGTRAP (Linux/Alpha) which Lisp code uses to handle breakpoints,
87 * pseudo-atomic sections, and some classes of error (e.g. "function
88 * not defined"). This never goes anywhere near the Lisp handlers at all.
89 * See runtime/alpha-arch.c and code/signal.lisp
91 * - WHN 20000728, dan 20010128 */
94 boolean maybe_gc_pending = 0;
97 * utility routines used by various signal handlers
101 build_fake_control_stack_frames(struct thread *th,os_context_t *context)
103 #ifndef LISP_FEATURE_X86
107 /* Build a fake stack frame or frames */
109 current_control_frame_pointer =
110 (lispobj *)(*os_context_register_addr(context, reg_CSP));
111 if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
112 == current_control_frame_pointer) {
113 /* There is a small window during call where the callee's
114 * frame isn't built yet. */
115 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
116 == FUN_POINTER_LOWTAG) {
117 /* We have called, but not built the new frame, so
118 * build it for them. */
119 current_control_frame_pointer[0] =
120 *os_context_register_addr(context, reg_OCFP);
121 current_control_frame_pointer[1] =
122 *os_context_register_addr(context, reg_LRA);
123 current_control_frame_pointer += 8;
124 /* Build our frame on top of it. */
125 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
128 /* We haven't yet called, build our frame as if the
129 * partial frame wasn't there. */
130 oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
133 /* We can't tell whether we are still in the caller if it had to
134 * allocate a stack frame due to stack arguments. */
135 /* This observation provoked some past CMUCL maintainer to ask
136 * "Can anything strange happen during return?" */
139 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
142 current_control_stack_pointer = current_control_frame_pointer + 8;
144 current_control_frame_pointer[0] = oldcont;
145 current_control_frame_pointer[1] = NIL;
146 current_control_frame_pointer[2] =
147 (lispobj)(*os_context_register_addr(context, reg_CODE));
152 fake_foreign_function_call(os_context_t *context)
155 struct thread *thread=arch_os_get_current_thread();
157 /* Get current Lisp state from context. */
159 dynamic_space_free_pointer =
160 (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
162 if ((long)dynamic_space_free_pointer & 1) {
163 lose("dead in fake_foreign_function_call, context = %x", context);
168 current_binding_stack_pointer =
169 (lispobj *)(*os_context_register_addr(context, reg_BSP));
172 build_fake_control_stack_frames(thread,context);
174 /* Do dynamic binding of the active interrupt context index
175 * and save the context in the context array. */
177 fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,thread));
179 if (context_index >= MAX_INTERRUPTS) {
180 lose("maximum interrupt nesting depth (%d) exceeded", MAX_INTERRUPTS);
183 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
184 make_fixnum(context_index + 1),thread);
186 thread->interrupt_contexts[context_index] = context;
188 /* no longer in Lisp now */
189 foreign_function_call_active = 1;
193 undo_fake_foreign_function_call(os_context_t *context)
195 struct thread *thread=arch_os_get_current_thread();
196 /* Block all blockable signals. */
199 sigaddset_blockable(&block);
200 sigprocmask(SIG_BLOCK, &block, 0);
202 /* going back into Lisp */
203 foreign_function_call_active = 0;
205 /* Undo dynamic binding. */
206 /* ### Do I really need to unbind_to_here()? */
207 /* FIXME: Is this to undo the binding of
208 * FREE_INTERRUPT_CONTEXT_INDEX? If so, we should say so. And
209 * perhaps yes, unbind_to_here() really would be clearer and less
211 /* dan (2001.08.10) thinks the above supposition is probably correct */
215 /* Put the dynamic space free pointer back into the context. */
216 *os_context_register_addr(context, reg_ALLOC) =
217 (unsigned long) dynamic_space_free_pointer;
221 /* a handler for the signal caused by execution of a trap opcode
222 * signalling an internal error */
224 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
227 lispobj context_sap = 0;
229 fake_foreign_function_call(context);
231 /* Allocate the SAP object while the interrupts are still
233 if (internal_errors_enabled) {
234 context_sap = alloc_sap(context);
237 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
239 if (internal_errors_enabled) {
240 SHOW("in interrupt_internal_error");
242 /* Display some rudimentary debugging information about the
243 * error, so that even if the Lisp error handler gets badly
244 * confused, we have a chance to determine what's going on. */
245 describe_internal_error(context);
247 funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
248 continuable ? T : NIL);
250 describe_internal_error(context);
251 /* There's no good way to recover from an internal error
252 * before the Lisp error handling mechanism is set up. */
253 lose("internal error too early in init, can't recover");
255 undo_fake_foreign_function_call(context);
257 arch_skip_instruction(context);
261 /* This function handles pending interrupts. Note that in C/kernel
262 * terms we dealt with the signal already; we just haven't decided
263 * whether to call a Lisp handler or do a GC or something like that.
264 * If it helps, you can think of pending_{signal,mask,info} as a
265 * one-element queue of signals that we have acknowledged but not
269 interrupt_handle_pending(os_context_t *context)
271 struct thread *thread;
272 struct interrupt_data *data;
275 boolean were_in_lisp = !foreign_function_call_active;
277 #ifdef LISP_FEATURE_SB_THREAD
278 while(stop_the_world) kill(getpid(),SIGSTOP);
280 thread=arch_os_get_current_thread();
281 data=thread->interrupt_data;
282 SetSymbolValue(INTERRUPT_PENDING, NIL,thread);
284 if (maybe_gc_pending) {
289 fake_foreign_function_call(context);
291 funcall0(SymbolFunction(SUB_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
325 memcpy(os_context_sigmask_addr(context), &pending_mask,
326 4 /* sizeof(sigset_t) */ );
328 sigemptyset(&data->pending_mask);
329 if (data->pending_signal) {
330 int signal = data->pending_signal;
332 memcpy(&info, &data->pending_info, sizeof(siginfo_t));
333 data->pending_signal = 0;
334 interrupt_handle_now(signal, &info, context);
339 * the two main signal handlers:
340 * interrupt_handle_now(..)
341 * maybe_now_maybe_later(..)
343 * to which we have added interrupt_handle_now_handler(..). Why?
344 * Well, mostly because the SPARC/Linux platform doesn't quite do
345 * signals the way we want them done. The third argument in the
346 * handler isn't filled in by the kernel properly, so we fix it up
347 * ourselves in the arch_os_get_context(..) function; however, we only
348 * want to do this when we first hit the handler, and not when
349 * interrupt_handle_now(..) is being called from some other handler
350 * (when the fixup will already have been done). -- CSR, 2002-07-23
354 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
356 os_context_t *context = (os_context_t*)void_context;
357 struct thread *thread=arch_os_get_current_thread();
359 boolean were_in_lisp;
361 union interrupt_handler handler;
363 #ifdef LISP_FEATURE_LINUX
364 /* Under Linux on some architectures, we appear to have to restore
365 the FPU control word from the context, as after the signal is
366 delivered we appear to have a null FPU control word. */
367 os_restore_fp_control(context);
369 handler = thread->interrupt_data->interrupt_handlers[signal];
371 if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
376 were_in_lisp = !foreign_function_call_active;
380 fake_foreign_function_call(context);
385 "/entering interrupt_handle_now(%d, info, context)\n",
389 if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
391 /* This can happen if someone tries to ignore or default one
392 * of the signals we need for runtime support, and the runtime
393 * support decides to pass on it. */
394 lose("no handler for signal %d in interrupt_handle_now(..)", signal);
396 } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
398 /* Allocate the SAPs while the interrupts are still disabled.
399 * (FIXME: Why? This is the way it was done in CMU CL, and it
400 * even had the comment noting that this is the way it was
401 * done, but no motivation..) */
402 lispobj info_sap,context_sap = alloc_sap(context);
403 info_sap = alloc_sap(info);
404 /* Allow signals again. */
405 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
408 SHOW("calling Lisp-level handler");
411 funcall3(handler.lisp,
418 SHOW("calling C-level handler");
421 /* Allow signals again. */
422 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
424 (*handler.c)(signal, info, void_context);
431 undo_fake_foreign_function_call(context);
436 "/returning from interrupt_handle_now(%d, info, context)\n",
442 store_signal_data_for_later (struct interrupt_data *data, int signal,
443 siginfo_t *info, os_context_t *context)
445 data->pending_signal = signal;
446 memcpy(&(data->pending_info), info, sizeof(siginfo_t));
447 memcpy(&(data->pending_mask),
448 os_context_sigmask_addr(context),
450 sigaddset_blockable(os_context_sigmask_addr(context));
455 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
457 os_context_t *context = arch_os_get_context(&void_context);
458 struct thread *thread=arch_os_get_current_thread();
459 struct interrupt_data *data=thread->interrupt_data;
460 #ifdef LISP_FEATURE_LINUX
461 os_restore_fp_control(context);
463 /* see comments at top of code/signal.lisp for what's going on here
464 * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW
466 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL) {
467 store_signal_data_for_later(data,signal,info,context);
468 SetSymbolValue(INTERRUPT_PENDING, T,thread);
471 (!foreign_function_call_active) &&
473 arch_pseudo_atomic_atomic(context)) {
474 store_signal_data_for_later(data,signal,info,context);
475 arch_set_pseudo_atomic_interrupted(context);
477 interrupt_handle_now(signal, info, context);
483 interrupt_handle_now_handler(int signal, siginfo_t *info, void *void_context)
485 os_context_t *context = arch_os_get_context(&void_context);
486 interrupt_handle_now(signal, info, context);
490 * stuff to detect and handle hitting the GC trigger
493 #ifndef LISP_FEATURE_GENCGC
494 /* since GENCGC has its own way to record trigger */
496 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
498 if (current_auto_gc_trigger == NULL)
501 void *badaddr=arch_get_bad_addr(signal,info,context);
502 return (badaddr >= (void *)current_auto_gc_trigger &&
503 badaddr <((void *)current_dynamic_space + DYNAMIC_SPACE_SIZE));
508 /* and similarly for the control stack guard page */
510 boolean handle_control_stack_guard_triggered(os_context_t *context,void *addr)
512 struct thread *th=arch_os_get_current_thread();
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>=(void *)CONTROL_STACK_GUARD_PAGE(th) &&
516 addr<(void *)(CONTROL_STACK_GUARD_PAGE(th)+os_vm_page_size)) {
519 /* fprintf(stderr, "hit end of control stack\n"); */
520 /* we hit the end of the control stack. disable protection
521 * temporarily so the error handler has some headroom */
522 protect_control_stack_guard_page(th->pid,0L);
525 native_pointer((lispobj) SymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
526 code = &(((struct simple_fun *) fun)->code);
528 /* Build a stack frame showing `interrupted' so that the
529 * user's backtrace makes (as much) sense (as usual) */
530 build_fake_control_stack_frames(th,context);
531 /* signal handler will "return" to this error-causing function */
532 *os_context_pc_addr(context) = code;
533 #ifdef LISP_FEATURE_X86
534 *os_context_register_addr(context,reg_ECX) = 0;
536 /* this much of the calling convention is common to all
538 *os_context_register_addr(context,reg_NARGS) = 0;
539 *os_context_register_addr(context,reg_LIP) = code;
540 *os_context_register_addr(context,reg_CFP) =
541 current_control_frame_pointer;
543 #ifdef ARCH_HAS_NPC_REGISTER
544 *os_context_npc_addr(context) =
545 4 + *os_context_pc_addr(context);
547 #ifdef LISP_FEATURE_SPARC
548 /* Bletch. This is a feature of the SPARC calling convention,
549 which sadly I'm not going to go into in large detail here,
550 as I don't know it well enough. Suffice to say that if the
553 (INST MOVE CODE-TN FUNCTION)
555 in compiler/sparc/call.lisp is changed, then this bit can
556 probably go away. -- CSR, 2002-07-24 */
557 *os_context_register_addr(context,reg_CODE) =
558 fun + FUN_POINTER_LOWTAG;
565 #ifndef LISP_FEATURE_X86
566 /* This function gets called from the SIGSEGV (for e.g. Linux or
567 * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
568 * whether the signal was due to treading on the mprotect()ed zone -
569 * and if so, arrange for a GC to happen. */
571 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
573 os_context_t *context=(os_context_t *) void_context;
574 struct thread *th=arch_os_get_current_thread();
575 struct interrupt_data *data=
576 th ? th->interrupt_data : global_interrupt_data;
578 if (!foreign_function_call_active
579 #ifndef LISP_FEATURE_GENCGC
580 /* nb: GENCGC on non-x86? I really don't think so. This
581 * happens every time */
582 && gc_trigger_hit(signal, info, context)
585 #ifndef LISP_FEATURE_GENCGC
586 clear_auto_gc_trigger();
589 if (arch_pseudo_atomic_atomic(context)) {
590 /* don't GC during an atomic operation. Instead, copy the
591 * signal mask somewhere safe. interrupt_handle_pending
592 * will detect pending_signal==0 and know to do a GC with the
593 * signal context instead of calling a Lisp-level handler */
594 maybe_gc_pending = 1;
595 if (data->pending_signal == 0) {
596 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
597 * idiom occurs over and over. It should be factored out
598 * into a function with a descriptive name. */
599 memcpy(&(data->pending_mask),
600 os_context_sigmask_addr(context),
602 sigaddset_blockable(os_context_sigmask_addr(context));
604 arch_set_pseudo_atomic_interrupted(context);
607 lispobj *old_free_space=current_dynamic_space;
608 fake_foreign_function_call(context);
609 funcall0(SymbolFunction(SUB_GC));
610 undo_fake_foreign_function_call(context);
611 if(current_dynamic_space==old_free_space)
612 /* MAYBE-GC (as the name suggest) might not. If it
613 * doesn't, it won't reset the GC trigger either, so we
614 * have to do it ourselves. Put it near the end of
615 * dynamic space so we're not running into it continually
617 set_auto_gc_trigger(DYNAMIC_SPACE_SIZE
618 -(u32)os_vm_page_size);
628 * noise to install handlers
631 /* SBCL used to have code to restore signal handlers on exit, which
632 * has been removed from the threaded version until we decide: exit of
635 /* SBCL comment: The "undoably" aspect is because we also arrange with
636 * atexit() for the handler to be restored to its old value. This is
637 * for tidiness: it shouldn't matter much ordinarily, but it does
638 * remove a window where e.g. memory fault signals (SIGSEGV or SIGBUS,
639 * which in ordinary operation of SBCL are sent to the generational
640 * garbage collector, then possibly onward to Lisp code) or SIGINT
641 * (which is ordinarily passed to Lisp code) could otherwise be
642 * handled bizarrely/brokenly because the Lisp code would try to deal
643 * with them using machinery (like stream output buffers) which has
644 * already been dismantled. */
646 /* I'm not sure (a) whether this is a real concern, (b) how it helps
650 uninstall_low_level_interrupt_handlers_atexit(void)
655 undoably_install_low_level_interrupt_handler (int signal,
661 struct thread *th=arch_os_get_current_thread();
662 struct interrupt_data *data=
663 th ? th->interrupt_data : global_interrupt_data;
665 if (0 > signal || signal >= NSIG) {
666 lose("bad signal number %d", signal);
669 sa.sa_sigaction = handler;
670 sigemptyset(&sa.sa_mask);
671 sigaddset_blockable(&sa.sa_mask);
672 sa.sa_flags = SA_SIGINFO | SA_RESTART;
673 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
674 if(signal==SIG_MEMORY_FAULT) sa.sa_flags|= SA_ONSTACK;
677 sigaction(signal, &sa, NULL);
678 data->interrupt_low_level_handlers[signal] =
679 (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
682 /* This is called from Lisp. */
684 install_handler(int signal, void handler(int, siginfo_t*, void*))
688 union interrupt_handler oldhandler;
689 struct thread *th=arch_os_get_current_thread();
690 struct interrupt_data *data=
691 th ? th->interrupt_data : global_interrupt_data;
693 FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
696 sigaddset(&new, signal);
697 sigprocmask(SIG_BLOCK, &new, &old);
700 sigaddset_blockable(&new);
702 FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%d\n",
703 interrupt_low_level_handlers[signal]));
704 if (data->interrupt_low_level_handlers[signal]==0) {
705 if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
706 ARE_SAME_HANDLER(handler, SIG_IGN)) {
707 sa.sa_sigaction = handler;
708 } else if (sigismember(&new, signal)) {
709 sa.sa_sigaction = maybe_now_maybe_later;
711 sa.sa_sigaction = interrupt_handle_now_handler;
714 sigemptyset(&sa.sa_mask);
715 sigaddset_blockable(&sa.sa_mask);
716 sa.sa_flags = SA_SIGINFO | SA_RESTART;
717 sigaction(signal, &sa, NULL);
720 oldhandler = data->interrupt_handlers[signal];
721 data->interrupt_handlers[signal].c = handler;
723 sigprocmask(SIG_SETMASK, &old, 0);
725 FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
727 return (unsigned long)oldhandler.lisp;
734 SHOW("entering interrupt_init()");
735 global_interrupt_data=calloc(sizeof(struct interrupt_data), 1);
737 /* Set up high level handler information. */
738 for (i = 0; i < NSIG; i++) {
739 global_interrupt_data->interrupt_handlers[i].c =
740 /* (The cast here blasts away the distinction between
741 * SA_SIGACTION-style three-argument handlers and
742 * signal(..)-style one-argument handlers, which is OK
743 * because it works to call the 1-argument form where the
744 * 3-argument form is expected.) */
745 (void (*)(int, siginfo_t*, void*))SIG_DFL;
748 SHOW("returning from interrupt_init()");