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