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