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