signal.c 6.7 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>
12 13 14 15
#include <as-layout.h>
#include <kern_util.h>
#include <os.h>
#include <sysdep/mcontext.h>
16
#include "internal.h"
L
Linus Torvalds 已提交
17

18
void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
J
Jeff Dike 已提交
19 20 21 22 23 24 25 26 27
	[SIGTRAP]	= relay_signal,
	[SIGFPE]	= relay_signal,
	[SIGILL]	= relay_signal,
	[SIGWINCH]	= winch,
	[SIGBUS]	= bus_handler,
	[SIGSEGV]	= segv_handler,
	[SIGIO]		= sigio_handler,
	[SIGVTALRM]	= timer_handler };

R
Richard Weinberger 已提交
28
static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
J
Jeff Dike 已提交
29
{
30 31
	struct uml_pt_regs r;
	int save_errno = errno;
J
Jeff Dike 已提交
32

33
	r.is_user = 0;
J
Jeff Dike 已提交
34
	if (sig == SIGSEGV) {
35
		/* For segfaults, we want the data from the sigcontext. */
A
Al Viro 已提交
36
		get_regs_from_mc(&r, mc);
37
		GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
38
	}
J
Jeff Dike 已提交
39

40
	/* enable signals if sig isn't IRQ signal */
J
Jeff Dike 已提交
41 42 43
	if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
		unblock_signals();

44
	(*sig_info[sig])(sig, si, &r);
J
Jeff Dike 已提交
45 46 47 48

	errno = save_errno;
}

J
Jeff Dike 已提交
49
/*
J
Jeff Dike 已提交
50
 * These are the asynchronous signals.  SIGPROF is excluded because we want to
51 52 53 54 55 56 57 58 59 60
 * 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 已提交
61
static int signals_enabled;
J
Jeff Dike 已提交
62
static unsigned int signals_pending;
63

R
Richard Weinberger 已提交
64
void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
L
Linus Torvalds 已提交
65
{
66 67 68
	int enabled;

	enabled = signals_enabled;
J
Jeff Dike 已提交
69
	if (!enabled && (sig == SIGIO)) {
J
Jeff Dike 已提交
70
		signals_pending |= SIGIO_MASK;
71 72 73 74 75
		return;
	}

	block_signals();

76
	sig_handler_common(sig, si, mc);
77 78

	set_signals(enabled);
L
Linus Torvalds 已提交
79 80
}

81
static void real_alarm_handler(mcontext_t *mc)
L
Linus Torvalds 已提交
82
{
83
	struct uml_pt_regs regs;
J
Jeff Dike 已提交
84

85
	if (mc != NULL)
A
Al Viro 已提交
86
		get_regs_from_mc(&regs, mc);
87
	regs.is_user = 0;
J
Jeff Dike 已提交
88
	unblock_signals();
89
	timer_handler(SIGVTALRM, NULL, &regs);
90 91
}

92
void alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
93 94 95 96
{
	int enabled;

	enabled = signals_enabled;
J
Jeff Dike 已提交
97
	if (!signals_enabled) {
J
Jeff Dike 已提交
98
		signals_pending |= SIGVTALRM_MASK;
99 100 101 102 103
		return;
	}

	block_signals();

104
	real_alarm_handler(mc);
105
	set_signals(enabled);
L
Linus Torvalds 已提交
106 107
}

J
Jeff Dike 已提交
108 109
void timer_init(void)
{
110
	set_handler(SIGVTALRM);
J
Jeff Dike 已提交
111 112
}

113 114
void set_sigstack(void *sig_stack, int size)
{
115 116 117 118 119
	stack_t stack = {
		.ss_flags = 0,
		.ss_sp = sig_stack,
		.ss_size = size - sizeof(void *)
	};
120

J
Jeff Dike 已提交
121
	if (sigaltstack(&stack, NULL) != 0)
122 123 124
		panic("enabling signal stack failed, errno = %d\n", errno);
}

R
Richard Weinberger 已提交
125
static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
126 127 128 129 130 131 132 133 134 135
	[SIGSEGV] = sig_handler,
	[SIGBUS] = sig_handler,
	[SIGILL] = sig_handler,
	[SIGFPE] = sig_handler,
	[SIGTRAP] = sig_handler,

	[SIGIO] = sig_handler,
	[SIGWINCH] = sig_handler,
	[SIGVTALRM] = alarm_handler
};
136

137

138
static void hard_handler(int sig, siginfo_t *si, void *p)
J
Jeff Dike 已提交
139
{
140 141
	struct ucontext *uc = p;
	mcontext_t *mc = &uc->uc_mcontext;
J
Jeff Dike 已提交
142
	unsigned long pending = 1UL << sig;
J
Jeff Dike 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156

	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 已提交
157
		bail = to_irq_stack(&pending);
J
Jeff Dike 已提交
158
		if (bail)
J
Jeff Dike 已提交
159 160 161 162 163
			return;

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

J
Jeff Dike 已提交
164
		while ((sig = ffs(pending)) != 0){
J
Jeff Dike 已提交
165 166
			sig--;
			pending &= ~(1 << sig);
R
Richard Weinberger 已提交
167
			(*handlers[sig])(sig, (struct siginfo *)si, mc);
J
Jeff Dike 已提交
168 169
		}

J
Jeff Dike 已提交
170 171
		/*
		 * Again, pending comes back with a mask of signals
J
Jeff Dike 已提交
172 173 174 175
		 * 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 已提交
176
		if (!nested)
J
Jeff Dike 已提交
177
			pending = from_irq_stack(nested);
J
Jeff Dike 已提交
178
	} while (pending);
J
Jeff Dike 已提交
179 180
}

181
void set_handler(int sig)
182 183
{
	struct sigaction action;
A
Al Viro 已提交
184
	int flags = SA_SIGINFO | SA_ONSTACK;
185
	sigset_t sig_mask;
186

187
	action.sa_sigaction = hard_handler;
188

A
Al Viro 已提交
189
	/* block irq ones */
