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