uml: code tidying under arch/um/os-Linux
[linux-2.6-block.git] / arch / um / os-Linux / signal.c
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  */
6
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"
16
17 /*
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.
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
29 /*
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.
33  */
34 static volatile int signals_enabled = 1;
35 static volatile int pending = 0;
36
37 void sig_handler(int sig, struct sigcontext *sc)
38 {
39         int enabled;
40
41         enabled = signals_enabled;
42         if (!enabled && (sig == SIGIO)) {
43                 pending |= SIGIO_MASK;
44                 return;
45         }
46
47         block_signals();
48
49         sig_handler_common_skas(sig, sc);
50
51         set_signals(enabled);
52 }
53
54 static void real_alarm_handler(struct sigcontext *sc)
55 {
56         struct uml_pt_regs regs;
57
58         if (sc != NULL)
59                 copy_sc(&regs, sc);
60         regs.is_user = 0;
61         unblock_signals();
62         timer_handler(SIGVTALRM, &regs);
63 }
64
65 void alarm_handler(int sig, struct sigcontext *sc)
66 {
67         int enabled;
68
69         enabled = signals_enabled;
70         if (!signals_enabled) {
71                 pending |= SIGVTALRM_MASK;
72                 return;
73         }
74
75         block_signals();
76
77         real_alarm_handler(sc);
78         set_signals(enabled);
79 }
80
81 void timer_init(void)
82 {
83         set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
84                     SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
85 }
86
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
93         if (sigaltstack(&stack, NULL) != 0)
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
103         if (sigaltstack(&stack, NULL) != 0)
104                 panic("disabling signal stack failed, errno = %d\n", errno);
105 }
106
107 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
108
109 void handle_signal(int sig, struct sigcontext *sc)
110 {
111         unsigned long pending = 1UL << sig;
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                  */
126                 bail = to_irq_stack(&pending);
127                 if (bail)
128                         return;
129
130                 nested = pending & 1;
131                 pending &= ~1;
132
133                 while ((sig = ffs(pending)) != 0){
134                         sig--;
135                         pending &= ~(1 << sig);
136                         (*handlers[sig])(sig, sc);
137                 }
138
139                 /*
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.
144                  */
145                 if (!nested)
146                         pending = from_irq_stack(nested);
147         } while (pending);
148 }
149
150 extern void hard_handler(int sig);
151
152 void set_handler(int sig, void (*handler)(int), int flags, ...)
153 {
154         struct sigaction action;
155         va_list ap;
156         sigset_t sig_mask;
157         int mask;
158
159         handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
160         action.sa_handler = hard_handler;
161
162         sigemptyset(&action.sa_mask);
163
164         va_start(ap, flags);
165         while ((mask = va_arg(ap, int)) != -1)
166                 sigaddset(&action.sa_mask, mask);
167         va_end(ap);
168
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);
173
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);
178 }
179
180 int change_sig(int signal, int on)
181 {
182         sigset_t sigset, old;
183
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);
189 }
190
191 void block_signals(void)
192 {
193         signals_enabled = 0;
194         /*
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.
199          */
200         mb();
201 }
202
203 void unblock_signals(void)
204 {
205         int save_pending;
206
207         if (signals_enabled == 1)
208                 return;
209
210         /*
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.
214          */
215         while(1) {
216                 /*
217                  * Save and reset save_pending after enabling signals.  This
218                  * way, pending won't be changed while we're reading it.
219                  */
220                 signals_enabled = 1;
221
222                 /*
223                  * Setting signals_enabled and reading pending must
224                  * happen in this order.
225                  */
226                 mb();
227
228                 save_pending = pending;
229                 if (save_pending == 0) {
230                         /*
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.
237                          */
238                         mb();
239                         return;
240                 }
241
242                 pending = 0;
243
244                 /*
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.
248                  */
249
250                 signals_enabled = 0;
251
252                 /*
253                  * Deal with SIGIO first because the alarm handler might
254                  * schedule, leaving the pending SIGIO stranded until we come
255                  * back here.
256                  */
257                 if (save_pending & SIGIO_MASK)
258                         sig_handler_common_skas(SIGIO, NULL);
259
260                 if (save_pending & SIGVTALRM_MASK)
261                         real_alarm_handler(NULL);
262         }
263 }
264
265 int get_signals(void)
266 {
267         return signals_enabled;
268 }
269
270 int set_signals(int enable)
271 {
272         int ret;
273         if (signals_enabled == enable)
274                 return enable;
275
276         ret = signals_enabled;
277         if (enable)
278                 unblock_signals();
279         else block_signals();
280
281         return ret;
282 }