blob: 6ae180703a6361bf9a8f0a7efd01b17e72e6da0f [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"
18#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080020/* Copied from linux/compiler-gcc.h since we can't include it directly */
21#define barrier() __asm__ __volatile__("": : :"memory")
22
Jeff Dike75ada8f2008-02-04 22:31:12 -080023void (*sig_info[NSIG])(int, struct uml_pt_regs *) = {
24 [SIGTRAP] = relay_signal,
25 [SIGFPE] = relay_signal,
26 [SIGILL] = relay_signal,
27 [SIGWINCH] = winch,
28 [SIGBUS] = bus_handler,
29 [SIGSEGV] = segv_handler,
30 [SIGIO] = sigio_handler,
31 [SIGVTALRM] = timer_handler };
32
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080033static void sig_handler_common(int sig, struct sigcontext *sc)
Jeff Dike75ada8f2008-02-04 22:31:12 -080034{
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080035 struct uml_pt_regs r;
36 int save_errno = errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080037
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080038 r.is_user = 0;
Jeff Dike75ada8f2008-02-04 22:31:12 -080039 if (sig == SIGSEGV) {
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080040 /* For segfaults, we want the data from the sigcontext. */
41 copy_sc(&r, sc);
42 GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
43 }
Jeff Dike75ada8f2008-02-04 22:31:12 -080044
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080045 /* enable signals if sig isn't IRQ signal */
Jeff Dike75ada8f2008-02-04 22:31:12 -080046 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
47 unblock_signals();
48
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080049 (*sig_info[sig])(sig, &r);
Jeff Dike75ada8f2008-02-04 22:31:12 -080050
51 errno = save_errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080052}
53
Jeff Dikeba180fd2007-10-16 01:27:00 -070054/*
Jeff Dike61b63c52007-10-16 01:27:27 -070055 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080056 * be able to profile all of UML, not just the non-critical sections. If
57 * profiling is not thread-safe, then that is not my problem. We can disable
58 * profiling when SMP is enabled in that case.
59 */
60#define SIGIO_BIT 0
61#define SIGIO_MASK (1 << SIGIO_BIT)
62
63#define SIGVTALRM_BIT 1
64#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
65
Jeff Dikefce8c412008-02-04 22:31:09 -080066static int signals_enabled;
Jeff Dikecfef8f32008-02-04 22:31:16 -080067static unsigned int signals_pending;
Jeff Dike1d7173b2006-01-18 17:42:49 -080068
Jeff Dike4b84c692006-09-25 23:33:04 -070069void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Jeff Dike1d7173b2006-01-18 17:42:49 -080071 int enabled;
72
Jeff Dike1d7173b2006-01-18 17:42:49 -080073 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070074 if (!enabled && (sig == SIGIO)) {
Jeff Dikecfef8f32008-02-04 22:31:16 -080075 signals_pending |= SIGIO_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080076 return;
77 }
78
79 block_signals();
80
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080081 sig_handler_common(sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080082
83 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Jeff Dike61b63c52007-10-16 01:27:27 -070086static void real_alarm_handler(struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Jeff Dike77bf4402007-10-16 01:26:58 -070088 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070089
Jeff Dikeba180fd2007-10-16 01:27:00 -070090 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -070091 copy_sc(&regs, sc);
Jeff Dike77bf4402007-10-16 01:26:58 -070092 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070093 unblock_signals();
Jeff Dike61b63c52007-10-16 01:27:27 -070094 timer_handler(SIGVTALRM, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080095}
96
Jeff Dike4b84c692006-09-25 23:33:04 -070097void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080098{
Jeff Dike1d7173b2006-01-18 17:42:49 -080099 int enabled;
100
Jeff Dike1d7173b2006-01-18 17:42:49 -0800101 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700102 if (!signals_enabled) {
Jeff Dikecfef8f32008-02-04 22:31:16 -0800103 signals_pending |= SIGVTALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800104 return;
105 }
106
107 block_signals();
108
Jeff Dike61b63c52007-10-16 01:27:27 -0700109 real_alarm_handler(sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800110 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Jeff Dike78a26e22007-10-16 01:27:23 -0700113void timer_init(void)
114{
115 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
Jeff Dike61b63c52007-10-16 01:27:27 -0700116 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
Jeff Dike78a26e22007-10-16 01:27:23 -0700117}
118
Gennady Sharapov0805d892006-01-08 01:01:29 -0800119void set_sigstack(void *sig_stack, int size)
120{
121 stack_t stack = ((stack_t) { .ss_flags = 0,
122 .ss_sp = (__ptr_t) sig_stack,
123 .ss_size = size - sizeof(void *) });
124
Jeff Dikeba180fd2007-10-16 01:27:00 -0700125 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800126 panic("enabling signal stack failed, errno = %d\n", errno);
127}
128
WANG Cong99764fa2008-07-23 21:28:49 -0700129static void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
Jeff Dike4b84c692006-09-25 23:33:04 -0700130
Jeff Dikec14b8492007-05-10 22:22:34 -0700131void handle_signal(int sig, struct sigcontext *sc)
132{
Jeff Dike508a9272007-09-18 22:46:49 -0700133 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700134
135 do {
136 int nested, bail;
137
138 /*
139 * pending comes back with one bit set for each
140 * interrupt that arrived while setting up the stack,
141 * plus a bit for this interrupt, plus the zero bit is
142 * set if this is a nested interrupt.
143 * If bail is true, then we interrupted another
144 * handler setting up the stack. In this case, we
145 * have to return, and the upper handler will deal
146 * with this interrupt.
147 */
Jeff Dike508a9272007-09-18 22:46:49 -0700148 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700149 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700150 return;
151
152 nested = pending & 1;
153 pending &= ~1;
154
Jeff Dikeba180fd2007-10-16 01:27:00 -0700155 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700156 sig--;
157 pending &= ~(1 << sig);
158 (*handlers[sig])(sig, sc);
159 }
160
Jeff Dikeba180fd2007-10-16 01:27:00 -0700161 /*
162 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700163 * that arrived while tearing down the stack. If this
164 * is non-zero, we just go back, set up the stack
165 * again, and handle the new interrupts.
166 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700167 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700168 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700169 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700170}
171
Jeff Dike4b84c692006-09-25 23:33:04 -0700172extern void hard_handler(int sig);
173
Gennady Sharapov0805d892006-01-08 01:01:29 -0800174void set_handler(int sig, void (*handler)(int), int flags, ...)
175{
176 struct sigaction action;
177 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800178 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800179 int mask;
180
Jeff Dike4b84c692006-09-25 23:33:04 -0700181 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
182 action.sa_handler = hard_handler;
183
Gennady Sharapov0805d892006-01-08 01:01:29 -0800184 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700185
186 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700187 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800188 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800189 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700190
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800191 if (sig == SIGSEGV)
192 flags |= SA_NODEFER;
193
Gennady Sharapov0805d892006-01-08 01:01:29 -0800194 action.sa_flags = flags;
195 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700196 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800197 panic("sigaction failed - errno = %d\n", errno);
198
199 sigemptyset(&sig_mask);
200 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700201 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800202 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800203}
204
205int change_sig(int signal, int on)
206{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800207 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800208
209 sigemptyset(&sigset);
210 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800211 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800212 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800213
214 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800215}
216
Gennady Sharapov0805d892006-01-08 01:01:29 -0800217void block_signals(void)
218{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800219 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700220 /*
221 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800222 * ensures that writes are flushed out before the return.
223 * This might matter if gcc figures out how to inline this and
224 * decides to shuffle this code into the caller.
225 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800226 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800227}
228
229void unblock_signals(void)
230{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800231 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800232
Jeff Dikeba180fd2007-10-16 01:27:00 -0700233 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800234 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800235
Jeff Dikeba180fd2007-10-16 01:27:00 -0700236 /*
237 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800238 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800239 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800240 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800241 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700242 /*
243 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800244 * way, signals_pending won't be changed while we're reading it.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800245 */
246 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800247
Jeff Dikeba180fd2007-10-16 01:27:00 -0700248 /*
Jeff Dikecfef8f32008-02-04 22:31:16 -0800249 * Setting signals_enabled and reading signals_pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800250 * happen in this order.
251 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800252 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800253
Jeff Dikecfef8f32008-02-04 22:31:16 -0800254 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800255 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800256 return;
257
Jeff Dikecfef8f32008-02-04 22:31:16 -0800258 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800259
Jeff Dikeba180fd2007-10-16 01:27:00 -0700260 /*
261 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800262 * handlers expect them off when they are called. They will
263 * be enabled again above.
264 */
265
266 signals_enabled = 0;
267
Jeff Dikeba180fd2007-10-16 01:27:00 -0700268 /*
269 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800270 * schedule, leaving the pending SIGIO stranded until we come
271 * back here.
272 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700273 if (save_pending & SIGIO_MASK)
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800274 sig_handler_common(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800275
Jeff Dikeba180fd2007-10-16 01:27:00 -0700276 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700277 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800278 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800279}
280
281int get_signals(void)
282{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800283 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800284}
285
286int set_signals(int enable)
287{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800288 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700289 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800290 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800291
Jeff Dike1d7173b2006-01-18 17:42:49 -0800292 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700293 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800294 unblock_signals();
295 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800296
Jeff Dike1d7173b2006-01-18 17:42:49 -0800297 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800298}