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