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