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