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 115 116 117 118
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 已提交
119
	if (sigaltstack(&stack, NULL) != 0)
120 121 122
		panic("enabling signal stack failed, errno = %d\n", errno);
}

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

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

135

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

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

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

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

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

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

185
	action.sa_sigaction = hard_handler;
186

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

193 194 195
	if (sig == SIGSEGV)
		flags |= SA_NODEFER;

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

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

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

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

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

	return 0;
220 221 222 223
}

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

void unblock_signals(void)
{
236
	int save_pending;
237

J
Jeff Dike 已提交
238
	if (signals_enabled == 1)
239
		return;
240

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

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

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

J
Jeff Dike 已提交
263
		signals_pending = 0;
264

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

		signals_enabled = 0;

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

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

int get_signals(void)
{
291
	return signals_enabled;
292 293 294 295 296
}

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

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

305
	return ret;
306
}