xref: /netbsd-src/sys/arch/amd64/amd64/linux_syscall.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: linux_syscall.c,v 1.29 2008/10/21 12:16:59 ad Exp $ */
2 
3 /*-
4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_syscall.c,v 1.29 2008/10/21 12:16:59 ad Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_linux.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/user.h>
43 #include <sys/signal.h>
44 #include <sys/syscall.h>
45 #include <sys/syscallvar.h>
46 
47 #include <uvm/uvm_extern.h>
48 
49 #include <machine/cpu.h>
50 #include <machine/psl.h>
51 #include <machine/userret.h>
52 
53 #include <compat/linux/linux_syscall.h>
54 #include <compat/linux/common/linux_types.h>
55 #include <compat/linux/common/linux_errno.h>
56 #include <compat/linux/common/linux_signal.h>
57 #include <compat/linux/common/linux_siginfo.h>
58 #include <compat/linux/arch/amd64/linux_siginfo.h>
59 #include <compat/linux/arch/amd64/linux_machdep.h>
60 
61 void linux_syscall_intern(struct proc *);
62 static void linux_syscall(struct trapframe *);
63 
64 void
65 linux_syscall_intern(struct proc *p)
66 {
67 
68 	p->p_md.md_syscall = linux_syscall;
69 }
70 
71 /*
72  * syscall(frame):
73  *	System call request from POSIX system call gate interface to kernel.
74  * Like trap(), argument is call by reference.
75  */
76 static void
77 linux_syscall(struct trapframe *frame)
78 {
79 	const struct sysent *callp;
80 	struct proc *p;
81 	struct lwp *l;
82 	int error;
83 	register_t code, rval[2];
84 	#define args (&frame->tf_rdi)
85 
86 	l = curlwp;
87 	p = l->l_proc;
88 
89 	code = frame->tf_rax;
90 
91 	LWP_CACHE_CREDS(l, p);
92 
93 	callp = p->p_emul->e_sysent;
94 
95 	code &= (LINUX_SYS_NSYSENT - 1);
96 	callp += code;
97 
98 	/*
99 	 * Linux system calls have a maximum of 6 arguments, they are
100 	 * already adjacent in the syscall trapframe.
101 	 */
102 
103 	if (__predict_false(p->p_trace_enabled)
104 	    && (error = trace_enter(code, args, callp->sy_narg)) != 0)
105 		goto out;
106 
107 	rval[0] = 0;
108 	rval[1] = 0;
109 	error = sy_call(callp, l, args, rval);
110 out:
111 	switch (error) {
112 	case 0:
113 		frame->tf_rax = rval[0];
114 		break;
115 	case ERESTART:
116 		/*
117 		 * The offset to adjust the PC by depends on whether we entered
118 		 * the kernel through the trap or call gate.  We pushed the
119 		 * size of the instruction into tf_err on entry.
120 		 */
121 		frame->tf_rip -= frame->tf_err;
122 		break;
123 	case EJUSTRETURN:
124 		/* nothing to do */
125 		break;
126 	default:
127 		error = native_to_linux_errno[error];
128 		frame->tf_rax = error;
129 		break;
130 	}
131 
132 	if (__predict_false(p->p_trace_enabled))
133 		trace_exit(code, rval, error);
134 
135 	userret(l);
136 }
137