Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_netbsd.c revision 1.28.2.3
      1 /*	$NetBSD: netbsd32_netbsd.c,v 1.28.2.3 2000/08/26 01:09:34 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 <vm/vm.h>
     79 #include <vm/vm_kern.h>
     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, witht he necessary conversions to 64bit before
    567  * calling the real syscall.
    568  */
    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)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 	extern struct emul emul_netbsd;
   1771 
   1772 
   1773 	NETBSD32TOP_UAP(path, const char);
   1774 	NETBSD32TOP_UAP(argp, char *);
   1775 	NETBSD32TOP_UAP(envp, char *);
   1776 	sg = stackgap_init(p->p_emul);
   1777 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1778 
   1779 	/*
   1780 	 * figure out the maximum size of an exec header, if necessary.
   1781 	 * XXX should be able to keep LKM code from modifying exec switch
   1782 	 * when we're still using it, but...
   1783 	 */
   1784 	if (exec_maxhdrsz == 0) {
   1785 		for (i = 0; i < nexecs; i++)
   1786 			if (execsw[i].es_check != NULL
   1787 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
   1788 				exec_maxhdrsz = execsw[i].es_hdrsz;
   1789 	}
   1790 
   1791 	/* init the namei data to point the file user's program name */
   1792 	/* XXX cgd 960926: why do this here?  most will be clobbered. */
   1793 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(&ua, path), p);
   1794 
   1795 	/*
   1796 	 * initialize the fields of the exec package.
   1797 	 */
   1798 	pack.ep_name = SCARG(&ua, path);
   1799 	MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
   1800 	pack.ep_hdrlen = exec_maxhdrsz;
   1801 	pack.ep_hdrvalid = 0;
   1802 	pack.ep_ndp = &nid;
   1803 	pack.ep_emul_arg = NULL;
   1804 	pack.ep_vmcmds.evs_cnt = 0;
   1805 	pack.ep_vmcmds.evs_used = 0;
   1806 	pack.ep_vap = &attr;
   1807 	pack.ep_emul = &emul_netbsd;
   1808 	pack.ep_flags = 0;
   1809 
   1810 	/* see if we can run it. */
   1811 	if ((error = check_exec(p, &pack)) != 0)
   1812 		goto freehdr;
   1813 
   1814 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
   1815 
   1816 	/* allocate an argument buffer */
   1817 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
   1818 #ifdef DIAGNOSTIC
   1819 	if (argp == (vaddr_t) 0)
   1820 		panic("execve: argp == NULL");
   1821 #endif
   1822 	dp = argp;
   1823 	argc = 0;
   1824 
   1825 	/* copy the fake args list, if there's one, freeing it as we go */
   1826 	if (pack.ep_flags & EXEC_HASARGL) {
   1827 		tmpfap = pack.ep_fa;
   1828 		while (*tmpfap != NULL) {
   1829 			char *cp;
   1830 
   1831 			cp = *tmpfap;
   1832 			while (*cp)
   1833 				*dp++ = *cp++;
   1834 			dp++;
   1835 
   1836 			FREE(*tmpfap, M_EXEC);
   1837 			tmpfap++; argc++;
   1838 		}
   1839 		FREE(pack.ep_fa, M_EXEC);
   1840 		pack.ep_flags &= ~EXEC_HASARGL;
   1841 	}
   1842 
   1843 	/* Now get argv & environment */
   1844 	if (!(cpp = (netbsd32_charp *)SCARG(&ua, argp))) {
   1845 		error = EINVAL;
   1846 		goto bad;
   1847 	}
   1848 
   1849 	if (pack.ep_flags & EXEC_SKIPARG)
   1850 		cpp++;
   1851 
   1852 	while (1) {
   1853 		len = argp + ARG_MAX - dp;
   1854 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
   1855 			goto bad;
   1856 		if (!sp)
   1857 			break;
   1858 		if ((error = copyinstr((char *)(u_long)sp, dp,
   1859 				       len, &len)) != 0) {
   1860 			if (error == ENAMETOOLONG)
   1861 				error = E2BIG;
   1862 			goto bad;
   1863 		}
   1864 		dp += len;
   1865 		cpp++;
   1866 		argc++;
   1867 	}
   1868 
   1869 	envc = 0;
   1870 	/* environment need not be there */
   1871 	if ((cpp = (netbsd32_charp *)SCARG(&ua, envp)) != NULL ) {
   1872 		while (1) {
   1873 			len = argp + ARG_MAX - dp;
   1874 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
   1875 				goto bad;
   1876 			if (!sp)
   1877 				break;
   1878 			if ((error = copyinstr((char *)(u_long)sp,
   1879 					       dp, len, &len)) != 0) {
   1880 				if (error == ENAMETOOLONG)
   1881 					error = E2BIG;
   1882 				goto bad;
   1883 			}
   1884 			dp += len;
   1885 			cpp++;
   1886 			envc++;
   1887 		}
   1888 	}
   1889 
   1890 	dp = (char *) ALIGN(dp);
   1891 
   1892 	szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
   1893 
   1894 	/* Now check if args & environ fit into new stack */
   1895 	if (pack.ep_flags & EXEC_32)
   1896 		len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(int) +
   1897 		       sizeof(int) + dp + STACKGAPLEN + szsigcode +
   1898 		       sizeof(struct ps_strings)) - argp;
   1899 	else
   1900 		len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
   1901 		       sizeof(int) + dp + STACKGAPLEN + szsigcode +
   1902 		       sizeof(struct ps_strings)) - argp;
   1903 
   1904 	len = ALIGN(len);	/* make the stack "safely" aligned */
   1905 
   1906 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
   1907 		error = ENOMEM;
   1908 		goto bad;
   1909 	}
   1910 
   1911 	/* adjust "active stack depth" for process VSZ */
   1912 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
   1913 
   1914 	/*
   1915 	 * Do whatever is necessary to prepare the address space
   1916 	 * for remapping.  Note that this might replace the current
   1917 	 * vmspace with another!
   1918 	 */
   1919 	uvmspace_exec(p);
   1920 
   1921 	/* Now map address space */
   1922 	vm = p->p_vmspace;
   1923 	vm->vm_taddr = (char *) pack.ep_taddr;
   1924 	vm->vm_tsize = btoc(pack.ep_tsize);
   1925 	vm->vm_daddr = (char *) pack.ep_daddr;
   1926 	vm->vm_dsize = btoc(pack.ep_dsize);
   1927 	vm->vm_ssize = btoc(pack.ep_ssize);
   1928 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
   1929 
   1930 	/* create the new process's VM space by running the vmcmds */
   1931 #ifdef DIAGNOSTIC
   1932 	if (pack.ep_vmcmds.evs_used == 0)
   1933 		panic("execve: no vmcmds");
   1934 #endif
   1935 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
   1936 		struct exec_vmcmd *vcp;
   1937 
   1938 		vcp = &pack.ep_vmcmds.evs_cmds[i];
   1939 		error = (*vcp->ev_proc)(p, vcp);
   1940 	}
   1941 
   1942 	/* free the vmspace-creation commands, and release their references */
   1943 	kill_vmcmds(&pack.ep_vmcmds);
   1944 
   1945 	/* if an error happened, deallocate and punt */
   1946 	if (error)
   1947 		goto exec_abort;
   1948 
   1949 	/* remember information about the process */
   1950 	arginfo.ps_nargvstr = argc;
   1951 	arginfo.ps_nenvstr = envc;
   1952 
   1953 	stack = (char *) (USRSTACK - len);
   1954 	/* Now copy argc, args & environ to new stack */
   1955 	if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
   1956 		goto exec_abort;
   1957 
   1958 	/* copy out the process's ps_strings structure */
   1959 	if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
   1960 		goto exec_abort;
   1961 
   1962 	/* copy out the process's signal trapoline code */
   1963 	if (szsigcode) {
   1964 		if (copyout((char *)pack.ep_emul->e_sigcode,
   1965 		    p->p_sigacts->ps_sigcode = (char *)PS_STRINGS - szsigcode,
   1966 		    szsigcode))
   1967 			goto exec_abort;
   1968 #ifdef PMAP_NEED_PROCWR
   1969 		/* This is code. Let the pmap do what is needed. */
   1970 		pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode);
   1971 #endif
   1972 	}
   1973 
   1974 	stopprofclock(p);	/* stop profiling */
   1975 	fdcloseexec(p);		/* handle close on exec */
   1976 	execsigs(p);		/* reset catched signals */
   1977 	p->p_ctxlink = NULL;	/* reset ucontext link */
   1978 
   1979 	/* set command name & other accounting info */
   1980 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
   1981 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
   1982 	p->p_comm[len] = 0;
   1983 	p->p_acflag &= ~AFORK;
   1984 
   1985 	/* record proc's vnode, for use by procfs and others */
   1986         if (p->p_textvp)
   1987                 vrele(p->p_textvp);
   1988 	VREF(pack.ep_vp);
   1989 	p->p_textvp = pack.ep_vp;
   1990 
   1991 	p->p_flag |= P_EXEC;
   1992 	if (p->p_flag & P_PPWAIT) {
   1993 		p->p_flag &= ~P_PPWAIT;
   1994 		wakeup((caddr_t) p->p_pptr);
   1995 	}
   1996 
   1997 	/*
   1998 	 * deal with set[ug]id.
   1999 	 * MNT_NOSUID and P_TRACED have already been used to disable s[ug]id.
   2000 	 */
   2001 	if (((attr.va_mode & S_ISUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
   2002 	 || ((attr.va_mode & S_ISGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
   2003 		p->p_ucred = crcopy(cred);
   2004 #ifdef KTRACE
   2005 		/*
   2006 		 * If process is being ktraced, turn off - unless
   2007 		 * root set it.
   2008 		 */
   2009 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
   2010 			ktrderef(p);
   2011 #endif
   2012 		if (attr.va_mode & S_ISUID)
   2013 			p->p_ucred->cr_uid = attr.va_uid;
   2014 		if (attr.va_mode & S_ISGID)
   2015 			p->p_ucred->cr_gid = attr.va_gid;
   2016 		p_sugid(p);
   2017 	} else
   2018 		p->p_flag &= ~P_SUGID;
   2019 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
   2020 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
   2021 
   2022 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2023 
   2024 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2025 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2026 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2027 	vput(pack.ep_vp);
   2028 
   2029 	/* setup new registers and do misc. setup. */
   2030 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack);
   2031 
   2032 	if (p->p_flag & P_TRACED)
   2033 		psignal(p, SIGTRAP);
   2034 
   2035 	p->p_emul = pack.ep_emul;
   2036 	FREE(pack.ep_hdr, M_EXEC);
   2037 
   2038 #ifdef KTRACE
   2039 	if (KTRPOINT(p, KTR_EMUL))
   2040 		ktremul(p);
   2041 #endif
   2042 
   2043 	return (EJUSTRETURN);
   2044 
   2045 bad:
   2046 	/* free the vmspace-creation commands, and release their references */
   2047 	kill_vmcmds(&pack.ep_vmcmds);
   2048 	/* kill any opened file descriptor, if necessary */
   2049 	if (pack.ep_flags & EXEC_HASFD) {
   2050 		pack.ep_flags &= ~EXEC_HASFD;
   2051 		(void) fdrelease(p, pack.ep_fd);
   2052 	}
   2053 	/* close and put the exec'd file */
   2054 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2055 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2056 	vput(pack.ep_vp);
   2057 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2058 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2059 
   2060 freehdr:
   2061 	FREE(pack.ep_hdr, M_EXEC);
   2062 	return error;
   2063 
   2064 exec_abort:
   2065 	/*
   2066 	 * the old process doesn't exist anymore.  exit gracefully.
   2067 	 * get rid of the (new) address space we have created, if any, get rid
   2068 	 * of our namei data and vnode, and exit noting failure
   2069 	 */
   2070 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
   2071 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
   2072 	if (pack.ep_emul_arg)
   2073 		FREE(pack.ep_emul_arg, M_TEMP);
   2074 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2075 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2076 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2077 	vput(pack.ep_vp);
   2078 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2079 	FREE(pack.ep_hdr, M_EXEC);
   2080 	exit1(p, W_EXITCODE(0, SIGABRT));
   2081 	exit1(p, -1);
   2082 
   2083 	/* NOTREACHED */
   2084 	return 0;
   2085 }
   2086 
   2087 int
   2088 netbsd32_umask(p, v, retval)
   2089 	struct proc *p;
   2090 	void *v;
   2091 	register_t *retval;
   2092 {
   2093 	struct netbsd32_umask_args /* {
   2094 		syscallarg(mode_t) newmask;
   2095 	} */ *uap = v;
   2096 	struct sys_umask_args ua;
   2097 
   2098 	NETBSD32TO64_UAP(newmask);
   2099 	return (sys_umask(p, &ua, retval));
   2100 }
   2101 
   2102 int
   2103 netbsd32_chroot(p, v, retval)
   2104 	struct proc *p;
   2105 	void *v;
   2106 	register_t *retval;
   2107 {
   2108 	struct netbsd32_chroot_args /* {
   2109 		syscallarg(const netbsd32_charp) path;
   2110 	} */ *uap = v;
   2111 	struct sys_chroot_args ua;
   2112 
   2113 	NETBSD32TOP_UAP(path, const char);
   2114 	return (sys_chroot(p, &ua, retval));
   2115 }
   2116 
   2117 int
   2118 netbsd32_sbrk(p, v, retval)
   2119 	struct proc *p;
   2120 	void *v;
   2121 	register_t *retval;
   2122 {
   2123 	struct netbsd32_sbrk_args /* {
   2124 		syscallarg(int) incr;
   2125 	} */ *uap = v;
   2126 	struct sys_sbrk_args ua;
   2127 
   2128 	NETBSD32TO64_UAP(incr);
   2129 	return (sys_sbrk(p, &ua, retval));
   2130 }
   2131 
   2132 int
   2133 netbsd32_sstk(p, v, retval)
   2134 	struct proc *p;
   2135 	void *v;
   2136 	register_t *retval;
   2137 {
   2138 	struct netbsd32_sstk_args /* {
   2139 		syscallarg(int) incr;
   2140 	} */ *uap = v;
   2141 	struct sys_sstk_args ua;
   2142 
   2143 	NETBSD32TO64_UAP(incr);
   2144 	return (sys_sstk(p, &ua, retval));
   2145 }
   2146 
   2147 int
   2148 netbsd32_munmap(p, v, retval)
   2149 	struct proc *p;
   2150 	void *v;
   2151 	register_t *retval;
   2152 {
   2153 	struct netbsd32_munmap_args /* {
   2154 		syscallarg(netbsd32_voidp) addr;
   2155 		syscallarg(netbsd32_size_t) len;
   2156 	} */ *uap = v;
   2157 	struct sys_munmap_args ua;
   2158 
   2159 	NETBSD32TOP_UAP(addr, void);
   2160 	NETBSD32TOX_UAP(len, size_t);
   2161 	return (sys_munmap(p, &ua, retval));
   2162 }
   2163 
   2164 int
   2165 netbsd32_mprotect(p, v, retval)
   2166 	struct proc *p;
   2167 	void *v;
   2168 	register_t *retval;
   2169 {
   2170 	struct netbsd32_mprotect_args /* {
   2171 		syscallarg(netbsd32_voidp) addr;
   2172 		syscallarg(netbsd32_size_t) len;
   2173 		syscallarg(int) prot;
   2174 	} */ *uap = v;
   2175 	struct sys_mprotect_args ua;
   2176 
   2177 	NETBSD32TOP_UAP(addr, void);
   2178 	NETBSD32TOX_UAP(len, size_t);
   2179 	NETBSD32TO64_UAP(prot);
   2180 	return (sys_mprotect(p, &ua, retval));
   2181 }
   2182 
   2183 int
   2184 netbsd32_madvise(p, v, retval)
   2185 	struct proc *p;
   2186 	void *v;
   2187 	register_t *retval;
   2188 {
   2189 	struct netbsd32_madvise_args /* {
   2190 		syscallarg(netbsd32_voidp) addr;
   2191 		syscallarg(netbsd32_size_t) len;
   2192 		syscallarg(int) behav;
   2193 	} */ *uap = v;
   2194 	struct sys_madvise_args ua;
   2195 
   2196 	NETBSD32TOP_UAP(addr, void);
   2197 	NETBSD32TOX_UAP(len, size_t);
   2198 	NETBSD32TO64_UAP(behav);
   2199 	return (sys_madvise(p, &ua, retval));
   2200 }
   2201 
   2202 int
   2203 netbsd32_mincore(p, v, retval)
   2204 	struct proc *p;
   2205 	void *v;
   2206 	register_t *retval;
   2207 {
   2208 	struct netbsd32_mincore_args /* {
   2209 		syscallarg(netbsd32_caddr_t) addr;
   2210 		syscallarg(netbsd32_size_t) len;
   2211 		syscallarg(netbsd32_charp) vec;
   2212 	} */ *uap = v;
   2213 	struct sys_mincore_args ua;
   2214 
   2215 	NETBSD32TOX64_UAP(addr, caddr_t);
   2216 	NETBSD32TOX_UAP(len, size_t);
   2217 	NETBSD32TOP_UAP(vec, char);
   2218 	return (sys_mincore(p, &ua, retval));
   2219 }
   2220 
   2221 int
   2222 netbsd32_getgroups(p, v, retval)
   2223 	struct proc *p;
   2224 	void *v;
   2225 	register_t *retval;
   2226 {
   2227 	struct netbsd32_getgroups_args /* {
   2228 		syscallarg(int) gidsetsize;
   2229 		syscallarg(netbsd32_gid_tp) gidset;
   2230 	} */ *uap = v;
   2231 	struct pcred *pc = p->p_cred;
   2232 	int ngrp;
   2233 	int error;
   2234 
   2235 	ngrp = SCARG(uap, gidsetsize);
   2236 	if (ngrp == 0) {
   2237 		*retval = pc->pc_ucred->cr_ngroups;
   2238 		return (0);
   2239 	}
   2240 	if (ngrp < pc->pc_ucred->cr_ngroups)
   2241 		return (EINVAL);
   2242 	ngrp = pc->pc_ucred->cr_ngroups;
   2243 	/* Should convert gid_t to netbsd32_gid_t, but they're the same */
   2244 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
   2245 			(caddr_t)(u_long)SCARG(uap, gidset),
   2246 			ngrp * sizeof(gid_t));
   2247 	if (error)
   2248 		return (error);
   2249 	*retval = ngrp;
   2250 	return (0);
   2251 }
   2252 
   2253 int
   2254 netbsd32_setgroups(p, v, retval)
   2255 	struct proc *p;
   2256 	void *v;
   2257 	register_t *retval;
   2258 {
   2259 	struct netbsd32_setgroups_args /* {
   2260 		syscallarg(int) gidsetsize;
   2261 		syscallarg(const netbsd32_gid_tp) gidset;
   2262 	} */ *uap = v;
   2263 	struct sys_setgroups_args ua;
   2264 
   2265 	NETBSD32TO64_UAP(gidsetsize);
   2266 	NETBSD32TOP_UAP(gidset, gid_t);
   2267 	return (sys_setgroups(p, &ua, retval));
   2268 }
   2269 
   2270 int
   2271 netbsd32_setpgid(p, v, retval)
   2272 	struct proc *p;
   2273 	void *v;
   2274 	register_t *retval;
   2275 {
   2276 	struct netbsd32_setpgid_args /* {
   2277 		syscallarg(int) pid;
   2278 		syscallarg(int) pgid;
   2279 	} */ *uap = v;
   2280 	struct sys_setpgid_args ua;
   2281 
   2282 	NETBSD32TO64_UAP(pid);
   2283 	NETBSD32TO64_UAP(pgid);
   2284 	return (sys_setpgid(p, &ua, retval));
   2285 }
   2286 
   2287 int
   2288 netbsd32_setitimer(p, v, retval)
   2289 	struct proc *p;
   2290 	void *v;
   2291 	register_t *retval;
   2292 {
   2293 	struct netbsd32_setitimer_args /* {
   2294 		syscallarg(int) which;
   2295 		syscallarg(const netbsd32_itimervalp_t) itv;
   2296 		syscallarg(netbsd32_itimervalp_t) oitv;
   2297 	} */ *uap = v;
   2298 	struct netbsd32_itimerval s32it, *itvp;
   2299 	int which = SCARG(uap, which);
   2300 	struct netbsd32_getitimer_args getargs;
   2301 	struct itimerval aitv;
   2302 	int s, error;
   2303 
   2304 	if ((u_int)which > ITIMER_PROF)
   2305 		return (EINVAL);
   2306 	itvp = (struct netbsd32_itimerval *)(u_long)SCARG(uap, itv);
   2307 	if (itvp && (error = copyin(itvp, &s32it, sizeof(s32it))))
   2308 		return (error);
   2309 	netbsd32_to_itimerval(&s32it, &aitv);
   2310 	if (SCARG(uap, oitv) != NULL) {
   2311 		SCARG(&getargs, which) = which;
   2312 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   2313 		if ((error = netbsd32_getitimer(p, &getargs, retval)) != 0)
   2314 			return (error);
   2315 	}
   2316 	if (itvp == 0)
   2317 		return (0);
   2318 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
   2319 		return (EINVAL);
   2320 	s = splclock();
   2321 	if (which == ITIMER_REAL) {
   2322 		callout_stop(&p->p_realit_ch);
   2323 		if (timerisset(&aitv.it_value)) {
   2324 			/*
   2325 			 * Don't need to check hzto() return value, here.
   2326 			 * callout_reset() does it for us.
   2327 			 */
   2328 			timeradd(&aitv.it_value, &time, &aitv.it_value);
   2329 			callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
   2330 			    realitexpire, p);
   2331 		}
   2332 		p->p_realtimer = aitv;
   2333 	} else
   2334 		p->p_stats->p_timer[which] = aitv;
   2335 	splx(s);
   2336 	return (0);
   2337 }
   2338 
   2339 int
   2340 netbsd32_getitimer(p, v, retval)
   2341 	struct proc *p;
   2342 	void *v;
   2343 	register_t *retval;
   2344 {
   2345 	struct netbsd32_getitimer_args /* {
   2346 		syscallarg(int) which;
   2347 		syscallarg(netbsd32_itimervalp_t) itv;
   2348 	} */ *uap = v;
   2349 	int which = SCARG(uap, which);
   2350 	struct netbsd32_itimerval s32it;
   2351 	struct itimerval aitv;
   2352 	int s;
   2353 
   2354 	if ((u_int)which > ITIMER_PROF)
   2355 		return (EINVAL);
   2356 	s = splclock();
   2357 	if (which == ITIMER_REAL) {
   2358 		/*
   2359 		 * Convert from absolute to relative time in .it_value
   2360 		 * part of real time timer.  If time for real time timer
   2361 		 * has passed return 0, else return difference between
   2362 		 * current time and time for the timer to go off.
   2363 		 */
   2364 		aitv = p->p_realtimer;
   2365 		if (timerisset(&aitv.it_value)) {
   2366 			if (timercmp(&aitv.it_value, &time, <))
   2367 				timerclear(&aitv.it_value);
   2368 			else
   2369 				timersub(&aitv.it_value, &time, &aitv.it_value);
   2370 		}
   2371 	} else
   2372 		aitv = p->p_stats->p_timer[which];
   2373 	splx(s);
   2374 	netbsd32_from_itimerval(&aitv, &s32it);
   2375 	return (copyout(&s32it, (caddr_t)(u_long)SCARG(uap, itv), sizeof(s32it)));
   2376 }
   2377 
   2378 int
   2379 netbsd32_fcntl(p, v, retval)
   2380 	struct proc *p;
   2381 	void *v;
   2382 	register_t *retval;
   2383 {
   2384 	struct netbsd32_fcntl_args /* {
   2385 		syscallarg(int) fd;
   2386 		syscallarg(int) cmd;
   2387 		syscallarg(netbsd32_voidp) arg;
   2388 	} */ *uap = v;
   2389 	struct sys_fcntl_args ua;
   2390 
   2391 	NETBSD32TO64_UAP(fd);
   2392 	NETBSD32TO64_UAP(cmd);
   2393 	NETBSD32TOP_UAP(arg, void);
   2394 	/* XXXX we can do this 'cause flock doesn't change */
   2395 	return (sys_fcntl(p, &ua, retval));
   2396 }
   2397 
   2398 int
   2399 netbsd32_dup2(p, v, retval)
   2400 	struct proc *p;
   2401 	void *v;
   2402 	register_t *retval;
   2403 {
   2404 	struct netbsd32_dup2_args /* {
   2405 		syscallarg(int) from;
   2406 		syscallarg(int) to;
   2407 	} */ *uap = v;
   2408 	struct sys_dup2_args ua;
   2409 
   2410 	NETBSD32TO64_UAP(from);
   2411 	NETBSD32TO64_UAP(to);
   2412 	return (sys_dup2(p, &ua, retval));
   2413 }
   2414 
   2415 int
   2416 netbsd32_select(p, v, retval)
   2417 	struct proc *p;
   2418 	void *v;
   2419 	register_t *retval;
   2420 {
   2421 	struct netbsd32_select_args /* {
   2422 		syscallarg(int) nd;
   2423 		syscallarg(netbsd32_fd_setp_t) in;
   2424 		syscallarg(netbsd32_fd_setp_t) ou;
   2425 		syscallarg(netbsd32_fd_setp_t) ex;
   2426 		syscallarg(netbsd32_timevalp_t) tv;
   2427 	} */ *uap = v;
   2428 /* This one must be done in-line 'cause of the timeval */
   2429 	struct netbsd32_timeval tv32;
   2430 	caddr_t bits;
   2431 	char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
   2432 	struct timeval atv;
   2433 	int s, ncoll, error = 0, timo;
   2434 	size_t ni;
   2435 	extern int	selwait, nselcoll;
   2436 	extern int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
   2437 
   2438 	if (SCARG(uap, nd) < 0)
   2439 		return (EINVAL);
   2440 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
   2441 		/* forgiving; slightly wrong */
   2442 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
   2443 	}
   2444 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
   2445 	if (ni * 6 > sizeof(smallbits))
   2446 		bits = malloc(ni * 6, M_TEMP, M_WAITOK);
   2447 	else
   2448 		bits = smallbits;
   2449 
   2450 #define	getbits(name, x) \
   2451 	if (SCARG(uap, name)) { \
   2452 		error = copyin((caddr_t)(u_long)SCARG(uap, name), bits + ni * x, ni); \
   2453 		if (error) \
   2454 			goto done; \
   2455 	} else \
   2456 		memset(bits + ni * x, 0, ni);
   2457 	getbits(in, 0);
   2458 	getbits(ou, 1);
   2459 	getbits(ex, 2);
   2460 #undef	getbits
   2461 
   2462 	if (SCARG(uap, tv)) {
   2463 		error = copyin((caddr_t)(u_long)SCARG(uap, tv), (caddr_t)&tv32,
   2464 			sizeof(tv32));
   2465 		if (error)
   2466 			goto done;
   2467 		netbsd32_to_timeval(&tv32, &atv);
   2468 		if (itimerfix(&atv)) {
   2469 			error = EINVAL;
   2470 			goto done;
   2471 		}
   2472 		s = splclock();
   2473 		timeradd(&atv, &time, &atv);
   2474 		splx(s);
   2475 	} else
   2476 		timo = 0;
   2477 retry:
   2478 	ncoll = nselcoll;
   2479 	p->p_flag |= P_SELECT;
   2480 	error = selscan(p, (fd_mask *)(bits + ni * 0),
   2481 			   (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
   2482 	if (error || *retval)
   2483 		goto done;
   2484 	if (SCARG(uap, tv)) {
   2485 		/*
   2486 		 * We have to recalculate the timeout on every retry.
   2487 		 */
   2488 		timo = hzto(&atv);
   2489 		if (timo <= 0)
   2490 			goto done;
   2491 	}
   2492 	s = splhigh();
   2493 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
   2494 		splx(s);
   2495 		goto retry;
   2496 	}
   2497 	p->p_flag &= ~P_SELECT;
   2498 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
   2499 	splx(s);
   2500 	if (error == 0)
   2501 		goto retry;
   2502 done:
   2503 	p->p_flag &= ~P_SELECT;
   2504 	/* select is not restarted after signals... */
   2505 	if (error == ERESTART)
   2506 		error = EINTR;
   2507 	if (error == EWOULDBLOCK)
   2508 		error = 0;
   2509 	if (error == 0) {
   2510 #define	putbits(name, x) \
   2511 		if (SCARG(uap, name)) { \
   2512 			error = copyout(bits + ni * x, (caddr_t)(u_long)SCARG(uap, name), ni); \
   2513 			if (error) \
   2514 				goto out; \
   2515 		}
   2516 		putbits(in, 3);
   2517 		putbits(ou, 4);
   2518 		putbits(ex, 5);
   2519 #undef putbits
   2520 	}
   2521 out:
   2522 	if (ni * 6 > sizeof(smallbits))
   2523 		free(bits, M_TEMP);
   2524 	return (error);
   2525 }
   2526 
   2527 int
   2528 netbsd32_fsync(p, v, retval)
   2529 	struct proc *p;
   2530 	void *v;
   2531 	register_t *retval;
   2532 {
   2533 	struct netbsd32_fsync_args /* {
   2534 		syscallarg(int) fd;
   2535 	} */ *uap = v;
   2536 	struct sys_fsync_args ua;
   2537 
   2538 	NETBSD32TO64_UAP(fd);
   2539 	return (sys_fsync(p, &ua, retval));
   2540 }
   2541 
   2542 int
   2543 netbsd32_setpriority(p, v, retval)
   2544 	struct proc *p;
   2545 	void *v;
   2546 	register_t *retval;
   2547 {
   2548 	struct netbsd32_setpriority_args /* {
   2549 		syscallarg(int) which;
   2550 		syscallarg(int) who;
   2551 		syscallarg(int) prio;
   2552 	} */ *uap = v;
   2553 	struct sys_setpriority_args ua;
   2554 
   2555 	NETBSD32TO64_UAP(which);
   2556 	NETBSD32TO64_UAP(who);
   2557 	NETBSD32TO64_UAP(prio);
   2558 	return (sys_setpriority(p, &ua, retval));
   2559 }
   2560 
   2561 int
   2562 netbsd32_socket(p, v, retval)
   2563 	struct proc *p;
   2564 	void *v;
   2565 	register_t *retval;
   2566 {
   2567 	struct netbsd32_socket_args /* {
   2568 		syscallarg(int) domain;
   2569 		syscallarg(int) type;
   2570 		syscallarg(int) protocol;
   2571 	} */ *uap = v;
   2572 	struct sys_socket_args ua;
   2573 
   2574 	NETBSD32TO64_UAP(domain);
   2575 	NETBSD32TO64_UAP(type);
   2576 	NETBSD32TO64_UAP(protocol);
   2577 	return (sys_socket(p, &ua, retval));
   2578 }
   2579 
   2580 int
   2581 netbsd32_connect(p, v, retval)
   2582 	struct proc *p;
   2583 	void *v;
   2584 	register_t *retval;
   2585 {
   2586 	struct netbsd32_connect_args /* {
   2587 		syscallarg(int) s;
   2588 		syscallarg(const netbsd32_sockaddrp_t) name;
   2589 		syscallarg(int) namelen;
   2590 	} */ *uap = v;
   2591 	struct sys_connect_args ua;
   2592 
   2593 	NETBSD32TO64_UAP(s);
   2594 	NETBSD32TOP_UAP(name, struct sockaddr);
   2595 	NETBSD32TO64_UAP(namelen);
   2596 	return (sys_connect(p, &ua, retval));
   2597 }
   2598 
   2599 int
   2600 netbsd32_getpriority(p, v, retval)
   2601 	struct proc *p;
   2602 	void *v;
   2603 	register_t *retval;
   2604 {
   2605 	struct netbsd32_getpriority_args /* {
   2606 		syscallarg(int) which;
   2607 		syscallarg(int) who;
   2608 	} */ *uap = v;
   2609 	struct sys_getpriority_args ua;
   2610 
   2611 	NETBSD32TO64_UAP(which);
   2612 	NETBSD32TO64_UAP(who);
   2613 	return (sys_getpriority(p, &ua, retval));
   2614 }
   2615 
   2616 int
   2617 netbsd32_bind(p, v, retval)
   2618 	struct proc *p;
   2619 	void *v;
   2620 	register_t *retval;
   2621 {
   2622 	struct netbsd32_bind_args /* {
   2623 		syscallarg(int) s;
   2624 		syscallarg(const netbsd32_sockaddrp_t) name;
   2625 		syscallarg(int) namelen;
   2626 	} */ *uap = v;
   2627 	struct sys_bind_args ua;
   2628 
   2629 	NETBSD32TO64_UAP(s);
   2630 	NETBSD32TOP_UAP(name, struct sockaddr);
   2631 	NETBSD32TO64_UAP(namelen);
   2632 	return (sys_bind(p, &ua, retval));
   2633 }
   2634 
   2635 int
   2636 netbsd32_setsockopt(p, v, retval)
   2637 	struct proc *p;
   2638 	void *v;
   2639 	register_t *retval;
   2640 {
   2641 	struct netbsd32_setsockopt_args /* {
   2642 		syscallarg(int) s;
   2643 		syscallarg(int) level;
   2644 		syscallarg(int) name;
   2645 		syscallarg(const netbsd32_voidp) val;
   2646 		syscallarg(int) valsize;
   2647 	} */ *uap = v;
   2648 	struct sys_setsockopt_args ua;
   2649 
   2650 	NETBSD32TO64_UAP(s);
   2651 	NETBSD32TO64_UAP(level);
   2652 	NETBSD32TO64_UAP(name);
   2653 	NETBSD32TOP_UAP(val, void);
   2654 	NETBSD32TO64_UAP(valsize);
   2655 	/* may be more efficient to do this inline. */
   2656 	return (sys_setsockopt(p, &ua, retval));
   2657 }
   2658 
   2659 int
   2660 netbsd32_listen(p, v, retval)
   2661 	struct proc *p;
   2662 	void *v;
   2663 	register_t *retval;
   2664 {
   2665 	struct netbsd32_listen_args /* {
   2666 		syscallarg(int) s;
   2667 		syscallarg(int) backlog;
   2668 	} */ *uap = v;
   2669 	struct sys_listen_args ua;
   2670 
   2671 	NETBSD32TO64_UAP(s);
   2672 	NETBSD32TO64_UAP(backlog);
   2673 	return (sys_listen(p, &ua, retval));
   2674 }
   2675 
   2676 int
   2677 netbsd32_gettimeofday(p, v, retval)
   2678 	struct proc *p;
   2679 	void *v;
   2680 	register_t *retval;
   2681 {
   2682 	struct netbsd32_gettimeofday_args /* {
   2683 		syscallarg(netbsd32_timevalp_t) tp;
   2684 		syscallarg(netbsd32_timezonep_t) tzp;
   2685 	} */ *uap = v;
   2686 	struct timeval atv;
   2687 	struct netbsd32_timeval tv32;
   2688 	int error = 0;
   2689 	struct netbsd32_timezone tzfake;
   2690 
   2691 	if (SCARG(uap, tp)) {
   2692 		microtime(&atv);
   2693 		netbsd32_from_timeval(&atv, &tv32);
   2694 		error = copyout(&tv32, (caddr_t)(u_long)SCARG(uap, tp), sizeof(tv32));
   2695 		if (error)
   2696 			return (error);
   2697 	}
   2698 	if (SCARG(uap, tzp)) {
   2699 		/*
   2700 		 * NetBSD has no kernel notion of time zone, so we just
   2701 		 * fake up a timezone struct and return it if demanded.
   2702 		 */
   2703 		tzfake.tz_minuteswest = 0;
   2704 		tzfake.tz_dsttime = 0;
   2705 		error = copyout(&tzfake, (caddr_t)(u_long)SCARG(uap, tzp), sizeof(tzfake));
   2706 	}
   2707 	return (error);
   2708 }
   2709 
   2710 #if 0
   2711 static int settime32 __P((struct timeval *));
   2712 /* This function is used by clock_settime and settimeofday */
   2713 static int
   2714 settime32(tv)
   2715 	struct timeval *tv;
   2716 {
   2717 	struct timeval delta;
   2718 	int s;
   2719 
   2720 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
   2721 	s = splclock();
   2722 	timersub(tv, &time, &delta);
   2723 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1)
   2724 		return (EPERM);
   2725 #ifdef notyet
   2726 	if ((delta.tv_sec < 86400) && securelevel > 0)
   2727 		return (EPERM);
   2728 #endif
   2729 	time = *tv;
   2730 	(void) spllowersoftclock();
   2731 	timeradd(&boottime, &delta, &boottime);
   2732 	timeradd(&runtime, &delta, &runtime);
   2733 #	if defined(NFS) || defined(NFSSERVER)
   2734 	{
   2735 		extern void	nqnfs_lease_updatetime __P((int));
   2736 
   2737 		nqnfs_lease_updatetime(delta.tv_sec);
   2738 	}
   2739 #	endif
   2740 	splx(s);
   2741 	resettodr();
   2742 	return (0);
   2743 }
   2744 #endif
   2745 
   2746 int
   2747 netbsd32_settimeofday(p, v, retval)
   2748 	struct proc *p;
   2749 	void *v;
   2750 	register_t *retval;
   2751 {
   2752 	struct netbsd32_settimeofday_args /* {
   2753 		syscallarg(const netbsd32_timevalp_t) tv;
   2754 		syscallarg(const netbsd32_timezonep_t) tzp;
   2755 	} */ *uap = v;
   2756 	struct netbsd32_timeval atv32;
   2757 	struct timeval atv;
   2758 	struct netbsd32_timezone atz;
   2759 	int error;
   2760 
   2761 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   2762 		return (error);
   2763 	/* Verify all parameters before changing time. */
   2764 	if (SCARG(uap, tv) && (error = copyin((caddr_t)(u_long)SCARG(uap, tv),
   2765 	    &atv32, sizeof(atv32))))
   2766 		return (error);
   2767 	netbsd32_to_timeval(&atv32, &atv);
   2768 	/* XXX since we don't use tz, probably no point in doing copyin. */
   2769 	if (SCARG(uap, tzp) && (error = copyin((caddr_t)(u_long)SCARG(uap, tzp),
   2770 	    &atz, sizeof(atz))))
   2771 		return (error);
   2772 	if (SCARG(uap, tv))
   2773 		if ((error = settime(&atv)))
   2774 			return (error);
   2775 	/*
   2776 	 * NetBSD has no kernel notion of time zone, and only an
   2777 	 * obsolete program would try to set it, so we log a warning.
   2778 	 */
   2779 	if (SCARG(uap, tzp))
   2780 		printf("pid %d attempted to set the "
   2781 		    "(obsolete) kernel time zone\n", p->p_pid);
   2782 	return (0);
   2783 }
   2784 
   2785 int
   2786 netbsd32_fchown(p, v, retval)
   2787 	struct proc *p;
   2788 	void *v;
   2789 	register_t *retval;
   2790 {
   2791 	struct netbsd32_fchown_args /* {
   2792 		syscallarg(int) fd;
   2793 		syscallarg(uid_t) uid;
   2794 		syscallarg(gid_t) gid;
   2795 	} */ *uap = v;
   2796 	struct sys_fchown_args ua;
   2797 
   2798 	NETBSD32TO64_UAP(fd);
   2799 	NETBSD32TO64_UAP(uid);
   2800 	NETBSD32TO64_UAP(gid);
   2801 	return (sys_fchown(p, &ua, retval));
   2802 }
   2803 
   2804 int
   2805 netbsd32_fchmod(p, v, retval)
   2806 	struct proc *p;
   2807 	void *v;
   2808 	register_t *retval;
   2809 {
   2810 	struct netbsd32_fchmod_args /* {
   2811 		syscallarg(int) fd;
   2812 		syscallarg(mode_t) mode;
   2813 	} */ *uap = v;
   2814 	struct sys_fchmod_args ua;
   2815 
   2816 	NETBSD32TO64_UAP(fd);
   2817 	NETBSD32TO64_UAP(mode);
   2818 	return (sys_fchmod(p, &ua, retval));
   2819 }
   2820 
   2821 int
   2822 netbsd32_setreuid(p, v, retval)
   2823 	struct proc *p;
   2824 	void *v;
   2825 	register_t *retval;
   2826 {
   2827 	struct netbsd32_setreuid_args /* {
   2828 		syscallarg(uid_t) ruid;
   2829 		syscallarg(uid_t) euid;
   2830 	} */ *uap = v;
   2831 	struct sys_setreuid_args ua;
   2832 
   2833 	NETBSD32TO64_UAP(ruid);
   2834 	NETBSD32TO64_UAP(euid);
   2835 	return (sys_setreuid(p, &ua, retval));
   2836 }
   2837 
   2838 int
   2839 netbsd32_setregid(p, v, retval)
   2840 	struct proc *p;
   2841 	void *v;
   2842 	register_t *retval;
   2843 {
   2844 	struct netbsd32_setregid_args /* {
   2845 		syscallarg(gid_t) rgid;
   2846 		syscallarg(gid_t) egid;
   2847 	} */ *uap = v;
   2848 	struct sys_setregid_args ua;
   2849 
   2850 	NETBSD32TO64_UAP(rgid);
   2851 	NETBSD32TO64_UAP(egid);
   2852 	return (sys_setregid(p, &ua, retval));
   2853 }
   2854 
   2855 int
   2856 netbsd32_getrusage(p, v, retval)
   2857 	struct proc *p;
   2858 	void *v;
   2859 	register_t *retval;
   2860 {
   2861 	struct netbsd32_getrusage_args /* {
   2862 		syscallarg(int) who;
   2863 		syscallarg(netbsd32_rusagep_t) rusage;
   2864 	} */ *uap = v;
   2865 	struct rusage *rup;
   2866 	struct netbsd32_rusage ru;
   2867 
   2868 	switch (SCARG(uap, who)) {
   2869 
   2870 	case RUSAGE_SELF:
   2871 		rup = &p->p_stats->p_ru;
   2872 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
   2873 		break;
   2874 
   2875 	case RUSAGE_CHILDREN:
   2876 		rup = &p->p_stats->p_cru;
   2877 		break;
   2878 
   2879 	default:
   2880 		return (EINVAL);
   2881 	}
   2882 	netbsd32_from_rusage(rup, &ru);
   2883 	return (copyout(&ru, (caddr_t)(u_long)SCARG(uap, rusage), sizeof(ru)));
   2884 }
   2885 
   2886 int
   2887 netbsd32_getsockopt(p, v, retval)
   2888 	struct proc *p;
   2889 	void *v;
   2890 	register_t *retval;
   2891 {
   2892 	struct netbsd32_getsockopt_args /* {
   2893 		syscallarg(int) s;
   2894 		syscallarg(int) level;
   2895 		syscallarg(int) name;
   2896 		syscallarg(netbsd32_voidp) val;
   2897 		syscallarg(netbsd32_intp) avalsize;
   2898 	} */ *uap = v;
   2899 	struct sys_getsockopt_args ua;
   2900 
   2901 	NETBSD32TO64_UAP(s);
   2902 	NETBSD32TO64_UAP(level);
   2903 	NETBSD32TO64_UAP(name);
   2904 	NETBSD32TOP_UAP(val, void);
   2905 	NETBSD32TOP_UAP(avalsize, int);
   2906 	return (sys_getsockopt(p, &ua, retval));
   2907 }
   2908 
   2909 int
   2910 netbsd32_readv(p, v, retval)
   2911 	struct proc *p;
   2912 	void *v;
   2913 	register_t *retval;
   2914 {
   2915 	struct netbsd32_readv_args /* {
   2916 		syscallarg(int) fd;
   2917 		syscallarg(const netbsd32_iovecp_t) iovp;
   2918 		syscallarg(int) iovcnt;
   2919 	} */ *uap = v;
   2920 	int fd = SCARG(uap, fd);
   2921 	struct file *fp;
   2922 	struct filedesc *fdp = p->p_fd;
   2923 
   2924 	if ((u_int)fd >= fdp->fd_nfiles ||
   2925 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   2926 	    (fp->f_flag & FREAD) == 0)
   2927 		return (EBADF);
   2928 
   2929 	return (dofilereadv32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp),
   2930 			      SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
   2931 }
   2932 
   2933 /* Damn thing copies in the iovec! */
   2934 int
   2935 dofilereadv32(p, fd, fp, iovp, iovcnt, offset, flags, retval)
   2936 	struct proc *p;
   2937 	int fd;
   2938 	struct file *fp;
   2939 	struct netbsd32_iovec *iovp;
   2940 	int iovcnt;
   2941 	off_t *offset;
   2942 	int flags;
   2943 	register_t *retval;
   2944 {
   2945 	struct uio auio;
   2946 	struct iovec *iov;
   2947 	struct iovec *needfree;
   2948 	struct iovec aiov[UIO_SMALLIOV];
   2949 	long i, cnt, error = 0;
   2950 	u_int iovlen;
   2951 #ifdef KTRACE
   2952 	struct iovec *ktriov = NULL;
   2953 #endif
   2954 
   2955 	/* note: can't use iovlen until iovcnt is validated */
   2956 	iovlen = iovcnt * sizeof(struct iovec);
   2957 	if ((u_int)iovcnt > UIO_SMALLIOV) {
   2958 		if ((u_int)iovcnt > IOV_MAX)
   2959 			return (EINVAL);
   2960 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
   2961 		needfree = iov;
   2962 	} else if ((u_int)iovcnt > 0) {
   2963 		iov = aiov;
   2964 		needfree = NULL;
   2965 	} else
   2966 		return (EINVAL);
   2967 
   2968 	auio.uio_iov = iov;
   2969 	auio.uio_iovcnt = iovcnt;
   2970 	auio.uio_rw = UIO_READ;
   2971 	auio.uio_segflg = UIO_USERSPACE;
   2972 	auio.uio_procp = p;
   2973 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
   2974 	if (error)
   2975 		goto done;
   2976 	auio.uio_resid = 0;
   2977 	for (i = 0; i < iovcnt; i++) {
   2978 		auio.uio_resid += iov->iov_len;
   2979 		/*
   2980 		 * Reads return ssize_t because -1 is returned on error.
   2981 		 * Therefore we must restrict the length to SSIZE_MAX to
   2982 		 * avoid garbage return values.
   2983 		 */
   2984 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   2985 			error = EINVAL;
   2986 			goto done;
   2987 		}
   2988 		iov++;
   2989 	}
   2990 #ifdef KTRACE
   2991 	/*
   2992 	 * if tracing, save a copy of iovec
   2993 	 */
   2994 	if (KTRPOINT(p, KTR_GENIO))  {
   2995 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   2996 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   2997 	}
   2998 #endif
   2999 	cnt = auio.uio_resid;
   3000 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
   3001 	if (error)
   3002 		if (auio.uio_resid != cnt && (error == ERESTART ||
   3003 		    error == EINTR || error == EWOULDBLOCK))
   3004 			error = 0;
   3005 	cnt -= auio.uio_resid;
   3006 #ifdef KTRACE
   3007 	if (KTRPOINT(p, KTR_GENIO))
   3008 		if (error == 0) {
   3009 			ktrgenio(p, fd, UIO_READ, ktriov, cnt,
   3010 			    error);
   3011 		FREE(ktriov, M_TEMP);
   3012 	}
   3013 #endif
   3014 	*retval = cnt;
   3015 done:
   3016 	if (needfree)
   3017 		FREE(needfree, M_IOV);
   3018 	return (error);
   3019 }
   3020 
   3021 
   3022 int
   3023 netbsd32_writev(p, v, retval)
   3024 	struct proc *p;
   3025 	void *v;
   3026 	register_t *retval;
   3027 {
   3028 	struct netbsd32_writev_args /* {
   3029 		syscallarg(int) fd;
   3030 		syscallarg(const netbsd32_iovecp_t) iovp;
   3031 		syscallarg(int) iovcnt;
   3032 	} */ *uap = v;
   3033 	int fd = SCARG(uap, fd);
   3034 	struct file *fp;
   3035 	struct filedesc *fdp = p->p_fd;
   3036 
   3037 	if ((u_int)fd >= fdp->fd_nfiles ||
   3038 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   3039 	    (fp->f_flag & FWRITE) == 0)
   3040 		return (EBADF);
   3041 
   3042 	return (dofilewritev32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp),
   3043 			       SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
   3044 }
   3045 
   3046 int
   3047 dofilewritev32(p, fd, fp, iovp, iovcnt, offset, flags, retval)
   3048 	struct proc *p;
   3049 	int fd;
   3050 	struct file *fp;
   3051 	struct netbsd32_iovec *iovp;
   3052 	int iovcnt;
   3053 	off_t *offset;
   3054 	int flags;
   3055 	register_t *retval;
   3056 {
   3057 	struct uio auio;
   3058 	struct iovec *iov;
   3059 	struct iovec *needfree;
   3060 	struct iovec aiov[UIO_SMALLIOV];
   3061 	long i, cnt, error = 0;
   3062 	u_int iovlen;
   3063 #ifdef KTRACE
   3064 	struct iovec *ktriov = NULL;
   3065 #endif
   3066 
   3067 	/* note: can't use iovlen until iovcnt is validated */
   3068 	iovlen = iovcnt * sizeof(struct iovec);
   3069 	if ((u_int)iovcnt > UIO_SMALLIOV) {
   3070 		if ((u_int)iovcnt > IOV_MAX)
   3071 			return (EINVAL);
   3072 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
   3073 		needfree = iov;
   3074 	} else if ((u_int)iovcnt > 0) {
   3075 		iov = aiov;
   3076 		needfree = NULL;
   3077 	} else
   3078 		return (EINVAL);
   3079 
   3080 	auio.uio_iov = iov;
   3081 	auio.uio_iovcnt = iovcnt;
   3082 	auio.uio_rw = UIO_WRITE;
   3083 	auio.uio_segflg = UIO_USERSPACE;
   3084 	auio.uio_procp = p;
   3085 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
   3086 	if (error)
   3087 		goto done;
   3088 	auio.uio_resid = 0;
   3089 	for (i = 0; i < iovcnt; i++) {
   3090 		auio.uio_resid += iov->iov_len;
   3091 		/*
   3092 		 * Writes return ssize_t because -1 is returned on error.
   3093 		 * Therefore we must restrict the length to SSIZE_MAX to
   3094 		 * avoid garbage return values.
   3095 		 */
   3096 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   3097 			error = EINVAL;
   3098 			goto done;
   3099 		}
   3100 		iov++;
   3101 	}
   3102 #ifdef KTRACE
   3103 	/*
   3104 	 * if tracing, save a copy of iovec
   3105 	 */
   3106 	if (KTRPOINT(p, KTR_GENIO))  {
   3107 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   3108 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   3109 	}
   3110 #endif
   3111 	cnt = auio.uio_resid;
   3112 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
   3113 	if (error) {
   3114 		if (auio.uio_resid != cnt && (error == ERESTART ||
   3115 		    error == EINTR || error == EWOULDBLOCK))
   3116 			error = 0;
   3117 		if (error == EPIPE)
   3118 			psignal(p, SIGPIPE);
   3119 	}
   3120 	cnt -= auio.uio_resid;
   3121 #ifdef KTRACE
   3122 	if (KTRPOINT(p, KTR_GENIO))
   3123 		if (error == 0) {
   3124 			ktrgenio(p, fd, UIO_WRITE, ktriov, cnt,
   3125 			    error);
   3126 		FREE(ktriov, M_TEMP);
   3127 	}
   3128 #endif
   3129 	*retval = cnt;
   3130 done:
   3131 	if (needfree)
   3132 		FREE(needfree, M_IOV);
   3133 	return (error);
   3134 }
   3135 
   3136 
   3137 int
   3138 netbsd32_rename(p, v, retval)
   3139 	struct proc *p;
   3140 	void *v;
   3141 	register_t *retval;
   3142 {
   3143 	struct netbsd32_rename_args /* {
   3144 		syscallarg(const netbsd32_charp) from;
   3145 		syscallarg(const netbsd32_charp) to;
   3146 	} */ *uap = v;
   3147 	struct sys_rename_args ua;
   3148 
   3149 	NETBSD32TOP_UAP(from, const char);
   3150 	NETBSD32TOP_UAP(to, const char)
   3151 
   3152 	return (sys_rename(p, &ua, retval));
   3153 }
   3154 
   3155 int
   3156 netbsd32_flock(p, v, retval)
   3157 	struct proc *p;
   3158 	void *v;
   3159 	register_t *retval;
   3160 {
   3161 	struct netbsd32_flock_args /* {
   3162 		syscallarg(int) fd;
   3163 		syscallarg(int) how;
   3164 	} */ *uap = v;
   3165 	struct sys_flock_args ua;
   3166 
   3167 	NETBSD32TO64_UAP(fd);
   3168 	NETBSD32TO64_UAP(how)
   3169 
   3170 	return (sys_flock(p, &ua, retval));
   3171 }
   3172 
   3173 int
   3174 netbsd32_mkfifo(p, v, retval)
   3175 	struct proc *p;
   3176 	void *v;
   3177 	register_t *retval;
   3178 {
   3179 	struct netbsd32_mkfifo_args /* {
   3180 		syscallarg(const netbsd32_charp) path;
   3181 		syscallarg(mode_t) mode;
   3182 	} */ *uap = v;
   3183 	struct sys_mkfifo_args ua;
   3184 
   3185 	NETBSD32TOP_UAP(path, const char)
   3186 	NETBSD32TO64_UAP(mode);
   3187 	return (sys_mkfifo(p, &ua, retval));
   3188 }
   3189 
   3190 int
   3191 netbsd32_shutdown(p, v, retval)
   3192 	struct proc *p;
   3193 	void *v;
   3194 	register_t *retval;
   3195 {
   3196 	struct netbsd32_shutdown_args /* {
   3197 		syscallarg(int) s;
   3198 		syscallarg(int) how;
   3199 	} */ *uap = v;
   3200 	struct sys_shutdown_args ua;
   3201 
   3202 	NETBSD32TO64_UAP(s)
   3203 	NETBSD32TO64_UAP(how);
   3204 	return (sys_shutdown(p, &ua, retval));
   3205 }
   3206 
   3207 int
   3208 netbsd32_socketpair(p, v, retval)
   3209 	struct proc *p;
   3210 	void *v;
   3211 	register_t *retval;
   3212 {
   3213 	struct netbsd32_socketpair_args /* {
   3214 		syscallarg(int) domain;
   3215 		syscallarg(int) type;
   3216 		syscallarg(int) protocol;
   3217 		syscallarg(netbsd32_intp) rsv;
   3218 	} */ *uap = v;
   3219 	struct sys_socketpair_args ua;
   3220 
   3221 	NETBSD32TO64_UAP(domain);
   3222 	NETBSD32TO64_UAP(type);
   3223 	NETBSD32TO64_UAP(protocol);
   3224 	NETBSD32TOP_UAP(rsv, int);
   3225 	/* Since we're just copying out two `int's we can do this */
   3226 	return (sys_socketpair(p, &ua, retval));
   3227 }
   3228 
   3229 int
   3230 netbsd32_mkdir(p, v, retval)
   3231 	struct proc *p;
   3232 	void *v;
   3233 	register_t *retval;
   3234 {
   3235 	struct netbsd32_mkdir_args /* {
   3236 		syscallarg(const netbsd32_charp) path;
   3237 		syscallarg(mode_t) mode;
   3238 	} */ *uap = v;
   3239 	struct sys_mkdir_args ua;
   3240 
   3241 	NETBSD32TOP_UAP(path, const char)
   3242 	NETBSD32TO64_UAP(mode);
   3243 	return (sys_mkdir(p, &ua, retval));
   3244 }
   3245 
   3246 int
   3247 netbsd32_rmdir(p, v, retval)
   3248 	struct proc *p;
   3249 	void *v;
   3250 	register_t *retval;
   3251 {
   3252 	struct netbsd32_rmdir_args /* {
   3253 		syscallarg(const netbsd32_charp) path;
   3254 	} */ *uap = v;
   3255 	struct sys_rmdir_args ua;
   3256 
   3257 	NETBSD32TOP_UAP(path, const char);
   3258 	return (sys_rmdir(p, &ua, retval));
   3259 }
   3260 
   3261 int
   3262 netbsd32_utimes(p, v, retval)
   3263 	struct proc *p;
   3264 	void *v;
   3265 	register_t *retval;
   3266 {
   3267 	struct netbsd32_utimes_args /* {
   3268 		syscallarg(const netbsd32_charp) path;
   3269 		syscallarg(const netbsd32_timevalp_t) tptr;
   3270 	} */ *uap = v;
   3271 	int error;
   3272 	struct nameidata nd;
   3273 
   3274 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (char *)(u_long)SCARG(uap, path), p);
   3275 	if ((error = namei(&nd)) != 0)
   3276 		return (error);
   3277 
   3278 	error = change_utimes32(nd.ni_vp, (struct timeval *)(u_long)SCARG(uap, tptr), p);
   3279 
   3280 	vrele(nd.ni_vp);
   3281 	return (error);
   3282 }
   3283 
   3284 /*
   3285  * Common routine to set access and modification times given a vnode.
   3286  */
   3287 static int
   3288 change_utimes32(vp, tptr, p)
   3289 	struct vnode *vp;
   3290 	struct timeval *tptr;
   3291 	struct proc *p;
   3292 {
   3293 	struct netbsd32_timeval tv32[2];
   3294 	struct timeval tv[2];
   3295 	struct vattr vattr;
   3296 	int error;
   3297 
   3298 	VATTR_NULL(&vattr);
   3299 	if (tptr == NULL) {
   3300 		microtime(&tv[0]);
   3301 		tv[1] = tv[0];
   3302 		vattr.va_vaflags |= VA_UTIMES_NULL;
   3303 	} else {
   3304 		error = copyin(tptr, tv, sizeof(tv));
   3305 		if (error)
   3306 			return (error);
   3307 	}
   3308 	netbsd32_to_timeval(&tv32[0], &tv[0]);
   3309 	netbsd32_to_timeval(&tv32[1], &tv[1]);
   3310 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   3311 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3312 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   3313 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   3314 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   3315 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   3316 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   3317 	VOP_UNLOCK(vp, 0);
   3318 	return (error);
   3319 }
   3320 
   3321 int
   3322 netbsd32_adjtime(p, v, retval)
   3323 	struct proc *p;
   3324 	void *v;
   3325 	register_t *retval;
   3326 {
   3327 	struct netbsd32_adjtime_args /* {
   3328 		syscallarg(const netbsd32_timevalp_t) delta;
   3329 		syscallarg(netbsd32_timevalp_t) olddelta;
   3330 	} */ *uap = v;
   3331 	struct netbsd32_timeval atv;
   3332 	int32_t ndelta, ntickdelta, odelta;
   3333 	int s, error;
   3334 	extern long bigadj, timedelta;
   3335 	extern int tickdelta;
   3336 
   3337 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   3338 		return (error);
   3339 
   3340 	error = copyin((caddr_t)(u_long)SCARG(uap, delta), &atv, sizeof(struct timeval));
   3341 	if (error)
   3342 		return (error);
   3343 	/*
   3344 	 * Compute the total correction and the rate at which to apply it.
   3345 	 * Round the adjustment down to a whole multiple of the per-tick
   3346 	 * delta, so that after some number of incremental changes in
   3347 	 * hardclock(), tickdelta will become zero, lest the correction
   3348 	 * overshoot and start taking us away from the desired final time.
   3349 	 */
   3350 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
   3351 	if (ndelta > bigadj)
   3352 		ntickdelta = 10 * tickadj;
   3353 	else
   3354 		ntickdelta = tickadj;
   3355 	if (ndelta % ntickdelta)
   3356 		ndelta = ndelta / ntickdelta * ntickdelta;
   3357 
   3358 	/*
   3359 	 * To make hardclock()'s job easier, make the per-tick delta negative
   3360 	 * if we want time to run slower; then hardclock can simply compute
   3361 	 * tick + tickdelta, and subtract tickdelta from timedelta.
   3362 	 */
   3363 	if (ndelta < 0)
   3364 		ntickdelta = -ntickdelta;
   3365 	s = splclock();
   3366 	odelta = timedelta;
   3367 	timedelta = ndelta;
   3368 	tickdelta = ntickdelta;
   3369 	splx(s);
   3370 
   3371 	if (SCARG(uap, olddelta)) {
   3372 		atv.tv_sec = odelta / 1000000;
   3373 		atv.tv_usec = odelta % 1000000;
   3374 		(void) copyout(&atv, (caddr_t)(u_long)SCARG(uap, olddelta),
   3375 		    sizeof(struct timeval));
   3376 	}
   3377 	return (0);
   3378 }
   3379 
   3380 int
   3381 netbsd32_quotactl(p, v, retval)
   3382 	struct proc *p;
   3383 	void *v;
   3384 	register_t *retval;
   3385 {
   3386 	struct netbsd32_quotactl_args /* {
   3387 		syscallarg(const netbsd32_charp) path;
   3388 		syscallarg(int) cmd;
   3389 		syscallarg(int) uid;
   3390 		syscallarg(netbsd32_caddr_t) arg;
   3391 	} */ *uap = v;
   3392 	struct sys_quotactl_args ua;
   3393 
   3394 	NETBSD32TOP_UAP(path, const char);
   3395 	NETBSD32TO64_UAP(cmd);
   3396 	NETBSD32TO64_UAP(uid);
   3397 	NETBSD32TOX64_UAP(arg, caddr_t);
   3398 	return (sys_quotactl(p, &ua, retval));
   3399 }
   3400 
   3401 #if defined(NFS) || defined(NFSSERVER)
   3402 int
   3403 netbsd32_nfssvc(p, v, retval)
   3404 	struct proc *p;
   3405 	void *v;
   3406 	register_t *retval;
   3407 {
   3408 #if 0
   3409 	struct netbsd32_nfssvc_args /* {
   3410 		syscallarg(int) flag;
   3411 		syscallarg(netbsd32_voidp) argp;
   3412 	} */ *uap = v;
   3413 	struct sys_nfssvc_args ua;
   3414 
   3415 	NETBSD32TO64_UAP(flag);
   3416 	NETBSD32TOP_UAP(argp, void);
   3417 	return (sys_nfssvc(p, &ua, retval));
   3418 #else
   3419 	/* Why would we want to support a 32-bit nfsd? */
   3420 	return (ENOSYS);
   3421 #endif
   3422 }
   3423 #endif
   3424 
   3425 int
   3426 netbsd32_statfs(p, v, retval)
   3427 	struct proc *p;
   3428 	void *v;
   3429 	register_t *retval;
   3430 {
   3431 	struct netbsd32_statfs_args /* {
   3432 		syscallarg(const netbsd32_charp) path;
   3433 		syscallarg(netbsd32_statfsp_t) buf;
   3434 	} */ *uap = v;
   3435 	struct mount *mp;
   3436 	struct statfs *sp;
   3437 	struct netbsd32_statfs s32;
   3438 	int error;
   3439 	struct nameidata nd;
   3440 
   3441 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (char *)(u_long)SCARG(uap, path), p);
   3442 	if ((error = namei(&nd)) != 0)
   3443 		return (error);
   3444 	mp = nd.ni_vp->v_mount;
   3445 	sp = &mp->mnt_stat;
   3446 	vrele(nd.ni_vp);
   3447 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   3448 		return (error);
   3449 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   3450 	netbsd32_from_statfs(sp, &s32);
   3451 	return (copyout(&s32, (caddr_t)(u_long)SCARG(uap, buf), sizeof(s32)));
   3452 }
   3453 
   3454 int
   3455 netbsd32_fstatfs(p, v, retval)
   3456 	struct proc *p;
   3457 	void *v;
   3458 	register_t *retval;
   3459 {
   3460 	struct netbsd32_fstatfs_args /* {
   3461 		syscallarg(int) fd;
   3462 		syscallarg(netbsd32_statfsp_t) buf;
   3463 	} */ *uap = v;
   3464 	struct file *fp;
   3465 	struct mount *mp;
   3466 	struct statfs *sp;
   3467 	struct netbsd32_statfs s32;
   3468 	int error;
   3469 
   3470 	/* getvnode() will use the descriptor for us */
   3471 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3472 		return (error);
   3473 	mp = ((struct vnode *)fp->f_data)->v_mount;
   3474 	sp = &mp->mnt_stat;
   3475 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   3476 		goto out;
   3477 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   3478 	netbsd32_from_statfs(sp, &s32);
   3479 	error = copyout(&s32, (caddr_t)(u_long)SCARG(uap, buf), sizeof(s32));
   3480  out:
   3481 	FILE_UNUSE(fp, p);
   3482 	return (error);
   3483 }
   3484 
   3485 #if defined(NFS) || defined(NFSSERVER)
   3486 int
   3487 netbsd32_getfh(p, v, retval)
   3488 	struct proc *p;
   3489 	void *v;
   3490 	register_t *retval;
   3491 {
   3492 	struct netbsd32_getfh_args /* {
   3493 		syscallarg(const netbsd32_charp) fname;
   3494 		syscallarg(netbsd32_fhandlep_t) fhp;
   3495 	} */ *uap = v;
   3496 	struct sys_getfh_args ua;
   3497 
   3498 	NETBSD32TOP_UAP(fname, const char);
   3499 	NETBSD32TOP_UAP(fhp, struct fhandle);
   3500 	/* Lucky for us a fhandlep_t doesn't change sizes */
   3501 	return (sys_getfh(p, &ua, retval));
   3502 }
   3503 #endif
   3504 
   3505 int
   3506 netbsd32_sysarch(p, v, retval)
   3507 	struct proc *p;
   3508 	void *v;
   3509 	register_t *retval;
   3510 {
   3511 	struct netbsd32_sysarch_args /* {
   3512 		syscallarg(int) op;
   3513 		syscallarg(netbsd32_voidp) parms;
   3514 	} */ *uap = v;
   3515 
   3516 	switch (SCARG(uap, op)) {
   3517 	default:
   3518 		printf("(sparc64) netbsd32_sysarch(%d)\n", SCARG(uap, op));
   3519 		return EINVAL;
   3520 	}
   3521 }
   3522 
   3523 int
   3524 netbsd32_pread(p, v, retval)
   3525 	struct proc *p;
   3526 	void *v;
   3527 	register_t *retval;
   3528 {
   3529 	struct netbsd32_pread_args /* {
   3530 		syscallarg(int) fd;
   3531 		syscallarg(netbsd32_voidp) buf;
   3532 		syscallarg(netbsd32_size_t) nbyte;
   3533 		syscallarg(int) pad;
   3534 		syscallarg(off_t) offset;
   3535 	} */ *uap = v;
   3536 	struct sys_pread_args ua;
   3537 	ssize_t rt;
   3538 	int error;
   3539 
   3540 	NETBSD32TO64_UAP(fd);
   3541 	NETBSD32TOP_UAP(buf, void);
   3542 	NETBSD32TOX_UAP(nbyte, size_t);
   3543 	NETBSD32TO64_UAP(pad);
   3544 	NETBSD32TO64_UAP(offset);
   3545 	error = sys_pread(p, &ua, (register_t *)&rt);
   3546 	*(netbsd32_ssize_t *)retval = rt;
   3547 	return (error);
   3548 }
   3549 
   3550 int
   3551 netbsd32_pwrite(p, v, retval)
   3552 	struct proc *p;
   3553 	void *v;
   3554 	register_t *retval;
   3555 {
   3556 	struct netbsd32_pwrite_args /* {
   3557 		syscallarg(int) fd;
   3558 		syscallarg(const netbsd32_voidp) buf;
   3559 		syscallarg(netbsd32_size_t) nbyte;
   3560 		syscallarg(int) pad;
   3561 		syscallarg(off_t) offset;
   3562 	} */ *uap = v;
   3563 	struct sys_pwrite_args ua;
   3564 	ssize_t rt;
   3565 	int error;
   3566 
   3567 	NETBSD32TO64_UAP(fd);
   3568 	NETBSD32TOP_UAP(buf, void);
   3569 	NETBSD32TOX_UAP(nbyte, size_t);
   3570 	NETBSD32TO64_UAP(pad);
   3571 	NETBSD32TO64_UAP(offset);
   3572 	error = sys_pwrite(p, &ua, (register_t *)&rt);
   3573 	*(netbsd32_ssize_t *)retval = rt;
   3574 	return (error);
   3575 }
   3576 
   3577 #ifdef NTP
   3578 int
   3579 netbsd32_ntp_gettime(p, v, retval)
   3580 	struct proc *p;
   3581 	void *v;
   3582 	register_t *retval;
   3583 {
   3584 	struct netbsd32_ntp_gettime_args /* {
   3585 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
   3586 	} */ *uap = v;
   3587 	struct netbsd32_ntptimeval ntv32;
   3588 	struct timeval atv;
   3589 	struct ntptimeval ntv;
   3590 	int error = 0;
   3591 	int s;
   3592 
   3593 	/* The following are NTP variables */
   3594 	extern long time_maxerror;
   3595 	extern long time_esterror;
   3596 	extern int time_status;
   3597 	extern int time_state;	/* clock state */
   3598 	extern int time_status;	/* clock status bits */
   3599 
   3600 	if (SCARG(uap, ntvp)) {
   3601 		s = splclock();
   3602 #ifdef EXT_CLOCK
   3603 		/*
   3604 		 * The microtime() external clock routine returns a
   3605 		 * status code. If less than zero, we declare an error
   3606 		 * in the clock status word and return the kernel
   3607 		 * (software) time variable. While there are other
   3608 		 * places that call microtime(), this is the only place
   3609 		 * that matters from an application point of view.
   3610 		 */
   3611 		if (microtime(&atv) < 0) {
   3612 			time_status |= STA_CLOCKERR;
   3613 			ntv.time = time;
   3614 		} else
   3615 			time_status &= ~STA_CLOCKERR;
   3616 #else /* EXT_CLOCK */
   3617 		microtime(&atv);
   3618 #endif /* EXT_CLOCK */
   3619 		ntv.time = atv;
   3620 		ntv.maxerror = time_maxerror;
   3621 		ntv.esterror = time_esterror;
   3622 		(void) splx(s);
   3623 
   3624 		netbsd32_from_timeval(&ntv.time, &ntv32.time);
   3625 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
   3626 		ntv32.esterror = (netbsd32_long)ntv.esterror;
   3627 		error = copyout((caddr_t)&ntv32, (caddr_t)(u_long)SCARG(uap, ntvp),
   3628 		    sizeof(ntv32));
   3629 	}
   3630 	if (!error) {
   3631 
   3632 		/*
   3633 		 * Status word error decode. If any of these conditions
   3634 		 * occur, an error is returned, instead of the status
   3635 		 * word. Most applications will care only about the fact
   3636 		 * the system clock may not be trusted, not about the
   3637 		 * details.
   3638 		 *
   3639 		 * Hardware or software error
   3640 		 */
   3641 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   3642 
   3643 		/*
   3644 		 * PPS signal lost when either time or frequency
   3645 		 * synchronization requested
   3646 		 */
   3647 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   3648 		    !(time_status & STA_PPSSIGNAL)) ||
   3649 
   3650 		/*
   3651 		 * PPS jitter exceeded when time synchronization
   3652 		 * requested
   3653 		 */
   3654 		    (time_status & STA_PPSTIME &&
   3655 		    time_status & STA_PPSJITTER) ||
   3656 
   3657 		/*
   3658 		 * PPS wander exceeded or calibration error when
   3659 		 * frequency synchronization requested
   3660 		 */
   3661 		    (time_status & STA_PPSFREQ &&
   3662 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
   3663 			*retval = TIME_ERROR;
   3664 		else
   3665 			*retval = (register_t)time_state;
   3666 	}
   3667 	return(error);
   3668 }
   3669 
   3670 int
   3671 netbsd32_ntp_adjtime(p, v, retval)
   3672 	struct proc *p;
   3673 	void *v;
   3674 	register_t *retval;
   3675 {
   3676 	struct netbsd32_ntp_adjtime_args /* {
   3677 		syscallarg(netbsd32_timexp_t) tp;
   3678 	} */ *uap = v;
   3679 	struct netbsd32_timex ntv32;
   3680 	struct timex ntv;
   3681 	int error = 0;
   3682 	int modes;
   3683 	int s;
   3684 	extern long time_freq;		/* frequency offset (scaled ppm) */
   3685 	extern long time_maxerror;
   3686 	extern long time_esterror;
   3687 	extern int time_state;	/* clock state */
   3688 	extern int time_status;	/* clock status bits */
   3689 	extern long time_constant;		/* pll time constant */
   3690 	extern long time_offset;		/* time offset (us) */
   3691 	extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
   3692 	extern long time_precision;	/* clock precision (us) */
   3693 
   3694 	if ((error = copyin((caddr_t)(u_long)SCARG(uap, tp), (caddr_t)&ntv32,
   3695 			sizeof(ntv32))))
   3696 		return (error);
   3697 	netbsd32_to_timex(&ntv32, &ntv);
   3698 
   3699 	/*
   3700 	 * Update selected clock variables - only the superuser can
   3701 	 * change anything. Note that there is no error checking here on
   3702 	 * the assumption the superuser should know what it is doing.
   3703 	 */
   3704 	modes = ntv.modes;
   3705 	if (modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)))
   3706 		return (error);
   3707 
   3708 	s = splclock();
   3709 	if (modes & MOD_FREQUENCY)
   3710 #ifdef PPS_SYNC
   3711 		time_freq = ntv.freq - pps_freq;
   3712 #else /* PPS_SYNC */
   3713 		time_freq = ntv.freq;
   3714 #endif /* PPS_SYNC */
   3715 	if (modes & MOD_MAXERROR)
   3716 		time_maxerror = ntv.maxerror;
   3717 	if (modes & MOD_ESTERROR)
   3718 		time_esterror = ntv.esterror;
   3719 	if (modes & MOD_STATUS) {
   3720 		time_status &= STA_RONLY;
   3721 		time_status |= ntv.status & ~STA_RONLY;
   3722 	}
   3723 	if (modes & MOD_TIMECONST)
   3724 		time_constant = ntv.constant;
   3725 	if (modes & MOD_OFFSET)
   3726 		hardupdate(ntv.offset);
   3727 
   3728 	/*
   3729 	 * Retrieve all clock variables
   3730 	 */
   3731 	if (time_offset < 0)
   3732 		ntv.offset = -(-time_offset >> SHIFT_UPDATE);
   3733 	else
   3734 		ntv.offset = time_offset >> SHIFT_UPDATE;
   3735 #ifdef PPS_SYNC
   3736 	ntv.freq = time_freq + pps_freq;
   3737 #else /* PPS_SYNC */
   3738 	ntv.freq = time_freq;
   3739 #endif /* PPS_SYNC */
   3740 	ntv.maxerror = time_maxerror;
   3741 	ntv.esterror = time_esterror;
   3742 	ntv.status = time_status;
   3743 	ntv.constant = time_constant;
   3744 	ntv.precision = time_precision;
   3745 	ntv.tolerance = time_tolerance;
   3746 #ifdef PPS_SYNC
   3747 	ntv.shift = pps_shift;
   3748 	ntv.ppsfreq = pps_freq;
   3749 	ntv.jitter = pps_jitter >> PPS_AVG;
   3750 	ntv.stabil = pps_stabil;
   3751 	ntv.calcnt = pps_calcnt;
   3752 	ntv.errcnt = pps_errcnt;
   3753 	ntv.jitcnt = pps_jitcnt;
   3754 	ntv.stbcnt = pps_stbcnt;
   3755 #endif /* PPS_SYNC */
   3756 	(void)splx(s);
   3757 
   3758 	netbsd32_from_timeval(&ntv, &ntv32);
   3759 	error = copyout((caddr_t)&ntv32, (caddr_t)SCARG(uap, tp), sizeof(ntv32));
   3760 	if (!error) {
   3761 
   3762 		/*
   3763 		 * Status word error decode. See comments in
   3764 		 * ntp_gettime() routine.
   3765 		 */
   3766 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   3767 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   3768 		    !(time_status & STA_PPSSIGNAL)) ||
   3769 		    (time_status & STA_PPSTIME &&
   3770 		    time_status & STA_PPSJITTER) ||
   3771 		    (time_status & STA_PPSFREQ &&
   3772 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
   3773 			*retval = TIME_ERROR;
   3774 		else
   3775 			*retval = (register_t)time_state;
   3776 	}
   3777 	return error;
   3778 }
   3779 #else
   3780 int
   3781 netbsd32_ntp_gettime(p, v, retval)
   3782 	struct proc *p;
   3783 	void *v;
   3784 	register_t *retval;
   3785 {
   3786 	return(ENOSYS);
   3787 }
   3788 
   3789 int
   3790 netbsd32_ntp_adjtime(p, v, retval)
   3791 	struct proc *p;
   3792 	void *v;
   3793 	register_t *retval;
   3794 {
   3795 	return (ENOSYS);
   3796 }
   3797 #endif
   3798 
   3799 int
   3800 netbsd32_setgid(p, v, retval)
   3801 	struct proc *p;
   3802 	void *v;
   3803 	register_t *retval;
   3804 {
   3805 	struct netbsd32_setgid_args /* {
   3806 		syscallarg(gid_t) gid;
   3807 	} */ *uap = v;
   3808 	struct sys_setgid_args ua;
   3809 
   3810 	NETBSD32TO64_UAP(gid);
   3811 	return (sys_setgid(p, v, retval));
   3812 }
   3813 
   3814 int
   3815 netbsd32_setegid(p, v, retval)
   3816 	struct proc *p;
   3817 	void *v;
   3818 	register_t *retval;
   3819 {
   3820 	struct netbsd32_setegid_args /* {
   3821 		syscallarg(gid_t) egid;
   3822 	} */ *uap = v;
   3823 	struct sys_setegid_args ua;
   3824 
   3825 	NETBSD32TO64_UAP(egid);
   3826 	return (sys_setegid(p, v, retval));
   3827 }
   3828 
   3829 int
   3830 netbsd32_seteuid(p, v, retval)
   3831 	struct proc *p;
   3832 	void *v;
   3833 	register_t *retval;
   3834 {
   3835 	struct netbsd32_seteuid_args /* {
   3836 		syscallarg(gid_t) euid;
   3837 	} */ *uap = v;
   3838 	struct sys_seteuid_args ua;
   3839 
   3840 	NETBSD32TO64_UAP(euid);
   3841 	return (sys_seteuid(p, v, retval));
   3842 }
   3843 
   3844 #ifdef LFS
   3845 int
   3846 netbsd32_sys_lfs_bmapv(p, v, retval)
   3847 	struct proc *p;
   3848 	void *v;
   3849 	register_t *retval;
   3850 {
   3851 #if 0
   3852 	struct netbsd32_lfs_bmapv_args /* {
   3853 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3854 		syscallarg(netbsd32_block_infop_t) blkiov;
   3855 		syscallarg(int) blkcnt;
   3856 	} */ *uap = v;
   3857 	struct sys_lfs_bmapv_args ua;
   3858 
   3859 	NETBSD32TOP_UAP(fdidp, struct fsid);
   3860 	NETBSD32TO64_UAP(blkcnt);
   3861 	/* XXX finish me */
   3862 #else
   3863 
   3864 	return (ENOSYS);	/* XXX */
   3865 #endif
   3866 }
   3867 
   3868 int
   3869 netbsd32_sys_lfs_markv(p, v, retval)
   3870 	struct proc *p;
   3871 	void *v;
   3872 	register_t *retval;
   3873 {
   3874 #if 0
   3875 	struct netbsd32_lfs_markv_args /* {
   3876 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3877 		syscallarg(netbsd32_block_infop_t) blkiov;
   3878 		syscallarg(int) blkcnt;
   3879 	} */ *uap = v;
   3880 #endif
   3881 
   3882 	return (ENOSYS);	/* XXX */
   3883 }
   3884 
   3885 int
   3886 netbsd32_sys_lfs_segclean(p, v, retval)
   3887 	struct proc *p;
   3888 	void *v;
   3889 	register_t *retval;
   3890 {
   3891 #if 0
   3892 	struct netbsd32_lfs_segclean_args /* {
   3893 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3894 		syscallarg(netbsd32_u_long) segment;
   3895 	} */ *uap = v;
   3896 #endif
   3897 
   3898 	return (ENOSYS);	/* XXX */
   3899 }
   3900 
   3901 int
   3902 netbsd32_sys_lfs_segwait(p, v, retval)
   3903 	struct proc *p;
   3904 	void *v;
   3905 	register_t *retval;
   3906 {
   3907 #if 0
   3908 	struct netbsd32_lfs_segwait_args /* {
   3909 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3910 		syscallarg(netbsd32_timevalp_t) tv;
   3911 	} */ *uap = v;
   3912 #endif
   3913 
   3914 	return (ENOSYS);	/* XXX */
   3915 }
   3916 #endif
   3917 
   3918 int
   3919 netbsd32_pathconf(p, v, retval)
   3920 	struct proc *p;
   3921 	void *v;
   3922 	register_t *retval;
   3923 {
   3924 	struct netbsd32_pathconf_args /* {
   3925 		syscallarg(int) fd;
   3926 		syscallarg(int) name;
   3927 	} */ *uap = v;
   3928 	struct sys_pathconf_args ua;
   3929 	long rt;
   3930 	int error;
   3931 
   3932 	NETBSD32TOP_UAP(path, const char);
   3933 	NETBSD32TO64_UAP(name);
   3934 	error = sys_pathconf(p, &ua, (register_t *)&rt);
   3935 	*(netbsd32_long *)retval = (netbsd32_long)rt;
   3936 	return (error);
   3937 }
   3938 
   3939 int
   3940 netbsd32_fpathconf(p, v, retval)
   3941 	struct proc *p;
   3942 	void *v;
   3943 	register_t *retval;
   3944 {
   3945 	struct netbsd32_fpathconf_args /* {
   3946 		syscallarg(int) fd;
   3947 		syscallarg(int) name;
   3948 	} */ *uap = v;
   3949 	struct sys_fpathconf_args ua;
   3950 	long rt;
   3951 	int error;
   3952 
   3953 	NETBSD32TO64_UAP(fd);
   3954 	NETBSD32TO64_UAP(name);
   3955 	error = sys_fpathconf(p, &ua, (register_t *)&rt);
   3956 	*(netbsd32_long *)retval = (netbsd32_long)rt;
   3957 	return (error);
   3958 }
   3959 
   3960 int
   3961 netbsd32_getrlimit(p, v, retval)
   3962 	struct proc *p;
   3963 	void *v;
   3964 	register_t *retval;
   3965 {
   3966 	struct netbsd32_getrlimit_args /* {
   3967 		syscallarg(int) which;
   3968 		syscallarg(netbsd32_rlimitp_t) rlp;
   3969 	} */ *uap = v;
   3970 	int which = SCARG(uap, which);
   3971 
   3972 	if ((u_int)which >= RLIM_NLIMITS)
   3973 		return (EINVAL);
   3974 	return (copyout(&p->p_rlimit[which], (caddr_t)(u_long)SCARG(uap, rlp),
   3975 	    sizeof(struct rlimit)));
   3976 }
   3977 
   3978 int
   3979 netbsd32_setrlimit(p, v, retval)
   3980 	struct proc *p;
   3981 	void *v;
   3982 	register_t *retval;
   3983 {
   3984 	struct netbsd32_setrlimit_args /* {
   3985 		syscallarg(int) which;
   3986 		syscallarg(const netbsd32_rlimitp_t) rlp;
   3987 	} */ *uap = v;
   3988 		int which = SCARG(uap, which);
   3989 	struct rlimit alim;
   3990 	int error;
   3991 
   3992 	error = copyin((caddr_t)(u_long)SCARG(uap, rlp), &alim, sizeof(struct rlimit));
   3993 	if (error)
   3994 		return (error);
   3995 	return (dosetrlimit(p, p->p_cred, which, &alim));
   3996 }
   3997 
   3998 int
   3999 netbsd32_mmap(p, v, retval)
   4000 	struct proc *p;
   4001 	void *v;
   4002 	register_t *retval;
   4003 {
   4004 	struct netbsd32_mmap_args /* {
   4005 		syscallarg(netbsd32_voidp) addr;
   4006 		syscallarg(netbsd32_size_t) len;
   4007 		syscallarg(int) prot;
   4008 		syscallarg(int) flags;
   4009 		syscallarg(int) fd;
   4010 		syscallarg(netbsd32_long) pad;
   4011 		syscallarg(off_t) pos;
   4012 	} */ *uap = v;
   4013 	struct sys_mmap_args ua;
   4014 	void *rt;
   4015 	int error;
   4016 
   4017 	NETBSD32TOP_UAP(addr, void);
   4018 	NETBSD32TOX_UAP(len, size_t);
   4019 	NETBSD32TO64_UAP(prot);
   4020 	NETBSD32TO64_UAP(flags);
   4021 	NETBSD32TO64_UAP(fd);
   4022 	NETBSD32TOX_UAP(pad, long);
   4023 	NETBSD32TOX_UAP(pos, off_t);
   4024 	error = sys_mmap(p, &ua, (register_t *)&rt);
   4025 	if ((long)rt > (long)UINT_MAX)
   4026 		printf("netbsd32_mmap: retval out of range: 0x%qx",
   4027 		    rt);
   4028 	*retval = (netbsd32_voidp)(u_long)rt;
   4029 	return (error);
   4030 }
   4031 
   4032 int
   4033 netbsd32_lseek(p, v, retval)
   4034 	struct proc *p;
   4035 	void *v;
   4036 	register_t *retval;
   4037 {
   4038 	struct netbsd32_lseek_args /* {
   4039 		syscallarg(int) fd;
   4040 		syscallarg(int) pad;
   4041 		syscallarg(off_t) offset;
   4042 		syscallarg(int) whence;
   4043 	} */ *uap = v;
   4044 	struct sys_lseek_args ua;
   4045 
   4046 	NETBSD32TO64_UAP(fd);
   4047 	NETBSD32TO64_UAP(pad);
   4048 	NETBSD32TO64_UAP(offset);
   4049 	NETBSD32TO64_UAP(whence);
   4050 	return (sys_lseek(p, &ua, retval));
   4051 }
   4052 
   4053 int
   4054 netbsd32_truncate(p, v, retval)
   4055 	struct proc *p;
   4056 	void *v;
   4057 	register_t *retval;
   4058 {
   4059 	struct netbsd32_truncate_args /* {
   4060 		syscallarg(const netbsd32_charp) path;
   4061 		syscallarg(int) pad;
   4062 		syscallarg(off_t) length;
   4063 	} */ *uap = v;
   4064 	struct sys_truncate_args ua;
   4065 
   4066 	NETBSD32TOP_UAP(path, const char);
   4067 	NETBSD32TO64_UAP(pad);
   4068 	NETBSD32TO64_UAP(length);
   4069 	return (sys_truncate(p, &ua, retval));
   4070 }
   4071 
   4072 int
   4073 netbsd32_ftruncate(p, v, retval)
   4074 	struct proc *p;
   4075 	void *v;
   4076 	register_t *retval;
   4077 {
   4078 	struct netbsd32_ftruncate_args /* {
   4079 		syscallarg(int) fd;
   4080 		syscallarg(int) pad;
   4081 		syscallarg(off_t) length;
   4082 	} */ *uap = v;
   4083 	struct sys_ftruncate_args ua;
   4084 
   4085 	NETBSD32TO64_UAP(fd);
   4086 	NETBSD32TO64_UAP(pad);
   4087 	NETBSD32TO64_UAP(length);
   4088 	return (sys_ftruncate(p, &ua, retval));
   4089 }
   4090 
   4091 int
   4092 netbsd32___sysctl(p, v, retval)
   4093 	struct proc *p;
   4094 	void *v;
   4095 	register_t *retval;
   4096 {
   4097 	struct netbsd32___sysctl_args /* {
   4098 		syscallarg(netbsd32_intp) name;
   4099 		syscallarg(u_int) namelen;
   4100 		syscallarg(netbsd32_voidp) old;
   4101 		syscallarg(netbsd32_size_tp) oldlenp;
   4102 		syscallarg(netbsd32_voidp) new;
   4103 		syscallarg(netbsd32_size_t) newlen;
   4104 	} */ *uap = v;
   4105 	int error;
   4106 	netbsd32_size_t savelen = 0;
   4107 	size_t oldlen = 0;
   4108 	sysctlfn *fn;
   4109 	int name[CTL_MAXNAME];
   4110 
   4111 /*
   4112  * Some of these sysctl functions do their own copyin/copyout.
   4113  * We need to disable or emulate the ones that need their
   4114  * arguments converted.
   4115  */
   4116 
   4117 	if (SCARG(uap, new) != NULL &&
   4118 	    (error = suser(p->p_ucred, &p->p_acflag)))
   4119 		return (error);
   4120 	/*
   4121 	 * all top-level sysctl names are non-terminal
   4122 	 */
   4123 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
   4124 		return (EINVAL);
   4125 	error = copyin((caddr_t)(u_long)SCARG(uap, name), &name,
   4126 		       SCARG(uap, namelen) * sizeof(int));
   4127 	if (error)
   4128 		return (error);
   4129 
   4130 	switch (name[0]) {
   4131 	case CTL_KERN:
   4132 		fn = kern_sysctl;
   4133 		break;
   4134 	case CTL_HW:
   4135 		fn = hw_sysctl;
   4136 		break;
   4137 	case CTL_VM:
   4138 		fn = uvm_sysctl;
   4139 		break;
   4140 	case CTL_NET:
   4141 		fn = net_sysctl;
   4142 		break;
   4143 	case CTL_VFS:
   4144 		fn = vfs_sysctl;
   4145 		break;
   4146 	case CTL_MACHDEP:
   4147 		fn = cpu_sysctl;
   4148 		break;
   4149 #ifdef DEBUG
   4150 	case CTL_DEBUG:
   4151 		fn = debug_sysctl;
   4152 		break;
   4153 #endif
   4154 #ifdef DDB
   4155 	case CTL_DDB:
   4156 		fn = ddb_sysctl;
   4157 		break;
   4158 #endif
   4159 	case CTL_PROC:
   4160 		fn = proc_sysctl;
   4161 		break;
   4162 	default:
   4163 		return (EOPNOTSUPP);
   4164 	}
   4165 
   4166 	/*
   4167 	 * XXX Hey, we wire `old', but what about `new'?
   4168 	 */
   4169 
   4170 	if (SCARG(uap, oldlenp) &&
   4171 	    (error = copyin((caddr_t)(u_long)SCARG(uap, oldlenp), &savelen,
   4172 	     sizeof(savelen))))
   4173 		return (error);
   4174 	if (SCARG(uap, old) != NULL) {
   4175 		error = lockmgr(&sysctl_memlock, LK_EXCLUSIVE, NULL);
   4176 		if (error)
   4177 			return (error);
   4178 		if (uvm_vslock(p, (void *)(u_long)SCARG(uap, old), savelen,
   4179 		    VM_PROT_READ|VM_PROT_WRITE) != KERN_SUCCESS) {
   4180 			(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
   4181 			return (EFAULT);
   4182 		}
   4183 		oldlen = savelen;
   4184 	}
   4185 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1,
   4186 		      (void *)(u_long)SCARG(uap, old), &oldlen,
   4187 		      (void *)(u_long)SCARG(uap, new), SCARG(uap, newlen), p);
   4188 	if (SCARG(uap, old) != NULL) {
   4189 		uvm_vsunlock(p, (void *)(u_long)SCARG(uap, old), savelen);
   4190 		(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
   4191 	}
   4192 	savelen = oldlen;
   4193 	if (error)
   4194 		return (error);
   4195 	if (SCARG(uap, oldlenp))
   4196 		error = copyout(&savelen,
   4197 		    (caddr_t)(u_long)SCARG(uap, oldlenp), sizeof(savelen));
   4198 	return (error);
   4199 }
   4200 
   4201 int
   4202 netbsd32_mlock(p, v, retval)
   4203 	struct proc *p;
   4204 	void *v;
   4205 	register_t *retval;
   4206 {
   4207 	struct netbsd32_mlock_args /* {
   4208 		syscallarg(const netbsd32_voidp) addr;
   4209 		syscallarg(netbsd32_size_t) len;
   4210 	} */ *uap = v;
   4211 	struct sys_mlock_args ua;
   4212 
   4213 	NETBSD32TOP_UAP(addr, const void);
   4214 	NETBSD32TO64_UAP(len);
   4215 	return (sys_mlock(p, &ua, retval));
   4216 }
   4217 
   4218 int
   4219 netbsd32_munlock(p, v, retval)
   4220 	struct proc *p;
   4221 	void *v;
   4222 	register_t *retval;
   4223 {
   4224 	struct netbsd32_munlock_args /* {
   4225 		syscallarg(const netbsd32_voidp) addr;
   4226 		syscallarg(netbsd32_size_t) len;
   4227 	} */ *uap = v;
   4228 	struct sys_munlock_args ua;
   4229 
   4230 	NETBSD32TOP_UAP(addr, const void);
   4231 	NETBSD32TO64_UAP(len);
   4232 	return (sys_munlock(p, &ua, retval));
   4233 }
   4234 
   4235 int
   4236 netbsd32_undelete(p, v, retval)
   4237 	struct proc *p;
   4238 	void *v;
   4239 	register_t *retval;
   4240 {
   4241 	struct netbsd32_undelete_args /* {
   4242 		syscallarg(const netbsd32_charp) path;
   4243 	} */ *uap = v;
   4244 	struct sys_undelete_args ua;
   4245 
   4246 	NETBSD32TOP_UAP(path, const char);
   4247 	return (sys_undelete(p, &ua, retval));
   4248 }
   4249 
   4250 int
   4251 netbsd32_futimes(p, v, retval)
   4252 	struct proc *p;
   4253 	void *v;
   4254 	register_t *retval;
   4255 {
   4256 	struct netbsd32_futimes_args /* {
   4257 		syscallarg(int) fd;
   4258 		syscallarg(const netbsd32_timevalp_t) tptr;
   4259 	} */ *uap = v;
   4260 	int error;
   4261 	struct file *fp;
   4262 
   4263 	/* getvnode() will use the descriptor for us */
   4264 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   4265 		return (error);
   4266 
   4267 	error = change_utimes32((struct vnode *)fp->f_data,
   4268 				(struct timeval *)(u_long)SCARG(uap, tptr), p);
   4269 	FILE_UNUSE(fp, p);
   4270 	return (error);
   4271 }
   4272 
   4273 int
   4274 netbsd32_getpgid(p, v, retval)
   4275 	struct proc *p;
   4276 	void *v;
   4277 	register_t *retval;
   4278 {
   4279 	struct netbsd32_getpgid_args /* {
   4280 		syscallarg(pid_t) pid;
   4281 	} */ *uap = v;
   4282 	struct sys_getpgid_args ua;
   4283 
   4284 	NETBSD32TO64_UAP(pid);
   4285 	return (sys_getpgid(p, &ua, retval));
   4286 }
   4287 
   4288 int
   4289 netbsd32_reboot(p, v, retval)
   4290 	struct proc *p;
   4291 	void *v;
   4292 	register_t *retval;
   4293 {
   4294 	struct netbsd32_reboot_args /* {
   4295 		syscallarg(int) opt;
   4296 		syscallarg(netbsd32_charp) bootstr;
   4297 	} */ *uap = v;
   4298 	struct sys_reboot_args ua;
   4299 
   4300 	NETBSD32TO64_UAP(opt);
   4301 	NETBSD32TOP_UAP(bootstr, char);
   4302 	return (sys_reboot(p, &ua, retval));
   4303 }
   4304 
   4305 int
   4306 netbsd32_poll(p, v, retval)
   4307 	struct proc *p;
   4308 	void *v;
   4309 	register_t *retval;
   4310 {
   4311 	struct netbsd32_poll_args /* {
   4312 		syscallarg(netbsd32_pollfdp_t) fds;
   4313 		syscallarg(u_int) nfds;
   4314 		syscallarg(int) timeout;
   4315 	} */ *uap = v;
   4316 	struct sys_poll_args ua;
   4317 
   4318 	NETBSD32TOP_UAP(fds, struct pollfd);
   4319 	NETBSD32TO64_UAP(nfds);
   4320 	NETBSD32TO64_UAP(timeout);
   4321 	return (sys_poll(p, &ua, retval));
   4322 }
   4323 
   4324 #if defined(SYSVSEM)
   4325 /*
   4326  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   4327  *
   4328  * This is BSD.  We won't support System V IPC.
   4329  * Too much work.
   4330  *
   4331  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   4332  */
   4333 int
   4334 netbsd32___semctl14(p, v, retval)
   4335 	struct proc *p;
   4336 	void *v;
   4337 	register_t *retval;
   4338 {
   4339 #if 0
   4340 	struct netbsd32___semctl_args /* {
   4341 		syscallarg(int) semid;
   4342 		syscallarg(int) semnum;
   4343 		syscallarg(int) cmd;
   4344 		syscallarg(netbsd32_semunu_t *) arg;
   4345 	} */ *uap = v;
   4346 	union netbsd32_semun sem32;
   4347 	int semid = SCARG(uap, semid);
   4348 	int semnum = SCARG(uap, semnum);
   4349 	int cmd = SCARG(uap, cmd);
   4350 	union netbsd32_semun *arg = (void*)(u_long)SCARG(uap, arg);
   4351 	union netbsd32_semun real_arg;
   4352 	struct ucred *cred = p->p_ucred;
   4353 	int i, rval, eval;
   4354 	struct netbsd32_semid_ds sbuf;
   4355 	struct semid_ds *semaptr;
   4356 
   4357 	semlock(p);
   4358 
   4359 	semid = IPCID_TO_IX(semid);
   4360 	if (semid < 0 || semid >= seminfo.semmsl)
   4361 		return(EINVAL);
   4362 
   4363 	semaptr = &sema[semid];
   4364 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
   4365 	    semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid)))
   4366 		return(EINVAL);
   4367 
   4368 	eval = 0;
   4369 	rval = 0;
   4370 
   4371 	switch (cmd) {
   4372 	case IPC_RMID:
   4373 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
   4374 			return(eval);
   4375 		semaptr->sem_perm.cuid = cred->cr_uid;
   4376 		semaptr->sem_perm.uid = cred->cr_uid;
   4377 		semtot -= semaptr->sem_nsems;
   4378 		for (i = semaptr->_sem_base - sem; i < semtot; i++)
   4379 			sem[i] = sem[i + semaptr->sem_nsems];
   4380 		for (i = 0; i < seminfo.semmni; i++) {
   4381 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
   4382 			    sema[i]._sem_base > semaptr->_sem_base)
   4383 				sema[i]._sem_base -= semaptr->sem_nsems;
   4384 		}
   4385 		semaptr->sem_perm.mode = 0;
   4386 		semundo_clear(semid, -1);
   4387 		wakeup((caddr_t)semaptr);
   4388 		break;
   4389 
   4390 	case IPC_SET:
   4391 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
   4392 			return(eval);
   4393 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4394 			return(eval);
   4395 		if ((eval = copyin((caddr_t)(u_long)real_arg.buf, (caddr_t)&sbuf,
   4396 		    sizeof(sbuf))) != 0)
   4397 			return(eval);
   4398 		semaptr->sem_perm.uid = sbuf.sem_perm.uid;
   4399 		semaptr->sem_perm.gid = sbuf.sem_perm.gid;
   4400 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
   4401 		    (sbuf.sem_perm.mode & 0777);
   4402 		semaptr->sem_ctime = time.tv_sec;
   4403 		break;
   4404 
   4405 	case IPC_STAT:
   4406 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4407 			return(eval);
   4408 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4409 			return(eval);
   4410 		eval = copyout((caddr_t)semaptr, (caddr_t)(u_long)real_arg.buf,
   4411 		    sizeof(struct semid_ds));
   4412 		break;
   4413 
   4414 	case GETNCNT:
   4415 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4416 			return(eval);
   4417 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4418 			return(EINVAL);
   4419 		rval = semaptr->_sem_base[semnum].semncnt;
   4420 		break;
   4421 
   4422 	case GETPID:
   4423 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4424 			return(eval);
   4425 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4426 			return(EINVAL);
   4427 		rval = semaptr->_sem_base[semnum].sempid;
   4428 		break;
   4429 
   4430 	case GETVAL:
   4431 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4432 			return(eval);
   4433 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4434 			return(EINVAL);
   4435 		rval = semaptr->_sem_base[semnum].semval;
   4436 		break;
   4437 
   4438 	case GETALL:
   4439 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4440 			return(eval);
   4441 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4442 			return(eval);
   4443 		for (i = 0; i < semaptr->sem_nsems; i++) {
   4444 			eval = copyout((caddr_t)&semaptr->_sem_base[i].semval,
   4445 			    &real_arg.array[i], sizeof(real_arg.array[0]));
   4446 			if (eval != 0)
   4447 				break;
   4448 		}
   4449 		break;
   4450 
   4451 	case GETZCNT:
   4452 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
   4453 			return(eval);
   4454 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4455 			return(EINVAL);
   4456 		rval = semaptr->_sem_base[semnum].semzcnt;
   4457 		break;
   4458 
   4459 	case SETVAL:
   4460 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
   4461 			return(eval);
   4462 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
   4463 			return(EINVAL);
   4464 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4465 			return(eval);
   4466 		semaptr->_sem_base[semnum].semval = real_arg.val;
   4467 		semundo_clear(semid, semnum);
   4468 		wakeup((caddr_t)semaptr);
   4469 		break;
   4470 
   4471 	case SETALL:
   4472 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
   4473 			return(eval);
   4474 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
   4475 			return(eval);
   4476 		for (i = 0; i < semaptr->sem_nsems; i++) {
   4477 			eval = copyin(&real_arg.array[i],
   4478 			    (caddr_t)&semaptr->_sem_base[i].semval,
   4479 			    sizeof(real_arg.array[0]));
   4480 			if (eval != 0)
   4481 				break;
   4482 		}
   4483 		semundo_clear(semid, -1);
   4484 		wakeup((caddr_t)semaptr);
   4485 		break;
   4486 
   4487 	default:
   4488 		return(EINVAL);
   4489 	}
   4490 
   4491 	if (eval == 0)
   4492 		*retval = rval;
   4493 	return(eval);
   4494 #else
   4495 	return (ENOSYS);
   4496 #endif
   4497 }
   4498 
   4499 int
   4500 netbsd32_semget(p, v, retval)
   4501 	struct proc *p;
   4502 	void *v;
   4503 	register_t *retval;
   4504 {
   4505 	struct netbsd32_semget_args /* {
   4506 		syscallarg(netbsd32_key_t) key;
   4507 		syscallarg(int) nsems;
   4508 		syscallarg(int) semflg;
   4509 	} */ *uap = v;
   4510 	struct sys_semget_args ua;
   4511 
   4512 	NETBSD32TOX_UAP(key, key_t);
   4513 	NETBSD32TO64_UAP(nsems);
   4514 	NETBSD32TO64_UAP(semflg);
   4515 	return (sys_semget(p, &ua, retval));
   4516 }
   4517 
   4518 int
   4519 netbsd32_semop(p, v, retval)
   4520 	struct proc *p;
   4521 	void *v;
   4522 	register_t *retval;
   4523 {
   4524 	struct netbsd32_semop_args /* {
   4525 		syscallarg(int) semid;
   4526 		syscallarg(netbsd32_sembufp_t) sops;
   4527 		syscallarg(netbsd32_size_t) nsops;
   4528 	} */ *uap = v;
   4529 	struct sys_semop_args ua;
   4530 
   4531 	NETBSD32TO64_UAP(semid);
   4532 	NETBSD32TOP_UAP(sops, struct sembuf);
   4533 	NETBSD32TOX_UAP(nsops, size_t);
   4534 	return (sys_semop(p, &ua, retval));
   4535 }
   4536 
   4537 int
   4538 netbsd32_semconfig(p, v, retval)
   4539 	struct proc *p;
   4540 	void *v;
   4541 	register_t *retval;
   4542 {
   4543 	struct netbsd32_semconfig_args /* {
   4544 		syscallarg(int) flag;
   4545 	} */ *uap = v;
   4546 	struct sys_semconfig_args ua;
   4547 
   4548 	NETBSD32TO64_UAP(flag);
   4549 	return (sys_semconfig(p, &ua, retval));
   4550 }
   4551 #endif /* SYSVSEM */
   4552 
   4553 #if defined(SYSVMSG)
   4554 
   4555 int
   4556 netbsd32___msgctl13(p, v, retval)
   4557 	struct proc *p;
   4558 	void *v;
   4559 	register_t *retval;
   4560 {
   4561 #if 0
   4562 	struct netbsd32_msgctl_args /* {
   4563 		syscallarg(int) msqid;
   4564 		syscallarg(int) cmd;
   4565 		syscallarg(netbsd32_msqid_dsp_t) buf;
   4566 	} */ *uap = v;
   4567 	struct sys_msgctl_args ua;
   4568 	struct msqid_ds ds;
   4569 	struct netbsd32_msqid_ds *ds32p;
   4570 	int error;
   4571 
   4572 	NETBSD32TO64_UAP(msqid);
   4573 	NETBSD32TO64_UAP(cmd);
   4574 	ds32p = (struct netbsd32_msqid_ds *)(u_long)SCARG(uap, buf);
   4575 	if (ds32p) {
   4576 		SCARG(&ua, buf) = NULL;
   4577 		netbsd32_to_msqid_ds(ds32p, &ds);
   4578 	} else
   4579 		SCARG(&ua, buf) = NULL;
   4580 	error = sys_msgctl(p, &ua, retval);
   4581 	if (error)
   4582 		return (error);
   4583 
   4584 	if (ds32p)
   4585 		netbsd32_from_msqid_ds(&ds, ds32p);
   4586 	return (0);
   4587 #else
   4588 	return (ENOSYS);
   4589 #endif
   4590 }
   4591 
   4592 int
   4593 netbsd32_msgget(p, v, retval)
   4594 	struct proc *p;
   4595 	void *v;
   4596 	register_t *retval;
   4597 {
   4598 #if 0
   4599 	struct netbsd32_msgget_args /* {
   4600 		syscallarg(netbsd32_key_t) key;
   4601 		syscallarg(int) msgflg;
   4602 	} */ *uap = v;
   4603 	struct sys_msgget_args ua;
   4604 
   4605 	NETBSD32TOX_UAP(key, key_t);
   4606 	NETBSD32TO64_UAP(msgflg);
   4607 	return (sys_msgget(p, &ua, retval));
   4608 #else
   4609 	return (ENOSYS);
   4610 #endif
   4611 }
   4612 
   4613 int
   4614 netbsd32_msgsnd(p, v, retval)
   4615 	struct proc *p;
   4616 	void *v;
   4617 	register_t *retval;
   4618 {
   4619 #if 0
   4620 	struct netbsd32_msgsnd_args /* {
   4621 		syscallarg(int) msqid;
   4622 		syscallarg(const netbsd32_voidp) msgp;
   4623 		syscallarg(netbsd32_size_t) msgsz;
   4624 		syscallarg(int) msgflg;
   4625 	} */ *uap = v;
   4626 	struct sys_msgsnd_args ua;
   4627 
   4628 	NETBSD32TO64_UAP(msqid);
   4629 	NETBSD32TOP_UAP(msgp, void);
   4630 	NETBSD32TOX_UAP(msgsz, size_t);
   4631 	NETBSD32TO64_UAP(msgflg);
   4632 	return (sys_msgsnd(p, &ua, retval));
   4633 #else
   4634 	return (ENOSYS);
   4635 #endif
   4636 }
   4637 
   4638 int
   4639 netbsd32_msgrcv(p, v, retval)
   4640 	struct proc *p;
   4641 	void *v;
   4642 	register_t *retval;
   4643 {
   4644 #if 0
   4645 	struct netbsd32_msgrcv_args /* {
   4646 		syscallarg(int) msqid;
   4647 		syscallarg(netbsd32_voidp) msgp;
   4648 		syscallarg(netbsd32_size_t) msgsz;
   4649 		syscallarg(netbsd32_long) msgtyp;
   4650 		syscallarg(int) msgflg;
   4651 	} */ *uap = v;
   4652 	struct sys_msgrcv_args ua;
   4653 	ssize_t rt;
   4654 	int error;
   4655 
   4656 	NETBSD32TO64_UAP(msqid);
   4657 	NETBSD32TOP_UAP(msgp, void);
   4658 	NETBSD32TOX_UAP(msgsz, size_t);
   4659 	NETBSD32TOX_UAP(msgtyp, long);
   4660 	NETBSD32TO64_UAP(msgflg);
   4661 	error = sys_msgrcv(p, &ua, (register_t *)&rt);
   4662 	*(netbsd32_ssize_t *)retval = rt;
   4663 	return (error);
   4664 #else
   4665 	return (ENOSYS);
   4666 #endif
   4667 }
   4668 #endif /* SYSVMSG */
   4669 
   4670 #if defined(SYSVSHM)
   4671 
   4672 int
   4673 netbsd32_shmat(p, v, retval)
   4674 	struct proc *p;
   4675 	void *v;
   4676 	register_t *retval;
   4677 {
   4678 #if 0
   4679 	struct netbsd32_shmat_args /* {
   4680 		syscallarg(int) shmid;
   4681 		syscallarg(const netbsd32_voidp) shmaddr;
   4682 		syscallarg(int) shmflg;
   4683 	} */ *uap = v;
   4684 	struct sys_shmat_args ua;
   4685 	void *rt;
   4686 	int error;
   4687 
   4688 	NETBSD32TO64_UAP(shmid);
   4689 	NETBSD32TOP_UAP(shmaddr, void);
   4690 	NETBSD32TO64_UAP(shmflg);
   4691 	error = sys_shmat(p, &ua, (register_t *)&rt);
   4692 	*retval = (netbsd32_voidp)(u_long)rt;
   4693 	return (error);
   4694 #else
   4695 	return (ENOSYS);
   4696 #endif
   4697 }
   4698 
   4699 int
   4700 netbsd32___shmctl13(p, v, retval)
   4701 	struct proc *p;
   4702 	void *v;
   4703 	register_t *retval;
   4704 {
   4705 #if 0
   4706 	struct netbsd32_shmctl_args /* {
   4707 		syscallarg(int) shmid;
   4708 		syscallarg(int) cmd;
   4709 		syscallarg(netbsd32_shmid_dsp_t) buf;
   4710 	} */ *uap = v;
   4711 	struct sys_shmctl_args ua;
   4712 	struct shmid_ds ds;
   4713 	struct netbsd32_shmid_ds *ds32p;
   4714 	int error;
   4715 
   4716 	NETBSD32TO64_UAP(shmid);
   4717 	NETBSD32TO64_UAP(cmd);
   4718 	ds32p = (struct netbsd32_shmid_ds *)(u_long)SCARG(uap, buf);
   4719 	if (ds32p) {
   4720 		SCARG(&ua, buf) = NULL;
   4721 		netbsd32_to_shmid_ds(ds32p, &ds);
   4722 	} else
   4723 		SCARG(&ua, buf) = NULL;
   4724 	error = sys_shmctl(p, &ua, retval);
   4725 	if (error)
   4726 		return (error);
   4727 
   4728 	if (ds32p)
   4729 		netbsd32_from_shmid_ds(&ds, ds32p);
   4730 	return (0);
   4731 #else
   4732 	return (ENOSYS);
   4733 #endif
   4734 }
   4735 
   4736 int
   4737 netbsd32_shmdt(p, v, retval)
   4738 	struct proc *p;
   4739 	void *v;
   4740 	register_t *retval;
   4741 {
   4742 #if 0
   4743 	struct netbsd32_shmdt_args /* {
   4744 		syscallarg(const netbsd32_voidp) shmaddr;
   4745 	} */ *uap = v;
   4746 	struct sys_shmdt_args ua;
   4747 
   4748 	NETBSD32TOP_UAP(shmaddr, const char);
   4749 	return (sys_shmdt(p, &ua, retval));
   4750 #else
   4751 	return (ENOSYS);
   4752 #endif
   4753 }
   4754 
   4755 int
   4756 netbsd32_shmget(p, v, retval)
   4757 	struct proc *p;
   4758 	void *v;
   4759 	register_t *retval;
   4760 {
   4761 #if 0
   4762 	struct netbsd32_shmget_args /* {
   4763 		syscallarg(netbsd32_key_t) key;
   4764 		syscallarg(netbsd32_size_t) size;
   4765 		syscallarg(int) shmflg;
   4766 	} */ *uap = v;
   4767 	struct sys_shmget_args ua;
   4768 
   4769 	NETBSD32TOX_UAP(key, key_t)
   4770 	NETBSD32TOX_UAP(size, size_t)
   4771 	NETBSD32TO64_UAP(shmflg);
   4772 	return (sys_shmget(p, &ua, retval));
   4773 #else
   4774 	return (ENOSYS);
   4775 #endif
   4776 }
   4777 #endif /* SYSVSHM */
   4778 
   4779 int
   4780 netbsd32_clock_gettime(p, v, retval)
   4781 	struct proc *p;
   4782 	void *v;
   4783 	register_t *retval;
   4784 {
   4785 	struct netbsd32_clock_gettime_args /* {
   4786 		syscallarg(netbsd32_clockid_t) clock_id;
   4787 		syscallarg(netbsd32_timespecp_t) tp;
   4788 	} */ *uap = v;
   4789 	clockid_t clock_id;
   4790 	struct timeval atv;
   4791 	struct timespec ats;
   4792 	struct netbsd32_timespec ts32;
   4793 
   4794 	clock_id = SCARG(uap, clock_id);
   4795 	if (clock_id != CLOCK_REALTIME)
   4796 		return (EINVAL);
   4797 
   4798 	microtime(&atv);
   4799 	TIMEVAL_TO_TIMESPEC(&atv,&ats);
   4800 	netbsd32_from_timespec(&ats, &ts32);
   4801 
   4802 	return copyout(&ts32, (caddr_t)(u_long)SCARG(uap, tp), sizeof(ts32));
   4803 }
   4804 
   4805 int
   4806 netbsd32_clock_settime(p, v, retval)
   4807 	struct proc *p;
   4808 	void *v;
   4809 	register_t *retval;
   4810 {
   4811 	struct netbsd32_clock_settime_args /* {
   4812 		syscallarg(netbsd32_clockid_t) clock_id;
   4813 		syscallarg(const netbsd32_timespecp_t) tp;
   4814 	} */ *uap = v;
   4815 	struct netbsd32_timespec ts32;
   4816 	clockid_t clock_id;
   4817 	struct timeval atv;
   4818 	struct timespec ats;
   4819 	int error;
   4820 
   4821 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   4822 		return (error);
   4823 
   4824 	clock_id = SCARG(uap, clock_id);
   4825 	if (clock_id != CLOCK_REALTIME)
   4826 		return (EINVAL);
   4827 
   4828 	if ((error = copyin((caddr_t)(u_long)SCARG(uap, tp), &ts32, sizeof(ts32))) != 0)
   4829 		return (error);
   4830 
   4831 	netbsd32_to_timespec(&ts32, &ats);
   4832 	TIMESPEC_TO_TIMEVAL(&atv,&ats);
   4833 	if ((error = settime(&atv)))
   4834 		return (error);
   4835 
   4836 	return 0;
   4837 }
   4838 
   4839 int
   4840 netbsd32_clock_getres(p, v, retval)
   4841 	struct proc *p;
   4842 	void *v;
   4843 	register_t *retval;
   4844 {
   4845 	struct netbsd32_clock_getres_args /* {
   4846 		syscallarg(netbsd32_clockid_t) clock_id;
   4847 		syscallarg(netbsd32_timespecp_t) tp;
   4848 	} */ *uap = v;
   4849 	struct netbsd32_timespec ts32;
   4850 	clockid_t clock_id;
   4851 	struct timespec ts;
   4852 	int error = 0;
   4853 
   4854 	clock_id = SCARG(uap, clock_id);
   4855 	if (clock_id != CLOCK_REALTIME)
   4856 		return (EINVAL);
   4857 
   4858 	if (SCARG(uap, tp)) {
   4859 		ts.tv_sec = 0;
   4860 		ts.tv_nsec = 1000000000 / hz;
   4861 
   4862 		netbsd32_from_timespec(&ts, &ts32);
   4863 		error = copyout(&ts, (caddr_t)(u_long)SCARG(uap, tp), sizeof(ts));
   4864 	}
   4865 
   4866 	return error;
   4867 }
   4868 
   4869 int
   4870 netbsd32_nanosleep(p, v, retval)
   4871 	struct proc *p;
   4872 	void *v;
   4873 	register_t *retval;
   4874 {
   4875 	struct netbsd32_nanosleep_args /* {
   4876 		syscallarg(const netbsd32_timespecp_t) rqtp;
   4877 		syscallarg(netbsd32_timespecp_t) rmtp;
   4878 	} */ *uap = v;
   4879 	static int nanowait;
   4880 	struct netbsd32_timespec ts32;
   4881 	struct timespec rqt;
   4882 	struct timespec rmt;
   4883 	struct timeval atv, utv;
   4884 	int error, s, timo;
   4885 
   4886 	error = copyin((caddr_t)(u_long)SCARG(uap, rqtp), (caddr_t)&ts32,
   4887 		       sizeof(ts32));
   4888 	if (error)
   4889 		return (error);
   4890 
   4891 	netbsd32_to_timespec(&ts32, &rqt);
   4892 	TIMESPEC_TO_TIMEVAL(&atv,&rqt)
   4893 	if (itimerfix(&atv))
   4894 		return (EINVAL);
   4895 
   4896 	s = splclock();
   4897 	timeradd(&atv,&time,&atv);
   4898 	timo = hzto(&atv);
   4899 	/*
   4900 	 * Avoid inadvertantly sleeping forever
   4901 	 */
   4902 	if (timo == 0)
   4903 		timo = 1;
   4904 	splx(s);
   4905 
   4906 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
   4907 	if (error == ERESTART)
   4908 		error = EINTR;
   4909 	if (error == EWOULDBLOCK)
   4910 		error = 0;
   4911 
   4912 	if (SCARG(uap, rmtp)) {
   4913 		int error;
   4914 
   4915 		s = splclock();
   4916 		utv = time;
   4917 		splx(s);
   4918 
   4919 		timersub(&atv, &utv, &utv);
   4920 		if (utv.tv_sec < 0)
   4921 			timerclear(&utv);
   4922 
   4923 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
   4924 		netbsd32_from_timespec(&rmt, &ts32);
   4925 		error = copyout((caddr_t)&ts32, (caddr_t)(u_long)SCARG(uap,rmtp),
   4926 			sizeof(ts32));
   4927 		if (error)
   4928 			return (error);
   4929 	}
   4930 
   4931 	return error;
   4932 }
   4933 
   4934 int
   4935 netbsd32_fdatasync(p, v, retval)
   4936 	struct proc *p;
   4937 	void *v;
   4938 	register_t *retval;
   4939 {
   4940 	struct netbsd32_fdatasync_args /* {
   4941 		syscallarg(int) fd;
   4942 	} */ *uap = v;
   4943 	struct sys_fdatasync_args ua;
   4944 
   4945 	NETBSD32TO64_UAP(fd);
   4946 
   4947 	return (sys_fdatasync(p, &ua, retval));
   4948 }
   4949 
   4950 int
   4951 netbsd32___posix_rename(p, v, retval)
   4952 	struct proc *p;
   4953 	void *v;
   4954 	register_t *retval;
   4955 {
   4956 	struct netbsd32___posix_rename_args /* {
   4957 		syscallarg(const netbsd32_charp) from;
   4958 		syscallarg(const netbsd32_charp) to;
   4959 	} */ *uap = v;
   4960 	struct sys___posix_rename_args ua;
   4961 
   4962 	NETBSD32TOP_UAP(from, const char);
   4963 	NETBSD32TOP_UAP(to, const char);
   4964 
   4965 	return (sys___posix_rename(p, &ua, retval));
   4966 }
   4967 
   4968 int
   4969 netbsd32_swapctl(p, v, retval)
   4970 	struct proc *p;
   4971 	void *v;
   4972 	register_t *retval;
   4973 {
   4974 	struct netbsd32_swapctl_args /* {
   4975 		syscallarg(int) cmd;
   4976 		syscallarg(const netbsd32_voidp) arg;
   4977 		syscallarg(int) misc;
   4978 	} */ *uap = v;
   4979 	struct sys_swapctl_args ua;
   4980 
   4981 	NETBSD32TO64_UAP(cmd);
   4982 	NETBSD32TOP_UAP(arg, const void);
   4983 	NETBSD32TO64_UAP(misc);
   4984 	return (sys_swapctl(p, &ua, retval));
   4985 }
   4986 
   4987 int
   4988 netbsd32_getdents(p, v, retval)
   4989 	struct proc *p;
   4990 	void *v;
   4991 	register_t *retval;
   4992 {
   4993 	struct netbsd32_getdents_args /* {
   4994 		syscallarg(int) fd;
   4995 		syscallarg(netbsd32_charp) buf;
   4996 		syscallarg(netbsd32_size_t) count;
   4997 	} */ *uap = v;
   4998 	struct file *fp;
   4999 	int error, done;
   5000 
   5001 	/* getvnode() will use the descriptor for us */
   5002 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   5003 		return (error);
   5004 	if ((fp->f_flag & FREAD) == 0) {
   5005 		error = EBADF;
   5006 		goto out;
   5007 	}
   5008 	error = vn_readdir(fp, (caddr_t)(u_long)SCARG(uap, buf), UIO_USERSPACE,
   5009 			SCARG(uap, count), &done, p, 0, 0);
   5010 	*retval = done;
   5011  out:
   5012 	FILE_UNUSE(fp, p);
   5013 	return (error);
   5014 }
   5015 
   5016 
   5017 int
   5018 netbsd32_minherit(p, v, retval)
   5019 	struct proc *p;
   5020 	void *v;
   5021 	register_t *retval;
   5022 {
   5023 	struct netbsd32_minherit_args /* {
   5024 		syscallarg(netbsd32_voidp) addr;
   5025 		syscallarg(netbsd32_size_t) len;
   5026 		syscallarg(int) inherit;
   5027 	} */ *uap = v;
   5028 	struct sys_minherit_args ua;
   5029 
   5030 	NETBSD32TOP_UAP(addr, void);
   5031 	NETBSD32TOX_UAP(len, size_t);
   5032 	NETBSD32TO64_UAP(inherit);
   5033 	return (sys_minherit(p, &ua, retval));
   5034 }
   5035 
   5036 int
   5037 netbsd32_lchmod(p, v, retval)
   5038 	struct proc *p;
   5039 	void *v;
   5040 	register_t *retval;
   5041 {
   5042 	struct netbsd32_lchmod_args /* {
   5043 		syscallarg(const netbsd32_charp) path;
   5044 		syscallarg(mode_t) mode;
   5045 	} */ *uap = v;
   5046 	struct sys_lchmod_args ua;
   5047 
   5048 	NETBSD32TOP_UAP(path, const char);
   5049 	NETBSD32TO64_UAP(mode);
   5050 	return (sys_lchmod(p, &ua, retval));
   5051 }
   5052 
   5053 int
   5054 netbsd32_lchown(p, v, retval)
   5055 	struct proc *p;
   5056 	void *v;
   5057 	register_t *retval;
   5058 {
   5059 	struct netbsd32_lchown_args /* {
   5060 		syscallarg(const netbsd32_charp) path;
   5061 		syscallarg(uid_t) uid;
   5062 		syscallarg(gid_t) gid;
   5063 	} */ *uap = v;
   5064 	struct sys_lchown_args ua;
   5065 
   5066 	NETBSD32TOP_UAP(path, const char);
   5067 	NETBSD32TO64_UAP(uid);
   5068 	NETBSD32TO64_UAP(gid);
   5069 	return (sys_lchown(p, &ua, retval));
   5070 }
   5071 
   5072 int
   5073 netbsd32_lutimes(p, v, retval)
   5074 	struct proc *p;
   5075 	void *v;
   5076 	register_t *retval;
   5077 {
   5078 	struct netbsd32_lutimes_args /* {
   5079 		syscallarg(const netbsd32_charp) path;
   5080 		syscallarg(const netbsd32_timevalp_t) tptr;
   5081 	} */ *uap = v;
   5082 	int error;
   5083 	struct nameidata nd;
   5084 
   5085 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, (caddr_t)(u_long)SCARG(uap, path), p);
   5086 	if ((error = namei(&nd)) != 0)
   5087 		return (error);
   5088 
   5089 	error = change_utimes32(nd.ni_vp, (struct timeval *)(u_long)SCARG(uap, tptr), p);
   5090 
   5091 	vrele(nd.ni_vp);
   5092 	return (error);
   5093 }
   5094 
   5095 
   5096 int
   5097 netbsd32___msync13(p, v, retval)
   5098 	struct proc *p;
   5099 	void *v;
   5100 	register_t *retval;
   5101 {
   5102 	struct netbsd32___msync13_args /* {
   5103 		syscallarg(netbsd32_voidp) addr;
   5104 		syscallarg(netbsd32_size_t) len;
   5105 		syscallarg(int) flags;
   5106 	} */ *uap = v;
   5107 	struct sys___msync13_args ua;
   5108 
   5109 	NETBSD32TOP_UAP(addr, void);
   5110 	NETBSD32TOX_UAP(len, size_t);
   5111 	NETBSD32TO64_UAP(flags);
   5112 	return (sys___msync13(p, &ua, retval));
   5113 }
   5114 
   5115 int
   5116 netbsd32___stat13(p, v, retval)
   5117 	struct proc *p;
   5118 	void *v;
   5119 	register_t *retval;
   5120 {
   5121 	struct netbsd32___stat13_args /* {
   5122 		syscallarg(const netbsd32_charp) path;
   5123 		syscallarg(netbsd32_statp_t) ub;
   5124 	} */ *uap = v;
   5125 	struct netbsd32_stat sb32;
   5126 	struct stat sb;
   5127 	int error;
   5128 	struct nameidata nd;
   5129 
   5130 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   5131 	    (caddr_t)(u_long)SCARG(uap, path), p);
   5132 	if ((error = namei(&nd)) != 0)
   5133 		return (error);
   5134 	error = vn_stat(nd.ni_vp, &sb, p);
   5135 	vput(nd.ni_vp);
   5136 	if (error)
   5137 		return (error);
   5138 	netbsd32_from___stat13(&sb, &sb32);
   5139 	error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof(sb32));
   5140 	return (error);
   5141 }
   5142 
   5143 int
   5144 netbsd32___fstat13(p, v, retval)
   5145 	struct proc *p;
   5146 	void *v;
   5147 	register_t *retval;
   5148 {
   5149 	struct netbsd32___fstat13_args /* {
   5150 		syscallarg(int) fd;
   5151 		syscallarg(netbsd32_statp_t) sb;
   5152 	} */ *uap = v;
   5153 	int fd = SCARG(uap, fd);
   5154 	struct filedesc *fdp = p->p_fd;
   5155 	struct file *fp;
   5156 	struct netbsd32_stat sb32;
   5157 	struct stat ub;
   5158 	int error = 0;
   5159 
   5160 	if ((u_int)fd >= fdp->fd_nfiles ||
   5161 	    (fp = fdp->fd_ofiles[fd]) == NULL)
   5162 		return (EBADF);
   5163 	switch (fp->f_type) {
   5164 
   5165 	case DTYPE_VNODE:
   5166 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
   5167 		break;
   5168 
   5169 	case DTYPE_SOCKET:
   5170 		error = soo_stat((struct socket *)fp->f_data, &ub);
   5171 		break;
   5172 
   5173 	default:
   5174 		panic("fstat");
   5175 		/*NOTREACHED*/
   5176 	}
   5177 	if (error == 0) {
   5178 		netbsd32_from___stat13(&ub, &sb32);
   5179 		error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, sb), sizeof(sb32));
   5180 	}
   5181 	return (error);
   5182 }
   5183 
   5184 int
   5185 netbsd32___lstat13(p, v, retval)
   5186 	struct proc *p;
   5187 	void *v;
   5188 	register_t *retval;
   5189 {
   5190 	struct netbsd32___lstat13_args /* {
   5191 		syscallarg(const netbsd32_charp) path;
   5192 		syscallarg(netbsd32_statp_t) ub;
   5193 	} */ *uap = v;
   5194 	struct netbsd32_stat sb32;
   5195 	struct stat sb;
   5196 	int error;
   5197 	struct nameidata nd;
   5198 
   5199 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   5200 	    (caddr_t)(u_long)SCARG(uap, path), p);
   5201 	if ((error = namei(&nd)) != 0)
   5202 		return (error);
   5203 	error = vn_stat(nd.ni_vp, &sb, p);
   5204 	vput(nd.ni_vp);
   5205 	if (error)
   5206 		return (error);
   5207 	netbsd32_from___stat13(&sb, &sb32);
   5208 	error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof(sb32));
   5209 	return (error);
   5210 }
   5211 
   5212 int
   5213 netbsd32___sigaltstack14(p, v, retval)
   5214 	struct proc *p;
   5215 	void *v;
   5216 	register_t *retval;
   5217 {
   5218 	struct netbsd32___sigaltstack14_args /* {
   5219 		syscallarg(const netbsd32_sigaltstackp_t) nss;
   5220 		syscallarg(netbsd32_sigaltstackp_t) oss;
   5221 	} */ *uap = v;
   5222 	struct netbsd32_sigaltstack s32;
   5223 	struct sigaltstack nss, oss;
   5224 	int error;
   5225 
   5226 	if (SCARG(uap, nss)) {
   5227 		error = copyin((caddr_t)(u_long)SCARG(uap, nss), &s32, sizeof(s32));
   5228 		if (error)
   5229 			return (error);
   5230 		nss.ss_sp = (void *)(u_long)s32.ss_sp;
   5231 		nss.ss_size = (size_t)s32.ss_size;
   5232 		nss.ss_flags = s32.ss_flags;
   5233 	}
   5234 	error = sigaltstack1(p,
   5235 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
   5236 	if (error)
   5237 		return (error);
   5238 	if (SCARG(uap, oss)) {
   5239 		s32.ss_sp = (netbsd32_voidp)(u_long)oss.ss_sp;
   5240 		s32.ss_size = (netbsd32_size_t)oss.ss_size;
   5241 		s32.ss_flags = oss.ss_flags;
   5242 		error = copyout(&s32, (caddr_t)(u_long)SCARG(uap, oss), sizeof(s32));
   5243 		if (error)
   5244 			return (error);
   5245 	}
   5246 	return (0);
   5247 }
   5248 
   5249 int
   5250 netbsd32___posix_chown(p, v, retval)
   5251 	struct proc *p;
   5252 	void *v;
   5253 	register_t *retval;
   5254 {
   5255 	struct netbsd32___posix_chown_args /* {
   5256 		syscallarg(const netbsd32_charp) path;
   5257 		syscallarg(uid_t) uid;
   5258 		syscallarg(gid_t) gid;
   5259 	} */ *uap = v;
   5260 	struct sys___posix_chown_args ua;
   5261 
   5262 	NETBSD32TOP_UAP(path, const char);
   5263 	NETBSD32TO64_UAP(uid);
   5264 	NETBSD32TO64_UAP(gid);
   5265 	return (sys___posix_chown(p, &ua, retval));
   5266 }
   5267 
   5268 int
   5269 netbsd32___posix_fchown(p, v, retval)
   5270 	struct proc *p;
   5271 	void *v;
   5272 	register_t *retval;
   5273 {
   5274 	struct netbsd32___posix_fchown_args /* {
   5275 		syscallarg(int) fd;
   5276 		syscallarg(uid_t) uid;
   5277 		syscallarg(gid_t) gid;
   5278 	} */ *uap = v;
   5279 	struct sys___posix_fchown_args ua;
   5280 
   5281 	NETBSD32TO64_UAP(fd);
   5282 	NETBSD32TO64_UAP(uid);
   5283 	NETBSD32TO64_UAP(gid);
   5284 	return (sys___posix_fchown(p, &ua, retval));
   5285 }
   5286 
   5287 int
   5288 netbsd32___posix_lchown(p, v, retval)
   5289 	struct proc *p;
   5290 	void *v;
   5291 	register_t *retval;
   5292 {
   5293 	struct netbsd32___posix_lchown_args /* {
   5294 		syscallarg(const netbsd32_charp) path;
   5295 		syscallarg(uid_t) uid;
   5296 		syscallarg(gid_t) gid;
   5297 	} */ *uap = v;
   5298 	struct sys___posix_lchown_args ua;
   5299 
   5300 	NETBSD32TOP_UAP(path, const char);
   5301 	NETBSD32TO64_UAP(uid);
   5302 	NETBSD32TO64_UAP(gid);
   5303 	return (sys___posix_lchown(p, &ua, retval));
   5304 }
   5305 
   5306 int
   5307 netbsd32_getsid(p, v, retval)
   5308 	struct proc *p;
   5309 	void *v;
   5310 	register_t *retval;
   5311 {
   5312 	struct netbsd32_getsid_args /* {
   5313 		syscallarg(pid_t) pid;
   5314 	} */ *uap = v;
   5315 	struct sys_getsid_args ua;
   5316 
   5317 	NETBSD32TO64_UAP(pid);
   5318 	return (sys_getsid(p, &ua, retval));
   5319 }
   5320 
   5321 int
   5322 netbsd32_fktrace(p, v, retval)
   5323 	struct proc *p;
   5324 	void *v;
   5325 	register_t *retval;
   5326 {
   5327 	struct netbsd32_fktrace_args /* {
   5328 		syscallarg(const int) fd;
   5329 		syscallarg(int) ops;
   5330 		syscallarg(int) facs;
   5331 		syscallarg(int) pid;
   5332 	} */ *uap = v;
   5333 	struct sys_fktrace_args ua;
   5334 
   5335 	NETBSD32TOX_UAP(fd, const int);
   5336 	NETBSD32TO64_UAP(ops);
   5337 	NETBSD32TO64_UAP(facs);
   5338 	NETBSD32TO64_UAP(pid);
   5339 	return (sys_fktrace(p, &ua, retval));
   5340 }
   5341 
   5342 int
   5343 netbsd32_preadv(p, v, retval)
   5344 	struct proc *p;
   5345 	void *v;
   5346 	register_t *retval;
   5347 {
   5348 	struct netbsd32_preadv_args /* {
   5349 		syscallarg(int) fd;
   5350 		syscallarg(const netbsd32_iovecp_t) iovp;
   5351 		syscallarg(int) iovcnt;
   5352 		syscallarg(int) pad;
   5353 		syscallarg(off_t) offset;
   5354 	} */ *uap = v;
   5355 	struct filedesc *fdp = p->p_fd;
   5356 	struct file *fp;
   5357 	struct vnode *vp;
   5358 	off_t offset;
   5359 	int error, fd = SCARG(uap, fd);
   5360 
   5361 	if ((u_int)fd >= fdp->fd_nfiles ||
   5362 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   5363 	    (fp->f_flag & FREAD) == 0)
   5364 		return (EBADF);
   5365 
   5366 	vp = (struct vnode *)fp->f_data;
   5367 	if (fp->f_type != DTYPE_VNODE
   5368 	    || vp->v_type == VFIFO)
   5369 		return (ESPIPE);
   5370 
   5371 	offset = SCARG(uap, offset);
   5372 
   5373 	/*
   5374 	 * XXX This works because no file systems actually
   5375 	 * XXX take any action on the seek operation.
   5376 	 */
   5377 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   5378 		return (error);
   5379 
   5380 	return (dofilereadv32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp), SCARG(uap, iovcnt),
   5381 	    &offset, 0, retval));
   5382 }
   5383 
   5384 int
   5385 netbsd32_pwritev(p, v, retval)
   5386 	struct proc *p;
   5387 	void *v;
   5388 	register_t *retval;
   5389 {
   5390 	struct netbsd32_pwritev_args /* {
   5391 		syscallarg(int) fd;
   5392 		syscallarg(const netbsd32_iovecp_t) iovp;
   5393 		syscallarg(int) iovcnt;
   5394 		syscallarg(int) pad;
   5395 		syscallarg(off_t) offset;
   5396 	} */ *uap = v;
   5397 	struct filedesc *fdp = p->p_fd;
   5398 	struct file *fp;
   5399 	struct vnode *vp;
   5400 	off_t offset;
   5401 	int error, fd = SCARG(uap, fd);
   5402 
   5403 	if ((u_int)fd >= fdp->fd_nfiles ||
   5404 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   5405 	    (fp->f_flag & FWRITE) == 0)
   5406 		return (EBADF);
   5407 
   5408 	vp = (struct vnode *)fp->f_data;
   5409 	if (fp->f_type != DTYPE_VNODE
   5410 	    || vp->v_type == VFIFO)
   5411 		return (ESPIPE);
   5412 
   5413 	offset = SCARG(uap, offset);
   5414 
   5415 	/*
   5416 	 * XXX This works because no file systems actually
   5417 	 * XXX take any action on the seek operation.
   5418 	 */
   5419 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   5420 		return (error);
   5421 
   5422 	return (dofilewritev32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp), SCARG(uap, iovcnt),
   5423 	    &offset, 0, retval));
   5424 }
   5425 
   5426 /* ARGSUSED */
   5427 int
   5428 netbsd32___sigaction14(p, v, retval)
   5429 	struct proc *p;
   5430 	void *v;
   5431 	register_t *retval;
   5432 {
   5433 	struct netbsd32___sigaction14_args /* {
   5434 		syscallarg(int) signum;
   5435 		syscallarg(const struct sigaction *) nsa;
   5436 		syscallarg(struct sigaction *) osa;
   5437 	} */ *uap = v;
   5438 	struct netbsd32_sigaction sa32;
   5439 	struct sigaction nsa, osa;
   5440 	int error;
   5441 
   5442 	if (SCARG(uap, nsa)) {
   5443 		error = copyin((caddr_t)(u_long)SCARG(uap, nsa),
   5444 			       &sa32, sizeof(sa32));
   5445 		if (error)
   5446 			return (error);
   5447 		nsa.sa_handler = (void *)(u_long)sa32.sa_handler;
   5448 		nsa.sa_mask = sa32.sa_mask;
   5449 		nsa.sa_flags = sa32.sa_flags;
   5450 	}
   5451 	error = sigaction1(p, SCARG(uap, signum),
   5452 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0);
   5453 	if (error)
   5454 		return (error);
   5455 	if (SCARG(uap, osa)) {
   5456 		sa32.sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
   5457 		sa32.sa_mask = osa.sa_mask;
   5458 		sa32.sa_flags = osa.sa_flags;
   5459 		error = copyout(&sa32, (caddr_t)(u_long)SCARG(uap, osa), sizeof(sa32));
   5460 		if (error)
   5461 			return (error);
   5462 	}
   5463 	return (0);
   5464 }
   5465 
   5466 int netbsd32___sigpending14(p, v, retval)
   5467 	struct proc *p;
   5468 	void   *v;
   5469 	register_t *retval;
   5470 {
   5471 	struct netbsd32___sigpending14_args /* {
   5472 		syscallarg(sigset_t *) set;
   5473 	} */ *uap = v;
   5474 	struct sys___sigpending14_args ua;
   5475 
   5476 	NETBSD32TOP_UAP(set, sigset_t);
   5477 	return (sys___sigpending14(p, &ua, retval));
   5478 }
   5479 
   5480 int netbsd32___sigprocmask14(p, v, retval)
   5481 	struct proc *p;
   5482 	void   *v;
   5483 	register_t *retval;
   5484 {
   5485 	struct netbsd32___sigprocmask14_args /* {
   5486 		syscallarg(int) how;
   5487 		syscallarg(const sigset_t *) set;
   5488 		syscallarg(sigset_t *) oset;
   5489 	} */ *uap = v;
   5490 	struct sys___sigprocmask14_args ua;
   5491 
   5492 	NETBSD32TO64_UAP(how);
   5493 	NETBSD32TOP_UAP(set, sigset_t);
   5494 	NETBSD32TOP_UAP(oset, sigset_t);
   5495 	return (sys___sigprocmask14(p, &ua, retval));
   5496 }
   5497 
   5498 int netbsd32___sigsuspend14(p, v, retval)
   5499 	struct proc *p;
   5500 	void   *v;
   5501 	register_t *retval;
   5502 {
   5503 	struct netbsd32___sigsuspend14_args /* {
   5504 		syscallarg(const sigset_t *) set;
   5505 	} */ *uap = v;
   5506 	struct sys___sigsuspend14_args ua;
   5507 
   5508 	NETBSD32TOP_UAP(set, sigset_t);
   5509 	return (sys___sigsuspend14(p, &ua, retval));
   5510 };
   5511 
   5512 
   5513 /*
   5514  * Find pathname of process's current directory.
   5515  *
   5516  * Use vfs vnode-to-name reverse cache; if that fails, fall back
   5517  * to reading directory contents.
   5518  */
   5519 int
   5520 getcwd_common __P((struct vnode *, struct vnode *,
   5521 		   char **, char *, int, int, struct proc *));
   5522 
   5523 int netbsd32___getcwd(p, v, retval)
   5524 	struct proc *p;
   5525 	void   *v;
   5526 	register_t *retval;
   5527 {
   5528 	struct netbsd32___getcwd_args /* {
   5529 		syscallarg(char *) bufp;
   5530 		syscallarg(size_t) length;
   5531 	} */ *uap = v;
   5532 
   5533 	int     error;
   5534 	char   *path;
   5535 	char   *bp, *bend;
   5536 	int     len = (int)SCARG(uap, length);
   5537 	int	lenused;
   5538 
   5539 	if (len > MAXPATHLEN*4)
   5540 		len = MAXPATHLEN*4;
   5541 	else if (len < 2)
   5542 		return ERANGE;
   5543 
   5544 	path = (char *)malloc(len, M_TEMP, M_WAITOK);
   5545 	if (!path)
   5546 		return ENOMEM;
   5547 
   5548 	bp = &path[len];
   5549 	bend = bp;
   5550 	*(--bp) = '\0';
   5551 
   5552 	/*
   5553 	 * 5th argument here is "max number of vnodes to traverse".
   5554 	 * Since each entry takes up at least 2 bytes in the output buffer,
   5555 	 * limit it to N/2 vnodes for an N byte buffer.
   5556 	 */
   5557 #define GETCWD_CHECK_ACCESS 0x0001
   5558 	error = getcwd_common (p->p_cwdi->cwdi_cdir, NULL, &bp, path, len/2,
   5559 			       GETCWD_CHECK_ACCESS, p);
   5560 
   5561 	if (error)
   5562 		goto out;
   5563 	lenused = bend - bp;
   5564 	*retval = lenused;
   5565 	/* put the result into user buffer */
   5566 	error = copyout(bp, (caddr_t)(u_long)SCARG(uap, bufp), lenused);
   5567 
   5568 out:
   5569 	free(path, M_TEMP);
   5570 	return error;
   5571 }
   5572 
   5573 int netbsd32_fchroot(p, v, retval)
   5574 	struct proc *p;
   5575 	void *v;
   5576 	register_t *retval;
   5577 {
   5578 	struct netbsd32_fchroot_args /* {
   5579 		syscallarg(int) fd;
   5580 	} */ *uap = v;
   5581 	struct sys_fchroot_args ua;
   5582 
   5583 	NETBSD32TO64_UAP(fd);
   5584 	return (sys_fchroot(p, &ua, retval));
   5585 }
   5586 
   5587 /*
   5588  * Open a file given a file handle.
   5589  *
   5590  * Check permissions, allocate an open file structure,
   5591  * and call the device open routine if any.
   5592  */
   5593 int
   5594 netbsd32_fhopen(p, v, retval)
   5595 	struct proc *p;
   5596 	void *v;
   5597 	register_t *retval;
   5598 {
   5599 	struct netbsd32_fhopen_args /* {
   5600 		syscallarg(const fhandle_t *) fhp;
   5601 		syscallarg(int) flags;
   5602 	} */ *uap = v;
   5603 	struct sys_fhopen_args ua;
   5604 
   5605 	NETBSD32TOP_UAP(fhp, fhandle_t);
   5606 	NETBSD32TO64_UAP(flags);
   5607 	return (sys_fhopen(p, &ua, retval));
   5608 }
   5609 
   5610 int netbsd32_fhstat(p, v, retval)
   5611 	struct proc *p;
   5612 	void *v;
   5613 	register_t *retval;
   5614 {
   5615 	struct netbsd32_fhstat_args /* {
   5616 		syscallarg(const netbsd32_fhandlep_t) fhp;
   5617 		syscallarg(struct stat *) sb;
   5618 	} */ *uap = v;
   5619 	struct sys_fhstat_args ua;
   5620 
   5621 	NETBSD32TOP_UAP(fhp, const fhandle_t);
   5622 	NETBSD32TOP_UAP(sb, struct stat);
   5623 	return (sys_fhstat(p, &ua, retval));
   5624 }
   5625 
   5626 int netbsd32_fhstatfs(p, v, retval)
   5627 	struct proc *p;
   5628 	void *v;
   5629 	register_t *retval;
   5630 {
   5631 	struct netbsd32_fhstatfs_args /* {
   5632 		syscallarg(const netbsd32_fhandlep_t) fhp;
   5633 		syscallarg(struct statfs *) buf;
   5634 	} */ *uap = v;
   5635 	struct sys_fhstatfs_args ua;
   5636 
   5637 	NETBSD32TOP_UAP(fhp, const fhandle_t);
   5638 	NETBSD32TOP_UAP(buf, struct statfs);
   5639 	return (sys_fhstatfs(p, &ua, retval));
   5640 }
   5641