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