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