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$ 39 */ 40 41 #ifdef __i386__ 42 #include "opt_npx.h" 43 #endif 44 45 #include <sys/param.h> 46 #include <sys/bus.h> 47 #include <sys/kernel.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/proc.h> 51 #include <sys/resourcevar.h> 52 #include <sys/signalvar.h> 53 #include <sys/systm.h> 54 #include <sys/vmmeter.h> 55 #include <machine/cpu.h> 56 #include <machine/pcb.h> 57 58 /* 59 * Define the code needed before returning to user mode, for 60 * trap and syscall. 61 * 62 * MPSAFE 63 */ 64 void 65 userret(td, frame, oticks) 66 struct thread *td; 67 struct trapframe *frame; 68 u_int oticks; 69 { 70 struct proc *p = td->td_proc; 71 struct kse *ke = td->td_kse; 72 struct ksegrp *kg = td->td_ksegrp; 73 int sig; 74 75 mtx_lock(&Giant); 76 PROC_LOCK(p); 77 while ((sig = CURSIG(p)) != 0) 78 postsig(sig); 79 PROC_UNLOCK(p); 80 mtx_unlock(&Giant); 81 82 mtx_lock_spin(&sched_lock); 83 td->td_priority = kg->kg_user_pri; 84 if (ke->ke_flags & KEF_NEEDRESCHED) { 85 DROP_GIANT(); 86 setrunqueue(td); 87 p->p_stats->p_ru.ru_nivcsw++; 88 mi_switch(); 89 mtx_unlock_spin(&sched_lock); 90 PICKUP_GIANT(); 91 mtx_lock(&Giant); 92 PROC_LOCK(p); 93 while ((sig = CURSIG(p)) != 0) 94 postsig(sig); 95 mtx_unlock(&Giant); 96 PROC_UNLOCK(p); 97 mtx_lock_spin(&sched_lock); 98 } 99 100 /* 101 * Charge system time if profiling. 102 */ 103 if (p->p_sflag & PS_PROFIL) { 104 quad_t ticks; 105 106 ticks = ke->ke_sticks - oticks; 107 mtx_unlock_spin(&sched_lock); 108 addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio); 109 } else 110 mtx_unlock_spin(&sched_lock); 111 } 112 113 /* 114 * Process an asynchronous software trap. 115 * This is relatively easy. 116 * This function will return with preemption disabled. 117 */ 118 void 119 ast(framep) 120 struct trapframe *framep; 121 { 122 struct thread *td = curthread; 123 struct proc *p = td->td_proc; 124 struct kse *ke = td->td_kse; 125 u_int prticks, sticks; 126 int sflag; 127 int flags; 128 #if defined(DEV_NPX) && !defined(SMP) 129 int ucode; 130 #endif 131 132 KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode")); 133 #ifdef WITNESS 134 if (witness_list(td)) 135 panic("Returning to user mode with mutex(s) held"); 136 #endif 137 mtx_assert(&Giant, MA_NOTOWNED); 138 prticks = 0; /* XXX: Quiet warning. */ 139 td->td_frame = framep; 140 /* 141 * This updates the p_sflag's for the checks below in one 142 * "atomic" operation with turning off the astpending flag. 143 * If another AST is triggered while we are handling the 144 * AST's saved in sflag, the astpending flag will be set and 145 * ast() will be called again. 146 */ 147 mtx_lock_spin(&sched_lock); 148 sticks = ke->ke_sticks; 149 sflag = p->p_sflag; 150 flags = ke->ke_flags; 151 p->p_sflag &= ~(PS_PROFPEND | PS_ALRMPEND); 152 ke->ke_flags &= ~(KEF_OWEUPC | KEF_ASTPENDING); 153 cnt.v_soft++; 154 if (flags & KEF_OWEUPC && sflag & PS_PROFIL) { 155 prticks = p->p_stats->p_prof.pr_ticks; 156 p->p_stats->p_prof.pr_ticks = 0; 157 } 158 mtx_unlock_spin(&sched_lock); 159 160 if (td->td_ucred != p->p_ucred) 161 cred_update_thread(td); 162 if (flags & KEF_OWEUPC && sflag & PS_PROFIL) 163 addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks); 164 if (sflag & PS_ALRMPEND) { 165 PROC_LOCK(p); 166 psignal(p, SIGVTALRM); 167 PROC_UNLOCK(p); 168 } 169 #if defined(DEV_NPX) && !defined(SMP) 170 if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) { 171 atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags, 172 PCB_NPXTRAP); 173 ucode = npxtrap(); 174 if (ucode != -1) { 175 trapsignal(p, SIGFPE, ucode); 176 } 177 } 178 #endif 179 if (sflag & PS_PROFPEND) { 180 PROC_LOCK(p); 181 psignal(p, SIGPROF); 182 PROC_UNLOCK(p); 183 } 184 185 userret(td, framep, sticks); 186 #ifdef DIAGNOSTIC 187 cred_free_thread(td); 188 #endif 189 mtx_assert(&Giant, MA_NOTOWNED); 190 } 191