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