1 /* $NetBSD: linux32_unistd.c,v 1.20 2008/02/02 22:57:05 dsl Exp $ */
4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Emmanuel Dreyfus
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior written
21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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.
34 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: linux32_unistd.c,v 1.20 2008/02/02 22:57:05 dsl Exp $");
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/fstypes.h>
41 #include <sys/signal.h>
42 #include <sys/dirent.h>
43 #include <sys/kernel.h>
44 #include <sys/fcntl.h>
45 #include <sys/select.h>
47 #include <sys/ucred.h>
50 #include <machine/types.h>
52 #include <sys/syscallargs.h>
54 #include <compat/netbsd32/netbsd32.h>
55 #include <compat/netbsd32/netbsd32_conv.h>
56 #include <compat/netbsd32/netbsd32_syscallargs.h>
58 #include <compat/linux/common/linux_types.h>
59 #include <compat/linux/common/linux_signal.h>
60 #include <compat/linux/common/linux_machdep.h>
61 #include <compat/linux/common/linux_misc.h>
62 #include <compat/linux/common/linux_oldolduname.h>
63 #include <compat/linux/linux_syscallargs.h>
65 #include <compat/linux32/common/linux32_types.h>
66 #include <compat/linux32/common/linux32_signal.h>
67 #include <compat/linux32/common/linux32_machdep.h>
68 #include <compat/linux32/common/linux32_sysctl.h>
69 #include <compat/linux32/common/linux32_socketcall.h>
70 #include <compat/linux32/linux32_syscallargs.h>
72 static int linux32_select1(struct lwp
*, register_t
*,
73 int, fd_set
*, fd_set
*, fd_set
*, struct timeval
*);
76 linux32_sys_brk(struct lwp
*l
, const struct linux32_sys_brk_args
*uap
, register_t
*retval
)
79 syscallarg(netbsd32_charp) nsize;
81 struct linux_sys_brk_args ua
;
83 NETBSD32TOP_UAP(nsize
, char);
84 return linux_sys_brk(l
, &ua
, retval
);
88 linux32_sys_llseek(struct lwp
*l
, const struct linux32_sys_llseek_args
*uap
, register_t
*retval
)
92 syscallarg(u_int32_t) ohigh;
93 syscallarg(u_int32_t) olow;
94 syscallarg(netbsd32_caddr_t) res;
95 syscallcarg(int) whence;
97 struct linux_sys_llseek_args ua
;
100 NETBSD32TO64_UAP(ohigh
);
101 NETBSD32TO64_UAP(olow
);
102 NETBSD32TOP_UAP(res
, char);
103 NETBSD32TO64_UAP(whence
);
105 return linux_sys_llseek(l
, &ua
, retval
);
109 linux32_sys_select(struct lwp
*l
, const struct linux32_sys_select_args
*uap
, register_t
*retval
)
112 syscallarg(int) nfds;
113 syscallarg(netbsd32_fd_setp_t) readfds;
114 syscallarg(netbsd32_fd_setp_t) writefds;
115 syscallarg(netbsd32_fd_setp_t) exceptfds;
116 syscallarg(netbsd32_timevalp_t) timeout;
119 return linux32_select1(l
, retval
, SCARG(uap
, nfds
),
120 SCARG_P32(uap
, readfds
),
121 SCARG_P32(uap
, writefds
),
122 SCARG_P32(uap
, exceptfds
),
123 SCARG_P32(uap
, timeout
));
127 linux32_sys_oldselect(struct lwp
*l
, const struct linux32_sys_oldselect_args
*uap
, register_t
*retval
)
130 syscallarg(linux32_oldselectp_t) lsp;
132 struct linux32_oldselect lsp32
;
135 if ((error
= copyin(SCARG_P32(uap
, lsp
), &lsp32
, sizeof(lsp32
))) != 0)
138 return linux32_select1(l
, retval
, lsp32
.nfds
,
139 NETBSD32PTR64(lsp32
.readfds
), NETBSD32PTR64(lsp32
.writefds
),
140 NETBSD32PTR64(lsp32
.exceptfds
), NETBSD32PTR64(lsp32
.timeout
));
144 linux32_select1(l
, retval
, nfds
, readfds
, writefds
, exceptfds
, timeout
)
148 fd_set
*readfds
, *writefds
, *exceptfds
;
149 struct timeval
*timeout
;
151 struct timeval tv0
, tv1
, utv
, *tv
= NULL
;
152 struct netbsd32_timeval utv32
;
155 timerclear(&utv
); /* XXX GCC4 */
158 * Store current time for computation of the amount of
162 if ((error
= copyin(timeout
, &utv32
, sizeof(utv32
))))
165 netbsd32_to_timeval(&utv32
, &utv
);
167 if (itimerfix(&utv
)) {
169 * The timeval was invalid. Convert it to something
170 * valid that will act as it does under Linux.
172 utv
.tv_sec
+= utv
.tv_usec
/ 1000000;
173 utv
.tv_usec
%= 1000000;
174 if (utv
.tv_usec
< 0) {
176 utv
.tv_usec
+= 1000000;
185 error
= selcommon(l
, retval
, nfds
,
186 readfds
, writefds
, exceptfds
, tv
, NULL
);
190 * See fs/select.c in the Linux kernel. Without this,
191 * Maelstrom doesn't work.
193 if (error
== ERESTART
)
201 * Compute how much time was left of the timeout,
202 * by subtracting the current time and the time
203 * before we started the call, and subtracting
204 * that result from the user-supplied value.
207 timersub(&tv1
, &tv0
, &tv1
);
208 timersub(&utv
, &tv1
, &utv
);
215 netbsd32_from_timeval(&utv
, &utv32
);
217 if ((error
= copyout(&utv32
, timeout
, sizeof(utv32
))))
225 linux32_sys_pipe(struct lwp
*l
, const struct linux32_sys_pipe_args
*uap
, register_t
*retval
)
228 syscallarg(netbsd32_intp) fd;
233 if ((error
= sys_pipe(l
, 0, retval
)))
236 pfds
[0] = (int)retval
[0];
237 pfds
[1] = (int)retval
[1];
239 if ((error
= copyout(pfds
, SCARG_P32(uap
, fd
), 2 * sizeof (int))) != 0)
250 linux32_sys_unlink(struct lwp
*l
, const struct linux32_sys_unlink_args
*uap
, register_t
*retval
)
253 syscallarg(const netbsd32_charp) path;
255 struct linux_sys_unlink_args ua
;
257 NETBSD32TOP_UAP(path
, const char);
259 return linux_sys_unlink(l
, &ua
, retval
);
263 linux32_sys_creat(struct lwp
*l
, const struct linux32_sys_creat_args
*uap
, register_t
*retval
)
266 syscallarg(const netbsd32_charp) path;
267 syscallarg(int) mode;
269 struct sys_open_args ua
;
271 NETBSD32TOP_UAP(path
, const char);
272 SCARG(&ua
, flags
) = O_CREAT
| O_TRUNC
| O_WRONLY
;
273 NETBSD32TO64_UAP(mode
);
275 return sys_open(l
, &ua
, retval
);
279 linux32_sys_mknod(struct lwp
*l
, const struct linux32_sys_mknod_args
*uap
, register_t
*retval
)
282 syscallarg(const netbsd32_charp) path;
283 syscallarg(int) mode;
286 struct linux_sys_mknod_args ua
;
288 NETBSD32TOP_UAP(path
, const char);
289 NETBSD32TO64_UAP(mode
);
290 NETBSD32TO64_UAP(dev
);
292 return linux_sys_mknod(l
, &ua
, retval
);
296 linux32_sys_break(struct lwp
*l
, const struct linux32_sys_break_args
*uap
, register_t
*retval
)
300 syscallarg(const netbsd32_charp) nsize;
308 linux32_sys_rename(struct lwp
*l
, const struct linux32_sys_rename_args
*uap
, register_t
*retval
)
311 syscallarg(const netbsd32_charp) from;
312 syscallarg(const netbsd32_charp) to;
314 struct sys___posix_rename_args ua
;
316 NETBSD32TOP_UAP(from
, const char);
317 NETBSD32TOP_UAP(to
, const char);
319 return sys___posix_rename(l
, &ua
, retval
);
323 linux32_sys_swapon(struct lwp
*l
, const struct linux32_sys_swapon_args
*uap
, register_t
*retval
)
326 syscallarg(const netbsd32_charp) name;
328 struct sys_swapctl_args ua
;
330 SCARG(&ua
, cmd
) = SWAP_ON
;
331 SCARG(&ua
, arg
) = SCARG_P32(uap
, name
);
332 SCARG(&ua
, misc
) = 0; /* priority */
333 return (sys_swapctl(l
, &ua
, retval
));
337 linux32_sys_swapoff(struct lwp
*l
, const struct linux32_sys_swapoff_args
*uap
, register_t
*retval
)
340 syscallarg(const netbsd32_charp) path;
342 struct sys_swapctl_args ua
;
344 SCARG(&ua
, cmd
) = SWAP_OFF
;
345 SCARG(&ua
, arg
) = SCARG_P32(uap
, path
);
346 SCARG(&ua
, misc
) = 0; /* priority */
347 return (sys_swapctl(l
, &ua
, retval
));
352 linux32_sys_reboot(struct lwp
*l
, const struct linux32_sys_reboot_args
*uap
, register_t
*retval
)
355 syscallarg(int) magic1;
356 syscallarg(int) magic2;
358 syscallarg(netbsd32_voidp) arg;
360 struct linux_sys_reboot_args ua
;
362 NETBSD32TO64_UAP(magic1
);
363 NETBSD32TO64_UAP(magic2
);
364 NETBSD32TO64_UAP(cmd
);
365 NETBSD32TOP_UAP(arg
, void);
367 return linux_sys_reboot(l
, &ua
, retval
);
371 linux32_sys_truncate(struct lwp
*l
, const struct linux32_sys_truncate_args
*uap
, register_t
*retval
)
374 syscallarg(const netbsd32_charp) path;
375 syscallarg(netbsd32_charp) buf;
376 syscallarg(int) count;
378 struct compat_43_sys_truncate_args ua
;
380 NETBSD32TOP_UAP(path
, const char);
381 NETBSD32TO64_UAP(length
);
383 return compat_43_sys_truncate(l
, &ua
, retval
);
387 linux32_sys_setresuid(struct lwp
*l
, const struct linux32_sys_setresuid_args
*uap
, register_t
*retval
)
390 syscallarg(uid_t) ruid;
391 syscallarg(uid_t) euid;
392 syscallarg(uid_t) suid;
394 struct linux_sys_setresuid_args ua
;
396 SCARG(&ua
, ruid
) = (SCARG(uap
, ruid
) == -1) ? -1 : SCARG(uap
, ruid
);
397 SCARG(&ua
, euid
) = (SCARG(uap
, euid
) == -1) ? -1 : SCARG(uap
, euid
);
398 SCARG(&ua
, suid
) = (SCARG(uap
, suid
) == -1) ? -1 : SCARG(uap
, suid
);
400 return linux_sys_setresuid(l
, &ua
, retval
);
404 linux32_sys_setresgid(struct lwp
*l
, const struct linux32_sys_setresgid_args
*uap
, register_t
*retval
)
407 syscallarg(gid_t) rgid;
408 syscallarg(gid_t) egid;
409 syscallarg(gid_t) sgid;
411 struct linux_sys_setresgid_args ua
;
413 SCARG(&ua
, rgid
) = (SCARG(uap
, rgid
) == -1) ? -1 : SCARG(uap
, rgid
);
414 SCARG(&ua
, egid
) = (SCARG(uap
, egid
) == -1) ? -1 : SCARG(uap
, egid
);
415 SCARG(&ua
, sgid
) = (SCARG(uap
, sgid
) == -1) ? -1 : SCARG(uap
, sgid
);
417 return linux_sys_setresgid(l
, &ua
, retval
);
421 linux32_sys_nice(struct lwp
*l
, const struct linux32_sys_nice_args
*uap
, register_t
*retval
)
424 syscallarg(int) incr;
426 struct netbsd32_setpriority_args bsa
;
428 SCARG(&bsa
, which
) = PRIO_PROCESS
;
429 SCARG(&bsa
, who
) = 0;
430 SCARG(&bsa
, prio
) = SCARG(uap
, incr
);
432 return netbsd32_setpriority(l
, &bsa
, retval
);
436 linux32_sys_alarm(struct lwp
*l
, const struct linux32_sys_alarm_args
*uap
, register_t
*retval
)
439 syscallarg(unsigned int) secs;
441 struct linux_sys_alarm_args ua
;
443 NETBSD32TO64_UAP(secs
);
445 return linux_sys_alarm(l
, &ua
, retval
);
449 linux32_sys_fdatasync(struct lwp
*l
, const struct linux32_sys_fdatasync_args
*uap
, register_t
*retval
)
454 struct linux_sys_fdatasync_args ua
;
456 NETBSD32TO64_UAP(fd
);
458 return linux_sys_fdatasync(l
, &ua
, retval
);
462 linux32_sys_setfsuid(struct lwp
*l
, const struct linux32_sys_setfsuid_args
*uap
, register_t
*retval
)
465 syscallarg(uid_t) uid;
467 struct linux_sys_setfsuid_args ua
;
469 NETBSD32TO64_UAP(uid
);
471 return linux_sys_setfsuid(l
, &ua
, retval
);
475 linux32_sys_setfsgid(struct lwp
*l
, const struct linux32_sys_setfsgid_args
*uap
, register_t
*retval
)
478 syscallarg(gid_t) gid;
480 struct linux_sys_setfsgid_args ua
;
482 NETBSD32TO64_UAP(gid
);
484 return linux_sys_setfsgid(l
, &ua
, retval
);
491 linux32_sys_pread(struct lwp
*l
,
492 const struct linux32_sys_pread_args
*uap
, register_t
*retval
)
496 syscallarg(netbsd32_voidp) buf;
497 syscallarg(netbsd32_size_t) nbyte;
498 syscallarg(linux32_off_t) offset;
500 struct sys_pread_args pra
;
502 SCARG(&pra
, fd
) = SCARG(uap
, fd
);
503 SCARG(&pra
, buf
) = SCARG_P32(uap
, buf
);
504 SCARG(&pra
, nbyte
) = SCARG(uap
, nbyte
);
505 SCARG(&pra
, offset
) = SCARG(uap
, offset
);
507 return sys_pread(l
, &pra
, retval
);
514 linux32_sys_pwrite(struct lwp
*l
,
515 const struct linux32_sys_pwrite_args
*uap
, register_t
*retval
)
519 syscallarg(const netbsd32_voidp) buf;
520 syscallarg(netbsd32_size_t) nbyte;
521 syscallarg(linux32_off_t) offset;
523 struct sys_pwrite_args pra
;
525 SCARG(&pra
, fd
) = SCARG(uap
, fd
);
526 SCARG(&pra
, buf
) = SCARG_P32(uap
, buf
);
527 SCARG(&pra
, nbyte
) = SCARG(uap
, nbyte
);
528 SCARG(&pra
, offset
) = SCARG(uap
, offset
);
530 return sys_pwrite(l
, &pra
, retval
);