kernel - Make pcb_onfault more robust.
[dragonfly.git] / sys / platform / pc32 / i386 / trap.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * 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 the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
38 * $FreeBSD: src/sys/i386/i386/trap.c,v 1.147.2.11 2003/02/27 19:09:59 luoqi Exp $
39 */
40
41/*
42 * 386 Trap and System call handling
43 */
44
1f2de5d4
MD
45#include "use_isa.h"
46#include "use_npx.h"
47
984263bc
MD
48#include "opt_cpu.h"
49#include "opt_ddb.h"
50#include "opt_ktrace.h"
51#include "opt_clock.h"
52#include "opt_trap.h"
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/proc.h>
57#include <sys/pioctl.h>
58#include <sys/kernel.h>
5ea440eb 59#include <sys/kerneldump.h>
984263bc
MD
60#include <sys/resourcevar.h>
61#include <sys/signalvar.h>
b1b4e5a6 62#include <sys/signal2.h>
984263bc
MD
63#include <sys/syscall.h>
64#include <sys/sysctl.h>
65#include <sys/sysent.h>
66#include <sys/uio.h>
67#include <sys/vmmeter.h>
4fd10eb6 68#include <sys/malloc.h>
984263bc
MD
69#ifdef KTRACE
70#include <sys/ktrace.h>
71#endif
80bbc910 72#include <sys/ktr.h>
a722be49 73#include <sys/upcall.h>
4a22e893 74#include <sys/vkernel.h>
a64ba182
MD
75#include <sys/sysproto.h>
76#include <sys/sysunion.h>
984263bc
MD
77
78#include <vm/vm.h>
79#include <vm/vm_param.h>
80#include <sys/lock.h>
81#include <vm/pmap.h>
82#include <vm/vm_kern.h>
83#include <vm/vm_map.h>
84#include <vm/vm_page.h>
85#include <vm/vm_extern.h>
86
87#include <machine/cpu.h>
984263bc
MD
88#include <machine/md_var.h>
89#include <machine/pcb.h>
984263bc 90#include <machine/smp.h>
984263bc 91#include <machine/tss.h>
431d0fef 92#include <machine/specialreg.h>
85100692 93#include <machine/globaldata.h>
87cf6827 94#include <machine/intr_machdep.h>
984263bc 95
87cf6827 96#include <machine_base/isa/isa_intr.h>
cb7d6921 97#include <machine_base/apic/lapic.h>
984263bc
MD
98
99#ifdef POWERFAIL_NMI
100#include <sys/syslog.h>
101#include <machine/clock.h>
102#endif
103
104#include <machine/vm86.h>
105
106#include <ddb/ddb.h>
684a93c4 107
245e4f17 108#include <sys/msgport2.h>
41c20dac 109#include <sys/thread2.h>
684a93c4 110#include <sys/mplock2.h>
984263bc 111
9753ab6c
MD
112#ifdef SMP
113
114#define MAKEMPSAFE(have_mplock) \
115 if (have_mplock == 0) { \
116 get_mplock(); \
117 have_mplock = 1; \
118 }
119
120#else
121
122#define MAKEMPSAFE(have_mplock)
123
124#endif
125
3ae0cd58 126int (*pmath_emulate) (struct trapframe *);
984263bc 127
c7eb0589 128extern void trap (struct trapframe *frame);
c7eb0589 129extern void syscall2 (struct trapframe *frame);
984263bc 130
3ae0cd58
RG
131static int trap_pfault (struct trapframe *, int, vm_offset_t);
132static void trap_fatal (struct trapframe *, vm_offset_t);
133void dblfault_handler (void);
984263bc
MD
134
135extern inthand_t IDTVEC(syscall);
136
137#define MAX_TRAP_MSG 28
138static char *trap_msg[] = {
139 "", /* 0 unused */
140 "privileged instruction fault", /* 1 T_PRIVINFLT */
141 "", /* 2 unused */
142 "breakpoint instruction fault", /* 3 T_BPTFLT */
143 "", /* 4 unused */
144 "", /* 5 unused */
145 "arithmetic trap", /* 6 T_ARITHTRAP */
146 "system forced exception", /* 7 T_ASTFLT */
147 "", /* 8 unused */
148 "general protection fault", /* 9 T_PROTFLT */
149 "trace trap", /* 10 T_TRCTRAP */
150 "", /* 11 unused */
151 "page fault", /* 12 T_PAGEFLT */
152 "", /* 13 unused */
153 "alignment fault", /* 14 T_ALIGNFLT */
154 "", /* 15 unused */
155 "", /* 16 unused */
156 "", /* 17 unused */
157 "integer divide fault", /* 18 T_DIVIDE */
158 "non-maskable interrupt trap", /* 19 T_NMI */
159 "overflow trap", /* 20 T_OFLOW */
160 "FPU bounds check fault", /* 21 T_BOUND */
161 "FPU device not available", /* 22 T_DNA */
162 "double fault", /* 23 T_DOUBLEFLT */
163 "FPU operand fetch fault", /* 24 T_FPOPFLT */
164 "invalid TSS fault", /* 25 T_TSSFLT */
165 "segment not present fault", /* 26 T_SEGNPFLT */
166 "stack fault", /* 27 T_STKFLT */
167 "machine check trap", /* 28 T_MCHK */
168};
169
984263bc
MD
170#if defined(I586_CPU) && !defined(NO_F00F_HACK)
171extern int has_f00f_bug;
172#endif
173
174#ifdef DDB
175static int ddb_on_nmi = 1;
176SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
177 &ddb_on_nmi, 0, "Go to DDB on NMI");
178#endif
179static int panic_on_nmi = 1;
180SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
181 &panic_on_nmi, 0, "Panic on NMI");
d9eea1a5
MD
182static int fast_release;
183SYSCTL_INT(_machdep, OID_AUTO, fast_release, CTLFLAG_RW,
184 &fast_release, 0, "Passive Release was optimal");
185static int slow_release;
186SYSCTL_INT(_machdep, OID_AUTO, slow_release, CTLFLAG_RW,
187 &slow_release, 0, "Passive Release was nonoptimal");
984263bc 188
4fd10eb6 189MALLOC_DEFINE(M_SYSMSG, "sysmsg", "sysmsg structure");
7062f5b4 190extern int max_sysmsg;
4fd10eb6 191
26a0694b 192/*
7bd858e5
MD
193 * Passively intercepts the thread switch function to increase the thread
194 * priority from a user priority to a kernel priority, reducing
d81ccc3e 195 * syscall and trap overhead for the case where no switch occurs.
7bd858e5 196 *
d86a23e0 197 * Synchronizes td_ucred with p_ucred. This is used by system calls,
7bd858e5
MD
198 * signal handling, faults, AST traps, and anything else that enters the
199 * kernel from userland and provides the kernel with a stable read-only
200 * copy of the process ucred.
26a0694b
MD
201 */
202static __inline void
d86a23e0 203userenter(struct thread *curtd, struct proc *curp)
26a0694b 204{
7bd858e5
MD
205 struct ucred *ocred;
206 struct ucred *ncred;
207
3824f392 208 curtd->td_release = lwkt_passive_release;
7bd858e5 209
d86a23e0
MD
210 if (curtd->td_ucred != curp->p_ucred) {
211 ncred = crhold(curp->p_ucred);
212 ocred = curtd->td_ucred;
213 curtd->td_ucred = ncred;
7bd858e5
MD
214 if (ocred)
215 crfree(ocred);
216 }
217
26a0694b
MD
218}
219
0a3f9b47 220/*
8ec60c3f
MD
221 * Handle signals, upcalls, profiling, and other AST's and/or tasks that
222 * must be completed before we can return to or try to return to userland.
0a3f9b47 223 *
8ec60c3f
MD
224 * Note that td_sticks is a 64 bit quantity, but there's no point doing 64
225 * arithmatic on the delta calculation so the absolute tick values are
226 * truncated to an integer.
37af14fe 227 */
a2a5ad0d 228static void
81eea9f4 229userret(struct lwp *lp, struct trapframe *frame, int sticks)
a2a5ad0d 230{
81eea9f4 231 struct proc *p = lp->lwp_proc;
8ba5f7ef 232 void (*hook)(void);
a2a5ad0d
MD
233 int sig;
234
8ba5f7ef
AH
235 if (p->p_userret != NULL) {
236 hook = p->p_userret;
237 p->p_userret = NULL;
238 (*hook)();
239 }
240
a2a5ad0d 241 /*
344ad853
MD
242 * Charge system time if profiling. Note: times are in microseconds.
243 * This may do a copyout and block, so do it first even though it
244 * means some system time will be charged as user time.
245 */
4643740a 246 if (p->p_flags & P_PROFIL) {
344ad853 247 addupc_task(p, frame->tf_eip,
bb3cd951 248 (u_int)((int)lp->lwp_thread->td_sticks - sticks));
344ad853
MD
249 }
250
251recheck:
252 /*
70d3d461
SS
253 * If the jungle wants us dead, so be it.
254 */
4643740a 255 if (lp->lwp_mpflags & LWP_MP_WEXIT) {
b5c4d81f 256 lwkt_gettoken(&p->p_token);
e3161323 257 lwp_exit(0);
b5c4d81f 258 lwkt_reltoken(&p->p_token); /* NOT REACHED */
e207d1fd 259 }
70d3d461
SS
260
261 /*
344ad853
MD
262 * Block here if we are in a stopped state.
263 */
5ea440eb 264 if (p->p_stat == SSTOP || dump_stop_usertds) {
4643740a 265 lwkt_gettoken(&p->p_token);
9a379a4a 266 tstop();
4643740a 267 lwkt_reltoken(&p->p_token);
344ad853
MD
268 goto recheck;
269 }
270
271 /*
4e7c41c5
MD
272 * Post any pending upcalls. If running a virtual kernel be sure
273 * to restore the virtual kernel's vmspace before posting the upcall.
a722be49 274 */
4643740a 275 if (p->p_flags & (P_SIGVTALRM | P_SIGPROF | P_UPCALLPEND)) {
fc509460 276 lwkt_gettoken(&p->p_token);
4643740a
MD
277 if (p->p_flags & P_SIGVTALRM) {
278 p->p_flags &= ~P_SIGVTALRM;
898e34b3
MD
279 ksignal(p, SIGVTALRM);
280 }
4643740a
MD
281 if (p->p_flags & P_SIGPROF) {
282 p->p_flags &= ~P_SIGPROF;
898e34b3
MD
283 ksignal(p, SIGPROF);
284 }
4643740a
MD
285 if (p->p_flags & P_UPCALLPEND) {
286 p->p_flags &= ~P_UPCALLPEND;
898e34b3
MD
287 postupcall(lp);
288 }
fc509460 289 lwkt_reltoken(&p->p_token);
344ad853 290 goto recheck;
a722be49
MD
291 }
292
293 /*
4e7c41c5
MD
294 * Post any pending signals. If running a virtual kernel be sure
295 * to restore the virtual kernel's vmspace before posting the signal.
2883d2d8
MD
296 *
297 * WARNING! postsig() can exit and not return.
a2a5ad0d 298 */
e473f776 299 if ((sig = CURSIG_TRACE(lp)) != 0) {
fc509460 300 lwkt_gettoken(&p->p_token);
a2a5ad0d 301 postsig(sig);
fc509460 302 lwkt_reltoken(&p->p_token);
344ad853 303 goto recheck;
a2a5ad0d 304 }
26a0694b
MD
305
306 /*
344ad853
MD
307 * block here if we are swapped out, but still process signals
308 * (such as SIGKILL). proc0 (the swapin scheduler) is already
309 * aware of our situation, we do not have to wake it up.
984263bc 310 */
4643740a 311 if (p->p_flags & P_SWAPPEDOUT) {
616516c8 312 lwkt_gettoken(&p->p_token);
144ce500 313 get_mplock();
4643740a 314 p->p_flags |= P_SWAPWAIT;
344ad853 315 swapin_request();
4643740a 316 if (p->p_flags & P_SWAPWAIT)
344ad853 317 tsleep(p, PCATCH, "SWOUT", 0);
4643740a 318 p->p_flags &= ~P_SWAPWAIT;
144ce500 319 rel_mplock();
616516c8 320 lwkt_reltoken(&p->p_token);
344ad853 321 goto recheck;
984263bc 322 }
ae7cb1b5
NT
323
324 /*
6562e2d8
MD
325 * In a multi-threaded program it is possible for a thread to change
326 * signal state during a system call which temporarily changes the
327 * signal mask. In this case postsig() might not be run and we
328 * have to restore the mask ourselves.
ae7cb1b5 329 */
6562e2d8
MD
330 if (lp->lwp_flags & LWP_OLDMASK) {
331 lp->lwp_flags &= ~LWP_OLDMASK;
332 lp->lwp_sigmask = lp->lwp_oldsigmask;
333 goto recheck;
334 }
984263bc
MD
335}
336
8ec60c3f
MD
337/*
338 * Cleanup from userenter and any passive release that might have occured.
339 * We must reclaim the current-process designation before we can return
340 * to usermode. We also handle both LWKT and USER reschedule requests.
341 */
342static __inline void
81eea9f4 343userexit(struct lwp *lp)
8ec60c3f 344{
81eea9f4 345 struct thread *td = lp->lwp_thread;
8397969c 346 /* globaldata_t gd = td->td_gd; */
8ec60c3f 347
8ec60c3f 348 /*
b9eb1c19
MD
349 * Handle stop requests at kernel priority. Any requests queued
350 * after this loop will generate another AST.
8ec60c3f 351 */
b9eb1c19 352 while (lp->lwp_proc->p_stat == SSTOP) {
4643740a 353 lwkt_gettoken(&lp->lwp_proc->p_token);
b9eb1c19 354 tstop();
4643740a 355 lwkt_reltoken(&lp->lwp_proc->p_token);
fede0c7f
NT
356 }
357
8ec60c3f 358 /*
b9eb1c19
MD
359 * Become the current user scheduled process if we aren't already,
360 * and deal with reschedule requests and other factors.
361 */
362 lp->lwp_proc->p_usched->acquire_curproc(lp);
363 /* WARNING: we may have migrated cpu's */
364 /* gd = td->td_gd; */
e3e6be1f
MD
365
366 /*
367 * Reduce our priority in preparation for a return to userland. If
368 * our passive release function was still in place, our priority was
369 * never raised and does not need to be reduced.
370 */
371 lwkt_passive_recover(td);
8ec60c3f
MD
372}
373
80bbc910
SS
374#if !defined(KTR_KERNENTRY)
375#define KTR_KERNENTRY KTR_ALL
376#endif
377KTR_INFO_MASTER(kernentry);
5bf48697
AE
378KTR_INFO(KTR_KERNENTRY, kernentry, trap, 0,
379 "TRAP(pid %d, tid %d, trapno %d, eva %lu)",
380 pid_t pid, lwpid_t tid, register_t trapno, vm_offset_t eva);
381KTR_INFO(KTR_KERNENTRY, kernentry, trap_ret, 0, "TRAP_RET(pid %d, tid %d)",
382 pid_t pid, lwpid_t tid);
383KTR_INFO(KTR_KERNENTRY, kernentry, syscall, 0, "SYSC(pid %d, tid %d, nr %d)",
384 pid_t pid, lwpid_t tid, register_t trapno);
385KTR_INFO(KTR_KERNENTRY, kernentry, syscall_ret, 0, "SYSRET(pid %d, tid %d, err %d)",
386 pid_t pid, lwpid_t tid, int err);
387KTR_INFO(KTR_KERNENTRY, kernentry, fork_ret, 0, "FORKRET(pid %d, tid %d)",
388 pid_t pid, lwpid_t tid);
80bbc910 389
984263bc 390/*
7517cb03 391 * Exception, fault, and trap interface to the kernel.
984263bc
MD
392 * This common code is called from assembly language IDT gate entry
393 * routines that prepare a suitable stack frame, and restore this
394 * frame after the exception has been processed.
a2a5ad0d
MD
395 *
396 * This function is also called from doreti in an interlock to handle ASTs.
397 * For example: hardwareint->INTROUTINE->(set ast)->doreti->trap
398 *
399 * NOTE! We have to retrieve the fault address prior to obtaining the
400 * MP lock because get_mplock() may switch out. YYY cr2 really ought
401 * to be retrieved by the assembly code, not here.
27e88a6e
MD
402 *
403 * XXX gd_trap_nesting_level currently prevents lwkt_switch() from panicing
404 * if an attempt is made to switch from a fast interrupt or IPI. This is
405 * necessary to properly take fatal kernel traps on SMP machines if
406 * get_mplock() has to block.
984263bc 407 */
9753ab6c 408
984263bc 409void
c7eb0589 410trap(struct trapframe *frame)
984263bc 411{
27e88a6e
MD
412 struct globaldata *gd = mycpu;
413 struct thread *td = gd->gd_curthread;
81eea9f4 414 struct lwp *lp = td->td_lwp;
7966cb69 415 struct proc *p;
37af14fe 416 int sticks = 0;
984263bc 417 int i = 0, ucode = 0, type, code;
9753ab6c
MD
418#ifdef SMP
419 int have_mplock = 0;
420#endif
7527bc81 421#ifdef INVARIANTS
f9235b6d 422 int crit_count = td->td_critcount;
3933a3ab 423 lwkt_tokref_t curstop = td->td_toks_stop;
7527bc81 424#endif
984263bc
MD
425 vm_offset_t eva;
426
7966cb69 427 p = td->td_proc;
984263bc 428#ifdef DDB
bfb6d329
AH
429 /*
430 * We need to allow T_DNA faults when the debugger is active since
2921c824 431 * some dumping paths do large bcopy() which use the floating
bfb6d329
AH
432 * point registers for faster copying.
433 */
5ea440eb 434 if (db_active && frame->tf_trapno != T_DNA) {
c7eb0589 435 eva = (frame->tf_trapno == T_PAGEFLT ? rcr2() : 0);
27e88a6e 436 ++gd->gd_trap_nesting_level;
9753ab6c 437 MAKEMPSAFE(have_mplock);
c7eb0589 438 trap_fatal(frame, eva);
27e88a6e 439 --gd->gd_trap_nesting_level;
8a8d5d85 440 goto out2;
984263bc
MD
441 }
442#endif
443
a2a5ad0d 444 eva = 0;
27e88a6e 445 ++gd->gd_trap_nesting_level;
c7eb0589 446 if (frame->tf_trapno == T_PAGEFLT) {
a2a5ad0d
MD
447 /*
448 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
449 * This problem is worked around by using an interrupt
450 * gate for the pagefault handler. We are finally ready
451 * to read %cr2 and then must reenable interrupts.
452 *
453 * XXX this should be in the switch statement, but the
454 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
455 * flow of control too much for this to be obviously
456 * correct.
457 */
458 eva = rcr2();
a2a5ad0d 459 cpu_enable_intr();
a2a5ad0d 460 }
80bbc910 461
27e88a6e 462 --gd->gd_trap_nesting_level;
a2a5ad0d 463
c7eb0589 464 if (!(frame->tf_eflags & PSL_I)) {
984263bc
MD
465 /*
466 * Buggy application or kernel code has disabled interrupts
467 * and then trapped. Enabling interrupts now is wrong, but
468 * it is better than running with interrupts disabled until
469 * they are accidentally enabled later.
470 */
c7eb0589
SS
471 type = frame->tf_trapno;
472 if (ISPL(frame->tf_cs)==SEL_UPL || (frame->tf_eflags & PSL_VM)) {
9753ab6c 473 MAKEMPSAFE(have_mplock);
26be20a0 474 kprintf(
984263bc
MD
475 "pid %ld (%s): trap %d with interrupts disabled\n",
476 (long)curproc->p_pid, curproc->p_comm, type);
a2a5ad0d 477 } else if (type != T_BPTFLT && type != T_TRCTRAP) {
984263bc
MD
478 /*
479 * XXX not quite right, since this may be for a
480 * multiple fault in user mode.
481 */
9753ab6c 482 MAKEMPSAFE(have_mplock);
26be20a0 483 kprintf("kernel trap %d with interrupts disabled\n",
984263bc 484 type);
a2a5ad0d 485 }
8a8d5d85 486 cpu_enable_intr();
984263bc
MD
487 }
488
984263bc
MD
489#if defined(I586_CPU) && !defined(NO_F00F_HACK)
490restart:
491#endif
c7eb0589
SS
492 type = frame->tf_trapno;
493 code = frame->tf_err;
984263bc
MD
494
495 if (in_vm86call) {
c7eb0589 496 if (frame->tf_eflags & PSL_VM &&
984263bc 497 (type == T_PROTFLT || type == T_STKFLT)) {
96728c05 498#ifdef SMP
b5d16701 499 KKASSERT(get_mplock_count(curthread) > 0);
96728c05 500#endif
c7eb0589 501 i = vm86_emulate((struct vm86frame *)frame);
96728c05 502#ifdef SMP
b5d16701 503 KKASSERT(get_mplock_count(curthread) > 0);
96728c05 504#endif
8a8d5d85 505 if (i != 0) {
984263bc
MD
506 /*
507 * returns to original process
508 */
d3d32139 509#ifdef SMP
c7eb0589 510 vm86_trap((struct vm86frame *)frame,
d3d32139
MD
511 have_mplock);
512#else
c7eb0589 513 vm86_trap((struct vm86frame *)frame, 0);
d3d32139
MD
514#endif
515 KKASSERT(0); /* NOT REACHED */
8a8d5d85
MD
516 }
517 goto out2;
984263bc
MD
518 }
519 switch (type) {
520 /*
521 * these traps want either a process context, or
522 * assume a normal userspace trap.
523 */
524 case T_PROTFLT:
525 case T_SEGNPFLT:
c7eb0589 526 trap_fatal(frame, eva);
8a8d5d85 527 goto out2;
984263bc
MD
528 case T_TRCTRAP:
529 type = T_BPTFLT; /* kernel breakpoint */
530 /* FALL THROUGH */
531 }
532 goto kernel_trap; /* normal kernel trap handling */
533 }
534
c7eb0589 535 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
984263bc
MD
536 /* user trap */
537
80bbc910
SS
538 KTR_LOG(kernentry_trap, p->p_pid, lp->lwp_tid,
539 frame->tf_trapno, eva);
540
d86a23e0 541 userenter(td, p);
26a0694b 542
37af14fe 543 sticks = (int)td->td_sticks;
c7eb0589 544 lp->lwp_md.md_regs = frame;
984263bc
MD
545
546 switch (type) {
547 case T_PRIVINFLT: /* privileged instruction fault */
984263bc 548 i = SIGILL;
bab69519 549 ucode = ILL_PRVOPC;
984263bc
MD
550 break;
551
552 case T_BPTFLT: /* bpt instruction fault */
553 case T_TRCTRAP: /* trace trap */
c7eb0589 554 frame->tf_eflags &= ~PSL_T;
984263bc 555 i = SIGTRAP;
bab69519 556 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
984263bc
MD
557 break;
558
559 case T_ARITHTRAP: /* arithmetic trap */
560 ucode = code;
561 i = SIGFPE;
562 break;
563
564 case T_ASTFLT: /* Allow process switch */
12e4aaff 565 mycpu->gd_cnt.v_soft++;
235957ed 566 if (mycpu->gd_reqflags & RQF_AST_OWEUPC) {
2a418930
MD
567 atomic_clear_int(&mycpu->gd_reqflags,
568 RQF_AST_OWEUPC);
93328593
SS
569 addupc_task(p, p->p_prof.pr_addr,
570 p->p_prof.pr_ticks);
984263bc
MD
571 }
572 goto out;
573
574 /*
575 * The following two traps can happen in
576 * vm86 mode, and, if so, we want to handle
577 * them specially.
578 */
579 case T_PROTFLT: /* general protection fault */
580 case T_STKFLT: /* stack fault */
c7eb0589
SS
581 if (frame->tf_eflags & PSL_VM) {
582 i = vm86_emulate((struct vm86frame *)frame);
984263bc
MD
583 if (i == 0)
584 goto out;
585 break;
586 }
f2642d5a
AH
587 i = SIGBUS;
588 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
589 break;
984263bc 590 case T_SEGNPFLT: /* segment not present fault */
f2642d5a
AH
591 i = SIGBUS;
592 ucode = BUS_ADRERR;
593 break;
984263bc
MD
594 case T_TSSFLT: /* invalid TSS fault */
595 case T_DOUBLEFLT: /* double fault */
596 default:
984263bc 597 i = SIGBUS;
bab69519 598 ucode = BUS_OBJERR;
984263bc
MD
599 break;
600
601 case T_PAGEFLT: /* page fault */
c7eb0589 602 i = trap_pfault(frame, TRUE, eva);
984263bc 603 if (i == -1)
d81ccc3e 604 goto out;
984263bc
MD
605#if defined(I586_CPU) && !defined(NO_F00F_HACK)
606 if (i == -2)
607 goto restart;
608#endif
609 if (i == 0)
610 goto out;
bab69519 611
f2642d5a
AH
612 if (i == SIGSEGV)
613 ucode = SEGV_MAPERR;
bab69519
JM
614 else {
615 i = SIGSEGV;
616 ucode = SEGV_ACCERR;
617 }
984263bc
MD
618 break;
619
620 case T_DIVIDE: /* integer divide fault */
621 ucode = FPE_INTDIV;
622 i = SIGFPE;
623 break;
624
625#if NISA > 0
626 case T_NMI:
9753ab6c 627 MAKEMPSAFE(have_mplock);
984263bc
MD
628#ifdef POWERFAIL_NMI
629 goto handle_powerfail;
630#else /* !POWERFAIL_NMI */
631 /* machine/parity/power fail/"kitchen sink" faults */
632 if (isa_nmi(code) == 0) {
633#ifdef DDB
634 /*
635 * NMI can be hooked up to a pushbutton
636 * for debugging.
637 */
638 if (ddb_on_nmi) {
26be20a0 639 kprintf ("NMI ... going to debugger\n");
c7eb0589 640 kdb_trap (type, 0, frame);
984263bc
MD
641 }
642#endif /* DDB */
8a8d5d85 643 goto out2;
984263bc
MD
644 } else if (panic_on_nmi)
645 panic("NMI indicates hardware failure");
646 break;
647#endif /* POWERFAIL_NMI */
648#endif /* NISA > 0 */
649
650 case T_OFLOW: /* integer overflow fault */
651 ucode = FPE_INTOVF;
652 i = SIGFPE;
653 break;
654
655 case T_BOUND: /* bounds check fault */
656 ucode = FPE_FLTSUB;
657 i = SIGFPE;
658 break;
659
660 case T_DNA:
431d0fef
MD
661 /*
662 * Virtual kernel intercept - pass the DNA exception
663 * to the virtual kernel if it asked to handle it.
664 * This occurs when the virtual kernel is holding
665 * onto the FP context for a different emulated
666 * process then the one currently running.
667 *
668 * We must still call npxdna() since we may have
669 * saved FP state that the virtual kernel needs
670 * to hand over to a different emulated process.
671 */
39005e16 672 if (lp->lwp_vkernel && lp->lwp_vkernel->ve &&
431d0fef
MD
673 (td->td_pcb->pcb_flags & FP_VIRTFP)
674 ) {
675 npxdna();
676 break;
677 }
678
984263bc 679#if NNPX > 0
a02705a9
MD
680 /*
681 * The kernel may have switched out the FP unit's
682 * state, causing the user process to take a fault
683 * when it tries to use the FP unit. Restore the
684 * state here
685 */
984263bc 686 if (npxdna())
d81ccc3e 687 goto out;
984263bc
MD
688#endif
689 if (!pmath_emulate) {
690 i = SIGFPE;
691 ucode = FPE_FPU_NP_TRAP;
692 break;
693 }
c7eb0589 694 i = (*pmath_emulate)(frame);
984263bc 695 if (i == 0) {
c7eb0589 696 if (!(frame->tf_eflags & PSL_T))
8a8d5d85 697 goto out2;
c7eb0589 698 frame->tf_eflags &= ~PSL_T;
984263bc
MD
699 i = SIGTRAP;
700 }
701 /* else ucode = emulator_only_knows() XXX */
702 break;
703
704 case T_FPOPFLT: /* FPU operand fetch fault */
bab69519 705 ucode = ILL_COPROC;
984263bc
MD
706 i = SIGILL;
707 break;
708
709 case T_XMMFLT: /* SIMD floating-point exception */
710 ucode = 0; /* XXX */
711 i = SIGFPE;
712 break;
713 }
714 } else {
715kernel_trap:
716 /* kernel trap */
717
718 switch (type) {
719 case T_PAGEFLT: /* page fault */
c7eb0589 720 trap_pfault(frame, FALSE, eva);
8a8d5d85 721 goto out2;
984263bc
MD
722
723 case T_DNA:
724#if NNPX > 0
725 /*
a02705a9
MD
726 * The kernel may be using npx for copying or other
727 * purposes.
984263bc
MD
728 */
729 if (npxdna())
8a8d5d85 730 goto out2;
984263bc
MD
731#endif
732 break;
733
734 case T_PROTFLT: /* general protection fault */
735 case T_SEGNPFLT: /* segment not present fault */
736 /*
737 * Invalid segment selectors and out of bounds
738 * %eip's and %esp's can be set up in user mode.
739 * This causes a fault in kernel mode when the
740 * kernel tries to return to user mode. We want
741 * to get this fault so that we can fix the
742 * problem here and not have to check all the
743 * selectors and pointers when the user changes
744 * them.
745 */
746#define MAYBE_DORETI_FAULT(where, whereto) \
747 do { \
c7eb0589
SS
748 if (frame->tf_eip == (int)where) { \
749 frame->tf_eip = (int)whereto; \
8a8d5d85 750 goto out2; \
984263bc
MD
751 } \
752 } while (0)
ef0fdad1 753 if (mycpu->gd_intr_nesting_level == 0) {
984263bc
MD
754 /*
755 * Invalid %fs's and %gs's can be created using
756 * procfs or PT_SETREGS or by invalidating the
757 * underlying LDT entry. This causes a fault
758 * in kernel mode when the kernel attempts to
759 * switch contexts. Lose the bad context
760 * (XXX) so that we can continue, and generate
761 * a signal.
762 */
984263bc
MD
763 MAYBE_DORETI_FAULT(doreti_iret,
764 doreti_iret_fault);
765 MAYBE_DORETI_FAULT(doreti_popl_ds,
766 doreti_popl_ds_fault);
767 MAYBE_DORETI_FAULT(doreti_popl_es,
768 doreti_popl_es_fault);
769 MAYBE_DORETI_FAULT(doreti_popl_fs,
770 doreti_popl_fs_fault);
4e7c41c5
MD
771 MAYBE_DORETI_FAULT(doreti_popl_gs,
772 doreti_popl_gs_fault);
93ad6da2
MD
773
774 /*
775 * NOTE: cpu doesn't push esp on kernel trap
776 */
777 if (td->td_pcb->pcb_onfault &&
778 td->td_pcb->pcb_onfault_sp ==
779 (int)&frame->tf_esp) {
c7eb0589 780 frame->tf_eip =
37af14fe 781 (register_t)td->td_pcb->pcb_onfault;
8a8d5d85 782 goto out2;
984263bc
MD
783 }
784 }
785 break;
786
787 case T_TSSFLT:
788 /*
789 * PSL_NT can be set in user mode and isn't cleared
790 * automatically when the kernel is entered. This
791 * causes a TSS fault when the kernel attempts to
792 * `iret' because the TSS link is uninitialized. We
793 * want to get this fault so that we can fix the
794 * problem here and not every time the kernel is
795 * entered.
796 */
c7eb0589
SS
797 if (frame->tf_eflags & PSL_NT) {
798 frame->tf_eflags &= ~PSL_NT;
8a8d5d85 799 goto out2;
984263bc
MD
800 }
801 break;
802
803 case T_TRCTRAP: /* trace trap */
c7eb0589 804 if (frame->tf_eip == (int)IDTVEC(syscall)) {
984263bc
MD
805 /*
806 * We've just entered system mode via the
807 * syscall lcall. Continue single stepping
808 * silently until the syscall handler has
809 * saved the flags.
810 */
8a8d5d85 811 goto out2;
984263bc 812 }
c7eb0589 813 if (frame->tf_eip == (int)IDTVEC(syscall) + 1) {
984263bc
MD
814 /*
815 * The syscall handler has now saved the
816 * flags. Stop single stepping it.
817 */
c7eb0589 818 frame->tf_eflags &= ~PSL_T;
8a8d5d85 819 goto out2;
984263bc
MD
820 }
821 /*
822 * Ignore debug register trace traps due to
823 * accesses in the user's address space, which
824 * can happen under several conditions such as
825 * if a user sets a watchpoint on a buffer and
826 * then passes that buffer to a system call.
827 * We still want to get TRCTRAPS for addresses
828 * in kernel space because that is useful when
829 * debugging the kernel.
830 */
831 if (user_dbreg_trap()) {
832 /*
833 * Reset breakpoint bits because the
834 * processor doesn't
835 */
836 load_dr6(rdr6() & 0xfffffff0);
8a8d5d85 837 goto out2;
984263bc
MD
838 }
839 /*
bab69519 840 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
984263bc
MD
841 */
842 case T_BPTFLT:
843 /*
844 * If DDB is enabled, let it handle the debugger trap.
845 * Otherwise, debugger traps "can't happen".
846 */
f2642d5a 847 ucode = TRAP_BRKPT;
984263bc 848#ifdef DDB
9753ab6c 849 MAKEMPSAFE(have_mplock);
c7eb0589 850 if (kdb_trap (type, 0, frame))
8a8d5d85 851 goto out2;
984263bc
MD
852#endif
853 break;
854
855#if NISA > 0
856 case T_NMI:
9753ab6c 857 MAKEMPSAFE(have_mplock);
984263bc
MD
858#ifdef POWERFAIL_NMI
859#ifndef TIMER_FREQ
860# define TIMER_FREQ 1193182
861#endif
862 handle_powerfail:
863 {
864 static unsigned lastalert = 0;
865
866 if(time_second - lastalert > 10)
867 {
868 log(LOG_WARNING, "NMI: power fail\n");
869 sysbeep(TIMER_FREQ/880, hz);
870 lastalert = time_second;
871 }
8a8d5d85
MD
872 /* YYY mp count */
873 goto out2;
984263bc
MD
874 }
875#else /* !POWERFAIL_NMI */
876 /* machine/parity/power fail/"kitchen sink" faults */
877 if (isa_nmi(code) == 0) {
878#ifdef DDB
879 /*
880 * NMI can be hooked up to a pushbutton
881 * for debugging.
882 */
883 if (ddb_on_nmi) {
26be20a0 884 kprintf ("NMI ... going to debugger\n");
c7eb0589 885 kdb_trap (type, 0, frame);
984263bc
MD
886 }
887#endif /* DDB */
8a8d5d85 888 goto out2;
984263bc 889 } else if (panic_on_nmi == 0)
8a8d5d85 890 goto out2;
984263bc
MD
891 /* FALL THROUGH */
892#endif /* POWERFAIL_NMI */
893#endif /* NISA > 0 */
894 }
895
9753ab6c 896 MAKEMPSAFE(have_mplock);
c7eb0589 897 trap_fatal(frame, eva);
8a8d5d85 898 goto out2;
984263bc
MD
899 }
900
4a22e893
MD
901 /*
902 * Virtual kernel intercept - if the fault is directly related to a
903 * VM context managed by a virtual kernel then let the virtual kernel
904 * handle it.
905 */
39005e16 906 if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
287ebb09 907 vkernel_trap(lp, frame);
4a22e893
MD
908 goto out;
909 }
910
bab69519 911 /* Translate fault for emulators (e.g. Linux) */
984263bc
MD
912 if (*p->p_sysent->sv_transtrap)
913 i = (*p->p_sysent->sv_transtrap)(i, type);
914
9753ab6c 915 MAKEMPSAFE(have_mplock);
08f2f1bb 916 trapsignal(lp, i, ucode);
984263bc
MD
917
918#ifdef DEBUG
919 if (type <= MAX_TRAP_MSG) {
920 uprintf("fatal process exception: %s",
921 trap_msg[type]);
922 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
923 uprintf(", fault VA = 0x%lx", (u_long)eva);
924 uprintf("\n");
925 }
926#endif
927
928out:
c7eb0589 929 userret(lp, frame, sticks);
81eea9f4 930 userexit(lp);
9753ab6c 931out2: ;
96728c05 932#ifdef SMP
9753ab6c
MD
933 if (have_mplock)
934 rel_mplock();
96728c05 935#endif
80bbc910
SS
936 if (p != NULL && lp != NULL)
937 KTR_LOG(kernentry_trap_ret, p->p_pid, lp->lwp_tid);
7527bc81 938#ifdef INVARIANTS
f9235b6d 939 KASSERT(crit_count == td->td_critcount,
3933a3ab 940 ("trap: critical section count mismatch! %d/%d",
f9235b6d 941 crit_count, td->td_pri));
3933a3ab
MD
942 KASSERT(curstop == td->td_toks_stop,
943 ("trap: extra tokens held after trap! %zd/%zd",
944 curstop - &td->td_toks_base,
945 td->td_toks_stop - &td->td_toks_base));
7527bc81 946#endif
984263bc
MD
947}
948
984263bc 949int
f123d5a1 950trap_pfault(struct trapframe *frame, int usermode, vm_offset_t eva)
984263bc
MD
951{
952 vm_offset_t va;
953 struct vmspace *vm = NULL;
954 vm_map_t map = 0;
955 int rv = 0;
1b9d3514 956 int fault_flags;
984263bc 957 vm_prot_t ftype;
37af14fe 958 thread_t td = curthread;
287ebb09 959 struct lwp *lp = td->td_lwp;
984263bc
MD
960
961 va = trunc_page(eva);
962 if (va >= KERNBASE) {
963 /*
964 * Don't allow user-mode faults in kernel address space.
965 * An exception: if the faulting address is the invalid
966 * instruction entry in the IDT, then the Intel Pentium
967 * F00F bug workaround was triggered, and we need to
968 * treat it is as an illegal instruction, and not a page
969 * fault.
970 */
971#if defined(I586_CPU) && !defined(NO_F00F_HACK)
972 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
973 frame->tf_trapno = T_PRIVINFLT;
974 return -2;
975 }
976#endif
977 if (usermode)
978 goto nogo;
979
e4846942 980 map = &kernel_map;
984263bc
MD
981 } else {
982 /*
983 * This is a fault on non-kernel virtual memory.
984 * vm is initialized above to NULL. If curproc is NULL
985 * or curproc->p_vmspace is NULL the fault is fatal.
986 */
287ebb09
MD
987 if (lp != NULL)
988 vm = lp->lwp_vmspace;
984263bc
MD
989
990 if (vm == NULL)
991 goto nogo;
992
993 map = &vm->vm_map;
994 }
995
996 if (frame->tf_err & PGEX_W)
997 ftype = VM_PROT_WRITE;
998 else
999 ftype = VM_PROT_READ;
1000
e4846942 1001 if (map != &kernel_map) {
984263bc
MD
1002 /*
1003 * Keep swapout from messing with us during this
1004 * critical time.
1005 */
287ebb09 1006 PHOLD(lp->lwp_proc);
984263bc
MD
1007
1008 /*
1b9d3514
MD
1009 * Issue fault
1010 */
1011 fault_flags = 0;
1012 if (usermode)
1013 fault_flags |= VM_FAULT_BURST;
1014 if (ftype & VM_PROT_WRITE)
1015 fault_flags |= VM_FAULT_DIRTY;
1016 else
1017 fault_flags |= VM_FAULT_NORMAL;
1018 rv = vm_fault(map, va, ftype, fault_flags);
287ebb09 1019 PRELE(lp->lwp_proc);
984263bc
MD
1020 } else {
1021 /*
bab69519
JM
1022 * Don't have to worry about process locking or stacks in the
1023 * kernel.
984263bc
MD
1024 */
1025 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
1026 }
984263bc
MD
1027 if (rv == KERN_SUCCESS)
1028 return (0);
1029nogo:
1030 if (!usermode) {
93ad6da2
MD
1031 /*
1032 * NOTE: cpu doesn't push esp on kernel trap
1033 */
37af14fe 1034 if (td->td_gd->gd_intr_nesting_level == 0 &&
93ad6da2
MD
1035 td->td_pcb->pcb_onfault &&
1036 td->td_pcb->pcb_onfault_sp == (int)&frame->tf_esp) {
37af14fe 1037 frame->tf_eip = (register_t)td->td_pcb->pcb_onfault;
984263bc
MD
1038 return (0);
1039 }
93ad6da2
MD
1040 if (td->td_gd->gd_intr_nesting_level == 0 &&
1041 td->td_pcb->pcb_onfault) {
1042 kprintf("ESP mismatch %p %08x\n",
1043 &frame->tf_esp, td->td_pcb->pcb_onfault_sp);
1044 }
984263bc
MD
1045 trap_fatal(frame, eva);
1046 return (-1);
1047 }
1048
1049 /* kludge to pass faulting virtual address to sendsig */
4e7c41c5 1050 frame->tf_xflags = frame->tf_err;
984263bc
MD
1051 frame->tf_err = eva;
1052
1053 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
1054}
1055
1056static void
f123d5a1 1057trap_fatal(struct trapframe *frame, vm_offset_t eva)
984263bc
MD
1058{
1059 int code, type, ss, esp;
1060 struct soft_segment_descriptor softseg;
1061
1062 code = frame->tf_err;
1063 type = frame->tf_trapno;
3951a45f 1064 sdtossd(&gdt[mycpu->gd_cpuid * NGDT + IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
984263bc
MD
1065
1066 if (type <= MAX_TRAP_MSG)
26be20a0 1067 kprintf("\n\nFatal trap %d: %s while in %s mode\n",
984263bc
MD
1068 type, trap_msg[type],
1069 frame->tf_eflags & PSL_VM ? "vm86" :
1070 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
1071#ifdef SMP
7a44d1cb 1072 /* three separate prints in case of a trap on an unmapped page */
26be20a0 1073 kprintf("cpuid = %d; ", mycpu->gd_cpuid);
cb7d6921 1074 kprintf("lapic.id = %08x\n", lapic->id);
984263bc
MD
1075#endif
1076 if (type == T_PAGEFLT) {
d557216f 1077 kprintf("fault virtual address = %p\n", (void *)eva);
26be20a0 1078 kprintf("fault code = %s %s, %s\n",
984263bc
MD
1079 code & PGEX_U ? "user" : "supervisor",
1080 code & PGEX_W ? "write" : "read",
1081 code & PGEX_P ? "protection violation" : "page not present");
1082 }
26be20a0 1083 kprintf("instruction pointer = 0x%x:0x%x\n",
984263bc
MD
1084 frame->tf_cs & 0xffff, frame->tf_eip);
1085 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
1086 ss = frame->tf_ss & 0xffff;
1087 esp = frame->tf_esp;
1088 } else {
1089 ss = GSEL(GDATA_SEL, SEL_KPL);
1090 esp = (int)&frame->tf_esp;
1091 }
26be20a0
SW
1092 kprintf("stack pointer = 0x%x:0x%x\n", ss, esp);
1093 kprintf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
1094 kprintf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
984263bc 1095 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
26be20a0 1096 kprintf(" = DPL %d, pres %d, def32 %d, gran %d\n",
984263bc
MD
1097 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
1098 softseg.ssd_gran);
26be20a0 1099 kprintf("processor eflags = ");
984263bc 1100 if (frame->tf_eflags & PSL_T)
26be20a0 1101 kprintf("trace trap, ");
984263bc 1102 if (frame->tf_eflags & PSL_I)
26be20a0 1103 kprintf("interrupt enabled, ");
984263bc 1104 if (frame->tf_eflags & PSL_NT)
26be20a0 1105 kprintf("nested task, ");
984263bc 1106 if (frame->tf_eflags & PSL_RF)
26be20a0 1107 kprintf("resume, ");
984263bc 1108 if (frame->tf_eflags & PSL_VM)
26be20a0
SW
1109 kprintf("vm86, ");
1110 kprintf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
1111 kprintf("current process = ");
984263bc 1112 if (curproc) {
26be20a0 1113 kprintf("%lu (%s)\n",
984263bc
MD
1114 (u_long)curproc->p_pid, curproc->p_comm ?
1115 curproc->p_comm : "");
1116 } else {
26be20a0 1117 kprintf("Idle\n");
984263bc 1118 }
26be20a0 1119 kprintf("current thread = pri %d ", curthread->td_pri);
f9235b6d 1120 if (curthread->td_critcount)
26be20a0
SW
1121 kprintf("(CRIT)");
1122 kprintf("\n");
984263bc
MD
1123#ifdef SMP
1124/**
1125 * XXX FIXME:
1126 * we probably SHOULD have stopped the other CPUs before now!
1127 * another CPU COULD have been touching cpl at this moment...
1128 */
26be20a0 1129 kprintf(" <- SMP: XXX");
984263bc 1130#endif
26be20a0 1131 kprintf("\n");
984263bc
MD
1132
1133#ifdef KDB
1134 if (kdb_trap(&psl))
1135 return;
1136#endif
1137#ifdef DDB
f7bc9806 1138 if ((debugger_on_panic || db_active) && kdb_trap(type, code, frame))
984263bc
MD
1139 return;
1140#endif
26be20a0 1141 kprintf("trap number = %d\n", type);
984263bc
MD
1142 if (type <= MAX_TRAP_MSG)
1143 panic("%s", trap_msg[type]);
1144 else
1145 panic("unknown/reserved trap");
1146}
1147
1148/*
1149 * Double fault handler. Called when a fault occurs while writing
1150 * a frame for a trap/exception onto the stack. This usually occurs
1151 * when the stack overflows (such is the case with infinite recursion,
1152 * for example).
1153 *
1154 * XXX Note that the current PTD gets replaced by IdlePTD when the
1155 * task switch occurs. This means that the stack that was active at
1156 * the time of the double fault is not available at <kstack> unless
1157 * the machine was idle when the double fault occurred. The downside
1158 * of this is that "trace <ebp>" in ddb won't work.
1159 */
093565f2
MD
1160static __inline
1161int
1162in_kstack_guard(register_t rptr)
1163{
1164 thread_t td = curthread;
1165
1166 if ((char *)rptr >= td->td_kstack &&
1167 (char *)rptr < td->td_kstack + PAGE_SIZE) {
1168 return 1;
1169 }
1170 return 0;
1171}
1172
984263bc 1173void
f123d5a1 1174dblfault_handler(void)
984263bc 1175{
85100692 1176 struct mdglobaldata *gd = mdcpu;
17a9f566 1177
093565f2
MD
1178 if (in_kstack_guard(gd->gd_common_tss.tss_esp) ||
1179 in_kstack_guard(gd->gd_common_tss.tss_ebp)) {
0174149e 1180 kprintf("DOUBLE FAULT - KERNEL STACK GUARD HIT!\n");
093565f2 1181 } else {
0174149e 1182 kprintf("DOUBLE FAULT:\n");
093565f2 1183 }
26be20a0
SW
1184 kprintf("eip = 0x%x\n", gd->gd_common_tss.tss_eip);
1185 kprintf("esp = 0x%x\n", gd->gd_common_tss.tss_esp);
1186 kprintf("ebp = 0x%x\n", gd->gd_common_tss.tss_ebp);
984263bc 1187#ifdef SMP
7a44d1cb 1188 /* three separate prints in case of a trap on an unmapped page */
093565f2 1189 kprintf("cpuid = %d; ", gd->mi.gd_cpuid);
cb7d6921 1190 kprintf("lapic.id = %08x\n", lapic->id);
984263bc
MD
1191#endif
1192 panic("double fault");
1193}
1194
1195/*
0d355d3b 1196 * syscall2 - MP aware system call request C handler
984263bc 1197 *
0d355d3b
MD
1198 * A system call is essentially treated as a trap. The MP lock is not
1199 * held on entry or return. We are responsible for handling ASTs
1200 * (e.g. a task switch) prior to return.
984263bc 1201 *
0d355d3b 1202 * MPSAFE
984263bc
MD
1203 */
1204void
c7eb0589 1205syscall2(struct trapframe *frame)
984263bc 1206{
dadab5e9
MD
1207 struct thread *td = curthread;
1208 struct proc *p = td->td_proc;
81eea9f4 1209 struct lwp *lp = td->td_lwp;
984263bc 1210 caddr_t params;
984263bc 1211 struct sysent *callp;
984263bc 1212 register_t orig_tf_eflags;
37af14fe 1213 int sticks;
984263bc
MD
1214 int error;
1215 int narg;
7527bc81 1216#ifdef INVARIANTS
f9235b6d 1217 int crit_count = td->td_critcount;
7527bc81 1218#endif
270ac911
MD
1219#ifdef SMP
1220 int have_mplock = 0;
1221#endif
984263bc 1222 u_int code;
a64ba182 1223 union sysunion args;
984263bc
MD
1224
1225#ifdef DIAGNOSTIC
c7eb0589 1226 if (ISPL(frame->tf_cs) != SEL_UPL) {
984263bc
MD
1227 get_mplock();
1228 panic("syscall");
1229 /* NOT REACHED */
1230 }
1231#endif
1232
80bbc910
SS
1233 KTR_LOG(kernentry_syscall, p->p_pid, lp->lwp_tid,
1234 frame->tf_eax);
1235
d86a23e0 1236 userenter(td, p); /* lazy raise our priority */
37af14fe 1237
4a22e893
MD
1238 /*
1239 * Misc
1240 */
37af14fe 1241 sticks = (int)td->td_sticks;
c7eb0589 1242 orig_tf_eflags = frame->tf_eflags;
4a22e893
MD
1243
1244 /*
1245 * Virtual kernel intercept - if a VM context managed by a virtual
1246 * kernel issues a system call the virtual kernel handles it, not us.
1247 * Restore the virtual kernel context and return from its system
1248 * call. The current frame is copied out to the virtual kernel.
1249 */
39005e16 1250 if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
bb47c072 1251 vkernel_trap(lp, frame);
4a22e893 1252 error = EJUSTRETURN;
e7912058 1253 callp = NULL;
4a22e893
MD
1254 goto out;
1255 }
984263bc 1256
4a22e893
MD
1257 /*
1258 * Get the system call parameters and account for time
1259 */
c7eb0589
SS
1260 lp->lwp_md.md_regs = frame;
1261 params = (caddr_t)frame->tf_esp + sizeof(int);
1262 code = frame->tf_eax;
984263bc
MD
1263
1264 if (p->p_sysent->sv_prepsyscall) {
270ac911 1265 (*p->p_sysent->sv_prepsyscall)(
c7eb0589 1266 frame, (int *)(&args.nosys.sysmsg + 1),
270ac911 1267 &code, &params);
984263bc
MD
1268 } else {
1269 /*
1270 * Need to check if this is a 32 bit or 64 bit syscall.
1271 * fuword is MP aware.
1272 */
1273 if (code == SYS_syscall) {
1274 /*
1275 * Code is first argument, followed by actual args.
1276 */
1277 code = fuword(params);
1278 params += sizeof(int);
1279 } else if (code == SYS___syscall) {
1280 /*
1281 * Like syscall, but code is a quad, so as to maintain
1282 * quad alignment for the rest of the arguments.
1283 */
1284 code = fuword(params);
1285 params += sizeof(quad_t);
1286 }
1287 }
1288
8ec60c3f 1289 code &= p->p_sysent->sv_mask;
8ba5f7ef 1290
7062f5b4
EN
1291 if (code >= p->p_sysent->sv_size)
1292 callp = &p->p_sysent->sv_table[0];
1293 else
1294 callp = &p->p_sysent->sv_table[code];
984263bc
MD
1295
1296 narg = callp->sy_narg & SYF_ARGMASK;
1297
8ba5f7ef
AH
1298#if 0
1299 if (p->p_sysent->sv_name[0] == 'L')
1300 kprintf("Linux syscall, code = %d\n", code);
1301#endif
1302
984263bc
MD
1303 /*
1304 * copyin is MP aware, but the tracing code is not
1305 */
8ec60c3f 1306 if (narg && params) {
f9a13fc4 1307 error = copyin(params, (caddr_t)(&args.nosys.sysmsg + 1),
8ec60c3f
MD
1308 narg * sizeof(register_t));
1309 if (error) {
984263bc 1310#ifdef KTRACE
270ac911
MD
1311 if (KTRPOINT(td, KTR_SYSCALL)) {
1312 MAKEMPSAFE(have_mplock);
1313
9fb04d14 1314 ktrsyscall(lp, code, narg,
f9a13fc4 1315 (void *)(&args.nosys.sysmsg + 1));
270ac911 1316 }
984263bc 1317#endif
8ec60c3f
MD
1318 goto bad;
1319 }
984263bc
MD
1320 }
1321
984263bc 1322#ifdef KTRACE
dadab5e9 1323 if (KTRPOINT(td, KTR_SYSCALL)) {
270ac911 1324 MAKEMPSAFE(have_mplock);
9fb04d14 1325 ktrsyscall(lp, code, narg, (void *)(&args.nosys.sysmsg + 1));
984263bc
MD
1326 }
1327#endif
01dce7bb
MD
1328
1329 /*
1330 * For traditional syscall code edx is left untouched when 32 bit
1331 * results are returned. Since edx is loaded from fds[1] when the
1332 * system call returns we pre-set it here.
1333 */
c7114eea 1334 args.sysmsg_fds[0] = 0;
c7eb0589 1335 args.sysmsg_fds[1] = frame->tf_edx;
984263bc 1336
4a22e893
MD
1337 /*
1338 * The syscall might manipulate the trap frame. If it does it
1339 * will probably return EJUSTRETURN.
1340 */
c7eb0589 1341 args.sysmsg_frame = frame;
4a22e893 1342
984263bc
MD
1343 STOPEVENT(p, S_SCE, narg); /* MP aware */
1344
270ac911 1345 /*
3919ced0
MD
1346 * NOTE: All system calls run MPSAFE now. The system call itself
1347 * is responsible for getting the MP lock.
270ac911 1348 */
a64ba182 1349 error = (*callp->sy_call)(&args);
984263bc 1350
4a22e893 1351out:
984263bc
MD
1352 /*
1353 * MP SAFE (we may or may not have the MP lock at this point)
1354 */
1355 switch (error) {
1356 case 0:
1357 /*
1358 * Reinitialize proc pointer `p' as it may be different
1359 * if this is a child returning from fork syscall.
1360 */
1361 p = curproc;
81eea9f4 1362 lp = curthread->td_lwp;
c7eb0589
SS
1363 frame->tf_eax = args.sysmsg_fds[0];
1364 frame->tf_edx = args.sysmsg_fds[1];
1365 frame->tf_eflags &= ~PSL_C;
984263bc 1366 break;
984263bc
MD
1367 case ERESTART:
1368 /*
1369 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1370 * int 0x80 is 2 bytes. We saved this in tf_err.
1371 */
c7eb0589 1372 frame->tf_eip -= frame->tf_err;
984263bc 1373 break;
984263bc
MD
1374 case EJUSTRETURN:
1375 break;
245e4f17
MD
1376 case EASYNC:
1377 panic("Unexpected EASYNC return value (for now)");
984263bc
MD
1378 default:
1379bad:
7062f5b4
EN
1380 if (p->p_sysent->sv_errsize) {
1381 if (error >= p->p_sysent->sv_errsize)
1382 error = -1; /* XXX */
1383 else
1384 error = p->p_sysent->sv_errtbl[error];
984263bc 1385 }
c7eb0589
SS
1386 frame->tf_eax = error;
1387 frame->tf_eflags |= PSL_C;
984263bc
MD
1388 break;
1389 }
1390
1391 /*
1392 * Traced syscall. trapsignal() is not MP aware.
1393 */
1394 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
270ac911 1395 MAKEMPSAFE(have_mplock);
c7eb0589 1396 frame->tf_eflags &= ~PSL_T;
f2642d5a 1397 trapsignal(lp, SIGTRAP, TRAP_TRACE);
984263bc
MD
1398 }
1399
1400 /*
1401 * Handle reschedule and other end-of-syscall issues
1402 */
c7eb0589 1403 userret(lp, frame, sticks);
984263bc
MD
1404
1405#ifdef KTRACE
dadab5e9 1406 if (KTRPOINT(td, KTR_SYSRET)) {
270ac911 1407 MAKEMPSAFE(have_mplock);
9fb04d14 1408 ktrsysret(lp, code, error, args.sysmsg_result);
984263bc
MD
1409 }
1410#endif
1411
1412 /*
1413 * This works because errno is findable through the
1414 * register set. If we ever support an emulation where this
1415 * is not the case, this code will need to be revisited.
1416 */
1417 STOPEVENT(p, S_SCX, code);
1418
81eea9f4 1419 userexit(lp);
8a8d5d85 1420#ifdef SMP
984263bc
MD
1421 /*
1422 * Release the MP lock if we had to get it
1423 */
270ac911
MD
1424 if (have_mplock)
1425 rel_mplock();
8a8d5d85 1426#endif
80bbc910 1427 KTR_LOG(kernentry_syscall_ret, p->p_pid, lp->lwp_tid, error);
7527bc81 1428#ifdef INVARIANTS
f9235b6d 1429 KASSERT(crit_count == td->td_critcount,
7527bc81 1430 ("syscall: critical section count mismatch! %d/%d",
f9235b6d 1431 crit_count, td->td_pri));
3933a3ab
MD
1432 KASSERT(&td->td_toks_base == td->td_toks_stop,
1433 ("syscall: extra tokens held after trap! %zd",
1434 td->td_toks_stop - &td->td_toks_base));
7527bc81 1435#endif
984263bc
MD
1436}
1437
2b0bd8aa
MD
1438/*
1439 * NOTE: MP lock not held at any point.
1440 */
91bd9c1e
SS
1441void
1442fork_return(struct lwp *lp, struct trapframe *frame)
1443{
1444 frame->tf_eax = 0; /* Child returns zero */
1445 frame->tf_eflags &= ~PSL_C; /* success */
1446 frame->tf_edx = 1;
1447
1448 generic_lwp_return(lp, frame);
80bbc910 1449 KTR_LOG(kernentry_fork_ret, lp->lwp_proc->p_pid, lp->lwp_tid);
91bd9c1e
SS
1450}
1451
a64ba182 1452/*
984263bc 1453 * Simplified back end of syscall(), used when returning from fork()
2b0bd8aa
MD
1454 * directly into user mode.
1455 *
1456 * This code will return back into the fork trampoline code which then
1457 * runs doreti.
1458 *
1459 * NOTE: The mplock is not held at any point.
984263bc
MD
1460 */
1461void
91bd9c1e 1462generic_lwp_return(struct lwp *lp, struct trapframe *frame)
984263bc 1463{
bb3cd951 1464 struct proc *p = lp->lwp_proc;
81eea9f4 1465
3a57cd9c
MD
1466 /*
1467 * Newly forked processes are given a kernel priority. We have to
1468 * adjust the priority to a normal user priority and fake entry
1469 * into the kernel (call userenter()) to install a passive release
1470 * function just in case userret() decides to stop the process. This
1471 * can occur when ^Z races a fork. If we do not install the passive
1472 * release function the current process designation will not be
1473 * released when the thread goes to sleep.
1474 */
1475 lwkt_setpri_self(TDPRI_USER_NORM);
d86a23e0 1476 userenter(lp->lwp_thread, p);
c7eb0589 1477 userret(lp, frame, 0);
984263bc 1478#ifdef KTRACE
81eea9f4 1479 if (KTRPOINT(lp->lwp_thread, KTR_SYSRET))
9fb04d14 1480 ktrsysret(lp, SYS_fork, 0, 0);
984263bc 1481#endif
4643740a 1482 lp->lwp_flags |= LWP_PASSIVE_ACQ;
81eea9f4 1483 userexit(lp);
4643740a 1484 lp->lwp_flags &= ~LWP_PASSIVE_ACQ;
984263bc 1485}
431d0fef
MD
1486
1487/*
4b486183 1488 * If PGEX_FPFAULT is set then set FP_VIRTFP in the PCB to force a T_DNA
431d0fef
MD
1489 * fault (which is then passed back to the virtual kernel) if an attempt is
1490 * made to use the FP unit.
1491 *
1492 * XXX this is a fairly big hack.
1493 */
1494void
1495set_vkernel_fp(struct trapframe *frame)
1496{
1497 struct thread *td = curthread;
1498
4b486183 1499 if (frame->tf_xflags & PGEX_FPFAULT) {
431d0fef
MD
1500 td->td_pcb->pcb_flags |= FP_VIRTFP;
1501 if (mdcpu->gd_npxthread == td)
1502 npxexit();
1503 } else {
1504 td->td_pcb->pcb_flags &= ~FP_VIRTFP;
1505 }
1506}
1507
bb47c072
MD
1508/*
1509 * Called from vkernel_trap() to fixup the vkernel's syscall
1510 * frame for vmspace_ctl() return.
1511 */
1512void
1513cpu_vkernel_trap(struct trapframe *frame, int error)
1514{
1515 frame->tf_eax = error;
1516 if (error)
1517 frame->tf_eflags |= PSL_C;
1518 else
1519 frame->tf_eflags &= ~PSL_C;
1520}