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