2 * Copyright (C) 2004 PathScale, Inc
3 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
13 #include "sysdep/barrier.h"
14 #include "sysdep/sigcontext.h"
18 * These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
19 * together under SIGVTALRM_BIT. 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 #define SIGALRM_MASK (1 << SIGALRM_BIT)
34 * These are used by both the signal handlers and
35 * block/unblock_signals. I don't want modifications cached in a
36 * register - they must go straight to memory.
38 static volatile int signals_enabled = 1;
39 static volatile int pending = 0;
41 void sig_handler(int sig, struct sigcontext *sc)
45 enabled = signals_enabled;
46 if (!enabled && (sig == SIGIO)) {
47 pending |= SIGIO_MASK;
53 sig_handler_common_skas(sig, sc);
58 static void real_alarm_handler(int sig, struct sigcontext *sc)
60 struct uml_pt_regs regs;
66 timer_handler(sig, ®s);
69 void alarm_handler(int sig, struct sigcontext *sc)
73 enabled = signals_enabled;
74 if (!signals_enabled) {
76 pending |= SIGVTALRM_MASK;
77 else pending |= SIGALRM_MASK;
84 real_alarm_handler(sig, sc);
90 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
91 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
93 set_handler(SIGALRM, (__sighandler_t) alarm_handler,
94 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
98 void set_sigstack(void *sig_stack, int size)
100 stack_t stack = ((stack_t) { .ss_flags = 0,
101 .ss_sp = (__ptr_t) sig_stack,
102 .ss_size = size - sizeof(void *) });
104 if (sigaltstack(&stack, NULL) != 0)
105 panic("enabling signal stack failed, errno = %d\n", errno);
108 void remove_sigstack(void)
110 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
114 if (sigaltstack(&stack, NULL) != 0)
115 panic("disabling signal stack failed, errno = %d\n", errno);
118 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
120 void handle_signal(int sig, struct sigcontext *sc)
122 unsigned long pending = 1UL << sig;
123 int timer = switch_timers(0);
129 * pending comes back with one bit set for each
130 * interrupt that arrived while setting up the stack,
131 * plus a bit for this interrupt, plus the zero bit is
132 * set if this is a nested interrupt.
133 * If bail is true, then we interrupted another
134 * handler setting up the stack. In this case, we
135 * have to return, and the upper handler will deal
136 * with this interrupt.
138 bail = to_irq_stack(&pending);
142 nested = pending & 1;
145 while ((sig = ffs(pending)) != 0){
147 pending &= ~(1 << sig);
148 (*handlers[sig])(sig, sc);
152 * Again, pending comes back with a mask of signals
153 * that arrived while tearing down the stack. If this
154 * is non-zero, we just go back, set up the stack
155 * again, and handle the new interrupts.
158 pending = from_irq_stack(nested);
161 switch_timers(timer);
164 extern void hard_handler(int sig);
166 void set_handler(int sig, void (*handler)(int), int flags, ...)
168 struct sigaction action;
173 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
174 action.sa_handler = hard_handler;
176 sigemptyset(&action.sa_mask);
179 while ((mask = va_arg(ap, int)) != -1)
180 sigaddset(&action.sa_mask, mask);
183 action.sa_flags = flags;
184 action.sa_restorer = NULL;
185 if (sigaction(sig, &action, NULL) < 0)
186 panic("sigaction failed - errno = %d\n", errno);
188 sigemptyset(&sig_mask);
189 sigaddset(&sig_mask, sig);
190 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
191 panic("sigprocmask failed - errno = %d\n", errno);
194 int change_sig(int signal, int on)
196 sigset_t sigset, old;
198 sigemptyset(&sigset);
199 sigaddset(&sigset, signal);
200 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
201 return !sigismember(&old, signal);
204 void block_signals(void)
208 * This must return with signals disabled, so this barrier
209 * ensures that writes are flushed out before the return.
210 * This might matter if gcc figures out how to inline this and
211 * decides to shuffle this code into the caller.
216 void unblock_signals(void)
220 if (signals_enabled == 1)
224 * We loop because the IRQ handler returns with interrupts off. So,
225 * interrupts may have arrived and we need to re-enable them and
230 * Save and reset save_pending after enabling signals. This
231 * way, pending won't be changed while we're reading it.
236 * Setting signals_enabled and reading pending must
237 * happen in this order.
241 save_pending = pending;
242 if (save_pending == 0) {
244 * This must return with signals enabled, so
245 * this barrier ensures that writes are
246 * flushed out before the return. This might
247 * matter if gcc figures out how to inline
248 * this (unlikely, given its size) and decides
249 * to shuffle this code into the caller.
258 * We have pending interrupts, so disable signals, as the
259 * handlers expect them off when they are called. They will
260 * be enabled again above.
266 * Deal with SIGIO first because the alarm handler might
267 * schedule, leaving the pending SIGIO stranded until we come
270 if (save_pending & SIGIO_MASK)
271 sig_handler_common_skas(SIGIO, NULL);
273 if (save_pending & SIGALRM_MASK)
274 real_alarm_handler(SIGALRM, NULL);
276 if (save_pending & SIGVTALRM_MASK)
277 real_alarm_handler(SIGVTALRM, NULL);
281 int get_signals(void)
283 return signals_enabled;
286 int set_signals(int enable)
289 if (signals_enabled == enable)
292 ret = signals_enabled;
295 else block_signals();