2 * Copyright (C) 2004 PathScale, Inc
3 * Licensed under the GPL
15 #include "signal_kern.h"
16 #include "sysdep/sigcontext.h"
17 #include "sysdep/barrier.h"
18 #include "sigcontext.h"
21 /* These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
22 * together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
23 * be able to profile all of UML, not just the non-critical sections. If
24 * profiling is not thread-safe, then that is not my problem. We can disable
25 * profiling when SMP is enabled in that case.
28 #define SIGIO_MASK (1 << SIGIO_BIT)
30 #define SIGVTALRM_BIT 1
31 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
34 #define SIGALRM_MASK (1 << SIGALRM_BIT)
36 /* These are used by both the signal handlers and
37 * block/unblock_signals. I don't want modifications cached in a
38 * register - they must go straight to memory.
40 static volatile int signals_enabled = 1;
41 static volatile int pending = 0;
43 void sig_handler(int sig, struct sigcontext *sc)
47 enabled = signals_enabled;
48 if(!enabled && (sig == SIGIO)){
49 pending |= SIGIO_MASK;
55 sig_handler_common_skas(sig, sc);
60 static void real_alarm_handler(int sig, struct sigcontext *sc)
62 struct uml_pt_regs regs;
71 timer_handler(sig, ®s);
77 void alarm_handler(int sig, struct sigcontext *sc)
81 enabled = signals_enabled;
84 pending |= SIGVTALRM_MASK;
85 else pending |= SIGALRM_MASK;
92 real_alarm_handler(sig, sc);
96 void set_sigstack(void *sig_stack, int size)
98 stack_t stack = ((stack_t) { .ss_flags = 0,
99 .ss_sp = (__ptr_t) sig_stack,
100 .ss_size = size - sizeof(void *) });
102 if(sigaltstack(&stack, NULL) != 0)
103 panic("enabling signal stack failed, errno = %d\n", errno);
106 void remove_sigstack(void)
108 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
112 if(sigaltstack(&stack, NULL) != 0)
113 panic("disabling signal stack failed, errno = %d\n", errno);
116 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
118 void handle_signal(int sig, struct sigcontext *sc)
120 unsigned long pending = 1UL << sig;
126 * pending comes back with one bit set for each
127 * interrupt that arrived while setting up the stack,
128 * plus a bit for this interrupt, plus the zero bit is
129 * set if this is a nested interrupt.
130 * If bail is true, then we interrupted another
131 * handler setting up the stack. In this case, we
132 * have to return, and the upper handler will deal
133 * with this interrupt.
135 bail = to_irq_stack(&pending);
139 nested = pending & 1;
142 while((sig = ffs(pending)) != 0){
144 pending &= ~(1 << sig);
145 (*handlers[sig])(sig, sc);
148 /* Again, pending comes back with a mask of signals
149 * that arrived while tearing down the stack. If this
150 * is non-zero, we just go back, set up the stack
151 * again, and handle the new interrupts.
154 pending = from_irq_stack(nested);
158 extern void hard_handler(int sig);
160 void set_handler(int sig, void (*handler)(int), int flags, ...)
162 struct sigaction action;
167 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
168 action.sa_handler = hard_handler;
170 sigemptyset(&action.sa_mask);
173 while((mask = va_arg(ap, int)) != -1)
174 sigaddset(&action.sa_mask, mask);
177 action.sa_flags = flags;
178 action.sa_restorer = NULL;
179 if(sigaction(sig, &action, NULL) < 0)
180 panic("sigaction failed - errno = %d\n", errno);
182 sigemptyset(&sig_mask);
183 sigaddset(&sig_mask, sig);
184 if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
185 panic("sigprocmask failed - errno = %d\n", errno);
188 int change_sig(int signal, int on)
190 sigset_t sigset, old;
192 sigemptyset(&sigset);
193 sigaddset(&sigset, signal);
194 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
195 return(!sigismember(&old, signal));
198 void block_signals(void)
201 /* This must return with signals disabled, so this barrier
202 * ensures that writes are flushed out before the return.
203 * This might matter if gcc figures out how to inline this and
204 * decides to shuffle this code into the caller.
209 void unblock_signals(void)
213 if(signals_enabled == 1)
216 /* We loop because the IRQ handler returns with interrupts off. So,
217 * interrupts may have arrived and we need to re-enable them and
221 /* Save and reset save_pending after enabling signals. This
222 * way, pending won't be changed while we're reading it.
226 /* Setting signals_enabled and reading pending must
227 * happen in this order.
231 save_pending = pending;
232 if(save_pending == 0){
233 /* This must return with signals enabled, so
234 * this barrier ensures that writes are
235 * flushed out before the return. This might
236 * matter if gcc figures out how to inline
237 * this (unlikely, given its size) and decides
238 * 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.
253 /* Deal with SIGIO first because the alarm handler might
254 * schedule, leaving the pending SIGIO stranded until we come
257 if(save_pending & SIGIO_MASK)
258 sig_handler_common_skas(SIGIO, NULL);
260 if(save_pending & SIGALRM_MASK)
261 real_alarm_handler(SIGALRM, NULL);
263 if(save_pending & SIGVTALRM_MASK)
264 real_alarm_handler(SIGVTALRM, NULL);
268 int get_signals(void)
270 return signals_enabled;
273 int set_signals(int enable)
276 if(signals_enabled == enable)
279 ret = signals_enabled;
282 else block_signals();