0.7.6.12:
[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  * to which we have added interrupt_handle_now_handler(..).  Why?
347  * Well, mostly because the SPARC/Linux platform doesn't quite do
348  * signals the way we want them done.  The third argument in the
349  * handler isn't filled in by the kernel properly, so we fix it up
350  * ourselves in the arch_os_get_context(..) function; however, we only
351  * want to do this when we first hit the handler, and not when
352  * interrupt_handle_now(..) is being called from some other handler
353  * (when the fixup will already have been done). -- CSR, 2002-07-23
354  */
355
356 void
357 interrupt_handle_now(int signal, siginfo_t *info, void *void_context)
358 {
359     os_context_t *context = (os_context_t*)void_context;
360 #ifndef __i386__
361     boolean were_in_lisp;
362 #endif
363     union interrupt_handler handler;
364
365 #ifdef LISP_FEATURE_LINUX
366     /* Under Linux on some architectures, we appear to have to restore
367        the FPU control word from the context, as after the signal is
368        delivered we appear to have a null FPU control word. */
369     os_restore_fp_control(context);
370 #endif 
371     handler = interrupt_handlers[signal];
372
373     if (ARE_SAME_HANDLER(handler.c, SIG_IGN)) {
374         return;
375     }
376     
377 #ifndef __i386__
378     were_in_lisp = !foreign_function_call_active;
379     if (were_in_lisp)
380 #endif
381     {
382         fake_foreign_function_call(context);
383     }
384
385 #ifdef QSHOW_SIGNALS
386     FSHOW((stderr,
387            "/entering interrupt_handle_now(%d, info, context)\n",
388            signal));
389 #endif
390
391     if (ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
392
393         /* This can happen if someone tries to ignore or default one
394          * of the signals we need for runtime support, and the runtime
395          * support decides to pass on it. */
396         lose("no handler for signal %d in interrupt_handle_now(..)", signal);
397
398     } else if (lowtag_of(handler.lisp) == FUN_POINTER_LOWTAG) {
399
400         /* Allocate the SAPs while the interrupts are still disabled.
401          * (FIXME: Why? This is the way it was done in CMU CL, and it
402          * even had the comment noting that this is the way it was
403          * done, but no motivation..) */
404         lispobj info_sap,context_sap = alloc_sap(context);
405         info_sap = alloc_sap(info);
406         /* Allow signals again. */
407         sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
408
409 #ifdef QSHOW_SIGNALS
410         SHOW("calling Lisp-level handler");
411 #endif
412
413         funcall3(handler.lisp,
414                  make_fixnum(signal),
415                  info_sap,
416                  context_sap);
417     } else {
418
419 #ifdef QSHOW_SIGNALS
420         SHOW("calling C-level handler");
421 #endif
422
423         /* Allow signals again. */
424         sigprocmask(SIG_SETMASK, os_context_sigmask_addr(context), 0);
425         
426         (*handler.c)(signal, info, void_context);
427     }
428
429 #ifndef __i386__
430     if (were_in_lisp)
431 #endif
432     {
433         undo_fake_foreign_function_call(context);
434     }
435
436 #ifdef QSHOW_SIGNALS
437     FSHOW((stderr,
438            "/returning from interrupt_handle_now(%d, info, context)\n",
439            signal));
440 #endif
441 }
442
443 static void
444 maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context)
445 {
446     os_context_t *context = arch_os_get_context(&void_context);
447
448 #ifdef LISP_FEATURE_LINUX
449     os_restore_fp_control(context);
450 #endif 
451     
452     /* see comments at top of code/signal.lisp for what's going on here
453      * with INTERRUPTS_ENABLED/INTERRUPT_HANDLE_NOW 
454      */
455     if (SymbolValue(INTERRUPTS_ENABLED) == NIL) {
456
457         /* FIXME: This code is exactly the same as the code in the
458          * other leg of the if(..), and should be factored out into
459          * a shared function. */
460         pending_signal = signal;
461         memcpy(&pending_info, info, sizeof(siginfo_t));
462         memcpy(&pending_mask,
463                os_context_sigmask_addr(context),
464                sizeof(sigset_t));
465         sigaddset_blockable(os_context_sigmask_addr(context));
466         SetSymbolValue(INTERRUPT_PENDING, T);
467
468     } else if (
469 #ifndef __i386__
470                (!foreign_function_call_active) &&
471 #endif
472                arch_pseudo_atomic_atomic(context)) {
473
474         /* FIXME: It would probably be good to replace these bare
475          * memcpy(..) calls with calls to cpy_siginfo_t and
476          * cpy_sigset_t, so that we only have to get the sizeof
477          * expressions right in one place, and after that static type
478          * checking takes over. */
479         pending_signal = signal;
480         memcpy(&pending_info, info, sizeof(siginfo_t));
481         memcpy(&pending_mask,
482                os_context_sigmask_addr(context),
483                sizeof(sigset_t));
484         sigaddset_blockable(os_context_sigmask_addr(context));
485
486         arch_set_pseudo_atomic_interrupted(context);
487
488     } else {
489         interrupt_handle_now(signal, info, context);
490     }
491 }
492 \f
493
494 void
495 interrupt_handle_now_handler(int signal, siginfo_t *info, void *void_context)
496 {
497     os_context_t *context = arch_os_get_context(&void_context);
498     interrupt_handle_now(signal, info, context);
499 }
500
501 /*
502  * stuff to detect and handle hitting the GC trigger
503  */
504
505 #ifndef LISP_FEATURE_GENCGC 
506 /* since GENCGC has its own way to record trigger */
507 static boolean
508 gc_trigger_hit(int signal, siginfo_t *info, os_context_t *context)
509 {
510     if (current_auto_gc_trigger == NULL)
511         return 0;
512     else{
513         void *badaddr=arch_get_bad_addr(signal,info,context);
514         return (badaddr >= (void *)current_auto_gc_trigger &&
515                 badaddr <((void *)current_dynamic_space + DYNAMIC_SPACE_SIZE));
516     }
517 }
518 #endif
519
520 /* and similarly for the control stack guard page */
521
522 boolean handle_control_stack_guard_triggered(os_context_t *context,void *addr)
523 {
524     /* note the os_context hackery here.  When the signal handler returns, 
525      * it won't go back to what it was doing ... */
526     if(addr>=(void *)CONTROL_STACK_GUARD_PAGE && 
527        addr<(void *)(CONTROL_STACK_GUARD_PAGE+os_vm_page_size)) {
528         void *fun;
529         void *code;
530         
531         /* we hit the end of the control stack.  disable protection
532          * temporarily so the error handler has some headroom */
533         protect_control_stack_guard_page(0);
534         
535         fun = (void *)
536             native_pointer((lispobj) SymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
537         code = &(((struct simple_fun *) fun)->code);
538
539         /* Build a stack frame showing `interrupted' so that the
540          * user's backtrace makes (as much) sense (as usual) */
541         build_fake_control_stack_frames(context);
542         /* signal handler will "return" to this error-causing function */
543         *os_context_pc_addr(context) = code;
544 #ifdef LISP_FEATURE_X86
545         *os_context_register_addr(context,reg_ECX) = 0; 
546 #else
547         /* this much of the calling convention is common to all
548            non-x86 ports */
549         *os_context_register_addr(context,reg_NARGS) = 0; 
550         *os_context_register_addr(context,reg_LIP) = code;
551         *os_context_register_addr(context,reg_CFP) = 
552             current_control_frame_pointer;
553 #endif
554 #ifdef ARCH_HAS_NPC_REGISTER
555         *os_context_npc_addr(context) =
556             4 + *os_context_pc_addr(context);
557 #endif
558 #ifdef LISP_FEATURE_SPARC
559         /* Bletch.  This is a feature of the SPARC calling convention,
560            which sadly I'm not going to go into in large detail here,
561            as I don't know it well enough.  Suffice to say that if the
562            line 
563
564            (INST MOVE CODE-TN FUNCTION) 
565
566            in compiler/sparc/call.lisp is changed, then this bit can
567            probably go away.  -- CSR, 2002-07-24 */
568         *os_context_register_addr(context,reg_CODE) = 
569             fun + FUN_POINTER_LOWTAG;
570 #endif
571         return 1;
572     }
573     else return 0;
574 }
575
576 #ifndef LISP_FEATURE_X86
577 /* This function gets called from the SIGSEGV (for e.g. Linux or
578  * OpenBSD) or SIGBUS (for e.g. FreeBSD) handler. Here we check
579  * whether the signal was due to treading on the mprotect()ed zone -
580  * and if so, arrange for a GC to happen. */
581 boolean
582 interrupt_maybe_gc(int signal, siginfo_t *info, void *void_context)
583 {
584     os_context_t *context=(os_context_t *) void_context;
585
586     if (!foreign_function_call_active
587 #ifndef LISP_FEATURE_GENCGC 
588         /* nb: GENCGC on non-x86?  I really don't think so.  This
589          * happens every time */
590         && gc_trigger_hit(signal, info, context)
591 #endif
592         ) {
593 #ifndef LISP_FEATURE_GENCGC 
594         clear_auto_gc_trigger();
595 #endif
596
597         if (arch_pseudo_atomic_atomic(context)) {
598             /* don't GC during an atomic operation.  Instead, copy the 
599              * signal mask somewhere safe.  interrupt_handle_pending
600              * will detect pending_signal==0 and know to do a GC with the
601              * signal context instead of calling a Lisp-level handler */
602             maybe_gc_pending = 1;
603             if (pending_signal == 0) {
604                 /* FIXME: This copy-pending_mask-then-sigaddset_blockable
605                  * idiom occurs over and over. It should be factored out
606                  * into a function with a descriptive name. */
607                 memcpy(&pending_mask,
608                        os_context_sigmask_addr(context),
609                        sizeof(sigset_t));
610                 sigaddset_blockable(os_context_sigmask_addr(context));
611             }
612             arch_set_pseudo_atomic_interrupted(context);
613         }
614         else {
615             lispobj *old_free_space=current_dynamic_space;
616             fake_foreign_function_call(context);
617             funcall0(SymbolFunction(MAYBE_GC));
618             undo_fake_foreign_function_call(context);
619             if(current_dynamic_space==old_free_space) 
620                 /* MAYBE-GC (as the name suggest) might not.  If it
621                  * doesn't, it won't reset the GC trigger either, so we
622                  * have to do it ourselves.  Put it near the end of
623                  * dynamic space so we're not running into it continually
624                  */
625                 set_auto_gc_trigger(DYNAMIC_SPACE_SIZE
626                                     -(u32)os_vm_page_size);
627         }       
628         return 1;
629     } else {
630         return 0;
631     }
632 }
633 #endif
634 \f
635 /*
636  * noise to install handlers
637  */
638
639 /*
640  * what low-level signal handlers looked like before
641  * undoably_install_low_level_interrupt_handler() got involved
642  */
643 struct low_level_signal_handler_state {
644     int was_modified;
645     void (*handler)(int, siginfo_t*, void*);
646 } old_low_level_signal_handler_states[NSIG];
647
648 void
649 uninstall_low_level_interrupt_handlers_atexit(void)
650 {
651     int signal;
652     for (signal = 0; signal < NSIG; ++signal) {
653         struct low_level_signal_handler_state
654             *old_low_level_signal_handler_state =
655             old_low_level_signal_handler_states + signal;
656         if (old_low_level_signal_handler_state->was_modified) {
657             struct sigaction sa;
658             sa.sa_sigaction = old_low_level_signal_handler_state->handler;
659             sigemptyset(&sa.sa_mask);
660             sa.sa_flags = SA_SIGINFO | SA_RESTART; 
661             sigaction(signal, &sa, NULL);
662         }
663     }
664 }
665
666 /* Undoably install a special low-level handler for signal; or if
667  * handler is SIG_DFL, remove any special handling for signal.
668  *
669  * The "undoably" aspect is because we also arrange with atexit() for
670  * the handler to be restored to its old value. This is for tidiness:
671  * it shouldn't matter much ordinarily, but it does remove a window
672  * where e.g. memory fault signals (SIGSEGV or SIGBUS, which in
673  * ordinary operation of SBCL are sent to the generational garbage
674  * collector, then possibly onward to Lisp code) or SIGINT (which is
675  * ordinarily passed to Lisp code) could otherwise be handled
676  * bizarrely/brokenly because the Lisp code would try to deal with
677  * them using machinery (like stream output buffers) which has already
678  * been dismantled. */
679 void
680 undoably_install_low_level_interrupt_handler (int signal,
681                                               void handler(int,
682                                                            siginfo_t*,
683                                                            void*))
684 {
685     struct sigaction sa;
686     struct low_level_signal_handler_state *old_low_level_signal_handler_state =
687         old_low_level_signal_handler_states + signal;
688
689     if (0 > signal || signal >= NSIG) {
690         lose("bad signal number %d", signal);
691     }
692
693     sa.sa_sigaction = handler;
694     sigemptyset(&sa.sa_mask);
695     sigaddset_blockable(&sa.sa_mask);
696     sa.sa_flags = SA_SIGINFO | SA_RESTART;
697 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
698     /* Signal handlers are run on the control stack, so if it is exhausted
699      * we had better use an alternate stack for whatever signal tells us
700      * we've exhausted it */
701     if(signal==SIG_MEMORY_FAULT) {
702         stack_t sigstack;
703         sigstack.ss_sp=(void *) ALTERNATE_SIGNAL_STACK_START;
704         sigstack.ss_flags=0;
705         sigstack.ss_size = SIGSTKSZ;
706         sigaltstack(&sigstack,0);
707         sa.sa_flags|=SA_ONSTACK;
708     }
709 #endif
710     
711     /* In the case of interrupt handlers which are modified more than
712      * once, we only save the original unmodified copy. */
713     if (!old_low_level_signal_handler_state->was_modified) {
714         struct sigaction *old_handler =
715             (struct sigaction*) &old_low_level_signal_handler_state->handler;
716         old_low_level_signal_handler_state->was_modified = 1;
717         sigaction(signal, &sa, old_handler);
718     } else {
719         sigaction(signal, &sa, NULL);
720     }
721
722     interrupt_low_level_handlers[signal] =
723         (ARE_SAME_HANDLER(handler, SIG_DFL) ? 0 : handler);
724 }
725
726 /* This is called from Lisp. */
727 unsigned long
728 install_handler(int signal, void handler(int, siginfo_t*, void*))
729 {
730     struct sigaction sa;
731     sigset_t old, new;
732     union interrupt_handler oldhandler;
733
734     FSHOW((stderr, "/entering POSIX install_handler(%d, ..)\n", signal));
735
736     sigemptyset(&new);
737     sigaddset(&new, signal);
738     sigprocmask(SIG_BLOCK, &new, &old);
739
740     sigemptyset(&new);
741     sigaddset_blockable(&new);
742
743     FSHOW((stderr, "/interrupt_low_level_handlers[signal]=%d\n",
744            interrupt_low_level_handlers[signal]));
745     if (interrupt_low_level_handlers[signal]==0) {
746         if (ARE_SAME_HANDLER(handler, SIG_DFL) ||
747             ARE_SAME_HANDLER(handler, SIG_IGN)) {
748             sa.sa_sigaction = handler;
749         } else if (sigismember(&new, signal)) {
750             sa.sa_sigaction = maybe_now_maybe_later;
751         } else {
752             sa.sa_sigaction = interrupt_handle_now_handler;
753         }
754
755         sigemptyset(&sa.sa_mask);
756         sigaddset_blockable(&sa.sa_mask);
757         sa.sa_flags = SA_SIGINFO | SA_RESTART;
758
759         sigaction(signal, &sa, NULL);
760     }
761
762     oldhandler = interrupt_handlers[signal];
763     interrupt_handlers[signal].c = handler;
764
765     sigprocmask(SIG_SETMASK, &old, 0);
766
767     FSHOW((stderr, "/leaving POSIX install_handler(%d, ..)\n", signal));
768
769     return (unsigned long)oldhandler.lisp;
770 }
771
772 void
773 interrupt_init(void)
774 {
775     int i;
776
777     SHOW("entering interrupt_init()");
778
779     /* Set up for recovery from any installed low-level handlers. */
780     atexit(&uninstall_low_level_interrupt_handlers_atexit);
781
782     /* Set up high level handler information. */
783     for (i = 0; i < NSIG; i++) {
784         interrupt_handlers[i].c =
785             /* (The cast here blasts away the distinction between
786              * SA_SIGACTION-style three-argument handlers and
787              * signal(..)-style one-argument handlers, which is OK
788              * because it works to call the 1-argument form where the
789              * 3-argument form is expected.) */
790             (void (*)(int, siginfo_t*, void*))SIG_DFL;
791     }
792
793     SHOW("returning from interrupt_init()");
794 }