blob: 3f1694b134cb2606efa628deb4291c5813a549c0 [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 Dikeba180fd2007-10-16 01:27:00 -070015#include "sysdep/barrier.h"
16#include "sysdep/sigcontext.h"
17#include "user.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{
114 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
Jeff Dike61b63c52007-10-16 01:27:27 -0700115 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
Jeff Dike78a26e22007-10-16 01:27:23 -0700116}
117
Gennady Sharapov0805d892006-01-08 01:01:29 -0800118void set_sigstack(void *sig_stack, int size)
119{
120 stack_t stack = ((stack_t) { .ss_flags = 0,
121 .ss_sp = (__ptr_t) sig_stack,
122 .ss_size = size - sizeof(void *) });
123
Jeff Dikeba180fd2007-10-16 01:27:00 -0700124 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800125 panic("enabling signal stack failed, errno = %d\n", errno);
126}
127
Jeff Dike4b84c692006-09-25 23:33:04 -0700128void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
129
Jeff Dikec14b8492007-05-10 22:22:34 -0700130void handle_signal(int sig, struct sigcontext *sc)
131{
Jeff Dike508a9272007-09-18 22:46:49 -0700132 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700133
134 do {
135 int nested, bail;
136
137 /*
138 * pending comes back with one bit set for each
139 * interrupt that arrived while setting up the stack,
140 * plus a bit for this interrupt, plus the zero bit is
141 * set if this is a nested interrupt.
142 * If bail is true, then we interrupted another
143 * handler setting up the stack. In this case, we
144 * have to return, and the upper handler will deal
145 * with this interrupt.
146 */
Jeff Dike508a9272007-09-18 22:46:49 -0700147 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700148 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700149 return;
150
151 nested = pending & 1;
152 pending &= ~1;
153
Jeff Dikeba180fd2007-10-16 01:27:00 -0700154 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700155 sig--;
156 pending &= ~(1 << sig);
157 (*handlers[sig])(sig, sc);
158 }
159
Jeff Dikeba180fd2007-10-16 01:27:00 -0700160 /*
161 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700162 * that arrived while tearing down the stack. If this
163 * is non-zero, we just go back, set up the stack
164 * again, and handle the new interrupts.
165 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700166 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700167 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700168 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700169}
170
Jeff Dike4b84c692006-09-25 23:33:04 -0700171extern void hard_handler(int sig);
172
Gennady Sharapov0805d892006-01-08 01:01:29 -0800173void set_handler(int sig, void (*handler)(int), int flags, ...)
174{
175 struct sigaction action;
176 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800177 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800178 int mask;
179
Jeff Dike4b84c692006-09-25 23:33:04 -0700180 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
181 action.sa_handler = hard_handler;
182
Gennady Sharapov0805d892006-01-08 01:01:29 -0800183 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700184
185 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700186 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800187 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800188 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700189
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800190 if (sig == SIGSEGV)
191 flags |= SA_NODEFER;
192
Gennady Sharapov0805d892006-01-08 01:01:29 -0800193 action.sa_flags = flags;
194 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700195 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800196 panic("sigaction failed - errno = %d\n", errno);
197
198 sigemptyset(&sig_mask);
199 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700200 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800201 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800202}
203
204int change_sig(int signal, int on)
205{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800206 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800207
208 sigemptyset(&sigset);
209 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800210 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800211 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800212
213 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800214}
215
Gennady Sharapov0805d892006-01-08 01:01:29 -0800216void block_signals(void)
217{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800218 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700219 /*
220 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800221 * ensures that writes are flushed out before the return.
222 * This might matter if gcc figures out how to inline this and
223 * decides to shuffle this code into the caller.
224 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800225 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800226}
227
228void unblock_signals(void)
229{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800230 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800231
Jeff Dikeba180fd2007-10-16 01:27:00 -0700232 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800233 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800234
Jeff Dikeba180fd2007-10-16 01:27:00 -0700235 /*
236 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800237 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800238 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800239 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800240 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700241 /*
242 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800243 * way, signals_pending won't be changed while we're reading it.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800244 */
245 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800246
Jeff Dikeba180fd2007-10-16 01:27:00 -0700247 /*
Jeff Dikecfef8f32008-02-04 22:31:16 -0800248 * Setting signals_enabled and reading signals_pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800249 * happen in this order.
250 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800251 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800252
Jeff Dikecfef8f32008-02-04 22:31:16 -0800253 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800254 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800255 return;
256
Jeff Dikecfef8f32008-02-04 22:31:16 -0800257 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800258
Jeff Dikeba180fd2007-10-16 01:27:00 -0700259 /*
260 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800261 * handlers expect them off when they are called. They will
262 * be enabled again above.
263 */
264
265 signals_enabled = 0;
266
Jeff Dikeba180fd2007-10-16 01:27:00 -0700267 /*
268 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800269 * schedule, leaving the pending SIGIO stranded until we come
270 * back here.
271 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700272 if (save_pending & SIGIO_MASK)
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800273 sig_handler_common(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800274
Jeff Dikeba180fd2007-10-16 01:27:00 -0700275 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700276 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800277 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800278}
279
280int get_signals(void)
281{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800282 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800283}
284
285int set_signals(int enable)
286{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800287 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700288 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800289 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800290
Jeff Dike1d7173b2006-01-18 17:42:49 -0800291 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700292 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800293 unblock_signals();
294 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800295
Jeff Dike1d7173b2006-01-18 17:42:49 -0800296 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800297}