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 "kern_util.h"
14 #include "sysdep/barrier.h"
15 #include "sysdep/sigcontext.h"
19 * These are the asynchronous signals. SIGPROF is excluded because we want to
20 * be able to profile all of UML, not just the non-critical sections. If
21 * profiling is not thread-safe, then that is not my problem. We can disable
22 * profiling when SMP is enabled in that case.
25 #define SIGIO_MASK (1 << SIGIO_BIT)
27 #define SIGVTALRM_BIT 1
28 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
31 * These are used by both the signal handlers and
32 * block/unblock_signals. I don't want modifications cached in a
33 * register - they must go straight to memory.
35 static volatile int signals_enabled = 1;
36 static volatile int pending = 0;
38 void sig_handler(int sig, struct sigcontext *sc)
42 enabled = signals_enabled;
43 if (!enabled && (sig == SIGIO)) {
44 pending |= SIGIO_MASK;
50 sig_handler_common_skas(sig, sc);
55 static void real_alarm_handler(struct sigcontext *sc)
57 struct uml_pt_regs regs;
63 timer_handler(SIGVTALRM, ®s);
66 void alarm_handler(int sig, struct sigcontext *sc)
70 enabled = signals_enabled;
71 if (!signals_enabled) {
72 pending |= SIGVTALRM_MASK;
78 real_alarm_handler(sc);
84 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
85 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
88 void set_sigstack(void *sig_stack, int size)
90 stack_t stack = ((stack_t) { .ss_flags = 0,
91 .ss_sp = (__ptr_t) sig_stack,
92 .ss_size = size - sizeof(void *) });
94 if (sigaltstack(&stack, NULL) != 0)
95 panic("enabling signal stack failed, errno = %d\n", errno);
98 void remove_sigstack(void)
100 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
104 if (sigaltstack(&stack, NULL) != 0)
105 panic("disabling signal stack failed, errno = %d\n", errno);
108 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
110 void handle_signal(int sig, struct sigcontext *sc)
112 unsigned long pending = 1UL << sig;
118 * pending comes back with one bit set for each
119 * interrupt that arrived while setting up the stack,
120 * plus a bit for this interrupt, plus the zero bit is
121 * set if this is a nested interrupt.
122 * If bail is true, then we interrupted another
123 * handler setting up the stack. In this case, we
124 * have to return, and the upper handler will deal
125 * with this interrupt.
127 bail = to_irq_stack(&pending);
131 nested = pending & 1;
134 while ((sig = ffs(pending)) != 0){
136 pending &= ~(1 << sig);
137 (*handlers[sig])(sig, sc);
141 * Again, pending comes back with a mask of signals
142 * that arrived while tearing down the stack. If this
143 * is non-zero, we just go back, set up the stack
144 * again, and handle the new interrupts.
147 pending = from_irq_stack(nested);
151 extern void hard_handler(int sig);
153 void set_handler(int sig, void (*handler)(int), int flags, ...)
155 struct sigaction action;
160 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
161 action.sa_handler = hard_handler;
163 sigemptyset(&action.sa_mask);
166 while ((mask = va_arg(ap, int)) != -1)
167 sigaddset(&action.sa_mask, mask);
170 action.sa_flags = flags;
171 action.sa_restorer = NULL;
172 if (sigaction(sig, &action, NULL) < 0)
173 panic("sigaction failed - errno = %d\n", errno);
175 sigemptyset(&sig_mask);
176 sigaddset(&sig_mask, sig);
177 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
178 panic("sigprocmask failed - errno = %d\n", errno);
181 int change_sig(int signal, int on)
183 sigset_t sigset, old;
185 sigemptyset(&sigset);
186 sigaddset(&sigset, signal);
187 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
189 return !sigismember(&old, signal);
192 void block_signals(void)
196 * This must return with signals disabled, so this barrier
197 * ensures that writes are flushed out before the return.
198 * This might matter if gcc figures out how to inline this and
199 * decides to shuffle this code into the caller.
204 void unblock_signals(void)
208 if (signals_enabled == 1)
212 * We loop because the IRQ handler returns with interrupts off. So,
213 * interrupts may have arrived and we need to re-enable them and
218 * Save and reset save_pending after enabling signals. This
219 * way, pending won't be changed while we're reading it.
224 * Setting signals_enabled and reading pending must
225 * happen in this order.
229 save_pending = pending;
230 if (save_pending == 0) {
232 * This must return with signals enabled, so
233 * this barrier ensures that writes are
234 * flushed out before the return. This might
235 * matter if gcc figures out how to inline
236 * this (unlikely, given its size) and decides
237 * to shuffle this code into the caller.
246 * We have pending interrupts, so disable signals, as the
247 * handlers expect them off when they are called. They will
248 * be enabled again above.
254 * Deal with SIGIO first because the alarm handler might
255 * schedule, leaving the pending SIGIO stranded until we come
258 if (save_pending & SIGIO_MASK)
259 sig_handler_common_skas(SIGIO, NULL);
261 if (save_pending & SIGVTALRM_MASK)
262 real_alarm_handler(NULL);
266 int get_signals(void)
268 return signals_enabled;
271 int set_signals(int enable)
274 if (signals_enabled == enable)
277 ret = signals_enabled;
280 else block_signals();