freebsd32_misc.c revision 220238
167217Sn_hibma/*-
261560Sn_hibma * Copyright (c) 2002 Doug Rabson
361560Sn_hibma * All rights reserved.
461560Sn_hibma *
561560Sn_hibma * Redistribution and use in source and binary forms, with or without
661560Sn_hibma * modification, are permitted provided that the following conditions
761560Sn_hibma * are met:
861560Sn_hibma * 1. Redistributions of source code must retain the above copyright
961560Sn_hibma *    notice, this list of conditions and the following disclaimer.
1061560Sn_hibma * 2. Redistributions in binary form must reproduce the above copyright
1161560Sn_hibma *    notice, this list of conditions and the following disclaimer in the
1261560Sn_hibma *    documentation and/or other materials provided with the distribution.
1361560Sn_hibma *
1461560Sn_hibma * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1561560Sn_hibma * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1661560Sn_hibma * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1761560Sn_hibma * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1861560Sn_hibma * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1961560Sn_hibma * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2061560Sn_hibma * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2161560Sn_hibma * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2261560Sn_hibma * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2361560Sn_hibma * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2461560Sn_hibma * SUCH DAMAGE.
2561560Sn_hibma */
2661560Sn_hibma
2761560Sn_hibma#include <sys/cdefs.h>
2861560Sn_hibma__FBSDID("$FreeBSD: head/sys/compat/freebsd32/freebsd32_misc.c 220238 2011-04-01 11:16:29Z kib $");
2984224Sdillon
3084224Sdillon#include "opt_compat.h"
3184224Sdillon#include "opt_inet.h"
3261560Sn_hibma#include "opt_inet6.h"
3361560Sn_hibma
3461560Sn_hibma#define __ELF_WORD_SIZE 32
3561560Sn_hibma
3661560Sn_hibma#include <sys/param.h>
3761560Sn_hibma#include <sys/bus.h>
3861560Sn_hibma#include <sys/clock.h>
3961560Sn_hibma#include <sys/exec.h>
4061560Sn_hibma#include <sys/fcntl.h>
41188945Sthompsa#include <sys/filedesc.h>
4261560Sn_hibma#include <sys/imgact.h>
43113273Smdodd#include <sys/jail.h>
4461560Sn_hibma#include <sys/kernel.h>
4561560Sn_hibma#include <sys/limits.h>
46187994Salfred#include <sys/linker.h>
47187994Salfred#include <sys/lock.h>
48187994Salfred#include <sys/malloc.h>
49187994Salfred#include <sys/file.h>		/* Must come after sys/malloc.h */
50187994Salfred#include <sys/imgact.h>
51188945Sthompsa#include <sys/mbuf.h>
52187994Salfred#include <sys/mman.h>
53187994Salfred#include <sys/module.h>
54188945Sthompsa#include <sys/mount.h>
55187994Salfred#include <sys/mutex.h>
56187994Salfred#include <sys/namei.h>
57187994Salfred#include <sys/proc.h>
58187994Salfred#include <sys/reboot.h>
59187994Salfred#include <sys/resource.h>
60187994Salfred#include <sys/resourcevar.h>
61205728Skaiw#include <sys/selinfo.h>
62205728Skaiw#include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
63205728Skaiw#include <sys/pipe.h>		/* Must come after sys/selinfo.h */
64205728Skaiw#include <sys/signal.h>
65187994Salfred#include <sys/signalvar.h>
66187994Salfred#include <sys/socket.h>
67187994Salfred#include <sys/socketvar.h>
68205728Skaiw#include <sys/stat.h>
69205728Skaiw#include <sys/syscall.h>
70205728Skaiw#include <sys/syscallsubr.h>
71240762Smav#include <sys/sysctl.h>
72205728Skaiw#include <sys/sysent.h>
73205728Skaiw#include <sys/sysproto.h>
74205728Skaiw#include <sys/systm.h>
75205728Skaiw#include <sys/thr.h>
76205728Skaiw#include <sys/unistd.h>
77205728Skaiw#include <sys/ucontext.h>
78205728Skaiw#include <sys/vnode.h>
79205728Skaiw#include <sys/wait.h>
80205728Skaiw#include <sys/ipc.h>
81205728Skaiw#include <sys/msg.h>
82205728Skaiw#include <sys/sem.h>
83205728Skaiw#include <sys/shm.h>
84205728Skaiw
85187994Salfred#ifdef INET
86188945Sthompsa#include <netinet/in.h>
87187994Salfred#endif
88187994Salfred
89187994Salfred#include <vm/vm.h>
90188945Sthompsa#include <vm/vm_param.h>
91187994Salfred#include <vm/pmap.h>
92187994Salfred#include <vm/vm_map.h>
93187994Salfred#include <vm/vm_object.h>
94187994Salfred#include <vm/vm_extern.h>
95187994Salfred
9661560Sn_hibma#include <machine/cpu.h>
9767217Sn_hibma#include <machine/elf.h>
9861560Sn_hibma
99192992Sthompsa#include <security/audit/audit.h>
100187994Salfred
101187994Salfred#include <compat/freebsd32/freebsd32_util.h>
10261560Sn_hibma#include <compat/freebsd32/freebsd32.h>
103187994Salfred#include <compat/freebsd32/freebsd32_ipc.h>
104187994Salfred#include <compat/freebsd32/freebsd32_signal.h>
105187994Salfred#include <compat/freebsd32/freebsd32_proto.h>
106213920Shselasky
107187994SalfredCTASSERT(sizeof(struct timeval32) == 8);
108189108SthompsaCTASSERT(sizeof(struct timespec32) == 8);
109188945SthompsaCTASSERT(sizeof(struct itimerval32) == 16);
110187994SalfredCTASSERT(sizeof(struct statfs32) == 256);
111187994SalfredCTASSERT(sizeof(struct rusage32) == 72);
112187994SalfredCTASSERT(sizeof(struct sigaltstack32) == 12);
113189108SthompsaCTASSERT(sizeof(struct kevent32) == 20);
114189108SthompsaCTASSERT(sizeof(struct iovec32) == 8);
115189108SthompsaCTASSERT(sizeof(struct msghdr32) == 28);
116187994SalfredCTASSERT(sizeof(struct stat32) == 96);
117187994SalfredCTASSERT(sizeof(struct sigaction32) == 24);
118187994Salfred
119187994Salfredstatic int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
120187994Salfredstatic int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
121187994Salfred
122187994Salfred#if BYTE_ORDER == BIG_ENDIAN
123187994Salfred#define PAIR32TO64(type, name) ((name ## 2) | ((type)(name ## 1) << 32))
12467217Sn_hibma#define RETVAL_HI 0
12567217Sn_hibma#define RETVAL_LO 1
126187994Salfred#else
127213920Shselasky#define PAIR32TO64(type, name) ((name ## 1) | ((type)(name ## 2) << 32))
128187994Salfred#define RETVAL_HI 1
129187994Salfred#define RETVAL_LO 0
130187994Salfred#endif
131187994Salfred
132187994Salfredvoid
133187994Salfredfreebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
134187994Salfred{
135213920Shselasky
136213920Shselasky	TV_CP(*s, *s32, ru_utime);
137213920Shselasky	TV_CP(*s, *s32, ru_stime);
138213920Shselasky	CP(*s, *s32, ru_maxrss);
139213920Shselasky	CP(*s, *s32, ru_ixrss);
140213920Shselasky	CP(*s, *s32, ru_idrss);
141213920Shselasky	CP(*s, *s32, ru_isrss);
142187994Salfred	CP(*s, *s32, ru_minflt);
143213920Shselasky	CP(*s, *s32, ru_majflt);
144187994Salfred	CP(*s, *s32, ru_nswap);
145187994Salfred	CP(*s, *s32, ru_inblock);
146187994Salfred	CP(*s, *s32, ru_oublock);
147187994Salfred	CP(*s, *s32, ru_msgsnd);
148187994Salfred	CP(*s, *s32, ru_msgrcv);
149187994Salfred	CP(*s, *s32, ru_nsignals);
150187994Salfred	CP(*s, *s32, ru_nvcsw);
151187994Salfred	CP(*s, *s32, ru_nivcsw);
152187994Salfred}
153187994Salfred
15467217Sn_hibmaint
15567217Sn_hibmafreebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
15667217Sn_hibma{
15767217Sn_hibma	int error, status;
15867217Sn_hibma	struct rusage32 ru32;
15967217Sn_hibma	struct rusage ru, *rup;
16067217Sn_hibma
16167217Sn_hibma	if (uap->rusage != NULL)
16261560Sn_hibma		rup = &ru;
16361560Sn_hibma	else
16467217Sn_hibma		rup = NULL;
16561560Sn_hibma	error = kern_wait(td, uap->pid, &status, uap->options, rup);
16667217Sn_hibma	if (error)
16767217Sn_hibma		return (error);
16861560Sn_hibma	if (uap->status != NULL)
16961560Sn_hibma		error = copyout(&status, uap->status, sizeof(status));
17061560Sn_hibma	if (uap->rusage != NULL && error == 0) {
17161560Sn_hibma		freebsd32_rusage_out(&ru, &ru32);
17267217Sn_hibma		error = copyout(&ru32, uap->rusage, sizeof(ru32));
17361560Sn_hibma	}
17461560Sn_hibma	return (error);
17561560Sn_hibma}
17661560Sn_hibma
177#ifdef COMPAT_FREEBSD4
178static void
179copy_statfs(struct statfs *in, struct statfs32 *out)
180{
181
182	statfs_scale_blocks(in, INT32_MAX);
183	bzero(out, sizeof(*out));
184	CP(*in, *out, f_bsize);
185	out->f_iosize = MIN(in->f_iosize, INT32_MAX);
186	CP(*in, *out, f_blocks);
187	CP(*in, *out, f_bfree);
188	CP(*in, *out, f_bavail);
189	out->f_files = MIN(in->f_files, INT32_MAX);
190	out->f_ffree = MIN(in->f_ffree, INT32_MAX);
191	CP(*in, *out, f_fsid);
192	CP(*in, *out, f_owner);
193	CP(*in, *out, f_type);
194	CP(*in, *out, f_flags);
195	out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
196	out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
197	strlcpy(out->f_fstypename,
198	      in->f_fstypename, MFSNAMELEN);
199	strlcpy(out->f_mntonname,
200	      in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
201	out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
202	out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
203	strlcpy(out->f_mntfromname,
204	      in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
205}
206#endif
207
208#ifdef COMPAT_FREEBSD4
209int
210freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
211{
212	struct statfs *buf, *sp;
213	struct statfs32 stat32;
214	size_t count, size;
215	int error;
216
217	count = uap->bufsize / sizeof(struct statfs32);
218	size = count * sizeof(struct statfs);
219	error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
220	if (size > 0) {
221		count = td->td_retval[0];
222		sp = buf;
223		while (count > 0 && error == 0) {
224			copy_statfs(sp, &stat32);
225			error = copyout(&stat32, uap->buf, sizeof(stat32));
226			sp++;
227			uap->buf++;
228			count--;
229		}
230		free(buf, M_TEMP);
231	}
232	return (error);
233}
234#endif
235
236int
237freebsd32_sigaltstack(struct thread *td,
238		      struct freebsd32_sigaltstack_args *uap)
239{
240	struct sigaltstack32 s32;
241	struct sigaltstack ss, oss, *ssp;
242	int error;
243
244	if (uap->ss != NULL) {
245		error = copyin(uap->ss, &s32, sizeof(s32));
246		if (error)
247			return (error);
248		PTRIN_CP(s32, ss, ss_sp);
249		CP(s32, ss, ss_size);
250		CP(s32, ss, ss_flags);
251		ssp = &ss;
252	} else
253		ssp = NULL;
254	error = kern_sigaltstack(td, ssp, &oss);
255	if (error == 0 && uap->oss != NULL) {
256		PTROUT_CP(oss, s32, ss_sp);
257		CP(oss, s32, ss_size);
258		CP(oss, s32, ss_flags);
259		error = copyout(&s32, uap->oss, sizeof(s32));
260	}
261	return (error);
262}
263
264/*
265 * Custom version of exec_copyin_args() so that we can translate
266 * the pointers.
267 */
268int
269freebsd32_exec_copyin_args(struct image_args *args, char *fname,
270    enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
271{
272	char *argp, *envp;
273	u_int32_t *p32, arg;
274	size_t length;
275	int error;
276
277	bzero(args, sizeof(*args));
278	if (argv == NULL)
279		return (EFAULT);
280
281	/*
282	 * Allocate demand-paged memory for the file name, argument, and
283	 * environment strings.
284	 */
285	error = exec_alloc_args(args);
286	if (error != 0)
287		return (error);
288
289	/*
290	 * Copy the file name.
291	 */
292	if (fname != NULL) {
293		args->fname = args->buf;
294		error = (segflg == UIO_SYSSPACE) ?
295		    copystr(fname, args->fname, PATH_MAX, &length) :
296		    copyinstr(fname, args->fname, PATH_MAX, &length);
297		if (error != 0)
298			goto err_exit;
299	} else
300		length = 0;
301
302	args->begin_argv = args->buf + length;
303	args->endp = args->begin_argv;
304	args->stringspace = ARG_MAX;
305
306	/*
307	 * extract arguments first
308	 */
309	p32 = argv;
310	for (;;) {
311		error = copyin(p32++, &arg, sizeof(arg));
312		if (error)
313			goto err_exit;
314		if (arg == 0)
315			break;
316		argp = PTRIN(arg);
317		error = copyinstr(argp, args->endp, args->stringspace, &length);
318		if (error) {
319			if (error == ENAMETOOLONG)
320				error = E2BIG;
321			goto err_exit;
322		}
323		args->stringspace -= length;
324		args->endp += length;
325		args->argc++;
326	}
327
328	args->begin_envv = args->endp;
329
330	/*
331	 * extract environment strings
332	 */
333	if (envv) {
334		p32 = envv;
335		for (;;) {
336			error = copyin(p32++, &arg, sizeof(arg));
337			if (error)
338				goto err_exit;
339			if (arg == 0)
340				break;
341			envp = PTRIN(arg);
342			error = copyinstr(envp, args->endp, args->stringspace,
343			    &length);
344			if (error) {
345				if (error == ENAMETOOLONG)
346					error = E2BIG;
347				goto err_exit;
348			}
349			args->stringspace -= length;
350			args->endp += length;
351			args->envc++;
352		}
353	}
354
355	return (0);
356
357err_exit:
358	exec_free_args(args);
359	return (error);
360}
361
362int
363freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
364{
365	struct image_args eargs;
366	int error;
367
368	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
369	    uap->argv, uap->envv);
370	if (error == 0)
371		error = kern_execve(td, &eargs, NULL);
372	return (error);
373}
374
375int
376freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
377{
378	struct image_args eargs;
379	int error;
380
381	error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
382	    uap->argv, uap->envv);
383	if (error == 0) {
384		eargs.fd = uap->fd;
385		error = kern_execve(td, &eargs, NULL);
386	}
387	return (error);
388}
389
390#ifdef __ia64__
391static int
392freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
393		       int prot, int fd, off_t pos)
394{
395	vm_map_t map;
396	vm_map_entry_t entry;
397	int rv;
398
399	map = &td->td_proc->p_vmspace->vm_map;
400	if (fd != -1)
401		prot |= VM_PROT_WRITE;
402
403	if (vm_map_lookup_entry(map, start, &entry)) {
404		if ((entry->protection & prot) != prot) {
405			rv = vm_map_protect(map,
406					    trunc_page(start),
407					    round_page(end),
408					    entry->protection | prot,
409					    FALSE);
410			if (rv != KERN_SUCCESS)
411				return (EINVAL);
412		}
413	} else {
414		vm_offset_t addr = trunc_page(start);
415		rv = vm_map_find(map, 0, 0,
416				 &addr, PAGE_SIZE, FALSE, prot,
417				 VM_PROT_ALL, 0);
418		if (rv != KERN_SUCCESS)
419			return (EINVAL);
420	}
421
422	if (fd != -1) {
423		struct pread_args r;
424		r.fd = fd;
425		r.buf = (void *) start;
426		r.nbyte = end - start;
427		r.offset = pos;
428		return (pread(td, &r));
429	} else {
430		while (start < end) {
431			subyte((void *) start, 0);
432			start++;
433		}
434		return (0);
435	}
436}
437#endif
438
439int
440freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
441{
442	struct mmap_args ap;
443	vm_offset_t addr = (vm_offset_t) uap->addr;
444	vm_size_t len	 = uap->len;
445	int prot	 = uap->prot;
446	int flags	 = uap->flags;
447	int fd		 = uap->fd;
448	off_t pos	 = PAIR32TO64(off_t,uap->pos);
449#ifdef __ia64__
450	vm_size_t pageoff;
451	int error;
452
453	/*
454	 * Attempt to handle page size hassles.
455	 */
456	pageoff = (pos & PAGE_MASK);
457	if (flags & MAP_FIXED) {
458		vm_offset_t start, end;
459		start = addr;
460		end = addr + len;
461
462		if (start != trunc_page(start)) {
463			error = freebsd32_mmap_partial(td, start,
464						       round_page(start), prot,
465						       fd, pos);
466			if (fd != -1)
467				pos += round_page(start) - start;
468			start = round_page(start);
469		}
470		if (end != round_page(end)) {
471			vm_offset_t t = trunc_page(end);
472			error = freebsd32_mmap_partial(td, t, end,
473						  prot, fd,
474						  pos + t - start);
475			end = trunc_page(end);
476		}
477		if (end > start && fd != -1 && (pos & PAGE_MASK)) {
478			/*
479			 * We can't map this region at all. The specified
480			 * address doesn't have the same alignment as the file
481			 * position. Fake the mapping by simply reading the
482			 * entire region into memory. First we need to make
483			 * sure the region exists.
484			 */
485			vm_map_t map;
486			struct pread_args r;
487			int rv;
488
489			prot |= VM_PROT_WRITE;
490			map = &td->td_proc->p_vmspace->vm_map;
491			rv = vm_map_remove(map, start, end);
492			if (rv != KERN_SUCCESS)
493				return (EINVAL);
494			rv = vm_map_find(map, 0, 0,
495					 &start, end - start, FALSE,
496					 prot, VM_PROT_ALL, 0);
497			if (rv != KERN_SUCCESS)
498				return (EINVAL);
499			r.fd = fd;
500			r.buf = (void *) start;
501			r.nbyte = end - start;
502			r.offset = pos;
503			error = pread(td, &r);
504			if (error)
505				return (error);
506
507			td->td_retval[0] = addr;
508			return (0);
509		}
510		if (end == start) {
511			/*
512			 * After dealing with the ragged ends, there
513			 * might be none left.
514			 */
515			td->td_retval[0] = addr;
516			return (0);
517		}
518		addr = start;
519		len = end - start;
520	}
521#endif
522
523	ap.addr = (void *) addr;
524	ap.len = len;
525	ap.prot = prot;
526	ap.flags = flags;
527	ap.fd = fd;
528	ap.pos = pos;
529
530	return (mmap(td, &ap));
531}
532
533#ifdef COMPAT_FREEBSD6
534int
535freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
536{
537	struct freebsd32_mmap_args ap;
538
539	ap.addr = uap->addr;
540	ap.len = uap->len;
541	ap.prot = uap->prot;
542	ap.flags = uap->flags;
543	ap.fd = uap->fd;
544	ap.pos1 = uap->pos1;
545	ap.pos2 = uap->pos2;
546
547	return (freebsd32_mmap(td, &ap));
548}
549#endif
550
551int
552freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
553{
554	struct itimerval itv, oitv, *itvp;
555	struct itimerval32 i32;
556	int error;
557
558	if (uap->itv != NULL) {
559		error = copyin(uap->itv, &i32, sizeof(i32));
560		if (error)
561			return (error);
562		TV_CP(i32, itv, it_interval);
563		TV_CP(i32, itv, it_value);
564		itvp = &itv;
565	} else
566		itvp = NULL;
567	error = kern_setitimer(td, uap->which, itvp, &oitv);
568	if (error || uap->oitv == NULL)
569		return (error);
570	TV_CP(oitv, i32, it_interval);
571	TV_CP(oitv, i32, it_value);
572	return (copyout(&i32, uap->oitv, sizeof(i32)));
573}
574
575int
576freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
577{
578	struct itimerval itv;
579	struct itimerval32 i32;
580	int error;
581
582	error = kern_getitimer(td, uap->which, &itv);
583	if (error || uap->itv == NULL)
584		return (error);
585	TV_CP(itv, i32, it_interval);
586	TV_CP(itv, i32, it_value);
587	return (copyout(&i32, uap->itv, sizeof(i32)));
588}
589
590int
591freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
592{
593	struct timeval32 tv32;
594	struct timeval tv, *tvp;
595	int error;
596
597	if (uap->tv != NULL) {
598		error = copyin(uap->tv, &tv32, sizeof(tv32));
599		if (error)
600			return (error);
601		CP(tv32, tv, tv_sec);
602		CP(tv32, tv, tv_usec);
603		tvp = &tv;
604	} else
605		tvp = NULL;
606	/*
607	 * XXX Do pointers need PTRIN()?
608	 */
609	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
610	    sizeof(int32_t) * 8));
611}
612
613int
614freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
615{
616	struct timespec32 ts32;
617	struct timespec ts;
618	struct timeval tv, *tvp;
619	sigset_t set, *uset;
620	int error;
621
622	if (uap->ts != NULL) {
623		error = copyin(uap->ts, &ts32, sizeof(ts32));
624		if (error != 0)
625			return (error);
626		CP(ts32, ts, tv_sec);
627		CP(ts32, ts, tv_nsec);
628		TIMESPEC_TO_TIMEVAL(&tv, &ts);
629		tvp = &tv;
630	} else
631		tvp = NULL;
632	if (uap->sm != NULL) {
633		error = copyin(uap->sm, &set, sizeof(set));
634		if (error != 0)
635			return (error);
636		uset = &set;
637	} else
638		uset = NULL;
639	/*
640	 * XXX Do pointers need PTRIN()?
641	 */
642	error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
643	    uset, sizeof(int32_t) * 8);
644	return (error);
645}
646
647/*
648 * Copy 'count' items into the destination list pointed to by uap->eventlist.
649 */
650static int
651freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
652{
653	struct freebsd32_kevent_args *uap;
654	struct kevent32	ks32[KQ_NEVENTS];
655	int i, error = 0;
656
657	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
658	uap = (struct freebsd32_kevent_args *)arg;
659
660	for (i = 0; i < count; i++) {
661		CP(kevp[i], ks32[i], ident);
662		CP(kevp[i], ks32[i], filter);
663		CP(kevp[i], ks32[i], flags);
664		CP(kevp[i], ks32[i], fflags);
665		CP(kevp[i], ks32[i], data);
666		PTROUT_CP(kevp[i], ks32[i], udata);
667	}
668	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
669	if (error == 0)
670		uap->eventlist += count;
671	return (error);
672}
673
674/*
675 * Copy 'count' items from the list pointed to by uap->changelist.
676 */
677static int
678freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
679{
680	struct freebsd32_kevent_args *uap;
681	struct kevent32	ks32[KQ_NEVENTS];
682	int i, error = 0;
683
684	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
685	uap = (struct freebsd32_kevent_args *)arg;
686
687	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
688	if (error)
689		goto done;
690	uap->changelist += count;
691
692	for (i = 0; i < count; i++) {
693		CP(ks32[i], kevp[i], ident);
694		CP(ks32[i], kevp[i], filter);
695		CP(ks32[i], kevp[i], flags);
696		CP(ks32[i], kevp[i], fflags);
697		CP(ks32[i], kevp[i], data);
698		PTRIN_CP(ks32[i], kevp[i], udata);
699	}
700done:
701	return (error);
702}
703
704int
705freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
706{
707	struct timespec32 ts32;
708	struct timespec ts, *tsp;
709	struct kevent_copyops k_ops = { uap,
710					freebsd32_kevent_copyout,
711					freebsd32_kevent_copyin};
712	int error;
713
714
715	if (uap->timeout) {
716		error = copyin(uap->timeout, &ts32, sizeof(ts32));
717		if (error)
718			return (error);
719		CP(ts32, ts, tv_sec);
720		CP(ts32, ts, tv_nsec);
721		tsp = &ts;
722	} else
723		tsp = NULL;
724	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
725	    &k_ops, tsp);
726	return (error);
727}
728
729int
730freebsd32_gettimeofday(struct thread *td,
731		       struct freebsd32_gettimeofday_args *uap)
732{
733	struct timeval atv;
734	struct timeval32 atv32;
735	struct timezone rtz;
736	int error = 0;
737
738	if (uap->tp) {
739		microtime(&atv);
740		CP(atv, atv32, tv_sec);
741		CP(atv, atv32, tv_usec);
742		error = copyout(&atv32, uap->tp, sizeof (atv32));
743	}
744	if (error == 0 && uap->tzp != NULL) {
745		rtz.tz_minuteswest = tz_minuteswest;
746		rtz.tz_dsttime = tz_dsttime;
747		error = copyout(&rtz, uap->tzp, sizeof (rtz));
748	}
749	return (error);
750}
751
752int
753freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
754{
755	struct rusage32 s32;
756	struct rusage s;
757	int error;
758
759	error = kern_getrusage(td, uap->who, &s);
760	if (error)
761		return (error);
762	if (uap->rusage != NULL) {
763		freebsd32_rusage_out(&s, &s32);
764		error = copyout(&s32, uap->rusage, sizeof(s32));
765	}
766	return (error);
767}
768
769static int
770freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
771{
772	struct iovec32 iov32;
773	struct iovec *iov;
774	struct uio *uio;
775	u_int iovlen;
776	int error, i;
777
778	*uiop = NULL;
779	if (iovcnt > UIO_MAXIOV)
780		return (EINVAL);
781	iovlen = iovcnt * sizeof(struct iovec);
782	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
783	iov = (struct iovec *)(uio + 1);
784	for (i = 0; i < iovcnt; i++) {
785		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
786		if (error) {
787			free(uio, M_IOV);
788			return (error);
789		}
790		iov[i].iov_base = PTRIN(iov32.iov_base);
791		iov[i].iov_len = iov32.iov_len;
792	}
793	uio->uio_iov = iov;
794	uio->uio_iovcnt = iovcnt;
795	uio->uio_segflg = UIO_USERSPACE;
796	uio->uio_offset = -1;
797	uio->uio_resid = 0;
798	for (i = 0; i < iovcnt; i++) {
799		if (iov->iov_len > INT_MAX - uio->uio_resid) {
800			free(uio, M_IOV);
801			return (EINVAL);
802		}
803		uio->uio_resid += iov->iov_len;
804		iov++;
805	}
806	*uiop = uio;
807	return (0);
808}
809
810int
811freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
812{
813	struct uio *auio;
814	int error;
815
816	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
817	if (error)
818		return (error);
819	error = kern_readv(td, uap->fd, auio);
820	free(auio, M_IOV);
821	return (error);
822}
823
824int
825freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
826{
827	struct uio *auio;
828	int error;
829
830	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
831	if (error)
832		return (error);
833	error = kern_writev(td, uap->fd, auio);
834	free(auio, M_IOV);
835	return (error);
836}
837
838int
839freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
840{
841	struct uio *auio;
842	int error;
843
844	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
845	if (error)
846		return (error);
847	error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
848	free(auio, M_IOV);
849	return (error);
850}
851
852int
853freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
854{
855	struct uio *auio;
856	int error;
857
858	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
859	if (error)
860		return (error);
861	error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
862	free(auio, M_IOV);
863	return (error);
864}
865
866int
867freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
868    int error)
869{
870	struct iovec32 iov32;
871	struct iovec *iov;
872	u_int iovlen;
873	int i;
874
875	*iovp = NULL;
876	if (iovcnt > UIO_MAXIOV)
877		return (error);
878	iovlen = iovcnt * sizeof(struct iovec);
879	iov = malloc(iovlen, M_IOV, M_WAITOK);
880	for (i = 0; i < iovcnt; i++) {
881		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
882		if (error) {
883			free(iov, M_IOV);
884			return (error);
885		}
886		iov[i].iov_base = PTRIN(iov32.iov_base);
887		iov[i].iov_len = iov32.iov_len;
888	}
889	*iovp = iov;
890	return (0);
891}
892
893static int
894freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
895{
896	struct msghdr32 m32;
897	int error;
898
899	error = copyin(msg32, &m32, sizeof(m32));
900	if (error)
901		return (error);
902	msg->msg_name = PTRIN(m32.msg_name);
903	msg->msg_namelen = m32.msg_namelen;
904	msg->msg_iov = PTRIN(m32.msg_iov);
905	msg->msg_iovlen = m32.msg_iovlen;
906	msg->msg_control = PTRIN(m32.msg_control);
907	msg->msg_controllen = m32.msg_controllen;
908	msg->msg_flags = m32.msg_flags;
909	return (0);
910}
911
912static int
913freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
914{
915	struct msghdr32 m32;
916	int error;
917
918	m32.msg_name = PTROUT(msg->msg_name);
919	m32.msg_namelen = msg->msg_namelen;
920	m32.msg_iov = PTROUT(msg->msg_iov);
921	m32.msg_iovlen = msg->msg_iovlen;
922	m32.msg_control = PTROUT(msg->msg_control);
923	m32.msg_controllen = msg->msg_controllen;
924	m32.msg_flags = msg->msg_flags;
925	error = copyout(&m32, msg32, sizeof(m32));
926	return (error);
927}
928
929#define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
930#define FREEBSD32_ALIGN(p)	\
931	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
932#define	FREEBSD32_CMSG_SPACE(l)	\
933	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
934
935#define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
936				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
937static int
938freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
939{
940	struct cmsghdr *cm;
941	void *data;
942	socklen_t clen, datalen;
943	int error;
944	caddr_t ctlbuf;
945	int len, maxlen, copylen;
946	struct mbuf *m;
947	error = 0;
948
949	len    = msg->msg_controllen;
950	maxlen = msg->msg_controllen;
951	msg->msg_controllen = 0;
952
953	m = control;
954	ctlbuf = msg->msg_control;
955
956	while (m && len > 0) {
957		cm = mtod(m, struct cmsghdr *);
958		clen = m->m_len;
959
960		while (cm != NULL) {
961
962			if (sizeof(struct cmsghdr) > clen ||
963			    cm->cmsg_len > clen) {
964				error = EINVAL;
965				break;
966			}
967
968			data   = CMSG_DATA(cm);
969			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
970
971			/* Adjust message length */
972			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
973			    datalen;
974
975
976			/* Copy cmsghdr */
977			copylen = sizeof(struct cmsghdr);
978			if (len < copylen) {
979				msg->msg_flags |= MSG_CTRUNC;
980				copylen = len;
981			}
982
983			error = copyout(cm,ctlbuf,copylen);
984			if (error)
985				goto exit;
986
987			ctlbuf += FREEBSD32_ALIGN(copylen);
988			len    -= FREEBSD32_ALIGN(copylen);
989
990			if (len <= 0)
991				break;
992
993			/* Copy data */
994			copylen = datalen;
995			if (len < copylen) {
996				msg->msg_flags |= MSG_CTRUNC;
997				copylen = len;
998			}
999
1000			error = copyout(data,ctlbuf,copylen);
1001			if (error)
1002				goto exit;
1003
1004			ctlbuf += FREEBSD32_ALIGN(copylen);
1005			len    -= FREEBSD32_ALIGN(copylen);
1006
1007			if (CMSG_SPACE(datalen) < clen) {
1008				clen -= CMSG_SPACE(datalen);
1009				cm = (struct cmsghdr *)
1010					((caddr_t)cm + CMSG_SPACE(datalen));
1011			} else {
1012				clen = 0;
1013				cm = NULL;
1014			}
1015		}
1016		m = m->m_next;
1017	}
1018
1019	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
1020
1021exit:
1022	return (error);
1023
1024}
1025
1026int
1027freebsd32_recvmsg(td, uap)
1028	struct thread *td;
1029	struct freebsd32_recvmsg_args /* {
1030		int	s;
1031		struct	msghdr32 *msg;
1032		int	flags;
1033	} */ *uap;
1034{
1035	struct msghdr msg;
1036	struct msghdr32 m32;
1037	struct iovec *uiov, *iov;
1038	struct mbuf *control = NULL;
1039	struct mbuf **controlp;
1040
1041	int error;
1042	error = copyin(uap->msg, &m32, sizeof(m32));
1043	if (error)
1044		return (error);
1045	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1046	if (error)
1047		return (error);
1048	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1049	    EMSGSIZE);
1050	if (error)
1051		return (error);
1052	msg.msg_flags = uap->flags;
1053	uiov = msg.msg_iov;
1054	msg.msg_iov = iov;
1055
1056	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1057	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1058	if (error == 0) {
1059		msg.msg_iov = uiov;
1060
1061		if (control != NULL)
1062			error = freebsd32_copy_msg_out(&msg, control);
1063		else
1064			msg.msg_controllen = 0;
1065
1066		if (error == 0)
1067			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1068	}
1069	free(iov, M_IOV);
1070
1071	if (control != NULL)
1072		m_freem(control);
1073
1074	return (error);
1075}
1076
1077
1078static int
1079freebsd32_convert_msg_in(struct mbuf **controlp)
1080{
1081	struct mbuf *control = *controlp;
1082	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1083	void *data;
1084	socklen_t clen = control->m_len, datalen;
1085	int error;
1086
1087	error = 0;
1088	*controlp = NULL;
1089
1090	while (cm != NULL) {
1091		if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1092			error = EINVAL;
1093			break;
1094		}
1095
1096		data = FREEBSD32_CMSG_DATA(cm);
1097		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1098
1099		*controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1100		    cm->cmsg_level);
1101		controlp = &(*controlp)->m_next;
1102
1103		if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1104			clen -= FREEBSD32_CMSG_SPACE(datalen);
1105			cm = (struct cmsghdr *)
1106				((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1107		} else {
1108			clen = 0;
1109			cm = NULL;
1110		}
1111	}
1112
1113	m_freem(control);
1114	return (error);
1115}
1116
1117
1118int
1119freebsd32_sendmsg(struct thread *td,
1120		  struct freebsd32_sendmsg_args *uap)
1121{
1122	struct msghdr msg;
1123	struct msghdr32 m32;
1124	struct iovec *iov;
1125	struct mbuf *control = NULL;
1126	struct sockaddr *to = NULL;
1127	int error;
1128
1129	error = copyin(uap->msg, &m32, sizeof(m32));
1130	if (error)
1131		return (error);
1132	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1133	if (error)
1134		return (error);
1135	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1136	    EMSGSIZE);
1137	if (error)
1138		return (error);
1139	msg.msg_iov = iov;
1140	if (msg.msg_name != NULL) {
1141		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1142		if (error) {
1143			to = NULL;
1144			goto out;
1145		}
1146		msg.msg_name = to;
1147	}
1148
1149	if (msg.msg_control) {
1150		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1151			error = EINVAL;
1152			goto out;
1153		}
1154
1155		error = sockargs(&control, msg.msg_control,
1156		    msg.msg_controllen, MT_CONTROL);
1157		if (error)
1158			goto out;
1159
1160		error = freebsd32_convert_msg_in(&control);
1161		if (error)
1162			goto out;
1163	}
1164
1165	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1166	    UIO_USERSPACE);
1167
1168out:
1169	free(iov, M_IOV);
1170	if (to)
1171		free(to, M_SONAME);
1172	return (error);
1173}
1174
1175int
1176freebsd32_recvfrom(struct thread *td,
1177		   struct freebsd32_recvfrom_args *uap)
1178{
1179	struct msghdr msg;
1180	struct iovec aiov;
1181	int error;
1182
1183	if (uap->fromlenaddr) {
1184		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1185		    sizeof(msg.msg_namelen));
1186		if (error)
1187			return (error);
1188	} else {
1189		msg.msg_namelen = 0;
1190	}
1191
1192	msg.msg_name = PTRIN(uap->from);
1193	msg.msg_iov = &aiov;
1194	msg.msg_iovlen = 1;
1195	aiov.iov_base = PTRIN(uap->buf);
1196	aiov.iov_len = uap->len;
1197	msg.msg_control = NULL;
1198	msg.msg_flags = uap->flags;
1199	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1200	if (error == 0 && uap->fromlenaddr)
1201		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1202		    sizeof (msg.msg_namelen));
1203	return (error);
1204}
1205
1206int
1207freebsd32_settimeofday(struct thread *td,
1208		       struct freebsd32_settimeofday_args *uap)
1209{
1210	struct timeval32 tv32;
1211	struct timeval tv, *tvp;
1212	struct timezone tz, *tzp;
1213	int error;
1214
1215	if (uap->tv) {
1216		error = copyin(uap->tv, &tv32, sizeof(tv32));
1217		if (error)
1218			return (error);
1219		CP(tv32, tv, tv_sec);
1220		CP(tv32, tv, tv_usec);
1221		tvp = &tv;
1222	} else
1223		tvp = NULL;
1224	if (uap->tzp) {
1225		error = copyin(uap->tzp, &tz, sizeof(tz));
1226		if (error)
1227			return (error);
1228		tzp = &tz;
1229	} else
1230		tzp = NULL;
1231	return (kern_settimeofday(td, tvp, tzp));
1232}
1233
1234int
1235freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1236{
1237	struct timeval32 s32[2];
1238	struct timeval s[2], *sp;
1239	int error;
1240
1241	if (uap->tptr != NULL) {
1242		error = copyin(uap->tptr, s32, sizeof(s32));
1243		if (error)
1244			return (error);
1245		CP(s32[0], s[0], tv_sec);
1246		CP(s32[0], s[0], tv_usec);
1247		CP(s32[1], s[1], tv_sec);
1248		CP(s32[1], s[1], tv_usec);
1249		sp = s;
1250	} else
1251		sp = NULL;
1252	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1253}
1254
1255int
1256freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1257{
1258	struct timeval32 s32[2];
1259	struct timeval s[2], *sp;
1260	int error;
1261
1262	if (uap->tptr != NULL) {
1263		error = copyin(uap->tptr, s32, sizeof(s32));
1264		if (error)
1265			return (error);
1266		CP(s32[0], s[0], tv_sec);
1267		CP(s32[0], s[0], tv_usec);
1268		CP(s32[1], s[1], tv_sec);
1269		CP(s32[1], s[1], tv_usec);
1270		sp = s;
1271	} else
1272		sp = NULL;
1273	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1274}
1275
1276int
1277freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1278{
1279	struct timeval32 s32[2];
1280	struct timeval s[2], *sp;
1281	int error;
1282
1283	if (uap->tptr != NULL) {
1284		error = copyin(uap->tptr, s32, sizeof(s32));
1285		if (error)
1286			return (error);
1287		CP(s32[0], s[0], tv_sec);
1288		CP(s32[0], s[0], tv_usec);
1289		CP(s32[1], s[1], tv_sec);
1290		CP(s32[1], s[1], tv_usec);
1291		sp = s;
1292	} else
1293		sp = NULL;
1294	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1295}
1296
1297int
1298freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1299{
1300	struct timeval32 s32[2];
1301	struct timeval s[2], *sp;
1302	int error;
1303
1304	if (uap->times != NULL) {
1305		error = copyin(uap->times, s32, sizeof(s32));
1306		if (error)
1307			return (error);
1308		CP(s32[0], s[0], tv_sec);
1309		CP(s32[0], s[0], tv_usec);
1310		CP(s32[1], s[1], tv_sec);
1311		CP(s32[1], s[1], tv_usec);
1312		sp = s;
1313	} else
1314		sp = NULL;
1315	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1316		sp, UIO_SYSSPACE));
1317}
1318
1319int
1320freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1321{
1322	struct timeval32 tv32;
1323	struct timeval delta, olddelta, *deltap;
1324	int error;
1325
1326	if (uap->delta) {
1327		error = copyin(uap->delta, &tv32, sizeof(tv32));
1328		if (error)
1329			return (error);
1330		CP(tv32, delta, tv_sec);
1331		CP(tv32, delta, tv_usec);
1332		deltap = &delta;
1333	} else
1334		deltap = NULL;
1335	error = kern_adjtime(td, deltap, &olddelta);
1336	if (uap->olddelta && error == 0) {
1337		CP(olddelta, tv32, tv_sec);
1338		CP(olddelta, tv32, tv_usec);
1339		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1340	}
1341	return (error);
1342}
1343
1344#ifdef COMPAT_FREEBSD4
1345int
1346freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1347{
1348	struct statfs32 s32;
1349	struct statfs s;
1350	int error;
1351
1352	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1353	if (error)
1354		return (error);
1355	copy_statfs(&s, &s32);
1356	return (copyout(&s32, uap->buf, sizeof(s32)));
1357}
1358#endif
1359
1360#ifdef COMPAT_FREEBSD4
1361int
1362freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1363{
1364	struct statfs32 s32;
1365	struct statfs s;
1366	int error;
1367
1368	error = kern_fstatfs(td, uap->fd, &s);
1369	if (error)
1370		return (error);
1371	copy_statfs(&s, &s32);
1372	return (copyout(&s32, uap->buf, sizeof(s32)));
1373}
1374#endif
1375
1376#ifdef COMPAT_FREEBSD4
1377int
1378freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1379{
1380	struct statfs32 s32;
1381	struct statfs s;
1382	fhandle_t fh;
1383	int error;
1384
1385	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1386		return (error);
1387	error = kern_fhstatfs(td, fh, &s);
1388	if (error)
1389		return (error);
1390	copy_statfs(&s, &s32);
1391	return (copyout(&s32, uap->buf, sizeof(s32)));
1392}
1393#endif
1394
1395int
1396freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1397{
1398	struct pread_args ap;
1399
1400	ap.fd = uap->fd;
1401	ap.buf = uap->buf;
1402	ap.nbyte = uap->nbyte;
1403	ap.offset = PAIR32TO64(off_t,uap->offset);
1404	return (pread(td, &ap));
1405}
1406
1407int
1408freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1409{
1410	struct pwrite_args ap;
1411
1412	ap.fd = uap->fd;
1413	ap.buf = uap->buf;
1414	ap.nbyte = uap->nbyte;
1415	ap.offset = PAIR32TO64(off_t,uap->offset);
1416	return (pwrite(td, &ap));
1417}
1418
1419int
1420freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1421{
1422	int error;
1423	struct lseek_args ap;
1424	off_t pos;
1425
1426	ap.fd = uap->fd;
1427	ap.offset = PAIR32TO64(off_t,uap->offset);
1428	ap.whence = uap->whence;
1429	error = lseek(td, &ap);
1430	/* Expand the quad return into two parts for eax and edx */
1431	pos = *(off_t *)(td->td_retval);
1432	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1433	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1434	return error;
1435}
1436
1437int
1438freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1439{
1440	struct truncate_args ap;
1441
1442	ap.path = uap->path;
1443	ap.length = PAIR32TO64(off_t,uap->length);
1444	return (truncate(td, &ap));
1445}
1446
1447int
1448freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1449{
1450	struct ftruncate_args ap;
1451
1452	ap.fd = uap->fd;
1453	ap.length = PAIR32TO64(off_t,uap->length);
1454	return (ftruncate(td, &ap));
1455}
1456
1457#ifdef COMPAT_43
1458int
1459ofreebsd32_getdirentries(struct thread *td,
1460    struct ofreebsd32_getdirentries_args *uap)
1461{
1462	struct ogetdirentries_args ap;
1463	int error;
1464	long loff;
1465	int32_t loff_cut;
1466
1467	ap.fd = uap->fd;
1468	ap.buf = uap->buf;
1469	ap.count = uap->count;
1470	ap.basep = NULL;
1471	error = kern_ogetdirentries(td, &ap, &loff);
1472	if (error == 0) {
1473		loff_cut = loff;
1474		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1475	}
1476	return (error);
1477}
1478#endif
1479
1480int
1481freebsd32_getdirentries(struct thread *td,
1482    struct freebsd32_getdirentries_args *uap)
1483{
1484	long base;
1485	int32_t base32;
1486	int error;
1487
1488	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
1489	if (error)
1490		return (error);
1491	if (uap->basep != NULL) {
1492		base32 = base;
1493		error = copyout(&base32, uap->basep, sizeof(int32_t));
1494	}
1495	return (error);
1496}
1497
1498#ifdef COMPAT_FREEBSD6
1499/* versions with the 'int pad' argument */
1500int
1501freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1502{
1503	struct pread_args ap;
1504
1505	ap.fd = uap->fd;
1506	ap.buf = uap->buf;
1507	ap.nbyte = uap->nbyte;
1508	ap.offset = PAIR32TO64(off_t,uap->offset);
1509	return (pread(td, &ap));
1510}
1511
1512int
1513freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1514{
1515	struct pwrite_args ap;
1516
1517	ap.fd = uap->fd;
1518	ap.buf = uap->buf;
1519	ap.nbyte = uap->nbyte;
1520	ap.offset = PAIR32TO64(off_t,uap->offset);
1521	return (pwrite(td, &ap));
1522}
1523
1524int
1525freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1526{
1527	int error;
1528	struct lseek_args ap;
1529	off_t pos;
1530
1531	ap.fd = uap->fd;
1532	ap.offset = PAIR32TO64(off_t,uap->offset);
1533	ap.whence = uap->whence;
1534	error = lseek(td, &ap);
1535	/* Expand the quad return into two parts for eax and edx */
1536	pos = *(off_t *)(td->td_retval);
1537	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1538	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1539	return error;
1540}
1541
1542int
1543freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1544{
1545	struct truncate_args ap;
1546
1547	ap.path = uap->path;
1548	ap.length = PAIR32TO64(off_t,uap->length);
1549	return (truncate(td, &ap));
1550}
1551
1552int
1553freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1554{
1555	struct ftruncate_args ap;
1556
1557	ap.fd = uap->fd;
1558	ap.length = PAIR32TO64(off_t,uap->length);
1559	return (ftruncate(td, &ap));
1560}
1561#endif /* COMPAT_FREEBSD6 */
1562
1563struct sf_hdtr32 {
1564	uint32_t headers;
1565	int hdr_cnt;
1566	uint32_t trailers;
1567	int trl_cnt;
1568};
1569
1570static int
1571freebsd32_do_sendfile(struct thread *td,
1572    struct freebsd32_sendfile_args *uap, int compat)
1573{
1574	struct sendfile_args ap;
1575	struct sf_hdtr32 hdtr32;
1576	struct sf_hdtr hdtr;
1577	struct uio *hdr_uio, *trl_uio;
1578	struct iovec32 *iov32;
1579	int error;
1580
1581	hdr_uio = trl_uio = NULL;
1582
1583	ap.fd = uap->fd;
1584	ap.s = uap->s;
1585	ap.offset = PAIR32TO64(off_t,uap->offset);
1586	ap.nbytes = uap->nbytes;
1587	ap.hdtr = (struct sf_hdtr *)uap->hdtr;		/* XXX not used */
1588	ap.sbytes = uap->sbytes;
1589	ap.flags = uap->flags;
1590
1591	if (uap->hdtr != NULL) {
1592		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1593		if (error)
1594			goto out;
1595		PTRIN_CP(hdtr32, hdtr, headers);
1596		CP(hdtr32, hdtr, hdr_cnt);
1597		PTRIN_CP(hdtr32, hdtr, trailers);
1598		CP(hdtr32, hdtr, trl_cnt);
1599
1600		if (hdtr.headers != NULL) {
1601			iov32 = PTRIN(hdtr32.headers);
1602			error = freebsd32_copyinuio(iov32,
1603			    hdtr32.hdr_cnt, &hdr_uio);
1604			if (error)
1605				goto out;
1606		}
1607		if (hdtr.trailers != NULL) {
1608			iov32 = PTRIN(hdtr32.trailers);
1609			error = freebsd32_copyinuio(iov32,
1610			    hdtr32.trl_cnt, &trl_uio);
1611			if (error)
1612				goto out;
1613		}
1614	}
1615
1616	error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1617out:
1618	if (hdr_uio)
1619		free(hdr_uio, M_IOV);
1620	if (trl_uio)
1621		free(trl_uio, M_IOV);
1622	return (error);
1623}
1624
1625#ifdef COMPAT_FREEBSD4
1626int
1627freebsd4_freebsd32_sendfile(struct thread *td,
1628    struct freebsd4_freebsd32_sendfile_args *uap)
1629{
1630	return (freebsd32_do_sendfile(td,
1631	    (struct freebsd32_sendfile_args *)uap, 1));
1632}
1633#endif
1634
1635int
1636freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1637{
1638
1639	return (freebsd32_do_sendfile(td, uap, 0));
1640}
1641
1642static void
1643copy_stat(struct stat *in, struct stat32 *out)
1644{
1645
1646	CP(*in, *out, st_dev);
1647	CP(*in, *out, st_ino);
1648	CP(*in, *out, st_mode);
1649	CP(*in, *out, st_nlink);
1650	CP(*in, *out, st_uid);
1651	CP(*in, *out, st_gid);
1652	CP(*in, *out, st_rdev);
1653	TS_CP(*in, *out, st_atim);
1654	TS_CP(*in, *out, st_mtim);
1655	TS_CP(*in, *out, st_ctim);
1656	CP(*in, *out, st_size);
1657	CP(*in, *out, st_blocks);
1658	CP(*in, *out, st_blksize);
1659	CP(*in, *out, st_flags);
1660	CP(*in, *out, st_gen);
1661	TS_CP(*in, *out, st_birthtim);
1662}
1663
1664#ifdef COMPAT_43
1665static void
1666copy_ostat(struct stat *in, struct ostat32 *out)
1667{
1668
1669	CP(*in, *out, st_dev);
1670	CP(*in, *out, st_ino);
1671	CP(*in, *out, st_mode);
1672	CP(*in, *out, st_nlink);
1673	CP(*in, *out, st_uid);
1674	CP(*in, *out, st_gid);
1675	CP(*in, *out, st_rdev);
1676	CP(*in, *out, st_size);
1677	TS_CP(*in, *out, st_atim);
1678	TS_CP(*in, *out, st_mtim);
1679	TS_CP(*in, *out, st_ctim);
1680	CP(*in, *out, st_blksize);
1681	CP(*in, *out, st_blocks);
1682	CP(*in, *out, st_flags);
1683	CP(*in, *out, st_gen);
1684}
1685#endif
1686
1687int
1688freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1689{
1690	struct stat sb;
1691	struct stat32 sb32;
1692	int error;
1693
1694	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1695	if (error)
1696		return (error);
1697	copy_stat(&sb, &sb32);
1698	error = copyout(&sb32, uap->ub, sizeof (sb32));
1699	return (error);
1700}
1701
1702#ifdef COMPAT_43
1703int
1704ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1705{
1706	struct stat sb;
1707	struct ostat32 sb32;
1708	int error;
1709
1710	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1711	if (error)
1712		return (error);
1713	copy_ostat(&sb, &sb32);
1714	error = copyout(&sb32, uap->ub, sizeof (sb32));
1715	return (error);
1716}
1717#endif
1718
1719int
1720freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1721{
1722	struct stat ub;
1723	struct stat32 ub32;
1724	int error;
1725
1726	error = kern_fstat(td, uap->fd, &ub);
1727	if (error)
1728		return (error);
1729	copy_stat(&ub, &ub32);
1730	error = copyout(&ub32, uap->ub, sizeof(ub32));
1731	return (error);
1732}
1733
1734#ifdef COMPAT_43
1735int
1736ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1737{
1738	struct stat ub;
1739	struct ostat32 ub32;
1740	int error;
1741
1742	error = kern_fstat(td, uap->fd, &ub);
1743	if (error)
1744		return (error);
1745	copy_ostat(&ub, &ub32);
1746	error = copyout(&ub32, uap->ub, sizeof(ub32));
1747	return (error);
1748}
1749#endif
1750
1751int
1752freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1753{
1754	struct stat ub;
1755	struct stat32 ub32;
1756	int error;
1757
1758	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1759	if (error)
1760		return (error);
1761	copy_stat(&ub, &ub32);
1762	error = copyout(&ub32, uap->buf, sizeof(ub32));
1763	return (error);
1764}
1765
1766int
1767freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1768{
1769	struct stat sb;
1770	struct stat32 sb32;
1771	int error;
1772
1773	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1774	if (error)
1775		return (error);
1776	copy_stat(&sb, &sb32);
1777	error = copyout(&sb32, uap->ub, sizeof (sb32));
1778	return (error);
1779}
1780
1781#ifdef COMPAT_43
1782int
1783ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1784{
1785	struct stat sb;
1786	struct ostat32 sb32;
1787	int error;
1788
1789	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1790	if (error)
1791		return (error);
1792	copy_ostat(&sb, &sb32);
1793	error = copyout(&sb32, uap->ub, sizeof (sb32));
1794	return (error);
1795}
1796#endif
1797
1798int
1799freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1800{
1801	int error, name[CTL_MAXNAME];
1802	size_t j, oldlen;
1803
1804	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1805		return (EINVAL);
1806 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1807 	if (error)
1808		return (error);
1809	if (uap->oldlenp)
1810		oldlen = fuword32(uap->oldlenp);
1811	else
1812		oldlen = 0;
1813	error = userland_sysctl(td, name, uap->namelen,
1814		uap->old, &oldlen, 1,
1815		uap->new, uap->newlen, &j, SCTL_MASK32);
1816	if (error && error != ENOMEM)
1817		return (error);
1818	if (uap->oldlenp)
1819		suword32(uap->oldlenp, j);
1820	return (0);
1821}
1822
1823int
1824freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1825{
1826	uint32_t version;
1827	int error;
1828	struct jail j;
1829
1830	error = copyin(uap->jail, &version, sizeof(uint32_t));
1831	if (error)
1832		return (error);
1833
1834	switch (version) {
1835	case 0:
1836	{
1837		/* FreeBSD single IPv4 jails. */
1838		struct jail32_v0 j32_v0;
1839
1840		bzero(&j, sizeof(struct jail));
1841		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1842		if (error)
1843			return (error);
1844		CP(j32_v0, j, version);
1845		PTRIN_CP(j32_v0, j, path);
1846		PTRIN_CP(j32_v0, j, hostname);
1847		j.ip4s = j32_v0.ip_number;
1848		break;
1849	}
1850
1851	case 1:
1852		/*
1853		 * Version 1 was used by multi-IPv4 jail implementations
1854		 * that never made it into the official kernel.
1855		 */
1856		return (EINVAL);
1857
1858	case 2:	/* JAIL_API_VERSION */
1859	{
1860		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
1861		struct jail32 j32;
1862
1863		error = copyin(uap->jail, &j32, sizeof(struct jail32));
1864		if (error)
1865			return (error);
1866		CP(j32, j, version);
1867		PTRIN_CP(j32, j, path);
1868		PTRIN_CP(j32, j, hostname);
1869		PTRIN_CP(j32, j, jailname);
1870		CP(j32, j, ip4s);
1871		CP(j32, j, ip6s);
1872		PTRIN_CP(j32, j, ip4);
1873		PTRIN_CP(j32, j, ip6);
1874		break;
1875	}
1876
1877	default:
1878		/* Sci-Fi jails are not supported, sorry. */
1879		return (EINVAL);
1880	}
1881	return (kern_jail(td, &j));
1882}
1883
1884int
1885freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1886{
1887	struct uio *auio;
1888	int error;
1889
1890	/* Check that we have an even number of iovecs. */
1891	if (uap->iovcnt & 1)
1892		return (EINVAL);
1893
1894	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1895	if (error)
1896		return (error);
1897	error = kern_jail_set(td, auio, uap->flags);
1898	free(auio, M_IOV);
1899	return (error);
1900}
1901
1902int
1903freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1904{
1905	struct iovec32 iov32;
1906	struct uio *auio;
1907	int error, i;
1908
1909	/* Check that we have an even number of iovecs. */
1910	if (uap->iovcnt & 1)
1911		return (EINVAL);
1912
1913	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1914	if (error)
1915		return (error);
1916	error = kern_jail_get(td, auio, uap->flags);
1917	if (error == 0)
1918		for (i = 0; i < uap->iovcnt; i++) {
1919			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1920			CP(auio->uio_iov[i], iov32, iov_len);
1921			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1922			if (error != 0)
1923				break;
1924		}
1925	free(auio, M_IOV);
1926	return (error);
1927}
1928
1929int
1930freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1931{
1932	struct sigaction32 s32;
1933	struct sigaction sa, osa, *sap;
1934	int error;
1935
1936	if (uap->act) {
1937		error = copyin(uap->act, &s32, sizeof(s32));
1938		if (error)
1939			return (error);
1940		sa.sa_handler = PTRIN(s32.sa_u);
1941		CP(s32, sa, sa_flags);
1942		CP(s32, sa, sa_mask);
1943		sap = &sa;
1944	} else
1945		sap = NULL;
1946	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1947	if (error == 0 && uap->oact != NULL) {
1948		s32.sa_u = PTROUT(osa.sa_handler);
1949		CP(osa, s32, sa_flags);
1950		CP(osa, s32, sa_mask);
1951		error = copyout(&s32, uap->oact, sizeof(s32));
1952	}
1953	return (error);
1954}
1955
1956#ifdef COMPAT_FREEBSD4
1957int
1958freebsd4_freebsd32_sigaction(struct thread *td,
1959			     struct freebsd4_freebsd32_sigaction_args *uap)
1960{
1961	struct sigaction32 s32;
1962	struct sigaction sa, osa, *sap;
1963	int error;
1964
1965	if (uap->act) {
1966		error = copyin(uap->act, &s32, sizeof(s32));
1967		if (error)
1968			return (error);
1969		sa.sa_handler = PTRIN(s32.sa_u);
1970		CP(s32, sa, sa_flags);
1971		CP(s32, sa, sa_mask);
1972		sap = &sa;
1973	} else
1974		sap = NULL;
1975	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
1976	if (error == 0 && uap->oact != NULL) {
1977		s32.sa_u = PTROUT(osa.sa_handler);
1978		CP(osa, s32, sa_flags);
1979		CP(osa, s32, sa_mask);
1980		error = copyout(&s32, uap->oact, sizeof(s32));
1981	}
1982	return (error);
1983}
1984#endif
1985
1986#ifdef COMPAT_43
1987struct osigaction32 {
1988	u_int32_t	sa_u;
1989	osigset_t	sa_mask;
1990	int		sa_flags;
1991};
1992
1993#define	ONSIG	32
1994
1995int
1996ofreebsd32_sigaction(struct thread *td,
1997			     struct ofreebsd32_sigaction_args *uap)
1998{
1999	struct osigaction32 s32;
2000	struct sigaction sa, osa, *sap;
2001	int error;
2002
2003	if (uap->signum <= 0 || uap->signum >= ONSIG)
2004		return (EINVAL);
2005
2006	if (uap->nsa) {
2007		error = copyin(uap->nsa, &s32, sizeof(s32));
2008		if (error)
2009			return (error);
2010		sa.sa_handler = PTRIN(s32.sa_u);
2011		CP(s32, sa, sa_flags);
2012		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2013		sap = &sa;
2014	} else
2015		sap = NULL;
2016	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2017	if (error == 0 && uap->osa != NULL) {
2018		s32.sa_u = PTROUT(osa.sa_handler);
2019		CP(osa, s32, sa_flags);
2020		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2021		error = copyout(&s32, uap->osa, sizeof(s32));
2022	}
2023	return (error);
2024}
2025
2026int
2027ofreebsd32_sigprocmask(struct thread *td,
2028			       struct ofreebsd32_sigprocmask_args *uap)
2029{
2030	sigset_t set, oset;
2031	int error;
2032
2033	OSIG2SIG(uap->mask, set);
2034	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2035	SIG2OSIG(oset, td->td_retval[0]);
2036	return (error);
2037}
2038
2039int
2040ofreebsd32_sigpending(struct thread *td,
2041			      struct ofreebsd32_sigpending_args *uap)
2042{
2043	struct proc *p = td->td_proc;
2044	sigset_t siglist;
2045
2046	PROC_LOCK(p);
2047	siglist = p->p_siglist;
2048	SIGSETOR(siglist, td->td_siglist);
2049	PROC_UNLOCK(p);
2050	SIG2OSIG(siglist, td->td_retval[0]);
2051	return (0);
2052}
2053
2054struct sigvec32 {
2055	u_int32_t	sv_handler;
2056	int		sv_mask;
2057	int		sv_flags;
2058};
2059
2060int
2061ofreebsd32_sigvec(struct thread *td,
2062			  struct ofreebsd32_sigvec_args *uap)
2063{
2064	struct sigvec32 vec;
2065	struct sigaction sa, osa, *sap;
2066	int error;
2067
2068	if (uap->signum <= 0 || uap->signum >= ONSIG)
2069		return (EINVAL);
2070
2071	if (uap->nsv) {
2072		error = copyin(uap->nsv, &vec, sizeof(vec));
2073		if (error)
2074			return (error);
2075		sa.sa_handler = PTRIN(vec.sv_handler);
2076		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2077		sa.sa_flags = vec.sv_flags;
2078		sa.sa_flags ^= SA_RESTART;
2079		sap = &sa;
2080	} else
2081		sap = NULL;
2082	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2083	if (error == 0 && uap->osv != NULL) {
2084		vec.sv_handler = PTROUT(osa.sa_handler);
2085		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2086		vec.sv_flags = osa.sa_flags;
2087		vec.sv_flags &= ~SA_NOCLDWAIT;
2088		vec.sv_flags ^= SA_RESTART;
2089		error = copyout(&vec, uap->osv, sizeof(vec));
2090	}
2091	return (error);
2092}
2093
2094int
2095ofreebsd32_sigblock(struct thread *td,
2096			    struct ofreebsd32_sigblock_args *uap)
2097{
2098	sigset_t set, oset;
2099
2100	OSIG2SIG(uap->mask, set);
2101	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2102	SIG2OSIG(oset, td->td_retval[0]);
2103	return (0);
2104}
2105
2106int
2107ofreebsd32_sigsetmask(struct thread *td,
2108			      struct ofreebsd32_sigsetmask_args *uap)
2109{
2110	sigset_t set, oset;
2111
2112	OSIG2SIG(uap->mask, set);
2113	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2114	SIG2OSIG(oset, td->td_retval[0]);
2115	return (0);
2116}
2117
2118int
2119ofreebsd32_sigsuspend(struct thread *td,
2120			      struct ofreebsd32_sigsuspend_args *uap)
2121{
2122	sigset_t mask;
2123
2124	OSIG2SIG(uap->mask, mask);
2125	return (kern_sigsuspend(td, mask));
2126}
2127
2128struct sigstack32 {
2129	u_int32_t	ss_sp;
2130	int		ss_onstack;
2131};
2132
2133int
2134ofreebsd32_sigstack(struct thread *td,
2135			    struct ofreebsd32_sigstack_args *uap)
2136{
2137	struct sigstack32 s32;
2138	struct sigstack nss, oss;
2139	int error = 0, unss;
2140
2141	if (uap->nss != NULL) {
2142		error = copyin(uap->nss, &s32, sizeof(s32));
2143		if (error)
2144			return (error);
2145		nss.ss_sp = PTRIN(s32.ss_sp);
2146		CP(s32, nss, ss_onstack);
2147		unss = 1;
2148	} else {
2149		unss = 0;
2150	}
2151	oss.ss_sp = td->td_sigstk.ss_sp;
2152	oss.ss_onstack = sigonstack(cpu_getstack(td));
2153	if (unss) {
2154		td->td_sigstk.ss_sp = nss.ss_sp;
2155		td->td_sigstk.ss_size = 0;
2156		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2157		td->td_pflags |= TDP_ALTSTACK;
2158	}
2159	if (uap->oss != NULL) {
2160		s32.ss_sp = PTROUT(oss.ss_sp);
2161		CP(oss, s32, ss_onstack);
2162		error = copyout(&s32, uap->oss, sizeof(s32));
2163	}
2164	return (error);
2165}
2166#endif
2167
2168int
2169freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2170{
2171	struct timespec32 rmt32, rqt32;
2172	struct timespec rmt, rqt;
2173	int error;
2174
2175	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2176	if (error)
2177		return (error);
2178
2179	CP(rqt32, rqt, tv_sec);
2180	CP(rqt32, rqt, tv_nsec);
2181
2182	if (uap->rmtp &&
2183	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2184		return (EFAULT);
2185	error = kern_nanosleep(td, &rqt, &rmt);
2186	if (error && uap->rmtp) {
2187		int error2;
2188
2189		CP(rmt, rmt32, tv_sec);
2190		CP(rmt, rmt32, tv_nsec);
2191
2192		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2193		if (error2)
2194			error = error2;
2195	}
2196	return (error);
2197}
2198
2199int
2200freebsd32_clock_gettime(struct thread *td,
2201			struct freebsd32_clock_gettime_args *uap)
2202{
2203	struct timespec	ats;
2204	struct timespec32 ats32;
2205	int error;
2206
2207	error = kern_clock_gettime(td, uap->clock_id, &ats);
2208	if (error == 0) {
2209		CP(ats, ats32, tv_sec);
2210		CP(ats, ats32, tv_nsec);
2211		error = copyout(&ats32, uap->tp, sizeof(ats32));
2212	}
2213	return (error);
2214}
2215
2216int
2217freebsd32_clock_settime(struct thread *td,
2218			struct freebsd32_clock_settime_args *uap)
2219{
2220	struct timespec	ats;
2221	struct timespec32 ats32;
2222	int error;
2223
2224	error = copyin(uap->tp, &ats32, sizeof(ats32));
2225	if (error)
2226		return (error);
2227	CP(ats32, ats, tv_sec);
2228	CP(ats32, ats, tv_nsec);
2229
2230	return (kern_clock_settime(td, uap->clock_id, &ats));
2231}
2232
2233int
2234freebsd32_clock_getres(struct thread *td,
2235		       struct freebsd32_clock_getres_args *uap)
2236{
2237	struct timespec	ts;
2238	struct timespec32 ts32;
2239	int error;
2240
2241	if (uap->tp == NULL)
2242		return (0);
2243	error = kern_clock_getres(td, uap->clock_id, &ts);
2244	if (error == 0) {
2245		CP(ts, ts32, tv_sec);
2246		CP(ts, ts32, tv_nsec);
2247		error = copyout(&ts32, uap->tp, sizeof(ts32));
2248	}
2249	return (error);
2250}
2251
2252int
2253freebsd32_thr_new(struct thread *td,
2254		  struct freebsd32_thr_new_args *uap)
2255{
2256	struct thr_param32 param32;
2257	struct thr_param param;
2258	int error;
2259
2260	if (uap->param_size < 0 ||
2261	    uap->param_size > sizeof(struct thr_param32))
2262		return (EINVAL);
2263	bzero(&param, sizeof(struct thr_param));
2264	bzero(&param32, sizeof(struct thr_param32));
2265	error = copyin(uap->param, &param32, uap->param_size);
2266	if (error != 0)
2267		return (error);
2268	param.start_func = PTRIN(param32.start_func);
2269	param.arg = PTRIN(param32.arg);
2270	param.stack_base = PTRIN(param32.stack_base);
2271	param.stack_size = param32.stack_size;
2272	param.tls_base = PTRIN(param32.tls_base);
2273	param.tls_size = param32.tls_size;
2274	param.child_tid = PTRIN(param32.child_tid);
2275	param.parent_tid = PTRIN(param32.parent_tid);
2276	param.flags = param32.flags;
2277	param.rtp = PTRIN(param32.rtp);
2278	param.spare[0] = PTRIN(param32.spare[0]);
2279	param.spare[1] = PTRIN(param32.spare[1]);
2280	param.spare[2] = PTRIN(param32.spare[2]);
2281
2282	return (kern_thr_new(td, &param));
2283}
2284
2285int
2286freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2287{
2288	struct timespec32 ts32;
2289	struct timespec ts, *tsp;
2290	int error;
2291
2292	error = 0;
2293	tsp = NULL;
2294	if (uap->timeout != NULL) {
2295		error = copyin((const void *)uap->timeout, (void *)&ts32,
2296		    sizeof(struct timespec32));
2297		if (error != 0)
2298			return (error);
2299		ts.tv_sec = ts32.tv_sec;
2300		ts.tv_nsec = ts32.tv_nsec;
2301		tsp = &ts;
2302	}
2303	return (kern_thr_suspend(td, tsp));
2304}
2305
2306void
2307siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2308{
2309	bzero(dst, sizeof(*dst));
2310	dst->si_signo = src->si_signo;
2311	dst->si_errno = src->si_errno;
2312	dst->si_code = src->si_code;
2313	dst->si_pid = src->si_pid;
2314	dst->si_uid = src->si_uid;
2315	dst->si_status = src->si_status;
2316	dst->si_addr = (uintptr_t)src->si_addr;
2317	dst->si_value.sigval_int = src->si_value.sival_int;
2318	dst->si_timerid = src->si_timerid;
2319	dst->si_overrun = src->si_overrun;
2320}
2321
2322int
2323freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2324{
2325	struct timespec32 ts32;
2326	struct timespec ts;
2327	struct timespec *timeout;
2328	sigset_t set;
2329	ksiginfo_t ksi;
2330	struct siginfo32 si32;
2331	int error;
2332
2333	if (uap->timeout) {
2334		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2335		if (error)
2336			return (error);
2337		ts.tv_sec = ts32.tv_sec;
2338		ts.tv_nsec = ts32.tv_nsec;
2339		timeout = &ts;
2340	} else
2341		timeout = NULL;
2342
2343	error = copyin(uap->set, &set, sizeof(set));
2344	if (error)
2345		return (error);
2346
2347	error = kern_sigtimedwait(td, set, &ksi, timeout);
2348	if (error)
2349		return (error);
2350
2351	if (uap->info) {
2352		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2353		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2354	}
2355
2356	if (error == 0)
2357		td->td_retval[0] = ksi.ksi_signo;
2358	return (error);
2359}
2360
2361/*
2362 * MPSAFE
2363 */
2364int
2365freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2366{
2367	ksiginfo_t ksi;
2368	struct siginfo32 si32;
2369	sigset_t set;
2370	int error;
2371
2372	error = copyin(uap->set, &set, sizeof(set));
2373	if (error)
2374		return (error);
2375
2376	error = kern_sigtimedwait(td, set, &ksi, NULL);
2377	if (error)
2378		return (error);
2379
2380	if (uap->info) {
2381		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2382		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2383	}
2384	if (error == 0)
2385		td->td_retval[0] = ksi.ksi_signo;
2386	return (error);
2387}
2388
2389int
2390freebsd32_cpuset_setid(struct thread *td,
2391    struct freebsd32_cpuset_setid_args *uap)
2392{
2393	struct cpuset_setid_args ap;
2394
2395	ap.which = uap->which;
2396	ap.id = PAIR32TO64(id_t,uap->id);
2397	ap.setid = uap->setid;
2398
2399	return (cpuset_setid(td, &ap));
2400}
2401
2402int
2403freebsd32_cpuset_getid(struct thread *td,
2404    struct freebsd32_cpuset_getid_args *uap)
2405{
2406	struct cpuset_getid_args ap;
2407
2408	ap.level = uap->level;
2409	ap.which = uap->which;
2410	ap.id = PAIR32TO64(id_t,uap->id);
2411	ap.setid = uap->setid;
2412
2413	return (cpuset_getid(td, &ap));
2414}
2415
2416int
2417freebsd32_cpuset_getaffinity(struct thread *td,
2418    struct freebsd32_cpuset_getaffinity_args *uap)
2419{
2420	struct cpuset_getaffinity_args ap;
2421
2422	ap.level = uap->level;
2423	ap.which = uap->which;
2424	ap.id = PAIR32TO64(id_t,uap->id);
2425	ap.cpusetsize = uap->cpusetsize;
2426	ap.mask = uap->mask;
2427
2428	return (cpuset_getaffinity(td, &ap));
2429}
2430
2431int
2432freebsd32_cpuset_setaffinity(struct thread *td,
2433    struct freebsd32_cpuset_setaffinity_args *uap)
2434{
2435	struct cpuset_setaffinity_args ap;
2436
2437	ap.level = uap->level;
2438	ap.which = uap->which;
2439	ap.id = PAIR32TO64(id_t,uap->id);
2440	ap.cpusetsize = uap->cpusetsize;
2441	ap.mask = uap->mask;
2442
2443	return (cpuset_setaffinity(td, &ap));
2444}
2445
2446int
2447freebsd32_nmount(struct thread *td,
2448    struct freebsd32_nmount_args /* {
2449    	struct iovec *iovp;
2450    	unsigned int iovcnt;
2451    	int flags;
2452    } */ *uap)
2453{
2454	struct uio *auio;
2455	int error;
2456
2457	AUDIT_ARG_FFLAGS(uap->flags);
2458
2459	/*
2460	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2461	 * userspace to set this flag, but we must filter it out if we want
2462	 * MNT_UPDATE on the root file system to work.
2463	 * MNT_ROOTFS should only be set by the kernel when mounting its
2464	 * root file system.
2465	 */
2466	uap->flags &= ~MNT_ROOTFS;
2467
2468	/*
2469	 * check that we have an even number of iovec's
2470	 * and that we have at least two options.
2471	 */
2472	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2473		return (EINVAL);
2474
2475	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2476	if (error)
2477		return (error);
2478	error = vfs_donmount(td, uap->flags, auio);
2479
2480	free(auio, M_IOV);
2481	return error;
2482}
2483
2484#if 0
2485int
2486freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2487{
2488	struct yyy32 *p32, s32;
2489	struct yyy *p = NULL, s;
2490	struct xxx_arg ap;
2491	int error;
2492
2493	if (uap->zzz) {
2494		error = copyin(uap->zzz, &s32, sizeof(s32));
2495		if (error)
2496			return (error);
2497		/* translate in */
2498		p = &s;
2499	}
2500	error = kern_xxx(td, p);
2501	if (error)
2502		return (error);
2503	if (uap->zzz) {
2504		/* translate out */
2505		error = copyout(&s32, p32, sizeof(s32));
2506	}
2507	return (error);
2508}
2509#endif
2510
2511int
2512syscall32_register(int *offset, struct sysent *new_sysent,
2513    struct sysent *old_sysent)
2514{
2515	if (*offset == NO_SYSCALL) {
2516		int i;
2517
2518		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2519			if (freebsd32_sysent[i].sy_call ==
2520			    (sy_call_t *)lkmnosys)
2521				break;
2522		if (i == SYS_MAXSYSCALL)
2523			return (ENFILE);
2524		*offset = i;
2525	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2526		return (EINVAL);
2527	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2528	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2529		return (EEXIST);
2530
2531	*old_sysent = freebsd32_sysent[*offset];
2532	freebsd32_sysent[*offset] = *new_sysent;
2533	return 0;
2534}
2535
2536int
2537syscall32_deregister(int *offset, struct sysent *old_sysent)
2538{
2539
2540	if (*offset)
2541		freebsd32_sysent[*offset] = *old_sysent;
2542	return 0;
2543}
2544
2545int
2546syscall32_module_handler(struct module *mod, int what, void *arg)
2547{
2548	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2549	modspecific_t ms;
2550	int error;
2551
2552	switch (what) {
2553	case MOD_LOAD:
2554		error = syscall32_register(data->offset, data->new_sysent,
2555		    &data->old_sysent);
2556		if (error) {
2557			/* Leave a mark so we know to safely unload below. */
2558			data->offset = NULL;
2559			return error;
2560		}
2561		ms.intval = *data->offset;
2562		MOD_XLOCK;
2563		module_setspecific(mod, &ms);
2564		MOD_XUNLOCK;
2565		if (data->chainevh)
2566			error = data->chainevh(mod, what, data->chainarg);
2567		return (error);
2568	case MOD_UNLOAD:
2569		/*
2570		 * MOD_LOAD failed, so just return without calling the
2571		 * chained handler since we didn't pass along the MOD_LOAD
2572		 * event.
2573		 */
2574		if (data->offset == NULL)
2575			return (0);
2576		if (data->chainevh) {
2577			error = data->chainevh(mod, what, data->chainarg);
2578			if (error)
2579				return (error);
2580		}
2581		error = syscall32_deregister(data->offset, &data->old_sysent);
2582		return (error);
2583	default:
2584		error = EOPNOTSUPP;
2585		if (data->chainevh)
2586			error = data->chainevh(mod, what, data->chainarg);
2587		return (error);
2588	}
2589}
2590
2591int
2592syscall32_helper_register(struct syscall_helper_data *sd)
2593{
2594	struct syscall_helper_data *sd1;
2595	int error;
2596
2597	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2598		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2599		    &sd1->old_sysent);
2600		if (error != 0) {
2601			syscall32_helper_unregister(sd);
2602			return (error);
2603		}
2604		sd1->registered = 1;
2605	}
2606	return (0);
2607}
2608
2609int
2610syscall32_helper_unregister(struct syscall_helper_data *sd)
2611{
2612	struct syscall_helper_data *sd1;
2613
2614	for (sd1 = sd; sd1->registered != 0; sd1++) {
2615		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2616		sd1->registered = 0;
2617	}
2618	return (0);
2619}
2620
2621register_t *
2622freebsd32_copyout_strings(struct image_params *imgp)
2623{
2624	int argc, envc, i;
2625	u_int32_t *vectp;
2626	char *stringp, *destp;
2627	u_int32_t *stack_base;
2628	struct freebsd32_ps_strings *arginfo;
2629	char canary[sizeof(long) * 8];
2630	int32_t pagesizes32[MAXPAGESIZES];
2631	size_t execpath_len;
2632	int szsigcode;
2633
2634	/*
2635	 * Calculate string base and vector table pointers.
2636	 * Also deal with signal trampoline code for this exec type.
2637	 */
2638	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2639		execpath_len = strlen(imgp->execpath) + 1;
2640	else
2641		execpath_len = 0;
2642	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2643	    sv_psstrings;
2644	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2645		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2646	else
2647		szsigcode = 0;
2648	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2649	    roundup(execpath_len, sizeof(char *)) -
2650	    roundup(sizeof(canary), sizeof(char *)) -
2651	    roundup(sizeof(pagesizes32), sizeof(char *)) -
2652	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2653
2654	/*
2655	 * install sigcode
2656	 */
2657	if (szsigcode != 0)
2658		copyout(imgp->proc->p_sysent->sv_sigcode,
2659			((caddr_t)arginfo - szsigcode), szsigcode);
2660
2661	/*
2662	 * Copy the image path for the rtld.
2663	 */
2664	if (execpath_len != 0) {
2665		imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2666		copyout(imgp->execpath, (void *)imgp->execpathp,
2667		    execpath_len);
2668	}
2669
2670	/*
2671	 * Prepare the canary for SSP.
2672	 */
2673	arc4rand(canary, sizeof(canary), 0);
2674	imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2675	    sizeof(canary);
2676	copyout(canary, (void *)imgp->canary, sizeof(canary));
2677	imgp->canarylen = sizeof(canary);
2678
2679	/*
2680	 * Prepare the pagesizes array.
2681	 */
2682	for (i = 0; i < MAXPAGESIZES; i++)
2683		pagesizes32[i] = (uint32_t)pagesizes[i];
2684	imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2685	    roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2686	copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2687	imgp->pagesizeslen = sizeof(pagesizes32);
2688
2689	/*
2690	 * If we have a valid auxargs ptr, prepare some room
2691	 * on the stack.
2692	 */
2693	if (imgp->auxargs) {
2694		/*
2695		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2696		 * lower compatibility.
2697		 */
2698		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2699			: (AT_COUNT * 2);
2700		/*
2701		 * The '+ 2' is for the null pointers at the end of each of
2702		 * the arg and env vector sets,and imgp->auxarg_size is room
2703		 * for argument of Runtime loader.
2704		 */
2705		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2706		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2707		    sizeof(u_int32_t));
2708	} else
2709		/*
2710		 * The '+ 2' is for the null pointers at the end of each of
2711		 * the arg and env vector sets
2712		 */
2713		vectp = (u_int32_t *)
2714			(destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2715
2716	/*
2717	 * vectp also becomes our initial stack base
2718	 */
2719	stack_base = vectp;
2720
2721	stringp = imgp->args->begin_argv;
2722	argc = imgp->args->argc;
2723	envc = imgp->args->envc;
2724	/*
2725	 * Copy out strings - arguments and environment.
2726	 */
2727	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2728
2729	/*
2730	 * Fill in "ps_strings" struct for ps, w, etc.
2731	 */
2732	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2733	suword32(&arginfo->ps_nargvstr, argc);
2734
2735	/*
2736	 * Fill in argument portion of vector table.
2737	 */
2738	for (; argc > 0; --argc) {
2739		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2740		while (*stringp++ != 0)
2741			destp++;
2742		destp++;
2743	}
2744
2745	/* a null vector table pointer separates the argp's from the envp's */
2746	suword32(vectp++, 0);
2747
2748	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2749	suword32(&arginfo->ps_nenvstr, envc);
2750
2751	/*
2752	 * Fill in environment portion of vector table.
2753	 */
2754	for (; envc > 0; --envc) {
2755		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2756		while (*stringp++ != 0)
2757			destp++;
2758		destp++;
2759	}
2760
2761	/* end of vector table is a null pointer */
2762	suword32(vectp, 0);
2763
2764	return ((register_t *)stack_base);
2765}
2766
2767int
2768freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2769{
2770	struct kld_file_stat stat;
2771	struct kld32_file_stat stat32;
2772	int error, version;
2773
2774	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2775	    != 0)
2776		return (error);
2777	if (version != sizeof(struct kld32_file_stat_1) &&
2778	    version != sizeof(struct kld32_file_stat))
2779		return (EINVAL);
2780
2781	error = kern_kldstat(td, uap->fileid, &stat);
2782	if (error != 0)
2783		return (error);
2784
2785	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2786	CP(stat, stat32, refs);
2787	CP(stat, stat32, id);
2788	PTROUT_CP(stat, stat32, address);
2789	CP(stat, stat32, size);
2790	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2791	return (copyout(&stat32, uap->stat, version));
2792}
2793