Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_50.c revision 1.24.2.1.6.1
      1 /*	$NetBSD: netbsd32_compat_50.c,v 1.24.2.1.6.1 2020/01/21 19:23:37 martin 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.24.2.1.6.1 2020/01/21 19:23:37 martin Exp $");
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_sysv.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/mount.h>
     48 #include <sys/socket.h>
     49 #include <sys/socketvar.h>
     50 #include <sys/stat.h>
     51 #include <sys/time.h>
     52 #include <sys/ktrace.h>
     53 #include <sys/eventvar.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/vnode.h>
     56 #include <sys/file.h>
     57 #include <sys/filedesc.h>
     58 #include <sys/poll.h>
     59 #include <sys/namei.h>
     60 #include <sys/statvfs.h>
     61 #include <sys/syscallargs.h>
     62 #include <sys/proc.h>
     63 #include <sys/dirent.h>
     64 #include <sys/kauth.h>
     65 #include <sys/vfs_syscalls.h>
     66 #include <sys/ipc.h>
     67 #include <sys/msg.h>
     68 #include <sys/sem.h>
     69 #include <sys/shm.h>
     70 
     71 #include <compat/netbsd32/netbsd32.h>
     72 #include <compat/netbsd32/netbsd32_syscallargs.h>
     73 #include <compat/netbsd32/netbsd32_conv.h>
     74 #include <compat/sys/mount.h>
     75 #include <compat/sys/time.h>
     76 
     77 
     78 /*
     79  * Common routine to set access and modification times given a vnode.
     80  */
     81 static int
     82 get_utimes32(const netbsd32_timeval50p_t *tptr, struct timeval *tv,
     83     struct timeval **tvp)
     84 {
     85 	int error;
     86 	struct netbsd32_timeval50 tv32[2];
     87 
     88 	if (tptr == NULL) {
     89 		*tvp = NULL;
     90 		return 0;
     91 	}
     92 
     93 	error = copyin(tptr, tv32, sizeof(tv32));
     94 	if (error)
     95 		return error;
     96 	netbsd32_to_timeval50(&tv32[0], &tv[0]);
     97 	netbsd32_to_timeval50(&tv32[1], &tv[1]);
     98 
     99 	*tvp = tv;
    100 	return 0;
    101 }
    102 
    103 int
    104 compat_50_netbsd32_mknod(struct lwp *l,
    105     const struct compat_50_netbsd32_mknod_args *uap, register_t *retval)
    106 {
    107 	/* {
    108 		syscallarg(netbsd32_charp) path;
    109 		syscallarg(mode_t) mode;
    110 		syscallarg(uint32_t) dev;
    111 	} */
    112 	return do_sys_mknod(l, SCARG_P32(uap, path), SCARG(uap, mode),
    113 	    SCARG(uap, dev), retval, UIO_USERSPACE);
    114 }
    115 
    116 int
    117 compat_50_netbsd32_select(struct lwp *l,
    118     const struct compat_50_netbsd32_select_args *uap, register_t *retval)
    119 {
    120 	/* {
    121 		syscallarg(int) nd;
    122 		syscallarg(netbsd32_fd_setp_t) in;
    123 		syscallarg(netbsd32_fd_setp_t) ou;
    124 		syscallarg(netbsd32_fd_setp_t) ex;
    125 		syscallarg(netbsd32_timeval50p_t) tv;
    126 	} */
    127 	int error;
    128 	struct netbsd32_timeval50 tv32;
    129 	struct timespec ats, *ts = NULL;
    130 
    131 	if (SCARG_P32(uap, tv)) {
    132 		error = copyin(SCARG_P32(uap, tv), &tv32, sizeof(tv32));
    133 		if (error != 0)
    134 			return error;
    135 		ats.tv_sec = tv32.tv_sec;
    136 		ats.tv_nsec = tv32.tv_usec * 1000;
    137 		ts = &ats;
    138 	}
    139 
    140 	return selcommon(retval, SCARG(uap, nd), SCARG_P32(uap, in),
    141 	    SCARG_P32(uap, ou), SCARG_P32(uap, ex), ts, NULL);
    142 	return 0;
    143 }
    144 
    145 int
    146 compat_50_netbsd32_gettimeofday(struct lwp *l,
    147     const struct compat_50_netbsd32_gettimeofday_args *uap, register_t *retval)
    148 {
    149 	/* {
    150 		syscallarg(netbsd32_timeval50p_t) tp;
    151 		syscallarg(netbsd32_timezonep_t) tzp;
    152 	} */
    153 	struct timeval atv;
    154 	struct netbsd32_timeval50 tv32;
    155 	int error = 0;
    156 	struct netbsd32_timezone tzfake;
    157 
    158 	if (SCARG_P32(uap, tp)) {
    159 		microtime(&atv);
    160 		netbsd32_from_timeval50(&atv, &tv32);
    161 		error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
    162 		if (error)
    163 			return error;
    164 	}
    165 	if (SCARG_P32(uap, tzp)) {
    166 		/*
    167 		 * NetBSD has no kernel notion of time zone, so we just
    168 		 * fake up a timezone struct and return it if demanded.
    169 		 */
    170 		tzfake.tz_minuteswest = 0;
    171 		tzfake.tz_dsttime = 0;
    172 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
    173 	}
    174 	return error;
    175 }
    176 
    177 int
    178 compat_50_netbsd32_settimeofday(struct lwp *l,
    179     const struct compat_50_netbsd32_settimeofday_args *uap, register_t *retval)
    180 {
    181 	/* {
    182 		syscallarg(const netbsd32_timeval50p_t) tv;
    183 		syscallarg(const netbsd32_timezonep_t) tzp;
    184 	} */
    185 	struct netbsd32_timeval50 atv32;
    186 	struct timeval atv;
    187 	struct timespec ats;
    188 	int error;
    189 	struct proc *p = l->l_proc;
    190 
    191 	/* Verify all parameters before changing time. */
    192 
    193 	/*
    194 	 * NetBSD has no kernel notion of time zone, and only an
    195 	 * obsolete program would try to set it, so we log a warning.
    196 	 */
    197 	if (SCARG_P32(uap, tzp))
    198 		printf("pid %d attempted to set the "
    199 		    "(obsolete) kernel time zone\n", p->p_pid);
    200 
    201 	if (SCARG_P32(uap, tv) == 0)
    202 		return 0;
    203 
    204 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
    205 		return error;
    206 
    207 	netbsd32_to_timeval50(&atv32, &atv);
    208 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
    209 	return settime(p, &ats);
    210 }
    211 
    212 int
    213 compat_50_netbsd32_utimes(struct lwp *l,
    214     const struct compat_50_netbsd32_utimes_args *uap, register_t *retval)
    215 {
    216 	/* {
    217 		syscallarg(const netbsd32_charp) path;
    218 		syscallarg(const netbsd32_timeval50p_t) tptr;
    219 	} */
    220 	int error;
    221 	struct timeval tv[2], *tvp;
    222 
    223 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    224 	if (error != 0)
    225 		return error;
    226 
    227 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
    228 	    tvp, UIO_SYSSPACE);
    229 }
    230 
    231 int
    232 compat_50_netbsd32_adjtime(struct lwp *l,
    233     const struct compat_50_netbsd32_adjtime_args *uap, register_t *retval)
    234 {
    235 	/* {
    236 		syscallarg(const netbsd32_timeval50p_t) delta;
    237 		syscallarg(netbsd32_timeval50p_t) olddelta;
    238 	} */
    239 	struct netbsd32_timeval50 atv;
    240 	int error;
    241 
    242 	extern int time_adjusted;     /* in kern_ntptime.c */
    243 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
    244 
    245 	if ((error = kauth_authorize_system(l->l_cred,
    246 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
    247 	    NULL)) != 0)
    248 		return (error);
    249 
    250 	if (SCARG_P32(uap, olddelta)) {
    251 		mutex_spin_enter(&timecounter_lock);
    252 		atv.tv_sec = time_adjtime / 1000000;
    253 		atv.tv_usec = time_adjtime % 1000000;
    254 		if (atv.tv_usec < 0) {
    255 			atv.tv_usec += 1000000;
    256 			atv.tv_sec--;
    257 		}
    258 		mutex_spin_exit(&timecounter_lock);
    259 
    260 		error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv));
    261 		if (error)
    262 			return (error);
    263 	}
    264 
    265 	if (SCARG_P32(uap, delta)) {
    266 		error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv));
    267 		if (error)
    268 			return (error);
    269 
    270 		mutex_spin_enter(&timecounter_lock);
    271 		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
    272 		if (time_adjtime)
    273 			/* We need to save the system time during shutdown */
    274 			time_adjusted |= 1;
    275 		mutex_spin_exit(&timecounter_lock);
    276 	}
    277 
    278 	return 0;
    279 }
    280 
    281 int
    282 compat_50_netbsd32_futimes(struct lwp *l,
    283     const struct compat_50_netbsd32_futimes_args *uap, register_t *retval)
    284 {
    285 	/* {
    286 		syscallarg(int) fd;
    287 		syscallarg(const netbsd32_timeval50p_t) tptr;
    288 	} */
    289 	int error;
    290 	file_t *fp;
    291 	struct timeval tv[2], *tvp;
    292 
    293 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    294 	if (error != 0)
    295 		return error;
    296 
    297 	/* fd_getvnode() will use the descriptor for us */
    298 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    299 		return error;
    300 
    301 	error = do_sys_utimes(l, fp->f_data, NULL, 0, tvp, UIO_SYSSPACE);
    302 
    303 	fd_putfile(SCARG(uap, fd));
    304 	return error;
    305 }
    306 
    307 int
    308 compat_50_netbsd32_clock_gettime(struct lwp *l,
    309     const struct compat_50_netbsd32_clock_gettime_args *uap, register_t *retval)
    310 {
    311 	/* {
    312 		syscallarg(netbsd32_clockid_t) clock_id;
    313 		syscallarg(netbsd32_timespec50p_t) tp;
    314 	} */
    315 	int error;
    316 	struct timespec ats;
    317 	struct netbsd32_timespec50 ts32;
    318 
    319 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
    320 	if (error != 0)
    321 		return error;
    322 
    323 	netbsd32_from_timespec50(&ats, &ts32);
    324 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    325 }
    326 
    327 int
    328 compat_50_netbsd32_clock_settime(struct lwp *l,
    329     const struct compat_50_netbsd32_clock_settime_args *uap, register_t *retval)
    330 {
    331 	/* {
    332 		syscallarg(netbsd32_clockid_t) clock_id;
    333 		syscallarg(const netbsd32_timespec50p_t) tp;
    334 	} */
    335 	struct netbsd32_timespec50 ts32;
    336 	struct timespec ats;
    337 	int error;
    338 
    339 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
    340 		return (error);
    341 
    342 	netbsd32_to_timespec50(&ts32, &ats);
    343 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
    344 }
    345 
    346 int
    347 compat_50_netbsd32_clock_getres(struct lwp *l,
    348     const struct compat_50_netbsd32_clock_getres_args *uap, register_t *retval)
    349 {
    350 	/* {
    351 		syscallarg(netbsd32_clockid_t) clock_id;
    352 		syscallarg(netbsd32_timespec50p_t) tp;
    353 	} */
    354 	struct netbsd32_timespec50 ts32;
    355 	struct timespec ts;
    356 	int error = 0;
    357 
    358 	error = clock_getres1(SCARG(uap, clock_id), &ts);
    359 	if (error != 0)
    360 		return error;
    361 
    362 	if (SCARG_P32(uap, tp)) {
    363 		netbsd32_from_timespec50(&ts, &ts32);
    364 		error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
    365 	}
    366 
    367 	return error;
    368 }
    369 
    370 int
    371 compat_50_netbsd32_timer_settime(struct lwp *l,
    372     const struct compat_50_netbsd32_timer_settime_args *uap, register_t *retval)
    373 {
    374 	/* {
    375 		syscallarg(netbsd32_timer_t) timerid;
    376 		syscallarg(int) flags;
    377 		syscallarg(const netbsd32_itimerspec50p_t) value;
    378 		syscallarg(netbsd32_itimerspec50p_t) ovalue;
    379 	} */
    380 	int error;
    381 	struct itimerspec value, ovalue, *ovp = NULL;
    382 	struct netbsd32_itimerspec50 its32;
    383 
    384 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
    385 		return (error);
    386 	netbsd32_to_timespec50(&its32.it_interval, &value.it_interval);
    387 	netbsd32_to_timespec50(&its32.it_value, &value.it_value);
    388 
    389 	if (SCARG_P32(uap, ovalue))
    390 		ovp = &ovalue;
    391 
    392 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
    393 	    SCARG(uap, flags), l->l_proc)) != 0)
    394 		return error;
    395 
    396 	if (ovp) {
    397 		netbsd32_from_timespec50(&ovp->it_interval, &its32.it_interval);
    398 		netbsd32_from_timespec50(&ovp->it_value, &its32.it_value);
    399 		return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
    400 	}
    401 	return 0;
    402 }
    403 
    404 int
    405 compat_50_netbsd32_timer_gettime(struct lwp *l, const struct compat_50_netbsd32_timer_gettime_args *uap, register_t *retval)
    406 {
    407 	/* {
    408 		syscallarg(netbsd32_timer_t) timerid;
    409 		syscallarg(netbsd32_itimerspec50p_t) value;
    410 	} */
    411 	int error;
    412 	struct itimerspec its;
    413 	struct netbsd32_itimerspec50 its32;
    414 
    415 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
    416 	    &its)) != 0)
    417 		return error;
    418 
    419 	netbsd32_from_timespec50(&its.it_interval, &its32.it_interval);
    420 	netbsd32_from_timespec50(&its.it_value, &its32.it_value);
    421 
    422 	return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
    423 }
    424 
    425 int
    426 compat_50_netbsd32_nanosleep(struct lwp *l,
    427     const struct compat_50_netbsd32_nanosleep_args *uap, register_t *retval)
    428 {
    429 	/* {
    430 		syscallarg(const netbsd32_timespec50p_t) rqtp;
    431 		syscallarg(netbsd32_timespecp_t) rmtp;
    432 	} */
    433 	struct netbsd32_timespec50 ts32;
    434 	struct timespec rqt, rmt;
    435 	int error, error1;
    436 
    437 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
    438 	if (error)
    439 		return (error);
    440 	netbsd32_to_timespec50(&ts32, &rqt);
    441 
    442 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
    443 	    SCARG_P32(uap, rmtp) ? &rmt : NULL);
    444 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
    445 		return error;
    446 
    447 	netbsd32_from_timespec50(&rmt, &ts32);
    448 	error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
    449 	return error1 ? error1 : error;
    450 }
    451 
    452 static int
    453 compat_50_netbsd32_sigtimedwait_put_info(const void *src, void *dst, size_t size)
    454 {
    455 	const siginfo_t *info = src;
    456 	siginfo32_t info32;
    457 
    458 	netbsd32_si_to_si32(&info32, info);
    459 
    460 	return copyout(&info32, dst, sizeof(info32));
    461 }
    462 
    463 static int
    464 compat_50_netbsd32_sigtimedwait_fetch_timeout(const void *src, void *dst, size_t size)
    465 {
    466 	struct timespec *ts = dst;
    467 	struct netbsd32_timespec50 ts32;
    468 	int error;
    469 
    470 	error = copyin(src, &ts32, sizeof(ts32));
    471 	if (error)
    472 		return error;
    473 
    474 	netbsd32_to_timespec50(&ts32, ts);
    475 	return 0;
    476 }
    477 
    478 static int
    479 compat_50_netbsd32_sigtimedwait_put_timeout(const void *src, void *dst, size_t size)
    480 {
    481 	const struct timespec *ts = src;
    482 	struct netbsd32_timespec50 ts32;
    483 
    484 	netbsd32_from_timespec50(ts, &ts32);
    485 
    486 	return copyout(&ts32, dst, sizeof(ts32));
    487 }
    488 
    489 int
    490 compat_50_netbsd32___sigtimedwait(struct lwp *l,
    491     const struct compat_50_netbsd32___sigtimedwait_args *uap, register_t *retval)
    492 {
    493 	/* {
    494 		syscallarg(netbsd32_sigsetp_t) set;
    495 		syscallarg(netbsd32_siginfop_t) info;
    496 		syscallarg(netbsd32_timespec50p_t) timeout;
    497 	} */
    498 	struct sys_____sigtimedwait50_args ua;
    499 	int res;
    500 
    501 	NETBSD32TOP_UAP(set, const sigset_t);
    502 	NETBSD32TOP_UAP(info, siginfo_t);
    503 	NETBSD32TOP_UAP(timeout, struct timespec);
    504 
    505 	res = sigtimedwait1(l, &ua, retval,
    506 	    copyin,
    507 	    compat_50_netbsd32_sigtimedwait_put_info,
    508 	    compat_50_netbsd32_sigtimedwait_fetch_timeout,
    509 	    compat_50_netbsd32_sigtimedwait_put_timeout);
    510 	if (!res)
    511 		*retval = 0; /* XXX NetBSD<=5 was not POSIX compliant */
    512 	return res;
    513 }
    514 
    515 int
    516 compat_50_netbsd32_lutimes(struct lwp *l,
    517     const struct compat_50_netbsd32_lutimes_args *uap, register_t *retval)
    518 {
    519 	/* {
    520 		syscallarg(const netbsd32_charp) path;
    521 		syscallarg(const netbsd32_timeval50p_t) tptr;
    522 	} */
    523 	int error;
    524 	struct timeval tv[2], *tvp;
    525 
    526 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    527 	if (error != 0)
    528 		return error;
    529 
    530 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
    531 	    tvp, UIO_SYSSPACE);
    532 }
    533 
    534 int
    535 compat_50_netbsd32__lwp_park(struct lwp *l,
    536     const struct compat_50_netbsd32__lwp_park_args *uap, register_t *retval)
    537 {
    538 	/* {
    539 		syscallarg(const netbsd32_timespec50p) ts;
    540 		syscallarg(lwpid_t) unpark;
    541 		syscallarg(netbsd32_voidp) hint;
    542 		syscallarg(netbsd32_voidp) unparkhint;
    543 	} */
    544 	struct timespec ts, *tsp;
    545 	struct netbsd32_timespec50 ts32;
    546 	int error;
    547 
    548 	if (SCARG_P32(uap, ts) == NULL)
    549 		tsp = NULL;
    550 	else {
    551 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof ts32);
    552 		if (error != 0)
    553 			return error;
    554 		netbsd32_to_timespec50(&ts32, &ts);
    555 		tsp = &ts;
    556 	}
    557 
    558 	if (SCARG(uap, unpark) != 0) {
    559 		error = lwp_unpark(SCARG(uap, unpark),
    560 		    SCARG_P32(uap, unparkhint));
    561 		if (error != 0)
    562 			return error;
    563 	}
    564 
    565 	return lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, tsp,
    566 	    SCARG_P32(uap, hint));
    567 	return 0;
    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 *private, 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 = private;
    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 *private, struct kevent *events,
    604     struct kevent *eventlist, size_t index, int n)
    605 {
    606 	struct netbsd32_kevent *kev32, *events32 = private;
    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 	return 0;
    686 }
    687 
    688 int
    689 compat_50_netbsd32_pollts(struct lwp *l,
    690     const struct compat_50_netbsd32_pollts_args *uap, register_t *retval)
    691 {
    692 	/* {
    693 		syscallarg(struct netbsd32_pollfdp_t) fds;
    694 		syscallarg(u_int) nfds;
    695 		syscallarg(const netbsd32_timespec50p_t) ts;
    696 		syscallarg(const netbsd32_sigsetp_t) mask;
    697 	} */
    698 	int error;
    699 	struct netbsd32_timespec50 ts32;
    700 	struct timespec ats, *ts = NULL;
    701 	sigset_t amask, *mask = NULL;
    702 
    703 	if (SCARG_P32(uap, ts)) {
    704 		error = copyin(SCARG_P32(uap, ts), &ts32, sizeof(ts32));
    705 		if (error != 0)
    706 			return error;
    707 		netbsd32_to_timespec50(&ts32, &ats);
    708 		ts = &ats;
    709 	}
    710 	if (NETBSD32PTR64( SCARG(uap, mask))) {
    711 		error = copyin(SCARG_P32(uap, mask), &amask, sizeof(amask));
    712 		if (error != 0)
    713 			return error;
    714 		mask = &amask;
    715 	}
    716 
    717 	return pollcommon(retval, SCARG_P32(uap, fds),
    718 	    SCARG(uap, nfds), ts, mask);
    719 }
    720 
    721 int
    722 compat_50_netbsd32___stat30(struct lwp *l,
    723     const struct compat_50_netbsd32___stat30_args *uap, register_t *retval)
    724 {
    725 	/* {
    726 		syscallarg(const netbsd32_charp) path;
    727 		syscallarg(netbsd32_stat50p_t) ub;
    728 	} */
    729 	struct netbsd32_stat50 sb32;
    730 	struct stat sb;
    731 	int error;
    732 	const char *path;
    733 
    734 	path = SCARG_P32(uap, path);
    735 
    736 	error = do_sys_stat(path, FOLLOW, &sb);
    737 	if (error)
    738 		return error;
    739 	netbsd32_from___stat50(&sb, &sb32);
    740 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    741 	return error;
    742 }
    743 
    744 int
    745 compat_50_netbsd32___fstat30(struct lwp *l,
    746     const struct compat_50_netbsd32___fstat30_args *uap, register_t *retval)
    747 {
    748 	/* {
    749 		syscallarg(int) fd;
    750 		syscallarg(netbsd32_stat50p_t) sb;
    751 	} */
    752 	struct netbsd32_stat50 sb32;
    753 	struct stat ub;
    754 	int error;
    755 
    756 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    757 	if (error == 0) {
    758 		netbsd32_from___stat50(&ub, &sb32);
    759 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    760 	}
    761 	return error;
    762 }
    763 
    764 int
    765 compat_50_netbsd32___lstat30(struct lwp *l,
    766     const struct compat_50_netbsd32___lstat30_args *uap, register_t *retval)
    767 {
    768 	/* {
    769 		syscallarg(const netbsd32_charp) path;
    770 		syscallarg(netbsd32_stat50p_t) ub;
    771 	} */
    772 	struct netbsd32_stat50 sb32;
    773 	struct stat sb;
    774 	int error;
    775 	const char *path;
    776 
    777 	path = SCARG_P32(uap, path);
    778 
    779 	error = do_sys_stat(path, NOFOLLOW, &sb);
    780 	if (error)
    781 		return error;
    782 	netbsd32_from___stat50(&sb, &sb32);
    783 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    784 	return error;
    785 }
    786 
    787 int
    788 compat_50_netbsd32___fhstat40(struct lwp *l, const struct compat_50_netbsd32___fhstat40_args *uap, register_t *retval)
    789 {
    790 	/* {
    791 		syscallarg(const netbsd32_pointer_t) fhp;
    792 		syscallarg(netbsd32_size_t) fh_size;
    793 		syscallarg(netbsd32_stat50p_t) sb;
    794 	} */
    795 	struct stat sb;
    796 	struct netbsd32_stat50 sb32;
    797 	int error;
    798 
    799 	error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
    800 	if (error == 0) {
    801 		netbsd32_from___stat50(&sb, &sb32);
    802 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    803 	}
    804 	return error;
    805 }
    806 
    807 int
    808 compat_50_netbsd32_wait4(struct lwp *l, const struct compat_50_netbsd32_wait4_args *uap, register_t *retval)
    809 {
    810 	/* {
    811 		syscallarg(int) pid;
    812 		syscallarg(netbsd32_intp) status;
    813 		syscallarg(int) options;
    814 		syscallarg(netbsd32_rusage50p_t) rusage;
    815 	} */
    816 	int error, status, pid = SCARG(uap, pid);
    817 	struct netbsd32_rusage50 ru32;
    818 	struct rusage ru;
    819 
    820 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
    821 	    SCARG_P32(uap, rusage) != NULL ? &ru : NULL);
    822 
    823 	retval[0] = pid;
    824 	if (pid == 0)
    825 		return error;
    826 
    827 	if (SCARG_P32(uap, rusage)) {
    828 		netbsd32_from_rusage50(&ru, &ru32);
    829 		error = copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    830 	}
    831 
    832 	if (error == 0 && SCARG_P32(uap, status))
    833 		error = copyout(&status, SCARG_P32(uap, status), sizeof(status));
    834 
    835 	return error;
    836 }
    837 
    838 
    839 int
    840 compat_50_netbsd32_getrusage(struct lwp *l, const struct compat_50_netbsd32_getrusage_args *uap, register_t *retval)
    841 {
    842 	/* {
    843 		syscallarg(int) who;
    844 		syscallarg(netbsd32_rusage50p_t) rusage;
    845 	} */
    846 	int error;
    847 	struct proc *p = l->l_proc;
    848 	struct rusage ru;
    849 	struct netbsd32_rusage50 ru32;
    850 
    851 	error = getrusage1(p, SCARG(uap, who), &ru);
    852 	if (error != 0)
    853 		return error;
    854 
    855 	netbsd32_from_rusage50(&ru, &ru32);
    856 	return copyout(&ru32, SCARG_P32(uap, rusage), sizeof(ru32));
    857 }
    858 
    859 int
    860 compat_50_netbsd32_setitimer(struct lwp *l,
    861     const struct compat_50_netbsd32_setitimer_args *uap, register_t *retval)
    862 {
    863 	/* {
    864 		syscallarg(int) which;
    865 		syscallarg(const netbsd32_itimerval50p_t) itv;
    866 		syscallarg(netbsd32_itimerval50p_t) oitv;
    867 	} */
    868 	struct proc *p = l->l_proc;
    869 	struct netbsd32_itimerval50 s32it, *itv32;
    870 	int which = SCARG(uap, which);
    871 	struct compat_50_netbsd32_getitimer_args getargs;
    872 	struct itimerval aitv;
    873 	int error;
    874 
    875 	if ((u_int)which > ITIMER_PROF)
    876 		return (EINVAL);
    877 	itv32 = SCARG_P32(uap, itv);
    878 	if (itv32) {
    879 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
    880 			return (error);
    881 		netbsd32_to_itimerval50(&s32it, &aitv);
    882 	}
    883 	if (SCARG_P32(uap, oitv) != 0) {
    884 		SCARG(&getargs, which) = which;
    885 		SCARG(&getargs, itv) = SCARG(uap, oitv);
    886 		if ((error = compat_50_netbsd32_getitimer(l, &getargs, retval)) != 0)
    887 			return (error);
    888 	}
    889 	if (itv32 == 0)
    890 		return 0;
    891 
    892 	return dosetitimer(p, which, &aitv);
    893 }
    894 
    895 int
    896 compat_50_netbsd32_getitimer(struct lwp *l, const struct compat_50_netbsd32_getitimer_args *uap, register_t *retval)
    897 {
    898 	/* {
    899 		syscallarg(int) which;
    900 		syscallarg(netbsd32_itimerval50p_t) itv;
    901 	} */
    902 	struct proc *p = l->l_proc;
    903 	struct netbsd32_itimerval50 s32it;
    904 	struct itimerval aitv;
    905 	int error;
    906 
    907 	error = dogetitimer(p, SCARG(uap, which), &aitv);
    908 	if (error)
    909 		return error;
    910 
    911 	netbsd32_from_itimerval50(&aitv, &s32it);
    912 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
    913 }
    914 
    915 #if defined(SYSVSEM)
    916 
    917 int
    918 compat_50_netbsd32___semctl14(struct lwp *l, const struct compat_50_netbsd32___semctl14_args *uap, register_t *retval)
    919 {
    920 	return do_netbsd32___semctl14(l, uap, retval, NULL);
    921 }
    922 
    923 int
    924 do_netbsd32___semctl14(struct lwp *l, const struct compat_50_netbsd32___semctl14_args *uap, register_t *retval, void *vkarg)
    925 {
    926 	/* {
    927 		syscallarg(int) semid;
    928 		syscallarg(int) semnum;
    929 		syscallarg(int) cmd;
    930 		syscallarg(netbsd32_semun50p_t) arg;
    931 	} */
    932 	struct semid_ds sembuf;
    933 	struct netbsd32_semid_ds50 sembuf32;
    934 	int cmd, error;
    935 	void *pass_arg;
    936 	union __semun karg;
    937 	union netbsd32_semun50 karg32;
    938 
    939 	cmd = SCARG(uap, cmd);
    940 
    941 	switch (cmd) {
    942 	case IPC_SET:
    943 	case IPC_STAT:
    944 		pass_arg = &sembuf;
    945 		break;
    946 
    947 	case GETALL:
    948 	case SETVAL:
    949 	case SETALL:
    950 		pass_arg = &karg;
    951 		break;
    952 	default:
    953 		pass_arg = NULL;
    954 		break;
    955 	}
    956 
    957 	if (pass_arg) {
    958 		if (vkarg != NULL)
    959 			karg32 = *(union netbsd32_semun50 *)vkarg;
    960 		else {
    961 			error = copyin(SCARG_P32(uap, arg), &karg32,
    962 					sizeof(karg32));
    963 			if (error)
    964 				return error;
    965 		}
    966 		if (pass_arg == &karg) {
    967 			switch (cmd) {
    968 			case GETALL:
    969 			case SETALL:
    970 				karg.array = NETBSD32PTR64(karg32.array);
    971 				break;
    972 			case SETVAL:
    973 				karg.val = karg32.val;
    974 				break;
    975 			}
    976 		}
    977 		if (cmd == IPC_SET) {
    978 			error = copyin(NETBSD32PTR64(karg32.buf), &sembuf32,
    979 			    sizeof(sembuf32));
    980 			if (error)
    981 				return (error);
    982 			netbsd32_to_semid_ds50(&sembuf32, &sembuf);
    983 		}
    984 	}
    985 
    986 	error = semctl1(l, SCARG(uap, semid), SCARG(uap, semnum), cmd,
    987 	    pass_arg, retval);
    988 
    989 	if (error == 0 && cmd == IPC_STAT) {
    990 		netbsd32_from_semid_ds50(&sembuf, &sembuf32);
    991 		error = copyout(&sembuf32, NETBSD32PTR64(karg32.buf),
    992 		    sizeof(sembuf32));
    993 	}
    994 
    995 	return (error);
    996 }
    997 #endif
    998 
    999 #if defined(SYSVMSG)
   1000 
   1001 int
   1002 compat_50_netbsd32___msgctl13(struct lwp *l, const struct compat_50_netbsd32___msgctl13_args *uap, register_t *retval)
   1003 {
   1004 	/* {
   1005 		syscallarg(int) msqid;
   1006 		syscallarg(int) cmd;
   1007 		syscallarg(netbsd32_msqid_ds50p_t) buf;
   1008 	} */
   1009 	struct msqid_ds ds;
   1010 	struct netbsd32_msqid_ds50 ds32;
   1011 	int error, cmd;
   1012 
   1013 	cmd = SCARG(uap, cmd);
   1014 	if (cmd == IPC_SET) {
   1015 		error = copyin(SCARG_P32(uap, buf), &ds32, sizeof(ds32));
   1016 		if (error)
   1017 			return error;
   1018 		netbsd32_to_msqid_ds50(&ds32, &ds);
   1019 	}
   1020 
   1021 	error = msgctl1(l, SCARG(uap, msqid), cmd,
   1022 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &ds : NULL);
   1023 
   1024 	if (error == 0 && cmd == IPC_STAT) {
   1025 		netbsd32_from_msqid_ds50(&ds, &ds32);
   1026 		error = copyout(&ds32, SCARG_P32(uap, buf), sizeof(ds32));
   1027 	}
   1028 
   1029 	return error;
   1030 }
   1031 #endif
   1032 
   1033 #if defined(SYSVSHM)
   1034 
   1035 int
   1036 compat_50_netbsd32___shmctl13(struct lwp *l, const struct compat_50_netbsd32___shmctl13_args *uap, register_t *retval)
   1037 {
   1038 	/* {
   1039 		syscallarg(int) shmid;
   1040 		syscallarg(int) cmd;
   1041 		syscallarg(netbsd32_shmid_ds50p_t) buf;
   1042 	} */
   1043 	struct shmid_ds ds;
   1044 	struct netbsd32_shmid_ds50 ds32;
   1045 	int error, cmd;
   1046 
   1047 	cmd = SCARG(uap, cmd);
   1048 	if (cmd == IPC_SET) {
   1049 		error = copyin(SCARG_P32(uap, buf), &ds32, sizeof(ds32));
   1050 		if (error)
   1051 			return error;
   1052 		netbsd32_to_shmid_ds50(&ds32, &ds);
   1053 	}
   1054 
   1055 	error = shmctl1(l, SCARG(uap, shmid), cmd,
   1056 	    (cmd == IPC_SET || cmd == IPC_STAT) ? &ds : NULL);
   1057 
   1058 	if (error == 0 && cmd == IPC_STAT) {
   1059 		netbsd32_from_shmid_ds50(&ds, &ds32);
   1060 		error = copyout(&ds32, SCARG_P32(uap, buf), sizeof(ds32));
   1061 	}
   1062 
   1063 	return error;
   1064 }
   1065 #endif
   1066 
   1067 int
   1068 compat_50_netbsd32_quotactl(struct lwp *l, const struct compat_50_netbsd32_quotactl_args *uap, register_t *retval)
   1069 {
   1070 	/* {
   1071 		syscallarg(const netbsd32_charp) path;
   1072 		syscallarg(int) cmd;
   1073 		syscallarg(int) uid;
   1074 		syscallarg(netbsd32_voidp) arg;
   1075 	} */
   1076 	struct compat_50_sys_quotactl_args ua;
   1077 
   1078 	NETBSD32TOP_UAP(path, const char);
   1079 	NETBSD32TO64_UAP(cmd);
   1080 	NETBSD32TO64_UAP(uid);
   1081 	NETBSD32TOP_UAP(arg, void *);
   1082 	return (compat_50_sys_quotactl(l, &ua, retval));
   1083 }
   1084