Home | History | Annotate | Line # | Download | only in common
linux32_unistd.c revision 1.25
      1 /*	$NetBSD: linux32_unistd.c,v 1.25 2008/11/19 18:36:04 ad Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      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
     19  *    permission.
     20  *
     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.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 
     36 __KERNEL_RCSID(0, "$NetBSD: linux32_unistd.c,v 1.25 2008/11/19 18:36:04 ad Exp $");
     37 
     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>
     46 #include <sys/proc.h>
     47 #include <sys/ucred.h>
     48 #include <sys/swap.h>
     49 
     50 #include <machine/types.h>
     51 
     52 #include <sys/syscallargs.h>
     53 
     54 #include <compat/netbsd32/netbsd32.h>
     55 #include <compat/netbsd32/netbsd32_conv.h>
     56 
     57 #include <compat/linux/common/linux_types.h>
     58 #include <compat/linux/common/linux_signal.h>
     59 #include <compat/linux/common/linux_machdep.h>
     60 #include <compat/linux/common/linux_misc.h>
     61 #include <compat/linux/common/linux_oldolduname.h>
     62 #include <compat/linux/common/linux_ipc.h>
     63 #include <compat/linux/common/linux_sem.h>
     64 #include <compat/linux/linux_syscallargs.h>
     65 
     66 #include <compat/linux32/common/linux32_types.h>
     67 #include <compat/linux32/common/linux32_signal.h>
     68 #include <compat/linux32/common/linux32_machdep.h>
     69 #include <compat/linux32/common/linux32_sysctl.h>
     70 #include <compat/linux32/common/linux32_socketcall.h>
     71 #include <compat/linux32/linux32_syscallargs.h>
     72 
     73 static int linux32_select1(struct lwp *, register_t *,
     74     int, fd_set *, fd_set *, fd_set *, struct timeval *);
     75 
     76 int
     77 linux32_sys_brk(struct lwp *l, const struct linux32_sys_brk_args *uap, register_t *retval)
     78 {
     79 	/* {
     80 		syscallarg(netbsd32_charp) nsize;
     81 	} */
     82 	struct linux_sys_brk_args ua;
     83 
     84 	NETBSD32TOP_UAP(nsize, char);
     85 	return linux_sys_brk(l, &ua, retval);
     86 }
     87 
     88 int
     89 linux32_sys_llseek(struct lwp *l, const struct linux32_sys_llseek_args *uap, register_t *retval)
     90 {
     91 	/* {
     92 		syscallcarg(int) fd;
     93                 syscallarg(u_int32_t) ohigh;
     94                 syscallarg(u_int32_t) olow;
     95 		syscallarg(netbsd32_caddr_t) res;
     96 		syscallcarg(int) whence;
     97 	} */
     98 	struct linux_sys_llseek_args ua;
     99 
    100 	NETBSD32TO64_UAP(fd);
    101 	NETBSD32TO64_UAP(ohigh);
    102 	NETBSD32TO64_UAP(olow);
    103 	NETBSD32TOP_UAP(res, char);
    104 	NETBSD32TO64_UAP(whence);
    105 
    106 	return linux_sys_llseek(l, &ua, retval);
    107 }
    108 
    109 int
    110 linux32_sys_select(struct lwp *l, const struct linux32_sys_select_args *uap, register_t *retval)
    111 {
    112 	/* {
    113 		syscallarg(int) nfds;
    114 		syscallarg(netbsd32_fd_setp_t) readfds;
    115 		syscallarg(netbsd32_fd_setp_t) writefds;
    116 		syscallarg(netbsd32_fd_setp_t) exceptfds;
    117 		syscallarg(netbsd32_timevalp_t) timeout;
    118 	} */
    119 
    120 	return linux32_select1(l, retval, SCARG(uap, nfds),
    121 	    SCARG_P32(uap, readfds),
    122 	    SCARG_P32(uap, writefds),
    123 	    SCARG_P32(uap, exceptfds),
    124 	    SCARG_P32(uap, timeout));
    125 }
    126 
    127 int
    128 linux32_sys_oldselect(struct lwp *l, const struct linux32_sys_oldselect_args *uap, register_t *retval)
    129 {
    130 	/* {
    131 		syscallarg(linux32_oldselectp_t) lsp;
    132 	} */
    133 	struct linux32_oldselect lsp32;
    134 	int error;
    135 
    136 	if ((error = copyin(SCARG_P32(uap, lsp), &lsp32, sizeof(lsp32))) != 0)
    137 		return error;
    138 
    139 	return linux32_select1(l, retval, lsp32.nfds,
    140 	     NETBSD32PTR64(lsp32.readfds), NETBSD32PTR64(lsp32.writefds),
    141 	     NETBSD32PTR64(lsp32.exceptfds), NETBSD32PTR64(lsp32.timeout));
    142 }
    143 
    144 static int
    145 linux32_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
    146         struct lwp *l;
    147         register_t *retval;
    148         int nfds;
    149         fd_set *readfds, *writefds, *exceptfds;
    150         struct timeval *timeout;
    151 {
    152 	struct timeval tv0, tv1, utv, *tv = NULL;
    153 	struct netbsd32_timeval utv32;
    154 	int error;
    155 
    156 	timerclear(&utv); /* XXX GCC4 */
    157 
    158 	/*
    159 	 * Store current time for computation of the amount of
    160 	 * time left.
    161 	 */
    162 	if (timeout) {
    163 		if ((error = copyin(timeout, &utv32, sizeof(utv32))))
    164 			return error;
    165 
    166 		netbsd32_to_timeval(&utv32, &utv);
    167 
    168 		if (itimerfix(&utv)) {
    169 			/*
    170 			 * The timeval was invalid.  Convert it to something
    171 			 * valid that will act as it does under Linux.
    172 			 */
    173 			utv.tv_sec += utv.tv_usec / 1000000;
    174 			utv.tv_usec %= 1000000;
    175 			if (utv.tv_usec < 0) {
    176 				utv.tv_sec -= 1;
    177 				utv.tv_usec += 1000000;
    178 			}
    179 			if (utv.tv_sec < 0)
    180 				timerclear(&utv);
    181 		}
    182 		microtime(&tv0);
    183 		tv = &utv;
    184 	}
    185 
    186 	error = selcommon(l, retval, nfds,
    187 	    readfds, writefds, exceptfds, tv, NULL);
    188 
    189 	if (error) {
    190 		/*
    191 		 * See fs/select.c in the Linux kernel.  Without this,
    192 		 * Maelstrom doesn't work.
    193 		 */
    194 		if (error == ERESTART)
    195 			error = EINTR;
    196 		return error;
    197 	}
    198 
    199 	if (timeout) {
    200 		if (*retval) {
    201 			/*
    202 			 * Compute how much time was left of the timeout,
    203 			 * by subtracting the current time and the time
    204 			 * before we started the call, and subtracting
    205 			 * that result from the user-supplied value.
    206 			 */
    207 			microtime(&tv1);
    208 			timersub(&tv1, &tv0, &tv1);
    209 			timersub(&utv, &tv1, &utv);
    210 			if (utv.tv_sec < 0)
    211 				timerclear(&utv);
    212 		} else {
    213 			timerclear(&utv);
    214 		}
    215 
    216 		netbsd32_from_timeval(&utv, &utv32);
    217 
    218 		if ((error = copyout(&utv32, timeout, sizeof(utv32))))
    219 			return error;
    220 	}
    221 
    222 	return 0;
    223 }
    224 
    225 int
    226 linux32_sys_pipe(struct lwp *l, const struct linux32_sys_pipe_args *uap, register_t *retval)
    227 {
    228 	/* {
    229 		syscallarg(netbsd32_intp) fd;
    230 	} */
    231 	int error;
    232 	int pfds[2];
    233 
    234 	if ((error = sys_pipe(l, 0, retval)))
    235 		return error;
    236 
    237 	pfds[0] = (int)retval[0];
    238 	pfds[1] = (int)retval[1];
    239 
    240 	if ((error = copyout(pfds, SCARG_P32(uap, fd), 2 * sizeof (int))) != 0)
    241 		return error;
    242 
    243 	retval[0] = 0;
    244 	retval[1] = 0;
    245 
    246 	return 0;
    247 }
    248 
    249 
    250 int
    251 linux32_sys_unlink(struct lwp *l, const struct linux32_sys_unlink_args *uap, register_t *retval)
    252 {
    253 	/* {
    254 		syscallarg(const netbsd32_charp) path;
    255 	} */
    256 	struct linux_sys_unlink_args ua;
    257 
    258 	NETBSD32TOP_UAP(path, const char);
    259 
    260 	return linux_sys_unlink(l, &ua, retval);
    261 }
    262 
    263 int
    264 linux32_sys_creat(struct lwp *l, const struct linux32_sys_creat_args *uap, register_t *retval)
    265 {
    266 	/* {
    267 		syscallarg(const netbsd32_charp) path;
    268 		syscallarg(int) mode;
    269 	} */
    270 	struct sys_open_args ua;
    271 
    272 	NETBSD32TOP_UAP(path, const char);
    273 	SCARG(&ua, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    274 	NETBSD32TO64_UAP(mode);
    275 
    276 	return sys_open(l, &ua, retval);
    277 }
    278 
    279 int
    280 linux32_sys_mknod(struct lwp *l, const struct linux32_sys_mknod_args *uap, register_t *retval)
    281 {
    282 	/* {
    283 		syscallarg(const netbsd32_charp) path;
    284 		syscallarg(int) mode;
    285 		syscallarg(int) dev;
    286 	} */
    287 	struct linux_sys_mknod_args ua;
    288 
    289 	NETBSD32TOP_UAP(path, const char);
    290 	NETBSD32TO64_UAP(mode);
    291 	NETBSD32TO64_UAP(dev);
    292 
    293 	return linux_sys_mknod(l, &ua, retval);
    294 }
    295 
    296 int
    297 linux32_sys_break(struct lwp *l, const struct linux32_sys_break_args *uap, register_t *retval)
    298 {
    299 #if 0
    300 	/* {
    301 		syscallarg(const netbsd32_charp) nsize;
    302 	} */
    303 #endif
    304 
    305 	return ENOSYS;
    306 }
    307 
    308 int
    309 linux32_sys_swapon(struct lwp *l, const struct linux32_sys_swapon_args *uap, register_t *retval)
    310 {
    311 	/* {
    312 		syscallarg(const netbsd32_charp) name;
    313 	} */
    314 	struct sys_swapctl_args ua;
    315 
    316         SCARG(&ua, cmd) = SWAP_ON;
    317         SCARG(&ua, arg) = SCARG_P32(uap, name);
    318         SCARG(&ua, misc) = 0;   /* priority */
    319         return (sys_swapctl(l, &ua, retval));
    320 }
    321 
    322 int
    323 linux32_sys_swapoff(struct lwp *l, const struct linux32_sys_swapoff_args *uap, register_t *retval)
    324 {
    325 	/* {
    326 		syscallarg(const netbsd32_charp) path;
    327 	} */
    328 	struct sys_swapctl_args ua;
    329 
    330         SCARG(&ua, cmd) = SWAP_OFF;
    331         SCARG(&ua, arg) = SCARG_P32(uap, path);
    332         SCARG(&ua, misc) = 0;   /* priority */
    333         return (sys_swapctl(l, &ua, retval));
    334 }
    335 
    336 
    337 int
    338 linux32_sys_reboot(struct lwp *l, const struct linux32_sys_reboot_args *uap, register_t *retval)
    339 {
    340 	/* {
    341 		syscallarg(int) magic1;
    342 		syscallarg(int) magic2;
    343 		syscallarg(int) cmd;
    344 		syscallarg(netbsd32_voidp) arg;
    345 	} */
    346 	struct linux_sys_reboot_args ua;
    347 
    348 	NETBSD32TO64_UAP(magic1);
    349 	NETBSD32TO64_UAP(magic2);
    350 	NETBSD32TO64_UAP(cmd);
    351 	NETBSD32TOP_UAP(arg, void);
    352 
    353 	return linux_sys_reboot(l, &ua, retval);
    354 }
    355 
    356 int
    357 linux32_sys_setresuid(struct lwp *l, const struct linux32_sys_setresuid_args *uap, register_t *retval)
    358 {
    359 	/* {
    360 		syscallarg(uid_t) ruid;
    361 		syscallarg(uid_t) euid;
    362 		syscallarg(uid_t) suid;
    363 	} */
    364 	struct linux_sys_setresuid_args ua;
    365 
    366 	SCARG(&ua, ruid) = (SCARG(uap, ruid) == -1) ? -1 : SCARG(uap, ruid);
    367 	SCARG(&ua, euid) = (SCARG(uap, euid) == -1) ? -1 : SCARG(uap, euid);
    368 	SCARG(&ua, suid) = (SCARG(uap, suid) == -1) ? -1 : SCARG(uap, suid);
    369 
    370 	return linux_sys_setresuid(l, &ua, retval);
    371 }
    372 
    373 int
    374 linux32_sys_setresgid(struct lwp *l, const struct linux32_sys_setresgid_args *uap, register_t *retval)
    375 {
    376 	/* {
    377 		syscallarg(gid_t) rgid;
    378 		syscallarg(gid_t) egid;
    379 		syscallarg(gid_t) sgid;
    380 	} */
    381 	struct linux_sys_setresgid_args ua;
    382 
    383 	SCARG(&ua, rgid) = (SCARG(uap, rgid) == -1) ? -1 : SCARG(uap, rgid);
    384 	SCARG(&ua, egid) = (SCARG(uap, egid) == -1) ? -1 : SCARG(uap, egid);
    385 	SCARG(&ua, sgid) = (SCARG(uap, sgid) == -1) ? -1 : SCARG(uap, sgid);
    386 
    387 	return linux_sys_setresgid(l, &ua, retval);
    388 }
    389 
    390 int
    391 linux32_sys_nice(struct lwp *l, const struct linux32_sys_nice_args *uap, register_t *retval)
    392 {
    393 	/* {
    394 		syscallarg(int) incr;
    395 	} */
    396 	struct proc *p = l->l_proc;
    397 	struct sys_setpriority_args bsa;
    398 
    399 	SCARG(&bsa, which) = PRIO_PROCESS;
    400 	SCARG(&bsa, who) = 0;
    401 	SCARG(&bsa, prio) = p->p_nice - NZERO + SCARG(uap, incr);
    402 
    403 	return sys_setpriority(l, &bsa, retval);
    404 }
    405 
    406 int
    407 linux32_sys_alarm(struct lwp *l, const struct linux32_sys_alarm_args *uap, register_t *retval)
    408 {
    409 	/* {
    410 		syscallarg(unsigned int) secs;
    411 	} */
    412 	struct linux_sys_alarm_args ua;
    413 
    414 	NETBSD32TO64_UAP(secs);
    415 
    416 	return linux_sys_alarm(l, &ua, retval);
    417 }
    418 
    419 int
    420 linux32_sys_fdatasync(struct lwp *l, const struct linux32_sys_fdatasync_args *uap, register_t *retval)
    421 {
    422 	/* {
    423 		syscallarg(int) fd;
    424 	} */
    425 	struct linux_sys_fdatasync_args ua;
    426 
    427 	NETBSD32TO64_UAP(fd);
    428 
    429 	return linux_sys_fdatasync(l, &ua, retval);
    430 }
    431 
    432 int
    433 linux32_sys_setfsuid(struct lwp *l, const struct linux32_sys_setfsuid_args *uap, register_t *retval)
    434 {
    435 	/* {
    436 		syscallarg(uid_t) uid;
    437 	} */
    438 	struct linux_sys_setfsuid_args ua;
    439 
    440 	NETBSD32TO64_UAP(uid);
    441 
    442 	return linux_sys_setfsuid(l, &ua, retval);
    443 }
    444 
    445 int
    446 linux32_sys_setfsgid(struct lwp *l, const struct linux32_sys_setfsgid_args *uap, register_t *retval)
    447 {
    448 	/* {
    449 		syscallarg(gid_t) gid;
    450 	} */
    451 	struct linux_sys_setfsgid_args ua;
    452 
    453 	NETBSD32TO64_UAP(gid);
    454 
    455 	return linux_sys_setfsgid(l, &ua, retval);
    456 }
    457 
    458 /*
    459  * pread(2).
    460  */
    461 int
    462 linux32_sys_pread(struct lwp *l,
    463     const struct linux32_sys_pread_args *uap, register_t *retval)
    464 {
    465 	/* {
    466 		syscallarg(int) fd;
    467 		syscallarg(netbsd32_voidp) buf;
    468 		syscallarg(netbsd32_size_t) nbyte;
    469 		syscallarg(linux32_off_t) offset;
    470 	} */
    471 	struct sys_pread_args pra;
    472 
    473 	SCARG(&pra, fd) = SCARG(uap, fd);
    474 	SCARG(&pra, buf) = SCARG_P32(uap, buf);
    475 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    476 	SCARG(&pra, offset) = SCARG(uap, offset);
    477 
    478 	return sys_pread(l, &pra, retval);
    479 }
    480 
    481 /*
    482  * pwrite(2).
    483  */
    484 int
    485 linux32_sys_pwrite(struct lwp *l,
    486     const struct linux32_sys_pwrite_args *uap, register_t *retval)
    487 {
    488 	/* {
    489 		syscallarg(int) fd;
    490 		syscallarg(const netbsd32_voidp) buf;
    491 		syscallarg(netbsd32_size_t) nbyte;
    492 		syscallarg(linux32_off_t) offset;
    493 	} */
    494 	struct sys_pwrite_args pra;
    495 
    496 	SCARG(&pra, fd) = SCARG(uap, fd);
    497 	SCARG(&pra, buf) = SCARG_P32(uap, buf);
    498 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    499 	SCARG(&pra, offset) = SCARG(uap, offset);
    500 
    501 	return sys_pwrite(l, &pra, retval);
    502 }
    503 
    504