]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
2eb5f31b AI |
2 | * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk}) |
3 | * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de) | |
1da177e4 | 4 | * Copyright (C) 2004 PathScale, Inc |
ba180fd4 | 5 | * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
6 | * Licensed under the GPL |
7 | */ | |
8 | ||
0805d89c | 9 | #include <stdlib.h> |
0805d89c | 10 | #include <stdarg.h> |
ba180fd4 JD |
11 | #include <errno.h> |
12 | #include <signal.h> | |
13 | #include <strings.h> | |
37185b33 AV |
14 | #include <as-layout.h> |
15 | #include <kern_util.h> | |
16 | #include <os.h> | |
17 | #include <sysdep/mcontext.h> | |
57a05d83 | 18 | #include <um_malloc.h> |
530ba6c7 | 19 | #include <sys/ucontext.h> |
1da177e4 | 20 | |
72383d43 | 21 | void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = { |
75ada8ff JD |
22 | [SIGTRAP] = relay_signal, |
23 | [SIGFPE] = relay_signal, | |
24 | [SIGILL] = relay_signal, | |
25 | [SIGWINCH] = winch, | |
26 | [SIGBUS] = bus_handler, | |
27 | [SIGSEGV] = segv_handler, | |
28 | [SIGIO] = sigio_handler, | |
2eb5f31b AI |
29 | [SIGALRM] = timer_handler |
30 | }; | |
75ada8ff | 31 | |
9a8c1359 | 32 | static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) |
75ada8ff | 33 | { |
5c2ffce1 | 34 | struct uml_pt_regs r; |
e6a2d1f7 | 35 | int save_errno = errno; |
75ada8ff | 36 | |
5c2ffce1 | 37 | r.is_user = 0; |
75ada8ff | 38 | if (sig == SIGSEGV) { |
e6a2d1f7 | 39 | /* For segfaults, we want the data from the sigcontext. */ |
5c2ffce1 AI |
40 | get_regs_from_mc(&r, mc); |
41 | GET_FAULTINFO_FROM_MC(r.faultinfo, mc); | |
e6a2d1f7 | 42 | } |
75ada8ff | 43 | |
e6a2d1f7 | 44 | /* enable signals if sig isn't IRQ signal */ |
2eb5f31b | 45 | if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGALRM)) |
75ada8ff JD |
46 | unblock_signals(); |
47 | ||
5c2ffce1 | 48 | (*sig_info[sig])(sig, si, &r); |
75ada8ff JD |
49 | |
50 | errno = save_errno; | |
75ada8ff JD |
51 | } |
52 | ||
ba180fd4 | 53 | /* |
61b63c55 | 54 | * These are the asynchronous signals. SIGPROF is excluded because we want to |
1d7173ba JD |
55 | * be able to profile all of UML, not just the non-critical sections. If |
56 | * profiling is not thread-safe, then that is not my problem. We can disable | |
57 | * profiling when SMP is enabled in that case. | |
58 | */ | |
59 | #define SIGIO_BIT 0 | |
60 | #define SIGIO_MASK (1 << SIGIO_BIT) | |
61 | ||
2eb5f31b AI |
62 | #define SIGALRM_BIT 1 |
63 | #define SIGALRM_MASK (1 << SIGALRM_BIT) | |
1d7173ba | 64 | |
fce8c41c | 65 | static int signals_enabled; |
cfef8f34 | 66 | static unsigned int signals_pending; |
d5e3f5cb | 67 | static unsigned int signals_active = 0; |
1d7173ba | 68 | |
9a8c1359 | 69 | void sig_handler(int sig, struct siginfo *si, mcontext_t *mc) |
1da177e4 | 70 | { |
1d7173ba JD |
71 | int enabled; |
72 | ||
1d7173ba | 73 | enabled = signals_enabled; |
ba180fd4 | 74 | if (!enabled && (sig == SIGIO)) { |
cfef8f34 | 75 | signals_pending |= SIGIO_MASK; |
1d7173ba JD |
76 | return; |
77 | } | |
78 | ||
79 | block_signals(); | |
80 | ||
d3c1cfcd | 81 | sig_handler_common(sig, si, mc); |
1d7173ba JD |
82 | |
83 | set_signals(enabled); | |
1da177e4 LT |
84 | } |
85 | ||
2eb5f31b | 86 | static void timer_real_alarm_handler(mcontext_t *mc) |
1da177e4 | 87 | { |
5c2ffce1 | 88 | struct uml_pt_regs regs; |
2ea5bc5e | 89 | |
248b74c7 | 90 | if (mc != NULL) |
5c2ffce1 AI |
91 | get_regs_from_mc(®s, mc); |
92 | timer_handler(SIGALRM, NULL, ®s); | |
1d7173ba JD |
93 | } |
94 | ||
2eb5f31b | 95 | void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc) |
1d7173ba | 96 | { |
1d7173ba JD |
97 | int enabled; |
98 | ||
1d7173ba | 99 | enabled = signals_enabled; |
ba180fd4 | 100 | if (!signals_enabled) { |
2eb5f31b | 101 | signals_pending |= SIGALRM_MASK; |
1d7173ba JD |
102 | return; |
103 | } | |
104 | ||
105 | block_signals(); | |
106 | ||
d5e3f5cb AI |
107 | signals_active |= SIGALRM_MASK; |
108 | ||
2eb5f31b | 109 | timer_real_alarm_handler(mc); |
d5e3f5cb AI |
110 | |
111 | signals_active &= ~SIGALRM_MASK; | |
112 | ||
1d7173ba | 113 | set_signals(enabled); |
1da177e4 LT |
114 | } |
115 | ||
2eb5f31b AI |
116 | void deliver_alarm(void) { |
117 | timer_alarm_handler(SIGALRM, NULL, NULL); | |
118 | } | |
119 | ||
120 | void timer_set_signal_handler(void) | |
78a26e25 | 121 | { |
2eb5f31b | 122 | set_handler(SIGALRM); |
78a26e25 JD |
123 | } |
124 | ||
0805d89c GS |
125 | void set_sigstack(void *sig_stack, int size) |
126 | { | |
9a75551a HWH |
127 | stack_t stack = { |
128 | .ss_flags = 0, | |
129 | .ss_sp = sig_stack, | |
130 | .ss_size = size - sizeof(void *) | |
131 | }; | |
0805d89c | 132 | |
ba180fd4 | 133 | if (sigaltstack(&stack, NULL) != 0) |
0805d89c GS |
134 | panic("enabling signal stack failed, errno = %d\n", errno); |
135 | } | |
136 | ||
9a8c1359 | 137 | static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = { |
00361683 AV |
138 | [SIGSEGV] = sig_handler, |
139 | [SIGBUS] = sig_handler, | |
140 | [SIGILL] = sig_handler, | |
141 | [SIGFPE] = sig_handler, | |
142 | [SIGTRAP] = sig_handler, | |
143 | ||
144 | [SIGIO] = sig_handler, | |
145 | [SIGWINCH] = sig_handler, | |
2eb5f31b | 146 | [SIGALRM] = timer_alarm_handler |
00361683 | 147 | }; |
4b84c69b | 148 | |
d3c1cfcd | 149 | static void hard_handler(int sig, siginfo_t *si, void *p) |
c14b8494 | 150 | { |
4d1a535b | 151 | ucontext_t *uc = p; |
248b74c7 | 152 | mcontext_t *mc = &uc->uc_mcontext; |
508a9274 | 153 | unsigned long pending = 1UL << sig; |
c14b8494 JD |
154 | |
155 | do { | |
156 | int nested, bail; | |
157 | ||
158 | /* | |
159 | * pending comes back with one bit set for each | |
160 | * interrupt that arrived while setting up the stack, | |
161 | * plus a bit for this interrupt, plus the zero bit is | |
162 | * set if this is a nested interrupt. | |
163 | * If bail is true, then we interrupted another | |
164 | * handler setting up the stack. In this case, we | |
165 | * have to return, and the upper handler will deal | |
166 | * with this interrupt. | |
167 | */ | |
508a9274 | 168 | bail = to_irq_stack(&pending); |
ba180fd4 | 169 | if (bail) |
c14b8494 JD |
170 | return; |
171 | ||
172 | nested = pending & 1; | |
173 | pending &= ~1; | |
174 | ||
ba180fd4 | 175 | while ((sig = ffs(pending)) != 0){ |
c14b8494 JD |
176 | sig--; |
177 | pending &= ~(1 << sig); | |
9a8c1359 | 178 | (*handlers[sig])(sig, (struct siginfo *)si, mc); |
c14b8494 JD |
179 | } |
180 | ||
ba180fd4 JD |
181 | /* |
182 | * Again, pending comes back with a mask of signals | |
c14b8494 JD |
183 | * that arrived while tearing down the stack. If this |
184 | * is non-zero, we just go back, set up the stack | |
185 | * again, and handle the new interrupts. | |
186 | */ | |
ba180fd4 | 187 | if (!nested) |
c14b8494 | 188 | pending = from_irq_stack(nested); |
ba180fd4 | 189 | } while (pending); |
c14b8494 JD |
190 | } |
191 | ||
00361683 | 192 | void set_handler(int sig) |
0805d89c GS |
193 | { |
194 | struct sigaction action; | |
e87df986 | 195 | int flags = SA_SIGINFO | SA_ONSTACK; |
1d7173ba | 196 | sigset_t sig_mask; |
0805d89c | 197 | |
7eb12255 | 198 | action.sa_sigaction = hard_handler; |
4b84c69b | 199 | |
e87df986 | 200 | /* block irq ones */ |
0805d89c | 201 | sigemptyset(&action.sa_mask); |
e87df986 AV |
202 | sigaddset(&action.sa_mask, SIGIO); |
203 | sigaddset(&action.sa_mask, SIGWINCH); | |
2eb5f31b | 204 | sigaddset(&action.sa_mask, SIGALRM); |
4b84c69b | 205 | |
e6a2d1f7 JD |
206 | if (sig == SIGSEGV) |
207 | flags |= SA_NODEFER; | |
208 | ||
e87df986 AV |
209 | if (sigismember(&action.sa_mask, sig)) |
210 | flags |= SA_RESTART; /* if it's an irq signal */ | |
211 | ||
212 | action.sa_flags = flags; | |
0805d89c | 213 | action.sa_restorer = NULL; |
ba180fd4 | 214 | if (sigaction(sig, &action, NULL) < 0) |
1d7173ba JD |
215 | panic("sigaction failed - errno = %d\n", errno); |
216 | ||
217 | sigemptyset(&sig_mask); | |
218 | sigaddset(&sig_mask, sig); | |
ba180fd4 | 219 | if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0) |
1d7173ba | 220 | panic("sigprocmask failed - errno = %d\n", errno); |
0805d89c GS |
221 | } |
222 | ||
223 | int change_sig(int signal, int on) | |
224 | { | |
cfef8f34 | 225 | sigset_t sigset; |
0805d89c GS |
226 | |
227 | sigemptyset(&sigset); | |
228 | sigaddset(&sigset, signal); | |
cfef8f34 | 229 | if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0) |
c9a3072d | 230 | return -errno; |
cfef8f34 JD |
231 | |
232 | return 0; | |
0805d89c GS |
233 | } |
234 | ||
0805d89c GS |
235 | void block_signals(void) |
236 | { | |
1d7173ba | 237 | signals_enabled = 0; |
ba180fd4 JD |
238 | /* |
239 | * This must return with signals disabled, so this barrier | |
53b17332 JD |
240 | * ensures that writes are flushed out before the return. |
241 | * This might matter if gcc figures out how to inline this and | |
242 | * decides to shuffle this code into the caller. | |
243 | */ | |
fce8c41c | 244 | barrier(); |
0805d89c GS |
245 | } |
246 | ||
247 | void unblock_signals(void) | |
248 | { | |
1d7173ba | 249 | int save_pending; |
0805d89c | 250 | |
ba180fd4 | 251 | if (signals_enabled == 1) |
1d7173ba | 252 | return; |
0805d89c | 253 | |
ba180fd4 JD |
254 | /* |
255 | * We loop because the IRQ handler returns with interrupts off. So, | |
1d7173ba | 256 | * interrupts may have arrived and we need to re-enable them and |
cfef8f34 | 257 | * recheck signals_pending. |
1d7173ba | 258 | */ |
5134d8fe | 259 | while (1) { |
ba180fd4 JD |
260 | /* |
261 | * Save and reset save_pending after enabling signals. This | |
cfef8f34 | 262 | * way, signals_pending won't be changed while we're reading it. |
1d7173ba JD |
263 | */ |
264 | signals_enabled = 1; | |
265 | ||
ba180fd4 | 266 | /* |
cfef8f34 | 267 | * Setting signals_enabled and reading signals_pending must |
53b17332 JD |
268 | * happen in this order. |
269 | */ | |
fce8c41c | 270 | barrier(); |
53b17332 | 271 | |
cfef8f34 | 272 | save_pending = signals_pending; |
fce8c41c | 273 | if (save_pending == 0) |
1d7173ba JD |
274 | return; |
275 | ||
cfef8f34 | 276 | signals_pending = 0; |
1d7173ba | 277 | |
ba180fd4 JD |
278 | /* |
279 | * We have pending interrupts, so disable signals, as the | |
1d7173ba JD |
280 | * handlers expect them off when they are called. They will |
281 | * be enabled again above. | |
282 | */ | |
283 | ||
284 | signals_enabled = 0; | |
285 | ||
ba180fd4 JD |
286 | /* |
287 | * Deal with SIGIO first because the alarm handler might | |
1d7173ba JD |
288 | * schedule, leaving the pending SIGIO stranded until we come |
289 | * back here. | |
d3c1cfcd MP |
290 | * |
291 | * SIGIO's handler doesn't use siginfo or mcontext, | |
292 | * so they can be NULL. | |
1d7173ba | 293 | */ |
ba180fd4 | 294 | if (save_pending & SIGIO_MASK) |
d3c1cfcd | 295 | sig_handler_common(SIGIO, NULL, NULL); |
1d7173ba | 296 | |
d5e3f5cb AI |
297 | /* Do not reenter the handler */ |
298 | ||
299 | if ((save_pending & SIGALRM_MASK) && (!(signals_active & SIGALRM_MASK))) | |
2eb5f31b | 300 | timer_real_alarm_handler(NULL); |
d5e3f5cb AI |
301 | |
302 | /* Rerun the loop only if there is still pending SIGIO and not in TIMER handler */ | |
303 | ||
304 | if (!(signals_pending & SIGIO_MASK) && (signals_active & SIGALRM_MASK)) | |
305 | return; | |
306 | ||
1d7173ba | 307 | } |
0805d89c GS |
308 | } |
309 | ||
310 | int get_signals(void) | |
311 | { | |
1d7173ba | 312 | return signals_enabled; |
0805d89c GS |
313 | } |
314 | ||
315 | int set_signals(int enable) | |
316 | { | |
0805d89c | 317 | int ret; |
ba180fd4 | 318 | if (signals_enabled == enable) |
1d7173ba | 319 | return enable; |
0805d89c | 320 | |
1d7173ba | 321 | ret = signals_enabled; |
ba180fd4 | 322 | if (enable) |
1d7173ba JD |
323 | unblock_signals(); |
324 | else block_signals(); | |
0805d89c | 325 | |
1d7173ba | 326 | return ret; |
0805d89c | 327 | } |
f72c22e4 RW |
328 | |
329 | int os_is_signal_stack(void) | |
330 | { | |
331 | stack_t ss; | |
332 | sigaltstack(NULL, &ss); | |
333 | ||
334 | return ss.ss_flags & SS_ONSTACK; | |
335 | } |