linux32_misc.c revision 1.26.18.1 1 1.26.18.1 martin /* $NetBSD: linux32_misc.c,v 1.26.18.1 2019/09/13 06:25:25 martin 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.26.18.1 martin __KERNEL_RCSID(0, "$NetBSD: linux32_misc.c,v 1.26.18.1 2019/09/13 06:25:25 martin 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.1 manu
47 1.1 manu #include <compat/netbsd32/netbsd32.h>
48 1.1 manu #include <compat/netbsd32/netbsd32_syscallargs.h>
49 1.1 manu
50 1.23 chs #include <compat/linux/common/linux_types.h>
51 1.23 chs
52 1.5 njoly #include <compat/linux32/common/linux32_types.h>
53 1.5 njoly #include <compat/linux32/common/linux32_signal.h>
54 1.22 christos #include <compat/linux32/common/linux32_sched.h>
55 1.5 njoly #include <compat/linux32/linux32_syscallargs.h>
56 1.5 njoly
57 1.11 christos #include <compat/linux/common/linux_ptrace.h>
58 1.19 chs #include <compat/linux/common/linux_emuldata.h>
59 1.1 manu #include <compat/linux/common/linux_signal.h>
60 1.1 manu #include <compat/linux/common/linux_misc.h>
61 1.5 njoly #include <compat/linux/common/linux_statfs.h>
62 1.15 ad #include <compat/linux/common/linux_ipc.h>
63 1.15 ad #include <compat/linux/common/linux_sem.h>
64 1.19 chs #include <compat/linux/common/linux_futex.h>
65 1.1 manu #include <compat/linux/linux_syscallargs.h>
66 1.1 manu
67 1.5 njoly extern const struct linux_mnttypes linux_fstypes[];
68 1.5 njoly extern const int linux_fstypes_cnt;
69 1.1 manu
70 1.19 chs void linux32_to_native_timespec(struct timespec *, struct linux32_timespec *);
71 1.1 manu
72 1.5 njoly /*
73 1.5 njoly * Implement the fs stat functions. Straightforward.
74 1.5 njoly */
75 1.1 manu int
76 1.9 dsl linux32_sys_statfs(struct lwp *l, const struct linux32_sys_statfs_args *uap, register_t *retval)
77 1.1 manu {
78 1.9 dsl /* {
79 1.5 njoly syscallarg(const netbsd32_charp char) path;
80 1.5 njoly syscallarg(linux32_statfsp) sp;
81 1.9 dsl } */
82 1.7 dsl struct statvfs *sb;
83 1.5 njoly struct linux_statfs ltmp;
84 1.1 manu int error;
85 1.1 manu
86 1.7 dsl sb = STATVFSBUF_GET();
87 1.7 dsl error = do_sys_pstatvfs(l, SCARG_P32(uap, path), ST_WAIT, sb);
88 1.7 dsl if (error == 0) {
89 1.7 dsl bsd_to_linux_statfs(sb, <mp);
90 1.7 dsl error = copyout(<mp, SCARG_P32(uap, sp), sizeof ltmp);
91 1.7 dsl }
92 1.1 manu
93 1.7 dsl STATVFSBUF_PUT(sb);
94 1.5 njoly return error;
95 1.1 manu }
96 1.12 njoly
97 1.17 njoly int
98 1.17 njoly linux32_sys_fstatfs(struct lwp *l, const struct linux32_sys_fstatfs_args *uap, register_t *retval)
99 1.17 njoly {
100 1.17 njoly /* {
101 1.17 njoly syscallarg(int) fd;
102 1.17 njoly syscallarg(linux32_statfsp) sp;
103 1.17 njoly } */
104 1.17 njoly struct statvfs *sb;
105 1.17 njoly struct linux_statfs ltmp;
106 1.17 njoly int error;
107 1.17 njoly
108 1.17 njoly sb = STATVFSBUF_GET();
109 1.17 njoly error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
110 1.17 njoly if (error == 0) {
111 1.17 njoly bsd_to_linux_statfs(sb, <mp);
112 1.17 njoly error = copyout(<mp, SCARG_P32(uap, sp), sizeof ltmp);
113 1.17 njoly }
114 1.17 njoly STATVFSBUF_PUT(sb);
115 1.17 njoly
116 1.17 njoly return error;
117 1.17 njoly }
118 1.17 njoly
119 1.20 chs int
120 1.20 chs linux32_sys_statfs64(struct lwp *l, const struct linux32_sys_statfs64_args *uap, register_t *retval)
121 1.20 chs {
122 1.20 chs /* {
123 1.20 chs syscallarg(const netbsd32_charp char) path;
124 1.20 chs syscallarg(linux32_statfs64p) sp;
125 1.20 chs } */
126 1.20 chs struct statvfs *sb;
127 1.20 chs struct linux_statfs64 ltmp;
128 1.20 chs int error;
129 1.20 chs
130 1.20 chs sb = STATVFSBUF_GET();
131 1.20 chs error = do_sys_pstatvfs(l, SCARG_P32(uap, path), ST_WAIT, sb);
132 1.20 chs if (error == 0) {
133 1.20 chs bsd_to_linux_statfs64(sb, <mp);
134 1.20 chs error = copyout(<mp, SCARG_P32(uap, sp), sizeof ltmp);
135 1.20 chs }
136 1.20 chs
137 1.20 chs STATVFSBUF_PUT(sb);
138 1.20 chs return error;
139 1.20 chs }
140 1.20 chs
141 1.20 chs int
142 1.20 chs linux32_sys_fstatfs64(struct lwp *l, const struct linux32_sys_fstatfs64_args *uap, register_t *retval)
143 1.20 chs {
144 1.20 chs /* {
145 1.20 chs syscallarg(int) fd;
146 1.20 chs syscallarg(linux32_statfs64p) sp;
147 1.20 chs } */
148 1.20 chs struct statvfs *sb;
149 1.20 chs struct linux_statfs64 ltmp;
150 1.20 chs int error;
151 1.20 chs
152 1.20 chs sb = STATVFSBUF_GET();
153 1.20 chs error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
154 1.20 chs if (error == 0) {
155 1.20 chs bsd_to_linux_statfs64(sb, <mp);
156 1.20 chs error = copyout(<mp, SCARG_P32(uap, sp), sizeof ltmp);
157 1.20 chs }
158 1.20 chs STATVFSBUF_PUT(sb);
159 1.20 chs
160 1.20 chs return error;
161 1.20 chs }
162 1.20 chs
163 1.11 christos extern const int linux_ptrace_request_map[];
164 1.12 njoly
165 1.11 christos int
166 1.11 christos linux32_sys_ptrace(struct lwp *l, const struct linux32_sys_ptrace_args *uap, register_t *retval)
167 1.11 christos {
168 1.11 christos /* {
169 1.11 christos i386, m68k, powerpc: T=int
170 1.11 christos alpha, amd64: T=long
171 1.11 christos syscallarg(T) request;
172 1.11 christos syscallarg(T) pid;
173 1.11 christos syscallarg(T) addr;
174 1.11 christos syscallarg(T) data;
175 1.11 christos } */
176 1.11 christos const int *ptr;
177 1.11 christos int request;
178 1.11 christos int error;
179 1.11 christos
180 1.11 christos ptr = linux_ptrace_request_map;
181 1.11 christos request = SCARG(uap, request);
182 1.11 christos while (*ptr != -1)
183 1.11 christos if (*ptr++ == request) {
184 1.12 njoly struct sys_ptrace_args pta;
185 1.11 christos
186 1.11 christos SCARG(&pta, req) = *ptr;
187 1.11 christos SCARG(&pta, pid) = SCARG(uap, pid);
188 1.12 njoly SCARG(&pta, addr) = NETBSD32IPTR64(SCARG(uap, addr));
189 1.11 christos SCARG(&pta, data) = SCARG(uap, data);
190 1.11 christos
191 1.11 christos /*
192 1.11 christos * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
193 1.11 christos * to continue where the process left off previously.
194 1.11 christos * The same thing is achieved by addr == (void *) 1
195 1.11 christos * on NetBSD, so rewrite 'addr' appropriately.
196 1.11 christos */
197 1.11 christos if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
198 1.11 christos SCARG(&pta, addr) = (void *) 1;
199 1.11 christos
200 1.13 ad error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
201 1.11 christos if (error)
202 1.11 christos return error;
203 1.11 christos switch (request) {
204 1.11 christos case LINUX_PTRACE_PEEKTEXT:
205 1.11 christos case LINUX_PTRACE_PEEKDATA:
206 1.11 christos error = copyout (retval,
207 1.12 njoly NETBSD32IPTR64(SCARG(uap, data)),
208 1.11 christos sizeof *retval);
209 1.11 christos *retval = SCARG(uap, data);
210 1.11 christos break;
211 1.11 christos default:
212 1.11 christos break;
213 1.11 christos }
214 1.11 christos return error;
215 1.11 christos }
216 1.11 christos else
217 1.11 christos ptr++;
218 1.11 christos
219 1.12 njoly return EIO;
220 1.11 christos }
221 1.16 njoly
222 1.16 njoly int
223 1.16 njoly linux32_sys_personality(struct lwp *l, const struct linux32_sys_personality_args *uap, register_t *retval)
224 1.16 njoly {
225 1.16 njoly /* {
226 1.18 njoly syscallarg(netbsd32_u_long) per;
227 1.16 njoly } */
228 1.21 chs struct linux_sys_personality_args ua;
229 1.16 njoly
230 1.21 chs NETBSD32TOX_UAP(per, long);
231 1.21 chs return linux_sys_personality(l, &ua, retval);
232 1.16 njoly }
233 1.19 chs
234 1.19 chs int
235 1.19 chs linux32_sys_futex(struct lwp *l,
236 1.19 chs const struct linux32_sys_futex_args *uap, register_t *retval)
237 1.19 chs {
238 1.19 chs /* {
239 1.19 chs syscallarg(linux32_intp_t) uaddr;
240 1.19 chs syscallarg(int) op;
241 1.19 chs syscallarg(int) val;
242 1.19 chs syscallarg(linux32_timespecp_t) timeout;
243 1.19 chs syscallarg(linux32_intp_t) uaddr2;
244 1.19 chs syscallarg(int) val3;
245 1.19 chs } */
246 1.19 chs struct linux_sys_futex_args ua;
247 1.19 chs struct linux32_timespec lts;
248 1.19 chs struct timespec ts = { 0, 0 };
249 1.19 chs int error;
250 1.19 chs
251 1.19 chs NETBSD32TOP_UAP(uaddr, int);
252 1.19 chs NETBSD32TO64_UAP(op);
253 1.19 chs NETBSD32TO64_UAP(val);
254 1.19 chs NETBSD32TOP_UAP(timeout, struct linux_timespec);
255 1.19 chs NETBSD32TOP_UAP(uaddr2, int);
256 1.19 chs NETBSD32TO64_UAP(val3);
257 1.19 chs if ((SCARG(uap, op) & ~LINUX_FUTEX_PRIVATE_FLAG) == LINUX_FUTEX_WAIT &&
258 1.19 chs SCARG_P32(uap, timeout) != NULL) {
259 1.19 chs if ((error = copyin((void *)SCARG_P32(uap, timeout),
260 1.19 chs <s, sizeof(lts))) != 0) {
261 1.19 chs return error;
262 1.19 chs }
263 1.19 chs linux32_to_native_timespec(&ts, <s);
264 1.19 chs }
265 1.26 dholland return linux_do_futex(l, &ua, &ts, retval);
266 1.19 chs }
267 1.19 chs
268 1.19 chs int
269 1.19 chs linux32_sys_set_robust_list(struct lwp *l,
270 1.19 chs const struct linux32_sys_set_robust_list_args *uap, register_t *retval)
271 1.19 chs {
272 1.19 chs /* {
273 1.19 chs syscallarg(linux32_robust_list_headp_t) head;
274 1.19 chs syscallarg(linux32_size_t) len;
275 1.19 chs } */
276 1.19 chs struct linux_sys_set_robust_list_args ua;
277 1.19 chs struct linux_emuldata *led;
278 1.19 chs
279 1.19 chs if (SCARG(uap, len) != 12)
280 1.19 chs return EINVAL;
281 1.19 chs
282 1.19 chs NETBSD32TOP_UAP(head, struct robust_list_head);
283 1.19 chs NETBSD32TOX64_UAP(len, size_t);
284 1.19 chs
285 1.19 chs led = l->l_emuldata;
286 1.19 chs led->led_robust_head = SCARG(&ua, head);
287 1.19 chs *retval = 0;
288 1.19 chs return 0;
289 1.19 chs }
290 1.19 chs
291 1.19 chs int
292 1.19 chs linux32_sys_get_robust_list(struct lwp *l,
293 1.19 chs const struct linux32_sys_get_robust_list_args *uap, register_t *retval)
294 1.19 chs {
295 1.19 chs /* {
296 1.26.18.1 martin syscallarg(linux32_pid_t) pid;
297 1.19 chs syscallarg(linux32_robust_list_headpp_t) head;
298 1.19 chs syscallarg(linux32_sizep_t) len;
299 1.19 chs } */
300 1.19 chs struct linux_sys_get_robust_list_args ua;
301 1.19 chs
302 1.26.18.1 martin NETBSD32TOX_UAP(pid, int);
303 1.19 chs NETBSD32TOP_UAP(head, struct robust_list_head *);
304 1.20 chs NETBSD32TOP_UAP(len, size_t *);
305 1.19 chs return linux_sys_get_robust_list(l, &ua, retval);
306 1.19 chs }
307 1.20 chs
308 1.20 chs int
309 1.20 chs linux32_sys_truncate64(struct lwp *l, const struct linux32_sys_truncate64_args *uap, register_t *retval)
310 1.20 chs {
311 1.20 chs /* {
312 1.20 chs syscallarg(netbsd32_charp) path;
313 1.20 chs syscallarg(off_t) length;
314 1.20 chs } */
315 1.20 chs struct sys_truncate_args ua;
316 1.20 chs
317 1.20 chs /* Linux doesn't have the 'pad' pseudo-parameter */
318 1.20 chs NETBSD32TOP_UAP(path, const char *);
319 1.20 chs SCARG(&ua, PAD) = 0;
320 1.20 chs SCARG(&ua, length) = ((off_t)SCARG(uap, lenhi) << 32) + SCARG(uap, lenlo);
321 1.20 chs return sys_truncate(l, &ua, retval);
322 1.20 chs }
323 1.20 chs
324 1.20 chs int
325 1.20 chs linux32_sys_ftruncate64(struct lwp *l, const struct linux32_sys_ftruncate64_args *uap, register_t *retval)
326 1.20 chs {
327 1.20 chs /* {
328 1.20 chs syscallarg(unsigned int) fd;
329 1.20 chs syscallarg(off_t) length;
330 1.20 chs } */
331 1.20 chs struct sys_ftruncate_args ua;
332 1.20 chs
333 1.20 chs /* Linux doesn't have the 'pad' pseudo-parameter */
334 1.20 chs NETBSD32TO64_UAP(fd);
335 1.20 chs SCARG(&ua, PAD) = 0;
336 1.20 chs SCARG(&ua, length) = ((off_t)SCARG(uap, lenhi) << 32) + SCARG(uap, lenlo);
337 1.20 chs return sys_ftruncate(l, &ua, retval);
338 1.20 chs }
339 1.20 chs
340 1.20 chs int
341 1.20 chs linux32_sys_setdomainname(struct lwp *l, const struct linux32_sys_setdomainname_args *uap, register_t *retval)
342 1.20 chs {
343 1.20 chs /* {
344 1.20 chs syscallarg(netbsd32_charp) domainname;
345 1.20 chs syscallarg(int) len;
346 1.20 chs } */
347 1.20 chs struct linux_sys_setdomainname_args ua;
348 1.20 chs
349 1.20 chs NETBSD32TOP_UAP(domainname, char);
350 1.20 chs NETBSD32TO64_UAP(len);
351 1.20 chs return linux_sys_setdomainname(l, &ua, retval);
352 1.20 chs }
353 1.25 njoly
354 1.25 njoly int
355 1.25 njoly linux32_sys_ppoll(struct lwp *l, const struct linux32_sys_ppoll_args *uap,
356 1.25 njoly register_t *retval)
357 1.25 njoly {
358 1.25 njoly /* {
359 1.25 njoly syscallarg(netbsd32_pollfdp_t) fds;
360 1.25 njoly syscallarg(u_int) nfds;
361 1.25 njoly syscallarg(linux32_timespecp_t) timeout;
362 1.25 njoly syscallarg(linux32_sigsetp_t) sigset;
363 1.25 njoly } */
364 1.25 njoly struct linux32_timespec lts0, *lts;
365 1.25 njoly struct timespec ts0, *ts = NULL;
366 1.25 njoly linux32_sigset_t lsigmask0, *lsigmask;
367 1.25 njoly sigset_t sigmask0, *sigmask = NULL;
368 1.25 njoly int error;
369 1.25 njoly
370 1.25 njoly lts = SCARG_P32(uap, timeout);
371 1.25 njoly if (lts) {
372 1.25 njoly if ((error = copyin(lts, <s0, sizeof(lts0))) != 0)
373 1.25 njoly return error;
374 1.25 njoly linux32_to_native_timespec(&ts0, <s0);
375 1.25 njoly ts = &ts0;
376 1.25 njoly }
377 1.25 njoly
378 1.25 njoly lsigmask = SCARG_P32(uap, sigset);
379 1.25 njoly if (lsigmask) {
380 1.25 njoly if ((error = copyin(lsigmask, &lsigmask0, sizeof(lsigmask0))))
381 1.25 njoly return error;
382 1.25 njoly linux32_to_native_sigset(&sigmask0, &lsigmask0);
383 1.25 njoly sigmask = &sigmask0;
384 1.25 njoly }
385 1.25 njoly
386 1.25 njoly return pollcommon(retval, SCARG_P32(uap, fds), SCARG(uap, nfds),
387 1.25 njoly ts, sigmask);
388 1.25 njoly }
389