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