190
	sigemptyset(&action.sa_mask);
A
Al Viro 已提交
191 192 193
	sigaddset(&action.sa_mask, SIGVTALRM);
	sigaddset(&action.sa_mask, SIGIO);
	sigaddset(&action.sa_mask, SIGWINCH);
194

195 196 197
	if (sig == SIGSEGV)
		flags |= SA_NODEFER;

A
Al Viro 已提交
198 199 200 201
	if (sigismember(&action.sa_mask, sig))
		flags |= SA_RESTART; /* if it's an irq signal */

	action.sa_flags = flags;
202
	action.sa_restorer = NULL;
J
Jeff Dike 已提交
203
	if (sigaction(sig, &action, NULL) < 0)
204 205 206 207
		panic("sigaction failed - errno = %d\n", errno);

	sigemptyset(&sig_mask);
	sigaddset(&sig_mask, sig);
J
Jeff Dike 已提交
208
	if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
209
		panic("sigprocmask failed - errno = %d\n", errno);
210 211 212 213
}

int change_sig(int signal, int on)
{
J
Jeff Dike 已提交
214
	sigset_t sigset;
215 216 217

	sigemptyset(&sigset);
	sigaddset(&sigset, signal);
J
Jeff Dike 已提交
218
	if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
219
		return -errno;
J
Jeff Dike 已提交
220 221

	return 0;
222 223 224 225
}

void block_signals(void)
{
226
	signals_enabled = 0;
J
Jeff Dike 已提交
227 228
	/*
	 * This must return with signals disabled, so this barrier
J
Jeff Dike 已提交
229 230 231 232
	 * 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.
	 */
J
Jeff Dike 已提交
233
	barrier();
234 235 236 237
}

void unblock_signals(void)
{
238
	int save_pending;
239

J
Jeff Dike 已提交
240
	if (signals_enabled == 1)
241
		return;
242

J
Jeff Dike 已提交
243 244
	/*
	 * We loop because the IRQ handler returns with interrupts off.  So,
245
	 * interrupts may have arrived and we need to re-enable them and
J
Jeff Dike 已提交
246
	 * recheck signals_pending.
247
	 */
J
Jeff Dike 已提交
248
	while (1) {
J
Jeff Dike 已提交
249 250
		/*
		 * Save and reset save_pending after enabling signals.  This
J
Jeff Dike 已提交
251
		 * way, signals_pending won't be changed while we're reading it.
252 253 254
		 */
		signals_enabled = 1;

J
Jeff Dike 已提交
255
		/*
J
Jeff Dike 已提交
256
		 * Setting signals_enabled and reading signals_pending must
J
Jeff Dike 已提交
257 258
		 * happen in this order.
		 */
J
Jeff Dike 已提交
259
		barrier();
J
Jeff Dike 已提交
260

J
Jeff Dike 已提交
261
		save_pending = signals_pending;
J
Jeff Dike 已提交
262
		if (save_pending == 0)
263 264
			return;

J
Jeff Dike 已提交
265
		signals_pending = 0;
266

J
Jeff Dike 已提交
267 268
		/*
		 * We have pending interrupts, so disable signals, as the
269 270 271 272 273 274
		 * handlers expect them off when they are called.  They will
		 * be enabled again above.
		 */

		signals_enabled = 0;

J
Jeff Dike 已提交
275 276
		/*
		 * Deal with SIGIO first because the alarm handler might
277 278
		 * schedule, leaving the pending SIGIO stranded until we come
		 * back here.
279 280 281
		 *
		 * SIGIO's handler doesn't use siginfo or mcontext,
		 * so they can be NULL.
282
		 */
J
Jeff Dike 已提交
283
		if (save_pending & SIGIO_MASK)
284
			sig_handler_common(SIGIO, NULL, NULL);
285

J
Jeff Dike 已提交
286
		if (save_pending & SIGVTALRM_MASK)
J
Jeff Dike 已提交
287
			real_alarm_handler(NULL);
288
	}
289 290 291 292
}

int get_signals(void)
{
293
	return signals_enabled;
294 295 296 297 298
}

int set_signals(int enable)
{
	int ret;
J
Jeff Dike 已提交
299
	if (signals_enabled == enable)
300
		return enable;
301

302
	ret = signals_enabled;
J
Jeff Dike 已提交
303
	if (enable)
304 305
		unblock_signals();
	else block_signals();
306

307
	return ret;
308
}
309 310 311 312 313 314 315 316

int os_is_signal_stack(void)
{
	stack_t ss;
	sigaltstack(NULL, &ss);

	return ss.ss_flags & SS_ONSTACK;
}