signal.c 6.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (C) 2004 PathScale, Inc
J
Jeff Dike 已提交
3
 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
4 5 6
 * Licensed under the GPL
 */

7 8
#include <stdlib.h>
#include <stdarg.h>
J
Jeff Dike 已提交
9 10 11
#include <errno.h>
#include <signal.h>
#include <strings.h>
J
Jeff Dike 已提交
12
#include "kern_util.h"
13
#include "os.h"
J
Jeff Dike 已提交
14 15 16
#include "sysdep/barrier.h"
#include "sysdep/sigcontext.h"
#include "user.h"
L
Linus Torvalds 已提交
17

J
Jeff Dike 已提交
18
/*
J
Jeff Dike 已提交
19
 * These are the asynchronous signals.  SIGPROF is excluded because we want to
20 21 22 23 24 25 26 27 28 29
 * be able to profile all of UML, not just the non-critical sections.  If
 * profiling is not thread-safe, then that is not my problem.  We can disable
 * profiling when SMP is enabled in that case.
 */
#define SIGIO_BIT 0
#define SIGIO_MASK (1 << SIGIO_BIT)

#define SIGVTALRM_BIT 1
#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)

J
Jeff Dike 已提交
30 31
/*
 * These are used by both the signal handlers and
J
Jeff Dike 已提交
32 33 34 35 36
 * block/unblock_signals.  I don't want modifications cached in a
 * register - they must go straight to memory.
 */
static volatile int signals_enabled = 1;
static volatile int pending = 0;
37

38
void sig_handler(int sig, struct sigcontext *sc)
L
Linus Torvalds 已提交
39
{
40 41 42
	int enabled;

	enabled = signals_enabled;
J
Jeff Dike 已提交
43
	if (!enabled && (sig == SIGIO)) {
44 45 46 47 48 49
		pending |= SIGIO_MASK;
		return;
	}

	block_signals();

J
Jeff Dike 已提交
50
	sig_handler_common_skas(sig, sc);
51 52

	set_signals(enabled);
L
Linus Torvalds 已提交
53 54
}

J
Jeff Dike 已提交
55
static void real_alarm_handler(struct sigcontext *sc)
L
Linus Torvalds 已提交
56
{
57
	struct uml_pt_regs regs;
J
Jeff Dike 已提交
58

J
Jeff Dike 已提交
59
	if (sc != NULL)
J
Jeff Dike 已提交
60
		copy_sc(&regs, sc);
61
	regs.is_user = 0;
J
Jeff Dike 已提交
62
	unblock_signals();
J
Jeff Dike 已提交
63
	timer_handler(SIGVTALRM, &regs);
64 65
}

66
void alarm_handler(int sig, struct sigcontext *sc)
67 68 69 70
{
	int enabled;

	enabled = signals_enabled;
J
Jeff Dike 已提交
71
	if (!signals_enabled) {
J
Jeff Dike 已提交
72
		pending |= SIGVTALRM_MASK;
73 74 75 76 77
		return;
	}

	block_signals();

J
Jeff Dike 已提交
78
	real_alarm_handler(sc);
79
	set_signals(enabled);
L
Linus Torvalds 已提交
80 81
}

J
Jeff Dike 已提交
82 83 84
void timer_init(void)
{
	set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
J
Jeff Dike 已提交
85
		    SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
J
Jeff Dike 已提交
86 87
}

88 89 90 91 92 93
void set_sigstack(void *sig_stack, int size)
{
	stack_t stack = ((stack_t) { .ss_flags	= 0,
				     .ss_sp	= (__ptr_t) sig_stack,
				     .ss_size 	= size - sizeof(void *) });

J
Jeff Dike 已提交
94
	if (sigaltstack(&stack, NULL) != 0)
95 96 97 98 99 100 101 102 103
		panic("enabling signal stack failed, errno = %d\n", errno);
}

void remove_sigstack(void)
{
	stack_t stack = ((stack_t) { .ss_flags	= SS_DISABLE,
				     .ss_sp	= NULL,
				     .ss_size	= 0 });

J
Jeff Dike 已提交
104
	if (sigaltstack(&stack, NULL) != 0)
105 106 107
		panic("disabling signal stack failed, errno = %d\n", errno);
}

108 109
void (*handlers[_NSIG])(int sig, struct sigcontext *sc);

J
Jeff Dike 已提交
110 111
void handle_signal(int sig, struct sigcontext *sc)
{
J
Jeff Dike 已提交
112
	unsigned long pending = 1UL << sig;
J
Jeff Dike 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126

	do {
		int nested, bail;

		/*
		 * pending comes back with one bit set for each
		 * interrupt that arrived while setting up the stack,
		 * plus a bit for this interrupt, plus the zero bit is
		 * set if this is a nested interrupt.
		 * If bail is true, then we interrupted another
		 * handler setting up the stack.  In this case, we
		 * have to return, and the upper handler will deal
		 * with this interrupt.
		 */
J
Jeff Dike 已提交
127
		bail = to_irq_stack(&pending);
J
Jeff Dike 已提交
128
		if (bail)
J
Jeff Dike 已提交
129 130 131 132 133
			return;

		nested = pending & 1;
		pending &= ~1;

J
Jeff Dike 已提交
134
		while ((sig = ffs(pending)) != 0){
J
Jeff Dike 已提交
135 136 137 138 139
			sig--;
			pending &= ~(1 << sig);
			(*handlers[sig])(sig, sc);
		}

J
Jeff Dike 已提交
140 141
		/*
		 * Again, pending comes back with a mask of signals
J
Jeff Dike 已提交
142 143 144 145
		 * that arrived while tearing down the stack.  If this
		 * is non-zero, we just go back, set up the stack
		 * again, and handle the new interrupts.
		 */
J
Jeff Dike 已提交
146
		if (!nested)
J
Jeff Dike 已提交
147
			pending = from_irq_stack(nested);
J
Jeff Dike 已提交
148
	} while (pending);
J
Jeff Dike 已提交
149 150
}

