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