blob: 62a66f38a9135d4976c597850cf69b8afca1b449 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2004 PathScale, Inc
Jeff Dikeba180fd2007-10-16 01:27:00 -07003 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Licensed under the GPL
5 */
6
Gennady Sharapov0805d892006-01-08 01:01:29 -08007#include <stdlib.h>
Gennady Sharapov0805d892006-01-08 01:01:29 -08008#include <stdarg.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -07009#include <errno.h>
10#include <signal.h>
11#include <strings.h>
Jeff Dikeedea1382008-02-04 22:30:46 -080012#include "kern_util.h"
Gennady Sharapovcff65c42006-01-18 17:42:42 -080013#include "os.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070014#include "sysdep/barrier.h"
15#include "sysdep/sigcontext.h"
16#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Jeff Dikefce8c412008-02-04 22:31:09 -080018/* Copied from linux/compiler-gcc.h since we can't include it directly */
19#define barrier() __asm__ __volatile__("": : :"memory")
20
Jeff Dikeba180fd2007-10-16 01:27:00 -070021/*
Jeff Dike61b63c52007-10-16 01:27:27 -070022 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080023 * 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
Jeff Dikefce8c412008-02-04 22:31:09 -080033static int signals_enabled;
34static unsigned int pending;
Jeff Dike1d7173b2006-01-18 17:42:49 -080035
Jeff Dike4b84c692006-09-25 23:33:04 -070036void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Jeff Dike1d7173b2006-01-18 17:42:49 -080038 int enabled;
39
Jeff Dike1d7173b2006-01-18 17:42:49 -080040 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070041 if (!enabled && (sig == SIGIO)) {
Jeff Dike1d7173b2006-01-18 17:42:49 -080042 pending |= SIGIO_MASK;
43 return;
44 }
45
46 block_signals();
47
Jeff Dike6aa802c2007-10-16 01:26:56 -070048 sig_handler_common_skas(sig, sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080049
50 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Jeff Dike61b63c52007-10-16 01:27:27 -070053static void real_alarm_handler(struct sigcontext *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Jeff Dike77bf4402007-10-16 01:26:58 -070055 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070056
Jeff Dikeba180fd2007-10-16 01:27:00 -070057 if (sc != NULL)
Jeff Dike2ea5bc52007-05-10 22:22:32 -070058 copy_sc(&regs, sc);
Jeff Dike77bf4402007-10-16 01:26:58 -070059 regs.is_user = 0;
Jeff Dike2ea5bc52007-05-10 22:22:32 -070060 unblock_signals();
Jeff Dike61b63c52007-10-16 01:27:27 -070061 timer_handler(SIGVTALRM, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -080062}
63
Jeff Dike4b84c692006-09-25 23:33:04 -070064void alarm_handler(int sig, struct sigcontext *sc)
Jeff Dike1d7173b2006-01-18 17:42:49 -080065{
Jeff Dike1d7173b2006-01-18 17:42:49 -080066 int enabled;
67
Jeff Dike1d7173b2006-01-18 17:42:49 -080068 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -070069 if (!signals_enabled) {
Jeff Dike61b63c52007-10-16 01:27:27 -070070 pending |= SIGVTALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080071 return;
72 }
73
74 block_signals();
75
Jeff Dike61b63c52007-10-16 01:27:27 -070076 real_alarm_handler(sc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080077 set_signals(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Jeff Dike78a26e22007-10-16 01:27:23 -070080void timer_init(void)
81{
82 set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
Jeff Dike61b63c52007-10-16 01:27:27 -070083 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
Jeff Dike78a26e22007-10-16 01:27:23 -070084}
85
Gennady Sharapov0805d892006-01-08 01:01:29 -080086void 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
Jeff Dikeba180fd2007-10-16 01:27:00 -070092 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -080093 panic("enabling signal stack failed, errno = %d\n", errno);
94}
95
96void remove_sigstack(void)
97{
98 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
99 .ss_sp = NULL,
100 .ss_size = 0 });
101
Jeff Dikeba180fd2007-10-16 01:27:00 -0700102 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800103 panic("disabling signal stack failed, errno = %d\n", errno);
104}
105
Jeff Dike4b84c692006-09-25 23:33:04 -0700106void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
107
Jeff Dikec14b8492007-05-10 22:22:34 -0700108void handle_signal(int sig, struct sigcontext *sc)
109{
Jeff Dike508a9272007-09-18 22:46:49 -0700110 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700111
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 */
Jeff Dike508a9272007-09-18 22:46:49 -0700125 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700126 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700127 return;
128
129 nested = pending & 1;
130 pending &= ~1;
131
Jeff Dikeba180fd2007-10-16 01:27:00 -0700132 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700133 sig--;
134 pending &= ~(1 << sig);
135 (*handlers[sig])(sig, sc);
136 }
137
Jeff Dikeba180fd2007-10-16 01:27:00 -0700138 /*
139 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700140 * 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 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700144 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700145 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700146 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700147}
148
Jeff Dike4b84c692006-09-25 23:33:04 -0700149extern void hard_handler(int sig);
150
Gennady Sharapov0805d892006-01-08 01:01:29 -0800151void set_handler(int sig, void (*handler)(int), int flags, ...)
152{
153 struct sigaction action;
154 va_list ap;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800155 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800156 int mask;
157
Jeff Dike4b84c692006-09-25 23:33:04 -0700158 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
159 action.sa_handler = hard_handler;
160
Gennady Sharapov0805d892006-01-08 01:01:29 -0800161 sigemptyset(&action.sa_mask);
Jeff Dike4b84c692006-09-25 23:33:04 -0700162
163 va_start(ap, flags);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700164 while ((mask = va_arg(ap, int)) != -1)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800165 sigaddset(&action.sa_mask, mask);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800166 va_end(ap);
Jeff Dike4b84c692006-09-25 23:33:04 -0700167
Gennady Sharapov0805d892006-01-08 01:01:29 -0800168 action.sa_flags = flags;
169 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700170 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800171 panic("sigaction failed - errno = %d\n", errno);
172
173 sigemptyset(&sig_mask);
174 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700175 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800176 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800177}
178
179int change_sig(int signal, int on)
180{
181 sigset_t sigset, old;
182
183 sigemptyset(&sigset);
184 sigaddset(&sigset, signal);
WANG Congc9a30722008-02-04 22:30:35 -0800185 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
186 return -errno;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700187 return !sigismember(&old, signal);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800188}
189
Gennady Sharapov0805d892006-01-08 01:01:29 -0800190void block_signals(void)
191{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800192 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700193 /*
194 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800195 * 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 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800199 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800200}
201
202void unblock_signals(void)
203{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800204 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800205
Jeff Dikeba180fd2007-10-16 01:27:00 -0700206 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800207 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800208
Jeff Dikeba180fd2007-10-16 01:27:00 -0700209 /*
210 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800211 * interrupts may have arrived and we need to re-enable them and
212 * recheck pending.
213 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700214 while(1) {
215 /*
216 * Save and reset save_pending after enabling signals. This
Jeff Dike1d7173b2006-01-18 17:42:49 -0800217 * way, pending won't be changed while we're reading it.
218 */
219 signals_enabled = 1;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800220
Jeff Dikeba180fd2007-10-16 01:27:00 -0700221 /*
222 * Setting signals_enabled and reading pending must
Jeff Dike53b17332006-11-02 22:07:22 -0800223 * happen in this order.
224 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800225 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800226
Jeff Dike1d7173b2006-01-18 17:42:49 -0800227 save_pending = pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800228 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800229 return;
230
231 pending = 0;
232
Jeff Dikeba180fd2007-10-16 01:27:00 -0700233 /*
234 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800235 * handlers expect them off when they are called. They will
236 * be enabled again above.
237 */
238
239 signals_enabled = 0;
240
Jeff Dikeba180fd2007-10-16 01:27:00 -0700241 /*
242 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800243 * schedule, leaving the pending SIGIO stranded until we come
244 * back here.
245 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700246 if (save_pending & SIGIO_MASK)
Jeff Dike6aa802c2007-10-16 01:26:56 -0700247 sig_handler_common_skas(SIGIO, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800248
Jeff Dikeba180fd2007-10-16 01:27:00 -0700249 if (save_pending & SIGVTALRM_MASK)
Jeff Dike61b63c52007-10-16 01:27:27 -0700250 real_alarm_handler(NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800251 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800252}
253
254int get_signals(void)
255{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800256 return signals_enabled;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800257}
258
259int set_signals(int enable)
260{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800261 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700262 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800263 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800264
Jeff Dike1d7173b2006-01-18 17:42:49 -0800265 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700266 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800267 unblock_signals();
268 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800269
Jeff Dike1d7173b2006-01-18 17:42:49 -0800270 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800271}