Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2004 PathScale, Inc | |
ba180fd4 | 3 | * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4 LT |
4 | * Licensed under the GPL |
5 | */ | |
6 | ||
0805d89c | 7 | #include <stdlib.h> |
0805d89c | 8 | #include <stdarg.h> |
ba180fd4 JD |
9 | #include <errno.h> |
10 | #include <signal.h> | |
11 | #include <strings.h> | |
cff65c4f | 12 | #include "os.h" |
ba180fd4 JD |
13 | #include "sysdep/barrier.h" |
14 | #include "sysdep/sigcontext.h" | |
15 | #include "user.h" | |
1da177e4 | 16 | |
ba180fd4 | 17 | /* |
61b63c55 | 18 | * These are the asynchronous signals. SIGPROF is excluded because we want to |
1d7173ba JD |
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. | |
22 | */ | |
23 | #define SIGIO_BIT 0 | |
24 | #define SIGIO_MASK (1 << SIGIO_BIT) | |
25 | ||
26 | #define SIGVTALRM_BIT 1 | |
27 | #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT) | |
28 | ||
ba180fd4 JD |
29 | /* |
30 | * These are used by both the signal handlers and | |
53b17332 JD |
31 | * block/unblock_signals. I don't want modifications cached in a |
32 | * register - they must go straight to memory. | |
33 | */ | |
34 | static volatile int signals_enabled = 1; | |
35 | static volatile int pending = 0; | |
1d7173ba | 36 | |
4b84c69b | 37 | void sig_handler(int sig, struct sigcontext *sc) |
1da177e4 | 38 | { |
1d7173ba JD |
39 | int enabled; |
40 | ||
1d7173ba | 41 | enabled = signals_enabled; |
ba180fd4 | 42 | if (!enabled && (sig == SIGIO)) { |
1d7173ba JD |
43 | pending |= SIGIO_MASK; |
44 | return; | |
45 | } | |
46 | ||
47 | block_signals(); | |
48 | ||
6aa802ce | 49 | sig_handler_common_skas(sig, sc); |
1d7173ba JD |
50 | |
51 | set_signals(enabled); | |
1da177e4 LT |
52 | } |
53 | ||
61b63c55 | 54 | static void real_alarm_handler(struct sigcontext *sc) |
1da177e4 | 55 | { |
77bf4400 | 56 | struct uml_pt_regs regs; |
2ea5bc5e | 57 | |
ba180fd4 | 58 | if (sc != NULL) |
2ea5bc5e | 59 | copy_sc(®s, sc); |
77bf4400 | 60 | regs.is_user = 0; |
2ea5bc5e | 61 | unblock_signals(); |
61b63c55 | 62 | timer_handler(SIGVTALRM, ®s); |
1d7173ba JD |
63 | } |
64 | ||
4b84c69b | 65 | void alarm_handler(int sig, struct sigcontext *sc) |
1d7173ba | 66 | { |
1d7173ba JD |
67 | int enabled; |
68 | ||
1d7173ba | 69 | enabled = signals_enabled; |
ba180fd4 | 70 | if (!signals_enabled) { |
61b63c55 | 71 | pending |= SIGVTALRM_MASK; |
1d7173ba JD |
72 | return; |
73 | } | |
74 | ||
75 | block_signals(); | |
76 | ||
61b63c55 | 77 | real_alarm_handler(sc); |
1d7173ba | 78 | set_signals(enabled); |
1da177e4 LT |
79 | } |
80 | ||
78a26e25 JD |
81 | void timer_init(void) |
82 | { | |
83 | set_handler(SIGVTALRM, (__sighandler_t) alarm_handler, | |
61b63c55 | 84 | SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1); |
78a26e25 JD |
85 | } |
86 | ||
0805d89c GS |
87 | void set_sigstack(void *sig_stack, int size) |
88 | { | |
89 | stack_t stack = ((stack_t) { .ss_flags = 0, | |
90 | .ss_sp = (__ptr_t) sig_stack, | |
91 | .ss_size = size - sizeof(void *) }); | |
92 | ||
ba180fd4 | 93 | if (sigaltstack(&stack, NULL) != 0) |
0805d89c GS |
94 | panic("enabling signal stack failed, errno = %d\n", errno); |
95 | } | |
96 | ||
97 | void remove_sigstack(void) | |
98 | { | |
99 | stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE, | |
100 | .ss_sp = NULL, | |
101 | .ss_size = 0 }); | |
102 | ||
ba180fd4 | 103 | if (sigaltstack(&stack, NULL) != 0) |
0805d89c GS |
104 | panic("disabling signal stack failed, errno = %d\n", errno); |
105 | } | |
106 | ||
4b84c69b JD |
107 | void (*handlers[_NSIG])(int sig, struct sigcontext *sc); |
108 | ||
c14b8494 JD |
109 | void handle_signal(int sig, struct sigcontext *sc) |
110 | { | |
508a9274 | 111 | unsigned long pending = 1UL << sig; |
c14b8494 JD |
112 | |
113 | do { | |
114 | int nested, bail; | |
115 | ||
116 | /* | |
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. | |
125 | */ | |
508a9274 | 126 | bail = to_irq_stack(&pending); |
ba180fd4 | 127 | if (bail) |
c14b8494 JD |
128 | return; |
129 | ||
130 | nested = pending & 1; | |
131 | pending &= ~1; | |
132 | ||
ba180fd4 | 133 | while ((sig = ffs(pending)) != 0){ |
c14b8494 JD |
134 | sig--; |
135 | pending &= ~(1 << sig); | |
136 | (*handlers[sig])(sig, sc); | |
137 | } | |
138 | ||
ba180fd4 JD |
139 | /* |
140 | * Again, pending comes back with a mask of signals | |
c14b8494 JD |
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. | |
144 | */ | |
ba180fd4 | 145 | if (!nested) |
c14b8494 | 146 | pending = from_irq_stack(nested); |
ba180fd4 | 147 | } while (pending); |
c14b8494 JD |
148 | } |
149 | ||
4b84c69b JD |
150 | extern void hard_handler(int sig); |
151 | ||
0805d89c GS |
152 | void set_handler(int sig, void (*handler)(int), int flags, ...) |
153 | { | |
154 | struct sigaction action; | |
155 | va_list ap; | |
1d7173ba | 156 | sigset_t sig_mask; |
0805d89c GS |
157 | int mask; |
158 | ||
4b84c69b JD |
159 | handlers[sig] = (void (*)(int, struct sigcontext *)) handler; |
160 | action.sa_handler = hard_handler; | |
161 | ||
0805d89c | 162 | sigemptyset(&action.sa_mask); |
4b84c69b JD |
163 | |
164 | va_start(ap, flags); | |
ba180fd4 | 165 | while ((mask = va_arg(ap, int)) != -1) |
0805d89c | 166 | sigaddset(&action.sa_mask, mask); |
0805d89c | 167 | va_end(ap); |
4b84c69b | 168 | |
0805d89c GS |
169 | action.sa_flags = flags; |
170 | action.sa_restorer = NULL; | |
ba180fd4 | 171 | if (sigaction(sig, &action, NULL) < 0) |
1d7173ba JD |
172 | panic("sigaction failed - errno = %d\n", errno); |
173 | ||
174 | sigemptyset(&sig_mask); | |
175 | sigaddset(&sig_mask, sig); | |
ba180fd4 | 176 | if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0) |
1d7173ba | 177 | panic("sigprocmask failed - errno = %d\n", errno); |
0805d89c GS |
178 | } |
179 | ||
180 | int change_sig(int signal, int on) | |
181 | { | |
182 | sigset_t sigset, old; | |
183 | ||
184 | sigemptyset(&sigset); | |
185 | sigaddset(&sigset, signal); | |
c9a3072d WC |
186 | if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0) |
187 | return -errno; | |
ba180fd4 | 188 | return !sigismember(&old, signal); |
0805d89c GS |
189 | } |
190 | ||
0805d89c GS |
191 | void block_signals(void) |
192 | { | |
1d7173ba | 193 | signals_enabled = 0; |
ba180fd4 JD |
194 | /* |
195 | * This must return with signals disabled, so this barrier | |
53b17332 JD |
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. | |
199 | */ | |
200 | mb(); | |
0805d89c GS |
201 | } |
202 | ||
203 | void unblock_signals(void) | |
204 | { | |
1d7173ba | 205 | int save_pending; |
0805d89c | 206 | |
ba180fd4 | 207 | if (signals_enabled == 1) |
1d7173ba | 208 | return; |
0805d89c | 209 | |
ba180fd4 JD |
210 | /* |
211 | * We loop because the IRQ handler returns with interrupts off. So, | |
1d7173ba JD |
212 | * interrupts may have arrived and we need to re-enable them and |
213 | * recheck pending. | |
214 | */ | |
ba180fd4 JD |
215 | while(1) { |
216 | /* | |
217 | * Save and reset save_pending after enabling signals. This | |
1d7173ba JD |
218 | * way, pending won't be changed while we're reading it. |
219 | */ | |
220 | signals_enabled = 1; | |
221 | ||
ba180fd4 JD |
222 | /* |
223 | * Setting signals_enabled and reading pending must | |
53b17332 JD |
224 | * happen in this order. |
225 | */ | |
226 | mb(); | |
227 | ||
1d7173ba | 228 | save_pending = pending; |
ba180fd4 JD |
229 | if (save_pending == 0) { |
230 | /* | |
231 | * This must return with signals enabled, so | |
53b17332 JD |
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. | |
237 | */ | |
238 | mb(); | |
1d7173ba | 239 | return; |
53b17332 | 240 | } |
1d7173ba JD |
241 | |
242 | pending = 0; | |
243 | ||
ba180fd4 JD |
244 | /* |
245 | * We have pending interrupts, so disable signals, as the | |
1d7173ba JD |
246 | * handlers expect them off when they are called. They will |
247 | * be enabled again above. | |
248 | */ | |
249 | ||
250 | signals_enabled = 0; | |
251 | ||
ba180fd4 JD |
252 | /* |
253 | * Deal with SIGIO first because the alarm handler might | |
1d7173ba JD |
254 | * schedule, leaving the pending SIGIO stranded until we come |
255 | * back here. | |
256 | */ | |
ba180fd4 | 257 | if (save_pending & SIGIO_MASK) |
6aa802ce | 258 | sig_handler_common_skas(SIGIO, NULL); |
1d7173ba | 259 | |
ba180fd4 | 260 | if (save_pending & SIGVTALRM_MASK) |
61b63c55 | 261 | real_alarm_handler(NULL); |
1d7173ba | 262 | } |
0805d89c GS |
263 | } |
264 | ||
265 | int get_signals(void) | |
266 | { | |
1d7173ba | 267 | return signals_enabled; |
0805d89c GS |
268 | } |
269 | ||
270 | int set_signals(int enable) | |
271 | { | |
0805d89c | 272 | int ret; |
ba180fd4 | 273 | if (signals_enabled == enable) |
1d7173ba | 274 | return enable; |
0805d89c | 275 | |
1d7173ba | 276 | ret = signals_enabled; |
ba180fd4 | 277 | if (enable) |
1d7173ba JD |
278 | unblock_signals(); |
279 | else block_signals(); | |
0805d89c | 280 | |
1d7173ba | 281 | return ret; |
0805d89c | 282 | } |