uml: style fixes pass 2
[deliverable/linux.git] / arch / um / os-Linux / signal.c
1 /*
2 * Copyright (C) 2004 PathScale, Inc
3 * Licensed under the GPL
4 */
5
6 #include <signal.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include "user.h"
15 #include "signal_kern.h"
16 #include "sysdep/sigcontext.h"
17 #include "sysdep/barrier.h"
18 #include "sigcontext.h"
19 #include "mode.h"
20 #include "os.h"
21
22 /* These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
23 * together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
24 * be able to profile all of UML, not just the non-critical sections. If
25 * profiling is not thread-safe, then that is not my problem. We can disable
26 * profiling when SMP is enabled in that case.
27 */
28 #define SIGIO_BIT 0
29 #define SIGIO_MASK (1 << SIGIO_BIT)
30
31 #define SIGVTALRM_BIT 1
32 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
33
34 #define SIGALRM_BIT 2
35 #define SIGALRM_MASK (1 << SIGALRM_BIT)
36
37 /* These are used by both the signal handlers and
38 * block/unblock_signals. I don't want modifications cached in a
39 * register - they must go straight to memory.
40 */
41 static volatile int signals_enabled = 1;
42 static volatile int pending = 0;
43
44 void sig_handler(int sig, struct sigcontext *sc)
45 {
46 int enabled;
47
48 enabled = signals_enabled;
49 if(!enabled && (sig == SIGIO)){
50 pending |= SIGIO_MASK;
51 return;
52 }
53
54 block_signals();
55
56 sig_handler_common_skas(sig, sc);
57
58 set_signals(enabled);
59 }
60
61 static void real_alarm_handler(int sig, struct sigcontext *sc)
62 {
63 union uml_pt_regs regs;
64
65 if(sig == SIGALRM)
66 switch_timers(0);
67
68 if(sc != NULL)
69 copy_sc(&regs, sc);
70 regs.skas.is_user = 0;
71 unblock_signals();
72 timer_handler(sig, &regs);
73
74 if(sig == SIGALRM)
75 switch_timers(1);
76 }
77
78 void alarm_handler(int sig, struct sigcontext *sc)
79 {
80 int enabled;
81
82 enabled = signals_enabled;
83 if(!signals_enabled){
84 if(sig == SIGVTALRM)
85 pending |= SIGVTALRM_MASK;
86 else pending |= SIGALRM_MASK;
87
88 return;
89 }
90
91 block_signals();
92
93 real_alarm_handler(sig, sc);
94 set_signals(enabled);
95 }
96
97 void set_sigstack(void *sig_stack, int size)
98 {
99 stack_t stack = ((stack_t) { .ss_flags = 0,
100 .ss_sp = (__ptr_t) sig_stack,
101 .ss_size = size - sizeof(void *) });
102
103 if(sigaltstack(&stack, NULL) != 0)
104 panic("enabling signal stack failed, errno = %d\n", errno);
105 }
106
107 void remove_sigstack(void)
108 {
109 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
110 .ss_sp = NULL,
111 .ss_size = 0 });
112
113 if(sigaltstack(&stack, NULL) != 0)
114 panic("disabling signal stack failed, errno = %d\n", errno);
115 }
116
117 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
118
119 void handle_signal(int sig, struct sigcontext *sc)
120 {
121 unsigned long pending = 1UL << sig;
122
123 do {
124 int nested, bail;
125
126 /*
127 * pending comes back with one bit set for each
128 * interrupt that arrived while setting up the stack,
129 * plus a bit for this interrupt, plus the zero bit is
130 * set if this is a nested interrupt.
131 * If bail is true, then we interrupted another
132 * handler setting up the stack. In this case, we
133 * have to return, and the upper handler will deal
134 * with this interrupt.
135 */
136 bail = to_irq_stack(&pending);
137 if(bail)
138 return;
139
140 nested = pending & 1;
141 pending &= ~1;
142
143 while((sig = ffs(pending)) != 0){
144 sig--;
145 pending &= ~(1 << sig);
146 (*handlers[sig])(sig, sc);
147 }
148
149 /* Again, pending comes back with a mask of signals
150 * that arrived while tearing down the stack. If this
151 * is non-zero, we just go back, set up the stack
152 * again, and handle the new interrupts.
153 */
154 if(!nested)
155 pending = from_irq_stack(nested);
156 } while(pending);
157 }
158
159 extern void hard_handler(int sig);
160
161 void set_handler(int sig, void (*handler)(int), int flags, ...)
162 {
163 struct sigaction action;
164 va_list ap;
165 sigset_t sig_mask;
166 int mask;
167
168 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
169 action.sa_handler = hard_handler;
170
171 sigemptyset(&action.sa_mask);
172
173 va_start(ap, flags);
174 while((mask = va_arg(ap, int)) != -1)
175 sigaddset(&action.sa_mask, mask);
176 va_end(ap);
177
178 action.sa_flags = flags;
179 action.sa_restorer = NULL;
180 if(sigaction(sig, &action, NULL) < 0)
181 panic("sigaction failed - errno = %d\n", errno);
182
183 sigemptyset(&sig_mask);
184 sigaddset(&sig_mask, sig);
185 if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
186 panic("sigprocmask failed - errno = %d\n", errno);
187 }
188
189 int change_sig(int signal, int on)
190 {
191 sigset_t sigset, old;
192
193 sigemptyset(&sigset);
194 sigaddset(&sigset, signal);
195 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
196 return(!sigismember(&old, signal));
197 }
198
199 void block_signals(void)
200 {
201 signals_enabled = 0;
202 /* This must return with signals disabled, so this barrier
203 * ensures that writes are flushed out before the return.
204 * This might matter if gcc figures out how to inline this and
205 * decides to shuffle this code into the caller.
206 */
207 mb();
208 }
209
210 void unblock_signals(void)
211 {
212 int save_pending;
213
214 if(signals_enabled == 1)
215 return;
216
217 /* We loop because the IRQ handler returns with interrupts off. So,
218 * interrupts may have arrived and we need to re-enable them and
219 * recheck pending.
220 */
221 while(1){
222 /* Save and reset save_pending after enabling signals. This
223 * way, pending won't be changed while we're reading it.
224 */
225 signals_enabled = 1;
226
227 /* Setting signals_enabled and reading pending must
228 * happen in this order.
229 */
230 mb();
231
232 save_pending = pending;
233 if(save_pending == 0){
234 /* This must return with signals enabled, so
235 * this barrier ensures that writes are
236 * flushed out before the return. This might
237 * matter if gcc figures out how to inline
238 * this (unlikely, given its size) and decides
239 * to shuffle this code into the caller.
240 */
241 mb();
242 return;
243 }
244
245 pending = 0;
246
247 /* We have pending interrupts, so disable signals, as the
248 * handlers expect them off when they are called. They will
249 * be enabled again above.
250 */
251
252 signals_enabled = 0;
253
254 /* Deal with SIGIO first because the alarm handler might
255 * schedule, leaving the pending SIGIO stranded until we come
256 * back here.
257 */
258 if(save_pending & SIGIO_MASK)
259 sig_handler_common_skas(SIGIO, NULL);
260
261 if(save_pending & SIGALRM_MASK)
262 real_alarm_handler(SIGALRM, NULL);
263
264 if(save_pending & SIGVTALRM_MASK)
265 real_alarm_handler(SIGVTALRM, NULL);
266 }
267 }
268
269 int get_signals(void)
270 {
271 return signals_enabled;
272 }
273
274 int set_signals(int enable)
275 {
276 int ret;
277 if(signals_enabled == enable)
278 return enable;
279
280 ret = signals_enabled;
281 if(enable)
282 unblock_signals();
283 else block_signals();
284
285 return ret;
286 }
This page took 0.049959 seconds and 6 git commands to generate.