1.0.25.36: unblock signals on low level errors
[sbcl.git] / src / runtime / interrupt.h
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
5  * This software is derived from the CMU CL system, which was
6  * written at Carnegie Mellon University and released into the
7  * public domain. The software is in the public domain and is
8  * provided with absolutely no warranty. See the COPYING and CREDITS
9  * files for more information.
10  */
11
12 #if !defined(_INCLUDE_INTERRUPT_H_)
13 #define _INCLUDE_INTERRUPT_H_
14
15 #include <signal.h>
16 #include <string.h>
17
18 /*
19  * This is a workaround for some slightly silly Linux/GNU Libc
20  * behaviour: glibc defines sigset_t to support 1024 signals, which is
21  * more than the kernel.  This is usually not a problem, but becomes
22  * one when we want to save a signal mask from a ucontext, and restore
23  * it later into another ucontext: the ucontext is allocated on the
24  * stack by the kernel, so copying a libc-sized sigset_t into it will
25  * overflow and cause other data on the stack to be corrupted */
26 /* FIXME: do not rely on NSIG being a multiple of 8 */
27 #define REAL_SIGSET_SIZE_BYTES ((NSIG/8))
28
29 /* Set all deferrable signals into *s. */
30 extern void sigaddset_deferrable(sigset_t *s);
31 /* Set all blockable signals into *s. */
32 extern void sigaddset_blockable(sigset_t *s);
33 /* Set all gc signals into *s. */
34 extern void sigaddset_gc(sigset_t *s);
35
36 extern sigset_t deferrable_sigset;
37 extern sigset_t blockable_sigset;
38 extern sigset_t gc_sigset;
39
40 extern void block_blockable_signals(void);
41 extern void unblock_deferrable_signals(void);
42 extern void unblock_gc_signals(void);
43 extern void unblock_signals_in_context_and_maybe_warn(os_context_t *context);
44
45 extern void check_deferrables_blocked_or_lose(void);
46 extern void check_blockables_blocked_or_lose(void);
47 extern void check_gc_signals_unblocked_or_lose(void);
48 extern void check_gc_signals_unblocked_in_sigset_or_lose(sigset_t *sigset);
49
50 static inline void
51 sigcopyset(sigset_t *new, sigset_t *old)
52 {
53     memcpy(new, old, REAL_SIGSET_SIZE_BYTES);
54 }
55
56 /* maximum signal nesting depth
57  *
58  * FIXME: In CMUCL this was 4096, and it was first scaled down to 256
59  * and then 32, until finally Stumpwm broke: it is possible to receive
60  * interrupts in sufficiently quick succession that handler nesting
61  * can become preposterous. Scaling this back up to 1024 is a bandaid:
62  * for a real fix we should consider the following things:
63  *
64  *   We should almost certainly always use
65  *   arrange_return_to_lisp_function, though it needs to be thought
66  *   about arguments, and it needs to be able to pass a frobbable
67  *   context to the callee...
68  *
69  *   There are cases when nesting handlers is exactly what we want:
70  *   eg. SIGINT.
71  *
72  *   There are cases when we probably want to drop duplicate signals
73  *   on the floor if they arrive before the previous one has been handled.
74  *   Eg. SIGPROF.
75  *
76  *   There are cases when we probably want to handle duplicate signals
77  *   after the previous handler has returned, not before. Eg. SIGALARM.
78  *
79  * -- NS 2007-01-29
80  */
81 #define MAX_INTERRUPTS 1024
82
83 union interrupt_handler {
84     lispobj lisp;
85     void (*c)(int, siginfo_t*, void*);
86 };
87
88 extern union interrupt_handler interrupt_handlers[NSIG];
89
90 struct interrupt_data {
91     /* signal information for pending signal.  pending_signal=0 when there
92      * is no pending signal. */
93     void (*pending_handler) (int, siginfo_t*, void*) ;
94     int pending_signal;
95     siginfo_t pending_info;
96     sigset_t pending_mask;
97 };
98
99 extern boolean interrupt_handler_pending_p(void);
100 extern void interrupt_init(void);
101 extern void fake_foreign_function_call(os_context_t* context);
102 extern void undo_fake_foreign_function_call(os_context_t* context);
103 extern void arrange_return_to_lisp_function(os_context_t *, lispobj);
104 extern void interrupt_handle_now(int, siginfo_t*, os_context_t*);
105 extern void interrupt_handle_pending(os_context_t*);
106 extern void interrupt_internal_error(os_context_t*, boolean continuable);
107 extern boolean handle_guard_page_triggered(os_context_t *,os_vm_address_t);
108 extern boolean maybe_defer_handler(void *handler, struct interrupt_data *data,
109                                    int signal, siginfo_t *info,
110                                    os_context_t *context);
111 #if defined LISP_FEATURE_GENCGC
112 /* assembly language stub that executes trap_PendingInterrupt */
113 extern void do_pending_interrupt(void);
114 #endif
115
116 #ifdef LISP_FEATURE_SB_THREAD
117 extern void interrupt_thread_handler(int, siginfo_t*, void*);
118 extern void sig_stop_for_gc_handler(int, siginfo_t*, void*);
119 #endif
120 typedef void (*interrupt_handler_t)(int, siginfo_t *, void *);
121 extern void undoably_install_low_level_interrupt_handler (
122                         int signal,
123                         interrupt_handler_t handler);
124 extern unsigned long install_handler(int signal,
125                                      interrupt_handler_t handler);
126
127 extern union interrupt_handler interrupt_handlers[NSIG];
128
129 /* The void* casting here avoids having to mess with the various types
130  * of function argument lists possible for signal handlers:
131  * SA_SIGACTION handlers have one signature, and the default old-style
132  * signal(..) handlers have another, and attempting to represent them
133  * "cleanly" with union types is in fact a mess. */
134 #define ARE_SAME_HANDLER(x, y) ((void*)(x) == (void*)(y))
135
136 extern void handle_trap(os_context_t *context, int trap);
137
138 #ifndef LISP_FEATURE_WIN32
139 extern void lisp_memory_fault_error(os_context_t *context,
140                                     os_vm_address_t addr);
141 #endif
142
143 #endif