Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2004 PathScale, Inc | |
3 | * Licensed under the GPL | |
4 | */ | |
5 | ||
6 | #include <signal.h> | |
0805d89c GS |
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> | |
0805d89c GS |
14 | #include "user.h" |
15 | #include "signal_kern.h" | |
16 | #include "sysdep/sigcontext.h" | |
53b17332 | 17 | #include "sysdep/barrier.h" |
0805d89c | 18 | #include "sigcontext.h" |
1da177e4 | 19 | #include "mode.h" |
cff65c4f | 20 | #include "os.h" |
1da177e4 | 21 | |
1d7173ba JD |
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 | ||
53b17332 JD |
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; | |
1d7173ba | 43 | |
4b84c69b | 44 | void sig_handler(int sig, struct sigcontext *sc) |
1da177e4 | 45 | { |
1d7173ba JD |
46 | int enabled; |
47 | ||
1d7173ba JD |
48 | enabled = signals_enabled; |
49 | if(!enabled && (sig == SIGIO)){ | |
50 | pending |= SIGIO_MASK; | |
51 | return; | |
52 | } | |
53 | ||
54 | block_signals(); | |
55 | ||
6aa802ce | 56 | sig_handler_common_skas(sig, sc); |
1d7173ba JD |
57 | |
58 | set_signals(enabled); | |
1da177e4 LT |
59 | } |
60 | ||
1d7173ba | 61 | static void real_alarm_handler(int sig, struct sigcontext *sc) |
1da177e4 | 62 | { |
2ea5bc5e JD |
63 | union uml_pt_regs regs; |
64 | ||
1da177e4 LT |
65 | if(sig == SIGALRM) |
66 | switch_timers(0); | |
67 | ||
2ea5bc5e JD |
68 | if(sc != NULL) |
69 | copy_sc(®s, sc); | |
70 | regs.skas.is_user = 0; | |
71 | unblock_signals(); | |
72 | timer_handler(sig, ®s); | |
1da177e4 LT |
73 | |
74 | if(sig == SIGALRM) | |
75 | switch_timers(1); | |
1d7173ba JD |
76 | } |
77 | ||
4b84c69b | 78 | void alarm_handler(int sig, struct sigcontext *sc) |
1d7173ba | 79 | { |
1d7173ba JD |
80 | int enabled; |
81 | ||
1d7173ba JD |
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); | |
1da177e4 LT |
95 | } |
96 | ||
0805d89c GS |
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 | ||
4b84c69b JD |
117 | void (*handlers[_NSIG])(int sig, struct sigcontext *sc); |
118 | ||
c14b8494 JD |
119 | void handle_signal(int sig, struct sigcontext *sc) |
120 | { | |
508a9274 | 121 | unsigned long pending = 1UL << sig; |
c14b8494 JD |
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 | */ | |
508a9274 | 136 | bail = to_irq_stack(&pending); |
c14b8494 JD |
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 | ||
4b84c69b JD |
159 | extern void hard_handler(int sig); |
160 | ||
0805d89c GS |
161 | void set_handler(int sig, void (*handler)(int), int flags, ...) |
162 | { | |
163 | struct sigaction action; | |
164 | va_list ap; | |
1d7173ba | 165 | sigset_t sig_mask; |
0805d89c GS |
166 | int mask; |
167 | ||
4b84c69b JD |
168 | handlers[sig] = (void (*)(int, struct sigcontext *)) handler; |
169 | action.sa_handler = hard_handler; | |
170 | ||
0805d89c | 171 | sigemptyset(&action.sa_mask); |
4b84c69b JD |
172 | |
173 | va_start(ap, flags); | |
174 | while((mask = va_arg(ap, int)) != -1) | |
0805d89c | 175 | sigaddset(&action.sa_mask, mask); |
0805d89c | 176 | va_end(ap); |
4b84c69b | 177 | |
0805d89c GS |
178 | action.sa_flags = flags; |
179 | action.sa_restorer = NULL; | |
180 | if(sigaction(sig, &action, NULL) < 0) | |
1d7173ba JD |
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); | |
0805d89c GS |
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 | ||
0805d89c GS |
199 | void block_signals(void) |
200 | { | |
1d7173ba | 201 | signals_enabled = 0; |
53b17332 JD |
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(); | |
0805d89c GS |
208 | } |
209 | ||
210 | void unblock_signals(void) | |
211 | { | |
1d7173ba | 212 | int save_pending; |
0805d89c | 213 | |
1d7173ba JD |
214 | if(signals_enabled == 1) |
215 | return; | |
0805d89c | 216 | |
1d7173ba JD |
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 | ||
53b17332 JD |
227 | /* Setting signals_enabled and reading pending must |
228 | * happen in this order. | |
229 | */ | |
230 | mb(); | |
231 | ||
1d7173ba | 232 | save_pending = pending; |
53b17332 JD |
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(); | |
1d7173ba | 242 | return; |
53b17332 | 243 | } |
1d7173ba JD |
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) | |
6aa802ce | 259 | sig_handler_common_skas(SIGIO, NULL); |
1d7173ba JD |
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 | } | |
0805d89c GS |
267 | } |
268 | ||
269 | int get_signals(void) | |
270 | { | |
1d7173ba | 271 | return signals_enabled; |
0805d89c GS |
272 | } |
273 | ||
274 | int set_signals(int enable) | |
275 | { | |
0805d89c | 276 | int ret; |
1d7173ba JD |
277 | if(signals_enabled == enable) |
278 | return enable; | |
0805d89c | 279 | |
1d7173ba JD |
280 | ret = signals_enabled; |
281 | if(enable) | |
282 | unblock_signals(); | |
283 | else block_signals(); | |
0805d89c | 284 | |
1d7173ba | 285 | return ret; |
0805d89c | 286 | } |