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