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