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