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