1 /* $NetBSD: linux_syscall.c,v 1.54 2018/08/10 21:44:58 pgoyette 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.54 2018/08/10 21:44:58 pgoyette 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 LWP_CACHE_CREDS(l, l->l_proc);
80
81 code = frame->tf_eax & (LINUX_SYS_NSYSENT - 1);
82 callp = linux_sysent;
83
84 callp += code;
85 /*
86 * Linux passes the args in ebx, ecx, edx, esi, edi, ebp, in
87 * increasing order.
88 */
89 args[0] = frame->tf_ebx;
90 args[1] = frame->tf_ecx;
91 args[2] = frame->tf_edx;
92 args[3] = frame->tf_esi;
93 args[4] = frame->tf_edi;
94 args[5] = frame->tf_ebp;
95
96 rval[0] = 0;
97 rval[1] = 0;
98
99 if (__predict_false(l->l_proc->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry))) {
100 error = trace_enter(code, callp, args);
101 if (__predict_true(error == 0))
102 error = sy_call(callp, l, args, rval);
103 } else
104 error = sy_call(callp, l, args, rval);
105
106 if (__predict_true(error == 0)) {
107 frame->tf_eax = rval[0];
108 /*
109 * XXX The linux libc code I (dsl) looked at doesn't use the
110 * carry bit.
111 * Values above 0xfffff000 are assumed to be errno values and
112 * not result codes!
113 */
114 frame->tf_eflags &= ~PSL_C; /* carry bit */
115 } else {
116 switch (error) {
117 case ERESTART:
118 /*
119 * The offset to adjust the PC by depends on whether
120 * we entered the kernel through the trap or call gate.
121 * We save the instruction size in tf_err on entry.
122 */
123 frame->tf_eip -= frame->tf_err;
124 break;
125 case EJUSTRETURN:
126 /* nothing to do */
127 break;
128 default:
129 error = native_to_linux_errno[error];
130 frame->tf_eax = error;
131 frame->tf_eflags |= PSL_C; /* carry bit */
132 break;
133 }
134 }
135 if (__predict_false(l->l_proc->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return)))
136 trace_exit(code, callp, args, rval, error);
137
138 userret(l);
139 }
140