subr_syscall.c revision 3258
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.34 1994/09/11 11:26:18 davidg Exp $ 39 */ 40 41/* 42 * 386 Trap and System call handling 43 */ 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/proc.h> 48#include <sys/user.h> 49#include <sys/acct.h> 50#include <sys/kernel.h> 51#include <sys/syscall.h> 52#include <sys/sysent.h> 53#ifdef KTRACE 54#include <sys/ktrace.h> 55#endif 56 57#include <vm/vm_param.h> 58#include <vm/pmap.h> 59#include <vm/vm_map.h> 60#include <vm/vm_page.h> 61 62#include <machine/cpu.h> 63#include <machine/psl.h> 64#include <machine/reg.h> 65#include <machine/trap.h> 66 67#include "isa.h" 68#include "npx.h" 69 70int trap_pfault __P((struct trapframe *, int)); 71void trap_fatal __P((struct trapframe *)); 72 73#define MAX_TRAP_MSG 27 74char *trap_msg[] = { 75 "reserved addressing fault", /* 0 T_RESADFLT */ 76 "privileged instruction fault", /* 1 T_PRIVINFLT */ 77 "reserved operand fault", /* 2 T_RESOPFLT */ 78 "breakpoint instruction fault", /* 3 T_BPTFLT */ 79 "", /* 4 unused */ 80 "system call trap", /* 5 T_SYSCALL */ 81 "arithmetic trap", /* 6 T_ARITHTRAP */ 82 "system forced exception", /* 7 T_ASTFLT */ 83 "segmentation (limit) fault", /* 8 T_SEGFLT */ 84 "general protection fault", /* 9 T_PROTFLT */ 85 "trace trap", /* 10 T_TRCTRAP */ 86 "", /* 11 unused */ 87 "page fault", /* 12 T_PAGEFLT */ 88 "page table fault", /* 13 T_TABLEFLT */ 89 "alignment fault", /* 14 T_ALIGNFLT */ 90 "kernel stack pointer not valid", /* 15 T_KSPNOTVAL */ 91 "bus error", /* 16 T_BUSERR */ 92 "kernel debugger fault", /* 17 T_KDBTRAP */ 93 "integer divide fault", /* 18 T_DIVIDE */ 94 "non-maskable interrupt trap", /* 19 T_NMI */ 95 "overflow trap", /* 20 T_OFLOW */ 96 "FPU bounds check fault", /* 21 T_BOUND */ 97 "FPU device not available", /* 22 T_DNA */ 98 "double fault", /* 23 T_DOUBLEFLT */ 99 "FPU operand fetch fault", /* 24 T_FPOPFLT */ 100 "invalid TSS fault", /* 25 T_TSSFLT */ 101 "segment not present fault", /* 26 T_SEGNPFLT */ 102 "stack fault", /* 27 T_STKFLT */ 103}; 104 105static inline void 106userret(p, frame, oticks) 107 struct proc *p; 108 struct trapframe *frame; 109 u_quad_t oticks; 110{ 111 int sig, s; 112 113 while (sig = CURSIG(p)) 114 postsig(sig); 115 p->p_priority = p->p_usrpri; 116 if (want_resched) { 117 /* 118 * Since we are curproc, clock will normally just change 119 * our priority without moving us from one queue to another 120 * (since the running process is not on a queue.) 121 * If that happened after we setrunqueue ourselves but before we 122 * mi_switch()'ed, we might not be on the queue indicated by 123 * our priority. 124 */ 125 s = splclock(); 126 setrunqueue(p); 127 p->p_stats->p_ru.ru_nivcsw++; 128 mi_switch(); 129 splx(s); 130 while (sig = CURSIG(p)) 131 postsig(sig); 132 } 133 if (p->p_stats->p_prof.pr_scale) { 134 u_quad_t ticks = p->p_sticks - oticks; 135 136 if (ticks) { 137#ifdef PROFTIMER 138 extern int profscale; 139 addupc(frame->tf_eip, &p->p_stats->p_prof, 140 ticks * profscale); 141#else 142 addupc(frame->tf_eip, &p->p_stats->p_prof, ticks); 143#endif 144 } 145 } 146 curpriority = p->p_priority; 147} 148 149/* 150 * trap(frame): 151 * Exception, fault, and trap interface to the FreeBSD kernel. 152 * This common code is called from assembly language IDT gate entry 153 * routines that prepare a suitable stack frame, and restore this 154 * frame after the exception has been processed. 155 */ 156 157/*ARGSUSED*/ 158void 159trap(frame) 160 struct trapframe frame; 161{ 162 struct proc *p = curproc; 163 u_quad_t sticks = 0; 164 int i = 0, ucode = 0, type, code, eva, fault_type; 165 166 frame.tf_eflags &= ~PSL_NT; /* clear nested trap XXX */ 167 type = frame.tf_trapno; 168 code = frame.tf_err; 169 170 if (ISPL(frame.tf_cs) == SEL_UPL) { 171 /* user trap */ 172 173 sticks = p->p_sticks; 174 p->p_md.md_regs = (int *)&frame; 175 176 switch (type) { 177 case T_RESADFLT: /* reserved addressing fault */ 178 case T_PRIVINFLT: /* privileged instruction fault */ 179 case T_RESOPFLT: /* reserved operand fault */ 180 ucode = type; 181 i = SIGILL; 182 break; 183 184 case T_BPTFLT: /* bpt instruction fault */ 185 case T_TRCTRAP: /* trace trap */ 186 frame.tf_eflags &= ~PSL_T; 187 i = SIGTRAP; 188 break; 189 190 case T_ARITHTRAP: /* arithmetic trap */ 191 ucode = code; 192 i = SIGFPE; 193 break; 194 195 case T_ASTFLT: /* Allow process switch */ 196 astoff(); 197 cnt.v_soft++; 198 if ((p->p_flag & P_OWEUPC) && p->p_stats->p_prof.pr_scale) { 199 addupc(frame.tf_eip, &p->p_stats->p_prof, 1); 200 p->p_flag &= ~P_OWEUPC; 201 } 202 goto out; 203 204 case T_PROTFLT: /* general protection fault */ 205 case T_SEGNPFLT: /* segment not present fault */ 206 case T_STKFLT: /* stack fault */ 207 ucode = code + BUS_SEGM_FAULT ; 208 i = SIGBUS; 209 break; 210 211 case T_PAGEFLT: /* page fault */ 212 i = trap_pfault(&frame, TRUE); 213 if (i == 0) 214 goto out; 215 216 ucode = T_PAGEFLT; 217 break; 218 219 case T_DIVIDE: /* integer divide fault */ 220 ucode = FPE_INTDIV_TRAP; 221 i = SIGFPE; 222 break; 223 224#if NISA > 0 225 case T_NMI: 226#ifdef DDB 227 /* NMI can be hooked up to a pushbutton for debugging */ 228 printf ("NMI ... going to debugger\n"); 229 if (kdb_trap (type, 0, &frame)) 230 return; 231#endif 232 /* machine/parity/power fail/"kitchen sink" faults */ 233 if (isa_nmi(code) == 0) return; 234 panic("NMI indicates hardware failure"); 235#endif 236 237 case T_OFLOW: /* integer overflow fault */ 238 ucode = FPE_INTOVF_TRAP; 239 i = SIGFPE; 240 break; 241 242 case T_BOUND: /* bounds check fault */ 243 ucode = FPE_SUBRNG_TRAP; 244 i = SIGFPE; 245 break; 246 247 case T_DNA: 248#if NNPX > 0 249 /* if a transparent fault (due to context switch "late") */ 250 if (npxdna()) 251 return; 252#endif /* NNPX > 0 */ 253 254#if defined(MATH_EMULATE) || defined(GPL_MATH_EMULATE) 255 i = math_emulate(&frame); 256 if (i == 0) return; 257#else /* MATH_EMULATE || GPL_MATH_EMULATE */ 258 panic("trap: math emulation necessary!"); 259#endif /* MATH_EMULATE || GPL_MATH_EMULATE */ 260 ucode = FPE_FPU_NP_TRAP; 261 break; 262 263 case T_FPOPFLT: /* FPU operand fetch fault */ 264 ucode = T_FPOPFLT; 265 i = SIGILL; 266 break; 267 268 default: 269 trap_fatal(&frame); 270 } 271 } else { 272 /* kernel trap */ 273 274 switch (type) { 275 case T_PAGEFLT: /* page fault */ 276 (void) trap_pfault(&frame, FALSE); 277 return; 278 279 case T_PROTFLT: /* general protection fault */ 280 case T_SEGNPFLT: /* segment not present fault */ 281 if (curpcb && curpcb->pcb_onfault) { 282 frame.tf_eip = (int)curpcb->pcb_onfault; 283 return; 284 } 285 break; 286 287#ifdef DDB 288 case T_BPTFLT: 289 case T_TRCTRAP: 290 if (kdb_trap (type, 0, &frame)) 291 return; 292 break; 293#else 294 case T_TRCTRAP: /* trace trap -- someone single stepping lcall's */ 295 /* Q: how do we turn it on again? */ 296 frame.tf_eflags &= ~PSL_T; 297 return; 298#endif 299 300#if NISA > 0 301 case T_NMI: 302#ifdef DDB 303 /* NMI can be hooked up to a pushbutton for debugging */ 304 printf ("NMI ... going to debugger\n"); 305 if (kdb_trap (type, 0, &frame)) 306 return; 307#endif 308 /* machine/parity/power fail/"kitchen sink" faults */ 309 if (isa_nmi(code) == 0) return; 310 /* FALL THROUGH */ 311#endif 312 } 313 314 trap_fatal(&frame); 315 } 316 317 trapsignal(p, i, ucode); 318 319#ifdef DIAGNOSTIC 320 eva = rcr2(); 321 if (type <= MAX_TRAP_MSG) { 322 uprintf("fatal process exception: %s", 323 trap_msg[type]); 324 if ((type == T_PAGEFLT) || (type == T_PROTFLT)) 325 uprintf(", fault VA = 0x%x", eva); 326 uprintf("\n"); 327 } 328#endif 329 330out: 331 userret(p, &frame, sticks); 332} 333 334int 335trap_pfault(frame, usermode) 336 struct trapframe *frame; 337 int usermode; 338{ 339 vm_offset_t va; 340 struct vmspace *vm = NULL; 341 vm_map_t map = 0; 342 int rv = 0, oldflags; 343 vm_prot_t ftype; 344 extern vm_map_t kernel_map; 345 int eva; 346 struct proc *p = curproc; 347 348 eva = rcr2(); 349 va = trunc_page((vm_offset_t)eva); 350 351 if (va >= KERNBASE) { 352 /* 353 * Don't allow user-mode faults in kernel address space. 354 */ 355 if (usermode) 356 goto nogo; 357 358 map = kernel_map; 359 } else { 360 /* 361 * This is a fault on non-kernel virtual memory. 362 * vm is initialized above to NULL. If curproc is NULL 363 * or curproc->p_vmspace is NULL the fault is fatal. 364 */ 365 if (p != NULL) 366 vm = p->p_vmspace; 367 368 if (vm == NULL) 369 goto nogo; 370 371 map = &vm->vm_map; 372 } 373 374 if (frame->tf_err & PGEX_W) 375 ftype = VM_PROT_READ | VM_PROT_WRITE; 376 else 377 ftype = VM_PROT_READ; 378 379 if (map != kernel_map) { 380 vm_offset_t pa; 381 vm_offset_t v = (vm_offset_t) vtopte(va); 382 vm_page_t ptepg; 383 384 /* 385 * Keep swapout from messing with us during this 386 * critical time. 387 */ 388 ++p->p_lock; 389 390 /* 391 * Grow the stack if necessary 392 */ 393 if ((caddr_t)va > vm->vm_maxsaddr 394 && (caddr_t)va < (caddr_t)USRSTACK) { 395 if (!grow(p, va)) { 396 rv = KERN_FAILURE; 397 --p->p_lock; 398 goto nogo; 399 } 400 } 401 402 /* 403 * Check if page table is mapped, if not, 404 * fault it first 405 */ 406 407 /* Fault the pte only if needed: */ 408 *(volatile char *)v += 0; 409 410 ptepg = (vm_page_t) pmap_pte_vm_page(vm_map_pmap(map), v); 411 if( ptepg->hold_count == 0) 412 ptepg->act_count += 3; 413 vm_page_hold(ptepg); 414 415 /* Fault in the user page: */ 416 rv = vm_fault(map, va, ftype, FALSE); 417 418 vm_page_unhold(ptepg); 419 420 /* 421 * page table pages don't need to be kept if they 422 * are not held 423 */ 424 if( ptepg->hold_count == 0 && ptepg->wire_count == 0) { 425 pmap_page_protect( VM_PAGE_TO_PHYS(ptepg), 426 VM_PROT_NONE); 427 vm_page_free(ptepg); 428 } 429 430 --p->p_lock; 431 } else { 432 /* 433 * Since we know that kernel virtual address addresses 434 * always have pte pages mapped, we just have to fault 435 * the page. 436 */ 437 rv = vm_fault(map, va, ftype, FALSE); 438 } 439 440 if (rv == KERN_SUCCESS) 441 return (0); 442nogo: 443 if (!usermode) { 444 if (curpcb && curpcb->pcb_onfault) { 445 frame->tf_eip = (int)curpcb->pcb_onfault; 446 return (0); 447 } 448 trap_fatal(frame); 449 } 450 451 /* kludge to pass faulting virtual address to sendsig */ 452 frame->tf_err = eva; 453 454 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 455} 456 457void 458trap_fatal(frame) 459 struct trapframe *frame; 460{ 461 int code, type, eva; 462 struct soft_segment_descriptor softseg; 463 464 code = frame->tf_err; 465 type = frame->tf_trapno; 466 eva = rcr2(); 467 sdtossd(gdt + IDXSEL(frame->tf_cs & 0xffff), &softseg); 468 469 if (type <= MAX_TRAP_MSG) 470 printf("\n\nFatal trap %d: %s while in %s mode\n", 471 type, trap_msg[type], 472 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel"); 473 if (type == T_PAGEFLT) { 474 printf("fault virtual address = 0x%x\n", eva); 475 printf("fault code = %s %s, %s\n", 476 code & PGEX_U ? "user" : "supervisor", 477 code & PGEX_W ? "write" : "read", 478 code & PGEX_P ? "protection violation" : "page not present"); 479 } 480 printf("instruction pointer = 0x%x:0x%x\n", frame->tf_cs & 0xffff, frame->tf_eip); 481 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n", 482 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type); 483 printf(" = DPL %d, pres %d, def32 %d, gran %d\n", 484 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32, softseg.ssd_gran); 485 printf("processor eflags = "); 486 if (frame->tf_eflags & PSL_T) 487 printf("trace/trap, "); 488 if (frame->tf_eflags & PSL_I) 489 printf("interrupt enabled, "); 490 if (frame->tf_eflags & PSL_NT) 491 printf("nested task, "); 492 if (frame->tf_eflags & PSL_RF) 493 printf("resume, "); 494 if (frame->tf_eflags & PSL_VM) 495 printf("vm86, "); 496 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12); 497 printf("current process = "); 498 if (curproc) { 499 printf("%d (%s)\n", 500 curproc->p_pid, curproc->p_comm ? 501 curproc->p_comm : ""); 502 } else { 503 printf("Idle\n"); 504 } 505 printf("interrupt mask = "); 506 if ((cpl & net_imask) == net_imask) 507 printf("net "); 508 if ((cpl & tty_imask) == tty_imask) 509 printf("tty "); 510 if ((cpl & bio_imask) == bio_imask) 511 printf("bio "); 512 if (cpl == 0) 513 printf("none"); 514 printf("\n"); 515 516#ifdef KDB 517 if (kdb_trap(&psl)) 518 return; 519#endif 520#ifdef DDB 521 if (kdb_trap (type, 0, frame)) 522 return; 523#endif 524 if (type <= MAX_TRAP_MSG) 525 panic(trap_msg[type]); 526 else 527 panic("unknown/reserved trap"); 528} 529 530/* 531 * Compensate for 386 brain damage (missing URKR). 532 * This is a little simpler than the pagefault handler in trap() because 533 * it the page tables have already been faulted in and high addresses 534 * are thrown out early for other reasons. 535 */ 536int trapwrite(addr) 537 unsigned addr; 538{ 539 struct proc *p; 540 vm_offset_t va, v; 541 struct vmspace *vm; 542 int oldflags; 543 int rv; 544 545 va = trunc_page((vm_offset_t)addr); 546 /* 547 * XXX - MAX is END. Changed > to >= for temp. fix. 548 */ 549 if (va >= VM_MAXUSER_ADDRESS) 550 return (1); 551 552 p = curproc; 553 vm = p->p_vmspace; 554 555 ++p->p_lock; 556 557 if ((caddr_t)va >= vm->vm_maxsaddr 558 && (caddr_t)va < (caddr_t)USRSTACK) { 559 if (!grow(p, va)) { 560 --p->p_lock; 561 return (1); 562 } 563 } 564 565 v = trunc_page(vtopte(va)); 566 567 /* 568 * wire the pte page 569 */ 570 if (va < USRSTACK) { 571 vm_map_pageable(&vm->vm_map, v, round_page(v+1), FALSE); 572 } 573 574 /* 575 * fault the data page 576 */ 577 rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, FALSE); 578 579 /* 580 * unwire the pte page 581 */ 582 if (va < USRSTACK) { 583 vm_map_pageable(&vm->vm_map, v, round_page(v+1), TRUE); 584 } 585 586 --p->p_lock; 587 588 if (rv != KERN_SUCCESS) 589 return 1; 590 591 return (0); 592} 593 594/* 595 * syscall(frame): 596 * System call request from POSIX system call gate interface to kernel. 597 * Like trap(), argument is call by reference. 598 */ 599/*ARGSUSED*/ 600void 601syscall(frame) 602 struct trapframe frame; 603{ 604 caddr_t params; 605 int i; 606 struct sysent *callp; 607 struct proc *p = curproc; 608 u_quad_t sticks; 609 int error, opc; 610 int args[8], rval[2]; 611 u_int code; 612 613 sticks = p->p_sticks; 614 if (ISPL(frame.tf_cs) != SEL_UPL) 615 panic("syscall"); 616 617 code = frame.tf_eax; 618 p->p_md.md_regs = (int *)&frame; 619 params = (caddr_t)frame.tf_esp + sizeof (int) ; 620 621 /* 622 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always. 623 */ 624 opc = frame.tf_eip - 7; 625 /* 626 * Need to check if this is a 32 bit or 64 bit syscall. 627 */ 628 if (code == SYS_syscall) { 629 /* 630 * Code is first argument, followed by actual args. 631 */ 632 code = fuword(params); 633 params += sizeof (int); 634 } else if (code == SYS___syscall) { 635 /* 636 * Like syscall, but code is a quad, so as to maintain 637 * quad alignment for the rest of the arguments. 638 */ 639 code = fuword(params + _QUAD_LOWWORD * sizeof(int)); 640 params += sizeof(quad_t); 641 } 642 643 if (p->p_sysent->sv_mask) 644 code = code & p->p_sysent->sv_mask; 645 646 if (code >= p->p_sysent->sv_size) 647 callp = &p->p_sysent->sv_table[0]; 648 else 649 callp = &p->p_sysent->sv_table[code]; 650 651 if ((i = callp->sy_narg * sizeof (int)) && 652 (error = copyin(params, (caddr_t)args, (u_int)i))) { 653#ifdef KTRACE 654 if (KTRPOINT(p, KTR_SYSCALL)) 655 ktrsyscall(p->p_tracep, code, callp->sy_narg, args); 656#endif 657 goto bad; 658 } 659#ifdef KTRACE 660 if (KTRPOINT(p, KTR_SYSCALL)) 661 ktrsyscall(p->p_tracep, code, callp->sy_narg, args); 662#endif 663 rval[0] = 0; 664 rval[1] = frame.tf_edx; 665 666 error = (*callp->sy_call)(p, args, rval); 667 668 switch (error) { 669 670 case 0: 671 /* 672 * Reinitialize proc pointer `p' as it may be different 673 * if this is a child returning from fork syscall. 674 */ 675 p = curproc; 676 frame.tf_eax = rval[0]; 677 frame.tf_edx = rval[1]; 678 frame.tf_eflags &= ~PSL_C; /* carry bit */ 679 break; 680 681 case ERESTART: 682 frame.tf_eip = opc; 683 break; 684 685 case EJUSTRETURN: 686 break; 687 688 default: 689 bad: 690 frame.tf_eax = error; 691 frame.tf_eflags |= PSL_C; /* carry bit */ 692 break; 693 } 694 695 userret(p, &frame, sticks); 696 697#ifdef KTRACE 698 if (KTRPOINT(p, KTR_SYSRET)) 699 ktrsysret(p->p_tracep, code, error, rval[0]); 700#endif 701} 702