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