blob: 07d9905e44ef0184682097417e72dbf100c7c25b [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>
Jeff Dike75ada8f2008-02-04 22:31:12 -080012#include "as-layout.h"
Jeff Dikeedea1382008-02-04 22:30:46 -080013#include "kern_util.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080014#include "os.h"
Jeff Dikefe2cc532008-05-12 14:02:00 -070015#include "process.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070016#include "sysdep/barrier.h"
17#include "sysdep/sigcontext.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jeff Dike75ada8f2008-02-04 22:31:12 -080019void (*sig_info[NSIG])(int, struct uml_pt_regs *) = {
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,
27 [SIGVTALRM] = timer_handler };
28
Al Viro248b74c2011-08-18 20:05:09 +010029static void sig_handler_common(int sig, mcontext_t *mc)
Jeff Dike75ada8f2008-02-04 22:31:12 -080030{
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080031 struct uml_pt_regs r;
32 int save_errno = errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080033
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080034 r.is_user = 0;
Jeff Dike75ada8f2008-02-04 22:31:12 -080035 if (sig == SIGSEGV) {
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080036 /* For segfaults, we want the data from the sigcontext. */
Al Viro248b74c2011-08-18 20:05:09 +010037 copy_sc(&r, (struct sigcontext *)mc);
38 GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080039 }
Jeff Dike75ada8f2008-02-04 22:31:12 -080040
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080041 /* enable signals if sig isn't IRQ signal */
Jeff Dike75ada8f2008-02-04 22:31:12 -080042 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
43 unblock_signals();
44
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080045 (*sig_info[sig])(sig, &r);
Jeff Dike75ada8f2008-02-04 22:31:12 -080046
47 errno = save_errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080048}
49
Jeff Dikeba180fd2007-10-16 01:27:00 -070050/*
Jeff Dike61b63c52007-10-16 01:27:27 -070051 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080052 * be able to profile all of UML, not just the non-critical sections. If
53 * profiling is not thread-safe, then that is not my problem. We can disable
54 * profiling when SMP is enabled in that case.
55 */
56#define SIGIO_BIT 0
57#define SIGIO_MASK (1 << SIGIO_BIT)
58
59#define SIGVTALRM_BIT 1
60#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
61
Jeff Dikefce8c412008-02-04 22:31:09 -080062static int signals_enabled;
Jeff Dikecfef8f32008-02-04 22:31:16 -080063static unsigned int signals_pending;
Jeff Dike1d7173b2006-01-18 17:42:49 -080064
Al Viro248b74c2011-08-18 20:05:09 +010065void sig_handler(int sig, mcontext_t *mc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Jeff Dike1d7173b2006-01-18 17:42:49 -080067 int enabled;
68
Jeff Dike1d7173b2006-01-18 17:42:49 -080069 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070070 if (!enabled && (sig == SIGIO)) {
Jeff Dikecfef8f32008-02-04 22:31:16 -080071 signals_pending |= SIGIO_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080072 return;
73 }
74
75 block_signals();
76
Al Viro248b74c2011-08-18 20:05:09 +010077 sig_handler_common(sig, mc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080078
79 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Al Viro248b74c2011-08-18 20:05:09 +010082static void real_alarm_handler(mcontext_t *mc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Jeff Dike77bf4402007-10-16 01:26:58 -070084 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070085
Al Viro248b74c2011-08-18 20:05:09 +010086 if (mc != NULL)
87 copy_sc(&regs, (struct sigcontext *)mc);
Jeff Dike77bf4402007-10-16 01:26:58 -070088 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070089 unblock_signals();
Jeff Dike61b63c52007-10-16 01:27:27 -070090 timer_handler(SIGVTALRM, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080091}
92
Al Viro248b74c2011-08-18 20:05:09 +010093void alarm_handler(int sig, 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) {
Jeff Dikecfef8f32008-02-04 22:31:16 -080099 signals_pending |= SIGVTALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800100 return;
101 }
102
103 block_signals();
104
Al Viro248b74c2011-08-18 20:05:09 +0100105 real_alarm_handler(mc);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800106 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Jeff Dike78a26e22007-10-16 01:27:23 -0700109void timer_init(void)
110{
Al Viro00361682011-08-18 20:04:39 +0100111 set_handler(SIGVTALRM);
Jeff Dike78a26e22007-10-16 01:27:23 -0700112}
113
Gennady Sharapov0805d892006-01-08 01:01:29 -0800114void set_sigstack(void *sig_stack, int size)
115{
116 stack_t stack = ((stack_t) { .ss_flags = 0,
117 .ss_sp = (__ptr_t) sig_stack,
118 .ss_size = size - sizeof(void *) });
119
Jeff Dikeba180fd2007-10-16 01:27:00 -0700120 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800121 panic("enabling signal stack failed, errno = %d\n", errno);
122}
123
Al Viro248b74c2011-08-18 20:05:09 +0100124static void (*handlers[_NSIG])(int sig, mcontext_t *mc) = {
Al Viro00361682011-08-18 20:04:39 +0100125 [SIGSEGV] = sig_handler,
126 [SIGBUS] = sig_handler,
127 [SIGILL] = sig_handler,
128 [SIGFPE] = sig_handler,
129 [SIGTRAP] = sig_handler,
130
131 [SIGIO] = sig_handler,
132 [SIGWINCH] = sig_handler,
133 [SIGVTALRM] = alarm_handler
134};
Jeff Dike4b84c692006-09-25 23:33:04 -0700135
Al Viro248b74c2011-08-18 20:05:09 +0100136
137static void hard_handler(int sig, siginfo_t *info, void *p)
Jeff Dikec14b8492007-05-10 22:22:34 -0700138{
Al Viro248b74c2011-08-18 20:05:09 +0100139 struct ucontext *uc = p;
140 mcontext_t *mc = &uc->uc_mcontext;
Jeff Dike508a9272007-09-18 22:46:49 -0700141 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700142
143 do {
144 int nested, bail;
145
146 /*
147 * pending comes back with one bit set for each
148 * interrupt that arrived while setting up the stack,
149 * plus a bit for this interrupt, plus the zero bit is
150 * set if this is a nested interrupt.
151 * If bail is true, then we interrupted another
152 * handler setting up the stack. In this case, we
153 * have to return, and the upper handler will deal
154 * with this interrupt.
155 */
Jeff Dike508a9272007-09-18 22:46:49 -0700156 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700157 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700158 return;
159
160 nested = pending & 1;
161 pending &= ~1;
162
Jeff Dikeba180fd2007-10-16 01:27:00 -0700163 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700164 sig--;
165 pending &= ~(1 << sig);
Al Viro248b74c2011-08-18 20:05:09 +0100166 (*handlers[sig])(sig, mc);
Jeff Dikec14b8492007-05-10 22:22:34 -0700167 }
168
Jeff Dikeba180fd2007-10-16 01:27:00 -0700169 /*
170 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700171 * that arrived while tearing down the stack. If this
172 * is non-zero, we just go back, set up the stack
173 * again, and handle the new interrupts.
174 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700175 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700176 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700177 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700178}
179
Al Viro00361682011-08-18 20:04:39 +0100180void set_handler(int sig)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800181{
182 struct sigaction action;
Al Viroe87df982011-08-18 20:04:29 +0100183 int flags = SA_SIGINFO | SA_ONSTACK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800184 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800185
Al Viro7eb12252011-08-18 20:03:39 +0100186 action.sa_sigaction = hard_handler;
Jeff Dike4b84c692006-09-25 23:33:04 -0700187
Al Viroe87df982011-08-18 20:04:29 +0100188 /* block irq ones */
Gennady Sharapov0805d892006-01-08 01:01:29 -0800189 sigemptyset(&action.sa_mask);
Al Viroe87df982011-08-18 20:04:29 +0100190 sigaddset(&action.sa_mask, SIGVTALRM);
191 sigaddset(&action.sa_mask, SIGIO);
192 sigaddset(&action.sa_mask, SIGWINCH);
Jeff Dike4b84c692006-09-25 23:33:04 -0700193
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800194 if (sig == SIGSEGV)
195 flags |= SA_NODEFER;
196
Al Viroe87df982011-08-18 20:04:29 +0100197 if (sigismember(&action.sa_mask, sig))
198 flags |= SA_RESTART; /* if it's an irq signal */
199
200 action.sa_flags = flags;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800201 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700202 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800203 panic("sigaction failed - errno = %d\n", errno);
204
205 sigemptyset(&sig_mask);
206 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700207 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800208 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800209}
210
211int change_sig(int signal, int on)
212{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800213 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800214
215 sigemptyset(&sigset);
216 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800217 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800218 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800219
220 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800221}
222
Gennady Sharapov0805d892006-01-08 01:01:29 -0800223void block_signals(void)
224{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800225 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700226 /*
227 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800228 * ensures that writes are flushed out before the return.
229 * This might matter if gcc figures out how to inline this and
230 * decides to shuffle this code into the caller.
231 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800232 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800233}
234
235void unblock_signals(void)
236{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800237 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800238
Jeff Dikeba180fd2007-10-16 01:27:00 -0700239 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800240 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800241
Jeff Dikeba180fd2007-10-16 01:27:00 -0700242 /*
243 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800244 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800245 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800246 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800247 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700248 /*
249 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800250 * way, signals_pending won't be changed while we're reading it.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800251 */
252 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800253
Jeff Dikeba180fd2007-10-16 01:27:00 -0700254 /*
Jeff Dikecfef8f32008-02-04 22:31:16 -0800255 * Setting signals_enabled and reading signals_pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800256 * happen in this order.
257 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800258 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800259
Jeff Dikecfef8f32008-02-04 22:31:16 -0800260 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800261 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800262 return;
263
Jeff Dikecfef8f32008-02-04 22:31:16 -0800264 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800265
Jeff Dikeba180fd2007-10-16 01:27:00 -0700266 /*
267 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800268 * handlers expect them off when they are called. They will
269 * be enabled again above.
270 */
271
272 signals_enabled = 0;
273
Jeff Dikeba180fd2007-10-16 01:27:00 -0700274 /*
275 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800276 * schedule, leaving the pending SIGIO stranded until we come
277 * back here.
278 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700279 if (save_pending & SIGIO_MASK)
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800280 sig_handler_common(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800281
Jeff Dikeba180fd2007-10-16 01:27:00 -0700282 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700283 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800284 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800285}
286
287int get_signals(void)
288{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800289 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800290}
291
292int set_signals(int enable)
293{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800294 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700295 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800296 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800297
Jeff Dike1d7173b2006-01-18 17:42:49 -0800298 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700299 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800300 unblock_signals();
301 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800302
Jeff Dike1d7173b2006-01-18 17:42:49 -0800303 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800304}