xref: /netbsd-src/sys/compat/linux32/common/linux32_misc.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: linux32_misc.c,v 1.22 2011/11/18 04:08:56 christos 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.22 2011/11/18 04:08:56 christos 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/malloc.h>
42 #include <sys/fstypes.h>
43 #include <sys/vfs_syscalls.h>
44 #include <sys/ptrace.h>
45 #include <sys/syscall.h>
46 
47 #include <compat/netbsd32/netbsd32.h>
48 #include <compat/netbsd32/netbsd32_syscallargs.h>
49 
50 #include <compat/linux32/common/linux32_types.h>
51 #include <compat/linux32/common/linux32_signal.h>
52 #include <compat/linux32/common/linux32_sched.h>
53 #include <compat/linux32/linux32_syscallargs.h>
54 
55 #include <compat/linux/common/linux_ptrace.h>
56 #include <compat/linux/common/linux_types.h>
57 #include <compat/linux/common/linux_emuldata.h>
58 #include <compat/linux/common/linux_signal.h>
59 #include <compat/linux/common/linux_misc.h>
60 #include <compat/linux/common/linux_statfs.h>
61 #include <compat/linux/common/linux_ipc.h>
62 #include <compat/linux/common/linux_sem.h>
63 #include <compat/linux/common/linux_futex.h>
64 #include <compat/linux/linux_syscallargs.h>
65 
66 extern const struct linux_mnttypes linux_fstypes[];
67 extern const int linux_fstypes_cnt;
68 
69 void linux32_to_native_timespec(struct timespec *, struct linux32_timespec *);
70 
71 /*
72  * Implement the fs stat functions. Straightforward.
73  */
74 int
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
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
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
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
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 		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
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
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 linux_sys_futex_args ua;
246 	struct linux32_timespec lts;
247 	struct timespec ts = { 0, 0 };
248 	int error;
249 
250 	NETBSD32TOP_UAP(uaddr, int);
251 	NETBSD32TO64_UAP(op);
252 	NETBSD32TO64_UAP(val);
253 	NETBSD32TOP_UAP(timeout, struct linux_timespec);
254 	NETBSD32TOP_UAP(uaddr2, int);
255 	NETBSD32TO64_UAP(val3);
256 	if ((SCARG(uap, op) & ~LINUX_FUTEX_PRIVATE_FLAG) == LINUX_FUTEX_WAIT &&
257 	    SCARG_P32(uap, timeout) != NULL) {
258 		if ((error = copyin((void *)SCARG_P32(uap, timeout),
259 		    &lts, sizeof(lts))) != 0) {
260 			return error;
261 		}
262 		linux32_to_native_timespec(&ts, &lts);
263 	}
264 	return linux_do_futex(l, &ua, retval, &ts);
265 }
266 
267 int
268 linux32_sys_set_robust_list(struct lwp *l,
269     const struct linux32_sys_set_robust_list_args *uap, register_t *retval)
270 {
271 	/* {
272 		syscallarg(linux32_robust_list_headp_t) head;
273 		syscallarg(linux32_size_t) len;
274 	} */
275 	struct linux_sys_set_robust_list_args ua;
276 	struct linux_emuldata *led;
277 
278 	if (SCARG(uap, len) != 12)
279 		return EINVAL;
280 
281 	NETBSD32TOP_UAP(head, struct robust_list_head);
282 	NETBSD32TOX64_UAP(len, size_t);
283 
284 	led = l->l_emuldata;
285 	led->led_robust_head = SCARG(&ua, head);
286 	*retval = 0;
287 	return 0;
288 }
289 
290 int
291 linux32_sys_get_robust_list(struct lwp *l,
292     const struct linux32_sys_get_robust_list_args *uap, register_t *retval)
293 {
294 	/* {
295 		syscallarg(linux32_robust_list_headpp_t) head;
296 		syscallarg(linux32_sizep_t) len;
297 	} */
298 	struct linux_sys_get_robust_list_args ua;
299 
300 	NETBSD32TOP_UAP(head, struct robust_list_head *);
301 	NETBSD32TOP_UAP(len, size_t *);
302 	return linux_sys_get_robust_list(l, &ua, retval);
303 }
304 
305 int
306 linux32_sys_truncate64(struct lwp *l, const struct linux32_sys_truncate64_args *uap, register_t *retval)
307 {
308 	/* {
309 		syscallarg(netbsd32_charp) path;
310 		syscallarg(off_t) length;
311 	} */
312 	struct sys_truncate_args ua;
313 
314 	/* Linux doesn't have the 'pad' pseudo-parameter */
315 	NETBSD32TOP_UAP(path, const char *);
316 	SCARG(&ua, PAD) = 0;
317 	SCARG(&ua, length) = ((off_t)SCARG(uap, lenhi) << 32) + SCARG(uap, lenlo);
318 	return sys_truncate(l, &ua, retval);
319 }
320 
321 int
322 linux32_sys_ftruncate64(struct lwp *l, const struct linux32_sys_ftruncate64_args *uap, register_t *retval)
323 {
324 	/* {
325 		syscallarg(unsigned int) fd;
326 		syscallarg(off_t) length;
327 	} */
328 	struct sys_ftruncate_args ua;
329 
330 	/* Linux doesn't have the 'pad' pseudo-parameter */
331 	NETBSD32TO64_UAP(fd);
332 	SCARG(&ua, PAD) = 0;
333 	SCARG(&ua, length) = ((off_t)SCARG(uap, lenhi) << 32) + SCARG(uap, lenlo);
334 	return sys_ftruncate(l, &ua, retval);
335 }
336 
337 int
338 linux32_sys_setdomainname(struct lwp *l, const struct linux32_sys_setdomainname_args *uap, register_t *retval)
339 {
340 	/* {
341 		syscallarg(netbsd32_charp) domainname;
342 		syscallarg(int) len;
343 	} */
344 	struct linux_sys_setdomainname_args ua;
345 
346 	NETBSD32TOP_UAP(domainname, char);
347 	NETBSD32TO64_UAP(len);
348 	return linux_sys_setdomainname(l, &ua, retval);
349 }
350