Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_50.c revision 1.37
      1 /*	$NetBSD: netbsd32_compat_50.c,v 1.37 2019/06/18 01:37:03 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_50.c,v 1.37 2019/06/18 01:37:03 christos Exp $");
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_compat_netbsd.h"
     43 #include "opt_ntp.h"
     44 #include "opt_quota.h"
     45 #endif
     46 
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/module.h>
     51 #include <sys/mount.h>
     52 #include <sys/socket.h>
     53 #include <sys/socketvar.h>
     54 #include <sys/stat.h>
     55 #include <sys/time.h>
     56 #include <sys/ktrace.h>
     57 #include <sys/eventvar.h>
     58 #include <sys/resourcevar.h>
     59 #include <sys/vnode.h>
     60 #include <sys/file.h>
     61 #include <sys/filedesc.h>
     62 #include <sys/poll.h>
     63 #include <sys/namei.h>
     64 #include <sys/statvfs.h>
     65 #include <sys/syscallargs.h>
     66 #include <sys/syscallvar.h>
     67 #include <sys/proc.h>
     68 #include <sys/dirent.h>
     69 #include <sys/kauth.h>
     70 #include <sys/vfs_syscalls.h>
     71 #include <sys/rnd.h>
     72 #include <sys/compat_stub.h>
     73 #include <sys/module_hook.h>
     74 
     75 #include <compat/netbsd32/netbsd32.h>
     76 #include <compat/netbsd32/netbsd32_syscall.h>
     77 #include <compat/netbsd32/netbsd32_syscallargs.h>
     78 #include <compat/netbsd32/netbsd32_conv.h>
     79 #include <compat/sys/mount.h>
     80 #include <compat/sys/time.h>
     81 #include <compat/sys/rnd.h>
     82 
     83 #if defined(COMPAT_50)
     84 
     85 /*
     86  * Common routine to set access and modification times given a vnode.
     87  */
     88 static int
     89 get_utimes32(const netbsd32_timeval50p_t *tptr, struct timeval *tv,
     90     struct timeval **tvp)
     91 {
     92 	int error;
     93 	struct netbsd32_timeval50 tv32[2];
     94 
     95 	if (tptr == NULL) {
     96 		*tvp = NULL;
     97 		return 0;
     98 	}
     99 
    100 	error = copyin(tptr, tv32, sizeof(tv32));
    101 	if (error)
    102 		return error;
    103 	netbsd32_to_timeval50(&tv32[0], &tv[0]);
    104 	netbsd32_to_timeval50(&tv32[1], &tv[1]);
    105 
    106 	*tvp = tv;
    107 	return 0;
    108 }
    109 
    110 int
    111 compat_50_netbsd32_mknod(struct lwp *l,
    112     const struct compat_50_netbsd32_mknod_args *uap, register_t *retval)
    113 {
    114 	/* {
    115 		syscallarg(netbsd32_charp) path;
    116 		syscallarg(mode_t) mode;
    117 		syscallarg(uint32_t) dev;
    118 	} */
    119 	return do_sys_mknod(l, SCARG_P32(uap, path), SCARG(uap, mode),
    120 	    SCARG(uap, dev), retval, UIO_USERSPACE);
    121 }
    122 
    123 int
    124 compat_50_netbsd32_select(struct lwp *l,
    125     const struct compat_50_netbsd32_select_args *uap, register_t *retval)
    126 {
    127 	/* {
    128 		syscallarg(int) nd;
    129 		syscallarg(netbsd32_fd_setp_t) in;
    130 		syscallarg(netbsd32_fd_setp_t) ou;
    131 		syscallarg(netbsd32_fd_setp_t) ex;
    132 		syscallarg(netbsd32_timeval50p_t) tv;
    133 	} */
    134 	int error;
    135 	struct netbsd32_timeval50 tv32;
    136 	struct timespec ats, *ts = NULL;
    137 
    138 	if (SCARG_P32(uap, tv)) {
    139 		error = copyin(SCARG_P32(uap, tv), &tv32, sizeof(tv32));
    140 		if (error != 0)
    141 			return error;
    142 		ats.tv_sec = tv32.tv_sec;
    143 		ats.tv_nsec = tv32.tv_usec * 1000;
    144 		ts = &ats;
    145 	}
    146 
    147 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
    148 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, NULL);
    149 }
    150 
    151 int
    152 compat_50_netbsd32_gettimeofday(struct lwp *l,
    153     const struct compat_50_netbsd32_gettimeofday_args *uap, register_t *retval)
    154 {
    155 	/* {
    156 		syscallarg(netbsd32_timeval50p_t) tp;
    157 		syscallarg(netbsd32_timezonep_t) tzp;
    158 	} */
    159 	struct timeval atv;
    160 	struct netbsd32_timeval50 tv32;
    161 	int error = 0;
    162 	struct netbsd32_timezone tzfake;
    163 
    164 	if (SCARG_P32(uap, tp)) {
    165 		microtime(&atv);
    166 		netbsd32_from_timeval50(&atv, &tv32);
    167 		error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
    168 		if (error)
    169 			return error;
    170 	}
    171 	if (SCARG_P32(uap, tzp)) {
    172 		/*
    173 		 * NetBSD has no kernel notion of time zone, so we just
    174 		 * fake up a timezone struct and return it if demanded.
    175 		 */
    176 		tzfake.tz_minuteswest = 0;
    177 		tzfake.tz_dsttime = 0;
    178 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
    179 	}
    180 	return error;
    181 }
    182 
    183 int
    184 compat_50_netbsd32_settimeofday(struct lwp *l,
    185     const struct compat_50_netbsd32_settimeofday_args *uap, register_t *retval)
    186 {
    187 	/* {
    188 		syscallarg(const netbsd32_timeval50p_t) tv;
    189 		syscallarg(const netbsd32_timezonep_t) tzp;
    190 	} */
    191 	struct netbsd32_timeval50 atv32;
    192 	struct timeval atv;
    193 	struct timespec ats;
    194 	int error;
    195 	struct proc *p = l->l_proc;
    196 
    197 	/* Verify all parameters before changing time. */
    198 
    199 	/*
    200 	 * NetBSD has no kernel notion of time zone, and only an
    201 	 * obsolete program would try to set it, so we log a warning.
    202 	 */
    203 	if (SCARG_P32(uap, tzp))
    204 		printf("pid %d attempted to set the "
    205 		    "(obsolete) kernel time zone\n", p->p_pid);
    206 
    207 	if (SCARG_P32(uap, tv) == 0)
    208 		return 0;
    209 
    210 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
    211 		return error;
    212 
    213 	netbsd32_to_timeval50(&atv32, &atv);
    214 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
    215 	return settime(p, &ats);
    216 }
    217 
    218 int
    219 compat_50_netbsd32_utimes(struct lwp *l,
    220     const struct compat_50_netbsd32_utimes_args *uap, register_t *retval)
    221 {
    222 	/* {
    223 		syscallarg(const netbsd32_charp) path;
    224 		syscallarg(const netbsd32_timeval50p_t) tptr;
    225 	} */
    226 	int error;
    227 	struct timeval tv[2], *tvp;
    228 
    229 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    230 	if (error != 0)
    231 		return error;
    232 
    233 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
    234 	    tvp, UIO_SYSSPACE);
    235 }
    236 
    237 int
    238 compat_50_netbsd32_adjtime(struct lwp *l,
    239     const struct compat_50_netbsd32_adjtime_args *uap, register_t *retval)
    240 {
    241 	/* {
    242 		syscallarg(const netbsd32_timeval50p_t) delta;
    243 		syscallarg(netbsd32_timeval50p_t) olddelta;
    244 	} */
    245 	struct netbsd32_timeval50 atv;
    246 	int error;
    247 
    248 	extern int time_adjusted;     /* in kern_ntptime.c */
    249 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
    250 
    251 	if ((error = kauth_authorize_system(l->l_cred,
    252 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
    253 	    NULL)) != 0)
    254 		return (error);
    255 
    256 	if (SCARG_P32(uap, olddelta)) {
    257 		mutex_spin_enter(&timecounter_lock);
    258 		atv.tv_sec = time_adjtime / 1000000;
    259 		atv.tv_usec = time_adjtime % 1000000;
    260 		if (atv.tv_usec < 0) {
    261 			atv.tv_usec += 1000000;
    262 			atv.tv_sec--;
    263 		}
    264 		mutex_spin_exit(&timecounter_lock);
    265 
    266 		error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv));
    267 		if (error)
    268 			return (error);
    269 	}
    270 
    271 	if (SCARG_P32(uap, delta)) {
    272 		error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv));
    273 		if (error)
    274 			return (error);
    275 
    276 		mutex_spin_enter(&timecounter_lock);
    277 		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
    278 		if (time_adjtime)
    279 			/* We need to save the system time during shutdown */
    280 			time_adjusted |= 1;
    281 		mutex_spin_exit(&timecounter_lock);
    282 	}
    283 
    284 	return 0;
    285 }
    286 
    287 int
    288 compat_50_netbsd32_futimes(struct lwp *l,
    289     const struct compat_50_netbsd32_futimes_args *uap, register_t *retval)
    290 {
    291 	/* {
    292 		syscallarg(int) fd;
    293 		syscallarg(const netbsd32_timeval50p_t) tptr;
    294 	} */
    295 	int error;
    296 	file_t *fp;
    297 	struct timeval tv[2], *tvp;
    298 
    299 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    300 	if (error != 0)
    301 		return error;
    302 
    303 	/* fd_getvnode() will use the descriptor for us */
    304 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    305 		return error;
    306 
    307 	error = do_sys_utimes(l, fp->f_vnode, NULL, 0, tvp, UIO_SYSSPACE);
    308 
    309 	fd_putfile(SCARG(uap, fd));
    310 	return error;
    311 }
    312 
    313 int
    314 compat_50_netbsd32_clock_gettime(struct lwp *l,
    315     const struct compat_50_netbsd32_clock_gettime_args *uap, register_t *retval)
    316 {
    317 	/* {
    318 		syscallarg(netbsd32_clockid_t) clock_id;
    319 		syscallarg(netbsd32_timespec50p_t) tp;
    320 	} */
    321 	int error;
    322 	struct timespec ats;
    323 	struct netbsd32_timespec50 ts32;
    324 
    325 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
    326 	if (error != 0)
    327 		return error;
    328 
    329 	netbsd32_from_timespec50(&ats, &ts32);
    330 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    331 }
    332 
    333 int
    334 compat_50_netbsd32_clock_settime(struct lwp *l,
    335     const struct compat_50_netbsd32_clock_settime_args *uap, register_t *retval)
    336 {
    337 	/* {
    338 		syscallarg(netbsd32_clockid_t) clock_id;
    339 		syscallarg(const netbsd32_timespec50p_t) tp;
    340 	} */
    341 	struct netbsd32_timespec50 ts32;
    342 	struct timespec ats;
    343 	int error;
    344 
    345 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
    346 		return (error);
    347 
    348 	netbsd32_to_timespec50(&ts32, &ats);
    349 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
    350 }
    351 
    352 int
    353 compat_50_netbsd32_clock_getres(struct lwp *l,
    354     const struct compat_50_netbsd32_clock_getres_args *uap, register_t *retval)
    355 {
    356 	/* {
    357 		syscallarg(netbsd32_clockid_t) clock_id;
    358 		syscallarg(netbsd32_timespec50p_t) tp;
    359 	} */
    360 	struct netbsd32_timespec50 ts32;
    361 	struct timespec ts;
    362 	int error;
    363 
    364 	error = clock_getres1(SCARG(uap, clock_id), &ts);
    365 	if (error != 0)
    366 		return error;
    367 
    368 	if (SCARG_P32(uap, tp)) {
    369 		netbsd32_from_timespec50(&ts, &ts32);
    370 		error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    371 	}
    372 
    373 	return error;
    374 }
    375 
    376 int
    377 compat_50_netbsd32_timer_settime(struct lwp *l,
    378     const struct compat_50_netbsd32_timer_settime_args *uap, register_t *retval)
    379 {
    380 	/* {
    381 		syscallarg(netbsd32_timer_t) timerid;
    382 		syscallarg(int) flags;
    383 		syscallarg(const netbsd32_itimerspec50p_t) value;
    384 		syscallarg(netbsd32_itimerspec50p_t) ovalue;
    385 	} */
    386 	int error;
    387 	struct itimerspec value, ovalue, *ovp = NULL;
    388 	struct netbsd32_itimerspec50 its32;
    389 
    390 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
    391 		return (error);
    392 	netbsd32_to_timespec50(&its32.it_interval, &value.it_interval);
    393 	netbsd32_to_timespec50(&its32.it_value, &value.it_value);
    394 
    395 	if (SCARG_P32(uap, ovalue))
    396 		ovp = &ovalue;
    397 
    398 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    399 	    SCARG(uap, flags), l->l_proc)) != 0)
    400 		return error;
    401 
    402 	if (ovp) {
    403 		netbsd32_from_timespec50(&ovp->it_interval, &its32.it_interval);
    404 		netbsd32_from_timespec50(&ovp->it_value, &its32.it_value);
    405 		return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
    406 	}
    407 	return 0;
    408 }
    409 
    410 int
    411 compat_50_netbsd32_timer_gettime(struct lwp *l, const struct compat_50_netbsd32_timer_gettime_args *uap, register_t *retval)
    412 {
    413 	/* {
    414 		syscallarg(netbsd32_timer_t) timerid;
    415 		syscallarg(netbsd32_itimerspec50p_t) value;
    416 	} */
    417 	int error;
    418 	struct itimerspec its;
    419 	struct netbsd32_itimerspec50 its32;
    420 
    421 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    422 	    &its)) != 0)
    423 		return error;
    424 
    425 	netbsd32_from_timespec50(&its.it_interval, &its32.it_interval);
    426 	netbsd32_from_timespec50(&its.it_value, &its32.it_value);
    427 
    428 	return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
    429 }
    430 
    431 int
    432 compat_50_netbsd32_nanosleep(struct lwp *l,
    433     const struct compat_50_netbsd32_nanosleep_args *uap, register_t *retval)
    434 {
    435 	/* {
    436 		syscallarg(const netbsd32_timespec50p_t) rqtp;
    437 		syscallarg(netbsd32_timespecp_t) rmtp;
    438 	} */
    439 	struct netbsd32_timespec50 ts32;
    440 	struct timespec rqt, rmt;
    441 	int error, error1;
    442 
    443 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
    444 	if (error)
    445 		return (error);
    446 	netbsd32_to_timespec50(&ts32, &rqt);
    447 
    448 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
    449 	    SCARG_P32(uap, rmtp) ? &rmt : NULL);
    450 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    451 		return error;
    452 
    453 	netbsd32_from_timespec50(&rmt, &ts32);
    454 	error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
    455 	return error1 ? error1 : error;
    456 }
    457 
    458 static int
    459 compat_50_netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
    460 {
    461 	const siginfo_t *info = src;
    462 	siginfo32_t info32;
    463 
    464 	netbsd32_si_to_si32(&info32, info);
    465 
    466 	return copyout(&info32, dst, sizeof(info32));
    467 }
    468 
    469 static int
    470 compat_50_netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
    471 {
    472 	struct timespec *ts = dst;
    473 	struct netbsd32_timespec50 ts32;
    474 	int error;
    475 
    476 	error = copyin(src, &ts32, sizeof(ts32));
    477 	if (error)
    478 		return error;
    479 
    480 	netbsd32_to_timespec50(&ts32, ts);
    481 	return 0;
    482 }
    483 
    484 static int
    485 compat_50_netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
    486 {
    487 	const struct timespec *ts = src;
    488 	struct netbsd32_timespec50 ts32;
    489 
    490 	netbsd32_from_timespec50(ts, &ts32);
    491 
    492 	return copyout(&ts32, dst, sizeof(ts32));
    493 }
    494 
    495 int
    496 compat_50_netbsd32___sigtimedwait(struct lwp *l,
    497     const struct compat_50_netbsd32___sigtimedwait_args *uap, register_t *retval)
    498 {
    499 	/* {
    500 		syscallarg(netbsd32_sigsetp_t) set;
    501 		syscallarg(netbsd32_siginfop_t) info;
    502 		syscallarg(netbsd32_timespec50p_t) timeout;
    503 	} */
    504 	struct sys_____sigtimedwait50_args ua;
    505 	int res;
    506 
    507 	NETBSD32TOP_UAP(set, const sigset_t);
    508 	NETBSD32TOP_UAP(info, siginfo_t);
    509 	NETBSD32TOP_UAP(timeout, struct timespec);
    510 
    511 	res = sigtimedwait1(l, &ua, retval,
    512 	    copyin,
    513 	    compat_50_netbsd32_sigtimedwait_put_info,
    514 	    compat_50_netbsd32_sigtimedwait_fetch_timeout,
    515 	    compat_50_netbsd32_sigtimedwait_put_timeout);
    516 	if (!res)
    517 		*retval = 0; /* NetBSD<=5 was not POSIX compliant */
    518 	return res;
    519 }
    520 
    521 int
    522 compat_50_netbsd32_lutimes(struct lwp *l,
    523     const struct compat_50_netbsd32_lutimes_args *uap, register_t *retval)
    524 {
    525 	/* {
    526 		syscallarg(const netbsd32_charp) path;
    527 		syscallarg(const netbsd32_timeval50p_t) tptr;
    528 	} */
    529 	int error;
    530 	struct timeval tv[2], *tvp;
    531 
    532 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    533 	if (error != 0)
    534 		return error;
    535 
    536 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
    537 	    tvp, UIO_SYSSPACE);
    538 }
    539 
    540 int
    541 compat_50_netbsd32__lwp_park(struct lwp *l,
    542     const struct compat_50_netbsd32__lwp_park_args *uap, register_t *retval)
    543 {
    544 	/* {
    545 		syscallarg(const netbsd32_timespec50p) ts;
    546 		syscallarg(lwpid_t) unpark;
    547 		syscallarg(netbsd32_voidp) hint;
    548 		syscallarg(netbsd32_voidp) unparkhint;
    549 	} */
    550 	struct timespec ts, *tsp;
    551 	struct netbsd32_timespec50 ts32;
    552 	int error;
    553 
    554 	if (SCARG_P32(uap, ts) == NULL)
    555 		tsp = NULL;
    556 	else {
    557 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof ts32);
    558 		if (error != 0)
    559 			return error;
    560 		netbsd32_to_timespec50(&ts32, &ts);
    561 		tsp = &ts;
    562 	}
    563 
    564 	if (SCARG(uap, unpark) != 0) {
    565 		error = lwp_unpark(SCARG(uap, unpark),
    566 		    SCARG_P32(uap, unparkhint));
    567 		if (error != 0)
    568 			return error;
    569 	}
    570 
    571 	return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp,
    572 	    SCARG_P32(uap, hint));
    573 }
    574 
    575 static int
    576 netbsd32_kevent_fetch_timeout(const void *src, void *dest, size_t length)
    577 {
    578 	struct netbsd32_timespec50 ts32;
    579 	int error;
    580 
    581 	KASSERT(length == sizeof(struct timespec50));
    582 
    583 	error = copyin(src, &ts32, sizeof(ts32));
    584 	if (error)
    585 		return error;
    586 	netbsd32_to_timespec50(&ts32, (struct timespec *)dest);
    587 	return 0;
    588 }
    589 
    590 static int
    591 netbsd32_kevent_fetch_changes(void *ctx, const struct kevent *changelist,
    592     struct kevent *changes, size_t index, int n)
    593 {
    594 	const struct netbsd32_kevent *src =
    595 	    (const struct netbsd32_kevent *)changelist;
    596 	struct netbsd32_kevent *kev32, *changes32 = ctx;
    597 	int error, i;
    598 
    599 	error = copyin(src + index, changes32, n * sizeof(*changes32));
    600 	if (error)
    601 		return error;
    602 	for (i = 0, kev32 = changes32; i < n; i++, kev32++, changes++)
    603 		netbsd32_to_kevent(kev32, changes);
    604 	return 0;
    605 }
    606 
    607 static int
    608 netbsd32_kevent_put_events(void *ctx, struct kevent *events,
    609     struct kevent *eventlist, size_t index, int n)
    610 {
    611 	struct netbsd32_kevent *kev32, *events32 = ctx;
    612 	int i;
    613 
    614 	for (i = 0, kev32 = events32; i < n; i++, kev32++, events++)
    615 		netbsd32_from_kevent(events, kev32);
    616 	kev32 = (struct netbsd32_kevent *)eventlist;
    617 	return  copyout(events32, kev32, n * sizeof(*events32));
    618 }
    619 
    620 int
    621 compat_50_netbsd32_kevent(struct lwp *l,
    622     const struct compat_50_netbsd32_kevent_args *uap, register_t *retval)
    623 {
    624 	/* {
    625 		syscallarg(int) fd;
    626 		syscallarg(netbsd32_keventp_t) changelist;
    627 		syscallarg(netbsd32_size_t) nchanges;
    628 		syscallarg(netbsd32_keventp_t) eventlist;
    629 		syscallarg(netbsd32_size_t) nevents;
    630 		syscallarg(netbsd32_timespec50p_t) timeout;
    631 	} */
    632 	int error;
    633 	size_t maxalloc, nchanges, nevents;
    634 	struct kevent_ops netbsd32_kevent_ops = {
    635 		.keo_fetch_timeout = netbsd32_kevent_fetch_timeout,
    636 		.keo_fetch_changes = netbsd32_kevent_fetch_changes,
    637 		.keo_put_events = netbsd32_kevent_put_events,
    638 	};
    639 
    640 	nchanges = SCARG(uap, nchanges);
    641 	nevents = SCARG(uap, nevents);
    642 	maxalloc = KQ_NEVENTS;
    643 
    644 	netbsd32_kevent_ops.keo_private =
    645 	    kmem_alloc(maxalloc * sizeof(struct netbsd32_kevent), KM_SLEEP);
    646 
    647 	error = kevent1(retval, SCARG(uap, fd),
    648 	    NETBSD32PTR64(SCARG(uap, changelist)), nchanges,
    649 	    NETBSD32PTR64(SCARG(uap, eventlist)), nevents,
    650 	    NETBSD32PTR64(SCARG(uap, timeout)), &netbsd32_kevent_ops);
    651 
    652 	kmem_free(netbsd32_kevent_ops.keo_private,
    653 	    maxalloc * sizeof(struct netbsd32_kevent));
    654 	return error;
    655 }
    656 
    657 int
    658 compat_50_netbsd32_pselect(struct lwp *l,
    659     const struct compat_50_netbsd32_pselect_args *uap, register_t *retval)
    660 {
    661 	/* {
    662 		syscallarg(int) nd;
    663 		syscallarg(netbsd32_fd_setp_t) in;
    664 		syscallarg(netbsd32_fd_setp_t) ou;
    665 		syscallarg(netbsd32_fd_setp_t) ex;
    666 		syscallarg(const netbsd32_timespec50p_t) ts;
    667 		syscallarg(const netbsd32_sigsetp_t) mask;
    668 	} */
    669 	int error;
    670 	struct netbsd32_timespec50 ts32;
    671 	struct timespec ats, *ts = NULL;
    672 	sigset_t amask, *mask = NULL;
    673 
    674 	if (SCARG_P32(uap, ts)) {
    675 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    676 		if (error != 0)
    677 			return error;
    678 		netbsd32_to_timespec50(&ts32, &ats);
    679 		ts = &ats;
    680 	}
    681 	if (SCARG_P32(uap, mask)) {
    682 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    683 		if (error != 0)
    684 			return error;
    685 		mask = &amask;
    686 	}
    687 
    688 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
    689 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, mask);
    690 }
    691 
    692 int
    693 compat_50_netbsd32_pollts(struct lwp *l,
    694     const struct compat_50_netbsd32_pollts_args *uap, register_t *retval)
    695 {
    696 	/* {
    697 		syscallarg(struct netbsd32_pollfdp_t) fds;
    698 		syscallarg(u_int) nfds;
    699 		syscallarg(const netbsd32_timespec50p_t) ts;
    700 		syscallarg(const netbsd32_sigsetp_t) mask;
    701 	} */
    702 	int error;
    703 	struct netbsd32_timespec50 ts32;
    704 	struct timespec ats, *ts = NULL;
    705 	sigset_t amask, *mask = NULL;
    706 
    707 	if (SCARG_P32(uap, ts)) {
    708 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    709 		if (error != 0)
    710 			return error;
    711 		netbsd32_to_timespec50(&ts32, &ats);
    712 		ts = &ats;
    713 	}
    714 	if (NETBSD32PTR64( SCARG(uap, mask))) {
    715 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    716 		if (error != 0)
    717 			return error;
    718 		mask = &amask;
    719 	}
    720 
    721 	return pollcommon(retval, SCARG_P32(uap, fds),
    722 	    SCARG(uap, nfds), ts, mask);
    723 }
    724 
    725 int
    726 compat_50_netbsd32___stat30(struct lwp *l,
    727     const struct compat_50_netbsd32___stat30_args *uap, register_t *retval)
    728 {
    729 	/* {
    730 		syscallarg(const netbsd32_charp) path;
    731 		syscallarg(netbsd32_stat50p_t) ub;
    732 	} */
    733 	struct netbsd32_stat50 sb32;
    734 	struct stat sb;
    735 	int error;
    736 	const char *path;
    737 
    738 	path = SCARG_P32(uap, path);
    739 
    740 	error = do_sys_stat(path, FOLLOW, &sb);
    741 	if (error)
    742 		return error;
    743 	netbsd32_from___stat50(&sb, &sb32);
    744 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    745 	return error;
    746 }
    747 
    748 int
    749 compat_50_netbsd32___fstat30(struct lwp *l,
    750     const struct compat_50_netbsd32___fstat30_args *uap, register_t *retval)
    751 {
    752 	/* {
    753 		syscallarg(int) fd;
    754 		syscallarg(netbsd32_stat50p_t) sb;
    755 	} */
    756 	struct netbsd32_stat50 sb32;
    757 	struct stat ub;
    758 	int error;
    759 
    760 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    761 	if (error == 0) {
    762 		netbsd32_from___stat50(&ub, &sb32);
    763 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    764 	}
    765 	return error;
    766 }
    767 
    768 int
    769 compat_50_netbsd32___lstat30(struct lwp *l,
    770     const struct compat_50_netbsd32___lstat30_args *uap, register_t *retval)
    771 {
    772 	/* {
    773 		syscallarg(const netbsd32_charp) path;
    774 		syscallarg(netbsd32_stat50p_t) ub;
    775 	} */
    776 	struct netbsd32_stat50 sb32;
    777 	struct stat sb;
    778 	int error;
    779 	const char *path;
    780 
    781 	path = SCARG_P32(uap, path);
    782 
    783 	error = do_sys_stat(path, NOFOLLOW, &sb);
    784 	if (error)
    785 		return error;
    786 	netbsd32_from___stat50(&sb, &sb32);
    787 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    788 	return error;
    789 }
    790 
    791 int
    792 compat_50_netbsd32___fhstat40(struct lwp *l, const struct compat_50_netbsd32___fhstat40_args *uap, register_t *retval)
    793 {
    794 	/* {
    795 		syscallarg(const netbsd32_pointer_t) fhp;
    796 		syscallarg(netbsd32_size_t) fh_size;
    797 		syscallarg(netbsd32_stat50p_t) sb;
    798 	} */
    799 	struct stat sb;
    800 	struct netbsd32_stat50 sb32;
    801 	int error;
    802 
    803 	error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
    804 	if (error != 0) {
    805 		netbsd32_from___stat50(&sb, &sb32);
    806 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
    807 	}
    808 	return error;
    809 }
    810 
    811 int
    812 compat_50_netbsd32_wait4(struct lwp *l, const struct compat_50_netbsd32_wait4_args *uap, register_t *retval)
    813 {
    814 	/* {
    815 		syscallarg(int) pid;
    816 		syscallarg(netbsd32_intp) status;
    817 		syscallarg(int) options;
    818 		syscallarg(netbsd32_rusage50p_t) rusage;
    819 	} */
    820 	int error, status, pid = SCARG(uap, pid);
    821 	struct netbsd32_rusage50 ru32;
    822 	struct rusage ru;
    823 
    824 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
    825 	    SCARG_P32(uap, rusage) != NULL ? &ru : NULL);
    826 
    827 	retval[0] = pid;
    828 	if (pid == 0)
    829 		return error;
    830 
    831 	if (SCARG_P32(uap, rusage)) {
    832 		netbsd32_from_rusage50(&ru, &ru32);
    833 		error = copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    834 	}
    835 
    836 	if (error == 0 && SCARG_P32(uap, status))
    837 		error = copyout(&status, SCARG_P32(uap, status), sizeof(status));
    838 
    839 	return error;
    840 }
    841 
    842 
    843 int
    844 compat_50_netbsd32_getrusage(struct lwp *l, const struct compat_50_netbsd32_getrusage_args *uap, register_t *retval)
    845 {
    846 	/* {
    847 		syscallarg(int) who;
    848 		syscallarg(netbsd32_rusage50p_t) rusage;
    849 	} */
    850 	int error;
    851 	struct proc *p = l->l_proc;
    852 	struct rusage ru;
    853 	struct netbsd32_rusage50 ru32;
    854 
    855 	error = getrusage1(p, SCARG(uap, who), &ru);
    856 	if (error != 0)
    857 		return error;
    858 
    859 	netbsd32_from_rusage50(&ru, &ru32);
    860 	return copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    861 }
    862 
    863 int
    864 compat_50_netbsd32_setitimer(struct lwp *l,
    865     const struct compat_50_netbsd32_setitimer_args *uap, register_t *retval)
    866 {
    867 	/* {
    868 		syscallarg(int) which;
    869 		syscallarg(const netbsd32_itimerval50p_t) itv;
    870 		syscallarg(netbsd32_itimerval50p_t) oitv;
    871 	} */
    872 	struct proc *p = l->l_proc;
    873 	struct netbsd32_itimerval50 s32it, *itv32;
    874 	int which = SCARG(uap, which);
    875 	struct compat_50_netbsd32_getitimer_args getargs;
    876 	struct itimerval aitv;
    877 	int error;
    878 
    879 	if ((u_int)which > ITIMER_PROF)
    880 		return (EINVAL);
    881 	itv32 = SCARG_P32(uap, itv);
    882 	if (itv32) {
    883 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
    884 			return (error);
    885 		netbsd32_to_itimerval50(&s32it, &aitv);
    886 	}
    887 	if (SCARG_P32(uap, oitv) != 0) {
    888 		SCARG(&getargs, which) = which;
    889 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    890 		if ((error = compat_50_netbsd32_getitimer(l, &getargs, retval)) != 0)
    891 			return (error);
    892 	}
    893 	if (itv32 == 0)
    894 		return 0;
    895 
    896 	return dosetitimer(p, which, &aitv);
    897 }
    898 
    899 int
    900 compat_50_netbsd32_getitimer(struct lwp *l, const struct compat_50_netbsd32_getitimer_args *uap, register_t *retval)
    901 {
    902 	/* {
    903 		syscallarg(int) which;
    904 		syscallarg(netbsd32_itimerval50p_t) itv;
    905 	} */
    906 	struct proc *p = l->l_proc;
    907 	struct netbsd32_itimerval50 s32it;
    908 	struct itimerval aitv;
    909 	int error;
    910 
    911 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    912 	if (error)
    913 		return error;
    914 
    915 	netbsd32_from_itimerval50(&aitv, &s32it);
    916 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
    917 }
    918 
    919 #ifdef QUOTA
    920 int
    921 compat_50_netbsd32_quotactl(struct lwp *l, const struct compat_50_netbsd32_quotactl_args *uap, register_t *retval)
    922 {
    923 	/* {
    924 		syscallarg(const netbsd32_charp) path;
    925 		syscallarg(int) cmd;
    926 		syscallarg(int) uid;
    927 		syscallarg(netbsd32_voidp) arg;
    928 	} */
    929 	struct compat_50_sys_quotactl_args ua;
    930 
    931 	NETBSD32TOP_UAP(path, const char);
    932 	NETBSD32TO64_UAP(cmd);
    933 	NETBSD32TO64_UAP(uid);
    934 	NETBSD32TOP_UAP(arg, void *);
    935 	return (compat_50_sys_quotactl(l, &ua, retval));
    936 }
    937 #endif
    938 
    939 #ifdef NTP
    940 int
    941 compat_50_netbsd32_ntp_gettime(struct lwp *l,
    942     const struct compat_50_netbsd32_ntp_gettime_args *uap, register_t *retval)
    943 {
    944 	/* {
    945 		syscallarg(netbsd32_ntptimeval50p_t) ntvp;
    946 	} */
    947 	struct netbsd32_ntptimeval50 ntv32;
    948 	struct ntptimeval ntv;
    949 	int error = 0;
    950 
    951 	if (vec_ntp_gettime == NULL)
    952 		return EINVAL;
    953 
    954 	if (SCARG_P32(uap, ntvp)) {
    955 		(*vec_ntp_gettime)(&ntv);
    956 
    957 		memset(&ntv32, 0, sizeof(ntv32));
    958 		ntv32.time.tv_sec = (int32_t)ntv.time.tv_sec;
    959 		ntv32.time.tv_nsec = ntv.time.tv_nsec;
    960 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
    961 		ntv32.esterror = (netbsd32_long)ntv.esterror;
    962 		ntv32.tai = (netbsd32_long)ntv.tai;
    963 		ntv32.time_state = ntv.time_state;
    964 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
    965 	}
    966 	if (!error) {
    967 		*retval = (*vec_ntp_timestatus)();
    968 	}
    969 
    970 	return (error);
    971 }
    972 #endif
    973 
    974 static struct syscall_package compat_netbsd32_50_syscalls[] = {
    975 	{ NETBSD32_SYS_compat_50_netbsd32_mknod, 0,
    976 	    (sy_call_t *)compat_50_netbsd32_mknod },
    977 	{ NETBSD32_SYS_compat_50_netbsd32_select, 0,
    978 	    (sy_call_t *)compat_50_netbsd32_select },
    979 	{ NETBSD32_SYS_compat_50_netbsd32_gettimeofday, 0,
    980 	    (sy_call_t *)compat_50_netbsd32_gettimeofday },
    981 	{ NETBSD32_SYS_compat_50_netbsd32_settimeofday, 0,
    982 	    (sy_call_t *)compat_50_netbsd32_settimeofday },
    983 	{ NETBSD32_SYS_compat_50_netbsd32_utimes, 0,
    984 	    (sy_call_t *)compat_50_netbsd32_utimes },
    985 	{ NETBSD32_SYS_compat_50_netbsd32_futimes, 0,
    986 	    (sy_call_t *)compat_50_netbsd32_futimes },
    987 	{ NETBSD32_SYS_compat_50_netbsd32_adjtime, 0,
    988 	    (sy_call_t *)compat_50_netbsd32_adjtime },
    989 	{ NETBSD32_SYS_compat_50_netbsd32_clock_gettime, 0,
    990 	    (sy_call_t *)compat_50_netbsd32_clock_gettime },
    991 	{ NETBSD32_SYS_compat_50_netbsd32_clock_settime, 0,
    992 	    (sy_call_t *)compat_50_netbsd32_clock_settime },
    993 	{ NETBSD32_SYS_compat_50_netbsd32_clock_getres, 0,
    994 	    (sy_call_t *)compat_50_netbsd32_clock_getres },
    995 	{ NETBSD32_SYS_compat_50_netbsd32_timer_settime, 0,
    996 	    (sy_call_t *)compat_50_netbsd32_timer_settime },
    997 	{ NETBSD32_SYS_compat_50_netbsd32_timer_gettime, 0,
    998 	    (sy_call_t *)compat_50_netbsd32_timer_gettime },
    999 	{ NETBSD32_SYS_compat_50_netbsd32_nanosleep, 0,
   1000 	    (sy_call_t *)compat_50_netbsd32_nanosleep },
   1001 	{ NETBSD32_SYS_compat_50_netbsd32___sigtimedwait, 0,
   1002 	    (sy_call_t *)compat_50_netbsd32___sigtimedwait },
   1003 	{ NETBSD32_SYS_compat_50_netbsd32_lutimes, 0,
   1004 	    (sy_call_t *)compat_50_netbsd32_lutimes },
   1005 	{ NETBSD32_SYS_compat_50_netbsd32__lwp_park, 0,
   1006 	    (sy_call_t *)compat_50_netbsd32__lwp_park },
   1007 	{ NETBSD32_SYS_compat_50_netbsd32_kevent, 0,
   1008 	    (sy_call_t *)compat_50_netbsd32_kevent },
   1009 	{ NETBSD32_SYS_compat_50_netbsd32_pselect, 0,
   1010 	    (sy_call_t *)compat_50_netbsd32_pselect },
   1011 	{ NETBSD32_SYS_compat_50_netbsd32_pollts, 0,
   1012 	    (sy_call_t *)compat_50_netbsd32_pollts },
   1013 	{ NETBSD32_SYS_compat_50_netbsd32___stat30, 0,
   1014 	    (sy_call_t *)compat_50_netbsd32___stat30 },
   1015 	{ NETBSD32_SYS_compat_50_netbsd32___fstat30, 0,
   1016 	    (sy_call_t *)compat_50_netbsd32___fstat30 },
   1017 	{ NETBSD32_SYS_compat_50_netbsd32___lstat30, 0,
   1018 	    (sy_call_t *)compat_50_netbsd32___lstat30 },
   1019 	{ NETBSD32_SYS_compat_50_netbsd32___fhstat40, 0,
   1020 	    (sy_call_t *)compat_50_netbsd32___fhstat40 },
   1021 	{ NETBSD32_SYS_compat_50_netbsd32_wait4, 0,
   1022 	    (sy_call_t *)compat_50_netbsd32_wait4 },
   1023 	{ NETBSD32_SYS_compat_50_netbsd32_getrusage, 0,
   1024 	    (sy_call_t *)compat_50_netbsd32_getrusage },
   1025 	{ NETBSD32_SYS_compat_50_netbsd32_setitimer, 0,
   1026 	    (sy_call_t *)compat_50_netbsd32_setitimer },
   1027 	{ NETBSD32_SYS_compat_50_netbsd32_getitimer, 0,
   1028 	    (sy_call_t *)compat_50_netbsd32_getitimer },
   1029 #ifdef QUOTA
   1030 	{ NETBSD32_SYS_compat_50_netbsd32_quotactl, 0,
   1031 	    (sy_call_t *)compat_50_netbsd32_quotactl },
   1032 #endif
   1033 #ifdef NTP
   1034 	{ NETBSD32_SYS_compat_50_netbsd32_ntp_gettime, 0,
   1035 	    (sy_call_t *)compat_50_netbsd32_ntp_gettime },
   1036 #endif
   1037 	{ 0, 0, NULL }
   1038 };
   1039 
   1040 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_50, "compat_netbsd32_60,compat_50");
   1041 
   1042 static int
   1043 compat_netbsd32_50_modcmd(modcmd_t cmd, void *arg)
   1044 {
   1045 	int ret;
   1046 
   1047 	switch (cmd) {
   1048 	case MODULE_CMD_INIT:
   1049                 ret = syscall_establish(&emul_netbsd32,
   1050 		    compat_netbsd32_50_syscalls);
   1051 		if (ret == 0)
   1052 			MODULE_HOOK_SET(rnd_ioctl32_50_hook, "rnd32_50",
   1053 			    compat32_50_rnd_ioctl);
   1054 		return ret;
   1055 
   1056 	case MODULE_CMD_FINI:
   1057                 ret = syscall_disestablish(&emul_netbsd32,
   1058 		    compat_netbsd32_50_syscalls);
   1059 		if (ret == 0)
   1060 			MODULE_HOOK_UNSET(rnd_ioctl32_50_hook);
   1061 		return ret;
   1062 
   1063 	default:
   1064 		return ENOTTY;
   1065 	}
   1066 }
   1067 #endif /* COMPAT_50 */
   1068