freebsd32_misc.c revision 315548
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: stable/11/sys/compat/freebsd32/freebsd32_misc.c 315548 2017-03-19 14:12:55Z trasz $");
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,
248    struct freebsd4_freebsd32_getfsstat_args *uap)
249{
250	struct statfs *buf, *sp;
251	struct statfs32 stat32;
252	size_t count, size, copycount;
253	int error;
254
255	count = uap->bufsize / sizeof(struct statfs32);
256	size = count * sizeof(struct statfs);
257	error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
258	if (size > 0) {
259		sp = buf;
260		copycount = count;
261		while (copycount > 0 && error == 0) {
262			copy_statfs(sp, &stat32);
263			error = copyout(&stat32, uap->buf, sizeof(stat32));
264			sp++;
265			uap->buf++;
266			copycount--;
267		}
268		free(buf, M_STATFS);
269	}
270	if (error == 0)
271		td->td_retval[0] = count;
272	return (error);
273}
274#endif
275
276#ifdef COMPAT_FREEBSD10
277int
278freebsd10_freebsd32_pipe(struct thread *td,
279    struct freebsd10_freebsd32_pipe_args *uap) {
280
281	return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
282}
283#endif
284
285int
286freebsd32_sigaltstack(struct thread *td,
287		      struct freebsd32_sigaltstack_args *uap)
288{
289	struct sigaltstack32 s32;
290	struct sigaltstack ss, oss, *ssp;
291	int error;
292
293	if (uap->ss != NULL) {
294		error = copyin(uap->ss, &s32, sizeof(s32));
295		if (error)
296			return (error);
297		PTRIN_CP(s32, ss, ss_sp);
298		CP(s32, ss, ss_size);
299		CP(s32, ss, ss_flags);
300		ssp = &ss;
301	} else
302		ssp = NULL;
303	error = kern_sigaltstack(td, ssp, &oss);
304	if (error == 0 && uap->oss != NULL) {
305		PTROUT_CP(oss, s32, ss_sp);
306		CP(oss, s32, ss_size);
307		CP(oss, s32, ss_flags);
308		error = copyout(&s32, uap->oss, sizeof(s32));
309	}
310	return (error);
311}
312
313/*
314 * Custom version of exec_copyin_args() so that we can translate
315 * the pointers.
316 */
317int
318freebsd32_exec_copyin_args(struct image_args *args, char *fname,
319    enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
320{
321	char *argp, *envp;
322	u_int32_t *p32, arg;
323	size_t length;
324	int error;
325
326	bzero(args, sizeof(*args));
327	if (argv == NULL)
328		return (EFAULT);
329
330	/*
331	 * Allocate demand-paged memory for the file name, argument, and
332	 * environment strings.
333	 */
334	error = exec_alloc_args(args);
335	if (error != 0)
336		return (error);
337
338	/*
339	 * Copy the file name.
340	 */
341	if (fname != NULL) {
342		args->fname = args->buf;
343		error = (segflg == UIO_SYSSPACE) ?
344		    copystr(fname, args->fname, PATH_MAX, &length) :
345		    copyinstr(fname, args->fname, PATH_MAX, &length);
346		if (error != 0)
347			goto err_exit;
348	} else
349		length = 0;
350
351	args->begin_argv = args->buf + length;
352	args->endp = args->begin_argv;
353	args->stringspace = ARG_MAX;
354
355	/*
356	 * extract arguments first
357	 */
358	p32 = argv;
359	for (;;) {
360		error = copyin(p32++, &arg, sizeof(arg));
361		if (error)
362			goto err_exit;
363		if (arg == 0)
364			break;
365		argp = PTRIN(arg);
366		error = copyinstr(argp, args->endp, args->stringspace, &length);
367		if (error) {
368			if (error == ENAMETOOLONG)
369				error = E2BIG;
370			goto err_exit;
371		}
372		args->stringspace -= length;
373		args->endp += length;
374		args->argc++;
375	}
376
377	args->begin_envv = args->endp;
378
379	/*
380	 * extract environment strings
381	 */
382	if (envv) {
383		p32 = envv;
384		for (;;) {
385			error = copyin(p32++, &arg, sizeof(arg));
386			if (error)
387				goto err_exit;
388			if (arg == 0)
389				break;
390			envp = PTRIN(arg);
391			error = copyinstr(envp, args->endp, args->stringspace,
392			    &length);
393			if (error) {
394				if (error == ENAMETOOLONG)
395					error = E2BIG;
396				goto err_exit;
397			}
398			args->stringspace -= length;
399			args->endp += length;
400			args->envc++;
401		}
402	}
403
404	return (0);
405
406err_exit:
407	exec_free_args(args);
408	return (error);
409}
410
411int
412freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
413{
414	struct image_args eargs;
415	struct vmspace *oldvmspace;
416	int error;
417
418	error = pre_execve(td, &oldvmspace);
419	if (error != 0)
420		return (error);
421	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
422	    uap->argv, uap->envv);
423	if (error == 0)
424		error = kern_execve(td, &eargs, NULL);
425	post_execve(td, error, oldvmspace);
426	return (error);
427}
428
429int
430freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
431{
432	struct image_args eargs;
433	struct vmspace *oldvmspace;
434	int error;
435
436	error = pre_execve(td, &oldvmspace);
437	if (error != 0)
438		return (error);
439	error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
440	    uap->argv, uap->envv);
441	if (error == 0) {
442		eargs.fd = uap->fd;
443		error = kern_execve(td, &eargs, NULL);
444	}
445	post_execve(td, error, oldvmspace);
446	return (error);
447}
448
449int
450freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
451{
452	int prot;
453
454	prot = uap->prot;
455#if defined(__amd64__)
456	if (i386_read_exec && (prot & PROT_READ) != 0)
457		prot |= PROT_EXEC;
458#endif
459	return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
460	    prot));
461}
462
463int
464freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
465{
466	int prot;
467
468	prot = uap->prot;
469#if defined(__amd64__)
470	if (i386_read_exec && (prot & PROT_READ))
471		prot |= PROT_EXEC;
472#endif
473
474	return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
475	    uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
476}
477
478#ifdef COMPAT_FREEBSD6
479int
480freebsd6_freebsd32_mmap(struct thread *td,
481    struct freebsd6_freebsd32_mmap_args *uap)
482{
483	int prot;
484
485	prot = uap->prot;
486#if defined(__amd64__)
487	if (i386_read_exec && (prot & PROT_READ))
488		prot |= PROT_EXEC;
489#endif
490
491	return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
492	    uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
493}
494#endif
495
496int
497freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
498{
499	struct itimerval itv, oitv, *itvp;
500	struct itimerval32 i32;
501	int error;
502
503	if (uap->itv != NULL) {
504		error = copyin(uap->itv, &i32, sizeof(i32));
505		if (error)
506			return (error);
507		TV_CP(i32, itv, it_interval);
508		TV_CP(i32, itv, it_value);
509		itvp = &itv;
510	} else
511		itvp = NULL;
512	error = kern_setitimer(td, uap->which, itvp, &oitv);
513	if (error || uap->oitv == NULL)
514		return (error);
515	TV_CP(oitv, i32, it_interval);
516	TV_CP(oitv, i32, it_value);
517	return (copyout(&i32, uap->oitv, sizeof(i32)));
518}
519
520int
521freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
522{
523	struct itimerval itv;
524	struct itimerval32 i32;
525	int error;
526
527	error = kern_getitimer(td, uap->which, &itv);
528	if (error || uap->itv == NULL)
529		return (error);
530	TV_CP(itv, i32, it_interval);
531	TV_CP(itv, i32, it_value);
532	return (copyout(&i32, uap->itv, sizeof(i32)));
533}
534
535int
536freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
537{
538	struct timeval32 tv32;
539	struct timeval tv, *tvp;
540	int error;
541
542	if (uap->tv != NULL) {
543		error = copyin(uap->tv, &tv32, sizeof(tv32));
544		if (error)
545			return (error);
546		CP(tv32, tv, tv_sec);
547		CP(tv32, tv, tv_usec);
548		tvp = &tv;
549	} else
550		tvp = NULL;
551	/*
552	 * XXX Do pointers need PTRIN()?
553	 */
554	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
555	    sizeof(int32_t) * 8));
556}
557
558int
559freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
560{
561	struct timespec32 ts32;
562	struct timespec ts;
563	struct timeval tv, *tvp;
564	sigset_t set, *uset;
565	int error;
566
567	if (uap->ts != NULL) {
568		error = copyin(uap->ts, &ts32, sizeof(ts32));
569		if (error != 0)
570			return (error);
571		CP(ts32, ts, tv_sec);
572		CP(ts32, ts, tv_nsec);
573		TIMESPEC_TO_TIMEVAL(&tv, &ts);
574		tvp = &tv;
575	} else
576		tvp = NULL;
577	if (uap->sm != NULL) {
578		error = copyin(uap->sm, &set, sizeof(set));
579		if (error != 0)
580			return (error);
581		uset = &set;
582	} else
583		uset = NULL;
584	/*
585	 * XXX Do pointers need PTRIN()?
586	 */
587	error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
588	    uset, sizeof(int32_t) * 8);
589	return (error);
590}
591
592/*
593 * Copy 'count' items into the destination list pointed to by uap->eventlist.
594 */
595static int
596freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
597{
598	struct freebsd32_kevent_args *uap;
599	struct kevent32	ks32[KQ_NEVENTS];
600	int i, error = 0;
601
602	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
603	uap = (struct freebsd32_kevent_args *)arg;
604
605	for (i = 0; i < count; i++) {
606		CP(kevp[i], ks32[i], ident);
607		CP(kevp[i], ks32[i], filter);
608		CP(kevp[i], ks32[i], flags);
609		CP(kevp[i], ks32[i], fflags);
610		CP(kevp[i], ks32[i], data);
611		PTROUT_CP(kevp[i], ks32[i], udata);
612	}
613	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
614	if (error == 0)
615		uap->eventlist += count;
616	return (error);
617}
618
619/*
620 * Copy 'count' items from the list pointed to by uap->changelist.
621 */
622static int
623freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
624{
625	struct freebsd32_kevent_args *uap;
626	struct kevent32	ks32[KQ_NEVENTS];
627	int i, error = 0;
628
629	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
630	uap = (struct freebsd32_kevent_args *)arg;
631
632	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
633	if (error)
634		goto done;
635	uap->changelist += count;
636
637	for (i = 0; i < count; i++) {
638		CP(ks32[i], kevp[i], ident);
639		CP(ks32[i], kevp[i], filter);
640		CP(ks32[i], kevp[i], flags);
641		CP(ks32[i], kevp[i], fflags);
642		CP(ks32[i], kevp[i], data);
643		PTRIN_CP(ks32[i], kevp[i], udata);
644	}
645done:
646	return (error);
647}
648
649int
650freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
651{
652	struct timespec32 ts32;
653	struct timespec ts, *tsp;
654	struct kevent_copyops k_ops = {
655		.arg = uap,
656		.k_copyout = freebsd32_kevent_copyout,
657		.k_copyin = freebsd32_kevent_copyin,
658	};
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 *sp;
1388	int error;
1389
1390	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1391	error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1392	if (error == 0) {
1393		copy_statfs(sp, &s32);
1394		error = copyout(&s32, uap->buf, sizeof(s32));
1395	}
1396	free(sp, M_STATFS);
1397	return (error);
1398}
1399#endif
1400
1401#ifdef COMPAT_FREEBSD4
1402int
1403freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1404{
1405	struct statfs32 s32;
1406	struct statfs *sp;
1407	int error;
1408
1409	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1410	error = kern_fstatfs(td, uap->fd, sp);
1411	if (error == 0) {
1412		copy_statfs(sp, &s32);
1413		error = copyout(&s32, uap->buf, sizeof(s32));
1414	}
1415	free(sp, M_STATFS);
1416	return (error);
1417}
1418#endif
1419
1420#ifdef COMPAT_FREEBSD4
1421int
1422freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1423{
1424	struct statfs32 s32;
1425	struct statfs *sp;
1426	fhandle_t fh;
1427	int error;
1428
1429	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1430		return (error);
1431	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1432	error = kern_fhstatfs(td, fh, sp);
1433	if (error == 0) {
1434		copy_statfs(sp, &s32);
1435		error = copyout(&s32, uap->buf, sizeof(s32));
1436	}
1437	free(sp, M_STATFS);
1438	return (error);
1439}
1440#endif
1441
1442int
1443freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1444{
1445	struct pread_args ap;
1446
1447	ap.fd = uap->fd;
1448	ap.buf = uap->buf;
1449	ap.nbyte = uap->nbyte;
1450	ap.offset = PAIR32TO64(off_t,uap->offset);
1451	return (sys_pread(td, &ap));
1452}
1453
1454int
1455freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1456{
1457	struct pwrite_args ap;
1458
1459	ap.fd = uap->fd;
1460	ap.buf = uap->buf;
1461	ap.nbyte = uap->nbyte;
1462	ap.offset = PAIR32TO64(off_t,uap->offset);
1463	return (sys_pwrite(td, &ap));
1464}
1465
1466#ifdef COMPAT_43
1467int
1468ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1469{
1470	struct lseek_args nuap;
1471
1472	nuap.fd = uap->fd;
1473	nuap.offset = uap->offset;
1474	nuap.whence = uap->whence;
1475	return (sys_lseek(td, &nuap));
1476}
1477#endif
1478
1479int
1480freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1481{
1482	int error;
1483	struct lseek_args ap;
1484	off_t pos;
1485
1486	ap.fd = uap->fd;
1487	ap.offset = PAIR32TO64(off_t,uap->offset);
1488	ap.whence = uap->whence;
1489	error = sys_lseek(td, &ap);
1490	/* Expand the quad return into two parts for eax and edx */
1491	pos = td->td_uretoff.tdu_off;
1492	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1493	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1494	return error;
1495}
1496
1497int
1498freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1499{
1500	struct truncate_args ap;
1501
1502	ap.path = uap->path;
1503	ap.length = PAIR32TO64(off_t,uap->length);
1504	return (sys_truncate(td, &ap));
1505}
1506
1507int
1508freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1509{
1510
1511	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1512}
1513
1514#ifdef COMPAT_43
1515int
1516ofreebsd32_getdirentries(struct thread *td,
1517    struct ofreebsd32_getdirentries_args *uap)
1518{
1519	struct ogetdirentries_args ap;
1520	int error;
1521	long loff;
1522	int32_t loff_cut;
1523
1524	ap.fd = uap->fd;
1525	ap.buf = uap->buf;
1526	ap.count = uap->count;
1527	ap.basep = NULL;
1528	error = kern_ogetdirentries(td, &ap, &loff);
1529	if (error == 0) {
1530		loff_cut = loff;
1531		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1532	}
1533	return (error);
1534}
1535#endif
1536
1537int
1538freebsd32_getdirentries(struct thread *td,
1539    struct freebsd32_getdirentries_args *uap)
1540{
1541	long base;
1542	int32_t base32;
1543	int error;
1544
1545	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1546	    NULL, UIO_USERSPACE);
1547	if (error)
1548		return (error);
1549	if (uap->basep != NULL) {
1550		base32 = base;
1551		error = copyout(&base32, uap->basep, sizeof(int32_t));
1552	}
1553	return (error);
1554}
1555
1556#ifdef COMPAT_FREEBSD6
1557/* versions with the 'int pad' argument */
1558int
1559freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1560{
1561	struct pread_args ap;
1562
1563	ap.fd = uap->fd;
1564	ap.buf = uap->buf;
1565	ap.nbyte = uap->nbyte;
1566	ap.offset = PAIR32TO64(off_t,uap->offset);
1567	return (sys_pread(td, &ap));
1568}
1569
1570int
1571freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1572{
1573	struct pwrite_args ap;
1574
1575	ap.fd = uap->fd;
1576	ap.buf = uap->buf;
1577	ap.nbyte = uap->nbyte;
1578	ap.offset = PAIR32TO64(off_t,uap->offset);
1579	return (sys_pwrite(td, &ap));
1580}
1581
1582int
1583freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1584{
1585	int error;
1586	struct lseek_args ap;
1587	off_t pos;
1588
1589	ap.fd = uap->fd;
1590	ap.offset = PAIR32TO64(off_t,uap->offset);
1591	ap.whence = uap->whence;
1592	error = sys_lseek(td, &ap);
1593	/* Expand the quad return into two parts for eax and edx */
1594	pos = *(off_t *)(td->td_retval);
1595	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1596	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1597	return error;
1598}
1599
1600int
1601freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1602{
1603	struct truncate_args ap;
1604
1605	ap.path = uap->path;
1606	ap.length = PAIR32TO64(off_t,uap->length);
1607	return (sys_truncate(td, &ap));
1608}
1609
1610int
1611freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1612{
1613
1614	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1615}
1616#endif /* COMPAT_FREEBSD6 */
1617
1618struct sf_hdtr32 {
1619	uint32_t headers;
1620	int hdr_cnt;
1621	uint32_t trailers;
1622	int trl_cnt;
1623};
1624
1625static int
1626freebsd32_do_sendfile(struct thread *td,
1627    struct freebsd32_sendfile_args *uap, int compat)
1628{
1629	struct sf_hdtr32 hdtr32;
1630	struct sf_hdtr hdtr;
1631	struct uio *hdr_uio, *trl_uio;
1632	struct file *fp;
1633	cap_rights_t rights;
1634	struct iovec32 *iov32;
1635	off_t offset, sbytes;
1636	int error;
1637
1638	offset = PAIR32TO64(off_t, uap->offset);
1639	if (offset < 0)
1640		return (EINVAL);
1641
1642	hdr_uio = trl_uio = NULL;
1643
1644	if (uap->hdtr != NULL) {
1645		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1646		if (error)
1647			goto out;
1648		PTRIN_CP(hdtr32, hdtr, headers);
1649		CP(hdtr32, hdtr, hdr_cnt);
1650		PTRIN_CP(hdtr32, hdtr, trailers);
1651		CP(hdtr32, hdtr, trl_cnt);
1652
1653		if (hdtr.headers != NULL) {
1654			iov32 = PTRIN(hdtr32.headers);
1655			error = freebsd32_copyinuio(iov32,
1656			    hdtr32.hdr_cnt, &hdr_uio);
1657			if (error)
1658				goto out;
1659#ifdef COMPAT_FREEBSD4
1660			/*
1661			 * In FreeBSD < 5.0 the nbytes to send also included
1662			 * the header.  If compat is specified subtract the
1663			 * header size from nbytes.
1664			 */
1665			if (compat) {
1666				if (uap->nbytes > hdr_uio->uio_resid)
1667					uap->nbytes -= hdr_uio->uio_resid;
1668				else
1669					uap->nbytes = 0;
1670			}
1671#endif
1672		}
1673		if (hdtr.trailers != NULL) {
1674			iov32 = PTRIN(hdtr32.trailers);
1675			error = freebsd32_copyinuio(iov32,
1676			    hdtr32.trl_cnt, &trl_uio);
1677			if (error)
1678				goto out;
1679		}
1680	}
1681
1682	AUDIT_ARG_FD(uap->fd);
1683
1684	if ((error = fget_read(td, uap->fd,
1685	    cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1686		goto out;
1687
1688	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1689	    uap->nbytes, &sbytes, uap->flags, td);
1690	fdrop(fp, td);
1691
1692	if (uap->sbytes != NULL)
1693		copyout(&sbytes, uap->sbytes, sizeof(off_t));
1694
1695out:
1696	if (hdr_uio)
1697		free(hdr_uio, M_IOV);
1698	if (trl_uio)
1699		free(trl_uio, M_IOV);
1700	return (error);
1701}
1702
1703#ifdef COMPAT_FREEBSD4
1704int
1705freebsd4_freebsd32_sendfile(struct thread *td,
1706    struct freebsd4_freebsd32_sendfile_args *uap)
1707{
1708	return (freebsd32_do_sendfile(td,
1709	    (struct freebsd32_sendfile_args *)uap, 1));
1710}
1711#endif
1712
1713int
1714freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1715{
1716
1717	return (freebsd32_do_sendfile(td, uap, 0));
1718}
1719
1720static void
1721copy_stat(struct stat *in, struct stat32 *out)
1722{
1723
1724	CP(*in, *out, st_dev);
1725	CP(*in, *out, st_ino);
1726	CP(*in, *out, st_mode);
1727	CP(*in, *out, st_nlink);
1728	CP(*in, *out, st_uid);
1729	CP(*in, *out, st_gid);
1730	CP(*in, *out, st_rdev);
1731	TS_CP(*in, *out, st_atim);
1732	TS_CP(*in, *out, st_mtim);
1733	TS_CP(*in, *out, st_ctim);
1734	CP(*in, *out, st_size);
1735	CP(*in, *out, st_blocks);
1736	CP(*in, *out, st_blksize);
1737	CP(*in, *out, st_flags);
1738	CP(*in, *out, st_gen);
1739	TS_CP(*in, *out, st_birthtim);
1740}
1741
1742#ifdef COMPAT_43
1743static void
1744copy_ostat(struct stat *in, struct ostat32 *out)
1745{
1746
1747	CP(*in, *out, st_dev);
1748	CP(*in, *out, st_ino);
1749	CP(*in, *out, st_mode);
1750	CP(*in, *out, st_nlink);
1751	CP(*in, *out, st_uid);
1752	CP(*in, *out, st_gid);
1753	CP(*in, *out, st_rdev);
1754	CP(*in, *out, st_size);
1755	TS_CP(*in, *out, st_atim);
1756	TS_CP(*in, *out, st_mtim);
1757	TS_CP(*in, *out, st_ctim);
1758	CP(*in, *out, st_blksize);
1759	CP(*in, *out, st_blocks);
1760	CP(*in, *out, st_flags);
1761	CP(*in, *out, st_gen);
1762}
1763#endif
1764
1765int
1766freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1767{
1768	struct stat sb;
1769	struct stat32 sb32;
1770	int error;
1771
1772	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1773	    &sb, NULL);
1774	if (error)
1775		return (error);
1776	copy_stat(&sb, &sb32);
1777	error = copyout(&sb32, uap->ub, sizeof (sb32));
1778	return (error);
1779}
1780
1781#ifdef COMPAT_43
1782int
1783ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1784{
1785	struct stat sb;
1786	struct ostat32 sb32;
1787	int error;
1788
1789	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1790	    &sb, NULL);
1791	if (error)
1792		return (error);
1793	copy_ostat(&sb, &sb32);
1794	error = copyout(&sb32, uap->ub, sizeof (sb32));
1795	return (error);
1796}
1797#endif
1798
1799int
1800freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1801{
1802	struct stat ub;
1803	struct stat32 ub32;
1804	int error;
1805
1806	error = kern_fstat(td, uap->fd, &ub);
1807	if (error)
1808		return (error);
1809	copy_stat(&ub, &ub32);
1810	error = copyout(&ub32, uap->ub, sizeof(ub32));
1811	return (error);
1812}
1813
1814#ifdef COMPAT_43
1815int
1816ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1817{
1818	struct stat ub;
1819	struct ostat32 ub32;
1820	int error;
1821
1822	error = kern_fstat(td, uap->fd, &ub);
1823	if (error)
1824		return (error);
1825	copy_ostat(&ub, &ub32);
1826	error = copyout(&ub32, uap->ub, sizeof(ub32));
1827	return (error);
1828}
1829#endif
1830
1831int
1832freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1833{
1834	struct stat ub;
1835	struct stat32 ub32;
1836	int error;
1837
1838	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1839	    &ub, NULL);
1840	if (error)
1841		return (error);
1842	copy_stat(&ub, &ub32);
1843	error = copyout(&ub32, uap->buf, sizeof(ub32));
1844	return (error);
1845}
1846
1847int
1848freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1849{
1850	struct stat sb;
1851	struct stat32 sb32;
1852	int error;
1853
1854	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1855	    UIO_USERSPACE, &sb, NULL);
1856	if (error)
1857		return (error);
1858	copy_stat(&sb, &sb32);
1859	error = copyout(&sb32, uap->ub, sizeof (sb32));
1860	return (error);
1861}
1862
1863#ifdef COMPAT_43
1864int
1865ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1866{
1867	struct stat sb;
1868	struct ostat32 sb32;
1869	int error;
1870
1871	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1872	    UIO_USERSPACE, &sb, NULL);
1873	if (error)
1874		return (error);
1875	copy_ostat(&sb, &sb32);
1876	error = copyout(&sb32, uap->ub, sizeof (sb32));
1877	return (error);
1878}
1879#endif
1880
1881int
1882freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1883{
1884	int error, name[CTL_MAXNAME];
1885	size_t j, oldlen;
1886	uint32_t tmp;
1887
1888	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1889		return (EINVAL);
1890 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1891 	if (error)
1892		return (error);
1893	if (uap->oldlenp) {
1894		error = fueword32(uap->oldlenp, &tmp);
1895		oldlen = tmp;
1896	} else {
1897		oldlen = 0;
1898	}
1899	if (error != 0)
1900		return (EFAULT);
1901	error = userland_sysctl(td, name, uap->namelen,
1902		uap->old, &oldlen, 1,
1903		uap->new, uap->newlen, &j, SCTL_MASK32);
1904	if (error && error != ENOMEM)
1905		return (error);
1906	if (uap->oldlenp)
1907		suword32(uap->oldlenp, j);
1908	return (0);
1909}
1910
1911int
1912freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1913{
1914	uint32_t version;
1915	int error;
1916	struct jail j;
1917
1918	error = copyin(uap->jail, &version, sizeof(uint32_t));
1919	if (error)
1920		return (error);
1921
1922	switch (version) {
1923	case 0:
1924	{
1925		/* FreeBSD single IPv4 jails. */
1926		struct jail32_v0 j32_v0;
1927
1928		bzero(&j, sizeof(struct jail));
1929		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1930		if (error)
1931			return (error);
1932		CP(j32_v0, j, version);
1933		PTRIN_CP(j32_v0, j, path);
1934		PTRIN_CP(j32_v0, j, hostname);
1935		j.ip4s = htonl(j32_v0.ip_number);	/* jail_v0 is host order */
1936		break;
1937	}
1938
1939	case 1:
1940		/*
1941		 * Version 1 was used by multi-IPv4 jail implementations
1942		 * that never made it into the official kernel.
1943		 */
1944		return (EINVAL);
1945
1946	case 2:	/* JAIL_API_VERSION */
1947	{
1948		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
1949		struct jail32 j32;
1950
1951		error = copyin(uap->jail, &j32, sizeof(struct jail32));
1952		if (error)
1953			return (error);
1954		CP(j32, j, version);
1955		PTRIN_CP(j32, j, path);
1956		PTRIN_CP(j32, j, hostname);
1957		PTRIN_CP(j32, j, jailname);
1958		CP(j32, j, ip4s);
1959		CP(j32, j, ip6s);
1960		PTRIN_CP(j32, j, ip4);
1961		PTRIN_CP(j32, j, ip6);
1962		break;
1963	}
1964
1965	default:
1966		/* Sci-Fi jails are not supported, sorry. */
1967		return (EINVAL);
1968	}
1969	return (kern_jail(td, &j));
1970}
1971
1972int
1973freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1974{
1975	struct uio *auio;
1976	int error;
1977
1978	/* Check that we have an even number of iovecs. */
1979	if (uap->iovcnt & 1)
1980		return (EINVAL);
1981
1982	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1983	if (error)
1984		return (error);
1985	error = kern_jail_set(td, auio, uap->flags);
1986	free(auio, M_IOV);
1987	return (error);
1988}
1989
1990int
1991freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1992{
1993	struct iovec32 iov32;
1994	struct uio *auio;
1995	int error, i;
1996
1997	/* Check that we have an even number of iovecs. */
1998	if (uap->iovcnt & 1)
1999		return (EINVAL);
2000
2001	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2002	if (error)
2003		return (error);
2004	error = kern_jail_get(td, auio, uap->flags);
2005	if (error == 0)
2006		for (i = 0; i < uap->iovcnt; i++) {
2007			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2008			CP(auio->uio_iov[i], iov32, iov_len);
2009			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2010			if (error != 0)
2011				break;
2012		}
2013	free(auio, M_IOV);
2014	return (error);
2015}
2016
2017int
2018freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2019{
2020	struct sigaction32 s32;
2021	struct sigaction sa, osa, *sap;
2022	int error;
2023
2024	if (uap->act) {
2025		error = copyin(uap->act, &s32, sizeof(s32));
2026		if (error)
2027			return (error);
2028		sa.sa_handler = PTRIN(s32.sa_u);
2029		CP(s32, sa, sa_flags);
2030		CP(s32, sa, sa_mask);
2031		sap = &sa;
2032	} else
2033		sap = NULL;
2034	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2035	if (error == 0 && uap->oact != NULL) {
2036		s32.sa_u = PTROUT(osa.sa_handler);
2037		CP(osa, s32, sa_flags);
2038		CP(osa, s32, sa_mask);
2039		error = copyout(&s32, uap->oact, sizeof(s32));
2040	}
2041	return (error);
2042}
2043
2044#ifdef COMPAT_FREEBSD4
2045int
2046freebsd4_freebsd32_sigaction(struct thread *td,
2047			     struct freebsd4_freebsd32_sigaction_args *uap)
2048{
2049	struct sigaction32 s32;
2050	struct sigaction sa, osa, *sap;
2051	int error;
2052
2053	if (uap->act) {
2054		error = copyin(uap->act, &s32, sizeof(s32));
2055		if (error)
2056			return (error);
2057		sa.sa_handler = PTRIN(s32.sa_u);
2058		CP(s32, sa, sa_flags);
2059		CP(s32, sa, sa_mask);
2060		sap = &sa;
2061	} else
2062		sap = NULL;
2063	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2064	if (error == 0 && uap->oact != NULL) {
2065		s32.sa_u = PTROUT(osa.sa_handler);
2066		CP(osa, s32, sa_flags);
2067		CP(osa, s32, sa_mask);
2068		error = copyout(&s32, uap->oact, sizeof(s32));
2069	}
2070	return (error);
2071}
2072#endif
2073
2074#ifdef COMPAT_43
2075struct osigaction32 {
2076	u_int32_t	sa_u;
2077	osigset_t	sa_mask;
2078	int		sa_flags;
2079};
2080
2081#define	ONSIG	32
2082
2083int
2084ofreebsd32_sigaction(struct thread *td,
2085			     struct ofreebsd32_sigaction_args *uap)
2086{
2087	struct osigaction32 s32;
2088	struct sigaction sa, osa, *sap;
2089	int error;
2090
2091	if (uap->signum <= 0 || uap->signum >= ONSIG)
2092		return (EINVAL);
2093
2094	if (uap->nsa) {
2095		error = copyin(uap->nsa, &s32, sizeof(s32));
2096		if (error)
2097			return (error);
2098		sa.sa_handler = PTRIN(s32.sa_u);
2099		CP(s32, sa, sa_flags);
2100		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2101		sap = &sa;
2102	} else
2103		sap = NULL;
2104	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2105	if (error == 0 && uap->osa != NULL) {
2106		s32.sa_u = PTROUT(osa.sa_handler);
2107		CP(osa, s32, sa_flags);
2108		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2109		error = copyout(&s32, uap->osa, sizeof(s32));
2110	}
2111	return (error);
2112}
2113
2114int
2115ofreebsd32_sigprocmask(struct thread *td,
2116			       struct ofreebsd32_sigprocmask_args *uap)
2117{
2118	sigset_t set, oset;
2119	int error;
2120
2121	OSIG2SIG(uap->mask, set);
2122	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2123	SIG2OSIG(oset, td->td_retval[0]);
2124	return (error);
2125}
2126
2127int
2128ofreebsd32_sigpending(struct thread *td,
2129			      struct ofreebsd32_sigpending_args *uap)
2130{
2131	struct proc *p = td->td_proc;
2132	sigset_t siglist;
2133
2134	PROC_LOCK(p);
2135	siglist = p->p_siglist;
2136	SIGSETOR(siglist, td->td_siglist);
2137	PROC_UNLOCK(p);
2138	SIG2OSIG(siglist, td->td_retval[0]);
2139	return (0);
2140}
2141
2142struct sigvec32 {
2143	u_int32_t	sv_handler;
2144	int		sv_mask;
2145	int		sv_flags;
2146};
2147
2148int
2149ofreebsd32_sigvec(struct thread *td,
2150			  struct ofreebsd32_sigvec_args *uap)
2151{
2152	struct sigvec32 vec;
2153	struct sigaction sa, osa, *sap;
2154	int error;
2155
2156	if (uap->signum <= 0 || uap->signum >= ONSIG)
2157		return (EINVAL);
2158
2159	if (uap->nsv) {
2160		error = copyin(uap->nsv, &vec, sizeof(vec));
2161		if (error)
2162			return (error);
2163		sa.sa_handler = PTRIN(vec.sv_handler);
2164		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2165		sa.sa_flags = vec.sv_flags;
2166		sa.sa_flags ^= SA_RESTART;
2167		sap = &sa;
2168	} else
2169		sap = NULL;
2170	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2171	if (error == 0 && uap->osv != NULL) {
2172		vec.sv_handler = PTROUT(osa.sa_handler);
2173		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2174		vec.sv_flags = osa.sa_flags;
2175		vec.sv_flags &= ~SA_NOCLDWAIT;
2176		vec.sv_flags ^= SA_RESTART;
2177		error = copyout(&vec, uap->osv, sizeof(vec));
2178	}
2179	return (error);
2180}
2181
2182int
2183ofreebsd32_sigblock(struct thread *td,
2184			    struct ofreebsd32_sigblock_args *uap)
2185{
2186	sigset_t set, oset;
2187
2188	OSIG2SIG(uap->mask, set);
2189	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2190	SIG2OSIG(oset, td->td_retval[0]);
2191	return (0);
2192}
2193
2194int
2195ofreebsd32_sigsetmask(struct thread *td,
2196			      struct ofreebsd32_sigsetmask_args *uap)
2197{
2198	sigset_t set, oset;
2199
2200	OSIG2SIG(uap->mask, set);
2201	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2202	SIG2OSIG(oset, td->td_retval[0]);
2203	return (0);
2204}
2205
2206int
2207ofreebsd32_sigsuspend(struct thread *td,
2208			      struct ofreebsd32_sigsuspend_args *uap)
2209{
2210	sigset_t mask;
2211
2212	OSIG2SIG(uap->mask, mask);
2213	return (kern_sigsuspend(td, mask));
2214}
2215
2216struct sigstack32 {
2217	u_int32_t	ss_sp;
2218	int		ss_onstack;
2219};
2220
2221int
2222ofreebsd32_sigstack(struct thread *td,
2223			    struct ofreebsd32_sigstack_args *uap)
2224{
2225	struct sigstack32 s32;
2226	struct sigstack nss, oss;
2227	int error = 0, unss;
2228
2229	if (uap->nss != NULL) {
2230		error = copyin(uap->nss, &s32, sizeof(s32));
2231		if (error)
2232			return (error);
2233		nss.ss_sp = PTRIN(s32.ss_sp);
2234		CP(s32, nss, ss_onstack);
2235		unss = 1;
2236	} else {
2237		unss = 0;
2238	}
2239	oss.ss_sp = td->td_sigstk.ss_sp;
2240	oss.ss_onstack = sigonstack(cpu_getstack(td));
2241	if (unss) {
2242		td->td_sigstk.ss_sp = nss.ss_sp;
2243		td->td_sigstk.ss_size = 0;
2244		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2245		td->td_pflags |= TDP_ALTSTACK;
2246	}
2247	if (uap->oss != NULL) {
2248		s32.ss_sp = PTROUT(oss.ss_sp);
2249		CP(oss, s32, ss_onstack);
2250		error = copyout(&s32, uap->oss, sizeof(s32));
2251	}
2252	return (error);
2253}
2254#endif
2255
2256int
2257freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2258{
2259	struct timespec32 rmt32, rqt32;
2260	struct timespec rmt, rqt;
2261	int error;
2262
2263	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2264	if (error)
2265		return (error);
2266
2267	CP(rqt32, rqt, tv_sec);
2268	CP(rqt32, rqt, tv_nsec);
2269
2270	if (uap->rmtp &&
2271	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2272		return (EFAULT);
2273	error = kern_nanosleep(td, &rqt, &rmt);
2274	if (error && uap->rmtp) {
2275		int error2;
2276
2277		CP(rmt, rmt32, tv_sec);
2278		CP(rmt, rmt32, tv_nsec);
2279
2280		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2281		if (error2)
2282			error = error2;
2283	}
2284	return (error);
2285}
2286
2287int
2288freebsd32_clock_gettime(struct thread *td,
2289			struct freebsd32_clock_gettime_args *uap)
2290{
2291	struct timespec	ats;
2292	struct timespec32 ats32;
2293	int error;
2294
2295	error = kern_clock_gettime(td, uap->clock_id, &ats);
2296	if (error == 0) {
2297		CP(ats, ats32, tv_sec);
2298		CP(ats, ats32, tv_nsec);
2299		error = copyout(&ats32, uap->tp, sizeof(ats32));
2300	}
2301	return (error);
2302}
2303
2304int
2305freebsd32_clock_settime(struct thread *td,
2306			struct freebsd32_clock_settime_args *uap)
2307{
2308	struct timespec	ats;
2309	struct timespec32 ats32;
2310	int error;
2311
2312	error = copyin(uap->tp, &ats32, sizeof(ats32));
2313	if (error)
2314		return (error);
2315	CP(ats32, ats, tv_sec);
2316	CP(ats32, ats, tv_nsec);
2317
2318	return (kern_clock_settime(td, uap->clock_id, &ats));
2319}
2320
2321int
2322freebsd32_clock_getres(struct thread *td,
2323		       struct freebsd32_clock_getres_args *uap)
2324{
2325	struct timespec	ts;
2326	struct timespec32 ts32;
2327	int error;
2328
2329	if (uap->tp == NULL)
2330		return (0);
2331	error = kern_clock_getres(td, uap->clock_id, &ts);
2332	if (error == 0) {
2333		CP(ts, ts32, tv_sec);
2334		CP(ts, ts32, tv_nsec);
2335		error = copyout(&ts32, uap->tp, sizeof(ts32));
2336	}
2337	return (error);
2338}
2339
2340int freebsd32_ktimer_create(struct thread *td,
2341    struct freebsd32_ktimer_create_args *uap)
2342{
2343	struct sigevent32 ev32;
2344	struct sigevent ev, *evp;
2345	int error, id;
2346
2347	if (uap->evp == NULL) {
2348		evp = NULL;
2349	} else {
2350		evp = &ev;
2351		error = copyin(uap->evp, &ev32, sizeof(ev32));
2352		if (error != 0)
2353			return (error);
2354		error = convert_sigevent32(&ev32, &ev);
2355		if (error != 0)
2356			return (error);
2357	}
2358	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2359	if (error == 0) {
2360		error = copyout(&id, uap->timerid, sizeof(int));
2361		if (error != 0)
2362			kern_ktimer_delete(td, id);
2363	}
2364	return (error);
2365}
2366
2367int
2368freebsd32_ktimer_settime(struct thread *td,
2369    struct freebsd32_ktimer_settime_args *uap)
2370{
2371	struct itimerspec32 val32, oval32;
2372	struct itimerspec val, oval, *ovalp;
2373	int error;
2374
2375	error = copyin(uap->value, &val32, sizeof(val32));
2376	if (error != 0)
2377		return (error);
2378	ITS_CP(val32, val);
2379	ovalp = uap->ovalue != NULL ? &oval : NULL;
2380	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2381	if (error == 0 && uap->ovalue != NULL) {
2382		ITS_CP(oval, oval32);
2383		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2384	}
2385	return (error);
2386}
2387
2388int
2389freebsd32_ktimer_gettime(struct thread *td,
2390    struct freebsd32_ktimer_gettime_args *uap)
2391{
2392	struct itimerspec32 val32;
2393	struct itimerspec val;
2394	int error;
2395
2396	error = kern_ktimer_gettime(td, uap->timerid, &val);
2397	if (error == 0) {
2398		ITS_CP(val, val32);
2399		error = copyout(&val32, uap->value, sizeof(val32));
2400	}
2401	return (error);
2402}
2403
2404int
2405freebsd32_clock_getcpuclockid2(struct thread *td,
2406    struct freebsd32_clock_getcpuclockid2_args *uap)
2407{
2408	clockid_t clk_id;
2409	int error;
2410
2411	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2412	    uap->which, &clk_id);
2413	if (error == 0)
2414		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2415	return (error);
2416}
2417
2418int
2419freebsd32_thr_new(struct thread *td,
2420		  struct freebsd32_thr_new_args *uap)
2421{
2422	struct thr_param32 param32;
2423	struct thr_param param;
2424	int error;
2425
2426	if (uap->param_size < 0 ||
2427	    uap->param_size > sizeof(struct thr_param32))
2428		return (EINVAL);
2429	bzero(&param, sizeof(struct thr_param));
2430	bzero(&param32, sizeof(struct thr_param32));
2431	error = copyin(uap->param, &param32, uap->param_size);
2432	if (error != 0)
2433		return (error);
2434	param.start_func = PTRIN(param32.start_func);
2435	param.arg = PTRIN(param32.arg);
2436	param.stack_base = PTRIN(param32.stack_base);
2437	param.stack_size = param32.stack_size;
2438	param.tls_base = PTRIN(param32.tls_base);
2439	param.tls_size = param32.tls_size;
2440	param.child_tid = PTRIN(param32.child_tid);
2441	param.parent_tid = PTRIN(param32.parent_tid);
2442	param.flags = param32.flags;
2443	param.rtp = PTRIN(param32.rtp);
2444	param.spare[0] = PTRIN(param32.spare[0]);
2445	param.spare[1] = PTRIN(param32.spare[1]);
2446	param.spare[2] = PTRIN(param32.spare[2]);
2447
2448	return (kern_thr_new(td, &param));
2449}
2450
2451int
2452freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2453{
2454	struct timespec32 ts32;
2455	struct timespec ts, *tsp;
2456	int error;
2457
2458	error = 0;
2459	tsp = NULL;
2460	if (uap->timeout != NULL) {
2461		error = copyin((const void *)uap->timeout, (void *)&ts32,
2462		    sizeof(struct timespec32));
2463		if (error != 0)
2464			return (error);
2465		ts.tv_sec = ts32.tv_sec;
2466		ts.tv_nsec = ts32.tv_nsec;
2467		tsp = &ts;
2468	}
2469	return (kern_thr_suspend(td, tsp));
2470}
2471
2472void
2473siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2474{
2475	bzero(dst, sizeof(*dst));
2476	dst->si_signo = src->si_signo;
2477	dst->si_errno = src->si_errno;
2478	dst->si_code = src->si_code;
2479	dst->si_pid = src->si_pid;
2480	dst->si_uid = src->si_uid;
2481	dst->si_status = src->si_status;
2482	dst->si_addr = (uintptr_t)src->si_addr;
2483	dst->si_value.sival_int = src->si_value.sival_int;
2484	dst->si_timerid = src->si_timerid;
2485	dst->si_overrun = src->si_overrun;
2486}
2487
2488int
2489freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2490{
2491	struct timespec32 ts32;
2492	struct timespec ts;
2493	struct timespec *timeout;
2494	sigset_t set;
2495	ksiginfo_t ksi;
2496	struct siginfo32 si32;
2497	int error;
2498
2499	if (uap->timeout) {
2500		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2501		if (error)
2502			return (error);
2503		ts.tv_sec = ts32.tv_sec;
2504		ts.tv_nsec = ts32.tv_nsec;
2505		timeout = &ts;
2506	} else
2507		timeout = NULL;
2508
2509	error = copyin(uap->set, &set, sizeof(set));
2510	if (error)
2511		return (error);
2512
2513	error = kern_sigtimedwait(td, set, &ksi, timeout);
2514	if (error)
2515		return (error);
2516
2517	if (uap->info) {
2518		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2519		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2520	}
2521
2522	if (error == 0)
2523		td->td_retval[0] = ksi.ksi_signo;
2524	return (error);
2525}
2526
2527/*
2528 * MPSAFE
2529 */
2530int
2531freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2532{
2533	ksiginfo_t ksi;
2534	struct siginfo32 si32;
2535	sigset_t set;
2536	int error;
2537
2538	error = copyin(uap->set, &set, sizeof(set));
2539	if (error)
2540		return (error);
2541
2542	error = kern_sigtimedwait(td, set, &ksi, NULL);
2543	if (error)
2544		return (error);
2545
2546	if (uap->info) {
2547		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2548		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2549	}
2550	if (error == 0)
2551		td->td_retval[0] = ksi.ksi_signo;
2552	return (error);
2553}
2554
2555int
2556freebsd32_cpuset_setid(struct thread *td,
2557    struct freebsd32_cpuset_setid_args *uap)
2558{
2559	struct cpuset_setid_args ap;
2560
2561	ap.which = uap->which;
2562	ap.id = PAIR32TO64(id_t,uap->id);
2563	ap.setid = uap->setid;
2564
2565	return (sys_cpuset_setid(td, &ap));
2566}
2567
2568int
2569freebsd32_cpuset_getid(struct thread *td,
2570    struct freebsd32_cpuset_getid_args *uap)
2571{
2572	struct cpuset_getid_args ap;
2573
2574	ap.level = uap->level;
2575	ap.which = uap->which;
2576	ap.id = PAIR32TO64(id_t,uap->id);
2577	ap.setid = uap->setid;
2578
2579	return (sys_cpuset_getid(td, &ap));
2580}
2581
2582int
2583freebsd32_cpuset_getaffinity(struct thread *td,
2584    struct freebsd32_cpuset_getaffinity_args *uap)
2585{
2586	struct cpuset_getaffinity_args ap;
2587
2588	ap.level = uap->level;
2589	ap.which = uap->which;
2590	ap.id = PAIR32TO64(id_t,uap->id);
2591	ap.cpusetsize = uap->cpusetsize;
2592	ap.mask = uap->mask;
2593
2594	return (sys_cpuset_getaffinity(td, &ap));
2595}
2596
2597int
2598freebsd32_cpuset_setaffinity(struct thread *td,
2599    struct freebsd32_cpuset_setaffinity_args *uap)
2600{
2601	struct cpuset_setaffinity_args ap;
2602
2603	ap.level = uap->level;
2604	ap.which = uap->which;
2605	ap.id = PAIR32TO64(id_t,uap->id);
2606	ap.cpusetsize = uap->cpusetsize;
2607	ap.mask = uap->mask;
2608
2609	return (sys_cpuset_setaffinity(td, &ap));
2610}
2611
2612int
2613freebsd32_nmount(struct thread *td,
2614    struct freebsd32_nmount_args /* {
2615    	struct iovec *iovp;
2616    	unsigned int iovcnt;
2617    	int flags;
2618    } */ *uap)
2619{
2620	struct uio *auio;
2621	uint64_t flags;
2622	int error;
2623
2624	/*
2625	 * Mount flags are now 64-bits. On 32-bit archtectures only
2626	 * 32-bits are passed in, but from here on everything handles
2627	 * 64-bit flags correctly.
2628	 */
2629	flags = uap->flags;
2630
2631	AUDIT_ARG_FFLAGS(flags);
2632
2633	/*
2634	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2635	 * userspace to set this flag, but we must filter it out if we want
2636	 * MNT_UPDATE on the root file system to work.
2637	 * MNT_ROOTFS should only be set by the kernel when mounting its
2638	 * root file system.
2639	 */
2640	flags &= ~MNT_ROOTFS;
2641
2642	/*
2643	 * check that we have an even number of iovec's
2644	 * and that we have at least two options.
2645	 */
2646	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2647		return (EINVAL);
2648
2649	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2650	if (error)
2651		return (error);
2652	error = vfs_donmount(td, flags, auio);
2653
2654	free(auio, M_IOV);
2655	return error;
2656}
2657
2658#if 0
2659int
2660freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2661{
2662	struct yyy32 *p32, s32;
2663	struct yyy *p = NULL, s;
2664	struct xxx_arg ap;
2665	int error;
2666
2667	if (uap->zzz) {
2668		error = copyin(uap->zzz, &s32, sizeof(s32));
2669		if (error)
2670			return (error);
2671		/* translate in */
2672		p = &s;
2673	}
2674	error = kern_xxx(td, p);
2675	if (error)
2676		return (error);
2677	if (uap->zzz) {
2678		/* translate out */
2679		error = copyout(&s32, p32, sizeof(s32));
2680	}
2681	return (error);
2682}
2683#endif
2684
2685int
2686syscall32_register(int *offset, struct sysent *new_sysent,
2687    struct sysent *old_sysent, int flags)
2688{
2689
2690	if ((flags & ~SY_THR_STATIC) != 0)
2691		return (EINVAL);
2692
2693	if (*offset == NO_SYSCALL) {
2694		int i;
2695
2696		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2697			if (freebsd32_sysent[i].sy_call ==
2698			    (sy_call_t *)lkmnosys)
2699				break;
2700		if (i == SYS_MAXSYSCALL)
2701			return (ENFILE);
2702		*offset = i;
2703	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2704		return (EINVAL);
2705	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2706	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2707		return (EEXIST);
2708
2709	*old_sysent = freebsd32_sysent[*offset];
2710	freebsd32_sysent[*offset] = *new_sysent;
2711	atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2712	return (0);
2713}
2714
2715int
2716syscall32_deregister(int *offset, struct sysent *old_sysent)
2717{
2718
2719	if (*offset == 0)
2720		return (0);
2721
2722	freebsd32_sysent[*offset] = *old_sysent;
2723	return (0);
2724}
2725
2726int
2727syscall32_module_handler(struct module *mod, int what, void *arg)
2728{
2729	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2730	modspecific_t ms;
2731	int error;
2732
2733	switch (what) {
2734	case MOD_LOAD:
2735		error = syscall32_register(data->offset, data->new_sysent,
2736		    &data->old_sysent, SY_THR_STATIC_KLD);
2737		if (error) {
2738			/* Leave a mark so we know to safely unload below. */
2739			data->offset = NULL;
2740			return error;
2741		}
2742		ms.intval = *data->offset;
2743		MOD_XLOCK;
2744		module_setspecific(mod, &ms);
2745		MOD_XUNLOCK;
2746		if (data->chainevh)
2747			error = data->chainevh(mod, what, data->chainarg);
2748		return (error);
2749	case MOD_UNLOAD:
2750		/*
2751		 * MOD_LOAD failed, so just return without calling the
2752		 * chained handler since we didn't pass along the MOD_LOAD
2753		 * event.
2754		 */
2755		if (data->offset == NULL)
2756			return (0);
2757		if (data->chainevh) {
2758			error = data->chainevh(mod, what, data->chainarg);
2759			if (error)
2760				return (error);
2761		}
2762		error = syscall32_deregister(data->offset, &data->old_sysent);
2763		return (error);
2764	default:
2765		error = EOPNOTSUPP;
2766		if (data->chainevh)
2767			error = data->chainevh(mod, what, data->chainarg);
2768		return (error);
2769	}
2770}
2771
2772int
2773syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2774{
2775	struct syscall_helper_data *sd1;
2776	int error;
2777
2778	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2779		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2780		    &sd1->old_sysent, flags);
2781		if (error != 0) {
2782			syscall32_helper_unregister(sd);
2783			return (error);
2784		}
2785		sd1->registered = 1;
2786	}
2787	return (0);
2788}
2789
2790int
2791syscall32_helper_unregister(struct syscall_helper_data *sd)
2792{
2793	struct syscall_helper_data *sd1;
2794
2795	for (sd1 = sd; sd1->registered != 0; sd1++) {
2796		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2797		sd1->registered = 0;
2798	}
2799	return (0);
2800}
2801
2802register_t *
2803freebsd32_copyout_strings(struct image_params *imgp)
2804{
2805	int argc, envc, i;
2806	u_int32_t *vectp;
2807	char *stringp;
2808	uintptr_t destp;
2809	u_int32_t *stack_base;
2810	struct freebsd32_ps_strings *arginfo;
2811	char canary[sizeof(long) * 8];
2812	int32_t pagesizes32[MAXPAGESIZES];
2813	size_t execpath_len;
2814	int szsigcode;
2815
2816	/*
2817	 * Calculate string base and vector table pointers.
2818	 * Also deal with signal trampoline code for this exec type.
2819	 */
2820	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2821		execpath_len = strlen(imgp->execpath) + 1;
2822	else
2823		execpath_len = 0;
2824	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2825	    sv_psstrings;
2826	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2827		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2828	else
2829		szsigcode = 0;
2830	destp =	(uintptr_t)arginfo;
2831
2832	/*
2833	 * install sigcode
2834	 */
2835	if (szsigcode != 0) {
2836		destp -= szsigcode;
2837		destp = rounddown2(destp, sizeof(uint32_t));
2838		copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2839		    szsigcode);
2840	}
2841
2842	/*
2843	 * Copy the image path for the rtld.
2844	 */
2845	if (execpath_len != 0) {
2846		destp -= execpath_len;
2847		imgp->execpathp = destp;
2848		copyout(imgp->execpath, (void *)destp, execpath_len);
2849	}
2850
2851	/*
2852	 * Prepare the canary for SSP.
2853	 */
2854	arc4rand(canary, sizeof(canary), 0);
2855	destp -= sizeof(canary);
2856	imgp->canary = destp;
2857	copyout(canary, (void *)destp, sizeof(canary));
2858	imgp->canarylen = sizeof(canary);
2859
2860	/*
2861	 * Prepare the pagesizes array.
2862	 */
2863	for (i = 0; i < MAXPAGESIZES; i++)
2864		pagesizes32[i] = (uint32_t)pagesizes[i];
2865	destp -= sizeof(pagesizes32);
2866	destp = rounddown2(destp, sizeof(uint32_t));
2867	imgp->pagesizes = destp;
2868	copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2869	imgp->pagesizeslen = sizeof(pagesizes32);
2870
2871	destp -= ARG_MAX - imgp->args->stringspace;
2872	destp = rounddown2(destp, sizeof(uint32_t));
2873
2874	/*
2875	 * If we have a valid auxargs ptr, prepare some room
2876	 * on the stack.
2877	 */
2878	if (imgp->auxargs) {
2879		/*
2880		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2881		 * lower compatibility.
2882		 */
2883		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2884			: (AT_COUNT * 2);
2885		/*
2886		 * The '+ 2' is for the null pointers at the end of each of
2887		 * the arg and env vector sets,and imgp->auxarg_size is room
2888		 * for argument of Runtime loader.
2889		 */
2890		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2891		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2892		    sizeof(u_int32_t));
2893	} else {
2894		/*
2895		 * The '+ 2' is for the null pointers at the end of each of
2896		 * the arg and env vector sets
2897		 */
2898		vectp = (u_int32_t *)(destp - (imgp->args->argc +
2899		    imgp->args->envc + 2) * sizeof(u_int32_t));
2900	}
2901
2902	/*
2903	 * vectp also becomes our initial stack base
2904	 */
2905	stack_base = vectp;
2906
2907	stringp = imgp->args->begin_argv;
2908	argc = imgp->args->argc;
2909	envc = imgp->args->envc;
2910	/*
2911	 * Copy out strings - arguments and environment.
2912	 */
2913	copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2914
2915	/*
2916	 * Fill in "ps_strings" struct for ps, w, etc.
2917	 */
2918	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2919	suword32(&arginfo->ps_nargvstr, argc);
2920
2921	/*
2922	 * Fill in argument portion of vector table.
2923	 */
2924	for (; argc > 0; --argc) {
2925		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2926		while (*stringp++ != 0)
2927			destp++;
2928		destp++;
2929	}
2930
2931	/* a null vector table pointer separates the argp's from the envp's */
2932	suword32(vectp++, 0);
2933
2934	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2935	suword32(&arginfo->ps_nenvstr, envc);
2936
2937	/*
2938	 * Fill in environment portion of vector table.
2939	 */
2940	for (; envc > 0; --envc) {
2941		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2942		while (*stringp++ != 0)
2943			destp++;
2944		destp++;
2945	}
2946
2947	/* end of vector table is a null pointer */
2948	suword32(vectp, 0);
2949
2950	return ((register_t *)stack_base);
2951}
2952
2953int
2954freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2955{
2956	struct kld_file_stat stat;
2957	struct kld32_file_stat stat32;
2958	int error, version;
2959
2960	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2961	    != 0)
2962		return (error);
2963	if (version != sizeof(struct kld32_file_stat_1) &&
2964	    version != sizeof(struct kld32_file_stat))
2965		return (EINVAL);
2966
2967	error = kern_kldstat(td, uap->fileid, &stat);
2968	if (error != 0)
2969		return (error);
2970
2971	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2972	CP(stat, stat32, refs);
2973	CP(stat, stat32, id);
2974	PTROUT_CP(stat, stat32, address);
2975	CP(stat, stat32, size);
2976	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2977	return (copyout(&stat32, uap->stat, version));
2978}
2979
2980int
2981freebsd32_posix_fallocate(struct thread *td,
2982    struct freebsd32_posix_fallocate_args *uap)
2983{
2984	int error;
2985
2986	error = kern_posix_fallocate(td, uap->fd,
2987	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
2988	return (kern_posix_error(td, error));
2989}
2990
2991int
2992freebsd32_posix_fadvise(struct thread *td,
2993    struct freebsd32_posix_fadvise_args *uap)
2994{
2995	int error;
2996
2997	error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2998	    PAIR32TO64(off_t, uap->len), uap->advice);
2999	return (kern_posix_error(td, error));
3000}
3001
3002int
3003convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3004{
3005
3006	CP(*sig32, *sig, sigev_notify);
3007	switch (sig->sigev_notify) {
3008	case SIGEV_NONE:
3009		break;
3010	case SIGEV_THREAD_ID:
3011		CP(*sig32, *sig, sigev_notify_thread_id);
3012		/* FALLTHROUGH */
3013	case SIGEV_SIGNAL:
3014		CP(*sig32, *sig, sigev_signo);
3015		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3016		break;
3017	case SIGEV_KEVENT:
3018		CP(*sig32, *sig, sigev_notify_kqueue);
3019		CP(*sig32, *sig, sigev_notify_kevent_flags);
3020		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3021		break;
3022	default:
3023		return (EINVAL);
3024	}
3025	return (0);
3026}
3027
3028int
3029freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3030{
3031	void *data;
3032	union {
3033		struct procctl_reaper_status rs;
3034		struct procctl_reaper_pids rp;
3035		struct procctl_reaper_kill rk;
3036	} x;
3037	union {
3038		struct procctl_reaper_pids32 rp;
3039	} x32;
3040	int error, error1, flags;
3041
3042	switch (uap->com) {
3043	case PROC_SPROTECT:
3044	case PROC_TRACE_CTL:
3045	case PROC_TRAPCAP_CTL:
3046		error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3047		if (error != 0)
3048			return (error);
3049		data = &flags;
3050		break;
3051	case PROC_REAP_ACQUIRE:
3052	case PROC_REAP_RELEASE:
3053		if (uap->data != NULL)
3054			return (EINVAL);
3055		data = NULL;
3056		break;
3057	case PROC_REAP_STATUS:
3058		data = &x.rs;
3059		break;
3060	case PROC_REAP_GETPIDS:
3061		error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3062		if (error != 0)
3063			return (error);
3064		CP(x32.rp, x.rp, rp_count);
3065		PTRIN_CP(x32.rp, x.rp, rp_pids);
3066		data = &x.rp;
3067		break;
3068	case PROC_REAP_KILL:
3069		error = copyin(uap->data, &x.rk, sizeof(x.rk));
3070		if (error != 0)
3071			return (error);
3072		data = &x.rk;
3073		break;
3074	case PROC_TRACE_STATUS:
3075	case PROC_TRAPCAP_STATUS:
3076		data = &flags;
3077		break;
3078	default:
3079		return (EINVAL);
3080	}
3081	error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3082	    uap->com, data);
3083	switch (uap->com) {
3084	case PROC_REAP_STATUS:
3085		if (error == 0)
3086			error = copyout(&x.rs, uap->data, sizeof(x.rs));
3087		break;
3088	case PROC_REAP_KILL:
3089		error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3090		if (error == 0)
3091			error = error1;
3092		break;
3093	case PROC_TRACE_STATUS:
3094	case PROC_TRAPCAP_STATUS:
3095		if (error == 0)
3096			error = copyout(&flags, uap->data, sizeof(flags));
3097		break;
3098	}
3099	return (error);
3100}
3101
3102int
3103freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3104{
3105	long tmp;
3106
3107	switch (uap->cmd) {
3108	/*
3109	 * Do unsigned conversion for arg when operation
3110	 * interprets it as flags or pointer.
3111	 */
3112	case F_SETLK_REMOTE:
3113	case F_SETLKW:
3114	case F_SETLK:
3115	case F_GETLK:
3116	case F_SETFD:
3117	case F_SETFL:
3118	case F_OGETLK:
3119	case F_OSETLK:
3120	case F_OSETLKW:
3121		tmp = (unsigned int)(uap->arg);
3122		break;
3123	default:
3124		tmp = uap->arg;
3125		break;
3126	}
3127	return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3128}
3129
3130int
3131freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3132{
3133	struct timespec32 ts32;
3134	struct timespec ts, *tsp;
3135	sigset_t set, *ssp;
3136	int error;
3137
3138	if (uap->ts != NULL) {
3139		error = copyin(uap->ts, &ts32, sizeof(ts32));
3140		if (error != 0)
3141			return (error);
3142		CP(ts32, ts, tv_sec);
3143		CP(ts32, ts, tv_nsec);
3144		tsp = &ts;
3145	} else
3146		tsp = NULL;
3147	if (uap->set != NULL) {
3148		error = copyin(uap->set, &set, sizeof(set));
3149		if (error != 0)
3150			return (error);
3151		ssp = &set;
3152	} else
3153		ssp = NULL;
3154
3155	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3156}
3157