Home | History | Annotate | Line # | Download | only in netbsd32
      1 /*	$NetBSD: netbsd32_compat_50.c,v 1.56 2023/07/29 13:40:51 rin 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.56 2023/07/29 13:40:51 rin 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 	NETBSD32TOP_UAP(set, const sigset_t);
    515 	NETBSD32TOP_UAP(info, siginfo_t);
    516 	NETBSD32TOP_UAP(timeout, struct timespec);
    517 
    518 	res = sigtimedwait1(l, &ua, retval,
    519 	    copyin,
    520 	    compat_50_netbsd32_sigtimedwait_put_info,
    521 	    compat_50_netbsd32_sigtimedwait_fetch_timeout,
    522 	    compat_50_netbsd32_sigtimedwait_put_timeout);
    523 	if (!res)
    524 		*retval = 0; /* NetBSD<=5 was not POSIX compliant */
    525 	return res;
    526 }
    527 
    528 int
    529 compat_50_netbsd32_lutimes(struct lwp *l,
    530     const struct compat_50_netbsd32_lutimes_args *uap, register_t *retval)
    531 {
    532 	/* {
    533 		syscallarg(const netbsd32_charp) path;
    534 		syscallarg(const netbsd32_timeval50p_t) tptr;
    535 	} */
    536 	int error;
    537 	struct timeval tv[2], *tvp;
    538 
    539 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    540 	if (error != 0)
    541 		return error;
    542 
    543 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
    544 	    tvp, UIO_SYSSPACE);
    545 }
    546 
    547 int
    548 compat_50_netbsd32__lwp_park(struct lwp *l,
    549     const struct compat_50_netbsd32__lwp_park_args *uap, register_t *retval)
    550 {
    551 	/* {
    552 		syscallarg(const netbsd32_timespec50p) ts;
    553 		syscallarg(lwpid_t) unpark;
    554 		syscallarg(netbsd32_voidp) hint;
    555 		syscallarg(netbsd32_voidp) unparkhint;
    556 	} */
    557 	struct timespec ts, *tsp;
    558 	struct netbsd32_timespec50 ts32;
    559 	int error;
    560 
    561 	if (SCARG_P32(uap, ts) == NULL)
    562 		tsp = NULL;
    563 	else {
    564 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof ts32);
    565 		if (error != 0)
    566 			return error;
    567 		netbsd32_to_timespec50(&ts32, &ts);
    568 		tsp = &ts;
    569 	}
    570 
    571 	if (SCARG(uap, unpark) != 0) {
    572 		error = lwp_unpark(&SCARG(uap, unpark), 1);
    573 		if (error != 0)
    574 			return error;
    575 	}
    576 
    577 	return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp);
    578 }
    579 
    580 static int
    581 compat_50_netbsd32_kevent_fetch_timeout(const void *src, void *dest,
    582     size_t length)
    583 {
    584 	struct netbsd32_timespec50 ts32;
    585 	int error;
    586 
    587 	KASSERT(length == sizeof(struct timespec50));
    588 
    589 	error = copyin(src, &ts32, sizeof(ts32));
    590 	if (error)
    591 		return error;
    592 	netbsd32_to_timespec50(&ts32, (struct timespec *)dest);
    593 	return 0;
    594 }
    595 
    596 int
    597 compat_50_netbsd32_kevent(struct lwp *l,
    598     const struct compat_50_netbsd32_kevent_args *uap, register_t *retval)
    599 {
    600 	/* {
    601 		syscallarg(int) fd;
    602 		syscallarg(netbsd32_kevent100p_t) changelist;
    603 		syscallarg(netbsd32_size_t) nchanges;
    604 		syscallarg(netbsd32_kevent100p_t) eventlist;
    605 		syscallarg(netbsd32_size_t) nevents;
    606 		syscallarg(netbsd32_timespec50p_t) timeout;
    607 	} */
    608 	struct kevent_ops kops = {
    609 		.keo_fetch_timeout = compat_50_netbsd32_kevent_fetch_timeout,
    610 		.keo_fetch_changes = compat_100_netbsd32_kevent_fetch_changes,
    611 		.keo_put_events = compat_100_netbsd32_kevent_put_events,
    612 	};
    613 
    614 	return netbsd32_kevent1(retval, SCARG(uap, fd),
    615 	    (netbsd32_keventp_t)SCARG(uap, changelist), SCARG(uap, nchanges),
    616 	    (netbsd32_keventp_t)SCARG(uap, eventlist), SCARG(uap, nevents),
    617 	    (netbsd32_timespecp_t)SCARG(uap, timeout), &kops);
    618 }
    619 
    620 int
    621 compat_50_netbsd32_pselect(struct lwp *l,
    622     const struct compat_50_netbsd32_pselect_args *uap, register_t *retval)
    623 {
    624 	/* {
    625 		syscallarg(int) nd;
    626 		syscallarg(netbsd32_fd_setp_t) in;
    627 		syscallarg(netbsd32_fd_setp_t) ou;
    628 		syscallarg(netbsd32_fd_setp_t) ex;
    629 		syscallarg(const netbsd32_timespec50p_t) ts;
    630 		syscallarg(const netbsd32_sigsetp_t) mask;
    631 	} */
    632 	int error;
    633 	struct netbsd32_timespec50 ts32;
    634 	struct timespec ats, *ts = NULL;
    635 	sigset_t amask, *mask = NULL;
    636 
    637 	if (SCARG_P32(uap, ts)) {
    638 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    639 		if (error != 0)
    640 			return error;
    641 		netbsd32_to_timespec50(&ts32, &ats);
    642 		ts = &ats;
    643 	}
    644 	if (SCARG_P32(uap, mask)) {
    645 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    646 		if (error != 0)
    647 			return error;
    648 		mask = &amask;
    649 	}
    650 
    651 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
    652 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, mask);
    653 }
    654 
    655 int
    656 compat_50_netbsd32_pollts(struct lwp *l,
    657     const struct compat_50_netbsd32_pollts_args *uap, register_t *retval)
    658 {
    659 	/* {
    660 		syscallarg(struct netbsd32_pollfdp_t) fds;
    661 		syscallarg(u_int) nfds;
    662 		syscallarg(const netbsd32_timespec50p_t) ts;
    663 		syscallarg(const netbsd32_sigsetp_t) mask;
    664 	} */
    665 	int error;
    666 	struct netbsd32_timespec50 ts32;
    667 	struct timespec ats, *ts = NULL;
    668 	sigset_t amask, *mask = NULL;
    669 
    670 	if (SCARG_P32(uap, ts)) {
    671 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    672 		if (error != 0)
    673 			return error;
    674 		netbsd32_to_timespec50(&ts32, &ats);
    675 		ts = &ats;
    676 	}
    677 	if (NETBSD32PTR64( SCARG(uap, mask))) {
    678 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    679 		if (error != 0)
    680 			return error;
    681 		mask = &amask;
    682 	}
    683 
    684 	return pollcommon(retval, SCARG_P32(uap, fds),
    685 	    SCARG(uap, nfds), ts, mask);
    686 }
    687 
    688 int
    689 compat_50_netbsd32___stat30(struct lwp *l,
    690     const struct compat_50_netbsd32___stat30_args *uap, register_t *retval)
    691 {
    692 	/* {
    693 		syscallarg(const netbsd32_charp) path;
    694 		syscallarg(netbsd32_stat50p_t) ub;
    695 	} */
    696 	struct netbsd32_stat50 sb32;
    697 	struct stat sb;
    698 	int error;
    699 	const char *path;
    700 
    701 	path = SCARG_P32(uap, path);
    702 
    703 	error = do_sys_stat(path, FOLLOW, &sb);
    704 	if (error)
    705 		return error;
    706 	netbsd32_from___stat50(&sb, &sb32);
    707 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    708 	return error;
    709 }
    710 
    711 int
    712 compat_50_netbsd32___fstat30(struct lwp *l,
    713     const struct compat_50_netbsd32___fstat30_args *uap, register_t *retval)
    714 {
    715 	/* {
    716 		syscallarg(int) fd;
    717 		syscallarg(netbsd32_stat50p_t) sb;
    718 	} */
    719 	struct netbsd32_stat50 sb32;
    720 	struct stat ub;
    721 	int error;
    722 
    723 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    724 	if (error == 0) {
    725 		netbsd32_from___stat50(&ub, &sb32);
    726 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    727 	}
    728 	return error;
    729 }
    730 
    731 int
    732 compat_50_netbsd32___lstat30(struct lwp *l,
    733     const struct compat_50_netbsd32___lstat30_args *uap, register_t *retval)
    734 {
    735 	/* {
    736 		syscallarg(const netbsd32_charp) path;
    737 		syscallarg(netbsd32_stat50p_t) ub;
    738 	} */
    739 	struct netbsd32_stat50 sb32;
    740 	struct stat sb;
    741 	int error;
    742 	const char *path;
    743 
    744 	path = SCARG_P32(uap, path);
    745 
    746 	error = do_sys_stat(path, NOFOLLOW, &sb);
    747 	if (error)
    748 		return error;
    749 	netbsd32_from___stat50(&sb, &sb32);
    750 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    751 	return error;
    752 }
    753 
    754 int
    755 compat_50_netbsd32___fhstat40(struct lwp *l, const struct compat_50_netbsd32___fhstat40_args *uap, register_t *retval)
    756 {
    757 	/* {
    758 		syscallarg(const netbsd32_pointer_t) fhp;
    759 		syscallarg(netbsd32_size_t) fh_size;
    760 		syscallarg(netbsd32_stat50p_t) sb;
    761 	} */
    762 	struct stat sb;
    763 	struct netbsd32_stat50 sb32;
    764 	int error;
    765 
    766 	error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
    767 	if (error == 0) {
    768 		netbsd32_from___stat50(&sb, &sb32);
    769 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    770 	}
    771 	return error;
    772 }
    773 
    774 int
    775 compat_50_netbsd32_wait4(struct lwp *l, const struct compat_50_netbsd32_wait4_args *uap, register_t *retval)
    776 {
    777 	/* {
    778 		syscallarg(int) pid;
    779 		syscallarg(netbsd32_intp) status;
    780 		syscallarg(int) options;
    781 		syscallarg(netbsd32_rusage50p_t) rusage;
    782 	} */
    783 	int error, status, pid = SCARG(uap, pid);
    784 	struct netbsd32_rusage50 ru32;
    785 	struct rusage ru;
    786 
    787 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
    788 	    SCARG_P32(uap, rusage) != NULL ? &ru : NULL);
    789 
    790 	retval[0] = pid;
    791 	if (pid == 0)
    792 		return error;
    793 
    794 	if (SCARG_P32(uap, rusage)) {
    795 		netbsd32_from_rusage50(&ru, &ru32);
    796 		error = copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    797 	}
    798 
    799 	if (error == 0 && SCARG_P32(uap, status))
    800 		error = copyout(&status, SCARG_P32(uap, status), sizeof(status));
    801 
    802 	return error;
    803 }
    804 
    805 
    806 int
    807 compat_50_netbsd32_getrusage(struct lwp *l, const struct compat_50_netbsd32_getrusage_args *uap, register_t *retval)
    808 {
    809 	/* {
    810 		syscallarg(int) who;
    811 		syscallarg(netbsd32_rusage50p_t) rusage;
    812 	} */
    813 	int error;
    814 	struct proc *p = l->l_proc;
    815 	struct rusage ru;
    816 	struct netbsd32_rusage50 ru32;
    817 
    818 	error = getrusage1(p, SCARG(uap, who), &ru);
    819 	if (error != 0)
    820 		return error;
    821 
    822 	netbsd32_from_rusage50(&ru, &ru32);
    823 	return copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    824 }
    825 
    826 int
    827 compat_50_netbsd32_setitimer(struct lwp *l,
    828     const struct compat_50_netbsd32_setitimer_args *uap, register_t *retval)
    829 {
    830 	/* {
    831 		syscallarg(int) which;
    832 		syscallarg(const netbsd32_itimerval50p_t) itv;
    833 		syscallarg(netbsd32_itimerval50p_t) oitv;
    834 	} */
    835 	struct proc *p = l->l_proc;
    836 	struct netbsd32_itimerval50 s32it, *itv32;
    837 	int which = SCARG(uap, which);
    838 	struct compat_50_netbsd32_getitimer_args getargs;
    839 	struct itimerval aitv;
    840 	int error;
    841 
    842 	itv32 = SCARG_P32(uap, itv);
    843 	if (itv32) {
    844 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
    845 			return error;
    846 		netbsd32_to_itimerval50(&s32it, &aitv);
    847 	}
    848 	if (SCARG_P32(uap, oitv) != 0) {
    849 		SCARG(&getargs, which) = which;
    850 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    851 		if ((error = compat_50_netbsd32_getitimer(l, &getargs, retval)) != 0)
    852 			return error;
    853 	}
    854 	if (itv32 == 0)
    855 		return 0;
    856 
    857 	return dosetitimer(p, which, &aitv);
    858 }
    859 
    860 int
    861 compat_50_netbsd32_getitimer(struct lwp *l, const struct compat_50_netbsd32_getitimer_args *uap, register_t *retval)
    862 {
    863 	/* {
    864 		syscallarg(int) which;
    865 		syscallarg(netbsd32_itimerval50p_t) itv;
    866 	} */
    867 	struct proc *p = l->l_proc;
    868 	struct netbsd32_itimerval50 s32it;
    869 	struct itimerval aitv;
    870 	int error;
    871 
    872 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    873 	if (error)
    874 		return error;
    875 
    876 	netbsd32_from_itimerval50(&aitv, &s32it);
    877 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
    878 }
    879 
    880 #ifdef NTP
    881 int
    882 compat_50_netbsd32_ntp_gettime(struct lwp *l,
    883     const struct compat_50_netbsd32_ntp_gettime_args *uap, register_t *retval)
    884 {
    885 	/* {
    886 		syscallarg(netbsd32_ntptimeval50p_t) ntvp;
    887 	} */
    888 	struct netbsd32_ntptimeval50 ntv32;
    889 	struct ntptimeval ntv;
    890 	int error = 0;
    891 
    892 	if (vec_ntp_gettime == NULL)
    893 		return EINVAL;
    894 
    895 	if (SCARG_P32(uap, ntvp)) {
    896 		(*vec_ntp_gettime)(&ntv);
    897 
    898 		memset(&ntv32, 0, sizeof(ntv32));
    899 		ntv32.time.tv_sec = (int32_t)ntv.time.tv_sec;
    900 		ntv32.time.tv_nsec = ntv.time.tv_nsec;
    901 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
    902 		ntv32.esterror = (netbsd32_long)ntv.esterror;
    903 		ntv32.tai = (netbsd32_long)ntv.tai;
    904 		ntv32.time_state = ntv.time_state;
    905 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
    906 	}
    907 	if (!error) {
    908 		*retval = (*vec_ntp_timestatus)();
    909 	}
    910 
    911 	return error;
    912 }
    913 #endif
    914 
    915 static struct syscall_package compat_netbsd32_50_syscalls[] = {
    916 	{ NETBSD32_SYS_compat_50_netbsd32_mknod, 0,
    917 	    (sy_call_t *)compat_50_netbsd32_mknod },
    918 	{ NETBSD32_SYS_compat_50_netbsd32_select, 0,
    919 	    (sy_call_t *)compat_50_netbsd32_select },
    920 	{ NETBSD32_SYS_compat_50_netbsd32_gettimeofday, 0,
    921 	    (sy_call_t *)compat_50_netbsd32_gettimeofday },
    922 	{ NETBSD32_SYS_compat_50_netbsd32_settimeofday, 0,
    923 	    (sy_call_t *)compat_50_netbsd32_settimeofday },
    924 	{ NETBSD32_SYS_compat_50_netbsd32_utimes, 0,
    925 	    (sy_call_t *)compat_50_netbsd32_utimes },
    926 	{ NETBSD32_SYS_compat_50_netbsd32_futimes, 0,
    927 	    (sy_call_t *)compat_50_netbsd32_futimes },
    928 	{ NETBSD32_SYS_compat_50_netbsd32_adjtime, 0,
    929 	    (sy_call_t *)compat_50_netbsd32_adjtime },
    930 	{ NETBSD32_SYS_compat_50_netbsd32_clock_gettime, 0,
    931 	    (sy_call_t *)compat_50_netbsd32_clock_gettime },
    932 	{ NETBSD32_SYS_compat_50_netbsd32_clock_settime, 0,
    933 	    (sy_call_t *)compat_50_netbsd32_clock_settime },
    934 	{ NETBSD32_SYS_compat_50_netbsd32_clock_getres, 0,
    935 	    (sy_call_t *)compat_50_netbsd32_clock_getres },
    936 	{ NETBSD32_SYS_compat_50_netbsd32_timer_settime, 0,
    937 	    (sy_call_t *)compat_50_netbsd32_timer_settime },
    938 	{ NETBSD32_SYS_compat_50_netbsd32_timer_gettime, 0,
    939 	    (sy_call_t *)compat_50_netbsd32_timer_gettime },
    940 	{ NETBSD32_SYS_compat_50_netbsd32_nanosleep, 0,
    941 	    (sy_call_t *)compat_50_netbsd32_nanosleep },
    942 	{ NETBSD32_SYS_compat_50_netbsd32___sigtimedwait, 0,
    943 	    (sy_call_t *)compat_50_netbsd32___sigtimedwait },
    944 	{ NETBSD32_SYS_compat_50_netbsd32_lutimes, 0,
    945 	    (sy_call_t *)compat_50_netbsd32_lutimes },
    946 	{ NETBSD32_SYS_compat_50_netbsd32__lwp_park, 0,
    947 	    (sy_call_t *)compat_50_netbsd32__lwp_park },
    948 	{ NETBSD32_SYS_compat_50_netbsd32_kevent, 0,
    949 	    (sy_call_t *)compat_50_netbsd32_kevent },
    950 	{ NETBSD32_SYS_compat_50_netbsd32_pselect, 0,
    951 	    (sy_call_t *)compat_50_netbsd32_pselect },
    952 	{ NETBSD32_SYS_compat_50_netbsd32_pollts, 0,
    953 	    (sy_call_t *)compat_50_netbsd32_pollts },
    954 	{ NETBSD32_SYS_compat_50_netbsd32___stat30, 0,
    955 	    (sy_call_t *)compat_50_netbsd32___stat30 },
    956 	{ NETBSD32_SYS_compat_50_netbsd32___fstat30, 0,
    957 	    (sy_call_t *)compat_50_netbsd32___fstat30 },
    958 	{ NETBSD32_SYS_compat_50_netbsd32___lstat30, 0,
    959 	    (sy_call_t *)compat_50_netbsd32___lstat30 },
    960 	{ NETBSD32_SYS_compat_50_netbsd32___fhstat40, 0,
    961 	    (sy_call_t *)compat_50_netbsd32___fhstat40 },
    962 	{ NETBSD32_SYS_compat_50_netbsd32_wait4, 0,
    963 	    (sy_call_t *)compat_50_netbsd32_wait4 },
    964 	{ NETBSD32_SYS_compat_50_netbsd32_getrusage, 0,
    965 	    (sy_call_t *)compat_50_netbsd32_getrusage },
    966 	{ NETBSD32_SYS_compat_50_netbsd32_setitimer, 0,
    967 	    (sy_call_t *)compat_50_netbsd32_setitimer },
    968 	{ NETBSD32_SYS_compat_50_netbsd32_getitimer, 0,
    969 	    (sy_call_t *)compat_50_netbsd32_getitimer },
    970 #ifdef NTP
    971 	{ NETBSD32_SYS_compat_50_netbsd32_ntp_gettime, 0,
    972 	    (sy_call_t *)compat_50_netbsd32_ntp_gettime },
    973 #endif
    974 	{ 0, 0, NULL }
    975 };
    976 
    977 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_50, "compat_netbsd32_60,compat_50");
    978 
    979 static int
    980 compat_netbsd32_50_modcmd(modcmd_t cmd, void *arg)
    981 {
    982 	int ret;
    983 
    984 	switch (cmd) {
    985 	case MODULE_CMD_INIT:
    986                 ret = syscall_establish(&emul_netbsd32,
    987 		    compat_netbsd32_50_syscalls);
    988 		if (ret == 0)
    989 			MODULE_HOOK_SET(rnd_ioctl32_50_hook,
    990 			    compat32_50_rnd_ioctl);
    991 		return ret;
    992 
    993 	case MODULE_CMD_FINI:
    994                 ret = syscall_disestablish(&emul_netbsd32,
    995 		    compat_netbsd32_50_syscalls);
    996 		if (ret == 0)
    997 			MODULE_HOOK_UNSET(rnd_ioctl32_50_hook);
    998 		return ret;
    999 
   1000 	default:
   1001 		return ENOTTY;
   1002 	}
   1003 }
   1004 #endif /* COMPAT_50 */
   1005