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