xref: /netbsd-src/sys/arch/i386/i386/linux_syscall.c (revision 68fa58437753598de948829082f591c269b48777)
1 /*	$NetBSD: linux_syscall.c,v 1.55 2023/10/05 19:41:04 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.55 2023/10/05 19:41:04 ad Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/signal.h>
39 #include <sys/syscall.h>
40 #include <sys/syscallvar.h>
41 
42 #include <uvm/uvm_extern.h>
43 
44 #include <machine/cpu.h>
45 #include <machine/psl.h>
46 #include <machine/userret.h>
47 
48 #include <compat/linux/common/linux_types.h>
49 #include <compat/linux/common/linux_errno.h>
50 #include <compat/linux/linux_syscall.h>
51 #include <compat/linux/common/linux_signal.h>
52 #include <compat/linux/arch/i386/linux_machdep.h>
53 
54 static void linux_syscall(struct trapframe *);
55 extern struct sysent linux_sysent[];
56 extern const uint32_t linux_sysent_nomodbits[];
57 
58 void
linux_syscall_intern(struct proc * p)59 linux_syscall_intern(struct proc *p)
60 {
61 
62 	p->p_md.md_syscall = linux_syscall;
63 }
64 
65 /*
66  * syscall(frame):
67  *	System call request from POSIX system call gate interface to kernel.
68  * Like trap(), argument is call by reference.
69  */
70 void
linux_syscall(struct trapframe * frame)71 linux_syscall(struct trapframe *frame)
72 {
73 	register const struct sysent *callp;
74 	struct lwp *l;
75 	int error;
76 	register_t code, args[6], rval[2];
77 
78 	l = curlwp;
79 
80 	code = frame->tf_eax & (LINUX_SYS_NSYSENT - 1);
81 	callp = linux_sysent;
82 
83 	callp += code;
84 	/*
85 	 * Linux passes the args in ebx, ecx, edx, esi, edi, ebp, in
86 	 * increasing order.
87 	 */
88 	args[0] = frame->tf_ebx;
89 	args[1] = frame->tf_ecx;
90 	args[2] = frame->tf_edx;
91 	args[3] = frame->tf_esi;
92 	args[4] = frame->tf_edi;
93 	args[5] = frame->tf_ebp;
94 
95 	rval[0] = 0;
96 	rval[1] = 0;
97 
98 	if (__predict_false(l->l_proc->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry))) {
99 		error = trace_enter(code, callp, args);
100 		if (__predict_true(error == 0))
101 			error = sy_call(callp, l, args, rval);
102 	} else
103 		error = sy_call(callp, l, args, rval);
104 
105 	if (__predict_true(error == 0)) {
106 		frame->tf_eax = rval[0];
107 		/*
108 		 * XXX The linux libc code I (dsl) looked at doesn't use the
109 		 * carry bit.
110 		 * Values above 0xfffff000 are assumed to be errno values and
111 		 * not result codes!
112 		 */
113 		frame->tf_eflags &= ~PSL_C;	/* carry bit */
114 	} else {
115 		switch (error) {
116 		case ERESTART:
117 			/*
118 			 * The offset to adjust the PC by depends on whether
119 			 * we entered the kernel through the trap or call gate.
120 			 * We save the instruction size in tf_err on entry.
121 			 */
122 			frame->tf_eip -= frame->tf_err;
123 			break;
124 		case EJUSTRETURN:
125 			/* nothing to do */
126 			break;
127 		default:
128 			error = native_to_linux_errno[error];
129 			frame->tf_eax = error;
130 			frame->tf_eflags |= PSL_C;	/* carry bit */
131 			break;
132 		}
133 	}
134 	if (__predict_false(l->l_proc->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return)))
135 		trace_exit(code, callp, args, rval, error);
136 
137 	userret(l);
138 }
139