Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_netbsd.c revision 1.11
      1 /*	$NetBSD: netbsd32_netbsd.c,v 1.11 1999/03/25 16:58:40 mrg 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 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
   1106 		return (error);
   1107 	auio.uio_iov = (struct iovec *)(u_long)mp->msg_iov;
   1108 	auio.uio_iovcnt = mp->msg_iovlen;
   1109 	auio.uio_segflg = UIO_USERSPACE;
   1110 	auio.uio_rw = UIO_READ;
   1111 	auio.uio_procp = p;
   1112 	auio.uio_offset = 0;			/* XXX */
   1113 	auio.uio_resid = 0;
   1114 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
   1115 #if 0
   1116 		/* cannot happen iov_len is unsigned */
   1117 		if (iov->iov_len < 0)
   1118 			return (EINVAL);
   1119 #endif
   1120 		/*
   1121 		 * Reads return ssize_t because -1 is returned on error.
   1122 		 * Therefore we must restrict the length to SSIZE_MAX to
   1123 		 * avoid garbage return values.
   1124 		 */
   1125 		auio.uio_resid += iov->iov_len;
   1126 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX)
   1127 			return (EINVAL);
   1128 	}
   1129 #ifdef KTRACE
   1130 	if (KTRPOINT(p, KTR_GENIO)) {
   1131 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
   1132 
   1133 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   1134 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   1135 	}
   1136 #endif
   1137 	len = auio.uio_resid;
   1138 	so = (struct socket *)fp->f_data;
   1139 	error = (*so->so_receive)(so, &from, &auio, NULL,
   1140 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
   1141 	if (error) {
   1142 		if (auio.uio_resid != len && (error == ERESTART ||
   1143 		    error == EINTR || error == EWOULDBLOCK))
   1144 			error = 0;
   1145 	}
   1146 #ifdef KTRACE
   1147 	if (ktriov != NULL) {
   1148 		if (error == 0)
   1149 			ktrgenio(p->p_tracep, s, UIO_READ,
   1150 				ktriov, len - auio.uio_resid, error);
   1151 		FREE(ktriov, M_TEMP);
   1152 	}
   1153 #endif
   1154 	if (error)
   1155 		goto out;
   1156 	*retsize = len - auio.uio_resid;
   1157 	if (mp->msg_name) {
   1158 		len = mp->msg_namelen;
   1159 		if (len <= 0 || from == 0)
   1160 			len = 0;
   1161 		else {
   1162 #ifdef COMPAT_OLDSOCK
   1163 			if (mp->msg_flags & MSG_COMPAT)
   1164 				mtod(from, struct osockaddr *)->sa_family =
   1165 				    mtod(from, struct sockaddr *)->sa_family;
   1166 #endif
   1167 			if (len > from->m_len)
   1168 				len = from->m_len;
   1169 			/* else if len < from->m_len ??? */
   1170 			error = copyout(mtod(from, caddr_t),
   1171 					(caddr_t)(u_long)mp->msg_name, (unsigned)len);
   1172 			if (error)
   1173 				goto out;
   1174 		}
   1175 		mp->msg_namelen = len;
   1176 		if (namelenp &&
   1177 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
   1178 #ifdef COMPAT_OLDSOCK
   1179 			if (mp->msg_flags & MSG_COMPAT)
   1180 				error = 0;	/* old recvfrom didn't check */
   1181 			else
   1182 #endif
   1183 			goto out;
   1184 		}
   1185 	}
   1186 	if (mp->msg_control) {
   1187 #ifdef COMPAT_OLDSOCK
   1188 		/*
   1189 		 * We assume that old recvmsg calls won't receive access
   1190 		 * rights and other control info, esp. as control info
   1191 		 * is always optional and those options didn't exist in 4.3.
   1192 		 * If we receive rights, trim the cmsghdr; anything else
   1193 		 * is tossed.
   1194 		 */
   1195 		if (control && mp->msg_flags & MSG_COMPAT) {
   1196 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
   1197 			    SOL_SOCKET ||
   1198 			    mtod(control, struct cmsghdr *)->cmsg_type !=
   1199 			    SCM_RIGHTS) {
   1200 				mp->msg_controllen = 0;
   1201 				goto out;
   1202 			}
   1203 			control->m_len -= sizeof(struct cmsghdr);
   1204 			control->m_data += sizeof(struct cmsghdr);
   1205 		}
   1206 #endif
   1207 		len = mp->msg_controllen;
   1208 		if (len <= 0 || control == 0)
   1209 			len = 0;
   1210 		else {
   1211 			struct mbuf *m = control;
   1212 			caddr_t p = (caddr_t)(u_long)mp->msg_control;
   1213 
   1214 			do {
   1215 				i = m->m_len;
   1216 				if (len < i) {
   1217 					mp->msg_flags |= MSG_CTRUNC;
   1218 					i = len;
   1219 				}
   1220 				error = copyout(mtod(m, caddr_t), p,
   1221 				    (unsigned)i);
   1222 				if (m->m_next)
   1223 					i = ALIGN(i);
   1224 				p += i;
   1225 				len -= i;
   1226 				if (error != 0 || len <= 0)
   1227 					break;
   1228 			} while ((m = m->m_next) != NULL);
   1229 			len = p - (caddr_t)(u_long)mp->msg_control;
   1230 		}
   1231 		mp->msg_controllen = len;
   1232 	}
   1233 out:
   1234 	if (from)
   1235 		m_freem(from);
   1236 	if (control)
   1237 		m_freem(control);
   1238 	return (error);
   1239 }
   1240 
   1241 
   1242 int
   1243 compat_netbsd32_sendmsg(p, v, retval)
   1244 	struct proc *p;
   1245 	void *v;
   1246 	register_t *retval;
   1247 {
   1248 	struct compat_netbsd32_sendmsg_args /* {
   1249 		syscallarg(int) s;
   1250 		syscallarg(const netbsd32_msghdrp_t) msg;
   1251 		syscallarg(int) flags;
   1252 	} */ *uap = v;
   1253 	struct msghdr msg;
   1254 	struct netbsd32_msghdr msg32;
   1255 	struct iovec aiov[UIO_SMALLIOV], *iov;
   1256 	int error;
   1257 
   1258 	error = copyin((caddr_t)(u_long)SCARG(uap, msg),
   1259 		       (caddr_t)&msg32, sizeof(msg32));
   1260 	if (error)
   1261 		return (error);
   1262 	netbsd32_to_msghdr(&msg32, &msg);
   1263 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
   1264 		if ((u_int)msg.msg_iovlen > IOV_MAX)
   1265 			return (EMSGSIZE);
   1266 		MALLOC(iov, struct iovec *,
   1267 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
   1268 		       M_WAITOK);
   1269 	} else if ((u_int)msg.msg_iovlen > 0)
   1270 		iov = aiov;
   1271 	else
   1272 		return (EMSGSIZE);
   1273 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
   1274 				   iov, msg.msg_iovlen);
   1275 	if (error)
   1276 		goto done;
   1277 	msg.msg_iov = iov;
   1278 #ifdef COMPAT_OLDSOCK
   1279 	msg.msg_flags = 0;
   1280 #endif
   1281 	/* Luckily we can use this directly */
   1282 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
   1283 done:
   1284 	if (iov != aiov)
   1285 		FREE(iov, M_IOV);
   1286 	return (error);
   1287 }
   1288 
   1289 int
   1290 compat_netbsd32_recvfrom(p, v, retval)
   1291 	struct proc *p;
   1292 	void *v;
   1293 	register_t *retval;
   1294 {
   1295 	struct compat_netbsd32_recvfrom_args /* {
   1296 		syscallarg(int) s;
   1297 		syscallarg(netbsd32_voidp) buf;
   1298 		syscallarg(netbsd32_size_t) len;
   1299 		syscallarg(int) flags;
   1300 		syscallarg(netbsd32_sockaddrp_t) from;
   1301 		syscallarg(netbsd32_intp) fromlenaddr;
   1302 	} */ *uap = v;
   1303 	struct netbsd32_msghdr msg;
   1304 	struct iovec aiov;
   1305 	int error;
   1306 
   1307 	if (SCARG(uap, fromlenaddr)) {
   1308 		error = copyin((caddr_t)(u_long)SCARG(uap, fromlenaddr),
   1309 			       (caddr_t)&msg.msg_namelen,
   1310 			       sizeof(msg.msg_namelen));
   1311 		if (error)
   1312 			return (error);
   1313 	} else
   1314 		msg.msg_namelen = 0;
   1315 	msg.msg_name = SCARG(uap, from);
   1316 	msg.msg_iov = NULL; /* We can't store a real pointer here */
   1317 	msg.msg_iovlen = 1;
   1318 	aiov.iov_base = (caddr_t)(u_long)SCARG(uap, buf);
   1319 	aiov.iov_len = (u_long)SCARG(uap, len);
   1320 	msg.msg_control = 0;
   1321 	msg.msg_flags = SCARG(uap, flags);
   1322 	return (recvit32(p, SCARG(uap, s), &msg, &aiov,
   1323 		       (caddr_t)(u_long)SCARG(uap, fromlenaddr), retval));
   1324 }
   1325 
   1326 int
   1327 compat_netbsd32_sendto(p, v, retval)
   1328 	struct proc *p;
   1329 	void *v;
   1330 	register_t *retval;
   1331 {
   1332 	struct compat_netbsd32_sendto_args /* {
   1333 		syscallarg(int) s;
   1334 		syscallarg(const netbsd32_voidp) buf;
   1335 		syscallarg(netbsd32_size_t) len;
   1336 		syscallarg(int) flags;
   1337 		syscallarg(const netbsd32_sockaddrp_t) to;
   1338 		syscallarg(int) tolen;
   1339 	} */ *uap = v;
   1340 	struct msghdr msg;
   1341 	struct iovec aiov;
   1342 
   1343 	msg.msg_name = (caddr_t)(u_long)SCARG(uap, to);		/* XXX kills const */
   1344 	msg.msg_namelen = SCARG(uap, tolen);
   1345 	msg.msg_iov = &aiov;
   1346 	msg.msg_iovlen = 1;
   1347 	msg.msg_control = 0;
   1348 #ifdef COMPAT_OLDSOCK
   1349 	msg.msg_flags = 0;
   1350 #endif
   1351 	aiov.iov_base = (char *)(u_long)SCARG(uap, buf);	/* XXX kills const */
   1352 	aiov.iov_len = SCARG(uap, len);
   1353 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
   1354 }
   1355 
   1356 int
   1357 compat_netbsd32_accept(p, v, retval)
   1358 	struct proc *p;
   1359 	void *v;
   1360 	register_t *retval;
   1361 {
   1362 	struct compat_netbsd32_accept_args /* {
   1363 		syscallarg(int) s;
   1364 		syscallarg(netbsd32_sockaddrp_t) name;
   1365 		syscallarg(netbsd32_intp) anamelen;
   1366 	} */ *uap = v;
   1367 	struct sys_accept_args ua;
   1368 
   1369 	NETBSD32TO64_UAP(s);
   1370 	NETBSD32TOP_UAP(name, struct sockaddr);
   1371 	NETBSD32TOP_UAP(anamelen, int);
   1372 	return (sys_accept(p, &ua, retval));
   1373 }
   1374 
   1375 int
   1376 compat_netbsd32_getpeername(p, v, retval)
   1377 	struct proc *p;
   1378 	void *v;
   1379 	register_t *retval;
   1380 {
   1381 	struct compat_netbsd32_getpeername_args /* {
   1382 		syscallarg(int) fdes;
   1383 		syscallarg(netbsd32_sockaddrp_t) asa;
   1384 		syscallarg(netbsd32_intp) alen;
   1385 	} */ *uap = v;
   1386 	struct sys_getpeername_args ua;
   1387 
   1388 	NETBSD32TO64_UAP(fdes);
   1389 	NETBSD32TOP_UAP(asa, struct sockaddr);
   1390 	NETBSD32TOP_UAP(alen, int);
   1391 /* NB: do the protocol specific sockaddrs need to be converted? */
   1392 	return (sys_getpeername(p, &ua, retval));
   1393 }
   1394 
   1395 int
   1396 compat_netbsd32_getsockname(p, v, retval)
   1397 	struct proc *p;
   1398 	void *v;
   1399 	register_t *retval;
   1400 {
   1401 	struct compat_netbsd32_getsockname_args /* {
   1402 		syscallarg(int) fdes;
   1403 		syscallarg(netbsd32_sockaddrp_t) asa;
   1404 		syscallarg(netbsd32_intp) alen;
   1405 	} */ *uap = v;
   1406 	struct sys_getsockname_args ua;
   1407 
   1408 	NETBSD32TO64_UAP(fdes);
   1409 	NETBSD32TOP_UAP(asa, struct sockaddr);
   1410 	NETBSD32TOP_UAP(alen, int);
   1411 	return (sys_getsockname(p, &ua, retval));
   1412 }
   1413 
   1414 int
   1415 compat_netbsd32_access(p, v, retval)
   1416 	struct proc *p;
   1417 	void *v;
   1418 	register_t *retval;
   1419 {
   1420 	struct compat_netbsd32_access_args /* {
   1421 		syscallarg(const netbsd32_charp) path;
   1422 		syscallarg(int) flags;
   1423 	} */ *uap = v;
   1424 	struct sys_access_args ua;
   1425 	caddr_t sg;
   1426 
   1427 	NETBSD32TOP_UAP(path, const char);
   1428 	NETBSD32TO64_UAP(flags);
   1429 	sg = stackgap_init(p->p_emul);
   1430 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1431 
   1432 	return (sys_access(p, &ua, retval));
   1433 }
   1434 
   1435 int
   1436 compat_netbsd32_chflags(p, v, retval)
   1437 	struct proc *p;
   1438 	void *v;
   1439 	register_t *retval;
   1440 {
   1441 	struct compat_netbsd32_chflags_args /* {
   1442 		syscallarg(const netbsd32_charp) path;
   1443 		syscallarg(netbsd32_u_long) flags;
   1444 	} */ *uap = v;
   1445 	struct sys_chflags_args ua;
   1446 
   1447 	NETBSD32TOP_UAP(path, const char);
   1448 	NETBSD32TO64_UAP(flags);
   1449 
   1450 	return (sys_chflags(p, &ua, retval));
   1451 }
   1452 
   1453 int
   1454 compat_netbsd32_fchflags(p, v, retval)
   1455 	struct proc *p;
   1456 	void *v;
   1457 	register_t *retval;
   1458 {
   1459 	struct compat_netbsd32_fchflags_args /* {
   1460 		syscallarg(int) fd;
   1461 		syscallarg(netbsd32_u_long) flags;
   1462 	} */ *uap = v;
   1463 	struct sys_fchflags_args ua;
   1464 
   1465 	NETBSD32TO64_UAP(fd);
   1466 	NETBSD32TO64_UAP(flags);
   1467 
   1468 	return (sys_fchflags(p, &ua, retval));
   1469 }
   1470 
   1471 int
   1472 compat_netbsd32_kill(p, v, retval)
   1473 	struct proc *p;
   1474 	void *v;
   1475 	register_t *retval;
   1476 {
   1477 	struct compat_netbsd32_kill_args /* {
   1478 		syscallarg(int) pid;
   1479 		syscallarg(int) signum;
   1480 	} */ *uap = v;
   1481 	struct sys_kill_args ua;
   1482 
   1483 	NETBSD32TO64_UAP(pid);
   1484 	NETBSD32TO64_UAP(signum);
   1485 
   1486 	return (sys_kill(p, &ua, retval));
   1487 }
   1488 
   1489 int
   1490 compat_netbsd32_dup(p, v, retval)
   1491 	struct proc *p;
   1492 	void *v;
   1493 	register_t *retval;
   1494 {
   1495 	struct compat_netbsd32_dup_args /* {
   1496 		syscallarg(int) fd;
   1497 	} */ *uap = v;
   1498 	struct sys_dup_args ua;
   1499 
   1500 	NETBSD32TO64_UAP(fd);
   1501 
   1502 	return (sys_dup(p, &ua, retval));
   1503 }
   1504 
   1505 int
   1506 compat_netbsd32_profil(p, v, retval)
   1507 	struct proc *p;
   1508 	void *v;
   1509 	register_t *retval;
   1510 {
   1511 	struct compat_netbsd32_profil_args /* {
   1512 		syscallarg(netbsd32_caddr_t) samples;
   1513 		syscallarg(netbsd32_size_t) size;
   1514 		syscallarg(netbsd32_u_long) offset;
   1515 		syscallarg(u_int) scale;
   1516 	} */ *uap = v;
   1517 	struct sys_profil_args ua;
   1518 
   1519 	NETBSD32TOX64_UAP(samples, caddr_t);
   1520 	NETBSD32TOX_UAP(size, size_t);
   1521 	NETBSD32TOX_UAP(offset, u_long);
   1522 	NETBSD32TO64_UAP(scale);
   1523 	return (sys_profil(p, &ua, retval));
   1524 }
   1525 
   1526 int
   1527 compat_netbsd32_ktrace(p, v, retval)
   1528 	struct proc *p;
   1529 	void *v;
   1530 	register_t *retval;
   1531 {
   1532 	struct compat_netbsd32_ktrace_args /* {
   1533 		syscallarg(const netbsd32_charp) fname;
   1534 		syscallarg(int) ops;
   1535 		syscallarg(int) facs;
   1536 		syscallarg(int) pid;
   1537 	} */ *uap = v;
   1538 	struct sys_ktrace_args ua;
   1539 
   1540 	NETBSD32TOP_UAP(fname, const char);
   1541 	NETBSD32TO64_UAP(ops);
   1542 	NETBSD32TO64_UAP(facs);
   1543 	NETBSD32TO64_UAP(pid);
   1544 	return (sys_ktrace(p, &ua, retval));
   1545 }
   1546 
   1547 int
   1548 compat_netbsd32_sigaction(p, v, retval)
   1549 	struct proc *p;
   1550 	void *v;
   1551 	register_t *retval;
   1552 {
   1553 	struct compat_netbsd32_sigaction_args /* {
   1554 		syscallarg(int) signum;
   1555 		syscallarg(const netbsd32_sigactionp_t) nsa;
   1556 		syscallarg(netbsd32_sigactionp_t) osa;
   1557 	} */ *uap = v;
   1558 	struct sigaction nsa, osa;
   1559 	struct netbsd32_sigaction *sa32p, sa32;
   1560 	int error;
   1561 
   1562 	if (SCARG(uap, nsa)) {
   1563 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, nsa);
   1564 		if (copyin(sa32p, &sa32, sizeof(sa32)))
   1565 			return EFAULT;
   1566 		nsa.sa_handler = (void *)(u_long)sa32.sa_handler;
   1567 		nsa.sa_mask = sa32.sa_mask;
   1568 		nsa.sa_flags = sa32.sa_flags;
   1569 	}
   1570 	error = sigaction1(p, SCARG(uap, signum),
   1571 			   SCARG(uap, nsa) ? &nsa : 0,
   1572 			   SCARG(uap, osa) ? &osa : 0);
   1573 
   1574 	if (error)
   1575 		return (error);
   1576 
   1577 	if (SCARG(uap, osa)) {
   1578 		sa32.sa_handler = (netbsd32_sigactionp_t)(u_long)osa.sa_handler;
   1579 		sa32.sa_mask = osa.sa_mask;
   1580 		sa32.sa_flags = osa.sa_flags;
   1581 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, osa);
   1582 		if (copyout(&sa32, sa32p, sizeof(sa32)))
   1583 			return EFAULT;
   1584 	}
   1585 
   1586 	return (0);
   1587 }
   1588 
   1589 int
   1590 compat_netbsd32___getlogin(p, v, retval)
   1591 	struct proc *p;
   1592 	void *v;
   1593 	register_t *retval;
   1594 {
   1595 	struct compat_netbsd32___getlogin_args /* {
   1596 		syscallarg(netbsd32_charp) namebuf;
   1597 		syscallarg(u_int) namelen;
   1598 	} */ *uap = v;
   1599 	struct sys___getlogin_args ua;
   1600 
   1601 	NETBSD32TOP_UAP(namebuf, char);
   1602 	NETBSD32TO64_UAP(namelen);
   1603 	return (sys___getlogin(p, &ua, retval));
   1604 }
   1605 
   1606 int
   1607 compat_netbsd32_setlogin(p, v, retval)
   1608 	struct proc *p;
   1609 	void *v;
   1610 	register_t *retval;
   1611 {
   1612 	struct compat_netbsd32_setlogin_args /* {
   1613 		syscallarg(const netbsd32_charp) namebuf;
   1614 	} */ *uap = v;
   1615 	struct sys_setlogin_args ua;
   1616 
   1617 	NETBSD32TOP_UAP(namebuf, char);
   1618 	return (sys_setlogin(p, &ua, retval));
   1619 }
   1620 
   1621 int
   1622 compat_netbsd32_acct(p, v, retval)
   1623 	struct proc *p;
   1624 	void *v;
   1625 	register_t *retval;
   1626 {
   1627 	struct compat_netbsd32_acct_args /* {
   1628 		syscallarg(const netbsd32_charp) path;
   1629 	} */ *uap = v;
   1630 	struct sys_acct_args ua;
   1631 
   1632 	NETBSD32TOP_UAP(path, const char);
   1633 	return (sys_acct(p, &ua, retval));
   1634 }
   1635 
   1636 int
   1637 compat_netbsd32_revoke(p, v, retval)
   1638 	struct proc *p;
   1639 	void *v;
   1640 	register_t *retval;
   1641 {
   1642 	struct compat_netbsd32_revoke_args /* {
   1643 		syscallarg(const netbsd32_charp) path;
   1644 	} */ *uap = v;
   1645 	struct sys_revoke_args ua;
   1646 	caddr_t sg;
   1647 
   1648 	NETBSD32TOP_UAP(path, const char);
   1649 	sg = stackgap_init(p->p_emul);
   1650 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1651 
   1652 	return (sys_revoke(p, &ua, retval));
   1653 }
   1654 
   1655 int
   1656 compat_netbsd32_symlink(p, v, retval)
   1657 	struct proc *p;
   1658 	void *v;
   1659 	register_t *retval;
   1660 {
   1661 	struct compat_netbsd32_symlink_args /* {
   1662 		syscallarg(const netbsd32_charp) path;
   1663 		syscallarg(const netbsd32_charp) link;
   1664 	} */ *uap = v;
   1665 	struct sys_symlink_args ua;
   1666 
   1667 	NETBSD32TOP_UAP(path, const char);
   1668 	NETBSD32TOP_UAP(link, const char);
   1669 
   1670 	return (sys_symlink(p, &ua, retval));
   1671 }
   1672 
   1673 int
   1674 compat_netbsd32_readlink(p, v, retval)
   1675 	struct proc *p;
   1676 	void *v;
   1677 	register_t *retval;
   1678 {
   1679 	struct compat_netbsd32_readlink_args /* {
   1680 		syscallarg(const netbsd32_charp) path;
   1681 		syscallarg(netbsd32_charp) buf;
   1682 		syscallarg(netbsd32_size_t) count;
   1683 	} */ *uap = v;
   1684 	struct sys_readlink_args ua;
   1685 	caddr_t sg;
   1686 
   1687 	NETBSD32TOP_UAP(path, const char);
   1688 	NETBSD32TOP_UAP(buf, char);
   1689 	NETBSD32TOX_UAP(count, size_t);
   1690 	sg = stackgap_init(p->p_emul);
   1691 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1692 
   1693 	return (sys_readlink(p, &ua, retval));
   1694 }
   1695 
   1696 int
   1697 compat_netbsd32_execve(p, v, retval)
   1698 	struct proc *p;
   1699 	void *v;
   1700 	register_t *retval;
   1701 {
   1702 	struct compat_netbsd32_execve_args /* {
   1703 		syscallarg(const netbsd32_charp) path;
   1704 		syscallarg(netbsd32_charpp) argp;
   1705 		syscallarg(netbsd32_charpp) envp;
   1706 	} */ *uap = v;
   1707 	struct sys_execve_args ua;
   1708 	caddr_t sg;
   1709 
   1710 	NETBSD32TOP_UAP(path, const char);
   1711 	NETBSD32TOP_UAP(argp, char *);
   1712 	NETBSD32TOP_UAP(envp, char *);
   1713 	sg = stackgap_init(p->p_emul);
   1714 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1715 
   1716 	return (sys_execve(p, &ua, retval));
   1717 }
   1718 
   1719 int
   1720 compat_netbsd32_umask(p, v, retval)
   1721 	struct proc *p;
   1722 	void *v;
   1723 	register_t *retval;
   1724 {
   1725 	struct compat_netbsd32_umask_args /* {
   1726 		syscallarg(mode_t) newmask;
   1727 	} */ *uap = v;
   1728 	struct sys_umask_args ua;
   1729 
   1730 	NETBSD32TO64_UAP(newmask);
   1731 	return (sys_umask(p, &ua, retval));
   1732 }
   1733 
   1734 int
   1735 compat_netbsd32_chroot(p, v, retval)
   1736 	struct proc *p;
   1737 	void *v;
   1738 	register_t *retval;
   1739 {
   1740 	struct compat_netbsd32_chroot_args /* {
   1741 		syscallarg(const netbsd32_charp) path;
   1742 	} */ *uap = v;
   1743 	struct sys_chroot_args ua;
   1744 
   1745 	NETBSD32TOP_UAP(path, const char);
   1746 	return (sys_chroot(p, &ua, retval));
   1747 }
   1748 
   1749 int
   1750 compat_netbsd32_sbrk(p, v, retval)
   1751 	struct proc *p;
   1752 	void *v;
   1753 	register_t *retval;
   1754 {
   1755 	struct compat_netbsd32_sbrk_args /* {
   1756 		syscallarg(int) incr;
   1757 	} */ *uap = v;
   1758 	struct sys_sbrk_args ua;
   1759 
   1760 	NETBSD32TO64_UAP(incr);
   1761 	return (sys_sbrk(p, &ua, retval));
   1762 }
   1763 
   1764 int
   1765 compat_netbsd32_sstk(p, v, retval)
   1766 	struct proc *p;
   1767 	void *v;
   1768 	register_t *retval;
   1769 {
   1770 	struct compat_netbsd32_sstk_args /* {
   1771 		syscallarg(int) incr;
   1772 	} */ *uap = v;
   1773 	struct sys_sstk_args ua;
   1774 
   1775 	NETBSD32TO64_UAP(incr);
   1776 	return (sys_sstk(p, &ua, retval));
   1777 }
   1778 
   1779 int
   1780 compat_netbsd32_munmap(p, v, retval)
   1781 	struct proc *p;
   1782 	void *v;
   1783 	register_t *retval;
   1784 {
   1785 	struct compat_netbsd32_munmap_args /* {
   1786 		syscallarg(netbsd32_voidp) addr;
   1787 		syscallarg(netbsd32_size_t) len;
   1788 	} */ *uap = v;
   1789 	struct sys_munmap_args ua;
   1790 
   1791 	NETBSD32TOP_UAP(addr, void);
   1792 	NETBSD32TOX_UAP(len, size_t);
   1793 	return (sys_munmap(p, &ua, retval));
   1794 }
   1795 
   1796 int
   1797 compat_netbsd32_mprotect(p, v, retval)
   1798 	struct proc *p;
   1799 	void *v;
   1800 	register_t *retval;
   1801 {
   1802 	struct compat_netbsd32_mprotect_args /* {
   1803 		syscallarg(netbsd32_voidp) addr;
   1804 		syscallarg(netbsd32_size_t) len;
   1805 		syscallarg(int) prot;
   1806 	} */ *uap = v;
   1807 	struct sys_mprotect_args ua;
   1808 
   1809 	NETBSD32TOP_UAP(addr, void);
   1810 	NETBSD32TOX_UAP(len, size_t);
   1811 	NETBSD32TO64_UAP(prot);
   1812 	return (sys_mprotect(p, &ua, retval));
   1813 }
   1814 
   1815 int
   1816 compat_netbsd32_madvise(p, v, retval)
   1817 	struct proc *p;
   1818 	void *v;
   1819 	register_t *retval;
   1820 {
   1821 	struct compat_netbsd32_madvise_args /* {
   1822 		syscallarg(netbsd32_voidp) addr;
   1823 		syscallarg(netbsd32_size_t) len;
   1824 		syscallarg(int) behav;
   1825 	} */ *uap = v;
   1826 	struct sys_madvise_args ua;
   1827 
   1828 	NETBSD32TOP_UAP(addr, void);
   1829 	NETBSD32TOX_UAP(len, size_t);
   1830 	NETBSD32TO64_UAP(behav);
   1831 	return (sys_madvise(p, &ua, retval));
   1832 }
   1833 
   1834 int
   1835 compat_netbsd32_mincore(p, v, retval)
   1836 	struct proc *p;
   1837 	void *v;
   1838 	register_t *retval;
   1839 {
   1840 	struct compat_netbsd32_mincore_args /* {
   1841 		syscallarg(netbsd32_caddr_t) addr;
   1842 		syscallarg(netbsd32_size_t) len;
   1843 		syscallarg(netbsd32_charp) vec;
   1844 	} */ *uap = v;
   1845 	struct sys_mincore_args ua;
   1846 
   1847 	NETBSD32TOX64_UAP(addr, caddr_t);
   1848 	NETBSD32TOX_UAP(len, size_t);
   1849 	NETBSD32TOP_UAP(vec, char);
   1850 	return (sys_mincore(p, &ua, retval));
   1851 }
   1852 
   1853 int
   1854 compat_netbsd32_getgroups(p, v, retval)
   1855 	struct proc *p;
   1856 	void *v;
   1857 	register_t *retval;
   1858 {
   1859 	struct compat_netbsd32_getgroups_args /* {
   1860 		syscallarg(int) gidsetsize;
   1861 		syscallarg(netbsd32_gid_tp) gidset;
   1862 	} */ *uap = v;
   1863 	register struct pcred *pc = p->p_cred;
   1864 	register int ngrp;
   1865 	int error;
   1866 
   1867 	ngrp = SCARG(uap, gidsetsize);
   1868 	if (ngrp == 0) {
   1869 		*retval = pc->pc_ucred->cr_ngroups;
   1870 		return (0);
   1871 	}
   1872 	if (ngrp < pc->pc_ucred->cr_ngroups)
   1873 		return (EINVAL);
   1874 	ngrp = pc->pc_ucred->cr_ngroups;
   1875 	/* Should convert gid_t to netbsd32_gid_t, but they're the same */
   1876 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
   1877 			(caddr_t)(u_long)SCARG(uap, gidset),
   1878 			ngrp * sizeof(gid_t));
   1879 	if (error)
   1880 		return (error);
   1881 	*retval = ngrp;
   1882 	return (0);
   1883 }
   1884 
   1885 int
   1886 compat_netbsd32_setgroups(p, v, retval)
   1887 	struct proc *p;
   1888 	void *v;
   1889 	register_t *retval;
   1890 {
   1891 	struct compat_netbsd32_setgroups_args /* {
   1892 		syscallarg(int) gidsetsize;
   1893 		syscallarg(const netbsd32_gid_tp) gidset;
   1894 	} */ *uap = v;
   1895 	struct sys_setgroups_args ua;
   1896 
   1897 	NETBSD32TO64_UAP(gidsetsize);
   1898 	NETBSD32TOP_UAP(gidset, gid_t);
   1899 	return (sys_setgroups(p, &ua, retval));
   1900 }
   1901 
   1902 int
   1903 compat_netbsd32_setpgid(p, v, retval)
   1904 	struct proc *p;
   1905 	void *v;
   1906 	register_t *retval;
   1907 {
   1908 	struct compat_netbsd32_setpgid_args /* {
   1909 		syscallarg(int) pid;
   1910 		syscallarg(int) pgid;
   1911 	} */ *uap = v;
   1912 	struct sys_setpgid_args ua;
   1913 
   1914 	NETBSD32TO64_UAP(pid);
   1915 	NETBSD32TO64_UAP(pgid);
   1916 	return (sys_setpgid(p, &ua, retval));
   1917 }
   1918 
   1919 int
   1920 compat_netbsd32_setitimer(p, v, retval)
   1921 	struct proc *p;
   1922 	void *v;
   1923 	register_t *retval;
   1924 {
   1925 	struct compat_netbsd32_setitimer_args /* {
   1926 		syscallarg(int) which;
   1927 		syscallarg(const netbsd32_itimervalp_t) itv;
   1928 		syscallarg(netbsd32_itimervalp_t) oitv;
   1929 	} */ *uap = v;
   1930 	struct netbsd32_itimerval s32it, *itvp;
   1931 	int which = SCARG(uap, which);
   1932 	struct compat_netbsd32_getitimer_args getargs;
   1933 	struct itimerval aitv;
   1934 	int s, error;
   1935 
   1936 	if ((u_int)which > ITIMER_PROF)
   1937 		return (EINVAL);
   1938 	itvp = (struct netbsd32_itimerval *)(u_long)SCARG(uap, itv);
   1939 	if (itvp && (error = copyin(itvp, &s32it, sizeof(s32it))))
   1940 		return (error);
   1941 	netbsd32_to_itimerval(&s32it, &aitv);
   1942 	if (SCARG(uap, oitv) != NULL) {
   1943 		SCARG(&getargs, which) = which;
   1944 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   1945 		if ((error = compat_netbsd32_getitimer(p, &getargs, retval)) != 0)
   1946 			return (error);
   1947 	}
   1948 	if (itvp == 0)
   1949 		return (0);
   1950 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
   1951 		return (EINVAL);
   1952 	s = splclock();
   1953 	if (which == ITIMER_REAL) {
   1954 		untimeout(realitexpire, p);
   1955 		if (timerisset(&aitv.it_value)) {
   1956 			timeradd(&aitv.it_value, &time, &aitv.it_value);
   1957 			timeout(realitexpire, p, hzto(&aitv.it_value));
   1958 		}
   1959 		p->p_realtimer = aitv;
   1960 	} else
   1961 		p->p_stats->p_timer[which] = aitv;
   1962 	splx(s);
   1963 	return (0);
   1964 }
   1965 
   1966 int
   1967 compat_netbsd32_getitimer(p, v, retval)
   1968 	struct proc *p;
   1969 	void *v;
   1970 	register_t *retval;
   1971 {
   1972 	struct compat_netbsd32_getitimer_args /* {
   1973 		syscallarg(int) which;
   1974 		syscallarg(netbsd32_itimervalp_t) itv;
   1975 	} */ *uap = v;
   1976 	int which = SCARG(uap, which);
   1977 	struct netbsd32_itimerval s32it;
   1978 	struct itimerval aitv;
   1979 	int s;
   1980 
   1981 	if ((u_int)which > ITIMER_PROF)
   1982 		return (EINVAL);
   1983 	s = splclock();
   1984 	if (which == ITIMER_REAL) {
   1985 		/*
   1986 		 * Convert from absolute to relative time in .it_value
   1987 		 * part of real time timer.  If time for real time timer
   1988 		 * has passed return 0, else return difference between
   1989 		 * current time and time for the timer to go off.
   1990 		 */
   1991 		aitv = p->p_realtimer;
   1992 		if (timerisset(&aitv.it_value)) {
   1993 			if (timercmp(&aitv.it_value, &time, <))
   1994 				timerclear(&aitv.it_value);
   1995 			else
   1996 				timersub(&aitv.it_value, &time, &aitv.it_value);
   1997 		}
   1998 	} else
   1999 		aitv = p->p_stats->p_timer[which];
   2000 	splx(s);
   2001 	netbsd32_from_itimerval(&aitv, &s32it);
   2002 	return (copyout(&s32it, (caddr_t)(u_long)SCARG(uap, itv), sizeof(s32it)));
   2003 }
   2004 
   2005 int
   2006 compat_netbsd32_fcntl(p, v, retval)
   2007 	struct proc *p;
   2008 	void *v;
   2009 	register_t *retval;
   2010 {
   2011 	struct compat_netbsd32_fcntl_args /* {
   2012 		syscallarg(int) fd;
   2013 		syscallarg(int) cmd;
   2014 		syscallarg(netbsd32_voidp) arg;
   2015 	} */ *uap = v;
   2016 	struct sys_fcntl_args ua;
   2017 
   2018 	NETBSD32TO64_UAP(fd);
   2019 	NETBSD32TO64_UAP(cmd);
   2020 	NETBSD32TOP_UAP(arg, void);
   2021 	/* XXXX we can do this 'cause flock doesn't change */
   2022 	return (sys_fcntl(p, &ua, retval));
   2023 }
   2024 
   2025 int
   2026 compat_netbsd32_dup2(p, v, retval)
   2027 	struct proc *p;
   2028 	void *v;
   2029 	register_t *retval;
   2030 {
   2031 	struct compat_netbsd32_dup2_args /* {
   2032 		syscallarg(int) from;
   2033 		syscallarg(int) to;
   2034 	} */ *uap = v;
   2035 	struct sys_dup2_args ua;
   2036 
   2037 	NETBSD32TO64_UAP(from);
   2038 	NETBSD32TO64_UAP(to);
   2039 	return (sys_dup2(p, &ua, retval));
   2040 }
   2041 
   2042 int
   2043 compat_netbsd32_select(p, v, retval)
   2044 	struct proc *p;
   2045 	void *v;
   2046 	register_t *retval;
   2047 {
   2048 	struct compat_netbsd32_select_args /* {
   2049 		syscallarg(int) nd;
   2050 		syscallarg(netbsd32_fd_setp_t) in;
   2051 		syscallarg(netbsd32_fd_setp_t) ou;
   2052 		syscallarg(netbsd32_fd_setp_t) ex;
   2053 		syscallarg(netbsd32_timevalp_t) tv;
   2054 	} */ *uap = v;
   2055 /* This one must be done in-line 'cause of the timeval */
   2056 	struct netbsd32_timeval tv32;
   2057 	caddr_t bits;
   2058 	char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
   2059 	struct timeval atv;
   2060 	int s, ncoll, error = 0, timo;
   2061 	size_t ni;
   2062 	extern int	selwait, nselcoll;
   2063 	extern int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
   2064 
   2065 	if (SCARG(uap, nd) < 0)
   2066 		return (EINVAL);
   2067 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
   2068 		/* forgiving; slightly wrong */
   2069 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
   2070 	}
   2071 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
   2072 	if (ni * 6 > sizeof(smallbits))
   2073 		bits = malloc(ni * 6, M_TEMP, M_WAITOK);
   2074 	else
   2075 		bits = smallbits;
   2076 
   2077 #define	getbits(name, x) \
   2078 	if (SCARG(uap, name)) { \
   2079 		error = copyin((caddr_t)(u_long)SCARG(uap, name), bits + ni * x, ni); \
   2080 		if (error) \
   2081 			goto done; \
   2082 	} else \
   2083 		memset(bits + ni * x, 0, ni);
   2084 	getbits(in, 0);
   2085 	getbits(ou, 1);
   2086 	getbits(ex, 2);
   2087 #undef	getbits
   2088 
   2089 	if (SCARG(uap, tv)) {
   2090 		error = copyin((caddr_t)(u_long)SCARG(uap, tv), (caddr_t)&tv32,
   2091 			sizeof(tv32));
   2092 		if (error)
   2093 			goto done;
   2094 		netbsd32_to_timeval(&tv32, &atv);
   2095 		if (itimerfix(&atv)) {
   2096 			error = EINVAL;
   2097 			goto done;
   2098 		}
   2099 		s = splclock();
   2100 		timeradd(&atv, &time, &atv);
   2101 		timo = hzto(&atv);
   2102 		/*
   2103 		 * Avoid inadvertently sleeping forever.
   2104 		 */
   2105 		if (timo == 0)
   2106 			timo = 1;
   2107 		splx(s);
   2108 	} else
   2109 		timo = 0;
   2110 retry:
   2111 	ncoll = nselcoll;
   2112 	p->p_flag |= P_SELECT;
   2113 	error = selscan(p, (fd_mask *)(bits + ni * 0),
   2114 			   (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
   2115 	if (error || *retval)
   2116 		goto done;
   2117 	s = splhigh();
   2118 	if (timo && timercmp(&time, &atv, >=)) {
   2119 		splx(s);
   2120 		goto done;
   2121 	}
   2122 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
   2123 		splx(s);
   2124 		goto retry;
   2125 	}
   2126 	p->p_flag &= ~P_SELECT;
   2127 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
   2128 	splx(s);
   2129 	if (error == 0)
   2130 		goto retry;
   2131 done:
   2132 	p->p_flag &= ~P_SELECT;
   2133 	/* select is not restarted after signals... */
   2134 	if (error == ERESTART)
   2135 		error = EINTR;
   2136 	if (error == EWOULDBLOCK)
   2137 		error = 0;
   2138 	if (error == 0) {
   2139 #define	putbits(name, x) \
   2140 		if (SCARG(uap, name)) { \
   2141 			error = copyout(bits + ni * x, (caddr_t)(u_long)SCARG(uap, name), ni); \
   2142 			if (error) \
   2143 				goto out; \
   2144 		}
   2145 		putbits(in, 3);
   2146 		putbits(ou, 4);
   2147 		putbits(ex, 5);
   2148 #undef putbits
   2149 	}
   2150 out:
   2151 	if (ni * 6 > sizeof(smallbits))
   2152 		free(bits, M_TEMP);
   2153 	return (error);
   2154 }
   2155 
   2156 int
   2157 compat_netbsd32_fsync(p, v, retval)
   2158 	struct proc *p;
   2159 	void *v;
   2160 	register_t *retval;
   2161 {
   2162 	struct compat_netbsd32_fsync_args /* {
   2163 		syscallarg(int) fd;
   2164 	} */ *uap = v;
   2165 	struct sys_fsync_args ua;
   2166 
   2167 	NETBSD32TO64_UAP(fd);
   2168 	return (sys_fsync(p, &ua, retval));
   2169 }
   2170 
   2171 int
   2172 compat_netbsd32_setpriority(p, v, retval)
   2173 	struct proc *p;
   2174 	void *v;
   2175 	register_t *retval;
   2176 {
   2177 	struct compat_netbsd32_setpriority_args /* {
   2178 		syscallarg(int) which;
   2179 		syscallarg(int) who;
   2180 		syscallarg(int) prio;
   2181 	} */ *uap = v;
   2182 	struct sys_setpriority_args ua;
   2183 
   2184 	NETBSD32TO64_UAP(which);
   2185 	NETBSD32TO64_UAP(who);
   2186 	NETBSD32TO64_UAP(prio);
   2187 	return (sys_setpriority(p, &ua, retval));
   2188 }
   2189 
   2190 int
   2191 compat_netbsd32_socket(p, v, retval)
   2192 	struct proc *p;
   2193 	void *v;
   2194 	register_t *retval;
   2195 {
   2196 	struct compat_netbsd32_socket_args /* {
   2197 		syscallarg(int) domain;
   2198 		syscallarg(int) type;
   2199 		syscallarg(int) protocol;
   2200 	} */ *uap = v;
   2201 	struct sys_socket_args ua;
   2202 
   2203 	NETBSD32TO64_UAP(domain);
   2204 	NETBSD32TO64_UAP(type);
   2205 	NETBSD32TO64_UAP(protocol);
   2206 	return (sys_socket(p, &ua, retval));
   2207 }
   2208 
   2209 int
   2210 compat_netbsd32_connect(p, v, retval)
   2211 	struct proc *p;
   2212 	void *v;
   2213 	register_t *retval;
   2214 {
   2215 	struct compat_netbsd32_connect_args /* {
   2216 		syscallarg(int) s;
   2217 		syscallarg(const netbsd32_sockaddrp_t) name;
   2218 		syscallarg(int) namelen;
   2219 	} */ *uap = v;
   2220 	struct sys_connect_args ua;
   2221 
   2222 	NETBSD32TO64_UAP(s);
   2223 	NETBSD32TOP_UAP(name, struct sockaddr);
   2224 	NETBSD32TO64_UAP(namelen);
   2225 	return (sys_connect(p, &ua, retval));
   2226 }
   2227 
   2228 int
   2229 compat_netbsd32_getpriority(p, v, retval)
   2230 	struct proc *p;
   2231 	void *v;
   2232 	register_t *retval;
   2233 {
   2234 	struct compat_netbsd32_getpriority_args /* {
   2235 		syscallarg(int) which;
   2236 		syscallarg(int) who;
   2237 	} */ *uap = v;
   2238 	struct sys_getpriority_args ua;
   2239 
   2240 	NETBSD32TO64_UAP(which);
   2241 	NETBSD32TO64_UAP(who);
   2242 	return (sys_getpriority(p, &ua, retval));
   2243 }
   2244 
   2245 int
   2246 compat_netbsd32_bind(p, v, retval)
   2247 	struct proc *p;
   2248 	void *v;
   2249 	register_t *retval;
   2250 {
   2251 	struct compat_netbsd32_bind_args /* {
   2252 		syscallarg(int) s;
   2253 		syscallarg(const netbsd32_sockaddrp_t) name;
   2254 		syscallarg(int) namelen;
   2255 	} */ *uap = v;
   2256 	struct sys_bind_args ua;
   2257 
   2258 	NETBSD32TO64_UAP(s);
   2259 	NETBSD32TOP_UAP(name, struct sockaddr);
   2260 	NETBSD32TO64_UAP(namelen);
   2261 	return (sys_bind(p, &ua, retval));
   2262 }
   2263 
   2264 int
   2265 compat_netbsd32_setsockopt(p, v, retval)
   2266 	struct proc *p;
   2267 	void *v;
   2268 	register_t *retval;
   2269 {
   2270 	struct compat_netbsd32_setsockopt_args /* {
   2271 		syscallarg(int) s;
   2272 		syscallarg(int) level;
   2273 		syscallarg(int) name;
   2274 		syscallarg(const netbsd32_voidp) val;
   2275 		syscallarg(int) valsize;
   2276 	} */ *uap = v;
   2277 	struct sys_setsockopt_args ua;
   2278 
   2279 	NETBSD32TO64_UAP(s);
   2280 	NETBSD32TO64_UAP(level);
   2281 	NETBSD32TO64_UAP(name);
   2282 	NETBSD32TOP_UAP(val, void);
   2283 	NETBSD32TO64_UAP(valsize);
   2284 	/* may be more efficient to do this inline. */
   2285 	return (sys_setsockopt(p, &ua, retval));
   2286 }
   2287 
   2288 int
   2289 compat_netbsd32_listen(p, v, retval)
   2290 	struct proc *p;
   2291 	void *v;
   2292 	register_t *retval;
   2293 {
   2294 	struct compat_netbsd32_listen_args /* {
   2295 		syscallarg(int) s;
   2296 		syscallarg(int) backlog;
   2297 	} */ *uap = v;
   2298 	struct sys_listen_args ua;
   2299 
   2300 	NETBSD32TO64_UAP(s);
   2301 	NETBSD32TO64_UAP(backlog);
   2302 	return (sys_listen(p, &ua, retval));
   2303 }
   2304 
   2305 int
   2306 compat_netbsd32_vtrace(p, v, retval)
   2307 	struct proc *p;
   2308 	void *v;
   2309 	register_t *retval;
   2310 {
   2311 #ifdef TRACE
   2312 	struct compat_netbsd32_vtrace_args /* {
   2313 		syscallarg(int) request;
   2314 		syscallarg(int) value;
   2315 	} */ *uap = v;
   2316 	struct sys_vtrace_args ua;
   2317 
   2318 	NETBSD32TO64_UAP(request);
   2319 	NETBSD32TO64_UAP(value);
   2320 	return (vtrace(p, &ua, retval));
   2321 #else
   2322 	return (ENOSYS);
   2323 #endif
   2324 }
   2325 
   2326 int
   2327 compat_netbsd32_gettimeofday(p, v, retval)
   2328 	struct proc *p;
   2329 	void *v;
   2330 	register_t *retval;
   2331 {
   2332 	struct compat_netbsd32_gettimeofday_args /* {
   2333 		syscallarg(netbsd32_timevalp_t) tp;
   2334 		syscallarg(netbsd32_timezonep_t) tzp;
   2335 	} */ *uap = v;
   2336 	struct timeval atv;
   2337 	struct netbsd32_timeval tv32;
   2338 	int error = 0;
   2339 	struct netbsd32_timezone tzfake;
   2340 
   2341 	if (SCARG(uap, tp)) {
   2342 		microtime(&atv);
   2343 		netbsd32_from_timeval(&atv, &tv32);
   2344 		error = copyout(&tv32, (caddr_t)(u_long)SCARG(uap, tp), sizeof(tv32));
   2345 		if (error)
   2346 			return (error);
   2347 	}
   2348 	if (SCARG(uap, tzp)) {
   2349 		/*
   2350 		 * NetBSD has no kernel notion of time zone, so we just
   2351 		 * fake up a timezone struct and return it if demanded.
   2352 		 */
   2353 		tzfake.tz_minuteswest = 0;
   2354 		tzfake.tz_dsttime = 0;
   2355 		error = copyout(&tzfake, (caddr_t)(u_long)SCARG(uap, tzp), sizeof(tzfake));
   2356 	}
   2357 	return (error);
   2358 }
   2359 
   2360 static int settime __P((struct timeval *));
   2361 /* This function is used by clock_settime and settimeofday */
   2362 static int
   2363 settime(tv)
   2364 	struct timeval *tv;
   2365 {
   2366 	struct timeval delta;
   2367 	int s;
   2368 
   2369 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
   2370 	s = splclock();
   2371 	timersub(tv, &time, &delta);
   2372 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1)
   2373 		return (EPERM);
   2374 #ifdef notyet
   2375 	if ((delta.tv_sec < 86400) && securelevel > 0)
   2376 		return (EPERM);
   2377 #endif
   2378 	time = *tv;
   2379 	(void) splsoftclock();
   2380 	timeradd(&boottime, &delta, &boottime);
   2381 	timeradd(&runtime, &delta, &runtime);
   2382 #	if defined(NFS) || defined(NFSSERVER)
   2383 		nqnfs_lease_updatetime(delta.tv_sec);
   2384 #	endif
   2385 	splx(s);
   2386 	resettodr();
   2387 	return (0);
   2388 }
   2389 
   2390 
   2391 int
   2392 compat_netbsd32_settimeofday(p, v, retval)
   2393 	struct proc *p;
   2394 	void *v;
   2395 	register_t *retval;
   2396 {
   2397 	struct compat_netbsd32_settimeofday_args /* {
   2398 		syscallarg(const netbsd32_timevalp_t) tv;
   2399 		syscallarg(const netbsd32_timezonep_t) tzp;
   2400 	} */ *uap = v;
   2401 	struct netbsd32_timeval atv32;
   2402 	struct timeval atv;
   2403 	struct netbsd32_timezone atz;
   2404 	int error;
   2405 
   2406 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   2407 		return (error);
   2408 	/* Verify all parameters before changing time. */
   2409 	if (SCARG(uap, tv) && (error = copyin((caddr_t)(u_long)SCARG(uap, tv),
   2410 	    &atv32, sizeof(atv32))))
   2411 		return (error);
   2412 	netbsd32_to_timeval(&atv32, &atv);
   2413 	/* XXX since we don't use tz, probably no point in doing copyin. */
   2414 	if (SCARG(uap, tzp) && (error = copyin((caddr_t)(u_long)SCARG(uap, tzp),
   2415 	    &atz, sizeof(atz))))
   2416 		return (error);
   2417 	if (SCARG(uap, tv))
   2418 		if ((error = settime(&atv)))
   2419 			return (error);
   2420 	/*
   2421 	 * NetBSD has no kernel notion of time zone, and only an
   2422 	 * obsolete program would try to set it, so we log a warning.
   2423 	 */
   2424 	if (SCARG(uap, tzp))
   2425 		printf("pid %d attempted to set the "
   2426 		    "(obsolete) kernel time zone\n", p->p_pid);
   2427 	return (0);
   2428 }
   2429 
   2430 int
   2431 compat_netbsd32_fchown(p, v, retval)
   2432 	struct proc *p;
   2433 	void *v;
   2434 	register_t *retval;
   2435 {
   2436 	struct compat_netbsd32_fchown_args /* {
   2437 		syscallarg(int) fd;
   2438 		syscallarg(uid_t) uid;
   2439 		syscallarg(gid_t) gid;
   2440 	} */ *uap = v;
   2441 	struct sys_fchown_args ua;
   2442 
   2443 	NETBSD32TO64_UAP(fd);
   2444 	NETBSD32TO64_UAP(uid);
   2445 	NETBSD32TO64_UAP(gid);
   2446 	return (sys_fchown(p, &ua, retval));
   2447 }
   2448 
   2449 int
   2450 compat_netbsd32_fchmod(p, v, retval)
   2451 	struct proc *p;
   2452 	void *v;
   2453 	register_t *retval;
   2454 {
   2455 	struct compat_netbsd32_fchmod_args /* {
   2456 		syscallarg(int) fd;
   2457 		syscallarg(mode_t) mode;
   2458 	} */ *uap = v;
   2459 	struct sys_fchmod_args ua;
   2460 
   2461 	NETBSD32TO64_UAP(fd);
   2462 	NETBSD32TO64_UAP(mode);
   2463 	return (sys_fchmod(p, &ua, retval));
   2464 }
   2465 
   2466 int
   2467 compat_netbsd32_setreuid(p, v, retval)
   2468 	struct proc *p;
   2469 	void *v;
   2470 	register_t *retval;
   2471 {
   2472 	struct compat_netbsd32_setreuid_args /* {
   2473 		syscallarg(uid_t) ruid;
   2474 		syscallarg(uid_t) euid;
   2475 	} */ *uap = v;
   2476 	struct sys_setreuid_args ua;
   2477 
   2478 	NETBSD32TO64_UAP(ruid);
   2479 	NETBSD32TO64_UAP(euid);
   2480 	return (sys_setreuid(p, &ua, retval));
   2481 }
   2482 
   2483 int
   2484 compat_netbsd32_setregid(p, v, retval)
   2485 	struct proc *p;
   2486 	void *v;
   2487 	register_t *retval;
   2488 {
   2489 	struct compat_netbsd32_setregid_args /* {
   2490 		syscallarg(gid_t) rgid;
   2491 		syscallarg(gid_t) egid;
   2492 	} */ *uap = v;
   2493 	struct sys_setregid_args ua;
   2494 
   2495 	NETBSD32TO64_UAP(rgid);
   2496 	NETBSD32TO64_UAP(egid);
   2497 	return (sys_setregid(p, &ua, retval));
   2498 }
   2499 
   2500 int
   2501 compat_netbsd32_getrusage(p, v, retval)
   2502 	struct proc *p;
   2503 	void *v;
   2504 	register_t *retval;
   2505 {
   2506 	struct compat_netbsd32_getrusage_args /* {
   2507 		syscallarg(int) who;
   2508 		syscallarg(netbsd32_rusagep_t) rusage;
   2509 	} */ *uap = v;
   2510 	struct rusage *rup;
   2511 	struct netbsd32_rusage ru;
   2512 
   2513 	switch (SCARG(uap, who)) {
   2514 
   2515 	case RUSAGE_SELF:
   2516 		rup = &p->p_stats->p_ru;
   2517 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
   2518 		break;
   2519 
   2520 	case RUSAGE_CHILDREN:
   2521 		rup = &p->p_stats->p_cru;
   2522 		break;
   2523 
   2524 	default:
   2525 		return (EINVAL);
   2526 	}
   2527 	netbsd32_from_rusage(rup, &ru);
   2528 	return (copyout(&ru, (caddr_t)(u_long)SCARG(uap, rusage), sizeof(ru)));
   2529 }
   2530 
   2531 int
   2532 compat_netbsd32_getsockopt(p, v, retval)
   2533 	struct proc *p;
   2534 	void *v;
   2535 	register_t *retval;
   2536 {
   2537 	struct compat_netbsd32_getsockopt_args /* {
   2538 		syscallarg(int) s;
   2539 		syscallarg(int) level;
   2540 		syscallarg(int) name;
   2541 		syscallarg(netbsd32_voidp) val;
   2542 		syscallarg(netbsd32_intp) avalsize;
   2543 	} */ *uap = v;
   2544 	struct sys_getsockopt_args ua;
   2545 
   2546 	NETBSD32TO64_UAP(s);
   2547 	NETBSD32TO64_UAP(level);
   2548 	NETBSD32TO64_UAP(name);
   2549 	NETBSD32TOP_UAP(val, void);
   2550 	NETBSD32TOP_UAP(avalsize, int);
   2551 	return (sys_getsockopt(p, &ua, retval));
   2552 }
   2553 
   2554 int
   2555 compat_netbsd32_readv(p, v, retval)
   2556 	struct proc *p;
   2557 	void *v;
   2558 	register_t *retval;
   2559 {
   2560 	struct compat_netbsd32_readv_args /* {
   2561 		syscallarg(int) fd;
   2562 		syscallarg(const netbsd32_iovecp_t) iovp;
   2563 		syscallarg(int) iovcnt;
   2564 	} */ *uap = v;
   2565 	int fd = SCARG(uap, fd);
   2566 	register struct file *fp;
   2567 	register struct filedesc *fdp = p->p_fd;
   2568 
   2569 	if ((u_int)fd >= fdp->fd_nfiles ||
   2570 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   2571 	    (fp->f_flag & FREAD) == 0)
   2572 		return (EBADF);
   2573 
   2574 	return (dofilereadv32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp),
   2575 			      SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
   2576 }
   2577 
   2578 /* Damn thing copies in the iovec! */
   2579 int
   2580 dofilereadv32(p, fd, fp, iovp, iovcnt, offset, flags, retval)
   2581 	struct proc *p;
   2582 	int fd;
   2583 	struct file *fp;
   2584 	struct netbsd32_iovec *iovp;
   2585 	int iovcnt;
   2586 	off_t *offset;
   2587 	int flags;
   2588 	register_t *retval;
   2589 {
   2590 	struct uio auio;
   2591 	register struct iovec *iov;
   2592 	struct iovec *needfree;
   2593 	struct iovec aiov[UIO_SMALLIOV];
   2594 	long i, cnt, error = 0;
   2595 	u_int iovlen;
   2596 #ifdef KTRACE
   2597 	struct iovec *ktriov = NULL;
   2598 #endif
   2599 
   2600 	/* note: can't use iovlen until iovcnt is validated */
   2601 	iovlen = iovcnt * sizeof(struct iovec);
   2602 	if ((u_int)iovcnt > UIO_SMALLIOV) {
   2603 		if ((u_int)iovcnt > IOV_MAX)
   2604 			return (EINVAL);
   2605 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
   2606 		needfree = iov;
   2607 	} else if ((u_int)iovcnt > 0) {
   2608 		iov = aiov;
   2609 		needfree = NULL;
   2610 	} else
   2611 		return (EINVAL);
   2612 
   2613 	auio.uio_iov = iov;
   2614 	auio.uio_iovcnt = iovcnt;
   2615 	auio.uio_rw = UIO_READ;
   2616 	auio.uio_segflg = UIO_USERSPACE;
   2617 	auio.uio_procp = p;
   2618 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
   2619 	if (error)
   2620 		goto done;
   2621 	auio.uio_resid = 0;
   2622 	for (i = 0; i < iovcnt; i++) {
   2623 		auio.uio_resid += iov->iov_len;
   2624 		/*
   2625 		 * Reads return ssize_t because -1 is returned on error.
   2626 		 * Therefore we must restrict the length to SSIZE_MAX to
   2627 		 * avoid garbage return values.
   2628 		 */
   2629 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   2630 			error = EINVAL;
   2631 			goto done;
   2632 		}
   2633 		iov++;
   2634 	}
   2635 #ifdef KTRACE
   2636 	/*
   2637 	 * if tracing, save a copy of iovec
   2638 	 */
   2639 	if (KTRPOINT(p, KTR_GENIO))  {
   2640 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   2641 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   2642 	}
   2643 #endif
   2644 	cnt = auio.uio_resid;
   2645 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
   2646 	if (error)
   2647 		if (auio.uio_resid != cnt && (error == ERESTART ||
   2648 		    error == EINTR || error == EWOULDBLOCK))
   2649 			error = 0;
   2650 	cnt -= auio.uio_resid;
   2651 #ifdef KTRACE
   2652 	if (KTRPOINT(p, KTR_GENIO))
   2653 		if (error == 0) {
   2654 			ktrgenio(p->p_tracep, fd, UIO_READ, ktriov, cnt,
   2655 			    error);
   2656 		FREE(ktriov, M_TEMP);
   2657 	}
   2658 #endif
   2659 	*retval = cnt;
   2660 done:
   2661 	if (needfree)
   2662 		FREE(needfree, M_IOV);
   2663 	return (error);
   2664 }
   2665 
   2666 
   2667 int
   2668 compat_netbsd32_writev(p, v, retval)
   2669 	struct proc *p;
   2670 	void *v;
   2671 	register_t *retval;
   2672 {
   2673 	struct compat_netbsd32_writev_args /* {
   2674 		syscallarg(int) fd;
   2675 		syscallarg(const netbsd32_iovecp_t) iovp;
   2676 		syscallarg(int) iovcnt;
   2677 	} */ *uap = v;
   2678 	int fd = SCARG(uap, fd);
   2679 	register struct file *fp;
   2680 	register struct filedesc *fdp = p->p_fd;
   2681 
   2682 	if ((u_int)fd >= fdp->fd_nfiles ||
   2683 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   2684 	    (fp->f_flag & FWRITE) == 0)
   2685 		return (EBADF);
   2686 
   2687 	return (dofilewritev32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp),
   2688 			       SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
   2689 }
   2690 
   2691 int
   2692 dofilewritev32(p, fd, fp, iovp, iovcnt, offset, flags, retval)
   2693 	struct proc *p;
   2694 	int fd;
   2695 	struct file *fp;
   2696 	struct netbsd32_iovec *iovp;
   2697 	int iovcnt;
   2698 	off_t *offset;
   2699 	int flags;
   2700 	register_t *retval;
   2701 {
   2702 	struct uio auio;
   2703 	register struct iovec *iov;
   2704 	struct iovec *needfree;
   2705 	struct iovec aiov[UIO_SMALLIOV];
   2706 	long i, cnt, error = 0;
   2707 	u_int iovlen;
   2708 #ifdef KTRACE
   2709 	struct iovec *ktriov = NULL;
   2710 #endif
   2711 
   2712 	/* note: can't use iovlen until iovcnt is validated */
   2713 	iovlen = iovcnt * sizeof(struct iovec);
   2714 	if ((u_int)iovcnt > UIO_SMALLIOV) {
   2715 		if ((u_int)iovcnt > IOV_MAX)
   2716 			return (EINVAL);
   2717 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
   2718 		needfree = iov;
   2719 	} else if ((u_int)iovcnt > 0) {
   2720 		iov = aiov;
   2721 		needfree = NULL;
   2722 	} else
   2723 		return (EINVAL);
   2724 
   2725 	auio.uio_iov = iov;
   2726 	auio.uio_iovcnt = iovcnt;
   2727 	auio.uio_rw = UIO_WRITE;
   2728 	auio.uio_segflg = UIO_USERSPACE;
   2729 	auio.uio_procp = p;
   2730 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
   2731 	if (error)
   2732 		goto done;
   2733 	auio.uio_resid = 0;
   2734 	for (i = 0; i < iovcnt; i++) {
   2735 		auio.uio_resid += iov->iov_len;
   2736 		/*
   2737 		 * Writes return ssize_t because -1 is returned on error.
   2738 		 * Therefore we must restrict the length to SSIZE_MAX to
   2739 		 * avoid garbage return values.
   2740 		 */
   2741 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   2742 			error = EINVAL;
   2743 			goto done;
   2744 		}
   2745 		iov++;
   2746 	}
   2747 #ifdef KTRACE
   2748 	/*
   2749 	 * if tracing, save a copy of iovec
   2750 	 */
   2751 	if (KTRPOINT(p, KTR_GENIO))  {
   2752 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   2753 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   2754 	}
   2755 #endif
   2756 	cnt = auio.uio_resid;
   2757 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
   2758 	if (error) {
   2759 		if (auio.uio_resid != cnt && (error == ERESTART ||
   2760 		    error == EINTR || error == EWOULDBLOCK))
   2761 			error = 0;
   2762 		if (error == EPIPE)
   2763 			psignal(p, SIGPIPE);
   2764 	}
   2765 	cnt -= auio.uio_resid;
   2766 #ifdef KTRACE
   2767 	if (KTRPOINT(p, KTR_GENIO))
   2768 		if (error == 0) {
   2769 			ktrgenio(p->p_tracep, fd, UIO_WRITE, ktriov, cnt,
   2770 			    error);
   2771 		FREE(ktriov, M_TEMP);
   2772 	}
   2773 #endif
   2774 	*retval = cnt;
   2775 done:
   2776 	if (needfree)
   2777 		FREE(needfree, M_IOV);
   2778 	return (error);
   2779 }
   2780 
   2781 
   2782 int
   2783 compat_netbsd32_rename(p, v, retval)
   2784 	struct proc *p;
   2785 	void *v;
   2786 	register_t *retval;
   2787 {
   2788 	struct compat_netbsd32_rename_args /* {
   2789 		syscallarg(const netbsd32_charp) from;
   2790 		syscallarg(const netbsd32_charp) to;
   2791 	} */ *uap = v;
   2792 	struct sys_rename_args ua;
   2793 
   2794 	NETBSD32TOP_UAP(from, const char *);
   2795 	NETBSD32TOP_UAP(to, const char *)
   2796 
   2797 	return (sys_rename(p, &ua, retval));
   2798 }
   2799 
   2800 int
   2801 compat_netbsd32_flock(p, v, retval)
   2802 	struct proc *p;
   2803 	void *v;
   2804 	register_t *retval;
   2805 {
   2806 	struct compat_netbsd32_flock_args /* {
   2807 		syscallarg(int) fd;
   2808 		syscallarg(int) how;
   2809 	} */ *uap = v;
   2810 	struct sys_flock_args ua;
   2811 
   2812 	NETBSD32TO64_UAP(fd);
   2813 	NETBSD32TO64_UAP(how)
   2814 
   2815 	return (sys_flock(p, &ua, retval));
   2816 }
   2817 
   2818 int
   2819 compat_netbsd32_mkfifo(p, v, retval)
   2820 	struct proc *p;
   2821 	void *v;
   2822 	register_t *retval;
   2823 {
   2824 	struct compat_netbsd32_mkfifo_args /* {
   2825 		syscallarg(const netbsd32_charp) path;
   2826 		syscallarg(mode_t) mode;
   2827 	} */ *uap = v;
   2828 	struct sys_mkfifo_args ua;
   2829 
   2830 	NETBSD32TOP_UAP(path, const char)
   2831 	NETBSD32TO64_UAP(mode);
   2832 	return (sys_mkfifo(p, &ua, retval));
   2833 }
   2834 
   2835 int
   2836 compat_netbsd32_shutdown(p, v, retval)
   2837 	struct proc *p;
   2838 	void *v;
   2839 	register_t *retval;
   2840 {
   2841 	struct compat_netbsd32_shutdown_args /* {
   2842 		syscallarg(int) s;
   2843 		syscallarg(int) how;
   2844 	} */ *uap = v;
   2845 	struct sys_shutdown_args ua;
   2846 
   2847 	NETBSD32TO64_UAP(s)
   2848 	NETBSD32TO64_UAP(how);
   2849 	return (sys_shutdown(p, &ua, retval));
   2850 }
   2851 
   2852 int
   2853 compat_netbsd32_socketpair(p, v, retval)
   2854 	struct proc *p;
   2855 	void *v;
   2856 	register_t *retval;
   2857 {
   2858 	struct compat_netbsd32_socketpair_args /* {
   2859 		syscallarg(int) domain;
   2860 		syscallarg(int) type;
   2861 		syscallarg(int) protocol;
   2862 		syscallarg(netbsd32_intp) rsv;
   2863 	} */ *uap = v;
   2864 	struct sys_socketpair_args ua;
   2865 
   2866 	NETBSD32TO64_UAP(domain);
   2867 	NETBSD32TO64_UAP(type);
   2868 	NETBSD32TO64_UAP(protocol);
   2869 	NETBSD32TOP_UAP(rsv, int);
   2870 	/* Since we're just copying out two `int's we can do this */
   2871 	return (sys_socketpair(p, &ua, retval));
   2872 }
   2873 
   2874 int
   2875 compat_netbsd32_mkdir(p, v, retval)
   2876 	struct proc *p;
   2877 	void *v;
   2878 	register_t *retval;
   2879 {
   2880 	struct compat_netbsd32_mkdir_args /* {
   2881 		syscallarg(const netbsd32_charp) path;
   2882 		syscallarg(mode_t) mode;
   2883 	} */ *uap = v;
   2884 	struct sys_mkdir_args ua;
   2885 
   2886 	NETBSD32TOP_UAP(path, const char)
   2887 	NETBSD32TO64_UAP(mode);
   2888 	return (sys_mkdir(p, &ua, retval));
   2889 }
   2890 
   2891 int
   2892 compat_netbsd32_rmdir(p, v, retval)
   2893 	struct proc *p;
   2894 	void *v;
   2895 	register_t *retval;
   2896 {
   2897 	struct compat_netbsd32_rmdir_args /* {
   2898 		syscallarg(const netbsd32_charp) path;
   2899 	} */ *uap = v;
   2900 	struct sys_rmdir_args ua;
   2901 
   2902 	NETBSD32TOP_UAP(path, const char);
   2903 	return (sys_rmdir(p, &ua, retval));
   2904 }
   2905 
   2906 int
   2907 compat_netbsd32_utimes(p, v, retval)
   2908 	struct proc *p;
   2909 	void *v;
   2910 	register_t *retval;
   2911 {
   2912 	struct compat_netbsd32_utimes_args /* {
   2913 		syscallarg(const netbsd32_charp) path;
   2914 		syscallarg(const netbsd32_timevalp_t) tptr;
   2915 	} */ *uap = v;
   2916 	int error;
   2917 	struct nameidata nd;
   2918 
   2919 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (char *)(u_long)SCARG(uap, path), p);
   2920 	if ((error = namei(&nd)) != 0)
   2921 		return (error);
   2922 
   2923 	error = change_utimes32(nd.ni_vp, (struct timeval *)(u_long)SCARG(uap, tptr), p);
   2924 
   2925 	vrele(nd.ni_vp);
   2926 	return (error);
   2927 }
   2928 
   2929 /*
   2930  * Common routine to set access and modification times given a vnode.
   2931  */
   2932 static int
   2933 change_utimes32(vp, tptr, p)
   2934 	struct vnode *vp;
   2935 	struct timeval *tptr;
   2936 	struct proc *p;
   2937 {
   2938 	struct netbsd32_timeval tv32[2];
   2939 	struct timeval tv[2];
   2940 	struct vattr vattr;
   2941 	int error;
   2942 
   2943 	VATTR_NULL(&vattr);
   2944 	if (tptr == NULL) {
   2945 		microtime(&tv[0]);
   2946 		tv[1] = tv[0];
   2947 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2948 	} else {
   2949 		error = copyin(tptr, tv, sizeof(tv));
   2950 		if (error)
   2951 			return (error);
   2952 	}
   2953 	netbsd32_to_timeval(&tv32[0], &tv[0]);
   2954 	netbsd32_to_timeval(&tv32[1], &tv[1]);
   2955 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2956 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2957 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   2958 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   2959 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   2960 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   2961 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2962 	VOP_UNLOCK(vp, 0);
   2963 	return (error);
   2964 }
   2965 
   2966 int
   2967 compat_netbsd32_adjtime(p, v, retval)
   2968 	struct proc *p;
   2969 	void *v;
   2970 	register_t *retval;
   2971 {
   2972 	struct compat_netbsd32_adjtime_args /* {
   2973 		syscallarg(const netbsd32_timevalp_t) delta;
   2974 		syscallarg(netbsd32_timevalp_t) olddelta;
   2975 	} */ *uap = v;
   2976 	struct netbsd32_timeval atv;
   2977 	int32_t ndelta, ntickdelta, odelta;
   2978 	int s, error;
   2979 	extern long bigadj, timedelta;
   2980 	extern int tickdelta;
   2981 
   2982 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   2983 		return (error);
   2984 
   2985 	error = copyin((caddr_t)(u_long)SCARG(uap, delta), &atv, sizeof(struct timeval));
   2986 	if (error)
   2987 		return (error);
   2988 	/*
   2989 	 * Compute the total correction and the rate at which to apply it.
   2990 	 * Round the adjustment down to a whole multiple of the per-tick
   2991 	 * delta, so that after some number of incremental changes in
   2992 	 * hardclock(), tickdelta will become zero, lest the correction
   2993 	 * overshoot and start taking us away from the desired final time.
   2994 	 */
   2995 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
   2996 	if (ndelta > bigadj)
   2997 		ntickdelta = 10 * tickadj;
   2998 	else
   2999 		ntickdelta = tickadj;
   3000 	if (ndelta % ntickdelta)
   3001 		ndelta = ndelta / ntickdelta * ntickdelta;
   3002 
   3003 	/*
   3004 	 * To make hardclock()'s job easier, make the per-tick delta negative
   3005 	 * if we want time to run slower; then hardclock can simply compute
   3006 	 * tick + tickdelta, and subtract tickdelta from timedelta.
   3007 	 */
   3008 	if (ndelta < 0)
   3009 		ntickdelta = -ntickdelta;
   3010 	s = splclock();
   3011 	odelta = timedelta;
   3012 	timedelta = ndelta;
   3013 	tickdelta = ntickdelta;
   3014 	splx(s);
   3015 
   3016 	if (SCARG(uap, olddelta)) {
   3017 		atv.tv_sec = odelta / 1000000;
   3018 		atv.tv_usec = odelta % 1000000;
   3019 		(void) copyout(&atv, (caddr_t)(u_long)SCARG(uap, olddelta),
   3020 		    sizeof(struct timeval));
   3021 	}
   3022 	return (0);
   3023 }
   3024 
   3025 int
   3026 compat_netbsd32_quotactl(p, v, retval)
   3027 	struct proc *p;
   3028 	void *v;
   3029 	register_t *retval;
   3030 {
   3031 	struct compat_netbsd32_quotactl_args /* {
   3032 		syscallarg(const netbsd32_charp) path;
   3033 		syscallarg(int) cmd;
   3034 		syscallarg(int) uid;
   3035 		syscallarg(netbsd32_caddr_t) arg;
   3036 	} */ *uap = v;
   3037 	struct sys_quotactl_args ua;
   3038 
   3039 	NETBSD32TOP_UAP(path, const char);
   3040 	NETBSD32TO64_UAP(cmd);
   3041 	NETBSD32TO64_UAP(uid);
   3042 	NETBSD32TOX64_UAP(arg, caddr_t);
   3043 	return (sys_quotactl(p, &ua, retval));
   3044 }
   3045 
   3046 #if defined(NFS) || defined(NFSSERVER)
   3047 int
   3048 compat_netbsd32_nfssvc(p, v, retval)
   3049 	struct proc *p;
   3050 	void *v;
   3051 	register_t *retval;
   3052 {
   3053 #if 0
   3054 	struct compat_netbsd32_nfssvc_args /* {
   3055 		syscallarg(int) flag;
   3056 		syscallarg(netbsd32_voidp) argp;
   3057 	} */ *uap = v;
   3058 	struct sys_nfssvc_args ua;
   3059 
   3060 	NETBSD32TO64_UAP(flag);
   3061 	NETBSD32TOP_UAP(argp, void);
   3062 	return (sys_nfssvc(p, &ua, retval));
   3063 #else
   3064 	/* Why would we want to support a 32-bit nfsd? */
   3065 	return (ENOSYS);
   3066 #endif
   3067 }
   3068 #endif
   3069 
   3070 int
   3071 compat_netbsd32_statfs(p, v, retval)
   3072 	struct proc *p;
   3073 	void *v;
   3074 	register_t *retval;
   3075 {
   3076 	struct compat_netbsd32_statfs_args /* {
   3077 		syscallarg(const netbsd32_charp) path;
   3078 		syscallarg(netbsd32_statfsp_t) buf;
   3079 	} */ *uap = v;
   3080 	register struct mount *mp;
   3081 	register struct statfs *sp;
   3082 	struct netbsd32_statfs s32;
   3083 	int error;
   3084 	struct nameidata nd;
   3085 
   3086 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (char *)(u_long)SCARG(uap, path), p);
   3087 	if ((error = namei(&nd)) != 0)
   3088 		return (error);
   3089 	mp = nd.ni_vp->v_mount;
   3090 	sp = &mp->mnt_stat;
   3091 	vrele(nd.ni_vp);
   3092 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   3093 		return (error);
   3094 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   3095 	netbsd32_from_statfs(sp, &s32);
   3096 	return (copyout(&s32, (caddr_t)(u_long)SCARG(uap, buf), sizeof(s32)));
   3097 }
   3098 
   3099 int
   3100 compat_netbsd32_fstatfs(p, v, retval)
   3101 	struct proc *p;
   3102 	void *v;
   3103 	register_t *retval;
   3104 {
   3105 	struct compat_netbsd32_fstatfs_args /* {
   3106 		syscallarg(int) fd;
   3107 		syscallarg(netbsd32_statfsp_t) buf;
   3108 	} */ *uap = v;
   3109 	struct file *fp;
   3110 	register struct mount *mp;
   3111 	register struct statfs *sp;
   3112 	struct netbsd32_statfs s32;
   3113 	int error;
   3114 
   3115 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3116 		return (error);
   3117 	mp = ((struct vnode *)fp->f_data)->v_mount;
   3118 	sp = &mp->mnt_stat;
   3119 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   3120 		return (error);
   3121 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   3122 	netbsd32_from_statfs(sp, &s32);
   3123 	return (copyout(&s32, (caddr_t)(u_long)SCARG(uap, buf), sizeof(s32)));
   3124 }
   3125 
   3126 #if defined(NFS) || defined(NFSSERVER)
   3127 int
   3128 compat_netbsd32_getfh(p, v, retval)
   3129 	struct proc *p;
   3130 	void *v;
   3131 	register_t *retval;
   3132 {
   3133 	struct compat_netbsd32_getfh_args /* {
   3134 		syscallarg(const netbsd32_charp) fname;
   3135 		syscallarg(netbsd32_fhandlep_t) fhp;
   3136 	} */ *uap = v;
   3137 	struct sys_getfh_args ua;
   3138 
   3139 	NETBSD32TOP_UAP(fname, const char);
   3140 	NETBSD32TOP_UAP(fhp, struct fhandle);
   3141 	/* Lucky for us a fhandlep_t doesn't change sizes */
   3142 	return (sys_getfh(p, &ua, retval));
   3143 }
   3144 #endif
   3145 
   3146 int
   3147 compat_netbsd32_sysarch(p, v, retval)
   3148 	struct proc *p;
   3149 	void *v;
   3150 	register_t *retval;
   3151 {
   3152 	struct compat_netbsd32_sysarch_args /* {
   3153 		syscallarg(int) op;
   3154 		syscallarg(netbsd32_voidp) parms;
   3155 	} */ *uap = v;
   3156 
   3157 	switch (SCARG(uap, op)) {
   3158 	default:
   3159 		printf("(sparc64) compat_netbsd32_sysarch(%d)\n", SCARG(uap, op));
   3160 		return EINVAL;
   3161 	}
   3162 }
   3163 
   3164 int
   3165 compat_netbsd32_pread(p, v, retval)
   3166 	struct proc *p;
   3167 	void *v;
   3168 	register_t *retval;
   3169 {
   3170 	struct compat_netbsd32_pread_args /* {
   3171 		syscallarg(int) fd;
   3172 		syscallarg(netbsd32_voidp) buf;
   3173 		syscallarg(netbsd32_size_t) nbyte;
   3174 		syscallarg(int) pad;
   3175 		syscallarg(off_t) offset;
   3176 	} */ *uap = v;
   3177 	struct sys_pread_args ua;
   3178 	ssize_t rt;
   3179 	int error;
   3180 
   3181 	NETBSD32TO64_UAP(fd);
   3182 	NETBSD32TOP_UAP(buf, void);
   3183 	NETBSD32TOX_UAP(nbyte, size_t);
   3184 	NETBSD32TO64_UAP(pad);
   3185 	NETBSD32TO64_UAP(offset);
   3186 	error = sys_pread(p, &ua, (register_t *)&rt);
   3187 	*(netbsd32_ssize_t *)retval = rt;
   3188 	return (error);
   3189 }
   3190 
   3191 int
   3192 compat_netbsd32_pwrite(p, v, retval)
   3193 	struct proc *p;
   3194 	void *v;
   3195 	register_t *retval;
   3196 {
   3197 	struct compat_netbsd32_pwrite_args /* {
   3198 		syscallarg(int) fd;
   3199 		syscallarg(const netbsd32_voidp) buf;
   3200 		syscallarg(netbsd32_size_t) nbyte;
   3201 		syscallarg(int) pad;
   3202 		syscallarg(off_t) offset;
   3203 	} */ *uap = v;
   3204 	struct sys_pwrite_args ua;
   3205 	ssize_t rt;
   3206 	int error;
   3207 
   3208 	NETBSD32TO64_UAP(fd);
   3209 	NETBSD32TOP_UAP(buf, void);
   3210 	NETBSD32TOX_UAP(nbyte, size_t);
   3211 	NETBSD32TO64_UAP(pad);
   3212 	NETBSD32TO64_UAP(offset);
   3213 	error = sys_pwrite(p, &ua, (register_t *)&rt);
   3214 	*(netbsd32_ssize_t *)retval = rt;
   3215 	return (error);
   3216 }
   3217 
   3218 #ifdef NTP
   3219 int
   3220 compat_netbsd32_ntp_gettime(p, v, retval)
   3221 	struct proc *p;
   3222 	void *v;
   3223 	register_t *retval;
   3224 {
   3225 	struct compat_netbsd32_ntp_gettime_args /* {
   3226 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
   3227 	} */ *uap = v;
   3228 	struct netbsd32_ntptimeval ntv32;
   3229 	struct timeval atv;
   3230 	struct ntptimeval ntv;
   3231 	int error = 0;
   3232 	int s;
   3233 
   3234 	/* The following are NTP variables */
   3235 	extern long time_maxerror;
   3236 	extern long time_esterror;
   3237 	extern int time_status;
   3238 	extern int time_state;	/* clock state */
   3239 	extern int time_status;	/* clock status bits */
   3240 
   3241 	if (SCARG(uap, ntvp)) {
   3242 		s = splclock();
   3243 #ifdef EXT_CLOCK
   3244 		/*
   3245 		 * The microtime() external clock routine returns a
   3246 		 * status code. If less than zero, we declare an error
   3247 		 * in the clock status word and return the kernel
   3248 		 * (software) time variable. While there are other
   3249 		 * places that call microtime(), this is the only place
   3250 		 * that matters from an application point of view.
   3251 		 */
   3252 		if (microtime(&atv) < 0) {
   3253 			time_status |= STA_CLOCKERR;
   3254 			ntv.time = time;
   3255 		} else
   3256 			time_status &= ~STA_CLOCKERR;
   3257 #else /* EXT_CLOCK */
   3258 		microtime(&atv);
   3259 #endif /* EXT_CLOCK */
   3260 		ntv.time = atv;
   3261 		ntv.maxerror = time_maxerror;
   3262 		ntv.esterror = time_esterror;
   3263 		(void) splx(s);
   3264 
   3265 		netbsd32_from_timeval(&ntv.time, &ntv32.time);
   3266 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
   3267 		ntv32.esterror = (netbsd32_long)ntv.esterror;
   3268 		error = copyout((caddr_t)&ntv32, (caddr_t)(u_long)SCARG(uap, ntvp),
   3269 		    sizeof(ntv32));
   3270 	}
   3271 	if (!error) {
   3272 
   3273 		/*
   3274 		 * Status word error decode. If any of these conditions
   3275 		 * occur, an error is returned, instead of the status
   3276 		 * word. Most applications will care only about the fact
   3277 		 * the system clock may not be trusted, not about the
   3278 		 * details.
   3279 		 *
   3280 		 * Hardware or software error
   3281 		 */
   3282 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   3283 
   3284 		/*
   3285 		 * PPS signal lost when either time or frequency
   3286 		 * synchronization requested
   3287 		 */
   3288 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   3289 		    !(time_status & STA_PPSSIGNAL)) ||
   3290 
   3291 		/*
   3292 		 * PPS jitter exceeded when time synchronization
   3293 		 * requested
   3294 		 */
   3295 		    (time_status & STA_PPSTIME &&
   3296 		    time_status & STA_PPSJITTER) ||
   3297 
   3298 		/*
   3299 		 * PPS wander exceeded or calibration error when
   3300 		 * frequency synchronization requested
   3301 		 */
   3302 		    (time_status & STA_PPSFREQ &&
   3303 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
   3304 			*retval = TIME_ERROR;
   3305 		else
   3306 			*retval = (register_t)time_state;
   3307 	}
   3308 	return(error);
   3309 }
   3310 
   3311 int
   3312 compat_netbsd32_ntp_adjtime(p, v, retval)
   3313 	struct proc *p;
   3314 	void *v;
   3315 	register_t *retval;
   3316 {
   3317 	struct compat_netbsd32_ntp_adjtime_args /* {
   3318 		syscallarg(netbsd32_timexp_t) tp;
   3319 	} */ *uap = v;
   3320 	struct netbsd32_timex ntv32;
   3321 	struct timex ntv;
   3322 	int error = 0;
   3323 	int modes;
   3324 	int s;
   3325 	extern long time_freq;		/* frequency offset (scaled ppm) */
   3326 	extern long time_maxerror;
   3327 	extern long time_esterror;
   3328 	extern int time_state;	/* clock state */
   3329 	extern int time_status;	/* clock status bits */
   3330 	extern long time_constant;		/* pll time constant */
   3331 	extern long time_offset;		/* time offset (us) */
   3332 	extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
   3333 	extern long time_precision;	/* clock precision (us) */
   3334 
   3335 	if ((error = copyin((caddr_t)(u_long)SCARG(uap, tp), (caddr_t)&ntv32,
   3336 			sizeof(ntv32))))
   3337 		return (error);
   3338 	netbsd32_to_timex(&ntv32, &ntv);
   3339 
   3340 	/*
   3341 	 * Update selected clock variables - only the superuser can
   3342 	 * change anything. Note that there is no error checking here on
   3343 	 * the assumption the superuser should know what it is doing.
   3344 	 */
   3345 	modes = ntv.modes;
   3346 	if (modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)))
   3347 		return (error);
   3348 
   3349 	s = splclock();
   3350 	if (modes & MOD_FREQUENCY)
   3351 #ifdef PPS_SYNC
   3352 		time_freq = ntv.freq - pps_freq;
   3353 #else /* PPS_SYNC */
   3354 		time_freq = ntv.freq;
   3355 #endif /* PPS_SYNC */
   3356 	if (modes & MOD_MAXERROR)
   3357 		time_maxerror = ntv.maxerror;
   3358 	if (modes & MOD_ESTERROR)
   3359 		time_esterror = ntv.esterror;
   3360 	if (modes & MOD_STATUS) {
   3361 		time_status &= STA_RONLY;
   3362 		time_status |= ntv.status & ~STA_RONLY;
   3363 	}
   3364 	if (modes & MOD_TIMECONST)
   3365 		time_constant = ntv.constant;
   3366 	if (modes & MOD_OFFSET)
   3367 		hardupdate(ntv.offset);
   3368 
   3369 	/*
   3370 	 * Retrieve all clock variables
   3371 	 */
   3372 	if (time_offset < 0)
   3373 		ntv.offset = -(-time_offset >> SHIFT_UPDATE);
   3374 	else
   3375 		ntv.offset = time_offset >> SHIFT_UPDATE;
   3376 #ifdef PPS_SYNC
   3377 	ntv.freq = time_freq + pps_freq;
   3378 #else /* PPS_SYNC */
   3379 	ntv.freq = time_freq;
   3380 #endif /* PPS_SYNC */
   3381 	ntv.maxerror = time_maxerror;
   3382 	ntv.esterror = time_esterror;
   3383 	ntv.status = time_status;
   3384 	ntv.constant = time_constant;
   3385 	ntv.precision = time_precision;
   3386 	ntv.tolerance = time_tolerance;
   3387 #ifdef PPS_SYNC
   3388 	ntv.shift = pps_shift;
   3389 	ntv.ppsfreq = pps_freq;
   3390 	ntv.jitter = pps_jitter >> PPS_AVG;
   3391 	ntv.stabil = pps_stabil;
   3392 	ntv.calcnt = pps_calcnt;
   3393 	ntv.errcnt = pps_errcnt;
   3394 	ntv.jitcnt = pps_jitcnt;
   3395 	ntv.stbcnt = pps_stbcnt;
   3396 #endif /* PPS_SYNC */
   3397 	(void)splx(s);
   3398 
   3399 	netbsd32_from_timeval(&ntv, &ntv32);
   3400 	error = copyout((caddr_t)&ntv32, (caddr_t)SCARG(uap, tp), sizeof(ntv32));
   3401 	if (!error) {
   3402 
   3403 		/*
   3404 		 * Status word error decode. See comments in
   3405 		 * ntp_gettime() routine.
   3406 		 */
   3407 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   3408 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   3409 		    !(time_status & STA_PPSSIGNAL)) ||
   3410 		    (time_status & STA_PPSTIME &&
   3411 		    time_status & STA_PPSJITTER) ||
   3412 		    (time_status & STA_PPSFREQ &&
   3413 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
   3414 			*retval = TIME_ERROR;
   3415 		else
   3416 			*retval = (register_t)time_state;
   3417 	}
   3418 	return error;
   3419 }
   3420 #endif
   3421 
   3422 int
   3423 compat_netbsd32_setgid(p, v, retval)
   3424 	struct proc *p;
   3425 	void *v;
   3426 	register_t *retval;
   3427 {
   3428 	struct compat_netbsd32_setgid_args /* {
   3429 		syscallarg(gid_t) gid;
   3430 	} */ *uap = v;
   3431 	struct sys_setgid_args ua;
   3432 
   3433 	NETBSD32TO64_UAP(gid);
   3434 	return (sys_setgid(p, v, retval));
   3435 }
   3436 
   3437 int
   3438 compat_netbsd32_setegid(p, v, retval)
   3439 	struct proc *p;
   3440 	void *v;
   3441 	register_t *retval;
   3442 {
   3443 	struct compat_netbsd32_setegid_args /* {
   3444 		syscallarg(gid_t) egid;
   3445 	} */ *uap = v;
   3446 	struct sys_setegid_args ua;
   3447 
   3448 	NETBSD32TO64_UAP(egid);
   3449 	return (sys_setegid(p, v, retval));
   3450 }
   3451 
   3452 int
   3453 compat_netbsd32_seteuid(p, v, retval)
   3454 	struct proc *p;
   3455 	void *v;
   3456 	register_t *retval;
   3457 {
   3458 	struct compat_netbsd32_seteuid_args /* {
   3459 		syscallarg(gid_t) euid;
   3460 	} */ *uap = v;
   3461 	struct sys_seteuid_args ua;
   3462 
   3463 	NETBSD32TO64_UAP(euid);
   3464 	return (sys_seteuid(p, v, retval));
   3465 }
   3466 
   3467 #ifdef LFS
   3468 int
   3469 compat_netbsd32_lfs_bmapv(p, v, retval)
   3470 	struct proc *p;
   3471 	void *v;
   3472 	register_t *retval;
   3473 {
   3474 #if 0
   3475 	struct compat_netbsd32_lfs_bmapv_args /* {
   3476 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3477 		syscallarg(netbsd32_block_infop_t) blkiov;
   3478 		syscallarg(int) blkcnt;
   3479 	} */ *uap = v;
   3480 	struct sys_lfs_bmapv_args ua;
   3481 
   3482 	NETBSD32TOP_UAP(fdidp, struct fsid);
   3483 	NETBSD32TO64_UAP(blkcnt);
   3484 	/* XXX finish me */
   3485 #else
   3486 
   3487 	return (ENOSYS);	/* XXX */
   3488 #endif
   3489 }
   3490 
   3491 int
   3492 compat_netbsd32_lfs_markv(p, v, retval)
   3493 	struct proc *p;
   3494 	void *v;
   3495 	register_t *retval;
   3496 {
   3497 	struct compat_netbsd32_lfs_markv_args /* {
   3498 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3499 		syscallarg(netbsd32_block_infop_t) blkiov;
   3500 		syscallarg(int) blkcnt;
   3501 	} */ *uap = v;
   3502 
   3503 	return (ENOSYS);	/* XXX */
   3504 }
   3505 
   3506 int
   3507 compat_netbsd32_lfs_segclean(p, v, retval)
   3508 	struct proc *p;
   3509 	void *v;
   3510 	register_t *retval;
   3511 {
   3512 	struct compat_netbsd32_lfs_segclean_args /* {
   3513 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3514 		syscallarg(netbsd32_u_long) segment;
   3515 	} */ *uap = v;
   3516 	return (ENOSYS);	/* XXX */
   3517 }
   3518 
   3519 int
   3520 compat_netbsd32_lfs_segwait(p, v, retval)
   3521 	struct proc *p;
   3522 	void *v;
   3523 	register_t *retval;
   3524 {
   3525 	struct compat_netbsd32_lfs_segwait_args /* {
   3526 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3527 		syscallarg(netbsd32_timevalp_t) tv;
   3528 	} */ *uap = v;
   3529 	return (ENOSYS);	/* XXX */
   3530 }
   3531 #endif
   3532 
   3533 int
   3534 compat_netbsd32_pathconf(p, v, retval)
   3535 	struct proc *p;
   3536 	void *v;
   3537 	register_t *retval;
   3538 {
   3539 	struct compat_netbsd32_pathconf_args /* {
   3540 		syscallarg(int) fd;
   3541 		syscallarg(int) name;
   3542 	} */ *uap = v;
   3543 	struct sys_pathconf_args ua;
   3544 	long rt;
   3545 	int error;
   3546 
   3547 	NETBSD32TOP_UAP(path, const char);
   3548 	NETBSD32TO64_UAP(name);
   3549 	error = sys_pathconf(p, &ua, (register_t *)&rt);
   3550 	*(netbsd32_long *)retval = (netbsd32_long)rt;
   3551 	return (error);
   3552 }
   3553 
   3554 int
   3555 compat_netbsd32_fpathconf(p, v, retval)
   3556 	struct proc *p;
   3557 	void *v;
   3558 	register_t *retval;
   3559 {
   3560 	struct compat_netbsd32_fpathconf_args /* {
   3561 		syscallarg(int) fd;
   3562 		syscallarg(int) name;
   3563 	} */ *uap = v;
   3564 	struct sys_fpathconf_args ua;
   3565 	long rt;
   3566 	int error;
   3567 
   3568 	NETBSD32TO64_UAP(fd);
   3569 	NETBSD32TO64_UAP(name);
   3570 	error = sys_fpathconf(p, &ua, (register_t *)&rt);
   3571 	*(netbsd32_long *)retval = (netbsd32_long)rt;
   3572 	return (error);
   3573 }
   3574 
   3575 int
   3576 compat_netbsd32_getrlimit(p, v, retval)
   3577 	struct proc *p;
   3578 	void *v;
   3579 	register_t *retval;
   3580 {
   3581 	struct compat_netbsd32_getrlimit_args /* {
   3582 		syscallarg(int) which;
   3583 		syscallarg(netbsd32_rlimitp_t) rlp;
   3584 	} */ *uap = v;
   3585 	int which = SCARG(uap, which);
   3586 
   3587 	if ((u_int)which >= RLIM_NLIMITS)
   3588 		return (EINVAL);
   3589 	return (copyout(&p->p_rlimit[which], (caddr_t)(u_long)SCARG(uap, rlp),
   3590 	    sizeof(struct rlimit)));
   3591 }
   3592 
   3593 int
   3594 compat_netbsd32_setrlimit(p, v, retval)
   3595 	struct proc *p;
   3596 	void *v;
   3597 	register_t *retval;
   3598 {
   3599 	struct compat_netbsd32_setrlimit_args /* {
   3600 		syscallarg(int) which;
   3601 		syscallarg(const netbsd32_rlimitp_t) rlp;
   3602 	} */ *uap = v;
   3603 		int which = SCARG(uap, which);
   3604 	struct rlimit alim;
   3605 	int error;
   3606 
   3607 	error = copyin((caddr_t)(u_long)SCARG(uap, rlp), &alim, sizeof(struct rlimit));
   3608 	if (error)
   3609 		return (error);
   3610 	return (dosetrlimit(p, which, &alim));
   3611 }
   3612 
   3613 int
   3614 compat_netbsd32_mmap(p, v, retval)
   3615 	struct proc *p;
   3616 	void *v;
   3617 	register_t *retval;
   3618 {
   3619 	struct compat_netbsd32_mmap_args /* {
   3620 		syscallarg(netbsd32_voidp) addr;
   3621 		syscallarg(netbsd32_size_t) len;
   3622 		syscallarg(int) prot;
   3623 		syscallarg(int) flags;
   3624 		syscallarg(int) fd;
   3625 		syscallarg(netbsd32_long) pad;
   3626 		syscallarg(off_t) pos;
   3627 	} */ *uap = v;
   3628 	struct sys_mmap_args ua;
   3629 	void *rt;
   3630 	int error;
   3631 
   3632 	NETBSD32TOP_UAP(addr, void);
   3633 	NETBSD32TOX_UAP(len, size_t);
   3634 	NETBSD32TO64_UAP(prot);
   3635 	NETBSD32TO64_UAP(flags);
   3636 	NETBSD32TO64_UAP(fd);
   3637 	NETBSD32TOX_UAP(pad, long);
   3638 	NETBSD32TOX_UAP(pos, off_t);
   3639 	error = sys_mmap(p, &ua, (register_t *)&rt);
   3640 	if ((long)rt > (long)UINT_MAX)
   3641 		printf("compat_netbsd32_mmap: retval out of range: 0x%qx",
   3642 		    rt);
   3643 	*retval = (netbsd32_voidp)(u_long)rt;
   3644 	return (error);
   3645 }
   3646 
   3647 int
   3648 compat_netbsd32_lseek(p, v, retval)
   3649 	struct proc *p;
   3650 	void *v;
   3651 	register_t *retval;
   3652 {
   3653 	struct compat_netbsd32_lseek_args /* {
   3654 		syscallarg(int) fd;
   3655 		syscallarg(int) pad;
   3656 		syscallarg(off_t) offset;
   3657 		syscallarg(int) whence;
   3658 	} */ *uap = v;
   3659 	struct sys_lseek_args ua;
   3660 
   3661 	NETBSD32TO64_UAP(fd);
   3662 	NETBSD32TO64_UAP(pad);
   3663 	NETBSD32TO64_UAP(offset);
   3664 	NETBSD32TO64_UAP(whence);
   3665 	return (sys_lseek(p, &ua, retval));
   3666 }
   3667 
   3668 int
   3669 compat_netbsd32_truncate(p, v, retval)
   3670 	struct proc *p;
   3671 	void *v;
   3672 	register_t *retval;
   3673 {
   3674 	struct compat_netbsd32_truncate_args /* {
   3675 		syscallarg(const netbsd32_charp) path;
   3676 		syscallarg(int) pad;
   3677 		syscallarg(off_t) length;
   3678 	} */ *uap = v;
   3679 	struct sys_truncate_args ua;
   3680 
   3681 	NETBSD32TOP_UAP(path, const char);
   3682 	NETBSD32TO64_UAP(pad);
   3683 	NETBSD32TO64_UAP(length);
   3684 	return (sys_truncate(p, &ua, retval));
   3685 }
   3686 
   3687 int
   3688 compat_netbsd32_ftruncate(p, v, retval)
   3689 	struct proc *p;
   3690 	void *v;
   3691 	register_t *retval;
   3692 {
   3693 	struct compat_netbsd32_ftruncate_args /* {
   3694 		syscallarg(int) fd;
   3695 		syscallarg(int) pad;
   3696 		syscallarg(off_t) length;
   3697 	} */ *uap = v;
   3698 	struct sys_ftruncate_args ua;
   3699 
   3700 	NETBSD32TO64_UAP(fd);
   3701 	NETBSD32TO64_UAP(pad);
   3702 	NETBSD32TO64_UAP(length);
   3703 	return (sys_ftruncate(p, &ua, retval));
   3704 }
   3705 
   3706 int
   3707 compat_netbsd32___sysctl(p, v, retval)
   3708 	struct proc *p;
   3709 	void *v;
   3710 	register_t *retval;
   3711 {
   3712 	struct compat_netbsd32___sysctl_args /* {
   3713 		syscallarg(netbsd32_intp) name;
   3714 		syscallarg(u_int) namelen;
   3715 		syscallarg(netbsd32_voidp) old;
   3716 		syscallarg(netbsd32_size_tp) oldlenp;
   3717 		syscallarg(netbsd32_voidp) new;
   3718 		syscallarg(netbsd32_size_t) newlen;
   3719 	} */ *uap = v;
   3720 	int error, dolock = 1;
   3721 	netbsd32_size_t savelen = 0;
   3722 	size_t oldlen = 0;
   3723 	sysctlfn *fn;
   3724 	int name[CTL_MAXNAME];
   3725 
   3726 /*
   3727  * Some of these sysctl functions do their own copyin/copyout.
   3728  * We need to disable or emulate the ones that need their
   3729  * arguments converted.
   3730  */
   3731 
   3732 	if (SCARG(uap, new) != NULL &&
   3733 	    (error = suser(p->p_ucred, &p->p_acflag)))
   3734 		return (error);
   3735 	/*
   3736 	 * all top-level sysctl names are non-terminal
   3737 	 */
   3738 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
   3739 		return (EINVAL);
   3740 	error = copyin((caddr_t)(u_long)SCARG(uap, name), &name,
   3741 		       SCARG(uap, namelen) * sizeof(int));
   3742 	if (error)
   3743 		return (error);
   3744 
   3745 	switch (name[0]) {
   3746 	case CTL_KERN:
   3747 		fn = kern_sysctl;
   3748 		if (name[2] != KERN_VNODE)	/* XXX */
   3749 			dolock = 0;
   3750 		break;
   3751 	case CTL_HW:
   3752 		fn = hw_sysctl;
   3753 		break;
   3754 	case CTL_VM:
   3755 		fn = uvm_sysctl;
   3756 		break;
   3757 	case CTL_NET:
   3758 		fn = net_sysctl;
   3759 		break;
   3760 	case CTL_VFS:
   3761 		fn = vfs_sysctl;
   3762 		break;
   3763 	case CTL_MACHDEP:
   3764 		fn = cpu_sysctl;
   3765 		break;
   3766 #ifdef DEBUG
   3767 	case CTL_DEBUG:
   3768 		fn = debug_sysctl;
   3769 		break;
   3770 #endif
   3771 #ifdef DDB
   3772 	case CTL_DDB:
   3773 		fn = ddb_sysctl;
   3774 		break;
   3775 #endif
   3776 	default:
   3777 		return (EOPNOTSUPP);
   3778 	}
   3779 
   3780 	if (SCARG(uap, oldlenp) &&
   3781 	    (error = copyin((caddr_t)(u_long)SCARG(uap, oldlenp), &savelen, sizeof(savelen))))
   3782 		return (error);
   3783 	if (SCARG(uap, old) != NULL) {
   3784 		if (!uvm_useracc((caddr_t)(u_long)SCARG(uap, old), savelen, B_WRITE))
   3785 			return (EFAULT);
   3786 #if 0 /* XXXXXXXX */
   3787 		while (memlock.sl_lock) {
   3788 			memlock.sl_want = 1;
   3789 			sleep((caddr_t)&memlock, PRIBIO+1);
   3790 			memlock.sl_locked++;
   3791 		}
   3792 		memlock.sl_lock = 1;
   3793 #endif /* XXXXXXXX */
   3794 		if (dolock)
   3795 			uvm_vslock(p, SCARG(uap, old), savelen);
   3796 		oldlen = savelen;
   3797 	}
   3798 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
   3799 	    &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
   3800 	if (SCARG(uap, old) != NULL) {
   3801 		if (dolock)
   3802 			uvm_vsunlock(p, SCARG(uap, old), savelen);
   3803 #if 0 /* XXXXXXXXXXX */
   3804 		memlock.sl_lock = 0;
   3805 		if (memlock.sl_want) {
   3806 			memlock.sl_want = 0;
   3807 			wakeup((caddr_t)&memlock);
   3808 		}
   3809 #endif /* XXXXXXXXX */
   3810 	}
   3811 	savelen = oldlen;
   3812 	if (error)
   3813 		return (error);
   3814 	if (SCARG(uap, oldlenp))
   3815 		error = copyout(&savelen, (caddr_t)(u_long)SCARG(uap, oldlenp), sizeof(savelen));
   3816 	return (error);
   3817 }
   3818 
   3819 int
   3820 compat_netbsd32_mlock(p, v, retval)
   3821 	struct proc *p;
   3822 	void *v;
   3823 	register_t *retval;
   3824 {
   3825 	struct compat_netbsd32_mlock_args /* {
   3826 		syscallarg(const netbsd32_voidp) addr;
   3827 		syscallarg(netbsd32_size_t) len;
   3828 	} */ *uap = v;
   3829 	struct sys_mlock_args ua;
   3830 
   3831 	NETBSD32TOP_UAP(addr, const void);
   3832 	NETBSD32TO64_UAP(len);
   3833 	return (sys_mlock(p, &ua, retval));
   3834 }
   3835 
   3836 int
   3837 compat_netbsd32_munlock(p, v, retval)
   3838 	struct proc *p;
   3839 	void *v;
   3840 	register_t *retval;
   3841 {
   3842 	struct compat_netbsd32_munlock_args /* {
   3843 		syscallarg(const netbsd32_voidp) addr;
   3844 		syscallarg(netbsd32_size_t) len;
   3845 	} */ *uap = v;
   3846 	struct sys_munlock_args ua;
   3847 
   3848 	NETBSD32TOP_UAP(addr, const void);
   3849 	NETBSD32TO64_UAP(len);
   3850 	return (sys_munlock(p, &ua, retval));
   3851 }
   3852 
   3853 int
   3854 compat_netbsd32_undelete(p, v, retval)
   3855 	struct proc *p;
   3856 	void *v;
   3857 	register_t *retval;
   3858 {
   3859 	struct compat_netbsd32_undelete_args /* {
   3860 		syscallarg(const netbsd32_charp) path;
   3861 	} */ *uap = v;
   3862 	struct sys_undelete_args ua;
   3863 
   3864 	NETBSD32TOP_UAP(path, const char);
   3865 	return (sys_undelete(p, &ua, retval));
   3866 }
   3867 
   3868 int
   3869 compat_netbsd32_futimes(p, v, retval)
   3870 	struct proc *p;
   3871 	void *v;
   3872 	register_t *retval;
   3873 {
   3874 	struct compat_netbsd32_futimes_args /* {
   3875 		syscallarg(int) fd;
   3876 		syscallarg(const netbsd32_timevalp_t) tptr;
   3877 	} */ *uap = v;
   3878 	int error;
   3879 	struct file *fp;
   3880 
   3881 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3882 		return (error);
   3883 
   3884 	return (change_utimes32((struct vnode *)fp->f_data,
   3885 				(struct timeval *)(u_long)SCARG(uap, tptr), p));
   3886 }
   3887 
   3888 int
   3889 compat_netbsd32_getpgid(p, v, retval)
   3890 	struct proc *p;
   3891 	void *v;
   3892 	register_t *retval;
   3893 {
   3894 	struct compat_netbsd32_getpgid_args /* {
   3895 		syscallarg(pid_t) pid;
   3896 	} */ *uap = v;
   3897 	struct sys_getpgid_args ua;
   3898 
   3899 	NETBSD32TO64_UAP(pid);
   3900 	return (sys_getpgid(p, &ua, retval));
   3901 }
   3902 
   3903 int
   3904 compat_netbsd32_reboot(p, v, retval)
   3905 	struct proc *p;
   3906 	void *v;
   3907 	register_t *retval;
   3908 {
   3909 	struct compat_netbsd32_reboot_args /* {
   3910 		syscallarg(int) opt;
   3911 		syscallarg(netbsd32_charp) bootstr;
   3912 	} */ *uap = v;
   3913 	struct sys_reboot_args ua;
   3914 
   3915 	NETBSD32TO64_UAP(opt);
   3916 	NETBSD32TOP_UAP(bootstr, char);
   3917 	return (sys_reboot(p, &ua, retval));
   3918 }
   3919 
   3920 int
   3921 compat_netbsd32_poll(p, v, retval)
   3922 	struct proc *p;
   3923 	void *v;
   3924 	register_t *retval;
   3925 {
   3926 	struct compat_netbsd32_poll_args /* {
   3927 		syscallarg(netbsd32_pollfdp_t) fds;
   3928 		syscallarg(u_int) nfds;
   3929 		syscallarg(int) timeout;
   3930 	} */ *uap = v;
   3931 	struct sys_poll_args ua;
   3932 
   3933 	NETBSD32TOP_UAP(fds, struct pollfd);
   3934 	NETBSD32TO64_UAP(nfds);
   3935 	NETBSD32TO64_UAP(timeout);
   3936 	return (sys_poll(p, &ua, retval));
   3937 }
   3938 
   3939 /*
   3940  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   3941  *
   3942  * This is BSD.  We won't support System V IPC.
   3943  * Too much work.
   3944  *
   3945  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   3946  */
   3947 int
   3948 compat_netbsd32___semctl(p, v, retval)
   3949 	struct proc *p;
   3950 	void *v;
   3951 	register_t *retval;
   3952 {
   3953 #if 0
   3954 	struct compat_netbsd32___semctl_args /* {
   3955 		syscallarg(int) semid;
   3956 		syscallarg(int) semnum;
   3957 		syscallarg(int) cmd;
   3958 		syscallarg(netbsd32_semunu_t) arg;
   3959 	} */ *uap = v;
   3960 	union netbsd32_semun sem32;
   3961 	int semid = SCARG(uap, semid);
   3962 	int semnum = SCARG(uap, semnum);
   3963 	int cmd = SCARG(uap, cmd);
   3964 	union netbsd32_semun *arg = (void*)(u_long)SCARG(uap, arg);
   3965 	union netbsd32_semun real_arg;
   3966 	struct ucred *cred = p->p_ucred;
   3967 	int i, rval, eval;
   3968 	struct netbsd32_semid_ds sbuf;
   3969 	register struct semid_ds *semaptr;
   3970 
   3971 	semlock(p);
   3972 
   3973 	semid = IPCID_TO_IX(semid);
   3974 	if (semid < 0 || semid >= seminfo.semmsl)
   3975 		return(EINVAL);
   3976 
   3977 	semaptr = &sema[semid];
   3978 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
   3979 	    semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid)))
   3980 		return(EINVAL);
   3981 
   3982 	eval = 0;
   3983 	rval = 0;
   3984 
   3985 	switch (cmd) {
   3986 	case IPC_RMID:
   3987 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
   3988 			return(eval);
   3989 		semaptr->sem_perm.cuid = cred->cr_uid;
   3990 		semaptr->sem_perm.uid = cred->cr_uid;
   3991 		semtot -= semaptr->sem_nsems;
   3992 		for (i = semaptr->sem_base - sem; i < semtot; i++)
   3993 			sem[i] = sem[i + semaptr->sem_nsems];
   3994 		for (i = 0; i < seminfo.semmni; i++) {
   3995 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
   3996 			    sema[i].sem_base > semaptr->sem_base)
   3997 				sema[i].sem_base -= semaptr->sem_nsems;
   3998 		}
   3999 		semaptr->sem_perm.mode = 0;
   4000 		semundo_clear(semid, -1);
   4001 		wakeup((caddr_t)semaptr);
   4002 		break;
   4003 
   4004 	case IPC_SET:
   4005 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
   4006 			return(eval);
   4007 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4008 			return(eval);
   4009 		if ((eval = copyin((caddr_t)(u_long)real_arg.buf, (caddr_t)&sbuf,
   4010 		    sizeof(sbuf))) != 0)
   4011 			return(eval);
   4012 		semaptr->sem_perm.uid = sbuf.sem_perm.uid;
   4013 		semaptr->sem_perm.gid = sbuf.sem_perm.gid;
   4014 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
   4015 		    (sbuf.sem_perm.mode & 0777);
   4016 		semaptr->sem_ctime = time.tv_sec;
   4017 		break;
   4018 
   4019 	case IPC_STAT:
   4020 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4021 			return(eval);
   4022 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4023 			return(eval);
   4024 		eval = copyout((caddr_t)semaptr, (caddr_t)(u_long)real_arg.buf,
   4025 		    sizeof(struct semid_ds));
   4026 		break;
   4027 
   4028 	case GETNCNT:
   4029 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4030 			return(eval);
   4031 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4032 			return(EINVAL);
   4033 		rval = semaptr->sem_base[semnum].semncnt;
   4034 		break;
   4035 
   4036 	case GETPID:
   4037 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4038 			return(eval);
   4039 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4040 			return(EINVAL);
   4041 		rval = semaptr->sem_base[semnum].sempid;
   4042 		break;
   4043 
   4044 	case GETVAL:
   4045 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4046 			return(eval);
   4047 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4048 			return(EINVAL);
   4049 		rval = semaptr->sem_base[semnum].semval;
   4050 		break;
   4051 
   4052 	case GETALL:
   4053 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4054 			return(eval);
   4055 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4056 			return(eval);
   4057 		for (i = 0; i < semaptr->sem_nsems; i++) {
   4058 			eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
   4059 			    &real_arg.array[i], sizeof(real_arg.array[0]));
   4060 			if (eval != 0)
   4061 				break;
   4062 		}
   4063 		break;
   4064 
   4065 	case GETZCNT:
   4066 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4067 			return(eval);
   4068 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4069 			return(EINVAL);
   4070 		rval = semaptr->sem_base[semnum].semzcnt;
   4071 		break;
   4072 
   4073 	case SETVAL:
   4074 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
   4075 			return(eval);
   4076 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4077 			return(EINVAL);
   4078 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4079 			return(eval);
   4080 		semaptr->sem_base[semnum].semval = real_arg.val;
   4081 		semundo_clear(semid, semnum);
   4082 		wakeup((caddr_t)semaptr);
   4083 		break;
   4084 
   4085 	case SETALL:
   4086 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
   4087 			return(eval);
   4088 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4089 			return(eval);
   4090 		for (i = 0; i < semaptr->sem_nsems; i++) {
   4091 			eval = copyin(&real_arg.array[i],
   4092 			    (caddr_t)&semaptr->sem_base[i].semval,
   4093 			    sizeof(real_arg.array[0]));
   4094 			if (eval != 0)
   4095 				break;
   4096 		}
   4097 		semundo_clear(semid, -1);
   4098 		wakeup((caddr_t)semaptr);
   4099 		break;
   4100 
   4101 	default:
   4102 		return(EINVAL);
   4103 	}
   4104 
   4105 	if (eval == 0)
   4106 		*retval = rval;
   4107 	return(eval);
   4108 #else
   4109 	return (ENOSYS);
   4110 #endif
   4111 }
   4112 
   4113 int
   4114 compat_netbsd32_semget(p, v, retval)
   4115 	struct proc *p;
   4116 	void *v;
   4117 	register_t *retval;
   4118 {
   4119 	struct compat_netbsd32_semget_args /* {
   4120 		syscallarg(netbsd32_key_t) key;
   4121 		syscallarg(int) nsems;
   4122 		syscallarg(int) semflg;
   4123 	} */ *uap = v;
   4124 	struct sys_semget_args ua;
   4125 
   4126 	NETBSD32TOX_UAP(key, key_t);
   4127 	NETBSD32TO64_UAP(nsems);
   4128 	NETBSD32TO64_UAP(semflg);
   4129 	return (sys_semget(p, &ua, retval));
   4130 }
   4131 
   4132 int
   4133 compat_netbsd32_semop(p, v, retval)
   4134 	struct proc *p;
   4135 	void *v;
   4136 	register_t *retval;
   4137 {
   4138 	struct compat_netbsd32_semop_args /* {
   4139 		syscallarg(int) semid;
   4140 		syscallarg(netbsd32_sembufp_t) sops;
   4141 		syscallarg(netbsd32_size_t) nsops;
   4142 	} */ *uap = v;
   4143 	struct sys_semop_args ua;
   4144 
   4145 	NETBSD32TO64_UAP(semid);
   4146 	NETBSD32TOP_UAP(sops, struct sembuf);
   4147 	NETBSD32TOX_UAP(nsops, size_t);
   4148 	return (sys_semop(p, &ua, retval));
   4149 }
   4150 
   4151 int
   4152 compat_netbsd32_semconfig(p, v, retval)
   4153 	struct proc *p;
   4154 	void *v;
   4155 	register_t *retval;
   4156 {
   4157 	struct compat_netbsd32_semconfig_args /* {
   4158 		syscallarg(int) flag;
   4159 	} */ *uap = v;
   4160 	struct sys_semconfig_args ua;
   4161 
   4162 	NETBSD32TO64_UAP(flag);
   4163 	return (sys_semconfig(p, &ua, retval));
   4164 }
   4165 
   4166 int
   4167 compat_netbsd32_msgctl(p, v, retval)
   4168 	struct proc *p;
   4169 	void *v;
   4170 	register_t *retval;
   4171 {
   4172 #if 0
   4173 	struct compat_netbsd32_msgctl_args /* {
   4174 		syscallarg(int) msqid;
   4175 		syscallarg(int) cmd;
   4176 		syscallarg(netbsd32_msqid_dsp_t) buf;
   4177 	} */ *uap = v;
   4178 	struct sys_msgctl_args ua;
   4179 	struct msqid_ds ds;
   4180 	struct netbsd32_msqid_ds *ds32p;
   4181 	int error;
   4182 
   4183 	NETBSD32TO64_UAP(msqid);
   4184 	NETBSD32TO64_UAP(cmd);
   4185 	ds32p = (struct netbsd32_msqid_ds *)(u_long)SCARG(uap, buf);
   4186 	if (ds32p) {
   4187 		SCARG(&ua, buf) = NULL;
   4188 		netbsd32_to_msqid_ds(ds32p, &ds);
   4189 	} else
   4190 		SCARG(&ua, buf) = NULL;
   4191 	error = sys_msgctl(p, &ua, retval);
   4192 	if (error)
   4193 		return (error);
   4194 
   4195 	if (ds32p)
   4196 		netbsd32_from_msqid_ds(&ds, ds32p);
   4197 	return (0);
   4198 #else
   4199 	return (ENOSYS);
   4200 #endif
   4201 }
   4202 
   4203 int
   4204 compat_netbsd32_msgget(p, v, retval)
   4205 	struct proc *p;
   4206 	void *v;
   4207 	register_t *retval;
   4208 {
   4209 #if 0
   4210 	struct compat_netbsd32_msgget_args /* {
   4211 		syscallarg(netbsd32_key_t) key;
   4212 		syscallarg(int) msgflg;
   4213 	} */ *uap = v;
   4214 	struct sys_msgget_args ua;
   4215 
   4216 	NETBSD32TOX_UAP(key, key_t);
   4217 	NETBSD32TO64_UAP(msgflg);
   4218 	return (sys_msgget(p, &ua, retval));
   4219 #else
   4220 	return (ENOSYS);
   4221 #endif
   4222 }
   4223 
   4224 int
   4225 compat_netbsd32_msgsnd(p, v, retval)
   4226 	struct proc *p;
   4227 	void *v;
   4228 	register_t *retval;
   4229 {
   4230 #if 0
   4231 	struct compat_netbsd32_msgsnd_args /* {
   4232 		syscallarg(int) msqid;
   4233 		syscallarg(const netbsd32_voidp) msgp;
   4234 		syscallarg(netbsd32_size_t) msgsz;
   4235 		syscallarg(int) msgflg;
   4236 	} */ *uap = v;
   4237 	struct sys_msgsnd_args ua;
   4238 
   4239 	NETBSD32TO64_UAP(msqid);
   4240 	NETBSD32TOP_UAP(msgp, void);
   4241 	NETBSD32TOX_UAP(msgsz, size_t);
   4242 	NETBSD32TO64_UAP(msgflg);
   4243 	return (sys_msgsnd(p, &ua, retval));
   4244 #else
   4245 	return (ENOSYS);
   4246 #endif
   4247 }
   4248 
   4249 int
   4250 compat_netbsd32_msgrcv(p, v, retval)
   4251 	struct proc *p;
   4252 	void *v;
   4253 	register_t *retval;
   4254 {
   4255 #if 0
   4256 	struct compat_netbsd32_msgrcv_args /* {
   4257 		syscallarg(int) msqid;
   4258 		syscallarg(netbsd32_voidp) msgp;
   4259 		syscallarg(netbsd32_size_t) msgsz;
   4260 		syscallarg(netbsd32_long) msgtyp;
   4261 		syscallarg(int) msgflg;
   4262 	} */ *uap = v;
   4263 	struct sys_msgrcv_args ua;
   4264 	ssize_t rt;
   4265 	int error;
   4266 
   4267 	NETBSD32TO64_UAP(msqid);
   4268 	NETBSD32TOP_UAP(msgp, void);
   4269 	NETBSD32TOX_UAP(msgsz, size_t);
   4270 	NETBSD32TOX_UAP(msgtyp, long);
   4271 	NETBSD32TO64_UAP(msgflg);
   4272 	error = sys_msgrcv(p, &ua, (register_t *)&rt);
   4273 	*(netbsd32_ssize_t *)retval = rt;
   4274 	return (error);
   4275 #else
   4276 	return (ENOSYS);
   4277 #endif
   4278 }
   4279 
   4280 int
   4281 compat_netbsd32_shmat(p, v, retval)
   4282 	struct proc *p;
   4283 	void *v;
   4284 	register_t *retval;
   4285 {
   4286 #if 0
   4287 	struct compat_netbsd32_shmat_args /* {
   4288 		syscallarg(int) shmid;
   4289 		syscallarg(const netbsd32_voidp) shmaddr;
   4290 		syscallarg(int) shmflg;
   4291 	} */ *uap = v;
   4292 	struct sys_shmat_args ua;
   4293 	void *rt;
   4294 	int error;
   4295 
   4296 	NETBSD32TO64_UAP(shmid);
   4297 	NETBSD32TOP_UAP(shmaddr, void);
   4298 	NETBSD32TO64_UAP(shmflg);
   4299 	error = sys_shmat(p, &ua, (register_t *)&rt);
   4300 	*retval = (netbsd32_voidp)(u_long)rt;
   4301 	return (error);
   4302 #else
   4303 	return (ENOSYS);
   4304 #endif
   4305 }
   4306 
   4307 int
   4308 compat_netbsd32_shmctl(p, v, retval)
   4309 	struct proc *p;
   4310 	void *v;
   4311 	register_t *retval;
   4312 {
   4313 #if 0
   4314 	struct compat_netbsd32_shmctl_args /* {
   4315 		syscallarg(int) shmid;
   4316 		syscallarg(int) cmd;
   4317 		syscallarg(netbsd32_shmid_dsp_t) buf;
   4318 	} */ *uap = v;
   4319 	struct sys_shmctl_args ua;
   4320 	struct shmid_ds ds;
   4321 	struct netbsd32_shmid_ds *ds32p;
   4322 	int error;
   4323 
   4324 	NETBSD32TO64_UAP(shmid);
   4325 	NETBSD32TO64_UAP(cmd);
   4326 	ds32p = (struct netbsd32_shmid_ds *)(u_long)SCARG(uap, buf);
   4327 	if (ds32p) {
   4328 		SCARG(&ua, buf) = NULL;
   4329 		netbsd32_to_shmid_ds(ds32p, &ds);
   4330 	} else
   4331 		SCARG(&ua, buf) = NULL;
   4332 	error = sys_shmctl(p, &ua, retval);
   4333 	if (error)
   4334 		return (error);
   4335 
   4336 	if (ds32p)
   4337 		netbsd32_from_shmid_ds(&ds, ds32p);
   4338 	return (0);
   4339 #else
   4340 	return (ENOSYS);
   4341 #endif
   4342 }
   4343 
   4344 int
   4345 compat_netbsd32_shmdt(p, v, retval)
   4346 	struct proc *p;
   4347 	void *v;
   4348 	register_t *retval;
   4349 {
   4350 #if 0
   4351 	struct compat_netbsd32_shmdt_args /* {
   4352 		syscallarg(const netbsd32_voidp) shmaddr;
   4353 	} */ *uap = v;
   4354 	struct sys_shmdt_args ua;
   4355 
   4356 	NETBSD32TOP_UAP(shmaddr, const char);
   4357 	return (sys_shmdt(p, &ua, retval));
   4358 #else
   4359 	return (ENOSYS);
   4360 #endif
   4361 }
   4362 
   4363 int
   4364 compat_netbsd32_shmget(p, v, retval)
   4365 	struct proc *p;
   4366 	void *v;
   4367 	register_t *retval;
   4368 {
   4369 #if 0
   4370 	struct compat_netbsd32_shmget_args /* {
   4371 		syscallarg(netbsd32_key_t) key;
   4372 		syscallarg(netbsd32_size_t) size;
   4373 		syscallarg(int) shmflg;
   4374 	} */ *uap = v;
   4375 	struct sys_shmget_args ua;
   4376 
   4377 	NETBSD32TOX_UAP(key, key_t)
   4378 	NETBSD32TOX_UAP(size, size_t)
   4379 	NETBSD32TO64_UAP(shmflg);
   4380 	return (sys_shmget(p, &ua, retval));
   4381 #else
   4382 	return (ENOSYS);
   4383 #endif
   4384 }
   4385 
   4386 int
   4387 compat_netbsd32_clock_gettime(p, v, retval)
   4388 	struct proc *p;
   4389 	void *v;
   4390 	register_t *retval;
   4391 {
   4392 	struct compat_netbsd32_clock_gettime_args /* {
   4393 		syscallarg(netbsd32_clockid_t) clock_id;
   4394 		syscallarg(netbsd32_timespecp_t) tp;
   4395 	} */ *uap = v;
   4396 	clockid_t clock_id;
   4397 	struct timeval atv;
   4398 	struct timespec ats;
   4399 	struct netbsd32_timespec ts32;
   4400 
   4401 	clock_id = SCARG(uap, clock_id);
   4402 	if (clock_id != CLOCK_REALTIME)
   4403 		return (EINVAL);
   4404 
   4405 	microtime(&atv);
   4406 	TIMEVAL_TO_TIMESPEC(&atv,&ats);
   4407 	netbsd32_from_timespec(&ats, &ts32);
   4408 
   4409 	return copyout(&ts32, (caddr_t)(u_long)SCARG(uap, tp), sizeof(ts32));
   4410 }
   4411 
   4412 int
   4413 compat_netbsd32_clock_settime(p, v, retval)
   4414 	struct proc *p;
   4415 	void *v;
   4416 	register_t *retval;
   4417 {
   4418 	struct compat_netbsd32_clock_settime_args /* {
   4419 		syscallarg(netbsd32_clockid_t) clock_id;
   4420 		syscallarg(const netbsd32_timespecp_t) tp;
   4421 	} */ *uap = v;
   4422 	struct netbsd32_timespec ts32;
   4423 	clockid_t clock_id;
   4424 	struct timeval atv;
   4425 	struct timespec ats;
   4426 	int error;
   4427 
   4428 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   4429 		return (error);
   4430 
   4431 	clock_id = SCARG(uap, clock_id);
   4432 	if (clock_id != CLOCK_REALTIME)
   4433 		return (EINVAL);
   4434 
   4435 	if ((error = copyin((caddr_t)(u_long)SCARG(uap, tp), &ts32, sizeof(ts32))) != 0)
   4436 		return (error);
   4437 
   4438 	netbsd32_to_timespec(&ts32, &ats);
   4439 	TIMESPEC_TO_TIMEVAL(&atv,&ats);
   4440 	if ((error = settime(&atv)))
   4441 		return (error);
   4442 
   4443 	return 0;
   4444 }
   4445 
   4446 int
   4447 compat_netbsd32_clock_getres(p, v, retval)
   4448 	struct proc *p;
   4449 	void *v;
   4450 	register_t *retval;
   4451 {
   4452 	struct compat_netbsd32_clock_getres_args /* {
   4453 		syscallarg(netbsd32_clockid_t) clock_id;
   4454 		syscallarg(netbsd32_timespecp_t) tp;
   4455 	} */ *uap = v;
   4456 	struct netbsd32_timespec ts32;
   4457 	clockid_t clock_id;
   4458 	struct timespec ts;
   4459 	int error = 0;
   4460 
   4461 	clock_id = SCARG(uap, clock_id);
   4462 	if (clock_id != CLOCK_REALTIME)
   4463 		return (EINVAL);
   4464 
   4465 	if (SCARG(uap, tp)) {
   4466 		ts.tv_sec = 0;
   4467 		ts.tv_nsec = 1000000000 / hz;
   4468 
   4469 		netbsd32_from_timespec(&ts, &ts32);
   4470 		error = copyout(&ts, (caddr_t)(u_long)SCARG(uap, tp), sizeof(ts));
   4471 	}
   4472 
   4473 	return error;
   4474 }
   4475 
   4476 int
   4477 compat_netbsd32_nanosleep(p, v, retval)
   4478 	struct proc *p;
   4479 	void *v;
   4480 	register_t *retval;
   4481 {
   4482 	struct compat_netbsd32_nanosleep_args /* {
   4483 		syscallarg(const netbsd32_timespecp_t) rqtp;
   4484 		syscallarg(netbsd32_timespecp_t) rmtp;
   4485 	} */ *uap = v;
   4486 	static int nanowait;
   4487 	struct netbsd32_timespec ts32;
   4488 	struct timespec rqt;
   4489 	struct timespec rmt;
   4490 	struct timeval atv, utv;
   4491 	int error, s, timo;
   4492 
   4493 	error = copyin((caddr_t)(u_long)SCARG(uap, rqtp), (caddr_t)&ts32,
   4494 		       sizeof(ts32));
   4495 	if (error)
   4496 		return (error);
   4497 
   4498 	netbsd32_to_timespec(&ts32, &rqt);
   4499 	TIMESPEC_TO_TIMEVAL(&atv,&rqt)
   4500 	if (itimerfix(&atv))
   4501 		return (EINVAL);
   4502 
   4503 	s = splclock();
   4504 	timeradd(&atv,&time,&atv);
   4505 	timo = hzto(&atv);
   4506 	/*
   4507 	 * Avoid inadvertantly sleeping forever
   4508 	 */
   4509 	if (timo == 0)
   4510 		timo = 1;
   4511 	splx(s);
   4512 
   4513 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
   4514 	if (error == ERESTART)
   4515 		error = EINTR;
   4516 	if (error == EWOULDBLOCK)
   4517 		error = 0;
   4518 
   4519 	if (SCARG(uap, rmtp)) {
   4520 		int error;
   4521 
   4522 		s = splclock();
   4523 		utv = time;
   4524 		splx(s);
   4525 
   4526 		timersub(&atv, &utv, &utv);
   4527 		if (utv.tv_sec < 0)
   4528 			timerclear(&utv);
   4529 
   4530 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
   4531 		netbsd32_from_timespec(&rmt, &ts32);
   4532 		error = copyout((caddr_t)&ts32, (caddr_t)(u_long)SCARG(uap,rmtp),
   4533 			sizeof(ts32));
   4534 		if (error)
   4535 			return (error);
   4536 	}
   4537 
   4538 	return error;
   4539 }
   4540 
   4541 int
   4542 compat_netbsd32_fdatasync(p, v, retval)
   4543 	struct proc *p;
   4544 	void *v;
   4545 	register_t *retval;
   4546 {
   4547 	struct compat_netbsd32_fdatasync_args /* {
   4548 		syscallarg(int) fd;
   4549 	} */ *uap = v;
   4550 	struct sys_fdatasync_args ua;
   4551 
   4552 	NETBSD32TO64_UAP(fd);
   4553 
   4554 	return (sys_fdatasync(p, &ua, retval));
   4555 }
   4556 
   4557 int
   4558 compat_netbsd32___posix_rename(p, v, retval)
   4559 	struct proc *p;
   4560 	void *v;
   4561 	register_t *retval;
   4562 {
   4563 	struct compat_netbsd32___posix_rename_args /* {
   4564 		syscallarg(const netbsd32_charp) from;
   4565 		syscallarg(const netbsd32_charp) to;
   4566 	} */ *uap = v;
   4567 	struct sys___posix_rename_args ua;
   4568 
   4569 	NETBSD32TOP_UAP(from, const char *);
   4570 	NETBSD32TOP_UAP(to, const char *);
   4571 
   4572 	return (sys___posix_rename(p, &ua, retval));
   4573 }
   4574 
   4575 int
   4576 compat_netbsd32_swapctl(p, v, retval)
   4577 	struct proc *p;
   4578 	void *v;
   4579 	register_t *retval;
   4580 {
   4581 	struct compat_netbsd32_swapctl_args /* {
   4582 		syscallarg(int) cmd;
   4583 		syscallarg(const netbsd32_voidp) arg;
   4584 		syscallarg(int) misc;
   4585 	} */ *uap = v;
   4586 	struct sys_swapctl_args ua;
   4587 
   4588 	NETBSD32TO64_UAP(cmd);
   4589 	NETBSD32TOP_UAP(arg, const void);
   4590 	NETBSD32TO64_UAP(misc);
   4591 	return (sys_swapctl(p, &ua, retval));
   4592 }
   4593 
   4594 int
   4595 compat_netbsd32_getdents(p, v, retval)
   4596 	struct proc *p;
   4597 	void *v;
   4598 	register_t *retval;
   4599 {
   4600 	struct compat_netbsd32_getdents_args /* {
   4601 		syscallarg(int) fd;
   4602 		syscallarg(netbsd32_charp) buf;
   4603 		syscallarg(netbsd32_size_t) count;
   4604 	} */ *uap = v;
   4605 	struct file *fp;
   4606 	int error, done;
   4607 
   4608 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   4609 		return (error);
   4610 	if ((fp->f_flag & FREAD) == 0)
   4611 		return (EBADF);
   4612 	error = vn_readdir(fp, (caddr_t)(u_long)SCARG(uap, buf), UIO_USERSPACE,
   4613 			SCARG(uap, count), &done, p, 0, 0);
   4614 	*retval = done;
   4615 	return (error);
   4616 }
   4617 
   4618 
   4619 int
   4620 compat_netbsd32_minherit(p, v, retval)
   4621 	struct proc *p;
   4622 	void *v;
   4623 	register_t *retval;
   4624 {
   4625 	struct compat_netbsd32_minherit_args /* {
   4626 		syscallarg(netbsd32_voidp) addr;
   4627 		syscallarg(netbsd32_size_t) len;
   4628 		syscallarg(int) inherit;
   4629 	} */ *uap = v;
   4630 	struct sys_minherit_args ua;
   4631 
   4632 	NETBSD32TOP_UAP(addr, void);
   4633 	NETBSD32TOX_UAP(len, size_t);
   4634 	NETBSD32TO64_UAP(inherit);
   4635 	return (sys_minherit(p, &ua, retval));
   4636 }
   4637 
   4638 int
   4639 compat_netbsd32_lchmod(p, v, retval)
   4640 	struct proc *p;
   4641 	void *v;
   4642 	register_t *retval;
   4643 {
   4644 	struct compat_netbsd32_lchmod_args /* {
   4645 		syscallarg(const netbsd32_charp) path;
   4646 		syscallarg(mode_t) mode;
   4647 	} */ *uap = v;
   4648 	struct sys_lchmod_args ua;
   4649 
   4650 	NETBSD32TOP_UAP(path, const char);
   4651 	NETBSD32TO64_UAP(mode);
   4652 	return (sys_lchmod(p, &ua, retval));
   4653 }
   4654 
   4655 int
   4656 compat_netbsd32_lchown(p, v, retval)
   4657 	struct proc *p;
   4658 	void *v;
   4659 	register_t *retval;
   4660 {
   4661 	struct compat_netbsd32_lchown_args /* {
   4662 		syscallarg(const netbsd32_charp) path;
   4663 		syscallarg(uid_t) uid;
   4664 		syscallarg(gid_t) gid;
   4665 	} */ *uap = v;
   4666 	struct sys_lchown_args ua;
   4667 
   4668 	NETBSD32TOP_UAP(path, const char);
   4669 	NETBSD32TO64_UAP(uid);
   4670 	NETBSD32TO64_UAP(gid);
   4671 	return (sys_lchown(p, &ua, retval));
   4672 }
   4673 
   4674 int
   4675 compat_netbsd32_lutimes(p, v, retval)
   4676 	struct proc *p;
   4677 	void *v;
   4678 	register_t *retval;
   4679 {
   4680 	struct compat_netbsd32_lutimes_args /* {
   4681 		syscallarg(const netbsd32_charp) path;
   4682 		syscallarg(const netbsd32_timevalp_t) tptr;
   4683 	} */ *uap = v;
   4684 	int error;
   4685 	struct nameidata nd;
   4686 
   4687 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, (caddr_t)(u_long)SCARG(uap, path), p);
   4688 	if ((error = namei(&nd)) != 0)
   4689 		return (error);
   4690 
   4691 	error = change_utimes32(nd.ni_vp, (struct timeval *)(u_long)SCARG(uap, tptr), p);
   4692 
   4693 	vrele(nd.ni_vp);
   4694 	return (error);
   4695 }
   4696 
   4697 
   4698 int
   4699 compat_netbsd32___msync13(p, v, retval)
   4700 	struct proc *p;
   4701 	void *v;
   4702 	register_t *retval;
   4703 {
   4704 	struct compat_netbsd32___msync13_args /* {
   4705 		syscallarg(netbsd32_voidp) addr;
   4706 		syscallarg(netbsd32_size_t) len;
   4707 		syscallarg(int) flags;
   4708 	} */ *uap = v;
   4709 	struct sys___msync13_args ua;
   4710 
   4711 	NETBSD32TOP_UAP(addr, void);
   4712 	NETBSD32TOX_UAP(len, size_t);
   4713 	NETBSD32TO64_UAP(flags);
   4714 	return (sys___msync13(p, &ua, retval));
   4715 }
   4716 
   4717 int
   4718 compat_netbsd32___stat13(p, v, retval)
   4719 	struct proc *p;
   4720 	void *v;
   4721 	register_t *retval;
   4722 {
   4723 	struct compat_netbsd32___stat13_args /* {
   4724 		syscallarg(const netbsd32_charp) path;
   4725 		syscallarg(netbsd32_statp_t) ub;
   4726 	} */ *uap = v;
   4727 	struct netbsd32_stat sb32;
   4728 	struct stat sb;
   4729 	int error;
   4730 	struct nameidata nd;
   4731 
   4732 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   4733 	    (caddr_t)(u_long)SCARG(uap, path), p);
   4734 	if ((error = namei(&nd)) != 0)
   4735 		return (error);
   4736 	error = vn_stat(nd.ni_vp, &sb, p);
   4737 	vput(nd.ni_vp);
   4738 	if (error)
   4739 		return (error);
   4740 	netbsd32_from___stat13(&sb, &sb32);
   4741 	error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof(sb32));
   4742 	return (error);
   4743 }
   4744 
   4745 int
   4746 compat_netbsd32___fstat13(p, v, retval)
   4747 	struct proc *p;
   4748 	void *v;
   4749 	register_t *retval;
   4750 {
   4751 	struct compat_netbsd32___fstat13_args /* {
   4752 		syscallarg(int) fd;
   4753 		syscallarg(netbsd32_statp_t) sb;
   4754 	} */ *uap = v;
   4755 	int fd = SCARG(uap, fd);
   4756 	register struct filedesc *fdp = p->p_fd;
   4757 	register struct file *fp;
   4758 	struct netbsd32_stat sb32;
   4759 	struct stat ub;
   4760 	int error = 0;
   4761 
   4762 	if ((u_int)fd >= fdp->fd_nfiles ||
   4763 	    (fp = fdp->fd_ofiles[fd]) == NULL)
   4764 		return (EBADF);
   4765 	switch (fp->f_type) {
   4766 
   4767 	case DTYPE_VNODE:
   4768 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
   4769 		break;
   4770 
   4771 	case DTYPE_SOCKET:
   4772 		error = soo_stat((struct socket *)fp->f_data, &ub);
   4773 		break;
   4774 
   4775 	default:
   4776 		panic("fstat");
   4777 		/*NOTREACHED*/
   4778 	}
   4779 	if (error == 0) {
   4780 		netbsd32_from___stat13(&ub, &sb32);
   4781 		error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, sb), sizeof(sb32));
   4782 	}
   4783 	return (error);
   4784 }
   4785 
   4786 int
   4787 compat_netbsd32___lstat13(p, v, retval)
   4788 	struct proc *p;
   4789 	void *v;
   4790 	register_t *retval;
   4791 {
   4792 	struct compat_netbsd32___lstat13_args /* {
   4793 		syscallarg(const netbsd32_charp) path;
   4794 		syscallarg(netbsd32_statp_t) ub;
   4795 	} */ *uap = v;
   4796 	struct netbsd32_stat sb32;
   4797 	struct stat sb;
   4798 	int error;
   4799 	struct nameidata nd;
   4800 
   4801 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   4802 	    (caddr_t)(u_long)SCARG(uap, path), p);
   4803 	if ((error = namei(&nd)) != 0)
   4804 		return (error);
   4805 	error = vn_stat(nd.ni_vp, &sb, p);
   4806 	vput(nd.ni_vp);
   4807 	if (error)
   4808 		return (error);
   4809 	netbsd32_from___stat13(&sb, &sb32);
   4810 	error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof(sb32));
   4811 	return (error);
   4812 }
   4813 
   4814 int
   4815 compat_netbsd32___sigaltstack14(p, v, retval)
   4816 	struct proc *p;
   4817 	void *v;
   4818 	register_t *retval;
   4819 {
   4820 	struct compat_netbsd32___sigaltstack14_args /* {
   4821 		syscallarg(const netbsd32_sigaltstackp_t) nss;
   4822 		syscallarg(netbsd32_sigaltstackp_t) oss;
   4823 	} */ *uap = v;
   4824 	struct netbsd32_sigaltstack s32;
   4825 	struct sigaltstack nss, oss;
   4826 	int error;
   4827 
   4828 	if (SCARG(uap, nss)) {
   4829 		error = copyin((caddr_t)(u_long)SCARG(uap, nss), &s32, sizeof(s32));
   4830 		if (error)
   4831 			return (error);
   4832 		nss.ss_sp = (void *)(u_long)s32.ss_sp;
   4833 		nss.ss_size = (size_t)s32.ss_size;
   4834 		nss.ss_flags = s32.ss_flags;
   4835 	}
   4836 	error = sigaltstack1(p,
   4837 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
   4838 	if (error)
   4839 		return (error);
   4840 	if (SCARG(uap, oss)) {
   4841 		s32.ss_sp = (netbsd32_voidp)(u_long)oss.ss_sp;
   4842 		s32.ss_size = (netbsd32_size_t)oss.ss_size;
   4843 		s32.ss_flags = oss.ss_flags;
   4844 		error = copyout(&s32, (caddr_t)(u_long)SCARG(uap, oss), sizeof(s32));
   4845 		if (error)
   4846 			return (error);
   4847 	}
   4848 	return (0);
   4849 }
   4850 
   4851 int
   4852 compat_netbsd32___posix_chown(p, v, retval)
   4853 	struct proc *p;
   4854 	void *v;
   4855 	register_t *retval;
   4856 {
   4857 	struct compat_netbsd32___posix_chown_args /* {
   4858 		syscallarg(const netbsd32_charp) path;
   4859 		syscallarg(uid_t) uid;
   4860 		syscallarg(gid_t) gid;
   4861 	} */ *uap = v;
   4862 	struct sys___posix_chown_args ua;
   4863 
   4864 	NETBSD32TOP_UAP(path, const char);
   4865 	NETBSD32TO64_UAP(uid);
   4866 	NETBSD32TO64_UAP(gid);
   4867 	return (sys___posix_chown(p, &ua, retval));
   4868 }
   4869 
   4870 int
   4871 compat_netbsd32___posix_fchown(p, v, retval)
   4872 	struct proc *p;
   4873 	void *v;
   4874 	register_t *retval;
   4875 {
   4876 	struct compat_netbsd32___posix_fchown_args /* {
   4877 		syscallarg(int) fd;
   4878 		syscallarg(uid_t) uid;
   4879 		syscallarg(gid_t) gid;
   4880 	} */ *uap = v;
   4881 	struct sys___posix_fchown_args ua;
   4882 
   4883 	NETBSD32TO64_UAP(fd);
   4884 	NETBSD32TO64_UAP(uid);
   4885 	NETBSD32TO64_UAP(gid);
   4886 	return (sys___posix_fchown(p, &ua, retval));
   4887 }
   4888 
   4889 int
   4890 compat_netbsd32___posix_lchown(p, v, retval)
   4891 	struct proc *p;
   4892 	void *v;
   4893 	register_t *retval;
   4894 {
   4895 	struct compat_netbsd32___posix_lchown_args /* {
   4896 		syscallarg(const netbsd32_charp) path;
   4897 		syscallarg(uid_t) uid;
   4898 		syscallarg(gid_t) gid;
   4899 	} */ *uap = v;
   4900 	struct sys___posix_lchown_args ua;
   4901 
   4902 	NETBSD32TOP_UAP(path, const char);
   4903 	NETBSD32TO64_UAP(uid);
   4904 	NETBSD32TO64_UAP(gid);
   4905 	return (sys___posix_lchown(p, &ua, retval));
   4906 }
   4907 
   4908 int
   4909 compat_netbsd32_getsid(p, v, retval)
   4910 	struct proc *p;
   4911 	void *v;
   4912 	register_t *retval;
   4913 {
   4914 	struct compat_netbsd32_getsid_args /* {
   4915 		syscallarg(pid_t) pid;
   4916 	} */ *uap = v;
   4917 	struct sys_getsid_args ua;
   4918 
   4919 	NETBSD32TO64_UAP(pid);
   4920 	return (sys_getsid(p, &ua, retval));
   4921 }
   4922 
   4923 int
   4924 compat_netbsd32_fktrace(p, v, retval)
   4925 	struct proc *p;
   4926 	void *v;
   4927 	register_t *retval;
   4928 {
   4929 	struct compat_netbsd32_fktrace_args /* {
   4930 		syscallarg(const int) fd;
   4931 		syscallarg(int) ops;
   4932 		syscallarg(int) facs;
   4933 		syscallarg(int) pid;
   4934 	} */ *uap = v;
   4935 	struct sys_fktrace_args ua;
   4936 
   4937 	NETBSD32TO64_UAP(fd);
   4938 	NETBSD32TO64_UAP(ops);
   4939 	NETBSD32TO64_UAP(facs);
   4940 	NETBSD32TO64_UAP(pid);
   4941 	return (sys_fktrace(p, &ua, retval));
   4942 }
   4943 
   4944 int
   4945 compat_netbsd32_preadv(p, v, retval)
   4946 	struct proc *p;
   4947 	void *v;
   4948 	register_t *retval;
   4949 {
   4950 	struct compat_netbsd32_preadv_args /* {
   4951 		syscallarg(int) fd;
   4952 		syscallarg(const netbsd32_iovecp_t) iovp;
   4953 		syscallarg(int) iovcnt;
   4954 		syscallarg(int) pad;
   4955 		syscallarg(off_t) offset;
   4956 	} */ *uap = v;
   4957 	struct filedesc *fdp = p->p_fd;
   4958 	struct file *fp;
   4959 	struct vnode *vp;
   4960 	off_t offset;
   4961 	int error, fd = SCARG(uap, fd);
   4962 
   4963 	if ((u_int)fd >= fdp->fd_nfiles ||
   4964 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   4965 	    (fp->f_flag & FREAD) == 0)
   4966 		return (EBADF);
   4967 
   4968 	vp = (struct vnode *)fp->f_data;
   4969 	if (fp->f_type != DTYPE_VNODE
   4970 	    || vp->v_type == VFIFO)
   4971 		return (ESPIPE);
   4972 
   4973 	offset = SCARG(uap, offset);
   4974 
   4975 	/*
   4976 	 * XXX This works because no file systems actually
   4977 	 * XXX take any action on the seek operation.
   4978 	 */
   4979 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   4980 		return (error);
   4981 
   4982 	return (dofilereadv32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp), SCARG(uap, iovcnt),
   4983 	    &offset, 0, retval));
   4984 }
   4985 
   4986 int
   4987 compat_netbsd32_pwritev(p, v, retval)
   4988 	struct proc *p;
   4989 	void *v;
   4990 	register_t *retval;
   4991 {
   4992 	struct compat_netbsd32_pwritev_args /* {
   4993 		syscallarg(int) fd;
   4994 		syscallarg(const netbsd32_iovecp_t) iovp;
   4995 		syscallarg(int) iovcnt;
   4996 		syscallarg(int) pad;
   4997 		syscallarg(off_t) offset;
   4998 	} */ *uap = v;
   4999 	struct filedesc *fdp = p->p_fd;
   5000 	struct file *fp;
   5001 	struct vnode *vp;
   5002 	off_t offset;
   5003 	int error, fd = SCARG(uap, fd);
   5004 
   5005 	if ((u_int)fd >= fdp->fd_nfiles ||
   5006 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   5007 	    (fp->f_flag & FWRITE) == 0)
   5008 		return (EBADF);
   5009 
   5010 	vp = (struct vnode *)fp->f_data;
   5011 	if (fp->f_type != DTYPE_VNODE
   5012 	    || vp->v_type == VFIFO)
   5013 		return (ESPIPE);
   5014 
   5015 	offset = SCARG(uap, offset);
   5016 
   5017 	/*
   5018 	 * XXX This works because no file systems actually
   5019 	 * XXX take any action on the seek operation.
   5020 	 */
   5021 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   5022 		return (error);
   5023 
   5024 	return (dofilewritev32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp), SCARG(uap, iovcnt),
   5025 	    &offset, 0, retval));
   5026 }
   5027 
   5028 int
   5029 compat_13_compat_netbsd32_sigprocmask(p, v, retval)
   5030 	register struct proc *p;
   5031 	void *v;
   5032 	register_t *retval;
   5033 {
   5034 	struct compat_13_compat_netbsd32_sigprocmask_args /* {
   5035 		syscallarg(int) how;
   5036 		syscallarg(int) mask;
   5037 	} */ *uap = v;
   5038 	sigset13_t ness, oess;
   5039 	sigset_t nbss, obss;
   5040 	int error;
   5041 
   5042 	ness = SCARG(uap, mask);
   5043 	native_sigset13_to_sigset(&ness, &nbss);
   5044 	error = sigprocmask1(p, SCARG(uap, how), &nbss, &obss);
   5045 	if (error)
   5046 		return (error);
   5047 	native_sigset_to_sigset13(&obss, &oess);
   5048 	*retval = oess;
   5049 	return (0);
   5050 }
   5051 
   5052 int
   5053 compat_13_compat_netbsd32_sigsuspend(p, v, retval)
   5054 	register struct proc *p;
   5055 	void *v;
   5056 	register_t *retval;
   5057 {
   5058 	struct compat_13_compat_netbsd32_sigsuspend_args /* {
   5059 		syscallarg(sigset13_t) mask;
   5060 	} */ *uap = v;
   5061 	sigset13_t ess;
   5062 	sigset_t bss;
   5063 
   5064 	ess = SCARG(uap, mask);
   5065 	native_sigset13_to_sigset(&ess, &bss);
   5066 	return (sigsuspend1(p, &bss));
   5067 }
   5068