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