subr_syscall.c revision 34924
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 * $Id: trap.c,v 1.123 1998/03/23 19:52:37 jlemon Exp $ 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_trap.h" 49#include "opt_vm86.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/sysent.h> 60#include <sys/uio.h> 61#include <sys/vmmeter.h> 62#ifdef KTRACE 63#include <sys/ktrace.h> 64#endif 65 66#include <vm/vm.h> 67#include <vm/vm_param.h> 68#include <vm/vm_prot.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#ifdef VM86 93#include <machine/vm86.h> 94#endif 95 96#include "isa.h" 97#include "npx.h" 98 99extern struct i386tss common_tss; 100 101int (*pmath_emulate) __P((struct trapframe *)); 102 103extern void trap __P((struct trapframe frame)); 104extern int trapwrite __P((unsigned addr)); 105extern void syscall __P((struct trapframe frame)); 106 107static int trap_pfault __P((struct trapframe *, int)); 108static void trap_fatal __P((struct trapframe *)); 109void dblfault_handler __P((void)); 110 111extern inthand_t IDTVEC(syscall); 112 113#define MAX_TRAP_MSG 28 114static char *trap_msg[] = { 115 "", /* 0 unused */ 116 "privileged instruction fault", /* 1 T_PRIVINFLT */ 117 "", /* 2 unused */ 118 "breakpoint instruction fault", /* 3 T_BPTFLT */ 119 "", /* 4 unused */ 120 "", /* 5 unused */ 121 "arithmetic trap", /* 6 T_ARITHTRAP */ 122 "system forced exception", /* 7 T_ASTFLT */ 123 "", /* 8 unused */ 124 "general protection fault", /* 9 T_PROTFLT */ 125 "trace trap", /* 10 T_TRCTRAP */ 126 "", /* 11 unused */ 127 "page fault", /* 12 T_PAGEFLT */ 128 "", /* 13 unused */ 129 "alignment fault", /* 14 T_ALIGNFLT */ 130 "", /* 15 unused */ 131 "", /* 16 unused */ 132 "", /* 17 unused */ 133 "integer divide fault", /* 18 T_DIVIDE */ 134 "non-maskable interrupt trap", /* 19 T_NMI */ 135 "overflow trap", /* 20 T_OFLOW */ 136 "FPU bounds check fault", /* 21 T_BOUND */ 137 "FPU device not available", /* 22 T_DNA */ 138 "double fault", /* 23 T_DOUBLEFLT */ 139 "FPU operand fetch fault", /* 24 T_FPOPFLT */ 140 "invalid TSS fault", /* 25 T_TSSFLT */ 141 "segment not present fault", /* 26 T_SEGNPFLT */ 142 "stack fault", /* 27 T_STKFLT */ 143 "machine check trap", /* 28 T_MCHK */ 144}; 145 146static void userret __P((struct proc *p, struct trapframe *frame, 147 u_quad_t oticks)); 148 149#if defined(I586_CPU) && !defined(NO_F00F_HACK) 150extern struct gate_descriptor *t_idt; 151extern int has_f00f_bug; 152#endif 153 154static inline void 155userret(p, frame, oticks) 156 struct proc *p; 157 struct trapframe *frame; 158 u_quad_t oticks; 159{ 160 int sig, s; 161 162 while ((sig = CURSIG(p)) != 0) 163 postsig(sig); 164 165#if 0 166 if (!want_resched && 167 (p->p_priority <= p->p_usrpri) && 168 (p->p_rtprio.type == RTP_PRIO_NORMAL)) { 169 int newpriority; 170 p->p_estcpu += 1; 171 newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice; 172 newpriority = min(newpriority, MAXPRI); 173 p->p_usrpri = newpriority; 174 } 175#endif 176 177 p->p_priority = p->p_usrpri; 178 if (want_resched) { 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 s = splhigh(); 188 setrunqueue(p); 189 p->p_stats->p_ru.ru_nivcsw++; 190 mi_switch(); 191 splx(s); 192 while ((sig = CURSIG(p)) != 0) 193 postsig(sig); 194 } 195 /* 196 * Charge system time if profiling. 197 */ 198 if (p->p_flag & P_PROFIL) 199 addupc_task(p, frame->tf_eip, 200 (u_int)(p->p_sticks - oticks) * psratio); 201 202 curpriority = p->p_priority; 203} 204 205/* 206 * Exception, fault, and trap interface to the FreeBSD kernel. 207 * This common code is called from assembly language IDT gate entry 208 * routines that prepare a suitable stack frame, and restore this 209 * frame after the exception has been processed. 210 */ 211 212void 213trap(frame) 214 struct trapframe frame; 215{ 216 struct proc *p = curproc; 217 u_quad_t sticks = 0; 218 int i = 0, ucode = 0, type, code; 219#ifdef DEBUG 220 u_long eva; 221#endif 222 223#if defined(I586_CPU) && !defined(NO_F00F_HACK) 224restart: 225#endif 226 type = frame.tf_trapno; 227 code = frame.tf_err; 228 229#ifdef VM86 230 if (in_vm86call) { 231 if (frame.tf_eflags & PSL_VM && 232 (type == T_PROTFLT || type == T_STKFLT)) { 233 i = vm86_emulate((struct vm86frame *)&frame); 234 if (i != 0) 235 /* 236 * returns to original process 237 */ 238 vm86_trap((struct vm86frame *)&frame); 239 return; 240 } 241 switch (type) { 242 /* 243 * these traps want either a process context, or 244 * assume a normal userspace trap. 245 */ 246 case T_PROTFLT: 247 case T_SEGNPFLT: 248 trap_fatal(&frame); 249 return; 250 case T_TRCTRAP: 251 type = T_BPTFLT; /* kernel breakpoint */ 252 /* FALL THROUGH */ 253 } 254 goto kernel_trap; /* normal kernel trap handling */ 255 } 256#endif 257 258 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) { 259 /* user trap */ 260 261 sticks = p->p_sticks; 262 p->p_md.md_regs = &frame; 263 264 switch (type) { 265 case T_PRIVINFLT: /* privileged instruction fault */ 266 ucode = type; 267 i = SIGILL; 268 break; 269 270 case T_BPTFLT: /* bpt instruction fault */ 271 case T_TRCTRAP: /* trace trap */ 272 frame.tf_eflags &= ~PSL_T; 273 i = SIGTRAP; 274 break; 275 276 case T_ARITHTRAP: /* arithmetic trap */ 277 ucode = code; 278 i = SIGFPE; 279 break; 280 281 case T_ASTFLT: /* Allow process switch */ 282 astoff(); 283 cnt.v_soft++; 284 if (p->p_flag & P_OWEUPC) { 285 p->p_flag &= ~P_OWEUPC; 286 addupc_task(p, p->p_stats->p_prof.pr_addr, 287 p->p_stats->p_prof.pr_ticks); 288 } 289 goto out; 290 291 /* 292 * The following two traps can happen in 293 * vm86 mode, and, if so, we want to handle 294 * them specially. 295 */ 296 case T_PROTFLT: /* general protection fault */ 297 case T_STKFLT: /* stack fault */ 298#ifdef VM86 299 if (frame.tf_eflags & PSL_VM) { 300 i = vm86_emulate((struct vm86frame *)&frame); 301 if (i == 0) 302 goto out; 303 break; 304 } 305#endif /* VM86 */ 306 /* FALL THROUGH */ 307 308 case T_SEGNPFLT: /* segment not present fault */ 309 case T_TSSFLT: /* invalid TSS fault */ 310 case T_DOUBLEFLT: /* double fault */ 311 default: 312 ucode = code + BUS_SEGM_FAULT ; 313 i = SIGBUS; 314 break; 315 316 case T_PAGEFLT: /* page fault */ 317 i = trap_pfault(&frame, TRUE); 318 if (i == -1) 319 return; 320#if defined(I586_CPU) && !defined(NO_F00F_HACK) 321 if (i == -2) 322 goto restart; 323#endif 324 if (i == 0) 325 goto out; 326 327 ucode = T_PAGEFLT; 328 break; 329 330 case T_DIVIDE: /* integer divide fault */ 331 ucode = FPE_INTDIV_TRAP; 332 i = SIGFPE; 333 break; 334 335#if NISA > 0 336 case T_NMI: 337#ifdef POWERFAIL_NMI 338 goto handle_powerfail; 339#else /* !POWERFAIL_NMI */ 340#ifdef DDB 341 /* NMI can be hooked up to a pushbutton for debugging */ 342 printf ("NMI ... going to debugger\n"); 343 if (kdb_trap (type, 0, &frame)) 344 return; 345#endif /* DDB */ 346 /* machine/parity/power fail/"kitchen sink" faults */ 347 if (isa_nmi(code) == 0) return; 348 panic("NMI indicates hardware failure"); 349#endif /* POWERFAIL_NMI */ 350#endif /* NISA > 0 */ 351 352 case T_OFLOW: /* integer overflow fault */ 353 ucode = FPE_INTOVF_TRAP; 354 i = SIGFPE; 355 break; 356 357 case T_BOUND: /* bounds check fault */ 358 ucode = FPE_SUBRNG_TRAP; 359 i = SIGFPE; 360 break; 361 362 case T_DNA: 363#if NNPX > 0 364 /* if a transparent fault (due to context switch "late") */ 365 if (npxdna()) 366 return; 367#endif 368 if (!pmath_emulate) { 369 i = SIGFPE; 370 ucode = FPE_FPU_NP_TRAP; 371 break; 372 } 373 i = (*pmath_emulate)(&frame); 374 if (i == 0) { 375 if (!(frame.tf_eflags & PSL_T)) 376 return; 377 frame.tf_eflags &= ~PSL_T; 378 i = SIGTRAP; 379 } 380 /* else ucode = emulator_only_knows() XXX */ 381 break; 382 383 case T_FPOPFLT: /* FPU operand fetch fault */ 384 ucode = T_FPOPFLT; 385 i = SIGILL; 386 break; 387 } 388 } else { 389#ifdef VM86 390kernel_trap: 391#endif 392 /* kernel trap */ 393 394 switch (type) { 395 case T_PAGEFLT: /* page fault */ 396 (void) trap_pfault(&frame, FALSE); 397 return; 398 399 case T_DNA: 400#if NNPX > 0 401 /* 402 * The kernel is apparently using npx for copying. 403 * XXX this should be fatal unless the kernel has 404 * registered such use. 405 */ 406 if (npxdna()) 407 return; 408#endif 409 break; 410 411 case T_PROTFLT: /* general protection fault */ 412 case T_SEGNPFLT: /* segment not present fault */ 413 /* 414 * Invalid segment selectors and out of bounds 415 * %eip's and %esp's can be set up in user mode. 416 * This causes a fault in kernel mode when the 417 * kernel tries to return to user mode. We want 418 * to get this fault so that we can fix the 419 * problem here and not have to check all the 420 * selectors and pointers when the user changes 421 * them. 422 */ 423#define MAYBE_DORETI_FAULT(where, whereto) \ 424 do { \ 425 if (frame.tf_eip == (int)where) { \ 426 frame.tf_eip = (int)whereto; \ 427 return; \ 428 } \ 429 } while (0) 430 431 if (intr_nesting_level == 0) { 432 /* 433 * Invalid %fs's and %gs's can be created using 434 * procfs or PT_SETREGS or by invalidating the 435 * underlying LDT entry. This causes a fault 436 * in kernel mode when the kernel attempts to 437 * switch contexts. Lose the bad context 438 * (XXX) so that we can continue, and generate 439 * a signal. 440 */ 441 if (frame.tf_eip == (int)cpu_switch_load_fs) { 442 curpcb->pcb_fs = 0; 443 psignal(p, SIGBUS); 444 return; 445 } 446 if (frame.tf_eip == (int)cpu_switch_load_gs) { 447 curpcb->pcb_gs = 0; 448 psignal(p, SIGBUS); 449 return; 450 } 451 MAYBE_DORETI_FAULT(doreti_iret, 452 doreti_iret_fault); 453 MAYBE_DORETI_FAULT(doreti_popl_ds, 454 doreti_popl_ds_fault); 455 MAYBE_DORETI_FAULT(doreti_popl_es, 456 doreti_popl_es_fault); 457 if (curpcb && curpcb->pcb_onfault) { 458 frame.tf_eip = (int)curpcb->pcb_onfault; 459 return; 460 } 461 } 462 break; 463 464 case T_TSSFLT: 465 /* 466 * PSL_NT can be set in user mode and isn't cleared 467 * automatically when the kernel is entered. This 468 * causes a TSS fault when the kernel attempts to 469 * `iret' because the TSS link is uninitialized. We 470 * want to get this fault so that we can fix the 471 * problem here and not every time the kernel is 472 * entered. 473 */ 474 if (frame.tf_eflags & PSL_NT) { 475 frame.tf_eflags &= ~PSL_NT; 476 return; 477 } 478 break; 479 480 case T_TRCTRAP: /* trace trap */ 481 if (frame.tf_eip == (int)IDTVEC(syscall)) { 482 /* 483 * We've just entered system mode via the 484 * syscall lcall. Continue single stepping 485 * silently until the syscall handler has 486 * saved the flags. 487 */ 488 return; 489 } 490 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) { 491 /* 492 * The syscall handler has now saved the 493 * flags. Stop single stepping it. 494 */ 495 frame.tf_eflags &= ~PSL_T; 496 return; 497 } 498 /* 499 * Fall through. 500 */ 501 case T_BPTFLT: 502 /* 503 * If DDB is enabled, let it handle the debugger trap. 504 * Otherwise, debugger traps "can't happen". 505 */ 506#ifdef DDB 507 if (kdb_trap (type, 0, &frame)) 508 return; 509#endif 510 break; 511 512#if NISA > 0 513 case T_NMI: 514#ifdef POWERFAIL_NMI 515#ifndef TIMER_FREQ 516# define TIMER_FREQ 1193182 517#endif 518 handle_powerfail: 519 { 520 static unsigned lastalert = 0; 521 522 if(time.tv_sec - lastalert > 10) 523 { 524 log(LOG_WARNING, "NMI: power fail\n"); 525 sysbeep(TIMER_FREQ/880, hz); 526 lastalert = time.tv_sec; 527 } 528 return; 529 } 530#else /* !POWERFAIL_NMI */ 531#ifdef DDB 532 /* NMI can be hooked up to a pushbutton for debugging */ 533 printf ("NMI ... going to debugger\n"); 534 if (kdb_trap (type, 0, &frame)) 535 return; 536#endif /* DDB */ 537 /* machine/parity/power fail/"kitchen sink" faults */ 538 if (isa_nmi(code) == 0) return; 539 /* FALL THROUGH */ 540#endif /* POWERFAIL_NMI */ 541#endif /* NISA > 0 */ 542 } 543 544 trap_fatal(&frame); 545 return; 546 } 547 548 trapsignal(p, i, ucode); 549 550#ifdef DEBUG 551 eva = rcr2(); 552 if (type <= MAX_TRAP_MSG) { 553 uprintf("fatal process exception: %s", 554 trap_msg[type]); 555 if ((type == T_PAGEFLT) || (type == T_PROTFLT)) 556 uprintf(", fault VA = 0x%x", eva); 557 uprintf("\n"); 558 } 559#endif 560 561out: 562 userret(p, &frame, sticks); 563} 564 565#ifdef notyet 566/* 567 * This version doesn't allow a page fault to user space while 568 * in the kernel. The rest of the kernel needs to be made "safe" 569 * before this can be used. I think the only things remaining 570 * to be made safe are the iBCS2 code and the process tracing/ 571 * debugging code. 572 */ 573static int 574trap_pfault(frame, usermode) 575 struct trapframe *frame; 576 int usermode; 577{ 578 vm_offset_t va; 579 struct vmspace *vm = NULL; 580 vm_map_t map = 0; 581 int rv = 0; 582 vm_prot_t ftype; 583 int eva; 584 struct proc *p = curproc; 585 586 if (frame->tf_err & PGEX_W) 587 ftype = VM_PROT_READ | VM_PROT_WRITE; 588 else 589 ftype = VM_PROT_READ; 590 591 eva = rcr2(); 592 va = trunc_page((vm_offset_t)eva); 593 594 if (va < VM_MIN_KERNEL_ADDRESS) { 595 vm_offset_t v; 596 vm_page_t mpte; 597 598 if (p == NULL || 599 (!usermode && va < VM_MAXUSER_ADDRESS && 600 (intr_nesting_level != 0 || curpcb == NULL || 601 curpcb->pcb_onfault == NULL))) { 602 trap_fatal(frame); 603 return (-1); 604 } 605 606 /* 607 * This is a fault on non-kernel virtual memory. 608 * vm is initialized above to NULL. If curproc is NULL 609 * or curproc->p_vmspace is NULL the fault is fatal. 610 */ 611 vm = p->p_vmspace; 612 if (vm == NULL) 613 goto nogo; 614 615 map = &vm->vm_map; 616 617 /* 618 * Keep swapout from messing with us during this 619 * critical time. 620 */ 621 ++p->p_lock; 622 623 /* 624 * Grow the stack if necessary 625 */ 626 if ((caddr_t)va > vm->vm_maxsaddr 627 && (caddr_t)va < (caddr_t)USRSTACK) { 628 if (!grow(p, va)) { 629 rv = KERN_FAILURE; 630 --p->p_lock; 631 goto nogo; 632 } 633 } 634 635 /* Fault in the user page: */ 636 rv = vm_fault(map, va, ftype, 637 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY : 0); 638 639 --p->p_lock; 640 } else { 641 /* 642 * Don't allow user-mode faults in kernel address space. 643 */ 644 if (usermode) 645 goto nogo; 646 647 /* 648 * Since we know that kernel virtual address addresses 649 * always have pte pages mapped, we just have to fault 650 * the page. 651 */ 652 rv = vm_fault(kernel_map, va, ftype, FALSE); 653 } 654 655 if (rv == KERN_SUCCESS) 656 return (0); 657nogo: 658 if (!usermode) { 659 if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) { 660 frame->tf_eip = (int)curpcb->pcb_onfault; 661 return (0); 662 } 663 trap_fatal(frame); 664 return (-1); 665 } 666 667 /* kludge to pass faulting virtual address to sendsig */ 668 frame->tf_err = eva; 669 670 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 671} 672#endif 673 674int 675trap_pfault(frame, usermode) 676 struct trapframe *frame; 677 int usermode; 678{ 679 vm_offset_t va; 680 struct vmspace *vm = NULL; 681 vm_map_t map = 0; 682 int rv = 0; 683 vm_prot_t ftype; 684 int eva; 685 struct proc *p = curproc; 686 687 eva = rcr2(); 688 va = trunc_page((vm_offset_t)eva); 689 690 if (va >= KERNBASE) { 691 /* 692 * Don't allow user-mode faults in kernel address space. 693 * An exception: if the faulting address is the invalid 694 * instruction entry in the IDT, then the Intel Pentium 695 * F00F bug workaround was triggered, and we need to 696 * treat it is as an illegal instruction, and not a page 697 * fault. 698 */ 699#if defined(I586_CPU) && !defined(NO_F00F_HACK) 700 if ((eva == (unsigned int)&t_idt[6]) && has_f00f_bug) { 701 frame->tf_trapno = T_PRIVINFLT; 702 return -2; 703 } 704#endif 705 if (usermode) 706 goto nogo; 707 708 map = kernel_map; 709 } else { 710 /* 711 * This is a fault on non-kernel virtual memory. 712 * vm is initialized above to NULL. If curproc is NULL 713 * or curproc->p_vmspace is NULL the fault is fatal. 714 */ 715 if (p != NULL) 716 vm = p->p_vmspace; 717 718 if (vm == NULL) 719 goto nogo; 720 721 map = &vm->vm_map; 722 } 723 724 if (frame->tf_err & PGEX_W) 725 ftype = VM_PROT_READ | VM_PROT_WRITE; 726 else 727 ftype = VM_PROT_READ; 728 729 if (map != kernel_map) { 730 /* 731 * Keep swapout from messing with us during this 732 * critical time. 733 */ 734 ++p->p_lock; 735 736 /* 737 * Grow the stack if necessary 738 */ 739 if ((caddr_t)va > vm->vm_maxsaddr 740 && (caddr_t)va < (caddr_t)USRSTACK) { 741 if (!grow(p, va)) { 742 rv = KERN_FAILURE; 743 --p->p_lock; 744 goto nogo; 745 } 746 } 747 748 /* Fault in the user page: */ 749 rv = vm_fault(map, va, ftype, 750 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY : 0); 751 752 --p->p_lock; 753 } else { 754 /* 755 * Don't have to worry about process locking or stacks in the kernel. 756 */ 757 rv = vm_fault(map, va, ftype, FALSE); 758 } 759 760 if (rv == KERN_SUCCESS) 761 return (0); 762nogo: 763 if (!usermode) { 764 if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) { 765 frame->tf_eip = (int)curpcb->pcb_onfault; 766 return (0); 767 } 768 trap_fatal(frame); 769 return (-1); 770 } 771 772 /* kludge to pass faulting virtual address to sendsig */ 773 frame->tf_err = eva; 774 775 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 776} 777 778static void 779trap_fatal(frame) 780 struct trapframe *frame; 781{ 782 int code, type, eva, ss, esp; 783 struct soft_segment_descriptor softseg; 784 785 code = frame->tf_err; 786 type = frame->tf_trapno; 787 eva = rcr2(); 788 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg); 789 790 if (type <= MAX_TRAP_MSG) 791 printf("\n\nFatal trap %d: %s while in %s mode\n", 792 type, trap_msg[type], 793 frame->tf_eflags & PSL_VM ? "vm86" : 794 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel"); 795#ifdef SMP 796 /* three seperate prints in case of a trap on an unmapped page */ 797 printf("mp_lock = %08x; ", mp_lock); 798 printf("cpuid = %d; ", cpuid); 799 printf("lapic.id = %08x\n", lapic.id); 800#endif 801 if (type == T_PAGEFLT) { 802 printf("fault virtual address = 0x%x\n", eva); 803 printf("fault code = %s %s, %s\n", 804 code & PGEX_U ? "user" : "supervisor", 805 code & PGEX_W ? "write" : "read", 806 code & PGEX_P ? "protection violation" : "page not present"); 807 } 808 printf("instruction pointer = 0x%x:0x%x\n", 809 frame->tf_cs & 0xffff, frame->tf_eip); 810 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) { 811 ss = frame->tf_ss & 0xffff; 812 esp = frame->tf_esp; 813 } else { 814 ss = GSEL(GDATA_SEL, SEL_KPL); 815 esp = (int)&frame->tf_esp; 816 } 817 printf("stack pointer = 0x%x:0x%x\n", ss, esp); 818 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp); 819 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n", 820 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type); 821 printf(" = DPL %d, pres %d, def32 %d, gran %d\n", 822 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32, 823 softseg.ssd_gran); 824 printf("processor eflags = "); 825 if (frame->tf_eflags & PSL_T) 826 printf("trace trap, "); 827 if (frame->tf_eflags & PSL_I) 828 printf("interrupt enabled, "); 829 if (frame->tf_eflags & PSL_NT) 830 printf("nested task, "); 831 if (frame->tf_eflags & PSL_RF) 832 printf("resume, "); 833 if (frame->tf_eflags & PSL_VM) 834 printf("vm86, "); 835 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12); 836 printf("current process = "); 837 if (curproc) { 838 printf("%lu (%s)\n", 839 (u_long)curproc->p_pid, curproc->p_comm ? 840 curproc->p_comm : ""); 841 } else { 842 printf("Idle\n"); 843 } 844 printf("interrupt mask = "); 845 if ((cpl & net_imask) == net_imask) 846 printf("net "); 847 if ((cpl & tty_imask) == tty_imask) 848 printf("tty "); 849 if ((cpl & bio_imask) == bio_imask) 850 printf("bio "); 851 if ((cpl & cam_imask) == cam_imask) 852 printf("cam "); 853 if (cpl == 0) 854 printf("none"); 855#ifdef SMP 856/** 857 * XXX FIXME: 858 * we probably SHOULD have stopped the other CPUs before now! 859 * another CPU COULD have been touching cpl at this moment... 860 */ 861 printf(" <- SMP: XXX"); 862#endif 863 printf("\n"); 864 865#ifdef KDB 866 if (kdb_trap(&psl)) 867 return; 868#endif 869#ifdef DDB 870 if (kdb_trap (type, 0, frame)) 871 return; 872#endif 873 printf("trap number = %d\n", type); 874 if (type <= MAX_TRAP_MSG) 875 panic(trap_msg[type]); 876 else 877 panic("unknown/reserved trap"); 878} 879 880/* 881 * Double fault handler. Called when a fault occurs while writing 882 * a frame for a trap/exception onto the stack. This usually occurs 883 * when the stack overflows (such is the case with infinite recursion, 884 * for example). 885 * 886 * XXX Note that the current PTD gets replaced by IdlePTD when the 887 * task switch occurs. This means that the stack that was active at 888 * the time of the double fault is not available at <kstack> unless 889 * the machine was idle when the double fault occurred. The downside 890 * of this is that "trace <ebp>" in ddb won't work. 891 */ 892void 893dblfault_handler() 894{ 895 printf("\nFatal double fault:\n"); 896 printf("eip = 0x%x\n", common_tss.tss_eip); 897 printf("esp = 0x%x\n", common_tss.tss_esp); 898 printf("ebp = 0x%x\n", common_tss.tss_ebp); 899#ifdef SMP 900 /* three seperate prints in case of a trap on an unmapped page */ 901 printf("mp_lock = %08x; ", mp_lock); 902 printf("cpuid = %d; ", cpuid); 903 printf("lapic.id = %08x\n", lapic.id); 904#endif 905 panic("double fault"); 906} 907 908/* 909 * Compensate for 386 brain damage (missing URKR). 910 * This is a little simpler than the pagefault handler in trap() because 911 * it the page tables have already been faulted in and high addresses 912 * are thrown out early for other reasons. 913 */ 914int trapwrite(addr) 915 unsigned addr; 916{ 917 struct proc *p; 918 vm_offset_t va; 919 struct vmspace *vm; 920 int rv; 921 922 va = trunc_page((vm_offset_t)addr); 923 /* 924 * XXX - MAX is END. Changed > to >= for temp. fix. 925 */ 926 if (va >= VM_MAXUSER_ADDRESS) 927 return (1); 928 929 p = curproc; 930 vm = p->p_vmspace; 931 932 ++p->p_lock; 933 934 if ((caddr_t)va >= vm->vm_maxsaddr 935 && (caddr_t)va < (caddr_t)USRSTACK) { 936 if (!grow(p, va)) { 937 --p->p_lock; 938 return (1); 939 } 940 } 941 942 /* 943 * fault the data page 944 */ 945 rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, VM_FAULT_DIRTY); 946 947 --p->p_lock; 948 949 if (rv != KERN_SUCCESS) 950 return 1; 951 952 return (0); 953} 954 955/* 956 * System call request from POSIX system call gate interface to kernel. 957 * Like trap(), argument is call by reference. 958 */ 959void 960syscall(frame) 961 struct trapframe frame; 962{ 963 caddr_t params; 964 int i; 965 struct sysent *callp; 966 struct proc *p = curproc; 967 u_quad_t sticks; 968 int error; 969 int args[8]; 970 u_int code; 971 972#ifdef DIAGNOSTIC 973 if (ISPL(frame.tf_cs) != SEL_UPL) 974 panic("syscall"); 975#endif 976 sticks = p->p_sticks; 977 p->p_md.md_regs = &frame; 978 params = (caddr_t)frame.tf_esp + sizeof(int); 979 code = frame.tf_eax; 980 if (p->p_sysent->sv_prepsyscall) { 981 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms); 982 } else { 983 /* 984 * Need to check if this is a 32 bit or 64 bit syscall. 985 */ 986 if (code == SYS_syscall) { 987 /* 988 * Code is first argument, followed by actual args. 989 */ 990 code = fuword(params); 991 params += sizeof(int); 992 } else if (code == SYS___syscall) { 993 /* 994 * Like syscall, but code is a quad, so as to maintain 995 * quad alignment for the rest of the arguments. 996 */ 997 code = fuword(params); 998 params += sizeof(quad_t); 999 } 1000 } 1001 1002 if (p->p_sysent->sv_mask) 1003 code &= p->p_sysent->sv_mask; 1004 1005 if (code >= p->p_sysent->sv_size) 1006 callp = &p->p_sysent->sv_table[0]; 1007 else 1008 callp = &p->p_sysent->sv_table[code]; 1009 1010 if (params && (i = callp->sy_narg * sizeof(int)) && 1011 (error = copyin(params, (caddr_t)args, (u_int)i))) { 1012#ifdef KTRACE 1013 if (KTRPOINT(p, KTR_SYSCALL)) 1014 ktrsyscall(p->p_tracep, code, callp->sy_narg, args); 1015#endif 1016 goto bad; 1017 } 1018#ifdef KTRACE 1019 if (KTRPOINT(p, KTR_SYSCALL)) 1020 ktrsyscall(p->p_tracep, code, callp->sy_narg, args); 1021#endif 1022 p->p_retval[0] = 0; 1023 p->p_retval[1] = frame.tf_edx; 1024 1025 STOPEVENT(p, S_SCE, callp->sy_narg); 1026 1027 error = (*callp->sy_call)(p, args); 1028 1029 switch (error) { 1030 1031 case 0: 1032 /* 1033 * Reinitialize proc pointer `p' as it may be different 1034 * if this is a child returning from fork syscall. 1035 */ 1036 p = curproc; 1037 frame.tf_eax = p->p_retval[0]; 1038 frame.tf_edx = p->p_retval[1]; 1039 frame.tf_eflags &= ~PSL_C; 1040 break; 1041 1042 case ERESTART: 1043 /* 1044 * Reconstruct pc, assuming lcall $X,y is 7 bytes, 1045 * int 0x80 is 2 bytes. We saved this in tf_err. 1046 */ 1047 frame.tf_eip -= frame.tf_err; 1048 break; 1049 1050 case EJUSTRETURN: 1051 break; 1052 1053 default: 1054bad: 1055 if (p->p_sysent->sv_errsize) 1056 if (error >= p->p_sysent->sv_errsize) 1057 error = -1; /* XXX */ 1058 else 1059 error = p->p_sysent->sv_errtbl[error]; 1060 frame.tf_eax = error; 1061 frame.tf_eflags |= PSL_C; 1062 break; 1063 } 1064 1065 if ((frame.tf_eflags & PSL_T) && !(frame.tf_eflags & PSL_VM)) { 1066 /* Traced syscall. */ 1067 frame.tf_eflags &= ~PSL_T; 1068 trapsignal(p, SIGTRAP, 0); 1069 } 1070 1071 userret(p, &frame, sticks); 1072 1073#ifdef KTRACE 1074 if (KTRPOINT(p, KTR_SYSRET)) 1075 ktrsysret(p->p_tracep, code, error, p->p_retval[0]); 1076#endif 1077 1078 /* 1079 * This works because errno is findable through the 1080 * register set. If we ever support an emulation where this 1081 * is not the case, this code will need to be revisited. 1082 */ 1083 STOPEVENT(p, S_SCX, code); 1084 1085} 1086 1087/* 1088 * Simplified back end of syscall(), used when returning from fork() 1089 * directly into user mode. 1090 */ 1091void 1092fork_return(p, frame) 1093 struct proc *p; 1094 struct trapframe frame; 1095{ 1096 frame.tf_eax = 0; /* Child returns zero */ 1097 frame.tf_eflags &= ~PSL_C; /* success */ 1098 frame.tf_edx = 1; 1099 1100 userret(p, &frame, 0); 1101#ifdef KTRACE 1102 if (KTRPOINT(p, KTR_SYSRET)) 1103 ktrsysret(p->p_tracep, SYS_fork, 0, 0); 1104#endif 1105} 1106