ia32_syscall.c revision 211515
132822Syokota/*-
232822Syokota * Copyright (C) 1994, David Greenman
332822Syokota * Copyright (c) 1990, 1993
432822Syokota *	The Regents of the University of California.  All rights reserved.
532822Syokota *
632822Syokota * This code is derived from software contributed to Berkeley by
732822Syokota * the University of Utah, and William Jolitz.
832822Syokota *
932822Syokota * Redistribution and use in source and binary forms, with or without
1037204Ssteve * modification, are permitted provided that the following conditions
1143334Syokota * are met:
1243334Syokota * 1. Redistributions of source code must retain the above copyright
1343334Syokota *    notice, this list of conditions and the following disclaimer.
1443334Syokota * 2. Redistributions in binary form must reproduce the above copyright
1543334Syokota *    notice, this list of conditions and the following disclaimer in the
1643334Syokota *    documentation and/or other materials provided with the distribution.
1743334Syokota * 3. All advertising materials mentioning features or use of this software
1843334Syokota *    must display the following acknowledgement:
1932822Syokota *	This product includes software developed by the University of
2032822Syokota *	California, Berkeley and its contributors.
2132822Syokota * 4. Neither the name of the University nor the names of its contributors
2232822Syokota *    may be used to endorse or promote products derived from this software
2332822Syokota *    without specific prior written permission.
2432822Syokota *
2532822Syokota * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2643334Syokota * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2732822Syokota * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2832822Syokota * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2932822Syokota * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3032822Syokota * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3137204Ssteve * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3237204Ssteve * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3332822Syokota * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3432822Syokota * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3532822Syokota * SUCH DAMAGE.
3632822Syokota */
3732822Syokota
3832822Syokota#include <sys/cdefs.h>
3932822Syokota__FBSDID("$FreeBSD: head/sys/amd64/ia32/ia32_syscall.c 211515 2010-08-19 16:41:27Z jhb $");
4032822Syokota
4132822Syokota/*
4232822Syokota * 386 Trap and System call handling
4332822Syokota */
4443334Syokota
4543334Syokota#include "opt_clock.h"
4632822Syokota#include "opt_cpu.h"
4732822Syokota#include "opt_isa.h"
4837204Ssteve
4943334Syokota#include <sys/param.h>
5032822Syokota#include <sys/bus.h>
5132822Syokota#include <sys/systm.h>
5232822Syokota#include <sys/proc.h>
5332822Syokota#include <sys/pioctl.h>
5432822Syokota#include <sys/kernel.h>
5532822Syokota#include <sys/ktr.h>
5632822Syokota#include <sys/lock.h>
5732822Syokota#include <sys/mutex.h>
5838140Syokota#include <sys/proc.h>
5932822Syokota#include <sys/ptrace.h>
6043334Syokota#include <sys/resourcevar.h>
6132822Syokota#include <sys/signalvar.h>
6232822Syokota#include <sys/syscall.h>
6332822Syokota#include <sys/sysctl.h>
6432822Syokota#include <sys/sysent.h>
6532822Syokota#include <sys/uio.h>
6632822Syokota#include <sys/vmmeter.h>
6732822Syokota#include <security/audit/audit.h>
6832822Syokota
6932822Syokota#include <vm/vm.h>
7032822Syokota#include <vm/vm_param.h>
7132822Syokota#include <vm/pmap.h>
7232822Syokota#include <vm/vm_kern.h>
7332822Syokota#include <vm/vm_map.h>
7443334Syokota#include <vm/vm_page.h>
7543334Syokota#include <vm/vm_extern.h>
7632822Syokota
7732822Syokota#include <machine/cpu.h>
7832822Syokota#include <machine/intr_machdep.h>
7943334Syokota#include <machine/md_var.h>
8032822Syokota
8132822Syokota#include <compat/freebsd32/freebsd32_util.h>
8243334Syokota
8332822Syokota#define	IDTVEC(name)	__CONCAT(X,name)
8432822Syokota
8532822Syokotaextern inthand_t IDTVEC(int0x80_syscall), IDTVEC(rsvd);
8632822Syokota
8732822Syokotavoid ia32_syscall(struct trapframe *frame);	/* Called from asm code */
8843334Syokota
8943334Syokotavoid
9032822Syokotaia32_set_syscall_retval(struct thread *td, int error)
9143334Syokota{
9232822Syokota
9332822Syokota	cpu_set_syscall_retval(td, error);
9432822Syokota}
9532822Syokota
9632822Syokotaint
9743334Syokotaia32_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
9832822Syokota{
9932822Syokota	struct proc *p;
10032822Syokota	struct trapframe *frame;
10132822Syokota	caddr_t params;
10232822Syokota	u_int32_t args[8];
10332822Syokota	int error, i;
10432822Syokota
10532822Syokota	p = td->td_proc;
10632822Syokota	frame = td->td_frame;
10732822Syokota
10843334Syokota	params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
10943334Syokota	sa->code = frame->tf_rax;
11043334Syokota
11143334Syokota	/*
11243334Syokota	 * Need to check if this is a 32 bit or 64 bit syscall.
11343334Syokota	 */
11432822Syokota	if (sa->code == SYS_syscall) {
11532822Syokota		/*
11632822Syokota		 * Code is first argument, followed by actual args.
11732822Syokota		 */
11832822Syokota		sa->code = fuword32(params);
11932822Syokota		params += sizeof(int);
12032822Syokota	} else if (sa->code == SYS___syscall) {
12132822Syokota		/*
12232822Syokota		 * Like syscall, but code is a quad, so as to maintain
12332822Syokota		 * quad alignment for the rest of the arguments.
12432822Syokota		 * We use a 32-bit fetch in case params is not
12532822Syokota		 * aligned.
12632822Syokota		 */
12732822Syokota		sa->code = fuword32(params);
12832822Syokota		params += sizeof(quad_t);
12932822Syokota	}
13032822Syokota 	if (p->p_sysent->sv_mask)
13132822Syokota 		sa->code &= p->p_sysent->sv_mask;
13232822Syokota 	if (sa->code >= p->p_sysent->sv_size)
13332822Syokota 		sa->callp = &p->p_sysent->sv_table[0];
13432822Syokota  	else
13532822Syokota 		sa->callp = &p->p_sysent->sv_table[sa->code];
13632822Syokota	sa->narg = sa->callp->sy_narg;
13732822Syokota
13832822Syokota	if (params != NULL && sa->narg != 0)
139		error = copyin(params, (caddr_t)args,
140		    (u_int)(sa->narg * sizeof(int)));
141	else
142		error = 0;
143
144	for (i = 0; i < sa->narg; i++)
145		sa->args[i] = args[i];
146
147	if (error == 0) {
148		td->td_retval[0] = 0;
149		td->td_retval[1] = frame->tf_rdx;
150	}
151
152	return (error);
153}
154
155void
156ia32_syscall(struct trapframe *frame)
157{
158	struct thread *td;
159	struct syscall_args sa;
160	register_t orig_tf_rflags;
161	int error;
162	ksiginfo_t ksi;
163
164	orig_tf_rflags = frame->tf_rflags;
165	td = curthread;
166	td->td_frame = frame;
167
168	error = syscallenter(td, &sa);
169
170	/*
171	 * Traced syscall.
172	 */
173	if (orig_tf_rflags & PSL_T) {
174		frame->tf_rflags &= ~PSL_T;
175		ksiginfo_init_trap(&ksi);
176		ksi.ksi_signo = SIGTRAP;
177		ksi.ksi_code = TRAP_TRACE;
178		ksi.ksi_addr = (void *)frame->tf_rip;
179		trapsignal(td, &ksi);
180	}
181
182	syscallret(td, error, &sa);
183}
184
185static void
186ia32_syscall_enable(void *dummy)
187{
188
189 	setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
190}
191
192static void
193ia32_syscall_disable(void *dummy)
194{
195
196 	setidt(IDT_SYSCALL, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
197}
198
199SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
200SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);
201