subr_syscall.c revision 218688
1/*-
2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 * Copyright (c) 2007 The FreeBSD Foundation
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the University of Utah, and William Jolitz.
9 *
10 * Portions of this software were developed by A. Joseph Koshy under
11 * sponsorship from the FreeBSD Foundation and Google, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgement:
23 *	This product includes software developed by the University of
24 *	California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
42 */
43
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 218688 2011-02-14 20:49:37Z bz $");
46
47#include "opt_ktrace.h"
48#include "opt_kdtrace.h"
49#include "opt_sched.h"
50
51#include <sys/param.h>
52#include <sys/bus.h>
53#include <sys/kernel.h>
54#include <sys/lock.h>
55#include <sys/mutex.h>
56#include <sys/pmckern.h>
57#include <sys/proc.h>
58#include <sys/ktr.h>
59#include <sys/pioctl.h>
60#include <sys/ptrace.h>
61#include <sys/resourcevar.h>
62#include <sys/sched.h>
63#include <sys/signalvar.h>
64#include <sys/syscall.h>
65#include <sys/syscallsubr.h>
66#include <sys/sysent.h>
67#include <sys/systm.h>
68#include <sys/vmmeter.h>
69#ifdef KTRACE
70#include <sys/uio.h>
71#include <sys/ktrace.h>
72#endif
73#include <security/audit/audit.h>
74
75#include <machine/cpu.h>
76
77#ifdef VIMAGE
78#include <net/vnet.h>
79#endif
80
81#ifdef XEN
82#include <vm/vm.h>
83#include <vm/vm_param.h>
84#include <vm/pmap.h>
85#endif
86
87#include <security/mac/mac_framework.h>
88
89/*
90 * Define the code needed before returning to user mode, for trap and
91 * syscall.
92 */
93void
94userret(struct thread *td, struct trapframe *frame)
95{
96	struct proc *p = td->td_proc;
97
98	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
99            td->td_name);
100#if 0
101#ifdef DIAGNOSTIC
102	/* Check that we called signotify() enough. */
103	PROC_LOCK(p);
104	thread_lock(td);
105	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
106	    (td->td_flags & TDF_ASTPENDING) == 0))
107		printf("failed to set signal flags properly for ast()\n");
108	thread_unlock(td);
109	PROC_UNLOCK(p);
110#endif
111#endif
112#ifdef KTRACE
113	KTRUSERRET(td);
114#endif
115	/*
116	 * If this thread tickled GEOM, we need to wait for the giggling to
117	 * stop before we return to userland
118	 */
119	if (td->td_pflags & TDP_GEOM)
120		g_waitidle();
121
122	/*
123	 * Charge system time if profiling.
124	 */
125	if (p->p_flag & P_PROFIL)
126		addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
127	/*
128	 * Let the scheduler adjust our priority etc.
129	 */
130	sched_userret(td);
131	KASSERT(td->td_locks == 0,
132	    ("userret: Returning with %d locks held.", td->td_locks));
133#ifdef VIMAGE
134	/* Unfortunately td_vnet_lpush needs VNET_DEBUG. */
135	VNET_ASSERT(curvnet == NULL,
136	    ("%s: Returning on td %p (pid %d, %s) with vnet %p set in %s",
137	    __func__, td, p->p_pid, td->td_name, curvnet,
138	    (td->td_vnet_lpush != NULL) ? td->td_vnet_lpush : "N/A"));
139#endif
140#ifdef XEN
141	PT_UPDATES_FLUSH();
142#endif
143}
144
145/*
146 * Process an asynchronous software trap.
147 * This is relatively easy.
148 * This function will return with preemption disabled.
149 */
150void
151ast(struct trapframe *framep)
152{
153	struct thread *td;
154	struct proc *p;
155	int flags;
156	int sig;
157
158	td = curthread;
159	p = td->td_proc;
160
161	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
162            p->p_comm);
163	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
164	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
165	mtx_assert(&Giant, MA_NOTOWNED);
166	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
167	td->td_frame = framep;
168	td->td_pticks = 0;
169
170	/*
171	 * This updates the td_flag's for the checks below in one
172	 * "atomic" operation with turning off the astpending flag.
173	 * If another AST is triggered while we are handling the
174	 * AST's saved in flags, the astpending flag will be set and
175	 * ast() will be called again.
176	 */
177	thread_lock(td);
178	flags = td->td_flags;
179	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK |
180	    TDF_NEEDRESCHED | TDF_ALRMPEND | TDF_PROFPEND | TDF_MACPEND);
181	thread_unlock(td);
182	PCPU_INC(cnt.v_trap);
183
184	if (td->td_ucred != p->p_ucred)
185		cred_update_thread(td);
186	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
187		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
188		td->td_profil_ticks = 0;
189		td->td_pflags &= ~TDP_OWEUPC;
190	}
191	if (flags & TDF_ALRMPEND) {
192		PROC_LOCK(p);
193		psignal(p, SIGVTALRM);
194		PROC_UNLOCK(p);
195	}
196	if (flags & TDF_PROFPEND) {
197		PROC_LOCK(p);
198		psignal(p, SIGPROF);
199		PROC_UNLOCK(p);
200	}
201#ifdef MAC
202	if (flags & TDF_MACPEND)
203		mac_thread_userret(td);
204#endif
205	if (flags & TDF_NEEDRESCHED) {
206#ifdef KTRACE
207		if (KTRPOINT(td, KTR_CSW))
208			ktrcsw(1, 1);
209#endif
210		thread_lock(td);
211		sched_prio(td, td->td_user_pri);
212		mi_switch(SW_INVOL | SWT_NEEDRESCHED, NULL);
213		thread_unlock(td);
214#ifdef KTRACE
215		if (KTRPOINT(td, KTR_CSW))
216			ktrcsw(0, 1);
217#endif
218	}
219
220	/*
221	 * Check for signals. Unlocked reads of p_pendingcnt or
222	 * p_siglist might cause process-directed signal to be handled
223	 * later.
224	 */
225	if (flags & TDF_NEEDSIGCHK || p->p_pendingcnt > 0 ||
226	    !SIGISEMPTY(p->p_siglist)) {
227		PROC_LOCK(p);
228		mtx_lock(&p->p_sigacts->ps_mtx);
229		while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
230			postsig(sig);
231		mtx_unlock(&p->p_sigacts->ps_mtx);
232		PROC_UNLOCK(p);
233	}
234	/*
235	 * We need to check to see if we have to exit or wait due to a
236	 * single threading requirement or some other STOP condition.
237	 */
238	if (flags & TDF_NEEDSUSPCHK) {
239		PROC_LOCK(p);
240		thread_suspend_check(0);
241		PROC_UNLOCK(p);
242	}
243
244	if (td->td_pflags & TDP_OLDMASK) {
245		td->td_pflags &= ~TDP_OLDMASK;
246		kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
247	}
248
249	userret(td, framep);
250	mtx_assert(&Giant, MA_NOTOWNED);
251}
252
253#ifdef HAVE_SYSCALL_ARGS_DEF
254const char *
255syscallname(struct proc *p, u_int code)
256{
257	static const char unknown[] = "unknown";
258	struct sysentvec *sv;
259
260	sv = p->p_sysent;
261	if (sv->sv_syscallnames == NULL || code >= sv->sv_size)
262		return (unknown);
263	return (sv->sv_syscallnames[code]);
264}
265
266int
267syscallenter(struct thread *td, struct syscall_args *sa)
268{
269	struct proc *p;
270	int error, traced;
271
272	PCPU_INC(cnt.v_syscall);
273	p = td->td_proc;
274
275	td->td_pticks = 0;
276	if (td->td_ucred != p->p_ucred)
277		cred_update_thread(td);
278	if (p->p_flag & P_TRACED) {
279		traced = 1;
280		PROC_LOCK(p);
281		td->td_dbgflags &= ~TDB_USERWR;
282		td->td_dbgflags |= TDB_SCE;
283		PROC_UNLOCK(p);
284	} else
285		traced = 0;
286	error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
287#ifdef KTRACE
288	if (KTRPOINT(td, KTR_SYSCALL))
289		ktrsyscall(sa->code, sa->narg, sa->args);
290#endif
291
292	CTR6(KTR_SYSC,
293"syscall: td=%p pid %d %s (%#lx, %#lx, %#lx)",
294	    td, td->td_proc->p_pid, syscallname(p, sa->code),
295	    sa->args[0], sa->args[1], sa->args[2]);
296
297	if (error == 0) {
298		STOPEVENT(p, S_SCE, sa->narg);
299		PTRACESTOP_SC(p, td, S_PT_SCE);
300		if (td->td_dbgflags & TDB_USERWR) {
301			/*
302			 * Reread syscall number and arguments if
303			 * debugger modified registers or memory.
304			 */
305			error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
306#ifdef KTRACE
307			if (KTRPOINT(td, KTR_SYSCALL))
308				ktrsyscall(sa->code, sa->narg, sa->args);
309#endif
310			if (error != 0)
311				goto retval;
312		}
313		error = syscall_thread_enter(td, sa->callp);
314		if (error != 0)
315			goto retval;
316
317#ifdef KDTRACE_HOOKS
318		/*
319		 * If the systrace module has registered it's probe
320		 * callback and if there is a probe active for the
321		 * syscall 'entry', process the probe.
322		 */
323		if (systrace_probe_func != NULL && sa->callp->sy_entry != 0)
324			(*systrace_probe_func)(sa->callp->sy_entry, sa->code,
325			    sa->callp, sa->args, 0);
326#endif
327
328		AUDIT_SYSCALL_ENTER(sa->code, td);
329		error = (sa->callp->sy_call)(td, sa->args);
330		AUDIT_SYSCALL_EXIT(error, td);
331
332		/* Save the latest error return value. */
333		td->td_errno = error;
334
335#ifdef KDTRACE_HOOKS
336		/*
337		 * If the systrace module has registered it's probe
338		 * callback and if there is a probe active for the
339		 * syscall 'return', process the probe.
340		 */
341		if (systrace_probe_func != NULL && sa->callp->sy_return != 0)
342			(*systrace_probe_func)(sa->callp->sy_return, sa->code,
343			    sa->callp, NULL, (error) ? -1 : td->td_retval[0]);
344#endif
345		syscall_thread_exit(td, sa->callp);
346		CTR4(KTR_SYSC, "syscall: p=%p error=%d return %#lx %#lx",
347		    p, error, td->td_retval[0], td->td_retval[1]);
348	}
349 retval:
350	if (traced) {
351		PROC_LOCK(p);
352		td->td_dbgflags &= ~TDB_SCE;
353		PROC_UNLOCK(p);
354	}
355	(p->p_sysent->sv_set_syscall_retval)(td, error);
356	return (error);
357}
358
359void
360syscallret(struct thread *td, int error, struct syscall_args *sa __unused)
361{
362	struct proc *p;
363	int traced;
364
365	p = td->td_proc;
366
367	/*
368	 * Check for misbehavior.
369	 */
370	WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
371	    syscallname(p, sa->code));
372	KASSERT(td->td_critnest == 0,
373	    ("System call %s returning in a critical section",
374	    syscallname(p, sa->code)));
375	KASSERT(td->td_locks == 0,
376	    ("System call %s returning with %d locks held",
377	     syscallname(p, sa->code), td->td_locks));
378
379	/*
380	 * Handle reschedule and other end-of-syscall issues
381	 */
382	userret(td, td->td_frame);
383
384	CTR4(KTR_SYSC, "syscall %s exit thread %p pid %d proc %s",
385	    syscallname(p, sa->code), td, td->td_proc->p_pid, td->td_name);
386
387#ifdef KTRACE
388	if (KTRPOINT(td, KTR_SYSRET))
389		ktrsysret(sa->code, error, td->td_retval[0]);
390#endif
391
392	if (p->p_flag & P_TRACED) {
393		traced = 1;
394		PROC_LOCK(p);
395		td->td_dbgflags |= TDB_SCX;
396		PROC_UNLOCK(p);
397	} else
398		traced = 0;
399	/*
400	 * This works because errno is findable through the
401	 * register set.  If we ever support an emulation where this
402	 * is not the case, this code will need to be revisited.
403	 */
404	STOPEVENT(p, S_SCX, sa->code);
405	PTRACESTOP_SC(p, td, S_PT_SCX);
406	if (traced || (td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0) {
407		PROC_LOCK(p);
408		td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC | TDB_FORK);
409		PROC_UNLOCK(p);
410	}
411}
412#endif /* HAVE_SYSCALL_ARGS_DEF */
413