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