kernel: Make SMP support default (and non-optional).
[dragonfly.git] / sys / platform / vkernel / i386 / exception.c
CommitLineData
6092278a
MD
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.
6092278a
MD
34 */
35
aaf8b91f 36#include "opt_ddb.h"
6092278a
MD
37#include <sys/types.h>
38#include <sys/systm.h>
24eb47e0 39#include <sys/reboot.h>
6092278a 40#include <sys/kernel.h>
bb195a01
MD
41#include <sys/kthread.h>
42#include <sys/reboot.h>
aaf8b91f 43#include <ddb/ddb.h>
f991237f
MD
44
45#include <sys/thread2.h>
46
6092278a
MD
47#include <machine/trap.h>
48#include <machine/md_var.h>
4e7c41c5 49#include <machine/segments.h>
24eb47e0 50#include <machine/cpu.h>
8f66501e 51#include <machine/smp.h>
24eb47e0
MD
52
53#include <err.h>
6092278a 54#include <signal.h>
8f8555f8 55#include <stdio.h>
24eb47e0 56#include <unistd.h>
6092278a 57
4e7c41c5
MD
58int _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
59int _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
60
6092278a 61static void exc_segfault(int signo, siginfo_t *info, void *ctx);
aaf8b91f
MD
62#ifdef DDB
63static void exc_debugger(int signo, siginfo_t *info, void *ctx);
64#endif
6092278a 65
24eb47e0
MD
66/*
67 * IPIs are 'fast' interrupts, so we deal with them directly from our
68 * signal handler.
6b8b1c37
MD
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.
24eb47e0
MD
73 */
74static
75void
21c396e5 76ipisig(int nada, siginfo_t *info, void *ctxp)
24eb47e0 77{
f9235b6d
MD
78 if (curthread->td_critcount == 0) {
79 ++curthread->td_critcount;
6b8b1c37 80 ++mycpu->gd_intr_nesting_level;
24eb47e0 81 lwkt_process_ipiq();
6b8b1c37 82 --mycpu->gd_intr_nesting_level;
f9235b6d 83 --curthread->td_critcount;
24eb47e0
MD
84 } else {
85 need_ipiq();
86 }
24eb47e0 87}
bb195a01 88
8f66501e
MD
89/*
90 * Unconditionally stop or restart a cpu.
91 *
92 * Note: cpu_mask_all_signals() masks all signals except SIGXCPU itself.
93 * SIGXCPU itself is blocked on entry to stopsig() by the signal handler
94 * itself.
6b8b1c37
MD
95 *
96 * WARNING: Signals are not physically disabled here so we have to enter
97 * our critical section before bumping gd_intr_nesting_level or another
98 * interrupt can come along and get really confused.
8f66501e
MD
99 */
100static
101void
102stopsig(int nada, siginfo_t *info, void *ctxp)
103{
104 sigset_t ss;
105
106 sigemptyset(&ss);
107 sigaddset(&ss, SIGALRM);
108 sigaddset(&ss, SIGIO);
109 sigaddset(&ss, SIGQUIT);
110 sigaddset(&ss, SIGUSR1);
111 sigaddset(&ss, SIGUSR2);
112 sigaddset(&ss, SIGTERM);
113 sigaddset(&ss, SIGWINCH);
114
f9235b6d 115 ++curthread->td_critcount;
8f66501e
MD
116 ++mycpu->gd_intr_nesting_level;
117 while (stopped_cpus & mycpu->gd_cpumask) {
118 sigsuspend(&ss);
119 }
120 --mycpu->gd_intr_nesting_level;
f9235b6d 121 --curthread->td_critcount;
8f66501e
MD
122}
123
9c2ed617
MD
124#if 0
125
126/*
127 * SIGIO is used by cothreads to signal back into the virtual kernel.
128 */
129static
130void
131iosig(int nada, siginfo_t *info, void *ctxp)
132{
133 signalintr(4);
134}
135
136#endif
137
8f8555f8
MD
138static
139void
140infosig(int nada, siginfo_t *info, void *ctxp)
141{
142 ucontext_t *ctx = ctxp;
143 char buf[256];
144
145 snprintf(buf, sizeof(buf), "lwp %d pc=%p sp=%p\n",
146 (int)lwp_gettid(),
147 (void *)(intptr_t)ctx->uc_mcontext.mc_eip,
148 (void *)(intptr_t)ctx->uc_mcontext.mc_esp);
149 write(2, buf, strlen(buf));
150}
151
6092278a
MD
152void
153init_exceptions(void)
154{
155 struct sigaction sa;
156
157 bzero(&sa, sizeof(sa));
158 sa.sa_sigaction = exc_segfault;
eec2b734 159 sa.sa_flags |= SA_SIGINFO | SA_NODEFER;
6092278a 160 sigemptyset(&sa.sa_mask);
eec2b734 161 sigaction(SIGBUS, &sa, NULL);
6092278a 162 sigaction(SIGSEGV, &sa, NULL);
4e7c41c5 163 sigaction(SIGTRAP, &sa, NULL);
731164d0 164 sigaction(SIGFPE, &sa, NULL);
bb195a01 165
aa56bc86
MD
166 sa.sa_flags &= ~SA_NODEFER;
167
aaf8b91f
MD
168#ifdef DDB
169 sa.sa_sigaction = exc_debugger;
170 sigaction(SIGQUIT, &sa, NULL);
171#endif
21c396e5
MD
172 sa.sa_sigaction = ipisig;
173 sigaction(SIGUSR1, &sa, NULL);
8f66501e
MD
174 sa.sa_sigaction = stopsig;
175 sigaction(SIGXCPU, &sa, NULL);
9c2ed617
MD
176#if 0
177 sa.sa_sigaction = iosig;
178 sigaction(SIGIO, &sa, NULL);
179#endif
8f8555f8
MD
180 sa.sa_sigaction = infosig;
181 sigaction(SIGINFO, &sa, NULL);
6092278a
MD
182}
183
184/*
185 * This function handles a segmentation fault.
186 *
187 * XXX We assume that trapframe is a subset of ucontext. It is as of
188 * this writing.
189 */
190static void
191exc_segfault(int signo, siginfo_t *info, void *ctxp)
192{
193 ucontext_t *ctx = ctxp;
6092278a 194
aaf8b91f
MD
195#if 0
196 kprintf("CAUGHT SEGFAULT EIP %08x ERR %08x TRAPNO %d err %d\n",
6092278a
MD
197 ctx->uc_mcontext.mc_eip,
198 ctx->uc_mcontext.mc_err,
4e7c41c5
MD
199 ctx->uc_mcontext.mc_trapno & 0xFFFF,
200 ctx->uc_mcontext.mc_trapno >> 16);
aaf8b91f 201#endif
4e7c41c5 202 kern_trap((struct trapframe *)&ctx->uc_mcontext.mc_gs);
6092278a
MD
203 splz();
204}
205
aaf8b91f
MD
206#ifdef DDB
207
208static void
209exc_debugger(int signo, siginfo_t *info, void *ctx)
210{
211 Debugger("interrupt from console");
212}
213
214#endif