]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/trap.c
This commit was generated by cvs2svn to compensate for changes in r174187,
[FreeBSD/FreeBSD.git] / 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$");
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_ktrace.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 KTRACE
73 #include <sys/ktrace.h>
74 #endif
75 #ifdef HWPMC_HOOKS
76 #include <sys/pmckern.h>
77 #endif
78 #include <security/audit/audit.h>
79
80 #include <vm/vm.h>
81 #include <vm/vm_param.h>
82 #include <vm/pmap.h>
83 #include <vm/vm_kern.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_extern.h>
87
88 #include <machine/cpu.h>
89 #include <machine/intr_machdep.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 extern void trap(struct trapframe *frame);
98 extern void syscall(struct trapframe *frame);
99 void dblfault_handler(struct trapframe *frame);
100
101 static int trap_pfault(struct trapframe *, int);
102 static void trap_fatal(struct trapframe *, vm_offset_t);
103
104 #define MAX_TRAP_MSG            30
105 static char *trap_msg[] = {
106         "",                                     /*  0 unused */
107         "privileged instruction fault",         /*  1 T_PRIVINFLT */
108         "",                                     /*  2 unused */
109         "breakpoint instruction fault",         /*  3 T_BPTFLT */
110         "",                                     /*  4 unused */
111         "",                                     /*  5 unused */
112         "arithmetic trap",                      /*  6 T_ARITHTRAP */
113         "",                                     /*  7 unused */
114         "",                                     /*  8 unused */
115         "general protection fault",             /*  9 T_PROTFLT */
116         "trace trap",                           /* 10 T_TRCTRAP */
117         "",                                     /* 11 unused */
118         "page fault",                           /* 12 T_PAGEFLT */
119         "",                                     /* 13 unused */
120         "alignment fault",                      /* 14 T_ALIGNFLT */
121         "",                                     /* 15 unused */
122         "",                                     /* 16 unused */
123         "",                                     /* 17 unused */
124         "integer divide fault",                 /* 18 T_DIVIDE */
125         "non-maskable interrupt trap",          /* 19 T_NMI */
126         "overflow trap",                        /* 20 T_OFLOW */
127         "FPU bounds check fault",               /* 21 T_BOUND */
128         "FPU device not available",             /* 22 T_DNA */
129         "double fault",                         /* 23 T_DOUBLEFLT */
130         "FPU operand fetch fault",              /* 24 T_FPOPFLT */
131         "invalid TSS fault",                    /* 25 T_TSSFLT */
132         "segment not present fault",            /* 26 T_SEGNPFLT */
133         "stack fault",                          /* 27 T_STKFLT */
134         "machine check trap",                   /* 28 T_MCHK */
135         "SIMD floating-point exception",        /* 29 T_XMMFLT */
136         "reserved (unknown) fault",             /* 30 T_RESERVED */
137 };
138
139 #ifdef KDB
140 static int kdb_on_nmi = 1;
141 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
142         &kdb_on_nmi, 0, "Go to KDB on NMI");
143 #endif
144 static int panic_on_nmi = 1;
145 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
146         &panic_on_nmi, 0, "Panic on NMI");
147
148 extern char *syscallnames[];
149
150 /*
151  * Exception, fault, and trap interface to the FreeBSD kernel.
152  * This common code is called from assembly language IDT gate entry
153  * routines that prepare a suitable stack frame, and restore this
154  * frame after the exception has been processed.
155  */
156
157 void
158 trap(struct trapframe *frame)
159 {
160         struct thread *td = curthread;
161         struct proc *p = td->td_proc;
162         int i = 0, ucode = 0, code;
163         u_int type;
164         register_t addr = 0;
165         ksiginfo_t ksi;
166
167         PCPU_INC(cnt.v_trap);
168         type = frame->tf_trapno;
169
170 #ifdef SMP
171 #ifdef STOP_NMI
172         /* Handler for NMI IPIs used for stopping CPUs. */
173         if (type == T_NMI) {
174                  if (ipi_nmi_handler() == 0)
175                            goto out;
176         }
177 #endif /* STOP_NMI */
178 #endif /* SMP */
179
180 #ifdef KDB
181         if (kdb_active) {
182                 kdb_reenter();
183                 goto out;
184         }
185 #endif
186
187 #ifdef  HWPMC_HOOKS
188         /*
189          * CPU PMCs interrupt using an NMI.  If the PMC module is
190          * active, pass the 'rip' value to the PMC module's interrupt
191          * handler.  A return value of '1' from the handler means that
192          * the NMI was handled by it and we can return immediately.
193          */
194         if (type == T_NMI && pmc_intr &&
195             (*pmc_intr)(PCPU_GET(cpuid), (uintptr_t) frame->tf_rip,
196                 TRAPF_USERMODE(frame)))
197                 goto out;
198 #endif
199
200         if ((frame->tf_rflags & PSL_I) == 0) {
201                 /*
202                  * Buggy application or kernel code has disabled
203                  * interrupts and then trapped.  Enabling interrupts
204                  * now is wrong, but it is better than running with
205                  * interrupts disabled until they are accidentally
206                  * enabled later.
207                  */
208                 if (ISPL(frame->tf_cs) == SEL_UPL)
209                         printf(
210                             "pid %ld (%s): trap %d with interrupts disabled\n",
211                             (long)curproc->p_pid, curthread->td_name, type);
212                 else if (type != T_NMI && type != T_BPTFLT &&
213                     type != T_TRCTRAP) {
214                         /*
215                          * XXX not quite right, since this may be for a
216                          * multiple fault in user mode.
217                          */
218                         printf("kernel trap %d with interrupts disabled\n",
219                             type);
220                         /*
221                          * We shouldn't enable interrupts while holding a
222                          * spin lock or servicing an NMI.
223                          */
224                         if (type != T_NMI && td->td_md.md_spinlock_count == 0)
225                                 enable_intr();
226                 }
227         }
228
229         code = frame->tf_err;
230         if (type == T_PAGEFLT) {
231                 /*
232                  * If we get a page fault while in a critical section, then
233                  * it is most likely a fatal kernel page fault.  The kernel
234                  * is already going to panic trying to get a sleep lock to
235                  * do the VM lookup, so just consider it a fatal trap so the
236                  * kernel can print out a useful trap message and even get
237                  * to the debugger.
238                  *
239                  * If we get a page fault while holding a non-sleepable
240                  * lock, then it is most likely a fatal kernel page fault.
241                  * If WITNESS is enabled, then it's going to whine about
242                  * bogus LORs with various VM locks, so just skip to the
243                  * fatal trap handling directly.
244                  */
245                 if (td->td_critnest != 0 ||
246                     WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
247                     "Kernel page fault") != 0)
248                         trap_fatal(frame, frame->tf_addr);
249         }
250
251         if (ISPL(frame->tf_cs) == SEL_UPL) {
252                 /* user trap */
253
254                 td->td_pticks = 0;
255                 td->td_frame = frame;
256                 addr = frame->tf_rip;
257                 if (td->td_ucred != p->p_ucred) 
258                         cred_update_thread(td);
259
260                 switch (type) {
261                 case T_PRIVINFLT:       /* privileged instruction fault */
262                         i = SIGILL;
263                         ucode = ILL_PRVOPC;
264                         break;
265
266                 case T_BPTFLT:          /* bpt instruction fault */
267                 case T_TRCTRAP:         /* trace trap */
268                         enable_intr();
269                         frame->tf_rflags &= ~PSL_T;
270                         i = SIGTRAP;
271                         ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
272                         break;
273
274                 case T_ARITHTRAP:       /* arithmetic trap */
275                         ucode = fputrap();
276                         if (ucode == -1)
277                                 goto userout;
278                         i = SIGFPE;
279                         break;
280
281                 case T_PROTFLT:         /* general protection fault */
282                         i = SIGBUS;
283                         ucode = BUS_OBJERR;
284                         break;
285                 case T_STKFLT:          /* stack fault */
286                 case T_SEGNPFLT:        /* segment not present fault */
287                         i = SIGBUS;
288                         ucode = BUS_ADRERR;
289                         break;
290                 case T_TSSFLT:          /* invalid TSS fault */
291                         i = SIGBUS;
292                         ucode = BUS_OBJERR;
293                         break;
294                 case T_DOUBLEFLT:       /* double fault */
295                 default:
296                         i = SIGBUS;
297                         ucode = BUS_OBJERR;
298                         break;
299
300                 case T_PAGEFLT:         /* page fault */
301                         addr = frame->tf_addr;
302 #ifdef KSE
303                         if (td->td_pflags & TDP_SA)
304                                 thread_user_enter(td);
305 #endif
306                         i = trap_pfault(frame, TRUE);
307                         if (i == -1)
308                                 goto userout;
309                         if (i == 0)
310                                 goto user;
311
312                         if (i == SIGSEGV)
313                                 ucode = SEGV_MAPERR;
314                         else {
315                                 i = SIGSEGV; /* XXX hack */
316                                 ucode = SEGV_ACCERR;
317                         }
318                         break;
319
320                 case T_DIVIDE:          /* integer divide fault */
321                         ucode = FPE_INTDIV;
322                         i = SIGFPE;
323                         break;
324
325 #ifdef DEV_ISA
326                 case T_NMI:
327                         /* machine/parity/power fail/"kitchen sink" faults */
328                         /* XXX Giant */
329                         if (isa_nmi(code) == 0) {
330 #ifdef KDB
331                                 /*
332                                  * NMI can be hooked up to a pushbutton
333                                  * for debugging.
334                                  */
335                                 if (kdb_on_nmi) {
336                                         printf ("NMI ... going to debugger\n");
337                                         kdb_trap(type, 0, frame);
338                                 }
339 #endif /* KDB */
340                                 goto userout;
341                         } else if (panic_on_nmi)
342                                 panic("NMI indicates hardware failure");
343                         break;
344 #endif /* DEV_ISA */
345
346                 case T_OFLOW:           /* integer overflow fault */
347                         ucode = FPE_INTOVF;
348                         i = SIGFPE;
349                         break;
350
351                 case T_BOUND:           /* bounds check fault */
352                         ucode = FPE_FLTSUB;
353                         i = SIGFPE;
354                         break;
355
356                 case T_DNA:
357                         /* transparent fault (due to context switch "late") */
358                         if (fpudna())
359                                 goto userout;
360                         printf("pid %d killed due to lack of floating point\n",
361                                 p->p_pid);
362                         i = SIGKILL;
363                         ucode = 0;
364                         break;
365
366                 case T_FPOPFLT:         /* FPU operand fetch fault */
367                         ucode = ILL_COPROC;
368                         i = SIGILL;
369                         break;
370
371                 case T_XMMFLT:          /* SIMD floating-point exception */
372                         ucode = 0; /* XXX */
373                         i = SIGFPE;
374                         break;
375                 }
376         } else {
377                 /* kernel trap */
378
379                 KASSERT(cold || td->td_ucred != NULL,
380                     ("kernel trap doesn't have ucred"));
381                 switch (type) {
382                 case T_PAGEFLT:                 /* page fault */
383                         (void) trap_pfault(frame, FALSE);
384                         goto out;
385
386                 case T_DNA:
387                         /*
388                          * The kernel is apparently using fpu for copying.
389                          * XXX this should be fatal unless the kernel has
390                          * registered such use.
391                          */
392                         if (fpudna()) {
393                                 printf("fpudna in kernel mode!\n");
394                                 goto out;
395                         }
396                         break;
397
398                 case T_STKFLT:          /* stack fault */
399                         break;
400
401                 case T_PROTFLT:         /* general protection fault */
402                 case T_SEGNPFLT:        /* segment not present fault */
403                         if (td->td_intr_nesting_level != 0)
404                                 break;
405
406                         /*
407                          * Invalid segment selectors and out of bounds
408                          * %rip's and %rsp's can be set up in user mode.
409                          * This causes a fault in kernel mode when the
410                          * kernel tries to return to user mode.  We want
411                          * to get this fault so that we can fix the
412                          * problem here and not have to check all the
413                          * selectors and pointers when the user changes
414                          * them.
415                          */
416                         if (frame->tf_rip == (long)doreti_iret) {
417                                 frame->tf_rip = (long)doreti_iret_fault;
418                                 goto out;
419                         }
420                         if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
421                                 frame->tf_rip =
422                                     (long)PCPU_GET(curpcb)->pcb_onfault;
423                                 goto out;
424                         }
425                         break;
426
427                 case T_TSSFLT:
428                         /*
429                          * PSL_NT can be set in user mode and isn't cleared
430                          * automatically when the kernel is entered.  This
431                          * causes a TSS fault when the kernel attempts to
432                          * `iret' because the TSS link is uninitialized.  We
433                          * want to get this fault so that we can fix the
434                          * problem here and not every time the kernel is
435                          * entered.
436                          */
437                         if (frame->tf_rflags & PSL_NT) {
438                                 frame->tf_rflags &= ~PSL_NT;
439                                 goto out;
440                         }
441                         break;
442
443                 case T_TRCTRAP:  /* trace trap */
444                         /*
445                          * Ignore debug register trace traps due to
446                          * accesses in the user's address space, which
447                          * can happen under several conditions such as
448                          * if a user sets a watchpoint on a buffer and
449                          * then passes that buffer to a system call.
450                          * We still want to get TRCTRAPS for addresses
451                          * in kernel space because that is useful when
452                          * debugging the kernel.
453                          */
454                         if (user_dbreg_trap()) {
455                                 /*
456                                  * Reset breakpoint bits because the
457                                  * processor doesn't
458                                  */
459                                 /* XXX check upper bits here */
460                                 load_dr6(rdr6() & 0xfffffff0);
461                                 goto out;
462                         }
463                         /*
464                          * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
465                          */
466                 case T_BPTFLT:
467                         /*
468                          * If KDB is enabled, let it handle the debugger trap.
469                          * Otherwise, debugger traps "can't happen".
470                          */
471 #ifdef KDB
472                         if (kdb_trap(type, 0, frame))
473                                 goto out;
474 #endif
475                         break;
476
477 #ifdef DEV_ISA
478                 case T_NMI:
479                         /* XXX Giant */
480                         /* machine/parity/power fail/"kitchen sink" faults */
481                         if (isa_nmi(code) == 0) {
482 #ifdef KDB
483                                 /*
484                                  * NMI can be hooked up to a pushbutton
485                                  * for debugging.
486                                  */
487                                 if (kdb_on_nmi) {
488                                         printf ("NMI ... going to debugger\n");
489                                         kdb_trap(type, 0, frame);
490                                 }
491 #endif /* KDB */
492                                 goto out;
493                         } else if (panic_on_nmi == 0)
494                                 goto out;
495                         /* FALLTHROUGH */
496 #endif /* DEV_ISA */
497                 }
498
499                 trap_fatal(frame, 0);
500                 goto out;
501         }
502
503         /* Translate fault for emulators (e.g. Linux) */
504         if (*p->p_sysent->sv_transtrap)
505                 i = (*p->p_sysent->sv_transtrap)(i, type);
506
507         ksiginfo_init_trap(&ksi);
508         ksi.ksi_signo = i;
509         ksi.ksi_code = ucode;
510         ksi.ksi_trapno = type;
511         ksi.ksi_addr = (void *)addr;
512         trapsignal(td, &ksi);
513
514 #ifdef DEBUG
515         if (type <= MAX_TRAP_MSG) {
516                 uprintf("fatal process exception: %s",
517                         trap_msg[type]);
518                 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
519                         uprintf(", fault VA = 0x%lx", frame->tf_addr);
520                 uprintf("\n");
521         }
522 #endif
523
524 user:
525         userret(td, frame);
526         mtx_assert(&Giant, MA_NOTOWNED);
527 userout:
528 out:
529         return;
530 }
531
532 static int
533 trap_pfault(frame, usermode)
534         struct trapframe *frame;
535         int usermode;
536 {
537         vm_offset_t va;
538         struct vmspace *vm = NULL;
539         vm_map_t map;
540         int rv = 0;
541         vm_prot_t ftype;
542         struct thread *td = curthread;
543         struct proc *p = td->td_proc;
544         vm_offset_t eva = frame->tf_addr;
545
546         va = trunc_page(eva);
547         if (va >= KERNBASE) {
548                 /*
549                  * Don't allow user-mode faults in kernel address space.
550                  */
551                 if (usermode)
552                         goto nogo;
553
554                 map = kernel_map;
555         } else {
556                 /*
557                  * This is a fault on non-kernel virtual memory.
558                  * vm is initialized above to NULL. If curproc is NULL
559                  * or curproc->p_vmspace is NULL the fault is fatal.
560                  */
561                 if (p != NULL)
562                         vm = p->p_vmspace;
563
564                 if (vm == NULL)
565                         goto nogo;
566
567                 map = &vm->vm_map;
568         }
569
570         /*
571          * PGEX_I is defined only if the execute disable bit capability is
572          * supported and enabled.
573          */
574         if (frame->tf_err & PGEX_W)
575                 ftype = VM_PROT_WRITE;
576         else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
577                 ftype = VM_PROT_EXECUTE;
578         else
579                 ftype = VM_PROT_READ;
580
581         if (map != kernel_map) {
582                 /*
583                  * Keep swapout from messing with us during this
584                  *      critical time.
585                  */
586                 PROC_LOCK(p);
587                 ++p->p_lock;
588                 PROC_UNLOCK(p);
589
590                 /* Fault in the user page: */
591                 rv = vm_fault(map, va, ftype,
592                               (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
593                                                       : VM_FAULT_NORMAL);
594
595                 PROC_LOCK(p);
596                 --p->p_lock;
597                 PROC_UNLOCK(p);
598         } else {
599                 /*
600                  * Don't have to worry about process locking or stacks in the
601                  * kernel.
602                  */
603                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
604         }
605         if (rv == KERN_SUCCESS)
606                 return (0);
607 nogo:
608         if (!usermode) {
609                 if (td->td_intr_nesting_level == 0 &&
610                     PCPU_GET(curpcb)->pcb_onfault != NULL) {
611                         frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
612                         return (0);
613                 }
614                 trap_fatal(frame, eva);
615                 return (-1);
616         }
617
618         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
619 }
620
621 static void
622 trap_fatal(frame, eva)
623         struct trapframe *frame;
624         vm_offset_t eva;
625 {
626         int code, ss;
627         u_int type;
628         long esp;
629         struct soft_segment_descriptor softseg;
630         char *msg;
631
632         code = frame->tf_err;
633         type = frame->tf_trapno;
634         sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)], &softseg);
635
636         if (type <= MAX_TRAP_MSG)
637                 msg = trap_msg[type];
638         else
639                 msg = "UNKNOWN";
640         printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
641             ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
642 #ifdef SMP
643         /* two separate prints in case of a trap on an unmapped page */
644         printf("cpuid = %d; ", PCPU_GET(cpuid));
645         printf("apic id = %02x\n", PCPU_GET(apic_id));
646 #endif
647         if (type == T_PAGEFLT) {
648                 printf("fault virtual address   = 0x%lx\n", eva);
649                 printf("fault code              = %s %s %s, %s\n",
650                         code & PGEX_U ? "user" : "supervisor",
651                         code & PGEX_W ? "write" : "read",
652                         code & PGEX_I ? "instruction" : "data",
653                         code & PGEX_P ? "protection violation" : "page not present");
654         }
655         printf("instruction pointer     = 0x%lx:0x%lx\n",
656                frame->tf_cs & 0xffff, frame->tf_rip);
657         if (ISPL(frame->tf_cs) == SEL_UPL) {
658                 ss = frame->tf_ss & 0xffff;
659                 esp = frame->tf_rsp;
660         } else {
661                 ss = GSEL(GDATA_SEL, SEL_KPL);
662                 esp = (long)&frame->tf_rsp;
663         }
664         printf("stack pointer           = 0x%x:0x%lx\n", ss, esp);
665         printf("frame pointer           = 0x%x:0x%lx\n", ss, frame->tf_rbp);
666         printf("code segment            = base 0x%lx, limit 0x%lx, type 0x%x\n",
667                softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
668         printf("                        = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
669                softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
670                softseg.ssd_gran);
671         printf("processor eflags        = ");
672         if (frame->tf_rflags & PSL_T)
673                 printf("trace trap, ");
674         if (frame->tf_rflags & PSL_I)
675                 printf("interrupt enabled, ");
676         if (frame->tf_rflags & PSL_NT)
677                 printf("nested task, ");
678         if (frame->tf_rflags & PSL_RF)
679                 printf("resume, ");
680         printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
681         printf("current process         = ");
682         if (curproc) {
683                 printf("%lu (%s)\n",
684                     (u_long)curproc->p_pid, curthread->td_name ?
685                     curthread->td_name : "");
686         } else {
687                 printf("Idle\n");
688         }
689
690 #ifdef KDB
691         if (debugger_on_panic || kdb_active)
692                 if (kdb_trap(type, 0, frame))
693                         return;
694 #endif
695         printf("trap number             = %d\n", type);
696         if (type <= MAX_TRAP_MSG)
697                 panic("%s", trap_msg[type]);
698         else
699                 panic("unknown/reserved trap");
700 }
701
702 /*
703  * Double fault handler. Called when a fault occurs while writing
704  * a frame for a trap/exception onto the stack. This usually occurs
705  * when the stack overflows (such is the case with infinite recursion,
706  * for example).
707  */
708 void
709 dblfault_handler(struct trapframe *frame)
710 {
711         printf("\nFatal double fault\n");
712         printf("rip = 0x%lx\n", frame->tf_rip);
713         printf("rsp = 0x%lx\n", frame->tf_rsp);
714         printf("rbp = 0x%lx\n", frame->tf_rbp);
715 #ifdef SMP
716         /* two separate prints in case of a trap on an unmapped page */
717         printf("cpuid = %d; ", PCPU_GET(cpuid));
718         printf("apic id = %02x\n", PCPU_GET(apic_id));
719 #endif
720         panic("double fault");
721 }
722
723 /*
724  *      syscall -       system call request C handler
725  *
726  *      A system call is essentially treated as a trap.
727  */
728 void
729 syscall(struct trapframe *frame)
730 {
731         caddr_t params;
732         struct sysent *callp;
733         struct thread *td = curthread;
734         struct proc *p = td->td_proc;
735         register_t orig_tf_rflags;
736         int error;
737         int narg;
738         register_t args[8];
739         register_t *argp;
740         u_int code;
741         int reg, regcnt;
742         ksiginfo_t ksi;
743
744         PCPU_INC(cnt.v_syscall);
745
746 #ifdef DIAGNOSTIC
747         if (ISPL(frame->tf_cs) != SEL_UPL) {
748                 panic("syscall");
749                 /* NOT REACHED */
750         }
751 #endif
752
753         reg = 0;
754         regcnt = 6;
755         td->td_pticks = 0;
756         td->td_frame = frame;
757         if (td->td_ucred != p->p_ucred) 
758                 cred_update_thread(td);
759 #ifdef KSE
760         if (p->p_flag & P_SA)
761                 thread_user_enter(td);
762 #endif
763         params = (caddr_t)frame->tf_rsp + sizeof(register_t);
764         code = frame->tf_rax;
765         orig_tf_rflags = frame->tf_rflags;
766
767         if (p->p_sysent->sv_prepsyscall) {
768                 /*
769                  * The prep code is MP aware.
770                  */
771                 (*p->p_sysent->sv_prepsyscall)(frame, (int *)args, &code, &params);
772         } else {
773                 if (code == SYS_syscall || code == SYS___syscall) {
774                         code = frame->tf_rdi;
775                         reg++;
776                         regcnt--;
777                 }
778         }
779
780         if (p->p_sysent->sv_mask)
781                 code &= p->p_sysent->sv_mask;
782
783         if (code >= p->p_sysent->sv_size)
784                 callp = &p->p_sysent->sv_table[0];
785         else
786                 callp = &p->p_sysent->sv_table[code];
787
788         narg = callp->sy_narg;
789
790         /*
791          * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
792          */
793         KASSERT(narg <= sizeof(args) / sizeof(args[0]),
794             ("Too many syscall arguments!"));
795         error = 0;
796         argp = &frame->tf_rdi;
797         argp += reg;
798         bcopy(argp, args, sizeof(args[0]) * regcnt);
799         if (narg > regcnt) {
800                 KASSERT(params != NULL, ("copyin args with no params!"));
801                 error = copyin(params, &args[regcnt],
802                         (narg - regcnt) * sizeof(args[0]));
803         }
804         argp = &args[0];
805
806 #ifdef KTRACE
807         if (KTRPOINT(td, KTR_SYSCALL))
808                 ktrsyscall(code, narg, argp);
809 #endif
810
811         CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
812             td->td_proc->p_pid, td->td_name, code);
813
814         td->td_syscalls++;
815
816         if (error == 0) {
817                 td->td_retval[0] = 0;
818                 td->td_retval[1] = frame->tf_rdx;
819
820                 STOPEVENT(p, S_SCE, narg);
821
822                 PTRACESTOP_SC(p, td, S_PT_SCE);
823
824                 AUDIT_SYSCALL_ENTER(code, td);
825                 error = (*callp->sy_call)(td, argp);
826                 AUDIT_SYSCALL_EXIT(error, td);
827         }
828
829         switch (error) {
830         case 0:
831                 frame->tf_rax = td->td_retval[0];
832                 frame->tf_rdx = td->td_retval[1];
833                 frame->tf_rflags &= ~PSL_C;
834                 break;
835
836         case ERESTART:
837                 /*
838                  * Reconstruct pc, we know that 'syscall' is 2 bytes.
839                  * We have to do a full context restore so that %r10
840                  * (which was holding the value of %rcx) is restored for
841                  * the next iteration.
842                  */
843                 frame->tf_rip -= frame->tf_err;
844                 frame->tf_r10 = frame->tf_rcx;
845                 td->td_pcb->pcb_flags |= PCB_FULLCTX;
846                 break;
847
848         case EJUSTRETURN:
849                 break;
850
851         default:
852                 if (p->p_sysent->sv_errsize) {
853                         if (error >= p->p_sysent->sv_errsize)
854                                 error = -1;     /* XXX */
855                         else
856                                 error = p->p_sysent->sv_errtbl[error];
857                 }
858                 frame->tf_rax = error;
859                 frame->tf_rflags |= PSL_C;
860                 break;
861         }
862
863         /*
864          * Traced syscall.
865          */
866         if (orig_tf_rflags & PSL_T) {
867                 frame->tf_rflags &= ~PSL_T;
868                 ksiginfo_init_trap(&ksi);
869                 ksi.ksi_signo = SIGTRAP;
870                 ksi.ksi_code = TRAP_TRACE;
871                 ksi.ksi_addr = (void *)frame->tf_rip;
872                 trapsignal(td, &ksi);
873         }
874
875         /*
876          * Check for misbehavior.
877          */
878         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
879             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
880         KASSERT(td->td_critnest == 0,
881             ("System call %s returning in a critical section",
882             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
883         KASSERT(td->td_locks == 0,
884             ("System call %s returning with %d locks held",
885             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
886             td->td_locks));
887
888         /*
889          * Handle reschedule and other end-of-syscall issues
890          */
891         userret(td, frame);
892
893         CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
894             td->td_proc->p_pid, td->td_name, code);
895
896 #ifdef KTRACE
897         if (KTRPOINT(td, KTR_SYSRET))
898                 ktrsysret(code, error, td->td_retval[0]);
899 #endif
900
901         /*
902          * This works because errno is findable through the
903          * register set.  If we ever support an emulation where this
904          * is not the case, this code will need to be revisited.
905          */
906         STOPEVENT(p, S_SCX, code);
907
908         PTRACESTOP_SC(p, td, S_PT_SCX);
909 }