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