blob: 036d0dbc7b52730ece2a72d69ed95570269fef76 [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{
Hans-Werner Hilse9a755512015-06-11 11:29:18 +0200115 stack_t stack = {
116 .ss_flags = 0,
117 .ss_sp = sig_stack,
118 .ss_size = size - sizeof(void *)
119 };
Gennady Sharapov0805d892006-01-08 01:01:29 -0800120
Jeff Dikeba180fd2007-10-16 01:27:00 -0700121 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800122 panic("enabling signal stack failed, errno = %d\n", errno);
123}
124
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200125static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
Al Viro00361682011-08-18 20:04:39 +0100126 [SIGSEGV] = sig_handler,
127 [SIGBUS] = sig_handler,
128 [SIGILL] = sig_handler,
129 [SIGFPE] = sig_handler,
130 [SIGTRAP] = sig_handler,
131
132 [SIGIO] = sig_handler,
133 [SIGWINCH] = sig_handler,
134 [SIGVTALRM] = alarm_handler
135};
Jeff Dike4b84c692006-09-25 23:33:04 -0700136
Al Viro248b74c2011-08-18 20:05:09 +0100137
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200138static void hard_handler(int sig, siginfo_t *si, void *p)
Jeff Dikec14b8492007-05-10 22:22:34 -0700139{
Al Viro248b74c2011-08-18 20:05:09 +0100140 struct ucontext *uc = p;
141 mcontext_t *mc = &uc->uc_mcontext;
Jeff Dike508a9272007-09-18 22:46:49 -0700142 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700143
144 do {
145 int nested, bail;
146
147 /*
148 * pending comes back with one bit set for each
149 * interrupt that arrived while setting up the stack,
150 * plus a bit for this interrupt, plus the zero bit is
151 * set if this is a nested interrupt.
152 * If bail is true, then we interrupted another
153 * handler setting up the stack. In this case, we
154 * have to return, and the upper handler will deal
155 * with this interrupt.
156 */
Jeff Dike508a9272007-09-18 22:46:49 -0700157 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700158 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700159 return;
160
161 nested = pending & 1;
162 pending &= ~1;
163
Jeff Dikeba180fd2007-10-16 01:27:00 -0700164 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700165 sig--;
166 pending &= ~(1 << sig);
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200167 (*handlers[sig])(sig, (struct siginfo *)si, mc);
Jeff Dikec14b8492007-05-10 22:22:34 -0700168 }
169
Jeff Dikeba180fd2007-10-16 01:27:00 -0700170 /*
171 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700172 * that arrived while tearing down the stack. If this
173 * is non-zero, we just go back, set up the stack
174 * again, and handle the new interrupts.
175 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700176 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700177 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700178 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700179}
180
Al Viro00361682011-08-18 20:04:39 +0100181void set_handler(int sig)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800182{
183 struct sigaction action;
Al Viroe87df982011-08-18 20:04:29 +0100184 int flags = SA_SIGINFO | SA_ONSTACK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800185 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800186
Al Viro7eb12252011-08-18 20:03:39 +0100187 action.sa_sigaction = hard_handler;
Jeff Dike4b84c692006-09-25 23:33:04 -0700188
Al Viroe87df982011-08-18 20:04:29 +0100189 /* block irq ones */
Gennady Sharapov0805d892006-01-08 01:01:29 -0800190 sigemptyset(&action.sa_mask);
Al Viroe87df982011-08-18 20:04:29 +0100191 sigaddset(&action.sa_mask, SIGVTALRM);
192 sigaddset(&action.sa_mask, SIGIO);
193 sigaddset(&action.sa_mask, SIGWINCH);
Jeff Dike4b84c692006-09-25 23:33:04 -0700194
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800195 if (sig == SIGSEGV)
196 flags |= SA_NODEFER;
197
Al Viroe87df982011-08-18 20:04:29 +0100198 if (sigismember(&action.sa_mask, sig))
199 flags |= SA_RESTART; /* if it's an irq signal */
200
201 action.sa_flags = flags;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800202 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700203 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800204 panic("sigaction failed - errno = %d\n", errno);
205
206 sigemptyset(&sig_mask);
207 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800209 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800210}
211
212int change_sig(int signal, int on)
213{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800214 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800215
216 sigemptyset(&sigset);
217 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800218 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800219 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800220
221 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800222}
223
Gennady Sharapov0805d892006-01-08 01:01:29 -0800224void block_signals(void)
225{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800226 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700227 /*
228 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800229 * ensures that writes are flushed out before the return.
230 * This might matter if gcc figures out how to inline this and
231 * decides to shuffle this code into the caller.
232 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800233 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800234}
235
236void unblock_signals(void)
237{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800238 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800239
Jeff Dikeba180fd2007-10-16 01:27:00 -0700240 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800241 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800242
Jeff Dikeba180fd2007-10-16 01:27:00 -0700243 /*
244 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800245 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800246 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800247 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800248 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700249 /*
250 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800251 * way, signals_pending won't be changed while we're reading it.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800252 */
253 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800254
Jeff Dikeba180fd2007-10-16 01:27:00 -0700255 /*
Jeff Dikecfef8f32008-02-04 22:31:16 -0800256 * Setting signals_enabled and reading signals_pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800257 * happen in this order.
258 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800259 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800260
Jeff Dikecfef8f32008-02-04 22:31:16 -0800261 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800262 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800263 return;
264
Jeff Dikecfef8f32008-02-04 22:31:16 -0800265 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800266
Jeff Dikeba180fd2007-10-16 01:27:00 -0700267 /*
268 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800269 * handlers expect them off when they are called. They will
270 * be enabled again above.
271 */
272
273 signals_enabled = 0;
274
Jeff Dikeba180fd2007-10-16 01:27:00 -0700275 /*
276 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800277 * schedule, leaving the pending SIGIO stranded until we come
278 * back here.
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200279 *
280 * SIGIO's handler doesn't use siginfo or mcontext,
281 * so they can be NULL.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800282 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700283 if (save_pending & SIGIO_MASK)
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200284 sig_handler_common(SIGIO, NULL, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800285
Jeff Dikeba180fd2007-10-16 01:27:00 -0700286 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700287 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800288 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800289}
290
291int get_signals(void)
292{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800293 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800294}
295
296int set_signals(int enable)
297{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800298 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700299 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800300 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800301
Jeff Dike1d7173b2006-01-18 17:42:49 -0800302 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700303 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800304 unblock_signals();
305 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800306
Jeff Dike1d7173b2006-01-18 17:42:49 -0800307 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800308}
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200309
310int os_is_signal_stack(void)
311{
312 stack_t ss;
313 sigaltstack(NULL, &ss);
314
315 return ss.ss_flags & SS_ONSTACK;
316}