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