uml: code tidying under arch/um/os-Linux
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / um / os-Linux / signal.c
blob37302e86fda0d335296a4db47c02dda226bf3758
1 /*
2 * Copyright (C) 2004 PathScale, Inc
3 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
5 */
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <signal.h>
11 #include <strings.h>
12 #include "os.h"
13 #include "sysdep/barrier.h"
14 #include "sysdep/sigcontext.h"
15 #include "user.h"
18 * These are the asynchronous signals. SIGPROF is excluded because we want to
19 * be able to profile all of UML, not just the non-critical sections. If
20 * profiling is not thread-safe, then that is not my problem. We can disable
21 * profiling when SMP is enabled in that case.
23 #define SIGIO_BIT 0
24 #define SIGIO_MASK (1 << SIGIO_BIT)
26 #define SIGVTALRM_BIT 1
27 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
30 * These are used by both the signal handlers and
31 * block/unblock_signals. I don't want modifications cached in a
32 * register - they must go straight to memory.
34 static volatile int signals_enabled = 1;
35 static volatile int pending = 0;
37 void sig_handler(int sig, struct sigcontext *sc)
39 int enabled;
41 enabled = signals_enabled;
42 if (!enabled && (sig == SIGIO)) {
43 pending |= SIGIO_MASK;
44 return;
47 block_signals();
49 sig_handler_common_skas(sig, sc);
51 set_signals(enabled);
54 static void real_alarm_handler(struct sigcontext *sc)
56 struct uml_pt_regs regs;
58 if (sc != NULL)
59 copy_sc(&regs, sc);
60 regs.is_user = 0;
61 unblock_signals();
62 timer_handler(SIGVTALRM, &regs);
65 void alarm_handler(int sig, struct sigcontext *sc)
67 int enabled;
69 enabled = signals_enabled;
70 if (!signals_enabled) {
71 pending |= SIGVTALRM_MASK;
72 return;
75 block_signals();
77 real_alarm_handler(sc);
78 set_signals(enabled);
81 void timer_init(void)
83 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
84 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
87 void set_sigstack(void *sig_stack, int size)
89 stack_t stack = ((stack_t) { .ss_flags = 0,
90 .ss_sp = (__ptr_t) sig_stack,
91 .ss_size = size - sizeof(void *) });
93 if (sigaltstack(&stack, NULL) != 0)
94 panic("enabling signal stack failed, errno = %d\n", errno);
97 void remove_sigstack(void)
99 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
100 .ss_sp = NULL,
101 .ss_size = 0 });
103 if (sigaltstack(&stack, NULL) != 0)
104 panic("disabling signal stack failed, errno = %d\n", errno);
107 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
109 void handle_signal(int sig, struct sigcontext *sc)
111 unsigned long pending = 1UL << sig;
113 do {
114 int nested, bail;
117 * pending comes back with one bit set for each
118 * interrupt that arrived while setting up the stack,
119 * plus a bit for this interrupt, plus the zero bit is
120 * set if this is a nested interrupt.
121 * If bail is true, then we interrupted another
122 * handler setting up the stack. In this case, we
123 * have to return, and the upper handler will deal
124 * with this interrupt.
126 bail = to_irq_stack(&pending);
127 if (bail)
128 return;
130 nested = pending & 1;
131 pending &= ~1;
133 while ((sig = ffs(pending)) != 0){
134 sig--;
135 pending &= ~(1 << sig);
136 (*handlers[sig])(sig, sc);
140 * Again, pending comes back with a mask of signals
141 * that arrived while tearing down the stack. If this
142 * is non-zero, we just go back, set up the stack
143 * again, and handle the new interrupts.
145 if (!nested)
146 pending = from_irq_stack(nested);
147 } while (pending);
150 extern void hard_handler(int sig);
152 void set_handler(int sig, void (*handler)(int), int flags, ...)
154 struct sigaction action;
155 va_list ap;
156 sigset_t sig_mask;
157 int mask;
159 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
160 action.sa_handler = hard_handler;
162 sigemptyset(&action.sa_mask);
164 va_start(ap, flags);
165 while ((mask = va_arg(ap, int)) != -1)
166 sigaddset(&action.sa_mask, mask);
167 va_end(ap);
169 action.sa_flags = flags;
170 action.sa_restorer = NULL;
171 if (sigaction(sig, &action, NULL) < 0)
172 panic("sigaction failed - errno = %d\n", errno);
174 sigemptyset(&sig_mask);
175 sigaddset(&sig_mask, sig);
176 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
177 panic("sigprocmask failed - errno = %d\n", errno);
180 int change_sig(int signal, int on)
182 sigset_t sigset, old;
184 sigemptyset(&sigset);
185 sigaddset(&sigset, signal);
186 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
187 return -errno;
188 return !sigismember(&old, signal);
191 void block_signals(void)
193 signals_enabled = 0;
195 * This must return with signals disabled, so this barrier
196 * ensures that writes are flushed out before the return.
197 * This might matter if gcc figures out how to inline this and
198 * decides to shuffle this code into the caller.
200 mb();
203 void unblock_signals(void)
205 int save_pending;
207 if (signals_enabled == 1)
208 return;
211 * We loop because the IRQ handler returns with interrupts off. So,
212 * interrupts may have arrived and we need to re-enable them and
213 * recheck pending.
215 while(1) {
217 * Save and reset save_pending after enabling signals. This
218 * way, pending won't be changed while we're reading it.
220 signals_enabled = 1;
223 * Setting signals_enabled and reading pending must
224 * happen in this order.
226 mb();
228 save_pending = pending;
229 if (save_pending == 0) {
231 * This must return with signals enabled, so
232 * this barrier ensures that writes are
233 * flushed out before the return. This might
234 * matter if gcc figures out how to inline
235 * this (unlikely, given its size) and decides
236 * to shuffle this code into the caller.
238 mb();
239 return;
242 pending = 0;
245 * We have pending interrupts, so disable signals, as the
246 * handlers expect them off when they are called. They will
247 * be enabled again above.
250 signals_enabled = 0;
253 * Deal with SIGIO first because the alarm handler might
254 * schedule, leaving the pending SIGIO stranded until we come
255 * back here.
257 if (save_pending & SIGIO_MASK)
258 sig_handler_common_skas(SIGIO, NULL);
260 if (save_pending & SIGVTALRM_MASK)
261 real_alarm_handler(NULL);
265 int get_signals(void)
267 return signals_enabled;
270 int set_signals(int enable)
272 int ret;
273 if (signals_enabled == enable)
274 return enable;
276 ret = signals_enabled;
277 if (enable)
278 unblock_signals();
279 else block_signals();
281 return ret;