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