blob: 08d30617699589f7becf333381d0643c468cef25 [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
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080029static void sig_handler_common(int sig, struct sigcontext *sc)
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. */
37 copy_sc(&r, sc);
38 GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
39 }
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
Jeff Dike4b84c692006-09-25 23:33:04 -070065void sig_handler(int sig, struct sigcontext *sc)
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
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080077 sig_handler_common(sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080078
79 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Jeff Dike61b63c52007-10-16 01:27:27 -070082static void real_alarm_handler(struct sigcontext *sc)
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
Jeff Dikeba180fd2007-10-16 01:27:00 -070086 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -070087 copy_sc(&regs, sc);
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
Jeff Dike4b84c692006-09-25 23:33:04 -070093void alarm_handler(int sig, struct sigcontext *sc)
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
Jeff Dike61b63c52007-10-16 01:27:27 -0700105 real_alarm_handler(sc);
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 Viro00361682011-08-18 20:04:39 +0100124static void (*handlers[_NSIG])(int sig, struct sigcontext *sc) = {
125 [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 Viro7eb12252011-08-18 20:03:39 +0100136static void handle_signal(int sig, struct sigcontext *sc)
Jeff Dikec14b8492007-05-10 22:22:34 -0700137{
Jeff Dike508a9272007-09-18 22:46:49 -0700138 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700139
140 do {
141 int nested, bail;
142
143 /*
144 * pending comes back with one bit set for each
145 * interrupt that arrived while setting up the stack,
146 * plus a bit for this interrupt, plus the zero bit is
147 * set if this is a nested interrupt.
148 * If bail is true, then we interrupted another
149 * handler setting up the stack. In this case, we
150 * have to return, and the upper handler will deal
151 * with this interrupt.
152 */
Jeff Dike508a9272007-09-18 22:46:49 -0700153 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700154 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700155 return;
156
157 nested = pending & 1;
158 pending &= ~1;
159
Jeff Dikeba180fd2007-10-16 01:27:00 -0700160 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700161 sig--;
162 pending &= ~(1 << sig);
163 (*handlers[sig])(sig, sc);
164 }
165
Jeff Dikeba180fd2007-10-16 01:27:00 -0700166 /*
167 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700168 * that arrived while tearing down the stack. If this
169 * is non-zero, we just go back, set up the stack
170 * again, and handle the new interrupts.
171 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700172 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700173 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700174 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700175}
176
Al Viro7eb12252011-08-18 20:03:39 +0100177static void hard_handler(int sig, siginfo_t *info, void *p)
178{
179 struct ucontext *uc = p;
180 handle_signal(sig, (struct sigcontext *) &uc->uc_mcontext);
181}
Jeff Dike4b84c692006-09-25 23:33:04 -0700182
Al Viro00361682011-08-18 20:04:39 +0100183void set_handler(int sig)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800184{
185 struct sigaction action;
Al Viroe87df982011-08-18 20:04:29 +0100186 int flags = SA_SIGINFO | SA_ONSTACK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800187 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800188
Al Viro7eb12252011-08-18 20:03:39 +0100189 action.sa_sigaction = hard_handler;
Jeff Dike4b84c692006-09-25 23:33:04 -0700190
Al Viroe87df982011-08-18 20:04:29 +0100191 /* block irq ones */
Gennady Sharapov0805d892006-01-08 01:01:29 -0800192 sigemptyset(&action.sa_mask);
Al Viroe87df982011-08-18 20:04:29 +0100193 sigaddset(&action.sa_mask, SIGVTALRM);
194 sigaddset(&action.sa_mask, SIGIO);
195 sigaddset(&action.sa_mask, SIGWINCH);
Jeff Dike4b84c692006-09-25 23:33:04 -0700196
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800197 if (sig == SIGSEGV)
198 flags |= SA_NODEFER;
199
Al Viroe87df982011-08-18 20:04:29 +0100200 if (sigismember(&action.sa_mask, sig))
201 flags |= SA_RESTART; /* if it's an irq signal */
202
203 action.sa_flags = flags;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800204 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700205 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800206 panic("sigaction failed - errno = %d\n", errno);
207
208 sigemptyset(&sig_mask);
209 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700210 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800211 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800212}
213
214int change_sig(int signal, int on)
215{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800216 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800217
218 sigemptyset(&sigset);
219 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800220 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800221 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800222
223 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800224}
225
Gennady Sharapov0805d892006-01-08 01:01:29 -0800226void block_signals(void)
227{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800228 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700229 /*
230 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800231 * ensures that writes are flushed out before the return.
232 * This might matter if gcc figures out how to inline this and
233 * decides to shuffle this code into the caller.
234 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800235 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800236}
237
238void unblock_signals(void)
239{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800240 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800241
Jeff Dikeba180fd2007-10-16 01:27:00 -0700242 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800243 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800244
Jeff Dikeba180fd2007-10-16 01:27:00 -0700245 /*
246 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800247 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800248 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800249 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800250 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700251 /*
252 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800253 * way, signals_pending won't be changed while we're reading it.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800254 */
255 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800256
Jeff Dikeba180fd2007-10-16 01:27:00 -0700257 /*
Jeff Dikecfef8f32008-02-04 22:31:16 -0800258 * Setting signals_enabled and reading signals_pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800259 * happen in this order.
260 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800261 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800262
Jeff Dikecfef8f32008-02-04 22:31:16 -0800263 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800264 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800265 return;
266
Jeff Dikecfef8f32008-02-04 22:31:16 -0800267 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800268
Jeff Dikeba180fd2007-10-16 01:27:00 -0700269 /*
270 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800271 * handlers expect them off when they are called. They will
272 * be enabled again above.
273 */
274
275 signals_enabled = 0;
276
Jeff Dikeba180fd2007-10-16 01:27:00 -0700277 /*
278 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800279 * schedule, leaving the pending SIGIO stranded until we come
280 * back here.
281 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700282 if (save_pending & SIGIO_MASK)
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800283 sig_handler_common(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800284
Jeff Dikeba180fd2007-10-16 01:27:00 -0700285 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700286 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800287 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800288}
289
290int get_signals(void)
291{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800292 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800293}
294
295int set_signals(int enable)
296{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800297 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700298 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800299 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800300
Jeff Dike1d7173b2006-01-18 17:42:49 -0800301 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700302 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800303 unblock_signals();
304 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800305
Jeff Dike1d7173b2006-01-18 17:42:49 -0800306 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800307}