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