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