xref: /netbsd-src/sys/compat/netbsd32/netbsd32_netbsd.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: netbsd32_netbsd.c,v 1.28 2000/05/30 20:54:26 eeh Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include "opt_ddb.h"
32 #include "opt_ktrace.h"
33 #include "opt_ntp.h"
34 #include "opt_compat_netbsd.h"
35 #include "opt_compat_freebsd.h"
36 #include "opt_compat_linux.h"
37 #include "opt_compat_sunos.h"
38 #include "opt_compat_43.h"
39 #include "opt_sysv.h"
40 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_LINUX) || \
41     defined(COMPAT_FREEBSD)
42 #define COMPAT_OLDSOCK /* used by <sys/socket.h> */
43 #endif
44 
45 #include "fs_lfs.h"
46 #include "fs_nfs.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h>
52 #include <sys/ipc.h>
53 #include <sys/msg.h>
54 #define msg __msg /* Don't ask me! */
55 #include <sys/sem.h>
56 #include <sys/shm.h>
57 #include <sys/malloc.h>
58 #include <sys/mount.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/socketvar.h>
62 #include <sys/mbuf.h>
63 #include <sys/stat.h>
64 #include <sys/time.h>
65 #include <sys/timex.h>
66 #include <sys/signalvar.h>
67 #include <sys/wait.h>
68 #include <sys/ptrace.h>
69 #include <sys/ktrace.h>
70 #include <sys/trace.h>
71 #include <sys/resourcevar.h>
72 #include <sys/pool.h>
73 #include <sys/vnode.h>
74 #include <sys/file.h>
75 #include <sys/filedesc.h>
76 #include <sys/namei.h>
77 
78 #include <vm/vm.h>
79 #include <vm/vm_kern.h>
80 #include <sys/syscallargs.h>
81 #include <sys/proc.h>
82 #include <sys/acct.h>
83 #include <sys/exec.h>
84 #include <sys/sysctl.h>
85 
86 #include <net/if.h>
87 
88 #include <compat/netbsd32/netbsd32.h>
89 #include <compat/netbsd32/netbsd32_syscallargs.h>
90 
91 #include <machine/frame.h>
92 
93 #if defined(DDB)
94 #include <ddb/ddbvar.h>
95 #endif
96 
97 static __inline void netbsd32_from_timeval __P((struct timeval *, struct netbsd32_timeval *));
98 static __inline void netbsd32_to_timeval __P((struct netbsd32_timeval *, struct timeval *));
99 static __inline void netbsd32_from_itimerval __P((struct itimerval *, struct netbsd32_itimerval *));
100 static __inline void netbsd32_to_itimerval __P((struct netbsd32_itimerval *, struct itimerval *));
101 static __inline void netbsd32_to_timespec __P((struct netbsd32_timespec *, struct timespec *));
102 static __inline void netbsd32_from_timespec __P((struct timespec *, struct netbsd32_timespec *));
103 static __inline void netbsd32_from_rusage __P((struct rusage *, struct netbsd32_rusage *));
104 static __inline void netbsd32_to_rusage __P((struct netbsd32_rusage *, struct rusage *));
105 static __inline int netbsd32_to_iovecin __P((struct netbsd32_iovec *, struct iovec *, int));
106 static __inline void netbsd32_to_msghdr __P((struct netbsd32_msghdr *, struct msghdr *));
107 static __inline void netbsd32_from_msghdr __P((struct netbsd32_msghdr *, struct msghdr *));
108 static __inline void netbsd32_from_statfs __P((struct statfs *, struct netbsd32_statfs *));
109 static __inline void netbsd32_from_timex __P((struct timex *, struct netbsd32_timex *));
110 static __inline void netbsd32_to_timex __P((struct netbsd32_timex *, struct timex *));
111 static __inline void netbsd32_from___stat13 __P((struct stat *, struct netbsd32_stat *));
112 static __inline void netbsd32_to_ipc_perm __P((struct netbsd32_ipc_perm *, struct ipc_perm *));
113 static __inline void netbsd32_from_ipc_perm __P((struct ipc_perm *, struct netbsd32_ipc_perm *));
114 static __inline void netbsd32_to_msg __P((struct netbsd32_msg *, struct msg *));
115 static __inline void netbsd32_from_msg __P((struct msg *, struct netbsd32_msg *));
116 static __inline void netbsd32_to_msqid_ds __P((struct netbsd32_msqid_ds *, struct msqid_ds *));
117 static __inline void netbsd32_from_msqid_ds __P((struct msqid_ds *, struct netbsd32_msqid_ds *));
118 static __inline void netbsd32_to_shmid_ds __P((struct netbsd32_shmid_ds *, struct shmid_ds *));
119 static __inline void netbsd32_from_shmid_ds __P((struct shmid_ds *, struct netbsd32_shmid_ds *));
120 static __inline void netbsd32_to_semid_ds __P((struct  netbsd32_semid_ds *, struct  semid_ds *));
121 static __inline void netbsd32_from_semid_ds __P((struct  semid_ds *, struct  netbsd32_semid_ds *));
122 
123 
124 static int recvit32 __P((struct proc *, int, struct netbsd32_msghdr *, struct iovec *, caddr_t,
125 			 register_t *));
126 static int dofilereadv32 __P((struct proc *, int, struct file *, struct netbsd32_iovec *,
127 			      int, off_t *, int, register_t *));
128 static int dofilewritev32 __P((struct proc *, int, struct file *, struct netbsd32_iovec *,
129 			       int,  off_t *, int, register_t *));
130 static int change_utimes32 __P((struct vnode *, struct timeval *, struct proc *));
131 
132 /* converters for structures that we need */
133 static __inline void
134 netbsd32_from_timeval(tv, tv32)
135 	struct timeval *tv;
136 	struct netbsd32_timeval *tv32;
137 {
138 
139 	tv32->tv_sec = (netbsd32_long)tv->tv_sec;
140 	tv32->tv_usec = (netbsd32_long)tv->tv_usec;
141 }
142 
143 static __inline void
144 netbsd32_to_timeval(tv32, tv)
145 	struct netbsd32_timeval *tv32;
146 	struct timeval *tv;
147 {
148 
149 	tv->tv_sec = (long)tv32->tv_sec;
150 	tv->tv_usec = (long)tv32->tv_usec;
151 }
152 
153 static __inline void
154 netbsd32_from_itimerval(itv, itv32)
155 	struct itimerval *itv;
156 	struct netbsd32_itimerval *itv32;
157 {
158 
159 	netbsd32_from_timeval(&itv->it_interval,
160 			     &itv32->it_interval);
161 	netbsd32_from_timeval(&itv->it_value,
162 			     &itv32->it_value);
163 }
164 
165 static __inline void
166 netbsd32_to_itimerval(itv32, itv)
167 	struct netbsd32_itimerval *itv32;
168 	struct itimerval *itv;
169 {
170 
171 	netbsd32_to_timeval(&itv32->it_interval, &itv->it_interval);
172 	netbsd32_to_timeval(&itv32->it_value, &itv->it_value);
173 }
174 
175 static __inline void
176 netbsd32_to_timespec(s32p, p)
177 	struct netbsd32_timespec *s32p;
178 	struct timespec *p;
179 {
180 
181 	p->tv_sec = (time_t)s32p->tv_sec;
182 	p->tv_nsec = (long)s32p->tv_nsec;
183 }
184 
185 static __inline void
186 netbsd32_from_timespec(p, s32p)
187 	struct timespec *p;
188 	struct netbsd32_timespec *s32p;
189 {
190 
191 	s32p->tv_sec = (netbsd32_time_t)p->tv_sec;
192 	s32p->tv_nsec = (netbsd32_long)p->tv_nsec;
193 }
194 
195 static __inline void
196 netbsd32_from_rusage(rup, ru32p)
197 	struct rusage *rup;
198 	struct netbsd32_rusage *ru32p;
199 {
200 
201 	netbsd32_from_timeval(&rup->ru_utime, &ru32p->ru_utime);
202 	netbsd32_from_timeval(&rup->ru_stime, &ru32p->ru_stime);
203 #define C(var)	ru32p->var = (netbsd32_long)rup->var
204 	C(ru_maxrss);
205 	C(ru_ixrss);
206 	C(ru_idrss);
207 	C(ru_isrss);
208 	C(ru_minflt);
209 	C(ru_majflt);
210 	C(ru_nswap);
211 	C(ru_inblock);
212 	C(ru_oublock);
213 	C(ru_msgsnd);
214 	C(ru_msgrcv);
215 	C(ru_nsignals);
216 	C(ru_nvcsw);
217 	C(ru_nivcsw);
218 #undef C
219 }
220 
221 static __inline void
222 netbsd32_to_rusage(ru32p, rup)
223 	struct netbsd32_rusage *ru32p;
224 	struct rusage *rup;
225 {
226 
227 	netbsd32_to_timeval(&ru32p->ru_utime, &rup->ru_utime);
228 	netbsd32_to_timeval(&ru32p->ru_stime, &rup->ru_stime);
229 #define C(var)	rup->var = (long)ru32p->var
230 	C(ru_maxrss);
231 	C(ru_ixrss);
232 	C(ru_idrss);
233 	C(ru_isrss);
234 	C(ru_minflt);
235 	C(ru_majflt);
236 	C(ru_nswap);
237 	C(ru_inblock);
238 	C(ru_oublock);
239 	C(ru_msgsnd);
240 	C(ru_msgrcv);
241 	C(ru_nsignals);
242 	C(ru_nvcsw);
243 	C(ru_nivcsw);
244 #undef C
245 }
246 
247 static __inline int
248 netbsd32_to_iovecin(iov32p, iovp, len)
249 	struct netbsd32_iovec *iov32p;
250 	struct iovec *iovp;
251 	int len;
252 {
253 	int i, error=0;
254 	u_int32_t iov_base;
255 	u_int32_t iov_len;
256 	/*
257 	 * We could allocate an iov32p, do a copyin, and translate
258 	 * each field and then free it all up, or we could copyin
259 	 * each field separately.  I'm doing the latter to reduce
260 	 * the number of MALLOC()s.
261 	 */
262 	for (i = 0; i < len; i++, iovp++, iov32p++) {
263 		if ((error = copyin((caddr_t)&iov32p->iov_base, &iov_base, sizeof(iov_base))))
264 		    return (error);
265 		if ((error = copyin((caddr_t)&iov32p->iov_len, &iov_len, sizeof(iov_len))))
266 		    return (error);
267 		iovp->iov_base = (void *)(u_long)iov_base;
268 		iovp->iov_len = (size_t)iov_len;
269 	}
270 }
271 
272 /* msg_iov must be done separately */
273 static __inline void
274 netbsd32_to_msghdr(mhp32, mhp)
275 	struct netbsd32_msghdr *mhp32;
276 	struct msghdr *mhp;
277 {
278 
279 	mhp->msg_name = (caddr_t)(u_long)mhp32->msg_name;
280 	mhp->msg_namelen = mhp32->msg_namelen;
281 	mhp->msg_iovlen = (size_t)mhp32->msg_iovlen;
282 	mhp->msg_control = (caddr_t)(u_long)mhp32->msg_control;
283 	mhp->msg_controllen = mhp32->msg_controllen;
284 	mhp->msg_flags = mhp32->msg_flags;
285 }
286 
287 /* msg_iov must be done separately */
288 static __inline void
289 netbsd32_from_msghdr(mhp32, mhp)
290 	struct netbsd32_msghdr *mhp32;
291 	struct msghdr *mhp;
292 {
293 
294 	mhp32->msg_name = mhp32->msg_name;
295 	mhp32->msg_namelen = mhp32->msg_namelen;
296 	mhp32->msg_iovlen = mhp32->msg_iovlen;
297 	mhp32->msg_control = mhp32->msg_control;
298 	mhp32->msg_controllen = mhp->msg_controllen;
299 	mhp32->msg_flags = mhp->msg_flags;
300 }
301 
302 static __inline void
303 netbsd32_from_statfs(sbp, sb32p)
304 	struct statfs *sbp;
305 	struct netbsd32_statfs *sb32p;
306 {
307 	sb32p->f_type = sbp->f_type;
308 	sb32p->f_flags = sbp->f_flags;
309 	sb32p->f_bsize = (netbsd32_long)sbp->f_bsize;
310 	sb32p->f_iosize = (netbsd32_long)sbp->f_iosize;
311 	sb32p->f_blocks = (netbsd32_long)sbp->f_blocks;
312 	sb32p->f_bfree = (netbsd32_long)sbp->f_bfree;
313 	sb32p->f_bavail = (netbsd32_long)sbp->f_bavail;
314 	sb32p->f_files = (netbsd32_long)sbp->f_files;
315 	sb32p->f_ffree = (netbsd32_long)sbp->f_ffree;
316 	sb32p->f_fsid = sbp->f_fsid;
317 	sb32p->f_owner = sbp->f_owner;
318 	sb32p->f_spare[0] = 0;
319 	sb32p->f_spare[1] = 0;
320 	sb32p->f_spare[2] = 0;
321 	sb32p->f_spare[3] = 0;
322 #if 1
323 	/* May as well do the whole batch in one go */
324 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN+MNAMELEN+MNAMELEN);
325 #else
326 	/* If we want to be careful */
327 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, MFSNAMELEN);
328 	memcpy(sb32p->f_mntonname, sbp->f_mntonname, MNAMELEN);
329 	memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, MNAMELEN);
330 #endif
331 }
332 
333 static __inline void
334 netbsd32_from_timex(txp, tx32p)
335 	struct timex *txp;
336 	struct netbsd32_timex *tx32p;
337 {
338 
339 	tx32p->modes = txp->modes;
340 	tx32p->offset = (netbsd32_long)txp->offset;
341 	tx32p->freq = (netbsd32_long)txp->freq;
342 	tx32p->maxerror = (netbsd32_long)txp->maxerror;
343 	tx32p->esterror = (netbsd32_long)txp->esterror;
344 	tx32p->status = txp->status;
345 	tx32p->constant = (netbsd32_long)txp->constant;
346 	tx32p->precision = (netbsd32_long)txp->precision;
347 	tx32p->tolerance = (netbsd32_long)txp->tolerance;
348 	tx32p->ppsfreq = (netbsd32_long)txp->ppsfreq;
349 	tx32p->jitter = (netbsd32_long)txp->jitter;
350 	tx32p->shift = txp->shift;
351 	tx32p->stabil = (netbsd32_long)txp->stabil;
352 	tx32p->jitcnt = (netbsd32_long)txp->jitcnt;
353 	tx32p->calcnt = (netbsd32_long)txp->calcnt;
354 	tx32p->errcnt = (netbsd32_long)txp->errcnt;
355 	tx32p->stbcnt = (netbsd32_long)txp->stbcnt;
356 }
357 
358 static __inline void
359 netbsd32_to_timex(tx32p, txp)
360 	struct netbsd32_timex *tx32p;
361 	struct timex *txp;
362 {
363 
364 	txp->modes = tx32p->modes;
365 	txp->offset = (long)tx32p->offset;
366 	txp->freq = (long)tx32p->freq;
367 	txp->maxerror = (long)tx32p->maxerror;
368 	txp->esterror = (long)tx32p->esterror;
369 	txp->status = tx32p->status;
370 	txp->constant = (long)tx32p->constant;
371 	txp->precision = (long)tx32p->precision;
372 	txp->tolerance = (long)tx32p->tolerance;
373 	txp->ppsfreq = (long)tx32p->ppsfreq;
374 	txp->jitter = (long)tx32p->jitter;
375 	txp->shift = tx32p->shift;
376 	txp->stabil = (long)tx32p->stabil;
377 	txp->jitcnt = (long)tx32p->jitcnt;
378 	txp->calcnt = (long)tx32p->calcnt;
379 	txp->errcnt = (long)tx32p->errcnt;
380 	txp->stbcnt = (long)tx32p->stbcnt;
381 }
382 
383 static __inline void
384 netbsd32_from___stat13(sbp, sb32p)
385 	struct stat *sbp;
386 	struct netbsd32_stat *sb32p;
387 {
388 	sb32p->st_dev = sbp->st_dev;
389 	sb32p->st_ino = sbp->st_ino;
390 	sb32p->st_mode = sbp->st_mode;
391 	sb32p->st_nlink = sbp->st_nlink;
392 	sb32p->st_uid = sbp->st_uid;
393 	sb32p->st_gid = sbp->st_gid;
394 	sb32p->st_rdev = sbp->st_rdev;
395 	if (sbp->st_size < (quad_t)1 << 32)
396 		sb32p->st_size = sbp->st_size;
397 	else
398 		sb32p->st_size = -2;
399 	sb32p->st_atimespec.tv_sec = (netbsd32_time_t)sbp->st_atimespec.tv_sec;
400 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
401 	sb32p->st_mtimespec.tv_sec = (netbsd32_time_t)sbp->st_mtimespec.tv_sec;
402 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
403 	sb32p->st_ctimespec.tv_sec = (netbsd32_time_t)sbp->st_ctimespec.tv_sec;
404 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
405 	sb32p->st_blksize = sbp->st_blksize;
406 	sb32p->st_blocks = sbp->st_blocks;
407 	sb32p->st_flags = sbp->st_flags;
408 	sb32p->st_gen = sbp->st_gen;
409 }
410 
411 static __inline void
412 netbsd32_to_ipc_perm(ip32p, ipp)
413 	struct netbsd32_ipc_perm *ip32p;
414 	struct ipc_perm *ipp;
415 {
416 
417 	ipp->cuid = ip32p->cuid;
418 	ipp->cgid = ip32p->cgid;
419 	ipp->uid = ip32p->uid;
420 	ipp->gid = ip32p->gid;
421 	ipp->mode = ip32p->mode;
422 	ipp->_seq = ip32p->_seq;
423 	ipp->_key = (key_t)ip32p->_key;
424 }
425 
426 static __inline void
427 netbsd32_from_ipc_perm(ipp, ip32p)
428 	struct ipc_perm *ipp;
429 	struct netbsd32_ipc_perm *ip32p;
430 {
431 
432 	ip32p->cuid = ipp->cuid;
433 	ip32p->cgid = ipp->cgid;
434 	ip32p->uid = ipp->uid;
435 	ip32p->gid = ipp->gid;
436 	ip32p->mode = ipp->mode;
437 	ip32p->_seq = ipp->_seq;
438 	ip32p->_key = (netbsd32_key_t)ipp->_key;
439 }
440 
441 static __inline void
442 netbsd32_to_msg(m32p, mp)
443 	struct netbsd32_msg *m32p;
444 	struct msg *mp;
445 {
446 
447 	mp->msg_next = (struct msg *)(u_long)m32p->msg_next;
448 	mp->msg_type = (long)m32p->msg_type;
449 	mp->msg_ts = m32p->msg_ts;
450 	mp->msg_spot = m32p->msg_spot;
451 }
452 
453 static __inline void
454 netbsd32_from_msg(mp, m32p)
455 	struct msg *mp;
456 	struct netbsd32_msg *m32p;
457 {
458 
459 	m32p->msg_next = (netbsd32_msgp_t)(u_long)mp->msg_next;
460 	m32p->msg_type = (netbsd32_long)mp->msg_type;
461 	m32p->msg_ts = mp->msg_ts;
462 	m32p->msg_spot = mp->msg_spot;
463 }
464 
465 static __inline void
466 netbsd32_to_msqid_ds(ds32p, dsp)
467 	struct netbsd32_msqid_ds *ds32p;
468 	struct msqid_ds *dsp;
469 {
470 
471 	netbsd32_to_ipc_perm(&ds32p->msg_perm, &dsp->msg_perm);
472 	netbsd32_to_msg((struct netbsd32_msg *)(u_long)ds32p->_msg_first, dsp->_msg_first);
473 	netbsd32_to_msg((struct netbsd32_msg *)(u_long)ds32p->_msg_last, dsp->_msg_last);
474 	dsp->_msg_cbytes = (u_long)ds32p->_msg_cbytes;
475 	dsp->msg_qnum = (u_long)ds32p->msg_qnum;
476 	dsp->msg_qbytes = (u_long)ds32p->msg_qbytes;
477 	dsp->msg_lspid = ds32p->msg_lspid;
478 	dsp->msg_lrpid = ds32p->msg_lrpid;
479 	dsp->msg_rtime = (time_t)ds32p->msg_rtime;
480 	dsp->msg_stime = (time_t)ds32p->msg_stime;
481 	dsp->msg_ctime = (time_t)ds32p->msg_ctime;
482 }
483 
484 static __inline void
485 netbsd32_from_msqid_ds(dsp, ds32p)
486 	struct msqid_ds *dsp;
487 	struct netbsd32_msqid_ds *ds32p;
488 {
489 
490 	netbsd32_from_ipc_perm(&dsp->msg_perm, &ds32p->msg_perm);
491 	netbsd32_from_msg(dsp->_msg_first, (struct netbsd32_msg *)(u_long)ds32p->_msg_first);
492 	netbsd32_from_msg(dsp->_msg_last, (struct netbsd32_msg *)(u_long)ds32p->_msg_last);
493 	ds32p->_msg_cbytes = (netbsd32_u_long)dsp->_msg_cbytes;
494 	ds32p->msg_qnum = (netbsd32_u_long)dsp->msg_qnum;
495 	ds32p->msg_qbytes = (netbsd32_u_long)dsp->msg_qbytes;
496 	ds32p->msg_lspid = dsp->msg_lspid;
497 	ds32p->msg_lrpid = dsp->msg_lrpid;
498 	ds32p->msg_rtime = dsp->msg_rtime;
499 	ds32p->msg_stime = dsp->msg_stime;
500 	ds32p->msg_ctime = dsp->msg_ctime;
501 }
502 
503 static __inline void
504 netbsd32_to_shmid_ds(ds32p, dsp)
505 	struct netbsd32_shmid_ds *ds32p;
506 	struct shmid_ds *dsp;
507 {
508 
509 	netbsd32_to_ipc_perm(&ds32p->shm_perm, &dsp->shm_perm);
510 	dsp->shm_segsz = ds32p->shm_segsz;
511 	dsp->shm_lpid = ds32p->shm_lpid;
512 	dsp->shm_cpid = ds32p->shm_cpid;
513 	dsp->shm_nattch = ds32p->shm_nattch;
514 	dsp->shm_atime = (long)ds32p->shm_atime;
515 	dsp->shm_dtime = (long)ds32p->shm_dtime;
516 	dsp->shm_ctime = (long)ds32p->shm_ctime;
517 	dsp->_shm_internal = (void *)(u_long)ds32p->_shm_internal;
518 }
519 
520 static __inline void
521 netbsd32_from_shmid_ds(dsp, ds32p)
522 	struct shmid_ds *dsp;
523 	struct netbsd32_shmid_ds *ds32p;
524 {
525 
526 	netbsd32_from_ipc_perm(&dsp->shm_perm, &ds32p->shm_perm);
527 	ds32p->shm_segsz = dsp->shm_segsz;
528 	ds32p->shm_lpid = dsp->shm_lpid;
529 	ds32p->shm_cpid = dsp->shm_cpid;
530 	ds32p->shm_nattch = dsp->shm_nattch;
531 	ds32p->shm_atime = (netbsd32_long)dsp->shm_atime;
532 	ds32p->shm_dtime = (netbsd32_long)dsp->shm_dtime;
533 	ds32p->shm_ctime = (netbsd32_long)dsp->shm_ctime;
534 	ds32p->_shm_internal = (netbsd32_voidp)(u_long)dsp->_shm_internal;
535 }
536 
537 static __inline void
538 netbsd32_to_semid_ds(s32dsp, dsp)
539 	struct  netbsd32_semid_ds *s32dsp;
540 	struct  semid_ds *dsp;
541 {
542 
543 	netbsd32_from_ipc_perm(&dsp->sem_perm, &s32dsp->sem_perm);
544 	dsp->_sem_base = (struct __sem *)(u_long)s32dsp->_sem_base;
545 	dsp->sem_nsems = s32dsp->sem_nsems;
546 	dsp->sem_otime = s32dsp->sem_otime;
547 	dsp->sem_ctime = s32dsp->sem_ctime;
548 }
549 
550 static __inline void
551 netbsd32_from_semid_ds(dsp, s32dsp)
552 	struct  semid_ds *dsp;
553 	struct  netbsd32_semid_ds *s32dsp;
554 {
555 
556 	netbsd32_to_ipc_perm(&s32dsp->sem_perm, &dsp->sem_perm);
557 	s32dsp->_sem_base = (netbsd32_semp_t)(u_long)dsp->_sem_base;
558 	s32dsp->sem_nsems = dsp->sem_nsems;
559 	s32dsp->sem_otime = dsp->sem_otime;
560 	s32dsp->sem_ctime = dsp->sem_ctime;
561 }
562 
563 /*
564  * below are all the standard NetBSD system calls, in the 32bit
565  * environment, witht he necessary conversions to 64bit before
566  * calling the real syscall.
567  */
568 
569 
570 int
571 netbsd32_exit(p, v, retval)
572 	struct proc *p;
573 	void *v;
574 	register_t *retval;
575 {
576 	struct netbsd32_exit_args /* {
577 		syscallarg(int) rval;
578 	} */ *uap = v;
579 	struct sys_exit_args ua;
580 
581 	NETBSD32TO64_UAP(rval);
582 	sys_exit(p, &ua, retval);
583 }
584 
585 int
586 netbsd32_read(p, v, retval)
587 	struct proc *p;
588 	void *v;
589 	register_t *retval;
590 {
591 	struct netbsd32_read_args /* {
592 		syscallarg(int) fd;
593 		syscallarg(netbsd32_voidp) buf;
594 		syscallarg(netbsd32_size_t) nbyte;
595 	} */ *uap = v;
596 	struct sys_read_args ua;
597 
598 	NETBSD32TO64_UAP(fd);
599 	NETBSD32TOP_UAP(buf, void *);
600 	NETBSD32TOX_UAP(nbyte, size_t);
601 	return sys_read(p, &ua, retval);
602 }
603 
604 int
605 netbsd32_write(p, v, retval)
606 	struct proc *p;
607 	void *v;
608 	register_t *retval;
609 {
610 	struct netbsd32_write_args /* {
611 		syscallarg(int) fd;
612 		syscallarg(const netbsd32_voidp) buf;
613 		syscallarg(netbsd32_size_t) nbyte;
614 	} */ *uap = v;
615 	struct sys_write_args ua;
616 
617 	NETBSD32TO64_UAP(fd);
618 	NETBSD32TOP_UAP(buf, void *);
619 	NETBSD32TOX_UAP(nbyte, size_t);
620 	return sys_write(p, &ua, retval);
621 }
622 
623 int
624 netbsd32_close(p, v, retval)
625 	struct proc *p;
626 	void *v;
627 	register_t *retval;
628 {
629 	struct netbsd32_close_args /* {
630 		syscallarg(int) fd;
631 	} */ *uap = v;
632 	struct sys_close_args ua;
633 
634 	NETBSD32TO64_UAP(fd);
635 	return sys_close(p, &ua, retval);
636 }
637 
638 int
639 netbsd32_open(p, v, retval)
640 	struct proc *p;
641 	void *v;
642 	register_t *retval;
643 {
644 	struct netbsd32_open_args /* {
645 		syscallarg(const netbsd32_charp) path;
646 		syscallarg(int) flags;
647 		syscallarg(mode_t) mode;
648 	} */ *uap = v;
649 	struct sys_open_args ua;
650 	caddr_t sg;
651 
652 	NETBSD32TOP_UAP(path, const char);
653 	NETBSD32TO64_UAP(flags);
654 	NETBSD32TO64_UAP(mode);
655 	sg = stackgap_init(p->p_emul);
656 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
657 
658 	return (sys_open(p, &ua, retval));
659 }
660 
661 int
662 netbsd32_wait4(q, v, retval)
663 	struct proc *q;
664 	void *v;
665 	register_t *retval;
666 {
667 	struct netbsd32_wait4_args /* {
668 		syscallarg(int) pid;
669 		syscallarg(netbsd32_intp) status;
670 		syscallarg(int) options;
671 		syscallarg(netbsd32_rusagep_t) rusage;
672 	} */ *uap = v;
673 	struct netbsd32_rusage ru32;
674 	int nfound;
675 	struct proc *p, *t;
676 	int status, error;
677 
678 	if (SCARG(uap, pid) == 0)
679 		SCARG(uap, pid) = -q->p_pgid;
680 	if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG))
681 		return (EINVAL);
682 
683 loop:
684 	nfound = 0;
685 	for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
686 		if (SCARG(uap, pid) != WAIT_ANY &&
687 		    p->p_pid != SCARG(uap, pid) &&
688 		    p->p_pgid != -SCARG(uap, pid))
689 			continue;
690 		nfound++;
691 		if (p->p_stat == SZOMB) {
692 			retval[0] = p->p_pid;
693 
694 			if (SCARG(uap, status)) {
695 				status = p->p_xstat;	/* convert to int */
696 				error = copyout((caddr_t)&status,
697 						(caddr_t)(u_long)SCARG(uap, status),
698 						sizeof(status));
699 				if (error)
700 					return (error);
701 			}
702 			if (SCARG(uap, rusage)) {
703 				netbsd32_from_rusage(p->p_ru, &ru32);
704 				if ((error = copyout((caddr_t)&ru32,
705 						     (caddr_t)(u_long)SCARG(uap, rusage),
706 						     sizeof(struct netbsd32_rusage))))
707 					return (error);
708 			}
709 			/*
710 			 * If we got the child via ptrace(2) or procfs, and
711 			 * the parent is different (meaning the process was
712 			 * attached, rather than run as a child), then we need
713 			 * to give it back to the old parent, and send the
714 			 * parent a SIGCHLD.  The rest of the cleanup will be
715 			 * done when the old parent waits on the child.
716 			 */
717 			if ((p->p_flag & P_TRACED) &&
718 			    p->p_oppid != p->p_pptr->p_pid) {
719 				t = pfind(p->p_oppid);
720 				proc_reparent(p, t ? t : initproc);
721 				p->p_oppid = 0;
722 				p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE);
723 				psignal(p->p_pptr, SIGCHLD);
724 				wakeup((caddr_t)p->p_pptr);
725 				return (0);
726 			}
727 			p->p_xstat = 0;
728 			ruadd(&q->p_stats->p_cru, p->p_ru);
729 			pool_put(&rusage_pool, p->p_ru);
730 
731 			/*
732 			 * Finally finished with old proc entry.
733 			 * Unlink it from its process group and free it.
734 			 */
735 			leavepgrp(p);
736 
737 			LIST_REMOVE(p, p_list);	/* off zombproc */
738 
739 			LIST_REMOVE(p, p_sibling);
740 
741 			/*
742 			 * Decrement the count of procs running with this uid.
743 			 */
744 			(void)chgproccnt(p->p_cred->p_ruid, -1);
745 
746 			/*
747 			 * Free up credentials.
748 			 */
749 			if (--p->p_cred->p_refcnt == 0) {
750 				crfree(p->p_cred->pc_ucred);
751 				pool_put(&pcred_pool, p->p_cred);
752 			}
753 
754 			/*
755 			 * Release reference to text vnode
756 			 */
757 			if (p->p_textvp)
758 				vrele(p->p_textvp);
759 
760 			pool_put(&proc_pool, p);
761 			nprocs--;
762 			return (0);
763 		}
764 		if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 &&
765 		    (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) {
766 			p->p_flag |= P_WAITED;
767 			retval[0] = p->p_pid;
768 
769 			if (SCARG(uap, status)) {
770 				status = W_STOPCODE(p->p_xstat);
771 				error = copyout((caddr_t)&status,
772 				    (caddr_t)(u_long)SCARG(uap, status),
773 				    sizeof(status));
774 			} else
775 				error = 0;
776 			return (error);
777 		}
778 	}
779 	if (nfound == 0)
780 		return (ECHILD);
781 	if (SCARG(uap, options) & WNOHANG) {
782 		retval[0] = 0;
783 		return (0);
784 	}
785 	if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0)
786 		return (error);
787 	goto loop;
788 }
789 
790 int
791 netbsd32_link(p, v, retval)
792 	struct proc *p;
793 	void *v;
794 	register_t *retval;
795 {
796 	struct netbsd32_link_args /* {
797 		syscallarg(const netbsd32_charp) path;
798 		syscallarg(const netbsd32_charp) link;
799 	} */ *uap = v;
800 	struct sys_link_args ua;
801 
802 	NETBSD32TOP_UAP(path, const char);
803 	NETBSD32TOP_UAP(link, const char);
804 	return (sys_link(p, &ua, retval));
805 }
806 
807 int
808 netbsd32_unlink(p, v, retval)
809 	struct proc *p;
810 	void *v;
811 	register_t *retval;
812 {
813 	struct netbsd32_unlink_args /* {
814 		syscallarg(const netbsd32_charp) path;
815 	} */ *uap = v;
816 	struct sys_unlink_args ua;
817 
818 	NETBSD32TOP_UAP(path, const char);
819 
820 	return (sys_unlink(p, &ua, retval));
821 }
822 
823 int
824 netbsd32_chdir(p, v, retval)
825 	struct proc *p;
826 	void *v;
827 	register_t *retval;
828 {
829 	struct netbsd32_chdir_args /* {
830 		syscallarg(const netbsd32_charp) path;
831 	} */ *uap = v;
832 	struct sys_chdir_args ua;
833 
834 	NETBSD32TOP_UAP(path, const char);
835 
836 	return (sys_chdir(p, &ua, retval));
837 }
838 
839 int
840 netbsd32_fchdir(p, v, retval)
841 	struct proc *p;
842 	void *v;
843 	register_t *retval;
844 {
845 	struct netbsd32_fchdir_args /* {
846 		syscallarg(int) fd;
847 	} */ *uap = v;
848 	struct sys_fchdir_args ua;
849 
850 	NETBSD32TO64_UAP(fd);
851 
852 	return (sys_fchdir(p, &ua, retval));
853 }
854 
855 int
856 netbsd32_mknod(p, v, retval)
857 	struct proc *p;
858 	void *v;
859 	register_t *retval;
860 {
861 	struct netbsd32_mknod_args /* {
862 		syscallarg(const netbsd32_charp) path;
863 		syscallarg(mode_t) mode;
864 		syscallarg(dev_t) dev;
865 	} */ *uap = v;
866 	struct sys_mknod_args ua;
867 
868 	NETBSD32TOP_UAP(path, const char);
869 	NETBSD32TO64_UAP(dev);
870 	NETBSD32TO64_UAP(mode);
871 
872 	return (sys_mknod(p, &ua, retval));
873 }
874 
875 int
876 netbsd32_chmod(p, v, retval)
877 	struct proc *p;
878 	void *v;
879 	register_t *retval;
880 {
881 	struct netbsd32_chmod_args /* {
882 		syscallarg(const netbsd32_charp) path;
883 		syscallarg(mode_t) mode;
884 	} */ *uap = v;
885 	struct sys_chmod_args ua;
886 
887 	NETBSD32TOP_UAP(path, const char);
888 	NETBSD32TO64_UAP(mode);
889 
890 	return (sys_chmod(p, &ua, retval));
891 }
892 
893 int
894 netbsd32_chown(p, v, retval)
895 	struct proc *p;
896 	void *v;
897 	register_t *retval;
898 {
899 	struct netbsd32_chown_args /* {
900 		syscallarg(const netbsd32_charp) path;
901 		syscallarg(uid_t) uid;
902 		syscallarg(gid_t) gid;
903 	} */ *uap = v;
904 	struct sys_chown_args ua;
905 
906 	NETBSD32TOP_UAP(path, const char);
907 	NETBSD32TO64_UAP(uid);
908 	NETBSD32TO64_UAP(gid);
909 
910 	return (sys_chown(p, &ua, retval));
911 }
912 
913 int
914 netbsd32_break(p, v, retval)
915 	struct proc *p;
916 	void *v;
917 	register_t *retval;
918 {
919 	struct netbsd32_break_args /* {
920 		syscallarg(netbsd32_charp) nsize;
921 	} */ *uap = v;
922 	struct sys_obreak_args ua;
923 
924 	SCARG(&ua, nsize) = (char *)(u_long)SCARG(uap, nsize);
925 	NETBSD32TOP_UAP(nsize, char);
926 	return (sys_obreak(p, &ua, retval));
927 }
928 
929 int
930 netbsd32_getfsstat(p, v, retval)
931 	struct proc *p;
932 	void *v;
933 	register_t *retval;
934 {
935 	struct netbsd32_getfsstat_args /* {
936 		syscallarg(netbsd32_statfsp_t) buf;
937 		syscallarg(netbsd32_long) bufsize;
938 		syscallarg(int) flags;
939 	} */ *uap = v;
940 	struct mount *mp, *nmp;
941 	struct statfs *sp;
942 	struct netbsd32_statfs sb32;
943 	caddr_t sfsp;
944 	long count, maxcount, error;
945 
946 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statfs);
947 	sfsp = (caddr_t)SCARG(uap, buf);
948 	simple_lock(&mountlist_slock);
949 	count = 0;
950 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
951 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
952 			nmp = mp->mnt_list.cqe_next;
953 			continue;
954 		}
955 		if (sfsp && count < maxcount) {
956 			sp = &mp->mnt_stat;
957 			/*
958 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
959 			 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
960 			 * overrides MNT_NOWAIT.
961 			 */
962 			if (SCARG(uap, flags) != MNT_NOWAIT &&
963 			    SCARG(uap, flags) != MNT_LAZY &&
964 			    (SCARG(uap, flags) == MNT_WAIT ||
965 			     SCARG(uap, flags) == 0) &&
966 			    (error = VFS_STATFS(mp, sp, p)) != 0) {
967 				simple_lock(&mountlist_slock);
968 				nmp = mp->mnt_list.cqe_next;
969 				vfs_unbusy(mp);
970 				continue;
971 			}
972 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
973 			sp->f_oflags = sp->f_flags & 0xffff;
974 			netbsd32_from_statfs(sp, &sb32);
975 			error = copyout(&sb32, sfsp, sizeof(sb32));
976 			if (error) {
977 				vfs_unbusy(mp);
978 				return (error);
979 			}
980 			sfsp += sizeof(sb32);
981 		}
982 		count++;
983 		simple_lock(&mountlist_slock);
984 		nmp = mp->mnt_list.cqe_next;
985 		vfs_unbusy(mp);
986 	}
987 	simple_unlock(&mountlist_slock);
988 	if (sfsp && count > maxcount)
989 		*retval = maxcount;
990 	else
991 		*retval = count;
992 	return (0);
993 }
994 
995 int
996 netbsd32_mount(p, v, retval)
997 	struct proc *p;
998 	void *v;
999 	register_t *retval;
1000 {
1001 	struct netbsd32_mount_args /* {
1002 		syscallarg(const netbsd32_charp) type;
1003 		syscallarg(const netbsd32_charp) path;
1004 		syscallarg(int) flags;
1005 		syscallarg(netbsd32_voidp) data;
1006 	} */ *uap = v;
1007 	struct sys_mount_args ua;
1008 
1009 	NETBSD32TOP_UAP(type, const char);
1010 	NETBSD32TOP_UAP(path, const char);
1011 	NETBSD32TO64_UAP(flags);
1012 	NETBSD32TOP_UAP(data, void);
1013 	return (sys_mount(p, &ua, retval));
1014 }
1015 
1016 int
1017 netbsd32_unmount(p, v, retval)
1018 	struct proc *p;
1019 	void *v;
1020 	register_t *retval;
1021 {
1022 	struct netbsd32_unmount_args /* {
1023 		syscallarg(const netbsd32_charp) path;
1024 		syscallarg(int) flags;
1025 	} */ *uap = v;
1026 	struct sys_unmount_args ua;
1027 
1028 	NETBSD32TOP_UAP(path, const char);
1029 	NETBSD32TO64_UAP(flags);
1030 	return (sys_unmount(p, &ua, retval));
1031 }
1032 
1033 int
1034 netbsd32_setuid(p, v, retval)
1035 	struct proc *p;
1036 	void *v;
1037 	register_t *retval;
1038 {
1039 	struct netbsd32_setuid_args /* {
1040 		syscallarg(uid_t) uid;
1041 	} */ *uap = v;
1042 	struct sys_setuid_args ua;
1043 
1044 	NETBSD32TO64_UAP(uid);
1045 	return (sys_setuid(p, &ua, retval));
1046 }
1047 
1048 int
1049 netbsd32_ptrace(p, v, retval)
1050 	struct proc *p;
1051 	void *v;
1052 	register_t *retval;
1053 {
1054 	struct netbsd32_ptrace_args /* {
1055 		syscallarg(int) req;
1056 		syscallarg(pid_t) pid;
1057 		syscallarg(netbsd32_caddr_t) addr;
1058 		syscallarg(int) data;
1059 	} */ *uap = v;
1060 	struct sys_ptrace_args ua;
1061 
1062 	NETBSD32TO64_UAP(req);
1063 	NETBSD32TO64_UAP(pid);
1064 	NETBSD32TOX64_UAP(addr, caddr_t);
1065 	NETBSD32TO64_UAP(data);
1066 	return (sys_ptrace(p, &ua, retval));
1067 }
1068 
1069 int
1070 netbsd32_recvmsg(p, v, retval)
1071 	struct proc *p;
1072 	void *v;
1073 	register_t *retval;
1074 {
1075 	struct netbsd32_recvmsg_args /* {
1076 		syscallarg(int) s;
1077 		syscallarg(netbsd32_msghdrp_t) msg;
1078 		syscallarg(int) flags;
1079 	} */ *uap = v;
1080 	struct netbsd32_msghdr msg;
1081 	struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
1082 	int error;
1083 
1084 	error = copyin((caddr_t)(u_long)SCARG(uap, msg), (caddr_t)&msg,
1085 		       sizeof(msg));
1086 		/* netbsd32_msghdr needs the iov pre-allocated */
1087 	if (error)
1088 		return (error);
1089 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
1090 		if ((u_int)msg.msg_iovlen > IOV_MAX)
1091 			return (EMSGSIZE);
1092 		MALLOC(iov, struct iovec *,
1093 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1094 		       M_WAITOK);
1095 	} else if ((u_int)msg.msg_iovlen > 0)
1096 		iov = aiov;
1097 	else
1098 		return (EMSGSIZE);
1099 #ifdef COMPAT_OLDSOCK
1100 	msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
1101 #else
1102 	msg.msg_flags = SCARG(uap, flags);
1103 #endif
1104 	uiov = (struct iovec *)(u_long)msg.msg_iov;
1105 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)uiov,
1106 				   iov, msg.msg_iovlen);
1107 	if (error)
1108 		goto done;
1109 	if ((error = recvit32(p, SCARG(uap, s), &msg, iov, (caddr_t)0, retval)) == 0) {
1110 		error = copyout((caddr_t)&msg, (caddr_t)(u_long)SCARG(uap, msg),
1111 		    sizeof(msg));
1112 	}
1113 done:
1114 	if (iov != aiov)
1115 		FREE(iov, M_IOV);
1116 	return (error);
1117 }
1118 
1119 int
1120 recvit32(p, s, mp, iov, namelenp, retsize)
1121 	struct proc *p;
1122 	int s;
1123 	struct netbsd32_msghdr *mp;
1124 	struct iovec *iov;
1125 	caddr_t namelenp;
1126 	register_t *retsize;
1127 {
1128 	struct file *fp;
1129 	struct uio auio;
1130 	int i;
1131 	int len, error;
1132 	struct mbuf *from = 0, *control = 0;
1133 	struct socket *so;
1134 #ifdef KTRACE
1135 	struct iovec *ktriov = NULL;
1136 #endif
1137 
1138 	/* getsock() will use the descriptor for us */
1139 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
1140 		return (error);
1141 	auio.uio_iov = (struct iovec *)(u_long)mp->msg_iov;
1142 	auio.uio_iovcnt = mp->msg_iovlen;
1143 	auio.uio_segflg = UIO_USERSPACE;
1144 	auio.uio_rw = UIO_READ;
1145 	auio.uio_procp = p;
1146 	auio.uio_offset = 0;			/* XXX */
1147 	auio.uio_resid = 0;
1148 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1149 #if 0
1150 		/* cannot happen iov_len is unsigned */
1151 		if (iov->iov_len < 0) {
1152 			error = EINVAL;
1153 			goto out1;
1154 		}
1155 #endif
1156 		/*
1157 		 * Reads return ssize_t because -1 is returned on error.
1158 		 * Therefore we must restrict the length to SSIZE_MAX to
1159 		 * avoid garbage return values.
1160 		 */
1161 		auio.uio_resid += iov->iov_len;
1162 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
1163 			error = EINVAL;
1164 			goto out1;
1165 		}
1166 	}
1167 #ifdef KTRACE
1168 	if (KTRPOINT(p, KTR_GENIO)) {
1169 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
1170 
1171 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
1172 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
1173 	}
1174 #endif
1175 	len = auio.uio_resid;
1176 	so = (struct socket *)fp->f_data;
1177 	error = (*so->so_receive)(so, &from, &auio, NULL,
1178 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
1179 	if (error) {
1180 		if (auio.uio_resid != len && (error == ERESTART ||
1181 		    error == EINTR || error == EWOULDBLOCK))
1182 			error = 0;
1183 	}
1184 #ifdef KTRACE
1185 	if (ktriov != NULL) {
1186 		if (error == 0)
1187 			ktrgenio(p, s, UIO_READ, ktriov,
1188 			    len - auio.uio_resid, error);
1189 		FREE(ktriov, M_TEMP);
1190 	}
1191 #endif
1192 	if (error)
1193 		goto out;
1194 	*retsize = len - auio.uio_resid;
1195 	if (mp->msg_name) {
1196 		len = mp->msg_namelen;
1197 		if (len <= 0 || from == 0)
1198 			len = 0;
1199 		else {
1200 #ifdef COMPAT_OLDSOCK
1201 			if (mp->msg_flags & MSG_COMPAT)
1202 				mtod(from, struct osockaddr *)->sa_family =
1203 				    mtod(from, struct sockaddr *)->sa_family;
1204 #endif
1205 			if (len > from->m_len)
1206 				len = from->m_len;
1207 			/* else if len < from->m_len ??? */
1208 			error = copyout(mtod(from, caddr_t),
1209 					(caddr_t)(u_long)mp->msg_name, (unsigned)len);
1210 			if (error)
1211 				goto out;
1212 		}
1213 		mp->msg_namelen = len;
1214 		if (namelenp &&
1215 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
1216 #ifdef COMPAT_OLDSOCK
1217 			if (mp->msg_flags & MSG_COMPAT)
1218 				error = 0;	/* old recvfrom didn't check */
1219 			else
1220 #endif
1221 			goto out;
1222 		}
1223 	}
1224 	if (mp->msg_control) {
1225 #ifdef COMPAT_OLDSOCK
1226 		/*
1227 		 * We assume that old recvmsg calls won't receive access
1228 		 * rights and other control info, esp. as control info
1229 		 * is always optional and those options didn't exist in 4.3.
1230 		 * If we receive rights, trim the cmsghdr; anything else
1231 		 * is tossed.
1232 		 */
1233 		if (control && mp->msg_flags & MSG_COMPAT) {
1234 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
1235 			    SOL_SOCKET ||
1236 			    mtod(control, struct cmsghdr *)->cmsg_type !=
1237 			    SCM_RIGHTS) {
1238 				mp->msg_controllen = 0;
1239 				goto out;
1240 			}
1241 			control->m_len -= sizeof(struct cmsghdr);
1242 			control->m_data += sizeof(struct cmsghdr);
1243 		}
1244 #endif
1245 		len = mp->msg_controllen;
1246 		if (len <= 0 || control == 0)
1247 			len = 0;
1248 		else {
1249 			struct mbuf *m = control;
1250 			caddr_t p = (caddr_t)(u_long)mp->msg_control;
1251 
1252 			do {
1253 				i = m->m_len;
1254 				if (len < i) {
1255 					mp->msg_flags |= MSG_CTRUNC;
1256 					i = len;
1257 				}
1258 				error = copyout(mtod(m, caddr_t), p,
1259 				    (unsigned)i);
1260 				if (m->m_next)
1261 					i = ALIGN(i);
1262 				p += i;
1263 				len -= i;
1264 				if (error != 0 || len <= 0)
1265 					break;
1266 			} while ((m = m->m_next) != NULL);
1267 			len = p - (caddr_t)(u_long)mp->msg_control;
1268 		}
1269 		mp->msg_controllen = len;
1270 	}
1271  out:
1272 	if (from)
1273 		m_freem(from);
1274 	if (control)
1275 		m_freem(control);
1276  out1:
1277 	FILE_UNUSE(fp, p);
1278 	return (error);
1279 }
1280 
1281 
1282 int
1283 netbsd32_sendmsg(p, v, retval)
1284 	struct proc *p;
1285 	void *v;
1286 	register_t *retval;
1287 {
1288 	struct netbsd32_sendmsg_args /* {
1289 		syscallarg(int) s;
1290 		syscallarg(const netbsd32_msghdrp_t) msg;
1291 		syscallarg(int) flags;
1292 	} */ *uap = v;
1293 	struct msghdr msg;
1294 	struct netbsd32_msghdr msg32;
1295 	struct iovec aiov[UIO_SMALLIOV], *iov;
1296 	int error;
1297 
1298 	error = copyin((caddr_t)(u_long)SCARG(uap, msg),
1299 		       (caddr_t)&msg32, sizeof(msg32));
1300 	if (error)
1301 		return (error);
1302 	netbsd32_to_msghdr(&msg32, &msg);
1303 	if ((u_int)msg.msg_iovlen > UIO_SMALLIOV) {
1304 		if ((u_int)msg.msg_iovlen > IOV_MAX)
1305 			return (EMSGSIZE);
1306 		MALLOC(iov, struct iovec *,
1307 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1308 		       M_WAITOK);
1309 	} else if ((u_int)msg.msg_iovlen > 0)
1310 		iov = aiov;
1311 	else
1312 		return (EMSGSIZE);
1313 	error = netbsd32_to_iovecin((struct netbsd32_iovec *)msg.msg_iov,
1314 				   iov, msg.msg_iovlen);
1315 	if (error)
1316 		goto done;
1317 	msg.msg_iov = iov;
1318 #ifdef COMPAT_OLDSOCK
1319 	msg.msg_flags = 0;
1320 #endif
1321 	/* Luckily we can use this directly */
1322 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
1323 done:
1324 	if (iov != aiov)
1325 		FREE(iov, M_IOV);
1326 	return (error);
1327 }
1328 
1329 int
1330 netbsd32_recvfrom(p, v, retval)
1331 	struct proc *p;
1332 	void *v;
1333 	register_t *retval;
1334 {
1335 	struct netbsd32_recvfrom_args /* {
1336 		syscallarg(int) s;
1337 		syscallarg(netbsd32_voidp) buf;
1338 		syscallarg(netbsd32_size_t) len;
1339 		syscallarg(int) flags;
1340 		syscallarg(netbsd32_sockaddrp_t) from;
1341 		syscallarg(netbsd32_intp) fromlenaddr;
1342 	} */ *uap = v;
1343 	struct netbsd32_msghdr msg;
1344 	struct iovec aiov;
1345 	int error;
1346 
1347 	if (SCARG(uap, fromlenaddr)) {
1348 		error = copyin((caddr_t)(u_long)SCARG(uap, fromlenaddr),
1349 			       (caddr_t)&msg.msg_namelen,
1350 			       sizeof(msg.msg_namelen));
1351 		if (error)
1352 			return (error);
1353 	} else
1354 		msg.msg_namelen = 0;
1355 	msg.msg_name = SCARG(uap, from);
1356 	msg.msg_iov = NULL; /* We can't store a real pointer here */
1357 	msg.msg_iovlen = 1;
1358 	aiov.iov_base = (caddr_t)(u_long)SCARG(uap, buf);
1359 	aiov.iov_len = (u_long)SCARG(uap, len);
1360 	msg.msg_control = 0;
1361 	msg.msg_flags = SCARG(uap, flags);
1362 	return (recvit32(p, SCARG(uap, s), &msg, &aiov,
1363 		       (caddr_t)(u_long)SCARG(uap, fromlenaddr), retval));
1364 }
1365 
1366 int
1367 netbsd32_sendto(p, v, retval)
1368 	struct proc *p;
1369 	void *v;
1370 	register_t *retval;
1371 {
1372 	struct netbsd32_sendto_args /* {
1373 		syscallarg(int) s;
1374 		syscallarg(const netbsd32_voidp) buf;
1375 		syscallarg(netbsd32_size_t) len;
1376 		syscallarg(int) flags;
1377 		syscallarg(const netbsd32_sockaddrp_t) to;
1378 		syscallarg(int) tolen;
1379 	} */ *uap = v;
1380 	struct msghdr msg;
1381 	struct iovec aiov;
1382 
1383 	msg.msg_name = (caddr_t)(u_long)SCARG(uap, to);		/* XXX kills const */
1384 	msg.msg_namelen = SCARG(uap, tolen);
1385 	msg.msg_iov = &aiov;
1386 	msg.msg_iovlen = 1;
1387 	msg.msg_control = 0;
1388 #ifdef COMPAT_OLDSOCK
1389 	msg.msg_flags = 0;
1390 #endif
1391 	aiov.iov_base = (char *)(u_long)SCARG(uap, buf);	/* XXX kills const */
1392 	aiov.iov_len = SCARG(uap, len);
1393 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
1394 }
1395 
1396 int
1397 netbsd32_accept(p, v, retval)
1398 	struct proc *p;
1399 	void *v;
1400 	register_t *retval;
1401 {
1402 	struct netbsd32_accept_args /* {
1403 		syscallarg(int) s;
1404 		syscallarg(netbsd32_sockaddrp_t) name;
1405 		syscallarg(netbsd32_intp) anamelen;
1406 	} */ *uap = v;
1407 	struct sys_accept_args ua;
1408 
1409 	NETBSD32TO64_UAP(s);
1410 	NETBSD32TOP_UAP(name, struct sockaddr);
1411 	NETBSD32TOP_UAP(anamelen, int);
1412 	return (sys_accept(p, &ua, retval));
1413 }
1414 
1415 int
1416 netbsd32_getpeername(p, v, retval)
1417 	struct proc *p;
1418 	void *v;
1419 	register_t *retval;
1420 {
1421 	struct netbsd32_getpeername_args /* {
1422 		syscallarg(int) fdes;
1423 		syscallarg(netbsd32_sockaddrp_t) asa;
1424 		syscallarg(netbsd32_intp) alen;
1425 	} */ *uap = v;
1426 	struct sys_getpeername_args ua;
1427 
1428 	NETBSD32TO64_UAP(fdes);
1429 	NETBSD32TOP_UAP(asa, struct sockaddr);
1430 	NETBSD32TOP_UAP(alen, int);
1431 /* NB: do the protocol specific sockaddrs need to be converted? */
1432 	return (sys_getpeername(p, &ua, retval));
1433 }
1434 
1435 int
1436 netbsd32_getsockname(p, v, retval)
1437 	struct proc *p;
1438 	void *v;
1439 	register_t *retval;
1440 {
1441 	struct netbsd32_getsockname_args /* {
1442 		syscallarg(int) fdes;
1443 		syscallarg(netbsd32_sockaddrp_t) asa;
1444 		syscallarg(netbsd32_intp) alen;
1445 	} */ *uap = v;
1446 	struct sys_getsockname_args ua;
1447 
1448 	NETBSD32TO64_UAP(fdes);
1449 	NETBSD32TOP_UAP(asa, struct sockaddr);
1450 	NETBSD32TOP_UAP(alen, int);
1451 	return (sys_getsockname(p, &ua, retval));
1452 }
1453 
1454 int
1455 netbsd32_access(p, v, retval)
1456 	struct proc *p;
1457 	void *v;
1458 	register_t *retval;
1459 {
1460 	struct netbsd32_access_args /* {
1461 		syscallarg(const netbsd32_charp) path;
1462 		syscallarg(int) flags;
1463 	} */ *uap = v;
1464 	struct sys_access_args ua;
1465 	caddr_t sg;
1466 
1467 	NETBSD32TOP_UAP(path, const char);
1468 	NETBSD32TO64_UAP(flags);
1469 	sg = stackgap_init(p->p_emul);
1470 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
1471 
1472 	return (sys_access(p, &ua, retval));
1473 }
1474 
1475 int
1476 netbsd32_chflags(p, v, retval)
1477 	struct proc *p;
1478 	void *v;
1479 	register_t *retval;
1480 {
1481 	struct netbsd32_chflags_args /* {
1482 		syscallarg(const netbsd32_charp) path;
1483 		syscallarg(netbsd32_u_long) flags;
1484 	} */ *uap = v;
1485 	struct sys_chflags_args ua;
1486 
1487 	NETBSD32TOP_UAP(path, const char);
1488 	NETBSD32TO64_UAP(flags);
1489 
1490 	return (sys_chflags(p, &ua, retval));
1491 }
1492 
1493 int
1494 netbsd32_fchflags(p, v, retval)
1495 	struct proc *p;
1496 	void *v;
1497 	register_t *retval;
1498 {
1499 	struct netbsd32_fchflags_args /* {
1500 		syscallarg(int) fd;
1501 		syscallarg(netbsd32_u_long) flags;
1502 	} */ *uap = v;
1503 	struct sys_fchflags_args ua;
1504 
1505 	NETBSD32TO64_UAP(fd);
1506 	NETBSD32TO64_UAP(flags);
1507 
1508 	return (sys_fchflags(p, &ua, retval));
1509 }
1510 
1511 int
1512 netbsd32_kill(p, v, retval)
1513 	struct proc *p;
1514 	void *v;
1515 	register_t *retval;
1516 {
1517 	struct netbsd32_kill_args /* {
1518 		syscallarg(int) pid;
1519 		syscallarg(int) signum;
1520 	} */ *uap = v;
1521 	struct sys_kill_args ua;
1522 
1523 	NETBSD32TO64_UAP(pid);
1524 	NETBSD32TO64_UAP(signum);
1525 
1526 	return (sys_kill(p, &ua, retval));
1527 }
1528 
1529 int
1530 netbsd32_dup(p, v, retval)
1531 	struct proc *p;
1532 	void *v;
1533 	register_t *retval;
1534 {
1535 	struct netbsd32_dup_args /* {
1536 		syscallarg(int) fd;
1537 	} */ *uap = v;
1538 	struct sys_dup_args ua;
1539 
1540 	NETBSD32TO64_UAP(fd);
1541 
1542 	return (sys_dup(p, &ua, retval));
1543 }
1544 
1545 int
1546 netbsd32_profil(p, v, retval)
1547 	struct proc *p;
1548 	void *v;
1549 	register_t *retval;
1550 {
1551 	struct netbsd32_profil_args /* {
1552 		syscallarg(netbsd32_caddr_t) samples;
1553 		syscallarg(netbsd32_size_t) size;
1554 		syscallarg(netbsd32_u_long) offset;
1555 		syscallarg(u_int) scale;
1556 	} */ *uap = v;
1557 	struct sys_profil_args ua;
1558 
1559 	NETBSD32TOX64_UAP(samples, caddr_t);
1560 	NETBSD32TOX_UAP(size, size_t);
1561 	NETBSD32TOX_UAP(offset, u_long);
1562 	NETBSD32TO64_UAP(scale);
1563 	return (sys_profil(p, &ua, retval));
1564 }
1565 
1566 int
1567 netbsd32_ktrace(p, v, retval)
1568 	struct proc *p;
1569 	void *v;
1570 	register_t *retval;
1571 {
1572 	struct netbsd32_ktrace_args /* {
1573 		syscallarg(const netbsd32_charp) fname;
1574 		syscallarg(int) ops;
1575 		syscallarg(int) facs;
1576 		syscallarg(int) pid;
1577 	} */ *uap = v;
1578 	struct sys_ktrace_args ua;
1579 
1580 	NETBSD32TOP_UAP(fname, const char);
1581 	NETBSD32TO64_UAP(ops);
1582 	NETBSD32TO64_UAP(facs);
1583 	NETBSD32TO64_UAP(pid);
1584 	return (sys_ktrace(p, &ua, retval));
1585 }
1586 
1587 int
1588 netbsd32_sigaction(p, v, retval)
1589 	struct proc *p;
1590 	void *v;
1591 	register_t *retval;
1592 {
1593 	struct netbsd32_sigaction_args /* {
1594 		syscallarg(int) signum;
1595 		syscallarg(const netbsd32_sigactionp_t) nsa;
1596 		syscallarg(netbsd32_sigactionp_t) osa;
1597 	} */ *uap = v;
1598 	struct sigaction nsa, osa;
1599 	struct netbsd32_sigaction *sa32p, sa32;
1600 	int error;
1601 
1602 	if (SCARG(uap, nsa)) {
1603 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, nsa);
1604 		if (copyin(sa32p, &sa32, sizeof(sa32)))
1605 			return EFAULT;
1606 		nsa.sa_handler = (void *)(u_long)sa32.sa_handler;
1607 		nsa.sa_mask = sa32.sa_mask;
1608 		nsa.sa_flags = sa32.sa_flags;
1609 	}
1610 	error = sigaction1(p, SCARG(uap, signum),
1611 			   SCARG(uap, nsa) ? &nsa : 0,
1612 			   SCARG(uap, osa) ? &osa : 0);
1613 
1614 	if (error)
1615 		return (error);
1616 
1617 	if (SCARG(uap, osa)) {
1618 		sa32.sa_handler = (netbsd32_sigactionp_t)(u_long)osa.sa_handler;
1619 		sa32.sa_mask = osa.sa_mask;
1620 		sa32.sa_flags = osa.sa_flags;
1621 		sa32p = (struct netbsd32_sigaction *)(u_long)SCARG(uap, osa);
1622 		if (copyout(&sa32, sa32p, sizeof(sa32)))
1623 			return EFAULT;
1624 	}
1625 
1626 	return (0);
1627 }
1628 
1629 int
1630 netbsd32___getlogin(p, v, retval)
1631 	struct proc *p;
1632 	void *v;
1633 	register_t *retval;
1634 {
1635 	struct netbsd32___getlogin_args /* {
1636 		syscallarg(netbsd32_charp) namebuf;
1637 		syscallarg(u_int) namelen;
1638 	} */ *uap = v;
1639 	struct sys___getlogin_args ua;
1640 
1641 	NETBSD32TOP_UAP(namebuf, char);
1642 	NETBSD32TO64_UAP(namelen);
1643 	return (sys___getlogin(p, &ua, retval));
1644 }
1645 
1646 int
1647 netbsd32_setlogin(p, v, retval)
1648 	struct proc *p;
1649 	void *v;
1650 	register_t *retval;
1651 {
1652 	struct netbsd32_setlogin_args /* {
1653 		syscallarg(const netbsd32_charp) namebuf;
1654 	} */ *uap = v;
1655 	struct sys_setlogin_args ua;
1656 
1657 	NETBSD32TOP_UAP(namebuf, char);
1658 	return (sys_setlogin(p, &ua, retval));
1659 }
1660 
1661 int
1662 netbsd32_acct(p, v, retval)
1663 	struct proc *p;
1664 	void *v;
1665 	register_t *retval;
1666 {
1667 	struct netbsd32_acct_args /* {
1668 		syscallarg(const netbsd32_charp) path;
1669 	} */ *uap = v;
1670 	struct sys_acct_args ua;
1671 
1672 	NETBSD32TOP_UAP(path, const char);
1673 	return (sys_acct(p, &ua, retval));
1674 }
1675 
1676 int
1677 netbsd32_revoke(p, v, retval)
1678 	struct proc *p;
1679 	void *v;
1680 	register_t *retval;
1681 {
1682 	struct netbsd32_revoke_args /* {
1683 		syscallarg(const netbsd32_charp) path;
1684 	} */ *uap = v;
1685 	struct sys_revoke_args ua;
1686 	caddr_t sg;
1687 
1688 	NETBSD32TOP_UAP(path, const char);
1689 	sg = stackgap_init(p->p_emul);
1690 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
1691 
1692 	return (sys_revoke(p, &ua, retval));
1693 }
1694 
1695 int
1696 netbsd32_symlink(p, v, retval)
1697 	struct proc *p;
1698 	void *v;
1699 	register_t *retval;
1700 {
1701 	struct netbsd32_symlink_args /* {
1702 		syscallarg(const netbsd32_charp) path;
1703 		syscallarg(const netbsd32_charp) link;
1704 	} */ *uap = v;
1705 	struct sys_symlink_args ua;
1706 
1707 	NETBSD32TOP_UAP(path, const char);
1708 	NETBSD32TOP_UAP(link, const char);
1709 
1710 	return (sys_symlink(p, &ua, retval));
1711 }
1712 
1713 int
1714 netbsd32_readlink(p, v, retval)
1715 	struct proc *p;
1716 	void *v;
1717 	register_t *retval;
1718 {
1719 	struct netbsd32_readlink_args /* {
1720 		syscallarg(const netbsd32_charp) path;
1721 		syscallarg(netbsd32_charp) buf;
1722 		syscallarg(netbsd32_size_t) count;
1723 	} */ *uap = v;
1724 	struct sys_readlink_args ua;
1725 	caddr_t sg;
1726 
1727 	NETBSD32TOP_UAP(path, const char);
1728 	NETBSD32TOP_UAP(buf, char);
1729 	NETBSD32TOX_UAP(count, size_t);
1730 	sg = stackgap_init(p->p_emul);
1731 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
1732 
1733 	return (sys_readlink(p, &ua, retval));
1734 }
1735 
1736 /*
1737  * Need to completly reimplement this syscall due to argument copying.
1738  */
1739 int
1740 netbsd32_execve(p, v, retval)
1741 	struct proc *p;
1742 	void *v;
1743 	register_t *retval;
1744 {
1745 	struct netbsd32_execve_args /* {
1746 		syscallarg(const netbsd32_charp) path;
1747 		syscallarg(netbsd32_charpp) argp;
1748 		syscallarg(netbsd32_charpp) envp;
1749 	} */ *uap = v;
1750 	struct sys_execve_args ua;
1751 	caddr_t sg;
1752 	/* Function args */
1753 	int error, i;
1754 	struct exec_package pack;
1755 	struct nameidata nid;
1756 	struct vattr attr;
1757 	struct ucred *cred = p->p_ucred;
1758 	char *argp;
1759 	netbsd32_charp const *cpp;
1760 	char *dp;
1761 	netbsd32_charp sp;
1762 	long argc, envc;
1763 	size_t len;
1764 	char *stack;
1765 	struct ps_strings arginfo;
1766 	struct vmspace *vm;
1767 	char **tmpfap;
1768 	int szsigcode;
1769 	extern struct emul emul_netbsd;
1770 
1771 
1772 	NETBSD32TOP_UAP(path, const char);
1773 	NETBSD32TOP_UAP(argp, char *);
1774 	NETBSD32TOP_UAP(envp, char *);
1775 	sg = stackgap_init(p->p_emul);
1776 	NETBSD32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
1777 
1778 	/*
1779 	 * figure out the maximum size of an exec header, if necessary.
1780 	 * XXX should be able to keep LKM code from modifying exec switch
1781 	 * when we're still using it, but...
1782 	 */
1783 	if (exec_maxhdrsz == 0) {
1784 		for (i = 0; i < nexecs; i++)
1785 			if (execsw[i].es_check != NULL
1786 			    && execsw[i].es_hdrsz > exec_maxhdrsz)
1787 				exec_maxhdrsz = execsw[i].es_hdrsz;
1788 	}
1789 
1790 	/* init the namei data to point the file user's program name */
1791 	/* XXX cgd 960926: why do this here?  most will be clobbered. */
1792 	NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(&ua, path), p);
1793 
1794 	/*
1795 	 * initialize the fields of the exec package.
1796 	 */
1797 	pack.ep_name = SCARG(&ua, path);
1798 	MALLOC(pack.ep_hdr, void *, exec_maxhdrsz, M_EXEC, M_WAITOK);
1799 	pack.ep_hdrlen = exec_maxhdrsz;
1800 	pack.ep_hdrvalid = 0;
1801 	pack.ep_ndp = &nid;
1802 	pack.ep_emul_arg = NULL;
1803 	pack.ep_vmcmds.evs_cnt = 0;
1804 	pack.ep_vmcmds.evs_used = 0;
1805 	pack.ep_vap = &attr;
1806 	pack.ep_emul = &emul_netbsd;
1807 	pack.ep_flags = 0;
1808 
1809 	/* see if we can run it. */
1810 	if ((error = check_exec(p, &pack)) != 0)
1811 		goto freehdr;
1812 
1813 	/* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
1814 
1815 	/* allocate an argument buffer */
1816 	argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
1817 #ifdef DIAGNOSTIC
1818 	if (argp == (vaddr_t) 0)
1819 		panic("execve: argp == NULL");
1820 #endif
1821 	dp = argp;
1822 	argc = 0;
1823 
1824 	/* copy the fake args list, if there's one, freeing it as we go */
1825 	if (pack.ep_flags & EXEC_HASARGL) {
1826 		tmpfap = pack.ep_fa;
1827 		while (*tmpfap != NULL) {
1828 			char *cp;
1829 
1830 			cp = *tmpfap;
1831 			while (*cp)
1832 				*dp++ = *cp++;
1833 			dp++;
1834 
1835 			FREE(*tmpfap, M_EXEC);
1836 			tmpfap++; argc++;
1837 		}
1838 		FREE(pack.ep_fa, M_EXEC);
1839 		pack.ep_flags &= ~EXEC_HASARGL;
1840 	}
1841 
1842 	/* Now get argv & environment */
1843 	if (!(cpp = (netbsd32_charp *)SCARG(&ua, argp))) {
1844 		error = EINVAL;
1845 		goto bad;
1846 	}
1847 
1848 	if (pack.ep_flags & EXEC_SKIPARG)
1849 		cpp++;
1850 
1851 	while (1) {
1852 		len = argp + ARG_MAX - dp;
1853 		if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
1854 			goto bad;
1855 		if (!sp)
1856 			break;
1857 		if ((error = copyinstr((char *)(u_long)sp, dp,
1858 				       len, &len)) != 0) {
1859 			if (error == ENAMETOOLONG)
1860 				error = E2BIG;
1861 			goto bad;
1862 		}
1863 		dp += len;
1864 		cpp++;
1865 		argc++;
1866 	}
1867 
1868 	envc = 0;
1869 	/* environment need not be there */
1870 	if ((cpp = (netbsd32_charp *)SCARG(&ua, envp)) != NULL ) {
1871 		while (1) {
1872 			len = argp + ARG_MAX - dp;
1873 			if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
1874 				goto bad;
1875 			if (!sp)
1876 				break;
1877 			if ((error = copyinstr((char *)(u_long)sp,
1878 					       dp, len, &len)) != 0) {
1879 				if (error == ENAMETOOLONG)
1880 					error = E2BIG;
1881 				goto bad;
1882 			}
1883 			dp += len;
1884 			cpp++;
1885 			envc++;
1886 		}
1887 	}
1888 
1889 	dp = (char *) ALIGN(dp);
1890 
1891 	szsigcode = pack.ep_emul->e_esigcode - pack.ep_emul->e_sigcode;
1892 
1893 	/* Now check if args & environ fit into new stack */
1894 	if (pack.ep_flags & EXEC_32)
1895 		len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(int) +
1896 		       sizeof(int) + dp + STACKGAPLEN + szsigcode +
1897 		       sizeof(struct ps_strings)) - argp;
1898 	else
1899 		len = ((argc + envc + 2 + pack.ep_emul->e_arglen) * sizeof(char *) +
1900 		       sizeof(int) + dp + STACKGAPLEN + szsigcode +
1901 		       sizeof(struct ps_strings)) - argp;
1902 
1903 	len = ALIGN(len);	/* make the stack "safely" aligned */
1904 
1905 	if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
1906 		error = ENOMEM;
1907 		goto bad;
1908 	}
1909 
1910 	/* adjust "active stack depth" for process VSZ */
1911 	pack.ep_ssize = len;	/* maybe should go elsewhere, but... */
1912 
1913 	/*
1914 	 * Do whatever is necessary to prepare the address space
1915 	 * for remapping.  Note that this might replace the current
1916 	 * vmspace with another!
1917 	 */
1918 	uvmspace_exec(p);
1919 
1920 	/* Now map address space */
1921 	vm = p->p_vmspace;
1922 	vm->vm_taddr = (char *) pack.ep_taddr;
1923 	vm->vm_tsize = btoc(pack.ep_tsize);
1924 	vm->vm_daddr = (char *) pack.ep_daddr;
1925 	vm->vm_dsize = btoc(pack.ep_dsize);
1926 	vm->vm_ssize = btoc(pack.ep_ssize);
1927 	vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
1928 
1929 	/* create the new process's VM space by running the vmcmds */
1930 #ifdef DIAGNOSTIC
1931 	if (pack.ep_vmcmds.evs_used == 0)
1932 		panic("execve: no vmcmds");
1933 #endif
1934 	for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
1935 		struct exec_vmcmd *vcp;
1936 
1937 		vcp = &pack.ep_vmcmds.evs_cmds[i];
1938 		error = (*vcp->ev_proc)(p, vcp);
1939 	}
1940 
1941 	/* free the vmspace-creation commands, and release their references */
1942 	kill_vmcmds(&pack.ep_vmcmds);
1943 
1944 	/* if an error happened, deallocate and punt */
1945 	if (error)
1946 		goto exec_abort;
1947 
1948 	/* remember information about the process */
1949 	arginfo.ps_nargvstr = argc;
1950 	arginfo.ps_nenvstr = envc;
1951 
1952 	stack = (char *) (USRSTACK - len);
1953 	/* Now copy argc, args & environ to new stack */
1954 	if (!(*pack.ep_emul->e_copyargs)(&pack, &arginfo, stack, argp))
1955 		goto exec_abort;
1956 
1957 	/* copy out the process's ps_strings structure */
1958 	if (copyout(&arginfo, (char *) PS_STRINGS, sizeof(arginfo)))
1959 		goto exec_abort;
1960 
1961 	/* copy out the process's signal trapoline code */
1962 	if (szsigcode) {
1963 		if (copyout((char *)pack.ep_emul->e_sigcode,
1964 		    p->p_sigacts->ps_sigcode = (char *)PS_STRINGS - szsigcode,
1965 		    szsigcode))
1966 			goto exec_abort;
1967 #ifdef PMAP_NEED_PROCWR
1968 		/* This is code. Let the pmap do what is needed. */
1969 		pmap_procwr(p, (vaddr_t)p->p_sigacts->ps_sigcode, szsigcode);
1970 #endif
1971 	}
1972 
1973 	stopprofclock(p);	/* stop profiling */
1974 	fdcloseexec(p);		/* handle close on exec */
1975 	execsigs(p);		/* reset catched signals */
1976 	p->p_ctxlink = NULL;	/* reset ucontext link */
1977 
1978 	/* set command name & other accounting info */
1979 	len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
1980 	memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
1981 	p->p_comm[len] = 0;
1982 	p->p_acflag &= ~AFORK;
1983 
1984 	/* record proc's vnode, for use by procfs and others */
1985         if (p->p_textvp)
1986                 vrele(p->p_textvp);
1987 	VREF(pack.ep_vp);
1988 	p->p_textvp = pack.ep_vp;
1989 
1990 	p->p_flag |= P_EXEC;
1991 	if (p->p_flag & P_PPWAIT) {
1992 		p->p_flag &= ~P_PPWAIT;
1993 		wakeup((caddr_t) p->p_pptr);
1994 	}
1995 
1996 	/*
1997 	 * deal with set[ug]id.
1998 	 * MNT_NOSUID and P_TRACED have already been used to disable s[ug]id.
1999 	 */
2000 	if (((attr.va_mode & S_ISUID) != 0 && p->p_ucred->cr_uid != attr.va_uid)
2001 	 || ((attr.va_mode & S_ISGID) != 0 && p->p_ucred->cr_gid != attr.va_gid)){
2002 		p->p_ucred = crcopy(cred);
2003 #ifdef KTRACE
2004 		/*
2005 		 * If process is being ktraced, turn off - unless
2006 		 * root set it.
2007 		 */
2008 		if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
2009 			ktrderef(p);
2010 #endif
2011 		if (attr.va_mode & S_ISUID)
2012 			p->p_ucred->cr_uid = attr.va_uid;
2013 		if (attr.va_mode & S_ISGID)
2014 			p->p_ucred->cr_gid = attr.va_gid;
2015 		p_sugid(p);
2016 	} else
2017 		p->p_flag &= ~P_SUGID;
2018 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
2019 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
2020 
2021 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
2022 
2023 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
2024 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
2025 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
2026 	vput(pack.ep_vp);
2027 
2028 	/* setup new registers and do misc. setup. */
2029 	(*pack.ep_emul->e_setregs)(p, &pack, (u_long) stack);
2030 
2031 	if (p->p_flag & P_TRACED)
2032 		psignal(p, SIGTRAP);
2033 
2034 	p->p_emul = pack.ep_emul;
2035 	FREE(pack.ep_hdr, M_EXEC);
2036 
2037 #ifdef KTRACE
2038 	if (KTRPOINT(p, KTR_EMUL))
2039 		ktremul(p);
2040 #endif
2041 
2042 	return (EJUSTRETURN);
2043 
2044 bad:
2045 	/* free the vmspace-creation commands, and release their references */
2046 	kill_vmcmds(&pack.ep_vmcmds);
2047 	/* kill any opened file descriptor, if necessary */
2048 	if (pack.ep_flags & EXEC_HASFD) {
2049 		pack.ep_flags &= ~EXEC_HASFD;
2050 		(void) fdrelease(p, pack.ep_fd);
2051 	}
2052 	/* close and put the exec'd file */
2053 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
2054 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
2055 	vput(pack.ep_vp);
2056 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
2057 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
2058 
2059 freehdr:
2060 	FREE(pack.ep_hdr, M_EXEC);
2061 	return error;
2062 
2063 exec_abort:
2064 	/*
2065 	 * the old process doesn't exist anymore.  exit gracefully.
2066 	 * get rid of the (new) address space we have created, if any, get rid
2067 	 * of our namei data and vnode, and exit noting failure
2068 	 */
2069 	uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
2070 		VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
2071 	if (pack.ep_emul_arg)
2072 		FREE(pack.ep_emul_arg, M_TEMP);
2073 	FREE(nid.ni_cnd.cn_pnbuf, M_NAMEI);
2074 	vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
2075 	VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
2076 	vput(pack.ep_vp);
2077 	uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
2078 	FREE(pack.ep_hdr, M_EXEC);
2079 	exit1(p, W_EXITCODE(0, SIGABRT));
2080 	exit1(p, -1);
2081 
2082 	/* NOTREACHED */
2083 	return 0;
2084 }
2085 
2086 int
2087 netbsd32_umask(p, v, retval)
2088 	struct proc *p;
2089 	void *v;
2090 	register_t *retval;
2091 {
2092 	struct netbsd32_umask_args /* {
2093 		syscallarg(mode_t) newmask;
2094 	} */ *uap = v;
2095 	struct sys_umask_args ua;
2096 
2097 	NETBSD32TO64_UAP(newmask);
2098 	return (sys_umask(p, &ua, retval));
2099 }
2100 
2101 int
2102 netbsd32_chroot(p, v, retval)
2103 	struct proc *p;
2104 	void *v;
2105 	register_t *retval;
2106 {
2107 	struct netbsd32_chroot_args /* {
2108 		syscallarg(const netbsd32_charp) path;
2109 	} */ *uap = v;
2110 	struct sys_chroot_args ua;
2111 
2112 	NETBSD32TOP_UAP(path, const char);
2113 	return (sys_chroot(p, &ua, retval));
2114 }
2115 
2116 int
2117 netbsd32_sbrk(p, v, retval)
2118 	struct proc *p;
2119 	void *v;
2120 	register_t *retval;
2121 {
2122 	struct netbsd32_sbrk_args /* {
2123 		syscallarg(int) incr;
2124 	} */ *uap = v;
2125 	struct sys_sbrk_args ua;
2126 
2127 	NETBSD32TO64_UAP(incr);
2128 	return (sys_sbrk(p, &ua, retval));
2129 }
2130 
2131 int
2132 netbsd32_sstk(p, v, retval)
2133 	struct proc *p;
2134 	void *v;
2135 	register_t *retval;
2136 {
2137 	struct netbsd32_sstk_args /* {
2138 		syscallarg(int) incr;
2139 	} */ *uap = v;
2140 	struct sys_sstk_args ua;
2141 
2142 	NETBSD32TO64_UAP(incr);
2143 	return (sys_sstk(p, &ua, retval));
2144 }
2145 
2146 int
2147 netbsd32_munmap(p, v, retval)
2148 	struct proc *p;
2149 	void *v;
2150 	register_t *retval;
2151 {
2152 	struct netbsd32_munmap_args /* {
2153 		syscallarg(netbsd32_voidp) addr;
2154 		syscallarg(netbsd32_size_t) len;
2155 	} */ *uap = v;
2156 	struct sys_munmap_args ua;
2157 
2158 	NETBSD32TOP_UAP(addr, void);
2159 	NETBSD32TOX_UAP(len, size_t);
2160 	return (sys_munmap(p, &ua, retval));
2161 }
2162 
2163 int
2164 netbsd32_mprotect(p, v, retval)
2165 	struct proc *p;
2166 	void *v;
2167 	register_t *retval;
2168 {
2169 	struct netbsd32_mprotect_args /* {
2170 		syscallarg(netbsd32_voidp) addr;
2171 		syscallarg(netbsd32_size_t) len;
2172 		syscallarg(int) prot;
2173 	} */ *uap = v;
2174 	struct sys_mprotect_args ua;
2175 
2176 	NETBSD32TOP_UAP(addr, void);
2177 	NETBSD32TOX_UAP(len, size_t);
2178 	NETBSD32TO64_UAP(prot);
2179 	return (sys_mprotect(p, &ua, retval));
2180 }
2181 
2182 int
2183 netbsd32_madvise(p, v, retval)
2184 	struct proc *p;
2185 	void *v;
2186 	register_t *retval;
2187 {
2188 	struct netbsd32_madvise_args /* {
2189 		syscallarg(netbsd32_voidp) addr;
2190 		syscallarg(netbsd32_size_t) len;
2191 		syscallarg(int) behav;
2192 	} */ *uap = v;
2193 	struct sys_madvise_args ua;
2194 
2195 	NETBSD32TOP_UAP(addr, void);
2196 	NETBSD32TOX_UAP(len, size_t);
2197 	NETBSD32TO64_UAP(behav);
2198 	return (sys_madvise(p, &ua, retval));
2199 }
2200 
2201 int
2202 netbsd32_mincore(p, v, retval)
2203 	struct proc *p;
2204 	void *v;
2205 	register_t *retval;
2206 {
2207 	struct netbsd32_mincore_args /* {
2208 		syscallarg(netbsd32_caddr_t) addr;
2209 		syscallarg(netbsd32_size_t) len;
2210 		syscallarg(netbsd32_charp) vec;
2211 	} */ *uap = v;
2212 	struct sys_mincore_args ua;
2213 
2214 	NETBSD32TOX64_UAP(addr, caddr_t);
2215 	NETBSD32TOX_UAP(len, size_t);
2216 	NETBSD32TOP_UAP(vec, char);
2217 	return (sys_mincore(p, &ua, retval));
2218 }
2219 
2220 int
2221 netbsd32_getgroups(p, v, retval)
2222 	struct proc *p;
2223 	void *v;
2224 	register_t *retval;
2225 {
2226 	struct netbsd32_getgroups_args /* {
2227 		syscallarg(int) gidsetsize;
2228 		syscallarg(netbsd32_gid_tp) gidset;
2229 	} */ *uap = v;
2230 	struct pcred *pc = p->p_cred;
2231 	int ngrp;
2232 	int error;
2233 
2234 	ngrp = SCARG(uap, gidsetsize);
2235 	if (ngrp == 0) {
2236 		*retval = pc->pc_ucred->cr_ngroups;
2237 		return (0);
2238 	}
2239 	if (ngrp < pc->pc_ucred->cr_ngroups)
2240 		return (EINVAL);
2241 	ngrp = pc->pc_ucred->cr_ngroups;
2242 	/* Should convert gid_t to netbsd32_gid_t, but they're the same */
2243 	error = copyout((caddr_t)pc->pc_ucred->cr_groups,
2244 			(caddr_t)(u_long)SCARG(uap, gidset),
2245 			ngrp * sizeof(gid_t));
2246 	if (error)
2247 		return (error);
2248 	*retval = ngrp;
2249 	return (0);
2250 }
2251 
2252 int
2253 netbsd32_setgroups(p, v, retval)
2254 	struct proc *p;
2255 	void *v;
2256 	register_t *retval;
2257 {
2258 	struct netbsd32_setgroups_args /* {
2259 		syscallarg(int) gidsetsize;
2260 		syscallarg(const netbsd32_gid_tp) gidset;
2261 	} */ *uap = v;
2262 	struct sys_setgroups_args ua;
2263 
2264 	NETBSD32TO64_UAP(gidsetsize);
2265 	NETBSD32TOP_UAP(gidset, gid_t);
2266 	return (sys_setgroups(p, &ua, retval));
2267 }
2268 
2269 int
2270 netbsd32_setpgid(p, v, retval)
2271 	struct proc *p;
2272 	void *v;
2273 	register_t *retval;
2274 {
2275 	struct netbsd32_setpgid_args /* {
2276 		syscallarg(int) pid;
2277 		syscallarg(int) pgid;
2278 	} */ *uap = v;
2279 	struct sys_setpgid_args ua;
2280 
2281 	NETBSD32TO64_UAP(pid);
2282 	NETBSD32TO64_UAP(pgid);
2283 	return (sys_setpgid(p, &ua, retval));
2284 }
2285 
2286 int
2287 netbsd32_setitimer(p, v, retval)
2288 	struct proc *p;
2289 	void *v;
2290 	register_t *retval;
2291 {
2292 	struct netbsd32_setitimer_args /* {
2293 		syscallarg(int) which;
2294 		syscallarg(const netbsd32_itimervalp_t) itv;
2295 		syscallarg(netbsd32_itimervalp_t) oitv;
2296 	} */ *uap = v;
2297 	struct netbsd32_itimerval s32it, *itvp;
2298 	int which = SCARG(uap, which);
2299 	struct netbsd32_getitimer_args getargs;
2300 	struct itimerval aitv;
2301 	int s, error;
2302 
2303 	if ((u_int)which > ITIMER_PROF)
2304 		return (EINVAL);
2305 	itvp = (struct netbsd32_itimerval *)(u_long)SCARG(uap, itv);
2306 	if (itvp && (error = copyin(itvp, &s32it, sizeof(s32it))))
2307 		return (error);
2308 	netbsd32_to_itimerval(&s32it, &aitv);
2309 	if (SCARG(uap, oitv) != NULL) {
2310 		SCARG(&getargs, which) = which;
2311 		SCARG(&getargs, itv) = SCARG(uap, oitv);
2312 		if ((error = netbsd32_getitimer(p, &getargs, retval)) != 0)
2313 			return (error);
2314 	}
2315 	if (itvp == 0)
2316 		return (0);
2317 	if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
2318 		return (EINVAL);
2319 	s = splclock();
2320 	if (which == ITIMER_REAL) {
2321 		callout_stop(&p->p_realit_ch);
2322 		if (timerisset(&aitv.it_value)) {
2323 			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 	*(netbsd32_ssize_t *)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 	*(netbsd32_ssize_t *)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 = (register_t)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_timeval(&ntv, &ntv32);
3756 	error = copyout((caddr_t)&ntv32, (caddr_t)SCARG(uap, tp), sizeof(ntv32));
3757 	if (!error) {
3758 
3759 		/*
3760 		 * Status word error decode. See comments in
3761 		 * ntp_gettime() routine.
3762 		 */
3763 		if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
3764 		    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
3765 		    !(time_status & STA_PPSSIGNAL)) ||
3766 		    (time_status & STA_PPSTIME &&
3767 		    time_status & STA_PPSJITTER) ||
3768 		    (time_status & STA_PPSFREQ &&
3769 		    time_status & (STA_PPSWANDER | STA_PPSERROR)))
3770 			*retval = TIME_ERROR;
3771 		else
3772 			*retval = (register_t)time_state;
3773 	}
3774 	return error;
3775 }
3776 #endif
3777 
3778 int
3779 netbsd32_setgid(p, v, retval)
3780 	struct proc *p;
3781 	void *v;
3782 	register_t *retval;
3783 {
3784 	struct netbsd32_setgid_args /* {
3785 		syscallarg(gid_t) gid;
3786 	} */ *uap = v;
3787 	struct sys_setgid_args ua;
3788 
3789 	NETBSD32TO64_UAP(gid);
3790 	return (sys_setgid(p, v, retval));
3791 }
3792 
3793 int
3794 netbsd32_setegid(p, v, retval)
3795 	struct proc *p;
3796 	void *v;
3797 	register_t *retval;
3798 {
3799 	struct netbsd32_setegid_args /* {
3800 		syscallarg(gid_t) egid;
3801 	} */ *uap = v;
3802 	struct sys_setegid_args ua;
3803 
3804 	NETBSD32TO64_UAP(egid);
3805 	return (sys_setegid(p, v, retval));
3806 }
3807 
3808 int
3809 netbsd32_seteuid(p, v, retval)
3810 	struct proc *p;
3811 	void *v;
3812 	register_t *retval;
3813 {
3814 	struct netbsd32_seteuid_args /* {
3815 		syscallarg(gid_t) euid;
3816 	} */ *uap = v;
3817 	struct sys_seteuid_args ua;
3818 
3819 	NETBSD32TO64_UAP(euid);
3820 	return (sys_seteuid(p, v, retval));
3821 }
3822 
3823 #ifdef LFS
3824 int
3825 netbsd32_sys_lfs_bmapv(p, v, retval)
3826 	struct proc *p;
3827 	void *v;
3828 	register_t *retval;
3829 {
3830 #if 0
3831 	struct netbsd32_lfs_bmapv_args /* {
3832 		syscallarg(netbsd32_fsid_tp_t) fsidp;
3833 		syscallarg(netbsd32_block_infop_t) blkiov;
3834 		syscallarg(int) blkcnt;
3835 	} */ *uap = v;
3836 	struct sys_lfs_bmapv_args ua;
3837 
3838 	NETBSD32TOP_UAP(fdidp, struct fsid);
3839 	NETBSD32TO64_UAP(blkcnt);
3840 	/* XXX finish me */
3841 #else
3842 
3843 	return (ENOSYS);	/* XXX */
3844 #endif
3845 }
3846 
3847 int
3848 netbsd32_sys_lfs_markv(p, v, retval)
3849 	struct proc *p;
3850 	void *v;
3851 	register_t *retval;
3852 {
3853 #if 0
3854 	struct netbsd32_lfs_markv_args /* {
3855 		syscallarg(netbsd32_fsid_tp_t) fsidp;
3856 		syscallarg(netbsd32_block_infop_t) blkiov;
3857 		syscallarg(int) blkcnt;
3858 	} */ *uap = v;
3859 #endif
3860 
3861 	return (ENOSYS);	/* XXX */
3862 }
3863 
3864 int
3865 netbsd32_sys_lfs_segclean(p, v, retval)
3866 	struct proc *p;
3867 	void *v;
3868 	register_t *retval;
3869 {
3870 #if 0
3871 	struct netbsd32_lfs_segclean_args /* {
3872 		syscallarg(netbsd32_fsid_tp_t) fsidp;
3873 		syscallarg(netbsd32_u_long) segment;
3874 	} */ *uap = v;
3875 #endif
3876 
3877 	return (ENOSYS);	/* XXX */
3878 }
3879 
3880 int
3881 netbsd32_sys_lfs_segwait(p, v, retval)
3882 	struct proc *p;
3883 	void *v;
3884 	register_t *retval;
3885 {
3886 #if 0
3887 	struct netbsd32_lfs_segwait_args /* {
3888 		syscallarg(netbsd32_fsid_tp_t) fsidp;
3889 		syscallarg(netbsd32_timevalp_t) tv;
3890 	} */ *uap = v;
3891 #endif
3892 
3893 	return (ENOSYS);	/* XXX */
3894 }
3895 #endif
3896 
3897 int
3898 netbsd32_pathconf(p, v, retval)
3899 	struct proc *p;
3900 	void *v;
3901 	register_t *retval;
3902 {
3903 	struct netbsd32_pathconf_args /* {
3904 		syscallarg(int) fd;
3905 		syscallarg(int) name;
3906 	} */ *uap = v;
3907 	struct sys_pathconf_args ua;
3908 	long rt;
3909 	int error;
3910 
3911 	NETBSD32TOP_UAP(path, const char);
3912 	NETBSD32TO64_UAP(name);
3913 	error = sys_pathconf(p, &ua, (register_t *)&rt);
3914 	*(netbsd32_long *)retval = (netbsd32_long)rt;
3915 	return (error);
3916 }
3917 
3918 int
3919 netbsd32_fpathconf(p, v, retval)
3920 	struct proc *p;
3921 	void *v;
3922 	register_t *retval;
3923 {
3924 	struct netbsd32_fpathconf_args /* {
3925 		syscallarg(int) fd;
3926 		syscallarg(int) name;
3927 	} */ *uap = v;
3928 	struct sys_fpathconf_args ua;
3929 	long rt;
3930 	int error;
3931 
3932 	NETBSD32TO64_UAP(fd);
3933 	NETBSD32TO64_UAP(name);
3934 	error = sys_fpathconf(p, &ua, (register_t *)&rt);
3935 	*(netbsd32_long *)retval = (netbsd32_long)rt;
3936 	return (error);
3937 }
3938 
3939 int
3940 netbsd32_getrlimit(p, v, retval)
3941 	struct proc *p;
3942 	void *v;
3943 	register_t *retval;
3944 {
3945 	struct netbsd32_getrlimit_args /* {
3946 		syscallarg(int) which;
3947 		syscallarg(netbsd32_rlimitp_t) rlp;
3948 	} */ *uap = v;
3949 	int which = SCARG(uap, which);
3950 
3951 	if ((u_int)which >= RLIM_NLIMITS)
3952 		return (EINVAL);
3953 	return (copyout(&p->p_rlimit[which], (caddr_t)(u_long)SCARG(uap, rlp),
3954 	    sizeof(struct rlimit)));
3955 }
3956 
3957 int
3958 netbsd32_setrlimit(p, v, retval)
3959 	struct proc *p;
3960 	void *v;
3961 	register_t *retval;
3962 {
3963 	struct netbsd32_setrlimit_args /* {
3964 		syscallarg(int) which;
3965 		syscallarg(const netbsd32_rlimitp_t) rlp;
3966 	} */ *uap = v;
3967 		int which = SCARG(uap, which);
3968 	struct rlimit alim;
3969 	int error;
3970 
3971 	error = copyin((caddr_t)(u_long)SCARG(uap, rlp), &alim, sizeof(struct rlimit));
3972 	if (error)
3973 		return (error);
3974 	return (dosetrlimit(p, p->p_cred, which, &alim));
3975 }
3976 
3977 int
3978 netbsd32_mmap(p, v, retval)
3979 	struct proc *p;
3980 	void *v;
3981 	register_t *retval;
3982 {
3983 	struct netbsd32_mmap_args /* {
3984 		syscallarg(netbsd32_voidp) addr;
3985 		syscallarg(netbsd32_size_t) len;
3986 		syscallarg(int) prot;
3987 		syscallarg(int) flags;
3988 		syscallarg(int) fd;
3989 		syscallarg(netbsd32_long) pad;
3990 		syscallarg(off_t) pos;
3991 	} */ *uap = v;
3992 	struct sys_mmap_args ua;
3993 	void *rt;
3994 	int error;
3995 
3996 	NETBSD32TOP_UAP(addr, void);
3997 	NETBSD32TOX_UAP(len, size_t);
3998 	NETBSD32TO64_UAP(prot);
3999 	NETBSD32TO64_UAP(flags);
4000 	NETBSD32TO64_UAP(fd);
4001 	NETBSD32TOX_UAP(pad, long);
4002 	NETBSD32TOX_UAP(pos, off_t);
4003 	error = sys_mmap(p, &ua, (register_t *)&rt);
4004 	if ((long)rt > (long)UINT_MAX)
4005 		printf("netbsd32_mmap: retval out of range: 0x%qx",
4006 		    rt);
4007 	*retval = (netbsd32_voidp)(u_long)rt;
4008 	return (error);
4009 }
4010 
4011 int
4012 netbsd32_lseek(p, v, retval)
4013 	struct proc *p;
4014 	void *v;
4015 	register_t *retval;
4016 {
4017 	struct netbsd32_lseek_args /* {
4018 		syscallarg(int) fd;
4019 		syscallarg(int) pad;
4020 		syscallarg(off_t) offset;
4021 		syscallarg(int) whence;
4022 	} */ *uap = v;
4023 	struct sys_lseek_args ua;
4024 
4025 	NETBSD32TO64_UAP(fd);
4026 	NETBSD32TO64_UAP(pad);
4027 	NETBSD32TO64_UAP(offset);
4028 	NETBSD32TO64_UAP(whence);
4029 	return (sys_lseek(p, &ua, retval));
4030 }
4031 
4032 int
4033 netbsd32_truncate(p, v, retval)
4034 	struct proc *p;
4035 	void *v;
4036 	register_t *retval;
4037 {
4038 	struct netbsd32_truncate_args /* {
4039 		syscallarg(const netbsd32_charp) path;
4040 		syscallarg(int) pad;
4041 		syscallarg(off_t) length;
4042 	} */ *uap = v;
4043 	struct sys_truncate_args ua;
4044 
4045 	NETBSD32TOP_UAP(path, const char);
4046 	NETBSD32TO64_UAP(pad);
4047 	NETBSD32TO64_UAP(length);
4048 	return (sys_truncate(p, &ua, retval));
4049 }
4050 
4051 int
4052 netbsd32_ftruncate(p, v, retval)
4053 	struct proc *p;
4054 	void *v;
4055 	register_t *retval;
4056 {
4057 	struct netbsd32_ftruncate_args /* {
4058 		syscallarg(int) fd;
4059 		syscallarg(int) pad;
4060 		syscallarg(off_t) length;
4061 	} */ *uap = v;
4062 	struct sys_ftruncate_args ua;
4063 
4064 	NETBSD32TO64_UAP(fd);
4065 	NETBSD32TO64_UAP(pad);
4066 	NETBSD32TO64_UAP(length);
4067 	return (sys_ftruncate(p, &ua, retval));
4068 }
4069 
4070 int
4071 netbsd32___sysctl(p, v, retval)
4072 	struct proc *p;
4073 	void *v;
4074 	register_t *retval;
4075 {
4076 	struct netbsd32___sysctl_args /* {
4077 		syscallarg(netbsd32_intp) name;
4078 		syscallarg(u_int) namelen;
4079 		syscallarg(netbsd32_voidp) old;
4080 		syscallarg(netbsd32_size_tp) oldlenp;
4081 		syscallarg(netbsd32_voidp) new;
4082 		syscallarg(netbsd32_size_t) newlen;
4083 	} */ *uap = v;
4084 	int error, dolock = 1;
4085 	netbsd32_size_t savelen = 0;
4086 	size_t oldlen = 0;
4087 	sysctlfn *fn;
4088 	int name[CTL_MAXNAME];
4089 
4090 /*
4091  * Some of these sysctl functions do their own copyin/copyout.
4092  * We need to disable or emulate the ones that need their
4093  * arguments converted.
4094  */
4095 
4096 	if (SCARG(uap, new) != NULL &&
4097 	    (error = suser(p->p_ucred, &p->p_acflag)))
4098 		return (error);
4099 	/*
4100 	 * all top-level sysctl names are non-terminal
4101 	 */
4102 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
4103 		return (EINVAL);
4104 	error = copyin((caddr_t)(u_long)SCARG(uap, name), &name,
4105 		       SCARG(uap, namelen) * sizeof(int));
4106 	if (error)
4107 		return (error);
4108 
4109 	switch (name[0]) {
4110 	case CTL_KERN:
4111 		fn = kern_sysctl;
4112 		if (name[2] != KERN_VNODE)	/* XXX */
4113 			dolock = 0;
4114 		break;
4115 	case CTL_HW:
4116 		fn = hw_sysctl;
4117 		break;
4118 	case CTL_VM:
4119 		fn = uvm_sysctl;
4120 		break;
4121 	case CTL_NET:
4122 		fn = net_sysctl;
4123 		break;
4124 	case CTL_VFS:
4125 		fn = vfs_sysctl;
4126 		break;
4127 	case CTL_MACHDEP:
4128 		fn = cpu_sysctl;
4129 		break;
4130 #ifdef DEBUG
4131 	case CTL_DEBUG:
4132 		fn = debug_sysctl;
4133 		break;
4134 #endif
4135 #ifdef DDB
4136 	case CTL_DDB:
4137 		fn = ddb_sysctl;
4138 		break;
4139 #endif
4140 	default:
4141 		return (EOPNOTSUPP);
4142 	}
4143 
4144 	if (SCARG(uap, oldlenp) &&
4145 	    (error = copyin((caddr_t)(u_long)SCARG(uap, oldlenp), &savelen, sizeof(savelen))))
4146 		return (error);
4147 	if (SCARG(uap, old) != NULL) {
4148 		if (!uvm_useracc((caddr_t)(u_long)SCARG(uap, old), savelen, B_WRITE))
4149 			return (EFAULT);
4150 #if 0 /* XXXXXXXX */
4151 		while (memlock.sl_lock) {
4152 			memlock.sl_want = 1;
4153 			(void) tsleep(&memlock, PRIBIO+1, "memlock", 0);
4154 			memlock.sl_locked++;
4155 		}
4156 		memlock.sl_lock = 1;
4157 #endif /* XXXXXXXX */
4158 		if (dolock) {
4159 			/*
4160 			 * XXX Um, this is kind of evil.  What should
4161 			 * XXX we be passing here?
4162 			 */
4163 			if (uvm_vslock(p, (void *)(u_long)SCARG(uap, old), savelen,
4164 			    VM_PROT_NONE) != KERN_SUCCESS) {
4165 #if 0 /* XXXXXXXX */
4166 				memlock.sl_lock = 0;
4167 				if (memlock.sl_want) {
4168 					memlock.sl_want = 0;
4169 					wakeup((caddr_t)&memlock);
4170 				}
4171 #endif /* XXXXXXXX */
4172 				return (EFAULT);
4173 			}
4174 		}
4175 		oldlen = savelen;
4176 	}
4177 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1,
4178 		      (void *)(u_long)SCARG(uap, old), &oldlen,
4179 		      (void *)(u_long)SCARG(uap, new), SCARG(uap, newlen), p);
4180 	if (SCARG(uap, old) != NULL) {
4181 		if (dolock)
4182 			uvm_vsunlock(p, (void *)(u_long)SCARG(uap, old), savelen);
4183 #if 0 /* XXXXXXXXXXX */
4184 		memlock.sl_lock = 0;
4185 		if (memlock.sl_want) {
4186 			memlock.sl_want = 0;
4187 			wakeup((caddr_t)&memlock);
4188 		}
4189 #endif /* XXXXXXXXX */
4190 	}
4191 	savelen = oldlen;
4192 	if (error)
4193 		return (error);
4194 	if (SCARG(uap, oldlenp))
4195 		error = copyout(&savelen, (caddr_t)(u_long)SCARG(uap, oldlenp), sizeof(savelen));
4196 	return (error);
4197 }
4198 
4199 int
4200 netbsd32_mlock(p, v, retval)
4201 	struct proc *p;
4202 	void *v;
4203 	register_t *retval;
4204 {
4205 	struct netbsd32_mlock_args /* {
4206 		syscallarg(const netbsd32_voidp) addr;
4207 		syscallarg(netbsd32_size_t) len;
4208 	} */ *uap = v;
4209 	struct sys_mlock_args ua;
4210 
4211 	NETBSD32TOP_UAP(addr, const void);
4212 	NETBSD32TO64_UAP(len);
4213 	return (sys_mlock(p, &ua, retval));
4214 }
4215 
4216 int
4217 netbsd32_munlock(p, v, retval)
4218 	struct proc *p;
4219 	void *v;
4220 	register_t *retval;
4221 {
4222 	struct netbsd32_munlock_args /* {
4223 		syscallarg(const netbsd32_voidp) addr;
4224 		syscallarg(netbsd32_size_t) len;
4225 	} */ *uap = v;
4226 	struct sys_munlock_args ua;
4227 
4228 	NETBSD32TOP_UAP(addr, const void);
4229 	NETBSD32TO64_UAP(len);
4230 	return (sys_munlock(p, &ua, retval));
4231 }
4232 
4233 int
4234 netbsd32_undelete(p, v, retval)
4235 	struct proc *p;
4236 	void *v;
4237 	register_t *retval;
4238 {
4239 	struct netbsd32_undelete_args /* {
4240 		syscallarg(const netbsd32_charp) path;
4241 	} */ *uap = v;
4242 	struct sys_undelete_args ua;
4243 
4244 	NETBSD32TOP_UAP(path, const char);
4245 	return (sys_undelete(p, &ua, retval));
4246 }
4247 
4248 int
4249 netbsd32_futimes(p, v, retval)
4250 	struct proc *p;
4251 	void *v;
4252 	register_t *retval;
4253 {
4254 	struct netbsd32_futimes_args /* {
4255 		syscallarg(int) fd;
4256 		syscallarg(const netbsd32_timevalp_t) tptr;
4257 	} */ *uap = v;
4258 	int error;
4259 	struct file *fp;
4260 
4261 	/* getvnode() will use the descriptor for us */
4262 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
4263 		return (error);
4264 
4265 	error = change_utimes32((struct vnode *)fp->f_data,
4266 				(struct timeval *)(u_long)SCARG(uap, tptr), p);
4267 	FILE_UNUSE(fp, p);
4268 	return (error);
4269 }
4270 
4271 int
4272 netbsd32_getpgid(p, v, retval)
4273 	struct proc *p;
4274 	void *v;
4275 	register_t *retval;
4276 {
4277 	struct netbsd32_getpgid_args /* {
4278 		syscallarg(pid_t) pid;
4279 	} */ *uap = v;
4280 	struct sys_getpgid_args ua;
4281 
4282 	NETBSD32TO64_UAP(pid);
4283 	return (sys_getpgid(p, &ua, retval));
4284 }
4285 
4286 int
4287 netbsd32_reboot(p, v, retval)
4288 	struct proc *p;
4289 	void *v;
4290 	register_t *retval;
4291 {
4292 	struct netbsd32_reboot_args /* {
4293 		syscallarg(int) opt;
4294 		syscallarg(netbsd32_charp) bootstr;
4295 	} */ *uap = v;
4296 	struct sys_reboot_args ua;
4297 
4298 	NETBSD32TO64_UAP(opt);
4299 	NETBSD32TOP_UAP(bootstr, char);
4300 	return (sys_reboot(p, &ua, retval));
4301 }
4302 
4303 int
4304 netbsd32_poll(p, v, retval)
4305 	struct proc *p;
4306 	void *v;
4307 	register_t *retval;
4308 {
4309 	struct netbsd32_poll_args /* {
4310 		syscallarg(netbsd32_pollfdp_t) fds;
4311 		syscallarg(u_int) nfds;
4312 		syscallarg(int) timeout;
4313 	} */ *uap = v;
4314 	struct sys_poll_args ua;
4315 
4316 	NETBSD32TOP_UAP(fds, struct pollfd);
4317 	NETBSD32TO64_UAP(nfds);
4318 	NETBSD32TO64_UAP(timeout);
4319 	return (sys_poll(p, &ua, retval));
4320 }
4321 
4322 #if defined(SYSVSEM)
4323 /*
4324  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
4325  *
4326  * This is BSD.  We won't support System V IPC.
4327  * Too much work.
4328  *
4329  * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
4330  */
4331 int
4332 netbsd32___semctl14(p, v, retval)
4333 	struct proc *p;
4334 	void *v;
4335 	register_t *retval;
4336 {
4337 #if 0
4338 	struct netbsd32___semctl_args /* {
4339 		syscallarg(int) semid;
4340 		syscallarg(int) semnum;
4341 		syscallarg(int) cmd;
4342 		syscallarg(netbsd32_semunu_t *) arg;
4343 	} */ *uap = v;
4344 	union netbsd32_semun sem32;
4345 	int semid = SCARG(uap, semid);
4346 	int semnum = SCARG(uap, semnum);
4347 	int cmd = SCARG(uap, cmd);
4348 	union netbsd32_semun *arg = (void*)(u_long)SCARG(uap, arg);
4349 	union netbsd32_semun real_arg;
4350 	struct ucred *cred = p->p_ucred;
4351 	int i, rval, eval;
4352 	struct netbsd32_semid_ds sbuf;
4353 	struct semid_ds *semaptr;
4354 
4355 	semlock(p);
4356 
4357 	semid = IPCID_TO_IX(semid);
4358 	if (semid < 0 || semid >= seminfo.semmsl)
4359 		return(EINVAL);
4360 
4361 	semaptr = &sema[semid];
4362 	if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
4363 	    semaptr->sem_perm.seq != IPCID_TO_SEQ(SCARG(uap, semid)))
4364 		return(EINVAL);
4365 
4366 	eval = 0;
4367 	rval = 0;
4368 
4369 	switch (cmd) {
4370 	case IPC_RMID:
4371 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)) != 0)
4372 			return(eval);
4373 		semaptr->sem_perm.cuid = cred->cr_uid;
4374 		semaptr->sem_perm.uid = cred->cr_uid;
4375 		semtot -= semaptr->sem_nsems;
4376 		for (i = semaptr->_sem_base - sem; i < semtot; i++)
4377 			sem[i] = sem[i + semaptr->sem_nsems];
4378 		for (i = 0; i < seminfo.semmni; i++) {
4379 			if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
4380 			    sema[i]._sem_base > semaptr->_sem_base)
4381 				sema[i]._sem_base -= semaptr->sem_nsems;
4382 		}
4383 		semaptr->sem_perm.mode = 0;
4384 		semundo_clear(semid, -1);
4385 		wakeup((caddr_t)semaptr);
4386 		break;
4387 
4388 	case IPC_SET:
4389 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_M)))
4390 			return(eval);
4391 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
4392 			return(eval);
4393 		if ((eval = copyin((caddr_t)(u_long)real_arg.buf, (caddr_t)&sbuf,
4394 		    sizeof(sbuf))) != 0)
4395 			return(eval);
4396 		semaptr->sem_perm.uid = sbuf.sem_perm.uid;
4397 		semaptr->sem_perm.gid = sbuf.sem_perm.gid;
4398 		semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
4399 		    (sbuf.sem_perm.mode & 0777);
4400 		semaptr->sem_ctime = time.tv_sec;
4401 		break;
4402 
4403 	case IPC_STAT:
4404 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
4405 			return(eval);
4406 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
4407 			return(eval);
4408 		eval = copyout((caddr_t)semaptr, (caddr_t)(u_long)real_arg.buf,
4409 		    sizeof(struct semid_ds));
4410 		break;
4411 
4412 	case GETNCNT:
4413 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
4414 			return(eval);
4415 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
4416 			return(EINVAL);
4417 		rval = semaptr->_sem_base[semnum].semncnt;
4418 		break;
4419 
4420 	case GETPID:
4421 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
4422 			return(eval);
4423 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
4424 			return(EINVAL);
4425 		rval = semaptr->_sem_base[semnum].sempid;
4426 		break;
4427 
4428 	case GETVAL:
4429 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
4430 			return(eval);
4431 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
4432 			return(EINVAL);
4433 		rval = semaptr->_sem_base[semnum].semval;
4434 		break;
4435 
4436 	case GETALL:
4437 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
4438 			return(eval);
4439 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
4440 			return(eval);
4441 		for (i = 0; i < semaptr->sem_nsems; i++) {
4442 			eval = copyout((caddr_t)&semaptr->_sem_base[i].semval,
4443 			    &real_arg.array[i], sizeof(real_arg.array[0]));
4444 			if (eval != 0)
4445 				break;
4446 		}
4447 		break;
4448 
4449 	case GETZCNT:
4450 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_R)))
4451 			return(eval);
4452 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
4453 			return(EINVAL);
4454 		rval = semaptr->_sem_base[semnum].semzcnt;
4455 		break;
4456 
4457 	case SETVAL:
4458 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
4459 			return(eval);
4460 		if (semnum < 0 || semnum >= semaptr->sem_nsems)
4461 			return(EINVAL);
4462 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
4463 			return(eval);
4464 		semaptr->_sem_base[semnum].semval = real_arg.val;
4465 		semundo_clear(semid, semnum);
4466 		wakeup((caddr_t)semaptr);
4467 		break;
4468 
4469 	case SETALL:
4470 		if ((eval = ipcperm(cred, &semaptr->sem_perm, IPC_W)))
4471 			return(eval);
4472 		if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
4473 			return(eval);
4474 		for (i = 0; i < semaptr->sem_nsems; i++) {
4475 			eval = copyin(&real_arg.array[i],
4476 			    (caddr_t)&semaptr->_sem_base[i].semval,
4477 			    sizeof(real_arg.array[0]));
4478 			if (eval != 0)
4479 				break;
4480 		}
4481 		semundo_clear(semid, -1);
4482 		wakeup((caddr_t)semaptr);
4483 		break;
4484 
4485 	default:
4486 		return(EINVAL);
4487 	}
4488 
4489 	if (eval == 0)
4490 		*retval = rval;
4491 	return(eval);
4492 #else
4493 	return (ENOSYS);
4494 #endif
4495 }
4496 
4497 int
4498 netbsd32_semget(p, v, retval)
4499 	struct proc *p;
4500 	void *v;
4501 	register_t *retval;
4502 {
4503 	struct netbsd32_semget_args /* {
4504 		syscallarg(netbsd32_key_t) key;
4505 		syscallarg(int) nsems;
4506 		syscallarg(int) semflg;
4507 	} */ *uap = v;
4508 	struct sys_semget_args ua;
4509 
4510 	NETBSD32TOX_UAP(key, key_t);
4511 	NETBSD32TO64_UAP(nsems);
4512 	NETBSD32TO64_UAP(semflg);
4513 	return (sys_semget(p, &ua, retval));
4514 }
4515 
4516 int
4517 netbsd32_semop(p, v, retval)
4518 	struct proc *p;
4519 	void *v;
4520 	register_t *retval;
4521 {
4522 	struct netbsd32_semop_args /* {
4523 		syscallarg(int) semid;
4524 		syscallarg(netbsd32_sembufp_t) sops;
4525 		syscallarg(netbsd32_size_t) nsops;
4526 	} */ *uap = v;
4527 	struct sys_semop_args ua;
4528 
4529 	NETBSD32TO64_UAP(semid);
4530 	NETBSD32TOP_UAP(sops, struct sembuf);
4531 	NETBSD32TOX_UAP(nsops, size_t);
4532 	return (sys_semop(p, &ua, retval));
4533 }
4534 
4535 int
4536 netbsd32_semconfig(p, v, retval)
4537 	struct proc *p;
4538 	void *v;
4539 	register_t *retval;
4540 {
4541 	struct netbsd32_semconfig_args /* {
4542 		syscallarg(int) flag;
4543 	} */ *uap = v;
4544 	struct sys_semconfig_args ua;
4545 
4546 	NETBSD32TO64_UAP(flag);
4547 	return (sys_semconfig(p, &ua, retval));
4548 }
4549 #endif /* SYSVSEM */
4550 
4551 #if defined(SYSVMSG)
4552 
4553 int
4554 netbsd32___msgctl13(p, v, retval)
4555 	struct proc *p;
4556 	void *v;
4557 	register_t *retval;
4558 {
4559 #if 0
4560 	struct netbsd32_msgctl_args /* {
4561 		syscallarg(int) msqid;
4562 		syscallarg(int) cmd;
4563 		syscallarg(netbsd32_msqid_dsp_t) buf;
4564 	} */ *uap = v;
4565 	struct sys_msgctl_args ua;
4566 	struct msqid_ds ds;
4567 	struct netbsd32_msqid_ds *ds32p;
4568 	int error;
4569 
4570 	NETBSD32TO64_UAP(msqid);
4571 	NETBSD32TO64_UAP(cmd);
4572 	ds32p = (struct netbsd32_msqid_ds *)(u_long)SCARG(uap, buf);
4573 	if (ds32p) {
4574 		SCARG(&ua, buf) = NULL;
4575 		netbsd32_to_msqid_ds(ds32p, &ds);
4576 	} else
4577 		SCARG(&ua, buf) = NULL;
4578 	error = sys_msgctl(p, &ua, retval);
4579 	if (error)
4580 		return (error);
4581 
4582 	if (ds32p)
4583 		netbsd32_from_msqid_ds(&ds, ds32p);
4584 	return (0);
4585 #else
4586 	return (ENOSYS);
4587 #endif
4588 }
4589 
4590 int
4591 netbsd32_msgget(p, v, retval)
4592 	struct proc *p;
4593 	void *v;
4594 	register_t *retval;
4595 {
4596 #if 0
4597 	struct netbsd32_msgget_args /* {
4598 		syscallarg(netbsd32_key_t) key;
4599 		syscallarg(int) msgflg;
4600 	} */ *uap = v;
4601 	struct sys_msgget_args ua;
4602 
4603 	NETBSD32TOX_UAP(key, key_t);
4604 	NETBSD32TO64_UAP(msgflg);
4605 	return (sys_msgget(p, &ua, retval));
4606 #else
4607 	return (ENOSYS);
4608 #endif
4609 }
4610 
4611 int
4612 netbsd32_msgsnd(p, v, retval)
4613 	struct proc *p;
4614 	void *v;
4615 	register_t *retval;
4616 {
4617 #if 0
4618 	struct netbsd32_msgsnd_args /* {
4619 		syscallarg(int) msqid;
4620 		syscallarg(const netbsd32_voidp) msgp;
4621 		syscallarg(netbsd32_size_t) msgsz;
4622 		syscallarg(int) msgflg;
4623 	} */ *uap = v;
4624 	struct sys_msgsnd_args ua;
4625 
4626 	NETBSD32TO64_UAP(msqid);
4627 	NETBSD32TOP_UAP(msgp, void);
4628 	NETBSD32TOX_UAP(msgsz, size_t);
4629 	NETBSD32TO64_UAP(msgflg);
4630 	return (sys_msgsnd(p, &ua, retval));
4631 #else
4632 	return (ENOSYS);
4633 #endif
4634 }
4635 
4636 int
4637 netbsd32_msgrcv(p, v, retval)
4638 	struct proc *p;
4639 	void *v;
4640 	register_t *retval;
4641 {
4642 #if 0
4643 	struct netbsd32_msgrcv_args /* {
4644 		syscallarg(int) msqid;
4645 		syscallarg(netbsd32_voidp) msgp;
4646 		syscallarg(netbsd32_size_t) msgsz;
4647 		syscallarg(netbsd32_long) msgtyp;
4648 		syscallarg(int) msgflg;
4649 	} */ *uap = v;
4650 	struct sys_msgrcv_args ua;
4651 	ssize_t rt;
4652 	int error;
4653 
4654 	NETBSD32TO64_UAP(msqid);
4655 	NETBSD32TOP_UAP(msgp, void);
4656 	NETBSD32TOX_UAP(msgsz, size_t);
4657 	NETBSD32TOX_UAP(msgtyp, long);
4658 	NETBSD32TO64_UAP(msgflg);
4659 	error = sys_msgrcv(p, &ua, (register_t *)&rt);
4660 	*(netbsd32_ssize_t *)retval = rt;
4661 	return (error);
4662 #else
4663 	return (ENOSYS);
4664 #endif
4665 }
4666 #endif /* SYSVMSG */
4667 
4668 #if defined(SYSVSHM)
4669 
4670 int
4671 netbsd32_shmat(p, v, retval)
4672 	struct proc *p;
4673 	void *v;
4674 	register_t *retval;
4675 {
4676 #if 0
4677 	struct netbsd32_shmat_args /* {
4678 		syscallarg(int) shmid;
4679 		syscallarg(const netbsd32_voidp) shmaddr;
4680 		syscallarg(int) shmflg;
4681 	} */ *uap = v;
4682 	struct sys_shmat_args ua;
4683 	void *rt;
4684 	int error;
4685 
4686 	NETBSD32TO64_UAP(shmid);
4687 	NETBSD32TOP_UAP(shmaddr, void);
4688 	NETBSD32TO64_UAP(shmflg);
4689 	error = sys_shmat(p, &ua, (register_t *)&rt);
4690 	*retval = (netbsd32_voidp)(u_long)rt;
4691 	return (error);
4692 #else
4693 	return (ENOSYS);
4694 #endif
4695 }
4696 
4697 int
4698 netbsd32___shmctl13(p, v, retval)
4699 	struct proc *p;
4700 	void *v;
4701 	register_t *retval;
4702 {
4703 #if 0
4704 	struct netbsd32_shmctl_args /* {
4705 		syscallarg(int) shmid;
4706 		syscallarg(int) cmd;
4707 		syscallarg(netbsd32_shmid_dsp_t) buf;
4708 	} */ *uap = v;
4709 	struct sys_shmctl_args ua;
4710 	struct shmid_ds ds;
4711 	struct netbsd32_shmid_ds *ds32p;
4712 	int error;
4713 
4714 	NETBSD32TO64_UAP(shmid);
4715 	NETBSD32TO64_UAP(cmd);
4716 	ds32p = (struct netbsd32_shmid_ds *)(u_long)SCARG(uap, buf);
4717 	if (ds32p) {
4718 		SCARG(&ua, buf) = NULL;
4719 		netbsd32_to_shmid_ds(ds32p, &ds);
4720 	} else
4721 		SCARG(&ua, buf) = NULL;
4722 	error = sys_shmctl(p, &ua, retval);
4723 	if (error)
4724 		return (error);
4725 
4726 	if (ds32p)
4727 		netbsd32_from_shmid_ds(&ds, ds32p);
4728 	return (0);
4729 #else
4730 	return (ENOSYS);
4731 #endif
4732 }
4733 
4734 int
4735 netbsd32_shmdt(p, v, retval)
4736 	struct proc *p;
4737 	void *v;
4738 	register_t *retval;
4739 {
4740 #if 0
4741 	struct netbsd32_shmdt_args /* {
4742 		syscallarg(const netbsd32_voidp) shmaddr;
4743 	} */ *uap = v;
4744 	struct sys_shmdt_args ua;
4745 
4746 	NETBSD32TOP_UAP(shmaddr, const char);
4747 	return (sys_shmdt(p, &ua, retval));
4748 #else
4749 	return (ENOSYS);
4750 #endif
4751 }
4752 
4753 int
4754 netbsd32_shmget(p, v, retval)
4755 	struct proc *p;
4756 	void *v;
4757 	register_t *retval;
4758 {
4759 #if 0
4760 	struct netbsd32_shmget_args /* {
4761 		syscallarg(netbsd32_key_t) key;
4762 		syscallarg(netbsd32_size_t) size;
4763 		syscallarg(int) shmflg;
4764 	} */ *uap = v;
4765 	struct sys_shmget_args ua;
4766 
4767 	NETBSD32TOX_UAP(key, key_t)
4768 	NETBSD32TOX_UAP(size, size_t)
4769 	NETBSD32TO64_UAP(shmflg);
4770 	return (sys_shmget(p, &ua, retval));
4771 #else
4772 	return (ENOSYS);
4773 #endif
4774 }
4775 #endif /* SYSVSHM */
4776 
4777 int
4778 netbsd32_clock_gettime(p, v, retval)
4779 	struct proc *p;
4780 	void *v;
4781 	register_t *retval;
4782 {
4783 	struct netbsd32_clock_gettime_args /* {
4784 		syscallarg(netbsd32_clockid_t) clock_id;
4785 		syscallarg(netbsd32_timespecp_t) tp;
4786 	} */ *uap = v;
4787 	clockid_t clock_id;
4788 	struct timeval atv;
4789 	struct timespec ats;
4790 	struct netbsd32_timespec ts32;
4791 
4792 	clock_id = SCARG(uap, clock_id);
4793 	if (clock_id != CLOCK_REALTIME)
4794 		return (EINVAL);
4795 
4796 	microtime(&atv);
4797 	TIMEVAL_TO_TIMESPEC(&atv,&ats);
4798 	netbsd32_from_timespec(&ats, &ts32);
4799 
4800 	return copyout(&ts32, (caddr_t)(u_long)SCARG(uap, tp), sizeof(ts32));
4801 }
4802 
4803 int
4804 netbsd32_clock_settime(p, v, retval)
4805 	struct proc *p;
4806 	void *v;
4807 	register_t *retval;
4808 {
4809 	struct netbsd32_clock_settime_args /* {
4810 		syscallarg(netbsd32_clockid_t) clock_id;
4811 		syscallarg(const netbsd32_timespecp_t) tp;
4812 	} */ *uap = v;
4813 	struct netbsd32_timespec ts32;
4814 	clockid_t clock_id;
4815 	struct timeval atv;
4816 	struct timespec ats;
4817 	int error;
4818 
4819 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
4820 		return (error);
4821 
4822 	clock_id = SCARG(uap, clock_id);
4823 	if (clock_id != CLOCK_REALTIME)
4824 		return (EINVAL);
4825 
4826 	if ((error = copyin((caddr_t)(u_long)SCARG(uap, tp), &ts32, sizeof(ts32))) != 0)
4827 		return (error);
4828 
4829 	netbsd32_to_timespec(&ts32, &ats);
4830 	TIMESPEC_TO_TIMEVAL(&atv,&ats);
4831 	if ((error = settime(&atv)))
4832 		return (error);
4833 
4834 	return 0;
4835 }
4836 
4837 int
4838 netbsd32_clock_getres(p, v, retval)
4839 	struct proc *p;
4840 	void *v;
4841 	register_t *retval;
4842 {
4843 	struct netbsd32_clock_getres_args /* {
4844 		syscallarg(netbsd32_clockid_t) clock_id;
4845 		syscallarg(netbsd32_timespecp_t) tp;
4846 	} */ *uap = v;
4847 	struct netbsd32_timespec ts32;
4848 	clockid_t clock_id;
4849 	struct timespec ts;
4850 	int error = 0;
4851 
4852 	clock_id = SCARG(uap, clock_id);
4853 	if (clock_id != CLOCK_REALTIME)
4854 		return (EINVAL);
4855 
4856 	if (SCARG(uap, tp)) {
4857 		ts.tv_sec = 0;
4858 		ts.tv_nsec = 1000000000 / hz;
4859 
4860 		netbsd32_from_timespec(&ts, &ts32);
4861 		error = copyout(&ts, (caddr_t)(u_long)SCARG(uap, tp), sizeof(ts));
4862 	}
4863 
4864 	return error;
4865 }
4866 
4867 int
4868 netbsd32_nanosleep(p, v, retval)
4869 	struct proc *p;
4870 	void *v;
4871 	register_t *retval;
4872 {
4873 	struct netbsd32_nanosleep_args /* {
4874 		syscallarg(const netbsd32_timespecp_t) rqtp;
4875 		syscallarg(netbsd32_timespecp_t) rmtp;
4876 	} */ *uap = v;
4877 	static int nanowait;
4878 	struct netbsd32_timespec ts32;
4879 	struct timespec rqt;
4880 	struct timespec rmt;
4881 	struct timeval atv, utv;
4882 	int error, s, timo;
4883 
4884 	error = copyin((caddr_t)(u_long)SCARG(uap, rqtp), (caddr_t)&ts32,
4885 		       sizeof(ts32));
4886 	if (error)
4887 		return (error);
4888 
4889 	netbsd32_to_timespec(&ts32, &rqt);
4890 	TIMESPEC_TO_TIMEVAL(&atv,&rqt)
4891 	if (itimerfix(&atv))
4892 		return (EINVAL);
4893 
4894 	s = splclock();
4895 	timeradd(&atv,&time,&atv);
4896 	timo = hzto(&atv);
4897 	/*
4898 	 * Avoid inadvertantly sleeping forever
4899 	 */
4900 	if (timo == 0)
4901 		timo = 1;
4902 	splx(s);
4903 
4904 	error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
4905 	if (error == ERESTART)
4906 		error = EINTR;
4907 	if (error == EWOULDBLOCK)
4908 		error = 0;
4909 
4910 	if (SCARG(uap, rmtp)) {
4911 		int error;
4912 
4913 		s = splclock();
4914 		utv = time;
4915 		splx(s);
4916 
4917 		timersub(&atv, &utv, &utv);
4918 		if (utv.tv_sec < 0)
4919 			timerclear(&utv);
4920 
4921 		TIMEVAL_TO_TIMESPEC(&utv,&rmt);
4922 		netbsd32_from_timespec(&rmt, &ts32);
4923 		error = copyout((caddr_t)&ts32, (caddr_t)(u_long)SCARG(uap,rmtp),
4924 			sizeof(ts32));
4925 		if (error)
4926 			return (error);
4927 	}
4928 
4929 	return error;
4930 }
4931 
4932 int
4933 netbsd32_fdatasync(p, v, retval)
4934 	struct proc *p;
4935 	void *v;
4936 	register_t *retval;
4937 {
4938 	struct netbsd32_fdatasync_args /* {
4939 		syscallarg(int) fd;
4940 	} */ *uap = v;
4941 	struct sys_fdatasync_args ua;
4942 
4943 	NETBSD32TO64_UAP(fd);
4944 
4945 	return (sys_fdatasync(p, &ua, retval));
4946 }
4947 
4948 int
4949 netbsd32___posix_rename(p, v, retval)
4950 	struct proc *p;
4951 	void *v;
4952 	register_t *retval;
4953 {
4954 	struct netbsd32___posix_rename_args /* {
4955 		syscallarg(const netbsd32_charp) from;
4956 		syscallarg(const netbsd32_charp) to;
4957 	} */ *uap = v;
4958 	struct sys___posix_rename_args ua;
4959 
4960 	NETBSD32TOP_UAP(from, const char);
4961 	NETBSD32TOP_UAP(to, const char);
4962 
4963 	return (sys___posix_rename(p, &ua, retval));
4964 }
4965 
4966 int
4967 netbsd32_swapctl(p, v, retval)
4968 	struct proc *p;
4969 	void *v;
4970 	register_t *retval;
4971 {
4972 	struct netbsd32_swapctl_args /* {
4973 		syscallarg(int) cmd;
4974 		syscallarg(const netbsd32_voidp) arg;
4975 		syscallarg(int) misc;
4976 	} */ *uap = v;
4977 	struct sys_swapctl_args ua;
4978 
4979 	NETBSD32TO64_UAP(cmd);
4980 	NETBSD32TOP_UAP(arg, const void);
4981 	NETBSD32TO64_UAP(misc);
4982 	return (sys_swapctl(p, &ua, retval));
4983 }
4984 
4985 int
4986 netbsd32_getdents(p, v, retval)
4987 	struct proc *p;
4988 	void *v;
4989 	register_t *retval;
4990 {
4991 	struct netbsd32_getdents_args /* {
4992 		syscallarg(int) fd;
4993 		syscallarg(netbsd32_charp) buf;
4994 		syscallarg(netbsd32_size_t) count;
4995 	} */ *uap = v;
4996 	struct file *fp;
4997 	int error, done;
4998 
4999 	/* getvnode() will use the descriptor for us */
5000 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
5001 		return (error);
5002 	if ((fp->f_flag & FREAD) == 0) {
5003 		error = EBADF;
5004 		goto out;
5005 	}
5006 	error = vn_readdir(fp, (caddr_t)(u_long)SCARG(uap, buf), UIO_USERSPACE,
5007 			SCARG(uap, count), &done, p, 0, 0);
5008 	*retval = done;
5009  out:
5010 	FILE_UNUSE(fp, p);
5011 	return (error);
5012 }
5013 
5014 
5015 int
5016 netbsd32_minherit(p, v, retval)
5017 	struct proc *p;
5018 	void *v;
5019 	register_t *retval;
5020 {
5021 	struct netbsd32_minherit_args /* {
5022 		syscallarg(netbsd32_voidp) addr;
5023 		syscallarg(netbsd32_size_t) len;
5024 		syscallarg(int) inherit;
5025 	} */ *uap = v;
5026 	struct sys_minherit_args ua;
5027 
5028 	NETBSD32TOP_UAP(addr, void);
5029 	NETBSD32TOX_UAP(len, size_t);
5030 	NETBSD32TO64_UAP(inherit);
5031 	return (sys_minherit(p, &ua, retval));
5032 }
5033 
5034 int
5035 netbsd32_lchmod(p, v, retval)
5036 	struct proc *p;
5037 	void *v;
5038 	register_t *retval;
5039 {
5040 	struct netbsd32_lchmod_args /* {
5041 		syscallarg(const netbsd32_charp) path;
5042 		syscallarg(mode_t) mode;
5043 	} */ *uap = v;
5044 	struct sys_lchmod_args ua;
5045 
5046 	NETBSD32TOP_UAP(path, const char);
5047 	NETBSD32TO64_UAP(mode);
5048 	return (sys_lchmod(p, &ua, retval));
5049 }
5050 
5051 int
5052 netbsd32_lchown(p, v, retval)
5053 	struct proc *p;
5054 	void *v;
5055 	register_t *retval;
5056 {
5057 	struct netbsd32_lchown_args /* {
5058 		syscallarg(const netbsd32_charp) path;
5059 		syscallarg(uid_t) uid;
5060 		syscallarg(gid_t) gid;
5061 	} */ *uap = v;
5062 	struct sys_lchown_args ua;
5063 
5064 	NETBSD32TOP_UAP(path, const char);
5065 	NETBSD32TO64_UAP(uid);
5066 	NETBSD32TO64_UAP(gid);
5067 	return (sys_lchown(p, &ua, retval));
5068 }
5069 
5070 int
5071 netbsd32_lutimes(p, v, retval)
5072 	struct proc *p;
5073 	void *v;
5074 	register_t *retval;
5075 {
5076 	struct netbsd32_lutimes_args /* {
5077 		syscallarg(const netbsd32_charp) path;
5078 		syscallarg(const netbsd32_timevalp_t) tptr;
5079 	} */ *uap = v;
5080 	int error;
5081 	struct nameidata nd;
5082 
5083 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, (caddr_t)(u_long)SCARG(uap, path), p);
5084 	if ((error = namei(&nd)) != 0)
5085 		return (error);
5086 
5087 	error = change_utimes32(nd.ni_vp, (struct timeval *)(u_long)SCARG(uap, tptr), p);
5088 
5089 	vrele(nd.ni_vp);
5090 	return (error);
5091 }
5092 
5093 
5094 int
5095 netbsd32___msync13(p, v, retval)
5096 	struct proc *p;
5097 	void *v;
5098 	register_t *retval;
5099 {
5100 	struct netbsd32___msync13_args /* {
5101 		syscallarg(netbsd32_voidp) addr;
5102 		syscallarg(netbsd32_size_t) len;
5103 		syscallarg(int) flags;
5104 	} */ *uap = v;
5105 	struct sys___msync13_args ua;
5106 
5107 	NETBSD32TOP_UAP(addr, void);
5108 	NETBSD32TOX_UAP(len, size_t);
5109 	NETBSD32TO64_UAP(flags);
5110 	return (sys___msync13(p, &ua, retval));
5111 }
5112 
5113 int
5114 netbsd32___stat13(p, v, retval)
5115 	struct proc *p;
5116 	void *v;
5117 	register_t *retval;
5118 {
5119 	struct netbsd32___stat13_args /* {
5120 		syscallarg(const netbsd32_charp) path;
5121 		syscallarg(netbsd32_statp_t) ub;
5122 	} */ *uap = v;
5123 	struct netbsd32_stat sb32;
5124 	struct stat sb;
5125 	int error;
5126 	struct nameidata nd;
5127 
5128 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
5129 	    (caddr_t)(u_long)SCARG(uap, path), p);
5130 	if ((error = namei(&nd)) != 0)
5131 		return (error);
5132 	error = vn_stat(nd.ni_vp, &sb, p);
5133 	vput(nd.ni_vp);
5134 	if (error)
5135 		return (error);
5136 	netbsd32_from___stat13(&sb, &sb32);
5137 	error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof(sb32));
5138 	return (error);
5139 }
5140 
5141 int
5142 netbsd32___fstat13(p, v, retval)
5143 	struct proc *p;
5144 	void *v;
5145 	register_t *retval;
5146 {
5147 	struct netbsd32___fstat13_args /* {
5148 		syscallarg(int) fd;
5149 		syscallarg(netbsd32_statp_t) sb;
5150 	} */ *uap = v;
5151 	int fd = SCARG(uap, fd);
5152 	struct filedesc *fdp = p->p_fd;
5153 	struct file *fp;
5154 	struct netbsd32_stat sb32;
5155 	struct stat ub;
5156 	int error = 0;
5157 
5158 	if ((u_int)fd >= fdp->fd_nfiles ||
5159 	    (fp = fdp->fd_ofiles[fd]) == NULL)
5160 		return (EBADF);
5161 	switch (fp->f_type) {
5162 
5163 	case DTYPE_VNODE:
5164 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
5165 		break;
5166 
5167 	case DTYPE_SOCKET:
5168 		error = soo_stat((struct socket *)fp->f_data, &ub);
5169 		break;
5170 
5171 	default:
5172 		panic("fstat");
5173 		/*NOTREACHED*/
5174 	}
5175 	if (error == 0) {
5176 		netbsd32_from___stat13(&ub, &sb32);
5177 		error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, sb), sizeof(sb32));
5178 	}
5179 	return (error);
5180 }
5181 
5182 int
5183 netbsd32___lstat13(p, v, retval)
5184 	struct proc *p;
5185 	void *v;
5186 	register_t *retval;
5187 {
5188 	struct netbsd32___lstat13_args /* {
5189 		syscallarg(const netbsd32_charp) path;
5190 		syscallarg(netbsd32_statp_t) ub;
5191 	} */ *uap = v;
5192 	struct netbsd32_stat sb32;
5193 	struct stat sb;
5194 	int error;
5195 	struct nameidata nd;
5196 
5197 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
5198 	    (caddr_t)(u_long)SCARG(uap, path), p);
5199 	if ((error = namei(&nd)) != 0)
5200 		return (error);
5201 	error = vn_stat(nd.ni_vp, &sb, p);
5202 	vput(nd.ni_vp);
5203 	if (error)
5204 		return (error);
5205 	netbsd32_from___stat13(&sb, &sb32);
5206 	error = copyout(&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof(sb32));
5207 	return (error);
5208 }
5209 
5210 int
5211 netbsd32___sigaltstack14(p, v, retval)
5212 	struct proc *p;
5213 	void *v;
5214 	register_t *retval;
5215 {
5216 	struct netbsd32___sigaltstack14_args /* {
5217 		syscallarg(const netbsd32_sigaltstackp_t) nss;
5218 		syscallarg(netbsd32_sigaltstackp_t) oss;
5219 	} */ *uap = v;
5220 	struct netbsd32_sigaltstack s32;
5221 	struct sigaltstack nss, oss;
5222 	int error;
5223 
5224 	if (SCARG(uap, nss)) {
5225 		error = copyin((caddr_t)(u_long)SCARG(uap, nss), &s32, sizeof(s32));
5226 		if (error)
5227 			return (error);
5228 		nss.ss_sp = (void *)(u_long)s32.ss_sp;
5229 		nss.ss_size = (size_t)s32.ss_size;
5230 		nss.ss_flags = s32.ss_flags;
5231 	}
5232 	error = sigaltstack1(p,
5233 	    SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
5234 	if (error)
5235 		return (error);
5236 	if (SCARG(uap, oss)) {
5237 		s32.ss_sp = (netbsd32_voidp)(u_long)oss.ss_sp;
5238 		s32.ss_size = (netbsd32_size_t)oss.ss_size;
5239 		s32.ss_flags = oss.ss_flags;
5240 		error = copyout(&s32, (caddr_t)(u_long)SCARG(uap, oss), sizeof(s32));
5241 		if (error)
5242 			return (error);
5243 	}
5244 	return (0);
5245 }
5246 
5247 int
5248 netbsd32___posix_chown(p, v, retval)
5249 	struct proc *p;
5250 	void *v;
5251 	register_t *retval;
5252 {
5253 	struct netbsd32___posix_chown_args /* {
5254 		syscallarg(const netbsd32_charp) path;
5255 		syscallarg(uid_t) uid;
5256 		syscallarg(gid_t) gid;
5257 	} */ *uap = v;
5258 	struct sys___posix_chown_args ua;
5259 
5260 	NETBSD32TOP_UAP(path, const char);
5261 	NETBSD32TO64_UAP(uid);
5262 	NETBSD32TO64_UAP(gid);
5263 	return (sys___posix_chown(p, &ua, retval));
5264 }
5265 
5266 int
5267 netbsd32___posix_fchown(p, v, retval)
5268 	struct proc *p;
5269 	void *v;
5270 	register_t *retval;
5271 {
5272 	struct netbsd32___posix_fchown_args /* {
5273 		syscallarg(int) fd;
5274 		syscallarg(uid_t) uid;
5275 		syscallarg(gid_t) gid;
5276 	} */ *uap = v;
5277 	struct sys___posix_fchown_args ua;
5278 
5279 	NETBSD32TO64_UAP(fd);
5280 	NETBSD32TO64_UAP(uid);
5281 	NETBSD32TO64_UAP(gid);
5282 	return (sys___posix_fchown(p, &ua, retval));
5283 }
5284 
5285 int
5286 netbsd32___posix_lchown(p, v, retval)
5287 	struct proc *p;
5288 	void *v;
5289 	register_t *retval;
5290 {
5291 	struct netbsd32___posix_lchown_args /* {
5292 		syscallarg(const netbsd32_charp) path;
5293 		syscallarg(uid_t) uid;
5294 		syscallarg(gid_t) gid;
5295 	} */ *uap = v;
5296 	struct sys___posix_lchown_args ua;
5297 
5298 	NETBSD32TOP_UAP(path, const char);
5299 	NETBSD32TO64_UAP(uid);
5300 	NETBSD32TO64_UAP(gid);
5301 	return (sys___posix_lchown(p, &ua, retval));
5302 }
5303 
5304 int
5305 netbsd32_getsid(p, v, retval)
5306 	struct proc *p;
5307 	void *v;
5308 	register_t *retval;
5309 {
5310 	struct netbsd32_getsid_args /* {
5311 		syscallarg(pid_t) pid;
5312 	} */ *uap = v;
5313 	struct sys_getsid_args ua;
5314 
5315 	NETBSD32TO64_UAP(pid);
5316 	return (sys_getsid(p, &ua, retval));
5317 }
5318 
5319 int
5320 netbsd32_fktrace(p, v, retval)
5321 	struct proc *p;
5322 	void *v;
5323 	register_t *retval;
5324 {
5325 	struct netbsd32_fktrace_args /* {
5326 		syscallarg(const int) fd;
5327 		syscallarg(int) ops;
5328 		syscallarg(int) facs;
5329 		syscallarg(int) pid;
5330 	} */ *uap = v;
5331 	struct sys_fktrace_args ua;
5332 
5333 	NETBSD32TOX_UAP(fd, const int);
5334 	NETBSD32TO64_UAP(ops);
5335 	NETBSD32TO64_UAP(facs);
5336 	NETBSD32TO64_UAP(pid);
5337 	return (sys_fktrace(p, &ua, retval));
5338 }
5339 
5340 int
5341 netbsd32_preadv(p, v, retval)
5342 	struct proc *p;
5343 	void *v;
5344 	register_t *retval;
5345 {
5346 	struct netbsd32_preadv_args /* {
5347 		syscallarg(int) fd;
5348 		syscallarg(const netbsd32_iovecp_t) iovp;
5349 		syscallarg(int) iovcnt;
5350 		syscallarg(int) pad;
5351 		syscallarg(off_t) offset;
5352 	} */ *uap = v;
5353 	struct filedesc *fdp = p->p_fd;
5354 	struct file *fp;
5355 	struct vnode *vp;
5356 	off_t offset;
5357 	int error, fd = SCARG(uap, fd);
5358 
5359 	if ((u_int)fd >= fdp->fd_nfiles ||
5360 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
5361 	    (fp->f_flag & FREAD) == 0)
5362 		return (EBADF);
5363 
5364 	vp = (struct vnode *)fp->f_data;
5365 	if (fp->f_type != DTYPE_VNODE
5366 	    || vp->v_type == VFIFO)
5367 		return (ESPIPE);
5368 
5369 	offset = SCARG(uap, offset);
5370 
5371 	/*
5372 	 * XXX This works because no file systems actually
5373 	 * XXX take any action on the seek operation.
5374 	 */
5375 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
5376 		return (error);
5377 
5378 	return (dofilereadv32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp), SCARG(uap, iovcnt),
5379 	    &offset, 0, retval));
5380 }
5381 
5382 int
5383 netbsd32_pwritev(p, v, retval)
5384 	struct proc *p;
5385 	void *v;
5386 	register_t *retval;
5387 {
5388 	struct netbsd32_pwritev_args /* {
5389 		syscallarg(int) fd;
5390 		syscallarg(const netbsd32_iovecp_t) iovp;
5391 		syscallarg(int) iovcnt;
5392 		syscallarg(int) pad;
5393 		syscallarg(off_t) offset;
5394 	} */ *uap = v;
5395 	struct filedesc *fdp = p->p_fd;
5396 	struct file *fp;
5397 	struct vnode *vp;
5398 	off_t offset;
5399 	int error, fd = SCARG(uap, fd);
5400 
5401 	if ((u_int)fd >= fdp->fd_nfiles ||
5402 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
5403 	    (fp->f_flag & FWRITE) == 0)
5404 		return (EBADF);
5405 
5406 	vp = (struct vnode *)fp->f_data;
5407 	if (fp->f_type != DTYPE_VNODE
5408 	    || vp->v_type == VFIFO)
5409 		return (ESPIPE);
5410 
5411 	offset = SCARG(uap, offset);
5412 
5413 	/*
5414 	 * XXX This works because no file systems actually
5415 	 * XXX take any action on the seek operation.
5416 	 */
5417 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
5418 		return (error);
5419 
5420 	return (dofilewritev32(p, fd, fp, (struct netbsd32_iovec *)(u_long)SCARG(uap, iovp), SCARG(uap, iovcnt),
5421 	    &offset, 0, retval));
5422 }
5423 
5424 /* ARGSUSED */
5425 int
5426 netbsd32___sigaction14(p, v, retval)
5427 	struct proc *p;
5428 	void *v;
5429 	register_t *retval;
5430 {
5431 	struct netbsd32___sigaction14_args /* {
5432 		syscallarg(int) signum;
5433 		syscallarg(const struct sigaction *) nsa;
5434 		syscallarg(struct sigaction *) osa;
5435 	} */ *uap = v;
5436 	struct netbsd32_sigaction sa32;
5437 	struct sigaction nsa, osa;
5438 	int error;
5439 
5440 	if (SCARG(uap, nsa)) {
5441 		error = copyin((caddr_t)(u_long)SCARG(uap, nsa),
5442 			       &sa32, sizeof(sa32));
5443 		if (error)
5444 			return (error);
5445 		nsa.sa_handler = (void *)(u_long)sa32.sa_handler;
5446 		nsa.sa_mask = sa32.sa_mask;
5447 		nsa.sa_flags = sa32.sa_flags;
5448 	}
5449 	error = sigaction1(p, SCARG(uap, signum),
5450 	    SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0);
5451 	if (error)
5452 		return (error);
5453 	if (SCARG(uap, osa)) {
5454 		sa32.sa_handler = (netbsd32_voidp)(u_long)osa.sa_handler;
5455 		sa32.sa_mask = osa.sa_mask;
5456 		sa32.sa_flags = osa.sa_flags;
5457 		error = copyout(&sa32, (caddr_t)(u_long)SCARG(uap, osa), sizeof(sa32));
5458 		if (error)
5459 			return (error);
5460 	}
5461 	return (0);
5462 }
5463 
5464 int netbsd32___sigpending14(p, v, retval)
5465 	struct proc *p;
5466 	void   *v;
5467 	register_t *retval;
5468 {
5469 	struct netbsd32___sigpending14_args /* {
5470 		syscallarg(sigset_t *) set;
5471 	} */ *uap = v;
5472 	struct sys___sigpending14_args ua;
5473 
5474 	NETBSD32TOP_UAP(set, sigset_t);
5475 	return (sys___sigpending14(p, &ua, retval));
5476 }
5477 
5478 int netbsd32___sigprocmask14(p, v, retval)
5479 	struct proc *p;
5480 	void   *v;
5481 	register_t *retval;
5482 {
5483 	struct netbsd32___sigprocmask14_args /* {
5484 		syscallarg(int) how;
5485 		syscallarg(const sigset_t *) set;
5486 		syscallarg(sigset_t *) oset;
5487 	} */ *uap = v;
5488 	struct sys___sigprocmask14_args ua;
5489 
5490 	NETBSD32TO64_UAP(how);
5491 	NETBSD32TOP_UAP(set, sigset_t);
5492 	NETBSD32TOP_UAP(oset, sigset_t);
5493 	return (sys___sigprocmask14(p, &ua, retval));
5494 }
5495 
5496 int netbsd32___sigsuspend14(p, v, retval)
5497 	struct proc *p;
5498 	void   *v;
5499 	register_t *retval;
5500 {
5501 	struct netbsd32___sigsuspend14_args /* {
5502 		syscallarg(const sigset_t *) set;
5503 	} */ *uap = v;
5504 	struct sys___sigsuspend14_args ua;
5505 
5506 	NETBSD32TOP_UAP(set, sigset_t);
5507 	return (sys___sigsuspend14(p, &ua, retval));
5508 };
5509 
5510 
5511 /*
5512  * Find pathname of process's current directory.
5513  *
5514  * Use vfs vnode-to-name reverse cache; if that fails, fall back
5515  * to reading directory contents.
5516  */
5517 int
5518 getcwd_common __P((struct vnode *, struct vnode *,
5519 		   char **, char *, int, int, struct proc *));
5520 
5521 int netbsd32___getcwd(p, v, retval)
5522 	struct proc *p;
5523 	void   *v;
5524 	register_t *retval;
5525 {
5526 	struct netbsd32___getcwd_args /* {
5527 		syscallarg(char *) bufp;
5528 		syscallarg(size_t) length;
5529 	} */ *uap = v;
5530 
5531 	int     error;
5532 	char   *path;
5533 	char   *bp, *bend;
5534 	int     len = (int)SCARG(uap, length);
5535 	int	lenused;
5536 
5537 	if (len > MAXPATHLEN*4)
5538 		len = MAXPATHLEN*4;
5539 	else if (len < 2)
5540 		return ERANGE;
5541 
5542 	path = (char *)malloc(len, M_TEMP, M_WAITOK);
5543 	if (!path)
5544 		return ENOMEM;
5545 
5546 	bp = &path[len];
5547 	bend = bp;
5548 	*(--bp) = '\0';
5549 
5550 	/*
5551 	 * 5th argument here is "max number of vnodes to traverse".
5552 	 * Since each entry takes up at least 2 bytes in the output buffer,
5553 	 * limit it to N/2 vnodes for an N byte buffer.
5554 	 */
5555 #define GETCWD_CHECK_ACCESS 0x0001
5556 	error = getcwd_common (p->p_cwdi->cwdi_cdir, NULL, &bp, path, len/2,
5557 			       GETCWD_CHECK_ACCESS, p);
5558 
5559 	if (error)
5560 		goto out;
5561 	lenused = bend - bp;
5562 	*retval = lenused;
5563 	/* put the result into user buffer */
5564 	error = copyout(bp, (caddr_t)(u_long)SCARG(uap, bufp), lenused);
5565 
5566 out:
5567 	free(path, M_TEMP);
5568 	return error;
5569 }
5570 
5571 int netbsd32_fchroot(p, v, retval)
5572 	struct proc *p;
5573 	void *v;
5574 	register_t *retval;
5575 {
5576 	struct netbsd32_fchroot_args /* {
5577 		syscallarg(int) fd;
5578 	} */ *uap = v;
5579 	struct sys_fchroot_args ua;
5580 
5581 	NETBSD32TO64_UAP(fd);
5582 	return (sys_fchroot(p, &ua, retval));
5583 }
5584 
5585 /*
5586  * Open a file given a file handle.
5587  *
5588  * Check permissions, allocate an open file structure,
5589  * and call the device open routine if any.
5590  */
5591 int
5592 netbsd32_fhopen(p, v, retval)
5593 	struct proc *p;
5594 	void *v;
5595 	register_t *retval;
5596 {
5597 	struct netbsd32_fhopen_args /* {
5598 		syscallarg(const fhandle_t *) fhp;
5599 		syscallarg(int) flags;
5600 	} */ *uap = v;
5601 	struct sys_fhopen_args ua;
5602 
5603 	NETBSD32TOP_UAP(fhp, fhandle_t);
5604 	NETBSD32TO64_UAP(flags);
5605 	return (sys_fhopen(p, &ua, retval));
5606 }
5607 
5608 int netbsd32_fhstat(p, v, retval)
5609 	struct proc *p;
5610 	void *v;
5611 	register_t *retval;
5612 {
5613 	struct netbsd32_fhstat_args /* {
5614 		syscallarg(const netbsd32_fhandlep_t) fhp;
5615 		syscallarg(struct stat *) sb;
5616 	} */ *uap = v;
5617 	struct sys_fhstat_args ua;
5618 
5619 	NETBSD32TOP_UAP(fhp, const fhandle_t);
5620 	NETBSD32TOP_UAP(sb, struct stat);
5621 	return (sys_fhstat(p, &ua, retval));
5622 }
5623 
5624 int netbsd32_fhstatfs(p, v, retval)
5625 	struct proc *p;
5626 	void *v;
5627 	register_t *retval;
5628 {
5629 	struct netbsd32_fhstatfs_args /* {
5630 		syscallarg(const netbsd32_fhandlep_t) fhp;
5631 		syscallarg(struct statfs *) buf;
5632 	} */ *uap = v;
5633 	struct sys_fhstatfs_args ua;
5634 
5635 	NETBSD32TOP_UAP(fhp, const fhandle_t);
5636 	NETBSD32TOP_UAP(buf, struct statfs);
5637 	return (sys_fhstatfs(p, &ua, retval));
5638 }
5639