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