0.7.5.16:
[sbcl/lichteblau.git] / src / runtime / interrupt.c
blob68c5fe06ca765ed31790a2991e5663e1e8b620c9
1 /*
2 * interrupt-handling magic
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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.
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <signal.h>
21 #include "runtime.h"
22 #include "arch.h"
23 #include "sbcl.h"
24 #include "os.h"
25 #include "interrupt.h"
26 #include "globals.h"
27 #include "lispregs.h"
28 #include "validate.h"
29 #include "monitor.h"
30 #include "gc.h"
31 #include "alloc.h"
32 #include "dynbind.h"
33 #include "interr.h"
36 void sigaddset_blockable(sigset_t *s)
38 sigaddset(s, SIGHUP);
39 sigaddset(s, SIGINT);
40 sigaddset(s, SIGQUIT);
41 sigaddset(s, SIGPIPE);
42 sigaddset(s, SIGALRM);
43 sigaddset(s, SIGURG);
44 sigaddset(s, SIGFPE);
45 sigaddset(s, SIGTSTP);
46 sigaddset(s, SIGCHLD);
47 sigaddset(s, SIGIO);
48 sigaddset(s, SIGXCPU);
49 sigaddset(s, SIGXFSZ);
50 sigaddset(s, SIGVTALRM);
51 sigaddset(s, SIGPROF);
52 sigaddset(s, SIGWINCH);
53 sigaddset(s, SIGUSR1);
54 sigaddset(s, SIGUSR2);
57 /* When we catch an internal error, should we pass it back to Lisp to
58 * be handled in a high-level way? (Early in cold init, the answer is
59 * 'no', because Lisp is still too brain-dead to handle anything.
60 * After sufficient initialization has been completed, the answer
61 * becomes 'yes'.) */
62 boolean internal_errors_enabled = 0;
64 os_context_t *lisp_interrupt_contexts[MAX_INTERRUPTS];
66 /* As far as I can tell, what's going on here is:
68 * In the case of most signals, when Lisp asks us to handle the
69 * signal, the outermost handler (the one actually passed to UNIX) is
70 * either interrupt_handle_now(..) or interrupt_handle_later(..).
71 * In that case, the Lisp-level handler is stored in interrupt_handlers[..]
72 * and interrupt_low_level_handlers[..] is cleared.
74 * However, some signals need special handling, e.g.
76 * o the SIGSEGV (for e.g. Linux) or SIGBUS (for e.g. FreeBSD) used by the
77 * garbage collector to detect violations of write protection,
78 * because some cases of such signals (e.g. GC-related violations of
79 * write protection) are handled at C level and never passed on to
80 * Lisp. For such signals, we still store any Lisp-level handler
81 * in interrupt_handlers[..], but for the outermost handle we use
82 * the value from interrupt_low_level_handlers[..], instead of the
83 * ordinary interrupt_handle_now(..) or interrupt_handle_later(..).
85 * o the SIGTRAP (Linux/Alpha) which Lisp code uses to handle breakpoints,
86 * pseudo-atomic sections, and some classes of error (e.g. "function
87 * not defined"). This never goes anywhere near the Lisp handlers at all.
88 * See runtime/alpha-arch.c and code/signal.lisp
90 * - WHN 20000728, dan 20010128 */
93 void (*interrupt_low_level_handlers[NSIG]) (int, siginfo_t*, void*) = {0};
94 union interrupt_handler interrupt_handlers[NSIG];
96 /* signal number, siginfo_t, and old mask information for pending signal
98 * pending_signal=0 when there is no pending signal. */
99 static int pending_signal = 0;
100 static siginfo_t pending_info;
101 static sigset_t pending_mask;
103 static boolean maybe_gc_pending = 0;
106 * utility routines used by various signal handlers
109 void
110 fake_foreign_function_call(os_context_t *context)
112 int context_index;
113 #ifndef __i386__
114 lispobj oldcont;
115 #endif
117 /* Get current Lisp state from context. */
118 #ifdef reg_ALLOC
119 dynamic_space_free_pointer =
120 (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
121 #ifdef alpha
122 if ((long)dynamic_space_free_pointer & 1) {
123 lose("dead in fake_foreign_function_call, context = %x", context);
125 #endif
126 #endif
127 #ifdef reg_BSP
128 current_binding_stack_pointer =
129 (lispobj *)(*os_context_register_addr(context, reg_BSP));
130 #endif
132 #ifndef __i386__
133 /* Build a fake stack frame. */
134 current_control_frame_pointer =
135 (lispobj *)(*os_context_register_addr(context, reg_CSP));
136 if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
137 == current_control_frame_pointer) {
138 /* There is a small window during call where the callee's
139 * frame isn't built yet. */
140 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
141 == FUN_POINTER_LOWTAG) {
142 /* We have called, but not built the new frame, so
143 * build it for them. */
144 current_control_frame_pointer[0] =
145 *os_context_register_addr(context, reg_OCFP);
146 current_control_frame_pointer[1] =
147 *os_context_register_addr(context, reg_LRA);
148 current_control_frame_pointer += 8;
149 /* Build our frame on top of it. */
150 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
152 else {
153 /* We haven't yet called, build our frame as if the
154 * partial frame wasn't there. */
155 oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
158 /* ### We can't tell whether we are still in the caller if it had
159 * to reg_ALLOCate the stack frame due to stack arguments. */
160 /* ### Can anything strange happen during return? */
161 else {
162 /* normal case */
163 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
166 current_control_stack_pointer = current_control_frame_pointer + 8;
168 current_control_frame_pointer[0] = oldcont;
169 current_control_frame_pointer[1] = NIL;
170 current_control_frame_pointer[2] =
171 (lispobj)(*os_context_register_addr(context, reg_CODE));
172 #endif
174 /* Do dynamic binding of the active interrupt context index
175 * and save the context in the context array. */
176 context_index = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
177 /* FIXME: Ick! Why use abstract "make_fixnum" in some places if
178 * you're going to convert from fixnum by bare >>2 in other
179 * places? Use fixnum_value(..) here, and look for other places
180 * which do bare >> and << for fixnum_value and make_fixnum. */
182 if (context_index >= MAX_INTERRUPTS) {
183 lose("maximum interrupt nesting depth (%d) exceeded",
184 MAX_INTERRUPTS);
187 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
188 make_fixnum(context_index + 1));
190 lisp_interrupt_contexts[context_index] = context;
192 /* no longer in Lisp now */
193 foreign_function_call_active = 1;
196 void
197 undo_fake_foreign_function_call(os_context_t *context)
199 /* Block all blockable signals. */
200 sigset_t block;
201 sigemptyset(&block);
202 sigaddset_blockable(&block);
203 sigprocmask(SIG_BLOCK, &block, 0);
205 /* going back into Lisp */
206 foreign_function_call_active = 0;
208 /* Undo dynamic binding. */
209 /* ### Do I really need to unbind_to_here()? */
210 /* FIXME: Is this to undo the binding of
211 * FREE_INTERRUPT_CONTEXT_INDEX? If so, we should say so. And
212 * perhaps yes, unbind_to_here() really would be clearer and less
213 * fragile.. */
214 /* dan (2001.08.10) thinks the above supposition is probably correct */
215 unbind();
217 #ifdef reg_ALLOC
218 /* Put the dynamic space free pointer back into the context. */
219 *os_context_register_addr(context, reg_ALLOC) =
220 (unsigned long) dynamic_space_free_pointer;
221 #endif
224 /* a handler for the signal caused by execution of a trap opcode
225 * signalling an internal error */
226 void
227 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
228 boolean continuable)
230 lispobj context_sap = 0;
232 fake_foreign_function_call(context);
234 /* Allocate the SAP object while the interrupts are still
235 * disabled. */
236 if (internal_errors_enabled) {
237 context_sap = alloc_sap(context);
240 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
242 if (internal_errors_enabled) {
243 SHOW("in interrupt_internal_error");
244 #if QSHOW
245 /* Display some rudimentary debugging information about the
246 * error, so that even if the Lisp error handler gets badly
247 * confused, we have a chance to determine what's going on. */
248 describe_internal_error(context);
249 #endif
250 funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
251 continuable ? T : NIL);
252 } else {
253 describe_internal_error(context);
254 /* There's no good way to recover from an internal error
255 * before the Lisp error handling mechanism is set up. */
256 lose("internal error too early in init, can't recover");
258 undo_fake_foreign_function_call(context);
259 if (continuable) {
260 arch_skip_instruction(context);
264 /* This function handles pending interrupts. Note that in C/kernel
265 * terms we dealt with the signal already; we just haven't decided
266 * whether to call a Lisp handler or do a GC or something like that.
267 * If it helps, you can think of pending_{signal,mask,info} as a
268 * one-element queue of signals that we have acknowledged but not
269 * processed */
271 void
272 interrupt_handle_pending(os_context_t *context)
274 #ifndef __i386__
275 boolean were_in_lisp = !foreign_function_call_active;
276 #endif
278 SetSymbolValue(INTERRUPT_PENDING, NIL);
280 if (maybe_gc_pending) {
281 maybe_gc_pending = 0;
282 #ifndef __i386__
283 if (were_in_lisp)
284 #endif
286 fake_foreign_function_call(context);
288 funcall0(SymbolFunction(MAYBE_GC));
289 #ifndef __i386__
290 if (were_in_lisp)
291 #endif
293 undo_fake_foreign_function_call(context);
297 /* FIXME: This isn't very clear. It would be good to reverse
298 * engineer it and rewrite the code more clearly, or write a clear
299 * explanation of what's going on in the comments, or both.
301 * WHN's question 1a: How come we unconditionally copy from
302 * pending_mask into the context, and then test whether
303 * pending_signal is set?
305 * WHN's question 1b: If pending_signal wasn't set, how could
306 * pending_mask be valid?
308 * Dan Barlow's reply (sbcl-devel 2001-03-13): And the answer is -
309 * or appears to be - because interrupt_maybe_gc set it that way
310 * (look in the #ifndef __i386__ bit). We can't GC during a
311 * pseudo-atomic, so we set maybe_gc_pending=1 and
312 * arch_set_pseudo_atomic_interrupted(..) When we come out of
313 * pseudo_atomic we're marked as interrupted, so we call
314 * interrupt_handle_pending, which does the GC using the pending
315 * context (it needs a context so that it has registers to use as
316 * GC roots) then notices there's no actual interrupt handler to
317 * call, so doesn't. That's the second question [1b] answered,
318 * anyway. Why we still need to copy the pending_mask into the
319 * context given that we're now done with the context anyway, I
320 * couldn't say. */
321 #if 0
322 memcpy(os_context_sigmask_addr(context), &pending_mask,
323 4 /* sizeof(sigset_t) */ );
324 #endif
325 sigemptyset(&pending_mask);
326 if (pending_signal) {
327 int signal = pending_signal;
328 siginfo_t info;
329 memcpy(&info, &pending_info, sizeof(siginfo_t));
330 pending_signal = 0;
331 interrupt_handle_now(signal, &info, context);
336 * the two main signal handlers:
337 * interrupt_handle_now(..)
338 * maybe_now_maybe_later(..)
341 void
342 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
344 os_context_t *context = (os_context_t*)void_context;
345 #ifndef __i386__
346 boolean were_in_lisp;
347 #endif
348 union interrupt_handler handler;
350 #ifdef LISP_FEATURE_LINUX
351 /* Under Linux, we appear to have to restore the fpu control word
352 from the context, as after the signal is delivered we appear to
353 have a null fpu control word. */
354 os_restore_fp_control(context);
355 #endif
356 handler = interrupt_handlers[signal];
358 if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
359 return;
362 #ifndef __i386__
363 were_in_lisp = !foreign_function_call_active;
364 if (were_in_lisp)
365 #endif
367 fake_foreign_function_call(context);
370 #ifdef QSHOW_SIGNALS
371 FSHOW((stderr,
372 "/entering interrupt_handle_now(%d, info, context)\n",
373 signal));
374 #endif
376 if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
378 /* This can happen if someone tries to ignore or default one
379 * of the signals we need for runtime support, and the runtime
380 * support decides to pass on it. */
381 lose("no handler for signal %d in interrupt_handle_now(..)", signal);
383 } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
385 /* Allocate the SAPs while the interrupts are still disabled.
386 * (FIXME: Why? This is the way it was done in CMU CL, and it
387 * even had the comment noting that this is the way it was
388 * done, but no motivation..) */
389 lispobj info_sap,context_sap = alloc_sap(context);
390 info_sap = alloc_sap(info);
391 /* Allow signals again. */
392 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
394 #ifdef QSHOW_SIGNALS
395 SHOW("calling Lisp-level handler");
396 #endif
398 funcall3(handler.lisp,
399 make_fixnum(signal),
400 info_sap,
401 context_sap);
402 } else {
404 #ifdef QSHOW_SIGNALS
405 SHOW("calling C-level handler");
406 #endif
408 /* Allow signals again. */
409 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
411 (*handler.c)(signal, info, void_context);
414 #ifndef __i386__
415 if (were_in_lisp)
416 #endif
418 undo_fake_foreign_function_call(context);
421 #ifdef QSHOW_SIGNALS
422 FSHOW((stderr,
423 "/returning from interrupt_handle_now(%d, info, context)\n",
424 signal));
425 #endif
428 static void
429 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
431 os_context_t *context = (os_context_t*)void_context;
433 /* FIXME: See Debian cmucl 2.4.17, and mail from DTC on the CMU CL
434 * mailing list 23 Oct 1999, for changes in FPU handling at
435 * interrupt time which should be ported into SBCL. Also see the
436 * analogous logic at the head of interrupt_handle_now for
437 * more related FIXME stuff.
440 #ifdef LISP_FEATURE_LINUX
441 os_restore_fp_control(context);
442 #endif
444 /* see comments at top of code/signal.lisp for what's going on here
445 * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW
447 if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
449 /* FIXME: This code is exactly the same as the code in the
450 * other leg of the if(..), and should be factored out into
451 * a shared function. */
452 pending_signal = signal;
453 memcpy(&pending_info, info, sizeof(siginfo_t));
454 memcpy(&pending_mask,
455 os_context_sigmask_addr(context),
456 sizeof(sigset_t));
457 sigaddset_blockable(os_context_sigmask_addr(context));
458 SetSymbolValue(INTERRUPT_PENDING, T);
460 } else if (
461 #ifndef __i386__
462 (!foreign_function_call_active) &&
463 #endif
464 arch_pseudo_atomic_atomic(context)) {
466 /* FIXME: It would probably be good to replace these bare
467 * memcpy(..) calls with calls to cpy_siginfo_t and
468 * cpy_sigset_t, so that we only have to get the sizeof
469 * expressions right in one place, and after that static type
470 * checking takes over. */
471 pending_signal = signal;
472 memcpy(&pending_info, info, sizeof(siginfo_t));
473 memcpy(&pending_mask,
474 os_context_sigmask_addr(context),
475 sizeof(sigset_t));
476 sigaddset_blockable(os_context_sigmask_addr(context));
478 arch_set_pseudo_atomic_interrupted(context);
480 } else {
481 interrupt_handle_now(signal, info, context);
486 * stuff to detect and handle hitting the GC trigger
489 #ifndef GENCGC /* since GENCGC has its own way to record trigger */
490 static boolean
491 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
493 if (current_auto_gc_trigger == NULL)
494 return 0;
495 else{
496 lispobj *badaddr=(lispobj *)arch_get_bad_addr(signal,
497 info,
498 context);
500 return (badaddr >= current_auto_gc_trigger &&
501 badaddr < current_dynamic_space + DYNAMIC_SPACE_SIZE);
504 #endif
506 #ifndef __i386__
507 /* This function gets called from the SIGSEGV (for e.g. Linux or
508 * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
509 * whether the signal was due to treading on the mprotect()ed zone -
510 * and if so, arrange for a GC to happen. */
511 boolean
512 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
514 os_context_t *context=(os_context_t *) void_context;
516 if (!foreign_function_call_active
517 #ifndef GENCGC /* since GENCGC has its own way to record trigger */
518 && gc_trigger_hit(signal, info, context)
519 #endif
521 #ifndef GENCGC /* since GENCGC has its own way to record trigger */
522 clear_auto_gc_trigger();
523 #endif
525 if (arch_pseudo_atomic_atomic(context)) {
526 /* don't GC during an atomic operation. Instead, copy the
527 * signal mask somewhere safe. interrupt_handle_pending
528 * will detect pending_signal==0 and know to do a GC with the
529 * signal context instead of calling a Lisp-level handler */
530 maybe_gc_pending = 1;
531 if (pending_signal == 0) {
532 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
533 * idiom occurs over and over. It should be factored out
534 * into a function with a descriptive name. */
535 memcpy(&pending_mask,
536 os_context_sigmask_addr(context),
537 sizeof(sigset_t));
538 sigaddset_blockable(os_context_sigmask_addr(context));
540 arch_set_pseudo_atomic_interrupted(context);
542 else {
543 lispobj *old_free_space=current_dynamic_space;
544 fake_foreign_function_call(context);
545 funcall0(SymbolFunction(MAYBE_GC));
546 undo_fake_foreign_function_call(context);
547 if(current_dynamic_space==old_free_space)
548 /* MAYBE-GC (as the name suggest) might not. If it
549 * doesn't, it won't reset the GC trigger either, so we
550 * have to do it ourselves. Add small amount of space
551 * to tide us over while GC is inhibited
553 set_auto_gc_trigger(DYNAMIC_SPACE_SIZE
554 -(u32)os_vm_page_size);
556 return 1;
557 } else {
558 return 0;
561 #endif
564 * noise to install handlers
568 * what low-level signal handlers looked like before
569 * undoably_install_low_level_interrupt_handler() got involved
571 struct low_level_signal_handler_state {
572 int was_modified;
573 void (*handler)(int, siginfo_t*, void*);
574 } old_low_level_signal_handler_states[NSIG];
576 void
577 uninstall_low_level_interrupt_handlers_atexit(void)
579 int signal;
580 for (signal = 0; signal < NSIG; ++signal) {
581 struct low_level_signal_handler_state
582 *old_low_level_signal_handler_state =
583 old_low_level_signal_handler_states + signal;
584 if (old_low_level_signal_handler_state->was_modified) {
585 struct sigaction sa;
586 sa.sa_sigaction = old_low_level_signal_handler_state->handler;
587 sigemptyset(&sa.sa_mask);
588 sa.sa_flags = SA_SIGINFO | SA_RESTART;
589 sigaction(signal, &sa, NULL);
594 /* Undoably install a special low-level handler for signal; or if
595 * handler is SIG_DFL, remove any special handling for signal.
597 * The "undoably" aspect is because we also arrange with atexit() for
598 * the handler to be restored to its old value. This is for tidiness:
599 * it shouldn't matter much ordinarily, but it does remove a window
600 * where e.g. memory fault signals (SIGSEGV or SIGBUS, which in
601 * ordinary operation of SBCL are sent to the generational garbage
602 * collector, then possibly onward to Lisp code) or SIGINT (which is
603 * ordinarily passed to Lisp code) could otherwise be handled
604 * bizarrely/brokenly because the Lisp code would try to deal with
605 * them using machinery (like stream output buffers) which has already
606 * been dismantled. */
607 void
608 undoably_install_low_level_interrupt_handler (int signal,
609 void handler(int,
610 siginfo_t*,
611 void*))
613 struct sigaction sa;
614 struct low_level_signal_handler_state *old_low_level_signal_handler_state =
615 old_low_level_signal_handler_states + signal;
617 if (0 > signal || signal >= NSIG) {
618 lose("bad signal number %d", signal);
621 sa.sa_sigaction = handler;
622 sigemptyset(&sa.sa_mask);
623 sigaddset_blockable(&sa.sa_mask);
624 sa.sa_flags = SA_SIGINFO | SA_RESTART;
626 /* In the case of interrupt handlers which are modified more than
627 * once, we only save the original unmodified copy. */
628 if (!old_low_level_signal_handler_state->was_modified) {
629 struct sigaction *old_handler =
630 (struct sigaction*) &old_low_level_signal_handler_state->handler;
631 old_low_level_signal_handler_state->was_modified = 1;
632 sigaction(signal, &sa, old_handler);
633 } else {
634 sigaction(signal, &sa, NULL);
637 interrupt_low_level_handlers[signal] =
638 (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
641 /* This is called from Lisp. */
642 unsigned long
643 install_handler(int signal, void handler(int, siginfo_t*, void*))
645 struct sigaction sa;
646 sigset_t old, new;
647 union interrupt_handler oldhandler;
649 FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
651 sigemptyset(&new);
652 sigaddset(&new, signal);
653 sigprocmask(SIG_BLOCK, &new, &old);
655 sigemptyset(&new);
656 sigaddset_blockable(&new);
658 FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%d\n",
659 interrupt_low_level_handlers[signal]));
660 if (interrupt_low_level_handlers[signal]==0) {
661 if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
662 ARE_SAME_HANDLER(handler, SIG_IGN)) {
663 sa.sa_sigaction = handler;
664 } else if (sigismember(&new, signal)) {
665 sa.sa_sigaction = maybe_now_maybe_later;
666 } else {
667 sa.sa_sigaction = interrupt_handle_now;
670 sigemptyset(&sa.sa_mask);
671 sigaddset_blockable(&sa.sa_mask);
672 sa.sa_flags = SA_SIGINFO | SA_RESTART;
674 sigaction(signal, &sa, NULL);
677 oldhandler = interrupt_handlers[signal];
678 interrupt_handlers[signal].c = handler;
680 sigprocmask(SIG_SETMASK, &old, 0);
682 FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
684 return (unsigned long)oldhandler.lisp;
687 void
688 interrupt_init(void)
690 int i;
692 SHOW("entering interrupt_init()");
694 /* Set up for recovery from any installed low-level handlers. */
695 atexit(&uninstall_low_level_interrupt_handlers_atexit);
697 /* Set up high level handler information. */
698 for (i = 0; i < NSIG; i++) {
699 interrupt_handlers[i].c =
700 /* (The cast here blasts away the distinction between
701 * SA_SIGACTION-style three-argument handlers and
702 * signal(..)-style one-argument handlers, which is OK
703 * because it works to call the 1-argument form where the
704 * 3-argument form is expected.) */
705 (void (*)(int, siginfo_t*, void*))SIG_DFL;
708 SHOW("returning from interrupt_init()");