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