blob: cde9e766b5b4512930577bf368c625d261f386f4 [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"
13#include "kern_constants.h"
Jeff Dikeedea1382008-02-04 22:30:46 -080014#include "kern_util.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080015#include "os.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070016#include "sysdep/barrier.h"
17#include "sysdep/sigcontext.h"
Jeff Dike75ada8f2008-02-04 22:31:12 -080018#include "task.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070019#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jeff Dike75ada8f2008-02-04 22:31:12 -080021void (*sig_info[NSIG])(int, struct uml_pt_regs *) = {
22 [SIGTRAP] = relay_signal,
23 [SIGFPE] = relay_signal,
24 [SIGILL] = relay_signal,
25 [SIGWINCH] = winch,
26 [SIGBUS] = bus_handler,
27 [SIGSEGV] = segv_handler,
28 [SIGIO] = sigio_handler,
29 [SIGVTALRM] = timer_handler };
30
31static struct uml_pt_regs ksig_regs[UM_NR_CPUS];
32
33void sig_handler_common_skas(int sig, void *sc_ptr)
34{
35 struct sigcontext *sc = sc_ptr;
36 struct uml_pt_regs *r;
37 void (*handler)(int, struct uml_pt_regs *);
38 int save_user, save_errno = errno;
39
40 /*
41 * This is done because to allow SIGSEGV to be delivered inside a SEGV
42 * handler. This can happen in copy_user, and if SEGV is disabled,
43 * the process will die.
44 * XXX Figure out why this is better than SA_NODEFER
45 */
46 if (sig == SIGSEGV) {
47 change_sig(SIGSEGV, 1);
48 /*
49 * For segfaults, we want the data from the
50 * sigcontext. In this case, we don't want to mangle
51 * the process registers, so use a static set of
52 * registers. For other signals, the process
53 * registers are OK.
54 */
55 r = &ksig_regs[cpu()];
56 copy_sc(r, sc_ptr);
57 } else
58 r = TASK_REGS(get_current());
59
60 save_user = r->is_user;
61 r->is_user = 0;
62 if ((sig == SIGFPE) || (sig == SIGSEGV) || (sig == SIGBUS) ||
63 (sig == SIGILL) || (sig == SIGTRAP))
64 GET_FAULTINFO_FROM_SC(r->faultinfo, sc);
65
66 change_sig(SIGUSR1, 1);
67
68 handler = sig_info[sig];
69
70 /* unblock SIGVTALRM, SIGIO if sig isn't IRQ signal */
71 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
72 unblock_signals();
73
74 handler(sig, r);
75
76 errno = save_errno;
77 r->is_user = save_user;
78}
79
Jeff Dikefce8c412008-02-04 22:31:09 -080080/* Copied from linux/compiler-gcc.h since we can't include it directly */
81#define barrier() __asm__ __volatile__("": : :"memory")
82
Jeff Dikeba180fd2007-10-16 01:27:00 -070083/*
Jeff Dike61b63c52007-10-16 01:27:27 -070084 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080085 * be able to profile all of UML, not just the non-critical sections. If
86 * profiling is not thread-safe, then that is not my problem. We can disable
87 * profiling when SMP is enabled in that case.
88 */
89#define SIGIO_BIT 0
90#define SIGIO_MASK (1 << SIGIO_BIT)
91
92#define SIGVTALRM_BIT 1
93#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
94
Jeff Dikefce8c412008-02-04 22:31:09 -080095static int signals_enabled;
96static unsigned int pending;
Jeff Dike1d7173b2006-01-18 17:42:49 -080097
Jeff Dike4b84c692006-09-25 23:33:04 -070098void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800100 int enabled;
101
Jeff Dike1d7173b2006-01-18 17:42:49 -0800102 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700103 if (!enabled && (sig == SIGIO)) {
Jeff Dike1d7173b2006-01-18 17:42:49 -0800104 pending |= SIGIO_MASK;
105 return;
106 }
107
108 block_signals();
109
Jeff Dike6aa802c2007-10-16 01:26:56 -0700110 sig_handler_common_skas(sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800111
112 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Jeff Dike61b63c52007-10-16 01:27:27 -0700115static void real_alarm_handler(struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jeff Dike77bf4402007-10-16 01:26:58 -0700117 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700118
Jeff Dikeba180fd2007-10-16 01:27:00 -0700119 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700120 copy_sc(&regs, sc);
Jeff Dike77bf4402007-10-16 01:26:58 -0700121 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700122 unblock_signals();
Jeff Dike61b63c52007-10-16 01:27:27 -0700123 timer_handler(SIGVTALRM, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800124}
125
Jeff Dike4b84c692006-09-25 23:33:04 -0700126void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800127{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800128 int enabled;
129
Jeff Dike1d7173b2006-01-18 17:42:49 -0800130 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700131 if (!signals_enabled) {
Jeff Dike61b63c52007-10-16 01:27:27 -0700132 pending |= SIGVTALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800133 return;
134 }
135
136 block_signals();
137
Jeff Dike61b63c52007-10-16 01:27:27 -0700138 real_alarm_handler(sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800139 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Jeff Dike78a26e22007-10-16 01:27:23 -0700142void timer_init(void)
143{
144 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
Jeff Dike61b63c52007-10-16 01:27:27 -0700145 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
Jeff Dike78a26e22007-10-16 01:27:23 -0700146}
147
Gennady Sharapov0805d892006-01-08 01:01:29 -0800148void set_sigstack(void *sig_stack, int size)
149{
150 stack_t stack = ((stack_t) { .ss_flags = 0,
151 .ss_sp = (__ptr_t) sig_stack,
152 .ss_size = size - sizeof(void *) });
153
Jeff Dikeba180fd2007-10-16 01:27:00 -0700154 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800155 panic("enabling signal stack failed, errno = %d\n", errno);
156}
157
158void remove_sigstack(void)
159{
160 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
161 .ss_sp = NULL,
162 .ss_size = 0 });
163
Jeff Dikeba180fd2007-10-16 01:27:00 -0700164 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800165 panic("disabling signal stack failed, errno = %d\n", errno);
166}
167
Jeff Dike4b84c692006-09-25 23:33:04 -0700168void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
169
Jeff Dikec14b8492007-05-10 22:22:34 -0700170void handle_signal(int sig, struct sigcontext *sc)
171{
Jeff Dike508a9272007-09-18 22:46:49 -0700172 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700173
174 do {
175 int nested, bail;
176
177 /*
178 * pending comes back with one bit set for each
179 * interrupt that arrived while setting up the stack,
180 * plus a bit for this interrupt, plus the zero bit is
181 * set if this is a nested interrupt.
182 * If bail is true, then we interrupted another
183 * handler setting up the stack. In this case, we
184 * have to return, and the upper handler will deal
185 * with this interrupt.
186 */
Jeff Dike508a9272007-09-18 22:46:49 -0700187 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700188 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700189 return;
190
191 nested = pending & 1;
192 pending &= ~1;
193
Jeff Dikeba180fd2007-10-16 01:27:00 -0700194 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700195 sig--;
196 pending &= ~(1 << sig);
197 (*handlers[sig])(sig, sc);
198 }
199
Jeff Dikeba180fd2007-10-16 01:27:00 -0700200 /*
201 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700202 * that arrived while tearing down the stack. If this
203 * is non-zero, we just go back, set up the stack
204 * again, and handle the new interrupts.
205 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700206 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700207 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700209}
210
Jeff Dike4b84c692006-09-25 23:33:04 -0700211extern void hard_handler(int sig);
212
Gennady Sharapov0805d892006-01-08 01:01:29 -0800213void set_handler(int sig, void (*handler)(int), int flags, ...)
214{
215 struct sigaction action;
216 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800217 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800218 int mask;
219
Jeff Dike4b84c692006-09-25 23:33:04 -0700220 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
221 action.sa_handler = hard_handler;
222
Gennady Sharapov0805d892006-01-08 01:01:29 -0800223 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700224
225 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700226 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800227 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800228 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700229
Gennady Sharapov0805d892006-01-08 01:01:29 -0800230 action.sa_flags = flags;
231 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700232 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800233 panic("sigaction failed - errno = %d\n", errno);
234
235 sigemptyset(&sig_mask);
236 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700237 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800238 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800239}
240
241int change_sig(int signal, int on)
242{
243 sigset_t sigset, old;
244
245 sigemptyset(&sigset);
246 sigaddset(&sigset, signal);
WANG Congc9a30722008-02-04 22:30:35 -0800247 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
248 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700249 return !sigismember(&old, signal);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800250}
251
Gennady Sharapov0805d892006-01-08 01:01:29 -0800252void block_signals(void)
253{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800254 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700255 /*
256 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800257 * ensures that writes are flushed out before the return.
258 * This might matter if gcc figures out how to inline this and
259 * decides to shuffle this code into the caller.
260 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800261 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800262}
263
264void unblock_signals(void)
265{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800266 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800267
Jeff Dikeba180fd2007-10-16 01:27:00 -0700268 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800269 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800270
Jeff Dikeba180fd2007-10-16 01:27:00 -0700271 /*
272 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800273 * interrupts may have arrived and we need to re-enable them and
274 * recheck pending.
275 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700276 while(1) {
277 /*
278 * Save and reset save_pending after enabling signals. This
Jeff Dike1d7173b2006-01-18 17:42:49 -0800279 * way, pending won't be changed while we're reading it.
280 */
281 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800282
Jeff Dikeba180fd2007-10-16 01:27:00 -0700283 /*
284 * Setting signals_enabled and reading pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800285 * happen in this order.
286 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800287 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800288
Jeff Dike1d7173b2006-01-18 17:42:49 -0800289 save_pending = pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800290 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800291 return;
292
293 pending = 0;
294
Jeff Dikeba180fd2007-10-16 01:27:00 -0700295 /*
296 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800297 * handlers expect them off when they are called. They will
298 * be enabled again above.
299 */
300
301 signals_enabled = 0;
302
Jeff Dikeba180fd2007-10-16 01:27:00 -0700303 /*
304 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800305 * schedule, leaving the pending SIGIO stranded until we come
306 * back here.
307 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700308 if (save_pending & SIGIO_MASK)
Jeff Dike6aa802c2007-10-16 01:26:56 -0700309 sig_handler_common_skas(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800310
Jeff Dikeba180fd2007-10-16 01:27:00 -0700311 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700312 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800313 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800314}
315
316int get_signals(void)
317{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800318 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800319}
320
321int set_signals(int enable)
322{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800323 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700324 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800325 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800326
Jeff Dike1d7173b2006-01-18 17:42:49 -0800327 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700328 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800329 unblock_signals();
330 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800331
Jeff Dike1d7173b2006-01-18 17:42:49 -0800332 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800333}