1 
2 /*
3  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Matthew Dillon <dillon@backplane.com>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "opt_ddb.h"
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/reboot.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <ddb/ddb.h>
43 
44 #include <sys/thread2.h>
45 #include <sys/lwp.h>
46 
47 #include <machine/trap.h>
48 #include <machine/md_var.h>
49 #include <machine/segments.h>
50 #include <machine/cpu.h>
51 #include <machine/smp.h>
52 
53 #include <err.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <unistd.h>
57 
58 int _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
59 int _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
60 
61 static void exc_segfault(int signo, siginfo_t *info, void *ctx);
62 #ifdef DDB
63 static void exc_debugger(int signo, siginfo_t *info, void *ctx);
64 #endif
65 
66 /*
67  * IPIs are 'fast' interrupts, so we deal with them directly from our
68  * signal handler.
69  *
70  * WARNING: Signals are not physically disabled here so we have to enter
71  * our critical section before bumping gd_intr_nesting_level or another
72  * interrupt can come along and get really confused.
73  */
74 static
75 void
76 ipisig(int nada, siginfo_t *info, void *ctxp)
77 {
78 	globaldata_t gd = mycpu;
79 	thread_t td = gd->gd_curthread;
80 	int save;
81 
82 	save = errno;
83 	if (td->td_critcount == 0) {
84 		++td->td_critcount;
85 		++gd->gd_cnt.v_ipi;
86 		++gd->gd_intr_nesting_level;
87 		atomic_swap_int(&gd->gd_npoll, 0);
88 		lwkt_process_ipiq();
89 		--gd->gd_intr_nesting_level;
90 		--td->td_critcount;
91 	} else {
92 		need_ipiq();
93 	}
94 	errno = save;
95 }
96 
97 /*
98  * Unconditionally stop or restart a cpu.
99  *
100  * Note: cpu_mask_all_signals() masks all signals except SIGXCPU itself.
101  * SIGXCPU itself is blocked on entry to stopsig() by the signal handler
102  * itself.
103  *
104  * WARNING: Signals are not physically disabled here so we have to enter
105  * our critical section before bumping gd_intr_nesting_level or another
106  * interrupt can come along and get really confused.
107  */
108 static
109 void
110 stopsig(int nada, siginfo_t *info, void *ctxp)
111 {
112 	globaldata_t gd = mycpu;
113 	thread_t td = gd->gd_curthread;
114 	sigset_t ss;
115 	int save;
116 
117 	save = errno;
118 	sigemptyset(&ss);
119 	sigaddset(&ss, SIGALRM);
120 	sigaddset(&ss, SIGIO);
121 	sigaddset(&ss, SIGURG);
122 	sigaddset(&ss, SIGQUIT);
123 	sigaddset(&ss, SIGUSR1);
124 	sigaddset(&ss, SIGUSR2);
125 	sigaddset(&ss, SIGTERM);
126 	sigaddset(&ss, SIGWINCH);
127 
128 	++td->td_critcount;
129 	++gd->gd_intr_nesting_level;
130 	while (CPUMASK_TESTMASK(stopped_cpus, gd->gd_cpumask)) {
131 		sigsuspend(&ss);
132 	}
133 	--gd->gd_intr_nesting_level;
134 	--td->td_critcount;
135 
136 	errno = save;
137 }
138 
139 /*
140  * SIGIO is used by cothreads to signal back into the virtual kernel.
141  */
142 static
143 void
144 kqueuesig(int nada, siginfo_t *info, void *ctxp)
145 {
146 	globaldata_t gd = mycpu;
147 	thread_t td = gd->gd_curthread;
148 	int save;
149 
150 	save = errno;
151 	if (td->td_critcount == 0) {
152 		++td->td_critcount;
153 		++gd->gd_intr_nesting_level;
154 		kqueue_intr(NULL);
155 		--gd->gd_intr_nesting_level;
156 		--td->td_critcount;
157 	} else {
158 		need_kqueue();
159 	}
160 	errno = save;
161 }
162 
163 static
164 void
165 timersig(int nada, siginfo_t *info, void *ctxp)
166 {
167 	globaldata_t gd = mycpu;
168 	thread_t td = gd->gd_curthread;
169 	int save;
170 
171 	save = errno;
172 	if (td->td_critcount == 0) {
173 		++td->td_critcount;
174 		++gd->gd_intr_nesting_level;
175 		vktimer_intr(NULL);
176 		--gd->gd_intr_nesting_level;
177 		--td->td_critcount;
178 	} else {
179 		need_timer();
180 	}
181 	errno = save;
182 }
183 
184 static
185 void
186 cosig(int nada, siginfo_t *info, void *ctxp)
187 {
188 	int save;
189 
190 	save = errno;
191 	/* handles critical section checks */
192 	signalintr(1);
193 	errno = save;
194 }
195 
196 static
197 void
198 infosig(int nada, siginfo_t *info, void *ctxp)
199 {
200 	ucontext_t *ctx = ctxp;
201 	char buf[256];
202 	int save;
203 
204 	save = errno;
205 	snprintf(buf, sizeof(buf), "lwp %d pc=%p sp=%p\n",
206 		(lwpid_t)lwp_gettid(),
207 		(void *)(intptr_t)ctx->uc_mcontext.mc_rip,
208 		(void *)(intptr_t)ctx->uc_mcontext.mc_rsp);
209 	write(2, buf, strlen(buf));
210 	errno = save;
211 }
212 
213 void
214 init_exceptions(void)
215 {
216 	struct sigaction sa;
217 
218 	bzero(&sa, sizeof(sa));
219 	sa.sa_sigaction = exc_segfault;
220 	sa.sa_flags |= SA_SIGINFO | SA_NODEFER;
221 	sigemptyset(&sa.sa_mask);
222 	sigaction(SIGBUS, &sa, NULL);
223 	sigaction(SIGSEGV, &sa, NULL);
224 	sigaction(SIGTRAP, &sa, NULL);
225 	sigaction(SIGFPE, &sa, NULL);
226 
227 	sa.sa_flags &= ~SA_NODEFER;
228 
229 #ifdef DDB
230 	sa.sa_sigaction = exc_debugger;
231 	sigaction(SIGQUIT, &sa, NULL);
232 #endif
233 	sa.sa_sigaction = ipisig;
234 	sigaction(SIGUSR1, &sa, NULL);
235 
236 	sa.sa_sigaction = stopsig;
237 	sigaction(SIGXCPU, &sa, NULL);
238 
239 	sa.sa_sigaction = kqueuesig;
240 	sigaction(SIGIO, &sa, NULL);
241 
242 	sa.sa_sigaction = timersig;
243 	sigaction(SIGURG, &sa, NULL);
244 
245 	sa.sa_sigaction = cosig;
246 	sigaction(SIGALRM, &sa, NULL);
247 
248 	sa.sa_sigaction = infosig;
249 	sigaction(SIGINFO, &sa, NULL);
250 }
251 
252 /*
253  * This function handles a segmentation fault.
254  *
255  * XXX We assume that trapframe is a subset of ucontext.  It is as of
256  *     this writing.
257  */
258 static void
259 exc_segfault(int signo, siginfo_t *info, void *ctxp)
260 {
261 	ucontext_t *ctx = ctxp;
262 	int save;
263 
264 	save = errno;
265 #if 0
266 	kprintf("CAUGHT SIG %d RIP %08lx ERR %08lx TRAPNO %ld "
267 		"err %ld addr %08lx\n",
268 		signo,
269 		ctx->uc_mcontext.mc_rip,
270 		ctx->uc_mcontext.mc_err,
271 		ctx->uc_mcontext.mc_trapno & 0xFFFF,
272 		ctx->uc_mcontext.mc_trapno >> 16,
273 		ctx->uc_mcontext.mc_addr);
274 #endif
275 	kern_trap((struct trapframe *)&ctx->uc_mcontext.mc_rdi);
276 	splz();
277 	errno = save;
278 }
279 
280 #ifdef DDB
281 
282 static void
283 exc_debugger(int signo, siginfo_t *info, void *ctxp)
284 {
285 	ucontext_t *ctx = ctxp;
286 	int save;
287 
288 	save = errno;
289 	kprintf("CAUGHT SIG %d RIP %08lx RSP %08lx TD %p\n",
290 		signo,
291 		ctx->uc_mcontext.mc_rip,
292 		ctx->uc_mcontext.mc_rsp,
293 		curthread);
294 	Debugger("interrupt from console");
295 	errno = save;
296 }
297 
298 #endif
299