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