FreeBSD/Linux Kernel Cross Reference
sys/amd64/amd64/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/9.1/sys/amd64/amd64/trap.c 237559 2012-06-25 09:41:47Z kib $");
42
43 /*
44 * AMD64 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_kdtrace.h"
53
54 #include <sys/param.h>
55 #include <sys/bus.h>
56 #include <sys/systm.h>
57 #include <sys/proc.h>
58 #include <sys/pioctl.h>
59 #include <sys/ptrace.h>
60 #include <sys/kdb.h>
61 #include <sys/kernel.h>
62 #include <sys/ktr.h>
63 #include <sys/lock.h>
64 #include <sys/mutex.h>
65 #include <sys/resourcevar.h>
66 #include <sys/signalvar.h>
67 #include <sys/syscall.h>
68 #include <sys/sysctl.h>
69 #include <sys/sysent.h>
70 #include <sys/uio.h>
71 #include <sys/vmmeter.h>
72 #ifdef HWPMC_HOOKS
73 #include <sys/pmckern.h>
74 PMC_SOFT_DEFINE( , , page_fault, all);
75 PMC_SOFT_DEFINE( , , page_fault, read);
76 PMC_SOFT_DEFINE( , , page_fault, write);
77 #endif
78
79 #include <vm/vm.h>
80 #include <vm/vm_param.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>
88 #include <machine/intr_machdep.h>
89 #include <x86/mca.h>
90 #include <machine/md_var.h>
91 #include <machine/pcb.h>
92 #ifdef SMP
93 #include <machine/smp.h>
94 #endif
95 #include <machine/tss.h>
96
97 #ifdef KDTRACE_HOOKS
98 #include <sys/dtrace_bsd.h>
99
100 /*
101 * This is a hook which is initialised by the dtrace module
102 * to handle traps which might occur during DTrace probe
103 * execution.
104 */
105 dtrace_trap_func_t dtrace_trap_func;
106
107 dtrace_doubletrap_func_t dtrace_doubletrap_func;
108
109 /*
110 * This is a hook which is initialised by the systrace module
111 * when it is loaded. This keeps the DTrace syscall provider
112 * implementation opaque.
113 */
114 systrace_probe_func_t systrace_probe_func;
115
116 /*
117 * These hooks are necessary for the pid, usdt and fasttrap providers.
118 */
119 dtrace_fasttrap_probe_ptr_t dtrace_fasttrap_probe_ptr;
120 dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr;
121 dtrace_return_probe_ptr_t dtrace_return_probe_ptr;
122 #endif
123
124 extern void trap(struct trapframe *frame);
125 extern void syscall(struct trapframe *frame);
126 void dblfault_handler(struct trapframe *frame);
127
128 static int trap_pfault(struct trapframe *, int);
129 static void trap_fatal(struct trapframe *, vm_offset_t);
130
131 #define MAX_TRAP_MSG 33
132 static char *trap_msg[] = {
133 "", /* 0 unused */
134 "privileged instruction fault", /* 1 T_PRIVINFLT */
135 "", /* 2 unused */
136 "breakpoint instruction fault", /* 3 T_BPTFLT */
137 "", /* 4 unused */
138 "", /* 5 unused */
139 "arithmetic trap", /* 6 T_ARITHTRAP */
140 "", /* 7 unused */
141 "", /* 8 unused */
142 "general protection fault", /* 9 T_PROTFLT */
143 "trace trap", /* 10 T_TRCTRAP */
144 "", /* 11 unused */
145 "page fault", /* 12 T_PAGEFLT */
146 "", /* 13 unused */
147 "alignment fault", /* 14 T_ALIGNFLT */
148 "", /* 15 unused */
149 "", /* 16 unused */
150 "", /* 17 unused */
151 "integer divide fault", /* 18 T_DIVIDE */
152 "non-maskable interrupt trap", /* 19 T_NMI */
153 "overflow trap", /* 20 T_OFLOW */
154 "FPU bounds check fault", /* 21 T_BOUND */
155 "FPU device not available", /* 22 T_DNA */
156 "double fault", /* 23 T_DOUBLEFLT */
157 "FPU operand fetch fault", /* 24 T_FPOPFLT */
158 "invalid TSS fault", /* 25 T_TSSFLT */
159 "segment not present fault", /* 26 T_SEGNPFLT */
160 "stack fault", /* 27 T_STKFLT */
161 "machine check trap", /* 28 T_MCHK */
162 "SIMD floating-point exception", /* 29 T_XMMFLT */
163 "reserved (unknown) fault", /* 30 T_RESERVED */
164 "", /* 31 unused (reserved) */
165 "DTrace pid return trap", /* 32 T_DTRACE_RET */
166 "DTrace fasttrap probe trap", /* 33 T_DTRACE_PROBE */
167 };
168
169 #ifdef KDB
170 static int kdb_on_nmi = 1;
171 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
172 &kdb_on_nmi, 0, "Go to KDB on NMI");
173 TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi);
174 #endif
175 static int panic_on_nmi = 1;
176 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
177 &panic_on_nmi, 0, "Panic on NMI");
178 TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi);
179 static int prot_fault_translation = 0;
180 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
181 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
182
183 /*
184 * Exception, fault, and trap interface to the FreeBSD kernel.
185 * This common code is called from assembly language IDT gate entry
186 * routines that prepare a suitable stack frame, and restore this
187 * frame after the exception has been processed.
188 */
189
190 void
191 trap(struct trapframe *frame)
192 {
193 struct thread *td = curthread;
194 struct proc *p = td->td_proc;
195 int i = 0, ucode = 0, code;
196 u_int type;
197 register_t addr = 0;
198 ksiginfo_t ksi;
199
200 PCPU_INC(cnt.v_trap);
201 type = frame->tf_trapno;
202
203 #ifdef SMP
204 /* Handler for NMI IPIs used for stopping CPUs. */
205 if (type == T_NMI) {
206 if (ipi_nmi_handler() == 0)
207 goto out;
208 }
209 #endif /* SMP */
210
211 #ifdef KDB
212 if (kdb_active) {
213 kdb_reenter();
214 goto out;
215 }
216 #endif
217
218 if (type == T_RESERVED) {
219 trap_fatal(frame, 0);
220 goto out;
221 }
222
223 #ifdef HWPMC_HOOKS
224 /*
225 * CPU PMCs interrupt using an NMI. If the PMC module is
226 * active, pass the 'rip' value to the PMC module's interrupt
227 * handler. A return value of '1' from the handler means that
228 * the NMI was handled by it and we can return immediately.
229 */
230 if (type == T_NMI && pmc_intr &&
231 (*pmc_intr)(PCPU_GET(cpuid), frame))
232 goto out;
233 #endif
234
235 if (type == T_MCHK) {
236 mca_intr();
237 goto out;
238 }
239
240 #ifdef KDTRACE_HOOKS
241 /*
242 * A trap can occur while DTrace executes a probe. Before
243 * executing the probe, DTrace blocks re-scheduling and sets
244 * a flag in it's per-cpu flags to indicate that it doesn't
245 * want to fault. On returning from the probe, the no-fault
246 * flag is cleared and finally re-scheduling is enabled.
247 *
248 * If the DTrace kernel module has registered a trap handler,
249 * call it and if it returns non-zero, assume that it has
250 * handled the trap and modified the trap frame so that this
251 * function can return normally.
252 */
253 if (type == T_DTRACE_PROBE || type == T_DTRACE_RET ||
254 type == T_BPTFLT) {
255 struct reg regs;
256
257 fill_frame_regs(frame, ®s);
258 if (type == T_DTRACE_PROBE &&
259 dtrace_fasttrap_probe_ptr != NULL &&
260 dtrace_fasttrap_probe_ptr(®s) == 0)
261 goto out;
262 else if (type == T_BPTFLT &&
263 dtrace_pid_probe_ptr != NULL &&
264 dtrace_pid_probe_ptr(®s) == 0)
265 goto out;
266 else if (type == T_DTRACE_RET &&
267 dtrace_return_probe_ptr != NULL &&
268 dtrace_return_probe_ptr(®s) == 0)
269 goto out;
270 }
271 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
272 goto out;
273 #endif
274
275 if ((frame->tf_rflags & PSL_I) == 0) {
276 /*
277 * Buggy application or kernel code has disabled
278 * interrupts and then trapped. Enabling interrupts
279 * now is wrong, but it is better than running with
280 * interrupts disabled until they are accidentally
281 * enabled later.
282 */
283 if (ISPL(frame->tf_cs) == SEL_UPL)
284 uprintf(
285 "pid %ld (%s): trap %d with interrupts disabled\n",
286 (long)curproc->p_pid, curthread->td_name, type);
287 else if (type != T_NMI && type != T_BPTFLT &&
288 type != T_TRCTRAP) {
289 /*
290 * XXX not quite right, since this may be for a
291 * multiple fault in user mode.
292 */
293 printf("kernel trap %d with interrupts disabled\n",
294 type);
295
296 /*
297 * We shouldn't enable interrupts while holding a
298 * spin lock.
299 */
300 if (td->td_md.md_spinlock_count == 0)
301 enable_intr();
302 }
303 }
304
305 code = frame->tf_err;
306
307 if (ISPL(frame->tf_cs) == SEL_UPL) {
308 /* user trap */
309
310 td->td_pticks = 0;
311 td->td_frame = frame;
312 addr = frame->tf_rip;
313 if (td->td_ucred != p->p_ucred)
314 cred_update_thread(td);
315
316 switch (type) {
317 case T_PRIVINFLT: /* privileged instruction fault */
318 i = SIGILL;
319 ucode = ILL_PRVOPC;
320 break;
321
322 case T_BPTFLT: /* bpt instruction fault */
323 case T_TRCTRAP: /* trace trap */
324 enable_intr();
325 frame->tf_rflags &= ~PSL_T;
326 i = SIGTRAP;
327 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
328 break;
329
330 case T_ARITHTRAP: /* arithmetic trap */
331 ucode = fputrap();
332 if (ucode == -1)
333 goto userout;
334 i = SIGFPE;
335 break;
336
337 case T_PROTFLT: /* general protection fault */
338 i = SIGBUS;
339 ucode = BUS_OBJERR;
340 break;
341 case T_STKFLT: /* stack fault */
342 case T_SEGNPFLT: /* segment not present fault */
343 i = SIGBUS;
344 ucode = BUS_ADRERR;
345 break;
346 case T_TSSFLT: /* invalid TSS fault */
347 i = SIGBUS;
348 ucode = BUS_OBJERR;
349 break;
350 case T_DOUBLEFLT: /* double fault */
351 default:
352 i = SIGBUS;
353 ucode = BUS_OBJERR;
354 break;
355
356 case T_PAGEFLT: /* page fault */
357 addr = frame->tf_addr;
358 i = trap_pfault(frame, TRUE);
359 if (i == -1)
360 goto userout;
361 if (i == 0)
362 goto user;
363
364 if (i == SIGSEGV)
365 ucode = SEGV_MAPERR;
366 else {
367 if (prot_fault_translation == 0) {
368 /*
369 * Autodetect.
370 * This check also covers the images
371 * without the ABI-tag ELF note.
372 */
373 if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
374 && p->p_osrel >= P_OSREL_SIGSEGV) {
375 i = SIGSEGV;
376 ucode = SEGV_ACCERR;
377 } else {
378 i = SIGBUS;
379 ucode = BUS_PAGE_FAULT;
380 }
381 } else if (prot_fault_translation == 1) {
382 /*
383 * Always compat mode.
384 */
385 i = SIGBUS;
386 ucode = BUS_PAGE_FAULT;
387 } else {
388 /*
389 * Always SIGSEGV mode.
390 */
391 i = SIGSEGV;
392 ucode = SEGV_ACCERR;
393 }
394 }
395 break;
396
397 case T_DIVIDE: /* integer divide fault */
398 ucode = FPE_INTDIV;
399 i = SIGFPE;
400 break;
401
402 #ifdef DEV_ISA
403 case T_NMI:
404 /* machine/parity/power fail/"kitchen sink" faults */
405 if (isa_nmi(code) == 0) {
406 #ifdef KDB
407 /*
408 * NMI can be hooked up to a pushbutton
409 * for debugging.
410 */
411 if (kdb_on_nmi) {
412 printf ("NMI ... going to debugger\n");
413 kdb_trap(type, 0, frame);
414 }
415 #endif /* KDB */
416 goto userout;
417 } else if (panic_on_nmi)
418 panic("NMI indicates hardware failure");
419 break;
420 #endif /* DEV_ISA */
421
422 case T_OFLOW: /* integer overflow fault */
423 ucode = FPE_INTOVF;
424 i = SIGFPE;
425 break;
426
427 case T_BOUND: /* bounds check fault */
428 ucode = FPE_FLTSUB;
429 i = SIGFPE;
430 break;
431
432 case T_DNA:
433 /* transparent fault (due to context switch "late") */
434 KASSERT(PCB_USER_FPU(td->td_pcb),
435 ("kernel FPU ctx has leaked"));
436 fpudna();
437 goto userout;
438
439 case T_FPOPFLT: /* FPU operand fetch fault */
440 ucode = ILL_COPROC;
441 i = SIGILL;
442 break;
443
444 case T_XMMFLT: /* SIMD floating-point exception */
445 ucode = 0; /* XXX */
446 i = SIGFPE;
447 break;
448 }
449 } else {
450 /* kernel trap */
451
452 KASSERT(cold || td->td_ucred != NULL,
453 ("kernel trap doesn't have ucred"));
454 switch (type) {
455 case T_PAGEFLT: /* page fault */
456 (void) trap_pfault(frame, FALSE);
457 goto out;
458
459 case T_DNA:
460 KASSERT(!PCB_USER_FPU(td->td_pcb),
461 ("Unregistered use of FPU in kernel"));
462 fpudna();
463 goto out;
464
465 case T_ARITHTRAP: /* arithmetic trap */
466 case T_XMMFLT: /* SIMD floating-point exception */
467 case T_FPOPFLT: /* FPU operand fetch fault */
468 /*
469 * XXXKIB for now disable any FPU traps in kernel
470 * handler registration seems to be overkill
471 */
472 trap_fatal(frame, 0);
473 goto out;
474
475 case T_STKFLT: /* stack fault */
476 break;
477
478 case T_PROTFLT: /* general protection fault */
479 case T_SEGNPFLT: /* segment not present fault */
480 if (td->td_intr_nesting_level != 0)
481 break;
482
483 /*
484 * Invalid segment selectors and out of bounds
485 * %rip's and %rsp's can be set up in user mode.
486 * This causes a fault in kernel mode when the
487 * kernel tries to return to user mode. We want
488 * to get this fault so that we can fix the
489 * problem here and not have to check all the
490 * selectors and pointers when the user changes
491 * them.
492 */
493 if (frame->tf_rip == (long)doreti_iret) {
494 frame->tf_rip = (long)doreti_iret_fault;
495 goto out;
496 }
497 if (frame->tf_rip == (long)ld_ds) {
498 frame->tf_rip = (long)ds_load_fault;
499 goto out;
500 }
501 if (frame->tf_rip == (long)ld_es) {
502 frame->tf_rip = (long)es_load_fault;
503 goto out;
504 }
505 if (frame->tf_rip == (long)ld_fs) {
506 frame->tf_rip = (long)fs_load_fault;
507 goto out;
508 }
509 if (frame->tf_rip == (long)ld_gs) {
510 frame->tf_rip = (long)gs_load_fault;
511 goto out;
512 }
513 if (frame->tf_rip == (long)ld_gsbase) {
514 frame->tf_rip = (long)gsbase_load_fault;
515 goto out;
516 }
517 if (frame->tf_rip == (long)ld_fsbase) {
518 frame->tf_rip = (long)fsbase_load_fault;
519 goto out;
520 }
521 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
522 frame->tf_rip =
523 (long)PCPU_GET(curpcb)->pcb_onfault;
524 goto out;
525 }
526 break;
527
528 case T_TSSFLT:
529 /*
530 * PSL_NT can be set in user mode and isn't cleared
531 * automatically when the kernel is entered. This
532 * causes a TSS fault when the kernel attempts to
533 * `iret' because the TSS link is uninitialized. We
534 * want to get this fault so that we can fix the
535 * problem here and not every time the kernel is
536 * entered.
537 */
538 if (frame->tf_rflags & PSL_NT) {
539 frame->tf_rflags &= ~PSL_NT;
540 goto out;
541 }
542 break;
543
544 case T_TRCTRAP: /* trace trap */
545 /*
546 * Ignore debug register trace traps due to
547 * accesses in the user's address space, which
548 * can happen under several conditions such as
549 * if a user sets a watchpoint on a buffer and
550 * then passes that buffer to a system call.
551 * We still want to get TRCTRAPS for addresses
552 * in kernel space because that is useful when
553 * debugging the kernel.
554 */
555 if (user_dbreg_trap()) {
556 /*
557 * Reset breakpoint bits because the
558 * processor doesn't
559 */
560 /* XXX check upper bits here */
561 load_dr6(rdr6() & 0xfffffff0);
562 goto out;
563 }
564 /*
565 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
566 */
567 case T_BPTFLT:
568 /*
569 * If KDB is enabled, let it handle the debugger trap.
570 * Otherwise, debugger traps "can't happen".
571 */
572 #ifdef KDB
573 if (kdb_trap(type, 0, frame))
574 goto out;
575 #endif
576 break;
577
578 #ifdef DEV_ISA
579 case T_NMI:
580 /* machine/parity/power fail/"kitchen sink" faults */
581 if (isa_nmi(code) == 0) {
582 #ifdef KDB
583 /*
584 * NMI can be hooked up to a pushbutton
585 * for debugging.
586 */
587 if (kdb_on_nmi) {
588 printf ("NMI ... going to debugger\n");
589 kdb_trap(type, 0, frame);
590 }
591 #endif /* KDB */
592 goto out;
593 } else if (panic_on_nmi == 0)
594 goto out;
595 /* FALLTHROUGH */
596 #endif /* DEV_ISA */
597 }
598
599 trap_fatal(frame, 0);
600 goto out;
601 }
602
603 /* Translate fault for emulators (e.g. Linux) */
604 if (*p->p_sysent->sv_transtrap)
605 i = (*p->p_sysent->sv_transtrap)(i, type);
606
607 ksiginfo_init_trap(&ksi);
608 ksi.ksi_signo = i;
609 ksi.ksi_code = ucode;
610 ksi.ksi_trapno = type;
611 ksi.ksi_addr = (void *)addr;
612 trapsignal(td, &ksi);
613
614 user:
615 userret(td, frame);
616 mtx_assert(&Giant, MA_NOTOWNED);
617 KASSERT(PCB_USER_FPU(td->td_pcb),
618 ("Return from trap with kernel FPU ctx leaked"));
619 userout:
620 out:
621 return;
622 }
623
624 static int
625 trap_pfault(frame, usermode)
626 struct trapframe *frame;
627 int usermode;
628 {
629 vm_offset_t va;
630 struct vmspace *vm = NULL;
631 vm_map_t map;
632 int rv = 0;
633 vm_prot_t ftype;
634 struct thread *td = curthread;
635 struct proc *p = td->td_proc;
636 vm_offset_t eva = frame->tf_addr;
637
638 if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
639 /*
640 * Due to both processor errata and lazy TLB invalidation when
641 * access restrictions are removed from virtual pages, memory
642 * accesses that are allowed by the physical mapping layer may
643 * nonetheless cause one spurious page fault per virtual page.
644 * When the thread is executing a "no faulting" section that
645 * is bracketed by vm_fault_{disable,enable}_pagefaults(),
646 * every page fault is treated as a spurious page fault,
647 * unless it accesses the same virtual address as the most
648 * recent page fault within the same "no faulting" section.
649 */
650 if (td->td_md.md_spurflt_addr != eva ||
651 (td->td_pflags & TDP_RESETSPUR) != 0) {
652 /*
653 * Do nothing to the TLB. A stale TLB entry is
654 * flushed automatically by a page fault.
655 */
656 td->td_md.md_spurflt_addr = eva;
657 td->td_pflags &= ~TDP_RESETSPUR;
658 return (0);
659 }
660 } else {
661 /*
662 * If we get a page fault while in a critical section, then
663 * it is most likely a fatal kernel page fault. The kernel
664 * is already going to panic trying to get a sleep lock to
665 * do the VM lookup, so just consider it a fatal trap so the
666 * kernel can print out a useful trap message and even get
667 * to the debugger.
668 *
669 * If we get a page fault while holding a non-sleepable
670 * lock, then it is most likely a fatal kernel page fault.
671 * If WITNESS is enabled, then it's going to whine about
672 * bogus LORs with various VM locks, so just skip to the
673 * fatal trap handling directly.
674 */
675 if (td->td_critnest != 0 ||
676 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
677 "Kernel page fault") != 0) {
678 trap_fatal(frame, eva);
679 return (-1);
680 }
681 }
682 va = trunc_page(eva);
683 if (va >= VM_MIN_KERNEL_ADDRESS) {
684 /*
685 * Don't allow user-mode faults in kernel address space.
686 */
687 if (usermode)
688 goto nogo;
689
690 map = kernel_map;
691 } else {
692 /*
693 * This is a fault on non-kernel virtual memory.
694 * vm is initialized above to NULL. If curproc is NULL
695 * or curproc->p_vmspace is NULL the fault is fatal.
696 */
697 if (p != NULL)
698 vm = p->p_vmspace;
699
700 if (vm == NULL)
701 goto nogo;
702
703 map = &vm->vm_map;
704
705 /*
706 * When accessing a usermode address, kernel must be
707 * ready to accept the page fault, and provide a
708 * handling routine. Since accessing the address
709 * without the handler is a bug, do not try to handle
710 * it normally, and panic immediately.
711 */
712 if (!usermode && (td->td_intr_nesting_level != 0 ||
713 PCPU_GET(curpcb)->pcb_onfault == NULL)) {
714 trap_fatal(frame, eva);
715 return (-1);
716 }
717 }
718
719 /*
720 * PGEX_I is defined only if the execute disable bit capability is
721 * supported and enabled.
722 */
723 if (frame->tf_err & PGEX_W)
724 ftype = VM_PROT_WRITE;
725 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
726 ftype = VM_PROT_EXECUTE;
727 else
728 ftype = VM_PROT_READ;
729
730 if (map != kernel_map) {
731 /*
732 * Keep swapout from messing with us during this
733 * critical time.
734 */
735 PROC_LOCK(p);
736 ++p->p_lock;
737 PROC_UNLOCK(p);
738
739 /* Fault in the user page: */
740 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
741
742 PROC_LOCK(p);
743 --p->p_lock;
744 PROC_UNLOCK(p);
745 } else {
746 /*
747 * Don't have to worry about process locking or stacks in the
748 * kernel.
749 */
750 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
751 }
752 if (rv == KERN_SUCCESS) {
753 #ifdef HWPMC_HOOKS
754 if (ftype == VM_PROT_READ || ftype == VM_PROT_WRITE) {
755 PMC_SOFT_CALL_TF( , , page_fault, all, frame);
756 if (ftype == VM_PROT_READ)
757 PMC_SOFT_CALL_TF( , , page_fault, read,
758 frame);
759 else
760 PMC_SOFT_CALL_TF( , , page_fault, write,
761 frame);
762 }
763 #endif
764 return (0);
765 }
766 nogo:
767 if (!usermode) {
768 if (td->td_intr_nesting_level == 0 &&
769 PCPU_GET(curpcb)->pcb_onfault != NULL) {
770 frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
771 return (0);
772 }
773 trap_fatal(frame, eva);
774 return (-1);
775 }
776
777 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
778 }
779
780 static void
781 trap_fatal(frame, eva)
782 struct trapframe *frame;
783 vm_offset_t eva;
784 {
785 int code, ss;
786 u_int type;
787 long esp;
788 struct soft_segment_descriptor softseg;
789 char *msg;
790
791 code = frame->tf_err;
792 type = frame->tf_trapno;
793 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
794 &softseg);
795
796 if (type <= MAX_TRAP_MSG)
797 msg = trap_msg[type];
798 else
799 msg = "UNKNOWN";
800 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
801 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
802 #ifdef SMP
803 /* two separate prints in case of a trap on an unmapped page */
804 printf("cpuid = %d; ", PCPU_GET(cpuid));
805 printf("apic id = %02x\n", PCPU_GET(apic_id));
806 #endif
807 if (type == T_PAGEFLT) {
808 printf("fault virtual address = 0x%lx\n", eva);
809 printf("fault code = %s %s %s, %s\n",
810 code & PGEX_U ? "user" : "supervisor",
811 code & PGEX_W ? "write" : "read",
812 code & PGEX_I ? "instruction" : "data",
813 code & PGEX_P ? "protection violation" : "page not present");
814 }
815 printf("instruction pointer = 0x%lx:0x%lx\n",
816 frame->tf_cs & 0xffff, frame->tf_rip);
817 if (ISPL(frame->tf_cs) == SEL_UPL) {
818 ss = frame->tf_ss & 0xffff;
819 esp = frame->tf_rsp;
820 } else {
821 ss = GSEL(GDATA_SEL, SEL_KPL);
822 esp = (long)&frame->tf_rsp;
823 }
824 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
825 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
826 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
827 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
828 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
829 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
830 softseg.ssd_gran);
831 printf("processor eflags = ");
832 if (frame->tf_rflags & PSL_T)
833 printf("trace trap, ");
834 if (frame->tf_rflags & PSL_I)
835 printf("interrupt enabled, ");
836 if (frame->tf_rflags & PSL_NT)
837 printf("nested task, ");
838 if (frame->tf_rflags & PSL_RF)
839 printf("resume, ");
840 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
841 printf("current process = ");
842 if (curproc) {
843 printf("%lu (%s)\n",
844 (u_long)curproc->p_pid, curthread->td_name ?
845 curthread->td_name : "");
846 } else {
847 printf("Idle\n");
848 }
849
850 #ifdef KDB
851 if (debugger_on_panic || kdb_active)
852 if (kdb_trap(type, 0, frame))
853 return;
854 #endif
855 printf("trap number = %d\n", type);
856 if (type <= MAX_TRAP_MSG)
857 panic("%s", trap_msg[type]);
858 else
859 panic("unknown/reserved trap");
860 }
861
862 /*
863 * Double fault handler. Called when a fault occurs while writing
864 * a frame for a trap/exception onto the stack. This usually occurs
865 * when the stack overflows (such is the case with infinite recursion,
866 * for example).
867 */
868 void
869 dblfault_handler(struct trapframe *frame)
870 {
871 #ifdef KDTRACE_HOOKS
872 if (dtrace_doubletrap_func != NULL)
873 (*dtrace_doubletrap_func)();
874 #endif
875 printf("\nFatal double fault\n");
876 printf("rip = 0x%lx\n", frame->tf_rip);
877 printf("rsp = 0x%lx\n", frame->tf_rsp);
878 printf("rbp = 0x%lx\n", frame->tf_rbp);
879 #ifdef SMP
880 /* two separate prints in case of a trap on an unmapped page */
881 printf("cpuid = %d; ", PCPU_GET(cpuid));
882 printf("apic id = %02x\n", PCPU_GET(apic_id));
883 #endif
884 panic("double fault");
885 }
886
887 int
888 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
889 {
890 struct proc *p;
891 struct trapframe *frame;
892 register_t *argp;
893 caddr_t params;
894 int reg, regcnt, error;
895
896 p = td->td_proc;
897 frame = td->td_frame;
898 reg = 0;
899 regcnt = 6;
900
901 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
902 sa->code = frame->tf_rax;
903
904 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
905 sa->code = frame->tf_rdi;
906 reg++;
907 regcnt--;
908 }
909 if (p->p_sysent->sv_mask)
910 sa->code &= p->p_sysent->sv_mask;
911
912 if (sa->code >= p->p_sysent->sv_size)
913 sa->callp = &p->p_sysent->sv_table[0];
914 else
915 sa->callp = &p->p_sysent->sv_table[sa->code];
916
917 sa->narg = sa->callp->sy_narg;
918 KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
919 ("Too many syscall arguments!"));
920 error = 0;
921 argp = &frame->tf_rdi;
922 argp += reg;
923 bcopy(argp, sa->args, sizeof(sa->args[0]) * regcnt);
924 if (sa->narg > regcnt) {
925 KASSERT(params != NULL, ("copyin args with no params!"));
926 error = copyin(params, &sa->args[regcnt],
927 (sa->narg - regcnt) * sizeof(sa->args[0]));
928 }
929
930 if (error == 0) {
931 td->td_retval[0] = 0;
932 td->td_retval[1] = frame->tf_rdx;
933 }
934
935 return (error);
936 }
937
938 #include "../../kern/subr_syscall.c"
939
940 /*
941 * System call handler for native binaries. The trap frame is already
942 * set up by the assembler trampoline and a pointer to it is saved in
943 * td_frame.
944 */
945 void
946 amd64_syscall(struct thread *td, int traced)
947 {
948 struct syscall_args sa;
949 int error;
950 ksiginfo_t ksi;
951
952 #ifdef DIAGNOSTIC
953 if (ISPL(td->td_frame->tf_cs) != SEL_UPL) {
954 panic("syscall");
955 /* NOT REACHED */
956 }
957 #endif
958 error = syscallenter(td, &sa);
959
960 /*
961 * Traced syscall.
962 */
963 if (__predict_false(traced)) {
964 td->td_frame->tf_rflags &= ~PSL_T;
965 ksiginfo_init_trap(&ksi);
966 ksi.ksi_signo = SIGTRAP;
967 ksi.ksi_code = TRAP_TRACE;
968 ksi.ksi_addr = (void *)td->td_frame->tf_rip;
969 trapsignal(td, &ksi);
970 }
971
972 KASSERT(PCB_USER_FPU(td->td_pcb),
973 ("System call %s returing with kernel FPU ctx leaked",
974 syscallname(td->td_proc, sa.code)));
975 KASSERT(td->td_pcb->pcb_save == get_pcb_user_save_td(td),
976 ("System call %s returning with mangled pcb_save",
977 syscallname(td->td_proc, sa.code)));
978
979 syscallret(td, error, &sa);
980
981 /*
982 * If the user-supplied value of %rip is not a canonical
983 * address, then some CPUs will trigger a ring 0 #GP during
984 * the sysret instruction. However, the fault handler would
985 * execute in ring 0 with the user's %gs and %rsp which would
986 * not be safe. Instead, use the full return path which
987 * catches the problem safely.
988 */
989 if (td->td_frame->tf_rip >= VM_MAXUSER_ADDRESS)
990 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
991 }
Cache object: b8cccc05a5412b6d165344a276cd4c80
|