subr_syscall.c revision 106655
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
3850477Speter * $FreeBSD: head/sys/kern/subr_trap.c 106655 2002-11-08 19:00:17Z rwatson $
394Srgrimes */
404Srgrimes
41104338Srwatson#include "opt_mac.h"
4278983Sjhb#ifdef __i386__
4371257Speter#include "opt_npx.h"
4478983Sjhb#endif
4513203Swollman
461549Srgrimes#include <sys/param.h>
4765557Sjasone#include <sys/bus.h>
481549Srgrimes#include <sys/kernel.h>
4978983Sjhb#include <sys/lock.h>
50104338Srwatson#include <sys/mac.h>
5167365Sjhb#include <sys/mutex.h>
5278983Sjhb#include <sys/proc.h>
5399072Sjulian#include <sys/kse.h>
5499072Sjulian#include <sys/ktr.h>
5531389Sbde#include <sys/resourcevar.h>
56104964Sjeff#include <sys/sched.h>
5731389Sbde#include <sys/signalvar.h>
5878983Sjhb#include <sys/systm.h>
5912662Sdg#include <sys/vmmeter.h>
601549Srgrimes#include <machine/cpu.h>
6131389Sbde#include <machine/pcb.h>
621549Srgrimes
6378983Sjhb/*
6478983Sjhb * Define the code needed before returning to user mode, for
6578983Sjhb * trap and syscall.
6682585Sdillon *
6782585Sdillon * MPSAFE
6878983Sjhb */
6971527Sjhbvoid
7083366Sjulianuserret(td, frame, oticks)
7183366Sjulian	struct thread *td;
721690Sdg	struct trapframe *frame;
7381493Sjhb	u_int oticks;
741690Sdg{
7583366Sjulian	struct proc *p = td->td_proc;
7683366Sjulian	struct kse *ke = td->td_kse;
77757Sdg
7899072Sjulian	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
7999072Sjulian            p->p_comm);
8093793Sbde#ifdef INVARIANTS
8193793Sbde	/* Check that we called signotify() enough. */
8279125Sjhb	mtx_lock(&Giant);
8378636Sjhb	PROC_LOCK(p);
8493793Sbde	mtx_lock_spin(&sched_lock);
85104306Sjmallett	if (SIGPENDING(p) && ((p->p_sflag & PS_NEEDSIGCHK) == 0 ||
86104383Sjmallett	    (td->td_kse->ke_flags & KEF_ASTPENDING) == 0))
87102266Srwatson		printf("failed to set signal flags properly for ast()\n");
8893793Sbde	mtx_unlock_spin(&sched_lock);
8982585Sdillon	PROC_UNLOCK(p);
9079125Sjhb	mtx_unlock(&Giant);
9193793Sbde#endif
9228013Sdyson
9393793Sbde	/*
94104964Sjeff	 * Let the scheduler adjust our priority etc.
9593793Sbde	 */
96104964Sjeff	sched_userret(td);
9771527Sjhb
986296Sdg	/*
99105974Sjulian	 * We need to check to see if we have to exit or wait due to a
100105974Sjulian	 * single threading requirement or some other STOP condition.
101105974Sjulian	 * Don't bother doing all the work if the stop bits are not set
102105974Sjulian	 * at this time.. If we miss it, we miss it.. no big deal.
10399072Sjulian	 */
104105974Sjulian	if (P_SHOULDSTOP(p)) {
105105974Sjulian		PROC_LOCK(p);
106105974Sjulian		thread_suspend_check(0);	/* Can suspend or kill */
107105974Sjulian		PROC_UNLOCK(p);
108105974Sjulian	}
109105974Sjulian
110105974Sjulian	/*
111105974Sjulian	 * Do special thread processing, e.g. upcall tweaking and such.
112105974Sjulian	 */
11399072Sjulian	if (p->p_flag & P_KSES) {
114103838Sjulian		thread_userret(td, frame);
11599072Sjulian		/* printf("KSE thread returned"); */
11699072Sjulian	}
11799072Sjulian
11899072Sjulian	/*
1196296Sdg	 * Charge system time if profiling.
12093793Sbde	 *
12193793Sbde	 * XXX should move PS_PROFIL to a place that can obviously be
12293793Sbde	 * accessed safely without sched_lock.
1236296Sdg	 */
12483366Sjulian	if (p->p_sflag & PS_PROFIL) {
12588119Sjhb		quad_t ticks;
12688119Sjhb
12793793Sbde		mtx_lock_spin(&sched_lock);
12888119Sjhb		ticks = ke->ke_sticks - oticks;
12988119Sjhb		mtx_unlock_spin(&sched_lock);
13088119Sjhb		addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio);
13193793Sbde	}
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;
146104240Sjhb	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"));
16281493Sjhb#ifdef WITNESS
16383366Sjulian	if (witness_list(td))
16481493Sjhb		panic("Returning to user mode with mutex(s) held");
16581493Sjhb#endif
16681493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
16793793Sbde	mtx_assert(&sched_lock, MA_NOTOWNED);
16893390Sjake	td->td_frame = framep;
169104297Sjhb
17093390Sjake	/*
17193390Sjake	 * This updates the p_sflag's for the checks below in one
17293390Sjake	 * "atomic" operation with turning off the astpending flag.
17393390Sjake	 * If another AST is triggered while we are handling the
17493390Sjake	 * AST's saved in sflag, the astpending flag will be set and
17593390Sjake	 * ast() will be called again.
17693390Sjake	 */
17793390Sjake	mtx_lock_spin(&sched_lock);
178104383Sjmallett	ke = td->td_kse;
17993390Sjake	sticks = ke->ke_sticks;
180104297Sjhb	flags = ke->ke_flags;
18193390Sjake	sflag = p->p_sflag;
182104296Sjhb	p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND | PS_XCPU);
183106655Srwatson#ifdef MAC
184106655Srwatson	p->p_sflag &= ~PS_MACPEND;
185106655Srwatson#endif
18693793Sbde	ke->ke_flags &= ~(KEF_ASTPENDING | KEF_NEEDRESCHED | KEF_OWEUPC);
18793390Sjake	cnt.v_soft++;
188104297Sjhb	prticks = 0;
18993390Sjake	if (flags & KEF_OWEUPC && sflag & PS_PROFIL) {
19093390Sjake		prticks = p->p_stats->p_prof.pr_ticks;
19193390Sjake		p->p_stats->p_prof.pr_ticks = 0;
19293390Sjake	}
19393390Sjake	mtx_unlock_spin(&sched_lock);
19499072Sjulian	/*
19599072Sjulian	 * XXXKSE While the fact that we owe a user profiling
19699072Sjulian	 * tick is stored per KSE in this code, the statistics
19799072Sjulian	 * themselves are still stored per process.
19899072Sjulian	 * This should probably change, by which I mean that
19999072Sjulian	 * possibly the location of both might change.
20099072Sjulian	 */
20191090Sjulian
20293390Sjake	if (td->td_ucred != p->p_ucred)
20393390Sjake		cred_update_thread(td);
20493390Sjake	if (flags & KEF_OWEUPC && sflag & PS_PROFIL)
20593390Sjake		addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks);
20693390Sjake	if (sflag & PS_ALRMPEND) {
20793390Sjake		PROC_LOCK(p);
20893390Sjake		psignal(p, SIGVTALRM);
20993390Sjake		PROC_UNLOCK(p);
21093390Sjake	}
21177015Sbde#if defined(DEV_NPX) && !defined(SMP)
21293390Sjake	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
21393390Sjake		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
21493390Sjake		    PCB_NPXTRAP);
21593390Sjake		ucode = npxtrap();
21693390Sjake		if (ucode != -1) {
21793390Sjake			trapsignal(p, SIGFPE, ucode);
21877015Sbde		}
21993390Sjake	}
22077015Sbde#endif
22193390Sjake	if (sflag & PS_PROFPEND) {
22293390Sjake		PROC_LOCK(p);
22393390Sjake		psignal(p, SIGPROF);
22493390Sjake		PROC_UNLOCK(p);
22593390Sjake	}
226104240Sjhb	if (sflag & PS_XCPU) {
227104240Sjhb		PROC_LOCK(p);
228104240Sjhb		rlim = &p->p_rlimit[RLIMIT_CPU];
229104240Sjhb		if (p->p_runtime.sec >= rlim->rlim_max)
230104240Sjhb			killproc(p, "exceeded maximum CPU limit");
231104240Sjhb		else {
232104240Sjhb			psignal(p, SIGXCPU);
233104719Sjhb			mtx_lock_spin(&sched_lock);
234104719Sjhb			if (p->p_cpulimit < rlim->rlim_max)
235104719Sjhb				p->p_cpulimit += 5;
236104719Sjhb			mtx_unlock_spin(&sched_lock);
237104240Sjhb		}
238104240Sjhb		PROC_UNLOCK(p);
239104240Sjhb	}
240106655Srwatson#ifdef MAC
241106655Srwatson	if (sflag & PS_MACPEND)
242106655Srwatson		mac_thread_userret(td);
243106655Srwatson#endif
24493793Sbde	if (flags & KEF_NEEDRESCHED) {
24593793Sbde		mtx_lock_spin(&sched_lock);
246104964Sjeff		sched_prio(td, kg->kg_user_pri);
24793793Sbde		p->p_stats->p_ru.ru_nivcsw++;
24893793Sbde		mi_switch();
24993793Sbde		mtx_unlock_spin(&sched_lock);
25093793Sbde	}
25193793Sbde	if (sflag & PS_NEEDSIGCHK) {
25293793Sbde		PROC_LOCK(p);
25399072Sjulian		while ((sig = cursig(td)) != 0)
25493793Sbde			postsig(sig);
25593793Sbde		PROC_UNLOCK(p);
25693793Sbde	}
25765557Sjasone
25893390Sjake	userret(td, framep, sticks);
25999753Smini#ifdef DIAGNOSTIC
26099753Smini	cred_free_thread(td);
26199753Smini#endif
26281493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
26324691Speter}
264