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