xref: /dflybsd-src/sys/platform/vkernel64/x86_64/exception.c (revision 0087561d6d4d84b8ac1a312cc720339cbf66781d)
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 
81 	if (td->td_critcount == 0) {
82 		++td->td_critcount;
83 		++gd->gd_cnt.v_ipi;
84 		++gd->gd_intr_nesting_level;
85 		atomic_swap_int(&gd->gd_npoll, 0);
86 		lwkt_process_ipiq();
87 		--gd->gd_intr_nesting_level;
88 		--td->td_critcount;
89 	} else {
90 		need_ipiq();
91 	}
92 }
93 
94 /*
95  * Unconditionally stop or restart a cpu.
96  *
97  * Note: cpu_mask_all_signals() masks all signals except SIGXCPU itself.
98  * SIGXCPU itself is blocked on entry to stopsig() by the signal handler
99  * itself.
100  *
101  * WARNING: Signals are not physically disabled here so we have to enter
102  * our critical section before bumping gd_intr_nesting_level or another
103  * interrupt can come along and get really confused.
104  */
105 static
106 void
107 stopsig(int nada, siginfo_t *info, void *ctxp)
108 {
109 	globaldata_t gd = mycpu;
110 	thread_t td = gd->gd_curthread;
111 	sigset_t ss;
112 
113 	sigemptyset(&ss);
114 	sigaddset(&ss, SIGALRM);
115 	sigaddset(&ss, SIGIO);
116 	sigaddset(&ss, SIGURG);
117 	sigaddset(&ss, SIGQUIT);
118 	sigaddset(&ss, SIGUSR1);
119 	sigaddset(&ss, SIGUSR2);
120 	sigaddset(&ss, SIGTERM);
121 	sigaddset(&ss, SIGWINCH);
122 
123 	++td->td_critcount;
124 	++gd->gd_intr_nesting_level;
125 	while (CPUMASK_TESTMASK(stopped_cpus, gd->gd_cpumask)) {
126 		sigsuspend(&ss);
127 	}
128 	--gd->gd_intr_nesting_level;
129 	--td->td_critcount;
130 }
131 
132 /*
133  * SIGIO is used by cothreads to signal back into the virtual kernel.
134  */
135 static
136 void
137 kqueuesig(int nada, siginfo_t *info, void *ctxp)
138 {
139 	globaldata_t gd = mycpu;
140 	thread_t td = gd->gd_curthread;
141 
142 	if (td->td_critcount == 0) {
143 		++td->td_critcount;
144 		++gd->gd_intr_nesting_level;
145 		kqueue_intr(NULL);
146 		--gd->gd_intr_nesting_level;
147 		--td->td_critcount;
148 	} else {
149 		need_kqueue();
150 	}
151 }
152 
153 static
154 void
155 timersig(int nada, siginfo_t *info, void *ctxp)
156 {
157 	globaldata_t gd = mycpu;
158 	thread_t td = gd->gd_curthread;
159 
160 	if (td->td_critcount == 0) {
161 		++td->td_critcount;
162 		++gd->gd_intr_nesting_level;
163 		vktimer_intr(NULL);
164 		--gd->gd_intr_nesting_level;
165 		--td->td_critcount;
166 	} else {
167 		need_timer();
168 	}
169 }
170 
171 static
172 void
173 cosig(int nada, siginfo_t *info, void *ctxp)
174 {
175 	/* handles critical section checks */
176 	signalintr(1);
177 }
178 
179 static
180 void
181 infosig(int nada, siginfo_t *info, void *ctxp)
182 {
183 	ucontext_t *ctx = ctxp;
184 	char buf[256];
185 
186 	snprintf(buf, sizeof(buf), "lwp %d pc=%p sp=%p\n",
187 		(lwpid_t)lwp_gettid(),
188 		(void *)(intptr_t)ctx->uc_mcontext.mc_rip,
189 		(void *)(intptr_t)ctx->uc_mcontext.mc_rsp);
190 	write(2, buf, strlen(buf));
191 }
192 
193 void
194 init_exceptions(void)
195 {
196 	struct sigaction sa;
197 
198 	bzero(&sa, sizeof(sa));
199 	sa.sa_sigaction = exc_segfault;
200 	sa.sa_flags |= SA_SIGINFO | SA_NODEFER;
201 	sigemptyset(&sa.sa_mask);
202 	sigaction(SIGBUS, &sa, NULL);
203 	sigaction(SIGSEGV, &sa, NULL);
204 	sigaction(SIGTRAP, &sa, NULL);
205 	sigaction(SIGFPE, &sa, NULL);
206 
207 	sa.sa_flags &= ~SA_NODEFER;
208 
209 #ifdef DDB
210 	sa.sa_sigaction = exc_debugger;
211 	sigaction(SIGQUIT, &sa, NULL);
212 #endif
213 	sa.sa_sigaction = ipisig;
214 	sigaction(SIGUSR1, &sa, NULL);
215 
216 	sa.sa_sigaction = stopsig;
217 	sigaction(SIGXCPU, &sa, NULL);
218 
219 	sa.sa_sigaction = kqueuesig;
220 	sigaction(SIGIO, &sa, NULL);
221 
222 	sa.sa_sigaction = timersig;
223 	sigaction(SIGURG, &sa, NULL);
224 
225 	sa.sa_sigaction = cosig;
226 	sigaction(SIGALRM, &sa, NULL);
227 
228 	sa.sa_sigaction = infosig;
229 	sigaction(SIGINFO, &sa, NULL);
230 }
231 
232 /*
233  * This function handles a segmentation fault.
234  *
235  * XXX We assume that trapframe is a subset of ucontext.  It is as of
236  *     this writing.
237  */
238 static void
239 exc_segfault(int signo, siginfo_t *info, void *ctxp)
240 {
241 	ucontext_t *ctx = ctxp;
242 
243 #if 0
244 	kprintf("CAUGHT SIG %d RIP %08lx ERR %08lx TRAPNO %ld "
245 		"err %ld addr %08lx\n",
246 		signo,
247 		ctx->uc_mcontext.mc_rip,
248 		ctx->uc_mcontext.mc_err,
249 		ctx->uc_mcontext.mc_trapno & 0xFFFF,
250 		ctx->uc_mcontext.mc_trapno >> 16,
251 		ctx->uc_mcontext.mc_addr);
252 #endif
253 	kern_trap((struct trapframe *)&ctx->uc_mcontext.mc_rdi);
254 	splz();
255 }
256 
257 #ifdef DDB
258 
259 static void
260 exc_debugger(int signo, siginfo_t *info, void *ctxp)
261 {
262 	ucontext_t *ctx = ctxp;
263 
264 	kprintf("CAUGHT SIG %d RIP %08lx RSP %08lx TD %p\n",
265 		signo,
266 		ctx->uc_mcontext.mc_rip,
267 		ctx->uc_mcontext.mc_rsp,
268 		curthread);
269 	Debugger("interrupt from console");
270 }
271 
272 #endif
273