blob: 49c113b576b7695954585e7c85c3be0fd3c1f35e [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 (sig == SIGALRM)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 switch_timers(0);
64
Jeff Dikeba180fd2007-10-16 01:27:00 -070065 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -070066 copy_sc(&regs, sc);
Jeff Dike77bf4402007-10-16 01:26:58 -070067 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070068 unblock_signals();
69 timer_handler(sig, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Jeff Dikeba180fd2007-10-16 01:27:00 -070071 if (sig == SIGALRM)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 switch_timers(1);
Jeff Dike1d7173b2006-01-18 17:42:49 -080073}
74
Jeff Dike4b84c692006-09-25 23:33:04 -070075void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080076{
Jeff Dike1d7173b2006-01-18 17:42:49 -080077 int enabled;
78
Jeff Dike1d7173b2006-01-18 17:42:49 -080079 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070080 if (!signals_enabled) {
81 if (sig == SIGVTALRM)
Jeff Dike1d7173b2006-01-18 17:42:49 -080082 pending |= SIGVTALRM_MASK;
83 else pending |= SIGALRM_MASK;
84
85 return;
86 }
87
88 block_signals();
89
90 real_alarm_handler(sig, sc);
91 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Gennady Sharapov0805d892006-01-08 01:01:29 -080094void set_sigstack(void *sig_stack, int size)
95{
96 stack_t stack = ((stack_t) { .ss_flags = 0,
97 .ss_sp = (__ptr_t) sig_stack,
98 .ss_size = size - sizeof(void *) });
99
Jeff Dikeba180fd2007-10-16 01:27:00 -0700100 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800101 panic("enabling signal stack failed, errno = %d\n", errno);
102}
103
104void remove_sigstack(void)
105{
106 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
107 .ss_sp = NULL,
108 .ss_size = 0 });
109
Jeff Dikeba180fd2007-10-16 01:27:00 -0700110 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800111 panic("disabling signal stack failed, errno = %d\n", errno);
112}
113
Jeff Dike4b84c692006-09-25 23:33:04 -0700114void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
115
Jeff Dikec14b8492007-05-10 22:22:34 -0700116void handle_signal(int sig, struct sigcontext *sc)
117{
Jeff Dike508a9272007-09-18 22:46:49 -0700118 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700119
120 do {
121 int nested, bail;
122
123 /*
124 * pending comes back with one bit set for each
125 * interrupt that arrived while setting up the stack,
126 * plus a bit for this interrupt, plus the zero bit is
127 * set if this is a nested interrupt.
128 * If bail is true, then we interrupted another
129 * handler setting up the stack. In this case, we
130 * have to return, and the upper handler will deal
131 * with this interrupt.
132 */
Jeff Dike508a9272007-09-18 22:46:49 -0700133 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700134 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700135 return;
136
137 nested = pending & 1;
138 pending &= ~1;
139
Jeff Dikeba180fd2007-10-16 01:27:00 -0700140 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700141 sig--;
142 pending &= ~(1 << sig);
143 (*handlers[sig])(sig, sc);
144 }
145
Jeff Dikeba180fd2007-10-16 01:27:00 -0700146 /*
147 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700148 * that arrived while tearing down the stack. If this
149 * is non-zero, we just go back, set up the stack
150 * again, and handle the new interrupts.
151 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700152 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700153 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700154 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700155}
156
Jeff Dike4b84c692006-09-25 23:33:04 -0700157extern void hard_handler(int sig);
158
Gennady Sharapov0805d892006-01-08 01:01:29 -0800159void set_handler(int sig, void (*handler)(int), int flags, ...)
160{
161 struct sigaction action;
162 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800163 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800164 int mask;
165
Jeff Dike4b84c692006-09-25 23:33:04 -0700166 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
167 action.sa_handler = hard_handler;
168
Gennady Sharapov0805d892006-01-08 01:01:29 -0800169 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700170
171 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700172 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800173 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800174 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700175
Gennady Sharapov0805d892006-01-08 01:01:29 -0800176 action.sa_flags = flags;
177 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700178 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800179 panic("sigaction failed - errno = %d\n", errno);
180
181 sigemptyset(&sig_mask);
182 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700183 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800184 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800185}
186
187int change_sig(int signal, int on)
188{
189 sigset_t sigset, old;
190
191 sigemptyset(&sigset);
192 sigaddset(&sigset, signal);
193 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700194 return !sigismember(&old, signal);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800195}
196
Gennady Sharapov0805d892006-01-08 01:01:29 -0800197void block_signals(void)
198{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800199 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700200 /*
201 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800202 * ensures that writes are flushed out before the return.
203 * This might matter if gcc figures out how to inline this and
204 * decides to shuffle this code into the caller.
205 */
206 mb();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800207}
208
209void unblock_signals(void)
210{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800211 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800212
Jeff Dikeba180fd2007-10-16 01:27:00 -0700213 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800214 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800215
Jeff Dikeba180fd2007-10-16 01:27:00 -0700216 /*
217 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800218 * interrupts may have arrived and we need to re-enable them and
219 * recheck pending.
220 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700221 while(1) {
222 /*
223 * Save and reset save_pending after enabling signals. This
Jeff Dike1d7173b2006-01-18 17:42:49 -0800224 * way, pending won't be changed while we're reading it.
225 */
226 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800227
Jeff Dikeba180fd2007-10-16 01:27:00 -0700228 /*
229 * Setting signals_enabled and reading pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800230 * happen in this order.
231 */
232 mb();
233
Jeff Dike1d7173b2006-01-18 17:42:49 -0800234 save_pending = pending;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700235 if (save_pending == 0) {
236 /*
237 * This must return with signals enabled, so
Jeff Dike53b17332006-11-02 22:07:22 -0800238 * this barrier ensures that writes are
239 * flushed out before the return. This might
240 * matter if gcc figures out how to inline
241 * this (unlikely, given its size) and decides
242 * to shuffle this code into the caller.
243 */
244 mb();
Jeff Dike1d7173b2006-01-18 17:42:49 -0800245 return;
Jeff Dike53b17332006-11-02 22:07:22 -0800246 }
Jeff Dike1d7173b2006-01-18 17:42:49 -0800247
248 pending = 0;
249
Jeff Dikeba180fd2007-10-16 01:27:00 -0700250 /*
251 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800252 * handlers expect them off when they are called. They will
253 * be enabled again above.
254 */
255
256 signals_enabled = 0;
257
Jeff Dikeba180fd2007-10-16 01:27:00 -0700258 /*
259 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800260 * schedule, leaving the pending SIGIO stranded until we come
261 * back here.
262 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700263 if (save_pending & SIGIO_MASK)
Jeff Dike6aa802c2007-10-16 01:26:56 -0700264 sig_handler_common_skas(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800265
Jeff Dikeba180fd2007-10-16 01:27:00 -0700266 if (save_pending & SIGALRM_MASK)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800267 real_alarm_handler(SIGALRM, NULL);
268
Jeff Dikeba180fd2007-10-16 01:27:00 -0700269 if (save_pending & SIGVTALRM_MASK)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800270 real_alarm_handler(SIGVTALRM, NULL);
271 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800272}
273
274int get_signals(void)
275{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800276 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800277}
278
279int set_signals(int enable)
280{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800281 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700282 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800283 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800284
Jeff Dike1d7173b2006-01-18 17:42:49 -0800285 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700286 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800287 unblock_signals();
288 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800289
Jeff Dike1d7173b2006-01-18 17:42:49 -0800290 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800291}