signal.c 6.5 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
#include "process.h"
J
Jeff Dike 已提交
16 17
#include "sysdep/barrier.h"
#include "sysdep/sigcontext.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,
115
		    SA_ONSTACK | SA_RESTART, 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);
}

W
WANG Cong 已提交
128
static void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
129

130
static void handle_signal(int sig, struct sigcontext *sc)
J
Jeff Dike 已提交
131
{
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 173 174 175
static void hard_handler(int sig, siginfo_t *info, void *p)
{
	struct ucontext *uc = p;
	handle_signal(sig, (struct sigcontext *) &uc->uc_mcontext);
}
176

177 178 179 180
void set_handler(int sig, void (*handler)(int), int flags, ...)
{
	struct sigaction action;
	va_list ap;
181
	sigset_t sig_mask;
182 183
	int mask;

184
	handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
185
	action.sa_sigaction = hard_handler;
186

187
	sigemptyset(&action.sa_mask);
188 189

	va_start(ap, flags);
J
Jeff Dike 已提交
190
	while ((mask = va_arg(ap, int)) != -1)
191 192
		sigaddset(&action.sa_mask, mask);
	va_end(ap);
193

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

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

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

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

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

	return 0;
218 219 220 221
}

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

void unblock_signals(void)
{
234
	int save_pending;
235

J
Jeff Dike 已提交
236
	if (signals_enabled == 1)
237
		return;
238

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

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

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

J
Jeff Dike 已提交
261
		signals_pending = 0;
262

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

		signals_enabled = 0;

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

J
Jeff Dike 已提交
279
		if (save_pending & SIGVTALRM_MASK)
J
Jeff Dike 已提交
280
			real_alarm_handler(NULL);
281
	}
282 283 284 285
}

int get_signals(void)
{
286
	return signals_enabled;
287 288 289 290 291
}

int set_signals(int enable)
{
	int ret;
J
Jeff Dike 已提交
292
	if (signals_enabled == enable)
293
		return enable;
294

295
	ret = signals_enabled;
J
Jeff Dike 已提交
296
	if (enable)
297 298
		unblock_signals();
	else block_signals();
299

300
	return ret;
301
}