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