blob: 7801666514ed60a83fd28618f6886981b0e77167 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Anton Ivanov2eb5f312015-11-02 16:16:37 +00002 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2004 PathScale, Inc
Jeff Dikeba180fd2007-10-16 01:27:00 -07005 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Licensed under the GPL
7 */
8
Gennady Sharapov0805d892006-01-08 01:01:29 -08009#include <stdlib.h>
Gennady Sharapov0805d892006-01-08 01:01:29 -080010#include <stdarg.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include <errno.h>
12#include <signal.h>
13#include <strings.h>
Al Viro37185b32012-10-08 03:27:32 +010014#include <as-layout.h>
15#include <kern_util.h>
16#include <os.h>
17#include <sysdep/mcontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Sergei Trofimovich72383d42012-12-30 01:37:31 +030019void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
Jeff Dike75ada8f2008-02-04 22:31:12 -080020 [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,
Anton Ivanov2eb5f312015-11-02 16:16:37 +000027 [SIGALRM] = timer_handler
28};
Jeff Dike75ada8f2008-02-04 22:31:12 -080029
Richard Weinberger9a8c1352013-07-19 11:31:36 +020030static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
Jeff Dike75ada8f2008-02-04 22:31:12 -080031{
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080032 struct uml_pt_regs r;
33 int save_errno = errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080034
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080035 r.is_user = 0;
Jeff Dike75ada8f2008-02-04 22:31:12 -080036 if (sig == SIGSEGV) {
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080037 /* For segfaults, we want the data from the sigcontext. */
Al Viroab1c0cc2011-08-18 20:05:19 +010038 get_regs_from_mc(&r, mc);
Al Viro248b74c2011-08-18 20:05:09 +010039 GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080040 }
Jeff Dike75ada8f2008-02-04 22:31:12 -080041
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080042 /* enable signals if sig isn't IRQ signal */
Anton Ivanov2eb5f312015-11-02 16:16:37 +000043 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGALRM))
Jeff Dike75ada8f2008-02-04 22:31:12 -080044 unblock_signals();
45
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020046 (*sig_info[sig])(sig, si, &r);
Jeff Dike75ada8f2008-02-04 22:31:12 -080047
48 errno = save_errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080049}
50
Jeff Dikeba180fd2007-10-16 01:27:00 -070051/*
Jeff Dike61b63c52007-10-16 01:27:27 -070052 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080053 * be able to profile all of UML, not just the non-critical sections. If
54 * profiling is not thread-safe, then that is not my problem. We can disable
55 * profiling when SMP is enabled in that case.
56 */
57#define SIGIO_BIT 0
58#define SIGIO_MASK (1 << SIGIO_BIT)
59
Anton Ivanov2eb5f312015-11-02 16:16:37 +000060#define SIGALRM_BIT 1
61#define SIGALRM_MASK (1 << SIGALRM_BIT)
Jeff Dike1d7173b2006-01-18 17:42:49 -080062
Jeff Dikefce8c412008-02-04 22:31:09 -080063static int signals_enabled;
Jeff Dikecfef8f32008-02-04 22:31:16 -080064static unsigned int signals_pending;
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +000065static unsigned int signals_active = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -080066
Richard Weinberger9a8c1352013-07-19 11:31:36 +020067void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Jeff Dike1d7173b2006-01-18 17:42:49 -080069 int enabled;
70
Jeff Dike1d7173b2006-01-18 17:42:49 -080071 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070072 if (!enabled && (sig == SIGIO)) {
Jeff Dikecfef8f32008-02-04 22:31:16 -080073 signals_pending |= SIGIO_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080074 return;
75 }
76
77 block_signals();
78
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020079 sig_handler_common(sig, si, mc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080080
81 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Anton Ivanov2eb5f312015-11-02 16:16:37 +000084static void timer_real_alarm_handler(mcontext_t *mc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Jeff Dike77bf4402007-10-16 01:26:58 -070086 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070087
Al Viro248b74c2011-08-18 20:05:09 +010088 if (mc != NULL)
Al Viroab1c0cc2011-08-18 20:05:19 +010089 get_regs_from_mc(&regs, mc);
Anton Ivanov2eb5f312015-11-02 16:16:37 +000090 timer_handler(SIGALRM, NULL, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080091}
92
Anton Ivanov2eb5f312015-11-02 16:16:37 +000093void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080094{
Jeff Dike1d7173b2006-01-18 17:42:49 -080095 int enabled;
96
Jeff Dike1d7173b2006-01-18 17:42:49 -080097 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070098 if (!signals_enabled) {
Anton Ivanov2eb5f312015-11-02 16:16:37 +000099 signals_pending |= SIGALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800100 return;
101 }
102
103 block_signals();
104
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +0000105 signals_active |= SIGALRM_MASK;
106
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000107 timer_real_alarm_handler(mc);
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +0000108
109 signals_active &= ~SIGALRM_MASK;
110
Jeff Dike1d7173b2006-01-18 17:42:49 -0800111 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000114void deliver_alarm(void) {
115 timer_alarm_handler(SIGALRM, NULL, NULL);
116}
117
118void timer_set_signal_handler(void)
Jeff Dike78a26e22007-10-16 01:27:23 -0700119{
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000120 set_handler(SIGALRM);
Jeff Dike78a26e22007-10-16 01:27:23 -0700121}
122
Gennady Sharapov0805d892006-01-08 01:01:29 -0800123void set_sigstack(void *sig_stack, int size)
124{
Hans-Werner Hilse9a755512015-06-11 11:29:18 +0200125 stack_t stack = {
126 .ss_flags = 0,
127 .ss_sp = sig_stack,
128 .ss_size = size - sizeof(void *)
129 };
Gennady Sharapov0805d892006-01-08 01:01:29 -0800130
Jeff Dikeba180fd2007-10-16 01:27:00 -0700131 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800132 panic("enabling signal stack failed, errno = %d\n", errno);
133}
134
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200135static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
Al Viro00361682011-08-18 20:04:39 +0100136 [SIGSEGV] = sig_handler,
137 [SIGBUS] = sig_handler,
138 [SIGILL] = sig_handler,
139 [SIGFPE] = sig_handler,
140 [SIGTRAP] = sig_handler,
141
142 [SIGIO] = sig_handler,
143 [SIGWINCH] = sig_handler,
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000144 [SIGALRM] = timer_alarm_handler
Al Viro00361682011-08-18 20:04:39 +0100145};
Jeff Dike4b84c692006-09-25 23:33:04 -0700146
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200147static void hard_handler(int sig, siginfo_t *si, void *p)
Jeff Dikec14b8492007-05-10 22:22:34 -0700148{
Al Viro248b74c2011-08-18 20:05:09 +0100149 struct ucontext *uc = p;
150 mcontext_t *mc = &uc->uc_mcontext;
Jeff Dike508a9272007-09-18 22:46:49 -0700151 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700152
153 do {
154 int nested, bail;
155
156 /*
157 * pending comes back with one bit set for each
158 * interrupt that arrived while setting up the stack,
159 * plus a bit for this interrupt, plus the zero bit is
160 * set if this is a nested interrupt.
161 * If bail is true, then we interrupted another
162 * handler setting up the stack. In this case, we
163 * have to return, and the upper handler will deal
164 * with this interrupt.
165 */
Jeff Dike508a9272007-09-18 22:46:49 -0700166 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700167 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700168 return;
169
170 nested = pending & 1;
171 pending &= ~1;
172
Jeff Dikeba180fd2007-10-16 01:27:00 -0700173 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700174 sig--;
175 pending &= ~(1 << sig);
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200176 (*handlers[sig])(sig, (struct siginfo *)si, mc);
Jeff Dikec14b8492007-05-10 22:22:34 -0700177 }
178
Jeff Dikeba180fd2007-10-16 01:27:00 -0700179 /*
180 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700181 * that arrived while tearing down the stack. If this
182 * is non-zero, we just go back, set up the stack
183 * again, and handle the new interrupts.
184 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700185 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700186 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700187 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700188}
189
Al Viro00361682011-08-18 20:04:39 +0100190void set_handler(int sig)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800191{
192 struct sigaction action;
Al Viroe87df982011-08-18 20:04:29 +0100193 int flags = SA_SIGINFO | SA_ONSTACK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800194 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800195
Al Viro7eb12252011-08-18 20:03:39 +0100196 action.sa_sigaction = hard_handler;
Jeff Dike4b84c692006-09-25 23:33:04 -0700197
Al Viroe87df982011-08-18 20:04:29 +0100198 /* block irq ones */
Gennady Sharapov0805d892006-01-08 01:01:29 -0800199 sigemptyset(&action.sa_mask);
Al Viroe87df982011-08-18 20:04:29 +0100200 sigaddset(&action.sa_mask, SIGIO);
201 sigaddset(&action.sa_mask, SIGWINCH);
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000202 sigaddset(&action.sa_mask, SIGALRM);
Jeff Dike4b84c692006-09-25 23:33:04 -0700203
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800204 if (sig == SIGSEGV)
205 flags |= SA_NODEFER;
206
Al Viroe87df982011-08-18 20:04:29 +0100207 if (sigismember(&action.sa_mask, sig))
208 flags |= SA_RESTART; /* if it's an irq signal */
209
210 action.sa_flags = flags;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800211 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700212 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800213 panic("sigaction failed - errno = %d\n", errno);
214
215 sigemptyset(&sig_mask);
216 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700217 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800218 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800219}
220
221int change_sig(int signal, int on)
222{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800223 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800224
225 sigemptyset(&sigset);
226 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800227 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800228 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800229
230 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800231}
232
Gennady Sharapov0805d892006-01-08 01:01:29 -0800233void block_signals(void)
234{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800235 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700236 /*
237 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800238 * ensures that writes are flushed out before the return.
239 * This might matter if gcc figures out how to inline this and
240 * decides to shuffle this code into the caller.
241 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800242 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800243}
244
245void unblock_signals(void)
246{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800247 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800248
Jeff Dikeba180fd2007-10-16 01:27:00 -0700249 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800250 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800251
Jeff Dikeba180fd2007-10-16 01:27:00 -0700252 /*
253 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800254 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800255 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800256 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800257 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700258 /*
259 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800260 * way, signals_pending won't be changed while we're reading it.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800261 */
262 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800263
Jeff Dikeba180fd2007-10-16 01:27:00 -0700264 /*
Jeff Dikecfef8f32008-02-04 22:31:16 -0800265 * Setting signals_enabled and reading signals_pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800266 * happen in this order.
267 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800268 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800269
Jeff Dikecfef8f32008-02-04 22:31:16 -0800270 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800271 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800272 return;
273
Jeff Dikecfef8f32008-02-04 22:31:16 -0800274 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800275
Jeff Dikeba180fd2007-10-16 01:27:00 -0700276 /*
277 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800278 * handlers expect them off when they are called. They will
279 * be enabled again above.
280 */
281
282 signals_enabled = 0;
283
Jeff Dikeba180fd2007-10-16 01:27:00 -0700284 /*
285 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800286 * schedule, leaving the pending SIGIO stranded until we come
287 * back here.
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200288 *
289 * SIGIO's handler doesn't use siginfo or mcontext,
290 * so they can be NULL.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800291 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700292 if (save_pending & SIGIO_MASK)
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200293 sig_handler_common(SIGIO, NULL, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800294
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +0000295 /* Do not reenter the handler */
296
297 if ((save_pending & SIGALRM_MASK) && (!(signals_active & SIGALRM_MASK)))
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000298 timer_real_alarm_handler(NULL);
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +0000299
300 /* Rerun the loop only if there is still pending SIGIO and not in TIMER handler */
301
302 if (!(signals_pending & SIGIO_MASK) && (signals_active & SIGALRM_MASK))
303 return;
304
Jeff Dike1d7173b2006-01-18 17:42:49 -0800305 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800306}
307
308int get_signals(void)
309{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800310 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800311}
312
313int set_signals(int enable)
314{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800315 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700316 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800317 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800318
Jeff Dike1d7173b2006-01-18 17:42:49 -0800319 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700320 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800321 unblock_signals();
322 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800323
Jeff Dike1d7173b2006-01-18 17:42:49 -0800324 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800325}
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200326
327int os_is_signal_stack(void)
328{
329 stack_t ss;
330 sigaltstack(NULL, &ss);
331
332 return ss.ss_flags & SS_ONSTACK;
333}