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