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