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