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