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