Revert "Fix very high interrupt rate for IRQ8 (rtc) unless pnpacpi=off"
[pv_ops_mirror.git] / arch / um / os-Linux / signal.c
blobe9800b0b5689b6abc750606ab886826709964e56
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 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
187 return !sigismember(&old, signal);
190 void block_signals(void)
192 signals_enabled = 0;
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.
199 mb();
202 void unblock_signals(void)
204 int save_pending;
206 if (signals_enabled == 1)
207 return;
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
212 * recheck pending.
214 while(1) {
216 * Save and reset save_pending after enabling signals. This
217 * way, pending won't be changed while we're reading it.
219 signals_enabled = 1;
222 * Setting signals_enabled and reading pending must
223 * happen in this order.
225 mb();
227 save_pending = pending;
228 if (save_pending == 0) {
230 * This must return with signals enabled, so
231 * this barrier ensures that writes are
232 * flushed out before the return. This might
233 * matter if gcc figures out how to inline
234 * this (unlikely, given its size) and decides
235 * to shuffle this code into the caller.
237 mb();
238 return;
241 pending = 0;
244 * We have pending interrupts, so disable signals, as the
245 * handlers expect them off when they are called. They will
246 * be enabled again above.
249 signals_enabled = 0;
252 * Deal with SIGIO first because the alarm handler might
253 * schedule, leaving the pending SIGIO stranded until we come
254 * back here.
256 if (save_pending & SIGIO_MASK)
257 sig_handler_common_skas(SIGIO, NULL);
259 if (save_pending & SIGVTALRM_MASK)
260 real_alarm_handler(NULL);
264 int get_signals(void)
266 return signals_enabled;
269 int set_signals(int enable)
271 int ret;
272 if (signals_enabled == enable)
273 return enable;
275 ret = signals_enabled;
276 if (enable)
277 unblock_signals();
278 else block_signals();
280 return ret;