blob: b897e8592d7713c3730bfb4ab15f0b9ca716eb1d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 PathScale, Inc
3 * Licensed under the GPL
4 */
5
6#include <signal.h>
Gennady Sharapov0805d892006-01-08 01:01:29 -08007#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_util.h"
Gennady Sharapov0805d892006-01-08 01:01:29 -080015#include "user.h"
16#include "signal_kern.h"
17#include "sysdep/sigcontext.h"
Jeff Dike53b17332006-11-02 22:07:22 -080018#include "sysdep/barrier.h"
Gennady Sharapov0805d892006-01-08 01:01:29 -080019#include "sigcontext.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "mode.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080021#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Jeff Dike1d7173b2006-01-18 17:42:49 -080023/* These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
24 * together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
25 * be able to profile all of UML, not just the non-critical sections. If
26 * profiling is not thread-safe, then that is not my problem. We can disable
27 * profiling when SMP is enabled in that case.
28 */
29#define SIGIO_BIT 0
30#define SIGIO_MASK (1 << SIGIO_BIT)
31
32#define SIGVTALRM_BIT 1
33#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
34
35#define SIGALRM_BIT 2
36#define SIGALRM_MASK (1 << SIGALRM_BIT)
37
Jeff Dike53b17332006-11-02 22:07:22 -080038/* These are used by both the signal handlers and
39 * block/unblock_signals. I don't want modifications cached in a
40 * register - they must go straight to memory.
41 */
42static volatile int signals_enabled = 1;
43static volatile int pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -080044
Jeff Dike4b84c692006-09-25 23:33:04 -070045void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Jeff Dike1d7173b2006-01-18 17:42:49 -080047 int enabled;
48
Jeff Dike1d7173b2006-01-18 17:42:49 -080049 enabled = signals_enabled;
50 if(!enabled && (sig == SIGIO)){
51 pending |= SIGIO_MASK;
52 return;
53 }
54
55 block_signals();
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
58 sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080059
60 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Jeff Dike1d7173b2006-01-18 17:42:49 -080063static void real_alarm_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if(sig == SIGALRM)
66 switch_timers(0);
67
68 CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
69 sig, sc);
70
71 if(sig == SIGALRM)
72 switch_timers(1);
Jeff Dike1d7173b2006-01-18 17:42:49 -080073
74}
75
Jeff Dike4b84c692006-09-25 23:33:04 -070076void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080077{
Jeff Dike1d7173b2006-01-18 17:42:49 -080078 int enabled;
79
Jeff Dike1d7173b2006-01-18 17:42:49 -080080 enabled = signals_enabled;
81 if(!signals_enabled){
82 if(sig == SIGVTALRM)
83 pending |= SIGVTALRM_MASK;
84 else pending |= SIGALRM_MASK;
85
86 return;
87 }
88
89 block_signals();
90
91 real_alarm_handler(sig, sc);
92 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Gennady Sharapov0805d892006-01-08 01:01:29 -080095void set_sigstack(void *sig_stack, int size)
96{
97 stack_t stack = ((stack_t) { .ss_flags = 0,
98 .ss_sp = (__ptr_t) sig_stack,
99 .ss_size = size - sizeof(void *) });
100
101 if(sigaltstack(&stack, NULL) != 0)
102 panic("enabling signal stack failed, errno = %d\n", errno);
103}
104
105void remove_sigstack(void)
106{
107 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
108 .ss_sp = NULL,
109 .ss_size = 0 });
110
111 if(sigaltstack(&stack, NULL) != 0)
112 panic("disabling signal stack failed, errno = %d\n", errno);
113}
114
Jeff Dike4b84c692006-09-25 23:33:04 -0700115void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
116
117extern void hard_handler(int sig);
118
Gennady Sharapov0805d892006-01-08 01:01:29 -0800119void set_handler(int sig, void (*handler)(int), int flags, ...)
120{
121 struct sigaction action;
122 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800123 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800124 int mask;
125
Jeff Dike4b84c692006-09-25 23:33:04 -0700126 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
127 action.sa_handler = hard_handler;
128
Gennady Sharapov0805d892006-01-08 01:01:29 -0800129 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700130
131 va_start(ap, flags);
132 while((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800133 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800134 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700135
Gennady Sharapov0805d892006-01-08 01:01:29 -0800136 action.sa_flags = flags;
137 action.sa_restorer = NULL;
138 if(sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800139 panic("sigaction failed - errno = %d\n", errno);
140
141 sigemptyset(&sig_mask);
142 sigaddset(&sig_mask, sig);
143 if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
144 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800145}
146
147int change_sig(int signal, int on)
148{
149 sigset_t sigset, old;
150
151 sigemptyset(&sigset);
152 sigaddset(&sigset, signal);
153 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
154 return(!sigismember(&old, signal));
155}
156
Gennady Sharapov0805d892006-01-08 01:01:29 -0800157void block_signals(void)
158{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800159 signals_enabled = 0;
Jeff Dike53b17332006-11-02 22:07:22 -0800160 /* This must return with signals disabled, so this barrier
161 * ensures that writes are flushed out before the return.
162 * This might matter if gcc figures out how to inline this and
163 * decides to shuffle this code into the caller.
164 */
165 mb();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800166}
167
168void unblock_signals(void)
169{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800170 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800171
Jeff Dike1d7173b2006-01-18 17:42:49 -0800172 if(signals_enabled == 1)
173 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800174
Jeff Dike1d7173b2006-01-18 17:42:49 -0800175 /* We loop because the IRQ handler returns with interrupts off. So,
176 * interrupts may have arrived and we need to re-enable them and
177 * recheck pending.
178 */
179 while(1){
180 /* Save and reset save_pending after enabling signals. This
181 * way, pending won't be changed while we're reading it.
182 */
183 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800184
Jeff Dike53b17332006-11-02 22:07:22 -0800185 /* Setting signals_enabled and reading pending must
186 * happen in this order.
187 */
188 mb();
189
Jeff Dike1d7173b2006-01-18 17:42:49 -0800190 save_pending = pending;
Jeff Dike53b17332006-11-02 22:07:22 -0800191 if(save_pending == 0){
192 /* This must return with signals enabled, so
193 * this barrier ensures that writes are
194 * flushed out before the return. This might
195 * matter if gcc figures out how to inline
196 * this (unlikely, given its size) and decides
197 * to shuffle this code into the caller.
198 */
199 mb();
Jeff Dike1d7173b2006-01-18 17:42:49 -0800200 return;
Jeff Dike53b17332006-11-02 22:07:22 -0800201 }
Jeff Dike1d7173b2006-01-18 17:42:49 -0800202
203 pending = 0;
204
205 /* We have pending interrupts, so disable signals, as the
206 * handlers expect them off when they are called. They will
207 * be enabled again above.
208 */
209
210 signals_enabled = 0;
211
212 /* Deal with SIGIO first because the alarm handler might
213 * schedule, leaving the pending SIGIO stranded until we come
214 * back here.
215 */
216 if(save_pending & SIGIO_MASK)
217 CHOOSE_MODE_PROC(sig_handler_common_tt,
218 sig_handler_common_skas, SIGIO, NULL);
219
220 if(save_pending & SIGALRM_MASK)
221 real_alarm_handler(SIGALRM, NULL);
222
223 if(save_pending & SIGVTALRM_MASK)
224 real_alarm_handler(SIGVTALRM, NULL);
225 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800226}
227
228int get_signals(void)
229{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800230 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800231}
232
233int set_signals(int enable)
234{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800235 int ret;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800236 if(signals_enabled == enable)
237 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800238
Jeff Dike1d7173b2006-01-18 17:42:49 -0800239 ret = signals_enabled;
240 if(enable)
241 unblock_signals();
242 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800243
Jeff Dike1d7173b2006-01-18 17:42:49 -0800244 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800245}
Gennady Sharapovabaf6972006-01-18 17:42:46 -0800246
247void os_usr1_signal(int on)
248{
249 change_sig(SIGUSR1, on);
250}