Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_netbsd.c revision 1.13
      1 /*	$NetBSD: netbsd32_netbsd.c,v 1.13 1999/05/26 01:07:07 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include "opt_ktrace.h"
     32 #include "opt_ntp.h"
     33 #include "opt_compat_freebsd.h"
     34 #include "opt_compat_linux.h"
     35 #include "opt_compat_sunos.h"
     36 #include "opt_compat_43.h"
     37 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_LINUX) || \
     38     defined(COMPAT_FREEBSD)
     39 #define COMPAT_OLDSOCK /* used by <sys/socket.h> */
     40 #endif
     41 
     42 #include "fs_lfs.h"
     43 #include "fs_nfs.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/ipc.h>
     49 #include <sys/msg.h>
     50 #include <sys/sem.h>
     51 #include <sys/shm.h>
     52 #include <sys/malloc.h>
     53 #include <sys/mount.h>
     54 #include <sys/socket.h>
     55 #include <sys/sockio.h>
     56 #include <sys/socketvar.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/stat.h>
     59 #include <sys/time.h>
     60 #include <sys/timex.h>
     61 #include <sys/signalvar.h>
     62 #include <sys/wait.h>
     63 #include <sys/ptrace.h>
     64 #include <sys/ktrace.h>
     65 #include <sys/trace.h>
     66 #include <sys/resourcevar.h>
     67 #include <sys/pool.h>
     68 #include <sys/vnode.h>
     69 #include <sys/file.h>
     70 #include <sys/filedesc.h>
     71 #include <sys/namei.h>
     72 
     73 #include <vm/vm.h>
     74 #include <sys/syscallargs.h>
     75 #include <sys/proc.h>
     76 #include <sys/sysctl.h>
     77 
     78 #include <net/if.h>
     79 
     80 #include <compat/netbsd32/netbsd32.h>
     81 #include <compat/netbsd32/netbsd32_syscallargs.h>
     82 
     83 #include <machine/frame.h>
     84 
     85 static __inline void netbsd32_from_timeval __P((struct timeval *, struct netbsd32_timeval *));
     86 static __inline void netbsd32_to_timeval __P((struct netbsd32_timeval *, struct timeval *));
     87 static __inline void netbsd32_from_itimerval __P((struct itimerval *, struct netbsd32_itimerval *));
     88 static __inline void netbsd32_to_itimerval __P((struct netbsd32_itimerval *, struct itimerval *));
     89 static __inline void netbsd32_to_timespec __P((struct netbsd32_timespec *, struct timespec *));
     90 static __inline void netbsd32_from_timespec __P((struct timespec *, struct netbsd32_timespec *));
     91 static __inline void netbsd32_from_rusage __P((struct rusage *, struct netbsd32_rusage *));
     92 static __inline void netbsd32_to_rusage __P((struct netbsd32_rusage *, struct rusage *));
     93 static __inline int netbsd32_to_iovecin __P((struct netbsd32_iovec *, struct iovec *, int));
     94 static __inline void netbsd32_to_msghdr __P((struct netbsd32_msghdr *, struct msghdr *));
     95 static __inline void netbsd32_from_msghdr __P((struct netbsd32_msghdr *, struct msghdr *));
     96 static __inline void netbsd32_from_statfs __P((struct statfs *, struct netbsd32_statfs *));
     97 static __inline void netbsd32_from_timex __P((struct timex *, struct netbsd32_timex *));
     98 static __inline void netbsd32_to_timex __P((struct netbsd32_timex *, struct timex *));
     99 static __inline void netbsd32_from___stat13 __P((struct stat *, struct netbsd32_stat *));
    100 static __inline void netbsd32_to_ipc_perm __P((struct netbsd32_ipc_perm *, struct ipc_perm *));
    101 static __inline void netbsd32_from_ipc_perm __P((struct ipc_perm *, struct netbsd32_ipc_perm *));
    102 static __inline void netbsd32_to_msg __P((struct netbsd32_msg *, struct msg *));
    103 static __inline void netbsd32_from_msg __P((struct msg *, struct netbsd32_msg *));
    104 static __inline void netbsd32_to_msqid_ds __P((struct netbsd32_msqid_ds *, struct msqid_ds *));
    105 static __inline void netbsd32_from_msqid_ds __P((struct msqid_ds *, struct netbsd32_msqid_ds *));
    106 static __inline void netbsd32_to_shmid_ds __P((struct netbsd32_shmid_ds *, struct shmid_ds *));
    107 static __inline void netbsd32_from_shmid_ds __P((struct shmid_ds *, struct netbsd32_shmid_ds *));
    108 static __inline void netbsd32_to_semid_ds __P((struct  netbsd32_semid_ds *, struct  semid_ds *));
    109 static __inline void netbsd32_from_semid_ds __P((struct  semid_ds *, struct  netbsd32_semid_ds *));
    110 
    111 
    112 static int recvit32 __P((struct proc *, int, struct netbsd32_msghdr *, struct iovec *, caddr_t,
    113 			 register_t *));
    114 static int dofilereadv32 __P((struct proc *, int, struct file *, struct netbsd32_iovec *,
    115 			      int, off_t *, int, register_t *));
    116 static int dofilewritev32 __P((struct proc *, int, struct file *, struct netbsd32_iovec *,
    117 			       int,  off_t *, int, register_t *));
    118 static int change_utimes32 __P((struct vnode *, struct timeval *, struct proc *));
    119 
    120 /* converters for structures that we need */
    121 static __inline void
    122 netbsd32_from_timeval(tv, tv32)
    123 	struct timeval *tv;
    124 	struct netbsd32_timeval *tv32;
    125 {
    126 
    127 	tv32->tv_sec = (netbsd32_long)tv->tv_sec;
    128 	tv32->tv_usec = (netbsd32_long)tv->tv_usec;
    129 }
    130 
    131 static __inline void
    132 netbsd32_to_timeval(tv32, tv)
    133 	struct netbsd32_timeval *tv32;
    134 	struct timeval *tv;
    135 {
    136 
    137 	tv->tv_sec = (long)tv32->tv_sec;
    138 	tv->tv_usec = (long)tv32->tv_usec;
    139 }
    140 
    141 static __inline void
    142 netbsd32_from_itimerval(itv, itv32)
    143 	struct itimerval *itv;
    144 	struct netbsd32_itimerval *itv32;
    145 {
    146 
    147 	netbsd32_from_timeval(&itv->it_interval,
    148 			     &itv32->it_interval);
    149 	netbsd32_from_timeval(&itv->it_value,
    150 			     &itv32->it_value);
    151 }
    152 
    153 static __inline void
    154 netbsd32_to_itimerval(itv32, itv)
    155 	struct netbsd32_itimerval *itv32;
    156 	struct itimerval *itv;
    157 {
    158 
    159 	netbsd32_to_timeval(&itv32->it_interval, &itv->it_interval);
    160 	netbsd32_to_timeval(&itv32->it_value, &itv->it_value);
    161 }
    162 
    163 static __inline void
    164 netbsd32_to_timespec(s32p, p)
    165 	struct netbsd32_timespec *s32p;
    166 	struct timespec *p;
    167 {
    168 
    169 	p->tv_sec = s32p->tv_sec;
    170 	p->tv_nsec = (long)s32p->tv_nsec;
    171 }
    172 
    173 static __inline void
    174 netbsd32_from_timespec(p, s32p)
    175 	struct timespec *p;
    176 	struct netbsd32_timespec *s32p;
    177 {
    178 
    179 	s32p->tv_sec = p->tv_sec;
    180 	s32p->tv_nsec = (netbsd32_long)p->tv_nsec;
    181 }
    182 
    183 static __inline void
    184 netbsd32_from_rusage(rup, ru32p)
    185 	struct rusage *rup;
    186 	struct netbsd32_rusage *ru32p;
    187 {
    188 
    189 	netbsd32_from_timeval(&rup->ru_utime, &ru32p->ru_utime);
    190 	netbsd32_from_timeval(&rup->ru_stime, &ru32p->ru_stime);
    191 #define C(var)	ru32p->var = (netbsd32_long)rup->var
    192 	C(ru_maxrss);
    193 	C(ru_ixrss);
    194 	C(ru_idrss);
    195 	C(ru_isrss);
    196 	C(ru_minflt);
    197 	C(ru_majflt);
    198 	C(ru_nswap);
    199 	C(ru_inblock);
    200 	C(ru_oublock);
    201 	C(ru_msgsnd);
    202 	C(ru_msgrcv);
    203 	C(ru_nsignals);
    204 	C(ru_nvcsw);
    205 	C(ru_nivcsw);
    206 #undef C
    207 }
    208 
    209 static __inline void
    210 netbsd32_to_rusage(ru32p, rup)
    211 	struct netbsd32_rusage *ru32p;
    212 	struct rusage *rup;
    213 {
    214 
    215 	netbsd32_to_timeval(&ru32p->ru_utime, &rup->ru_utime);
    216 	netbsd32_to_timeval(&ru32p->ru_stime, &rup->ru_stime);
    217 #define C(var)	rup->var = (long)ru32p->var
    218 	C(ru_maxrss);
    219 	C(ru_ixrss);
    220 	C(ru_idrss);
    221 	C(ru_isrss);
    222 	C(ru_minflt);
    223 	C(ru_majflt);
    224 	C(ru_nswap);
    225 	C(ru_inblock);
    226 	C(ru_oublock);
    227 	C(ru_msgsnd);
    228 	C(ru_msgrcv);
    229 	C(ru_nsignals);
    230 	C(ru_nvcsw);
    231 	C(ru_nivcsw);
    232 #undef C
    233 }
    234 
    235 static __inline int
    236 netbsd32_to_iovecin(iov32p, iovp, len)
    237 	struct netbsd32_iovec *iov32p;
    238 	struct iovec *iovp;
    239 	int len;
    240 {
    241 	int i, error=0;
    242 	u_int32_t iov_base;
    243 	u_int32_t iov_len;
    244 	/*
    245 	 * We could allocate an iov32p, do a copyin, and translate
    246 	 * each field and then free it all up, or we could copyin
    247 	 * each field separately.  I'm doing the latter to reduce
    248 	 * the number of MALLOC()s.
    249 	 */
    250 printf("converting iovec at %p len %lx to %p\n", iov32p, len, iovp);
    251 	for (i = 0; i < len; i++, iovp++, iov32p++) {
    252 		if ((error = copyin((caddr_t)&iov32p->iov_base, &iov_base, sizeof(iov_base))))
    253 		    return (error);
    254 		if ((error = copyin((caddr_t)&iov32p->iov_len, &iov_len, sizeof(iov_len))))
    255 		    return (error);
    256 		iovp->iov_base = (void *)(u_long)iov_base;
    257 		iovp->iov_len = (size_t)iov_len;
    258 printf("iovec slot %d base %p len %lx\n", i, iovp->iov_base, iovp->iov_len);
    259 	}
    260 }
    261 
    262 /* msg_iov must be done separately */
    263 static __inline void
    264 netbsd32_to_msghdr(mhp32, mhp)
    265 	struct netbsd32_msghdr *mhp32;
    266 	struct msghdr *mhp;
    267 {
    268 
    269 	mhp->msg_name = (caddr_t)(u_long)mhp32->msg_name;
    270 	mhp->msg_namelen = mhp32->msg_namelen;
    271 	mhp->msg_iovlen = (size_t)mhp32->msg_iovlen;
    272 	mhp->msg_control = (caddr_t)(u_long)mhp32->msg_control;
    273 	mhp->msg_controllen = mhp32->msg_controllen;
    274 	mhp->msg_flags = mhp32->msg_flags;
    275 }
    276 
    277 /* msg_iov must be done separately */
    278 static __inline void
    279 netbsd32_from_msghdr(mhp32, mhp)
    280 	struct netbsd32_msghdr *mhp32;
    281 	struct msghdr *mhp;
    282 {
    283 
    284 	mhp32->msg_name = mhp32->msg_name;
    285 	mhp32->msg_namelen = mhp32->msg_namelen;
    286 	mhp32->msg_iovlen = mhp32->msg_iovlen;
    287 	mhp32->msg_control = mhp32->msg_control;
    288 	mhp32->msg_controllen = mhp->msg_controllen;
    289 	mhp32->msg_flags = mhp->msg_flags;
    290 }
    291 
    292 static __inline void
    293 netbsd32_from_statfs(sbp, sb32p)
    294 	struct statfs *sbp;
    295 	struct netbsd32_statfs *sb32p;
    296 {
    297 
    298 	sb32p->f_type = sbp->f_type;
    299 	sb32p->f_flags = sbp->f_flags;
    300 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
    301 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
    302 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
    303 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
    304 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
    305 	sb32p->f_files = (netbsd32_long)sbp->f_files;
    306 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
    307 	sb32p->f_fsid = sbp->f_fsid;
    308 	sb32p->f_owner = sbp->f_owner;
    309 	sb32p->f_spare[0] = 0;
    310 	sb32p->f_spare[1] = 0;
    311 	sb32p->f_spare[2] = 0;
    312 	sb32p->f_spare[3] = 0;
    313 #if 1
    314 	/* May as well do the whole batch in one go */
    315 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN+MNAMELEN+MNAMELEN);
    316 #else
    317 	/* If we want to be careful */
    318 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN);
    319 	memcpy(sb32p->f_mntonname, sbp->f_mntonname, MNAMELEN);
    320 	memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    321 #endif
    322 }
    323 
    324 static __inline void
    325 netbsd32_from_timex(txp, tx32p)
    326 	struct timex *txp;
    327 	struct netbsd32_timex *tx32p;
    328 {
    329 
    330 	tx32p->modes = txp->modes;
    331 	tx32p->offset = (netbsd32_long)txp->offset;
    332 	tx32p->freq = (netbsd32_long)txp->freq;
    333 	tx32p->maxerror = (netbsd32_long)txp->maxerror;
    334 	tx32p->esterror = (netbsd32_long)txp->esterror;
    335 	tx32p->status = txp->status;
    336 	tx32p->constant = (netbsd32_long)txp->constant;
    337 	tx32p->precision = (netbsd32_long)txp->precision;
    338 	tx32p->tolerance = (netbsd32_long)txp->tolerance;
    339 	tx32p->ppsfreq = (netbsd32_long)txp->ppsfreq;
    340 	tx32p->jitter = (netbsd32_long)txp->jitter;
    341 	tx32p->shift = txp->shift;
    342 	tx32p->stabil = (netbsd32_long)txp->stabil;
    343 	tx32p->jitcnt = (netbsd32_long)txp->jitcnt;
    344 	tx32p->calcnt = (netbsd32_long)txp->calcnt;
    345 	tx32p->errcnt = (netbsd32_long)txp->errcnt;
    346 	tx32p->stbcnt = (netbsd32_long)txp->stbcnt;
    347 }
    348 
    349 static __inline void
    350 netbsd32_to_timex(tx32p, txp)
    351 	struct netbsd32_timex *tx32p;
    352 	struct timex *txp;
    353 {
    354 
    355 	txp->modes = tx32p->modes;
    356 	txp->offset = (long)tx32p->offset;
    357 	txp->freq = (long)tx32p->freq;
    358 	txp->maxerror = (long)tx32p->maxerror;
    359 	txp->esterror = (long)tx32p->esterror;
    360 	txp->status = tx32p->status;
    361 	txp->constant = (long)tx32p->constant;
    362 	txp->precision = (long)tx32p->precision;
    363 	txp->tolerance = (long)tx32p->tolerance;
    364 	txp->ppsfreq = (long)tx32p->ppsfreq;
    365 	txp->jitter = (long)tx32p->jitter;
    366 	txp->shift = tx32p->shift;
    367 	txp->stabil = (long)tx32p->stabil;
    368 	txp->jitcnt = (long)tx32p->jitcnt;
    369 	txp->calcnt = (long)tx32p->calcnt;
    370 	txp->errcnt = (long)tx32p->errcnt;
    371 	txp->stbcnt = (long)tx32p->stbcnt;
    372 }
    373 
    374 static __inline void
    375 netbsd32_from___stat13(sbp, sb32p)
    376 	struct stat *sbp;
    377 	struct netbsd32_stat *sb32p;
    378 {
    379 	sb32p->st_dev = sbp->st_dev;
    380 	sb32p->st_ino = sbp->st_ino;
    381 	sb32p->st_mode = sbp->st_mode;
    382 	sb32p->st_nlink = sbp->st_nlink;
    383 	sb32p->st_uid = sbp->st_uid;
    384 	sb32p->st_gid = sbp->st_gid;
    385 	sb32p->st_rdev = sbp->st_rdev;
    386 	if (sbp->st_size < (quad_t)1 << 32)
    387 		sb32p->st_size = sbp->st_size;
    388 	else
    389 		sb32p->st_size = -2;
    390 	sb32p->st_atimespec.tv_sec = sbp->st_atimespec.tv_sec;
    391 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
    392 	sb32p->st_mtimespec.tv_sec = sbp->st_mtimespec.tv_sec;
    393 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
    394 	sb32p->st_ctimespec.tv_sec = sbp->st_ctimespec.tv_sec;
    395 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
    396 	sb32p->st_blksize = sbp->st_blksize;
    397 	sb32p->st_blocks = sbp->st_blocks;
    398 	sb32p->st_flags = sbp->st_flags;
    399 	sb32p->st_gen = sbp->st_gen;
    400 }
    401 
    402 static __inline void
    403 netbsd32_to_ipc_perm(ip32p, ipp)
    404 	struct netbsd32_ipc_perm *ip32p;
    405 	struct ipc_perm *ipp;
    406 {
    407 
    408 	ipp->cuid = ip32p->cuid;
    409 	ipp->cgid = ip32p->cgid;
    410 	ipp->uid = ip32p->uid;
    411 	ipp->gid = ip32p->gid;
    412 	ipp->mode = ip32p->mode;
    413 	ipp->seq = ip32p->seq;
    414 	ipp->key = (key_t)ip32p->key;
    415 }
    416 
    417 static __inline void
    418 netbsd32_from_ipc_perm(ipp, ip32p)
    419 	struct ipc_perm *ipp;
    420 	struct netbsd32_ipc_perm *ip32p;
    421 {
    422 
    423 	ip32p->cuid = ipp->cuid;
    424 	ip32p->cgid = ipp->cgid;
    425 	ip32p->uid = ipp->uid;
    426 	ip32p->gid = ipp->gid;
    427 	ip32p->mode = ipp->mode;
    428 	ip32p->seq = ipp->seq;
    429 	ip32p->key = (netbsd32_key_t)ipp->key;
    430 }
    431 
    432 static __inline void
    433 netbsd32_to_msg(m32p, mp)
    434 	struct netbsd32_msg *m32p;
    435 	struct msg *mp;
    436 {
    437 
    438 	mp->msg_next = (struct msg *)(u_long)m32p->msg_next;
    439 	mp->msg_type = (long)m32p->msg_type;
    440 	mp->msg_ts = m32p->msg_ts;
    441 	mp->msg_spot = m32p->msg_spot;
    442 }
    443 
    444 static __inline void
    445 netbsd32_from_msg(mp, m32p)
    446 	struct msg *mp;
    447 	struct netbsd32_msg *m32p;
    448 {
    449 
    450 	m32p->msg_next = (netbsd32_msgp_t)(u_long)mp->msg_next;
    451 	m32p->msg_type = (netbsd32_long)mp->msg_type;
    452 	m32p->msg_ts = mp->msg_ts;
    453 	m32p->msg_spot = mp->msg_spot;
    454 }
    455 
    456 static __inline void
    457 netbsd32_to_msqid_ds(ds32p, dsp)
    458 	struct netbsd32_msqid_ds *ds32p;
    459 	struct msqid_ds *dsp;
    460 {
    461 
    462 	netbsd32_to_ipc_perm(&ds32p->msg_perm, &dsp->msg_perm);
    463 	netbsd32_to_msg((struct netbsd32_msg *)(u_long)ds32p->msg_first, dsp->msg_first);
    464 	netbsd32_to_msg((struct netbsd32_msg *)(u_long)ds32p->msg_last, dsp->msg_last);
    465 	dsp->msg_cbytes = (u_long)ds32p->msg_cbytes;
    466 	dsp->msg_qnum = (u_long)ds32p->msg_qnum;
    467 	dsp->msg_qbytes = (u_long)ds32p->msg_qbytes;
    468 	dsp->msg_lspid = ds32p->msg_lspid;
    469 	dsp->msg_lrpid = ds32p->msg_lrpid;
    470 	dsp->msg_rtime = (time_t)ds32p->msg_rtime;
    471 	dsp->msg_stime = (time_t)ds32p->msg_stime;
    472 	dsp->msg_ctime = (time_t)ds32p->msg_ctime;
    473 }
    474 
    475 static __inline void
    476 netbsd32_from_msqid_ds(dsp, ds32p)
    477 	struct msqid_ds *dsp;
    478 	struct netbsd32_msqid_ds *ds32p;
    479 {
    480 
    481 	netbsd32_from_ipc_perm(&dsp->msg_perm, &ds32p->msg_perm);
    482 	netbsd32_from_msg(dsp->msg_first, (struct netbsd32_msg *)(u_long)ds32p->msg_first);
    483 	netbsd32_from_msg(dsp->msg_last, (struct netbsd32_msg *)(u_long)ds32p->msg_last);
    484 	ds32p->msg_cbytes = (netbsd32_u_long)dsp->msg_cbytes;
    485 	ds32p->msg_qnum = (netbsd32_u_long)dsp->msg_qnum;
    486 	ds32p->msg_qbytes = (netbsd32_u_long)dsp->msg_qbytes;
    487 	ds32p->msg_lspid = dsp->msg_lspid;
    488 	ds32p->msg_lrpid = dsp->msg_lrpid;
    489 	ds32p->msg_rtime = dsp->msg_rtime;
    490 	ds32p->msg_stime = dsp->msg_stime;
    491 	ds32p->msg_ctime = dsp->msg_ctime;
    492 }
    493 
    494 static __inline void
    495 netbsd32_to_shmid_ds(ds32p, dsp)
    496 	struct netbsd32_shmid_ds *ds32p;
    497 	struct shmid_ds *dsp;
    498 {
    499 
    500 	netbsd32_to_ipc_perm(&ds32p->shm_perm, &dsp->shm_perm);
    501 	dsp->shm_segsz = ds32p->shm_segsz;
    502 	dsp->shm_lpid = ds32p->shm_lpid;
    503 	dsp->shm_cpid = ds32p->shm_cpid;
    504 	dsp->shm_nattch = ds32p->shm_nattch;
    505 	dsp->shm_atime = (long)ds32p->shm_atime;
    506 	dsp->shm_dtime = (long)ds32p->shm_dtime;
    507 	dsp->shm_ctime = (long)ds32p->shm_ctime;
    508 	dsp->shm_internal = (void *)(u_long)ds32p->shm_internal;
    509 }
    510 
    511 static __inline void
    512 netbsd32_from_shmid_ds(dsp, ds32p)
    513 	struct shmid_ds *dsp;
    514 	struct netbsd32_shmid_ds *ds32p;
    515 {
    516 
    517 	netbsd32_from_ipc_perm(&dsp->shm_perm, &ds32p->shm_perm);
    518 	ds32p->shm_segsz = dsp->shm_segsz;
    519 	ds32p->shm_lpid = dsp->shm_lpid;
    520 	ds32p->shm_cpid = dsp->shm_cpid;
    521 	ds32p->shm_nattch = dsp->shm_nattch;
    522 	ds32p->shm_atime = (netbsd32_long)dsp->shm_atime;
    523 	ds32p->shm_dtime = (netbsd32_long)dsp->shm_dtime;
    524 	ds32p->shm_ctime = (netbsd32_long)dsp->shm_ctime;
    525 	ds32p->shm_internal = (netbsd32_voidp)(u_long)dsp->shm_internal;
    526 }
    527 
    528 static __inline void
    529 netbsd32_to_semid_ds(s32dsp, dsp)
    530 	struct  netbsd32_semid_ds *s32dsp;
    531 	struct  semid_ds *dsp;
    532 {
    533 
    534 	netbsd32_from_ipc_perm(&dsp->sem_perm, &s32dsp->sem_perm);
    535 	dsp->sem_base = (struct sem *)(u_long)s32dsp->sem_base;
    536 	dsp->sem_nsems = s32dsp->sem_nsems;
    537 	dsp->sem_otime = s32dsp->sem_otime;
    538 	dsp->sem_ctime = s32dsp->sem_ctime;
    539 }
    540 
    541 static __inline void
    542 netbsd32_from_semid_ds(dsp, s32dsp)
    543 	struct  semid_ds *dsp;
    544 	struct  netbsd32_semid_ds *s32dsp;
    545 {
    546 
    547 	netbsd32_to_ipc_perm(&s32dsp->sem_perm, &dsp->sem_perm);
    548 	s32dsp->sem_base = (netbsd32_semp_t)(u_long)dsp->sem_base;
    549 	s32dsp->sem_nsems = dsp->sem_nsems;
    550 	s32dsp->sem_otime = dsp->sem_otime;
    551 	s32dsp->sem_ctime = dsp->sem_ctime;
    552 }
    553 
    554 /*
    555  * below are all the standard NetBSD system calls, in the 32bit
    556  * environment, witht he necessary conversions to 64bit before
    557  * calling the real syscall.
    558  */
    559 
    560 
    561 int
    562 compat_netbsd32_exit(p, v, retval)
    563 	struct proc *p;
    564 	void *v;
    565 	register_t *retval;
    566 {
    567 	struct compat_netbsd32_exit_args /* {
    568 		syscallarg(int) rval;
    569 	} */ *uap = v;
    570 	struct sys_exit_args ua;
    571 
    572 	NETBSD32TO64_UAP(rval);
    573 	sys_exit(p, &ua, retval);
    574 }
    575 
    576 int
    577 compat_netbsd32_read(p, v, retval)
    578 	struct proc *p;
    579 	void *v;
    580 	register_t *retval;
    581 {
    582 	struct compat_netbsd32_read_args /* {
    583 		syscallarg(int) fd;
    584 		syscallarg(netbsd32_voidp) buf;
    585 		syscallarg(netbsd32_size_t) nbyte;
    586 	} */ *uap = v;
    587 	struct sys_read_args ua;
    588 
    589 	NETBSD32TO64_UAP(fd);
    590 	NETBSD32TOP_UAP(buf, void *);
    591 	NETBSD32TOX_UAP(nbyte, size_t);
    592 	return sys_read(p, &ua, retval);
    593 }
    594 
    595 int
    596 compat_netbsd32_write(p, v, retval)
    597 	struct proc *p;
    598 	void *v;
    599 	register_t *retval;
    600 {
    601 	struct compat_netbsd32_write_args /* {
    602 		syscallarg(int) fd;
    603 		syscallarg(const netbsd32_voidp) buf;
    604 		syscallarg(netbsd32_size_t) nbyte;
    605 	} */ *uap = v;
    606 	struct sys_write_args ua;
    607 
    608 	NETBSD32TO64_UAP(fd);
    609 	NETBSD32TOP_UAP(buf, void *);
    610 	NETBSD32TOX_UAP(nbyte, size_t);
    611 	return sys_write(p, &ua, retval);
    612 }
    613 
    614 int
    615 compat_netbsd32_close(p, v, retval)
    616 	struct proc *p;
    617 	void *v;
    618 	register_t *retval;
    619 {
    620 	struct compat_netbsd32_close_args /* {
    621 		syscallarg(int) fd;
    622 	} */ *uap = v;
    623 	struct sys_close_args ua;
    624 
    625 	NETBSD32TO64_UAP(fd);
    626 	return sys_write(p, &ua, retval);
    627 }
    628 
    629 int
    630 compat_netbsd32_open(p, v, retval)
    631 	struct proc *p;
    632 	void *v;
    633 	register_t *retval;
    634 {
    635 	struct compat_netbsd32_open_args /* {
    636 		syscallarg(const netbsd32_charp) path;
    637 		syscallarg(int) flags;
    638 		syscallarg(mode_t) mode;
    639 	} */ *uap = v;
    640 	struct sys_open_args ua;
    641 	caddr_t sg;
    642 
    643 	NETBSD32TOP_UAP(path, const char);
    644 	NETBSD32TO64_UAP(flags);
    645 	NETBSD32TO64_UAP(mode);
    646 	sg = stackgap_init(p->p_emul);
    647 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
    648 
    649 	return (sys_open(p, &ua, retval));
    650 }
    651 
    652 int
    653 compat_netbsd32_wait4(q, v, retval)
    654 	struct proc *q;
    655 	void *v;
    656 	register_t *retval;
    657 {
    658 	struct compat_netbsd32_wait4_args /* {
    659 		syscallarg(int) pid;
    660 		syscallarg(netbsd32_intp) status;
    661 		syscallarg(int) options;
    662 		syscallarg(netbsd32_rusagep_t) rusage;
    663 	} */ *uap = v;
    664 	struct netbsd32_rusage ru32;
    665 	register int nfound;
    666 	register struct proc *p, *t;
    667 	int status, error;
    668 
    669 	if (SCARG(uap, pid) == 0)
    670 		SCARG(uap, pid) = -q->p_pgid;
    671 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
    672 		return (EINVAL);
    673 
    674 loop:
    675 	nfound = 0;
    676 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    677 		if (SCARG(uap, pid) != WAIT_ANY &&
    678 		    p->p_pid != SCARG(uap, pid) &&
    679 		    p->p_pgid != -SCARG(uap, pid))
    680 			continue;
    681 		nfound++;
    682 		if (p->p_stat == SZOMB) {
    683 			retval[0] = p->p_pid;
    684 
    685 			if (SCARG(uap, status)) {
    686 				status = p->p_xstat;	/* convert to int */
    687 				error = copyout((caddr_t)&status,
    688 						(caddr_t)(u_long)SCARG(uap, status),
    689 						sizeof(status));
    690 				if (error)
    691 					return (error);
    692 			}
    693 			if (SCARG(uap, rusage)) {
    694 				netbsd32_from_rusage(p->p_ru, &ru32);
    695 				if ((error = copyout((caddr_t)&ru32,
    696 						     (caddr_t)(u_long)SCARG(uap, rusage),
    697 						     sizeof(struct netbsd32_rusage))))
    698 					return (error);
    699 			}
    700 			/*
    701 			 * If we got the child via ptrace(2) or procfs, and
    702 			 * the parent is different (meaning the process was
    703 			 * attached, rather than run as a child), then we need
    704 			 * to give it back to the old parent, and send the
    705 			 * parent a SIGCHLD.  The rest of the cleanup will be
    706 			 * done when the old parent waits on the child.
    707 			 */
    708 			if ((p->p_flag & P_TRACED) &&
    709 			    p->p_oppid != p->p_pptr->p_pid) {
    710 				t = pfind(p->p_oppid);
    711 				proc_reparent(p, t ? t : initproc);
    712 				p->p_oppid = 0;
    713 				p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    714 				psignal(p->p_pptr, SIGCHLD);
    715 				wakeup((caddr_t)p->p_pptr);
    716 				return (0);
    717 			}
    718 			p->p_xstat = 0;
    719 			ruadd(&q->p_stats->p_cru, p->p_ru);
    720 			pool_put(&rusage_pool, p->p_ru);
    721 
    722 			/*
    723 			 * Finally finished with old proc entry.
    724 			 * Unlink it from its process group and free it.
    725 			 */
    726 			leavepgrp(p);
    727 
    728 			LIST_REMOVE(p, p_list);	/* off zombproc */
    729 
    730 			LIST_REMOVE(p, p_sibling);
    731 
    732 			/*
    733 			 * Decrement the count of procs running with this uid.
    734 			 */
    735 			(void)chgproccnt(p->p_cred->p_ruid, -1);
    736 
    737 			/*
    738 			 * Free up credentials.
    739 			 */
    740 			if (--p->p_cred->p_refcnt == 0) {
    741 				crfree(p->p_cred->pc_ucred);
    742 				pool_put(&pcred_pool, p->p_cred);
    743 			}
    744 
    745 			/*
    746 			 * Release reference to text vnode
    747 			 */
    748 			if (p->p_textvp)
    749 				vrele(p->p_textvp);
    750 
    751 			/*
    752 			 * Give machine-dependent layer a chance
    753 			 * to free anything that cpu_exit couldn't
    754 			 * release while still running in process context.
    755 			 */
    756 			cpu_wait(p);
    757 			pool_put(&proc_pool, p);
    758 			nprocs--;
    759 			return (0);
    760 		}
    761 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
    762 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
    763 			p->p_flag |= P_WAITED;
    764 			retval[0] = p->p_pid;
    765 
    766 			if (SCARG(uap, status)) {
    767 				status = W_STOPCODE(p->p_xstat);
    768 				error = copyout((caddr_t)&status,
    769 				    (caddr_t)(u_long)SCARG(uap, status),
    770 				    sizeof(status));
    771 			} else
    772 				error = 0;
    773 			return (error);
    774 		}
    775 	}
    776 	if (nfound == 0)
    777 		return (ECHILD);
    778 	if (SCARG(uap, options) & WNOHANG) {
    779 		retval[0] = 0;
    780 		return (0);
    781 	}
    782 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
    783 		return (error);
    784 	goto loop;
    785 }
    786 
    787 int
    788 compat_netbsd32_link(p, v, retval)
    789 	struct proc *p;
    790 	void *v;
    791 	register_t *retval;
    792 {
    793 	struct compat_netbsd32_link_args /* {
    794 		syscallarg(const netbsd32_charp) path;
    795 		syscallarg(const netbsd32_charp) link;
    796 	} */ *uap = v;
    797 	struct sys_link_args ua;
    798 
    799 	NETBSD32TOP_UAP(path, const char);
    800 	NETBSD32TOP_UAP(link, const char);
    801 	return (sys_link(p, &ua, retval));
    802 }
    803 
    804 int
    805 compat_netbsd32_unlink(p, v, retval)
    806 	struct proc *p;
    807 	void *v;
    808 	register_t *retval;
    809 {
    810 	struct compat_netbsd32_unlink_args /* {
    811 		syscallarg(const netbsd32_charp) path;
    812 	} */ *uap = v;
    813 	struct sys_unlink_args ua;
    814 
    815 	NETBSD32TOP_UAP(path, const char);
    816 
    817 	return (sys_unlink(p, &ua, retval));
    818 }
    819 
    820 int
    821 compat_netbsd32_chdir(p, v, retval)
    822 	struct proc *p;
    823 	void *v;
    824 	register_t *retval;
    825 {
    826 	struct compat_netbsd32_chdir_args /* {
    827 		syscallarg(const netbsd32_charp) path;
    828 	} */ *uap = v;
    829 	struct sys_chdir_args ua;
    830 
    831 	NETBSD32TOP_UAP(path, const char);
    832 
    833 	return (sys_chdir(p, &ua, retval));
    834 }
    835 
    836 int
    837 compat_netbsd32_fchdir(p, v, retval)
    838 	struct proc *p;
    839 	void *v;
    840 	register_t *retval;
    841 {
    842 	struct compat_netbsd32_fchdir_args /* {
    843 		syscallarg(int) fd;
    844 	} */ *uap = v;
    845 	struct sys_fchdir_args ua;
    846 
    847 	NETBSD32TO64_UAP(fd);
    848 
    849 	return (sys_fchdir(p, &ua, retval));
    850 }
    851 
    852 int
    853 compat_netbsd32_mknod(p, v, retval)
    854 	struct proc *p;
    855 	void *v;
    856 	register_t *retval;
    857 {
    858 	struct compat_netbsd32_mknod_args /* {
    859 		syscallarg(const netbsd32_charp) path;
    860 		syscallarg(mode_t) mode;
    861 		syscallarg(dev_t) dev;
    862 	} */ *uap = v;
    863 	struct sys_mknod_args ua;
    864 
    865 	NETBSD32TOP_UAP(path, const char);
    866 	NETBSD32TO64_UAP(dev);
    867 	NETBSD32TO64_UAP(mode);
    868 
    869 	return (sys_mknod(p, &ua, retval));
    870 }
    871 
    872 int
    873 compat_netbsd32_chmod(p, v, retval)
    874 	struct proc *p;
    875 	void *v;
    876 	register_t *retval;
    877 {
    878 	struct compat_netbsd32_chmod_args /* {
    879 		syscallarg(const netbsd32_charp) path;
    880 		syscallarg(mode_t) mode;
    881 	} */ *uap = v;
    882 	struct sys_chmod_args ua;
    883 
    884 	NETBSD32TOP_UAP(path, const char);
    885 	NETBSD32TO64_UAP(mode);
    886 
    887 	return (sys_chmod(p, &ua, retval));
    888 }
    889 
    890 int
    891 compat_netbsd32_chown(p, v, retval)
    892 	struct proc *p;
    893 	void *v;
    894 	register_t *retval;
    895 {
    896 	struct compat_netbsd32_chown_args /* {
    897 		syscallarg(const netbsd32_charp) path;
    898 		syscallarg(uid_t) uid;
    899 		syscallarg(gid_t) gid;
    900 	} */ *uap = v;
    901 	struct sys_chown_args ua;
    902 
    903 	NETBSD32TOP_UAP(path, const char);
    904 	NETBSD32TO64_UAP(uid);
    905 	NETBSD32TO64_UAP(gid);
    906 
    907 	return (sys_chown(p, &ua, retval));
    908 }
    909 
    910 int
    911 compat_netbsd32_break(p, v, retval)
    912 	struct proc *p;
    913 	void *v;
    914 	register_t *retval;
    915 {
    916 	struct compat_netbsd32_break_args /* {
    917 		syscallarg(netbsd32_charp) nsize;
    918 	} */ *uap = v;
    919 	struct sys_obreak_args ua;
    920 
    921 	SCARG(&ua, nsize) = (char *)(u_long)SCARG(uap, nsize);
    922 	NETBSD32TOP_UAP(nsize, char);
    923 	return (sys_obreak(p, &ua, retval));
    924 }
    925 
    926 int
    927 compat_netbsd32_getfsstat(p, v, retval)
    928 	struct proc *p;
    929 	void *v;
    930 	register_t *retval;
    931 {
    932 	struct compat_netbsd32_getfsstat_args /* {
    933 		syscallarg(netbsd32_statfsp_t) buf;
    934 		syscallarg(netbsd32_long) bufsize;
    935 		syscallarg(int) flags;
    936 	} */ *uap = v;
    937 	struct sys_getfsstat_args ua;
    938 	struct statfs sb;
    939 	struct netbsd32_statfs *sb32p;
    940 	int error;
    941 
    942 	sb32p = (struct netbsd32_statfs *)(u_long)SCARG(uap, buf);
    943 	if (sb32p)
    944 		SCARG(&ua, buf) = &sb;
    945 	else
    946 		SCARG(&ua, buf) = NULL;
    947 	NETBSD32TOX_UAP(bufsize, long);
    948 	NETBSD32TO64_UAP(flags);
    949 	error = sys_getfsstat(p, &ua, retval);
    950 	if (error)
    951 		return (error);
    952 
    953 	if (sb32p) {
    954 		struct netbsd32_statfs sb32;
    955 		netbsd32_from_statfs(&sb, &sb32);
    956 		if (copyout(&sb32, sb32p, sizeof(sb32)))
    957 			return EFAULT;
    958 	}
    959 	return (0);
    960 }
    961 
    962 int
    963 compat_netbsd32_mount(p, v, retval)
    964 	struct proc *p;
    965 	void *v;
    966 	register_t *retval;
    967 {
    968 	struct compat_netbsd32_mount_args /* {
    969 		syscallarg(const netbsd32_charp) type;
    970 		syscallarg(const netbsd32_charp) path;
    971 		syscallarg(int) flags;
    972 		syscallarg(netbsd32_voidp) data;
    973 	} */ *uap = v;
    974 	struct sys_mount_args ua;
    975 
    976 	NETBSD32TOP_UAP(type, const char);
    977 	NETBSD32TOP_UAP(path, const char);
    978 	NETBSD32TO64_UAP(flags);
    979 	NETBSD32TOP_UAP(data, void);
    980 	return (sys_mount(p, &ua, retval));
    981 }
    982 
    983 int
    984 compat_netbsd32_unmount(p, v, retval)
    985 	struct proc *p;
    986 	void *v;
    987 	register_t *retval;
    988 {
    989 	struct compat_netbsd32_unmount_args /* {
    990 		syscallarg(const netbsd32_charp) path;
    991 		syscallarg(int) flags;
    992 	} */ *uap = v;
    993 	struct sys_unmount_args ua;
    994 
    995 	NETBSD32TOP_UAP(path, const char);
    996 	NETBSD32TO64_UAP(flags);
    997 	return (sys_unmount(p, &ua, retval));
    998 }
    999 
   1000 int
   1001 compat_netbsd32_setuid(p, v, retval)
   1002 	struct proc *p;
   1003 	void *v;
   1004 	register_t *retval;
   1005 {
   1006 	struct compat_netbsd32_setuid_args /* {
   1007 		syscallarg(uid_t) uid;
   1008 	} */ *uap = v;
   1009 	struct sys_setuid_args ua;
   1010 
   1011 	NETBSD32TO64_UAP(uid);
   1012 	return (sys_setuid(p, &ua, retval));
   1013 }
   1014 
   1015 int
   1016 compat_netbsd32_ptrace(p, v, retval)
   1017 	struct proc *p;
   1018 	void *v;
   1019 	register_t *retval;
   1020 {
   1021 	struct compat_netbsd32_ptrace_args /* {
   1022 		syscallarg(int) req;
   1023 		syscallarg(pid_t) pid;
   1024 		syscallarg(netbsd32_caddr_t) addr;
   1025 		syscallarg(int) data;
   1026 	} */ *uap = v;
   1027 	struct sys_ptrace_args ua;
   1028 
   1029 	NETBSD32TO64_UAP(req);
   1030 	NETBSD32TO64_UAP(pid);
   1031 	NETBSD32TOX64_UAP(addr, caddr_t);
   1032 	NETBSD32TO64_UAP(data);
   1033 	return (sys_ptrace(p, &ua, retval));
   1034 }
   1035 
   1036 int
   1037 compat_netbsd32_recvmsg(p, v, retval)
   1038 	struct proc *p;
   1039 	void *v;
   1040 	register_t *retval;
   1041 {
   1042 	struct compat_netbsd32_recvmsg_args /* {
   1043 		syscallarg(int) s;
   1044 		syscallarg(netbsd32_msghdrp_t) msg;
   1045 		syscallarg(int) flags;
   1046 	} */ *uap = v;
   1047 	struct netbsd32_msghdr msg;
   1048 	struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
   1049 	register int error;
   1050 
   1051 	error = copyin((caddr_t)(u_long)SCARG(uap, msg), (caddr_t)&msg,
   1052 		       sizeof(msg));
   1053 		/* netbsd32_msghdr needs the iov pre-allocated */
   1054 	if (error)
   1055 		return (error);
   1056 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
   1057 		if ((u_int)msg.msg_iovlen > IOV_MAX)
   1058 			return (EMSGSIZE);
   1059 		MALLOC(iov, struct iovec *,
   1060 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
   1061 		       M_WAITOK);
   1062 	} else if ((u_int)msg.msg_iovlen > 0)
   1063 		iov = aiov;
   1064 	else
   1065 		return (EMSGSIZE);
   1066 #ifdef COMPAT_OLDSOCK
   1067 	msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
   1068 #else
   1069 	msg.msg_flags = SCARG(uap, flags);
   1070 #endif
   1071 	uiov = (struct iovec *)(u_long)msg.msg_iov;
   1072 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)uiov,
   1073 				   iov, msg.msg_iovlen);
   1074 	if (error)
   1075 		goto done;
   1076 	if ((error = recvit32(p, SCARG(uap, s), &msg, iov, (caddr_t)0, retval)) == 0) {
   1077 		error = copyout((caddr_t)&msg, (caddr_t)(u_long)SCARG(uap, msg),
   1078 		    sizeof(msg));
   1079 	}
   1080 done:
   1081 	if (iov != aiov)
   1082 		FREE(iov, M_IOV);
   1083 	return (error);
   1084 }
   1085 
   1086 int
   1087 recvit32(p, s, mp, iov, namelenp, retsize)
   1088 	struct proc *p;
   1089 	int s;
   1090 	struct netbsd32_msghdr *mp;
   1091 	struct iovec *iov;
   1092 	caddr_t namelenp;
   1093 	register_t *retsize;
   1094 {
   1095 	struct file *fp;
   1096 	struct uio auio;
   1097 	register int i;
   1098 	int len, error;
   1099 	struct mbuf *from = 0, *control = 0;
   1100 	struct socket *so;
   1101 #ifdef KTRACE
   1102 	struct iovec *ktriov = NULL;
   1103 #endif
   1104 
   1105 	/* getsock() will use the descriptor for us */
   1106 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
   1107 		return (error);
   1108 	auio.uio_iov = (struct iovec *)(u_long)mp->msg_iov;
   1109 	auio.uio_iovcnt = mp->msg_iovlen;
   1110 	auio.uio_segflg = UIO_USERSPACE;
   1111 	auio.uio_rw = UIO_READ;
   1112 	auio.uio_procp = p;
   1113 	auio.uio_offset = 0;			/* XXX */
   1114 	auio.uio_resid = 0;
   1115 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
   1116 #if 0
   1117 		/* cannot happen iov_len is unsigned */
   1118 		if (iov->iov_len < 0) {
   1119 			error = EINVAL;
   1120 			goto out1;
   1121 		}
   1122 #endif
   1123 		/*
   1124 		 * Reads return ssize_t because -1 is returned on error.
   1125 		 * Therefore we must restrict the length to SSIZE_MAX to
   1126 		 * avoid garbage return values.
   1127 		 */
   1128 		auio.uio_resid += iov->iov_len;
   1129 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   1130 			error = EINVAL;
   1131 			goto out1;
   1132 		}
   1133 	}
   1134 #ifdef KTRACE
   1135 	if (KTRPOINT(p, KTR_GENIO)) {
   1136 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
   1137 
   1138 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   1139 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   1140 	}
   1141 #endif
   1142 	len = auio.uio_resid;
   1143 	so = (struct socket *)fp->f_data;
   1144 	error = (*so->so_receive)(so, &from, &auio, NULL,
   1145 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
   1146 	if (error) {
   1147 		if (auio.uio_resid != len && (error == ERESTART ||
   1148 		    error == EINTR || error == EWOULDBLOCK))
   1149 			error = 0;
   1150 	}
   1151 #ifdef KTRACE
   1152 	if (ktriov != NULL) {
   1153 		if (error == 0)
   1154 			ktrgenio(p->p_tracep, s, UIO_READ,
   1155 				ktriov, len - auio.uio_resid, error);
   1156 		FREE(ktriov, M_TEMP);
   1157 	}
   1158 #endif
   1159 	if (error)
   1160 		goto out;
   1161 	*retsize = len - auio.uio_resid;
   1162 	if (mp->msg_name) {
   1163 		len = mp->msg_namelen;
   1164 		if (len <= 0 || from == 0)
   1165 			len = 0;
   1166 		else {
   1167 #ifdef COMPAT_OLDSOCK
   1168 			if (mp->msg_flags & MSG_COMPAT)
   1169 				mtod(from, struct osockaddr *)->sa_family =
   1170 				    mtod(from, struct sockaddr *)->sa_family;
   1171 #endif
   1172 			if (len > from->m_len)
   1173 				len = from->m_len;
   1174 			/* else if len < from->m_len ??? */
   1175 			error = copyout(mtod(from, caddr_t),
   1176 					(caddr_t)(u_long)mp->msg_name, (unsigned)len);
   1177 			if (error)
   1178 				goto out;
   1179 		}
   1180 		mp->msg_namelen = len;
   1181 		if (namelenp &&
   1182 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
   1183 #ifdef COMPAT_OLDSOCK
   1184 			if (mp->msg_flags & MSG_COMPAT)
   1185 				error = 0;	/* old recvfrom didn't check */
   1186 			else
   1187 #endif
   1188 			goto out;
   1189 		}
   1190 	}
   1191 	if (mp->msg_control) {
   1192 #ifdef COMPAT_OLDSOCK
   1193 		/*
   1194 		 * We assume that old recvmsg calls won't receive access
   1195 		 * rights and other control info, esp. as control info
   1196 		 * is always optional and those options didn't exist in 4.3.
   1197 		 * If we receive rights, trim the cmsghdr; anything else
   1198 		 * is tossed.
   1199 		 */
   1200 		if (control && mp->msg_flags & MSG_COMPAT) {
   1201 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
   1202 			    SOL_SOCKET ||
   1203 			    mtod(control, struct cmsghdr *)->cmsg_type !=
   1204 			    SCM_RIGHTS) {
   1205 				mp->msg_controllen = 0;
   1206 				goto out;
   1207 			}
   1208 			control->m_len -= sizeof(struct cmsghdr);
   1209 			control->m_data += sizeof(struct cmsghdr);
   1210 		}
   1211 #endif
   1212 		len = mp->msg_controllen;
   1213 		if (len <= 0 || control == 0)
   1214 			len = 0;
   1215 		else {
   1216 			struct mbuf *m = control;
   1217 			caddr_t p = (caddr_t)(u_long)mp->msg_control;
   1218 
   1219 			do {
   1220 				i = m->m_len;
   1221 				if (len < i) {
   1222 					mp->msg_flags |= MSG_CTRUNC;
   1223 					i = len;
   1224 				}
   1225 				error = copyout(mtod(m, caddr_t), p,
   1226 				    (unsigned)i);
   1227 				if (m->m_next)
   1228 					i = ALIGN(i);
   1229 				p += i;
   1230 				len -= i;
   1231 				if (error != 0 || len <= 0)
   1232 					break;
   1233 			} while ((m = m->m_next) != NULL);
   1234 			len = p - (caddr_t)(u_long)mp->msg_control;
   1235 		}
   1236 		mp->msg_controllen = len;
   1237 	}
   1238  out:
   1239 	if (from)
   1240 		m_freem(from);
   1241 	if (control)
   1242 		m_freem(control);
   1243  out1:
   1244 	FILE_UNUSE(fp);
   1245 	return (error);
   1246 }
   1247 
   1248 
   1249 int
   1250 compat_netbsd32_sendmsg(p, v, retval)
   1251 	struct proc *p;
   1252 	void *v;
   1253 	register_t *retval;
   1254 {
   1255 	struct compat_netbsd32_sendmsg_args /* {
   1256 		syscallarg(int) s;
   1257 		syscallarg(const netbsd32_msghdrp_t) msg;
   1258 		syscallarg(int) flags;
   1259 	} */ *uap = v;
   1260 	struct msghdr msg;
   1261 	struct netbsd32_msghdr msg32;
   1262 	struct iovec aiov[UIO_SMALLIOV], *iov;
   1263 	int error;
   1264 
   1265 	error = copyin((caddr_t)(u_long)SCARG(uap, msg),
   1266 		       (caddr_t)&msg32, sizeof(msg32));
   1267 	if (error)
   1268 		return (error);
   1269 	netbsd32_to_msghdr(&msg32, &msg);
   1270 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
   1271 		if ((u_int)msg.msg_iovlen > IOV_MAX)
   1272 			return (EMSGSIZE);
   1273 		MALLOC(iov, struct iovec *,
   1274 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
   1275 		       M_WAITOK);
   1276 	} else if ((u_int)msg.msg_iovlen > 0)
   1277 		iov = aiov;
   1278 	else
   1279 		return (EMSGSIZE);
   1280 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
   1281 				   iov, msg.msg_iovlen);
   1282 	if (error)
   1283 		goto done;
   1284 	msg.msg_iov = iov;
   1285 #ifdef COMPAT_OLDSOCK
   1286 	msg.msg_flags = 0;
   1287 #endif
   1288 	/* Luckily we can use this directly */
   1289 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
   1290 done:
   1291 	if (iov != aiov)
   1292 		FREE(iov, M_IOV);
   1293 	return (error);
   1294 }
   1295 
   1296 int
   1297 compat_netbsd32_recvfrom(p, v, retval)
   1298 	struct proc *p;
   1299 	void *v;
   1300 	register_t *retval;
   1301 {
   1302 	struct compat_netbsd32_recvfrom_args /* {
   1303 		syscallarg(int) s;
   1304 		syscallarg(netbsd32_voidp) buf;
   1305 		syscallarg(netbsd32_size_t) len;
   1306 		syscallarg(int) flags;
   1307 		syscallarg(netbsd32_sockaddrp_t) from;
   1308 		syscallarg(netbsd32_intp) fromlenaddr;
   1309 	} */ *uap = v;
   1310 	struct netbsd32_msghdr msg;
   1311 	struct iovec aiov;
   1312 	int error;
   1313 
   1314 	if (SCARG(uap, fromlenaddr)) {
   1315 		error = copyin((caddr_t)(u_long)SCARG(uap, fromlenaddr),
   1316 			       (caddr_t)&msg.msg_namelen,
   1317 			       sizeof(msg.msg_namelen));
   1318 		if (error)
   1319 			return (error);
   1320 	} else
   1321 		msg.msg_namelen = 0;
   1322 	msg.msg_name = SCARG(uap, from);
   1323 	msg.msg_iov = NULL; /* We can't store a real pointer here */
   1324 	msg.msg_iovlen = 1;
   1325 	aiov.iov_base = (caddr_t)(u_long)SCARG(uap, buf);
   1326 	aiov.iov_len = (u_long)SCARG(uap, len);
   1327 	msg.msg_control = 0;
   1328 	msg.msg_flags = SCARG(uap, flags);
   1329 	return (recvit32(p, SCARG(uap, s), &msg, &aiov,
   1330 		       (caddr_t)(u_long)SCARG(uap, fromlenaddr), retval));
   1331 }
   1332 
   1333 int
   1334 compat_netbsd32_sendto(p, v, retval)
   1335 	struct proc *p;
   1336 	void *v;
   1337 	register_t *retval;
   1338 {
   1339 	struct compat_netbsd32_sendto_args /* {
   1340 		syscallarg(int) s;
   1341 		syscallarg(const netbsd32_voidp) buf;
   1342 		syscallarg(netbsd32_size_t) len;
   1343 		syscallarg(int) flags;
   1344 		syscallarg(const netbsd32_sockaddrp_t) to;
   1345 		syscallarg(int) tolen;
   1346 	} */ *uap = v;
   1347 	struct msghdr msg;
   1348 	struct iovec aiov;
   1349 
   1350 	msg.msg_name = (caddr_t)(u_long)SCARG(uap, to);		/* XXX kills const */
   1351 	msg.msg_namelen = SCARG(uap, tolen);
   1352 	msg.msg_iov = &aiov;
   1353 	msg.msg_iovlen = 1;
   1354 	msg.msg_control = 0;
   1355 #ifdef COMPAT_OLDSOCK
   1356 	msg.msg_flags = 0;
   1357 #endif
   1358 	aiov.iov_base = (char *)(u_long)SCARG(uap, buf);	/* XXX kills const */
   1359 	aiov.iov_len = SCARG(uap, len);
   1360 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
   1361 }
   1362 
   1363 int
   1364 compat_netbsd32_accept(p, v, retval)
   1365 	struct proc *p;
   1366 	void *v;
   1367 	register_t *retval;
   1368 {
   1369 	struct compat_netbsd32_accept_args /* {
   1370 		syscallarg(int) s;
   1371 		syscallarg(netbsd32_sockaddrp_t) name;
   1372 		syscallarg(netbsd32_intp) anamelen;
   1373 	} */ *uap = v;
   1374 	struct sys_accept_args ua;
   1375 
   1376 	NETBSD32TO64_UAP(s);
   1377 	NETBSD32TOP_UAP(name, struct sockaddr);
   1378 	NETBSD32TOP_UAP(anamelen, int);
   1379 	return (sys_accept(p, &ua, retval));
   1380 }
   1381 
   1382 int
   1383 compat_netbsd32_getpeername(p, v, retval)
   1384 	struct proc *p;
   1385 	void *v;
   1386 	register_t *retval;
   1387 {
   1388 	struct compat_netbsd32_getpeername_args /* {
   1389 		syscallarg(int) fdes;
   1390 		syscallarg(netbsd32_sockaddrp_t) asa;
   1391 		syscallarg(netbsd32_intp) alen;
   1392 	} */ *uap = v;
   1393 	struct sys_getpeername_args ua;
   1394 
   1395 	NETBSD32TO64_UAP(fdes);
   1396 	NETBSD32TOP_UAP(asa, struct sockaddr);
   1397 	NETBSD32TOP_UAP(alen, int);
   1398 /* NB: do the protocol specific sockaddrs need to be converted? */
   1399 	return (sys_getpeername(p, &ua, retval));
   1400 }
   1401 
   1402 int
   1403 compat_netbsd32_getsockname(p, v, retval)
   1404 	struct proc *p;
   1405 	void *v;
   1406 	register_t *retval;
   1407 {
   1408 	struct compat_netbsd32_getsockname_args /* {
   1409 		syscallarg(int) fdes;
   1410 		syscallarg(netbsd32_sockaddrp_t) asa;
   1411 		syscallarg(netbsd32_intp) alen;
   1412 	} */ *uap = v;
   1413 	struct sys_getsockname_args ua;
   1414 
   1415 	NETBSD32TO64_UAP(fdes);
   1416 	NETBSD32TOP_UAP(asa, struct sockaddr);
   1417 	NETBSD32TOP_UAP(alen, int);
   1418 	return (sys_getsockname(p, &ua, retval));
   1419 }
   1420 
   1421 int
   1422 compat_netbsd32_access(p, v, retval)
   1423 	struct proc *p;
   1424 	void *v;
   1425 	register_t *retval;
   1426 {
   1427 	struct compat_netbsd32_access_args /* {
   1428 		syscallarg(const netbsd32_charp) path;
   1429 		syscallarg(int) flags;
   1430 	} */ *uap = v;
   1431 	struct sys_access_args ua;
   1432 	caddr_t sg;
   1433 
   1434 	NETBSD32TOP_UAP(path, const char);
   1435 	NETBSD32TO64_UAP(flags);
   1436 	sg = stackgap_init(p->p_emul);
   1437 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1438 
   1439 	return (sys_access(p, &ua, retval));
   1440 }
   1441 
   1442 int
   1443 compat_netbsd32_chflags(p, v, retval)
   1444 	struct proc *p;
   1445 	void *v;
   1446 	register_t *retval;
   1447 {
   1448 	struct compat_netbsd32_chflags_args /* {
   1449 		syscallarg(const netbsd32_charp) path;
   1450 		syscallarg(netbsd32_u_long) flags;
   1451 	} */ *uap = v;
   1452 	struct sys_chflags_args ua;
   1453 
   1454 	NETBSD32TOP_UAP(path, const char);
   1455 	NETBSD32TO64_UAP(flags);
   1456 
   1457 	return (sys_chflags(p, &ua, retval));
   1458 }
   1459 
   1460 int
   1461 compat_netbsd32_fchflags(p, v, retval)
   1462 	struct proc *p;
   1463 	void *v;
   1464 	register_t *retval;
   1465 {
   1466 	struct compat_netbsd32_fchflags_args /* {
   1467 		syscallarg(int) fd;
   1468 		syscallarg(netbsd32_u_long) flags;
   1469 	} */ *uap = v;
   1470 	struct sys_fchflags_args ua;
   1471 
   1472 	NETBSD32TO64_UAP(fd);
   1473 	NETBSD32TO64_UAP(flags);
   1474 
   1475 	return (sys_fchflags(p, &ua, retval));
   1476 }
   1477 
   1478 int
   1479 compat_netbsd32_kill(p, v, retval)
   1480 	struct proc *p;
   1481 	void *v;
   1482 	register_t *retval;
   1483 {
   1484 	struct compat_netbsd32_kill_args /* {
   1485 		syscallarg(int) pid;
   1486 		syscallarg(int) signum;
   1487 	} */ *uap = v;
   1488 	struct sys_kill_args ua;
   1489 
   1490 	NETBSD32TO64_UAP(pid);
   1491 	NETBSD32TO64_UAP(signum);
   1492 
   1493 	return (sys_kill(p, &ua, retval));
   1494 }
   1495 
   1496 int
   1497 compat_netbsd32_dup(p, v, retval)
   1498 	struct proc *p;
   1499 	void *v;
   1500 	register_t *retval;
   1501 {
   1502 	struct compat_netbsd32_dup_args /* {
   1503 		syscallarg(int) fd;
   1504 	} */ *uap = v;
   1505 	struct sys_dup_args ua;
   1506 
   1507 	NETBSD32TO64_UAP(fd);
   1508 
   1509 	return (sys_dup(p, &ua, retval));
   1510 }
   1511 
   1512 int
   1513 compat_netbsd32_profil(p, v, retval)
   1514 	struct proc *p;
   1515 	void *v;
   1516 	register_t *retval;
   1517 {
   1518 	struct compat_netbsd32_profil_args /* {
   1519 		syscallarg(netbsd32_caddr_t) samples;
   1520 		syscallarg(netbsd32_size_t) size;
   1521 		syscallarg(netbsd32_u_long) offset;
   1522 		syscallarg(u_int) scale;
   1523 	} */ *uap = v;
   1524 	struct sys_profil_args ua;
   1525 
   1526 	NETBSD32TOX64_UAP(samples, caddr_t);
   1527 	NETBSD32TOX_UAP(size, size_t);
   1528 	NETBSD32TOX_UAP(offset, u_long);
   1529 	NETBSD32TO64_UAP(scale);
   1530 	return (sys_profil(p, &ua, retval));
   1531 }
   1532 
   1533 int
   1534 compat_netbsd32_ktrace(p, v, retval)
   1535 	struct proc *p;
   1536 	void *v;
   1537 	register_t *retval;
   1538 {
   1539 	struct compat_netbsd32_ktrace_args /* {
   1540 		syscallarg(const netbsd32_charp) fname;
   1541 		syscallarg(int) ops;
   1542 		syscallarg(int) facs;
   1543 		syscallarg(int) pid;
   1544 	} */ *uap = v;
   1545 	struct sys_ktrace_args ua;
   1546 
   1547 	NETBSD32TOP_UAP(fname, const char);
   1548 	NETBSD32TO64_UAP(ops);
   1549 	NETBSD32TO64_UAP(facs);
   1550 	NETBSD32TO64_UAP(pid);
   1551 	return (sys_ktrace(p, &ua, retval));
   1552 }
   1553 
   1554 int
   1555 compat_netbsd32_sigaction(p, v, retval)
   1556 	struct proc *p;
   1557 	void *v;
   1558 	register_t *retval;
   1559 {
   1560 	struct compat_netbsd32_sigaction_args /* {
   1561 		syscallarg(int) signum;
   1562 		syscallarg(const netbsd32_sigactionp_t) nsa;
   1563 		syscallarg(netbsd32_sigactionp_t) osa;
   1564 	} */ *uap = v;
   1565 	struct sigaction nsa, osa;
   1566 	struct netbsd32_sigaction *sa32p, sa32;
   1567 	int error;
   1568 
   1569 	if (SCARG(uap, nsa)) {
   1570 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, nsa);
   1571 		if (copyin(sa32p, &sa32, sizeof(sa32)))
   1572 			return EFAULT;
   1573 		nsa.sa_handler = (void *)(u_long)sa32.sa_handler;
   1574 		nsa.sa_mask = sa32.sa_mask;
   1575 		nsa.sa_flags = sa32.sa_flags;
   1576 	}
   1577 	error = sigaction1(p, SCARG(uap, signum),
   1578 			   SCARG(uap, nsa) ? &nsa : 0,
   1579 			   SCARG(uap, osa) ? &osa : 0);
   1580 
   1581 	if (error)
   1582 		return (error);
   1583 
   1584 	if (SCARG(uap, osa)) {
   1585 		sa32.sa_handler = (netbsd32_sigactionp_t)(u_long)osa.sa_handler;
   1586 		sa32.sa_mask = osa.sa_mask;
   1587 		sa32.sa_flags = osa.sa_flags;
   1588 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, osa);
   1589 		if (copyout(&sa32, sa32p, sizeof(sa32)))
   1590 			return EFAULT;
   1591 	}
   1592 
   1593 	return (0);
   1594 }
   1595 
   1596 int
   1597 compat_netbsd32___getlogin(p, v, retval)
   1598 	struct proc *p;
   1599 	void *v;
   1600 	register_t *retval;
   1601 {
   1602 	struct compat_netbsd32___getlogin_args /* {
   1603 		syscallarg(netbsd32_charp) namebuf;
   1604 		syscallarg(u_int) namelen;
   1605 	} */ *uap = v;
   1606 	struct sys___getlogin_args ua;
   1607 
   1608 	NETBSD32TOP_UAP(namebuf, char);
   1609 	NETBSD32TO64_UAP(namelen);
   1610 	return (sys___getlogin(p, &ua, retval));
   1611 }
   1612 
   1613 int
   1614 compat_netbsd32_setlogin(p, v, retval)
   1615 	struct proc *p;
   1616 	void *v;
   1617 	register_t *retval;
   1618 {
   1619 	struct compat_netbsd32_setlogin_args /* {
   1620 		syscallarg(const netbsd32_charp) namebuf;
   1621 	} */ *uap = v;
   1622 	struct sys_setlogin_args ua;
   1623 
   1624 	NETBSD32TOP_UAP(namebuf, char);
   1625 	return (sys_setlogin(p, &ua, retval));
   1626 }
   1627 
   1628 int
   1629 compat_netbsd32_acct(p, v, retval)
   1630 	struct proc *p;
   1631 	void *v;
   1632 	register_t *retval;
   1633 {
   1634 	struct compat_netbsd32_acct_args /* {
   1635 		syscallarg(const netbsd32_charp) path;
   1636 	} */ *uap = v;
   1637 	struct sys_acct_args ua;
   1638 
   1639 	NETBSD32TOP_UAP(path, const char);
   1640 	return (sys_acct(p, &ua, retval));
   1641 }
   1642 
   1643 int
   1644 compat_netbsd32_revoke(p, v, retval)
   1645 	struct proc *p;
   1646 	void *v;
   1647 	register_t *retval;
   1648 {
   1649 	struct compat_netbsd32_revoke_args /* {
   1650 		syscallarg(const netbsd32_charp) path;
   1651 	} */ *uap = v;
   1652 	struct sys_revoke_args ua;
   1653 	caddr_t sg;
   1654 
   1655 	NETBSD32TOP_UAP(path, const char);
   1656 	sg = stackgap_init(p->p_emul);
   1657 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1658 
   1659 	return (sys_revoke(p, &ua, retval));
   1660 }
   1661 
   1662 int
   1663 compat_netbsd32_symlink(p, v, retval)
   1664 	struct proc *p;
   1665 	void *v;
   1666 	register_t *retval;
   1667 {
   1668 	struct compat_netbsd32_symlink_args /* {
   1669 		syscallarg(const netbsd32_charp) path;
   1670 		syscallarg(const netbsd32_charp) link;
   1671 	} */ *uap = v;
   1672 	struct sys_symlink_args ua;
   1673 
   1674 	NETBSD32TOP_UAP(path, const char);
   1675 	NETBSD32TOP_UAP(link, const char);
   1676 
   1677 	return (sys_symlink(p, &ua, retval));
   1678 }
   1679 
   1680 int
   1681 compat_netbsd32_readlink(p, v, retval)
   1682 	struct proc *p;
   1683 	void *v;
   1684 	register_t *retval;
   1685 {
   1686 	struct compat_netbsd32_readlink_args /* {
   1687 		syscallarg(const netbsd32_charp) path;
   1688 		syscallarg(netbsd32_charp) buf;
   1689 		syscallarg(netbsd32_size_t) count;
   1690 	} */ *uap = v;
   1691 	struct sys_readlink_args ua;
   1692 	caddr_t sg;
   1693 
   1694 	NETBSD32TOP_UAP(path, const char);
   1695 	NETBSD32TOP_UAP(buf, char);
   1696 	NETBSD32TOX_UAP(count, size_t);
   1697 	sg = stackgap_init(p->p_emul);
   1698 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1699 
   1700 	return (sys_readlink(p, &ua, retval));
   1701 }
   1702 
   1703 int
   1704 compat_netbsd32_execve(p, v, retval)
   1705 	struct proc *p;
   1706 	void *v;
   1707 	register_t *retval;
   1708 {
   1709 	struct compat_netbsd32_execve_args /* {
   1710 		syscallarg(const netbsd32_charp) path;
   1711 		syscallarg(netbsd32_charpp) argp;
   1712 		syscallarg(netbsd32_charpp) envp;
   1713 	} */ *uap = v;
   1714 	struct sys_execve_args ua;
   1715 	caddr_t sg;
   1716 
   1717 	NETBSD32TOP_UAP(path, const char);
   1718 	NETBSD32TOP_UAP(argp, char *);
   1719 	NETBSD32TOP_UAP(envp, char *);
   1720 	sg = stackgap_init(p->p_emul);
   1721 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1722 
   1723 	return (sys_execve(p, &ua, retval));
   1724 }
   1725 
   1726 int
   1727 compat_netbsd32_umask(p, v, retval)
   1728 	struct proc *p;
   1729 	void *v;
   1730 	register_t *retval;
   1731 {
   1732 	struct compat_netbsd32_umask_args /* {
   1733 		syscallarg(mode_t) newmask;
   1734 	} */ *uap = v;
   1735 	struct sys_umask_args ua;
   1736 
   1737 	NETBSD32TO64_UAP(newmask);
   1738 	return (sys_umask(p, &ua, retval));
   1739 }
   1740 
   1741 int
   1742 compat_netbsd32_chroot(p, v, retval)
   1743 	struct proc *p;
   1744 	void *v;
   1745 	register_t *retval;
   1746 {
   1747 	struct compat_netbsd32_chroot_args /* {
   1748 		syscallarg(const netbsd32_charp) path;
   1749 	} */ *uap = v;
   1750 	struct sys_chroot_args ua;
   1751 
   1752 	NETBSD32TOP_UAP(path, const char);
   1753 	return (sys_chroot(p, &ua, retval));
   1754 }
   1755 
   1756 int
   1757 compat_netbsd32_sbrk(p, v, retval)
   1758 	struct proc *p;
   1759 	void *v;
   1760 	register_t *retval;
   1761 {
   1762 	struct compat_netbsd32_sbrk_args /* {
   1763 		syscallarg(int) incr;
   1764 	} */ *uap = v;
   1765 	struct sys_sbrk_args ua;
   1766 
   1767 	NETBSD32TO64_UAP(incr);
   1768 	return (sys_sbrk(p, &ua, retval));
   1769 }
   1770 
   1771 int
   1772 compat_netbsd32_sstk(p, v, retval)
   1773 	struct proc *p;
   1774 	void *v;
   1775 	register_t *retval;
   1776 {
   1777 	struct compat_netbsd32_sstk_args /* {
   1778 		syscallarg(int) incr;
   1779 	} */ *uap = v;
   1780 	struct sys_sstk_args ua;
   1781 
   1782 	NETBSD32TO64_UAP(incr);
   1783 	return (sys_sstk(p, &ua, retval));
   1784 }
   1785 
   1786 int
   1787 compat_netbsd32_munmap(p, v, retval)
   1788 	struct proc *p;
   1789 	void *v;
   1790 	register_t *retval;
   1791 {
   1792 	struct compat_netbsd32_munmap_args /* {
   1793 		syscallarg(netbsd32_voidp) addr;
   1794 		syscallarg(netbsd32_size_t) len;
   1795 	} */ *uap = v;
   1796 	struct sys_munmap_args ua;
   1797 
   1798 	NETBSD32TOP_UAP(addr, void);
   1799 	NETBSD32TOX_UAP(len, size_t);
   1800 	return (sys_munmap(p, &ua, retval));
   1801 }
   1802 
   1803 int
   1804 compat_netbsd32_mprotect(p, v, retval)
   1805 	struct proc *p;
   1806 	void *v;
   1807 	register_t *retval;
   1808 {
   1809 	struct compat_netbsd32_mprotect_args /* {
   1810 		syscallarg(netbsd32_voidp) addr;
   1811 		syscallarg(netbsd32_size_t) len;
   1812 		syscallarg(int) prot;
   1813 	} */ *uap = v;
   1814 	struct sys_mprotect_args ua;
   1815 
   1816 	NETBSD32TOP_UAP(addr, void);
   1817 	NETBSD32TOX_UAP(len, size_t);
   1818 	NETBSD32TO64_UAP(prot);
   1819 	return (sys_mprotect(p, &ua, retval));
   1820 }
   1821 
   1822 int
   1823 compat_netbsd32_madvise(p, v, retval)
   1824 	struct proc *p;
   1825 	void *v;
   1826 	register_t *retval;
   1827 {
   1828 	struct compat_netbsd32_madvise_args /* {
   1829 		syscallarg(netbsd32_voidp) addr;
   1830 		syscallarg(netbsd32_size_t) len;
   1831 		syscallarg(int) behav;
   1832 	} */ *uap = v;
   1833 	struct sys_madvise_args ua;
   1834 
   1835 	NETBSD32TOP_UAP(addr, void);
   1836 	NETBSD32TOX_UAP(len, size_t);
   1837 	NETBSD32TO64_UAP(behav);
   1838 	return (sys_madvise(p, &ua, retval));
   1839 }
   1840 
   1841 int
   1842 compat_netbsd32_mincore(p, v, retval)
   1843 	struct proc *p;
   1844 	void *v;
   1845 	register_t *retval;
   1846 {
   1847 	struct compat_netbsd32_mincore_args /* {
   1848 		syscallarg(netbsd32_caddr_t) addr;
   1849 		syscallarg(netbsd32_size_t) len;
   1850 		syscallarg(netbsd32_charp) vec;
   1851 	} */ *uap = v;
   1852 	struct sys_mincore_args ua;
   1853 
   1854 	NETBSD32TOX64_UAP(addr, caddr_t);
   1855 	NETBSD32TOX_UAP(len, size_t);
   1856 	NETBSD32TOP_UAP(vec, char);
   1857 	return (sys_mincore(p, &ua, retval));
   1858 }
   1859 
   1860 int
   1861 compat_netbsd32_getgroups(p, v, retval)
   1862 	struct proc *p;
   1863 	void *v;
   1864 	register_t *retval;
   1865 {
   1866 	struct compat_netbsd32_getgroups_args /* {
   1867 		syscallarg(int) gidsetsize;
   1868 		syscallarg(netbsd32_gid_tp) gidset;
   1869 	} */ *uap = v;
   1870 	register struct pcred *pc = p->p_cred;
   1871 	register int ngrp;
   1872 	int error;
   1873 
   1874 	ngrp = SCARG(uap, gidsetsize);
   1875 	if (ngrp == 0) {
   1876 		*retval = pc->pc_ucred->cr_ngroups;
   1877 		return (0);
   1878 	}
   1879 	if (ngrp < pc->pc_ucred->cr_ngroups)
   1880 		return (EINVAL);
   1881 	ngrp = pc->pc_ucred->cr_ngroups;
   1882 	/* Should convert gid_t to netbsd32_gid_t, but they're the same */
   1883 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
   1884 			(caddr_t)(u_long)SCARG(uap, gidset),
   1885 			ngrp * sizeof(gid_t));
   1886 	if (error)
   1887 		return (error);
   1888 	*retval = ngrp;
   1889 	return (0);
   1890 }
   1891 
   1892 int
   1893 compat_netbsd32_setgroups(p, v, retval)
   1894 	struct proc *p;
   1895 	void *v;
   1896 	register_t *retval;
   1897 {
   1898 	struct compat_netbsd32_setgroups_args /* {
   1899 		syscallarg(int) gidsetsize;
   1900 		syscallarg(const netbsd32_gid_tp) gidset;
   1901 	} */ *uap = v;
   1902 	struct sys_setgroups_args ua;
   1903 
   1904 	NETBSD32TO64_UAP(gidsetsize);
   1905 	NETBSD32TOP_UAP(gidset, gid_t);
   1906 	return (sys_setgroups(p, &ua, retval));
   1907 }
   1908 
   1909 int
   1910 compat_netbsd32_setpgid(p, v, retval)
   1911 	struct proc *p;
   1912 	void *v;
   1913 	register_t *retval;
   1914 {
   1915 	struct compat_netbsd32_setpgid_args /* {
   1916 		syscallarg(int) pid;
   1917 		syscallarg(int) pgid;
   1918 	} */ *uap = v;
   1919 	struct sys_setpgid_args ua;
   1920 
   1921 	NETBSD32TO64_UAP(pid);
   1922 	NETBSD32TO64_UAP(pgid);
   1923 	return (sys_setpgid(p, &ua, retval));
   1924 }
   1925 
   1926 int
   1927 compat_netbsd32_setitimer(p, v, retval)
   1928 	struct proc *p;
   1929 	void *v;
   1930 	register_t *retval;
   1931 {
   1932 	struct compat_netbsd32_setitimer_args /* {
   1933 		syscallarg(int) which;
   1934 		syscallarg(const netbsd32_itimervalp_t) itv;
   1935 		syscallarg(netbsd32_itimervalp_t) oitv;
   1936 	} */ *uap = v;
   1937 	struct netbsd32_itimerval s32it, *itvp;
   1938 	int which = SCARG(uap, which);
   1939 	struct compat_netbsd32_getitimer_args getargs;
   1940 	struct itimerval aitv;
   1941 	int s, error;
   1942 
   1943 	if ((u_int)which > ITIMER_PROF)
   1944 		return (EINVAL);
   1945 	itvp = (struct netbsd32_itimerval *)(u_long)SCARG(uap, itv);
   1946 	if (itvp && (error = copyin(itvp, &s32it, sizeof(s32it))))
   1947 		return (error);
   1948 	netbsd32_to_itimerval(&s32it, &aitv);
   1949 	if (SCARG(uap, oitv) != NULL) {
   1950 		SCARG(&getargs, which) = which;
   1951 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   1952 		if ((error = compat_netbsd32_getitimer(p, &getargs, retval)) != 0)
   1953 			return (error);
   1954 	}
   1955 	if (itvp == 0)
   1956 		return (0);
   1957 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
   1958 		return (EINVAL);
   1959 	s = splclock();
   1960 	if (which == ITIMER_REAL) {
   1961 		untimeout(realitexpire, p);
   1962 		if (timerisset(&aitv.it_value)) {
   1963 			timeradd(&aitv.it_value, &time, &aitv.it_value);
   1964 			timeout(realitexpire, p, hzto(&aitv.it_value));
   1965 		}
   1966 		p->p_realtimer = aitv;
   1967 	} else
   1968 		p->p_stats->p_timer[which] = aitv;
   1969 	splx(s);
   1970 	return (0);
   1971 }
   1972 
   1973 int
   1974 compat_netbsd32_getitimer(p, v, retval)
   1975 	struct proc *p;
   1976 	void *v;
   1977 	register_t *retval;
   1978 {
   1979 	struct compat_netbsd32_getitimer_args /* {
   1980 		syscallarg(int) which;
   1981 		syscallarg(netbsd32_itimervalp_t) itv;
   1982 	} */ *uap = v;
   1983 	int which = SCARG(uap, which);
   1984 	struct netbsd32_itimerval s32it;
   1985 	struct itimerval aitv;
   1986 	int s;
   1987 
   1988 	if ((u_int)which > ITIMER_PROF)
   1989 		return (EINVAL);
   1990 	s = splclock();
   1991 	if (which == ITIMER_REAL) {
   1992 		/*
   1993 		 * Convert from absolute to relative time in .it_value
   1994 		 * part of real time timer.  If time for real time timer
   1995 		 * has passed return 0, else return difference between
   1996 		 * current time and time for the timer to go off.
   1997 		 */
   1998 		aitv = p->p_realtimer;
   1999 		if (timerisset(&aitv.it_value)) {
   2000 			if (timercmp(&aitv.it_value, &time, <))
   2001 				timerclear(&aitv.it_value);
   2002 			else
   2003 				timersub(&aitv.it_value, &time, &aitv.it_value);
   2004 		}
   2005 	} else
   2006 		aitv = p->p_stats->p_timer[which];
   2007 	splx(s);
   2008 	netbsd32_from_itimerval(&aitv, &s32it);
   2009 	return (copyout(&s32it, (caddr_t)(u_long)SCARG(uap, itv), sizeof(s32it)));
   2010 }
   2011 
   2012 int
   2013 compat_netbsd32_fcntl(p, v, retval)
   2014 	struct proc *p;
   2015 	void *v;
   2016 	register_t *retval;
   2017 {
   2018 	struct compat_netbsd32_fcntl_args /* {
   2019 		syscallarg(int) fd;
   2020 		syscallarg(int) cmd;
   2021 		syscallarg(netbsd32_voidp) arg;
   2022 	} */ *uap = v;
   2023 	struct sys_fcntl_args ua;
   2024 
   2025 	NETBSD32TO64_UAP(fd);
   2026 	NETBSD32TO64_UAP(cmd);
   2027 	NETBSD32TOP_UAP(arg, void);
   2028 	/* XXXX we can do this 'cause flock doesn't change */
   2029 	return (sys_fcntl(p, &ua, retval));
   2030 }
   2031 
   2032 int
   2033 compat_netbsd32_dup2(p, v, retval)
   2034 	struct proc *p;
   2035 	void *v;
   2036 	register_t *retval;
   2037 {
   2038 	struct compat_netbsd32_dup2_args /* {
   2039 		syscallarg(int) from;
   2040 		syscallarg(int) to;
   2041 	} */ *uap = v;
   2042 	struct sys_dup2_args ua;
   2043 
   2044 	NETBSD32TO64_UAP(from);
   2045 	NETBSD32TO64_UAP(to);
   2046 	return (sys_dup2(p, &ua, retval));
   2047 }
   2048 
   2049 int
   2050 compat_netbsd32_select(p, v, retval)
   2051 	struct proc *p;
   2052 	void *v;
   2053 	register_t *retval;
   2054 {
   2055 	struct compat_netbsd32_select_args /* {
   2056 		syscallarg(int) nd;
   2057 		syscallarg(netbsd32_fd_setp_t) in;
   2058 		syscallarg(netbsd32_fd_setp_t) ou;
   2059 		syscallarg(netbsd32_fd_setp_t) ex;
   2060 		syscallarg(netbsd32_timevalp_t) tv;
   2061 	} */ *uap = v;
   2062 /* This one must be done in-line 'cause of the timeval */
   2063 	struct netbsd32_timeval tv32;
   2064 	caddr_t bits;
   2065 	char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
   2066 	struct timeval atv;
   2067 	int s, ncoll, error = 0, timo;
   2068 	size_t ni;
   2069 	extern int	selwait, nselcoll;
   2070 	extern int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
   2071 
   2072 	if (SCARG(uap, nd) < 0)
   2073 		return (EINVAL);
   2074 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
   2075 		/* forgiving; slightly wrong */
   2076 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
   2077 	}
   2078 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
   2079 	if (ni * 6 > sizeof(smallbits))
   2080 		bits = malloc(ni * 6, M_TEMP, M_WAITOK);
   2081 	else
   2082 		bits = smallbits;
   2083 
   2084 #define	getbits(name, x) \
   2085 	if (SCARG(uap, name)) { \
   2086 		error = copyin((caddr_t)(u_long)SCARG(uap, name), bits + ni * x, ni); \
   2087 		if (error) \
   2088 			goto done; \
   2089 	} else \
   2090 		memset(bits + ni * x, 0, ni);
   2091 	getbits(in, 0);
   2092 	getbits(ou, 1);
   2093 	getbits(ex, 2);
   2094 #undef	getbits
   2095 
   2096 	if (SCARG(uap, tv)) {
   2097 		error = copyin((caddr_t)(u_long)SCARG(uap, tv), (caddr_t)&tv32,
   2098 			sizeof(tv32));
   2099 		if (error)
   2100 			goto done;
   2101 		netbsd32_to_timeval(&tv32, &atv);
   2102 		if (itimerfix(&atv)) {
   2103 			error = EINVAL;
   2104 			goto done;
   2105 		}
   2106 		s = splclock();
   2107 		timeradd(&atv, &time, &atv);
   2108 		timo = hzto(&atv);
   2109 		/*
   2110 		 * Avoid inadvertently sleeping forever.
   2111 		 */
   2112 		if (timo == 0)
   2113 			timo = 1;
   2114 		splx(s);
   2115 	} else
   2116 		timo = 0;
   2117 retry:
   2118 	ncoll = nselcoll;
   2119 	p->p_flag |= P_SELECT;
   2120 	error = selscan(p, (fd_mask *)(bits + ni * 0),
   2121 			   (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
   2122 	if (error || *retval)
   2123 		goto done;
   2124 	s = splhigh();
   2125 	if (timo && timercmp(&time, &atv, >=)) {
   2126 		splx(s);
   2127 		goto done;
   2128 	}
   2129 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
   2130 		splx(s);
   2131 		goto retry;
   2132 	}
   2133 	p->p_flag &= ~P_SELECT;
   2134 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
   2135 	splx(s);
   2136 	if (error == 0)
   2137 		goto retry;
   2138 done:
   2139 	p->p_flag &= ~P_SELECT;
   2140 	/* select is not restarted after signals... */
   2141 	if (error == ERESTART)
   2142 		error = EINTR;
   2143 	if (error == EWOULDBLOCK)
   2144 		error = 0;
   2145 	if (error == 0) {
   2146 #define	putbits(name, x) \
   2147 		if (SCARG(uap, name)) { \
   2148 			error = copyout(bits + ni * x, (caddr_t)(u_long)SCARG(uap, name), ni); \
   2149 			if (error) \
   2150 				goto out; \
   2151 		}
   2152 		putbits(in, 3);
   2153 		putbits(ou, 4);
   2154 		putbits(ex, 5);
   2155 #undef putbits
   2156 	}
   2157 out:
   2158 	if (ni * 6 > sizeof(smallbits))
   2159 		free(bits, M_TEMP);
   2160 	return (error);
   2161 }
   2162 
   2163 int
   2164 compat_netbsd32_fsync(p, v, retval)
   2165 	struct proc *p;
   2166 	void *v;
   2167 	register_t *retval;
   2168 {
   2169 	struct compat_netbsd32_fsync_args /* {
   2170 		syscallarg(int) fd;
   2171 	} */ *uap = v;
   2172 	struct sys_fsync_args ua;
   2173 
   2174 	NETBSD32TO64_UAP(fd);
   2175 	return (sys_fsync(p, &ua, retval));
   2176 }
   2177 
   2178 int
   2179 compat_netbsd32_setpriority(p, v, retval)
   2180 	struct proc *p;
   2181 	void *v;
   2182 	register_t *retval;
   2183 {
   2184 	struct compat_netbsd32_setpriority_args /* {
   2185 		syscallarg(int) which;
   2186 		syscallarg(int) who;
   2187 		syscallarg(int) prio;
   2188 	} */ *uap = v;
   2189 	struct sys_setpriority_args ua;
   2190 
   2191 	NETBSD32TO64_UAP(which);
   2192 	NETBSD32TO64_UAP(who);
   2193 	NETBSD32TO64_UAP(prio);
   2194 	return (sys_setpriority(p, &ua, retval));
   2195 }
   2196 
   2197 int
   2198 compat_netbsd32_socket(p, v, retval)
   2199 	struct proc *p;
   2200 	void *v;
   2201 	register_t *retval;
   2202 {
   2203 	struct compat_netbsd32_socket_args /* {
   2204 		syscallarg(int) domain;
   2205 		syscallarg(int) type;
   2206 		syscallarg(int) protocol;
   2207 	} */ *uap = v;
   2208 	struct sys_socket_args ua;
   2209 
   2210 	NETBSD32TO64_UAP(domain);
   2211 	NETBSD32TO64_UAP(type);
   2212 	NETBSD32TO64_UAP(protocol);
   2213 	return (sys_socket(p, &ua, retval));
   2214 }
   2215 
   2216 int
   2217 compat_netbsd32_connect(p, v, retval)
   2218 	struct proc *p;
   2219 	void *v;
   2220 	register_t *retval;
   2221 {
   2222 	struct compat_netbsd32_connect_args /* {
   2223 		syscallarg(int) s;
   2224 		syscallarg(const netbsd32_sockaddrp_t) name;
   2225 		syscallarg(int) namelen;
   2226 	} */ *uap = v;
   2227 	struct sys_connect_args ua;
   2228 
   2229 	NETBSD32TO64_UAP(s);
   2230 	NETBSD32TOP_UAP(name, struct sockaddr);
   2231 	NETBSD32TO64_UAP(namelen);
   2232 	return (sys_connect(p, &ua, retval));
   2233 }
   2234 
   2235 int
   2236 compat_netbsd32_getpriority(p, v, retval)
   2237 	struct proc *p;
   2238 	void *v;
   2239 	register_t *retval;
   2240 {
   2241 	struct compat_netbsd32_getpriority_args /* {
   2242 		syscallarg(int) which;
   2243 		syscallarg(int) who;
   2244 	} */ *uap = v;
   2245 	struct sys_getpriority_args ua;
   2246 
   2247 	NETBSD32TO64_UAP(which);
   2248 	NETBSD32TO64_UAP(who);
   2249 	return (sys_getpriority(p, &ua, retval));
   2250 }
   2251 
   2252 int
   2253 compat_netbsd32_bind(p, v, retval)
   2254 	struct proc *p;
   2255 	void *v;
   2256 	register_t *retval;
   2257 {
   2258 	struct compat_netbsd32_bind_args /* {
   2259 		syscallarg(int) s;
   2260 		syscallarg(const netbsd32_sockaddrp_t) name;
   2261 		syscallarg(int) namelen;
   2262 	} */ *uap = v;
   2263 	struct sys_bind_args ua;
   2264 
   2265 	NETBSD32TO64_UAP(s);
   2266 	NETBSD32TOP_UAP(name, struct sockaddr);
   2267 	NETBSD32TO64_UAP(namelen);
   2268 	return (sys_bind(p, &ua, retval));
   2269 }
   2270 
   2271 int
   2272 compat_netbsd32_setsockopt(p, v, retval)
   2273 	struct proc *p;
   2274 	void *v;
   2275 	register_t *retval;
   2276 {
   2277 	struct compat_netbsd32_setsockopt_args /* {
   2278 		syscallarg(int) s;
   2279 		syscallarg(int) level;
   2280 		syscallarg(int) name;
   2281 		syscallarg(const netbsd32_voidp) val;
   2282 		syscallarg(int) valsize;
   2283 	} */ *uap = v;
   2284 	struct sys_setsockopt_args ua;
   2285 
   2286 	NETBSD32TO64_UAP(s);
   2287 	NETBSD32TO64_UAP(level);
   2288 	NETBSD32TO64_UAP(name);
   2289 	NETBSD32TOP_UAP(val, void);
   2290 	NETBSD32TO64_UAP(valsize);
   2291 	/* may be more efficient to do this inline. */
   2292 	return (sys_setsockopt(p, &ua, retval));
   2293 }
   2294 
   2295 int
   2296 compat_netbsd32_listen(p, v, retval)
   2297 	struct proc *p;
   2298 	void *v;
   2299 	register_t *retval;
   2300 {
   2301 	struct compat_netbsd32_listen_args /* {
   2302 		syscallarg(int) s;
   2303 		syscallarg(int) backlog;
   2304 	} */ *uap = v;
   2305 	struct sys_listen_args ua;
   2306 
   2307 	NETBSD32TO64_UAP(s);
   2308 	NETBSD32TO64_UAP(backlog);
   2309 	return (sys_listen(p, &ua, retval));
   2310 }
   2311 
   2312 int
   2313 compat_netbsd32_vtrace(p, v, retval)
   2314 	struct proc *p;
   2315 	void *v;
   2316 	register_t *retval;
   2317 {
   2318 #ifdef TRACE
   2319 	struct compat_netbsd32_vtrace_args /* {
   2320 		syscallarg(int) request;
   2321 		syscallarg(int) value;
   2322 	} */ *uap = v;
   2323 	struct sys_vtrace_args ua;
   2324 
   2325 	NETBSD32TO64_UAP(request);
   2326 	NETBSD32TO64_UAP(value);
   2327 	return (vtrace(p, &ua, retval));
   2328 #else
   2329 	return (ENOSYS);
   2330 #endif
   2331 }
   2332 
   2333 int
   2334 compat_netbsd32_gettimeofday(p, v, retval)
   2335 	struct proc *p;
   2336 	void *v;
   2337 	register_t *retval;
   2338 {
   2339 	struct compat_netbsd32_gettimeofday_args /* {
   2340 		syscallarg(netbsd32_timevalp_t) tp;
   2341 		syscallarg(netbsd32_timezonep_t) tzp;
   2342 	} */ *uap = v;
   2343 	struct timeval atv;
   2344 	struct netbsd32_timeval tv32;
   2345 	int error = 0;
   2346 	struct netbsd32_timezone tzfake;
   2347 
   2348 	if (SCARG(uap, tp)) {
   2349 		microtime(&atv);
   2350 		netbsd32_from_timeval(&atv, &tv32);
   2351 		error = copyout(&tv32, (caddr_t)(u_long)SCARG(uap, tp), sizeof(tv32));
   2352 		if (error)
   2353 			return (error);
   2354 	}
   2355 	if (SCARG(uap, tzp)) {
   2356 		/*
   2357 		 * NetBSD has no kernel notion of time zone, so we just
   2358 		 * fake up a timezone struct and return it if demanded.
   2359 		 */
   2360 		tzfake.tz_minuteswest = 0;
   2361 		tzfake.tz_dsttime = 0;
   2362 		error = copyout(&tzfake, (caddr_t)(u_long)SCARG(uap, tzp), sizeof(tzfake));
   2363 	}
   2364 	return (error);
   2365 }
   2366 
   2367 static int settime __P((struct timeval *));
   2368 /* This function is used by clock_settime and settimeofday */
   2369 static int
   2370 settime(tv)
   2371 	struct timeval *tv;
   2372 {
   2373 	struct timeval delta;
   2374 	int s;
   2375 
   2376 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
   2377 	s = splclock();
   2378 	timersub(tv, &time, &delta);
   2379 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1)
   2380 		return (EPERM);
   2381 #ifdef notyet
   2382 	if ((delta.tv_sec < 86400) && securelevel > 0)
   2383 		return (EPERM);
   2384 #endif
   2385 	time = *tv;
   2386 	(void) splsoftclock();
   2387 	timeradd(&boottime, &delta, &boottime);
   2388 	timeradd(&runtime, &delta, &runtime);
   2389 #	if defined(NFS) || defined(NFSSERVER)
   2390 		nqnfs_lease_updatetime(delta.tv_sec);
   2391 #	endif
   2392 	splx(s);
   2393 	resettodr();
   2394 	return (0);
   2395 }
   2396 
   2397 
   2398 int
   2399 compat_netbsd32_settimeofday(p, v, retval)
   2400 	struct proc *p;
   2401 	void *v;
   2402 	register_t *retval;
   2403 {
   2404 	struct compat_netbsd32_settimeofday_args /* {
   2405 		syscallarg(const netbsd32_timevalp_t) tv;
   2406 		syscallarg(const netbsd32_timezonep_t) tzp;
   2407 	} */ *uap = v;
   2408 	struct netbsd32_timeval atv32;
   2409 	struct timeval atv;
   2410 	struct netbsd32_timezone atz;
   2411 	int error;
   2412 
   2413 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   2414 		return (error);
   2415 	/* Verify all parameters before changing time. */
   2416 	if (SCARG(uap, tv) && (error = copyin((caddr_t)(u_long)SCARG(uap, tv),
   2417 	    &atv32, sizeof(atv32))))
   2418 		return (error);
   2419 	netbsd32_to_timeval(&atv32, &atv);
   2420 	/* XXX since we don't use tz, probably no point in doing copyin. */
   2421 	if (SCARG(uap, tzp) && (error = copyin((caddr_t)(u_long)SCARG(uap, tzp),
   2422 	    &atz, sizeof(atz))))
   2423 		return (error);
   2424 	if (SCARG(uap, tv))
   2425 		if ((error = settime(&atv)))
   2426 			return (error);
   2427 	/*
   2428 	 * NetBSD has no kernel notion of time zone, and only an
   2429 	 * obsolete program would try to set it, so we log a warning.
   2430 	 */
   2431 	if (SCARG(uap, tzp))
   2432 		printf("pid %d attempted to set the "
   2433 		    "(obsolete) kernel time zone\n", p->p_pid);
   2434 	return (0);
   2435 }
   2436 
   2437 int
   2438 compat_netbsd32_fchown(p, v, retval)
   2439 	struct proc *p;
   2440 	void *v;
   2441 	register_t *retval;
   2442 {
   2443 	struct compat_netbsd32_fchown_args /* {
   2444 		syscallarg(int) fd;
   2445 		syscallarg(uid_t) uid;
   2446 		syscallarg(gid_t) gid;
   2447 	} */ *uap = v;
   2448 	struct sys_fchown_args ua;
   2449 
   2450 	NETBSD32TO64_UAP(fd);
   2451 	NETBSD32TO64_UAP(uid);
   2452 	NETBSD32TO64_UAP(gid);
   2453 	return (sys_fchown(p, &ua, retval));
   2454 }
   2455 
   2456 int
   2457 compat_netbsd32_fchmod(p, v, retval)
   2458 	struct proc *p;
   2459 	void *v;
   2460 	register_t *retval;
   2461 {
   2462 	struct compat_netbsd32_fchmod_args /* {
   2463 		syscallarg(int) fd;
   2464 		syscallarg(mode_t) mode;
   2465 	} */ *uap = v;
   2466 	struct sys_fchmod_args ua;
   2467 
   2468 	NETBSD32TO64_UAP(fd);
   2469 	NETBSD32TO64_UAP(mode);
   2470 	return (sys_fchmod(p, &ua, retval));
   2471 }
   2472 
   2473 int
   2474 compat_netbsd32_setreuid(p, v, retval)
   2475 	struct proc *p;
   2476 	void *v;
   2477 	register_t *retval;
   2478 {
   2479 	struct compat_netbsd32_setreuid_args /* {
   2480 		syscallarg(uid_t) ruid;
   2481 		syscallarg(uid_t) euid;
   2482 	} */ *uap = v;
   2483 	struct sys_setreuid_args ua;
   2484 
   2485 	NETBSD32TO64_UAP(ruid);
   2486 	NETBSD32TO64_UAP(euid);
   2487 	return (sys_setreuid(p, &ua, retval));
   2488 }
   2489 
   2490 int
   2491 compat_netbsd32_setregid(p, v, retval)
   2492 	struct proc *p;
   2493 	void *v;
   2494 	register_t *retval;
   2495 {
   2496 	struct compat_netbsd32_setregid_args /* {
   2497 		syscallarg(gid_t) rgid;
   2498 		syscallarg(gid_t) egid;
   2499 	} */ *uap = v;
   2500 	struct sys_setregid_args ua;
   2501 
   2502 	NETBSD32TO64_UAP(rgid);
   2503 	NETBSD32TO64_UAP(egid);
   2504 	return (sys_setregid(p, &ua, retval));
   2505 }
   2506 
   2507 int
   2508 compat_netbsd32_getrusage(p, v, retval)
   2509 	struct proc *p;
   2510 	void *v;
   2511 	register_t *retval;
   2512 {
   2513 	struct compat_netbsd32_getrusage_args /* {
   2514 		syscallarg(int) who;
   2515 		syscallarg(netbsd32_rusagep_t) rusage;
   2516 	} */ *uap = v;
   2517 	struct rusage *rup;
   2518 	struct netbsd32_rusage ru;
   2519 
   2520 	switch (SCARG(uap, who)) {
   2521 
   2522 	case RUSAGE_SELF:
   2523 		rup = &p->p_stats->p_ru;
   2524 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
   2525 		break;
   2526 
   2527 	case RUSAGE_CHILDREN:
   2528 		rup = &p->p_stats->p_cru;
   2529 		break;
   2530 
   2531 	default:
   2532 		return (EINVAL);
   2533 	}
   2534 	netbsd32_from_rusage(rup, &ru);
   2535 	return (copyout(&ru, (caddr_t)(u_long)SCARG(uap, rusage), sizeof(ru)));
   2536 }
   2537 
   2538 int
   2539 compat_netbsd32_getsockopt(p, v, retval)
   2540 	struct proc *p;
   2541 	void *v;
   2542 	register_t *retval;
   2543 {
   2544 	struct compat_netbsd32_getsockopt_args /* {
   2545 		syscallarg(int) s;
   2546 		syscallarg(int) level;
   2547 		syscallarg(int) name;
   2548 		syscallarg(netbsd32_voidp) val;
   2549 		syscallarg(netbsd32_intp) avalsize;
   2550 	} */ *uap = v;
   2551 	struct sys_getsockopt_args ua;
   2552 
   2553 	NETBSD32TO64_UAP(s);
   2554 	NETBSD32TO64_UAP(level);
   2555 	NETBSD32TO64_UAP(name);
   2556 	NETBSD32TOP_UAP(val, void);
   2557 	NETBSD32TOP_UAP(avalsize, int);
   2558 	return (sys_getsockopt(p, &ua, retval));
   2559 }
   2560 
   2561 int
   2562 compat_netbsd32_readv(p, v, retval)
   2563 	struct proc *p;
   2564 	void *v;
   2565 	register_t *retval;
   2566 {
   2567 	struct compat_netbsd32_readv_args /* {
   2568 		syscallarg(int) fd;
   2569 		syscallarg(const netbsd32_iovecp_t) iovp;
   2570 		syscallarg(int) iovcnt;
   2571 	} */ *uap = v;
   2572 	int fd = SCARG(uap, fd);
   2573 	register struct file *fp;
   2574 	register struct filedesc *fdp = p->p_fd;
   2575 
   2576 	if ((u_int)fd >= fdp->fd_nfiles ||
   2577 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   2578 	    (fp->f_flag & FREAD) == 0)
   2579 		return (EBADF);
   2580 
   2581 	return (dofilereadv32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp),
   2582 			      SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
   2583 }
   2584 
   2585 /* Damn thing copies in the iovec! */
   2586 int
   2587 dofilereadv32(p, fd, fp, iovp, iovcnt, offset, flags, retval)
   2588 	struct proc *p;
   2589 	int fd;
   2590 	struct file *fp;
   2591 	struct netbsd32_iovec *iovp;
   2592 	int iovcnt;
   2593 	off_t *offset;
   2594 	int flags;
   2595 	register_t *retval;
   2596 {
   2597 	struct uio auio;
   2598 	register struct iovec *iov;
   2599 	struct iovec *needfree;
   2600 	struct iovec aiov[UIO_SMALLIOV];
   2601 	long i, cnt, error = 0;
   2602 	u_int iovlen;
   2603 #ifdef KTRACE
   2604 	struct iovec *ktriov = NULL;
   2605 #endif
   2606 
   2607 	/* note: can't use iovlen until iovcnt is validated */
   2608 	iovlen = iovcnt * sizeof(struct iovec);
   2609 	if ((u_int)iovcnt > UIO_SMALLIOV) {
   2610 		if ((u_int)iovcnt > IOV_MAX)
   2611 			return (EINVAL);
   2612 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
   2613 		needfree = iov;
   2614 	} else if ((u_int)iovcnt > 0) {
   2615 		iov = aiov;
   2616 		needfree = NULL;
   2617 	} else
   2618 		return (EINVAL);
   2619 
   2620 	auio.uio_iov = iov;
   2621 	auio.uio_iovcnt = iovcnt;
   2622 	auio.uio_rw = UIO_READ;
   2623 	auio.uio_segflg = UIO_USERSPACE;
   2624 	auio.uio_procp = p;
   2625 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
   2626 	if (error)
   2627 		goto done;
   2628 	auio.uio_resid = 0;
   2629 	for (i = 0; i < iovcnt; i++) {
   2630 		auio.uio_resid += iov->iov_len;
   2631 		/*
   2632 		 * Reads return ssize_t because -1 is returned on error.
   2633 		 * Therefore we must restrict the length to SSIZE_MAX to
   2634 		 * avoid garbage return values.
   2635 		 */
   2636 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   2637 			error = EINVAL;
   2638 			goto done;
   2639 		}
   2640 		iov++;
   2641 	}
   2642 #ifdef KTRACE
   2643 	/*
   2644 	 * if tracing, save a copy of iovec
   2645 	 */
   2646 	if (KTRPOINT(p, KTR_GENIO))  {
   2647 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   2648 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   2649 	}
   2650 #endif
   2651 	cnt = auio.uio_resid;
   2652 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
   2653 	if (error)
   2654 		if (auio.uio_resid != cnt && (error == ERESTART ||
   2655 		    error == EINTR || error == EWOULDBLOCK))
   2656 			error = 0;
   2657 	cnt -= auio.uio_resid;
   2658 #ifdef KTRACE
   2659 	if (KTRPOINT(p, KTR_GENIO))
   2660 		if (error == 0) {
   2661 			ktrgenio(p->p_tracep, fd, UIO_READ, ktriov, cnt,
   2662 			    error);
   2663 		FREE(ktriov, M_TEMP);
   2664 	}
   2665 #endif
   2666 	*retval = cnt;
   2667 done:
   2668 	if (needfree)
   2669 		FREE(needfree, M_IOV);
   2670 	return (error);
   2671 }
   2672 
   2673 
   2674 int
   2675 compat_netbsd32_writev(p, v, retval)
   2676 	struct proc *p;
   2677 	void *v;
   2678 	register_t *retval;
   2679 {
   2680 	struct compat_netbsd32_writev_args /* {
   2681 		syscallarg(int) fd;
   2682 		syscallarg(const netbsd32_iovecp_t) iovp;
   2683 		syscallarg(int) iovcnt;
   2684 	} */ *uap = v;
   2685 	int fd = SCARG(uap, fd);
   2686 	register struct file *fp;
   2687 	register struct filedesc *fdp = p->p_fd;
   2688 
   2689 	if ((u_int)fd >= fdp->fd_nfiles ||
   2690 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   2691 	    (fp->f_flag & FWRITE) == 0)
   2692 		return (EBADF);
   2693 
   2694 	return (dofilewritev32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp),
   2695 			       SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
   2696 }
   2697 
   2698 int
   2699 dofilewritev32(p, fd, fp, iovp, iovcnt, offset, flags, retval)
   2700 	struct proc *p;
   2701 	int fd;
   2702 	struct file *fp;
   2703 	struct netbsd32_iovec *iovp;
   2704 	int iovcnt;
   2705 	off_t *offset;
   2706 	int flags;
   2707 	register_t *retval;
   2708 {
   2709 	struct uio auio;
   2710 	register struct iovec *iov;
   2711 	struct iovec *needfree;
   2712 	struct iovec aiov[UIO_SMALLIOV];
   2713 	long i, cnt, error = 0;
   2714 	u_int iovlen;
   2715 #ifdef KTRACE
   2716 	struct iovec *ktriov = NULL;
   2717 #endif
   2718 
   2719 	/* note: can't use iovlen until iovcnt is validated */
   2720 	iovlen = iovcnt * sizeof(struct iovec);
   2721 	if ((u_int)iovcnt > UIO_SMALLIOV) {
   2722 		if ((u_int)iovcnt > IOV_MAX)
   2723 			return (EINVAL);
   2724 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
   2725 		needfree = iov;
   2726 	} else if ((u_int)iovcnt > 0) {
   2727 		iov = aiov;
   2728 		needfree = NULL;
   2729 	} else
   2730 		return (EINVAL);
   2731 
   2732 	auio.uio_iov = iov;
   2733 	auio.uio_iovcnt = iovcnt;
   2734 	auio.uio_rw = UIO_WRITE;
   2735 	auio.uio_segflg = UIO_USERSPACE;
   2736 	auio.uio_procp = p;
   2737 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
   2738 	if (error)
   2739 		goto done;
   2740 	auio.uio_resid = 0;
   2741 	for (i = 0; i < iovcnt; i++) {
   2742 		auio.uio_resid += iov->iov_len;
   2743 		/*
   2744 		 * Writes return ssize_t because -1 is returned on error.
   2745 		 * Therefore we must restrict the length to SSIZE_MAX to
   2746 		 * avoid garbage return values.
   2747 		 */
   2748 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   2749 			error = EINVAL;
   2750 			goto done;
   2751 		}
   2752 		iov++;
   2753 	}
   2754 #ifdef KTRACE
   2755 	/*
   2756 	 * if tracing, save a copy of iovec
   2757 	 */
   2758 	if (KTRPOINT(p, KTR_GENIO))  {
   2759 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   2760 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   2761 	}
   2762 #endif
   2763 	cnt = auio.uio_resid;
   2764 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
   2765 	if (error) {
   2766 		if (auio.uio_resid != cnt && (error == ERESTART ||
   2767 		    error == EINTR || error == EWOULDBLOCK))
   2768 			error = 0;
   2769 		if (error == EPIPE)
   2770 			psignal(p, SIGPIPE);
   2771 	}
   2772 	cnt -= auio.uio_resid;
   2773 #ifdef KTRACE
   2774 	if (KTRPOINT(p, KTR_GENIO))
   2775 		if (error == 0) {
   2776 			ktrgenio(p->p_tracep, fd, UIO_WRITE, ktriov, cnt,
   2777 			    error);
   2778 		FREE(ktriov, M_TEMP);
   2779 	}
   2780 #endif
   2781 	*retval = cnt;
   2782 done:
   2783 	if (needfree)
   2784 		FREE(needfree, M_IOV);
   2785 	return (error);
   2786 }
   2787 
   2788 
   2789 int
   2790 compat_netbsd32_rename(p, v, retval)
   2791 	struct proc *p;
   2792 	void *v;
   2793 	register_t *retval;
   2794 {
   2795 	struct compat_netbsd32_rename_args /* {
   2796 		syscallarg(const netbsd32_charp) from;
   2797 		syscallarg(const netbsd32_charp) to;
   2798 	} */ *uap = v;
   2799 	struct sys_rename_args ua;
   2800 
   2801 	NETBSD32TOP_UAP(from, const char *);
   2802 	NETBSD32TOP_UAP(to, const char *)
   2803 
   2804 	return (sys_rename(p, &ua, retval));
   2805 }
   2806 
   2807 int
   2808 compat_netbsd32_flock(p, v, retval)
   2809 	struct proc *p;
   2810 	void *v;
   2811 	register_t *retval;
   2812 {
   2813 	struct compat_netbsd32_flock_args /* {
   2814 		syscallarg(int) fd;
   2815 		syscallarg(int) how;
   2816 	} */ *uap = v;
   2817 	struct sys_flock_args ua;
   2818 
   2819 	NETBSD32TO64_UAP(fd);
   2820 	NETBSD32TO64_UAP(how)
   2821 
   2822 	return (sys_flock(p, &ua, retval));
   2823 }
   2824 
   2825 int
   2826 compat_netbsd32_mkfifo(p, v, retval)
   2827 	struct proc *p;
   2828 	void *v;
   2829 	register_t *retval;
   2830 {
   2831 	struct compat_netbsd32_mkfifo_args /* {
   2832 		syscallarg(const netbsd32_charp) path;
   2833 		syscallarg(mode_t) mode;
   2834 	} */ *uap = v;
   2835 	struct sys_mkfifo_args ua;
   2836 
   2837 	NETBSD32TOP_UAP(path, const char)
   2838 	NETBSD32TO64_UAP(mode);
   2839 	return (sys_mkfifo(p, &ua, retval));
   2840 }
   2841 
   2842 int
   2843 compat_netbsd32_shutdown(p, v, retval)
   2844 	struct proc *p;
   2845 	void *v;
   2846 	register_t *retval;
   2847 {
   2848 	struct compat_netbsd32_shutdown_args /* {
   2849 		syscallarg(int) s;
   2850 		syscallarg(int) how;
   2851 	} */ *uap = v;
   2852 	struct sys_shutdown_args ua;
   2853 
   2854 	NETBSD32TO64_UAP(s)
   2855 	NETBSD32TO64_UAP(how);
   2856 	return (sys_shutdown(p, &ua, retval));
   2857 }
   2858 
   2859 int
   2860 compat_netbsd32_socketpair(p, v, retval)
   2861 	struct proc *p;
   2862 	void *v;
   2863 	register_t *retval;
   2864 {
   2865 	struct compat_netbsd32_socketpair_args /* {
   2866 		syscallarg(int) domain;
   2867 		syscallarg(int) type;
   2868 		syscallarg(int) protocol;
   2869 		syscallarg(netbsd32_intp) rsv;
   2870 	} */ *uap = v;
   2871 	struct sys_socketpair_args ua;
   2872 
   2873 	NETBSD32TO64_UAP(domain);
   2874 	NETBSD32TO64_UAP(type);
   2875 	NETBSD32TO64_UAP(protocol);
   2876 	NETBSD32TOP_UAP(rsv, int);
   2877 	/* Since we're just copying out two `int's we can do this */
   2878 	return (sys_socketpair(p, &ua, retval));
   2879 }
   2880 
   2881 int
   2882 compat_netbsd32_mkdir(p, v, retval)
   2883 	struct proc *p;
   2884 	void *v;
   2885 	register_t *retval;
   2886 {
   2887 	struct compat_netbsd32_mkdir_args /* {
   2888 		syscallarg(const netbsd32_charp) path;
   2889 		syscallarg(mode_t) mode;
   2890 	} */ *uap = v;
   2891 	struct sys_mkdir_args ua;
   2892 
   2893 	NETBSD32TOP_UAP(path, const char)
   2894 	NETBSD32TO64_UAP(mode);
   2895 	return (sys_mkdir(p, &ua, retval));
   2896 }
   2897 
   2898 int
   2899 compat_netbsd32_rmdir(p, v, retval)
   2900 	struct proc *p;
   2901 	void *v;
   2902 	register_t *retval;
   2903 {
   2904 	struct compat_netbsd32_rmdir_args /* {
   2905 		syscallarg(const netbsd32_charp) path;
   2906 	} */ *uap = v;
   2907 	struct sys_rmdir_args ua;
   2908 
   2909 	NETBSD32TOP_UAP(path, const char);
   2910 	return (sys_rmdir(p, &ua, retval));
   2911 }
   2912 
   2913 int
   2914 compat_netbsd32_utimes(p, v, retval)
   2915 	struct proc *p;
   2916 	void *v;
   2917 	register_t *retval;
   2918 {
   2919 	struct compat_netbsd32_utimes_args /* {
   2920 		syscallarg(const netbsd32_charp) path;
   2921 		syscallarg(const netbsd32_timevalp_t) tptr;
   2922 	} */ *uap = v;
   2923 	int error;
   2924 	struct nameidata nd;
   2925 
   2926 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (char *)(u_long)SCARG(uap, path), p);
   2927 	if ((error = namei(&nd)) != 0)
   2928 		return (error);
   2929 
   2930 	error = change_utimes32(nd.ni_vp, (struct timeval *)(u_long)SCARG(uap, tptr), p);
   2931 
   2932 	vrele(nd.ni_vp);
   2933 	return (error);
   2934 }
   2935 
   2936 /*
   2937  * Common routine to set access and modification times given a vnode.
   2938  */
   2939 static int
   2940 change_utimes32(vp, tptr, p)
   2941 	struct vnode *vp;
   2942 	struct timeval *tptr;
   2943 	struct proc *p;
   2944 {
   2945 	struct netbsd32_timeval tv32[2];
   2946 	struct timeval tv[2];
   2947 	struct vattr vattr;
   2948 	int error;
   2949 
   2950 	VATTR_NULL(&vattr);
   2951 	if (tptr == NULL) {
   2952 		microtime(&tv[0]);
   2953 		tv[1] = tv[0];
   2954 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2955 	} else {
   2956 		error = copyin(tptr, tv, sizeof(tv));
   2957 		if (error)
   2958 			return (error);
   2959 	}
   2960 	netbsd32_to_timeval(&tv32[0], &tv[0]);
   2961 	netbsd32_to_timeval(&tv32[1], &tv[1]);
   2962 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2963 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2964 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   2965 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   2966 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   2967 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   2968 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2969 	VOP_UNLOCK(vp, 0);
   2970 	return (error);
   2971 }
   2972 
   2973 int
   2974 compat_netbsd32_adjtime(p, v, retval)
   2975 	struct proc *p;
   2976 	void *v;
   2977 	register_t *retval;
   2978 {
   2979 	struct compat_netbsd32_adjtime_args /* {
   2980 		syscallarg(const netbsd32_timevalp_t) delta;
   2981 		syscallarg(netbsd32_timevalp_t) olddelta;
   2982 	} */ *uap = v;
   2983 	struct netbsd32_timeval atv;
   2984 	int32_t ndelta, ntickdelta, odelta;
   2985 	int s, error;
   2986 	extern long bigadj, timedelta;
   2987 	extern int tickdelta;
   2988 
   2989 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   2990 		return (error);
   2991 
   2992 	error = copyin((caddr_t)(u_long)SCARG(uap, delta), &atv, sizeof(struct timeval));
   2993 	if (error)
   2994 		return (error);
   2995 	/*
   2996 	 * Compute the total correction and the rate at which to apply it.
   2997 	 * Round the adjustment down to a whole multiple of the per-tick
   2998 	 * delta, so that after some number of incremental changes in
   2999 	 * hardclock(), tickdelta will become zero, lest the correction
   3000 	 * overshoot and start taking us away from the desired final time.
   3001 	 */
   3002 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
   3003 	if (ndelta > bigadj)
   3004 		ntickdelta = 10 * tickadj;
   3005 	else
   3006 		ntickdelta = tickadj;
   3007 	if (ndelta % ntickdelta)
   3008 		ndelta = ndelta / ntickdelta * ntickdelta;
   3009 
   3010 	/*
   3011 	 * To make hardclock()'s job easier, make the per-tick delta negative
   3012 	 * if we want time to run slower; then hardclock can simply compute
   3013 	 * tick + tickdelta, and subtract tickdelta from timedelta.
   3014 	 */
   3015 	if (ndelta < 0)
   3016 		ntickdelta = -ntickdelta;
   3017 	s = splclock();
   3018 	odelta = timedelta;
   3019 	timedelta = ndelta;
   3020 	tickdelta = ntickdelta;
   3021 	splx(s);
   3022 
   3023 	if (SCARG(uap, olddelta)) {
   3024 		atv.tv_sec = odelta / 1000000;
   3025 		atv.tv_usec = odelta % 1000000;
   3026 		(void) copyout(&atv, (caddr_t)(u_long)SCARG(uap, olddelta),
   3027 		    sizeof(struct timeval));
   3028 	}
   3029 	return (0);
   3030 }
   3031 
   3032 int
   3033 compat_netbsd32_quotactl(p, v, retval)
   3034 	struct proc *p;
   3035 	void *v;
   3036 	register_t *retval;
   3037 {
   3038 	struct compat_netbsd32_quotactl_args /* {
   3039 		syscallarg(const netbsd32_charp) path;
   3040 		syscallarg(int) cmd;
   3041 		syscallarg(int) uid;
   3042 		syscallarg(netbsd32_caddr_t) arg;
   3043 	} */ *uap = v;
   3044 	struct sys_quotactl_args ua;
   3045 
   3046 	NETBSD32TOP_UAP(path, const char);
   3047 	NETBSD32TO64_UAP(cmd);
   3048 	NETBSD32TO64_UAP(uid);
   3049 	NETBSD32TOX64_UAP(arg, caddr_t);
   3050 	return (sys_quotactl(p, &ua, retval));
   3051 }
   3052 
   3053 #if defined(NFS) || defined(NFSSERVER)
   3054 int
   3055 compat_netbsd32_nfssvc(p, v, retval)
   3056 	struct proc *p;
   3057 	void *v;
   3058 	register_t *retval;
   3059 {
   3060 #if 0
   3061 	struct compat_netbsd32_nfssvc_args /* {
   3062 		syscallarg(int) flag;
   3063 		syscallarg(netbsd32_voidp) argp;
   3064 	} */ *uap = v;
   3065 	struct sys_nfssvc_args ua;
   3066 
   3067 	NETBSD32TO64_UAP(flag);
   3068 	NETBSD32TOP_UAP(argp, void);
   3069 	return (sys_nfssvc(p, &ua, retval));
   3070 #else
   3071 	/* Why would we want to support a 32-bit nfsd? */
   3072 	return (ENOSYS);
   3073 #endif
   3074 }
   3075 #endif
   3076 
   3077 int
   3078 compat_netbsd32_statfs(p, v, retval)
   3079 	struct proc *p;
   3080 	void *v;
   3081 	register_t *retval;
   3082 {
   3083 	struct compat_netbsd32_statfs_args /* {
   3084 		syscallarg(const netbsd32_charp) path;
   3085 		syscallarg(netbsd32_statfsp_t) buf;
   3086 	} */ *uap = v;
   3087 	register struct mount *mp;
   3088 	register struct statfs *sp;
   3089 	struct netbsd32_statfs s32;
   3090 	int error;
   3091 	struct nameidata nd;
   3092 
   3093 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (char *)(u_long)SCARG(uap, path), p);
   3094 	if ((error = namei(&nd)) != 0)
   3095 		return (error);
   3096 	mp = nd.ni_vp->v_mount;
   3097 	sp = &mp->mnt_stat;
   3098 	vrele(nd.ni_vp);
   3099 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   3100 		return (error);
   3101 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   3102 	netbsd32_from_statfs(sp, &s32);
   3103 	return (copyout(&s32, (caddr_t)(u_long)SCARG(uap, buf), sizeof(s32)));
   3104 }
   3105 
   3106 int
   3107 compat_netbsd32_fstatfs(p, v, retval)
   3108 	struct proc *p;
   3109 	void *v;
   3110 	register_t *retval;
   3111 {
   3112 	struct compat_netbsd32_fstatfs_args /* {
   3113 		syscallarg(int) fd;
   3114 		syscallarg(netbsd32_statfsp_t) buf;
   3115 	} */ *uap = v;
   3116 	struct file *fp;
   3117 	register struct mount *mp;
   3118 	register struct statfs *sp;
   3119 	struct netbsd32_statfs s32;
   3120 	int error;
   3121 
   3122 	/* getvnode() will use the descriptor for us */
   3123 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3124 		return (error);
   3125 	mp = ((struct vnode *)fp->f_data)->v_mount;
   3126 	sp = &mp->mnt_stat;
   3127 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   3128 		goto out;
   3129 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   3130 	netbsd32_from_statfs(sp, &s32);
   3131 	error = copyout(&s32, (caddr_t)(u_long)SCARG(uap, buf), sizeof(s32));
   3132  out:
   3133 	FILE_UNUSE(fp);
   3134 	return (error);
   3135 }
   3136 
   3137 #if defined(NFS) || defined(NFSSERVER)
   3138 int
   3139 compat_netbsd32_getfh(p, v, retval)
   3140 	struct proc *p;
   3141 	void *v;
   3142 	register_t *retval;
   3143 {
   3144 	struct compat_netbsd32_getfh_args /* {
   3145 		syscallarg(const netbsd32_charp) fname;
   3146 		syscallarg(netbsd32_fhandlep_t) fhp;
   3147 	} */ *uap = v;
   3148 	struct sys_getfh_args ua;
   3149 
   3150 	NETBSD32TOP_UAP(fname, const char);
   3151 	NETBSD32TOP_UAP(fhp, struct fhandle);
   3152 	/* Lucky for us a fhandlep_t doesn't change sizes */
   3153 	return (sys_getfh(p, &ua, retval));
   3154 }
   3155 #endif
   3156 
   3157 int
   3158 compat_netbsd32_sysarch(p, v, retval)
   3159 	struct proc *p;
   3160 	void *v;
   3161 	register_t *retval;
   3162 {
   3163 	struct compat_netbsd32_sysarch_args /* {
   3164 		syscallarg(int) op;
   3165 		syscallarg(netbsd32_voidp) parms;
   3166 	} */ *uap = v;
   3167 
   3168 	switch (SCARG(uap, op)) {
   3169 	default:
   3170 		printf("(sparc64) compat_netbsd32_sysarch(%d)\n", SCARG(uap, op));
   3171 		return EINVAL;
   3172 	}
   3173 }
   3174 
   3175 int
   3176 compat_netbsd32_pread(p, v, retval)
   3177 	struct proc *p;
   3178 	void *v;
   3179 	register_t *retval;
   3180 {
   3181 	struct compat_netbsd32_pread_args /* {
   3182 		syscallarg(int) fd;
   3183 		syscallarg(netbsd32_voidp) buf;
   3184 		syscallarg(netbsd32_size_t) nbyte;
   3185 		syscallarg(int) pad;
   3186 		syscallarg(off_t) offset;
   3187 	} */ *uap = v;
   3188 	struct sys_pread_args ua;
   3189 	ssize_t rt;
   3190 	int error;
   3191 
   3192 	NETBSD32TO64_UAP(fd);
   3193 	NETBSD32TOP_UAP(buf, void);
   3194 	NETBSD32TOX_UAP(nbyte, size_t);
   3195 	NETBSD32TO64_UAP(pad);
   3196 	NETBSD32TO64_UAP(offset);
   3197 	error = sys_pread(p, &ua, (register_t *)&rt);
   3198 	*(netbsd32_ssize_t *)retval = rt;
   3199 	return (error);
   3200 }
   3201 
   3202 int
   3203 compat_netbsd32_pwrite(p, v, retval)
   3204 	struct proc *p;
   3205 	void *v;
   3206 	register_t *retval;
   3207 {
   3208 	struct compat_netbsd32_pwrite_args /* {
   3209 		syscallarg(int) fd;
   3210 		syscallarg(const netbsd32_voidp) buf;
   3211 		syscallarg(netbsd32_size_t) nbyte;
   3212 		syscallarg(int) pad;
   3213 		syscallarg(off_t) offset;
   3214 	} */ *uap = v;
   3215 	struct sys_pwrite_args ua;
   3216 	ssize_t rt;
   3217 	int error;
   3218 
   3219 	NETBSD32TO64_UAP(fd);
   3220 	NETBSD32TOP_UAP(buf, void);
   3221 	NETBSD32TOX_UAP(nbyte, size_t);
   3222 	NETBSD32TO64_UAP(pad);
   3223 	NETBSD32TO64_UAP(offset);
   3224 	error = sys_pwrite(p, &ua, (register_t *)&rt);
   3225 	*(netbsd32_ssize_t *)retval = rt;
   3226 	return (error);
   3227 }
   3228 
   3229 #ifdef NTP
   3230 int
   3231 compat_netbsd32_ntp_gettime(p, v, retval)
   3232 	struct proc *p;
   3233 	void *v;
   3234 	register_t *retval;
   3235 {
   3236 	struct compat_netbsd32_ntp_gettime_args /* {
   3237 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
   3238 	} */ *uap = v;
   3239 	struct netbsd32_ntptimeval ntv32;
   3240 	struct timeval atv;
   3241 	struct ntptimeval ntv;
   3242 	int error = 0;
   3243 	int s;
   3244 
   3245 	/* The following are NTP variables */
   3246 	extern long time_maxerror;
   3247 	extern long time_esterror;
   3248 	extern int time_status;
   3249 	extern int time_state;	/* clock state */
   3250 	extern int time_status;	/* clock status bits */
   3251 
   3252 	if (SCARG(uap, ntvp)) {
   3253 		s = splclock();
   3254 #ifdef EXT_CLOCK
   3255 		/*
   3256 		 * The microtime() external clock routine returns a
   3257 		 * status code. If less than zero, we declare an error
   3258 		 * in the clock status word and return the kernel
   3259 		 * (software) time variable. While there are other
   3260 		 * places that call microtime(), this is the only place
   3261 		 * that matters from an application point of view.
   3262 		 */
   3263 		if (microtime(&atv) < 0) {
   3264 			time_status |= STA_CLOCKERR;
   3265 			ntv.time = time;
   3266 		} else
   3267 			time_status &= ~STA_CLOCKERR;
   3268 #else /* EXT_CLOCK */
   3269 		microtime(&atv);
   3270 #endif /* EXT_CLOCK */
   3271 		ntv.time = atv;
   3272 		ntv.maxerror = time_maxerror;
   3273 		ntv.esterror = time_esterror;
   3274 		(void) splx(s);
   3275 
   3276 		netbsd32_from_timeval(&ntv.time, &ntv32.time);
   3277 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
   3278 		ntv32.esterror = (netbsd32_long)ntv.esterror;
   3279 		error = copyout((caddr_t)&ntv32, (caddr_t)(u_long)SCARG(uap, ntvp),
   3280 		    sizeof(ntv32));
   3281 	}
   3282 	if (!error) {
   3283 
   3284 		/*
   3285 		 * Status word error decode. If any of these conditions
   3286 		 * occur, an error is returned, instead of the status
   3287 		 * word. Most applications will care only about the fact
   3288 		 * the system clock may not be trusted, not about the
   3289 		 * details.
   3290 		 *
   3291 		 * Hardware or software error
   3292 		 */
   3293 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   3294 
   3295 		/*
   3296 		 * PPS signal lost when either time or frequency
   3297 		 * synchronization requested
   3298 		 */
   3299 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   3300 		    !(time_status & STA_PPSSIGNAL)) ||
   3301 
   3302 		/*
   3303 		 * PPS jitter exceeded when time synchronization
   3304 		 * requested
   3305 		 */
   3306 		    (time_status & STA_PPSTIME &&
   3307 		    time_status & STA_PPSJITTER) ||
   3308 
   3309 		/*
   3310 		 * PPS wander exceeded or calibration error when
   3311 		 * frequency synchronization requested
   3312 		 */
   3313 		    (time_status & STA_PPSFREQ &&
   3314 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
   3315 			*retval = TIME_ERROR;
   3316 		else
   3317 			*retval = (register_t)time_state;
   3318 	}
   3319 	return(error);
   3320 }
   3321 
   3322 int
   3323 compat_netbsd32_ntp_adjtime(p, v, retval)
   3324 	struct proc *p;
   3325 	void *v;
   3326 	register_t *retval;
   3327 {
   3328 	struct compat_netbsd32_ntp_adjtime_args /* {
   3329 		syscallarg(netbsd32_timexp_t) tp;
   3330 	} */ *uap = v;
   3331 	struct netbsd32_timex ntv32;
   3332 	struct timex ntv;
   3333 	int error = 0;
   3334 	int modes;
   3335 	int s;
   3336 	extern long time_freq;		/* frequency offset (scaled ppm) */
   3337 	extern long time_maxerror;
   3338 	extern long time_esterror;
   3339 	extern int time_state;	/* clock state */
   3340 	extern int time_status;	/* clock status bits */
   3341 	extern long time_constant;		/* pll time constant */
   3342 	extern long time_offset;		/* time offset (us) */
   3343 	extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
   3344 	extern long time_precision;	/* clock precision (us) */
   3345 
   3346 	if ((error = copyin((caddr_t)(u_long)SCARG(uap, tp), (caddr_t)&ntv32,
   3347 			sizeof(ntv32))))
   3348 		return (error);
   3349 	netbsd32_to_timex(&ntv32, &ntv);
   3350 
   3351 	/*
   3352 	 * Update selected clock variables - only the superuser can
   3353 	 * change anything. Note that there is no error checking here on
   3354 	 * the assumption the superuser should know what it is doing.
   3355 	 */
   3356 	modes = ntv.modes;
   3357 	if (modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)))
   3358 		return (error);
   3359 
   3360 	s = splclock();
   3361 	if (modes & MOD_FREQUENCY)
   3362 #ifdef PPS_SYNC
   3363 		time_freq = ntv.freq - pps_freq;
   3364 #else /* PPS_SYNC */
   3365 		time_freq = ntv.freq;
   3366 #endif /* PPS_SYNC */
   3367 	if (modes & MOD_MAXERROR)
   3368 		time_maxerror = ntv.maxerror;
   3369 	if (modes & MOD_ESTERROR)
   3370 		time_esterror = ntv.esterror;
   3371 	if (modes & MOD_STATUS) {
   3372 		time_status &= STA_RONLY;
   3373 		time_status |= ntv.status & ~STA_RONLY;
   3374 	}
   3375 	if (modes & MOD_TIMECONST)
   3376 		time_constant = ntv.constant;
   3377 	if (modes & MOD_OFFSET)
   3378 		hardupdate(ntv.offset);
   3379 
   3380 	/*
   3381 	 * Retrieve all clock variables
   3382 	 */
   3383 	if (time_offset < 0)
   3384 		ntv.offset = -(-time_offset >> SHIFT_UPDATE);
   3385 	else
   3386 		ntv.offset = time_offset >> SHIFT_UPDATE;
   3387 #ifdef PPS_SYNC
   3388 	ntv.freq = time_freq + pps_freq;
   3389 #else /* PPS_SYNC */
   3390 	ntv.freq = time_freq;
   3391 #endif /* PPS_SYNC */
   3392 	ntv.maxerror = time_maxerror;
   3393 	ntv.esterror = time_esterror;
   3394 	ntv.status = time_status;
   3395 	ntv.constant = time_constant;
   3396 	ntv.precision = time_precision;
   3397 	ntv.tolerance = time_tolerance;
   3398 #ifdef PPS_SYNC
   3399 	ntv.shift = pps_shift;
   3400 	ntv.ppsfreq = pps_freq;
   3401 	ntv.jitter = pps_jitter >> PPS_AVG;
   3402 	ntv.stabil = pps_stabil;
   3403 	ntv.calcnt = pps_calcnt;
   3404 	ntv.errcnt = pps_errcnt;
   3405 	ntv.jitcnt = pps_jitcnt;
   3406 	ntv.stbcnt = pps_stbcnt;
   3407 #endif /* PPS_SYNC */
   3408 	(void)splx(s);
   3409 
   3410 	netbsd32_from_timeval(&ntv, &ntv32);
   3411 	error = copyout((caddr_t)&ntv32, (caddr_t)SCARG(uap, tp), sizeof(ntv32));
   3412 	if (!error) {
   3413 
   3414 		/*
   3415 		 * Status word error decode. See comments in
   3416 		 * ntp_gettime() routine.
   3417 		 */
   3418 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   3419 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   3420 		    !(time_status & STA_PPSSIGNAL)) ||
   3421 		    (time_status & STA_PPSTIME &&
   3422 		    time_status & STA_PPSJITTER) ||
   3423 		    (time_status & STA_PPSFREQ &&
   3424 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
   3425 			*retval = TIME_ERROR;
   3426 		else
   3427 			*retval = (register_t)time_state;
   3428 	}
   3429 	return error;
   3430 }
   3431 #endif
   3432 
   3433 int
   3434 compat_netbsd32_setgid(p, v, retval)
   3435 	struct proc *p;
   3436 	void *v;
   3437 	register_t *retval;
   3438 {
   3439 	struct compat_netbsd32_setgid_args /* {
   3440 		syscallarg(gid_t) gid;
   3441 	} */ *uap = v;
   3442 	struct sys_setgid_args ua;
   3443 
   3444 	NETBSD32TO64_UAP(gid);
   3445 	return (sys_setgid(p, v, retval));
   3446 }
   3447 
   3448 int
   3449 compat_netbsd32_setegid(p, v, retval)
   3450 	struct proc *p;
   3451 	void *v;
   3452 	register_t *retval;
   3453 {
   3454 	struct compat_netbsd32_setegid_args /* {
   3455 		syscallarg(gid_t) egid;
   3456 	} */ *uap = v;
   3457 	struct sys_setegid_args ua;
   3458 
   3459 	NETBSD32TO64_UAP(egid);
   3460 	return (sys_setegid(p, v, retval));
   3461 }
   3462 
   3463 int
   3464 compat_netbsd32_seteuid(p, v, retval)
   3465 	struct proc *p;
   3466 	void *v;
   3467 	register_t *retval;
   3468 {
   3469 	struct compat_netbsd32_seteuid_args /* {
   3470 		syscallarg(gid_t) euid;
   3471 	} */ *uap = v;
   3472 	struct sys_seteuid_args ua;
   3473 
   3474 	NETBSD32TO64_UAP(euid);
   3475 	return (sys_seteuid(p, v, retval));
   3476 }
   3477 
   3478 #ifdef LFS
   3479 int
   3480 compat_netbsd32_lfs_bmapv(p, v, retval)
   3481 	struct proc *p;
   3482 	void *v;
   3483 	register_t *retval;
   3484 {
   3485 #if 0
   3486 	struct compat_netbsd32_lfs_bmapv_args /* {
   3487 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3488 		syscallarg(netbsd32_block_infop_t) blkiov;
   3489 		syscallarg(int) blkcnt;
   3490 	} */ *uap = v;
   3491 	struct sys_lfs_bmapv_args ua;
   3492 
   3493 	NETBSD32TOP_UAP(fdidp, struct fsid);
   3494 	NETBSD32TO64_UAP(blkcnt);
   3495 	/* XXX finish me */
   3496 #else
   3497 
   3498 	return (ENOSYS);	/* XXX */
   3499 #endif
   3500 }
   3501 
   3502 int
   3503 compat_netbsd32_lfs_markv(p, v, retval)
   3504 	struct proc *p;
   3505 	void *v;
   3506 	register_t *retval;
   3507 {
   3508 	struct compat_netbsd32_lfs_markv_args /* {
   3509 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3510 		syscallarg(netbsd32_block_infop_t) blkiov;
   3511 		syscallarg(int) blkcnt;
   3512 	} */ *uap = v;
   3513 
   3514 	return (ENOSYS);	/* XXX */
   3515 }
   3516 
   3517 int
   3518 compat_netbsd32_lfs_segclean(p, v, retval)
   3519 	struct proc *p;
   3520 	void *v;
   3521 	register_t *retval;
   3522 {
   3523 	struct compat_netbsd32_lfs_segclean_args /* {
   3524 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3525 		syscallarg(netbsd32_u_long) segment;
   3526 	} */ *uap = v;
   3527 	return (ENOSYS);	/* XXX */
   3528 }
   3529 
   3530 int
   3531 compat_netbsd32_lfs_segwait(p, v, retval)
   3532 	struct proc *p;
   3533 	void *v;
   3534 	register_t *retval;
   3535 {
   3536 	struct compat_netbsd32_lfs_segwait_args /* {
   3537 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3538 		syscallarg(netbsd32_timevalp_t) tv;
   3539 	} */ *uap = v;
   3540 	return (ENOSYS);	/* XXX */
   3541 }
   3542 #endif
   3543 
   3544 int
   3545 compat_netbsd32_pathconf(p, v, retval)
   3546 	struct proc *p;
   3547 	void *v;
   3548 	register_t *retval;
   3549 {
   3550 	struct compat_netbsd32_pathconf_args /* {
   3551 		syscallarg(int) fd;
   3552 		syscallarg(int) name;
   3553 	} */ *uap = v;
   3554 	struct sys_pathconf_args ua;
   3555 	long rt;
   3556 	int error;
   3557 
   3558 	NETBSD32TOP_UAP(path, const char);
   3559 	NETBSD32TO64_UAP(name);
   3560 	error = sys_pathconf(p, &ua, (register_t *)&rt);
   3561 	*(netbsd32_long *)retval = (netbsd32_long)rt;
   3562 	return (error);
   3563 }
   3564 
   3565 int
   3566 compat_netbsd32_fpathconf(p, v, retval)
   3567 	struct proc *p;
   3568 	void *v;
   3569 	register_t *retval;
   3570 {
   3571 	struct compat_netbsd32_fpathconf_args /* {
   3572 		syscallarg(int) fd;
   3573 		syscallarg(int) name;
   3574 	} */ *uap = v;
   3575 	struct sys_fpathconf_args ua;
   3576 	long rt;
   3577 	int error;
   3578 
   3579 	NETBSD32TO64_UAP(fd);
   3580 	NETBSD32TO64_UAP(name);
   3581 	error = sys_fpathconf(p, &ua, (register_t *)&rt);
   3582 	*(netbsd32_long *)retval = (netbsd32_long)rt;
   3583 	return (error);
   3584 }
   3585 
   3586 int
   3587 compat_netbsd32_getrlimit(p, v, retval)
   3588 	struct proc *p;
   3589 	void *v;
   3590 	register_t *retval;
   3591 {
   3592 	struct compat_netbsd32_getrlimit_args /* {
   3593 		syscallarg(int) which;
   3594 		syscallarg(netbsd32_rlimitp_t) rlp;
   3595 	} */ *uap = v;
   3596 	int which = SCARG(uap, which);
   3597 
   3598 	if ((u_int)which >= RLIM_NLIMITS)
   3599 		return (EINVAL);
   3600 	return (copyout(&p->p_rlimit[which], (caddr_t)(u_long)SCARG(uap, rlp),
   3601 	    sizeof(struct rlimit)));
   3602 }
   3603 
   3604 int
   3605 compat_netbsd32_setrlimit(p, v, retval)
   3606 	struct proc *p;
   3607 	void *v;
   3608 	register_t *retval;
   3609 {
   3610 	struct compat_netbsd32_setrlimit_args /* {
   3611 		syscallarg(int) which;
   3612 		syscallarg(const netbsd32_rlimitp_t) rlp;
   3613 	} */ *uap = v;
   3614 		int which = SCARG(uap, which);
   3615 	struct rlimit alim;
   3616 	int error;
   3617 
   3618 	error = copyin((caddr_t)(u_long)SCARG(uap, rlp), &alim, sizeof(struct rlimit));
   3619 	if (error)
   3620 		return (error);
   3621 	return (dosetrlimit(p, which, &alim));
   3622 }
   3623 
   3624 int
   3625 compat_netbsd32_mmap(p, v, retval)
   3626 	struct proc *p;
   3627 	void *v;
   3628 	register_t *retval;
   3629 {
   3630 	struct compat_netbsd32_mmap_args /* {
   3631 		syscallarg(netbsd32_voidp) addr;
   3632 		syscallarg(netbsd32_size_t) len;
   3633 		syscallarg(int) prot;
   3634 		syscallarg(int) flags;
   3635 		syscallarg(int) fd;
   3636 		syscallarg(netbsd32_long) pad;
   3637 		syscallarg(off_t) pos;
   3638 	} */ *uap = v;
   3639 	struct sys_mmap_args ua;
   3640 	void *rt;
   3641 	int error;
   3642 
   3643 	NETBSD32TOP_UAP(addr, void);
   3644 	NETBSD32TOX_UAP(len, size_t);
   3645 	NETBSD32TO64_UAP(prot);
   3646 	NETBSD32TO64_UAP(flags);
   3647 	NETBSD32TO64_UAP(fd);
   3648 	NETBSD32TOX_UAP(pad, long);
   3649 	NETBSD32TOX_UAP(pos, off_t);
   3650 	error = sys_mmap(p, &ua, (register_t *)&rt);
   3651 	if ((long)rt > (long)UINT_MAX)
   3652 		printf("compat_netbsd32_mmap: retval out of range: 0x%qx",
   3653 		    rt);
   3654 	*retval = (netbsd32_voidp)(u_long)rt;
   3655 	return (error);
   3656 }
   3657 
   3658 int
   3659 compat_netbsd32_lseek(p, v, retval)
   3660 	struct proc *p;
   3661 	void *v;
   3662 	register_t *retval;
   3663 {
   3664 	struct compat_netbsd32_lseek_args /* {
   3665 		syscallarg(int) fd;
   3666 		syscallarg(int) pad;
   3667 		syscallarg(off_t) offset;
   3668 		syscallarg(int) whence;
   3669 	} */ *uap = v;
   3670 	struct sys_lseek_args ua;
   3671 
   3672 	NETBSD32TO64_UAP(fd);
   3673 	NETBSD32TO64_UAP(pad);
   3674 	NETBSD32TO64_UAP(offset);
   3675 	NETBSD32TO64_UAP(whence);
   3676 	return (sys_lseek(p, &ua, retval));
   3677 }
   3678 
   3679 int
   3680 compat_netbsd32_truncate(p, v, retval)
   3681 	struct proc *p;
   3682 	void *v;
   3683 	register_t *retval;
   3684 {
   3685 	struct compat_netbsd32_truncate_args /* {
   3686 		syscallarg(const netbsd32_charp) path;
   3687 		syscallarg(int) pad;
   3688 		syscallarg(off_t) length;
   3689 	} */ *uap = v;
   3690 	struct sys_truncate_args ua;
   3691 
   3692 	NETBSD32TOP_UAP(path, const char);
   3693 	NETBSD32TO64_UAP(pad);
   3694 	NETBSD32TO64_UAP(length);
   3695 	return (sys_truncate(p, &ua, retval));
   3696 }
   3697 
   3698 int
   3699 compat_netbsd32_ftruncate(p, v, retval)
   3700 	struct proc *p;
   3701 	void *v;
   3702 	register_t *retval;
   3703 {
   3704 	struct compat_netbsd32_ftruncate_args /* {
   3705 		syscallarg(int) fd;
   3706 		syscallarg(int) pad;
   3707 		syscallarg(off_t) length;
   3708 	} */ *uap = v;
   3709 	struct sys_ftruncate_args ua;
   3710 
   3711 	NETBSD32TO64_UAP(fd);
   3712 	NETBSD32TO64_UAP(pad);
   3713 	NETBSD32TO64_UAP(length);
   3714 	return (sys_ftruncate(p, &ua, retval));
   3715 }
   3716 
   3717 int
   3718 compat_netbsd32___sysctl(p, v, retval)
   3719 	struct proc *p;
   3720 	void *v;
   3721 	register_t *retval;
   3722 {
   3723 	struct compat_netbsd32___sysctl_args /* {
   3724 		syscallarg(netbsd32_intp) name;
   3725 		syscallarg(u_int) namelen;
   3726 		syscallarg(netbsd32_voidp) old;
   3727 		syscallarg(netbsd32_size_tp) oldlenp;
   3728 		syscallarg(netbsd32_voidp) new;
   3729 		syscallarg(netbsd32_size_t) newlen;
   3730 	} */ *uap = v;
   3731 	int error, dolock = 1;
   3732 	netbsd32_size_t savelen = 0;
   3733 	size_t oldlen = 0;
   3734 	sysctlfn *fn;
   3735 	int name[CTL_MAXNAME];
   3736 
   3737 /*
   3738  * Some of these sysctl functions do their own copyin/copyout.
   3739  * We need to disable or emulate the ones that need their
   3740  * arguments converted.
   3741  */
   3742 
   3743 	if (SCARG(uap, new) != NULL &&
   3744 	    (error = suser(p->p_ucred, &p->p_acflag)))
   3745 		return (error);
   3746 	/*
   3747 	 * all top-level sysctl names are non-terminal
   3748 	 */
   3749 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
   3750 		return (EINVAL);
   3751 	error = copyin((caddr_t)(u_long)SCARG(uap, name), &name,
   3752 		       SCARG(uap, namelen) * sizeof(int));
   3753 	if (error)
   3754 		return (error);
   3755 
   3756 	switch (name[0]) {
   3757 	case CTL_KERN:
   3758 		fn = kern_sysctl;
   3759 		if (name[2] != KERN_VNODE)	/* XXX */
   3760 			dolock = 0;
   3761 		break;
   3762 	case CTL_HW:
   3763 		fn = hw_sysctl;
   3764 		break;
   3765 	case CTL_VM:
   3766 		fn = uvm_sysctl;
   3767 		break;
   3768 	case CTL_NET:
   3769 		fn = net_sysctl;
   3770 		break;
   3771 	case CTL_VFS:
   3772 		fn = vfs_sysctl;
   3773 		break;
   3774 	case CTL_MACHDEP:
   3775 		fn = cpu_sysctl;
   3776 		break;
   3777 #ifdef DEBUG
   3778 	case CTL_DEBUG:
   3779 		fn = debug_sysctl;
   3780 		break;
   3781 #endif
   3782 #ifdef DDB
   3783 	case CTL_DDB:
   3784 		fn = ddb_sysctl;
   3785 		break;
   3786 #endif
   3787 	default:
   3788 		return (EOPNOTSUPP);
   3789 	}
   3790 
   3791 	if (SCARG(uap, oldlenp) &&
   3792 	    (error = copyin((caddr_t)(u_long)SCARG(uap, oldlenp), &savelen, sizeof(savelen))))
   3793 		return (error);
   3794 	if (SCARG(uap, old) != NULL) {
   3795 		if (!uvm_useracc((caddr_t)(u_long)SCARG(uap, old), savelen, B_WRITE))
   3796 			return (EFAULT);
   3797 #if 0 /* XXXXXXXX */
   3798 		while (memlock.sl_lock) {
   3799 			memlock.sl_want = 1;
   3800 			sleep((caddr_t)&memlock, PRIBIO+1);
   3801 			memlock.sl_locked++;
   3802 		}
   3803 		memlock.sl_lock = 1;
   3804 #endif /* XXXXXXXX */
   3805 		if (dolock) {
   3806 			/*
   3807 			 * XXX Um, this is kind of evil.  What should
   3808 			 * XXX we be passing here?
   3809 			 */
   3810 			uvm_vslock(p, SCARG(uap, old), savelen, VM_PROT_NONE);
   3811 		}
   3812 		oldlen = savelen;
   3813 	}
   3814 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
   3815 	    &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
   3816 	if (SCARG(uap, old) != NULL) {
   3817 		if (dolock)
   3818 			uvm_vsunlock(p, SCARG(uap, old), savelen);
   3819 #if 0 /* XXXXXXXXXXX */
   3820 		memlock.sl_lock = 0;
   3821 		if (memlock.sl_want) {
   3822 			memlock.sl_want = 0;
   3823 			wakeup((caddr_t)&memlock);
   3824 		}
   3825 #endif /* XXXXXXXXX */
   3826 	}
   3827 	savelen = oldlen;
   3828 	if (error)
   3829 		return (error);
   3830 	if (SCARG(uap, oldlenp))
   3831 		error = copyout(&savelen, (caddr_t)(u_long)SCARG(uap, oldlenp), sizeof(savelen));
   3832 	return (error);
   3833 }
   3834 
   3835 int
   3836 compat_netbsd32_mlock(p, v, retval)
   3837 	struct proc *p;
   3838 	void *v;
   3839 	register_t *retval;
   3840 {
   3841 	struct compat_netbsd32_mlock_args /* {
   3842 		syscallarg(const netbsd32_voidp) addr;
   3843 		syscallarg(netbsd32_size_t) len;
   3844 	} */ *uap = v;
   3845 	struct sys_mlock_args ua;
   3846 
   3847 	NETBSD32TOP_UAP(addr, const void);
   3848 	NETBSD32TO64_UAP(len);
   3849 	return (sys_mlock(p, &ua, retval));
   3850 }
   3851 
   3852 int
   3853 compat_netbsd32_munlock(p, v, retval)
   3854 	struct proc *p;
   3855 	void *v;
   3856 	register_t *retval;
   3857 {
   3858 	struct compat_netbsd32_munlock_args /* {
   3859 		syscallarg(const netbsd32_voidp) addr;
   3860 		syscallarg(netbsd32_size_t) len;
   3861 	} */ *uap = v;
   3862 	struct sys_munlock_args ua;
   3863 
   3864 	NETBSD32TOP_UAP(addr, const void);
   3865 	NETBSD32TO64_UAP(len);
   3866 	return (sys_munlock(p, &ua, retval));
   3867 }
   3868 
   3869 int
   3870 compat_netbsd32_undelete(p, v, retval)
   3871 	struct proc *p;
   3872 	void *v;
   3873 	register_t *retval;
   3874 {
   3875 	struct compat_netbsd32_undelete_args /* {
   3876 		syscallarg(const netbsd32_charp) path;
   3877 	} */ *uap = v;
   3878 	struct sys_undelete_args ua;
   3879 
   3880 	NETBSD32TOP_UAP(path, const char);
   3881 	return (sys_undelete(p, &ua, retval));
   3882 }
   3883 
   3884 int
   3885 compat_netbsd32_futimes(p, v, retval)
   3886 	struct proc *p;
   3887 	void *v;
   3888 	register_t *retval;
   3889 {
   3890 	struct compat_netbsd32_futimes_args /* {
   3891 		syscallarg(int) fd;
   3892 		syscallarg(const netbsd32_timevalp_t) tptr;
   3893 	} */ *uap = v;
   3894 	int error;
   3895 	struct file *fp;
   3896 
   3897 	/* getvnode() will use the descriptor for us */
   3898 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3899 		return (error);
   3900 
   3901 	error = change_utimes32((struct vnode *)fp->f_data,
   3902 				(struct timeval *)(u_long)SCARG(uap, tptr), p);
   3903 	FILE_UNUSE(fp);
   3904 	return (error);
   3905 }
   3906 
   3907 int
   3908 compat_netbsd32_getpgid(p, v, retval)
   3909 	struct proc *p;
   3910 	void *v;
   3911 	register_t *retval;
   3912 {
   3913 	struct compat_netbsd32_getpgid_args /* {
   3914 		syscallarg(pid_t) pid;
   3915 	} */ *uap = v;
   3916 	struct sys_getpgid_args ua;
   3917 
   3918 	NETBSD32TO64_UAP(pid);
   3919 	return (sys_getpgid(p, &ua, retval));
   3920 }
   3921 
   3922 int
   3923 compat_netbsd32_reboot(p, v, retval)
   3924 	struct proc *p;
   3925 	void *v;
   3926 	register_t *retval;
   3927 {
   3928 	struct compat_netbsd32_reboot_args /* {
   3929 		syscallarg(int) opt;
   3930 		syscallarg(netbsd32_charp) bootstr;
   3931 	} */ *uap = v;
   3932 	struct sys_reboot_args ua;
   3933 
   3934 	NETBSD32TO64_UAP(opt);
   3935 	NETBSD32TOP_UAP(bootstr, char);
   3936 	return (sys_reboot(p, &ua, retval));
   3937 }
   3938 
   3939 int
   3940 compat_netbsd32_poll(p, v, retval)
   3941 	struct proc *p;
   3942 	void *v;
   3943 	register_t *retval;
   3944 {
   3945 	struct compat_netbsd32_poll_args /* {
   3946 		syscallarg(netbsd32_pollfdp_t) fds;
   3947 		syscallarg(u_int) nfds;
   3948 		syscallarg(int) timeout;
   3949 	} */ *uap = v;
   3950 	struct sys_poll_args ua;
   3951 
   3952 	NETBSD32TOP_UAP(fds, struct pollfd);
   3953 	NETBSD32TO64_UAP(nfds);
   3954 	NETBSD32TO64_UAP(timeout);
   3955 	return (sys_poll(p, &ua, retval));
   3956 }
   3957 
   3958 /*
   3959  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   3960  *
   3961  * This is BSD.  We won't support System V IPC.
   3962  * Too much work.
   3963  *
   3964  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   3965  */
   3966 int
   3967 compat_netbsd32___semctl(p, v, retval)
   3968 	struct proc *p;
   3969 	void *v;
   3970 	register_t *retval;
   3971 {
   3972 #if 0
   3973 	struct compat_netbsd32___semctl_args /* {
   3974 		syscallarg(int) semid;
   3975 		syscallarg(int) semnum;
   3976 		syscallarg(int) cmd;
   3977 		syscallarg(netbsd32_semunu_t) arg;
   3978 	} */ *uap = v;
   3979 	union netbsd32_semun sem32;
   3980 	int semid = SCARG(uap, semid);
   3981 	int semnum = SCARG(uap, semnum);
   3982 	int cmd = SCARG(uap, cmd);
   3983 	union netbsd32_semun *arg = (void*)(u_long)SCARG(uap, arg);
   3984 	union netbsd32_semun real_arg;
   3985 	struct ucred *cred = p->p_ucred;
   3986 	int i, rval, eval;
   3987 	struct netbsd32_semid_ds sbuf;
   3988 	register struct semid_ds *semaptr;
   3989 
   3990 	semlock(p);
   3991 
   3992 	semid = IPCID_TO_IX(semid);
   3993 	if (semid < 0 || semid >= seminfo.semmsl)
   3994 		return(EINVAL);
   3995 
   3996 	semaptr = &sema[semid];
   3997 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
   3998 	    semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid)))
   3999 		return(EINVAL);
   4000 
   4001 	eval = 0;
   4002 	rval = 0;
   4003 
   4004 	switch (cmd) {
   4005 	case IPC_RMID:
   4006 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
   4007 			return(eval);
   4008 		semaptr->sem_perm.cuid = cred->cr_uid;
   4009 		semaptr->sem_perm.uid = cred->cr_uid;
   4010 		semtot -= semaptr->sem_nsems;
   4011 		for (i = semaptr->sem_base - sem; i < semtot; i++)
   4012 			sem[i] = sem[i + semaptr->sem_nsems];
   4013 		for (i = 0; i < seminfo.semmni; i++) {
   4014 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
   4015 			    sema[i].sem_base > semaptr->sem_base)
   4016 				sema[i].sem_base -= semaptr->sem_nsems;
   4017 		}
   4018 		semaptr->sem_perm.mode = 0;
   4019 		semundo_clear(semid, -1);
   4020 		wakeup((caddr_t)semaptr);
   4021 		break;
   4022 
   4023 	case IPC_SET:
   4024 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
   4025 			return(eval);
   4026 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4027 			return(eval);
   4028 		if ((eval = copyin((caddr_t)(u_long)real_arg.buf, (caddr_t)&sbuf,
   4029 		    sizeof(sbuf))) != 0)
   4030 			return(eval);
   4031 		semaptr->sem_perm.uid = sbuf.sem_perm.uid;
   4032 		semaptr->sem_perm.gid = sbuf.sem_perm.gid;
   4033 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
   4034 		    (sbuf.sem_perm.mode & 0777);
   4035 		semaptr->sem_ctime = time.tv_sec;
   4036 		break;
   4037 
   4038 	case IPC_STAT:
   4039 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4040 			return(eval);
   4041 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4042 			return(eval);
   4043 		eval = copyout((caddr_t)semaptr, (caddr_t)(u_long)real_arg.buf,
   4044 		    sizeof(struct semid_ds));
   4045 		break;
   4046 
   4047 	case GETNCNT:
   4048 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4049 			return(eval);
   4050 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4051 			return(EINVAL);
   4052 		rval = semaptr->sem_base[semnum].semncnt;
   4053 		break;
   4054 
   4055 	case GETPID:
   4056 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4057 			return(eval);
   4058 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4059 			return(EINVAL);
   4060 		rval = semaptr->sem_base[semnum].sempid;
   4061 		break;
   4062 
   4063 	case GETVAL:
   4064 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4065 			return(eval);
   4066 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4067 			return(EINVAL);
   4068 		rval = semaptr->sem_base[semnum].semval;
   4069 		break;
   4070 
   4071 	case GETALL:
   4072 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4073 			return(eval);
   4074 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4075 			return(eval);
   4076 		for (i = 0; i < semaptr->sem_nsems; i++) {
   4077 			eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
   4078 			    &real_arg.array[i], sizeof(real_arg.array[0]));
   4079 			if (eval != 0)
   4080 				break;
   4081 		}
   4082 		break;
   4083 
   4084 	case GETZCNT:
   4085 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4086 			return(eval);
   4087 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4088 			return(EINVAL);
   4089 		rval = semaptr->sem_base[semnum].semzcnt;
   4090 		break;
   4091 
   4092 	case SETVAL:
   4093 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
   4094 			return(eval);
   4095 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4096 			return(EINVAL);
   4097 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4098 			return(eval);
   4099 		semaptr->sem_base[semnum].semval = real_arg.val;
   4100 		semundo_clear(semid, semnum);
   4101 		wakeup((caddr_t)semaptr);
   4102 		break;
   4103 
   4104 	case SETALL:
   4105 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
   4106 			return(eval);
   4107 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4108 			return(eval);
   4109 		for (i = 0; i < semaptr->sem_nsems; i++) {
   4110 			eval = copyin(&real_arg.array[i],
   4111 			    (caddr_t)&semaptr->sem_base[i].semval,
   4112 			    sizeof(real_arg.array[0]));
   4113 			if (eval != 0)
   4114 				break;
   4115 		}
   4116 		semundo_clear(semid, -1);
   4117 		wakeup((caddr_t)semaptr);
   4118 		break;
   4119 
   4120 	default:
   4121 		return(EINVAL);
   4122 	}
   4123 
   4124 	if (eval == 0)
   4125 		*retval = rval;
   4126 	return(eval);
   4127 #else
   4128 	return (ENOSYS);
   4129 #endif
   4130 }
   4131 
   4132 int
   4133 compat_netbsd32_semget(p, v, retval)
   4134 	struct proc *p;
   4135 	void *v;
   4136 	register_t *retval;
   4137 {
   4138 	struct compat_netbsd32_semget_args /* {
   4139 		syscallarg(netbsd32_key_t) key;
   4140 		syscallarg(int) nsems;
   4141 		syscallarg(int) semflg;
   4142 	} */ *uap = v;
   4143 	struct sys_semget_args ua;
   4144 
   4145 	NETBSD32TOX_UAP(key, key_t);
   4146 	NETBSD32TO64_UAP(nsems);
   4147 	NETBSD32TO64_UAP(semflg);
   4148 	return (sys_semget(p, &ua, retval));
   4149 }
   4150 
   4151 int
   4152 compat_netbsd32_semop(p, v, retval)
   4153 	struct proc *p;
   4154 	void *v;
   4155 	register_t *retval;
   4156 {
   4157 	struct compat_netbsd32_semop_args /* {
   4158 		syscallarg(int) semid;
   4159 		syscallarg(netbsd32_sembufp_t) sops;
   4160 		syscallarg(netbsd32_size_t) nsops;
   4161 	} */ *uap = v;
   4162 	struct sys_semop_args ua;
   4163 
   4164 	NETBSD32TO64_UAP(semid);
   4165 	NETBSD32TOP_UAP(sops, struct sembuf);
   4166 	NETBSD32TOX_UAP(nsops, size_t);
   4167 	return (sys_semop(p, &ua, retval));
   4168 }
   4169 
   4170 int
   4171 compat_netbsd32_semconfig(p, v, retval)
   4172 	struct proc *p;
   4173 	void *v;
   4174 	register_t *retval;
   4175 {
   4176 	struct compat_netbsd32_semconfig_args /* {
   4177 		syscallarg(int) flag;
   4178 	} */ *uap = v;
   4179 	struct sys_semconfig_args ua;
   4180 
   4181 	NETBSD32TO64_UAP(flag);
   4182 	return (sys_semconfig(p, &ua, retval));
   4183 }
   4184 
   4185 int
   4186 compat_netbsd32_msgctl(p, v, retval)
   4187 	struct proc *p;
   4188 	void *v;
   4189 	register_t *retval;
   4190 {
   4191 #if 0
   4192 	struct compat_netbsd32_msgctl_args /* {
   4193 		syscallarg(int) msqid;
   4194 		syscallarg(int) cmd;
   4195 		syscallarg(netbsd32_msqid_dsp_t) buf;
   4196 	} */ *uap = v;
   4197 	struct sys_msgctl_args ua;
   4198 	struct msqid_ds ds;
   4199 	struct netbsd32_msqid_ds *ds32p;
   4200 	int error;
   4201 
   4202 	NETBSD32TO64_UAP(msqid);
   4203 	NETBSD32TO64_UAP(cmd);
   4204 	ds32p = (struct netbsd32_msqid_ds *)(u_long)SCARG(uap, buf);
   4205 	if (ds32p) {
   4206 		SCARG(&ua, buf) = NULL;
   4207 		netbsd32_to_msqid_ds(ds32p, &ds);
   4208 	} else
   4209 		SCARG(&ua, buf) = NULL;
   4210 	error = sys_msgctl(p, &ua, retval);
   4211 	if (error)
   4212 		return (error);
   4213 
   4214 	if (ds32p)
   4215 		netbsd32_from_msqid_ds(&ds, ds32p);
   4216 	return (0);
   4217 #else
   4218 	return (ENOSYS);
   4219 #endif
   4220 }
   4221 
   4222 int
   4223 compat_netbsd32_msgget(p, v, retval)
   4224 	struct proc *p;
   4225 	void *v;
   4226 	register_t *retval;
   4227 {
   4228 #if 0
   4229 	struct compat_netbsd32_msgget_args /* {
   4230 		syscallarg(netbsd32_key_t) key;
   4231 		syscallarg(int) msgflg;
   4232 	} */ *uap = v;
   4233 	struct sys_msgget_args ua;
   4234 
   4235 	NETBSD32TOX_UAP(key, key_t);
   4236 	NETBSD32TO64_UAP(msgflg);
   4237 	return (sys_msgget(p, &ua, retval));
   4238 #else
   4239 	return (ENOSYS);
   4240 #endif
   4241 }
   4242 
   4243 int
   4244 compat_netbsd32_msgsnd(p, v, retval)
   4245 	struct proc *p;
   4246 	void *v;
   4247 	register_t *retval;
   4248 {
   4249 #if 0
   4250 	struct compat_netbsd32_msgsnd_args /* {
   4251 		syscallarg(int) msqid;
   4252 		syscallarg(const netbsd32_voidp) msgp;
   4253 		syscallarg(netbsd32_size_t) msgsz;
   4254 		syscallarg(int) msgflg;
   4255 	} */ *uap = v;
   4256 	struct sys_msgsnd_args ua;
   4257 
   4258 	NETBSD32TO64_UAP(msqid);
   4259 	NETBSD32TOP_UAP(msgp, void);
   4260 	NETBSD32TOX_UAP(msgsz, size_t);
   4261 	NETBSD32TO64_UAP(msgflg);
   4262 	return (sys_msgsnd(p, &ua, retval));
   4263 #else
   4264 	return (ENOSYS);
   4265 #endif
   4266 }
   4267 
   4268 int
   4269 compat_netbsd32_msgrcv(p, v, retval)
   4270 	struct proc *p;
   4271 	void *v;
   4272 	register_t *retval;
   4273 {
   4274 #if 0
   4275 	struct compat_netbsd32_msgrcv_args /* {
   4276 		syscallarg(int) msqid;
   4277 		syscallarg(netbsd32_voidp) msgp;
   4278 		syscallarg(netbsd32_size_t) msgsz;
   4279 		syscallarg(netbsd32_long) msgtyp;
   4280 		syscallarg(int) msgflg;
   4281 	} */ *uap = v;
   4282 	struct sys_msgrcv_args ua;
   4283 	ssize_t rt;
   4284 	int error;
   4285 
   4286 	NETBSD32TO64_UAP(msqid);
   4287 	NETBSD32TOP_UAP(msgp, void);
   4288 	NETBSD32TOX_UAP(msgsz, size_t);
   4289 	NETBSD32TOX_UAP(msgtyp, long);
   4290 	NETBSD32TO64_UAP(msgflg);
   4291 	error = sys_msgrcv(p, &ua, (register_t *)&rt);
   4292 	*(netbsd32_ssize_t *)retval = rt;
   4293 	return (error);
   4294 #else
   4295 	return (ENOSYS);
   4296 #endif
   4297 }
   4298 
   4299 int
   4300 compat_netbsd32_shmat(p, v, retval)
   4301 	struct proc *p;
   4302 	void *v;
   4303 	register_t *retval;
   4304 {
   4305 #if 0
   4306 	struct compat_netbsd32_shmat_args /* {
   4307 		syscallarg(int) shmid;
   4308 		syscallarg(const netbsd32_voidp) shmaddr;
   4309 		syscallarg(int) shmflg;
   4310 	} */ *uap = v;
   4311 	struct sys_shmat_args ua;
   4312 	void *rt;
   4313 	int error;
   4314 
   4315 	NETBSD32TO64_UAP(shmid);
   4316 	NETBSD32TOP_UAP(shmaddr, void);
   4317 	NETBSD32TO64_UAP(shmflg);
   4318 	error = sys_shmat(p, &ua, (register_t *)&rt);
   4319 	*retval = (netbsd32_voidp)(u_long)rt;
   4320 	return (error);
   4321 #else
   4322 	return (ENOSYS);
   4323 #endif
   4324 }
   4325 
   4326 int
   4327 compat_netbsd32_shmctl(p, v, retval)
   4328 	struct proc *p;
   4329 	void *v;
   4330 	register_t *retval;
   4331 {
   4332 #if 0
   4333 	struct compat_netbsd32_shmctl_args /* {
   4334 		syscallarg(int) shmid;
   4335 		syscallarg(int) cmd;
   4336 		syscallarg(netbsd32_shmid_dsp_t) buf;
   4337 	} */ *uap = v;
   4338 	struct sys_shmctl_args ua;
   4339 	struct shmid_ds ds;
   4340 	struct netbsd32_shmid_ds *ds32p;
   4341 	int error;
   4342 
   4343 	NETBSD32TO64_UAP(shmid);
   4344 	NETBSD32TO64_UAP(cmd);
   4345 	ds32p = (struct netbsd32_shmid_ds *)(u_long)SCARG(uap, buf);
   4346 	if (ds32p) {
   4347 		SCARG(&ua, buf) = NULL;
   4348 		netbsd32_to_shmid_ds(ds32p, &ds);
   4349 	} else
   4350 		SCARG(&ua, buf) = NULL;
   4351 	error = sys_shmctl(p, &ua, retval);
   4352 	if (error)
   4353 		return (error);
   4354 
   4355 	if (ds32p)
   4356 		netbsd32_from_shmid_ds(&ds, ds32p);
   4357 	return (0);
   4358 #else
   4359 	return (ENOSYS);
   4360 #endif
   4361 }
   4362 
   4363 int
   4364 compat_netbsd32_shmdt(p, v, retval)
   4365 	struct proc *p;
   4366 	void *v;
   4367 	register_t *retval;
   4368 {
   4369 #if 0
   4370 	struct compat_netbsd32_shmdt_args /* {
   4371 		syscallarg(const netbsd32_voidp) shmaddr;
   4372 	} */ *uap = v;
   4373 	struct sys_shmdt_args ua;
   4374 
   4375 	NETBSD32TOP_UAP(shmaddr, const char);
   4376 	return (sys_shmdt(p, &ua, retval));
   4377 #else
   4378 	return (ENOSYS);
   4379 #endif
   4380 }
   4381 
   4382 int
   4383 compat_netbsd32_shmget(p, v, retval)
   4384 	struct proc *p;
   4385 	void *v;
   4386 	register_t *retval;
   4387 {
   4388 #if 0
   4389 	struct compat_netbsd32_shmget_args /* {
   4390 		syscallarg(netbsd32_key_t) key;
   4391 		syscallarg(netbsd32_size_t) size;
   4392 		syscallarg(int) shmflg;
   4393 	} */ *uap = v;
   4394 	struct sys_shmget_args ua;
   4395 
   4396 	NETBSD32TOX_UAP(key, key_t)
   4397 	NETBSD32TOX_UAP(size, size_t)
   4398 	NETBSD32TO64_UAP(shmflg);
   4399 	return (sys_shmget(p, &ua, retval));
   4400 #else
   4401 	return (ENOSYS);
   4402 #endif
   4403 }
   4404 
   4405 int
   4406 compat_netbsd32_clock_gettime(p, v, retval)
   4407 	struct proc *p;
   4408 	void *v;
   4409 	register_t *retval;
   4410 {
   4411 	struct compat_netbsd32_clock_gettime_args /* {
   4412 		syscallarg(netbsd32_clockid_t) clock_id;
   4413 		syscallarg(netbsd32_timespecp_t) tp;
   4414 	} */ *uap = v;
   4415 	clockid_t clock_id;
   4416 	struct timeval atv;
   4417 	struct timespec ats;
   4418 	struct netbsd32_timespec ts32;
   4419 
   4420 	clock_id = SCARG(uap, clock_id);
   4421 	if (clock_id != CLOCK_REALTIME)
   4422 		return (EINVAL);
   4423 
   4424 	microtime(&atv);
   4425 	TIMEVAL_TO_TIMESPEC(&atv,&ats);
   4426 	netbsd32_from_timespec(&ats, &ts32);
   4427 
   4428 	return copyout(&ts32, (caddr_t)(u_long)SCARG(uap, tp), sizeof(ts32));
   4429 }
   4430 
   4431 int
   4432 compat_netbsd32_clock_settime(p, v, retval)
   4433 	struct proc *p;
   4434 	void *v;
   4435 	register_t *retval;
   4436 {
   4437 	struct compat_netbsd32_clock_settime_args /* {
   4438 		syscallarg(netbsd32_clockid_t) clock_id;
   4439 		syscallarg(const netbsd32_timespecp_t) tp;
   4440 	} */ *uap = v;
   4441 	struct netbsd32_timespec ts32;
   4442 	clockid_t clock_id;
   4443 	struct timeval atv;
   4444 	struct timespec ats;
   4445 	int error;
   4446 
   4447 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   4448 		return (error);
   4449 
   4450 	clock_id = SCARG(uap, clock_id);
   4451 	if (clock_id != CLOCK_REALTIME)
   4452 		return (EINVAL);
   4453 
   4454 	if ((error = copyin((caddr_t)(u_long)SCARG(uap, tp), &ts32, sizeof(ts32))) != 0)
   4455 		return (error);
   4456 
   4457 	netbsd32_to_timespec(&ts32, &ats);
   4458 	TIMESPEC_TO_TIMEVAL(&atv,&ats);
   4459 	if ((error = settime(&atv)))
   4460 		return (error);
   4461 
   4462 	return 0;
   4463 }
   4464 
   4465 int
   4466 compat_netbsd32_clock_getres(p, v, retval)
   4467 	struct proc *p;
   4468 	void *v;
   4469 	register_t *retval;
   4470 {
   4471 	struct compat_netbsd32_clock_getres_args /* {
   4472 		syscallarg(netbsd32_clockid_t) clock_id;
   4473 		syscallarg(netbsd32_timespecp_t) tp;
   4474 	} */ *uap = v;
   4475 	struct netbsd32_timespec ts32;
   4476 	clockid_t clock_id;
   4477 	struct timespec ts;
   4478 	int error = 0;
   4479 
   4480 	clock_id = SCARG(uap, clock_id);
   4481 	if (clock_id != CLOCK_REALTIME)
   4482 		return (EINVAL);
   4483 
   4484 	if (SCARG(uap, tp)) {
   4485 		ts.tv_sec = 0;
   4486 		ts.tv_nsec = 1000000000 / hz;
   4487 
   4488 		netbsd32_from_timespec(&ts, &ts32);
   4489 		error = copyout(&ts, (caddr_t)(u_long)SCARG(uap, tp), sizeof(ts));
   4490 	}
   4491 
   4492 	return error;
   4493 }
   4494 
   4495 int
   4496 compat_netbsd32_nanosleep(p, v, retval)
   4497 	struct proc *p;
   4498 	void *v;
   4499 	register_t *retval;
   4500 {
   4501 	struct compat_netbsd32_nanosleep_args /* {
   4502 		syscallarg(const netbsd32_timespecp_t) rqtp;
   4503 		syscallarg(netbsd32_timespecp_t) rmtp;
   4504 	} */ *uap = v;
   4505 	static int nanowait;
   4506 	struct netbsd32_timespec ts32;
   4507 	struct timespec rqt;
   4508 	struct timespec rmt;
   4509 	struct timeval atv, utv;
   4510 	int error, s, timo;
   4511 
   4512 	error = copyin((caddr_t)(u_long)SCARG(uap, rqtp), (caddr_t)&ts32,
   4513 		       sizeof(ts32));
   4514 	if (error)
   4515 		return (error);
   4516 
   4517 	netbsd32_to_timespec(&ts32, &rqt);
   4518 	TIMESPEC_TO_TIMEVAL(&atv,&rqt)
   4519 	if (itimerfix(&atv))
   4520 		return (EINVAL);
   4521 
   4522 	s = splclock();
   4523 	timeradd(&atv,&time,&atv);
   4524 	timo = hzto(&atv);
   4525 	/*
   4526 	 * Avoid inadvertantly sleeping forever
   4527 	 */
   4528 	if (timo == 0)
   4529 		timo = 1;
   4530 	splx(s);
   4531 
   4532 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
   4533 	if (error == ERESTART)
   4534 		error = EINTR;
   4535 	if (error == EWOULDBLOCK)
   4536 		error = 0;
   4537 
   4538 	if (SCARG(uap, rmtp)) {
   4539 		int error;
   4540 
   4541 		s = splclock();
   4542 		utv = time;
   4543 		splx(s);
   4544 
   4545 		timersub(&atv, &utv, &utv);
   4546 		if (utv.tv_sec < 0)
   4547 			timerclear(&utv);
   4548 
   4549 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
   4550 		netbsd32_from_timespec(&rmt, &ts32);
   4551 		error = copyout((caddr_t)&ts32, (caddr_t)(u_long)SCARG(uap,rmtp),
   4552 			sizeof(ts32));
   4553 		if (error)
   4554 			return (error);
   4555 	}
   4556 
   4557 	return error;
   4558 }
   4559 
   4560 int
   4561 compat_netbsd32_fdatasync(p, v, retval)
   4562 	struct proc *p;
   4563 	void *v;
   4564 	register_t *retval;
   4565 {
   4566 	struct compat_netbsd32_fdatasync_args /* {
   4567 		syscallarg(int) fd;
   4568 	} */ *uap = v;
   4569 	struct sys_fdatasync_args ua;
   4570 
   4571 	NETBSD32TO64_UAP(fd);
   4572 
   4573 	return (sys_fdatasync(p, &ua, retval));
   4574 }
   4575 
   4576 int
   4577 compat_netbsd32___posix_rename(p, v, retval)
   4578 	struct proc *p;
   4579 	void *v;
   4580 	register_t *retval;
   4581 {
   4582 	struct compat_netbsd32___posix_rename_args /* {
   4583 		syscallarg(const netbsd32_charp) from;
   4584 		syscallarg(const netbsd32_charp) to;
   4585 	} */ *uap = v;
   4586 	struct sys___posix_rename_args ua;
   4587 
   4588 	NETBSD32TOP_UAP(from, const char *);
   4589 	NETBSD32TOP_UAP(to, const char *);
   4590 
   4591 	return (sys___posix_rename(p, &ua, retval));
   4592 }
   4593 
   4594 int
   4595 compat_netbsd32_swapctl(p, v, retval)
   4596 	struct proc *p;
   4597 	void *v;
   4598 	register_t *retval;
   4599 {
   4600 	struct compat_netbsd32_swapctl_args /* {
   4601 		syscallarg(int) cmd;
   4602 		syscallarg(const netbsd32_voidp) arg;
   4603 		syscallarg(int) misc;
   4604 	} */ *uap = v;
   4605 	struct sys_swapctl_args ua;
   4606 
   4607 	NETBSD32TO64_UAP(cmd);
   4608 	NETBSD32TOP_UAP(arg, const void);
   4609 	NETBSD32TO64_UAP(misc);
   4610 	return (sys_swapctl(p, &ua, retval));
   4611 }
   4612 
   4613 int
   4614 compat_netbsd32_getdents(p, v, retval)
   4615 	struct proc *p;
   4616 	void *v;
   4617 	register_t *retval;
   4618 {
   4619 	struct compat_netbsd32_getdents_args /* {
   4620 		syscallarg(int) fd;
   4621 		syscallarg(netbsd32_charp) buf;
   4622 		syscallarg(netbsd32_size_t) count;
   4623 	} */ *uap = v;
   4624 	struct file *fp;
   4625 	int error, done;
   4626 
   4627 	/* getvnode() will use the descriptor for us */
   4628 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   4629 		return (error);
   4630 	if ((fp->f_flag & FREAD) == 0) {
   4631 		error = EBADF;
   4632 		goto out;
   4633 	}
   4634 	error = vn_readdir(fp, (caddr_t)(u_long)SCARG(uap, buf), UIO_USERSPACE,
   4635 			SCARG(uap, count), &done, p, 0, 0);
   4636 	*retval = done;
   4637  out:
   4638 	FILE_UNUSE(fp);
   4639 	return (error);
   4640 }
   4641 
   4642 
   4643 int
   4644 compat_netbsd32_minherit(p, v, retval)
   4645 	struct proc *p;
   4646 	void *v;
   4647 	register_t *retval;
   4648 {
   4649 	struct compat_netbsd32_minherit_args /* {
   4650 		syscallarg(netbsd32_voidp) addr;
   4651 		syscallarg(netbsd32_size_t) len;
   4652 		syscallarg(int) inherit;
   4653 	} */ *uap = v;
   4654 	struct sys_minherit_args ua;
   4655 
   4656 	NETBSD32TOP_UAP(addr, void);
   4657 	NETBSD32TOX_UAP(len, size_t);
   4658 	NETBSD32TO64_UAP(inherit);
   4659 	return (sys_minherit(p, &ua, retval));
   4660 }
   4661 
   4662 int
   4663 compat_netbsd32_lchmod(p, v, retval)
   4664 	struct proc *p;
   4665 	void *v;
   4666 	register_t *retval;
   4667 {
   4668 	struct compat_netbsd32_lchmod_args /* {
   4669 		syscallarg(const netbsd32_charp) path;
   4670 		syscallarg(mode_t) mode;
   4671 	} */ *uap = v;
   4672 	struct sys_lchmod_args ua;
   4673 
   4674 	NETBSD32TOP_UAP(path, const char);
   4675 	NETBSD32TO64_UAP(mode);
   4676 	return (sys_lchmod(p, &ua, retval));
   4677 }
   4678 
   4679 int
   4680 compat_netbsd32_lchown(p, v, retval)
   4681 	struct proc *p;
   4682 	void *v;
   4683 	register_t *retval;
   4684 {
   4685 	struct compat_netbsd32_lchown_args /* {
   4686 		syscallarg(const netbsd32_charp) path;
   4687 		syscallarg(uid_t) uid;
   4688 		syscallarg(gid_t) gid;
   4689 	} */ *uap = v;
   4690 	struct sys_lchown_args ua;
   4691 
   4692 	NETBSD32TOP_UAP(path, const char);
   4693 	NETBSD32TO64_UAP(uid);
   4694 	NETBSD32TO64_UAP(gid);
   4695 	return (sys_lchown(p, &ua, retval));
   4696 }
   4697 
   4698 int
   4699 compat_netbsd32_lutimes(p, v, retval)
   4700 	struct proc *p;
   4701 	void *v;
   4702 	register_t *retval;
   4703 {
   4704 	struct compat_netbsd32_lutimes_args /* {
   4705 		syscallarg(const netbsd32_charp) path;
   4706 		syscallarg(const netbsd32_timevalp_t) tptr;
   4707 	} */ *uap = v;
   4708 	int error;
   4709 	struct nameidata nd;
   4710 
   4711 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, (caddr_t)(u_long)SCARG(uap, path), p);
   4712 	if ((error = namei(&nd)) != 0)
   4713 		return (error);
   4714 
   4715 	error = change_utimes32(nd.ni_vp, (struct timeval *)(u_long)SCARG(uap, tptr), p);
   4716 
   4717 	vrele(nd.ni_vp);
   4718 	return (error);
   4719 }
   4720 
   4721 
   4722 int
   4723 compat_netbsd32___msync13(p, v, retval)
   4724 	struct proc *p;
   4725 	void *v;
   4726 	register_t *retval;
   4727 {
   4728 	struct compat_netbsd32___msync13_args /* {
   4729 		syscallarg(netbsd32_voidp) addr;
   4730 		syscallarg(netbsd32_size_t) len;
   4731 		syscallarg(int) flags;
   4732 	} */ *uap = v;
   4733 	struct sys___msync13_args ua;
   4734 
   4735 	NETBSD32TOP_UAP(addr, void);
   4736 	NETBSD32TOX_UAP(len, size_t);
   4737 	NETBSD32TO64_UAP(flags);
   4738 	return (sys___msync13(p, &ua, retval));
   4739 }
   4740 
   4741 int
   4742 compat_netbsd32___stat13(p, v, retval)
   4743 	struct proc *p;
   4744 	void *v;
   4745 	register_t *retval;
   4746 {
   4747 	struct compat_netbsd32___stat13_args /* {
   4748 		syscallarg(const netbsd32_charp) path;
   4749 		syscallarg(netbsd32_statp_t) ub;
   4750 	} */ *uap = v;
   4751 	struct netbsd32_stat sb32;
   4752 	struct stat sb;
   4753 	int error;
   4754 	struct nameidata nd;
   4755 
   4756 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   4757 	    (caddr_t)(u_long)SCARG(uap, path), p);
   4758 	if ((error = namei(&nd)) != 0)
   4759 		return (error);
   4760 	error = vn_stat(nd.ni_vp, &sb, p);
   4761 	vput(nd.ni_vp);
   4762 	if (error)
   4763 		return (error);
   4764 	netbsd32_from___stat13(&sb, &sb32);
   4765 	error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof(sb32));
   4766 	return (error);
   4767 }
   4768 
   4769 int
   4770 compat_netbsd32___fstat13(p, v, retval)
   4771 	struct proc *p;
   4772 	void *v;
   4773 	register_t *retval;
   4774 {
   4775 	struct compat_netbsd32___fstat13_args /* {
   4776 		syscallarg(int) fd;
   4777 		syscallarg(netbsd32_statp_t) sb;
   4778 	} */ *uap = v;
   4779 	int fd = SCARG(uap, fd);
   4780 	register struct filedesc *fdp = p->p_fd;
   4781 	register struct file *fp;
   4782 	struct netbsd32_stat sb32;
   4783 	struct stat ub;
   4784 	int error = 0;
   4785 
   4786 	if ((u_int)fd >= fdp->fd_nfiles ||
   4787 	    (fp = fdp->fd_ofiles[fd]) == NULL)
   4788 		return (EBADF);
   4789 	switch (fp->f_type) {
   4790 
   4791 	case DTYPE_VNODE:
   4792 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
   4793 		break;
   4794 
   4795 	case DTYPE_SOCKET:
   4796 		error = soo_stat((struct socket *)fp->f_data, &ub);
   4797 		break;
   4798 
   4799 	default:
   4800 		panic("fstat");
   4801 		/*NOTREACHED*/
   4802 	}
   4803 	if (error == 0) {
   4804 		netbsd32_from___stat13(&ub, &sb32);
   4805 		error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, sb), sizeof(sb32));
   4806 	}
   4807 	return (error);
   4808 }
   4809 
   4810 int
   4811 compat_netbsd32___lstat13(p, v, retval)
   4812 	struct proc *p;
   4813 	void *v;
   4814 	register_t *retval;
   4815 {
   4816 	struct compat_netbsd32___lstat13_args /* {
   4817 		syscallarg(const netbsd32_charp) path;
   4818 		syscallarg(netbsd32_statp_t) ub;
   4819 	} */ *uap = v;
   4820 	struct netbsd32_stat sb32;
   4821 	struct stat sb;
   4822 	int error;
   4823 	struct nameidata nd;
   4824 
   4825 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   4826 	    (caddr_t)(u_long)SCARG(uap, path), p);
   4827 	if ((error = namei(&nd)) != 0)
   4828 		return (error);
   4829 	error = vn_stat(nd.ni_vp, &sb, p);
   4830 	vput(nd.ni_vp);
   4831 	if (error)
   4832 		return (error);
   4833 	netbsd32_from___stat13(&sb, &sb32);
   4834 	error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof(sb32));
   4835 	return (error);
   4836 }
   4837 
   4838 int
   4839 compat_netbsd32___sigaltstack14(p, v, retval)
   4840 	struct proc *p;
   4841 	void *v;
   4842 	register_t *retval;
   4843 {
   4844 	struct compat_netbsd32___sigaltstack14_args /* {
   4845 		syscallarg(const netbsd32_sigaltstackp_t) nss;
   4846 		syscallarg(netbsd32_sigaltstackp_t) oss;
   4847 	} */ *uap = v;
   4848 	struct netbsd32_sigaltstack s32;
   4849 	struct sigaltstack nss, oss;
   4850 	int error;
   4851 
   4852 	if (SCARG(uap, nss)) {
   4853 		error = copyin((caddr_t)(u_long)SCARG(uap, nss), &s32, sizeof(s32));
   4854 		if (error)
   4855 			return (error);
   4856 		nss.ss_sp = (void *)(u_long)s32.ss_sp;
   4857 		nss.ss_size = (size_t)s32.ss_size;
   4858 		nss.ss_flags = s32.ss_flags;
   4859 	}
   4860 	error = sigaltstack1(p,
   4861 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
   4862 	if (error)
   4863 		return (error);
   4864 	if (SCARG(uap, oss)) {
   4865 		s32.ss_sp = (netbsd32_voidp)(u_long)oss.ss_sp;
   4866 		s32.ss_size = (netbsd32_size_t)oss.ss_size;
   4867 		s32.ss_flags = oss.ss_flags;
   4868 		error = copyout(&s32, (caddr_t)(u_long)SCARG(uap, oss), sizeof(s32));
   4869 		if (error)
   4870 			return (error);
   4871 	}
   4872 	return (0);
   4873 }
   4874 
   4875 int
   4876 compat_netbsd32___posix_chown(p, v, retval)
   4877 	struct proc *p;
   4878 	void *v;
   4879 	register_t *retval;
   4880 {
   4881 	struct compat_netbsd32___posix_chown_args /* {
   4882 		syscallarg(const netbsd32_charp) path;
   4883 		syscallarg(uid_t) uid;
   4884 		syscallarg(gid_t) gid;
   4885 	} */ *uap = v;
   4886 	struct sys___posix_chown_args ua;
   4887 
   4888 	NETBSD32TOP_UAP(path, const char);
   4889 	NETBSD32TO64_UAP(uid);
   4890 	NETBSD32TO64_UAP(gid);
   4891 	return (sys___posix_chown(p, &ua, retval));
   4892 }
   4893 
   4894 int
   4895 compat_netbsd32___posix_fchown(p, v, retval)
   4896 	struct proc *p;
   4897 	void *v;
   4898 	register_t *retval;
   4899 {
   4900 	struct compat_netbsd32___posix_fchown_args /* {
   4901 		syscallarg(int) fd;
   4902 		syscallarg(uid_t) uid;
   4903 		syscallarg(gid_t) gid;
   4904 	} */ *uap = v;
   4905 	struct sys___posix_fchown_args ua;
   4906 
   4907 	NETBSD32TO64_UAP(fd);
   4908 	NETBSD32TO64_UAP(uid);
   4909 	NETBSD32TO64_UAP(gid);
   4910 	return (sys___posix_fchown(p, &ua, retval));
   4911 }
   4912 
   4913 int
   4914 compat_netbsd32___posix_lchown(p, v, retval)
   4915 	struct proc *p;
   4916 	void *v;
   4917 	register_t *retval;
   4918 {
   4919 	struct compat_netbsd32___posix_lchown_args /* {
   4920 		syscallarg(const netbsd32_charp) path;
   4921 		syscallarg(uid_t) uid;
   4922 		syscallarg(gid_t) gid;
   4923 	} */ *uap = v;
   4924 	struct sys___posix_lchown_args ua;
   4925 
   4926 	NETBSD32TOP_UAP(path, const char);
   4927 	NETBSD32TO64_UAP(uid);
   4928 	NETBSD32TO64_UAP(gid);
   4929 	return (sys___posix_lchown(p, &ua, retval));
   4930 }
   4931 
   4932 int
   4933 compat_netbsd32_getsid(p, v, retval)
   4934 	struct proc *p;
   4935 	void *v;
   4936 	register_t *retval;
   4937 {
   4938 	struct compat_netbsd32_getsid_args /* {
   4939 		syscallarg(pid_t) pid;
   4940 	} */ *uap = v;
   4941 	struct sys_getsid_args ua;
   4942 
   4943 	NETBSD32TO64_UAP(pid);
   4944 	return (sys_getsid(p, &ua, retval));
   4945 }
   4946 
   4947 int
   4948 compat_netbsd32_fktrace(p, v, retval)
   4949 	struct proc *p;
   4950 	void *v;
   4951 	register_t *retval;
   4952 {
   4953 	struct compat_netbsd32_fktrace_args /* {
   4954 		syscallarg(const int) fd;
   4955 		syscallarg(int) ops;
   4956 		syscallarg(int) facs;
   4957 		syscallarg(int) pid;
   4958 	} */ *uap = v;
   4959 	struct sys_fktrace_args ua;
   4960 
   4961 	NETBSD32TO64_UAP(fd);
   4962 	NETBSD32TO64_UAP(ops);
   4963 	NETBSD32TO64_UAP(facs);
   4964 	NETBSD32TO64_UAP(pid);
   4965 	return (sys_fktrace(p, &ua, retval));
   4966 }
   4967 
   4968 int
   4969 compat_netbsd32_preadv(p, v, retval)
   4970 	struct proc *p;
   4971 	void *v;
   4972 	register_t *retval;
   4973 {
   4974 	struct compat_netbsd32_preadv_args /* {
   4975 		syscallarg(int) fd;
   4976 		syscallarg(const netbsd32_iovecp_t) iovp;
   4977 		syscallarg(int) iovcnt;
   4978 		syscallarg(int) pad;
   4979 		syscallarg(off_t) offset;
   4980 	} */ *uap = v;
   4981 	struct filedesc *fdp = p->p_fd;
   4982 	struct file *fp;
   4983 	struct vnode *vp;
   4984 	off_t offset;
   4985 	int error, fd = SCARG(uap, fd);
   4986 
   4987 	if ((u_int)fd >= fdp->fd_nfiles ||
   4988 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   4989 	    (fp->f_flag & FREAD) == 0)
   4990 		return (EBADF);
   4991 
   4992 	vp = (struct vnode *)fp->f_data;
   4993 	if (fp->f_type != DTYPE_VNODE
   4994 	    || vp->v_type == VFIFO)
   4995 		return (ESPIPE);
   4996 
   4997 	offset = SCARG(uap, offset);
   4998 
   4999 	/*
   5000 	 * XXX This works because no file systems actually
   5001 	 * XXX take any action on the seek operation.
   5002 	 */
   5003 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   5004 		return (error);
   5005 
   5006 	return (dofilereadv32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp), SCARG(uap, iovcnt),
   5007 	    &offset, 0, retval));
   5008 }
   5009 
   5010 int
   5011 compat_netbsd32_pwritev(p, v, retval)
   5012 	struct proc *p;
   5013 	void *v;
   5014 	register_t *retval;
   5015 {
   5016 	struct compat_netbsd32_pwritev_args /* {
   5017 		syscallarg(int) fd;
   5018 		syscallarg(const netbsd32_iovecp_t) iovp;
   5019 		syscallarg(int) iovcnt;
   5020 		syscallarg(int) pad;
   5021 		syscallarg(off_t) offset;
   5022 	} */ *uap = v;
   5023 	struct filedesc *fdp = p->p_fd;
   5024 	struct file *fp;
   5025 	struct vnode *vp;
   5026 	off_t offset;
   5027 	int error, fd = SCARG(uap, fd);
   5028 
   5029 	if ((u_int)fd >= fdp->fd_nfiles ||
   5030 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   5031 	    (fp->f_flag & FWRITE) == 0)
   5032 		return (EBADF);
   5033 
   5034 	vp = (struct vnode *)fp->f_data;
   5035 	if (fp->f_type != DTYPE_VNODE
   5036 	    || vp->v_type == VFIFO)
   5037 		return (ESPIPE);
   5038 
   5039 	offset = SCARG(uap, offset);
   5040 
   5041 	/*
   5042 	 * XXX This works because no file systems actually
   5043 	 * XXX take any action on the seek operation.
   5044 	 */
   5045 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   5046 		return (error);
   5047 
   5048 	return (dofilewritev32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp), SCARG(uap, iovcnt),
   5049 	    &offset, 0, retval));
   5050 }
   5051 
   5052 int
   5053 compat_13_compat_netbsd32_sigprocmask(p, v, retval)
   5054 	register struct proc *p;
   5055 	void *v;
   5056 	register_t *retval;
   5057 {
   5058 	struct compat_13_compat_netbsd32_sigprocmask_args /* {
   5059 		syscallarg(int) how;
   5060 		syscallarg(int) mask;
   5061 	} */ *uap = v;
   5062 	sigset13_t ness, oess;
   5063 	sigset_t nbss, obss;
   5064 	int error;
   5065 
   5066 	ness = SCARG(uap, mask);
   5067 	native_sigset13_to_sigset(&ness, &nbss);
   5068 	error = sigprocmask1(p, SCARG(uap, how), &nbss, &obss);
   5069 	if (error)
   5070 		return (error);
   5071 	native_sigset_to_sigset13(&obss, &oess);
   5072 	*retval = oess;
   5073 	return (0);
   5074 }
   5075 
   5076 int
   5077 compat_13_compat_netbsd32_sigsuspend(p, v, retval)
   5078 	register struct proc *p;
   5079 	void *v;
   5080 	register_t *retval;
   5081 {
   5082 	struct compat_13_compat_netbsd32_sigsuspend_args /* {
   5083 		syscallarg(sigset13_t) mask;
   5084 	} */ *uap = v;
   5085 	sigset13_t ess;
   5086 	sigset_t bss;
   5087 
   5088 	ess = SCARG(uap, mask);
   5089 	native_sigset13_to_sigset(&ess, &bss);
   5090 	return (sigsuspend1(p, &bss));
   5091 }
   5092