subr_syscall.c revision 3513
12490Sjkh/*- 251862Sdcs * Copyright (C) 1994, David Greenman 32490Sjkh * Copyright (c) 1990, 1993 418464Sbde * The Regents of the University of California. All rights reserved. 52490Sjkh * 62490Sjkh * This code is derived from software contributed to Berkeley by 72490Sjkh * the University of Utah, and William Jolitz. 852571Sjkh * 952571Sjkh * Redistribution and use in source and binary forms, with or without 1052571Sjkh * modification, are permitted provided that the following conditions 1152571Sjkh * are met: 1252571Sjkh * 1. Redistributions of source code must retain the above copyright 1340478Sbde * notice, this list of conditions and the following disclaimer. 1440478Sbde * 2. Redistributions in binary form must reproduce the above copyright 152490Sjkh * notice, this list of conditions and the following disclaimer in the 1640478Sbde * documentation and/or other materials provided with the distribution. 1718464Sbde * 3. All advertising materials mentioning features or use of this software 182490Sjkh * must display the following acknowledgement: 192490Sjkh * This product includes software developed by the University of 202490Sjkh * California, Berkeley and its contributors. 2140478Sbde * 4. Neither the name of the University nor the names of its contributors 222490Sjkh * may be used to endorse or promote products derived from this software 232490Sjkh * without specific prior written permission. 242490Sjkh * 252490Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2640478Sbde * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2715930Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2840478Sbde * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2922449Swosch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3040478Sbde * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3122449Swosch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3222449Swosch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 332490Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3440478Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3540478Sbde * SUCH DAMAGE. 3652888Sjoerg * 3752586Sdcs * from: @(#)trap.c 7.4 (Berkeley) 5/13/91 3840478Sbde * $Id: trap.c,v 1.38 1994/10/10 07:33:01 sos Exp $ 392490Sjkh */ 4039479Sphk 4151909Sdcs/* 4252586Sdcs * 386 Trap and System call handling 432490Sjkh */ 4439479Sphk 4555059Smarcel#include <sys/param.h> 462490Sjkh#include <sys/systm.h> 472490Sjkh#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#include <machine/../isa/isa_device.h> 67 68#include "isa.h" 69#include "npx.h" 70 71int trap_pfault __P((struct trapframe *, int)); 72void trap_fatal __P((struct trapframe *)); 73 74#define MAX_TRAP_MSG 27 75char *trap_msg[] = { 76 "reserved addressing fault", /* 0 T_RESADFLT */ 77 "privileged instruction fault", /* 1 T_PRIVINFLT */ 78 "reserved operand fault", /* 2 T_RESOPFLT */ 79 "breakpoint instruction fault", /* 3 T_BPTFLT */ 80 "", /* 4 unused */ 81 "system call trap", /* 5 T_SYSCALL */ 82 "arithmetic trap", /* 6 T_ARITHTRAP */ 83 "system forced exception", /* 7 T_ASTFLT */ 84 "segmentation (limit) fault", /* 8 T_SEGFLT */ 85 "general protection fault", /* 9 T_PROTFLT */ 86 "trace trap", /* 10 T_TRCTRAP */ 87 "", /* 11 unused */ 88 "page fault", /* 12 T_PAGEFLT */ 89 "page table fault", /* 13 T_TABLEFLT */ 90 "alignment fault", /* 14 T_ALIGNFLT */ 91 "kernel stack pointer not valid", /* 15 T_KSPNOTVAL */ 92 "bus error", /* 16 T_BUSERR */ 93 "kernel debugger fault", /* 17 T_KDBTRAP */ 94 "integer divide fault", /* 18 T_DIVIDE */ 95 "non-maskable interrupt trap", /* 19 T_NMI */ 96 "overflow trap", /* 20 T_OFLOW */ 97 "FPU bounds check fault", /* 21 T_BOUND */ 98 "FPU device not available", /* 22 T_DNA */ 99 "double fault", /* 23 T_DOUBLEFLT */ 100 "FPU operand fetch fault", /* 24 T_FPOPFLT */ 101 "invalid TSS fault", /* 25 T_TSSFLT */ 102 "segment not present fault", /* 26 T_SEGNPFLT */ 103 "stack fault", /* 27 T_STKFLT */ 104}; 105 106static inline void 107userret(p, frame, oticks) 108 struct proc *p; 109 struct trapframe *frame; 110 u_quad_t oticks; 111{ 112 int sig, s; 113 114 while ((sig = CURSIG(p)) != 0) 115 postsig(sig); 116 p->p_priority = p->p_usrpri; 117 if (want_resched) { 118 /* 119 * Since we are curproc, clock will normally just change 120 * our priority without moving us from one queue to another 121 * (since the running process is not on a queue.) 122 * If that happened after we setrunqueue ourselves but before we 123 * mi_switch()'ed, we might not be on the queue indicated by 124 * our priority. 125 */ 126 s = splclock(); 127 setrunqueue(p); 128 p->p_stats->p_ru.ru_nivcsw++; 129 mi_switch(); 130 splx(s); 131 while ((sig = CURSIG(p)) != 0) 132 postsig(sig); 133 } 134 if (p->p_stats->p_prof.pr_scale) { 135 u_quad_t ticks = p->p_sticks - oticks; 136 137 if (ticks) { 138#ifdef PROFTIMER 139 extern int profscale; 140 addupc(frame->tf_eip, &p->p_stats->p_prof, 141 ticks * profscale); 142#else 143 addupc(frame->tf_eip, &p->p_stats->p_prof, ticks); 144#endif 145 } 146 } 147 curpriority = p->p_priority; 148} 149 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; 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 = NULL; 342 vm_map_t map = 0; 343 int rv = 0; 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 if (va >= KERNBASE) { 353 /* 354 * Don't allow user-mode faults in kernel address space. 355 */ 356 if (usermode) 357 goto nogo; 358 359 map = kernel_map; 360 } else { 361 /* 362 * This is a fault on non-kernel virtual memory. 363 * vm is initialized above to NULL. If curproc is NULL 364 * or curproc->p_vmspace is NULL the fault is fatal. 365 */ 366 if (p != NULL) 367 vm = p->p_vmspace; 368 369 if (vm == NULL) 370 goto nogo; 371 372 map = &vm->vm_map; 373 } 374 375 if (frame->tf_err & PGEX_W) 376 ftype = VM_PROT_READ | VM_PROT_WRITE; 377 else 378 ftype = VM_PROT_READ; 379 380 if (map != kernel_map) { 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("%lu (%s)\n", 500 (u_long)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 rv; 543 544 va = trunc_page((vm_offset_t)addr); 545 /* 546 * XXX - MAX is END. Changed > to >= for temp. fix. 547 */ 548 if (va >= VM_MAXUSER_ADDRESS) 549 return (1); 550 551 p = curproc; 552 vm = p->p_vmspace; 553 554 ++p->p_lock; 555 556 if ((caddr_t)va >= vm->vm_maxsaddr 557 && (caddr_t)va < (caddr_t)USRSTACK) { 558 if (!grow(p, va)) { 559 --p->p_lock; 560 return (1); 561 } 562 } 563 564 v = trunc_page(vtopte(va)); 565 566 /* 567 * wire the pte page 568 */ 569 if (va < USRSTACK) { 570 vm_map_pageable(&vm->vm_map, v, round_page(v+1), FALSE); 571 } 572 573 /* 574 * fault the data page 575 */ 576 rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, FALSE); 577 578 /* 579 * unwire the pte page 580 */ 581 if (va < USRSTACK) { 582 vm_map_pageable(&vm->vm_map, v, round_page(v+1), TRUE); 583 } 584 585 --p->p_lock; 586 587 if (rv != KERN_SUCCESS) 588 return 1; 589 590 return (0); 591} 592 593/* 594 * syscall(frame): 595 * System call request from POSIX system call gate interface to kernel. 596 * Like trap(), argument is call by reference. 597 */ 598/*ARGSUSED*/ 599void 600syscall(frame) 601 struct trapframe frame; 602{ 603 caddr_t params; 604 int i; 605 struct sysent *callp; 606 struct proc *p = curproc; 607 u_quad_t sticks; 608 int error, opc; 609 int args[8], rval[2]; 610 u_int code; 611 612 sticks = p->p_sticks; 613 if (ISPL(frame.tf_cs) != SEL_UPL) 614 panic("syscall"); 615 616 code = frame.tf_eax; 617 p->p_md.md_regs = (int *)&frame; 618 params = (caddr_t)frame.tf_esp + sizeof (int) ; 619 620 /* 621 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always. 622 */ 623 opc = frame.tf_eip - 7; 624 /* 625 * Need to check if this is a 32 bit or 64 bit syscall. 626 */ 627 if (code == SYS_syscall) { 628 /* 629 * Code is first argument, followed by actual args. 630 */ 631 code = fuword(params); 632 params += sizeof (int); 633 } else if (code == SYS___syscall) { 634 /* 635 * Like syscall, but code is a quad, so as to maintain 636 * quad alignment for the rest of the arguments. 637 */ 638 code = fuword(params + _QUAD_LOWWORD * sizeof(int)); 639 params += sizeof(quad_t); 640 } 641 642 if (p->p_sysent->sv_mask) 643 code = code & p->p_sysent->sv_mask; 644 645 if (code >= p->p_sysent->sv_size) 646 callp = &p->p_sysent->sv_table[0]; 647 else 648 callp = &p->p_sysent->sv_table[code]; 649 650 if ((i = callp->sy_narg * sizeof (int)) && 651 (error = copyin(params, (caddr_t)args, (u_int)i))) { 652#ifdef KTRACE 653 if (KTRPOINT(p, KTR_SYSCALL)) 654 ktrsyscall(p->p_tracep, code, callp->sy_narg, args); 655#endif 656 goto bad; 657 } 658#ifdef KTRACE 659 if (KTRPOINT(p, KTR_SYSCALL)) 660 ktrsyscall(p->p_tracep, code, callp->sy_narg, args); 661#endif 662 rval[0] = 0; 663 rval[1] = frame.tf_edx; 664 665 error = (*callp->sy_call)(p, args, rval); 666 667 switch (error) { 668 669 case 0: 670 /* 671 * Reinitialize proc pointer `p' as it may be different 672 * if this is a child returning from fork syscall. 673 */ 674 p = curproc; 675 frame.tf_eax = rval[0]; 676 frame.tf_edx = rval[1]; 677 frame.tf_eflags &= ~PSL_C; /* carry bit */ 678 break; 679 680 case ERESTART: 681 frame.tf_eip = opc; 682 break; 683 684 case EJUSTRETURN: 685 break; 686 687 default: 688 bad: 689 if (p->p_sysent->sv_errsize) 690 if (error >= p->p_sysent->sv_errsize) 691 error = -1; /* XXX */ 692 else 693 error = p->p_sysent->sv_errtbl[error]; 694 frame.tf_eax = error; 695 frame.tf_eflags |= PSL_C; /* carry bit */ 696 break; 697 } 698 699 userret(p, &frame, sticks); 700 701#ifdef KTRACE 702 if (KTRPOINT(p, KTR_SYSRET)) 703 ktrsysret(p->p_tracep, code, error, rval[0]); 704#endif 705} 706