0.7.13.5
[sbcl/lichteblau.git] / src / runtime / interrupt.c
blob4027b3dbf2fde10a9a3caf8bf7497de834b9100a
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"
34 #include "genesis/simple-fun.h"
35 #include "genesis/fdefn.h"
36 #include "genesis/symbol.h"
37 #include "genesis/static-symbols.h"
39 void sigaddset_blockable(sigset_t *s)
41 sigaddset(s, SIGHUP);
42 sigaddset(s, SIGINT);
43 sigaddset(s, SIGQUIT);
44 sigaddset(s, SIGPIPE);
45 sigaddset(s, SIGALRM);
46 sigaddset(s, SIGURG);
47 sigaddset(s, SIGFPE);
48 sigaddset(s, SIGTSTP);
49 sigaddset(s, SIGCHLD);
50 sigaddset(s, SIGIO);
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
64 * becomes 'yes'.) */
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 boolean maybe_gc_pending = 0;
109 * utility routines used by various signal handlers
112 void
113 build_fake_control_stack_frames(os_context_t *context)
115 #ifndef LISP_FEATURE_X86
117 lispobj oldcont;
119 /* Build a fake stack frame or frames */
121 current_control_frame_pointer =
122 (lispobj *)(*os_context_register_addr(context, reg_CSP));
123 if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
124 == current_control_frame_pointer) {
125 /* There is a small window during call where the callee's
126 * frame isn't built yet. */
127 if (lowtag_of(*os_context_register_addr(context, reg_CODE))
128 == FUN_POINTER_LOWTAG) {
129 /* We have called, but not built the new frame, so
130 * build it for them. */
131 current_control_frame_pointer[0] =
132 *os_context_register_addr(context, reg_OCFP);
133 current_control_frame_pointer[1] =
134 *os_context_register_addr(context, reg_LRA);
135 current_control_frame_pointer += 8;
136 /* Build our frame on top of it. */
137 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
139 else {
140 /* We haven't yet called, build our frame as if the
141 * partial frame wasn't there. */
142 oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
145 /* We can't tell whether we are still in the caller if it had to
146 * allocate a stack frame due to stack arguments. */
147 /* This observation provoked some past CMUCL maintainer to ask
148 * "Can anything strange happen during return?" */
149 else {
150 /* normal case */
151 oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
154 current_control_stack_pointer = current_control_frame_pointer + 8;
156 current_control_frame_pointer[0] = oldcont;
157 current_control_frame_pointer[1] = NIL;
158 current_control_frame_pointer[2] =
159 (lispobj)(*os_context_register_addr(context, reg_CODE));
160 #endif
163 void
164 fake_foreign_function_call(os_context_t *context)
166 int context_index;
168 /* Get current Lisp state from context. */
169 #ifdef reg_ALLOC
170 dynamic_space_free_pointer =
171 (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
172 #ifdef alpha
173 if ((long)dynamic_space_free_pointer & 1) {
174 lose("dead in fake_foreign_function_call, context = %x", context);
176 #endif
177 #endif
178 #ifdef reg_BSP
179 current_binding_stack_pointer =
180 (lispobj *)(*os_context_register_addr(context, reg_BSP));
181 #endif
183 build_fake_control_stack_frames(context);
185 /* Do dynamic binding of the active interrupt context index
186 * and save the context in the context array. */
187 context_index = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
188 /* FIXME: Ick! Why use abstract "make_fixnum" in some places if
189 * you're going to convert from fixnum by bare >>2 in other
190 * places? Use fixnum_value(..) here, and look for other places
191 * which do bare >> and << for fixnum_value and make_fixnum. */
193 if (context_index >= MAX_INTERRUPTS) {
194 lose("maximum interrupt nesting depth (%d) exceeded", MAX_INTERRUPTS);
197 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
198 make_fixnum(context_index + 1));
200 lisp_interrupt_contexts[context_index] = context;
202 /* no longer in Lisp now */
203 foreign_function_call_active = 1;
206 void
207 undo_fake_foreign_function_call(os_context_t *context)
209 /* Block all blockable signals. */
210 sigset_t block;
211 sigemptyset(&block);
212 sigaddset_blockable(&block);
213 sigprocmask(SIG_BLOCK, &block, 0);
215 /* going back into Lisp */
216 foreign_function_call_active = 0;
218 /* Undo dynamic binding. */
219 /* ### Do I really need to unbind_to_here()? */
220 /* FIXME: Is this to undo the binding of
221 * FREE_INTERRUPT_CONTEXT_INDEX? If so, we should say so. And
222 * perhaps yes, unbind_to_here() really would be clearer and less
223 * fragile.. */
224 /* dan (2001.08.10) thinks the above supposition is probably correct */
225 unbind();
227 #ifdef reg_ALLOC
228 /* Put the dynamic space free pointer back into the context. */
229 *os_context_register_addr(context, reg_ALLOC) =
230 (unsigned long) dynamic_space_free_pointer;
231 #endif
234 /* a handler for the signal caused by execution of a trap opcode
235 * signalling an internal error */
236 void
237 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
238 boolean continuable)
240 lispobj context_sap = 0;
242 fake_foreign_function_call(context);
244 /* Allocate the SAP object while the interrupts are still
245 * disabled. */
246 if (internal_errors_enabled) {
247 context_sap = alloc_sap(context);
250 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
252 if (internal_errors_enabled) {
253 SHOW("in interrupt_internal_error");
254 #if QSHOW
255 /* Display some rudimentary debugging information about the
256 * error, so that even if the Lisp error handler gets badly
257 * confused, we have a chance to determine what's going on. */
258 describe_internal_error(context);
259 #endif
260 funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
261 continuable ? T : NIL);
262 } else {
263 describe_internal_error(context);
264 /* There's no good way to recover from an internal error
265 * before the Lisp error handling mechanism is set up. */
266 lose("internal error too early in init, can't recover");
268 undo_fake_foreign_function_call(context);
269 if (continuable) {
270 arch_skip_instruction(context);
274 /* This function handles pending interrupts. Note that in C/kernel
275 * terms we dealt with the signal already; we just haven't decided
276 * whether to call a Lisp handler or do a GC or something like that.
277 * If it helps, you can think of pending_{signal,mask,info} as a
278 * one-element queue of signals that we have acknowledged but not
279 * processed */
281 void
282 interrupt_handle_pending(os_context_t *context)
284 #ifndef __i386__
285 boolean were_in_lisp = !foreign_function_call_active;
286 #endif
288 SetSymbolValue(INTERRUPT_PENDING, NIL);
290 if (maybe_gc_pending) {
291 maybe_gc_pending = 0;
292 #ifndef __i386__
293 if (were_in_lisp)
294 #endif
296 fake_foreign_function_call(context);
298 funcall0(SymbolFunction(MAYBE_GC));
299 #ifndef __i386__
300 if (were_in_lisp)
301 #endif
303 undo_fake_foreign_function_call(context);
307 /* FIXME: This isn't very clear. It would be good to reverse
308 * engineer it and rewrite the code more clearly, or write a clear
309 * explanation of what's going on in the comments, or both.
311 * WHN's question 1a: How come we unconditionally copy from
312 * pending_mask into the context, and then test whether
313 * pending_signal is set?
315 * WHN's question 1b: If pending_signal wasn't set, how could
316 * pending_mask be valid?
318 * Dan Barlow's reply (sbcl-devel 2001-03-13): And the answer is -
319 * or appears to be - because interrupt_maybe_gc set it that way
320 * (look in the #ifndef __i386__ bit). We can't GC during a
321 * pseudo-atomic, so we set maybe_gc_pending=1 and
322 * arch_set_pseudo_atomic_interrupted(..) When we come out of
323 * pseudo_atomic we're marked as interrupted, so we call
324 * interrupt_handle_pending, which does the GC using the pending
325 * context (it needs a context so that it has registers to use as
326 * GC roots) then notices there's no actual interrupt handler to
327 * call, so doesn't. That's the second question [1b] answered,
328 * anyway. Why we still need to copy the pending_mask into the
329 * context given that we're now done with the context anyway, I
330 * couldn't say. */
331 #if 0
332 memcpy(os_context_sigmask_addr(context), &pending_mask,
333 4 /* sizeof(sigset_t) */ );
334 #endif
335 sigemptyset(&pending_mask);
336 if (pending_signal) {
337 int signal = pending_signal;
338 siginfo_t info;
339 memcpy(&info, &pending_info, sizeof(siginfo_t));
340 pending_signal = 0;
341 interrupt_handle_now(signal, &info, context);
346 * the two main signal handlers:
347 * interrupt_handle_now(..)
348 * maybe_now_maybe_later(..)
350 * to which we have added interrupt_handle_now_handler(..). Why?
351 * Well, mostly because the SPARC/Linux platform doesn't quite do
352 * signals the way we want them done. The third argument in the
353 * handler isn't filled in by the kernel properly, so we fix it up
354 * ourselves in the arch_os_get_context(..) function; however, we only
355 * want to do this when we first hit the handler, and not when
356 * interrupt_handle_now(..) is being called from some other handler
357 * (when the fixup will already have been done). -- CSR, 2002-07-23
360 void
361 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
363 os_context_t *context = (os_context_t*)void_context;
364 #ifndef __i386__
365 boolean were_in_lisp;
366 #endif
367 union interrupt_handler handler;
369 #ifdef LISP_FEATURE_LINUX
370 /* Under Linux on some architectures, we appear to have to restore
371 the FPU control word from the context, as after the signal is
372 delivered we appear to have a null FPU control word. */
373 os_restore_fp_control(context);
374 #endif
375 handler = interrupt_handlers[signal];
377 if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
378 return;
381 #ifndef __i386__
382 were_in_lisp = !foreign_function_call_active;
383 if (were_in_lisp)
384 #endif
386 fake_foreign_function_call(context);
389 #ifdef QSHOW_SIGNALS
390 FSHOW((stderr,
391 "/entering interrupt_handle_now(%d, info, context)\n",
392 signal));
393 #endif
395 if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
397 /* This can happen if someone tries to ignore or default one
398 * of the signals we need for runtime support, and the runtime
399 * support decides to pass on it. */
400 lose("no handler for signal %d in interrupt_handle_now(..)", signal);
402 } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
404 /* Allocate the SAPs while the interrupts are still disabled.
405 * (FIXME: Why? This is the way it was done in CMU CL, and it
406 * even had the comment noting that this is the way it was
407 * done, but no motivation..) */
408 lispobj info_sap,context_sap = alloc_sap(context);
409 info_sap = alloc_sap(info);
410 /* Allow signals again. */
411 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
413 #ifdef QSHOW_SIGNALS
414 SHOW("calling Lisp-level handler");
415 #endif
417 funcall3(handler.lisp,
418 make_fixnum(signal),
419 info_sap,
420 context_sap);
421 } else {
423 #ifdef QSHOW_SIGNALS
424 SHOW("calling C-level handler");
425 #endif
427 /* Allow signals again. */
428 sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
430 (*handler.c)(signal, info, void_context);
433 #ifndef __i386__
434 if (were_in_lisp)
435 #endif
437 undo_fake_foreign_function_call(context);
440 #ifdef QSHOW_SIGNALS
441 FSHOW((stderr,
442 "/returning from interrupt_handle_now(%d, info, context)\n",
443 signal));
444 #endif
447 static void
448 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
450 os_context_t *context = arch_os_get_context(&void_context);
452 #ifdef LISP_FEATURE_LINUX
453 os_restore_fp_control(context);
454 #endif
456 /* see comments at top of code/signal.lisp for what's going on here
457 * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW
459 if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
461 /* FIXME: This code is exactly the same as the code in the
462 * other leg of the if(..), and should be factored out into
463 * a shared function. */
464 pending_signal = signal;
465 memcpy(&pending_info, info, sizeof(siginfo_t));
466 memcpy(&pending_mask,
467 os_context_sigmask_addr(context),
468 sizeof(sigset_t));
469 sigaddset_blockable(os_context_sigmask_addr(context));
470 SetSymbolValue(INTERRUPT_PENDING, T);
472 } else if (
473 #ifndef __i386__
474 (!foreign_function_call_active) &&
475 #endif
476 arch_pseudo_atomic_atomic(context)) {
478 /* FIXME: It would probably be good to replace these bare
479 * memcpy(..) calls with calls to cpy_siginfo_t and
480 * cpy_sigset_t, so that we only have to get the sizeof
481 * expressions right in one place, and after that static type
482 * checking takes over. */
483 pending_signal = signal;
484 memcpy(&pending_info, info, sizeof(siginfo_t));
485 memcpy(&pending_mask,
486 os_context_sigmask_addr(context),
487 sizeof(sigset_t));
488 sigaddset_blockable(os_context_sigmask_addr(context));
490 arch_set_pseudo_atomic_interrupted(context);
492 } else {
493 interrupt_handle_now(signal, info, context);
498 void
499 interrupt_handle_now_handler(int signal, siginfo_t *info, void *void_context)
501 os_context_t *context = arch_os_get_context(&void_context);
502 interrupt_handle_now(signal, info, context);
506 * stuff to detect and handle hitting the GC trigger
509 #ifndef LISP_FEATURE_GENCGC
510 /* since GENCGC has its own way to record trigger */
511 static boolean
512 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
514 if (current_auto_gc_trigger == NULL)
515 return 0;
516 else{
517 void *badaddr=arch_get_bad_addr(signal,info,context);
518 return (badaddr >= (void *)current_auto_gc_trigger &&
519 badaddr <((void *)current_dynamic_space + DYNAMIC_SPACE_SIZE));
522 #endif
524 /* and similarly for the control stack guard page */
526 boolean handle_control_stack_guard_triggered(os_context_t *context,void *addr)
528 /* note the os_context hackery here. When the signal handler returns,
529 * it won't go back to what it was doing ... */
530 if(addr>=(void *)CONTROL_STACK_GUARD_PAGE &&
531 addr<(void *)(CONTROL_STACK_GUARD_PAGE+os_vm_page_size)) {
532 void *fun;
533 void *code;
535 /* we hit the end of the control stack. disable protection
536 * temporarily so the error handler has some headroom */
537 protect_control_stack_guard_page(0);
539 fun = (void *)
540 native_pointer((lispobj) SymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
541 code = &(((struct simple_fun *) fun)->code);
543 /* Build a stack frame showing `interrupted' so that the
544 * user's backtrace makes (as much) sense (as usual) */
545 build_fake_control_stack_frames(context);
546 /* signal handler will "return" to this error-causing function */
547 *os_context_pc_addr(context) = code;
548 #ifdef LISP_FEATURE_X86
549 *os_context_register_addr(context,reg_ECX) = 0;
550 #else
551 /* this much of the calling convention is common to all
552 non-x86 ports */
553 *os_context_register_addr(context,reg_NARGS) = 0;
554 *os_context_register_addr(context,reg_LIP) = code;
555 *os_context_register_addr(context,reg_CFP) =
556 current_control_frame_pointer;
557 #endif
558 #ifdef ARCH_HAS_NPC_REGISTER
559 *os_context_npc_addr(context) =
560 4 + *os_context_pc_addr(context);
561 #endif
562 #ifdef LISP_FEATURE_SPARC
563 /* Bletch. This is a feature of the SPARC calling convention,
564 which sadly I'm not going to go into in large detail here,
565 as I don't know it well enough. Suffice to say that if the
566 line
568 (INST MOVE CODE-TN FUNCTION)
570 in compiler/sparc/call.lisp is changed, then this bit can
571 probably go away. -- CSR, 2002-07-24 */
572 *os_context_register_addr(context,reg_CODE) =
573 fun + FUN_POINTER_LOWTAG;
574 #endif
575 return 1;
577 else return 0;
580 #ifndef LISP_FEATURE_X86
581 /* This function gets called from the SIGSEGV (for e.g. Linux or
582 * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
583 * whether the signal was due to treading on the mprotect()ed zone -
584 * and if so, arrange for a GC to happen. */
585 boolean
586 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
588 os_context_t *context=(os_context_t *) void_context;
590 if (!foreign_function_call_active
591 #ifndef LISP_FEATURE_GENCGC
592 /* nb: GENCGC on non-x86? I really don't think so. This
593 * happens every time */
594 && gc_trigger_hit(signal, info, context)
595 #endif
597 #ifndef LISP_FEATURE_GENCGC
598 clear_auto_gc_trigger();
599 #endif
601 if (arch_pseudo_atomic_atomic(context)) {
602 /* don't GC during an atomic operation. Instead, copy the
603 * signal mask somewhere safe. interrupt_handle_pending
604 * will detect pending_signal==0 and know to do a GC with the
605 * signal context instead of calling a Lisp-level handler */
606 maybe_gc_pending = 1;
607 if (pending_signal == 0) {
608 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
609 * idiom occurs over and over. It should be factored out
610 * into a function with a descriptive name. */
611 memcpy(&pending_mask,
612 os_context_sigmask_addr(context),
613 sizeof(sigset_t));
614 sigaddset_blockable(os_context_sigmask_addr(context));
616 arch_set_pseudo_atomic_interrupted(context);
618 else {
619 lispobj *old_free_space=current_dynamic_space;
620 fake_foreign_function_call(context);
621 funcall0(SymbolFunction(MAYBE_GC));
622 undo_fake_foreign_function_call(context);
623 if(current_dynamic_space==old_free_space)
624 /* MAYBE-GC (as the name suggest) might not. If it
625 * doesn't, it won't reset the GC trigger either, so we
626 * have to do it ourselves. Put it near the end of
627 * dynamic space so we're not running into it continually
629 set_auto_gc_trigger(DYNAMIC_SPACE_SIZE
630 -(u32)os_vm_page_size);
632 return 1;
633 } else {
634 return 0;
637 #endif
640 * noise to install handlers
644 * what low-level signal handlers looked like before
645 * undoably_install_low_level_interrupt_handler() got involved
647 struct low_level_signal_handler_state {
648 int was_modified;
649 void (*handler)(int, siginfo_t*, void*);
650 } old_low_level_signal_handler_states[NSIG];
652 void
653 uninstall_low_level_interrupt_handlers_atexit(void)
655 int signal;
656 for (signal = 0; signal < NSIG; ++signal) {
657 struct low_level_signal_handler_state
658 *old_low_level_signal_handler_state =
659 old_low_level_signal_handler_states + signal;
660 if (old_low_level_signal_handler_state->was_modified) {
661 struct sigaction sa;
662 sa.sa_sigaction = old_low_level_signal_handler_state->handler;
663 sigemptyset(&sa.sa_mask);
664 sa.sa_flags = SA_SIGINFO | SA_RESTART;
665 sigaction(signal, &sa, NULL);
670 /* Undoably install a special low-level handler for signal; or if
671 * handler is SIG_DFL, remove any special handling for signal.
673 * The "undoably" aspect is because we also arrange with atexit() for
674 * the handler to be restored to its old value. This is for tidiness:
675 * it shouldn't matter much ordinarily, but it does remove a window
676 * where e.g. memory fault signals (SIGSEGV or SIGBUS, which in
677 * ordinary operation of SBCL are sent to the generational garbage
678 * collector, then possibly onward to Lisp code) or SIGINT (which is
679 * ordinarily passed to Lisp code) could otherwise be handled
680 * bizarrely/brokenly because the Lisp code would try to deal with
681 * them using machinery (like stream output buffers) which has already
682 * been dismantled. */
683 void
684 undoably_install_low_level_interrupt_handler (int signal,
685 void handler(int,
686 siginfo_t*,
687 void*))
689 struct sigaction sa;
690 struct low_level_signal_handler_state *old_low_level_signal_handler_state =
691 old_low_level_signal_handler_states + signal;
693 if (0 > signal || signal >= NSIG) {
694 lose("bad signal number %d", signal);
697 sa.sa_sigaction = handler;
698 sigemptyset(&sa.sa_mask);
699 sigaddset_blockable(&sa.sa_mask);
700 sa.sa_flags = SA_SIGINFO | SA_RESTART;
701 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
702 /* Signal handlers are run on the control stack, so if it is exhausted
703 * we had better use an alternate stack for whatever signal tells us
704 * we've exhausted it */
705 if(signal==SIG_MEMORY_FAULT) {
706 stack_t sigstack;
707 sigstack.ss_sp=(void *) ALTERNATE_SIGNAL_STACK_START;
708 sigstack.ss_flags=0;
709 sigstack.ss_size = SIGSTKSZ;
710 sigaltstack(&sigstack,0);
711 sa.sa_flags|=SA_ONSTACK;
713 #endif
715 /* In the case of interrupt handlers which are modified more than
716 * once, we only save the original unmodified copy. */
717 if (!old_low_level_signal_handler_state->was_modified) {
718 struct sigaction *old_handler =
719 (struct sigaction*) &old_low_level_signal_handler_state->handler;
720 old_low_level_signal_handler_state->was_modified = 1;
721 sigaction(signal, &sa, old_handler);
722 } else {
723 sigaction(signal, &sa, NULL);
726 interrupt_low_level_handlers[signal] =
727 (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
730 /* This is called from Lisp. */
731 unsigned long
732 install_handler(int signal, void handler(int, siginfo_t*, void*))
734 struct sigaction sa;
735 sigset_t old, new;
736 union interrupt_handler oldhandler;
738 FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
740 sigemptyset(&new);
741 sigaddset(&new, signal);
742 sigprocmask(SIG_BLOCK, &new, &old);
744 sigemptyset(&new);
745 sigaddset_blockable(&new);
747 FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%d\n",
748 interrupt_low_level_handlers[signal]));
749 if (interrupt_low_level_handlers[signal]==0) {
750 if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
751 ARE_SAME_HANDLER(handler, SIG_IGN)) {
752 sa.sa_sigaction = handler;
753 } else if (sigismember(&new, signal)) {
754 sa.sa_sigaction = maybe_now_maybe_later;
755 } else {
756 sa.sa_sigaction = interrupt_handle_now_handler;
759 sigemptyset(&sa.sa_mask);
760 sigaddset_blockable(&sa.sa_mask);
761 sa.sa_flags = SA_SIGINFO | SA_RESTART;
763 sigaction(signal, &sa, NULL);
766 oldhandler = interrupt_handlers[signal];
767 interrupt_handlers[signal].c = handler;
769 sigprocmask(SIG_SETMASK, &old, 0);
771 FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
773 return (unsigned long)oldhandler.lisp;
776 void
777 interrupt_init(void)
779 int i;
781 SHOW("entering interrupt_init()");
783 /* Set up for recovery from any installed low-level handlers. */
784 atexit(&uninstall_low_level_interrupt_handlers_atexit);
786 /* Set up high level handler information. */
787 for (i = 0; i < NSIG; i++) {
788 interrupt_handlers[i].c =
789 /* (The cast here blasts away the distinction between
790 * SA_SIGACTION-style three-argument handlers and
791 * signal(..)-style one-argument handlers, which is OK
792 * because it works to call the 1-argument form where the
793 * 3-argument form is expected.) */
794 (void (*)(int, siginfo_t*, void*))SIG_DFL;
797 SHOW("returning from interrupt_init()");