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