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