151 152
extern void hard_handler(int sig);

153 154 155 156
void set_handler(int sig, void (*handler)(int), int flags, ...)
{
	struct sigaction action;
	va_list ap;
157
	sigset_t sig_mask;
158 159
	int mask;

160 161 162
	handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
	action.sa_handler = hard_handler;

163
	sigemptyset(&action.sa_mask);
164 165

	va_start(ap, flags);
J
Jeff Dike 已提交
166
	while ((mask = va_arg(ap, int)) != -1)
167 168
		sigaddset(&action.sa_mask, mask);
	va_end(ap);
169

170 171
	action.sa_flags = flags;
	action.sa_restorer = NULL;
J
Jeff Dike 已提交
172
	if (sigaction(sig, &action, NULL) < 0)
173 174 175 176
		panic("sigaction failed - errno = %d\n", errno);

	sigemptyset(&sig_mask);
	sigaddset(&sig_mask, sig);
J
Jeff Dike 已提交
177
	if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
178
		panic("sigprocmask failed - errno = %d\n", errno);
179 180 181 182 183 184 185 186
}

int change_sig(int signal, int on)
{
	sigset_t sigset, old;

	sigemptyset(&sigset);
	sigaddset(&sigset, signal);
187 188
	if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
		return -errno;
J
Jeff Dike 已提交
189
	return !sigismember(&old, signal);
190 191 192 193
}

void block_signals(void)
{
194
	signals_enabled = 0;
J
Jeff Dike 已提交
195 196
	/*
	 * This must return with signals disabled, so this barrier
J
Jeff Dike 已提交
197 198 199 200 201
	 * ensures that writes are flushed out before the return.
	 * This might matter if gcc figures out how to inline this and
	 * decides to shuffle this code into the caller.
	 */
	mb();
202 203 204 205
}

void unblock_signals(void)
{
206
	int save_pending;
207

J
Jeff Dike 已提交
208
	if (signals_enabled == 1)
209
		return;
210

J
Jeff Dike 已提交
211 212
	/*
	 * We loop because the IRQ handler returns with interrupts off.  So,
213 214 215
	 * interrupts may have arrived and we need to re-enable them and
	 * recheck pending.
	 */
J
Jeff Dike 已提交
216 217 218
	while(1) {
		/*
		 * Save and reset save_pending after enabling signals.  This
219 220 221 222
		 * way, pending won't be changed while we're reading it.
		 */
		signals_enabled = 1;

J
Jeff Dike 已提交
223 224
		/*
		 * Setting signals_enabled and reading pending must
J
Jeff Dike 已提交
225 226 227 228
		 * happen in this order.
		 */
		mb();

229
		save_pending = pending;
J
Jeff Dike 已提交
230 231 232
		if (save_pending == 0) {
			/*
			 * This must return with signals enabled, so
J
Jeff Dike 已提交
233 234 235 236 237 238 239
			 * this barrier ensures that writes are
			 * flushed out before the return.  This might
			 * matter if gcc figures out how to inline
			 * this (unlikely, given its size) and decides
			 * to shuffle this code into the caller.
			 */
			mb();
240
			return;
J
Jeff Dike 已提交
241
		}
242 243 244

		pending = 0;

J
Jeff Dike 已提交
245 246
		/*
		 * We have pending interrupts, so disable signals, as the
247 248 249 250 251 252
		 * handlers expect them off when they are called.  They will
		 * be enabled again above.
		 */

		signals_enabled = 0;

J
Jeff Dike 已提交
253 254
		/*
		 * Deal with SIGIO first because the alarm handler might
255 256 257
		 * schedule, leaving the pending SIGIO stranded until we come
		 * back here.
		 */
J
Jeff Dike 已提交
258
		if (save_pending & SIGIO_MASK)
J
Jeff Dike 已提交
259
			sig_handler_common_skas(SIGIO, NULL);
260

J
Jeff Dike 已提交
261
		if (save_pending & SIGVTALRM_MASK)
J
Jeff Dike 已提交
262
			real_alarm_handler(NULL);
263
	}
264 265 266 267
}

int get_signals(void)
{
268
	return signals_enabled;
269 270 271 272 273
}

int set_signals(int enable)
{
	int ret;
J
Jeff Dike 已提交
274
	if (signals_enabled == enable)
275
		return enable;
276

277
	ret = signals_enabled;
J
Jeff Dike 已提交
278
	if (enable)
279 280
		unblock_signals();
	else block_signals();
281

282
	return ret;
283
}