subr_syscall.c revision 219133
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 219133 2011-03-01 13:32:07Z rwatson $");
46116182Sobrien
47219133Srwatson#include "opt_capabilities.h"
48118240Speter#include "opt_ktrace.h"
49209258Srpaulo#include "opt_kdtrace.h"
50170640Sjeff#include "opt_sched.h"
5113203Swollman
521549Srgrimes#include <sys/param.h>
5365557Sjasone#include <sys/bus.h>
54219133Srwatson#include <sys/capability.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>
61208453Skib#include <sys/pioctl.h>
62208453Skib#include <sys/ptrace.h>
6331389Sbde#include <sys/resourcevar.h>
64104964Sjeff#include <sys/sched.h>
6531389Sbde#include <sys/signalvar.h>
66208453Skib#include <sys/syscall.h>
67209613Sjhb#include <sys/syscallsubr.h>
68208453Skib#include <sys/sysent.h>
6978983Sjhb#include <sys/systm.h>
7012662Sdg#include <sys/vmmeter.h>
71118240Speter#ifdef KTRACE
72118240Speter#include <sys/uio.h>
73118240Speter#include <sys/ktrace.h>
74118240Speter#endif
75208453Skib#include <security/audit/audit.h>
76118240Speter
771549Srgrimes#include <machine/cpu.h>
781549Srgrimes
79218688Sbz#ifdef VIMAGE
80218688Sbz#include <net/vnet.h>
81218688Sbz#endif
82218688Sbz
83184042Skmacy#ifdef XEN
84184042Skmacy#include <vm/vm.h>
85184042Skmacy#include <vm/vm_param.h>
86184042Skmacy#include <vm/pmap.h>
87184042Skmacy#endif
88184042Skmacy
89163606Srwatson#include <security/mac/mac_framework.h>
90163606Srwatson
9178983Sjhb/*
92167211Srwatson * Define the code needed before returning to user mode, for trap and
93167211Srwatson * syscall.
9478983Sjhb */
9571527Sjhbvoid
96155455Sphkuserret(struct thread *td, struct trapframe *frame)
971690Sdg{
9883366Sjulian	struct proc *p = td->td_proc;
99757Sdg
10099072Sjulian	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
101173601Sjulian            td->td_name);
102197963Skib#if 0
103126661Srwatson#ifdef DIAGNOSTIC
104110190Sjulian	/* Check that we called signotify() enough. */
10578636Sjhb	PROC_LOCK(p);
106170307Sjeff	thread_lock(td);
107112888Sjeff	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
108111032Sjulian	    (td->td_flags & TDF_ASTPENDING) == 0))
109102266Srwatson		printf("failed to set signal flags properly for ast()\n");
110170307Sjeff	thread_unlock(td);
11182585Sdillon	PROC_UNLOCK(p);
11293793Sbde#endif
113197963Skib#endif
114152376Srwatson#ifdef KTRACE
115152376Srwatson	KTRUSERRET(td);
116152376Srwatson#endif
11793793Sbde	/*
118136837Sphk	 * If this thread tickled GEOM, we need to wait for the giggling to
119136837Sphk	 * stop before we return to userland
120136837Sphk	 */
121136837Sphk	if (td->td_pflags & TDP_GEOM)
122136837Sphk		g_waitidle();
123136837Sphk
124136837Sphk	/*
125110190Sjulian	 * Charge system time if profiling.
126110190Sjulian	 */
127213236Semaste	if (p->p_flag & P_PROFIL)
128155455Sphk		addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
129139324Sjeff	/*
130139324Sjeff	 * Let the scheduler adjust our priority etc.
131139324Sjeff	 */
132139324Sjeff	sched_userret(td);
133144061Sjeff	KASSERT(td->td_locks == 0,
134144061Sjeff	    ("userret: Returning with %d locks held.", td->td_locks));
135218688Sbz#ifdef VIMAGE
136218688Sbz	/* Unfortunately td_vnet_lpush needs VNET_DEBUG. */
137218688Sbz	VNET_ASSERT(curvnet == NULL,
138218688Sbz	    ("%s: Returning on td %p (pid %d, %s) with vnet %p set in %s",
139218688Sbz	    __func__, td, p->p_pid, td->td_name, curvnet,
140218688Sbz	    (td->td_vnet_lpush != NULL) ? td->td_vnet_lpush : "N/A"));
141218688Sbz#endif
142184042Skmacy#ifdef XEN
143184042Skmacy	PT_UPDATES_FLUSH();
144184042Skmacy#endif
1451690Sdg}
1461690Sdg
1474Srgrimes/*
14878983Sjhb * Process an asynchronous software trap.
14978983Sjhb * This is relatively easy.
15081493Sjhb * This function will return with preemption disabled.
1514Srgrimes */
152798Swollmanvoid
15399072Sjulianast(struct trapframe *framep)
15465557Sjasone{
155104297Sjhb	struct thread *td;
156104297Sjhb	struct proc *p;
15783366Sjulian	int flags;
15893793Sbde	int sig;
15965557Sjasone
160104297Sjhb	td = curthread;
161104297Sjhb	p = td->td_proc;
162104378Sjmallett
16399072Sjulian	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
16499072Sjulian            p->p_comm);
16572911Sjhb	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
166111883Sjhb	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
16781493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
168170307Sjeff	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
16993390Sjake	td->td_frame = framep;
170155455Sphk	td->td_pticks = 0;
171104297Sjhb
17293390Sjake	/*
173172207Sjeff	 * This updates the td_flag's for the checks below in one
17493390Sjake	 * "atomic" operation with turning off the astpending flag.
17593390Sjake	 * If another AST is triggered while we are handling the
176172207Sjeff	 * AST's saved in flags, the astpending flag will be set and
17793390Sjake	 * ast() will be called again.
17893390Sjake	 */
179170307Sjeff	thread_lock(td);
180170307Sjeff	flags = td->td_flags;
181177471Sjeff	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK |
182177471Sjeff	    TDF_NEEDRESCHED | TDF_ALRMPEND | TDF_PROFPEND | TDF_MACPEND);
183170307Sjeff	thread_unlock(td);
184170292Sattilio	PCPU_INC(cnt.v_trap);
185135573Sjhb
18693390Sjake	if (td->td_ucred != p->p_ucred)
18793390Sjake		cred_update_thread(td);
188132266Sjhb	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
189132266Sjhb		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
190132266Sjhb		td->td_profil_ticks = 0;
191132266Sjhb		td->td_pflags &= ~TDP_OWEUPC;
192131437Sjhb	}
193172207Sjeff	if (flags & TDF_ALRMPEND) {
19493390Sjake		PROC_LOCK(p);
19593390Sjake		psignal(p, SIGVTALRM);
19693390Sjake		PROC_UNLOCK(p);
19793390Sjake	}
198172207Sjeff	if (flags & TDF_PROFPEND) {
19993390Sjake		PROC_LOCK(p);
20093390Sjake		psignal(p, SIGPROF);
20193390Sjake		PROC_UNLOCK(p);
20293390Sjake	}
203106655Srwatson#ifdef MAC
204172207Sjeff	if (flags & TDF_MACPEND)
205106655Srwatson		mac_thread_userret(td);
206106655Srwatson#endif
207111032Sjulian	if (flags & TDF_NEEDRESCHED) {
208118240Speter#ifdef KTRACE
209118240Speter		if (KTRPOINT(td, KTR_CSW))
210119781Speter			ktrcsw(1, 1);
211118240Speter#endif
212170307Sjeff		thread_lock(td);
213163709Sjb		sched_prio(td, td->td_user_pri);
214178272Sjeff		mi_switch(SW_INVOL | SWT_NEEDRESCHED, NULL);
215170307Sjeff		thread_unlock(td);
216118240Speter#ifdef KTRACE
217118240Speter		if (KTRPOINT(td, KTR_CSW))
218119781Speter			ktrcsw(0, 1);
219118240Speter#endif
22093793Sbde	}
221197963Skib
222197963Skib	/*
223197963Skib	 * Check for signals. Unlocked reads of p_pendingcnt or
224197963Skib	 * p_siglist might cause process-directed signal to be handled
225197963Skib	 * later.
226197963Skib	 */
227197963Skib	if (flags & TDF_NEEDSIGCHK || p->p_pendingcnt > 0 ||
228197963Skib	    !SIGISEMPTY(p->p_siglist)) {
22993793Sbde		PROC_LOCK(p);
230114983Sjhb		mtx_lock(&p->p_sigacts->ps_mtx);
231195702Skib		while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
23293793Sbde			postsig(sig);
233114983Sjhb		mtx_unlock(&p->p_sigacts->ps_mtx);
23493793Sbde		PROC_UNLOCK(p);
23593793Sbde	}
236177471Sjeff	/*
237177471Sjeff	 * We need to check to see if we have to exit or wait due to a
238177471Sjeff	 * single threading requirement or some other STOP condition.
239177471Sjeff	 */
240177471Sjeff	if (flags & TDF_NEEDSUSPCHK) {
241177471Sjeff		PROC_LOCK(p);
242177471Sjeff		thread_suspend_check(0);
243177471Sjeff		PROC_UNLOCK(p);
244177471Sjeff	}
24565557Sjasone
246198508Skib	if (td->td_pflags & TDP_OLDMASK) {
247198508Skib		td->td_pflags &= ~TDP_OLDMASK;
248198508Skib		kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
249198508Skib	}
250198508Skib
251155455Sphk	userret(td, framep);
25281493Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
25324691Speter}
254208453Skib
255208453Skib#ifdef HAVE_SYSCALL_ARGS_DEF
256208566Skibconst char *
257208453Skibsyscallname(struct proc *p, u_int code)
258208453Skib{
259208453Skib	static const char unknown[] = "unknown";
260209697Skib	struct sysentvec *sv;
261208453Skib
262209697Skib	sv = p->p_sysent;
263209697Skib	if (sv->sv_syscallnames == NULL || code >= sv->sv_size)
264208453Skib		return (unknown);
265209697Skib	return (sv->sv_syscallnames[code]);
266208453Skib}
267208453Skib
268208453Skibint
269208453Skibsyscallenter(struct thread *td, struct syscall_args *sa)
270208453Skib{
271208453Skib	struct proc *p;
272208453Skib	int error, traced;
273208453Skib
274208453Skib	PCPU_INC(cnt.v_syscall);
275208453Skib	p = td->td_proc;
276208453Skib
277208453Skib	td->td_pticks = 0;
278208453Skib	if (td->td_ucred != p->p_ucred)
279208453Skib		cred_update_thread(td);
280208453Skib	if (p->p_flag & P_TRACED) {
281208453Skib		traced = 1;
282208453Skib		PROC_LOCK(p);
283208453Skib		td->td_dbgflags &= ~TDB_USERWR;
284208453Skib		td->td_dbgflags |= TDB_SCE;
285208453Skib		PROC_UNLOCK(p);
286208453Skib	} else
287208453Skib		traced = 0;
288208453Skib	error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
289208453Skib#ifdef KTRACE
290208453Skib	if (KTRPOINT(td, KTR_SYSCALL))
291208453Skib		ktrsyscall(sa->code, sa->narg, sa->args);
292208453Skib#endif
293208453Skib
294208453Skib	CTR6(KTR_SYSC,
295208453Skib"syscall: td=%p pid %d %s (%#lx, %#lx, %#lx)",
296208453Skib	    td, td->td_proc->p_pid, syscallname(p, sa->code),
297208453Skib	    sa->args[0], sa->args[1], sa->args[2]);
298208453Skib
299208453Skib	if (error == 0) {
300208453Skib		STOPEVENT(p, S_SCE, sa->narg);
301208453Skib		PTRACESTOP_SC(p, td, S_PT_SCE);
302208453Skib		if (td->td_dbgflags & TDB_USERWR) {
303208453Skib			/*
304208453Skib			 * Reread syscall number and arguments if
305208453Skib			 * debugger modified registers or memory.
306208453Skib			 */
307208453Skib			error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
308208453Skib#ifdef KTRACE
309208453Skib			if (KTRPOINT(td, KTR_SYSCALL))
310208453Skib				ktrsyscall(sa->code, sa->narg, sa->args);
311208453Skib#endif
312208453Skib			if (error != 0)
313208453Skib				goto retval;
314208453Skib		}
315219133Srwatson
316219133Srwatson#ifdef CAPABILITIES
317219133Srwatson		/*
318219133Srwatson		 * In capability mode, we only allow access to system calls
319219133Srwatson		 * flagged with SYF_CAPENABLED.
320219133Srwatson		 */
321219133Srwatson		if (IN_CAPABILITY_MODE(td) &&
322219133Srwatson		    !(sa->callp->sy_flags & SYF_CAPENABLED)) {
323219133Srwatson			error = ECAPMODE;
324219133Srwatson			goto retval;
325219133Srwatson		}
326219133Srwatson#endif
327219133Srwatson
328209579Skib		error = syscall_thread_enter(td, sa->callp);
329209579Skib		if (error != 0)
330209579Skib			goto retval;
331208453Skib
332208453Skib#ifdef KDTRACE_HOOKS
333208453Skib		/*
334208453Skib		 * If the systrace module has registered it's probe
335208453Skib		 * callback and if there is a probe active for the
336208453Skib		 * syscall 'entry', process the probe.
337208453Skib		 */
338208453Skib		if (systrace_probe_func != NULL && sa->callp->sy_entry != 0)
339208453Skib			(*systrace_probe_func)(sa->callp->sy_entry, sa->code,
340211617Srpaulo			    sa->callp, sa->args, 0);
341208453Skib#endif
342208453Skib
343208453Skib		AUDIT_SYSCALL_ENTER(sa->code, td);
344208453Skib		error = (sa->callp->sy_call)(td, sa->args);
345208453Skib		AUDIT_SYSCALL_EXIT(error, td);
346208453Skib
347208453Skib		/* Save the latest error return value. */
348208453Skib		td->td_errno = error;
349208453Skib
350208453Skib#ifdef KDTRACE_HOOKS
351208453Skib		/*
352208453Skib		 * If the systrace module has registered it's probe
353208453Skib		 * callback and if there is a probe active for the
354208453Skib		 * syscall 'return', process the probe.
355208453Skib		 */
356208453Skib		if (systrace_probe_func != NULL && sa->callp->sy_return != 0)
357208453Skib			(*systrace_probe_func)(sa->callp->sy_return, sa->code,
358211617Srpaulo			    sa->callp, NULL, (error) ? -1 : td->td_retval[0]);
359208453Skib#endif
360209579Skib		syscall_thread_exit(td, sa->callp);
361208453Skib		CTR4(KTR_SYSC, "syscall: p=%p error=%d return %#lx %#lx",
362208453Skib		    p, error, td->td_retval[0], td->td_retval[1]);
363208453Skib	}
364208453Skib retval:
365208453Skib	if (traced) {
366208453Skib		PROC_LOCK(p);
367208453Skib		td->td_dbgflags &= ~TDB_SCE;
368208453Skib		PROC_UNLOCK(p);
369208453Skib	}
370208453Skib	(p->p_sysent->sv_set_syscall_retval)(td, error);
371208453Skib	return (error);
372208453Skib}
373208453Skib
374208453Skibvoid
375208453Skibsyscallret(struct thread *td, int error, struct syscall_args *sa __unused)
376208453Skib{
377208453Skib	struct proc *p;
378208453Skib	int traced;
379208453Skib
380208453Skib	p = td->td_proc;
381208453Skib
382208453Skib	/*
383208453Skib	 * Check for misbehavior.
384208453Skib	 */
385208453Skib	WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
386208453Skib	    syscallname(p, sa->code));
387208453Skib	KASSERT(td->td_critnest == 0,
388208453Skib	    ("System call %s returning in a critical section",
389208453Skib	    syscallname(p, sa->code)));
390208453Skib	KASSERT(td->td_locks == 0,
391208453Skib	    ("System call %s returning with %d locks held",
392208453Skib	     syscallname(p, sa->code), td->td_locks));
393208453Skib
394208453Skib	/*
395208453Skib	 * Handle reschedule and other end-of-syscall issues
396208453Skib	 */
397208453Skib	userret(td, td->td_frame);
398208453Skib
399208453Skib	CTR4(KTR_SYSC, "syscall %s exit thread %p pid %d proc %s",
400208453Skib	    syscallname(p, sa->code), td, td->td_proc->p_pid, td->td_name);
401208453Skib
402208453Skib#ifdef KTRACE
403208453Skib	if (KTRPOINT(td, KTR_SYSRET))
404208453Skib		ktrsysret(sa->code, error, td->td_retval[0]);
405208453Skib#endif
406208453Skib
407208453Skib	if (p->p_flag & P_TRACED) {
408208453Skib		traced = 1;
409208453Skib		PROC_LOCK(p);
410208453Skib		td->td_dbgflags |= TDB_SCX;
411208453Skib		PROC_UNLOCK(p);
412208453Skib	} else
413208453Skib		traced = 0;
414208453Skib	/*
415208453Skib	 * This works because errno is findable through the
416208453Skib	 * register set.  If we ever support an emulation where this
417208453Skib	 * is not the case, this code will need to be revisited.
418208453Skib	 */
419208453Skib	STOPEVENT(p, S_SCX, sa->code);
420208453Skib	PTRACESTOP_SC(p, td, S_PT_SCX);
421217819Skib	if (traced || (td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0) {
422208453Skib		PROC_LOCK(p);
423217819Skib		td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC | TDB_FORK);
424208453Skib		PROC_UNLOCK(p);
425208453Skib	}
426208453Skib}
427208453Skib#endif /* HAVE_SYSCALL_ARGS_DEF */
428