xref: /netbsd-src/sys/arch/amd64/amd64/linux_syscall.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: linux_syscall.c,v 1.8 2006/03/07 07:21:50 thorpej 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: linux_syscall.c,v 1.8 2006/03/07 07:21:50 thorpej Exp $");
41 
42 #include "opt_compat_linux.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/user.h>
48 #include <sys/signal.h>
49 #include <sys/sa.h>
50 #include <sys/savar.h>
51 #include <sys/syscall.h>
52 
53 #include <uvm/uvm_extern.h>
54 
55 #include <machine/cpu.h>
56 #include <machine/psl.h>
57 #include <machine/userret.h>
58 
59 #include <compat/linux/linux_syscall.h>
60 #include <compat/linux/common/linux_types.h>
61 #include <compat/linux/common/linux_errno.h>
62 #include <compat/linux/common/linux_signal.h>
63 #include <compat/linux/common/linux_siginfo.h>
64 #include <compat/linux/arch/amd64/linux_siginfo.h>
65 #include <compat/linux/arch/amd64/linux_syscall.h>
66 #include <compat/linux/arch/amd64/linux_machdep.h>
67 #include <compat/linux/common/linux_errno.h>
68 
69 void linux_syscall_intern(struct proc *);
70 static void linux_syscall_plain(struct trapframe *);
71 static void linux_syscall_fancy(struct trapframe *);
72 
73 void
74 linux_syscall_intern(struct proc *p)
75 {
76 
77 	if (trace_is_enabled(p))
78 		p->p_md.md_syscall = linux_syscall_fancy;
79 	else
80 		p->p_md.md_syscall = linux_syscall_plain;
81 }
82 
83 /*
84  * syscall(frame):
85  *	System call request from POSIX system call gate interface to kernel.
86  * Like trap(), argument is call by reference.
87  */
88 static void
89 linux_syscall_plain(struct trapframe *frame)
90 {
91 	caddr_t params;
92 	const struct sysent *callp;
93 	struct proc *p;
94 	struct lwp *l;
95 	int error;
96 	size_t argsize, argoff;
97 	register_t code, args[9], rval[2], *argp;
98 
99 	uvmexp.syscalls++;
100 	l = curlwp;
101 	p = l->l_proc;
102 
103 	code = frame->tf_rax;
104 	callp = p->p_emul->e_sysent;
105 	argoff = 0;
106 	argp = &args[0];
107 
108 	code &= (LINUX_SYS_NSYSENT - 1);
109 	callp += code;
110 
111 	argsize = (callp->sy_argsize >> 3) + argoff;
112 	if (argsize) {
113 		switch (MIN(argsize, 6)) {
114 		case 6:
115 			args[5] = frame->tf_r9;
116 		case 5:
117 			args[4] = frame->tf_r8;
118 		case 4:
119 			args[3] = frame->tf_r10;
120 		case 3:
121 			args[2] = frame->tf_rdx;
122 		case 2:
123 			args[1] = frame->tf_rsi;
124 		case 1:
125 			args[0] = frame->tf_rdi;
126 			break;
127 		default:
128 			panic("impossible syscall argsize");
129 		}
130 		if (argsize > 6) {
131 			argsize -= 6;
132 			params = (caddr_t)frame->tf_rsp + sizeof(register_t);
133 			error = copyin(params, (caddr_t)&args[6],
134 					argsize << 3);
135 			if (error != 0)
136 				goto bad;
137 		}
138 	}
139 
140 	rval[0] = 0;
141 	rval[1] = 0;
142 	KERNEL_PROC_LOCK(l);
143 	error = (*callp->sy_call)(l, argp, rval);
144 	KERNEL_PROC_UNLOCK(l);
145 
146 	switch (error) {
147 	case 0:
148 		frame->tf_rax = rval[0];
149 		frame->tf_rflags &= ~PSL_C;	/* carry bit */
150 		break;
151 	case ERESTART:
152 		/*
153 		 * The offset to adjust the PC by depends on whether we entered
154 		 * the kernel through the trap or call gate.  We pushed the
155 		 * size of the instruction into tf_err on entry.
156 		 */
157 		frame->tf_rip -= frame->tf_err;
158 		break;
159 	case EJUSTRETURN:
160 		/* nothing to do */
161 		break;
162 	default:
163 	bad:
164 		frame->tf_rax = native_to_linux_errno[error];
165 		frame->tf_rflags |= PSL_C;	/* carry bit */
166 		break;
167 	}
168 
169 	userret(l);
170 }
171 
172 static void
173 linux_syscall_fancy(struct trapframe *frame)
174 {
175 	caddr_t params;
176 	const struct sysent *callp;
177 	struct proc *p;
178 	struct lwp *l;
179 	int error;
180 	size_t argsize, argoff;
181 	register_t code, args[9], rval[2], *argp;
182 
183 	uvmexp.syscalls++;
184 	l = curlwp;
185 	p = l->l_proc;
186 
187 	code = frame->tf_rax;
188 	callp = p->p_emul->e_sysent;
189 	argp = &args[0];
190 	argoff = 0;
191 
192 	code &= (SYS_NSYSENT - 1);
193 	callp += code;
194 
195 	argsize = (callp->sy_argsize >> 3) + argoff;
196 	if (argsize) {
197 		switch (MIN(argsize, 6)) {
198 		case 6:
199 			args[5] = frame->tf_r9;
200 		case 5:
201 			args[4] = frame->tf_r8;
202 		case 4:
203 			args[3] = frame->tf_r10;
204 		case 3:
205 			args[2] = frame->tf_rdx;
206 		case 2:
207 			args[1] = frame->tf_rsi;
208 		case 1:
209 			args[0] = frame->tf_rdi;
210 			break;
211 		default:
212 			panic("impossible syscall argsize");
213 		}
214 		if (argsize > 6) {
215 			argsize -= 6;
216 			params = (caddr_t)frame->tf_rsp + sizeof(register_t);
217 			error = copyin(params, (caddr_t)&args[6],
218 					argsize << 3);
219 			if (error != 0)
220 				goto bad;
221 		}
222 	}
223 
224 	KERNEL_PROC_LOCK(l);
225 	if ((error = trace_enter(l, code, code, NULL, argp)) != 0)
226 		goto out;
227 
228 	rval[0] = 0;
229 	rval[1] = 0;
230 	error = (*callp->sy_call)(l, argp, rval);
231 out:
232 	KERNEL_PROC_UNLOCK(l);
233 	switch (error) {
234 	case 0:
235 		frame->tf_rax = rval[0];
236 		frame->tf_rflags &= ~PSL_C;	/* carry bit */
237 		break;
238 	case ERESTART:
239 		/*
240 		 * The offset to adjust the PC by depends on whether we entered
241 		 * the kernel through the trap or call gate.  We pushed the
242 		 * size of the instruction into tf_err on entry.
243 		 */
244 		frame->tf_rip -= frame->tf_err;
245 		break;
246 	case EJUSTRETURN:
247 		/* nothing to do */
248 		break;
249 	default:
250 	bad:
251 		frame->tf_rax = native_to_linux_errno[error];
252 		frame->tf_rflags |= PSL_C;	/* carry bit */
253 		break;
254 	}
255 
256 	trace_exit(l, code, argp, rval, error);
257 
258 	userret(l);
259 }
260