freebsd32_misc.c revision 174268
1163302Sru/*-
2153761Swollman * Copyright (c) 2002 Doug Rabson
367578Swollman * All rights reserved.
42742Swollman *
52742Swollman * Redistribution and use in source and binary forms, with or without
62742Swollman * modification, are permitted provided that the following conditions
72742Swollman * are met:
8158421Swollman * 1. Redistributions of source code must retain the above copyright
92742Swollman *    notice, this list of conditions and the following disclaimer.
102742Swollman * 2. Redistributions in binary form must reproduce the above copyright
11158421Swollman *    notice, this list of conditions and the following disclaimer in the
12158421Swollman *    documentation and/or other materials provided with the distribution.
132742Swollman *
1486222Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1520094Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620094Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720094Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1820094Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920094Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20158421Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21158421Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220094Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232742Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242742Swollman * SUCH DAMAGE.
252742Swollman */
262742Swollman
2714343Swollman#include <sys/cdefs.h>
2858787Sru__FBSDID("$FreeBSD: head/sys/compat/freebsd32/freebsd32_misc.c 174268 2007-12-04 20:25:41Z jkim $");
2914343Swollman
3030711Swollman#include "opt_compat.h"
3130711Swollman
32149514Swollman#include <sys/param.h>
3330711Swollman#include <sys/systm.h>
3430711Swollman#include <sys/bus.h>
3530711Swollman#include <sys/clock.h>
3630711Swollman#include <sys/exec.h>
3730711Swollman#include <sys/fcntl.h>
382742Swollman#include <sys/filedesc.h>
3930711Swollman#include <sys/namei.h>
4030711Swollman#include <sys/imgact.h>
4130711Swollman#include <sys/kernel.h>
4230711Swollman#include <sys/limits.h>
4330711Swollman#include <sys/lock.h>
442742Swollman#include <sys/malloc.h>
4530711Swollman#include <sys/file.h>		/* Must come after sys/malloc.h */
4630711Swollman#include <sys/mbuf.h>
4730711Swollman#include <sys/mman.h>
4830711Swollman#include <sys/module.h>
4930711Swollman#include <sys/mount.h>
5030711Swollman#include <sys/mutex.h>
5130711Swollman#include <sys/proc.h>
5230711Swollman#include <sys/reboot.h>
5330711Swollman#include <sys/resource.h>
5430711Swollman#include <sys/resourcevar.h>
552742Swollman#include <sys/selinfo.h>
5630711Swollman#include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
5730711Swollman#include <sys/pipe.h>		/* Must come after sys/selinfo.h */
5830711Swollman#include <sys/signal.h>
5986222Swollman#include <sys/signalvar.h>
6030711Swollman#include <sys/socket.h>
6130711Swollman#include <sys/socketvar.h>
622742Swollman#include <sys/stat.h>
6330711Swollman#include <sys/syscall.h>
642742Swollman#include <sys/syscallsubr.h>
652742Swollman#include <sys/sysctl.h>
662742Swollman#include <sys/sysent.h>
6719878Swollman#include <sys/sysproto.h>
68158421Swollman#include <sys/thr.h>
6919878Swollman#include <sys/unistd.h>
7019878Swollman#include <sys/ucontext.h>
7119878Swollman#include <sys/vnode.h>
7219878Swollman#include <sys/wait.h>
732742Swollman#include <sys/ipc.h>
7419878Swollman#include <sys/shm.h>
752742Swollman
7619878Swollman#include <vm/vm.h>
772742Swollman#include <vm/vm_kern.h>
78158421Swollman#include <vm/vm_param.h>
792742Swollman#include <vm/pmap.h>
802742Swollman#include <vm/vm_map.h>
8119878Swollman#include <vm/vm_object.h>
822742Swollman#include <vm/vm_extern.h>
8319878Swollman
842742Swollman#include <machine/cpu.h>
8519878Swollman
862742Swollman#include <compat/freebsd32/freebsd32_util.h>
8719878Swollman#include <compat/freebsd32/freebsd32.h>
882742Swollman#include <compat/freebsd32/freebsd32_signal.h>
89158421Swollman#include <compat/freebsd32/freebsd32_proto.h>
90158421Swollman
912742SwollmanCTASSERT(sizeof(struct timeval32) == 8);
922742SwollmanCTASSERT(sizeof(struct timespec32) == 8);
9314343SwollmanCTASSERT(sizeof(struct statfs32) == 256);
9419878SwollmanCTASSERT(sizeof(struct rusage32) == 72);
9519878Swollman
962742Swollmanint
9719878Swollmanfreebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
9819878Swollman{
9919878Swollman	int error, status;
10019878Swollman	struct rusage32 ru32;
10119878Swollman	struct rusage ru, *rup;
1022742Swollman
1032742Swollman	if (uap->rusage != NULL)
1042742Swollman		rup = &ru;
1052742Swollman	else
10686222Swollman		rup = NULL;
10730711Swollman	error = kern_wait(td, uap->pid, &status, uap->options, rup);
1082742Swollman	if (error)
1092742Swollman		return (error);
110158421Swollman	if (uap->status != NULL)
111158421Swollman		error = copyout(&status, uap->status, sizeof(status));
1122742Swollman	if (uap->rusage != NULL && error == 0) {
1132742Swollman		TV_CP(ru, ru32, ru_utime);
11430711Swollman		TV_CP(ru, ru32, ru_stime);
11530711Swollman		CP(ru, ru32, ru_maxrss);
1162742Swollman		CP(ru, ru32, ru_ixrss);
1172742Swollman		CP(ru, ru32, ru_idrss);
1182742Swollman		CP(ru, ru32, ru_isrss);
1192742Swollman		CP(ru, ru32, ru_minflt);
12030711Swollman		CP(ru, ru32, ru_majflt);
12130711Swollman		CP(ru, ru32, ru_nswap);
12230711Swollman		CP(ru, ru32, ru_inblock);
1232742Swollman		CP(ru, ru32, ru_oublock);
1242742Swollman		CP(ru, ru32, ru_msgsnd);
1252742Swollman		CP(ru, ru32, ru_msgrcv);
1262742Swollman		CP(ru, ru32, ru_nsignals);
12730711Swollman		CP(ru, ru32, ru_nvcsw);
1282742Swollman		CP(ru, ru32, ru_nivcsw);
1292742Swollman		error = copyout(&ru32, uap->rusage, sizeof(ru32));
1302742Swollman	}
1312742Swollman	return (error);
13230711Swollman}
1332742Swollman
1342742Swollman#ifdef COMPAT_FREEBSD4
135158421Swollmanstatic void
1362742Swollmancopy_statfs(struct statfs *in, struct statfs32 *out)
1372742Swollman{
13830711Swollman
1392742Swollman	statfs_scale_blocks(in, INT32_MAX);
1402742Swollman	bzero(out, sizeof(*out));
1412742Swollman	CP(*in, *out, f_bsize);
1422742Swollman	out->f_iosize = MIN(in->f_iosize, INT32_MAX);
1432742Swollman	CP(*in, *out, f_blocks);
1442742Swollman	CP(*in, *out, f_bfree);
1452742Swollman	CP(*in, *out, f_bavail);
14619878Swollman	out->f_files = MIN(in->f_files, INT32_MAX);
1472742Swollman	out->f_ffree = MIN(in->f_ffree, INT32_MAX);
1482742Swollman	CP(*in, *out, f_fsid);
1492742Swollman	CP(*in, *out, f_owner);
1502742Swollman	CP(*in, *out, f_type);
15130711Swollman	CP(*in, *out, f_flags);
1522742Swollman	out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
1532742Swollman	out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
1542742Swollman	strlcpy(out->f_fstypename,
1552742Swollman	      in->f_fstypename, MFSNAMELEN);
15630711Swollman	strlcpy(out->f_mntonname,
15730711Swollman	      in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
15830711Swollman	out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
1592742Swollman	out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
1602742Swollman	strlcpy(out->f_mntfromname,
1612742Swollman	      in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
1622742Swollman}
1632742Swollman#endif
1642742Swollman
16530711Swollman#ifdef COMPAT_FREEBSD4
1662742Swollmanint
16730711Swollmanfreebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
16830711Swollman{
16930711Swollman	struct statfs *buf, *sp;
17030711Swollman	struct statfs32 stat32;
17130711Swollman	size_t count, size;
17230711Swollman	int error;
17330711Swollman
1742742Swollman	count = uap->bufsize / sizeof(struct statfs32);
17530711Swollman	size = count * sizeof(struct statfs);
1762742Swollman	error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
1772742Swollman	if (size > 0) {
1782742Swollman		count = td->td_retval[0];
1792742Swollman		sp = buf;
18030711Swollman		while (count > 0 && error == 0) {
1812742Swollman			copy_statfs(sp, &stat32);
1822742Swollman			error = copyout(&stat32, uap->buf, sizeof(stat32));
1832742Swollman			sp++;
1842742Swollman			uap->buf++;
1852742Swollman			count--;
1862742Swollman		}
1872742Swollman		free(buf, M_TEMP);
1882742Swollman	}
1892742Swollman	return (error);
1902742Swollman}
1912742Swollman#endif
19219878Swollman
1932742SwollmanCTASSERT(sizeof(struct sigaltstack32) == 12);
19419878Swollman
1952742Swollmanint
19619878Swollmanfreebsd32_sigaltstack(struct thread *td,
1972742Swollman		      struct freebsd32_sigaltstack_args *uap)
1982742Swollman{
19919878Swollman	struct sigaltstack32 s32;
20019878Swollman	struct sigaltstack ss, oss, *ssp;
2012742Swollman	int error;
20219878Swollman
20319878Swollman	if (uap->ss != NULL) {
2042742Swollman		error = copyin(uap->ss, &s32, sizeof(s32));
20530711Swollman		if (error)
20619878Swollman			return (error);
20719878Swollman		PTRIN_CP(s32, ss, ss_sp);
20819878Swollman		CP(s32, ss, ss_size);
20919878Swollman		CP(s32, ss, ss_flags);
21030711Swollman		ssp = &ss;
21143014Swollman	} else
21243543Swollman		ssp = NULL;
21358787Sru	error = kern_sigaltstack(td, ssp, &oss);
214163302Sru	if (error == 0 && uap->oss != NULL) {
215163302Sru		PTROUT_CP(oss, s32, ss_sp);
216163302Sru		CP(oss, s32, ss_size);
217163302Sru		CP(oss, s32, ss_flags);
218163302Sru		error = copyout(&s32, uap->oss, sizeof(s32));
219163302Sru	}
220163302Sru	return (error);
221163302Sru}
22220094Swollman
2232742Swollman/*
2242742Swollman * Custom version of exec_copyin_args() so that we can translate
22519878Swollman * the pointers.
2262742Swollman */
2272742Swollmanstatic int
2282742Swollmanfreebsd32_exec_copyin_args(struct image_args *args, char *fname,
2292742Swollman    enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
23030711Swollman{
23130711Swollman	char *argp, *envp;
2322742Swollman	u_int32_t *p32, arg;
2332742Swollman	size_t length;
2342742Swollman	int error;
2352742Swollman
23619878Swollman	bzero(args, sizeof(*args));
23730711Swollman	if (argv == NULL)
2382742Swollman		return (EFAULT);
2392742Swollman
2402742Swollman	/*
241158421Swollman	 * Allocate temporary demand zeroed space for argument and
242158421Swollman	 *	environment strings
243158421Swollman	 */
24430711Swollman	args->buf = (char *) kmem_alloc_wait(exec_map,
2452742Swollman	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
2462742Swollman	if (args->buf == NULL)
24730711Swollman		return (ENOMEM);
2482742Swollman	args->begin_argv = args->buf;
2492742Swollman	args->endp = args->begin_argv;
2502742Swollman	args->stringspace = ARG_MAX;
2512742Swollman
2522742Swollman	args->fname = args->buf + ARG_MAX;
25330711Swollman
2542742Swollman	/*
2552742Swollman	 * Copy the file name.
2562742Swollman	 */
2572742Swollman	error = (segflg == UIO_SYSSPACE) ?
25819878Swollman	    copystr(fname, args->fname, PATH_MAX, &length) :
25930711Swollman	    copyinstr(fname, args->fname, PATH_MAX, &length);
26030711Swollman	if (error != 0)
2612742Swollman		goto err_exit;
2622742Swollman
2632742Swollman	/*
264158421Swollman	 * extract arguments first
265158421Swollman	 */
26630711Swollman	p32 = argv;
26730711Swollman	for (;;) {
2682742Swollman		error = copyin(p32++, &arg, sizeof(arg));
2692742Swollman		if (error)
2702742Swollman			goto err_exit;
2712742Swollman		if (arg == 0)
2722742Swollman			break;
2732742Swollman		argp = PTRIN(arg);
2742742Swollman		error = copyinstr(argp, args->endp, args->stringspace, &length);
27530711Swollman		if (error) {
27630711Swollman			if (error == ENAMETOOLONG)
27730711Swollman				error = E2BIG;
2782742Swollman			goto err_exit;
2792742Swollman		}
2802742Swollman		args->stringspace -= length;
2812742Swollman		args->endp += length;
28230711Swollman		args->argc++;
28330711Swollman	}
2842742Swollman
2852742Swollman	args->begin_envv = args->endp;
2862742Swollman
2872742Swollman	/*
2882742Swollman	 * extract environment strings
28930711Swollman	 */
29086222Swollman	if (envv) {
2912742Swollman		p32 = envv;
2922742Swollman		for (;;) {
2932742Swollman			error = copyin(p32++, &arg, sizeof(arg));
2942742Swollman			if (error)
2952742Swollman				goto err_exit;
29630711Swollman			if (arg == 0)
2972742Swollman				break;
29830711Swollman			envp = PTRIN(arg);
2992742Swollman			error = copyinstr(envp, args->endp, args->stringspace,
3002742Swollman			    &length);
301158421Swollman			if (error) {
3022742Swollman				if (error == ENAMETOOLONG)
30386222Swollman					error = E2BIG;
30414343Swollman				goto err_exit;
305158421Swollman			}
306158421Swollman			args->stringspace -= length;
307158421Swollman			args->endp += length;
308158421Swollman			args->envc++;
3092742Swollman		}
3102742Swollman	}
3112742Swollman
31219878Swollman	return (0);
31330711Swollman
3142742Swollmanerr_exit:
3152742Swollman	kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
3162742Swollman	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
3172742Swollman	args->buf = NULL;
3182742Swollman	return (error);
3192742Swollman}
32019878Swollman
3212742Swollmanint
32219878Swollmanfreebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
3232742Swollman{
32419878Swollman	struct image_args eargs;
3252742Swollman	int error;
32619878Swollman
3272742Swollman	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
32819878Swollman	    uap->argv, uap->envv);
32919878Swollman	if (error == 0)
3302742Swollman		error = kern_execve(td, &eargs, NULL);
33119878Swollman	return (error);
332149514Swollman}
3332742Swollman
3342742Swollman#ifdef __ia64__
33519878Swollmanstatic int
3362742Swollmanfreebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
33758787Sru		       int prot, int fd, off_t pos)
338158421Swollman{
33958787Sru	vm_map_t map;
34058787Sru	vm_map_entry_t entry;
34158787Sru	int rv;
34258787Sru
34343014Swollman	map = &td->td_proc->p_vmspace->vm_map;
3442742Swollman	if (fd != -1)
3452742Swollman		prot |= VM_PROT_WRITE;
3462742Swollman
3472742Swollman	if (vm_map_lookup_entry(map, start, &entry)) {
3482742Swollman		if ((entry->protection & prot) != prot) {
3492742Swollman			rv = vm_map_protect(map,
3502742Swollman					    trunc_page(start),
3512742Swollman					    round_page(end),
3522742Swollman					    entry->protection | prot,
3532742Swollman					    FALSE);
3542742Swollman			if (rv != KERN_SUCCESS)
35530711Swollman				return (EINVAL);
3562742Swollman		}
3572742Swollman	} else {
3582742Swollman		vm_offset_t addr = trunc_page(start);
3592742Swollman		rv = vm_map_find(map, 0, 0,
36030711Swollman				 &addr, PAGE_SIZE, FALSE, prot,
36130711Swollman				 VM_PROT_ALL, 0);
36230711Swollman		if (rv != KERN_SUCCESS)
3632742Swollman			return (EINVAL);
3642742Swollman	}
3652742Swollman
3662742Swollman	if (fd != -1) {
36730711Swollman		struct pread_args r;
36830711Swollman		r.fd = fd;
36930711Swollman		r.buf = (void *) start;
3702742Swollman		r.nbyte = end - start;
3712742Swollman		r.offset = pos;
3722742Swollman		return (pread(td, &r));
3732742Swollman	} else {
37419878Swollman		while (start < end) {
3752742Swollman			subyte((void *) start, 0);
3762742Swollman			start++;
3772742Swollman		}
3782742Swollman		return (0);
3792742Swollman	}
38014343Swollman}
3812742Swollman#endif
3822742Swollman
3832742Swollmanint
38430711Swollmanfreebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
38520094Swollman{
38619878Swollman	struct mmap_args ap;
3872742Swollman	vm_offset_t addr = (vm_offset_t) uap->addr;
38819878Swollman	vm_size_t len	 = uap->len;
3892742Swollman	int prot	 = uap->prot;
39019878Swollman	int flags	 = uap->flags;
3912742Swollman	int fd		 = uap->fd;
39219878Swollman	off_t pos	 = (uap->poslo
3932742Swollman			    | ((off_t)uap->poshi << 32));
39419878Swollman#ifdef __ia64__
3952742Swollman	vm_size_t pageoff;
39619878Swollman	int error;
3972742Swollman
3982742Swollman	/*
39919878Swollman	 * Attempt to handle page size hassles.
4002742Swollman	 */
4012742Swollman	pageoff = (pos & PAGE_MASK);
4022742Swollman	if (flags & MAP_FIXED) {
40319878Swollman		vm_offset_t start, end;
40419878Swollman		start = addr;
4052742Swollman		end = addr + len;
40619878Swollman
40719878Swollman		if (start != trunc_page(start)) {
40830711Swollman			error = freebsd32_mmap_partial(td, start,
40919878Swollman						       round_page(start), prot,
4108029Swollman						       fd, pos);
4112742Swollman			if (fd != -1)
4122742Swollman				pos += round_page(start) - start;
4132742Swollman			start = round_page(start);
41430711Swollman		}
4152742Swollman		if (end != round_page(end)) {
4162742Swollman			vm_offset_t t = trunc_page(end);
417158421Swollman			error = freebsd32_mmap_partial(td, t, end,
418158421Swollman						  prot, fd,
41920094Swollman						  pos + t - start);
42020094Swollman			end = trunc_page(end);
42120094Swollman		}
4222742Swollman		if (end > start && fd != -1 && (pos & PAGE_MASK)) {
4232742Swollman			/*
4242742Swollman			 * We can't map this region at all. The specified
42530711Swollman			 * address doesn't have the same alignment as the file
4262742Swollman			 * position. Fake the mapping by simply reading the
42730711Swollman			 * entire region into memory. First we need to make
42830711Swollman			 * sure the region exists.
42930711Swollman			 */
4302742Swollman			vm_map_t map;
4312742Swollman			struct pread_args r;
4322742Swollman			int rv;
43330711Swollman
43430711Swollman			prot |= VM_PROT_WRITE;
43530711Swollman			map = &td->td_proc->p_vmspace->vm_map;
43630711Swollman			rv = vm_map_remove(map, start, end);
4372742Swollman			if (rv != KERN_SUCCESS)
4382742Swollman				return (EINVAL);
4392742Swollman			rv = vm_map_find(map, 0, 0,
4402742Swollman					 &start, end - start, FALSE,
44130711Swollman					 prot, VM_PROT_ALL, 0);
4422742Swollman			if (rv != KERN_SUCCESS)
4432742Swollman				return (EINVAL);
4442742Swollman			r.fd = fd;
44514343Swollman			r.buf = (void *) start;
44619878Swollman			r.nbyte = end - start;
44743014Swollman			r.offset = pos;
44843014Swollman			error = pread(td, &r);
44943014Swollman			if (error)
45058787Sru				return (error);
45158787Sru
45258787Sru			td->td_retval[0] = addr;
45343014Swollman			return (0);
45443014Swollman		}
45543014Swollman		if (end == start) {
45643014Swollman			/*
45743014Swollman			 * After dealing with the ragged ends, there
45843014Swollman			 * might be none left.
4592742Swollman			 */
4602742Swollman			td->td_retval[0] = addr;
4612742Swollman			return (0);
4622742Swollman		}
46330711Swollman		addr = start;
4642742Swollman		len = end - start;
4652742Swollman	}
4662742Swollman#endif
4672742Swollman
46830711Swollman	ap.addr = (void *) addr;
4692742Swollman	ap.len = len;
47014343Swollman	ap.prot = prot;
47130711Swollman	ap.flags = flags;
47230711Swollman	ap.fd = fd;
47358787Sru	ap.pos = pos;
47458787Sru
47530711Swollman	return (mmap(td, &ap));
47614343Swollman}
4772742Swollman
4782742Swollman#ifdef COMPAT_FREEBSD6
4792742Swollmanint
48019878Swollmanfreebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
48130711Swollman{
4822742Swollman	struct freebsd32_mmap_args ap;
4832742Swollman
4842742Swollman	ap.addr = uap->addr;
4852742Swollman	ap.len = uap->len;
48630711Swollman	ap.prot = uap->prot;
48730711Swollman	ap.flags = uap->flags;
4882742Swollman	ap.fd = uap->fd;
4892742Swollman	ap.poslo = uap->poslo;
4902742Swollman	ap.poshi = uap->poshi;
4912742Swollman
49219878Swollman	return (freebsd32_mmap(td, &ap));
49386222Swollman}
49486222Swollman#endif
49586222Swollman
49686222Swollmanstruct itimerval32 {
49786222Swollman	struct timeval32 it_interval;
49886222Swollman	struct timeval32 it_value;
4992742Swollman};
5002742Swollman
5012742SwollmanCTASSERT(sizeof(struct itimerval32) == 16);
502158421Swollman
50330711Swollmanint
50430711Swollmanfreebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
50530711Swollman{
50630711Swollman	struct itimerval itv, oitv, *itvp;
5072742Swollman	struct itimerval32 i32;
5082742Swollman	int error;
50919878Swollman
51030711Swollman	if (uap->itv != NULL) {
51130711Swollman		error = copyin(uap->itv, &i32, sizeof(i32));
5122742Swollman		if (error)
5132742Swollman			return (error);
5142742Swollman		TV_CP(i32, itv, it_interval);
5152742Swollman		TV_CP(i32, itv, it_value);
5162742Swollman		itvp = &itv;
51730711Swollman	} else
5182742Swollman		itvp = NULL;
5192742Swollman	error = kern_setitimer(td, uap->which, itvp, &oitv);
5202742Swollman	if (error || uap->oitv == NULL)
5212742Swollman		return (error);
52230711Swollman	TV_CP(oitv, i32, it_interval);
5232742Swollman	TV_CP(oitv, i32, it_value);
5242742Swollman	return (copyout(&i32, uap->oitv, sizeof(i32)));
5252742Swollman}
52630711Swollman
52730711Swollmanint
52830711Swollmanfreebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
52958787Sru{
5302742Swollman	struct itimerval itv;
5312742Swollman	struct itimerval32 i32;
5322742Swollman	int error;
53358787Sru
53458787Sru	error = kern_getitimer(td, uap->which, &itv);
53558787Sru	if (error || uap->itv == NULL)
53658787Sru		return (error);
53758787Sru	TV_CP(itv, i32, it_interval);
53858787Sru	TV_CP(itv, i32, it_value);
53958787Sru	return (copyout(&i32, uap->itv, sizeof(i32)));
54058787Sru}
5412742Swollman
54219878Swollmanint
54314343Swollmanfreebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
54419878Swollman{
54519878Swollman	struct timeval32 tv32;
5462742Swollman	struct timeval tv, *tvp;
5472742Swollman	int error;
54858787Sru
54958787Sru	if (uap->tv != NULL) {
5502742Swollman		error = copyin(uap->tv, &tv32, sizeof(tv32));
5512742Swollman		if (error)
5522742Swollman			return (error);
5532742Swollman		CP(tv32, tv, tv_sec);
55430711Swollman		CP(tv32, tv, tv_usec);
5552742Swollman		tvp = &tv;
5562742Swollman	} else
5572742Swollman		tvp = NULL;
5582742Swollman	/*
5592742Swollman	 * XXX big-endian needs to convert the fd_sets too.
56086222Swollman	 * XXX Do pointers need PTRIN()?
5612742Swollman	 */
5622742Swollman	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
5632742Swollman}
5642742Swollman
5652742Swollmanstruct kevent32 {
56630711Swollman	u_int32_t	ident;		/* identifier for this event */
5672742Swollman	short		filter;		/* filter for event */
5682742Swollman	u_short		flags;
569149514Swollman	u_int		fflags;
570149514Swollman	int32_t		data;
571149514Swollman	u_int32_t	udata;		/* opaque user data identifier */
572149514Swollman};
573149514Swollman
574149514SwollmanCTASSERT(sizeof(struct kevent32) == 20);
575149514Swollmanstatic int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
576149514Swollmanstatic int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
577149514Swollman
578149514Swollman/*
579149514Swollman * Copy 'count' items into the destination list pointed to by uap->eventlist.
580149514Swollman */
581149514Swollmanstatic int
582149514Swollmanfreebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
583149514Swollman{
584158421Swollman	struct freebsd32_kevent_args *uap;
585158421Swollman	struct kevent32	ks32[KQ_NEVENTS];
586158421Swollman	int i, error = 0;
587158421Swollman
588158421Swollman	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
589158421Swollman	uap = (struct freebsd32_kevent_args *)arg;
590149514Swollman
5912742Swollman	for (i = 0; i < count; i++) {
59219878Swollman		CP(kevp[i], ks32[i], ident);
5932742Swollman		CP(kevp[i], ks32[i], filter);
59419878Swollman		CP(kevp[i], ks32[i], flags);
5952742Swollman		CP(kevp[i], ks32[i], fflags);
59619878Swollman		CP(kevp[i], ks32[i], data);
5972742Swollman		PTROUT_CP(kevp[i], ks32[i], udata);
59819878Swollman	}
5992742Swollman	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
60019878Swollman	if (error == 0)
6012742Swollman		uap->eventlist += count;
60219878Swollman	return (error);
6032742Swollman}
6042742Swollman
60519878Swollman/*
6062742Swollman * Copy 'count' items from the list pointed to by uap->changelist.
60719878Swollman */
6082742Swollmanstatic int
60919878Swollmanfreebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
61020094Swollman{
61119878Swollman	struct freebsd32_kevent_args *uap;
61219878Swollman	struct kevent32	ks32[KQ_NEVENTS];
613149514Swollman	int i, error = 0;
614149514Swollman
615158421Swollman	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
616158421Swollman	uap = (struct freebsd32_kevent_args *)arg;
617158421Swollman
618158421Swollman	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
619158421Swollman	if (error)
6202742Swollman		goto done;
6212742Swollman	uap->changelist += count;
62214343Swollman
62319878Swollman	for (i = 0; i < count; i++) {
6242742Swollman		CP(ks32[i], kevp[i], ident);
6252742Swollman		CP(ks32[i], kevp[i], filter);
6262742Swollman		CP(ks32[i], kevp[i], flags);
6272742Swollman		CP(ks32[i], kevp[i], fflags);
6282742Swollman		CP(ks32[i], kevp[i], data);
6292742Swollman		PTRIN_CP(ks32[i], kevp[i], udata);
63086222Swollman	}
6312742Swollmandone:
6322742Swollman	return (error);
6332742Swollman}
6342742Swollman
6352742Swollmanint
63630711Swollmanfreebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
6372742Swollman{
6382742Swollman	struct timespec32 ts32;
6392742Swollman	struct timespec ts, *tsp;
6402742Swollman	struct kevent_copyops k_ops = { uap,
64130711Swollman					freebsd32_kevent_copyout,
642					freebsd32_kevent_copyin};
643	int error;
644
645
646	if (uap->timeout) {
647		error = copyin(uap->timeout, &ts32, sizeof(ts32));
648		if (error)
649			return (error);
650		CP(ts32, ts, tv_sec);
651		CP(ts32, ts, tv_nsec);
652		tsp = &ts;
653	} else
654		tsp = NULL;
655	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
656	    &k_ops, tsp);
657	return (error);
658}
659
660int
661freebsd32_gettimeofday(struct thread *td,
662		       struct freebsd32_gettimeofday_args *uap)
663{
664	struct timeval atv;
665	struct timeval32 atv32;
666	struct timezone rtz;
667	int error = 0;
668
669	if (uap->tp) {
670		microtime(&atv);
671		CP(atv, atv32, tv_sec);
672		CP(atv, atv32, tv_usec);
673		error = copyout(&atv32, uap->tp, sizeof (atv32));
674	}
675	if (error == 0 && uap->tzp != NULL) {
676		rtz.tz_minuteswest = tz_minuteswest;
677		rtz.tz_dsttime = tz_dsttime;
678		error = copyout(&rtz, uap->tzp, sizeof (rtz));
679	}
680	return (error);
681}
682
683int
684freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
685{
686	struct rusage32 s32;
687	struct rusage s;
688	int error;
689
690	error = kern_getrusage(td, uap->who, &s);
691	if (error)
692		return (error);
693	if (uap->rusage != NULL) {
694		TV_CP(s, s32, ru_utime);
695		TV_CP(s, s32, ru_stime);
696		CP(s, s32, ru_maxrss);
697		CP(s, s32, ru_ixrss);
698		CP(s, s32, ru_idrss);
699		CP(s, s32, ru_isrss);
700		CP(s, s32, ru_minflt);
701		CP(s, s32, ru_majflt);
702		CP(s, s32, ru_nswap);
703		CP(s, s32, ru_inblock);
704		CP(s, s32, ru_oublock);
705		CP(s, s32, ru_msgsnd);
706		CP(s, s32, ru_msgrcv);
707		CP(s, s32, ru_nsignals);
708		CP(s, s32, ru_nvcsw);
709		CP(s, s32, ru_nivcsw);
710		error = copyout(&s32, uap->rusage, sizeof(s32));
711	}
712	return (error);
713}
714
715struct iovec32 {
716	u_int32_t iov_base;
717	int	iov_len;
718};
719
720CTASSERT(sizeof(struct iovec32) == 8);
721
722static int
723freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
724{
725	struct iovec32 iov32;
726	struct iovec *iov;
727	struct uio *uio;
728	u_int iovlen;
729	int error, i;
730
731	*uiop = NULL;
732	if (iovcnt > UIO_MAXIOV)
733		return (EINVAL);
734	iovlen = iovcnt * sizeof(struct iovec);
735	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
736	iov = (struct iovec *)(uio + 1);
737	for (i = 0; i < iovcnt; i++) {
738		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
739		if (error) {
740			free(uio, M_IOV);
741			return (error);
742		}
743		iov[i].iov_base = PTRIN(iov32.iov_base);
744		iov[i].iov_len = iov32.iov_len;
745	}
746	uio->uio_iov = iov;
747	uio->uio_iovcnt = iovcnt;
748	uio->uio_segflg = UIO_USERSPACE;
749	uio->uio_offset = -1;
750	uio->uio_resid = 0;
751	for (i = 0; i < iovcnt; i++) {
752		if (iov->iov_len > INT_MAX - uio->uio_resid) {
753			free(uio, M_IOV);
754			return (EINVAL);
755		}
756		uio->uio_resid += iov->iov_len;
757		iov++;
758	}
759	*uiop = uio;
760	return (0);
761}
762
763int
764freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
765{
766	struct uio *auio;
767	int error;
768
769	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
770	if (error)
771		return (error);
772	error = kern_readv(td, uap->fd, auio);
773	free(auio, M_IOV);
774	return (error);
775}
776
777int
778freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
779{
780	struct uio *auio;
781	int error;
782
783	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
784	if (error)
785		return (error);
786	error = kern_writev(td, uap->fd, auio);
787	free(auio, M_IOV);
788	return (error);
789}
790
791int
792freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
793{
794	struct uio *auio;
795	int error;
796
797	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
798	if (error)
799		return (error);
800	error = kern_preadv(td, uap->fd, auio, uap->offset);
801	free(auio, M_IOV);
802	return (error);
803}
804
805int
806freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
807{
808	struct uio *auio;
809	int error;
810
811	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
812	if (error)
813		return (error);
814	error = kern_pwritev(td, uap->fd, auio, uap->offset);
815	free(auio, M_IOV);
816	return (error);
817}
818
819static int
820freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
821    int error)
822{
823	struct iovec32 iov32;
824	struct iovec *iov;
825	u_int iovlen;
826	int i;
827
828	*iovp = NULL;
829	if (iovcnt > UIO_MAXIOV)
830		return (error);
831	iovlen = iovcnt * sizeof(struct iovec);
832	iov = malloc(iovlen, M_IOV, M_WAITOK);
833	for (i = 0; i < iovcnt; i++) {
834		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
835		if (error) {
836			free(iov, M_IOV);
837			return (error);
838		}
839		iov[i].iov_base = PTRIN(iov32.iov_base);
840		iov[i].iov_len = iov32.iov_len;
841	}
842	*iovp = iov;
843	return (0);
844}
845
846struct msghdr32 {
847	u_int32_t	 msg_name;
848	socklen_t	 msg_namelen;
849	u_int32_t	 msg_iov;
850	int		 msg_iovlen;
851	u_int32_t	 msg_control;
852	socklen_t	 msg_controllen;
853	int		 msg_flags;
854};
855CTASSERT(sizeof(struct msghdr32) == 28);
856
857static int
858freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
859{
860	struct msghdr32 m32;
861	int error;
862
863	error = copyin(msg32, &m32, sizeof(m32));
864	if (error)
865		return (error);
866	msg->msg_name = PTRIN(m32.msg_name);
867	msg->msg_namelen = m32.msg_namelen;
868	msg->msg_iov = PTRIN(m32.msg_iov);
869	msg->msg_iovlen = m32.msg_iovlen;
870	msg->msg_control = PTRIN(m32.msg_control);
871	msg->msg_controllen = m32.msg_controllen;
872	msg->msg_flags = m32.msg_flags;
873	return (0);
874}
875
876static int
877freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
878{
879	struct msghdr32 m32;
880	int error;
881
882	m32.msg_name = PTROUT(msg->msg_name);
883	m32.msg_namelen = msg->msg_namelen;
884	m32.msg_iov = PTROUT(msg->msg_iov);
885	m32.msg_iovlen = msg->msg_iovlen;
886	m32.msg_control = PTROUT(msg->msg_control);
887	m32.msg_controllen = msg->msg_controllen;
888	m32.msg_flags = msg->msg_flags;
889	error = copyout(&m32, msg32, sizeof(m32));
890	return (error);
891}
892
893#define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
894#define FREEBSD32_ALIGN(p)	\
895	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
896#define	FREEBSD32_CMSG_SPACE(l)	\
897	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
898
899#define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
900				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
901static int
902freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
903{
904	struct cmsghdr *cm;
905	void *data;
906	socklen_t clen, datalen;
907	int error;
908	caddr_t ctlbuf;
909	int len, maxlen, copylen;
910	struct mbuf *m;
911	error = 0;
912
913	len    = msg->msg_controllen;
914	maxlen = msg->msg_controllen;
915	msg->msg_controllen = 0;
916
917	m = control;
918	ctlbuf = msg->msg_control;
919
920	while (m && len > 0) {
921		cm = mtod(m, struct cmsghdr *);
922		clen = m->m_len;
923
924		while (cm != NULL) {
925
926			if (sizeof(struct cmsghdr) > clen ||
927			    cm->cmsg_len > clen) {
928				error = EINVAL;
929				break;
930			}
931
932			data   = CMSG_DATA(cm);
933			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
934
935			/* Adjust message length */
936			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
937			    datalen;
938
939
940			/* Copy cmsghdr */
941			copylen = sizeof(struct cmsghdr);
942			if (len < copylen) {
943				msg->msg_flags |= MSG_CTRUNC;
944				copylen = len;
945			}
946
947			error = copyout(cm,ctlbuf,copylen);
948			if (error)
949				goto exit;
950
951			ctlbuf += FREEBSD32_ALIGN(copylen);
952			len    -= FREEBSD32_ALIGN(copylen);
953
954			if (len <= 0)
955				break;
956
957			/* Copy data */
958			copylen = datalen;
959			if (len < copylen) {
960				msg->msg_flags |= MSG_CTRUNC;
961				copylen = len;
962			}
963
964			error = copyout(data,ctlbuf,copylen);
965			if (error)
966				goto exit;
967
968			ctlbuf += FREEBSD32_ALIGN(copylen);
969			len    -= FREEBSD32_ALIGN(copylen);
970
971			if (CMSG_SPACE(datalen) < clen) {
972				clen -= CMSG_SPACE(datalen);
973				cm = (struct cmsghdr *)
974					((caddr_t)cm + CMSG_SPACE(datalen));
975			} else {
976				clen = 0;
977				cm = NULL;
978			}
979		}
980		m = m->m_next;
981	}
982
983	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
984
985exit:
986	return (error);
987
988}
989
990int
991freebsd32_recvmsg(td, uap)
992	struct thread *td;
993	struct freebsd32_recvmsg_args /* {
994		int	s;
995		struct	msghdr32 *msg;
996		int	flags;
997	} */ *uap;
998{
999	struct msghdr msg;
1000	struct msghdr32 m32;
1001	struct iovec *uiov, *iov;
1002	struct mbuf *control = NULL;
1003	struct mbuf **controlp;
1004
1005	int error;
1006	error = copyin(uap->msg, &m32, sizeof(m32));
1007	if (error)
1008		return (error);
1009	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1010	if (error)
1011		return (error);
1012	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1013	    EMSGSIZE);
1014	if (error)
1015		return (error);
1016	msg.msg_flags = uap->flags;
1017	uiov = msg.msg_iov;
1018	msg.msg_iov = iov;
1019
1020	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1021	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1022	if (error == 0) {
1023		msg.msg_iov = uiov;
1024
1025		if (control != NULL)
1026			error = freebsd32_copy_msg_out(&msg, control);
1027
1028		if (error == 0)
1029			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1030	}
1031	free(iov, M_IOV);
1032
1033	if (control != NULL)
1034		m_freem(control);
1035
1036	return (error);
1037}
1038
1039
1040static int
1041freebsd32_convert_msg_in(struct mbuf **controlp)
1042{
1043	struct mbuf *control = *controlp;
1044	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1045	void *data;
1046	socklen_t clen = control->m_len, datalen;
1047	int error;
1048
1049	error = 0;
1050	*controlp = NULL;
1051
1052	while (cm != NULL) {
1053		if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1054			error = EINVAL;
1055			break;
1056		}
1057
1058		data = FREEBSD32_CMSG_DATA(cm);
1059		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1060
1061		*controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1062		    cm->cmsg_level);
1063		controlp = &(*controlp)->m_next;
1064
1065		if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1066			clen -= FREEBSD32_CMSG_SPACE(datalen);
1067			cm = (struct cmsghdr *)
1068				((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1069		} else {
1070			clen = 0;
1071			cm = NULL;
1072		}
1073	}
1074
1075	m_freem(control);
1076	return (error);
1077}
1078
1079
1080int
1081freebsd32_sendmsg(struct thread *td,
1082		  struct freebsd32_sendmsg_args *uap)
1083{
1084	struct msghdr msg;
1085	struct msghdr32 m32;
1086	struct iovec *iov;
1087	struct mbuf *control = NULL;
1088	struct sockaddr *to = NULL;
1089	int error;
1090
1091	error = copyin(uap->msg, &m32, sizeof(m32));
1092	if (error)
1093		return (error);
1094	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1095	if (error)
1096		return (error);
1097	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1098	    EMSGSIZE);
1099	if (error)
1100		return (error);
1101	msg.msg_iov = iov;
1102	if (msg.msg_name != NULL) {
1103		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1104		if (error) {
1105			to = NULL;
1106			goto out;
1107		}
1108		msg.msg_name = to;
1109	}
1110
1111	if (msg.msg_control) {
1112		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1113			error = EINVAL;
1114			goto out;
1115		}
1116
1117		error = sockargs(&control, msg.msg_control,
1118		    msg.msg_controllen, MT_CONTROL);
1119		if (error)
1120			goto out;
1121
1122		error = freebsd32_convert_msg_in(&control);
1123		if (error)
1124			goto out;
1125	}
1126
1127	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1128	    UIO_USERSPACE);
1129
1130out:
1131	free(iov, M_IOV);
1132	if (to)
1133		free(to, M_SONAME);
1134	return (error);
1135}
1136
1137int
1138freebsd32_recvfrom(struct thread *td,
1139		   struct freebsd32_recvfrom_args *uap)
1140{
1141	struct msghdr msg;
1142	struct iovec aiov;
1143	int error;
1144
1145	if (uap->fromlenaddr) {
1146		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1147		    sizeof(msg.msg_namelen));
1148		if (error)
1149			return (error);
1150	} else {
1151		msg.msg_namelen = 0;
1152	}
1153
1154	msg.msg_name = PTRIN(uap->from);
1155	msg.msg_iov = &aiov;
1156	msg.msg_iovlen = 1;
1157	aiov.iov_base = PTRIN(uap->buf);
1158	aiov.iov_len = uap->len;
1159	msg.msg_control = NULL;
1160	msg.msg_flags = uap->flags;
1161	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1162	if (error == 0 && uap->fromlenaddr)
1163		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1164		    sizeof (msg.msg_namelen));
1165	return (error);
1166}
1167
1168int
1169freebsd32_settimeofday(struct thread *td,
1170		       struct freebsd32_settimeofday_args *uap)
1171{
1172	struct timeval32 tv32;
1173	struct timeval tv, *tvp;
1174	struct timezone tz, *tzp;
1175	int error;
1176
1177	if (uap->tv) {
1178		error = copyin(uap->tv, &tv32, sizeof(tv32));
1179		if (error)
1180			return (error);
1181		CP(tv32, tv, tv_sec);
1182		CP(tv32, tv, tv_usec);
1183		tvp = &tv;
1184	} else
1185		tvp = NULL;
1186	if (uap->tzp) {
1187		error = copyin(uap->tzp, &tz, sizeof(tz));
1188		if (error)
1189			return (error);
1190		tzp = &tz;
1191	} else
1192		tzp = NULL;
1193	return (kern_settimeofday(td, tvp, tzp));
1194}
1195
1196int
1197freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1198{
1199	struct timeval32 s32[2];
1200	struct timeval s[2], *sp;
1201	int error;
1202
1203	if (uap->tptr != NULL) {
1204		error = copyin(uap->tptr, s32, sizeof(s32));
1205		if (error)
1206			return (error);
1207		CP(s32[0], s[0], tv_sec);
1208		CP(s32[0], s[0], tv_usec);
1209		CP(s32[1], s[1], tv_sec);
1210		CP(s32[1], s[1], tv_usec);
1211		sp = s;
1212	} else
1213		sp = NULL;
1214	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1215}
1216
1217int
1218freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1219{
1220	struct timeval32 s32[2];
1221	struct timeval s[2], *sp;
1222	int error;
1223
1224	if (uap->tptr != NULL) {
1225		error = copyin(uap->tptr, s32, sizeof(s32));
1226		if (error)
1227			return (error);
1228		CP(s32[0], s[0], tv_sec);
1229		CP(s32[0], s[0], tv_usec);
1230		CP(s32[1], s[1], tv_sec);
1231		CP(s32[1], s[1], tv_usec);
1232		sp = s;
1233	} else
1234		sp = NULL;
1235	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1236}
1237
1238int
1239freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1240{
1241	struct timeval32 s32[2];
1242	struct timeval s[2], *sp;
1243	int error;
1244
1245	if (uap->tptr != NULL) {
1246		error = copyin(uap->tptr, s32, sizeof(s32));
1247		if (error)
1248			return (error);
1249		CP(s32[0], s[0], tv_sec);
1250		CP(s32[0], s[0], tv_usec);
1251		CP(s32[1], s[1], tv_sec);
1252		CP(s32[1], s[1], tv_usec);
1253		sp = s;
1254	} else
1255		sp = NULL;
1256	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1257}
1258
1259
1260int
1261freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1262{
1263	struct timeval32 tv32;
1264	struct timeval delta, olddelta, *deltap;
1265	int error;
1266
1267	if (uap->delta) {
1268		error = copyin(uap->delta, &tv32, sizeof(tv32));
1269		if (error)
1270			return (error);
1271		CP(tv32, delta, tv_sec);
1272		CP(tv32, delta, tv_usec);
1273		deltap = &delta;
1274	} else
1275		deltap = NULL;
1276	error = kern_adjtime(td, deltap, &olddelta);
1277	if (uap->olddelta && error == 0) {
1278		CP(olddelta, tv32, tv_sec);
1279		CP(olddelta, tv32, tv_usec);
1280		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1281	}
1282	return (error);
1283}
1284
1285#ifdef COMPAT_FREEBSD4
1286int
1287freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1288{
1289	struct statfs32 s32;
1290	struct statfs s;
1291	int error;
1292
1293	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1294	if (error)
1295		return (error);
1296	copy_statfs(&s, &s32);
1297	return (copyout(&s32, uap->buf, sizeof(s32)));
1298}
1299#endif
1300
1301#ifdef COMPAT_FREEBSD4
1302int
1303freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1304{
1305	struct statfs32 s32;
1306	struct statfs s;
1307	int error;
1308
1309	error = kern_fstatfs(td, uap->fd, &s);
1310	if (error)
1311		return (error);
1312	copy_statfs(&s, &s32);
1313	return (copyout(&s32, uap->buf, sizeof(s32)));
1314}
1315#endif
1316
1317#ifdef COMPAT_FREEBSD4
1318int
1319freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1320{
1321	struct statfs32 s32;
1322	struct statfs s;
1323	fhandle_t fh;
1324	int error;
1325
1326	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1327		return (error);
1328	error = kern_fhstatfs(td, fh, &s);
1329	if (error)
1330		return (error);
1331	copy_statfs(&s, &s32);
1332	return (copyout(&s32, uap->buf, sizeof(s32)));
1333}
1334#endif
1335
1336int
1337freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1338{
1339	/*
1340	 * Vector through to semsys if it is loaded.
1341	 */
1342	return sysent[SYS_semsys].sy_call(td, uap);
1343}
1344
1345int
1346freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1347{
1348	switch (uap->which) {
1349	case 2:
1350		return (freebsd32_msgsnd(td,
1351		    (struct freebsd32_msgsnd_args *)&uap->a2));
1352		break;
1353	case 3:
1354		return (freebsd32_msgrcv(td,
1355		    (struct freebsd32_msgrcv_args *)&uap->a2));
1356		break;
1357	default:
1358		/*
1359		 * Vector through to msgsys if it is loaded.
1360		 */
1361		return (sysent[SYS_msgsys].sy_call(td, uap));
1362		break;
1363	}
1364}
1365
1366int
1367freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1368{
1369	const void *msgp;
1370	long mtype;
1371	int32_t mtype32;
1372	int error;
1373
1374	msgp = PTRIN(uap->msgp);
1375	if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1376		return (error);
1377	mtype = mtype32;
1378	return (kern_msgsnd(td, uap->msqid,
1379	    (const char *)msgp + sizeof(mtype32),
1380	    uap->msgsz, uap->msgflg, mtype));
1381}
1382
1383int
1384freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1385{
1386	void *msgp;
1387	long mtype;
1388	int32_t mtype32;
1389	int error;
1390
1391	msgp = PTRIN(uap->msgp);
1392	if ((error = kern_msgrcv(td, uap->msqid,
1393	    (char *)msgp + sizeof(mtype32), uap->msgsz,
1394	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1395		return (error);
1396	mtype32 = (int32_t)mtype;
1397	return (copyout(&mtype32, msgp, sizeof(mtype32)));
1398}
1399
1400int
1401freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1402{
1403
1404	switch (uap->which) {
1405	case 0:	{	/* shmat */
1406		struct shmat_args ap;
1407
1408		ap.shmid = uap->a2;
1409		ap.shmaddr = PTRIN(uap->a3);
1410		ap.shmflg = uap->a4;
1411		return (sysent[SYS_shmat].sy_call(td, &ap));
1412	}
1413	case 2: {	/* shmdt */
1414		struct shmdt_args ap;
1415
1416		ap.shmaddr = PTRIN(uap->a2);
1417		return (sysent[SYS_shmdt].sy_call(td, &ap));
1418	}
1419	case 3: {	/* shmget */
1420		struct shmget_args ap;
1421
1422		ap.key = uap->a2;
1423		ap.size = uap->a3;
1424		ap.shmflg = uap->a4;
1425		return (sysent[SYS_shmget].sy_call(td, &ap));
1426	}
1427	case 4: {	/* shmctl */
1428		struct freebsd32_shmctl_args ap;
1429
1430		ap.shmid = uap->a2;
1431		ap.cmd = uap->a3;
1432		ap.buf = PTRIN(uap->a4);
1433		return (freebsd32_shmctl(td, &ap));
1434	}
1435	case 1:		/* oshmctl */
1436	default:
1437		return (EINVAL);
1438	}
1439}
1440
1441struct ipc_perm32 {
1442	uint16_t	cuid;
1443	uint16_t	cgid;
1444	uint16_t	uid;
1445	uint16_t	gid;
1446	uint16_t	mode;
1447	uint16_t	seq;
1448	uint32_t	key;
1449};
1450struct shmid_ds32 {
1451	struct ipc_perm32 shm_perm;
1452	int32_t		shm_segsz;
1453	int32_t		shm_lpid;
1454	int32_t		shm_cpid;
1455	int16_t		shm_nattch;
1456	int32_t		shm_atime;
1457	int32_t		shm_dtime;
1458	int32_t		shm_ctime;
1459	uint32_t	shm_internal;
1460};
1461struct shm_info32 {
1462	int32_t		used_ids;
1463	uint32_t	shm_tot;
1464	uint32_t	shm_rss;
1465	uint32_t	shm_swp;
1466	uint32_t	swap_attempts;
1467	uint32_t	swap_successes;
1468};
1469struct shminfo32 {
1470	uint32_t	shmmax;
1471	uint32_t	shmmin;
1472	uint32_t	shmmni;
1473	uint32_t	shmseg;
1474	uint32_t	shmall;
1475};
1476
1477int
1478freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1479{
1480	int error = 0;
1481	union {
1482		struct shmid_ds shmid_ds;
1483		struct shm_info shm_info;
1484		struct shminfo shminfo;
1485	} u;
1486	union {
1487		struct shmid_ds32 shmid_ds32;
1488		struct shm_info32 shm_info32;
1489		struct shminfo32 shminfo32;
1490	} u32;
1491	size_t sz;
1492
1493	if (uap->cmd == IPC_SET) {
1494		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1495		    sizeof(u32.shmid_ds32))))
1496			goto done;
1497		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.cuid);
1498		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.cgid);
1499		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.uid);
1500		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.gid);
1501		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.mode);
1502		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.seq);
1503		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.key);
1504		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1505		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1506		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1507		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1508		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1509		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1510		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1511		PTRIN_CP(u32.shmid_ds32, u.shmid_ds, shm_internal);
1512	}
1513
1514	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1515	if (error)
1516		goto done;
1517
1518	/* Cases in which we need to copyout */
1519	switch (uap->cmd) {
1520	case IPC_INFO:
1521		CP(u.shminfo, u32.shminfo32, shmmax);
1522		CP(u.shminfo, u32.shminfo32, shmmin);
1523		CP(u.shminfo, u32.shminfo32, shmmni);
1524		CP(u.shminfo, u32.shminfo32, shmseg);
1525		CP(u.shminfo, u32.shminfo32, shmall);
1526		error = copyout(&u32.shminfo32, uap->buf,
1527		    sizeof(u32.shminfo32));
1528		break;
1529	case SHM_INFO:
1530		CP(u.shm_info, u32.shm_info32, used_ids);
1531		CP(u.shm_info, u32.shm_info32, shm_rss);
1532		CP(u.shm_info, u32.shm_info32, shm_tot);
1533		CP(u.shm_info, u32.shm_info32, shm_swp);
1534		CP(u.shm_info, u32.shm_info32, swap_attempts);
1535		CP(u.shm_info, u32.shm_info32, swap_successes);
1536		error = copyout(&u32.shm_info32, uap->buf,
1537		    sizeof(u32.shm_info32));
1538		break;
1539	case SHM_STAT:
1540	case IPC_STAT:
1541		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.cuid);
1542		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.cgid);
1543		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.uid);
1544		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.gid);
1545		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.mode);
1546		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.seq);
1547		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.key);
1548		CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1549		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1550		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1551		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1552		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1553		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1554		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1555		PTROUT_CP(u.shmid_ds, u32.shmid_ds32, shm_internal);
1556		error = copyout(&u32.shmid_ds32, uap->buf,
1557		    sizeof(u32.shmid_ds32));
1558		break;
1559	}
1560
1561done:
1562	if (error) {
1563		/* Invalidate the return value */
1564		td->td_retval[0] = -1;
1565	}
1566	return (error);
1567}
1568
1569int
1570freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1571{
1572	struct pread_args ap;
1573
1574	ap.fd = uap->fd;
1575	ap.buf = uap->buf;
1576	ap.nbyte = uap->nbyte;
1577	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1578	return (pread(td, &ap));
1579}
1580
1581int
1582freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1583{
1584	struct pwrite_args ap;
1585
1586	ap.fd = uap->fd;
1587	ap.buf = uap->buf;
1588	ap.nbyte = uap->nbyte;
1589	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1590	return (pwrite(td, &ap));
1591}
1592
1593int
1594freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1595{
1596	int error;
1597	struct lseek_args ap;
1598	off_t pos;
1599
1600	ap.fd = uap->fd;
1601	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1602	ap.whence = uap->whence;
1603	error = lseek(td, &ap);
1604	/* Expand the quad return into two parts for eax and edx */
1605	pos = *(off_t *)(td->td_retval);
1606	td->td_retval[0] = pos & 0xffffffff;	/* %eax */
1607	td->td_retval[1] = pos >> 32;		/* %edx */
1608	return error;
1609}
1610
1611int
1612freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1613{
1614	struct truncate_args ap;
1615
1616	ap.path = uap->path;
1617	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1618	return (truncate(td, &ap));
1619}
1620
1621int
1622freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1623{
1624	struct ftruncate_args ap;
1625
1626	ap.fd = uap->fd;
1627	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1628	return (ftruncate(td, &ap));
1629}
1630
1631#ifdef COMPAT_FREEBSD6
1632/* versions with the 'int pad' argument */
1633int
1634freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1635{
1636	struct pread_args ap;
1637
1638	ap.fd = uap->fd;
1639	ap.buf = uap->buf;
1640	ap.nbyte = uap->nbyte;
1641	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1642	return (pread(td, &ap));
1643}
1644
1645int
1646freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1647{
1648	struct pwrite_args ap;
1649
1650	ap.fd = uap->fd;
1651	ap.buf = uap->buf;
1652	ap.nbyte = uap->nbyte;
1653	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1654	return (pwrite(td, &ap));
1655}
1656
1657int
1658freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1659{
1660	int error;
1661	struct lseek_args ap;
1662	off_t pos;
1663
1664	ap.fd = uap->fd;
1665	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1666	ap.whence = uap->whence;
1667	error = lseek(td, &ap);
1668	/* Expand the quad return into two parts for eax and edx */
1669	pos = *(off_t *)(td->td_retval);
1670	td->td_retval[0] = pos & 0xffffffff;	/* %eax */
1671	td->td_retval[1] = pos >> 32;		/* %edx */
1672	return error;
1673}
1674
1675int
1676freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1677{
1678	struct truncate_args ap;
1679
1680	ap.path = uap->path;
1681	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1682	return (truncate(td, &ap));
1683}
1684
1685int
1686freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1687{
1688	struct ftruncate_args ap;
1689
1690	ap.fd = uap->fd;
1691	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1692	return (ftruncate(td, &ap));
1693}
1694#endif /* COMPAT_FREEBSD6 */
1695
1696struct sf_hdtr32 {
1697	uint32_t headers;
1698	int hdr_cnt;
1699	uint32_t trailers;
1700	int trl_cnt;
1701};
1702
1703static int
1704freebsd32_do_sendfile(struct thread *td,
1705    struct freebsd32_sendfile_args *uap, int compat)
1706{
1707	struct sendfile_args ap;
1708	struct sf_hdtr32 hdtr32;
1709	struct sf_hdtr hdtr;
1710	struct uio *hdr_uio, *trl_uio;
1711	struct iovec32 *iov32;
1712	int error;
1713
1714	hdr_uio = trl_uio = NULL;
1715
1716	ap.fd = uap->fd;
1717	ap.s = uap->s;
1718	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1719	ap.nbytes = uap->nbytes;
1720	ap.hdtr = (struct sf_hdtr *)uap->hdtr;		/* XXX not used */
1721	ap.sbytes = uap->sbytes;
1722	ap.flags = uap->flags;
1723
1724	if (uap->hdtr != NULL) {
1725		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1726		if (error)
1727			goto out;
1728		PTRIN_CP(hdtr32, hdtr, headers);
1729		CP(hdtr32, hdtr, hdr_cnt);
1730		PTRIN_CP(hdtr32, hdtr, trailers);
1731		CP(hdtr32, hdtr, trl_cnt);
1732
1733		if (hdtr.headers != NULL) {
1734			iov32 = PTRIN(hdtr32.headers);
1735			error = freebsd32_copyinuio(iov32,
1736			    hdtr32.hdr_cnt, &hdr_uio);
1737			if (error)
1738				goto out;
1739		}
1740		if (hdtr.trailers != NULL) {
1741			iov32 = PTRIN(hdtr32.trailers);
1742			error = freebsd32_copyinuio(iov32,
1743			    hdtr32.trl_cnt, &trl_uio);
1744			if (error)
1745				goto out;
1746		}
1747	}
1748
1749	error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1750out:
1751	if (hdr_uio)
1752		free(hdr_uio, M_IOV);
1753	if (trl_uio)
1754		free(trl_uio, M_IOV);
1755	return (error);
1756}
1757
1758#ifdef COMPAT_FREEBSD4
1759int
1760freebsd4_freebsd32_sendfile(struct thread *td,
1761    struct freebsd4_freebsd32_sendfile_args *uap)
1762{
1763	return (freebsd32_do_sendfile(td,
1764	    (struct freebsd32_sendfile_args *)uap, 1));
1765}
1766#endif
1767
1768int
1769freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1770{
1771
1772	return (freebsd32_do_sendfile(td, uap, 0));
1773}
1774
1775struct stat32 {
1776	dev_t	st_dev;
1777	ino_t	st_ino;
1778	mode_t	st_mode;
1779	nlink_t	st_nlink;
1780	uid_t	st_uid;
1781	gid_t	st_gid;
1782	dev_t	st_rdev;
1783	struct timespec32 st_atimespec;
1784	struct timespec32 st_mtimespec;
1785	struct timespec32 st_ctimespec;
1786	off_t	st_size;
1787	int64_t	st_blocks;
1788	u_int32_t st_blksize;
1789	u_int32_t st_flags;
1790	u_int32_t st_gen;
1791	struct timespec32 st_birthtimespec;
1792	unsigned int :(8 / 2) * (16 - (int)sizeof(struct timespec32));
1793	unsigned int :(8 / 2) * (16 - (int)sizeof(struct timespec32));
1794};
1795
1796
1797CTASSERT(sizeof(struct stat32) == 96);
1798
1799static void
1800copy_stat( struct stat *in, struct stat32 *out)
1801{
1802	CP(*in, *out, st_dev);
1803	CP(*in, *out, st_ino);
1804	CP(*in, *out, st_mode);
1805	CP(*in, *out, st_nlink);
1806	CP(*in, *out, st_uid);
1807	CP(*in, *out, st_gid);
1808	CP(*in, *out, st_rdev);
1809	TS_CP(*in, *out, st_atimespec);
1810	TS_CP(*in, *out, st_mtimespec);
1811	TS_CP(*in, *out, st_ctimespec);
1812	CP(*in, *out, st_size);
1813	CP(*in, *out, st_blocks);
1814	CP(*in, *out, st_blksize);
1815	CP(*in, *out, st_flags);
1816	CP(*in, *out, st_gen);
1817}
1818
1819int
1820freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1821{
1822	struct stat sb;
1823	struct stat32 sb32;
1824	int error;
1825
1826	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1827	if (error)
1828		return (error);
1829	copy_stat(&sb, &sb32);
1830	error = copyout(&sb32, uap->ub, sizeof (sb32));
1831	return (error);
1832}
1833
1834int
1835freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1836{
1837	struct stat ub;
1838	struct stat32 ub32;
1839	int error;
1840
1841	error = kern_fstat(td, uap->fd, &ub);
1842	if (error)
1843		return (error);
1844	copy_stat(&ub, &ub32);
1845	error = copyout(&ub32, uap->ub, sizeof(ub32));
1846	return (error);
1847}
1848
1849int
1850freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1851{
1852	struct stat sb;
1853	struct stat32 sb32;
1854	int error;
1855
1856	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1857	if (error)
1858		return (error);
1859	copy_stat(&sb, &sb32);
1860	error = copyout(&sb32, uap->ub, sizeof (sb32));
1861	return (error);
1862}
1863
1864/*
1865 * MPSAFE
1866 */
1867int
1868freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1869{
1870	int error, name[CTL_MAXNAME];
1871	size_t j, oldlen;
1872
1873	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1874		return (EINVAL);
1875 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1876 	if (error)
1877		return (error);
1878	mtx_lock(&Giant);
1879	if (uap->oldlenp)
1880		oldlen = fuword32(uap->oldlenp);
1881	else
1882		oldlen = 0;
1883	error = userland_sysctl(td, name, uap->namelen,
1884		uap->old, &oldlen, 1,
1885		uap->new, uap->newlen, &j, SCTL_MASK32);
1886	if (error && error != ENOMEM)
1887		goto done2;
1888	if (uap->oldlenp)
1889		suword32(uap->oldlenp, j);
1890done2:
1891	mtx_unlock(&Giant);
1892	return (error);
1893}
1894
1895struct sigaction32 {
1896	u_int32_t	sa_u;
1897	int		sa_flags;
1898	sigset_t	sa_mask;
1899};
1900
1901CTASSERT(sizeof(struct sigaction32) == 24);
1902
1903int
1904freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1905{
1906	struct sigaction32 s32;
1907	struct sigaction sa, osa, *sap;
1908	int error;
1909
1910	if (uap->act) {
1911		error = copyin(uap->act, &s32, sizeof(s32));
1912		if (error)
1913			return (error);
1914		sa.sa_handler = PTRIN(s32.sa_u);
1915		CP(s32, sa, sa_flags);
1916		CP(s32, sa, sa_mask);
1917		sap = &sa;
1918	} else
1919		sap = NULL;
1920	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1921	if (error == 0 && uap->oact != NULL) {
1922		s32.sa_u = PTROUT(osa.sa_handler);
1923		CP(osa, s32, sa_flags);
1924		CP(osa, s32, sa_mask);
1925		error = copyout(&s32, uap->oact, sizeof(s32));
1926	}
1927	return (error);
1928}
1929
1930#ifdef COMPAT_FREEBSD4
1931int
1932freebsd4_freebsd32_sigaction(struct thread *td,
1933			     struct freebsd4_freebsd32_sigaction_args *uap)
1934{
1935	struct sigaction32 s32;
1936	struct sigaction sa, osa, *sap;
1937	int error;
1938
1939	if (uap->act) {
1940		error = copyin(uap->act, &s32, sizeof(s32));
1941		if (error)
1942			return (error);
1943		sa.sa_handler = PTRIN(s32.sa_u);
1944		CP(s32, sa, sa_flags);
1945		CP(s32, sa, sa_mask);
1946		sap = &sa;
1947	} else
1948		sap = NULL;
1949	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
1950	if (error == 0 && uap->oact != NULL) {
1951		s32.sa_u = PTROUT(osa.sa_handler);
1952		CP(osa, s32, sa_flags);
1953		CP(osa, s32, sa_mask);
1954		error = copyout(&s32, uap->oact, sizeof(s32));
1955	}
1956	return (error);
1957}
1958#endif
1959
1960#ifdef COMPAT_43
1961struct osigaction32 {
1962	u_int32_t	sa_u;
1963	osigset_t	sa_mask;
1964	int		sa_flags;
1965};
1966
1967#define	ONSIG	32
1968
1969int
1970ofreebsd32_sigaction(struct thread *td,
1971			     struct ofreebsd32_sigaction_args *uap)
1972{
1973	struct osigaction32 s32;
1974	struct sigaction sa, osa, *sap;
1975	int error;
1976
1977	if (uap->signum <= 0 || uap->signum >= ONSIG)
1978		return (EINVAL);
1979
1980	if (uap->nsa) {
1981		error = copyin(uap->nsa, &s32, sizeof(s32));
1982		if (error)
1983			return (error);
1984		sa.sa_handler = PTRIN(s32.sa_u);
1985		CP(s32, sa, sa_flags);
1986		OSIG2SIG(s32.sa_mask, sa.sa_mask);
1987		sap = &sa;
1988	} else
1989		sap = NULL;
1990	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
1991	if (error == 0 && uap->osa != NULL) {
1992		s32.sa_u = PTROUT(osa.sa_handler);
1993		CP(osa, s32, sa_flags);
1994		SIG2OSIG(osa.sa_mask, s32.sa_mask);
1995		error = copyout(&s32, uap->osa, sizeof(s32));
1996	}
1997	return (error);
1998}
1999
2000int
2001ofreebsd32_sigprocmask(struct thread *td,
2002			       struct ofreebsd32_sigprocmask_args *uap)
2003{
2004	sigset_t set, oset;
2005	int error;
2006
2007	OSIG2SIG(uap->mask, set);
2008	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
2009	SIG2OSIG(oset, td->td_retval[0]);
2010	return (error);
2011}
2012
2013int
2014ofreebsd32_sigpending(struct thread *td,
2015			      struct ofreebsd32_sigpending_args *uap)
2016{
2017	struct proc *p = td->td_proc;
2018	sigset_t siglist;
2019
2020	PROC_LOCK(p);
2021	siglist = p->p_siglist;
2022	SIGSETOR(siglist, td->td_siglist);
2023	PROC_UNLOCK(p);
2024	SIG2OSIG(siglist, td->td_retval[0]);
2025	return (0);
2026}
2027
2028struct sigvec32 {
2029	u_int32_t	sv_handler;
2030	int		sv_mask;
2031	int		sv_flags;
2032};
2033
2034int
2035ofreebsd32_sigvec(struct thread *td,
2036			  struct ofreebsd32_sigvec_args *uap)
2037{
2038	struct sigvec32 vec;
2039	struct sigaction sa, osa, *sap;
2040	int error;
2041
2042	if (uap->signum <= 0 || uap->signum >= ONSIG)
2043		return (EINVAL);
2044
2045	if (uap->nsv) {
2046		error = copyin(uap->nsv, &vec, sizeof(vec));
2047		if (error)
2048			return (error);
2049		sa.sa_handler = PTRIN(vec.sv_handler);
2050		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2051		sa.sa_flags = vec.sv_flags;
2052		sa.sa_flags ^= SA_RESTART;
2053		sap = &sa;
2054	} else
2055		sap = NULL;
2056	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2057	if (error == 0 && uap->osv != NULL) {
2058		vec.sv_handler = PTROUT(osa.sa_handler);
2059		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2060		vec.sv_flags = osa.sa_flags;
2061		vec.sv_flags &= ~SA_NOCLDWAIT;
2062		vec.sv_flags ^= SA_RESTART;
2063		error = copyout(&vec, uap->osv, sizeof(vec));
2064	}
2065	return (error);
2066}
2067
2068int
2069ofreebsd32_sigblock(struct thread *td,
2070			    struct ofreebsd32_sigblock_args *uap)
2071{
2072	struct proc *p = td->td_proc;
2073	sigset_t set;
2074
2075	OSIG2SIG(uap->mask, set);
2076	SIG_CANTMASK(set);
2077	PROC_LOCK(p);
2078	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
2079	SIGSETOR(td->td_sigmask, set);
2080	PROC_UNLOCK(p);
2081	return (0);
2082}
2083
2084int
2085ofreebsd32_sigsetmask(struct thread *td,
2086			      struct ofreebsd32_sigsetmask_args *uap)
2087{
2088	struct proc *p = td->td_proc;
2089	sigset_t set;
2090
2091	OSIG2SIG(uap->mask, set);
2092	SIG_CANTMASK(set);
2093	PROC_LOCK(p);
2094	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
2095	SIGSETLO(td->td_sigmask, set);
2096	signotify(td);
2097	PROC_UNLOCK(p);
2098	return (0);
2099}
2100
2101int
2102ofreebsd32_sigsuspend(struct thread *td,
2103			      struct ofreebsd32_sigsuspend_args *uap)
2104{
2105	struct proc *p = td->td_proc;
2106	sigset_t mask;
2107
2108	PROC_LOCK(p);
2109	td->td_oldsigmask = td->td_sigmask;
2110	td->td_pflags |= TDP_OLDMASK;
2111	OSIG2SIG(uap->mask, mask);
2112	SIG_CANTMASK(mask);
2113	SIGSETLO(td->td_sigmask, mask);
2114	signotify(td);
2115	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
2116		/* void */;
2117	PROC_UNLOCK(p);
2118	/* always return EINTR rather than ERESTART... */
2119	return (EINTR);
2120}
2121
2122struct sigstack32 {
2123	u_int32_t	ss_sp;
2124	int		ss_onstack;
2125};
2126
2127int
2128ofreebsd32_sigstack(struct thread *td,
2129			    struct ofreebsd32_sigstack_args *uap)
2130{
2131	struct sigstack32 s32;
2132	struct sigstack nss, oss;
2133	int error = 0, unss;
2134
2135	if (uap->nss != NULL) {
2136		error = copyin(uap->nss, &s32, sizeof(s32));
2137		if (error)
2138			return (error);
2139		nss.ss_sp = PTRIN(s32.ss_sp);
2140		CP(s32, nss, ss_onstack);
2141		unss = 1;
2142	} else {
2143		unss = 0;
2144	}
2145	oss.ss_sp = td->td_sigstk.ss_sp;
2146	oss.ss_onstack = sigonstack(cpu_getstack(td));
2147	if (unss) {
2148		td->td_sigstk.ss_sp = nss.ss_sp;
2149		td->td_sigstk.ss_size = 0;
2150		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2151		td->td_pflags |= TDP_ALTSTACK;
2152	}
2153	if (uap->oss != NULL) {
2154		s32.ss_sp = PTROUT(oss.ss_sp);
2155		CP(oss, s32, ss_onstack);
2156		error = copyout(&s32, uap->oss, sizeof(s32));
2157	}
2158	return (error);
2159}
2160#endif
2161
2162int
2163freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2164{
2165	struct timespec32 rmt32, rqt32;
2166	struct timespec rmt, rqt;
2167	int error;
2168
2169	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2170	if (error)
2171		return (error);
2172
2173	CP(rqt32, rqt, tv_sec);
2174	CP(rqt32, rqt, tv_nsec);
2175
2176	if (uap->rmtp &&
2177	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2178		return (EFAULT);
2179	error = kern_nanosleep(td, &rqt, &rmt);
2180	if (error && uap->rmtp) {
2181		int error2;
2182
2183		CP(rmt, rmt32, tv_sec);
2184		CP(rmt, rmt32, tv_nsec);
2185
2186		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2187		if (error2)
2188			error = error2;
2189	}
2190	return (error);
2191}
2192
2193int
2194freebsd32_clock_gettime(struct thread *td,
2195			struct freebsd32_clock_gettime_args *uap)
2196{
2197	struct timespec	ats;
2198	struct timespec32 ats32;
2199	int error;
2200
2201	error = kern_clock_gettime(td, uap->clock_id, &ats);
2202	if (error == 0) {
2203		CP(ats, ats32, tv_sec);
2204		CP(ats, ats32, tv_nsec);
2205		error = copyout(&ats32, uap->tp, sizeof(ats32));
2206	}
2207	return (error);
2208}
2209
2210int
2211freebsd32_clock_settime(struct thread *td,
2212			struct freebsd32_clock_settime_args *uap)
2213{
2214	struct timespec	ats;
2215	struct timespec32 ats32;
2216	int error;
2217
2218	error = copyin(uap->tp, &ats32, sizeof(ats32));
2219	if (error)
2220		return (error);
2221	CP(ats32, ats, tv_sec);
2222	CP(ats32, ats, tv_nsec);
2223
2224	return (kern_clock_settime(td, uap->clock_id, &ats));
2225}
2226
2227int
2228freebsd32_clock_getres(struct thread *td,
2229		       struct freebsd32_clock_getres_args *uap)
2230{
2231	struct timespec	ts;
2232	struct timespec32 ts32;
2233	int error;
2234
2235	if (uap->tp == NULL)
2236		return (0);
2237	error = kern_clock_getres(td, uap->clock_id, &ts);
2238	if (error == 0) {
2239		CP(ts, ts32, tv_sec);
2240		CP(ts, ts32, tv_nsec);
2241		error = copyout(&ts32, uap->tp, sizeof(ts32));
2242	}
2243	return (error);
2244}
2245
2246int
2247freebsd32_thr_new(struct thread *td,
2248		  struct freebsd32_thr_new_args *uap)
2249{
2250	struct thr_param32 param32;
2251	struct thr_param param;
2252	int error;
2253
2254	if (uap->param_size < 0 ||
2255	    uap->param_size > sizeof(struct thr_param32))
2256		return (EINVAL);
2257	bzero(&param, sizeof(struct thr_param));
2258	bzero(&param32, sizeof(struct thr_param32));
2259	error = copyin(uap->param, &param32, uap->param_size);
2260	if (error != 0)
2261		return (error);
2262	param.start_func = PTRIN(param32.start_func);
2263	param.arg = PTRIN(param32.arg);
2264	param.stack_base = PTRIN(param32.stack_base);
2265	param.stack_size = param32.stack_size;
2266	param.tls_base = PTRIN(param32.tls_base);
2267	param.tls_size = param32.tls_size;
2268	param.child_tid = PTRIN(param32.child_tid);
2269	param.parent_tid = PTRIN(param32.parent_tid);
2270	param.flags = param32.flags;
2271	param.rtp = PTRIN(param32.rtp);
2272	param.spare[0] = PTRIN(param32.spare[0]);
2273	param.spare[1] = PTRIN(param32.spare[1]);
2274	param.spare[2] = PTRIN(param32.spare[2]);
2275
2276	return (kern_thr_new(td, &param));
2277}
2278
2279int
2280freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2281{
2282	struct timespec32 ts32;
2283	struct timespec ts, *tsp;
2284	int error;
2285
2286	error = 0;
2287	tsp = NULL;
2288	if (uap->timeout != NULL) {
2289		error = copyin((const void *)uap->timeout, (void *)&ts32,
2290		    sizeof(struct timespec32));
2291		if (error != 0)
2292			return (error);
2293		ts.tv_sec = ts32.tv_sec;
2294		ts.tv_nsec = ts32.tv_nsec;
2295		tsp = &ts;
2296	}
2297	return (kern_thr_suspend(td, tsp));
2298}
2299
2300void
2301siginfo_to_siginfo32(siginfo_t *src, struct siginfo32 *dst)
2302{
2303	bzero(dst, sizeof(*dst));
2304	dst->si_signo = src->si_signo;
2305	dst->si_errno = src->si_errno;
2306	dst->si_code = src->si_code;
2307	dst->si_pid = src->si_pid;
2308	dst->si_uid = src->si_uid;
2309	dst->si_status = src->si_status;
2310	dst->si_addr = dst->si_addr;
2311	dst->si_value.sigval_int = src->si_value.sival_int;
2312	dst->si_timerid = src->si_timerid;
2313	dst->si_overrun = src->si_overrun;
2314}
2315
2316int
2317freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2318{
2319	struct timespec32 ts32;
2320	struct timespec ts;
2321	struct timespec *timeout;
2322	sigset_t set;
2323	ksiginfo_t ksi;
2324	struct siginfo32 si32;
2325	int error;
2326
2327	if (uap->timeout) {
2328		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2329		if (error)
2330			return (error);
2331		ts.tv_sec = ts32.tv_sec;
2332		ts.tv_nsec = ts32.tv_nsec;
2333		timeout = &ts;
2334	} else
2335		timeout = NULL;
2336
2337	error = copyin(uap->set, &set, sizeof(set));
2338	if (error)
2339		return (error);
2340
2341	error = kern_sigtimedwait(td, set, &ksi, timeout);
2342	if (error)
2343		return (error);
2344
2345	if (uap->info) {
2346		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2347		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2348	}
2349
2350	if (error == 0)
2351		td->td_retval[0] = ksi.ksi_signo;
2352	return (error);
2353}
2354
2355/*
2356 * MPSAFE
2357 */
2358int
2359freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2360{
2361	ksiginfo_t ksi;
2362	struct siginfo32 si32;
2363	sigset_t set;
2364	int error;
2365
2366	error = copyin(uap->set, &set, sizeof(set));
2367	if (error)
2368		return (error);
2369
2370	error = kern_sigtimedwait(td, set, &ksi, NULL);
2371	if (error)
2372		return (error);
2373
2374	if (uap->info) {
2375		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2376		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2377	}
2378	if (error == 0)
2379		td->td_retval[0] = ksi.ksi_signo;
2380	return (error);
2381}
2382
2383#if 0
2384
2385int
2386freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2387{
2388	int error;
2389	struct yyy32 *p32, s32;
2390	struct yyy *p = NULL, s;
2391
2392	if (uap->zzz) {
2393		error = copyin(uap->zzz, &s32, sizeof(s32));
2394		if (error)
2395			return (error);
2396		/* translate in */
2397		p = &s;
2398	}
2399	error = kern_xxx(td, p);
2400	if (error)
2401		return (error);
2402	if (uap->zzz) {
2403		/* translate out */
2404		error = copyout(&s32, p32, sizeof(s32));
2405	}
2406	return (error);
2407}
2408
2409#endif
2410