FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_trap.c
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: releng/5.0/sys/kern/subr_trap.c 106655 2002-11-08 19:00:17Z rwatson $
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 */
69 void
70 userret(td, frame, oticks)
71 struct thread *td;
72 struct trapframe *frame;
73 u_int oticks;
74 {
75 struct proc *p = td->td_proc;
76 struct kse *ke = td->td_kse;
77
78 CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
79 p->p_comm);
80 #ifdef INVARIANTS
81 /* Check that we called signotify() enough. */
82 mtx_lock(&Giant);
83 PROC_LOCK(p);
84 mtx_lock_spin(&sched_lock);
85 if (SIGPENDING(p) && ((p->p_sflag & PS_NEEDSIGCHK) == 0 ||
86 (td->td_kse->ke_flags & KEF_ASTPENDING) == 0))
87 printf("failed to set signal flags properly for ast()\n");
88 mtx_unlock_spin(&sched_lock);
89 PROC_UNLOCK(p);
90 mtx_unlock(&Giant);
91 #endif
92
93 /*
94 * Let the scheduler adjust our priority etc.
95 */
96 sched_userret(td);
97
98 /*
99 * We need to check to see if we have to exit or wait due to a
100 * single threading requirement or some other STOP condition.
101 * Don't bother doing all the work if the stop bits are not set
102 * at this time.. If we miss it, we miss it.. no big deal.
103 */
104 if (P_SHOULDSTOP(p)) {
105 PROC_LOCK(p);
106 thread_suspend_check(0); /* Can suspend or kill */
107 PROC_UNLOCK(p);
108 }
109
110 /*
111 * Do special thread processing, e.g. upcall tweaking and such.
112 */
113 if (p->p_flag & P_KSES) {
114 thread_userret(td, frame);
115 /* printf("KSE thread returned"); */
116 }
117
118 /*
119 * Charge system time if profiling.
120 *
121 * XXX should move PS_PROFIL to a place that can obviously be
122 * accessed safely without sched_lock.
123 */
124 if (p->p_sflag & PS_PROFIL) {
125 quad_t ticks;
126
127 mtx_lock_spin(&sched_lock);
128 ticks = ke->ke_sticks - oticks;
129 mtx_unlock_spin(&sched_lock);
130 addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio);
131 }
132 }
133
134 /*
135 * Process an asynchronous software trap.
136 * This is relatively easy.
137 * This function will return with preemption disabled.
138 */
139 void
140 ast(struct trapframe *framep)
141 {
142 struct thread *td;
143 struct proc *p;
144 struct kse *ke;
145 struct ksegrp *kg;
146 struct rlimit *rlim;
147 u_int prticks, sticks;
148 int sflag;
149 int flags;
150 int sig;
151 #if defined(DEV_NPX) && !defined(SMP)
152 int ucode;
153 #endif
154
155 td = curthread;
156 p = td->td_proc;
157 kg = td->td_ksegrp;
158
159 CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
160 p->p_comm);
161 KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
162 #ifdef WITNESS
163 if (witness_list(td))
164 panic("Returning to user mode with mutex(s) held");
165 #endif
166 mtx_assert(&Giant, MA_NOTOWNED);
167 mtx_assert(&sched_lock, MA_NOTOWNED);
168 td->td_frame = framep;
169
170 /*
171 * This updates the p_sflag's for the checks below in one
172 * "atomic" operation with turning off the astpending flag.
173 * If another AST is triggered while we are handling the
174 * AST's saved in sflag, the astpending flag will be set and
175 * ast() will be called again.
176 */
177 mtx_lock_spin(&sched_lock);
178 ke = td->td_kse;
179 sticks = ke->ke_sticks;
180 flags = ke->ke_flags;
181 sflag = p->p_sflag;
182 p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND | PS_XCPU);
183 #ifdef MAC
184 p->p_sflag &= ~PS_MACPEND;
185 #endif
186 ke->ke_flags &= ~(KEF_ASTPENDING | KEF_NEEDRESCHED | KEF_OWEUPC);
187 cnt.v_soft++;
188 prticks = 0;
189 if (flags & KEF_OWEUPC && sflag & PS_PROFIL) {
190 prticks = p->p_stats->p_prof.pr_ticks;
191 p->p_stats->p_prof.pr_ticks = 0;
192 }
193 mtx_unlock_spin(&sched_lock);
194 /*
195 * XXXKSE While the fact that we owe a user profiling
196 * tick is stored per KSE in this code, the statistics
197 * themselves are still stored per process.
198 * This should probably change, by which I mean that
199 * possibly the location of both might change.
200 */
201
202 if (td->td_ucred != p->p_ucred)
203 cred_update_thread(td);
204 if (flags & KEF_OWEUPC && sflag & PS_PROFIL)
205 addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks);
206 if (sflag & PS_ALRMPEND) {
207 PROC_LOCK(p);
208 psignal(p, SIGVTALRM);
209 PROC_UNLOCK(p);
210 }
211 #if defined(DEV_NPX) && !defined(SMP)
212 if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
213 atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
214 PCB_NPXTRAP);
215 ucode = npxtrap();
216 if (ucode != -1) {
217 trapsignal(p, SIGFPE, ucode);
218 }
219 }
220 #endif
221 if (sflag & PS_PROFPEND) {
222 PROC_LOCK(p);
223 psignal(p, SIGPROF);
224 PROC_UNLOCK(p);
225 }
226 if (sflag & PS_XCPU) {
227 PROC_LOCK(p);
228 rlim = &p->p_rlimit[RLIMIT_CPU];
229 if (p->p_runtime.sec >= rlim->rlim_max)
230 killproc(p, "exceeded maximum CPU limit");
231 else {
232 psignal(p, SIGXCPU);
233 mtx_lock_spin(&sched_lock);
234 if (p->p_cpulimit < rlim->rlim_max)
235 p->p_cpulimit += 5;
236 mtx_unlock_spin(&sched_lock);
237 }
238 PROC_UNLOCK(p);
239 }
240 #ifdef MAC
241 if (sflag & PS_MACPEND)
242 mac_thread_userret(td);
243 #endif
244 if (flags & KEF_NEEDRESCHED) {
245 mtx_lock_spin(&sched_lock);
246 sched_prio(td, kg->kg_user_pri);
247 p->p_stats->p_ru.ru_nivcsw++;
248 mi_switch();
249 mtx_unlock_spin(&sched_lock);
250 }
251 if (sflag & PS_NEEDSIGCHK) {
252 PROC_LOCK(p);
253 while ((sig = cursig(td)) != 0)
254 postsig(sig);
255 PROC_UNLOCK(p);
256 }
257
258 userret(td, framep, sticks);
259 #ifdef DIAGNOSTIC
260 cred_free_thread(td);
261 #endif
262 mtx_assert(&Giant, MA_NOTOWNED);
263 }
Cache object: b3e86af3c219f720b39449dfb3f82aaf
|