signal.c 5.83 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
/*
 * Copyright (C) 2004 PathScale, Inc
Jeff Dike's avatar
Jeff Dike committed
3
 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds's avatar
Linus Torvalds committed
4
5
6
 * Licensed under the GPL
 */

7
8
#include <stdlib.h>
#include <stdarg.h>
Jeff Dike's avatar
Jeff Dike committed
9
10
11
#include <errno.h>
#include <signal.h>
#include <strings.h>
Jeff Dike's avatar
Jeff Dike committed
12
#include "kern_util.h"
13
#include "os.h"
Jeff Dike's avatar
Jeff Dike committed
14
15
16
#include "sysdep/barrier.h"
#include "sysdep/sigcontext.h"
#include "user.h"
Linus Torvalds's avatar
Linus Torvalds committed
17

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

Jeff Dike's avatar
Jeff Dike committed
21
/*
Jeff Dike's avatar
Jeff Dike committed
22
 * These are the asynchronous signals.  SIGPROF is excluded because we want to
23
24
25
26
27
28
29
30
31
32
 * 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)

Jeff Dike's avatar
Jeff Dike committed
33
34
static int signals_enabled;
static unsigned int pending;
35

36
void sig_handler(int sig, struct sigcontext *sc)
Linus Torvalds's avatar
Linus Torvalds committed
37
{
38
39
40
	int enabled;

	enabled = signals_enabled;
Jeff Dike's avatar
Jeff Dike committed
41
	if (!enabled && (sig == SIGIO)) {
42
43
44
45
46
47
		pending |= SIGIO_MASK;
		return;
	}

	block_signals();

Jeff Dike's avatar
Jeff Dike committed
48
	sig_handler_common_skas(sig, sc);
49
50

	set_signals(enabled);
Linus Torvalds's avatar
Linus Torvalds committed
51
52
}

Jeff Dike's avatar
Jeff Dike committed
53
static void real_alarm_handler(struct sigcontext *sc)
Linus Torvalds's avatar
Linus Torvalds committed
54
{
55
	struct uml_pt_regs regs;
Jeff Dike's avatar
Jeff Dike committed
56

Jeff Dike's avatar
Jeff Dike committed
57
	if (sc != NULL)
Jeff Dike's avatar
Jeff Dike committed
58
		copy_sc(&regs, sc);
59
	regs.is_user = 0;
Jeff Dike's avatar
Jeff Dike committed
60
	unblock_signals();
Jeff Dike's avatar
Jeff Dike committed
61
	timer_handler(SIGVTALRM, &regs);
62
63
}

64
void alarm_handler(int sig, struct sigcontext *sc)
65
66
67
68
{
	int enabled;

	enabled = signals_enabled;
Jeff Dike's avatar
Jeff Dike committed
69
	if (!signals_enabled) {
Jeff Dike's avatar
Jeff Dike committed
70
		pending |= SIGVTALRM_MASK;
71
72
73
74
75
		return;
	}

	block_signals();

Jeff Dike's avatar
Jeff Dike committed
76
	real_alarm_handler(sc);
77
	set_signals(enabled);
Linus Torvalds's avatar
Linus Torvalds committed
78
79
}

Jeff Dike's avatar
Jeff Dike committed
80
81
82
void timer_init(void)
{
	set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
Jeff Dike's avatar
Jeff Dike committed
83
		    SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
Jeff Dike's avatar
Jeff Dike committed
84
85
}

86
87
88
89
90
91
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 *) });

Jeff Dike's avatar
Jeff Dike committed
92
	if (sigaltstack(&stack, NULL) != 0)
93
94
95
96
97
98
99
100
101
		panic("enabling signal stack failed, errno = %d\n", errno);
}

void remove_sigstack(void)
{
	stack_t stack = ((stack_t) { .ss_flags	= SS_DISABLE,
				     .ss_sp	= NULL,
				     .ss_size	= 0 });

Jeff Dike's avatar
Jeff Dike committed
102
	if (sigaltstack(&stack, NULL) != 0)
103
104
105
		panic("disabling signal stack failed, errno = %d\n", errno);
}

106
107
void (*handlers[_NSIG])(int sig, struct sigcontext *sc);

Jeff Dike's avatar
Jeff Dike committed
108
109
void handle_signal(int sig, struct sigcontext *sc)
{
Jeff Dike's avatar
Jeff Dike committed
110
	unsigned long pending = 1UL << sig;
Jeff Dike's avatar
Jeff Dike committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124

	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.
		 */
Jeff Dike's avatar
Jeff Dike committed
125
		bail = to_irq_stack(&pending);
Jeff Dike's avatar
Jeff Dike committed
126
		if (bail)
Jeff Dike's avatar
Jeff Dike committed
127
128
129
130
131
			return;

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

Jeff Dike's avatar
Jeff Dike committed
132
		while ((sig = ffs(pending)) != 0){
Jeff Dike's avatar
Jeff Dike committed
133
134
135
136
137
			sig--;
			pending &= ~(1 << sig);
			(*handlers[sig])(sig, sc);
		}

