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/7.3/sys/amd64/amd64/trap.c 198589 2009-10-29 14:34:02Z jhb $");
42
43 /*
44 * AMD64 Trap and System call handling
45 */
46
47 #include "opt_clock.h"
48 #include "opt_compat.h"
49 #include "opt_cpu.h"
50 #include "opt_hwpmc_hooks.h"
51 #include "opt_isa.h"
52 #include "opt_kdb.h"
53 #include "opt_kdtrace.h"
54 #include "opt_ktrace.h"
55
56 #include <sys/param.h>
57 #include <sys/bus.h>
58 #include <sys/systm.h>
59 #include <sys/proc.h>
60 #include <sys/pioctl.h>
61 #include <sys/ptrace.h>
62 #include <sys/kdb.h>
63 #include <sys/kernel.h>
64 #include <sys/ktr.h>
65 #include <sys/lock.h>
66 #include <sys/mutex.h>
67 #include <sys/resourcevar.h>
68 #include <sys/signalvar.h>
69 #include <sys/syscall.h>
70 #include <sys/sysctl.h>
71 #include <sys/sysent.h>
72 #include <sys/uio.h>
73 #include <sys/vmmeter.h>
74 #ifdef KTRACE
75 #include <sys/ktrace.h>
76 #endif
77 #ifdef HWPMC_HOOKS
78 #include <sys/pmckern.h>
79 #endif
80 #include <security/audit/audit.h>
81
82 #include <vm/vm.h>
83 #include <vm/vm_param.h>
84 #include <vm/pmap.h>
85 #include <vm/vm_kern.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_extern.h>
89
90 #include <machine/cpu.h>
91 #include <machine/intr_machdep.h>
92 #include <machine/mca.h>
93 #include <machine/md_var.h>
94 #include <machine/pcb.h>
95 #ifdef SMP
96 #include <machine/smp.h>
97 #endif
98 #include <machine/tss.h>
99
100 #ifdef KDTRACE_HOOKS
101 #include <sys/dtrace_bsd.h>
102
103 /*
104 * This is a hook which is initialised by the dtrace module
105 * to handle traps which might occur during DTrace probe
106 * execution.
107 */
108 dtrace_trap_func_t dtrace_trap_func;
109
110 dtrace_doubletrap_func_t dtrace_doubletrap_func;
111
112 /*
113 * This is a hook which is initialised by the systrace module
114 * when it is loaded. This keeps the DTrace syscall provider
115 * implementation opaque.
116 */
117 systrace_probe_func_t systrace_probe_func;
118 #endif
119
120 /* Defined in amd64/amd64/elf_machdep.c. */
121 extern struct sysentvec elf64_freebsd_sysvec;
122 #ifdef COMPAT_IA32
123 /* Defined in compat/ia32/ia32_sysvec.c. */
124 extern struct sysentvec ia32_freebsd_sysvec;
125 #endif
126
127 extern void trap(struct trapframe *frame);
128 extern void syscall(struct trapframe *frame);
129 void dblfault_handler(struct trapframe *frame);
130
131 static int trap_pfault(struct trapframe *, int);
132 static void trap_fatal(struct trapframe *, vm_offset_t);
133
134 #define MAX_TRAP_MSG 30
135 static char *trap_msg[] = {
136 "", /* 0 unused */
137 "privileged instruction fault", /* 1 T_PRIVINFLT */
138 "", /* 2 unused */
139 "breakpoint instruction fault", /* 3 T_BPTFLT */
140 "", /* 4 unused */
141 "", /* 5 unused */
142 "arithmetic trap", /* 6 T_ARITHTRAP */
143 "", /* 7 unused */
144 "", /* 8 unused */
145 "general protection fault", /* 9 T_PROTFLT */
146 "trace trap", /* 10 T_TRCTRAP */
147 "", /* 11 unused */
148 "page fault", /* 12 T_PAGEFLT */
149 "", /* 13 unused */
150 "alignment fault", /* 14 T_ALIGNFLT */
151 "", /* 15 unused */
152 "", /* 16 unused */
153 "", /* 17 unused */
154 "integer divide fault", /* 18 T_DIVIDE */
155 "non-maskable interrupt trap", /* 19 T_NMI */
156 "overflow trap", /* 20 T_OFLOW */
157 "FPU bounds check fault", /* 21 T_BOUND */
158 "FPU device not available", /* 22 T_DNA */
159 "double fault", /* 23 T_DOUBLEFLT */
160 "FPU operand fetch fault", /* 24 T_FPOPFLT */
161 "invalid TSS fault", /* 25 T_TSSFLT */
162 "segment not present fault", /* 26 T_SEGNPFLT */
163 "stack fault", /* 27 T_STKFLT */
164 "machine check trap", /* 28 T_MCHK */
165 "SIMD floating-point exception", /* 29 T_XMMFLT */
166 "reserved (unknown) fault", /* 30 T_RESERVED */
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 #endif
174 static int panic_on_nmi = 1;
175 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
176 &panic_on_nmi, 0, "Panic on NMI");
177 static int prot_fault_translation = 0;
178 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
179 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
180
181 extern char *syscallnames[];
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 #ifdef STOP_NMI
205 /* Handler for NMI IPIs used for stopping CPUs. */
206 if (type == T_NMI) {
207 if (ipi_nmi_handler() == 0)
208 goto out;
209 }
210 #endif /* STOP_NMI */
211 #endif /* SMP */
212
213 #ifdef KDB
214 if (kdb_active) {
215 kdb_reenter();
216 goto out;
217 }
218 #endif
219
220 #ifdef HWPMC_HOOKS
221 /*
222 * CPU PMCs interrupt using an NMI. If the PMC module is
223 * active, pass the 'rip' value to the PMC module's interrupt
224 * handler. A return value of '1' from the handler means that
225 * the NMI was handled by it and we can return immediately.
226 */
227 if (type == T_NMI && pmc_intr &&
228 (*pmc_intr)(PCPU_GET(cpuid), frame))
229 goto out;
230 #endif
231
232 if (type == T_MCHK) {
233 if (!mca_intr())
234 trap_fatal(frame, 0);
235 goto out;
236 }
237
238 #ifdef KDTRACE_HOOKS
239 /*
240 * A trap can occur while DTrace executes a probe. Before
241 * executing the probe, DTrace blocks re-scheduling and sets
242 * a flag in it's per-cpu flags to indicate that it doesn't
243 * want to fault. On returning from the the probe, the no-fault
244 * flag is cleared and finally re-scheduling is enabled.
245 *
246 * If the DTrace kernel module has registered a trap handler,
247 * call it and if it returns non-zero, assume that it has
248 * handled the trap and modified the trap frame so that this
249 * function can return normally.
250 */
251 if (dtrace_trap_func != NULL)
252 if ((*dtrace_trap_func)(frame, type))
253 goto out;
254 #endif
255
256 if ((frame->tf_rflags & PSL_I) == 0) {
257 /*
258 * Buggy application or kernel code has disabled
259 * interrupts and then trapped. Enabling interrupts
260 * now is wrong, but it is better than running with
261 * interrupts disabled until they are accidentally
262 * enabled later.
263 */
264 if (ISPL(frame->tf_cs) == SEL_UPL)
265 printf(
266 "pid %ld (%s): trap %d with interrupts disabled\n",
267 (long)curproc->p_pid, curproc->p_comm, type);
268 else if (type != T_NMI && type != T_BPTFLT &&
269 type != T_TRCTRAP) {
270 /*
271 * XXX not quite right, since this may be for a
272 * multiple fault in user mode.
273 */
274 printf("kernel trap %d with interrupts disabled\n",
275 type);
276 /*
277 * We shouldn't enable interrupts while holding a
278 * spin lock or servicing an NMI.
279 */
280 if (type != T_NMI && td->td_md.md_spinlock_count == 0)
281 enable_intr();
282 }
283 }
284
285 code = frame->tf_err;
286 if (type == T_PAGEFLT) {
287 /*
288 * If we get a page fault while in a critical section, then
289 * it is most likely a fatal kernel page fault. The kernel
290 * is already going to panic trying to get a sleep lock to
291 * do the VM lookup, so just consider it a fatal trap so the
292 * kernel can print out a useful trap message and even get
293 * to the debugger.
294 *
295 * If we get a page fault while holding a non-sleepable
296 * lock, then it is most likely a fatal kernel page fault.
297 * If WITNESS is enabled, then it's going to whine about
298 * bogus LORs with various VM locks, so just skip to the
299 * fatal trap handling directly.
300 */
301 if (td->td_critnest != 0 ||
302 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
303 "Kernel page fault") != 0)
304 trap_fatal(frame, frame->tf_addr);
305 }
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 #ifdef KSE
359 if (td->td_pflags & TDP_SA)
360 thread_user_enter(td);
361 #endif
362 i = trap_pfault(frame, TRUE);
363 if (i == -1)
364 goto userout;
365 if (i == 0)
366 goto user;
367
368 if (i == SIGSEGV)
369 ucode = SEGV_MAPERR;
370 else {
371 if (prot_fault_translation == 0) {
372 /*
373 * Autodetect.
374 * This check also covers the images
375 * without the ABI-tag ELF note.
376 */
377 if ((curproc->p_sysent ==
378 &elf64_freebsd_sysvec
379 #ifdef COMPAT_IA32
380 || curproc->p_sysent ==
381 &ia32_freebsd_sysvec
382 #endif
383 ) && p->p_osrel >= 700004) {
384 i = SIGSEGV;
385 ucode = SEGV_ACCERR;
386 } else {
387 i = SIGBUS;
388 ucode = BUS_PAGE_FAULT;
389 }
390 } else if (prot_fault_translation == 1) {
391 /*
392 * Always compat mode.
393 */
394 i = SIGBUS;
395 ucode = BUS_PAGE_FAULT;
396 } else {
397 /*
398 * Always SIGSEGV mode.
399 */
400 i = SIGSEGV;
401 ucode = SEGV_ACCERR;
402 }
403 }
404 break;
405
406 case T_DIVIDE: /* integer divide fault */
407 ucode = FPE_INTDIV;
408 i = SIGFPE;
409 break;
410
411 #ifdef DEV_ISA
412 case T_NMI:
413 /* machine/parity/power fail/"kitchen sink" faults */
414 /* XXX Giant */
415 if (isa_nmi(code) == 0) {
416 #ifdef KDB
417 /*
418 * NMI can be hooked up to a pushbutton
419 * for debugging.
420 */
421 if (kdb_on_nmi) {
422 printf ("NMI ... going to debugger\n");
423 kdb_trap(type, 0, frame);
424 }
425 #endif /* KDB */
426 goto userout;
427 } else if (panic_on_nmi)
428 panic("NMI indicates hardware failure");
429 break;
430 #endif /* DEV_ISA */
431
432 case T_OFLOW: /* integer overflow fault */
433 ucode = FPE_INTOVF;
434 i = SIGFPE;
435 break;
436
437 case T_BOUND: /* bounds check fault */
438 ucode = FPE_FLTSUB;
439 i = SIGFPE;
440 break;
441
442 case T_DNA:
443 /* transparent fault (due to context switch "late") */
444 fpudna();
445 goto userout;
446
447 case T_FPOPFLT: /* FPU operand fetch fault */
448 ucode = ILL_COPROC;
449 i = SIGILL;
450 break;
451
452 case T_XMMFLT: /* SIMD floating-point exception */
453 ucode = 0; /* XXX */
454 i = SIGFPE;
455 break;
456 }
457 } else {
458 /* kernel trap */
459
460 KASSERT(cold || td->td_ucred != NULL,
461 ("kernel trap doesn't have ucred"));
462 switch (type) {
463 case T_PAGEFLT: /* page fault */
464 (void) trap_pfault(frame, FALSE);
465 goto out;
466
467 case T_DNA:
468 /*
469 * The kernel is apparently using fpu for copying.
470 * XXX this should be fatal unless the kernel has
471 * registered such use.
472 */
473 fpudna();
474 printf("fpudna in kernel mode!\n");
475 goto out;
476
477 case T_STKFLT: /* stack fault */
478 break;
479
480 case T_PROTFLT: /* general protection fault */
481 case T_SEGNPFLT: /* segment not present fault */
482 if (td->td_intr_nesting_level != 0)
483 break;
484
485 /*
486 * Invalid segment selectors and out of bounds
487 * %rip's and %rsp's can be set up in user mode.
488 * This causes a fault in kernel mode when the
489 * kernel tries to return to user mode. We want
490 * to get this fault so that we can fix the
491 * problem here and not have to check all the
492 * selectors and pointers when the user changes
493 * them.
494 */
495 if (frame->tf_rip == (long)doreti_iret) {
496 frame->tf_rip = (long)doreti_iret_fault;
497 goto out;
498 }
499 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
500 frame->tf_rip =
501 (long)PCPU_GET(curpcb)->pcb_onfault;
502 goto out;
503 }
504 break;
505
506 case T_TSSFLT:
507 /*
508 * PSL_NT can be set in user mode and isn't cleared
509 * automatically when the kernel is entered. This
510 * causes a TSS fault when the kernel attempts to
511 * `iret' because the TSS link is uninitialized. We
512 * want to get this fault so that we can fix the
513 * problem here and not every time the kernel is
514 * entered.
515 */
516 if (frame->tf_rflags & PSL_NT) {
517 frame->tf_rflags &= ~PSL_NT;
518 goto out;
519 }
520 break;
521
522 case T_TRCTRAP: /* trace trap */
523 /*
524 * Ignore debug register trace traps due to
525 * accesses in the user's address space, which
526 * can happen under several conditions such as
527 * if a user sets a watchpoint on a buffer and
528 * then passes that buffer to a system call.
529 * We still want to get TRCTRAPS for addresses
530 * in kernel space because that is useful when
531 * debugging the kernel.
532 */
533 if (user_dbreg_trap()) {
534 /*
535 * Reset breakpoint bits because the
536 * processor doesn't
537 */
538 /* XXX check upper bits here */
539 load_dr6(rdr6() & 0xfffffff0);
540 goto out;
541 }
542 /*
543 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
544 */
545 case T_BPTFLT:
546 /*
547 * If KDB is enabled, let it handle the debugger trap.
548 * Otherwise, debugger traps "can't happen".
549 */
550 #ifdef KDB
551 if (kdb_trap(type, 0, frame))
552 goto out;
553 #endif
554 break;
555
556 #ifdef DEV_ISA
557 case T_NMI:
558 /* XXX Giant */
559 /* machine/parity/power fail/"kitchen sink" faults */
560 if (isa_nmi(code) == 0) {
561 #ifdef KDB
562 /*
563 * NMI can be hooked up to a pushbutton
564 * for debugging.
565 */
566 if (kdb_on_nmi) {
567 printf ("NMI ... going to debugger\n");
568 kdb_trap(type, 0, frame);
569 }
570 #endif /* KDB */
571 goto out;
572 } else if (panic_on_nmi == 0)
573 goto out;
574 /* FALLTHROUGH */
575 #endif /* DEV_ISA */
576 }
577
578 trap_fatal(frame, 0);
579 goto out;
580 }
581
582 /* Translate fault for emulators (e.g. Linux) */
583 if (*p->p_sysent->sv_transtrap)
584 i = (*p->p_sysent->sv_transtrap)(i, type);
585
586 ksiginfo_init_trap(&ksi);
587 ksi.ksi_signo = i;
588 ksi.ksi_code = ucode;
589 ksi.ksi_trapno = type;
590 ksi.ksi_addr = (void *)addr;
591 trapsignal(td, &ksi);
592
593 #ifdef DEBUG
594 if (type <= MAX_TRAP_MSG) {
595 uprintf("fatal process exception: %s",
596 trap_msg[type]);
597 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
598 uprintf(", fault VA = 0x%lx", frame->tf_addr);
599 uprintf("\n");
600 }
601 #endif
602
603 user:
604 userret(td, frame);
605 mtx_assert(&Giant, MA_NOTOWNED);
606 userout:
607 out:
608 return;
609 }
610
611 static int
612 trap_pfault(frame, usermode)
613 struct trapframe *frame;
614 int usermode;
615 {
616 vm_offset_t va;
617 struct vmspace *vm = NULL;
618 vm_map_t map;
619 int rv = 0;
620 vm_prot_t ftype;
621 struct thread *td = curthread;
622 struct proc *p = td->td_proc;
623 vm_offset_t eva = frame->tf_addr;
624
625 va = trunc_page(eva);
626 if (va >= VM_MIN_KERNEL_ADDRESS) {
627 /*
628 * Don't allow user-mode faults in kernel address space.
629 */
630 if (usermode)
631 goto nogo;
632
633 map = kernel_map;
634 } else {
635 /*
636 * This is a fault on non-kernel virtual memory.
637 * vm is initialized above to NULL. If curproc is NULL
638 * or curproc->p_vmspace is NULL the fault is fatal.
639 */
640 if (p != NULL)
641 vm = p->p_vmspace;
642
643 if (vm == NULL)
644 goto nogo;
645
646 map = &vm->vm_map;
647 }
648
649 /*
650 * PGEX_I is defined only if the execute disable bit capability is
651 * supported and enabled.
652 */
653 if (frame->tf_err & PGEX_W)
654 ftype = VM_PROT_WRITE;
655 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
656 ftype = VM_PROT_EXECUTE;
657 else
658 ftype = VM_PROT_READ;
659
660 if (map != kernel_map) {
661 /*
662 * Keep swapout from messing with us during this
663 * critical time.
664 */
665 PROC_LOCK(p);
666 ++p->p_lock;
667 PROC_UNLOCK(p);
668
669 /* Fault in the user page: */
670 rv = vm_fault(map, va, ftype,
671 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
672 : VM_FAULT_NORMAL);
673
674 PROC_LOCK(p);
675 --p->p_lock;
676 PROC_UNLOCK(p);
677 } else {
678 /*
679 * Don't have to worry about process locking or stacks in the
680 * kernel.
681 */
682 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
683 }
684 if (rv == KERN_SUCCESS)
685 return (0);
686 nogo:
687 if (!usermode) {
688 if (td->td_intr_nesting_level == 0 &&
689 PCPU_GET(curpcb)->pcb_onfault != NULL) {
690 frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
691 return (0);
692 }
693 trap_fatal(frame, eva);
694 return (-1);
695 }
696
697 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
698 }
699
700 static void
701 trap_fatal(frame, eva)
702 struct trapframe *frame;
703 vm_offset_t eva;
704 {
705 int code, ss;
706 u_int type;
707 long esp;
708 struct soft_segment_descriptor softseg;
709 char *msg;
710
711 code = frame->tf_err;
712 type = frame->tf_trapno;
713 sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
714 &softseg);
715
716 if (type <= MAX_TRAP_MSG)
717 msg = trap_msg[type];
718 else
719 msg = "UNKNOWN";
720 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
721 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
722 #ifdef SMP
723 /* two separate prints in case of a trap on an unmapped page */
724 printf("cpuid = %d; ", PCPU_GET(cpuid));
725 printf("apic id = %02x\n", PCPU_GET(apic_id));
726 #endif
727 if (type == T_PAGEFLT) {
728 printf("fault virtual address = 0x%lx\n", eva);
729 printf("fault code = %s %s %s, %s\n",
730 code & PGEX_U ? "user" : "supervisor",
731 code & PGEX_W ? "write" : "read",
732 code & PGEX_I ? "instruction" : "data",
733 code & PGEX_P ? "protection violation" : "page not present");
734 }
735 printf("instruction pointer = 0x%lx:0x%lx\n",
736 frame->tf_cs & 0xffff, frame->tf_rip);
737 if (ISPL(frame->tf_cs) == SEL_UPL) {
738 ss = frame->tf_ss & 0xffff;
739 esp = frame->tf_rsp;
740 } else {
741 ss = GSEL(GDATA_SEL, SEL_KPL);
742 esp = (long)&frame->tf_rsp;
743 }
744 printf("stack pointer = 0x%x:0x%lx\n", ss, esp);
745 printf("frame pointer = 0x%x:0x%lx\n", ss, frame->tf_rbp);
746 printf("code segment = base 0x%lx, limit 0x%lx, type 0x%x\n",
747 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
748 printf(" = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
749 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
750 softseg.ssd_gran);
751 printf("processor eflags = ");
752 if (frame->tf_rflags & PSL_T)
753 printf("trace trap, ");
754 if (frame->tf_rflags & PSL_I)
755 printf("interrupt enabled, ");
756 if (frame->tf_rflags & PSL_NT)
757 printf("nested task, ");
758 if (frame->tf_rflags & PSL_RF)
759 printf("resume, ");
760 printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
761 printf("current process = ");
762 if (curproc) {
763 printf("%lu (%s)\n",
764 (u_long)curproc->p_pid, curproc->p_comm ?
765 curproc->p_comm : "");
766 } else {
767 printf("Idle\n");
768 }
769
770 #ifdef KDB
771 if (debugger_on_panic || kdb_active)
772 if (kdb_trap(type, 0, frame))
773 return;
774 #endif
775 printf("trap number = %d\n", type);
776 if (type <= MAX_TRAP_MSG)
777 panic("%s", trap_msg[type]);
778 else
779 panic("unknown/reserved trap");
780 }
781
782 /*
783 * Double fault handler. Called when a fault occurs while writing
784 * a frame for a trap/exception onto the stack. This usually occurs
785 * when the stack overflows (such is the case with infinite recursion,
786 * for example).
787 */
788 void
789 dblfault_handler(struct trapframe *frame)
790 {
791 #ifdef KDTRACE_HOOKS
792 if (dtrace_doubletrap_func != NULL)
793 (*dtrace_doubletrap_func)();
794 #endif
795 printf("\nFatal double fault\n");
796 printf("rip = 0x%lx\n", frame->tf_rip);
797 printf("rsp = 0x%lx\n", frame->tf_rsp);
798 printf("rbp = 0x%lx\n", frame->tf_rbp);
799 #ifdef SMP
800 /* two separate prints in case of a trap on an unmapped page */
801 printf("cpuid = %d; ", PCPU_GET(cpuid));
802 printf("apic id = %02x\n", PCPU_GET(apic_id));
803 #endif
804 panic("double fault");
805 }
806
807 /*
808 * syscall - system call request C handler
809 *
810 * A system call is essentially treated as a trap.
811 */
812 void
813 syscall(struct trapframe *frame)
814 {
815 caddr_t params;
816 struct sysent *callp;
817 struct thread *td = curthread;
818 struct proc *p = td->td_proc;
819 register_t orig_tf_rflags;
820 int error;
821 int narg;
822 register_t args[8];
823 register_t *argp;
824 u_int code;
825 int reg, regcnt;
826 ksiginfo_t ksi;
827
828 PCPU_INC(cnt.v_syscall);
829
830 #ifdef DIAGNOSTIC
831 if (ISPL(frame->tf_cs) != SEL_UPL) {
832 panic("syscall");
833 /* NOT REACHED */
834 }
835 #endif
836
837 reg = 0;
838 regcnt = 6;
839 td->td_pticks = 0;
840 td->td_frame = frame;
841 if (td->td_ucred != p->p_ucred)
842 cred_update_thread(td);
843 #ifdef KSE
844 if (p->p_flag & P_SA)
845 thread_user_enter(td);
846 #endif
847 params = (caddr_t)frame->tf_rsp + sizeof(register_t);
848 code = frame->tf_rax;
849 orig_tf_rflags = frame->tf_rflags;
850
851 if (p->p_sysent->sv_prepsyscall) {
852 /*
853 * The prep code is MP aware.
854 */
855 (*p->p_sysent->sv_prepsyscall)(frame, (int *)args, &code, ¶ms);
856 } else {
857 if (code == SYS_syscall || code == SYS___syscall) {
858 code = frame->tf_rdi;
859 reg++;
860 regcnt--;
861 }
862 }
863
864 if (p->p_sysent->sv_mask)
865 code &= p->p_sysent->sv_mask;
866
867 if (code >= p->p_sysent->sv_size)
868 callp = &p->p_sysent->sv_table[0];
869 else
870 callp = &p->p_sysent->sv_table[code];
871
872 narg = callp->sy_narg;
873
874 /*
875 * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
876 */
877 KASSERT(narg <= sizeof(args) / sizeof(args[0]),
878 ("Too many syscall arguments!"));
879 error = 0;
880 argp = &frame->tf_rdi;
881 argp += reg;
882 bcopy(argp, args, sizeof(args[0]) * regcnt);
883 if (narg > regcnt) {
884 KASSERT(params != NULL, ("copyin args with no params!"));
885 error = copyin(params, &args[regcnt],
886 (narg - regcnt) * sizeof(args[0]));
887 }
888 argp = &args[0];
889
890 #ifdef KTRACE
891 if (KTRPOINT(td, KTR_SYSCALL))
892 ktrsyscall(code, narg, argp);
893 #endif
894
895 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
896 td->td_proc->p_pid, td->td_proc->p_comm, code);
897
898 td->td_syscalls++;
899
900 if (error == 0) {
901 td->td_retval[0] = 0;
902 td->td_retval[1] = frame->tf_rdx;
903
904 STOPEVENT(p, S_SCE, narg);
905
906 PTRACESTOP_SC(p, td, S_PT_SCE);
907
908 #ifdef KDTRACE_HOOKS
909 /*
910 * If the systrace module has registered it's probe
911 * callback and if there is a probe active for the
912 * syscall 'entry', process the probe.
913 */
914 if (systrace_probe_func != NULL && callp->sy_entry != 0)
915 (*systrace_probe_func)(callp->sy_entry, code, callp,
916 args);
917 #endif
918
919 AUDIT_SYSCALL_ENTER(code, td);
920 error = (*callp->sy_call)(td, argp);
921 AUDIT_SYSCALL_EXIT(error, td);
922
923 /* Save the latest error return value. */
924 td->td_errno = error;
925
926 #ifdef KDTRACE_HOOKS
927 /*
928 * If the systrace module has registered it's probe
929 * callback and if there is a probe active for the
930 * syscall 'return', process the probe.
931 */
932 if (systrace_probe_func != NULL && callp->sy_return != 0)
933 (*systrace_probe_func)(callp->sy_return, code, callp,
934 args);
935 #endif
936 }
937
938 switch (error) {
939 case 0:
940 frame->tf_rax = td->td_retval[0];
941 frame->tf_rdx = td->td_retval[1];
942 frame->tf_rflags &= ~PSL_C;
943 break;
944
945 case ERESTART:
946 /*
947 * Reconstruct pc, we know that 'syscall' is 2 bytes.
948 * We have to do a full context restore so that %r10
949 * (which was holding the value of %rcx) is restored for
950 * the next iteration.
951 */
952 frame->tf_rip -= frame->tf_err;
953 frame->tf_r10 = frame->tf_rcx;
954 td->td_pcb->pcb_flags |= PCB_FULLCTX;
955 break;
956
957 case EJUSTRETURN:
958 break;
959
960 default:
961 if (p->p_sysent->sv_errsize) {
962 if (error >= p->p_sysent->sv_errsize)
963 error = -1; /* XXX */
964 else
965 error = p->p_sysent->sv_errtbl[error];
966 }
967 frame->tf_rax = error;
968 frame->tf_rflags |= PSL_C;
969 break;
970 }
971
972 /*
973 * Traced syscall.
974 */
975 if (orig_tf_rflags & PSL_T) {
976 frame->tf_rflags &= ~PSL_T;
977 ksiginfo_init_trap(&ksi);
978 ksi.ksi_signo = SIGTRAP;
979 ksi.ksi_code = TRAP_TRACE;
980 ksi.ksi_addr = (void *)frame->tf_rip;
981 trapsignal(td, &ksi);
982 }
983
984 /*
985 * Check for misbehavior.
986 */
987 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
988 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
989 KASSERT(td->td_critnest == 0,
990 ("System call %s returning in a critical section",
991 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
992 KASSERT(td->td_locks == 0,
993 ("System call %s returning with %d locks held",
994 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
995 td->td_locks));
996
997 /*
998 * Handle reschedule and other end-of-syscall issues
999 */
1000 userret(td, frame);
1001
1002 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1003 td->td_proc->p_pid, td->td_proc->p_comm, code);
1004
1005 #ifdef KTRACE
1006 if (KTRPOINT(td, KTR_SYSRET))
1007 ktrsysret(code, error, td->td_retval[0]);
1008 #endif
1009
1010 /*
1011 * This works because errno is findable through the
1012 * register set. If we ever support an emulation where this
1013 * is not the case, this code will need to be revisited.
1014 */
1015 STOPEVENT(p, S_SCX, code);
1016
1017 PTRACESTOP_SC(p, td, S_PT_SCX);
1018 }
Cache object: 3c38d136baa1765c9c60cbe60955ddde
|