xref: /freebsd/sys/compat/freebsd32/freebsd32_misc.c (revision b197d4b893974c9eb4d7b38704c6d5c486235d6f)
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 			error = EINVAL;
1561 			break;
1562 		}
1563 		msglen = FREEBSD32_ALIGN(cm->cmsg_len);
1564 		if (msglen > buflen || msglen < cm->cmsg_len) {
1565 			error = EINVAL;
1566 			break;
1567 		}
1568 		buflen -= msglen;
1569 
1570 		in1 = (char *)in1 + msglen;
1571 		outlen += CMSG_ALIGN(sizeof(*cm)) +
1572 		    CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm)));
1573 	}
1574 	if (error != 0)
1575 		goto out;
1576 
1577 	/*
1578 	 * Allocate up to MJUMPAGESIZE space for the re-aligned and
1579 	 * re-padded control messages.  This allows a full MCLBYTES of
1580 	 * 32-bit sized and aligned messages to fit and avoids an ABI
1581 	 * mismatch with the native implementation.
1582 	 */
1583 	m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0);
1584 	if (m == NULL) {
1585 		error = EINVAL;
1586 		goto out;
1587 	}
1588 	m->m_len = outlen;
1589 	md = mtod(m, void *);
1590 
1591 	/*
1592 	 * Make a second pass over input messages, copying them into the output
1593 	 * buffer.
1594 	 */
1595 	in1 = in;
1596 	while (outlen > 0) {
1597 		/* Copy the message header and align the length field. */
1598 		cm = md;
1599 		memcpy(cm, in1, sizeof(*cm));
1600 		msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm));
1601 		cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen;
1602 
1603 		/* Copy the message body. */
1604 		in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm));
1605 		md = (char *)md + CMSG_ALIGN(sizeof(*cm));
1606 		memcpy(md, in1, msglen);
1607 		in1 = (char *)in1 + FREEBSD32_ALIGN(msglen);
1608 		md = (char *)md + CMSG_ALIGN(msglen);
1609 		KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen),
1610 		    ("outlen %u underflow, msglen %u", outlen, msglen));
1611 		outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen);
1612 	}
1613 
1614 	*mp = m;
1615 out:
1616 	free(in, M_TEMP);
1617 	return (error);
1618 }
1619 
1620 int
1621 freebsd32_sendmsg(struct thread *td, struct freebsd32_sendmsg_args *uap)
1622 {
1623 	struct msghdr msg;
1624 	struct iovec *iov;
1625 	struct mbuf *control = NULL;
1626 	struct sockaddr *to = NULL;
1627 	int error;
1628 
1629 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1630 	if (error)
1631 		return (error);
1632 	error = freebsd32_copyiniov((void *)msg.msg_iov, msg.msg_iovlen, &iov,
1633 	    EMSGSIZE);
1634 	if (error)
1635 		return (error);
1636 	msg.msg_iov = iov;
1637 	if (msg.msg_name != NULL) {
1638 		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1639 		if (error) {
1640 			to = NULL;
1641 			goto out;
1642 		}
1643 		msg.msg_name = to;
1644 	}
1645 
1646 	if (msg.msg_control) {
1647 		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1648 			error = EINVAL;
1649 			goto out;
1650 		}
1651 
1652 		error = freebsd32_copyin_control(&control, msg.msg_control,
1653 		    msg.msg_controllen);
1654 		if (error)
1655 			goto out;
1656 
1657 		msg.msg_control = NULL;
1658 		msg.msg_controllen = 0;
1659 	}
1660 
1661 	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1662 	    UIO_USERSPACE);
1663 
1664 out:
1665 	free(iov, M_IOV);
1666 	if (to)
1667 		free(to, M_SONAME);
1668 	return (error);
1669 }
1670 
1671 #ifdef COMPAT_43
1672 int
1673 ofreebsd32_sendmsg(struct thread *td, struct ofreebsd32_sendmsg_args *uap)
1674 {
1675 	return (ENOSYS);
1676 }
1677 #endif
1678 
1679 
1680 int
1681 freebsd32_settimeofday(struct thread *td,
1682 		       struct freebsd32_settimeofday_args *uap)
1683 {
1684 	struct timeval32 tv32;
1685 	struct timeval tv, *tvp;
1686 	struct timezone tz, *tzp;
1687 	int error;
1688 
1689 	if (uap->tv) {
1690 		error = copyin(uap->tv, &tv32, sizeof(tv32));
1691 		if (error)
1692 			return (error);
1693 		CP(tv32, tv, tv_sec);
1694 		CP(tv32, tv, tv_usec);
1695 		tvp = &tv;
1696 	} else
1697 		tvp = NULL;
1698 	if (uap->tzp) {
1699 		error = copyin(uap->tzp, &tz, sizeof(tz));
1700 		if (error)
1701 			return (error);
1702 		tzp = &tz;
1703 	} else
1704 		tzp = NULL;
1705 	return (kern_settimeofday(td, tvp, tzp));
1706 }
1707 
1708 int
1709 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1710 {
1711 	struct timeval32 s32[2];
1712 	struct timeval s[2], *sp;
1713 	int error;
1714 
1715 	if (uap->tptr != NULL) {
1716 		error = copyin(uap->tptr, s32, sizeof(s32));
1717 		if (error)
1718 			return (error);
1719 		CP(s32[0], s[0], tv_sec);
1720 		CP(s32[0], s[0], tv_usec);
1721 		CP(s32[1], s[1], tv_sec);
1722 		CP(s32[1], s[1], tv_usec);
1723 		sp = s;
1724 	} else
1725 		sp = NULL;
1726 	return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1727 	    sp, UIO_SYSSPACE));
1728 }
1729 
1730 int
1731 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1732 {
1733 	struct timeval32 s32[2];
1734 	struct timeval s[2], *sp;
1735 	int error;
1736 
1737 	if (uap->tptr != NULL) {
1738 		error = copyin(uap->tptr, s32, sizeof(s32));
1739 		if (error)
1740 			return (error);
1741 		CP(s32[0], s[0], tv_sec);
1742 		CP(s32[0], s[0], tv_usec);
1743 		CP(s32[1], s[1], tv_sec);
1744 		CP(s32[1], s[1], tv_usec);
1745 		sp = s;
1746 	} else
1747 		sp = NULL;
1748 	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1749 }
1750 
1751 int
1752 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1753 {
1754 	struct timeval32 s32[2];
1755 	struct timeval s[2], *sp;
1756 	int error;
1757 
1758 	if (uap->tptr != NULL) {
1759 		error = copyin(uap->tptr, s32, sizeof(s32));
1760 		if (error)
1761 			return (error);
1762 		CP(s32[0], s[0], tv_sec);
1763 		CP(s32[0], s[0], tv_usec);
1764 		CP(s32[1], s[1], tv_sec);
1765 		CP(s32[1], s[1], tv_usec);
1766 		sp = s;
1767 	} else
1768 		sp = NULL;
1769 	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1770 }
1771 
1772 int
1773 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1774 {
1775 	struct timeval32 s32[2];
1776 	struct timeval s[2], *sp;
1777 	int error;
1778 
1779 	if (uap->times != NULL) {
1780 		error = copyin(uap->times, s32, sizeof(s32));
1781 		if (error)
1782 			return (error);
1783 		CP(s32[0], s[0], tv_sec);
1784 		CP(s32[0], s[0], tv_usec);
1785 		CP(s32[1], s[1], tv_sec);
1786 		CP(s32[1], s[1], tv_usec);
1787 		sp = s;
1788 	} else
1789 		sp = NULL;
1790 	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1791 		sp, UIO_SYSSPACE));
1792 }
1793 
1794 int
1795 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1796 {
1797 	struct timespec32 ts32[2];
1798 	struct timespec ts[2], *tsp;
1799 	int error;
1800 
1801 	if (uap->times != NULL) {
1802 		error = copyin(uap->times, ts32, sizeof(ts32));
1803 		if (error)
1804 			return (error);
1805 		CP(ts32[0], ts[0], tv_sec);
1806 		CP(ts32[0], ts[0], tv_nsec);
1807 		CP(ts32[1], ts[1], tv_sec);
1808 		CP(ts32[1], ts[1], tv_nsec);
1809 		tsp = ts;
1810 	} else
1811 		tsp = NULL;
1812 	return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1813 }
1814 
1815 int
1816 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1817 {
1818 	struct timespec32 ts32[2];
1819 	struct timespec ts[2], *tsp;
1820 	int error;
1821 
1822 	if (uap->times != NULL) {
1823 		error = copyin(uap->times, ts32, sizeof(ts32));
1824 		if (error)
1825 			return (error);
1826 		CP(ts32[0], ts[0], tv_sec);
1827 		CP(ts32[0], ts[0], tv_nsec);
1828 		CP(ts32[1], ts[1], tv_sec);
1829 		CP(ts32[1], ts[1], tv_nsec);
1830 		tsp = ts;
1831 	} else
1832 		tsp = NULL;
1833 	return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1834 	    tsp, UIO_SYSSPACE, uap->flag));
1835 }
1836 
1837 int
1838 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1839 {
1840 	struct timeval32 tv32;
1841 	struct timeval delta, olddelta, *deltap;
1842 	int error;
1843 
1844 	if (uap->delta) {
1845 		error = copyin(uap->delta, &tv32, sizeof(tv32));
1846 		if (error)
1847 			return (error);
1848 		CP(tv32, delta, tv_sec);
1849 		CP(tv32, delta, tv_usec);
1850 		deltap = &delta;
1851 	} else
1852 		deltap = NULL;
1853 	error = kern_adjtime(td, deltap, &olddelta);
1854 	if (uap->olddelta && error == 0) {
1855 		CP(olddelta, tv32, tv_sec);
1856 		CP(olddelta, tv32, tv_usec);
1857 		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1858 	}
1859 	return (error);
1860 }
1861 
1862 #ifdef COMPAT_FREEBSD4
1863 int
1864 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1865 {
1866 	struct ostatfs32 s32;
1867 	struct statfs *sp;
1868 	int error;
1869 
1870 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1871 	error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1872 	if (error == 0) {
1873 		copy_statfs(sp, &s32);
1874 		error = copyout(&s32, uap->buf, sizeof(s32));
1875 	}
1876 	free(sp, M_STATFS);
1877 	return (error);
1878 }
1879 #endif
1880 
1881 #ifdef COMPAT_FREEBSD4
1882 int
1883 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1884 {
1885 	struct ostatfs32 s32;
1886 	struct statfs *sp;
1887 	int error;
1888 
1889 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1890 	error = kern_fstatfs(td, uap->fd, sp);
1891 	if (error == 0) {
1892 		copy_statfs(sp, &s32);
1893 		error = copyout(&s32, uap->buf, sizeof(s32));
1894 	}
1895 	free(sp, M_STATFS);
1896 	return (error);
1897 }
1898 #endif
1899 
1900 #ifdef COMPAT_FREEBSD4
1901 int
1902 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1903 {
1904 	struct ostatfs32 s32;
1905 	struct statfs *sp;
1906 	fhandle_t fh;
1907 	int error;
1908 
1909 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1910 		return (error);
1911 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1912 	error = kern_fhstatfs(td, fh, sp);
1913 	if (error == 0) {
1914 		copy_statfs(sp, &s32);
1915 		error = copyout(&s32, uap->buf, sizeof(s32));
1916 	}
1917 	free(sp, M_STATFS);
1918 	return (error);
1919 }
1920 #endif
1921 
1922 int
1923 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1924 {
1925 
1926 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1927 	    PAIR32TO64(off_t, uap->offset)));
1928 }
1929 
1930 int
1931 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1932 {
1933 
1934 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1935 	    PAIR32TO64(off_t, uap->offset)));
1936 }
1937 
1938 #ifdef COMPAT_43
1939 int
1940 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1941 {
1942 
1943 	return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1944 }
1945 #endif
1946 
1947 int
1948 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1949 {
1950 	int error;
1951 	off_t pos;
1952 
1953 	error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1954 	    uap->whence);
1955 	/* Expand the quad return into two parts for eax and edx */
1956 	pos = td->td_uretoff.tdu_off;
1957 	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1958 	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1959 	return error;
1960 }
1961 
1962 int
1963 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1964 {
1965 
1966 	return (kern_truncate(td, uap->path, UIO_USERSPACE,
1967 	    PAIR32TO64(off_t, uap->length)));
1968 }
1969 
1970 #ifdef COMPAT_43
1971 int
1972 ofreebsd32_truncate(struct thread *td, struct ofreebsd32_truncate_args *uap)
1973 {
1974 	return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
1975 }
1976 #endif
1977 
1978 int
1979 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1980 {
1981 
1982 	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1983 }
1984 
1985 #ifdef COMPAT_43
1986 int
1987 ofreebsd32_ftruncate(struct thread *td, struct ofreebsd32_ftruncate_args *uap)
1988 {
1989 	return (kern_ftruncate(td, uap->fd, uap->length));
1990 }
1991 
1992 int
1993 ofreebsd32_getdirentries(struct thread *td,
1994     struct ofreebsd32_getdirentries_args *uap)
1995 {
1996 	struct ogetdirentries_args ap;
1997 	int error;
1998 	long loff;
1999 	int32_t loff_cut;
2000 
2001 	ap.fd = uap->fd;
2002 	ap.buf = uap->buf;
2003 	ap.count = uap->count;
2004 	ap.basep = NULL;
2005 	error = kern_ogetdirentries(td, &ap, &loff);
2006 	if (error == 0) {
2007 		loff_cut = loff;
2008 		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
2009 	}
2010 	return (error);
2011 }
2012 #endif
2013 
2014 #if defined(COMPAT_FREEBSD11)
2015 int
2016 freebsd11_freebsd32_getdirentries(struct thread *td,
2017     struct freebsd11_freebsd32_getdirentries_args *uap)
2018 {
2019 	long base;
2020 	int32_t base32;
2021 	int error;
2022 
2023 	error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
2024 	    &base, NULL);
2025 	if (error)
2026 		return (error);
2027 	if (uap->basep != NULL) {
2028 		base32 = base;
2029 		error = copyout(&base32, uap->basep, sizeof(int32_t));
2030 	}
2031 	return (error);
2032 }
2033 #endif /* COMPAT_FREEBSD11 */
2034 
2035 #ifdef COMPAT_FREEBSD6
2036 /* versions with the 'int pad' argument */
2037 int
2038 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
2039 {
2040 
2041 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
2042 	    PAIR32TO64(off_t, uap->offset)));
2043 }
2044 
2045 int
2046 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
2047 {
2048 
2049 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
2050 	    PAIR32TO64(off_t, uap->offset)));
2051 }
2052 
2053 int
2054 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
2055 {
2056 	int error;
2057 	off_t pos;
2058 
2059 	error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2060 	    uap->whence);
2061 	/* Expand the quad return into two parts for eax and edx */
2062 	pos = *(off_t *)(td->td_retval);
2063 	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
2064 	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
2065 	return error;
2066 }
2067 
2068 int
2069 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
2070 {
2071 
2072 	return (kern_truncate(td, uap->path, UIO_USERSPACE,
2073 	    PAIR32TO64(off_t, uap->length)));
2074 }
2075 
2076 int
2077 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
2078 {
2079 
2080 	return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
2081 }
2082 #endif /* COMPAT_FREEBSD6 */
2083 
2084 struct sf_hdtr32 {
2085 	uint32_t headers;
2086 	int hdr_cnt;
2087 	uint32_t trailers;
2088 	int trl_cnt;
2089 };
2090 
2091 static int
2092 freebsd32_do_sendfile(struct thread *td,
2093     struct freebsd32_sendfile_args *uap, int compat)
2094 {
2095 	struct sf_hdtr32 hdtr32;
2096 	struct sf_hdtr hdtr;
2097 	struct uio *hdr_uio, *trl_uio;
2098 	struct file *fp;
2099 	cap_rights_t rights;
2100 	struct iovec32 *iov32;
2101 	off_t offset, sbytes;
2102 	int error;
2103 
2104 	offset = PAIR32TO64(off_t, uap->offset);
2105 	if (offset < 0)
2106 		return (EINVAL);
2107 
2108 	hdr_uio = trl_uio = NULL;
2109 
2110 	if (uap->hdtr != NULL) {
2111 		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
2112 		if (error)
2113 			goto out;
2114 		PTRIN_CP(hdtr32, hdtr, headers);
2115 		CP(hdtr32, hdtr, hdr_cnt);
2116 		PTRIN_CP(hdtr32, hdtr, trailers);
2117 		CP(hdtr32, hdtr, trl_cnt);
2118 
2119 		if (hdtr.headers != NULL) {
2120 			iov32 = PTRIN(hdtr32.headers);
2121 			error = freebsd32_copyinuio(iov32,
2122 			    hdtr32.hdr_cnt, &hdr_uio);
2123 			if (error)
2124 				goto out;
2125 #ifdef COMPAT_FREEBSD4
2126 			/*
2127 			 * In FreeBSD < 5.0 the nbytes to send also included
2128 			 * the header.  If compat is specified subtract the
2129 			 * header size from nbytes.
2130 			 */
2131 			if (compat) {
2132 				if (uap->nbytes > hdr_uio->uio_resid)
2133 					uap->nbytes -= hdr_uio->uio_resid;
2134 				else
2135 					uap->nbytes = 0;
2136 			}
2137 #endif
2138 		}
2139 		if (hdtr.trailers != NULL) {
2140 			iov32 = PTRIN(hdtr32.trailers);
2141 			error = freebsd32_copyinuio(iov32,
2142 			    hdtr32.trl_cnt, &trl_uio);
2143 			if (error)
2144 				goto out;
2145 		}
2146 	}
2147 
2148 	AUDIT_ARG_FD(uap->fd);
2149 
2150 	if ((error = fget_read(td, uap->fd,
2151 	    cap_rights_init_one(&rights, CAP_PREAD), &fp)) != 0)
2152 		goto out;
2153 
2154 	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
2155 	    uap->nbytes, &sbytes, uap->flags, td);
2156 	fdrop(fp, td);
2157 
2158 	if (uap->sbytes != NULL)
2159 		copyout(&sbytes, uap->sbytes, sizeof(off_t));
2160 
2161 out:
2162 	if (hdr_uio)
2163 		free(hdr_uio, M_IOV);
2164 	if (trl_uio)
2165 		free(trl_uio, M_IOV);
2166 	return (error);
2167 }
2168 
2169 #ifdef COMPAT_FREEBSD4
2170 int
2171 freebsd4_freebsd32_sendfile(struct thread *td,
2172     struct freebsd4_freebsd32_sendfile_args *uap)
2173 {
2174 	return (freebsd32_do_sendfile(td,
2175 	    (struct freebsd32_sendfile_args *)uap, 1));
2176 }
2177 #endif
2178 
2179 int
2180 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
2181 {
2182 
2183 	return (freebsd32_do_sendfile(td, uap, 0));
2184 }
2185 
2186 static void
2187 copy_stat(struct stat *in, struct stat32 *out)
2188 {
2189 
2190 #ifndef __amd64__
2191 	/*
2192 	 * 32-bit architectures other than i386 have 64-bit time_t.  This
2193 	 * results in struct timespec32 with 12 bytes for tv_sec and tv_nsec,
2194 	 * and 4 bytes of padding.  Zero the padding holes in struct stat32.
2195 	 */
2196 	bzero(&out->st_atim, sizeof(out->st_atim));
2197 	bzero(&out->st_mtim, sizeof(out->st_mtim));
2198 	bzero(&out->st_ctim, sizeof(out->st_ctim));
2199 	bzero(&out->st_birthtim, sizeof(out->st_birthtim));
2200 #endif
2201 	CP(*in, *out, st_dev);
2202 	CP(*in, *out, st_ino);
2203 	CP(*in, *out, st_mode);
2204 	CP(*in, *out, st_nlink);
2205 	CP(*in, *out, st_uid);
2206 	CP(*in, *out, st_gid);
2207 	CP(*in, *out, st_rdev);
2208 	TS_CP(*in, *out, st_atim);
2209 	TS_CP(*in, *out, st_mtim);
2210 	TS_CP(*in, *out, st_ctim);
2211 	CP(*in, *out, st_size);
2212 	CP(*in, *out, st_blocks);
2213 	CP(*in, *out, st_blksize);
2214 	CP(*in, *out, st_flags);
2215 	CP(*in, *out, st_gen);
2216 	TS_CP(*in, *out, st_birthtim);
2217 	out->st_padding0 = 0;
2218 	out->st_padding1 = 0;
2219 #ifdef __STAT32_TIME_T_EXT
2220 	out->st_atim_ext = 0;
2221 	out->st_mtim_ext = 0;
2222 	out->st_ctim_ext = 0;
2223 	out->st_btim_ext = 0;
2224 #endif
2225 	bzero(out->st_spare, sizeof(out->st_spare));
2226 }
2227 
2228 #ifdef COMPAT_43
2229 static void
2230 copy_ostat(struct stat *in, struct ostat32 *out)
2231 {
2232 
2233 	bzero(out, sizeof(*out));
2234 	CP(*in, *out, st_dev);
2235 	CP(*in, *out, st_ino);
2236 	CP(*in, *out, st_mode);
2237 	CP(*in, *out, st_nlink);
2238 	CP(*in, *out, st_uid);
2239 	CP(*in, *out, st_gid);
2240 	CP(*in, *out, st_rdev);
2241 	out->st_size = MIN(in->st_size, INT32_MAX);
2242 	TS_CP(*in, *out, st_atim);
2243 	TS_CP(*in, *out, st_mtim);
2244 	TS_CP(*in, *out, st_ctim);
2245 	CP(*in, *out, st_blksize);
2246 	CP(*in, *out, st_blocks);
2247 	CP(*in, *out, st_flags);
2248 	CP(*in, *out, st_gen);
2249 }
2250 #endif
2251 
2252 #ifdef COMPAT_43
2253 int
2254 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
2255 {
2256 	struct stat sb;
2257 	struct ostat32 sb32;
2258 	int error;
2259 
2260 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2261 	    &sb, NULL);
2262 	if (error)
2263 		return (error);
2264 	copy_ostat(&sb, &sb32);
2265 	error = copyout(&sb32, uap->ub, sizeof (sb32));
2266 	return (error);
2267 }
2268 #endif
2269 
2270 int
2271 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
2272 {
2273 	struct stat ub;
2274 	struct stat32 ub32;
2275 	int error;
2276 
2277 	error = kern_fstat(td, uap->fd, &ub);
2278 	if (error)
2279 		return (error);
2280 	copy_stat(&ub, &ub32);
2281 	error = copyout(&ub32, uap->sb, sizeof(ub32));
2282 	return (error);
2283 }
2284 
2285 #ifdef COMPAT_43
2286 int
2287 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2288 {
2289 	struct stat ub;
2290 	struct ostat32 ub32;
2291 	int error;
2292 
2293 	error = kern_fstat(td, uap->fd, &ub);
2294 	if (error)
2295 		return (error);
2296 	copy_ostat(&ub, &ub32);
2297 	error = copyout(&ub32, uap->sb, sizeof(ub32));
2298 	return (error);
2299 }
2300 #endif
2301 
2302 int
2303 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2304 {
2305 	struct stat ub;
2306 	struct stat32 ub32;
2307 	int error;
2308 
2309 	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2310 	    &ub, NULL);
2311 	if (error)
2312 		return (error);
2313 	copy_stat(&ub, &ub32);
2314 	error = copyout(&ub32, uap->buf, sizeof(ub32));
2315 	return (error);
2316 }
2317 
2318 #ifdef COMPAT_43
2319 int
2320 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2321 {
2322 	struct stat sb;
2323 	struct ostat32 sb32;
2324 	int error;
2325 
2326 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2327 	    UIO_USERSPACE, &sb, NULL);
2328 	if (error)
2329 		return (error);
2330 	copy_ostat(&sb, &sb32);
2331 	error = copyout(&sb32, uap->ub, sizeof (sb32));
2332 	return (error);
2333 }
2334 #endif
2335 
2336 int
2337 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2338 {
2339 	struct stat sb;
2340 	struct stat32 sb32;
2341 	struct fhandle fh;
2342 	int error;
2343 
2344 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2345         if (error != 0)
2346                 return (error);
2347 	error = kern_fhstat(td, fh, &sb);
2348 	if (error != 0)
2349 		return (error);
2350 	copy_stat(&sb, &sb32);
2351 	error = copyout(&sb32, uap->sb, sizeof (sb32));
2352 	return (error);
2353 }
2354 
2355 #if defined(COMPAT_FREEBSD11)
2356 extern int ino64_trunc_error;
2357 
2358 static int
2359 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2360 {
2361 
2362 #ifndef __amd64__
2363 	/*
2364 	 * 32-bit architectures other than i386 have 64-bit time_t.  This
2365 	 * results in struct timespec32 with 12 bytes for tv_sec and tv_nsec,
2366 	 * and 4 bytes of padding.  Zero the padding holes in freebsd11_stat32.
2367 	 */
2368 	bzero(&out->st_atim, sizeof(out->st_atim));
2369 	bzero(&out->st_mtim, sizeof(out->st_mtim));
2370 	bzero(&out->st_ctim, sizeof(out->st_ctim));
2371 	bzero(&out->st_birthtim, sizeof(out->st_birthtim));
2372 #endif
2373 
2374 	CP(*in, *out, st_ino);
2375 	if (in->st_ino != out->st_ino) {
2376 		switch (ino64_trunc_error) {
2377 		default:
2378 		case 0:
2379 			break;
2380 		case 1:
2381 			return (EOVERFLOW);
2382 		case 2:
2383 			out->st_ino = UINT32_MAX;
2384 			break;
2385 		}
2386 	}
2387 	CP(*in, *out, st_nlink);
2388 	if (in->st_nlink != out->st_nlink) {
2389 		switch (ino64_trunc_error) {
2390 		default:
2391 		case 0:
2392 			break;
2393 		case 1:
2394 			return (EOVERFLOW);
2395 		case 2:
2396 			out->st_nlink = UINT16_MAX;
2397 			break;
2398 		}
2399 	}
2400 	out->st_dev = in->st_dev;
2401 	if (out->st_dev != in->st_dev) {
2402 		switch (ino64_trunc_error) {
2403 		default:
2404 			break;
2405 		case 1:
2406 			return (EOVERFLOW);
2407 		}
2408 	}
2409 	CP(*in, *out, st_mode);
2410 	CP(*in, *out, st_uid);
2411 	CP(*in, *out, st_gid);
2412 	out->st_rdev = in->st_rdev;
2413 	if (out->st_rdev != in->st_rdev) {
2414 		switch (ino64_trunc_error) {
2415 		default:
2416 			break;
2417 		case 1:
2418 			return (EOVERFLOW);
2419 		}
2420 	}
2421 	TS_CP(*in, *out, st_atim);
2422 	TS_CP(*in, *out, st_mtim);
2423 	TS_CP(*in, *out, st_ctim);
2424 	CP(*in, *out, st_size);
2425 	CP(*in, *out, st_blocks);
2426 	CP(*in, *out, st_blksize);
2427 	CP(*in, *out, st_flags);
2428 	CP(*in, *out, st_gen);
2429 	TS_CP(*in, *out, st_birthtim);
2430 	out->st_lspare = 0;
2431 	bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2432 	    sizeof(*out) - offsetof(struct freebsd11_stat32,
2433 	    st_birthtim) - sizeof(out->st_birthtim));
2434 	return (0);
2435 }
2436 
2437 int
2438 freebsd11_freebsd32_stat(struct thread *td,
2439     struct freebsd11_freebsd32_stat_args *uap)
2440 {
2441 	struct stat sb;
2442 	struct freebsd11_stat32 sb32;
2443 	int error;
2444 
2445 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2446 	    &sb, NULL);
2447 	if (error != 0)
2448 		return (error);
2449 	error = freebsd11_cvtstat32(&sb, &sb32);
2450 	if (error == 0)
2451 		error = copyout(&sb32, uap->ub, sizeof (sb32));
2452 	return (error);
2453 }
2454 
2455 int
2456 freebsd11_freebsd32_fstat(struct thread *td,
2457     struct freebsd11_freebsd32_fstat_args *uap)
2458 {
2459 	struct stat sb;
2460 	struct freebsd11_stat32 sb32;
2461 	int error;
2462 
2463 	error = kern_fstat(td, uap->fd, &sb);
2464 	if (error != 0)
2465 		return (error);
2466 	error = freebsd11_cvtstat32(&sb, &sb32);
2467 	if (error == 0)
2468 		error = copyout(&sb32, uap->sb, sizeof (sb32));
2469 	return (error);
2470 }
2471 
2472 int
2473 freebsd11_freebsd32_fstatat(struct thread *td,
2474     struct freebsd11_freebsd32_fstatat_args *uap)
2475 {
2476 	struct stat sb;
2477 	struct freebsd11_stat32 sb32;
2478 	int error;
2479 
2480 	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2481 	    &sb, NULL);
2482 	if (error != 0)
2483 		return (error);
2484 	error = freebsd11_cvtstat32(&sb, &sb32);
2485 	if (error == 0)
2486 		error = copyout(&sb32, uap->buf, sizeof (sb32));
2487 	return (error);
2488 }
2489 
2490 int
2491 freebsd11_freebsd32_lstat(struct thread *td,
2492     struct freebsd11_freebsd32_lstat_args *uap)
2493 {
2494 	struct stat sb;
2495 	struct freebsd11_stat32 sb32;
2496 	int error;
2497 
2498 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2499 	    UIO_USERSPACE, &sb, NULL);
2500 	if (error != 0)
2501 		return (error);
2502 	error = freebsd11_cvtstat32(&sb, &sb32);
2503 	if (error == 0)
2504 		error = copyout(&sb32, uap->ub, sizeof (sb32));
2505 	return (error);
2506 }
2507 
2508 int
2509 freebsd11_freebsd32_fhstat(struct thread *td,
2510     struct freebsd11_freebsd32_fhstat_args *uap)
2511 {
2512 	struct stat sb;
2513 	struct freebsd11_stat32 sb32;
2514 	struct fhandle fh;
2515 	int error;
2516 
2517 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2518         if (error != 0)
2519                 return (error);
2520 	error = kern_fhstat(td, fh, &sb);
2521 	if (error != 0)
2522 		return (error);
2523 	error = freebsd11_cvtstat32(&sb, &sb32);
2524 	if (error == 0)
2525 		error = copyout(&sb32, uap->sb, sizeof (sb32));
2526 	return (error);
2527 }
2528 
2529 static int
2530 freebsd11_cvtnstat32(struct stat *sb, struct nstat32 *nsb32)
2531 {
2532 	struct nstat nsb;
2533 	int error;
2534 
2535 	error = freebsd11_cvtnstat(sb, &nsb);
2536 	if (error != 0)
2537 		return (error);
2538 
2539 	bzero(nsb32, sizeof(*nsb32));
2540 	CP(nsb, *nsb32, st_dev);
2541 	CP(nsb, *nsb32, st_ino);
2542 	CP(nsb, *nsb32, st_mode);
2543 	CP(nsb, *nsb32, st_nlink);
2544 	CP(nsb, *nsb32, st_uid);
2545 	CP(nsb, *nsb32, st_gid);
2546 	CP(nsb, *nsb32, st_rdev);
2547 	CP(nsb, *nsb32, st_atim.tv_sec);
2548 	CP(nsb, *nsb32, st_atim.tv_nsec);
2549 	CP(nsb, *nsb32, st_mtim.tv_sec);
2550 	CP(nsb, *nsb32, st_mtim.tv_nsec);
2551 	CP(nsb, *nsb32, st_ctim.tv_sec);
2552 	CP(nsb, *nsb32, st_ctim.tv_nsec);
2553 	CP(nsb, *nsb32, st_size);
2554 	CP(nsb, *nsb32, st_blocks);
2555 	CP(nsb, *nsb32, st_blksize);
2556 	CP(nsb, *nsb32, st_flags);
2557 	CP(nsb, *nsb32, st_gen);
2558 	CP(nsb, *nsb32, st_birthtim.tv_sec);
2559 	CP(nsb, *nsb32, st_birthtim.tv_nsec);
2560 	return (0);
2561 }
2562 
2563 int
2564 freebsd11_freebsd32_nstat(struct thread *td,
2565     struct freebsd11_freebsd32_nstat_args *uap)
2566 {
2567 	struct stat sb;
2568 	struct nstat32 nsb;
2569 	int error;
2570 
2571 	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2572 	    &sb, NULL);
2573 	if (error != 0)
2574 		return (error);
2575 	error = freebsd11_cvtnstat32(&sb, &nsb);
2576 	if (error != 0)
2577 		error = copyout(&nsb, uap->ub, sizeof (nsb));
2578 	return (error);
2579 }
2580 
2581 int
2582 freebsd11_freebsd32_nlstat(struct thread *td,
2583     struct freebsd11_freebsd32_nlstat_args *uap)
2584 {
2585 	struct stat sb;
2586 	struct nstat32 nsb;
2587 	int error;
2588 
2589 	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2590 	    UIO_USERSPACE, &sb, NULL);
2591 	if (error != 0)
2592 		return (error);
2593 	error = freebsd11_cvtnstat32(&sb, &nsb);
2594 	if (error == 0)
2595 		error = copyout(&nsb, uap->ub, sizeof (nsb));
2596 	return (error);
2597 }
2598 
2599 int
2600 freebsd11_freebsd32_nfstat(struct thread *td,
2601     struct freebsd11_freebsd32_nfstat_args *uap)
2602 {
2603 	struct nstat32 nub;
2604 	struct stat ub;
2605 	int error;
2606 
2607 	error = kern_fstat(td, uap->fd, &ub);
2608 	if (error != 0)
2609 		return (error);
2610 	error = freebsd11_cvtnstat32(&ub, &nub);
2611 	if (error == 0)
2612 		error = copyout(&nub, uap->sb, sizeof(nub));
2613 	return (error);
2614 }
2615 #endif
2616 
2617 int
2618 freebsd32___sysctl(struct thread *td, struct freebsd32___sysctl_args *uap)
2619 {
2620 	int error, name[CTL_MAXNAME];
2621 	size_t j, oldlen;
2622 	uint32_t tmp;
2623 
2624 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2625 		return (EINVAL);
2626  	error = copyin(uap->name, name, uap->namelen * sizeof(int));
2627  	if (error)
2628 		return (error);
2629 	if (uap->oldlenp) {
2630 		error = fueword32(uap->oldlenp, &tmp);
2631 		oldlen = tmp;
2632 	} else {
2633 		oldlen = 0;
2634 	}
2635 	if (error != 0)
2636 		return (EFAULT);
2637 	error = userland_sysctl(td, name, uap->namelen,
2638 		uap->old, &oldlen, 1,
2639 		uap->new, uap->newlen, &j, SCTL_MASK32);
2640 	if (error)
2641 		return (error);
2642 	if (uap->oldlenp)
2643 		suword32(uap->oldlenp, j);
2644 	return (0);
2645 }
2646 
2647 int
2648 freebsd32___sysctlbyname(struct thread *td,
2649     struct freebsd32___sysctlbyname_args *uap)
2650 {
2651 	size_t oldlen, rv;
2652 	int error;
2653 	uint32_t tmp;
2654 
2655 	if (uap->oldlenp != NULL) {
2656 		error = fueword32(uap->oldlenp, &tmp);
2657 		oldlen = tmp;
2658 	} else {
2659 		error = oldlen = 0;
2660 	}
2661 	if (error != 0)
2662 		return (EFAULT);
2663 	error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2664 	    &oldlen, uap->new, uap->newlen, &rv, SCTL_MASK32, 1);
2665 	if (error != 0)
2666 		return (error);
2667 	if (uap->oldlenp != NULL)
2668 		error = suword32(uap->oldlenp, rv);
2669 
2670 	return (error);
2671 }
2672 
2673 int
2674 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2675 {
2676 	uint32_t version;
2677 	int error;
2678 	struct jail j;
2679 
2680 	error = copyin(uap->jail, &version, sizeof(uint32_t));
2681 	if (error)
2682 		return (error);
2683 
2684 	switch (version) {
2685 	case 0:
2686 	{
2687 		/* FreeBSD single IPv4 jails. */
2688 		struct jail32_v0 j32_v0;
2689 
2690 		bzero(&j, sizeof(struct jail));
2691 		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2692 		if (error)
2693 			return (error);
2694 		CP(j32_v0, j, version);
2695 		PTRIN_CP(j32_v0, j, path);
2696 		PTRIN_CP(j32_v0, j, hostname);
2697 		j.ip4s = htonl(j32_v0.ip_number);	/* jail_v0 is host order */
2698 		break;
2699 	}
2700 
2701 	case 1:
2702 		/*
2703 		 * Version 1 was used by multi-IPv4 jail implementations
2704 		 * that never made it into the official kernel.
2705 		 */
2706 		return (EINVAL);
2707 
2708 	case 2:	/* JAIL_API_VERSION */
2709 	{
2710 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
2711 		struct jail32 j32;
2712 
2713 		error = copyin(uap->jail, &j32, sizeof(struct jail32));
2714 		if (error)
2715 			return (error);
2716 		CP(j32, j, version);
2717 		PTRIN_CP(j32, j, path);
2718 		PTRIN_CP(j32, j, hostname);
2719 		PTRIN_CP(j32, j, jailname);
2720 		CP(j32, j, ip4s);
2721 		CP(j32, j, ip6s);
2722 		PTRIN_CP(j32, j, ip4);
2723 		PTRIN_CP(j32, j, ip6);
2724 		break;
2725 	}
2726 
2727 	default:
2728 		/* Sci-Fi jails are not supported, sorry. */
2729 		return (EINVAL);
2730 	}
2731 	return (kern_jail(td, &j));
2732 }
2733 
2734 int
2735 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2736 {
2737 	struct uio *auio;
2738 	int error;
2739 
2740 	/* Check that we have an even number of iovecs. */
2741 	if (uap->iovcnt & 1)
2742 		return (EINVAL);
2743 
2744 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2745 	if (error)
2746 		return (error);
2747 	error = kern_jail_set(td, auio, uap->flags);
2748 	free(auio, M_IOV);
2749 	return (error);
2750 }
2751 
2752 int
2753 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2754 {
2755 	struct iovec32 iov32;
2756 	struct uio *auio;
2757 	int error, i;
2758 
2759 	/* Check that we have an even number of iovecs. */
2760 	if (uap->iovcnt & 1)
2761 		return (EINVAL);
2762 
2763 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2764 	if (error)
2765 		return (error);
2766 	error = kern_jail_get(td, auio, uap->flags);
2767 	if (error == 0)
2768 		for (i = 0; i < uap->iovcnt; i++) {
2769 			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2770 			CP(auio->uio_iov[i], iov32, iov_len);
2771 			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2772 			if (error != 0)
2773 				break;
2774 		}
2775 	free(auio, M_IOV);
2776 	return (error);
2777 }
2778 
2779 int
2780 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2781 {
2782 	struct sigaction32 s32;
2783 	struct sigaction sa, osa, *sap;
2784 	int error;
2785 
2786 	if (uap->act) {
2787 		error = copyin(uap->act, &s32, sizeof(s32));
2788 		if (error)
2789 			return (error);
2790 		sa.sa_handler = PTRIN(s32.sa_u);
2791 		CP(s32, sa, sa_flags);
2792 		CP(s32, sa, sa_mask);
2793 		sap = &sa;
2794 	} else
2795 		sap = NULL;
2796 	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2797 	if (error == 0 && uap->oact != NULL) {
2798 		s32.sa_u = PTROUT(osa.sa_handler);
2799 		CP(osa, s32, sa_flags);
2800 		CP(osa, s32, sa_mask);
2801 		error = copyout(&s32, uap->oact, sizeof(s32));
2802 	}
2803 	return (error);
2804 }
2805 
2806 #ifdef COMPAT_FREEBSD4
2807 int
2808 freebsd4_freebsd32_sigaction(struct thread *td,
2809 			     struct freebsd4_freebsd32_sigaction_args *uap)
2810 {
2811 	struct sigaction32 s32;
2812 	struct sigaction sa, osa, *sap;
2813 	int error;
2814 
2815 	if (uap->act) {
2816 		error = copyin(uap->act, &s32, sizeof(s32));
2817 		if (error)
2818 			return (error);
2819 		sa.sa_handler = PTRIN(s32.sa_u);
2820 		CP(s32, sa, sa_flags);
2821 		CP(s32, sa, sa_mask);
2822 		sap = &sa;
2823 	} else
2824 		sap = NULL;
2825 	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2826 	if (error == 0 && uap->oact != NULL) {
2827 		s32.sa_u = PTROUT(osa.sa_handler);
2828 		CP(osa, s32, sa_flags);
2829 		CP(osa, s32, sa_mask);
2830 		error = copyout(&s32, uap->oact, sizeof(s32));
2831 	}
2832 	return (error);
2833 }
2834 #endif
2835 
2836 #ifdef COMPAT_43
2837 struct osigaction32 {
2838 	uint32_t	sa_u;
2839 	osigset_t	sa_mask;
2840 	int		sa_flags;
2841 };
2842 
2843 #define	ONSIG	32
2844 
2845 int
2846 ofreebsd32_sigaction(struct thread *td,
2847 			     struct ofreebsd32_sigaction_args *uap)
2848 {
2849 	struct osigaction32 s32;
2850 	struct sigaction sa, osa, *sap;
2851 	int error;
2852 
2853 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2854 		return (EINVAL);
2855 
2856 	if (uap->nsa) {
2857 		error = copyin(uap->nsa, &s32, sizeof(s32));
2858 		if (error)
2859 			return (error);
2860 		sa.sa_handler = PTRIN(s32.sa_u);
2861 		CP(s32, sa, sa_flags);
2862 		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2863 		sap = &sa;
2864 	} else
2865 		sap = NULL;
2866 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2867 	if (error == 0 && uap->osa != NULL) {
2868 		s32.sa_u = PTROUT(osa.sa_handler);
2869 		CP(osa, s32, sa_flags);
2870 		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2871 		error = copyout(&s32, uap->osa, sizeof(s32));
2872 	}
2873 	return (error);
2874 }
2875 
2876 struct sigvec32 {
2877 	uint32_t	sv_handler;
2878 	int		sv_mask;
2879 	int		sv_flags;
2880 };
2881 
2882 int
2883 ofreebsd32_sigvec(struct thread *td,
2884 			  struct ofreebsd32_sigvec_args *uap)
2885 {
2886 	struct sigvec32 vec;
2887 	struct sigaction sa, osa, *sap;
2888 	int error;
2889 
2890 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2891 		return (EINVAL);
2892 
2893 	if (uap->nsv) {
2894 		error = copyin(uap->nsv, &vec, sizeof(vec));
2895 		if (error)
2896 			return (error);
2897 		sa.sa_handler = PTRIN(vec.sv_handler);
2898 		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2899 		sa.sa_flags = vec.sv_flags;
2900 		sa.sa_flags ^= SA_RESTART;
2901 		sap = &sa;
2902 	} else
2903 		sap = NULL;
2904 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2905 	if (error == 0 && uap->osv != NULL) {
2906 		vec.sv_handler = PTROUT(osa.sa_handler);
2907 		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2908 		vec.sv_flags = osa.sa_flags;
2909 		vec.sv_flags &= ~SA_NOCLDWAIT;
2910 		vec.sv_flags ^= SA_RESTART;
2911 		error = copyout(&vec, uap->osv, sizeof(vec));
2912 	}
2913 	return (error);
2914 }
2915 
2916 struct sigstack32 {
2917 	uint32_t	ss_sp;
2918 	int		ss_onstack;
2919 };
2920 
2921 int
2922 ofreebsd32_sigstack(struct thread *td,
2923 			    struct ofreebsd32_sigstack_args *uap)
2924 {
2925 	struct sigstack32 s32;
2926 	struct sigstack nss, oss;
2927 	int error = 0, unss;
2928 
2929 	if (uap->nss != NULL) {
2930 		error = copyin(uap->nss, &s32, sizeof(s32));
2931 		if (error)
2932 			return (error);
2933 		nss.ss_sp = PTRIN(s32.ss_sp);
2934 		CP(s32, nss, ss_onstack);
2935 		unss = 1;
2936 	} else {
2937 		unss = 0;
2938 	}
2939 	oss.ss_sp = td->td_sigstk.ss_sp;
2940 	oss.ss_onstack = sigonstack(cpu_getstack(td));
2941 	if (unss) {
2942 		td->td_sigstk.ss_sp = nss.ss_sp;
2943 		td->td_sigstk.ss_size = 0;
2944 		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2945 		td->td_pflags |= TDP_ALTSTACK;
2946 	}
2947 	if (uap->oss != NULL) {
2948 		s32.ss_sp = PTROUT(oss.ss_sp);
2949 		CP(oss, s32, ss_onstack);
2950 		error = copyout(&s32, uap->oss, sizeof(s32));
2951 	}
2952 	return (error);
2953 }
2954 #endif
2955 
2956 int
2957 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2958 {
2959 
2960 	return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2961 	    TIMER_RELTIME, uap->rqtp, uap->rmtp));
2962 }
2963 
2964 int
2965 freebsd32_clock_nanosleep(struct thread *td,
2966     struct freebsd32_clock_nanosleep_args *uap)
2967 {
2968 	int error;
2969 
2970 	error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2971 	    uap->rqtp, uap->rmtp);
2972 	return (kern_posix_error(td, error));
2973 }
2974 
2975 static int
2976 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2977     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2978 {
2979 	struct timespec32 rmt32, rqt32;
2980 	struct timespec rmt, rqt;
2981 	int error, error2;
2982 
2983 	error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2984 	if (error)
2985 		return (error);
2986 
2987 	CP(rqt32, rqt, tv_sec);
2988 	CP(rqt32, rqt, tv_nsec);
2989 
2990 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2991 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2992 		CP(rmt, rmt32, tv_sec);
2993 		CP(rmt, rmt32, tv_nsec);
2994 
2995 		error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2996 		if (error2 != 0)
2997 			error = error2;
2998 	}
2999 	return (error);
3000 }
3001 
3002 int
3003 freebsd32_clock_gettime(struct thread *td,
3004 			struct freebsd32_clock_gettime_args *uap)
3005 {
3006 	struct timespec	ats;
3007 	struct timespec32 ats32;
3008 	int error;
3009 
3010 	error = kern_clock_gettime(td, uap->clock_id, &ats);
3011 	if (error == 0) {
3012 		CP(ats, ats32, tv_sec);
3013 		CP(ats, ats32, tv_nsec);
3014 		error = copyout(&ats32, uap->tp, sizeof(ats32));
3015 	}
3016 	return (error);
3017 }
3018 
3019 int
3020 freebsd32_clock_settime(struct thread *td,
3021 			struct freebsd32_clock_settime_args *uap)
3022 {
3023 	struct timespec	ats;
3024 	struct timespec32 ats32;
3025 	int error;
3026 
3027 	error = copyin(uap->tp, &ats32, sizeof(ats32));
3028 	if (error)
3029 		return (error);
3030 	CP(ats32, ats, tv_sec);
3031 	CP(ats32, ats, tv_nsec);
3032 
3033 	return (kern_clock_settime(td, uap->clock_id, &ats));
3034 }
3035 
3036 int
3037 freebsd32_clock_getres(struct thread *td,
3038 		       struct freebsd32_clock_getres_args *uap)
3039 {
3040 	struct timespec	ts;
3041 	struct timespec32 ts32;
3042 	int error;
3043 
3044 	if (uap->tp == NULL)
3045 		return (0);
3046 	error = kern_clock_getres(td, uap->clock_id, &ts);
3047 	if (error == 0) {
3048 		CP(ts, ts32, tv_sec);
3049 		CP(ts, ts32, tv_nsec);
3050 		error = copyout(&ts32, uap->tp, sizeof(ts32));
3051 	}
3052 	return (error);
3053 }
3054 
3055 int freebsd32_ktimer_create(struct thread *td,
3056     struct freebsd32_ktimer_create_args *uap)
3057 {
3058 	struct sigevent32 ev32;
3059 	struct sigevent ev, *evp;
3060 	int error, id;
3061 
3062 	if (uap->evp == NULL) {
3063 		evp = NULL;
3064 	} else {
3065 		evp = &ev;
3066 		error = copyin(uap->evp, &ev32, sizeof(ev32));
3067 		if (error != 0)
3068 			return (error);
3069 		error = convert_sigevent32(&ev32, &ev);
3070 		if (error != 0)
3071 			return (error);
3072 	}
3073 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
3074 	if (error == 0) {
3075 		error = copyout(&id, uap->timerid, sizeof(int));
3076 		if (error != 0)
3077 			kern_ktimer_delete(td, id);
3078 	}
3079 	return (error);
3080 }
3081 
3082 int
3083 freebsd32_ktimer_settime(struct thread *td,
3084     struct freebsd32_ktimer_settime_args *uap)
3085 {
3086 	struct itimerspec32 val32, oval32;
3087 	struct itimerspec val, oval, *ovalp;
3088 	int error;
3089 
3090 	error = copyin(uap->value, &val32, sizeof(val32));
3091 	if (error != 0)
3092 		return (error);
3093 	ITS_CP(val32, val);
3094 	ovalp = uap->ovalue != NULL ? &oval : NULL;
3095 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
3096 	if (error == 0 && uap->ovalue != NULL) {
3097 		ITS_CP(oval, oval32);
3098 		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
3099 	}
3100 	return (error);
3101 }
3102 
3103 int
3104 freebsd32_ktimer_gettime(struct thread *td,
3105     struct freebsd32_ktimer_gettime_args *uap)
3106 {
3107 	struct itimerspec32 val32;
3108 	struct itimerspec val;
3109 	int error;
3110 
3111 	error = kern_ktimer_gettime(td, uap->timerid, &val);
3112 	if (error == 0) {
3113 		ITS_CP(val, val32);
3114 		error = copyout(&val32, uap->value, sizeof(val32));
3115 	}
3116 	return (error);
3117 }
3118 
3119 int
3120 freebsd32_clock_getcpuclockid2(struct thread *td,
3121     struct freebsd32_clock_getcpuclockid2_args *uap)
3122 {
3123 	clockid_t clk_id;
3124 	int error;
3125 
3126 	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
3127 	    uap->which, &clk_id);
3128 	if (error == 0)
3129 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
3130 	return (error);
3131 }
3132 
3133 int
3134 freebsd32_thr_new(struct thread *td,
3135 		  struct freebsd32_thr_new_args *uap)
3136 {
3137 	struct thr_param32 param32;
3138 	struct thr_param param;
3139 	int error;
3140 
3141 	if (uap->param_size < 0 ||
3142 	    uap->param_size > sizeof(struct thr_param32))
3143 		return (EINVAL);
3144 	bzero(&param, sizeof(struct thr_param));
3145 	bzero(&param32, sizeof(struct thr_param32));
3146 	error = copyin(uap->param, &param32, uap->param_size);
3147 	if (error != 0)
3148 		return (error);
3149 	param.start_func = PTRIN(param32.start_func);
3150 	param.arg = PTRIN(param32.arg);
3151 	param.stack_base = PTRIN(param32.stack_base);
3152 	param.stack_size = param32.stack_size;
3153 	param.tls_base = PTRIN(param32.tls_base);
3154 	param.tls_size = param32.tls_size;
3155 	param.child_tid = PTRIN(param32.child_tid);
3156 	param.parent_tid = PTRIN(param32.parent_tid);
3157 	param.flags = param32.flags;
3158 	param.rtp = PTRIN(param32.rtp);
3159 	param.spare[0] = PTRIN(param32.spare[0]);
3160 	param.spare[1] = PTRIN(param32.spare[1]);
3161 	param.spare[2] = PTRIN(param32.spare[2]);
3162 
3163 	return (kern_thr_new(td, &param));
3164 }
3165 
3166 int
3167 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
3168 {
3169 	struct timespec32 ts32;
3170 	struct timespec ts, *tsp;
3171 	int error;
3172 
3173 	error = 0;
3174 	tsp = NULL;
3175 	if (uap->timeout != NULL) {
3176 		error = copyin((const void *)uap->timeout, (void *)&ts32,
3177 		    sizeof(struct timespec32));
3178 		if (error != 0)
3179 			return (error);
3180 		ts.tv_sec = ts32.tv_sec;
3181 		ts.tv_nsec = ts32.tv_nsec;
3182 		tsp = &ts;
3183 	}
3184 	return (kern_thr_suspend(td, tsp));
3185 }
3186 
3187 void
3188 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
3189 {
3190 	bzero(dst, sizeof(*dst));
3191 	dst->si_signo = src->si_signo;
3192 	dst->si_errno = src->si_errno;
3193 	dst->si_code = src->si_code;
3194 	dst->si_pid = src->si_pid;
3195 	dst->si_uid = src->si_uid;
3196 	dst->si_status = src->si_status;
3197 	dst->si_addr = (uintptr_t)src->si_addr;
3198 	dst->si_value.sival_int = src->si_value.sival_int;
3199 	dst->si_timerid = src->si_timerid;
3200 	dst->si_overrun = src->si_overrun;
3201 }
3202 
3203 #ifndef _FREEBSD32_SYSPROTO_H_
3204 struct freebsd32_sigqueue_args {
3205         pid_t pid;
3206         int signum;
3207         /* union sigval32 */ int value;
3208 };
3209 #endif
3210 int
3211 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
3212 {
3213 	union sigval sv;
3214 
3215 	/*
3216 	 * On 32-bit ABIs, sival_int and sival_ptr are the same.
3217 	 * On 64-bit little-endian ABIs, the low bits are the same.
3218 	 * In 64-bit big-endian ABIs, sival_int overlaps with
3219 	 * sival_ptr's HIGH bits.  We choose to support sival_int
3220 	 * rather than sival_ptr in this case as it seems to be
3221 	 * more common.
3222 	 */
3223 	bzero(&sv, sizeof(sv));
3224 	sv.sival_int = (uint32_t)(uint64_t)uap->value;
3225 
3226 	return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
3227 }
3228 
3229 int
3230 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
3231 {
3232 	struct timespec32 ts32;
3233 	struct timespec ts;
3234 	struct timespec *timeout;
3235 	sigset_t set;
3236 	ksiginfo_t ksi;
3237 	struct siginfo32 si32;
3238 	int error;
3239 
3240 	if (uap->timeout) {
3241 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
3242 		if (error)
3243 			return (error);
3244 		ts.tv_sec = ts32.tv_sec;
3245 		ts.tv_nsec = ts32.tv_nsec;
3246 		timeout = &ts;
3247 	} else
3248 		timeout = NULL;
3249 
3250 	error = copyin(uap->set, &set, sizeof(set));
3251 	if (error)
3252 		return (error);
3253 
3254 	error = kern_sigtimedwait(td, set, &ksi, timeout);
3255 	if (error)
3256 		return (error);
3257 
3258 	if (uap->info) {
3259 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
3260 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
3261 	}
3262 
3263 	if (error == 0)
3264 		td->td_retval[0] = ksi.ksi_signo;
3265 	return (error);
3266 }
3267 
3268 /*
3269  * MPSAFE
3270  */
3271 int
3272 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
3273 {
3274 	ksiginfo_t ksi;
3275 	struct siginfo32 si32;
3276 	sigset_t set;
3277 	int error;
3278 
3279 	error = copyin(uap->set, &set, sizeof(set));
3280 	if (error)
3281 		return (error);
3282 
3283 	error = kern_sigtimedwait(td, set, &ksi, NULL);
3284 	if (error)
3285 		return (error);
3286 
3287 	if (uap->info) {
3288 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
3289 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
3290 	}
3291 	if (error == 0)
3292 		td->td_retval[0] = ksi.ksi_signo;
3293 	return (error);
3294 }
3295 
3296 int
3297 freebsd32_cpuset_setid(struct thread *td,
3298     struct freebsd32_cpuset_setid_args *uap)
3299 {
3300 
3301 	return (kern_cpuset_setid(td, uap->which,
3302 	    PAIR32TO64(id_t, uap->id), uap->setid));
3303 }
3304 
3305 int
3306 freebsd32_cpuset_getid(struct thread *td,
3307     struct freebsd32_cpuset_getid_args *uap)
3308 {
3309 
3310 	return (kern_cpuset_getid(td, uap->level, uap->which,
3311 	    PAIR32TO64(id_t, uap->id), uap->setid));
3312 }
3313 
3314 static int
3315 copyin32_set(const void *u, void *k, size_t size)
3316 {
3317 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
3318 	int rv;
3319 	struct bitset *kb = k;
3320 	int *p;
3321 
3322 	rv = copyin(u, k, size);
3323 	if (rv != 0)
3324 		return (rv);
3325 
3326 	p = (int *)kb->__bits;
3327 	/* Loop through swapping words.
3328 	 * `size' is in bytes, we need bits. */
3329 	for (int i = 0; i < __bitset_words(size * 8); i++) {
3330 		int tmp = p[0];
3331 		p[0] = p[1];
3332 		p[1] = tmp;
3333 		p += 2;
3334 	}
3335 	return (0);
3336 #else
3337 	return (copyin(u, k, size));
3338 #endif
3339 }
3340 
3341 static int
3342 copyout32_set(const void *k, void *u, size_t size)
3343 {
3344 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
3345 	const struct bitset *kb = k;
3346 	struct bitset *ub = u;
3347 	const int *kp = (const int *)kb->__bits;
3348 	int *up = (int *)ub->__bits;
3349 	int rv;
3350 
3351 	for (int i = 0; i < __bitset_words(CPU_SETSIZE); i++) {
3352 		/* `size' is in bytes, we need bits. */
3353 		for (int i = 0; i < __bitset_words(size * 8); i++) {
3354 			rv = suword32(up, kp[1]);
3355 			if (rv == 0)
3356 				rv = suword32(up + 1, kp[0]);
3357 			if (rv != 0)
3358 				return (EFAULT);
3359 		}
3360 	}
3361 	return (0);
3362 #else
3363 	return (copyout(k, u, size));
3364 #endif
3365 }
3366 
3367 static const struct cpuset_copy_cb cpuset_copy32_cb = {
3368 	.cpuset_copyin = copyin32_set,
3369 	.cpuset_copyout = copyout32_set
3370 };
3371 
3372 int
3373 freebsd32_cpuset_getaffinity(struct thread *td,
3374     struct freebsd32_cpuset_getaffinity_args *uap)
3375 {
3376 
3377 	return (user_cpuset_getaffinity(td, uap->level, uap->which,
3378 	    PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask,
3379 	    &cpuset_copy32_cb));
3380 }
3381 
3382 int
3383 freebsd32_cpuset_setaffinity(struct thread *td,
3384     struct freebsd32_cpuset_setaffinity_args *uap)
3385 {
3386 
3387 	return (user_cpuset_setaffinity(td, uap->level, uap->which,
3388 	    PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask,
3389 	    &cpuset_copy32_cb));
3390 }
3391 
3392 int
3393 freebsd32_cpuset_getdomain(struct thread *td,
3394     struct freebsd32_cpuset_getdomain_args *uap)
3395 {
3396 
3397 	return (kern_cpuset_getdomain(td, uap->level, uap->which,
3398 	    PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy,
3399 	    &cpuset_copy32_cb));
3400 }
3401 
3402 int
3403 freebsd32_cpuset_setdomain(struct thread *td,
3404     struct freebsd32_cpuset_setdomain_args *uap)
3405 {
3406 
3407 	return (kern_cpuset_setdomain(td, uap->level, uap->which,
3408 	    PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy,
3409 	    &cpuset_copy32_cb));
3410 }
3411 
3412 int
3413 freebsd32_nmount(struct thread *td,
3414     struct freebsd32_nmount_args /* {
3415     	struct iovec *iovp;
3416     	unsigned int iovcnt;
3417     	int flags;
3418     } */ *uap)
3419 {
3420 	struct uio *auio;
3421 	uint64_t flags;
3422 	int error;
3423 
3424 	/*
3425 	 * Mount flags are now 64-bits. On 32-bit archtectures only
3426 	 * 32-bits are passed in, but from here on everything handles
3427 	 * 64-bit flags correctly.
3428 	 */
3429 	flags = uap->flags;
3430 
3431 	AUDIT_ARG_FFLAGS(flags);
3432 
3433 	/*
3434 	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
3435 	 * userspace to set this flag, but we must filter it out if we want
3436 	 * MNT_UPDATE on the root file system to work.
3437 	 * MNT_ROOTFS should only be set by the kernel when mounting its
3438 	 * root file system.
3439 	 */
3440 	flags &= ~MNT_ROOTFS;
3441 
3442 	/*
3443 	 * check that we have an even number of iovec's
3444 	 * and that we have at least two options.
3445 	 */
3446 	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3447 		return (EINVAL);
3448 
3449 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3450 	if (error)
3451 		return (error);
3452 	error = vfs_donmount(td, flags, auio);
3453 
3454 	free(auio, M_IOV);
3455 	return error;
3456 }
3457 
3458 #if 0
3459 int
3460 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3461 {
3462 	struct yyy32 *p32, s32;
3463 	struct yyy *p = NULL, s;
3464 	struct xxx_arg ap;
3465 	int error;
3466 
3467 	if (uap->zzz) {
3468 		error = copyin(uap->zzz, &s32, sizeof(s32));
3469 		if (error)
3470 			return (error);
3471 		/* translate in */
3472 		p = &s;
3473 	}
3474 	error = kern_xxx(td, p);
3475 	if (error)
3476 		return (error);
3477 	if (uap->zzz) {
3478 		/* translate out */
3479 		error = copyout(&s32, p32, sizeof(s32));
3480 	}
3481 	return (error);
3482 }
3483 #endif
3484 
3485 int
3486 syscall32_module_handler(struct module *mod, int what, void *arg)
3487 {
3488 
3489 	return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3490 }
3491 
3492 int
3493 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3494 {
3495 
3496 	return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3497 }
3498 
3499 int
3500 syscall32_helper_unregister(struct syscall_helper_data *sd)
3501 {
3502 
3503 	return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3504 }
3505 
3506 int
3507 freebsd32_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
3508 {
3509 	struct sysentvec *sysent;
3510 	int argc, envc, i;
3511 	uint32_t *vectp;
3512 	char *stringp;
3513 	uintptr_t destp, ustringp;
3514 	struct freebsd32_ps_strings *arginfo;
3515 	char canary[sizeof(long) * 8];
3516 	int32_t pagesizes32[MAXPAGESIZES];
3517 	size_t execpath_len;
3518 	int error, szsigcode;
3519 
3520 	sysent = imgp->sysent;
3521 
3522 	arginfo = (struct freebsd32_ps_strings *)PROC_PS_STRINGS(imgp->proc);
3523 	imgp->ps_strings = arginfo;
3524 	destp =	(uintptr_t)arginfo;
3525 
3526 	/*
3527 	 * Install sigcode.
3528 	 */
3529 	if (!PROC_HAS_SHP(imgp->proc)) {
3530 		szsigcode = *sysent->sv_szsigcode;
3531 		destp -= szsigcode;
3532 		destp = rounddown2(destp, sizeof(uint32_t));
3533 		error = copyout(sysent->sv_sigcode, (void *)destp,
3534 		    szsigcode);
3535 		if (error != 0)
3536 			return (error);
3537 	}
3538 
3539 	/*
3540 	 * Copy the image path for the rtld.
3541 	 */
3542 	if (imgp->execpath != NULL && imgp->auxargs != NULL) {
3543 		execpath_len = strlen(imgp->execpath) + 1;
3544 		destp -= execpath_len;
3545 		imgp->execpathp = (void *)destp;
3546 		error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
3547 		if (error != 0)
3548 			return (error);
3549 	}
3550 
3551 	/*
3552 	 * Prepare the canary for SSP.
3553 	 */
3554 	arc4rand(canary, sizeof(canary), 0);
3555 	destp -= sizeof(canary);
3556 	imgp->canary = (void *)destp;
3557 	error = copyout(canary, imgp->canary, sizeof(canary));
3558 	if (error != 0)
3559 		return (error);
3560 	imgp->canarylen = sizeof(canary);
3561 
3562 	/*
3563 	 * Prepare the pagesizes array.
3564 	 */
3565 	for (i = 0; i < MAXPAGESIZES; i++)
3566 		pagesizes32[i] = (uint32_t)pagesizes[i];
3567 	destp -= sizeof(pagesizes32);
3568 	destp = rounddown2(destp, sizeof(uint32_t));
3569 	imgp->pagesizes = (void *)destp;
3570 	error = copyout(pagesizes32, imgp->pagesizes, sizeof(pagesizes32));
3571 	if (error != 0)
3572 		return (error);
3573 	imgp->pagesizeslen = sizeof(pagesizes32);
3574 
3575 	/*
3576 	 * Allocate room for the argument and environment strings.
3577 	 */
3578 	destp -= ARG_MAX - imgp->args->stringspace;
3579 	destp = rounddown2(destp, sizeof(uint32_t));
3580 	ustringp = destp;
3581 
3582 	if (imgp->auxargs) {
3583 		/*
3584 		 * Allocate room on the stack for the ELF auxargs
3585 		 * array.  It has up to AT_COUNT entries.
3586 		 */
3587 		destp -= AT_COUNT * sizeof(Elf32_Auxinfo);
3588 		destp = rounddown2(destp, sizeof(uint32_t));
3589 	}
3590 
3591 	vectp = (uint32_t *)destp;
3592 
3593 	/*
3594 	 * Allocate room for the argv[] and env vectors including the
3595 	 * terminating NULL pointers.
3596 	 */
3597 	vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3598 
3599 	/*
3600 	 * vectp also becomes our initial stack base
3601 	 */
3602 	*stack_base = (uintptr_t)vectp;
3603 
3604 	stringp = imgp->args->begin_argv;
3605 	argc = imgp->args->argc;
3606 	envc = imgp->args->envc;
3607 	/*
3608 	 * Copy out strings - arguments and environment.
3609 	 */
3610 	error = copyout(stringp, (void *)ustringp,
3611 	    ARG_MAX - imgp->args->stringspace);
3612 	if (error != 0)
3613 		return (error);
3614 
3615 	/*
3616 	 * Fill in "ps_strings" struct for ps, w, etc.
3617 	 */
3618 	imgp->argv = vectp;
3619 	if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||
3620 	    suword32(&arginfo->ps_nargvstr, argc) != 0)
3621 		return (EFAULT);
3622 
3623 	/*
3624 	 * Fill in argument portion of vector table.
3625 	 */
3626 	for (; argc > 0; --argc) {
3627 		if (suword32(vectp++, ustringp) != 0)
3628 			return (EFAULT);
3629 		while (*stringp++ != 0)
3630 			ustringp++;
3631 		ustringp++;
3632 	}
3633 
3634 	/* a null vector table pointer separates the argp's from the envp's */
3635 	if (suword32(vectp++, 0) != 0)
3636 		return (EFAULT);
3637 
3638 	imgp->envv = vectp;
3639 	if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||
3640 	    suword32(&arginfo->ps_nenvstr, envc) != 0)
3641 		return (EFAULT);
3642 
3643 	/*
3644 	 * Fill in environment portion of vector table.
3645 	 */
3646 	for (; envc > 0; --envc) {
3647 		if (suword32(vectp++, ustringp) != 0)
3648 			return (EFAULT);
3649 		while (*stringp++ != 0)
3650 			ustringp++;
3651 		ustringp++;
3652 	}
3653 
3654 	/* end of vector table is a null pointer */
3655 	if (suword32(vectp, 0) != 0)
3656 		return (EFAULT);
3657 
3658 	if (imgp->auxargs) {
3659 		vectp++;
3660 		error = imgp->sysent->sv_copyout_auxargs(imgp,
3661 		    (uintptr_t)vectp);
3662 		if (error != 0)
3663 			return (error);
3664 	}
3665 
3666 	return (0);
3667 }
3668 
3669 int
3670 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3671 {
3672 	struct kld_file_stat *stat;
3673 	struct kld_file_stat32 *stat32;
3674 	int error, version;
3675 
3676 	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3677 	    != 0)
3678 		return (error);
3679 	if (version != sizeof(struct kld_file_stat_1_32) &&
3680 	    version != sizeof(struct kld_file_stat32))
3681 		return (EINVAL);
3682 
3683 	stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3684 	stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3685 	error = kern_kldstat(td, uap->fileid, stat);
3686 	if (error == 0) {
3687 		bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3688 		CP(*stat, *stat32, refs);
3689 		CP(*stat, *stat32, id);
3690 		PTROUT_CP(*stat, *stat32, address);
3691 		CP(*stat, *stat32, size);
3692 		bcopy(&stat->pathname[0], &stat32->pathname[0],
3693 		    sizeof(stat->pathname));
3694 		stat32->version  = version;
3695 		error = copyout(stat32, uap->stat, version);
3696 	}
3697 	free(stat, M_TEMP);
3698 	free(stat32, M_TEMP);
3699 	return (error);
3700 }
3701 
3702 int
3703 freebsd32_posix_fallocate(struct thread *td,
3704     struct freebsd32_posix_fallocate_args *uap)
3705 {
3706 	int error;
3707 
3708 	error = kern_posix_fallocate(td, uap->fd,
3709 	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3710 	return (kern_posix_error(td, error));
3711 }
3712 
3713 int
3714 freebsd32_posix_fadvise(struct thread *td,
3715     struct freebsd32_posix_fadvise_args *uap)
3716 {
3717 	int error;
3718 
3719 	error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3720 	    PAIR32TO64(off_t, uap->len), uap->advice);
3721 	return (kern_posix_error(td, error));
3722 }
3723 
3724 int
3725 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3726 {
3727 
3728 	CP(*sig32, *sig, sigev_notify);
3729 	switch (sig->sigev_notify) {
3730 	case SIGEV_NONE:
3731 		break;
3732 	case SIGEV_THREAD_ID:
3733 		CP(*sig32, *sig, sigev_notify_thread_id);
3734 		/* FALLTHROUGH */
3735 	case SIGEV_SIGNAL:
3736 		CP(*sig32, *sig, sigev_signo);
3737 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3738 		break;
3739 	case SIGEV_KEVENT:
3740 		CP(*sig32, *sig, sigev_notify_kqueue);
3741 		CP(*sig32, *sig, sigev_notify_kevent_flags);
3742 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3743 		break;
3744 	default:
3745 		return (EINVAL);
3746 	}
3747 	return (0);
3748 }
3749 
3750 int
3751 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3752 {
3753 	void *data;
3754 	union {
3755 		struct procctl_reaper_status rs;
3756 		struct procctl_reaper_pids rp;
3757 		struct procctl_reaper_kill rk;
3758 	} x;
3759 	union {
3760 		struct procctl_reaper_pids32 rp;
3761 	} x32;
3762 	int error, error1, flags, signum;
3763 
3764 	if (uap->com >= PROC_PROCCTL_MD_MIN)
3765 		return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3766 		    uap->com, PTRIN(uap->data)));
3767 
3768 	switch (uap->com) {
3769 	case PROC_ASLR_CTL:
3770 	case PROC_PROTMAX_CTL:
3771 	case PROC_SPROTECT:
3772 	case PROC_STACKGAP_CTL:
3773 	case PROC_TRACE_CTL:
3774 	case PROC_TRAPCAP_CTL:
3775 	case PROC_NO_NEW_PRIVS_CTL:
3776 	case PROC_WXMAP_CTL:
3777 		error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3778 		if (error != 0)
3779 			return (error);
3780 		data = &flags;
3781 		break;
3782 	case PROC_REAP_ACQUIRE:
3783 	case PROC_REAP_RELEASE:
3784 		if (uap->data != NULL)
3785 			return (EINVAL);
3786 		data = NULL;
3787 		break;
3788 	case PROC_REAP_STATUS:
3789 		data = &x.rs;
3790 		break;
3791 	case PROC_REAP_GETPIDS:
3792 		error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3793 		if (error != 0)
3794 			return (error);
3795 		CP(x32.rp, x.rp, rp_count);
3796 		PTRIN_CP(x32.rp, x.rp, rp_pids);
3797 		data = &x.rp;
3798 		break;
3799 	case PROC_REAP_KILL:
3800 		error = copyin(uap->data, &x.rk, sizeof(x.rk));
3801 		if (error != 0)
3802 			return (error);
3803 		data = &x.rk;
3804 		break;
3805 	case PROC_ASLR_STATUS:
3806 	case PROC_PROTMAX_STATUS:
3807 	case PROC_STACKGAP_STATUS:
3808 	case PROC_TRACE_STATUS:
3809 	case PROC_TRAPCAP_STATUS:
3810 	case PROC_NO_NEW_PRIVS_STATUS:
3811 	case PROC_WXMAP_STATUS:
3812 		data = &flags;
3813 		break;
3814 	case PROC_PDEATHSIG_CTL:
3815 		error = copyin(uap->data, &signum, sizeof(signum));
3816 		if (error != 0)
3817 			return (error);
3818 		data = &signum;
3819 		break;
3820 	case PROC_PDEATHSIG_STATUS:
3821 		data = &signum;
3822 		break;
3823 	default:
3824 		return (EINVAL);
3825 	}
3826 	error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3827 	    uap->com, data);
3828 	switch (uap->com) {
3829 	case PROC_REAP_STATUS:
3830 		if (error == 0)
3831 			error = copyout(&x.rs, uap->data, sizeof(x.rs));
3832 		break;
3833 	case PROC_REAP_KILL:
3834 		error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3835 		if (error == 0)
3836 			error = error1;
3837 		break;
3838 	case PROC_ASLR_STATUS:
3839 	case PROC_PROTMAX_STATUS:
3840 	case PROC_STACKGAP_STATUS:
3841 	case PROC_TRACE_STATUS:
3842 	case PROC_TRAPCAP_STATUS:
3843 	case PROC_NO_NEW_PRIVS_STATUS:
3844 	case PROC_WXMAP_STATUS:
3845 		if (error == 0)
3846 			error = copyout(&flags, uap->data, sizeof(flags));
3847 		break;
3848 	case PROC_PDEATHSIG_STATUS:
3849 		if (error == 0)
3850 			error = copyout(&signum, uap->data, sizeof(signum));
3851 		break;
3852 	}
3853 	return (error);
3854 }
3855 
3856 int
3857 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3858 {
3859 	long tmp;
3860 
3861 	switch (uap->cmd) {
3862 	/*
3863 	 * Do unsigned conversion for arg when operation
3864 	 * interprets it as flags or pointer.
3865 	 */
3866 	case F_SETLK_REMOTE:
3867 	case F_SETLKW:
3868 	case F_SETLK:
3869 	case F_GETLK:
3870 	case F_SETFD:
3871 	case F_SETFL:
3872 	case F_OGETLK:
3873 	case F_OSETLK:
3874 	case F_OSETLKW:
3875 	case F_KINFO:
3876 		tmp = (unsigned int)(uap->arg);
3877 		break;
3878 	default:
3879 		tmp = uap->arg;
3880 		break;
3881 	}
3882 	return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3883 }
3884 
3885 int
3886 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3887 {
3888 	struct timespec32 ts32;
3889 	struct timespec ts, *tsp;
3890 	sigset_t set, *ssp;
3891 	int error;
3892 
3893 	if (uap->ts != NULL) {
3894 		error = copyin(uap->ts, &ts32, sizeof(ts32));
3895 		if (error != 0)
3896 			return (error);
3897 		CP(ts32, ts, tv_sec);
3898 		CP(ts32, ts, tv_nsec);
3899 		tsp = &ts;
3900 	} else
3901 		tsp = NULL;
3902 	if (uap->set != NULL) {
3903 		error = copyin(uap->set, &set, sizeof(set));
3904 		if (error != 0)
3905 			return (error);
3906 		ssp = &set;
3907 	} else
3908 		ssp = NULL;
3909 
3910 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3911 }
3912 
3913 int
3914 freebsd32_sched_rr_get_interval(struct thread *td,
3915     struct freebsd32_sched_rr_get_interval_args *uap)
3916 {
3917 	struct timespec ts;
3918 	struct timespec32 ts32;
3919 	int error;
3920 
3921 	error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3922 	if (error == 0) {
3923 		CP(ts, ts32, tv_sec);
3924 		CP(ts, ts32, tv_nsec);
3925 		error = copyout(&ts32, uap->interval, sizeof(ts32));
3926 	}
3927 	return (error);
3928 }
3929 
3930 static void
3931 timex_to_32(struct timex32 *dst, struct timex *src)
3932 {
3933 	CP(*src, *dst, modes);
3934 	CP(*src, *dst, offset);
3935 	CP(*src, *dst, freq);
3936 	CP(*src, *dst, maxerror);
3937 	CP(*src, *dst, esterror);
3938 	CP(*src, *dst, status);
3939 	CP(*src, *dst, constant);
3940 	CP(*src, *dst, precision);
3941 	CP(*src, *dst, tolerance);
3942 	CP(*src, *dst, ppsfreq);
3943 	CP(*src, *dst, jitter);
3944 	CP(*src, *dst, shift);
3945 	CP(*src, *dst, stabil);
3946 	CP(*src, *dst, jitcnt);
3947 	CP(*src, *dst, calcnt);
3948 	CP(*src, *dst, errcnt);
3949 	CP(*src, *dst, stbcnt);
3950 }
3951 
3952 static void
3953 timex_from_32(struct timex *dst, struct timex32 *src)
3954 {
3955 	CP(*src, *dst, modes);
3956 	CP(*src, *dst, offset);
3957 	CP(*src, *dst, freq);
3958 	CP(*src, *dst, maxerror);
3959 	CP(*src, *dst, esterror);
3960 	CP(*src, *dst, status);
3961 	CP(*src, *dst, constant);
3962 	CP(*src, *dst, precision);
3963 	CP(*src, *dst, tolerance);
3964 	CP(*src, *dst, ppsfreq);
3965 	CP(*src, *dst, jitter);
3966 	CP(*src, *dst, shift);
3967 	CP(*src, *dst, stabil);
3968 	CP(*src, *dst, jitcnt);
3969 	CP(*src, *dst, calcnt);
3970 	CP(*src, *dst, errcnt);
3971 	CP(*src, *dst, stbcnt);
3972 }
3973 
3974 int
3975 freebsd32_ntp_adjtime(struct thread *td, struct freebsd32_ntp_adjtime_args *uap)
3976 {
3977 	struct timex tx;
3978 	struct timex32 tx32;
3979 	int error, retval;
3980 
3981 	error = copyin(uap->tp, &tx32, sizeof(tx32));
3982 	if (error == 0) {
3983 		timex_from_32(&tx, &tx32);
3984 		error = kern_ntp_adjtime(td, &tx, &retval);
3985 		if (error == 0) {
3986 			timex_to_32(&tx32, &tx);
3987 			error = copyout(&tx32, uap->tp, sizeof(tx32));
3988 			if (error == 0)
3989 				td->td_retval[0] = retval;
3990 		}
3991 	}
3992 	return (error);
3993 }
3994 
3995 #ifdef FFCLOCK
3996 extern struct mtx ffclock_mtx;
3997 extern struct ffclock_estimate ffclock_estimate;
3998 extern int8_t ffclock_updated;
3999 
4000 int
4001 freebsd32_ffclock_setestimate(struct thread *td,
4002     struct freebsd32_ffclock_setestimate_args *uap)
4003 {
4004 	struct ffclock_estimate cest;
4005 	struct ffclock_estimate32 cest32;
4006 	int error;
4007 
4008 	/* Reuse of PRIV_CLOCK_SETTIME. */
4009 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
4010 		return (error);
4011 
4012 	if ((error = copyin(uap->cest, &cest32,
4013 	    sizeof(struct ffclock_estimate32))) != 0)
4014 		return (error);
4015 
4016 	CP(cest.update_time, cest32.update_time, sec);
4017 	memcpy(&cest.update_time.frac, &cest32.update_time.frac, sizeof(uint64_t));
4018 	CP(cest, cest32, update_ffcount);
4019 	CP(cest, cest32, leapsec_next);
4020 	CP(cest, cest32, period);
4021 	CP(cest, cest32, errb_abs);
4022 	CP(cest, cest32, errb_rate);
4023 	CP(cest, cest32, status);
4024 	CP(cest, cest32, leapsec_total);
4025 	CP(cest, cest32, leapsec);
4026 
4027 	mtx_lock(&ffclock_mtx);
4028 	memcpy(&ffclock_estimate, &cest, sizeof(struct ffclock_estimate));
4029 	ffclock_updated++;
4030 	mtx_unlock(&ffclock_mtx);
4031 	return (error);
4032 }
4033 
4034 int
4035 freebsd32_ffclock_getestimate(struct thread *td,
4036     struct freebsd32_ffclock_getestimate_args *uap)
4037 {
4038 	struct ffclock_estimate cest;
4039 	struct ffclock_estimate32 cest32;
4040 	int error;
4041 
4042 	mtx_lock(&ffclock_mtx);
4043 	memcpy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
4044 	mtx_unlock(&ffclock_mtx);
4045 
4046 	CP(cest32.update_time, cest.update_time, sec);
4047 	memcpy(&cest32.update_time.frac, &cest.update_time.frac, sizeof(uint64_t));
4048 	CP(cest32, cest, update_ffcount);
4049 	CP(cest32, cest, leapsec_next);
4050 	CP(cest32, cest, period);
4051 	CP(cest32, cest, errb_abs);
4052 	CP(cest32, cest, errb_rate);
4053 	CP(cest32, cest, status);
4054 	CP(cest32, cest, leapsec_total);
4055 	CP(cest32, cest, leapsec);
4056 
4057 	error = copyout(&cest32, uap->cest, sizeof(struct ffclock_estimate32));
4058 	return (error);
4059 }
4060 #else /* !FFCLOCK */
4061 int
4062 freebsd32_ffclock_setestimate(struct thread *td,
4063     struct freebsd32_ffclock_setestimate_args *uap)
4064 {
4065 	return (ENOSYS);
4066 }
4067 
4068 int
4069 freebsd32_ffclock_getestimate(struct thread *td,
4070     struct freebsd32_ffclock_getestimate_args *uap)
4071 {
4072 	return (ENOSYS);
4073 }
4074 #endif /* FFCLOCK */
4075 
4076 #ifdef COMPAT_43
4077 int
4078 ofreebsd32_sethostid(struct thread *td, struct ofreebsd32_sethostid_args *uap)
4079 {
4080 	int name[] = { CTL_KERN, KERN_HOSTID };
4081 	long hostid;
4082 
4083 	hostid = uap->hostid;
4084 	return (kernel_sysctl(td, name, nitems(name), NULL, NULL, &hostid,
4085 	    sizeof(hostid), NULL, 0));
4086 }
4087 #endif
4088