blob: 1c5267ec13b0943e324f32528b1eb5d77405af2d [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
Gennady Sharapov0805d892006-01-08 01:01:29 -080088void set_sigstack(void *sig_stack, int size)
89{
90 stack_t stack = ((stack_t) { .ss_flags = 0,
91 .ss_sp = (__ptr_t) sig_stack,
92 .ss_size = size - sizeof(void *) });
93
Jeff Dikeba180fd2007-10-16 01:27:00 -070094 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -080095 panic("enabling signal stack failed, errno = %d\n", errno);
96}
97
98void remove_sigstack(void)
99{
100 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
101 .ss_sp = NULL,
102 .ss_size = 0 });
103
Jeff Dikeba180fd2007-10-16 01:27:00 -0700104 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800105 panic("disabling signal stack failed, errno = %d\n", errno);
106}
107
Jeff Dike4b84c692006-09-25 23:33:04 -0700108void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
109
Jeff Dikec14b8492007-05-10 22:22:34 -0700110void handle_signal(int sig, struct sigcontext *sc)
111{
Jeff Dike508a9272007-09-18 22:46:49 -0700112 unsigned long pending = 1UL << sig;
Jeff Dike181bde82007-10-16 01:27:22 -0700113 int timer = switch_timers(0);
Jeff Dikec14b8492007-05-10 22:22:34 -0700114
115 do {
116 int nested, bail;
117
118 /*
119 * pending comes back with one bit set for each
120 * interrupt that arrived while setting up the stack,
121 * plus a bit for this interrupt, plus the zero bit is
122 * set if this is a nested interrupt.
123 * If bail is true, then we interrupted another
124 * handler setting up the stack. In this case, we
125 * have to return, and the upper handler will deal
126 * with this interrupt.
127 */
Jeff Dike508a9272007-09-18 22:46:49 -0700128 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700130 return;
131
132 nested = pending & 1;
133 pending &= ~1;
134
Jeff Dikeba180fd2007-10-16 01:27:00 -0700135 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700136 sig--;
137 pending &= ~(1 << sig);
138 (*handlers[sig])(sig, sc);
139 }
140
Jeff Dikeba180fd2007-10-16 01:27:00 -0700141 /*
142 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700143 * that arrived while tearing down the stack. If this
144 * is non-zero, we just go back, set up the stack
145 * again, and handle the new interrupts.
146 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700147 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700148 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700149 } while (pending);
Jeff Dike181bde82007-10-16 01:27:22 -0700150
151 switch_timers(timer);
Jeff Dikec14b8492007-05-10 22:22:34 -0700152}
153
Jeff Dike4b84c692006-09-25 23:33:04 -0700154extern void hard_handler(int sig);
155
Gennady Sharapov0805d892006-01-08 01:01:29 -0800156void set_handler(int sig, void (*handler)(int), int flags, ...)
157{
158 struct sigaction action;
159 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800160 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800161 int mask;
162
Jeff Dike4b84c692006-09-25 23:33:04 -0700163 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
164 action.sa_handler = hard_handler;
165
Gennady Sharapov0805d892006-01-08 01:01:29 -0800166 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700167
168 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700169 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800170 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800171 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700172
Gennady Sharapov0805d892006-01-08 01:01:29 -0800173 action.sa_flags = flags;
174 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700175 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800176 panic("sigaction failed - errno = %d\n", errno);
177
178 sigemptyset(&sig_mask);
179 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700180 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800181 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800182}
183
184int change_sig(int signal, int on)
185{
186 sigset_t sigset, old;
187
188 sigemptyset(&sigset);
189 sigaddset(&sigset, signal);
190 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700191 return !sigismember(&old, signal);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800192}
193
Gennady Sharapov0805d892006-01-08 01:01:29 -0800194void block_signals(void)
195{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800196 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700197 /*
198 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800199 * ensures that writes are flushed out before the return.
200 * This might matter if gcc figures out how to inline this and
201 * decides to shuffle this code into the caller.
202 */
203 mb();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800204}
205
206void unblock_signals(void)
207{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800208 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800209
Jeff Dikeba180fd2007-10-16 01:27:00 -0700210 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800211 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800212
Jeff Dikeba180fd2007-10-16 01:27:00 -0700213 /*
214 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800215 * interrupts may have arrived and we need to re-enable them and
216 * recheck pending.
217 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700218 while(1) {
219 /*
220 * Save and reset save_pending after enabling signals. This
Jeff Dike1d7173b2006-01-18 17:42:49 -0800221 * way, pending won't be changed while we're reading it.
222 */
223 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800224
Jeff Dikeba180fd2007-10-16 01:27:00 -0700225 /*
226 * Setting signals_enabled and reading pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800227 * happen in this order.
228 */
229 mb();
230
Jeff Dike1d7173b2006-01-18 17:42:49 -0800231 save_pending = pending;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700232 if (save_pending == 0) {
233 /*
234 * This must return with signals enabled, so
Jeff Dike53b17332006-11-02 22:07:22 -0800235 * this barrier ensures that writes are
236 * flushed out before the return. This might
237 * matter if gcc figures out how to inline
238 * this (unlikely, given its size) and decides
239 * to shuffle this code into the caller.
240 */
241 mb();
Jeff Dike1d7173b2006-01-18 17:42:49 -0800242 return;
Jeff Dike53b17332006-11-02 22:07:22 -0800243 }
Jeff Dike1d7173b2006-01-18 17:42:49 -0800244
245 pending = 0;
246
Jeff Dikeba180fd2007-10-16 01:27:00 -0700247 /*
248 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800249 * handlers expect them off when they are called. They will
250 * be enabled again above.
251 */
252
253 signals_enabled = 0;
254
Jeff Dikeba180fd2007-10-16 01:27:00 -0700255 /*
256 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800257 * schedule, leaving the pending SIGIO stranded until we come
258 * back here.
259 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700260 if (save_pending & SIGIO_MASK)
Jeff Dike6aa802c2007-10-16 01:26:56 -0700261 sig_handler_common_skas(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800262
Jeff Dikeba180fd2007-10-16 01:27:00 -0700263 if (save_pending & SIGALRM_MASK)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800264 real_alarm_handler(SIGALRM, NULL);
265
Jeff Dikeba180fd2007-10-16 01:27:00 -0700266 if (save_pending & SIGVTALRM_MASK)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800267 real_alarm_handler(SIGVTALRM, NULL);
268 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800269}
270
271int get_signals(void)
272{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800273 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800274}
275
276int set_signals(int enable)
277{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800278 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700279 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800280 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800281
Jeff Dike1d7173b2006-01-18 17:42:49 -0800282 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700283 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800284 unblock_signals();
285 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800286
Jeff Dike1d7173b2006-01-18 17:42:49 -0800287 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800288}