Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_netbsd.c revision 1.28.2.1
      1 /*	$NetBSD: netbsd32_netbsd.c,v 1.28.2.1 2000/07/13 20:20:27 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include "opt_ddb.h"
     32 #include "opt_ktrace.h"
     33 #include "opt_ntp.h"
     34 #include "opt_compat_netbsd.h"
     35 #include "opt_compat_freebsd.h"
     36 #include "opt_compat_linux.h"
     37 #include "opt_compat_sunos.h"
     38 #include "opt_compat_43.h"
     39 #include "opt_sysv.h"
     40 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_LINUX) || \
     41     defined(COMPAT_FREEBSD)
     42 #define COMPAT_OLDSOCK /* used by <sys/socket.h> */
     43 #endif
     44 
     45 #include "fs_lfs.h"
     46 #include "fs_nfs.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/filedesc.h>
     51 #include <sys/kernel.h>
     52 #include <sys/ipc.h>
     53 #include <sys/msg.h>
     54 #define msg __msg /* Don't ask me! */
     55 #include <sys/sem.h>
     56 #include <sys/shm.h>
     57 #include <sys/malloc.h>
     58 #include <sys/mount.h>
     59 #include <sys/socket.h>
     60 #include <sys/sockio.h>
     61 #include <sys/socketvar.h>
     62 #include <sys/mbuf.h>
     63 #include <sys/stat.h>
     64 #include <sys/time.h>
     65 #include <sys/timex.h>
     66 #include <sys/signalvar.h>
     67 #include <sys/wait.h>
     68 #include <sys/ptrace.h>
     69 #include <sys/ktrace.h>
     70 #include <sys/trace.h>
     71 #include <sys/resourcevar.h>
     72 #include <sys/pool.h>
     73 #include <sys/vnode.h>
     74 #include <sys/file.h>
     75 #include <sys/filedesc.h>
     76 #include <sys/namei.h>
     77 
     78 #include <vm/vm.h>
     79 #include <vm/vm_kern.h>
     80 #include <sys/syscallargs.h>
     81 #include <sys/proc.h>
     82 #include <sys/acct.h>
     83 #include <sys/exec.h>
     84 #include <sys/sysctl.h>
     85 
     86 #include <net/if.h>
     87 
     88 #include <compat/netbsd32/netbsd32.h>
     89 #include <compat/netbsd32/netbsd32_syscallargs.h>
     90 
     91 #include <machine/frame.h>
     92 
     93 #if defined(DDB)
     94 #include <ddb/ddbvar.h>
     95 #endif
     96 
     97 static __inline void netbsd32_from_timeval __P((struct timeval *, struct netbsd32_timeval *));
     98 static __inline void netbsd32_to_timeval __P((struct netbsd32_timeval *, struct timeval *));
     99 static __inline void netbsd32_from_itimerval __P((struct itimerval *, struct netbsd32_itimerval *));
    100 static __inline void netbsd32_to_itimerval __P((struct netbsd32_itimerval *, struct itimerval *));
    101 static __inline void netbsd32_to_timespec __P((struct netbsd32_timespec *, struct timespec *));
    102 static __inline void netbsd32_from_timespec __P((struct timespec *, struct netbsd32_timespec *));
    103 static __inline void netbsd32_from_rusage __P((struct rusage *, struct netbsd32_rusage *));
    104 static __inline void netbsd32_to_rusage __P((struct netbsd32_rusage *, struct rusage *));
    105 static __inline int netbsd32_to_iovecin __P((struct netbsd32_iovec *, struct iovec *, int));
    106 static __inline void netbsd32_to_msghdr __P((struct netbsd32_msghdr *, struct msghdr *));
    107 static __inline void netbsd32_from_msghdr __P((struct netbsd32_msghdr *, struct msghdr *));
    108 static __inline void netbsd32_from_statfs __P((struct statfs *, struct netbsd32_statfs *));
    109 static __inline void netbsd32_from_timex __P((struct timex *, struct netbsd32_timex *));
    110 static __inline void netbsd32_to_timex __P((struct netbsd32_timex *, struct timex *));
    111 static __inline void netbsd32_from___stat13 __P((struct stat *, struct netbsd32_stat *));
    112 static __inline void netbsd32_to_ipc_perm __P((struct netbsd32_ipc_perm *, struct ipc_perm *));
    113 static __inline void netbsd32_from_ipc_perm __P((struct ipc_perm *, struct netbsd32_ipc_perm *));
    114 static __inline void netbsd32_to_msg __P((struct netbsd32_msg *, struct msg *));
    115 static __inline void netbsd32_from_msg __P((struct msg *, struct netbsd32_msg *));
    116 static __inline void netbsd32_to_msqid_ds __P((struct netbsd32_msqid_ds *, struct msqid_ds *));
    117 static __inline void netbsd32_from_msqid_ds __P((struct msqid_ds *, struct netbsd32_msqid_ds *));
    118 static __inline void netbsd32_to_shmid_ds __P((struct netbsd32_shmid_ds *, struct shmid_ds *));
    119 static __inline void netbsd32_from_shmid_ds __P((struct shmid_ds *, struct netbsd32_shmid_ds *));
    120 static __inline void netbsd32_to_semid_ds __P((struct  netbsd32_semid_ds *, struct  semid_ds *));
    121 static __inline void netbsd32_from_semid_ds __P((struct  semid_ds *, struct  netbsd32_semid_ds *));
    122 
    123 
    124 static int recvit32 __P((struct proc *, int, struct netbsd32_msghdr *, struct iovec *, caddr_t,
    125 			 register_t *));
    126 static int dofilereadv32 __P((struct proc *, int, struct file *, struct netbsd32_iovec *,
    127 			      int, off_t *, int, register_t *));
    128 static int dofilewritev32 __P((struct proc *, int, struct file *, struct netbsd32_iovec *,
    129 			       int,  off_t *, int, register_t *));
    130 static int change_utimes32 __P((struct vnode *, struct timeval *, struct proc *));
    131 
    132 /* converters for structures that we need */
    133 static __inline void
    134 netbsd32_from_timeval(tv, tv32)
    135 	struct timeval *tv;
    136 	struct netbsd32_timeval *tv32;
    137 {
    138 
    139 	tv32->tv_sec = (netbsd32_long)tv->tv_sec;
    140 	tv32->tv_usec = (netbsd32_long)tv->tv_usec;
    141 }
    142 
    143 static __inline void
    144 netbsd32_to_timeval(tv32, tv)
    145 	struct netbsd32_timeval *tv32;
    146 	struct timeval *tv;
    147 {
    148 
    149 	tv->tv_sec = (long)tv32->tv_sec;
    150 	tv->tv_usec = (long)tv32->tv_usec;
    151 }
    152 
    153 static __inline void
    154 netbsd32_from_itimerval(itv, itv32)
    155 	struct itimerval *itv;
    156 	struct netbsd32_itimerval *itv32;
    157 {
    158 
    159 	netbsd32_from_timeval(&itv->it_interval,
    160 			     &itv32->it_interval);
    161 	netbsd32_from_timeval(&itv->it_value,
    162 			     &itv32->it_value);
    163 }
    164 
    165 static __inline void
    166 netbsd32_to_itimerval(itv32, itv)
    167 	struct netbsd32_itimerval *itv32;
    168 	struct itimerval *itv;
    169 {
    170 
    171 	netbsd32_to_timeval(&itv32->it_interval, &itv->it_interval);
    172 	netbsd32_to_timeval(&itv32->it_value, &itv->it_value);
    173 }
    174 
    175 static __inline void
    176 netbsd32_to_timespec(s32p, p)
    177 	struct netbsd32_timespec *s32p;
    178 	struct timespec *p;
    179 {
    180 
    181 	p->tv_sec = (time_t)s32p->tv_sec;
    182 	p->tv_nsec = (long)s32p->tv_nsec;
    183 }
    184 
    185 static __inline void
    186 netbsd32_from_timespec(p, s32p)
    187 	struct timespec *p;
    188 	struct netbsd32_timespec *s32p;
    189 {
    190 
    191 	s32p->tv_sec = (netbsd32_time_t)p->tv_sec;
    192 	s32p->tv_nsec = (netbsd32_long)p->tv_nsec;
    193 }
    194 
    195 static __inline void
    196 netbsd32_from_rusage(rup, ru32p)
    197 	struct rusage *rup;
    198 	struct netbsd32_rusage *ru32p;
    199 {
    200 
    201 	netbsd32_from_timeval(&rup->ru_utime, &ru32p->ru_utime);
    202 	netbsd32_from_timeval(&rup->ru_stime, &ru32p->ru_stime);
    203 #define C(var)	ru32p->var = (netbsd32_long)rup->var
    204 	C(ru_maxrss);
    205 	C(ru_ixrss);
    206 	C(ru_idrss);
    207 	C(ru_isrss);
    208 	C(ru_minflt);
    209 	C(ru_majflt);
    210 	C(ru_nswap);
    211 	C(ru_inblock);
    212 	C(ru_oublock);
    213 	C(ru_msgsnd);
    214 	C(ru_msgrcv);
    215 	C(ru_nsignals);
    216 	C(ru_nvcsw);
    217 	C(ru_nivcsw);
    218 #undef C
    219 }
    220 
    221 static __inline void
    222 netbsd32_to_rusage(ru32p, rup)
    223 	struct netbsd32_rusage *ru32p;
    224 	struct rusage *rup;
    225 {
    226 
    227 	netbsd32_to_timeval(&ru32p->ru_utime, &rup->ru_utime);
    228 	netbsd32_to_timeval(&ru32p->ru_stime, &rup->ru_stime);
    229 #define C(var)	rup->var = (long)ru32p->var
    230 	C(ru_maxrss);
    231 	C(ru_ixrss);
    232 	C(ru_idrss);
    233 	C(ru_isrss);
    234 	C(ru_minflt);
    235 	C(ru_majflt);
    236 	C(ru_nswap);
    237 	C(ru_inblock);
    238 	C(ru_oublock);
    239 	C(ru_msgsnd);
    240 	C(ru_msgrcv);
    241 	C(ru_nsignals);
    242 	C(ru_nvcsw);
    243 	C(ru_nivcsw);
    244 #undef C
    245 }
    246 
    247 static __inline int
    248 netbsd32_to_iovecin(iov32p, iovp, len)
    249 	struct netbsd32_iovec *iov32p;
    250 	struct iovec *iovp;
    251 	int len;
    252 {
    253 	int i, error=0;
    254 	u_int32_t iov_base;
    255 	u_int32_t iov_len;
    256 	/*
    257 	 * We could allocate an iov32p, do a copyin, and translate
    258 	 * each field and then free it all up, or we could copyin
    259 	 * each field separately.  I'm doing the latter to reduce
    260 	 * the number of MALLOC()s.
    261 	 */
    262 	for (i = 0; i < len; i++, iovp++, iov32p++) {
    263 		if ((error = copyin((caddr_t)&iov32p->iov_base, &iov_base, sizeof(iov_base))))
    264 		    return (error);
    265 		if ((error = copyin((caddr_t)&iov32p->iov_len, &iov_len, sizeof(iov_len))))
    266 		    return (error);
    267 		iovp->iov_base = (void *)(u_long)iov_base;
    268 		iovp->iov_len = (size_t)iov_len;
    269 	}
    270 }
    271 
    272 /* msg_iov must be done separately */
    273 static __inline void
    274 netbsd32_to_msghdr(mhp32, mhp)
    275 	struct netbsd32_msghdr *mhp32;
    276 	struct msghdr *mhp;
    277 {
    278 
    279 	mhp->msg_name = (caddr_t)(u_long)mhp32->msg_name;
    280 	mhp->msg_namelen = mhp32->msg_namelen;
    281 	mhp->msg_iovlen = (size_t)mhp32->msg_iovlen;
    282 	mhp->msg_control = (caddr_t)(u_long)mhp32->msg_control;
    283 	mhp->msg_controllen = mhp32->msg_controllen;
    284 	mhp->msg_flags = mhp32->msg_flags;
    285 }
    286 
    287 /* msg_iov must be done separately */
    288 static __inline void
    289 netbsd32_from_msghdr(mhp32, mhp)
    290 	struct netbsd32_msghdr *mhp32;
    291 	struct msghdr *mhp;
    292 {
    293 
    294 	mhp32->msg_name = mhp32->msg_name;
    295 	mhp32->msg_namelen = mhp32->msg_namelen;
    296 	mhp32->msg_iovlen = mhp32->msg_iovlen;
    297 	mhp32->msg_control = mhp32->msg_control;
    298 	mhp32->msg_controllen = mhp->msg_controllen;
    299 	mhp32->msg_flags = mhp->msg_flags;
    300 }
    301 
    302 static __inline void
    303 netbsd32_from_statfs(sbp, sb32p)
    304 	struct statfs *sbp;
    305 	struct netbsd32_statfs *sb32p;
    306 {
    307 	sb32p->f_type = sbp->f_type;
    308 	sb32p->f_flags = sbp->f_flags;
    309 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
    310 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
    311 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
    312 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
    313 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
    314 	sb32p->f_files = (netbsd32_long)sbp->f_files;
    315 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
    316 	sb32p->f_fsid = sbp->f_fsid;
    317 	sb32p->f_owner = sbp->f_owner;
    318 	sb32p->f_spare[0] = 0;
    319 	sb32p->f_spare[1] = 0;
    320 	sb32p->f_spare[2] = 0;
    321 	sb32p->f_spare[3] = 0;
    322 #if 1
    323 	/* May as well do the whole batch in one go */
    324 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN+MNAMELEN+MNAMELEN);
    325 #else
    326 	/* If we want to be careful */
    327 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN);
    328 	memcpy(sb32p->f_mntonname, sbp->f_mntonname, MNAMELEN);
    329 	memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    330 #endif
    331 }
    332 
    333 static __inline void
    334 netbsd32_from_timex(txp, tx32p)
    335 	struct timex *txp;
    336 	struct netbsd32_timex *tx32p;
    337 {
    338 
    339 	tx32p->modes = txp->modes;
    340 	tx32p->offset = (netbsd32_long)txp->offset;
    341 	tx32p->freq = (netbsd32_long)txp->freq;
    342 	tx32p->maxerror = (netbsd32_long)txp->maxerror;
    343 	tx32p->esterror = (netbsd32_long)txp->esterror;
    344 	tx32p->status = txp->status;
    345 	tx32p->constant = (netbsd32_long)txp->constant;
    346 	tx32p->precision = (netbsd32_long)txp->precision;
    347 	tx32p->tolerance = (netbsd32_long)txp->tolerance;
    348 	tx32p->ppsfreq = (netbsd32_long)txp->ppsfreq;
    349 	tx32p->jitter = (netbsd32_long)txp->jitter;
    350 	tx32p->shift = txp->shift;
    351 	tx32p->stabil = (netbsd32_long)txp->stabil;
    352 	tx32p->jitcnt = (netbsd32_long)txp->jitcnt;
    353 	tx32p->calcnt = (netbsd32_long)txp->calcnt;
    354 	tx32p->errcnt = (netbsd32_long)txp->errcnt;
    355 	tx32p->stbcnt = (netbsd32_long)txp->stbcnt;
    356 }
    357 
    358 static __inline void
    359 netbsd32_to_timex(tx32p, txp)
    360 	struct netbsd32_timex *tx32p;
    361 	struct timex *txp;
    362 {
    363 
    364 	txp->modes = tx32p->modes;
    365 	txp->offset = (long)tx32p->offset;
    366 	txp->freq = (long)tx32p->freq;
    367 	txp->maxerror = (long)tx32p->maxerror;
    368 	txp->esterror = (long)tx32p->esterror;
    369 	txp->status = tx32p->status;
    370 	txp->constant = (long)tx32p->constant;
    371 	txp->precision = (long)tx32p->precision;
    372 	txp->tolerance = (long)tx32p->tolerance;
    373 	txp->ppsfreq = (long)tx32p->ppsfreq;
    374 	txp->jitter = (long)tx32p->jitter;
    375 	txp->shift = tx32p->shift;
    376 	txp->stabil = (long)tx32p->stabil;
    377 	txp->jitcnt = (long)tx32p->jitcnt;
    378 	txp->calcnt = (long)tx32p->calcnt;
    379 	txp->errcnt = (long)tx32p->errcnt;
    380 	txp->stbcnt = (long)tx32p->stbcnt;
    381 }
    382 
    383 static __inline void
    384 netbsd32_from___stat13(sbp, sb32p)
    385 	struct stat *sbp;
    386 	struct netbsd32_stat *sb32p;
    387 {
    388 	sb32p->st_dev = sbp->st_dev;
    389 	sb32p->st_ino = sbp->st_ino;
    390 	sb32p->st_mode = sbp->st_mode;
    391 	sb32p->st_nlink = sbp->st_nlink;
    392 	sb32p->st_uid = sbp->st_uid;
    393 	sb32p->st_gid = sbp->st_gid;
    394 	sb32p->st_rdev = sbp->st_rdev;
    395 	if (sbp->st_size < (quad_t)1 << 32)
    396 		sb32p->st_size = sbp->st_size;
    397 	else
    398 		sb32p->st_size = -2;
    399 	sb32p->st_atimespec.tv_sec = (netbsd32_time_t)sbp->st_atimespec.tv_sec;
    400 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
    401 	sb32p->st_mtimespec.tv_sec = (netbsd32_time_t)sbp->st_mtimespec.tv_sec;
    402 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
    403 	sb32p->st_ctimespec.tv_sec = (netbsd32_time_t)sbp->st_ctimespec.tv_sec;
    404 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
    405 	sb32p->st_blksize = sbp->st_blksize;
    406 	sb32p->st_blocks = sbp->st_blocks;
    407 	sb32p->st_flags = sbp->st_flags;
    408 	sb32p->st_gen = sbp->st_gen;
    409 }
    410 
    411 static __inline void
    412 netbsd32_to_ipc_perm(ip32p, ipp)
    413 	struct netbsd32_ipc_perm *ip32p;
    414 	struct ipc_perm *ipp;
    415 {
    416 
    417 	ipp->cuid = ip32p->cuid;
    418 	ipp->cgid = ip32p->cgid;
    419 	ipp->uid = ip32p->uid;
    420 	ipp->gid = ip32p->gid;
    421 	ipp->mode = ip32p->mode;
    422 	ipp->_seq = ip32p->_seq;
    423 	ipp->_key = (key_t)ip32p->_key;
    424 }
    425 
    426 static __inline void
    427 netbsd32_from_ipc_perm(ipp, ip32p)
    428 	struct ipc_perm *ipp;
    429 	struct netbsd32_ipc_perm *ip32p;
    430 {
    431 
    432 	ip32p->cuid = ipp->cuid;
    433 	ip32p->cgid = ipp->cgid;
    434 	ip32p->uid = ipp->uid;
    435 	ip32p->gid = ipp->gid;
    436 	ip32p->mode = ipp->mode;
    437 	ip32p->_seq = ipp->_seq;
    438 	ip32p->_key = (netbsd32_key_t)ipp->_key;
    439 }
    440 
    441 static __inline void
    442 netbsd32_to_msg(m32p, mp)
    443 	struct netbsd32_msg *m32p;
    444 	struct msg *mp;
    445 {
    446 
    447 	mp->msg_next = (struct msg *)(u_long)m32p->msg_next;
    448 	mp->msg_type = (long)m32p->msg_type;
    449 	mp->msg_ts = m32p->msg_ts;
    450 	mp->msg_spot = m32p->msg_spot;
    451 }
    452 
    453 static __inline void
    454 netbsd32_from_msg(mp, m32p)
    455 	struct msg *mp;
    456 	struct netbsd32_msg *m32p;
    457 {
    458 
    459 	m32p->msg_next = (netbsd32_msgp_t)(u_long)mp->msg_next;
    460 	m32p->msg_type = (netbsd32_long)mp->msg_type;
    461 	m32p->msg_ts = mp->msg_ts;
    462 	m32p->msg_spot = mp->msg_spot;
    463 }
    464 
    465 static __inline void
    466 netbsd32_to_msqid_ds(ds32p, dsp)
    467 	struct netbsd32_msqid_ds *ds32p;
    468 	struct msqid_ds *dsp;
    469 {
    470 
    471 	netbsd32_to_ipc_perm(&ds32p->msg_perm, &dsp->msg_perm);
    472 	netbsd32_to_msg((struct netbsd32_msg *)(u_long)ds32p->_msg_first, dsp->_msg_first);
    473 	netbsd32_to_msg((struct netbsd32_msg *)(u_long)ds32p->_msg_last, dsp->_msg_last);
    474 	dsp->_msg_cbytes = (u_long)ds32p->_msg_cbytes;
    475 	dsp->msg_qnum = (u_long)ds32p->msg_qnum;
    476 	dsp->msg_qbytes = (u_long)ds32p->msg_qbytes;
    477 	dsp->msg_lspid = ds32p->msg_lspid;
    478 	dsp->msg_lrpid = ds32p->msg_lrpid;
    479 	dsp->msg_rtime = (time_t)ds32p->msg_rtime;
    480 	dsp->msg_stime = (time_t)ds32p->msg_stime;
    481 	dsp->msg_ctime = (time_t)ds32p->msg_ctime;
    482 }
    483 
    484 static __inline void
    485 netbsd32_from_msqid_ds(dsp, ds32p)
    486 	struct msqid_ds *dsp;
    487 	struct netbsd32_msqid_ds *ds32p;
    488 {
    489 
    490 	netbsd32_from_ipc_perm(&dsp->msg_perm, &ds32p->msg_perm);
    491 	netbsd32_from_msg(dsp->_msg_first, (struct netbsd32_msg *)(u_long)ds32p->_msg_first);
    492 	netbsd32_from_msg(dsp->_msg_last, (struct netbsd32_msg *)(u_long)ds32p->_msg_last);
    493 	ds32p->_msg_cbytes = (netbsd32_u_long)dsp->_msg_cbytes;
    494 	ds32p->msg_qnum = (netbsd32_u_long)dsp->msg_qnum;
    495 	ds32p->msg_qbytes = (netbsd32_u_long)dsp->msg_qbytes;
    496 	ds32p->msg_lspid = dsp->msg_lspid;
    497 	ds32p->msg_lrpid = dsp->msg_lrpid;
    498 	ds32p->msg_rtime = dsp->msg_rtime;
    499 	ds32p->msg_stime = dsp->msg_stime;
    500 	ds32p->msg_ctime = dsp->msg_ctime;
    501 }
    502 
    503 static __inline void
    504 netbsd32_to_shmid_ds(ds32p, dsp)
    505 	struct netbsd32_shmid_ds *ds32p;
    506 	struct shmid_ds *dsp;
    507 {
    508 
    509 	netbsd32_to_ipc_perm(&ds32p->shm_perm, &dsp->shm_perm);
    510 	dsp->shm_segsz = ds32p->shm_segsz;
    511 	dsp->shm_lpid = ds32p->shm_lpid;
    512 	dsp->shm_cpid = ds32p->shm_cpid;
    513 	dsp->shm_nattch = ds32p->shm_nattch;
    514 	dsp->shm_atime = (long)ds32p->shm_atime;
    515 	dsp->shm_dtime = (long)ds32p->shm_dtime;
    516 	dsp->shm_ctime = (long)ds32p->shm_ctime;
    517 	dsp->_shm_internal = (void *)(u_long)ds32p->_shm_internal;
    518 }
    519 
    520 static __inline void
    521 netbsd32_from_shmid_ds(dsp, ds32p)
    522 	struct shmid_ds *dsp;
    523 	struct netbsd32_shmid_ds *ds32p;
    524 {
    525 
    526 	netbsd32_from_ipc_perm(&dsp->shm_perm, &ds32p->shm_perm);
    527 	ds32p->shm_segsz = dsp->shm_segsz;
    528 	ds32p->shm_lpid = dsp->shm_lpid;
    529 	ds32p->shm_cpid = dsp->shm_cpid;
    530 	ds32p->shm_nattch = dsp->shm_nattch;
    531 	ds32p->shm_atime = (netbsd32_long)dsp->shm_atime;
    532 	ds32p->shm_dtime = (netbsd32_long)dsp->shm_dtime;
    533 	ds32p->shm_ctime = (netbsd32_long)dsp->shm_ctime;
    534 	ds32p->_shm_internal = (netbsd32_voidp)(u_long)dsp->_shm_internal;
    535 }
    536 
    537 static __inline void
    538 netbsd32_to_semid_ds(s32dsp, dsp)
    539 	struct  netbsd32_semid_ds *s32dsp;
    540 	struct  semid_ds *dsp;
    541 {
    542 
    543 	netbsd32_from_ipc_perm(&dsp->sem_perm, &s32dsp->sem_perm);
    544 	dsp->_sem_base = (struct __sem *)(u_long)s32dsp->_sem_base;
    545 	dsp->sem_nsems = s32dsp->sem_nsems;
    546 	dsp->sem_otime = s32dsp->sem_otime;
    547 	dsp->sem_ctime = s32dsp->sem_ctime;
    548 }
    549 
    550 static __inline void
    551 netbsd32_from_semid_ds(dsp, s32dsp)
    552 	struct  semid_ds *dsp;
    553 	struct  netbsd32_semid_ds *s32dsp;
    554 {
    555 
    556 	netbsd32_to_ipc_perm(&s32dsp->sem_perm, &dsp->sem_perm);
    557 	s32dsp->_sem_base = (netbsd32_semp_t)(u_long)dsp->_sem_base;
    558 	s32dsp->sem_nsems = dsp->sem_nsems;
    559 	s32dsp->sem_otime = dsp->sem_otime;
    560 	s32dsp->sem_ctime = dsp->sem_ctime;
    561 }
    562 
    563 /*
    564  * below are all the standard NetBSD system calls, in the 32bit
    565  * environment, witht he necessary conversions to 64bit before
    566  * calling the real syscall.
    567  */
    568 
    569 
    570 int
    571 netbsd32_exit(p, v, retval)
    572 	struct proc *p;
    573 	void *v;
    574 	register_t *retval;
    575 {
    576 	struct netbsd32_exit_args /* {
    577 		syscallarg(int) rval;
    578 	} */ *uap = v;
    579 	struct sys_exit_args ua;
    580 
    581 	NETBSD32TO64_UAP(rval);
    582 	sys_exit(p, &ua, retval);
    583 }
    584 
    585 int
    586 netbsd32_read(p, v, retval)
    587 	struct proc *p;
    588 	void *v;
    589 	register_t *retval;
    590 {
    591 	struct netbsd32_read_args /* {
    592 		syscallarg(int) fd;
    593 		syscallarg(netbsd32_voidp) buf;
    594 		syscallarg(netbsd32_size_t) nbyte;
    595 	} */ *uap = v;
    596 	struct sys_read_args ua;
    597 
    598 	NETBSD32TO64_UAP(fd);
    599 	NETBSD32TOP_UAP(buf, void *);
    600 	NETBSD32TOX_UAP(nbyte, size_t);
    601 	return sys_read(p, &ua, retval);
    602 }
    603 
    604 int
    605 netbsd32_write(p, v, retval)
    606 	struct proc *p;
    607 	void *v;
    608 	register_t *retval;
    609 {
    610 	struct netbsd32_write_args /* {
    611 		syscallarg(int) fd;
    612 		syscallarg(const netbsd32_voidp) buf;
    613 		syscallarg(netbsd32_size_t) nbyte;
    614 	} */ *uap = v;
    615 	struct sys_write_args ua;
    616 
    617 	NETBSD32TO64_UAP(fd);
    618 	NETBSD32TOP_UAP(buf, void *);
    619 	NETBSD32TOX_UAP(nbyte, size_t);
    620 	return sys_write(p, &ua, retval);
    621 }
    622 
    623 int
    624 netbsd32_close(p, v, retval)
    625 	struct proc *p;
    626 	void *v;
    627 	register_t *retval;
    628 {
    629 	struct netbsd32_close_args /* {
    630 		syscallarg(int) fd;
    631 	} */ *uap = v;
    632 	struct sys_close_args ua;
    633 
    634 	NETBSD32TO64_UAP(fd);
    635 	return sys_close(p, &ua, retval);
    636 }
    637 
    638 int
    639 netbsd32_open(p, v, retval)
    640 	struct proc *p;
    641 	void *v;
    642 	register_t *retval;
    643 {
    644 	struct netbsd32_open_args /* {
    645 		syscallarg(const netbsd32_charp) path;
    646 		syscallarg(int) flags;
    647 		syscallarg(mode_t) mode;
    648 	} */ *uap = v;
    649 	struct sys_open_args ua;
    650 	caddr_t sg;
    651 
    652 	NETBSD32TOP_UAP(path, const char);
    653 	NETBSD32TO64_UAP(flags);
    654 	NETBSD32TO64_UAP(mode);
    655 	sg = stackgap_init(p->p_emul);
    656 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
    657 
    658 	return (sys_open(p, &ua, retval));
    659 }
    660 
    661 int
    662 netbsd32_wait4(q, v, retval)
    663 	struct proc *q;
    664 	void *v;
    665 	register_t *retval;
    666 {
    667 	struct netbsd32_wait4_args /* {
    668 		syscallarg(int) pid;
    669 		syscallarg(netbsd32_intp) status;
    670 		syscallarg(int) options;
    671 		syscallarg(netbsd32_rusagep_t) rusage;
    672 	} */ *uap = v;
    673 	struct netbsd32_rusage ru32;
    674 	int nfound;
    675 	struct proc *p, *t;
    676 	int status, error;
    677 
    678 	if (SCARG(uap, pid) == 0)
    679 		SCARG(uap, pid) = -q->p_pgid;
    680 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
    681 		return (EINVAL);
    682 
    683 loop:
    684 	nfound = 0;
    685 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    686 		if (SCARG(uap, pid) != WAIT_ANY &&
    687 		    p->p_pid != SCARG(uap, pid) &&
    688 		    p->p_pgid != -SCARG(uap, pid))
    689 			continue;
    690 		nfound++;
    691 		if (p->p_stat == SZOMB) {
    692 			retval[0] = p->p_pid;
    693 
    694 			if (SCARG(uap, status)) {
    695 				status = p->p_xstat;	/* convert to int */
    696 				error = copyout((caddr_t)&status,
    697 						(caddr_t)(u_long)SCARG(uap, status),
    698 						sizeof(status));
    699 				if (error)
    700 					return (error);
    701 			}
    702 			if (SCARG(uap, rusage)) {
    703 				netbsd32_from_rusage(p->p_ru, &ru32);
    704 				if ((error = copyout((caddr_t)&ru32,
    705 						     (caddr_t)(u_long)SCARG(uap, rusage),
    706 						     sizeof(struct netbsd32_rusage))))
    707 					return (error);
    708 			}
    709 			/*
    710 			 * If we got the child via ptrace(2) or procfs, and
    711 			 * the parent is different (meaning the process was
    712 			 * attached, rather than run as a child), then we need
    713 			 * to give it back to the old parent, and send the
    714 			 * parent a SIGCHLD.  The rest of the cleanup will be
    715 			 * done when the old parent waits on the child.
    716 			 */
    717 			if ((p->p_flag & P_TRACED) &&
    718 			    p->p_oppid != p->p_pptr->p_pid) {
    719 				t = pfind(p->p_oppid);
    720 				proc_reparent(p, t ? t : initproc);
    721 				p->p_oppid = 0;
    722 				p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
    723 				psignal(p->p_pptr, SIGCHLD);
    724 				wakeup((caddr_t)p->p_pptr);
    725 				return (0);
    726 			}
    727 			p->p_xstat = 0;
    728 			ruadd(&q->p_stats->p_cru, p->p_ru);
    729 			pool_put(&rusage_pool, p->p_ru);
    730 
    731 			/*
    732 			 * Finally finished with old proc entry.
    733 			 * Unlink it from its process group and free it.
    734 			 */
    735 			leavepgrp(p);
    736 
    737 			LIST_REMOVE(p, p_list);	/* off zombproc */
    738 
    739 			LIST_REMOVE(p, p_sibling);
    740 
    741 			/*
    742 			 * Decrement the count of procs running with this uid.
    743 			 */
    744 			(void)chgproccnt(p->p_cred->p_ruid, -1);
    745 
    746 			/*
    747 			 * Free up credentials.
    748 			 */
    749 			if (--p->p_cred->p_refcnt == 0) {
    750 				crfree(p->p_cred->pc_ucred);
    751 				pool_put(&pcred_pool, p->p_cred);
    752 			}
    753 
    754 			/*
    755 			 * Release reference to text vnode
    756 			 */
    757 			if (p->p_textvp)
    758 				vrele(p->p_textvp);
    759 
    760 			pool_put(&proc_pool, p);
    761 			nprocs--;
    762 			return (0);
    763 		}
    764 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
    765 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
    766 			p->p_flag |= P_WAITED;
    767 			retval[0] = p->p_pid;
    768 
    769 			if (SCARG(uap, status)) {
    770 				status = W_STOPCODE(p->p_xstat);
    771 				error = copyout((caddr_t)&status,
    772 				    (caddr_t)(u_long)SCARG(uap, status),
    773 				    sizeof(status));
    774 			} else
    775 				error = 0;
    776 			return (error);
    777 		}
    778 	}
    779 	if (nfound == 0)
    780 		return (ECHILD);
    781 	if (SCARG(uap, options) & WNOHANG) {
    782 		retval[0] = 0;
    783 		return (0);
    784 	}
    785 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
    786 		return (error);
    787 	goto loop;
    788 }
    789 
    790 int
    791 netbsd32_link(p, v, retval)
    792 	struct proc *p;
    793 	void *v;
    794 	register_t *retval;
    795 {
    796 	struct netbsd32_link_args /* {
    797 		syscallarg(const netbsd32_charp) path;
    798 		syscallarg(const netbsd32_charp) link;
    799 	} */ *uap = v;
    800 	struct sys_link_args ua;
    801 
    802 	NETBSD32TOP_UAP(path, const char);
    803 	NETBSD32TOP_UAP(link, const char);
    804 	return (sys_link(p, &ua, retval));
    805 }
    806 
    807 int
    808 netbsd32_unlink(p, v, retval)
    809 	struct proc *p;
    810 	void *v;
    811 	register_t *retval;
    812 {
    813 	struct netbsd32_unlink_args /* {
    814 		syscallarg(const netbsd32_charp) path;
    815 	} */ *uap = v;
    816 	struct sys_unlink_args ua;
    817 
    818 	NETBSD32TOP_UAP(path, const char);
    819 
    820 	return (sys_unlink(p, &ua, retval));
    821 }
    822 
    823 int
    824 netbsd32_chdir(p, v, retval)
    825 	struct proc *p;
    826 	void *v;
    827 	register_t *retval;
    828 {
    829 	struct netbsd32_chdir_args /* {
    830 		syscallarg(const netbsd32_charp) path;
    831 	} */ *uap = v;
    832 	struct sys_chdir_args ua;
    833 
    834 	NETBSD32TOP_UAP(path, const char);
    835 
    836 	return (sys_chdir(p, &ua, retval));
    837 }
    838 
    839 int
    840 netbsd32_fchdir(p, v, retval)
    841 	struct proc *p;
    842 	void *v;
    843 	register_t *retval;
    844 {
    845 	struct netbsd32_fchdir_args /* {
    846 		syscallarg(int) fd;
    847 	} */ *uap = v;
    848 	struct sys_fchdir_args ua;
    849 
    850 	NETBSD32TO64_UAP(fd);
    851 
    852 	return (sys_fchdir(p, &ua, retval));
    853 }
    854 
    855 int
    856 netbsd32_mknod(p, v, retval)
    857 	struct proc *p;
    858 	void *v;
    859 	register_t *retval;
    860 {
    861 	struct netbsd32_mknod_args /* {
    862 		syscallarg(const netbsd32_charp) path;
    863 		syscallarg(mode_t) mode;
    864 		syscallarg(dev_t) dev;
    865 	} */ *uap = v;
    866 	struct sys_mknod_args ua;
    867 
    868 	NETBSD32TOP_UAP(path, const char);
    869 	NETBSD32TO64_UAP(dev);
    870 	NETBSD32TO64_UAP(mode);
    871 
    872 	return (sys_mknod(p, &ua, retval));
    873 }
    874 
    875 int
    876 netbsd32_chmod(p, v, retval)
    877 	struct proc *p;
    878 	void *v;
    879 	register_t *retval;
    880 {
    881 	struct netbsd32_chmod_args /* {
    882 		syscallarg(const netbsd32_charp) path;
    883 		syscallarg(mode_t) mode;
    884 	} */ *uap = v;
    885 	struct sys_chmod_args ua;
    886 
    887 	NETBSD32TOP_UAP(path, const char);
    888 	NETBSD32TO64_UAP(mode);
    889 
    890 	return (sys_chmod(p, &ua, retval));
    891 }
    892 
    893 int
    894 netbsd32_chown(p, v, retval)
    895 	struct proc *p;
    896 	void *v;
    897 	register_t *retval;
    898 {
    899 	struct netbsd32_chown_args /* {
    900 		syscallarg(const netbsd32_charp) path;
    901 		syscallarg(uid_t) uid;
    902 		syscallarg(gid_t) gid;
    903 	} */ *uap = v;
    904 	struct sys_chown_args ua;
    905 
    906 	NETBSD32TOP_UAP(path, const char);
    907 	NETBSD32TO64_UAP(uid);
    908 	NETBSD32TO64_UAP(gid);
    909 
    910 	return (sys_chown(p, &ua, retval));
    911 }
    912 
    913 int
    914 netbsd32_break(p, v, retval)
    915 	struct proc *p;
    916 	void *v;
    917 	register_t *retval;
    918 {
    919 	struct netbsd32_break_args /* {
    920 		syscallarg(netbsd32_charp) nsize;
    921 	} */ *uap = v;
    922 	struct sys_obreak_args ua;
    923 
    924 	SCARG(&ua, nsize) = (char *)(u_long)SCARG(uap, nsize);
    925 	NETBSD32TOP_UAP(nsize, char);
    926 	return (sys_obreak(p, &ua, retval));
    927 }
    928 
    929 int
    930 netbsd32_getfsstat(p, v, retval)
    931 	struct proc *p;
    932 	void *v;
    933 	register_t *retval;
    934 {
    935 	struct netbsd32_getfsstat_args /* {
    936 		syscallarg(netbsd32_statfsp_t) buf;
    937 		syscallarg(netbsd32_long) bufsize;
    938 		syscallarg(int) flags;
    939 	} */ *uap = v;
    940 	struct mount *mp, *nmp;
    941 	struct statfs *sp;
    942 	struct netbsd32_statfs sb32;
    943 	caddr_t sfsp;
    944 	long count, maxcount, error;
    945 
    946 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
    947 	sfsp = (caddr_t)SCARG(uap, buf);
    948 	simple_lock(&mountlist_slock);
    949 	count = 0;
    950 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    951 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    952 			nmp = mp->mnt_list.cqe_next;
    953 			continue;
    954 		}
    955 		if (sfsp && count < maxcount) {
    956 			sp = &mp->mnt_stat;
    957 			/*
    958 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    959 			 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
    960 			 * overrides MNT_NOWAIT.
    961 			 */
    962 			if (SCARG(uap, flags) != MNT_NOWAIT &&
    963 			    SCARG(uap, flags) != MNT_LAZY &&
    964 			    (SCARG(uap, flags) == MNT_WAIT ||
    965 			     SCARG(uap, flags) == 0) &&
    966 			    (error = VFS_STATFS(mp, sp, p)) != 0) {
    967 				simple_lock(&mountlist_slock);
    968 				nmp = mp->mnt_list.cqe_next;
    969 				vfs_unbusy(mp);
    970 				continue;
    971 			}
    972 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    973 			sp->f_oflags = sp->f_flags & 0xffff;
    974 			netbsd32_from_statfs(sp, &sb32);
    975 			error = copyout(&sb32, sfsp, sizeof(sb32));
    976 			if (error) {
    977 				vfs_unbusy(mp);
    978 				return (error);
    979 			}
    980 			sfsp += sizeof(sb32);
    981 		}
    982 		count++;
    983 		simple_lock(&mountlist_slock);
    984 		nmp = mp->mnt_list.cqe_next;
    985 		vfs_unbusy(mp);
    986 	}
    987 	simple_unlock(&mountlist_slock);
    988 	if (sfsp && count > maxcount)
    989 		*retval = maxcount;
    990 	else
    991 		*retval = count;
    992 	return (0);
    993 }
    994 
    995 int
    996 netbsd32_mount(p, v, retval)
    997 	struct proc *p;
    998 	void *v;
    999 	register_t *retval;
   1000 {
   1001 	struct netbsd32_mount_args /* {
   1002 		syscallarg(const netbsd32_charp) type;
   1003 		syscallarg(const netbsd32_charp) path;
   1004 		syscallarg(int) flags;
   1005 		syscallarg(netbsd32_voidp) data;
   1006 	} */ *uap = v;
   1007 	struct sys_mount_args ua;
   1008 
   1009 	NETBSD32TOP_UAP(type, const char);
   1010 	NETBSD32TOP_UAP(path, const char);
   1011 	NETBSD32TO64_UAP(flags);
   1012 	NETBSD32TOP_UAP(data, void);
   1013 	return (sys_mount(p, &ua, retval));
   1014 }
   1015 
   1016 int
   1017 netbsd32_unmount(p, v, retval)
   1018 	struct proc *p;
   1019 	void *v;
   1020 	register_t *retval;
   1021 {
   1022 	struct netbsd32_unmount_args /* {
   1023 		syscallarg(const netbsd32_charp) path;
   1024 		syscallarg(int) flags;
   1025 	} */ *uap = v;
   1026 	struct sys_unmount_args ua;
   1027 
   1028 	NETBSD32TOP_UAP(path, const char);
   1029 	NETBSD32TO64_UAP(flags);
   1030 	return (sys_unmount(p, &ua, retval));
   1031 }
   1032 
   1033 int
   1034 netbsd32_setuid(p, v, retval)
   1035 	struct proc *p;
   1036 	void *v;
   1037 	register_t *retval;
   1038 {
   1039 	struct netbsd32_setuid_args /* {
   1040 		syscallarg(uid_t) uid;
   1041 	} */ *uap = v;
   1042 	struct sys_setuid_args ua;
   1043 
   1044 	NETBSD32TO64_UAP(uid);
   1045 	return (sys_setuid(p, &ua, retval));
   1046 }
   1047 
   1048 int
   1049 netbsd32_ptrace(p, v, retval)
   1050 	struct proc *p;
   1051 	void *v;
   1052 	register_t *retval;
   1053 {
   1054 	struct netbsd32_ptrace_args /* {
   1055 		syscallarg(int) req;
   1056 		syscallarg(pid_t) pid;
   1057 		syscallarg(netbsd32_caddr_t) addr;
   1058 		syscallarg(int) data;
   1059 	} */ *uap = v;
   1060 	struct sys_ptrace_args ua;
   1061 
   1062 	NETBSD32TO64_UAP(req);
   1063 	NETBSD32TO64_UAP(pid);
   1064 	NETBSD32TOX64_UAP(addr, caddr_t);
   1065 	NETBSD32TO64_UAP(data);
   1066 	return (sys_ptrace(p, &ua, retval));
   1067 }
   1068 
   1069 int
   1070 netbsd32_recvmsg(p, v, retval)
   1071 	struct proc *p;
   1072 	void *v;
   1073 	register_t *retval;
   1074 {
   1075 	struct netbsd32_recvmsg_args /* {
   1076 		syscallarg(int) s;
   1077 		syscallarg(netbsd32_msghdrp_t) msg;
   1078 		syscallarg(int) flags;
   1079 	} */ *uap = v;
   1080 	struct netbsd32_msghdr msg;
   1081 	struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
   1082 	int error;
   1083 
   1084 	error = copyin((caddr_t)(u_long)SCARG(uap, msg), (caddr_t)&msg,
   1085 		       sizeof(msg));
   1086 		/* netbsd32_msghdr needs the iov pre-allocated */
   1087 	if (error)
   1088 		return (error);
   1089 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
   1090 		if ((u_int)msg.msg_iovlen > IOV_MAX)
   1091 			return (EMSGSIZE);
   1092 		MALLOC(iov, struct iovec *,
   1093 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
   1094 		       M_WAITOK);
   1095 	} else if ((u_int)msg.msg_iovlen > 0)
   1096 		iov = aiov;
   1097 	else
   1098 		return (EMSGSIZE);
   1099 #ifdef COMPAT_OLDSOCK
   1100 	msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
   1101 #else
   1102 	msg.msg_flags = SCARG(uap, flags);
   1103 #endif
   1104 	uiov = (struct iovec *)(u_long)msg.msg_iov;
   1105 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)uiov,
   1106 				   iov, msg.msg_iovlen);
   1107 	if (error)
   1108 		goto done;
   1109 	if ((error = recvit32(p, SCARG(uap, s), &msg, iov, (caddr_t)0, retval)) == 0) {
   1110 		error = copyout((caddr_t)&msg, (caddr_t)(u_long)SCARG(uap, msg),
   1111 		    sizeof(msg));
   1112 	}
   1113 done:
   1114 	if (iov != aiov)
   1115 		FREE(iov, M_IOV);
   1116 	return (error);
   1117 }
   1118 
   1119 int
   1120 recvit32(p, s, mp, iov, namelenp, retsize)
   1121 	struct proc *p;
   1122 	int s;
   1123 	struct netbsd32_msghdr *mp;
   1124 	struct iovec *iov;
   1125 	caddr_t namelenp;
   1126 	register_t *retsize;
   1127 {
   1128 	struct file *fp;
   1129 	struct uio auio;
   1130 	int i;
   1131 	int len, error;
   1132 	struct mbuf *from = 0, *control = 0;
   1133 	struct socket *so;
   1134 #ifdef KTRACE
   1135 	struct iovec *ktriov = NULL;
   1136 #endif
   1137 
   1138 	/* getsock() will use the descriptor for us */
   1139 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
   1140 		return (error);
   1141 	auio.uio_iov = (struct iovec *)(u_long)mp->msg_iov;
   1142 	auio.uio_iovcnt = mp->msg_iovlen;
   1143 	auio.uio_segflg = UIO_USERSPACE;
   1144 	auio.uio_rw = UIO_READ;
   1145 	auio.uio_procp = p;
   1146 	auio.uio_offset = 0;			/* XXX */
   1147 	auio.uio_resid = 0;
   1148 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
   1149 #if 0
   1150 		/* cannot happen iov_len is unsigned */
   1151 		if (iov->iov_len < 0) {
   1152 			error = EINVAL;
   1153 			goto out1;
   1154 		}
   1155 #endif
   1156 		/*
   1157 		 * Reads return ssize_t because -1 is returned on error.
   1158 		 * Therefore we must restrict the length to SSIZE_MAX to
   1159 		 * avoid garbage return values.
   1160 		 */
   1161 		auio.uio_resid += iov->iov_len;
   1162 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
   1163 			error = EINVAL;
   1164 			goto out1;
   1165 		}
   1166 	}
   1167 #ifdef KTRACE
   1168 	if (KTRPOINT(p, KTR_GENIO)) {
   1169 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
   1170 
   1171 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
   1172 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
   1173 	}
   1174 #endif
   1175 	len = auio.uio_resid;
   1176 	so = (struct socket *)fp->f_data;
   1177 	error = (*so->so_receive)(so, &from, &auio, NULL,
   1178 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
   1179 	if (error) {
   1180 		if (auio.uio_resid != len && (error == ERESTART ||
   1181 		    error == EINTR || error == EWOULDBLOCK))
   1182 			error = 0;
   1183 	}
   1184 #ifdef KTRACE
   1185 	if (ktriov != NULL) {
   1186 		if (error == 0)
   1187 			ktrgenio(p, s, UIO_READ, ktriov,
   1188 			    len - auio.uio_resid, error);
   1189 		FREE(ktriov, M_TEMP);
   1190 	}
   1191 #endif
   1192 	if (error)
   1193 		goto out;
   1194 	*retsize = len - auio.uio_resid;
   1195 	if (mp->msg_name) {
   1196 		len = mp->msg_namelen;
   1197 		if (len <= 0 || from == 0)
   1198 			len = 0;
   1199 		else {
   1200 #ifdef COMPAT_OLDSOCK
   1201 			if (mp->msg_flags & MSG_COMPAT)
   1202 				mtod(from, struct osockaddr *)->sa_family =
   1203 				    mtod(from, struct sockaddr *)->sa_family;
   1204 #endif
   1205 			if (len > from->m_len)
   1206 				len = from->m_len;
   1207 			/* else if len < from->m_len ??? */
   1208 			error = copyout(mtod(from, caddr_t),
   1209 					(caddr_t)(u_long)mp->msg_name, (unsigned)len);
   1210 			if (error)
   1211 				goto out;
   1212 		}
   1213 		mp->msg_namelen = len;
   1214 		if (namelenp &&
   1215 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
   1216 #ifdef COMPAT_OLDSOCK
   1217 			if (mp->msg_flags & MSG_COMPAT)
   1218 				error = 0;	/* old recvfrom didn't check */
   1219 			else
   1220 #endif
   1221 			goto out;
   1222 		}
   1223 	}
   1224 	if (mp->msg_control) {
   1225 #ifdef COMPAT_OLDSOCK
   1226 		/*
   1227 		 * We assume that old recvmsg calls won't receive access
   1228 		 * rights and other control info, esp. as control info
   1229 		 * is always optional and those options didn't exist in 4.3.
   1230 		 * If we receive rights, trim the cmsghdr; anything else
   1231 		 * is tossed.
   1232 		 */
   1233 		if (control && mp->msg_flags & MSG_COMPAT) {
   1234 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
   1235 			    SOL_SOCKET ||
   1236 			    mtod(control, struct cmsghdr *)->cmsg_type !=
   1237 			    SCM_RIGHTS) {
   1238 				mp->msg_controllen = 0;
   1239 				goto out;
   1240 			}
   1241 			control->m_len -= sizeof(struct cmsghdr);
   1242 			control->m_data += sizeof(struct cmsghdr);
   1243 		}
   1244 #endif
   1245 		len = mp->msg_controllen;
   1246 		if (len <= 0 || control == 0)
   1247 			len = 0;
   1248 		else {
   1249 			struct mbuf *m = control;
   1250 			caddr_t p = (caddr_t)(u_long)mp->msg_control;
   1251 
   1252 			do {
   1253 				i = m->m_len;
   1254 				if (len < i) {
   1255 					mp->msg_flags |= MSG_CTRUNC;
   1256 					i = len;
   1257 				}
   1258 				error = copyout(mtod(m, caddr_t), p,
   1259 				    (unsigned)i);
   1260 				if (m->m_next)
   1261 					i = ALIGN(i);
   1262 				p += i;
   1263 				len -= i;
   1264 				if (error != 0 || len <= 0)
   1265 					break;
   1266 			} while ((m = m->m_next) != NULL);
   1267 			len = p - (caddr_t)(u_long)mp->msg_control;
   1268 		}
   1269 		mp->msg_controllen = len;
   1270 	}
   1271  out:
   1272 	if (from)
   1273 		m_freem(from);
   1274 	if (control)
   1275 		m_freem(control);
   1276  out1:
   1277 	FILE_UNUSE(fp, p);
   1278 	return (error);
   1279 }
   1280 
   1281 
   1282 int
   1283 netbsd32_sendmsg(p, v, retval)
   1284 	struct proc *p;
   1285 	void *v;
   1286 	register_t *retval;
   1287 {
   1288 	struct netbsd32_sendmsg_args /* {
   1289 		syscallarg(int) s;
   1290 		syscallarg(const netbsd32_msghdrp_t) msg;
   1291 		syscallarg(int) flags;
   1292 	} */ *uap = v;
   1293 	struct msghdr msg;
   1294 	struct netbsd32_msghdr msg32;
   1295 	struct iovec aiov[UIO_SMALLIOV], *iov;
   1296 	int error;
   1297 
   1298 	error = copyin((caddr_t)(u_long)SCARG(uap, msg),
   1299 		       (caddr_t)&msg32, sizeof(msg32));
   1300 	if (error)
   1301 		return (error);
   1302 	netbsd32_to_msghdr(&msg32, &msg);
   1303 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
   1304 		if ((u_int)msg.msg_iovlen > IOV_MAX)
   1305 			return (EMSGSIZE);
   1306 		MALLOC(iov, struct iovec *,
   1307 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
   1308 		       M_WAITOK);
   1309 	} else if ((u_int)msg.msg_iovlen > 0)
   1310 		iov = aiov;
   1311 	else
   1312 		return (EMSGSIZE);
   1313 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
   1314 				   iov, msg.msg_iovlen);
   1315 	if (error)
   1316 		goto done;
   1317 	msg.msg_iov = iov;
   1318 #ifdef COMPAT_OLDSOCK
   1319 	msg.msg_flags = 0;
   1320 #endif
   1321 	/* Luckily we can use this directly */
   1322 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
   1323 done:
   1324 	if (iov != aiov)
   1325 		FREE(iov, M_IOV);
   1326 	return (error);
   1327 }
   1328 
   1329 int
   1330 netbsd32_recvfrom(p, v, retval)
   1331 	struct proc *p;
   1332 	void *v;
   1333 	register_t *retval;
   1334 {
   1335 	struct netbsd32_recvfrom_args /* {
   1336 		syscallarg(int) s;
   1337 		syscallarg(netbsd32_voidp) buf;
   1338 		syscallarg(netbsd32_size_t) len;
   1339 		syscallarg(int) flags;
   1340 		syscallarg(netbsd32_sockaddrp_t) from;
   1341 		syscallarg(netbsd32_intp) fromlenaddr;
   1342 	} */ *uap = v;
   1343 	struct netbsd32_msghdr msg;
   1344 	struct iovec aiov;
   1345 	int error;
   1346 
   1347 	if (SCARG(uap, fromlenaddr)) {
   1348 		error = copyin((caddr_t)(u_long)SCARG(uap, fromlenaddr),
   1349 			       (caddr_t)&msg.msg_namelen,
   1350 			       sizeof(msg.msg_namelen));
   1351 		if (error)
   1352 			return (error);
   1353 	} else
   1354 		msg.msg_namelen = 0;
   1355 	msg.msg_name = SCARG(uap, from);
   1356 	msg.msg_iov = NULL; /* We can't store a real pointer here */
   1357 	msg.msg_iovlen = 1;
   1358 	aiov.iov_base = (caddr_t)(u_long)SCARG(uap, buf);
   1359 	aiov.iov_len = (u_long)SCARG(uap, len);
   1360 	msg.msg_control = 0;
   1361 	msg.msg_flags = SCARG(uap, flags);
   1362 	return (recvit32(p, SCARG(uap, s), &msg, &aiov,
   1363 		       (caddr_t)(u_long)SCARG(uap, fromlenaddr), retval));
   1364 }
   1365 
   1366 int
   1367 netbsd32_sendto(p, v, retval)
   1368 	struct proc *p;
   1369 	void *v;
   1370 	register_t *retval;
   1371 {
   1372 	struct netbsd32_sendto_args /* {
   1373 		syscallarg(int) s;
   1374 		syscallarg(const netbsd32_voidp) buf;
   1375 		syscallarg(netbsd32_size_t) len;
   1376 		syscallarg(int) flags;
   1377 		syscallarg(const netbsd32_sockaddrp_t) to;
   1378 		syscallarg(int) tolen;
   1379 	} */ *uap = v;
   1380 	struct msghdr msg;
   1381 	struct iovec aiov;
   1382 
   1383 	msg.msg_name = (caddr_t)(u_long)SCARG(uap, to);		/* XXX kills const */
   1384 	msg.msg_namelen = SCARG(uap, tolen);
   1385 	msg.msg_iov = &aiov;
   1386 	msg.msg_iovlen = 1;
   1387 	msg.msg_control = 0;
   1388 #ifdef COMPAT_OLDSOCK
   1389 	msg.msg_flags = 0;
   1390 #endif
   1391 	aiov.iov_base = (char *)(u_long)SCARG(uap, buf);	/* XXX kills const */
   1392 	aiov.iov_len = SCARG(uap, len);
   1393 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
   1394 }
   1395 
   1396 int
   1397 netbsd32_accept(p, v, retval)
   1398 	struct proc *p;
   1399 	void *v;
   1400 	register_t *retval;
   1401 {
   1402 	struct netbsd32_accept_args /* {
   1403 		syscallarg(int) s;
   1404 		syscallarg(netbsd32_sockaddrp_t) name;
   1405 		syscallarg(netbsd32_intp) anamelen;
   1406 	} */ *uap = v;
   1407 	struct sys_accept_args ua;
   1408 
   1409 	NETBSD32TO64_UAP(s);
   1410 	NETBSD32TOP_UAP(name, struct sockaddr);
   1411 	NETBSD32TOP_UAP(anamelen, int);
   1412 	return (sys_accept(p, &ua, retval));
   1413 }
   1414 
   1415 int
   1416 netbsd32_getpeername(p, v, retval)
   1417 	struct proc *p;
   1418 	void *v;
   1419 	register_t *retval;
   1420 {
   1421 	struct netbsd32_getpeername_args /* {
   1422 		syscallarg(int) fdes;
   1423 		syscallarg(netbsd32_sockaddrp_t) asa;
   1424 		syscallarg(netbsd32_intp) alen;
   1425 	} */ *uap = v;
   1426 	struct sys_getpeername_args ua;
   1427 
   1428 	NETBSD32TO64_UAP(fdes);
   1429 	NETBSD32TOP_UAP(asa, struct sockaddr);
   1430 	NETBSD32TOP_UAP(alen, int);
   1431 /* NB: do the protocol specific sockaddrs need to be converted? */
   1432 	return (sys_getpeername(p, &ua, retval));
   1433 }
   1434 
   1435 int
   1436 netbsd32_getsockname(p, v, retval)
   1437 	struct proc *p;
   1438 	void *v;
   1439 	register_t *retval;
   1440 {
   1441 	struct netbsd32_getsockname_args /* {
   1442 		syscallarg(int) fdes;
   1443 		syscallarg(netbsd32_sockaddrp_t) asa;
   1444 		syscallarg(netbsd32_intp) alen;
   1445 	} */ *uap = v;
   1446 	struct sys_getsockname_args ua;
   1447 
   1448 	NETBSD32TO64_UAP(fdes);
   1449 	NETBSD32TOP_UAP(asa, struct sockaddr);
   1450 	NETBSD32TOP_UAP(alen, int);
   1451 	return (sys_getsockname(p, &ua, retval));
   1452 }
   1453 
   1454 int
   1455 netbsd32_access(p, v, retval)
   1456 	struct proc *p;
   1457 	void *v;
   1458 	register_t *retval;
   1459 {
   1460 	struct netbsd32_access_args /* {
   1461 		syscallarg(const netbsd32_charp) path;
   1462 		syscallarg(int) flags;
   1463 	} */ *uap = v;
   1464 	struct sys_access_args ua;
   1465 	caddr_t sg;
   1466 
   1467 	NETBSD32TOP_UAP(path, const char);
   1468 	NETBSD32TO64_UAP(flags);
   1469 	sg = stackgap_init(p->p_emul);
   1470 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1471 
   1472 	return (sys_access(p, &ua, retval));
   1473 }
   1474 
   1475 int
   1476 netbsd32_chflags(p, v, retval)
   1477 	struct proc *p;
   1478 	void *v;
   1479 	register_t *retval;
   1480 {
   1481 	struct netbsd32_chflags_args /* {
   1482 		syscallarg(const netbsd32_charp) path;
   1483 		syscallarg(netbsd32_u_long) flags;
   1484 	} */ *uap = v;
   1485 	struct sys_chflags_args ua;
   1486 
   1487 	NETBSD32TOP_UAP(path, const char);
   1488 	NETBSD32TO64_UAP(flags);
   1489 
   1490 	return (sys_chflags(p, &ua, retval));
   1491 }
   1492 
   1493 int
   1494 netbsd32_fchflags(p, v, retval)
   1495 	struct proc *p;
   1496 	void *v;
   1497 	register_t *retval;
   1498 {
   1499 	struct netbsd32_fchflags_args /* {
   1500 		syscallarg(int) fd;
   1501 		syscallarg(netbsd32_u_long) flags;
   1502 	} */ *uap = v;
   1503 	struct sys_fchflags_args ua;
   1504 
   1505 	NETBSD32TO64_UAP(fd);
   1506 	NETBSD32TO64_UAP(flags);
   1507 
   1508 	return (sys_fchflags(p, &ua, retval));
   1509 }
   1510 
   1511 int
   1512 netbsd32_kill(p, v, retval)
   1513 	struct proc *p;
   1514 	void *v;
   1515 	register_t *retval;
   1516 {
   1517 	struct netbsd32_kill_args /* {
   1518 		syscallarg(int) pid;
   1519 		syscallarg(int) signum;
   1520 	} */ *uap = v;
   1521 	struct sys_kill_args ua;
   1522 
   1523 	NETBSD32TO64_UAP(pid);
   1524 	NETBSD32TO64_UAP(signum);
   1525 
   1526 	return (sys_kill(p, &ua, retval));
   1527 }
   1528 
   1529 int
   1530 netbsd32_dup(p, v, retval)
   1531 	struct proc *p;
   1532 	void *v;
   1533 	register_t *retval;
   1534 {
   1535 	struct netbsd32_dup_args /* {
   1536 		syscallarg(int) fd;
   1537 	} */ *uap = v;
   1538 	struct sys_dup_args ua;
   1539 
   1540 	NETBSD32TO64_UAP(fd);
   1541 
   1542 	return (sys_dup(p, &ua, retval));
   1543 }
   1544 
   1545 int
   1546 netbsd32_profil(p, v, retval)
   1547 	struct proc *p;
   1548 	void *v;
   1549 	register_t *retval;
   1550 {
   1551 	struct netbsd32_profil_args /* {
   1552 		syscallarg(netbsd32_caddr_t) samples;
   1553 		syscallarg(netbsd32_size_t) size;
   1554 		syscallarg(netbsd32_u_long) offset;
   1555 		syscallarg(u_int) scale;
   1556 	} */ *uap = v;
   1557 	struct sys_profil_args ua;
   1558 
   1559 	NETBSD32TOX64_UAP(samples, caddr_t);
   1560 	NETBSD32TOX_UAP(size, size_t);
   1561 	NETBSD32TOX_UAP(offset, u_long);
   1562 	NETBSD32TO64_UAP(scale);
   1563 	return (sys_profil(p, &ua, retval));
   1564 }
   1565 
   1566 int
   1567 netbsd32_ktrace(p, v, retval)
   1568 	struct proc *p;
   1569 	void *v;
   1570 	register_t *retval;
   1571 {
   1572 	struct netbsd32_ktrace_args /* {
   1573 		syscallarg(const netbsd32_charp) fname;
   1574 		syscallarg(int) ops;
   1575 		syscallarg(int) facs;
   1576 		syscallarg(int) pid;
   1577 	} */ *uap = v;
   1578 	struct sys_ktrace_args ua;
   1579 
   1580 	NETBSD32TOP_UAP(fname, const char);
   1581 	NETBSD32TO64_UAP(ops);
   1582 	NETBSD32TO64_UAP(facs);
   1583 	NETBSD32TO64_UAP(pid);
   1584 	return (sys_ktrace(p, &ua, retval));
   1585 }
   1586 
   1587 int
   1588 netbsd32_sigaction(p, v, retval)
   1589 	struct proc *p;
   1590 	void *v;
   1591 	register_t *retval;
   1592 {
   1593 	struct netbsd32_sigaction_args /* {
   1594 		syscallarg(int) signum;
   1595 		syscallarg(const netbsd32_sigactionp_t) nsa;
   1596 		syscallarg(netbsd32_sigactionp_t) osa;
   1597 	} */ *uap = v;
   1598 	struct sigaction nsa, osa;
   1599 	struct netbsd32_sigaction *sa32p, sa32;
   1600 	int error;
   1601 
   1602 	if (SCARG(uap, nsa)) {
   1603 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, nsa);
   1604 		if (copyin(sa32p, &sa32, sizeof(sa32)))
   1605 			return EFAULT;
   1606 		nsa.sa_handler = (void *)(u_long)sa32.sa_handler;
   1607 		nsa.sa_mask = sa32.sa_mask;
   1608 		nsa.sa_flags = sa32.sa_flags;
   1609 	}
   1610 	error = sigaction1(p, SCARG(uap, signum),
   1611 			   SCARG(uap, nsa) ? &nsa : 0,
   1612 			   SCARG(uap, osa) ? &osa : 0);
   1613 
   1614 	if (error)
   1615 		return (error);
   1616 
   1617 	if (SCARG(uap, osa)) {
   1618 		sa32.sa_handler = (netbsd32_sigactionp_t)(u_long)osa.sa_handler;
   1619 		sa32.sa_mask = osa.sa_mask;
   1620 		sa32.sa_flags = osa.sa_flags;
   1621 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, osa);
   1622 		if (copyout(&sa32, sa32p, sizeof(sa32)))
   1623 			return EFAULT;
   1624 	}
   1625 
   1626 	return (0);
   1627 }
   1628 
   1629 int
   1630 netbsd32___getlogin(p, v, retval)
   1631 	struct proc *p;
   1632 	void *v;
   1633 	register_t *retval;
   1634 {
   1635 	struct netbsd32___getlogin_args /* {
   1636 		syscallarg(netbsd32_charp) namebuf;
   1637 		syscallarg(u_int) namelen;
   1638 	} */ *uap = v;
   1639 	struct sys___getlogin_args ua;
   1640 
   1641 	NETBSD32TOP_UAP(namebuf, char);
   1642 	NETBSD32TO64_UAP(namelen);
   1643 	return (sys___getlogin(p, &ua, retval));
   1644 }
   1645 
   1646 int
   1647 netbsd32_setlogin(p, v, retval)
   1648 	struct proc *p;
   1649 	void *v;
   1650 	register_t *retval;
   1651 {
   1652 	struct netbsd32_setlogin_args /* {
   1653 		syscallarg(const netbsd32_charp) namebuf;
   1654 	} */ *uap = v;
   1655 	struct sys_setlogin_args ua;
   1656 
   1657 	NETBSD32TOP_UAP(namebuf, char);
   1658 	return (sys_setlogin(p, &ua, retval));
   1659 }
   1660 
   1661 int
   1662 netbsd32_acct(p, v, retval)
   1663 	struct proc *p;
   1664 	void *v;
   1665 	register_t *retval;
   1666 {
   1667 	struct netbsd32_acct_args /* {
   1668 		syscallarg(const netbsd32_charp) path;
   1669 	} */ *uap = v;
   1670 	struct sys_acct_args ua;
   1671 
   1672 	NETBSD32TOP_UAP(path, const char);
   1673 	return (sys_acct(p, &ua, retval));
   1674 }
   1675 
   1676 int
   1677 netbsd32_revoke(p, v, retval)
   1678 	struct proc *p;
   1679 	void *v;
   1680 	register_t *retval;
   1681 {
   1682 	struct netbsd32_revoke_args /* {
   1683 		syscallarg(const netbsd32_charp) path;
   1684 	} */ *uap = v;
   1685 	struct sys_revoke_args ua;
   1686 	caddr_t sg;
   1687 
   1688 	NETBSD32TOP_UAP(path, const char);
   1689 	sg = stackgap_init(p->p_emul);
   1690 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1691 
   1692 	return (sys_revoke(p, &ua, retval));
   1693 }
   1694 
   1695 int
   1696 netbsd32_symlink(p, v, retval)
   1697 	struct proc *p;
   1698 	void *v;
   1699 	register_t *retval;
   1700 {
   1701 	struct netbsd32_symlink_args /* {
   1702 		syscallarg(const netbsd32_charp) path;
   1703 		syscallarg(const netbsd32_charp) link;
   1704 	} */ *uap = v;
   1705 	struct sys_symlink_args ua;
   1706 
   1707 	NETBSD32TOP_UAP(path, const char);
   1708 	NETBSD32TOP_UAP(link, const char);
   1709 
   1710 	return (sys_symlink(p, &ua, retval));
   1711 }
   1712 
   1713 int
   1714 netbsd32_readlink(p, v, retval)
   1715 	struct proc *p;
   1716 	void *v;
   1717 	register_t *retval;
   1718 {
   1719 	struct netbsd32_readlink_args /* {
   1720 		syscallarg(const netbsd32_charp) path;
   1721 		syscallarg(netbsd32_charp) buf;
   1722 		syscallarg(netbsd32_size_t) count;
   1723 	} */ *uap = v;
   1724 	struct sys_readlink_args ua;
   1725 	caddr_t sg;
   1726 
   1727 	NETBSD32TOP_UAP(path, const char);
   1728 	NETBSD32TOP_UAP(buf, char);
   1729 	NETBSD32TOX_UAP(count, size_t);
   1730 	sg = stackgap_init(p->p_emul);
   1731 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1732 
   1733 	return (sys_readlink(p, &ua, retval));
   1734 }
   1735 
   1736 /*
   1737  * Need to completly reimplement this syscall due to argument copying.
   1738  */
   1739 int
   1740 netbsd32_execve(p, v, retval)
   1741 	struct proc *p;
   1742 	void *v;
   1743 	register_t *retval;
   1744 {
   1745 	struct netbsd32_execve_args /* {
   1746 		syscallarg(const netbsd32_charp) path;
   1747 		syscallarg(netbsd32_charpp) argp;
   1748 		syscallarg(netbsd32_charpp) envp;
   1749 	} */ *uap = v;
   1750 	struct sys_execve_args ua;
   1751 	caddr_t sg;
   1752 	/* Function args */
   1753 	int error, i;
   1754 	struct exec_package pack;
   1755 	struct nameidata nid;
   1756 	struct vattr attr;
   1757 	struct ucred *cred = p->p_ucred;
   1758 	char *argp;
   1759 	netbsd32_charp const *cpp;
   1760 	char *dp;
   1761 	netbsd32_charp sp;
   1762 	long argc, envc;
   1763 	size_t len;
   1764 	char *stack;
   1765 	struct ps_strings arginfo;
   1766 	struct vmspace *vm;
   1767 	char **tmpfap;
   1768 	int szsigcode;
   1769 	extern struct emul emul_netbsd;
   1770 
   1771 
   1772 	NETBSD32TOP_UAP(path, const char);
   1773 	NETBSD32TOP_UAP(argp, char *);
   1774 	NETBSD32TOP_UAP(envp, char *);
   1775 	sg = stackgap_init(p->p_emul);
   1776 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
   1777 
   1778 	/*
   1779 	 * figure out the maximum size of an exec header, if necessary.
   1780 	 * XXX should be able to keep LKM code from modifying exec switch
   1781 	 * when we're still using it, but...
   1782 	 */
   1783 	if (exec_maxhdrsz == 0) {
   1784 		for (i = 0; i < nexecs; i++)
   1785 			if (execsw[i].es_check != NULL
   1786 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
   1787 				exec_maxhdrsz = execsw[i].es_hdrsz;
   1788 	}
   1789 
   1790 	/* init the namei data to point the file user's program name */
   1791 	/* XXX cgd 960926: why do this here?  most will be clobbered. */
   1792 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(&ua, path), p);
   1793 
   1794 	/*
   1795 	 * initialize the fields of the exec package.
   1796 	 */
   1797 	pack.ep_name = SCARG(&ua, path);
   1798 	MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
   1799 	pack.ep_hdrlen = exec_maxhdrsz;
   1800 	pack.ep_hdrvalid = 0;
   1801 	pack.ep_ndp = &nid;
   1802 	pack.ep_emul_arg = NULL;
   1803 	pack.ep_vmcmds.evs_cnt = 0;
   1804 	pack.ep_vmcmds.evs_used = 0;
   1805 	pack.ep_vap = &attr;
   1806 	pack.ep_emul = &emul_netbsd;
   1807 	pack.ep_flags = 0;
   1808 
   1809 	/* see if we can run it. */
   1810 	if ((error = check_exec(p, &pack)) != 0)
   1811 		goto freehdr;
   1812 
   1813 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
   1814 
   1815 	/* allocate an argument buffer */
   1816 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
   1817 #ifdef DIAGNOSTIC
   1818 	if (argp == (vaddr_t) 0)
   1819 		panic("execve: argp == NULL");
   1820 #endif
   1821 	dp = argp;
   1822 	argc = 0;
   1823 
   1824 	/* copy the fake args list, if there's one, freeing it as we go */
   1825 	if (pack.ep_flags & EXEC_HASARGL) {
   1826 		tmpfap = pack.ep_fa;
   1827 		while (*tmpfap != NULL) {
   1828 			char *cp;
   1829 
   1830 			cp = *tmpfap;
   1831 			while (*cp)
   1832 				*dp++ = *cp++;
   1833 			dp++;
   1834 
   1835 			FREE(*tmpfap, M_EXEC);
   1836 			tmpfap++; argc++;
   1837 		}
   1838 		FREE(pack.ep_fa, M_EXEC);
   1839 		pack.ep_flags &= ~EXEC_HASARGL;
   1840 	}
   1841 
   1842 	/* Now get argv & environment */
   1843 	if (!(cpp = (netbsd32_charp *)SCARG(&ua, argp))) {
   1844 		error = EINVAL;
   1845 		goto bad;
   1846 	}
   1847 
   1848 	if (pack.ep_flags & EXEC_SKIPARG)
   1849 		cpp++;
   1850 
   1851 	while (1) {
   1852 		len = argp + ARG_MAX - dp;
   1853 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
   1854 			goto bad;
   1855 		if (!sp)
   1856 			break;
   1857 		if ((error = copyinstr((char *)(u_long)sp, dp,
   1858 				       len, &len)) != 0) {
   1859 			if (error == ENAMETOOLONG)
   1860 				error = E2BIG;
   1861 			goto bad;
   1862 		}
   1863 		dp += len;
   1864 		cpp++;
   1865 		argc++;
   1866 	}
   1867 
   1868 	envc = 0;
   1869 	/* environment need not be there */
   1870 	if ((cpp = (netbsd32_charp *)SCARG(&ua, envp)) != NULL ) {
   1871 		while (1) {
   1872 			len = argp + ARG_MAX - dp;
   1873 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
   1874 				goto bad;
   1875 			if (!sp)
   1876 				break;
   1877 			if ((error = copyinstr((char *)(u_long)sp,
   1878 					       dp, len, &len)) != 0) {
   1879 				if (error == ENAMETOOLONG)
   1880 					error = E2BIG;
   1881 				goto bad;
   1882 			}
   1883 			dp += len;
   1884 			cpp++;
   1885 			envc++;
   1886 		}
   1887 	}
   1888 
   1889 	dp = (char *) ALIGN(dp);
   1890 
   1891 	szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
   1892 
   1893 	/* Now check if args & environ fit into new stack */
   1894 	if (pack.ep_flags & EXEC_32)
   1895 		len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(int) +
   1896 		       sizeof(int) + dp + STACKGAPLEN + szsigcode +
   1897 		       sizeof(struct ps_strings)) - argp;
   1898 	else
   1899 		len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
   1900 		       sizeof(int) + dp + STACKGAPLEN + szsigcode +
   1901 		       sizeof(struct ps_strings)) - argp;
   1902 
   1903 	len = ALIGN(len);	/* make the stack "safely" aligned */
   1904 
   1905 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
   1906 		error = ENOMEM;
   1907 		goto bad;
   1908 	}
   1909 
   1910 	/* adjust "active stack depth" for process VSZ */
   1911 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
   1912 
   1913 	/*
   1914 	 * Do whatever is necessary to prepare the address space
   1915 	 * for remapping.  Note that this might replace the current
   1916 	 * vmspace with another!
   1917 	 */
   1918 	uvmspace_exec(p);
   1919 
   1920 	/* Now map address space */
   1921 	vm = p->p_vmspace;
   1922 	vm->vm_taddr = (char *) pack.ep_taddr;
   1923 	vm->vm_tsize = btoc(pack.ep_tsize);
   1924 	vm->vm_daddr = (char *) pack.ep_daddr;
   1925 	vm->vm_dsize = btoc(pack.ep_dsize);
   1926 	vm->vm_ssize = btoc(pack.ep_ssize);
   1927 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
   1928 
   1929 	/* create the new process's VM space by running the vmcmds */
   1930 #ifdef DIAGNOSTIC
   1931 	if (pack.ep_vmcmds.evs_used == 0)
   1932 		panic("execve: no vmcmds");
   1933 #endif
   1934 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
   1935 		struct exec_vmcmd *vcp;
   1936 
   1937 		vcp = &pack.ep_vmcmds.evs_cmds[i];
   1938 		error = (*vcp->ev_proc)(p, vcp);
   1939 	}
   1940 
   1941 	/* free the vmspace-creation commands, and release their references */
   1942 	kill_vmcmds(&pack.ep_vmcmds);
   1943 
   1944 	/* if an error happened, deallocate and punt */
   1945 	if (error)
   1946 		goto exec_abort;
   1947 
   1948 	/* remember information about the process */
   1949 	arginfo.ps_nargvstr = argc;
   1950 	arginfo.ps_nenvstr = envc;
   1951 
   1952 	stack = (char *) (USRSTACK - len);
   1953 	/* Now copy argc, args & environ to new stack */
   1954 	if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
   1955 		goto exec_abort;
   1956 
   1957 	/* copy out the process's ps_strings structure */
   1958 	if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
   1959 		goto exec_abort;
   1960 
   1961 	/* copy out the process's signal trapoline code */
   1962 	if (szsigcode) {
   1963 		if (copyout((char *)pack.ep_emul->e_sigcode,
   1964 		    p->p_sigacts->ps_sigcode = (char *)PS_STRINGS - szsigcode,
   1965 		    szsigcode))
   1966 			goto exec_abort;
   1967 #ifdef PMAP_NEED_PROCWR
   1968 		/* This is code. Let the pmap do what is needed. */
   1969 		pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode);
   1970 #endif
   1971 	}
   1972 
   1973 	stopprofclock(p);	/* stop profiling */
   1974 	fdcloseexec(p);		/* handle close on exec */
   1975 	execsigs(p);		/* reset catched signals */
   1976 	p->p_ctxlink = NULL;	/* reset ucontext link */
   1977 
   1978 	/* set command name & other accounting info */
   1979 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
   1980 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
   1981 	p->p_comm[len] = 0;
   1982 	p->p_acflag &= ~AFORK;
   1983 
   1984 	/* record proc's vnode, for use by procfs and others */
   1985         if (p->p_textvp)
   1986                 vrele(p->p_textvp);
   1987 	VREF(pack.ep_vp);
   1988 	p->p_textvp = pack.ep_vp;
   1989 
   1990 	p->p_flag |= P_EXEC;
   1991 	if (p->p_flag & P_PPWAIT) {
   1992 		p->p_flag &= ~P_PPWAIT;
   1993 		wakeup((caddr_t) p->p_pptr);
   1994 	}
   1995 
   1996 	/*
   1997 	 * deal with set[ug]id.
   1998 	 * MNT_NOSUID and P_TRACED have already been used to disable s[ug]id.
   1999 	 */
   2000 	if (((attr.va_mode & S_ISUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
   2001 	 || ((attr.va_mode & S_ISGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
   2002 		p->p_ucred = crcopy(cred);
   2003 #ifdef KTRACE
   2004 		/*
   2005 		 * If process is being ktraced, turn off - unless
   2006 		 * root set it.
   2007 		 */
   2008 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
   2009 			ktrderef(p);
   2010 #endif
   2011 		if (attr.va_mode & S_ISUID)
   2012 			p->p_ucred->cr_uid = attr.va_uid;
   2013 		if (attr.va_mode & S_ISGID)
   2014 			p->p_ucred->cr_gid = attr.va_gid;
   2015 		p_sugid(p);
   2016 	} else
   2017 		p->p_flag &= ~P_SUGID;
   2018 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
   2019 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
   2020 
   2021 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2022 
   2023 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2024 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2025 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2026 	vput(pack.ep_vp);
   2027 
   2028 	/* setup new registers and do misc. setup. */
   2029 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack);
   2030 
   2031 	if (p->p_flag & P_TRACED)
   2032 		psignal(p, SIGTRAP);
   2033 
   2034 	p->p_emul = pack.ep_emul;
   2035 	FREE(pack.ep_hdr, M_EXEC);
   2036 
   2037 #ifdef KTRACE
   2038 	if (KTRPOINT(p, KTR_EMUL))
   2039 		ktremul(p);
   2040 #endif
   2041 
   2042 	return (EJUSTRETURN);
   2043 
   2044 bad:
   2045 	/* free the vmspace-creation commands, and release their references */
   2046 	kill_vmcmds(&pack.ep_vmcmds);
   2047 	/* kill any opened file descriptor, if necessary */
   2048 	if (pack.ep_flags & EXEC_HASFD) {
   2049 		pack.ep_flags &= ~EXEC_HASFD;
   2050 		(void) fdrelease(p, pack.ep_fd);
   2051 	}
   2052 	/* close and put the exec'd file */
   2053 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2054 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2055 	vput(pack.ep_vp);
   2056 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2057 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2058 
   2059 freehdr:
   2060 	FREE(pack.ep_hdr, M_EXEC);
   2061 	return error;
   2062 
   2063 exec_abort:
   2064 	/*
   2065 	 * the old process doesn't exist anymore.  exit gracefully.
   2066 	 * get rid of the (new) address space we have created, if any, get rid
   2067 	 * of our namei data and vnode, and exit noting failure
   2068 	 */
   2069 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
   2070 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
   2071 	if (pack.ep_emul_arg)
   2072 		FREE(pack.ep_emul_arg, M_TEMP);
   2073 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
   2074 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
   2075 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
   2076 	vput(pack.ep_vp);
   2077 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
   2078 	FREE(pack.ep_hdr, M_EXEC);
   2079 	exit1(p, W_EXITCODE(0, SIGABRT));
   2080 	exit1(p, -1);
   2081 
   2082 	/* NOTREACHED */
   2083 	return 0;
   2084 }
   2085 
   2086 int
   2087 netbsd32_umask(p, v, retval)
   2088 	struct proc *p;
   2089 	void *v;
   2090 	register_t *retval;
   2091 {
   2092 	struct netbsd32_umask_args /* {
   2093 		syscallarg(mode_t) newmask;
   2094 	} */ *uap = v;
   2095 	struct sys_umask_args ua;
   2096 
   2097 	NETBSD32TO64_UAP(newmask);
   2098 	return (sys_umask(p, &ua, retval));
   2099 }
   2100 
   2101 int
   2102 netbsd32_chroot(p, v, retval)
   2103 	struct proc *p;
   2104 	void *v;
   2105 	register_t *retval;
   2106 {
   2107 	struct netbsd32_chroot_args /* {
   2108 		syscallarg(const netbsd32_charp) path;
   2109 	} */ *uap = v;
   2110 	struct sys_chroot_args ua;
   2111 
   2112 	NETBSD32TOP_UAP(path, const char);
   2113 	return (sys_chroot(p, &ua, retval));
   2114 }
   2115 
   2116 int
   2117 netbsd32_sbrk(p, v, retval)
   2118 	struct proc *p;
   2119 	void *v;
   2120 	register_t *retval;
   2121 {
   2122 	struct netbsd32_sbrk_args /* {
   2123 		syscallarg(int) incr;
   2124 	} */ *uap = v;
   2125 	struct sys_sbrk_args ua;
   2126 
   2127 	NETBSD32TO64_UAP(incr);
   2128 	return (sys_sbrk(p, &ua, retval));
   2129 }
   2130 
   2131 int
   2132 netbsd32_sstk(p, v, retval)
   2133 	struct proc *p;
   2134 	void *v;
   2135 	register_t *retval;
   2136 {
   2137 	struct netbsd32_sstk_args /* {
   2138 		syscallarg(int) incr;
   2139 	} */ *uap = v;
   2140 	struct sys_sstk_args ua;
   2141 
   2142 	NETBSD32TO64_UAP(incr);
   2143 	return (sys_sstk(p, &ua, retval));
   2144 }
   2145 
   2146 int
   2147 netbsd32_munmap(p, v, retval)
   2148 	struct proc *p;
   2149 	void *v;
   2150 	register_t *retval;
   2151 {
   2152 	struct netbsd32_munmap_args /* {
   2153 		syscallarg(netbsd32_voidp) addr;
   2154 		syscallarg(netbsd32_size_t) len;
   2155 	} */ *uap = v;
   2156 	struct sys_munmap_args ua;
   2157 
   2158 	NETBSD32TOP_UAP(addr, void);
   2159 	NETBSD32TOX_UAP(len, size_t);
   2160 	return (sys_munmap(p, &ua, retval));
   2161 }
   2162 
   2163 int
   2164 netbsd32_mprotect(p, v, retval)
   2165 	struct proc *p;
   2166 	void *v;
   2167 	register_t *retval;
   2168 {
   2169 	struct netbsd32_mprotect_args /* {
   2170 		syscallarg(netbsd32_voidp) addr;
   2171 		syscallarg(netbsd32_size_t) len;
   2172 		syscallarg(int) prot;
   2173 	} */ *uap = v;
   2174 	struct sys_mprotect_args ua;
   2175 
   2176 	NETBSD32TOP_UAP(addr, void);
   2177 	NETBSD32TOX_UAP(len, size_t);
   2178 	NETBSD32TO64_UAP(prot);
   2179 	return (sys_mprotect(p, &ua, retval));
   2180 }
   2181 
   2182 int
   2183 netbsd32_madvise(p, v, retval)
   2184 	struct proc *p;
   2185 	void *v;
   2186 	register_t *retval;
   2187 {
   2188 	struct netbsd32_madvise_args /* {
   2189 		syscallarg(netbsd32_voidp) addr;
   2190 		syscallarg(netbsd32_size_t) len;
   2191 		syscallarg(int) behav;
   2192 	} */ *uap = v;
   2193 	struct sys_madvise_args ua;
   2194 
   2195 	NETBSD32TOP_UAP(addr, void);
   2196 	NETBSD32TOX_UAP(len, size_t);
   2197 	NETBSD32TO64_UAP(behav);
   2198 	return (sys_madvise(p, &ua, retval));
   2199 }
   2200 
   2201 int
   2202 netbsd32_mincore(p, v, retval)
   2203 	struct proc *p;
   2204 	void *v;
   2205 	register_t *retval;
   2206 {
   2207 	struct netbsd32_mincore_args /* {
   2208 		syscallarg(netbsd32_caddr_t) addr;
   2209 		syscallarg(netbsd32_size_t) len;
   2210 		syscallarg(netbsd32_charp) vec;
   2211 	} */ *uap = v;
   2212 	struct sys_mincore_args ua;
   2213 
   2214 	NETBSD32TOX64_UAP(addr, caddr_t);
   2215 	NETBSD32TOX_UAP(len, size_t);
   2216 	NETBSD32TOP_UAP(vec, char);
   2217 	return (sys_mincore(p, &ua, retval));
   2218 }
   2219 
   2220 int
   2221 netbsd32_getgroups(p, v, retval)
   2222 	struct proc *p;
   2223 	void *v;
   2224 	register_t *retval;
   2225 {
   2226 	struct netbsd32_getgroups_args /* {
   2227 		syscallarg(int) gidsetsize;
   2228 		syscallarg(netbsd32_gid_tp) gidset;
   2229 	} */ *uap = v;
   2230 	struct pcred *pc = p->p_cred;
   2231 	int ngrp;
   2232 	int error;
   2233 
   2234 	ngrp = SCARG(uap, gidsetsize);
   2235 	if (ngrp == 0) {
   2236 		*retval = pc->pc_ucred->cr_ngroups;
   2237 		return (0);
   2238 	}
   2239 	if (ngrp < pc->pc_ucred->cr_ngroups)
   2240 		return (EINVAL);
   2241 	ngrp = pc->pc_ucred->cr_ngroups;
   2242 	/* Should convert gid_t to netbsd32_gid_t, but they're the same */
   2243 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
   2244 			(caddr_t)(u_long)SCARG(uap, gidset),
   2245 			ngrp * sizeof(gid_t));
   2246 	if (error)
   2247 		return (error);
   2248 	*retval = ngrp;
   2249 	return (0);
   2250 }
   2251 
   2252 int
   2253 netbsd32_setgroups(p, v, retval)
   2254 	struct proc *p;
   2255 	void *v;
   2256 	register_t *retval;
   2257 {
   2258 	struct netbsd32_setgroups_args /* {
   2259 		syscallarg(int) gidsetsize;
   2260 		syscallarg(const netbsd32_gid_tp) gidset;
   2261 	} */ *uap = v;
   2262 	struct sys_setgroups_args ua;
   2263 
   2264 	NETBSD32TO64_UAP(gidsetsize);
   2265 	NETBSD32TOP_UAP(gidset, gid_t);
   2266 	return (sys_setgroups(p, &ua, retval));
   2267 }
   2268 
   2269 int
   2270 netbsd32_setpgid(p, v, retval)
   2271 	struct proc *p;
   2272 	void *v;
   2273 	register_t *retval;
   2274 {
   2275 	struct netbsd32_setpgid_args /* {
   2276 		syscallarg(int) pid;
   2277 		syscallarg(int) pgid;
   2278 	} */ *uap = v;
   2279 	struct sys_setpgid_args ua;
   2280 
   2281 	NETBSD32TO64_UAP(pid);
   2282 	NETBSD32TO64_UAP(pgid);
   2283 	return (sys_setpgid(p, &ua, retval));
   2284 }
   2285 
   2286 int
   2287 netbsd32_setitimer(p, v, retval)
   2288 	struct proc *p;
   2289 	void *v;
   2290 	register_t *retval;
   2291 {
   2292 	struct netbsd32_setitimer_args /* {
   2293 		syscallarg(int) which;
   2294 		syscallarg(const netbsd32_itimervalp_t) itv;
   2295 		syscallarg(netbsd32_itimervalp_t) oitv;
   2296 	} */ *uap = v;
   2297 	struct netbsd32_itimerval s32it, *itvp;
   2298 	int which = SCARG(uap, which);
   2299 	struct netbsd32_getitimer_args getargs;
   2300 	struct itimerval aitv;
   2301 	int s, error;
   2302 
   2303 	if ((u_int)which > ITIMER_PROF)
   2304 		return (EINVAL);
   2305 	itvp = (struct netbsd32_itimerval *)(u_long)SCARG(uap, itv);
   2306 	if (itvp && (error = copyin(itvp, &s32it, sizeof(s32it))))
   2307 		return (error);
   2308 	netbsd32_to_itimerval(&s32it, &aitv);
   2309 	if (SCARG(uap, oitv) != NULL) {
   2310 		SCARG(&getargs, which) = which;
   2311 		SCARG(&getargs, itv) = SCARG(uap, oitv);
   2312 		if ((error = netbsd32_getitimer(p, &getargs, retval)) != 0)
   2313 			return (error);
   2314 	}
   2315 	if (itvp == 0)
   2316 		return (0);
   2317 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
   2318 		return (EINVAL);
   2319 	s = splclock();
   2320 	if (which == ITIMER_REAL) {
   2321 		callout_stop(&p->p_realit_ch);
   2322 		if (timerisset(&aitv.it_value)) {
   2323 			/*
   2324 			 * Don't need to check hzto() return value, here.
   2325 			 * callout_reset() does it for us.
   2326 			 */
   2327 			timeradd(&aitv.it_value, &time, &aitv.it_value);
   2328 			callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
   2329 			    realitexpire, p);
   2330 		}
   2331 		p->p_realtimer = aitv;
   2332 	} else
   2333 		p->p_stats->p_timer[which] = aitv;
   2334 	splx(s);
   2335 	return (0);
   2336 }
   2337 
   2338 int
   2339 netbsd32_getitimer(p, v, retval)
   2340 	struct proc *p;
   2341 	void *v;
   2342 	register_t *retval;
   2343 {
   2344 	struct netbsd32_getitimer_args /* {
   2345 		syscallarg(int) which;
   2346 		syscallarg(netbsd32_itimervalp_t) itv;
   2347 	} */ *uap = v;
   2348 	int which = SCARG(uap, which);
   2349 	struct netbsd32_itimerval s32it;
   2350 	struct itimerval aitv;
   2351 	int s;
   2352 
   2353 	if ((u_int)which > ITIMER_PROF)
   2354 		return (EINVAL);
   2355 	s = splclock();
   2356 	if (which == ITIMER_REAL) {
   2357 		/*
   2358 		 * Convert from absolute to relative time in .it_value
   2359 		 * part of real time timer.  If time for real time timer
   2360 		 * has passed return 0, else return difference between
   2361 		 * current time and time for the timer to go off.
   2362 		 */
   2363 		aitv = p->p_realtimer;
   2364 		if (timerisset(&aitv.it_value)) {
   2365 			if (timercmp(&aitv.it_value, &time, <))
   2366 				timerclear(&aitv.it_value);
   2367 			else
   2368 				timersub(&aitv.it_value, &time, &aitv.it_value);
   2369 		}
   2370 	} else
   2371 		aitv = p->p_stats->p_timer[which];
   2372 	splx(s);
   2373 	netbsd32_from_itimerval(&aitv, &s32it);
   2374 	return (copyout(&s32it, (caddr_t)(u_long)SCARG(uap, itv), sizeof(s32it)));
   2375 }
   2376 
   2377 int
   2378 netbsd32_fcntl(p, v, retval)
   2379 	struct proc *p;
   2380 	void *v;
   2381 	register_t *retval;
   2382 {
   2383 	struct netbsd32_fcntl_args /* {
   2384 		syscallarg(int) fd;
   2385 		syscallarg(int) cmd;
   2386 		syscallarg(netbsd32_voidp) arg;
   2387 	} */ *uap = v;
   2388 	struct sys_fcntl_args ua;
   2389 
   2390 	NETBSD32TO64_UAP(fd);
   2391 	NETBSD32TO64_UAP(cmd);
   2392 	NETBSD32TOP_UAP(arg, void);
   2393 	/* XXXX we can do this 'cause flock doesn't change */
   2394 	return (sys_fcntl(p, &ua, retval));
   2395 }
   2396 
   2397 int
   2398 netbsd32_dup2(p, v, retval)
   2399 	struct proc *p;
   2400 	void *v;
   2401 	register_t *retval;
   2402 {
   2403 	struct netbsd32_dup2_args /* {
   2404 		syscallarg(int) from;
   2405 		syscallarg(int) to;
   2406 	} */ *uap = v;
   2407 	struct sys_dup2_args ua;
   2408 
   2409 	NETBSD32TO64_UAP(from);
   2410 	NETBSD32TO64_UAP(to);
   2411 	return (sys_dup2(p, &ua, retval));
   2412 }
   2413 
   2414 int
   2415 netbsd32_select(p, v, retval)
   2416 	struct proc *p;
   2417 	void *v;
   2418 	register_t *retval;
   2419 {
   2420 	struct netbsd32_select_args /* {
   2421 		syscallarg(int) nd;
   2422 		syscallarg(netbsd32_fd_setp_t) in;
   2423 		syscallarg(netbsd32_fd_setp_t) ou;
   2424 		syscallarg(netbsd32_fd_setp_t) ex;
   2425 		syscallarg(netbsd32_timevalp_t) tv;
   2426 	} */ *uap = v;
   2427 /* This one must be done in-line 'cause of the timeval */
   2428 	struct netbsd32_timeval tv32;
   2429 	caddr_t bits;
   2430 	char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
   2431 	struct timeval atv;
   2432 	int s, ncoll, error = 0, timo;
   2433 	size_t ni;
   2434 	extern int	selwait, nselcoll;
   2435 	extern int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
   2436 
   2437 	if (SCARG(uap, nd) < 0)
   2438 		return (EINVAL);
   2439 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
   2440 		/* forgiving; slightly wrong */
   2441 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
   2442 	}
   2443 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
   2444 	if (ni * 6 > sizeof(smallbits))
   2445 		bits = malloc(ni * 6, M_TEMP, M_WAITOK);
   2446 	else
   2447 		bits = smallbits;
   2448 
   2449 #define	getbits(name, x) \
   2450 	if (SCARG(uap, name)) { \
   2451 		error = copyin((caddr_t)(u_long)SCARG(uap, name), bits + ni * x, ni); \
   2452 		if (error) \
   2453 			goto done; \
   2454 	} else \
   2455 		memset(bits + ni * x, 0, ni);
   2456 	getbits(in, 0);
   2457 	getbits(ou, 1);
   2458 	getbits(ex, 2);
   2459 #undef	getbits
   2460 
   2461 	if (SCARG(uap, tv)) {
   2462 		error = copyin((caddr_t)(u_long)SCARG(uap, tv), (caddr_t)&tv32,
   2463 			sizeof(tv32));
   2464 		if (error)
   2465 			goto done;
   2466 		netbsd32_to_timeval(&tv32, &atv);
   2467 		if (itimerfix(&atv)) {
   2468 			error = EINVAL;
   2469 			goto done;
   2470 		}
   2471 		s = splclock();
   2472 		timeradd(&atv, &time, &atv);
   2473 		splx(s);
   2474 	} else
   2475 		timo = 0;
   2476 retry:
   2477 	ncoll = nselcoll;
   2478 	p->p_flag |= P_SELECT;
   2479 	error = selscan(p, (fd_mask *)(bits + ni * 0),
   2480 			   (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
   2481 	if (error || *retval)
   2482 		goto done;
   2483 	if (SCARG(uap, tv)) {
   2484 		/*
   2485 		 * We have to recalculate the timeout on every retry.
   2486 		 */
   2487 		timo = hzto(&atv);
   2488 		if (timo <= 0)
   2489 			goto done;
   2490 	}
   2491 	s = splhigh();
   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