Fix VKERNEL64 build.
[dragonfly.git] / sys / platform / vkernel64 / x86_64 / exception.c
blob6a163fc087ebaca179835d2d0bd84a83f258e8ad
2 /*
3 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Matthew Dillon <dillon@backplane.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
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.
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>
44 #include <sys/thread2.h>
46 #include <machine/trap.h>
47 #include <machine/md_var.h>
48 #include <machine/segments.h>
49 #include <machine/cpu.h>
50 #include <machine/smp.h>
52 #include <err.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <unistd.h>
57 int _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
58 int _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
60 static void exc_segfault(int signo, siginfo_t *info, void *ctx);
61 #ifdef DDB
62 static void exc_debugger(int signo, siginfo_t *info, void *ctx);
63 #endif
66 * IPIs are 'fast' interrupts, so we deal with them directly from our
67 * signal handler.
69 * WARNING: Signals are not physically disabled here so we have to enter
70 * our critical section before bumping gd_intr_nesting_level or another
71 * interrupt can come along and get really confused.
73 static
74 void
75 ipisig(int nada, siginfo_t *info, void *ctxp)
77 globaldata_t gd = mycpu;
78 thread_t td = gd->gd_curthread;
80 if (td->td_critcount == 0) {
81 ++td->td_critcount;
82 ++gd->gd_intr_nesting_level;
83 atomic_swap_int(&gd->gd_npoll, 0);
84 lwkt_process_ipiq();
85 --gd->gd_intr_nesting_level;
86 --td->td_critcount;
87 } else {
88 need_ipiq();
93 * Unconditionally stop or restart a cpu.
95 * Note: cpu_mask_all_signals() masks all signals except SIGXCPU itself.
96 * SIGXCPU itself is blocked on entry to stopsig() by the signal handler
97 * itself.
99 * WARNING: Signals are not physically disabled here so we have to enter
100 * our critical section before bumping gd_intr_nesting_level or another
101 * interrupt can come along and get really confused.
103 static
104 void
105 stopsig(int nada, siginfo_t *info, void *ctxp)
107 globaldata_t gd = mycpu;
108 thread_t td = gd->gd_curthread;
109 sigset_t ss;
111 sigemptyset(&ss);
112 sigaddset(&ss, SIGALRM);
113 sigaddset(&ss, SIGIO);
114 sigaddset(&ss, SIGQUIT);
115 sigaddset(&ss, SIGUSR1);
116 sigaddset(&ss, SIGUSR2);
117 sigaddset(&ss, SIGTERM);
118 sigaddset(&ss, SIGWINCH);
120 ++td->td_critcount;
121 ++gd->gd_intr_nesting_level;
122 while (CPUMASK_TESTMASK(stopped_cpus, gd->gd_cpumask)) {
123 sigsuspend(&ss);
125 --gd->gd_intr_nesting_level;
126 --td->td_critcount;
129 #if 0
132 * SIGIO is used by cothreads to signal back into the virtual kernel.
134 static
135 void
136 iosig(int nada, siginfo_t *info, void *ctxp)
138 signalintr(4);
141 #endif
143 static
144 void
145 infosig(int nada, siginfo_t *info, void *ctxp)
147 ucontext_t *ctx = ctxp;
148 char buf[256];
150 snprintf(buf, sizeof(buf), "lwp %d pc=%p sp=%p\n",
151 (int)lwp_gettid(),
152 (void *)(intptr_t)ctx->uc_mcontext.mc_rip,
153 (void *)(intptr_t)ctx->uc_mcontext.mc_rsp);
154 write(2, buf, strlen(buf));
157 void
158 init_exceptions(void)
160 struct sigaction sa;
162 bzero(&sa, sizeof(sa));
163 sa.sa_sigaction = exc_segfault;
164 sa.sa_flags |= SA_SIGINFO | SA_NODEFER;
165 sigemptyset(&sa.sa_mask);
166 sigaction(SIGBUS, &sa, NULL);
167 sigaction(SIGSEGV, &sa, NULL);
168 sigaction(SIGTRAP, &sa, NULL);
169 sigaction(SIGFPE, &sa, NULL);
171 sa.sa_flags &= ~SA_NODEFER;
173 #ifdef DDB
174 sa.sa_sigaction = exc_debugger;
175 sigaction(SIGQUIT, &sa, NULL);
176 #endif
177 sa.sa_sigaction = ipisig;
178 sigaction(SIGUSR1, &sa, NULL);
179 sa.sa_sigaction = stopsig;
180 sigaction(SIGXCPU, &sa, NULL);
181 #if 0
182 sa.sa_sigaction = iosig;
183 sigaction(SIGIO, &sa, NULL);
184 #endif
185 sa.sa_sigaction = infosig;
186 sigaction(SIGINFO, &sa, NULL);
190 * This function handles a segmentation fault.
192 * XXX We assume that trapframe is a subset of ucontext. It is as of
193 * this writing.
195 static void
196 exc_segfault(int signo, siginfo_t *info, void *ctxp)
198 ucontext_t *ctx = ctxp;
200 #if 0
201 kprintf("CAUGHT SIG %d RIP %08lx ERR %08lx TRAPNO %ld "
202 "err %ld addr %08lx\n",
203 signo,
204 ctx->uc_mcontext.mc_rip,
205 ctx->uc_mcontext.mc_err,
206 ctx->uc_mcontext.mc_trapno & 0xFFFF,
207 ctx->uc_mcontext.mc_trapno >> 16,
208 ctx->uc_mcontext.mc_addr);
209 #endif
210 kern_trap((struct trapframe *)&ctx->uc_mcontext.mc_rdi);
211 splz();
214 #ifdef DDB
216 static void
217 exc_debugger(int signo, siginfo_t *info, void *ctxp)
219 ucontext_t *ctx = ctxp;
221 kprintf("CAUGHT SIG %d RIP %08lx RSP %08lx TD %p\n",
222 signo,
223 ctx->uc_mcontext.mc_rip,
224 ctx->uc_mcontext.mc_rsp,
225 curthread);
226 Debugger("interrupt from console");
229 #endif