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