xref: /freebsd/sys/compat/freebsd32/freebsd32_misc.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_ktrace.h"
35 
36 #define __ELF_WORD_SIZE 32
37 
38 #ifdef COMPAT_FREEBSD11
39 #define	_WANT_FREEBSD11_KEVENT
40 #endif
41 
42 #include <sys/param.h>
43 #include <sys/bus.h>
44 #include <sys/capsicum.h>
45 #include <sys/clock.h>
46 #include <sys/exec.h>
47 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/imgact.h>
50 #include <sys/jail.h>
51 #include <sys/kernel.h>
52 #include <sys/limits.h>
53 #include <sys/linker.h>
54 #include <sys/lock.h>
55 #include <sys/malloc.h>
56 #include <sys/file.h>		/* Must come after sys/malloc.h */
57 #include <sys/imgact.h>
58 #include <sys/mbuf.h>
59 #include <sys/mman.h>
60 #include <sys/module.h>
61 #include <sys/mount.h>
62 #include <sys/mutex.h>
63 #include <sys/namei.h>
64 #include <sys/proc.h>
65 #include <sys/procctl.h>
66 #include <sys/ptrace.h>
67 #include <sys/reboot.h>
68 #include <sys/resource.h>
69 #include <sys/resourcevar.h>
70 #include <sys/selinfo.h>
71 #include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
72 #include <sys/pipe.h>		/* Must come after sys/selinfo.h */
73 #include <sys/signal.h>
74 #include <sys/signalvar.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/stat.h>
78 #include <sys/syscall.h>
79 #include <sys/syscallsubr.h>
80 #include <sys/sysctl.h>
81 #include <sys/sysent.h>
82 #include <sys/sysproto.h>
83 #include <sys/systm.h>
84 #include <sys/thr.h>
85 #include <sys/timex.h>
86 #include <sys/unistd.h>
87 #include <sys/ucontext.h>
88 #include <sys/umtx.h>
89 #include <sys/vnode.h>
90 #include <sys/wait.h>
91 #include <sys/ipc.h>
92 #include <sys/msg.h>
93 #include <sys/sem.h>
94 #include <sys/shm.h>
95 #ifdef KTRACE
96 #include <sys/ktrace.h>
97 #endif
98 
99 #ifdef INET
100 #include <netinet/in.h>
101 #endif
102 
103 #include <vm/vm.h>
104 #include <vm/vm_param.h>
105 #include <vm/pmap.h>
106 #include <vm/vm_map.h>
107 #include <vm/vm_object.h>
108 #include <vm/vm_extern.h>
109 
110 #include <machine/cpu.h>
111 #include <machine/elf.h>
112 #ifdef __amd64__
113 #include <machine/md_var.h>
114 #endif
115 
116 #include <security/audit/audit.h>
117 
118 #include <compat/freebsd32/freebsd32_util.h>
119 #include <compat/freebsd32/freebsd32.h>
120 #include <compat/freebsd32/freebsd32_ipc.h>
121 #include <compat/freebsd32/freebsd32_misc.h>
122 #include <compat/freebsd32/freebsd32_signal.h>
123 #include <compat/freebsd32/freebsd32_proto.h>
124 
125 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
126 
127 struct ptrace_io_desc32 {
128 	int		piod_op;
129 	uint32_t	piod_offs;
130 	uint32_t	piod_addr;
131 	uint32_t	piod_len;
132 };
133 
134 struct ptrace_sc_ret32 {
135 	uint32_t	sr_retval[2];
136 	int		sr_error;
137 };
138 
139 struct ptrace_vm_entry32 {
140 	int		pve_entry;
141 	int		pve_timestamp;
142 	uint32_t	pve_start;
143 	uint32_t	pve_end;
144 	uint32_t	pve_offset;
145 	u_int		pve_prot;
146 	u_int		pve_pathlen;
147 	int32_t		pve_fileid;
148 	u_int		pve_fsid;
149 	uint32_t	pve_path;
150 };
151 
152 #ifdef __amd64__
153 CTASSERT(sizeof(struct timeval32) == 8);
154 CTASSERT(sizeof(struct timespec32) == 8);
155 CTASSERT(sizeof(struct itimerval32) == 16);
156 CTASSERT(sizeof(struct bintime32) == 12);
157 #endif
158 CTASSERT(sizeof(struct statfs32) == 256);
159 #ifdef __amd64__
160 CTASSERT(sizeof(struct rusage32) == 72);
161 #endif
162 CTASSERT(sizeof(struct sigaltstack32) == 12);
163 #ifdef __amd64__
164 CTASSERT(sizeof(struct kevent32) == 56);
165 #else
166 CTASSERT(sizeof(struct kevent32) == 64);
167 #endif
168 CTASSERT(sizeof(struct iovec32) == 8);
169 CTASSERT(sizeof(struct msghdr32) == 28);
170 #ifdef __amd64__
171 CTASSERT(sizeof(struct stat32) == 208);
172 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
173 #endif
174 CTASSERT(sizeof(struct sigaction32) == 24);
175 
176 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
177 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
178 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
179     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
180 
181 void
182 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
183 {
184 
185 	TV_CP(*s, *s32, ru_utime);
186 	TV_CP(*s, *s32, ru_stime);
187 	CP(*s, *s32, ru_maxrss);
188 	CP(*s, *s32, ru_ixrss);
189 	CP(*s, *s32, ru_idrss);
190 	CP(*s, *s32, ru_isrss);
191 	CP(*s, *s32, ru_minflt);
192 	CP(*s, *s32, ru_majflt);
193 	CP(*s, *s32, ru_nswap);
194 	CP(*s, *s32, ru_inblock);
195 	CP(*s, *s32, ru_oublock);
196 	CP(*s, *s32, ru_msgsnd);
197 	CP(*s, *s32, ru_msgrcv);
198 	CP(*s, *s32, ru_nsignals);
199 	CP(*s, *s32, ru_nvcsw);
200 	CP(*s, *s32, ru_nivcsw);
201 }
202 
203 int
204 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
205 {
206 	int error, status;
207 	struct rusage32 ru32;
208 	struct rusage ru, *rup;
209 
210 	if (uap->rusage != NULL)
211 		rup = &ru;
212 	else
213 		rup = NULL;
214 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
215 	if (error)
216 		return (error);
217 	if (uap->status != NULL)
218 		error = copyout(&status, uap->status, sizeof(status));
219 	if (uap->rusage != NULL && error == 0) {
220 		freebsd32_rusage_out(&ru, &ru32);
221 		error = copyout(&ru32, uap->rusage, sizeof(ru32));
222 	}
223 	return (error);
224 }
225 
226 int
227 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
228 {
229 	struct wrusage32 wru32;
230 	struct __wrusage wru, *wrup;
231 	struct siginfo32 si32;
232 	struct __siginfo si, *sip;
233 	int error, status;
234 
235 	if (uap->wrusage != NULL)
236 		wrup = &wru;
237 	else
238 		wrup = NULL;
239 	if (uap->info != NULL) {
240 		sip = &si;
241 		bzero(sip, sizeof(*sip));
242 	} else
243 		sip = NULL;
244 	error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
245 	    &status, uap->options, wrup, sip);
246 	if (error != 0)
247 		return (error);
248 	if (uap->status != NULL)
249 		error = copyout(&status, uap->status, sizeof(status));
250 	if (uap->wrusage != NULL && error == 0) {
251 		freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
252 		freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
253 		error = copyout(&wru32, uap->wrusage, sizeof(wru32));
254 	}
255 	if (uap->info != NULL && error == 0) {
256 		siginfo_to_siginfo32 (&si, &si32);
257 		error = copyout(&si32, uap->info, sizeof(si32));
258 	}
259 	return (error);
260 }
261 
262 #ifdef COMPAT_FREEBSD4
263 static void
264 copy_statfs(struct statfs *in, struct statfs32 *out)
265 {
266 
267 	statfs_scale_blocks(in, INT32_MAX);
268 	bzero(out, sizeof(*out));
269 	CP(*in, *out, f_bsize);
270 	out->f_iosize = MIN(in->f_iosize, INT32_MAX);
271 	CP(*in, *out, f_blocks);
272 	CP(*in, *out, f_bfree);
273 	CP(*in, *out, f_bavail);
274 	out->f_files = MIN(in->f_files, INT32_MAX);
275 	out->f_ffree = MIN(in->f_ffree, INT32_MAX);
276 	CP(*in, *out, f_fsid);
277 	CP(*in, *out, f_owner);
278 	CP(*in, *out, f_type);
279 	CP(*in, *out, f_flags);
280 	out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
281 	out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
282 	strlcpy(out->f_fstypename,
283 	      in->f_fstypename, MFSNAMELEN);
284 	strlcpy(out->f_mntonname,
285 	      in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
286 	out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
287 	out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
288 	strlcpy(out->f_mntfromname,
289 	      in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
290 }
291 #endif
292 
293 #ifdef COMPAT_FREEBSD4
294 int
295 freebsd4_freebsd32_getfsstat(struct thread *td,
296     struct freebsd4_freebsd32_getfsstat_args *uap)
297 {
298 	struct statfs *buf, *sp;
299 	struct statfs32 stat32;
300 	size_t count, size, copycount;
301 	int error;
302 
303 	count = uap->bufsize / sizeof(struct statfs32);
304 	size = count * sizeof(struct statfs);
305 	error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
306 	if (size > 0) {
307 		sp = buf;
308 		copycount = count;
309 		while (copycount > 0 && error == 0) {
310 			copy_statfs(sp, &stat32);
311 			error = copyout(&stat32, uap->buf, sizeof(stat32));
312 			sp++;
313 			uap->buf++;
314 			copycount--;
315 		}
316 		free(buf, M_STATFS);
317 	}
318 	if (error == 0)
319 		td->td_retval[0] = count;
320 	return (error);
321 }
322 #endif
323 
324 #ifdef COMPAT_FREEBSD10
325 int
326 freebsd10_freebsd32_pipe(struct thread *td,
327     struct freebsd10_freebsd32_pipe_args *uap) {
328 	return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
329 }
330 #endif
331 
332 int
333 freebsd32_sigaltstack(struct thread *td,
334 		      struct freebsd32_sigaltstack_args *uap)
335 {
336 	struct sigaltstack32 s32;
337 	struct sigaltstack ss, oss, *ssp;
338 	int error;
339 
340 	if (uap->ss != NULL) {
341 		error = copyin(uap->ss, &s32, sizeof(s32));
342 		if (error)
343 			return (error);
344 		PTRIN_CP(s32, ss, ss_sp);
345 		CP(s32, ss, ss_size);
346 		CP(s32, ss, ss_flags);
347 		ssp = &ss;
348 	} else
349 		ssp = NULL;
350 	error = kern_sigaltstack(td, ssp, &oss);
351 	if (error == 0 && uap->oss != NULL) {
352 		PTROUT_CP(oss, s32, ss_sp);
353 		CP(oss, s32, ss_size);
354 		CP(oss, s32, ss_flags);
355 		error = copyout(&s32, uap->oss, sizeof(s32));
356 	}
357 	return (error);
358 }
359 
360 /*
361  * Custom version of exec_copyin_args() so that we can translate
362  * the pointers.
363  */
364 int
365 freebsd32_exec_copyin_args(struct image_args *args, const char *fname,
366     enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
367 {
368 	char *argp, *envp;
369 	u_int32_t *p32, arg;
370 	int error;
371 
372 	bzero(args, sizeof(*args));
373 	if (argv == NULL)
374 		return (EFAULT);
375 
376 	/*
377 	 * Allocate demand-paged memory for the file name, argument, and
378 	 * environment strings.
379 	 */
380 	error = exec_alloc_args(args);
381 	if (error != 0)
382 		return (error);
383 
384 	/*
385 	 * Copy the file name.
386 	 */
387 	error = exec_args_add_fname(args, fname, segflg);
388 	if (error != 0)
389 		goto err_exit;
390 
391 	/*
392 	 * extract arguments first
393 	 */
394 	p32 = argv;
395 	for (;;) {
396 		error = copyin(p32++, &arg, sizeof(arg));
397 		if (error)
398 			goto err_exit;
399 		if (arg == 0)
400 			break;
401 		argp = PTRIN(arg);
402 		error = exec_args_add_arg(args, argp, UIO_USERSPACE);
403 		if (error != 0)
404 			goto err_exit;
405 	}
406 
407 	/*
408 	 * extract environment strings
409 	 */
410 	if (envv) {
411 		p32 = envv;
412 		for (;;) {
413 			error = copyin(p32++, &arg, sizeof(arg));
414 			if (error)
415 				goto err_exit;
416 			if (arg == 0)
417 				break;
418 			envp = PTRIN(arg);
419 			error = exec_args_add_env(args, envp, UIO_USERSPACE);
420 			if (error != 0)
421 				goto err_exit;
422 		}
423 	}
424 
425 	return (0);
426 
427 err_exit:
428 	exec_free_args(args);
429 	return (error);
430 }
431 
432 int
433 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
434 {
435 	struct image_args eargs;
436 	struct vmspace *oldvmspace;
437 	int error;
438 
439 	error = pre_execve(td, &oldvmspace);
440 	if (error != 0)
441 		return (error);
442 	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
443 	    uap->argv, uap->envv);
444 	if (error == 0)
445 		error = kern_execve(td, &eargs, NULL, oldvmspace);
446 	post_execve(td, error, oldvmspace);
447 	AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
448 	return (error);
449 }
450 
451 int
452 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
453 {
454 	struct image_args eargs;
455 	struct vmspace *oldvmspace;
456 	int error;
457 
458 	error = pre_execve(td, &oldvmspace);
459 	if (error != 0)
460 		return (error);
461 	error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
462 	    uap->argv, uap->envv);
463 	if (error == 0) {
464 		eargs.fd = uap->fd;
465 		error = kern_execve(td, &eargs, NULL, oldvmspace);
466 	}
467 	post_execve(td, error, oldvmspace);
468 	AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
469 	return (error);
470 }
471 
472 int
473 freebsd32_mknodat(struct thread *td, struct freebsd32_mknodat_args *uap)
474 {
475 
476 	return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE,
477 	    uap->mode, PAIR32TO64(dev_t, uap->dev)));
478 }
479 
480 int
481 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
482 {
483 	int prot;
484 
485 	prot = uap->prot;
486 #if defined(__amd64__)
487 	if (i386_read_exec && (prot & PROT_READ) != 0)
488 		prot |= PROT_EXEC;
489 #endif
490 	return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
491 	    prot));
492 }
493 
494 int
495 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
496 {
497 	int prot;
498 
499 	prot = uap->prot;
500 #if defined(__amd64__)
501 	if (i386_read_exec && (prot & PROT_READ))
502 		prot |= PROT_EXEC;
503 #endif
504 
505 	return (kern_mmap(td, &(struct mmap_req){
506 		.mr_hint = (uintptr_t)uap->addr,
507 		.mr_len = uap->len,
508 		.mr_prot = prot,
509 		.mr_flags = uap->flags,
510 		.mr_fd = uap->fd,
511 		.mr_pos = PAIR32TO64(off_t, uap->pos),
512 	    }));
513 }
514 
515 #ifdef COMPAT_FREEBSD6
516 int
517 freebsd6_freebsd32_mmap(struct thread *td,
518     struct freebsd6_freebsd32_mmap_args *uap)
519 {
520 	int prot;
521 
522 	prot = uap->prot;
523 #if defined(__amd64__)
524 	if (i386_read_exec && (prot & PROT_READ))
525 		prot |= PROT_EXEC;
526 #endif
527 
528 	return (kern_mmap(td, &(struct mmap_req){
529 		.mr_hint = (uintptr_t)uap->addr,
530 		.mr_len = uap->len,
531 		.mr_prot = prot,
532 		.mr_flags = uap->flags,
533 		.mr_fd = uap->fd,
534 		.mr_pos = PAIR32TO64(off_t, uap->pos),
535 	    }));
536 }
537 #endif
538 
539 int
540 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
541 {
542 	struct itimerval itv, oitv, *itvp;
543 	struct itimerval32 i32;
544 	int error;
545 
546 	if (uap->itv != NULL) {
547 		error = copyin(uap->itv, &i32, sizeof(i32));
548 		if (error)
549 			return (error);
550 		TV_CP(i32, itv, it_interval);
551 		TV_CP(i32, itv, it_value);
552 		itvp = &itv;
553 	} else
554 		itvp = NULL;
555 	error = kern_setitimer(td, uap->which, itvp, &oitv);
556 	if (error || uap->oitv == NULL)
557 		return (error);
558 	TV_CP(oitv, i32, it_interval);
559 	TV_CP(oitv, i32, it_value);
560 	return (copyout(&i32, uap->oitv, sizeof(i32)));
561 }
562 
563 int
564 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
565 {
566 	struct itimerval itv;
567 	struct itimerval32 i32;
568 	int error;
569 
570 	error = kern_getitimer(td, uap->which, &itv);
571 	if (error || uap->itv == NULL)
572 		return (error);
573 	TV_CP(itv, i32, it_interval);
574 	TV_CP(itv, i32, it_value);
575 	return (copyout(&i32, uap->itv, sizeof(i32)));
576 }
577 
578 int
579 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
580 {
581 	struct timeval32 tv32;
582 	struct timeval tv, *tvp;
583 	int error;
584 
585 	if (uap->tv != NULL) {
586 		error = copyin(uap->tv, &tv32, sizeof(tv32));
587 		if (error)
588 			return (error);
589 		CP(tv32, tv, tv_sec);
590 		CP(tv32, tv, tv_usec);
591 		tvp = &tv;
592 	} else
593 		tvp = NULL;
594 	/*
595 	 * XXX Do pointers need PTRIN()?
596 	 */
597 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
598 	    sizeof(int32_t) * 8));
599 }
600 
601 int
602 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
603 {
604 	struct timespec32 ts32;
605 	struct timespec ts;
606 	struct timeval tv, *tvp;
607 	sigset_t set, *uset;
608 	int error;
609 
610 	if (uap->ts != NULL) {
611 		error = copyin(uap->ts, &ts32, sizeof(ts32));
612 		if (error != 0)
613 			return (error);
614 		CP(ts32, ts, tv_sec);
615 		CP(ts32, ts, tv_nsec);
616 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
617 		tvp = &tv;
618 	} else
619 		tvp = NULL;
620 	if (uap->sm != NULL) {
621 		error = copyin(uap->sm, &set, sizeof(set));
622 		if (error != 0)
623 			return (error);
624 		uset = &set;
625 	} else
626 		uset = NULL;
627 	/*
628 	 * XXX Do pointers need PTRIN()?
629 	 */
630 	error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
631 	    uset, sizeof(int32_t) * 8);
632 	return (error);
633 }
634 
635 /*
636  * Copy 'count' items into the destination list pointed to by uap->eventlist.
637  */
638 static int
639 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
640 {
641 	struct freebsd32_kevent_args *uap;
642 	struct kevent32	ks32[KQ_NEVENTS];
643 	uint64_t e;
644 	int i, j, error;
645 
646 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
647 	uap = (struct freebsd32_kevent_args *)arg;
648 
649 	for (i = 0; i < count; i++) {
650 		CP(kevp[i], ks32[i], ident);
651 		CP(kevp[i], ks32[i], filter);
652 		CP(kevp[i], ks32[i], flags);
653 		CP(kevp[i], ks32[i], fflags);
654 #if BYTE_ORDER == LITTLE_ENDIAN
655 		ks32[i].data1 = kevp[i].data;
656 		ks32[i].data2 = kevp[i].data >> 32;
657 #else
658 		ks32[i].data1 = kevp[i].data >> 32;
659 		ks32[i].data2 = kevp[i].data;
660 #endif
661 		PTROUT_CP(kevp[i], ks32[i], udata);
662 		for (j = 0; j < nitems(kevp->ext); j++) {
663 			e = kevp[i].ext[j];
664 #if BYTE_ORDER == LITTLE_ENDIAN
665 			ks32[i].ext64[2 * j] = e;
666 			ks32[i].ext64[2 * j + 1] = e >> 32;
667 #else
668 			ks32[i].ext64[2 * j] = e >> 32;
669 			ks32[i].ext64[2 * j + 1] = e;
670 #endif
671 		}
672 	}
673 	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
674 	if (error == 0)
675 		uap->eventlist += count;
676 	return (error);
677 }
678 
679 /*
680  * Copy 'count' items from the list pointed to by uap->changelist.
681  */
682 static int
683 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
684 {
685 	struct freebsd32_kevent_args *uap;
686 	struct kevent32	ks32[KQ_NEVENTS];
687 	uint64_t e;
688 	int i, j, error;
689 
690 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
691 	uap = (struct freebsd32_kevent_args *)arg;
692 
693 	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
694 	if (error)
695 		goto done;
696 	uap->changelist += count;
697 
698 	for (i = 0; i < count; i++) {
699 		CP(ks32[i], kevp[i], ident);
700 		CP(ks32[i], kevp[i], filter);
701 		CP(ks32[i], kevp[i], flags);
702 		CP(ks32[i], kevp[i], fflags);
703 		kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
704 		PTRIN_CP(ks32[i], kevp[i], udata);
705 		for (j = 0; j < nitems(kevp->ext); j++) {
706 #if BYTE_ORDER == LITTLE_ENDIAN
707 			e = ks32[i].ext64[2 * j + 1];
708 			e <<= 32;
709 			e += ks32[i].ext64[2 * j];
710 #else
711 			e = ks32[i].ext64[2 * j];
712 			e <<= 32;
713 			e += ks32[i].ext64[2 * j + 1];
714 #endif
715 			kevp[i].ext[j] = e;
716 		}
717 	}
718 done:
719 	return (error);
720 }
721 
722 int
723 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
724 {
725 	struct timespec32 ts32;
726 	struct timespec ts, *tsp;
727 	struct kevent_copyops k_ops = {
728 		.arg = uap,
729 		.k_copyout = freebsd32_kevent_copyout,
730 		.k_copyin = freebsd32_kevent_copyin,
731 	};
732 #ifdef KTRACE
733 	struct kevent32 *eventlist = uap->eventlist;
734 #endif
735 	int error;
736 
737 	if (uap->timeout) {
738 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
739 		if (error)
740 			return (error);
741 		CP(ts32, ts, tv_sec);
742 		CP(ts32, ts, tv_nsec);
743 		tsp = &ts;
744 	} else
745 		tsp = NULL;
746 #ifdef KTRACE
747 	if (KTRPOINT(td, KTR_STRUCT_ARRAY))
748 		ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
749 		    uap->nchanges, sizeof(struct kevent32));
750 #endif
751 	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
752 	    &k_ops, tsp);
753 #ifdef KTRACE
754 	if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
755 		ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
756 		    td->td_retval[0], sizeof(struct kevent32));
757 #endif
758 	return (error);
759 }
760 
761 #ifdef COMPAT_FREEBSD11
762 static int
763 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
764 {
765 	struct freebsd11_freebsd32_kevent_args *uap;
766 	struct kevent32_freebsd11 ks32[KQ_NEVENTS];
767 	int i, error;
768 
769 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
770 	uap = (struct freebsd11_freebsd32_kevent_args *)arg;
771 
772 	for (i = 0; i < count; i++) {
773 		CP(kevp[i], ks32[i], ident);
774 		CP(kevp[i], ks32[i], filter);
775 		CP(kevp[i], ks32[i], flags);
776 		CP(kevp[i], ks32[i], fflags);
777 		CP(kevp[i], ks32[i], data);
778 		PTROUT_CP(kevp[i], ks32[i], udata);
779 	}
780 	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
781 	if (error == 0)
782 		uap->eventlist += count;
783 	return (error);
784 }
785 
786 /*
787  * Copy 'count' items from the list pointed to by uap->changelist.
788  */
789 static int
790 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
791 {
792 	struct freebsd11_freebsd32_kevent_args *uap;
793 	struct kevent32_freebsd11 ks32[KQ_NEVENTS];
794 	int i, j, error;
795 
796 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
797 	uap = (struct freebsd11_freebsd32_kevent_args *)arg;
798 
799 	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
800 	if (error)
801 		goto done;
802 	uap->changelist += count;
803 
804 	for (i = 0; i < count; i++) {
805 		CP(ks32[i], kevp[i], ident);
806 		CP(ks32[i], kevp[i], filter);
807 		CP(ks32[i], kevp[i], flags);
808 		CP(ks32[i], kevp[i], fflags);
809 		CP(ks32[i], kevp[i], data);
810 		PTRIN_CP(ks32[i], kevp[i], udata);
811 		for (j = 0; j < nitems(kevp->ext); j++)
812 			kevp[i].ext[j] = 0;
813 	}
814 done:
815 	return (error);
816 }
817 
818 int
819 freebsd11_freebsd32_kevent(struct thread *td,
820     struct freebsd11_freebsd32_kevent_args *uap)
821 {
822 	struct timespec32 ts32;
823 	struct timespec ts, *tsp;
824 	struct kevent_copyops k_ops = {
825 		.arg = uap,
826 		.k_copyout = freebsd32_kevent11_copyout,
827 		.k_copyin = freebsd32_kevent11_copyin,
828 	};
829 #ifdef KTRACE
830 	struct kevent32_freebsd11 *eventlist = uap->eventlist;
831 #endif
832 	int error;
833 
834 	if (uap->timeout) {
835 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
836 		if (error)
837 			return (error);
838 		CP(ts32, ts, tv_sec);
839 		CP(ts32, ts, tv_nsec);
840 		tsp = &ts;
841 	} else
842 		tsp = NULL;
843 #ifdef KTRACE
844 	if (KTRPOINT(td, KTR_STRUCT_ARRAY))
845 		ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
846 		    uap->changelist, uap->nchanges,
847 		    sizeof(struct kevent32_freebsd11));
848 #endif
849 	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
850 	    &k_ops, tsp);
851 #ifdef KTRACE
852 	if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
853 		ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
854 		    eventlist, td->td_retval[0],
855 		    sizeof(struct kevent32_freebsd11));
856 #endif
857 	return (error);
858 }
859 #endif
860 
861 int
862 freebsd32_gettimeofday(struct thread *td,
863 		       struct freebsd32_gettimeofday_args *uap)
864 {
865 	struct timeval atv;
866 	struct timeval32 atv32;
867 	struct timezone rtz;
868 	int error = 0;
869 
870 	if (uap->tp) {
871 		microtime(&atv);
872 		CP(atv, atv32, tv_sec);
873 		CP(atv, atv32, tv_usec);
874 		error = copyout(&atv32, uap->tp, sizeof (atv32));
875 	}
876 	if (error == 0 && uap->tzp != NULL) {
877 		rtz.tz_minuteswest = 0;
878 		rtz.tz_dsttime = 0;
879 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
880 	}
881 	return (error);
882 }
883 
884 int
885 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
886 {
887 	struct rusage32 s32;
888 	struct rusage s;
889 	int error;
890 
891 	error = kern_getrusage(td, uap->who, &s);
892 	if (error == 0) {
893 		freebsd32_rusage_out(&s, &s32);
894 		error = copyout(&s32, uap->rusage, sizeof(s32));
895 	}
896 	return (error);
897 }
898 
899 static void
900 ptrace_lwpinfo_to32(const struct ptrace_lwpinfo *pl,
901     struct ptrace_lwpinfo32 *pl32)
902 {
903 
904 	bzero(pl32, sizeof(*pl32));
905 	pl32->pl_lwpid = pl->pl_lwpid;
906 	pl32->pl_event = pl->pl_event;
907 	pl32->pl_flags = pl->pl_flags;
908 	pl32->pl_sigmask = pl->pl_sigmask;
909 	pl32->pl_siglist = pl->pl_siglist;
910 	siginfo_to_siginfo32(&pl->pl_siginfo, &pl32->pl_siginfo);
911 	strcpy(pl32->pl_tdname, pl->pl_tdname);
912 	pl32->pl_child_pid = pl->pl_child_pid;
913 	pl32->pl_syscall_code = pl->pl_syscall_code;
914 	pl32->pl_syscall_narg = pl->pl_syscall_narg;
915 }
916 
917 static void
918 ptrace_sc_ret_to32(const struct ptrace_sc_ret *psr,
919     struct ptrace_sc_ret32 *psr32)
920 {
921 
922 	bzero(psr32, sizeof(*psr32));
923 	psr32->sr_retval[0] = psr->sr_retval[0];
924 	psr32->sr_retval[1] = psr->sr_retval[1];
925 	psr32->sr_error = psr->sr_error;
926 }
927 
928 int
929 freebsd32_ptrace(struct thread *td, struct freebsd32_ptrace_args *uap)
930 {
931 	union {
932 		struct ptrace_io_desc piod;
933 		struct ptrace_lwpinfo pl;
934 		struct ptrace_vm_entry pve;
935 		struct dbreg32 dbreg;
936 		struct fpreg32 fpreg;
937 		struct reg32 reg;
938 		register_t args[nitems(td->td_sa.args)];
939 		struct ptrace_sc_ret psr;
940 		int ptevents;
941 	} r;
942 	union {
943 		struct ptrace_io_desc32 piod;
944 		struct ptrace_lwpinfo32 pl;
945 		struct ptrace_vm_entry32 pve;
946 		uint32_t args[nitems(td->td_sa.args)];
947 		struct ptrace_sc_ret32 psr;
948 	} r32;
949 	void *addr;
950 	int data, error = 0, i;
951 
952 	AUDIT_ARG_PID(uap->pid);
953 	AUDIT_ARG_CMD(uap->req);
954 	AUDIT_ARG_VALUE(uap->data);
955 	addr = &r;
956 	data = uap->data;
957 	switch (uap->req) {
958 	case PT_GET_EVENT_MASK:
959 	case PT_GET_SC_ARGS:
960 	case PT_GET_SC_RET:
961 		break;
962 	case PT_LWPINFO:
963 		if (uap->data > sizeof(r32.pl))
964 			return (EINVAL);
965 
966 		/*
967 		 * Pass size of native structure in 'data'.  Truncate
968 		 * if necessary to avoid siginfo.
969 		 */
970 		data = sizeof(r.pl);
971 		if (uap->data < offsetof(struct ptrace_lwpinfo32, pl_siginfo) +
972 		    sizeof(struct siginfo32))
973 			data = offsetof(struct ptrace_lwpinfo, pl_siginfo);
974 		break;
975 	case PT_GETREGS:
976 		bzero(&r.reg, sizeof(r.reg));
977 		break;
978 	case PT_GETFPREGS:
979 		bzero(&r.fpreg, sizeof(r.fpreg));
980 		break;
981 	case PT_GETDBREGS:
982 		bzero(&r.dbreg, sizeof(r.dbreg));
983 		break;
984 	case PT_SETREGS:
985 		error = copyin(uap->addr, &r.reg, sizeof(r.reg));
986 		break;
987 	case PT_SETFPREGS:
988 		error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg));
989 		break;
990 	case PT_SETDBREGS:
991 		error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg));
992 		break;
993 	case PT_SET_EVENT_MASK:
994 		if (uap->data != sizeof(r.ptevents))
995 			error = EINVAL;
996 		else
997 			error = copyin(uap->addr, &r.ptevents, uap->data);
998 		break;
999 	case PT_IO:
1000 		error = copyin(uap->addr, &r32.piod, sizeof(r32.piod));
1001 		if (error)
1002 			break;
1003 		CP(r32.piod, r.piod, piod_op);
1004 		PTRIN_CP(r32.piod, r.piod, piod_offs);
1005 		PTRIN_CP(r32.piod, r.piod, piod_addr);
1006 		CP(r32.piod, r.piod, piod_len);
1007 		break;
1008 	case PT_VM_ENTRY:
1009 		error = copyin(uap->addr, &r32.pve, sizeof(r32.pve));
1010 		if (error)
1011 			break;
1012 
1013 		CP(r32.pve, r.pve, pve_entry);
1014 		CP(r32.pve, r.pve, pve_timestamp);
1015 		CP(r32.pve, r.pve, pve_start);
1016 		CP(r32.pve, r.pve, pve_end);
1017 		CP(r32.pve, r.pve, pve_offset);
1018 		CP(r32.pve, r.pve, pve_prot);
1019 		CP(r32.pve, r.pve, pve_pathlen);
1020 		CP(r32.pve, r.pve, pve_fileid);
1021 		CP(r32.pve, r.pve, pve_fsid);
1022 		PTRIN_CP(r32.pve, r.pve, pve_path);
1023 		break;
1024 	default:
1025 		addr = uap->addr;
1026 		break;
1027 	}
1028 	if (error)
1029 		return (error);
1030 
1031 	error = kern_ptrace(td, uap->req, uap->pid, addr, data);
1032 	if (error)
1033 		return (error);
1034 
1035 	switch (uap->req) {
1036 	case PT_VM_ENTRY:
1037 		CP(r.pve, r32.pve, pve_entry);
1038 		CP(r.pve, r32.pve, pve_timestamp);
1039 		CP(r.pve, r32.pve, pve_start);
1040 		CP(r.pve, r32.pve, pve_end);
1041 		CP(r.pve, r32.pve, pve_offset);
1042 		CP(r.pve, r32.pve, pve_prot);
1043 		CP(r.pve, r32.pve, pve_pathlen);
1044 		CP(r.pve, r32.pve, pve_fileid);
1045 		CP(r.pve, r32.pve, pve_fsid);
1046 		error = copyout(&r32.pve, uap->addr, sizeof(r32.pve));
1047 		break;
1048 	case PT_IO:
1049 		CP(r.piod, r32.piod, piod_len);
1050 		error = copyout(&r32.piod, uap->addr, sizeof(r32.piod));
1051 		break;
1052 	case PT_GETREGS:
1053 		error = copyout(&r.reg, uap->addr, sizeof(r.reg));
1054 		break;
1055 	case PT_GETFPREGS:
1056 		error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg));
1057 		break;
1058 	case PT_GETDBREGS:
1059 		error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg));
1060 		break;
1061 	case PT_GET_EVENT_MASK:
1062 		/* NB: The size in uap->data is validated in kern_ptrace(). */
1063 		error = copyout(&r.ptevents, uap->addr, uap->data);
1064 		break;
1065 	case PT_LWPINFO:
1066 		ptrace_lwpinfo_to32(&r.pl, &r32.pl);
1067 		error = copyout(&r32.pl, uap->addr, uap->data);
1068 		break;
1069 	case PT_GET_SC_ARGS:
1070 		for (i = 0; i < nitems(r.args); i++)
1071 			r32.args[i] = (uint32_t)r.args[i];
1072 		error = copyout(r32.args, uap->addr, MIN(uap->data,
1073 		    sizeof(r32.args)));
1074 		break;
1075 	case PT_GET_SC_RET:
1076 		ptrace_sc_ret_to32(&r.psr, &r32.psr);
1077 		error = copyout(&r32.psr, uap->addr, MIN(uap->data,
1078 		    sizeof(r32.psr)));
1079 		break;
1080 	}
1081 
1082 	return (error);
1083 }
1084 
1085 int
1086 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
1087 {
1088 	struct iovec32 iov32;
1089 	struct iovec *iov;
1090 	struct uio *uio;
1091 	u_int iovlen;
1092 	int error, i;
1093 
1094 	*uiop = NULL;
1095 	if (iovcnt > UIO_MAXIOV)
1096 		return (EINVAL);
1097 	iovlen = iovcnt * sizeof(struct iovec);
1098 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
1099 	iov = (struct iovec *)(uio + 1);
1100 	for (i = 0; i < iovcnt; i++) {
1101 		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
1102 		if (error) {
1103 			free(uio, M_IOV);
1104 			return (error);
1105 		}
1106 		iov[i].iov_base = PTRIN(iov32.iov_base);
1107 		iov[i].iov_len = iov32.iov_len;
1108 	}
1109 	uio->uio_iov = iov;
1110 	uio->uio_iovcnt = iovcnt;
1111 	uio->uio_segflg = UIO_USERSPACE;
1112 	uio->uio_offset = -1;
1113 	uio->uio_resid = 0;
1114 	for (i = 0; i < iovcnt; i++) {
1115 		if (iov->iov_len > INT_MAX - uio->uio_resid) {
1116 			free(uio, M_IOV);
1117 			return (EINVAL);
1118 		}
1119 		uio->uio_resid += iov->iov_len;
1120 		iov++;
1121 	}
1122 	*uiop = uio;
1123 	return (0);
1124 }
1125 
1126 int
1127 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
1128 {
1129 	struct uio *auio;
1130 	int error;
1131 
1132 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1133 	if (error)
1134 		return (error);
1135 	error = kern_readv(td, uap->fd, auio);
1136 	free(auio, M_IOV);
1137 	return (error);
1138 }
1139 
1140 int
1141 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
1142 {
1143 	struct uio *auio;
1144 	int error;
1145 
1146 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1147 	if (error)
1148 		return (error);
1149 	error = kern_writev(td, uap->fd, auio);
1150 	free(auio, M_IOV);
1151 	return (error);
1152 }
1153 
1154 int
1155 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
1156 {
1157 	struct uio *auio;
1158 	int error;
1159 
1160 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1161 	if (error)
1162 		return (error);
1163 	error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
1164 	free(auio, M_IOV);
1165 	return (error);
1166 }
1167 
1168 int
1169 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
1170 {
1171 	struct uio *auio;
1172 	int error;
1173 
1174 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1175 	if (error)
1176 		return (error);
1177 	error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
1178 	free(auio, M_IOV);
1179 	return (error);
1180 }
1181 
1182 int
1183 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
1184     int error)
1185 {
1186 	struct iovec32 iov32;
1187 	struct iovec *iov;
1188 	u_int iovlen;
1189 	int i;
1190 
1191 	*iovp = NULL;
1192 	if (iovcnt > UIO_MAXIOV)
1193 		return (error);
1194 	iovlen = iovcnt * sizeof(struct iovec);
1195 	iov = malloc(iovlen, M_IOV, M_WAITOK);
1196 	for (i = 0; i < iovcnt; i++) {
1197 		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
1198 		if (error) {
1199 			free(iov, M_IOV);
1200 			return (error);
1201 		}
1202 		iov[i].iov_base = PTRIN(iov32.iov_base);
1203 		iov[i].iov_len = iov32.iov_len;
1204 	}
1205 	*iovp = iov;
1206 	return (0);
1207 }
1208 
1209 static int
1210 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
1211 {
1212 	struct msghdr32 m32;
1213 	int error;
1214 
1215 	error = copyin(msg32, &m32, sizeof(m32));
1216 	if (error)
1217 		return (error);
1218 	msg->msg_name = PTRIN(m32.msg_name);
1219 	msg->msg_namelen = m32.msg_namelen;
1220 	msg->msg_iov = PTRIN(m32.msg_iov);
1221 	msg->msg_iovlen = m32.msg_iovlen;
1222 	msg->msg_control = PTRIN(m32.msg_control);
1223 	msg->msg_controllen = m32.msg_controllen;
1224 	msg->msg_flags = m32.msg_flags;
1225 	return (0);
1226 }
1227 
1228 static int
1229 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1230 {
1231 	struct msghdr32 m32;
1232 	int error;
1233 
1234 	m32.msg_name = PTROUT(msg->msg_name);
1235 	m32.msg_namelen = msg->msg_namelen;
1236 	m32.msg_iov = PTROUT(msg->msg_iov);
1237 	m32.msg_iovlen = msg->msg_iovlen;
1238 	m32.msg_control = PTROUT(msg->msg_control);
1239 	m32.msg_controllen = msg->msg_controllen;
1240 	m32.msg_flags = msg->msg_flags;
1241 	error = copyout(&m32, msg32, sizeof(m32));
1242 	return (error);
1243 }
1244 
1245 #ifndef __mips__
1246 #define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
1247 #else
1248 #define FREEBSD32_ALIGNBYTES	(sizeof(long) - 1)
1249 #endif
1250 #define FREEBSD32_ALIGN(p)	\
1251 	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1252 #define	FREEBSD32_CMSG_SPACE(l)	\
1253 	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1254 
1255 #define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
1256 				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1257 
1258 static size_t
1259 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
1260 {
1261 	size_t copylen;
1262 	union {
1263 		struct timespec32 ts;
1264 		struct timeval32 tv;
1265 		struct bintime32 bt;
1266 	} tmp32;
1267 
1268 	union {
1269 		struct timespec ts;
1270 		struct timeval tv;
1271 		struct bintime bt;
1272 	} *in;
1273 
1274 	in = data;
1275 	copylen = 0;
1276 	switch (cm->cmsg_level) {
1277 	case SOL_SOCKET:
1278 		switch (cm->cmsg_type) {
1279 		case SCM_TIMESTAMP:
1280 			TV_CP(*in, tmp32, tv);
1281 			copylen = sizeof(tmp32.tv);
1282 			break;
1283 
1284 		case SCM_BINTIME:
1285 			BT_CP(*in, tmp32, bt);
1286 			copylen = sizeof(tmp32.bt);
1287 			break;
1288 
1289 		case SCM_REALTIME:
1290 		case SCM_MONOTONIC:
1291 			TS_CP(*in, tmp32, ts);
1292 			copylen = sizeof(tmp32.ts);
1293 			break;
1294 
1295 		default:
1296 			break;
1297 		}
1298 
1299 	default:
1300 		break;
1301 	}
1302 
1303 	if (copylen == 0)
1304 		return (datalen);
1305 
1306 	KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
1307 
1308 	bcopy(&tmp32, data, copylen);
1309 	return (copylen);
1310 }
1311 
1312 static int
1313 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1314 {
1315 	struct cmsghdr *cm;
1316 	void *data;
1317 	socklen_t clen, datalen, datalen_out, oldclen;
1318 	int error;
1319 	caddr_t ctlbuf;
1320 	int len, maxlen, copylen;
1321 	struct mbuf *m;
1322 	error = 0;
1323 
1324 	len    = msg->msg_controllen;
1325 	maxlen = msg->msg_controllen;
1326 	msg->msg_controllen = 0;
1327 
1328 	ctlbuf = msg->msg_control;
1329 	for (m = control; m != NULL && len > 0; m = m->m_next) {
1330 		cm = mtod(m, struct cmsghdr *);
1331 		clen = m->m_len;
1332 		while (cm != NULL) {
1333 			if (sizeof(struct cmsghdr) > clen ||
1334 			    cm->cmsg_len > clen) {
1335 				error = EINVAL;
1336 				break;
1337 			}
1338 
1339 			data   = CMSG_DATA(cm);
1340 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1341 			datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
1342 
1343 			/*
1344 			 * Copy out the message header.  Preserve the native
1345 			 * message size in case we need to inspect the message
1346 			 * contents later.
1347 			 */
1348 			copylen = sizeof(struct cmsghdr);
1349 			if (len < copylen) {
1350 				msg->msg_flags |= MSG_CTRUNC;
1351 				m_dispose_extcontrolm(m);
1352 				goto exit;
1353 			}
1354 			oldclen = cm->cmsg_len;
1355 			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1356 			    datalen_out;
1357 			error = copyout(cm, ctlbuf, copylen);
1358 			cm->cmsg_len = oldclen;
1359 			if (error != 0)
1360 				goto exit;
1361 
1362 			ctlbuf += FREEBSD32_ALIGN(copylen);
1363 			len    -= FREEBSD32_ALIGN(copylen);
1364 
1365 			copylen = datalen_out;
1366 			if (len < copylen) {
1367 				msg->msg_flags |= MSG_CTRUNC;
1368 				m_dispose_extcontrolm(m);
1369 				break;
1370 			}
1371 
1372 			/* Copy out the message data. */
1373 			error = copyout(data, ctlbuf, copylen);
1374 			if (error)
1375 				goto exit;
1376 
1377 			ctlbuf += FREEBSD32_ALIGN(copylen);
1378 			len    -= FREEBSD32_ALIGN(copylen);
1379 
1380 			if (CMSG_SPACE(datalen) < clen) {
1381 				clen -= CMSG_SPACE(datalen);
1382 				cm = (struct cmsghdr *)
1383 				    ((caddr_t)cm + CMSG_SPACE(datalen));
1384 			} else {
1385 				clen = 0;
1386 				cm = NULL;
1387 			}
1388 
1389 			msg->msg_controllen +=
1390 			    FREEBSD32_CMSG_SPACE(datalen_out);
1391 		}
1392 	}
1393 	if (len == 0 && m != NULL) {
1394 		msg->msg_flags |= MSG_CTRUNC;
1395 		m_dispose_extcontrolm(m);
1396 	}
1397 
1398 exit:
1399 	return (error);
1400 }
1401 
1402 int
1403 freebsd32_recvmsg(td, uap)
1404 	struct thread *td;
1405 	struct freebsd32_recvmsg_args /* {
1406 		int	s;
1407 		struct	msghdr32 *msg;
1408 		int	flags;
1409 	} */ *uap;
1410 {
1411 	struct msghdr msg;
1412 	struct msghdr32 m32;
1413 	struct iovec *uiov, *iov;
1414 	struct mbuf *control = NULL;
1415 	struct mbuf **controlp;
1416 
1417 	int error;
1418 	error = copyin(uap->msg, &m32, sizeof(m32));
1419 	if (error)
1420 		return (error);
1421 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1422 	if (error)
1423 		return (error);
1424 	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1425 	    EMSGSIZE);
1426 	if (error)
1427 		return (error);
1428 	msg.msg_flags = uap->flags;
1429 	uiov = msg.msg_iov;
1430 	msg.msg_iov = iov;
1431 
1432 	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1433 	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1434 	if (error == 0) {
1435 		msg.msg_iov = uiov;
1436 
1437 		if (control != NULL)
1438 			error = freebsd32_copy_msg_out(&msg, control);
1439 		else
1440 			msg.msg_controllen = 0;
1441 
1442 		if (error == 0)
1443 			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1444 	}
1445 	free(iov, M_IOV);
1446 
1447 	if (control != NULL) {
1448 		if (error != 0)
1449 			m_dispose_extcontrolm(control);
1450 		m_freem(control);
1451 	}
1452 
1453 	return (error);
1454 }
1455 
1456 /*
1457  * Copy-in the array of control messages constructed using alignment
1458  * and padding suitable for a 32-bit environment and construct an
1459  * mbuf using alignment and padding suitable for a 64-bit kernel.
1460  * The alignment and padding are defined indirectly by CMSG_DATA(),
1461  * CMSG_SPACE() and CMSG_LEN().
1462  */
1463 static int
1464 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1465 {
1466 	struct cmsghdr *cm;
1467 	struct mbuf *m;
1468 	void *in, *in1, *md;
1469 	u_int msglen, outlen;
1470 	int error;
1471 
1472 	if (buflen > MCLBYTES)
1473 		return (EINVAL);
1474 
1475 	in = malloc(buflen, M_TEMP, M_WAITOK);
1476 	error = copyin(buf, in, buflen);
1477 	if (error != 0)
1478 		goto out;
1479 
1480 	/*
1481 	 * Make a pass over the input buffer to determine the amount of space
1482 	 * required for 64 bit-aligned copies of the control messages.
1483 	 */
1484 	in1 = in;
1485 	outlen = 0;
1486 	while (buflen > 0) {
1487 		if (buflen < sizeof(*cm)) {
1488 			error = EINVAL;
1489 			break;
1490 		}
1491 		cm = (struct cmsghdr *)in1;
1492 		if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm))) {
1493 			error = EINVAL;
1494 			break;
1495 		}
1496 		msglen = FREEBSD32_ALIGN(cm->cmsg_len);
1497 		if (msglen > buflen || msglen < cm->cmsg_len) {
1498 			error = EINVAL;
1499 			break;
1500 		}
1501 		buflen -= msglen;
1502 
1503 		in1 = (char *)in1 + msglen;
1504 		outlen += CMSG_ALIGN(sizeof(*cm)) +
1505 		    CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm)));
1506 	}
1507 	if (error == 0 && outlen > MCLBYTES) {
1508 		/*
1509 		 * XXXMJ This implies that the upper limit on 32-bit aligned
1510 		 * control messages is less than MCLBYTES, and so we are not
1511 		 * perfectly compatible.  However, there is no platform
1512 		 * guarantee that mbuf clusters larger than MCLBYTES can be
1513 		 * allocated.
1514 		 */
1515 		error = EINVAL;
1516 	}
1517 	if (error != 0)
1518 		goto out;
1519 
1520 	m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0);
1521 	m->m_len = outlen;
1522 	md = mtod(m, void *);
1523 
1524 	/*
1525 	 * Make a second pass over input messages, copying them into the output
1526 	 * buffer.
1527 	 */
1528 	in1 = in;
1529 	while (outlen > 0) {
1530 		/* Copy the message header and align the length field. */
1531 		cm = md;
1532 		memcpy(cm, in1, sizeof(*cm));
1533 		msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm));
1534 		cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen;
1535 
1536 		/* Copy the message body. */
1537 		in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm));
1538 		md = (char *)md + CMSG_ALIGN(sizeof(*cm));
1539 		memcpy(md, in1, msglen);
1540 		in1 = (char *)in1 + FREEBSD32_ALIGN(msglen);
1541 		md = (char *)md + CMSG_ALIGN(msglen);
1542 		KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen),
1543 		    ("outlen %u underflow, msglen %u", outlen, msglen));
1544 		outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen);
1545 	}
1546 
1547 	*mp = m;
1548 out:
1549 	free(in, M_TEMP);
1550 	return (error);
1551 }
1552 
1553 int
1554 freebsd32_sendmsg(struct thread *td,
1555 		  struct freebsd32_sendmsg_args *uap)
1556 {
1557 	struct msghdr msg;
1558 	struct msghdr32 m32;
1559 	struct iovec *iov;
1560 	struct mbuf *control = NULL;
1561 	struct sockaddr *to = NULL;
1562 	int error;
1563 
1564 	error = copyin(uap->msg, &m32, sizeof(m32));
1565 	if (error)
1566 		return (error);
1567 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1568 	if (error)
1569 		return (error);
1570 	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1571 	    EMSGSIZE);
1572 	if (error)
1573 		return (error);
1574 	msg.msg_iov = iov;
1575 	if (msg.msg_name != NULL) {
1576 		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1577 		if (error) {
1578 			to = NULL;
1579 			goto out;
1580 		}
1581 		msg.msg_name = to;
1582 	}
1583 
1584 	if (msg.msg_control) {
1585 		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1586 			error = EINVAL;
1587 			goto out;
1588 		}
1589 
1590 		error = freebsd32_copyin_control(&control, msg.msg_control,
1591 		    msg.msg_controllen);
1592 		if (error)
1593 			goto out;
1594 
1595 		msg.msg_control = NULL;
1596 		msg.msg_controllen = 0;
1597 	}
1598 
1599 	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1600 	    UIO_USERSPACE);
1601 
1602 out:
1603 	free(iov, M_IOV);
1604 	if (to)
1605 		free(to, M_SONAME);
1606 	return (error);
1607 }
1608 
1609 int
1610 freebsd32_recvfrom(struct thread *td,
1611 		   struct freebsd32_recvfrom_args *uap)
1612 {
1613 	struct msghdr msg;
1614 	struct iovec aiov;
1615 	int error;
1616 
1617 	if (uap->fromlenaddr) {
1618 		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1619 		    sizeof(msg.msg_namelen));
1620 		if (error)
1621 			return (error);
1622 	} else {
1623 		msg.msg_namelen = 0;
1624 	}
1625 
1626 	msg.msg_name = PTRIN(uap->from);
1627 	msg.msg_iov = &aiov;
1628 	msg.msg_iovlen = 1;
1629 	aiov.iov_base = PTRIN(uap->buf);
1630 	aiov.iov_len = uap->len;
1631 	msg.msg_control = NULL;
1632 	msg.msg_flags = uap->flags;
1633 	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1634 	if (error == 0 && uap->fromlenaddr)
1635 		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1636 		    sizeof (msg.msg_namelen));
1637 	return (error);
1638 }
1639 
1640 int
1641 freebsd32_settimeofday(struct thread *td,
1642 		       struct freebsd32_settimeofday_args *uap)
1643 {
1644 	struct timeval32 tv32;
1645 	struct timeval tv, *tvp;
1646 	struct timezone tz, *tzp;
1647 	int error;
1648 
1649 	if (uap->tv) {
1650 		error = copyin(uap->tv, &tv32, sizeof(tv32));
1651 		if (error)
1652 			return (error);
1653 		CP(tv32, tv, tv_sec);
1654 		CP(tv32, tv, tv_usec);
1655 		tvp = &tv;
1656 	} else
1657 		tvp = NULL;
1658 	if (uap->tzp) {
1659 		error = copyin(uap->tzp, &tz, sizeof(tz));
1660 		if (error)
1661 			return (error);
1662 		tzp = &tz;
1663 	} else
1664 		tzp = NULL;
1665 	return (kern_settimeofday(td, tvp, tzp));
1666 }
1667 
1668 int
1669 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1670 {
1671 	struct timeval32 s32[2];
1672 	struct timeval s[2], *sp;
1673 	int error;
1674 
1675 	if (uap->tptr != NULL) {
1676 		error = copyin(uap->tptr, s32, sizeof(s32));
1677 		if (error)
1678 			return (error);
1679 		CP(s32[0], s[0], tv_sec);
1680 		CP(s32[0], s[0], tv_usec);
1681 		CP(s32[1], s[1], tv_sec);
1682 		CP(s32[1], s[1], tv_usec);
1683 		sp = s;
1684 	} else
1685 		sp = NULL;
1686 	return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1687 	    sp, UIO_SYSSPACE));
1688 }
1689 
1690 int
1691 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1692 {
1693 	struct timeval32 s32[2];
1694 	struct timeval s[2], *sp;
1695 	int error;
1696 
1697 	if (uap->tptr != NULL) {
1698 		error = copyin(uap->tptr, s32, sizeof(s32));
1699 		if (error)
1700 			return (error);
1701 		CP(s32[0], s[0], tv_sec);
1702 		CP(s32[0], s[0], tv_usec);
1703 		CP(s32[1], s[1], tv_sec);
1704 		CP(s32[1], s[1], tv_usec);
1705 		sp = s;
1706 	} else
1707 		sp = NULL;
1708 	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1709 }
1710 
1711 int
1712 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1713 {
1714 	struct timeval32 s32[2];
1715 	struct timeval s[2], *sp;
1716 	int error;
1717 
1718 	if (uap->tptr != NULL) {
1719 		error = copyin(uap->tptr, s32, sizeof(s32));
1720 		if (error)
1721 			return (error);
1722 		CP(s32[0], s[0], tv_sec);
1723 		CP(s32[0], s[0], tv_usec);
1724 		CP(s32[1], s[1], tv_sec);
1725 		CP(s32[1], s[1], tv_usec);
1726 		sp = s;
1727 	} else
1728 		sp = NULL;
1729 	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1730 }
1731 
1732 int
1733 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1734 {
1735 	struct timeval32 s32[2];
1736 	struct timeval s[2], *sp;
1737 	int error;
1738 
1739 	if (uap->times != NULL) {
1740 		error = copyin(uap->times, s32, sizeof(s32));
1741 		if (error)
1742 			return (error);
1743 		CP(s32[0], s[0], tv_sec);
1744 		CP(s32[0], s[0], tv_usec);
1745 		CP(s32[1], s[1], tv_sec);
1746 		CP(s32[1], s[1], tv_usec);
1747 		sp = s;
1748 	} else
1749 		sp = NULL;
1750 	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1751 		sp, UIO_SYSSPACE));
1752 }
1753 
1754 int
1755 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1756 {
1757 	struct timespec32 ts32[2];
1758 	struct timespec ts[2], *tsp;
1759 	int error;
1760 
1761 	if (uap->times != NULL) {
1762 		error = copyin(uap->times, ts32, sizeof(ts32));
1763 		if (error)
1764 			return (error);
1765 		CP(ts32[0], ts[0], tv_sec);
1766 		CP(ts32[0], ts[0], tv_nsec);
1767 		CP(ts32[1], ts[1], tv_sec);
1768 		CP(ts32[1], ts[1], tv_nsec);
1769 		tsp = ts;
1770 	} else
1771 		tsp = NULL;
1772 	return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1773 }
1774 
1775 int
1776 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1777 {
1778 	struct timespec32 ts32[2];
1779 	struct timespec ts[2], *tsp;
1780 	int error;
1781 
1782 	if (uap->times != NULL) {
1783 		error = copyin(uap->times, ts32, sizeof(ts32));
1784 		if (error)
1785 			return (error);
1786 		CP(ts32[0], ts[0], tv_sec);
1787 		CP(ts32[0], ts[0], tv_nsec);
1788 		CP(ts32[1], ts[1], tv_sec);
1789 		CP(ts32[1], ts[1], tv_nsec);
1790 		tsp = ts;
1791 	} else
1792 		tsp = NULL;
1793 	return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1794 	    tsp, UIO_SYSSPACE, uap->flag));
1795 }
1796 
1797 int
1798 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1799 {
1800 	struct timeval32 tv32;
1801 	struct timeval delta, olddelta, *deltap;
1802 	int error;
1803 
1804 	if (uap->delta) {
1805 		error = copyin(uap->delta, &tv32, sizeof(tv32));
1806 		if (error)
1807 			return (error);
1808 		CP(tv32, delta, tv_sec);
1809 		CP(tv32, delta, tv_usec);
1810 		deltap = &delta;
1811 	} else
1812 		deltap = NULL;
1813 	error = kern_adjtime(td, deltap, &olddelta);
1814 	if (uap->olddelta && error == 0) {
1815 		CP(olddelta, tv32, tv_sec);
1816 		CP(olddelta, tv32, tv_usec);
1817 		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1818 	}
1819 	return (error);
1820 }
1821 
1822 #ifdef COMPAT_FREEBSD4
1823 int
1824 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1825 {
1826 	struct statfs32 s32;
1827 	struct statfs *sp;
1828 	int error;
1829 
1830 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1831 	error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1832 	if (error == 0) {
1833 		copy_statfs(sp, &s32);
1834 		error = copyout(&s32, uap->buf, sizeof(s32));
1835 	}
1836 	free(sp, M_STATFS);
1837 	return (error);
1838 }
1839 #endif
1840 
1841 #ifdef COMPAT_FREEBSD4
1842 int
1843 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1844 {
1845 	struct statfs32 s32;
1846 	struct statfs *sp;
1847 	int error;
1848 
1849 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1850 	error = kern_fstatfs(td, uap->fd, sp);
1851 	if (error == 0) {
1852 		copy_statfs(sp, &s32);
1853 		error = copyout(&s32, uap->buf, sizeof(s32));
1854 	}
1855 	free(sp, M_STATFS);
1856 	return (error);
1857 }
1858 #endif
1859 
1860 #ifdef COMPAT_FREEBSD4
1861 int
1862 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1863 {
1864 	struct statfs32 s32;
1865 	struct statfs *sp;
1866 	fhandle_t fh;
1867 	int error;
1868 
1869 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1870 		return (error);
1871 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1872 	error = kern_fhstatfs(td, fh, sp);
1873 	if (error == 0) {
1874 		copy_statfs(sp, &s32);
1875 		error = copyout(&s32, uap->buf, sizeof(s32));
1876 	}
1877 	free(sp, M_STATFS);
1878 	return (error);
1879 }
1880 #endif
1881 
1882 int
1883 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1884 {
1885 
1886 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1887 	    PAIR32TO64(off_t, uap->offset)));
1888 }
1889 
1890 int
1891 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1892 {
1893 
1894 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1895 	    PAIR32TO64(off_t, uap->offset)));
1896 }
1897 
1898 #ifdef COMPAT_43
1899 int
1900 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1901 {
1902 
1903 	return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1904 }
1905 #endif
1906 
1907 int
1908 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1909 {
1910 	int error;
1911 	off_t pos;
1912 
1913 	error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1914 	    uap->whence);
1915 	/* Expand the quad return into two parts for eax and edx */
1916 	pos = td->td_uretoff.tdu_off;
1917 	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1918 	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1919 	return error;
1920 }
1921 
1922 int
1923 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1924 {
1925 
1926 	return (kern_truncate(td, uap->path, UIO_USERSPACE,
1927 	    PAIR32TO64(off_t, uap->length)));
1928 }
1929 
1930 int
1931 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1932 {
1933 
1934 	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1935 }
1936 
1937 #ifdef COMPAT_43
1938 int
1939 ofreebsd32_getdirentries(struct thread *td,
1940     struct ofreebsd32_getdirentries_args *uap)
1941 {
1942 	struct ogetdirentries_args ap;
1943 	int error;
1944 	long loff;
1945 	int32_t loff_cut;
1946 
1947 	ap.fd = uap->fd;
1948 	ap.buf = uap->buf;
1949 	ap.count = uap->count;
1950 	ap.basep = NULL;
1951 	error = kern_ogetdirentries(td, &ap, &loff);
1952 	if (error == 0) {
1953 		loff_cut = loff;
1954 		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1955 	}
1956 	return (error);
1957 }
1958 #endif
1959 
1960 #if defined(COMPAT_FREEBSD11)
1961 int
1962 freebsd11_freebsd32_getdirentries(struct thread *td,
1963     struct freebsd11_freebsd32_getdirentries_args *uap)
1964 {
1965 	long base;
1966 	int32_t base32;
1967 	int error;
1968 
1969 	error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1970 	    &base, NULL);
1971 	if (error)
1972 		return (error);
1973 	if (uap->basep != NULL) {
1974 		base32 = base;
1975 		error = copyout(&base32, uap->basep, sizeof(int32_t));
1976 	}
1977 	return (error);
1978 }
1979 
1980 int
1981 freebsd11_freebsd32_getdents(struct thread *td,
1982     struct freebsd11_freebsd32_getdents_args *uap)
1983 {
1984 	struct freebsd11_freebsd32_getdirentries_args ap;
1985 
1986 	ap.fd = uap->fd;
1987 	ap.buf = uap->buf;
1988 	ap.count = uap->count;
1989 	ap.basep = NULL;
1990 	return (freebsd11_freebsd32_getdirentries(td, &ap));
1991 }
1992 #endif /* COMPAT_FREEBSD11 */
1993 
1994 #ifdef COMPAT_FREEBSD6
1995 /* versions with the 'int pad' argument */
1996 int
1997 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1998 {
1999 
2000 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
2001 	    PAIR32TO64(off_t, uap->offset)));
2002 }
2003 
2004 int
2005 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
2006 {
2007 
2008 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
2009 	    PAIR32TO64(off_t, uap->offset)));
2010 }
2011 
2012 int
2013 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
2014 {
2015 	int error;
2016 	off_t pos;
2017 
2018 	error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2019 	    uap->whence);
2020 	/* Expand the quad return into two parts for eax and edx */
2021 	pos = *(off_t *)(td->td_retval);
2022 	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
2023 	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
2024 	return error;
2025 }
2026 
2027 int
2028 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
2029 {
2030 
2031 	return (kern_truncate(td, uap->path, UIO_USERSPACE,
2032 	    PAIR32TO64(off_t, uap->length)));
2033 }
2034 
2035 int
2036 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
2037 {
2038 
2039 	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
2040 }
2041 #endif /* COMPAT_FREEBSD6 */
2042 
2043 struct sf_hdtr32 {
2044 	uint32_t headers;
2045 	int hdr_cnt;
2046 	uint32_t trailers;
2047 	int trl_cnt;
2048 };
2049 
2050 static int
2051 freebsd32_do_sendfile(struct thread *td,
2052     struct freebsd32_sendfile_args *uap, int compat)
2053 {
2054 	struct sf_hdtr32 hdtr32;
2055 	struct sf_hdtr hdtr;
2056 	struct uio *hdr_uio, *trl_uio;
2057 	struct file *fp;
2058 	cap_rights_t rights;
2059 	struct iovec32 *iov32;
2060 	off_t offset, sbytes;
2061 	int error;
2062 
2063 	offset = PAIR32TO64(off_t, uap->offset);
2064 	if (offset < 0)
2065 		return (EINVAL);
2066 
2067 	hdr_uio = trl_uio = NULL;
2068 
2069 	if (uap->hdtr != NULL) {
2070 		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
2071 		if (error)
2072 			goto out;
2073 		PTRIN_CP(hdtr32, hdtr, headers);
2074 		CP(hdtr32, hdtr, hdr_cnt);
2075 		PTRIN_CP(hdtr32, hdtr, trailers);
2076 		CP(hdtr32, hdtr, trl_cnt);
2077 
2078 		if (hdtr.headers != NULL) {
2079 			iov32 = PTRIN(hdtr32.headers);
2080 			error = freebsd32_copyinuio(iov32,
2081 			    hdtr32.hdr_cnt, &hdr_uio);
2082 			if (error)
2083 				goto out;
2084 #ifdef COMPAT_FREEBSD4
2085 			/*
2086 			 * In FreeBSD < 5.0 the nbytes to send also included
2087 			 * the header.  If compat is specified subtract the
2088 			 * header size from nbytes.
2089 			 */
2090 			if (compat) {
2091 				if (uap->nbytes > hdr_uio->uio_resid)
2092 					uap->nbytes -= hdr_uio->uio_resid;
2093 				else
2094 					uap->nbytes = 0;
2095 			}
2096 #endif
2097 		}
2098 		if (hdtr.trailers != NULL) {
2099 			iov32 = PTRIN(hdtr32.trailers);
2100 			error = freebsd32_copyinuio(iov32,
2101 			    hdtr32.trl_cnt, &trl_uio);
2102 			if (error)
2103 				goto out;
2104 		}
2105 	}
2106 
2107 	AUDIT_ARG_FD(uap->fd);
2108 
2109 	if ((error = fget_read(td, uap->fd,
2110 	    cap_rights_init_one(&rights, CAP_PREAD), &fp)) != 0)
2111 		goto out;
2112 
2113 	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
2114 	    uap->nbytes, &sbytes, uap->flags, td);
2115 	fdrop(fp, td);
2116 
2117 	if (uap->sbytes != NULL)
2118 		copyout(&sbytes, uap->sbytes, sizeof(off_t));
2119 
2120 out:
2121 	if (hdr_uio)
2122 		free(hdr_uio, M_IOV);
2123 	if (trl_uio)
2124 		free(trl_uio, M_IOV);
2125 	return (error);
2126 }
2127 
2128 #ifdef COMPAT_FREEBSD4
2129 int
2130 freebsd4_freebsd32_sendfile(struct thread *td,
2131     struct freebsd4_freebsd32_sendfile_args *uap)
2132 {
2133 	return (freebsd32_do_sendfile(td,
2134 	    (struct freebsd32_sendfile_args *)uap, 1));
2135 }
2136 #endif
2137 
2138 int
2139 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
2140 {
2141 
2142 	return (freebsd32_do_sendfile(td, uap, 0));
2143 }
2144 
2145 static void
2146 copy_stat(struct stat *in, struct stat32 *out)
2147 {
2148 
2149 	CP(*in, *out, st_dev);
2150 	CP(*in, *out, st_ino);
2151 	CP(*in, *out, st_mode);
2152 	CP(*in, *out, st_nlink);
2153 	CP(*in, *out, st_uid);
2154 	CP(*in, *out, st_gid);
2155 	CP(*in, *out, st_rdev);
2156 	TS_CP(*in, *out, st_atim);
2157 	TS_CP(*in, *out, st_mtim);
2158 	TS_CP(*in, *out, st_ctim);
2159 	CP(*in, *out, st_size);
2160 	CP(*in, *out, st_blocks);
2161 	CP(*in, *out, st_blksize);
2162 	CP(*in, *out, st_flags);
2163 	CP(*in, *out, st_gen);
2164 	TS_CP(*in, *out, st_birthtim);
2165 	out->st_padding0 = 0;
2166 	out->st_padding1 = 0;
2167 #ifdef __STAT32_TIME_T_EXT
2168 	out->st_atim_ext = 0;
2169 	out->st_mtim_ext = 0;
2170 	out->st_ctim_ext = 0;
2171 	out->st_btim_ext = 0;
2172 #endif
2173 	bzero(out->st_spare, sizeof(out->st_spare));
2174 }
2175 
2176 #ifdef COMPAT_43
2177 static void
2178 copy_ostat(struct stat *in, struct ostat32 *out)
2179 {
2180 
2181 	bzero(out, sizeof(*out));
2182 	CP(*in, *out, st_dev);
2183 	CP(*in, *out, st_ino);
2184 	CP(*in, *out, st_mode);
2185 	CP(*in, *out, st_nlink);
2186 	CP(*in, *out, st_uid);
2187 	CP(*in, *out, st_gid);
2188 	CP(*in, *out, st_rdev);
2189 	out->st_size = MIN(in->st_size, INT32_MAX);
2190 	TS_CP(*in, *out, st_atim);
2191 	TS_CP(*in, *out, st_mtim);
2192 	TS_CP(*in, *out, st_ctim);
2193 	CP(*in, *out, st_blksize);
2194 	CP(*in, *out, st_blocks);
2195 	CP(*in, *out, st_flags);
2196 	CP(*in, *out, st_gen);
2197 }
2198 #endif
2199 
2200 #ifdef COMPAT_43
2201 int
2202 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
2203 {
2204 	struct stat sb;
2205 	struct ostat32 sb32;
2206 	int error;
2207 
2208 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2209 	    &sb, NULL);
2210 	if (error)
2211 		return (error);
2212 	copy_ostat(&sb, &sb32);
2213 	error = copyout(&sb32, uap->ub, sizeof (sb32));
2214 	return (error);
2215 }
2216 #endif
2217 
2218 int
2219 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
2220 {
2221 	struct stat ub;
2222 	struct stat32 ub32;
2223 	int error;
2224 
2225 	error = kern_fstat(td, uap->fd, &ub);
2226 	if (error)
2227 		return (error);
2228 	copy_stat(&ub, &ub32);
2229 	error = copyout(&ub32, uap->ub, sizeof(ub32));
2230 	return (error);
2231 }
2232 
2233 #ifdef COMPAT_43
2234 int
2235 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2236 {
2237 	struct stat ub;
2238 	struct ostat32 ub32;
2239 	int error;
2240 
2241 	error = kern_fstat(td, uap->fd, &ub);
2242 	if (error)
2243 		return (error);
2244 	copy_ostat(&ub, &ub32);
2245 	error = copyout(&ub32, uap->ub, sizeof(ub32));
2246 	return (error);
2247 }
2248 #endif
2249 
2250 int
2251 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2252 {
2253 	struct stat ub;
2254 	struct stat32 ub32;
2255 	int error;
2256 
2257 	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2258 	    &ub, NULL);
2259 	if (error)
2260 		return (error);
2261 	copy_stat(&ub, &ub32);
2262 	error = copyout(&ub32, uap->buf, sizeof(ub32));
2263 	return (error);
2264 }
2265 
2266 #ifdef COMPAT_43
2267 int
2268 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2269 {
2270 	struct stat sb;
2271 	struct ostat32 sb32;
2272 	int error;
2273 
2274 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2275 	    UIO_USERSPACE, &sb, NULL);
2276 	if (error)
2277 		return (error);
2278 	copy_ostat(&sb, &sb32);
2279 	error = copyout(&sb32, uap->ub, sizeof (sb32));
2280 	return (error);
2281 }
2282 #endif
2283 
2284 int
2285 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2286 {
2287 	struct stat sb;
2288 	struct stat32 sb32;
2289 	struct fhandle fh;
2290 	int error;
2291 
2292 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2293         if (error != 0)
2294                 return (error);
2295 	error = kern_fhstat(td, fh, &sb);
2296 	if (error != 0)
2297 		return (error);
2298 	copy_stat(&sb, &sb32);
2299 	error = copyout(&sb32, uap->sb, sizeof (sb32));
2300 	return (error);
2301 }
2302 
2303 #if defined(COMPAT_FREEBSD11)
2304 extern int ino64_trunc_error;
2305 
2306 static int
2307 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2308 {
2309 
2310 	CP(*in, *out, st_ino);
2311 	if (in->st_ino != out->st_ino) {
2312 		switch (ino64_trunc_error) {
2313 		default:
2314 		case 0:
2315 			break;
2316 		case 1:
2317 			return (EOVERFLOW);
2318 		case 2:
2319 			out->st_ino = UINT32_MAX;
2320 			break;
2321 		}
2322 	}
2323 	CP(*in, *out, st_nlink);
2324 	if (in->st_nlink != out->st_nlink) {
2325 		switch (ino64_trunc_error) {
2326 		default:
2327 		case 0:
2328 			break;
2329 		case 1:
2330 			return (EOVERFLOW);
2331 		case 2:
2332 			out->st_nlink = UINT16_MAX;
2333 			break;
2334 		}
2335 	}
2336 	out->st_dev = in->st_dev;
2337 	if (out->st_dev != in->st_dev) {
2338 		switch (ino64_trunc_error) {
2339 		default:
2340 			break;
2341 		case 1:
2342 			return (EOVERFLOW);
2343 		}
2344 	}
2345 	CP(*in, *out, st_mode);
2346 	CP(*in, *out, st_uid);
2347 	CP(*in, *out, st_gid);
2348 	out->st_rdev = in->st_rdev;
2349 	if (out->st_rdev != in->st_rdev) {
2350 		switch (ino64_trunc_error) {
2351 		default:
2352 			break;
2353 		case 1:
2354 			return (EOVERFLOW);
2355 		}
2356 	}
2357 	TS_CP(*in, *out, st_atim);
2358 	TS_CP(*in, *out, st_mtim);
2359 	TS_CP(*in, *out, st_ctim);
2360 	CP(*in, *out, st_size);
2361 	CP(*in, *out, st_blocks);
2362 	CP(*in, *out, st_blksize);
2363 	CP(*in, *out, st_flags);
2364 	CP(*in, *out, st_gen);
2365 	TS_CP(*in, *out, st_birthtim);
2366 	out->st_lspare = 0;
2367 	bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2368 	    sizeof(*out) - offsetof(struct freebsd11_stat32,
2369 	    st_birthtim) - sizeof(out->st_birthtim));
2370 	return (0);
2371 }
2372 
2373 int
2374 freebsd11_freebsd32_stat(struct thread *td,
2375     struct freebsd11_freebsd32_stat_args *uap)
2376 {
2377 	struct stat sb;
2378 	struct freebsd11_stat32 sb32;
2379 	int error;
2380 
2381 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2382 	    &sb, NULL);
2383 	if (error != 0)
2384 		return (error);
2385 	error = freebsd11_cvtstat32(&sb, &sb32);
2386 	if (error == 0)
2387 		error = copyout(&sb32, uap->ub, sizeof (sb32));
2388 	return (error);
2389 }
2390 
2391 int
2392 freebsd11_freebsd32_fstat(struct thread *td,
2393     struct freebsd11_freebsd32_fstat_args *uap)
2394 {
2395 	struct stat sb;
2396 	struct freebsd11_stat32 sb32;
2397 	int error;
2398 
2399 	error = kern_fstat(td, uap->fd, &sb);
2400 	if (error != 0)
2401 		return (error);
2402 	error = freebsd11_cvtstat32(&sb, &sb32);
2403 	if (error == 0)
2404 		error = copyout(&sb32, uap->ub, sizeof (sb32));
2405 	return (error);
2406 }
2407 
2408 int
2409 freebsd11_freebsd32_fstatat(struct thread *td,
2410     struct freebsd11_freebsd32_fstatat_args *uap)
2411 {
2412 	struct stat sb;
2413 	struct freebsd11_stat32 sb32;
2414 	int error;
2415 
2416 	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2417 	    &sb, NULL);
2418 	if (error != 0)
2419 		return (error);
2420 	error = freebsd11_cvtstat32(&sb, &sb32);
2421 	if (error == 0)
2422 		error = copyout(&sb32, uap->buf, sizeof (sb32));
2423 	return (error);
2424 }
2425 
2426 int
2427 freebsd11_freebsd32_lstat(struct thread *td,
2428     struct freebsd11_freebsd32_lstat_args *uap)
2429 {
2430 	struct stat sb;
2431 	struct freebsd11_stat32 sb32;
2432 	int error;
2433 
2434 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2435 	    UIO_USERSPACE, &sb, NULL);
2436 	if (error != 0)
2437 		return (error);
2438 	error = freebsd11_cvtstat32(&sb, &sb32);
2439 	if (error == 0)
2440 		error = copyout(&sb32, uap->ub, sizeof (sb32));
2441 	return (error);
2442 }
2443 
2444 int
2445 freebsd11_freebsd32_fhstat(struct thread *td,
2446     struct freebsd11_freebsd32_fhstat_args *uap)
2447 {
2448 	struct stat sb;
2449 	struct freebsd11_stat32 sb32;
2450 	struct fhandle fh;
2451 	int error;
2452 
2453 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2454         if (error != 0)
2455                 return (error);
2456 	error = kern_fhstat(td, fh, &sb);
2457 	if (error != 0)
2458 		return (error);
2459 	error = freebsd11_cvtstat32(&sb, &sb32);
2460 	if (error == 0)
2461 		error = copyout(&sb32, uap->sb, sizeof (sb32));
2462 	return (error);
2463 }
2464 #endif
2465 
2466 int
2467 freebsd32___sysctl(struct thread *td, struct freebsd32___sysctl_args *uap)
2468 {
2469 	int error, name[CTL_MAXNAME];
2470 	size_t j, oldlen;
2471 	uint32_t tmp;
2472 
2473 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2474 		return (EINVAL);
2475  	error = copyin(uap->name, name, uap->namelen * sizeof(int));
2476  	if (error)
2477 		return (error);
2478 	if (uap->oldlenp) {
2479 		error = fueword32(uap->oldlenp, &tmp);
2480 		oldlen = tmp;
2481 	} else {
2482 		oldlen = 0;
2483 	}
2484 	if (error != 0)
2485 		return (EFAULT);
2486 	error = userland_sysctl(td, name, uap->namelen,
2487 		uap->old, &oldlen, 1,
2488 		uap->new, uap->newlen, &j, SCTL_MASK32);
2489 	if (error)
2490 		return (error);
2491 	if (uap->oldlenp)
2492 		suword32(uap->oldlenp, j);
2493 	return (0);
2494 }
2495 
2496 int
2497 freebsd32___sysctlbyname(struct thread *td,
2498     struct freebsd32___sysctlbyname_args *uap)
2499 {
2500 	size_t oldlen, rv;
2501 	int error;
2502 	uint32_t tmp;
2503 
2504 	if (uap->oldlenp != NULL) {
2505 		error = fueword32(uap->oldlenp, &tmp);
2506 		oldlen = tmp;
2507 	} else {
2508 		error = oldlen = 0;
2509 	}
2510 	if (error != 0)
2511 		return (EFAULT);
2512 	error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2513 	    &oldlen, uap->new, uap->newlen, &rv, SCTL_MASK32, 1);
2514 	if (error != 0)
2515 		return (error);
2516 	if (uap->oldlenp != NULL)
2517 		error = suword32(uap->oldlenp, rv);
2518 
2519 	return (error);
2520 }
2521 
2522 int
2523 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2524 {
2525 	uint32_t version;
2526 	int error;
2527 	struct jail j;
2528 
2529 	error = copyin(uap->jail, &version, sizeof(uint32_t));
2530 	if (error)
2531 		return (error);
2532 
2533 	switch (version) {
2534 	case 0:
2535 	{
2536 		/* FreeBSD single IPv4 jails. */
2537 		struct jail32_v0 j32_v0;
2538 
2539 		bzero(&j, sizeof(struct jail));
2540 		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2541 		if (error)
2542 			return (error);
2543 		CP(j32_v0, j, version);
2544 		PTRIN_CP(j32_v0, j, path);
2545 		PTRIN_CP(j32_v0, j, hostname);
2546 		j.ip4s = htonl(j32_v0.ip_number);	/* jail_v0 is host order */
2547 		break;
2548 	}
2549 
2550 	case 1:
2551 		/*
2552 		 * Version 1 was used by multi-IPv4 jail implementations
2553 		 * that never made it into the official kernel.
2554 		 */
2555 		return (EINVAL);
2556 
2557 	case 2:	/* JAIL_API_VERSION */
2558 	{
2559 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
2560 		struct jail32 j32;
2561 
2562 		error = copyin(uap->jail, &j32, sizeof(struct jail32));
2563 		if (error)
2564 			return (error);
2565 		CP(j32, j, version);
2566 		PTRIN_CP(j32, j, path);
2567 		PTRIN_CP(j32, j, hostname);
2568 		PTRIN_CP(j32, j, jailname);
2569 		CP(j32, j, ip4s);
2570 		CP(j32, j, ip6s);
2571 		PTRIN_CP(j32, j, ip4);
2572 		PTRIN_CP(j32, j, ip6);
2573 		break;
2574 	}
2575 
2576 	default:
2577 		/* Sci-Fi jails are not supported, sorry. */
2578 		return (EINVAL);
2579 	}
2580 	return (kern_jail(td, &j));
2581 }
2582 
2583 int
2584 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2585 {
2586 	struct uio *auio;
2587 	int error;
2588 
2589 	/* Check that we have an even number of iovecs. */
2590 	if (uap->iovcnt & 1)
2591 		return (EINVAL);
2592 
2593 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2594 	if (error)
2595 		return (error);
2596 	error = kern_jail_set(td, auio, uap->flags);
2597 	free(auio, M_IOV);
2598 	return (error);
2599 }
2600 
2601 int
2602 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2603 {
2604 	struct iovec32 iov32;
2605 	struct uio *auio;
2606 	int error, i;
2607 
2608 	/* Check that we have an even number of iovecs. */
2609 	if (uap->iovcnt & 1)
2610 		return (EINVAL);
2611 
2612 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2613 	if (error)
2614 		return (error);
2615 	error = kern_jail_get(td, auio, uap->flags);
2616 	if (error == 0)
2617 		for (i = 0; i < uap->iovcnt; i++) {
2618 			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2619 			CP(auio->uio_iov[i], iov32, iov_len);
2620 			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2621 			if (error != 0)
2622 				break;
2623 		}
2624 	free(auio, M_IOV);
2625 	return (error);
2626 }
2627 
2628 int
2629 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2630 {
2631 	struct sigaction32 s32;
2632 	struct sigaction sa, osa, *sap;
2633 	int error;
2634 
2635 	if (uap->act) {
2636 		error = copyin(uap->act, &s32, sizeof(s32));
2637 		if (error)
2638 			return (error);
2639 		sa.sa_handler = PTRIN(s32.sa_u);
2640 		CP(s32, sa, sa_flags);
2641 		CP(s32, sa, sa_mask);
2642 		sap = &sa;
2643 	} else
2644 		sap = NULL;
2645 	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2646 	if (error == 0 && uap->oact != NULL) {
2647 		s32.sa_u = PTROUT(osa.sa_handler);
2648 		CP(osa, s32, sa_flags);
2649 		CP(osa, s32, sa_mask);
2650 		error = copyout(&s32, uap->oact, sizeof(s32));
2651 	}
2652 	return (error);
2653 }
2654 
2655 #ifdef COMPAT_FREEBSD4
2656 int
2657 freebsd4_freebsd32_sigaction(struct thread *td,
2658 			     struct freebsd4_freebsd32_sigaction_args *uap)
2659 {
2660 	struct sigaction32 s32;
2661 	struct sigaction sa, osa, *sap;
2662 	int error;
2663 
2664 	if (uap->act) {
2665 		error = copyin(uap->act, &s32, sizeof(s32));
2666 		if (error)
2667 			return (error);
2668 		sa.sa_handler = PTRIN(s32.sa_u);
2669 		CP(s32, sa, sa_flags);
2670 		CP(s32, sa, sa_mask);
2671 		sap = &sa;
2672 	} else
2673 		sap = NULL;
2674 	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2675 	if (error == 0 && uap->oact != NULL) {
2676 		s32.sa_u = PTROUT(osa.sa_handler);
2677 		CP(osa, s32, sa_flags);
2678 		CP(osa, s32, sa_mask);
2679 		error = copyout(&s32, uap->oact, sizeof(s32));
2680 	}
2681 	return (error);
2682 }
2683 #endif
2684 
2685 #ifdef COMPAT_43
2686 struct osigaction32 {
2687 	u_int32_t	sa_u;
2688 	osigset_t	sa_mask;
2689 	int		sa_flags;
2690 };
2691 
2692 #define	ONSIG	32
2693 
2694 int
2695 ofreebsd32_sigaction(struct thread *td,
2696 			     struct ofreebsd32_sigaction_args *uap)
2697 {
2698 	struct osigaction32 s32;
2699 	struct sigaction sa, osa, *sap;
2700 	int error;
2701 
2702 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2703 		return (EINVAL);
2704 
2705 	if (uap->nsa) {
2706 		error = copyin(uap->nsa, &s32, sizeof(s32));
2707 		if (error)
2708 			return (error);
2709 		sa.sa_handler = PTRIN(s32.sa_u);
2710 		CP(s32, sa, sa_flags);
2711 		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2712 		sap = &sa;
2713 	} else
2714 		sap = NULL;
2715 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2716 	if (error == 0 && uap->osa != NULL) {
2717 		s32.sa_u = PTROUT(osa.sa_handler);
2718 		CP(osa, s32, sa_flags);
2719 		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2720 		error = copyout(&s32, uap->osa, sizeof(s32));
2721 	}
2722 	return (error);
2723 }
2724 
2725 int
2726 ofreebsd32_sigprocmask(struct thread *td,
2727 			       struct ofreebsd32_sigprocmask_args *uap)
2728 {
2729 	sigset_t set, oset;
2730 	int error;
2731 
2732 	OSIG2SIG(uap->mask, set);
2733 	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2734 	SIG2OSIG(oset, td->td_retval[0]);
2735 	return (error);
2736 }
2737 
2738 int
2739 ofreebsd32_sigpending(struct thread *td,
2740 			      struct ofreebsd32_sigpending_args *uap)
2741 {
2742 	struct proc *p = td->td_proc;
2743 	sigset_t siglist;
2744 
2745 	PROC_LOCK(p);
2746 	siglist = p->p_siglist;
2747 	SIGSETOR(siglist, td->td_siglist);
2748 	PROC_UNLOCK(p);
2749 	SIG2OSIG(siglist, td->td_retval[0]);
2750 	return (0);
2751 }
2752 
2753 struct sigvec32 {
2754 	u_int32_t	sv_handler;
2755 	int		sv_mask;
2756 	int		sv_flags;
2757 };
2758 
2759 int
2760 ofreebsd32_sigvec(struct thread *td,
2761 			  struct ofreebsd32_sigvec_args *uap)
2762 {
2763 	struct sigvec32 vec;
2764 	struct sigaction sa, osa, *sap;
2765 	int error;
2766 
2767 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2768 		return (EINVAL);
2769 
2770 	if (uap->nsv) {
2771 		error = copyin(uap->nsv, &vec, sizeof(vec));
2772 		if (error)
2773 			return (error);
2774 		sa.sa_handler = PTRIN(vec.sv_handler);
2775 		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2776 		sa.sa_flags = vec.sv_flags;
2777 		sa.sa_flags ^= SA_RESTART;
2778 		sap = &sa;
2779 	} else
2780 		sap = NULL;
2781 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2782 	if (error == 0 && uap->osv != NULL) {
2783 		vec.sv_handler = PTROUT(osa.sa_handler);
2784 		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2785 		vec.sv_flags = osa.sa_flags;
2786 		vec.sv_flags &= ~SA_NOCLDWAIT;
2787 		vec.sv_flags ^= SA_RESTART;
2788 		error = copyout(&vec, uap->osv, sizeof(vec));
2789 	}
2790 	return (error);
2791 }
2792 
2793 int
2794 ofreebsd32_sigblock(struct thread *td,
2795 			    struct ofreebsd32_sigblock_args *uap)
2796 {
2797 	sigset_t set, oset;
2798 
2799 	OSIG2SIG(uap->mask, set);
2800 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2801 	SIG2OSIG(oset, td->td_retval[0]);
2802 	return (0);
2803 }
2804 
2805 int
2806 ofreebsd32_sigsetmask(struct thread *td,
2807 			      struct ofreebsd32_sigsetmask_args *uap)
2808 {
2809 	sigset_t set, oset;
2810 
2811 	OSIG2SIG(uap->mask, set);
2812 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2813 	SIG2OSIG(oset, td->td_retval[0]);
2814 	return (0);
2815 }
2816 
2817 int
2818 ofreebsd32_sigsuspend(struct thread *td,
2819 			      struct ofreebsd32_sigsuspend_args *uap)
2820 {
2821 	sigset_t mask;
2822 
2823 	OSIG2SIG(uap->mask, mask);
2824 	return (kern_sigsuspend(td, mask));
2825 }
2826 
2827 struct sigstack32 {
2828 	u_int32_t	ss_sp;
2829 	int		ss_onstack;
2830 };
2831 
2832 int
2833 ofreebsd32_sigstack(struct thread *td,
2834 			    struct ofreebsd32_sigstack_args *uap)
2835 {
2836 	struct sigstack32 s32;
2837 	struct sigstack nss, oss;
2838 	int error = 0, unss;
2839 
2840 	if (uap->nss != NULL) {
2841 		error = copyin(uap->nss, &s32, sizeof(s32));
2842 		if (error)
2843 			return (error);
2844 		nss.ss_sp = PTRIN(s32.ss_sp);
2845 		CP(s32, nss, ss_onstack);
2846 		unss = 1;
2847 	} else {
2848 		unss = 0;
2849 	}
2850 	oss.ss_sp = td->td_sigstk.ss_sp;
2851 	oss.ss_onstack = sigonstack(cpu_getstack(td));
2852 	if (unss) {
2853 		td->td_sigstk.ss_sp = nss.ss_sp;
2854 		td->td_sigstk.ss_size = 0;
2855 		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2856 		td->td_pflags |= TDP_ALTSTACK;
2857 	}
2858 	if (uap->oss != NULL) {
2859 		s32.ss_sp = PTROUT(oss.ss_sp);
2860 		CP(oss, s32, ss_onstack);
2861 		error = copyout(&s32, uap->oss, sizeof(s32));
2862 	}
2863 	return (error);
2864 }
2865 #endif
2866 
2867 int
2868 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2869 {
2870 
2871 	return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2872 	    TIMER_RELTIME, uap->rqtp, uap->rmtp));
2873 }
2874 
2875 int
2876 freebsd32_clock_nanosleep(struct thread *td,
2877     struct freebsd32_clock_nanosleep_args *uap)
2878 {
2879 	int error;
2880 
2881 	error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2882 	    uap->rqtp, uap->rmtp);
2883 	return (kern_posix_error(td, error));
2884 }
2885 
2886 static int
2887 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2888     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2889 {
2890 	struct timespec32 rmt32, rqt32;
2891 	struct timespec rmt, rqt;
2892 	int error, error2;
2893 
2894 	error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2895 	if (error)
2896 		return (error);
2897 
2898 	CP(rqt32, rqt, tv_sec);
2899 	CP(rqt32, rqt, tv_nsec);
2900 
2901 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2902 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2903 		CP(rmt, rmt32, tv_sec);
2904 		CP(rmt, rmt32, tv_nsec);
2905 
2906 		error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2907 		if (error2 != 0)
2908 			error = error2;
2909 	}
2910 	return (error);
2911 }
2912 
2913 int
2914 freebsd32_clock_gettime(struct thread *td,
2915 			struct freebsd32_clock_gettime_args *uap)
2916 {
2917 	struct timespec	ats;
2918 	struct timespec32 ats32;
2919 	int error;
2920 
2921 	error = kern_clock_gettime(td, uap->clock_id, &ats);
2922 	if (error == 0) {
2923 		CP(ats, ats32, tv_sec);
2924 		CP(ats, ats32, tv_nsec);
2925 		error = copyout(&ats32, uap->tp, sizeof(ats32));
2926 	}
2927 	return (error);
2928 }
2929 
2930 int
2931 freebsd32_clock_settime(struct thread *td,
2932 			struct freebsd32_clock_settime_args *uap)
2933 {
2934 	struct timespec	ats;
2935 	struct timespec32 ats32;
2936 	int error;
2937 
2938 	error = copyin(uap->tp, &ats32, sizeof(ats32));
2939 	if (error)
2940 		return (error);
2941 	CP(ats32, ats, tv_sec);
2942 	CP(ats32, ats, tv_nsec);
2943 
2944 	return (kern_clock_settime(td, uap->clock_id, &ats));
2945 }
2946 
2947 int
2948 freebsd32_clock_getres(struct thread *td,
2949 		       struct freebsd32_clock_getres_args *uap)
2950 {
2951 	struct timespec	ts;
2952 	struct timespec32 ts32;
2953 	int error;
2954 
2955 	if (uap->tp == NULL)
2956 		return (0);
2957 	error = kern_clock_getres(td, uap->clock_id, &ts);
2958 	if (error == 0) {
2959 		CP(ts, ts32, tv_sec);
2960 		CP(ts, ts32, tv_nsec);
2961 		error = copyout(&ts32, uap->tp, sizeof(ts32));
2962 	}
2963 	return (error);
2964 }
2965 
2966 int freebsd32_ktimer_create(struct thread *td,
2967     struct freebsd32_ktimer_create_args *uap)
2968 {
2969 	struct sigevent32 ev32;
2970 	struct sigevent ev, *evp;
2971 	int error, id;
2972 
2973 	if (uap->evp == NULL) {
2974 		evp = NULL;
2975 	} else {
2976 		evp = &ev;
2977 		error = copyin(uap->evp, &ev32, sizeof(ev32));
2978 		if (error != 0)
2979 			return (error);
2980 		error = convert_sigevent32(&ev32, &ev);
2981 		if (error != 0)
2982 			return (error);
2983 	}
2984 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2985 	if (error == 0) {
2986 		error = copyout(&id, uap->timerid, sizeof(int));
2987 		if (error != 0)
2988 			kern_ktimer_delete(td, id);
2989 	}
2990 	return (error);
2991 }
2992 
2993 int
2994 freebsd32_ktimer_settime(struct thread *td,
2995     struct freebsd32_ktimer_settime_args *uap)
2996 {
2997 	struct itimerspec32 val32, oval32;
2998 	struct itimerspec val, oval, *ovalp;
2999 	int error;
3000 
3001 	error = copyin(uap->value, &val32, sizeof(val32));
3002 	if (error != 0)
3003 		return (error);
3004 	ITS_CP(val32, val);
3005 	ovalp = uap->ovalue != NULL ? &oval : NULL;
3006 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
3007 	if (error == 0 && uap->ovalue != NULL) {
3008 		ITS_CP(oval, oval32);
3009 		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
3010 	}
3011 	return (error);
3012 }
3013 
3014 int
3015 freebsd32_ktimer_gettime(struct thread *td,
3016     struct freebsd32_ktimer_gettime_args *uap)
3017 {
3018 	struct itimerspec32 val32;
3019 	struct itimerspec val;
3020 	int error;
3021 
3022 	error = kern_ktimer_gettime(td, uap->timerid, &val);
3023 	if (error == 0) {
3024 		ITS_CP(val, val32);
3025 		error = copyout(&val32, uap->value, sizeof(val32));
3026 	}
3027 	return (error);
3028 }
3029 
3030 int
3031 freebsd32_clock_getcpuclockid2(struct thread *td,
3032     struct freebsd32_clock_getcpuclockid2_args *uap)
3033 {
3034 	clockid_t clk_id;
3035 	int error;
3036 
3037 	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
3038 	    uap->which, &clk_id);
3039 	if (error == 0)
3040 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
3041 	return (error);
3042 }
3043 
3044 int
3045 freebsd32_thr_new(struct thread *td,
3046 		  struct freebsd32_thr_new_args *uap)
3047 {
3048 	struct thr_param32 param32;
3049 	struct thr_param param;
3050 	int error;
3051 
3052 	if (uap->param_size < 0 ||
3053 	    uap->param_size > sizeof(struct thr_param32))
3054 		return (EINVAL);
3055 	bzero(&param, sizeof(struct thr_param));
3056 	bzero(&param32, sizeof(struct thr_param32));
3057 	error = copyin(uap->param, &param32, uap->param_size);
3058 	if (error != 0)
3059 		return (error);
3060 	param.start_func = PTRIN(param32.start_func);
3061 	param.arg = PTRIN(param32.arg);
3062 	param.stack_base = PTRIN(param32.stack_base);
3063 	param.stack_size = param32.stack_size;
3064 	param.tls_base = PTRIN(param32.tls_base);
3065 	param.tls_size = param32.tls_size;
3066 	param.child_tid = PTRIN(param32.child_tid);
3067 	param.parent_tid = PTRIN(param32.parent_tid);
3068 	param.flags = param32.flags;
3069 	param.rtp = PTRIN(param32.rtp);
3070 	param.spare[0] = PTRIN(param32.spare[0]);
3071 	param.spare[1] = PTRIN(param32.spare[1]);
3072 	param.spare[2] = PTRIN(param32.spare[2]);
3073 
3074 	return (kern_thr_new(td, &param));
3075 }
3076 
3077 int
3078 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
3079 {
3080 	struct timespec32 ts32;
3081 	struct timespec ts, *tsp;
3082 	int error;
3083 
3084 	error = 0;
3085 	tsp = NULL;
3086 	if (uap->timeout != NULL) {
3087 		error = copyin((const void *)uap->timeout, (void *)&ts32,
3088 		    sizeof(struct timespec32));
3089 		if (error != 0)
3090 			return (error);
3091 		ts.tv_sec = ts32.tv_sec;
3092 		ts.tv_nsec = ts32.tv_nsec;
3093 		tsp = &ts;
3094 	}
3095 	return (kern_thr_suspend(td, tsp));
3096 }
3097 
3098 void
3099 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
3100 {
3101 	bzero(dst, sizeof(*dst));
3102 	dst->si_signo = src->si_signo;
3103 	dst->si_errno = src->si_errno;
3104 	dst->si_code = src->si_code;
3105 	dst->si_pid = src->si_pid;
3106 	dst->si_uid = src->si_uid;
3107 	dst->si_status = src->si_status;
3108 	dst->si_addr = (uintptr_t)src->si_addr;
3109 	dst->si_value.sival_int = src->si_value.sival_int;
3110 	dst->si_timerid = src->si_timerid;
3111 	dst->si_overrun = src->si_overrun;
3112 }
3113 
3114 #ifndef _FREEBSD32_SYSPROTO_H_
3115 struct freebsd32_sigqueue_args {
3116         pid_t pid;
3117         int signum;
3118         /* union sigval32 */ int value;
3119 };
3120 #endif
3121 int
3122 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
3123 {
3124 	union sigval sv;
3125 
3126 	/*
3127 	 * On 32-bit ABIs, sival_int and sival_ptr are the same.
3128 	 * On 64-bit little-endian ABIs, the low bits are the same.
3129 	 * In 64-bit big-endian ABIs, sival_int overlaps with
3130 	 * sival_ptr's HIGH bits.  We choose to support sival_int
3131 	 * rather than sival_ptr in this case as it seems to be
3132 	 * more common.
3133 	 */
3134 	bzero(&sv, sizeof(sv));
3135 	sv.sival_int = uap->value;
3136 
3137 	return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
3138 }
3139 
3140 int
3141 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
3142 {
3143 	struct timespec32 ts32;
3144 	struct timespec ts;
3145 	struct timespec *timeout;
3146 	sigset_t set;
3147 	ksiginfo_t ksi;
3148 	struct siginfo32 si32;
3149 	int error;
3150 
3151 	if (uap->timeout) {
3152 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
3153 		if (error)
3154 			return (error);
3155 		ts.tv_sec = ts32.tv_sec;
3156 		ts.tv_nsec = ts32.tv_nsec;
3157 		timeout = &ts;
3158 	} else
3159 		timeout = NULL;
3160 
3161 	error = copyin(uap->set, &set, sizeof(set));
3162 	if (error)
3163 		return (error);
3164 
3165 	error = kern_sigtimedwait(td, set, &ksi, timeout);
3166 	if (error)
3167 		return (error);
3168 
3169 	if (uap->info) {
3170 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
3171 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
3172 	}
3173 
3174 	if (error == 0)
3175 		td->td_retval[0] = ksi.ksi_signo;
3176 	return (error);
3177 }
3178 
3179 /*
3180  * MPSAFE
3181  */
3182 int
3183 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
3184 {
3185 	ksiginfo_t ksi;
3186 	struct siginfo32 si32;
3187 	sigset_t set;
3188 	int error;
3189 
3190 	error = copyin(uap->set, &set, sizeof(set));
3191 	if (error)
3192 		return (error);
3193 
3194 	error = kern_sigtimedwait(td, set, &ksi, NULL);
3195 	if (error)
3196 		return (error);
3197 
3198 	if (uap->info) {
3199 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
3200 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
3201 	}
3202 	if (error == 0)
3203 		td->td_retval[0] = ksi.ksi_signo;
3204 	return (error);
3205 }
3206 
3207 int
3208 freebsd32_cpuset_setid(struct thread *td,
3209     struct freebsd32_cpuset_setid_args *uap)
3210 {
3211 
3212 	return (kern_cpuset_setid(td, uap->which,
3213 	    PAIR32TO64(id_t, uap->id), uap->setid));
3214 }
3215 
3216 int
3217 freebsd32_cpuset_getid(struct thread *td,
3218     struct freebsd32_cpuset_getid_args *uap)
3219 {
3220 
3221 	return (kern_cpuset_getid(td, uap->level, uap->which,
3222 	    PAIR32TO64(id_t, uap->id), uap->setid));
3223 }
3224 
3225 int
3226 freebsd32_cpuset_getaffinity(struct thread *td,
3227     struct freebsd32_cpuset_getaffinity_args *uap)
3228 {
3229 
3230 	return (kern_cpuset_getaffinity(td, uap->level, uap->which,
3231 	    PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3232 }
3233 
3234 int
3235 freebsd32_cpuset_setaffinity(struct thread *td,
3236     struct freebsd32_cpuset_setaffinity_args *uap)
3237 {
3238 
3239 	return (kern_cpuset_setaffinity(td, uap->level, uap->which,
3240 	    PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3241 }
3242 
3243 int
3244 freebsd32_cpuset_getdomain(struct thread *td,
3245     struct freebsd32_cpuset_getdomain_args *uap)
3246 {
3247 
3248 	return (kern_cpuset_getdomain(td, uap->level, uap->which,
3249 	    PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3250 }
3251 
3252 int
3253 freebsd32_cpuset_setdomain(struct thread *td,
3254     struct freebsd32_cpuset_setdomain_args *uap)
3255 {
3256 
3257 	return (kern_cpuset_setdomain(td, uap->level, uap->which,
3258 	    PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3259 }
3260 
3261 int
3262 freebsd32_nmount(struct thread *td,
3263     struct freebsd32_nmount_args /* {
3264     	struct iovec *iovp;
3265     	unsigned int iovcnt;
3266     	int flags;
3267     } */ *uap)
3268 {
3269 	struct uio *auio;
3270 	uint64_t flags;
3271 	int error;
3272 
3273 	/*
3274 	 * Mount flags are now 64-bits. On 32-bit archtectures only
3275 	 * 32-bits are passed in, but from here on everything handles
3276 	 * 64-bit flags correctly.
3277 	 */
3278 	flags = uap->flags;
3279 
3280 	AUDIT_ARG_FFLAGS(flags);
3281 
3282 	/*
3283 	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
3284 	 * userspace to set this flag, but we must filter it out if we want
3285 	 * MNT_UPDATE on the root file system to work.
3286 	 * MNT_ROOTFS should only be set by the kernel when mounting its
3287 	 * root file system.
3288 	 */
3289 	flags &= ~MNT_ROOTFS;
3290 
3291 	/*
3292 	 * check that we have an even number of iovec's
3293 	 * and that we have at least two options.
3294 	 */
3295 	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3296 		return (EINVAL);
3297 
3298 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3299 	if (error)
3300 		return (error);
3301 	error = vfs_donmount(td, flags, auio);
3302 
3303 	free(auio, M_IOV);
3304 	return error;
3305 }
3306 
3307 #if 0
3308 int
3309 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3310 {
3311 	struct yyy32 *p32, s32;
3312 	struct yyy *p = NULL, s;
3313 	struct xxx_arg ap;
3314 	int error;
3315 
3316 	if (uap->zzz) {
3317 		error = copyin(uap->zzz, &s32, sizeof(s32));
3318 		if (error)
3319 			return (error);
3320 		/* translate in */
3321 		p = &s;
3322 	}
3323 	error = kern_xxx(td, p);
3324 	if (error)
3325 		return (error);
3326 	if (uap->zzz) {
3327 		/* translate out */
3328 		error = copyout(&s32, p32, sizeof(s32));
3329 	}
3330 	return (error);
3331 }
3332 #endif
3333 
3334 int
3335 syscall32_module_handler(struct module *mod, int what, void *arg)
3336 {
3337 
3338 	return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3339 }
3340 
3341 int
3342 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3343 {
3344 
3345 	return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3346 }
3347 
3348 int
3349 syscall32_helper_unregister(struct syscall_helper_data *sd)
3350 {
3351 
3352 	return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3353 }
3354 
3355 int
3356 freebsd32_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
3357 {
3358 	int argc, envc, i;
3359 	u_int32_t *vectp;
3360 	char *stringp;
3361 	uintptr_t destp, ustringp;
3362 	struct freebsd32_ps_strings *arginfo;
3363 	char canary[sizeof(long) * 8];
3364 	int32_t pagesizes32[MAXPAGESIZES];
3365 	size_t execpath_len;
3366 	int error, szsigcode;
3367 
3368 	/*
3369 	 * Calculate string base and vector table pointers.
3370 	 * Also deal with signal trampoline code for this exec type.
3371 	 */
3372 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
3373 		execpath_len = strlen(imgp->execpath) + 1;
3374 	else
3375 		execpath_len = 0;
3376 	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3377 	    sv_psstrings;
3378 	imgp->ps_strings = arginfo;
3379 	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3380 		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3381 	else
3382 		szsigcode = 0;
3383 	destp =	(uintptr_t)arginfo;
3384 
3385 	/*
3386 	 * install sigcode
3387 	 */
3388 	if (szsigcode != 0) {
3389 		destp -= szsigcode;
3390 		destp = rounddown2(destp, sizeof(uint32_t));
3391 		error = copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3392 		    szsigcode);
3393 		if (error != 0)
3394 			return (error);
3395 	}
3396 
3397 	/*
3398 	 * Copy the image path for the rtld.
3399 	 */
3400 	if (execpath_len != 0) {
3401 		destp -= execpath_len;
3402 		imgp->execpathp = (void *)destp;
3403 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
3404 		if (error != 0)
3405 			return (error);
3406 	}
3407 
3408 	/*
3409 	 * Prepare the canary for SSP.
3410 	 */
3411 	arc4rand(canary, sizeof(canary), 0);
3412 	destp -= sizeof(canary);
3413 	imgp->canary = (void *)destp;
3414 	error = copyout(canary, imgp->canary, sizeof(canary));
3415 	if (error != 0)
3416 		return (error);
3417 	imgp->canarylen = sizeof(canary);
3418 
3419 	/*
3420 	 * Prepare the pagesizes array.
3421 	 */
3422 	for (i = 0; i < MAXPAGESIZES; i++)
3423 		pagesizes32[i] = (uint32_t)pagesizes[i];
3424 	destp -= sizeof(pagesizes32);
3425 	destp = rounddown2(destp, sizeof(uint32_t));
3426 	imgp->pagesizes = (void *)destp;
3427 	error = copyout(pagesizes32, imgp->pagesizes, sizeof(pagesizes32));
3428 	if (error != 0)
3429 		return (error);
3430 	imgp->pagesizeslen = sizeof(pagesizes32);
3431 
3432 	/*
3433 	 * Allocate room for the argument and environment strings.
3434 	 */
3435 	destp -= ARG_MAX - imgp->args->stringspace;
3436 	destp = rounddown2(destp, sizeof(uint32_t));
3437 	ustringp = destp;
3438 
3439 	exec_stackgap(imgp, &destp);
3440 
3441 	if (imgp->auxargs) {
3442 		/*
3443 		 * Allocate room on the stack for the ELF auxargs
3444 		 * array.  It has up to AT_COUNT entries.
3445 		 */
3446 		destp -= AT_COUNT * sizeof(Elf32_Auxinfo);
3447 		destp = rounddown2(destp, sizeof(uint32_t));
3448 	}
3449 
3450 	vectp = (uint32_t *)destp;
3451 
3452 	/*
3453 	 * Allocate room for the argv[] and env vectors including the
3454 	 * terminating NULL pointers.
3455 	 */
3456 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3457 
3458 	/*
3459 	 * vectp also becomes our initial stack base
3460 	 */
3461 	*stack_base = (uintptr_t)vectp;
3462 
3463 	stringp = imgp->args->begin_argv;
3464 	argc = imgp->args->argc;
3465 	envc = imgp->args->envc;
3466 	/*
3467 	 * Copy out strings - arguments and environment.
3468 	 */
3469 	error = copyout(stringp, (void *)ustringp,
3470 	    ARG_MAX - imgp->args->stringspace);
3471 	if (error != 0)
3472 		return (error);
3473 
3474 	/*
3475 	 * Fill in "ps_strings" struct for ps, w, etc.
3476 	 */
3477 	imgp->argv = vectp;
3478 	if (suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp) != 0 ||
3479 	    suword32(&arginfo->ps_nargvstr, argc) != 0)
3480 		return (EFAULT);
3481 
3482 	/*
3483 	 * Fill in argument portion of vector table.
3484 	 */
3485 	for (; argc > 0; --argc) {
3486 		if (suword32(vectp++, ustringp) != 0)
3487 			return (EFAULT);
3488 		while (*stringp++ != 0)
3489 			ustringp++;
3490 		ustringp++;
3491 	}
3492 
3493 	/* a null vector table pointer separates the argp's from the envp's */
3494 	if (suword32(vectp++, 0) != 0)
3495 		return (EFAULT);
3496 
3497 	imgp->envv = vectp;
3498 	if (suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp) != 0 ||
3499 	    suword32(&arginfo->ps_nenvstr, envc) != 0)
3500 		return (EFAULT);
3501 
3502 	/*
3503 	 * Fill in environment portion of vector table.
3504 	 */
3505 	for (; envc > 0; --envc) {
3506 		if (suword32(vectp++, ustringp) != 0)
3507 			return (EFAULT);
3508 		while (*stringp++ != 0)
3509 			ustringp++;
3510 		ustringp++;
3511 	}
3512 
3513 	/* end of vector table is a null pointer */
3514 	if (suword32(vectp, 0) != 0)
3515 		return (EFAULT);
3516 
3517 	if (imgp->auxargs) {
3518 		vectp++;
3519 		error = imgp->sysent->sv_copyout_auxargs(imgp,
3520 		    (uintptr_t)vectp);
3521 		if (error != 0)
3522 			return (error);
3523 	}
3524 
3525 	return (0);
3526 }
3527 
3528 int
3529 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3530 {
3531 	struct kld_file_stat *stat;
3532 	struct kld32_file_stat *stat32;
3533 	int error, version;
3534 
3535 	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3536 	    != 0)
3537 		return (error);
3538 	if (version != sizeof(struct kld32_file_stat_1) &&
3539 	    version != sizeof(struct kld32_file_stat))
3540 		return (EINVAL);
3541 
3542 	stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3543 	stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3544 	error = kern_kldstat(td, uap->fileid, stat);
3545 	if (error == 0) {
3546 		bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3547 		CP(*stat, *stat32, refs);
3548 		CP(*stat, *stat32, id);
3549 		PTROUT_CP(*stat, *stat32, address);
3550 		CP(*stat, *stat32, size);
3551 		bcopy(&stat->pathname[0], &stat32->pathname[0],
3552 		    sizeof(stat->pathname));
3553 		stat32->version  = version;
3554 		error = copyout(stat32, uap->stat, version);
3555 	}
3556 	free(stat, M_TEMP);
3557 	free(stat32, M_TEMP);
3558 	return (error);
3559 }
3560 
3561 int
3562 freebsd32_posix_fallocate(struct thread *td,
3563     struct freebsd32_posix_fallocate_args *uap)
3564 {
3565 	int error;
3566 
3567 	error = kern_posix_fallocate(td, uap->fd,
3568 	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3569 	return (kern_posix_error(td, error));
3570 }
3571 
3572 int
3573 freebsd32_posix_fadvise(struct thread *td,
3574     struct freebsd32_posix_fadvise_args *uap)
3575 {
3576 	int error;
3577 
3578 	error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3579 	    PAIR32TO64(off_t, uap->len), uap->advice);
3580 	return (kern_posix_error(td, error));
3581 }
3582 
3583 int
3584 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3585 {
3586 
3587 	CP(*sig32, *sig, sigev_notify);
3588 	switch (sig->sigev_notify) {
3589 	case SIGEV_NONE:
3590 		break;
3591 	case SIGEV_THREAD_ID:
3592 		CP(*sig32, *sig, sigev_notify_thread_id);
3593 		/* FALLTHROUGH */
3594 	case SIGEV_SIGNAL:
3595 		CP(*sig32, *sig, sigev_signo);
3596 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3597 		break;
3598 	case SIGEV_KEVENT:
3599 		CP(*sig32, *sig, sigev_notify_kqueue);
3600 		CP(*sig32, *sig, sigev_notify_kevent_flags);
3601 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3602 		break;
3603 	default:
3604 		return (EINVAL);
3605 	}
3606 	return (0);
3607 }
3608 
3609 int
3610 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3611 {
3612 	void *data;
3613 	union {
3614 		struct procctl_reaper_status rs;
3615 		struct procctl_reaper_pids rp;
3616 		struct procctl_reaper_kill rk;
3617 	} x;
3618 	union {
3619 		struct procctl_reaper_pids32 rp;
3620 	} x32;
3621 	int error, error1, flags, signum;
3622 
3623 	if (uap->com >= PROC_PROCCTL_MD_MIN)
3624 		return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3625 		    uap->com, PTRIN(uap->data)));
3626 
3627 	switch (uap->com) {
3628 	case PROC_ASLR_CTL:
3629 	case PROC_PROTMAX_CTL:
3630 	case PROC_SPROTECT:
3631 	case PROC_STACKGAP_CTL:
3632 	case PROC_TRACE_CTL:
3633 	case PROC_TRAPCAP_CTL:
3634 		error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3635 		if (error != 0)
3636 			return (error);
3637 		data = &flags;
3638 		break;
3639 	case PROC_REAP_ACQUIRE:
3640 	case PROC_REAP_RELEASE:
3641 		if (uap->data != NULL)
3642 			return (EINVAL);
3643 		data = NULL;
3644 		break;
3645 	case PROC_REAP_STATUS:
3646 		data = &x.rs;
3647 		break;
3648 	case PROC_REAP_GETPIDS:
3649 		error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3650 		if (error != 0)
3651 			return (error);
3652 		CP(x32.rp, x.rp, rp_count);
3653 		PTRIN_CP(x32.rp, x.rp, rp_pids);
3654 		data = &x.rp;
3655 		break;
3656 	case PROC_REAP_KILL:
3657 		error = copyin(uap->data, &x.rk, sizeof(x.rk));
3658 		if (error != 0)
3659 			return (error);
3660 		data = &x.rk;
3661 		break;
3662 	case PROC_ASLR_STATUS:
3663 	case PROC_PROTMAX_STATUS:
3664 	case PROC_STACKGAP_STATUS:
3665 	case PROC_TRACE_STATUS:
3666 	case PROC_TRAPCAP_STATUS:
3667 		data = &flags;
3668 		break;
3669 	case PROC_PDEATHSIG_CTL:
3670 		error = copyin(uap->data, &signum, sizeof(signum));
3671 		if (error != 0)
3672 			return (error);
3673 		data = &signum;
3674 		break;
3675 	case PROC_PDEATHSIG_STATUS:
3676 		data = &signum;
3677 		break;
3678 	default:
3679 		return (EINVAL);
3680 	}
3681 	error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3682 	    uap->com, data);
3683 	switch (uap->com) {
3684 	case PROC_REAP_STATUS:
3685 		if (error == 0)
3686 			error = copyout(&x.rs, uap->data, sizeof(x.rs));
3687 		break;
3688 	case PROC_REAP_KILL:
3689 		error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3690 		if (error == 0)
3691 			error = error1;
3692 		break;
3693 	case PROC_ASLR_STATUS:
3694 	case PROC_PROTMAX_STATUS:
3695 	case PROC_STACKGAP_STATUS:
3696 	case PROC_TRACE_STATUS:
3697 	case PROC_TRAPCAP_STATUS:
3698 		if (error == 0)
3699 			error = copyout(&flags, uap->data, sizeof(flags));
3700 		break;
3701 	case PROC_PDEATHSIG_STATUS:
3702 		if (error == 0)
3703 			error = copyout(&signum, uap->data, sizeof(signum));
3704 		break;
3705 	}
3706 	return (error);
3707 }
3708 
3709 int
3710 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3711 {
3712 	long tmp;
3713 
3714 	switch (uap->cmd) {
3715 	/*
3716 	 * Do unsigned conversion for arg when operation
3717 	 * interprets it as flags or pointer.
3718 	 */
3719 	case F_SETLK_REMOTE:
3720 	case F_SETLKW:
3721 	case F_SETLK:
3722 	case F_GETLK:
3723 	case F_SETFD:
3724 	case F_SETFL:
3725 	case F_OGETLK:
3726 	case F_OSETLK:
3727 	case F_OSETLKW:
3728 		tmp = (unsigned int)(uap->arg);
3729 		break;
3730 	default:
3731 		tmp = uap->arg;
3732 		break;
3733 	}
3734 	return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3735 }
3736 
3737 int
3738 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3739 {
3740 	struct timespec32 ts32;
3741 	struct timespec ts, *tsp;
3742 	sigset_t set, *ssp;
3743 	int error;
3744 
3745 	if (uap->ts != NULL) {
3746 		error = copyin(uap->ts, &ts32, sizeof(ts32));
3747 		if (error != 0)
3748 			return (error);
3749 		CP(ts32, ts, tv_sec);
3750 		CP(ts32, ts, tv_nsec);
3751 		tsp = &ts;
3752 	} else
3753 		tsp = NULL;
3754 	if (uap->set != NULL) {
3755 		error = copyin(uap->set, &set, sizeof(set));
3756 		if (error != 0)
3757 			return (error);
3758 		ssp = &set;
3759 	} else
3760 		ssp = NULL;
3761 
3762 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3763 }
3764 
3765 int
3766 freebsd32_sched_rr_get_interval(struct thread *td,
3767     struct freebsd32_sched_rr_get_interval_args *uap)
3768 {
3769 	struct timespec ts;
3770 	struct timespec32 ts32;
3771 	int error;
3772 
3773 	error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3774 	if (error == 0) {
3775 		CP(ts, ts32, tv_sec);
3776 		CP(ts, ts32, tv_nsec);
3777 		error = copyout(&ts32, uap->interval, sizeof(ts32));
3778 	}
3779 	return (error);
3780 }
3781 
3782 static void
3783 timex_to_32(struct timex32 *dst, struct timex *src)
3784 {
3785 	CP(*src, *dst, modes);
3786 	CP(*src, *dst, offset);
3787 	CP(*src, *dst, freq);
3788 	CP(*src, *dst, maxerror);
3789 	CP(*src, *dst, esterror);
3790 	CP(*src, *dst, status);
3791 	CP(*src, *dst, constant);
3792 	CP(*src, *dst, precision);
3793 	CP(*src, *dst, tolerance);
3794 	CP(*src, *dst, ppsfreq);
3795 	CP(*src, *dst, jitter);
3796 	CP(*src, *dst, shift);
3797 	CP(*src, *dst, stabil);
3798 	CP(*src, *dst, jitcnt);
3799 	CP(*src, *dst, calcnt);
3800 	CP(*src, *dst, errcnt);
3801 	CP(*src, *dst, stbcnt);
3802 }
3803 
3804 static void
3805 timex_from_32(struct timex *dst, struct timex32 *src)
3806 {
3807 	CP(*src, *dst, modes);
3808 	CP(*src, *dst, offset);
3809 	CP(*src, *dst, freq);
3810 	CP(*src, *dst, maxerror);
3811 	CP(*src, *dst, esterror);
3812 	CP(*src, *dst, status);
3813 	CP(*src, *dst, constant);
3814 	CP(*src, *dst, precision);
3815 	CP(*src, *dst, tolerance);
3816 	CP(*src, *dst, ppsfreq);
3817 	CP(*src, *dst, jitter);
3818 	CP(*src, *dst, shift);
3819 	CP(*src, *dst, stabil);
3820 	CP(*src, *dst, jitcnt);
3821 	CP(*src, *dst, calcnt);
3822 	CP(*src, *dst, errcnt);
3823 	CP(*src, *dst, stbcnt);
3824 }
3825 
3826 int
3827 freebsd32_ntp_adjtime(struct thread *td, struct freebsd32_ntp_adjtime_args *uap)
3828 {
3829 	struct timex tx;
3830 	struct timex32 tx32;
3831 	int error, retval;
3832 
3833 	error = copyin(uap->tp, &tx32, sizeof(tx32));
3834 	if (error == 0) {
3835 		timex_from_32(&tx, &tx32);
3836 		error = kern_ntp_adjtime(td, &tx, &retval);
3837 		if (error == 0) {
3838 			timex_to_32(&tx32, &tx);
3839 			error = copyout(&tx32, uap->tp, sizeof(tx32));
3840 			if (error == 0)
3841 				td->td_retval[0] = retval;
3842 		}
3843 	}
3844 	return (error);
3845 }
3846