cb5fcafd684334be53818e93ac1a9a5c73e6edee
[sbcl.git] / src / runtime / interrupt.c
1 /*
2  * interrupt-handling magic
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
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.
14  */
15
16 #include <stdio.h>
17
18 #include <signal.h>
19 #ifdef mach /* KLUDGE: #ifdef on lowercase symbols? Ick. -- WHN 19990904 */
20 #ifdef mips
21 #include <mips/cpu.h>
22 #endif
23 #endif
24
25 #include "runtime.h"
26 #include "arch.h"
27 #include "sbcl.h"
28 #include "os.h"
29 #include "interrupt.h"
30 #include "globals.h"
31 #include "lispregs.h"
32 #include "validate.h"
33 #include "monitor.h"
34 #include "gc.h"
35 #include "alloc.h"
36 #include "dynbind.h"
37 #include "interr.h"
38
39 void sigaddset_blockable(sigset_t *s)
40 {
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, SIGTSTP);
48     sigaddset(s, SIGCHLD);
49     sigaddset(s, SIGIO);
50     sigaddset(s, SIGXCPU);
51     sigaddset(s, SIGXFSZ);
52     sigaddset(s, SIGVTALRM);
53     sigaddset(s, SIGPROF);
54     sigaddset(s, SIGWINCH);
55     sigaddset(s, SIGUSR1);
56     sigaddset(s, SIGUSR2);
57 }
58
59 /* When we catch an internal error, should we pass it back to Lisp to
60  * be handled in a high-level way? (Early in cold init, the answer is
61  * 'no', because Lisp is still too brain-dead to handle anything.
62  * After sufficient initialization has been completed, the answer
63  * becomes 'yes'.) */
64 boolean internal_errors_enabled = 0;
65
66 os_context_t *lisp_interrupt_contexts[MAX_INTERRUPTS];
67
68 /* As far as I can tell, what's going on here is:
69  *
70  * In the case of most signals, when Lisp asks us to handle the
71  * signal, the outermost handler (the one actually passed to UNIX) is
72  * either interrupt_handle_now(..) or interrupt_handle_later(..).
73  * In that case, the Lisp-level handler is stored in interrupt_handlers[..]
74  * and interrupt_low_level_handlers[..] is cleared.
75  *
76  * However, some signals need special handling, e.g. 
77  *
78  * o the SIGSEGV (for e.g. Linux) or SIGBUS (for e.g. FreeBSD) used by the
79  *   garbage collector to detect violations of write protection,
80  *   because some cases of such signals (e.g. GC-related violations of
81  *   write protection) are handled at C level and never passed on to
82  *   Lisp. For such signals, we still store any Lisp-level handler
83  *   in interrupt_handlers[..], but for the outermost handle we use
84  *   the value from interrupt_low_level_handlers[..], instead of the
85  *   ordinary interrupt_handle_now(..) or interrupt_handle_later(..).
86  *
87  * o the SIGTRAP (Linux/Alpha) which Lisp code uses to handle breakpoints,
88  *   pseudo-atomic sections, and some classes of error (e.g. "function
89  *   not defined").  This never goes anywhere near the Lisp handlers at all.
90  *   See runtime/alpha-arch.c and code/signal.lisp 
91  * 
92  * - WHN 20000728, dan 20010128 */
93
94
95 void (*interrupt_low_level_handlers[NSIG]) (int, siginfo_t*, void*) = {0};
96 union interrupt_handler interrupt_handlers[NSIG];
97
98 /* signal number, siginfo_t, and old mask information for pending signal
99  *
100  * pending_signal=0 when there is no pending signal. */
101 static int pending_signal = 0;
102 static siginfo_t pending_info;
103 static sigset_t pending_mask;
104
105 static boolean maybe_gc_pending = 0;
106 \f
107 /*
108  * utility routines used by various signal handlers
109  */
110
111 void
112 fake_foreign_function_call(os_context_t *context)
113 {
114     int context_index;
115 #ifndef __i386__
116     lispobj oldcont;
117 #endif
118
119     /* Get current Lisp state from context. */
120 #ifdef reg_ALLOC
121     dynamic_space_free_pointer =
122         (lispobj *)(*os_context_register_addr(context, reg_ALLOC));
123 #ifdef alpha
124     if ((long)dynamic_space_free_pointer & 1) {
125         lose("dead in fake_foreign_function_call, context = %x", context);
126     }
127 #endif
128 #endif
129 #ifdef reg_BSP
130     current_binding_stack_pointer =
131         (lispobj *)(*os_context_register_addr(context, reg_BSP));
132 #endif
133
134 #ifndef __i386__
135     /* Build a fake stack frame. */
136     current_control_frame_pointer =
137         (lispobj *)(*os_context_register_addr(context, reg_CSP));
138     if ((lispobj *)(*os_context_register_addr(context, reg_CFP))
139         == current_control_frame_pointer) {
140         /* There is a small window during call where the callee's
141          * frame isn't built yet. */
142         if (LowtagOf(*os_context_register_addr(context, reg_CODE))
143             == type_FunctionPointer) {
144             /* We have called, but not built the new frame, so
145              * build it for them. */
146             current_control_frame_pointer[0] =
147                 *os_context_register_addr(context, reg_OCFP);
148             current_control_frame_pointer[1] =
149                 *os_context_register_addr(context, reg_LRA);
150             current_control_frame_pointer += 8;
151             /* Build our frame on top of it. */
152             oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
153         }
154         else {
155             /* We haven't yet called, build our frame as if the
156              * partial frame wasn't there. */
157             oldcont = (lispobj)(*os_context_register_addr(context, reg_OCFP));
158         }
159     }
160     /* ### We can't tell whether we are still in the caller if it had
161      * to reg_ALLOCate the stack frame due to stack arguments. */
162     /* ### Can anything strange happen during return? */
163     else {
164         /* normal case */
165         oldcont = (lispobj)(*os_context_register_addr(context, reg_CFP));
166     }
167
168     current_control_stack_pointer = current_control_frame_pointer + 8;
169
170     current_control_frame_pointer[0] = oldcont;
171     current_control_frame_pointer[1] = NIL;
172     current_control_frame_pointer[2] =
173         (lispobj)(*os_context_register_addr(context, reg_CODE));
174 #endif
175
176     /* Do dynamic binding of the active interrupt context index
177      * and save the context in the context array. */
178     context_index = SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)>>2;
179     /* FIXME: Ick! Why use abstract "make_fixnum" in some places if
180      * you're going to convert from fixnum by bare >>2 in other
181      * places? Use fixnum_value(..) here, and look for other places
182      * which do bare >> and << for fixnum_value and make_fixnum. */
183
184     if (context_index >= MAX_INTERRUPTS) {
185         lose("maximum interrupt nesting depth (%d) exceeded",
186              MAX_INTERRUPTS);
187     }
188
189     bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,
190                   make_fixnum(context_index + 1));
191
192     lisp_interrupt_contexts[context_index] = context;
193
194     /* no longer in Lisp now */
195     foreign_function_call_active = 1;
196 }
197
198 void
199 undo_fake_foreign_function_call(os_context_t *context)
200 {
201     /* Block all blockable signals. */
202     sigset_t block;
203     sigemptyset(&block);
204     sigaddset_blockable(&block);
205     sigprocmask(SIG_BLOCK, &block, 0);
206
207     /* going back into Lisp */
208     foreign_function_call_active = 0;
209
210     /* Undo dynamic binding. */
211     /* ### Do I really need to unbind_to_here()? */
212     /* FIXME: Is this to undo the binding of
213      * FREE_INTERRUPT_CONTEXT_INDEX? If so, we should say so. And
214      * perhaps yes, unbind_to_here() really would be clearer and less
215      * fragile.. */
216     unbind();
217
218 #ifdef reg_ALLOC
219     /* Put the dynamic space free pointer back into the context. */
220     *os_context_register_addr(context, reg_ALLOC) =
221         (unsigned long) dynamic_space_free_pointer;
222 #endif
223 }
224
225 /* a handler for the signal caused by execution of a trap opcode
226  * signalling an internal error */
227 void
228 interrupt_internal_error(int signal, siginfo_t *info, os_context_t *context,
229                          boolean continuable)
230 {
231     lispobj context_sap = 0;
232
233     fake_foreign_function_call(context);
234
235     /* Allocate the SAP object while the interrupts are still
236      * disabled. */
237     if (internal_errors_enabled) {
238         context_sap = alloc_sap(context);
239     }
240
241     sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
242
243     if (internal_errors_enabled) {
244         SHOW("in interrupt_internal_error");
245 #if QSHOW
246         /* Display some rudimentary debugging information about the
247          * error, so that even if the Lisp error handler gets badly
248          * confused, we have a chance to determine what's going on. */
249         describe_internal_error(context);
250 #endif
251         funcall2(SymbolFunction(INTERNAL_ERROR), context_sap,
252                  continuable ? T : NIL);
253     } else {
254         describe_internal_error(context);
255         /* There's no good way to recover from an internal error
256          * before the Lisp error handling mechanism is set up. */
257         lose("internal error too early in init, can't recover");
258     }
259     undo_fake_foreign_function_call(context);
260     if (continuable) {
261         arch_skip_instruction(context);
262     }
263 }
264
265 /* This function handles pending interrupts.  Note that in C/kernel
266  * terms we dealt with the signal already; we just haven't decided
267  * whether to call a Lisp handler or do a GC or something like that.
268  * If it helps, you can think of pending_{signal,mask,info} as a
269  * one-element queue of signals that we have acknowledged but not
270  * processed */
271
272 void
273 interrupt_handle_pending(os_context_t *context)
274 {
275 #ifndef __i386__
276     boolean were_in_lisp = !foreign_function_call_active;
277 #endif
278
279     SetSymbolValue(INTERRUPT_PENDING, NIL);
280
281     if (maybe_gc_pending) {
282         maybe_gc_pending = 0;
283 #ifndef __i386__
284         if (were_in_lisp)
285 #endif
286         {
287             fake_foreign_function_call(context);
288         }
289         funcall0(SymbolFunction(MAYBE_GC));
290 #ifndef __i386__
291         if (were_in_lisp)
292 #endif
293         {
294             undo_fake_foreign_function_call(context);
295         }
296     }
297
298     /* FIXME: This isn't very clear. It would be good to reverse
299      * engineer it and rewrite the code more clearly, or write a clear
300      * explanation of what's going on in the comments, or both.
301      *
302      * WHN's question 1a: How come we unconditionally copy from
303      * pending_mask into the context, and then test whether
304      * pending_signal is set?
305      * 
306      * WHN's question 1b: If pending_signal wasn't set, how could
307      * pending_mask be valid?
308      * 
309      * Dan Barlow's reply (sbcl-devel 2001-03-13): And the answer is -
310      * or appears to be - because interrupt_maybe_gc set it that way
311      * (look in the #ifndef __i386__ bit). We can't GC during a
312      * pseudo-atomic, so we set maybe_gc_pending=1 and
313      * arch_set_pseudo_atomic_interrupted(..) When we come out of
314      * pseudo_atomic we're marked as interrupted, so we call
315      * interrupt_handle_pending, which does the GC using the pending
316      * context (it needs a context so that it has registers to use as
317      * GC roots) then notices there's no actual interrupt handler to
318      * call, so doesn't. That's the second question [1b] answered,
319      * anyway. Why we still need to copy the pending_mask into the
320      * context given that we're now done with the context anyway, I
321      * couldn't say. */
322     memcpy(os_context_sigmask_addr(context), &pending_mask, sizeof(sigset_t));
323     sigemptyset(&pending_mask);
324     if (pending_signal) {
325         int signal = pending_signal;
326         siginfo_t info;
327         memcpy(&info, &pending_info, sizeof(siginfo_t));
328         pending_signal = 0;
329         interrupt_handle_now(signal, &info, context);
330     }
331 }
332 \f
333 /*
334  * the two main signal handlers:
335  *   interrupt_handle_now(..)
336  *   maybe_now_maybe_later(..)
337  */
338
339 void
340 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
341 {
342     os_context_t *context = (os_context_t*)void_context;
343 #ifndef __i386__
344     boolean were_in_lisp;
345 #endif
346     union interrupt_handler handler;
347
348     /* FIXME: The CMU CL we forked off of had this Linux-only
349      * operation here. Newer CMU CLs (e.g. 18c) have hairier
350      * Linux/i386-only logic here. SBCL seems to be more reliable
351      * without anything here. However, if we start supporting code
352      * which sets the rounding mode, then we may want to do something
353      * special to force the rounding mode back to some standard value
354      * here, so that ISRs can have a standard environment. (OTOH, if
355      * rounding modes are under user control, then perhaps we should
356      * leave this up to the user.)
357      *
358      * In the absence of a test case to show that this is really a
359      * problem, we just suppress this code completely (just like the
360      * parallel code in maybe_now_maybe_later).
361      * #ifdef __linux__
362      *    SET_FPU_CONTROL_WORD(context->__fpregs_mem.cw);
363      * #endif */
364
365     handler = interrupt_handlers[signal];
366
367     if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
368         return;
369     }
370
371 #ifndef __i386__
372     were_in_lisp = !foreign_function_call_active;
373     if (were_in_lisp)
374 #endif
375     {
376         fake_foreign_function_call(context);
377     }
378
379 #ifdef QSHOW_SIGNALS
380     FSHOW((stderr,
381            "/entering interrupt_handle_now(%d, info, context)\n",
382            signal));
383 #endif
384
385     if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
386
387         /* This can happen if someone tries to ignore or default one
388          * of the signals we need for runtime support, and the runtime
389          * support decides to pass on it. */
390         lose("no handler for signal %d in interrupt_handle_now(..)", signal);
391
392     } else if (LowtagOf(handler.lisp) == type_FunctionPointer) {
393
394         /* Allocate the SAPs while the interrupts are still disabled.
395          * (FIXME: Why? This is the way it was done in CMU CL, and it
396          * even had the comment noting that this is the way it was
397          * done, but no motivation..) */
398         lispobj info_sap,context_sap = alloc_sap(context);
399         info_sap = alloc_sap(info);
400         /* Allow signals again. */
401         sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
402
403 #ifdef QSHOW_SIGNALS
404         SHOW("calling Lisp-level handler");
405 #endif
406
407         funcall3(handler.lisp,
408                  make_fixnum(signal),
409                  info_sap,
410                  context_sap);
411     } else {
412
413 #ifdef QSHOW_SIGNALS
414         SHOW("calling C-level handler");
415 #endif
416
417         /* Allow signals again. */
418         sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
419         
420         (*handler.c)(signal, info, void_context);
421     }
422
423 #ifndef __i386__
424     if (were_in_lisp)
425 #endif
426     {
427         undo_fake_foreign_function_call(context);
428     }
429
430 #ifdef QSHOW_SIGNALS
431     FSHOW((stderr,
432            "/returning from interrupt_handle_now(%d, info, context)\n",
433            signal));
434 #endif
435 }
436
437 static void
438 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
439 {
440     os_context_t *context = (os_context_t*)void_context;
441
442     /* FIXME: See Debian cmucl 2.4.17, and mail from DTC on the CMU CL
443      * mailing list 23 Oct 1999, for changes in FPU handling at
444      * interrupt time which should be ported into SBCL. Also see the
445      * analogous logic at the head of interrupt_handle_now for
446      * more related FIXME stuff. 
447      *
448      * For now, we just suppress this code completely.
449      * #ifdef __linux__
450      *    SET_FPU_CONTROL_WORD(context->__fpregs_mem.cw);
451      * #endif */
452
453     /* see comments at top of code/signal.lisp for what's going on here
454      * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW 
455      */
456     if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
457
458         /* FIXME: This code is exactly the same as the code in the
459          * other leg of the if(..), and should be factored out into
460          * a shared function. */
461         pending_signal = signal;
462         memcpy(&pending_info, info, sizeof(siginfo_t));
463         memcpy(&pending_mask,
464                os_context_sigmask_addr(context),
465                sizeof(sigset_t));
466         sigaddset_blockable(os_context_sigmask_addr(context));
467         SetSymbolValue(INTERRUPT_PENDING, T);
468
469     } else if (
470 #ifndef __i386__
471                (!foreign_function_call_active) &&
472 #endif
473                arch_pseudo_atomic_atomic(context)) {
474
475         /* FIXME: It would probably be good to replace these bare
476          * memcpy(..) calls with calls to cpy_siginfo_t and
477          * cpy_sigset_t, so that we only have to get the sizeof
478          * expressions right in one place, and after that static type
479          * checking takes over. */
480         pending_signal = signal;
481         memcpy(&pending_info, info, sizeof(siginfo_t));
482         memcpy(&pending_mask,
483                os_context_sigmask_addr(context),
484                sizeof(sigset_t));
485         sigaddset_blockable(os_context_sigmask_addr(context));
486
487         arch_set_pseudo_atomic_interrupted(context);
488
489     } else {
490         interrupt_handle_now(signal, info, context);
491     }
492 }
493 \f
494 /*
495  * stuff to detect and handle hitting the GC trigger
496  */
497
498 #ifndef INTERNAL_GC_TRIGGER
499 static boolean
500 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
501 {
502     if (current_auto_gc_trigger == NULL)
503         return 0;
504     else{
505         lispobj *badaddr=(lispobj *)arch_get_bad_addr(signal,
506                                                       info,
507                                                       context);
508
509         return (badaddr >= current_auto_gc_trigger &&
510                 badaddr < current_dynamic_space + DYNAMIC_SPACE_SIZE);
511     }
512 }
513 #endif
514
515 #ifndef __i386__
516 /* This function gets called from the SIGSEGV (for e.g. Linux or
517  * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
518  * whether the signal was due to treading on the mprotect()ed zone -
519  * and if so, arrange for a GC to happen. */
520 boolean
521 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
522 {
523     os_context_t *context=(os_context_t *) void_context;
524
525     if (!foreign_function_call_active
526 #ifndef INTERNAL_GC_TRIGGER
527         && gc_trigger_hit(signal, info, context)
528 #endif
529         ) {
530 #ifndef INTERNAL_GC_TRIGGER
531         clear_auto_gc_trigger();
532 #endif
533
534         if (arch_pseudo_atomic_atomic(context)) {
535             /* don't GC during an atomic operation.  Instead, copy the 
536              * signal mask somewhere safe.  interrupt_handle_pending
537              * will detect pending_signal==0 and know to do a GC with the
538              * signal context instead of calling a Lisp-level handler */
539             maybe_gc_pending = 1;
540             if (pending_signal == 0) {
541                 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
542                  * idiom occurs over and over. It should be factored out
543                  * into a function with a descriptive name. */
544                 memcpy(&pending_mask,
545                        os_context_sigmask_addr(context),
546                        sizeof(sigset_t));
547                 sigaddset_blockable(os_context_sigmask_addr(context));
548             }
549             arch_set_pseudo_atomic_interrupted(context);
550         }
551         else {
552             fake_foreign_function_call(context);
553             funcall0(SymbolFunction(MAYBE_GC));
554             undo_fake_foreign_function_call(context);
555         }
556
557         return 1;
558     } else {
559         return 0;
560     }
561 }
562 #endif
563 \f
564 /*
565  * noise to install handlers
566  */
567
568 /*
569  * what low-level signal handlers looked like before
570  * undoably_install_low_level_interrupt_handler() got involved
571  */
572 struct low_level_signal_handler_state {
573     int was_modified;
574     void (*handler)(int, siginfo_t*, void*);
575 } old_low_level_signal_handler_states[NSIG];
576
577 void
578 uninstall_low_level_interrupt_handlers_atexit(void)
579 {
580     int signal;
581     for (signal = 0; signal < NSIG; ++signal) {
582         struct low_level_signal_handler_state
583             *old_low_level_signal_handler_state =
584             old_low_level_signal_handler_states + signal;
585         if (old_low_level_signal_handler_state->was_modified) {
586             struct sigaction sa;
587             sa.sa_sigaction = old_low_level_signal_handler_state->handler;
588             sigemptyset(&sa.sa_mask);
589             sa.sa_flags = SA_SIGINFO | SA_RESTART; 
590             sigaction(signal, &sa, NULL);
591         }
592     }
593 }
594
595 /* Undoably install a special low-level handler for signal; or if
596  * handler is SIG_DFL, remove any special handling for signal.
597  *
598  * The "undoably" aspect is because we also arrange with atexit() for
599  * the handler to be restored to its old value. This is for tidiness:
600  * it shouldn't matter much ordinarily, but it does remove a window
601  * where e.g. memory fault signals (SIGSEGV or SIGBUS, which in
602  * ordinary operation of SBCL are sent to the generational garbage
603  * collector, then possibly onward to Lisp code) or SIGINT (which is
604  * ordinarily passed to Lisp code) could otherwise be handled
605  * bizarrely/brokenly because the Lisp code would try to deal with
606  * them using machinery (like stream output buffers) which has already
607  * been dismantled. */
608 void
609 undoably_install_low_level_interrupt_handler (int signal,
610                                               void handler(int,
611                                                            siginfo_t*,
612                                                            void*))
613 {
614     struct sigaction sa;
615     struct low_level_signal_handler_state *old_low_level_signal_handler_state =
616         old_low_level_signal_handler_states + signal;
617
618     if (0 > signal || signal >= NSIG) {
619         lose("bad signal number %d", signal);
620     }
621
622     sa.sa_sigaction = handler;
623     sigemptyset(&sa.sa_mask);
624     sigaddset_blockable(&sa.sa_mask);
625     sa.sa_flags = SA_SIGINFO | SA_RESTART;
626
627     /* In the case of interrupt handlers which are modified
628      * more than once, we only save the original unmodified
629      * copy. */
630     if (!old_low_level_signal_handler_state->was_modified) {
631         old_low_level_signal_handler_state->was_modified = 1;
632         sigaction(signal, &sa, &old_low_level_signal_handler_state->handler);
633     } else {
634         sigaction(signal, &sa, NULL);
635     }
636
637     interrupt_low_level_handlers[signal] =
638         (ARE_SAME_HANDLER(handler,SIG_DFL) ? 0 : handler);
639 }
640
641 /* This is called from Lisp. */
642 unsigned long
643 install_handler(int signal, void handler(int, siginfo_t*, void*))
644 {
645     struct sigaction sa;
646     sigset_t old, new;
647     union interrupt_handler oldhandler;
648
649     FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
650
651     sigemptyset(&new);
652     sigaddset(&new, signal);
653     sigprocmask(SIG_BLOCK, &new, &old);
654
655     sigemptyset(&new);
656     sigaddset_blockable(&new);
657
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;
668         }
669
670         sigemptyset(&sa.sa_mask);
671         sigaddset_blockable(&sa.sa_mask);
672         sa.sa_flags = SA_SIGINFO | SA_RESTART;
673
674         sigaction(signal, &sa, NULL);
675     }
676
677     oldhandler = interrupt_handlers[signal];
678     interrupt_handlers[signal].c = handler;
679
680     sigprocmask(SIG_SETMASK, &old, 0);
681
682     FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
683
684     return (unsigned long)oldhandler.lisp;
685 }
686
687 void
688 interrupt_init(void)
689 {
690     int i;
691
692     SHOW("entering interrupt_init()");
693
694     /* Set up for recovery from any installed low-level handlers. */
695     atexit(&uninstall_low_level_interrupt_handlers_atexit);
696
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;
706     }
707
708     SHOW("returning from interrupt_init()");
709 }