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