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"
18 /* Copied from linux/compiler-gcc.h since we can't include it directly */
19 #define barrier() __asm__ __volatile__("": : :"memory")
22 * These are the asynchronous signals. 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)
33 static int signals_enabled;
34 static unsigned int pending;
36 void sig_handler(int sig, struct sigcontext *sc)
40 enabled = signals_enabled;
41 if (!enabled && (sig == SIGIO)) {
42 pending |= SIGIO_MASK;
48 sig_handler_common_skas(sig, sc);
53 static void real_alarm_handler(struct sigcontext *sc)
55 struct uml_pt_regs regs;
61 timer_handler(SIGVTALRM, ®s);
64 void alarm_handler(int sig, struct sigcontext *sc)
68 enabled = signals_enabled;
69 if (!signals_enabled) {
70 pending |= SIGVTALRM_MASK;
76 real_alarm_handler(sc);
82 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
83 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
86 void set_sigstack(void *sig_stack, int size)
88 stack_t stack = ((stack_t) { .ss_flags = 0,
89 .ss_sp = (__ptr_t) sig_stack,
90 .ss_size = size - sizeof(void *) });
92 if (sigaltstack(&stack, NULL) != 0)
93 panic("enabling signal stack failed, errno = %d\n", errno);
96 void remove_sigstack(void)
98 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
102 if (sigaltstack(&stack, NULL) != 0)
103 panic("disabling signal stack failed, errno = %d\n", errno);
106 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
108 void handle_signal(int sig, struct sigcontext *sc)
110 unsigned long pending = 1UL << sig;
116 * pending comes back with one bit set for each
117 * interrupt that arrived while setting up the stack,
118 * plus a bit for this interrupt, plus the zero bit is
119 * set if this is a nested interrupt.
120 * If bail is true, then we interrupted another
121 * handler setting up the stack. In this case, we
122 * have to return, and the upper handler will deal
123 * with this interrupt.
125 bail = to_irq_stack(&pending);
129 nested = pending & 1;
132 while ((sig = ffs(pending)) != 0){
134 pending &= ~(1 << sig);
135 (*handlers[sig])(sig, sc);
139 * Again, pending comes back with a mask of signals
140 * that arrived while tearing down the stack. If this
141 * is non-zero, we just go back, set up the stack
142 * again, and handle the new interrupts.
145 pending = from_irq_stack(nested);
149 extern void hard_handler(int sig);
151 void set_handler(int sig, void (*handler)(int), int flags, ...)
153 struct sigaction action;
158 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
159 action.sa_handler = hard_handler;
161 sigemptyset(&action.sa_mask);
164 while ((mask = va_arg(ap, int)) != -1)
165 sigaddset(&action.sa_mask, mask);
168 action.sa_flags = flags;
169 action.sa_restorer = NULL;
170 if (sigaction(sig, &action, NULL) < 0)
171 panic("sigaction failed - errno = %d\n", errno);
173 sigemptyset(&sig_mask);
174 sigaddset(&sig_mask, sig);
175 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
176 panic("sigprocmask failed - errno = %d\n", errno);
179 int change_sig(int signal, int on)
181 sigset_t sigset, old;
183 sigemptyset(&sigset);
184 sigaddset(&sigset, signal);
185 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
187 return !sigismember(&old, signal);
190 void block_signals(void)
194 * This must return with signals disabled, so this barrier
195 * ensures that writes are flushed out before the return.
196 * This might matter if gcc figures out how to inline this and
197 * decides to shuffle this code into the caller.
202 void unblock_signals(void)
206 if (signals_enabled == 1)
210 * We loop because the IRQ handler returns with interrupts off. So,
211 * interrupts may have arrived and we need to re-enable them and
216 * Save and reset save_pending after enabling signals. This
217 * way, pending won't be changed while we're reading it.
222 * Setting signals_enabled and reading pending must
223 * happen in this order.
227 save_pending = pending;
228 if (save_pending == 0)
234 * We have pending interrupts, so disable signals, as the
235 * handlers expect them off when they are called. They will
236 * be enabled again above.
242 * Deal with SIGIO first because the alarm handler might
243 * schedule, leaving the pending SIGIO stranded until we come
246 if (save_pending & SIGIO_MASK)
247 sig_handler_common_skas(SIGIO, NULL);
249 if (save_pending & SIGVTALRM_MASK)
250 real_alarm_handler(NULL);
254 int get_signals(void)
256 return signals_enabled;
259 int set_signals(int enable)
262 if (signals_enabled == enable)
265 ret = signals_enabled;
268 else block_signals();