xref: /netbsd-src/sys/arch/amd64/amd64/linux_syscall.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: linux_syscall.c,v 1.30 2009/11/21 03:11:01 rmind 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.30 2009/11/21 03:11:01 rmind 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/signal.h>
43 #include <sys/syscall.h>
44 #include <sys/syscallvar.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 #include <machine/cpu.h>
49 #include <machine/psl.h>
50 #include <machine/userret.h>
51 
52 #include <compat/linux/linux_syscall.h>
53 #include <compat/linux/common/linux_types.h>
54 #include <compat/linux/common/linux_errno.h>
55 #include <compat/linux/common/linux_signal.h>
56 #include <compat/linux/common/linux_siginfo.h>
57 #include <compat/linux/arch/amd64/linux_siginfo.h>
58 #include <compat/linux/arch/amd64/linux_machdep.h>
59 
60 void linux_syscall_intern(struct proc *);
61 static void linux_syscall(struct trapframe *);
62 
63 void
64 linux_syscall_intern(struct proc *p)
65 {
66 
67 	p->p_md.md_syscall = linux_syscall;
68 }
69 
70 /*
71  * syscall(frame):
72  *	System call request from POSIX system call gate interface to kernel.
73  * Like trap(), argument is call by reference.
74  */
75 static void
76 linux_syscall(struct trapframe *frame)
77 {
78 	const struct sysent *callp;
79 	struct proc *p;
80 	struct lwp *l;
81 	int error;
82 	register_t code, rval[2];
83 	#define args (&frame->tf_rdi)
84 
85 	l = curlwp;
86 	p = l->l_proc;
87 
88 	code = frame->tf_rax;
89 
90 	LWP_CACHE_CREDS(l, p);
91 
92 	callp = p->p_emul->e_sysent;
93 
94 	code &= (LINUX_SYS_NSYSENT - 1);
95 	callp += code;
96 
97 	/*
98 	 * Linux system calls have a maximum of 6 arguments, they are
99 	 * already adjacent in the syscall trapframe.
100 	 */
101 
102 	if (__predict_false(p->p_trace_enabled)
103 	    && (error = trace_enter(code, args, callp->sy_narg)) != 0)
104 		goto out;
105 
106 	rval[0] = 0;
107 	rval[1] = 0;
108 	error = sy_call(callp, l, args, rval);
109 out:
110 	switch (error) {
111 	case 0:
112 		frame->tf_rax = rval[0];
113 		break;
114 	case ERESTART:
115 		/*
116 		 * The offset to adjust the PC by depends on whether we entered
117 		 * the kernel through the trap or call gate.  We pushed the
118 		 * size of the instruction into tf_err on entry.
119 		 */
120 		frame->tf_rip -= frame->tf_err;
121 		break;
122 	case EJUSTRETURN:
123 		/* nothing to do */
124 		break;
125 	default:
126 		error = native_to_linux_errno[error];
127 		frame->tf_rax = error;
128 		break;
129 	}
130 
131 	if (__predict_false(p->p_trace_enabled))
132 		trace_exit(code, rval, error);
133 
134 	userret(l);
135 }
136