xref: /freebsd/sys/compat/freebsd32/freebsd32_misc.c (revision 3823d5e198425b4f5e5a80267d195769d1063773)
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 
1836 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1837 		return (EINVAL);
1838  	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1839  	if (error)
1840 		return (error);
1841 	if (uap->oldlenp)
1842 		oldlen = fuword32(uap->oldlenp);
1843 	else
1844 		oldlen = 0;
1845 	error = userland_sysctl(td, name, uap->namelen,
1846 		uap->old, &oldlen, 1,
1847 		uap->new, uap->newlen, &j, SCTL_MASK32);
1848 	if (error && error != ENOMEM)
1849 		return (error);
1850 	if (uap->oldlenp)
1851 		suword32(uap->oldlenp, j);
1852 	return (0);
1853 }
1854 
1855 int
1856 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1857 {
1858 	uint32_t version;
1859 	int error;
1860 	struct jail j;
1861 
1862 	error = copyin(uap->jail, &version, sizeof(uint32_t));
1863 	if (error)
1864 		return (error);
1865 
1866 	switch (version) {
1867 	case 0:
1868 	{
1869 		/* FreeBSD single IPv4 jails. */
1870 		struct jail32_v0 j32_v0;
1871 
1872 		bzero(&j, sizeof(struct jail));
1873 		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1874 		if (error)
1875 			return (error);
1876 		CP(j32_v0, j, version);
1877 		PTRIN_CP(j32_v0, j, path);
1878 		PTRIN_CP(j32_v0, j, hostname);
1879 		j.ip4s = htonl(j32_v0.ip_number);	/* jail_v0 is host order */
1880 		break;
1881 	}
1882 
1883 	case 1:
1884 		/*
1885 		 * Version 1 was used by multi-IPv4 jail implementations
1886 		 * that never made it into the official kernel.
1887 		 */
1888 		return (EINVAL);
1889 
1890 	case 2:	/* JAIL_API_VERSION */
1891 	{
1892 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
1893 		struct jail32 j32;
1894 
1895 		error = copyin(uap->jail, &j32, sizeof(struct jail32));
1896 		if (error)
1897 			return (error);
1898 		CP(j32, j, version);
1899 		PTRIN_CP(j32, j, path);
1900 		PTRIN_CP(j32, j, hostname);
1901 		PTRIN_CP(j32, j, jailname);
1902 		CP(j32, j, ip4s);
1903 		CP(j32, j, ip6s);
1904 		PTRIN_CP(j32, j, ip4);
1905 		PTRIN_CP(j32, j, ip6);
1906 		break;
1907 	}
1908 
1909 	default:
1910 		/* Sci-Fi jails are not supported, sorry. */
1911 		return (EINVAL);
1912 	}
1913 	return (kern_jail(td, &j));
1914 }
1915 
1916 int
1917 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1918 {
1919 	struct uio *auio;
1920 	int error;
1921 
1922 	/* Check that we have an even number of iovecs. */
1923 	if (uap->iovcnt & 1)
1924 		return (EINVAL);
1925 
1926 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1927 	if (error)
1928 		return (error);
1929 	error = kern_jail_set(td, auio, uap->flags);
1930 	free(auio, M_IOV);
1931 	return (error);
1932 }
1933 
1934 int
1935 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1936 {
1937 	struct iovec32 iov32;
1938 	struct uio *auio;
1939 	int error, i;
1940 
1941 	/* Check that we have an even number of iovecs. */
1942 	if (uap->iovcnt & 1)
1943 		return (EINVAL);
1944 
1945 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1946 	if (error)
1947 		return (error);
1948 	error = kern_jail_get(td, auio, uap->flags);
1949 	if (error == 0)
1950 		for (i = 0; i < uap->iovcnt; i++) {
1951 			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1952 			CP(auio->uio_iov[i], iov32, iov_len);
1953 			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1954 			if (error != 0)
1955 				break;
1956 		}
1957 	free(auio, M_IOV);
1958 	return (error);
1959 }
1960 
1961 int
1962 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1963 {
1964 	struct sigaction32 s32;
1965 	struct sigaction sa, osa, *sap;
1966 	int error;
1967 
1968 	if (uap->act) {
1969 		error = copyin(uap->act, &s32, sizeof(s32));
1970 		if (error)
1971 			return (error);
1972 		sa.sa_handler = PTRIN(s32.sa_u);
1973 		CP(s32, sa, sa_flags);
1974 		CP(s32, sa, sa_mask);
1975 		sap = &sa;
1976 	} else
1977 		sap = NULL;
1978 	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1979 	if (error == 0 && uap->oact != NULL) {
1980 		s32.sa_u = PTROUT(osa.sa_handler);
1981 		CP(osa, s32, sa_flags);
1982 		CP(osa, s32, sa_mask);
1983 		error = copyout(&s32, uap->oact, sizeof(s32));
1984 	}
1985 	return (error);
1986 }
1987 
1988 #ifdef COMPAT_FREEBSD4
1989 int
1990 freebsd4_freebsd32_sigaction(struct thread *td,
1991 			     struct freebsd4_freebsd32_sigaction_args *uap)
1992 {
1993 	struct sigaction32 s32;
1994 	struct sigaction sa, osa, *sap;
1995 	int error;
1996 
1997 	if (uap->act) {
1998 		error = copyin(uap->act, &s32, sizeof(s32));
1999 		if (error)
2000 			return (error);
2001 		sa.sa_handler = PTRIN(s32.sa_u);
2002 		CP(s32, sa, sa_flags);
2003 		CP(s32, sa, sa_mask);
2004 		sap = &sa;
2005 	} else
2006 		sap = NULL;
2007 	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2008 	if (error == 0 && uap->oact != NULL) {
2009 		s32.sa_u = PTROUT(osa.sa_handler);
2010 		CP(osa, s32, sa_flags);
2011 		CP(osa, s32, sa_mask);
2012 		error = copyout(&s32, uap->oact, sizeof(s32));
2013 	}
2014 	return (error);
2015 }
2016 #endif
2017 
2018 #ifdef COMPAT_43
2019 struct osigaction32 {
2020 	u_int32_t	sa_u;
2021 	osigset_t	sa_mask;
2022 	int		sa_flags;
2023 };
2024 
2025 #define	ONSIG	32
2026 
2027 int
2028 ofreebsd32_sigaction(struct thread *td,
2029 			     struct ofreebsd32_sigaction_args *uap)
2030 {
2031 	struct osigaction32 s32;
2032 	struct sigaction sa, osa, *sap;
2033 	int error;
2034 
2035 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2036 		return (EINVAL);
2037 
2038 	if (uap->nsa) {
2039 		error = copyin(uap->nsa, &s32, sizeof(s32));
2040 		if (error)
2041 			return (error);
2042 		sa.sa_handler = PTRIN(s32.sa_u);
2043 		CP(s32, sa, sa_flags);
2044 		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2045 		sap = &sa;
2046 	} else
2047 		sap = NULL;
2048 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2049 	if (error == 0 && uap->osa != NULL) {
2050 		s32.sa_u = PTROUT(osa.sa_handler);
2051 		CP(osa, s32, sa_flags);
2052 		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2053 		error = copyout(&s32, uap->osa, sizeof(s32));
2054 	}
2055 	return (error);
2056 }
2057 
2058 int
2059 ofreebsd32_sigprocmask(struct thread *td,
2060 			       struct ofreebsd32_sigprocmask_args *uap)
2061 {
2062 	sigset_t set, oset;
2063 	int error;
2064 
2065 	OSIG2SIG(uap->mask, set);
2066 	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2067 	SIG2OSIG(oset, td->td_retval[0]);
2068 	return (error);
2069 }
2070 
2071 int
2072 ofreebsd32_sigpending(struct thread *td,
2073 			      struct ofreebsd32_sigpending_args *uap)
2074 {
2075 	struct proc *p = td->td_proc;
2076 	sigset_t siglist;
2077 
2078 	PROC_LOCK(p);
2079 	siglist = p->p_siglist;
2080 	SIGSETOR(siglist, td->td_siglist);
2081 	PROC_UNLOCK(p);
2082 	SIG2OSIG(siglist, td->td_retval[0]);
2083 	return (0);
2084 }
2085 
2086 struct sigvec32 {
2087 	u_int32_t	sv_handler;
2088 	int		sv_mask;
2089 	int		sv_flags;
2090 };
2091 
2092 int
2093 ofreebsd32_sigvec(struct thread *td,
2094 			  struct ofreebsd32_sigvec_args *uap)
2095 {
2096 	struct sigvec32 vec;
2097 	struct sigaction sa, osa, *sap;
2098 	int error;
2099 
2100 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2101 		return (EINVAL);
2102 
2103 	if (uap->nsv) {
2104 		error = copyin(uap->nsv, &vec, sizeof(vec));
2105 		if (error)
2106 			return (error);
2107 		sa.sa_handler = PTRIN(vec.sv_handler);
2108 		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2109 		sa.sa_flags = vec.sv_flags;
2110 		sa.sa_flags ^= SA_RESTART;
2111 		sap = &sa;
2112 	} else
2113 		sap = NULL;
2114 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2115 	if (error == 0 && uap->osv != NULL) {
2116 		vec.sv_handler = PTROUT(osa.sa_handler);
2117 		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2118 		vec.sv_flags = osa.sa_flags;
2119 		vec.sv_flags &= ~SA_NOCLDWAIT;
2120 		vec.sv_flags ^= SA_RESTART;
2121 		error = copyout(&vec, uap->osv, sizeof(vec));
2122 	}
2123 	return (error);
2124 }
2125 
2126 int
2127 ofreebsd32_sigblock(struct thread *td,
2128 			    struct ofreebsd32_sigblock_args *uap)
2129 {
2130 	sigset_t set, oset;
2131 
2132 	OSIG2SIG(uap->mask, set);
2133 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2134 	SIG2OSIG(oset, td->td_retval[0]);
2135 	return (0);
2136 }
2137 
2138 int
2139 ofreebsd32_sigsetmask(struct thread *td,
2140 			      struct ofreebsd32_sigsetmask_args *uap)
2141 {
2142 	sigset_t set, oset;
2143 
2144 	OSIG2SIG(uap->mask, set);
2145 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2146 	SIG2OSIG(oset, td->td_retval[0]);
2147 	return (0);
2148 }
2149 
2150 int
2151 ofreebsd32_sigsuspend(struct thread *td,
2152 			      struct ofreebsd32_sigsuspend_args *uap)
2153 {
2154 	sigset_t mask;
2155 
2156 	OSIG2SIG(uap->mask, mask);
2157 	return (kern_sigsuspend(td, mask));
2158 }
2159 
2160 struct sigstack32 {
2161 	u_int32_t	ss_sp;
2162 	int		ss_onstack;
2163 };
2164 
2165 int
2166 ofreebsd32_sigstack(struct thread *td,
2167 			    struct ofreebsd32_sigstack_args *uap)
2168 {
2169 	struct sigstack32 s32;
2170 	struct sigstack nss, oss;
2171 	int error = 0, unss;
2172 
2173 	if (uap->nss != NULL) {
2174 		error = copyin(uap->nss, &s32, sizeof(s32));
2175 		if (error)
2176 			return (error);
2177 		nss.ss_sp = PTRIN(s32.ss_sp);
2178 		CP(s32, nss, ss_onstack);
2179 		unss = 1;
2180 	} else {
2181 		unss = 0;
2182 	}
2183 	oss.ss_sp = td->td_sigstk.ss_sp;
2184 	oss.ss_onstack = sigonstack(cpu_getstack(td));
2185 	if (unss) {
2186 		td->td_sigstk.ss_sp = nss.ss_sp;
2187 		td->td_sigstk.ss_size = 0;
2188 		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2189 		td->td_pflags |= TDP_ALTSTACK;
2190 	}
2191 	if (uap->oss != NULL) {
2192 		s32.ss_sp = PTROUT(oss.ss_sp);
2193 		CP(oss, s32, ss_onstack);
2194 		error = copyout(&s32, uap->oss, sizeof(s32));
2195 	}
2196 	return (error);
2197 }
2198 #endif
2199 
2200 int
2201 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2202 {
2203 	struct timespec32 rmt32, rqt32;
2204 	struct timespec rmt, rqt;
2205 	int error;
2206 
2207 	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2208 	if (error)
2209 		return (error);
2210 
2211 	CP(rqt32, rqt, tv_sec);
2212 	CP(rqt32, rqt, tv_nsec);
2213 
2214 	if (uap->rmtp &&
2215 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2216 		return (EFAULT);
2217 	error = kern_nanosleep(td, &rqt, &rmt);
2218 	if (error && uap->rmtp) {
2219 		int error2;
2220 
2221 		CP(rmt, rmt32, tv_sec);
2222 		CP(rmt, rmt32, tv_nsec);
2223 
2224 		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2225 		if (error2)
2226 			error = error2;
2227 	}
2228 	return (error);
2229 }
2230 
2231 int
2232 freebsd32_clock_gettime(struct thread *td,
2233 			struct freebsd32_clock_gettime_args *uap)
2234 {
2235 	struct timespec	ats;
2236 	struct timespec32 ats32;
2237 	int error;
2238 
2239 	error = kern_clock_gettime(td, uap->clock_id, &ats);
2240 	if (error == 0) {
2241 		CP(ats, ats32, tv_sec);
2242 		CP(ats, ats32, tv_nsec);
2243 		error = copyout(&ats32, uap->tp, sizeof(ats32));
2244 	}
2245 	return (error);
2246 }
2247 
2248 int
2249 freebsd32_clock_settime(struct thread *td,
2250 			struct freebsd32_clock_settime_args *uap)
2251 {
2252 	struct timespec	ats;
2253 	struct timespec32 ats32;
2254 	int error;
2255 
2256 	error = copyin(uap->tp, &ats32, sizeof(ats32));
2257 	if (error)
2258 		return (error);
2259 	CP(ats32, ats, tv_sec);
2260 	CP(ats32, ats, tv_nsec);
2261 
2262 	return (kern_clock_settime(td, uap->clock_id, &ats));
2263 }
2264 
2265 int
2266 freebsd32_clock_getres(struct thread *td,
2267 		       struct freebsd32_clock_getres_args *uap)
2268 {
2269 	struct timespec	ts;
2270 	struct timespec32 ts32;
2271 	int error;
2272 
2273 	if (uap->tp == NULL)
2274 		return (0);
2275 	error = kern_clock_getres(td, uap->clock_id, &ts);
2276 	if (error == 0) {
2277 		CP(ts, ts32, tv_sec);
2278 		CP(ts, ts32, tv_nsec);
2279 		error = copyout(&ts32, uap->tp, sizeof(ts32));
2280 	}
2281 	return (error);
2282 }
2283 
2284 int freebsd32_ktimer_create(struct thread *td,
2285     struct freebsd32_ktimer_create_args *uap)
2286 {
2287 	struct sigevent32 ev32;
2288 	struct sigevent ev, *evp;
2289 	int error, id;
2290 
2291 	if (uap->evp == NULL) {
2292 		evp = NULL;
2293 	} else {
2294 		evp = &ev;
2295 		error = copyin(uap->evp, &ev32, sizeof(ev32));
2296 		if (error != 0)
2297 			return (error);
2298 		error = convert_sigevent32(&ev32, &ev);
2299 		if (error != 0)
2300 			return (error);
2301 	}
2302 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2303 	if (error == 0) {
2304 		error = copyout(&id, uap->timerid, sizeof(int));
2305 		if (error != 0)
2306 			kern_ktimer_delete(td, id);
2307 	}
2308 	return (error);
2309 }
2310 
2311 int
2312 freebsd32_ktimer_settime(struct thread *td,
2313     struct freebsd32_ktimer_settime_args *uap)
2314 {
2315 	struct itimerspec32 val32, oval32;
2316 	struct itimerspec val, oval, *ovalp;
2317 	int error;
2318 
2319 	error = copyin(uap->value, &val32, sizeof(val32));
2320 	if (error != 0)
2321 		return (error);
2322 	ITS_CP(val32, val);
2323 	ovalp = uap->ovalue != NULL ? &oval : NULL;
2324 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2325 	if (error == 0 && uap->ovalue != NULL) {
2326 		ITS_CP(oval, oval32);
2327 		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2328 	}
2329 	return (error);
2330 }
2331 
2332 int
2333 freebsd32_ktimer_gettime(struct thread *td,
2334     struct freebsd32_ktimer_gettime_args *uap)
2335 {
2336 	struct itimerspec32 val32;
2337 	struct itimerspec val;
2338 	int error;
2339 
2340 	error = kern_ktimer_gettime(td, uap->timerid, &val);
2341 	if (error == 0) {
2342 		ITS_CP(val, val32);
2343 		error = copyout(&val32, uap->value, sizeof(val32));
2344 	}
2345 	return (error);
2346 }
2347 
2348 int
2349 freebsd32_clock_getcpuclockid2(struct thread *td,
2350     struct freebsd32_clock_getcpuclockid2_args *uap)
2351 {
2352 	clockid_t clk_id;
2353 	int error;
2354 
2355 	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2356 	    uap->which, &clk_id);
2357 	if (error == 0)
2358 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2359 	return (error);
2360 }
2361 
2362 int
2363 freebsd32_thr_new(struct thread *td,
2364 		  struct freebsd32_thr_new_args *uap)
2365 {
2366 	struct thr_param32 param32;
2367 	struct thr_param param;
2368 	int error;
2369 
2370 	if (uap->param_size < 0 ||
2371 	    uap->param_size > sizeof(struct thr_param32))
2372 		return (EINVAL);
2373 	bzero(&param, sizeof(struct thr_param));
2374 	bzero(&param32, sizeof(struct thr_param32));
2375 	error = copyin(uap->param, &param32, uap->param_size);
2376 	if (error != 0)
2377 		return (error);
2378 	param.start_func = PTRIN(param32.start_func);
2379 	param.arg = PTRIN(param32.arg);
2380 	param.stack_base = PTRIN(param32.stack_base);
2381 	param.stack_size = param32.stack_size;
2382 	param.tls_base = PTRIN(param32.tls_base);
2383 	param.tls_size = param32.tls_size;
2384 	param.child_tid = PTRIN(param32.child_tid);
2385 	param.parent_tid = PTRIN(param32.parent_tid);
2386 	param.flags = param32.flags;
2387 	param.rtp = PTRIN(param32.rtp);
2388 	param.spare[0] = PTRIN(param32.spare[0]);
2389 	param.spare[1] = PTRIN(param32.spare[1]);
2390 	param.spare[2] = PTRIN(param32.spare[2]);
2391 
2392 	return (kern_thr_new(td, &param));
2393 }
2394 
2395 int
2396 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2397 {
2398 	struct timespec32 ts32;
2399 	struct timespec ts, *tsp;
2400 	int error;
2401 
2402 	error = 0;
2403 	tsp = NULL;
2404 	if (uap->timeout != NULL) {
2405 		error = copyin((const void *)uap->timeout, (void *)&ts32,
2406 		    sizeof(struct timespec32));
2407 		if (error != 0)
2408 			return (error);
2409 		ts.tv_sec = ts32.tv_sec;
2410 		ts.tv_nsec = ts32.tv_nsec;
2411 		tsp = &ts;
2412 	}
2413 	return (kern_thr_suspend(td, tsp));
2414 }
2415 
2416 void
2417 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2418 {
2419 	bzero(dst, sizeof(*dst));
2420 	dst->si_signo = src->si_signo;
2421 	dst->si_errno = src->si_errno;
2422 	dst->si_code = src->si_code;
2423 	dst->si_pid = src->si_pid;
2424 	dst->si_uid = src->si_uid;
2425 	dst->si_status = src->si_status;
2426 	dst->si_addr = (uintptr_t)src->si_addr;
2427 	dst->si_value.sival_int = src->si_value.sival_int;
2428 	dst->si_timerid = src->si_timerid;
2429 	dst->si_overrun = src->si_overrun;
2430 }
2431 
2432 int
2433 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2434 {
2435 	struct timespec32 ts32;
2436 	struct timespec ts;
2437 	struct timespec *timeout;
2438 	sigset_t set;
2439 	ksiginfo_t ksi;
2440 	struct siginfo32 si32;
2441 	int error;
2442 
2443 	if (uap->timeout) {
2444 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2445 		if (error)
2446 			return (error);
2447 		ts.tv_sec = ts32.tv_sec;
2448 		ts.tv_nsec = ts32.tv_nsec;
2449 		timeout = &ts;
2450 	} else
2451 		timeout = NULL;
2452 
2453 	error = copyin(uap->set, &set, sizeof(set));
2454 	if (error)
2455 		return (error);
2456 
2457 	error = kern_sigtimedwait(td, set, &ksi, timeout);
2458 	if (error)
2459 		return (error);
2460 
2461 	if (uap->info) {
2462 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2463 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2464 	}
2465 
2466 	if (error == 0)
2467 		td->td_retval[0] = ksi.ksi_signo;
2468 	return (error);
2469 }
2470 
2471 /*
2472  * MPSAFE
2473  */
2474 int
2475 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2476 {
2477 	ksiginfo_t ksi;
2478 	struct siginfo32 si32;
2479 	sigset_t set;
2480 	int error;
2481 
2482 	error = copyin(uap->set, &set, sizeof(set));
2483 	if (error)
2484 		return (error);
2485 
2486 	error = kern_sigtimedwait(td, set, &ksi, NULL);
2487 	if (error)
2488 		return (error);
2489 
2490 	if (uap->info) {
2491 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2492 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2493 	}
2494 	if (error == 0)
2495 		td->td_retval[0] = ksi.ksi_signo;
2496 	return (error);
2497 }
2498 
2499 int
2500 freebsd32_cpuset_setid(struct thread *td,
2501     struct freebsd32_cpuset_setid_args *uap)
2502 {
2503 	struct cpuset_setid_args ap;
2504 
2505 	ap.which = uap->which;
2506 	ap.id = PAIR32TO64(id_t,uap->id);
2507 	ap.setid = uap->setid;
2508 
2509 	return (sys_cpuset_setid(td, &ap));
2510 }
2511 
2512 int
2513 freebsd32_cpuset_getid(struct thread *td,
2514     struct freebsd32_cpuset_getid_args *uap)
2515 {
2516 	struct cpuset_getid_args ap;
2517 
2518 	ap.level = uap->level;
2519 	ap.which = uap->which;
2520 	ap.id = PAIR32TO64(id_t,uap->id);
2521 	ap.setid = uap->setid;
2522 
2523 	return (sys_cpuset_getid(td, &ap));
2524 }
2525 
2526 int
2527 freebsd32_cpuset_getaffinity(struct thread *td,
2528     struct freebsd32_cpuset_getaffinity_args *uap)
2529 {
2530 	struct cpuset_getaffinity_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_getaffinity(td, &ap));
2539 }
2540 
2541 int
2542 freebsd32_cpuset_setaffinity(struct thread *td,
2543     struct freebsd32_cpuset_setaffinity_args *uap)
2544 {
2545 	struct cpuset_setaffinity_args ap;
2546 
2547 	ap.level = uap->level;
2548 	ap.which = uap->which;
2549 	ap.id = PAIR32TO64(id_t,uap->id);
2550 	ap.cpusetsize = uap->cpusetsize;
2551 	ap.mask = uap->mask;
2552 
2553 	return (sys_cpuset_setaffinity(td, &ap));
2554 }
2555 
2556 int
2557 freebsd32_nmount(struct thread *td,
2558     struct freebsd32_nmount_args /* {
2559     	struct iovec *iovp;
2560     	unsigned int iovcnt;
2561     	int flags;
2562     } */ *uap)
2563 {
2564 	struct uio *auio;
2565 	uint64_t flags;
2566 	int error;
2567 
2568 	/*
2569 	 * Mount flags are now 64-bits. On 32-bit archtectures only
2570 	 * 32-bits are passed in, but from here on everything handles
2571 	 * 64-bit flags correctly.
2572 	 */
2573 	flags = uap->flags;
2574 
2575 	AUDIT_ARG_FFLAGS(flags);
2576 
2577 	/*
2578 	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2579 	 * userspace to set this flag, but we must filter it out if we want
2580 	 * MNT_UPDATE on the root file system to work.
2581 	 * MNT_ROOTFS should only be set by the kernel when mounting its
2582 	 * root file system.
2583 	 */
2584 	flags &= ~MNT_ROOTFS;
2585 
2586 	/*
2587 	 * check that we have an even number of iovec's
2588 	 * and that we have at least two options.
2589 	 */
2590 	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2591 		return (EINVAL);
2592 
2593 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2594 	if (error)
2595 		return (error);
2596 	error = vfs_donmount(td, flags, auio);
2597 
2598 	free(auio, M_IOV);
2599 	return error;
2600 }
2601 
2602 #if 0
2603 int
2604 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2605 {
2606 	struct yyy32 *p32, s32;
2607 	struct yyy *p = NULL, s;
2608 	struct xxx_arg ap;
2609 	int error;
2610 
2611 	if (uap->zzz) {
2612 		error = copyin(uap->zzz, &s32, sizeof(s32));
2613 		if (error)
2614 			return (error);
2615 		/* translate in */
2616 		p = &s;
2617 	}
2618 	error = kern_xxx(td, p);
2619 	if (error)
2620 		return (error);
2621 	if (uap->zzz) {
2622 		/* translate out */
2623 		error = copyout(&s32, p32, sizeof(s32));
2624 	}
2625 	return (error);
2626 }
2627 #endif
2628 
2629 int
2630 syscall32_register(int *offset, struct sysent *new_sysent,
2631     struct sysent *old_sysent, int flags)
2632 {
2633 
2634 	if ((flags & ~SY_THR_STATIC) != 0)
2635 		return (EINVAL);
2636 
2637 	if (*offset == NO_SYSCALL) {
2638 		int i;
2639 
2640 		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2641 			if (freebsd32_sysent[i].sy_call ==
2642 			    (sy_call_t *)lkmnosys)
2643 				break;
2644 		if (i == SYS_MAXSYSCALL)
2645 			return (ENFILE);
2646 		*offset = i;
2647 	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2648 		return (EINVAL);
2649 	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2650 	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2651 		return (EEXIST);
2652 
2653 	*old_sysent = freebsd32_sysent[*offset];
2654 	freebsd32_sysent[*offset] = *new_sysent;
2655 	atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2656 	return (0);
2657 }
2658 
2659 int
2660 syscall32_deregister(int *offset, struct sysent *old_sysent)
2661 {
2662 
2663 	if (*offset == 0)
2664 		return (0);
2665 
2666 	freebsd32_sysent[*offset] = *old_sysent;
2667 	return (0);
2668 }
2669 
2670 int
2671 syscall32_module_handler(struct module *mod, int what, void *arg)
2672 {
2673 	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2674 	modspecific_t ms;
2675 	int error;
2676 
2677 	switch (what) {
2678 	case MOD_LOAD:
2679 		error = syscall32_register(data->offset, data->new_sysent,
2680 		    &data->old_sysent, SY_THR_STATIC_KLD);
2681 		if (error) {
2682 			/* Leave a mark so we know to safely unload below. */
2683 			data->offset = NULL;
2684 			return error;
2685 		}
2686 		ms.intval = *data->offset;
2687 		MOD_XLOCK;
2688 		module_setspecific(mod, &ms);
2689 		MOD_XUNLOCK;
2690 		if (data->chainevh)
2691 			error = data->chainevh(mod, what, data->chainarg);
2692 		return (error);
2693 	case MOD_UNLOAD:
2694 		/*
2695 		 * MOD_LOAD failed, so just return without calling the
2696 		 * chained handler since we didn't pass along the MOD_LOAD
2697 		 * event.
2698 		 */
2699 		if (data->offset == NULL)
2700 			return (0);
2701 		if (data->chainevh) {
2702 			error = data->chainevh(mod, what, data->chainarg);
2703 			if (error)
2704 				return (error);
2705 		}
2706 		error = syscall32_deregister(data->offset, &data->old_sysent);
2707 		return (error);
2708 	default:
2709 		error = EOPNOTSUPP;
2710 		if (data->chainevh)
2711 			error = data->chainevh(mod, what, data->chainarg);
2712 		return (error);
2713 	}
2714 }
2715 
2716 int
2717 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2718 {
2719 	struct syscall_helper_data *sd1;
2720 	int error;
2721 
2722 	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2723 		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2724 		    &sd1->old_sysent, flags);
2725 		if (error != 0) {
2726 			syscall32_helper_unregister(sd);
2727 			return (error);
2728 		}
2729 		sd1->registered = 1;
2730 	}
2731 	return (0);
2732 }
2733 
2734 int
2735 syscall32_helper_unregister(struct syscall_helper_data *sd)
2736 {
2737 	struct syscall_helper_data *sd1;
2738 
2739 	for (sd1 = sd; sd1->registered != 0; sd1++) {
2740 		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2741 		sd1->registered = 0;
2742 	}
2743 	return (0);
2744 }
2745 
2746 register_t *
2747 freebsd32_copyout_strings(struct image_params *imgp)
2748 {
2749 	int argc, envc, i;
2750 	u_int32_t *vectp;
2751 	char *stringp;
2752 	uintptr_t destp;
2753 	u_int32_t *stack_base;
2754 	struct freebsd32_ps_strings *arginfo;
2755 	char canary[sizeof(long) * 8];
2756 	int32_t pagesizes32[MAXPAGESIZES];
2757 	size_t execpath_len;
2758 	int szsigcode;
2759 
2760 	/*
2761 	 * Calculate string base and vector table pointers.
2762 	 * Also deal with signal trampoline code for this exec type.
2763 	 */
2764 	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2765 		execpath_len = strlen(imgp->execpath) + 1;
2766 	else
2767 		execpath_len = 0;
2768 	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2769 	    sv_psstrings;
2770 	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2771 		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2772 	else
2773 		szsigcode = 0;
2774 	destp =	(uintptr_t)arginfo;
2775 
2776 	/*
2777 	 * install sigcode
2778 	 */
2779 	if (szsigcode != 0) {
2780 		destp -= szsigcode;
2781 		destp = rounddown2(destp, sizeof(uint32_t));
2782 		copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2783 		    szsigcode);
2784 	}
2785 
2786 	/*
2787 	 * Copy the image path for the rtld.
2788 	 */
2789 	if (execpath_len != 0) {
2790 		destp -= execpath_len;
2791 		imgp->execpathp = destp;
2792 		copyout(imgp->execpath, (void *)destp, execpath_len);
2793 	}
2794 
2795 	/*
2796 	 * Prepare the canary for SSP.
2797 	 */
2798 	arc4rand(canary, sizeof(canary), 0);
2799 	destp -= sizeof(canary);
2800 	imgp->canary = destp;
2801 	copyout(canary, (void *)destp, sizeof(canary));
2802 	imgp->canarylen = sizeof(canary);
2803 
2804 	/*
2805 	 * Prepare the pagesizes array.
2806 	 */
2807 	for (i = 0; i < MAXPAGESIZES; i++)
2808 		pagesizes32[i] = (uint32_t)pagesizes[i];
2809 	destp -= sizeof(pagesizes32);
2810 	destp = rounddown2(destp, sizeof(uint32_t));
2811 	imgp->pagesizes = destp;
2812 	copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2813 	imgp->pagesizeslen = sizeof(pagesizes32);
2814 
2815 	destp -= ARG_MAX - imgp->args->stringspace;
2816 	destp = rounddown2(destp, sizeof(uint32_t));
2817 
2818 	/*
2819 	 * If we have a valid auxargs ptr, prepare some room
2820 	 * on the stack.
2821 	 */
2822 	if (imgp->auxargs) {
2823 		/*
2824 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2825 		 * lower compatibility.
2826 		 */
2827 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2828 			: (AT_COUNT * 2);
2829 		/*
2830 		 * The '+ 2' is for the null pointers at the end of each of
2831 		 * the arg and env vector sets,and imgp->auxarg_size is room
2832 		 * for argument of Runtime loader.
2833 		 */
2834 		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2835 		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2836 		    sizeof(u_int32_t));
2837 	} else {
2838 		/*
2839 		 * The '+ 2' is for the null pointers at the end of each of
2840 		 * the arg and env vector sets
2841 		 */
2842 		vectp = (u_int32_t *)(destp - (imgp->args->argc +
2843 		    imgp->args->envc + 2) * sizeof(u_int32_t));
2844 	}
2845 
2846 	/*
2847 	 * vectp also becomes our initial stack base
2848 	 */
2849 	stack_base = vectp;
2850 
2851 	stringp = imgp->args->begin_argv;
2852 	argc = imgp->args->argc;
2853 	envc = imgp->args->envc;
2854 	/*
2855 	 * Copy out strings - arguments and environment.
2856 	 */
2857 	copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2858 
2859 	/*
2860 	 * Fill in "ps_strings" struct for ps, w, etc.
2861 	 */
2862 	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2863 	suword32(&arginfo->ps_nargvstr, argc);
2864 
2865 	/*
2866 	 * Fill in argument portion of vector table.
2867 	 */
2868 	for (; argc > 0; --argc) {
2869 		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2870 		while (*stringp++ != 0)
2871 			destp++;
2872 		destp++;
2873 	}
2874 
2875 	/* a null vector table pointer separates the argp's from the envp's */
2876 	suword32(vectp++, 0);
2877 
2878 	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2879 	suword32(&arginfo->ps_nenvstr, envc);
2880 
2881 	/*
2882 	 * Fill in environment portion of vector table.
2883 	 */
2884 	for (; envc > 0; --envc) {
2885 		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2886 		while (*stringp++ != 0)
2887 			destp++;
2888 		destp++;
2889 	}
2890 
2891 	/* end of vector table is a null pointer */
2892 	suword32(vectp, 0);
2893 
2894 	return ((register_t *)stack_base);
2895 }
2896 
2897 int
2898 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2899 {
2900 	struct kld_file_stat stat;
2901 	struct kld32_file_stat stat32;
2902 	int error, version;
2903 
2904 	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2905 	    != 0)
2906 		return (error);
2907 	if (version != sizeof(struct kld32_file_stat_1) &&
2908 	    version != sizeof(struct kld32_file_stat))
2909 		return (EINVAL);
2910 
2911 	error = kern_kldstat(td, uap->fileid, &stat);
2912 	if (error != 0)
2913 		return (error);
2914 
2915 	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2916 	CP(stat, stat32, refs);
2917 	CP(stat, stat32, id);
2918 	PTROUT_CP(stat, stat32, address);
2919 	CP(stat, stat32, size);
2920 	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2921 	return (copyout(&stat32, uap->stat, version));
2922 }
2923 
2924 int
2925 freebsd32_posix_fallocate(struct thread *td,
2926     struct freebsd32_posix_fallocate_args *uap)
2927 {
2928 
2929 	td->td_retval[0] = kern_posix_fallocate(td, uap->fd,
2930 	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
2931 	return (0);
2932 }
2933 
2934 int
2935 freebsd32_posix_fadvise(struct thread *td,
2936     struct freebsd32_posix_fadvise_args *uap)
2937 {
2938 
2939 	td->td_retval[0] = kern_posix_fadvise(td, uap->fd,
2940 	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len),
2941 	    uap->advice);
2942 	return (0);
2943 }
2944 
2945 int
2946 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2947 {
2948 
2949 	CP(*sig32, *sig, sigev_notify);
2950 	switch (sig->sigev_notify) {
2951 	case SIGEV_NONE:
2952 		break;
2953 	case SIGEV_THREAD_ID:
2954 		CP(*sig32, *sig, sigev_notify_thread_id);
2955 		/* FALLTHROUGH */
2956 	case SIGEV_SIGNAL:
2957 		CP(*sig32, *sig, sigev_signo);
2958 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2959 		break;
2960 	case SIGEV_KEVENT:
2961 		CP(*sig32, *sig, sigev_notify_kqueue);
2962 		CP(*sig32, *sig, sigev_notify_kevent_flags);
2963 		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2964 		break;
2965 	default:
2966 		return (EINVAL);
2967 	}
2968 	return (0);
2969 }
2970 
2971 int
2972 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
2973 {
2974 	void *data;
2975 	int error, flags;
2976 
2977 	switch (uap->com) {
2978 	case PROC_SPROTECT:
2979 		error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
2980 		if (error)
2981 			return (error);
2982 		data = &flags;
2983 		break;
2984 	default:
2985 		return (EINVAL);
2986 	}
2987 	return (kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
2988 	    uap->com, data));
2989 }
2990 
2991 int
2992 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
2993 {
2994 	long tmp;
2995 
2996 	switch (uap->cmd) {
2997 	/*
2998 	 * Do unsigned conversion for arg when operation
2999 	 * interprets it as flags or pointer.
3000 	 */
3001 	case F_SETLK_REMOTE:
3002 	case F_SETLKW:
3003 	case F_SETLK:
3004 	case F_GETLK:
3005 	case F_SETFD:
3006 	case F_SETFL:
3007 		tmp = (unsigned int)(uap->arg);
3008 		break;
3009 	default:
3010 		tmp = uap->arg;
3011 		break;
3012 	}
3013 	return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3014 }
3015