a27eb08fc1f42ae8d52541b91942419757c3bc98
[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 "runtime.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
28 #ifdef LISP_FEATURE_WIN32
29 # define REAL_SIGSET_SIZE_BYTES (4)
30 #else
31 # define REAL_SIGSET_SIZE_BYTES ((NSIG/8))
32 #endif
33
34 static inline void
35 sigcopyset(sigset_t *new, sigset_t *old)
36 {
37     memcpy(new, old, REAL_SIGSET_SIZE_BYTES);
38 }
39
40 extern void get_current_sigmask(sigset_t *sigset);
41
42 /* Set all deferrable signals into *s. */
43 extern void sigaddset_deferrable(sigset_t *s);
44 /* Set all blockable signals into *s. */
45 extern void sigaddset_blockable(sigset_t *s);
46 /* Set all gc signals into *s. */
47 extern void sigaddset_gc(sigset_t *s);
48
49 extern sigset_t deferrable_sigset;
50 extern sigset_t blockable_sigset;
51 extern sigset_t gc_sigset;
52
53 extern boolean deferrables_blocked_p(sigset_t *sigset);
54 extern boolean blockables_blocked_p(sigset_t *sigset);
55 extern boolean gc_signals_blocked_p(sigset_t *sigset);
56
57 extern void check_deferrables_blocked_or_lose(sigset_t *sigset);
58 extern void check_blockables_blocked_or_lose(sigset_t *sigset);
59 extern void check_gc_signals_blocked_or_lose(sigset_t *sigset);
60
61 extern void check_deferrables_unblocked_or_lose(sigset_t *sigset);
62 extern void check_blockables_unblocked_or_lose(sigset_t *sigset);
63 extern void check_gc_signals_unblocked_or_lose(sigset_t *sigset);
64
65 extern void block_deferrable_signals(sigset_t *where, sigset_t *old);
66 extern void block_blockable_signals(sigset_t *where, sigset_t *old);
67 extern void block_gc_signals(sigset_t *where, sigset_t *old);
68
69 extern void unblock_deferrable_signals(sigset_t *where, sigset_t *old);
70 extern void unblock_blockable_signals(sigset_t *where, sigset_t *old);
71 extern void unblock_gc_signals(sigset_t *where, sigset_t *old);
72
73 extern void maybe_save_gc_mask_and_block_deferrables(sigset_t *sigset);
74
75 /* maximum signal nesting depth
76  *
77  * FIXME: In CMUCL this was 4096, and it was first scaled down to 256
78  * and then 32, until finally Stumpwm broke: it is possible to receive
79  * interrupts in sufficiently quick succession that handler nesting
80  * can become preposterous. Scaling this back up to 1024 is a bandaid:
81  * for a real fix we should consider the following things:
82  *
83  *   We should almost certainly always use
84  *   arrange_return_to_lisp_function, though it needs to be thought
85  *   about arguments, and it needs to be able to pass a frobbable
86  *   context to the callee...
87  *
88  *   There are cases when nesting handlers is exactly what we want:
89  *   eg. SIGINT.
90  *
91  *   There are cases when we probably want to drop duplicate signals
92  *   on the floor if they arrive before the previous one has been handled.
93  *   Eg. SIGPROF.
94  *
95  *   There are cases when we probably want to handle duplicate signals
96  *   after the previous handler has returned, not before. Eg. SIGALARM.
97  *
98  * -- NS 2007-01-29
99  */
100 #define MAX_INTERRUPTS 1024
101
102 union interrupt_handler {
103     lispobj lisp;
104     void (*c)(int, siginfo_t*, os_context_t*);
105 };
106
107 extern union interrupt_handler interrupt_handlers[NSIG];
108
109 struct interrupt_data {
110     /* signal information for pending signal.  pending_signal=0 when there
111      * is no pending signal. */
112     void (*pending_handler) (int, siginfo_t*, os_context_t*) ;
113     int pending_signal;
114     siginfo_t pending_info;
115     sigset_t pending_mask;
116     /* Was pending mask saved for gc request? True if GC_PENDING or
117      * SIG_STOP_FOR_GC happened in a pseudo atomic with GC_INHIBIT NIL
118      * and with no pending handler. Both deferrable interrupt handlers
119      * and gc are careful not to clobber each other's pending_mask. */
120     boolean gc_blocked_deferrables;
121 #ifdef GENCGC_IS_PRECISE
122     /* On PPC when consing wants to turn to alloc(), it does so via a
123      * trap. When alloc() wants to save the sigmask it consults
124      * allocation_trap_context. It does not look up the most recent
125      * context, because alloc() can be called from other places
126      * too. */
127     os_context_t *allocation_trap_context;
128 #endif
129 };
130
131 typedef lispobj (*call_into_lisp_lookalike)(
132     lispobj fun, lispobj *args, int nargs);
133
134 extern boolean interrupt_handler_pending_p(void);
135 extern void interrupt_init(void);
136 extern void fake_foreign_function_call(os_context_t* context);
137 extern void undo_fake_foreign_function_call(os_context_t* context);
138 extern void arrange_return_to_c_function(
139     os_context_t *, call_into_lisp_lookalike, lispobj);
140 extern void arrange_return_to_lisp_function(os_context_t *, lispobj);
141 extern void interrupt_handle_now(int, siginfo_t*, os_context_t*);
142 extern void interrupt_handle_pending(os_context_t*);
143 extern void interrupt_internal_error(os_context_t*, boolean continuable);
144 extern boolean handle_guard_page_triggered(os_context_t *,os_vm_address_t);
145 extern boolean maybe_defer_handler(void *handler, struct interrupt_data *data,
146                                    int signal, siginfo_t *info,
147                                    os_context_t *context);
148 #if defined LISP_FEATURE_GENCGC
149 /* assembly language stub that executes trap_PendingInterrupt */
150 extern void do_pending_interrupt(void);
151 #endif
152
153 #ifdef LISP_FEATURE_SB_THREAD
154 extern void sig_stop_for_gc_handler(int, siginfo_t*, os_context_t*);
155 #endif
156 typedef void (*interrupt_handler_t)(int, siginfo_t *, os_context_t *);
157 extern void undoably_install_low_level_interrupt_handler (
158                         int signal,
159                         interrupt_handler_t handler);
160 extern uword_t install_handler(int signal,
161                                      interrupt_handler_t handler);
162
163 extern union interrupt_handler interrupt_handlers[NSIG];
164
165 /* The void* casting here avoids having to mess with the various types
166  * of function argument lists possible for signal handlers:
167  * SA_SIGACTION handlers have one signature, and the default old-style
168  * signal(..) handlers have another, and attempting to represent them
169  * "cleanly" with union types is in fact a mess. */
170 #define ARE_SAME_HANDLER(x, y) ((void*)(x) == (void*)(y))
171
172 extern void handle_trap(os_context_t *context, int trap);
173
174 #ifndef LISP_FEATURE_WIN32
175 extern void lisp_memory_fault_error(os_context_t *context,
176                                     os_vm_address_t addr);
177 #endif
178
179 #include "thread.h"
180
181 extern void lower_thread_control_stack_guard_page(struct thread *th);
182 extern void reset_thread_control_stack_guard_page(struct thread *th);
183
184 #if defined(LISP_FEATURE_SB_SAFEPOINT) && !defined(LISP_FEATURE_WIN32)
185 # ifdef LISP_FEATURE_SB_THRUPTION
186 void thruption_handler(int signal, siginfo_t *info, os_context_t *context);
187 # endif
188 #endif
189
190 #endif