subr_syscall.c revision 110140
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 * $FreeBSD: head/sys/kern/subr_trap.c 110140 2003-01-31 11:22:31Z tjr $ 39 */ 40 41#include "opt_mac.h" 42#ifdef __i386__ 43#include "opt_npx.h" 44#endif 45 46#include <sys/param.h> 47#include <sys/bus.h> 48#include <sys/kernel.h> 49#include <sys/lock.h> 50#include <sys/mac.h> 51#include <sys/mutex.h> 52#include <sys/proc.h> 53#include <sys/kse.h> 54#include <sys/ktr.h> 55#include <sys/resourcevar.h> 56#include <sys/sched.h> 57#include <sys/signalvar.h> 58#include <sys/systm.h> 59#include <sys/vmmeter.h> 60#include <machine/cpu.h> 61#include <machine/pcb.h> 62 63/* 64 * Define the code needed before returning to user mode, for 65 * trap and syscall. 66 * 67 * MPSAFE 68 */ 69void 70userret(td, frame, oticks) 71 struct thread *td; 72 struct trapframe *frame; 73 u_int oticks; 74{ 75 struct proc *p = td->td_proc; 76#ifdef INVARIANTS 77 struct kse *ke; 78#endif 79 u_int64_t eticks; 80 81 CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid, 82 p->p_comm); 83#ifdef INVARIANTS 84 /* 85 * Check that we called signotify() enough. 86 * XXXKSE this checking is bogus for threaded program, 87 */ 88 mtx_lock(&Giant); 89 PROC_LOCK(p); 90 mtx_lock_spin(&sched_lock); 91 ke = td->td_kse; 92 if (SIGPENDING(p) && ((p->p_sflag & PS_NEEDSIGCHK) == 0 || 93 (td->td_kse->ke_flags & KEF_ASTPENDING) == 0)) 94 printf("failed to set signal flags properly for ast()\n"); 95 mtx_unlock_spin(&sched_lock); 96 PROC_UNLOCK(p); 97 mtx_unlock(&Giant); 98#endif 99 100 /* 101 * Let the scheduler adjust our priority etc. 102 */ 103 sched_userret(td); 104 105 /* 106 * Charge system time if profiling. 107 * 108 * XXX should move PS_PROFIL to a place that can obviously be 109 * accessed safely without sched_lock. 110 */ 111 112 if (p->p_sflag & PS_PROFIL) { 113 eticks = td->td_sticks - oticks; 114 addupc_task(td, TRAPF_PC(frame), (u_int)eticks * psratio); 115 } 116 117 /* 118 * We need to check to see if we have to exit or wait due to a 119 * single threading requirement or some other STOP condition. 120 * Don't bother doing all the work if the stop bits are not set 121 * at this time.. If we miss it, we miss it.. no big deal. 122 */ 123 if (P_SHOULDSTOP(p)) { 124 PROC_LOCK(p); 125 thread_suspend_check(0); /* Can suspend or kill */ 126 PROC_UNLOCK(p); 127 } 128 129 /* 130 * Do special thread processing, e.g. upcall tweaking and such. 131 */ 132 if (p->p_flag & P_KSES) { 133 thread_userret(td, frame); 134 } 135} 136 137/* 138 * Process an asynchronous software trap. 139 * This is relatively easy. 140 * This function will return with preemption disabled. 141 */ 142void 143ast(struct trapframe *framep) 144{ 145 struct thread *td; 146 struct proc *p; 147 struct kse *ke; 148 struct ksegrp *kg; 149 struct rlimit *rlim; 150 u_int prticks, sticks; 151 int sflag; 152 int flags; 153 int tflags; 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 = td->td_sticks; 184 tflags = td->td_flags; 185 flags = ke->ke_flags; 186 sflag = p->p_sflag; 187 p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND | PS_XCPU); 188#ifdef MAC 189 p->p_sflag &= ~PS_MACPEND; 190#endif 191 ke->ke_flags &= ~(KEF_ASTPENDING | KEF_NEEDRESCHED); 192 td->td_flags &= ~(TDF_ASTPENDING | TDF_OWEUPC); 193 cnt.v_soft++; 194 prticks = 0; 195 if (tflags & TDF_OWEUPC && sflag & PS_PROFIL) { 196 prticks = td->td_prticks; 197 td->td_prticks = 0; 198 } 199 mtx_unlock_spin(&sched_lock); 200 /* 201 * XXXKSE While the fact that we owe a user profiling 202 * tick is stored per KSE in this code, the statistics 203 * themselves are still stored per process. 204 * This should probably change, by which I mean that 205 * possibly the location of both might change. 206 */ 207 208 if (td->td_ucred != p->p_ucred) 209 cred_update_thread(td); 210 if (tflags & TDF_OWEUPC && sflag & PS_PROFIL) { 211 addupc_task(td, td->td_praddr, prticks); 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 trapsignal(p, SIGFPE, ucode); 225 } 226 } 227#endif 228 if (sflag & PS_PROFPEND) { 229 PROC_LOCK(p); 230 psignal(p, SIGPROF); 231 PROC_UNLOCK(p); 232 } 233 if (sflag & PS_XCPU) { 234 PROC_LOCK(p); 235 rlim = &p->p_rlimit[RLIMIT_CPU]; 236 if (p->p_runtime.sec >= rlim->rlim_max) 237 killproc(p, "exceeded maximum CPU limit"); 238 else { 239 psignal(p, SIGXCPU); 240 mtx_lock_spin(&sched_lock); 241 if (p->p_cpulimit < rlim->rlim_max) 242 p->p_cpulimit += 5; 243 mtx_unlock_spin(&sched_lock); 244 } 245 PROC_UNLOCK(p); 246 } 247#ifdef MAC 248 if (sflag & PS_MACPEND) 249 mac_thread_userret(td); 250#endif 251 if (flags & KEF_NEEDRESCHED) { 252 mtx_lock_spin(&sched_lock); 253 sched_prio(td, kg->kg_user_pri); 254 p->p_stats->p_ru.ru_nivcsw++; 255 mi_switch(); 256 mtx_unlock_spin(&sched_lock); 257 } 258 if (sflag & PS_NEEDSIGCHK) { 259 PROC_LOCK(p); 260 while ((sig = cursig(td)) != 0) 261 postsig(sig); 262 PROC_UNLOCK(p); 263 } 264 265 userret(td, framep, sticks); 266#ifdef DIAGNOSTIC 267 cred_free_thread(td); 268#endif 269 mtx_assert(&Giant, MA_NOTOWNED); 270} 271