xref: /freebsd/sys/compat/freebsd32/freebsd32_misc.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
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 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/clock.h>
36 #include <sys/exec.h>
37 #include <sys/fcntl.h>
38 #include <sys/filedesc.h>
39 #include <sys/namei.h>
40 #include <sys/imgact.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/file.h>		/* Must come after sys/malloc.h */
46 #include <sys/mbuf.h>
47 #include <sys/mman.h>
48 #include <sys/module.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
51 #include <sys/proc.h>
52 #include <sys/reboot.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/selinfo.h>
56 #include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
57 #include <sys/pipe.h>		/* Must come after sys/selinfo.h */
58 #include <sys/signal.h>
59 #include <sys/signalvar.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/stat.h>
63 #include <sys/syscall.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/sysproto.h>
68 #include <sys/thr.h>
69 #include <sys/unistd.h>
70 #include <sys/ucontext.h>
71 #include <sys/vnode.h>
72 #include <sys/wait.h>
73 #include <sys/ipc.h>
74 #include <sys/msg.h>
75 #include <sys/sem.h>
76 #include <sys/shm.h>
77 
78 #include <vm/vm.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_param.h>
81 #include <vm/pmap.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_extern.h>
85 
86 #include <machine/cpu.h>
87 
88 #include <compat/freebsd32/freebsd32_util.h>
89 #include <compat/freebsd32/freebsd32.h>
90 #include <compat/freebsd32/freebsd32_ipc.h>
91 #include <compat/freebsd32/freebsd32_signal.h>
92 #include <compat/freebsd32/freebsd32_proto.h>
93 
94 CTASSERT(sizeof(struct timeval32) == 8);
95 CTASSERT(sizeof(struct timespec32) == 8);
96 CTASSERT(sizeof(struct itimerval32) == 16);
97 CTASSERT(sizeof(struct statfs32) == 256);
98 CTASSERT(sizeof(struct rusage32) == 72);
99 CTASSERT(sizeof(struct sigaltstack32) == 12);
100 CTASSERT(sizeof(struct kevent32) == 20);
101 CTASSERT(sizeof(struct iovec32) == 8);
102 CTASSERT(sizeof(struct msghdr32) == 28);
103 CTASSERT(sizeof(struct stat32) == 96);
104 CTASSERT(sizeof(struct sigaction32) == 24);
105 
106 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
107 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
108 
109 int
110 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
111 {
112 	int error, status;
113 	struct rusage32 ru32;
114 	struct rusage ru, *rup;
115 
116 	if (uap->rusage != NULL)
117 		rup = &ru;
118 	else
119 		rup = NULL;
120 	error = kern_wait(td, uap->pid, &status, uap->options, rup);
121 	if (error)
122 		return (error);
123 	if (uap->status != NULL)
124 		error = copyout(&status, uap->status, sizeof(status));
125 	if (uap->rusage != NULL && error == 0) {
126 		TV_CP(ru, ru32, ru_utime);
127 		TV_CP(ru, ru32, ru_stime);
128 		CP(ru, ru32, ru_maxrss);
129 		CP(ru, ru32, ru_ixrss);
130 		CP(ru, ru32, ru_idrss);
131 		CP(ru, ru32, ru_isrss);
132 		CP(ru, ru32, ru_minflt);
133 		CP(ru, ru32, ru_majflt);
134 		CP(ru, ru32, ru_nswap);
135 		CP(ru, ru32, ru_inblock);
136 		CP(ru, ru32, ru_oublock);
137 		CP(ru, ru32, ru_msgsnd);
138 		CP(ru, ru32, ru_msgrcv);
139 		CP(ru, ru32, ru_nsignals);
140 		CP(ru, ru32, ru_nvcsw);
141 		CP(ru, ru32, ru_nivcsw);
142 		error = copyout(&ru32, uap->rusage, sizeof(ru32));
143 	}
144 	return (error);
145 }
146 
147 #ifdef COMPAT_FREEBSD4
148 static void
149 copy_statfs(struct statfs *in, struct statfs32 *out)
150 {
151 
152 	statfs_scale_blocks(in, INT32_MAX);
153 	bzero(out, sizeof(*out));
154 	CP(*in, *out, f_bsize);
155 	out->f_iosize = MIN(in->f_iosize, INT32_MAX);
156 	CP(*in, *out, f_blocks);
157 	CP(*in, *out, f_bfree);
158 	CP(*in, *out, f_bavail);
159 	out->f_files = MIN(in->f_files, INT32_MAX);
160 	out->f_ffree = MIN(in->f_ffree, INT32_MAX);
161 	CP(*in, *out, f_fsid);
162 	CP(*in, *out, f_owner);
163 	CP(*in, *out, f_type);
164 	CP(*in, *out, f_flags);
165 	out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
166 	out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
167 	strlcpy(out->f_fstypename,
168 	      in->f_fstypename, MFSNAMELEN);
169 	strlcpy(out->f_mntonname,
170 	      in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
171 	out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
172 	out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
173 	strlcpy(out->f_mntfromname,
174 	      in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
175 }
176 #endif
177 
178 #ifdef COMPAT_FREEBSD4
179 int
180 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
181 {
182 	struct statfs *buf, *sp;
183 	struct statfs32 stat32;
184 	size_t count, size;
185 	int error;
186 
187 	count = uap->bufsize / sizeof(struct statfs32);
188 	size = count * sizeof(struct statfs);
189 	error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
190 	if (size > 0) {
191 		count = td->td_retval[0];
192 		sp = buf;
193 		while (count > 0 && error == 0) {
194 			copy_statfs(sp, &stat32);
195 			error = copyout(&stat32, uap->buf, sizeof(stat32));
196 			sp++;
197 			uap->buf++;
198 			count--;
199 		}
200 		free(buf, M_TEMP);
201 	}
202 	return (error);
203 }
204 #endif
205 
206 int
207 freebsd32_sigaltstack(struct thread *td,
208 		      struct freebsd32_sigaltstack_args *uap)
209 {
210 	struct sigaltstack32 s32;
211 	struct sigaltstack ss, oss, *ssp;
212 	int error;
213 
214 	if (uap->ss != NULL) {
215 		error = copyin(uap->ss, &s32, sizeof(s32));
216 		if (error)
217 			return (error);
218 		PTRIN_CP(s32, ss, ss_sp);
219 		CP(s32, ss, ss_size);
220 		CP(s32, ss, ss_flags);
221 		ssp = &ss;
222 	} else
223 		ssp = NULL;
224 	error = kern_sigaltstack(td, ssp, &oss);
225 	if (error == 0 && uap->oss != NULL) {
226 		PTROUT_CP(oss, s32, ss_sp);
227 		CP(oss, s32, ss_size);
228 		CP(oss, s32, ss_flags);
229 		error = copyout(&s32, uap->oss, sizeof(s32));
230 	}
231 	return (error);
232 }
233 
234 /*
235  * Custom version of exec_copyin_args() so that we can translate
236  * the pointers.
237  */
238 static int
239 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
240     enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
241 {
242 	char *argp, *envp;
243 	u_int32_t *p32, arg;
244 	size_t length;
245 	int error;
246 
247 	bzero(args, sizeof(*args));
248 	if (argv == NULL)
249 		return (EFAULT);
250 
251 	/*
252 	 * Allocate temporary demand zeroed space for argument and
253 	 *	environment strings
254 	 */
255 	args->buf = (char *) kmem_alloc_wait(exec_map,
256 	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
257 	if (args->buf == NULL)
258 		return (ENOMEM);
259 	args->begin_argv = args->buf;
260 	args->endp = args->begin_argv;
261 	args->stringspace = ARG_MAX;
262 
263 	/*
264 	 * Copy the file name.
265 	 */
266 	if (fname != NULL) {
267 		args->fname = args->buf + ARG_MAX;
268 		error = (segflg == UIO_SYSSPACE) ?
269 		    copystr(fname, args->fname, PATH_MAX, &length) :
270 		    copyinstr(fname, args->fname, PATH_MAX, &length);
271 		if (error != 0)
272 			goto err_exit;
273 	} else
274 		args->fname = NULL;
275 
276 	/*
277 	 * extract arguments first
278 	 */
279 	p32 = argv;
280 	for (;;) {
281 		error = copyin(p32++, &arg, sizeof(arg));
282 		if (error)
283 			goto err_exit;
284 		if (arg == 0)
285 			break;
286 		argp = PTRIN(arg);
287 		error = copyinstr(argp, args->endp, args->stringspace, &length);
288 		if (error) {
289 			if (error == ENAMETOOLONG)
290 				error = E2BIG;
291 			goto err_exit;
292 		}
293 		args->stringspace -= length;
294 		args->endp += length;
295 		args->argc++;
296 	}
297 
298 	args->begin_envv = args->endp;
299 
300 	/*
301 	 * extract environment strings
302 	 */
303 	if (envv) {
304 		p32 = envv;
305 		for (;;) {
306 			error = copyin(p32++, &arg, sizeof(arg));
307 			if (error)
308 				goto err_exit;
309 			if (arg == 0)
310 				break;
311 			envp = PTRIN(arg);
312 			error = copyinstr(envp, args->endp, args->stringspace,
313 			    &length);
314 			if (error) {
315 				if (error == ENAMETOOLONG)
316 					error = E2BIG;
317 				goto err_exit;
318 			}
319 			args->stringspace -= length;
320 			args->endp += length;
321 			args->envc++;
322 		}
323 	}
324 
325 	return (0);
326 
327 err_exit:
328 	kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
329 	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
330 	args->buf = NULL;
331 	return (error);
332 }
333 
334 int
335 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
336 {
337 	struct image_args eargs;
338 	int error;
339 
340 	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
341 	    uap->argv, uap->envv);
342 	if (error == 0)
343 		error = kern_execve(td, &eargs, NULL);
344 	return (error);
345 }
346 
347 int
348 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
349 {
350 	struct image_args eargs;
351 	int error;
352 
353 	error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
354 	    uap->argv, uap->envv);
355 	if (error == 0) {
356 		eargs.fd = uap->fd;
357 		error = kern_execve(td, &eargs, NULL);
358 	}
359 	return (error);
360 }
361 
362 #ifdef __ia64__
363 static int
364 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
365 		       int prot, int fd, off_t pos)
366 {
367 	vm_map_t map;
368 	vm_map_entry_t entry;
369 	int rv;
370 
371 	map = &td->td_proc->p_vmspace->vm_map;
372 	if (fd != -1)
373 		prot |= VM_PROT_WRITE;
374 
375 	if (vm_map_lookup_entry(map, start, &entry)) {
376 		if ((entry->protection & prot) != prot) {
377 			rv = vm_map_protect(map,
378 					    trunc_page(start),
379 					    round_page(end),
380 					    entry->protection | prot,
381 					    FALSE);
382 			if (rv != KERN_SUCCESS)
383 				return (EINVAL);
384 		}
385 	} else {
386 		vm_offset_t addr = trunc_page(start);
387 		rv = vm_map_find(map, 0, 0,
388 				 &addr, PAGE_SIZE, FALSE, prot,
389 				 VM_PROT_ALL, 0);
390 		if (rv != KERN_SUCCESS)
391 			return (EINVAL);
392 	}
393 
394 	if (fd != -1) {
395 		struct pread_args r;
396 		r.fd = fd;
397 		r.buf = (void *) start;
398 		r.nbyte = end - start;
399 		r.offset = pos;
400 		return (pread(td, &r));
401 	} else {
402 		while (start < end) {
403 			subyte((void *) start, 0);
404 			start++;
405 		}
406 		return (0);
407 	}
408 }
409 #endif
410 
411 int
412 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
413 {
414 	struct mmap_args ap;
415 	vm_offset_t addr = (vm_offset_t) uap->addr;
416 	vm_size_t len	 = uap->len;
417 	int prot	 = uap->prot;
418 	int flags	 = uap->flags;
419 	int fd		 = uap->fd;
420 	off_t pos	 = (uap->poslo
421 			    | ((off_t)uap->poshi << 32));
422 #ifdef __ia64__
423 	vm_size_t pageoff;
424 	int error;
425 
426 	/*
427 	 * Attempt to handle page size hassles.
428 	 */
429 	pageoff = (pos & PAGE_MASK);
430 	if (flags & MAP_FIXED) {
431 		vm_offset_t start, end;
432 		start = addr;
433 		end = addr + len;
434 
435 		if (start != trunc_page(start)) {
436 			error = freebsd32_mmap_partial(td, start,
437 						       round_page(start), prot,
438 						       fd, pos);
439 			if (fd != -1)
440 				pos += round_page(start) - start;
441 			start = round_page(start);
442 		}
443 		if (end != round_page(end)) {
444 			vm_offset_t t = trunc_page(end);
445 			error = freebsd32_mmap_partial(td, t, end,
446 						  prot, fd,
447 						  pos + t - start);
448 			end = trunc_page(end);
449 		}
450 		if (end > start && fd != -1 && (pos & PAGE_MASK)) {
451 			/*
452 			 * We can't map this region at all. The specified
453 			 * address doesn't have the same alignment as the file
454 			 * position. Fake the mapping by simply reading the
455 			 * entire region into memory. First we need to make
456 			 * sure the region exists.
457 			 */
458 			vm_map_t map;
459 			struct pread_args r;
460 			int rv;
461 
462 			prot |= VM_PROT_WRITE;
463 			map = &td->td_proc->p_vmspace->vm_map;
464 			rv = vm_map_remove(map, start, end);
465 			if (rv != KERN_SUCCESS)
466 				return (EINVAL);
467 			rv = vm_map_find(map, 0, 0,
468 					 &start, end - start, FALSE,
469 					 prot, VM_PROT_ALL, 0);
470 			if (rv != KERN_SUCCESS)
471 				return (EINVAL);
472 			r.fd = fd;
473 			r.buf = (void *) start;
474 			r.nbyte = end - start;
475 			r.offset = pos;
476 			error = pread(td, &r);
477 			if (error)
478 				return (error);
479 
480 			td->td_retval[0] = addr;
481 			return (0);
482 		}
483 		if (end == start) {
484 			/*
485 			 * After dealing with the ragged ends, there
486 			 * might be none left.
487 			 */
488 			td->td_retval[0] = addr;
489 			return (0);
490 		}
491 		addr = start;
492 		len = end - start;
493 	}
494 #endif
495 
496 	ap.addr = (void *) addr;
497 	ap.len = len;
498 	ap.prot = prot;
499 	ap.flags = flags;
500 	ap.fd = fd;
501 	ap.pos = pos;
502 
503 	return (mmap(td, &ap));
504 }
505 
506 #ifdef COMPAT_FREEBSD6
507 int
508 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
509 {
510 	struct freebsd32_mmap_args ap;
511 
512 	ap.addr = uap->addr;
513 	ap.len = uap->len;
514 	ap.prot = uap->prot;
515 	ap.flags = uap->flags;
516 	ap.fd = uap->fd;
517 	ap.poslo = uap->poslo;
518 	ap.poshi = uap->poshi;
519 
520 	return (freebsd32_mmap(td, &ap));
521 }
522 #endif
523 
524 int
525 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
526 {
527 	struct itimerval itv, oitv, *itvp;
528 	struct itimerval32 i32;
529 	int error;
530 
531 	if (uap->itv != NULL) {
532 		error = copyin(uap->itv, &i32, sizeof(i32));
533 		if (error)
534 			return (error);
535 		TV_CP(i32, itv, it_interval);
536 		TV_CP(i32, itv, it_value);
537 		itvp = &itv;
538 	} else
539 		itvp = NULL;
540 	error = kern_setitimer(td, uap->which, itvp, &oitv);
541 	if (error || uap->oitv == NULL)
542 		return (error);
543 	TV_CP(oitv, i32, it_interval);
544 	TV_CP(oitv, i32, it_value);
545 	return (copyout(&i32, uap->oitv, sizeof(i32)));
546 }
547 
548 int
549 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
550 {
551 	struct itimerval itv;
552 	struct itimerval32 i32;
553 	int error;
554 
555 	error = kern_getitimer(td, uap->which, &itv);
556 	if (error || uap->itv == NULL)
557 		return (error);
558 	TV_CP(itv, i32, it_interval);
559 	TV_CP(itv, i32, it_value);
560 	return (copyout(&i32, uap->itv, sizeof(i32)));
561 }
562 
563 int
564 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
565 {
566 	struct timeval32 tv32;
567 	struct timeval tv, *tvp;
568 	int error;
569 
570 	if (uap->tv != NULL) {
571 		error = copyin(uap->tv, &tv32, sizeof(tv32));
572 		if (error)
573 			return (error);
574 		CP(tv32, tv, tv_sec);
575 		CP(tv32, tv, tv_usec);
576 		tvp = &tv;
577 	} else
578 		tvp = NULL;
579 	/*
580 	 * XXX big-endian needs to convert the fd_sets too.
581 	 * XXX Do pointers need PTRIN()?
582 	 */
583 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
584 }
585 
586 /*
587  * Copy 'count' items into the destination list pointed to by uap->eventlist.
588  */
589 static int
590 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
591 {
592 	struct freebsd32_kevent_args *uap;
593 	struct kevent32	ks32[KQ_NEVENTS];
594 	int i, error = 0;
595 
596 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
597 	uap = (struct freebsd32_kevent_args *)arg;
598 
599 	for (i = 0; i < count; i++) {
600 		CP(kevp[i], ks32[i], ident);
601 		CP(kevp[i], ks32[i], filter);
602 		CP(kevp[i], ks32[i], flags);
603 		CP(kevp[i], ks32[i], fflags);
604 		CP(kevp[i], ks32[i], data);
605 		PTROUT_CP(kevp[i], ks32[i], udata);
606 	}
607 	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
608 	if (error == 0)
609 		uap->eventlist += count;
610 	return (error);
611 }
612 
613 /*
614  * Copy 'count' items from the list pointed to by uap->changelist.
615  */
616 static int
617 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
618 {
619 	struct freebsd32_kevent_args *uap;
620 	struct kevent32	ks32[KQ_NEVENTS];
621 	int i, error = 0;
622 
623 	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
624 	uap = (struct freebsd32_kevent_args *)arg;
625 
626 	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
627 	if (error)
628 		goto done;
629 	uap->changelist += count;
630 
631 	for (i = 0; i < count; i++) {
632 		CP(ks32[i], kevp[i], ident);
633 		CP(ks32[i], kevp[i], filter);
634 		CP(ks32[i], kevp[i], flags);
635 		CP(ks32[i], kevp[i], fflags);
636 		CP(ks32[i], kevp[i], data);
637 		PTRIN_CP(ks32[i], kevp[i], udata);
638 	}
639 done:
640 	return (error);
641 }
642 
643 int
644 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
645 {
646 	struct timespec32 ts32;
647 	struct timespec ts, *tsp;
648 	struct kevent_copyops k_ops = { uap,
649 					freebsd32_kevent_copyout,
650 					freebsd32_kevent_copyin};
651 	int error;
652 
653 
654 	if (uap->timeout) {
655 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
656 		if (error)
657 			return (error);
658 		CP(ts32, ts, tv_sec);
659 		CP(ts32, ts, tv_nsec);
660 		tsp = &ts;
661 	} else
662 		tsp = NULL;
663 	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
664 	    &k_ops, tsp);
665 	return (error);
666 }
667 
668 int
669 freebsd32_gettimeofday(struct thread *td,
670 		       struct freebsd32_gettimeofday_args *uap)
671 {
672 	struct timeval atv;
673 	struct timeval32 atv32;
674 	struct timezone rtz;
675 	int error = 0;
676 
677 	if (uap->tp) {
678 		microtime(&atv);
679 		CP(atv, atv32, tv_sec);
680 		CP(atv, atv32, tv_usec);
681 		error = copyout(&atv32, uap->tp, sizeof (atv32));
682 	}
683 	if (error == 0 && uap->tzp != NULL) {
684 		rtz.tz_minuteswest = tz_minuteswest;
685 		rtz.tz_dsttime = tz_dsttime;
686 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
687 	}
688 	return (error);
689 }
690 
691 int
692 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
693 {
694 	struct rusage32 s32;
695 	struct rusage s;
696 	int error;
697 
698 	error = kern_getrusage(td, uap->who, &s);
699 	if (error)
700 		return (error);
701 	if (uap->rusage != NULL) {
702 		TV_CP(s, s32, ru_utime);
703 		TV_CP(s, s32, ru_stime);
704 		CP(s, s32, ru_maxrss);
705 		CP(s, s32, ru_ixrss);
706 		CP(s, s32, ru_idrss);
707 		CP(s, s32, ru_isrss);
708 		CP(s, s32, ru_minflt);
709 		CP(s, s32, ru_majflt);
710 		CP(s, s32, ru_nswap);
711 		CP(s, s32, ru_inblock);
712 		CP(s, s32, ru_oublock);
713 		CP(s, s32, ru_msgsnd);
714 		CP(s, s32, ru_msgrcv);
715 		CP(s, s32, ru_nsignals);
716 		CP(s, s32, ru_nvcsw);
717 		CP(s, s32, ru_nivcsw);
718 		error = copyout(&s32, uap->rusage, sizeof(s32));
719 	}
720 	return (error);
721 }
722 
723 static int
724 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
725 {
726 	struct iovec32 iov32;
727 	struct iovec *iov;
728 	struct uio *uio;
729 	u_int iovlen;
730 	int error, i;
731 
732 	*uiop = NULL;
733 	if (iovcnt > UIO_MAXIOV)
734 		return (EINVAL);
735 	iovlen = iovcnt * sizeof(struct iovec);
736 	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
737 	iov = (struct iovec *)(uio + 1);
738 	for (i = 0; i < iovcnt; i++) {
739 		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
740 		if (error) {
741 			free(uio, M_IOV);
742 			return (error);
743 		}
744 		iov[i].iov_base = PTRIN(iov32.iov_base);
745 		iov[i].iov_len = iov32.iov_len;
746 	}
747 	uio->uio_iov = iov;
748 	uio->uio_iovcnt = iovcnt;
749 	uio->uio_segflg = UIO_USERSPACE;
750 	uio->uio_offset = -1;
751 	uio->uio_resid = 0;
752 	for (i = 0; i < iovcnt; i++) {
753 		if (iov->iov_len > INT_MAX - uio->uio_resid) {
754 			free(uio, M_IOV);
755 			return (EINVAL);
756 		}
757 		uio->uio_resid += iov->iov_len;
758 		iov++;
759 	}
760 	*uiop = uio;
761 	return (0);
762 }
763 
764 int
765 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
766 {
767 	struct uio *auio;
768 	int error;
769 
770 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
771 	if (error)
772 		return (error);
773 	error = kern_readv(td, uap->fd, auio);
774 	free(auio, M_IOV);
775 	return (error);
776 }
777 
778 int
779 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
780 {
781 	struct uio *auio;
782 	int error;
783 
784 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
785 	if (error)
786 		return (error);
787 	error = kern_writev(td, uap->fd, auio);
788 	free(auio, M_IOV);
789 	return (error);
790 }
791 
792 int
793 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
794 {
795 	struct uio *auio;
796 	int error;
797 
798 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
799 	if (error)
800 		return (error);
801 	error = kern_preadv(td, uap->fd, auio, uap->offset);
802 	free(auio, M_IOV);
803 	return (error);
804 }
805 
806 int
807 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
808 {
809 	struct uio *auio;
810 	int error;
811 
812 	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
813 	if (error)
814 		return (error);
815 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
816 	free(auio, M_IOV);
817 	return (error);
818 }
819 
820 static int
821 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
822     int error)
823 {
824 	struct iovec32 iov32;
825 	struct iovec *iov;
826 	u_int iovlen;
827 	int i;
828 
829 	*iovp = NULL;
830 	if (iovcnt > UIO_MAXIOV)
831 		return (error);
832 	iovlen = iovcnt * sizeof(struct iovec);
833 	iov = malloc(iovlen, M_IOV, M_WAITOK);
834 	for (i = 0; i < iovcnt; i++) {
835 		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
836 		if (error) {
837 			free(iov, M_IOV);
838 			return (error);
839 		}
840 		iov[i].iov_base = PTRIN(iov32.iov_base);
841 		iov[i].iov_len = iov32.iov_len;
842 	}
843 	*iovp = iov;
844 	return (0);
845 }
846 
847 static int
848 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
849 {
850 	struct msghdr32 m32;
851 	int error;
852 
853 	error = copyin(msg32, &m32, sizeof(m32));
854 	if (error)
855 		return (error);
856 	msg->msg_name = PTRIN(m32.msg_name);
857 	msg->msg_namelen = m32.msg_namelen;
858 	msg->msg_iov = PTRIN(m32.msg_iov);
859 	msg->msg_iovlen = m32.msg_iovlen;
860 	msg->msg_control = PTRIN(m32.msg_control);
861 	msg->msg_controllen = m32.msg_controllen;
862 	msg->msg_flags = m32.msg_flags;
863 	return (0);
864 }
865 
866 static int
867 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
868 {
869 	struct msghdr32 m32;
870 	int error;
871 
872 	m32.msg_name = PTROUT(msg->msg_name);
873 	m32.msg_namelen = msg->msg_namelen;
874 	m32.msg_iov = PTROUT(msg->msg_iov);
875 	m32.msg_iovlen = msg->msg_iovlen;
876 	m32.msg_control = PTROUT(msg->msg_control);
877 	m32.msg_controllen = msg->msg_controllen;
878 	m32.msg_flags = msg->msg_flags;
879 	error = copyout(&m32, msg32, sizeof(m32));
880 	return (error);
881 }
882 
883 #define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
884 #define FREEBSD32_ALIGN(p)	\
885 	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
886 #define	FREEBSD32_CMSG_SPACE(l)	\
887 	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
888 
889 #define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
890 				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
891 static int
892 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
893 {
894 	struct cmsghdr *cm;
895 	void *data;
896 	socklen_t clen, datalen;
897 	int error;
898 	caddr_t ctlbuf;
899 	int len, maxlen, copylen;
900 	struct mbuf *m;
901 	error = 0;
902 
903 	len    = msg->msg_controllen;
904 	maxlen = msg->msg_controllen;
905 	msg->msg_controllen = 0;
906 
907 	m = control;
908 	ctlbuf = msg->msg_control;
909 
910 	while (m && len > 0) {
911 		cm = mtod(m, struct cmsghdr *);
912 		clen = m->m_len;
913 
914 		while (cm != NULL) {
915 
916 			if (sizeof(struct cmsghdr) > clen ||
917 			    cm->cmsg_len > clen) {
918 				error = EINVAL;
919 				break;
920 			}
921 
922 			data   = CMSG_DATA(cm);
923 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
924 
925 			/* Adjust message length */
926 			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
927 			    datalen;
928 
929 
930 			/* Copy cmsghdr */
931 			copylen = sizeof(struct cmsghdr);
932 			if (len < copylen) {
933 				msg->msg_flags |= MSG_CTRUNC;
934 				copylen = len;
935 			}
936 
937 			error = copyout(cm,ctlbuf,copylen);
938 			if (error)
939 				goto exit;
940 
941 			ctlbuf += FREEBSD32_ALIGN(copylen);
942 			len    -= FREEBSD32_ALIGN(copylen);
943 
944 			if (len <= 0)
945 				break;
946 
947 			/* Copy data */
948 			copylen = datalen;
949 			if (len < copylen) {
950 				msg->msg_flags |= MSG_CTRUNC;
951 				copylen = len;
952 			}
953 
954 			error = copyout(data,ctlbuf,copylen);
955 			if (error)
956 				goto exit;
957 
958 			ctlbuf += FREEBSD32_ALIGN(copylen);
959 			len    -= FREEBSD32_ALIGN(copylen);
960 
961 			if (CMSG_SPACE(datalen) < clen) {
962 				clen -= CMSG_SPACE(datalen);
963 				cm = (struct cmsghdr *)
964 					((caddr_t)cm + CMSG_SPACE(datalen));
965 			} else {
966 				clen = 0;
967 				cm = NULL;
968 			}
969 		}
970 		m = m->m_next;
971 	}
972 
973 	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
974 
975 exit:
976 	return (error);
977 
978 }
979 
980 int
981 freebsd32_recvmsg(td, uap)
982 	struct thread *td;
983 	struct freebsd32_recvmsg_args /* {
984 		int	s;
985 		struct	msghdr32 *msg;
986 		int	flags;
987 	} */ *uap;
988 {
989 	struct msghdr msg;
990 	struct msghdr32 m32;
991 	struct iovec *uiov, *iov;
992 	struct mbuf *control = NULL;
993 	struct mbuf **controlp;
994 
995 	int error;
996 	error = copyin(uap->msg, &m32, sizeof(m32));
997 	if (error)
998 		return (error);
999 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1000 	if (error)
1001 		return (error);
1002 	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1003 	    EMSGSIZE);
1004 	if (error)
1005 		return (error);
1006 	msg.msg_flags = uap->flags;
1007 	uiov = msg.msg_iov;
1008 	msg.msg_iov = iov;
1009 
1010 	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1011 	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1012 	if (error == 0) {
1013 		msg.msg_iov = uiov;
1014 
1015 		if (control != NULL)
1016 			error = freebsd32_copy_msg_out(&msg, control);
1017 
1018 		if (error == 0)
1019 			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1020 	}
1021 	free(iov, M_IOV);
1022 
1023 	if (control != NULL)
1024 		m_freem(control);
1025 
1026 	return (error);
1027 }
1028 
1029 
1030 static int
1031 freebsd32_convert_msg_in(struct mbuf **controlp)
1032 {
1033 	struct mbuf *control = *controlp;
1034 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1035 	void *data;
1036 	socklen_t clen = control->m_len, datalen;
1037 	int error;
1038 
1039 	error = 0;
1040 	*controlp = NULL;
1041 
1042 	while (cm != NULL) {
1043 		if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1044 			error = EINVAL;
1045 			break;
1046 		}
1047 
1048 		data = FREEBSD32_CMSG_DATA(cm);
1049 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1050 
1051 		*controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1052 		    cm->cmsg_level);
1053 		controlp = &(*controlp)->m_next;
1054 
1055 		if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1056 			clen -= FREEBSD32_CMSG_SPACE(datalen);
1057 			cm = (struct cmsghdr *)
1058 				((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1059 		} else {
1060 			clen = 0;
1061 			cm = NULL;
1062 		}
1063 	}
1064 
1065 	m_freem(control);
1066 	return (error);
1067 }
1068 
1069 
1070 int
1071 freebsd32_sendmsg(struct thread *td,
1072 		  struct freebsd32_sendmsg_args *uap)
1073 {
1074 	struct msghdr msg;
1075 	struct msghdr32 m32;
1076 	struct iovec *iov;
1077 	struct mbuf *control = NULL;
1078 	struct sockaddr *to = NULL;
1079 	int error;
1080 
1081 	error = copyin(uap->msg, &m32, sizeof(m32));
1082 	if (error)
1083 		return (error);
1084 	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1085 	if (error)
1086 		return (error);
1087 	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1088 	    EMSGSIZE);
1089 	if (error)
1090 		return (error);
1091 	msg.msg_iov = iov;
1092 	if (msg.msg_name != NULL) {
1093 		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1094 		if (error) {
1095 			to = NULL;
1096 			goto out;
1097 		}
1098 		msg.msg_name = to;
1099 	}
1100 
1101 	if (msg.msg_control) {
1102 		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1103 			error = EINVAL;
1104 			goto out;
1105 		}
1106 
1107 		error = sockargs(&control, msg.msg_control,
1108 		    msg.msg_controllen, MT_CONTROL);
1109 		if (error)
1110 			goto out;
1111 
1112 		error = freebsd32_convert_msg_in(&control);
1113 		if (error)
1114 			goto out;
1115 	}
1116 
1117 	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1118 	    UIO_USERSPACE);
1119 
1120 out:
1121 	free(iov, M_IOV);
1122 	if (to)
1123 		free(to, M_SONAME);
1124 	return (error);
1125 }
1126 
1127 int
1128 freebsd32_recvfrom(struct thread *td,
1129 		   struct freebsd32_recvfrom_args *uap)
1130 {
1131 	struct msghdr msg;
1132 	struct iovec aiov;
1133 	int error;
1134 
1135 	if (uap->fromlenaddr) {
1136 		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1137 		    sizeof(msg.msg_namelen));
1138 		if (error)
1139 			return (error);
1140 	} else {
1141 		msg.msg_namelen = 0;
1142 	}
1143 
1144 	msg.msg_name = PTRIN(uap->from);
1145 	msg.msg_iov = &aiov;
1146 	msg.msg_iovlen = 1;
1147 	aiov.iov_base = PTRIN(uap->buf);
1148 	aiov.iov_len = uap->len;
1149 	msg.msg_control = NULL;
1150 	msg.msg_flags = uap->flags;
1151 	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1152 	if (error == 0 && uap->fromlenaddr)
1153 		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1154 		    sizeof (msg.msg_namelen));
1155 	return (error);
1156 }
1157 
1158 int
1159 freebsd32_settimeofday(struct thread *td,
1160 		       struct freebsd32_settimeofday_args *uap)
1161 {
1162 	struct timeval32 tv32;
1163 	struct timeval tv, *tvp;
1164 	struct timezone tz, *tzp;
1165 	int error;
1166 
1167 	if (uap->tv) {
1168 		error = copyin(uap->tv, &tv32, sizeof(tv32));
1169 		if (error)
1170 			return (error);
1171 		CP(tv32, tv, tv_sec);
1172 		CP(tv32, tv, tv_usec);
1173 		tvp = &tv;
1174 	} else
1175 		tvp = NULL;
1176 	if (uap->tzp) {
1177 		error = copyin(uap->tzp, &tz, sizeof(tz));
1178 		if (error)
1179 			return (error);
1180 		tzp = &tz;
1181 	} else
1182 		tzp = NULL;
1183 	return (kern_settimeofday(td, tvp, tzp));
1184 }
1185 
1186 int
1187 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1188 {
1189 	struct timeval32 s32[2];
1190 	struct timeval s[2], *sp;
1191 	int error;
1192 
1193 	if (uap->tptr != NULL) {
1194 		error = copyin(uap->tptr, s32, sizeof(s32));
1195 		if (error)
1196 			return (error);
1197 		CP(s32[0], s[0], tv_sec);
1198 		CP(s32[0], s[0], tv_usec);
1199 		CP(s32[1], s[1], tv_sec);
1200 		CP(s32[1], s[1], tv_usec);
1201 		sp = s;
1202 	} else
1203 		sp = NULL;
1204 	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1205 }
1206 
1207 int
1208 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1209 {
1210 	struct timeval32 s32[2];
1211 	struct timeval s[2], *sp;
1212 	int error;
1213 
1214 	if (uap->tptr != NULL) {
1215 		error = copyin(uap->tptr, s32, sizeof(s32));
1216 		if (error)
1217 			return (error);
1218 		CP(s32[0], s[0], tv_sec);
1219 		CP(s32[0], s[0], tv_usec);
1220 		CP(s32[1], s[1], tv_sec);
1221 		CP(s32[1], s[1], tv_usec);
1222 		sp = s;
1223 	} else
1224 		sp = NULL;
1225 	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1226 }
1227 
1228 int
1229 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1230 {
1231 	struct timeval32 s32[2];
1232 	struct timeval s[2], *sp;
1233 	int error;
1234 
1235 	if (uap->tptr != NULL) {
1236 		error = copyin(uap->tptr, s32, sizeof(s32));
1237 		if (error)
1238 			return (error);
1239 		CP(s32[0], s[0], tv_sec);
1240 		CP(s32[0], s[0], tv_usec);
1241 		CP(s32[1], s[1], tv_sec);
1242 		CP(s32[1], s[1], tv_usec);
1243 		sp = s;
1244 	} else
1245 		sp = NULL;
1246 	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1247 }
1248 
1249 int
1250 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1251 {
1252 	struct timeval32 s32[2];
1253 	struct timeval s[2], *sp;
1254 	int error;
1255 
1256 	if (uap->times != NULL) {
1257 		error = copyin(uap->times, s32, sizeof(s32));
1258 		if (error)
1259 			return (error);
1260 		CP(s32[0], s[0], tv_sec);
1261 		CP(s32[0], s[0], tv_usec);
1262 		CP(s32[1], s[1], tv_sec);
1263 		CP(s32[1], s[1], tv_usec);
1264 		sp = s;
1265 	} else
1266 		sp = NULL;
1267 	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1268 		sp, UIO_SYSSPACE));
1269 }
1270 
1271 int
1272 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1273 {
1274 	struct timeval32 tv32;
1275 	struct timeval delta, olddelta, *deltap;
1276 	int error;
1277 
1278 	if (uap->delta) {
1279 		error = copyin(uap->delta, &tv32, sizeof(tv32));
1280 		if (error)
1281 			return (error);
1282 		CP(tv32, delta, tv_sec);
1283 		CP(tv32, delta, tv_usec);
1284 		deltap = &delta;
1285 	} else
1286 		deltap = NULL;
1287 	error = kern_adjtime(td, deltap, &olddelta);
1288 	if (uap->olddelta && error == 0) {
1289 		CP(olddelta, tv32, tv_sec);
1290 		CP(olddelta, tv32, tv_usec);
1291 		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1292 	}
1293 	return (error);
1294 }
1295 
1296 #ifdef COMPAT_FREEBSD4
1297 int
1298 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1299 {
1300 	struct statfs32 s32;
1301 	struct statfs s;
1302 	int error;
1303 
1304 	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1305 	if (error)
1306 		return (error);
1307 	copy_statfs(&s, &s32);
1308 	return (copyout(&s32, uap->buf, sizeof(s32)));
1309 }
1310 #endif
1311 
1312 #ifdef COMPAT_FREEBSD4
1313 int
1314 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1315 {
1316 	struct statfs32 s32;
1317 	struct statfs s;
1318 	int error;
1319 
1320 	error = kern_fstatfs(td, uap->fd, &s);
1321 	if (error)
1322 		return (error);
1323 	copy_statfs(&s, &s32);
1324 	return (copyout(&s32, uap->buf, sizeof(s32)));
1325 }
1326 #endif
1327 
1328 #ifdef COMPAT_FREEBSD4
1329 int
1330 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1331 {
1332 	struct statfs32 s32;
1333 	struct statfs s;
1334 	fhandle_t fh;
1335 	int error;
1336 
1337 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1338 		return (error);
1339 	error = kern_fhstatfs(td, fh, &s);
1340 	if (error)
1341 		return (error);
1342 	copy_statfs(&s, &s32);
1343 	return (copyout(&s32, uap->buf, sizeof(s32)));
1344 }
1345 #endif
1346 
1347 static void
1348 freebsd32_ipcperm_in(struct ipc_perm32 *ip32, struct ipc_perm *ip)
1349 {
1350 
1351 	CP(*ip32, *ip, cuid);
1352 	CP(*ip32, *ip, cgid);
1353 	CP(*ip32, *ip, uid);
1354 	CP(*ip32, *ip, gid);
1355 	CP(*ip32, *ip, mode);
1356 	CP(*ip32, *ip, seq);
1357 	CP(*ip32, *ip, key);
1358 }
1359 
1360 static void
1361 freebsd32_ipcperm_out(struct ipc_perm *ip, struct ipc_perm32 *ip32)
1362 {
1363 
1364 	CP(*ip, *ip32, cuid);
1365 	CP(*ip, *ip32, cgid);
1366 	CP(*ip, *ip32, uid);
1367 	CP(*ip, *ip32, gid);
1368 	CP(*ip, *ip32, mode);
1369 	CP(*ip, *ip32, seq);
1370 	CP(*ip, *ip32, key);
1371 }
1372 
1373 int
1374 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1375 {
1376 
1377 	switch (uap->which) {
1378 	case 0:
1379 		return (freebsd32_semctl(td,
1380 		    (struct freebsd32_semctl_args *)&uap->a2));
1381 	default:
1382 		return (semsys(td, (struct semsys_args *)uap));
1383 	}
1384 }
1385 
1386 int
1387 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
1388 {
1389 	struct semid_ds32 dsbuf32;
1390 	struct semid_ds dsbuf;
1391 	union semun semun;
1392 	union semun32 arg;
1393 	register_t rval;
1394 	int error;
1395 
1396 	switch (uap->cmd) {
1397 	case SEM_STAT:
1398 	case IPC_SET:
1399 	case IPC_STAT:
1400 	case GETALL:
1401 	case SETVAL:
1402 	case SETALL:
1403 		error = copyin(uap->arg, &arg, sizeof(arg));
1404 		if (error)
1405 			return (error);
1406 		break;
1407 	}
1408 
1409 	switch (uap->cmd) {
1410 	case SEM_STAT:
1411 	case IPC_STAT:
1412 		semun.buf = &dsbuf;
1413 		break;
1414 	case IPC_SET:
1415 		error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
1416 		if (error)
1417 			return (error);
1418 		freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
1419 		PTRIN_CP(dsbuf32, dsbuf, sem_base);
1420 		CP(dsbuf32, dsbuf, sem_nsems);
1421 		CP(dsbuf32, dsbuf, sem_otime);
1422 		CP(dsbuf32, dsbuf, sem_pad1);
1423 		CP(dsbuf32, dsbuf, sem_ctime);
1424 		CP(dsbuf32, dsbuf, sem_pad2);
1425 		CP(dsbuf32, dsbuf, sem_pad3[0]);
1426 		CP(dsbuf32, dsbuf, sem_pad3[1]);
1427 		CP(dsbuf32, dsbuf, sem_pad3[2]);
1428 		CP(dsbuf32, dsbuf, sem_pad3[3]);
1429 		semun.buf = &dsbuf;
1430 		break;
1431 	case GETALL:
1432 	case SETALL:
1433 		semun.array = PTRIN(arg.array);
1434 		break;
1435 	case SETVAL:
1436 		semun.val = arg.val;
1437 		break;
1438 	}
1439 
1440 	error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
1441 	    &rval);
1442 	if (error)
1443 		return (error);
1444 
1445 	switch (uap->cmd) {
1446 	case SEM_STAT:
1447 	case IPC_STAT:
1448 		freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
1449 		PTROUT_CP(dsbuf, dsbuf32, sem_base);
1450 		CP(dsbuf, dsbuf32, sem_nsems);
1451 		CP(dsbuf, dsbuf32, sem_otime);
1452 		CP(dsbuf, dsbuf32, sem_pad1);
1453 		CP(dsbuf, dsbuf32, sem_ctime);
1454 		CP(dsbuf, dsbuf32, sem_pad2);
1455 		CP(dsbuf, dsbuf32, sem_pad3[0]);
1456 		CP(dsbuf, dsbuf32, sem_pad3[1]);
1457 		CP(dsbuf, dsbuf32, sem_pad3[2]);
1458 		CP(dsbuf, dsbuf32, sem_pad3[3]);
1459 		error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
1460 		break;
1461 	}
1462 
1463 	if (error == 0)
1464 		td->td_retval[0] = rval;
1465 	return (error);
1466 }
1467 
1468 int
1469 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1470 {
1471 
1472 	switch (uap->which) {
1473 	case 0:
1474 		return (freebsd32_msgctl(td,
1475 		    (struct freebsd32_msgctl_args *)&uap->a2));
1476 	case 2:
1477 		return (freebsd32_msgsnd(td,
1478 		    (struct freebsd32_msgsnd_args *)&uap->a2));
1479 	case 3:
1480 		return (freebsd32_msgrcv(td,
1481 		    (struct freebsd32_msgrcv_args *)&uap->a2));
1482 	default:
1483 		return (msgsys(td, (struct msgsys_args *)uap));
1484 	}
1485 }
1486 
1487 int
1488 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
1489 {
1490 	struct msqid_ds msqbuf;
1491 	struct msqid_ds32 msqbuf32;
1492 	int error;
1493 
1494 	if (uap->cmd == IPC_SET) {
1495 		error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
1496 		if (error)
1497 			return (error);
1498 		freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
1499 		PTRIN_CP(msqbuf32, msqbuf, msg_first);
1500 		PTRIN_CP(msqbuf32, msqbuf, msg_last);
1501 		CP(msqbuf32, msqbuf, msg_cbytes);
1502 		CP(msqbuf32, msqbuf, msg_qnum);
1503 		CP(msqbuf32, msqbuf, msg_qbytes);
1504 		CP(msqbuf32, msqbuf, msg_lspid);
1505 		CP(msqbuf32, msqbuf, msg_lrpid);
1506 		CP(msqbuf32, msqbuf, msg_stime);
1507 		CP(msqbuf32, msqbuf, msg_pad1);
1508 		CP(msqbuf32, msqbuf, msg_rtime);
1509 		CP(msqbuf32, msqbuf, msg_pad2);
1510 		CP(msqbuf32, msqbuf, msg_ctime);
1511 		CP(msqbuf32, msqbuf, msg_pad3);
1512 		CP(msqbuf32, msqbuf, msg_pad4[0]);
1513 		CP(msqbuf32, msqbuf, msg_pad4[1]);
1514 		CP(msqbuf32, msqbuf, msg_pad4[2]);
1515 		CP(msqbuf32, msqbuf, msg_pad4[3]);
1516 	}
1517 	error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
1518 	if (error)
1519 		return (error);
1520 	if (uap->cmd == IPC_STAT) {
1521 		freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
1522 		PTROUT_CP(msqbuf, msqbuf32, msg_first);
1523 		PTROUT_CP(msqbuf, msqbuf32, msg_last);
1524 		CP(msqbuf, msqbuf32, msg_cbytes);
1525 		CP(msqbuf, msqbuf32, msg_qnum);
1526 		CP(msqbuf, msqbuf32, msg_qbytes);
1527 		CP(msqbuf, msqbuf32, msg_lspid);
1528 		CP(msqbuf, msqbuf32, msg_lrpid);
1529 		CP(msqbuf, msqbuf32, msg_stime);
1530 		CP(msqbuf, msqbuf32, msg_pad1);
1531 		CP(msqbuf, msqbuf32, msg_rtime);
1532 		CP(msqbuf, msqbuf32, msg_pad2);
1533 		CP(msqbuf, msqbuf32, msg_ctime);
1534 		CP(msqbuf, msqbuf32, msg_pad3);
1535 		CP(msqbuf, msqbuf32, msg_pad4[0]);
1536 		CP(msqbuf, msqbuf32, msg_pad4[1]);
1537 		CP(msqbuf, msqbuf32, msg_pad4[2]);
1538 		CP(msqbuf, msqbuf32, msg_pad4[3]);
1539 		error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
1540 	}
1541 	return (error);
1542 }
1543 
1544 int
1545 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
1546 {
1547 	const void *msgp;
1548 	long mtype;
1549 	int32_t mtype32;
1550 	int error;
1551 
1552 	msgp = PTRIN(uap->msgp);
1553 	if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
1554 		return (error);
1555 	mtype = mtype32;
1556 	return (kern_msgsnd(td, uap->msqid,
1557 	    (const char *)msgp + sizeof(mtype32),
1558 	    uap->msgsz, uap->msgflg, mtype));
1559 }
1560 
1561 int
1562 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
1563 {
1564 	void *msgp;
1565 	long mtype;
1566 	int32_t mtype32;
1567 	int error;
1568 
1569 	msgp = PTRIN(uap->msgp);
1570 	if ((error = kern_msgrcv(td, uap->msqid,
1571 	    (char *)msgp + sizeof(mtype32), uap->msgsz,
1572 	    uap->msgtyp, uap->msgflg, &mtype)) != 0)
1573 		return (error);
1574 	mtype32 = (int32_t)mtype;
1575 	return (copyout(&mtype32, msgp, sizeof(mtype32)));
1576 }
1577 
1578 int
1579 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1580 {
1581 
1582 	switch (uap->which) {
1583 	case 0:	{	/* shmat */
1584 		struct shmat_args ap;
1585 
1586 		ap.shmid = uap->a2;
1587 		ap.shmaddr = PTRIN(uap->a3);
1588 		ap.shmflg = uap->a4;
1589 		return (sysent[SYS_shmat].sy_call(td, &ap));
1590 	}
1591 	case 2: {	/* shmdt */
1592 		struct shmdt_args ap;
1593 
1594 		ap.shmaddr = PTRIN(uap->a2);
1595 		return (sysent[SYS_shmdt].sy_call(td, &ap));
1596 	}
1597 	case 3: {	/* shmget */
1598 		struct shmget_args ap;
1599 
1600 		ap.key = uap->a2;
1601 		ap.size = uap->a3;
1602 		ap.shmflg = uap->a4;
1603 		return (sysent[SYS_shmget].sy_call(td, &ap));
1604 	}
1605 	case 4: {	/* shmctl */
1606 		struct freebsd32_shmctl_args ap;
1607 
1608 		ap.shmid = uap->a2;
1609 		ap.cmd = uap->a3;
1610 		ap.buf = PTRIN(uap->a4);
1611 		return (freebsd32_shmctl(td, &ap));
1612 	}
1613 	case 1:		/* oshmctl */
1614 	default:
1615 		return (EINVAL);
1616 	}
1617 }
1618 
1619 int
1620 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1621 {
1622 	int error = 0;
1623 	union {
1624 		struct shmid_ds shmid_ds;
1625 		struct shm_info shm_info;
1626 		struct shminfo shminfo;
1627 	} u;
1628 	union {
1629 		struct shmid_ds32 shmid_ds32;
1630 		struct shm_info32 shm_info32;
1631 		struct shminfo32 shminfo32;
1632 	} u32;
1633 	size_t sz;
1634 
1635 	if (uap->cmd == IPC_SET) {
1636 		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1637 		    sizeof(u32.shmid_ds32))))
1638 			goto done;
1639 		freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
1640 		    &u.shmid_ds.shm_perm);
1641 		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1642 		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1643 		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1644 		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1645 		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1646 		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1647 		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1648 		PTRIN_CP(u32.shmid_ds32, u.shmid_ds, shm_internal);
1649 	}
1650 
1651 	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1652 	if (error)
1653 		goto done;
1654 
1655 	/* Cases in which we need to copyout */
1656 	switch (uap->cmd) {
1657 	case IPC_INFO:
1658 		CP(u.shminfo, u32.shminfo32, shmmax);
1659 		CP(u.shminfo, u32.shminfo32, shmmin);
1660 		CP(u.shminfo, u32.shminfo32, shmmni);
1661 		CP(u.shminfo, u32.shminfo32, shmseg);
1662 		CP(u.shminfo, u32.shminfo32, shmall);
1663 		error = copyout(&u32.shminfo32, uap->buf,
1664 		    sizeof(u32.shminfo32));
1665 		break;
1666 	case SHM_INFO:
1667 		CP(u.shm_info, u32.shm_info32, used_ids);
1668 		CP(u.shm_info, u32.shm_info32, shm_rss);
1669 		CP(u.shm_info, u32.shm_info32, shm_tot);
1670 		CP(u.shm_info, u32.shm_info32, shm_swp);
1671 		CP(u.shm_info, u32.shm_info32, swap_attempts);
1672 		CP(u.shm_info, u32.shm_info32, swap_successes);
1673 		error = copyout(&u32.shm_info32, uap->buf,
1674 		    sizeof(u32.shm_info32));
1675 		break;
1676 	case SHM_STAT:
1677 	case IPC_STAT:
1678 		freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
1679 		    &u32.shmid_ds32.shm_perm);
1680 		CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1681 		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1682 		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1683 		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1684 		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1685 		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1686 		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1687 		PTROUT_CP(u.shmid_ds, u32.shmid_ds32, shm_internal);
1688 		error = copyout(&u32.shmid_ds32, uap->buf,
1689 		    sizeof(u32.shmid_ds32));
1690 		break;
1691 	}
1692 
1693 done:
1694 	if (error) {
1695 		/* Invalidate the return value */
1696 		td->td_retval[0] = -1;
1697 	}
1698 	return (error);
1699 }
1700 
1701 int
1702 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1703 {
1704 	struct pread_args ap;
1705 
1706 	ap.fd = uap->fd;
1707 	ap.buf = uap->buf;
1708 	ap.nbyte = uap->nbyte;
1709 	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1710 	return (pread(td, &ap));
1711 }
1712 
1713 int
1714 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1715 {
1716 	struct pwrite_args ap;
1717 
1718 	ap.fd = uap->fd;
1719 	ap.buf = uap->buf;
1720 	ap.nbyte = uap->nbyte;
1721 	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1722 	return (pwrite(td, &ap));
1723 }
1724 
1725 int
1726 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1727 {
1728 	int error;
1729 	struct lseek_args ap;
1730 	off_t pos;
1731 
1732 	ap.fd = uap->fd;
1733 	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1734 	ap.whence = uap->whence;
1735 	error = lseek(td, &ap);
1736 	/* Expand the quad return into two parts for eax and edx */
1737 	pos = *(off_t *)(td->td_retval);
1738 	td->td_retval[0] = pos & 0xffffffff;	/* %eax */
1739 	td->td_retval[1] = pos >> 32;		/* %edx */
1740 	return error;
1741 }
1742 
1743 int
1744 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1745 {
1746 	struct truncate_args ap;
1747 
1748 	ap.path = uap->path;
1749 	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1750 	return (truncate(td, &ap));
1751 }
1752 
1753 int
1754 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1755 {
1756 	struct ftruncate_args ap;
1757 
1758 	ap.fd = uap->fd;
1759 	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1760 	return (ftruncate(td, &ap));
1761 }
1762 
1763 #ifdef COMPAT_FREEBSD6
1764 /* versions with the 'int pad' argument */
1765 int
1766 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1767 {
1768 	struct pread_args ap;
1769 
1770 	ap.fd = uap->fd;
1771 	ap.buf = uap->buf;
1772 	ap.nbyte = uap->nbyte;
1773 	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1774 	return (pread(td, &ap));
1775 }
1776 
1777 int
1778 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1779 {
1780 	struct pwrite_args ap;
1781 
1782 	ap.fd = uap->fd;
1783 	ap.buf = uap->buf;
1784 	ap.nbyte = uap->nbyte;
1785 	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1786 	return (pwrite(td, &ap));
1787 }
1788 
1789 int
1790 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1791 {
1792 	int error;
1793 	struct lseek_args ap;
1794 	off_t pos;
1795 
1796 	ap.fd = uap->fd;
1797 	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1798 	ap.whence = uap->whence;
1799 	error = lseek(td, &ap);
1800 	/* Expand the quad return into two parts for eax and edx */
1801 	pos = *(off_t *)(td->td_retval);
1802 	td->td_retval[0] = pos & 0xffffffff;	/* %eax */
1803 	td->td_retval[1] = pos >> 32;		/* %edx */
1804 	return error;
1805 }
1806 
1807 int
1808 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1809 {
1810 	struct truncate_args ap;
1811 
1812 	ap.path = uap->path;
1813 	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1814 	return (truncate(td, &ap));
1815 }
1816 
1817 int
1818 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1819 {
1820 	struct ftruncate_args ap;
1821 
1822 	ap.fd = uap->fd;
1823 	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1824 	return (ftruncate(td, &ap));
1825 }
1826 #endif /* COMPAT_FREEBSD6 */
1827 
1828 struct sf_hdtr32 {
1829 	uint32_t headers;
1830 	int hdr_cnt;
1831 	uint32_t trailers;
1832 	int trl_cnt;
1833 };
1834 
1835 static int
1836 freebsd32_do_sendfile(struct thread *td,
1837     struct freebsd32_sendfile_args *uap, int compat)
1838 {
1839 	struct sendfile_args ap;
1840 	struct sf_hdtr32 hdtr32;
1841 	struct sf_hdtr hdtr;
1842 	struct uio *hdr_uio, *trl_uio;
1843 	struct iovec32 *iov32;
1844 	int error;
1845 
1846 	hdr_uio = trl_uio = NULL;
1847 
1848 	ap.fd = uap->fd;
1849 	ap.s = uap->s;
1850 	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1851 	ap.nbytes = uap->nbytes;
1852 	ap.hdtr = (struct sf_hdtr *)uap->hdtr;		/* XXX not used */
1853 	ap.sbytes = uap->sbytes;
1854 	ap.flags = uap->flags;
1855 
1856 	if (uap->hdtr != NULL) {
1857 		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1858 		if (error)
1859 			goto out;
1860 		PTRIN_CP(hdtr32, hdtr, headers);
1861 		CP(hdtr32, hdtr, hdr_cnt);
1862 		PTRIN_CP(hdtr32, hdtr, trailers);
1863 		CP(hdtr32, hdtr, trl_cnt);
1864 
1865 		if (hdtr.headers != NULL) {
1866 			iov32 = PTRIN(hdtr32.headers);
1867 			error = freebsd32_copyinuio(iov32,
1868 			    hdtr32.hdr_cnt, &hdr_uio);
1869 			if (error)
1870 				goto out;
1871 		}
1872 		if (hdtr.trailers != NULL) {
1873 			iov32 = PTRIN(hdtr32.trailers);
1874 			error = freebsd32_copyinuio(iov32,
1875 			    hdtr32.trl_cnt, &trl_uio);
1876 			if (error)
1877 				goto out;
1878 		}
1879 	}
1880 
1881 	error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1882 out:
1883 	if (hdr_uio)
1884 		free(hdr_uio, M_IOV);
1885 	if (trl_uio)
1886 		free(trl_uio, M_IOV);
1887 	return (error);
1888 }
1889 
1890 #ifdef COMPAT_FREEBSD4
1891 int
1892 freebsd4_freebsd32_sendfile(struct thread *td,
1893     struct freebsd4_freebsd32_sendfile_args *uap)
1894 {
1895 	return (freebsd32_do_sendfile(td,
1896 	    (struct freebsd32_sendfile_args *)uap, 1));
1897 }
1898 #endif
1899 
1900 int
1901 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1902 {
1903 
1904 	return (freebsd32_do_sendfile(td, uap, 0));
1905 }
1906 
1907 static void
1908 copy_stat( struct stat *in, struct stat32 *out)
1909 {
1910 	CP(*in, *out, st_dev);
1911 	CP(*in, *out, st_ino);
1912 	CP(*in, *out, st_mode);
1913 	CP(*in, *out, st_nlink);
1914 	CP(*in, *out, st_uid);
1915 	CP(*in, *out, st_gid);
1916 	CP(*in, *out, st_rdev);
1917 	TS_CP(*in, *out, st_atimespec);
1918 	TS_CP(*in, *out, st_mtimespec);
1919 	TS_CP(*in, *out, st_ctimespec);
1920 	CP(*in, *out, st_size);
1921 	CP(*in, *out, st_blocks);
1922 	CP(*in, *out, st_blksize);
1923 	CP(*in, *out, st_flags);
1924 	CP(*in, *out, st_gen);
1925 }
1926 
1927 int
1928 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1929 {
1930 	struct stat sb;
1931 	struct stat32 sb32;
1932 	int error;
1933 
1934 	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1935 	if (error)
1936 		return (error);
1937 	copy_stat(&sb, &sb32);
1938 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1939 	return (error);
1940 }
1941 
1942 int
1943 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1944 {
1945 	struct stat ub;
1946 	struct stat32 ub32;
1947 	int error;
1948 
1949 	error = kern_fstat(td, uap->fd, &ub);
1950 	if (error)
1951 		return (error);
1952 	copy_stat(&ub, &ub32);
1953 	error = copyout(&ub32, uap->ub, sizeof(ub32));
1954 	return (error);
1955 }
1956 
1957 int
1958 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1959 {
1960 	struct stat ub;
1961 	struct stat32 ub32;
1962 	int error;
1963 
1964 	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1965 	if (error)
1966 		return (error);
1967 	copy_stat(&ub, &ub32);
1968 	error = copyout(&ub32, uap->buf, sizeof(ub32));
1969 	return (error);
1970 }
1971 
1972 int
1973 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1974 {
1975 	struct stat sb;
1976 	struct stat32 sb32;
1977 	int error;
1978 
1979 	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1980 	if (error)
1981 		return (error);
1982 	copy_stat(&sb, &sb32);
1983 	error = copyout(&sb32, uap->ub, sizeof (sb32));
1984 	return (error);
1985 }
1986 
1987 /*
1988  * MPSAFE
1989  */
1990 int
1991 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1992 {
1993 	int error, name[CTL_MAXNAME];
1994 	size_t j, oldlen;
1995 
1996 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1997 		return (EINVAL);
1998  	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1999  	if (error)
2000 		return (error);
2001 	mtx_lock(&Giant);
2002 	if (uap->oldlenp)
2003 		oldlen = fuword32(uap->oldlenp);
2004 	else
2005 		oldlen = 0;
2006 	error = userland_sysctl(td, name, uap->namelen,
2007 		uap->old, &oldlen, 1,
2008 		uap->new, uap->newlen, &j, SCTL_MASK32);
2009 	if (error && error != ENOMEM)
2010 		goto done2;
2011 	if (uap->oldlenp)
2012 		suword32(uap->oldlenp, j);
2013 done2:
2014 	mtx_unlock(&Giant);
2015 	return (error);
2016 }
2017 
2018 int
2019 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2020 {
2021 	struct sigaction32 s32;
2022 	struct sigaction sa, osa, *sap;
2023 	int error;
2024 
2025 	if (uap->act) {
2026 		error = copyin(uap->act, &s32, sizeof(s32));
2027 		if (error)
2028 			return (error);
2029 		sa.sa_handler = PTRIN(s32.sa_u);
2030 		CP(s32, sa, sa_flags);
2031 		CP(s32, sa, sa_mask);
2032 		sap = &sa;
2033 	} else
2034 		sap = NULL;
2035 	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2036 	if (error == 0 && uap->oact != NULL) {
2037 		s32.sa_u = PTROUT(osa.sa_handler);
2038 		CP(osa, s32, sa_flags);
2039 		CP(osa, s32, sa_mask);
2040 		error = copyout(&s32, uap->oact, sizeof(s32));
2041 	}
2042 	return (error);
2043 }
2044 
2045 #ifdef COMPAT_FREEBSD4
2046 int
2047 freebsd4_freebsd32_sigaction(struct thread *td,
2048 			     struct freebsd4_freebsd32_sigaction_args *uap)
2049 {
2050 	struct sigaction32 s32;
2051 	struct sigaction sa, osa, *sap;
2052 	int error;
2053 
2054 	if (uap->act) {
2055 		error = copyin(uap->act, &s32, sizeof(s32));
2056 		if (error)
2057 			return (error);
2058 		sa.sa_handler = PTRIN(s32.sa_u);
2059 		CP(s32, sa, sa_flags);
2060 		CP(s32, sa, sa_mask);
2061 		sap = &sa;
2062 	} else
2063 		sap = NULL;
2064 	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2065 	if (error == 0 && uap->oact != NULL) {
2066 		s32.sa_u = PTROUT(osa.sa_handler);
2067 		CP(osa, s32, sa_flags);
2068 		CP(osa, s32, sa_mask);
2069 		error = copyout(&s32, uap->oact, sizeof(s32));
2070 	}
2071 	return (error);
2072 }
2073 #endif
2074 
2075 #ifdef COMPAT_43
2076 struct osigaction32 {
2077 	u_int32_t	sa_u;
2078 	osigset_t	sa_mask;
2079 	int		sa_flags;
2080 };
2081 
2082 #define	ONSIG	32
2083 
2084 int
2085 ofreebsd32_sigaction(struct thread *td,
2086 			     struct ofreebsd32_sigaction_args *uap)
2087 {
2088 	struct osigaction32 s32;
2089 	struct sigaction sa, osa, *sap;
2090 	int error;
2091 
2092 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2093 		return (EINVAL);
2094 
2095 	if (uap->nsa) {
2096 		error = copyin(uap->nsa, &s32, sizeof(s32));
2097 		if (error)
2098 			return (error);
2099 		sa.sa_handler = PTRIN(s32.sa_u);
2100 		CP(s32, sa, sa_flags);
2101 		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2102 		sap = &sa;
2103 	} else
2104 		sap = NULL;
2105 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2106 	if (error == 0 && uap->osa != NULL) {
2107 		s32.sa_u = PTROUT(osa.sa_handler);
2108 		CP(osa, s32, sa_flags);
2109 		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2110 		error = copyout(&s32, uap->osa, sizeof(s32));
2111 	}
2112 	return (error);
2113 }
2114 
2115 int
2116 ofreebsd32_sigprocmask(struct thread *td,
2117 			       struct ofreebsd32_sigprocmask_args *uap)
2118 {
2119 	sigset_t set, oset;
2120 	int error;
2121 
2122 	OSIG2SIG(uap->mask, set);
2123 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
2124 	SIG2OSIG(oset, td->td_retval[0]);
2125 	return (error);
2126 }
2127 
2128 int
2129 ofreebsd32_sigpending(struct thread *td,
2130 			      struct ofreebsd32_sigpending_args *uap)
2131 {
2132 	struct proc *p = td->td_proc;
2133 	sigset_t siglist;
2134 
2135 	PROC_LOCK(p);
2136 	siglist = p->p_siglist;
2137 	SIGSETOR(siglist, td->td_siglist);
2138 	PROC_UNLOCK(p);
2139 	SIG2OSIG(siglist, td->td_retval[0]);
2140 	return (0);
2141 }
2142 
2143 struct sigvec32 {
2144 	u_int32_t	sv_handler;
2145 	int		sv_mask;
2146 	int		sv_flags;
2147 };
2148 
2149 int
2150 ofreebsd32_sigvec(struct thread *td,
2151 			  struct ofreebsd32_sigvec_args *uap)
2152 {
2153 	struct sigvec32 vec;
2154 	struct sigaction sa, osa, *sap;
2155 	int error;
2156 
2157 	if (uap->signum <= 0 || uap->signum >= ONSIG)
2158 		return (EINVAL);
2159 
2160 	if (uap->nsv) {
2161 		error = copyin(uap->nsv, &vec, sizeof(vec));
2162 		if (error)
2163 			return (error);
2164 		sa.sa_handler = PTRIN(vec.sv_handler);
2165 		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2166 		sa.sa_flags = vec.sv_flags;
2167 		sa.sa_flags ^= SA_RESTART;
2168 		sap = &sa;
2169 	} else
2170 		sap = NULL;
2171 	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2172 	if (error == 0 && uap->osv != NULL) {
2173 		vec.sv_handler = PTROUT(osa.sa_handler);
2174 		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2175 		vec.sv_flags = osa.sa_flags;
2176 		vec.sv_flags &= ~SA_NOCLDWAIT;
2177 		vec.sv_flags ^= SA_RESTART;
2178 		error = copyout(&vec, uap->osv, sizeof(vec));
2179 	}
2180 	return (error);
2181 }
2182 
2183 int
2184 ofreebsd32_sigblock(struct thread *td,
2185 			    struct ofreebsd32_sigblock_args *uap)
2186 {
2187 	struct proc *p = td->td_proc;
2188 	sigset_t set;
2189 
2190 	OSIG2SIG(uap->mask, set);
2191 	SIG_CANTMASK(set);
2192 	PROC_LOCK(p);
2193 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
2194 	SIGSETOR(td->td_sigmask, set);
2195 	PROC_UNLOCK(p);
2196 	return (0);
2197 }
2198 
2199 int
2200 ofreebsd32_sigsetmask(struct thread *td,
2201 			      struct ofreebsd32_sigsetmask_args *uap)
2202 {
2203 	struct proc *p = td->td_proc;
2204 	sigset_t set;
2205 
2206 	OSIG2SIG(uap->mask, set);
2207 	SIG_CANTMASK(set);
2208 	PROC_LOCK(p);
2209 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
2210 	SIGSETLO(td->td_sigmask, set);
2211 	signotify(td);
2212 	PROC_UNLOCK(p);
2213 	return (0);
2214 }
2215 
2216 int
2217 ofreebsd32_sigsuspend(struct thread *td,
2218 			      struct ofreebsd32_sigsuspend_args *uap)
2219 {
2220 	struct proc *p = td->td_proc;
2221 	sigset_t mask;
2222 
2223 	PROC_LOCK(p);
2224 	td->td_oldsigmask = td->td_sigmask;
2225 	td->td_pflags |= TDP_OLDMASK;
2226 	OSIG2SIG(uap->mask, mask);
2227 	SIG_CANTMASK(mask);
2228 	SIGSETLO(td->td_sigmask, mask);
2229 	signotify(td);
2230 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
2231 		/* void */;
2232 	PROC_UNLOCK(p);
2233 	/* always return EINTR rather than ERESTART... */
2234 	return (EINTR);
2235 }
2236 
2237 struct sigstack32 {
2238 	u_int32_t	ss_sp;
2239 	int		ss_onstack;
2240 };
2241 
2242 int
2243 ofreebsd32_sigstack(struct thread *td,
2244 			    struct ofreebsd32_sigstack_args *uap)
2245 {
2246 	struct sigstack32 s32;
2247 	struct sigstack nss, oss;
2248 	int error = 0, unss;
2249 
2250 	if (uap->nss != NULL) {
2251 		error = copyin(uap->nss, &s32, sizeof(s32));
2252 		if (error)
2253 			return (error);
2254 		nss.ss_sp = PTRIN(s32.ss_sp);
2255 		CP(s32, nss, ss_onstack);
2256 		unss = 1;
2257 	} else {
2258 		unss = 0;
2259 	}
2260 	oss.ss_sp = td->td_sigstk.ss_sp;
2261 	oss.ss_onstack = sigonstack(cpu_getstack(td));
2262 	if (unss) {
2263 		td->td_sigstk.ss_sp = nss.ss_sp;
2264 		td->td_sigstk.ss_size = 0;
2265 		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2266 		td->td_pflags |= TDP_ALTSTACK;
2267 	}
2268 	if (uap->oss != NULL) {
2269 		s32.ss_sp = PTROUT(oss.ss_sp);
2270 		CP(oss, s32, ss_onstack);
2271 		error = copyout(&s32, uap->oss, sizeof(s32));
2272 	}
2273 	return (error);
2274 }
2275 #endif
2276 
2277 int
2278 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2279 {
2280 	struct timespec32 rmt32, rqt32;
2281 	struct timespec rmt, rqt;
2282 	int error;
2283 
2284 	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2285 	if (error)
2286 		return (error);
2287 
2288 	CP(rqt32, rqt, tv_sec);
2289 	CP(rqt32, rqt, tv_nsec);
2290 
2291 	if (uap->rmtp &&
2292 	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2293 		return (EFAULT);
2294 	error = kern_nanosleep(td, &rqt, &rmt);
2295 	if (error && uap->rmtp) {
2296 		int error2;
2297 
2298 		CP(rmt, rmt32, tv_sec);
2299 		CP(rmt, rmt32, tv_nsec);
2300 
2301 		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2302 		if (error2)
2303 			error = error2;
2304 	}
2305 	return (error);
2306 }
2307 
2308 int
2309 freebsd32_clock_gettime(struct thread *td,
2310 			struct freebsd32_clock_gettime_args *uap)
2311 {
2312 	struct timespec	ats;
2313 	struct timespec32 ats32;
2314 	int error;
2315 
2316 	error = kern_clock_gettime(td, uap->clock_id, &ats);
2317 	if (error == 0) {
2318 		CP(ats, ats32, tv_sec);
2319 		CP(ats, ats32, tv_nsec);
2320 		error = copyout(&ats32, uap->tp, sizeof(ats32));
2321 	}
2322 	return (error);
2323 }
2324 
2325 int
2326 freebsd32_clock_settime(struct thread *td,
2327 			struct freebsd32_clock_settime_args *uap)
2328 {
2329 	struct timespec	ats;
2330 	struct timespec32 ats32;
2331 	int error;
2332 
2333 	error = copyin(uap->tp, &ats32, sizeof(ats32));
2334 	if (error)
2335 		return (error);
2336 	CP(ats32, ats, tv_sec);
2337 	CP(ats32, ats, tv_nsec);
2338 
2339 	return (kern_clock_settime(td, uap->clock_id, &ats));
2340 }
2341 
2342 int
2343 freebsd32_clock_getres(struct thread *td,
2344 		       struct freebsd32_clock_getres_args *uap)
2345 {
2346 	struct timespec	ts;
2347 	struct timespec32 ts32;
2348 	int error;
2349 
2350 	if (uap->tp == NULL)
2351 		return (0);
2352 	error = kern_clock_getres(td, uap->clock_id, &ts);
2353 	if (error == 0) {
2354 		CP(ts, ts32, tv_sec);
2355 		CP(ts, ts32, tv_nsec);
2356 		error = copyout(&ts32, uap->tp, sizeof(ts32));
2357 	}
2358 	return (error);
2359 }
2360 
2361 int
2362 freebsd32_thr_new(struct thread *td,
2363 		  struct freebsd32_thr_new_args *uap)
2364 {
2365 	struct thr_param32 param32;
2366 	struct thr_param param;
2367 	int error;
2368 
2369 	if (uap->param_size < 0 ||
2370 	    uap->param_size > sizeof(struct thr_param32))
2371 		return (EINVAL);
2372 	bzero(&param, sizeof(struct thr_param));
2373 	bzero(&param32, sizeof(struct thr_param32));
2374 	error = copyin(uap->param, &param32, uap->param_size);
2375 	if (error != 0)
2376 		return (error);
2377 	param.start_func = PTRIN(param32.start_func);
2378 	param.arg = PTRIN(param32.arg);
2379 	param.stack_base = PTRIN(param32.stack_base);
2380 	param.stack_size = param32.stack_size;
2381 	param.tls_base = PTRIN(param32.tls_base);
2382 	param.tls_size = param32.tls_size;
2383 	param.child_tid = PTRIN(param32.child_tid);
2384 	param.parent_tid = PTRIN(param32.parent_tid);
2385 	param.flags = param32.flags;
2386 	param.rtp = PTRIN(param32.rtp);
2387 	param.spare[0] = PTRIN(param32.spare[0]);
2388 	param.spare[1] = PTRIN(param32.spare[1]);
2389 	param.spare[2] = PTRIN(param32.spare[2]);
2390 
2391 	return (kern_thr_new(td, &param));
2392 }
2393 
2394 int
2395 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2396 {
2397 	struct timespec32 ts32;
2398 	struct timespec ts, *tsp;
2399 	int error;
2400 
2401 	error = 0;
2402 	tsp = NULL;
2403 	if (uap->timeout != NULL) {
2404 		error = copyin((const void *)uap->timeout, (void *)&ts32,
2405 		    sizeof(struct timespec32));
2406 		if (error != 0)
2407 			return (error);
2408 		ts.tv_sec = ts32.tv_sec;
2409 		ts.tv_nsec = ts32.tv_nsec;
2410 		tsp = &ts;
2411 	}
2412 	return (kern_thr_suspend(td, tsp));
2413 }
2414 
2415 void
2416 siginfo_to_siginfo32(siginfo_t *src, struct siginfo32 *dst)
2417 {
2418 	bzero(dst, sizeof(*dst));
2419 	dst->si_signo = src->si_signo;
2420 	dst->si_errno = src->si_errno;
2421 	dst->si_code = src->si_code;
2422 	dst->si_pid = src->si_pid;
2423 	dst->si_uid = src->si_uid;
2424 	dst->si_status = src->si_status;
2425 	dst->si_addr = dst->si_addr;
2426 	dst->si_value.sigval_int = src->si_value.sival_int;
2427 	dst->si_timerid = src->si_timerid;
2428 	dst->si_overrun = src->si_overrun;
2429 }
2430 
2431 int
2432 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2433 {
2434 	struct timespec32 ts32;
2435 	struct timespec ts;
2436 	struct timespec *timeout;
2437 	sigset_t set;
2438 	ksiginfo_t ksi;
2439 	struct siginfo32 si32;
2440 	int error;
2441 
2442 	if (uap->timeout) {
2443 		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2444 		if (error)
2445 			return (error);
2446 		ts.tv_sec = ts32.tv_sec;
2447 		ts.tv_nsec = ts32.tv_nsec;
2448 		timeout = &ts;
2449 	} else
2450 		timeout = NULL;
2451 
2452 	error = copyin(uap->set, &set, sizeof(set));
2453 	if (error)
2454 		return (error);
2455 
2456 	error = kern_sigtimedwait(td, set, &ksi, timeout);
2457 	if (error)
2458 		return (error);
2459 
2460 	if (uap->info) {
2461 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2462 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2463 	}
2464 
2465 	if (error == 0)
2466 		td->td_retval[0] = ksi.ksi_signo;
2467 	return (error);
2468 }
2469 
2470 /*
2471  * MPSAFE
2472  */
2473 int
2474 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2475 {
2476 	ksiginfo_t ksi;
2477 	struct siginfo32 si32;
2478 	sigset_t set;
2479 	int error;
2480 
2481 	error = copyin(uap->set, &set, sizeof(set));
2482 	if (error)
2483 		return (error);
2484 
2485 	error = kern_sigtimedwait(td, set, &ksi, NULL);
2486 	if (error)
2487 		return (error);
2488 
2489 	if (uap->info) {
2490 		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2491 		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2492 	}
2493 	if (error == 0)
2494 		td->td_retval[0] = ksi.ksi_signo;
2495 	return (error);
2496 }
2497 
2498 #if 0
2499 
2500 int
2501 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2502 {
2503 	int error;
2504 	struct yyy32 *p32, s32;
2505 	struct yyy *p = NULL, s;
2506 
2507 	if (uap->zzz) {
2508 		error = copyin(uap->zzz, &s32, sizeof(s32));
2509 		if (error)
2510 			return (error);
2511 		/* translate in */
2512 		p = &s;
2513 	}
2514 	error = kern_xxx(td, p);
2515 	if (error)
2516 		return (error);
2517 	if (uap->zzz) {
2518 		/* translate out */
2519 		error = copyout(&s32, p32, sizeof(s32));
2520 	}
2521 	return (error);
2522 }
2523 
2524 #endif
2525