Kprobes: move kprobe examples to samples/
[linux-2.6/linux-2.6-openrd.git] / arch / um / os-Linux / signal.c
blob3f1694b134cb2606efa628deb4291c5813a549c0
1 /*
2 * Copyright (C) 2004 PathScale, Inc
3 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
5 */
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <signal.h>
11 #include <strings.h>
12 #include "as-layout.h"
13 #include "kern_util.h"
14 #include "os.h"
15 #include "sysdep/barrier.h"
16 #include "sysdep/sigcontext.h"
17 #include "user.h"
19 /* Copied from linux/compiler-gcc.h since we can't include it directly */
20 #define barrier() __asm__ __volatile__("": : :"memory")
22 void (*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 };
32 static void sig_handler_common(int sig, struct sigcontext *sc)
34 struct uml_pt_regs r;
35 int save_errno = errno;
37 r.is_user = 0;
38 if (sig == SIGSEGV) {
39 /* For segfaults, we want the data from the sigcontext. */
40 copy_sc(&r, sc);
41 GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
44 /* enable signals if sig isn't IRQ signal */
45 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
46 unblock_signals();
48 (*sig_info[sig])(sig, &r);
50 errno = save_errno;
54 * These are the asynchronous signals. SIGPROF is excluded because we want to
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.
59 #define SIGIO_BIT 0
60 #define SIGIO_MASK (1 << SIGIO_BIT)
62 #define SIGVTALRM_BIT 1
63 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
65 static int signals_enabled;
66 static unsigned int signals_pending;
68 void sig_handler(int sig, struct sigcontext *sc)
70 int enabled;
72 enabled = signals_enabled;
73 if (!enabled && (sig == SIGIO)) {
74 signals_pending |= SIGIO_MASK;
75 return;
78 block_signals();
80 sig_handler_common(sig, sc);
82 set_signals(enabled);
85 static void real_alarm_handler(struct sigcontext *sc)
87 struct uml_pt_regs regs;
89 if (sc != NULL)
90 copy_sc(&regs, sc);
91 regs.is_user = 0;
92 unblock_signals();
93 timer_handler(SIGVTALRM, &regs);
96 void alarm_handler(int sig, struct sigcontext *sc)
98 int enabled;
100 enabled = signals_enabled;
101 if (!signals_enabled) {
102 signals_pending |= SIGVTALRM_MASK;
103 return;
106 block_signals();
108 real_alarm_handler(sc);
109 set_signals(enabled);
112 void timer_init(void)
114 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
115 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
118 void set_sigstack(void *sig_stack, int size)
120 stack_t stack = ((stack_t) { .ss_flags = 0,
121 .ss_sp = (__ptr_t) sig_stack,
122 .ss_size = size - sizeof(void *) });
124 if (sigaltstack(&stack, NULL) != 0)
125 panic("enabling signal stack failed, errno = %d\n", errno);
128 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
130 void handle_signal(int sig, struct sigcontext *sc)
132 unsigned long pending = 1UL << sig;
134 do {
135 int nested, bail;
138 * pending comes back with one bit set for each
139 * interrupt that arrived while setting up the stack,
140 * plus a bit for this interrupt, plus the zero bit is
141 * set if this is a nested interrupt.
142 * If bail is true, then we interrupted another
143 * handler setting up the stack. In this case, we
144 * have to return, and the upper handler will deal
145 * with this interrupt.
147 bail = to_irq_stack(&pending);
148 if (bail)
149 return;
151 nested = pending & 1;
152 pending &= ~1;
154 while ((sig = ffs(pending)) != 0){
155 sig--;
156 pending &= ~(1 << sig);
157 (*handlers[sig])(sig, sc);
161 * Again, pending comes back with a mask of signals
162 * that arrived while tearing down the stack. If this
163 * is non-zero, we just go back, set up the stack
164 * again, and handle the new interrupts.
166 if (!nested)
167 pending = from_irq_stack(nested);
168 } while (pending);
171 extern void hard_handler(int sig);
173 void set_handler(int sig, void (*handler)(int), int flags, ...)
175 struct sigaction action;
176 va_list ap;
177 sigset_t sig_mask;
178 int mask;
180 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
181 action.sa_handler = hard_handler;
183 sigemptyset(&action.sa_mask);
185 va_start(ap, flags);
186 while ((mask = va_arg(ap, int)) != -1)
187 sigaddset(&action.sa_mask, mask);
188 va_end(ap);
190 if (sig == SIGSEGV)
191 flags |= SA_NODEFER;
193 action.sa_flags = flags;
194 action.sa_restorer = NULL;
195 if (sigaction(sig, &action, NULL) < 0)
196 panic("sigaction failed - errno = %d\n", errno);
198 sigemptyset(&sig_mask);
199 sigaddset(&sig_mask, sig);
200 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
201 panic("sigprocmask failed - errno = %d\n", errno);
204 int change_sig(int signal, int on)
206 sigset_t sigset;
208 sigemptyset(&sigset);
209 sigaddset(&sigset, signal);
210 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
211 return -errno;
213 return 0;
216 void block_signals(void)
218 signals_enabled = 0;
220 * This must return with signals disabled, so this barrier
221 * ensures that writes are flushed out before the return.
222 * This might matter if gcc figures out how to inline this and
223 * decides to shuffle this code into the caller.
225 barrier();
228 void unblock_signals(void)
230 int save_pending;
232 if (signals_enabled == 1)
233 return;
236 * We loop because the IRQ handler returns with interrupts off. So,
237 * interrupts may have arrived and we need to re-enable them and
238 * recheck signals_pending.
240 while (1) {
242 * Save and reset save_pending after enabling signals. This
243 * way, signals_pending won't be changed while we're reading it.
245 signals_enabled = 1;
248 * Setting signals_enabled and reading signals_pending must
249 * happen in this order.
251 barrier();
253 save_pending = signals_pending;
254 if (save_pending == 0)
255 return;
257 signals_pending = 0;
260 * We have pending interrupts, so disable signals, as the
261 * handlers expect them off when they are called. They will
262 * be enabled again above.
265 signals_enabled = 0;
268 * Deal with SIGIO first because the alarm handler might
269 * schedule, leaving the pending SIGIO stranded until we come
270 * back here.
272 if (save_pending & SIGIO_MASK)
273 sig_handler_common(SIGIO, NULL);
275 if (save_pending & SIGVTALRM_MASK)
276 real_alarm_handler(NULL);
280 int get_signals(void)
282 return signals_enabled;
285 int set_signals(int enable)
287 int ret;
288 if (signals_enabled == enable)
289 return enable;
291 ret = signals_enabled;
292 if (enable)
293 unblock_signals();
294 else block_signals();
296 return ret;