subr_syscall.c revision 136152
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 136152 2004-10-05 18:51:11Z jhb $"); 42 43#include "opt_ktrace.h" 44#include "opt_mac.h" 45#ifdef __i386__ 46#include "opt_npx.h" 47#endif 48 49#include <sys/param.h> 50#include <sys/bus.h> 51#include <sys/kernel.h> 52#include <sys/lock.h> 53#include <sys/mac.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/* 71 * Define the code needed before returning to user mode, for 72 * trap and syscall. 73 * 74 * MPSAFE 75 */ 76void 77userret(td, frame, oticks) 78 struct thread *td; 79 struct trapframe *frame; 80 u_int oticks; 81{ 82 struct proc *p = td->td_proc; 83 84 CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid, 85 p->p_comm); 86#ifdef DIAGNOSTIC 87 /* Check that we called signotify() enough. */ 88 PROC_LOCK(p); 89 mtx_lock_spin(&sched_lock); 90 if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 || 91 (td->td_flags & TDF_ASTPENDING) == 0)) 92 printf("failed to set signal flags properly for ast()\n"); 93 mtx_unlock_spin(&sched_lock); 94 PROC_UNLOCK(p); 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_SA) 118 thread_userret(td, frame); 119 120 /* 121 * Charge system time if profiling. 122 */ 123 if (p->p_flag & P_PROFIL) { 124 quad_t ticks; 125 126 ticks = td->td_sticks - oticks; 127 addupc_task(td, TRAPF_PC(frame), (u_int)ticks * psratio); 128 } 129} 130 131/* 132 * Process an asynchronous software trap. 133 * This is relatively easy. 134 * This function will return with preemption disabled. 135 */ 136void 137ast(struct trapframe *framep) 138{ 139 struct thread *td; 140 struct proc *p; 141 struct ksegrp *kg; 142 struct rlimit rlim; 143 u_int sticks; 144 int sflag; 145 int flags; 146 int sig; 147#if defined(DEV_NPX) && !defined(SMP) 148 int ucode; 149#endif 150 151 td = curthread; 152 p = td->td_proc; 153 kg = td->td_ksegrp; 154 155 CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid, 156 p->p_comm); 157 KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode")); 158 WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode"); 159 mtx_assert(&Giant, MA_NOTOWNED); 160 mtx_assert(&sched_lock, MA_NOTOWNED); 161 td->td_frame = framep; 162 sticks = td->td_sticks; 163 164 if ((p->p_flag & P_SA) && (td->td_mailbox == NULL)) 165 thread_user_enter(td); 166 167 /* 168 * This updates the p_sflag's for the checks below in one 169 * "atomic" operation with turning off the astpending flag. 170 * If another AST is triggered while we are handling the 171 * AST's saved in sflag, the astpending flag will be set and 172 * ast() will be called again. 173 */ 174 mtx_lock_spin(&sched_lock); 175 flags = td->td_flags; 176 sflag = p->p_sflag; 177 p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU); 178#ifdef MAC 179 p->p_sflag &= ~PS_MACPEND; 180#endif 181 td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | 182 TDF_NEEDRESCHED | TDF_INTERRUPT); 183 cnt.v_soft++; 184 mtx_unlock_spin(&sched_lock); 185 186 /* 187 * XXXKSE While the fact that we owe a user profiling 188 * tick is stored per KSE in this code, the statistics 189 * themselves are still stored per process. 190 * This should probably change, by which I mean that 191 * possibly the location of both might change. 192 */ 193 if (td->td_ucred != p->p_ucred) 194 cred_update_thread(td); 195 if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) { 196 addupc_task(td, td->td_profil_addr, td->td_profil_ticks); 197 td->td_profil_ticks = 0; 198 td->td_pflags &= ~TDP_OWEUPC; 199 } 200 if (sflag & PS_ALRMPEND) { 201 PROC_LOCK(p); 202 psignal(p, SIGVTALRM); 203 PROC_UNLOCK(p); 204 } 205#if defined(DEV_NPX) && !defined(SMP) 206 if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) { 207 atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags, 208 PCB_NPXTRAP); 209 ucode = npxtrap(); 210 if (ucode != -1) { 211 trapsignal(td, SIGFPE, ucode); 212 } 213 } 214#endif 215 if (sflag & PS_PROFPEND) { 216 PROC_LOCK(p); 217 psignal(p, SIGPROF); 218 PROC_UNLOCK(p); 219 } 220 if (sflag & PS_XCPU) { 221 PROC_LOCK(p); 222 lim_rlimit(p, RLIMIT_CPU, &rlim); 223 mtx_lock_spin(&sched_lock); 224 if (p->p_rux.rux_runtime.sec >= rlim.rlim_max) { 225 mtx_unlock_spin(&sched_lock); 226 killproc(p, "exceeded maximum CPU limit"); 227 } else { 228 if (p->p_cpulimit < rlim.rlim_max) 229 p->p_cpulimit += 5; 230 mtx_unlock_spin(&sched_lock); 231 psignal(p, SIGXCPU); 232 } 233 PROC_UNLOCK(p); 234 } 235#ifdef MAC 236 if (sflag & PS_MACPEND) 237 mac_thread_userret(td); 238#endif 239 if (flags & TDF_NEEDRESCHED) { 240#ifdef KTRACE 241 if (KTRPOINT(td, KTR_CSW)) 242 ktrcsw(1, 1); 243#endif 244 mtx_lock_spin(&sched_lock); 245 sched_prio(td, kg->kg_user_pri); 246 mi_switch(SW_INVOL, NULL); 247 mtx_unlock_spin(&sched_lock); 248#ifdef KTRACE 249 if (KTRPOINT(td, KTR_CSW)) 250 ktrcsw(0, 1); 251#endif 252 } 253 if (flags & TDF_NEEDSIGCHK) { 254 PROC_LOCK(p); 255 mtx_lock(&p->p_sigacts->ps_mtx); 256 while ((sig = cursig(td)) != 0) 257 postsig(sig); 258 mtx_unlock(&p->p_sigacts->ps_mtx); 259 PROC_UNLOCK(p); 260 } 261 262 userret(td, framep, sticks); 263 mtx_assert(&Giant, MA_NOTOWNED); 264} 265