subr_syscall.c revision 170292
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 170292 2007-06-04 21:45:18Z attilio $");
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>
5367365Sjhb#include <sys/mutex.h>
5478983Sjhb#include <sys/proc.h>
5599072Sjulian#include <sys/ktr.h>
5631389Sbde#include <sys/resourcevar.h>
57104964Sjeff#include <sys/sched.h>
5831389Sbde#include <sys/signalvar.h>
5978983Sjhb#include <sys/systm.h>
6012662Sdg#include <sys/vmmeter.h>
61118240Speter#ifdef KTRACE
62118240Speter#include <sys/uio.h>
63118240Speter#include <sys/ktrace.h>
64118240Speter#endif
65118240Speter
661549Srgrimes#include <machine/cpu.h>
6731389Sbde#include <machine/pcb.h>
681549Srgrimes
69163606Srwatson#include <security/mac/mac_framework.h>
70163606Srwatson
7178983Sjhb/*
72167211Srwatson * Define the code needed before returning to user mode, for trap and
73167211Srwatson * syscall.
7478983Sjhb */
7571527Sjhbvoid
76155455Sphkuserret(struct thread *td, struct trapframe *frame)
771690Sdg{
7883366Sjulian	struct proc *p = td->td_proc;
79757Sdg
8099072Sjulian	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
8199072Sjulian            p->p_comm);
82126661Srwatson#ifdef DIAGNOSTIC
83110190Sjulian	/* Check that we called signotify() enough. */
8478636Sjhb	PROC_LOCK(p);
8593793Sbde	mtx_lock_spin(&sched_lock);
86112888Sjeff	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
87111032Sjulian	    (td->td_flags & TDF_ASTPENDING) == 0))
88102266Srwatson		printf("failed to set signal flags properly for ast()\n");
8993793Sbde	mtx_unlock_spin(&sched_lock);
9082585Sdillon	PROC_UNLOCK(p);
9193793Sbde#endif
9228013Sdyson
93152376Srwatson#ifdef KTRACE
94152376Srwatson	KTRUSERRET(td);
95152376Srwatson#endif
96152376Srwatson
9793793Sbde	/*
98136837Sphk	 * If this thread tickled GEOM, we need to wait for the giggling to
99136837Sphk	 * stop before we return to userland
100136837Sphk	 */
101136837Sphk	if (td->td_pflags & TDP_GEOM)
102136837Sphk		g_waitidle();
103136837Sphk
104136837Sphk	/*
105105974Sjulian	 * We need to check to see if we have to exit or wait due to a
106105974Sjulian	 * single threading requirement or some other STOP condition.
107105974Sjulian	 * Don't bother doing all the work if the stop bits are not set
108105974Sjulian	 * at this time.. If we miss it, we miss it.. no big deal.
10999072Sjulian	 */
110105974Sjulian	if (P_SHOULDSTOP(p)) {
111105974Sjulian		PROC_LOCK(p);
112105974Sjulian		thread_suspend_check(0);	/* Can suspend or kill */
113105974Sjulian		PROC_UNLOCK(p);
114105974Sjulian	}
115105974Sjulian
116163709Sjb#ifdef KSE
117105974Sjulian	/*
118105974Sjulian	 * Do special thread processing, e.g. upcall tweaking and such.
119105974Sjulian	 */
120126932Speter	if (p->p_flag & P_SA)
121103838Sjulian		thread_userret(td, frame);
122163709Sjb#endif
123110190Sjulian
124110190Sjulian	/*
125110190Sjulian	 * Charge system time if profiling.
126110190Sjulian	 */
127113874Sjhb	if (p->p_flag & P_PROFIL) {
128110190Sjulian
129155455Sphk		addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
130110190Sjulian	}
131139452Sjhb
132139324Sjeff	/*
133139324Sjeff	 * Let the scheduler adjust our priority etc.
134139324Sjeff	 */
135139324Sjeff	sched_userret(td);
136144061Sjeff	KASSERT(td->td_locks == 0,
137144061Sjeff	    ("userret: Returning with %d locks held.", td->td_locks));
1381690Sdg}
1391690Sdg
1404Srgrimes/*
14178983Sjhb * Process an asynchronous software trap.
14278983Sjhb * This is relatively easy.
14381493Sjhb * This function will return with preemption disabled.
1444Srgrimes */
145798Swollmanvoid
14699072Sjulianast(struct trapframe *framep)
14765557Sjasone{
148104297Sjhb	struct thread *td;
149104297Sjhb	struct proc *p;
15081493Sjhb	int sflag;
15183366Sjulian	int flags;
15293793Sbde	int sig;
15377015Sbde#if defined(DEV_NPX) && !defined(SMP)
15477015Sbde	int ucode;
155151316Sdavidxu	ksiginfo_t ksi;
15677015Sbde#endif
15765557Sjasone
158104297Sjhb	td = curthread;
159104297Sjhb	p = td->td_proc;
160104378Sjmallett
16199072Sjulian	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
16299072Sjulian            p->p_comm);
16372911Sjhb	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
164111883Sjhb	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
16581493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
16693793Sbde	mtx_assert(&sched_lock, MA_NOTOWNED);
16793390Sjake	td->td_frame = framep;
168155455Sphk	td->td_pticks = 0;
169104297Sjhb
170163709Sjb#ifdef KSE
171133340Sdavidxu	if ((p->p_flag & P_SA) && (td->td_mailbox == NULL))
172134571Sjulian		thread_user_enter(td);
173163709Sjb#endif
174135573Sjhb
17593390Sjake	/*
17693390Sjake	 * This updates the p_sflag's for the checks below in one
17793390Sjake	 * "atomic" operation with turning off the astpending flag.
17893390Sjake	 * If another AST is triggered while we are handling the
17993390Sjake	 * AST's saved in sflag, the astpending flag will be set and
18093390Sjake	 * ast() will be called again.
18193390Sjake	 */
18293390Sjake	mtx_lock_spin(&sched_lock);
183111032Sjulian	flags = td->td_flags;
18493390Sjake	sflag = p->p_sflag;
185170174Sjeff	if (p->p_sflag & (PS_ALRMPEND | PS_PROFPEND))
186170174Sjeff		p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND);
187106655Srwatson#ifdef MAC
188155504Sdavidxu	if (p->p_sflag & PS_MACPEND)
189155504Sdavidxu		p->p_sflag &= ~PS_MACPEND;
190106655Srwatson#endif
191112888Sjeff	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
192132266Sjhb	    TDF_NEEDRESCHED | TDF_INTERRUPT);
19393390Sjake	mtx_unlock_spin(&sched_lock);
194170292Sattilio	PCPU_INC(cnt.v_trap);
195135573Sjhb
19699072Sjulian	/*
19799072Sjulian	 * XXXKSE While the fact that we owe a user profiling
198164936Sjulian	 * tick is stored per thread in this code, the statistics
19999072Sjulian	 * themselves are still stored per process.
20099072Sjulian	 * This should probably change, by which I mean that
20199072Sjulian	 * possibly the location of both might change.
20299072Sjulian	 */
20393390Sjake	if (td->td_ucred != p->p_ucred)
20493390Sjake		cred_update_thread(td);
205132266Sjhb	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
206132266Sjhb		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
207132266Sjhb		td->td_profil_ticks = 0;
208132266Sjhb		td->td_pflags &= ~TDP_OWEUPC;
209131437Sjhb	}
21093390Sjake	if (sflag & PS_ALRMPEND) {
21193390Sjake		PROC_LOCK(p);
21293390Sjake		psignal(p, SIGVTALRM);
21393390Sjake		PROC_UNLOCK(p);
21493390Sjake	}
21577015Sbde#if defined(DEV_NPX) && !defined(SMP)
21693390Sjake	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
21793390Sjake		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
21893390Sjake		    PCB_NPXTRAP);
21993390Sjake		ucode = npxtrap();
22093390Sjake		if (ucode != -1) {
221151316Sdavidxu			ksiginfo_init_trap(&ksi);
222151316Sdavidxu			ksi.ksi_signo = SIGFPE;
223151316Sdavidxu			ksi.ksi_code = ucode;
224151316Sdavidxu			trapsignal(td, &ksi);
22577015Sbde		}
22693390Sjake	}
22777015Sbde#endif
22893390Sjake	if (sflag & PS_PROFPEND) {
22993390Sjake		PROC_LOCK(p);
23093390Sjake		psignal(p, SIGPROF);
23193390Sjake		PROC_UNLOCK(p);
23293390Sjake	}
233106655Srwatson#ifdef MAC
234106655Srwatson	if (sflag & PS_MACPEND)
235106655Srwatson		mac_thread_userret(td);
236106655Srwatson#endif
237111032Sjulian	if (flags & TDF_NEEDRESCHED) {
238118240Speter#ifdef KTRACE
239118240Speter		if (KTRPOINT(td, KTR_CSW))
240119781Speter			ktrcsw(1, 1);
241118240Speter#endif
24293793Sbde		mtx_lock_spin(&sched_lock);
243163709Sjb		sched_prio(td, td->td_user_pri);
244131473Sjhb		mi_switch(SW_INVOL, NULL);
24593793Sbde		mtx_unlock_spin(&sched_lock);
246118240Speter#ifdef KTRACE
247118240Speter		if (KTRPOINT(td, KTR_CSW))
248119781Speter			ktrcsw(0, 1);
249118240Speter#endif
25093793Sbde	}
251112888Sjeff	if (flags & TDF_NEEDSIGCHK) {
25293793Sbde		PROC_LOCK(p);
253114983Sjhb		mtx_lock(&p->p_sigacts->ps_mtx);
254116963Sdavidxu		while ((sig = cursig(td)) != 0)
25593793Sbde			postsig(sig);
256114983Sjhb		mtx_unlock(&p->p_sigacts->ps_mtx);
25793793Sbde		PROC_UNLOCK(p);
25893793Sbde	}
25965557Sjasone
260155455Sphk	userret(td, framep);
26181493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
26224691Speter}
263