linux32_misc.c revision 1.34 1 1.34 ryo /* $NetBSD: linux32_misc.c,v 1.34 2021/11/25 03:08:04 ryo Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.5 njoly * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
5 1.5 njoly * All rights reserved.
6 1.5 njoly *
7 1.5 njoly * This code is derived from software contributed to The NetBSD Foundation
8 1.5 njoly * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
9 1.5 njoly * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center;
10 1.5 njoly * by Edgar Fu\ss, Mathematisches Institut der Uni Bonn.
11 1.1 manu *
12 1.1 manu * Redistribution and use in source and binary forms, with or without
13 1.1 manu * modification, are permitted provided that the following conditions
14 1.1 manu * are met:
15 1.1 manu * 1. Redistributions of source code must retain the above copyright
16 1.1 manu * notice, this list of conditions and the following disclaimer.
17 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 manu * notice, this list of conditions and the following disclaimer in the
19 1.1 manu * documentation and/or other materials provided with the distribution.
20 1.1 manu *
21 1.5 njoly * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.5 njoly * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.5 njoly * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.5 njoly * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
32 1.1 manu */
33 1.1 manu
34 1.1 manu #include <sys/cdefs.h>
35 1.34 ryo __KERNEL_RCSID(0, "$NetBSD: linux32_misc.c,v 1.34 2021/11/25 03:08:04 ryo Exp $");
36 1.1 manu
37 1.1 manu #include <sys/param.h>
38 1.1 manu #include <sys/proc.h>
39 1.5 njoly #include <sys/time.h>
40 1.5 njoly #include <sys/types.h>
41 1.7 dsl #include <sys/fstypes.h>
42 1.7 dsl #include <sys/vfs_syscalls.h>
43 1.11 christos #include <sys/ptrace.h>
44 1.14 njoly #include <sys/syscall.h>
45 1.25 njoly #include <sys/poll.h>
46 1.28 thorpej #include <sys/futex.h>
47 1.1 manu
48 1.1 manu #include <compat/netbsd32/netbsd32.h>
49 1.1 manu #include <compat/netbsd32/netbsd32_syscallargs.h>
50 1.1 manu
51 1.23 chs #include <compat/linux/common/linux_types.h>
52 1.23 chs
53 1.5 njoly #include <compat/linux32/common/linux32_types.h>
54 1.34 ryo #include <compat/linux32/common/linux32_machdep.h>
55 1.5 njoly #include <compat/linux32/common/linux32_signal.h>
56 1.22 christos #include <compat/linux32/common/linux32_sched.h>
57 1.5 njoly #include <compat/linux32/linux32_syscallargs.h>
58 1.5 njoly
59 1.11 christos #include <compat/linux/common/linux_ptrace.h>
60 1.19 chs #include <compat/linux/common/linux_emuldata.h>
61 1.1 manu #include <compat/linux/common/linux_signal.h>
62 1.1 manu #include <compat/linux/common/linux_misc.h>
63 1.5 njoly #include <compat/linux/common/linux_statfs.h>
64 1.15 ad #include <compat/linux/common/linux_ipc.h>
65 1.15 ad #include <compat/linux/common/linux_sem.h>
66 1.1 manu #include <compat/linux/linux_syscallargs.h>
67 1.1 manu
68 1.5 njoly extern const struct linux_mnttypes linux_fstypes[];
69 1.5 njoly extern const int linux_fstypes_cnt;
70 1.1 manu
71 1.5 njoly /*
72 1.5 njoly * Implement the fs stat functions. Straightforward.
73 1.5 njoly */
74 1.1 manu int
75 1.9 dsl linux32_sys_statfs(struct lwp *l, const struct linux32_sys_statfs_args *uap, register_t *retval)
76 1.1 manu {
77 1.9 dsl /* {
78 1.5 njoly syscallarg(const netbsd32_charp char) path;
79 1.5 njoly syscallarg(linux32_statfsp) sp;
80 1.9 dsl } */
81 1.7 dsl struct statvfs *sb;
82 1.5 njoly struct linux_statfs ltmp;
83 1.1 manu int error;
84 1.1 manu
85 1.7 dsl sb = STATVFSBUF_GET();
86 1.7 dsl error = do_sys_pstatvfs(l, SCARG_P32(uap, path), ST_WAIT, sb);
87 1.7 dsl if (error == 0) {
88 1.7 dsl bsd_to_linux_statfs(sb, <mp);
89 1.7 dsl error = copyout(<mp, SCARG_P32(uap, sp), sizeof ltmp);
90 1.7 dsl }
91 1.1 manu
92 1.7 dsl STATVFSBUF_PUT(sb);
93 1.5 njoly return error;
94 1.1 manu }
95 1.12 njoly
96 1.17 njoly int
97 1.17 njoly linux32_sys_fstatfs(struct lwp *l, const struct linux32_sys_fstatfs_args *uap, register_t *retval)
98 1.17 njoly {
99 1.17 njoly /* {
100 1.17 njoly syscallarg(int) fd;
101 1.17 njoly syscallarg(linux32_statfsp) sp;
102 1.17 njoly } */
103 1.17 njoly struct statvfs *sb;
104 1.17 njoly struct linux_statfs ltmp;
105 1.17 njoly int error;
106 1.17 njoly
107 1.17 njoly sb = STATVFSBUF_GET();
108 1.17 njoly error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
109 1.17 njoly if (error == 0) {
110 1.17 njoly bsd_to_linux_statfs(sb, <mp);
111 1.17 njoly error = copyout(<mp, SCARG_P32(uap, sp), sizeof ltmp);
112 1.17 njoly }
113 1.17 njoly STATVFSBUF_PUT(sb);
114 1.17 njoly
115 1.17 njoly return error;
116 1.17 njoly }
117 1.17 njoly
118 1.20 chs int
119 1.20 chs linux32_sys_statfs64(struct lwp *l, const struct linux32_sys_statfs64_args *uap, register_t *retval)
120 1.20 chs {
121 1.20 chs /* {
122 1.20 chs syscallarg(const netbsd32_charp char) path;
123 1.20 chs syscallarg(linux32_statfs64p) sp;
124 1.20 chs } */
125 1.20 chs struct statvfs *sb;
126 1.20 chs struct linux_statfs64 ltmp;
127 1.20 chs int error;
128 1.20 chs
129 1.20 chs sb = STATVFSBUF_GET();
130 1.20 chs error = do_sys_pstatvfs(l, SCARG_P32(uap, path), ST_WAIT, sb);
131 1.20 chs if (error == 0) {
132 1.20 chs bsd_to_linux_statfs64(sb, <mp);
133 1.20 chs error = copyout(<mp, SCARG_P32(uap, sp), sizeof ltmp);
134 1.20 chs }
135 1.20 chs
136 1.20 chs STATVFSBUF_PUT(sb);
137 1.20 chs return error;
138 1.20 chs }
139 1.20 chs
140 1.20 chs int
141 1.20 chs linux32_sys_fstatfs64(struct lwp *l, const struct linux32_sys_fstatfs64_args *uap, register_t *retval)
142 1.20 chs {
143 1.20 chs /* {
144 1.20 chs syscallarg(int) fd;
145 1.20 chs syscallarg(linux32_statfs64p) sp;
146 1.20 chs } */
147 1.20 chs struct statvfs *sb;
148 1.20 chs struct linux_statfs64 ltmp;
149 1.20 chs int error;
150 1.20 chs
151 1.20 chs sb = STATVFSBUF_GET();
152 1.20 chs error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
153 1.20 chs if (error == 0) {
154 1.20 chs bsd_to_linux_statfs64(sb, <mp);
155 1.20 chs error = copyout(<mp, SCARG_P32(uap, sp), sizeof ltmp);
156 1.20 chs }
157 1.20 chs STATVFSBUF_PUT(sb);
158 1.20 chs
159 1.20 chs return error;
160 1.20 chs }
161 1.20 chs
162 1.11 christos extern const int linux_ptrace_request_map[];
163 1.12 njoly
164 1.11 christos int
165 1.11 christos linux32_sys_ptrace(struct lwp *l, const struct linux32_sys_ptrace_args *uap, register_t *retval)
166 1.11 christos {
167 1.11 christos /* {
168 1.11 christos i386, m68k, powerpc: T=int
169 1.34 ryo aarch64, alpha, amd64: T=long
170 1.11 christos syscallarg(T) request;
171 1.11 christos syscallarg(T) pid;
172 1.11 christos syscallarg(T) addr;
173 1.11 christos syscallarg(T) data;
174 1.11 christos } */
175 1.11 christos const int *ptr;
176 1.11 christos int request;
177 1.11 christos int error;
178 1.11 christos
179 1.11 christos ptr = linux_ptrace_request_map;
180 1.11 christos request = SCARG(uap, request);
181 1.11 christos while (*ptr != -1)
182 1.11 christos if (*ptr++ == request) {
183 1.12 njoly struct sys_ptrace_args pta;
184 1.11 christos
185 1.11 christos SCARG(&pta, req) = *ptr;
186 1.11 christos SCARG(&pta, pid) = SCARG(uap, pid);
187 1.12 njoly SCARG(&pta, addr) = NETBSD32IPTR64(SCARG(uap, addr));
188 1.11 christos SCARG(&pta, data) = SCARG(uap, data);
189 1.11 christos
190 1.11 christos /*
191 1.11 christos * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
192 1.11 christos * to continue where the process left off previously.
193 1.11 christos * The same thing is achieved by addr == (void *) 1
194 1.11 christos * on NetBSD, so rewrite 'addr' appropriately.
195 1.11 christos */
196 1.11 christos if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
197 1.11 christos SCARG(&pta, addr) = (void *) 1;
198 1.11 christos
199 1.13 ad error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
200 1.11 christos if (error)
201 1.11 christos return error;
202 1.11 christos switch (request) {
203 1.11 christos case LINUX_PTRACE_PEEKTEXT:
204 1.11 christos case LINUX_PTRACE_PEEKDATA:
205 1.11 christos error = copyout (retval,
206 1.12 njoly NETBSD32IPTR64(SCARG(uap, data)),
207 1.11 christos sizeof *retval);
208 1.11 christos *retval = SCARG(uap, data);
209 1.11 christos break;
210 1.11 christos default:
211 1.11 christos break;
212 1.11 christos }
213 1.11 christos return error;
214 1.11 christos }
215 1.11 christos else
216 1.11 christos ptr++;
217 1.11 christos
218 1.12 njoly return EIO;
219 1.11 christos }
220 1.16 njoly
221 1.16 njoly int
222 1.16 njoly linux32_sys_personality(struct lwp *l, const struct linux32_sys_personality_args *uap, register_t *retval)
223 1.16 njoly {
224 1.16 njoly /* {
225 1.18 njoly syscallarg(netbsd32_u_long) per;
226 1.16 njoly } */
227 1.21 chs struct linux_sys_personality_args ua;
228 1.16 njoly
229 1.21 chs NETBSD32TOX_UAP(per, long);
230 1.21 chs return linux_sys_personality(l, &ua, retval);
231 1.16 njoly }
232 1.19 chs
233 1.19 chs int
234 1.19 chs linux32_sys_futex(struct lwp *l,
235 1.19 chs const struct linux32_sys_futex_args *uap, register_t *retval)
236 1.19 chs {
237 1.19 chs /* {
238 1.19 chs syscallarg(linux32_intp_t) uaddr;
239 1.19 chs syscallarg(int) op;
240 1.19 chs syscallarg(int) val;
241 1.19 chs syscallarg(linux32_timespecp_t) timeout;
242 1.19 chs syscallarg(linux32_intp_t) uaddr2;
243 1.19 chs syscallarg(int) val3;
244 1.19 chs } */
245 1.19 chs struct linux32_timespec lts;
246 1.28 thorpej struct timespec ts, *tsp = NULL;
247 1.28 thorpej int val2 = 0;
248 1.19 chs int error;
249 1.19 chs
250 1.28 thorpej /*
251 1.28 thorpej * Linux overlays the "timeout" field and the "val2" field.
252 1.29 thorpej * "timeout" is only valid for FUTEX_WAIT and FUTEX_WAIT_BITSET
253 1.29 thorpej * on Linux.
254 1.28 thorpej */
255 1.29 thorpej const int op = (SCARG(uap, op) & FUTEX_CMD_MASK);
256 1.29 thorpej if ((op == FUTEX_WAIT || op == FUTEX_WAIT_BITSET) &&
257 1.19 chs SCARG_P32(uap, timeout) != NULL) {
258 1.28 thorpej if ((error = copyin(SCARG_P32(uap, timeout),
259 1.19 chs <s, sizeof(lts))) != 0) {
260 1.19 chs return error;
261 1.19 chs }
262 1.19 chs linux32_to_native_timespec(&ts, <s);
263 1.28 thorpej tsp = &ts;
264 1.28 thorpej } else {
265 1.28 thorpej val2 = (int)(uintptr_t)SCARG_P32(uap, timeout);
266 1.19 chs }
267 1.19 chs
268 1.30 thorpej return linux_do_futex(SCARG_P32(uap, uaddr), SCARG(uap, op),
269 1.28 thorpej SCARG(uap, val), tsp, SCARG_P32(uap, uaddr2), val2,
270 1.28 thorpej SCARG(uap, val3), retval);
271 1.19 chs }
272 1.20 chs
273 1.20 chs int
274 1.20 chs linux32_sys_truncate64(struct lwp *l, const struct linux32_sys_truncate64_args *uap, register_t *retval)
275 1.20 chs {
276 1.20 chs /* {
277 1.20 chs syscallarg(netbsd32_charp) path;
278 1.20 chs syscallarg(off_t) length;
279 1.20 chs } */
280 1.20 chs struct sys_truncate_args ua;
281 1.20 chs
282 1.20 chs /* Linux doesn't have the 'pad' pseudo-parameter */
283 1.20 chs NETBSD32TOP_UAP(path, const char *);
284 1.20 chs SCARG(&ua, PAD) = 0;
285 1.20 chs SCARG(&ua, length) = ((off_t)SCARG(uap, lenhi) << 32) + SCARG(uap, lenlo);
286 1.20 chs return sys_truncate(l, &ua, retval);
287 1.20 chs }
288 1.20 chs
289 1.20 chs int
290 1.20 chs linux32_sys_ftruncate64(struct lwp *l, const struct linux32_sys_ftruncate64_args *uap, register_t *retval)
291 1.20 chs {
292 1.20 chs /* {
293 1.20 chs syscallarg(unsigned int) fd;
294 1.20 chs syscallarg(off_t) length;
295 1.20 chs } */
296 1.20 chs struct sys_ftruncate_args ua;
297 1.20 chs
298 1.20 chs /* Linux doesn't have the 'pad' pseudo-parameter */
299 1.20 chs NETBSD32TO64_UAP(fd);
300 1.20 chs SCARG(&ua, PAD) = 0;
301 1.20 chs SCARG(&ua, length) = ((off_t)SCARG(uap, lenhi) << 32) + SCARG(uap, lenlo);
302 1.20 chs return sys_ftruncate(l, &ua, retval);
303 1.20 chs }
304 1.20 chs
305 1.20 chs int
306 1.20 chs linux32_sys_setdomainname(struct lwp *l, const struct linux32_sys_setdomainname_args *uap, register_t *retval)
307 1.20 chs {
308 1.20 chs /* {
309 1.20 chs syscallarg(netbsd32_charp) domainname;
310 1.20 chs syscallarg(int) len;
311 1.20 chs } */
312 1.20 chs struct linux_sys_setdomainname_args ua;
313 1.20 chs
314 1.20 chs NETBSD32TOP_UAP(domainname, char);
315 1.20 chs NETBSD32TO64_UAP(len);
316 1.20 chs return linux_sys_setdomainname(l, &ua, retval);
317 1.20 chs }
318 1.25 njoly
319 1.25 njoly int
320 1.25 njoly linux32_sys_ppoll(struct lwp *l, const struct linux32_sys_ppoll_args *uap,
321 1.25 njoly register_t *retval)
322 1.25 njoly {
323 1.25 njoly /* {
324 1.25 njoly syscallarg(netbsd32_pollfdp_t) fds;
325 1.25 njoly syscallarg(u_int) nfds;
326 1.25 njoly syscallarg(linux32_timespecp_t) timeout;
327 1.25 njoly syscallarg(linux32_sigsetp_t) sigset;
328 1.25 njoly } */
329 1.25 njoly struct linux32_timespec lts0, *lts;
330 1.25 njoly struct timespec ts0, *ts = NULL;
331 1.25 njoly linux32_sigset_t lsigmask0, *lsigmask;
332 1.25 njoly sigset_t sigmask0, *sigmask = NULL;
333 1.25 njoly int error;
334 1.25 njoly
335 1.25 njoly lts = SCARG_P32(uap, timeout);
336 1.25 njoly if (lts) {
337 1.25 njoly if ((error = copyin(lts, <s0, sizeof(lts0))) != 0)
338 1.25 njoly return error;
339 1.25 njoly linux32_to_native_timespec(&ts0, <s0);
340 1.25 njoly ts = &ts0;
341 1.25 njoly }
342 1.25 njoly
343 1.25 njoly lsigmask = SCARG_P32(uap, sigset);
344 1.25 njoly if (lsigmask) {
345 1.25 njoly if ((error = copyin(lsigmask, &lsigmask0, sizeof(lsigmask0))))
346 1.25 njoly return error;
347 1.25 njoly linux32_to_native_sigset(&sigmask0, &lsigmask0);
348 1.25 njoly sigmask = &sigmask0;
349 1.25 njoly }
350 1.25 njoly
351 1.25 njoly return pollcommon(retval, SCARG_P32(uap, fds), SCARG(uap, nfds),
352 1.25 njoly ts, sigmask);
353 1.25 njoly }
354 1.32 thorpej
355 1.32 thorpej int
356 1.32 thorpej linux32_sys_eventfd(struct lwp *l, const struct linux32_sys_eventfd_args *uap,
357 1.32 thorpej register_t *retval)
358 1.32 thorpej {
359 1.32 thorpej /* {
360 1.32 thorpej syscallarg(unsigned int) initval;
361 1.32 thorpej } */
362 1.32 thorpej struct linux_sys_eventfd_args ua;
363 1.32 thorpej
364 1.32 thorpej NETBSD32TO64_UAP(initval);
365 1.32 thorpej
366 1.32 thorpej return linux_sys_eventfd(l, &ua, retval);
367 1.32 thorpej }
368 1.32 thorpej
369 1.32 thorpej int
370 1.32 thorpej linux32_sys_eventfd2(struct lwp *l, const struct linux32_sys_eventfd2_args *uap,
371 1.32 thorpej register_t *retval)
372 1.32 thorpej {
373 1.32 thorpej /* {
374 1.32 thorpej syscallarg(unsigned int) initval;
375 1.32 thorpej syscallarg(int) flags;
376 1.32 thorpej } */
377 1.32 thorpej struct linux_sys_eventfd2_args ua;
378 1.32 thorpej
379 1.32 thorpej NETBSD32TO64_UAP(initval);
380 1.32 thorpej NETBSD32TO64_UAP(flags);
381 1.32 thorpej
382 1.32 thorpej return linux_sys_eventfd2(l, &ua, retval);
383 1.32 thorpej }
384 1.33 thorpej
385 1.33 thorpej static inline off_t
386 1.33 thorpej linux32_hilo_to_off_t(unsigned long hi, unsigned long lo)
387 1.33 thorpej {
388 1.33 thorpej return (((off_t)hi) << 32) | lo;
389 1.33 thorpej }
390 1.33 thorpej
391 1.33 thorpej int
392 1.33 thorpej linux32_sys_preadv(struct lwp *l, const struct linux32_sys_preadv_args *uap,
393 1.33 thorpej register_t *retval)
394 1.33 thorpej {
395 1.33 thorpej /* {
396 1.33 thorpej syscallarg(int) fd;
397 1.33 thorpej syscallarg(const netbsd32_iovecp_t) iovp;
398 1.33 thorpej syscallarg(int) iovcnt;
399 1.33 thorpej syscallarg(netbsd32_u_long) off_lo;
400 1.33 thorpej syscallarg(netbsd32_u_long) off_hi;
401 1.33 thorpej } */
402 1.33 thorpej struct netbsd32_preadv_args ua;
403 1.33 thorpej
404 1.33 thorpej SCARG(&ua, fd) = SCARG(uap, fd);
405 1.33 thorpej SCARG(&ua, iovp) = SCARG(uap, iovp);
406 1.33 thorpej SCARG(&ua, iovcnt) = SCARG(uap, iovcnt);
407 1.33 thorpej SCARG(&ua, PAD) = 0;
408 1.33 thorpej SCARG(&ua, offset) = linux32_hilo_to_off_t(SCARG(uap, off_hi),
409 1.33 thorpej SCARG(uap, off_lo));
410 1.33 thorpej return netbsd32_preadv(l, &ua, retval);
411 1.33 thorpej }
412 1.33 thorpej
413 1.33 thorpej int
414 1.33 thorpej linux32_sys_pwritev(struct lwp *l, const struct linux32_sys_pwritev_args *uap,
415 1.33 thorpej register_t *retval)
416 1.33 thorpej {
417 1.33 thorpej /* {
418 1.33 thorpej syscallarg(int) fd;
419 1.33 thorpej syscallarg(const netbsd32_iovecp_t) iovp;
420 1.33 thorpej syscallarg(int) iovcnt;
421 1.33 thorpej syscallarg(netbsd32_u_long) off_lo;
422 1.33 thorpej syscallarg(netbsd32_u_long) off_hi;
423 1.33 thorpej } */
424 1.33 thorpej struct netbsd32_pwritev_args ua;
425 1.33 thorpej
426 1.33 thorpej SCARG(&ua, fd) = SCARG(uap, fd);
427 1.33 thorpej SCARG(&ua, iovp) = SCARG(uap, iovp);
428 1.33 thorpej SCARG(&ua, iovcnt) = SCARG(uap, iovcnt);
429 1.33 thorpej SCARG(&ua, PAD) = 0;
430 1.33 thorpej SCARG(&ua, offset) = linux32_hilo_to_off_t(SCARG(uap, off_hi),
431 1.33 thorpej SCARG(uap, off_lo));
432 1.33 thorpej return netbsd32_pwritev(l, &ua, retval);
433 1.33 thorpej }
434