2 * Copyright (C) 2004 PathScale, Inc
3 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
12 #include "as-layout.h"
13 #include "kern_util.h"
15 #include "sysdep/barrier.h"
16 #include "sysdep/sigcontext.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,
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)
35 int save_errno = errno;
39 /* For segfaults, we want the data from the sigcontext. */
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))
48 (*sig_info[sig])(sig, &r);
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.
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 pending;
68 void sig_handler(int sig, struct sigcontext *sc)
72 enabled = signals_enabled;
73 if (!enabled && (sig == SIGIO)) {
74 pending |= SIGIO_MASK;
80 sig_handler_common(sig, sc);
85 static void real_alarm_handler(struct sigcontext *sc)
87 struct uml_pt_regs regs;
93 timer_handler(SIGVTALRM, ®s);
96 void alarm_handler(int sig, struct sigcontext *sc)
100 enabled = signals_enabled;
101 if (!signals_enabled) {
102 pending |= SIGVTALRM_MASK;
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 remove_sigstack(void)
130 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
134 if (sigaltstack(&stack, NULL) != 0)
135 panic("disabling signal stack failed, errno = %d\n", errno);
138 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
140 void handle_signal(int sig, struct sigcontext *sc)
142 unsigned long pending = 1UL << sig;
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.
157 bail = to_irq_stack(&pending);
161 nested = pending & 1;
164 while ((sig = ffs(pending)) != 0){
166 pending &= ~(1 << sig);
167 (*handlers[sig])(sig, sc);
171 * Again, pending comes back with a mask of signals
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.
177 pending = from_irq_stack(nested);
181 extern void hard_handler(int sig);
183 void set_handler(int sig, void (*handler)(int), int flags, ...)
185 struct sigaction action;
190 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
191 action.sa_handler = hard_handler;
193 sigemptyset(&action.sa_mask);
196 while ((mask = va_arg(ap, int)) != -1)
197 sigaddset(&action.sa_mask, mask);
203 action.sa_flags = flags;
204 action.sa_restorer = NULL;
205 if (sigaction(sig, &action, NULL) < 0)
206 panic("sigaction failed - errno = %d\n", errno);
208 sigemptyset(&sig_mask);
209 sigaddset(&sig_mask, sig);
210 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
211 panic("sigprocmask failed - errno = %d\n", errno);
214 int change_sig(int signal, int on)
216 sigset_t sigset, old;
218 sigemptyset(&sigset);
219 sigaddset(&sigset, signal);
220 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
222 return !sigismember(&old, signal);
225 void block_signals(void)
229 * This must return with signals disabled, so this barrier
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.
237 void unblock_signals(void)
241 if (signals_enabled == 1)
245 * We loop because the IRQ handler returns with interrupts off. So,
246 * interrupts may have arrived and we need to re-enable them and
251 * Save and reset save_pending after enabling signals. This
252 * way, pending won't be changed while we're reading it.
257 * Setting signals_enabled and reading pending must
258 * happen in this order.
262 save_pending = pending;
263 if (save_pending == 0)
269 * We have pending interrupts, so disable signals, as the
270 * handlers expect them off when they are called. They will
271 * be enabled again above.
277 * Deal with SIGIO first because the alarm handler might
278 * schedule, leaving the pending SIGIO stranded until we come
281 if (save_pending & SIGIO_MASK)
282 sig_handler_common(SIGIO, NULL);
284 if (save_pending & SIGVTALRM_MASK)
285 real_alarm_handler(NULL);
289 int get_signals(void)
291 return signals_enabled;
294 int set_signals(int enable)
297 if (signals_enabled == enable)
300 ret = signals_enabled;
303 else block_signals();