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