uml: tidy IRQ code
[safe/jmp/linux-2.6] / 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         CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
57                          sig, sc);
58
59         set_signals(enabled);
60 }
61
62 static void real_alarm_handler(int sig, struct sigcontext *sc)
63 {
64         union uml_pt_regs regs;
65
66         if(sig == SIGALRM)
67                 switch_timers(0);
68
69         if(sc != NULL)
70                 copy_sc(&regs, sc);
71         regs.skas.is_user = 0;
72         unblock_signals();
73         timer_handler(sig, &regs);
74
75         if(sig == SIGALRM)
76                 switch_timers(1);
77 }
78
79 void alarm_handler(int sig, struct sigcontext *sc)
80 {
81         int enabled;
82
83         enabled = signals_enabled;
84         if(!signals_enabled){
85                 if(sig == SIGVTALRM)
86                         pending |= SIGVTALRM_MASK;
87                 else pending |= SIGALRM_MASK;
88
89                 return;
90         }
91
92         block_signals();
93
94         real_alarm_handler(sig, sc);
95         set_signals(enabled);
96 }
97
98 void set_sigstack(void *sig_stack, int size)
99 {
100         stack_t stack = ((stack_t) { .ss_flags  = 0,
101                                      .ss_sp     = (__ptr_t) sig_stack,
102                                      .ss_size   = size - sizeof(void *) });
103
104         if(sigaltstack(&stack, NULL) != 0)
105                 panic("enabling signal stack failed, errno = %d\n", errno);
106 }
107
108 void remove_sigstack(void)
109 {
110         stack_t stack = ((stack_t) { .ss_flags  = SS_DISABLE,
111                                      .ss_sp     = NULL,
112                                      .ss_size   = 0 });
113
114         if(sigaltstack(&stack, NULL) != 0)
115                 panic("disabling signal stack failed, errno = %d\n", errno);
116 }
117
118 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
119
120 extern void hard_handler(int sig);
121
122 void set_handler(int sig, void (*handler)(int), int flags, ...)
123 {
124         struct sigaction action;
125         va_list ap;
126         sigset_t sig_mask;
127         int mask;
128
129         handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
130         action.sa_handler = hard_handler;
131
132         sigemptyset(&action.sa_mask);
133
134         va_start(ap, flags);
135         while((mask = va_arg(ap, int)) != -1)
136                 sigaddset(&action.sa_mask, mask);
137         va_end(ap);
138
139         action.sa_flags = flags;
140         action.sa_restorer = NULL;
141         if(sigaction(sig, &action, NULL) < 0)
142                 panic("sigaction failed - errno = %d\n", errno);
143
144         sigemptyset(&sig_mask);
145         sigaddset(&sig_mask, sig);
146         if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
147                 panic("sigprocmask failed - errno = %d\n", errno);
148 }
149
150 int change_sig(int signal, int on)
151 {
152         sigset_t sigset, old;
153
154         sigemptyset(&sigset);
155         sigaddset(&sigset, signal);
156         sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
157         return(!sigismember(&old, signal));
158 }
159
160 void block_signals(void)
161 {
162         signals_enabled = 0;
163         /* This must return with signals disabled, so this barrier
164          * ensures that writes are flushed out before the return.
165          * This might matter if gcc figures out how to inline this and
166          * decides to shuffle this code into the caller.
167          */
168         mb();
169 }
170
171 void unblock_signals(void)
172 {
173         int save_pending;
174
175         if(signals_enabled == 1)
176                 return;
177
178         /* We loop because the IRQ handler returns with interrupts off.  So,
179          * interrupts may have arrived and we need to re-enable them and
180          * recheck pending.
181          */
182         while(1){
183                 /* Save and reset save_pending after enabling signals.  This
184                  * way, pending won't be changed while we're reading it.
185                  */
186                 signals_enabled = 1;
187
188                 /* Setting signals_enabled and reading pending must
189                  * happen in this order.
190                  */
191                 mb();
192
193                 save_pending = pending;
194                 if(save_pending == 0){
195                         /* This must return with signals enabled, so
196                          * this barrier ensures that writes are
197                          * flushed out before the return.  This might
198                          * matter if gcc figures out how to inline
199                          * this (unlikely, given its size) and decides
200                          * to shuffle this code into the caller.
201                          */
202                         mb();
203                         return;
204                 }
205
206                 pending = 0;
207
208                 /* We have pending interrupts, so disable signals, as the
209                  * handlers expect them off when they are called.  They will
210                  * be enabled again above.
211                  */
212
213                 signals_enabled = 0;
214
215                 /* Deal with SIGIO first because the alarm handler might
216                  * schedule, leaving the pending SIGIO stranded until we come
217                  * back here.
218                  */
219                 if(save_pending & SIGIO_MASK)
220                         CHOOSE_MODE_PROC(sig_handler_common_tt,
221                                          sig_handler_common_skas, SIGIO, NULL);
222
223                 if(save_pending & SIGALRM_MASK)
224                         real_alarm_handler(SIGALRM, NULL);
225
226                 if(save_pending & SIGVTALRM_MASK)
227                         real_alarm_handler(SIGVTALRM, NULL);
228         }
229 }
230
231 int get_signals(void)
232 {
233         return signals_enabled;
234 }
235
236 int set_signals(int enable)
237 {
238         int ret;
239         if(signals_enabled == enable)
240                 return enable;
241
242         ret = signals_enabled;
243         if(enable)
244                 unblock_signals();
245         else block_signals();
246
247         return ret;
248 }