signal.c 6.4 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 "as-layout.h"
J
Jeff Dike 已提交
13
#include "kern_util.h"
14
#include "os.h"
J
Jeff Dike 已提交
15 16 17
#include "sysdep/barrier.h"
#include "sysdep/sigcontext.h"
#include "user.h"
L
Linus Torvalds 已提交
18

19 20 21
/* Copied from linux/compiler-gcc.h since we can't include it directly */
#define barrier() __asm__ __volatile__("": : :"memory")

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

32
static void sig_handler_common(int sig, struct sigcontext *sc)
J
Jeff Dike 已提交
33
{
34 35
	struct uml_pt_regs r;
	int save_errno = errno;
J
Jeff Dike 已提交
36

37
	r.is_user = 0;
J
Jeff Dike 已提交
38
	if (sig == SIGSEGV) {
39 40 41 42
		/* For segfaults, we want the data from the sigcontext. */
		copy_sc(&r, sc);
		GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
	}
J
Jeff Dike 已提交
43

44
	/* enable signals if sig isn't IRQ signal */
J
Jeff Dike 已提交
45 46 47
	if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
		unblock_signals();

48
	(*sig_info[sig])(sig, &r);
J
Jeff Dike 已提交
49 50 51 52

	errno = save_errno;
}

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

68
void sig_handler(int sig, struct sigcontext *sc)
L
Linus Torvalds 已提交
69
{
70 71 72
	int enabled;

	enabled = signals_enabled;
J
Jeff Dike 已提交
73
	if (!enabled && (sig == SIGIO)) {
J
Jeff Dike 已提交
74
		signals_pending |= SIGIO_MASK;
75 76 77 78 79
		return;
	}

	block_signals();

80
	sig_handler_common(sig, sc);
81 82

	set_signals(enabled);
L
Linus Torvalds 已提交
83 84
}

J
Jeff Dike 已提交
85
static void real_alarm_handler(struct sigcontext *sc)
L
Linus Torvalds 已提交
86
{
87
	struct uml_pt_regs regs;
J
Jeff Dike 已提交
88

J
Jeff Dike 已提交
89
	if (sc != NULL)
J
Jeff Dike 已提交
90
		copy_sc(&regs, sc);
91
	regs.is_user = 0;
J
Jeff Dike 已提交
92
	unblock_signals();
J
Jeff Dike 已提交
93
	timer_handler(SIGVTALRM, &regs);
94 95
}

96
void alarm_handler(int sig, struct sigcontext *sc)
97 98 99 100
{
	int enabled;

	enabled = signals_enabled;
J
Jeff Dike 已提交
101
	if (!signals_enabled) {
J
Jeff Dike 已提交
102
		signals_pending |= SIGVTALRM_MASK;
103 104 105 106 107
		return;
	}

	block_signals();

J
Jeff Dike 已提交
108
	real_alarm_handler(sc);
109
	set_signals(enabled);
L
Linus Torvalds 已提交
110 111
}

J
Jeff Dike 已提交
112 113 114
void timer_init(void)
{
	set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
J
Jeff Dike 已提交
115
		    SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
J
Jeff Dike 已提交
116 117
}

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

128 129
void (*handlers[_NSIG])(int sig, struct sigcontext *sc);

