freebsd32_misc.c revision 315551
1293531Sdchagin/*-
2293531Sdchagin * Copyright (c) 2002 Doug Rabson
3293531Sdchagin * All rights reserved.
4293531Sdchagin *
5293531Sdchagin * Redistribution and use in source and binary forms, with or without
6294369Sjhb * modification, are permitted provided that the following conditions
7293531Sdchagin * are met:
8293531Sdchagin * 1. Redistributions of source code must retain the above copyright
9293531Sdchagin *    notice, this list of conditions and the following disclaimer.
10293531Sdchagin * 2. Redistributions in binary form must reproduce the above copyright
11293531Sdchagin *    notice, this list of conditions and the following disclaimer in the
12293531Sdchagin *    documentation and/or other materials provided with the distribution.
13293531Sdchagin *
14293531Sdchagin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15293531Sdchagin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16293531Sdchagin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17293531Sdchagin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18293531Sdchagin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19293531Sdchagin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20293531Sdchagin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21293531Sdchagin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22293531Sdchagin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23293531Sdchagin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24293531Sdchagin * SUCH DAMAGE.
25293531Sdchagin */
26293531Sdchagin
27293531Sdchagin#include <sys/cdefs.h>
28293531Sdchagin__FBSDID("$FreeBSD: stable/11/sys/compat/freebsd32/freebsd32_misc.c 315551 2017-03-19 14:40:01Z trasz $");
29293531Sdchagin
30293531Sdchagin#include "opt_compat.h"
31293531Sdchagin#include "opt_inet.h"
32293531Sdchagin#include "opt_inet6.h"
33293531Sdchagin
34293531Sdchagin#define __ELF_WORD_SIZE 32
35293531Sdchagin
36293531Sdchagin#include <sys/param.h>
37293531Sdchagin#include <sys/bus.h>
38293531Sdchagin#include <sys/capsicum.h>
39293531Sdchagin#include <sys/clock.h>
40293531Sdchagin#include <sys/exec.h>
41293531Sdchagin#include <sys/fcntl.h>
42293531Sdchagin#include <sys/filedesc.h>
43293531Sdchagin#include <sys/imgact.h>
44293531Sdchagin#include <sys/jail.h>
45293531Sdchagin#include <sys/kernel.h>
46293531Sdchagin#include <sys/limits.h>
47293531Sdchagin#include <sys/linker.h>
48293531Sdchagin#include <sys/lock.h>
49293531Sdchagin#include <sys/malloc.h>
50293531Sdchagin#include <sys/file.h>		/* Must come after sys/malloc.h */
51293531Sdchagin#include <sys/imgact.h>
52293531Sdchagin#include <sys/mbuf.h>
53293531Sdchagin#include <sys/mman.h>
54293531Sdchagin#include <sys/module.h>
55293531Sdchagin#include <sys/mount.h>
56293531Sdchagin#include <sys/mutex.h>
57293531Sdchagin#include <sys/namei.h>
58293531Sdchagin#include <sys/proc.h>
59293531Sdchagin#include <sys/procctl.h>
60293531Sdchagin#include <sys/reboot.h>
61293531Sdchagin#include <sys/resource.h>
62293531Sdchagin#include <sys/resourcevar.h>
63293531Sdchagin#include <sys/selinfo.h>
64293531Sdchagin#include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
65293531Sdchagin#include <sys/pipe.h>		/* Must come after sys/selinfo.h */
66293531Sdchagin#include <sys/signal.h>
67293531Sdchagin#include <sys/signalvar.h>
68293531Sdchagin#include <sys/socket.h>
69293531Sdchagin#include <sys/socketvar.h>
70293531Sdchagin#include <sys/stat.h>
71293531Sdchagin#include <sys/syscall.h>
72293531Sdchagin#include <sys/syscallsubr.h>
73293531Sdchagin#include <sys/sysctl.h>
74293531Sdchagin#include <sys/sysent.h>
75293531Sdchagin#include <sys/sysproto.h>
76293531Sdchagin#include <sys/systm.h>
77293531Sdchagin#include <sys/thr.h>
78293531Sdchagin#include <sys/unistd.h>
79293531Sdchagin#include <sys/ucontext.h>
80293531Sdchagin#include <sys/vnode.h>
81293531Sdchagin#include <sys/wait.h>
82293531Sdchagin#include <sys/ipc.h>
83293531Sdchagin#include <sys/msg.h>
84293531Sdchagin#include <sys/sem.h>
85293531Sdchagin#include <sys/shm.h>
86293531Sdchagin
87293531Sdchagin#ifdef INET
88293531Sdchagin#include <netinet/in.h>
89293531Sdchagin#endif
90293531Sdchagin
91293531Sdchagin#include <vm/vm.h>
92293531Sdchagin#include <vm/vm_param.h>
93293531Sdchagin#include <vm/pmap.h>
94293531Sdchagin#include <vm/vm_map.h>
95293531Sdchagin#include <vm/vm_object.h>
96293531Sdchagin#include <vm/vm_extern.h>
97293531Sdchagin
98293531Sdchagin#include <machine/cpu.h>
99293531Sdchagin#include <machine/elf.h>
100293531Sdchagin
101293531Sdchagin#include <security/audit/audit.h>
102293531Sdchagin
103293531Sdchagin#include <compat/freebsd32/freebsd32_util.h>
104293531Sdchagin#include <compat/freebsd32/freebsd32.h>
105293531Sdchagin#include <compat/freebsd32/freebsd32_ipc.h>
106293531Sdchagin#include <compat/freebsd32/freebsd32_misc.h>
107293531Sdchagin#include <compat/freebsd32/freebsd32_signal.h>
108293531Sdchagin#include <compat/freebsd32/freebsd32_proto.h>
109293531Sdchagin
110293531SdchaginFEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
111293531Sdchagin
112293531Sdchagin#ifndef __mips__
113293531SdchaginCTASSERT(sizeof(struct timeval32) == 8);
114293531SdchaginCTASSERT(sizeof(struct timespec32) == 8);
115293531SdchaginCTASSERT(sizeof(struct itimerval32) == 16);
116293531Sdchagin#endif
117293531SdchaginCTASSERT(sizeof(struct statfs32) == 256);
118293531Sdchagin#ifndef __mips__
119293531SdchaginCTASSERT(sizeof(struct rusage32) == 72);
120293531Sdchagin#endif
121293531SdchaginCTASSERT(sizeof(struct sigaltstack32) == 12);
122293531SdchaginCTASSERT(sizeof(struct kevent32) == 20);
123293531SdchaginCTASSERT(sizeof(struct iovec32) == 8);
124293531SdchaginCTASSERT(sizeof(struct msghdr32) == 28);
125293531Sdchagin#ifndef __mips__
126293531SdchaginCTASSERT(sizeof(struct stat32) == 96);
127293531Sdchagin#endif
128293531SdchaginCTASSERT(sizeof(struct sigaction32) == 24);
129293531Sdchagin
130293531Sdchaginstatic int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
131293531Sdchaginstatic int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
132293531Sdchagin
133293531Sdchaginvoid
134293531Sdchaginfreebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
135293531Sdchagin{
136293531Sdchagin
137293531Sdchagin	TV_CP(*s, *s32, ru_utime);
138293531Sdchagin	TV_CP(*s, *s32, ru_stime);
139293531Sdchagin	CP(*s, *s32, ru_maxrss);
140293531Sdchagin	CP(*s, *s32, ru_ixrss);
141293531Sdchagin	CP(*s, *s32, ru_idrss);
142293531Sdchagin	CP(*s, *s32, ru_isrss);
143293531Sdchagin	CP(*s, *s32, ru_minflt);
144293531Sdchagin	CP(*s, *s32, ru_majflt);
145293531Sdchagin	CP(*s, *s32, ru_nswap);
146293531Sdchagin	CP(*s, *s32, ru_inblock);
147293531Sdchagin	CP(*s, *s32, ru_oublock);
148293531Sdchagin	CP(*s, *s32, ru_msgsnd);
149293531Sdchagin	CP(*s, *s32, ru_msgrcv);
150293531Sdchagin	CP(*s, *s32, ru_nsignals);
151293531Sdchagin	CP(*s, *s32, ru_nvcsw);
152293531Sdchagin	CP(*s, *s32, ru_nivcsw);
153293531Sdchagin}
154293531Sdchagin
155293531Sdchaginint
156293531Sdchaginfreebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
157293531Sdchagin{
158293531Sdchagin	int error, status;
159293531Sdchagin	struct rusage32 ru32;
160293531Sdchagin	struct rusage ru, *rup;
161293531Sdchagin
162293531Sdchagin	if (uap->rusage != NULL)
163293531Sdchagin		rup = &ru;
164293531Sdchagin	else
165293531Sdchagin		rup = NULL;
166293531Sdchagin	error = kern_wait(td, uap->pid, &status, uap->options, rup);
167293531Sdchagin	if (error)
168293531Sdchagin		return (error);
169293531Sdchagin	if (uap->status != NULL)
170293531Sdchagin		error = copyout(&status, uap->status, sizeof(status));
171293531Sdchagin	if (uap->rusage != NULL && error == 0) {
172293531Sdchagin		freebsd32_rusage_out(&ru, &ru32);
173293531Sdchagin		error = copyout(&ru32, uap->rusage, sizeof(ru32));
174293531Sdchagin	}
175293531Sdchagin	return (error);
176293531Sdchagin}
177293531Sdchagin
178293531Sdchaginint
179293531Sdchaginfreebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
180293531Sdchagin{
181293531Sdchagin	struct wrusage32 wru32;
182293531Sdchagin	struct __wrusage wru, *wrup;
183293531Sdchagin	struct siginfo32 si32;
184293531Sdchagin	struct __siginfo si, *sip;
185293531Sdchagin	int error, status;
186293531Sdchagin
187293531Sdchagin	if (uap->wrusage != NULL)
188293531Sdchagin		wrup = &wru;
189293531Sdchagin	else
190293531Sdchagin		wrup = NULL;
191293531Sdchagin	if (uap->info != NULL) {
192293531Sdchagin		sip = &si;
193293531Sdchagin		bzero(sip, sizeof(*sip));
194293531Sdchagin	} else
195293531Sdchagin		sip = NULL;
196293531Sdchagin	error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
197293531Sdchagin	    &status, uap->options, wrup, sip);
198293531Sdchagin	if (error != 0)
199293531Sdchagin		return (error);
200293531Sdchagin	if (uap->status != NULL)
201293531Sdchagin		error = copyout(&status, uap->status, sizeof(status));
202293531Sdchagin	if (uap->wrusage != NULL && error == 0) {
203293531Sdchagin		freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
204293531Sdchagin		freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
205293531Sdchagin		error = copyout(&wru32, uap->wrusage, sizeof(wru32));
206293531Sdchagin	}
207293531Sdchagin	if (uap->info != NULL && error == 0) {
208293531Sdchagin		siginfo_to_siginfo32 (&si, &si32);
209293531Sdchagin		error = copyout(&si32, uap->info, sizeof(si32));
210293531Sdchagin	}
211293531Sdchagin	return (error);
212293531Sdchagin}
213293531Sdchagin
214293531Sdchagin#ifdef COMPAT_FREEBSD4
215293531Sdchaginstatic void
216293531Sdchagincopy_statfs(struct statfs *in, struct statfs32 *out)
217293531Sdchagin{
218293531Sdchagin
219293531Sdchagin	statfs_scale_blocks(in, INT32_MAX);
220293531Sdchagin	bzero(out, sizeof(*out));
221293531Sdchagin	CP(*in, *out, f_bsize);
222293531Sdchagin	out->f_iosize = MIN(in->f_iosize, INT32_MAX);
223293531Sdchagin	CP(*in, *out, f_blocks);
224293531Sdchagin	CP(*in, *out, f_bfree);
225293531Sdchagin	CP(*in, *out, f_bavail);
226293531Sdchagin	out->f_files = MIN(in->f_files, INT32_MAX);
227293531Sdchagin	out->f_ffree = MIN(in->f_ffree, INT32_MAX);
228293531Sdchagin	CP(*in, *out, f_fsid);
229293531Sdchagin	CP(*in, *out, f_owner);
230293531Sdchagin	CP(*in, *out, f_type);
231293531Sdchagin	CP(*in, *out, f_flags);
232293531Sdchagin	out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
233293531Sdchagin	out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
234293547Sdchagin	strlcpy(out->f_fstypename,
235293531Sdchagin	      in->f_fstypename, MFSNAMELEN);
236293531Sdchagin	strlcpy(out->f_mntonname,
237293531Sdchagin	      in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
238293531Sdchagin	out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
239293531Sdchagin	out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
240293531Sdchagin	strlcpy(out->f_mntfromname,
241293531Sdchagin	      in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
242293531Sdchagin}
243293531Sdchagin#endif
244293531Sdchagin
245293531Sdchagin#ifdef COMPAT_FREEBSD4
246293531Sdchaginint
247293531Sdchaginfreebsd4_freebsd32_getfsstat(struct thread *td,
248293531Sdchagin    struct freebsd4_freebsd32_getfsstat_args *uap)
249293531Sdchagin{
250293531Sdchagin	struct statfs *buf, *sp;
251293531Sdchagin	struct statfs32 stat32;
252293531Sdchagin	size_t count, size, copycount;
253293547Sdchagin	int error;
254293547Sdchagin
255293531Sdchagin	count = uap->bufsize / sizeof(struct statfs32);
256293531Sdchagin	size = count * sizeof(struct statfs);
257293531Sdchagin	error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
258293531Sdchagin	if (size > 0) {
259293531Sdchagin		sp = buf;
260293531Sdchagin		copycount = count;
261293531Sdchagin		while (copycount > 0 && error == 0) {
262293531Sdchagin			copy_statfs(sp, &stat32);
263293531Sdchagin			error = copyout(&stat32, uap->buf, sizeof(stat32));
264293531Sdchagin			sp++;
265293531Sdchagin			uap->buf++;
266293531Sdchagin			copycount--;
267293531Sdchagin		}
268293531Sdchagin		free(buf, M_STATFS);
269293531Sdchagin	}
270293531Sdchagin	if (error == 0)
271293531Sdchagin		td->td_retval[0] = count;
272293531Sdchagin	return (error);
273293531Sdchagin}
274293531Sdchagin#endif
275293531Sdchagin
276293531Sdchagin#ifdef COMPAT_FREEBSD10
277293531Sdchaginint
278293531Sdchaginfreebsd10_freebsd32_pipe(struct thread *td,
279293531Sdchagin    struct freebsd10_freebsd32_pipe_args *uap) {
280293531Sdchagin
281293531Sdchagin	return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
282293531Sdchagin}
283293531Sdchagin#endif
284293531Sdchagin
285293531Sdchaginint
286293531Sdchaginfreebsd32_sigaltstack(struct thread *td,
287293531Sdchagin		      struct freebsd32_sigaltstack_args *uap)
288293531Sdchagin{
289293531Sdchagin	struct sigaltstack32 s32;
290293531Sdchagin	struct sigaltstack ss, oss, *ssp;
291293531Sdchagin	int error;
292293556Sdchagin
293293531Sdchagin	if (uap->ss != NULL) {
294293531Sdchagin		error = copyin(uap->ss, &s32, sizeof(s32));
295293531Sdchagin		if (error)
296293531Sdchagin			return (error);
297293531Sdchagin		PTRIN_CP(s32, ss, ss_sp);
298293531Sdchagin		CP(s32, ss, ss_size);
299293531Sdchagin		CP(s32, ss, ss_flags);
300293531Sdchagin		ssp = &ss;
301293583Sdchagin	} else
302293547Sdchagin		ssp = NULL;
303293531Sdchagin	error = kern_sigaltstack(td, ssp, &oss);
304293531Sdchagin	if (error == 0 && uap->oss != NULL) {
305293550Sdchagin		PTROUT_CP(oss, s32, ss_sp);
306293568Sdchagin		CP(oss, s32, ss_size);
307293531Sdchagin		CP(oss, s32, ss_flags);
308293531Sdchagin		error = copyout(&s32, uap->oss, sizeof(s32));
309293531Sdchagin	}
310293531Sdchagin	return (error);
311293550Sdchagin}
312293547Sdchagin
313293531Sdchagin/*
314293531Sdchagin * Custom version of exec_copyin_args() so that we can translate
315293531Sdchagin * the pointers.
316293531Sdchagin */
317293531Sdchaginint
318293531Sdchaginfreebsd32_exec_copyin_args(struct image_args *args, char *fname,
319293531Sdchagin    enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
320293589Sdchagin{
321293531Sdchagin	char *argp, *envp;
322293531Sdchagin	u_int32_t *p32, arg;
323293531Sdchagin	size_t length;
324293531Sdchagin	int error;
325293531Sdchagin
326293531Sdchagin	bzero(args, sizeof(*args));
327293593Sdchagin	if (argv == NULL)
328293589Sdchagin		return (EFAULT);
329293531Sdchagin
330293531Sdchagin	/*
331293531Sdchagin	 * Allocate demand-paged memory for the file name, argument, and
332293531Sdchagin	 * environment strings.
333293531Sdchagin	 */
334293570Sdchagin	error = exec_alloc_args(args);
335293531Sdchagin	if (error != 0)
336		return (error);
337
338	/*
339	 * Copy the file name.
340	 */
341	if (fname != NULL) {
342		args->fname = args->buf;
343		error = (segflg == UIO_SYSSPACE) ?
344		    copystr(fname, args->fname, PATH_MAX, &length) :
345		    copyinstr(fname, args->fname, PATH_MAX, &length);
346		if (error != 0)
347			goto err_exit;
348	} else
349		length = 0;
350
351	args->begin_argv = args->buf + length;
352	args->endp = args->begin_argv;
353	args->stringspace = ARG_MAX;
354
355	/*
356	 * extract arguments first
357	 */
358	p32 = argv;
359	for (;;) {
360		error = copyin(p32++, &arg, sizeof(arg));
361		if (error)
362			goto err_exit;
363		if (arg == 0)
364			break;
365		argp = PTRIN(arg);
366		error = copyinstr(argp, args->endp, args->stringspace, &length);
367		if (error) {
368			if (error == ENAMETOOLONG)
369				error = E2BIG;
370			goto err_exit;
371		}
372		args->stringspace -= length;
373		args->endp += length;
374		args->argc++;
375	}
376
377	args->begin_envv = args->endp;
378
379	/*
380	 * extract environment strings
381	 */
382	if (envv) {
383		p32 = envv;
384		for (;;) {
385			error = copyin(p32++, &arg, sizeof(arg));
386			if (error)
387				goto err_exit;
388			if (arg == 0)
389				break;
390			envp = PTRIN(arg);
391			error = copyinstr(envp, args->endp, args->stringspace,
392			    &length);
393			if (error) {
394				if (error == ENAMETOOLONG)
395					error = E2BIG;
396				goto err_exit;
397			}
398			args->stringspace -= length;
399			args->endp += length;
400			args->envc++;
401		}
402	}
403
404	return (0);
405
406err_exit:
407	exec_free_args(args);
408	return (error);
409}
410
411int
412freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
413{
414	struct image_args eargs;
415	struct vmspace *oldvmspace;
416	int error;
417
418	error = pre_execve(td, &oldvmspace);
419	if (error != 0)
420		return (error);
421	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
422	    uap->argv, uap->envv);
423	if (error == 0)
424		error = kern_execve(td, &eargs, NULL);
425	post_execve(td, error, oldvmspace);
426	return (error);
427}
428
429int
430freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
431{
432	struct image_args eargs;
433	struct vmspace *oldvmspace;
434	int error;
435
436	error = pre_execve(td, &oldvmspace);
437	if (error != 0)
438		return (error);
439	error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
440	    uap->argv, uap->envv);
441	if (error == 0) {
442		eargs.fd = uap->fd;
443		error = kern_execve(td, &eargs, NULL);
444	}
445	post_execve(td, error, oldvmspace);
446	return (error);
447}
448
449int
450freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
451{
452	int prot;
453
454	prot = uap->prot;
455#if defined(__amd64__)
456	if (i386_read_exec && (prot & PROT_READ) != 0)
457		prot |= PROT_EXEC;
458#endif
459	return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
460	    prot));
461}
462
463int
464freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
465{
466	int prot;
467
468	prot = uap->prot;
469#if defined(__amd64__)
470	if (i386_read_exec && (prot & PROT_READ))
471		prot |= PROT_EXEC;
472#endif
473
474	return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
475	    uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
476}
477
478#ifdef COMPAT_FREEBSD6
479int
480freebsd6_freebsd32_mmap(struct thread *td,
481    struct freebsd6_freebsd32_mmap_args *uap)
482{
483	int prot;
484
485	prot = uap->prot;
486#if defined(__amd64__)
487	if (i386_read_exec && (prot & PROT_READ))
488		prot |= PROT_EXEC;
489#endif
490
491	return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
492	    uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
493}
494#endif
495
496int
497freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
498{
499	struct itimerval itv, oitv, *itvp;
500	struct itimerval32 i32;
501	int error;
502
503	if (uap->itv != NULL) {
504		error = copyin(uap->itv, &i32, sizeof(i32));
505		if (error)
506			return (error);
507		TV_CP(i32, itv, it_interval);
508		TV_CP(i32, itv, it_value);
509		itvp = &itv;
510	} else
511		itvp = NULL;
512	error = kern_setitimer(td, uap->which, itvp, &oitv);
513	if (error || uap->oitv == NULL)
514		return (error);
515	TV_CP(oitv, i32, it_interval);
516	TV_CP(oitv, i32, it_value);
517	return (copyout(&i32, uap->oitv, sizeof(i32)));
518}
519
520int
521freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
522{
523	struct itimerval itv;
524	struct itimerval32 i32;
525	int error;
526
527	error = kern_getitimer(td, uap->which, &itv);
528	if (error || uap->itv == NULL)
529		return (error);
530	TV_CP(itv, i32, it_interval);
531	TV_CP(itv, i32, it_value);
532	return (copyout(&i32, uap->itv, sizeof(i32)));
533}
534
535int
536freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
537{
538	struct timeval32 tv32;
539	struct timeval tv, *tvp;
540	int error;
541
542	if (uap->tv != NULL) {
543		error = copyin(uap->tv, &tv32, sizeof(tv32));
544		if (error)
545			return (error);
546		CP(tv32, tv, tv_sec);
547		CP(tv32, tv, tv_usec);
548		tvp = &tv;
549	} else
550		tvp = NULL;
551	/*
552	 * XXX Do pointers need PTRIN()?
553	 */
554	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
555	    sizeof(int32_t) * 8));
556}
557
558int
559freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
560{
561	struct timespec32 ts32;
562	struct timespec ts;
563	struct timeval tv, *tvp;
564	sigset_t set, *uset;
565	int error;
566
567	if (uap->ts != NULL) {
568		error = copyin(uap->ts, &ts32, sizeof(ts32));
569		if (error != 0)
570			return (error);
571		CP(ts32, ts, tv_sec);
572		CP(ts32, ts, tv_nsec);
573		TIMESPEC_TO_TIMEVAL(&tv, &ts);
574		tvp = &tv;
575	} else
576		tvp = NULL;
577	if (uap->sm != NULL) {
578		error = copyin(uap->sm, &set, sizeof(set));
579		if (error != 0)
580			return (error);
581		uset = &set;
582	} else
583		uset = NULL;
584	/*
585	 * XXX Do pointers need PTRIN()?
586	 */
587	error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
588	    uset, sizeof(int32_t) * 8);
589	return (error);
590}
591
592/*
593 * Copy 'count' items into the destination list pointed to by uap->eventlist.
594 */
595static int
596freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
597{
598	struct freebsd32_kevent_args *uap;
599	struct kevent32	ks32[KQ_NEVENTS];
600	int i, error = 0;
601
602	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
603	uap = (struct freebsd32_kevent_args *)arg;
604
605	for (i = 0; i < count; i++) {
606		CP(kevp[i], ks32[i], ident);
607		CP(kevp[i], ks32[i], filter);
608		CP(kevp[i], ks32[i], flags);
609		CP(kevp[i], ks32[i], fflags);
610		CP(kevp[i], ks32[i], data);
611		PTROUT_CP(kevp[i], ks32[i], udata);
612	}
613	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
614	if (error == 0)
615		uap->eventlist += count;
616	return (error);
617}
618
619/*
620 * Copy 'count' items from the list pointed to by uap->changelist.
621 */
622static int
623freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
624{
625	struct freebsd32_kevent_args *uap;
626	struct kevent32	ks32[KQ_NEVENTS];
627	int i, error = 0;
628
629	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
630	uap = (struct freebsd32_kevent_args *)arg;
631
632	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
633	if (error)
634		goto done;
635	uap->changelist += count;
636
637	for (i = 0; i < count; i++) {
638		CP(ks32[i], kevp[i], ident);
639		CP(ks32[i], kevp[i], filter);
640		CP(ks32[i], kevp[i], flags);
641		CP(ks32[i], kevp[i], fflags);
642		CP(ks32[i], kevp[i], data);
643		PTRIN_CP(ks32[i], kevp[i], udata);
644	}
645done:
646	return (error);
647}
648
649int
650freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
651{
652	struct timespec32 ts32;
653	struct timespec ts, *tsp;
654	struct kevent_copyops k_ops = {
655		.arg = uap,
656		.k_copyout = freebsd32_kevent_copyout,
657		.k_copyin = freebsd32_kevent_copyin,
658	};
659	int error;
660
661
662	if (uap->timeout) {
663		error = copyin(uap->timeout, &ts32, sizeof(ts32));
664		if (error)
665			return (error);
666		CP(ts32, ts, tv_sec);
667		CP(ts32, ts, tv_nsec);
668		tsp = &ts;
669	} else
670		tsp = NULL;
671	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
672	    &k_ops, tsp);
673	return (error);
674}
675
676int
677freebsd32_gettimeofday(struct thread *td,
678		       struct freebsd32_gettimeofday_args *uap)
679{
680	struct timeval atv;
681	struct timeval32 atv32;
682	struct timezone rtz;
683	int error = 0;
684
685	if (uap->tp) {
686		microtime(&atv);
687		CP(atv, atv32, tv_sec);
688		CP(atv, atv32, tv_usec);
689		error = copyout(&atv32, uap->tp, sizeof (atv32));
690	}
691	if (error == 0 && uap->tzp != NULL) {
692		rtz.tz_minuteswest = tz_minuteswest;
693		rtz.tz_dsttime = tz_dsttime;
694		error = copyout(&rtz, uap->tzp, sizeof (rtz));
695	}
696	return (error);
697}
698
699int
700freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
701{
702	struct rusage32 s32;
703	struct rusage s;
704	int error;
705
706	error = kern_getrusage(td, uap->who, &s);
707	if (error)
708		return (error);
709	if (uap->rusage != NULL) {
710		freebsd32_rusage_out(&s, &s32);
711		error = copyout(&s32, uap->rusage, sizeof(s32));
712	}
713	return (error);
714}
715
716static int
717freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
718{
719	struct iovec32 iov32;
720	struct iovec *iov;
721	struct uio *uio;
722	u_int iovlen;
723	int error, i;
724
725	*uiop = NULL;
726	if (iovcnt > UIO_MAXIOV)
727		return (EINVAL);
728	iovlen = iovcnt * sizeof(struct iovec);
729	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
730	iov = (struct iovec *)(uio + 1);
731	for (i = 0; i < iovcnt; i++) {
732		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
733		if (error) {
734			free(uio, M_IOV);
735			return (error);
736		}
737		iov[i].iov_base = PTRIN(iov32.iov_base);
738		iov[i].iov_len = iov32.iov_len;
739	}
740	uio->uio_iov = iov;
741	uio->uio_iovcnt = iovcnt;
742	uio->uio_segflg = UIO_USERSPACE;
743	uio->uio_offset = -1;
744	uio->uio_resid = 0;
745	for (i = 0; i < iovcnt; i++) {
746		if (iov->iov_len > INT_MAX - uio->uio_resid) {
747			free(uio, M_IOV);
748			return (EINVAL);
749		}
750		uio->uio_resid += iov->iov_len;
751		iov++;
752	}
753	*uiop = uio;
754	return (0);
755}
756
757int
758freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
759{
760	struct uio *auio;
761	int error;
762
763	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
764	if (error)
765		return (error);
766	error = kern_readv(td, uap->fd, auio);
767	free(auio, M_IOV);
768	return (error);
769}
770
771int
772freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
773{
774	struct uio *auio;
775	int error;
776
777	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
778	if (error)
779		return (error);
780	error = kern_writev(td, uap->fd, auio);
781	free(auio, M_IOV);
782	return (error);
783}
784
785int
786freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
787{
788	struct uio *auio;
789	int error;
790
791	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
792	if (error)
793		return (error);
794	error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
795	free(auio, M_IOV);
796	return (error);
797}
798
799int
800freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
801{
802	struct uio *auio;
803	int error;
804
805	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
806	if (error)
807		return (error);
808	error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
809	free(auio, M_IOV);
810	return (error);
811}
812
813int
814freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
815    int error)
816{
817	struct iovec32 iov32;
818	struct iovec *iov;
819	u_int iovlen;
820	int i;
821
822	*iovp = NULL;
823	if (iovcnt > UIO_MAXIOV)
824		return (error);
825	iovlen = iovcnt * sizeof(struct iovec);
826	iov = malloc(iovlen, M_IOV, M_WAITOK);
827	for (i = 0; i < iovcnt; i++) {
828		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
829		if (error) {
830			free(iov, M_IOV);
831			return (error);
832		}
833		iov[i].iov_base = PTRIN(iov32.iov_base);
834		iov[i].iov_len = iov32.iov_len;
835	}
836	*iovp = iov;
837	return (0);
838}
839
840static int
841freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
842{
843	struct msghdr32 m32;
844	int error;
845
846	error = copyin(msg32, &m32, sizeof(m32));
847	if (error)
848		return (error);
849	msg->msg_name = PTRIN(m32.msg_name);
850	msg->msg_namelen = m32.msg_namelen;
851	msg->msg_iov = PTRIN(m32.msg_iov);
852	msg->msg_iovlen = m32.msg_iovlen;
853	msg->msg_control = PTRIN(m32.msg_control);
854	msg->msg_controllen = m32.msg_controllen;
855	msg->msg_flags = m32.msg_flags;
856	return (0);
857}
858
859static int
860freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
861{
862	struct msghdr32 m32;
863	int error;
864
865	m32.msg_name = PTROUT(msg->msg_name);
866	m32.msg_namelen = msg->msg_namelen;
867	m32.msg_iov = PTROUT(msg->msg_iov);
868	m32.msg_iovlen = msg->msg_iovlen;
869	m32.msg_control = PTROUT(msg->msg_control);
870	m32.msg_controllen = msg->msg_controllen;
871	m32.msg_flags = msg->msg_flags;
872	error = copyout(&m32, msg32, sizeof(m32));
873	return (error);
874}
875
876#ifndef __mips__
877#define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
878#else
879#define FREEBSD32_ALIGNBYTES	(sizeof(long) - 1)
880#endif
881#define FREEBSD32_ALIGN(p)	\
882	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
883#define	FREEBSD32_CMSG_SPACE(l)	\
884	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
885
886#define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
887				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
888static int
889freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
890{
891	struct cmsghdr *cm;
892	void *data;
893	socklen_t clen, datalen;
894	int error;
895	caddr_t ctlbuf;
896	int len, maxlen, copylen;
897	struct mbuf *m;
898	error = 0;
899
900	len    = msg->msg_controllen;
901	maxlen = msg->msg_controllen;
902	msg->msg_controllen = 0;
903
904	m = control;
905	ctlbuf = msg->msg_control;
906
907	while (m && len > 0) {
908		cm = mtod(m, struct cmsghdr *);
909		clen = m->m_len;
910
911		while (cm != NULL) {
912
913			if (sizeof(struct cmsghdr) > clen ||
914			    cm->cmsg_len > clen) {
915				error = EINVAL;
916				break;
917			}
918
919			data   = CMSG_DATA(cm);
920			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
921
922			/* Adjust message length */
923			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
924			    datalen;
925
926
927			/* Copy cmsghdr */
928			copylen = sizeof(struct cmsghdr);
929			if (len < copylen) {
930				msg->msg_flags |= MSG_CTRUNC;
931				copylen = len;
932			}
933
934			error = copyout(cm,ctlbuf,copylen);
935			if (error)
936				goto exit;
937
938			ctlbuf += FREEBSD32_ALIGN(copylen);
939			len    -= FREEBSD32_ALIGN(copylen);
940
941			if (len <= 0)
942				break;
943
944			/* Copy data */
945			copylen = datalen;
946			if (len < copylen) {
947				msg->msg_flags |= MSG_CTRUNC;
948				copylen = len;
949			}
950
951			error = copyout(data,ctlbuf,copylen);
952			if (error)
953				goto exit;
954
955			ctlbuf += FREEBSD32_ALIGN(copylen);
956			len    -= FREEBSD32_ALIGN(copylen);
957
958			if (CMSG_SPACE(datalen) < clen) {
959				clen -= CMSG_SPACE(datalen);
960				cm = (struct cmsghdr *)
961					((caddr_t)cm + CMSG_SPACE(datalen));
962			} else {
963				clen = 0;
964				cm = NULL;
965			}
966		}
967		m = m->m_next;
968	}
969
970	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
971
972exit:
973	return (error);
974
975}
976
977int
978freebsd32_recvmsg(td, uap)
979	struct thread *td;
980	struct freebsd32_recvmsg_args /* {
981		int	s;
982		struct	msghdr32 *msg;
983		int	flags;
984	} */ *uap;
985{
986	struct msghdr msg;
987	struct msghdr32 m32;
988	struct iovec *uiov, *iov;
989	struct mbuf *control = NULL;
990	struct mbuf **controlp;
991
992	int error;
993	error = copyin(uap->msg, &m32, sizeof(m32));
994	if (error)
995		return (error);
996	error = freebsd32_copyinmsghdr(uap->msg, &msg);
997	if (error)
998		return (error);
999	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1000	    EMSGSIZE);
1001	if (error)
1002		return (error);
1003	msg.msg_flags = uap->flags;
1004	uiov = msg.msg_iov;
1005	msg.msg_iov = iov;
1006
1007	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1008	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1009	if (error == 0) {
1010		msg.msg_iov = uiov;
1011
1012		if (control != NULL)
1013			error = freebsd32_copy_msg_out(&msg, control);
1014		else
1015			msg.msg_controllen = 0;
1016
1017		if (error == 0)
1018			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1019	}
1020	free(iov, M_IOV);
1021
1022	if (control != NULL)
1023		m_freem(control);
1024
1025	return (error);
1026}
1027
1028/*
1029 * Copy-in the array of control messages constructed using alignment
1030 * and padding suitable for a 32-bit environment and construct an
1031 * mbuf using alignment and padding suitable for a 64-bit kernel.
1032 * The alignment and padding are defined indirectly by CMSG_DATA(),
1033 * CMSG_SPACE() and CMSG_LEN().
1034 */
1035static int
1036freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1037{
1038	struct mbuf *m;
1039	void *md;
1040	u_int idx, len, msglen;
1041	int error;
1042
1043	buflen = FREEBSD32_ALIGN(buflen);
1044
1045	if (buflen > MCLBYTES)
1046		return (EINVAL);
1047
1048	/*
1049	 * Iterate over the buffer and get the length of each message
1050	 * in there. This has 32-bit alignment and padding. Use it to
1051	 * determine the length of these messages when using 64-bit
1052	 * alignment and padding.
1053	 */
1054	idx = 0;
1055	len = 0;
1056	while (idx < buflen) {
1057		error = copyin(buf + idx, &msglen, sizeof(msglen));
1058		if (error)
1059			return (error);
1060		if (msglen < sizeof(struct cmsghdr))
1061			return (EINVAL);
1062		msglen = FREEBSD32_ALIGN(msglen);
1063		if (idx + msglen > buflen)
1064			return (EINVAL);
1065		idx += msglen;
1066		msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1067		    FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1068		len += CMSG_ALIGN(msglen);
1069	}
1070
1071	if (len > MCLBYTES)
1072		return (EINVAL);
1073
1074	m = m_get(M_WAITOK, MT_CONTROL);
1075	if (len > MLEN)
1076		MCLGET(m, M_WAITOK);
1077	m->m_len = len;
1078
1079	md = mtod(m, void *);
1080	while (buflen > 0) {
1081		error = copyin(buf, md, sizeof(struct cmsghdr));
1082		if (error)
1083			break;
1084		msglen = *(u_int *)md;
1085		msglen = FREEBSD32_ALIGN(msglen);
1086
1087		/* Modify the message length to account for alignment. */
1088		*(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1089		    FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1090
1091		md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1092		buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1093		buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1094
1095		msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1096		if (msglen > 0) {
1097			error = copyin(buf, md, msglen);
1098			if (error)
1099				break;
1100			md = (char *)md + CMSG_ALIGN(msglen);
1101			buf += msglen;
1102			buflen -= msglen;
1103		}
1104	}
1105
1106	if (error)
1107		m_free(m);
1108	else
1109		*mp = m;
1110	return (error);
1111}
1112
1113int
1114freebsd32_sendmsg(struct thread *td,
1115		  struct freebsd32_sendmsg_args *uap)
1116{
1117	struct msghdr msg;
1118	struct msghdr32 m32;
1119	struct iovec *iov;
1120	struct mbuf *control = NULL;
1121	struct sockaddr *to = NULL;
1122	int error;
1123
1124	error = copyin(uap->msg, &m32, sizeof(m32));
1125	if (error)
1126		return (error);
1127	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1128	if (error)
1129		return (error);
1130	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1131	    EMSGSIZE);
1132	if (error)
1133		return (error);
1134	msg.msg_iov = iov;
1135	if (msg.msg_name != NULL) {
1136		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1137		if (error) {
1138			to = NULL;
1139			goto out;
1140		}
1141		msg.msg_name = to;
1142	}
1143
1144	if (msg.msg_control) {
1145		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1146			error = EINVAL;
1147			goto out;
1148		}
1149
1150		error = freebsd32_copyin_control(&control, msg.msg_control,
1151		    msg.msg_controllen);
1152		if (error)
1153			goto out;
1154
1155		msg.msg_control = NULL;
1156		msg.msg_controllen = 0;
1157	}
1158
1159	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1160	    UIO_USERSPACE);
1161
1162out:
1163	free(iov, M_IOV);
1164	if (to)
1165		free(to, M_SONAME);
1166	return (error);
1167}
1168
1169int
1170freebsd32_recvfrom(struct thread *td,
1171		   struct freebsd32_recvfrom_args *uap)
1172{
1173	struct msghdr msg;
1174	struct iovec aiov;
1175	int error;
1176
1177	if (uap->fromlenaddr) {
1178		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1179		    sizeof(msg.msg_namelen));
1180		if (error)
1181			return (error);
1182	} else {
1183		msg.msg_namelen = 0;
1184	}
1185
1186	msg.msg_name = PTRIN(uap->from);
1187	msg.msg_iov = &aiov;
1188	msg.msg_iovlen = 1;
1189	aiov.iov_base = PTRIN(uap->buf);
1190	aiov.iov_len = uap->len;
1191	msg.msg_control = NULL;
1192	msg.msg_flags = uap->flags;
1193	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1194	if (error == 0 && uap->fromlenaddr)
1195		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1196		    sizeof (msg.msg_namelen));
1197	return (error);
1198}
1199
1200int
1201freebsd32_settimeofday(struct thread *td,
1202		       struct freebsd32_settimeofday_args *uap)
1203{
1204	struct timeval32 tv32;
1205	struct timeval tv, *tvp;
1206	struct timezone tz, *tzp;
1207	int error;
1208
1209	if (uap->tv) {
1210		error = copyin(uap->tv, &tv32, sizeof(tv32));
1211		if (error)
1212			return (error);
1213		CP(tv32, tv, tv_sec);
1214		CP(tv32, tv, tv_usec);
1215		tvp = &tv;
1216	} else
1217		tvp = NULL;
1218	if (uap->tzp) {
1219		error = copyin(uap->tzp, &tz, sizeof(tz));
1220		if (error)
1221			return (error);
1222		tzp = &tz;
1223	} else
1224		tzp = NULL;
1225	return (kern_settimeofday(td, tvp, tzp));
1226}
1227
1228int
1229freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1230{
1231	struct timeval32 s32[2];
1232	struct timeval s[2], *sp;
1233	int error;
1234
1235	if (uap->tptr != NULL) {
1236		error = copyin(uap->tptr, s32, sizeof(s32));
1237		if (error)
1238			return (error);
1239		CP(s32[0], s[0], tv_sec);
1240		CP(s32[0], s[0], tv_usec);
1241		CP(s32[1], s[1], tv_sec);
1242		CP(s32[1], s[1], tv_usec);
1243		sp = s;
1244	} else
1245		sp = NULL;
1246	return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1247	    sp, UIO_SYSSPACE));
1248}
1249
1250int
1251freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1252{
1253	struct timeval32 s32[2];
1254	struct timeval s[2], *sp;
1255	int error;
1256
1257	if (uap->tptr != NULL) {
1258		error = copyin(uap->tptr, s32, sizeof(s32));
1259		if (error)
1260			return (error);
1261		CP(s32[0], s[0], tv_sec);
1262		CP(s32[0], s[0], tv_usec);
1263		CP(s32[1], s[1], tv_sec);
1264		CP(s32[1], s[1], tv_usec);
1265		sp = s;
1266	} else
1267		sp = NULL;
1268	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1269}
1270
1271int
1272freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1273{
1274	struct timeval32 s32[2];
1275	struct timeval s[2], *sp;
1276	int error;
1277
1278	if (uap->tptr != NULL) {
1279		error = copyin(uap->tptr, s32, sizeof(s32));
1280		if (error)
1281			return (error);
1282		CP(s32[0], s[0], tv_sec);
1283		CP(s32[0], s[0], tv_usec);
1284		CP(s32[1], s[1], tv_sec);
1285		CP(s32[1], s[1], tv_usec);
1286		sp = s;
1287	} else
1288		sp = NULL;
1289	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1290}
1291
1292int
1293freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1294{
1295	struct timeval32 s32[2];
1296	struct timeval s[2], *sp;
1297	int error;
1298
1299	if (uap->times != NULL) {
1300		error = copyin(uap->times, 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_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1311		sp, UIO_SYSSPACE));
1312}
1313
1314int
1315freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1316{
1317	struct timespec32 ts32[2];
1318	struct timespec ts[2], *tsp;
1319	int error;
1320
1321	if (uap->times != NULL) {
1322		error = copyin(uap->times, ts32, sizeof(ts32));
1323		if (error)
1324			return (error);
1325		CP(ts32[0], ts[0], tv_sec);
1326		CP(ts32[0], ts[0], tv_nsec);
1327		CP(ts32[1], ts[1], tv_sec);
1328		CP(ts32[1], ts[1], tv_nsec);
1329		tsp = ts;
1330	} else
1331		tsp = NULL;
1332	return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1333}
1334
1335int
1336freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1337{
1338	struct timespec32 ts32[2];
1339	struct timespec ts[2], *tsp;
1340	int error;
1341
1342	if (uap->times != NULL) {
1343		error = copyin(uap->times, ts32, sizeof(ts32));
1344		if (error)
1345			return (error);
1346		CP(ts32[0], ts[0], tv_sec);
1347		CP(ts32[0], ts[0], tv_nsec);
1348		CP(ts32[1], ts[1], tv_sec);
1349		CP(ts32[1], ts[1], tv_nsec);
1350		tsp = ts;
1351	} else
1352		tsp = NULL;
1353	return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1354	    tsp, UIO_SYSSPACE, uap->flag));
1355}
1356
1357int
1358freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1359{
1360	struct timeval32 tv32;
1361	struct timeval delta, olddelta, *deltap;
1362	int error;
1363
1364	if (uap->delta) {
1365		error = copyin(uap->delta, &tv32, sizeof(tv32));
1366		if (error)
1367			return (error);
1368		CP(tv32, delta, tv_sec);
1369		CP(tv32, delta, tv_usec);
1370		deltap = &delta;
1371	} else
1372		deltap = NULL;
1373	error = kern_adjtime(td, deltap, &olddelta);
1374	if (uap->olddelta && error == 0) {
1375		CP(olddelta, tv32, tv_sec);
1376		CP(olddelta, tv32, tv_usec);
1377		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1378	}
1379	return (error);
1380}
1381
1382#ifdef COMPAT_FREEBSD4
1383int
1384freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1385{
1386	struct statfs32 s32;
1387	struct statfs *sp;
1388	int error;
1389
1390	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1391	error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1392	if (error == 0) {
1393		copy_statfs(sp, &s32);
1394		error = copyout(&s32, uap->buf, sizeof(s32));
1395	}
1396	free(sp, M_STATFS);
1397	return (error);
1398}
1399#endif
1400
1401#ifdef COMPAT_FREEBSD4
1402int
1403freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1404{
1405	struct statfs32 s32;
1406	struct statfs *sp;
1407	int error;
1408
1409	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1410	error = kern_fstatfs(td, uap->fd, sp);
1411	if (error == 0) {
1412		copy_statfs(sp, &s32);
1413		error = copyout(&s32, uap->buf, sizeof(s32));
1414	}
1415	free(sp, M_STATFS);
1416	return (error);
1417}
1418#endif
1419
1420#ifdef COMPAT_FREEBSD4
1421int
1422freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1423{
1424	struct statfs32 s32;
1425	struct statfs *sp;
1426	fhandle_t fh;
1427	int error;
1428
1429	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1430		return (error);
1431	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1432	error = kern_fhstatfs(td, fh, sp);
1433	if (error == 0) {
1434		copy_statfs(sp, &s32);
1435		error = copyout(&s32, uap->buf, sizeof(s32));
1436	}
1437	free(sp, M_STATFS);
1438	return (error);
1439}
1440#endif
1441
1442int
1443freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1444{
1445	struct pread_args ap;
1446
1447	ap.fd = uap->fd;
1448	ap.buf = uap->buf;
1449	ap.nbyte = uap->nbyte;
1450	ap.offset = PAIR32TO64(off_t,uap->offset);
1451	return (sys_pread(td, &ap));
1452}
1453
1454int
1455freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1456{
1457	struct pwrite_args ap;
1458
1459	ap.fd = uap->fd;
1460	ap.buf = uap->buf;
1461	ap.nbyte = uap->nbyte;
1462	ap.offset = PAIR32TO64(off_t,uap->offset);
1463	return (sys_pwrite(td, &ap));
1464}
1465
1466#ifdef COMPAT_43
1467int
1468ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1469{
1470
1471	return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1472}
1473#endif
1474
1475int
1476freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1477{
1478	int error;
1479	off_t pos;
1480
1481	error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1482	    uap->whence);
1483	/* Expand the quad return into two parts for eax and edx */
1484	pos = td->td_uretoff.tdu_off;
1485	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1486	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1487	return error;
1488}
1489
1490int
1491freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1492{
1493
1494	return (kern_truncate(td, uap->path, UIO_USERSPACE,
1495	    PAIR32TO64(off_t, uap->length)));
1496}
1497
1498int
1499freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1500{
1501
1502	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1503}
1504
1505#ifdef COMPAT_43
1506int
1507ofreebsd32_getdirentries(struct thread *td,
1508    struct ofreebsd32_getdirentries_args *uap)
1509{
1510	struct ogetdirentries_args ap;
1511	int error;
1512	long loff;
1513	int32_t loff_cut;
1514
1515	ap.fd = uap->fd;
1516	ap.buf = uap->buf;
1517	ap.count = uap->count;
1518	ap.basep = NULL;
1519	error = kern_ogetdirentries(td, &ap, &loff);
1520	if (error == 0) {
1521		loff_cut = loff;
1522		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1523	}
1524	return (error);
1525}
1526#endif
1527
1528int
1529freebsd32_getdirentries(struct thread *td,
1530    struct freebsd32_getdirentries_args *uap)
1531{
1532	long base;
1533	int32_t base32;
1534	int error;
1535
1536	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1537	    NULL, UIO_USERSPACE);
1538	if (error)
1539		return (error);
1540	if (uap->basep != NULL) {
1541		base32 = base;
1542		error = copyout(&base32, uap->basep, sizeof(int32_t));
1543	}
1544	return (error);
1545}
1546
1547#ifdef COMPAT_FREEBSD6
1548/* versions with the 'int pad' argument */
1549int
1550freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1551{
1552	struct pread_args ap;
1553
1554	ap.fd = uap->fd;
1555	ap.buf = uap->buf;
1556	ap.nbyte = uap->nbyte;
1557	ap.offset = PAIR32TO64(off_t,uap->offset);
1558	return (sys_pread(td, &ap));
1559}
1560
1561int
1562freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1563{
1564	struct pwrite_args ap;
1565
1566	ap.fd = uap->fd;
1567	ap.buf = uap->buf;
1568	ap.nbyte = uap->nbyte;
1569	ap.offset = PAIR32TO64(off_t,uap->offset);
1570	return (sys_pwrite(td, &ap));
1571}
1572
1573int
1574freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1575{
1576	int error;
1577	off_t pos;
1578
1579	error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1580	    uap->whence);
1581	/* Expand the quad return into two parts for eax and edx */
1582	pos = *(off_t *)(td->td_retval);
1583	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1584	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1585	return error;
1586}
1587
1588int
1589freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1590{
1591
1592	return (kern_truncate(td, uap->path, UIO_USERSPACE,
1593	    PAIR32TO64(off_t, uap->length)));
1594}
1595
1596int
1597freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1598{
1599
1600	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1601}
1602#endif /* COMPAT_FREEBSD6 */
1603
1604struct sf_hdtr32 {
1605	uint32_t headers;
1606	int hdr_cnt;
1607	uint32_t trailers;
1608	int trl_cnt;
1609};
1610
1611static int
1612freebsd32_do_sendfile(struct thread *td,
1613    struct freebsd32_sendfile_args *uap, int compat)
1614{
1615	struct sf_hdtr32 hdtr32;
1616	struct sf_hdtr hdtr;
1617	struct uio *hdr_uio, *trl_uio;
1618	struct file *fp;
1619	cap_rights_t rights;
1620	struct iovec32 *iov32;
1621	off_t offset, sbytes;
1622	int error;
1623
1624	offset = PAIR32TO64(off_t, uap->offset);
1625	if (offset < 0)
1626		return (EINVAL);
1627
1628	hdr_uio = trl_uio = NULL;
1629
1630	if (uap->hdtr != NULL) {
1631		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1632		if (error)
1633			goto out;
1634		PTRIN_CP(hdtr32, hdtr, headers);
1635		CP(hdtr32, hdtr, hdr_cnt);
1636		PTRIN_CP(hdtr32, hdtr, trailers);
1637		CP(hdtr32, hdtr, trl_cnt);
1638
1639		if (hdtr.headers != NULL) {
1640			iov32 = PTRIN(hdtr32.headers);
1641			error = freebsd32_copyinuio(iov32,
1642			    hdtr32.hdr_cnt, &hdr_uio);
1643			if (error)
1644				goto out;
1645#ifdef COMPAT_FREEBSD4
1646			/*
1647			 * In FreeBSD < 5.0 the nbytes to send also included
1648			 * the header.  If compat is specified subtract the
1649			 * header size from nbytes.
1650			 */
1651			if (compat) {
1652				if (uap->nbytes > hdr_uio->uio_resid)
1653					uap->nbytes -= hdr_uio->uio_resid;
1654				else
1655					uap->nbytes = 0;
1656			}
1657#endif
1658		}
1659		if (hdtr.trailers != NULL) {
1660			iov32 = PTRIN(hdtr32.trailers);
1661			error = freebsd32_copyinuio(iov32,
1662			    hdtr32.trl_cnt, &trl_uio);
1663			if (error)
1664				goto out;
1665		}
1666	}
1667
1668	AUDIT_ARG_FD(uap->fd);
1669
1670	if ((error = fget_read(td, uap->fd,
1671	    cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1672		goto out;
1673
1674	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1675	    uap->nbytes, &sbytes, uap->flags, td);
1676	fdrop(fp, td);
1677
1678	if (uap->sbytes != NULL)
1679		copyout(&sbytes, uap->sbytes, sizeof(off_t));
1680
1681out:
1682	if (hdr_uio)
1683		free(hdr_uio, M_IOV);
1684	if (trl_uio)
1685		free(trl_uio, M_IOV);
1686	return (error);
1687}
1688
1689#ifdef COMPAT_FREEBSD4
1690int
1691freebsd4_freebsd32_sendfile(struct thread *td,
1692    struct freebsd4_freebsd32_sendfile_args *uap)
1693{
1694	return (freebsd32_do_sendfile(td,
1695	    (struct freebsd32_sendfile_args *)uap, 1));
1696}
1697#endif
1698
1699int
1700freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1701{
1702
1703	return (freebsd32_do_sendfile(td, uap, 0));
1704}
1705
1706static void
1707copy_stat(struct stat *in, struct stat32 *out)
1708{
1709
1710	CP(*in, *out, st_dev);
1711	CP(*in, *out, st_ino);
1712	CP(*in, *out, st_mode);
1713	CP(*in, *out, st_nlink);
1714	CP(*in, *out, st_uid);
1715	CP(*in, *out, st_gid);
1716	CP(*in, *out, st_rdev);
1717	TS_CP(*in, *out, st_atim);
1718	TS_CP(*in, *out, st_mtim);
1719	TS_CP(*in, *out, st_ctim);
1720	CP(*in, *out, st_size);
1721	CP(*in, *out, st_blocks);
1722	CP(*in, *out, st_blksize);
1723	CP(*in, *out, st_flags);
1724	CP(*in, *out, st_gen);
1725	TS_CP(*in, *out, st_birthtim);
1726}
1727
1728#ifdef COMPAT_43
1729static void
1730copy_ostat(struct stat *in, struct ostat32 *out)
1731{
1732
1733	CP(*in, *out, st_dev);
1734	CP(*in, *out, st_ino);
1735	CP(*in, *out, st_mode);
1736	CP(*in, *out, st_nlink);
1737	CP(*in, *out, st_uid);
1738	CP(*in, *out, st_gid);
1739	CP(*in, *out, st_rdev);
1740	CP(*in, *out, st_size);
1741	TS_CP(*in, *out, st_atim);
1742	TS_CP(*in, *out, st_mtim);
1743	TS_CP(*in, *out, st_ctim);
1744	CP(*in, *out, st_blksize);
1745	CP(*in, *out, st_blocks);
1746	CP(*in, *out, st_flags);
1747	CP(*in, *out, st_gen);
1748}
1749#endif
1750
1751int
1752freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1753{
1754	struct stat sb;
1755	struct stat32 sb32;
1756	int error;
1757
1758	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1759	    &sb, NULL);
1760	if (error)
1761		return (error);
1762	copy_stat(&sb, &sb32);
1763	error = copyout(&sb32, uap->ub, sizeof (sb32));
1764	return (error);
1765}
1766
1767#ifdef COMPAT_43
1768int
1769ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1770{
1771	struct stat sb;
1772	struct ostat32 sb32;
1773	int error;
1774
1775	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1776	    &sb, NULL);
1777	if (error)
1778		return (error);
1779	copy_ostat(&sb, &sb32);
1780	error = copyout(&sb32, uap->ub, sizeof (sb32));
1781	return (error);
1782}
1783#endif
1784
1785int
1786freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1787{
1788	struct stat ub;
1789	struct stat32 ub32;
1790	int error;
1791
1792	error = kern_fstat(td, uap->fd, &ub);
1793	if (error)
1794		return (error);
1795	copy_stat(&ub, &ub32);
1796	error = copyout(&ub32, uap->ub, sizeof(ub32));
1797	return (error);
1798}
1799
1800#ifdef COMPAT_43
1801int
1802ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1803{
1804	struct stat ub;
1805	struct ostat32 ub32;
1806	int error;
1807
1808	error = kern_fstat(td, uap->fd, &ub);
1809	if (error)
1810		return (error);
1811	copy_ostat(&ub, &ub32);
1812	error = copyout(&ub32, uap->ub, sizeof(ub32));
1813	return (error);
1814}
1815#endif
1816
1817int
1818freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1819{
1820	struct stat ub;
1821	struct stat32 ub32;
1822	int error;
1823
1824	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1825	    &ub, NULL);
1826	if (error)
1827		return (error);
1828	copy_stat(&ub, &ub32);
1829	error = copyout(&ub32, uap->buf, sizeof(ub32));
1830	return (error);
1831}
1832
1833int
1834freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1835{
1836	struct stat sb;
1837	struct stat32 sb32;
1838	int error;
1839
1840	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1841	    UIO_USERSPACE, &sb, NULL);
1842	if (error)
1843		return (error);
1844	copy_stat(&sb, &sb32);
1845	error = copyout(&sb32, uap->ub, sizeof (sb32));
1846	return (error);
1847}
1848
1849#ifdef COMPAT_43
1850int
1851ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1852{
1853	struct stat sb;
1854	struct ostat32 sb32;
1855	int error;
1856
1857	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1858	    UIO_USERSPACE, &sb, NULL);
1859	if (error)
1860		return (error);
1861	copy_ostat(&sb, &sb32);
1862	error = copyout(&sb32, uap->ub, sizeof (sb32));
1863	return (error);
1864}
1865#endif
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	uint32_t tmp;
1873
1874	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1875		return (EINVAL);
1876 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1877 	if (error)
1878		return (error);
1879	if (uap->oldlenp) {
1880		error = fueword32(uap->oldlenp, &tmp);
1881		oldlen = tmp;
1882	} else {
1883		oldlen = 0;
1884	}
1885	if (error != 0)
1886		return (EFAULT);
1887	error = userland_sysctl(td, name, uap->namelen,
1888		uap->old, &oldlen, 1,
1889		uap->new, uap->newlen, &j, SCTL_MASK32);
1890	if (error && error != ENOMEM)
1891		return (error);
1892	if (uap->oldlenp)
1893		suword32(uap->oldlenp, j);
1894	return (0);
1895}
1896
1897int
1898freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1899{
1900	uint32_t version;
1901	int error;
1902	struct jail j;
1903
1904	error = copyin(uap->jail, &version, sizeof(uint32_t));
1905	if (error)
1906		return (error);
1907
1908	switch (version) {
1909	case 0:
1910	{
1911		/* FreeBSD single IPv4 jails. */
1912		struct jail32_v0 j32_v0;
1913
1914		bzero(&j, sizeof(struct jail));
1915		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1916		if (error)
1917			return (error);
1918		CP(j32_v0, j, version);
1919		PTRIN_CP(j32_v0, j, path);
1920		PTRIN_CP(j32_v0, j, hostname);
1921		j.ip4s = htonl(j32_v0.ip_number);	/* jail_v0 is host order */
1922		break;
1923	}
1924
1925	case 1:
1926		/*
1927		 * Version 1 was used by multi-IPv4 jail implementations
1928		 * that never made it into the official kernel.
1929		 */
1930		return (EINVAL);
1931
1932	case 2:	/* JAIL_API_VERSION */
1933	{
1934		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
1935		struct jail32 j32;
1936
1937		error = copyin(uap->jail, &j32, sizeof(struct jail32));
1938		if (error)
1939			return (error);
1940		CP(j32, j, version);
1941		PTRIN_CP(j32, j, path);
1942		PTRIN_CP(j32, j, hostname);
1943		PTRIN_CP(j32, j, jailname);
1944		CP(j32, j, ip4s);
1945		CP(j32, j, ip6s);
1946		PTRIN_CP(j32, j, ip4);
1947		PTRIN_CP(j32, j, ip6);
1948		break;
1949	}
1950
1951	default:
1952		/* Sci-Fi jails are not supported, sorry. */
1953		return (EINVAL);
1954	}
1955	return (kern_jail(td, &j));
1956}
1957
1958int
1959freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1960{
1961	struct uio *auio;
1962	int error;
1963
1964	/* Check that we have an even number of iovecs. */
1965	if (uap->iovcnt & 1)
1966		return (EINVAL);
1967
1968	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1969	if (error)
1970		return (error);
1971	error = kern_jail_set(td, auio, uap->flags);
1972	free(auio, M_IOV);
1973	return (error);
1974}
1975
1976int
1977freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1978{
1979	struct iovec32 iov32;
1980	struct uio *auio;
1981	int error, i;
1982
1983	/* Check that we have an even number of iovecs. */
1984	if (uap->iovcnt & 1)
1985		return (EINVAL);
1986
1987	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1988	if (error)
1989		return (error);
1990	error = kern_jail_get(td, auio, uap->flags);
1991	if (error == 0)
1992		for (i = 0; i < uap->iovcnt; i++) {
1993			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1994			CP(auio->uio_iov[i], iov32, iov_len);
1995			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1996			if (error != 0)
1997				break;
1998		}
1999	free(auio, M_IOV);
2000	return (error);
2001}
2002
2003int
2004freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2005{
2006	struct sigaction32 s32;
2007	struct sigaction sa, osa, *sap;
2008	int error;
2009
2010	if (uap->act) {
2011		error = copyin(uap->act, &s32, sizeof(s32));
2012		if (error)
2013			return (error);
2014		sa.sa_handler = PTRIN(s32.sa_u);
2015		CP(s32, sa, sa_flags);
2016		CP(s32, sa, sa_mask);
2017		sap = &sa;
2018	} else
2019		sap = NULL;
2020	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2021	if (error == 0 && uap->oact != NULL) {
2022		s32.sa_u = PTROUT(osa.sa_handler);
2023		CP(osa, s32, sa_flags);
2024		CP(osa, s32, sa_mask);
2025		error = copyout(&s32, uap->oact, sizeof(s32));
2026	}
2027	return (error);
2028}
2029
2030#ifdef COMPAT_FREEBSD4
2031int
2032freebsd4_freebsd32_sigaction(struct thread *td,
2033			     struct freebsd4_freebsd32_sigaction_args *uap)
2034{
2035	struct sigaction32 s32;
2036	struct sigaction sa, osa, *sap;
2037	int error;
2038
2039	if (uap->act) {
2040		error = copyin(uap->act, &s32, sizeof(s32));
2041		if (error)
2042			return (error);
2043		sa.sa_handler = PTRIN(s32.sa_u);
2044		CP(s32, sa, sa_flags);
2045		CP(s32, sa, sa_mask);
2046		sap = &sa;
2047	} else
2048		sap = NULL;
2049	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2050	if (error == 0 && uap->oact != NULL) {
2051		s32.sa_u = PTROUT(osa.sa_handler);
2052		CP(osa, s32, sa_flags);
2053		CP(osa, s32, sa_mask);
2054		error = copyout(&s32, uap->oact, sizeof(s32));
2055	}
2056	return (error);
2057}
2058#endif
2059
2060#ifdef COMPAT_43
2061struct osigaction32 {
2062	u_int32_t	sa_u;
2063	osigset_t	sa_mask;
2064	int		sa_flags;
2065};
2066
2067#define	ONSIG	32
2068
2069int
2070ofreebsd32_sigaction(struct thread *td,
2071			     struct ofreebsd32_sigaction_args *uap)
2072{
2073	struct osigaction32 s32;
2074	struct sigaction sa, osa, *sap;
2075	int error;
2076
2077	if (uap->signum <= 0 || uap->signum >= ONSIG)
2078		return (EINVAL);
2079
2080	if (uap->nsa) {
2081		error = copyin(uap->nsa, &s32, sizeof(s32));
2082		if (error)
2083			return (error);
2084		sa.sa_handler = PTRIN(s32.sa_u);
2085		CP(s32, sa, sa_flags);
2086		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2087		sap = &sa;
2088	} else
2089		sap = NULL;
2090	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2091	if (error == 0 && uap->osa != NULL) {
2092		s32.sa_u = PTROUT(osa.sa_handler);
2093		CP(osa, s32, sa_flags);
2094		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2095		error = copyout(&s32, uap->osa, sizeof(s32));
2096	}
2097	return (error);
2098}
2099
2100int
2101ofreebsd32_sigprocmask(struct thread *td,
2102			       struct ofreebsd32_sigprocmask_args *uap)
2103{
2104	sigset_t set, oset;
2105	int error;
2106
2107	OSIG2SIG(uap->mask, set);
2108	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2109	SIG2OSIG(oset, td->td_retval[0]);
2110	return (error);
2111}
2112
2113int
2114ofreebsd32_sigpending(struct thread *td,
2115			      struct ofreebsd32_sigpending_args *uap)
2116{
2117	struct proc *p = td->td_proc;
2118	sigset_t siglist;
2119
2120	PROC_LOCK(p);
2121	siglist = p->p_siglist;
2122	SIGSETOR(siglist, td->td_siglist);
2123	PROC_UNLOCK(p);
2124	SIG2OSIG(siglist, td->td_retval[0]);
2125	return (0);
2126}
2127
2128struct sigvec32 {
2129	u_int32_t	sv_handler;
2130	int		sv_mask;
2131	int		sv_flags;
2132};
2133
2134int
2135ofreebsd32_sigvec(struct thread *td,
2136			  struct ofreebsd32_sigvec_args *uap)
2137{
2138	struct sigvec32 vec;
2139	struct sigaction sa, osa, *sap;
2140	int error;
2141
2142	if (uap->signum <= 0 || uap->signum >= ONSIG)
2143		return (EINVAL);
2144
2145	if (uap->nsv) {
2146		error = copyin(uap->nsv, &vec, sizeof(vec));
2147		if (error)
2148			return (error);
2149		sa.sa_handler = PTRIN(vec.sv_handler);
2150		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2151		sa.sa_flags = vec.sv_flags;
2152		sa.sa_flags ^= SA_RESTART;
2153		sap = &sa;
2154	} else
2155		sap = NULL;
2156	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2157	if (error == 0 && uap->osv != NULL) {
2158		vec.sv_handler = PTROUT(osa.sa_handler);
2159		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2160		vec.sv_flags = osa.sa_flags;
2161		vec.sv_flags &= ~SA_NOCLDWAIT;
2162		vec.sv_flags ^= SA_RESTART;
2163		error = copyout(&vec, uap->osv, sizeof(vec));
2164	}
2165	return (error);
2166}
2167
2168int
2169ofreebsd32_sigblock(struct thread *td,
2170			    struct ofreebsd32_sigblock_args *uap)
2171{
2172	sigset_t set, oset;
2173
2174	OSIG2SIG(uap->mask, set);
2175	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2176	SIG2OSIG(oset, td->td_retval[0]);
2177	return (0);
2178}
2179
2180int
2181ofreebsd32_sigsetmask(struct thread *td,
2182			      struct ofreebsd32_sigsetmask_args *uap)
2183{
2184	sigset_t set, oset;
2185
2186	OSIG2SIG(uap->mask, set);
2187	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2188	SIG2OSIG(oset, td->td_retval[0]);
2189	return (0);
2190}
2191
2192int
2193ofreebsd32_sigsuspend(struct thread *td,
2194			      struct ofreebsd32_sigsuspend_args *uap)
2195{
2196	sigset_t mask;
2197
2198	OSIG2SIG(uap->mask, mask);
2199	return (kern_sigsuspend(td, mask));
2200}
2201
2202struct sigstack32 {
2203	u_int32_t	ss_sp;
2204	int		ss_onstack;
2205};
2206
2207int
2208ofreebsd32_sigstack(struct thread *td,
2209			    struct ofreebsd32_sigstack_args *uap)
2210{
2211	struct sigstack32 s32;
2212	struct sigstack nss, oss;
2213	int error = 0, unss;
2214
2215	if (uap->nss != NULL) {
2216		error = copyin(uap->nss, &s32, sizeof(s32));
2217		if (error)
2218			return (error);
2219		nss.ss_sp = PTRIN(s32.ss_sp);
2220		CP(s32, nss, ss_onstack);
2221		unss = 1;
2222	} else {
2223		unss = 0;
2224	}
2225	oss.ss_sp = td->td_sigstk.ss_sp;
2226	oss.ss_onstack = sigonstack(cpu_getstack(td));
2227	if (unss) {
2228		td->td_sigstk.ss_sp = nss.ss_sp;
2229		td->td_sigstk.ss_size = 0;
2230		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2231		td->td_pflags |= TDP_ALTSTACK;
2232	}
2233	if (uap->oss != NULL) {
2234		s32.ss_sp = PTROUT(oss.ss_sp);
2235		CP(oss, s32, ss_onstack);
2236		error = copyout(&s32, uap->oss, sizeof(s32));
2237	}
2238	return (error);
2239}
2240#endif
2241
2242int
2243freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2244{
2245	struct timespec32 rmt32, rqt32;
2246	struct timespec rmt, rqt;
2247	int error;
2248
2249	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2250	if (error)
2251		return (error);
2252
2253	CP(rqt32, rqt, tv_sec);
2254	CP(rqt32, rqt, tv_nsec);
2255
2256	if (uap->rmtp &&
2257	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2258		return (EFAULT);
2259	error = kern_nanosleep(td, &rqt, &rmt);
2260	if (error && uap->rmtp) {
2261		int error2;
2262
2263		CP(rmt, rmt32, tv_sec);
2264		CP(rmt, rmt32, tv_nsec);
2265
2266		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2267		if (error2)
2268			error = error2;
2269	}
2270	return (error);
2271}
2272
2273int
2274freebsd32_clock_gettime(struct thread *td,
2275			struct freebsd32_clock_gettime_args *uap)
2276{
2277	struct timespec	ats;
2278	struct timespec32 ats32;
2279	int error;
2280
2281	error = kern_clock_gettime(td, uap->clock_id, &ats);
2282	if (error == 0) {
2283		CP(ats, ats32, tv_sec);
2284		CP(ats, ats32, tv_nsec);
2285		error = copyout(&ats32, uap->tp, sizeof(ats32));
2286	}
2287	return (error);
2288}
2289
2290int
2291freebsd32_clock_settime(struct thread *td,
2292			struct freebsd32_clock_settime_args *uap)
2293{
2294	struct timespec	ats;
2295	struct timespec32 ats32;
2296	int error;
2297
2298	error = copyin(uap->tp, &ats32, sizeof(ats32));
2299	if (error)
2300		return (error);
2301	CP(ats32, ats, tv_sec);
2302	CP(ats32, ats, tv_nsec);
2303
2304	return (kern_clock_settime(td, uap->clock_id, &ats));
2305}
2306
2307int
2308freebsd32_clock_getres(struct thread *td,
2309		       struct freebsd32_clock_getres_args *uap)
2310{
2311	struct timespec	ts;
2312	struct timespec32 ts32;
2313	int error;
2314
2315	if (uap->tp == NULL)
2316		return (0);
2317	error = kern_clock_getres(td, uap->clock_id, &ts);
2318	if (error == 0) {
2319		CP(ts, ts32, tv_sec);
2320		CP(ts, ts32, tv_nsec);
2321		error = copyout(&ts32, uap->tp, sizeof(ts32));
2322	}
2323	return (error);
2324}
2325
2326int freebsd32_ktimer_create(struct thread *td,
2327    struct freebsd32_ktimer_create_args *uap)
2328{
2329	struct sigevent32 ev32;
2330	struct sigevent ev, *evp;
2331	int error, id;
2332
2333	if (uap->evp == NULL) {
2334		evp = NULL;
2335	} else {
2336		evp = &ev;
2337		error = copyin(uap->evp, &ev32, sizeof(ev32));
2338		if (error != 0)
2339			return (error);
2340		error = convert_sigevent32(&ev32, &ev);
2341		if (error != 0)
2342			return (error);
2343	}
2344	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2345	if (error == 0) {
2346		error = copyout(&id, uap->timerid, sizeof(int));
2347		if (error != 0)
2348			kern_ktimer_delete(td, id);
2349	}
2350	return (error);
2351}
2352
2353int
2354freebsd32_ktimer_settime(struct thread *td,
2355    struct freebsd32_ktimer_settime_args *uap)
2356{
2357	struct itimerspec32 val32, oval32;
2358	struct itimerspec val, oval, *ovalp;
2359	int error;
2360
2361	error = copyin(uap->value, &val32, sizeof(val32));
2362	if (error != 0)
2363		return (error);
2364	ITS_CP(val32, val);
2365	ovalp = uap->ovalue != NULL ? &oval : NULL;
2366	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2367	if (error == 0 && uap->ovalue != NULL) {
2368		ITS_CP(oval, oval32);
2369		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2370	}
2371	return (error);
2372}
2373
2374int
2375freebsd32_ktimer_gettime(struct thread *td,
2376    struct freebsd32_ktimer_gettime_args *uap)
2377{
2378	struct itimerspec32 val32;
2379	struct itimerspec val;
2380	int error;
2381
2382	error = kern_ktimer_gettime(td, uap->timerid, &val);
2383	if (error == 0) {
2384		ITS_CP(val, val32);
2385		error = copyout(&val32, uap->value, sizeof(val32));
2386	}
2387	return (error);
2388}
2389
2390int
2391freebsd32_clock_getcpuclockid2(struct thread *td,
2392    struct freebsd32_clock_getcpuclockid2_args *uap)
2393{
2394	clockid_t clk_id;
2395	int error;
2396
2397	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2398	    uap->which, &clk_id);
2399	if (error == 0)
2400		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2401	return (error);
2402}
2403
2404int
2405freebsd32_thr_new(struct thread *td,
2406		  struct freebsd32_thr_new_args *uap)
2407{
2408	struct thr_param32 param32;
2409	struct thr_param param;
2410	int error;
2411
2412	if (uap->param_size < 0 ||
2413	    uap->param_size > sizeof(struct thr_param32))
2414		return (EINVAL);
2415	bzero(&param, sizeof(struct thr_param));
2416	bzero(&param32, sizeof(struct thr_param32));
2417	error = copyin(uap->param, &param32, uap->param_size);
2418	if (error != 0)
2419		return (error);
2420	param.start_func = PTRIN(param32.start_func);
2421	param.arg = PTRIN(param32.arg);
2422	param.stack_base = PTRIN(param32.stack_base);
2423	param.stack_size = param32.stack_size;
2424	param.tls_base = PTRIN(param32.tls_base);
2425	param.tls_size = param32.tls_size;
2426	param.child_tid = PTRIN(param32.child_tid);
2427	param.parent_tid = PTRIN(param32.parent_tid);
2428	param.flags = param32.flags;
2429	param.rtp = PTRIN(param32.rtp);
2430	param.spare[0] = PTRIN(param32.spare[0]);
2431	param.spare[1] = PTRIN(param32.spare[1]);
2432	param.spare[2] = PTRIN(param32.spare[2]);
2433
2434	return (kern_thr_new(td, &param));
2435}
2436
2437int
2438freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2439{
2440	struct timespec32 ts32;
2441	struct timespec ts, *tsp;
2442	int error;
2443
2444	error = 0;
2445	tsp = NULL;
2446	if (uap->timeout != NULL) {
2447		error = copyin((const void *)uap->timeout, (void *)&ts32,
2448		    sizeof(struct timespec32));
2449		if (error != 0)
2450			return (error);
2451		ts.tv_sec = ts32.tv_sec;
2452		ts.tv_nsec = ts32.tv_nsec;
2453		tsp = &ts;
2454	}
2455	return (kern_thr_suspend(td, tsp));
2456}
2457
2458void
2459siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2460{
2461	bzero(dst, sizeof(*dst));
2462	dst->si_signo = src->si_signo;
2463	dst->si_errno = src->si_errno;
2464	dst->si_code = src->si_code;
2465	dst->si_pid = src->si_pid;
2466	dst->si_uid = src->si_uid;
2467	dst->si_status = src->si_status;
2468	dst->si_addr = (uintptr_t)src->si_addr;
2469	dst->si_value.sival_int = src->si_value.sival_int;
2470	dst->si_timerid = src->si_timerid;
2471	dst->si_overrun = src->si_overrun;
2472}
2473
2474int
2475freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2476{
2477	struct timespec32 ts32;
2478	struct timespec ts;
2479	struct timespec *timeout;
2480	sigset_t set;
2481	ksiginfo_t ksi;
2482	struct siginfo32 si32;
2483	int error;
2484
2485	if (uap->timeout) {
2486		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2487		if (error)
2488			return (error);
2489		ts.tv_sec = ts32.tv_sec;
2490		ts.tv_nsec = ts32.tv_nsec;
2491		timeout = &ts;
2492	} else
2493		timeout = NULL;
2494
2495	error = copyin(uap->set, &set, sizeof(set));
2496	if (error)
2497		return (error);
2498
2499	error = kern_sigtimedwait(td, set, &ksi, timeout);
2500	if (error)
2501		return (error);
2502
2503	if (uap->info) {
2504		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2505		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2506	}
2507
2508	if (error == 0)
2509		td->td_retval[0] = ksi.ksi_signo;
2510	return (error);
2511}
2512
2513/*
2514 * MPSAFE
2515 */
2516int
2517freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2518{
2519	ksiginfo_t ksi;
2520	struct siginfo32 si32;
2521	sigset_t set;
2522	int error;
2523
2524	error = copyin(uap->set, &set, sizeof(set));
2525	if (error)
2526		return (error);
2527
2528	error = kern_sigtimedwait(td, set, &ksi, NULL);
2529	if (error)
2530		return (error);
2531
2532	if (uap->info) {
2533		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2534		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2535	}
2536	if (error == 0)
2537		td->td_retval[0] = ksi.ksi_signo;
2538	return (error);
2539}
2540
2541int
2542freebsd32_cpuset_setid(struct thread *td,
2543    struct freebsd32_cpuset_setid_args *uap)
2544{
2545	struct cpuset_setid_args ap;
2546
2547	ap.which = uap->which;
2548	ap.id = PAIR32TO64(id_t,uap->id);
2549	ap.setid = uap->setid;
2550
2551	return (sys_cpuset_setid(td, &ap));
2552}
2553
2554int
2555freebsd32_cpuset_getid(struct thread *td,
2556    struct freebsd32_cpuset_getid_args *uap)
2557{
2558	struct cpuset_getid_args ap;
2559
2560	ap.level = uap->level;
2561	ap.which = uap->which;
2562	ap.id = PAIR32TO64(id_t,uap->id);
2563	ap.setid = uap->setid;
2564
2565	return (sys_cpuset_getid(td, &ap));
2566}
2567
2568int
2569freebsd32_cpuset_getaffinity(struct thread *td,
2570    struct freebsd32_cpuset_getaffinity_args *uap)
2571{
2572	struct cpuset_getaffinity_args ap;
2573
2574	ap.level = uap->level;
2575	ap.which = uap->which;
2576	ap.id = PAIR32TO64(id_t,uap->id);
2577	ap.cpusetsize = uap->cpusetsize;
2578	ap.mask = uap->mask;
2579
2580	return (sys_cpuset_getaffinity(td, &ap));
2581}
2582
2583int
2584freebsd32_cpuset_setaffinity(struct thread *td,
2585    struct freebsd32_cpuset_setaffinity_args *uap)
2586{
2587	struct cpuset_setaffinity_args ap;
2588
2589	ap.level = uap->level;
2590	ap.which = uap->which;
2591	ap.id = PAIR32TO64(id_t,uap->id);
2592	ap.cpusetsize = uap->cpusetsize;
2593	ap.mask = uap->mask;
2594
2595	return (sys_cpuset_setaffinity(td, &ap));
2596}
2597
2598int
2599freebsd32_nmount(struct thread *td,
2600    struct freebsd32_nmount_args /* {
2601    	struct iovec *iovp;
2602    	unsigned int iovcnt;
2603    	int flags;
2604    } */ *uap)
2605{
2606	struct uio *auio;
2607	uint64_t flags;
2608	int error;
2609
2610	/*
2611	 * Mount flags are now 64-bits. On 32-bit archtectures only
2612	 * 32-bits are passed in, but from here on everything handles
2613	 * 64-bit flags correctly.
2614	 */
2615	flags = uap->flags;
2616
2617	AUDIT_ARG_FFLAGS(flags);
2618
2619	/*
2620	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2621	 * userspace to set this flag, but we must filter it out if we want
2622	 * MNT_UPDATE on the root file system to work.
2623	 * MNT_ROOTFS should only be set by the kernel when mounting its
2624	 * root file system.
2625	 */
2626	flags &= ~MNT_ROOTFS;
2627
2628	/*
2629	 * check that we have an even number of iovec's
2630	 * and that we have at least two options.
2631	 */
2632	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2633		return (EINVAL);
2634
2635	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2636	if (error)
2637		return (error);
2638	error = vfs_donmount(td, flags, auio);
2639
2640	free(auio, M_IOV);
2641	return error;
2642}
2643
2644#if 0
2645int
2646freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2647{
2648	struct yyy32 *p32, s32;
2649	struct yyy *p = NULL, s;
2650	struct xxx_arg ap;
2651	int error;
2652
2653	if (uap->zzz) {
2654		error = copyin(uap->zzz, &s32, sizeof(s32));
2655		if (error)
2656			return (error);
2657		/* translate in */
2658		p = &s;
2659	}
2660	error = kern_xxx(td, p);
2661	if (error)
2662		return (error);
2663	if (uap->zzz) {
2664		/* translate out */
2665		error = copyout(&s32, p32, sizeof(s32));
2666	}
2667	return (error);
2668}
2669#endif
2670
2671int
2672syscall32_register(int *offset, struct sysent *new_sysent,
2673    struct sysent *old_sysent, int flags)
2674{
2675
2676	if ((flags & ~SY_THR_STATIC) != 0)
2677		return (EINVAL);
2678
2679	if (*offset == NO_SYSCALL) {
2680		int i;
2681
2682		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2683			if (freebsd32_sysent[i].sy_call ==
2684			    (sy_call_t *)lkmnosys)
2685				break;
2686		if (i == SYS_MAXSYSCALL)
2687			return (ENFILE);
2688		*offset = i;
2689	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2690		return (EINVAL);
2691	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2692	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2693		return (EEXIST);
2694
2695	*old_sysent = freebsd32_sysent[*offset];
2696	freebsd32_sysent[*offset] = *new_sysent;
2697	atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2698	return (0);
2699}
2700
2701int
2702syscall32_deregister(int *offset, struct sysent *old_sysent)
2703{
2704
2705	if (*offset == 0)
2706		return (0);
2707
2708	freebsd32_sysent[*offset] = *old_sysent;
2709	return (0);
2710}
2711
2712int
2713syscall32_module_handler(struct module *mod, int what, void *arg)
2714{
2715	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2716	modspecific_t ms;
2717	int error;
2718
2719	switch (what) {
2720	case MOD_LOAD:
2721		error = syscall32_register(data->offset, data->new_sysent,
2722		    &data->old_sysent, SY_THR_STATIC_KLD);
2723		if (error) {
2724			/* Leave a mark so we know to safely unload below. */
2725			data->offset = NULL;
2726			return error;
2727		}
2728		ms.intval = *data->offset;
2729		MOD_XLOCK;
2730		module_setspecific(mod, &ms);
2731		MOD_XUNLOCK;
2732		if (data->chainevh)
2733			error = data->chainevh(mod, what, data->chainarg);
2734		return (error);
2735	case MOD_UNLOAD:
2736		/*
2737		 * MOD_LOAD failed, so just return without calling the
2738		 * chained handler since we didn't pass along the MOD_LOAD
2739		 * event.
2740		 */
2741		if (data->offset == NULL)
2742			return (0);
2743		if (data->chainevh) {
2744			error = data->chainevh(mod, what, data->chainarg);
2745			if (error)
2746				return (error);
2747		}
2748		error = syscall32_deregister(data->offset, &data->old_sysent);
2749		return (error);
2750	default:
2751		error = EOPNOTSUPP;
2752		if (data->chainevh)
2753			error = data->chainevh(mod, what, data->chainarg);
2754		return (error);
2755	}
2756}
2757
2758int
2759syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2760{
2761	struct syscall_helper_data *sd1;
2762	int error;
2763
2764	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2765		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2766		    &sd1->old_sysent, flags);
2767		if (error != 0) {
2768			syscall32_helper_unregister(sd);
2769			return (error);
2770		}
2771		sd1->registered = 1;
2772	}
2773	return (0);
2774}
2775
2776int
2777syscall32_helper_unregister(struct syscall_helper_data *sd)
2778{
2779	struct syscall_helper_data *sd1;
2780
2781	for (sd1 = sd; sd1->registered != 0; sd1++) {
2782		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2783		sd1->registered = 0;
2784	}
2785	return (0);
2786}
2787
2788register_t *
2789freebsd32_copyout_strings(struct image_params *imgp)
2790{
2791	int argc, envc, i;
2792	u_int32_t *vectp;
2793	char *stringp;
2794	uintptr_t destp;
2795	u_int32_t *stack_base;
2796	struct freebsd32_ps_strings *arginfo;
2797	char canary[sizeof(long) * 8];
2798	int32_t pagesizes32[MAXPAGESIZES];
2799	size_t execpath_len;
2800	int szsigcode;
2801
2802	/*
2803	 * Calculate string base and vector table pointers.
2804	 * Also deal with signal trampoline code for this exec type.
2805	 */
2806	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2807		execpath_len = strlen(imgp->execpath) + 1;
2808	else
2809		execpath_len = 0;
2810	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2811	    sv_psstrings;
2812	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2813		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2814	else
2815		szsigcode = 0;
2816	destp =	(uintptr_t)arginfo;
2817
2818	/*
2819	 * install sigcode
2820	 */
2821	if (szsigcode != 0) {
2822		destp -= szsigcode;
2823		destp = rounddown2(destp, sizeof(uint32_t));
2824		copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2825		    szsigcode);
2826	}
2827
2828	/*
2829	 * Copy the image path for the rtld.
2830	 */
2831	if (execpath_len != 0) {
2832		destp -= execpath_len;
2833		imgp->execpathp = destp;
2834		copyout(imgp->execpath, (void *)destp, execpath_len);
2835	}
2836
2837	/*
2838	 * Prepare the canary for SSP.
2839	 */
2840	arc4rand(canary, sizeof(canary), 0);
2841	destp -= sizeof(canary);
2842	imgp->canary = destp;
2843	copyout(canary, (void *)destp, sizeof(canary));
2844	imgp->canarylen = sizeof(canary);
2845
2846	/*
2847	 * Prepare the pagesizes array.
2848	 */
2849	for (i = 0; i < MAXPAGESIZES; i++)
2850		pagesizes32[i] = (uint32_t)pagesizes[i];
2851	destp -= sizeof(pagesizes32);
2852	destp = rounddown2(destp, sizeof(uint32_t));
2853	imgp->pagesizes = destp;
2854	copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2855	imgp->pagesizeslen = sizeof(pagesizes32);
2856
2857	destp -= ARG_MAX - imgp->args->stringspace;
2858	destp = rounddown2(destp, sizeof(uint32_t));
2859
2860	/*
2861	 * If we have a valid auxargs ptr, prepare some room
2862	 * on the stack.
2863	 */
2864	if (imgp->auxargs) {
2865		/*
2866		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2867		 * lower compatibility.
2868		 */
2869		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2870			: (AT_COUNT * 2);
2871		/*
2872		 * The '+ 2' is for the null pointers at the end of each of
2873		 * the arg and env vector sets,and imgp->auxarg_size is room
2874		 * for argument of Runtime loader.
2875		 */
2876		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2877		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2878		    sizeof(u_int32_t));
2879	} else {
2880		/*
2881		 * The '+ 2' is for the null pointers at the end of each of
2882		 * the arg and env vector sets
2883		 */
2884		vectp = (u_int32_t *)(destp - (imgp->args->argc +
2885		    imgp->args->envc + 2) * sizeof(u_int32_t));
2886	}
2887
2888	/*
2889	 * vectp also becomes our initial stack base
2890	 */
2891	stack_base = vectp;
2892
2893	stringp = imgp->args->begin_argv;
2894	argc = imgp->args->argc;
2895	envc = imgp->args->envc;
2896	/*
2897	 * Copy out strings - arguments and environment.
2898	 */
2899	copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2900
2901	/*
2902	 * Fill in "ps_strings" struct for ps, w, etc.
2903	 */
2904	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2905	suword32(&arginfo->ps_nargvstr, argc);
2906
2907	/*
2908	 * Fill in argument portion of vector table.
2909	 */
2910	for (; argc > 0; --argc) {
2911		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2912		while (*stringp++ != 0)
2913			destp++;
2914		destp++;
2915	}
2916
2917	/* a null vector table pointer separates the argp's from the envp's */
2918	suword32(vectp++, 0);
2919
2920	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2921	suword32(&arginfo->ps_nenvstr, envc);
2922
2923	/*
2924	 * Fill in environment portion of vector table.
2925	 */
2926	for (; envc > 0; --envc) {
2927		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2928		while (*stringp++ != 0)
2929			destp++;
2930		destp++;
2931	}
2932
2933	/* end of vector table is a null pointer */
2934	suword32(vectp, 0);
2935
2936	return ((register_t *)stack_base);
2937}
2938
2939int
2940freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2941{
2942	struct kld_file_stat stat;
2943	struct kld32_file_stat stat32;
2944	int error, version;
2945
2946	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2947	    != 0)
2948		return (error);
2949	if (version != sizeof(struct kld32_file_stat_1) &&
2950	    version != sizeof(struct kld32_file_stat))
2951		return (EINVAL);
2952
2953	error = kern_kldstat(td, uap->fileid, &stat);
2954	if (error != 0)
2955		return (error);
2956
2957	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2958	CP(stat, stat32, refs);
2959	CP(stat, stat32, id);
2960	PTROUT_CP(stat, stat32, address);
2961	CP(stat, stat32, size);
2962	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2963	return (copyout(&stat32, uap->stat, version));
2964}
2965
2966int
2967freebsd32_posix_fallocate(struct thread *td,
2968    struct freebsd32_posix_fallocate_args *uap)
2969{
2970	int error;
2971
2972	error = kern_posix_fallocate(td, uap->fd,
2973	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
2974	return (kern_posix_error(td, error));
2975}
2976
2977int
2978freebsd32_posix_fadvise(struct thread *td,
2979    struct freebsd32_posix_fadvise_args *uap)
2980{
2981	int error;
2982
2983	error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2984	    PAIR32TO64(off_t, uap->len), uap->advice);
2985	return (kern_posix_error(td, error));
2986}
2987
2988int
2989convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2990{
2991
2992	CP(*sig32, *sig, sigev_notify);
2993	switch (sig->sigev_notify) {
2994	case SIGEV_NONE:
2995		break;
2996	case SIGEV_THREAD_ID:
2997		CP(*sig32, *sig, sigev_notify_thread_id);
2998		/* FALLTHROUGH */
2999	case SIGEV_SIGNAL:
3000		CP(*sig32, *sig, sigev_signo);
3001		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3002		break;
3003	case SIGEV_KEVENT:
3004		CP(*sig32, *sig, sigev_notify_kqueue);
3005		CP(*sig32, *sig, sigev_notify_kevent_flags);
3006		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3007		break;
3008	default:
3009		return (EINVAL);
3010	}
3011	return (0);
3012}
3013
3014int
3015freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3016{
3017	void *data;
3018	union {
3019		struct procctl_reaper_status rs;
3020		struct procctl_reaper_pids rp;
3021		struct procctl_reaper_kill rk;
3022	} x;
3023	union {
3024		struct procctl_reaper_pids32 rp;
3025	} x32;
3026	int error, error1, flags;
3027
3028	switch (uap->com) {
3029	case PROC_SPROTECT:
3030	case PROC_TRACE_CTL:
3031	case PROC_TRAPCAP_CTL:
3032		error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3033		if (error != 0)
3034			return (error);
3035		data = &flags;
3036		break;
3037	case PROC_REAP_ACQUIRE:
3038	case PROC_REAP_RELEASE:
3039		if (uap->data != NULL)
3040			return (EINVAL);
3041		data = NULL;
3042		break;
3043	case PROC_REAP_STATUS:
3044		data = &x.rs;
3045		break;
3046	case PROC_REAP_GETPIDS:
3047		error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3048		if (error != 0)
3049			return (error);
3050		CP(x32.rp, x.rp, rp_count);
3051		PTRIN_CP(x32.rp, x.rp, rp_pids);
3052		data = &x.rp;
3053		break;
3054	case PROC_REAP_KILL:
3055		error = copyin(uap->data, &x.rk, sizeof(x.rk));
3056		if (error != 0)
3057			return (error);
3058		data = &x.rk;
3059		break;
3060	case PROC_TRACE_STATUS:
3061	case PROC_TRAPCAP_STATUS:
3062		data = &flags;
3063		break;
3064	default:
3065		return (EINVAL);
3066	}
3067	error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3068	    uap->com, data);
3069	switch (uap->com) {
3070	case PROC_REAP_STATUS:
3071		if (error == 0)
3072			error = copyout(&x.rs, uap->data, sizeof(x.rs));
3073		break;
3074	case PROC_REAP_KILL:
3075		error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3076		if (error == 0)
3077			error = error1;
3078		break;
3079	case PROC_TRACE_STATUS:
3080	case PROC_TRAPCAP_STATUS:
3081		if (error == 0)
3082			error = copyout(&flags, uap->data, sizeof(flags));
3083		break;
3084	}
3085	return (error);
3086}
3087
3088int
3089freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3090{
3091	long tmp;
3092
3093	switch (uap->cmd) {
3094	/*
3095	 * Do unsigned conversion for arg when operation
3096	 * interprets it as flags or pointer.
3097	 */
3098	case F_SETLK_REMOTE:
3099	case F_SETLKW:
3100	case F_SETLK:
3101	case F_GETLK:
3102	case F_SETFD:
3103	case F_SETFL:
3104	case F_OGETLK:
3105	case F_OSETLK:
3106	case F_OSETLKW:
3107		tmp = (unsigned int)(uap->arg);
3108		break;
3109	default:
3110		tmp = uap->arg;
3111		break;
3112	}
3113	return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3114}
3115
3116int
3117freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3118{
3119	struct timespec32 ts32;
3120	struct timespec ts, *tsp;
3121	sigset_t set, *ssp;
3122	int error;
3123
3124	if (uap->ts != NULL) {
3125		error = copyin(uap->ts, &ts32, sizeof(ts32));
3126		if (error != 0)
3127			return (error);
3128		CP(ts32, ts, tv_sec);
3129		CP(ts32, ts, tv_nsec);
3130		tsp = &ts;
3131	} else
3132		tsp = NULL;
3133	if (uap->set != NULL) {
3134		error = copyin(uap->set, &set, sizeof(set));
3135		if (error != 0)
3136			return (error);
3137		ssp = &set;
3138	} else
3139		ssp = NULL;
3140
3141	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3142}
3143