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