blob: f248fb2ab58cfbd2cde0cc823cbb5c55a47db76f [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 Dikee6a2d1f2008-02-04 22:31:13 -080019/* Copied from linux/compiler-gcc.h since we can't include it directly */
20#define barrier() __asm__ __volatile__("": : :"memory")
21
Jeff Dike75ada8f2008-02-04 22:31:12 -080022void (*sig_info[NSIG])(int, struct uml_pt_regs *) = {
23 [SIGTRAP] = relay_signal,
24 [SIGFPE] = relay_signal,
25 [SIGILL] = relay_signal,
26 [SIGWINCH] = winch,
27 [SIGBUS] = bus_handler,
28 [SIGSEGV] = segv_handler,
29 [SIGIO] = sigio_handler,
30 [SIGVTALRM] = timer_handler };
31
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080032static void sig_handler_common(int sig, struct sigcontext *sc)
Jeff Dike75ada8f2008-02-04 22:31:12 -080033{
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080034 struct uml_pt_regs r;
35 int save_errno = errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080036
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080037 r.is_user = 0;
Jeff Dike75ada8f2008-02-04 22:31:12 -080038 if (sig == SIGSEGV) {
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080039 /* For segfaults, we want the data from the sigcontext. */
40 copy_sc(&r, sc);
41 GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
42 }
Jeff Dike75ada8f2008-02-04 22:31:12 -080043
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080044 /* enable signals if sig isn't IRQ signal */
Jeff Dike75ada8f2008-02-04 22:31:12 -080045 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
46 unblock_signals();
47
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080048 (*sig_info[sig])(sig, &r);
Jeff Dike75ada8f2008-02-04 22:31:12 -080049
50 errno = save_errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080051}
52
Jeff Dikeba180fd2007-10-16 01:27:00 -070053/*
Jeff Dike61b63c52007-10-16 01:27:27 -070054 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080055 * be able to profile all of UML, not just the non-critical sections. If
56 * profiling is not thread-safe, then that is not my problem. We can disable
57 * profiling when SMP is enabled in that case.
58 */
59#define SIGIO_BIT 0
60#define SIGIO_MASK (1 << SIGIO_BIT)
61
62#define SIGVTALRM_BIT 1
63#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
64
Jeff Dikefce8c412008-02-04 22:31:09 -080065static int signals_enabled;
Jeff Dikecfef8f32008-02-04 22:31:16 -080066static unsigned int signals_pending;
Jeff Dike1d7173b2006-01-18 17:42:49 -080067
Jeff Dike4b84c692006-09-25 23:33:04 -070068void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Jeff Dike1d7173b2006-01-18 17:42:49 -080070 int enabled;
71
Jeff Dike1d7173b2006-01-18 17:42:49 -080072 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070073 if (!enabled && (sig == SIGIO)) {
Jeff Dikecfef8f32008-02-04 22:31:16 -080074 signals_pending |= SIGIO_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080075 return;
76 }
77
78 block_signals();
79
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080080 sig_handler_common(sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080081
82 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Jeff Dike61b63c52007-10-16 01:27:27 -070085static void real_alarm_handler(struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Jeff Dike77bf4402007-10-16 01:26:58 -070087 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070088
Jeff Dikeba180fd2007-10-16 01:27:00 -070089 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -070090 copy_sc(&regs, sc);
Jeff Dike77bf4402007-10-16 01:26:58 -070091 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070092 unblock_signals();
Jeff Dike61b63c52007-10-16 01:27:27 -070093 timer_handler(SIGVTALRM, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080094}
95
Jeff Dike4b84c692006-09-25 23:33:04 -070096void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080097{
Jeff Dike1d7173b2006-01-18 17:42:49 -080098 int enabled;
99
Jeff Dike1d7173b2006-01-18 17:42:49 -0800100 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700101 if (!signals_enabled) {
Jeff Dikecfef8f32008-02-04 22:31:16 -0800102 signals_pending |= SIGVTALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800103 return;
104 }
105
106 block_signals();
107
Jeff Dike61b63c52007-10-16 01:27:27 -0700108 real_alarm_handler(sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800109 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Jeff Dike78a26e22007-10-16 01:27:23 -0700112void timer_init(void)
113{
Al Viroe87df982011-08-18 20:04:29 +0100114 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler);
Jeff Dike78a26e22007-10-16 01:27:23 -0700115}
116
Gennady Sharapov0805d892006-01-08 01:01:29 -0800117void set_sigstack(void *sig_stack, int size)
118{
119 stack_t stack = ((stack_t) { .ss_flags = 0,
120 .ss_sp = (__ptr_t) sig_stack,
121 .ss_size = size - sizeof(void *) });
122
Jeff Dikeba180fd2007-10-16 01:27:00 -0700123 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800124 panic("enabling signal stack failed, errno = %d\n", errno);
125}
126
WANG Cong99764fa2008-07-23 21:28:49 -0700127static void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
Jeff Dike4b84c692006-09-25 23:33:04 -0700128
Al Viro7eb12252011-08-18 20:03:39 +0100129static void handle_signal(int sig, struct sigcontext *sc)
Jeff Dikec14b8492007-05-10 22:22:34 -0700130{
Jeff Dike508a9272007-09-18 22:46:49 -0700131 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700132
133 do {
134 int nested, bail;
135
136 /*
137 * pending comes back with one bit set for each
138 * interrupt that arrived while setting up the stack,
139 * plus a bit for this interrupt, plus the zero bit is
140 * set if this is a nested interrupt.
141 * If bail is true, then we interrupted another
142 * handler setting up the stack. In this case, we
143 * have to return, and the upper handler will deal
144 * with this interrupt.
145 */
Jeff Dike508a9272007-09-18 22:46:49 -0700146 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700147 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700148 return;
149
150 nested = pending & 1;
151 pending &= ~1;
152
Jeff Dikeba180fd2007-10-16 01:27:00 -0700153 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700154 sig--;
155 pending &= ~(1 << sig);
156 (*handlers[sig])(sig, sc);
157 }
158
Jeff Dikeba180fd2007-10-16 01:27:00 -0700159 /*
160 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700161 * that arrived while tearing down the stack. If this
162 * is non-zero, we just go back, set up the stack
163 * again, and handle the new interrupts.
164 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700165 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700166 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700167 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700168}
169
Al Viro7eb12252011-08-18 20:03:39 +0100170static void hard_handler(int sig, siginfo_t *info, void *p)
171{
172 struct ucontext *uc = p;
173 handle_signal(sig, (struct sigcontext *) &uc->uc_mcontext);
174}
Jeff Dike4b84c692006-09-25 23:33:04 -0700175
Al Viroe87df982011-08-18 20:04:29 +0100176void set_handler(int sig, void (*handler)(int))
Gennady Sharapov0805d892006-01-08 01:01:29 -0800177{
178 struct sigaction action;
Al Viroe87df982011-08-18 20:04:29 +0100179 int flags = SA_SIGINFO | SA_ONSTACK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800180 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800181
Jeff Dike4b84c692006-09-25 23:33:04 -0700182 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
Al Viro7eb12252011-08-18 20:03:39 +0100183 action.sa_sigaction = hard_handler;
Jeff Dike4b84c692006-09-25 23:33:04 -0700184
Al Viroe87df982011-08-18 20:04:29 +0100185 /* block irq ones */
Gennady Sharapov0805d892006-01-08 01:01:29 -0800186 sigemptyset(&action.sa_mask);
Al Viroe87df982011-08-18 20:04:29 +0100187 sigaddset(&action.sa_mask, SIGVTALRM);
188 sigaddset(&action.sa_mask, SIGIO);
189 sigaddset(&action.sa_mask, SIGWINCH);
Jeff Dike4b84c692006-09-25 23:33:04 -0700190
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800191 if (sig == SIGSEGV)
192 flags |= SA_NODEFER;
193
Al Viroe87df982011-08-18 20:04:29 +0100194 if (sigismember(&action.sa_mask, sig))
195 flags |= SA_RESTART; /* if it's an irq signal */
196
197 action.sa_flags = flags;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800198 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700199 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800200 panic("sigaction failed - errno = %d\n", errno);
201
202 sigemptyset(&sig_mask);
203 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700204 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800205 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800206}
207
208int change_sig(int signal, int on)
209{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800210 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800211
212 sigemptyset(&sigset);
213 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800214 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800215 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800216
217 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800218}
219
Gennady Sharapov0805d892006-01-08 01:01:29 -0800220void block_signals(void)
221{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800222 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700223 /*
224 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800225 * ensures that writes are flushed out before the return.
226 * This might matter if gcc figures out how to inline this and
227 * decides to shuffle this code into the caller.
228 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800229 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800230}
231
232void unblock_signals(void)
233{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800234 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800235
Jeff Dikeba180fd2007-10-16 01:27:00 -0700236 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800237 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800238
Jeff Dikeba180fd2007-10-16 01:27:00 -0700239 /*
240 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800241 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800242 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800243 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800244 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700245 /*
246 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800247 * way, signals_pending won't be changed while we're reading it.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800248 */
249 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800250
Jeff Dikeba180fd2007-10-16 01:27:00 -0700251 /*
Jeff Dikecfef8f32008-02-04 22:31:16 -0800252 * Setting signals_enabled and reading signals_pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800253 * happen in this order.
254 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800255 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800256
Jeff Dikecfef8f32008-02-04 22:31:16 -0800257 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800258 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800259 return;
260
Jeff Dikecfef8f32008-02-04 22:31:16 -0800261 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800262
Jeff Dikeba180fd2007-10-16 01:27:00 -0700263 /*
264 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800265 * handlers expect them off when they are called. They will
266 * be enabled again above.
267 */
268
269 signals_enabled = 0;
270
Jeff Dikeba180fd2007-10-16 01:27:00 -0700271 /*
272 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800273 * schedule, leaving the pending SIGIO stranded until we come
274 * back here.
275 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700276 if (save_pending & SIGIO_MASK)
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800277 sig_handler_common(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800278
Jeff Dikeba180fd2007-10-16 01:27:00 -0700279 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700280 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800281 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800282}
283
284int get_signals(void)
285{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800286 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800287}
288
289int set_signals(int enable)
290{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800291 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700292 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800293 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800294
Jeff Dike1d7173b2006-01-18 17:42:49 -0800295 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700296 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800297 unblock_signals();
298 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800299
Jeff Dike1d7173b2006-01-18 17:42:49 -0800300 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800301}