blob: e9800b0b5689b6abc750606ab886826709964e56 [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/*
Jeff Dike61b63c52007-10-16 01:27:27 -070018 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080019 * be able to profile all of UML, not just the non-critical sections. If
20 * profiling is not thread-safe, then that is not my problem. We can disable
21 * profiling when SMP is enabled in that case.
22 */
23#define SIGIO_BIT 0
24#define SIGIO_MASK (1 << SIGIO_BIT)
25
26#define SIGVTALRM_BIT 1
27#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
28
Jeff Dikeba180fd2007-10-16 01:27:00 -070029/*
30 * These are used by both the signal handlers and
Jeff Dike53b17332006-11-02 22:07:22 -080031 * block/unblock_signals. I don't want modifications cached in a
32 * register - they must go straight to memory.
33 */
34static volatile int signals_enabled = 1;
35static volatile int pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -080036
Jeff Dike4b84c692006-09-25 23:33:04 -070037void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Jeff Dike1d7173b2006-01-18 17:42:49 -080039 int enabled;
40
Jeff Dike1d7173b2006-01-18 17:42:49 -080041 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070042 if (!enabled && (sig == SIGIO)) {
Jeff Dike1d7173b2006-01-18 17:42:49 -080043 pending |= SIGIO_MASK;
44 return;
45 }
46
47 block_signals();
48
Jeff Dike6aa802c2007-10-16 01:26:56 -070049 sig_handler_common_skas(sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080050
51 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Jeff Dike61b63c52007-10-16 01:27:27 -070054static void real_alarm_handler(struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Jeff Dike77bf4402007-10-16 01:26:58 -070056 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070057
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -070059 copy_sc(&regs, sc);
Jeff Dike77bf4402007-10-16 01:26:58 -070060 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070061 unblock_signals();
Jeff Dike61b63c52007-10-16 01:27:27 -070062 timer_handler(SIGVTALRM, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080063}
64
Jeff Dike4b84c692006-09-25 23:33:04 -070065void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080066{
Jeff Dike1d7173b2006-01-18 17:42:49 -080067 int enabled;
68
Jeff Dike1d7173b2006-01-18 17:42:49 -080069 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070070 if (!signals_enabled) {
Jeff Dike61b63c52007-10-16 01:27:27 -070071 pending |= SIGVTALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080072 return;
73 }
74
75 block_signals();
76
Jeff Dike61b63c52007-10-16 01:27:27 -070077 real_alarm_handler(sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080078 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Jeff Dike78a26e22007-10-16 01:27:23 -070081void timer_init(void)
82{
83 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
Jeff Dike61b63c52007-10-16 01:27:27 -070084 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
Jeff Dike78a26e22007-10-16 01:27:23 -070085}
86
Gennady Sharapov0805d892006-01-08 01:01:29 -080087void set_sigstack(void *sig_stack, int size)
88{
89 stack_t stack = ((stack_t) { .ss_flags = 0,
90 .ss_sp = (__ptr_t) sig_stack,
91 .ss_size = size - sizeof(void *) });
92
Jeff Dikeba180fd2007-10-16 01:27:00 -070093 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -080094 panic("enabling signal stack failed, errno = %d\n", errno);
95}
96
97void remove_sigstack(void)
98{
99 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
100 .ss_sp = NULL,
101 .ss_size = 0 });
102
Jeff Dikeba180fd2007-10-16 01:27:00 -0700103 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800104 panic("disabling signal stack failed, errno = %d\n", errno);
105}
106
Jeff Dike4b84c692006-09-25 23:33:04 -0700107void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
108
Jeff Dikec14b8492007-05-10 22:22:34 -0700109void handle_signal(int sig, struct sigcontext *sc)
110{
Jeff Dike508a9272007-09-18 22:46:49 -0700111 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700112
113 do {
114 int nested, bail;
115
116 /*
117 * pending comes back with one bit set for each
118 * interrupt that arrived while setting up the stack,
119 * plus a bit for this interrupt, plus the zero bit is
120 * set if this is a nested interrupt.
121 * If bail is true, then we interrupted another
122 * handler setting up the stack. In this case, we
123 * have to return, and the upper handler will deal
124 * with this interrupt.
125 */
Jeff Dike508a9272007-09-18 22:46:49 -0700126 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700127 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700128 return;
129
130 nested = pending & 1;
131 pending &= ~1;
132
Jeff Dikeba180fd2007-10-16 01:27:00 -0700133 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700134 sig--;
135 pending &= ~(1 << sig);
136 (*handlers[sig])(sig, sc);
137 }
138
Jeff Dikeba180fd2007-10-16 01:27:00 -0700139 /*
140 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700141 * that arrived while tearing down the stack. If this
142 * is non-zero, we just go back, set up the stack
143 * again, and handle the new interrupts.
144 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700145 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700146 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700147 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700148}
149
Jeff Dike4b84c692006-09-25 23:33:04 -0700150extern void hard_handler(int sig);
151
Gennady Sharapov0805d892006-01-08 01:01:29 -0800152void set_handler(int sig, void (*handler)(int), int flags, ...)
153{
154 struct sigaction action;
155 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800156 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800157 int mask;
158
Jeff Dike4b84c692006-09-25 23:33:04 -0700159 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
160 action.sa_handler = hard_handler;
161
Gennady Sharapov0805d892006-01-08 01:01:29 -0800162 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700163
164 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700165 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800166 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800167 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700168
Gennady Sharapov0805d892006-01-08 01:01:29 -0800169 action.sa_flags = flags;
170 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700171 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800172 panic("sigaction failed - errno = %d\n", errno);
173
174 sigemptyset(&sig_mask);
175 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700176 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800177 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800178}
179
180int change_sig(int signal, int on)
181{
182 sigset_t sigset, old;
183
184 sigemptyset(&sigset);
185 sigaddset(&sigset, signal);
186 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700187 return !sigismember(&old, signal);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800188}
189
Gennady Sharapov0805d892006-01-08 01:01:29 -0800190void block_signals(void)
191{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800192 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700193 /*
194 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800195 * ensures that writes are flushed out before the return.
196 * This might matter if gcc figures out how to inline this and
197 * decides to shuffle this code into the caller.
198 */
199 mb();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800200}
201
202void unblock_signals(void)
203{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800204 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800205
Jeff Dikeba180fd2007-10-16 01:27:00 -0700206 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800207 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800208
Jeff Dikeba180fd2007-10-16 01:27:00 -0700209 /*
210 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800211 * interrupts may have arrived and we need to re-enable them and
212 * recheck pending.
213 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700214 while(1) {
215 /*
216 * Save and reset save_pending after enabling signals. This
Jeff Dike1d7173b2006-01-18 17:42:49 -0800217 * way, pending won't be changed while we're reading it.
218 */
219 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800220
Jeff Dikeba180fd2007-10-16 01:27:00 -0700221 /*
222 * Setting signals_enabled and reading pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800223 * happen in this order.
224 */
225 mb();
226
Jeff Dike1d7173b2006-01-18 17:42:49 -0800227 save_pending = pending;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700228 if (save_pending == 0) {
229 /*
230 * This must return with signals enabled, so
Jeff Dike53b17332006-11-02 22:07:22 -0800231 * this barrier ensures that writes are
232 * flushed out before the return. This might
233 * matter if gcc figures out how to inline
234 * this (unlikely, given its size) and decides
235 * to shuffle this code into the caller.
236 */
237 mb();
Jeff Dike1d7173b2006-01-18 17:42:49 -0800238 return;
Jeff Dike53b17332006-11-02 22:07:22 -0800239 }
Jeff Dike1d7173b2006-01-18 17:42:49 -0800240
241 pending = 0;
242
Jeff Dikeba180fd2007-10-16 01:27:00 -0700243 /*
244 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800245 * handlers expect them off when they are called. They will
246 * be enabled again above.
247 */
248
249 signals_enabled = 0;
250
Jeff Dikeba180fd2007-10-16 01:27:00 -0700251 /*
252 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800253 * schedule, leaving the pending SIGIO stranded until we come
254 * back here.
255 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700256 if (save_pending & SIGIO_MASK)
Jeff Dike6aa802c2007-10-16 01:26:56 -0700257 sig_handler_common_skas(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800258
Jeff Dikeba180fd2007-10-16 01:27:00 -0700259 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700260 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800261 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800262}
263
264int get_signals(void)
265{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800266 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800267}
268
269int set_signals(int enable)
270{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800271 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700272 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800273 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800274
Jeff Dike1d7173b2006-01-18 17:42:49 -0800275 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700276 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800277 unblock_signals();
278 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800279
Jeff Dike1d7173b2006-01-18 17:42:49 -0800280 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800281}