FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/trap.c
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 */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: releng/11.1/sys/i386/i386/trap.c 333371 2018-05-08 17:12:10Z gordon $");
42
43 /*
44 * 386 Trap and System call handling
45 */
46
47 #include "opt_clock.h"
48 #include "opt_cpu.h"
49 #include "opt_hwpmc_hooks.h"
50 #include "opt_isa.h"
51 #include "opt_kdb.h"
52 #include "opt_stack.h"
53 #include "opt_trap.h"
54
55 #include <sys/param.h>
56 #include <sys/bus.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/pioctl.h>
60 #include <sys/ptrace.h>
61 #include <sys/kdb.h>
62 #include <sys/kernel.h>
63 #include <sys/ktr.h>
64 #include <sys/lock.h>
65 #include <sys/mutex.h>
66 #include <sys/resourcevar.h>
67 #include <sys/signalvar.h>
68 #include <sys/syscall.h>
69 #include <sys/sysctl.h>
70 #include <sys/sysent.h>
71 #include <sys/uio.h>
72 #include <sys/vmmeter.h>
73 #ifdef HWPMC_HOOKS
74 #include <sys/pmckern.h>
75 PMC_SOFT_DEFINE( , , page_fault, all);
76 PMC_SOFT_DEFINE( , , page_fault, read);
77 PMC_SOFT_DEFINE( , , page_fault, write);
78 #endif
79 #include <security/audit/audit.h>
80
81 #include <vm/vm.h>
82 #include <vm/vm_param.h>
83 #include <vm/pmap.h>
84 #include <vm/vm_kern.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_extern.h>
88
89 #include <machine/cpu.h>
90 #include <machine/intr_machdep.h>
91 #include <x86/mca.h>
92 #include <machine/md_var.h>
93 #include <machine/pcb.h>
94 #ifdef SMP
95 #include <machine/smp.h>
96 #endif
97 #include <machine/stack.h>
98 #include <machine/tss.h>
99 #include <machine/vm86.h>
100
101 #ifdef POWERFAIL_NMI
102 #include <sys/syslog.h>
103 #include <machine/clock.h>
104 #endif
105
106 #ifdef KDTRACE_HOOKS
107 #include <sys/dtrace_bsd.h>
108 #endif
109
110 extern void trap(struct trapframe *frame);
111 extern void syscall(struct trapframe *frame);
112
113 static int trap_pfault(struct trapframe *, int, vm_offset_t);
114 static void trap_fatal(struct trapframe *, vm_offset_t);
115 void dblfault_handler(void);
116
117 extern inthand_t IDTVEC(lcall_syscall);
118
119 extern inthand_t IDTVEC(bpt), IDTVEC(dbg), IDTVEC(int0x80_syscall);
120
121 #define MAX_TRAP_MSG 32
122 static char *trap_msg[] = {
123 "", /* 0 unused */
124 "privileged instruction fault", /* 1 T_PRIVINFLT */
125 "", /* 2 unused */
126 "breakpoint instruction fault", /* 3 T_BPTFLT */
127 "", /* 4 unused */
128 "", /* 5 unused */
129 "arithmetic trap", /* 6 T_ARITHTRAP */
130 "", /* 7 unused */
131 "", /* 8 unused */
132 "general protection fault", /* 9 T_PROTFLT */
133 "trace trap", /* 10 T_TRCTRAP */
134 "", /* 11 unused */
135 "page fault", /* 12 T_PAGEFLT */
136 "", /* 13 unused */
137 "alignment fault", /* 14 T_ALIGNFLT */
138 "", /* 15 unused */
139 "", /* 16 unused */
140 "", /* 17 unused */
141 "integer divide fault", /* 18 T_DIVIDE */
142 "non-maskable interrupt trap", /* 19 T_NMI */
143 "overflow trap", /* 20 T_OFLOW */
144 "FPU bounds check fault", /* 21 T_BOUND */
145 "FPU device not available", /* 22 T_DNA */
146 "double fault", /* 23 T_DOUBLEFLT */
147 "FPU operand fetch fault", /* 24 T_FPOPFLT */
148 "invalid TSS fault", /* 25 T_TSSFLT */
149 "segment not present fault", /* 26 T_SEGNPFLT */
150 "stack fault", /* 27 T_STKFLT */
151 "machine check trap", /* 28 T_MCHK */
152 "SIMD floating-point exception", /* 29 T_XMMFLT */
153 "reserved (unknown) fault", /* 30 T_RESERVED */
154 "", /* 31 unused (reserved) */
155 "DTrace pid return trap", /* 32 T_DTRACE_RET */
156 };
157
158 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
159 int has_f00f_bug = 0; /* Initialized so that it can be patched. */
160 #endif
161
162 static int prot_fault_translation = 0;
163 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
164 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
165 static int uprintf_signal;
166 SYSCTL_INT(_machdep, OID_AUTO, uprintf_signal, CTLFLAG_RW,
167 &uprintf_signal, 0,
168 "Print debugging information on trap signal to ctty");
169
170 /*
171 * Exception, fault, and trap interface to the FreeBSD kernel.
172 * This common code is called from assembly language IDT gate entry
173 * routines that prepare a suitable stack frame, and restore this
174 * frame after the exception has been processed.
175 */
176
177 void
178 trap(struct trapframe *frame)
179 {
180 #ifdef KDTRACE_HOOKS
181 struct reg regs;
182 #endif
183 struct thread *td = curthread;
184 struct proc *p = td->td_proc;
185 #ifdef KDB
186 register_t dr6;
187 #endif
188 int i = 0, ucode = 0;
189 u_int type;
190 register_t addr = 0;
191 vm_offset_t eva;
192 ksiginfo_t ksi;
193 #ifdef POWERFAIL_NMI
194 static int lastalert = 0;
195 #endif
196
197 PCPU_INC(cnt.v_trap);
198 type = frame->tf_trapno;
199
200 #ifdef SMP
201 /* Handler for NMI IPIs used for stopping CPUs. */
202 if (type == T_NMI) {
203 if (ipi_nmi_handler() == 0)
204 goto out;
205 }
206 #endif /* SMP */
207
208 #ifdef KDB
209 if (kdb_active) {
210 kdb_reenter();
211 goto out;
212 }
213 #endif
214
215 if (type == T_RESERVED) {
216 trap_fatal(frame, 0);
217 goto out;
218 }
219
220 if (type == T_NMI) {
221 #ifdef HWPMC_HOOKS
222 /*
223 * CPU PMCs interrupt using an NMI so we check for that first.
224 * If the HWPMC module is active, 'pmc_hook' will point to
225 * the function to be called. A non-zero return value from the
226 * hook means that the NMI was consumed by it and that we can
227 * return immediately.
228 */
229 if (pmc_intr != NULL &&
230 (*pmc_intr)(PCPU_GET(cpuid), frame) != 0)
231 goto out;
232 #endif
233
234 #ifdef STACK
235 if (stack_nmi_handler(frame) != 0)
236 goto out;
237 #endif
238 }
239
240 if (type == T_MCHK) {
241 mca_intr();
242 goto out;
243 }
244
245 #ifdef KDTRACE_HOOKS
246 /*
247 * A trap can occur while DTrace executes a probe. Before
248 * executing the probe, DTrace blocks re-scheduling and sets
249 * a flag in its per-cpu flags to indicate that it doesn't
250 * want to fault. On returning from the probe, the no-fault
251 * flag is cleared and finally re-scheduling is enabled.
252 */
253 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
254 dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
255 goto out;
256 #endif
257
258 if ((frame->tf_eflags & PSL_I) == 0) {
259 /*
260 * Buggy application or kernel code has disabled
261 * interrupts and then trapped. Enabling interrupts
262 * now is wrong, but it is better than running with
263 * interrupts disabled until they are accidentally
264 * enabled later.
265 */
266 if (TRAPF_USERMODE(frame) &&
267 (curpcb->pcb_flags & PCB_VM86CALL) == 0)
268 uprintf(
269 "pid %ld (%s): trap %d with interrupts disabled\n",
270 (long)curproc->p_pid, curthread->td_name, type);
271 else if (type != T_NMI && type != T_BPTFLT &&
272 type != T_TRCTRAP &&
273 frame->tf_eip != (int)cpu_switch_load_gs) {
274 /*
275 * XXX not quite right, since this may be for a
276 * multiple fault in user mode.
277 */
278 printf("kernel trap %d with interrupts disabled\n",
279 type);
280 /*
281 * Page faults need interrupts disabled until later,
282 * and we shouldn't enable interrupts while holding
283 * a spin lock.
284 */
285 if (type != T_PAGEFLT &&
286 td->td_md.md_spinlock_count == 0)
287 enable_intr();
288 }
289 }
290 eva = 0;
291 if (type == T_PAGEFLT) {
292 /*
293 * For some Cyrix CPUs, %cr2 is clobbered by
294 * interrupts. This problem is worked around by using
295 * an interrupt gate for the pagefault handler. We
296 * are finally ready to read %cr2 and conditionally
297 * reenable interrupts. If we hold a spin lock, then
298 * we must not reenable interrupts. This might be a
299 * spurious page fault.
300 */
301 eva = rcr2();
302 if (td->td_md.md_spinlock_count == 0)
303 enable_intr();
304 }
305
306 if (TRAPF_USERMODE(frame) && (curpcb->pcb_flags & PCB_VM86CALL) == 0) {
307 /* user trap */
308
309 td->td_pticks = 0;
310 td->td_frame = frame;
311 addr = frame->tf_eip;
312 if (td->td_cowgen != p->p_cowgen)
313 thread_cow_update(td);
314
315 switch (type) {
316 case T_PRIVINFLT: /* privileged instruction fault */
317 i = SIGILL;
318 ucode = ILL_PRVOPC;
319 break;
320
321 case T_BPTFLT: /* bpt instruction fault */
322 case T_TRCTRAP: /* trace trap */
323 enable_intr();
324 #ifdef KDTRACE_HOOKS
325 if (type == T_BPTFLT) {
326 fill_frame_regs(frame, ®s);
327 if (dtrace_pid_probe_ptr != NULL &&
328 dtrace_pid_probe_ptr(®s) == 0)
329 goto out;
330 }
331 #endif
332 user_trctrap_out:
333 frame->tf_eflags &= ~PSL_T;
334 i = SIGTRAP;
335 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
336 break;
337
338 case T_ARITHTRAP: /* arithmetic trap */
339 ucode = npxtrap_x87();
340 if (ucode == -1)
341 goto userout;
342 i = SIGFPE;
343 break;
344
345 /*
346 * The following two traps can happen in
347 * vm86 mode, and, if so, we want to handle
348 * them specially.
349 */
350 case T_PROTFLT: /* general protection fault */
351 case T_STKFLT: /* stack fault */
352 if (frame->tf_eflags & PSL_VM) {
353 i = vm86_emulate((struct vm86frame *)frame);
354 if (i == SIGTRAP) {
355 type = T_TRCTRAP;
356 load_dr6(rdr6() | 0x4000);
357 goto user_trctrap_out;
358 }
359 if (i == 0)
360 goto user;
361 break;
362 }
363 i = SIGBUS;
364 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
365 break;
366 case T_SEGNPFLT: /* segment not present fault */
367 i = SIGBUS;
368 ucode = BUS_ADRERR;
369 break;
370 case T_TSSFLT: /* invalid TSS fault */
371 i = SIGBUS;
372 ucode = BUS_OBJERR;
373 break;
374 case T_ALIGNFLT:
375 i = SIGBUS;
376 ucode = BUS_ADRALN;
377 break;
378 case T_DOUBLEFLT: /* double fault */
379 default:
380 i = SIGBUS;
381 ucode = BUS_OBJERR;
382 break;
383
384 case T_PAGEFLT: /* page fault */
385
386 i = trap_pfault(frame, TRUE, eva);
387 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
388 if (i == -2) {
389 /*
390 * The f00f hack workaround has triggered, so
391 * treat the fault as an illegal instruction
392 * (T_PRIVINFLT) instead of a page fault.
393 */
394 type = frame->tf_trapno = T_PRIVINFLT;
395
396 /* Proceed as in that case. */
397 ucode = ILL_PRVOPC;
398 i = SIGILL;
399 break;
400 }
401 #endif
402 if (i == -1)
403 goto userout;
404 if (i == 0)
405 goto user;
406
407 if (i == SIGSEGV)
408 ucode = SEGV_MAPERR;
409 else {
410 if (prot_fault_translation == 0) {
411 /*
412 * Autodetect.
413 * This check also covers the images
414 * without the ABI-tag ELF note.
415 */
416 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
417 && p->p_osrel >= P_OSREL_SIGSEGV) {
418 i = SIGSEGV;
419 ucode = SEGV_ACCERR;
420 } else {
421 i = SIGBUS;
422 ucode = BUS_PAGE_FAULT;
423 }
424 } else if (prot_fault_translation == 1) {
425 /*
426 * Always compat mode.
427 */
428 i = SIGBUS;
429 ucode = BUS_PAGE_FAULT;
430 } else {
431 /*
432 * Always SIGSEGV mode.
433 */
434 i = SIGSEGV;
435 ucode = SEGV_ACCERR;
436 }
437 }
438 addr = eva;
439 break;
440
441 case T_DIVIDE: /* integer divide fault */
442 ucode = FPE_INTDIV;
443 i = SIGFPE;
444 break;
445
446 #ifdef DEV_ISA
447 case T_NMI:
448 #ifdef POWERFAIL_NMI
449 #ifndef TIMER_FREQ
450 # define TIMER_FREQ 1193182
451 #endif
452 if (time_second - lastalert > 10) {
453 log(LOG_WARNING, "NMI: power fail\n");
454 sysbeep(880, hz);
455 lastalert = time_second;
456 }
457 goto userout;
458 #else /* !POWERFAIL_NMI */
459 nmi_handle_intr(type, frame);
460 break;
461 #endif /* POWERFAIL_NMI */
462 #endif /* DEV_ISA */
463
464 case T_OFLOW: /* integer overflow fault */
465 ucode = FPE_INTOVF;
466 i = SIGFPE;
467 break;
468
469 case T_BOUND: /* bounds check fault */
470 ucode = FPE_FLTSUB;
471 i = SIGFPE;
472 break;
473
474 case T_DNA:
475 KASSERT(PCB_USER_FPU(td->td_pcb),
476 ("kernel FPU ctx has leaked"));
477 /* transparent fault (due to context switch "late") */
478 if (npxdna())
479 goto userout;
480 uprintf("pid %d killed due to lack of floating point\n",
481 p->p_pid);
482 i = SIGKILL;
483 ucode = 0;
484 break;
485
486 case T_FPOPFLT: /* FPU operand fetch fault */
487 ucode = ILL_COPROC;
488 i = SIGILL;
489 break;
490
491 case T_XMMFLT: /* SIMD floating-point exception */
492 ucode = npxtrap_sse();
493 if (ucode == -1)
494 goto userout;
495 i = SIGFPE;
496 break;
497 #ifdef KDTRACE_HOOKS
498 case T_DTRACE_RET:
499 enable_intr();
500 fill_frame_regs(frame, ®s);
501 if (dtrace_return_probe_ptr != NULL &&
502 dtrace_return_probe_ptr(®s) == 0)
503 goto out;
504 break;
505 #endif
506 }
507 } else {
508 /* kernel trap */
509
510 KASSERT(cold || td->td_ucred != NULL,
511 ("kernel trap doesn't have ucred"));
512 switch (type) {
513 case T_PAGEFLT: /* page fault */
514 (void) trap_pfault(frame, FALSE, eva);
515 goto out;
516
517 case T_DNA:
518 if (PCB_USER_FPU(td->td_pcb))
519 panic("Unregistered use of FPU in kernel");
520 if (npxdna())
521 goto out;
522 break;
523
524 case T_ARITHTRAP: /* arithmetic trap */
525 case T_XMMFLT: /* SIMD floating-point exception */
526 case T_FPOPFLT: /* FPU operand fetch fault */
527 /*
528 * XXXKIB for now disable any FPU traps in kernel
529 * handler registration seems to be overkill
530 */
531 trap_fatal(frame, 0);
532 goto out;
533
534 /*
535 * The following two traps can happen in
536 * vm86 mode, and, if so, we want to handle
537 * them specially.
538 */
539 case T_PROTFLT: /* general protection fault */
540 case T_STKFLT: /* stack fault */
541 if (frame->tf_eflags & PSL_VM) {
542 i = vm86_emulate((struct vm86frame *)frame);
543 if (i == SIGTRAP) {
544 type = T_TRCTRAP;
545 load_dr6(rdr6() | 0x4000);
546 goto kernel_trctrap;
547 }
548 if (i != 0)
549 /*
550 * returns to original process
551 */
552 vm86_trap((struct vm86frame *)frame);
553 goto out;
554 }
555 if (type == T_STKFLT)
556 break;
557
558 /* FALL THROUGH */
559
560 case T_SEGNPFLT: /* segment not present fault */
561 if (curpcb->pcb_flags & PCB_VM86CALL)
562 break;
563
564 /*
565 * Invalid %fs's and %gs's can be created using
566 * procfs or PT_SETREGS or by invalidating the
567 * underlying LDT entry. This causes a fault
568 * in kernel mode when the kernel attempts to
569 * switch contexts. Lose the bad context
570 * (XXX) so that we can continue, and generate
571 * a signal.
572 */
573 if (frame->tf_eip == (int)cpu_switch_load_gs) {
574 curpcb->pcb_gs = 0;
575 #if 0
576 PROC_LOCK(p);
577 kern_psignal(p, SIGBUS);
578 PROC_UNLOCK(p);
579 #endif
580 goto out;
581 }
582
583 if (td->td_intr_nesting_level != 0)
584 break;
585
586 /*
587 * Invalid segment selectors and out of bounds
588 * %eip's and %esp's can be set up in user mode.
589 * This causes a fault in kernel mode when the
590 * kernel tries to return to user mode. We want
591 * to get this fault so that we can fix the
592 * problem here and not have to check all the
593 * selectors and pointers when the user changes
594 * them.
595 */
596 if (frame->tf_eip == (int)doreti_iret) {
597 frame->tf_eip = (int)doreti_iret_fault;
598 goto out;
599 }
600 if (frame->tf_eip == (int)doreti_popl_ds) {
601 frame->tf_eip = (int)doreti_popl_ds_fault;
602 goto out;
603 }
604 if (frame->tf_eip == (int)doreti_popl_es) {
605 frame->tf_eip = (int)doreti_popl_es_fault;
606 goto out;
607 }
608 if (frame->tf_eip == (int)doreti_popl_fs) {
609 frame->tf_eip = (int)doreti_popl_fs_fault;
610 goto out;
611 }
612 if (curpcb->pcb_onfault != NULL) {
613 frame->tf_eip =
614 (int)curpcb->pcb_onfault;
615 goto out;
616 }
617 break;
618
619 case T_TSSFLT:
620 /*
621 * PSL_NT can be set in user mode and isn't cleared
622 * automatically when the kernel is entered. This
623 * causes a TSS fault when the kernel attempts to
624 * `iret' because the TSS link is uninitialized. We
625 * want to get this fault so that we can fix the
626 * problem here and not every time the kernel is
627 * entered.
628 */
629 if (frame->tf_eflags & PSL_NT) {
630 frame->tf_eflags &= ~PSL_NT;
631 goto out;
632 }
633 break;
634
635 case T_TRCTRAP: /* trace trap */
636 kernel_trctrap:
637 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
638 /*
639 * We've just entered system mode via the
640 * syscall lcall. Continue single stepping
641 * silently until the syscall handler has
642 * saved the flags.
643 */
644 goto out;
645 }
646 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
647 /*
648 * The syscall handler has now saved the
649 * flags. Stop single stepping it.
650 */
651 frame->tf_eflags &= ~PSL_T;
652 goto out;
653 }
654 /*
655 * Ignore debug register trace traps due to
656 * accesses in the user's address space, which
657 * can happen under several conditions such as
658 * if a user sets a watchpoint on a buffer and
659 * then passes that buffer to a system call.
660 * We still want to get TRCTRAPS for addresses
661 * in kernel space because that is useful when
662 * debugging the kernel.
663 */
664 if (user_dbreg_trap() &&
665 !(curpcb->pcb_flags & PCB_VM86CALL)) {
666 /*
667 * Reset breakpoint bits because the
668 * processor doesn't
669 */
670 load_dr6(rdr6() & ~0xf);
671 goto out;
672 }
673
674 /*
675 * Malicious user code can configure a debug
676 * register watchpoint to trap on data access
677 * to the top of stack and then execute 'pop
678 * %ss; int 3'. Due to exception deferral for
679 * 'pop %ss', the CPU will not interrupt 'int
680 * 3' to raise the DB# exception for the debug
681 * register but will postpone the DB# until
682 * execution of the first instruction of the
683 * BP# handler (in kernel mode). Normally the
684 * previous check would ignore DB# exceptions
685 * for watchpoints on user addresses raised in
686 * kernel mode. However, some CPU errata
687 * include cases where DB# exceptions do not
688 * properly set bits in %dr6, e.g. Haswell
689 * HSD23 and Skylake-X SKZ24.
690 *
691 * A deferred DB# can also be raised on the
692 * first instructions of system call entry
693 * points or single-step traps via similar use
694 * of 'pop %ss' or 'mov xxx, %ss'.
695 */
696 if (frame->tf_eip ==
697 (uintptr_t)IDTVEC(int0x80_syscall) ||
698 frame->tf_eip == (uintptr_t)IDTVEC(bpt) ||
699 frame->tf_eip == (uintptr_t)IDTVEC(dbg))
700 return;
701 /*
702 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
703 */
704 case T_BPTFLT:
705 /*
706 * If KDB is enabled, let it handle the debugger trap.
707 * Otherwise, debugger traps "can't happen".
708 */
709 #ifdef KDB
710 /* XXX %dr6 is not quite reentrant. */
711 dr6 = rdr6();
712 load_dr6(dr6 & ~0x4000);
713 if (kdb_trap(type, dr6, frame))
714 goto out;
715 #endif
716 break;
717
718 #ifdef DEV_ISA
719 case T_NMI:
720 #ifdef POWERFAIL_NMI
721 if (time_second - lastalert > 10) {
722 log(LOG_WARNING, "NMI: power fail\n");
723 sysbeep(880, hz);
724 lastalert = time_second;
725 }
726 goto out;
727 #else /* !POWERFAIL_NMI */
728 nmi_handle_intr(type, frame);
729 goto out;
730 #endif /* POWERFAIL_NMI */
731 #endif /* DEV_ISA */
732 }
733
734 trap_fatal(frame, eva);
735 goto out;
736 }
737
738 /* Translate fault for emulators (e.g. Linux) */
739 if (*p->p_sysent->sv_transtrap)
740 i = (*p->p_sysent->sv_transtrap)(i, type);
741
742 ksiginfo_init_trap(&ksi);
743 ksi.ksi_signo = i;
744 ksi.ksi_code = ucode;
745 ksi.ksi_addr = (void *)addr;
746 ksi.ksi_trapno = type;
747 if (uprintf_signal) {
748 uprintf("pid %d comm %s: signal %d err %x code %d type %d "
749 "addr 0x%x esp 0x%08x eip 0x%08x "
750 "<%02x %02x %02x %02x %02x %02x %02x %02x>\n",
751 p->p_pid, p->p_comm, i, frame->tf_err, ucode, type, addr,
752 frame->tf_esp, frame->tf_eip,
753 fubyte((void *)(frame->tf_eip + 0)),
754 fubyte((void *)(frame->tf_eip + 1)),
755 fubyte((void *)(frame->tf_eip + 2)),
756 fubyte((void *)(frame->tf_eip + 3)),
757 fubyte((void *)(frame->tf_eip + 4)),
758 fubyte((void *)(frame->tf_eip + 5)),
759 fubyte((void *)(frame->tf_eip + 6)),
760 fubyte((void *)(frame->tf_eip + 7)));
761 }
762 KASSERT((read_eflags() & PSL_I) != 0, ("interrupts disabled"));
763 trapsignal(td, &ksi);
764
765 #ifdef DEBUG
766 if (type <= MAX_TRAP_MSG) {
767 uprintf("fatal process exception: %s",
768 trap_msg[type]);
769 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
770 uprintf(", fault VA = 0x%lx", (u_long)eva);
771 uprintf("\n");
772 }
773 #endif
774
775 user:
776 userret(td, frame);
777 KASSERT(PCB_USER_FPU(td->td_pcb),
778 ("Return from trap with kernel FPU ctx leaked"));
779 userout:
780 out:
781 return;
782 }
783
784 static int
785 trap_pfault(frame, usermode, eva)
786 struct trapframe *frame;
787 int usermode;
788 vm_offset_t eva;
789 {
790 vm_offset_t va;
791 vm_map_t map;
792 int rv = 0;
793 vm_prot_t ftype;
794 struct thread *td = curthread;
795 struct proc *p = td->td_proc;
796
797 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
798 /*
799 * Due to both processor errata and lazy TLB invalidation when
800 * access restrictions are removed from virtual pages, memory
801 * accesses that are allowed by the physical mapping layer may
802 * nonetheless cause one spurious page fault per virtual page.
803 * When the thread is executing a "no faulting" section that
804 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
805 * every page fault is treated as a spurious page fault,
806 * unless it accesses the same virtual address as the most
807 * recent page fault within the same "no faulting" section.
808 */
809 if (td->td_md.md_spurflt_addr != eva ||
810 (td->td_pflags & TDP_RESETSPUR) != 0) {
811 /*
812 * Do nothing to the TLB. A stale TLB entry is
813 * flushed automatically by a page fault.
814 */
815 td->td_md.md_spurflt_addr = eva;
816 td->td_pflags &= ~TDP_RESETSPUR;
817 return (0);
818 }
819 } else {
820 /*
821 * If we get a page fault while in a critical section, then
822 * it is most likely a fatal kernel page fault. The kernel
823 * is already going to panic trying to get a sleep lock to
824 * do the VM lookup, so just consider it a fatal trap so the
825 * kernel can print out a useful trap message and even get
826 * to the debugger.
827 *
828 * If we get a page fault while holding a non-sleepable
829 * lock, then it is most likely a fatal kernel page fault.
830 * If WITNESS is enabled, then it's going to whine about
831 * bogus LORs with various VM locks, so just skip to the
832 * fatal trap handling directly.
833 */
834 if (td->td_critnest != 0 ||
835 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
836 "Kernel page fault") != 0) {
837 trap_fatal(frame, eva);
838 return (-1);
839 }
840 }
841 va = trunc_page(eva);
842 if (va >= KERNBASE) {
843 /*
844 * Don't allow user-mode faults in kernel address space.
845 * An exception: if the faulting address is the invalid
846 * instruction entry in the IDT, then the Intel Pentium
847 * F00F bug workaround was triggered, and we need to
848 * treat it is as an illegal instruction, and not a page
849 * fault.
850 */
851 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
852 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
853 return (-2);
854 #endif
855 if (usermode)
856 goto nogo;
857
858 map = kernel_map;
859 } else {
860 map = &p->p_vmspace->vm_map;
861
862 /*
863 * When accessing a user-space address, kernel must be
864 * ready to accept the page fault, and provide a
865 * handling routine. Since accessing the address
866 * without the handler is a bug, do not try to handle
867 * it normally, and panic immediately.
868 */
869 if (!usermode && (td->td_intr_nesting_level != 0 ||
870 curpcb->pcb_onfault == NULL)) {
871 trap_fatal(frame, eva);
872 return (-1);
873 }
874 }
875
876 /*
877 * If the trap was caused by errant bits in the PTE then panic.
878 */
879 if (frame->tf_err & PGEX_RSV) {
880 trap_fatal(frame, eva);
881 return (-1);
882 }
883
884 /*
885 * PGEX_I is defined only if the execute disable bit capability is
886 * supported and enabled.
887 */
888 if (frame->tf_err & PGEX_W)
889 ftype = VM_PROT_WRITE;
890 #if defined(PAE) || defined(PAE_TABLES)
891 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
892 ftype = VM_PROT_EXECUTE;
893 #endif
894 else
895 ftype = VM_PROT_READ;
896
897 /* Fault in the page. */
898 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
899 if (rv == KERN_SUCCESS) {
900 #ifdef HWPMC_HOOKS
901 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
902 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
903 if (ftype == VM_PROT_READ)
904 PMC_SOFT_CALL_TF( , , page_fault, read,
905 frame);
906 else
907 PMC_SOFT_CALL_TF( , , page_fault, write,
908 frame);
909 }
910 #endif
911 return (0);
912 }
913 nogo:
914 if (!usermode) {
915 if (td->td_intr_nesting_level == 0 &&
916 curpcb->pcb_onfault != NULL) {
917 frame->tf_eip = (int)curpcb->pcb_onfault;
918 return (0);
919 }
920 trap_fatal(frame, eva);
921 return (-1);
922 }
923 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
924 }
925
926 static void
927 trap_fatal(frame, eva)
928 struct trapframe *frame;
929 vm_offset_t eva;
930 {
931 int code, ss, esp;
932 u_int type;
933 struct soft_segment_descriptor softseg;
934 char *msg;
935
936 code = frame->tf_err;
937 type = frame->tf_trapno;
938 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
939
940 if (type <= MAX_TRAP_MSG)
941 msg = trap_msg[type];
942 else
943 msg = "UNKNOWN";
944 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
945 frame->tf_eflags & PSL_VM ? "vm86" :
946 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
947 #ifdef SMP
948 /* two separate prints in case of a trap on an unmapped page */
949 printf("cpuid = %d; ", PCPU_GET(cpuid));
950 printf("apic id = %02x\n", PCPU_GET(apic_id));
951 #endif
952 if (type == T_PAGEFLT) {
953 printf("fault virtual address = 0x%x\n", eva);
954 printf("fault code = %s %s%s, %s\n",
955 code & PGEX_U ? "user" : "supervisor",
956 code & PGEX_W ? "write" : "read",
957 #if defined(PAE) || defined(PAE_TABLES)
958 pg_nx != 0 ?
959 (code & PGEX_I ? " instruction" : " data") :
960 #endif
961 "",
962 code & PGEX_RSV ? "reserved bits in PTE" :
963 code & PGEX_P ? "protection violation" : "page not present");
964 }
965 printf("instruction pointer = 0x%x:0x%x\n",
966 frame->tf_cs & 0xffff, frame->tf_eip);
967 if (TF_HAS_STACKREGS(frame)) {
968 ss = frame->tf_ss & 0xffff;
969 esp = frame->tf_esp;
970 } else {
971 ss = GSEL(GDATA_SEL, SEL_KPL);
972 esp = (int)&frame->tf_esp;
973 }
974 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
975 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
976 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
977 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
978 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
979 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
980 softseg.ssd_gran);
981 printf("processor eflags = ");
982 if (frame->tf_eflags & PSL_T)
983 printf("trace trap, ");
984 if (frame->tf_eflags & PSL_I)
985 printf("interrupt enabled, ");
986 if (frame->tf_eflags & PSL_NT)
987 printf("nested task, ");
988 if (frame->tf_eflags & PSL_RF)
989 printf("resume, ");
990 if (frame->tf_eflags & PSL_VM)
991 printf("vm86, ");
992 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
993 printf("current process = %d (%s)\n",
994 curproc->p_pid, curthread->td_name);
995
996 #ifdef KDB
997 if (debugger_on_panic || kdb_active) {
998 frame->tf_err = eva; /* smuggle fault address to ddb */
999 if (kdb_trap(type, 0, frame)) {
1000 frame->tf_err = code; /* restore error code */
1001 return;
1002 }
1003 frame->tf_err = code; /* restore error code */
1004 }
1005 #endif
1006 printf("trap number = %d\n", type);
1007 if (type <= MAX_TRAP_MSG)
1008 panic("%s", trap_msg[type]);
1009 else
1010 panic("unknown/reserved trap");
1011 }
1012
1013 /*
1014 * Double fault handler. Called when a fault occurs while writing
1015 * a frame for a trap/exception onto the stack. This usually occurs
1016 * when the stack overflows (such is the case with infinite recursion,
1017 * for example).
1018 *
1019 * XXX Note that the current PTD gets replaced by IdlePTD when the
1020 * task switch occurs. This means that the stack that was active at
1021 * the time of the double fault is not available at <kstack> unless
1022 * the machine was idle when the double fault occurred. The downside
1023 * of this is that "trace <ebp>" in ddb won't work.
1024 */
1025 void
1026 dblfault_handler()
1027 {
1028 #ifdef KDTRACE_HOOKS
1029 if (dtrace_doubletrap_func != NULL)
1030 (*dtrace_doubletrap_func)();
1031 #endif
1032 printf("\nFatal double fault:\n");
1033 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
1034 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
1035 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
1036 #ifdef SMP
1037 /* two separate prints in case of a trap on an unmapped page */
1038 printf("cpuid = %d; ", PCPU_GET(cpuid));
1039 printf("apic id = %02x\n", PCPU_GET(apic_id));
1040 #endif
1041 panic("double fault");
1042 }
1043
1044 int
1045 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
1046 {
1047 struct proc *p;
1048 struct trapframe *frame;
1049 caddr_t params;
1050 long tmp;
1051 int error;
1052
1053 p = td->td_proc;
1054 frame = td->td_frame;
1055
1056 params = (caddr_t)frame->tf_esp + sizeof(int);
1057 sa->code = frame->tf_eax;
1058
1059 /*
1060 * Need to check if this is a 32 bit or 64 bit syscall.
1061 */
1062 if (sa->code == SYS_syscall) {
1063 /*
1064 * Code is first argument, followed by actual args.
1065 */
1066 error = fueword(params, &tmp);
1067 if (error == -1)
1068 return (EFAULT);
1069 sa->code = tmp;
1070 params += sizeof(int);
1071 } else if (sa->code == SYS___syscall) {
1072 /*
1073 * Like syscall, but code is a quad, so as to maintain
1074 * quad alignment for the rest of the arguments.
1075 */
1076 error = fueword(params, &tmp);
1077 if (error == -1)
1078 return (EFAULT);
1079 sa->code = tmp;
1080 params += sizeof(quad_t);
1081 }
1082
1083 if (p->p_sysent->sv_mask)
1084 sa->code &= p->p_sysent->sv_mask;
1085 if (sa->code >= p->p_sysent->sv_size)
1086 sa->callp = &p->p_sysent->sv_table[0];
1087 else
1088 sa->callp = &p->p_sysent->sv_table[sa->code];
1089 sa->narg = sa->callp->sy_narg;
1090
1091 if (params != NULL && sa->narg != 0)
1092 error = copyin(params, (caddr_t)sa->args,
1093 (u_int)(sa->narg * sizeof(int)));
1094 else
1095 error = 0;
1096
1097 if (error == 0) {
1098 td->td_retval[0] = 0;
1099 td->td_retval[1] = frame->tf_edx;
1100 }
1101
1102 return (error);
1103 }
1104
1105 #include "../../kern/subr_syscall.c"
1106
1107 /*
1108 * syscall - system call request C handler. A system call is
1109 * essentially treated as a trap by reusing the frame layout.
1110 */
1111 void
1112 syscall(struct trapframe *frame)
1113 {
1114 struct thread *td;
1115 struct syscall_args sa;
1116 register_t orig_tf_eflags;
1117 int error;
1118 ksiginfo_t ksi;
1119
1120 #ifdef DIAGNOSTIC
1121 if (!(TRAPF_USERMODE(frame) &&
1122 (curpcb->pcb_flags & PCB_VM86CALL) == 0)) {
1123 panic("syscall");
1124 /* NOT REACHED */
1125 }
1126 #endif
1127 orig_tf_eflags = frame->tf_eflags;
1128
1129 td = curthread;
1130 td->td_frame = frame;
1131
1132 error = syscallenter(td, &sa);
1133
1134 /*
1135 * Traced syscall.
1136 */
1137 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1138 frame->tf_eflags &= ~PSL_T;
1139 ksiginfo_init_trap(&ksi);
1140 ksi.ksi_signo = SIGTRAP;
1141 ksi.ksi_code = TRAP_TRACE;
1142 ksi.ksi_addr = (void *)frame->tf_eip;
1143 trapsignal(td, &ksi);
1144 }
1145
1146 KASSERT(PCB_USER_FPU(td->td_pcb),
1147 ("System call %s returning with kernel FPU ctx leaked",
1148 syscallname(td->td_proc, sa.code)));
1149 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
1150 ("System call %s returning with mangled pcb_save",
1151 syscallname(td->td_proc, sa.code)));
1152
1153 syscallret(td, error, &sa);
1154 }
Cache object: 1fc89c5368ccc7eea0eaf607228b6e70
|