| @@ -1,794 +1,798 @@ | | | @@ -1,794 +1,798 @@ |
1 | /* $NetBSD: trap.c,v 1.92 2017/02/23 03:34:22 kamil Exp $ */ | | 1 | /* $NetBSD: trap.c,v 1.93 2017/03/09 00:16:07 chs Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Charles M. Hannum. | | 8 | * by Charles M. Hannum. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | /*- | | 32 | /*- |
33 | * Copyright (c) 1990 The Regents of the University of California. | | 33 | * Copyright (c) 1990 The Regents of the University of California. |
34 | * All rights reserved. | | 34 | * All rights reserved. |
35 | * | | 35 | * |
36 | * This code is derived from software contributed to Berkeley by | | 36 | * This code is derived from software contributed to Berkeley by |
37 | * the University of Utah, and William Jolitz. | | 37 | * the University of Utah, and William Jolitz. |
38 | * | | 38 | * |
39 | * Redistribution and use in source and binary forms, with or without | | 39 | * Redistribution and use in source and binary forms, with or without |
40 | * modification, are permitted provided that the following conditions | | 40 | * modification, are permitted provided that the following conditions |
41 | * are met: | | 41 | * are met: |
42 | * 1. Redistributions of source code must retain the above copyright | | 42 | * 1. Redistributions of source code must retain the above copyright |
43 | * notice, this list of conditions and the following disclaimer. | | 43 | * notice, this list of conditions and the following disclaimer. |
44 | * 2. Redistributions in binary form must reproduce the above copyright | | 44 | * 2. Redistributions in binary form must reproduce the above copyright |
45 | * notice, this list of conditions and the following disclaimer in the | | 45 | * notice, this list of conditions and the following disclaimer in the |
46 | * documentation and/or other materials provided with the distribution. | | 46 | * documentation and/or other materials provided with the distribution. |
47 | * 3. Neither the name of the University nor the names of its contributors | | 47 | * 3. Neither the name of the University nor the names of its contributors |
48 | * may be used to endorse or promote products derived from this software | | 48 | * may be used to endorse or promote products derived from this software |
49 | * without specific prior written permission. | | 49 | * without specific prior written permission. |
50 | * | | 50 | * |
51 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | | 51 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
52 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 52 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
53 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 53 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
54 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 54 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
55 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 55 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
56 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 56 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
57 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 57 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 58 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
59 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 59 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
60 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 60 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
61 | * SUCH DAMAGE. | | 61 | * SUCH DAMAGE. |
62 | * | | 62 | * |
63 | * @(#)trap.c 7.4 (Berkeley) 5/13/91 | | 63 | * @(#)trap.c 7.4 (Berkeley) 5/13/91 |
64 | */ | | 64 | */ |
65 | | | 65 | |
66 | /* | | 66 | /* |
67 | * 386 Trap and System call handling | | 67 | * 386 Trap and System call handling |
68 | */ | | 68 | */ |
69 | | | 69 | |
70 | #include <sys/cdefs.h> | | 70 | #include <sys/cdefs.h> |
71 | __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.92 2017/02/23 03:34:22 kamil Exp $"); | | 71 | __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.93 2017/03/09 00:16:07 chs Exp $"); |
72 | | | 72 | |
73 | #include "opt_ddb.h" | | 73 | #include "opt_ddb.h" |
74 | #include "opt_kgdb.h" | | 74 | #include "opt_kgdb.h" |
75 | #include "opt_xen.h" | | 75 | #include "opt_xen.h" |
76 | #include "opt_dtrace.h" | | 76 | #include "opt_dtrace.h" |
77 | | | 77 | |
78 | #include <sys/param.h> | | 78 | #include <sys/param.h> |
79 | #include <sys/systm.h> | | 79 | #include <sys/systm.h> |
80 | #include <sys/proc.h> | | 80 | #include <sys/proc.h> |
81 | #include <sys/acct.h> | | 81 | #include <sys/acct.h> |
82 | #include <sys/kauth.h> | | 82 | #include <sys/kauth.h> |
83 | #include <sys/kernel.h> | | 83 | #include <sys/kernel.h> |
84 | #include <sys/kmem.h> | | 84 | #include <sys/kmem.h> |
85 | #include <sys/ras.h> | | 85 | #include <sys/ras.h> |
86 | #include <sys/signal.h> | | 86 | #include <sys/signal.h> |
87 | #include <sys/syscall.h> | | 87 | #include <sys/syscall.h> |
88 | #include <sys/cpu.h> | | 88 | #include <sys/cpu.h> |
89 | #include <sys/ucontext.h> | | 89 | #include <sys/ucontext.h> |
90 | | | 90 | |
91 | #include <uvm/uvm_extern.h> | | 91 | #include <uvm/uvm_extern.h> |
92 | | | 92 | |
93 | #include <machine/cpufunc.h> | | 93 | #include <machine/cpufunc.h> |
94 | #include <x86/fpu.h> | | 94 | #include <x86/fpu.h> |
95 | #include <machine/psl.h> | | 95 | #include <machine/psl.h> |
96 | #include <machine/reg.h> | | 96 | #include <machine/reg.h> |
97 | #include <machine/trap.h> | | 97 | #include <machine/trap.h> |
98 | #include <machine/userret.h> | | 98 | #include <machine/userret.h> |
99 | #include <machine/db_machdep.h> | | 99 | #include <machine/db_machdep.h> |
100 | | | 100 | |
101 | #include <x86/nmi.h> | | 101 | #include <x86/nmi.h> |
102 | | | 102 | |
103 | #ifndef XEN | | 103 | #ifndef XEN |
104 | #include "isa.h" | | 104 | #include "isa.h" |
105 | #endif | | 105 | #endif |
106 | | | 106 | |
107 | #include <sys/kgdb.h> | | 107 | #include <sys/kgdb.h> |
108 | | | 108 | |
109 | #ifdef KDTRACE_HOOKS | | 109 | #ifdef KDTRACE_HOOKS |
110 | #include <sys/dtrace_bsd.h> | | 110 | #include <sys/dtrace_bsd.h> |
111 | | | 111 | |
112 | /* | | 112 | /* |
113 | * This is a hook which is initialized by the dtrace module | | 113 | * This is a hook which is initialized by the dtrace module |
114 | * to handle traps which might occur during DTrace probe | | 114 | * to handle traps which might occur during DTrace probe |
115 | * execution. | | 115 | * execution. |
116 | */ | | 116 | */ |
117 | dtrace_trap_func_t dtrace_trap_func = NULL; | | 117 | dtrace_trap_func_t dtrace_trap_func = NULL; |
118 | | | 118 | |
119 | dtrace_doubletrap_func_t dtrace_doubletrap_func = NULL; | | 119 | dtrace_doubletrap_func_t dtrace_doubletrap_func = NULL; |
120 | #endif | | 120 | #endif |
121 | | | 121 | |
122 | void trap(struct trapframe *); | | 122 | void trap(struct trapframe *); |
123 | void trap_return_fault_return(struct trapframe *) __dead; | | 123 | void trap_return_fault_return(struct trapframe *) __dead; |
124 | | | 124 | |
125 | const char * const trap_type[] = { | | 125 | const char * const trap_type[] = { |
126 | "privileged instruction fault", /* 0 T_PRIVINFLT */ | | 126 | "privileged instruction fault", /* 0 T_PRIVINFLT */ |
127 | "breakpoint trap", /* 1 T_BPTFLT */ | | 127 | "breakpoint trap", /* 1 T_BPTFLT */ |
128 | "arithmetic trap", /* 2 T_ARITHTRAP */ | | 128 | "arithmetic trap", /* 2 T_ARITHTRAP */ |
129 | "asynchronous system trap", /* 3 T_ASTFLT */ | | 129 | "asynchronous system trap", /* 3 T_ASTFLT */ |
130 | "protection fault", /* 4 T_PROTFLT */ | | 130 | "protection fault", /* 4 T_PROTFLT */ |
131 | "trace trap", /* 5 T_TRCTRAP */ | | 131 | "trace trap", /* 5 T_TRCTRAP */ |
132 | "page fault", /* 6 T_PAGEFLT */ | | 132 | "page fault", /* 6 T_PAGEFLT */ |
133 | "alignment fault", /* 7 T_ALIGNFLT */ | | 133 | "alignment fault", /* 7 T_ALIGNFLT */ |
134 | "integer divide fault", /* 8 T_DIVIDE */ | | 134 | "integer divide fault", /* 8 T_DIVIDE */ |
135 | "non-maskable interrupt", /* 9 T_NMI */ | | 135 | "non-maskable interrupt", /* 9 T_NMI */ |
136 | "overflow trap", /* 10 T_OFLOW */ | | 136 | "overflow trap", /* 10 T_OFLOW */ |
137 | "bounds check fault", /* 11 T_BOUND */ | | 137 | "bounds check fault", /* 11 T_BOUND */ |
138 | "FPU not available fault", /* 12 T_DNA */ | | 138 | "FPU not available fault", /* 12 T_DNA */ |
139 | "double fault", /* 13 T_DOUBLEFLT */ | | 139 | "double fault", /* 13 T_DOUBLEFLT */ |
140 | "FPU operand fetch fault", /* 14 T_FPOPFLT */ | | 140 | "FPU operand fetch fault", /* 14 T_FPOPFLT */ |
141 | "invalid TSS fault", /* 15 T_TSSFLT */ | | 141 | "invalid TSS fault", /* 15 T_TSSFLT */ |
142 | "segment not present fault", /* 16 T_SEGNPFLT */ | | 142 | "segment not present fault", /* 16 T_SEGNPFLT */ |
143 | "stack fault", /* 17 T_STKFLT */ | | 143 | "stack fault", /* 17 T_STKFLT */ |
144 | "machine check fault", /* 18 T_MCA */ | | 144 | "machine check fault", /* 18 T_MCA */ |
145 | "SSE FP exception", /* 19 T_XMM */ | | 145 | "SSE FP exception", /* 19 T_XMM */ |
146 | "reserved trap", /* 20 T_RESERVED */ | | 146 | "reserved trap", /* 20 T_RESERVED */ |
147 | }; | | 147 | }; |
148 | int trap_types = __arraycount(trap_type); | | 148 | int trap_types = __arraycount(trap_type); |
149 | | | 149 | |
150 | #ifdef DEBUG | | 150 | #ifdef DEBUG |
151 | int trapdebug = 0; | | 151 | int trapdebug = 0; |
152 | #endif | | 152 | #endif |
153 | | | 153 | |
154 | #define IDTVEC(name) __CONCAT(X, name) | | 154 | #define IDTVEC(name) __CONCAT(X, name) |
155 | | | 155 | |
156 | #ifdef TRAP_SIGDEBUG | | 156 | #ifdef TRAP_SIGDEBUG |
157 | static void frame_dump(struct trapframe *); | | 157 | static void frame_dump(struct trapframe *, struct pcb *); |
158 | #endif | | 158 | #endif |
159 | | | 159 | |
160 | static void * | | 160 | static void * |
161 | onfault_handler(const struct pcb *pcb, const struct trapframe *tf) | | 161 | onfault_handler(const struct pcb *pcb, const struct trapframe *tf) |
162 | { | | 162 | { |
163 | struct onfault_table { | | 163 | struct onfault_table { |
164 | uintptr_t start; | | 164 | uintptr_t start; |
165 | uintptr_t end; | | 165 | uintptr_t end; |
166 | void *handler; | | 166 | void *handler; |
167 | }; | | 167 | }; |
168 | extern const struct onfault_table onfault_table[]; | | 168 | extern const struct onfault_table onfault_table[]; |
169 | const struct onfault_table *p; | | 169 | const struct onfault_table *p; |
170 | uintptr_t pc; | | 170 | uintptr_t pc; |
171 | | | 171 | |
172 | if (pcb->pcb_onfault != NULL) { | | 172 | if (pcb->pcb_onfault != NULL) { |
173 | return pcb->pcb_onfault; | | 173 | return pcb->pcb_onfault; |
174 | } | | 174 | } |
175 | | | 175 | |
176 | pc = tf->tf_rip; | | 176 | pc = tf->tf_rip; |
177 | for (p = onfault_table; p->start; p++) { | | 177 | for (p = onfault_table; p->start; p++) { |
178 | if (p->start <= pc && pc < p->end) { | | 178 | if (p->start <= pc && pc < p->end) { |
179 | return p->handler; | | 179 | return p->handler; |
180 | } | | 180 | } |
181 | } | | 181 | } |
182 | return NULL; | | 182 | return NULL; |
183 | } | | 183 | } |
184 | | | 184 | |
185 | static void | | 185 | static void |
186 | trap_print(const struct trapframe *frame, const lwp_t *l) | | 186 | trap_print(const struct trapframe *frame, const lwp_t *l) |
187 | { | | 187 | { |
188 | const int type = frame->tf_trapno; | | 188 | const int type = frame->tf_trapno; |
189 | | | 189 | |
190 | if (frame->tf_trapno < trap_types) { | | 190 | if (frame->tf_trapno < trap_types) { |
191 | printf("fatal %s", trap_type[type]); | | 191 | printf("fatal %s", trap_type[type]); |
192 | } else { | | 192 | } else { |
193 | printf("unknown trap %d", type); | | 193 | printf("unknown trap %d", type); |
194 | } | | 194 | } |
195 | printf(" in %s mode\n", (type & T_USER) ? "user" : "supervisor"); | | 195 | printf(" in %s mode\n", (type & T_USER) ? "user" : "supervisor"); |
196 | | | 196 | |
197 | printf("trap type %d code %lx rip %lx cs %lx rflags %lx cr2 %lx " | | 197 | printf("trap type %d code %#lx rip %#lx cs %#lx rflags %#lx cr2 %#lx " |
198 | "ilevel %x rsp %lx\n", | | 198 | "ilevel %#x rsp %#lx\n", |
199 | type, frame->tf_err, (u_long)frame->tf_rip, frame->tf_cs, | | 199 | type, frame->tf_err, (u_long)frame->tf_rip, frame->tf_cs, |
200 | frame->tf_rflags, rcr2(), curcpu()->ci_ilevel, frame->tf_rsp); | | 200 | frame->tf_rflags, rcr2(), curcpu()->ci_ilevel, frame->tf_rsp); |
201 | | | 201 | |
202 | printf("curlwp %p pid %d.%d lowest kstack %p\n", | | 202 | printf("curlwp %p pid %d.%d lowest kstack %p\n", |
203 | l, l->l_proc->p_pid, l->l_lid, KSTACK_LOWEST_ADDR(l)); | | 203 | l, l->l_proc->p_pid, l->l_lid, KSTACK_LOWEST_ADDR(l)); |
204 | } | | 204 | } |
205 | | | 205 | |
206 | /* | | 206 | /* |
207 | * trap(frame): exception, fault, and trap interface to BSD kernel. | | 207 | * trap(frame): exception, fault, and trap interface to BSD kernel. |
208 | * | | 208 | * |
209 | * This common code is called from assembly language IDT gate entry routines | | 209 | * This common code is called from assembly language IDT gate entry routines |
210 | * that prepare a suitable stack frame, and restore this frame after the | | 210 | * that prepare a suitable stack frame, and restore this frame after the |
211 | * exception has been processed. Note that the effect is as if the arguments | | 211 | * exception has been processed. Note that the effect is as if the arguments |
212 | * were passed call by reference. | | 212 | * were passed call by reference. |
213 | * | | 213 | * |
214 | * Note that the fpu traps (07 T_DNA, 10 T_ARITHTRAP and 13 T_XMM) | | 214 | * Note that the fpu traps (07 T_DNA, 10 T_ARITHTRAP and 13 T_XMM) |
215 | * jump directly into the code in x86/fpu.c so they get processed | | 215 | * jump directly into the code in x86/fpu.c so they get processed |
216 | * without interrupts being enabled. | | 216 | * without interrupts being enabled. |
217 | */ | | 217 | */ |
218 | void | | 218 | void |
219 | trap(struct trapframe *frame) | | 219 | trap(struct trapframe *frame) |
220 | { | | 220 | { |
221 | struct lwp *l = curlwp; | | 221 | struct lwp *l = curlwp; |
222 | struct proc *p; | | 222 | struct proc *p; |
223 | struct pcb *pcb; | | 223 | struct pcb *pcb; |
224 | extern char fusuintrfailure[], kcopy_fault[]; | | 224 | extern char fusuintrfailure[], kcopy_fault[]; |
225 | extern char IDTVEC(oosyscall)[]; | | 225 | extern char IDTVEC(oosyscall)[]; |
226 | extern char IDTVEC(osyscall)[]; | | 226 | extern char IDTVEC(osyscall)[]; |
227 | extern char IDTVEC(syscall32)[]; | | 227 | extern char IDTVEC(syscall32)[]; |
228 | #ifndef XEN | | 228 | #ifndef XEN |
229 | struct trapframe *vframe; | | 229 | struct trapframe *vframe; |
230 | #endif | | 230 | #endif |
231 | ksiginfo_t ksi; | | 231 | ksiginfo_t ksi; |
232 | void *onfault; | | 232 | void *onfault; |
233 | int type, error; | | 233 | int type, error; |
234 | uint64_t cr2; | | 234 | uint64_t cr2; |
235 | bool pfail; | | 235 | bool pfail; |
236 | | | 236 | |
237 | if (__predict_true(l != NULL)) { | | 237 | if (__predict_true(l != NULL)) { |
238 | pcb = lwp_getpcb(l); | | 238 | pcb = lwp_getpcb(l); |
239 | p = l->l_proc; | | 239 | p = l->l_proc; |
240 | } else { | | 240 | } else { |
241 | /* | | 241 | /* |
242 | * this can happen eg. on break points in early on boot. | | 242 | * this can happen eg. on break points in early on boot. |
243 | */ | | 243 | */ |
244 | pcb = NULL; | | 244 | pcb = NULL; |
245 | p = NULL; | | 245 | p = NULL; |
246 | } | | 246 | } |
247 | type = frame->tf_trapno; | | 247 | type = frame->tf_trapno; |
248 | | | 248 | |
249 | #ifdef DEBUG | | 249 | #ifdef DEBUG |
250 | if (trapdebug) { | | 250 | if (trapdebug) { |
251 | trap_print(frame, l); | | 251 | trap_print(frame, l); |
252 | } | | 252 | } |
253 | #endif | | 253 | #endif |
254 | if (type != T_NMI && !KERNELMODE(frame->tf_cs, frame->tf_rflags)) { | | 254 | if (type != T_NMI && !KERNELMODE(frame->tf_cs, frame->tf_rflags)) { |
255 | type |= T_USER; | | 255 | type |= T_USER; |
256 | l->l_md.md_regs = frame; | | 256 | l->l_md.md_regs = frame; |
257 | LWP_CACHE_CREDS(l, p); | | 257 | LWP_CACHE_CREDS(l, p); |
258 | } | | 258 | } |
259 | | | 259 | |
260 | #ifdef KDTRACE_HOOKS | | 260 | #ifdef KDTRACE_HOOKS |
261 | /* | | 261 | /* |
262 | * A trap can occur while DTrace executes a probe. Before | | 262 | * A trap can occur while DTrace executes a probe. Before |
263 | * executing the probe, DTrace blocks re-scheduling and sets | | 263 | * executing the probe, DTrace blocks re-scheduling and sets |
264 | * a flag in its per-cpu flags to indicate that it doesn't | | 264 | * a flag in its per-cpu flags to indicate that it doesn't |
265 | * want to fault. On returning from the the probe, the no-fault | | 265 | * want to fault. On returning from the the probe, the no-fault |
266 | * flag is cleared and finally re-scheduling is enabled. | | 266 | * flag is cleared and finally re-scheduling is enabled. |
267 | * | | 267 | * |
268 | * If the DTrace kernel module has registered a trap handler, | | 268 | * If the DTrace kernel module has registered a trap handler, |
269 | * call it and if it returns non-zero, assume that it has | | 269 | * call it and if it returns non-zero, assume that it has |
270 | * handled the trap and modified the trap frame so that this | | 270 | * handled the trap and modified the trap frame so that this |
271 | * function can return normally. | | 271 | * function can return normally. |
272 | */ | | 272 | */ |
273 | if ((type == T_PROTFLT || type == T_PAGEFLT) && | | 273 | if ((type == T_PROTFLT || type == T_PAGEFLT) && |
274 | dtrace_trap_func != NULL) { | | 274 | dtrace_trap_func != NULL) { |
275 | if ((*dtrace_trap_func)(frame, type)) { | | 275 | if ((*dtrace_trap_func)(frame, type)) { |
276 | return; | | 276 | return; |
277 | } | | 277 | } |
278 | } | | 278 | } |
279 | #endif | | 279 | #endif |
280 | | | 280 | |
281 | switch (type) { | | 281 | switch (type) { |
282 | | | 282 | |
283 | default: | | 283 | default: |
284 | we_re_toast: | | 284 | we_re_toast: |
285 | trap_print(frame, l); | | 285 | trap_print(frame, l); |
286 | | | 286 | |
287 | if (kdb_trap(type, 0, frame)) | | 287 | if (kdb_trap(type, 0, frame)) |
288 | return; | | 288 | return; |
289 | if (kgdb_trap(type, frame)) | | 289 | if (kgdb_trap(type, frame)) |
290 | return; | | 290 | return; |
291 | /* | | 291 | /* |
292 | * If this is a breakpoint, don't panic if we're not connected. | | 292 | * If this is a breakpoint, don't panic if we're not connected. |
293 | */ | | 293 | */ |
294 | if (type == T_BPTFLT && kgdb_disconnected()) { | | 294 | if (type == T_BPTFLT && kgdb_disconnected()) { |
295 | printf("kgdb: ignored %s\n", trap_type[type]); | | 295 | printf("kgdb: ignored %s\n", trap_type[type]); |
296 | return; | | 296 | return; |
297 | } | | 297 | } |
298 | panic("trap"); | | 298 | panic("trap"); |
299 | /*NOTREACHED*/ | | 299 | /*NOTREACHED*/ |
300 | | | 300 | |
301 | case T_PROTFLT: | | 301 | case T_PROTFLT: |
302 | case T_SEGNPFLT: | | 302 | case T_SEGNPFLT: |
303 | case T_ALIGNFLT: | | 303 | case T_ALIGNFLT: |
304 | case T_TSSFLT: | | 304 | case T_TSSFLT: |
305 | if (p == NULL) | | 305 | if (p == NULL) |
306 | goto we_re_toast; | | 306 | goto we_re_toast; |
307 | /* Check for copyin/copyout fault. */ | | 307 | /* Check for copyin/copyout fault. */ |
308 | onfault = onfault_handler(pcb, frame); | | 308 | onfault = onfault_handler(pcb, frame); |
309 | if (onfault != NULL) { | | 309 | if (onfault != NULL) { |
310 | copyefault: | | 310 | copyefault: |
311 | error = EFAULT; | | 311 | error = EFAULT; |
312 | copyfault: | | 312 | copyfault: |
313 | frame->tf_rip = (uintptr_t)onfault; | | 313 | frame->tf_rip = (uintptr_t)onfault; |
314 | frame->tf_rax = error; | | 314 | frame->tf_rax = error; |
315 | return; | | 315 | return; |
316 | } | | 316 | } |
317 | | | 317 | |
318 | /* | | 318 | /* |
319 | * Check for failure during return to user mode. | | 319 | * Check for failure during return to user mode. |
320 | * This can happen loading invalid values into the segment | | 320 | * This can happen loading invalid values into the segment |
321 | * registers, or during the 'iret' itself. | | 321 | * registers, or during the 'iret' itself. |
322 | * | | 322 | * |
323 | * We do this by looking at the instruction we faulted on. | | 323 | * We do this by looking at the instruction we faulted on. |
324 | * The specific instructions we recognize only happen when | | 324 | * The specific instructions we recognize only happen when |
325 | * returning from a trap, syscall, or interrupt. | | 325 | * returning from a trap, syscall, or interrupt. |
326 | */ | | 326 | */ |
327 | | | 327 | |
328 | kernelfault: | | 328 | kernelfault: |
329 | #ifdef XEN | | 329 | #ifdef XEN |
330 | /* | | 330 | /* |
331 | * XXX: there has to be an equivalent 'problem' | | 331 | * XXX: there has to be an equivalent 'problem' |
332 | * but I (dsl) don't know exactly what happens! | | 332 | * but I (dsl) don't know exactly what happens! |
333 | * For now panic the kernel. | | 333 | * For now panic the kernel. |
334 | */ | | 334 | */ |
335 | goto we_re_toast; | | 335 | goto we_re_toast; |
336 | #else | | 336 | #else |
337 | KSI_INIT_TRAP(&ksi); | | 337 | KSI_INIT_TRAP(&ksi); |
338 | ksi.ksi_signo = SIGSEGV; | | 338 | ksi.ksi_signo = SIGSEGV; |
339 | ksi.ksi_code = SEGV_ACCERR; | | 339 | ksi.ksi_code = SEGV_ACCERR; |
340 | ksi.ksi_trap = type; | | 340 | ksi.ksi_trap = type; |
341 | | | 341 | |
342 | /* Get %rsp value before fault - there may be a pad word | | 342 | /* Get %rsp value before fault - there may be a pad word |
343 | * below the trap frame. */ | | 343 | * below the trap frame. */ |
344 | vframe = (void *)frame->tf_rsp; | | 344 | vframe = (void *)frame->tf_rsp; |
345 | if (frame->tf_rip == 0) { | | 345 | if (frame->tf_rip == 0) { |
346 | /* | | 346 | /* |
347 | * Assume that if we jumped to null we | | 347 | * Assume that if we jumped to null we |
348 | * probably did it via a null function | | 348 | * probably did it via a null function |
349 | * pointer, so print the return address. | | 349 | * pointer, so print the return address. |
350 | */ | | 350 | */ |
351 | printf("kernel jumped to null; return addr was %p\n", | | 351 | printf("kernel jumped to null; return addr was %p\n", |
352 | *(void **)frame->tf_rsp); | | 352 | *(void **)frame->tf_rsp); |
353 | goto we_re_toast; | | 353 | goto we_re_toast; |
354 | } | | 354 | } |
355 | switch (*(uint16_t *)frame->tf_rip) { | | 355 | switch (*(uint16_t *)frame->tf_rip) { |
356 | case 0xcf48: /* iretq */ | | 356 | case 0xcf48: /* iretq */ |
357 | /* | | 357 | /* |
358 | * The 'iretq' instruction faulted, so we have the | | 358 | * The 'iretq' instruction faulted, so we have the |
359 | * 'user' registers saved after the kernel | | 359 | * 'user' registers saved after the kernel |
360 | * %rip:%cs:%fl:%rsp:%ss of the iret, and below that | | 360 | * %rip:%cs:%fl:%rsp:%ss of the iret, and below that |
361 | * the user %rip:%cs:%fl:%rsp:%ss the 'iret' was | | 361 | * the user %rip:%cs:%fl:%rsp:%ss the 'iret' was |
362 | * processing. | | 362 | * processing. |
363 | * We must copy the user register back over the | | 363 | * We must copy the user register back over the |
364 | * kernel fault frame to generate a normal stack | | 364 | * kernel fault frame to generate a normal stack |
365 | * frame (eg for sending a SIGSEGV). | | 365 | * frame (eg for sending a SIGSEGV). |
366 | */ | | 366 | */ |
367 | vframe = (void *)((char *)vframe | | 367 | vframe = (void *)((char *)vframe |
368 | - offsetof(struct trapframe, tf_rip)); | | 368 | - offsetof(struct trapframe, tf_rip)); |
369 | memmove(vframe, frame, | | 369 | memmove(vframe, frame, |
370 | offsetof(struct trapframe, tf_rip)); | | 370 | offsetof(struct trapframe, tf_rip)); |
371 | /* Set the faulting address to the user %eip */ | | 371 | /* Set the faulting address to the user %eip */ |
372 | ksi.ksi_addr = (void *)vframe->tf_rip; | | 372 | ksi.ksi_addr = (void *)vframe->tf_rip; |
373 | break; | | 373 | break; |
374 | case 0x848e: /* mov 0xa8(%rsp),%es (8e 84 24 a8 00 00 00) */ | | 374 | case 0x848e: /* mov 0xa8(%rsp),%es (8e 84 24 a8 00 00 00) */ |
375 | case 0x9c8e: /* mov 0xb0(%rsp),%ds (8e 9c 24 b0 00 00 00) */ | | 375 | case 0x9c8e: /* mov 0xb0(%rsp),%ds (8e 9c 24 b0 00 00 00) */ |
376 | #ifdef USER_LDT | | 376 | #ifdef USER_LDT |
377 | case 0xa48e: /* mov 0xa0(%rsp),%fs (8e a4 24 a0 00 00 00) */ | | 377 | case 0xa48e: /* mov 0xa0(%rsp),%fs (8e a4 24 a0 00 00 00) */ |
378 | case 0xac8e: /* mov 0x98(%rsp),%gs (8e ac 24 98 00 00 00) */ | | 378 | case 0xac8e: /* mov 0x98(%rsp),%gs (8e ac 24 98 00 00 00) */ |
379 | #endif | | 379 | #endif |
380 | /* | | 380 | /* |
381 | * We faulted loading one of the user segment registers. | | 381 | * We faulted loading one of the user segment registers. |
382 | * The stack frame containing the user registers is | | 382 | * The stack frame containing the user registers is |
383 | * still valid and pointed to by tf_rsp. | | 383 | * still valid and pointed to by tf_rsp. |
384 | * Maybe we should check the iretq follows. | | 384 | * Maybe we should check the iretq follows. |
385 | */ | | 385 | */ |
386 | if (KERNELMODE(vframe->tf_cs, vframe->tf_eflags)) | | 386 | if (KERNELMODE(vframe->tf_cs, vframe->tf_eflags)) |
387 | goto we_re_toast; | | 387 | goto we_re_toast; |
388 | /* There is no valid address for the fault */ | | 388 | /* There is no valid address for the fault */ |
389 | break; | | 389 | break; |
390 | | | 390 | |
391 | default: | | 391 | default: |
392 | goto we_re_toast; | | 392 | goto we_re_toast; |
393 | } | | 393 | } |
394 | | | 394 | |
395 | /* XXX: worry about on-stack trampolines for nested | | 395 | /* XXX: worry about on-stack trampolines for nested |
396 | * handlers?? */ | | 396 | * handlers?? */ |
397 | /* Save outer frame for any signal return */ | | 397 | /* Save outer frame for any signal return */ |
398 | l->l_md.md_regs = vframe; | | 398 | l->l_md.md_regs = vframe; |
399 | (*p->p_emul->e_trapsignal)(l, &ksi); | | 399 | (*p->p_emul->e_trapsignal)(l, &ksi); |
400 | /* Return to user by reloading the user frame */ | | 400 | /* Return to user by reloading the user frame */ |
401 | trap_return_fault_return(vframe); | | 401 | trap_return_fault_return(vframe); |
402 | /* NOTREACHED */ | | 402 | /* NOTREACHED */ |
403 | #endif | | 403 | #endif |
404 | | | 404 | |
405 | case T_PROTFLT|T_USER: /* protection fault */ | | 405 | case T_PROTFLT|T_USER: /* protection fault */ |
406 | case T_TSSFLT|T_USER: | | 406 | case T_TSSFLT|T_USER: |
407 | case T_SEGNPFLT|T_USER: | | 407 | case T_SEGNPFLT|T_USER: |
408 | case T_STKFLT|T_USER: | | 408 | case T_STKFLT|T_USER: |
409 | case T_ALIGNFLT|T_USER: | | 409 | case T_ALIGNFLT|T_USER: |
410 | #ifdef TRAP_SIGDEBUG | | 410 | #ifdef TRAP_SIGDEBUG |
411 | printf("pid %d.%d (%s): BUS/SEGV (%x) at rip %lx addr %lx\n", | | 411 | printf("pid %d.%d (%s): BUS/SEGV (%#x) at rip %#lx addr %#lx\n", |
412 | p->p_pid, l->l_lid, p->p_comm, type, frame->tf_rip, rcr2()); | | 412 | p->p_pid, l->l_lid, p->p_comm, type, frame->tf_rip, rcr2()); |
413 | frame_dump(frame); | | 413 | frame_dump(frame, pcb); |
414 | #endif | | 414 | #endif |
415 | KSI_INIT_TRAP(&ksi); | | 415 | KSI_INIT_TRAP(&ksi); |
416 | ksi.ksi_trap = type & ~T_USER; | | 416 | ksi.ksi_trap = type & ~T_USER; |
417 | ksi.ksi_addr = (void *)rcr2(); | | 417 | ksi.ksi_addr = (void *)rcr2(); |
418 | switch (type) { | | 418 | switch (type) { |
419 | case T_SEGNPFLT|T_USER: | | 419 | case T_SEGNPFLT|T_USER: |
420 | case T_STKFLT|T_USER: | | 420 | case T_STKFLT|T_USER: |
421 | ksi.ksi_signo = SIGBUS; | | 421 | ksi.ksi_signo = SIGBUS; |
422 | ksi.ksi_code = BUS_ADRERR; | | 422 | ksi.ksi_code = BUS_ADRERR; |
423 | break; | | 423 | break; |
424 | case T_TSSFLT|T_USER: | | 424 | case T_TSSFLT|T_USER: |
425 | ksi.ksi_signo = SIGBUS; | | 425 | ksi.ksi_signo = SIGBUS; |
426 | ksi.ksi_code = BUS_OBJERR; | | 426 | ksi.ksi_code = BUS_OBJERR; |
427 | break; | | 427 | break; |
428 | case T_ALIGNFLT|T_USER: | | 428 | case T_ALIGNFLT|T_USER: |
429 | ksi.ksi_signo = SIGBUS; | | 429 | ksi.ksi_signo = SIGBUS; |
430 | ksi.ksi_code = BUS_ADRALN; | | 430 | ksi.ksi_code = BUS_ADRALN; |
431 | break; | | 431 | break; |
432 | case T_PROTFLT|T_USER: | | 432 | case T_PROTFLT|T_USER: |
433 | ksi.ksi_signo = SIGSEGV; | | 433 | ksi.ksi_signo = SIGSEGV; |
434 | ksi.ksi_code = SEGV_ACCERR; | | 434 | ksi.ksi_code = SEGV_ACCERR; |
435 | break; | | 435 | break; |
436 | default: | | 436 | default: |
437 | KASSERT(0); | | 437 | KASSERT(0); |
438 | break; | | 438 | break; |
439 | } | | 439 | } |
440 | goto trapsignal; | | 440 | goto trapsignal; |
441 | | | 441 | |
442 | case T_PRIVINFLT|T_USER: /* privileged instruction fault */ | | 442 | case T_PRIVINFLT|T_USER: /* privileged instruction fault */ |
443 | case T_FPOPFLT|T_USER: /* coprocessor operand fault */ | | 443 | case T_FPOPFLT|T_USER: /* coprocessor operand fault */ |
444 | #ifdef TRAP_SIGDEBUG | | 444 | #ifdef TRAP_SIGDEBUG |
445 | printf("pid %d.%d (%s): ILL at rip %lx addr %lx\n", | | 445 | printf("pid %d.%d (%s): ILL at rip %#lx addr %#lx\n", |
446 | p->p_pid, l->l_lid, p->p_comm, frame->tf_rip, rcr2()); | | 446 | p->p_pid, l->l_lid, p->p_comm, frame->tf_rip, rcr2()); |
447 | frame_dump(frame); | | 447 | frame_dump(frame, pcb); |
448 | #endif | | 448 | #endif |
449 | KSI_INIT_TRAP(&ksi); | | 449 | KSI_INIT_TRAP(&ksi); |
450 | ksi.ksi_signo = SIGILL; | | 450 | ksi.ksi_signo = SIGILL; |
451 | ksi.ksi_trap = type & ~T_USER; | | 451 | ksi.ksi_trap = type & ~T_USER; |
452 | ksi.ksi_addr = (void *) frame->tf_rip; | | 452 | ksi.ksi_addr = (void *) frame->tf_rip; |
453 | switch (type) { | | 453 | switch (type) { |
454 | case T_PRIVINFLT|T_USER: | | 454 | case T_PRIVINFLT|T_USER: |
455 | ksi.ksi_code = ILL_PRVOPC; | | 455 | ksi.ksi_code = ILL_PRVOPC; |
456 | break; | | 456 | break; |
457 | case T_FPOPFLT|T_USER: | | 457 | case T_FPOPFLT|T_USER: |
458 | ksi.ksi_code = ILL_COPROC; | | 458 | ksi.ksi_code = ILL_COPROC; |
459 | break; | | 459 | break; |
460 | default: | | 460 | default: |
461 | KASSERT(0); | | 461 | KASSERT(0); |
462 | break; | | 462 | break; |
463 | } | | 463 | } |
464 | goto trapsignal; | | 464 | goto trapsignal; |
465 | | | 465 | |
466 | case T_ASTFLT|T_USER: | | 466 | case T_ASTFLT|T_USER: |
467 | /* Allow process switch. */ | | 467 | /* Allow process switch. */ |
468 | //curcpu()->ci_data.cpu_nast++; | | 468 | //curcpu()->ci_data.cpu_nast++; |
469 | if (l->l_pflag & LP_OWEUPC) { | | 469 | if (l->l_pflag & LP_OWEUPC) { |
470 | l->l_pflag &= ~LP_OWEUPC; | | 470 | l->l_pflag &= ~LP_OWEUPC; |
471 | ADDUPROF(l); | | 471 | ADDUPROF(l); |
472 | } | | 472 | } |
473 | /* Allow a forced task switch. */ | | 473 | /* Allow a forced task switch. */ |
474 | if (curcpu()->ci_want_resched) { | | 474 | if (curcpu()->ci_want_resched) { |
475 | preempt(); | | 475 | preempt(); |
476 | } | | 476 | } |
477 | goto out; | | 477 | goto out; |
478 | | | 478 | |
479 | case T_BOUND|T_USER: | | 479 | case T_BOUND|T_USER: |
480 | case T_OFLOW|T_USER: | | 480 | case T_OFLOW|T_USER: |
481 | case T_DIVIDE|T_USER: | | 481 | case T_DIVIDE|T_USER: |
482 | KSI_INIT_TRAP(&ksi); | | 482 | KSI_INIT_TRAP(&ksi); |
483 | ksi.ksi_signo = SIGFPE; | | 483 | ksi.ksi_signo = SIGFPE; |
484 | ksi.ksi_trap = type & ~T_USER; | | 484 | ksi.ksi_trap = type & ~T_USER; |
485 | ksi.ksi_addr = (void *)frame->tf_rip; | | 485 | ksi.ksi_addr = (void *)frame->tf_rip; |
486 | switch (type) { | | 486 | switch (type) { |
487 | case T_BOUND|T_USER: | | 487 | case T_BOUND|T_USER: |
488 | ksi.ksi_code = FPE_FLTSUB; | | 488 | ksi.ksi_code = FPE_FLTSUB; |
489 | break; | | 489 | break; |
490 | case T_OFLOW|T_USER: | | 490 | case T_OFLOW|T_USER: |
491 | ksi.ksi_code = FPE_INTOVF; | | 491 | ksi.ksi_code = FPE_INTOVF; |
492 | break; | | 492 | break; |
493 | case T_DIVIDE|T_USER: | | 493 | case T_DIVIDE|T_USER: |
494 | ksi.ksi_code = FPE_INTDIV; | | 494 | ksi.ksi_code = FPE_INTDIV; |
495 | break; | | 495 | break; |
496 | default: | | 496 | default: |
497 | #ifdef DIAGNOSTIC | | 497 | #ifdef DIAGNOSTIC |
498 | panic("unhandled type %x\n", type); | | 498 | panic("unhandled type %x\n", type); |
499 | #endif | | 499 | #endif |
500 | break; | | 500 | break; |
501 | } | | 501 | } |
502 | goto trapsignal; | | 502 | goto trapsignal; |
503 | | | 503 | |
504 | case T_PAGEFLT: | | 504 | case T_PAGEFLT: |
505 | /* Allow page faults in kernel mode. */ | | 505 | /* Allow page faults in kernel mode. */ |
506 | if (__predict_false(l == NULL)) | | 506 | if (__predict_false(l == NULL)) |
507 | goto we_re_toast; | | 507 | goto we_re_toast; |
508 | | | 508 | |
509 | /* | | 509 | /* |
510 | * fusuintrfailure is used by [fs]uswintr() to prevent | | 510 | * fusuintrfailure is used by [fs]uswintr() to prevent |
511 | * page faulting from inside the profiling interrupt. | | 511 | * page faulting from inside the profiling interrupt. |
512 | */ | | 512 | */ |
513 | onfault = pcb->pcb_onfault; | | 513 | onfault = pcb->pcb_onfault; |
514 | if (onfault == fusuintrfailure) { | | 514 | if (onfault == fusuintrfailure) { |
515 | goto copyefault; | | 515 | goto copyefault; |
516 | } | | 516 | } |
517 | if (cpu_intr_p() || (l->l_pflag & LP_INTR) != 0) { | | 517 | if (cpu_intr_p() || (l->l_pflag & LP_INTR) != 0) { |
518 | goto we_re_toast; | | 518 | goto we_re_toast; |
519 | } | | 519 | } |
520 | | | 520 | |
521 | cr2 = rcr2(); | | 521 | cr2 = rcr2(); |
522 | | | 522 | |
523 | if (frame->tf_err & PGEX_X) { | | 523 | if (frame->tf_err & PGEX_X) { |
524 | /* SMEP might have brought us here */ | | 524 | /* SMEP might have brought us here */ |
525 | if (cr2 > VM_MIN_ADDRESS && cr2 <= VM_MAXUSER_ADDRESS) | | 525 | if (cr2 > VM_MIN_ADDRESS && cr2 <= VM_MAXUSER_ADDRESS) |
526 | panic("prevented execution of %p (SMEP)", | | 526 | panic("prevented execution of %p (SMEP)", |
527 | (void *)cr2); | | 527 | (void *)cr2); |
528 | } | | 528 | } |
529 | | | 529 | |
530 | goto faultcommon; | | 530 | goto faultcommon; |
531 | | | 531 | |
532 | case T_PAGEFLT|T_USER: { /* page fault */ | | 532 | case T_PAGEFLT|T_USER: { /* page fault */ |
533 | register vaddr_t va; | | 533 | register vaddr_t va; |
534 | register struct vmspace *vm; | | 534 | register struct vmspace *vm; |
535 | register struct vm_map *map; | | 535 | register struct vm_map *map; |
536 | vm_prot_t ftype; | | 536 | vm_prot_t ftype; |
537 | extern struct vm_map *kernel_map; | | 537 | extern struct vm_map *kernel_map; |
538 | | | 538 | |
539 | cr2 = rcr2(); | | 539 | cr2 = rcr2(); |
540 | if (p->p_emul->e_usertrap != NULL && | | 540 | if (p->p_emul->e_usertrap != NULL && |
541 | (*p->p_emul->e_usertrap)(l, cr2, frame) != 0) | | 541 | (*p->p_emul->e_usertrap)(l, cr2, frame) != 0) |
542 | return; | | 542 | return; |
543 | faultcommon: | | 543 | faultcommon: |
544 | vm = p->p_vmspace; | | 544 | vm = p->p_vmspace; |
545 | if (__predict_false(vm == NULL)) { | | 545 | if (__predict_false(vm == NULL)) { |
546 | goto we_re_toast; | | 546 | goto we_re_toast; |
547 | } | | 547 | } |
548 | pcb->pcb_cr2 = cr2; | | 548 | pcb->pcb_cr2 = cr2; |
549 | va = trunc_page((vaddr_t)cr2); | | 549 | va = trunc_page((vaddr_t)cr2); |
550 | /* | | 550 | /* |
551 | * It is only a kernel address space fault iff: | | 551 | * It is only a kernel address space fault iff: |
552 | * 1. (type & T_USER) == 0 and | | 552 | * 1. (type & T_USER) == 0 and |
553 | * 2. pcb_onfault not set or | | 553 | * 2. pcb_onfault not set or |
554 | * 3. pcb_onfault set but supervisor space fault | | 554 | * 3. pcb_onfault set but supervisor space fault |
555 | * The last can occur during an exec() copyin where the | | 555 | * The last can occur during an exec() copyin where the |
556 | * argument space is lazy-allocated. | | 556 | * argument space is lazy-allocated. |
557 | */ | | 557 | */ |
558 | if (type == T_PAGEFLT && va >= VM_MIN_KERNEL_ADDRESS) | | 558 | if (type == T_PAGEFLT && va >= VM_MIN_KERNEL_ADDRESS) |
559 | map = kernel_map; | | 559 | map = kernel_map; |
560 | else | | 560 | else |
561 | map = &vm->vm_map; | | 561 | map = &vm->vm_map; |
562 | if (frame->tf_err & PGEX_W) | | 562 | if (frame->tf_err & PGEX_W) |
563 | ftype = VM_PROT_WRITE; | | 563 | ftype = VM_PROT_WRITE; |
564 | else if (frame->tf_err & PGEX_X) | | 564 | else if (frame->tf_err & PGEX_X) |
565 | ftype = VM_PROT_EXECUTE; | | 565 | ftype = VM_PROT_EXECUTE; |
566 | else | | 566 | else |
567 | ftype = VM_PROT_READ; | | 567 | ftype = VM_PROT_READ; |
568 | | | 568 | |
569 | #ifdef DIAGNOSTIC | | 569 | #ifdef DIAGNOSTIC |
570 | if (map == kernel_map && va == 0) { | | 570 | if (map == kernel_map && va == 0) { |
571 | printf("trap: bad kernel access at %lx\n", va); | | 571 | printf("trap: bad kernel access at %lx\n", va); |
572 | goto we_re_toast; | | 572 | goto we_re_toast; |
573 | } | | 573 | } |
574 | #endif | | 574 | #endif |
575 | /* Fault the original page in. */ | | 575 | /* Fault the original page in. */ |
576 | onfault = pcb->pcb_onfault; | | 576 | onfault = pcb->pcb_onfault; |
577 | pcb->pcb_onfault = NULL; | | 577 | pcb->pcb_onfault = NULL; |
578 | error = uvm_fault(map, va, ftype); | | 578 | error = uvm_fault(map, va, ftype); |
579 | pcb->pcb_onfault = onfault; | | 579 | pcb->pcb_onfault = onfault; |
580 | if (error == 0) { | | 580 | if (error == 0) { |
581 | if (map != kernel_map && (void *)va >= vm->vm_maxsaddr) | | 581 | if (map != kernel_map && (void *)va >= vm->vm_maxsaddr) |
582 | uvm_grow(p, va); | | 582 | uvm_grow(p, va); |
583 | | | 583 | |
584 | pfail = false; | | 584 | pfail = false; |
585 | while (type == T_PAGEFLT) { | | 585 | while (type == T_PAGEFLT) { |
586 | /* | | 586 | /* |
587 | * we need to switch pmap now if we're in | | 587 | * we need to switch pmap now if we're in |
588 | * the middle of copyin/out. | | 588 | * the middle of copyin/out. |
589 | * | | 589 | * |
590 | * but we don't need to do so for kcopy as | | 590 | * but we don't need to do so for kcopy as |
591 | * it never touch userspace. | | 591 | * it never touch userspace. |
592 | */ | | 592 | */ |
593 | kpreempt_disable(); | | 593 | kpreempt_disable(); |
594 | if (curcpu()->ci_want_pmapload) { | | 594 | if (curcpu()->ci_want_pmapload) { |
595 | onfault = onfault_handler(pcb, frame); | | 595 | onfault = onfault_handler(pcb, frame); |
596 | if (onfault != kcopy_fault) { | | 596 | if (onfault != kcopy_fault) { |
597 | pmap_load(); | | 597 | pmap_load(); |
598 | } | | 598 | } |
599 | } | | 599 | } |
600 | /* | | 600 | /* |
601 | * We need to keep the pmap loaded and | | 601 | * We need to keep the pmap loaded and |
602 | * so avoid being preempted until back | | 602 | * so avoid being preempted until back |
603 | * into the copy functions. Disable | | 603 | * into the copy functions. Disable |
604 | * interrupts at the hardware level before | | 604 | * interrupts at the hardware level before |
605 | * re-enabling preemption. Interrupts | | 605 | * re-enabling preemption. Interrupts |
606 | * will be re-enabled by 'iret' when | | 606 | * will be re-enabled by 'iret' when |
607 | * returning back out of the trap stub. | | 607 | * returning back out of the trap stub. |
608 | * They'll only be re-enabled when the | | 608 | * They'll only be re-enabled when the |
609 | * program counter is once again in | | 609 | * program counter is once again in |
610 | * the copy functions, and so visible | | 610 | * the copy functions, and so visible |
611 | * to cpu_kpreempt_exit(). | | 611 | * to cpu_kpreempt_exit(). |
612 | */ | | 612 | */ |
613 | #ifndef XEN | | 613 | #ifndef XEN |
614 | x86_disable_intr(); | | 614 | x86_disable_intr(); |
615 | #endif | | 615 | #endif |
616 | l->l_nopreempt--; | | 616 | l->l_nopreempt--; |
617 | if (l->l_nopreempt > 0 || !l->l_dopreempt || | | 617 | if (l->l_nopreempt > 0 || !l->l_dopreempt || |
618 | pfail) { | | 618 | pfail) { |
619 | return; | | 619 | return; |
620 | } | | 620 | } |
621 | #ifndef XEN | | 621 | #ifndef XEN |
622 | x86_enable_intr(); | | 622 | x86_enable_intr(); |
623 | #endif | | 623 | #endif |
624 | /* | | 624 | /* |
625 | * If preemption fails for some reason, | | 625 | * If preemption fails for some reason, |
626 | * don't retry it. The conditions won't | | 626 | * don't retry it. The conditions won't |
627 | * change under our nose. | | 627 | * change under our nose. |
628 | */ | | 628 | */ |
629 | pfail = kpreempt(0); | | 629 | pfail = kpreempt(0); |
630 | } | | 630 | } |
631 | goto out; | | 631 | goto out; |
632 | } | | 632 | } |
633 | | | 633 | |
634 | if (type == T_PAGEFLT) { | | 634 | if (type == T_PAGEFLT) { |
635 | onfault = onfault_handler(pcb, frame); | | 635 | onfault = onfault_handler(pcb, frame); |
636 | if (onfault != NULL) | | 636 | if (onfault != NULL) |
637 | goto copyfault; | | 637 | goto copyfault; |
638 | printf("uvm_fault(%p, 0x%lx, %d) -> %x\n", | | 638 | printf("uvm_fault(%p, 0x%lx, %d) -> %x\n", |
639 | map, va, ftype, error); | | 639 | map, va, ftype, error); |
640 | goto kernelfault; | | 640 | goto kernelfault; |
641 | } | | 641 | } |
642 | | | 642 | |
643 | KSI_INIT_TRAP(&ksi); | | 643 | KSI_INIT_TRAP(&ksi); |
644 | ksi.ksi_trap = type & ~T_USER; | | 644 | ksi.ksi_trap = type & ~T_USER; |
645 | ksi.ksi_addr = (void *)cr2; | | 645 | ksi.ksi_addr = (void *)cr2; |
646 | switch (error) { | | 646 | switch (error) { |
647 | case EINVAL: | | 647 | case EINVAL: |
648 | ksi.ksi_signo = SIGBUS; | | 648 | ksi.ksi_signo = SIGBUS; |
649 | ksi.ksi_code = BUS_ADRERR; | | 649 | ksi.ksi_code = BUS_ADRERR; |
650 | break; | | 650 | break; |
651 | case EACCES: | | 651 | case EACCES: |
652 | ksi.ksi_signo = SIGSEGV; | | 652 | ksi.ksi_signo = SIGSEGV; |
653 | ksi.ksi_code = SEGV_ACCERR; | | 653 | ksi.ksi_code = SEGV_ACCERR; |
654 | error = EFAULT; | | 654 | error = EFAULT; |
655 | break; | | 655 | break; |
656 | case ENOMEM: | | 656 | case ENOMEM: |
657 | ksi.ksi_signo = SIGKILL; | | 657 | ksi.ksi_signo = SIGKILL; |
658 | printf("UVM: pid %d.%d (%s), uid %d killed: " | | 658 | printf("UVM: pid %d.%d (%s), uid %d killed: " |
659 | "out of swap\n", p->p_pid, l->l_lid, p->p_comm, | | 659 | "out of swap\n", p->p_pid, l->l_lid, p->p_comm, |
660 | l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1); | | 660 | l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1); |
661 | break; | | 661 | break; |
662 | default: | | 662 | default: |
663 | ksi.ksi_signo = SIGSEGV; | | 663 | ksi.ksi_signo = SIGSEGV; |
664 | ksi.ksi_code = SEGV_MAPERR; | | 664 | ksi.ksi_code = SEGV_MAPERR; |
665 | break; | | 665 | break; |
666 | } | | 666 | } |
667 | | | 667 | |
668 | #ifdef TRAP_SIGDEBUG | | 668 | #ifdef TRAP_SIGDEBUG |
669 | printf("pid %d.%d (%s): signal %d at rip %#lx addr %#lx " | | 669 | printf("pid %d.%d (%s): signal %d at rip %#lx addr %#lx " |
670 | "error %d trap %d cr2 %p\n", p->p_pid, l->l_lid, p->p_comm, | | 670 | "error %d trap %d cr2 %p\n", p->p_pid, l->l_lid, p->p_comm, |
671 | ksi.ksi_signo, frame->tf_rip, va, error, ksi.ksi_trap, | | 671 | ksi.ksi_signo, frame->tf_rip, va, error, ksi.ksi_trap, |
672 | ksi.ksi_addr); | | 672 | ksi.ksi_addr); |
673 | frame_dump(frame); | | 673 | frame_dump(frame, pcb); |
674 | #endif | | 674 | #endif |
675 | (*p->p_emul->e_trapsignal)(l, &ksi); | | 675 | (*p->p_emul->e_trapsignal)(l, &ksi); |
676 | break; | | 676 | break; |
677 | } | | 677 | } |
678 | | | 678 | |
679 | case T_TRCTRAP: | | 679 | case T_TRCTRAP: |
680 | /* | | 680 | /* |
681 | * Ignore debug register trace traps due to | | 681 | * Ignore debug register trace traps due to |
682 | * accesses in the user's address space, which | | 682 | * accesses in the user's address space, which |
683 | * can happen under several conditions such as | | 683 | * can happen under several conditions such as |
684 | * if a user sets a watchpoint on a buffer and | | 684 | * if a user sets a watchpoint on a buffer and |
685 | * then passes that buffer to a system call. | | 685 | * then passes that buffer to a system call. |
686 | * We still want to get TRCTRAPS for addresses | | 686 | * We still want to get TRCTRAPS for addresses |
687 | * in kernel space because that is useful when | | 687 | * in kernel space because that is useful when |
688 | * debugging the kernel. | | 688 | * debugging the kernel. |
689 | */ | | 689 | */ |
690 | if (x86_dbregs_user_trap()) | | 690 | if (x86_dbregs_user_trap()) |
691 | break; | | 691 | break; |
692 | | | 692 | |
693 | /* Check whether they single-stepped into a lcall. */ | | 693 | /* Check whether they single-stepped into a lcall. */ |
694 | if (frame->tf_rip == (uint64_t)IDTVEC(oosyscall) || | | 694 | if (frame->tf_rip == (uint64_t)IDTVEC(oosyscall) || |
695 | frame->tf_rip == (uint64_t)IDTVEC(osyscall) || | | 695 | frame->tf_rip == (uint64_t)IDTVEC(osyscall) || |
696 | frame->tf_rip == (uint64_t)IDTVEC(syscall32)) { | | 696 | frame->tf_rip == (uint64_t)IDTVEC(syscall32)) { |
697 | frame->tf_rflags &= ~PSL_T; | | 697 | frame->tf_rflags &= ~PSL_T; |
698 | return; | | 698 | return; |
699 | } | | 699 | } |
700 | goto we_re_toast; | | 700 | goto we_re_toast; |
701 | | | 701 | |
702 | case T_BPTFLT|T_USER: /* bpt instruction fault */ | | 702 | case T_BPTFLT|T_USER: /* bpt instruction fault */ |
703 | case T_TRCTRAP|T_USER: /* trace trap */ | | 703 | case T_TRCTRAP|T_USER: /* trace trap */ |
704 | /* | | 704 | /* |
705 | * Don't go single-stepping into a RAS. | | 705 | * Don't go single-stepping into a RAS. |
706 | */ | | 706 | */ |
707 | | | 707 | |
708 | if (p->p_raslist == NULL || | | 708 | if (p->p_raslist == NULL || |
709 | (ras_lookup(p, (void *)frame->tf_rip) == (void *)-1)) { | | 709 | (ras_lookup(p, (void *)frame->tf_rip) == (void *)-1)) { |
710 | KSI_INIT_TRAP(&ksi); | | 710 | KSI_INIT_TRAP(&ksi); |
711 | ksi.ksi_signo = SIGTRAP; | | 711 | ksi.ksi_signo = SIGTRAP; |
712 | ksi.ksi_trap = type & ~T_USER; | | 712 | ksi.ksi_trap = type & ~T_USER; |
713 | if (x86_dbregs_user_trap()) { | | 713 | if (x86_dbregs_user_trap()) { |
714 | x86_dbregs_store_dr6(l); | | 714 | x86_dbregs_store_dr6(l); |
715 | ksi.ksi_code = TRAP_DBREG; | | 715 | ksi.ksi_code = TRAP_DBREG; |
716 | } else if (type == (T_BPTFLT|T_USER)) | | 716 | } else if (type == (T_BPTFLT|T_USER)) |
717 | ksi.ksi_code = TRAP_BRKPT; | | 717 | ksi.ksi_code = TRAP_BRKPT; |
718 | else | | 718 | else |
719 | ksi.ksi_code = TRAP_TRACE; | | 719 | ksi.ksi_code = TRAP_TRACE; |
720 | (*p->p_emul->e_trapsignal)(l, &ksi); | | 720 | (*p->p_emul->e_trapsignal)(l, &ksi); |
721 | } | | 721 | } |
722 | break; | | 722 | break; |
723 | | | 723 | |
724 | case T_NMI: | | 724 | case T_NMI: |
725 | if (nmi_dispatch(frame)) | | 725 | if (nmi_dispatch(frame)) |
726 | return; | | 726 | return; |
727 | /* NMI can be hooked up to a pushbutton for debugging */ | | 727 | /* NMI can be hooked up to a pushbutton for debugging */ |
728 | if (kgdb_trap(type, frame)) | | 728 | if (kgdb_trap(type, frame)) |
729 | return; | | 729 | return; |
730 | if (kdb_trap(type, 0, frame)) | | 730 | if (kdb_trap(type, 0, frame)) |
731 | return; | | 731 | return; |
732 | /* machine/parity/power fail/"kitchen sink" faults */ | | 732 | /* machine/parity/power fail/"kitchen sink" faults */ |
733 | | | 733 | |
734 | x86_nmi(); | | 734 | x86_nmi(); |
735 | return; | | 735 | return; |
736 | } | | 736 | } |
737 | | | 737 | |
738 | if ((type & T_USER) == 0) | | 738 | if ((type & T_USER) == 0) |
739 | return; | | 739 | return; |
740 | out: | | 740 | out: |
741 | userret(l); | | 741 | userret(l); |
742 | return; | | 742 | return; |
743 | trapsignal: | | 743 | trapsignal: |
744 | (*p->p_emul->e_trapsignal)(l, &ksi); | | 744 | (*p->p_emul->e_trapsignal)(l, &ksi); |
745 | userret(l); | | 745 | userret(l); |
746 | } | | 746 | } |
747 | | | 747 | |
748 | /* | | 748 | /* |
749 | * startlwp: start of a new LWP. | | 749 | * startlwp: start of a new LWP. |
750 | */ | | 750 | */ |
751 | void | | 751 | void |
752 | startlwp(void *arg) | | 752 | startlwp(void *arg) |
753 | { | | 753 | { |
754 | ucontext_t *uc = arg; | | 754 | ucontext_t *uc = arg; |
755 | lwp_t *l = curlwp; | | 755 | lwp_t *l = curlwp; |
756 | int error __diagused; | | 756 | int error __diagused; |
757 | | | 757 | |
758 | error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags); | | 758 | error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags); |
759 | KASSERT(error == 0); | | 759 | KASSERT(error == 0); |
760 | | | 760 | |
761 | kmem_free(uc, sizeof(ucontext_t)); | | 761 | kmem_free(uc, sizeof(ucontext_t)); |
762 | userret(l); | | 762 | userret(l); |
763 | } | | 763 | } |
764 | | | 764 | |
765 | #ifdef TRAP_SIGDEBUG | | 765 | #ifdef TRAP_SIGDEBUG |
766 | static void | | 766 | void |
767 | frame_dump(struct trapframe *tf) | | 767 | frame_dump(struct trapframe *tf, struct pcb *pcb) |
768 | { | | 768 | { |
769 | int i; | | 769 | int i; |
770 | unsigned long *p; | | 770 | unsigned long *p; |
771 | | | 771 | |
772 | printf("rip %p rsp %p rfl %p\n", | | 772 | printf("trapframe %p\n", tf); |
773 | (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)tf->tf_rflags); | | 773 | printf("rip 0x%016lx rsp 0x%016lx rfl 0x%016lx\n", |
774 | printf("rdi %p rsi %p rdx %p\n", | | 774 | tf->tf_rip, tf->tf_rsp, tf->tf_rflags); |
775 | (void *)tf->tf_rdi, (void *)tf->tf_rsi, (void *)tf->tf_rdx); | | 775 | printf("rdi 0x%016lx rsi 0x%016lx rdx 0x%016lx\n", |
776 | printf("rcx %p r8 %p r9 %p\n", | | 776 | tf->tf_rdi, tf->tf_rsi, tf->tf_rdx); |
777 | (void *)tf->tf_rcx, (void *)tf->tf_r8, (void *)tf->tf_r9); | | 777 | printf("rcx 0x%016lx r8 0x%016lx r9 0x%016lx\n", |
778 | printf("r10 %p r11 %p r12 %p\n", | | 778 | tf->tf_rcx, tf->tf_r8, tf->tf_r9); |
779 | (void *)tf->tf_r10, (void *)tf->tf_r11, (void *)tf->tf_r12); | | 779 | printf("r10 0x%016lx r11 0x%016lx r12 0x%016lx\n", |
780 | printf("r13 %p r14 %p r15 %p\n", | | 780 | tf->tf_r10, tf->tf_r11, tf->tf_r12); |
781 | (void *)tf->tf_r13, (void *)tf->tf_r14, (void *)tf->tf_r15); | | 781 | printf("r13 0x%016lx r14 0x%016lx r15 0x%016lx\n", |
782 | printf("rbp %p rbx %p rax %p\n", | | 782 | tf->tf_r13, tf->tf_r14, tf->tf_r15); |
783 | (void *)tf->tf_rbp, (void *)tf->tf_rbx, (void *)tf->tf_rax); | | 783 | printf("rbp 0x%016lx rbx 0x%016lx rax 0x%016lx\n", |
784 | printf("cs %lx ds %lx es %lx fs %lx gs %lx ss %lx\n", | | 784 | tf->tf_rbp, tf->tf_rbx, tf->tf_rax); |
| | | 785 | printf("cs 0x%04lx ds 0x%04lx es 0x%04lx " |
| | | 786 | "fs 0x%04lx gs 0x%04lx ss 0x%04lx\n", |
785 | tf->tf_cs & 0xffff, tf->tf_ds & 0xffff, tf->tf_es & 0xffff, | | 787 | tf->tf_cs & 0xffff, tf->tf_ds & 0xffff, tf->tf_es & 0xffff, |
786 | tf->tf_fs & 0xffff, tf->tf_gs & 0xffff, tf->tf_ss & 0xffff); | | 788 | tf->tf_fs & 0xffff, tf->tf_gs & 0xffff, tf->tf_ss & 0xffff); |
787 | | | 789 | printf("fsbase 0x%016lx gsbase 0x%016lx\n", |
| | | 790 | pcb->pcb_fs, pcb->pcb_gs); |
788 | printf("\n"); | | 791 | printf("\n"); |
789 | printf("Stack dump:\n"); | | 792 | printf("Stack dump:\n"); |
790 | for (i = 0, p = (unsigned long *) tf; i < 20; i ++, p += 4) | | 793 | for (i = 0, p = (unsigned long *) tf; i < 20; i ++, p += 4) |
791 | printf(" 0x%.16lx 0x%.16lx 0x%.16lx 0x%.16lx\n", *p, p[1], p[2], p[3]); | | 794 | printf(" 0x%.16lx 0x%.16lx 0x%.16lx 0x%.16lx\n", |
| | | 795 | p[0], p[1], p[2], p[3]); |
792 | printf("\n"); | | 796 | printf("\n"); |
793 | } | | 797 | } |
794 | #endif | | 798 | #endif |