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