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