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