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