2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
37 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include "opt_ktrace.h"
48 #include "opt_sched.h"
50 #include <sys/param.h>
52 #include <sys/kernel.h>
54 #include <sys/mutex.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>
64 #include <sys/ktrace.h>
67 #include <machine/cpu.h>
68 #include <machine/pcb.h>
70 #include <security/mac/mac_framework.h>
73 * Define the code needed before returning to user mode, for trap and
77 userret(struct thread *td, struct trapframe *frame)
79 struct proc *p = td->td_proc;
81 CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
84 /* Check that we called signotify() enough. */
87 if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
88 (td->td_flags & TDF_ASTPENDING) == 0))
89 printf("failed to set signal flags properly for ast()\n");
99 * If this thread tickled GEOM, we need to wait for the giggling to
100 * stop before we return to userland
102 if (td->td_pflags & TDP_GEOM)
106 * We need to check to see if we have to exit or wait due to a
107 * single threading requirement or some other STOP condition.
108 * Don't bother doing all the work if the stop bits are not set
109 * at this time.. If we miss it, we miss it.. no big deal.
111 if (P_SHOULDSTOP(p)) {
113 thread_suspend_check(0); /* Can suspend or kill */
119 * Do special thread processing, e.g. upcall tweaking and such.
121 if (p->p_flag & P_SA)
122 thread_userret(td, frame);
126 * Charge system time if profiling.
128 if (p->p_flag & P_PROFIL) {
130 addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
134 * Let the scheduler adjust our priority etc.
137 KASSERT(td->td_locks == 0,
138 ("userret: Returning with %d locks held.", td->td_locks));
142 * Process an asynchronous software trap.
143 * This is relatively easy.
144 * This function will return with preemption disabled.
147 ast(struct trapframe *framep)
153 #if defined(DEV_NPX) && !defined(SMP)
161 CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
163 KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
164 WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
165 mtx_assert(&Giant, MA_NOTOWNED);
166 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
167 td->td_frame = framep;
171 if ((p->p_flag & P_SA) && (td->td_mailbox == NULL))
172 thread_user_enter(td);
176 * This updates the td_flag's for the checks below in one
177 * "atomic" operation with turning off the astpending flag.
178 * If another AST is triggered while we are handling the
179 * AST's saved in flags, the astpending flag will be set and
180 * ast() will be called again.
183 flags = td->td_flags;
184 td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
185 TDF_NEEDRESCHED | TDF_INTERRUPT | TDF_ALRMPEND | TDF_PROFPEND |
188 PCPU_INC(cnt.v_trap);
191 * XXXKSE While the fact that we owe a user profiling
192 * tick is stored per thread in this code, the statistics
193 * themselves are still stored per process.
194 * This should probably change, by which I mean that
195 * possibly the location of both might change.
197 if (td->td_ucred != p->p_ucred)
198 cred_update_thread(td);
199 if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
200 addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
201 td->td_profil_ticks = 0;
202 td->td_pflags &= ~TDP_OWEUPC;
204 if (flags & TDF_ALRMPEND) {
206 psignal(p, SIGVTALRM);
209 #if defined(DEV_NPX) && !defined(SMP)
210 if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
211 atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
215 ksiginfo_init_trap(&ksi);
216 ksi.ksi_signo = SIGFPE;
217 ksi.ksi_code = ucode;
218 trapsignal(td, &ksi);
222 if (flags & TDF_PROFPEND) {
228 if (flags & TDF_MACPEND)
229 mac_thread_userret(td);
231 if (flags & TDF_NEEDRESCHED) {
233 if (KTRPOINT(td, KTR_CSW))
237 sched_prio(td, td->td_user_pri);
238 SCHED_STAT_INC(switch_needresched);
239 mi_switch(SW_INVOL, NULL);
242 if (KTRPOINT(td, KTR_CSW))
246 if (flags & TDF_NEEDSIGCHK) {
248 mtx_lock(&p->p_sigacts->ps_mtx);
249 while ((sig = cursig(td)) != 0)
251 mtx_unlock(&p->p_sigacts->ps_mtx);
256 mtx_assert(&Giant, MA_NOTOWNED);