J
Jeff Dike 已提交
130 131
void handle_signal(int sig, struct sigcontext *sc)
{
J
Jeff Dike 已提交
132
	unsigned long pending = 1UL << sig;
J
Jeff Dike 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146

	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 已提交
147
		bail = to_irq_stack(&pending);
J
Jeff Dike 已提交
148
		if (bail)
J
Jeff Dike 已提交
149 150 151 152 153
			return;

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

J
Jeff Dike 已提交
154
		while ((sig = ffs(pending)) != 0){
J
Jeff Dike 已提交
155 156 157 158 159
			sig--;
			pending &= ~(1 << sig);
			(*handlers[sig])(sig, sc);
		}

J
Jeff Dike 已提交
160 161
		/*
		 * Again, pending comes back with a mask of signals
J
Jeff Dike 已提交
162 163 164 165
		 * 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 已提交
166
		if (!nested)
J
Jeff Dike 已提交
167
			pending = from_irq_stack(nested);
J
Jeff Dike 已提交
168
	} while (pending);
J
Jeff Dike 已提交
169 170
}

171 172
extern void hard_handler(int sig);

173 174 175 176
void set_handler(int sig, void (*handler)(int), int flags, ...)
{
	struct sigaction action;
	va_list ap;
177
	sigset_t sig_mask;
178 179
	int mask;

180 181 182
	handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
	action.sa_handler = hard_handler;

183
	sigemptyset(&action.sa_mask);
184 185

	va_start(ap, flags);
J
Jeff Dike 已提交
186
	while ((mask = va_arg(ap, int)) != -1)
187 188
		sigaddset(&action.sa_mask, mask);
	va_end(ap);
189

190 191 192
	if (sig == SIGSEGV)
		flags |= SA_NODEFER;

193 194
	action.sa_flags = flags;
	action.sa_restorer = NULL;
J
Jeff Dike 已提交
195
	if (sigaction(sig, &action, NULL) < 0)
196 197 198 199
		panic("sigaction failed - errno = %d\n", errno);

	sigemptyset(&sig_mask);
	sigaddset(&sig_mask, sig);
J
Jeff Dike 已提交
200
	if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
201
		panic("sigprocmask failed - errno = %d\n", errno);
202 203 204 205
}

int change_sig(int signal, int on)
{
J
Jeff Dike 已提交
206
	sigset_t sigset;
207 208 209

	sigemptyset(&sigset);
	sigaddset(&sigset, signal);
J
Jeff Dike 已提交
210
	if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
211
		return -errno;
J
Jeff Dike 已提交
212 213

	return 0;
214 215 216 217
}

void block_signals(void)
{
218
	signals_enabled = 0;
J
Jeff Dike 已提交
219 220
	/*
	 * This must return with signals disabled, so this barrier
J
Jeff Dike 已提交
221 222 223 224
	 * 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 已提交
225
	barrier();
226 227 228 229
}

void unblock_signals(void)
{
230
	int save_pending;
231

J
Jeff Dike 已提交
232
	if (signals_enabled == 1)
233
		return;
234

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

J
Jeff Dike 已提交
247
		/*
J
Jeff Dike 已提交
248
		 * Setting signals_enabled and reading signals_pending must
J
Jeff Dike 已提交
249 250
		 * happen in this order.
		 */
J
Jeff Dike 已提交
251
		barrier();
J
Jeff Dike 已提交
252

J
Jeff Dike 已提交
253
		save_pending = signals_pending;
J
Jeff Dike 已提交
254
		if (save_pending == 0)
255 256
			return;

J
Jeff Dike 已提交
257
		signals_pending = 0;
258

J
Jeff Dike 已提交
259 260
		/*
		 * We have pending interrupts, so disable signals, as the
261 262 263 264 265 266
		 * handlers expect them off when they are called.  They will
		 * be enabled again above.
		 */

		signals_enabled = 0;

J
Jeff Dike 已提交
267 268
		/*
		 * Deal with SIGIO first because the alarm handler might
269 270 271
		 * schedule, leaving the pending SIGIO stranded until we come
		 * back here.
		 */
J
Jeff Dike 已提交
272
		if (save_pending & SIGIO_MASK)
273
			sig_handler_common(SIGIO, NULL);
274

J
Jeff Dike 已提交
275
		if (save_pending & SIGVTALRM_MASK)
J
Jeff Dike 已提交
276
			real_alarm_handler(NULL);
277
	}
278 279 280 281
}

int get_signals(void)
{
282
	return signals_enabled;
283 284 285 286 287
}

int set_signals(int enable)
{
	int ret;
J
Jeff Dike 已提交
288
	if (signals_enabled == enable)
289
		return enable;
290

291
	ret = signals_enabled;
J
Jeff Dike 已提交
292
	if (enable)
293 294
		unblock_signals();
	else block_signals();
295

296
	return ret;
297
}