Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_50.c revision 1.39.2.3
      1 /*	$NetBSD: netbsd32_compat_50.c,v 1.39.2.3 2022/08/03 11:11:31 martin 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_50.c,v 1.39.2.3 2022/08/03 11:11:31 martin Exp $");
     33 
     34 #if defined(_KERNEL_OPT)
     35 #include "opt_compat_netbsd.h"
     36 #include "opt_compat_netbsd32.h"
     37 #include "opt_ntp.h"
     38 #include "opt_quota.h"
     39 #endif
     40 
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/module.h>
     45 #include <sys/mount.h>
     46 #include <sys/socket.h>
     47 #include <sys/socketvar.h>
     48 #include <sys/stat.h>
     49 #include <sys/time.h>
     50 #include <sys/ktrace.h>
     51 #include <sys/eventvar.h>
     52 #include <sys/resourcevar.h>
     53 #include <sys/vnode.h>
     54 #include <sys/file.h>
     55 #include <sys/filedesc.h>
     56 #include <sys/poll.h>
     57 #include <sys/namei.h>
     58 #include <sys/statvfs.h>
     59 #include <sys/syscallargs.h>
     60 #include <sys/syscallvar.h>
     61 #include <sys/proc.h>
     62 #include <sys/dirent.h>
     63 #include <sys/kauth.h>
     64 #include <sys/vfs_syscalls.h>
     65 #include <sys/rnd.h>
     66 #include <sys/compat_stub.h>
     67 #include <sys/module_hook.h>
     68 
     69 #include <compat/netbsd32/netbsd32.h>
     70 #include <compat/netbsd32/netbsd32_syscall.h>
     71 #include <compat/netbsd32/netbsd32_syscallargs.h>
     72 #include <compat/netbsd32/netbsd32_conv.h>
     73 #include <compat/sys/mount.h>
     74 #include <compat/sys/time.h>
     75 #include <compat/sys/rnd.h>
     76 
     77 #if defined(COMPAT_50)
     78 
     79 /*
     80  * Common routine to set access and modification times given a vnode.
     81  */
     82 static int
     83 get_utimes32(const netbsd32_timeval50p_t *tptr, struct timeval *tv,
     84     struct timeval **tvp)
     85 {
     86 	int error;
     87 	struct netbsd32_timeval50 tv32[2];
     88 
     89 	if (tptr == NULL) {
     90 		*tvp = NULL;
     91 		return 0;
     92 	}
     93 
     94 	error = copyin(tptr, tv32, sizeof(tv32));
     95 	if (error)
     96 		return error;
     97 	netbsd32_to_timeval50(&tv32[0], &tv[0]);
     98 	netbsd32_to_timeval50(&tv32[1], &tv[1]);
     99 
    100 	*tvp = tv;
    101 	return 0;
    102 }
    103 
    104 int
    105 compat_50_netbsd32_mknod(struct lwp *l,
    106     const struct compat_50_netbsd32_mknod_args *uap, register_t *retval)
    107 {
    108 	/* {
    109 		syscallarg(netbsd32_charp) path;
    110 		syscallarg(mode_t) mode;
    111 		syscallarg(uint32_t) dev;
    112 	} */
    113 	return do_sys_mknod(l, SCARG_P32(uap, path), SCARG(uap, mode),
    114 	    SCARG(uap, dev), UIO_USERSPACE);
    115 }
    116 
    117 int
    118 compat_50_netbsd32_select(struct lwp *l,
    119     const struct compat_50_netbsd32_select_args *uap, register_t *retval)
    120 {
    121 	/* {
    122 		syscallarg(int) nd;
    123 		syscallarg(netbsd32_fd_setp_t) in;
    124 		syscallarg(netbsd32_fd_setp_t) ou;
    125 		syscallarg(netbsd32_fd_setp_t) ex;
    126 		syscallarg(netbsd32_timeval50p_t) tv;
    127 	} */
    128 	int error;
    129 	struct netbsd32_timeval50 tv32;
    130 	struct timespec ats, *ts = NULL;
    131 
    132 	if (SCARG_P32(uap, tv)) {
    133 		error = copyin(SCARG_P32(uap, tv), &tv32, sizeof(tv32));
    134 		if (error != 0)
    135 			return error;
    136 		ats.tv_sec = tv32.tv_sec;
    137 		ats.tv_nsec = tv32.tv_usec * 1000;
    138 		ts = &ats;
    139 	}
    140 
    141 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
    142 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, NULL);
    143 }
    144 
    145 int
    146 compat_50_netbsd32_gettimeofday(struct lwp *l,
    147     const struct compat_50_netbsd32_gettimeofday_args *uap, register_t *retval)
    148 {
    149 	/* {
    150 		syscallarg(netbsd32_timeval50p_t) tp;
    151 		syscallarg(netbsd32_timezonep_t) tzp;
    152 	} */
    153 	struct timeval atv;
    154 	struct netbsd32_timeval50 tv32;
    155 	int error = 0;
    156 	struct netbsd32_timezone tzfake;
    157 
    158 	if (SCARG_P32(uap, tp)) {
    159 		microtime(&atv);
    160 		netbsd32_from_timeval50(&atv, &tv32);
    161 		error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
    162 		if (error)
    163 			return error;
    164 	}
    165 	if (SCARG_P32(uap, tzp)) {
    166 		/*
    167 		 * NetBSD has no kernel notion of time zone, so we just
    168 		 * fake up a timezone struct and return it if demanded.
    169 		 */
    170 		memset(&tzfake, 0, sizeof(tzfake));
    171 		tzfake.tz_minuteswest = 0;
    172 		tzfake.tz_dsttime = 0;
    173 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
    174 	}
    175 	return error;
    176 }
    177 
    178 int
    179 compat_50_netbsd32_settimeofday(struct lwp *l,
    180     const struct compat_50_netbsd32_settimeofday_args *uap, register_t *retval)
    181 {
    182 	/* {
    183 		syscallarg(const netbsd32_timeval50p_t) tv;
    184 		syscallarg(const netbsd32_timezonep_t) tzp;
    185 	} */
    186 	struct netbsd32_timeval50 atv32;
    187 	struct timeval atv;
    188 	struct timespec ats;
    189 	int error;
    190 	struct proc *p = l->l_proc;
    191 
    192 	/* Verify all parameters before changing time. */
    193 
    194 	/*
    195 	 * NetBSD has no kernel notion of time zone, and only an
    196 	 * obsolete program would try to set it, so we log a warning.
    197 	 */
    198 	if (SCARG_P32(uap, tzp))
    199 		printf("pid %d attempted to set the "
    200 		    "(obsolete) kernel time zone\n", p->p_pid);
    201 
    202 	if (SCARG_P32(uap, tv) == 0)
    203 		return 0;
    204 
    205 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
    206 		return error;
    207 
    208 	netbsd32_to_timeval50(&atv32, &atv);
    209 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
    210 	return settime(p, &ats);
    211 }
    212 
    213 int
    214 compat_50_netbsd32_utimes(struct lwp *l,
    215     const struct compat_50_netbsd32_utimes_args *uap, register_t *retval)
    216 {
    217 	/* {
    218 		syscallarg(const netbsd32_charp) path;
    219 		syscallarg(const netbsd32_timeval50p_t) tptr;
    220 	} */
    221 	int error;
    222 	struct timeval tv[2], *tvp;
    223 
    224 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    225 	if (error != 0)
    226 		return error;
    227 
    228 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
    229 	    tvp, UIO_SYSSPACE);
    230 }
    231 
    232 int
    233 compat_50_netbsd32_adjtime(struct lwp *l,
    234     const struct compat_50_netbsd32_adjtime_args *uap, register_t *retval)
    235 {
    236 	/* {
    237 		syscallarg(const netbsd32_timeval50p_t) delta;
    238 		syscallarg(netbsd32_timeval50p_t) olddelta;
    239 	} */
    240 	struct netbsd32_timeval50 atv;
    241 	int error;
    242 
    243 	extern int time_adjusted;     /* in kern_ntptime.c */
    244 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
    245 
    246 	if ((error = kauth_authorize_system(l->l_cred,
    247 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
    248 	    NULL)) != 0)
    249 		return (error);
    250 
    251 	if (SCARG_P32(uap, olddelta)) {
    252 		memset(&atv, 0, sizeof(atv));
    253 
    254 		mutex_spin_enter(&timecounter_lock);
    255 		atv.tv_sec = time_adjtime / 1000000;
    256 		atv.tv_usec = time_adjtime % 1000000;
    257 		if (atv.tv_usec < 0) {
    258 			atv.tv_usec += 1000000;
    259 			atv.tv_sec--;
    260 		}
    261 		mutex_spin_exit(&timecounter_lock);
    262 
    263 		error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv));
    264 		if (error)
    265 			return (error);
    266 	}
    267 
    268 	if (SCARG_P32(uap, delta)) {
    269 		error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv));
    270 		if (error)
    271 			return (error);
    272 
    273 		mutex_spin_enter(&timecounter_lock);
    274 		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
    275 		if (time_adjtime)
    276 			/* We need to save the system time during shutdown */
    277 			time_adjusted |= 1;
    278 		mutex_spin_exit(&timecounter_lock);
    279 	}
    280 
    281 	return 0;
    282 }
    283 
    284 int
    285 compat_50_netbsd32_futimes(struct lwp *l,
    286     const struct compat_50_netbsd32_futimes_args *uap, register_t *retval)
    287 {
    288 	/* {
    289 		syscallarg(int) fd;
    290 		syscallarg(const netbsd32_timeval50p_t) tptr;
    291 	} */
    292 	int error;
    293 	file_t *fp;
    294 	struct timeval tv[2], *tvp;
    295 
    296 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    297 	if (error != 0)
    298 		return error;
    299 
    300 	/* fd_getvnode() will use the descriptor for us */
    301 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    302 		return error;
    303 
    304 	error = do_sys_utimes(l, fp->f_vnode, NULL, 0, tvp, UIO_SYSSPACE);
    305 
    306 	fd_putfile(SCARG(uap, fd));
    307 	return error;
    308 }
    309 
    310 int
    311 compat_50_netbsd32_clock_gettime(struct lwp *l,
    312     const struct compat_50_netbsd32_clock_gettime_args *uap, register_t *retval)
    313 {
    314 	/* {
    315 		syscallarg(netbsd32_clockid_t) clock_id;
    316 		syscallarg(netbsd32_timespec50p_t) tp;
    317 	} */
    318 	int error;
    319 	struct timespec ats;
    320 	struct netbsd32_timespec50 ts32;
    321 
    322 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
    323 	if (error != 0)
    324 		return error;
    325 
    326 	netbsd32_from_timespec50(&ats, &ts32);
    327 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    328 }
    329 
    330 int
    331 compat_50_netbsd32_clock_settime(struct lwp *l,
    332     const struct compat_50_netbsd32_clock_settime_args *uap, register_t *retval)
    333 {
    334 	/* {
    335 		syscallarg(netbsd32_clockid_t) clock_id;
    336 		syscallarg(const netbsd32_timespec50p_t) tp;
    337 	} */
    338 	struct netbsd32_timespec50 ts32;
    339 	struct timespec ats;
    340 	int error;
    341 
    342 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
    343 		return (error);
    344 
    345 	netbsd32_to_timespec50(&ts32, &ats);
    346 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
    347 }
    348 
    349 int
    350 compat_50_netbsd32_clock_getres(struct lwp *l,
    351     const struct compat_50_netbsd32_clock_getres_args *uap, register_t *retval)
    352 {
    353 	/* {
    354 		syscallarg(netbsd32_clockid_t) clock_id;
    355 		syscallarg(netbsd32_timespec50p_t) tp;
    356 	} */
    357 	struct netbsd32_timespec50 ts32;
    358 	struct timespec ts;
    359 	int error;
    360 
    361 	error = clock_getres1(SCARG(uap, clock_id), &ts);
    362 	if (error != 0)
    363 		return error;
    364 
    365 	if (SCARG_P32(uap, tp)) {
    366 		netbsd32_from_timespec50(&ts, &ts32);
    367 		error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    368 	}
    369 
    370 	return error;
    371 }
    372 
    373 int
    374 compat_50_netbsd32_timer_settime(struct lwp *l,
    375     const struct compat_50_netbsd32_timer_settime_args *uap, register_t *retval)
    376 {
    377 	/* {
    378 		syscallarg(netbsd32_timer_t) timerid;
    379 		syscallarg(int) flags;
    380 		syscallarg(const netbsd32_itimerspec50p_t) value;
    381 		syscallarg(netbsd32_itimerspec50p_t) ovalue;
    382 	} */
    383 	int error;
    384 	struct itimerspec value, ovalue, *ovp = NULL;
    385 	struct netbsd32_itimerspec50 its32;
    386 
    387 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
    388 		return (error);
    389 	netbsd32_to_timespec50(&its32.it_interval, &value.it_interval);
    390 	netbsd32_to_timespec50(&its32.it_value, &value.it_value);
    391 
    392 	if (SCARG_P32(uap, ovalue))
    393 		ovp = &ovalue;
    394 
    395 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    396 	    SCARG(uap, flags), l->l_proc)) != 0)
    397 		return error;
    398 
    399 	if (ovp) {
    400 		memset(&its32, 0, sizeof(its32));
    401 		netbsd32_from_timespec50(&ovp->it_interval, &its32.it_interval);
    402 		netbsd32_from_timespec50(&ovp->it_value, &its32.it_value);
    403 		return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
    404 	}
    405 	return 0;
    406 }
    407 
    408 int
    409 compat_50_netbsd32_timer_gettime(struct lwp *l, const struct compat_50_netbsd32_timer_gettime_args *uap, register_t *retval)
    410 {
    411 	/* {
    412 		syscallarg(netbsd32_timer_t) timerid;
    413 		syscallarg(netbsd32_itimerspec50p_t) value;
    414 	} */
    415 	int error;
    416 	struct itimerspec its;
    417 	struct netbsd32_itimerspec50 its32;
    418 
    419 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    420 	    &its)) != 0)
    421 		return error;
    422 
    423 	memset(&its32, 0, sizeof(its32));
    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(sb32));
    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