subr_syscall.c revision 68808
1228753Smm/*- 2228753Smm * Copyright (C) 1994, David Greenman 3228753Smm * Copyright (c) 1990, 1993 4228753Smm * The Regents of the University of California. All rights reserved. 5228753Smm * 6228753Smm * This code is derived from software contributed to Berkeley by 7228753Smm * the University of Utah, and William Jolitz. 8228753Smm * 9228753Smm * Redistribution and use in source and binary forms, with or without 10228753Smm * modification, are permitted provided that the following conditions 11228753Smm * are met: 12228753Smm * 1. Redistributions of source code must retain the above copyright 13228753Smm * notice, this list of conditions and the following disclaimer. 14228753Smm * 2. Redistributions in binary form must reproduce the above copyright 15228753Smm * notice, this list of conditions and the following disclaimer in the 16228753Smm * documentation and/or other materials provided with the distribution. 17228753Smm * 3. All advertising materials mentioning features or use of this software 18228753Smm * must display the following acknowledgement: 19228753Smm * This product includes software developed by the University of 20228753Smm * California, Berkeley and its contributors. 21228753Smm * 4. Neither the name of the University nor the names of its contributors 22228753Smm * may be used to endorse or promote products derived from this software 23228753Smm * without specific prior written permission. 24228753Smm * 25228763Smm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26228753Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27248616Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28232153Smm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29228753Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30228753Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31228753Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32228753Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33228753Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34228753Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35228753Smm * SUCH DAMAGE. 36228753Smm * 37228753Smm * from: @(#)trap.c 7.4 (Berkeley) 5/13/91 38228753Smm * $FreeBSD: head/sys/kern/subr_trap.c 68808 2000-11-16 02:16:44Z jhb $ 39228753Smm */ 40228753Smm 41228753Smm/* 42228753Smm * 386 Trap and System call handling 43228753Smm */ 44228753Smm 45228753Smm#include "opt_cpu.h" 46228753Smm#include "opt_ddb.h" 47228753Smm#include "opt_ktrace.h" 48228753Smm#include "opt_clock.h" 49228753Smm#include "opt_trap.h" 50228753Smm 51228753Smm#include <sys/param.h> 52228753Smm#include <sys/bus.h> 53228776Smm#include <sys/systm.h> 54232153Smm#include <sys/proc.h> 55232153Smm#include <sys/pioctl.h> 56228753Smm#include <sys/ipl.h> 57228753Smm#include <sys/kernel.h> 58228753Smm#include <sys/ktr.h> 59228753Smm#include <sys/mutex.h> 60228753Smm#include <sys/resourcevar.h> 61228753Smm#include <sys/signalvar.h> 62228753Smm#include <sys/syscall.h> 63228753Smm#include <sys/sysctl.h> 64228753Smm#include <sys/sysent.h> 65228753Smm#include <sys/uio.h> 66228753Smm#include <sys/vmmeter.h> 67228753Smm#ifdef KTRACE 68228753Smm#include <sys/ktrace.h> 69228753Smm#endif 70228776Smm 71228776Smm#include <vm/vm.h> 72228753Smm#include <vm/vm_param.h> 73228753Smm#include <sys/lock.h> 74228753Smm#include <vm/pmap.h> 75228753Smm#include <vm/vm_kern.h> 76228753Smm#include <vm/vm_map.h> 77228753Smm#include <vm/vm_page.h> 78228753Smm#include <vm/vm_extern.h> 79228753Smm 80228776Smm#include <machine/cpu.h> 81228776Smm#include <machine/md_var.h> 82228753Smm#include <machine/pcb.h> 83228753Smm#ifdef SMP 84228776Smm#include <machine/smp.h> 85228776Smm#endif 86228753Smm#include <machine/tss.h> 87228753Smm 88228753Smm#include <i386/isa/icu.h> 89228753Smm#include <i386/isa/intr_machdep.h> 90228753Smm 91228753Smm#ifdef POWERFAIL_NMI 92228753Smm#include <sys/syslog.h> 93228753Smm#include <machine/clock.h> 94228753Smm#endif 95228776Smm 96228776Smm#include <machine/vm86.h> 97228753Smm 98228753Smm#include <ddb/ddb.h> 99228753Smm 100228753Smm#include "isa.h" 101228753Smm#include "npx.h" 102228776Smm 103228776Smm#include <sys/sysctl.h> 104228753Smm 105228753Smmint (*pmath_emulate) __P((struct trapframe *)); 106228753Smm 107228753Smmextern void trap __P((struct trapframe frame)); 108228753Smmextern int trapwrite __P((unsigned addr)); 109228753Smmextern void syscall2 __P((struct trapframe frame)); 110228753Smmextern void ast __P((struct trapframe frame)); 111228753Smm 112228753Smmstatic int trap_pfault __P((struct trapframe *, int, vm_offset_t)); 113228753Smmstatic void trap_fatal __P((struct trapframe *, vm_offset_t)); 114228753Smmvoid dblfault_handler __P((void)); 115228753Smm 116228753Smmextern inthand_t IDTVEC(syscall); 117228753Smm 118228753Smm#define MAX_TRAP_MSG 28 119228753Smmstatic char *trap_msg[] = { 120228753Smm "", /* 0 unused */ 121228753Smm "privileged instruction fault", /* 1 T_PRIVINFLT */ 122228753Smm "", /* 2 unused */ 123228753Smm "breakpoint instruction fault", /* 3 T_BPTFLT */ 124228753Smm "", /* 4 unused */ 125228753Smm "", /* 5 unused */ 126228753Smm "arithmetic trap", /* 6 T_ARITHTRAP */ 127228753Smm "system forced exception", /* 7 T_ASTFLT */ 128228753Smm "", /* 8 unused */ 129228753Smm "general protection fault", /* 9 T_PROTFLT */ 130228753Smm "trace trap", /* 10 T_TRCTRAP */ 131228753Smm "", /* 11 unused */ 132228753Smm "page fault", /* 12 T_PAGEFLT */ 133228753Smm "", /* 13 unused */ 134228753Smm "alignment fault", /* 14 T_ALIGNFLT */ 135228753Smm "", /* 15 unused */ 136228753Smm "", /* 16 unused */ 137228753Smm "", /* 17 unused */ 138228753Smm "integer divide fault", /* 18 T_DIVIDE */ 139228753Smm "non-maskable interrupt trap", /* 19 T_NMI */ 140228776Smm "overflow trap", /* 20 T_OFLOW */ 141228753Smm "FPU bounds check fault", /* 21 T_BOUND */ 142228753Smm "FPU device not available", /* 22 T_DNA */ 143228753Smm "double fault", /* 23 T_DOUBLEFLT */ 144228753Smm "FPU operand fetch fault", /* 24 T_FPOPFLT */ 145228753Smm "invalid TSS fault", /* 25 T_TSSFLT */ 146228753Smm "segment not present fault", /* 26 T_SEGNPFLT */ 147248616Smm "stack fault", /* 27 T_STKFLT */ 148248616Smm "machine check trap", /* 28 T_MCHK */ 149248616Smm}; 150248616Smm 151248616Smmstatic __inline int userret __P((struct proc *p, struct trapframe *frame, 152248616Smm u_quad_t oticks, int have_giant)); 153248616Smm 154248616Smm#if defined(I586_CPU) && !defined(NO_F00F_HACK) 155248616Smmextern int has_f00f_bug; 156248616Smm#endif 157248616Smm 158248616Smm#ifdef DDB 159248616Smmstatic int ddb_on_nmi = 1; 160248616SmmSYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW, 161248616Smm &ddb_on_nmi, 0, "Go to DDB on NMI"); 162248616Smm#endif 163248616Smmstatic int panic_on_nmi = 1; 164248616SmmSYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW, 165248616Smm &panic_on_nmi, 0, "Panic on NMI"); 166248616Smm 167228776Smmstatic __inline int 168228776Smmuserret(p, frame, oticks, have_giant) 169228776Smm struct proc *p; 170228776Smm struct trapframe *frame; 171228776Smm u_quad_t oticks; 172228753Smm int have_giant; 173228753Smm{ 174228753Smm int sig, s; 175228753Smm 176232153Smm while ((sig = CURSIG(p)) != 0) { 177228753Smm if (have_giant == 0) { 178228753Smm mtx_enter(&Giant, MTX_DEF); 179228753Smm have_giant = 1; 180228753Smm } 181228776Smm postsig(sig); 182228753Smm } 183228753Smm 184228753Smm p->p_priority = p->p_usrpri; 185228753Smm if (resched_wanted()) { 186228753Smm /* 187228776Smm * Since we are curproc, clock will normally just change 188228776Smm * our priority without moving us from one queue to another 189228776Smm * (since the running process is not on a queue.) 190228776Smm * If that happened after we setrunqueue ourselves but before we 191228776Smm * mi_switch()'ed, we might not be on the queue indicated by 192228753Smm * our priority. 193228753Smm */ 194228753Smm s = splhigh(); 195228753Smm DROP_GIANT_NOSWITCH(); 196228776Smm mtx_enter(&sched_lock, MTX_SPIN); 197228753Smm setrunqueue(p); 198228753Smm p->p_stats->p_ru.ru_nivcsw++; 199228753Smm mi_switch(); 200228753Smm mtx_exit(&sched_lock, MTX_SPIN); 201228753Smm PICKUP_GIANT(); 202228753Smm splx(s); 203228753Smm while ((sig = CURSIG(p)) != 0) { 204228753Smm if (have_giant == 0) { 205228753Smm mtx_enter(&Giant, MTX_DEF); 206228753Smm have_giant = 1; 207228753Smm } 208228753Smm postsig(sig); 209228753Smm } 210228776Smm } 211228753Smm /* 212228753Smm * Charge system time if profiling. 213228753Smm */ 214228753Smm if (p->p_flag & P_PROFIL) { 215228776Smm if (have_giant == 0) { 216228776Smm mtx_enter(&Giant, MTX_DEF); 217228753Smm have_giant = 1; 218228776Smm } 219228776Smm addupc_task(p, frame->tf_eip, 220228776Smm (u_int)(p->p_sticks - oticks) * psratio); 221228776Smm } 222228753Smm curpriority = p->p_priority; 223228753Smm return(have_giant); 224228753Smm} 225228753Smm 226228753Smm/* 227228753Smm * Exception, fault, and trap interface to the FreeBSD kernel. 228228753Smm * This common code is called from assembly language IDT gate entry 229228753Smm * routines that prepare a suitable stack frame, and restore this 230228753Smm * frame after the exception has been processed. 231228753Smm */ 232228753Smm 233228753Smmvoid 234228753Smmtrap(frame) 235228753Smm struct trapframe frame; 236228753Smm{ 237228753Smm struct proc *p = curproc; 238228753Smm u_quad_t sticks = 0; 239228753Smm int i = 0, ucode = 0, type, code; 240228753Smm vm_offset_t eva; 241228753Smm#ifdef POWERFAIL_NMI 242228753Smm static int lastalert = 0; 243228753Smm#endif 244228753Smm 245228753Smm atomic_add_int(&cnt.v_trap, 1); 246228753Smm 247228753Smm if ((frame.tf_eflags & PSL_I) == 0) { 248228753Smm /* 249228753Smm * Buggy application or kernel code has disabled 250228753Smm * interrupts and then trapped. Enabling interrupts 251228753Smm * now is wrong, but it is better than running with 252228753Smm * interrupts disabled until they are accidentally 253228753Smm * enabled later. XXX Consider whether is this still 254228776Smm * correct. 255228776Smm */ 256248616Smm type = frame.tf_trapno; 257248616Smm if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM)) 258248616Smm printf( 259228776Smm "pid %ld (%s): trap %d with interrupts disabled\n", 260228753Smm (long)curproc->p_pid, curproc->p_comm, type); 261228753Smm else if (type != T_BPTFLT && type != T_TRCTRAP) 262228776Smm /* 263228753Smm * XXX not quite right, since this may be for a 264228753Smm * multiple fault in user mode. 265228753Smm */ 266228753Smm printf("kernel trap %d with interrupts disabled\n", 267228776Smm type); 268228753Smm enable_intr(); 269228753Smm } 270228776Smm 271228753Smm eva = 0; 272228753Smm if (frame.tf_trapno == T_PAGEFLT) { 273228753Smm /* 274228753Smm * For some Cyrix CPUs, %cr2 is clobbered by 275228753Smm * interrupts. This problem is worked around by using 276228753Smm * an interrupt gate for the pagefault handler. We 277228776Smm * are finally ready to read %cr2 and then must 278228753Smm * reenable interrupts. 279228753Smm */ 280228776Smm eva = rcr2(); 281228776Smm enable_intr(); 282228776Smm } 283228776Smm 284228776Smm mtx_enter(&Giant, MTX_DEF); 285228776Smm 286228776Smm#if defined(I586_CPU) && !defined(NO_F00F_HACK) 287228776Smmrestart: 288228776Smm#endif 289228753Smm 290228753Smm type = frame.tf_trapno; 291228753Smm code = frame.tf_err; 292228753Smm 293228753Smm if ((ISPL(frame.tf_cs) == SEL_UPL) || 294228753Smm ((frame.tf_eflags & PSL_VM) && !in_vm86call)) { 295228776Smm /* user trap */ 296228753Smm 297228753Smm sticks = p->p_sticks; 298228753Smm p->p_md.md_regs = &frame; 299228753Smm 300228776Smm switch (type) { 301228753Smm case T_PRIVINFLT: /* privileged instruction fault */ 302228753Smm ucode = type; 303228753Smm i = SIGILL; 304228776Smm break; 305228753Smm 306228753Smm case T_BPTFLT: /* bpt instruction fault */ 307228753Smm case T_TRCTRAP: /* trace trap */ 308228753Smm frame.tf_eflags &= ~PSL_T; 309228776Smm i = SIGTRAP; 310228776Smm break; 311228776Smm 312248616Smm case T_ARITHTRAP: /* arithmetic trap */ 313248616Smm ucode = code; 314248616Smm i = SIGFPE; 315248616Smm break; 316248616Smm 317228776Smm /* 318228776Smm * The following two traps can happen in 319228776Smm * vm86 mode, and, if so, we want to handle 320228776Smm * them specially. 321228776Smm */ 322228776Smm case T_PROTFLT: /* general protection fault */ 323228776Smm case T_STKFLT: /* stack fault */ 324228776Smm if (frame.tf_eflags & PSL_VM) { 325228776Smm i = vm86_emulate((struct vm86frame *)&frame); 326248616Smm if (i == 0) 327248616Smm goto user; 328248616Smm break; 329248616Smm } 330248616Smm /* FALL THROUGH */ 331228776Smm 332228753Smm case T_SEGNPFLT: /* segment not present fault */ 333228753Smm case T_TSSFLT: /* invalid TSS fault */ 334228753Smm case T_DOUBLEFLT: /* double fault */ 335228776Smm default: 336228753Smm ucode = code + BUS_SEGM_FAULT ; 337228753Smm i = SIGBUS; 338228776Smm break; 339228753Smm 340228753Smm case T_PAGEFLT: /* page fault */ 341228753Smm i = trap_pfault(&frame, TRUE, eva); 342228776Smm#if defined(I586_CPU) && !defined(NO_F00F_HACK) 343228753Smm if (i == -2) { 344228753Smm /* 345228776Smm * f00f hack workaround has triggered, treat 346228753Smm * as illegal instruction not page fault. 347228776Smm */ 348228753Smm frame.tf_trapno = T_PRIVINFLT; 349228753Smm goto restart; 350228753Smm } 351228776Smm#endif 352228753Smm if (i == -1) 353228753Smm goto out; 354228776Smm if (i == 0) 355228753Smm goto user; 356228776Smm 357228753Smm ucode = T_PAGEFLT; 358228753Smm break; 359248616Smm 360248616Smm case T_DIVIDE: /* integer divide fault */ 361248616Smm ucode = FPE_INTDIV; 362248616Smm i = SIGFPE; 363248616Smm break; 364228776Smm 365228753Smm#if NISA > 0 366228776Smm case T_NMI: 367228753Smm#ifdef POWERFAIL_NMI 368228776Smm#ifndef TIMER_FREQ 369228753Smm# define TIMER_FREQ 1193182 370228753Smm#endif 371228753Smm if (time_second - lastalert > 10) { 372228753Smm log(LOG_WARNING, "NMI: power fail\n"); 373228753Smm sysbeep(TIMER_FREQ/880, hz); 374228753Smm lastalert = time_second; 375228753Smm } 376228753Smm goto out; 377228753Smm#else /* !POWERFAIL_NMI */ 378228753Smm /* machine/parity/power fail/"kitchen sink" faults */ 379228753Smm if (isa_nmi(code) == 0) { 380228753Smm#ifdef DDB 381228753Smm /* 382228753Smm * NMI can be hooked up to a pushbutton 383228753Smm * for debugging. 384228753Smm */ 385228753Smm if (ddb_on_nmi) { 386228753Smm printf ("NMI ... going to debugger\n"); 387228753Smm kdb_trap (type, 0, &frame); 388228753Smm } 389228753Smm#endif /* DDB */ 390228753Smm goto out; 391228753Smm } else if (panic_on_nmi) 392228753Smm panic("NMI indicates hardware failure"); 393228753Smm break; 394228753Smm#endif /* POWERFAIL_NMI */ 395228753Smm#endif /* NISA > 0 */ 396228753Smm 397228753Smm case T_OFLOW: /* integer overflow fault */ 398228753Smm ucode = FPE_INTOVF; 399228753Smm i = SIGFPE; 400228753Smm break; 401228753Smm 402228776Smm case T_BOUND: /* bounds check fault */ 403228753Smm ucode = FPE_FLTSUB; 404228753Smm i = SIGFPE; 405228753Smm break; 406228753Smm 407228753Smm case T_DNA: 408228753Smm#if NNPX > 0 409228753Smm /* transparent fault (due to context switch "late") */ 410228753Smm if (npxdna()) 411228753Smm goto out; 412228753Smm#endif 413228753Smm if (!pmath_emulate) { 414228753Smm i = SIGFPE; 415228753Smm ucode = FPE_FPU_NP_TRAP; 416228753Smm break; 417228753Smm } 418228753Smm i = (*pmath_emulate)(&frame); 419228753Smm if (i == 0) { 420228753Smm if (!(frame.tf_eflags & PSL_T)) 421228776Smm goto out; 422248616Smm frame.tf_eflags &= ~PSL_T; 423248616Smm i = SIGTRAP; 424248616Smm } 425248616Smm /* else ucode = emulator_only_knows() XXX */ 426248616Smm break; 427248616Smm 428248616Smm case T_FPOPFLT: /* FPU operand fetch fault */ 429248616Smm ucode = T_FPOPFLT; 430248616Smm i = SIGILL; 431248616Smm break; 432248616Smm } 433248616Smm } else { 434248616Smm /* kernel trap */ 435248616Smm 436248616Smm switch (type) { 437248616Smm case T_PAGEFLT: /* page fault */ 438248616Smm (void) trap_pfault(&frame, FALSE, eva); 439248616Smm goto out; 440228776Smm 441228753Smm case T_DNA: 442228753Smm#if NNPX > 0 443228776Smm /* 444228753Smm * The kernel is apparently using npx for copying. 445228753Smm * XXX this should be fatal unless the kernel has 446228753Smm * registered such use. 447228753Smm */ 448228753Smm if (npxdna()) 449228753Smm goto out; 450228753Smm#endif 451228753Smm break; 452228753Smm 453228753Smm /* 454228753Smm * The following two traps can happen in 455228753Smm * vm86 mode, and, if so, we want to handle 456228753Smm * them specially. 457228753Smm */ 458228753Smm case T_PROTFLT: /* general protection fault */ 459228753Smm case T_STKFLT: /* stack fault */ 460228753Smm if (frame.tf_eflags & PSL_VM) { 461228753Smm i = vm86_emulate((struct vm86frame *)&frame); 462228753Smm if (i != 0) 463228753Smm /* 464228753Smm * returns to original process 465228753Smm */ 466228753Smm vm86_trap((struct vm86frame *)&frame); 467228753Smm goto out; 468228753Smm } 469228753Smm if (type == T_STKFLT) 470228753Smm break; 471228753Smm 472228753Smm /* FALL THROUGH */ 473228753Smm 474228753Smm case T_SEGNPFLT: /* segment not present fault */ 475228753Smm if (in_vm86call) 476228753Smm break; 477228753Smm 478228753Smm if (intr_nesting_level != 0) 479228753Smm break; 480228753Smm 481228753Smm /* 482248616Smm * Invalid %fs's and %gs's can be created using 483248616Smm * procfs or PT_SETREGS or by invalidating the 484248616Smm * underlying LDT entry. This causes a fault 485248616Smm * in kernel mode when the kernel attempts to 486248616Smm * switch contexts. Lose the bad context 487248616Smm * (XXX) so that we can continue, and generate 488248616Smm * a signal. 489248616Smm */ 490248616Smm if (frame.tf_eip == (int)cpu_switch_load_gs) { 491248616Smm curpcb->pcb_gs = 0; 492248616Smm psignal(p, SIGBUS); 493248616Smm goto out; 494248616Smm } 495248616Smm 496248616Smm /* 497248616Smm * Invalid segment selectors and out of bounds 498248616Smm * %eip's and %esp's can be set up in user mode. 499228753Smm * This causes a fault in kernel mode when the 500228753Smm * kernel tries to return to user mode. We want 501228753Smm * to get this fault so that we can fix the 502228753Smm * problem here and not have to check all the 503228753Smm * selectors and pointers when the user changes 504228753Smm * them. 505228753Smm */ 506228753Smm if (frame.tf_eip == (int)doreti_iret) { 507228753Smm frame.tf_eip = (int)doreti_iret_fault; 508228753Smm goto out; 509228753Smm } 510228753Smm if (frame.tf_eip == (int)doreti_popl_ds) { 511228753Smm frame.tf_eip = (int)doreti_popl_ds_fault; 512228753Smm goto out; 513228753Smm } 514228753Smm if (frame.tf_eip == (int)doreti_popl_es) { 515228753Smm frame.tf_eip = (int)doreti_popl_es_fault; 516228753Smm goto out; 517228753Smm } 518228753Smm if (frame.tf_eip == (int)doreti_popl_fs) { 519228753Smm frame.tf_eip = (int)doreti_popl_fs_fault; 520228753Smm goto out; 521228753Smm } 522228753Smm if (curpcb && curpcb->pcb_onfault) { 523228753Smm frame.tf_eip = (int)curpcb->pcb_onfault; 524228753Smm goto out; 525228753Smm } 526228753Smm break; 527228753Smm 528228753Smm case T_TSSFLT: 529228753Smm /* 530228776Smm * PSL_NT can be set in user mode and isn't cleared 531228753Smm * automatically when the kernel is entered. This 532228753Smm * causes a TSS fault when the kernel attempts to 533228753Smm * `iret' because the TSS link is uninitialized. We 534228753Smm * want to get this fault so that we can fix the 535228753Smm * problem here and not every time the kernel is 536228753Smm * entered. 537228753Smm */ 538228753Smm if (frame.tf_eflags & PSL_NT) { 539228753Smm frame.tf_eflags &= ~PSL_NT; 540228753Smm goto out; 541228776Smm } 542228753Smm break; 543228753Smm 544228753Smm case T_TRCTRAP: /* trace trap */ 545228753Smm if (frame.tf_eip == (int)IDTVEC(syscall)) { 546228776Smm /* 547228753Smm * We've just entered system mode via the 548232153Smm * syscall lcall. Continue single stepping 549228776Smm * silently until the syscall handler has 550228776Smm * saved the flags. 551228776Smm */ 552228776Smm goto out; 553228776Smm } 554228776Smm if (frame.tf_eip == (int)IDTVEC(syscall) + 1) { 555228776Smm /* 556228776Smm * The syscall handler has now saved the 557228753Smm * flags. Stop single stepping it. 558228753Smm */ 559228753Smm frame.tf_eflags &= ~PSL_T; 560228753Smm goto out; 561228753Smm } 562228753Smm /* 563228753Smm * Ignore debug register trace traps due to 564228753Smm * accesses in the user's address space, which 565228753Smm * can happen under several conditions such as 566228753Smm * if a user sets a watchpoint on a buffer and 567228753Smm * then passes that buffer to a system call. 568228753Smm * We still want to get TRCTRAPS for addresses 569228753Smm * in kernel space because that is useful when 570228776Smm * debugging the kernel. 571228753Smm */ 572228753Smm if (user_dbreg_trap() && !in_vm86call) { 573228753Smm /* 574228753Smm * Reset breakpoint bits because the 575232153Smm * processor doesn't 576228753Smm */ 577228753Smm load_dr6(rdr6() & 0xfffffff0); 578228753Smm goto out; 579228753Smm } 580228753Smm /* 581228753Smm * Fall through (TRCTRAP kernel mode, kernel address) 582228753Smm */ 583228753Smm case T_BPTFLT: 584228753Smm /* 585228753Smm * If DDB is enabled, let it handle the debugger trap. 586228753Smm * Otherwise, debugger traps "can't happen". 587228753Smm */ 588228753Smm#ifdef DDB 589228753Smm if (kdb_trap (type, 0, &frame)) 590228753Smm goto out; 591232153Smm#endif 592228753Smm break; 593228753Smm 594228753Smm#if NISA > 0 595228753Smm case T_NMI: 596228753Smm#ifdef POWERFAIL_NMI 597232153Smm if (time_second - lastalert > 10) { 598232153Smm log(LOG_WARNING, "NMI: power fail\n"); 599232153Smm sysbeep(TIMER_FREQ/880, hz); 600232153Smm lastalert = time_second; 601232153Smm } 602232153Smm goto out; 603232153Smm#else /* !POWERFAIL_NMI */ 604232153Smm /* machine/parity/power fail/"kitchen sink" faults */ 605232153Smm if (isa_nmi(code) == 0) { 606232153Smm#ifdef DDB 607232153Smm /* 608232153Smm * NMI can be hooked up to a pushbutton 609232153Smm * for debugging. 610232153Smm */ 611232153Smm if (ddb_on_nmi) { 612232153Smm printf ("NMI ... going to debugger\n"); 613232153Smm kdb_trap (type, 0, &frame); 614232153Smm } 615232153Smm#endif /* DDB */ 616232153Smm goto out; 617232153Smm } else if (panic_on_nmi == 0) 618232153Smm goto out; 619232153Smm /* FALL THROUGH */ 620228776Smm#endif /* POWERFAIL_NMI */ 621228753Smm#endif /* NISA > 0 */ 622228753Smm } 623228753Smm 624228753Smm trap_fatal(&frame, eva); 625228753Smm goto out; 626228753Smm } 627228753Smm 628228753Smm /* Translate fault for emulators (e.g. Linux) */ 629228753Smm if (*p->p_sysent->sv_transtrap) 630228753Smm i = (*p->p_sysent->sv_transtrap)(i, type); 631228753Smm 632228753Smm trapsignal(p, i, ucode); 633228753Smm 634228776Smm#ifdef DEBUG 635228753Smm if (type <= MAX_TRAP_MSG) { 636228753Smm uprintf("fatal process exception: %s", 637228776Smm trap_msg[type]); 638228753Smm if ((type == T_PAGEFLT) || (type == T_PROTFLT)) 639228753Smm uprintf(", fault VA = 0x%lx", (u_long)eva); 640248616Smm uprintf("\n"); 641248616Smm } 642248616Smm#endif 643248616Smm 644248616Smmuser: 645228776Smm userret(p, &frame, sticks, 1); 646228776Smmout: 647228776Smm mtx_exit(&Giant, MTX_DEF); 648228776Smm} 649228753Smm 650228753Smm#ifdef notyet 651228776Smm/* 652228776Smm * This version doesn't allow a page fault to user space while 653228776Smm * in the kernel. The rest of the kernel needs to be made "safe" 654228776Smm * before this can be used. I think the only things remaining 655228753Smm * to be made safe are the iBCS2 code and the process tracing/ 656228776Smm * debugging code. 657228776Smm */ 658228776Smmstatic int 659228753Smmtrap_pfault(frame, usermode, eva) 660228753Smm struct trapframe *frame; 661228753Smm int usermode; 662228753Smm vm_offset_t eva; 663228753Smm{ 664228753Smm vm_offset_t va; 665228753Smm struct vmspace *vm = NULL; 666228753Smm vm_map_t map = 0; 667228753Smm int rv = 0; 668228753Smm vm_prot_t ftype; 669228753Smm struct proc *p = curproc; 670228753Smm 671228753Smm if (frame->tf_err & PGEX_W) 672228753Smm ftype = VM_PROT_WRITE; 673228753Smm else 674228753Smm ftype = VM_PROT_READ; 675228753Smm 676228753Smm va = trunc_page(eva); 677228753Smm if (va < VM_MIN_KERNEL_ADDRESS) { 678228753Smm vm_offset_t v; 679228753Smm vm_page_t mpte; 680228753Smm 681228753Smm if (p == NULL || 682228776Smm (!usermode && va < VM_MAXUSER_ADDRESS && 683228753Smm (intr_nesting_level != 0 || curpcb == NULL || 684228753Smm curpcb->pcb_onfault == NULL))) { 685228753Smm trap_fatal(frame, eva); 686228753Smm return (-1); 687228753Smm } 688228753Smm 689228753Smm /* 690228753Smm * This is a fault on non-kernel virtual memory. 691228753Smm * vm is initialized above to NULL. If curproc is NULL 692228753Smm * or curproc->p_vmspace is NULL the fault is fatal. 693228753Smm */ 694228753Smm vm = p->p_vmspace; 695228776Smm if (vm == NULL) 696228753Smm goto nogo; 697228753Smm 698228753Smm map = &vm->vm_map; 699228753Smm 700228753Smm /* 701228776Smm * Keep swapout from messing with us during this 702228753Smm * critical time. 703228776Smm */ 704228753Smm ++p->p_lock; 705228753Smm 706228776Smm /* 707228753Smm * Grow the stack if necessary 708228753Smm */ 709228753Smm /* grow_stack returns false only if va falls into 710228753Smm * a growable stack region and the stack growth 711228753Smm * fails. It returns true if va was not within 712228753Smm * a growable stack region, or if the stack 713228753Smm * growth succeeded. 714228753Smm */ 715228753Smm if (!grow_stack (p, va)) { 716228753Smm rv = KERN_FAILURE; 717228776Smm --p->p_lock; 718228753Smm goto nogo; 719228753Smm } 720228776Smm 721228753Smm /* Fault in the user page: */ 722228753Smm rv = vm_fault(map, va, ftype, 723228753Smm (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY 724228776Smm : VM_FAULT_NORMAL); 725228753Smm 726228776Smm --p->p_lock; 727228753Smm } else { 728228753Smm /* 729228776Smm * Don't allow user-mode faults in kernel address space. 730228753Smm */ 731228753Smm if (usermode) 732228753Smm goto nogo; 733228776Smm 734228753Smm /* 735228753Smm * Since we know that kernel virtual address addresses 736228753Smm * always have pte pages mapped, we just have to fault 737228753Smm * the page. 738228753Smm */ 739228753Smm rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL); 740248616Smm } 741248616Smm 742248616Smm if (rv == KERN_SUCCESS) 743248616Smm return (0); 744248616Smmnogo: 745248616Smm if (!usermode) { 746248616Smm if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) { 747248616Smm frame->tf_eip = (int)curpcb->pcb_onfault; 748248616Smm return (0); 749248616Smm } 750228753Smm trap_fatal(frame, eva); 751228753Smm return (-1); 752228753Smm } 753228753Smm 754228753Smm /* kludge to pass faulting virtual address to sendsig */ 755228753Smm frame->tf_err = eva; 756228776Smm 757228753Smm return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 758228753Smm} 759228753Smm#endif 760228776Smm 761228776Smmint 762228776Smmtrap_pfault(frame, usermode, eva) 763228753Smm struct trapframe *frame; 764228753Smm int usermode; 765228753Smm vm_offset_t eva; 766228753Smm{ 767228753Smm vm_offset_t va; 768228753Smm struct vmspace *vm = NULL; 769228776Smm vm_map_t map = 0; 770228776Smm int rv = 0; 771228753Smm vm_prot_t ftype; 772228753Smm struct proc *p = curproc; 773228753Smm 774228753Smm va = trunc_page(eva); 775228753Smm if (va >= KERNBASE) { 776228753Smm /* 777228753Smm * Don't allow user-mode faults in kernel address space. 778228753Smm * An exception: if the faulting address is the invalid 779228753Smm * instruction entry in the IDT, then the Intel Pentium 780228753Smm * F00F bug workaround was triggered, and we need to 781228753Smm * treat it is as an illegal instruction, and not a page 782228753Smm * fault. 783228753Smm */ 784228753Smm#if defined(I586_CPU) && !defined(NO_F00F_HACK) 785228753Smm if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) 786228753Smm return -2; 787228753Smm#endif 788228753Smm if (usermode) 789228753Smm goto nogo; 790228753Smm 791228753Smm map = kernel_map; 792228753Smm } else { 793228753Smm /* 794228753Smm * This is a fault on non-kernel virtual memory. 795228753Smm * vm is initialized above to NULL. If curproc is NULL 796228753Smm * or curproc->p_vmspace is NULL the fault is fatal. 797228753Smm */ 798228753Smm if (p != NULL) 799228753Smm vm = p->p_vmspace; 800228753Smm 801228753Smm if (vm == NULL) 802228753Smm goto nogo; 803228753Smm 804228753Smm map = &vm->vm_map; 805228753Smm } 806228753Smm 807228753Smm if (frame->tf_err & PGEX_W) 808228753Smm ftype = VM_PROT_WRITE; 809228753Smm else 810228753Smm ftype = VM_PROT_READ; 811228753Smm 812228753Smm if (map != kernel_map) { 813228753Smm /* 814228753Smm * Keep swapout from messing with us during this 815228753Smm * critical time. 816228753Smm */ 817228753Smm ++p->p_lock; 818228753Smm 819228753Smm /* 820228753Smm * Grow the stack if necessary 821228753Smm */ 822228753Smm /* grow_stack returns false only if va falls into 823228753Smm * a growable stack region and the stack growth 824228753Smm * fails. It returns true if va was not within 825228753Smm * a growable stack region, or if the stack 826228753Smm * growth succeeded. 827228753Smm */ 828228753Smm if (!grow_stack (p, va)) { 829228753Smm rv = KERN_FAILURE; 830228753Smm --p->p_lock; 831228753Smm goto nogo; 832228753Smm } 833228776Smm 834228753Smm /* Fault in the user page: */ 835228776Smm rv = vm_fault(map, va, ftype, 836228753Smm (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY 837228753Smm : VM_FAULT_NORMAL); 838228753Smm 839228753Smm --p->p_lock; 840228753Smm } else { 841228753Smm /* 842228753Smm * Don't have to worry about process locking or stacks in the kernel. 843228753Smm */ 844228776Smm rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 845228753Smm } 846228753Smm 847228753Smm if (rv == KERN_SUCCESS) 848228753Smm return (0); 849228753Smmnogo: 850228753Smm if (!usermode) { 851228753Smm if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) { 852228776Smm frame->tf_eip = (int)curpcb->pcb_onfault; 853228753Smm return (0); 854228776Smm } 855228753Smm trap_fatal(frame, eva); 856228753Smm return (-1); 857228753Smm } 858228753Smm 859228753Smm /* kludge to pass faulting virtual address to sendsig */ 860228753Smm frame->tf_err = eva; 861228753Smm 862228753Smm return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV); 863228753Smm} 864228753Smm 865228753Smmstatic void 866228753Smmtrap_fatal(frame, eva) 867228753Smm struct trapframe *frame; 868228753Smm vm_offset_t eva; 869228753Smm{ 870228753Smm int code, type, ss, esp; 871228753Smm struct soft_segment_descriptor softseg; 872228753Smm 873228753Smm code = frame->tf_err; 874228753Smm type = frame->tf_trapno; 875228753Smm sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg); 876228753Smm 877228753Smm if (type <= MAX_TRAP_MSG) 878228753Smm printf("\n\nFatal trap %d: %s while in %s mode\n", 879228753Smm type, trap_msg[type], 880228753Smm frame->tf_eflags & PSL_VM ? "vm86" : 881228753Smm ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel"); 882228753Smm#ifdef SMP 883228753Smm /* two seperate prints in case of a trap on an unmapped page */ 884228753Smm printf("cpuid = %d; ", cpuid); 885228753Smm printf("lapic.id = %08x\n", lapic.id); 886228753Smm#endif 887228753Smm if (type == T_PAGEFLT) { 888228753Smm printf("fault virtual address = 0x%x\n", eva); 889228753Smm printf("fault code = %s %s, %s\n", 890228753Smm code & PGEX_U ? "user" : "supervisor", 891228753Smm code & PGEX_W ? "write" : "read", 892228753Smm code & PGEX_P ? "protection violation" : "page not present"); 893228753Smm } 894228753Smm printf("instruction pointer = 0x%x:0x%x\n", 895228753Smm frame->tf_cs & 0xffff, frame->tf_eip); 896228753Smm if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) { 897228753Smm ss = frame->tf_ss & 0xffff; 898228753Smm esp = frame->tf_esp; 899228753Smm } else { 900228753Smm ss = GSEL(GDATA_SEL, SEL_KPL); 901228753Smm esp = (int)&frame->tf_esp; 902228753Smm } 903228753Smm printf("stack pointer = 0x%x:0x%x\n", ss, esp); 904228753Smm printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp); 905228753Smm printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n", 906228753Smm softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type); 907228753Smm printf(" = DPL %d, pres %d, def32 %d, gran %d\n", 908228753Smm softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32, 909228753Smm softseg.ssd_gran); 910228753Smm printf("processor eflags = "); 911228753Smm if (frame->tf_eflags & PSL_T) 912228753Smm printf("trace trap, "); 913228753Smm if (frame->tf_eflags & PSL_I) 914228753Smm printf("interrupt enabled, "); 915228753Smm if (frame->tf_eflags & PSL_NT) 916228753Smm printf("nested task, "); 917228753Smm if (frame->tf_eflags & PSL_RF) 918228753Smm printf("resume, "); 919228753Smm if (frame->tf_eflags & PSL_VM) 920228753Smm printf("vm86, "); 921228753Smm printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12); 922228753Smm printf("current process = "); 923228753Smm if (curproc) { 924228753Smm printf("%lu (%s)\n", 925228753Smm (u_long)curproc->p_pid, curproc->p_comm ? 926228753Smm curproc->p_comm : ""); 927228753Smm } else { 928228753Smm printf("Idle\n"); 929228753Smm } 930228753Smm 931228753Smm#ifdef KDB 932228753Smm if (kdb_trap(&psl)) 933228753Smm return; 934228753Smm#endif 935228753Smm#ifdef DDB 936228753Smm if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame)) 937228753Smm return; 938228753Smm#endif 939228753Smm printf("trap number = %d\n", type); 940228753Smm if (type <= MAX_TRAP_MSG) 941228753Smm panic(trap_msg[type]); 942228753Smm else 943228753Smm panic("unknown/reserved trap"); 944228753Smm} 945228753Smm 946228753Smm/* 947228753Smm * Double fault handler. Called when a fault occurs while writing 948228753Smm * a frame for a trap/exception onto the stack. This usually occurs 949228753Smm * when the stack overflows (such is the case with infinite recursion, 950228753Smm * for example). 951228753Smm * 952228753Smm * XXX Note that the current PTD gets replaced by IdlePTD when the 953228753Smm * task switch occurs. This means that the stack that was active at 954228753Smm * the time of the double fault is not available at <kstack> unless 955228753Smm * the machine was idle when the double fault occurred. The downside 956228753Smm * of this is that "trace <ebp>" in ddb won't work. 957228753Smm */ 958228753Smmvoid 959228753Smmdblfault_handler() 960228753Smm{ 961228753Smm printf("\nFatal double fault:\n"); 962228753Smm printf("eip = 0x%x\n", common_tss.tss_eip); 963228753Smm printf("esp = 0x%x\n", common_tss.tss_esp); 964228753Smm printf("ebp = 0x%x\n", common_tss.tss_ebp); 965228753Smm#ifdef SMP 966228753Smm /* two seperate prints in case of a trap on an unmapped page */ 967228753Smm printf("cpuid = %d; ", cpuid); 968228753Smm printf("lapic.id = %08x\n", lapic.id); 969228753Smm#endif 970228753Smm panic("double fault"); 971228753Smm} 972228753Smm 973228753Smm/* 974228753Smm * Compensate for 386 brain damage (missing URKR). 975228753Smm * This is a little simpler than the pagefault handler in trap() because 976228753Smm * it the page tables have already been faulted in and high addresses 977228753Smm * are thrown out early for other reasons. 978228753Smm */ 979228753Smmint trapwrite(addr) 980228753Smm unsigned addr; 981228753Smm{ 982228753Smm struct proc *p; 983228753Smm vm_offset_t va; 984228753Smm struct vmspace *vm; 985228753Smm int rv; 986228753Smm 987228753Smm va = trunc_page((vm_offset_t)addr); 988228753Smm /* 989228753Smm * XXX - MAX is END. Changed > to >= for temp. fix. 990228753Smm */ 991228753Smm if (va >= VM_MAXUSER_ADDRESS) 992228753Smm return (1); 993228753Smm 994228753Smm p = curproc; 995228753Smm vm = p->p_vmspace; 996228753Smm 997228753Smm ++p->p_lock; 998228753Smm 999228753Smm if (!grow_stack (p, va)) { 1000228776Smm --p->p_lock; 1001228753Smm return (1); 1002228753Smm } 1003228753Smm 1004228753Smm /* 1005228753Smm * fault the data page 1006228753Smm */ 1007228753Smm rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY); 1008228753Smm 1009228753Smm --p->p_lock; 1010228776Smm 1011228753Smm if (rv != KERN_SUCCESS) 1012228753Smm return 1; 1013228753Smm 1014228753Smm return (0); 1015228753Smm} 1016228753Smm 1017228753Smm/* 1018228753Smm * syscall2 - MP aware system call request C handler 1019228753Smm * 1020228753Smm * A system call is essentially treated as a trap except that the 1021228753Smm * MP lock is not held on entry or return. We are responsible for 1022228753Smm * obtaining the MP lock if necessary and for handling ASTs 1023228753Smm * (e.g. a task switch) prior to return. 1024228753Smm * 1025228753Smm * In general, only simple access and manipulation of curproc and 1026228753Smm * the current stack is allowed without having to hold MP lock. 1027228753Smm */ 1028228753Smmvoid 1029228753Smmsyscall2(frame) 1030228753Smm struct trapframe frame; 1031228753Smm{ 1032228753Smm caddr_t params; 1033228753Smm int i; 1034228753Smm struct sysent *callp; 1035228753Smm struct proc *p = curproc; 1036228776Smm u_quad_t sticks; 1037228776Smm int error; 1038228776Smm int narg; 1039228753Smm int args[8]; 1040228753Smm int have_giant = 0; 1041228753Smm u_int code; 1042228753Smm 1043228753Smm atomic_add_int(&cnt.v_syscall, 1); 1044228753Smm 1045228753Smm#ifdef DIAGNOSTIC 1046228753Smm if (ISPL(frame.tf_cs) != SEL_UPL) { 1047228753Smm mtx_enter(&Giant, MTX_DEF); 1048228776Smm panic("syscall"); 1049228753Smm /* NOT REACHED */ 1050228753Smm } 1051228753Smm#endif 1052228753Smm 1053228753Smm /* 1054228753Smm * handle atomicy by looping since interrupts are enabled and the 1055228753Smm * MP lock is not held. 1056228753Smm */ 1057228753Smm sticks = ((volatile struct proc *)p)->p_sticks; 1058228753Smm while (sticks != ((volatile struct proc *)p)->p_sticks) 1059228753Smm sticks = ((volatile struct proc *)p)->p_sticks; 1060228753Smm 1061228753Smm p->p_md.md_regs = &frame; 1062228753Smm params = (caddr_t)frame.tf_esp + sizeof(int); 1063228753Smm code = frame.tf_eax; 1064228753Smm 1065228753Smm if (p->p_sysent->sv_prepsyscall) { 1066228753Smm /* 1067228753Smm * The prep code is not MP aware. 1068228753Smm */ 1069228753Smm mtx_enter(&Giant, MTX_DEF); 1070228753Smm (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms); 1071228753Smm mtx_exit(&Giant, MTX_DEF); 1072228753Smm } else { 1073228753Smm /* 1074228753Smm * Need to check if this is a 32 bit or 64 bit syscall. 1075228753Smm * fuword is MP aware. 1076228753Smm */ 1077228753Smm if (code == SYS_syscall) { 1078228753Smm /* 1079228753Smm * Code is first argument, followed by actual args. 1080228753Smm */ 1081228753Smm code = fuword(params); 1082228753Smm params += sizeof(int); 1083228753Smm } else if (code == SYS___syscall) { 1084228753Smm /* 1085228753Smm * Like syscall, but code is a quad, so as to maintain 1086228753Smm * quad alignment for the rest of the arguments. 1087228753Smm */ 1088228753Smm code = fuword(params); 1089228753Smm params += sizeof(quad_t); 1090228753Smm } 1091228753Smm } 1092228753Smm 1093228753Smm if (p->p_sysent->sv_mask) 1094228753Smm code &= p->p_sysent->sv_mask; 1095228753Smm 1096228753Smm if (code >= p->p_sysent->sv_size) 1097228753Smm callp = &p->p_sysent->sv_table[0]; 1098228753Smm else 1099228753Smm callp = &p->p_sysent->sv_table[code]; 1100228753Smm 1101228753Smm narg = callp->sy_narg & SYF_ARGMASK; 1102228753Smm 1103228753Smm /* 1104228753Smm * copyin is MP aware, but the tracing code is not 1105228753Smm */ 1106228753Smm if (params && (i = narg * sizeof(int)) && 1107228753Smm (error = copyin(params, (caddr_t)args, (u_int)i))) { 1108228753Smm mtx_enter(&Giant, MTX_DEF); 1109228753Smm have_giant = 1; 1110228753Smm#ifdef KTRACE 1111228753Smm if (KTRPOINT(p, KTR_SYSCALL)) 1112228753Smm ktrsyscall(p->p_tracep, code, narg, args); 1113228753Smm#endif 1114228753Smm goto bad; 1115228753Smm } 1116228753Smm 1117228753Smm /* 1118228753Smm * Try to run the syscall without the MP lock if the syscall 1119228753Smm * is MP safe. We have to obtain the MP lock no matter what if 1120228753Smm * we are ktracing 1121228753Smm */ 1122228753Smm if ((callp->sy_narg & SYF_MPSAFE) == 0) { 1123228753Smm mtx_enter(&Giant, MTX_DEF); 1124228753Smm have_giant = 1; 1125228753Smm } 1126 1127#ifdef KTRACE 1128 if (KTRPOINT(p, KTR_SYSCALL)) { 1129 if (have_giant == 0) { 1130 mtx_enter(&Giant, MTX_DEF); 1131 have_giant = 1; 1132 } 1133 ktrsyscall(p->p_tracep, code, narg, args); 1134 } 1135#endif 1136 p->p_retval[0] = 0; 1137 p->p_retval[1] = frame.tf_edx; 1138 1139 STOPEVENT(p, S_SCE, narg); /* MP aware */ 1140 1141 error = (*callp->sy_call)(p, args); 1142 1143 /* 1144 * MP SAFE (we may or may not have the MP lock at this point) 1145 */ 1146 switch (error) { 1147 case 0: 1148 /* 1149 * Reinitialize proc pointer `p' as it may be different 1150 * if this is a child returning from fork syscall. 1151 */ 1152 p = curproc; 1153 frame.tf_eax = p->p_retval[0]; 1154 frame.tf_edx = p->p_retval[1]; 1155 frame.tf_eflags &= ~PSL_C; 1156 break; 1157 1158 case ERESTART: 1159 /* 1160 * Reconstruct pc, assuming lcall $X,y is 7 bytes, 1161 * int 0x80 is 2 bytes. We saved this in tf_err. 1162 */ 1163 frame.tf_eip -= frame.tf_err; 1164 break; 1165 1166 case EJUSTRETURN: 1167 break; 1168 1169 default: 1170bad: 1171 if (p->p_sysent->sv_errsize) { 1172 if (error >= p->p_sysent->sv_errsize) 1173 error = -1; /* XXX */ 1174 else 1175 error = p->p_sysent->sv_errtbl[error]; 1176 } 1177 frame.tf_eax = error; 1178 frame.tf_eflags |= PSL_C; 1179 break; 1180 } 1181 1182 /* 1183 * Traced syscall. trapsignal() is not MP aware. 1184 */ 1185 if ((frame.tf_eflags & PSL_T) && !(frame.tf_eflags & PSL_VM)) { 1186 if (have_giant == 0) { 1187 mtx_enter(&Giant, MTX_DEF); 1188 have_giant = 1; 1189 } 1190 frame.tf_eflags &= ~PSL_T; 1191 trapsignal(p, SIGTRAP, 0); 1192 } 1193 1194 /* 1195 * Handle reschedule and other end-of-syscall issues 1196 */ 1197 have_giant = userret(p, &frame, sticks, have_giant); 1198 1199#ifdef KTRACE 1200 if (KTRPOINT(p, KTR_SYSRET)) { 1201 if (have_giant == 0) { 1202 mtx_enter(&Giant, MTX_DEF); 1203 have_giant = 1; 1204 } 1205 ktrsysret(p->p_tracep, code, error, p->p_retval[0]); 1206 } 1207#endif 1208 1209 /* 1210 * This works because errno is findable through the 1211 * register set. If we ever support an emulation where this 1212 * is not the case, this code will need to be revisited. 1213 */ 1214 STOPEVENT(p, S_SCX, code); 1215 1216 /* 1217 * Release the MP lock if we had to get it 1218 */ 1219 if (have_giant) 1220 mtx_exit(&Giant, MTX_DEF); 1221 1222 mtx_assert(&sched_lock, MA_NOTOWNED); 1223 mtx_assert(&Giant, MA_NOTOWNED); 1224} 1225 1226void 1227ast(frame) 1228 struct trapframe frame; 1229{ 1230 struct proc *p = CURPROC; 1231 u_quad_t sticks; 1232 1233 /* 1234 * handle atomicy by looping since interrupts are enabled and the 1235 * MP lock is not held. 1236 */ 1237 sticks = ((volatile struct proc *)p)->p_sticks; 1238 while (sticks != ((volatile struct proc *)p)->p_sticks) 1239 sticks = ((volatile struct proc *)p)->p_sticks; 1240 1241 astoff(); 1242 atomic_add_int(&cnt.v_soft, 1); 1243 if (p->p_flag & P_OWEUPC) { 1244 mtx_enter(&Giant, MTX_DEF); 1245 p->p_flag &= ~P_OWEUPC; 1246 addupc_task(p, p->p_stats->p_prof.pr_addr, 1247 p->p_stats->p_prof.pr_ticks); 1248 } 1249 if (p->p_flag & P_ALRMPEND) { 1250 if (!mtx_owned(&Giant)) 1251 mtx_enter(&Giant, MTX_DEF); 1252 p->p_flag &= ~P_ALRMPEND; 1253 psignal(p, SIGVTALRM); 1254 } 1255 if (p->p_flag & P_PROFPEND) { 1256 if (!mtx_owned(&Giant)) 1257 mtx_enter(&Giant, MTX_DEF); 1258 p->p_flag &= ~P_PROFPEND; 1259 psignal(p, SIGPROF); 1260 } 1261 if (userret(p, &frame, sticks, mtx_owned(&Giant)) != 0) 1262 mtx_exit(&Giant, MTX_DEF); 1263} 1264 1265/* 1266 * Simplified back end of syscall(), used when returning from fork() 1267 * directly into user mode. Giant is not held on entry, and must not 1268 * be held on return. 1269 */ 1270void 1271fork_return(p, frame) 1272 struct proc *p; 1273 struct trapframe frame; 1274{ 1275 int have_giant; 1276 1277 frame.tf_eax = 0; /* Child returns zero */ 1278 frame.tf_eflags &= ~PSL_C; /* success */ 1279 frame.tf_edx = 1; 1280 1281 have_giant = userret(p, &frame, 0, mtx_owned(&Giant)); 1282#ifdef KTRACE 1283 if (KTRPOINT(p, KTR_SYSRET)) { 1284 if (have_giant == 0) { 1285 mtx_enter(&Giant, MTX_DEF); 1286 have_giant = 1; 1287 } 1288 ktrsysret(p->p_tracep, SYS_fork, 0, 0); 1289 } 1290#endif 1291 if (have_giant) 1292 mtx_exit(&Giant, MTX_DEF); 1293} 1294