subr_syscall.c revision 155455
14Srgrimes/*- 21690Sdg * Copyright (C) 1994, David Greenman 31690Sdg * Copyright (c) 1990, 1993 41690Sdg * The Regents of the University of California. All rights reserved. 54Srgrimes * 64Srgrimes * This code is derived from software contributed to Berkeley by 74Srgrimes * the University of Utah, and William Jolitz. 84Srgrimes * 94Srgrimes * Redistribution and use in source and binary forms, with or without 104Srgrimes * modification, are permitted provided that the following conditions 114Srgrimes * are met: 124Srgrimes * 1. Redistributions of source code must retain the above copyright 134Srgrimes * notice, this list of conditions and the following disclaimer. 144Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 154Srgrimes * notice, this list of conditions and the following disclaimer in the 164Srgrimes * documentation and/or other materials provided with the distribution. 174Srgrimes * 3. All advertising materials mentioning features or use of this software 184Srgrimes * must display the following acknowledgement: 194Srgrimes * This product includes software developed by the University of 204Srgrimes * California, Berkeley and its contributors. 214Srgrimes * 4. Neither the name of the University nor the names of its contributors 224Srgrimes * may be used to endorse or promote products derived from this software 234Srgrimes * without specific prior written permission. 244Srgrimes * 254Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 264Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 274Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 284Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 294Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 304Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 314Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 324Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 334Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 344Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 354Srgrimes * SUCH DAMAGE. 364Srgrimes * 37608Srgrimes * from: @(#)trap.c 7.4 (Berkeley) 5/13/91 384Srgrimes */ 394Srgrimes 40116182Sobrien#include <sys/cdefs.h> 41116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 155455 2006-02-08 08:09:17Z phk $"); 42116182Sobrien 43118240Speter#include "opt_ktrace.h" 44104338Srwatson#include "opt_mac.h" 4578983Sjhb#ifdef __i386__ 4671257Speter#include "opt_npx.h" 4778983Sjhb#endif 4813203Swollman 491549Srgrimes#include <sys/param.h> 5065557Sjasone#include <sys/bus.h> 511549Srgrimes#include <sys/kernel.h> 5278983Sjhb#include <sys/lock.h> 53104338Srwatson#include <sys/mac.h> 5467365Sjhb#include <sys/mutex.h> 5578983Sjhb#include <sys/proc.h> 5699072Sjulian#include <sys/ktr.h> 5731389Sbde#include <sys/resourcevar.h> 58104964Sjeff#include <sys/sched.h> 5931389Sbde#include <sys/signalvar.h> 6078983Sjhb#include <sys/systm.h> 6112662Sdg#include <sys/vmmeter.h> 62118240Speter#ifdef KTRACE 63118240Speter#include <sys/uio.h> 64118240Speter#include <sys/ktrace.h> 65118240Speter#endif 66118240Speter 671549Srgrimes#include <machine/cpu.h> 6831389Sbde#include <machine/pcb.h> 691549Srgrimes 7078983Sjhb/* 7178983Sjhb * Define the code needed before returning to user mode, for 7278983Sjhb * trap and syscall. 7382585Sdillon * 7482585Sdillon * MPSAFE 7578983Sjhb */ 7671527Sjhbvoid 77155455Sphkuserret(struct thread *td, struct trapframe *frame) 781690Sdg{ 7983366Sjulian struct proc *p = td->td_proc; 80757Sdg 8199072Sjulian CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid, 8299072Sjulian p->p_comm); 83126661Srwatson#ifdef DIAGNOSTIC 84110190Sjulian /* Check that we called signotify() enough. */ 8578636Sjhb PROC_LOCK(p); 8693793Sbde mtx_lock_spin(&sched_lock); 87112888Sjeff if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 || 88111032Sjulian (td->td_flags & TDF_ASTPENDING) == 0)) 89102266Srwatson printf("failed to set signal flags properly for ast()\n"); 9093793Sbde mtx_unlock_spin(&sched_lock); 9182585Sdillon PROC_UNLOCK(p); 9293793Sbde#endif 9328013Sdyson 94152376Srwatson#ifdef KTRACE 95152376Srwatson KTRUSERRET(td); 96152376Srwatson#endif 97152376Srwatson 9893793Sbde /* 99136837Sphk * If this thread tickled GEOM, we need to wait for the giggling to 100136837Sphk * stop before we return to userland 101136837Sphk */ 102136837Sphk if (td->td_pflags & TDP_GEOM) 103136837Sphk g_waitidle(); 104136837Sphk 105136837Sphk /* 106105974Sjulian * We need to check to see if we have to exit or wait due to a 107105974Sjulian * single threading requirement or some other STOP condition. 108105974Sjulian * Don't bother doing all the work if the stop bits are not set 109105974Sjulian * at this time.. If we miss it, we miss it.. no big deal. 11099072Sjulian */ 111105974Sjulian if (P_SHOULDSTOP(p)) { 112105974Sjulian PROC_LOCK(p); 113105974Sjulian thread_suspend_check(0); /* Can suspend or kill */ 114105974Sjulian PROC_UNLOCK(p); 115105974Sjulian } 116105974Sjulian 117105974Sjulian /* 118105974Sjulian * Do special thread processing, e.g. upcall tweaking and such. 119105974Sjulian */ 120126932Speter if (p->p_flag & P_SA) 121103838Sjulian thread_userret(td, frame); 122110190Sjulian 123110190Sjulian /* 124110190Sjulian * Charge system time if profiling. 125110190Sjulian */ 126113874Sjhb if (p->p_flag & P_PROFIL) { 127110190Sjulian 128155455Sphk addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio); 129110190Sjulian } 130139452Sjhb 131139324Sjeff /* 132139324Sjeff * Let the scheduler adjust our priority etc. 133139324Sjeff */ 134139324Sjeff sched_userret(td); 135144061Sjeff KASSERT(td->td_locks == 0, 136144061Sjeff ("userret: Returning with %d locks held.", td->td_locks)); 1371690Sdg} 1381690Sdg 1394Srgrimes/* 14078983Sjhb * Process an asynchronous software trap. 14178983Sjhb * This is relatively easy. 14281493Sjhb * This function will return with preemption disabled. 1434Srgrimes */ 144798Swollmanvoid 14599072Sjulianast(struct trapframe *framep) 14665557Sjasone{ 147104297Sjhb struct thread *td; 148104297Sjhb struct proc *p; 149104297Sjhb struct ksegrp *kg; 150125454Sjhb struct rlimit rlim; 15181493Sjhb int sflag; 15283366Sjulian int flags; 15393793Sbde int sig; 15477015Sbde#if defined(DEV_NPX) && !defined(SMP) 15577015Sbde int ucode; 156151316Sdavidxu ksiginfo_t ksi; 15777015Sbde#endif 15865557Sjasone 159104297Sjhb td = curthread; 160104297Sjhb p = td->td_proc; 161104378Sjmallett kg = td->td_ksegrp; 162104378Sjmallett 16399072Sjulian CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid, 16499072Sjulian p->p_comm); 16572911Sjhb KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode")); 166111883Sjhb WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode"); 16781493Sjhb mtx_assert(&Giant, MA_NOTOWNED); 16893793Sbde mtx_assert(&sched_lock, MA_NOTOWNED); 16993390Sjake td->td_frame = framep; 170155455Sphk td->td_pticks = 0; 171104297Sjhb 172133340Sdavidxu if ((p->p_flag & P_SA) && (td->td_mailbox == NULL)) 173134571Sjulian thread_user_enter(td); 174135573Sjhb 17593390Sjake /* 17693390Sjake * This updates the p_sflag's for the checks below in one 17793390Sjake * "atomic" operation with turning off the astpending flag. 17893390Sjake * If another AST is triggered while we are handling the 17993390Sjake * AST's saved in sflag, the astpending flag will be set and 18093390Sjake * ast() will be called again. 18193390Sjake */ 18293390Sjake mtx_lock_spin(&sched_lock); 183111032Sjulian flags = td->td_flags; 18493390Sjake sflag = p->p_sflag; 185112888Sjeff p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU); 186106655Srwatson#ifdef MAC 187106655Srwatson p->p_sflag &= ~PS_MACPEND; 188106655Srwatson#endif 189112888Sjeff td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | 190132266Sjhb TDF_NEEDRESCHED | TDF_INTERRUPT); 19193390Sjake cnt.v_soft++; 19293390Sjake mtx_unlock_spin(&sched_lock); 193135573Sjhb 19499072Sjulian /* 19599072Sjulian * XXXKSE While the fact that we owe a user profiling 19699072Sjulian * tick is stored per KSE in this code, the statistics 19799072Sjulian * themselves are still stored per process. 19899072Sjulian * This should probably change, by which I mean that 19999072Sjulian * possibly the location of both might change. 20099072Sjulian */ 20193390Sjake if (td->td_ucred != p->p_ucred) 20293390Sjake cred_update_thread(td); 203132266Sjhb if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) { 204132266Sjhb addupc_task(td, td->td_profil_addr, td->td_profil_ticks); 205132266Sjhb td->td_profil_ticks = 0; 206132266Sjhb td->td_pflags &= ~TDP_OWEUPC; 207131437Sjhb } 20893390Sjake if (sflag & PS_ALRMPEND) { 20993390Sjake PROC_LOCK(p); 21093390Sjake psignal(p, SIGVTALRM); 21193390Sjake PROC_UNLOCK(p); 21293390Sjake } 21377015Sbde#if defined(DEV_NPX) && !defined(SMP) 21493390Sjake if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) { 21593390Sjake atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags, 21693390Sjake PCB_NPXTRAP); 21793390Sjake ucode = npxtrap(); 21893390Sjake if (ucode != -1) { 219151316Sdavidxu ksiginfo_init_trap(&ksi); 220151316Sdavidxu ksi.ksi_signo = SIGFPE; 221151316Sdavidxu ksi.ksi_code = ucode; 222151316Sdavidxu trapsignal(td, &ksi); 22377015Sbde } 22493390Sjake } 22577015Sbde#endif 22693390Sjake if (sflag & PS_PROFPEND) { 22793390Sjake PROC_LOCK(p); 22893390Sjake psignal(p, SIGPROF); 22993390Sjake PROC_UNLOCK(p); 23093390Sjake } 231104240Sjhb if (sflag & PS_XCPU) { 232104240Sjhb PROC_LOCK(p); 233125454Sjhb lim_rlimit(p, RLIMIT_CPU, &rlim); 234113636Sjhb mtx_lock_spin(&sched_lock); 235155444Sphk if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) { 236113636Sjhb mtx_unlock_spin(&sched_lock); 237104240Sjhb killproc(p, "exceeded maximum CPU limit"); 238113636Sjhb } else { 239125454Sjhb if (p->p_cpulimit < rlim.rlim_max) 240104719Sjhb p->p_cpulimit += 5; 241104719Sjhb mtx_unlock_spin(&sched_lock); 242113636Sjhb psignal(p, SIGXCPU); 243104240Sjhb } 244104240Sjhb PROC_UNLOCK(p); 245104240Sjhb } 246106655Srwatson#ifdef MAC 247106655Srwatson if (sflag & PS_MACPEND) 248106655Srwatson mac_thread_userret(td); 249106655Srwatson#endif 250111032Sjulian if (flags & TDF_NEEDRESCHED) { 251118240Speter#ifdef KTRACE 252118240Speter if (KTRPOINT(td, KTR_CSW)) 253119781Speter ktrcsw(1, 1); 254118240Speter#endif 25593793Sbde mtx_lock_spin(&sched_lock); 256104964Sjeff sched_prio(td, kg->kg_user_pri); 257131473Sjhb mi_switch(SW_INVOL, NULL); 25893793Sbde mtx_unlock_spin(&sched_lock); 259118240Speter#ifdef KTRACE 260118240Speter if (KTRPOINT(td, KTR_CSW)) 261119781Speter ktrcsw(0, 1); 262118240Speter#endif 26393793Sbde } 264112888Sjeff if (flags & TDF_NEEDSIGCHK) { 26593793Sbde PROC_LOCK(p); 266114983Sjhb mtx_lock(&p->p_sigacts->ps_mtx); 267116963Sdavidxu while ((sig = cursig(td)) != 0) 26893793Sbde postsig(sig); 269114983Sjhb mtx_unlock(&p->p_sigacts->ps_mtx); 27093793Sbde PROC_UNLOCK(p); 27193793Sbde } 27265557Sjasone 273155455Sphk userret(td, framep); 27481493Sjhb mtx_assert(&Giant, MA_NOTOWNED); 27524691Speter} 276