subr_syscall.c revision 105974
1160995Ssam/*- 2160995Ssam * Copyright (C) 1994, David Greenman 3160995Ssam * Copyright (c) 1990, 1993 4160995Ssam * The Regents of the University of California. All rights reserved. 5160995Ssam * 6160995Ssam * This code is derived from software contributed to Berkeley by 7160995Ssam * the University of Utah, and William Jolitz. 8160995Ssam * 9160995Ssam * Redistribution and use in source and binary forms, with or without 10160995Ssam * modification, are permitted provided that the following conditions 11160995Ssam * are met: 12160995Ssam * 1. Redistributions of source code must retain the above copyright 13160995Ssam * notice, this list of conditions and the following disclaimer. 14160995Ssam * 2. Redistributions in binary form must reproduce the above copyright 15160995Ssam * notice, this list of conditions and the following disclaimer in the 16160995Ssam * documentation and/or other materials provided with the distribution. 17160995Ssam * 3. All advertising materials mentioning features or use of this software 18160995Ssam * must display the following acknowledgement: 19160995Ssam * This product includes software developed by the University of 20160995Ssam * California, Berkeley and its contributors. 21160995Ssam * 4. Neither the name of the University nor the names of its contributors 22160995Ssam * may be used to endorse or promote products derived from this software 23160995Ssam * without specific prior written permission. 24160995Ssam * 25160995Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26160995Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27160995Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28160995Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29160995Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30160995Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31160995Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32160995Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33160995Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34160995Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35160995Ssam * SUCH DAMAGE. 36160995Ssam * 37160995Ssam * from: @(#)trap.c 7.4 (Berkeley) 5/13/91 38160995Ssam * $FreeBSD: head/sys/kern/subr_trap.c 105974 2002-10-26 04:44:17Z julian $ 39160995Ssam */ 40160995Ssam 41160995Ssam#include "opt_mac.h" 42160995Ssam#ifdef __i386__ 43160995Ssam#include "opt_npx.h" 44160995Ssam#endif 45160995Ssam 46160995Ssam#include <sys/param.h> 47160995Ssam#include <sys/bus.h> 48160995Ssam#include <sys/kernel.h> 49228975Suqs#include <sys/lock.h> 50160995Ssam#include <sys/mac.h> 51160995Ssam#include <sys/mutex.h> 52160995Ssam#include <sys/proc.h> 53160995Ssam#include <sys/kse.h> 54160995Ssam#include <sys/ktr.h> 55160995Ssam#include <sys/resourcevar.h> 56160995Ssam#include <sys/sched.h> 57160995Ssam#include <sys/signalvar.h> 58160995Ssam#include <sys/systm.h> 59160995Ssam#include <sys/vmmeter.h> 60160995Ssam#include <machine/cpu.h> 61160995Ssam#include <machine/pcb.h> 62160995Ssam 63160995Ssam/* 64160995Ssam * Define the code needed before returning to user mode, for 65160995Ssam * trap and syscall. 66160995Ssam * 67160995Ssam * MPSAFE 68160995Ssam */ 69160995Ssamvoid 70160995Ssamuserret(td, frame, oticks) 71160995Ssam struct thread *td; 72160995Ssam struct trapframe *frame; 73160995Ssam u_int oticks; 74160995Ssam{ 75160995Ssam struct proc *p = td->td_proc; 76160995Ssam struct kse *ke = td->td_kse; 77160995Ssam 78160995Ssam CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid, 79160995Ssam p->p_comm); 80160995Ssam#ifdef INVARIANTS 81160995Ssam /* Check that we called signotify() enough. */ 82160995Ssam mtx_lock(&Giant); 83 PROC_LOCK(p); 84 mtx_lock_spin(&sched_lock); 85 if (SIGPENDING(p) && ((p->p_sflag & PS_NEEDSIGCHK) == 0 || 86 (td->td_kse->ke_flags & KEF_ASTPENDING) == 0)) 87 printf("failed to set signal flags properly for ast()\n"); 88 mtx_unlock_spin(&sched_lock); 89 PROC_UNLOCK(p); 90 mtx_unlock(&Giant); 91#endif 92 93#ifdef MAC 94 mac_thread_userret(td); 95#endif 96 97 /* 98 * Let the scheduler adjust our priority etc. 99 */ 100 sched_userret(td); 101 102 /* 103 * We need to check to see if we have to exit or wait due to a 104 * single threading requirement or some other STOP condition. 105 * Don't bother doing all the work if the stop bits are not set 106 * at this time.. If we miss it, we miss it.. no big deal. 107 */ 108 if (P_SHOULDSTOP(p)) { 109 PROC_LOCK(p); 110 thread_suspend_check(0); /* Can suspend or kill */ 111 PROC_UNLOCK(p); 112 } 113 114 /* 115 * Do special thread processing, e.g. upcall tweaking and such. 116 */ 117 if (p->p_flag & P_KSES) { 118 thread_userret(td, frame); 119 /* printf("KSE thread returned"); */ 120 } 121 122 /* 123 * Charge system time if profiling. 124 * 125 * XXX should move PS_PROFIL to a place that can obviously be 126 * accessed safely without sched_lock. 127 */ 128 if (p->p_sflag & PS_PROFIL) { 129 quad_t ticks; 130 131 mtx_lock_spin(&sched_lock); 132 ticks = ke->ke_sticks - oticks; 133 mtx_unlock_spin(&sched_lock); 134 addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio); 135 } 136} 137 138/* 139 * Process an asynchronous software trap. 140 * This is relatively easy. 141 * This function will return with preemption disabled. 142 */ 143void 144ast(struct trapframe *framep) 145{ 146 struct thread *td; 147 struct proc *p; 148 struct kse *ke; 149 struct ksegrp *kg; 150 struct rlimit *rlim; 151 u_int prticks, sticks; 152 int sflag; 153 int flags; 154 int sig; 155#if defined(DEV_NPX) && !defined(SMP) 156 int ucode; 157#endif 158 159 td = curthread; 160 p = td->td_proc; 161 kg = td->td_ksegrp; 162 163 CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid, 164 p->p_comm); 165 KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode")); 166#ifdef WITNESS 167 if (witness_list(td)) 168 panic("Returning to user mode with mutex(s) held"); 169#endif 170 mtx_assert(&Giant, MA_NOTOWNED); 171 mtx_assert(&sched_lock, MA_NOTOWNED); 172 td->td_frame = framep; 173 174 /* 175 * This updates the p_sflag's for the checks below in one 176 * "atomic" operation with turning off the astpending flag. 177 * If another AST is triggered while we are handling the 178 * AST's saved in sflag, the astpending flag will be set and 179 * ast() will be called again. 180 */ 181 mtx_lock_spin(&sched_lock); 182 ke = td->td_kse; 183 sticks = ke->ke_sticks; 184 flags = ke->ke_flags; 185 sflag = p->p_sflag; 186 p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND | PS_XCPU); 187 ke->ke_flags &= ~(KEF_ASTPENDING | KEF_NEEDRESCHED | KEF_OWEUPC); 188 cnt.v_soft++; 189 prticks = 0; 190 if (flags & KEF_OWEUPC && sflag & PS_PROFIL) { 191 prticks = p->p_stats->p_prof.pr_ticks; 192 p->p_stats->p_prof.pr_ticks = 0; 193 } 194 mtx_unlock_spin(&sched_lock); 195 /* 196 * XXXKSE While the fact that we owe a user profiling 197 * tick is stored per KSE in this code, the statistics 198 * themselves are still stored per process. 199 * This should probably change, by which I mean that 200 * possibly the location of both might change. 201 */ 202 203 if (td->td_ucred != p->p_ucred) 204 cred_update_thread(td); 205 if (flags & KEF_OWEUPC && sflag & PS_PROFIL) 206 addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks); 207 if (sflag & PS_ALRMPEND) { 208 PROC_LOCK(p); 209 psignal(p, SIGVTALRM); 210 PROC_UNLOCK(p); 211 } 212#if defined(DEV_NPX) && !defined(SMP) 213 if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) { 214 atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags, 215 PCB_NPXTRAP); 216 ucode = npxtrap(); 217 if (ucode != -1) { 218 trapsignal(p, SIGFPE, ucode); 219 } 220 } 221#endif 222 if (sflag & PS_PROFPEND) { 223 PROC_LOCK(p); 224 psignal(p, SIGPROF); 225 PROC_UNLOCK(p); 226 } 227 if (sflag & PS_XCPU) { 228 PROC_LOCK(p); 229 rlim = &p->p_rlimit[RLIMIT_CPU]; 230 if (p->p_runtime.sec >= rlim->rlim_max) 231 killproc(p, "exceeded maximum CPU limit"); 232 else { 233 psignal(p, SIGXCPU); 234 mtx_lock_spin(&sched_lock); 235 if (p->p_cpulimit < rlim->rlim_max) 236 p->p_cpulimit += 5; 237 mtx_unlock_spin(&sched_lock); 238 } 239 PROC_UNLOCK(p); 240 } 241 if (flags & KEF_NEEDRESCHED) { 242 mtx_lock_spin(&sched_lock); 243 sched_prio(td, kg->kg_user_pri); 244 p->p_stats->p_ru.ru_nivcsw++; 245 mi_switch(); 246 mtx_unlock_spin(&sched_lock); 247 } 248 if (sflag & PS_NEEDSIGCHK) { 249 PROC_LOCK(p); 250 while ((sig = cursig(td)) != 0) 251 postsig(sig); 252 PROC_UNLOCK(p); 253 } 254 255 userret(td, framep, sticks); 256#ifdef DIAGNOSTIC 257 cred_free_thread(td); 258#endif 259 mtx_assert(&Giant, MA_NOTOWNED); 260} 261