Merge branch 'for-2.6.30' into for-2.6.31
[linux-2.6-block.git] / arch / um / os-Linux / signal.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2004 PathScale, Inc
ba180fd4 3 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4
LT
4 * Licensed under the GPL
5 */
6
0805d89c 7#include <stdlib.h>
0805d89c 8#include <stdarg.h>
ba180fd4
JD
9#include <errno.h>
10#include <signal.h>
11#include <strings.h>
75ada8ff 12#include "as-layout.h"
edea1385 13#include "kern_util.h"
cff65c4f 14#include "os.h"
fe2cc53e 15#include "process.h"
ba180fd4
JD
16#include "sysdep/barrier.h"
17#include "sysdep/sigcontext.h"
18#include "user.h"
1da177e4 19
e6a2d1f7
JD
20/* Copied from linux/compiler-gcc.h since we can't include it directly */
21#define barrier() __asm__ __volatile__("": : :"memory")
22
75ada8ff
JD
23void (*sig_info[NSIG])(int, struct uml_pt_regs *) = {
24 [SIGTRAP] = relay_signal,
25 [SIGFPE] = relay_signal,
26 [SIGILL] = relay_signal,
27 [SIGWINCH] = winch,
28 [SIGBUS] = bus_handler,
29 [SIGSEGV] = segv_handler,
30 [SIGIO] = sigio_handler,
31 [SIGVTALRM] = timer_handler };
32
e6a2d1f7 33static void sig_handler_common(int sig, struct sigcontext *sc)
75ada8ff 34{
e6a2d1f7
JD
35 struct uml_pt_regs r;
36 int save_errno = errno;
75ada8ff 37
e6a2d1f7 38 r.is_user = 0;
75ada8ff 39 if (sig == SIGSEGV) {
e6a2d1f7
JD
40 /* For segfaults, we want the data from the sigcontext. */
41 copy_sc(&r, sc);
42 GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
43 }
75ada8ff 44
e6a2d1f7 45 /* enable signals if sig isn't IRQ signal */
75ada8ff
JD
46 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
47 unblock_signals();
48
e6a2d1f7 49 (*sig_info[sig])(sig, &r);
75ada8ff
JD
50
51 errno = save_errno;
75ada8ff
JD
52}
53
ba180fd4 54/*
61b63c55 55 * These are the asynchronous signals. SIGPROF is excluded because we want to
1d7173ba
JD
56 * be able to profile all of UML, not just the non-critical sections. If
57 * profiling is not thread-safe, then that is not my problem. We can disable
58 * profiling when SMP is enabled in that case.
59 */
60#define SIGIO_BIT 0
61#define SIGIO_MASK (1 << SIGIO_BIT)
62
63#define SIGVTALRM_BIT 1
64#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
65
fce8c41c 66static int signals_enabled;
cfef8f34 67static unsigned int signals_pending;
1d7173ba 68
4b84c69b 69void sig_handler(int sig, struct sigcontext *sc)
1da177e4 70{
1d7173ba
JD
71 int enabled;
72
1d7173ba 73 enabled = signals_enabled;
ba180fd4 74 if (!enabled && (sig == SIGIO)) {
cfef8f34 75 signals_pending |= SIGIO_MASK;
1d7173ba
JD
76 return;
77 }
78
79 block_signals();
80
e6a2d1f7 81 sig_handler_common(sig, sc);
1d7173ba
JD
82
83 set_signals(enabled);
1da177e4
LT
84}
85
61b63c55 86static void real_alarm_handler(struct sigcontext *sc)
1da177e4 87{
77bf4400 88 struct uml_pt_regs regs;
2ea5bc5e 89
ba180fd4 90 if (sc != NULL)
2ea5bc5e 91 copy_sc(&regs, sc);
77bf4400 92 regs.is_user = 0;
2ea5bc5e 93 unblock_signals();
61b63c55 94 timer_handler(SIGVTALRM, &regs);
1d7173ba
JD
95}
96
4b84c69b 97void alarm_handler(int sig, struct sigcontext *sc)
1d7173ba 98{
1d7173ba
JD
99 int enabled;
100
1d7173ba 101 enabled = signals_enabled;
ba180fd4 102 if (!signals_enabled) {
cfef8f34 103 signals_pending |= SIGVTALRM_MASK;
1d7173ba
JD
104 return;
105 }
106
107 block_signals();
108
61b63c55 109 real_alarm_handler(sc);
1d7173ba 110 set_signals(enabled);
1da177e4
LT
111}
112
78a26e25
JD
113void timer_init(void)
114{
115 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
61b63c55 116 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
78a26e25
JD
117}
118
0805d89c
GS
119void set_sigstack(void *sig_stack, int size)
120{
121 stack_t stack = ((stack_t) { .ss_flags = 0,
122 .ss_sp = (__ptr_t) sig_stack,
123 .ss_size = size - sizeof(void *) });
124
ba180fd4 125 if (sigaltstack(&stack, NULL) != 0)
0805d89c
GS
126 panic("enabling signal stack failed, errno = %d\n", errno);
127}
128
99764fa4 129static void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
4b84c69b 130
c14b8494
JD
131void handle_signal(int sig, struct sigcontext *sc)
132{
508a9274 133 unsigned long pending = 1UL << sig;
c14b8494
JD
134
135 do {
136 int nested, bail;
137
138 /*
139 * pending comes back with one bit set for each
140 * interrupt that arrived while setting up the stack,
141 * plus a bit for this interrupt, plus the zero bit is
142 * set if this is a nested interrupt.
143 * If bail is true, then we interrupted another
144 * handler setting up the stack. In this case, we
145 * have to return, and the upper handler will deal
146 * with this interrupt.
147 */
508a9274 148 bail = to_irq_stack(&pending);
ba180fd4 149 if (bail)
c14b8494
JD
150 return;
151
152 nested = pending & 1;
153 pending &= ~1;
154
ba180fd4 155 while ((sig = ffs(pending)) != 0){
c14b8494
JD
156 sig--;
157 pending &= ~(1 << sig);
158 (*handlers[sig])(sig, sc);
159 }
160
ba180fd4
JD
161 /*
162 * Again, pending comes back with a mask of signals
c14b8494
JD
163 * that arrived while tearing down the stack. If this
164 * is non-zero, we just go back, set up the stack
165 * again, and handle the new interrupts.
166 */
ba180fd4 167 if (!nested)
c14b8494 168 pending = from_irq_stack(nested);
ba180fd4 169 } while (pending);
c14b8494
JD
170}
171
4b84c69b
JD
172extern void hard_handler(int sig);
173
0805d89c
GS
174void set_handler(int sig, void (*handler)(int), int flags, ...)
175{
176 struct sigaction action;
177 va_list ap;
1d7173ba 178 sigset_t sig_mask;
0805d89c
GS
179 int mask;
180
4b84c69b
JD
181 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
182 action.sa_handler = hard_handler;
183
0805d89c 184 sigemptyset(&action.sa_mask);
4b84c69b
JD
185
186 va_start(ap, flags);
ba180fd4 187 while ((mask = va_arg(ap, int)) != -1)
0805d89c 188 sigaddset(&action.sa_mask, mask);
0805d89c 189 va_end(ap);
4b84c69b 190
e6a2d1f7
JD
191 if (sig == SIGSEGV)
192 flags |= SA_NODEFER;
193
0805d89c
GS
194 action.sa_flags = flags;
195 action.sa_restorer = NULL;
ba180fd4 196 if (sigaction(sig, &action, NULL) < 0)
1d7173ba
JD
197 panic("sigaction failed - errno = %d\n", errno);
198
199 sigemptyset(&sig_mask);
200 sigaddset(&sig_mask, sig);
ba180fd4 201 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
1d7173ba 202 panic("sigprocmask failed - errno = %d\n", errno);
0805d89c
GS
203}
204
205int change_sig(int signal, int on)
206{
cfef8f34 207 sigset_t sigset;
0805d89c
GS
208
209 sigemptyset(&sigset);
210 sigaddset(&sigset, signal);
cfef8f34 211 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
c9a3072d 212 return -errno;
cfef8f34
JD
213
214 return 0;
0805d89c
GS
215}
216
0805d89c
GS
217void block_signals(void)
218{
1d7173ba 219 signals_enabled = 0;
ba180fd4
JD
220 /*
221 * This must return with signals disabled, so this barrier
53b17332
JD
222 * ensures that writes are flushed out before the return.
223 * This might matter if gcc figures out how to inline this and
224 * decides to shuffle this code into the caller.
225 */
fce8c41c 226 barrier();
0805d89c
GS
227}
228
229void unblock_signals(void)
230{
1d7173ba 231 int save_pending;
0805d89c 232
ba180fd4 233 if (signals_enabled == 1)
1d7173ba 234 return;
0805d89c 235
ba180fd4
JD
236 /*
237 * We loop because the IRQ handler returns with interrupts off. So,
1d7173ba 238 * interrupts may have arrived and we need to re-enable them and
cfef8f34 239 * recheck signals_pending.
1d7173ba 240 */
5134d8fe 241 while (1) {
ba180fd4
JD
242 /*
243 * Save and reset save_pending after enabling signals. This
cfef8f34 244 * way, signals_pending won't be changed while we're reading it.
1d7173ba
JD
245 */
246 signals_enabled = 1;
247
ba180fd4 248 /*
cfef8f34 249 * Setting signals_enabled and reading signals_pending must
53b17332
JD
250 * happen in this order.
251 */
fce8c41c 252 barrier();
53b17332 253
cfef8f34 254 save_pending = signals_pending;
fce8c41c 255 if (save_pending == 0)
1d7173ba
JD
256 return;
257
cfef8f34 258 signals_pending = 0;
1d7173ba 259
ba180fd4
JD
260 /*
261 * We have pending interrupts, so disable signals, as the
1d7173ba
JD
262 * handlers expect them off when they are called. They will
263 * be enabled again above.
264 */
265
266 signals_enabled = 0;
267
ba180fd4
JD
268 /*
269 * Deal with SIGIO first because the alarm handler might
1d7173ba
JD
270 * schedule, leaving the pending SIGIO stranded until we come
271 * back here.
272 */
ba180fd4 273 if (save_pending & SIGIO_MASK)
e6a2d1f7 274 sig_handler_common(SIGIO, NULL);
1d7173ba 275
ba180fd4 276 if (save_pending & SIGVTALRM_MASK)
61b63c55 277 real_alarm_handler(NULL);
1d7173ba 278 }
0805d89c
GS
279}
280
281int get_signals(void)
282{
1d7173ba 283 return signals_enabled;
0805d89c
GS
284}
285
286int set_signals(int enable)
287{
0805d89c 288 int ret;
ba180fd4 289 if (signals_enabled == enable)
1d7173ba 290 return enable;
0805d89c 291
1d7173ba 292 ret = signals_enabled;
ba180fd4 293 if (enable)
1d7173ba
JD
294 unblock_signals();
295 else block_signals();
0805d89c 296
1d7173ba 297 return ret;
0805d89c 298}