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 * $FreeBSD$
39 */
40
41 /*
42 * 386 Trap and System call handling
43 */
44
45 #include "opt_cpu.h"
46 #include "opt_ddb.h"
47 #include "opt_ktrace.h"
48 #include "opt_clock.h"
49 #include "opt_trap.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/pioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/syscall.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/uio.h>
62 #include <sys/vmmeter.h>
63 #ifdef KTRACE
64 #include <sys/ktrace.h>
65 #endif
66
67 #include <vm/vm.h>
68 #include <vm/vm_param.h>
69 #include <sys/lock.h>
70 #include <vm/pmap.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_extern.h>
75
76 #include <machine/cpu.h>
77 #include <machine/ipl.h>
78 #include <machine/md_var.h>
79 #include <machine/pcb.h>
80 #ifdef SMP
81 #include <machine/smp.h>
82 #endif
83 #include <machine/tss.h>
84
85 #include <i386/isa/intr_machdep.h>
86
87 #ifdef POWERFAIL_NMI
88 #include <sys/syslog.h>
89 #include <machine/clock.h>
90 #endif
91
92 #include <machine/vm86.h>
93
94 #include <ddb/ddb.h>
95
96 #include "isa.h"
97 #include "npx.h"
98
99 int (*pmath_emulate) __P((struct trapframe *));
100
101 extern void trap __P((struct trapframe frame));
102 extern int trapwrite __P((unsigned addr));
103 extern void syscall2 __P((struct trapframe frame));
104
105 static int trap_pfault __P((struct trapframe *, int, vm_offset_t));
106 static void trap_fatal __P((struct trapframe *, vm_offset_t));
107 void dblfault_handler __P((void));
108
109 extern inthand_t IDTVEC(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 "system forced exception", /* 7 T_ASTFLT */
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 static __inline int userret __P((struct proc *p, struct trapframe *frame,
145 u_quad_t oticks, int have_mplock));
146
147 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
148 extern int has_f00f_bug;
149 #endif
150
151 #ifdef DDB
152 static int ddb_on_nmi = 1;
153 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
154 &ddb_on_nmi, 0, "Go to DDB on NMI");
155 #endif
156 static int panic_on_nmi = 1;
157 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
158 &panic_on_nmi, 0, "Panic on NMI");
159
160 static __inline int
161 userret(p, frame, oticks, have_mplock)
162 struct proc *p;
163 struct trapframe *frame;
164 u_quad_t oticks;
165 int have_mplock;
166 {
167 int sig, s;
168
169 while ((sig = CURSIG(p)) != 0) {
170 if (have_mplock == 0) {
171 get_mplock();
172 have_mplock = 1;
173 }
174 postsig(sig);
175 }
176
177 p->p_priority = p->p_usrpri;
178 if (resched_wanted()) {
179 /*
180 * Since we are curproc, clock will normally just change
181 * our priority without moving us from one queue to another
182 * (since the running process is not on a queue.)
183 * If that happened after we setrunqueue ourselves but before we
184 * mi_switch()'ed, we might not be on the queue indicated by
185 * our priority.
186 */
187 if (have_mplock == 0) {
188 get_mplock();
189 have_mplock = 1;
190 }
191 s = splhigh();
192 setrunqueue(p);
193 p->p_stats->p_ru.ru_nivcsw++;
194 mi_switch();
195 splx(s);
196 while ((sig = CURSIG(p)) != 0)
197 postsig(sig);
198 }
199 /*
200 * Charge system time if profiling.
201 */
202 if (p->p_flag & P_PROFIL) {
203 if (have_mplock == 0) {
204 get_mplock();
205 have_mplock = 1;
206 }
207 addupc_task(p, frame->tf_eip,
208 (u_int)(p->p_sticks - oticks) * psratio);
209 }
210 curpriority = p->p_priority;
211 return(have_mplock);
212 }
213
214 #ifdef DEVICE_POLLING
215 extern u_int32_t poll_in_trap;
216 extern int ether_poll __P((int count));
217 #endif /* DEVICE_POLLING */
218
219 /*
220 * Exception, fault, and trap interface to the FreeBSD kernel.
221 * This common code is called from assembly language IDT gate entry
222 * routines that prepare a suitable stack frame, and restore this
223 * frame after the exception has been processed.
224 */
225
226 void
227 trap(frame)
228 struct trapframe frame;
229 {
230 struct proc *p = curproc;
231 u_quad_t sticks = 0;
232 int i = 0, ucode = 0, type, code;
233 vm_offset_t eva;
234
235 #ifdef DDB
236 if (db_active) {
237 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
238 trap_fatal(&frame, eva);
239 return;
240 }
241 #endif
242
243 if (!(frame.tf_eflags & PSL_I)) {
244 /*
245 * Buggy application or kernel code has disabled interrupts
246 * and then trapped. Enabling interrupts now is wrong, but
247 * it is better than running with interrupts disabled until
248 * they are accidentally enabled later.
249 */
250 type = frame.tf_trapno;
251 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
252 printf(
253 "pid %ld (%s): trap %d with interrupts disabled\n",
254 (long)curproc->p_pid, curproc->p_comm, type);
255 else if (type != T_BPTFLT && type != T_TRCTRAP)
256 /*
257 * XXX not quite right, since this may be for a
258 * multiple fault in user mode.
259 */
260 printf("kernel trap %d with interrupts disabled\n",
261 type);
262 enable_intr();
263 }
264
265 eva = 0;
266 if (frame.tf_trapno == T_PAGEFLT) {
267 /*
268 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
269 * This problem is worked around by using an interrupt
270 * gate for the pagefault handler. We are finally ready
271 * to read %cr2 and then must reenable interrupts.
272 *
273 * XXX this should be in the switch statement, but the
274 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
275 * flow of control too much for this to be obviously
276 * correct.
277 */
278 eva = rcr2();
279 enable_intr();
280 }
281
282 #ifdef DEVICE_POLLING
283 if (poll_in_trap)
284 ether_poll(poll_in_trap);
285 #endif /* DEVICE_POLLING */
286
287 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
288 restart:
289 #endif
290 type = frame.tf_trapno;
291 code = frame.tf_err;
292
293 if (in_vm86call) {
294 if (frame.tf_eflags & PSL_VM &&
295 (type == T_PROTFLT || type == T_STKFLT)) {
296 i = vm86_emulate((struct vm86frame *)&frame);
297 if (i != 0)
298 /*
299 * returns to original process
300 */
301 vm86_trap((struct vm86frame *)&frame);
302 return;
303 }
304 switch (type) {
305 /*
306 * these traps want either a process context, or
307 * assume a normal userspace trap.
308 */
309 case T_PROTFLT:
310 case T_SEGNPFLT:
311 trap_fatal(&frame, eva);
312 return;
313 case T_TRCTRAP:
314 type = T_BPTFLT; /* kernel breakpoint */
315 /* FALL THROUGH */
316 }
317 goto kernel_trap; /* normal kernel trap handling */
318 }
319
320 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
321 /* user trap */
322
323 sticks = p->p_sticks;
324 p->p_md.md_regs = &frame;
325
326 switch (type) {
327 case T_PRIVINFLT: /* privileged instruction fault */
328 ucode = type;
329 i = SIGILL;
330 break;
331
332 case T_BPTFLT: /* bpt instruction fault */
333 case T_TRCTRAP: /* trace trap */
334 frame.tf_eflags &= ~PSL_T;
335 i = SIGTRAP;
336 break;
337
338 case T_ARITHTRAP: /* arithmetic trap */
339 ucode = code;
340 i = SIGFPE;
341 break;
342
343 case T_ASTFLT: /* Allow process switch */
344 astoff();
345 cnt.v_soft++;
346 if (p->p_flag & P_OWEUPC) {
347 p->p_flag &= ~P_OWEUPC;
348 addupc_task(p, p->p_stats->p_prof.pr_addr,
349 p->p_stats->p_prof.pr_ticks);
350 }
351 goto out;
352
353 /*
354 * The following two traps can happen in
355 * vm86 mode, and, if so, we want to handle
356 * them specially.
357 */
358 case T_PROTFLT: /* general protection fault */
359 case T_STKFLT: /* stack fault */
360 if (frame.tf_eflags & PSL_VM) {
361 i = vm86_emulate((struct vm86frame *)&frame);
362 if (i == 0)
363 goto out;
364 break;
365 }
366 /* FALL THROUGH */
367
368 case T_SEGNPFLT: /* segment not present fault */
369 case T_TSSFLT: /* invalid TSS fault */
370 case T_DOUBLEFLT: /* double fault */
371 default:
372 ucode = code + BUS_SEGM_FAULT ;
373 i = SIGBUS;
374 break;
375
376 case T_PAGEFLT: /* page fault */
377 i = trap_pfault(&frame, TRUE, eva);
378 if (i == -1)
379 return;
380 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
381 if (i == -2)
382 goto restart;
383 #endif
384 if (i == 0)
385 goto out;
386
387 ucode = T_PAGEFLT;
388 break;
389
390 case T_DIVIDE: /* integer divide fault */
391 ucode = FPE_INTDIV;
392 i = SIGFPE;
393 break;
394
395 #if NISA > 0
396 case T_NMI:
397 #ifdef POWERFAIL_NMI
398 goto handle_powerfail;
399 #else /* !POWERFAIL_NMI */
400 /* machine/parity/power fail/"kitchen sink" faults */
401 if (isa_nmi(code) == 0) {
402 #ifdef DDB
403 /*
404 * NMI can be hooked up to a pushbutton
405 * for debugging.
406 */
407 if (ddb_on_nmi) {
408 printf ("NMI ... going to debugger\n");
409 kdb_trap (type, 0, &frame);
410 }
411 #endif /* DDB */
412 return;
413 } else if (panic_on_nmi)
414 panic("NMI indicates hardware failure");
415 break;
416 #endif /* POWERFAIL_NMI */
417 #endif /* NISA > 0 */
418
419 case T_OFLOW: /* integer overflow fault */
420 ucode = FPE_INTOVF;
421 i = SIGFPE;
422 break;
423
424 case T_BOUND: /* bounds check fault */
425 ucode = FPE_FLTSUB;
426 i = SIGFPE;
427 break;
428
429 case T_DNA:
430 #if NNPX > 0
431 /* if a transparent fault (due to context switch "late") */
432 if (npxdna())
433 return;
434 #endif
435 if (!pmath_emulate) {
436 i = SIGFPE;
437 ucode = FPE_FPU_NP_TRAP;
438 break;
439 }
440 i = (*pmath_emulate)(&frame);
441 if (i == 0) {
442 if (!(frame.tf_eflags & PSL_T))
443 return;
444 frame.tf_eflags &= ~PSL_T;
445 i = SIGTRAP;
446 }
447 /* else ucode = emulator_only_knows() XXX */
448 break;
449
450 case T_FPOPFLT: /* FPU operand fetch fault */
451 ucode = T_FPOPFLT;
452 i = SIGILL;
453 break;
454
455 case T_XMMFLT: /* SIMD floating-point exception */
456 ucode = 0; /* XXX */
457 i = SIGFPE;
458 break;
459 }
460 } else {
461 kernel_trap:
462 /* kernel trap */
463
464 switch (type) {
465 case T_PAGEFLT: /* page fault */
466 (void) trap_pfault(&frame, FALSE, eva);
467 return;
468
469 case T_DNA:
470 #if NNPX > 0
471 /*
472 * The kernel is apparently using npx for copying.
473 * XXX this should be fatal unless the kernel has
474 * registered such use.
475 */
476 if (npxdna())
477 return;
478 #endif
479 break;
480
481 case T_PROTFLT: /* general protection fault */
482 case T_SEGNPFLT: /* segment not present fault */
483 /*
484 * Invalid segment selectors and out of bounds
485 * %eip's and %esp's can be set up in user mode.
486 * This causes a fault in kernel mode when the
487 * kernel tries to return to user mode. We want
488 * to get this fault so that we can fix the
489 * problem here and not have to check all the
490 * selectors and pointers when the user changes
491 * them.
492 */
493 #define MAYBE_DORETI_FAULT(where, whereto) \
494 do { \
495 if (frame.tf_eip == (int)where) { \
496 frame.tf_eip = (int)whereto; \
497 return; \
498 } \
499 } while (0)
500
501 if (intr_nesting_level == 0) {
502 /*
503 * Invalid %fs's and %gs's can be created using
504 * procfs or PT_SETREGS or by invalidating the
505 * underlying LDT entry. This causes a fault
506 * in kernel mode when the kernel attempts to
507 * switch contexts. Lose the bad context
508 * (XXX) so that we can continue, and generate
509 * a signal.
510 */
511 if (frame.tf_eip == (int)cpu_switch_load_gs) {
512 curpcb->pcb_gs = 0;
513 psignal(p, SIGBUS);
514 return;
515 }
516 MAYBE_DORETI_FAULT(doreti_iret,
517 doreti_iret_fault);
518 MAYBE_DORETI_FAULT(doreti_popl_ds,
519 doreti_popl_ds_fault);
520 MAYBE_DORETI_FAULT(doreti_popl_es,
521 doreti_popl_es_fault);
522 MAYBE_DORETI_FAULT(doreti_popl_fs,
523 doreti_popl_fs_fault);
524 if (curpcb && curpcb->pcb_onfault) {
525 frame.tf_eip = (int)curpcb->pcb_onfault;
526 return;
527 }
528 }
529 break;
530
531 case T_TSSFLT:
532 /*
533 * PSL_NT can be set in user mode and isn't cleared
534 * automatically when the kernel is entered. This
535 * causes a TSS fault when the kernel attempts to
536 * `iret' because the TSS link is uninitialized. We
537 * want to get this fault so that we can fix the
538 * problem here and not every time the kernel is
539 * entered.
540 */
541 if (frame.tf_eflags & PSL_NT) {
542 frame.tf_eflags &= ~PSL_NT;
543 return;
544 }
545 break;
546
547 case T_TRCTRAP: /* trace trap */
548 if (frame.tf_eip == (int)IDTVEC(syscall)) {
549 /*
550 * We've just entered system mode via the
551 * syscall lcall. Continue single stepping
552 * silently until the syscall handler has
553 * saved the flags.
554 */
555 return;
556 }
557 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
558 /*
559 * The syscall handler has now saved the
560 * flags. Stop single stepping it.
561 */
562 frame.tf_eflags &= ~PSL_T;
563 return;
564 }
565 /*
566 * Ignore debug register trace traps due to
567 * accesses in the user's address space, which
568 * can happen under several conditions such as
569 * if a user sets a watchpoint on a buffer and
570 * then passes that buffer to a system call.
571 * We still want to get TRCTRAPS for addresses
572 * in kernel space because that is useful when
573 * debugging the kernel.
574 */
575 if (user_dbreg_trap()) {
576 /*
577 * Reset breakpoint bits because the
578 * processor doesn't
579 */
580 load_dr6(rdr6() & 0xfffffff0);
581 return;
582 }
583 /*
584 * Fall through (TRCTRAP kernel mode, kernel address)
585 */
586 case T_BPTFLT:
587 /*
588 * If DDB is enabled, let it handle the debugger trap.
589 * Otherwise, debugger traps "can't happen".
590 */
591 #ifdef DDB
592 if (kdb_trap (type, 0, &frame))
593 return;
594 #endif
595 break;
596
597 #if NISA > 0
598 case T_NMI:
599 #ifdef POWERFAIL_NMI
600 #ifndef TIMER_FREQ
601 # define TIMER_FREQ 1193182
602 #endif
603 handle_powerfail:
604 {
605 static unsigned lastalert = 0;
606
607 if(time_second - lastalert > 10)
608 {
609 log(LOG_WARNING, "NMI: power fail\n");
610 sysbeep(TIMER_FREQ/880, hz);
611 lastalert = time_second;
612 }
613 return;
614 }
615 #else /* !POWERFAIL_NMI */
616 /* machine/parity/power fail/"kitchen sink" faults */
617 if (isa_nmi(code) == 0) {
618 #ifdef DDB
619 /*
620 * NMI can be hooked up to a pushbutton
621 * for debugging.
622 */
623 if (ddb_on_nmi) {
624 printf ("NMI ... going to debugger\n");
625 kdb_trap (type, 0, &frame);
626 }
627 #endif /* DDB */
628 return;
629 } else if (panic_on_nmi == 0)
630 return;
631 /* FALL THROUGH */
632 #endif /* POWERFAIL_NMI */
633 #endif /* NISA > 0 */
634 }
635
636 trap_fatal(&frame, eva);
637 return;
638 }
639
640 /* Translate fault for emulators (e.g. Linux) */
641 if (*p->p_sysent->sv_transtrap)
642 i = (*p->p_sysent->sv_transtrap)(i, type);
643
644 trapsignal(p, i, ucode);
645
646 #ifdef DEBUG
647 if (type <= MAX_TRAP_MSG) {
648 uprintf("fatal process exception: %s",
649 trap_msg[type]);
650 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
651 uprintf(", fault VA = 0x%lx", (u_long)eva);
652 uprintf("\n");
653 }
654 #endif
655
656 out:
657 userret(p, &frame, sticks, 1);
658 }
659
660 #ifdef notyet
661 /*
662 * This version doesn't allow a page fault to user space while
663 * in the kernel. The rest of the kernel needs to be made "safe"
664 * before this can be used. I think the only things remaining
665 * to be made safe are the iBCS2 code and the process tracing/
666 * debugging code.
667 */
668 static int
669 trap_pfault(frame, usermode, eva)
670 struct trapframe *frame;
671 int usermode;
672 vm_offset_t eva;
673 {
674 vm_offset_t va;
675 struct vmspace *vm = NULL;
676 vm_map_t map = 0;
677 int rv = 0;
678 vm_prot_t ftype;
679 struct proc *p = curproc;
680
681 if (frame->tf_err & PGEX_W)
682 ftype = VM_PROT_WRITE;
683 else
684 ftype = VM_PROT_READ;
685
686 va = trunc_page(eva);
687 if (va < VM_MIN_KERNEL_ADDRESS) {
688 vm_offset_t v;
689 vm_page_t mpte;
690
691 if (p == NULL ||
692 (!usermode && va < VM_MAXUSER_ADDRESS &&
693 (intr_nesting_level != 0 || curpcb == NULL ||
694 curpcb->pcb_onfault == NULL))) {
695 trap_fatal(frame, eva);
696 return (-1);
697 }
698
699 /*
700 * This is a fault on non-kernel virtual memory.
701 * vm is initialized above to NULL. If curproc is NULL
702 * or curproc->p_vmspace is NULL the fault is fatal.
703 */
704 vm = p->p_vmspace;
705 if (vm == NULL)
706 goto nogo;
707
708 map = &vm->vm_map;
709
710 /*
711 * Keep swapout from messing with us during this
712 * critical time.
713 */
714 ++p->p_lock;
715
716 /*
717 * Grow the stack if necessary
718 */
719 /* grow_stack returns false only if va falls into
720 * a growable stack region and the stack growth
721 * fails. It returns true if va was not within
722 * a growable stack region, or if the stack
723 * growth succeeded.
724 */
725 if (!grow_stack (p, va)) {
726 rv = KERN_FAILURE;
727 --p->p_lock;
728 goto nogo;
729 }
730
731 /* Fault in the user page: */
732 rv = vm_fault(map, va, ftype,
733 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
734 : VM_FAULT_NORMAL);
735
736 --p->p_lock;
737 } else {
738 /*
739 * Don't allow user-mode faults in kernel address space.
740 */
741 if (usermode)
742 goto nogo;
743
744 /*
745 * Since we know that kernel virtual address addresses
746 * always have pte pages mapped, we just have to fault
747 * the page.
748 */
749 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
750 }
751
752 if (rv == KERN_SUCCESS)
753 return (0);
754 nogo:
755 if (!usermode) {
756 if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) {
757 frame->tf_eip = (int)curpcb->pcb_onfault;
758 return (0);
759 }
760 trap_fatal(frame, eva);
761 return (-1);
762 }
763
764 /* kludge to pass faulting virtual address to sendsig */
765 frame->tf_err = eva;
766
767 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
768 }
769 #endif
770
771 int
772 trap_pfault(frame, usermode, eva)
773 struct trapframe *frame;
774 int usermode;
775 vm_offset_t eva;
776 {
777 vm_offset_t va;
778 struct vmspace *vm = NULL;
779 vm_map_t map = 0;
780 int rv = 0;
781 vm_prot_t ftype;
782 struct proc *p = curproc;
783
784 va = trunc_page(eva);
785 if (va >= KERNBASE) {
786 /*
787 * Don't allow user-mode faults in kernel address space.
788 * An exception: if the faulting address is the invalid
789 * instruction entry in the IDT, then the Intel Pentium
790 * F00F bug workaround was triggered, and we need to
791 * treat it is as an illegal instruction, and not a page
792 * fault.
793 */
794 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
795 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
796 frame->tf_trapno = T_PRIVINFLT;
797 return -2;
798 }
799 #endif
800 if (usermode)
801 goto nogo;
802
803 map = kernel_map;
804 } else {
805 /*
806 * This is a fault on non-kernel virtual memory.
807 * vm is initialized above to NULL. If curproc is NULL
808 * or curproc->p_vmspace is NULL the fault is fatal.
809 */
810 if (p != NULL)
811 vm = p->p_vmspace;
812
813 if (vm == NULL)
814 goto nogo;
815
816 map = &vm->vm_map;
817 }
818
819 if (frame->tf_err & PGEX_W)
820 ftype = VM_PROT_WRITE;
821 else
822 ftype = VM_PROT_READ;
823
824 if (map != kernel_map) {
825 /*
826 * Keep swapout from messing with us during this
827 * critical time.
828 */
829 ++p->p_lock;
830
831 /*
832 * Grow the stack if necessary
833 */
834 /* grow_stack returns false only if va falls into
835 * a growable stack region and the stack growth
836 * fails. It returns true if va was not within
837 * a growable stack region, or if the stack
838 * growth succeeded.
839 */
840 if (!grow_stack (p, va)) {
841 rv = KERN_FAILURE;
842 --p->p_lock;
843 goto nogo;
844 }
845
846 /* Fault in the user page: */
847 rv = vm_fault(map, va, ftype,
848 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
849 : VM_FAULT_NORMAL);
850
851 --p->p_lock;
852 } else {
853 /*
854 * Don't have to worry about process locking or stacks in the kernel.
855 */
856 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
857 }
858
859 if (rv == KERN_SUCCESS)
860 return (0);
861 nogo:
862 if (!usermode) {
863 if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) {
864 frame->tf_eip = (int)curpcb->pcb_onfault;
865 return (0);
866 }
867 trap_fatal(frame, eva);
868 return (-1);
869 }
870
871 /* kludge to pass faulting virtual address to sendsig */
872 frame->tf_err = eva;
873
874 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
875 }
876
877 static void
878 trap_fatal(frame, eva)
879 struct trapframe *frame;
880 vm_offset_t eva;
881 {
882 int code, type, ss, esp;
883 struct soft_segment_descriptor softseg;
884
885 code = frame->tf_err;
886 type = frame->tf_trapno;
887 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
888
889 if (type <= MAX_TRAP_MSG)
890 printf("\n\nFatal trap %d: %s while in %s mode\n",
891 type, trap_msg[type],
892 frame->tf_eflags & PSL_VM ? "vm86" :
893 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
894 #ifdef SMP
895 /* three seperate prints in case of a trap on an unmapped page */
896 printf("mp_lock = %08x; ", mp_lock);
897 printf("cpuid = %d; ", cpuid);
898 printf("lapic.id = %08x\n", lapic.id);
899 #endif
900 if (type == T_PAGEFLT) {
901 printf("fault virtual address = 0x%x\n", eva);
902 printf("fault code = %s %s, %s\n",
903 code & PGEX_U ? "user" : "supervisor",
904 code & PGEX_W ? "write" : "read",
905 code & PGEX_P ? "protection violation" : "page not present");
906 }
907 printf("instruction pointer = 0x%x:0x%x\n",
908 frame->tf_cs & 0xffff, frame->tf_eip);
909 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
910 ss = frame->tf_ss & 0xffff;
911 esp = frame->tf_esp;
912 } else {
913 ss = GSEL(GDATA_SEL, SEL_KPL);
914 esp = (int)&frame->tf_esp;
915 }
916 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
917 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
918 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
919 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
920 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
921 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
922 softseg.ssd_gran);
923 printf("processor eflags = ");
924 if (frame->tf_eflags & PSL_T)
925 printf("trace trap, ");
926 if (frame->tf_eflags & PSL_I)
927 printf("interrupt enabled, ");
928 if (frame->tf_eflags & PSL_NT)
929 printf("nested task, ");
930 if (frame->tf_eflags & PSL_RF)
931 printf("resume, ");
932 if (frame->tf_eflags & PSL_VM)
933 printf("vm86, ");
934 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
935 printf("current process = ");
936 if (curproc) {
937 printf("%lu (%s)\n",
938 (u_long)curproc->p_pid, curproc->p_comm ?
939 curproc->p_comm : "");
940 } else {
941 printf("Idle\n");
942 }
943 printf("interrupt mask = ");
944 if ((cpl & net_imask) == net_imask)
945 printf("net ");
946 if ((cpl & tty_imask) == tty_imask)
947 printf("tty ");
948 if ((cpl & bio_imask) == bio_imask)
949 printf("bio ");
950 if ((cpl & cam_imask) == cam_imask)
951 printf("cam ");
952 if (cpl == 0)
953 printf("none");
954 #ifdef SMP
955 /**
956 * XXX FIXME:
957 * we probably SHOULD have stopped the other CPUs before now!
958 * another CPU COULD have been touching cpl at this moment...
959 */
960 printf(" <- SMP: XXX");
961 #endif
962 printf("\n");
963
964 #ifdef KDB
965 if (kdb_trap(&psl))
966 return;
967 #endif
968 #ifdef DDB
969 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
970 return;
971 #endif
972 printf("trap number = %d\n", type);
973 if (type <= MAX_TRAP_MSG)
974 panic("%s", trap_msg[type]);
975 else
976 panic("unknown/reserved trap");
977 }
978
979 /*
980 * Double fault handler. Called when a fault occurs while writing
981 * a frame for a trap/exception onto the stack. This usually occurs
982 * when the stack overflows (such is the case with infinite recursion,
983 * for example).
984 *
985 * XXX Note that the current PTD gets replaced by IdlePTD when the
986 * task switch occurs. This means that the stack that was active at
987 * the time of the double fault is not available at <kstack> unless
988 * the machine was idle when the double fault occurred. The downside
989 * of this is that "trace <ebp>" in ddb won't work.
990 */
991 void
992 dblfault_handler()
993 {
994 printf("\nFatal double fault:\n");
995 printf("eip = 0x%x\n", common_tss.tss_eip);
996 printf("esp = 0x%x\n", common_tss.tss_esp);
997 printf("ebp = 0x%x\n", common_tss.tss_ebp);
998 #ifdef SMP
999 /* three seperate prints in case of a trap on an unmapped page */
1000 printf("mp_lock = %08x; ", mp_lock);
1001 printf("cpuid = %d; ", cpuid);
1002 printf("lapic.id = %08x\n", lapic.id);
1003 #endif
1004 panic("double fault");
1005 }
1006
1007 /*
1008 * Compensate for 386 brain damage (missing URKR).
1009 * This is a little simpler than the pagefault handler in trap() because
1010 * it the page tables have already been faulted in and high addresses
1011 * are thrown out early for other reasons.
1012 */
1013 int trapwrite(addr)
1014 unsigned addr;
1015 {
1016 struct proc *p;
1017 vm_offset_t va;
1018 struct vmspace *vm;
1019 int rv;
1020
1021 va = trunc_page((vm_offset_t)addr);
1022 /*
1023 * XXX - MAX is END. Changed > to >= for temp. fix.
1024 */
1025 if (va >= VM_MAXUSER_ADDRESS)
1026 return (1);
1027
1028 p = curproc;
1029 vm = p->p_vmspace;
1030
1031 ++p->p_lock;
1032
1033 if (!grow_stack (p, va)) {
1034 --p->p_lock;
1035 return (1);
1036 }
1037
1038 /*
1039 * fault the data page
1040 */
1041 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1042
1043 --p->p_lock;
1044
1045 if (rv != KERN_SUCCESS)
1046 return 1;
1047
1048 return (0);
1049 }
1050
1051 /*
1052 * syscall2 - MP aware system call request C handler
1053 *
1054 * A system call is essentially treated as a trap except that the
1055 * MP lock is not held on entry or return. We are responsible for
1056 * obtaining the MP lock if necessary and for handling ASTs
1057 * (e.g. a task switch) prior to return.
1058 *
1059 * In general, only simple access and manipulation of curproc and
1060 * the current stack is allowed without having to hold MP lock.
1061 */
1062 void
1063 syscall2(frame)
1064 struct trapframe frame;
1065 {
1066 caddr_t params;
1067 int i;
1068 struct sysent *callp;
1069 struct proc *p = curproc;
1070 register_t orig_tf_eflags;
1071 u_quad_t sticks;
1072 int error;
1073 int narg;
1074 int args[8];
1075 int have_mplock = 0;
1076 u_int code;
1077
1078 #ifdef DIAGNOSTIC
1079 if (ISPL(frame.tf_cs) != SEL_UPL) {
1080 get_mplock();
1081 panic("syscall");
1082 /* NOT REACHED */
1083 }
1084 #endif
1085
1086 /*
1087 * handle atomicy by looping since interrupts are enabled and the
1088 * MP lock is not held.
1089 */
1090 sticks = ((volatile struct proc *)p)->p_sticks;
1091 while (sticks != ((volatile struct proc *)p)->p_sticks)
1092 sticks = ((volatile struct proc *)p)->p_sticks;
1093
1094 p->p_md.md_regs = &frame;
1095 params = (caddr_t)frame.tf_esp + sizeof(int);
1096 code = frame.tf_eax;
1097 orig_tf_eflags = frame.tf_eflags;
1098
1099 if (p->p_sysent->sv_prepsyscall) {
1100 /*
1101 * The prep code is not MP aware.
1102 */
1103 get_mplock();
1104 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
1105 rel_mplock();
1106 } else {
1107 /*
1108 * Need to check if this is a 32 bit or 64 bit syscall.
1109 * fuword is MP aware.
1110 */
1111 if (code == SYS_syscall) {
1112 /*
1113 * Code is first argument, followed by actual args.
1114 */
1115 code = fuword(params);
1116 params += sizeof(int);
1117 } else if (code == SYS___syscall) {
1118 /*
1119 * Like syscall, but code is a quad, so as to maintain
1120 * quad alignment for the rest of the arguments.
1121 */
1122 code = fuword(params);
1123 params += sizeof(quad_t);
1124 }
1125 }
1126
1127 if (p->p_sysent->sv_mask)
1128 code &= p->p_sysent->sv_mask;
1129
1130 if (code >= p->p_sysent->sv_size)
1131 callp = &p->p_sysent->sv_table[0];
1132 else
1133 callp = &p->p_sysent->sv_table[code];
1134
1135 narg = callp->sy_narg & SYF_ARGMASK;
1136
1137 /*
1138 * copyin is MP aware, but the tracing code is not
1139 */
1140 if (params && (i = narg * sizeof(int)) &&
1141 (error = copyin(params, (caddr_t)args, (u_int)i))) {
1142 get_mplock();
1143 have_mplock = 1;
1144 #ifdef KTRACE
1145 if (KTRPOINT(p, KTR_SYSCALL))
1146 ktrsyscall(p->p_tracep, code, narg, args);
1147 #endif
1148 goto bad;
1149 }
1150
1151 /*
1152 * Try to run the syscall without the MP lock if the syscall
1153 * is MP safe. We have to obtain the MP lock no matter what if
1154 * we are ktracing
1155 */
1156 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1157 get_mplock();
1158 have_mplock = 1;
1159 }
1160
1161 #ifdef KTRACE
1162 if (KTRPOINT(p, KTR_SYSCALL)) {
1163 if (have_mplock == 0) {
1164 get_mplock();
1165 have_mplock = 1;
1166 }
1167 ktrsyscall(p->p_tracep, code, narg, args);
1168 }
1169 #endif
1170 p->p_retval[0] = 0;
1171 p->p_retval[1] = frame.tf_edx;
1172
1173 STOPEVENT(p, S_SCE, narg); /* MP aware */
1174
1175 error = (*callp->sy_call)(p, args);
1176
1177 /*
1178 * MP SAFE (we may or may not have the MP lock at this point)
1179 */
1180 switch (error) {
1181 case 0:
1182 /*
1183 * Reinitialize proc pointer `p' as it may be different
1184 * if this is a child returning from fork syscall.
1185 */
1186 p = curproc;
1187 frame.tf_eax = p->p_retval[0];
1188 frame.tf_edx = p->p_retval[1];
1189 frame.tf_eflags &= ~PSL_C;
1190 break;
1191
1192 case ERESTART:
1193 /*
1194 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1195 * int 0x80 is 2 bytes. We saved this in tf_err.
1196 */
1197 frame.tf_eip -= frame.tf_err;
1198 break;
1199
1200 case EJUSTRETURN:
1201 break;
1202
1203 default:
1204 bad:
1205 if (p->p_sysent->sv_errsize) {
1206 if (error >= p->p_sysent->sv_errsize)
1207 error = -1; /* XXX */
1208 else
1209 error = p->p_sysent->sv_errtbl[error];
1210 }
1211 frame.tf_eax = error;
1212 frame.tf_eflags |= PSL_C;
1213 break;
1214 }
1215
1216 /*
1217 * Traced syscall. trapsignal() is not MP aware.
1218 */
1219 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1220 if (have_mplock == 0) {
1221 get_mplock();
1222 have_mplock = 1;
1223 }
1224 frame.tf_eflags &= ~PSL_T;
1225 trapsignal(p, SIGTRAP, 0);
1226 }
1227
1228 /*
1229 * Handle reschedule and other end-of-syscall issues
1230 */
1231 have_mplock = userret(p, &frame, sticks, have_mplock);
1232
1233 #ifdef KTRACE
1234 if (KTRPOINT(p, KTR_SYSRET)) {
1235 if (have_mplock == 0) {
1236 get_mplock();
1237 have_mplock = 1;
1238 }
1239 ktrsysret(p->p_tracep, code, error, p->p_retval[0]);
1240 }
1241 #endif
1242
1243 /*
1244 * This works because errno is findable through the
1245 * register set. If we ever support an emulation where this
1246 * is not the case, this code will need to be revisited.
1247 */
1248 STOPEVENT(p, S_SCX, code);
1249
1250 /*
1251 * Release the MP lock if we had to get it
1252 */
1253 if (have_mplock)
1254 rel_mplock();
1255 }
1256
1257 /*
1258 * Simplified back end of syscall(), used when returning from fork()
1259 * directly into user mode. MP lock is held on entry and should be
1260 * held on return.
1261 */
1262 void
1263 fork_return(p, frame)
1264 struct proc *p;
1265 struct trapframe frame;
1266 {
1267 frame.tf_eax = 0; /* Child returns zero */
1268 frame.tf_eflags &= ~PSL_C; /* success */
1269 frame.tf_edx = 1;
1270
1271 userret(p, &frame, 0, 1);
1272 #ifdef KTRACE
1273 if (KTRPOINT(p, KTR_SYSRET))
1274 ktrsysret(p->p_tracep, SYS_fork, 0, 0);
1275 #endif
1276 }
Cache object: ba2fc2526aa319c03adedbf5235ad9ce
|