2 * Copyright (C) 2004 PathScale, Inc
3 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
13 #include "sysdep/barrier.h"
14 #include "sysdep/sigcontext.h"
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.
24 #define SIGIO_MASK (1 << SIGIO_BIT)
26 #define SIGVTALRM_BIT 1
27 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
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.
34 static volatile int signals_enabled
= 1;
35 static volatile int pending
= 0;
37 void sig_handler(int sig
, struct sigcontext
*sc
)
41 enabled
= signals_enabled
;
42 if (!enabled
&& (sig
== SIGIO
)) {
43 pending
|= SIGIO_MASK
;
49 sig_handler_common_skas(sig
, sc
);
54 static void real_alarm_handler(struct sigcontext
*sc
)
56 struct uml_pt_regs regs
;
62 timer_handler(SIGVTALRM
, ®s
);
65 void alarm_handler(int sig
, struct sigcontext
*sc
)
69 enabled
= signals_enabled
;
70 if (!signals_enabled
) {
71 pending
|= SIGVTALRM_MASK
;
77 real_alarm_handler(sc
);
83 set_handler(SIGVTALRM
, (__sighandler_t
) alarm_handler
,
84 SA_ONSTACK
| SA_RESTART
, SIGUSR1
, SIGIO
, SIGWINCH
, -1);
87 void set_sigstack(void *sig_stack
, int size
)
89 stack_t stack
= ((stack_t
) { .ss_flags
= 0,
90 .ss_sp
= (__ptr_t
) sig_stack
,
91 .ss_size
= size
- sizeof(void *) });
93 if (sigaltstack(&stack
, NULL
) != 0)
94 panic("enabling signal stack failed, errno = %d\n", errno
);
97 void remove_sigstack(void)
99 stack_t stack
= ((stack_t
) { .ss_flags
= SS_DISABLE
,
103 if (sigaltstack(&stack
, NULL
) != 0)
104 panic("disabling signal stack failed, errno = %d\n", errno
);
107 void (*handlers
[_NSIG
])(int sig
, struct sigcontext
*sc
);
109 void handle_signal(int sig
, struct sigcontext
*sc
)
111 unsigned long pending
= 1UL << sig
;
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.
126 bail
= to_irq_stack(&pending
);
130 nested
= pending
& 1;
133 while ((sig
= ffs(pending
)) != 0){
135 pending
&= ~(1 << sig
);
136 (*handlers
[sig
])(sig
, sc
);
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.
146 pending
= from_irq_stack(nested
);
150 extern void hard_handler(int sig
);
152 void set_handler(int sig
, void (*handler
)(int), int flags
, ...)
154 struct sigaction action
;
159 handlers
[sig
] = (void (*)(int, struct sigcontext
*)) handler
;
160 action
.sa_handler
= hard_handler
;
162 sigemptyset(&action
.sa_mask
);
165 while ((mask
= va_arg(ap
, int)) != -1)
166 sigaddset(&action
.sa_mask
, mask
);
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
);
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
);
180 int change_sig(int signal
, int on
)
182 sigset_t sigset
, old
;
184 sigemptyset(&sigset
);
185 sigaddset(&sigset
, signal
);
186 sigprocmask(on
? SIG_UNBLOCK
: SIG_BLOCK
, &sigset
, &old
);
187 return !sigismember(&old
, signal
);
190 void block_signals(void)
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.
202 void unblock_signals(void)
206 if (signals_enabled
== 1)
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
216 * Save and reset save_pending after enabling signals. This
217 * way, pending won't be changed while we're reading it.
222 * Setting signals_enabled and reading pending must
223 * happen in this order.
227 save_pending
= pending
;
228 if (save_pending
== 0) {
230 * This must return with signals enabled, so
231 * this barrier ensures that writes are
232 * flushed out before the return. This might
233 * matter if gcc figures out how to inline
234 * this (unlikely, given its size) and decides
235 * to shuffle this code into the caller.
244 * We have pending interrupts, so disable signals, as the
245 * handlers expect them off when they are called. They will
246 * be enabled again above.
252 * Deal with SIGIO first because the alarm handler might
253 * schedule, leaving the pending SIGIO stranded until we come
256 if (save_pending
& SIGIO_MASK
)
257 sig_handler_common_skas(SIGIO
, NULL
);
259 if (save_pending
& SIGVTALRM_MASK
)
260 real_alarm_handler(NULL
);
264 int get_signals(void)
266 return signals_enabled
;
269 int set_signals(int enable
)
272 if (signals_enabled
== enable
)
275 ret
= signals_enabled
;
278 else block_signals();