]> err.no Git - linux-2.6/blob - arch/um/os-Linux/signal.c
uml: use barrier() instead of mb()
[linux-2.6] / 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 "kern_util.h"
13 #include "os.h"
14 #include "sysdep/barrier.h"
15 #include "sysdep/sigcontext.h"
16 #include "user.h"
17
18 /* Copied from linux/compiler-gcc.h since we can't include it directly */
19 #define barrier() __asm__ __volatile__("": : :"memory")
20
21 /*
22  * These are the asynchronous signals.  SIGPROF is excluded because we want to
23  * be able to profile all of UML, not just the non-critical sections.  If
24  * profiling is not thread-safe, then that is not my problem.  We can disable
25  * profiling when SMP is enabled in that case.
26  */
27 #define SIGIO_BIT 0
28 #define SIGIO_MASK (1 << SIGIO_BIT)
29
30 #define SIGVTALRM_BIT 1
31 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
32
33 static int signals_enabled;
34 static unsigned int pending;
35
36 void sig_handler(int sig, struct sigcontext *sc)
37 {
38         int enabled;
39
40         enabled = signals_enabled;
41         if (!enabled && (sig == SIGIO)) {
42                 pending |= SIGIO_MASK;
43                 return;
44         }
45
46         block_signals();
47
48         sig_handler_common_skas(sig, sc);
49
50         set_signals(enabled);
51 }
52
53 static void real_alarm_handler(struct sigcontext *sc)
54 {
55         struct uml_pt_regs regs;
56
57         if (sc != NULL)
58                 copy_sc(&regs, sc);
59         regs.is_user = 0;
60         unblock_signals();
61         timer_handler(SIGVTALRM, &regs);
62 }
63
64 void alarm_handler(int sig, struct sigcontext *sc)
65 {
66         int enabled;
67
68         enabled = signals_enabled;
69         if (!signals_enabled) {
70                 pending |= SIGVTALRM_MASK;
71                 return;
72         }
73
74         block_signals();
75
76         real_alarm_handler(sc);
77         set_signals(enabled);
78 }
79
80 void timer_init(void)
81 {
82         set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
83                     SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
84 }
85
86 void set_sigstack(void *sig_stack, int size)
87 {
88         stack_t stack = ((stack_t) { .ss_flags  = 0,
89                                      .ss_sp     = (__ptr_t) sig_stack,
90                                      .ss_size   = size - sizeof(void *) });
91
92         if (sigaltstack(&stack, NULL) != 0)
93                 panic("enabling signal stack failed, errno = %d\n", errno);
94 }
95
96 void remove_sigstack(void)
97 {
98         stack_t stack = ((stack_t) { .ss_flags  = SS_DISABLE,
99                                      .ss_sp     = NULL,
100                                      .ss_size   = 0 });
101
102         if (sigaltstack(&stack, NULL) != 0)
103                 panic("disabling signal stack failed, errno = %d\n", errno);
104 }
105
106 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
107
108 void handle_signal(int sig, struct sigcontext *sc)
109 {
110         unsigned long pending = 1UL << sig;
111
112         do {
113                 int nested, bail;
114
115                 /*
116                  * pending comes back with one bit set for each
117                  * interrupt that arrived while setting up the stack,
118                  * plus a bit for this interrupt, plus the zero bit is
119                  * set if this is a nested interrupt.
120                  * If bail is true, then we interrupted another
121                  * handler setting up the stack.  In this case, we
122                  * have to return, and the upper handler will deal
123                  * with this interrupt.
124                  */
125                 bail = to_irq_stack(&pending);
126                 if (bail)
127                         return;
128
129                 nested = pending & 1;
130                 pending &= ~1;
131
132                 while ((sig = ffs(pending)) != 0){
133                         sig--;
134                         pending &= ~(1 << sig);
135                         (*handlers[sig])(sig, sc);
136                 }
137
138                 /*
139                  * Again, pending comes back with a mask of signals
140                  * that arrived while tearing down the stack.  If this
141                  * is non-zero, we just go back, set up the stack
142                  * again, and handle the new interrupts.
143                  */
144                 if (!nested)
145                         pending = from_irq_stack(nested);
146         } while (pending);
147 }
148
149 extern void hard_handler(int sig);
150
151 void set_handler(int sig, void (*handler)(int), int flags, ...)
152 {
153         struct sigaction action;
154         va_list ap;
155         sigset_t sig_mask;
156         int mask;
157
158         handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
159         action.sa_handler = hard_handler;
160
161         sigemptyset(&action.sa_mask);
162
163         va_start(ap, flags);
164         while ((mask = va_arg(ap, int)) != -1)
165                 sigaddset(&action.sa_mask, mask);
166         va_end(ap);
167
168         action.sa_flags = flags;
169         action.sa_restorer = NULL;
170         if (sigaction(sig, &action, NULL) < 0)
171                 panic("sigaction failed - errno = %d\n", errno);
172
173         sigemptyset(&sig_mask);
174         sigaddset(&sig_mask, sig);
175         if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
176                 panic("sigprocmask failed - errno = %d\n", errno);
177 }
178
179 int change_sig(int signal, int on)
180 {
181         sigset_t sigset, old;
182
183         sigemptyset(&sigset);
184         sigaddset(&sigset, signal);
185         if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
186                 return -errno;
187         return !sigismember(&old, signal);
188 }
189
190 void block_signals(void)
191 {
192         signals_enabled = 0;
193         /*
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.
198          */
199         barrier();
200 }
201
202 void unblock_signals(void)
203 {
204         int save_pending;
205
206         if (signals_enabled == 1)
207                 return;
208
209         /*
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.
213          */
214         while(1) {
215                 /*
216                  * Save and reset save_pending after enabling signals.  This
217                  * way, pending won't be changed while we're reading it.
218                  */
219                 signals_enabled = 1;
220
221                 /*
222                  * Setting signals_enabled and reading pending must
223                  * happen in this order.
224                  */
225                 barrier();
226
227                 save_pending = pending;
228                 if (save_pending == 0)
229                         return;
230
231                 pending = 0;
232
233                 /*
234                  * We have pending interrupts, so disable signals, as the
235                  * handlers expect them off when they are called.  They will
236                  * be enabled again above.
237                  */
238
239                 signals_enabled = 0;
240
241                 /*
242                  * Deal with SIGIO first because the alarm handler might
243                  * schedule, leaving the pending SIGIO stranded until we come
244                  * back here.
245                  */
246                 if (save_pending & SIGIO_MASK)
247                         sig_handler_common_skas(SIGIO, NULL);
248
249                 if (save_pending & SIGVTALRM_MASK)
250                         real_alarm_handler(NULL);
251         }
252 }
253
254 int get_signals(void)
255 {
256         return signals_enabled;
257 }
258
259 int set_signals(int enable)
260 {
261         int ret;
262         if (signals_enabled == enable)
263                 return enable;
264
265         ret = signals_enabled;
266         if (enable)
267                 unblock_signals();
268         else block_signals();
269
270         return ret;
271 }