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