1 /* $NetBSD: syscall.c,v 1.12 2023/02/25 00:40:22 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
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/param.h>
33 #include <sys/cpu.h>
34 #include <sys/ktrace.h>
35 #include <sys/proc.h>
36 #include <sys/reboot.h>
37 #include <sys/systm.h>
38 #include <sys/lwp.h>
39 #include <sys/syscallvar.h>
40
41 #include <uvm/uvm_extern.h>
42
43 #include <aarch64/userret.h>
44 #include <aarch64/frame.h>
45 #include <aarch64/machdep.h>
46 #include <aarch64/armreg.h>
47
48 #ifndef NARGREG
49 #define NARGREG 8 /* 8 args are in registers */
50 #endif
51 #define MOREARGS(sp) ((const void *)(uintptr_t)(sp)) /* more args go here */
52
53 #ifndef EMULNAME
54 #include <sys/syscall.h>
55
56 #define SYSCALL_INDIRECT_CODE_REG 17 /* netbsd/aarch64 use x17 */
57
58 #define EMULNAME(x) (x)
59 #define EMULNAMEU(x) (x)
60
61 __KERNEL_RCSID(0, "$NetBSD: syscall.c,v 1.12 2023/02/25 00:40:22 riastradh Exp $");
62
63 void
cpu_spawn_return(struct lwp * l)64 cpu_spawn_return(struct lwp *l)
65 {
66
67 userret(l);
68 }
69
70 void
md_child_return(struct lwp * l)71 md_child_return(struct lwp *l)
72 {
73 struct trapframe * const tf = lwp_trapframe(l);
74
75 tf->tf_reg[0] = 0;
76 tf->tf_reg[1] = 1;
77 tf->tf_spsr &= ~NZCV_C;
78 l->l_md.md_cpacr = CPACR_FPEN_NONE;
79
80 userret(l);
81 }
82 #endif
83
84 static void EMULNAME(syscall)(struct trapframe *);
85
86 void
EMULNAME(syscall)87 EMULNAME(syscall)(struct trapframe *tf)
88 {
89 struct lwp * const l = curlwp;
90 struct proc * const p = l->l_proc;
91 register_t rval[2];
92 register_t args[10];
93 int error;
94
95 LWP_CACHE_CREDS(l, p);
96
97 curcpu()->ci_data.cpu_nsyscall++; /* XXX unsafe curcpu() */
98
99 register_t *params = (void *)tf->tf_reg;
100 size_t nargs = NARGREG;
101 #ifdef SYSCALL_CODE_REG
102 /*
103 * mov x<SYSCALL_CODE_REG>, #<syscall_no>
104 * svc #<optional>
105 */
106 size_t code = tf->tf_reg[SYSCALL_CODE_REG];
107 #if (SYSCALL_CODE_REG == 0)
108 params++;
109 #endif
110 #else /* SYSCALL_CODE_REG */
111 /*
112 * svc #<syscall_no>
113 */
114 size_t code = tf->tf_esr & 0xffff;
115 #endif /* SYSCALL_CODE_REG */
116
117 #ifndef SYSCALL_NO_INDIRECT
118 switch (code) {
119 case EMULNAMEU(SYS_syscall):
120 case EMULNAMEU(SYS___syscall):
121 #if (SYSCALL_INDIRECT_CODE_REG == 0)
122 code = *params++;
123 nargs -= 1;
124 #else
125 code = tf->tf_reg[SYSCALL_INDIRECT_CODE_REG];
126 #endif
127 /*
128 * code is first argument,
129 * followed by actual args.
130 */
131 break;
132 default:
133 break;
134 }
135 #endif /* !SYSCALL_NO_INDIRECT */
136
137 code &= EMULNAMEU(SYS_NSYSENT) - 1;
138 const struct sysent * const callp = p->p_emul->e_sysent + code;
139
140 if (__predict_false(callp->sy_narg > nargs)) {
141 const size_t diff = callp->sy_narg - nargs;
142 memcpy(args, params, nargs * sizeof(params[0]));
143 error = copyin(MOREARGS(tf->tf_sp), &args[nargs],
144 diff * sizeof(register_t));
145 if (error)
146 goto bad;
147 params = args;
148 }
149
150 rval[0] = 0;
151 rval[1] = tf->tf_reg[1];
152 error = sy_invoke(callp, l, params, rval, code);
153
154 if (__predict_true(error == 0)) {
155 tf->tf_reg[0] = rval[0];
156 #ifndef SYSCALL_NO_RVAL1
157 tf->tf_reg[1] = rval[1];
158 #endif
159 tf->tf_spsr &= ~NZCV_C;
160 } else {
161 switch (error) {
162 case ERESTART:
163 /*
164 * Set user's pc back to redo the system call.
165 */
166 tf->tf_pc -= 4;
167 break;
168 case EJUSTRETURN:
169 /* nothing to do */
170 break;
171 default:
172 bad:
173 #ifndef __HAVE_MINIMAL_EMUL
174 if (p->p_emul->e_errno)
175 error = p->p_emul->e_errno[error];
176 #elif defined(SYSCALL_EMUL_ERRNO)
177 error = SYSCALL_EMUL_ERRNO(error);
178 #endif
179 tf->tf_reg[0] = error;
180 tf->tf_spsr |= NZCV_C;
181 break;
182 }
183 }
184
185 userret(l);
186 }
187
188 void EMULNAME(syscall_intern)(struct proc *);
189
190 void
EMULNAME(syscall_intern)191 EMULNAME(syscall_intern)(struct proc *p)
192 {
193
194 p->p_md.md_syscall = EMULNAME(syscall);
195 }
196