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