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