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