Home | History | Annotate | Line # | Download | only in common
linux32_misc.c revision 1.1.8.1
      1 /*	$NetBSD: linux32_misc.c,v 1.1.8.1 2006/05/24 15:48:27 tron 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_misc.c,v 1.1.8.1 2006/05/24 15:48:27 tron 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/sa.h>
     47 #include <sys/proc.h>
     48 #include <sys/ucred.h>
     49 #include <sys/swap.h>
     50 
     51 #include <machine/types.h>
     52 
     53 #include <sys/syscallargs.h>
     54 
     55 #include <compat/netbsd32/netbsd32.h>
     56 #include <compat/netbsd32/netbsd32_conv.h>
     57 #include <compat/netbsd32/netbsd32_syscallargs.h>
     58 
     59 #include <compat/linux/common/linux_types.h>
     60 #include <compat/linux/common/linux_signal.h>
     61 #include <compat/linux/common/linux_machdep.h>
     62 #include <compat/linux/common/linux_misc.h>
     63 #include <compat/linux/common/linux_limit.h>
     64 #include <compat/linux/common/linux_oldolduname.h>
     65 #include <compat/linux/linux_syscallargs.h>
     66 
     67 #include <compat/linux32/common/linux32_types.h>
     68 #include <compat/linux32/common/linux32_signal.h>
     69 #include <compat/linux32/common/linux32_machdep.h>
     70 #include <compat/linux32/common/linux32_sysctl.h>
     71 #include <compat/linux32/common/linux32_socketcall.h>
     72 #include <compat/linux32/linux32_syscallargs.h>
     73 
     74 static int linux32_select1(struct lwp *, register_t *,
     75     int, fd_set *, fd_set *, fd_set *, struct timeval *);
     76 
     77 int
     78 linux32_sys_uname(l, v, retval)
     79 	struct lwp *l;
     80 	void *v;
     81 	register_t *retval;
     82 {
     83 	struct linux32_sys_uname_args /* {
     84 		syscallarg(linux32_utsnamep) up;
     85 	} */ *uap = v;
     86 	struct linux_utsname luts;
     87 	struct linux_utsname *lp;
     88 
     89 	strncpy(luts.l_sysname, linux32_sysname, sizeof(luts.l_sysname));
     90 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
     91 	strncpy(luts.l_release, linux32_release, sizeof(luts.l_release));
     92 	strncpy(luts.l_version, linux32_version, sizeof(luts.l_version));
     93 #ifdef LINUX_UNAME_ARCH
     94 	strncpy(luts.l_machine, LINUX_UNAME_ARCH, sizeof(luts.l_machine));
     95 #else
     96 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
     97 #endif
     98 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
     99 
    100 	lp = (struct linux_utsname *)NETBSD32PTR64(SCARG(uap, up));
    101 
    102         return copyout(&luts, lp, sizeof(luts));
    103 }
    104 
    105 int
    106 linux32_sys_open(l, v, retval)
    107 	struct lwp *l;
    108 	void *v;
    109 	register_t *retval;
    110 {
    111 	struct linux32_sys_open_args /* {
    112 		syscallarg(const netbsd32_charp) path;
    113 		syscallarg(int) flags;
    114 		syscallarg(int) mode;
    115 	} */ *uap = v;
    116 	struct linux_sys_open_args ua;
    117 
    118 	NETBSD32TOP_UAP(path, const char);
    119 	NETBSD32TO64_UAP(flags);
    120 	NETBSD32TO64_UAP(mode);
    121 
    122 	return linux_sys_open(l, &ua, retval);
    123 }
    124 
    125 
    126 int
    127 linux32_sys_brk(l, v, retval)
    128 	struct lwp *l;
    129 	void *v;
    130 	register_t *retval;
    131 {
    132 	struct linux32_sys_brk_args /* {
    133 		syscallarg(netbsd32_charp) nsize;
    134 	} */ *uap = v;
    135 	struct linux_sys_brk_args ua;
    136 
    137 	NETBSD32TOP_UAP(nsize, char);
    138 	return linux_sys_brk(l, &ua, retval);
    139 }
    140 
    141 int
    142 linux32_sys_old_mmap(l, v, retval)
    143 	struct lwp *l;
    144 	void *v;
    145 	register_t *retval;
    146 {
    147 	struct linux32_sys_old_mmap_args /* {
    148 		syscallarg(linux32_oldmmapp) lmp;
    149 	} */ *uap = v;
    150 	struct linux_sys_old_mmap_args ua;
    151 
    152 	NETBSD32TOP_UAP(lmp, struct linux_oldmmap);
    153 	return linux_sys_old_mmap(l, &ua, retval);
    154 
    155 	return 0;
    156 }
    157 
    158 int
    159 linux32_sys_mprotect(l, v, retval)
    160 	struct lwp *l;
    161 	void *v;
    162 	register_t *retval;
    163 {
    164 	struct linux32_sys_mprotect_args /* {
    165 		syscallarg(netbsd32_voidp) addr;
    166 		syscallarg(netbsd32_long) len;
    167 		syscallarg(int) prot;
    168 	} */ *uap = v;
    169 	struct sys_mprotect_args ua;
    170 
    171 	NETBSD32TOP_UAP(addr, void);
    172 	NETBSD32TOX_UAP(len, long);
    173 	NETBSD32TO64_UAP(prot);
    174 	return (linux_sys_mprotect(l, &ua, retval));
    175 }
    176 
    177 int
    178 linux32_sys_stat64(l, v, retval)
    179 	struct lwp *l;
    180 	void *v;
    181 	register_t *retval;
    182 {
    183 	struct linux32_sys_stat64_args /* {
    184 	        syscallarg(netbsd32_charp) path;
    185 	        syscallarg(linux32_statp) sp;
    186 	} */ *uap = v;
    187 	struct sys___stat30_args ua;
    188 	caddr_t sg = stackgap_init(l->l_proc, 0);
    189 	int error;
    190 	struct stat st;
    191 	struct linux32_stat64 st32;
    192 	struct stat *stp;
    193 	struct linux32_stat64 *st32p;
    194 
    195 	NETBSD32TOP_UAP(path, const char);
    196 
    197 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
    198 
    199 	stp = stackgap_alloc(l->l_proc, &sg, sizeof(*stp));
    200 	SCARG(&ua, ub) = stp;
    201 
    202 	if ((error = sys___stat30(l, &ua, retval)) != 0)
    203 		return error;
    204 
    205 	if ((error = copyin(stp, &st, sizeof(st))) != 0)
    206 		return error;
    207 
    208 	linux32_from_stat(&st, &st32);
    209 
    210 	st32p = (struct linux32_stat64 *)(long)SCARG(uap, sp);
    211 
    212 	if ((error = copyout(&st32, st32p, sizeof(*st32p))) != 0)
    213 		return error;
    214 
    215 	return 0;
    216 }
    217 
    218 int
    219 linux32_sys_lstat64(l, v, retval)
    220 	struct lwp *l;
    221 	void *v;
    222 	register_t *retval;
    223 {
    224 	struct linux32_sys_lstat64_args /* {
    225 	        syscallarg(netbsd32_charp) path;
    226 	        syscallarg(linux32_stat64p) sp;
    227 	} */ *uap = v;
    228 	struct sys___lstat30_args ua;
    229 	caddr_t sg = stackgap_init(l->l_proc, 0);
    230 	int error;
    231 	struct stat st;
    232 	struct linux32_stat64 st32;
    233 	struct stat *stp;
    234 	struct linux32_stat64 *st32p;
    235 
    236 	NETBSD32TOP_UAP(path, const char);
    237 
    238 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
    239 
    240 	stp = stackgap_alloc(l->l_proc, &sg, sizeof(*stp));
    241 	SCARG(&ua, ub) = stp;
    242 
    243 	if ((error = sys___lstat30(l, &ua, retval)) != 0)
    244 		return error;
    245 
    246 	if ((error = copyin(stp, &st, sizeof(st))) != 0)
    247 		return error;
    248 
    249 	linux32_from_stat(&st, &st32);
    250 
    251 	st32p = (struct linux32_stat64 *)(long)SCARG(uap, sp);
    252 
    253 	if ((error = copyout(&st32, st32p, sizeof(*st32p))) != 0)
    254 		return error;
    255 
    256 	return 0;
    257 }
    258 
    259 int
    260 linux32_sys_fstat64(l, v, retval)
    261 	struct lwp *l;
    262 	void *v;
    263 	register_t *retval;
    264 {
    265 	struct linux32_sys_fstat64_args /* {
    266 	        syscallarg(int) fd;
    267 	        syscallarg(linux32_stat64p) sp;
    268 	} */ *uap = v;
    269 	struct sys___fstat30_args ua;
    270 	caddr_t sg = stackgap_init(l->l_proc, 0);
    271 	int error;
    272 	struct stat st;
    273 	struct linux32_stat64 st32;
    274 	struct stat *stp;
    275 	struct linux32_stat64 *st32p;
    276 
    277 	NETBSD32TO64_UAP(fd);
    278 
    279 	stp = stackgap_alloc(l->l_proc, &sg, sizeof(*stp));
    280 	SCARG(&ua, sb) = stp;
    281 
    282 	if ((error = sys___fstat30(l, &ua, retval)) != 0)
    283 		return error;
    284 
    285 	if ((error = copyin(stp, &st, sizeof(st))) != 0)
    286 		return error;
    287 
    288 	linux32_from_stat(&st, &st32);
    289 
    290 	st32p = (struct linux32_stat64 *)(long)SCARG(uap, sp);
    291 
    292 	if ((error = copyout(&st32, st32p, sizeof(*st32p))) != 0)
    293 		return error;
    294 
    295 	return 0;
    296 }
    297 
    298 int
    299 linux32_sys_access(l, v, retval)
    300 	struct lwp *l;
    301 	void *v;
    302 	register_t *retval;
    303 {
    304 	struct linux32_sys_access_args /* {
    305 		syscallarg(const netbsd32_charp) path;
    306 		syscallarg(int) flags;
    307 	} */ *uap = v;
    308 	struct sys_access_args ua;
    309 	caddr_t sg;
    310 
    311 	NETBSD32TOP_UAP(path, const char);
    312 	NETBSD32TO64_UAP(flags);
    313 
    314 	sg = stackgap_init(l->l_proc, 0);
    315 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
    316 
    317 	return sys_access(l, &ua, retval);
    318 }
    319 
    320 int
    321 linux32_sys_getrlimit(l, v, retval)
    322 	struct lwp *l;
    323 	void *v;
    324 	register_t *retval;
    325 {
    326 	struct linux32_sys_getrlimit_args /* {
    327 		syscallarg(int) which;
    328 		syscallarg(netbsd32_orlimitp_t) rlp;
    329 	} */ *uap = v;
    330 	struct proc *p = l->l_proc;
    331 	caddr_t sg = stackgap_init(p, 0);
    332 	struct sys_getrlimit_args ap;
    333 	struct orlimit orl;
    334 	struct rlimit rl;
    335 	int error;
    336 
    337 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
    338 	if ((error = SCARG(&ap, which)) < 0)
    339 		return -error;
    340 
    341 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
    342 
    343 	if ((error = sys_getrlimit(l, &ap, retval)) != 0)
    344 		return error;
    345 
    346 	if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
    347 		return error;
    348 
    349 	bsd_to_linux_rlimit(&orl, &rl);
    350 
    351 	return copyout(&orl, NETBSD32PTR64(SCARG(uap, rlp)), sizeof(orl));
    352 }
    353 
    354 int
    355 linux32_sys_setrlimit(l, v, retval)
    356 	struct lwp *l;
    357 	void *v;
    358 	register_t *retval;
    359 {
    360 	struct linux32_sys_setrlimit_args /* {
    361 		syscallarg(int) which;
    362 		syscallarg(netbsd32_orlimitp_t) rlp;
    363 	} */ *uap = v;
    364 	struct proc *p = l->l_proc;
    365 	caddr_t sg = stackgap_init(p, 0);
    366 	struct sys_getrlimit_args ap;
    367 	struct rlimit rl;
    368 	struct orlimit orl;
    369 	int error;
    370 
    371 	SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
    372 	SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
    373 	if ((error = SCARG(&ap, which)) < 0)
    374 		return -error;
    375 
    376 	if ((error = copyin(NETBSD32PTR64(SCARG(uap, rlp)),
    377 	    &orl, sizeof(orl))) != 0)
    378 		return error;
    379 
    380 	linux_to_bsd_rlimit(&rl, &orl);
    381 
    382 	if ((error = copyout(&rl, SCARG(&ap, rlp), sizeof(rl))) != 0)
    383 		return error;
    384 
    385 	return sys_setrlimit(l, &ap, retval);
    386 }
    387 
    388 #if defined(__amd64__)
    389 int
    390 linux32_sys_ugetrlimit(l, v, retval)
    391 	struct lwp *l;
    392 	void *v;
    393 	register_t *retval;
    394 {
    395 	return linux32_sys_getrlimit(l, v, retval);
    396 }
    397 #endif
    398 
    399 extern struct timezone linux_sys_tz;
    400 int
    401 linux32_sys_gettimeofday(l, v, retval)
    402 	struct lwp *l;
    403 	void *v;
    404 	register_t *retval;
    405 {
    406 	struct linux32_sys_gettimeofday_args /* {
    407 		syscallarg(netbsd32_timevalp_t) tp;
    408 		syscallarg(netbsd32_timezonep_t) tzp;
    409 	} */ *uap = v;
    410 	struct timeval tv;
    411 	struct netbsd32_timeval tv32;
    412 	int error;
    413 
    414 	if (NETBSD32PTR64(SCARG(uap, tp)) != NULL) {
    415 		microtime(&tv);
    416 		netbsd32_from_timeval(&tv, &tv32);
    417 		if ((error = copyout(&tv32,
    418 		    (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
    419 		    sizeof(tv32))) != 0)
    420 			return error;
    421 	}
    422 
    423 	/* timezone size does not change */
    424 	if (NETBSD32PTR64(SCARG(uap, tzp)) != NULL) {
    425 		if ((error = copyout(&linux_sys_tz,
    426 		    (caddr_t)NETBSD32PTR64(SCARG(uap, tzp)),
    427 		    sizeof(linux_sys_tz))) != 0)
    428 			return error;
    429 	}
    430 
    431 	return 0;
    432 }
    433 
    434 int
    435 linux32_sys_settimeofday(l, v, retval)
    436 	struct lwp *l;
    437 	void *v;
    438 	register_t *retval;
    439 {
    440 	struct linux32_sys_settimeofday_args /* {
    441 		syscallarg(netbsd32_timevalp_t) tp;
    442 		syscallarg(netbsd32_timezonep_t) tzp;
    443 	} */ *uap = v;
    444 	struct linux_sys_settimeofday_args ua;
    445 
    446 	NETBSD32TOP_UAP(tp, struct timeval);
    447 	NETBSD32TOP_UAP(tzp, struct timezone);
    448 
    449 	return linux_sys_settimeofday(l, &ua, retval);
    450 }
    451 
    452 int
    453 linux32_sys_fcntl64(l, v, retval)
    454 	struct lwp *l;
    455 	void *v;
    456 	register_t *retval;
    457 {
    458 	struct linux32_sys_fcntl64_args /* {
    459 		syscallcarg(int) fd;
    460                 syscallarg(int) cmd;
    461 		syscallarg(netbsd32_voidp) arg;
    462 	} */ *uap = v;
    463 	struct linux_sys_fcntl64_args ua;
    464 
    465 	NETBSD32TO64_UAP(fd);
    466 	NETBSD32TO64_UAP(cmd);
    467 	NETBSD32TOP_UAP(arg, void);
    468 
    469 	return linux_sys_fcntl64(l, &ua, retval);
    470 }
    471 
    472 int
    473 linux32_sys_fcntl(l, v, retval)
    474 	struct lwp *l;
    475 	void *v;
    476 	register_t *retval;
    477 {
    478 	struct linux32_sys_fcntl_args /* {
    479 		syscallcarg(int) fd;
    480                 syscallarg(int) cmd;
    481 		syscallarg(netbsd32_voidp) arg;
    482 	} */ *uap = v;
    483 	struct linux_sys_fcntl_args ua;
    484 
    485 	NETBSD32TO64_UAP(fd);
    486 	NETBSD32TO64_UAP(cmd);
    487 	NETBSD32TOP_UAP(arg, void);
    488 
    489 	return linux_sys_fcntl(l, &ua, retval);
    490 }
    491 
    492 int
    493 linux32_sys_llseek(l, v, retval)
    494 	struct lwp *l;
    495 	void *v;
    496 	register_t *retval;
    497 {
    498 	struct linux32_sys_llseek_args /* {
    499 		syscallcarg(int) fd;
    500                 syscallarg(u_int32_t) ohigh;
    501                 syscallarg(u_int32_t) olow;
    502 		syscallarg(netbsd32_caddr_t) res;
    503 		syscallcarg(int) whence;
    504 	} */ *uap = v;
    505 	struct linux_sys_llseek_args ua;
    506 
    507 	NETBSD32TO64_UAP(fd);
    508 	NETBSD32TO64_UAP(ohigh);
    509 	NETBSD32TO64_UAP(olow);
    510 	NETBSD32TOP_UAP(res, char);
    511 	NETBSD32TO64_UAP(whence);
    512 
    513 	return linux_sys_llseek(l, &ua, retval);
    514 }
    515 
    516 
    517 int
    518 linux32_sys_time(l, v, retval)
    519 	struct lwp *l;
    520 	void *v;
    521 	register_t *retval;
    522 {
    523 	struct linux32_sys_time_args /* {
    524 		syscallcarg(linux32_timep_t) t;
    525 	} */ *uap = v;
    526 	struct linux_sys_time_args ua;
    527 
    528 	NETBSD32TOP_UAP(t, linux_time_t);
    529 
    530 	return linux_sys_time(l, &ua, retval);
    531 }
    532 
    533 
    534 int
    535 linux32_sys_getdents(l, v, retval)
    536 	struct lwp *l;
    537 	void *v;
    538 	register_t *retval;
    539 {
    540 	struct linux32_sys_getdents_args /* {
    541 		syscallcarg(int) fd;
    542 		syscallcarg(linux32_direntp_t) dent;
    543 		syscallcarg(unsigned int) count;
    544 	} */ *uap = v;
    545 	struct linux_sys_getdents_args ua;
    546 
    547 	NETBSD32TO64_UAP(fd);
    548 	NETBSD32TOP_UAP(dent, struct linux_dirent);
    549 	NETBSD32TO64_UAP(count);
    550 
    551 	return linux_sys_getdents(l, &ua, retval);
    552 }
    553 
    554 
    555 int
    556 linux32_sys_getdents64(l, v, retval)
    557 	struct lwp *l;
    558 	void *v;
    559 	register_t *retval;
    560 {
    561 	struct linux32_sys_getdents64_args /* {
    562 		syscallcarg(int) fd;
    563 		syscallcarg(linux32_dirent64p_t) dent;
    564 		syscallcarg(unsigned int) count;
    565 	} */ *uap = v;
    566 	struct linux_sys_getdents64_args ua;
    567 
    568 	NETBSD32TO64_UAP(fd);
    569 	NETBSD32TOP_UAP(dent, struct linux_dirent64);
    570 	NETBSD32TO64_UAP(count);
    571 
    572 	return linux_sys_getdents64(l, &ua, retval);
    573 }
    574 
    575 int
    576 linux32_sys_socketpair(l, v, retval)
    577 	struct lwp *l;
    578 	void *v;
    579 	register_t *retval;
    580 {
    581 	struct linux32_sys_socketpair_args /* {
    582 		syscallarg(int) domain;
    583 		syscallarg(int) type;
    584 		syscallarg(int) protocol;
    585 		syscallarg(netbsd32_intp) rsv;
    586 	} */ *uap = v;
    587 	struct linux_sys_socketpair_args ua;
    588 
    589 	NETBSD32TO64_UAP(domain);
    590 	NETBSD32TO64_UAP(type);
    591 	NETBSD32TO64_UAP(protocol);
    592 	NETBSD32TOP_UAP(rsv, int)
    593 
    594 	return linux_sys_socketpair(l, &ua, retval);
    595 }
    596 
    597 int
    598 linux32_sys_sendto(l, v, retval)
    599 	struct lwp *l;
    600 	void *v;
    601 	register_t *retval;
    602 {
    603 	struct linux32_sys_sendto_args /* {
    604 		syscallarg(int) s;
    605 		syscallarg(netbsd32_voidp) msg;
    606 		syscallarg(int) len;
    607 		syscallarg(int) flags;
    608 		syscallarg(netbsd32_osockaddrp_t) to;
    609 		syscallarg(int) tolen;
    610 	} */ *uap = v;
    611 	struct linux_sys_sendto_args ua;
    612 
    613 	NETBSD32TO64_UAP(s);
    614 	NETBSD32TOP_UAP(msg, void);
    615 	NETBSD32TO64_UAP(len);
    616 	NETBSD32TO64_UAP(flags);
    617 	NETBSD32TOP_UAP(to, struct osockaddr);
    618 	NETBSD32TO64_UAP(tolen);
    619 
    620 	return linux_sys_sendto(l, &ua, retval);
    621 }
    622 
    623 
    624 int
    625 linux32_sys_recvfrom(l, v, retval)
    626 	struct lwp *l;
    627 	void *v;
    628 	register_t *retval;
    629 {
    630 	struct linux32_sys_recvfrom_args /* {
    631 		syscallarg(int) s;
    632 		syscallarg(netbsd32_voidp) buf;
    633 		syscallarg(netbsd32_size_t) len;
    634 		syscallarg(int) flags;
    635 		syscallarg(netbsd32_osockaddrp_t) from;
    636 		syscallarg(netbsd32_intp) fromlenaddr;
    637 	} */ *uap = v;
    638 	struct linux_sys_recvfrom_args ua;
    639 
    640 	NETBSD32TO64_UAP(s);
    641 	NETBSD32TOP_UAP(buf, void);
    642 	NETBSD32TO64_UAP(len);
    643 	NETBSD32TO64_UAP(flags);
    644 	NETBSD32TOP_UAP(from, struct osockaddr);
    645 	NETBSD32TOP_UAP(fromlenaddr, unsigned int);
    646 
    647 	return linux_sys_recvfrom(l, &ua, retval);
    648 }
    649 
    650 int
    651 linux32_sys_setsockopt(l, v, retval)
    652 	struct lwp *l;
    653 	void *v;
    654 	register_t *retval;
    655 {
    656 	struct linux32_sys_setsockopt_args /* {
    657 		syscallarg(int) s;
    658 		syscallarg(int) level;
    659 		syscallarg(int) optname;
    660 		syscallarg(netbsd32_voidp) optval;
    661 		syscallarg(int) optlen;
    662 	} */ *uap = v;
    663 	struct linux_sys_setsockopt_args ua;
    664 
    665 	NETBSD32TO64_UAP(s);
    666 	NETBSD32TO64_UAP(level);
    667 	NETBSD32TO64_UAP(optname);
    668 	NETBSD32TOP_UAP(optval, void);
    669 	NETBSD32TO64_UAP(optlen);
    670 
    671 	return linux_sys_setsockopt(l, &ua, retval);
    672 }
    673 
    674 
    675 int
    676 linux32_sys_getsockopt(l, v, retval)
    677 	struct lwp *l;
    678 	void *v;
    679 	register_t *retval;
    680 {
    681 	struct linux32_sys_getsockopt_args /* {
    682 		syscallarg(int) s;
    683 		syscallarg(int) level;
    684 		syscallarg(int) optname;
    685 		syscallarg(netbsd32_voidp) optval;
    686 		syscallarg(netbsd32_intp) optlen;
    687 	} */ *uap = v;
    688 	struct linux_sys_getsockopt_args ua;
    689 
    690 	NETBSD32TO64_UAP(s);
    691 	NETBSD32TO64_UAP(level);
    692 	NETBSD32TO64_UAP(optname);
    693 	NETBSD32TOP_UAP(optval, void);
    694 	NETBSD32TOP_UAP(optlen, int);
    695 
    696 	return linux_sys_getsockopt(l, &ua, retval);
    697 }
    698 
    699 int
    700 linux32_sys_socket(l, v, retval)
    701 	struct lwp *l;
    702 	void *v;
    703 	register_t *retval;
    704 {
    705 	struct linux32_sys_socket_args /* {
    706 		syscallarg(int) domain;
    707 		syscallarg(int) type;
    708 		syscallarg(int) protocol;
    709 	} */ *uap = v;
    710 	struct linux_sys_socket_args ua;
    711 
    712 	NETBSD32TO64_UAP(domain);
    713 	NETBSD32TO64_UAP(type);
    714 	NETBSD32TO64_UAP(protocol);
    715 
    716 	return linux_sys_socket(l, &ua, retval);
    717 }
    718 
    719 int
    720 linux32_sys_bind(l, v, retval)
    721 	struct lwp *l;
    722 	void *v;
    723 	register_t *retval;
    724 {
    725 	struct linux32_sys_bind_args /* {
    726 		syscallarg(int) s;
    727 		syscallarg(netbsd32_osockaddrp_t) name;
    728 		syscallarg(int) namelen;
    729 	} */ *uap = v;
    730 	struct linux_sys_bind_args ua;
    731 
    732 	NETBSD32TO64_UAP(s);
    733 	NETBSD32TOP_UAP(name, struct osockaddr)
    734 	NETBSD32TO64_UAP(namelen);
    735 
    736 	return linux_sys_bind(l, &ua, retval);
    737 }
    738 
    739 int
    740 linux32_sys_connect(l, v, retval)
    741 	struct lwp *l;
    742 	void *v;
    743 	register_t *retval;
    744 {
    745 	struct linux32_sys_connect_args /* {
    746 		syscallarg(int) s;
    747 		syscallarg(netbsd32_osockaddrp_t) name;
    748 		syscallarg(int) namelen;
    749 	} */ *uap = v;
    750 	struct linux_sys_connect_args ua;
    751 
    752 	NETBSD32TO64_UAP(s);
    753 	NETBSD32TOP_UAP(name, struct osockaddr)
    754 	NETBSD32TO64_UAP(namelen);
    755 
    756 	printf("linux32_sys_connect: s = %d, name = %p, namelen = %d\n",
    757 		SCARG(&ua, s), SCARG(&ua, name), SCARG(&ua, namelen));
    758 
    759 	return linux_sys_connect(l, &ua, retval);
    760 }
    761 
    762 int
    763 linux32_sys_accept(l, v, retval)
    764 	struct lwp *l;
    765 	void *v;
    766 	register_t *retval;
    767 {
    768 	struct linux32_sys_accept_args /* {
    769 		syscallarg(int) s;
    770 		syscallarg(netbsd32_osockaddrp_t) name;
    771 		syscallarg(netbsd32_intp) anamelen;
    772 	} */ *uap = v;
    773 	struct linux_sys_accept_args ua;
    774 
    775 	NETBSD32TO64_UAP(s);
    776 	NETBSD32TOP_UAP(name, struct osockaddr)
    777 	NETBSD32TOP_UAP(anamelen, int);
    778 
    779 	return linux_sys_accept(l, &ua, retval);
    780 }
    781 
    782 int
    783 linux32_sys_getpeername(l, v, retval)
    784 	struct lwp *l;
    785 	void *v;
    786 	register_t *retval;
    787 {
    788 	struct linux32_sys_getpeername_args /* {
    789 		syscallarg(int) fdes;
    790 		syscallarg(netbsd32_sockaddrp_t) asa;
    791 		syscallarg(netbsd32_intp) alen;
    792 	} */ *uap = v;
    793 	struct linux_sys_getpeername_args ua;
    794 
    795 	NETBSD32TO64_UAP(fdes);
    796 	NETBSD32TOP_UAP(asa, struct sockaddr)
    797 	NETBSD32TOP_UAP(alen, int);
    798 
    799 	return linux_sys_getpeername(l, &ua, retval);
    800 }
    801 
    802 int
    803 linux32_sys_getsockname(l, v, retval)
    804 	struct lwp *l;
    805 	void *v;
    806 	register_t *retval;
    807 {
    808 	struct linux32_sys_getsockname_args /* {
    809 		syscallarg(int) fdec;
    810 		syscallarg(netbsd32_charp) asa;
    811 		syscallarg(netbsd32_intp) alen;
    812 	} */ *uap = v;
    813 	struct linux_sys_getsockname_args ua;
    814 
    815 	NETBSD32TO64_UAP(fdec);
    816 	NETBSD32TOP_UAP(asa, char)
    817 	NETBSD32TOP_UAP(alen, int);
    818 
    819 	return linux_sys_getsockname(l, &ua, retval);
    820 }
    821 
    822 int
    823 linux32_sys_sendmsg(l, v, retval)
    824 	struct lwp *l;
    825 	void *v;
    826 	register_t *retval;
    827 {
    828 	struct linux32_sys_sendmsg_args /* {
    829 		syscallarg(int) s;
    830 		syscallarg(netbsd32_msghdrp_t) msg;
    831 		syscallarg(int) flags;
    832 	} */ *uap = v;
    833 	struct linux_sys_sendmsg_args ua;
    834 
    835 	NETBSD32TO64_UAP(s);
    836 	NETBSD32TOP_UAP(msg, struct msghdr);
    837 	NETBSD32TO64_UAP(flags);
    838 
    839 	return linux_sys_sendmsg(l, &ua, retval);
    840 }
    841 
    842 int
    843 linux32_sys_recvmsg(l, v, retval)
    844 	struct lwp *l;
    845 	void *v;
    846 	register_t *retval;
    847 {
    848 	struct linux32_sys_recvmsg_args /* {
    849 		syscallarg(int) s;
    850 		syscallarg(netbsd32_msghdrp_t) msg;
    851 		syscallarg(int) flags;
    852 	} */ *uap = v;
    853 	struct linux_sys_recvmsg_args ua;
    854 
    855 	NETBSD32TO64_UAP(s);
    856 	NETBSD32TOP_UAP(msg, struct msghdr);
    857 	NETBSD32TO64_UAP(flags);
    858 
    859 	return linux_sys_recvmsg(l, &ua, retval);
    860 }
    861 
    862 int
    863 linux32_sys_send(l, v, retval)
    864 	struct lwp *l;
    865 	void *v;
    866 	register_t *retval;
    867 {
    868 	struct linux32_sys_send_args /* {
    869 		syscallarg(int) s;
    870 		syscallarg(netbsd32_voidp) buf;
    871 		syscallarg(int) len;
    872 		syscallarg(int) flags;
    873 	} */ *uap = v;
    874 	struct sys_sendto_args ua;
    875 
    876 	NETBSD32TO64_UAP(s);
    877 	NETBSD32TOP_UAP(buf, void);
    878 	NETBSD32TO64_UAP(len);
    879 	NETBSD32TO64_UAP(flags);
    880 	SCARG(&ua, to) = NULL;
    881 	SCARG(&ua, tolen) = 0;
    882 
    883 	return sys_sendto(l, &ua, retval);
    884 }
    885 
    886 int
    887 linux32_sys_recv(l, v, retval)
    888 	struct lwp *l;
    889 	void *v;
    890 	register_t *retval;
    891 {
    892 	struct linux32_sys_recv_args /* {
    893 		syscallarg(int) s;
    894 		syscallarg(netbsd32_voidp) buf;
    895 		syscallarg(int) len;
    896 		syscallarg(int) flags;
    897 	} */ *uap = v;
    898 	struct sys_recvfrom_args ua;
    899 
    900 	NETBSD32TO64_UAP(s);
    901 	NETBSD32TOP_UAP(buf, void);
    902 	NETBSD32TO64_UAP(len);
    903 	NETBSD32TO64_UAP(flags);
    904 	SCARG(&ua, from) = NULL;
    905 	SCARG(&ua, fromlenaddr) = NULL;
    906 
    907 	return sys_recvfrom(l, &ua, retval);
    908 }
    909 
    910 int
    911 linux32_sys_readlink(l, v, retval)
    912 	struct lwp *l;
    913 	void *v;
    914 	register_t *retval;
    915 {
    916 	struct linux32_sys_readlink_args /* {
    917 		syscallarg(const netbsd32_charp) name;
    918 		syscallarg(netbsd32_charp) buf;
    919 		syscallarg(int) count;
    920 	} */ *uap = v;
    921 	struct linux_sys_readlink_args ua;
    922 
    923 	NETBSD32TOP_UAP(name, const char);
    924 	NETBSD32TOP_UAP(buf, char)
    925 	NETBSD32TO64_UAP(count);
    926 
    927 	return linux_sys_readlink(l, &ua, retval);
    928 }
    929 
    930 
    931 int
    932 linux32_sys_select(l, v, retval)
    933 	struct lwp *l;
    934 	void *v;
    935 	register_t *retval;
    936 {
    937 	struct linux32_sys_select_args /* {
    938 		syscallarg(int) nfds;
    939 		syscallarg(netbsd32_fd_setp_t) readfds;
    940 		syscallarg(netbsd32_fd_setp_t) writefds;
    941 		syscallarg(netbsd32_fd_setp_t) exceptfds;
    942 		syscallarg(netbsd32_timevalp_t) timeout;
    943 	} */ *uap = v;
    944 
    945 	return linux32_select1(l, retval, SCARG(uap, nfds),
    946 	    NETBSD32PTR64(SCARG(uap, readfds)),
    947 	    NETBSD32PTR64(SCARG(uap, writefds)),
    948 	    NETBSD32PTR64(SCARG(uap, exceptfds)),
    949 	    NETBSD32PTR64(SCARG(uap, timeout)));
    950 }
    951 
    952 int
    953 linux32_sys_oldselect(l, v, retval)
    954 	struct lwp *l;
    955 	void *v;
    956 	register_t *retval;
    957 {
    958 	struct linux32_sys_oldselect_args /* {
    959 		syscallarg(linux32_oldselectp_t) lsp;
    960 	} */ *uap = v;
    961 	struct linux32_oldselect lsp32;
    962 	int error;
    963 
    964 	if ((error = copyin(NETBSD32PTR64(SCARG(uap, lsp)),
    965 	    &lsp32, sizeof(lsp32))) != 0)
    966 		return error;
    967 
    968 	return linux32_select1(l, retval, lsp32.nfds,
    969 	     NETBSD32PTR64(lsp32.readfds), NETBSD32PTR64(lsp32.writefds),
    970 	     NETBSD32PTR64(lsp32.exceptfds), NETBSD32PTR64(lsp32.timeout));
    971 }
    972 
    973 static int
    974 linux32_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
    975         struct lwp *l;
    976         register_t *retval;
    977         int nfds;
    978         fd_set *readfds, *writefds, *exceptfds;
    979         struct timeval *timeout;
    980 {
    981 	struct timeval tv0, tv1, utv, otv;
    982 	struct netbsd32_timeval utv32;
    983 	int error;
    984 
    985 	/*
    986 	 * Store current time for computation of the amount of
    987 	 * time left.
    988 	 */
    989 	if (timeout) {
    990 		if ((error = copyin(timeout, &utv32, sizeof(utv32))))
    991 			return error;
    992 
    993 		netbsd32_to_timeval(&utv32, &utv);
    994 		otv = utv;
    995 
    996 		if (itimerfix(&utv)) {
    997 			/*
    998 			 * The timeval was invalid.  Convert it to something
    999 			 * valid that will act as it does under Linux.
   1000 			 */
   1001 			utv.tv_sec += utv.tv_usec / 1000000;
   1002 			utv.tv_usec %= 1000000;
   1003 			if (utv.tv_usec < 0) {
   1004 				utv.tv_sec -= 1;
   1005 				utv.tv_usec += 1000000;
   1006 			}
   1007 			if (utv.tv_sec < 0)
   1008 				timerclear(&utv);
   1009 		}
   1010 		microtime(&tv0);
   1011 	} else {
   1012 		timerclear(&utv);
   1013 	}
   1014 
   1015 	error = selcommon(l, retval, nfds,
   1016 	    readfds, writefds, exceptfds, &utv, NULL);
   1017 
   1018 	if (error) {
   1019 		/*
   1020 		 * See fs/select.c in the Linux kernel.  Without this,
   1021 		 * Maelstrom doesn't work.
   1022 		 */
   1023 		if (error == ERESTART)
   1024 			error = EINTR;
   1025 		return error;
   1026 	}
   1027 
   1028 	if (timeout) {
   1029 		if (*retval) {
   1030 			/*
   1031 			 * Compute how much time was left of the timeout,
   1032 			 * by subtracting the current time and the time
   1033 			 * before we started the call, and subtracting
   1034 			 * that result from the user-supplied value.
   1035 			 */
   1036 			microtime(&tv1);
   1037 			timersub(&tv1, &tv0, &tv1);
   1038 			timersub(&otv, &tv1, &utv);
   1039 			if (utv.tv_sec < 0)
   1040 				timerclear(&utv);
   1041 		} else {
   1042 			timerclear(&utv);
   1043 		}
   1044 
   1045 		netbsd32_from_timeval(&utv, &utv32);
   1046 
   1047 		if ((error = copyout(&utv32, timeout, sizeof(utv32))))
   1048 			return error;
   1049 	}
   1050 
   1051 	return 0;
   1052 }
   1053 
   1054 int
   1055 linux32_sys_pipe(l, v, retval)
   1056 	struct lwp *l;
   1057 	void *v;
   1058 	register_t *retval;
   1059 {
   1060 	struct linux32_sys_pipe_args /* {
   1061 		syscallarg(netbsd32_intp) fd;
   1062 	} */ *uap = v;
   1063 	int error;
   1064 	int pfds[2];
   1065 
   1066 	if ((error = sys_pipe(l, 0, retval)))
   1067 		return error;
   1068 
   1069 	pfds[0] = (int)retval[0];
   1070 	pfds[1] = (int)retval[1];
   1071 
   1072 	if ((error = copyout(pfds, NETBSD32PTR64(SCARG(uap, fd)),
   1073 	    2 * sizeof (int))) != 0)
   1074 		return error;
   1075 
   1076 	retval[0] = 0;
   1077 	retval[1] = 0;
   1078 
   1079 	return 0;
   1080 }
   1081 
   1082 
   1083 int
   1084 linux32_sys_times(l, v, retval)
   1085 	struct lwp *l;
   1086 	void *v;
   1087 	register_t *retval;
   1088 {
   1089 	struct linux32_sys_times_args /* {
   1090 		syscallarg(linux32_tmsp_t) tms;
   1091 	} */ *uap = v;
   1092 	struct linux32_tms ltms32;
   1093 	struct linux_tms ltms;
   1094 	struct linux_tms *ltmsp;
   1095 	struct linux_sys_times_args ua;
   1096 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1097 	int error;
   1098 
   1099 	ltmsp = stackgap_alloc(l->l_proc, &sg, sizeof(*ltmsp));
   1100 	SCARG(&ua, tms) = (struct times *)ltmsp;
   1101 
   1102 	if ((error = linux_sys_times(l, &ua, retval)) != 0)
   1103 		return error;
   1104 
   1105 	if ((error = copyin(ltmsp, &ltms, sizeof(ltms))) != 0)
   1106 		return error;
   1107 
   1108 	ltms32.ltms32_utime = (linux32_clock_t)ltms.ltms_utime;
   1109 	ltms32.ltms32_stime = (linux32_clock_t)ltms.ltms_stime;
   1110 	ltms32.ltms32_cutime = (linux32_clock_t)ltms.ltms_cutime;
   1111 	ltms32.ltms32_cstime = (linux32_clock_t)ltms.ltms_cstime;
   1112 
   1113 	if ((error = copyout(&ltms32,
   1114 	    NETBSD32PTR64(SCARG(uap, tms)), sizeof(ltms32))) != 0)
   1115 		return error;
   1116 
   1117 	return 0;
   1118 }
   1119 
   1120 int
   1121 linux32_sys_clone(l, v, retval)
   1122 	struct lwp *l;
   1123 	void *v;
   1124 	register_t *retval;
   1125 {
   1126 	struct linux32_sys_clone_args /* {
   1127 		syscallarg(int) flags;
   1128 		syscallarg(netbsd32_voidp) stack;
   1129 	} */ *uap = v;
   1130 	struct linux_sys_clone_args ua;
   1131 
   1132 	NETBSD32TO64_UAP(flags);
   1133 	NETBSD32TOP_UAP(stack, void *);
   1134 #ifdef LINUX_NPTL
   1135 	SCARG(&ua, parent_tidptr) = NULL;
   1136 	SCARG(&ua, child_tidptr) = NULL;
   1137 #endif
   1138 
   1139 	return linux_sys_clone(l, &ua, retval);
   1140 }
   1141 
   1142 int
   1143 linux32_sys_sched_setscheduler(l, v, retval)
   1144 	struct lwp *l;
   1145 	void *v;
   1146 	register_t *retval;
   1147 {
   1148 	struct linux32_sys_sched_setscheduler_args /* {
   1149 		syscallarg(int) pid;
   1150 		syscallarg(int) policy;
   1151 		syscallarg(const linux32_sched_paramp_t) sp;
   1152 	} */ *uap = v;
   1153 	struct linux_sys_sched_setscheduler_args ua;
   1154 
   1155 	NETBSD32TO64_UAP(pid);
   1156 	NETBSD32TO64_UAP(policy);
   1157 	NETBSD32TOP_UAP(sp, const struct linux_sched_param);
   1158 
   1159 	return linux_sys_sched_setscheduler(l, &ua, retval);
   1160 }
   1161 
   1162 
   1163 int
   1164 linux32_sys_waitpid(l, v, retval)
   1165 	struct lwp *l;
   1166 	void *v;
   1167 	register_t *retval;
   1168 {
   1169 	struct linux32_sys_waitpid_args /* {
   1170 		syscallarg(int) pid;
   1171 		syscallarg(netbsd32_intp) status;
   1172 		syscallarg(int) options;
   1173 	} */ *uap = v;
   1174 	struct linux_sys_wait4_args ua;
   1175 
   1176 	SCARG(&ua, pid) = SCARG(uap, pid);
   1177 	SCARG(&ua, status) = NETBSD32PTR64(SCARG(uap, status));
   1178 	SCARG(&ua, options) = SCARG(uap, options);
   1179 	SCARG(&ua, rusage) = NULL;
   1180 
   1181 	return linux_sys_wait4(l, &ua, retval);
   1182 }
   1183 
   1184 int
   1185 linux32_sys_wait4(l, v, retval)
   1186 	struct lwp *l;
   1187 	void *v;
   1188 	register_t *retval;
   1189 {
   1190 	struct linux32_sys_wait4_args /* {
   1191 		syscallarg(int) pid;
   1192 		syscallarg(netbsd32_intp) status;
   1193 		syscallarg(int) options;
   1194 		syscallarg(netbsd32_rusagep_t) rusage;
   1195 	} */ *uap = v;
   1196 	struct linux_sys_wait4_args ua;
   1197 
   1198 	NETBSD32TO64_UAP(pid);
   1199 	NETBSD32TOP_UAP(status, int);
   1200 	NETBSD32TO64_UAP(options);
   1201 	NETBSD32TOP_UAP(rusage, struct rusage);
   1202 
   1203 	return linux_sys_wait4(l, &ua, retval);
   1204 }
   1205 
   1206 int
   1207 linux32_sys_unlink(l, v, retval)
   1208 	struct lwp *l;
   1209 	void *v;
   1210 	register_t *retval;
   1211 {
   1212 	struct linux32_sys_unlink_args /* {
   1213 		syscallarg(const netbsd32_charp) path;
   1214 	} */ *uap = v;
   1215 	struct linux_sys_unlink_args ua;
   1216 
   1217 	NETBSD32TOP_UAP(path, const char);
   1218 
   1219 	return linux_sys_unlink(l, &ua, retval);
   1220 }
   1221 
   1222 int
   1223 linux32_sys_chdir(l, v, retval)
   1224 	struct lwp *l;
   1225 	void *v;
   1226 	register_t *retval;
   1227 {
   1228 	struct linux32_sys_chdir_args /* {
   1229 		syscallarg(const netbsd32_charp) path;
   1230 	} */ *uap = v;
   1231 	struct sys_chdir_args ua;
   1232 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1233 
   1234 	NETBSD32TOP_UAP(path, const char);
   1235 
   1236 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1237 
   1238 	return sys_chdir(l, &ua, retval);
   1239 }
   1240 
   1241 int
   1242 linux32_sys_link(l, v, retval)
   1243 	struct lwp *l;
   1244 	void *v;
   1245 	register_t *retval;
   1246 {
   1247 	struct linux32_sys_link_args /* {
   1248 		syscallarg(const netbsd32_charp) path;
   1249 		syscallarg(const netbsd32_charp) link;
   1250 	} */ *uap = v;
   1251 	struct sys_link_args ua;
   1252 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1253 
   1254 	NETBSD32TOP_UAP(path, const char);
   1255 	NETBSD32TOP_UAP(link, const char);
   1256 
   1257 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1258 	CHECK_ALT_CREAT(l, &sg, SCARG(&ua, link));
   1259 
   1260 	return sys_link(l, &ua, retval);
   1261 }
   1262 
   1263 int
   1264 linux32_sys_creat(l, v, retval)
   1265 	struct lwp *l;
   1266 	void *v;
   1267 	register_t *retval;
   1268 {
   1269 	struct linux32_sys_creat_args /* {
   1270 		syscallarg(const netbsd32_charp) path;
   1271 		syscallarg(int) mode;
   1272 	} */ *uap = v;
   1273 	struct sys_open_args ua;
   1274 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1275 
   1276 	NETBSD32TOP_UAP(path, const char);
   1277 	SCARG(&ua, flags) = O_CREAT | O_TRUNC | O_WRONLY;
   1278 	NETBSD32TO64_UAP(mode);
   1279 
   1280 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1281 
   1282 	return sys_open(l, &ua, retval);
   1283 }
   1284 
   1285 int
   1286 linux32_sys_mknod(l, v, retval)
   1287 	struct lwp *l;
   1288 	void *v;
   1289 	register_t *retval;
   1290 {
   1291 	struct linux32_sys_mknod_args /* {
   1292 		syscallarg(const netbsd32_charp) path;
   1293 		syscallarg(int) mode;
   1294 		syscallarg(int) dev;
   1295 	} */ *uap = v;
   1296 	struct linux_sys_mknod_args ua;
   1297 
   1298 	NETBSD32TOP_UAP(path, const char);
   1299 	NETBSD32TO64_UAP(mode);
   1300 	NETBSD32TO64_UAP(dev);
   1301 
   1302 	return linux_sys_mknod(l, &ua, retval);
   1303 }
   1304 
   1305 int
   1306 linux32_sys_chmod(l, v, retval)
   1307 	struct lwp *l;
   1308 	void *v;
   1309 	register_t *retval;
   1310 {
   1311 	struct linux32_sys_chmod_args /* {
   1312 		syscallarg(const netbsd32_charp) path;
   1313 		syscallarg(int) mode;
   1314 	} */ *uap = v;
   1315 	struct sys_chmod_args ua;
   1316 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1317 
   1318 	NETBSD32TOP_UAP(path, const char);
   1319 	NETBSD32TO64_UAP(mode);
   1320 
   1321 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1322 
   1323 	return sys_chmod(l, &ua, retval);
   1324 }
   1325 
   1326 int
   1327 linux32_sys_lchown16(l, v, retval)
   1328 	struct lwp *l;
   1329 	void *v;
   1330 	register_t *retval;
   1331 {
   1332 	struct linux32_sys_lchown16_args /* {
   1333 		syscallarg(const netbsd32_charp) path;
   1334 		syscallarg(int) uid;
   1335 		syscallarg(int) gid;
   1336 	} */ *uap = v;
   1337         struct sys___posix_lchown_args ua;
   1338         caddr_t sg = stackgap_init(l->l_proc, 0);
   1339 
   1340 	NETBSD32TOP_UAP(path, const char);
   1341         CHECK_ALT_SYMLINK(l, &sg, SCARG(&ua, path));
   1342 
   1343         if ((linux32_uid_t)SCARG(uap, uid) == (linux32_uid_t)-1)
   1344         	SCARG(&ua, uid) = (uid_t)-1;
   1345 	else
   1346         	SCARG(&ua, uid) = SCARG(uap, uid);
   1347 
   1348         if ((linux32_gid_t)SCARG(uap, gid) == (linux32_gid_t)-1)
   1349         	SCARG(&ua, gid) = (gid_t)-1;
   1350 	else
   1351         	SCARG(&ua, gid) = SCARG(uap, gid);
   1352 
   1353         return sys___posix_lchown(l, &ua, retval);
   1354 }
   1355 
   1356 int
   1357 linux32_sys_break(l, v, retval)
   1358 	struct lwp *l;
   1359 	void *v;
   1360 	register_t *retval;
   1361 {
   1362 #if 0
   1363 	struct linux32_sys_break_args /* {
   1364 		syscallarg(const netbsd32_charp) nsize;
   1365 	} */ *uap = v;
   1366 #endif
   1367 
   1368 	return ENOSYS;
   1369 }
   1370 
   1371 int
   1372 linux32_sys_stime(l, v, retval)
   1373 	struct lwp *l;
   1374 	void *v;
   1375 	register_t *retval;
   1376 {
   1377 	struct linux32_sys_stime_args /* {
   1378 		syscallarg(linux32_timep_t) t;
   1379 	} */ *uap = v;
   1380 	struct proc *p = l->l_proc;
   1381 	struct timespec ts;
   1382 	linux32_time_t tt32;
   1383 	int error;
   1384 
   1385 	if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
   1386 		return error;
   1387 
   1388 	if ((error = copyin(&tt32,
   1389 	    NETBSD32PTR64(SCARG(uap, t)),
   1390 	    sizeof tt32)) != 0)
   1391 
   1392 	ts.tv_sec = (long)tt32;
   1393 	ts.tv_nsec = 0;
   1394 
   1395 	return settime(p, &ts);
   1396 }
   1397 
   1398 int
   1399 linux32_sys_utime(l, v, retval)
   1400 	struct lwp *l;
   1401 	void *v;
   1402 	register_t *retval;
   1403 {
   1404 	struct linux32_sys_utime_args /* {
   1405 		syscallarg(const netbsd32_charp) path;
   1406 		syscallarg(linux32_utimbufp_t) times;
   1407 	} */ *uap = v;
   1408 	struct proc *p = l->l_proc;
   1409         caddr_t sg = stackgap_init(p, 0);
   1410         struct sys_utimes_args ua;
   1411         struct timeval tv[2], *tvp;
   1412         struct linux32_utimbuf lut;
   1413         int error;
   1414 
   1415 	NETBSD32TOP_UAP(path, const char);
   1416         CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1417 
   1418 
   1419         if (NETBSD32PTR64(SCARG(uap, times)) != NULL) {
   1420                 if ((error = copyin(NETBSD32PTR64(SCARG(uap, times)),
   1421 		    &lut, sizeof lut)))
   1422                         return error;
   1423 
   1424                 tv[0].tv_sec = (long)lut.l_actime;
   1425                 tv[0].tv_usec = 0;
   1426                 tv[1].tv_sec = (long)lut.l_modtime;
   1427 		tv[1].tv_usec = 0;
   1428 
   1429 	        tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv));
   1430 
   1431                 if ((error = copyout(tv, tvp, sizeof(tv))))
   1432                         return error;
   1433                 SCARG(&ua, tptr) = tvp;
   1434         } else {
   1435                SCARG(&ua, tptr) = NULL;
   1436 	}
   1437 
   1438         return sys_utimes(l, &ua, retval);
   1439 }
   1440 
   1441 int
   1442 linux32_sys_rename(l, v, retval)
   1443 	struct lwp *l;
   1444 	void *v;
   1445 	register_t *retval;
   1446 {
   1447 	struct linux32_sys_rename_args /* {
   1448 		syscallarg(const netbsd32_charp) from;
   1449 		syscallarg(const netbsd32_charp) to;
   1450 	} */ *uap = v;
   1451 	struct sys_rename_args ua;
   1452 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1453 
   1454 	NETBSD32TOP_UAP(from, const char);
   1455 	NETBSD32TOP_UAP(to, const char);
   1456 
   1457 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, from));
   1458 	CHECK_ALT_CREAT(l, &sg, SCARG(&ua, to));
   1459 
   1460 	return sys___posix_rename(l, &ua, retval);
   1461 }
   1462 
   1463 int
   1464 linux32_sys_mkdir(l, v, retval)
   1465 	struct lwp *l;
   1466 	void *v;
   1467 	register_t *retval;
   1468 {
   1469 	struct linux32_sys_mkdir_args /* {
   1470 		syscallarg(const netbsd32_charp) path;
   1471 		syscallarg(int) mode;
   1472 	} */ *uap = v;
   1473 	struct sys_mkdir_args ua;
   1474 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1475 
   1476 	NETBSD32TOP_UAP(path, const char);
   1477 	NETBSD32TO64_UAP(mode);
   1478 
   1479 	CHECK_ALT_CREAT(l, &sg, SCARG(&ua, path));
   1480 
   1481 	return sys_mkdir(l, &ua, retval);
   1482 }
   1483 
   1484 int
   1485 linux32_sys_rmdir(l, v, retval)
   1486 	struct lwp *l;
   1487 	void *v;
   1488 	register_t *retval;
   1489 {
   1490 	struct linux32_sys_rmdir_args /* {
   1491 		syscallarg(const netbsd32_charp) path;
   1492 	} */ *uap = v;
   1493 	struct sys_rmdir_args ua;
   1494 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1495 
   1496 	NETBSD32TOP_UAP(path, const char);
   1497 
   1498 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1499 
   1500 	return sys_rmdir(l, &ua, retval);
   1501 }
   1502 
   1503 int
   1504 linux32_sys_signal(l, v, retval)
   1505 	struct lwp *l;
   1506 	void *v;
   1507 	register_t *retval;
   1508 {
   1509 	struct linux32_sys_signal_args /* {
   1510 		syscallarg(int) signum;
   1511 		syscallarg(linux32_handler_t) handler;
   1512 	} */ *uap = v;
   1513         struct proc *p = l->l_proc;
   1514         struct sigaction nbsa, obsa;
   1515         int error, sig;
   1516 
   1517         *retval = -1;
   1518 
   1519         sig = SCARG(uap, signum);
   1520         if (sig < 0 || sig >= LINUX32__NSIG)
   1521                 return EINVAL;
   1522 
   1523         nbsa.sa_handler = NETBSD32PTR64(SCARG(uap, handler));
   1524         sigemptyset(&nbsa.sa_mask);
   1525         nbsa.sa_flags = SA_RESETHAND | SA_NODEFER;
   1526 
   1527         if ((error = sigaction1(p, linux32_to_native_signo[sig],
   1528             &nbsa, &obsa, NULL, 0)) != 0)
   1529 		return error;
   1530 
   1531         *retval = (int)(long)obsa.sa_handler;
   1532         return 0;
   1533 }
   1534 
   1535 int
   1536 linux32_sys_oldolduname(l, v, retval)
   1537         struct lwp *l;
   1538         void *v;
   1539         register_t *retval;
   1540 {
   1541         struct linux32_sys_uname_args /* {
   1542                 syscallarg(linux32_oldoldutsnamep_t) up;
   1543         } */ *uap = v;
   1544         struct linux_oldoldutsname luts;
   1545 
   1546         strncpy(luts.l_sysname, linux32_sysname, sizeof(luts.l_sysname));
   1547         strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
   1548         strncpy(luts.l_release, linux32_release, sizeof(luts.l_release));
   1549         strncpy(luts.l_version, linux32_version, sizeof(luts.l_version));
   1550         strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
   1551 
   1552         return copyout(&luts, NETBSD32PTR64(SCARG(uap, up)), sizeof(luts));
   1553 }
   1554 
   1555 int
   1556 linux32_sys_getgroups16(l, v, retval)
   1557 	struct lwp *l;
   1558 	void *v;
   1559 	register_t *retval;
   1560 {
   1561 	struct linux32_sys_getgroups16_args /* {
   1562 		syscallarg(int) gidsetsize;
   1563 		syscallarg(linux32_gidp_t) gidset;
   1564 	} */ *uap = v;
   1565 	struct linux_sys_getgroups16_args ua;
   1566 
   1567 	NETBSD32TO64_UAP(gidsetsize);
   1568 	NETBSD32TOP_UAP(gidset, linux32_gid_t);
   1569 
   1570 	return linux_sys_getgroups16(l, &ua, retval);
   1571 }
   1572 
   1573 int
   1574 linux32_sys_setgroups16(l, v, retval)
   1575 	struct lwp *l;
   1576 	void *v;
   1577 	register_t *retval;
   1578 {
   1579 	struct linux32_sys_setgroups16_args /* {
   1580 		syscallarg(int) gidsetsize;
   1581 		syscallarg(linux32_gidp_t) gidset;
   1582 	} */ *uap = v;
   1583 	struct linux_sys_setgroups16_args ua;
   1584 
   1585 	NETBSD32TO64_UAP(gidsetsize);
   1586 	NETBSD32TOP_UAP(gidset, linux32_gid_t);
   1587 
   1588 	return linux_sys_setgroups16(l, &ua, retval);
   1589 }
   1590 
   1591 int
   1592 linux32_sys_symlink(l, v, retval)
   1593 	struct lwp *l;
   1594 	void *v;
   1595 	register_t *retval;
   1596 {
   1597 	struct linux32_sys_symlink_args /* {
   1598 		syscallarg(const netbsd32_charp) path;
   1599 		syscallarg(const netbsd32_charp) link;
   1600 	} */ *uap = v;
   1601 	struct sys_symlink_args ua;
   1602 	caddr_t sg = stackgap_init(l->l_proc, 0);
   1603 
   1604 	NETBSD32TOP_UAP(path, const char);
   1605 	NETBSD32TOP_UAP(link, const char);
   1606 
   1607 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1608 	CHECK_ALT_CREAT(l, &sg, SCARG(&ua, link));
   1609 
   1610 	return sys_symlink(l, &ua, retval);
   1611 }
   1612 
   1613 
   1614 int
   1615 linux32_sys_swapon(l, v, retval)
   1616 	struct lwp *l;
   1617 	void *v;
   1618 	register_t *retval;
   1619 {
   1620 	struct linux32_sys_swapon_args /* {
   1621 		syscallarg(const netbsd32_charp) name;
   1622 	} */ *uap = v;
   1623 	struct sys_swapctl_args ua;
   1624 
   1625         SCARG(&ua, cmd) = SWAP_ON;
   1626         SCARG(&ua, arg) = (void *)__UNCONST(NETBSD32PTR64(SCARG(uap, name)));
   1627         SCARG(&ua, misc) = 0;   /* priority */
   1628         return (sys_swapctl(l, &ua, retval));
   1629 }
   1630 
   1631 int
   1632 linux32_sys_swapoff(l, v, retval)
   1633 	struct lwp *l;
   1634 	void *v;
   1635 	register_t *retval;
   1636 {
   1637 	struct linux32_sys_swapoff_args /* {
   1638 		syscallarg(const netbsd32_charp) path;
   1639 	} */ *uap = v;
   1640 	struct sys_swapctl_args ua;
   1641 
   1642         SCARG(&ua, cmd) = SWAP_OFF;
   1643         SCARG(&ua, arg) = (void *)__UNCONST(NETBSD32PTR64(SCARG(uap, path)));
   1644         SCARG(&ua, misc) = 0;   /* priority */
   1645         return (sys_swapctl(l, &ua, retval));
   1646 }
   1647 
   1648 
   1649 int
   1650 linux32_sys_reboot(l, v, retval)
   1651 	struct lwp *l;
   1652 	void *v;
   1653 	register_t *retval;
   1654 {
   1655 	struct linux32_sys_reboot_args /* {
   1656 		syscallarg(int) magic1;
   1657 		syscallarg(int) magic2;
   1658 		syscallarg(int) cmd;
   1659 		syscallarg(netbsd32_voidp) arg;
   1660 	} */ *uap = v;
   1661 	struct linux_sys_reboot_args ua;
   1662 
   1663 	NETBSD32TO64_UAP(magic1);
   1664 	NETBSD32TO64_UAP(magic2);
   1665 	NETBSD32TO64_UAP(cmd);
   1666 	NETBSD32TOP_UAP(arg, void);
   1667 
   1668 	return linux_sys_reboot(l, &ua, retval);
   1669 }
   1670 
   1671 int
   1672 linux32_sys_truncate(l, v, retval)
   1673 	struct lwp *l;
   1674 	void *v;
   1675 	register_t *retval;
   1676 {
   1677 	struct linux32_sys_truncate_args /* {
   1678 		syscallarg(const netbsd32_charp) path;
   1679 		syscallarg(netbsd32_charp) buf;
   1680 		syscallarg(int) count;
   1681 	} */ *uap = v;
   1682 	struct compat_43_sys_truncate_args ua;
   1683 
   1684 	NETBSD32TOP_UAP(path, const char);
   1685 	NETBSD32TO64_UAP(length);
   1686 
   1687 	return compat_43_sys_truncate(l, &ua, retval);
   1688 }
   1689 
   1690 int
   1691 linux32_sys_fchown16(l, v, retval)
   1692 	struct lwp *l;
   1693 	void *v;
   1694 	register_t *retval;
   1695 {
   1696 	struct linux32_sys_fchown16_args /* {
   1697 		syscallarg(int) fd;
   1698 		syscallarg(int) uid;
   1699 		syscallarg(int) gid;
   1700 	} */ *uap = v;
   1701         struct sys___posix_fchown_args ua;
   1702 
   1703 	SCARG(&ua, fd) = SCARG(uap, fd);
   1704 
   1705         if ((linux32_uid_t)SCARG(uap, uid) == (linux32_uid_t)-1)
   1706         	SCARG(&ua, uid) = (uid_t)-1;
   1707 	else
   1708         	SCARG(&ua, uid) = SCARG(uap, uid);
   1709 
   1710         if ((linux32_gid_t)SCARG(uap, gid) == (linux32_gid_t)-1)
   1711         	SCARG(&ua, gid) = (gid_t)-1;
   1712 	else
   1713         	SCARG(&ua, gid) = SCARG(uap, gid);
   1714 
   1715         return sys___posix_fchown(l, &ua, retval);
   1716 }
   1717