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