FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/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.3/sys/i386/i386/trap.c 134685 2004-09-03 06:40:26Z julian $");
42
43 /*
44 * 386 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 #include "opt_npx.h"
52 #include "opt_trap.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
76 #include <vm/vm.h>
77 #include <vm/vm_param.h>
78 #include <vm/pmap.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_extern.h>
83
84 #include <machine/cpu.h>
85 #include <machine/intr_machdep.h>
86 #include <machine/md_var.h>
87 #include <machine/pcb.h>
88 #ifdef SMP
89 #include <machine/smp.h>
90 #endif
91 #include <machine/tss.h>
92 #include <machine/vm86.h>
93
94 #ifdef POWERFAIL_NMI
95 #include <sys/syslog.h>
96 #include <machine/clock.h>
97 #endif
98
99 extern void trap(struct trapframe frame);
100 #ifdef I386_CPU
101 extern int trapwrite(unsigned addr);
102 #endif
103 extern void syscall(struct trapframe frame);
104
105 static int trap_pfault(struct trapframe *, int, vm_offset_t);
106 static void trap_fatal(struct trapframe *, vm_offset_t);
107 void dblfault_handler(void);
108
109 extern inthand_t IDTVEC(lcall_syscall);
110
111 #define MAX_TRAP_MSG 28
112 static char *trap_msg[] = {
113 "", /* 0 unused */
114 "privileged instruction fault", /* 1 T_PRIVINFLT */
115 "", /* 2 unused */
116 "breakpoint instruction fault", /* 3 T_BPTFLT */
117 "", /* 4 unused */
118 "", /* 5 unused */
119 "arithmetic trap", /* 6 T_ARITHTRAP */
120 "", /* 7 unused */
121 "", /* 8 unused */
122 "general protection fault", /* 9 T_PROTFLT */
123 "trace trap", /* 10 T_TRCTRAP */
124 "", /* 11 unused */
125 "page fault", /* 12 T_PAGEFLT */
126 "", /* 13 unused */
127 "alignment fault", /* 14 T_ALIGNFLT */
128 "", /* 15 unused */
129 "", /* 16 unused */
130 "", /* 17 unused */
131 "integer divide fault", /* 18 T_DIVIDE */
132 "non-maskable interrupt trap", /* 19 T_NMI */
133 "overflow trap", /* 20 T_OFLOW */
134 "FPU bounds check fault", /* 21 T_BOUND */
135 "FPU device not available", /* 22 T_DNA */
136 "double fault", /* 23 T_DOUBLEFLT */
137 "FPU operand fetch fault", /* 24 T_FPOPFLT */
138 "invalid TSS fault", /* 25 T_TSSFLT */
139 "segment not present fault", /* 26 T_SEGNPFLT */
140 "stack fault", /* 27 T_STKFLT */
141 "machine check trap", /* 28 T_MCHK */
142 };
143
144 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
145 extern int has_f00f_bug;
146 #endif
147
148 #ifdef KDB
149 static int kdb_on_nmi = 1;
150 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
151 &kdb_on_nmi, 0, "Go to KDB on NMI");
152 #endif
153 static int panic_on_nmi = 1;
154 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
155 &panic_on_nmi, 0, "Panic on NMI");
156
157 #ifdef WITNESS
158 extern char *syscallnames[];
159 #endif
160
161 #ifdef DEVICE_POLLING
162 extern u_int32_t poll_in_trap;
163 extern int ether_poll(int count);
164 #endif /* DEVICE_POLLING */
165
166 /*
167 * Exception, fault, and trap interface to the FreeBSD kernel.
168 * This common code is called from assembly language IDT gate entry
169 * routines that prepare a suitable stack frame, and restore this
170 * frame after the exception has been processed.
171 */
172
173 void
174 trap(frame)
175 struct trapframe frame;
176 {
177 struct thread *td = curthread;
178 struct proc *p = td->td_proc;
179 u_int sticks = 0;
180 int i = 0, ucode = 0, type, code;
181 vm_offset_t eva;
182 #ifdef POWERFAIL_NMI
183 static int lastalert = 0;
184 #endif
185
186 atomic_add_int(&cnt.v_trap, 1);
187 type = frame.tf_trapno;
188
189 #ifdef KDB
190 if (kdb_active) {
191 kdb_reenter();
192 goto out;
193 }
194 #endif
195
196 if ((frame.tf_eflags & PSL_I) == 0) {
197 /*
198 * Buggy application or kernel code has disabled
199 * interrupts and then trapped. Enabling interrupts
200 * now is wrong, but it is better than running with
201 * interrupts disabled until they are accidentally
202 * enabled later.
203 */
204 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
205 printf(
206 "pid %ld (%s): trap %d with interrupts disabled\n",
207 (long)curproc->p_pid, curproc->p_comm, type);
208 else if (type != T_BPTFLT && type != T_TRCTRAP &&
209 frame.tf_eip != (int)cpu_switch_load_gs) {
210 /*
211 * XXX not quite right, since this may be for a
212 * multiple fault in user mode.
213 */
214 printf("kernel trap %d with interrupts disabled\n",
215 type);
216 /*
217 * Page faults need interrupts diasabled until later,
218 * and we shouldn't enable interrupts while in a
219 * critical section.
220 */
221 if (type != T_PAGEFLT && td->td_critnest == 0)
222 enable_intr();
223 }
224 }
225
226 eva = 0;
227 code = frame.tf_err;
228 if (type == T_PAGEFLT) {
229 /*
230 * For some Cyrix CPUs, %cr2 is clobbered by
231 * interrupts. This problem is worked around by using
232 * an interrupt gate for the pagefault handler. We
233 * are finally ready to read %cr2 and then must
234 * reenable interrupts.
235 *
236 * If we get a page fault while in a critical section, then
237 * it is most likely a fatal kernel page fault. The kernel
238 * is already going to panic trying to get a sleep lock to
239 * do the VM lookup, so just consider it a fatal trap so the
240 * kernel can print out a useful trap message and even get
241 * to the debugger.
242 */
243 eva = rcr2();
244 if (td->td_critnest == 0)
245 enable_intr();
246 else
247 trap_fatal(&frame, eva);
248 }
249
250 #ifdef DEVICE_POLLING
251 if (poll_in_trap)
252 ether_poll(poll_in_trap);
253 #endif /* DEVICE_POLLING */
254
255 if ((ISPL(frame.tf_cs) == SEL_UPL) ||
256 ((frame.tf_eflags & PSL_VM) &&
257 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) {
258 /* user trap */
259
260 sticks = td->td_sticks;
261 td->td_frame = &frame;
262 if (td->td_ucred != p->p_ucred)
263 cred_update_thread(td);
264
265 switch (type) {
266 case T_PRIVINFLT: /* privileged instruction fault */
267 ucode = type;
268 i = SIGILL;
269 break;
270
271 case T_BPTFLT: /* bpt instruction fault */
272 case T_TRCTRAP: /* trace trap */
273 enable_intr();
274 frame.tf_eflags &= ~PSL_T;
275 i = SIGTRAP;
276 break;
277
278 case T_ARITHTRAP: /* arithmetic trap */
279 #ifdef DEV_NPX
280 ucode = npxtrap();
281 if (ucode == -1)
282 goto userout;
283 #else
284 ucode = code;
285 #endif
286 i = SIGFPE;
287 break;
288
289 /*
290 * The following two traps can happen in
291 * vm86 mode, and, if so, we want to handle
292 * them specially.
293 */
294 case T_PROTFLT: /* general protection fault */
295 case T_STKFLT: /* stack fault */
296 if (frame.tf_eflags & PSL_VM) {
297 i = vm86_emulate((struct vm86frame *)&frame);
298 if (i == 0)
299 goto user;
300 break;
301 }
302 /* FALLTHROUGH */
303
304 case T_SEGNPFLT: /* segment not present fault */
305 case T_TSSFLT: /* invalid TSS fault */
306 case T_DOUBLEFLT: /* double fault */
307 default:
308 ucode = code + BUS_SEGM_FAULT ;
309 i = SIGBUS;
310 break;
311
312 case T_PAGEFLT: /* page fault */
313 if (td->td_pflags & TDP_SA)
314 thread_user_enter(td);
315
316 i = trap_pfault(&frame, TRUE, eva);
317 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
318 if (i == -2) {
319 /*
320 * The f00f hack workaround has triggered, so
321 * treat the fault as an illegal instruction
322 * (T_PRIVINFLT) instead of a page fault.
323 */
324 type = frame.tf_trapno = T_PRIVINFLT;
325
326 /* Proceed as in that case. */
327 ucode = type;
328 i = SIGILL;
329 break;
330 }
331 #endif
332 if (i == -1)
333 goto userout;
334 if (i == 0)
335 goto user;
336
337 ucode = T_PAGEFLT;
338 break;
339
340 case T_DIVIDE: /* integer divide fault */
341 ucode = FPE_INTDIV;
342 i = SIGFPE;
343 break;
344
345 #ifdef DEV_ISA
346 case T_NMI:
347 #ifdef POWERFAIL_NMI
348 #ifndef TIMER_FREQ
349 # define TIMER_FREQ 1193182
350 #endif
351 mtx_lock(&Giant);
352 if (time_second - lastalert > 10) {
353 log(LOG_WARNING, "NMI: power fail\n");
354 sysbeep(TIMER_FREQ/880, hz);
355 lastalert = time_second;
356 }
357 mtx_unlock(&Giant);
358 goto userout;
359 #else /* !POWERFAIL_NMI */
360 /* machine/parity/power fail/"kitchen sink" faults */
361 /* XXX Giant */
362 if (isa_nmi(code) == 0) {
363 #ifdef KDB
364 /*
365 * NMI can be hooked up to a pushbutton
366 * for debugging.
367 */
368 if (kdb_on_nmi) {
369 printf ("NMI ... going to debugger\n");
370 kdb_trap(type, 0, &frame);
371 }
372 #endif /* KDB */
373 goto userout;
374 } else if (panic_on_nmi)
375 panic("NMI indicates hardware failure");
376 break;
377 #endif /* POWERFAIL_NMI */
378 #endif /* DEV_ISA */
379
380 case T_OFLOW: /* integer overflow fault */
381 ucode = FPE_INTOVF;
382 i = SIGFPE;
383 break;
384
385 case T_BOUND: /* bounds check fault */
386 ucode = FPE_FLTSUB;
387 i = SIGFPE;
388 break;
389
390 case T_DNA:
391 #ifdef DEV_NPX
392 /* transparent fault (due to context switch "late") */
393 if (npxdna())
394 goto userout;
395 #endif
396 i = SIGFPE;
397 ucode = FPE_FPU_NP_TRAP;
398 break;
399
400 case T_FPOPFLT: /* FPU operand fetch fault */
401 ucode = T_FPOPFLT;
402 i = SIGILL;
403 break;
404
405 case T_XMMFLT: /* SIMD floating-point exception */
406 ucode = 0; /* XXX */
407 i = SIGFPE;
408 break;
409 }
410 } else {
411 /* kernel trap */
412
413 KASSERT(cold || td->td_ucred != NULL,
414 ("kernel trap doesn't have ucred"));
415 switch (type) {
416 case T_PAGEFLT: /* page fault */
417 (void) trap_pfault(&frame, FALSE, eva);
418 goto out;
419
420 case T_DNA:
421 #ifdef DEV_NPX
422 /*
423 * The kernel is apparently using npx for copying.
424 * XXX this should be fatal unless the kernel has
425 * registered such use.
426 */
427 if (npxdna())
428 goto out;
429 #endif
430 break;
431
432 /*
433 * The following two traps can happen in
434 * vm86 mode, and, if so, we want to handle
435 * them specially.
436 */
437 case T_PROTFLT: /* general protection fault */
438 case T_STKFLT: /* stack fault */
439 if (frame.tf_eflags & PSL_VM) {
440 i = vm86_emulate((struct vm86frame *)&frame);
441 if (i != 0)
442 /*
443 * returns to original process
444 */
445 vm86_trap((struct vm86frame *)&frame);
446 goto out;
447 }
448 if (type == T_STKFLT)
449 break;
450
451 /* FALL THROUGH */
452
453 case T_SEGNPFLT: /* segment not present fault */
454 if (PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)
455 break;
456
457 /*
458 * Invalid %fs's and %gs's can be created using
459 * procfs or PT_SETREGS or by invalidating the
460 * underlying LDT entry. This causes a fault
461 * in kernel mode when the kernel attempts to
462 * switch contexts. Lose the bad context
463 * (XXX) so that we can continue, and generate
464 * a signal.
465 */
466 if (frame.tf_eip == (int)cpu_switch_load_gs) {
467 PCPU_GET(curpcb)->pcb_gs = 0;
468 #if 0
469 PROC_LOCK(p);
470 psignal(p, SIGBUS);
471 PROC_UNLOCK(p);
472 #endif
473 goto out;
474 }
475
476 if (td->td_intr_nesting_level != 0)
477 break;
478
479 /*
480 * Invalid segment selectors and out of bounds
481 * %eip's and %esp's can be set up in user mode.
482 * This causes a fault in kernel mode when the
483 * kernel tries to return to user mode. We want
484 * to get this fault so that we can fix the
485 * problem here and not have to check all the
486 * selectors and pointers when the user changes
487 * them.
488 */
489 if (frame.tf_eip == (int)doreti_iret) {
490 frame.tf_eip = (int)doreti_iret_fault;
491 goto out;
492 }
493 if (frame.tf_eip == (int)doreti_popl_ds) {
494 frame.tf_eip = (int)doreti_popl_ds_fault;
495 goto out;
496 }
497 if (frame.tf_eip == (int)doreti_popl_es) {
498 frame.tf_eip = (int)doreti_popl_es_fault;
499 goto out;
500 }
501 if (frame.tf_eip == (int)doreti_popl_fs) {
502 frame.tf_eip = (int)doreti_popl_fs_fault;
503 goto out;
504 }
505 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
506 frame.tf_eip =
507 (int)PCPU_GET(curpcb)->pcb_onfault;
508 goto out;
509 }
510 break;
511
512 case T_TSSFLT:
513 /*
514 * PSL_NT can be set in user mode and isn't cleared
515 * automatically when the kernel is entered. This
516 * causes a TSS fault when the kernel attempts to
517 * `iret' because the TSS link is uninitialized. We
518 * want to get this fault so that we can fix the
519 * problem here and not every time the kernel is
520 * entered.
521 */
522 if (frame.tf_eflags & PSL_NT) {
523 frame.tf_eflags &= ~PSL_NT;
524 goto out;
525 }
526 break;
527
528 case T_TRCTRAP: /* trace trap */
529 if (frame.tf_eip == (int)IDTVEC(lcall_syscall)) {
530 /*
531 * We've just entered system mode via the
532 * syscall lcall. Continue single stepping
533 * silently until the syscall handler has
534 * saved the flags.
535 */
536 goto out;
537 }
538 if (frame.tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
539 /*
540 * The syscall handler has now saved the
541 * flags. Stop single stepping it.
542 */
543 frame.tf_eflags &= ~PSL_T;
544 goto out;
545 }
546 /*
547 * Ignore debug register trace traps due to
548 * accesses in the user's address space, which
549 * can happen under several conditions such as
550 * if a user sets a watchpoint on a buffer and
551 * then passes that buffer to a system call.
552 * We still want to get TRCTRAPS for addresses
553 * in kernel space because that is useful when
554 * debugging the kernel.
555 */
556 /* XXX Giant */
557 if (user_dbreg_trap() &&
558 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)) {
559 /*
560 * Reset breakpoint bits because the
561 * processor doesn't
562 */
563 load_dr6(rdr6() & 0xfffffff0);
564 goto out;
565 }
566 /*
567 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
568 */
569 case T_BPTFLT:
570 /*
571 * If KDB is enabled, let it handle the debugger trap.
572 * Otherwise, debugger traps "can't happen".
573 */
574 #ifdef KDB
575 /* XXX Giant */
576 if (kdb_trap(type, 0, &frame))
577 goto out;
578 #endif
579 break;
580
581 #ifdef DEV_ISA
582 case T_NMI:
583 #ifdef POWERFAIL_NMI
584 mtx_lock(&Giant);
585 if (time_second - lastalert > 10) {
586 log(LOG_WARNING, "NMI: power fail\n");
587 sysbeep(TIMER_FREQ/880, hz);
588 lastalert = time_second;
589 }
590 mtx_unlock(&Giant);
591 goto out;
592 #else /* !POWERFAIL_NMI */
593 /* XXX Giant */
594 /* machine/parity/power fail/"kitchen sink" faults */
595 if (isa_nmi(code) == 0) {
596 #ifdef KDB
597 /*
598 * NMI can be hooked up to a pushbutton
599 * for debugging.
600 */
601 if (kdb_on_nmi) {
602 printf ("NMI ... going to debugger\n");
603 kdb_trap(type, 0, &frame);
604 }
605 #endif /* KDB */
606 goto out;
607 } else if (panic_on_nmi == 0)
608 goto out;
609 /* FALLTHROUGH */
610 #endif /* POWERFAIL_NMI */
611 #endif /* DEV_ISA */
612 }
613
614 trap_fatal(&frame, eva);
615 goto out;
616 }
617
618 /* Translate fault for emulators (e.g. Linux) */
619 if (*p->p_sysent->sv_transtrap)
620 i = (*p->p_sysent->sv_transtrap)(i, type);
621
622 trapsignal(td, i, ucode);
623
624 #ifdef DEBUG
625 if (type <= MAX_TRAP_MSG) {
626 uprintf("fatal process exception: %s",
627 trap_msg[type]);
628 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
629 uprintf(", fault VA = 0x%lx", (u_long)eva);
630 uprintf("\n");
631 }
632 #endif
633
634 user:
635 userret(td, &frame, sticks);
636 mtx_assert(&Giant, MA_NOTOWNED);
637 userout:
638 out:
639 return;
640 }
641
642 static int
643 trap_pfault(frame, usermode, eva)
644 struct trapframe *frame;
645 int usermode;
646 vm_offset_t eva;
647 {
648 vm_offset_t va;
649 struct vmspace *vm = NULL;
650 vm_map_t map = 0;
651 int rv = 0;
652 vm_prot_t ftype;
653 struct thread *td = curthread;
654 struct proc *p = td->td_proc;
655
656 va = trunc_page(eva);
657 if (va >= KERNBASE) {
658 /*
659 * Don't allow user-mode faults in kernel address space.
660 * An exception: if the faulting address is the invalid
661 * instruction entry in the IDT, then the Intel Pentium
662 * F00F bug workaround was triggered, and we need to
663 * treat it is as an illegal instruction, and not a page
664 * fault.
665 */
666 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
667 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
668 return -2;
669 #endif
670 if (usermode)
671 goto nogo;
672
673 map = kernel_map;
674 } else {
675 /*
676 * This is a fault on non-kernel virtual memory.
677 * vm is initialized above to NULL. If curproc is NULL
678 * or curproc->p_vmspace is NULL the fault is fatal.
679 */
680 if (p != NULL)
681 vm = p->p_vmspace;
682
683 if (vm == NULL)
684 goto nogo;
685
686 map = &vm->vm_map;
687 }
688
689 if (frame->tf_err & PGEX_W)
690 ftype = VM_PROT_WRITE;
691 else
692 ftype = VM_PROT_READ;
693
694 if (map != kernel_map) {
695 /*
696 * Keep swapout from messing with us during this
697 * critical time.
698 */
699 PROC_LOCK(p);
700 ++p->p_lock;
701 PROC_UNLOCK(p);
702
703 /* Fault in the user page: */
704 rv = vm_fault(map, va, ftype,
705 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
706 : VM_FAULT_NORMAL);
707
708 PROC_LOCK(p);
709 --p->p_lock;
710 PROC_UNLOCK(p);
711 } else {
712 /*
713 * Don't have to worry about process locking or stacks in the
714 * kernel.
715 */
716 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
717 }
718 if (rv == KERN_SUCCESS)
719 return (0);
720 nogo:
721 if (!usermode) {
722 if (td->td_intr_nesting_level == 0 &&
723 PCPU_GET(curpcb)->pcb_onfault != NULL) {
724 frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
725 return (0);
726 }
727 trap_fatal(frame, eva);
728 return (-1);
729 }
730
731 /* kludge to pass faulting virtual address to sendsig */
732 frame->tf_err = eva;
733
734 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
735 }
736
737 static void
738 trap_fatal(frame, eva)
739 struct trapframe *frame;
740 vm_offset_t eva;
741 {
742 int code, type, ss, esp;
743 struct soft_segment_descriptor softseg;
744
745 code = frame->tf_err;
746 type = frame->tf_trapno;
747 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
748
749 if (type <= MAX_TRAP_MSG)
750 printf("\n\nFatal trap %d: %s while in %s mode\n",
751 type, trap_msg[type],
752 frame->tf_eflags & PSL_VM ? "vm86" :
753 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
754 #ifdef SMP
755 /* two separate prints in case of a trap on an unmapped page */
756 printf("cpuid = %d; ", PCPU_GET(cpuid));
757 printf("apic id = %02x\n", PCPU_GET(apic_id));
758 #endif
759 if (type == T_PAGEFLT) {
760 printf("fault virtual address = 0x%x\n", eva);
761 printf("fault code = %s %s, %s\n",
762 code & PGEX_U ? "user" : "supervisor",
763 code & PGEX_W ? "write" : "read",
764 code & PGEX_P ? "protection violation" : "page not present");
765 }
766 printf("instruction pointer = 0x%x:0x%x\n",
767 frame->tf_cs & 0xffff, frame->tf_eip);
768 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
769 ss = frame->tf_ss & 0xffff;
770 esp = frame->tf_esp;
771 } else {
772 ss = GSEL(GDATA_SEL, SEL_KPL);
773 esp = (int)&frame->tf_esp;
774 }
775 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
776 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
777 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
778 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
779 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
780 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
781 softseg.ssd_gran);
782 printf("processor eflags = ");
783 if (frame->tf_eflags & PSL_T)
784 printf("trace trap, ");
785 if (frame->tf_eflags & PSL_I)
786 printf("interrupt enabled, ");
787 if (frame->tf_eflags & PSL_NT)
788 printf("nested task, ");
789 if (frame->tf_eflags & PSL_RF)
790 printf("resume, ");
791 if (frame->tf_eflags & PSL_VM)
792 printf("vm86, ");
793 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
794 printf("current process = ");
795 if (curproc) {
796 printf("%lu (%s)\n",
797 (u_long)curproc->p_pid, curproc->p_comm ?
798 curproc->p_comm : "");
799 } else {
800 printf("Idle\n");
801 }
802
803 #ifdef KDB
804 if (kdb_trap(type, 0, frame))
805 return;
806 #endif
807 printf("trap number = %d\n", type);
808 if (type <= MAX_TRAP_MSG)
809 panic("%s", trap_msg[type]);
810 else
811 panic("unknown/reserved trap");
812 }
813
814 /*
815 * Double fault handler. Called when a fault occurs while writing
816 * a frame for a trap/exception onto the stack. This usually occurs
817 * when the stack overflows (such is the case with infinite recursion,
818 * for example).
819 *
820 * XXX Note that the current PTD gets replaced by IdlePTD when the
821 * task switch occurs. This means that the stack that was active at
822 * the time of the double fault is not available at <kstack> unless
823 * the machine was idle when the double fault occurred. The downside
824 * of this is that "trace <ebp>" in ddb won't work.
825 */
826 void
827 dblfault_handler()
828 {
829 printf("\nFatal double fault:\n");
830 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
831 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
832 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
833 #ifdef SMP
834 /* two separate prints in case of a trap on an unmapped page */
835 printf("cpuid = %d; ", PCPU_GET(cpuid));
836 printf("apic id = %02x\n", PCPU_GET(apic_id));
837 #endif
838 panic("double fault");
839 }
840
841 #ifdef I386_CPU
842 /*
843 * Compensate for 386 brain damage (missing URKR).
844 * This is a little simpler than the pagefault handler in trap() because
845 * it the page tables have already been faulted in and high addresses
846 * are thrown out early for other reasons.
847 */
848 int trapwrite(addr)
849 unsigned addr;
850 {
851 struct thread *td;
852 struct proc *p;
853 vm_offset_t va;
854 struct vmspace *vm;
855 int rv;
856
857 va = trunc_page((vm_offset_t)addr);
858 /*
859 * XXX - MAX is END. Changed > to >= for temp. fix.
860 */
861 if (va >= VM_MAXUSER_ADDRESS)
862 return (1);
863
864 td = curthread;
865 p = td->td_proc;
866 vm = p->p_vmspace;
867
868 PROC_LOCK(p);
869 ++p->p_lock;
870 PROC_UNLOCK(p);
871
872 /*
873 * fault the data page
874 */
875 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
876
877 PROC_LOCK(p);
878 --p->p_lock;
879 PROC_UNLOCK(p);
880
881 if (rv != KERN_SUCCESS)
882 return 1;
883
884 return (0);
885 }
886 #endif
887
888 /*
889 * syscall - system call request C handler
890 *
891 * A system call is essentially treated as a trap.
892 */
893 void
894 syscall(frame)
895 struct trapframe frame;
896 {
897 caddr_t params;
898 struct sysent *callp;
899 struct thread *td = curthread;
900 struct proc *p = td->td_proc;
901 register_t orig_tf_eflags;
902 u_int sticks;
903 int error;
904 int narg;
905 int args[8];
906 u_int code;
907
908 /*
909 * note: PCPU_LAZY_INC() can only be used if we can afford
910 * occassional inaccuracy in the count.
911 */
912 PCPU_LAZY_INC(cnt.v_syscall);
913
914 #ifdef DIAGNOSTIC
915 if (ISPL(frame.tf_cs) != SEL_UPL) {
916 mtx_lock(&Giant); /* try to stabilize the system XXX */
917 panic("syscall");
918 /* NOT REACHED */
919 mtx_unlock(&Giant);
920 }
921 #endif
922
923 sticks = td->td_sticks;
924 td->td_frame = &frame;
925 if (td->td_ucred != p->p_ucred)
926 cred_update_thread(td);
927 if (p->p_flag & P_SA)
928 thread_user_enter(td);
929 params = (caddr_t)frame.tf_esp + sizeof(int);
930 code = frame.tf_eax;
931 orig_tf_eflags = frame.tf_eflags;
932
933 if (p->p_sysent->sv_prepsyscall) {
934 /*
935 * The prep code is MP aware.
936 */
937 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
938 } else {
939 /*
940 * Need to check if this is a 32 bit or 64 bit syscall.
941 * fuword is MP aware.
942 */
943 if (code == SYS_syscall) {
944 /*
945 * Code is first argument, followed by actual args.
946 */
947 code = fuword(params);
948 params += sizeof(int);
949 } else if (code == SYS___syscall) {
950 /*
951 * Like syscall, but code is a quad, so as to maintain
952 * quad alignment for the rest of the arguments.
953 */
954 code = fuword(params);
955 params += sizeof(quad_t);
956 }
957 }
958
959 if (p->p_sysent->sv_mask)
960 code &= p->p_sysent->sv_mask;
961
962 if (code >= p->p_sysent->sv_size)
963 callp = &p->p_sysent->sv_table[0];
964 else
965 callp = &p->p_sysent->sv_table[code];
966
967 narg = callp->sy_narg & SYF_ARGMASK;
968
969 /*
970 * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
971 */
972 if (params != NULL && narg != 0)
973 error = copyin(params, (caddr_t)args,
974 (u_int)(narg * sizeof(int)));
975 else
976 error = 0;
977
978 #ifdef KTRACE
979 if (KTRPOINT(td, KTR_SYSCALL))
980 ktrsyscall(code, narg, args);
981 #endif
982
983 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
984 td->td_proc->p_pid, td->td_proc->p_comm, code);
985
986 /*
987 * Try to run the syscall without Giant if the syscall
988 * is MP safe.
989 */
990 if ((callp->sy_narg & SYF_MPSAFE) == 0)
991 mtx_lock(&Giant);
992
993 if (error == 0) {
994 td->td_retval[0] = 0;
995 td->td_retval[1] = frame.tf_edx;
996
997 STOPEVENT(p, S_SCE, narg);
998
999 PTRACESTOP_SC(p, td, S_PT_SCE);
1000
1001 error = (*callp->sy_call)(td, args);
1002 }
1003
1004 switch (error) {
1005 case 0:
1006 frame.tf_eax = td->td_retval[0];
1007 frame.tf_edx = td->td_retval[1];
1008 frame.tf_eflags &= ~PSL_C;
1009 break;
1010
1011 case ERESTART:
1012 /*
1013 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1014 * int 0x80 is 2 bytes. We saved this in tf_err.
1015 */
1016 frame.tf_eip -= frame.tf_err;
1017 break;
1018
1019 case EJUSTRETURN:
1020 break;
1021
1022 default:
1023 if (p->p_sysent->sv_errsize) {
1024 if (error >= p->p_sysent->sv_errsize)
1025 error = -1; /* XXX */
1026 else
1027 error = p->p_sysent->sv_errtbl[error];
1028 }
1029 frame.tf_eax = error;
1030 frame.tf_eflags |= PSL_C;
1031 break;
1032 }
1033
1034 /*
1035 * Release Giant if we previously set it.
1036 */
1037 if ((callp->sy_narg & SYF_MPSAFE) == 0)
1038 mtx_unlock(&Giant);
1039
1040 /*
1041 * Traced syscall.
1042 */
1043 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1044 frame.tf_eflags &= ~PSL_T;
1045 trapsignal(td, SIGTRAP, 0);
1046 }
1047
1048 /*
1049 * Handle reschedule and other end-of-syscall issues
1050 */
1051 userret(td, &frame, sticks);
1052
1053 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1054 td->td_proc->p_pid, td->td_proc->p_comm, code);
1055
1056 #ifdef KTRACE
1057 if (KTRPOINT(td, KTR_SYSRET))
1058 ktrsysret(code, error, td->td_retval[0]);
1059 #endif
1060
1061 /*
1062 * This works because errno is findable through the
1063 * register set. If we ever support an emulation where this
1064 * is not the case, this code will need to be revisited.
1065 */
1066 STOPEVENT(p, S_SCX, code);
1067
1068 PTRACESTOP_SC(p, td, S_PT_SCX);
1069
1070 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
1071 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
1072 mtx_assert(&sched_lock, MA_NOTOWNED);
1073 mtx_assert(&Giant, MA_NOTOWNED);
1074 }
1075
Cache object: 48936d4f68b99f971c013862b5274804
|