freebsd32_misc.c revision 255426
1139825Simp/*-
245386Swpaul * Copyright (c) 2002 Doug Rabson
345386Swpaul * All rights reserved.
445386Swpaul *
545386Swpaul * Redistribution and use in source and binary forms, with or without
645386Swpaul * modification, are permitted provided that the following conditions
745386Swpaul * are met:
845386Swpaul * 1. Redistributions of source code must retain the above copyright
945386Swpaul *    notice, this list of conditions and the following disclaimer.
1045386Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1145386Swpaul *    notice, this list of conditions and the following disclaimer in the
1245386Swpaul *    documentation and/or other materials provided with the distribution.
1345386Swpaul *
1445386Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1545386Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1645386Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1745386Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1845386Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1945386Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2045386Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2145386Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2245386Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2345386Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2445386Swpaul * SUCH DAMAGE.
2545386Swpaul */
2645386Swpaul
2745386Swpaul#include <sys/cdefs.h>
2845386Swpaul__FBSDID("$FreeBSD: head/sys/compat/freebsd32/freebsd32_misc.c 255426 2013-09-09 18:11:59Z jhb $");
2945386Swpaul
3045386Swpaul#include "opt_compat.h"
3145386Swpaul#include "opt_inet.h"
3250477Speter#include "opt_inet6.h"
3345386Swpaul
3445386Swpaul#define __ELF_WORD_SIZE 32
3545386Swpaul
3645386Swpaul#include <sys/param.h>
3745386Swpaul#include <sys/bus.h>
3845386Swpaul#include <sys/capability.h>
3945386Swpaul#include <sys/clock.h>
4045386Swpaul#include <sys/exec.h>
4145386Swpaul#include <sys/fcntl.h>
4245386Swpaul#include <sys/filedesc.h>
4345386Swpaul#include <sys/imgact.h>
4445386Swpaul#include <sys/jail.h>
4545386Swpaul#include <sys/kernel.h>
4645386Swpaul#include <sys/limits.h>
4745386Swpaul#include <sys/linker.h>
4845386Swpaul#include <sys/lock.h>
4945386Swpaul#include <sys/malloc.h>
5045386Swpaul#include <sys/file.h>		/* Must come after sys/malloc.h */
5145386Swpaul#include <sys/imgact.h>
5245386Swpaul#include <sys/mbuf.h>
5345386Swpaul#include <sys/mman.h>
5445386Swpaul#include <sys/module.h>
5545386Swpaul#include <sys/mount.h>
5645386Swpaul#include <sys/mutex.h>
5745386Swpaul#include <sys/namei.h>
5845386Swpaul#include <sys/proc.h>
5945386Swpaul#include <sys/reboot.h>
6045386Swpaul#include <sys/resource.h>
6145386Swpaul#include <sys/resourcevar.h>
6245386Swpaul#include <sys/selinfo.h>
6363699Swpaul#include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
6445386Swpaul#include <sys/pipe.h>		/* Must come after sys/selinfo.h */
6545386Swpaul#include <sys/signal.h>
6645386Swpaul#include <sys/signalvar.h>
6745386Swpaul#include <sys/socket.h>
6845386Swpaul#include <sys/socketvar.h>
6945386Swpaul#include <sys/stat.h>
7045386Swpaul#include <sys/syscall.h>
7145386Swpaul#include <sys/syscallsubr.h>
7245386Swpaul#include <sys/sysctl.h>
7345386Swpaul#include <sys/sysent.h>
7445386Swpaul#include <sys/sysproto.h>
7545386Swpaul#include <sys/systm.h>
7664139Swpaul#include <sys/thr.h>
7745386Swpaul#include <sys/unistd.h>
7845386Swpaul#include <sys/ucontext.h>
7945386Swpaul#include <sys/vnode.h>
8045386Swpaul#include <sys/wait.h>
8145386Swpaul#include <sys/ipc.h>
8245386Swpaul#include <sys/msg.h>
8345386Swpaul#include <sys/sem.h>
8445386Swpaul#include <sys/shm.h>
8556206Swpaul
8656206Swpaul#ifdef INET
8756206Swpaul#include <netinet/in.h>
8856206Swpaul#endif
8956206Swpaul
9056206Swpaul#include <vm/vm.h>
9156206Swpaul#include <vm/vm_param.h>
9245386Swpaul#include <vm/pmap.h>
9345386Swpaul#include <vm/vm_map.h>
9445386Swpaul#include <vm/vm_object.h>
9545386Swpaul#include <vm/vm_extern.h>
9645386Swpaul
9745386Swpaul#include <machine/cpu.h>
9845386Swpaul#include <machine/elf.h>
9945386Swpaul
10045386Swpaul#include <security/audit/audit.h>
10145386Swpaul
10245386Swpaul#include <compat/freebsd32/freebsd32_util.h>
10345386Swpaul#include <compat/freebsd32/freebsd32.h>
10445386Swpaul#include <compat/freebsd32/freebsd32_ipc.h>
10545386Swpaul#include <compat/freebsd32/freebsd32_misc.h>
10645386Swpaul#include <compat/freebsd32/freebsd32_signal.h>
10745386Swpaul#include <compat/freebsd32/freebsd32_proto.h>
10845386Swpaul
10945386SwpaulFEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
11045386Swpaul
11145386Swpaul#ifndef __mips__
11245386SwpaulCTASSERT(sizeof(struct timeval32) == 8);
11345386SwpaulCTASSERT(sizeof(struct timespec32) == 8);
11445386SwpaulCTASSERT(sizeof(struct itimerval32) == 16);
11545386Swpaul#endif
11645386SwpaulCTASSERT(sizeof(struct statfs32) == 256);
11745386Swpaul#ifndef __mips__
11845386SwpaulCTASSERT(sizeof(struct rusage32) == 72);
11945386Swpaul#endif
12045386SwpaulCTASSERT(sizeof(struct sigaltstack32) == 12);
12145386SwpaulCTASSERT(sizeof(struct kevent32) == 20);
12245386SwpaulCTASSERT(sizeof(struct iovec32) == 8);
12345386SwpaulCTASSERT(sizeof(struct msghdr32) == 28);
12445386Swpaul#ifndef __mips__
12545386SwpaulCTASSERT(sizeof(struct stat32) == 96);
12645386Swpaul#endif
12745386SwpaulCTASSERT(sizeof(struct sigaction32) == 24);
12845386Swpaul
12945386Swpaulstatic int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
13045386Swpaulstatic int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
13145386Swpaul
13245386Swpaulvoid
13345386Swpaulfreebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
13445386Swpaul{
13545386Swpaul
13645386Swpaul	TV_CP(*s, *s32, ru_utime);
13745386Swpaul	TV_CP(*s, *s32, ru_stime);
13845386Swpaul	CP(*s, *s32, ru_maxrss);
13963699Swpaul	CP(*s, *s32, ru_ixrss);
14098849Sken	CP(*s, *s32, ru_idrss);
14145386Swpaul	CP(*s, *s32, ru_isrss);
14245386Swpaul	CP(*s, *s32, ru_minflt);
14345386Swpaul	CP(*s, *s32, ru_majflt);
14445386Swpaul	CP(*s, *s32, ru_nswap);
14545386Swpaul	CP(*s, *s32, ru_inblock);
14645386Swpaul	CP(*s, *s32, ru_oublock);
14745386Swpaul	CP(*s, *s32, ru_msgsnd);
14845386Swpaul	CP(*s, *s32, ru_msgrcv);
14945386Swpaul	CP(*s, *s32, ru_nsignals);
15045386Swpaul	CP(*s, *s32, ru_nvcsw);
15145386Swpaul	CP(*s, *s32, ru_nivcsw);
15245386Swpaul}
15345386Swpaul
15445386Swpaulint
15576033Swpaulfreebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
15676033Swpaul{
15776033Swpaul	int error, status;
15876033Swpaul	struct rusage32 ru32;
15976033Swpaul	struct rusage ru, *rup;
16076033Swpaul
16145386Swpaul	if (uap->rusage != NULL)
16245386Swpaul		rup = &ru;
16345386Swpaul	else
16445386Swpaul		rup = NULL;
16545386Swpaul	error = kern_wait(td, uap->pid, &status, uap->options, rup);
16645386Swpaul	if (error)
16745386Swpaul		return (error);
16845386Swpaul	if (uap->status != NULL)
16945386Swpaul		error = copyout(&status, uap->status, sizeof(status));
17045386Swpaul	if (uap->rusage != NULL && error == 0) {
17145386Swpaul		freebsd32_rusage_out(&ru, &ru32);
17245386Swpaul		error = copyout(&ru32, uap->rusage, sizeof(ru32));
17345386Swpaul	}
17445386Swpaul	return (error);
17545386Swpaul}
17645386Swpaul
17745386Swpaulint
17845386Swpaulfreebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
17945386Swpaul{
18045386Swpaul	struct wrusage32 wru32;
18145386Swpaul	struct __wrusage wru, *wrup;
18245386Swpaul	struct siginfo32 si32;
18345386Swpaul	struct __siginfo si, *sip;
18445386Swpaul	int error, status;
18545386Swpaul
18645386Swpaul	if (uap->wrusage != NULL)
18745386Swpaul		wrup = &wru;
18845386Swpaul	else
18945386Swpaul		wrup = NULL;
19045386Swpaul	if (uap->info != NULL) {
19145386Swpaul		sip = &si;
19245386Swpaul		bzero(sip, sizeof(*sip));
19345386Swpaul	} else
19445386Swpaul		sip = NULL;
19545386Swpaul	error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
19645386Swpaul	    &status, uap->options, wrup, sip);
19745386Swpaul	if (error != 0)
19845386Swpaul		return (error);
19945386Swpaul	if (uap->status != NULL)
20045386Swpaul		error = copyout(&status, uap->status, sizeof(status));
20145386Swpaul	if (uap->wrusage != NULL && error == 0) {
20245386Swpaul		freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
20345386Swpaul		freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
20445386Swpaul		error = copyout(&wru32, uap->wrusage, sizeof(wru32));
20545386Swpaul	}
20645386Swpaul	if (uap->info != NULL && error == 0) {
20745386Swpaul		siginfo_to_siginfo32 (&si, &si32);
20845386Swpaul		error = copyout(&si32, uap->info, sizeof(si32));
20945386Swpaul	}
21045386Swpaul	return (error);
21145386Swpaul}
21245386Swpaul
21345386Swpaul#ifdef COMPAT_FREEBSD4
21445386Swpaulstatic void
21545386Swpaulcopy_statfs(struct statfs *in, struct statfs32 *out)
21645386Swpaul{
21745386Swpaul
21845386Swpaul	statfs_scale_blocks(in, INT32_MAX);
21945386Swpaul	bzero(out, sizeof(*out));
22045386Swpaul	CP(*in, *out, f_bsize);
22145386Swpaul	out->f_iosize = MIN(in->f_iosize, INT32_MAX);
22245386Swpaul	CP(*in, *out, f_blocks);
22345386Swpaul	CP(*in, *out, f_bfree);
22445386Swpaul	CP(*in, *out, f_bavail);
22545386Swpaul	out->f_files = MIN(in->f_files, INT32_MAX);
22645386Swpaul	out->f_ffree = MIN(in->f_ffree, INT32_MAX);
22745386Swpaul	CP(*in, *out, f_fsid);
22845386Swpaul	CP(*in, *out, f_owner);
22945386Swpaul	CP(*in, *out, f_type);
23045386Swpaul	CP(*in, *out, f_flags);
23145386Swpaul	out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
23245386Swpaul	out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
23345386Swpaul	strlcpy(out->f_fstypename,
23445386Swpaul	      in->f_fstypename, MFSNAMELEN);
23545386Swpaul	strlcpy(out->f_mntonname,
23645386Swpaul	      in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
23745386Swpaul	out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
23845386Swpaul	out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
23945386Swpaul	strlcpy(out->f_mntfromname,
24045386Swpaul	      in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
24145386Swpaul}
24245386Swpaul#endif
24345386Swpaul
24445386Swpaul#ifdef COMPAT_FREEBSD4
24545386Swpaulint
24645386Swpaulfreebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
24745386Swpaul{
24845386Swpaul	struct statfs *buf, *sp;
24945386Swpaul	struct statfs32 stat32;
25045386Swpaul	size_t count, size;
25145386Swpaul	int error;
25245386Swpaul
25345386Swpaul	count = uap->bufsize / sizeof(struct statfs32);
25445386Swpaul	size = count * sizeof(struct statfs);
25545386Swpaul	error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
25645386Swpaul	if (size > 0) {
25745386Swpaul		count = td->td_retval[0];
25845386Swpaul		sp = buf;
25945386Swpaul		while (count > 0 && error == 0) {
26045386Swpaul			copy_statfs(sp, &stat32);
26145386Swpaul			error = copyout(&stat32, uap->buf, sizeof(stat32));
26245386Swpaul			sp++;
26345386Swpaul			uap->buf++;
26445386Swpaul			count--;
26545386Swpaul		}
26645386Swpaul		free(buf, M_TEMP);
26745386Swpaul	}
26845386Swpaul	return (error);
26945386Swpaul}
27045386Swpaul#endif
27145386Swpaul
27245386Swpaulint
27345386Swpaulfreebsd32_sigaltstack(struct thread *td,
27445386Swpaul		      struct freebsd32_sigaltstack_args *uap)
27545386Swpaul{
27645386Swpaul	struct sigaltstack32 s32;
27745386Swpaul	struct sigaltstack ss, oss, *ssp;
27845386Swpaul	int error;
27945386Swpaul
28045386Swpaul	if (uap->ss != NULL) {
28145386Swpaul		error = copyin(uap->ss, &s32, sizeof(s32));
28245386Swpaul		if (error)
28345386Swpaul			return (error);
28445386Swpaul		PTRIN_CP(s32, ss, ss_sp);
28545386Swpaul		CP(s32, ss, ss_size);
28645386Swpaul		CP(s32, ss, ss_flags);
28745386Swpaul		ssp = &ss;
28845386Swpaul	} else
28945386Swpaul		ssp = NULL;
29045386Swpaul	error = kern_sigaltstack(td, ssp, &oss);
29145386Swpaul	if (error == 0 && uap->oss != NULL) {
29245386Swpaul		PTROUT_CP(oss, s32, ss_sp);
29345386Swpaul		CP(oss, s32, ss_size);
29445386Swpaul		CP(oss, s32, ss_flags);
29545386Swpaul		error = copyout(&s32, uap->oss, sizeof(s32));
29645386Swpaul	}
29745386Swpaul	return (error);
29845386Swpaul}
29945386Swpaul
30045386Swpaul/*
30145386Swpaul * Custom version of exec_copyin_args() so that we can translate
30245386Swpaul * the pointers.
30345386Swpaul */
30445386Swpaulint
30545386Swpaulfreebsd32_exec_copyin_args(struct image_args *args, char *fname,
30645386Swpaul    enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
30745386Swpaul{
30845386Swpaul	char *argp, *envp;
30945386Swpaul	u_int32_t *p32, arg;
31045386Swpaul	size_t length;
31145386Swpaul	int error;
31245386Swpaul
31345386Swpaul	bzero(args, sizeof(*args));
31445386Swpaul	if (argv == NULL)
31545386Swpaul		return (EFAULT);
31645386Swpaul
31745386Swpaul	/*
31845386Swpaul	 * Allocate demand-paged memory for the file name, argument, and
31945386Swpaul	 * environment strings.
32045386Swpaul	 */
32145386Swpaul	error = exec_alloc_args(args);
322153770Syongari	if (error != 0)
32345386Swpaul		return (error);
32445386Swpaul
32545386Swpaul	/*
32645386Swpaul	 * Copy the file name.
32745386Swpaul	 */
32845386Swpaul	if (fname != NULL) {
32945386Swpaul		args->fname = args->buf;
33045386Swpaul		error = (segflg == UIO_SYSSPACE) ?
33145386Swpaul		    copystr(fname, args->fname, PATH_MAX, &length) :
33245386Swpaul		    copyinstr(fname, args->fname, PATH_MAX, &length);
33345386Swpaul		if (error != 0)
33445386Swpaul			goto err_exit;
33545386Swpaul	} else
33645386Swpaul		length = 0;
33745386Swpaul
33845386Swpaul	args->begin_argv = args->buf + length;
33945386Swpaul	args->endp = args->begin_argv;
34045386Swpaul	args->stringspace = ARG_MAX;
34145386Swpaul
34245386Swpaul	/*
34345386Swpaul	 * extract arguments first
34445386Swpaul	 */
34545386Swpaul	p32 = argv;
34645386Swpaul	for (;;) {
34745386Swpaul		error = copyin(p32++, &arg, sizeof(arg));
34845386Swpaul		if (error)
34945386Swpaul			goto err_exit;
35045386Swpaul		if (arg == 0)
35198849Sken			break;
35245386Swpaul		argp = PTRIN(arg);
35345386Swpaul		error = copyinstr(argp, args->endp, args->stringspace, &length);
35445386Swpaul		if (error) {
35545386Swpaul			if (error == ENAMETOOLONG)
35645386Swpaul				error = E2BIG;
35745386Swpaul			goto err_exit;
35845386Swpaul		}
35945386Swpaul		args->stringspace -= length;
36045386Swpaul		args->endp += length;
36145386Swpaul		args->argc++;
36245386Swpaul	}
36345386Swpaul
36445386Swpaul	args->begin_envv = args->endp;
36545386Swpaul
36645386Swpaul	/*
36745386Swpaul	 * extract environment strings
36845386Swpaul	 */
36945386Swpaul	if (envv) {
37045386Swpaul		p32 = envv;
37145386Swpaul		for (;;) {
37245386Swpaul			error = copyin(p32++, &arg, sizeof(arg));
37345386Swpaul			if (error)
37445386Swpaul				goto err_exit;
37545386Swpaul			if (arg == 0)
37645386Swpaul				break;
37745386Swpaul			envp = PTRIN(arg);
37845386Swpaul			error = copyinstr(envp, args->endp, args->stringspace,
37945386Swpaul			    &length);
38045386Swpaul			if (error) {
38145386Swpaul				if (error == ENAMETOOLONG)
38245386Swpaul					error = E2BIG;
38345386Swpaul				goto err_exit;
38445386Swpaul			}
38545386Swpaul			args->stringspace -= length;
38645386Swpaul			args->endp += length;
38745386Swpaul			args->envc++;
38845386Swpaul		}
38945386Swpaul	}
39045386Swpaul
39145386Swpaul	return (0);
39245386Swpaul
39345386Swpaulerr_exit:
39445386Swpaul	exec_free_args(args);
39545386Swpaul	return (error);
39645386Swpaul}
39745386Swpaul
39845386Swpaulint
39945386Swpaulfreebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
40045386Swpaul{
40145386Swpaul	struct image_args eargs;
402153982Syongari	int error;
403229441Syongari
404229441Syongari	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
405153396Sscottl	    uap->argv, uap->envv);
40645386Swpaul	if (error == 0)
40745386Swpaul		error = kern_execve(td, &eargs, NULL);
40845386Swpaul	return (error);
40945386Swpaul}
41045386Swpaul
41145386Swpaulint
41245386Swpaulfreebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
41345386Swpaul{
41445386Swpaul	struct image_args eargs;
41545386Swpaul	int error;
41645386Swpaul
41745386Swpaul	error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
41845386Swpaul	    uap->argv, uap->envv);
41945386Swpaul	if (error == 0) {
42045386Swpaul		eargs.fd = uap->fd;
42145386Swpaul		error = kern_execve(td, &eargs, NULL);
42245386Swpaul	}
42345386Swpaul	return (error);
42445386Swpaul}
42545386Swpaul
42645386Swpaul#ifdef __ia64__
42745386Swpaulstatic int
42845386Swpaulfreebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
42945386Swpaul		       int prot, int fd, off_t pos)
43045386Swpaul{
43198849Sken	vm_map_t map;
43298849Sken	vm_map_entry_t entry;
43398849Sken	int rv;
43498849Sken
43598849Sken	map = &td->td_proc->p_vmspace->vm_map;
43698849Sken	if (fd != -1)
43798849Sken		prot |= VM_PROT_WRITE;
43898849Sken
43998849Sken	if (vm_map_lookup_entry(map, start, &entry)) {
44098849Sken		if ((entry->protection & prot) != prot) {
44198849Sken			rv = vm_map_protect(map,
44298849Sken					    trunc_page(start),
44398849Sken					    round_page(end),
44498849Sken					    entry->protection | prot,
44598849Sken					    FALSE);
44698849Sken			if (rv != KERN_SUCCESS)
44798849Sken				return (EINVAL);
44898849Sken		}
44998849Sken	} else {
45098849Sken		vm_offset_t addr = trunc_page(start);
45198849Sken		rv = vm_map_find(map, NULL, 0, &addr, PAGE_SIZE, 0,
45298849Sken		    VMFS_NO_SPACE, prot, VM_PROT_ALL, 0);
45398849Sken		if (rv != KERN_SUCCESS)
45498849Sken			return (EINVAL);
45598849Sken	}
45698849Sken
45798849Sken	if (fd != -1) {
45898849Sken		struct pread_args r;
45998849Sken		r.fd = fd;
46098849Sken		r.buf = (void *) start;
46198849Sken		r.nbyte = end - start;
46298849Sken		r.offset = pos;
46398849Sken		return (sys_pread(td, &r));
46498849Sken	} else {
46598849Sken		while (start < end) {
46698849Sken			subyte((void *) start, 0);
46798849Sken			start++;
46898849Sken		}
46998849Sken		return (0);
47098849Sken	}
47198849Sken}
47298849Sken#endif
47398849Sken
47498849Skenint
47598849Skenfreebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
47698849Sken{
47798849Sken	struct mprotect_args ap;
47845386Swpaul
47945386Swpaul	ap.addr = PTRIN(uap->addr);
48045386Swpaul	ap.len = uap->len;
48145386Swpaul	ap.prot = uap->prot;
482229404Syongari#if defined(__amd64__) || defined(__ia64__)
483229404Syongari	if (i386_read_exec && (ap.prot & PROT_READ) != 0)
48445386Swpaul		ap.prot |= PROT_EXEC;
48545386Swpaul#endif
486153396Sscottl	return (sys_mprotect(td, &ap));
487153396Sscottl}
488153396Sscottl
489153396Sscottlint
490153396Sscottlfreebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
491153396Sscottl{
492153396Sscottl	struct mmap_args ap;
493153396Sscottl	vm_offset_t addr = (vm_offset_t) uap->addr;
494153396Sscottl	vm_size_t len	 = uap->len;
495153396Sscottl	int prot	 = uap->prot;
496153396Sscottl	int flags	 = uap->flags;
497153396Sscottl	int fd		 = uap->fd;
49845386Swpaul	off_t pos	 = PAIR32TO64(off_t,uap->pos);
49945386Swpaul#ifdef __ia64__
50045386Swpaul	vm_size_t pageoff;
50145386Swpaul	int error;
50245386Swpaul
50345386Swpaul	/*
50445386Swpaul	 * Attempt to handle page size hassles.
50545386Swpaul	 */
50645386Swpaul	pageoff = (pos & PAGE_MASK);
50745386Swpaul	if (flags & MAP_FIXED) {
50845386Swpaul		vm_offset_t start, end;
50945386Swpaul		start = addr;
51045386Swpaul		end = addr + len;
51145386Swpaul
51245386Swpaul		if (start != trunc_page(start)) {
51345386Swpaul			error = freebsd32_mmap_partial(td, start,
51445386Swpaul						       round_page(start), prot,
51545386Swpaul						       fd, pos);
51645386Swpaul			if (fd != -1)
51745386Swpaul				pos += round_page(start) - start;
51845386Swpaul			start = round_page(start);
51945386Swpaul		}
52045386Swpaul		if (end != round_page(end)) {
521229404Syongari			vm_offset_t t = trunc_page(end);
522229404Syongari			error = freebsd32_mmap_partial(td, t, end,
52345386Swpaul						  prot, fd,
524229404Syongari						  pos + t - start);
525229404Syongari			end = trunc_page(end);
52645386Swpaul		}
527229404Syongari		if (end > start && fd != -1 && (pos & PAGE_MASK)) {
52845386Swpaul			/*
52945386Swpaul			 * We can't map this region at all. The specified
53045386Swpaul			 * address doesn't have the same alignment as the file
53145386Swpaul			 * position. Fake the mapping by simply reading the
53245386Swpaul			 * entire region into memory. First we need to make
53345386Swpaul			 * sure the region exists.
53445386Swpaul			 */
53545386Swpaul			vm_map_t map;
53645386Swpaul			struct pread_args r;
53745386Swpaul			int rv;
53845386Swpaul
53945386Swpaul			prot |= VM_PROT_WRITE;
54045386Swpaul			map = &td->td_proc->p_vmspace->vm_map;
541229404Syongari			rv = vm_map_remove(map, start, end);
542229404Syongari			if (rv != KERN_SUCCESS)
54345386Swpaul				return (EINVAL);
54445386Swpaul			rv = vm_map_find(map, NULL, 0, &start, end - start,
54545386Swpaul			    0, VMFS_NO_SPACE, prot, VM_PROT_ALL, 0);
54645386Swpaul			if (rv != KERN_SUCCESS)
54745386Swpaul				return (EINVAL);
54845386Swpaul			r.fd = fd;
54945386Swpaul			r.buf = (void *) start;
55045386Swpaul			r.nbyte = end - start;
55145386Swpaul			r.offset = pos;
55245386Swpaul			error = sys_pread(td, &r);
55345386Swpaul			if (error)
55445386Swpaul				return (error);
55545386Swpaul
55645386Swpaul			td->td_retval[0] = addr;
55745386Swpaul			return (0);
55845386Swpaul		}
55945386Swpaul		if (end == start) {
56045386Swpaul			/*
56145386Swpaul			 * After dealing with the ragged ends, there
56245386Swpaul			 * might be none left.
56345386Swpaul			 */
56445386Swpaul			td->td_retval[0] = addr;
56545386Swpaul			return (0);
56645386Swpaul		}
56745386Swpaul		addr = start;
56845386Swpaul		len = end - start;
56945386Swpaul	}
57045386Swpaul#endif
57145386Swpaul
57245386Swpaul#if defined(__amd64__) || defined(__ia64__)
57345386Swpaul	if (i386_read_exec && (prot & PROT_READ))
57445386Swpaul		prot |= PROT_EXEC;
57545386Swpaul#endif
576229404Syongari
577229404Syongari	ap.addr = (void *) addr;
57845386Swpaul	ap.len = len;
579229404Syongari	ap.prot = prot;
580229404Syongari	ap.flags = flags;
58145386Swpaul	ap.fd = fd;
58245386Swpaul	ap.pos = pos;
583229404Syongari
584229404Syongari	return (sys_mmap(td, &ap));
58545386Swpaul}
586229404Syongari
587229404Syongari#ifdef COMPAT_FREEBSD6
58845386Swpaulint
58945386Swpaulfreebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
590229404Syongari{
591229404Syongari	struct freebsd32_mmap_args ap;
59245386Swpaul
593229404Syongari	ap.addr = uap->addr;
594229404Syongari	ap.len = uap->len;
59545386Swpaul	ap.prot = uap->prot;
59645386Swpaul	ap.flags = uap->flags;
597229404Syongari	ap.fd = uap->fd;
598229404Syongari	ap.pos1 = uap->pos1;
59945386Swpaul	ap.pos2 = uap->pos2;
600229404Syongari
601229404Syongari	return (freebsd32_mmap(td, &ap));
60245386Swpaul}
603229404Syongari#endif
604229404Syongari
60545386Swpaulint
60645386Swpaulfreebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
607229441Syongari{
608229441Syongari	struct itimerval itv, oitv, *itvp;
609229441Syongari	struct itimerval32 i32;
610229441Syongari	int error;
61145386Swpaul
61245386Swpaul	if (uap->itv != NULL) {
61345386Swpaul		error = copyin(uap->itv, &i32, sizeof(i32));
61445386Swpaul		if (error)
61545386Swpaul			return (error);
616229404Syongari		TV_CP(i32, itv, it_interval);
617229404Syongari		TV_CP(i32, itv, it_value);
61845386Swpaul		itvp = &itv;
619229404Syongari	} else
620229404Syongari		itvp = NULL;
62145386Swpaul	error = kern_setitimer(td, uap->which, itvp, &oitv);
62245386Swpaul	if (error || uap->oitv == NULL)
623229404Syongari		return (error);
624229404Syongari	TV_CP(oitv, i32, it_interval);
62545386Swpaul	TV_CP(oitv, i32, it_value);
626229404Syongari	return (copyout(&i32, uap->oitv, sizeof(i32)));
627229404Syongari}
62845386Swpaul
62945386Swpaulint
63045386Swpaulfreebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
631229404Syongari{
632229404Syongari	struct itimerval itv;
63345386Swpaul	struct itimerval32 i32;
634229404Syongari	int error;
635229404Syongari
63645386Swpaul	error = kern_getitimer(td, uap->which, &itv);
63745386Swpaul	if (error || uap->itv == NULL)
638229404Syongari		return (error);
639229404Syongari	TV_CP(itv, i32, it_interval);
64045386Swpaul	TV_CP(itv, i32, it_value);
641229404Syongari	return (copyout(&i32, uap->itv, sizeof(i32)));
642229404Syongari}
64345386Swpaul
64445386Swpaulint
645229404Syongarifreebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
646229404Syongari{
64745386Swpaul	struct timeval32 tv32;
648229404Syongari	struct timeval tv, *tvp;
649229404Syongari	int error;
65045386Swpaul
65145386Swpaul	if (uap->tv != NULL) {
652229404Syongari		error = copyin(uap->tv, &tv32, sizeof(tv32));
653229404Syongari		if (error)
65445386Swpaul			return (error);
655229404Syongari		CP(tv32, tv, tv_sec);
656229404Syongari		CP(tv32, tv, tv_usec);
65745386Swpaul		tvp = &tv;
658229404Syongari	} else
659229404Syongari		tvp = NULL;
66045386Swpaul	/*
66145386Swpaul	 * XXX Do pointers need PTRIN()?
662229441Syongari	 */
663229441Syongari	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
664229441Syongari	    sizeof(int32_t) * 8));
665229441Syongari}
666229441Syongari
667229441Syongariint
668229441Syongarifreebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
669229441Syongari{
67045386Swpaul	struct timespec32 ts32;
67145386Swpaul	struct timespec ts;
67245386Swpaul	struct timeval tv, *tvp;
67345386Swpaul	sigset_t set, *uset;
67445386Swpaul	int error;
67545386Swpaul
676229404Syongari	if (uap->ts != NULL) {
677229404Syongari		error = copyin(uap->ts, &ts32, sizeof(ts32));
67845386Swpaul		if (error != 0)
679229404Syongari			return (error);
680229404Syongari		CP(ts32, ts, tv_sec);
68145386Swpaul		CP(ts32, ts, tv_nsec);
68245386Swpaul		TIMESPEC_TO_TIMEVAL(&tv, &ts);
683229404Syongari		tvp = &tv;
684229404Syongari	} else
68545386Swpaul		tvp = NULL;
686229404Syongari	if (uap->sm != NULL) {
687229404Syongari		error = copyin(uap->sm, &set, sizeof(set));
68845386Swpaul		if (error != 0)
68945386Swpaul			return (error);
69045386Swpaul		uset = &set;
691229441Syongari	} else
692229441Syongari		uset = NULL;
69345386Swpaul	/*
69445386Swpaul	 * XXX Do pointers need PTRIN()?
69545386Swpaul	 */
69645386Swpaul	error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
69745386Swpaul	    uset, sizeof(int32_t) * 8);
698150716Sjhb	return (error);
69945386Swpaul}
70045386Swpaul
70145386Swpaul/*
70245386Swpaul * Copy 'count' items into the destination list pointed to by uap->eventlist.
70349036Swpaul */
70445386Swpaulstatic int
70545386Swpaulfreebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
70645386Swpaul{
70745386Swpaul	struct freebsd32_kevent_args *uap;
70845386Swpaul	struct kevent32	ks32[KQ_NEVENTS];
70945386Swpaul	int i, error = 0;
71045386Swpaul
71145386Swpaul	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
71245386Swpaul	uap = (struct freebsd32_kevent_args *)arg;
71345386Swpaul
71445386Swpaul	for (i = 0; i < count; i++) {
71545386Swpaul		CP(kevp[i], ks32[i], ident);
71645386Swpaul		CP(kevp[i], ks32[i], filter);
71745386Swpaul		CP(kevp[i], ks32[i], flags);
71845386Swpaul		CP(kevp[i], ks32[i], fflags);
71945386Swpaul		CP(kevp[i], ks32[i], data);
72045386Swpaul		PTROUT_CP(kevp[i], ks32[i], udata);
72145386Swpaul	}
72245386Swpaul	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
72345386Swpaul	if (error == 0)
72445386Swpaul		uap->eventlist += count;
72545386Swpaul	return (error);
72645386Swpaul}
72745386Swpaul
72845386Swpaul/*
72945386Swpaul * Copy 'count' items from the list pointed to by uap->changelist.
73045386Swpaul */
73145386Swpaulstatic int
73245386Swpaulfreebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
73345386Swpaul{
73445386Swpaul	struct freebsd32_kevent_args *uap;
73545386Swpaul	struct kevent32	ks32[KQ_NEVENTS];
73645386Swpaul	int i, error = 0;
73745386Swpaul
73845386Swpaul	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
73945386Swpaul	uap = (struct freebsd32_kevent_args *)arg;
74045386Swpaul
74145386Swpaul	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
74245386Swpaul	if (error)
74345386Swpaul		goto done;
74445386Swpaul	uap->changelist += count;
74545386Swpaul
74645386Swpaul	for (i = 0; i < count; i++) {
74745386Swpaul		CP(ks32[i], kevp[i], ident);
74845386Swpaul		CP(ks32[i], kevp[i], filter);
74945386Swpaul		CP(ks32[i], kevp[i], flags);
75045386Swpaul		CP(ks32[i], kevp[i], fflags);
75145386Swpaul		CP(ks32[i], kevp[i], data);
75245386Swpaul		PTRIN_CP(ks32[i], kevp[i], udata);
75345386Swpaul	}
75445386Swpauldone:
75545386Swpaul	return (error);
75645386Swpaul}
75745386Swpaul
75845386Swpaulint
75945386Swpaulfreebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
76045386Swpaul{
76145386Swpaul	struct timespec32 ts32;
76245386Swpaul	struct timespec ts, *tsp;
76345386Swpaul	struct kevent_copyops k_ops = { uap,
76445386Swpaul					freebsd32_kevent_copyout,
76545386Swpaul					freebsd32_kevent_copyin};
76645386Swpaul	int error;
76745386Swpaul
76845386Swpaul
76945386Swpaul	if (uap->timeout) {
77045386Swpaul		error = copyin(uap->timeout, &ts32, sizeof(ts32));
77145386Swpaul		if (error)
772229404Syongari			return (error);
77345386Swpaul		CP(ts32, ts, tv_sec);
77445386Swpaul		CP(ts32, ts, tv_nsec);
775153770Syongari		tsp = &ts;
776153770Syongari	} else
777153770Syongari		tsp = NULL;
778153770Syongari	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
77945386Swpaul	    &k_ops, tsp);
78045386Swpaul	return (error);
78145386Swpaul}
78245386Swpaul
78345386Swpaulint
78445386Swpaulfreebsd32_gettimeofday(struct thread *td,
78545386Swpaul		       struct freebsd32_gettimeofday_args *uap)
78645386Swpaul{
78745386Swpaul	struct timeval atv;
78845386Swpaul	struct timeval32 atv32;
78945386Swpaul	struct timezone rtz;
79045386Swpaul	int error = 0;
79145386Swpaul
79245386Swpaul	if (uap->tp) {
79345386Swpaul		microtime(&atv);
79445386Swpaul		CP(atv, atv32, tv_sec);
79545386Swpaul		CP(atv, atv32, tv_usec);
79645386Swpaul		error = copyout(&atv32, uap->tp, sizeof (atv32));
79745386Swpaul	}
79845386Swpaul	if (error == 0 && uap->tzp != NULL) {
79945386Swpaul		rtz.tz_minuteswest = tz_minuteswest;
80045386Swpaul		rtz.tz_dsttime = tz_dsttime;
80145386Swpaul		error = copyout(&rtz, uap->tzp, sizeof (rtz));
80245386Swpaul	}
80345386Swpaul	return (error);
80445386Swpaul}
80545386Swpaul
80645386Swpaulint
80745386Swpaulfreebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
80845386Swpaul{
80945386Swpaul	struct rusage32 s32;
81045386Swpaul	struct rusage s;
81145386Swpaul	int error;
81245386Swpaul
81345386Swpaul	error = kern_getrusage(td, uap->who, &s);
81445386Swpaul	if (error)
81545386Swpaul		return (error);
81645386Swpaul	if (uap->rusage != NULL) {
81745386Swpaul		freebsd32_rusage_out(&s, &s32);
81845386Swpaul		error = copyout(&s32, uap->rusage, sizeof(s32));
81945386Swpaul	}
82045386Swpaul	return (error);
82145386Swpaul}
82245386Swpaul
82345386Swpaulstatic int
82445386Swpaulfreebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
825153770Syongari{
826153770Syongari	struct iovec32 iov32;
827153770Syongari	struct iovec *iov;
828153770Syongari	struct uio *uio;
82945386Swpaul	u_int iovlen;
830153770Syongari	int error, i;
831153770Syongari
832153770Syongari	*uiop = NULL;
833153770Syongari	if (iovcnt > UIO_MAXIOV)
83445386Swpaul		return (EINVAL);
83545386Swpaul	iovlen = iovcnt * sizeof(struct iovec);
83645386Swpaul	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
83745386Swpaul	iov = (struct iovec *)(uio + 1);
838153770Syongari	for (i = 0; i < iovcnt; i++) {
83945386Swpaul		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
840153770Syongari		if (error) {
841153770Syongari			free(uio, M_IOV);
842153770Syongari			return (error);
843153770Syongari		}
844153770Syongari		iov[i].iov_base = PTRIN(iov32.iov_base);
845153770Syongari		iov[i].iov_len = iov32.iov_len;
84645386Swpaul	}
84745386Swpaul	uio->uio_iov = iov;
848153770Syongari	uio->uio_iovcnt = iovcnt;
84945386Swpaul	uio->uio_segflg = UIO_USERSPACE;
850153770Syongari	uio->uio_offset = -1;
851153770Syongari	uio->uio_resid = 0;
852153770Syongari	for (i = 0; i < iovcnt; i++) {
853153770Syongari		if (iov->iov_len > INT_MAX - uio->uio_resid) {
854153770Syongari			free(uio, M_IOV);
855153770Syongari			return (EINVAL);
85645386Swpaul		}
85745386Swpaul		uio->uio_resid += iov->iov_len;
85845386Swpaul		iov++;
85945386Swpaul	}
86045386Swpaul	*uiop = uio;
861229404Syongari	return (0);
862229404Syongari}
86345386Swpaul
864229441Syongariint
86545386Swpaulfreebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
866153770Syongari{
867153770Syongari	struct uio *auio;
868153770Syongari	int error;
869153770Syongari
87045386Swpaul	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
87145386Swpaul	if (error)
87245386Swpaul		return (error);
87345386Swpaul	error = kern_readv(td, uap->fd, auio);
87445386Swpaul	free(auio, M_IOV);
87545386Swpaul	return (error);
87645386Swpaul}
87745386Swpaul
87845386Swpaulint
87945386Swpaulfreebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
88045386Swpaul{
88145386Swpaul	struct uio *auio;
88245386Swpaul	int error;
88345386Swpaul
88445386Swpaul	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
88545386Swpaul	if (error)
88645386Swpaul		return (error);
88745386Swpaul	error = kern_writev(td, uap->fd, auio);
88845386Swpaul	free(auio, M_IOV);
88945386Swpaul	return (error);
89045386Swpaul}
89145386Swpaul
89245386Swpaulint
89345386Swpaulfreebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
89445386Swpaul{
89545386Swpaul	struct uio *auio;
89645386Swpaul	int error;
897153770Syongari
89845386Swpaul	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
89945386Swpaul	if (error)
900153770Syongari		return (error);
90145386Swpaul	error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
90245386Swpaul	free(auio, M_IOV);
903153770Syongari	return (error);
90445386Swpaul}
905153770Syongari
90645386Swpaulint
907153982Syongarifreebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
908153982Syongari{
909153982Syongari	struct uio *auio;
910153982Syongari	int error;
911153982Syongari
912153982Syongari	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
913153982Syongari	if (error)
914153982Syongari		return (error);
915229441Syongari	error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
91645386Swpaul	free(auio, M_IOV);
91745386Swpaul	return (error);
918229441Syongari}
919229441Syongari
920229441Syongariint
92145386Swpaulfreebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
92245386Swpaul    int error)
923229404Syongari{
92445386Swpaul	struct iovec32 iov32;
925229404Syongari	struct iovec *iov;
92645386Swpaul	u_int iovlen;
927229404Syongari	int i;
92845386Swpaul
92945386Swpaul	*iovp = NULL;
930229441Syongari	if (iovcnt > UIO_MAXIOV)
931229441Syongari		return (error);
932229441Syongari	iovlen = iovcnt * sizeof(struct iovec);
933229441Syongari	iov = malloc(iovlen, M_IOV, M_WAITOK);
934229441Syongari	for (i = 0; i < iovcnt; i++) {
935229441Syongari		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
936229441Syongari		if (error) {
937229441Syongari			free(iov, M_IOV);
938229441Syongari			return (error);
939229441Syongari		}
940229441Syongari		iov[i].iov_base = PTRIN(iov32.iov_base);
941229441Syongari		iov[i].iov_len = iov32.iov_len;
942229441Syongari	}
943229441Syongari	*iovp = iov;
944229441Syongari	return (0);
945229441Syongari}
946229441Syongari
947229441Syongaristatic int
948229441Syongarifreebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
949229441Syongari{
950229441Syongari	struct msghdr32 m32;
951229441Syongari	int error;
952229441Syongari
953229441Syongari	error = copyin(msg32, &m32, sizeof(m32));
954229441Syongari	if (error)
955229441Syongari		return (error);
956229441Syongari	msg->msg_name = PTRIN(m32.msg_name);
957229441Syongari	msg->msg_namelen = m32.msg_namelen;
958153770Syongari	msg->msg_iov = PTRIN(m32.msg_iov);
95945386Swpaul	msg->msg_iovlen = m32.msg_iovlen;
96045386Swpaul	msg->msg_control = PTRIN(m32.msg_control);
96145386Swpaul	msg->msg_controllen = m32.msg_controllen;
96245386Swpaul	msg->msg_flags = m32.msg_flags;
96345386Swpaul	return (0);
96445386Swpaul}
965229441Syongari
966229441Syongaristatic int
967229441Syongarifreebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
968229441Syongari{
969229441Syongari	struct msghdr32 m32;
970229441Syongari	int error;
971229441Syongari
972229441Syongari	m32.msg_name = PTROUT(msg->msg_name);
973229441Syongari	m32.msg_namelen = msg->msg_namelen;
974229441Syongari	m32.msg_iov = PTROUT(msg->msg_iov);
975153982Syongari	m32.msg_iovlen = msg->msg_iovlen;
976153982Syongari	m32.msg_control = PTROUT(msg->msg_control);
977153982Syongari	m32.msg_controllen = msg->msg_controllen;
978229441Syongari	m32.msg_flags = msg->msg_flags;
979229441Syongari	error = copyout(&m32, msg32, sizeof(m32));
980229441Syongari	return (error);
981229441Syongari}
982229441Syongari
98345386Swpaul#ifndef __mips__
984153396Sscottl#define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
985229432Syongari#else
986229441Syongari#define FREEBSD32_ALIGNBYTES	(sizeof(long) - 1)
987229441Syongari#endif
988229441Syongari#define FREEBSD32_ALIGN(p)	\
98945386Swpaul	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
990153396Sscottl#define	FREEBSD32_CMSG_SPACE(l)	\
991229432Syongari	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
992229441Syongari
993229441Syongari#define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
994229441Syongari				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
99545386Swpaulstatic int
996153396Sscottlfreebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
997229432Syongari{
99845386Swpaul	struct cmsghdr *cm;
99945386Swpaul	void *data;
100045386Swpaul	socklen_t clen, datalen;
1001229404Syongari	int error;
1002229404Syongari	caddr_t ctlbuf;
1003219547Smarius	int len, maxlen, copylen;
100445386Swpaul	struct mbuf *m;
100545386Swpaul	error = 0;
100645386Swpaul
100745386Swpaul	len    = msg->msg_controllen;
100845386Swpaul	maxlen = msg->msg_controllen;
100945386Swpaul	msg->msg_controllen = 0;
101045386Swpaul
101145386Swpaul	m = control;
101245386Swpaul	ctlbuf = msg->msg_control;
101360938Sjake
101445386Swpaul	while (m && len > 0) {
101545386Swpaul		cm = mtod(m, struct cmsghdr *);
101698849Sken		clen = m->m_len;
101798849Sken
101898849Sken		while (cm != NULL) {
101998849Sken
102098849Sken			if (sizeof(struct cmsghdr) > clen ||
102198849Sken			    cm->cmsg_len > clen) {
102245386Swpaul				error = EINVAL;
1023153396Sscottl				break;
1024147256Sbrooks			}
102545386Swpaul
102645386Swpaul			data   = CMSG_DATA(cm);
102749011Swpaul			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
102849011Swpaul
102949011Swpaul			/* Adjust message length */
103045386Swpaul			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1031229404Syongari			    datalen;
1032229404Syongari
1033229404Syongari
103498849Sken			/* Copy cmsghdr */
1035229441Syongari			copylen = sizeof(struct cmsghdr);
1036229441Syongari			if (len < copylen) {
103745386Swpaul				msg->msg_flags |= MSG_CTRUNC;
1038229441Syongari				copylen = len;
1039229441Syongari			}
1040229441Syongari
1041153982Syongari			error = copyout(cm,ctlbuf,copylen);
1042153982Syongari			if (error)
1043153982Syongari				goto exit;
1044153982Syongari
1045153982Syongari			ctlbuf += FREEBSD32_ALIGN(copylen);
1046153982Syongari			len    -= FREEBSD32_ALIGN(copylen);
1047153982Syongari
1048153982Syongari			if (len <= 0)
104960938Sjake				break;
1050229404Syongari
1051229404Syongari			/* Copy data */
1052229404Syongari			copylen = datalen;
1053229404Syongari			if (len < copylen) {
1054229404Syongari				msg->msg_flags |= MSG_CTRUNC;
1055229404Syongari				copylen = len;
105645386Swpaul			}
105748011Swpaul
105867087Swpaul			error = copyout(data,ctlbuf,copylen);
1059199559Sjhb			if (error)
1060199559Sjhb				goto exit;
106198849Sken
1062229438Syongari			ctlbuf += FREEBSD32_ALIGN(copylen);
1063229438Syongari			len    -= FREEBSD32_ALIGN(copylen);
1064153396Sscottl
106545386Swpaul			if (CMSG_SPACE(datalen) < clen) {
106645386Swpaul				clen -= CMSG_SPACE(datalen);
106772200Sbmilekic				cm = (struct cmsghdr *)
106872200Sbmilekic					((caddr_t)cm + CMSG_SPACE(datalen));
1069122689Ssam			} else {
107067087Swpaul				clen = 0;
107145386Swpaul				cm = NULL;
107245386Swpaul			}
107345386Swpaul		}
107445386Swpaul		m = m->m_next;
107545386Swpaul	}
107645386Swpaul
107745386Swpaul	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
107845386Swpaul
107945386Swpaulexit:
1080153770Syongari	return (error);
108145386Swpaul
108245386Swpaul}
108345386Swpaul
108445386Swpaulint
1085153770Syongarifreebsd32_recvmsg(td, uap)
1086153770Syongari	struct thread *td;
108745386Swpaul	struct freebsd32_recvmsg_args /* {
108845386Swpaul		int	s;
108945386Swpaul		struct	msghdr32 *msg;
109045386Swpaul		int	flags;
109145386Swpaul	} */ *uap;
1092153770Syongari{
109345386Swpaul	struct msghdr msg;
109445386Swpaul	struct msghdr32 m32;
109545386Swpaul	struct iovec *uiov, *iov;
109645386Swpaul	struct mbuf *control = NULL;
109745386Swpaul	struct mbuf **controlp;
109845386Swpaul
1099153770Syongari	int error;
1100153770Syongari	error = copyin(uap->msg, &m32, sizeof(m32));
1101	if (error)
1102		return (error);
1103	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1104	if (error)
1105		return (error);
1106	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1107	    EMSGSIZE);
1108	if (error)
1109		return (error);
1110	msg.msg_flags = uap->flags;
1111	uiov = msg.msg_iov;
1112	msg.msg_iov = iov;
1113
1114	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1115	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1116	if (error == 0) {
1117		msg.msg_iov = uiov;
1118
1119		if (control != NULL)
1120			error = freebsd32_copy_msg_out(&msg, control);
1121		else
1122			msg.msg_controllen = 0;
1123
1124		if (error == 0)
1125			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1126	}
1127	free(iov, M_IOV);
1128
1129	if (control != NULL)
1130		m_freem(control);
1131
1132	return (error);
1133}
1134
1135
1136static int
1137freebsd32_convert_msg_in(struct mbuf **controlp)
1138{
1139	struct mbuf *control = *controlp;
1140	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1141	void *data;
1142	socklen_t clen = control->m_len, datalen;
1143	int error;
1144
1145	error = 0;
1146	*controlp = NULL;
1147
1148	while (cm != NULL) {
1149		if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1150			error = EINVAL;
1151			break;
1152		}
1153
1154		data = FREEBSD32_CMSG_DATA(cm);
1155		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1156
1157		*controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1158		    cm->cmsg_level);
1159		controlp = &(*controlp)->m_next;
1160
1161		if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1162			clen -= FREEBSD32_CMSG_SPACE(datalen);
1163			cm = (struct cmsghdr *)
1164				((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1165		} else {
1166			clen = 0;
1167			cm = NULL;
1168		}
1169	}
1170
1171	m_freem(control);
1172	return (error);
1173}
1174
1175
1176int
1177freebsd32_sendmsg(struct thread *td,
1178		  struct freebsd32_sendmsg_args *uap)
1179{
1180	struct msghdr msg;
1181	struct msghdr32 m32;
1182	struct iovec *iov;
1183	struct mbuf *control = NULL;
1184	struct sockaddr *to = NULL;
1185	int error;
1186
1187	error = copyin(uap->msg, &m32, sizeof(m32));
1188	if (error)
1189		return (error);
1190	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1191	if (error)
1192		return (error);
1193	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1194	    EMSGSIZE);
1195	if (error)
1196		return (error);
1197	msg.msg_iov = iov;
1198	if (msg.msg_name != NULL) {
1199		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1200		if (error) {
1201			to = NULL;
1202			goto out;
1203		}
1204		msg.msg_name = to;
1205	}
1206
1207	if (msg.msg_control) {
1208		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1209			error = EINVAL;
1210			goto out;
1211		}
1212
1213		error = sockargs(&control, msg.msg_control,
1214		    msg.msg_controllen, MT_CONTROL);
1215		if (error)
1216			goto out;
1217
1218		error = freebsd32_convert_msg_in(&control);
1219		if (error)
1220			goto out;
1221	}
1222
1223	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1224	    UIO_USERSPACE);
1225
1226out:
1227	free(iov, M_IOV);
1228	if (to)
1229		free(to, M_SONAME);
1230	return (error);
1231}
1232
1233int
1234freebsd32_recvfrom(struct thread *td,
1235		   struct freebsd32_recvfrom_args *uap)
1236{
1237	struct msghdr msg;
1238	struct iovec aiov;
1239	int error;
1240
1241	if (uap->fromlenaddr) {
1242		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1243		    sizeof(msg.msg_namelen));
1244		if (error)
1245			return (error);
1246	} else {
1247		msg.msg_namelen = 0;
1248	}
1249
1250	msg.msg_name = PTRIN(uap->from);
1251	msg.msg_iov = &aiov;
1252	msg.msg_iovlen = 1;
1253	aiov.iov_base = PTRIN(uap->buf);
1254	aiov.iov_len = uap->len;
1255	msg.msg_control = NULL;
1256	msg.msg_flags = uap->flags;
1257	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1258	if (error == 0 && uap->fromlenaddr)
1259		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1260		    sizeof (msg.msg_namelen));
1261	return (error);
1262}
1263
1264int
1265freebsd32_settimeofday(struct thread *td,
1266		       struct freebsd32_settimeofday_args *uap)
1267{
1268	struct timeval32 tv32;
1269	struct timeval tv, *tvp;
1270	struct timezone tz, *tzp;
1271	int error;
1272
1273	if (uap->tv) {
1274		error = copyin(uap->tv, &tv32, sizeof(tv32));
1275		if (error)
1276			return (error);
1277		CP(tv32, tv, tv_sec);
1278		CP(tv32, tv, tv_usec);
1279		tvp = &tv;
1280	} else
1281		tvp = NULL;
1282	if (uap->tzp) {
1283		error = copyin(uap->tzp, &tz, sizeof(tz));
1284		if (error)
1285			return (error);
1286		tzp = &tz;
1287	} else
1288		tzp = NULL;
1289	return (kern_settimeofday(td, tvp, tzp));
1290}
1291
1292int
1293freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1294{
1295	struct timeval32 s32[2];
1296	struct timeval s[2], *sp;
1297	int error;
1298
1299	if (uap->tptr != NULL) {
1300		error = copyin(uap->tptr, s32, sizeof(s32));
1301		if (error)
1302			return (error);
1303		CP(s32[0], s[0], tv_sec);
1304		CP(s32[0], s[0], tv_usec);
1305		CP(s32[1], s[1], tv_sec);
1306		CP(s32[1], s[1], tv_usec);
1307		sp = s;
1308	} else
1309		sp = NULL;
1310	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1311}
1312
1313int
1314freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1315{
1316	struct timeval32 s32[2];
1317	struct timeval s[2], *sp;
1318	int error;
1319
1320	if (uap->tptr != NULL) {
1321		error = copyin(uap->tptr, s32, sizeof(s32));
1322		if (error)
1323			return (error);
1324		CP(s32[0], s[0], tv_sec);
1325		CP(s32[0], s[0], tv_usec);
1326		CP(s32[1], s[1], tv_sec);
1327		CP(s32[1], s[1], tv_usec);
1328		sp = s;
1329	} else
1330		sp = NULL;
1331	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1332}
1333
1334int
1335freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1336{
1337	struct timeval32 s32[2];
1338	struct timeval s[2], *sp;
1339	int error;
1340
1341	if (uap->tptr != NULL) {
1342		error = copyin(uap->tptr, s32, sizeof(s32));
1343		if (error)
1344			return (error);
1345		CP(s32[0], s[0], tv_sec);
1346		CP(s32[0], s[0], tv_usec);
1347		CP(s32[1], s[1], tv_sec);
1348		CP(s32[1], s[1], tv_usec);
1349		sp = s;
1350	} else
1351		sp = NULL;
1352	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1353}
1354
1355int
1356freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1357{
1358	struct timeval32 s32[2];
1359	struct timeval s[2], *sp;
1360	int error;
1361
1362	if (uap->times != NULL) {
1363		error = copyin(uap->times, s32, sizeof(s32));
1364		if (error)
1365			return (error);
1366		CP(s32[0], s[0], tv_sec);
1367		CP(s32[0], s[0], tv_usec);
1368		CP(s32[1], s[1], tv_sec);
1369		CP(s32[1], s[1], tv_usec);
1370		sp = s;
1371	} else
1372		sp = NULL;
1373	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1374		sp, UIO_SYSSPACE));
1375}
1376
1377int
1378freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1379{
1380	struct timeval32 tv32;
1381	struct timeval delta, olddelta, *deltap;
1382	int error;
1383
1384	if (uap->delta) {
1385		error = copyin(uap->delta, &tv32, sizeof(tv32));
1386		if (error)
1387			return (error);
1388		CP(tv32, delta, tv_sec);
1389		CP(tv32, delta, tv_usec);
1390		deltap = &delta;
1391	} else
1392		deltap = NULL;
1393	error = kern_adjtime(td, deltap, &olddelta);
1394	if (uap->olddelta && error == 0) {
1395		CP(olddelta, tv32, tv_sec);
1396		CP(olddelta, tv32, tv_usec);
1397		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1398	}
1399	return (error);
1400}
1401
1402#ifdef COMPAT_FREEBSD4
1403int
1404freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1405{
1406	struct statfs32 s32;
1407	struct statfs s;
1408	int error;
1409
1410	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1411	if (error)
1412		return (error);
1413	copy_statfs(&s, &s32);
1414	return (copyout(&s32, uap->buf, sizeof(s32)));
1415}
1416#endif
1417
1418#ifdef COMPAT_FREEBSD4
1419int
1420freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1421{
1422	struct statfs32 s32;
1423	struct statfs s;
1424	int error;
1425
1426	error = kern_fstatfs(td, uap->fd, &s);
1427	if (error)
1428		return (error);
1429	copy_statfs(&s, &s32);
1430	return (copyout(&s32, uap->buf, sizeof(s32)));
1431}
1432#endif
1433
1434#ifdef COMPAT_FREEBSD4
1435int
1436freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1437{
1438	struct statfs32 s32;
1439	struct statfs s;
1440	fhandle_t fh;
1441	int error;
1442
1443	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1444		return (error);
1445	error = kern_fhstatfs(td, fh, &s);
1446	if (error)
1447		return (error);
1448	copy_statfs(&s, &s32);
1449	return (copyout(&s32, uap->buf, sizeof(s32)));
1450}
1451#endif
1452
1453int
1454freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1455{
1456	struct pread_args ap;
1457
1458	ap.fd = uap->fd;
1459	ap.buf = uap->buf;
1460	ap.nbyte = uap->nbyte;
1461	ap.offset = PAIR32TO64(off_t,uap->offset);
1462	return (sys_pread(td, &ap));
1463}
1464
1465int
1466freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1467{
1468	struct pwrite_args ap;
1469
1470	ap.fd = uap->fd;
1471	ap.buf = uap->buf;
1472	ap.nbyte = uap->nbyte;
1473	ap.offset = PAIR32TO64(off_t,uap->offset);
1474	return (sys_pwrite(td, &ap));
1475}
1476
1477#ifdef COMPAT_43
1478int
1479ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1480{
1481	struct lseek_args nuap;
1482
1483	nuap.fd = uap->fd;
1484	nuap.offset = uap->offset;
1485	nuap.whence = uap->whence;
1486	return (sys_lseek(td, &nuap));
1487}
1488#endif
1489
1490int
1491freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1492{
1493	int error;
1494	struct lseek_args ap;
1495	off_t pos;
1496
1497	ap.fd = uap->fd;
1498	ap.offset = PAIR32TO64(off_t,uap->offset);
1499	ap.whence = uap->whence;
1500	error = sys_lseek(td, &ap);
1501	/* Expand the quad return into two parts for eax and edx */
1502	pos = *(off_t *)(td->td_retval);
1503	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1504	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1505	return error;
1506}
1507
1508int
1509freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1510{
1511	struct truncate_args ap;
1512
1513	ap.path = uap->path;
1514	ap.length = PAIR32TO64(off_t,uap->length);
1515	return (sys_truncate(td, &ap));
1516}
1517
1518int
1519freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1520{
1521	struct ftruncate_args ap;
1522
1523	ap.fd = uap->fd;
1524	ap.length = PAIR32TO64(off_t,uap->length);
1525	return (sys_ftruncate(td, &ap));
1526}
1527
1528#ifdef COMPAT_43
1529int
1530ofreebsd32_getdirentries(struct thread *td,
1531    struct ofreebsd32_getdirentries_args *uap)
1532{
1533	struct ogetdirentries_args ap;
1534	int error;
1535	long loff;
1536	int32_t loff_cut;
1537
1538	ap.fd = uap->fd;
1539	ap.buf = uap->buf;
1540	ap.count = uap->count;
1541	ap.basep = NULL;
1542	error = kern_ogetdirentries(td, &ap, &loff);
1543	if (error == 0) {
1544		loff_cut = loff;
1545		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1546	}
1547	return (error);
1548}
1549#endif
1550
1551int
1552freebsd32_getdirentries(struct thread *td,
1553    struct freebsd32_getdirentries_args *uap)
1554{
1555	long base;
1556	int32_t base32;
1557	int error;
1558
1559	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1560	    NULL, UIO_USERSPACE);
1561	if (error)
1562		return (error);
1563	if (uap->basep != NULL) {
1564		base32 = base;
1565		error = copyout(&base32, uap->basep, sizeof(int32_t));
1566	}
1567	return (error);
1568}
1569
1570#ifdef COMPAT_FREEBSD6
1571/* versions with the 'int pad' argument */
1572int
1573freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1574{
1575	struct pread_args ap;
1576
1577	ap.fd = uap->fd;
1578	ap.buf = uap->buf;
1579	ap.nbyte = uap->nbyte;
1580	ap.offset = PAIR32TO64(off_t,uap->offset);
1581	return (sys_pread(td, &ap));
1582}
1583
1584int
1585freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1586{
1587	struct pwrite_args ap;
1588
1589	ap.fd = uap->fd;
1590	ap.buf = uap->buf;
1591	ap.nbyte = uap->nbyte;
1592	ap.offset = PAIR32TO64(off_t,uap->offset);
1593	return (sys_pwrite(td, &ap));
1594}
1595
1596int
1597freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1598{
1599	int error;
1600	struct lseek_args ap;
1601	off_t pos;
1602
1603	ap.fd = uap->fd;
1604	ap.offset = PAIR32TO64(off_t,uap->offset);
1605	ap.whence = uap->whence;
1606	error = sys_lseek(td, &ap);
1607	/* Expand the quad return into two parts for eax and edx */
1608	pos = *(off_t *)(td->td_retval);
1609	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1610	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1611	return error;
1612}
1613
1614int
1615freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1616{
1617	struct truncate_args ap;
1618
1619	ap.path = uap->path;
1620	ap.length = PAIR32TO64(off_t,uap->length);
1621	return (sys_truncate(td, &ap));
1622}
1623
1624int
1625freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1626{
1627	struct ftruncate_args ap;
1628
1629	ap.fd = uap->fd;
1630	ap.length = PAIR32TO64(off_t,uap->length);
1631	return (sys_ftruncate(td, &ap));
1632}
1633#endif /* COMPAT_FREEBSD6 */
1634
1635struct sf_hdtr32 {
1636	uint32_t headers;
1637	int hdr_cnt;
1638	uint32_t trailers;
1639	int trl_cnt;
1640};
1641
1642static int
1643freebsd32_do_sendfile(struct thread *td,
1644    struct freebsd32_sendfile_args *uap, int compat)
1645{
1646	struct sf_hdtr32 hdtr32;
1647	struct sf_hdtr hdtr;
1648	struct uio *hdr_uio, *trl_uio;
1649	struct iovec32 *iov32;
1650	struct file *fp;
1651	cap_rights_t rights;
1652	off_t offset;
1653	int error;
1654
1655	offset = PAIR32TO64(off_t, uap->offset);
1656	if (offset < 0)
1657		return (EINVAL);
1658
1659	hdr_uio = trl_uio = NULL;
1660
1661	if (uap->hdtr != NULL) {
1662		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1663		if (error)
1664			goto out;
1665		PTRIN_CP(hdtr32, hdtr, headers);
1666		CP(hdtr32, hdtr, hdr_cnt);
1667		PTRIN_CP(hdtr32, hdtr, trailers);
1668		CP(hdtr32, hdtr, trl_cnt);
1669
1670		if (hdtr.headers != NULL) {
1671			iov32 = PTRIN(hdtr32.headers);
1672			error = freebsd32_copyinuio(iov32,
1673			    hdtr32.hdr_cnt, &hdr_uio);
1674			if (error)
1675				goto out;
1676		}
1677		if (hdtr.trailers != NULL) {
1678			iov32 = PTRIN(hdtr32.trailers);
1679			error = freebsd32_copyinuio(iov32,
1680			    hdtr32.trl_cnt, &trl_uio);
1681			if (error)
1682				goto out;
1683		}
1684	}
1685
1686	AUDIT_ARG_FD(uap->fd);
1687
1688	if ((error = fget_read(td, uap->fd,
1689	    cap_rights_init(&rights, CAP_PREAD), &fp)) != 0) {
1690		goto out;
1691	}
1692
1693	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1694	    uap->nbytes, uap->sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1695	fdrop(fp, td);
1696
1697out:
1698	if (hdr_uio)
1699		free(hdr_uio, M_IOV);
1700	if (trl_uio)
1701		free(trl_uio, M_IOV);
1702	return (error);
1703}
1704
1705#ifdef COMPAT_FREEBSD4
1706int
1707freebsd4_freebsd32_sendfile(struct thread *td,
1708    struct freebsd4_freebsd32_sendfile_args *uap)
1709{
1710	return (freebsd32_do_sendfile(td,
1711	    (struct freebsd32_sendfile_args *)uap, 1));
1712}
1713#endif
1714
1715int
1716freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1717{
1718
1719	return (freebsd32_do_sendfile(td, uap, 0));
1720}
1721
1722static void
1723copy_stat(struct stat *in, struct stat32 *out)
1724{
1725
1726	CP(*in, *out, st_dev);
1727	CP(*in, *out, st_ino);
1728	CP(*in, *out, st_mode);
1729	CP(*in, *out, st_nlink);
1730	CP(*in, *out, st_uid);
1731	CP(*in, *out, st_gid);
1732	CP(*in, *out, st_rdev);
1733	TS_CP(*in, *out, st_atim);
1734	TS_CP(*in, *out, st_mtim);
1735	TS_CP(*in, *out, st_ctim);
1736	CP(*in, *out, st_size);
1737	CP(*in, *out, st_blocks);
1738	CP(*in, *out, st_blksize);
1739	CP(*in, *out, st_flags);
1740	CP(*in, *out, st_gen);
1741	TS_CP(*in, *out, st_birthtim);
1742}
1743
1744#ifdef COMPAT_43
1745static void
1746copy_ostat(struct stat *in, struct ostat32 *out)
1747{
1748
1749	CP(*in, *out, st_dev);
1750	CP(*in, *out, st_ino);
1751	CP(*in, *out, st_mode);
1752	CP(*in, *out, st_nlink);
1753	CP(*in, *out, st_uid);
1754	CP(*in, *out, st_gid);
1755	CP(*in, *out, st_rdev);
1756	CP(*in, *out, st_size);
1757	TS_CP(*in, *out, st_atim);
1758	TS_CP(*in, *out, st_mtim);
1759	TS_CP(*in, *out, st_ctim);
1760	CP(*in, *out, st_blksize);
1761	CP(*in, *out, st_blocks);
1762	CP(*in, *out, st_flags);
1763	CP(*in, *out, st_gen);
1764}
1765#endif
1766
1767int
1768freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1769{
1770	struct stat sb;
1771	struct stat32 sb32;
1772	int error;
1773
1774	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1775	if (error)
1776		return (error);
1777	copy_stat(&sb, &sb32);
1778	error = copyout(&sb32, uap->ub, sizeof (sb32));
1779	return (error);
1780}
1781
1782#ifdef COMPAT_43
1783int
1784ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1785{
1786	struct stat sb;
1787	struct ostat32 sb32;
1788	int error;
1789
1790	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1791	if (error)
1792		return (error);
1793	copy_ostat(&sb, &sb32);
1794	error = copyout(&sb32, uap->ub, sizeof (sb32));
1795	return (error);
1796}
1797#endif
1798
1799int
1800freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1801{
1802	struct stat ub;
1803	struct stat32 ub32;
1804	int error;
1805
1806	error = kern_fstat(td, uap->fd, &ub);
1807	if (error)
1808		return (error);
1809	copy_stat(&ub, &ub32);
1810	error = copyout(&ub32, uap->ub, sizeof(ub32));
1811	return (error);
1812}
1813
1814#ifdef COMPAT_43
1815int
1816ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1817{
1818	struct stat ub;
1819	struct ostat32 ub32;
1820	int error;
1821
1822	error = kern_fstat(td, uap->fd, &ub);
1823	if (error)
1824		return (error);
1825	copy_ostat(&ub, &ub32);
1826	error = copyout(&ub32, uap->ub, sizeof(ub32));
1827	return (error);
1828}
1829#endif
1830
1831int
1832freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1833{
1834	struct stat ub;
1835	struct stat32 ub32;
1836	int error;
1837
1838	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1839	if (error)
1840		return (error);
1841	copy_stat(&ub, &ub32);
1842	error = copyout(&ub32, uap->buf, sizeof(ub32));
1843	return (error);
1844}
1845
1846int
1847freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1848{
1849	struct stat sb;
1850	struct stat32 sb32;
1851	int error;
1852
1853	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1854	if (error)
1855		return (error);
1856	copy_stat(&sb, &sb32);
1857	error = copyout(&sb32, uap->ub, sizeof (sb32));
1858	return (error);
1859}
1860
1861#ifdef COMPAT_43
1862int
1863ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1864{
1865	struct stat sb;
1866	struct ostat32 sb32;
1867	int error;
1868
1869	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1870	if (error)
1871		return (error);
1872	copy_ostat(&sb, &sb32);
1873	error = copyout(&sb32, uap->ub, sizeof (sb32));
1874	return (error);
1875}
1876#endif
1877
1878int
1879freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1880{
1881	int error, name[CTL_MAXNAME];
1882	size_t j, oldlen;
1883
1884	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1885		return (EINVAL);
1886 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1887 	if (error)
1888		return (error);
1889	if (uap->oldlenp)
1890		oldlen = fuword32(uap->oldlenp);
1891	else
1892		oldlen = 0;
1893	error = userland_sysctl(td, name, uap->namelen,
1894		uap->old, &oldlen, 1,
1895		uap->new, uap->newlen, &j, SCTL_MASK32);
1896	if (error && error != ENOMEM)
1897		return (error);
1898	if (uap->oldlenp)
1899		suword32(uap->oldlenp, j);
1900	return (0);
1901}
1902
1903int
1904freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1905{
1906	uint32_t version;
1907	int error;
1908	struct jail j;
1909
1910	error = copyin(uap->jail, &version, sizeof(uint32_t));
1911	if (error)
1912		return (error);
1913
1914	switch (version) {
1915	case 0:
1916	{
1917		/* FreeBSD single IPv4 jails. */
1918		struct jail32_v0 j32_v0;
1919
1920		bzero(&j, sizeof(struct jail));
1921		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1922		if (error)
1923			return (error);
1924		CP(j32_v0, j, version);
1925		PTRIN_CP(j32_v0, j, path);
1926		PTRIN_CP(j32_v0, j, hostname);
1927		j.ip4s = j32_v0.ip_number;
1928		break;
1929	}
1930
1931	case 1:
1932		/*
1933		 * Version 1 was used by multi-IPv4 jail implementations
1934		 * that never made it into the official kernel.
1935		 */
1936		return (EINVAL);
1937
1938	case 2:	/* JAIL_API_VERSION */
1939	{
1940		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
1941		struct jail32 j32;
1942
1943		error = copyin(uap->jail, &j32, sizeof(struct jail32));
1944		if (error)
1945			return (error);
1946		CP(j32, j, version);
1947		PTRIN_CP(j32, j, path);
1948		PTRIN_CP(j32, j, hostname);
1949		PTRIN_CP(j32, j, jailname);
1950		CP(j32, j, ip4s);
1951		CP(j32, j, ip6s);
1952		PTRIN_CP(j32, j, ip4);
1953		PTRIN_CP(j32, j, ip6);
1954		break;
1955	}
1956
1957	default:
1958		/* Sci-Fi jails are not supported, sorry. */
1959		return (EINVAL);
1960	}
1961	return (kern_jail(td, &j));
1962}
1963
1964int
1965freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1966{
1967	struct uio *auio;
1968	int error;
1969
1970	/* Check that we have an even number of iovecs. */
1971	if (uap->iovcnt & 1)
1972		return (EINVAL);
1973
1974	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1975	if (error)
1976		return (error);
1977	error = kern_jail_set(td, auio, uap->flags);
1978	free(auio, M_IOV);
1979	return (error);
1980}
1981
1982int
1983freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1984{
1985	struct iovec32 iov32;
1986	struct uio *auio;
1987	int error, i;
1988
1989	/* Check that we have an even number of iovecs. */
1990	if (uap->iovcnt & 1)
1991		return (EINVAL);
1992
1993	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1994	if (error)
1995		return (error);
1996	error = kern_jail_get(td, auio, uap->flags);
1997	if (error == 0)
1998		for (i = 0; i < uap->iovcnt; i++) {
1999			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2000			CP(auio->uio_iov[i], iov32, iov_len);
2001			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2002			if (error != 0)
2003				break;
2004		}
2005	free(auio, M_IOV);
2006	return (error);
2007}
2008
2009int
2010freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2011{
2012	struct sigaction32 s32;
2013	struct sigaction sa, osa, *sap;
2014	int error;
2015
2016	if (uap->act) {
2017		error = copyin(uap->act, &s32, sizeof(s32));
2018		if (error)
2019			return (error);
2020		sa.sa_handler = PTRIN(s32.sa_u);
2021		CP(s32, sa, sa_flags);
2022		CP(s32, sa, sa_mask);
2023		sap = &sa;
2024	} else
2025		sap = NULL;
2026	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2027	if (error == 0 && uap->oact != NULL) {
2028		s32.sa_u = PTROUT(osa.sa_handler);
2029		CP(osa, s32, sa_flags);
2030		CP(osa, s32, sa_mask);
2031		error = copyout(&s32, uap->oact, sizeof(s32));
2032	}
2033	return (error);
2034}
2035
2036#ifdef COMPAT_FREEBSD4
2037int
2038freebsd4_freebsd32_sigaction(struct thread *td,
2039			     struct freebsd4_freebsd32_sigaction_args *uap)
2040{
2041	struct sigaction32 s32;
2042	struct sigaction sa, osa, *sap;
2043	int error;
2044
2045	if (uap->act) {
2046		error = copyin(uap->act, &s32, sizeof(s32));
2047		if (error)
2048			return (error);
2049		sa.sa_handler = PTRIN(s32.sa_u);
2050		CP(s32, sa, sa_flags);
2051		CP(s32, sa, sa_mask);
2052		sap = &sa;
2053	} else
2054		sap = NULL;
2055	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2056	if (error == 0 && uap->oact != NULL) {
2057		s32.sa_u = PTROUT(osa.sa_handler);
2058		CP(osa, s32, sa_flags);
2059		CP(osa, s32, sa_mask);
2060		error = copyout(&s32, uap->oact, sizeof(s32));
2061	}
2062	return (error);
2063}
2064#endif
2065
2066#ifdef COMPAT_43
2067struct osigaction32 {
2068	u_int32_t	sa_u;
2069	osigset_t	sa_mask;
2070	int		sa_flags;
2071};
2072
2073#define	ONSIG	32
2074
2075int
2076ofreebsd32_sigaction(struct thread *td,
2077			     struct ofreebsd32_sigaction_args *uap)
2078{
2079	struct osigaction32 s32;
2080	struct sigaction sa, osa, *sap;
2081	int error;
2082
2083	if (uap->signum <= 0 || uap->signum >= ONSIG)
2084		return (EINVAL);
2085
2086	if (uap->nsa) {
2087		error = copyin(uap->nsa, &s32, sizeof(s32));
2088		if (error)
2089			return (error);
2090		sa.sa_handler = PTRIN(s32.sa_u);
2091		CP(s32, sa, sa_flags);
2092		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2093		sap = &sa;
2094	} else
2095		sap = NULL;
2096	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2097	if (error == 0 && uap->osa != NULL) {
2098		s32.sa_u = PTROUT(osa.sa_handler);
2099		CP(osa, s32, sa_flags);
2100		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2101		error = copyout(&s32, uap->osa, sizeof(s32));
2102	}
2103	return (error);
2104}
2105
2106int
2107ofreebsd32_sigprocmask(struct thread *td,
2108			       struct ofreebsd32_sigprocmask_args *uap)
2109{
2110	sigset_t set, oset;
2111	int error;
2112
2113	OSIG2SIG(uap->mask, set);
2114	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2115	SIG2OSIG(oset, td->td_retval[0]);
2116	return (error);
2117}
2118
2119int
2120ofreebsd32_sigpending(struct thread *td,
2121			      struct ofreebsd32_sigpending_args *uap)
2122{
2123	struct proc *p = td->td_proc;
2124	sigset_t siglist;
2125
2126	PROC_LOCK(p);
2127	siglist = p->p_siglist;
2128	SIGSETOR(siglist, td->td_siglist);
2129	PROC_UNLOCK(p);
2130	SIG2OSIG(siglist, td->td_retval[0]);
2131	return (0);
2132}
2133
2134struct sigvec32 {
2135	u_int32_t	sv_handler;
2136	int		sv_mask;
2137	int		sv_flags;
2138};
2139
2140int
2141ofreebsd32_sigvec(struct thread *td,
2142			  struct ofreebsd32_sigvec_args *uap)
2143{
2144	struct sigvec32 vec;
2145	struct sigaction sa, osa, *sap;
2146	int error;
2147
2148	if (uap->signum <= 0 || uap->signum >= ONSIG)
2149		return (EINVAL);
2150
2151	if (uap->nsv) {
2152		error = copyin(uap->nsv, &vec, sizeof(vec));
2153		if (error)
2154			return (error);
2155		sa.sa_handler = PTRIN(vec.sv_handler);
2156		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2157		sa.sa_flags = vec.sv_flags;
2158		sa.sa_flags ^= SA_RESTART;
2159		sap = &sa;
2160	} else
2161		sap = NULL;
2162	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2163	if (error == 0 && uap->osv != NULL) {
2164		vec.sv_handler = PTROUT(osa.sa_handler);
2165		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2166		vec.sv_flags = osa.sa_flags;
2167		vec.sv_flags &= ~SA_NOCLDWAIT;
2168		vec.sv_flags ^= SA_RESTART;
2169		error = copyout(&vec, uap->osv, sizeof(vec));
2170	}
2171	return (error);
2172}
2173
2174int
2175ofreebsd32_sigblock(struct thread *td,
2176			    struct ofreebsd32_sigblock_args *uap)
2177{
2178	sigset_t set, oset;
2179
2180	OSIG2SIG(uap->mask, set);
2181	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2182	SIG2OSIG(oset, td->td_retval[0]);
2183	return (0);
2184}
2185
2186int
2187ofreebsd32_sigsetmask(struct thread *td,
2188			      struct ofreebsd32_sigsetmask_args *uap)
2189{
2190	sigset_t set, oset;
2191
2192	OSIG2SIG(uap->mask, set);
2193	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2194	SIG2OSIG(oset, td->td_retval[0]);
2195	return (0);
2196}
2197
2198int
2199ofreebsd32_sigsuspend(struct thread *td,
2200			      struct ofreebsd32_sigsuspend_args *uap)
2201{
2202	sigset_t mask;
2203
2204	OSIG2SIG(uap->mask, mask);
2205	return (kern_sigsuspend(td, mask));
2206}
2207
2208struct sigstack32 {
2209	u_int32_t	ss_sp;
2210	int		ss_onstack;
2211};
2212
2213int
2214ofreebsd32_sigstack(struct thread *td,
2215			    struct ofreebsd32_sigstack_args *uap)
2216{
2217	struct sigstack32 s32;
2218	struct sigstack nss, oss;
2219	int error = 0, unss;
2220
2221	if (uap->nss != NULL) {
2222		error = copyin(uap->nss, &s32, sizeof(s32));
2223		if (error)
2224			return (error);
2225		nss.ss_sp = PTRIN(s32.ss_sp);
2226		CP(s32, nss, ss_onstack);
2227		unss = 1;
2228	} else {
2229		unss = 0;
2230	}
2231	oss.ss_sp = td->td_sigstk.ss_sp;
2232	oss.ss_onstack = sigonstack(cpu_getstack(td));
2233	if (unss) {
2234		td->td_sigstk.ss_sp = nss.ss_sp;
2235		td->td_sigstk.ss_size = 0;
2236		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2237		td->td_pflags |= TDP_ALTSTACK;
2238	}
2239	if (uap->oss != NULL) {
2240		s32.ss_sp = PTROUT(oss.ss_sp);
2241		CP(oss, s32, ss_onstack);
2242		error = copyout(&s32, uap->oss, sizeof(s32));
2243	}
2244	return (error);
2245}
2246#endif
2247
2248int
2249freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2250{
2251	struct timespec32 rmt32, rqt32;
2252	struct timespec rmt, rqt;
2253	int error;
2254
2255	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2256	if (error)
2257		return (error);
2258
2259	CP(rqt32, rqt, tv_sec);
2260	CP(rqt32, rqt, tv_nsec);
2261
2262	if (uap->rmtp &&
2263	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2264		return (EFAULT);
2265	error = kern_nanosleep(td, &rqt, &rmt);
2266	if (error && uap->rmtp) {
2267		int error2;
2268
2269		CP(rmt, rmt32, tv_sec);
2270		CP(rmt, rmt32, tv_nsec);
2271
2272		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2273		if (error2)
2274			error = error2;
2275	}
2276	return (error);
2277}
2278
2279int
2280freebsd32_clock_gettime(struct thread *td,
2281			struct freebsd32_clock_gettime_args *uap)
2282{
2283	struct timespec	ats;
2284	struct timespec32 ats32;
2285	int error;
2286
2287	error = kern_clock_gettime(td, uap->clock_id, &ats);
2288	if (error == 0) {
2289		CP(ats, ats32, tv_sec);
2290		CP(ats, ats32, tv_nsec);
2291		error = copyout(&ats32, uap->tp, sizeof(ats32));
2292	}
2293	return (error);
2294}
2295
2296int
2297freebsd32_clock_settime(struct thread *td,
2298			struct freebsd32_clock_settime_args *uap)
2299{
2300	struct timespec	ats;
2301	struct timespec32 ats32;
2302	int error;
2303
2304	error = copyin(uap->tp, &ats32, sizeof(ats32));
2305	if (error)
2306		return (error);
2307	CP(ats32, ats, tv_sec);
2308	CP(ats32, ats, tv_nsec);
2309
2310	return (kern_clock_settime(td, uap->clock_id, &ats));
2311}
2312
2313int
2314freebsd32_clock_getres(struct thread *td,
2315		       struct freebsd32_clock_getres_args *uap)
2316{
2317	struct timespec	ts;
2318	struct timespec32 ts32;
2319	int error;
2320
2321	if (uap->tp == NULL)
2322		return (0);
2323	error = kern_clock_getres(td, uap->clock_id, &ts);
2324	if (error == 0) {
2325		CP(ts, ts32, tv_sec);
2326		CP(ts, ts32, tv_nsec);
2327		error = copyout(&ts32, uap->tp, sizeof(ts32));
2328	}
2329	return (error);
2330}
2331
2332int freebsd32_ktimer_create(struct thread *td,
2333    struct freebsd32_ktimer_create_args *uap)
2334{
2335	struct sigevent32 ev32;
2336	struct sigevent ev, *evp;
2337	int error, id;
2338
2339	if (uap->evp == NULL) {
2340		evp = NULL;
2341	} else {
2342		evp = &ev;
2343		error = copyin(uap->evp, &ev32, sizeof(ev32));
2344		if (error != 0)
2345			return (error);
2346		error = convert_sigevent32(&ev32, &ev);
2347		if (error != 0)
2348			return (error);
2349	}
2350	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2351	if (error == 0) {
2352		error = copyout(&id, uap->timerid, sizeof(int));
2353		if (error != 0)
2354			kern_ktimer_delete(td, id);
2355	}
2356	return (error);
2357}
2358
2359int
2360freebsd32_ktimer_settime(struct thread *td,
2361    struct freebsd32_ktimer_settime_args *uap)
2362{
2363	struct itimerspec32 val32, oval32;
2364	struct itimerspec val, oval, *ovalp;
2365	int error;
2366
2367	error = copyin(uap->value, &val32, sizeof(val32));
2368	if (error != 0)
2369		return (error);
2370	ITS_CP(val32, val);
2371	ovalp = uap->ovalue != NULL ? &oval : NULL;
2372	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2373	if (error == 0 && uap->ovalue != NULL) {
2374		ITS_CP(oval, oval32);
2375		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2376	}
2377	return (error);
2378}
2379
2380int
2381freebsd32_ktimer_gettime(struct thread *td,
2382    struct freebsd32_ktimer_gettime_args *uap)
2383{
2384	struct itimerspec32 val32;
2385	struct itimerspec val;
2386	int error;
2387
2388	error = kern_ktimer_gettime(td, uap->timerid, &val);
2389	if (error == 0) {
2390		ITS_CP(val, val32);
2391		error = copyout(&val32, uap->value, sizeof(val32));
2392	}
2393	return (error);
2394}
2395
2396int
2397freebsd32_clock_getcpuclockid2(struct thread *td,
2398    struct freebsd32_clock_getcpuclockid2_args *uap)
2399{
2400	clockid_t clk_id;
2401	int error;
2402
2403	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2404	    uap->which, &clk_id);
2405	if (error == 0)
2406		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2407	return (error);
2408}
2409
2410int
2411freebsd32_thr_new(struct thread *td,
2412		  struct freebsd32_thr_new_args *uap)
2413{
2414	struct thr_param32 param32;
2415	struct thr_param param;
2416	int error;
2417
2418	if (uap->param_size < 0 ||
2419	    uap->param_size > sizeof(struct thr_param32))
2420		return (EINVAL);
2421	bzero(&param, sizeof(struct thr_param));
2422	bzero(&param32, sizeof(struct thr_param32));
2423	error = copyin(uap->param, &param32, uap->param_size);
2424	if (error != 0)
2425		return (error);
2426	param.start_func = PTRIN(param32.start_func);
2427	param.arg = PTRIN(param32.arg);
2428	param.stack_base = PTRIN(param32.stack_base);
2429	param.stack_size = param32.stack_size;
2430	param.tls_base = PTRIN(param32.tls_base);
2431	param.tls_size = param32.tls_size;
2432	param.child_tid = PTRIN(param32.child_tid);
2433	param.parent_tid = PTRIN(param32.parent_tid);
2434	param.flags = param32.flags;
2435	param.rtp = PTRIN(param32.rtp);
2436	param.spare[0] = PTRIN(param32.spare[0]);
2437	param.spare[1] = PTRIN(param32.spare[1]);
2438	param.spare[2] = PTRIN(param32.spare[2]);
2439
2440	return (kern_thr_new(td, &param));
2441}
2442
2443int
2444freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2445{
2446	struct timespec32 ts32;
2447	struct timespec ts, *tsp;
2448	int error;
2449
2450	error = 0;
2451	tsp = NULL;
2452	if (uap->timeout != NULL) {
2453		error = copyin((const void *)uap->timeout, (void *)&ts32,
2454		    sizeof(struct timespec32));
2455		if (error != 0)
2456			return (error);
2457		ts.tv_sec = ts32.tv_sec;
2458		ts.tv_nsec = ts32.tv_nsec;
2459		tsp = &ts;
2460	}
2461	return (kern_thr_suspend(td, tsp));
2462}
2463
2464void
2465siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2466{
2467	bzero(dst, sizeof(*dst));
2468	dst->si_signo = src->si_signo;
2469	dst->si_errno = src->si_errno;
2470	dst->si_code = src->si_code;
2471	dst->si_pid = src->si_pid;
2472	dst->si_uid = src->si_uid;
2473	dst->si_status = src->si_status;
2474	dst->si_addr = (uintptr_t)src->si_addr;
2475	dst->si_value.sival_int = src->si_value.sival_int;
2476	dst->si_timerid = src->si_timerid;
2477	dst->si_overrun = src->si_overrun;
2478}
2479
2480int
2481freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2482{
2483	struct timespec32 ts32;
2484	struct timespec ts;
2485	struct timespec *timeout;
2486	sigset_t set;
2487	ksiginfo_t ksi;
2488	struct siginfo32 si32;
2489	int error;
2490
2491	if (uap->timeout) {
2492		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2493		if (error)
2494			return (error);
2495		ts.tv_sec = ts32.tv_sec;
2496		ts.tv_nsec = ts32.tv_nsec;
2497		timeout = &ts;
2498	} else
2499		timeout = NULL;
2500
2501	error = copyin(uap->set, &set, sizeof(set));
2502	if (error)
2503		return (error);
2504
2505	error = kern_sigtimedwait(td, set, &ksi, timeout);
2506	if (error)
2507		return (error);
2508
2509	if (uap->info) {
2510		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2511		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2512	}
2513
2514	if (error == 0)
2515		td->td_retval[0] = ksi.ksi_signo;
2516	return (error);
2517}
2518
2519/*
2520 * MPSAFE
2521 */
2522int
2523freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2524{
2525	ksiginfo_t ksi;
2526	struct siginfo32 si32;
2527	sigset_t set;
2528	int error;
2529
2530	error = copyin(uap->set, &set, sizeof(set));
2531	if (error)
2532		return (error);
2533
2534	error = kern_sigtimedwait(td, set, &ksi, NULL);
2535	if (error)
2536		return (error);
2537
2538	if (uap->info) {
2539		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2540		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2541	}
2542	if (error == 0)
2543		td->td_retval[0] = ksi.ksi_signo;
2544	return (error);
2545}
2546
2547int
2548freebsd32_cpuset_setid(struct thread *td,
2549    struct freebsd32_cpuset_setid_args *uap)
2550{
2551	struct cpuset_setid_args ap;
2552
2553	ap.which = uap->which;
2554	ap.id = PAIR32TO64(id_t,uap->id);
2555	ap.setid = uap->setid;
2556
2557	return (sys_cpuset_setid(td, &ap));
2558}
2559
2560int
2561freebsd32_cpuset_getid(struct thread *td,
2562    struct freebsd32_cpuset_getid_args *uap)
2563{
2564	struct cpuset_getid_args ap;
2565
2566	ap.level = uap->level;
2567	ap.which = uap->which;
2568	ap.id = PAIR32TO64(id_t,uap->id);
2569	ap.setid = uap->setid;
2570
2571	return (sys_cpuset_getid(td, &ap));
2572}
2573
2574int
2575freebsd32_cpuset_getaffinity(struct thread *td,
2576    struct freebsd32_cpuset_getaffinity_args *uap)
2577{
2578	struct cpuset_getaffinity_args ap;
2579
2580	ap.level = uap->level;
2581	ap.which = uap->which;
2582	ap.id = PAIR32TO64(id_t,uap->id);
2583	ap.cpusetsize = uap->cpusetsize;
2584	ap.mask = uap->mask;
2585
2586	return (sys_cpuset_getaffinity(td, &ap));
2587}
2588
2589int
2590freebsd32_cpuset_setaffinity(struct thread *td,
2591    struct freebsd32_cpuset_setaffinity_args *uap)
2592{
2593	struct cpuset_setaffinity_args ap;
2594
2595	ap.level = uap->level;
2596	ap.which = uap->which;
2597	ap.id = PAIR32TO64(id_t,uap->id);
2598	ap.cpusetsize = uap->cpusetsize;
2599	ap.mask = uap->mask;
2600
2601	return (sys_cpuset_setaffinity(td, &ap));
2602}
2603
2604int
2605freebsd32_nmount(struct thread *td,
2606    struct freebsd32_nmount_args /* {
2607    	struct iovec *iovp;
2608    	unsigned int iovcnt;
2609    	int flags;
2610    } */ *uap)
2611{
2612	struct uio *auio;
2613	uint64_t flags;
2614	int error;
2615
2616	/*
2617	 * Mount flags are now 64-bits. On 32-bit archtectures only
2618	 * 32-bits are passed in, but from here on everything handles
2619	 * 64-bit flags correctly.
2620	 */
2621	flags = uap->flags;
2622
2623	AUDIT_ARG_FFLAGS(flags);
2624
2625	/*
2626	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2627	 * userspace to set this flag, but we must filter it out if we want
2628	 * MNT_UPDATE on the root file system to work.
2629	 * MNT_ROOTFS should only be set by the kernel when mounting its
2630	 * root file system.
2631	 */
2632	flags &= ~MNT_ROOTFS;
2633
2634	/*
2635	 * check that we have an even number of iovec's
2636	 * and that we have at least two options.
2637	 */
2638	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2639		return (EINVAL);
2640
2641	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2642	if (error)
2643		return (error);
2644	error = vfs_donmount(td, flags, auio);
2645
2646	free(auio, M_IOV);
2647	return error;
2648}
2649
2650#if 0
2651int
2652freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2653{
2654	struct yyy32 *p32, s32;
2655	struct yyy *p = NULL, s;
2656	struct xxx_arg ap;
2657	int error;
2658
2659	if (uap->zzz) {
2660		error = copyin(uap->zzz, &s32, sizeof(s32));
2661		if (error)
2662			return (error);
2663		/* translate in */
2664		p = &s;
2665	}
2666	error = kern_xxx(td, p);
2667	if (error)
2668		return (error);
2669	if (uap->zzz) {
2670		/* translate out */
2671		error = copyout(&s32, p32, sizeof(s32));
2672	}
2673	return (error);
2674}
2675#endif
2676
2677int
2678syscall32_register(int *offset, struct sysent *new_sysent,
2679    struct sysent *old_sysent)
2680{
2681	if (*offset == NO_SYSCALL) {
2682		int i;
2683
2684		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2685			if (freebsd32_sysent[i].sy_call ==
2686			    (sy_call_t *)lkmnosys)
2687				break;
2688		if (i == SYS_MAXSYSCALL)
2689			return (ENFILE);
2690		*offset = i;
2691	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2692		return (EINVAL);
2693	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2694	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2695		return (EEXIST);
2696
2697	*old_sysent = freebsd32_sysent[*offset];
2698	freebsd32_sysent[*offset] = *new_sysent;
2699	return 0;
2700}
2701
2702int
2703syscall32_deregister(int *offset, struct sysent *old_sysent)
2704{
2705
2706	if (*offset)
2707		freebsd32_sysent[*offset] = *old_sysent;
2708	return 0;
2709}
2710
2711int
2712syscall32_module_handler(struct module *mod, int what, void *arg)
2713{
2714	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2715	modspecific_t ms;
2716	int error;
2717
2718	switch (what) {
2719	case MOD_LOAD:
2720		error = syscall32_register(data->offset, data->new_sysent,
2721		    &data->old_sysent);
2722		if (error) {
2723			/* Leave a mark so we know to safely unload below. */
2724			data->offset = NULL;
2725			return error;
2726		}
2727		ms.intval = *data->offset;
2728		MOD_XLOCK;
2729		module_setspecific(mod, &ms);
2730		MOD_XUNLOCK;
2731		if (data->chainevh)
2732			error = data->chainevh(mod, what, data->chainarg);
2733		return (error);
2734	case MOD_UNLOAD:
2735		/*
2736		 * MOD_LOAD failed, so just return without calling the
2737		 * chained handler since we didn't pass along the MOD_LOAD
2738		 * event.
2739		 */
2740		if (data->offset == NULL)
2741			return (0);
2742		if (data->chainevh) {
2743			error = data->chainevh(mod, what, data->chainarg);
2744			if (error)
2745				return (error);
2746		}
2747		error = syscall32_deregister(data->offset, &data->old_sysent);
2748		return (error);
2749	default:
2750		error = EOPNOTSUPP;
2751		if (data->chainevh)
2752			error = data->chainevh(mod, what, data->chainarg);
2753		return (error);
2754	}
2755}
2756
2757int
2758syscall32_helper_register(struct syscall_helper_data *sd)
2759{
2760	struct syscall_helper_data *sd1;
2761	int error;
2762
2763	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2764		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2765		    &sd1->old_sysent);
2766		if (error != 0) {
2767			syscall32_helper_unregister(sd);
2768			return (error);
2769		}
2770		sd1->registered = 1;
2771	}
2772	return (0);
2773}
2774
2775int
2776syscall32_helper_unregister(struct syscall_helper_data *sd)
2777{
2778	struct syscall_helper_data *sd1;
2779
2780	for (sd1 = sd; sd1->registered != 0; sd1++) {
2781		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2782		sd1->registered = 0;
2783	}
2784	return (0);
2785}
2786
2787register_t *
2788freebsd32_copyout_strings(struct image_params *imgp)
2789{
2790	int argc, envc, i;
2791	u_int32_t *vectp;
2792	char *stringp, *destp;
2793	u_int32_t *stack_base;
2794	struct freebsd32_ps_strings *arginfo;
2795	char canary[sizeof(long) * 8];
2796	int32_t pagesizes32[MAXPAGESIZES];
2797	size_t execpath_len;
2798	int szsigcode;
2799
2800	/*
2801	 * Calculate string base and vector table pointers.
2802	 * Also deal with signal trampoline code for this exec type.
2803	 */
2804	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2805		execpath_len = strlen(imgp->execpath) + 1;
2806	else
2807		execpath_len = 0;
2808	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2809	    sv_psstrings;
2810	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2811		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2812	else
2813		szsigcode = 0;
2814	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2815	    roundup(execpath_len, sizeof(char *)) -
2816	    roundup(sizeof(canary), sizeof(char *)) -
2817	    roundup(sizeof(pagesizes32), sizeof(char *)) -
2818	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2819
2820	/*
2821	 * install sigcode
2822	 */
2823	if (szsigcode != 0)
2824		copyout(imgp->proc->p_sysent->sv_sigcode,
2825			((caddr_t)arginfo - szsigcode), szsigcode);
2826
2827	/*
2828	 * Copy the image path for the rtld.
2829	 */
2830	if (execpath_len != 0) {
2831		imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2832		copyout(imgp->execpath, (void *)imgp->execpathp,
2833		    execpath_len);
2834	}
2835
2836	/*
2837	 * Prepare the canary for SSP.
2838	 */
2839	arc4rand(canary, sizeof(canary), 0);
2840	imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2841	    sizeof(canary);
2842	copyout(canary, (void *)imgp->canary, sizeof(canary));
2843	imgp->canarylen = sizeof(canary);
2844
2845	/*
2846	 * Prepare the pagesizes array.
2847	 */
2848	for (i = 0; i < MAXPAGESIZES; i++)
2849		pagesizes32[i] = (uint32_t)pagesizes[i];
2850	imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2851	    roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2852	copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2853	imgp->pagesizeslen = sizeof(pagesizes32);
2854
2855	/*
2856	 * If we have a valid auxargs ptr, prepare some room
2857	 * on the stack.
2858	 */
2859	if (imgp->auxargs) {
2860		/*
2861		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2862		 * lower compatibility.
2863		 */
2864		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2865			: (AT_COUNT * 2);
2866		/*
2867		 * The '+ 2' is for the null pointers at the end of each of
2868		 * the arg and env vector sets,and imgp->auxarg_size is room
2869		 * for argument of Runtime loader.
2870		 */
2871		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2872		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2873		    sizeof(u_int32_t));
2874	} else
2875		/*
2876		 * The '+ 2' is for the null pointers at the end of each of
2877		 * the arg and env vector sets
2878		 */
2879		vectp = (u_int32_t *)
2880			(destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2881
2882	/*
2883	 * vectp also becomes our initial stack base
2884	 */
2885	stack_base = vectp;
2886
2887	stringp = imgp->args->begin_argv;
2888	argc = imgp->args->argc;
2889	envc = imgp->args->envc;
2890	/*
2891	 * Copy out strings - arguments and environment.
2892	 */
2893	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2894
2895	/*
2896	 * Fill in "ps_strings" struct for ps, w, etc.
2897	 */
2898	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2899	suword32(&arginfo->ps_nargvstr, argc);
2900
2901	/*
2902	 * Fill in argument portion of vector table.
2903	 */
2904	for (; argc > 0; --argc) {
2905		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2906		while (*stringp++ != 0)
2907			destp++;
2908		destp++;
2909	}
2910
2911	/* a null vector table pointer separates the argp's from the envp's */
2912	suword32(vectp++, 0);
2913
2914	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2915	suword32(&arginfo->ps_nenvstr, envc);
2916
2917	/*
2918	 * Fill in environment portion of vector table.
2919	 */
2920	for (; envc > 0; --envc) {
2921		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2922		while (*stringp++ != 0)
2923			destp++;
2924		destp++;
2925	}
2926
2927	/* end of vector table is a null pointer */
2928	suword32(vectp, 0);
2929
2930	return ((register_t *)stack_base);
2931}
2932
2933int
2934freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2935{
2936	struct kld_file_stat stat;
2937	struct kld32_file_stat stat32;
2938	int error, version;
2939
2940	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2941	    != 0)
2942		return (error);
2943	if (version != sizeof(struct kld32_file_stat_1) &&
2944	    version != sizeof(struct kld32_file_stat))
2945		return (EINVAL);
2946
2947	error = kern_kldstat(td, uap->fileid, &stat);
2948	if (error != 0)
2949		return (error);
2950
2951	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2952	CP(stat, stat32, refs);
2953	CP(stat, stat32, id);
2954	PTROUT_CP(stat, stat32, address);
2955	CP(stat, stat32, size);
2956	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2957	return (copyout(&stat32, uap->stat, version));
2958}
2959
2960int
2961freebsd32_posix_fallocate(struct thread *td,
2962    struct freebsd32_posix_fallocate_args *uap)
2963{
2964
2965	return (kern_posix_fallocate(td, uap->fd,
2966	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2967}
2968
2969int
2970freebsd32_posix_fadvise(struct thread *td,
2971    struct freebsd32_posix_fadvise_args *uap)
2972{
2973
2974	return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2975	    PAIR32TO64(off_t, uap->len), uap->advice));
2976}
2977
2978int
2979convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2980{
2981
2982	CP(*sig32, *sig, sigev_notify);
2983	switch (sig->sigev_notify) {
2984	case SIGEV_NONE:
2985		break;
2986	case SIGEV_THREAD_ID:
2987		CP(*sig32, *sig, sigev_notify_thread_id);
2988		/* FALLTHROUGH */
2989	case SIGEV_SIGNAL:
2990		CP(*sig32, *sig, sigev_signo);
2991		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2992		break;
2993	case SIGEV_KEVENT:
2994		CP(*sig32, *sig, sigev_notify_kqueue);
2995		CP(*sig32, *sig, sigev_notify_kevent_flags);
2996		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2997		break;
2998	default:
2999		return (EINVAL);
3000	}
3001	return (0);
3002}
3003