subr_syscall.c revision 198508
14Srgrimes/*-
21690Sdg * Copyright (C) 1994, David Greenman
31690Sdg * Copyright (c) 1990, 1993
41690Sdg *	The Regents of the University of California.  All rights reserved.
5174395Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation
64Srgrimes *
74Srgrimes * This code is derived from software contributed to Berkeley by
84Srgrimes * the University of Utah, and William Jolitz.
94Srgrimes *
10174395Sjkoshy * Portions of this software were developed by A. Joseph Koshy under
11174395Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc.
12174395Sjkoshy *
134Srgrimes * Redistribution and use in source and binary forms, with or without
144Srgrimes * modification, are permitted provided that the following conditions
154Srgrimes * are met:
164Srgrimes * 1. Redistributions of source code must retain the above copyright
174Srgrimes *    notice, this list of conditions and the following disclaimer.
184Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
194Srgrimes *    notice, this list of conditions and the following disclaimer in the
204Srgrimes *    documentation and/or other materials provided with the distribution.
214Srgrimes * 3. All advertising materials mentioning features or use of this software
224Srgrimes *    must display the following acknowledgement:
234Srgrimes *	This product includes software developed by the University of
244Srgrimes *	California, Berkeley and its contributors.
254Srgrimes * 4. Neither the name of the University nor the names of its contributors
264Srgrimes *    may be used to endorse or promote products derived from this software
274Srgrimes *    without specific prior written permission.
284Srgrimes *
294Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
304Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
314Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
324Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
334Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
344Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
354Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
364Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
374Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
384Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
394Srgrimes * SUCH DAMAGE.
404Srgrimes *
41608Srgrimes *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
424Srgrimes */
434Srgrimes
44116182Sobrien#include <sys/cdefs.h>
45116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 198508 2009-10-27 10:55:34Z kib $");
46116182Sobrien
47118240Speter#include "opt_ktrace.h"
4878983Sjhb#ifdef __i386__
4971257Speter#include "opt_npx.h"
5078983Sjhb#endif
51170640Sjeff#include "opt_sched.h"
5213203Swollman
531549Srgrimes#include <sys/param.h>
5465557Sjasone#include <sys/bus.h>
551549Srgrimes#include <sys/kernel.h>
5678983Sjhb#include <sys/lock.h>
5767365Sjhb#include <sys/mutex.h>
58174395Sjkoshy#include <sys/pmckern.h>
5978983Sjhb#include <sys/proc.h>
6099072Sjulian#include <sys/ktr.h>
6131389Sbde#include <sys/resourcevar.h>
62104964Sjeff#include <sys/sched.h>
6331389Sbde#include <sys/signalvar.h>
6478983Sjhb#include <sys/systm.h>
6512662Sdg#include <sys/vmmeter.h>
66118240Speter#ifdef KTRACE
67118240Speter#include <sys/uio.h>
68118240Speter#include <sys/ktrace.h>
69118240Speter#endif
70118240Speter
711549Srgrimes#include <machine/cpu.h>
7231389Sbde#include <machine/pcb.h>
731549Srgrimes
74184042Skmacy#ifdef XEN
75184042Skmacy#include <vm/vm.h>
76184042Skmacy#include <vm/vm_param.h>
77184042Skmacy#include <vm/pmap.h>
78184042Skmacy#endif
79184042Skmacy
80163606Srwatson#include <security/mac/mac_framework.h>
81163606Srwatson
8278983Sjhb/*
83167211Srwatson * Define the code needed before returning to user mode, for trap and
84167211Srwatson * syscall.
8578983Sjhb */
8671527Sjhbvoid
87155455Sphkuserret(struct thread *td, struct trapframe *frame)
881690Sdg{
8983366Sjulian	struct proc *p = td->td_proc;
90757Sdg
9199072Sjulian	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
92173601Sjulian            td->td_name);
93197963Skib#if 0
94126661Srwatson#ifdef DIAGNOSTIC
95110190Sjulian	/* Check that we called signotify() enough. */
9678636Sjhb	PROC_LOCK(p);
97170307Sjeff	thread_lock(td);
98112888Sjeff	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
99111032Sjulian	    (td->td_flags & TDF_ASTPENDING) == 0))
100102266Srwatson		printf("failed to set signal flags properly for ast()\n");
101170307Sjeff	thread_unlock(td);
10282585Sdillon	PROC_UNLOCK(p);
10393793Sbde#endif
104197963Skib#endif
105152376Srwatson#ifdef KTRACE
106152376Srwatson	KTRUSERRET(td);
107152376Srwatson#endif
10893793Sbde	/*
109136837Sphk	 * If this thread tickled GEOM, we need to wait for the giggling to
110136837Sphk	 * stop before we return to userland
111136837Sphk	 */
112136837Sphk	if (td->td_pflags & TDP_GEOM)
113136837Sphk		g_waitidle();
114136837Sphk
115136837Sphk	/*
116110190Sjulian	 * Charge system time if profiling.
117110190Sjulian	 */
118113874Sjhb	if (p->p_flag & P_PROFIL) {
119155455Sphk		addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
120110190Sjulian	}
121139324Sjeff	/*
122139324Sjeff	 * Let the scheduler adjust our priority etc.
123139324Sjeff	 */
124139324Sjeff	sched_userret(td);
125144061Sjeff	KASSERT(td->td_locks == 0,
126144061Sjeff	    ("userret: Returning with %d locks held.", td->td_locks));
127184042Skmacy#ifdef XEN
128184042Skmacy	PT_UPDATES_FLUSH();
129184042Skmacy#endif
1301690Sdg}
1311690Sdg
1324Srgrimes/*
13378983Sjhb * Process an asynchronous software trap.
13478983Sjhb * This is relatively easy.
13581493Sjhb * This function will return with preemption disabled.
1364Srgrimes */
137798Swollmanvoid
13899072Sjulianast(struct trapframe *framep)
13965557Sjasone{
140104297Sjhb	struct thread *td;
141104297Sjhb	struct proc *p;
14283366Sjulian	int flags;
14393793Sbde	int sig;
14477015Sbde#if defined(DEV_NPX) && !defined(SMP)
14577015Sbde	int ucode;
146151316Sdavidxu	ksiginfo_t ksi;
14777015Sbde#endif
14865557Sjasone
149104297Sjhb	td = curthread;
150104297Sjhb	p = td->td_proc;
151104378Sjmallett
15299072Sjulian	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
15399072Sjulian            p->p_comm);
15472911Sjhb	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
155111883Sjhb	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
15681493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
157170307Sjeff	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
15893390Sjake	td->td_frame = framep;
159155455Sphk	td->td_pticks = 0;
160104297Sjhb
16193390Sjake	/*
162172207Sjeff	 * This updates the td_flag's for the checks below in one
16393390Sjake	 * "atomic" operation with turning off the astpending flag.
16493390Sjake	 * If another AST is triggered while we are handling the
165172207Sjeff	 * AST's saved in flags, the astpending flag will be set and
16693390Sjake	 * ast() will be called again.
16793390Sjake	 */
168170307Sjeff	thread_lock(td);
169170307Sjeff	flags = td->td_flags;
170177471Sjeff	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK |
171177471Sjeff	    TDF_NEEDRESCHED | TDF_ALRMPEND | TDF_PROFPEND | TDF_MACPEND);
172170307Sjeff	thread_unlock(td);
173170292Sattilio	PCPU_INC(cnt.v_trap);
174135573Sjhb
17593390Sjake	if (td->td_ucred != p->p_ucred)
17693390Sjake		cred_update_thread(td);
177132266Sjhb	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
178132266Sjhb		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
179132266Sjhb		td->td_profil_ticks = 0;
180132266Sjhb		td->td_pflags &= ~TDP_OWEUPC;
181131437Sjhb	}
182172207Sjeff	if (flags & TDF_ALRMPEND) {
18393390Sjake		PROC_LOCK(p);
18493390Sjake		psignal(p, SIGVTALRM);
18593390Sjake		PROC_UNLOCK(p);
18693390Sjake	}
18777015Sbde#if defined(DEV_NPX) && !defined(SMP)
18893390Sjake	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
18993390Sjake		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
19093390Sjake		    PCB_NPXTRAP);
19193390Sjake		ucode = npxtrap();
19293390Sjake		if (ucode != -1) {
193151316Sdavidxu			ksiginfo_init_trap(&ksi);
194151316Sdavidxu			ksi.ksi_signo = SIGFPE;
195151316Sdavidxu			ksi.ksi_code = ucode;
196151316Sdavidxu			trapsignal(td, &ksi);
19777015Sbde		}
19893390Sjake	}
19977015Sbde#endif
200172207Sjeff	if (flags & TDF_PROFPEND) {
20193390Sjake		PROC_LOCK(p);
20293390Sjake		psignal(p, SIGPROF);
20393390Sjake		PROC_UNLOCK(p);
20493390Sjake	}
205106655Srwatson#ifdef MAC
206172207Sjeff	if (flags & TDF_MACPEND)
207106655Srwatson		mac_thread_userret(td);
208106655Srwatson#endif
209111032Sjulian	if (flags & TDF_NEEDRESCHED) {
210118240Speter#ifdef KTRACE
211118240Speter		if (KTRPOINT(td, KTR_CSW))
212119781Speter			ktrcsw(1, 1);
213118240Speter#endif
214170307Sjeff		thread_lock(td);
215163709Sjb		sched_prio(td, td->td_user_pri);
216178272Sjeff		mi_switch(SW_INVOL | SWT_NEEDRESCHED, NULL);
217170307Sjeff		thread_unlock(td);
218118240Speter#ifdef KTRACE
219118240Speter		if (KTRPOINT(td, KTR_CSW))
220119781Speter			ktrcsw(0, 1);
221118240Speter#endif
22293793Sbde	}
223197963Skib
224197963Skib	/*
225197963Skib	 * Check for signals. Unlocked reads of p_pendingcnt or
226197963Skib	 * p_siglist might cause process-directed signal to be handled
227197963Skib	 * later.
228197963Skib	 */
229197963Skib	if (flags & TDF_NEEDSIGCHK || p->p_pendingcnt > 0 ||
230197963Skib	    !SIGISEMPTY(p->p_siglist)) {
23193793Sbde		PROC_LOCK(p);
232114983Sjhb		mtx_lock(&p->p_sigacts->ps_mtx);
233195702Skib		while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
23493793Sbde			postsig(sig);
235114983Sjhb		mtx_unlock(&p->p_sigacts->ps_mtx);
23693793Sbde		PROC_UNLOCK(p);
23793793Sbde	}
238177471Sjeff	/*
239177471Sjeff	 * We need to check to see if we have to exit or wait due to a
240177471Sjeff	 * single threading requirement or some other STOP condition.
241177471Sjeff	 */
242177471Sjeff	if (flags & TDF_NEEDSUSPCHK) {
243177471Sjeff		PROC_LOCK(p);
244177471Sjeff		thread_suspend_check(0);
245177471Sjeff		PROC_UNLOCK(p);
246177471Sjeff	}
24765557Sjasone
248198508Skib	if (td->td_pflags & TDP_OLDMASK) {
249198508Skib		td->td_pflags &= ~TDP_OLDMASK;
250198508Skib		kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
251198508Skib	}
252198508Skib
253155455Sphk	userret(td, framep);
25481493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
25524691Speter}
256