xref: /freebsd/sys/compat/freebsd32/freebsd32_misc.c (revision 4313cc83440a39bdf976f955b1d4d3f3c4d1552f)
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_compat.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 
34 #define __ELF_WORD_SIZE 32
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/capsicum.h>
39 #include <sys/clock.h>
40 #include <sys/exec.h>
41 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/imgact.h>
44 #include <sys/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
47 #include <sys/linker.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/file.h>		/* Must come after sys/malloc.h */
51 #include <sys/imgact.h>
52 #include <sys/mbuf.h>
53 #include <sys/mman.h>
54 #include <sys/module.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/namei.h>
58 #include <sys/proc.h>
59 #include <sys/procctl.h>
60 #include <sys/reboot.h>
61 #include <sys/resource.h>
62 #include <sys/resourcevar.h>
63 #include <sys/selinfo.h>
64 #include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
65 #include <sys/pipe.h>		/* Must come after sys/selinfo.h */
66 #include <sys/signal.h>
67 #include <sys/signalvar.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/stat.h>
71 #include <sys/syscall.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
74 #include <sys/sysent.h>
75 #include <sys/sysproto.h>
76 #include <sys/systm.h>
77 #include <sys/thr.h>
78 #include <sys/unistd.h>
79 #include <sys/ucontext.h>
80 #include <sys/vnode.h>
81 #include <sys/wait.h>
82 #include <sys/ipc.h>
83 #include <sys/msg.h>
84 #include <sys/sem.h>
85 #include <sys/shm.h>
86 #include <sys/condvar.h>
87 #include <sys/sf_buf.h>
88 #include <sys/sf_sync.h>
89 #include <sys/sf_base.h>
90 
91 #ifdef INET
92 #include <netinet/in.h>
93 #endif
94 
95 #include <vm/vm.h>
96 #include <vm/vm_param.h>
97 #include <vm/pmap.h>
98 #include <vm/vm_map.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_extern.h>
101 
102 #include <machine/cpu.h>
103 #include <machine/elf.h>
104 
105 #include <security/audit/audit.h>
106 
107 #include <compat/freebsd32/freebsd32_util.h>
108 #include <compat/freebsd32/freebsd32.h>
109 #include <compat/freebsd32/freebsd32_ipc.h>
110 #include <compat/freebsd32/freebsd32_misc.h>
111 #include <compat/freebsd32/freebsd32_signal.h>
112 #include <compat/freebsd32/freebsd32_proto.h>
113 
114 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
115 
116 #ifndef __mips__
117 CTASSERT(sizeof(struct timeval32) == 8);
118 CTASSERT(sizeof(struct timespec32) == 8);
119 CTASSERT(sizeof(struct itimerval32) == 16);
120 #endif
121 CTASSERT(sizeof(struct statfs32) == 256);
122 #ifndef __mips__
123 CTASSERT(sizeof(struct rusage32) == 72);
124 #endif
125 CTASSERT(sizeof(struct sigaltstack32) == 12);
126 CTASSERT(sizeof(struct kevent32) == 20);
127 CTASSERT(sizeof(struct iovec32) == 8);
128 CTASSERT(sizeof(struct msghdr32) == 28);
129 #ifndef __mips__
130 CTASSERT(sizeof(struct stat32) == 96);
131 #endif
132 CTASSERT(sizeof(struct sigaction32) == 24);
133 
134 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
135 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
136 
137 void
138 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
139 {
140 
141 	TV_CP(*s, *s32, ru_utime);
142 	TV_CP(*s, *s32, ru_stime);
143 	CP(*s, *s32, ru_maxrss);
144 	CP(*s, *s32, ru_ixrss);
145 	CP(*s, *s32, ru_idrss);
146 	CP(*s, *s32, ru_isrss);
147 	CP(*s, *s32, ru_minflt);
148 	CP(*s, *s32, ru_majflt);
149 	CP(*s, *s32, ru_nswap);
150 	CP(*s, *s32, ru_inblock);
151 	CP(*s, *s32, ru_oublock);
152 	CP(*s, *s32, ru_msgsnd);
153 	CP(*s, *s32, ru_msgrcv);
154 	CP(*s, *s32, ru_nsignals);
155 	CP(*s, *s32, ru_nvcsw);
156 	CP(*s, *s32, ru_nivcsw);
157 }
158 
159 int
160 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
161 {
162 	int error, status;
163 	struct rusage32 ru32;
164 	struct rusage ru, *rup;
165 
166 	if (uap->rusage != NULL)
167 		rup = &ru;
168 	else
169 		rup = NULL;
170 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
171 	if (error)
172 		return (error);
173 	if (uap->status != NULL)
174 		error = copyout(&status, uap->status, sizeof(status));
175 	if (uap->rusage != NULL && error == 0) {
176 		freebsd32_rusage_out(&ru, &ru32);
177 		error = copyout(&ru32, uap->rusage, sizeof(ru32));
178 	}
179 	return (error);
180 }
181 
182 int
183 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
184 {
185 	struct wrusage32 wru32;
186 	struct __wrusage wru, *wrup;
187 	struct siginfo32 si32;
188 	struct __siginfo si, *sip;
189 	int error, status;
190 
191 	if (uap->wrusage != NULL)
192 		wrup = &wru;
193 	else
194 		wrup = NULL;
195 	if (uap->info != NULL) {
196 		sip = &si;
197 		bzero(sip, sizeof(*sip));
198 	} else
199 		sip = NULL;
200 	error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
201 	    &status, uap->options, wrup, sip);
202 	if (error != 0)
203 		return (error);
204 	if (uap->status != NULL)
205 		error = copyout(&status, uap->status, sizeof(status));
206 	if (uap->wrusage != NULL && error == 0) {
207 		freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
208 		freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
209 		error = copyout(&wru32, uap->wrusage, sizeof(wru32));
210 	}
211 	if (uap->info != NULL && error == 0) {
212 		siginfo_to_siginfo32 (&si, &si32);
213 		error = copyout(&si32, uap->info, sizeof(si32));
214 	}
215 	return (error);
216 }
217 
218 #ifdef COMPAT_FREEBSD4
219 static void
220 copy_statfs(struct statfs *in, struct statfs32 *out)
221 {
222 
223 	statfs_scale_blocks(in, INT32_MAX);
224 	bzero(out, sizeof(*out));
225 	CP(*in, *out, f_bsize);
226 	out->f_iosize = MIN(in->f_iosize, INT32_MAX);
227 	CP(*in, *out, f_blocks);
228 	CP(*in, *out, f_bfree);
229 	CP(*in, *out, f_bavail);
230 	out->f_files = MIN(in->f_files, INT32_MAX);
231 	out->f_ffree = MIN(in->f_ffree, INT32_MAX);
232 	CP(*in, *out, f_fsid);
233 	CP(*in, *out, f_owner);
234 	CP(*in, *out, f_type);
235 	CP(*in, *out, f_flags);
236 	out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
237 	out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
238 	strlcpy(out->f_fstypename,
239 	      in->f_fstypename, MFSNAMELEN);
240 	strlcpy(out->f_mntonname,
241 	      in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
242 	out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
243 	out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
244 	strlcpy(out->f_mntfromname,
245 	      in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
246 }
247 #endif
248 
249 #ifdef COMPAT_FREEBSD4
250 int
251 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
252 {
253 	struct statfs *buf, *sp;
254 	struct statfs32 stat32;
255 	size_t count, size;
256 	int error;
257 
258 	count = uap->bufsize / sizeof(struct statfs32);
259 	size = count * sizeof(struct statfs);
260 	error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
261 	if (size > 0) {
262 		count = td->td_retval[0];
263 		sp = buf;
264 		while (count > 0 && error == 0) {
265 			copy_statfs(sp, &stat32);
266 			error = copyout(&stat32, uap->buf, sizeof(stat32));
267 			sp++;
268 			uap->buf++;
269 			count--;
270 		}
271 		free(buf, M_TEMP);
272 	}
273 	return (error);
274 }
275 #endif
276 
277 int
278 freebsd32_sigaltstack(struct thread *td,
279 		      struct freebsd32_sigaltstack_args *uap)
280 {
281 	struct sigaltstack32 s32;
282 	struct sigaltstack ss, oss, *ssp;
283 	int error;
284 
285 	if (uap->ss != NULL) {
286 		error = copyin(uap->ss, &s32, sizeof(s32));
287 		if (error)
288 			return (error);
289 		PTRIN_CP(s32, ss, ss_sp);
290 		CP(s32, ss, ss_size);
291 		CP(s32, ss, ss_flags);
292 		ssp = &ss;
293 	} else
294 		ssp = NULL;
295 	error = kern_sigaltstack(td, ssp, &oss);
296 	if (error == 0 && uap->oss != NULL) {
297 		PTROUT_CP(oss, s32, ss_sp);
298 		CP(oss, s32, ss_size);
299 		CP(oss, s32, ss_flags);
300 		error = copyout(&s32, uap->oss, sizeof(s32));
301 	}
302 	return (error);
303 }
304 
305 /*
306  * Custom version of exec_copyin_args() so that we can translate
307  * the pointers.
308  */
309 int
310 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
311     enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
312 {
313 	char *argp, *envp;
314 	u_int32_t *p32, arg;
315 	size_t length;
316 	int error;
317 
318 	bzero(args, sizeof(*args));
319 	if (argv == NULL)
320 		return (EFAULT);
321 
322 	/*
323 	 * Allocate demand-paged memory for the file name, argument, and
324 	 * environment strings.
325 	 */
326 	error = exec_alloc_args(args);
327 	if (error != 0)
328 		return (error);
329 
330 	/*
331 	 * Copy the file name.
332 	 */
333 	if (fname != NULL) {
334 		args->fname = args->buf;
335 		error = (segflg == UIO_SYSSPACE) ?
336 		    copystr(fname, args->fname, PATH_MAX, &length) :
337 		    copyinstr(fname, args->fname, PATH_MAX, &length);
338 		if (error != 0)
339 			goto err_exit;
340 	} else
341 		length = 0;
342 
343 	args->begin_argv = args->buf + length;
344 	args->endp = args->begin_argv;
345 	args->stringspace = ARG_MAX;
346 
347 	/*
348 	 * extract arguments first
349 	 */
350 	p32 = argv;
351 	for (;;) {
352 		error = copyin(p32++, &arg, sizeof(arg));
353 		if (error)
354 			goto err_exit;
355 		if (arg == 0)
356 			break;
357 		argp = PTRIN(arg);
358 		error = copyinstr(argp, args->endp, args->stringspace, &length);
359 		if (error) {
360 			if (error == ENAMETOOLONG)
361 				error = E2BIG;
362 			goto err_exit;
363 		}
364 		args->stringspace -= length;
365 		args->endp += length;
366 		args->argc++;
367 	}
368 
369 	args->begin_envv = args->endp;
370 
371 	/*
372 	 * extract environment strings
373 	 */
374 	if (envv) {
375 		p32 = envv;
376 		for (;;) {
377 			error = copyin(p32++, &arg, sizeof(arg));
378 			if (error)
379 				goto err_exit;
380 			if (arg == 0)
381 				break;
382 			envp = PTRIN(arg);
383 			error = copyinstr(envp, args->endp, args->stringspace,
384 			    &length);
385 			if (error) {
386 				if (error == ENAMETOOLONG)
387 					error = E2BIG;
388 				goto err_exit;
389 			}
390 			args->stringspace -= length;
391 			args->endp += length;
392 			args->envc++;
393 		}
394 	}
395 
396 	return (0);
397 
398 err_exit:
399 	exec_free_args(args);
400 	return (error);
401 }
402 
403 int
404 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
405 {
406 	struct image_args eargs;
407 	int error;
408 
409 	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
410 	    uap->argv, uap->envv);
411 	if (error == 0)
412 		error = kern_execve(td, &eargs, NULL);
413 	return (error);
414 }
415 
416 int
417 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
418 {
419 	struct image_args eargs;
420 	int error;
421 
422 	error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
423 	    uap->argv, uap->envv);
424 	if (error == 0) {
425 		eargs.fd = uap->fd;
426 		error = kern_execve(td, &eargs, NULL);
427 	}
428 	return (error);
429 }
430 
431 #ifdef __ia64__
432 static int
433 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
434 		       int prot, int fd, off_t pos)
435 {
436 	vm_map_t map;
437 	vm_map_entry_t entry;
438 	int rv;
439 
440 	map = &td->td_proc->p_vmspace->vm_map;
441 	if (fd != -1)
442 		prot |= VM_PROT_WRITE;
443 
444 	if (vm_map_lookup_entry(map, start, &entry)) {
445 		if ((entry->protection & prot) != prot) {
446 			rv = vm_map_protect(map,
447 					    trunc_page(start),
448 					    round_page(end),
449 					    entry->protection | prot,
450 					    FALSE);
451 			if (rv != KERN_SUCCESS)
452 				return (EINVAL);
453 		}
454 	} else {
455 		vm_offset_t addr = trunc_page(start);
456 		rv = vm_map_find(map, NULL, 0, &addr, PAGE_SIZE, 0,
457 		    VMFS_NO_SPACE, prot, VM_PROT_ALL, 0);
458 		if (rv != KERN_SUCCESS)
459 			return (EINVAL);
460 	}
461 
462 	if (fd != -1) {
463 		struct pread_args r;
464 		r.fd = fd;
465 		r.buf = (void *) start;
466 		r.nbyte = end - start;
467 		r.offset = pos;
468 		return (sys_pread(td, &r));
469 	} else {
470 		while (start < end) {
471 			subyte((void *) start, 0);
472 			start++;
473 		}
474 		return (0);
475 	}
476 }
477 #endif
478 
479 int
480 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
481 {
482 	struct mprotect_args ap;
483 
484 	ap.addr = PTRIN(uap->addr);
485 	ap.len = uap->len;
486 	ap.prot = uap->prot;
487 #if defined(__amd64__) || defined(__ia64__)
488 	if (i386_read_exec && (ap.prot & PROT_READ) != 0)
489 		ap.prot |= PROT_EXEC;
490 #endif
491 	return (sys_mprotect(td, &ap));
492 }
493 
494 int
495 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
496 {
497 	struct mmap_args ap;
498 	vm_offset_t addr = (vm_offset_t) uap->addr;
499 	vm_size_t len	 = uap->len;
500 	int prot	 = uap->prot;
501 	int flags	 = uap->flags;
502 	int fd		 = uap->fd;
503 	off_t pos	 = PAIR32TO64(off_t,uap->pos);
504 #ifdef __ia64__
505 	vm_size_t pageoff;
506 	int error;
507 
508 	/*
509 	 * Attempt to handle page size hassles.
510 	 */
511 	pageoff = (pos & PAGE_MASK);
512 	if (flags & MAP_FIXED) {
513 		vm_offset_t start, end;
514 		start = addr;
515 		end = addr + len;
516 
517 		if (start != trunc_page(start)) {
518 			error = freebsd32_mmap_partial(td, start,
519 						       round_page(start), prot,
520 						       fd, pos);
521 			if (fd != -1)
522 				pos += round_page(start) - start;
523 			start = round_page(start);
524 		}
525 		if (end != round_page(end)) {
526 			vm_offset_t t = trunc_page(end);
527 			error = freebsd32_mmap_partial(td, t, end,
528 						  prot, fd,
529 						  pos + t - start);
530 			end = trunc_page(end);
531 		}
532 		if (end > start && fd != -1 && (pos & PAGE_MASK)) {
533 			/*
534 			 * We can't map this region at all. The specified
535 			 * address doesn't have the same alignment as the file
536 			 * position. Fake the mapping by simply reading the
537 			 * entire region into memory. First we need to make
538 			 * sure the region exists.
539 			 */
540 			vm_map_t map;
541 			struct pread_args r;
542 			int rv;
543 
544 			prot |= VM_PROT_WRITE;
545 			map = &td->td_proc->p_vmspace->vm_map;
546 			rv = vm_map_remove(map, start, end);
547 			if (rv != KERN_SUCCESS)
548 				return (EINVAL);
549 			rv = vm_map_find(map, NULL, 0, &start, end - start,
550 			    0, VMFS_NO_SPACE, prot, VM_PROT_ALL, 0);
551 			if (rv != KERN_SUCCESS)
552 				return (EINVAL);
553 			r.fd = fd;
554 			r.buf = (void *) start;
555 			r.nbyte = end - start;
556 			r.offset = pos;
557 			error = sys_pread(td, &r);
558 			if (error)
559 				return (error);
560 
561 			td->td_retval[0] = addr;
562 			return (0);
563 		}
564 		if (end == start) {
565 			/*
566 			 * After dealing with the ragged ends, there
567 			 * might be none left.
568 			 */
569 			td->td_retval[0] = addr;
570 			return (0);
571 		}
572 		addr = start;
573 		len = end - start;
574 	}
575 #endif
576 
577 #if defined(__amd64__) || defined(__ia64__)
578 	if (i386_read_exec && (prot & PROT_READ))
579 		prot |= PROT_EXEC;
580 #endif
581 
582 	ap.addr = (void *) addr;
583 	ap.len = len;
584 	ap.prot = prot;
585 	ap.flags = flags;
586 	ap.fd = fd;
587 	ap.pos = pos;
588 
589 	return (sys_mmap(td, &ap));
590 }
591 
592 #ifdef COMPAT_FREEBSD6
593 int
594 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
595 {
596 	struct freebsd32_mmap_args ap;
597 
598 	ap.addr = uap->addr;
599 	ap.len = uap->len;
600 	ap.prot = uap->prot;
601 	ap.flags = uap->flags;
602 	ap.fd = uap->fd;
603 	ap.pos1 = uap->pos1;
604 	ap.pos2 = uap->pos2;
605 
606 	return (freebsd32_mmap(td, &ap));
607 }
608 #endif
609 
610 int
611 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
612 {
613 	struct itimerval itv, oitv, *itvp;
614 	struct itimerval32 i32;
615 	int error;
616 
617 	if (uap->itv != NULL) {
618 		error = copyin(uap->itv, &i32, sizeof(i32));
619 		if (error)
620 			return (error);
621 		TV_CP(i32, itv, it_interval);
622 		TV_CP(i32, itv, it_value);
623 		itvp = &itv;
624 	} else
625 		itvp = NULL;
626 	error = kern_setitimer(td, uap->which, itvp, &oitv);
627 	if (error || uap->oitv == NULL)
628 		return (error);
629 	TV_CP(oitv, i32, it_interval);
630 	TV_CP(oitv, i32, it_value);
631 	return (copyout(&i32, uap->oitv, sizeof(i32)));
632 }
633 
634 int
635 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
636 {
637 	struct itimerval itv;
638 	struct itimerval32 i32;
639 	int error;
640 
641 	error = kern_getitimer(td, uap->which, &itv);
642 	if (error || uap->itv == NULL)
643 		return (error);
644 	TV_CP(itv, i32, it_interval);
645 	TV_CP(itv, i32, it_value);
646 	return (copyout(&i32, uap->itv, sizeof(i32)));
647 }
648 
649 int
650 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
651 {
652 	struct timeval32 tv32;
653 	struct timeval tv, *tvp;
654 	int error;
655 
656 	if (uap->tv != NULL) {
657 		error = copyin(uap->tv, &tv32, sizeof(tv32));
658 		if (error)
659 			return (error);
660 		CP(tv32, tv, tv_sec);
661 		CP(tv32, tv, tv_usec);
662 		tvp = &tv;
663 	} else
664 		tvp = NULL;
665 	/*
666 	 * XXX Do pointers need PTRIN()?
667 	 */
668 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
669 	    sizeof(int32_t) * 8));
670 }
671 
672 int
673 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
674 {
675 	struct timespec32 ts32;
676 	struct timespec ts;
677 	struct timeval tv, *tvp;
678 	sigset_t set, *uset;
679 	int error;
680 
681 	if (uap->ts != NULL) {
682 		error = copyin(uap->ts, &ts32, sizeof(ts32));
683 		if (error != 0)
684 			return (error);
685 		CP(ts32, ts, tv_sec);
686 		CP(ts32, ts, tv_nsec);
687 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
688 		tvp = &tv;
689 	} else
690 		tvp = NULL;
691 	if (uap->sm != NULL) {
692 		error = copyin(uap->sm, &set, sizeof(set));
693 		if (error != 0)
694 			return (error);
695 		uset = &set;
696 	} else
697 		uset = NULL;
698 	/*
699 	 * XXX Do pointers need PTRIN()?
700 	 */
701 	error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
702 	    uset, sizeof(int32_t) * 8);
703 	return (error);
704 }
705 
706 /*
707  * Copy 'count' items into the destination list pointed to by uap->eventlist.
708  */
709 static int
710 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
711 {
712 	struct freebsd32_kevent_args *uap;
713 	struct kevent32	ks32[KQ_NEVENTS];
714 	int i, error = 0;
715 
716 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
717 	uap = (struct freebsd32_kevent_args *)arg;
718 
719 	for (i = 0; i < count; i++) {
720 		CP(kevp[i], ks32[i], ident);
721 		CP(kevp[i], ks32[i], filter);
722 		CP(kevp[i], ks32[i], flags);
723 		CP(kevp[i], ks32[i], fflags);
724 		CP(kevp[i], ks32[i], data);
725 		PTROUT_CP(kevp[i], ks32[i], udata);
726 	}
727 	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
728 	if (error == 0)
729 		uap->eventlist += count;
730 	return (error);
731 }
732 
733 /*
734  * Copy 'count' items from the list pointed to by uap->changelist.
735  */
736 static int
737 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
738 {
739 	struct freebsd32_kevent_args *uap;
740 	struct kevent32	ks32[KQ_NEVENTS];
741 	int i, error = 0;
742 
743 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
744 	uap = (struct freebsd32_kevent_args *)arg;
745 
746 	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
747 	if (error)
748 		goto done;
749 	uap->changelist += count;
750 
751 	for (i = 0; i < count; i++) {
752 		CP(ks32[i], kevp[i], ident);
753 		CP(ks32[i], kevp[i], filter);
754 		CP(ks32[i], kevp[i], flags);
755 		CP(ks32[i], kevp[i], fflags);
756 		CP(ks32[i], kevp[i], data);
757 		PTRIN_CP(ks32[i], kevp[i], udata);
758 	}
759 done:
760 	return (error);
761 }
762 
763 int
764 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
765 {
766 	struct timespec32 ts32;
767 	struct timespec ts, *tsp;
768 	struct kevent_copyops k_ops = { uap,
769 					freebsd32_kevent_copyout,
770 					freebsd32_kevent_copyin};
771 	int error;
772 
773 
774 	if (uap->timeout) {
775 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
776 		if (error)
777 			return (error);
778 		CP(ts32, ts, tv_sec);
779 		CP(ts32, ts, tv_nsec);
780 		tsp = &ts;
781 	} else
782 		tsp = NULL;
783 	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
784 	    &k_ops, tsp);
785 	return (error);
786 }
787 
788 int
789 freebsd32_gettimeofday(struct thread *td,
790 		       struct freebsd32_gettimeofday_args *uap)
791 {
792 	struct timeval atv;
793 	struct timeval32 atv32;
794 	struct timezone rtz;
795 	int error = 0;
796 
797 	if (uap->tp) {
798 		microtime(&atv);
799 		CP(atv, atv32, tv_sec);
800 		CP(atv, atv32, tv_usec);
801 		error = copyout(&atv32, uap->tp, sizeof (atv32));
802 	}
803 	if (error == 0 && uap->tzp != NULL) {
804 		rtz.tz_minuteswest = tz_minuteswest;
805 		rtz.tz_dsttime = tz_dsttime;
806 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
807 	}
808 	return (error);
809 }
810 
811 int
812 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
813 {
814 	struct rusage32 s32;
815 	struct rusage s;
816 	int error;
817 
818 	error = kern_getrusage(td, uap->who, &s);
819 	if (error)
820 		return (error);
821 	if (uap->rusage != NULL) {
822 		freebsd32_rusage_out(&s, &s32);
823 		error = copyout(&s32, uap->rusage, sizeof(s32));
824 	}
825 	return (error);
826 }
827 
828 static int
829 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
830 {
831 	struct iovec32 iov32;
832 	struct iovec *iov;
833 	struct uio *uio;
834 	u_int iovlen;
835 	int error, i;
836 
837 	*uiop = NULL;
838 	if (iovcnt > UIO_MAXIOV)
839 		return (EINVAL);
840 	iovlen = iovcnt * sizeof(struct iovec);
841 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
842 	iov = (struct iovec *)(uio + 1);
843 	for (i = 0; i < iovcnt; i++) {
844 		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
845 		if (error) {
846 			free(uio, M_IOV);
847 			return (error);
848 		}
849 		iov[i].iov_base = PTRIN(iov32.iov_base);
850 		iov[i].iov_len = iov32.iov_len;
851 	}
852 	uio->uio_iov = iov;
853 	uio->uio_iovcnt = iovcnt;
854 	uio->uio_segflg = UIO_USERSPACE;
855 	uio->uio_offset = -1;
856 	uio->uio_resid = 0;
857 	for (i = 0; i < iovcnt; i++) {
858 		if (iov->iov_len > INT_MAX - uio->uio_resid) {
859 			free(uio, M_IOV);
860 			return (EINVAL);
861 		}
862 		uio->uio_resid += iov->iov_len;
863 		iov++;
864 	}
865 	*uiop = uio;
866 	return (0);
867 }
868 
869 int
870 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
871 {
872 	struct uio *auio;
873 	int error;
874 
875 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
876 	if (error)
877 		return (error);
878 	error = kern_readv(td, uap->fd, auio);
879 	free(auio, M_IOV);
880 	return (error);
881 }
882 
883 int
884 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
885 {
886 	struct uio *auio;
887 	int error;
888 
889 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
890 	if (error)
891 		return (error);
892 	error = kern_writev(td, uap->fd, auio);
893 	free(auio, M_IOV);
894 	return (error);
895 }
896 
897 int
898 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
899 {
900 	struct uio *auio;
901 	int error;
902 
903 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
904 	if (error)
905 		return (error);
906 	error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
907 	free(auio, M_IOV);
908 	return (error);
909 }
910 
911 int
912 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
913 {
914 	struct uio *auio;
915 	int error;
916 
917 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
918 	if (error)
919 		return (error);
920 	error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
921 	free(auio, M_IOV);
922 	return (error);
923 }
924 
925 int
926 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
927     int error)
928 {
929 	struct iovec32 iov32;
930 	struct iovec *iov;
931 	u_int iovlen;
932 	int i;
933 
934 	*iovp = NULL;
935 	if (iovcnt > UIO_MAXIOV)
936 		return (error);
937 	iovlen = iovcnt * sizeof(struct iovec);
938 	iov = malloc(iovlen, M_IOV, M_WAITOK);
939 	for (i = 0; i < iovcnt; i++) {
940 		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
941 		if (error) {
942 			free(iov, M_IOV);
943 			return (error);
944 		}
945 		iov[i].iov_base = PTRIN(iov32.iov_base);
946 		iov[i].iov_len = iov32.iov_len;
947 	}
948 	*iovp = iov;
949 	return (0);
950 }
951 
952 static int
953 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
954 {
955 	struct msghdr32 m32;
956 	int error;
957 
958 	error = copyin(msg32, &m32, sizeof(m32));
959 	if (error)
960 		return (error);
961 	msg->msg_name = PTRIN(m32.msg_name);
962 	msg->msg_namelen = m32.msg_namelen;
963 	msg->msg_iov = PTRIN(m32.msg_iov);
964 	msg->msg_iovlen = m32.msg_iovlen;
965 	msg->msg_control = PTRIN(m32.msg_control);
966 	msg->msg_controllen = m32.msg_controllen;
967 	msg->msg_flags = m32.msg_flags;
968 	return (0);
969 }
970 
971 static int
972 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
973 {
974 	struct msghdr32 m32;
975 	int error;
976 
977 	m32.msg_name = PTROUT(msg->msg_name);
978 	m32.msg_namelen = msg->msg_namelen;
979 	m32.msg_iov = PTROUT(msg->msg_iov);
980 	m32.msg_iovlen = msg->msg_iovlen;
981 	m32.msg_control = PTROUT(msg->msg_control);
982 	m32.msg_controllen = msg->msg_controllen;
983 	m32.msg_flags = msg->msg_flags;
984 	error = copyout(&m32, msg32, sizeof(m32));
985 	return (error);
986 }
987 
988 #ifndef __mips__
989 #define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
990 #else
991 #define FREEBSD32_ALIGNBYTES	(sizeof(long) - 1)
992 #endif
993 #define FREEBSD32_ALIGN(p)	\
994 	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
995 #define	FREEBSD32_CMSG_SPACE(l)	\
996 	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
997 
998 #define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
999 				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1000 static int
1001 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1002 {
1003 	struct cmsghdr *cm;
1004 	void *data;
1005 	socklen_t clen, datalen;
1006 	int error;
1007 	caddr_t ctlbuf;
1008 	int len, maxlen, copylen;
1009 	struct mbuf *m;
1010 	error = 0;
1011 
1012 	len    = msg->msg_controllen;
1013 	maxlen = msg->msg_controllen;
1014 	msg->msg_controllen = 0;
1015 
1016 	m = control;
1017 	ctlbuf = msg->msg_control;
1018 
1019 	while (m && len > 0) {
1020 		cm = mtod(m, struct cmsghdr *);
1021 		clen = m->m_len;
1022 
1023 		while (cm != NULL) {
1024 
1025 			if (sizeof(struct cmsghdr) > clen ||
1026 			    cm->cmsg_len > clen) {
1027 				error = EINVAL;
1028 				break;
1029 			}
1030 
1031 			data   = CMSG_DATA(cm);
1032 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1033 
1034 			/* Adjust message length */
1035 			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1036 			    datalen;
1037 
1038 
1039 			/* Copy cmsghdr */
1040 			copylen = sizeof(struct cmsghdr);
1041 			if (len < copylen) {
1042 				msg->msg_flags |= MSG_CTRUNC;
1043 				copylen = len;
1044 			}
1045 
1046 			error = copyout(cm,ctlbuf,copylen);
1047 			if (error)
1048 				goto exit;
1049 
1050 			ctlbuf += FREEBSD32_ALIGN(copylen);
1051 			len    -= FREEBSD32_ALIGN(copylen);
1052 
1053 			if (len <= 0)
1054 				break;
1055 
1056 			/* Copy data */
1057 			copylen = datalen;
1058 			if (len < copylen) {
1059 				msg->msg_flags |= MSG_CTRUNC;
1060 				copylen = len;
1061 			}
1062 
1063 			error = copyout(data,ctlbuf,copylen);
1064 			if (error)
1065 				goto exit;
1066 
1067 			ctlbuf += FREEBSD32_ALIGN(copylen);
1068 			len    -= FREEBSD32_ALIGN(copylen);
1069 
1070 			if (CMSG_SPACE(datalen) < clen) {
1071 				clen -= CMSG_SPACE(datalen);
1072 				cm = (struct cmsghdr *)
1073 					((caddr_t)cm + CMSG_SPACE(datalen));
1074 			} else {
1075 				clen = 0;
1076 				cm = NULL;
1077 			}
1078 		}
1079 		m = m->m_next;
1080 	}
1081 
1082 	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
1083 
1084 exit:
1085 	return (error);
1086 
1087 }
1088 
1089 int
1090 freebsd32_recvmsg(td, uap)
1091 	struct thread *td;
1092 	struct freebsd32_recvmsg_args /* {
1093 		int	s;
1094 		struct	msghdr32 *msg;
1095 		int	flags;
1096 	} */ *uap;
1097 {
1098 	struct msghdr msg;
1099 	struct msghdr32 m32;
1100 	struct iovec *uiov, *iov;
1101 	struct mbuf *control = NULL;
1102 	struct mbuf **controlp;
1103 
1104 	int error;
1105 	error = copyin(uap->msg, &m32, sizeof(m32));
1106 	if (error)
1107 		return (error);
1108 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1109 	if (error)
1110 		return (error);
1111 	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1112 	    EMSGSIZE);
1113 	if (error)
1114 		return (error);
1115 	msg.msg_flags = uap->flags;
1116 	uiov = msg.msg_iov;
1117 	msg.msg_iov = iov;
1118 
1119 	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1120 	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1121 	if (error == 0) {
1122 		msg.msg_iov = uiov;
1123 
1124 		if (control != NULL)
1125 			error = freebsd32_copy_msg_out(&msg, control);
1126 		else
1127 			msg.msg_controllen = 0;
1128 
1129 		if (error == 0)
1130 			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1131 	}
1132 	free(iov, M_IOV);
1133 
1134 	if (control != NULL)
1135 		m_freem(control);
1136 
1137 	return (error);
1138 }
1139 
1140 /*
1141  * Copy-in the array of control messages constructed using alignment
1142  * and padding suitable for a 32-bit environment and construct an
1143  * mbuf using alignment and padding suitable for a 64-bit kernel.
1144  * The alignment and padding are defined indirectly by CMSG_DATA(),
1145  * CMSG_SPACE() and CMSG_LEN().
1146  */
1147 static int
1148 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1149 {
1150 	struct mbuf *m;
1151 	void *md;
1152 	u_int idx, len, msglen;
1153 	int error;
1154 
1155 	buflen = FREEBSD32_ALIGN(buflen);
1156 
1157 	if (buflen > MCLBYTES)
1158 		return (EINVAL);
1159 
1160 	/*
1161 	 * Iterate over the buffer and get the length of each message
1162 	 * in there. This has 32-bit alignment and padding. Use it to
1163 	 * determine the length of these messages when using 64-bit
1164 	 * alignment and padding.
1165 	 */
1166 	idx = 0;
1167 	len = 0;
1168 	while (idx < buflen) {
1169 		error = copyin(buf + idx, &msglen, sizeof(msglen));
1170 		if (error)
1171 			return (error);
1172 		if (msglen < sizeof(struct cmsghdr))
1173 			return (EINVAL);
1174 		msglen = FREEBSD32_ALIGN(msglen);
1175 		if (idx + msglen > buflen)
1176 			return (EINVAL);
1177 		idx += msglen;
1178 		msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1179 		    FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1180 		len += CMSG_ALIGN(msglen);
1181 	}
1182 
1183 	if (len > MCLBYTES)
1184 		return (EINVAL);
1185 
1186 	m = m_get(M_WAITOK, MT_CONTROL);
1187 	if (len > MLEN)
1188 		MCLGET(m, M_WAITOK);
1189 	m->m_len = len;
1190 
1191 	md = mtod(m, void *);
1192 	while (buflen > 0) {
1193 		error = copyin(buf, md, sizeof(struct cmsghdr));
1194 		if (error)
1195 			break;
1196 		msglen = *(u_int *)md;
1197 		msglen = FREEBSD32_ALIGN(msglen);
1198 
1199 		/* Modify the message length to account for alignment. */
1200 		*(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1201 		    FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1202 
1203 		md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1204 		buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1205 		buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1206 
1207 		msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1208 		if (msglen > 0) {
1209 			error = copyin(buf, md, msglen);
1210 			if (error)
1211 				break;
1212 			md = (char *)md + CMSG_ALIGN(msglen);
1213 			buf += msglen;
1214 			buflen -= msglen;
1215 		}
1216 	}
1217 
1218 	if (error)
1219 		m_free(m);
1220 	else
1221 		*mp = m;
1222 	return (error);
1223 }
1224 
1225 int
1226 freebsd32_sendmsg(struct thread *td,
1227 		  struct freebsd32_sendmsg_args *uap)
1228 {
1229 	struct msghdr msg;
1230 	struct msghdr32 m32;
1231 	struct iovec *iov;
1232 	struct mbuf *control = NULL;
1233 	struct sockaddr *to = NULL;
1234 	int error;
1235 
1236 	error = copyin(uap->msg, &m32, sizeof(m32));
1237 	if (error)
1238 		return (error);
1239 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1240 	if (error)
1241 		return (error);
1242 	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1243 	    EMSGSIZE);
1244 	if (error)
1245 		return (error);
1246 	msg.msg_iov = iov;
1247 	if (msg.msg_name != NULL) {
1248 		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1249 		if (error) {
1250 			to = NULL;
1251 			goto out;
1252 		}
1253 		msg.msg_name = to;
1254 	}
1255 
1256 	if (msg.msg_control) {
1257 		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1258 			error = EINVAL;
1259 			goto out;
1260 		}
1261 
1262 		error = freebsd32_copyin_control(&control, msg.msg_control,
1263 		    msg.msg_controllen);
1264 		if (error)
1265 			goto out;
1266 
1267 		msg.msg_control = NULL;
1268 		msg.msg_controllen = 0;
1269 	}
1270 
1271 	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1272 	    UIO_USERSPACE);
1273 
1274 out:
1275 	free(iov, M_IOV);
1276 	if (to)
1277 		free(to, M_SONAME);
1278 	return (error);
1279 }
1280 
1281 int
1282 freebsd32_recvfrom(struct thread *td,
1283 		   struct freebsd32_recvfrom_args *uap)
1284 {
1285 	struct msghdr msg;
1286 	struct iovec aiov;
1287 	int error;
1288 
1289 	if (uap->fromlenaddr) {
1290 		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1291 		    sizeof(msg.msg_namelen));
1292 		if (error)
1293 			return (error);
1294 	} else {
1295 		msg.msg_namelen = 0;
1296 	}
1297 
1298 	msg.msg_name = PTRIN(uap->from);
1299 	msg.msg_iov = &aiov;
1300 	msg.msg_iovlen = 1;
1301 	aiov.iov_base = PTRIN(uap->buf);
1302 	aiov.iov_len = uap->len;
1303 	msg.msg_control = NULL;
1304 	msg.msg_flags = uap->flags;
1305 	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1306 	if (error == 0 && uap->fromlenaddr)
1307 		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1308 		    sizeof (msg.msg_namelen));
1309 	return (error);
1310 }
1311 
1312 int
1313 freebsd32_settimeofday(struct thread *td,
1314 		       struct freebsd32_settimeofday_args *uap)
1315 {
1316 	struct timeval32 tv32;
1317 	struct timeval tv, *tvp;
1318 	struct timezone tz, *tzp;
1319 	int error;
1320 
1321 	if (uap->tv) {
1322 		error = copyin(uap->tv, &tv32, sizeof(tv32));
1323 		if (error)
1324 			return (error);
1325 		CP(tv32, tv, tv_sec);
1326 		CP(tv32, tv, tv_usec);
1327 		tvp = &tv;
1328 	} else
1329 		tvp = NULL;
1330 	if (uap->tzp) {
1331 		error = copyin(uap->tzp, &tz, sizeof(tz));
1332 		if (error)
1333 			return (error);
1334 		tzp = &tz;
1335 	} else
1336 		tzp = NULL;
1337 	return (kern_settimeofday(td, tvp, tzp));
1338 }
1339 
1340 int
1341 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1342 {
1343 	struct timeval32 s32[2];
1344 	struct timeval s[2], *sp;
1345 	int error;
1346 
1347 	if (uap->tptr != NULL) {
1348 		error = copyin(uap->tptr, s32, sizeof(s32));
1349 		if (error)
1350 			return (error);
1351 		CP(s32[0], s[0], tv_sec);
1352 		CP(s32[0], s[0], tv_usec);
1353 		CP(s32[1], s[1], tv_sec);
1354 		CP(s32[1], s[1], tv_usec);
1355 		sp = s;
1356 	} else
1357 		sp = NULL;
1358 	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1359 }
1360 
1361 int
1362 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1363 {
1364 	struct timeval32 s32[2];
1365 	struct timeval s[2], *sp;
1366 	int error;
1367 
1368 	if (uap->tptr != NULL) {
1369 		error = copyin(uap->tptr, s32, sizeof(s32));
1370 		if (error)
1371 			return (error);
1372 		CP(s32[0], s[0], tv_sec);
1373 		CP(s32[0], s[0], tv_usec);
1374 		CP(s32[1], s[1], tv_sec);
1375 		CP(s32[1], s[1], tv_usec);
1376 		sp = s;
1377 	} else
1378 		sp = NULL;
1379 	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1380 }
1381 
1382 int
1383 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1384 {
1385 	struct timeval32 s32[2];
1386 	struct timeval s[2], *sp;
1387 	int error;
1388 
1389 	if (uap->tptr != NULL) {
1390 		error = copyin(uap->tptr, s32, sizeof(s32));
1391 		if (error)
1392 			return (error);
1393 		CP(s32[0], s[0], tv_sec);
1394 		CP(s32[0], s[0], tv_usec);
1395 		CP(s32[1], s[1], tv_sec);
1396 		CP(s32[1], s[1], tv_usec);
1397 		sp = s;
1398 	} else
1399 		sp = NULL;
1400 	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1401 }
1402 
1403 int
1404 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1405 {
1406 	struct timeval32 s32[2];
1407 	struct timeval s[2], *sp;
1408 	int error;
1409 
1410 	if (uap->times != NULL) {
1411 		error = copyin(uap->times, s32, sizeof(s32));
1412 		if (error)
1413 			return (error);
1414 		CP(s32[0], s[0], tv_sec);
1415 		CP(s32[0], s[0], tv_usec);
1416 		CP(s32[1], s[1], tv_sec);
1417 		CP(s32[1], s[1], tv_usec);
1418 		sp = s;
1419 	} else
1420 		sp = NULL;
1421 	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1422 		sp, UIO_SYSSPACE));
1423 }
1424 
1425 int
1426 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1427 {
1428 	struct timeval32 tv32;
1429 	struct timeval delta, olddelta, *deltap;
1430 	int error;
1431 
1432 	if (uap->delta) {
1433 		error = copyin(uap->delta, &tv32, sizeof(tv32));
1434 		if (error)
1435 			return (error);
1436 		CP(tv32, delta, tv_sec);
1437 		CP(tv32, delta, tv_usec);
1438 		deltap = &delta;
1439 	} else
1440 		deltap = NULL;
1441 	error = kern_adjtime(td, deltap, &olddelta);
1442 	if (uap->olddelta && error == 0) {
1443 		CP(olddelta, tv32, tv_sec);
1444 		CP(olddelta, tv32, tv_usec);
1445 		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1446 	}
1447 	return (error);
1448 }
1449 
1450 #ifdef COMPAT_FREEBSD4
1451 int
1452 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1453 {
1454 	struct statfs32 s32;
1455 	struct statfs s;
1456 	int error;
1457 
1458 	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1459 	if (error)
1460 		return (error);
1461 	copy_statfs(&s, &s32);
1462 	return (copyout(&s32, uap->buf, sizeof(s32)));
1463 }
1464 #endif
1465 
1466 #ifdef COMPAT_FREEBSD4
1467 int
1468 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1469 {
1470 	struct statfs32 s32;
1471 	struct statfs s;
1472 	int error;
1473 
1474 	error = kern_fstatfs(td, uap->fd, &s);
1475 	if (error)
1476 		return (error);
1477 	copy_statfs(&s, &s32);
1478 	return (copyout(&s32, uap->buf, sizeof(s32)));
1479 }
1480 #endif
1481 
1482 #ifdef COMPAT_FREEBSD4
1483 int
1484 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1485 {
1486 	struct statfs32 s32;
1487 	struct statfs s;
1488 	fhandle_t fh;
1489 	int error;
1490 
1491 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1492 		return (error);
1493 	error = kern_fhstatfs(td, fh, &s);
1494 	if (error)
1495 		return (error);
1496 	copy_statfs(&s, &s32);
1497 	return (copyout(&s32, uap->buf, sizeof(s32)));
1498 }
1499 #endif
1500 
1501 int
1502 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1503 {
1504 	struct pread_args ap;
1505 
1506 	ap.fd = uap->fd;
1507 	ap.buf = uap->buf;
1508 	ap.nbyte = uap->nbyte;
1509 	ap.offset = PAIR32TO64(off_t,uap->offset);
1510 	return (sys_pread(td, &ap));
1511 }
1512 
1513 int
1514 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1515 {
1516 	struct pwrite_args ap;
1517 
1518 	ap.fd = uap->fd;
1519 	ap.buf = uap->buf;
1520 	ap.nbyte = uap->nbyte;
1521 	ap.offset = PAIR32TO64(off_t,uap->offset);
1522 	return (sys_pwrite(td, &ap));
1523 }
1524 
1525 #ifdef COMPAT_43
1526 int
1527 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1528 {
1529 	struct lseek_args nuap;
1530 
1531 	nuap.fd = uap->fd;
1532 	nuap.offset = uap->offset;
1533 	nuap.whence = uap->whence;
1534 	return (sys_lseek(td, &nuap));
1535 }
1536 #endif
1537 
1538 int
1539 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1540 {
1541 	int error;
1542 	struct lseek_args ap;
1543 	off_t pos;
1544 
1545 	ap.fd = uap->fd;
1546 	ap.offset = PAIR32TO64(off_t,uap->offset);
1547 	ap.whence = uap->whence;
1548 	error = sys_lseek(td, &ap);
1549 	/* Expand the quad return into two parts for eax and edx */
1550 	pos = td->td_uretoff.tdu_off;
1551 	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1552 	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1553 	return error;
1554 }
1555 
1556 int
1557 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1558 {
1559 	struct truncate_args ap;
1560 
1561 	ap.path = uap->path;
1562 	ap.length = PAIR32TO64(off_t,uap->length);
1563 	return (sys_truncate(td, &ap));
1564 }
1565 
1566 int
1567 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1568 {
1569 	struct ftruncate_args ap;
1570 
1571 	ap.fd = uap->fd;
1572 	ap.length = PAIR32TO64(off_t,uap->length);
1573 	return (sys_ftruncate(td, &ap));
1574 }
1575 
1576 #ifdef COMPAT_43
1577 int
1578 ofreebsd32_getdirentries(struct thread *td,
1579     struct ofreebsd32_getdirentries_args *uap)
1580 {
1581 	struct ogetdirentries_args ap;
1582 	int error;
1583 	long loff;
1584 	int32_t loff_cut;
1585 
1586 	ap.fd = uap->fd;
1587 	ap.buf = uap->buf;
1588 	ap.count = uap->count;
1589 	ap.basep = NULL;
1590 	error = kern_ogetdirentries(td, &ap, &loff);
1591 	if (error == 0) {
1592 		loff_cut = loff;
1593 		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1594 	}
1595 	return (error);
1596 }
1597 #endif
1598 
1599 int
1600 freebsd32_getdirentries(struct thread *td,
1601     struct freebsd32_getdirentries_args *uap)
1602 {
1603 	long base;
1604 	int32_t base32;
1605 	int error;
1606 
1607 	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1608 	    NULL, UIO_USERSPACE);
1609 	if (error)
1610 		return (error);
1611 	if (uap->basep != NULL) {
1612 		base32 = base;
1613 		error = copyout(&base32, uap->basep, sizeof(int32_t));
1614 	}
1615 	return (error);
1616 }
1617 
1618 #ifdef COMPAT_FREEBSD6
1619 /* versions with the 'int pad' argument */
1620 int
1621 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1622 {
1623 	struct pread_args ap;
1624 
1625 	ap.fd = uap->fd;
1626 	ap.buf = uap->buf;
1627 	ap.nbyte = uap->nbyte;
1628 	ap.offset = PAIR32TO64(off_t,uap->offset);
1629 	return (sys_pread(td, &ap));
1630 }
1631 
1632 int
1633 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1634 {
1635 	struct pwrite_args ap;
1636 
1637 	ap.fd = uap->fd;
1638 	ap.buf = uap->buf;
1639 	ap.nbyte = uap->nbyte;
1640 	ap.offset = PAIR32TO64(off_t,uap->offset);
1641 	return (sys_pwrite(td, &ap));
1642 }
1643 
1644 int
1645 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1646 {
1647 	int error;
1648 	struct lseek_args ap;
1649 	off_t pos;
1650 
1651 	ap.fd = uap->fd;
1652 	ap.offset = PAIR32TO64(off_t,uap->offset);
1653 	ap.whence = uap->whence;
1654 	error = sys_lseek(td, &ap);
1655 	/* Expand the quad return into two parts for eax and edx */
1656 	pos = *(off_t *)(td->td_retval);
1657 	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1658 	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1659 	return error;
1660 }
1661 
1662 int
1663 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1664 {
1665 	struct truncate_args ap;
1666 
1667 	ap.path = uap->path;
1668 	ap.length = PAIR32TO64(off_t,uap->length);
1669 	return (sys_truncate(td, &ap));
1670 }
1671 
1672 int
1673 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1674 {
1675 	struct ftruncate_args ap;
1676 
1677 	ap.fd = uap->fd;
1678 	ap.length = PAIR32TO64(off_t,uap->length);
1679 	return (sys_ftruncate(td, &ap));
1680 }
1681 #endif /* COMPAT_FREEBSD6 */
1682 
1683 struct sf_hdtr32 {
1684 	uint32_t headers;
1685 	int hdr_cnt;
1686 	uint32_t trailers;
1687 	int trl_cnt;
1688 };
1689 
1690 struct sf_hdtr_kq32 {
1691 	int kq_fd;
1692 	uint32_t kq_flags;
1693 	uint32_t kq_udata;	/* 32-bit void ptr */
1694 	uint32_t kq_ident;	/* 32-bit uintptr_t */
1695 };
1696 
1697 static int
1698 freebsd32_do_sendfile(struct thread *td,
1699     struct freebsd32_sendfile_args *uap, int compat)
1700 {
1701 	struct sf_hdtr32 hdtr32;
1702 	struct sf_hdtr hdtr;
1703 	struct sf_hdtr_kq32 hdtr_kq32;
1704 	struct sf_hdtr_kq hdtr_kq;
1705 	struct uio *hdr_uio, *trl_uio;
1706 	struct iovec32 *iov32;
1707 	off_t offset;
1708 	int error;
1709 	off_t sbytes;
1710 
1711 	offset = PAIR32TO64(off_t, uap->offset);
1712 	if (offset < 0)
1713 		return (EINVAL);
1714 
1715 	hdr_uio = trl_uio = NULL;
1716 
1717 	if (uap->hdtr != NULL) {
1718 		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1719 		if (error)
1720 			goto out;
1721 		PTRIN_CP(hdtr32, hdtr, headers);
1722 		CP(hdtr32, hdtr, hdr_cnt);
1723 		PTRIN_CP(hdtr32, hdtr, trailers);
1724 		CP(hdtr32, hdtr, trl_cnt);
1725 
1726 		if (hdtr.headers != NULL) {
1727 			iov32 = PTRIN(hdtr32.headers);
1728 			error = freebsd32_copyinuio(iov32,
1729 			    hdtr32.hdr_cnt, &hdr_uio);
1730 			if (error)
1731 				goto out;
1732 		}
1733 		if (hdtr.trailers != NULL) {
1734 			iov32 = PTRIN(hdtr32.trailers);
1735 			error = freebsd32_copyinuio(iov32,
1736 			    hdtr32.trl_cnt, &trl_uio);
1737 			if (error)
1738 				goto out;
1739 		}
1740 
1741 		/*
1742 		 * If SF_KQUEUE is set, then we need to also copy in
1743 		 * the kqueue data after the normal hdtr set and set do_kqueue=1.
1744 		 */
1745 		if (uap->flags & SF_KQUEUE) {
1746 			error = copyin(((char *) uap->hdtr) + sizeof(hdtr32),
1747 			    &hdtr_kq32,
1748 			    sizeof(hdtr_kq32));
1749 			if (error != 0)
1750 				goto out;
1751 
1752 			/* 32->64 bit fields */
1753 			CP(hdtr_kq32, hdtr_kq, kq_fd);
1754 			CP(hdtr_kq32, hdtr_kq, kq_flags);
1755 			PTRIN_CP(hdtr_kq32, hdtr_kq, kq_udata);
1756 			CP(hdtr_kq32, hdtr_kq, kq_ident);
1757 		}
1758 	}
1759 
1760 
1761 	/* Call sendfile */
1762 	/* XXX stack depth! */
1763 	error = _do_sendfile(td, uap->fd, uap->s, uap->flags, compat,
1764 	    offset, uap->nbytes, &sbytes, hdr_uio, trl_uio, &hdtr_kq);
1765 
1766 	if (uap->sbytes != NULL)
1767 		copyout(&sbytes, uap->sbytes, sizeof(off_t));
1768 
1769 out:
1770 	if (hdr_uio)
1771 		free(hdr_uio, M_IOV);
1772 	if (trl_uio)
1773 		free(trl_uio, M_IOV);
1774 	return (error);
1775 }
1776 
1777 #ifdef COMPAT_FREEBSD4
1778 int
1779 freebsd4_freebsd32_sendfile(struct thread *td,
1780     struct freebsd4_freebsd32_sendfile_args *uap)
1781 {
1782 	return (freebsd32_do_sendfile(td,
1783 	    (struct freebsd32_sendfile_args *)uap, 1));
1784 }
1785 #endif
1786 
1787 int
1788 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1789 {
1790 
1791 	return (freebsd32_do_sendfile(td, uap, 0));
1792 }
1793 
1794 static void
1795 copy_stat(struct stat *in, struct stat32 *out)
1796 {
1797 
1798 	CP(*in, *out, st_dev);
1799 	CP(*in, *out, st_ino);
1800 	CP(*in, *out, st_mode);
1801 	CP(*in, *out, st_nlink);
1802 	CP(*in, *out, st_uid);
1803 	CP(*in, *out, st_gid);
1804 	CP(*in, *out, st_rdev);
1805 	TS_CP(*in, *out, st_atim);
1806 	TS_CP(*in, *out, st_mtim);
1807 	TS_CP(*in, *out, st_ctim);
1808 	CP(*in, *out, st_size);
1809 	CP(*in, *out, st_blocks);
1810 	CP(*in, *out, st_blksize);
1811 	CP(*in, *out, st_flags);
1812 	CP(*in, *out, st_gen);
1813 	TS_CP(*in, *out, st_birthtim);
1814 }
1815 
1816 #ifdef COMPAT_43
1817 static void
1818 copy_ostat(struct stat *in, struct ostat32 *out)
1819 {
1820 
1821 	CP(*in, *out, st_dev);
1822 	CP(*in, *out, st_ino);
1823 	CP(*in, *out, st_mode);
1824 	CP(*in, *out, st_nlink);
1825 	CP(*in, *out, st_uid);
1826 	CP(*in, *out, st_gid);
1827 	CP(*in, *out, st_rdev);
1828 	CP(*in, *out, st_size);
1829 	TS_CP(*in, *out, st_atim);
1830 	TS_CP(*in, *out, st_mtim);
1831 	TS_CP(*in, *out, st_ctim);
1832 	CP(*in, *out, st_blksize);
1833 	CP(*in, *out, st_blocks);
1834 	CP(*in, *out, st_flags);
1835 	CP(*in, *out, st_gen);
1836 }
1837 #endif
1838 
1839 int
1840 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1841 {
1842 	struct stat sb;
1843 	struct stat32 sb32;
1844 	int error;
1845 
1846 	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1847 	if (error)
1848 		return (error);
1849 	copy_stat(&sb, &sb32);
1850 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1851 	return (error);
1852 }
1853 
1854 #ifdef COMPAT_43
1855 int
1856 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1857 {
1858 	struct stat sb;
1859 	struct ostat32 sb32;
1860 	int error;
1861 
1862 	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1863 	if (error)
1864 		return (error);
1865 	copy_ostat(&sb, &sb32);
1866 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1867 	return (error);
1868 }
1869 #endif
1870 
1871 int
1872 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1873 {
1874 	struct stat ub;
1875 	struct stat32 ub32;
1876 	int error;
1877 
1878 	error = kern_fstat(td, uap->fd, &ub);
1879 	if (error)
1880 		return (error);
1881 	copy_stat(&ub, &ub32);
1882 	error = copyout(&ub32, uap->ub, sizeof(ub32));
1883 	return (error);
1884 }
1885 
1886 #ifdef COMPAT_43
1887 int
1888 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1889 {
1890 	struct stat ub;
1891 	struct ostat32 ub32;
1892 	int error;
1893 
1894 	error = kern_fstat(td, uap->fd, &ub);
1895 	if (error)
1896 		return (error);
1897 	copy_ostat(&ub, &ub32);
1898 	error = copyout(&ub32, uap->ub, sizeof(ub32));
1899 	return (error);
1900 }
1901 #endif
1902 
1903 int
1904 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1905 {
1906 	struct stat ub;
1907 	struct stat32 ub32;
1908 	int error;
1909 
1910 	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1911 	if (error)
1912 		return (error);
1913 	copy_stat(&ub, &ub32);
1914 	error = copyout(&ub32, uap->buf, sizeof(ub32));
1915 	return (error);
1916 }
1917 
1918 int
1919 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1920 {
1921 	struct stat sb;
1922 	struct stat32 sb32;
1923 	int error;
1924 
1925 	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1926 	if (error)
1927 		return (error);
1928 	copy_stat(&sb, &sb32);
1929 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1930 	return (error);
1931 }
1932 
1933 #ifdef COMPAT_43
1934 int
1935 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1936 {
1937 	struct stat sb;
1938 	struct ostat32 sb32;
1939 	int error;
1940 
1941 	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1942 	if (error)
1943 		return (error);
1944 	copy_ostat(&sb, &sb32);
1945 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1946 	return (error);
1947 }
1948 #endif
1949 
1950 int
1951 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1952 {
1953 	int error, name[CTL_MAXNAME];
1954 	size_t j, oldlen;
1955 
1956 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1957 		return (EINVAL);
1958  	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1959  	if (error)
1960 		return (error);
1961 	if (uap->oldlenp)
1962 		oldlen = fuword32(uap->oldlenp);
1963 	else
1964 		oldlen = 0;
1965 	error = userland_sysctl(td, name, uap->namelen,
1966 		uap->old, &oldlen, 1,
1967 		uap->new, uap->newlen, &j, SCTL_MASK32);
1968 	if (error && error != ENOMEM)
1969 		return (error);
1970 	if (uap->oldlenp)
1971 		suword32(uap->oldlenp, j);
1972 	return (0);
1973 }
1974 
1975 int
1976 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1977 {
1978 	uint32_t version;
1979 	int error;
1980 	struct jail j;
1981 
1982 	error = copyin(uap->jail, &version, sizeof(uint32_t));
1983 	if (error)
1984 		return (error);
1985 
1986 	switch (version) {
1987 	case 0:
1988 	{
1989 		/* FreeBSD single IPv4 jails. */
1990 		struct jail32_v0 j32_v0;
1991 
1992 		bzero(&j, sizeof(struct jail));
1993 		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1994 		if (error)
1995 			return (error);
1996 		CP(j32_v0, j, version);
1997 		PTRIN_CP(j32_v0, j, path);
1998 		PTRIN_CP(j32_v0, j, hostname);
1999 		j.ip4s = htonl(j32_v0.ip_number);	/* jail_v0 is host order */
2000 		break;
2001 	}
2002 
2003 	case 1:
2004 		/*
2005 		 * Version 1 was used by multi-IPv4 jail implementations
2006 		 * that never made it into the official kernel.
2007 		 */
2008 		return (EINVAL);
2009 
2010 	case 2:	/* JAIL_API_VERSION */
2011 	{
2012 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
2013 		struct jail32 j32;
2014 
2015 		error = copyin(uap->jail, &j32, sizeof(struct jail32));
2016 		if (error)
2017 			return (error);
2018 		CP(j32, j, version);
2019 		PTRIN_CP(j32, j, path);
2020 		PTRIN_CP(j32, j, hostname);
2021 		PTRIN_CP(j32, j, jailname);
2022 		CP(j32, j, ip4s);
2023 		CP(j32, j, ip6s);
2024 		PTRIN_CP(j32, j, ip4);
2025 		PTRIN_CP(j32, j, ip6);
2026 		break;
2027 	}
2028 
2029 	default:
2030 		/* Sci-Fi jails are not supported, sorry. */
2031 		return (EINVAL);
2032 	}
2033 	return (kern_jail(td, &j));
2034 }
2035 
2036 int
2037 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2038 {
2039 	struct uio *auio;
2040 	int error;
2041 
2042 	/* Check that we have an even number of iovecs. */
2043 	if (uap->iovcnt & 1)
2044 		return (EINVAL);
2045 
2046 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2047 	if (error)
2048 		return (error);
2049 	error = kern_jail_set(td, auio, uap->flags);
2050 	free(auio, M_IOV);
2051 	return (error);
2052 }
2053 
2054 int
2055 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2056 {
2057 	struct iovec32 iov32;
2058 	struct uio *auio;
2059 	int error, i;
2060 
2061 	/* Check that we have an even number of iovecs. */
2062 	if (uap->iovcnt & 1)
2063 		return (EINVAL);
2064 
2065 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2066 	if (error)
2067 		return (error);
2068 	error = kern_jail_get(td, auio, uap->flags);
2069 	if (error == 0)
2070 		for (i = 0; i < uap->iovcnt; i++) {
2071 			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2072 			CP(auio->uio_iov[i], iov32, iov_len);
2073 			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2074 			if (error != 0)
2075 				break;
2076 		}
2077 	free(auio, M_IOV);
2078 	return (error);
2079 }
2080 
2081 int
2082 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2083 {
2084 	struct sigaction32 s32;
2085 	struct sigaction sa, osa, *sap;
2086 	int error;
2087 
2088 	if (uap->act) {
2089 		error = copyin(uap->act, &s32, sizeof(s32));
2090 		if (error)
2091 			return (error);
2092 		sa.sa_handler = PTRIN(s32.sa_u);
2093 		CP(s32, sa, sa_flags);
2094 		CP(s32, sa, sa_mask);
2095 		sap = &sa;
2096 	} else
2097 		sap = NULL;
2098 	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2099 	if (error == 0 && uap->oact != NULL) {
2100 		s32.sa_u = PTROUT(osa.sa_handler);
2101 		CP(osa, s32, sa_flags);
2102 		CP(osa, s32, sa_mask);
2103 		error = copyout(&s32, uap->oact, sizeof(s32));
2104 	}
2105 	return (error);
2106 }
2107 
2108 #ifdef COMPAT_FREEBSD4
2109 int
2110 freebsd4_freebsd32_sigaction(struct thread *td,
2111 			     struct freebsd4_freebsd32_sigaction_args *uap)
2112 {
2113 	struct sigaction32 s32;
2114 	struct sigaction sa, osa, *sap;
2115 	int error;
2116 
2117 	if (uap->act) {
2118 		error = copyin(uap->act, &s32, sizeof(s32));
2119 		if (error)
2120 			return (error);
2121 		sa.sa_handler = PTRIN(s32.sa_u);
2122 		CP(s32, sa, sa_flags);
2123 		CP(s32, sa, sa_mask);
2124 		sap = &sa;
2125 	} else
2126 		sap = NULL;
2127 	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2128 	if (error == 0 && uap->oact != NULL) {
2129 		s32.sa_u = PTROUT(osa.sa_handler);
2130 		CP(osa, s32, sa_flags);
2131 		CP(osa, s32, sa_mask);
2132 		error = copyout(&s32, uap->oact, sizeof(s32));
2133 	}
2134 	return (error);
2135 }
2136 #endif
2137 
2138 #ifdef COMPAT_43
2139 struct osigaction32 {
2140 	u_int32_t	sa_u;
2141 	osigset_t	sa_mask;
2142 	int		sa_flags;
2143 };
2144 
2145 #define	ONSIG	32
2146 
2147 int
2148 ofreebsd32_sigaction(struct thread *td,
2149 			     struct ofreebsd32_sigaction_args *uap)
2150 {
2151 	struct osigaction32 s32;
2152 	struct sigaction sa, osa, *sap;
2153 	int error;
2154 
2155 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2156 		return (EINVAL);
2157 
2158 	if (uap->nsa) {
2159 		error = copyin(uap->nsa, &s32, sizeof(s32));
2160 		if (error)
2161 			return (error);
2162 		sa.sa_handler = PTRIN(s32.sa_u);
2163 		CP(s32, sa, sa_flags);
2164 		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2165 		sap = &sa;
2166 	} else
2167 		sap = NULL;
2168 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2169 	if (error == 0 && uap->osa != NULL) {
2170 		s32.sa_u = PTROUT(osa.sa_handler);
2171 		CP(osa, s32, sa_flags);
2172 		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2173 		error = copyout(&s32, uap->osa, sizeof(s32));
2174 	}
2175 	return (error);
2176 }
2177 
2178 int
2179 ofreebsd32_sigprocmask(struct thread *td,
2180 			       struct ofreebsd32_sigprocmask_args *uap)
2181 {
2182 	sigset_t set, oset;
2183 	int error;
2184 
2185 	OSIG2SIG(uap->mask, set);
2186 	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2187 	SIG2OSIG(oset, td->td_retval[0]);
2188 	return (error);
2189 }
2190 
2191 int
2192 ofreebsd32_sigpending(struct thread *td,
2193 			      struct ofreebsd32_sigpending_args *uap)
2194 {
2195 	struct proc *p = td->td_proc;
2196 	sigset_t siglist;
2197 
2198 	PROC_LOCK(p);
2199 	siglist = p->p_siglist;
2200 	SIGSETOR(siglist, td->td_siglist);
2201 	PROC_UNLOCK(p);
2202 	SIG2OSIG(siglist, td->td_retval[0]);
2203 	return (0);
2204 }
2205 
2206 struct sigvec32 {
2207 	u_int32_t	sv_handler;
2208 	int		sv_mask;
2209 	int		sv_flags;
2210 };
2211 
2212 int
2213 ofreebsd32_sigvec(struct thread *td,
2214 			  struct ofreebsd32_sigvec_args *uap)
2215 {
2216 	struct sigvec32 vec;
2217 	struct sigaction sa, osa, *sap;
2218 	int error;
2219 
2220 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2221 		return (EINVAL);
2222 
2223 	if (uap->nsv) {
2224 		error = copyin(uap->nsv, &vec, sizeof(vec));
2225 		if (error)
2226 			return (error);
2227 		sa.sa_handler = PTRIN(vec.sv_handler);
2228 		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2229 		sa.sa_flags = vec.sv_flags;
2230 		sa.sa_flags ^= SA_RESTART;
2231 		sap = &sa;
2232 	} else
2233 		sap = NULL;
2234 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2235 	if (error == 0 && uap->osv != NULL) {
2236 		vec.sv_handler = PTROUT(osa.sa_handler);
2237 		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2238 		vec.sv_flags = osa.sa_flags;
2239 		vec.sv_flags &= ~SA_NOCLDWAIT;
2240 		vec.sv_flags ^= SA_RESTART;
2241 		error = copyout(&vec, uap->osv, sizeof(vec));
2242 	}
2243 	return (error);
2244 }
2245 
2246 int
2247 ofreebsd32_sigblock(struct thread *td,
2248 			    struct ofreebsd32_sigblock_args *uap)
2249 {
2250 	sigset_t set, oset;
2251 
2252 	OSIG2SIG(uap->mask, set);
2253 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2254 	SIG2OSIG(oset, td->td_retval[0]);
2255 	return (0);
2256 }
2257 
2258 int
2259 ofreebsd32_sigsetmask(struct thread *td,
2260 			      struct ofreebsd32_sigsetmask_args *uap)
2261 {
2262 	sigset_t set, oset;
2263 
2264 	OSIG2SIG(uap->mask, set);
2265 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2266 	SIG2OSIG(oset, td->td_retval[0]);
2267 	return (0);
2268 }
2269 
2270 int
2271 ofreebsd32_sigsuspend(struct thread *td,
2272 			      struct ofreebsd32_sigsuspend_args *uap)
2273 {
2274 	sigset_t mask;
2275 
2276 	OSIG2SIG(uap->mask, mask);
2277 	return (kern_sigsuspend(td, mask));
2278 }
2279 
2280 struct sigstack32 {
2281 	u_int32_t	ss_sp;
2282 	int		ss_onstack;
2283 };
2284 
2285 int
2286 ofreebsd32_sigstack(struct thread *td,
2287 			    struct ofreebsd32_sigstack_args *uap)
2288 {
2289 	struct sigstack32 s32;
2290 	struct sigstack nss, oss;
2291 	int error = 0, unss;
2292 
2293 	if (uap->nss != NULL) {
2294 		error = copyin(uap->nss, &s32, sizeof(s32));
2295 		if (error)
2296 			return (error);
2297 		nss.ss_sp = PTRIN(s32.ss_sp);
2298 		CP(s32, nss, ss_onstack);
2299 		unss = 1;
2300 	} else {
2301 		unss = 0;
2302 	}
2303 	oss.ss_sp = td->td_sigstk.ss_sp;
2304 	oss.ss_onstack = sigonstack(cpu_getstack(td));
2305 	if (unss) {
2306 		td->td_sigstk.ss_sp = nss.ss_sp;
2307 		td->td_sigstk.ss_size = 0;
2308 		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2309 		td->td_pflags |= TDP_ALTSTACK;
2310 	}
2311 	if (uap->oss != NULL) {
2312 		s32.ss_sp = PTROUT(oss.ss_sp);
2313 		CP(oss, s32, ss_onstack);
2314 		error = copyout(&s32, uap->oss, sizeof(s32));
2315 	}
2316 	return (error);
2317 }
2318 #endif
2319 
2320 int
2321 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2322 {
2323 	struct timespec32 rmt32, rqt32;
2324 	struct timespec rmt, rqt;
2325 	int error;
2326 
2327 	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2328 	if (error)
2329 		return (error);
2330 
2331 	CP(rqt32, rqt, tv_sec);
2332 	CP(rqt32, rqt, tv_nsec);
2333 
2334 	if (uap->rmtp &&
2335 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2336 		return (EFAULT);
2337 	error = kern_nanosleep(td, &rqt, &rmt);
2338 	if (error && uap->rmtp) {
2339 		int error2;
2340 
2341 		CP(rmt, rmt32, tv_sec);
2342 		CP(rmt, rmt32, tv_nsec);
2343 
2344 		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2345 		if (error2)
2346 			error = error2;
2347 	}
2348 	return (error);
2349 }
2350 
2351 int
2352 freebsd32_clock_gettime(struct thread *td,
2353 			struct freebsd32_clock_gettime_args *uap)
2354 {
2355 	struct timespec	ats;
2356 	struct timespec32 ats32;
2357 	int error;
2358 
2359 	error = kern_clock_gettime(td, uap->clock_id, &ats);
2360 	if (error == 0) {
2361 		CP(ats, ats32, tv_sec);
2362 		CP(ats, ats32, tv_nsec);
2363 		error = copyout(&ats32, uap->tp, sizeof(ats32));
2364 	}
2365 	return (error);
2366 }
2367 
2368 int
2369 freebsd32_clock_settime(struct thread *td,
2370 			struct freebsd32_clock_settime_args *uap)
2371 {
2372 	struct timespec	ats;
2373 	struct timespec32 ats32;
2374 	int error;
2375 
2376 	error = copyin(uap->tp, &ats32, sizeof(ats32));
2377 	if (error)
2378 		return (error);
2379 	CP(ats32, ats, tv_sec);
2380 	CP(ats32, ats, tv_nsec);
2381 
2382 	return (kern_clock_settime(td, uap->clock_id, &ats));
2383 }
2384 
2385 int
2386 freebsd32_clock_getres(struct thread *td,
2387 		       struct freebsd32_clock_getres_args *uap)
2388 {
2389 	struct timespec	ts;
2390 	struct timespec32 ts32;
2391 	int error;
2392 
2393 	if (uap->tp == NULL)
2394 		return (0);
2395 	error = kern_clock_getres(td, uap->clock_id, &ts);
2396 	if (error == 0) {
2397 		CP(ts, ts32, tv_sec);
2398 		CP(ts, ts32, tv_nsec);
2399 		error = copyout(&ts32, uap->tp, sizeof(ts32));
2400 	}
2401 	return (error);
2402 }
2403 
2404 int freebsd32_ktimer_create(struct thread *td,
2405     struct freebsd32_ktimer_create_args *uap)
2406 {
2407 	struct sigevent32 ev32;
2408 	struct sigevent ev, *evp;
2409 	int error, id;
2410 
2411 	if (uap->evp == NULL) {
2412 		evp = NULL;
2413 	} else {
2414 		evp = &ev;
2415 		error = copyin(uap->evp, &ev32, sizeof(ev32));
2416 		if (error != 0)
2417 			return (error);
2418 		error = convert_sigevent32(&ev32, &ev);
2419 		if (error != 0)
2420 			return (error);
2421 	}
2422 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2423 	if (error == 0) {
2424 		error = copyout(&id, uap->timerid, sizeof(int));
2425 		if (error != 0)
2426 			kern_ktimer_delete(td, id);
2427 	}
2428 	return (error);
2429 }
2430 
2431 int
2432 freebsd32_ktimer_settime(struct thread *td,
2433     struct freebsd32_ktimer_settime_args *uap)
2434 {
2435 	struct itimerspec32 val32, oval32;
2436 	struct itimerspec val, oval, *ovalp;
2437 	int error;
2438 
2439 	error = copyin(uap->value, &val32, sizeof(val32));
2440 	if (error != 0)
2441 		return (error);
2442 	ITS_CP(val32, val);
2443 	ovalp = uap->ovalue != NULL ? &oval : NULL;
2444 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2445 	if (error == 0 && uap->ovalue != NULL) {
2446 		ITS_CP(oval, oval32);
2447 		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2448 	}
2449 	return (error);
2450 }
2451 
2452 int
2453 freebsd32_ktimer_gettime(struct thread *td,
2454     struct freebsd32_ktimer_gettime_args *uap)
2455 {
2456 	struct itimerspec32 val32;
2457 	struct itimerspec val;
2458 	int error;
2459 
2460 	error = kern_ktimer_gettime(td, uap->timerid, &val);
2461 	if (error == 0) {
2462 		ITS_CP(val, val32);
2463 		error = copyout(&val32, uap->value, sizeof(val32));
2464 	}
2465 	return (error);
2466 }
2467 
2468 int
2469 freebsd32_clock_getcpuclockid2(struct thread *td,
2470     struct freebsd32_clock_getcpuclockid2_args *uap)
2471 {
2472 	clockid_t clk_id;
2473 	int error;
2474 
2475 	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2476 	    uap->which, &clk_id);
2477 	if (error == 0)
2478 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2479 	return (error);
2480 }
2481 
2482 int
2483 freebsd32_thr_new(struct thread *td,
2484 		  struct freebsd32_thr_new_args *uap)
2485 {
2486 	struct thr_param32 param32;
2487 	struct thr_param param;
2488 	int error;
2489 
2490 	if (uap->param_size < 0 ||
2491 	    uap->param_size > sizeof(struct thr_param32))
2492 		return (EINVAL);
2493 	bzero(&param, sizeof(struct thr_param));
2494 	bzero(&param32, sizeof(struct thr_param32));
2495 	error = copyin(uap->param, &param32, uap->param_size);
2496 	if (error != 0)
2497 		return (error);
2498 	param.start_func = PTRIN(param32.start_func);
2499 	param.arg = PTRIN(param32.arg);
2500 	param.stack_base = PTRIN(param32.stack_base);
2501 	param.stack_size = param32.stack_size;
2502 	param.tls_base = PTRIN(param32.tls_base);
2503 	param.tls_size = param32.tls_size;
2504 	param.child_tid = PTRIN(param32.child_tid);
2505 	param.parent_tid = PTRIN(param32.parent_tid);
2506 	param.flags = param32.flags;
2507 	param.rtp = PTRIN(param32.rtp);
2508 	param.spare[0] = PTRIN(param32.spare[0]);
2509 	param.spare[1] = PTRIN(param32.spare[1]);
2510 	param.spare[2] = PTRIN(param32.spare[2]);
2511 
2512 	return (kern_thr_new(td, &param));
2513 }
2514 
2515 int
2516 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2517 {
2518 	struct timespec32 ts32;
2519 	struct timespec ts, *tsp;
2520 	int error;
2521 
2522 	error = 0;
2523 	tsp = NULL;
2524 	if (uap->timeout != NULL) {
2525 		error = copyin((const void *)uap->timeout, (void *)&ts32,
2526 		    sizeof(struct timespec32));
2527 		if (error != 0)
2528 			return (error);
2529 		ts.tv_sec = ts32.tv_sec;
2530 		ts.tv_nsec = ts32.tv_nsec;
2531 		tsp = &ts;
2532 	}
2533 	return (kern_thr_suspend(td, tsp));
2534 }
2535 
2536 void
2537 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2538 {
2539 	bzero(dst, sizeof(*dst));
2540 	dst->si_signo = src->si_signo;
2541 	dst->si_errno = src->si_errno;
2542 	dst->si_code = src->si_code;
2543 	dst->si_pid = src->si_pid;
2544 	dst->si_uid = src->si_uid;
2545 	dst->si_status = src->si_status;
2546 	dst->si_addr = (uintptr_t)src->si_addr;
2547 	dst->si_value.sival_int = src->si_value.sival_int;
2548 	dst->si_timerid = src->si_timerid;
2549 	dst->si_overrun = src->si_overrun;
2550 }
2551 
2552 int
2553 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2554 {
2555 	struct timespec32 ts32;
2556 	struct timespec ts;
2557 	struct timespec *timeout;
2558 	sigset_t set;
2559 	ksiginfo_t ksi;
2560 	struct siginfo32 si32;
2561 	int error;
2562 
2563 	if (uap->timeout) {
2564 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2565 		if (error)
2566 			return (error);
2567 		ts.tv_sec = ts32.tv_sec;
2568 		ts.tv_nsec = ts32.tv_nsec;
2569 		timeout = &ts;
2570 	} else
2571 		timeout = NULL;
2572 
2573 	error = copyin(uap->set, &set, sizeof(set));
2574 	if (error)
2575 		return (error);
2576 
2577 	error = kern_sigtimedwait(td, set, &ksi, timeout);
2578 	if (error)
2579 		return (error);
2580 
2581 	if (uap->info) {
2582 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2583 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2584 	}
2585 
2586 	if (error == 0)
2587 		td->td_retval[0] = ksi.ksi_signo;
2588 	return (error);
2589 }
2590 
2591 /*
2592  * MPSAFE
2593  */
2594 int
2595 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2596 {
2597 	ksiginfo_t ksi;
2598 	struct siginfo32 si32;
2599 	sigset_t set;
2600 	int error;
2601 
2602 	error = copyin(uap->set, &set, sizeof(set));
2603 	if (error)
2604 		return (error);
2605 
2606 	error = kern_sigtimedwait(td, set, &ksi, NULL);
2607 	if (error)
2608 		return (error);
2609 
2610 	if (uap->info) {
2611 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2612 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2613 	}
2614 	if (error == 0)
2615 		td->td_retval[0] = ksi.ksi_signo;
2616 	return (error);
2617 }
2618 
2619 int
2620 freebsd32_cpuset_setid(struct thread *td,
2621     struct freebsd32_cpuset_setid_args *uap)
2622 {
2623 	struct cpuset_setid_args ap;
2624 
2625 	ap.which = uap->which;
2626 	ap.id = PAIR32TO64(id_t,uap->id);
2627 	ap.setid = uap->setid;
2628 
2629 	return (sys_cpuset_setid(td, &ap));
2630 }
2631 
2632 int
2633 freebsd32_cpuset_getid(struct thread *td,
2634     struct freebsd32_cpuset_getid_args *uap)
2635 {
2636 	struct cpuset_getid_args ap;
2637 
2638 	ap.level = uap->level;
2639 	ap.which = uap->which;
2640 	ap.id = PAIR32TO64(id_t,uap->id);
2641 	ap.setid = uap->setid;
2642 
2643 	return (sys_cpuset_getid(td, &ap));
2644 }
2645 
2646 int
2647 freebsd32_cpuset_getaffinity(struct thread *td,
2648     struct freebsd32_cpuset_getaffinity_args *uap)
2649 {
2650 	struct cpuset_getaffinity_args ap;
2651 
2652 	ap.level = uap->level;
2653 	ap.which = uap->which;
2654 	ap.id = PAIR32TO64(id_t,uap->id);
2655 	ap.cpusetsize = uap->cpusetsize;
2656 	ap.mask = uap->mask;
2657 
2658 	return (sys_cpuset_getaffinity(td, &ap));
2659 }
2660 
2661 int
2662 freebsd32_cpuset_setaffinity(struct thread *td,
2663     struct freebsd32_cpuset_setaffinity_args *uap)
2664 {
2665 	struct cpuset_setaffinity_args ap;
2666 
2667 	ap.level = uap->level;
2668 	ap.which = uap->which;
2669 	ap.id = PAIR32TO64(id_t,uap->id);
2670 	ap.cpusetsize = uap->cpusetsize;
2671 	ap.mask = uap->mask;
2672 
2673 	return (sys_cpuset_setaffinity(td, &ap));
2674 }
2675 
2676 int
2677 freebsd32_nmount(struct thread *td,
2678     struct freebsd32_nmount_args /* {
2679     	struct iovec *iovp;
2680     	unsigned int iovcnt;
2681     	int flags;
2682     } */ *uap)
2683 {
2684 	struct uio *auio;
2685 	uint64_t flags;
2686 	int error;
2687 
2688 	/*
2689 	 * Mount flags are now 64-bits. On 32-bit archtectures only
2690 	 * 32-bits are passed in, but from here on everything handles
2691 	 * 64-bit flags correctly.
2692 	 */
2693 	flags = uap->flags;
2694 
2695 	AUDIT_ARG_FFLAGS(flags);
2696 
2697 	/*
2698 	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2699 	 * userspace to set this flag, but we must filter it out if we want
2700 	 * MNT_UPDATE on the root file system to work.
2701 	 * MNT_ROOTFS should only be set by the kernel when mounting its
2702 	 * root file system.
2703 	 */
2704 	flags &= ~MNT_ROOTFS;
2705 
2706 	/*
2707 	 * check that we have an even number of iovec's
2708 	 * and that we have at least two options.
2709 	 */
2710 	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2711 		return (EINVAL);
2712 
2713 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2714 	if (error)
2715 		return (error);
2716 	error = vfs_donmount(td, flags, auio);
2717 
2718 	free(auio, M_IOV);
2719 	return error;
2720 }
2721 
2722 #if 0
2723 int
2724 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2725 {
2726 	struct yyy32 *p32, s32;
2727 	struct yyy *p = NULL, s;
2728 	struct xxx_arg ap;
2729 	int error;
2730 
2731 	if (uap->zzz) {
2732 		error = copyin(uap->zzz, &s32, sizeof(s32));
2733 		if (error)
2734 			return (error);
2735 		/* translate in */
2736 		p = &s;
2737 	}
2738 	error = kern_xxx(td, p);
2739 	if (error)
2740 		return (error);
2741 	if (uap->zzz) {
2742 		/* translate out */
2743 		error = copyout(&s32, p32, sizeof(s32));
2744 	}
2745 	return (error);
2746 }
2747 #endif
2748 
2749 int
2750 syscall32_register(int *offset, struct sysent *new_sysent,
2751     struct sysent *old_sysent)
2752 {
2753 	if (*offset == NO_SYSCALL) {
2754 		int i;
2755 
2756 		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2757 			if (freebsd32_sysent[i].sy_call ==
2758 			    (sy_call_t *)lkmnosys)
2759 				break;
2760 		if (i == SYS_MAXSYSCALL)
2761 			return (ENFILE);
2762 		*offset = i;
2763 	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2764 		return (EINVAL);
2765 	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2766 	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2767 		return (EEXIST);
2768 
2769 	*old_sysent = freebsd32_sysent[*offset];
2770 	freebsd32_sysent[*offset] = *new_sysent;
2771 	return 0;
2772 }
2773 
2774 int
2775 syscall32_deregister(int *offset, struct sysent *old_sysent)
2776 {
2777 
2778 	if (*offset)
2779 		freebsd32_sysent[*offset] = *old_sysent;
2780 	return 0;
2781 }
2782 
2783 int
2784 syscall32_module_handler(struct module *mod, int what, void *arg)
2785 {
2786 	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2787 	modspecific_t ms;
2788 	int error;
2789 
2790 	switch (what) {
2791 	case MOD_LOAD:
2792 		error = syscall32_register(data->offset, data->new_sysent,
2793 		    &data->old_sysent);
2794 		if (error) {
2795 			/* Leave a mark so we know to safely unload below. */
2796 			data->offset = NULL;
2797 			return error;
2798 		}
2799 		ms.intval = *data->offset;
2800 		MOD_XLOCK;
2801 		module_setspecific(mod, &ms);
2802 		MOD_XUNLOCK;
2803 		if (data->chainevh)
2804 			error = data->chainevh(mod, what, data->chainarg);
2805 		return (error);
2806 	case MOD_UNLOAD:
2807 		/*
2808 		 * MOD_LOAD failed, so just return without calling the
2809 		 * chained handler since we didn't pass along the MOD_LOAD
2810 		 * event.
2811 		 */
2812 		if (data->offset == NULL)
2813 			return (0);
2814 		if (data->chainevh) {
2815 			error = data->chainevh(mod, what, data->chainarg);
2816 			if (error)
2817 				return (error);
2818 		}
2819 		error = syscall32_deregister(data->offset, &data->old_sysent);
2820 		return (error);
2821 	default:
2822 		error = EOPNOTSUPP;
2823 		if (data->chainevh)
2824 			error = data->chainevh(mod, what, data->chainarg);
2825 		return (error);
2826 	}
2827 }
2828 
2829 int
2830 syscall32_helper_register(struct syscall_helper_data *sd)
2831 {
2832 	struct syscall_helper_data *sd1;
2833 	int error;
2834 
2835 	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2836 		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2837 		    &sd1->old_sysent);
2838 		if (error != 0) {
2839 			syscall32_helper_unregister(sd);
2840 			return (error);
2841 		}
2842 		sd1->registered = 1;
2843 	}
2844 	return (0);
2845 }
2846 
2847 int
2848 syscall32_helper_unregister(struct syscall_helper_data *sd)
2849 {
2850 	struct syscall_helper_data *sd1;
2851 
2852 	for (sd1 = sd; sd1->registered != 0; sd1++) {
2853 		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2854 		sd1->registered = 0;
2855 	}
2856 	return (0);
2857 }
2858 
2859 register_t *
2860 freebsd32_copyout_strings(struct image_params *imgp)
2861 {
2862 	int argc, envc, i;
2863 	u_int32_t *vectp;
2864 	char *stringp;
2865 	uintptr_t destp;
2866 	u_int32_t *stack_base;
2867 	struct freebsd32_ps_strings *arginfo;
2868 	char canary[sizeof(long) * 8];
2869 	int32_t pagesizes32[MAXPAGESIZES];
2870 	size_t execpath_len;
2871 	int szsigcode;
2872 
2873 	/*
2874 	 * Calculate string base and vector table pointers.
2875 	 * Also deal with signal trampoline code for this exec type.
2876 	 */
2877 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2878 		execpath_len = strlen(imgp->execpath) + 1;
2879 	else
2880 		execpath_len = 0;
2881 	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2882 	    sv_psstrings;
2883 	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2884 		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2885 	else
2886 		szsigcode = 0;
2887 	destp =	(uintptr_t)arginfo;
2888 
2889 	/*
2890 	 * install sigcode
2891 	 */
2892 	if (szsigcode != 0) {
2893 		destp -= szsigcode;
2894 		destp = rounddown2(destp, sizeof(uint32_t));
2895 		copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2896 		    szsigcode);
2897 	}
2898 
2899 	/*
2900 	 * Copy the image path for the rtld.
2901 	 */
2902 	if (execpath_len != 0) {
2903 		destp -= execpath_len;
2904 		imgp->execpathp = destp;
2905 		copyout(imgp->execpath, (void *)destp, execpath_len);
2906 	}
2907 
2908 	/*
2909 	 * Prepare the canary for SSP.
2910 	 */
2911 	arc4rand(canary, sizeof(canary), 0);
2912 	destp -= sizeof(canary);
2913 	imgp->canary = destp;
2914 	copyout(canary, (void *)destp, sizeof(canary));
2915 	imgp->canarylen = sizeof(canary);
2916 
2917 	/*
2918 	 * Prepare the pagesizes array.
2919 	 */
2920 	for (i = 0; i < MAXPAGESIZES; i++)
2921 		pagesizes32[i] = (uint32_t)pagesizes[i];
2922 	destp -= sizeof(pagesizes32);
2923 	destp = rounddown2(destp, sizeof(uint32_t));
2924 	imgp->pagesizes = destp;
2925 	copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2926 	imgp->pagesizeslen = sizeof(pagesizes32);
2927 
2928 	destp -= ARG_MAX - imgp->args->stringspace;
2929 	destp = rounddown2(destp, sizeof(uint32_t));
2930 
2931 	/*
2932 	 * If we have a valid auxargs ptr, prepare some room
2933 	 * on the stack.
2934 	 */
2935 	if (imgp->auxargs) {
2936 		/*
2937 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2938 		 * lower compatibility.
2939 		 */
2940 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2941 			: (AT_COUNT * 2);
2942 		/*
2943 		 * The '+ 2' is for the null pointers at the end of each of
2944 		 * the arg and env vector sets,and imgp->auxarg_size is room
2945 		 * for argument of Runtime loader.
2946 		 */
2947 		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2948 		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2949 		    sizeof(u_int32_t));
2950 	} else {
2951 		/*
2952 		 * The '+ 2' is for the null pointers at the end of each of
2953 		 * the arg and env vector sets
2954 		 */
2955 		vectp = (u_int32_t *)(destp - (imgp->args->argc +
2956 		    imgp->args->envc + 2) * sizeof(u_int32_t));
2957 	}
2958 
2959 	/*
2960 	 * vectp also becomes our initial stack base
2961 	 */
2962 	stack_base = vectp;
2963 
2964 	stringp = imgp->args->begin_argv;
2965 	argc = imgp->args->argc;
2966 	envc = imgp->args->envc;
2967 	/*
2968 	 * Copy out strings - arguments and environment.
2969 	 */
2970 	copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2971 
2972 	/*
2973 	 * Fill in "ps_strings" struct for ps, w, etc.
2974 	 */
2975 	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2976 	suword32(&arginfo->ps_nargvstr, argc);
2977 
2978 	/*
2979 	 * Fill in argument portion of vector table.
2980 	 */
2981 	for (; argc > 0; --argc) {
2982 		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2983 		while (*stringp++ != 0)
2984 			destp++;
2985 		destp++;
2986 	}
2987 
2988 	/* a null vector table pointer separates the argp's from the envp's */
2989 	suword32(vectp++, 0);
2990 
2991 	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2992 	suword32(&arginfo->ps_nenvstr, envc);
2993 
2994 	/*
2995 	 * Fill in environment portion of vector table.
2996 	 */
2997 	for (; envc > 0; --envc) {
2998 		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2999 		while (*stringp++ != 0)
3000 			destp++;
3001 		destp++;
3002 	}
3003 
3004 	/* end of vector table is a null pointer */
3005 	suword32(vectp, 0);
3006 
3007 	return ((register_t *)stack_base);
3008 }
3009 
3010 int
3011 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3012 {
3013 	struct kld_file_stat stat;
3014 	struct kld32_file_stat stat32;
3015 	int error, version;
3016 
3017 	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3018 	    != 0)
3019 		return (error);
3020 	if (version != sizeof(struct kld32_file_stat_1) &&
3021 	    version != sizeof(struct kld32_file_stat))
3022 		return (EINVAL);
3023 
3024 	error = kern_kldstat(td, uap->fileid, &stat);
3025 	if (error != 0)
3026 		return (error);
3027 
3028 	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
3029 	CP(stat, stat32, refs);
3030 	CP(stat, stat32, id);
3031 	PTROUT_CP(stat, stat32, address);
3032 	CP(stat, stat32, size);
3033 	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
3034 	return (copyout(&stat32, uap->stat, version));
3035 }
3036 
3037 int
3038 freebsd32_posix_fallocate(struct thread *td,
3039     struct freebsd32_posix_fallocate_args *uap)
3040 {
3041 
3042 	td->td_retval[0] = kern_posix_fallocate(td, uap->fd,
3043 	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3044 	return (0);
3045 }
3046 
3047 int
3048 freebsd32_posix_fadvise(struct thread *td,
3049     struct freebsd32_posix_fadvise_args *uap)
3050 {
3051 
3052 	td->td_retval[0] = kern_posix_fadvise(td, uap->fd,
3053 	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len),
3054 	    uap->advice);
3055 	return (0);
3056 }
3057 
3058 int
3059 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3060 {
3061 
3062 	CP(*sig32, *sig, sigev_notify);
3063 	switch (sig->sigev_notify) {
3064 	case SIGEV_NONE:
3065 		break;
3066 	case SIGEV_THREAD_ID:
3067 		CP(*sig32, *sig, sigev_notify_thread_id);
3068 		/* FALLTHROUGH */
3069 	case SIGEV_SIGNAL:
3070 		CP(*sig32, *sig, sigev_signo);
3071 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3072 		break;
3073 	case SIGEV_KEVENT:
3074 		CP(*sig32, *sig, sigev_notify_kqueue);
3075 		CP(*sig32, *sig, sigev_notify_kevent_flags);
3076 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3077 		break;
3078 	default:
3079 		return (EINVAL);
3080 	}
3081 	return (0);
3082 }
3083 
3084 int
3085 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3086 {
3087 	void *data;
3088 	int error, flags;
3089 
3090 	switch (uap->com) {
3091 	case PROC_SPROTECT:
3092 		error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3093 		if (error)
3094 			return (error);
3095 		data = &flags;
3096 		break;
3097 	default:
3098 		return (EINVAL);
3099 	}
3100 	return (kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3101 	    uap->com, data));
3102 }
3103