Jeff Dike's avatar
Jeff Dike committed
138
139
		/*
		 * Again, pending comes back with a mask of signals
Jeff Dike's avatar
Jeff Dike committed
140
141
142
143
		 * 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.
		 */
Jeff Dike's avatar
Jeff Dike committed
144
		if (!nested)
Jeff Dike's avatar
Jeff Dike committed
145
			pending = from_irq_stack(nested);
Jeff Dike's avatar
Jeff Dike committed
146
	} while (pending);
Jeff Dike's avatar
Jeff Dike committed
147
148
}

149
150
extern void hard_handler(int sig);

151
152
153
154
void set_handler(int sig, void (*handler)(int), int flags, ...)
{
	struct sigaction action;
	va_list ap;
155
	sigset_t sig_mask;
156
157
	int mask;

158
159
160
	handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
	action.sa_handler = hard_handler;

161
	sigemptyset(&action.sa_mask);
162
163

	va_start(ap, flags);
Jeff Dike's avatar
Jeff Dike committed
164
	while ((mask = va_arg(ap, int)) != -1)
165
166
		sigaddset(&action.sa_mask, mask);
	va_end(ap);
167

168
169
	action.sa_flags = flags;
	action.sa_restorer = NULL;
Jeff Dike's avatar
Jeff Dike committed
170
	if (sigaction(sig, &action, NULL) < 0)
171
172
173
174
		panic("sigaction failed - errno = %d\n", errno);

	sigemptyset(&sig_mask);
	sigaddset(&sig_mask, sig);
Jeff Dike's avatar
Jeff Dike committed
175
	if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
176
		panic("sigprocmask failed - errno = %d\n", errno);
177
178
179
180
181
182
183
184
}

int change_sig(int signal, int on)
{
	sigset_t sigset, old;

	sigemptyset(&sigset);
	sigaddset(&sigset, signal);
185
186
	if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
		return -errno;
Jeff Dike's avatar
Jeff Dike committed
187
	return !sigismember(&old, signal);
188
189
190
191
}

void block_signals(void)
{
192
	signals_enabled = 0;
Jeff Dike's avatar
Jeff Dike committed
193
194
	/*
	 * This must return with signals disabled, so this barrier
Jeff Dike's avatar
Jeff Dike committed
195
196
197
198
	 * 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.
	 */
Jeff Dike's avatar
Jeff Dike committed
199
	barrier();
200
201
202
203
}

void unblock_signals(void)
{
204
	int save_pending;
205

Jeff Dike's avatar
Jeff Dike committed
206
	if (signals_enabled == 1)
207
		return;
208

Jeff Dike's avatar
Jeff Dike committed
209
210
	/*
	 * We loop because the IRQ handler returns with interrupts off.  So,
211
212
213
	 * interrupts may have arrived and we need to re-enable them and
	 * recheck pending.
	 */
Jeff Dike's avatar
Jeff Dike committed
214
215
216
	while(1) {
		/*
		 * Save and reset save_pending after enabling signals.  This
217
218
219
220
		 * way, pending won't be changed while we're reading it.
		 */
		signals_enabled = 1;

Jeff Dike's avatar
Jeff Dike committed
221
222
		/*
		 * Setting signals_enabled and reading pending must
Jeff Dike's avatar
Jeff Dike committed
223
224
		 * happen in this order.
		 */
Jeff Dike's avatar
Jeff Dike committed
225
		barrier();
Jeff Dike's avatar
Jeff Dike committed
226

227
		save_pending = pending;
Jeff Dike's avatar
Jeff Dike committed
228
		if (save_pending == 0)
229
230
231
232
			return;

		pending = 0;

Jeff Dike's avatar
Jeff Dike committed
233
234
		/*
		 * We have pending interrupts, so disable signals, as the
235
236
237
238
239
240
		 * handlers expect them off when they are called.  They will
		 * be enabled again above.
		 */

		signals_enabled = 0;

Jeff Dike's avatar
Jeff Dike committed
241
242
		/*
		 * Deal with SIGIO first because the alarm handler might
243
244
245
		 * schedule, leaving the pending SIGIO stranded until we come
		 * back here.
		 */
Jeff Dike's avatar
Jeff Dike committed
246
		if (save_pending & SIGIO_MASK)
Jeff Dike's avatar
Jeff Dike committed
247
			sig_handler_common_skas(SIGIO, NULL);
248

Jeff Dike's avatar
Jeff Dike committed
249
		if (save_pending & SIGVTALRM_MASK)
Jeff Dike's avatar
Jeff Dike committed
250
			real_alarm_handler(NULL);
251
	}
252
253
254
255
}

int get_signals(void)
{
256
	return signals_enabled;
257
258
259
260
261
}

int set_signals(int enable)
{
	int ret;
Jeff Dike's avatar
Jeff Dike committed
262
	if (signals_enabled == enable)
263
		return enable;
264

265
	ret = signals_enabled;
Jeff Dike's avatar
Jeff Dike committed
266
	if (enable)
267
268
		unblock_signals();
	else block_signals();
269

270
	return ret;
271
}