blob: 905924b773d345b8f49de2905e62a546f6461cd9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 PathScale, Inc
Jeff Dikeba180fd2007-10-16 01:27:00 -07003 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Licensed under the GPL
5 */
6
Gennady Sharapov0805d892006-01-08 01:01:29 -08007#include <stdlib.h>
Gennady Sharapov0805d892006-01-08 01:01:29 -08008#include <stdarg.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07009#include <errno.h>
10#include <signal.h>
11#include <strings.h>
Al Viro37185b32012-10-08 03:27:32 +010012#include <as-layout.h>
13#include <kern_util.h>
14#include <os.h>
15#include <sysdep/mcontext.h>
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020016#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Sergei Trofimovich72383d42012-12-30 01:37:31 +030018void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
Jeff Dike75ada8f2008-02-04 22:31:12 -080019 [SIGTRAP] = relay_signal,
20 [SIGFPE] = relay_signal,
21 [SIGILL] = relay_signal,
22 [SIGWINCH] = winch,
23 [SIGBUS] = bus_handler,
24 [SIGSEGV] = segv_handler,
25 [SIGIO] = sigio_handler,
26 [SIGVTALRM] = timer_handler };
27
Richard Weinberger9a8c1352013-07-19 11:31:36 +020028static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
Jeff Dike75ada8f2008-02-04 22:31:12 -080029{
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080030 struct uml_pt_regs r;
31 int save_errno = errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080032
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080033 r.is_user = 0;
Jeff Dike75ada8f2008-02-04 22:31:12 -080034 if (sig == SIGSEGV) {
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080035 /* For segfaults, we want the data from the sigcontext. */
Al Viroab1c0cc2011-08-18 20:05:19 +010036 get_regs_from_mc(&r, mc);
Al Viro248b74c2011-08-18 20:05:09 +010037 GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080038 }
Jeff Dike75ada8f2008-02-04 22:31:12 -080039
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080040 /* enable signals if sig isn't IRQ signal */
Jeff Dike75ada8f2008-02-04 22:31:12 -080041 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
42 unblock_signals();
43
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020044 (*sig_info[sig])(sig, si, &r);
Jeff Dike75ada8f2008-02-04 22:31:12 -080045
46 errno = save_errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080047}
48
Jeff Dikeba180fd2007-10-16 01:27:00 -070049/*
Jeff Dike61b63c52007-10-16 01:27:27 -070050 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080051 * be able to profile all of UML, not just the non-critical sections. If
52 * profiling is not thread-safe, then that is not my problem. We can disable
53 * profiling when SMP is enabled in that case.
54 */
55#define SIGIO_BIT 0
56#define SIGIO_MASK (1 << SIGIO_BIT)
57
58#define SIGVTALRM_BIT 1
59#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
60
Jeff Dikefce8c412008-02-04 22:31:09 -080061static int signals_enabled;
Jeff Dikecfef8f32008-02-04 22:31:16 -080062static unsigned int signals_pending;
Jeff Dike1d7173b2006-01-18 17:42:49 -080063
Richard Weinberger9a8c1352013-07-19 11:31:36 +020064void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Jeff Dike1d7173b2006-01-18 17:42:49 -080066 int enabled;
67
Jeff Dike1d7173b2006-01-18 17:42:49 -080068 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070069 if (!enabled && (sig == SIGIO)) {
Jeff Dikecfef8f32008-02-04 22:31:16 -080070 signals_pending |= SIGIO_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080071 return;
72 }
73
74 block_signals();
75
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020076 sig_handler_common(sig, si, mc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080077
78 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Al Viro248b74c2011-08-18 20:05:09 +010081static void real_alarm_handler(mcontext_t *mc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Jeff Dike77bf4402007-10-16 01:26:58 -070083 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070084
Al Viro248b74c2011-08-18 20:05:09 +010085 if (mc != NULL)
Al Viroab1c0cc2011-08-18 20:05:19 +010086 get_regs_from_mc(&regs, mc);
Jeff Dike77bf4402007-10-16 01:26:58 -070087 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070088 unblock_signals();
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020089 timer_handler(SIGVTALRM, NULL, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080090}
91
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020092void alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080093{
Jeff Dike1d7173b2006-01-18 17:42:49 -080094 int enabled;
95
Jeff Dike1d7173b2006-01-18 17:42:49 -080096 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070097 if (!signals_enabled) {
Jeff Dikecfef8f32008-02-04 22:31:16 -080098 signals_pending |= SIGVTALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080099 return;
100 }
101
102 block_signals();
103
Al Viro248b74c2011-08-18 20:05:09 +0100104 real_alarm_handler(mc);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800105 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Jeff Dike78a26e22007-10-16 01:27:23 -0700108void timer_init(void)
109{
Al Viro00361682011-08-18 20:04:39 +0100110 set_handler(SIGVTALRM);
Jeff Dike78a26e22007-10-16 01:27:23 -0700111}
112
Gennady Sharapov0805d892006-01-08 01:01:29 -0800113void set_sigstack(void *sig_stack, int size)
114{
115 stack_t stack = ((stack_t) { .ss_flags = 0,
116 .ss_sp = (__ptr_t) sig_stack,
117 .ss_size = size - sizeof(void *) });
118
Jeff Dikeba180fd2007-10-16 01:27:00 -0700119 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800120 panic("enabling signal stack failed, errno = %d\n", errno);
121}
122
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200123static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
Al Viro00361682011-08-18 20:04:39 +0100124 [SIGSEGV] = sig_handler,
125 [SIGBUS] = sig_handler,
126 [SIGILL] = sig_handler,
127 [SIGFPE] = sig_handler,
128 [SIGTRAP] = sig_handler,
129
130 [SIGIO] = sig_handler,
131 [SIGWINCH] = sig_handler,
132 [SIGVTALRM] = alarm_handler
133};
Jeff Dike4b84c692006-09-25 23:33:04 -0700134
Al Viro248b74c2011-08-18 20:05:09 +0100135
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200136static void hard_handler(int sig, siginfo_t *si, void *p)
Jeff Dikec14b8492007-05-10 22:22:34 -0700137{
Al Viro248b74c2011-08-18 20:05:09 +0100138 struct ucontext *uc = p;
139 mcontext_t *mc = &uc->uc_mcontext;
Jeff Dike508a9272007-09-18 22:46:49 -0700140 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700141
142 do {
143 int nested, bail;
144
145 /*
146 * pending comes back with one bit set for each
147 * interrupt that arrived while setting up the stack,
148 * plus a bit for this interrupt, plus the zero bit is
149 * set if this is a nested interrupt.
150 * If bail is true, then we interrupted another
151 * handler setting up the stack. In this case, we
152 * have to return, and the upper handler will deal
153 * with this interrupt.
154 */
Jeff Dike508a9272007-09-18 22:46:49 -0700155 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700156 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700157 return;
158
159 nested = pending & 1;
160 pending &= ~1;
161
Jeff Dikeba180fd2007-10-16 01:27:00 -0700162 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700163 sig--;
164 pending &= ~(1 << sig);
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200165 (*handlers[sig])(sig, (struct siginfo *)si, mc);
Jeff Dikec14b8492007-05-10 22:22:34 -0700166 }
167
Jeff Dikeba180fd2007-10-16 01:27:00 -0700168 /*
169 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700170 * that arrived while tearing down the stack. If this
171 * is non-zero, we just go back, set up the stack
172 * again, and handle the new interrupts.
173 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700174 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700175 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700176 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700177}
178
Al Viro00361682011-08-18 20:04:39 +0100179void set_handler(int sig)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800180{
181 struct sigaction action;
Al Viroe87df982011-08-18 20:04:29 +0100182 int flags = SA_SIGINFO | SA_ONSTACK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800183 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800184
Al Viro7eb12252011-08-18 20:03:39 +0100185 action.sa_sigaction = hard_handler;
Jeff Dike4b84c692006-09-25 23:33:04 -0700186
Al Viroe87df982011-08-18 20:04:29 +0100187 /* block irq ones */
Gennady Sharapov0805d892006-01-08 01:01:29 -0800188 sigemptyset(&action.sa_mask);
Al Viroe87df982011-08-18 20:04:29 +0100189 sigaddset(&action.sa_mask, SIGVTALRM);
190 sigaddset(&action.sa_mask, SIGIO);
191 sigaddset(&action.sa_mask, SIGWINCH);
Jeff Dike4b84c692006-09-25 23:33:04 -0700192
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800193 if (sig == SIGSEGV)
194 flags |= SA_NODEFER;
195
Al Viroe87df982011-08-18 20:04:29 +0100196 if (sigismember(&action.sa_mask, sig))
197 flags |= SA_RESTART; /* if it's an irq signal */
198
199 action.sa_flags = flags;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800200 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700201 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800202 panic("sigaction failed - errno = %d\n", errno);
203
204 sigemptyset(&sig_mask);
205 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700206 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800207 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800208}
209
210int change_sig(int signal, int on)
211{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800212 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800213
214 sigemptyset(&sigset);
215 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800216 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800217 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800218
219 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800220}
221
Gennady Sharapov0805d892006-01-08 01:01:29 -0800222void block_signals(void)
223{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800224 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700225 /*
226 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800227 * ensures that writes are flushed out before the return.
228 * This might matter if gcc figures out how to inline this and
229 * decides to shuffle this code into the caller.
230 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800231 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800232}
233
234void unblock_signals(void)
235{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800236 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800237
Jeff Dikeba180fd2007-10-16 01:27:00 -0700238 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800239 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800240
Jeff Dikeba180fd2007-10-16 01:27:00 -0700241 /*
242 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800243 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800244 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800245 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800246 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700247 /*
248 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800249 * way, signals_pending won't be changed while we're reading it.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800250 */
251 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800252
Jeff Dikeba180fd2007-10-16 01:27:00 -0700253 /*
Jeff Dikecfef8f32008-02-04 22:31:16 -0800254 * Setting signals_enabled and reading signals_pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800255 * happen in this order.
256 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800257 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800258
Jeff Dikecfef8f32008-02-04 22:31:16 -0800259 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800260 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800261 return;
262
Jeff Dikecfef8f32008-02-04 22:31:16 -0800263 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800264
Jeff Dikeba180fd2007-10-16 01:27:00 -0700265 /*
266 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800267 * handlers expect them off when they are called. They will
268 * be enabled again above.
269 */
270
271 signals_enabled = 0;
272
Jeff Dikeba180fd2007-10-16 01:27:00 -0700273 /*
274 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800275 * schedule, leaving the pending SIGIO stranded until we come
276 * back here.
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200277 *
278 * SIGIO's handler doesn't use siginfo or mcontext,
279 * so they can be NULL.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800280 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700281 if (save_pending & SIGIO_MASK)
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200282 sig_handler_common(SIGIO, NULL, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800283
Jeff Dikeba180fd2007-10-16 01:27:00 -0700284 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700285 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800286 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800287}
288
289int get_signals(void)
290{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800291 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800292}
293
294int set_signals(int enable)
295{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800296 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700297 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800298 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800299
Jeff Dike1d7173b2006-01-18 17:42:49 -0800300 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700301 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800302 unblock_signals();
303 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800304
Jeff Dike1d7173b2006-01-18 17:42:49 -0800305 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800306}