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