subr_trap.c revision 163709
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 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 163709 2006-10-26 21:42:22Z jb $");
42
43#include "opt_ktrace.h"
44#include "opt_mac.h"
45#ifdef __i386__
46#include "opt_npx.h"
47#endif
48
49#include <sys/param.h>
50#include <sys/bus.h>
51#include <sys/kernel.h>
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#include <sys/proc.h>
55#include <sys/ktr.h>
56#include <sys/resourcevar.h>
57#include <sys/sched.h>
58#include <sys/signalvar.h>
59#include <sys/systm.h>
60#include <sys/vmmeter.h>
61#ifdef KTRACE
62#include <sys/uio.h>
63#include <sys/ktrace.h>
64#endif
65
66#include <machine/cpu.h>
67#include <machine/pcb.h>
68
69#include <security/mac/mac_framework.h>
70
71/*
72 * Define the code needed before returning to user mode, for
73 * trap and syscall.
74 *
75 * MPSAFE
76 */
77void
78userret(struct thread *td, struct trapframe *frame)
79{
80	struct proc *p = td->td_proc;
81
82	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
83            p->p_comm);
84#ifdef DIAGNOSTIC
85	/* Check that we called signotify() enough. */
86	PROC_LOCK(p);
87	mtx_lock_spin(&sched_lock);
88	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
89	    (td->td_flags & TDF_ASTPENDING) == 0))
90		printf("failed to set signal flags properly for ast()\n");
91	mtx_unlock_spin(&sched_lock);
92	PROC_UNLOCK(p);
93#endif
94
95#ifdef KTRACE
96	KTRUSERRET(td);
97#endif
98
99	/*
100	 * If this thread tickled GEOM, we need to wait for the giggling to
101	 * stop before we return to userland
102	 */
103	if (td->td_pflags & TDP_GEOM)
104		g_waitidle();
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#ifdef KSE
119	/*
120	 * Do special thread processing, e.g. upcall tweaking and such.
121	 */
122	if (p->p_flag & P_SA)
123		thread_userret(td, frame);
124#endif
125
126	/*
127	 * Charge system time if profiling.
128	 */
129	if (p->p_flag & P_PROFIL) {
130
131		addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
132	}
133
134	/*
135	 * Let the scheduler adjust our priority etc.
136	 */
137	sched_userret(td);
138	KASSERT(td->td_locks == 0,
139	    ("userret: Returning with %d locks held.", td->td_locks));
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#ifdef KSE
153	struct ksegrp *kg;
154#endif
155	struct rlimit rlim;
156	int sflag;
157	int flags;
158	int sig;
159#if defined(DEV_NPX) && !defined(SMP)
160	int ucode;
161	ksiginfo_t ksi;
162#endif
163
164	td = curthread;
165	p = td->td_proc;
166#ifdef KSE
167	kg = td->td_ksegrp;
168#endif
169
170	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
171            p->p_comm);
172	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
173	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
174	mtx_assert(&Giant, MA_NOTOWNED);
175	mtx_assert(&sched_lock, MA_NOTOWNED);
176	td->td_frame = framep;
177	td->td_pticks = 0;
178
179#ifdef KSE
180	if ((p->p_flag & P_SA) && (td->td_mailbox == NULL))
181		thread_user_enter(td);
182#endif
183
184	/*
185	 * This updates the p_sflag's for the checks below in one
186	 * "atomic" operation with turning off the astpending flag.
187	 * If another AST is triggered while we are handling the
188	 * AST's saved in sflag, the astpending flag will be set and
189	 * ast() will be called again.
190	 */
191	mtx_lock_spin(&sched_lock);
192	flags = td->td_flags;
193	sflag = p->p_sflag;
194	if (p->p_sflag & (PS_ALRMPEND | PS_PROFPEND | PS_XCPU))
195		p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU);
196#ifdef MAC
197	if (p->p_sflag & PS_MACPEND)
198		p->p_sflag &= ~PS_MACPEND;
199#endif
200	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
201	    TDF_NEEDRESCHED | TDF_INTERRUPT);
202	cnt.v_trap++;
203	mtx_unlock_spin(&sched_lock);
204
205	/*
206	 * XXXKSE While the fact that we owe a user profiling
207	 * tick is stored per KSE in this code, the statistics
208	 * themselves are still stored per process.
209	 * This should probably change, by which I mean that
210	 * possibly the location of both might change.
211	 */
212	if (td->td_ucred != p->p_ucred)
213		cred_update_thread(td);
214	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
215		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
216		td->td_profil_ticks = 0;
217		td->td_pflags &= ~TDP_OWEUPC;
218	}
219	if (sflag & PS_ALRMPEND) {
220		PROC_LOCK(p);
221		psignal(p, SIGVTALRM);
222		PROC_UNLOCK(p);
223	}
224#if defined(DEV_NPX) && !defined(SMP)
225	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
226		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
227		    PCB_NPXTRAP);
228		ucode = npxtrap();
229		if (ucode != -1) {
230			ksiginfo_init_trap(&ksi);
231			ksi.ksi_signo = SIGFPE;
232			ksi.ksi_code = ucode;
233			trapsignal(td, &ksi);
234		}
235	}
236#endif
237	if (sflag & PS_PROFPEND) {
238		PROC_LOCK(p);
239		psignal(p, SIGPROF);
240		PROC_UNLOCK(p);
241	}
242	if (sflag & PS_XCPU) {
243		PROC_LOCK(p);
244		lim_rlimit(p, RLIMIT_CPU, &rlim);
245		mtx_lock_spin(&sched_lock);
246		if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
247			mtx_unlock_spin(&sched_lock);
248			killproc(p, "exceeded maximum CPU limit");
249		} else {
250			if (p->p_cpulimit < rlim.rlim_max)
251				p->p_cpulimit += 5;
252			mtx_unlock_spin(&sched_lock);
253			psignal(p, SIGXCPU);
254		}
255		PROC_UNLOCK(p);
256	}
257#ifdef MAC
258	if (sflag & PS_MACPEND)
259		mac_thread_userret(td);
260#endif
261	if (flags & TDF_NEEDRESCHED) {
262#ifdef KTRACE
263		if (KTRPOINT(td, KTR_CSW))
264			ktrcsw(1, 1);
265#endif
266		mtx_lock_spin(&sched_lock);
267#ifdef KSE
268		sched_prio(td, kg->kg_user_pri);
269#else
270		sched_prio(td, td->td_user_pri);
271#endif
272		mi_switch(SW_INVOL, NULL);
273		mtx_unlock_spin(&sched_lock);
274#ifdef KTRACE
275		if (KTRPOINT(td, KTR_CSW))
276			ktrcsw(0, 1);
277#endif
278	}
279	if (flags & TDF_NEEDSIGCHK) {
280		PROC_LOCK(p);
281		mtx_lock(&p->p_sigacts->ps_mtx);
282		while ((sig = cursig(td)) != 0)
283			postsig(sig);
284		mtx_unlock(&p->p_sigacts->ps_mtx);
285		PROC_UNLOCK(p);
286	}
287
288	userret(td, framep);
289	mtx_assert(&Giant, MA_NOTOWNED);
290}
291