subr_syscall.c revision 2357
1157091Simp/*- 2157091Simp * Copyright (C) 1994, David Greenman 3157091Simp * Copyright (c) 1990, 1993 4157091Simp * The Regents of the University of California. All rights reserved. 5157091Simp * 6157091Simp * This code is derived from software contributed to Berkeley by 7157091Simp * the University of Utah, and William Jolitz. 8157091Simp * 9157091Simp * Redistribution and use in source and binary forms, with or without 10157091Simp * modification, are permitted provided that the following conditions 11157091Simp * are met: 12157091Simp * 1. Redistributions of source code must retain the above copyright 13157091Simp * notice, this list of conditions and the following disclaimer. 14157091Simp * 2. Redistributions in binary form must reproduce the above copyright 15157091Simp * notice, this list of conditions and the following disclaimer in the 16157091Simp * documentation and/or other materials provided with the distribution. 17157091Simp * 3. All advertising materials mentioning features or use of this software 18157091Simp * must display the following acknowledgement: 19157091Simp * This product includes software developed by the University of 20157091Simp * California, Berkeley and its contributors. 21157091Simp * 4. Neither the name of the University nor the names of its contributors 22157091Simp * may be used to endorse or promote products derived from this software 23157091Simp * without specific prior written permission. 24157091Simp * 25157091Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26157091Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27157091Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28157091Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29157091Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30163524Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31163524Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32163524Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33163524Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34163524Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35163524Simp * SUCH DAMAGE. 36163524Simp * 37163524Simp * from: @(#)trap.c 7.4 (Berkeley) 5/13/91 38163524Simp * $Id: trap.c,v 1.31 1994/08/27 16:14:13 davidg Exp $ 39163524Simp */ 40163524Simp 41163524Simp/* 42163524Simp * 386 Trap and System call handling 43163524Simp */ 44163524Simp 45163524Simp#include <sys/param.h> 46163524Simp#include <sys/systm.h> 47163524Simp#include <sys/proc.h> 48163524Simp#include <sys/user.h> 49163524Simp#include <sys/acct.h> 50163524Simp#include <sys/kernel.h> 51163524Simp#include <sys/syscall.h> 52163524Simp#include <sys/sysent.h> 53163524Simp#ifdef KTRACE 54163524Simp#include <sys/ktrace.h> 55163524Simp#endif 56163524Simp 57163524Simp#include <vm/vm_param.h> 58163524Simp#include <vm/pmap.h> 59163524Simp#include <vm/vm_map.h> 60163524Simp#include <vm/vm_page.h> 61163524Simp 62163524Simp#include <machine/cpu.h> 63163524Simp#include <machine/psl.h> 64163524Simp#include <machine/reg.h> 65163524Simp#include <machine/eflags.h> 66163524Simp#include <machine/trap.h> 67163524Simp 68163524Simp#include "isa.h" 69163524Simp#include "npx.h" 70163524Simp 71163524Simpint trap_pfault __P((struct trapframe *, int)); 72163524Simpvoid trap_fatal __P((struct trapframe *)); 73163524Simp 74163524Simp#define MAX_TRAP_MSG 27 75163524Simpchar *trap_msg[] = { 76163524Simp "reserved addressing fault", /* 0 T_RESADFLT */ 77163524Simp "privileged instruction fault", /* 1 T_PRIVINFLT */ 78163524Simp "reserved operand fault", /* 2 T_RESOPFLT */ 79163524Simp "breakpoint instruction fault", /* 3 T_BPTFLT */ 80163524Simp "", /* 4 unused */ 81163524Simp "system call trap", /* 5 T_SYSCALL */ 82163524Simp "arithmetic trap", /* 6 T_ARITHTRAP */ 83163524Simp "system forced exception", /* 7 T_ASTFLT */ 84163524Simp "segmentation (limit) fault", /* 8 T_SEGFLT */ 85163524Simp "general protection fault", /* 9 T_PROTFLT */ 86163524Simp "trace trap", /* 10 T_TRCTRAP */ 87163524Simp "", /* 11 unused */ 88163524Simp "page fault", /* 12 T_PAGEFLT */ 89163524Simp "page table fault", /* 13 T_TABLEFLT */ 90163524Simp "alignment fault", /* 14 T_ALIGNFLT */ 91163524Simp "kernel stack pointer not valid", /* 15 T_KSPNOTVAL */ 92163524Simp "bus error", /* 16 T_BUSERR */ 93163524Simp "kernel debugger fault", /* 17 T_KDBTRAP */ 94163524Simp "integer divide fault", /* 18 T_DIVIDE */ 95163524Simp "non-maskable interrupt trap", /* 19 T_NMI */ 96163524Simp "overflow trap", /* 20 T_OFLOW */ 97163524Simp "FPU bounds check fault", /* 21 T_BOUND */ 98163524Simp "FPU device not available", /* 22 T_DNA */ 99163524Simp "double fault", /* 23 T_DOUBLEFLT */ 100163524Simp "FPU operand fetch fault", /* 24 T_FPOPFLT */ 101163524Simp "invalid TSS fault", /* 25 T_TSSFLT */ 102163524Simp "segment not present fault", /* 26 T_SEGNPFLT */ 103163524Simp "stack fault", /* 27 T_STKFLT */ 104163524Simp}; 105163524Simp 106163524Simpstatic inline void 107163524Simpuserret(p, frame, oticks) 108163524Simp struct proc *p; 109163524Simp struct trapframe *frame; 110163524Simp u_quad_t oticks; 111163524Simp{ 112163524Simp int sig, s; 113163524Simp 114163524Simp while (sig = CURSIG(p)) 115163524Simp postsig(sig); 116163524Simp p->p_priority = p->p_usrpri; 117163524Simp if (want_resched) { 118163524Simp /* 119163524Simp * Since we are curproc, clock will normally just change 120163524Simp * our priority without moving us from one queue to another 121163524Simp * (since the running process is not on a queue.) 122163524Simp * If that happened after we setrunqueue ourselves but before we 123163524Simp * mi_switch()'ed, we might not be on the queue indicated by 124163524Simp * our priority. 125163524Simp */ 126163524Simp s = splclock(); 127163524Simp setrunqueue(p); 128163524Simp p->p_stats->p_ru.ru_nivcsw++; 129163524Simp mi_switch(); 130163524Simp splx(s); 131163524Simp while (sig = CURSIG(p)) 132163524Simp postsig(sig); 133163524Simp } 134163524Simp if (p->p_stats->p_prof.pr_scale) { 135163680Simp u_quad_t ticks = p->p_sticks - oticks; 136163680Simp 137163680Simp if (ticks) { 138163680Simp#ifdef PROFTIMER 139163680Simp extern int profscale; 140163680Simp addupc(frame->tf_eip, &p->p_stats->p_prof, 141163680Simp ticks * profscale); 142163680Simp#else 143163680Simp addupc(frame->tf_eip, &p->p_stats->p_prof, ticks); 144163680Simp#endif 145163680Simp } 146163680Simp } 147163680Simp curpriority = p->p_priority; 148163680Simp} 149157091Simp 150/* 151 * trap(frame): 152 * Exception, fault, and trap interface to the FreeBSD kernel. 153 * This common code is called from assembly language IDT gate entry 154 * routines that prepare a suitable stack frame, and restore this 155 * frame after the exception has been processed. 156 */ 157 158/*ARGSUSED*/ 159void 160trap(frame) 161 struct trapframe frame; 162{ 163 struct proc *p = curproc; 164 u_quad_t sticks = 0; 165 int i = 0, ucode = 0, type, code, eva, fault_type; 166 167 frame.tf_eflags &= ~PSL_NT; /* clear nested trap XXX */ 168 type = frame.tf_trapno; 169 code = frame.tf_err; 170 171 if (ISPL(frame.tf_cs) == SEL_UPL) { 172 /* user trap */ 173 174 sticks = p->p_sticks; 175 p->p_md.md_regs = (int *)&frame; 176 177 switch (type) { 178 case T_RESADFLT: /* reserved addressing fault */ 179 case T_PRIVINFLT: /* privileged instruction fault */ 180 case T_RESOPFLT: /* reserved operand fault */ 181 ucode = type; 182 i = SIGILL; 183 break; 184 185 case T_BPTFLT: /* bpt instruction fault */ 186 case T_TRCTRAP: /* trace trap */ 187 frame.tf_eflags &= ~PSL_T; 188 i = SIGTRAP; 189 break; 190 191 case T_ARITHTRAP: /* arithmetic trap */ 192 ucode = code; 193 i = SIGFPE; 194 break; 195 196 case T_ASTFLT: /* Allow process switch */ 197 astoff(); 198 cnt.v_soft++; 199 if ((p->p_flag & P_OWEUPC) && p->p_stats->p_prof.pr_scale) { 200 addupc(frame.tf_eip, &p->p_stats->p_prof, 1); 201 p->p_flag &= ~P_OWEUPC; 202 } 203 goto out; 204 205 case T_PROTFLT: /* general protection fault */ 206 case T_SEGNPFLT: /* segment not present fault */ 207 case T_STKFLT: /* stack fault */ 208 ucode = code + BUS_SEGM_FAULT ; 209 i = SIGBUS; 210 break; 211 212 case T_PAGEFLT: /* page fault */ 213 i = trap_pfault(&frame, TRUE); 214 if (i == 0) 215 goto out; 216 217 ucode = T_PAGEFLT; 218 break; 219 220 case T_DIVIDE: /* integer divide fault */ 221 ucode = FPE_INTDIV_TRAP; 222 i = SIGFPE; 223 break; 224 225#if NISA > 0 226 case T_NMI: 227#ifdef DDB 228 /* NMI can be hooked up to a pushbutton for debugging */ 229 printf ("NMI ... going to debugger\n"); 230 if (kdb_trap (type, 0, &frame)) 231 return; 232#endif 233 /* machine/parity/power fail/"kitchen sink" faults */ 234 if (isa_nmi(code) == 0) return; 235 panic("NMI indicates hardware failure"); 236#endif 237 238 case T_OFLOW: /* integer overflow fault */ 239 ucode = FPE_INTOVF_TRAP; 240 i = SIGFPE; 241 break; 242 243 case T_BOUND: /* bounds check fault */ 244 ucode = FPE_SUBRNG_TRAP; 245 i = SIGFPE; 246 break; 247 248 case T_DNA: 249#if NNPX > 0 250 /* if a transparent fault (due to context switch "late") */ 251 if (npxdna()) 252 return; 253#endif /* NNPX > 0 */ 254 255#if defined(MATH_EMULATE) || defined(GPL_MATH_EMULATE) 256 i = math_emulate(&frame); 257 if (i == 0) return; 258#else /* MATH_EMULATE || GPL_MATH_EMULATE */ 259 panic("trap: math emulation necessary!"); 260#endif /* MATH_EMULATE || GPL_MATH_EMULATE */ 261 ucode = FPE_FPU_NP_TRAP; 262 break; 263 264 case T_FPOPFLT: /* FPU operand fetch fault */ 265 ucode = T_FPOPFLT; 266 i = SIGILL; 267 break; 268 269 default: 270 trap_fatal(&frame); 271 } 272 } else { 273 /* kernel trap */ 274 275 switch (type) { 276 case T_PAGEFLT: /* page fault */ 277 (void) trap_pfault(&frame, FALSE); 278 return; 279 280 case T_PROTFLT: /* general protection fault */ 281 case T_SEGNPFLT: /* segment not present fault */ 282 if (curpcb && curpcb->pcb_onfault) { 283 frame.tf_eip = (int)curpcb->pcb_onfault; 284 return; 285 } 286 break; 287 288#ifdef DDB 289 case T_BPTFLT: 290 case T_TRCTRAP: 291 if (kdb_trap (type, 0, &frame)) 292 return; 293 break; 294#else 295 case T_TRCTRAP: /* trace trap -- someone single stepping lcall's */ 296 /* Q: how do we turn it on again? */ 297 frame.tf_eflags &= ~PSL_T; 298 return; 299#endif 300 301#if NISA > 0 302 case T_NMI: 303#ifdef DDB 304 /* NMI can be hooked up to a pushbutton for debugging */ 305 printf ("NMI ... going to debugger\n"); 306 if (kdb_trap (type, 0, &frame)) 307 return; 308#endif 309 /* machine/parity/power fail/"kitchen sink" faults */ 310 if (isa_nmi(code) == 0) return; 311 /* FALL THROUGH */ 312#endif 313 } 314 315 trap_fatal(&frame); 316 } 317 318 trapsignal(p, i, ucode); 319 320#ifdef DIAGNOSTIC 321 eva = rcr2(); 322 if (type <= MAX_TRAP_MSG) { 323 uprintf("fatal process exception: %s", 324 trap_msg[type]); 325 if ((type == T_PAGEFLT) || (type == T_PROTFLT)) 326 uprintf(", fault VA = 0x%x", eva); 327 uprintf("\n"); 328 } 329#endif 330 331out: 332 userret(p, &frame, sticks); 333} 334 335int 336trap_pfault(frame, usermode) 337 struct trapframe *frame; 338 int usermode; 339{ 340 vm_offset_t va; 341 struct vmspace *vm; 342 vm_map_t map = 0; 343 int rv = 0, oldflags; 344 vm_prot_t ftype; 345 extern vm_map_t kernel_map; 346 int eva; 347 struct proc *p = curproc; 348 349 eva = rcr2(); 350 va = trunc_page((vm_offset_t)eva); 351 352 /* 353 * Don't allow user-mode faults in kernel address space 354 */ 355 if (usermode && (va >= KERNBASE)) { 356 goto nogo; 357 } 358 359 if ((p == 0) || (va >= KERNBASE)) { 360 vm = 0; 361 map = kernel_map; 362 } else { 363 vm = p->p_vmspace; 364 map = &vm->vm_map; 365 } 366 367 if (frame->tf_err & PGEX_W) 368 ftype = VM_PROT_READ | VM_PROT_WRITE; 369 else 370 ftype = VM_PROT_READ; 371 372 if (map != kernel_map) { 373 vm_offset_t pa; 374 vm_offset_t v = (vm_offset_t) vtopte(va); 375 vm_page_t ptepg; 376 377 /* 378 * Keep swapout from messing with us during this 379 * critical time. 380 */ 381 ++p->p_lock; 382 383 /* 384 * Grow the stack if necessary 385 */ 386 if ((caddr_t)va > vm->vm_maxsaddr 387 && (caddr_t)va < (caddr_t)USRSTACK) { 388 if (!grow(p, va)) { 389 rv = KERN_FAILURE; 390 --p->p_lock; 391 goto nogo; 392 } 393 } 394 395 /* 396 * Check if page table is mapped, if not, 397 * fault it first 398 */ 399 400 /* Fault the pte only if needed: */ 401 *(volatile char *)v += 0; 402 403 ptepg = (vm_page_t) pmap_pte_vm_page(vm_map_pmap(map), v); 404 if( ptepg->hold_count == 0) 405 ptepg->act_count += 3; 406 vm_page_hold(ptepg); 407 408 /* Fault in the user page: */ 409 rv = vm_fault(map, va, ftype, FALSE); 410 411 vm_page_unhold(ptepg); 412 413 /* 414 * page table pages don't need to be kept if they 415 * are not held 416 */ 417 if( ptepg->hold_count == 0 && ptepg->wire_count == 0) { 418 pmap_page_protect( VM_PAGE_TO_PHYS(ptepg), 419 VM_PROT_NONE); 420 vm_page_free(ptepg); 421 } 422 423 --p->p_lock; 424 } else { 425 /* 426 * Since we know that kernel virtual address addresses 427 * always have pte pages mapped, we just have to fault 428 * the page. 429 */ 430 rv = vm_fault(map, va, ftype, FALSE); 431 } 432 433 if (rv == KERN_SUCCESS) 434 return (0); 435nogo: 436 if (!usermode) { 437 if (curpcb->pcb_onfault) { 438 frame->tf_eip = (int)curpcb->pcb_onfault; 439 return (0); 440 } 441 trap_fatal(frame); 442 } 443 444 /* kludge to pass faulting virtual address to sendsig */ 445 frame->tf_err = eva; 446 447 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 448} 449 450void 451trap_fatal(frame) 452 struct trapframe *frame; 453{ 454 int code, type, eva; 455 456 code = frame->tf_err; 457 type = frame->tf_trapno; 458 eva = rcr2(); 459 460 if (type <= MAX_TRAP_MSG) 461 printf("\n\nFatal trap %d: %s while in %s mode\n", 462 type, trap_msg[type], 463 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel"); 464 if (type == T_PAGEFLT) { 465 printf("fault virtual address = 0x%x\n", eva); 466 printf("fault code = %s %s, %s\n", 467 code & PGEX_U ? "user" : "supervisor", 468 code & PGEX_W ? "write" : "read", 469 code & PGEX_P ? "protection violation" : "page not present"); 470 } 471 printf("instruction pointer = 0x%x\n", frame->tf_eip); 472 printf("processor eflags = "); 473 if (frame->tf_eflags & EFL_TF) 474 printf("trace/trap, "); 475 if (frame->tf_eflags & EFL_IF) 476 printf("interrupt enabled, "); 477 if (frame->tf_eflags & EFL_NT) 478 printf("nested task, "); 479 if (frame->tf_eflags & EFL_RF) 480 printf("resume, "); 481 if (frame->tf_eflags & EFL_VM) 482 printf("vm86, "); 483 printf("IOPL = %d\n", (frame->tf_eflags & EFL_IOPL) >> 12); 484 printf("current process = "); 485 if (curproc) { 486 printf("%d (%s)\n", 487 curproc->p_pid, curproc->p_comm ? 488 curproc->p_comm : ""); 489 } else { 490 printf("Idle\n"); 491 } 492 printf("interrupt mask = "); 493 if ((cpl & net_imask) == net_imask) 494 printf("net "); 495 if ((cpl & tty_imask) == tty_imask) 496 printf("tty "); 497 if ((cpl & bio_imask) == bio_imask) 498 printf("bio "); 499 if (cpl == 0) 500 printf("none"); 501 printf("\n"); 502 503#ifdef KDB 504 if (kdb_trap(&psl)) 505 return; 506#endif 507#ifdef DDB 508 if (kdb_trap (type, 0, frame)) 509 return; 510#endif 511 if (type <= MAX_TRAP_MSG) 512 panic(trap_msg[type]); 513 else 514 panic("unknown/reserved trap"); 515} 516 517/* 518 * Compensate for 386 brain damage (missing URKR). 519 * This is a little simpler than the pagefault handler in trap() because 520 * it the page tables have already been faulted in and high addresses 521 * are thrown out early for other reasons. 522 */ 523int trapwrite(addr) 524 unsigned addr; 525{ 526 struct proc *p; 527 vm_offset_t va, v; 528 struct vmspace *vm; 529 int oldflags; 530 int rv; 531 532 va = trunc_page((vm_offset_t)addr); 533 /* 534 * XXX - MAX is END. Changed > to >= for temp. fix. 535 */ 536 if (va >= VM_MAXUSER_ADDRESS) 537 return (1); 538 539 p = curproc; 540 vm = p->p_vmspace; 541 542 ++p->p_lock; 543 544 if ((caddr_t)va >= vm->vm_maxsaddr 545 && (caddr_t)va < (caddr_t)USRSTACK) { 546 if (!grow(p, va)) { 547 --p->p_lock; 548 return (1); 549 } 550 } 551 552 v = trunc_page(vtopte(va)); 553 554 /* 555 * wire the pte page 556 */ 557 if (va < USRSTACK) { 558 vm_map_pageable(&vm->vm_map, v, round_page(v+1), FALSE); 559 } 560 561 /* 562 * fault the data page 563 */ 564 rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, FALSE); 565 566 /* 567 * unwire the pte page 568 */ 569 if (va < USRSTACK) { 570 vm_map_pageable(&vm->vm_map, v, round_page(v+1), TRUE); 571 } 572 573 --p->p_lock; 574 575 if (rv != KERN_SUCCESS) 576 return 1; 577 578 return (0); 579} 580 581/* 582 * syscall(frame): 583 * System call request from POSIX system call gate interface to kernel. 584 * Like trap(), argument is call by reference. 585 */ 586/*ARGSUSED*/ 587void 588syscall(frame) 589 struct trapframe frame; 590{ 591 caddr_t params; 592 int i; 593 struct sysent *callp; 594 struct proc *p = curproc; 595 u_quad_t sticks; 596 int error, opc; 597 int args[8], rval[2]; 598 u_int code; 599 600 sticks = p->p_sticks; 601 if (ISPL(frame.tf_cs) != SEL_UPL) 602 panic("syscall"); 603 604 code = frame.tf_eax; 605 p->p_md.md_regs = (int *)&frame; 606 params = (caddr_t)frame.tf_esp + sizeof (int) ; 607 608 /* 609 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always. 610 */ 611 opc = frame.tf_eip - 7; 612 /* 613 * Need to check if this is a 32 bit or 64 bit syscall. 614 */ 615 if (code == SYS_syscall) { 616 /* 617 * Code is first argument, followed by actual args. 618 */ 619 code = fuword(params); 620 params += sizeof (int); 621 } else if (code == SYS___syscall) { 622 /* 623 * Like syscall, but code is a quad, so as to maintain 624 * quad alignment for the rest of the arguments. 625 */ 626 code = fuword(params + _QUAD_LOWWORD * sizeof(int)); 627 params += sizeof(quad_t); 628 } 629 630 if (p->p_sysent->sv_mask) 631 code = code & p->p_sysent->sv_mask; 632 633 if (code >= p->p_sysent->sv_size) 634 callp = &p->p_sysent->sv_table[0]; 635 else 636 callp = &p->p_sysent->sv_table[code]; 637 638 if ((i = callp->sy_narg * sizeof (int)) && 639 (error = copyin(params, (caddr_t)args, (u_int)i))) { 640#ifdef KTRACE 641 if (KTRPOINT(p, KTR_SYSCALL)) 642 ktrsyscall(p->p_tracep, code, callp->sy_narg, args); 643#endif 644 goto bad; 645 } 646#ifdef KTRACE 647 if (KTRPOINT(p, KTR_SYSCALL)) 648 ktrsyscall(p->p_tracep, code, callp->sy_narg, args); 649#endif 650 rval[0] = 0; 651 rval[1] = frame.tf_edx; 652 653 error = (*callp->sy_call)(p, args, rval); 654 655 switch (error) { 656 657 case 0: 658 /* 659 * Reinitialize proc pointer `p' as it may be different 660 * if this is a child returning from fork syscall. 661 */ 662 p = curproc; 663 frame.tf_eax = rval[0]; 664 frame.tf_edx = rval[1]; 665 frame.tf_eflags &= ~PSL_C; /* carry bit */ 666 break; 667 668 case ERESTART: 669 frame.tf_eip = opc; 670 break; 671 672 case EJUSTRETURN: 673 break; 674 675 default: 676 bad: 677 frame.tf_eax = error; 678 frame.tf_eflags |= PSL_C; /* carry bit */ 679 break; 680 } 681 682 userret(p, &frame, sticks); 683 684#ifdef KTRACE 685 if (KTRPOINT(p, KTR_SYSRET)) 686 ktrsysret(p->p_tracep, code, error, rval[0]); 687#endif 688} 689