subr_syscall.c revision 170640
1/*- 2 * Copyright (C) 1994, David Greenman 3 * Copyright (c) 1990, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the University of Utah, and William Jolitz. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91 38 */ 39 40#include <sys/cdefs.h> 41__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 170640 2007-06-12 23:27:31Z jeff $"); 42 43#include "opt_ktrace.h" 44#include "opt_mac.h" 45#ifdef __i386__ 46#include "opt_npx.h" 47#endif 48#include "opt_sched.h" 49 50#include <sys/param.h> 51#include <sys/bus.h> 52#include <sys/kernel.h> 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <sys/proc.h> 56#include <sys/ktr.h> 57#include <sys/resourcevar.h> 58#include <sys/sched.h> 59#include <sys/signalvar.h> 60#include <sys/systm.h> 61#include <sys/vmmeter.h> 62#ifdef KTRACE 63#include <sys/uio.h> 64#include <sys/ktrace.h> 65#endif 66 67#include <machine/cpu.h> 68#include <machine/pcb.h> 69 70#include <security/mac/mac_framework.h> 71 72/* 73 * Define the code needed before returning to user mode, for trap and 74 * syscall. 75 */ 76void 77userret(struct thread *td, struct trapframe *frame) 78{ 79 struct proc *p = td->td_proc; 80 81 CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid, 82 p->p_comm); 83#ifdef DIAGNOSTIC 84 /* Check that we called signotify() enough. */ 85 PROC_LOCK(p); 86 thread_lock(td); 87 if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 || 88 (td->td_flags & TDF_ASTPENDING) == 0)) 89 printf("failed to set signal flags properly for ast()\n"); 90 thread_unlock(td); 91 PROC_UNLOCK(p); 92#endif 93 94#ifdef KTRACE 95 KTRUSERRET(td); 96#endif 97 98 /* 99 * If this thread tickled GEOM, we need to wait for the giggling to 100 * stop before we return to userland 101 */ 102 if (td->td_pflags & TDP_GEOM) 103 g_waitidle(); 104 105 /* 106 * We need to check to see if we have to exit or wait due to a 107 * single threading requirement or some other STOP condition. 108 * Don't bother doing all the work if the stop bits are not set 109 * at this time.. If we miss it, we miss it.. no big deal. 110 */ 111 if (P_SHOULDSTOP(p)) { 112 PROC_LOCK(p); 113 thread_suspend_check(0); /* Can suspend or kill */ 114 PROC_UNLOCK(p); 115 } 116 117#ifdef KSE 118 /* 119 * Do special thread processing, e.g. upcall tweaking and such. 120 */ 121 if (p->p_flag & P_SA) 122 thread_userret(td, frame); 123#endif 124 125 /* 126 * Charge system time if profiling. 127 */ 128 if (p->p_flag & P_PROFIL) { 129 130 addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio); 131 } 132 133 /* 134 * Let the scheduler adjust our priority etc. 135 */ 136 sched_userret(td); 137 KASSERT(td->td_locks == 0, 138 ("userret: Returning with %d locks held.", td->td_locks)); 139} 140 141/* 142 * Process an asynchronous software trap. 143 * This is relatively easy. 144 * This function will return with preemption disabled. 145 */ 146void 147ast(struct trapframe *framep) 148{ 149 struct thread *td; 150 struct proc *p; 151 int sflag; 152 int flags; 153 int sig; 154#if defined(DEV_NPX) && !defined(SMP) 155 int ucode; 156 ksiginfo_t ksi; 157#endif 158 159 td = curthread; 160 p = td->td_proc; 161 162 CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid, 163 p->p_comm); 164 KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode")); 165 WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode"); 166 mtx_assert(&Giant, MA_NOTOWNED); 167 THREAD_LOCK_ASSERT(td, MA_NOTOWNED); 168 td->td_frame = framep; 169 td->td_pticks = 0; 170 171#ifdef KSE 172 if ((p->p_flag & P_SA) && (td->td_mailbox == NULL)) 173 thread_user_enter(td); 174#endif 175 176 /* 177 * This updates the p_sflag's for the checks below in one 178 * "atomic" operation with turning off the astpending flag. 179 * If another AST is triggered while we are handling the 180 * AST's saved in sflag, the astpending flag will be set and 181 * ast() will be called again. 182 */ 183 PROC_SLOCK(p); 184 sflag = p->p_sflag; 185 if (p->p_sflag & (PS_ALRMPEND | PS_PROFPEND)) 186 p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND); 187#ifdef MAC 188 if (p->p_sflag & PS_MACPEND) 189 p->p_sflag &= ~PS_MACPEND; 190#endif 191 thread_lock(td); 192 PROC_SUNLOCK(p); 193 flags = td->td_flags; 194 td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | 195 TDF_NEEDRESCHED | TDF_INTERRUPT); 196 thread_unlock(td); 197 PCPU_INC(cnt.v_trap); 198 199 /* 200 * XXXKSE While the fact that we owe a user profiling 201 * tick is stored per thread in this code, the statistics 202 * themselves are still stored per process. 203 * This should probably change, by which I mean that 204 * possibly the location of both might change. 205 */ 206 if (td->td_ucred != p->p_ucred) 207 cred_update_thread(td); 208 if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) { 209 addupc_task(td, td->td_profil_addr, td->td_profil_ticks); 210 td->td_profil_ticks = 0; 211 td->td_pflags &= ~TDP_OWEUPC; 212 } 213 if (sflag & PS_ALRMPEND) { 214 PROC_LOCK(p); 215 psignal(p, SIGVTALRM); 216 PROC_UNLOCK(p); 217 } 218#if defined(DEV_NPX) && !defined(SMP) 219 if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) { 220 atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags, 221 PCB_NPXTRAP); 222 ucode = npxtrap(); 223 if (ucode != -1) { 224 ksiginfo_init_trap(&ksi); 225 ksi.ksi_signo = SIGFPE; 226 ksi.ksi_code = ucode; 227 trapsignal(td, &ksi); 228 } 229 } 230#endif 231 if (sflag & PS_PROFPEND) { 232 PROC_LOCK(p); 233 psignal(p, SIGPROF); 234 PROC_UNLOCK(p); 235 } 236#ifdef MAC 237 if (sflag & PS_MACPEND) 238 mac_thread_userret(td); 239#endif 240 if (flags & TDF_NEEDRESCHED) { 241#ifdef KTRACE 242 if (KTRPOINT(td, KTR_CSW)) 243 ktrcsw(1, 1); 244#endif 245 thread_lock(td); 246 sched_prio(td, td->td_user_pri); 247 SCHED_STAT_INC(switch_needresched); 248 mi_switch(SW_INVOL, NULL); 249 thread_unlock(td); 250#ifdef KTRACE 251 if (KTRPOINT(td, KTR_CSW)) 252 ktrcsw(0, 1); 253#endif 254 } 255 if (flags & TDF_NEEDSIGCHK) { 256 PROC_LOCK(p); 257 mtx_lock(&p->p_sigacts->ps_mtx); 258 while ((sig = cursig(td)) != 0) 259 postsig(sig); 260 mtx_unlock(&p->p_sigacts->ps_mtx); 261 PROC_UNLOCK(p); 262 } 263 264 userret(td, framep); 265 mtx_assert(&Giant, MA_NOTOWNED); 266} 267