blob: 53593c8b9c3420761db2b1c0390454a79993bee0 [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>
Gennady Sharapovcff65c42006-01-18 17:42:42 -080012#include "os.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070013#include "sysdep/barrier.h"
14#include "sysdep/sigcontext.h"
15#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Jeff Dikeba180fd2007-10-16 01:27:00 -070017/*
18 * These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
Jeff Dike1d7173b2006-01-18 17:42:49 -080019 * together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
20 * be able to profile all of UML, not just the non-critical sections. If
21 * profiling is not thread-safe, then that is not my problem. We can disable
22 * profiling when SMP is enabled in that case.
23 */
24#define SIGIO_BIT 0
25#define SIGIO_MASK (1 << SIGIO_BIT)
26
27#define SIGVTALRM_BIT 1
28#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
29
30#define SIGALRM_BIT 2
31#define SIGALRM_MASK (1 << SIGALRM_BIT)
32
Jeff Dikeba180fd2007-10-16 01:27:00 -070033/*
34 * These are used by both the signal handlers and
Jeff Dike53b17332006-11-02 22:07:22 -080035 * block/unblock_signals. I don't want modifications cached in a
36 * register - they must go straight to memory.
37 */
38static volatile int signals_enabled = 1;
39static volatile int pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -080040
Jeff Dike4b84c692006-09-25 23:33:04 -070041void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Jeff Dike1d7173b2006-01-18 17:42:49 -080043 int enabled;
44
Jeff Dike1d7173b2006-01-18 17:42:49 -080045 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070046 if (!enabled && (sig == SIGIO)) {
Jeff Dike1d7173b2006-01-18 17:42:49 -080047 pending |= SIGIO_MASK;
48 return;
49 }
50
51 block_signals();
52
Jeff Dike6aa802c2007-10-16 01:26:56 -070053 sig_handler_common_skas(sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080054
55 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Jeff Dike1d7173b2006-01-18 17:42:49 -080058static void real_alarm_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Jeff Dike77bf4402007-10-16 01:26:58 -070060 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070061
Jeff Dikeba180fd2007-10-16 01:27:00 -070062 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -070063 copy_sc(&regs, sc);
Jeff Dike77bf4402007-10-16 01:26:58 -070064 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070065 unblock_signals();
66 timer_handler(sig, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080067}
68
Jeff Dike4b84c692006-09-25 23:33:04 -070069void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080070{
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 (!signals_enabled) {
75 if (sig == SIGVTALRM)
Jeff Dike1d7173b2006-01-18 17:42:49 -080076 pending |= SIGVTALRM_MASK;
77 else pending |= SIGALRM_MASK;
78
79 return;
80 }
81
82 block_signals();
83
84 real_alarm_handler(sig, sc);
85 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Jeff Dike78a26e22007-10-16 01:27:23 -070088void timer_init(void)
89{
90 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
91 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
92 SIGALRM, -1);
93 set_handler(SIGALRM, (__sighandler_t) alarm_handler,
94 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
95 SIGALRM, -1);
96}
97
Gennady Sharapov0805d892006-01-08 01:01:29 -080098void set_sigstack(void *sig_stack, int size)
99{
100 stack_t stack = ((stack_t) { .ss_flags = 0,
101 .ss_sp = (__ptr_t) sig_stack,
102 .ss_size = size - sizeof(void *) });
103
Jeff Dikeba180fd2007-10-16 01:27:00 -0700104 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800105 panic("enabling signal stack failed, errno = %d\n", errno);
106}
107
108void remove_sigstack(void)
109{
110 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
111 .ss_sp = NULL,
112 .ss_size = 0 });
113
Jeff Dikeba180fd2007-10-16 01:27:00 -0700114 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800115 panic("disabling signal stack failed, errno = %d\n", errno);
116}
117
Jeff Dike4b84c692006-09-25 23:33:04 -0700118void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
119
Jeff Dikec14b8492007-05-10 22:22:34 -0700120void handle_signal(int sig, struct sigcontext *sc)
121{
Jeff Dike508a9272007-09-18 22:46:49 -0700122 unsigned long pending = 1UL << sig;
Jeff Dike181bde82007-10-16 01:27:22 -0700123 int timer = switch_timers(0);
Jeff Dikec14b8492007-05-10 22:22:34 -0700124
125 do {
126 int nested, bail;
127
128 /*
129 * pending comes back with one bit set for each
130 * interrupt that arrived while setting up the stack,
131 * plus a bit for this interrupt, plus the zero bit is
132 * set if this is a nested interrupt.
133 * If bail is true, then we interrupted another
134 * handler setting up the stack. In this case, we
135 * have to return, and the upper handler will deal
136 * with this interrupt.
137 */
Jeff Dike508a9272007-09-18 22:46:49 -0700138 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700139 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700140 return;
141
142 nested = pending & 1;
143 pending &= ~1;
144
Jeff Dikeba180fd2007-10-16 01:27:00 -0700145 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700146 sig--;
147 pending &= ~(1 << sig);
148 (*handlers[sig])(sig, sc);
149 }
150
Jeff Dikeba180fd2007-10-16 01:27:00 -0700151 /*
152 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700153 * that arrived while tearing down the stack. If this
154 * is non-zero, we just go back, set up the stack
155 * again, and handle the new interrupts.
156 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700157 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700158 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700159 } while (pending);
Jeff Dike181bde82007-10-16 01:27:22 -0700160
161 switch_timers(timer);
Jeff Dikec14b8492007-05-10 22:22:34 -0700162}
163
Jeff Dike4b84c692006-09-25 23:33:04 -0700164extern void hard_handler(int sig);
165
Gennady Sharapov0805d892006-01-08 01:01:29 -0800166void set_handler(int sig, void (*handler)(int), int flags, ...)
167{
168 struct sigaction action;
169 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800170 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800171 int mask;
172
Jeff Dike4b84c692006-09-25 23:33:04 -0700173 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
174 action.sa_handler = hard_handler;
175
Gennady Sharapov0805d892006-01-08 01:01:29 -0800176 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700177
178 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700179 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800180 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800181 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700182
Gennady Sharapov0805d892006-01-08 01:01:29 -0800183 action.sa_flags = flags;
184 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700185 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800186 panic("sigaction failed - errno = %d\n", errno);
187
188 sigemptyset(&sig_mask);
189 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700190 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800191 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800192}
193
194int change_sig(int signal, int on)
195{
196 sigset_t sigset, old;
197
198 sigemptyset(&sigset);
199 sigaddset(&sigset, signal);
200 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700201 return !sigismember(&old, signal);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800202}
203
Gennady Sharapov0805d892006-01-08 01:01:29 -0800204void block_signals(void)
205{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800206 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700207 /*
208 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800209 * ensures that writes are flushed out before the return.
210 * This might matter if gcc figures out how to inline this and
211 * decides to shuffle this code into the caller.
212 */
213 mb();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800214}
215
216void unblock_signals(void)
217{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800218 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800219
Jeff Dikeba180fd2007-10-16 01:27:00 -0700220 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800221 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800222
Jeff Dikeba180fd2007-10-16 01:27:00 -0700223 /*
224 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800225 * interrupts may have arrived and we need to re-enable them and
226 * recheck pending.
227 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700228 while(1) {
229 /*
230 * Save and reset save_pending after enabling signals. This
Jeff Dike1d7173b2006-01-18 17:42:49 -0800231 * way, pending won't be changed while we're reading it.
232 */
233 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800234
Jeff Dikeba180fd2007-10-16 01:27:00 -0700235 /*
236 * Setting signals_enabled and reading pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800237 * happen in this order.
238 */
239 mb();
240
Jeff Dike1d7173b2006-01-18 17:42:49 -0800241 save_pending = pending;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700242 if (save_pending == 0) {
243 /*
244 * This must return with signals enabled, so
Jeff Dike53b17332006-11-02 22:07:22 -0800245 * this barrier ensures that writes are
246 * flushed out before the return. This might
247 * matter if gcc figures out how to inline
248 * this (unlikely, given its size) and decides
249 * to shuffle this code into the caller.
250 */
251 mb();
Jeff Dike1d7173b2006-01-18 17:42:49 -0800252 return;
Jeff Dike53b17332006-11-02 22:07:22 -0800253 }
Jeff Dike1d7173b2006-01-18 17:42:49 -0800254
255 pending = 0;
256
Jeff Dikeba180fd2007-10-16 01:27:00 -0700257 /*
258 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800259 * handlers expect them off when they are called. They will
260 * be enabled again above.
261 */
262
263 signals_enabled = 0;
264
Jeff Dikeba180fd2007-10-16 01:27:00 -0700265 /*
266 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800267 * schedule, leaving the pending SIGIO stranded until we come
268 * back here.
269 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700270 if (save_pending & SIGIO_MASK)
Jeff Dike6aa802c2007-10-16 01:26:56 -0700271 sig_handler_common_skas(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800272
Jeff Dikeba180fd2007-10-16 01:27:00 -0700273 if (save_pending & SIGALRM_MASK)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800274 real_alarm_handler(SIGALRM, NULL);
275
Jeff Dikeba180fd2007-10-16 01:27:00 -0700276 if (save_pending & SIGVTALRM_MASK)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800277 real_alarm_handler(SIGVTALRM, NULL);
278 }
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}