ia32_syscall.c revision 202882
1/*-
2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/sys/amd64/ia32/ia32_syscall.c 202882 2010-01-23 11:45:35Z kib $");
40
41/*
42 * 386 Trap and System call handling
43 */
44
45#include "opt_clock.h"
46#include "opt_cpu.h"
47#include "opt_isa.h"
48#include "opt_ktrace.h"
49
50#include <sys/param.h>
51#include <sys/bus.h>
52#include <sys/systm.h>
53#include <sys/proc.h>
54#include <sys/pioctl.h>
55#include <sys/kernel.h>
56#include <sys/ktr.h>
57#include <sys/lock.h>
58#include <sys/mutex.h>
59#include <sys/ptrace.h>
60#include <sys/resourcevar.h>
61#include <sys/signalvar.h>
62#include <sys/syscall.h>
63#include <sys/sysctl.h>
64#include <sys/sysent.h>
65#include <sys/uio.h>
66#include <sys/vmmeter.h>
67#ifdef KTRACE
68#include <sys/ktrace.h>
69#endif
70#include <security/audit/audit.h>
71
72#include <vm/vm.h>
73#include <vm/vm_param.h>
74#include <vm/pmap.h>
75#include <vm/vm_kern.h>
76#include <vm/vm_map.h>
77#include <vm/vm_page.h>
78#include <vm/vm_extern.h>
79
80#include <machine/cpu.h>
81#include <machine/intr_machdep.h>
82#include <machine/md_var.h>
83
84#define	IDTVEC(name)	__CONCAT(X,name)
85
86extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(rsvd);
87extern const char *freebsd32_syscallnames[];
88
89void ia32_syscall(struct trapframe *frame);	/* Called from asm code */
90
91struct ia32_syscall_args {
92	u_int code;
93	caddr_t params;
94	struct sysent *callp;
95	u_int64_t args64[8];
96	int narg;
97};
98
99static int
100fetch_ia32_syscall_args(struct thread *td, struct ia32_syscall_args *sa)
101{
102	struct proc *p;
103	struct trapframe *frame;
104	u_int32_t args[8];
105	int error, i;
106
107	p = td->td_proc;
108	frame = td->td_frame;
109
110	sa->params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
111	sa->code = frame->tf_rax;
112
113	if (p->p_sysent->sv_prepsyscall) {
114		/*
115		 * The prep code is MP aware.
116		 */
117		(*p->p_sysent->sv_prepsyscall)(frame, args, &sa->code,
118		    &sa->params);
119	} else {
120		/*
121		 * Need to check if this is a 32 bit or 64 bit syscall.
122		 * fuword is MP aware.
123		 */
124		if (sa->code == SYS_syscall) {
125			/*
126			 * Code is first argument, followed by actual args.
127			 */
128			sa->code = fuword32(sa->params);
129			sa->params += sizeof(int);
130		} else if (sa->code == SYS___syscall) {
131			/*
132			 * Like syscall, but code is a quad, so as to maintain
133			 * quad alignment for the rest of the arguments.
134			 * We use a 32-bit fetch in case params is not
135			 * aligned.
136			 */
137			sa->code = fuword32(sa->params);
138			sa->params += sizeof(quad_t);
139		}
140	}
141 	if (p->p_sysent->sv_mask)
142 		sa->code &= p->p_sysent->sv_mask;
143 	if (sa->code >= p->p_sysent->sv_size)
144 		sa->callp = &p->p_sysent->sv_table[0];
145  	else
146 		sa->callp = &p->p_sysent->sv_table[sa->code];
147	sa->narg = sa->callp->sy_narg;
148
149	if (sa->params != NULL && sa->narg != 0)
150		error = copyin(sa->params, (caddr_t)args,
151		    (u_int)(sa->narg * sizeof(int)));
152	else
153		error = 0;
154
155	for (i = 0; i < sa->narg; i++)
156		sa->args64[i] = args[i];
157
158#ifdef KTRACE
159	if (KTRPOINT(td, KTR_SYSCALL))
160		ktrsyscall(sa->code, sa->narg, sa->args64);
161#endif
162
163	return (error);
164}
165
166void
167ia32_syscall(struct trapframe *frame)
168{
169	struct thread *td;
170	struct proc *p;
171	struct ia32_syscall_args sa;
172	register_t orig_tf_rflags;
173	int error;
174	ksiginfo_t ksi;
175
176	PCPU_INC(cnt.v_syscall);
177	td = curthread;
178	p = td->td_proc;
179	td->td_syscalls++;
180
181	td->td_pticks = 0;
182	td->td_frame = frame;
183	if (td->td_ucred != p->p_ucred)
184		cred_update_thread(td);
185	orig_tf_rflags = frame->tf_rflags;
186	if (p->p_flag & P_TRACED) {
187		PROC_LOCK(p);
188		td->td_dbgflags &= ~TDB_USERWR;
189		PROC_UNLOCK(p);
190	}
191	error = fetch_ia32_syscall_args(td, &sa);
192
193	CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
194	    td->td_proc->p_pid, td->td_name, sa.code);
195
196	if (error == 0) {
197		td->td_retval[0] = 0;
198		td->td_retval[1] = frame->tf_rdx;
199
200		STOPEVENT(p, S_SCE, sa.narg);
201		PTRACESTOP_SC(p, td, S_PT_SCE);
202		if (td->td_dbgflags & TDB_USERWR) {
203			/*
204			 * Reread syscall number and arguments if
205			 * debugger modified registers or memory.
206			 */
207			error = fetch_ia32_syscall_args(td, &sa);
208			if (error != 0)
209				goto retval;
210			td->td_retval[1] = frame->tf_rdx;
211		}
212
213		AUDIT_SYSCALL_ENTER(sa.code, td);
214		error = (*sa.callp->sy_call)(td, sa.args64);
215		AUDIT_SYSCALL_EXIT(error, td);
216
217		/* Save the latest error return value. */
218		td->td_errno = error;
219	}
220 retval:
221	cpu_set_syscall_retval(td, error);
222
223	/*
224	 * Traced syscall.
225	 */
226	if (orig_tf_rflags & PSL_T) {
227		frame->tf_rflags &= ~PSL_T;
228		ksiginfo_init_trap(&ksi);
229		ksi.ksi_signo = SIGTRAP;
230		ksi.ksi_code = TRAP_TRACE;
231		ksi.ksi_addr = (void *)frame->tf_rip;
232		trapsignal(td, &ksi);
233	}
234
235	/*
236	 * Check for misbehavior.
237	 */
238	WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
239	    (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
240	     freebsd32_syscallnames[sa.code] : "???");
241	KASSERT(td->td_critnest == 0,
242	    ("System call %s returning in a critical section",
243	    (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
244	     freebsd32_syscallnames[sa.code] : "???"));
245	KASSERT(td->td_locks == 0,
246	    ("System call %s returning with %d locks held",
247	    (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
248	     freebsd32_syscallnames[sa.code] : "???", td->td_locks));
249
250	/*
251	 * Handle reschedule and other end-of-syscall issues
252	 */
253	userret(td, frame);
254
255	CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
256	    td->td_proc->p_pid, td->td_proc->p_comm, sa.code);
257#ifdef KTRACE
258	if (KTRPOINT(td, KTR_SYSRET))
259		ktrsysret(sa.code, error, td->td_retval[0]);
260#endif
261
262	/*
263	 * This works because errno is findable through the
264	 * register set.  If we ever support an emulation where this
265	 * is not the case, this code will need to be revisited.
266	 */
267	STOPEVENT(p, S_SCX, sa.code);
268
269	PTRACESTOP_SC(p, td, S_PT_SCX);
270}
271
272
273static void
274ia32_syscall_enable(void *dummy)
275{
276
277 	setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
278}
279
280static void
281ia32_syscall_disable(void *dummy)
282{
283
284 	setidt(IDT_SYSCALL, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
285}
286
287SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
288SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);
289