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$"); 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/mutex.h> 54 #include <sys/proc.h> 55 #include <sys/ktr.h> 56 #include <sys/resourcevar.h> 57 #include <sys/sched.h> 58 #include <sys/signalvar.h> 59 #include <sys/systm.h> 60 #include <sys/vmmeter.h> 61 #ifdef KTRACE 62 #include <sys/uio.h> 63 #include <sys/ktrace.h> 64 #endif 65 66 #include <machine/cpu.h> 67 #include <machine/pcb.h> 68 69 #include <security/mac/mac_framework.h> 70 71 /* 72 * Define the code needed before returning to user mode, for trap and 73 * syscall. 74 */ 75 void 76 userret(struct thread *td, struct trapframe *frame) 77 { 78 struct proc *p = td->td_proc; 79 80 CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid, 81 p->p_comm); 82 #ifdef DIAGNOSTIC 83 /* Check that we called signotify() enough. */ 84 PROC_LOCK(p); 85 mtx_lock_spin(&sched_lock); 86 if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 || 87 (td->td_flags & TDF_ASTPENDING) == 0)) 88 printf("failed to set signal flags properly for ast()\n"); 89 mtx_unlock_spin(&sched_lock); 90 PROC_UNLOCK(p); 91 #endif 92 93 #ifdef KTRACE 94 KTRUSERRET(td); 95 #endif 96 97 /* 98 * If this thread tickled GEOM, we need to wait for the giggling to 99 * stop before we return to userland 100 */ 101 if (td->td_pflags & TDP_GEOM) 102 g_waitidle(); 103 104 /* 105 * We need to check to see if we have to exit or wait due to a 106 * single threading requirement or some other STOP condition. 107 * Don't bother doing all the work if the stop bits are not set 108 * at this time.. If we miss it, we miss it.. no big deal. 109 */ 110 if (P_SHOULDSTOP(p)) { 111 PROC_LOCK(p); 112 thread_suspend_check(0); /* Can suspend or kill */ 113 PROC_UNLOCK(p); 114 } 115 116 #ifdef KSE 117 /* 118 * Do special thread processing, e.g. upcall tweaking and such. 119 */ 120 if (p->p_flag & P_SA) 121 thread_userret(td, frame); 122 #endif 123 124 /* 125 * Charge system time if profiling. 126 */ 127 if (p->p_flag & P_PROFIL) { 128 129 addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio); 130 } 131 132 /* 133 * Let the scheduler adjust our priority etc. 134 */ 135 sched_userret(td); 136 KASSERT(td->td_locks == 0, 137 ("userret: Returning with %d locks held.", td->td_locks)); 138 } 139 140 /* 141 * Process an asynchronous software trap. 142 * This is relatively easy. 143 * This function will return with preemption disabled. 144 */ 145 void 146 ast(struct trapframe *framep) 147 { 148 struct thread *td; 149 struct proc *p; 150 struct rlimit rlim; 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 mtx_assert(&sched_lock, 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 mtx_lock_spin(&sched_lock); 184 flags = td->td_flags; 185 sflag = p->p_sflag; 186 if (p->p_sflag & (PS_ALRMPEND | PS_PROFPEND | PS_XCPU)) 187 p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU); 188 #ifdef MAC 189 if (p->p_sflag & PS_MACPEND) 190 p->p_sflag &= ~PS_MACPEND; 191 #endif 192 td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | 193 TDF_NEEDRESCHED | TDF_INTERRUPT); 194 cnt.v_trap++; 195 mtx_unlock_spin(&sched_lock); 196 197 /* 198 * XXXKSE While the fact that we owe a user profiling 199 * tick is stored per thread in this code, the statistics 200 * themselves are still stored per process. 201 * This should probably change, by which I mean that 202 * possibly the location of both might change. 203 */ 204 if (td->td_ucred != p->p_ucred) 205 cred_update_thread(td); 206 if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) { 207 addupc_task(td, td->td_profil_addr, td->td_profil_ticks); 208 td->td_profil_ticks = 0; 209 td->td_pflags &= ~TDP_OWEUPC; 210 } 211 if (sflag & PS_ALRMPEND) { 212 PROC_LOCK(p); 213 psignal(p, SIGVTALRM); 214 PROC_UNLOCK(p); 215 } 216 #if defined(DEV_NPX) && !defined(SMP) 217 if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) { 218 atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags, 219 PCB_NPXTRAP); 220 ucode = npxtrap(); 221 if (ucode != -1) { 222 ksiginfo_init_trap(&ksi); 223 ksi.ksi_signo = SIGFPE; 224 ksi.ksi_code = ucode; 225 trapsignal(td, &ksi); 226 } 227 } 228 #endif 229 if (sflag & PS_PROFPEND) { 230 PROC_LOCK(p); 231 psignal(p, SIGPROF); 232 PROC_UNLOCK(p); 233 } 234 if (sflag & PS_XCPU) { 235 PROC_LOCK(p); 236 lim_rlimit(p, RLIMIT_CPU, &rlim); 237 mtx_lock_spin(&sched_lock); 238 if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) { 239 mtx_unlock_spin(&sched_lock); 240 killproc(p, "exceeded maximum CPU limit"); 241 } else { 242 if (p->p_cpulimit < rlim.rlim_max) 243 p->p_cpulimit += 5; 244 mtx_unlock_spin(&sched_lock); 245 psignal(p, SIGXCPU); 246 } 247 PROC_UNLOCK(p); 248 } 249 #ifdef MAC 250 if (sflag & PS_MACPEND) 251 mac_thread_userret(td); 252 #endif 253 if (flags & TDF_NEEDRESCHED) { 254 #ifdef KTRACE 255 if (KTRPOINT(td, KTR_CSW)) 256 ktrcsw(1, 1); 257 #endif 258 mtx_lock_spin(&sched_lock); 259 sched_prio(td, td->td_user_pri); 260 mi_switch(SW_INVOL, NULL); 261 mtx_unlock_spin(&sched_lock); 262 #ifdef KTRACE 263 if (KTRPOINT(td, KTR_CSW)) 264 ktrcsw(0, 1); 265 #endif 266 } 267 if (flags & TDF_NEEDSIGCHK) { 268 PROC_LOCK(p); 269 mtx_lock(&p->p_sigacts->ps_mtx); 270 while ((sig = cursig(td)) != 0) 271 postsig(sig); 272 mtx_unlock(&p->p_sigacts->ps_mtx); 273 PROC_UNLOCK(p); 274 } 275 276 userret(td, framep); 277 mtx_assert(&Giant, MA_NOTOWNED); 278 } 279