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