Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_50.c revision 1.40
      1 /*	$NetBSD: netbsd32_compat_50.c,v 1.40 2019/09/20 15:09:07 kamil 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.40 2019/09/20 15:09:07 kamil 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 
    144 		if (tv32.tv_usec < 0 || tv32.tv_usec >= 1000000)
    145 			return EINVAL;
    146 
    147 		ats.tv_sec = tv32.tv_sec;
    148 		ats.tv_nsec = tv32.tv_usec * 1000;
    149 		ts = &ats;
    150 	}
    151 
    152 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
    153 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, NULL);
    154 }
    155 
    156 int
    157 compat_50_netbsd32_gettimeofday(struct lwp *l,
    158     const struct compat_50_netbsd32_gettimeofday_args *uap, register_t *retval)
    159 {
    160 	/* {
    161 		syscallarg(netbsd32_timeval50p_t) tp;
    162 		syscallarg(netbsd32_timezonep_t) tzp;
    163 	} */
    164 	struct timeval atv;
    165 	struct netbsd32_timeval50 tv32;
    166 	int error = 0;
    167 	struct netbsd32_timezone tzfake;
    168 
    169 	if (SCARG_P32(uap, tp)) {
    170 		microtime(&atv);
    171 		netbsd32_from_timeval50(&atv, &tv32);
    172 		error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
    173 		if (error)
    174 			return error;
    175 	}
    176 	if (SCARG_P32(uap, tzp)) {
    177 		/*
    178 		 * NetBSD has no kernel notion of time zone, so we just
    179 		 * fake up a timezone struct and return it if demanded.
    180 		 */
    181 		tzfake.tz_minuteswest = 0;
    182 		tzfake.tz_dsttime = 0;
    183 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
    184 	}
    185 	return error;
    186 }
    187 
    188 int
    189 compat_50_netbsd32_settimeofday(struct lwp *l,
    190     const struct compat_50_netbsd32_settimeofday_args *uap, register_t *retval)
    191 {
    192 	/* {
    193 		syscallarg(const netbsd32_timeval50p_t) tv;
    194 		syscallarg(const netbsd32_timezonep_t) tzp;
    195 	} */
    196 	struct netbsd32_timeval50 atv32;
    197 	struct timeval atv;
    198 	struct timespec ats;
    199 	int error;
    200 	struct proc *p = l->l_proc;
    201 
    202 	/* Verify all parameters before changing time. */
    203 
    204 	/*
    205 	 * NetBSD has no kernel notion of time zone, and only an
    206 	 * obsolete program would try to set it, so we log a warning.
    207 	 */
    208 	if (SCARG_P32(uap, tzp))
    209 		printf("pid %d attempted to set the "
    210 		    "(obsolete) kernel time zone\n", p->p_pid);
    211 
    212 	if (SCARG_P32(uap, tv) == 0)
    213 		return 0;
    214 
    215 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
    216 		return error;
    217 
    218 	netbsd32_to_timeval50(&atv32, &atv);
    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 	extern int time_adjusted;     /* in kern_ntptime.c */
    254 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
    255 
    256 	if ((error = kauth_authorize_system(l->l_cred,
    257 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
    258 	    NULL)) != 0)
    259 		return (error);
    260 
    261 	if (SCARG_P32(uap, olddelta)) {
    262 		mutex_spin_enter(&timecounter_lock);
    263 		atv.tv_sec = time_adjtime / 1000000;
    264 		atv.tv_usec = time_adjtime % 1000000;
    265 		if (atv.tv_usec < 0) {
    266 			atv.tv_usec += 1000000;
    267 			atv.tv_sec--;
    268 		}
    269 		mutex_spin_exit(&timecounter_lock);
    270 
    271 		error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv));
    272 		if (error)
    273 			return (error);
    274 	}
    275 
    276 	if (SCARG_P32(uap, delta)) {
    277 		error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv));
    278 		if (error)
    279 			return (error);
    280 
    281 		mutex_spin_enter(&timecounter_lock);
    282 		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
    283 		if (time_adjtime)
    284 			/* We need to save the system time during shutdown */
    285 			time_adjusted |= 1;
    286 		mutex_spin_exit(&timecounter_lock);
    287 	}
    288 
    289 	return 0;
    290 }
    291 
    292 int
    293 compat_50_netbsd32_futimes(struct lwp *l,
    294     const struct compat_50_netbsd32_futimes_args *uap, register_t *retval)
    295 {
    296 	/* {
    297 		syscallarg(int) fd;
    298 		syscallarg(const netbsd32_timeval50p_t) tptr;
    299 	} */
    300 	int error;
    301 	file_t *fp;
    302 	struct timeval tv[2], *tvp;
    303 
    304 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    305 	if (error != 0)
    306 		return error;
    307 
    308 	/* fd_getvnode() will use the descriptor for us */
    309 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    310 		return error;
    311 
    312 	error = do_sys_utimes(l, fp->f_vnode, NULL, 0, tvp, UIO_SYSSPACE);
    313 
    314 	fd_putfile(SCARG(uap, fd));
    315 	return error;
    316 }
    317 
    318 int
    319 compat_50_netbsd32_clock_gettime(struct lwp *l,
    320     const struct compat_50_netbsd32_clock_gettime_args *uap, register_t *retval)
    321 {
    322 	/* {
    323 		syscallarg(netbsd32_clockid_t) clock_id;
    324 		syscallarg(netbsd32_timespec50p_t) tp;
    325 	} */
    326 	int error;
    327 	struct timespec ats;
    328 	struct netbsd32_timespec50 ts32;
    329 
    330 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
    331 	if (error != 0)
    332 		return error;
    333 
    334 	netbsd32_from_timespec50(&ats, &ts32);
    335 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    336 }
    337 
    338 int
    339 compat_50_netbsd32_clock_settime(struct lwp *l,
    340     const struct compat_50_netbsd32_clock_settime_args *uap, register_t *retval)
    341 {
    342 	/* {
    343 		syscallarg(netbsd32_clockid_t) clock_id;
    344 		syscallarg(const netbsd32_timespec50p_t) tp;
    345 	} */
    346 	struct netbsd32_timespec50 ts32;
    347 	struct timespec ats;
    348 	int error;
    349 
    350 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
    351 		return (error);
    352 
    353 	netbsd32_to_timespec50(&ts32, &ats);
    354 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
    355 }
    356 
    357 int
    358 compat_50_netbsd32_clock_getres(struct lwp *l,
    359     const struct compat_50_netbsd32_clock_getres_args *uap, register_t *retval)
    360 {
    361 	/* {
    362 		syscallarg(netbsd32_clockid_t) clock_id;
    363 		syscallarg(netbsd32_timespec50p_t) tp;
    364 	} */
    365 	struct netbsd32_timespec50 ts32;
    366 	struct timespec ts;
    367 	int error;
    368 
    369 	error = clock_getres1(SCARG(uap, clock_id), &ts);
    370 	if (error != 0)
    371 		return error;
    372 
    373 	if (SCARG_P32(uap, tp)) {
    374 		netbsd32_from_timespec50(&ts, &ts32);
    375 		error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    376 	}
    377 
    378 	return error;
    379 }
    380 
    381 int
    382 compat_50_netbsd32_timer_settime(struct lwp *l,
    383     const struct compat_50_netbsd32_timer_settime_args *uap, register_t *retval)
    384 {
    385 	/* {
    386 		syscallarg(netbsd32_timer_t) timerid;
    387 		syscallarg(int) flags;
    388 		syscallarg(const netbsd32_itimerspec50p_t) value;
    389 		syscallarg(netbsd32_itimerspec50p_t) ovalue;
    390 	} */
    391 	int error;
    392 	struct itimerspec value, ovalue, *ovp = NULL;
    393 	struct netbsd32_itimerspec50 its32;
    394 
    395 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
    396 		return (error);
    397 	netbsd32_to_timespec50(&its32.it_interval, &value.it_interval);
    398 	netbsd32_to_timespec50(&its32.it_value, &value.it_value);
    399 
    400 	if (SCARG_P32(uap, ovalue))
    401 		ovp = &ovalue;
    402 
    403 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    404 	    SCARG(uap, flags), l->l_proc)) != 0)
    405 		return error;
    406 
    407 	if (ovp) {
    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 	netbsd32_from_timespec50(&its.it_interval, &its32.it_interval);
    431 	netbsd32_from_timespec50(&its.it_value, &its32.it_value);
    432 
    433 	return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
    434 }
    435 
    436 int
    437 compat_50_netbsd32_nanosleep(struct lwp *l,
    438     const struct compat_50_netbsd32_nanosleep_args *uap, register_t *retval)
    439 {
    440 	/* {
    441 		syscallarg(const netbsd32_timespec50p_t) rqtp;
    442 		syscallarg(netbsd32_timespecp_t) rmtp;
    443 	} */
    444 	struct netbsd32_timespec50 ts32;
    445 	struct timespec rqt, rmt;
    446 	int error, error1;
    447 
    448 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
    449 	if (error)
    450 		return (error);
    451 	netbsd32_to_timespec50(&ts32, &rqt);
    452 
    453 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
    454 	    SCARG_P32(uap, rmtp) ? &rmt : NULL);
    455 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    456 		return error;
    457 
    458 	netbsd32_from_timespec50(&rmt, &ts32);
    459 	error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
    460 	return error1 ? error1 : error;
    461 }
    462 
    463 static int
    464 compat_50_netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
    465 {
    466 	const siginfo_t *info = src;
    467 	siginfo32_t info32;
    468 
    469 	netbsd32_si_to_si32(&info32, info);
    470 
    471 	return copyout(&info32, dst, sizeof(info32));
    472 }
    473 
    474 static int
    475 compat_50_netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
    476 {
    477 	struct timespec *ts = dst;
    478 	struct netbsd32_timespec50 ts32;
    479 	int error;
    480 
    481 	error = copyin(src, &ts32, sizeof(ts32));
    482 	if (error)
    483 		return error;
    484 
    485 	netbsd32_to_timespec50(&ts32, ts);
    486 	return 0;
    487 }
    488 
    489 static int
    490 compat_50_netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
    491 {
    492 	const struct timespec *ts = src;
    493 	struct netbsd32_timespec50 ts32;
    494 
    495 	netbsd32_from_timespec50(ts, &ts32);
    496 
    497 	return copyout(&ts32, dst, sizeof(ts32));
    498 }
    499 
    500 int
    501 compat_50_netbsd32___sigtimedwait(struct lwp *l,
    502     const struct compat_50_netbsd32___sigtimedwait_args *uap, register_t *retval)
    503 {
    504 	/* {
    505 		syscallarg(netbsd32_sigsetp_t) set;
    506 		syscallarg(netbsd32_siginfop_t) info;
    507 		syscallarg(netbsd32_timespec50p_t) timeout;
    508 	} */
    509 	struct sys_____sigtimedwait50_args ua;
    510 	int res;
    511 
    512 	NETBSD32TOP_UAP(set, const sigset_t);
    513 	NETBSD32TOP_UAP(info, siginfo_t);
    514 	NETBSD32TOP_UAP(timeout, struct timespec);
    515 
    516 	res = sigtimedwait1(l, &ua, retval,
    517 	    copyin,
    518 	    compat_50_netbsd32_sigtimedwait_put_info,
    519 	    compat_50_netbsd32_sigtimedwait_fetch_timeout,
    520 	    compat_50_netbsd32_sigtimedwait_put_timeout);
    521 	if (!res)
    522 		*retval = 0; /* NetBSD<=5 was not POSIX compliant */
    523 	return res;
    524 }
    525 
    526 int
    527 compat_50_netbsd32_lutimes(struct lwp *l,
    528     const struct compat_50_netbsd32_lutimes_args *uap, register_t *retval)
    529 {
    530 	/* {
    531 		syscallarg(const netbsd32_charp) path;
    532 		syscallarg(const netbsd32_timeval50p_t) tptr;
    533 	} */
    534 	int error;
    535 	struct timeval tv[2], *tvp;
    536 
    537 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    538 	if (error != 0)
    539 		return error;
    540 
    541 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
    542 	    tvp, UIO_SYSSPACE);
    543 }
    544 
    545 int
    546 compat_50_netbsd32__lwp_park(struct lwp *l,
    547     const struct compat_50_netbsd32__lwp_park_args *uap, register_t *retval)
    548 {
    549 	/* {
    550 		syscallarg(const netbsd32_timespec50p) ts;
    551 		syscallarg(lwpid_t) unpark;
    552 		syscallarg(netbsd32_voidp) hint;
    553 		syscallarg(netbsd32_voidp) unparkhint;
    554 	} */
    555 	struct timespec ts, *tsp;
    556 	struct netbsd32_timespec50 ts32;
    557 	int error;
    558 
    559 	if (SCARG_P32(uap, ts) == NULL)
    560 		tsp = NULL;
    561 	else {
    562 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof ts32);
    563 		if (error != 0)
    564 			return error;
    565 		netbsd32_to_timespec50(&ts32, &ts);
    566 		tsp = &ts;
    567 	}
    568 
    569 	if (SCARG(uap, unpark) != 0) {
    570 		error = lwp_unpark(SCARG(uap, unpark),
    571 		    SCARG_P32(uap, unparkhint));
    572 		if (error != 0)
    573 			return error;
    574 	}
    575 
    576 	return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp,
    577 	    SCARG_P32(uap, hint));
    578 }
    579 
    580 static int
    581 netbsd32_kevent_fetch_timeout(const void *src, void *dest, size_t length)
    582 {
    583 	struct netbsd32_timespec50 ts32;
    584 	int error;
    585 
    586 	KASSERT(length == sizeof(struct timespec50));
    587 
    588 	error = copyin(src, &ts32, sizeof(ts32));
    589 	if (error)
    590 		return error;
    591 	netbsd32_to_timespec50(&ts32, (struct timespec *)dest);
    592 	return 0;
    593 }
    594 
    595 static int
    596 netbsd32_kevent_fetch_changes(void *ctx, const struct kevent *changelist,
    597     struct kevent *changes, size_t index, int n)
    598 {
    599 	const struct netbsd32_kevent *src =
    600 	    (const struct netbsd32_kevent *)changelist;
    601 	struct netbsd32_kevent *kev32, *changes32 = ctx;
    602 	int error, i;
    603 
    604 	error = copyin(src + index, changes32, n * sizeof(*changes32));
    605 	if (error)
    606 		return error;
    607 	for (i = 0, kev32 = changes32; i < n; i++, kev32++, changes++)
    608 		netbsd32_to_kevent(kev32, changes);
    609 	return 0;
    610 }
    611 
    612 static int
    613 netbsd32_kevent_put_events(void *ctx, struct kevent *events,
    614     struct kevent *eventlist, size_t index, int n)
    615 {
    616 	struct netbsd32_kevent *kev32, *events32 = ctx;
    617 	int i;
    618 
    619 	for (i = 0, kev32 = events32; i < n; i++, kev32++, events++)
    620 		netbsd32_from_kevent(events, kev32);
    621 	kev32 = (struct netbsd32_kevent *)eventlist;
    622 	return  copyout(events32, kev32, n * sizeof(*events32));
    623 }
    624 
    625 int
    626 compat_50_netbsd32_kevent(struct lwp *l,
    627     const struct compat_50_netbsd32_kevent_args *uap, register_t *retval)
    628 {
    629 	/* {
    630 		syscallarg(int) fd;
    631 		syscallarg(netbsd32_keventp_t) changelist;
    632 		syscallarg(netbsd32_size_t) nchanges;
    633 		syscallarg(netbsd32_keventp_t) eventlist;
    634 		syscallarg(netbsd32_size_t) nevents;
    635 		syscallarg(netbsd32_timespec50p_t) timeout;
    636 	} */
    637 	int error;
    638 	size_t maxalloc, nchanges, nevents;
    639 	struct kevent_ops netbsd32_kevent_ops = {
    640 		.keo_fetch_timeout = netbsd32_kevent_fetch_timeout,
    641 		.keo_fetch_changes = netbsd32_kevent_fetch_changes,
    642 		.keo_put_events = netbsd32_kevent_put_events,
    643 	};
    644 
    645 	nchanges = SCARG(uap, nchanges);
    646 	nevents = SCARG(uap, nevents);
    647 	maxalloc = KQ_NEVENTS;
    648 
    649 	netbsd32_kevent_ops.keo_private =
    650 	    kmem_alloc(maxalloc * sizeof(struct netbsd32_kevent), KM_SLEEP);
    651 
    652 	error = kevent1(retval, SCARG(uap, fd),
    653 	    NETBSD32PTR64(SCARG(uap, changelist)), nchanges,
    654 	    NETBSD32PTR64(SCARG(uap, eventlist)), nevents,
    655 	    NETBSD32PTR64(SCARG(uap, timeout)), &netbsd32_kevent_ops);
    656 
    657 	kmem_free(netbsd32_kevent_ops.keo_private,
    658 	    maxalloc * sizeof(struct netbsd32_kevent));
    659 	return error;
    660 }
    661 
    662 int
    663 compat_50_netbsd32_pselect(struct lwp *l,
    664     const struct compat_50_netbsd32_pselect_args *uap, register_t *retval)
    665 {
    666 	/* {
    667 		syscallarg(int) nd;
    668 		syscallarg(netbsd32_fd_setp_t) in;
    669 		syscallarg(netbsd32_fd_setp_t) ou;
    670 		syscallarg(netbsd32_fd_setp_t) ex;
    671 		syscallarg(const netbsd32_timespec50p_t) ts;
    672 		syscallarg(const netbsd32_sigsetp_t) mask;
    673 	} */
    674 	int error;
    675 	struct netbsd32_timespec50 ts32;
    676 	struct timespec ats, *ts = NULL;
    677 	sigset_t amask, *mask = NULL;
    678 
    679 	if (SCARG_P32(uap, ts)) {
    680 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    681 		if (error != 0)
    682 			return error;
    683 		netbsd32_to_timespec50(&ts32, &ats);
    684 		ts = &ats;
    685 	}
    686 	if (SCARG_P32(uap, mask)) {
    687 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    688 		if (error != 0)
    689 			return error;
    690 		mask = &amask;
    691 	}
    692 
    693 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
    694 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, mask);
    695 }
    696 
    697 int
    698 compat_50_netbsd32_pollts(struct lwp *l,
    699     const struct compat_50_netbsd32_pollts_args *uap, register_t *retval)
    700 {
    701 	/* {
    702 		syscallarg(struct netbsd32_pollfdp_t) fds;
    703 		syscallarg(u_int) nfds;
    704 		syscallarg(const netbsd32_timespec50p_t) ts;
    705 		syscallarg(const netbsd32_sigsetp_t) mask;
    706 	} */
    707 	int error;
    708 	struct netbsd32_timespec50 ts32;
    709 	struct timespec ats, *ts = NULL;
    710 	sigset_t amask, *mask = NULL;
    711 
    712 	if (SCARG_P32(uap, ts)) {
    713 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    714 		if (error != 0)
    715 			return error;
    716 		netbsd32_to_timespec50(&ts32, &ats);
    717 		ts = &ats;
    718 	}
    719 	if (NETBSD32PTR64( SCARG(uap, mask))) {
    720 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    721 		if (error != 0)
    722 			return error;
    723 		mask = &amask;
    724 	}
    725 
    726 	return pollcommon(retval, SCARG_P32(uap, fds),
    727 	    SCARG(uap, nfds), ts, mask);
    728 }
    729 
    730 int
    731 compat_50_netbsd32___stat30(struct lwp *l,
    732     const struct compat_50_netbsd32___stat30_args *uap, register_t *retval)
    733 {
    734 	/* {
    735 		syscallarg(const netbsd32_charp) path;
    736 		syscallarg(netbsd32_stat50p_t) ub;
    737 	} */
    738 	struct netbsd32_stat50 sb32;
    739 	struct stat sb;
    740 	int error;
    741 	const char *path;
    742 
    743 	path = SCARG_P32(uap, path);
    744 
    745 	error = do_sys_stat(path, FOLLOW, &sb);
    746 	if (error)
    747 		return error;
    748 	netbsd32_from___stat50(&sb, &sb32);
    749 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    750 	return error;
    751 }
    752 
    753 int
    754 compat_50_netbsd32___fstat30(struct lwp *l,
    755     const struct compat_50_netbsd32___fstat30_args *uap, register_t *retval)
    756 {
    757 	/* {
    758 		syscallarg(int) fd;
    759 		syscallarg(netbsd32_stat50p_t) sb;
    760 	} */
    761 	struct netbsd32_stat50 sb32;
    762 	struct stat ub;
    763 	int error;
    764 
    765 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    766 	if (error == 0) {
    767 		netbsd32_from___stat50(&ub, &sb32);
    768 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    769 	}
    770 	return error;
    771 }
    772 
    773 int
    774 compat_50_netbsd32___lstat30(struct lwp *l,
    775     const struct compat_50_netbsd32___lstat30_args *uap, register_t *retval)
    776 {
    777 	/* {
    778 		syscallarg(const netbsd32_charp) path;
    779 		syscallarg(netbsd32_stat50p_t) ub;
    780 	} */
    781 	struct netbsd32_stat50 sb32;
    782 	struct stat sb;
    783 	int error;
    784 	const char *path;
    785 
    786 	path = SCARG_P32(uap, path);
    787 
    788 	error = do_sys_stat(path, NOFOLLOW, &sb);
    789 	if (error)
    790 		return error;
    791 	netbsd32_from___stat50(&sb, &sb32);
    792 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    793 	return error;
    794 }
    795 
    796 int
    797 compat_50_netbsd32___fhstat40(struct lwp *l, const struct compat_50_netbsd32___fhstat40_args *uap, register_t *retval)
    798 {
    799 	/* {
    800 		syscallarg(const netbsd32_pointer_t) fhp;
    801 		syscallarg(netbsd32_size_t) fh_size;
    802 		syscallarg(netbsd32_stat50p_t) sb;
    803 	} */
    804 	struct stat sb;
    805 	struct netbsd32_stat50 sb32;
    806 	int error;
    807 
    808 	error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
    809 	if (error != 0) {
    810 		netbsd32_from___stat50(&sb, &sb32);
    811 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
    812 	}
    813 	return error;
    814 }
    815 
    816 int
    817 compat_50_netbsd32_wait4(struct lwp *l, const struct compat_50_netbsd32_wait4_args *uap, register_t *retval)
    818 {
    819 	/* {
    820 		syscallarg(int) pid;
    821 		syscallarg(netbsd32_intp) status;
    822 		syscallarg(int) options;
    823 		syscallarg(netbsd32_rusage50p_t) rusage;
    824 	} */
    825 	int error, status, pid = SCARG(uap, pid);
    826 	struct netbsd32_rusage50 ru32;
    827 	struct rusage ru;
    828 
    829 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
    830 	    SCARG_P32(uap, rusage) != NULL ? &ru : NULL);
    831 
    832 	retval[0] = pid;
    833 	if (pid == 0)
    834 		return error;
    835 
    836 	if (SCARG_P32(uap, rusage)) {
    837 		netbsd32_from_rusage50(&ru, &ru32);
    838 		error = copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    839 	}
    840 
    841 	if (error == 0 && SCARG_P32(uap, status))
    842 		error = copyout(&status, SCARG_P32(uap, status), sizeof(status));
    843 
    844 	return error;
    845 }
    846 
    847 
    848 int
    849 compat_50_netbsd32_getrusage(struct lwp *l, const struct compat_50_netbsd32_getrusage_args *uap, register_t *retval)
    850 {
    851 	/* {
    852 		syscallarg(int) who;
    853 		syscallarg(netbsd32_rusage50p_t) rusage;
    854 	} */
    855 	int error;
    856 	struct proc *p = l->l_proc;
    857 	struct rusage ru;
    858 	struct netbsd32_rusage50 ru32;
    859 
    860 	error = getrusage1(p, SCARG(uap, who), &ru);
    861 	if (error != 0)
    862 		return error;
    863 
    864 	netbsd32_from_rusage50(&ru, &ru32);
    865 	return copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    866 }
    867 
    868 int
    869 compat_50_netbsd32_setitimer(struct lwp *l,
    870     const struct compat_50_netbsd32_setitimer_args *uap, register_t *retval)
    871 {
    872 	/* {
    873 		syscallarg(int) which;
    874 		syscallarg(const netbsd32_itimerval50p_t) itv;
    875 		syscallarg(netbsd32_itimerval50p_t) oitv;
    876 	} */
    877 	struct proc *p = l->l_proc;
    878 	struct netbsd32_itimerval50 s32it, *itv32;
    879 	int which = SCARG(uap, which);
    880 	struct compat_50_netbsd32_getitimer_args getargs;
    881 	struct itimerval aitv;
    882 	int error;
    883 
    884 	if ((u_int)which > ITIMER_PROF)
    885 		return (EINVAL);
    886 	itv32 = SCARG_P32(uap, itv);
    887 	if (itv32) {
    888 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
    889 			return (error);
    890 		netbsd32_to_itimerval50(&s32it, &aitv);
    891 	}
    892 	if (SCARG_P32(uap, oitv) != 0) {
    893 		SCARG(&getargs, which) = which;
    894 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    895 		if ((error = compat_50_netbsd32_getitimer(l, &getargs, retval)) != 0)
    896 			return (error);
    897 	}
    898 	if (itv32 == 0)
    899 		return 0;
    900 
    901 	return dosetitimer(p, which, &aitv);
    902 }
    903 
    904 int
    905 compat_50_netbsd32_getitimer(struct lwp *l, const struct compat_50_netbsd32_getitimer_args *uap, register_t *retval)
    906 {
    907 	/* {
    908 		syscallarg(int) which;
    909 		syscallarg(netbsd32_itimerval50p_t) itv;
    910 	} */
    911 	struct proc *p = l->l_proc;
    912 	struct netbsd32_itimerval50 s32it;
    913 	struct itimerval aitv;
    914 	int error;
    915 
    916 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    917 	if (error)
    918 		return error;
    919 
    920 	netbsd32_from_itimerval50(&aitv, &s32it);
    921 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
    922 }
    923 
    924 #ifdef QUOTA
    925 int
    926 compat_50_netbsd32_quotactl(struct lwp *l, const struct compat_50_netbsd32_quotactl_args *uap, register_t *retval)
    927 {
    928 	/* {
    929 		syscallarg(const netbsd32_charp) path;
    930 		syscallarg(int) cmd;
    931 		syscallarg(int) uid;
    932 		syscallarg(netbsd32_voidp) arg;
    933 	} */
    934 	struct compat_50_sys_quotactl_args ua;
    935 
    936 	NETBSD32TOP_UAP(path, const char);
    937 	NETBSD32TO64_UAP(cmd);
    938 	NETBSD32TO64_UAP(uid);
    939 	NETBSD32TOP_UAP(arg, void *);
    940 	return (compat_50_sys_quotactl(l, &ua, retval));
    941 }
    942 #endif
    943 
    944 #ifdef NTP
    945 int
    946 compat_50_netbsd32_ntp_gettime(struct lwp *l,
    947     const struct compat_50_netbsd32_ntp_gettime_args *uap, register_t *retval)
    948 {
    949 	/* {
    950 		syscallarg(netbsd32_ntptimeval50p_t) ntvp;
    951 	} */
    952 	struct netbsd32_ntptimeval50 ntv32;
    953 	struct ntptimeval ntv;
    954 	int error = 0;
    955 
    956 	if (vec_ntp_gettime == NULL)
    957 		return EINVAL;
    958 
    959 	if (SCARG_P32(uap, ntvp)) {
    960 		(*vec_ntp_gettime)(&ntv);
    961 
    962 		memset(&ntv32, 0, sizeof(ntv32));
    963 		ntv32.time.tv_sec = (int32_t)ntv.time.tv_sec;
    964 		ntv32.time.tv_nsec = ntv.time.tv_nsec;
    965 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
    966 		ntv32.esterror = (netbsd32_long)ntv.esterror;
    967 		ntv32.tai = (netbsd32_long)ntv.tai;
    968 		ntv32.time_state = ntv.time_state;
    969 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
    970 	}
    971 	if (!error) {
    972 		*retval = (*vec_ntp_timestatus)();
    973 	}
    974 
    975 	return (error);
    976 }
    977 #endif
    978 
    979 static struct syscall_package compat_netbsd32_50_syscalls[] = {
    980 	{ NETBSD32_SYS_compat_50_netbsd32_mknod, 0,
    981 	    (sy_call_t *)compat_50_netbsd32_mknod },
    982 	{ NETBSD32_SYS_compat_50_netbsd32_select, 0,
    983 	    (sy_call_t *)compat_50_netbsd32_select },
    984 	{ NETBSD32_SYS_compat_50_netbsd32_gettimeofday, 0,
    985 	    (sy_call_t *)compat_50_netbsd32_gettimeofday },
    986 	{ NETBSD32_SYS_compat_50_netbsd32_settimeofday, 0,
    987 	    (sy_call_t *)compat_50_netbsd32_settimeofday },
    988 	{ NETBSD32_SYS_compat_50_netbsd32_utimes, 0,
    989 	    (sy_call_t *)compat_50_netbsd32_utimes },
    990 	{ NETBSD32_SYS_compat_50_netbsd32_futimes, 0,
    991 	    (sy_call_t *)compat_50_netbsd32_futimes },
    992 	{ NETBSD32_SYS_compat_50_netbsd32_adjtime, 0,
    993 	    (sy_call_t *)compat_50_netbsd32_adjtime },
    994 	{ NETBSD32_SYS_compat_50_netbsd32_clock_gettime, 0,
    995 	    (sy_call_t *)compat_50_netbsd32_clock_gettime },
    996 	{ NETBSD32_SYS_compat_50_netbsd32_clock_settime, 0,
    997 	    (sy_call_t *)compat_50_netbsd32_clock_settime },
    998 	{ NETBSD32_SYS_compat_50_netbsd32_clock_getres, 0,
    999 	    (sy_call_t *)compat_50_netbsd32_clock_getres },
   1000 	{ NETBSD32_SYS_compat_50_netbsd32_timer_settime, 0,
   1001 	    (sy_call_t *)compat_50_netbsd32_timer_settime },
   1002 	{ NETBSD32_SYS_compat_50_netbsd32_timer_gettime, 0,
   1003 	    (sy_call_t *)compat_50_netbsd32_timer_gettime },
   1004 	{ NETBSD32_SYS_compat_50_netbsd32_nanosleep, 0,
   1005 	    (sy_call_t *)compat_50_netbsd32_nanosleep },
   1006 	{ NETBSD32_SYS_compat_50_netbsd32___sigtimedwait, 0,
   1007 	    (sy_call_t *)compat_50_netbsd32___sigtimedwait },
   1008 	{ NETBSD32_SYS_compat_50_netbsd32_lutimes, 0,
   1009 	    (sy_call_t *)compat_50_netbsd32_lutimes },
   1010 	{ NETBSD32_SYS_compat_50_netbsd32__lwp_park, 0,
   1011 	    (sy_call_t *)compat_50_netbsd32__lwp_park },
   1012 	{ NETBSD32_SYS_compat_50_netbsd32_kevent, 0,
   1013 	    (sy_call_t *)compat_50_netbsd32_kevent },
   1014 	{ NETBSD32_SYS_compat_50_netbsd32_pselect, 0,
   1015 	    (sy_call_t *)compat_50_netbsd32_pselect },
   1016 	{ NETBSD32_SYS_compat_50_netbsd32_pollts, 0,
   1017 	    (sy_call_t *)compat_50_netbsd32_pollts },
   1018 	{ NETBSD32_SYS_compat_50_netbsd32___stat30, 0,
   1019 	    (sy_call_t *)compat_50_netbsd32___stat30 },
   1020 	{ NETBSD32_SYS_compat_50_netbsd32___fstat30, 0,
   1021 	    (sy_call_t *)compat_50_netbsd32___fstat30 },
   1022 	{ NETBSD32_SYS_compat_50_netbsd32___lstat30, 0,
   1023 	    (sy_call_t *)compat_50_netbsd32___lstat30 },
   1024 	{ NETBSD32_SYS_compat_50_netbsd32___fhstat40, 0,
   1025 	    (sy_call_t *)compat_50_netbsd32___fhstat40 },
   1026 	{ NETBSD32_SYS_compat_50_netbsd32_wait4, 0,
   1027 	    (sy_call_t *)compat_50_netbsd32_wait4 },
   1028 	{ NETBSD32_SYS_compat_50_netbsd32_getrusage, 0,
   1029 	    (sy_call_t *)compat_50_netbsd32_getrusage },
   1030 	{ NETBSD32_SYS_compat_50_netbsd32_setitimer, 0,
   1031 	    (sy_call_t *)compat_50_netbsd32_setitimer },
   1032 	{ NETBSD32_SYS_compat_50_netbsd32_getitimer, 0,
   1033 	    (sy_call_t *)compat_50_netbsd32_getitimer },
   1034 #ifdef QUOTA
   1035 	{ NETBSD32_SYS_compat_50_netbsd32_quotactl, 0,
   1036 	    (sy_call_t *)compat_50_netbsd32_quotactl },
   1037 #endif
   1038 #ifdef NTP
   1039 	{ NETBSD32_SYS_compat_50_netbsd32_ntp_gettime, 0,
   1040 	    (sy_call_t *)compat_50_netbsd32_ntp_gettime },
   1041 #endif
   1042 	{ 0, 0, NULL }
   1043 };
   1044 
   1045 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_50, "compat_netbsd32_60,compat_50");
   1046 
   1047 static int
   1048 compat_netbsd32_50_modcmd(modcmd_t cmd, void *arg)
   1049 {
   1050 	int ret;
   1051 
   1052 	switch (cmd) {
   1053 	case MODULE_CMD_INIT:
   1054                 ret = syscall_establish(&emul_netbsd32,
   1055 		    compat_netbsd32_50_syscalls);
   1056 		if (ret == 0)
   1057 			MODULE_HOOK_SET(rnd_ioctl32_50_hook, "rnd32_50",
   1058 			    compat32_50_rnd_ioctl);
   1059 		return ret;
   1060 
   1061 	case MODULE_CMD_FINI:
   1062                 ret = syscall_disestablish(&emul_netbsd32,
   1063 		    compat_netbsd32_50_syscalls);
   1064 		if (ret == 0)
   1065 			MODULE_HOOK_UNSET(rnd_ioctl32_50_hook);
   1066 		return ret;
   1067 
   1068 	default:
   1069 		return ENOTTY;
   1070 	}
   1071 }
   1072 #endif /* COMPAT_50 */
   1073