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