blob: 91a35da5fe90dadb65aa6b0f499d720c7094e21b [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;
66static unsigned int 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 Dike1d7173b2006-01-18 17:42:49 -080074 pending |= SIGIO_MASK;
75 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 Dike61b63c52007-10-16 01:27:27 -0700102 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
128void remove_sigstack(void)
129{
130 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
131 .ss_sp = NULL,
132 .ss_size = 0 });
133
Jeff Dikeba180fd2007-10-16 01:27:00 -0700134 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800135 panic("disabling signal stack failed, errno = %d\n", errno);
136}
137
Jeff Dike4b84c692006-09-25 23:33:04 -0700138void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
139
Jeff Dikec14b8492007-05-10 22:22:34 -0700140void handle_signal(int sig, struct sigcontext *sc)
141{
Jeff Dike508a9272007-09-18 22:46:49 -0700142 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700143
144 do {
145 int nested, bail;
146
147 /*
148 * pending comes back with one bit set for each
149 * interrupt that arrived while setting up the stack,
150 * plus a bit for this interrupt, plus the zero bit is
151 * set if this is a nested interrupt.
152 * If bail is true, then we interrupted another
153 * handler setting up the stack. In this case, we
154 * have to return, and the upper handler will deal
155 * with this interrupt.
156 */
Jeff Dike508a9272007-09-18 22:46:49 -0700157 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700158 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700159 return;
160
161 nested = pending & 1;
162 pending &= ~1;
163
Jeff Dikeba180fd2007-10-16 01:27:00 -0700164 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700165 sig--;
166 pending &= ~(1 << sig);
167 (*handlers[sig])(sig, sc);
168 }
169
Jeff Dikeba180fd2007-10-16 01:27:00 -0700170 /*
171 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700172 * that arrived while tearing down the stack. If this
173 * is non-zero, we just go back, set up the stack
174 * again, and handle the new interrupts.
175 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700176 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700177 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700178 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700179}
180
Jeff Dike4b84c692006-09-25 23:33:04 -0700181extern void hard_handler(int sig);
182
Gennady Sharapov0805d892006-01-08 01:01:29 -0800183void set_handler(int sig, void (*handler)(int), int flags, ...)
184{
185 struct sigaction action;
186 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800187 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800188 int mask;
189
Jeff Dike4b84c692006-09-25 23:33:04 -0700190 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
191 action.sa_handler = hard_handler;
192
Gennady Sharapov0805d892006-01-08 01:01:29 -0800193 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700194
195 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700196 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800197 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800198 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700199
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800200 if (sig == SIGSEGV)
201 flags |= SA_NODEFER;
202
Gennady Sharapov0805d892006-01-08 01:01:29 -0800203 action.sa_flags = flags;
204 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{
216 sigset_t sigset, old;
217
218 sigemptyset(&sigset);
219 sigaddset(&sigset, signal);
WANG Congc9a30722008-02-04 22:30:35 -0800220 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
221 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700222 return !sigismember(&old, signal);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800223}
224
Gennady Sharapov0805d892006-01-08 01:01:29 -0800225void block_signals(void)
226{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800227 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700228 /*
229 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800230 * ensures that writes are flushed out before the return.
231 * This might matter if gcc figures out how to inline this and
232 * decides to shuffle this code into the caller.
233 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800234 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800235}
236
237void unblock_signals(void)
238{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800239 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800240
Jeff Dikeba180fd2007-10-16 01:27:00 -0700241 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800242 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800243
Jeff Dikeba180fd2007-10-16 01:27:00 -0700244 /*
245 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800246 * interrupts may have arrived and we need to re-enable them and
247 * recheck pending.
248 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700249 while(1) {
250 /*
251 * Save and reset save_pending after enabling signals. This
Jeff Dike1d7173b2006-01-18 17:42:49 -0800252 * way, pending won't be changed while we're reading it.
253 */
254 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800255
Jeff Dikeba180fd2007-10-16 01:27:00 -0700256 /*
257 * Setting signals_enabled and reading pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800258 * happen in this order.
259 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800260 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800261
Jeff Dike1d7173b2006-01-18 17:42:49 -0800262 save_pending = pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800263 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800264 return;
265
266 pending = 0;
267
Jeff Dikeba180fd2007-10-16 01:27:00 -0700268 /*
269 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800270 * handlers expect them off when they are called. They will
271 * be enabled again above.
272 */
273
274 signals_enabled = 0;
275
Jeff Dikeba180fd2007-10-16 01:27:00 -0700276 /*
277 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800278 * schedule, leaving the pending SIGIO stranded until we come
279 * back here.
280 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700281 if (save_pending & SIGIO_MASK)
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800282 sig_handler_common(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800283
Jeff Dikeba180fd2007-10-16 01:27:00 -0700284 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700285 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800286 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800287}
288
289int get_signals(void)
290{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800291 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800292}
293
294int set_signals(int enable)
295{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800296 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700297 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800298 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800299
Jeff Dike1d7173b2006-01-18 17:42:49 -0800300 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700301 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800302 unblock_signals();
303 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800304
Jeff Dike1d7173b2006-01-18 17:42:49 -0800305 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800306}