subr_syscall.c revision 173601
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 173601 2007-11-14 06:51:33Z julian $"); 42116182Sobrien 43118240Speter#include "opt_ktrace.h" 44104338Srwatson#include "opt_mac.h" 4578983Sjhb#ifdef __i386__ 4671257Speter#include "opt_npx.h" 4778983Sjhb#endif 48170640Sjeff#include "opt_sched.h" 4913203Swollman 501549Srgrimes#include <sys/param.h> 5165557Sjasone#include <sys/bus.h> 521549Srgrimes#include <sys/kernel.h> 5378983Sjhb#include <sys/lock.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 70163606Srwatson#include <security/mac/mac_framework.h> 71163606Srwatson 7278983Sjhb/* 73167211Srwatson * Define the code needed before returning to user mode, for trap and 74167211Srwatson * syscall. 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, 82173601Sjulian td->td_name); 83126661Srwatson#ifdef DIAGNOSTIC 84110190Sjulian /* Check that we called signotify() enough. */ 8578636Sjhb PROC_LOCK(p); 86170307Sjeff thread_lock(td); 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"); 90170307Sjeff thread_unlock(td); 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 117163709Sjb#ifdef KSE 118105974Sjulian /* 119105974Sjulian * Do special thread processing, e.g. upcall tweaking and such. 120105974Sjulian */ 121126932Speter if (p->p_flag & P_SA) 122103838Sjulian thread_userret(td, frame); 123163709Sjb#endif 124110190Sjulian 125110190Sjulian /* 126110190Sjulian * Charge system time if profiling. 127110190Sjulian */ 128113874Sjhb if (p->p_flag & P_PROFIL) { 129110190Sjulian 130155455Sphk addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio); 131110190Sjulian } 132139452Sjhb 133139324Sjeff /* 134139324Sjeff * Let the scheduler adjust our priority etc. 135139324Sjeff */ 136139324Sjeff sched_userret(td); 137144061Sjeff KASSERT(td->td_locks == 0, 138144061Sjeff ("userret: Returning with %d locks held.", td->td_locks)); 1391690Sdg} 1401690Sdg 1414Srgrimes/* 14278983Sjhb * Process an asynchronous software trap. 14378983Sjhb * This is relatively easy. 14481493Sjhb * This function will return with preemption disabled. 1454Srgrimes */ 146798Swollmanvoid 14799072Sjulianast(struct trapframe *framep) 14865557Sjasone{ 149104297Sjhb struct thread *td; 150104297Sjhb struct proc *p; 15183366Sjulian int flags; 15293793Sbde int sig; 15377015Sbde#if defined(DEV_NPX) && !defined(SMP) 15477015Sbde int ucode; 155151316Sdavidxu ksiginfo_t ksi; 15677015Sbde#endif 15765557Sjasone 158104297Sjhb td = curthread; 159104297Sjhb p = td->td_proc; 160104378Sjmallett 16199072Sjulian CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid, 16299072Sjulian p->p_comm); 16372911Sjhb KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode")); 164111883Sjhb WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode"); 16581493Sjhb mtx_assert(&Giant, MA_NOTOWNED); 166170307Sjeff THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 16793390Sjake td->td_frame = framep; 168155455Sphk td->td_pticks = 0; 169104297Sjhb 170163709Sjb#ifdef KSE 171133340Sdavidxu if ((p->p_flag & P_SA) && (td->td_mailbox == NULL)) 172134571Sjulian thread_user_enter(td); 173163709Sjb#endif 174135573Sjhb 17593390Sjake /* 176172207Sjeff * This updates the td_flag'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 179172207Sjeff * AST's saved in flags, the astpending flag will be set and 18093390Sjake * ast() will be called again. 18193390Sjake */ 182170307Sjeff thread_lock(td); 183170307Sjeff flags = td->td_flags; 184112888Sjeff td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | 185172207Sjeff TDF_NEEDRESCHED | TDF_INTERRUPT | TDF_ALRMPEND | TDF_PROFPEND | 186172207Sjeff TDF_MACPEND); 187170307Sjeff thread_unlock(td); 188170292Sattilio PCPU_INC(cnt.v_trap); 189135573Sjhb 19099072Sjulian /* 19199072Sjulian * XXXKSE While the fact that we owe a user profiling 192164936Sjulian * tick is stored per thread in this code, the statistics 19399072Sjulian * themselves are still stored per process. 19499072Sjulian * This should probably change, by which I mean that 19599072Sjulian * possibly the location of both might change. 19699072Sjulian */ 19793390Sjake if (td->td_ucred != p->p_ucred) 19893390Sjake cred_update_thread(td); 199132266Sjhb if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) { 200132266Sjhb addupc_task(td, td->td_profil_addr, td->td_profil_ticks); 201132266Sjhb td->td_profil_ticks = 0; 202132266Sjhb td->td_pflags &= ~TDP_OWEUPC; 203131437Sjhb } 204172207Sjeff if (flags & TDF_ALRMPEND) { 20593390Sjake PROC_LOCK(p); 20693390Sjake psignal(p, SIGVTALRM); 20793390Sjake PROC_UNLOCK(p); 20893390Sjake } 20977015Sbde#if defined(DEV_NPX) && !defined(SMP) 21093390Sjake if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) { 21193390Sjake atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags, 21293390Sjake PCB_NPXTRAP); 21393390Sjake ucode = npxtrap(); 21493390Sjake if (ucode != -1) { 215151316Sdavidxu ksiginfo_init_trap(&ksi); 216151316Sdavidxu ksi.ksi_signo = SIGFPE; 217151316Sdavidxu ksi.ksi_code = ucode; 218151316Sdavidxu trapsignal(td, &ksi); 21977015Sbde } 22093390Sjake } 22177015Sbde#endif 222172207Sjeff if (flags & TDF_PROFPEND) { 22393390Sjake PROC_LOCK(p); 22493390Sjake psignal(p, SIGPROF); 22593390Sjake PROC_UNLOCK(p); 22693390Sjake } 227106655Srwatson#ifdef MAC 228172207Sjeff if (flags & TDF_MACPEND) 229106655Srwatson mac_thread_userret(td); 230106655Srwatson#endif 231111032Sjulian if (flags & TDF_NEEDRESCHED) { 232118240Speter#ifdef KTRACE 233118240Speter if (KTRPOINT(td, KTR_CSW)) 234119781Speter ktrcsw(1, 1); 235118240Speter#endif 236170307Sjeff thread_lock(td); 237163709Sjb sched_prio(td, td->td_user_pri); 238170307Sjeff SCHED_STAT_INC(switch_needresched); 239131473Sjhb mi_switch(SW_INVOL, NULL); 240170307Sjeff thread_unlock(td); 241118240Speter#ifdef KTRACE 242118240Speter if (KTRPOINT(td, KTR_CSW)) 243119781Speter ktrcsw(0, 1); 244118240Speter#endif 24593793Sbde } 246112888Sjeff if (flags & TDF_NEEDSIGCHK) { 24793793Sbde PROC_LOCK(p); 248114983Sjhb mtx_lock(&p->p_sigacts->ps_mtx); 249116963Sdavidxu while ((sig = cursig(td)) != 0) 25093793Sbde postsig(sig); 251114983Sjhb mtx_unlock(&p->p_sigacts->ps_mtx); 25293793Sbde PROC_UNLOCK(p); 25393793Sbde } 25465557Sjasone 255155455Sphk userret(td, framep); 25681493Sjhb mtx_assert(&Giant, MA_NOTOWNED); 25724691Speter} 258