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