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