subr_syscall.c revision 126661
14Srgrimes/*-
21690Sdg * Copyright (C) 1994, David Greenman
31690Sdg * Copyright (c) 1990, 1993
41690Sdg *	The Regents of the University of California.  All rights reserved.
54Srgrimes *
64Srgrimes * This code is derived from software contributed to Berkeley by
74Srgrimes * the University of Utah, and William Jolitz.
84Srgrimes *
94Srgrimes * Redistribution and use in source and binary forms, with or without
104Srgrimes * modification, are permitted provided that the following conditions
114Srgrimes * are met:
124Srgrimes * 1. Redistributions of source code must retain the above copyright
134Srgrimes *    notice, this list of conditions and the following disclaimer.
144Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
154Srgrimes *    notice, this list of conditions and the following disclaimer in the
164Srgrimes *    documentation and/or other materials provided with the distribution.
174Srgrimes * 3. All advertising materials mentioning features or use of this software
184Srgrimes *    must display the following acknowledgement:
194Srgrimes *	This product includes software developed by the University of
204Srgrimes *	California, Berkeley and its contributors.
214Srgrimes * 4. Neither the name of the University nor the names of its contributors
224Srgrimes *    may be used to endorse or promote products derived from this software
234Srgrimes *    without specific prior written permission.
244Srgrimes *
254Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354Srgrimes * SUCH DAMAGE.
364Srgrimes *
37608Srgrimes *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
384Srgrimes */
394Srgrimes
40116182Sobrien#include <sys/cdefs.h>
41116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 126661 2004-03-05 17:35:28Z rwatson $");
42116182Sobrien
43118240Speter#include "opt_ktrace.h"
44104338Srwatson#include "opt_mac.h"
4578983Sjhb#ifdef __i386__
4671257Speter#include "opt_npx.h"
4778983Sjhb#endif
4813203Swollman
491549Srgrimes#include <sys/param.h>
5065557Sjasone#include <sys/bus.h>
511549Srgrimes#include <sys/kernel.h>
5278983Sjhb#include <sys/lock.h>
53104338Srwatson#include <sys/mac.h>
5467365Sjhb#include <sys/mutex.h>
5578983Sjhb#include <sys/proc.h>
5699072Sjulian#include <sys/ktr.h>
5731389Sbde#include <sys/resourcevar.h>
58104964Sjeff#include <sys/sched.h>
5931389Sbde#include <sys/signalvar.h>
6078983Sjhb#include <sys/systm.h>
6112662Sdg#include <sys/vmmeter.h>
62118240Speter#ifdef KTRACE
63118240Speter#include <sys/uio.h>
64118240Speter#include <sys/ktrace.h>
65118240Speter#endif
66118240Speter
671549Srgrimes#include <machine/cpu.h>
6831389Sbde#include <machine/pcb.h>
691549Srgrimes
7078983Sjhb/*
7178983Sjhb * Define the code needed before returning to user mode, for
7278983Sjhb * trap and syscall.
7382585Sdillon *
7482585Sdillon * MPSAFE
7578983Sjhb */
7671527Sjhbvoid
7783366Sjulianuserret(td, frame, oticks)
7883366Sjulian	struct thread *td;
791690Sdg	struct trapframe *frame;
8081493Sjhb	u_int oticks;
811690Sdg{
8283366Sjulian	struct proc *p = td->td_proc;
83757Sdg
8499072Sjulian	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
8599072Sjulian            p->p_comm);
86126661Srwatson#ifdef DIAGNOSTIC
87110190Sjulian	/* Check that we called signotify() enough. */
8878636Sjhb	PROC_LOCK(p);
8993793Sbde	mtx_lock_spin(&sched_lock);
90112888Sjeff	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
91111032Sjulian	    (td->td_flags & TDF_ASTPENDING) == 0))
92102266Srwatson		printf("failed to set signal flags properly for ast()\n");
9393793Sbde	mtx_unlock_spin(&sched_lock);
9482585Sdillon	PROC_UNLOCK(p);
9593793Sbde#endif
9628013Sdyson
9793793Sbde	/*
98104964Sjeff	 * Let the scheduler adjust our priority etc.
9993793Sbde	 */
100104964Sjeff	sched_userret(td);
10171527Sjhb
1026296Sdg	/*
103105974Sjulian	 * We need to check to see if we have to exit or wait due to a
104105974Sjulian	 * single threading requirement or some other STOP condition.
105105974Sjulian	 * Don't bother doing all the work if the stop bits are not set
106105974Sjulian	 * at this time.. If we miss it, we miss it.. no big deal.
10799072Sjulian	 */
108105974Sjulian	if (P_SHOULDSTOP(p)) {
109105974Sjulian		PROC_LOCK(p);
110105974Sjulian		thread_suspend_check(0);	/* Can suspend or kill */
111105974Sjulian		PROC_UNLOCK(p);
112105974Sjulian	}
113105974Sjulian
114105974Sjulian	/*
115105974Sjulian	 * Do special thread processing, e.g. upcall tweaking and such.
116105974Sjulian	 */
117116361Sdavidxu	if (p->p_flag & P_SA) {
118103838Sjulian		thread_userret(td, frame);
11999072Sjulian	}
120110190Sjulian
121110190Sjulian	/*
122110190Sjulian	 * Charge system time if profiling.
123110190Sjulian	 */
124113874Sjhb	if (p->p_flag & P_PROFIL) {
125110190Sjulian		quad_t ticks;
126110190Sjulian
127110190Sjulian		mtx_lock_spin(&sched_lock);
128111024Sjeff		ticks = td->td_sticks - oticks;
129110190Sjulian		mtx_unlock_spin(&sched_lock);
130111032Sjulian		addupc_task(td, TRAPF_PC(frame), (u_int)ticks * psratio);
131110190Sjulian	}
1321690Sdg}
1331690Sdg
1344Srgrimes/*
13578983Sjhb * Process an asynchronous software trap.
13678983Sjhb * This is relatively easy.
13781493Sjhb * This function will return with preemption disabled.
1384Srgrimes */
139798Swollmanvoid
14099072Sjulianast(struct trapframe *framep)
14165557Sjasone{
142104297Sjhb	struct thread *td;
143104297Sjhb	struct proc *p;
144103838Sjulian	struct kse *ke;
145104297Sjhb	struct ksegrp *kg;
146125454Sjhb	struct rlimit rlim;
14781493Sjhb	u_int prticks, sticks;
14881493Sjhb	int sflag;
14983366Sjulian	int flags;
15093793Sbde	int sig;
15177015Sbde#if defined(DEV_NPX) && !defined(SMP)
15277015Sbde	int ucode;
15377015Sbde#endif
15465557Sjasone
155104297Sjhb	td = curthread;
156104297Sjhb	p = td->td_proc;
157104378Sjmallett	kg = td->td_ksegrp;
158104378Sjmallett
15999072Sjulian	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
16099072Sjulian            p->p_comm);
16172911Sjhb	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
162111883Sjhb	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
16381493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
16493793Sbde	mtx_assert(&sched_lock, MA_NOTOWNED);
16593390Sjake	td->td_frame = framep;
166104297Sjhb
16793390Sjake	/*
16893390Sjake	 * This updates the p_sflag's for the checks below in one
16993390Sjake	 * "atomic" operation with turning off the astpending flag.
17093390Sjake	 * If another AST is triggered while we are handling the
17193390Sjake	 * AST's saved in sflag, the astpending flag will be set and
17293390Sjake	 * ast() will be called again.
17393390Sjake	 */
17493390Sjake	mtx_lock_spin(&sched_lock);
175104383Sjmallett	ke = td->td_kse;
176111024Sjeff	sticks = td->td_sticks;
177111032Sjulian	flags = td->td_flags;
17893390Sjake	sflag = p->p_sflag;
179112888Sjeff	p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU);
180106655Srwatson#ifdef MAC
181106655Srwatson	p->p_sflag &= ~PS_MACPEND;
182106655Srwatson#endif
183112888Sjeff	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
184116963Sdavidxu	    TDF_NEEDRESCHED | TDF_OWEUPC | TDF_INTERRUPT);
18593390Sjake	cnt.v_soft++;
186104297Sjhb	prticks = 0;
187113874Sjhb	if (flags & TDF_OWEUPC && p->p_flag & P_PROFIL) {
188110190Sjulian		prticks = p->p_stats->p_prof.pr_ticks;
189110190Sjulian		p->p_stats->p_prof.pr_ticks = 0;
19093390Sjake	}
19193390Sjake	mtx_unlock_spin(&sched_lock);
19299072Sjulian	/*
19399072Sjulian	 * XXXKSE While the fact that we owe a user profiling
19499072Sjulian	 * tick is stored per KSE in this code, the statistics
19599072Sjulian	 * themselves are still stored per process.
19699072Sjulian	 * This should probably change, by which I mean that
19799072Sjulian	 * possibly the location of both might change.
19899072Sjulian	 */
19991090Sjulian
20093390Sjake	if (td->td_ucred != p->p_ucred)
20193390Sjake		cred_update_thread(td);
202113874Sjhb	if (flags & TDF_OWEUPC && p->p_flag & P_PROFIL)
203111032Sjulian		addupc_task(td, p->p_stats->p_prof.pr_addr, prticks);
20493390Sjake	if (sflag & PS_ALRMPEND) {
20593390Sjake		PROC_LOCK(p);
20693390Sjake		psignal(p, SIGVTALRM);
20793390Sjake		PROC_UNLOCK(p);
20893390Sjake	}
20977015Sbde#if defined(DEV_NPX) && !defined(SMP)
21093390Sjake	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
21193390Sjake		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
21293390Sjake		    PCB_NPXTRAP);
21393390Sjake		ucode = npxtrap();
21493390Sjake		if (ucode != -1) {
215112883Sjeff			trapsignal(td, SIGFPE, ucode);
21677015Sbde		}
21793390Sjake	}
21877015Sbde#endif
21993390Sjake	if (sflag & PS_PROFPEND) {
22093390Sjake		PROC_LOCK(p);
22193390Sjake		psignal(p, SIGPROF);
22293390Sjake		PROC_UNLOCK(p);
22393390Sjake	}
224104240Sjhb	if (sflag & PS_XCPU) {
225104240Sjhb		PROC_LOCK(p);
226125454Sjhb		lim_rlimit(p, RLIMIT_CPU, &rlim);
227113636Sjhb		mtx_lock_spin(&sched_lock);
228125454Sjhb		if (p->p_runtime.sec >= rlim.rlim_max) {
229113636Sjhb			mtx_unlock_spin(&sched_lock);
230104240Sjhb			killproc(p, "exceeded maximum CPU limit");
231113636Sjhb		} else {
232125454Sjhb			if (p->p_cpulimit < rlim.rlim_max)
233104719Sjhb				p->p_cpulimit += 5;
234104719Sjhb			mtx_unlock_spin(&sched_lock);
235113636Sjhb			psignal(p, SIGXCPU);
236104240Sjhb		}
237104240Sjhb		PROC_UNLOCK(p);
238104240Sjhb	}
239106655Srwatson#ifdef MAC
240106655Srwatson	if (sflag & PS_MACPEND)
241106655Srwatson		mac_thread_userret(td);
242106655Srwatson#endif
243111032Sjulian	if (flags & TDF_NEEDRESCHED) {
244118240Speter#ifdef KTRACE
245118240Speter		if (KTRPOINT(td, KTR_CSW))
246119781Speter			ktrcsw(1, 1);
247118240Speter#endif
24893793Sbde		mtx_lock_spin(&sched_lock);
249104964Sjeff		sched_prio(td, kg->kg_user_pri);
250124944Sjeff		mi_switch(SW_INVOL);
25193793Sbde		mtx_unlock_spin(&sched_lock);
252118240Speter#ifdef KTRACE
253118240Speter		if (KTRPOINT(td, KTR_CSW))
254119781Speter			ktrcsw(0, 1);
255118240Speter#endif
25693793Sbde	}
257112888Sjeff	if (flags & TDF_NEEDSIGCHK) {
25893793Sbde		PROC_LOCK(p);
259114983Sjhb		mtx_lock(&p->p_sigacts->ps_mtx);
260116963Sdavidxu		while ((sig = cursig(td)) != 0)
26193793Sbde			postsig(sig);
262114983Sjhb		mtx_unlock(&p->p_sigacts->ps_mtx);
26393793Sbde		PROC_UNLOCK(p);
26493793Sbde	}
26565557Sjasone
26693390Sjake	userret(td, framep, sticks);
26799753Smini#ifdef DIAGNOSTIC
26899753Smini	cred_free_thread(td);
26999753Smini#endif
27081493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
27124691Speter}
272