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
#include "process.h"
J
Jeff Dike 已提交
16 17 18
#include "sysdep/barrier.h"
#include "sysdep/sigcontext.h"
#include "user.h"
L
Linus Torvalds 已提交
19

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

J
Jeff Dike 已提交
23 24 25 26 27 28 29 30 31 32
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 };

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

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

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

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

	errno = save_errno;
}

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

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

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

	block_signals();

81
	sig_handler_common(sig, sc);
82 83

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

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

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

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

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

	block_signals();

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

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

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

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

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

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

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

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

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

172 173
extern void hard_handler(int sig);

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

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

184
	sigemptyset(&action.sa_mask);
185 186

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

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

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

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

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

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

	return 0;
215 216 217 218
}

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

void unblock_signals(void)
{
231
	int save_pending;
232

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

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

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

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

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

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

		signals_enabled = 0;

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

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

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

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

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

297
	return ret;
298
}