subr_syscall.c revision 184042
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 184042 2008-10-19 01:35:27Z kmacy $");
46116182Sobrien
47174395Sjkoshy#include "opt_hwpmc_hooks.h"
48118240Speter#include "opt_ktrace.h"
49104338Srwatson#include "opt_mac.h"
5078983Sjhb#ifdef __i386__
5171257Speter#include "opt_npx.h"
5278983Sjhb#endif
53170640Sjeff#include "opt_sched.h"
5413203Swollman
551549Srgrimes#include <sys/param.h>
5665557Sjasone#include <sys/bus.h>
571549Srgrimes#include <sys/kernel.h>
5878983Sjhb#include <sys/lock.h>
5967365Sjhb#include <sys/mutex.h>
60174395Sjkoshy#include <sys/pmckern.h>
6178983Sjhb#include <sys/proc.h>
6299072Sjulian#include <sys/ktr.h>
6331389Sbde#include <sys/resourcevar.h>
64104964Sjeff#include <sys/sched.h>
6531389Sbde#include <sys/signalvar.h>
6678983Sjhb#include <sys/systm.h>
6712662Sdg#include <sys/vmmeter.h>
68118240Speter#ifdef KTRACE
69118240Speter#include <sys/uio.h>
70118240Speter#include <sys/ktrace.h>
71118240Speter#endif
72118240Speter
731549Srgrimes#include <machine/cpu.h>
7431389Sbde#include <machine/pcb.h>
751549Srgrimes
76184042Skmacy#ifdef XEN
77184042Skmacy#include <vm/vm.h>
78184042Skmacy#include <vm/vm_param.h>
79184042Skmacy#include <vm/pmap.h>
80184042Skmacy#endif
81184042Skmacy
82163606Srwatson#include <security/mac/mac_framework.h>
83163606Srwatson
8478983Sjhb/*
85167211Srwatson * Define the code needed before returning to user mode, for trap and
86167211Srwatson * syscall.
8778983Sjhb */
8871527Sjhbvoid
89155455Sphkuserret(struct thread *td, struct trapframe *frame)
901690Sdg{
9183366Sjulian	struct proc *p = td->td_proc;
92757Sdg
9399072Sjulian	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
94173601Sjulian            td->td_name);
95126661Srwatson#ifdef DIAGNOSTIC
96110190Sjulian	/* Check that we called signotify() enough. */
9778636Sjhb	PROC_LOCK(p);
98170307Sjeff	thread_lock(td);
99112888Sjeff	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
100111032Sjulian	    (td->td_flags & TDF_ASTPENDING) == 0))
101102266Srwatson		printf("failed to set signal flags properly for ast()\n");
102170307Sjeff	thread_unlock(td);
10382585Sdillon	PROC_UNLOCK(p);
10493793Sbde#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	}
182174395Sjkoshy#if defined(HWPMC_HOOKS)
183174395Sjkoshy	if (td->td_pflags & TDP_CALLCHAIN) {
184174395Sjkoshy		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_USER_CALLCHAIN,
185174395Sjkoshy		    (void *) framep);
186174395Sjkoshy		td->td_pflags &= ~TDP_CALLCHAIN;
187174395Sjkoshy	}
188174395Sjkoshy#endif
189172207Sjeff	if (flags & TDF_ALRMPEND) {
19093390Sjake		PROC_LOCK(p);
19193390Sjake		psignal(p, SIGVTALRM);
19293390Sjake		PROC_UNLOCK(p);
19393390Sjake	}
19477015Sbde#if defined(DEV_NPX) && !defined(SMP)
19593390Sjake	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
19693390Sjake		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
19793390Sjake		    PCB_NPXTRAP);
19893390Sjake		ucode = npxtrap();
19993390Sjake		if (ucode != -1) {
200151316Sdavidxu			ksiginfo_init_trap(&ksi);
201151316Sdavidxu			ksi.ksi_signo = SIGFPE;
202151316Sdavidxu			ksi.ksi_code = ucode;
203151316Sdavidxu			trapsignal(td, &ksi);
20477015Sbde		}
20593390Sjake	}
20677015Sbde#endif
207172207Sjeff	if (flags & TDF_PROFPEND) {
20893390Sjake		PROC_LOCK(p);
20993390Sjake		psignal(p, SIGPROF);
21093390Sjake		PROC_UNLOCK(p);
21193390Sjake	}
212106655Srwatson#ifdef MAC
213172207Sjeff	if (flags & TDF_MACPEND)
214106655Srwatson		mac_thread_userret(td);
215106655Srwatson#endif
216111032Sjulian	if (flags & TDF_NEEDRESCHED) {
217118240Speter#ifdef KTRACE
218118240Speter		if (KTRPOINT(td, KTR_CSW))
219119781Speter			ktrcsw(1, 1);
220118240Speter#endif
221170307Sjeff		thread_lock(td);
222163709Sjb		sched_prio(td, td->td_user_pri);
223178272Sjeff		mi_switch(SW_INVOL | SWT_NEEDRESCHED, NULL);
224170307Sjeff		thread_unlock(td);
225118240Speter#ifdef KTRACE
226118240Speter		if (KTRPOINT(td, KTR_CSW))
227119781Speter			ktrcsw(0, 1);
228118240Speter#endif
22993793Sbde	}
230112888Sjeff	if (flags & TDF_NEEDSIGCHK) {
23193793Sbde		PROC_LOCK(p);
232114983Sjhb		mtx_lock(&p->p_sigacts->ps_mtx);
233116963Sdavidxu		while ((sig = cursig(td)) != 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
248155455Sphk	userret(td, framep);
24981493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
25024691Speter}
251