Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_netbsd.c revision 1.29
      1 /*	$NetBSD: netbsd32_netbsd.c,v 1.29 2000/06/26 14:21:08 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 
     80 #include <uvm/uvm_extern.h>
     81 
     82 #include <sys/syscallargs.h>
     83 #include <sys/proc.h>
     84 #include <sys/acct.h>
     85 #include <sys/exec.h>
     86 #include <sys/sysctl.h>
     87 
     88 #include <net/if.h>
     89 
     90 #include <compat/netbsd32/netbsd32.h>
     91 #include <compat/netbsd32/netbsd32_syscallargs.h>
     92 
     93 #include <machine/frame.h>
     94 
     95 #if defined(DDB)
     96 #include <ddb/ddbvar.h>
     97 #endif
     98 
     99 static __inline void netbsd32_from_timeval __P((struct timeval *, struct netbsd32_timeval *));
    100 static __inline void netbsd32_to_timeval __P((struct netbsd32_timeval *, struct timeval *));
    101 static __inline void netbsd32_from_itimerval __P((struct itimerval *, struct netbsd32_itimerval *));
    102 static __inline void netbsd32_to_itimerval __P((struct netbsd32_itimerval *, struct itimerval *));
    103 static __inline void netbsd32_to_timespec __P((struct netbsd32_timespec *, struct timespec *));
    104 static __inline void netbsd32_from_timespec __P((struct timespec *, struct netbsd32_timespec *));
    105 static __inline void netbsd32_from_rusage __P((struct rusage *, struct netbsd32_rusage *));
    106 static __inline void netbsd32_to_rusage __P((struct netbsd32_rusage *, struct rusage *));
    107 static __inline int netbsd32_to_iovecin __P((struct netbsd32_iovec *, struct iovec *, int));
    108 static __inline void netbsd32_to_msghdr __P((struct netbsd32_msghdr *, struct msghdr *));
    109 static __inline void netbsd32_from_msghdr __P((struct netbsd32_msghdr *, struct msghdr *));
    110 static __inline void netbsd32_from_statfs __P((struct statfs *, struct netbsd32_statfs *));
    111 static __inline void netbsd32_from_timex __P((struct timex *, struct netbsd32_timex *));
    112 static __inline void netbsd32_to_timex __P((struct netbsd32_timex *, struct timex *));
    113 static __inline void netbsd32_from___stat13 __P((struct stat *, struct netbsd32_stat *));
    114 static __inline void netbsd32_to_ipc_perm __P((struct netbsd32_ipc_perm *, struct ipc_perm *));
    115 static __inline void netbsd32_from_ipc_perm __P((struct ipc_perm *, struct netbsd32_ipc_perm *));
    116 static __inline void netbsd32_to_msg __P((struct netbsd32_msg *, struct msg *));
    117 static __inline void netbsd32_from_msg __P((struct msg *, struct netbsd32_msg *));
    118 static __inline void netbsd32_to_msqid_ds __P((struct netbsd32_msqid_ds *, struct msqid_ds *));
    119 static __inline void netbsd32_from_msqid_ds __P((struct msqid_ds *, struct netbsd32_msqid_ds *));
    120 static __inline void netbsd32_to_shmid_ds __P((struct netbsd32_shmid_ds *, struct shmid_ds *));
    121 static __inline void netbsd32_from_shmid_ds __P((struct shmid_ds *, struct netbsd32_shmid_ds *));
    122 static __inline void netbsd32_to_semid_ds __P((struct  netbsd32_semid_ds *, struct  semid_ds *));
    123 static __inline void netbsd32_from_semid_ds __P((struct  semid_ds *, struct  netbsd32_semid_ds *));
    124 
    125 
    126 static int recvit32 __P((struct proc *, int, struct netbsd32_msghdr *, struct iovec *, caddr_t,
    127 			 register_t *));
    128 static int dofilereadv32 __P((struct proc *, int, struct file *, struct netbsd32_iovec *,
    129 			      int, off_t *, int, register_t *));
    130 static int dofilewritev32 __P((struct proc *, int, struct file *, struct netbsd32_iovec *,
    131 			       int,  off_t *, int, register_t *));
    132 static int change_utimes32 __P((struct vnode *, struct timeval *, struct proc *));
    133 
    134 /* converters for structures that we need */
    135 static __inline void
    136 netbsd32_from_timeval(tv, tv32)
    137 	struct timeval *tv;
    138 	struct netbsd32_timeval *tv32;
    139 {
    140 
    141 	tv32->tv_sec = (netbsd32_long)tv->tv_sec;
    142 	tv32->tv_usec = (netbsd32_long)tv->tv_usec;
    143 }
    144 
    145 static __inline void
    146 netbsd32_to_timeval(tv32, tv)
    147 	struct netbsd32_timeval *tv32;
    148 	struct timeval *tv;
    149 {
    150 
    151 	tv->tv_sec = (long)tv32->tv_sec;
    152 	tv->tv_usec = (long)tv32->tv_usec;
    153 }
    154 
    155 static __inline void
    156 netbsd32_from_itimerval(itv, itv32)
    157 	struct itimerval *itv;
    158 	struct netbsd32_itimerval *itv32;
    159 {
    160 
    161 	netbsd32_from_timeval(&itv->it_interval,
    162 			     &itv32->it_interval);
    163 	netbsd32_from_timeval(&itv->it_value,
    164 			     &itv32->it_value);
    165 }
    166 
    167 static __inline void
    168 netbsd32_to_itimerval(itv32, itv)
    169 	struct netbsd32_itimerval *itv32;
    170 	struct itimerval *itv;
    171 {
    172 
    173 	netbsd32_to_timeval(&itv32->it_interval, &itv->it_interval);
    174 	netbsd32_to_timeval(&itv32->it_value, &itv->it_value);
    175 }
    176 
    177 static __inline void
    178 netbsd32_to_timespec(s32p, p)
    179 	struct netbsd32_timespec *s32p;
    180 	struct timespec *p;
    181 {
    182 
    183 	p->tv_sec = (time_t)s32p->tv_sec;
    184 	p->tv_nsec = (long)s32p->tv_nsec;
    185 }
    186 
    187 static __inline void
    188 netbsd32_from_timespec(p, s32p)
    189 	struct timespec *p;
    190 	struct netbsd32_timespec *s32p;
    191 {
    192 
    193 	s32p->tv_sec = (netbsd32_time_t)p->tv_sec;
    194 	s32p->tv_nsec = (netbsd32_long)p->tv_nsec;
    195 }
    196 
    197 static __inline void
    198 netbsd32_from_rusage(rup, ru32p)
    199 	struct rusage *rup;
    200 	struct netbsd32_rusage *ru32p;
    201 {
    202 
    203 	netbsd32_from_timeval(&rup->ru_utime, &ru32p->ru_utime);
    204 	netbsd32_from_timeval(&rup->ru_stime, &ru32p->ru_stime);
    205 #define C(var)	ru32p->var = (netbsd32_long)rup->var
    206 	C(ru_maxrss);
    207 	C(ru_ixrss);
    208 	C(ru_idrss);
    209 	C(ru_isrss);
    210 	C(ru_minflt);
    211 	C(ru_majflt);
    212 	C(ru_nswap);
    213 	C(ru_inblock);
    214 	C(ru_oublock);
    215 	C(ru_msgsnd);
    216 	C(ru_msgrcv);
    217 	C(ru_nsignals);
    218 	C(ru_nvcsw);
    219 	C(ru_nivcsw);
    220 #undef C
    221 }
    222 
    223 static __inline void
    224 netbsd32_to_rusage(ru32p, rup)
    225 	struct netbsd32_rusage *ru32p;
    226 	struct rusage *rup;
    227 {
    228 
    229 	netbsd32_to_timeval(&ru32p->ru_utime, &rup->ru_utime);
    230 	netbsd32_to_timeval(&ru32p->ru_stime, &rup->ru_stime);
    231 #define C(var)	rup->var = (long)ru32p->var
    232 	C(ru_maxrss);
    233 	C(ru_ixrss);
    234 	C(ru_idrss);
    235 	C(ru_isrss);
    236 	C(ru_minflt);
    237 	C(ru_majflt);
    238 	C(ru_nswap);
    239 	C(ru_inblock);
    240 	C(ru_oublock);
    241 	C(ru_msgsnd);
    242 	C(ru_msgrcv);
    243 	C(ru_nsignals);
    244 	C(ru_nvcsw);
    245 	C(ru_nivcsw);
    246 #undef C
    247 }
    248 
    249 static __inline int
    250 netbsd32_to_iovecin(iov32p, iovp, len)
    251 	struct netbsd32_iovec *iov32p;
    252 	struct iovec *iovp;
    253 	int len;
    254 {
    255 	int i, error=0;
    256 	u_int32_t iov_base;
    257 	u_int32_t iov_len;
    258 	/*
    259 	 * We could allocate an iov32p, do a copyin, and translate
    260 	 * each field and then free it all up, or we could copyin
    261 	 * each field separately.  I'm doing the latter to reduce
    262 	 * the number of MALLOC()s.
    263 	 */
    264 	for (i = 0; i < len; i++, iovp++, iov32p++) {
    265 		if ((error = copyin((caddr_t)&iov32p->iov_base, &iov_base, sizeof(iov_base))))
    266 		    return (error);
    267 		if ((error = copyin((caddr_t)&iov32p->iov_len, &iov_len, sizeof(iov_len))))
    268 		    return (error);
    269 		iovp->iov_base = (void *)(u_long)iov_base;
    270 		iovp->iov_len = (size_t)iov_len;
    271 	}
    272 }
    273 
    274 /* msg_iov must be done separately */
    275 static __inline void
    276 netbsd32_to_msghdr(mhp32, mhp)
    277 	struct netbsd32_msghdr *mhp32;
    278 	struct msghdr *mhp;
    279 {
    280 
    281 	mhp->msg_name = (caddr_t)(u_long)mhp32->msg_name;
    282 	mhp->msg_namelen = mhp32->msg_namelen;
    283 	mhp->msg_iovlen = (size_t)mhp32->msg_iovlen;
    284 	mhp->msg_control = (caddr_t)(u_long)mhp32->msg_control;
    285 	mhp->msg_controllen = mhp32->msg_controllen;
    286 	mhp->msg_flags = mhp32->msg_flags;
    287 }
    288 
    289 /* msg_iov must be done separately */
    290 static __inline void
    291 netbsd32_from_msghdr(mhp32, mhp)
    292 	struct netbsd32_msghdr *mhp32;
    293 	struct msghdr *mhp;
    294 {
    295 
    296 	mhp32->msg_name = mhp32->msg_name;
    297 	mhp32->msg_namelen = mhp32->msg_namelen;
    298 	mhp32->msg_iovlen = mhp32->msg_iovlen;
    299 	mhp32->msg_control = mhp32->msg_control;
    300 	mhp32->msg_controllen = mhp->msg_controllen;
    301 	mhp32->msg_flags = mhp->msg_flags;
    302 }
    303 
    304 static __inline void
    305 netbsd32_from_statfs(sbp, sb32p)
    306 	struct statfs *sbp;
    307 	struct netbsd32_statfs *sb32p;
    308 {
    309 	sb32p->f_type = sbp->f_type;
    310 	sb32p->f_flags = sbp->f_flags;
    311 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
    312 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
    313 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
    314 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
    315 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
    316 	sb32p->f_files = (netbsd32_long)sbp->f_files;
    317 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
    318 	sb32p->f_fsid = sbp->f_fsid;
    319 	sb32p->f_owner = sbp->f_owner;
    320 	sb32p->f_spare[0] = 0;
    321 	sb32p->f_spare[1] = 0;
    322 	sb32p->f_spare[2] = 0;
    323 	sb32p->f_spare[3] = 0;
    324 #if 1
    325 	/* May as well do the whole batch in one go */
    326 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN+MNAMELEN+MNAMELEN);
    327 #else
    328 	/* If we want to be careful */
    329 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN);
    330 	memcpy(sb32p->f_mntonname, sbp->f_mntonname, MNAMELEN);
    331 	memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    332 #endif
    333 }
    334 
    335 static __inline void
    336 netbsd32_from_timex(txp, tx32p)
    337 	struct timex *txp;
    338 	struct netbsd32_timex *tx32p;
    339 {
    340 
    341 	tx32p->modes = txp->modes;
    342 	tx32p->offset = (netbsd32_long)txp->offset;
    343 	tx32p->freq = (netbsd32_long)txp->freq;
    344 	tx32p->maxerror = (netbsd32_long)txp->maxerror;
    345 	tx32p->esterror = (netbsd32_long)txp->esterror;
    346 	tx32p->status = txp->status;
    347 	tx32p->constant = (netbsd32_long)txp->constant;
    348 	tx32p->precision = (netbsd32_long)txp->precision;
    349 	tx32p->tolerance = (netbsd32_long)txp->tolerance;
    350 	tx32p->ppsfreq = (netbsd32_long)txp->ppsfreq;
    351 	tx32p->jitter = (netbsd32_long)txp->jitter;
    352 	tx32p->shift = txp->shift;
    353 	tx32p->stabil = (netbsd32_long)txp->stabil;
    354 	tx32p->jitcnt = (netbsd32_long)txp->jitcnt;
    355 	tx32p->calcnt = (netbsd32_long)txp->calcnt;
    356 	tx32p->errcnt = (netbsd32_long)txp->errcnt;
    357 	tx32p->stbcnt = (netbsd32_long)txp->stbcnt;
    358 }
    359 
    360 static __inline void
    361 netbsd32_to_timex(tx32p, txp)
    362 	struct netbsd32_timex *tx32p;
    363 	struct timex *txp;
    364 {
    365 
    366 	txp->modes = tx32p->modes;
    367 	txp->offset = (long)tx32p->offset;
    368 	txp->freq = (long)tx32p->freq;
    369 	txp->maxerror = (long)tx32p->maxerror;
    370 	txp->esterror = (long)tx32p->esterror;
    371 	txp->status = tx32p->status;
    372 	txp->constant = (long)tx32p->constant;
    373 	txp->precision = (long)tx32p->precision;
    374 	txp->tolerance = (long)tx32p->tolerance;
    375 	txp->ppsfreq = (long)tx32p->ppsfreq;
    376 	txp->jitter = (long)tx32p->jitter;
    377 	txp->shift = tx32p->shift;
    378 	txp->stabil = (long)tx32p->stabil;
    379 	txp->jitcnt = (long)tx32p->jitcnt;
    380 	txp->calcnt = (long)tx32p->calcnt;
    381 	txp->errcnt = (long)tx32p->errcnt;
    382 	txp->stbcnt = (long)tx32p->stbcnt;
    383 }
    384 
    385 static __inline void
    386 netbsd32_from___stat13(sbp, sb32p)
    387 	struct stat *sbp;
    388 	struct netbsd32_stat *sb32p;
    389 {
    390 	sb32p->st_dev = sbp->st_dev;
    391 	sb32p->st_ino = sbp->st_ino;
    392 	sb32p->st_mode = sbp->st_mode;
    393 	sb32p->st_nlink = sbp->st_nlink;
    394 	sb32p->st_uid = sbp->st_uid;
    395 	sb32p->st_gid = sbp->st_gid;
    396 	sb32p->st_rdev = sbp->st_rdev;
    397 	if (sbp->st_size < (quad_t)1 << 32)
    398 		sb32p->st_size = sbp->st_size;
    399 	else
    400 		sb32p->st_size = -2;
    401 	sb32p->st_atimespec.tv_sec = (netbsd32_time_t)sbp->st_atimespec.tv_sec;
    402 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
    403 	sb32p->st_mtimespec.tv_sec = (netbsd32_time_t)sbp->st_mtimespec.tv_sec;
    404 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
    405 	sb32p->st_ctimespec.tv_sec = (netbsd32_time_t)sbp->st_ctimespec.tv_sec;
    406 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
    407 	sb32p->st_blksize = sbp->st_blksize;
    408 	sb32p->st_blocks = sbp->st_blocks;
    409 	sb32p->st_flags = sbp->st_flags;
    410 	sb32p->st_gen = sbp->st_gen;
    411 }
    412 
    413 static __inline void
    414 netbsd32_to_ipc_perm(ip32p, ipp)
    415 	struct netbsd32_ipc_perm *ip32p;
    416 	struct ipc_perm *ipp;
    417 {
    418 
    419 	ipp->cuid = ip32p->cuid;
    420 	ipp->cgid = ip32p->cgid;
    421 	ipp->uid = ip32p->uid;
    422 	ipp->gid = ip32p->gid;
    423 	ipp->mode = ip32p->mode;
    424 	ipp->_seq = ip32p->_seq;
    425 	ipp->_key = (key_t)ip32p->_key;
    426 }
    427 
    428 static __inline void
    429 netbsd32_from_ipc_perm(ipp, ip32p)
    430 	struct ipc_perm *ipp;
    431 	struct netbsd32_ipc_perm *ip32p;
    432 {
    433 
    434 	ip32p->cuid = ipp->cuid;
    435 	ip32p->cgid = ipp->cgid;
    436 	ip32p->uid = ipp->uid;
    437 	ip32p->gid = ipp->gid;
    438 	ip32p->mode = ipp->mode;
    439 	ip32p->_seq = ipp->_seq;
    440 	ip32p->_key = (netbsd32_key_t)ipp->_key;
    441 }
    442 
    443 static __inline void
    444 netbsd32_to_msg(m32p, mp)
    445 	struct netbsd32_msg *m32p;
    446 	struct msg *mp;
    447 {
    448 
    449 	mp->msg_next = (struct msg *)(u_long)m32p->msg_next;
    450 	mp->msg_type = (long)m32p->msg_type;
    451 	mp->msg_ts = m32p->msg_ts;
    452 	mp->msg_spot = m32p->msg_spot;
    453 }
    454 
    455 static __inline void
    456 netbsd32_from_msg(mp, m32p)
    457 	struct msg *mp;
    458 	struct netbsd32_msg *m32p;
    459 {
    460 
    461 	m32p->msg_next = (netbsd32_msgp_t)(u_long)mp->msg_next;
    462 	m32p->msg_type = (netbsd32_long)mp->msg_type;
    463 	m32p->msg_ts = mp->msg_ts;
    464 	m32p->msg_spot = mp->msg_spot;
    465 }
    466 
    467 static __inline void
    468 netbsd32_to_msqid_ds(ds32p, dsp)
    469 	struct netbsd32_msqid_ds *ds32p;
    470 	struct msqid_ds *dsp;
    471 {
    472 
    473 	netbsd32_to_ipc_perm(&ds32p->msg_perm, &dsp->msg_perm);
    474 	netbsd32_to_msg((struct netbsd32_msg *)(u_long)ds32p->_msg_first, dsp->_msg_first);
    475 	netbsd32_to_msg((struct netbsd32_msg *)(u_long)ds32p->_msg_last, dsp->_msg_last);
    476 	dsp->_msg_cbytes = (u_long)ds32p->_msg_cbytes;
    477 	dsp->msg_qnum = (u_long)ds32p->msg_qnum;
    478 	dsp->msg_qbytes = (u_long)ds32p->msg_qbytes;
    479 	dsp->msg_lspid = ds32p->msg_lspid;
    480 	dsp->msg_lrpid = ds32p->msg_lrpid;
    481 	dsp->msg_rtime = (time_t)ds32p->msg_rtime;
    482 	dsp->msg_stime = (time_t)ds32p->msg_stime;
    483 	dsp->msg_ctime = (time_t)ds32p->msg_ctime;
    484 }
    485 
    486 static __inline void
    487 netbsd32_from_msqid_ds(dsp, ds32p)
    488 	struct msqid_ds *dsp;
    489 	struct netbsd32_msqid_ds *ds32p;
    490 {
    491 
    492 	netbsd32_from_ipc_perm(&dsp->msg_perm, &ds32p->msg_perm);
    493 	netbsd32_from_msg(dsp->_msg_first, (struct netbsd32_msg *)(u_long)ds32p->_msg_first);
    494 	netbsd32_from_msg(dsp->_msg_last, (struct netbsd32_msg *)(u_long)ds32p->_msg_last);
    495 	ds32p->_msg_cbytes = (netbsd32_u_long)dsp->_msg_cbytes;
    496 	ds32p->msg_qnum = (netbsd32_u_long)dsp->msg_qnum;
    497 	ds32p->msg_qbytes = (netbsd32_u_long)dsp->msg_qbytes;
    498 	ds32p->msg_lspid = dsp->msg_lspid;
    499 	ds32p->msg_lrpid = dsp->msg_lrpid;
    500 	ds32p->msg_rtime = dsp->msg_rtime;
    501 	ds32p->msg_stime = dsp->msg_stime;
    502 	ds32p->msg_ctime = dsp->msg_ctime;
    503 }
    504 
    505 static __inline void
    506 netbsd32_to_shmid_ds(ds32p, dsp)
    507 	struct netbsd32_shmid_ds *ds32p;
    508 	struct shmid_ds *dsp;
    509 {
    510 
    511 	netbsd32_to_ipc_perm(&ds32p->shm_perm, &dsp->shm_perm);
    512 	dsp->shm_segsz = ds32p->shm_segsz;
    513 	dsp->shm_lpid = ds32p->shm_lpid;
    514 	dsp->shm_cpid = ds32p->shm_cpid;
    515 	dsp->shm_nattch = ds32p->shm_nattch;
    516 	dsp->shm_atime = (long)ds32p->shm_atime;
    517 	dsp->shm_dtime = (long)ds32p->shm_dtime;
    518 	dsp->shm_ctime = (long)ds32p->shm_ctime;
    519 	dsp->_shm_internal = (void *)(u_long)ds32p->_shm_internal;
    520 }
    521 
    522 static __inline void
    523 netbsd32_from_shmid_ds(dsp, ds32p)
    524 	struct shmid_ds *dsp;
    525 	struct netbsd32_shmid_ds *ds32p;
    526 {
    527 
    528 	netbsd32_from_ipc_perm(&dsp->shm_perm, &ds32p->shm_perm);
    529 	ds32p->shm_segsz = dsp->shm_segsz;
    530 	ds32p->shm_lpid = dsp->shm_lpid;
    531 	ds32p->shm_cpid = dsp->shm_cpid;
    532 	ds32p->shm_nattch = dsp->shm_nattch;
    533 	ds32p->shm_atime = (netbsd32_long)dsp->shm_atime;
    534 	ds32p->shm_dtime = (netbsd32_long)dsp->shm_dtime;
    535 	ds32p->shm_ctime = (netbsd32_long)dsp->shm_ctime;
    536 	ds32p->_shm_internal = (netbsd32_voidp)(u_long)dsp->_shm_internal;
    537 }
    538 
    539 static __inline void
    540 netbsd32_to_semid_ds(s32dsp, dsp)
    541 	struct  netbsd32_semid_ds *s32dsp;
    542 	struct  semid_ds *dsp;
    543 {
    544 
    545 	netbsd32_from_ipc_perm(&dsp->sem_perm, &s32dsp->sem_perm);
    546 	dsp->_sem_base = (struct __sem *)(u_long)s32dsp->_sem_base;
    547 	dsp->sem_nsems = s32dsp->sem_nsems;
    548 	dsp->sem_otime = s32dsp->sem_otime;
    549 	dsp->sem_ctime = s32dsp->sem_ctime;
    550 }
    551 
    552 static __inline void
    553 netbsd32_from_semid_ds(dsp, s32dsp)
    554 	struct  semid_ds *dsp;
    555 	struct  netbsd32_semid_ds *s32dsp;
    556 {
    557 
    558 	netbsd32_to_ipc_perm(&s32dsp->sem_perm, &dsp->sem_perm);
    559 	s32dsp->_sem_base = (netbsd32_semp_t)(u_long)dsp->_sem_base;
    560 	s32dsp->sem_nsems = dsp->sem_nsems;
    561 	s32dsp->sem_otime = dsp->sem_otime;
    562 	s32dsp->sem_ctime = dsp->sem_ctime;
    563 }
    564 
    565 /*
    566  * below are all the standard NetBSD system calls, in the 32bit
    567  * environment, witht he necessary conversions to 64bit before
    568  * calling the real syscall.
    569  */
    570 
    571 
    572 int
    573 netbsd32_exit(p, v, retval)
    574 	struct proc *p;
    575 	void *v;
    576 	register_t *retval;
    577 {
    578 	struct netbsd32_exit_args /* {
    579 		syscallarg(int) rval;
    580 	} */ *uap = v;
    581 	struct sys_exit_args ua;
    582 
    583 	NETBSD32TO64_UAP(rval);
    584 	sys_exit(p, &ua, retval);
    585 }
    586 
    587 int
    588 netbsd32_read(p, v, retval)
    589 	struct proc *p;
    590 	void *v;
    591 	register_t *retval;
    592 {
    593 	struct netbsd32_read_args /* {
    594 		syscallarg(int) fd;
    595 		syscallarg(netbsd32_voidp) buf;
    596 		syscallarg(netbsd32_size_t) nbyte;
    597 	} */ *uap = v;
    598 	struct sys_read_args ua;
    599 
    600 	NETBSD32TO64_UAP(fd);
    601 	NETBSD32TOP_UAP(buf, void *);
    602 	NETBSD32TOX_UAP(nbyte, size_t);
    603 	return sys_read(p, &ua, retval);
    604 }
    605 
    606 int
    607 netbsd32_write(p, v, retval)
    608 	struct proc *p;
    609 	void *v;
    610 	register_t *retval;
    611 {
    612 	struct netbsd32_write_args /* {
    613 		syscallarg(int) fd;
    614 		syscallarg(const netbsd32_voidp) buf;
    615 		syscallarg(netbsd32_size_t) nbyte;
    616 	} */ *uap = v;
    617 	struct sys_write_args ua;
    618 
    619 	NETBSD32TO64_UAP(fd);
    620 	NETBSD32TOP_UAP(buf, void *);
    621 	NETBSD32TOX_UAP(nbyte, size_t);
    622 	return sys_write(p, &ua, retval);
    623 }
    624 
    625 int
    626 netbsd32_close(p, v, retval)
    627 	struct proc *p;
    628 	void *v;
    629 	register_t *retval;
    630 {
    631 	struct netbsd32_close_args /* {
    632 		syscallarg(int) fd;
    633 	} */ *uap = v;
    634 	struct sys_close_args ua;
    635 
    636 	NETBSD32TO64_UAP(fd);
    637 	return sys_close(p, &ua, retval);
    638 }
    639 
    640 int
    641 netbsd32_open(p, v, retval)
    642 	struct proc *p;
    643 	void *v;
    644 	register_t *retval;
    645 {
    646 	struct netbsd32_open_args /* {
    647 		syscallarg(const netbsd32_charp) path;
    648 		syscallarg(int) flags;
    649 		syscallarg(mode_t) mode;
    650 	} */ *uap = v;
    651 	struct sys_open_args ua;
    652 	caddr_t sg;
    653 
    654 	NETBSD32TOP_UAP(path, const char);
    655 	NETBSD32TO64_UAP(flags);
    656 	NETBSD32TO64_UAP(mode);
    657 	sg = stackgap_init(p->p_emul);
    658 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
    659 
    660 	return (sys_open(p, &ua, retval));
    661 }
    662 
    663 int
    664 netbsd32_wait4(q, v, retval)
    665 	struct proc *q;
    666 	void *v;
    667 	register_t *retval;
    668 {
    669 	struct netbsd32_wait4_args /* {
    670 		syscallarg(int) pid;
    671 		syscallarg(netbsd32_intp) status;
    672 		syscallarg(int) options;
    673 		syscallarg(netbsd32_rusagep_t) rusage;
    674 	} */ *uap = v;
    675 	struct netbsd32_rusage ru32;
    676 	int nfound;
    677 	struct proc *p, *t;
    678 	int status, error;
    679 
    680 	if (SCARG(uap, pid) == 0)
    681 		SCARG(uap, pid) = -q->p_pgid;
    682 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
    683 		return (EINVAL);
    684 
    685 loop:
    686 	nfound = 0;
    687 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    688 		if (SCARG(uap, pid) != WAIT_ANY &&
    689 		    p->p_pid != SCARG(uap, pid) &&
    690 		    p->p_pgid != -SCARG(uap, pid))
    691 			continue;
    692 		nfound++;
    693 		if (p->p_stat == SZOMB) {
    694 			retval[0] = p->p_pid;
    695 
    696 			if (SCARG(uap, status)) {
    697 				status = p->p_xstat;	/* convert to int */
    698 				error = copyout((caddr_t)&status,
    699 						(caddr_t)(u_long)SCARG(uap, status),
    700 						sizeof(status));
    701 				if (error)
    702 					return (error);
    703 			}
    704 			if (SCARG(uap, rusage)) {
    705 				netbsd32_from_rusage(p->p_ru, &ru32);
    706 				if ((error = copyout((caddr_t)&ru32,
    707 						     (caddr_t)(u_long)SCARG(uap, rusage),
    708 						     sizeof(struct netbsd32_rusage))))
    709 					return (error);
    710 			}
    711 			/*
    712 			 * If we got the child via ptrace(2) or procfs, and
    713 			 * the parent is different (meaning the process was
    714 			 * attached, rather than run as a child), then we need
    715 			 * to give it back to the old parent, and send the
    716 			 * parent a SIGCHLD.  The rest of the cleanup will be
    717 			 * done when the old parent waits on the child.
    718 			 */
    719 			if ((p->p_flag & P_TRACED) &&
    720 			    p->p_oppid != p->p_pptr->p_pid) {
    721 				t = pfind(p->p_oppid);
    722 				proc_reparent(p, t ? t : initproc);
    723 				p->p_oppid = 0;
    724 				p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    725 				psignal(p->p_pptr, SIGCHLD);
    726 				wakeup((caddr_t)p->p_pptr);
    727 				return (0);
    728 			}
    729 			p->p_xstat = 0;
    730 			ruadd(&q->p_stats->p_cru, p->p_ru);
    731 			pool_put(&rusage_pool, p->p_ru);
    732 
    733 			/*
    734 			 * Finally finished with old proc entry.
    735 			 * Unlink it from its process group and free it.
    736 			 */
    737 			leavepgrp(p);
    738 
    739 			LIST_REMOVE(p, p_list);	/* off zombproc */
    740 
    741 			LIST_REMOVE(p, p_sibling);
    742 
    743 			/*
    744 			 * Decrement the count of procs running with this uid.
    745 			 */
    746 			(void)chgproccnt(p->p_cred->p_ruid, -1);
    747 
    748 			/*
    749 			 * Free up credentials.
    750 			 */
    751 			if (--p->p_cred->p_refcnt == 0) {
    752 				crfree(p->p_cred->pc_ucred);
    753 				pool_put(&pcred_pool, p->p_cred);
    754 			}
    755 
    756 			/*
    757 			 * Release reference to text vnode
    758 			 */
    759 			if (p->p_textvp)
    760 				vrele(p->p_textvp);
    761 
    762 			pool_put(&proc_pool, p);
    763 			nprocs--;
    764 			return (0);
    765 		}
    766 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
    767 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
    768 			p->p_flag |= P_WAITED;
    769 			retval[0] = p->p_pid;
    770 
    771 			if (SCARG(uap, status)) {
    772 				status = W_STOPCODE(p->p_xstat);
    773 				error = copyout((caddr_t)&status,
    774 				    (caddr_t)(u_long)SCARG(uap, status),
    775 				    sizeof(status));
    776 			} else
    777 				error = 0;
    778 			return (error);
    779 		}
    780 	}
    781 	if (nfound == 0)
    782 		return (ECHILD);
    783 	if (SCARG(uap, options) & WNOHANG) {
    784 		retval[0] = 0;
    785 		return (0);
    786 	}
    787 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
    788 		return (error);
    789 	goto loop;
    790 }
    791 
    792 int
    793 netbsd32_link(p, v, retval)
    794 	struct proc *p;
    795 	void *v;
    796 	register_t *retval;
    797 {
    798 	struct netbsd32_link_args /* {
    799 		syscallarg(const netbsd32_charp) path;
    800 		syscallarg(const netbsd32_charp) link;
    801 	} */ *uap = v;
    802 	struct sys_link_args ua;
    803 
    804 	NETBSD32TOP_UAP(path, const char);
    805 	NETBSD32TOP_UAP(link, const char);
    806 	return (sys_link(p, &ua, retval));
    807 }
    808 
    809 int
    810 netbsd32_unlink(p, v, retval)
    811 	struct proc *p;
    812 	void *v;
    813 	register_t *retval;
    814 {
    815 	struct netbsd32_unlink_args /* {
    816 		syscallarg(const netbsd32_charp) path;
    817 	} */ *uap = v;
    818 	struct sys_unlink_args ua;
    819 
    820 	NETBSD32TOP_UAP(path, const char);
    821 
    822 	return (sys_unlink(p, &ua, retval));
    823 }
    824 
    825 int
    826 netbsd32_chdir(p, v, retval)
    827 	struct proc *p;
    828 	void *v;
    829 	register_t *retval;
    830 {
    831 	struct netbsd32_chdir_args /* {
    832 		syscallarg(const netbsd32_charp) path;
    833 	} */ *uap = v;
    834 	struct sys_chdir_args ua;
    835 
    836 	NETBSD32TOP_UAP(path, const char);
    837 
    838 	return (sys_chdir(p, &ua, retval));
    839 }
    840 
    841 int
    842 netbsd32_fchdir(p, v, retval)
    843 	struct proc *p;
    844 	void *v;
    845 	register_t *retval;
    846 {
    847 	struct netbsd32_fchdir_args /* {
    848 		syscallarg(int) fd;
    849 	} */ *uap = v;
    850 	struct sys_fchdir_args ua;
    851 
    852 	NETBSD32TO64_UAP(fd);
    853 
    854 	return (sys_fchdir(p, &ua, retval));
    855 }
    856 
    857 int
    858 netbsd32_mknod(p, v, retval)
    859 	struct proc *p;
    860 	void *v;
    861 	register_t *retval;
    862 {
    863 	struct netbsd32_mknod_args /* {
    864 		syscallarg(const netbsd32_charp) path;
    865 		syscallarg(mode_t) mode;
    866 		syscallarg(dev_t) dev;
    867 	} */ *uap = v;
    868 	struct sys_mknod_args ua;
    869 
    870 	NETBSD32TOP_UAP(path, const char);
    871 	NETBSD32TO64_UAP(dev);
    872 	NETBSD32TO64_UAP(mode);
    873 
    874 	return (sys_mknod(p, &ua, retval));
    875 }
    876 
    877 int
    878 netbsd32_chmod(p, v, retval)
    879 	struct proc *p;
    880 	void *v;
    881 	register_t *retval;
    882 {
    883 	struct netbsd32_chmod_args /* {
    884 		syscallarg(const netbsd32_charp) path;
    885 		syscallarg(mode_t) mode;
    886 	} */ *uap = v;
    887 	struct sys_chmod_args ua;
    888 
    889 	NETBSD32TOP_UAP(path, const char);
    890 	NETBSD32TO64_UAP(mode);
    891 
    892 	return (sys_chmod(p, &ua, retval));
    893 }
    894 
    895 int
    896 netbsd32_chown(p, v, retval)
    897 	struct proc *p;
    898 	void *v;
    899 	register_t *retval;
    900 {
    901 	struct netbsd32_chown_args /* {
    902 		syscallarg(const netbsd32_charp) path;
    903 		syscallarg(uid_t) uid;
    904 		syscallarg(gid_t) gid;
    905 	} */ *uap = v;
    906 	struct sys_chown_args ua;
    907 
    908 	NETBSD32TOP_UAP(path, const char);
    909 	NETBSD32TO64_UAP(uid);
    910 	NETBSD32TO64_UAP(gid);
    911 
    912 	return (sys_chown(p, &ua, retval));
    913 }
    914 
    915 int
    916 netbsd32_break(p, v, retval)
    917 	struct proc *p;
    918 	void *v;
    919 	register_t *retval;
    920 {
    921 	struct netbsd32_break_args /* {
    922 		syscallarg(netbsd32_charp) nsize;
    923 	} */ *uap = v;
    924 	struct sys_obreak_args ua;
    925 
    926 	SCARG(&ua, nsize) = (char *)(u_long)SCARG(uap, nsize);
    927 	NETBSD32TOP_UAP(nsize, char);
    928 	return (sys_obreak(p, &ua, retval));
    929 }
    930 
    931 int
    932 netbsd32_getfsstat(p, v, retval)
    933 	struct proc *p;
    934 	void *v;
    935 	register_t *retval;
    936 {
    937 	struct netbsd32_getfsstat_args /* {
    938 		syscallarg(netbsd32_statfsp_t) buf;
    939 		syscallarg(netbsd32_long) bufsize;
    940 		syscallarg(int) flags;
    941 	} */ *uap = v;
    942 	struct mount *mp, *nmp;
    943 	struct statfs *sp;
    944 	struct netbsd32_statfs sb32;
    945 	caddr_t sfsp;
    946 	long count, maxcount, error;
    947 
    948 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
    949 	sfsp = (caddr_t)SCARG(uap, buf);
    950 	simple_lock(&mountlist_slock);
    951 	count = 0;
    952 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    953 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    954 			nmp = mp->mnt_list.cqe_next;
    955 			continue;
    956 		}
    957 		if (sfsp && count < maxcount) {
    958 			sp = &mp->mnt_stat;
    959 			/*
    960 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    961 			 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
    962 			 * overrides MNT_NOWAIT.
    963 			 */
    964 			if (SCARG(uap, flags) != MNT_NOWAIT &&
    965 			    SCARG(uap, flags) != MNT_LAZY &&
    966 			    (SCARG(uap, flags) == MNT_WAIT ||
    967 			     SCARG(uap, flags) == 0) &&
    968 			    (error = VFS_STATFS(mp, sp, p)) != 0) {
    969 				simple_lock(&mountlist_slock);
    970 				nmp = mp->mnt_list.cqe_next;
    971 				vfs_unbusy(mp);
    972 				continue;
    973 			}
    974 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    975 			sp->f_oflags = sp->f_flags & 0xffff;
    976 			netbsd32_from_statfs(sp, &sb32);
    977 			error = copyout(&sb32, sfsp, sizeof(sb32));
    978 			if (error) {
    979 				vfs_unbusy(mp);
    980 				return (error);
    981 			}
    982 			sfsp += sizeof(sb32);
    983 		}
    984 		count++;
    985 		simple_lock(&mountlist_slock);
    986 		nmp = mp->mnt_list.cqe_next;
    987 		vfs_unbusy(mp);
    988 	}
    989 	simple_unlock(&mountlist_slock);
    990 	if (sfsp && count > maxcount)
    991 		*retval = maxcount;
    992 	else
    993 		*retval = count;
    994 	return (0);
    995 }
    996 
    997 int
    998 netbsd32_mount(p, v, retval)
    999 	struct proc *p;
   1000 	void *v;
   1001 	register_t *retval;
   1002 {
   1003 	struct netbsd32_mount_args /* {
   1004 		syscallarg(const netbsd32_charp) type;
   1005 		syscallarg(const netbsd32_charp) path;
   1006 		syscallarg(int) flags;
   1007 		syscallarg(netbsd32_voidp) data;
   1008 	} */ *uap = v;
   1009 	struct sys_mount_args ua;
   1010 
   1011 	NETBSD32TOP_UAP(type, const char);
   1012 	NETBSD32TOP_UAP(path, const char);
   1013 	NETBSD32TO64_UAP(flags);
   1014 	NETBSD32TOP_UAP(data, void);
   1015 	return (sys_mount(p, &ua, retval));
   1016 }
   1017 
   1018 int
   1019 netbsd32_unmount(p, v, retval)
   1020 	struct proc *p;
   1021 	void *v;
   1022 	register_t *retval;
   1023 {
   1024 	struct netbsd32_unmount_args /* {
   1025 		syscallarg(const netbsd32_charp) path;
   1026 		syscallarg(int) flags;
   1027 	} */ *uap = v;
   1028 	struct sys_unmount_args ua;
   1029 
   1030 	NETBSD32TOP_UAP(path, const char);
   1031 	NETBSD32TO64_UAP(flags);
   1032 	return (sys_unmount(p, &ua, retval));
   1033 }
   1034 
   1035 int
   1036 netbsd32_setuid(p, v, retval)
   1037 	struct proc *p;
   1038 	void *v;
   1039 	register_t *retval;
   1040 {
   1041 	struct netbsd32_setuid_args /* {
   1042 		syscallarg(uid_t) uid;
   1043 	} */ *uap = v;
   1044 	struct sys_setuid_args ua;
   1045 
   1046 	NETBSD32TO64_UAP(uid);
   1047 	return (sys_setuid(p, &ua, retval));
   1048 }
   1049 
   1050 int
   1051 netbsd32_ptrace(p, v, retval)
   1052 	struct proc *p;
   1053 	void *v;
   1054 	register_t *retval;
   1055 {
   1056 	struct netbsd32_ptrace_args /* {
   1057 		syscallarg(int) req;
   1058 		syscallarg(pid_t) pid;
   1059 		syscallarg(netbsd32_caddr_t) addr;
   1060 		syscallarg(int) data;
   1061 	} */ *uap = v;
   1062 	struct sys_ptrace_args ua;
   1063 
   1064 	NETBSD32TO64_UAP(req);
   1065 	NETBSD32TO64_UAP(pid);
   1066 	NETBSD32TOX64_UAP(addr, caddr_t);
   1067 	NETBSD32TO64_UAP(data);
   1068 	return (sys_ptrace(p, &ua, retval));
   1069 }
   1070 
   1071 int
   1072 netbsd32_recvmsg(p, v, retval)
   1073 	struct proc *p;
   1074 	void *v;
   1075 	register_t *retval;
   1076 {
   1077 	struct netbsd32_recvmsg_args /* {
   1078 		syscallarg(int) s;
   1079 		syscallarg(netbsd32_msghdrp_t) msg;
   1080 		syscallarg(int) flags;
   1081 	} */ *uap = v;
   1082 	struct netbsd32_msghdr msg;
   1083 	struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
   1084 	int error;
   1085 
   1086 	error = copyin((caddr_t)(u_long)SCARG(uap, msg), (caddr_t)&msg,
   1087 		       sizeof(msg));
   1088 		/* netbsd32_msghdr needs the iov pre-allocated */
   1089 	if (error)
   1090 		return (error);
   1091 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
   1092 		if ((u_int)msg.msg_iovlen > IOV_MAX)
   1093 			return (EMSGSIZE);
   1094 		MALLOC(iov, struct iovec *,
   1095 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
   1096 		       M_WAITOK);
   1097 	} else if ((u_int)msg.msg_iovlen > 0)
   1098 		iov = aiov;
   1099 	else
   1100 		return (EMSGSIZE);
   1101 #ifdef COMPAT_OLDSOCK
   1102 	msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
   1103 #else
   1104 	msg.msg_flags = SCARG(uap, flags);
   1105 #endif
   1106 	uiov = (struct iovec *)(u_long)msg.msg_iov;
   1107 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)uiov,
   1108 				   iov, msg.msg_iovlen);
   1109 	if (error)
   1110 		goto done;
   1111 	if ((error = recvit32(p, SCARG(uap, s), &msg, iov, (caddr_t)0, retval)) == 0) {
   1112 		error = copyout((caddr_t)&msg, (caddr_t)(u_long)SCARG(uap, msg),
   1113 		    sizeof(msg));
   1114 	}
   1115 done:
   1116 	if (iov != aiov)
   1117 		FREE(iov, M_IOV);
   1118 	return (error);
   1119 }
   1120 
   1121 int
   1122 recvit32(p, s, mp, iov, namelenp, retsize)
   1123 	struct proc *p;
   1124 	int s;
   1125 	struct netbsd32_msghdr *mp;
   1126 	struct iovec *iov;
   1127 	caddr_t namelenp;
   1128 	register_t *retsize;
   1129 {
   1130 	struct file *fp;
   1131 	struct uio auio;
   1132 	int i;
   1133 	int len, error;
   1134 	struct mbuf *from = 0, *control = 0;
   1135 	struct socket *so;
   1136 #ifdef KTRACE
   1137 	struct iovec *ktriov = NULL;
   1138 #endif
   1139 
   1140 	/* getsock() will use the descriptor for us */
   1141 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
   1142 		return (error);
   1143 	auio.uio_iov = (struct iovec *)(u_long)mp->msg_iov;
   1144 	auio.uio_iovcnt = mp->msg_iovlen;
   1145 	auio.uio_segflg = UIO_USERSPACE;
   1146 	auio.uio_rw = UIO_READ;
   1147 	auio.uio_procp = p;
   1148 	auio.uio_offset = 0;			/* XXX */
   1149 	auio.uio_resid = 0;
   1150 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
   1151 #if 0
   1152 		/* cannot happen iov_len is unsigned */
   1153 		if (iov->iov_len < 0) {
   1154 			error = EINVAL;
   1155 			goto out1;
   1156 		}
   1157 #endif
   1158 		/*
   1159 		 * Reads return ssize_t because -1 is returned on error.
   1160 		 * Therefore we must restrict the length to SSIZE_MAX to
   1161 		 * avoid garbage return values.
   1162 		 */
   1163 		auio.uio_resid += iov->iov_len;
   1164 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   1165 			error = EINVAL;
   1166 			goto out1;
   1167 		}
   1168 	}
   1169 #ifdef KTRACE
   1170 	if (KTRPOINT(p, KTR_GENIO)) {
   1171 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
   1172 
   1173 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   1174 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   1175 	}
   1176 #endif
   1177 	len = auio.uio_resid;
   1178 	so = (struct socket *)fp->f_data;
   1179 	error = (*so->so_receive)(so, &from, &auio, NULL,
   1180 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
   1181 	if (error) {
   1182 		if (auio.uio_resid != len && (error == ERESTART ||
   1183 		    error == EINTR || error == EWOULDBLOCK))
   1184 			error = 0;
   1185 	}
   1186 #ifdef KTRACE
   1187 	if (ktriov != NULL) {
   1188 		if (error == 0)
   1189 			ktrgenio(p, s, UIO_READ, ktriov,
   1190 			    len - auio.uio_resid, error);
   1191 		FREE(ktriov, M_TEMP);
   1192 	}
   1193 #endif
   1194 	if (error)
   1195 		goto out;
   1196 	*retsize = len - auio.uio_resid;
   1197 	if (mp->msg_name) {
   1198 		len = mp->msg_namelen;
   1199 		if (len <= 0 || from == 0)
   1200 			len = 0;
   1201 		else {
   1202 #ifdef COMPAT_OLDSOCK
   1203 			if (mp->msg_flags & MSG_COMPAT)
   1204 				mtod(from, struct osockaddr *)->sa_family =
   1205 				    mtod(from, struct sockaddr *)->sa_family;
   1206 #endif
   1207 			if (len > from->m_len)
   1208 				len = from->m_len;
   1209 			/* else if len < from->m_len ??? */
   1210 			error = copyout(mtod(from, caddr_t),
   1211 					(caddr_t)(u_long)mp->msg_name, (unsigned)len);
   1212 			if (error)
   1213 				goto out;
   1214 		}
   1215 		mp->msg_namelen = len;
   1216 		if (namelenp &&
   1217 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
   1218 #ifdef COMPAT_OLDSOCK
   1219 			if (mp->msg_flags & MSG_COMPAT)
   1220 				error = 0;	/* old recvfrom didn't check */
   1221 			else
   1222 #endif
   1223 			goto out;
   1224 		}
   1225 	}
   1226 	if (mp->msg_control) {
   1227 #ifdef COMPAT_OLDSOCK
   1228 		/*
   1229 		 * We assume that old recvmsg calls won't receive access
   1230 		 * rights and other control info, esp. as control info
   1231 		 * is always optional and those options didn't exist in 4.3.
   1232 		 * If we receive rights, trim the cmsghdr; anything else
   1233 		 * is tossed.
   1234 		 */
   1235 		if (control && mp->msg_flags & MSG_COMPAT) {
   1236 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
   1237 			    SOL_SOCKET ||
   1238 			    mtod(control, struct cmsghdr *)->cmsg_type !=
   1239 			    SCM_RIGHTS) {
   1240 				mp->msg_controllen = 0;
   1241 				goto out;
   1242 			}
   1243 			control->m_len -= sizeof(struct cmsghdr);
   1244 			control->m_data += sizeof(struct cmsghdr);
   1245 		}
   1246 #endif
   1247 		len = mp->msg_controllen;
   1248 		if (len <= 0 || control == 0)
   1249 			len = 0;
   1250 		else {
   1251 			struct mbuf *m = control;
   1252 			caddr_t p = (caddr_t)(u_long)mp->msg_control;
   1253 
   1254 			do {
   1255 				i = m->m_len;
   1256 				if (len < i) {
   1257 					mp->msg_flags |= MSG_CTRUNC;
   1258 					i = len;
   1259 				}
   1260 				error = copyout(mtod(m, caddr_t), p,
   1261 				    (unsigned)i);
   1262 				if (m->m_next)
   1263 					i = ALIGN(i);
   1264 				p += i;
   1265 				len -= i;
   1266 				if (error != 0 || len <= 0)
   1267 					break;
   1268 			} while ((m = m->m_next) != NULL);
   1269 			len = p - (caddr_t)(u_long)mp->msg_control;
   1270 		}
   1271 		mp->msg_controllen = len;
   1272 	}
   1273  out:
   1274 	if (from)
   1275 		m_freem(from);
   1276 	if (control)
   1277 		m_freem(control);
   1278  out1:
   1279 	FILE_UNUSE(fp, p);
   1280 	return (error);
   1281 }
   1282 
   1283 
   1284 int
   1285 netbsd32_sendmsg(p, v, retval)
   1286 	struct proc *p;
   1287 	void *v;
   1288 	register_t *retval;
   1289 {
   1290 	struct netbsd32_sendmsg_args /* {
   1291 		syscallarg(int) s;
   1292 		syscallarg(const netbsd32_msghdrp_t) msg;
   1293 		syscallarg(int) flags;
   1294 	} */ *uap = v;
   1295 	struct msghdr msg;
   1296 	struct netbsd32_msghdr msg32;
   1297 	struct iovec aiov[UIO_SMALLIOV], *iov;
   1298 	int error;
   1299 
   1300 	error = copyin((caddr_t)(u_long)SCARG(uap, msg),
   1301 		       (caddr_t)&msg32, sizeof(msg32));
   1302 	if (error)
   1303 		return (error);
   1304 	netbsd32_to_msghdr(&msg32, &msg);
   1305 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
   1306 		if ((u_int)msg.msg_iovlen > IOV_MAX)
   1307 			return (EMSGSIZE);
   1308 		MALLOC(iov, struct iovec *,
   1309 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
   1310 		       M_WAITOK);
   1311 	} else if ((u_int)msg.msg_iovlen > 0)
   1312 		iov = aiov;
   1313 	else
   1314 		return (EMSGSIZE);
   1315 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
   1316 				   iov, msg.msg_iovlen);
   1317 	if (error)
   1318 		goto done;
   1319 	msg.msg_iov = iov;
   1320 #ifdef COMPAT_OLDSOCK
   1321 	msg.msg_flags = 0;
   1322 #endif
   1323 	/* Luckily we can use this directly */
   1324 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
   1325 done:
   1326 	if (iov != aiov)
   1327 		FREE(iov, M_IOV);
   1328 	return (error);
   1329 }
   1330 
   1331 int
   1332 netbsd32_recvfrom(p, v, retval)
   1333 	struct proc *p;
   1334 	void *v;
   1335 	register_t *retval;
   1336 {
   1337 	struct netbsd32_recvfrom_args /* {
   1338 		syscallarg(int) s;
   1339 		syscallarg(netbsd32_voidp) buf;
   1340 		syscallarg(netbsd32_size_t) len;
   1341 		syscallarg(int) flags;
   1342 		syscallarg(netbsd32_sockaddrp_t) from;
   1343 		syscallarg(netbsd32_intp) fromlenaddr;
   1344 	} */ *uap = v;
   1345 	struct netbsd32_msghdr msg;
   1346 	struct iovec aiov;
   1347 	int error;
   1348 
   1349 	if (SCARG(uap, fromlenaddr)) {
   1350 		error = copyin((caddr_t)(u_long)SCARG(uap, fromlenaddr),
   1351 			       (caddr_t)&msg.msg_namelen,
   1352 			       sizeof(msg.msg_namelen));
   1353 		if (error)
   1354 			return (error);
   1355 	} else
   1356 		msg.msg_namelen = 0;
   1357 	msg.msg_name = SCARG(uap, from);
   1358 	msg.msg_iov = NULL; /* We can't store a real pointer here */
   1359 	msg.msg_iovlen = 1;
   1360 	aiov.iov_base = (caddr_t)(u_long)SCARG(uap, buf);
   1361 	aiov.iov_len = (u_long)SCARG(uap, len);
   1362 	msg.msg_control = 0;
   1363 	msg.msg_flags = SCARG(uap, flags);
   1364 	return (recvit32(p, SCARG(uap, s), &msg, &aiov,
   1365 		       (caddr_t)(u_long)SCARG(uap, fromlenaddr), retval));
   1366 }
   1367 
   1368 int
   1369 netbsd32_sendto(p, v, retval)
   1370 	struct proc *p;
   1371 	void *v;
   1372 	register_t *retval;
   1373 {
   1374 	struct netbsd32_sendto_args /* {
   1375 		syscallarg(int) s;
   1376 		syscallarg(const netbsd32_voidp) buf;
   1377 		syscallarg(netbsd32_size_t) len;
   1378 		syscallarg(int) flags;
   1379 		syscallarg(const netbsd32_sockaddrp_t) to;
   1380 		syscallarg(int) tolen;
   1381 	} */ *uap = v;
   1382 	struct msghdr msg;
   1383 	struct iovec aiov;
   1384 
   1385 	msg.msg_name = (caddr_t)(u_long)SCARG(uap, to);		/* XXX kills const */
   1386 	msg.msg_namelen = SCARG(uap, tolen);
   1387 	msg.msg_iov = &aiov;
   1388 	msg.msg_iovlen = 1;
   1389 	msg.msg_control = 0;
   1390 #ifdef COMPAT_OLDSOCK
   1391 	msg.msg_flags = 0;
   1392 #endif
   1393 	aiov.iov_base = (char *)(u_long)SCARG(uap, buf);	/* XXX kills const */
   1394 	aiov.iov_len = SCARG(uap, len);
   1395 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
   1396 }
   1397 
   1398 int
   1399 netbsd32_accept(p, v, retval)
   1400 	struct proc *p;
   1401 	void *v;
   1402 	register_t *retval;
   1403 {
   1404 	struct netbsd32_accept_args /* {
   1405 		syscallarg(int) s;
   1406 		syscallarg(netbsd32_sockaddrp_t) name;
   1407 		syscallarg(netbsd32_intp) anamelen;
   1408 	} */ *uap = v;
   1409 	struct sys_accept_args ua;
   1410 
   1411 	NETBSD32TO64_UAP(s);
   1412 	NETBSD32TOP_UAP(name, struct sockaddr);
   1413 	NETBSD32TOP_UAP(anamelen, int);
   1414 	return (sys_accept(p, &ua, retval));
   1415 }
   1416 
   1417 int
   1418 netbsd32_getpeername(p, v, retval)
   1419 	struct proc *p;
   1420 	void *v;
   1421 	register_t *retval;
   1422 {
   1423 	struct netbsd32_getpeername_args /* {
   1424 		syscallarg(int) fdes;
   1425 		syscallarg(netbsd32_sockaddrp_t) asa;
   1426 		syscallarg(netbsd32_intp) alen;
   1427 	} */ *uap = v;
   1428 	struct sys_getpeername_args ua;
   1429 
   1430 	NETBSD32TO64_UAP(fdes);
   1431 	NETBSD32TOP_UAP(asa, struct sockaddr);
   1432 	NETBSD32TOP_UAP(alen, int);
   1433 /* NB: do the protocol specific sockaddrs need to be converted? */
   1434 	return (sys_getpeername(p, &ua, retval));
   1435 }
   1436 
   1437 int
   1438 netbsd32_getsockname(p, v, retval)
   1439 	struct proc *p;
   1440 	void *v;
   1441 	register_t *retval;
   1442 {
   1443 	struct netbsd32_getsockname_args /* {
   1444 		syscallarg(int) fdes;
   1445 		syscallarg(netbsd32_sockaddrp_t) asa;
   1446 		syscallarg(netbsd32_intp) alen;
   1447 	} */ *uap = v;
   1448 	struct sys_getsockname_args ua;
   1449 
   1450 	NETBSD32TO64_UAP(fdes);
   1451 	NETBSD32TOP_UAP(asa, struct sockaddr);
   1452 	NETBSD32TOP_UAP(alen, int);
   1453 	return (sys_getsockname(p, &ua, retval));
   1454 }
   1455 
   1456 int
   1457 netbsd32_access(p, v, retval)
   1458 	struct proc *p;
   1459 	void *v;
   1460 	register_t *retval;
   1461 {
   1462 	struct netbsd32_access_args /* {
   1463 		syscallarg(const netbsd32_charp) path;
   1464 		syscallarg(int) flags;
   1465 	} */ *uap = v;
   1466 	struct sys_access_args ua;
   1467 	caddr_t sg;
   1468 
   1469 	NETBSD32TOP_UAP(path, const char);
   1470 	NETBSD32TO64_UAP(flags);
   1471 	sg = stackgap_init(p->p_emul);
   1472 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1473 
   1474 	return (sys_access(p, &ua, retval));
   1475 }
   1476 
   1477 int
   1478 netbsd32_chflags(p, v, retval)
   1479 	struct proc *p;
   1480 	void *v;
   1481 	register_t *retval;
   1482 {
   1483 	struct netbsd32_chflags_args /* {
   1484 		syscallarg(const netbsd32_charp) path;
   1485 		syscallarg(netbsd32_u_long) flags;
   1486 	} */ *uap = v;
   1487 	struct sys_chflags_args ua;
   1488 
   1489 	NETBSD32TOP_UAP(path, const char);
   1490 	NETBSD32TO64_UAP(flags);
   1491 
   1492 	return (sys_chflags(p, &ua, retval));
   1493 }
   1494 
   1495 int
   1496 netbsd32_fchflags(p, v, retval)
   1497 	struct proc *p;
   1498 	void *v;
   1499 	register_t *retval;
   1500 {
   1501 	struct netbsd32_fchflags_args /* {
   1502 		syscallarg(int) fd;
   1503 		syscallarg(netbsd32_u_long) flags;
   1504 	} */ *uap = v;
   1505 	struct sys_fchflags_args ua;
   1506 
   1507 	NETBSD32TO64_UAP(fd);
   1508 	NETBSD32TO64_UAP(flags);
   1509 
   1510 	return (sys_fchflags(p, &ua, retval));
   1511 }
   1512 
   1513 int
   1514 netbsd32_kill(p, v, retval)
   1515 	struct proc *p;
   1516 	void *v;
   1517 	register_t *retval;
   1518 {
   1519 	struct netbsd32_kill_args /* {
   1520 		syscallarg(int) pid;
   1521 		syscallarg(int) signum;
   1522 	} */ *uap = v;
   1523 	struct sys_kill_args ua;
   1524 
   1525 	NETBSD32TO64_UAP(pid);
   1526 	NETBSD32TO64_UAP(signum);
   1527 
   1528 	return (sys_kill(p, &ua, retval));
   1529 }
   1530 
   1531 int
   1532 netbsd32_dup(p, v, retval)
   1533 	struct proc *p;
   1534 	void *v;
   1535 	register_t *retval;
   1536 {
   1537 	struct netbsd32_dup_args /* {
   1538 		syscallarg(int) fd;
   1539 	} */ *uap = v;
   1540 	struct sys_dup_args ua;
   1541 
   1542 	NETBSD32TO64_UAP(fd);
   1543 
   1544 	return (sys_dup(p, &ua, retval));
   1545 }
   1546 
   1547 int
   1548 netbsd32_profil(p, v, retval)
   1549 	struct proc *p;
   1550 	void *v;
   1551 	register_t *retval;
   1552 {
   1553 	struct netbsd32_profil_args /* {
   1554 		syscallarg(netbsd32_caddr_t) samples;
   1555 		syscallarg(netbsd32_size_t) size;
   1556 		syscallarg(netbsd32_u_long) offset;
   1557 		syscallarg(u_int) scale;
   1558 	} */ *uap = v;
   1559 	struct sys_profil_args ua;
   1560 
   1561 	NETBSD32TOX64_UAP(samples, caddr_t);
   1562 	NETBSD32TOX_UAP(size, size_t);
   1563 	NETBSD32TOX_UAP(offset, u_long);
   1564 	NETBSD32TO64_UAP(scale);
   1565 	return (sys_profil(p, &ua, retval));
   1566 }
   1567 
   1568 int
   1569 netbsd32_ktrace(p, v, retval)
   1570 	struct proc *p;
   1571 	void *v;
   1572 	register_t *retval;
   1573 {
   1574 	struct netbsd32_ktrace_args /* {
   1575 		syscallarg(const netbsd32_charp) fname;
   1576 		syscallarg(int) ops;
   1577 		syscallarg(int) facs;
   1578 		syscallarg(int) pid;
   1579 	} */ *uap = v;
   1580 	struct sys_ktrace_args ua;
   1581 
   1582 	NETBSD32TOP_UAP(fname, const char);
   1583 	NETBSD32TO64_UAP(ops);
   1584 	NETBSD32TO64_UAP(facs);
   1585 	NETBSD32TO64_UAP(pid);
   1586 	return (sys_ktrace(p, &ua, retval));
   1587 }
   1588 
   1589 int
   1590 netbsd32_sigaction(p, v, retval)
   1591 	struct proc *p;
   1592 	void *v;
   1593 	register_t *retval;
   1594 {
   1595 	struct netbsd32_sigaction_args /* {
   1596 		syscallarg(int) signum;
   1597 		syscallarg(const netbsd32_sigactionp_t) nsa;
   1598 		syscallarg(netbsd32_sigactionp_t) osa;
   1599 	} */ *uap = v;
   1600 	struct sigaction nsa, osa;
   1601 	struct netbsd32_sigaction *sa32p, sa32;
   1602 	int error;
   1603 
   1604 	if (SCARG(uap, nsa)) {
   1605 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, nsa);
   1606 		if (copyin(sa32p, &sa32, sizeof(sa32)))
   1607 			return EFAULT;
   1608 		nsa.sa_handler = (void *)(u_long)sa32.sa_handler;
   1609 		nsa.sa_mask = sa32.sa_mask;
   1610 		nsa.sa_flags = sa32.sa_flags;
   1611 	}
   1612 	error = sigaction1(p, SCARG(uap, signum),
   1613 			   SCARG(uap, nsa) ? &nsa : 0,
   1614 			   SCARG(uap, osa) ? &osa : 0);
   1615 
   1616 	if (error)
   1617 		return (error);
   1618 
   1619 	if (SCARG(uap, osa)) {
   1620 		sa32.sa_handler = (netbsd32_sigactionp_t)(u_long)osa.sa_handler;
   1621 		sa32.sa_mask = osa.sa_mask;
   1622 		sa32.sa_flags = osa.sa_flags;
   1623 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, osa);
   1624 		if (copyout(&sa32, sa32p, sizeof(sa32)))
   1625 			return EFAULT;
   1626 	}
   1627 
   1628 	return (0);
   1629 }
   1630 
   1631 int
   1632 netbsd32___getlogin(p, v, retval)
   1633 	struct proc *p;
   1634 	void *v;
   1635 	register_t *retval;
   1636 {
   1637 	struct netbsd32___getlogin_args /* {
   1638 		syscallarg(netbsd32_charp) namebuf;
   1639 		syscallarg(u_int) namelen;
   1640 	} */ *uap = v;
   1641 	struct sys___getlogin_args ua;
   1642 
   1643 	NETBSD32TOP_UAP(namebuf, char);
   1644 	NETBSD32TO64_UAP(namelen);
   1645 	return (sys___getlogin(p, &ua, retval));
   1646 }
   1647 
   1648 int
   1649 netbsd32_setlogin(p, v, retval)
   1650 	struct proc *p;
   1651 	void *v;
   1652 	register_t *retval;
   1653 {
   1654 	struct netbsd32_setlogin_args /* {
   1655 		syscallarg(const netbsd32_charp) namebuf;
   1656 	} */ *uap = v;
   1657 	struct sys_setlogin_args ua;
   1658 
   1659 	NETBSD32TOP_UAP(namebuf, char);
   1660 	return (sys_setlogin(p, &ua, retval));
   1661 }
   1662 
   1663 int
   1664 netbsd32_acct(p, v, retval)
   1665 	struct proc *p;
   1666 	void *v;
   1667 	register_t *retval;
   1668 {
   1669 	struct netbsd32_acct_args /* {
   1670 		syscallarg(const netbsd32_charp) path;
   1671 	} */ *uap = v;
   1672 	struct sys_acct_args ua;
   1673 
   1674 	NETBSD32TOP_UAP(path, const char);
   1675 	return (sys_acct(p, &ua, retval));
   1676 }
   1677 
   1678 int
   1679 netbsd32_revoke(p, v, retval)
   1680 	struct proc *p;
   1681 	void *v;
   1682 	register_t *retval;
   1683 {
   1684 	struct netbsd32_revoke_args /* {
   1685 		syscallarg(const netbsd32_charp) path;
   1686 	} */ *uap = v;
   1687 	struct sys_revoke_args ua;
   1688 	caddr_t sg;
   1689 
   1690 	NETBSD32TOP_UAP(path, const char);
   1691 	sg = stackgap_init(p->p_emul);
   1692 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1693 
   1694 	return (sys_revoke(p, &ua, retval));
   1695 }
   1696 
   1697 int
   1698 netbsd32_symlink(p, v, retval)
   1699 	struct proc *p;
   1700 	void *v;
   1701 	register_t *retval;
   1702 {
   1703 	struct netbsd32_symlink_args /* {
   1704 		syscallarg(const netbsd32_charp) path;
   1705 		syscallarg(const netbsd32_charp) link;
   1706 	} */ *uap = v;
   1707 	struct sys_symlink_args ua;
   1708 
   1709 	NETBSD32TOP_UAP(path, const char);
   1710 	NETBSD32TOP_UAP(link, const char);
   1711 
   1712 	return (sys_symlink(p, &ua, retval));
   1713 }
   1714 
   1715 int
   1716 netbsd32_readlink(p, v, retval)
   1717 	struct proc *p;
   1718 	void *v;
   1719 	register_t *retval;
   1720 {
   1721 	struct netbsd32_readlink_args /* {
   1722 		syscallarg(const netbsd32_charp) path;
   1723 		syscallarg(netbsd32_charp) buf;
   1724 		syscallarg(netbsd32_size_t) count;
   1725 	} */ *uap = v;
   1726 	struct sys_readlink_args ua;
   1727 	caddr_t sg;
   1728 
   1729 	NETBSD32TOP_UAP(path, const char);
   1730 	NETBSD32TOP_UAP(buf, char);
   1731 	NETBSD32TOX_UAP(count, size_t);
   1732 	sg = stackgap_init(p->p_emul);
   1733 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1734 
   1735 	return (sys_readlink(p, &ua, retval));
   1736 }
   1737 
   1738 /*
   1739  * Need to completly reimplement this syscall due to argument copying.
   1740  */
   1741 int
   1742 netbsd32_execve(p, v, retval)
   1743 	struct proc *p;
   1744 	void *v;
   1745 	register_t *retval;
   1746 {
   1747 	struct netbsd32_execve_args /* {
   1748 		syscallarg(const netbsd32_charp) path;
   1749 		syscallarg(netbsd32_charpp) argp;
   1750 		syscallarg(netbsd32_charpp) envp;
   1751 	} */ *uap = v;
   1752 	struct sys_execve_args ua;
   1753 	caddr_t sg;
   1754 	/* Function args */
   1755 	int error, i;
   1756 	struct exec_package pack;
   1757 	struct nameidata nid;
   1758 	struct vattr attr;
   1759 	struct ucred *cred = p->p_ucred;
   1760 	char *argp;
   1761 	netbsd32_charp const *cpp;
   1762 	char *dp;
   1763 	netbsd32_charp sp;
   1764 	long argc, envc;
   1765 	size_t len;
   1766 	char *stack;
   1767 	struct ps_strings arginfo;
   1768 	struct vmspace *vm;
   1769 	char **tmpfap;
   1770 	int szsigcode;
   1771 	extern struct emul emul_netbsd;
   1772 
   1773 
   1774 	NETBSD32TOP_UAP(path, const char);
   1775 	NETBSD32TOP_UAP(argp, char *);
   1776 	NETBSD32TOP_UAP(envp, char *);
   1777 	sg = stackgap_init(p->p_emul);
   1778 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1779 
   1780 	/*
   1781 	 * figure out the maximum size of an exec header, if necessary.
   1782 	 * XXX should be able to keep LKM code from modifying exec switch
   1783 	 * when we're still using it, but...
   1784 	 */
   1785 	if (exec_maxhdrsz == 0) {
   1786 		for (i = 0; i < nexecs; i++)
   1787 			if (execsw[i].es_check != NULL
   1788 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
   1789 				exec_maxhdrsz = execsw[i].es_hdrsz;
   1790 	}
   1791 
   1792 	/* init the namei data to point the file user's program name */
   1793 	/* XXX cgd 960926: why do this here?  most will be clobbered. */
   1794 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(&ua, path), p);
   1795 
   1796 	/*
   1797 	 * initialize the fields of the exec package.
   1798 	 */
   1799 	pack.ep_name = SCARG(&ua, path);
   1800 	MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
   1801 	pack.ep_hdrlen = exec_maxhdrsz;
   1802 	pack.ep_hdrvalid = 0;
   1803 	pack.ep_ndp = &nid;
   1804 	pack.ep_emul_arg = NULL;
   1805 	pack.ep_vmcmds.evs_cnt = 0;
   1806 	pack.ep_vmcmds.evs_used = 0;
   1807 	pack.ep_vap = &attr;
   1808 	pack.ep_emul = &emul_netbsd;
   1809 	pack.ep_flags = 0;
   1810 
   1811 	/* see if we can run it. */
   1812 	if ((error = check_exec(p, &pack)) != 0)
   1813 		goto freehdr;
   1814 
   1815 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
   1816 
   1817 	/* allocate an argument buffer */
   1818 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
   1819 #ifdef DIAGNOSTIC
   1820 	if (argp == (vaddr_t) 0)
   1821 		panic("execve: argp == NULL");
   1822 #endif
   1823 	dp = argp;
   1824 	argc = 0;
   1825 
   1826 	/* copy the fake args list, if there's one, freeing it as we go */
   1827 	if (pack.ep_flags & EXEC_HASARGL) {
   1828 		tmpfap = pack.ep_fa;
   1829 		while (*tmpfap != NULL) {
   1830 			char *cp;
   1831 
   1832 			cp = *tmpfap;
   1833 			while (*cp)
   1834 				*dp++ = *cp++;
   1835 			dp++;
   1836 
   1837 			FREE(*tmpfap, M_EXEC);
   1838 			tmpfap++; argc++;
   1839 		}
   1840 		FREE(pack.ep_fa, M_EXEC);
   1841 		pack.ep_flags &= ~EXEC_HASARGL;
   1842 	}
   1843 
   1844 	/* Now get argv & environment */
   1845 	if (!(cpp = (netbsd32_charp *)SCARG(&ua, argp))) {
   1846 		error = EINVAL;
   1847 		goto bad;
   1848 	}
   1849 
   1850 	if (pack.ep_flags & EXEC_SKIPARG)
   1851 		cpp++;
   1852 
   1853 	while (1) {
   1854 		len = argp + ARG_MAX - dp;
   1855 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
   1856 			goto bad;
   1857 		if (!sp)
   1858 			break;
   1859 		if ((error = copyinstr((char *)(u_long)sp, dp,
   1860 				       len, &len)) != 0) {
   1861 			if (error == ENAMETOOLONG)
   1862 				error = E2BIG;
   1863 			goto bad;
   1864 		}
   1865 		dp += len;
   1866 		cpp++;
   1867 		argc++;
   1868 	}
   1869 
   1870 	envc = 0;
   1871 	/* environment need not be there */
   1872 	if ((cpp = (netbsd32_charp *)SCARG(&ua, envp)) != NULL ) {
   1873 		while (1) {
   1874 			len = argp + ARG_MAX - dp;
   1875 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
   1876 				goto bad;
   1877 			if (!sp)
   1878 				break;
   1879 			if ((error = copyinstr((char *)(u_long)sp,
   1880 					       dp, len, &len)) != 0) {
   1881 				if (error == ENAMETOOLONG)
   1882 					error = E2BIG;
   1883 				goto bad;
   1884 			}
   1885 			dp += len;
   1886 			cpp++;
   1887 			envc++;
   1888 		}
   1889 	}
   1890 
   1891 	dp = (char *) ALIGN(dp);
   1892 
   1893 	szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
   1894 
   1895 	/* Now check if args & environ fit into new stack */
   1896 	if (pack.ep_flags & EXEC_32)
   1897 		len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(int) +
   1898 		       sizeof(int) + dp + STACKGAPLEN + szsigcode +
   1899 		       sizeof(struct ps_strings)) - argp;
   1900 	else
   1901 		len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
   1902 		       sizeof(int) + dp + STACKGAPLEN + szsigcode +
   1903 		       sizeof(struct ps_strings)) - argp;
   1904 
   1905 	len = ALIGN(len);	/* make the stack "safely" aligned */
   1906 
   1907 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
   1908 		error = ENOMEM;
   1909 		goto bad;
   1910 	}
   1911 
   1912 	/* adjust "active stack depth" for process VSZ */
   1913 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
   1914 
   1915 	/*
   1916 	 * Do whatever is necessary to prepare the address space
   1917 	 * for remapping.  Note that this might replace the current
   1918 	 * vmspace with another!
   1919 	 */
   1920 	uvmspace_exec(p);
   1921 
   1922 	/* Now map address space */
   1923 	vm = p->p_vmspace;
   1924 	vm->vm_taddr = (char *) pack.ep_taddr;
   1925 	vm->vm_tsize = btoc(pack.ep_tsize);
   1926 	vm->vm_daddr = (char *) pack.ep_daddr;
   1927 	vm->vm_dsize = btoc(pack.ep_dsize);
   1928 	vm->vm_ssize = btoc(pack.ep_ssize);
   1929 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
   1930 
   1931 	/* create the new process's VM space by running the vmcmds */
   1932 #ifdef DIAGNOSTIC
   1933 	if (pack.ep_vmcmds.evs_used == 0)
   1934 		panic("execve: no vmcmds");
   1935 #endif
   1936 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
   1937 		struct exec_vmcmd *vcp;
   1938 
   1939 		vcp = &pack.ep_vmcmds.evs_cmds[i];
   1940 		error = (*vcp->ev_proc)(p, vcp);
   1941 	}
   1942 
   1943 	/* free the vmspace-creation commands, and release their references */
   1944 	kill_vmcmds(&pack.ep_vmcmds);
   1945 
   1946 	/* if an error happened, deallocate and punt */
   1947 	if (error)
   1948 		goto exec_abort;
   1949 
   1950 	/* remember information about the process */
   1951 	arginfo.ps_nargvstr = argc;
   1952 	arginfo.ps_nenvstr = envc;
   1953 
   1954 	stack = (char *) (USRSTACK - len);
   1955 	/* Now copy argc, args & environ to new stack */
   1956 	if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
   1957 		goto exec_abort;
   1958 
   1959 	/* copy out the process's ps_strings structure */
   1960 	if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
   1961 		goto exec_abort;
   1962 
   1963 	/* copy out the process's signal trapoline code */
   1964 	if (szsigcode) {
   1965 		if (copyout((char *)pack.ep_emul->e_sigcode,
   1966 		    p->p_sigacts->ps_sigcode = (char *)PS_STRINGS - szsigcode,
   1967 		    szsigcode))
   1968 			goto exec_abort;
   1969 #ifdef PMAP_NEED_PROCWR
   1970 		/* This is code. Let the pmap do what is needed. */
   1971 		pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode);
   1972 #endif
   1973 	}
   1974 
   1975 	stopprofclock(p);	/* stop profiling */
   1976 	fdcloseexec(p);		/* handle close on exec */
   1977 	execsigs(p);		/* reset catched signals */
   1978 	p->p_ctxlink = NULL;	/* reset ucontext link */
   1979 
   1980 	/* set command name & other accounting info */
   1981 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
   1982 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
   1983 	p->p_comm[len] = 0;
   1984 	p->p_acflag &= ~AFORK;
   1985 
   1986 	/* record proc's vnode, for use by procfs and others */
   1987         if (p->p_textvp)
   1988                 vrele(p->p_textvp);
   1989 	VREF(pack.ep_vp);
   1990 	p->p_textvp = pack.ep_vp;
   1991 
   1992 	p->p_flag |= P_EXEC;
   1993 	if (p->p_flag & P_PPWAIT) {
   1994 		p->p_flag &= ~P_PPWAIT;
   1995 		wakeup((caddr_t) p->p_pptr);
   1996 	}
   1997 
   1998 	/*
   1999 	 * deal with set[ug]id.
   2000 	 * MNT_NOSUID and P_TRACED have already been used to disable s[ug]id.
   2001 	 */
   2002 	if (((attr.va_mode & S_ISUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
   2003 	 || ((attr.va_mode & S_ISGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
   2004 		p->p_ucred = crcopy(cred);
   2005 #ifdef KTRACE
   2006 		/*
   2007 		 * If process is being ktraced, turn off - unless
   2008 		 * root set it.
   2009 		 */
   2010 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
   2011 			ktrderef(p);
   2012 #endif
   2013 		if (attr.va_mode & S_ISUID)
   2014 			p->p_ucred->cr_uid = attr.va_uid;
   2015 		if (attr.va_mode & S_ISGID)
   2016 			p->p_ucred->cr_gid = attr.va_gid;
   2017 		p_sugid(p);
   2018 	} else
   2019 		p->p_flag &= ~P_SUGID;
   2020 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
   2021 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
   2022 
   2023 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2024 
   2025 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2026 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2027 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2028 	vput(pack.ep_vp);
   2029 
   2030 	/* setup new registers and do misc. setup. */
   2031 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack);
   2032 
   2033 	if (p->p_flag & P_TRACED)
   2034 		psignal(p, SIGTRAP);
   2035 
   2036 	p->p_emul = pack.ep_emul;
   2037 	FREE(pack.ep_hdr, M_EXEC);
   2038 
   2039 #ifdef KTRACE
   2040 	if (KTRPOINT(p, KTR_EMUL))
   2041 		ktremul(p);
   2042 #endif
   2043 
   2044 	return (EJUSTRETURN);
   2045 
   2046 bad:
   2047 	/* free the vmspace-creation commands, and release their references */
   2048 	kill_vmcmds(&pack.ep_vmcmds);
   2049 	/* kill any opened file descriptor, if necessary */
   2050 	if (pack.ep_flags & EXEC_HASFD) {
   2051 		pack.ep_flags &= ~EXEC_HASFD;
   2052 		(void) fdrelease(p, pack.ep_fd);
   2053 	}
   2054 	/* close and put the exec'd file */
   2055 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2056 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2057 	vput(pack.ep_vp);
   2058 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2059 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2060 
   2061 freehdr:
   2062 	FREE(pack.ep_hdr, M_EXEC);
   2063 	return error;
   2064 
   2065 exec_abort:
   2066 	/*
   2067 	 * the old process doesn't exist anymore.  exit gracefully.
   2068 	 * get rid of the (new) address space we have created, if any, get rid
   2069 	 * of our namei data and vnode, and exit noting failure
   2070 	 */
   2071 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
   2072 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
   2073 	if (pack.ep_emul_arg)
   2074 		FREE(pack.ep_emul_arg, M_TEMP);
   2075 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2076 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2077 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2078 	vput(pack.ep_vp);
   2079 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2080 	FREE(pack.ep_hdr, M_EXEC);
   2081 	exit1(p, W_EXITCODE(0, SIGABRT));
   2082 	exit1(p, -1);
   2083 
   2084 	/* NOTREACHED */
   2085 	return 0;
   2086 }
   2087 
   2088 int
   2089 netbsd32_umask(p, v, retval)
   2090 	struct proc *p;
   2091 	void *v;
   2092 	register_t *retval;
   2093 {
   2094 	struct netbsd32_umask_args /* {
   2095 		syscallarg(mode_t) newmask;
   2096 	} */ *uap = v;
   2097 	struct sys_umask_args ua;
   2098 
   2099 	NETBSD32TO64_UAP(newmask);
   2100 	return (sys_umask(p, &ua, retval));
   2101 }
   2102 
   2103 int
   2104 netbsd32_chroot(p, v, retval)
   2105 	struct proc *p;
   2106 	void *v;
   2107 	register_t *retval;
   2108 {
   2109 	struct netbsd32_chroot_args /* {
   2110 		syscallarg(const netbsd32_charp) path;
   2111 	} */ *uap = v;
   2112 	struct sys_chroot_args ua;
   2113 
   2114 	NETBSD32TOP_UAP(path, const char);
   2115 	return (sys_chroot(p, &ua, retval));
   2116 }
   2117 
   2118 int
   2119 netbsd32_sbrk(p, v, retval)
   2120 	struct proc *p;
   2121 	void *v;
   2122 	register_t *retval;
   2123 {
   2124 	struct netbsd32_sbrk_args /* {
   2125 		syscallarg(int) incr;
   2126 	} */ *uap = v;
   2127 	struct sys_sbrk_args ua;
   2128 
   2129 	NETBSD32TO64_UAP(incr);
   2130 	return (sys_sbrk(p, &ua, retval));
   2131 }
   2132 
   2133 int
   2134 netbsd32_sstk(p, v, retval)
   2135 	struct proc *p;
   2136 	void *v;
   2137 	register_t *retval;
   2138 {
   2139 	struct netbsd32_sstk_args /* {
   2140 		syscallarg(int) incr;
   2141 	} */ *uap = v;
   2142 	struct sys_sstk_args ua;
   2143 
   2144 	NETBSD32TO64_UAP(incr);
   2145 	return (sys_sstk(p, &ua, retval));
   2146 }
   2147 
   2148 int
   2149 netbsd32_munmap(p, v, retval)
   2150 	struct proc *p;
   2151 	void *v;
   2152 	register_t *retval;
   2153 {
   2154 	struct netbsd32_munmap_args /* {
   2155 		syscallarg(netbsd32_voidp) addr;
   2156 		syscallarg(netbsd32_size_t) len;
   2157 	} */ *uap = v;
   2158 	struct sys_munmap_args ua;
   2159 
   2160 	NETBSD32TOP_UAP(addr, void);
   2161 	NETBSD32TOX_UAP(len, size_t);
   2162 	return (sys_munmap(p, &ua, retval));
   2163 }
   2164 
   2165 int
   2166 netbsd32_mprotect(p, v, retval)
   2167 	struct proc *p;
   2168 	void *v;
   2169 	register_t *retval;
   2170 {
   2171 	struct netbsd32_mprotect_args /* {
   2172 		syscallarg(netbsd32_voidp) addr;
   2173 		syscallarg(netbsd32_size_t) len;
   2174 		syscallarg(int) prot;
   2175 	} */ *uap = v;
   2176 	struct sys_mprotect_args ua;
   2177 
   2178 	NETBSD32TOP_UAP(addr, void);
   2179 	NETBSD32TOX_UAP(len, size_t);
   2180 	NETBSD32TO64_UAP(prot);
   2181 	return (sys_mprotect(p, &ua, retval));
   2182 }
   2183 
   2184 int
   2185 netbsd32_madvise(p, v, retval)
   2186 	struct proc *p;
   2187 	void *v;
   2188 	register_t *retval;
   2189 {
   2190 	struct netbsd32_madvise_args /* {
   2191 		syscallarg(netbsd32_voidp) addr;
   2192 		syscallarg(netbsd32_size_t) len;
   2193 		syscallarg(int) behav;
   2194 	} */ *uap = v;
   2195 	struct sys_madvise_args ua;
   2196 
   2197 	NETBSD32TOP_UAP(addr, void);
   2198 	NETBSD32TOX_UAP(len, size_t);
   2199 	NETBSD32TO64_UAP(behav);
   2200 	return (sys_madvise(p, &ua, retval));
   2201 }
   2202 
   2203 int
   2204 netbsd32_mincore(p, v, retval)
   2205 	struct proc *p;
   2206 	void *v;
   2207 	register_t *retval;
   2208 {
   2209 	struct netbsd32_mincore_args /* {
   2210 		syscallarg(netbsd32_caddr_t) addr;
   2211 		syscallarg(netbsd32_size_t) len;
   2212 		syscallarg(netbsd32_charp) vec;
   2213 	} */ *uap = v;
   2214 	struct sys_mincore_args ua;
   2215 
   2216 	NETBSD32TOX64_UAP(addr, caddr_t);
   2217 	NETBSD32TOX_UAP(len, size_t);
   2218 	NETBSD32TOP_UAP(vec, char);
   2219 	return (sys_mincore(p, &ua, retval));
   2220 }
   2221 
   2222 int
   2223 netbsd32_getgroups(p, v, retval)
   2224 	struct proc *p;
   2225 	void *v;
   2226 	register_t *retval;
   2227 {
   2228 	struct netbsd32_getgroups_args /* {
   2229 		syscallarg(int) gidsetsize;
   2230 		syscallarg(netbsd32_gid_tp) gidset;
   2231 	} */ *uap = v;
   2232 	struct pcred *pc = p->p_cred;
   2233 	int ngrp;
   2234 	int error;
   2235 
   2236 	ngrp = SCARG(uap, gidsetsize);
   2237 	if (ngrp == 0) {
   2238 		*retval = pc->pc_ucred->cr_ngroups;
   2239 		return (0);
   2240 	}
   2241 	if (ngrp < pc->pc_ucred->cr_ngroups)
   2242 		return (EINVAL);
   2243 	ngrp = pc->pc_ucred->cr_ngroups;
   2244 	/* Should convert gid_t to netbsd32_gid_t, but they're the same */
   2245 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
   2246 			(caddr_t)(u_long)SCARG(uap, gidset),
   2247 			ngrp * sizeof(gid_t));
   2248 	if (error)
   2249 		return (error);
   2250 	*retval = ngrp;
   2251 	return (0);
   2252 }
   2253 
   2254 int
   2255 netbsd32_setgroups(p, v, retval)
   2256 	struct proc *p;
   2257 	void *v;
   2258 	register_t *retval;
   2259 {
   2260 	struct netbsd32_setgroups_args /* {
   2261 		syscallarg(int) gidsetsize;
   2262 		syscallarg(const netbsd32_gid_tp) gidset;
   2263 	} */ *uap = v;
   2264 	struct sys_setgroups_args ua;
   2265 
   2266 	NETBSD32TO64_UAP(gidsetsize);
   2267 	NETBSD32TOP_UAP(gidset, gid_t);
   2268 	return (sys_setgroups(p, &ua, retval));
   2269 }
   2270 
   2271 int
   2272 netbsd32_setpgid(p, v, retval)
   2273 	struct proc *p;
   2274 	void *v;
   2275 	register_t *retval;
   2276 {
   2277 	struct netbsd32_setpgid_args /* {
   2278 		syscallarg(int) pid;
   2279 		syscallarg(int) pgid;
   2280 	} */ *uap = v;
   2281 	struct sys_setpgid_args ua;
   2282 
   2283 	NETBSD32TO64_UAP(pid);
   2284 	NETBSD32TO64_UAP(pgid);
   2285 	return (sys_setpgid(p, &ua, retval));
   2286 }
   2287 
   2288 int
   2289 netbsd32_setitimer(p, v, retval)
   2290 	struct proc *p;
   2291 	void *v;
   2292 	register_t *retval;
   2293 {
   2294 	struct netbsd32_setitimer_args /* {
   2295 		syscallarg(int) which;
   2296 		syscallarg(const netbsd32_itimervalp_t) itv;
   2297 		syscallarg(netbsd32_itimervalp_t) oitv;
   2298 	} */ *uap = v;
   2299 	struct netbsd32_itimerval s32it, *itvp;
   2300 	int which = SCARG(uap, which);
   2301 	struct netbsd32_getitimer_args getargs;
   2302 	struct itimerval aitv;
   2303 	int s, error;
   2304 
   2305 	if ((u_int)which > ITIMER_PROF)
   2306 		return (EINVAL);
   2307 	itvp = (struct netbsd32_itimerval *)(u_long)SCARG(uap, itv);
   2308 	if (itvp && (error = copyin(itvp, &s32it, sizeof(s32it))))
   2309 		return (error);
   2310 	netbsd32_to_itimerval(&s32it, &aitv);
   2311 	if (SCARG(uap, oitv) != NULL) {
   2312 		SCARG(&getargs, which) = which;
   2313 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   2314 		if ((error = netbsd32_getitimer(p, &getargs, retval)) != 0)
   2315 			return (error);
   2316 	}
   2317 	if (itvp == 0)
   2318 		return (0);
   2319 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
   2320 		return (EINVAL);
   2321 	s = splclock();
   2322 	if (which == ITIMER_REAL) {
   2323 		callout_stop(&p->p_realit_ch);
   2324 		if (timerisset(&aitv.it_value)) {
   2325 			timeradd(&aitv.it_value, &time, &aitv.it_value);
   2326 			callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
   2327 			    realitexpire, p);
   2328 		}
   2329 		p->p_realtimer = aitv;
   2330 	} else
   2331 		p->p_stats->p_timer[which] = aitv;
   2332 	splx(s);
   2333 	return (0);
   2334 }
   2335 
   2336 int
   2337 netbsd32_getitimer(p, v, retval)
   2338 	struct proc *p;
   2339 	void *v;
   2340 	register_t *retval;
   2341 {
   2342 	struct netbsd32_getitimer_args /* {
   2343 		syscallarg(int) which;
   2344 		syscallarg(netbsd32_itimervalp_t) itv;
   2345 	} */ *uap = v;
   2346 	int which = SCARG(uap, which);
   2347 	struct netbsd32_itimerval s32it;
   2348 	struct itimerval aitv;
   2349 	int s;
   2350 
   2351 	if ((u_int)which > ITIMER_PROF)
   2352 		return (EINVAL);
   2353 	s = splclock();
   2354 	if (which == ITIMER_REAL) {
   2355 		/*
   2356 		 * Convert from absolute to relative time in .it_value
   2357 		 * part of real time timer.  If time for real time timer
   2358 		 * has passed return 0, else return difference between
   2359 		 * current time and time for the timer to go off.
   2360 		 */
   2361 		aitv = p->p_realtimer;
   2362 		if (timerisset(&aitv.it_value)) {
   2363 			if (timercmp(&aitv.it_value, &time, <))
   2364 				timerclear(&aitv.it_value);
   2365 			else
   2366 				timersub(&aitv.it_value, &time, &aitv.it_value);
   2367 		}
   2368 	} else
   2369 		aitv = p->p_stats->p_timer[which];
   2370 	splx(s);
   2371 	netbsd32_from_itimerval(&aitv, &s32it);
   2372 	return (copyout(&s32it, (caddr_t)(u_long)SCARG(uap, itv), sizeof(s32it)));
   2373 }
   2374 
   2375 int
   2376 netbsd32_fcntl(p, v, retval)
   2377 	struct proc *p;
   2378 	void *v;
   2379 	register_t *retval;
   2380 {
   2381 	struct netbsd32_fcntl_args /* {
   2382 		syscallarg(int) fd;
   2383 		syscallarg(int) cmd;
   2384 		syscallarg(netbsd32_voidp) arg;
   2385 	} */ *uap = v;
   2386 	struct sys_fcntl_args ua;
   2387 
   2388 	NETBSD32TO64_UAP(fd);
   2389 	NETBSD32TO64_UAP(cmd);
   2390 	NETBSD32TOP_UAP(arg, void);
   2391 	/* XXXX we can do this 'cause flock doesn't change */
   2392 	return (sys_fcntl(p, &ua, retval));
   2393 }
   2394 
   2395 int
   2396 netbsd32_dup2(p, v, retval)
   2397 	struct proc *p;
   2398 	void *v;
   2399 	register_t *retval;
   2400 {
   2401 	struct netbsd32_dup2_args /* {
   2402 		syscallarg(int) from;
   2403 		syscallarg(int) to;
   2404 	} */ *uap = v;
   2405 	struct sys_dup2_args ua;
   2406 
   2407 	NETBSD32TO64_UAP(from);
   2408 	NETBSD32TO64_UAP(to);
   2409 	return (sys_dup2(p, &ua, retval));
   2410 }
   2411 
   2412 int
   2413 netbsd32_select(p, v, retval)
   2414 	struct proc *p;
   2415 	void *v;
   2416 	register_t *retval;
   2417 {
   2418 	struct netbsd32_select_args /* {
   2419 		syscallarg(int) nd;
   2420 		syscallarg(netbsd32_fd_setp_t) in;
   2421 		syscallarg(netbsd32_fd_setp_t) ou;
   2422 		syscallarg(netbsd32_fd_setp_t) ex;
   2423 		syscallarg(netbsd32_timevalp_t) tv;
   2424 	} */ *uap = v;
   2425 /* This one must be done in-line 'cause of the timeval */
   2426 	struct netbsd32_timeval tv32;
   2427 	caddr_t bits;
   2428 	char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
   2429 	struct timeval atv;
   2430 	int s, ncoll, error = 0, timo;
   2431 	size_t ni;
   2432 	extern int	selwait, nselcoll;
   2433 	extern int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
   2434 
   2435 	if (SCARG(uap, nd) < 0)
   2436 		return (EINVAL);
   2437 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
   2438 		/* forgiving; slightly wrong */
   2439 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
   2440 	}
   2441 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
   2442 	if (ni * 6 > sizeof(smallbits))
   2443 		bits = malloc(ni * 6, M_TEMP, M_WAITOK);
   2444 	else
   2445 		bits = smallbits;
   2446 
   2447 #define	getbits(name, x) \
   2448 	if (SCARG(uap, name)) { \
   2449 		error = copyin((caddr_t)(u_long)SCARG(uap, name), bits + ni * x, ni); \
   2450 		if (error) \
   2451 			goto done; \
   2452 	} else \
   2453 		memset(bits + ni * x, 0, ni);
   2454 	getbits(in, 0);
   2455 	getbits(ou, 1);
   2456 	getbits(ex, 2);
   2457 #undef	getbits
   2458 
   2459 	if (SCARG(uap, tv)) {
   2460 		error = copyin((caddr_t)(u_long)SCARG(uap, tv), (caddr_t)&tv32,
   2461 			sizeof(tv32));
   2462 		if (error)
   2463 			goto done;
   2464 		netbsd32_to_timeval(&tv32, &atv);
   2465 		if (itimerfix(&atv)) {
   2466 			error = EINVAL;
   2467 			goto done;
   2468 		}
   2469 		s = splclock();
   2470 		timeradd(&atv, &time, &atv);
   2471 		timo = hzto(&atv);
   2472 		/*
   2473 		 * Avoid inadvertently sleeping forever.
   2474 		 */
   2475 		if (timo == 0)
   2476 			timo = 1;
   2477 		splx(s);
   2478 	} else
   2479 		timo = 0;
   2480 retry:
   2481 	ncoll = nselcoll;
   2482 	p->p_flag |= P_SELECT;
   2483 	error = selscan(p, (fd_mask *)(bits + ni * 0),
   2484 			   (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
   2485 	if (error || *retval)
   2486 		goto done;
   2487 	s = splhigh();
   2488 	if (timo && timercmp(&time, &atv, >=)) {
   2489 		splx(s);
   2490 		goto done;
   2491 	}
   2492 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
   2493 		splx(s);
   2494 		goto retry;
   2495 	}
   2496 	p->p_flag &= ~P_SELECT;
   2497 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
   2498 	splx(s);
   2499 	if (error == 0)
   2500 		goto retry;
   2501 done:
   2502 	p->p_flag &= ~P_SELECT;
   2503 	/* select is not restarted after signals... */
   2504 	if (error == ERESTART)
   2505 		error = EINTR;
   2506 	if (error == EWOULDBLOCK)
   2507 		error = 0;
   2508 	if (error == 0) {
   2509 #define	putbits(name, x) \
   2510 		if (SCARG(uap, name)) { \
   2511 			error = copyout(bits + ni * x, (caddr_t)(u_long)SCARG(uap, name), ni); \
   2512 			if (error) \
   2513 				goto out; \
   2514 		}
   2515 		putbits(in, 3);
   2516 		putbits(ou, 4);
   2517 		putbits(ex, 5);
   2518 #undef putbits
   2519 	}
   2520 out:
   2521 	if (ni * 6 > sizeof(smallbits))
   2522 		free(bits, M_TEMP);
   2523 	return (error);
   2524 }
   2525 
   2526 int
   2527 netbsd32_fsync(p, v, retval)
   2528 	struct proc *p;
   2529 	void *v;
   2530 	register_t *retval;
   2531 {
   2532 	struct netbsd32_fsync_args /* {
   2533 		syscallarg(int) fd;
   2534 	} */ *uap = v;
   2535 	struct sys_fsync_args ua;
   2536 
   2537 	NETBSD32TO64_UAP(fd);
   2538 	return (sys_fsync(p, &ua, retval));
   2539 }
   2540 
   2541 int
   2542 netbsd32_setpriority(p, v, retval)
   2543 	struct proc *p;
   2544 	void *v;
   2545 	register_t *retval;
   2546 {
   2547 	struct netbsd32_setpriority_args /* {
   2548 		syscallarg(int) which;
   2549 		syscallarg(int) who;
   2550 		syscallarg(int) prio;
   2551 	} */ *uap = v;
   2552 	struct sys_setpriority_args ua;
   2553 
   2554 	NETBSD32TO64_UAP(which);
   2555 	NETBSD32TO64_UAP(who);
   2556 	NETBSD32TO64_UAP(prio);
   2557 	return (sys_setpriority(p, &ua, retval));
   2558 }
   2559 
   2560 int
   2561 netbsd32_socket(p, v, retval)
   2562 	struct proc *p;
   2563 	void *v;
   2564 	register_t *retval;
   2565 {
   2566 	struct netbsd32_socket_args /* {
   2567 		syscallarg(int) domain;
   2568 		syscallarg(int) type;
   2569 		syscallarg(int) protocol;
   2570 	} */ *uap = v;
   2571 	struct sys_socket_args ua;
   2572 
   2573 	NETBSD32TO64_UAP(domain);
   2574 	NETBSD32TO64_UAP(type);
   2575 	NETBSD32TO64_UAP(protocol);
   2576 	return (sys_socket(p, &ua, retval));
   2577 }
   2578 
   2579 int
   2580 netbsd32_connect(p, v, retval)
   2581 	struct proc *p;
   2582 	void *v;
   2583 	register_t *retval;
   2584 {
   2585 	struct netbsd32_connect_args /* {
   2586 		syscallarg(int) s;
   2587 		syscallarg(const netbsd32_sockaddrp_t) name;
   2588 		syscallarg(int) namelen;
   2589 	} */ *uap = v;
   2590 	struct sys_connect_args ua;
   2591 
   2592 	NETBSD32TO64_UAP(s);
   2593 	NETBSD32TOP_UAP(name, struct sockaddr);
   2594 	NETBSD32TO64_UAP(namelen);
   2595 	return (sys_connect(p, &ua, retval));
   2596 }
   2597 
   2598 int
   2599 netbsd32_getpriority(p, v, retval)
   2600 	struct proc *p;
   2601 	void *v;
   2602 	register_t *retval;
   2603 {
   2604 	struct netbsd32_getpriority_args /* {
   2605 		syscallarg(int) which;
   2606 		syscallarg(int) who;
   2607 	} */ *uap = v;
   2608 	struct sys_getpriority_args ua;
   2609 
   2610 	NETBSD32TO64_UAP(which);
   2611 	NETBSD32TO64_UAP(who);
   2612 	return (sys_getpriority(p, &ua, retval));
   2613 }
   2614 
   2615 int
   2616 netbsd32_bind(p, v, retval)
   2617 	struct proc *p;
   2618 	void *v;
   2619 	register_t *retval;
   2620 {
   2621 	struct netbsd32_bind_args /* {
   2622 		syscallarg(int) s;
   2623 		syscallarg(const netbsd32_sockaddrp_t) name;
   2624 		syscallarg(int) namelen;
   2625 	} */ *uap = v;
   2626 	struct sys_bind_args ua;
   2627 
   2628 	NETBSD32TO64_UAP(s);
   2629 	NETBSD32TOP_UAP(name, struct sockaddr);
   2630 	NETBSD32TO64_UAP(namelen);
   2631 	return (sys_bind(p, &ua, retval));
   2632 }
   2633 
   2634 int
   2635 netbsd32_setsockopt(p, v, retval)
   2636 	struct proc *p;
   2637 	void *v;
   2638 	register_t *retval;
   2639 {
   2640 	struct netbsd32_setsockopt_args /* {
   2641 		syscallarg(int) s;
   2642 		syscallarg(int) level;
   2643 		syscallarg(int) name;
   2644 		syscallarg(const netbsd32_voidp) val;
   2645 		syscallarg(int) valsize;
   2646 	} */ *uap = v;
   2647 	struct sys_setsockopt_args ua;
   2648 
   2649 	NETBSD32TO64_UAP(s);
   2650 	NETBSD32TO64_UAP(level);
   2651 	NETBSD32TO64_UAP(name);
   2652 	NETBSD32TOP_UAP(val, void);
   2653 	NETBSD32TO64_UAP(valsize);
   2654 	/* may be more efficient to do this inline. */
   2655 	return (sys_setsockopt(p, &ua, retval));
   2656 }
   2657 
   2658 int
   2659 netbsd32_listen(p, v, retval)
   2660 	struct proc *p;
   2661 	void *v;
   2662 	register_t *retval;
   2663 {
   2664 	struct netbsd32_listen_args /* {
   2665 		syscallarg(int) s;
   2666 		syscallarg(int) backlog;
   2667 	} */ *uap = v;
   2668 	struct sys_listen_args ua;
   2669 
   2670 	NETBSD32TO64_UAP(s);
   2671 	NETBSD32TO64_UAP(backlog);
   2672 	return (sys_listen(p, &ua, retval));
   2673 }
   2674 
   2675 int
   2676 netbsd32_gettimeofday(p, v, retval)
   2677 	struct proc *p;
   2678 	void *v;
   2679 	register_t *retval;
   2680 {
   2681 	struct netbsd32_gettimeofday_args /* {
   2682 		syscallarg(netbsd32_timevalp_t) tp;
   2683 		syscallarg(netbsd32_timezonep_t) tzp;
   2684 	} */ *uap = v;
   2685 	struct timeval atv;
   2686 	struct netbsd32_timeval tv32;
   2687 	int error = 0;
   2688 	struct netbsd32_timezone tzfake;
   2689 
   2690 	if (SCARG(uap, tp)) {
   2691 		microtime(&atv);
   2692 		netbsd32_from_timeval(&atv, &tv32);
   2693 		error = copyout(&tv32, (caddr_t)(u_long)SCARG(uap, tp), sizeof(tv32));
   2694 		if (error)
   2695 			return (error);
   2696 	}
   2697 	if (SCARG(uap, tzp)) {
   2698 		/*
   2699 		 * NetBSD has no kernel notion of time zone, so we just
   2700 		 * fake up a timezone struct and return it if demanded.
   2701 		 */
   2702 		tzfake.tz_minuteswest = 0;
   2703 		tzfake.tz_dsttime = 0;
   2704 		error = copyout(&tzfake, (caddr_t)(u_long)SCARG(uap, tzp), sizeof(tzfake));
   2705 	}
   2706 	return (error);
   2707 }
   2708 
   2709 #if 0
   2710 static int settime32 __P((struct timeval *));
   2711 /* This function is used by clock_settime and settimeofday */
   2712 static int
   2713 settime32(tv)
   2714 	struct timeval *tv;
   2715 {
   2716 	struct timeval delta;
   2717 	int s;
   2718 
   2719 	/* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
   2720 	s = splclock();
   2721 	timersub(tv, &time, &delta);
   2722 	if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1)
   2723 		return (EPERM);
   2724 #ifdef notyet
   2725 	if ((delta.tv_sec < 86400) && securelevel > 0)
   2726 		return (EPERM);
   2727 #endif
   2728 	time = *tv;
   2729 	(void) spllowersoftclock();
   2730 	timeradd(&boottime, &delta, &boottime);
   2731 	timeradd(&runtime, &delta, &runtime);
   2732 #	if defined(NFS) || defined(NFSSERVER)
   2733 	{
   2734 		extern void	nqnfs_lease_updatetime __P((int));
   2735 
   2736 		nqnfs_lease_updatetime(delta.tv_sec);
   2737 	}
   2738 #	endif
   2739 	splx(s);
   2740 	resettodr();
   2741 	return (0);
   2742 }
   2743 #endif
   2744 
   2745 int
   2746 netbsd32_settimeofday(p, v, retval)
   2747 	struct proc *p;
   2748 	void *v;
   2749 	register_t *retval;
   2750 {
   2751 	struct netbsd32_settimeofday_args /* {
   2752 		syscallarg(const netbsd32_timevalp_t) tv;
   2753 		syscallarg(const netbsd32_timezonep_t) tzp;
   2754 	} */ *uap = v;
   2755 	struct netbsd32_timeval atv32;
   2756 	struct timeval atv;
   2757 	struct netbsd32_timezone atz;
   2758 	int error;
   2759 
   2760 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   2761 		return (error);
   2762 	/* Verify all parameters before changing time. */
   2763 	if (SCARG(uap, tv) && (error = copyin((caddr_t)(u_long)SCARG(uap, tv),
   2764 	    &atv32, sizeof(atv32))))
   2765 		return (error);
   2766 	netbsd32_to_timeval(&atv32, &atv);
   2767 	/* XXX since we don't use tz, probably no point in doing copyin. */
   2768 	if (SCARG(uap, tzp) && (error = copyin((caddr_t)(u_long)SCARG(uap, tzp),
   2769 	    &atz, sizeof(atz))))
   2770 		return (error);
   2771 	if (SCARG(uap, tv))
   2772 		if ((error = settime(&atv)))
   2773 			return (error);
   2774 	/*
   2775 	 * NetBSD has no kernel notion of time zone, and only an
   2776 	 * obsolete program would try to set it, so we log a warning.
   2777 	 */
   2778 	if (SCARG(uap, tzp))
   2779 		printf("pid %d attempted to set the "
   2780 		    "(obsolete) kernel time zone\n", p->p_pid);
   2781 	return (0);
   2782 }
   2783 
   2784 int
   2785 netbsd32_fchown(p, v, retval)
   2786 	struct proc *p;
   2787 	void *v;
   2788 	register_t *retval;
   2789 {
   2790 	struct netbsd32_fchown_args /* {
   2791 		syscallarg(int) fd;
   2792 		syscallarg(uid_t) uid;
   2793 		syscallarg(gid_t) gid;
   2794 	} */ *uap = v;
   2795 	struct sys_fchown_args ua;
   2796 
   2797 	NETBSD32TO64_UAP(fd);
   2798 	NETBSD32TO64_UAP(uid);
   2799 	NETBSD32TO64_UAP(gid);
   2800 	return (sys_fchown(p, &ua, retval));
   2801 }
   2802 
   2803 int
   2804 netbsd32_fchmod(p, v, retval)
   2805 	struct proc *p;
   2806 	void *v;
   2807 	register_t *retval;
   2808 {
   2809 	struct netbsd32_fchmod_args /* {
   2810 		syscallarg(int) fd;
   2811 		syscallarg(mode_t) mode;
   2812 	} */ *uap = v;
   2813 	struct sys_fchmod_args ua;
   2814 
   2815 	NETBSD32TO64_UAP(fd);
   2816 	NETBSD32TO64_UAP(mode);
   2817 	return (sys_fchmod(p, &ua, retval));
   2818 }
   2819 
   2820 int
   2821 netbsd32_setreuid(p, v, retval)
   2822 	struct proc *p;
   2823 	void *v;
   2824 	register_t *retval;
   2825 {
   2826 	struct netbsd32_setreuid_args /* {
   2827 		syscallarg(uid_t) ruid;
   2828 		syscallarg(uid_t) euid;
   2829 	} */ *uap = v;
   2830 	struct sys_setreuid_args ua;
   2831 
   2832 	NETBSD32TO64_UAP(ruid);
   2833 	NETBSD32TO64_UAP(euid);
   2834 	return (sys_setreuid(p, &ua, retval));
   2835 }
   2836 
   2837 int
   2838 netbsd32_setregid(p, v, retval)
   2839 	struct proc *p;
   2840 	void *v;
   2841 	register_t *retval;
   2842 {
   2843 	struct netbsd32_setregid_args /* {
   2844 		syscallarg(gid_t) rgid;
   2845 		syscallarg(gid_t) egid;
   2846 	} */ *uap = v;
   2847 	struct sys_setregid_args ua;
   2848 
   2849 	NETBSD32TO64_UAP(rgid);
   2850 	NETBSD32TO64_UAP(egid);
   2851 	return (sys_setregid(p, &ua, retval));
   2852 }
   2853 
   2854 int
   2855 netbsd32_getrusage(p, v, retval)
   2856 	struct proc *p;
   2857 	void *v;
   2858 	register_t *retval;
   2859 {
   2860 	struct netbsd32_getrusage_args /* {
   2861 		syscallarg(int) who;
   2862 		syscallarg(netbsd32_rusagep_t) rusage;
   2863 	} */ *uap = v;
   2864 	struct rusage *rup;
   2865 	struct netbsd32_rusage ru;
   2866 
   2867 	switch (SCARG(uap, who)) {
   2868 
   2869 	case RUSAGE_SELF:
   2870 		rup = &p->p_stats->p_ru;
   2871 		calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
   2872 		break;
   2873 
   2874 	case RUSAGE_CHILDREN:
   2875 		rup = &p->p_stats->p_cru;
   2876 		break;
   2877 
   2878 	default:
   2879 		return (EINVAL);
   2880 	}
   2881 	netbsd32_from_rusage(rup, &ru);
   2882 	return (copyout(&ru, (caddr_t)(u_long)SCARG(uap, rusage), sizeof(ru)));
   2883 }
   2884 
   2885 int
   2886 netbsd32_getsockopt(p, v, retval)
   2887 	struct proc *p;
   2888 	void *v;
   2889 	register_t *retval;
   2890 {
   2891 	struct netbsd32_getsockopt_args /* {
   2892 		syscallarg(int) s;
   2893 		syscallarg(int) level;
   2894 		syscallarg(int) name;
   2895 		syscallarg(netbsd32_voidp) val;
   2896 		syscallarg(netbsd32_intp) avalsize;
   2897 	} */ *uap = v;
   2898 	struct sys_getsockopt_args ua;
   2899 
   2900 	NETBSD32TO64_UAP(s);
   2901 	NETBSD32TO64_UAP(level);
   2902 	NETBSD32TO64_UAP(name);
   2903 	NETBSD32TOP_UAP(val, void);
   2904 	NETBSD32TOP_UAP(avalsize, int);
   2905 	return (sys_getsockopt(p, &ua, retval));
   2906 }
   2907 
   2908 int
   2909 netbsd32_readv(p, v, retval)
   2910 	struct proc *p;
   2911 	void *v;
   2912 	register_t *retval;
   2913 {
   2914 	struct netbsd32_readv_args /* {
   2915 		syscallarg(int) fd;
   2916 		syscallarg(const netbsd32_iovecp_t) iovp;
   2917 		syscallarg(int) iovcnt;
   2918 	} */ *uap = v;
   2919 	int fd = SCARG(uap, fd);
   2920 	struct file *fp;
   2921 	struct filedesc *fdp = p->p_fd;
   2922 
   2923 	if ((u_int)fd >= fdp->fd_nfiles ||
   2924 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   2925 	    (fp->f_flag & FREAD) == 0)
   2926 		return (EBADF);
   2927 
   2928 	return (dofilereadv32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp),
   2929 			      SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
   2930 }
   2931 
   2932 /* Damn thing copies in the iovec! */
   2933 int
   2934 dofilereadv32(p, fd, fp, iovp, iovcnt, offset, flags, retval)
   2935 	struct proc *p;
   2936 	int fd;
   2937 	struct file *fp;
   2938 	struct netbsd32_iovec *iovp;
   2939 	int iovcnt;
   2940 	off_t *offset;
   2941 	int flags;
   2942 	register_t *retval;
   2943 {
   2944 	struct uio auio;
   2945 	struct iovec *iov;
   2946 	struct iovec *needfree;
   2947 	struct iovec aiov[UIO_SMALLIOV];
   2948 	long i, cnt, error = 0;
   2949 	u_int iovlen;
   2950 #ifdef KTRACE
   2951 	struct iovec *ktriov = NULL;
   2952 #endif
   2953 
   2954 	/* note: can't use iovlen until iovcnt is validated */
   2955 	iovlen = iovcnt * sizeof(struct iovec);
   2956 	if ((u_int)iovcnt > UIO_SMALLIOV) {
   2957 		if ((u_int)iovcnt > IOV_MAX)
   2958 			return (EINVAL);
   2959 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
   2960 		needfree = iov;
   2961 	} else if ((u_int)iovcnt > 0) {
   2962 		iov = aiov;
   2963 		needfree = NULL;
   2964 	} else
   2965 		return (EINVAL);
   2966 
   2967 	auio.uio_iov = iov;
   2968 	auio.uio_iovcnt = iovcnt;
   2969 	auio.uio_rw = UIO_READ;
   2970 	auio.uio_segflg = UIO_USERSPACE;
   2971 	auio.uio_procp = p;
   2972 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
   2973 	if (error)
   2974 		goto done;
   2975 	auio.uio_resid = 0;
   2976 	for (i = 0; i < iovcnt; i++) {
   2977 		auio.uio_resid += iov->iov_len;
   2978 		/*
   2979 		 * Reads return ssize_t because -1 is returned on error.
   2980 		 * Therefore we must restrict the length to SSIZE_MAX to
   2981 		 * avoid garbage return values.
   2982 		 */
   2983 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   2984 			error = EINVAL;
   2985 			goto done;
   2986 		}
   2987 		iov++;
   2988 	}
   2989 #ifdef KTRACE
   2990 	/*
   2991 	 * if tracing, save a copy of iovec
   2992 	 */
   2993 	if (KTRPOINT(p, KTR_GENIO))  {
   2994 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   2995 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   2996 	}
   2997 #endif
   2998 	cnt = auio.uio_resid;
   2999 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
   3000 	if (error)
   3001 		if (auio.uio_resid != cnt && (error == ERESTART ||
   3002 		    error == EINTR || error == EWOULDBLOCK))
   3003 			error = 0;
   3004 	cnt -= auio.uio_resid;
   3005 #ifdef KTRACE
   3006 	if (KTRPOINT(p, KTR_GENIO))
   3007 		if (error == 0) {
   3008 			ktrgenio(p, fd, UIO_READ, ktriov, cnt,
   3009 			    error);
   3010 		FREE(ktriov, M_TEMP);
   3011 	}
   3012 #endif
   3013 	*retval = cnt;
   3014 done:
   3015 	if (needfree)
   3016 		FREE(needfree, M_IOV);
   3017 	return (error);
   3018 }
   3019 
   3020 
   3021 int
   3022 netbsd32_writev(p, v, retval)
   3023 	struct proc *p;
   3024 	void *v;
   3025 	register_t *retval;
   3026 {
   3027 	struct netbsd32_writev_args /* {
   3028 		syscallarg(int) fd;
   3029 		syscallarg(const netbsd32_iovecp_t) iovp;
   3030 		syscallarg(int) iovcnt;
   3031 	} */ *uap = v;
   3032 	int fd = SCARG(uap, fd);
   3033 	struct file *fp;
   3034 	struct filedesc *fdp = p->p_fd;
   3035 
   3036 	if ((u_int)fd >= fdp->fd_nfiles ||
   3037 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   3038 	    (fp->f_flag & FWRITE) == 0)
   3039 		return (EBADF);
   3040 
   3041 	return (dofilewritev32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp),
   3042 			       SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
   3043 }
   3044 
   3045 int
   3046 dofilewritev32(p, fd, fp, iovp, iovcnt, offset, flags, retval)
   3047 	struct proc *p;
   3048 	int fd;
   3049 	struct file *fp;
   3050 	struct netbsd32_iovec *iovp;
   3051 	int iovcnt;
   3052 	off_t *offset;
   3053 	int flags;
   3054 	register_t *retval;
   3055 {
   3056 	struct uio auio;
   3057 	struct iovec *iov;
   3058 	struct iovec *needfree;
   3059 	struct iovec aiov[UIO_SMALLIOV];
   3060 	long i, cnt, error = 0;
   3061 	u_int iovlen;
   3062 #ifdef KTRACE
   3063 	struct iovec *ktriov = NULL;
   3064 #endif
   3065 
   3066 	/* note: can't use iovlen until iovcnt is validated */
   3067 	iovlen = iovcnt * sizeof(struct iovec);
   3068 	if ((u_int)iovcnt > UIO_SMALLIOV) {
   3069 		if ((u_int)iovcnt > IOV_MAX)
   3070 			return (EINVAL);
   3071 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
   3072 		needfree = iov;
   3073 	} else if ((u_int)iovcnt > 0) {
   3074 		iov = aiov;
   3075 		needfree = NULL;
   3076 	} else
   3077 		return (EINVAL);
   3078 
   3079 	auio.uio_iov = iov;
   3080 	auio.uio_iovcnt = iovcnt;
   3081 	auio.uio_rw = UIO_WRITE;
   3082 	auio.uio_segflg = UIO_USERSPACE;
   3083 	auio.uio_procp = p;
   3084 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
   3085 	if (error)
   3086 		goto done;
   3087 	auio.uio_resid = 0;
   3088 	for (i = 0; i < iovcnt; i++) {
   3089 		auio.uio_resid += iov->iov_len;
   3090 		/*
   3091 		 * Writes return ssize_t because -1 is returned on error.
   3092 		 * Therefore we must restrict the length to SSIZE_MAX to
   3093 		 * avoid garbage return values.
   3094 		 */
   3095 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   3096 			error = EINVAL;
   3097 			goto done;
   3098 		}
   3099 		iov++;
   3100 	}
   3101 #ifdef KTRACE
   3102 	/*
   3103 	 * if tracing, save a copy of iovec
   3104 	 */
   3105 	if (KTRPOINT(p, KTR_GENIO))  {
   3106 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   3107 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   3108 	}
   3109 #endif
   3110 	cnt = auio.uio_resid;
   3111 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
   3112 	if (error) {
   3113 		if (auio.uio_resid != cnt && (error == ERESTART ||
   3114 		    error == EINTR || error == EWOULDBLOCK))
   3115 			error = 0;
   3116 		if (error == EPIPE)
   3117 			psignal(p, SIGPIPE);
   3118 	}
   3119 	cnt -= auio.uio_resid;
   3120 #ifdef KTRACE
   3121 	if (KTRPOINT(p, KTR_GENIO))
   3122 		if (error == 0) {
   3123 			ktrgenio(p, fd, UIO_WRITE, ktriov, cnt,
   3124 			    error);
   3125 		FREE(ktriov, M_TEMP);
   3126 	}
   3127 #endif
   3128 	*retval = cnt;
   3129 done:
   3130 	if (needfree)
   3131 		FREE(needfree, M_IOV);
   3132 	return (error);
   3133 }
   3134 
   3135 
   3136 int
   3137 netbsd32_rename(p, v, retval)
   3138 	struct proc *p;
   3139 	void *v;
   3140 	register_t *retval;
   3141 {
   3142 	struct netbsd32_rename_args /* {
   3143 		syscallarg(const netbsd32_charp) from;
   3144 		syscallarg(const netbsd32_charp) to;
   3145 	} */ *uap = v;
   3146 	struct sys_rename_args ua;
   3147 
   3148 	NETBSD32TOP_UAP(from, const char);
   3149 	NETBSD32TOP_UAP(to, const char)
   3150 
   3151 	return (sys_rename(p, &ua, retval));
   3152 }
   3153 
   3154 int
   3155 netbsd32_flock(p, v, retval)
   3156 	struct proc *p;
   3157 	void *v;
   3158 	register_t *retval;
   3159 {
   3160 	struct netbsd32_flock_args /* {
   3161 		syscallarg(int) fd;
   3162 		syscallarg(int) how;
   3163 	} */ *uap = v;
   3164 	struct sys_flock_args ua;
   3165 
   3166 	NETBSD32TO64_UAP(fd);
   3167 	NETBSD32TO64_UAP(how)
   3168 
   3169 	return (sys_flock(p, &ua, retval));
   3170 }
   3171 
   3172 int
   3173 netbsd32_mkfifo(p, v, retval)
   3174 	struct proc *p;
   3175 	void *v;
   3176 	register_t *retval;
   3177 {
   3178 	struct netbsd32_mkfifo_args /* {
   3179 		syscallarg(const netbsd32_charp) path;
   3180 		syscallarg(mode_t) mode;
   3181 	} */ *uap = v;
   3182 	struct sys_mkfifo_args ua;
   3183 
   3184 	NETBSD32TOP_UAP(path, const char)
   3185 	NETBSD32TO64_UAP(mode);
   3186 	return (sys_mkfifo(p, &ua, retval));
   3187 }
   3188 
   3189 int
   3190 netbsd32_shutdown(p, v, retval)
   3191 	struct proc *p;
   3192 	void *v;
   3193 	register_t *retval;
   3194 {
   3195 	struct netbsd32_shutdown_args /* {
   3196 		syscallarg(int) s;
   3197 		syscallarg(int) how;
   3198 	} */ *uap = v;
   3199 	struct sys_shutdown_args ua;
   3200 
   3201 	NETBSD32TO64_UAP(s)
   3202 	NETBSD32TO64_UAP(how);
   3203 	return (sys_shutdown(p, &ua, retval));
   3204 }
   3205 
   3206 int
   3207 netbsd32_socketpair(p, v, retval)
   3208 	struct proc *p;
   3209 	void *v;
   3210 	register_t *retval;
   3211 {
   3212 	struct netbsd32_socketpair_args /* {
   3213 		syscallarg(int) domain;
   3214 		syscallarg(int) type;
   3215 		syscallarg(int) protocol;
   3216 		syscallarg(netbsd32_intp) rsv;
   3217 	} */ *uap = v;
   3218 	struct sys_socketpair_args ua;
   3219 
   3220 	NETBSD32TO64_UAP(domain);
   3221 	NETBSD32TO64_UAP(type);
   3222 	NETBSD32TO64_UAP(protocol);
   3223 	NETBSD32TOP_UAP(rsv, int);
   3224 	/* Since we're just copying out two `int's we can do this */
   3225 	return (sys_socketpair(p, &ua, retval));
   3226 }
   3227 
   3228 int
   3229 netbsd32_mkdir(p, v, retval)
   3230 	struct proc *p;
   3231 	void *v;
   3232 	register_t *retval;
   3233 {
   3234 	struct netbsd32_mkdir_args /* {
   3235 		syscallarg(const netbsd32_charp) path;
   3236 		syscallarg(mode_t) mode;
   3237 	} */ *uap = v;
   3238 	struct sys_mkdir_args ua;
   3239 
   3240 	NETBSD32TOP_UAP(path, const char)
   3241 	NETBSD32TO64_UAP(mode);
   3242 	return (sys_mkdir(p, &ua, retval));
   3243 }
   3244 
   3245 int
   3246 netbsd32_rmdir(p, v, retval)
   3247 	struct proc *p;
   3248 	void *v;
   3249 	register_t *retval;
   3250 {
   3251 	struct netbsd32_rmdir_args /* {
   3252 		syscallarg(const netbsd32_charp) path;
   3253 	} */ *uap = v;
   3254 	struct sys_rmdir_args ua;
   3255 
   3256 	NETBSD32TOP_UAP(path, const char);
   3257 	return (sys_rmdir(p, &ua, retval));
   3258 }
   3259 
   3260 int
   3261 netbsd32_utimes(p, v, retval)
   3262 	struct proc *p;
   3263 	void *v;
   3264 	register_t *retval;
   3265 {
   3266 	struct netbsd32_utimes_args /* {
   3267 		syscallarg(const netbsd32_charp) path;
   3268 		syscallarg(const netbsd32_timevalp_t) tptr;
   3269 	} */ *uap = v;
   3270 	int error;
   3271 	struct nameidata nd;
   3272 
   3273 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (char *)(u_long)SCARG(uap, path), p);
   3274 	if ((error = namei(&nd)) != 0)
   3275 		return (error);
   3276 
   3277 	error = change_utimes32(nd.ni_vp, (struct timeval *)(u_long)SCARG(uap, tptr), p);
   3278 
   3279 	vrele(nd.ni_vp);
   3280 	return (error);
   3281 }
   3282 
   3283 /*
   3284  * Common routine to set access and modification times given a vnode.
   3285  */
   3286 static int
   3287 change_utimes32(vp, tptr, p)
   3288 	struct vnode *vp;
   3289 	struct timeval *tptr;
   3290 	struct proc *p;
   3291 {
   3292 	struct netbsd32_timeval tv32[2];
   3293 	struct timeval tv[2];
   3294 	struct vattr vattr;
   3295 	int error;
   3296 
   3297 	VATTR_NULL(&vattr);
   3298 	if (tptr == NULL) {
   3299 		microtime(&tv[0]);
   3300 		tv[1] = tv[0];
   3301 		vattr.va_vaflags |= VA_UTIMES_NULL;
   3302 	} else {
   3303 		error = copyin(tptr, tv, sizeof(tv));
   3304 		if (error)
   3305 			return (error);
   3306 	}
   3307 	netbsd32_to_timeval(&tv32[0], &tv[0]);
   3308 	netbsd32_to_timeval(&tv32[1], &tv[1]);
   3309 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   3310 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3311 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   3312 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   3313 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   3314 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   3315 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   3316 	VOP_UNLOCK(vp, 0);
   3317 	return (error);
   3318 }
   3319 
   3320 int
   3321 netbsd32_adjtime(p, v, retval)
   3322 	struct proc *p;
   3323 	void *v;
   3324 	register_t *retval;
   3325 {
   3326 	struct netbsd32_adjtime_args /* {
   3327 		syscallarg(const netbsd32_timevalp_t) delta;
   3328 		syscallarg(netbsd32_timevalp_t) olddelta;
   3329 	} */ *uap = v;
   3330 	struct netbsd32_timeval atv;
   3331 	int32_t ndelta, ntickdelta, odelta;
   3332 	int s, error;
   3333 	extern long bigadj, timedelta;
   3334 	extern int tickdelta;
   3335 
   3336 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   3337 		return (error);
   3338 
   3339 	error = copyin((caddr_t)(u_long)SCARG(uap, delta), &atv, sizeof(struct timeval));
   3340 	if (error)
   3341 		return (error);
   3342 	/*
   3343 	 * Compute the total correction and the rate at which to apply it.
   3344 	 * Round the adjustment down to a whole multiple of the per-tick
   3345 	 * delta, so that after some number of incremental changes in
   3346 	 * hardclock(), tickdelta will become zero, lest the correction
   3347 	 * overshoot and start taking us away from the desired final time.
   3348 	 */
   3349 	ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
   3350 	if (ndelta > bigadj)
   3351 		ntickdelta = 10 * tickadj;
   3352 	else
   3353 		ntickdelta = tickadj;
   3354 	if (ndelta % ntickdelta)
   3355 		ndelta = ndelta / ntickdelta * ntickdelta;
   3356 
   3357 	/*
   3358 	 * To make hardclock()'s job easier, make the per-tick delta negative
   3359 	 * if we want time to run slower; then hardclock can simply compute
   3360 	 * tick + tickdelta, and subtract tickdelta from timedelta.
   3361 	 */
   3362 	if (ndelta < 0)
   3363 		ntickdelta = -ntickdelta;
   3364 	s = splclock();
   3365 	odelta = timedelta;
   3366 	timedelta = ndelta;
   3367 	tickdelta = ntickdelta;
   3368 	splx(s);
   3369 
   3370 	if (SCARG(uap, olddelta)) {
   3371 		atv.tv_sec = odelta / 1000000;
   3372 		atv.tv_usec = odelta % 1000000;
   3373 		(void) copyout(&atv, (caddr_t)(u_long)SCARG(uap, olddelta),
   3374 		    sizeof(struct timeval));
   3375 	}
   3376 	return (0);
   3377 }
   3378 
   3379 int
   3380 netbsd32_quotactl(p, v, retval)
   3381 	struct proc *p;
   3382 	void *v;
   3383 	register_t *retval;
   3384 {
   3385 	struct netbsd32_quotactl_args /* {
   3386 		syscallarg(const netbsd32_charp) path;
   3387 		syscallarg(int) cmd;
   3388 		syscallarg(int) uid;
   3389 		syscallarg(netbsd32_caddr_t) arg;
   3390 	} */ *uap = v;
   3391 	struct sys_quotactl_args ua;
   3392 
   3393 	NETBSD32TOP_UAP(path, const char);
   3394 	NETBSD32TO64_UAP(cmd);
   3395 	NETBSD32TO64_UAP(uid);
   3396 	NETBSD32TOX64_UAP(arg, caddr_t);
   3397 	return (sys_quotactl(p, &ua, retval));
   3398 }
   3399 
   3400 #if defined(NFS) || defined(NFSSERVER)
   3401 int
   3402 netbsd32_nfssvc(p, v, retval)
   3403 	struct proc *p;
   3404 	void *v;
   3405 	register_t *retval;
   3406 {
   3407 #if 0
   3408 	struct netbsd32_nfssvc_args /* {
   3409 		syscallarg(int) flag;
   3410 		syscallarg(netbsd32_voidp) argp;
   3411 	} */ *uap = v;
   3412 	struct sys_nfssvc_args ua;
   3413 
   3414 	NETBSD32TO64_UAP(flag);
   3415 	NETBSD32TOP_UAP(argp, void);
   3416 	return (sys_nfssvc(p, &ua, retval));
   3417 #else
   3418 	/* Why would we want to support a 32-bit nfsd? */
   3419 	return (ENOSYS);
   3420 #endif
   3421 }
   3422 #endif
   3423 
   3424 int
   3425 netbsd32_statfs(p, v, retval)
   3426 	struct proc *p;
   3427 	void *v;
   3428 	register_t *retval;
   3429 {
   3430 	struct netbsd32_statfs_args /* {
   3431 		syscallarg(const netbsd32_charp) path;
   3432 		syscallarg(netbsd32_statfsp_t) buf;
   3433 	} */ *uap = v;
   3434 	struct mount *mp;
   3435 	struct statfs *sp;
   3436 	struct netbsd32_statfs s32;
   3437 	int error;
   3438 	struct nameidata nd;
   3439 
   3440 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (char *)(u_long)SCARG(uap, path), p);
   3441 	if ((error = namei(&nd)) != 0)
   3442 		return (error);
   3443 	mp = nd.ni_vp->v_mount;
   3444 	sp = &mp->mnt_stat;
   3445 	vrele(nd.ni_vp);
   3446 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   3447 		return (error);
   3448 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   3449 	netbsd32_from_statfs(sp, &s32);
   3450 	return (copyout(&s32, (caddr_t)(u_long)SCARG(uap, buf), sizeof(s32)));
   3451 }
   3452 
   3453 int
   3454 netbsd32_fstatfs(p, v, retval)
   3455 	struct proc *p;
   3456 	void *v;
   3457 	register_t *retval;
   3458 {
   3459 	struct netbsd32_fstatfs_args /* {
   3460 		syscallarg(int) fd;
   3461 		syscallarg(netbsd32_statfsp_t) buf;
   3462 	} */ *uap = v;
   3463 	struct file *fp;
   3464 	struct mount *mp;
   3465 	struct statfs *sp;
   3466 	struct netbsd32_statfs s32;
   3467 	int error;
   3468 
   3469 	/* getvnode() will use the descriptor for us */
   3470 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3471 		return (error);
   3472 	mp = ((struct vnode *)fp->f_data)->v_mount;
   3473 	sp = &mp->mnt_stat;
   3474 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   3475 		goto out;
   3476 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   3477 	netbsd32_from_statfs(sp, &s32);
   3478 	error = copyout(&s32, (caddr_t)(u_long)SCARG(uap, buf), sizeof(s32));
   3479  out:
   3480 	FILE_UNUSE(fp, p);
   3481 	return (error);
   3482 }
   3483 
   3484 #if defined(NFS) || defined(NFSSERVER)
   3485 int
   3486 netbsd32_getfh(p, v, retval)
   3487 	struct proc *p;
   3488 	void *v;
   3489 	register_t *retval;
   3490 {
   3491 	struct netbsd32_getfh_args /* {
   3492 		syscallarg(const netbsd32_charp) fname;
   3493 		syscallarg(netbsd32_fhandlep_t) fhp;
   3494 	} */ *uap = v;
   3495 	struct sys_getfh_args ua;
   3496 
   3497 	NETBSD32TOP_UAP(fname, const char);
   3498 	NETBSD32TOP_UAP(fhp, struct fhandle);
   3499 	/* Lucky for us a fhandlep_t doesn't change sizes */
   3500 	return (sys_getfh(p, &ua, retval));
   3501 }
   3502 #endif
   3503 
   3504 int
   3505 netbsd32_sysarch(p, v, retval)
   3506 	struct proc *p;
   3507 	void *v;
   3508 	register_t *retval;
   3509 {
   3510 	struct netbsd32_sysarch_args /* {
   3511 		syscallarg(int) op;
   3512 		syscallarg(netbsd32_voidp) parms;
   3513 	} */ *uap = v;
   3514 
   3515 	switch (SCARG(uap, op)) {
   3516 	default:
   3517 		printf("(sparc64) netbsd32_sysarch(%d)\n", SCARG(uap, op));
   3518 		return EINVAL;
   3519 	}
   3520 }
   3521 
   3522 int
   3523 netbsd32_pread(p, v, retval)
   3524 	struct proc *p;
   3525 	void *v;
   3526 	register_t *retval;
   3527 {
   3528 	struct netbsd32_pread_args /* {
   3529 		syscallarg(int) fd;
   3530 		syscallarg(netbsd32_voidp) buf;
   3531 		syscallarg(netbsd32_size_t) nbyte;
   3532 		syscallarg(int) pad;
   3533 		syscallarg(off_t) offset;
   3534 	} */ *uap = v;
   3535 	struct sys_pread_args ua;
   3536 	ssize_t rt;
   3537 	int error;
   3538 
   3539 	NETBSD32TO64_UAP(fd);
   3540 	NETBSD32TOP_UAP(buf, void);
   3541 	NETBSD32TOX_UAP(nbyte, size_t);
   3542 	NETBSD32TO64_UAP(pad);
   3543 	NETBSD32TO64_UAP(offset);
   3544 	error = sys_pread(p, &ua, (register_t *)&rt);
   3545 	*(netbsd32_ssize_t *)retval = rt;
   3546 	return (error);
   3547 }
   3548 
   3549 int
   3550 netbsd32_pwrite(p, v, retval)
   3551 	struct proc *p;
   3552 	void *v;
   3553 	register_t *retval;
   3554 {
   3555 	struct netbsd32_pwrite_args /* {
   3556 		syscallarg(int) fd;
   3557 		syscallarg(const netbsd32_voidp) buf;
   3558 		syscallarg(netbsd32_size_t) nbyte;
   3559 		syscallarg(int) pad;
   3560 		syscallarg(off_t) offset;
   3561 	} */ *uap = v;
   3562 	struct sys_pwrite_args ua;
   3563 	ssize_t rt;
   3564 	int error;
   3565 
   3566 	NETBSD32TO64_UAP(fd);
   3567 	NETBSD32TOP_UAP(buf, void);
   3568 	NETBSD32TOX_UAP(nbyte, size_t);
   3569 	NETBSD32TO64_UAP(pad);
   3570 	NETBSD32TO64_UAP(offset);
   3571 	error = sys_pwrite(p, &ua, (register_t *)&rt);
   3572 	*(netbsd32_ssize_t *)retval = rt;
   3573 	return (error);
   3574 }
   3575 
   3576 #ifdef NTP
   3577 int
   3578 netbsd32_ntp_gettime(p, v, retval)
   3579 	struct proc *p;
   3580 	void *v;
   3581 	register_t *retval;
   3582 {
   3583 	struct netbsd32_ntp_gettime_args /* {
   3584 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
   3585 	} */ *uap = v;
   3586 	struct netbsd32_ntptimeval ntv32;
   3587 	struct timeval atv;
   3588 	struct ntptimeval ntv;
   3589 	int error = 0;
   3590 	int s;
   3591 
   3592 	/* The following are NTP variables */
   3593 	extern long time_maxerror;
   3594 	extern long time_esterror;
   3595 	extern int time_status;
   3596 	extern int time_state;	/* clock state */
   3597 	extern int time_status;	/* clock status bits */
   3598 
   3599 	if (SCARG(uap, ntvp)) {
   3600 		s = splclock();
   3601 #ifdef EXT_CLOCK
   3602 		/*
   3603 		 * The microtime() external clock routine returns a
   3604 		 * status code. If less than zero, we declare an error
   3605 		 * in the clock status word and return the kernel
   3606 		 * (software) time variable. While there are other
   3607 		 * places that call microtime(), this is the only place
   3608 		 * that matters from an application point of view.
   3609 		 */
   3610 		if (microtime(&atv) < 0) {
   3611 			time_status |= STA_CLOCKERR;
   3612 			ntv.time = time;
   3613 		} else
   3614 			time_status &= ~STA_CLOCKERR;
   3615 #else /* EXT_CLOCK */
   3616 		microtime(&atv);
   3617 #endif /* EXT_CLOCK */
   3618 		ntv.time = atv;
   3619 		ntv.maxerror = time_maxerror;
   3620 		ntv.esterror = time_esterror;
   3621 		(void) splx(s);
   3622 
   3623 		netbsd32_from_timeval(&ntv.time, &ntv32.time);
   3624 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
   3625 		ntv32.esterror = (netbsd32_long)ntv.esterror;
   3626 		error = copyout((caddr_t)&ntv32, (caddr_t)(u_long)SCARG(uap, ntvp),
   3627 		    sizeof(ntv32));
   3628 	}
   3629 	if (!error) {
   3630 
   3631 		/*
   3632 		 * Status word error decode. If any of these conditions
   3633 		 * occur, an error is returned, instead of the status
   3634 		 * word. Most applications will care only about the fact
   3635 		 * the system clock may not be trusted, not about the
   3636 		 * details.
   3637 		 *
   3638 		 * Hardware or software error
   3639 		 */
   3640 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   3641 
   3642 		/*
   3643 		 * PPS signal lost when either time or frequency
   3644 		 * synchronization requested
   3645 		 */
   3646 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   3647 		    !(time_status & STA_PPSSIGNAL)) ||
   3648 
   3649 		/*
   3650 		 * PPS jitter exceeded when time synchronization
   3651 		 * requested
   3652 		 */
   3653 		    (time_status & STA_PPSTIME &&
   3654 		    time_status & STA_PPSJITTER) ||
   3655 
   3656 		/*
   3657 		 * PPS wander exceeded or calibration error when
   3658 		 * frequency synchronization requested
   3659 		 */
   3660 		    (time_status & STA_PPSFREQ &&
   3661 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
   3662 			*retval = TIME_ERROR;
   3663 		else
   3664 			*retval = (register_t)time_state;
   3665 	}
   3666 	return(error);
   3667 }
   3668 
   3669 int
   3670 netbsd32_ntp_adjtime(p, v, retval)
   3671 	struct proc *p;
   3672 	void *v;
   3673 	register_t *retval;
   3674 {
   3675 	struct netbsd32_ntp_adjtime_args /* {
   3676 		syscallarg(netbsd32_timexp_t) tp;
   3677 	} */ *uap = v;
   3678 	struct netbsd32_timex ntv32;
   3679 	struct timex ntv;
   3680 	int error = 0;
   3681 	int modes;
   3682 	int s;
   3683 	extern long time_freq;		/* frequency offset (scaled ppm) */
   3684 	extern long time_maxerror;
   3685 	extern long time_esterror;
   3686 	extern int time_state;	/* clock state */
   3687 	extern int time_status;	/* clock status bits */
   3688 	extern long time_constant;		/* pll time constant */
   3689 	extern long time_offset;		/* time offset (us) */
   3690 	extern long time_tolerance;	/* frequency tolerance (scaled ppm) */
   3691 	extern long time_precision;	/* clock precision (us) */
   3692 
   3693 	if ((error = copyin((caddr_t)(u_long)SCARG(uap, tp), (caddr_t)&ntv32,
   3694 			sizeof(ntv32))))
   3695 		return (error);
   3696 	netbsd32_to_timex(&ntv32, &ntv);
   3697 
   3698 	/*
   3699 	 * Update selected clock variables - only the superuser can
   3700 	 * change anything. Note that there is no error checking here on
   3701 	 * the assumption the superuser should know what it is doing.
   3702 	 */
   3703 	modes = ntv.modes;
   3704 	if (modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)))
   3705 		return (error);
   3706 
   3707 	s = splclock();
   3708 	if (modes & MOD_FREQUENCY)
   3709 #ifdef PPS_SYNC
   3710 		time_freq = ntv.freq - pps_freq;
   3711 #else /* PPS_SYNC */
   3712 		time_freq = ntv.freq;
   3713 #endif /* PPS_SYNC */
   3714 	if (modes & MOD_MAXERROR)
   3715 		time_maxerror = ntv.maxerror;
   3716 	if (modes & MOD_ESTERROR)
   3717 		time_esterror = ntv.esterror;
   3718 	if (modes & MOD_STATUS) {
   3719 		time_status &= STA_RONLY;
   3720 		time_status |= ntv.status & ~STA_RONLY;
   3721 	}
   3722 	if (modes & MOD_TIMECONST)
   3723 		time_constant = ntv.constant;
   3724 	if (modes & MOD_OFFSET)
   3725 		hardupdate(ntv.offset);
   3726 
   3727 	/*
   3728 	 * Retrieve all clock variables
   3729 	 */
   3730 	if (time_offset < 0)
   3731 		ntv.offset = -(-time_offset >> SHIFT_UPDATE);
   3732 	else
   3733 		ntv.offset = time_offset >> SHIFT_UPDATE;
   3734 #ifdef PPS_SYNC
   3735 	ntv.freq = time_freq + pps_freq;
   3736 #else /* PPS_SYNC */
   3737 	ntv.freq = time_freq;
   3738 #endif /* PPS_SYNC */
   3739 	ntv.maxerror = time_maxerror;
   3740 	ntv.esterror = time_esterror;
   3741 	ntv.status = time_status;
   3742 	ntv.constant = time_constant;
   3743 	ntv.precision = time_precision;
   3744 	ntv.tolerance = time_tolerance;
   3745 #ifdef PPS_SYNC
   3746 	ntv.shift = pps_shift;
   3747 	ntv.ppsfreq = pps_freq;
   3748 	ntv.jitter = pps_jitter >> PPS_AVG;
   3749 	ntv.stabil = pps_stabil;
   3750 	ntv.calcnt = pps_calcnt;
   3751 	ntv.errcnt = pps_errcnt;
   3752 	ntv.jitcnt = pps_jitcnt;
   3753 	ntv.stbcnt = pps_stbcnt;
   3754 #endif /* PPS_SYNC */
   3755 	(void)splx(s);
   3756 
   3757 	netbsd32_from_timeval(&ntv, &ntv32);
   3758 	error = copyout((caddr_t)&ntv32, (caddr_t)SCARG(uap, tp), sizeof(ntv32));
   3759 	if (!error) {
   3760 
   3761 		/*
   3762 		 * Status word error decode. See comments in
   3763 		 * ntp_gettime() routine.
   3764 		 */
   3765 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
   3766 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
   3767 		    !(time_status & STA_PPSSIGNAL)) ||
   3768 		    (time_status & STA_PPSTIME &&
   3769 		    time_status & STA_PPSJITTER) ||
   3770 		    (time_status & STA_PPSFREQ &&
   3771 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
   3772 			*retval = TIME_ERROR;
   3773 		else
   3774 			*retval = (register_t)time_state;
   3775 	}
   3776 	return error;
   3777 }
   3778 #endif
   3779 
   3780 int
   3781 netbsd32_setgid(p, v, retval)
   3782 	struct proc *p;
   3783 	void *v;
   3784 	register_t *retval;
   3785 {
   3786 	struct netbsd32_setgid_args /* {
   3787 		syscallarg(gid_t) gid;
   3788 	} */ *uap = v;
   3789 	struct sys_setgid_args ua;
   3790 
   3791 	NETBSD32TO64_UAP(gid);
   3792 	return (sys_setgid(p, v, retval));
   3793 }
   3794 
   3795 int
   3796 netbsd32_setegid(p, v, retval)
   3797 	struct proc *p;
   3798 	void *v;
   3799 	register_t *retval;
   3800 {
   3801 	struct netbsd32_setegid_args /* {
   3802 		syscallarg(gid_t) egid;
   3803 	} */ *uap = v;
   3804 	struct sys_setegid_args ua;
   3805 
   3806 	NETBSD32TO64_UAP(egid);
   3807 	return (sys_setegid(p, v, retval));
   3808 }
   3809 
   3810 int
   3811 netbsd32_seteuid(p, v, retval)
   3812 	struct proc *p;
   3813 	void *v;
   3814 	register_t *retval;
   3815 {
   3816 	struct netbsd32_seteuid_args /* {
   3817 		syscallarg(gid_t) euid;
   3818 	} */ *uap = v;
   3819 	struct sys_seteuid_args ua;
   3820 
   3821 	NETBSD32TO64_UAP(euid);
   3822 	return (sys_seteuid(p, v, retval));
   3823 }
   3824 
   3825 #ifdef LFS
   3826 int
   3827 netbsd32_sys_lfs_bmapv(p, v, retval)
   3828 	struct proc *p;
   3829 	void *v;
   3830 	register_t *retval;
   3831 {
   3832 #if 0
   3833 	struct netbsd32_lfs_bmapv_args /* {
   3834 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3835 		syscallarg(netbsd32_block_infop_t) blkiov;
   3836 		syscallarg(int) blkcnt;
   3837 	} */ *uap = v;
   3838 	struct sys_lfs_bmapv_args ua;
   3839 
   3840 	NETBSD32TOP_UAP(fdidp, struct fsid);
   3841 	NETBSD32TO64_UAP(blkcnt);
   3842 	/* XXX finish me */
   3843 #else
   3844 
   3845 	return (ENOSYS);	/* XXX */
   3846 #endif
   3847 }
   3848 
   3849 int
   3850 netbsd32_sys_lfs_markv(p, v, retval)
   3851 	struct proc *p;
   3852 	void *v;
   3853 	register_t *retval;
   3854 {
   3855 #if 0
   3856 	struct netbsd32_lfs_markv_args /* {
   3857 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3858 		syscallarg(netbsd32_block_infop_t) blkiov;
   3859 		syscallarg(int) blkcnt;
   3860 	} */ *uap = v;
   3861 #endif
   3862 
   3863 	return (ENOSYS);	/* XXX */
   3864 }
   3865 
   3866 int
   3867 netbsd32_sys_lfs_segclean(p, v, retval)
   3868 	struct proc *p;
   3869 	void *v;
   3870 	register_t *retval;
   3871 {
   3872 #if 0
   3873 	struct netbsd32_lfs_segclean_args /* {
   3874 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3875 		syscallarg(netbsd32_u_long) segment;
   3876 	} */ *uap = v;
   3877 #endif
   3878 
   3879 	return (ENOSYS);	/* XXX */
   3880 }
   3881 
   3882 int
   3883 netbsd32_sys_lfs_segwait(p, v, retval)
   3884 	struct proc *p;
   3885 	void *v;
   3886 	register_t *retval;
   3887 {
   3888 #if 0
   3889 	struct netbsd32_lfs_segwait_args /* {
   3890 		syscallarg(netbsd32_fsid_tp_t) fsidp;
   3891 		syscallarg(netbsd32_timevalp_t) tv;
   3892 	} */ *uap = v;
   3893 #endif
   3894 
   3895 	return (ENOSYS);	/* XXX */
   3896 }
   3897 #endif
   3898 
   3899 int
   3900 netbsd32_pathconf(p, v, retval)
   3901 	struct proc *p;
   3902 	void *v;
   3903 	register_t *retval;
   3904 {
   3905 	struct netbsd32_pathconf_args /* {
   3906 		syscallarg(int) fd;
   3907 		syscallarg(int) name;
   3908 	} */ *uap = v;
   3909 	struct sys_pathconf_args ua;
   3910 	long rt;
   3911 	int error;
   3912 
   3913 	NETBSD32TOP_UAP(path, const char);
   3914 	NETBSD32TO64_UAP(name);
   3915 	error = sys_pathconf(p, &ua, (register_t *)&rt);
   3916 	*(netbsd32_long *)retval = (netbsd32_long)rt;
   3917 	return (error);
   3918 }
   3919 
   3920 int
   3921 netbsd32_fpathconf(p, v, retval)
   3922 	struct proc *p;
   3923 	void *v;
   3924 	register_t *retval;
   3925 {
   3926 	struct netbsd32_fpathconf_args /* {
   3927 		syscallarg(int) fd;
   3928 		syscallarg(int) name;
   3929 	} */ *uap = v;
   3930 	struct sys_fpathconf_args ua;
   3931 	long rt;
   3932 	int error;
   3933 
   3934 	NETBSD32TO64_UAP(fd);
   3935 	NETBSD32TO64_UAP(name);
   3936 	error = sys_fpathconf(p, &ua, (register_t *)&rt);
   3937 	*(netbsd32_long *)retval = (netbsd32_long)rt;
   3938 	return (error);
   3939 }
   3940 
   3941 int
   3942 netbsd32_getrlimit(p, v, retval)
   3943 	struct proc *p;
   3944 	void *v;
   3945 	register_t *retval;
   3946 {
   3947 	struct netbsd32_getrlimit_args /* {
   3948 		syscallarg(int) which;
   3949 		syscallarg(netbsd32_rlimitp_t) rlp;
   3950 	} */ *uap = v;
   3951 	int which = SCARG(uap, which);
   3952 
   3953 	if ((u_int)which >= RLIM_NLIMITS)
   3954 		return (EINVAL);
   3955 	return (copyout(&p->p_rlimit[which], (caddr_t)(u_long)SCARG(uap, rlp),
   3956 	    sizeof(struct rlimit)));
   3957 }
   3958 
   3959 int
   3960 netbsd32_setrlimit(p, v, retval)
   3961 	struct proc *p;
   3962 	void *v;
   3963 	register_t *retval;
   3964 {
   3965 	struct netbsd32_setrlimit_args /* {
   3966 		syscallarg(int) which;
   3967 		syscallarg(const netbsd32_rlimitp_t) rlp;
   3968 	} */ *uap = v;
   3969 		int which = SCARG(uap, which);
   3970 	struct rlimit alim;
   3971 	int error;
   3972 
   3973 	error = copyin((caddr_t)(u_long)SCARG(uap, rlp), &alim, sizeof(struct rlimit));
   3974 	if (error)
   3975 		return (error);
   3976 	return (dosetrlimit(p, p->p_cred, which, &alim));
   3977 }
   3978 
   3979 int
   3980 netbsd32_mmap(p, v, retval)
   3981 	struct proc *p;
   3982 	void *v;
   3983 	register_t *retval;
   3984 {
   3985 	struct netbsd32_mmap_args /* {
   3986 		syscallarg(netbsd32_voidp) addr;
   3987 		syscallarg(netbsd32_size_t) len;
   3988 		syscallarg(int) prot;
   3989 		syscallarg(int) flags;
   3990 		syscallarg(int) fd;
   3991 		syscallarg(netbsd32_long) pad;
   3992 		syscallarg(off_t) pos;
   3993 	} */ *uap = v;
   3994 	struct sys_mmap_args ua;
   3995 	void *rt;
   3996 	int error;
   3997 
   3998 	NETBSD32TOP_UAP(addr, void);
   3999 	NETBSD32TOX_UAP(len, size_t);
   4000 	NETBSD32TO64_UAP(prot);
   4001 	NETBSD32TO64_UAP(flags);
   4002 	NETBSD32TO64_UAP(fd);
   4003 	NETBSD32TOX_UAP(pad, long);
   4004 	NETBSD32TOX_UAP(pos, off_t);
   4005 	error = sys_mmap(p, &ua, (register_t *)&rt);
   4006 	if ((long)rt > (long)UINT_MAX)
   4007 		printf("netbsd32_mmap: retval out of range: 0x%qx",
   4008 		    rt);
   4009 	*retval = (netbsd32_voidp)(u_long)rt;
   4010 	return (error);
   4011 }
   4012 
   4013 int
   4014 netbsd32_lseek(p, v, retval)
   4015 	struct proc *p;
   4016 	void *v;
   4017 	register_t *retval;
   4018 {
   4019 	struct netbsd32_lseek_args /* {
   4020 		syscallarg(int) fd;
   4021 		syscallarg(int) pad;
   4022 		syscallarg(off_t) offset;
   4023 		syscallarg(int) whence;
   4024 	} */ *uap = v;
   4025 	struct sys_lseek_args ua;
   4026 
   4027 	NETBSD32TO64_UAP(fd);
   4028 	NETBSD32TO64_UAP(pad);
   4029 	NETBSD32TO64_UAP(offset);
   4030 	NETBSD32TO64_UAP(whence);
   4031 	return (sys_lseek(p, &ua, retval));
   4032 }
   4033 
   4034 int
   4035 netbsd32_truncate(p, v, retval)
   4036 	struct proc *p;
   4037 	void *v;
   4038 	register_t *retval;
   4039 {
   4040 	struct netbsd32_truncate_args /* {
   4041 		syscallarg(const netbsd32_charp) path;
   4042 		syscallarg(int) pad;
   4043 		syscallarg(off_t) length;
   4044 	} */ *uap = v;
   4045 	struct sys_truncate_args ua;
   4046 
   4047 	NETBSD32TOP_UAP(path, const char);
   4048 	NETBSD32TO64_UAP(pad);
   4049 	NETBSD32TO64_UAP(length);
   4050 	return (sys_truncate(p, &ua, retval));
   4051 }
   4052 
   4053 int
   4054 netbsd32_ftruncate(p, v, retval)
   4055 	struct proc *p;
   4056 	void *v;
   4057 	register_t *retval;
   4058 {
   4059 	struct netbsd32_ftruncate_args /* {
   4060 		syscallarg(int) fd;
   4061 		syscallarg(int) pad;
   4062 		syscallarg(off_t) length;
   4063 	} */ *uap = v;
   4064 	struct sys_ftruncate_args ua;
   4065 
   4066 	NETBSD32TO64_UAP(fd);
   4067 	NETBSD32TO64_UAP(pad);
   4068 	NETBSD32TO64_UAP(length);
   4069 	return (sys_ftruncate(p, &ua, retval));
   4070 }
   4071 
   4072 int
   4073 netbsd32___sysctl(p, v, retval)
   4074 	struct proc *p;
   4075 	void *v;
   4076 	register_t *retval;
   4077 {
   4078 	struct netbsd32___sysctl_args /* {
   4079 		syscallarg(netbsd32_intp) name;
   4080 		syscallarg(u_int) namelen;
   4081 		syscallarg(netbsd32_voidp) old;
   4082 		syscallarg(netbsd32_size_tp) oldlenp;
   4083 		syscallarg(netbsd32_voidp) new;
   4084 		syscallarg(netbsd32_size_t) newlen;
   4085 	} */ *uap = v;
   4086 	int error, dolock = 1;
   4087 	netbsd32_size_t savelen = 0;
   4088 	size_t oldlen = 0;
   4089 	sysctlfn *fn;
   4090 	int name[CTL_MAXNAME];
   4091 
   4092 /*
   4093  * Some of these sysctl functions do their own copyin/copyout.
   4094  * We need to disable or emulate the ones that need their
   4095  * arguments converted.
   4096  */
   4097 
   4098 	if (SCARG(uap, new) != NULL &&
   4099 	    (error = suser(p->p_ucred, &p->p_acflag)))
   4100 		return (error);
   4101 	/*
   4102 	 * all top-level sysctl names are non-terminal
   4103 	 */
   4104 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
   4105 		return (EINVAL);
   4106 	error = copyin((caddr_t)(u_long)SCARG(uap, name), &name,
   4107 		       SCARG(uap, namelen) * sizeof(int));
   4108 	if (error)
   4109 		return (error);
   4110 
   4111 	switch (name[0]) {
   4112 	case CTL_KERN:
   4113 		fn = kern_sysctl;
   4114 		if (name[2] != KERN_VNODE)	/* XXX */
   4115 			dolock = 0;
   4116 		break;
   4117 	case CTL_HW:
   4118 		fn = hw_sysctl;
   4119 		break;
   4120 	case CTL_VM:
   4121 		fn = uvm_sysctl;
   4122 		break;
   4123 	case CTL_NET:
   4124 		fn = net_sysctl;
   4125 		break;
   4126 	case CTL_VFS:
   4127 		fn = vfs_sysctl;
   4128 		break;
   4129 	case CTL_MACHDEP:
   4130 		fn = cpu_sysctl;
   4131 		break;
   4132 #ifdef DEBUG
   4133 	case CTL_DEBUG:
   4134 		fn = debug_sysctl;
   4135 		break;
   4136 #endif
   4137 #ifdef DDB
   4138 	case CTL_DDB:
   4139 		fn = ddb_sysctl;
   4140 		break;
   4141 #endif
   4142 	default:
   4143 		return (EOPNOTSUPP);
   4144 	}
   4145 
   4146 	if (SCARG(uap, oldlenp) &&
   4147 	    (error = copyin((caddr_t)(u_long)SCARG(uap, oldlenp), &savelen, sizeof(savelen))))
   4148 		return (error);
   4149 	if (SCARG(uap, old) != NULL) {
   4150 		if (!uvm_useracc((caddr_t)(u_long)SCARG(uap, old), savelen, B_WRITE))
   4151 			return (EFAULT);
   4152 #if 0 /* XXXXXXXX */
   4153 		while (memlock.sl_lock) {
   4154 			memlock.sl_want = 1;
   4155 			(void) tsleep(&memlock, PRIBIO+1, "memlock", 0);
   4156 			memlock.sl_locked++;
   4157 		}
   4158 		memlock.sl_lock = 1;
   4159 #endif /* XXXXXXXX */
   4160 		if (dolock) {
   4161 			/*
   4162 			 * XXX Um, this is kind of evil.  What should
   4163 			 * XXX we be passing here?
   4164 			 */
   4165 			if (uvm_vslock(p, (void *)(u_long)SCARG(uap, old), savelen,
   4166 			    VM_PROT_NONE) != KERN_SUCCESS) {
   4167 #if 0 /* XXXXXXXX */
   4168 				memlock.sl_lock = 0;
   4169 				if (memlock.sl_want) {
   4170 					memlock.sl_want = 0;
   4171 					wakeup((caddr_t)&memlock);
   4172 				}
   4173 #endif /* XXXXXXXX */
   4174 				return (EFAULT);
   4175 			}
   4176 		}
   4177 		oldlen = savelen;
   4178 	}
   4179 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1,
   4180 		      (void *)(u_long)SCARG(uap, old), &oldlen,
   4181 		      (void *)(u_long)SCARG(uap, new), SCARG(uap, newlen), p);
   4182 	if (SCARG(uap, old) != NULL) {
   4183 		if (dolock)
   4184 			uvm_vsunlock(p, (void *)(u_long)SCARG(uap, old), savelen);
   4185 #if 0 /* XXXXXXXXXXX */
   4186 		memlock.sl_lock = 0;
   4187 		if (memlock.sl_want) {
   4188 			memlock.sl_want = 0;
   4189 			wakeup((caddr_t)&memlock);
   4190 		}
   4191 #endif /* XXXXXXXXX */
   4192 	}
   4193 	savelen = oldlen;
   4194 	if (error)
   4195 		return (error);
   4196 	if (SCARG(uap, oldlenp))
   4197 		error = copyout(&savelen, (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