blob: 7ff8f57b7150c56fcce0495466fb98e8580d5810 [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 Dikeedea1382008-02-04 22:30:46 -080012#include "kern_util.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080013#include "os.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070014#include "sysdep/barrier.h"
15#include "sysdep/sigcontext.h"
16#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Jeff Dikeba180fd2007-10-16 01:27:00 -070018/*
Jeff Dike61b63c52007-10-16 01:27:27 -070019 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080020 * 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
Jeff Dikeba180fd2007-10-16 01:27:00 -070030/*
31 * These are used by both the signal handlers and
Jeff Dike53b17332006-11-02 22:07:22 -080032 * block/unblock_signals. I don't want modifications cached in a
33 * register - they must go straight to memory.
34 */
35static volatile int signals_enabled = 1;
36static volatile int pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -080037
Jeff Dike4b84c692006-09-25 23:33:04 -070038void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Jeff Dike1d7173b2006-01-18 17:42:49 -080040 int enabled;
41
Jeff Dike1d7173b2006-01-18 17:42:49 -080042 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070043 if (!enabled && (sig == SIGIO)) {
Jeff Dike1d7173b2006-01-18 17:42:49 -080044 pending |= SIGIO_MASK;
45 return;
46 }
47
48 block_signals();
49
Jeff Dike6aa802c2007-10-16 01:26:56 -070050 sig_handler_common_skas(sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080051
52 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Jeff Dike61b63c52007-10-16 01:27:27 -070055static void real_alarm_handler(struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Jeff Dike77bf4402007-10-16 01:26:58 -070057 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070058
Jeff Dikeba180fd2007-10-16 01:27:00 -070059 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -070060 copy_sc(&regs, sc);
Jeff Dike77bf4402007-10-16 01:26:58 -070061 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070062 unblock_signals();
Jeff Dike61b63c52007-10-16 01:27:27 -070063 timer_handler(SIGVTALRM, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080064}
65
Jeff Dike4b84c692006-09-25 23:33:04 -070066void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080067{
Jeff Dike1d7173b2006-01-18 17:42:49 -080068 int enabled;
69
Jeff Dike1d7173b2006-01-18 17:42:49 -080070 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070071 if (!signals_enabled) {
Jeff Dike61b63c52007-10-16 01:27:27 -070072 pending |= SIGVTALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080073 return;
74 }
75
76 block_signals();
77
Jeff Dike61b63c52007-10-16 01:27:27 -070078 real_alarm_handler(sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080079 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Jeff Dike78a26e22007-10-16 01:27:23 -070082void timer_init(void)
83{
84 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
Jeff Dike61b63c52007-10-16 01:27:27 -070085 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
Jeff Dike78a26e22007-10-16 01:27:23 -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 Dikec14b8492007-05-10 22:22:34 -0700113
114 do {
115 int nested, bail;
116
117 /*
118 * pending comes back with one bit set for each
119 * interrupt that arrived while setting up the stack,
120 * plus a bit for this interrupt, plus the zero bit is
121 * set if this is a nested interrupt.
122 * If bail is true, then we interrupted another
123 * handler setting up the stack. In this case, we
124 * have to return, and the upper handler will deal
125 * with this interrupt.
126 */
Jeff Dike508a9272007-09-18 22:46:49 -0700127 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700128 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700129 return;
130
131 nested = pending & 1;
132 pending &= ~1;
133
Jeff Dikeba180fd2007-10-16 01:27:00 -0700134 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700135 sig--;
136 pending &= ~(1 << sig);
137 (*handlers[sig])(sig, sc);
138 }
139
Jeff Dikeba180fd2007-10-16 01:27:00 -0700140 /*
141 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700142 * that arrived while tearing down the stack. If this
143 * is non-zero, we just go back, set up the stack
144 * again, and handle the new interrupts.
145 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700146 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700147 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700148 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700149}
150
Jeff Dike4b84c692006-09-25 23:33:04 -0700151extern void hard_handler(int sig);
152
Gennady Sharapov0805d892006-01-08 01:01:29 -0800153void set_handler(int sig, void (*handler)(int), int flags, ...)
154{
155 struct sigaction action;
156 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800157 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800158 int mask;
159
Jeff Dike4b84c692006-09-25 23:33:04 -0700160 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
161 action.sa_handler = hard_handler;
162
Gennady Sharapov0805d892006-01-08 01:01:29 -0800163 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700164
165 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700166 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800167 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800168 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700169
Gennady Sharapov0805d892006-01-08 01:01:29 -0800170 action.sa_flags = flags;
171 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700172 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800173 panic("sigaction failed - errno = %d\n", errno);
174
175 sigemptyset(&sig_mask);
176 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700177 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800178 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800179}
180
181int change_sig(int signal, int on)
182{
183 sigset_t sigset, old;
184
185 sigemptyset(&sigset);
186 sigaddset(&sigset, signal);
WANG Congc9a30722008-02-04 22:30:35 -0800187 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
188 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700189 return !sigismember(&old, signal);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800190}
191
Gennady Sharapov0805d892006-01-08 01:01:29 -0800192void block_signals(void)
193{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800194 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700195 /*
196 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800197 * ensures that writes are flushed out before the return.
198 * This might matter if gcc figures out how to inline this and
199 * decides to shuffle this code into the caller.
200 */
201 mb();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800202}
203
204void unblock_signals(void)
205{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800206 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800207
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800209 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800210
Jeff Dikeba180fd2007-10-16 01:27:00 -0700211 /*
212 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800213 * interrupts may have arrived and we need to re-enable them and
214 * recheck pending.
215 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700216 while(1) {
217 /*
218 * Save and reset save_pending after enabling signals. This
Jeff Dike1d7173b2006-01-18 17:42:49 -0800219 * way, pending won't be changed while we're reading it.
220 */
221 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800222
Jeff Dikeba180fd2007-10-16 01:27:00 -0700223 /*
224 * Setting signals_enabled and reading pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800225 * happen in this order.
226 */
227 mb();
228
Jeff Dike1d7173b2006-01-18 17:42:49 -0800229 save_pending = pending;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700230 if (save_pending == 0) {
231 /*
232 * This must return with signals enabled, so
Jeff Dike53b17332006-11-02 22:07:22 -0800233 * this barrier ensures that writes are
234 * flushed out before the return. This might
235 * matter if gcc figures out how to inline
236 * this (unlikely, given its size) and decides
237 * to shuffle this code into the caller.
238 */
239 mb();
Jeff Dike1d7173b2006-01-18 17:42:49 -0800240 return;
Jeff Dike53b17332006-11-02 22:07:22 -0800241 }
Jeff Dike1d7173b2006-01-18 17:42:49 -0800242
243 pending = 0;
244
Jeff Dikeba180fd2007-10-16 01:27:00 -0700245 /*
246 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800247 * handlers expect them off when they are called. They will
248 * be enabled again above.
249 */
250
251 signals_enabled = 0;
252
Jeff Dikeba180fd2007-10-16 01:27:00 -0700253 /*
254 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800255 * schedule, leaving the pending SIGIO stranded until we come
256 * back here.
257 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700258 if (save_pending & SIGIO_MASK)
Jeff Dike6aa802c2007-10-16 01:26:56 -0700259 sig_handler_common_skas(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800260
Jeff Dikeba180fd2007-10-16 01:27:00 -0700261 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700262 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800263 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800264}
265
266int get_signals(void)
267{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800268 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800269}
270
271int set_signals(int enable)
272{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800273 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700274 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800275 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800276
Jeff Dike1d7173b2006-01-18 17:42:49 -0800277 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700278 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800279 unblock_signals();
280 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800281
Jeff Dike1d7173b2006-01-18 17:42:49 -0800282 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800283}