Home | History | Annotate | Line # | Download | only in netbsd32
      1 /*	$NetBSD: netbsd32_compat_50.c,v 1.57 2026/09/20 13:45:00 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2020 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.57 2026/09/20 13:45:00 riastradh 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/timevar.h>
     51 #include <sys/timex.h>
     52 #include <sys/ktrace.h>
     53 #include <sys/eventvar.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/vnode.h>
     56 #include <sys/file.h>
     57 #include <sys/filedesc.h>
     58 #include <sys/poll.h>
     59 #include <sys/namei.h>
     60 #include <sys/statvfs.h>
     61 #include <sys/syscallargs.h>
     62 #include <sys/syscallvar.h>
     63 #include <sys/proc.h>
     64 #include <sys/dirent.h>
     65 #include <sys/kauth.h>
     66 #include <sys/vfs_syscalls.h>
     67 #include <sys/compat_stub.h>
     68 #include <sys/module_hook.h>
     69 
     70 #include <compat/netbsd32/netbsd32.h>
     71 #include <compat/netbsd32/netbsd32_syscall.h>
     72 #include <compat/netbsd32/netbsd32_syscallargs.h>
     73 #include <compat/netbsd32/netbsd32_conv.h>
     74 #include <compat/netbsd32/netbsd32_event.h>
     75 #include <compat/sys/mount.h>
     76 #include <compat/sys/time.h>
     77 #include <compat/sys/rnd.h>
     78 
     79 #if defined(COMPAT_50)
     80 
     81 /*
     82  * Common routine to set access and modification times given a vnode.
     83  */
     84 static int
     85 get_utimes32(const netbsd32_timeval50p_t *tptr, struct timeval *tv,
     86     struct timeval **tvp)
     87 {
     88 	int error;
     89 	struct netbsd32_timeval50 tv32[2];
     90 
     91 	if (tptr == NULL) {
     92 		*tvp = NULL;
     93 		return 0;
     94 	}
     95 
     96 	error = copyin(tptr, tv32, sizeof(tv32));
     97 	if (error)
     98 		return error;
     99 	netbsd32_to_timeval50(&tv32[0], &tv[0]);
    100 	netbsd32_to_timeval50(&tv32[1], &tv[1]);
    101 
    102 	*tvp = tv;
    103 	return 0;
    104 }
    105 
    106 int
    107 compat_50_netbsd32_mknod(struct lwp *l,
    108     const struct compat_50_netbsd32_mknod_args *uap, register_t *retval)
    109 {
    110 	/* {
    111 		syscallarg(netbsd32_charp) path;
    112 		syscallarg(mode_t) mode;
    113 		syscallarg(uint32_t) dev;
    114 	} */
    115 	return do_sys_mknod(l, SCARG_P32(uap, path), SCARG(uap, mode),
    116 	    SCARG(uap, dev), UIO_USERSPACE);
    117 }
    118 
    119 int
    120 compat_50_netbsd32_select(struct lwp *l,
    121     const struct compat_50_netbsd32_select_args *uap, register_t *retval)
    122 {
    123 	/* {
    124 		syscallarg(int) nd;
    125 		syscallarg(netbsd32_fd_setp_t) in;
    126 		syscallarg(netbsd32_fd_setp_t) ou;
    127 		syscallarg(netbsd32_fd_setp_t) ex;
    128 		syscallarg(netbsd32_timeval50p_t) tv;
    129 	} */
    130 	int error;
    131 	struct netbsd32_timeval50 tv32;
    132 	struct timespec ats, *ts = NULL;
    133 
    134 	if (SCARG_P32(uap, tv)) {
    135 		error = copyin(SCARG_P32(uap, tv), &tv32, sizeof(tv32));
    136 		if (error != 0)
    137 			return error;
    138 
    139 		if (tv32.tv_usec < 0 || tv32.tv_usec >= 1000000)
    140 			return EINVAL;
    141 
    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 		memset(&tzfake, 0, sizeof(tzfake));
    177 		tzfake.tz_minuteswest = 0;
    178 		tzfake.tz_dsttime = 0;
    179 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
    180 	}
    181 	return error;
    182 }
    183 
    184 int
    185 compat_50_netbsd32_settimeofday(struct lwp *l,
    186     const struct compat_50_netbsd32_settimeofday_args *uap, register_t *retval)
    187 {
    188 	/* {
    189 		syscallarg(const netbsd32_timeval50p_t) tv;
    190 		syscallarg(const netbsd32_timezonep_t) tzp;
    191 	} */
    192 	struct netbsd32_timeval50 atv32;
    193 	struct timeval atv;
    194 	struct timespec ats;
    195 	int error;
    196 	struct proc *p = l->l_proc;
    197 
    198 	/* Verify all parameters before changing time. */
    199 
    200 	/*
    201 	 * NetBSD has no kernel notion of time zone, and only an
    202 	 * obsolete program would try to set it, so we log a warning.
    203 	 */
    204 	if (SCARG_P32(uap, tzp))
    205 		printf("pid %d attempted to set the "
    206 		    "(obsolete) kernel time zone\n", p->p_pid);
    207 
    208 	if (SCARG_P32(uap, tv) == 0)
    209 		return 0;
    210 
    211 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
    212 		return error;
    213 
    214 	netbsd32_to_timeval50(&atv32, &atv);
    215 
    216 	if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
    217 		return EINVAL;
    218 
    219 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
    220 	return settime(p, &ats);
    221 }
    222 
    223 int
    224 compat_50_netbsd32_utimes(struct lwp *l,
    225     const struct compat_50_netbsd32_utimes_args *uap, register_t *retval)
    226 {
    227 	/* {
    228 		syscallarg(const netbsd32_charp) path;
    229 		syscallarg(const netbsd32_timeval50p_t) tptr;
    230 	} */
    231 	int error;
    232 	struct timeval tv[2], *tvp;
    233 
    234 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    235 	if (error != 0)
    236 		return error;
    237 
    238 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
    239 	    tvp, UIO_SYSSPACE);
    240 }
    241 
    242 int
    243 compat_50_netbsd32_adjtime(struct lwp *l,
    244     const struct compat_50_netbsd32_adjtime_args *uap, register_t *retval)
    245 {
    246 	/* {
    247 		syscallarg(const netbsd32_timeval50p_t) delta;
    248 		syscallarg(netbsd32_timeval50p_t) olddelta;
    249 	} */
    250 	struct netbsd32_timeval50 atv;
    251 	int error;
    252 
    253 	if ((error = kauth_authorize_system(l->l_cred,
    254 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
    255 	    NULL)) != 0)
    256 		return error;
    257 
    258 	if (SCARG_P32(uap, olddelta)) {
    259 		memset(&atv, 0, sizeof(atv));
    260 
    261 		mutex_spin_enter(&timecounter_lock);
    262 		atv.tv_sec = time_adjtime / 1000000;
    263 		atv.tv_usec = time_adjtime % 1000000;
    264 		if (atv.tv_usec < 0) {
    265 			atv.tv_usec += 1000000;
    266 			atv.tv_sec--;
    267 		}
    268 		mutex_spin_exit(&timecounter_lock);
    269 
    270 		error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv));
    271 		if (error)
    272 			return error;
    273 	}
    274 
    275 	if (SCARG_P32(uap, delta)) {
    276 		error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv));
    277 		if (error)
    278 			return error;
    279 
    280 		mutex_spin_enter(&timecounter_lock);
    281 		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
    282 		if (time_adjtime)
    283 			/* We need to save the system time during shutdown */
    284 			time_adjusted |= 1;
    285 		mutex_spin_exit(&timecounter_lock);
    286 	}
    287 
    288 	return 0;
    289 }
    290 
    291 int
    292 compat_50_netbsd32_futimes(struct lwp *l,
    293     const struct compat_50_netbsd32_futimes_args *uap, register_t *retval)
    294 {
    295 	/* {
    296 		syscallarg(int) fd;
    297 		syscallarg(const netbsd32_timeval50p_t) tptr;
    298 	} */
    299 	int error;
    300 	file_t *fp;
    301 	struct timeval tv[2], *tvp;
    302 
    303 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    304 	if (error != 0)
    305 		return error;
    306 
    307 	/* fd_getvnode() will use the descriptor for us */
    308 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    309 		return error;
    310 
    311 	error = do_sys_utimes(l, fp->f_vnode, NULL, 0, tvp, UIO_SYSSPACE);
    312 
    313 	fd_putfile(SCARG(uap, fd));
    314 	return error;
    315 }
    316 
    317 int
    318 compat_50_netbsd32_clock_gettime(struct lwp *l,
    319     const struct compat_50_netbsd32_clock_gettime_args *uap, register_t *retval)
    320 {
    321 	/* {
    322 		syscallarg(netbsd32_clockid_t) clock_id;
    323 		syscallarg(netbsd32_timespec50p_t) tp;
    324 	} */
    325 	int error;
    326 	struct timespec ats;
    327 	struct netbsd32_timespec50 ts32;
    328 
    329 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
    330 	if (error != 0)
    331 		return error;
    332 
    333 	netbsd32_from_timespec50(&ats, &ts32);
    334 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    335 }
    336 
    337 int
    338 compat_50_netbsd32_clock_settime(struct lwp *l,
    339     const struct compat_50_netbsd32_clock_settime_args *uap, register_t *retval)
    340 {
    341 	/* {
    342 		syscallarg(netbsd32_clockid_t) clock_id;
    343 		syscallarg(const netbsd32_timespec50p_t) tp;
    344 	} */
    345 	struct netbsd32_timespec50 ts32;
    346 	struct timespec ats;
    347 	int error;
    348 
    349 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
    350 		return error;
    351 
    352 	netbsd32_to_timespec50(&ts32, &ats);
    353 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
    354 }
    355 
    356 int
    357 compat_50_netbsd32_clock_getres(struct lwp *l,
    358     const struct compat_50_netbsd32_clock_getres_args *uap, register_t *retval)
    359 {
    360 	/* {
    361 		syscallarg(netbsd32_clockid_t) clock_id;
    362 		syscallarg(netbsd32_timespec50p_t) tp;
    363 	} */
    364 	struct netbsd32_timespec50 ts32;
    365 	struct timespec ts;
    366 	int error;
    367 
    368 	error = clock_getres1(SCARG(uap, clock_id), &ts);
    369 	if (error != 0)
    370 		return error;
    371 
    372 	if (SCARG_P32(uap, tp)) {
    373 		netbsd32_from_timespec50(&ts, &ts32);
    374 		error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    375 	}
    376 
    377 	return error;
    378 }
    379 
    380 int
    381 compat_50_netbsd32_timer_settime(struct lwp *l,
    382     const struct compat_50_netbsd32_timer_settime_args *uap, register_t *retval)
    383 {
    384 	/* {
    385 		syscallarg(netbsd32_timer_t) timerid;
    386 		syscallarg(int) flags;
    387 		syscallarg(const netbsd32_itimerspec50p_t) value;
    388 		syscallarg(netbsd32_itimerspec50p_t) ovalue;
    389 	} */
    390 	int error;
    391 	struct itimerspec value, ovalue, *ovp = NULL;
    392 	struct netbsd32_itimerspec50 its32;
    393 
    394 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
    395 		return error;
    396 	netbsd32_to_timespec50(&its32.it_interval, &value.it_interval);
    397 	netbsd32_to_timespec50(&its32.it_value, &value.it_value);
    398 
    399 	if (SCARG_P32(uap, ovalue))
    400 		ovp = &ovalue;
    401 
    402 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    403 	    SCARG(uap, flags), l->l_proc)) != 0)
    404 		return error;
    405 
    406 	if (ovp) {
    407 		memset(&its32, 0, sizeof(its32));
    408 		netbsd32_from_timespec50(&ovp->it_interval, &its32.it_interval);
    409 		netbsd32_from_timespec50(&ovp->it_value, &its32.it_value);
    410 		return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
    411 	}
    412 	return 0;
    413 }
    414 
    415 int
    416 compat_50_netbsd32_timer_gettime(struct lwp *l, const struct compat_50_netbsd32_timer_gettime_args *uap, register_t *retval)
    417 {
    418 	/* {
    419 		syscallarg(netbsd32_timer_t) timerid;
    420 		syscallarg(netbsd32_itimerspec50p_t) value;
    421 	} */
    422 	int error;
    423 	struct itimerspec its;
    424 	struct netbsd32_itimerspec50 its32;
    425 
    426 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    427 	    &its)) != 0)
    428 		return error;
    429 
    430 	memset(&its32, 0, sizeof(its32));
    431 
    432 	netbsd32_from_timespec50(&its.it_interval, &its32.it_interval);
    433 	netbsd32_from_timespec50(&its.it_value, &its32.it_value);
    434 
    435 	return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
    436 }
    437 
    438 int
    439 compat_50_netbsd32_nanosleep(struct lwp *l,
    440     const struct compat_50_netbsd32_nanosleep_args *uap, register_t *retval)
    441 {
    442 	/* {
    443 		syscallarg(const netbsd32_timespec50p_t) rqtp;
    444 		syscallarg(netbsd32_timespecp_t) rmtp;
    445 	} */
    446 	struct netbsd32_timespec50 ts32;
    447 	struct timespec rqt, rmt;
    448 	int error, error1;
    449 
    450 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
    451 	if (error)
    452 		return error;
    453 	netbsd32_to_timespec50(&ts32, &rqt);
    454 
    455 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
    456 	    SCARG_P32(uap, rmtp) ? &rmt : NULL);
    457 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    458 		return error;
    459 
    460 	netbsd32_from_timespec50(&rmt, &ts32);
    461 	error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
    462 	return error1 ? error1 : error;
    463 }
    464 
    465 static int
    466 compat_50_netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
    467 {
    468 	const siginfo_t *info = src;
    469 	siginfo32_t info32;
    470 
    471 	netbsd32_si_to_si32(&info32, info);
    472 
    473 	return copyout(&info32, dst, sizeof(info32));
    474 }
    475 
    476 static int
    477 compat_50_netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
    478 {
    479 	struct timespec *ts = dst;
    480 	struct netbsd32_timespec50 ts32;
    481 	int error;
    482 
    483 	error = copyin(src, &ts32, sizeof(ts32));
    484 	if (error)
    485 		return error;
    486 
    487 	netbsd32_to_timespec50(&ts32, ts);
    488 	return 0;
    489 }
    490 
    491 static int
    492 compat_50_netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
    493 {
    494 	const struct timespec *ts = src;
    495 	struct netbsd32_timespec50 ts32;
    496 
    497 	netbsd32_from_timespec50(ts, &ts32);
    498 
    499 	return copyout(&ts32, dst, sizeof(ts32));
    500 }
    501 
    502 int
    503 compat_50_netbsd32___sigtimedwait(struct lwp *l,
    504     const struct compat_50_netbsd32___sigtimedwait_args *uap, register_t *retval)
    505 {
    506 	/* {
    507 		syscallarg(netbsd32_sigsetp_t) set;
    508 		syscallarg(netbsd32_siginfop_t) info;
    509 		syscallarg(netbsd32_timespec50p_t) timeout;
    510 	} */
    511 	struct sys_____sigtimedwait50_args ua;
    512 	int res;
    513 
    514 	memset(&ua, 0, sizeof(ua));
    515 	NETBSD32TOP_UAP(set, const sigset_t);
    516 	NETBSD32TOP_UAP(info, siginfo_t);
    517 	NETBSD32TOP_UAP(timeout, struct timespec);
    518 
    519 	res = sigtimedwait1(l, &ua, retval,
    520 	    copyin,
    521 	    compat_50_netbsd32_sigtimedwait_put_info,
    522 	    compat_50_netbsd32_sigtimedwait_fetch_timeout,
    523 	    compat_50_netbsd32_sigtimedwait_put_timeout);
    524 	if (!res)
    525 		*retval = 0; /* NetBSD<=5 was not POSIX compliant */
    526 	return res;
    527 }
    528 
    529 int
    530 compat_50_netbsd32_lutimes(struct lwp *l,
    531     const struct compat_50_netbsd32_lutimes_args *uap, register_t *retval)
    532 {
    533 	/* {
    534 		syscallarg(const netbsd32_charp) path;
    535 		syscallarg(const netbsd32_timeval50p_t) tptr;
    536 	} */
    537 	int error;
    538 	struct timeval tv[2], *tvp;
    539 
    540 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    541 	if (error != 0)
    542 		return error;
    543 
    544 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
    545 	    tvp, UIO_SYSSPACE);
    546 }
    547 
    548 int
    549 compat_50_netbsd32__lwp_park(struct lwp *l,
    550     const struct compat_50_netbsd32__lwp_park_args *uap, register_t *retval)
    551 {
    552 	/* {
    553 		syscallarg(const netbsd32_timespec50p) ts;
    554 		syscallarg(lwpid_t) unpark;
    555 		syscallarg(netbsd32_voidp) hint;
    556 		syscallarg(netbsd32_voidp) unparkhint;
    557 	} */
    558 	struct timespec ts, *tsp;
    559 	struct netbsd32_timespec50 ts32;
    560 	int error;
    561 
    562 	if (SCARG_P32(uap, ts) == NULL)
    563 		tsp = NULL;
    564 	else {
    565 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof ts32);
    566 		if (error != 0)
    567 			return error;
    568 		netbsd32_to_timespec50(&ts32, &ts);
    569 		tsp = &ts;
    570 	}
    571 
    572 	if (SCARG(uap, unpark) != 0) {
    573 		error = lwp_unpark(&SCARG(uap, unpark), 1);
    574 		if (error != 0)
    575 			return error;
    576 	}
    577 
    578 	return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp);
    579 }
    580 
    581 static int
    582 compat_50_netbsd32_kevent_fetch_timeout(const void *src, void *dest,
    583     size_t length)
    584 {
    585 	struct netbsd32_timespec50 ts32;
    586 	int error;
    587 
    588 	KASSERT(length == sizeof(struct timespec50));
    589 
    590 	error = copyin(src, &ts32, sizeof(ts32));
    591 	if (error)
    592 		return error;
    593 	netbsd32_to_timespec50(&ts32, (struct timespec *)dest);
    594 	return 0;
    595 }
    596 
    597 int
    598 compat_50_netbsd32_kevent(struct lwp *l,
    599     const struct compat_50_netbsd32_kevent_args *uap, register_t *retval)
    600 {
    601 	/* {
    602 		syscallarg(int) fd;
    603 		syscallarg(netbsd32_kevent100p_t) changelist;
    604 		syscallarg(netbsd32_size_t) nchanges;
    605 		syscallarg(netbsd32_kevent100p_t) eventlist;
    606 		syscallarg(netbsd32_size_t) nevents;
    607 		syscallarg(netbsd32_timespec50p_t) timeout;
    608 	} */
    609 	struct kevent_ops kops = {
    610 		.keo_fetch_timeout = compat_50_netbsd32_kevent_fetch_timeout,
    611 		.keo_fetch_changes = compat_100_netbsd32_kevent_fetch_changes,
    612 		.keo_put_events = compat_100_netbsd32_kevent_put_events,
    613 	};
    614 
    615 	return netbsd32_kevent1(retval, SCARG(uap, fd),
    616 	    (netbsd32_keventp_t)SCARG(uap, changelist), SCARG(uap, nchanges),
    617 	    (netbsd32_keventp_t)SCARG(uap, eventlist), SCARG(uap, nevents),
    618 	    (netbsd32_timespecp_t)SCARG(uap, timeout), &kops);
    619 }
    620 
    621 int
    622 compat_50_netbsd32_pselect(struct lwp *l,
    623     const struct compat_50_netbsd32_pselect_args *uap, register_t *retval)
    624 {
    625 	/* {
    626 		syscallarg(int) nd;
    627 		syscallarg(netbsd32_fd_setp_t) in;
    628 		syscallarg(netbsd32_fd_setp_t) ou;
    629 		syscallarg(netbsd32_fd_setp_t) ex;
    630 		syscallarg(const netbsd32_timespec50p_t) ts;
    631 		syscallarg(const netbsd32_sigsetp_t) mask;
    632 	} */
    633 	int error;
    634 	struct netbsd32_timespec50 ts32;
    635 	struct timespec ats, *ts = NULL;
    636 	sigset_t amask, *mask = NULL;
    637 
    638 	if (SCARG_P32(uap, ts)) {
    639 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    640 		if (error != 0)
    641 			return error;
    642 		netbsd32_to_timespec50(&ts32, &ats);
    643 		ts = &ats;
    644 	}
    645 	if (SCARG_P32(uap, mask)) {
    646 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    647 		if (error != 0)
    648 			return error;
    649 		mask = &amask;
    650 	}
    651 
    652 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
    653 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, mask);
    654 }
    655 
    656 int
    657 compat_50_netbsd32_pollts(struct lwp *l,
    658     const struct compat_50_netbsd32_pollts_args *uap, register_t *retval)
    659 {
    660 	/* {
    661 		syscallarg(struct netbsd32_pollfdp_t) fds;
    662 		syscallarg(u_int) nfds;
    663 		syscallarg(const netbsd32_timespec50p_t) ts;
    664 		syscallarg(const netbsd32_sigsetp_t) mask;
    665 	} */
    666 	int error;
    667 	struct netbsd32_timespec50 ts32;
    668 	struct timespec ats, *ts = NULL;
    669 	sigset_t amask, *mask = NULL;
    670 
    671 	if (SCARG_P32(uap, ts)) {
    672 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    673 		if (error != 0)
    674 			return error;
    675 		netbsd32_to_timespec50(&ts32, &ats);
    676 		ts = &ats;
    677 	}
    678 	if (NETBSD32PTR64( SCARG(uap, mask))) {
    679 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    680 		if (error != 0)
    681 			return error;
    682 		mask = &amask;
    683 	}
    684 
    685 	return pollcommon(retval, SCARG_P32(uap, fds),
    686 	    SCARG(uap, nfds), ts, mask);
    687 }
    688 
    689 int
    690 compat_50_netbsd32___stat30(struct lwp *l,
    691     const struct compat_50_netbsd32___stat30_args *uap, register_t *retval)
    692 {
    693 	/* {
    694 		syscallarg(const netbsd32_charp) path;
    695 		syscallarg(netbsd32_stat50p_t) ub;
    696 	} */
    697 	struct netbsd32_stat50 sb32;
    698 	struct stat sb;
    699 	int error;
    700 	const char *path;
    701 
    702 	path = SCARG_P32(uap, path);
    703 
    704 	error = do_sys_stat(path, FOLLOW, &sb);
    705 	if (error)
    706 		return error;
    707 	netbsd32_from___stat50(&sb, &sb32);
    708 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    709 	return error;
    710 }
    711 
    712 int
    713 compat_50_netbsd32___fstat30(struct lwp *l,
    714     const struct compat_50_netbsd32___fstat30_args *uap, register_t *retval)
    715 {
    716 	/* {
    717 		syscallarg(int) fd;
    718 		syscallarg(netbsd32_stat50p_t) sb;
    719 	} */
    720 	struct netbsd32_stat50 sb32;
    721 	struct stat ub;
    722 	int error;
    723 
    724 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    725 	if (error == 0) {
    726 		netbsd32_from___stat50(&ub, &sb32);
    727 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    728 	}
    729 	return error;
    730 }
    731 
    732 int
    733 compat_50_netbsd32___lstat30(struct lwp *l,
    734     const struct compat_50_netbsd32___lstat30_args *uap, register_t *retval)
    735 {
    736 	/* {
    737 		syscallarg(const netbsd32_charp) path;
    738 		syscallarg(netbsd32_stat50p_t) ub;
    739 	} */
    740 	struct netbsd32_stat50 sb32;
    741 	struct stat sb;
    742 	int error;
    743 	const char *path;
    744 
    745 	path = SCARG_P32(uap, path);
    746 
    747 	error = do_sys_stat(path, NOFOLLOW, &sb);
    748 	if (error)
    749 		return error;
    750 	netbsd32_from___stat50(&sb, &sb32);
    751 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    752 	return error;
    753 }
    754 
    755 int
    756 compat_50_netbsd32___fhstat40(struct lwp *l, const struct compat_50_netbsd32___fhstat40_args *uap, register_t *retval)
    757 {
    758 	/* {
    759 		syscallarg(const netbsd32_pointer_t) fhp;
    760 		syscallarg(netbsd32_size_t) fh_size;
    761 		syscallarg(netbsd32_stat50p_t) sb;
    762 	} */
    763 	struct stat sb;
    764 	struct netbsd32_stat50 sb32;
    765 	int error;
    766 
    767 	error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
    768 	if (error == 0) {
    769 		netbsd32_from___stat50(&sb, &sb32);
    770 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    771 	}
    772 	return error;
    773 }
    774 
    775 int
    776 compat_50_netbsd32_wait4(struct lwp *l, const struct compat_50_netbsd32_wait4_args *uap, register_t *retval)
    777 {
    778 	/* {
    779 		syscallarg(int) pid;
    780 		syscallarg(netbsd32_intp) status;
    781 		syscallarg(int) options;
    782 		syscallarg(netbsd32_rusage50p_t) rusage;
    783 	} */
    784 	int error, status, pid = SCARG(uap, pid);
    785 	struct netbsd32_rusage50 ru32;
    786 	struct rusage ru;
    787 
    788 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
    789 	    SCARG_P32(uap, rusage) != NULL ? &ru : NULL);
    790 
    791 	retval[0] = pid;
    792 	if (pid == 0)
    793 		return error;
    794 
    795 	if (SCARG_P32(uap, rusage)) {
    796 		netbsd32_from_rusage50(&ru, &ru32);
    797 		error = copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    798 	}
    799 
    800 	if (error == 0 && SCARG_P32(uap, status))
    801 		error = copyout(&status, SCARG_P32(uap, status), sizeof(status));
    802 
    803 	return error;
    804 }
    805 
    806 
    807 int
    808 compat_50_netbsd32_getrusage(struct lwp *l, const struct compat_50_netbsd32_getrusage_args *uap, register_t *retval)
    809 {
    810 	/* {
    811 		syscallarg(int) who;
    812 		syscallarg(netbsd32_rusage50p_t) rusage;
    813 	} */
    814 	int error;
    815 	struct proc *p = l->l_proc;
    816 	struct rusage ru;
    817 	struct netbsd32_rusage50 ru32;
    818 
    819 	error = getrusage1(p, SCARG(uap, who), &ru);
    820 	if (error != 0)
    821 		return error;
    822 
    823 	netbsd32_from_rusage50(&ru, &ru32);
    824 	return copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    825 }
    826 
    827 int
    828 compat_50_netbsd32_setitimer(struct lwp *l,
    829     const struct compat_50_netbsd32_setitimer_args *uap, register_t *retval)
    830 {
    831 	/* {
    832 		syscallarg(int) which;
    833 		syscallarg(const netbsd32_itimerval50p_t) itv;
    834 		syscallarg(netbsd32_itimerval50p_t) oitv;
    835 	} */
    836 	struct proc *p = l->l_proc;
    837 	struct netbsd32_itimerval50 s32it, *itv32;
    838 	int which = SCARG(uap, which);
    839 	struct compat_50_netbsd32_getitimer_args getargs;
    840 	struct itimerval aitv;
    841 	int error;
    842 
    843 	itv32 = SCARG_P32(uap, itv);
    844 	if (itv32) {
    845 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
    846 			return error;
    847 		netbsd32_to_itimerval50(&s32it, &aitv);
    848 	}
    849 	if (SCARG_P32(uap, oitv) != 0) {
    850 		memset(&getargs, 0, sizeof(getargs));
    851 		SCARG(&getargs, which) = which;
    852 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    853 		if ((error = compat_50_netbsd32_getitimer(l, &getargs, retval)) != 0)
    854 			return error;
    855 	}
    856 	if (itv32 == 0)
    857 		return 0;
    858 
    859 	return dosetitimer(p, which, &aitv);
    860 }
    861 
    862 int
    863 compat_50_netbsd32_getitimer(struct lwp *l, const struct compat_50_netbsd32_getitimer_args *uap, register_t *retval)
    864 {
    865 	/* {
    866 		syscallarg(int) which;
    867 		syscallarg(netbsd32_itimerval50p_t) itv;
    868 	} */
    869 	struct proc *p = l->l_proc;
    870 	struct netbsd32_itimerval50 s32it;
    871 	struct itimerval aitv;
    872 	int error;
    873 
    874 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    875 	if (error)
    876 		return error;
    877 
    878 	netbsd32_from_itimerval50(&aitv, &s32it);
    879 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
    880 }
    881 
    882 #ifdef NTP
    883 int
    884 compat_50_netbsd32_ntp_gettime(struct lwp *l,
    885     const struct compat_50_netbsd32_ntp_gettime_args *uap, register_t *retval)
    886 {
    887 	/* {
    888 		syscallarg(netbsd32_ntptimeval50p_t) ntvp;
    889 	} */
    890 	struct netbsd32_ntptimeval50 ntv32;
    891 	struct ntptimeval ntv;
    892 	int error = 0;
    893 
    894 	if (vec_ntp_gettime == NULL)
    895 		return EINVAL;
    896 
    897 	if (SCARG_P32(uap, ntvp)) {
    898 		(*vec_ntp_gettime)(&ntv);
    899 
    900 		memset(&ntv32, 0, sizeof(ntv32));
    901 		ntv32.time.tv_sec = (int32_t)ntv.time.tv_sec;
    902 		ntv32.time.tv_nsec = ntv.time.tv_nsec;
    903 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
    904 		ntv32.esterror = (netbsd32_long)ntv.esterror;
    905 		ntv32.tai = (netbsd32_long)ntv.tai;
    906 		ntv32.time_state = ntv.time_state;
    907 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
    908 	}
    909 	if (!error) {
    910 		*retval = (*vec_ntp_timestatus)();
    911 	}
    912 
    913 	return error;
    914 }
    915 #endif
    916 
    917 static struct syscall_package compat_netbsd32_50_syscalls[] = {
    918 	{ NETBSD32_SYS_compat_50_netbsd32_mknod, 0,
    919 	    (sy_call_t *)compat_50_netbsd32_mknod },
    920 	{ NETBSD32_SYS_compat_50_netbsd32_select, 0,
    921 	    (sy_call_t *)compat_50_netbsd32_select },
    922 	{ NETBSD32_SYS_compat_50_netbsd32_gettimeofday, 0,
    923 	    (sy_call_t *)compat_50_netbsd32_gettimeofday },
    924 	{ NETBSD32_SYS_compat_50_netbsd32_settimeofday, 0,
    925 	    (sy_call_t *)compat_50_netbsd32_settimeofday },
    926 	{ NETBSD32_SYS_compat_50_netbsd32_utimes, 0,
    927 	    (sy_call_t *)compat_50_netbsd32_utimes },
    928 	{ NETBSD32_SYS_compat_50_netbsd32_futimes, 0,
    929 	    (sy_call_t *)compat_50_netbsd32_futimes },
    930 	{ NETBSD32_SYS_compat_50_netbsd32_adjtime, 0,
    931 	    (sy_call_t *)compat_50_netbsd32_adjtime },
    932 	{ NETBSD32_SYS_compat_50_netbsd32_clock_gettime, 0,
    933 	    (sy_call_t *)compat_50_netbsd32_clock_gettime },
    934 	{ NETBSD32_SYS_compat_50_netbsd32_clock_settime, 0,
    935 	    (sy_call_t *)compat_50_netbsd32_clock_settime },
    936 	{ NETBSD32_SYS_compat_50_netbsd32_clock_getres, 0,
    937 	    (sy_call_t *)compat_50_netbsd32_clock_getres },
    938 	{ NETBSD32_SYS_compat_50_netbsd32_timer_settime, 0,
    939 	    (sy_call_t *)compat_50_netbsd32_timer_settime },
    940 	{ NETBSD32_SYS_compat_50_netbsd32_timer_gettime, 0,
    941 	    (sy_call_t *)compat_50_netbsd32_timer_gettime },
    942 	{ NETBSD32_SYS_compat_50_netbsd32_nanosleep, 0,
    943 	    (sy_call_t *)compat_50_netbsd32_nanosleep },
    944 	{ NETBSD32_SYS_compat_50_netbsd32___sigtimedwait, 0,
    945 	    (sy_call_t *)compat_50_netbsd32___sigtimedwait },
    946 	{ NETBSD32_SYS_compat_50_netbsd32_lutimes, 0,
    947 	    (sy_call_t *)compat_50_netbsd32_lutimes },
    948 	{ NETBSD32_SYS_compat_50_netbsd32__lwp_park, 0,
    949 	    (sy_call_t *)compat_50_netbsd32__lwp_park },
    950 	{ NETBSD32_SYS_compat_50_netbsd32_kevent, 0,
    951 	    (sy_call_t *)compat_50_netbsd32_kevent },
    952 	{ NETBSD32_SYS_compat_50_netbsd32_pselect, 0,
    953 	    (sy_call_t *)compat_50_netbsd32_pselect },
    954 	{ NETBSD32_SYS_compat_50_netbsd32_pollts, 0,
    955 	    (sy_call_t *)compat_50_netbsd32_pollts },
    956 	{ NETBSD32_SYS_compat_50_netbsd32___stat30, 0,
    957 	    (sy_call_t *)compat_50_netbsd32___stat30 },
    958 	{ NETBSD32_SYS_compat_50_netbsd32___fstat30, 0,
    959 	    (sy_call_t *)compat_50_netbsd32___fstat30 },
    960 	{ NETBSD32_SYS_compat_50_netbsd32___lstat30, 0,
    961 	    (sy_call_t *)compat_50_netbsd32___lstat30 },
    962 	{ NETBSD32_SYS_compat_50_netbsd32___fhstat40, 0,
    963 	    (sy_call_t *)compat_50_netbsd32___fhstat40 },
    964 	{ NETBSD32_SYS_compat_50_netbsd32_wait4, 0,
    965 	    (sy_call_t *)compat_50_netbsd32_wait4 },
    966 	{ NETBSD32_SYS_compat_50_netbsd32_getrusage, 0,
    967 	    (sy_call_t *)compat_50_netbsd32_getrusage },
    968 	{ NETBSD32_SYS_compat_50_netbsd32_setitimer, 0,
    969 	    (sy_call_t *)compat_50_netbsd32_setitimer },
    970 	{ NETBSD32_SYS_compat_50_netbsd32_getitimer, 0,
    971 	    (sy_call_t *)compat_50_netbsd32_getitimer },
    972 #ifdef NTP
    973 	{ NETBSD32_SYS_compat_50_netbsd32_ntp_gettime, 0,
    974 	    (sy_call_t *)compat_50_netbsd32_ntp_gettime },
    975 #endif
    976 	{ 0, 0, NULL }
    977 };
    978 
    979 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_50, "compat_netbsd32_60,compat_50");
    980 
    981 static int
    982 compat_netbsd32_50_modcmd(modcmd_t cmd, void *arg)
    983 {
    984 	int ret;
    985 
    986 	switch (cmd) {
    987 	case MODULE_CMD_INIT:
    988                 ret = syscall_establish(&emul_netbsd32,
    989 		    compat_netbsd32_50_syscalls);
    990 		if (ret == 0)
    991 			MODULE_HOOK_SET(rnd_ioctl32_50_hook,
    992 			    compat32_50_rnd_ioctl);
    993 		return ret;
    994 
    995 	case MODULE_CMD_FINI:
    996                 ret = syscall_disestablish(&emul_netbsd32,
    997 		    compat_netbsd32_50_syscalls);
    998 		if (ret == 0)
    999 			MODULE_HOOK_UNSET(rnd_ioctl32_50_hook);
   1000 		return ret;
   1001 
   1002 	default:
   1003 		return ENOTTY;
   1004 	}
   1005 }
   1006 #endif /* COMPAT_50 */
   1007