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