freebsd32_misc.c revision 156114
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 156114 2006-02-28 19:39:18Z ps $");
29
30#include "opt_compat.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/bus.h>
35#include <sys/exec.h>
36#include <sys/fcntl.h>
37#include <sys/filedesc.h>
38#include <sys/namei.h>
39#include <sys/imgact.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/malloc.h>
43#include <sys/file.h>		/* Must come after sys/malloc.h */
44#include <sys/mbuf.h>
45#include <sys/mman.h>
46#include <sys/module.h>
47#include <sys/mount.h>
48#include <sys/mutex.h>
49#include <sys/namei.h>
50#include <sys/param.h>
51#include <sys/proc.h>
52#include <sys/reboot.h>
53#include <sys/resource.h>
54#include <sys/resourcevar.h>
55#include <sys/selinfo.h>
56#include <sys/eventvar.h>	/* Must come after sys/selinfo.h */
57#include <sys/pipe.h>		/* Must come after sys/selinfo.h */
58#include <sys/signal.h>
59#include <sys/signalvar.h>
60#include <sys/socket.h>
61#include <sys/socketvar.h>
62#include <sys/stat.h>
63#include <sys/syscall.h>
64#include <sys/syscallsubr.h>
65#include <sys/sysctl.h>
66#include <sys/sysent.h>
67#include <sys/sysproto.h>
68#include <sys/systm.h>
69#include <sys/unistd.h>
70#include <sys/vnode.h>
71#include <sys/wait.h>
72
73#include <vm/vm.h>
74#include <vm/vm_kern.h>
75#include <vm/vm_param.h>
76#include <vm/pmap.h>
77#include <vm/vm_map.h>
78#include <vm/vm_object.h>
79#include <vm/vm_extern.h>
80
81#include <machine/cpu.h>
82
83#include <compat/freebsd32/freebsd32_util.h>
84#include <compat/freebsd32/freebsd32.h>
85#include <compat/freebsd32/freebsd32_proto.h>
86
87CTASSERT(sizeof(struct timeval32) == 8);
88CTASSERT(sizeof(struct timespec32) == 8);
89CTASSERT(sizeof(struct statfs32) == 256);
90CTASSERT(sizeof(struct rusage32) == 72);
91
92int
93freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
94{
95	int error, status;
96	struct rusage32 ru32;
97	struct rusage ru, *rup;
98
99	if (uap->rusage != NULL)
100		rup = &ru;
101	else
102		rup = NULL;
103	error = kern_wait(td, uap->pid, &status, uap->options, rup);
104	if (error)
105		return (error);
106	if (uap->status != NULL)
107		error = copyout(&status, uap->status, sizeof(status));
108	if (uap->rusage != NULL && error == 0) {
109		TV_CP(ru, ru32, ru_utime);
110		TV_CP(ru, ru32, ru_stime);
111		CP(ru, ru32, ru_maxrss);
112		CP(ru, ru32, ru_ixrss);
113		CP(ru, ru32, ru_idrss);
114		CP(ru, ru32, ru_isrss);
115		CP(ru, ru32, ru_minflt);
116		CP(ru, ru32, ru_majflt);
117		CP(ru, ru32, ru_nswap);
118		CP(ru, ru32, ru_inblock);
119		CP(ru, ru32, ru_oublock);
120		CP(ru, ru32, ru_msgsnd);
121		CP(ru, ru32, ru_msgrcv);
122		CP(ru, ru32, ru_nsignals);
123		CP(ru, ru32, ru_nvcsw);
124		CP(ru, ru32, ru_nivcsw);
125		error = copyout(&ru32, uap->rusage, sizeof(ru32));
126	}
127	return (error);
128}
129
130#ifdef COMPAT_FREEBSD4
131static void
132copy_statfs(struct statfs *in, struct statfs32 *out)
133{
134	CP(*in, *out, f_bsize);
135	CP(*in, *out, f_iosize);
136	CP(*in, *out, f_blocks);
137	CP(*in, *out, f_bfree);
138	CP(*in, *out, f_bavail);
139	CP(*in, *out, f_files);
140	CP(*in, *out, f_ffree);
141	CP(*in, *out, f_fsid);
142	CP(*in, *out, f_owner);
143	CP(*in, *out, f_type);
144	CP(*in, *out, f_flags);
145	CP(*in, *out, f_flags);
146	CP(*in, *out, f_syncwrites);
147	CP(*in, *out, f_asyncwrites);
148	bcopy(in->f_fstypename,
149	      out->f_fstypename, MFSNAMELEN);
150	bcopy(in->f_mntonname,
151	      out->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
152	CP(*in, *out, f_syncreads);
153	CP(*in, *out, f_asyncreads);
154	bcopy(in->f_mntfromname,
155	      out->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
156}
157#endif
158
159#ifdef COMPAT_FREEBSD4
160int
161freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
162{
163	struct statfs *buf, *sp;
164	struct statfs32 stat32;
165	size_t count, size;
166	int error;
167
168	count = uap->bufsize / sizeof(struct statfs32);
169	size = count * sizeof(struct statfs);
170	error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
171	if (size > 0) {
172		count = td->td_retval[0];
173		sp = buf;
174		while (count > 0 && error == 0) {
175			copy_statfs(sp, &stat32);
176			error = copyout(&stat32, uap->buf, sizeof(stat32));
177			sp++;
178			uap->buf++;
179			count--;
180		}
181		free(buf, M_TEMP);
182	}
183	return (error);
184}
185#endif
186
187struct sigaltstack32 {
188	u_int32_t	ss_sp;
189	u_int32_t	ss_size;
190	int		ss_flags;
191};
192
193CTASSERT(sizeof(struct sigaltstack32) == 12);
194
195int
196freebsd32_sigaltstack(struct thread *td,
197		      struct freebsd32_sigaltstack_args *uap)
198{
199	struct sigaltstack32 s32;
200	struct sigaltstack ss, oss, *ssp;
201	int error;
202
203	if (uap->ss != NULL) {
204		error = copyin(uap->ss, &s32, sizeof(s32));
205		if (error)
206			return (error);
207		PTRIN_CP(s32, ss, ss_sp);
208		CP(s32, ss, ss_size);
209		CP(s32, ss, ss_flags);
210		ssp = &ss;
211	} else
212		ssp = NULL;
213	error = kern_sigaltstack(td, ssp, &oss);
214	if (error == 0 && uap->oss != NULL) {
215		PTROUT_CP(oss, s32, ss_sp);
216		CP(oss, s32, ss_size);
217		CP(oss, s32, ss_flags);
218		error = copyout(&s32, uap->oss, sizeof(s32));
219	}
220	return (error);
221}
222
223/*
224 * Custom version of exec_copyin_args() so that we can translate
225 * the pointers.
226 */
227static int
228freebsd32_exec_copyin_args(struct image_args *args, char *fname,
229    enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
230{
231	char *argp, *envp;
232	u_int32_t *p32, arg;
233	size_t length;
234	int error;
235
236	bzero(args, sizeof(*args));
237	if (argv == NULL)
238		return (EFAULT);
239
240	/*
241	 * Allocate temporary demand zeroed space for argument and
242	 *	environment strings
243	 */
244	args->buf = (char *) kmem_alloc_wait(exec_map,
245	    PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
246	if (args->buf == NULL)
247		return (ENOMEM);
248	args->begin_argv = args->buf;
249	args->endp = args->begin_argv;
250	args->stringspace = ARG_MAX;
251
252	args->fname = args->buf + ARG_MAX;
253
254	/*
255	 * Copy the file name.
256	 */
257	error = (segflg == UIO_SYSSPACE) ?
258	    copystr(fname, args->fname, PATH_MAX, &length) :
259	    copyinstr(fname, args->fname, PATH_MAX, &length);
260	if (error != 0)
261		return (error);
262
263	/*
264	 * extract arguments first
265	 */
266	p32 = argv;
267	for (;;) {
268		error = copyin(p32++, &arg, sizeof(arg));
269		if (error)
270			return (error);
271		if (arg == 0)
272			break;
273		argp = PTRIN(arg);
274		error = copyinstr(argp, args->endp, args->stringspace, &length);
275		if (error) {
276			if (error == ENAMETOOLONG)
277				return (E2BIG);
278			else
279				return (error);
280		}
281		args->stringspace -= length;
282		args->endp += length;
283		args->argc++;
284	}
285
286	args->begin_envv = args->endp;
287
288	/*
289	 * extract environment strings
290	 */
291	if (envv) {
292		p32 = envv;
293		for (;;) {
294			error = copyin(p32++, &arg, sizeof(arg));
295			if (error)
296				return (error);
297			if (arg == 0)
298				break;
299			envp = PTRIN(arg);
300			error = copyinstr(envp, args->endp, args->stringspace,
301			    &length);
302			if (error) {
303				if (error == ENAMETOOLONG)
304					return (E2BIG);
305				else
306					return (error);
307			}
308			args->stringspace -= length;
309			args->endp += length;
310			args->envc++;
311		}
312	}
313
314	return (0);
315}
316
317int
318freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
319{
320	struct image_args eargs;
321	int error;
322
323	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
324	    uap->argv, uap->envv);
325	if (error == 0)
326		error = kern_execve(td, &eargs, NULL);
327	return (error);
328}
329
330#ifdef __ia64__
331static int
332freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
333		       int prot, int fd, off_t pos)
334{
335	vm_map_t map;
336	vm_map_entry_t entry;
337	int rv;
338
339	map = &td->td_proc->p_vmspace->vm_map;
340	if (fd != -1)
341		prot |= VM_PROT_WRITE;
342
343	if (vm_map_lookup_entry(map, start, &entry)) {
344		if ((entry->protection & prot) != prot) {
345			rv = vm_map_protect(map,
346					    trunc_page(start),
347					    round_page(end),
348					    entry->protection | prot,
349					    FALSE);
350			if (rv != KERN_SUCCESS)
351				return (EINVAL);
352		}
353	} else {
354		vm_offset_t addr = trunc_page(start);
355		rv = vm_map_find(map, 0, 0,
356				 &addr, PAGE_SIZE, FALSE, prot,
357				 VM_PROT_ALL, 0);
358		if (rv != KERN_SUCCESS)
359			return (EINVAL);
360	}
361
362	if (fd != -1) {
363		struct pread_args r;
364		r.fd = fd;
365		r.buf = (void *) start;
366		r.nbyte = end - start;
367		r.offset = pos;
368		return (pread(td, &r));
369	} else {
370		while (start < end) {
371			subyte((void *) start, 0);
372			start++;
373		}
374		return (0);
375	}
376}
377#endif
378
379int
380freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
381{
382	struct mmap_args ap;
383	vm_offset_t addr = (vm_offset_t) uap->addr;
384	vm_size_t len	 = uap->len;
385	int prot	 = uap->prot;
386	int flags	 = uap->flags;
387	int fd		 = uap->fd;
388	off_t pos	 = (uap->poslo
389			    | ((off_t)uap->poshi << 32));
390#ifdef __ia64__
391	vm_size_t pageoff;
392	int error;
393
394	/*
395	 * Attempt to handle page size hassles.
396	 */
397	pageoff = (pos & PAGE_MASK);
398	if (flags & MAP_FIXED) {
399		vm_offset_t start, end;
400		start = addr;
401		end = addr + len;
402
403		mtx_lock(&Giant);
404		if (start != trunc_page(start)) {
405			error = freebsd32_mmap_partial(td, start,
406						       round_page(start), prot,
407						       fd, pos);
408			if (fd != -1)
409				pos += round_page(start) - start;
410			start = round_page(start);
411		}
412		if (end != round_page(end)) {
413			vm_offset_t t = trunc_page(end);
414			error = freebsd32_mmap_partial(td, t, end,
415						  prot, fd,
416						  pos + t - start);
417			end = trunc_page(end);
418		}
419		if (end > start && fd != -1 && (pos & PAGE_MASK)) {
420			/*
421			 * We can't map this region at all. The specified
422			 * address doesn't have the same alignment as the file
423			 * position. Fake the mapping by simply reading the
424			 * entire region into memory. First we need to make
425			 * sure the region exists.
426			 */
427			vm_map_t map;
428			struct pread_args r;
429			int rv;
430
431			prot |= VM_PROT_WRITE;
432			map = &td->td_proc->p_vmspace->vm_map;
433			rv = vm_map_remove(map, start, end);
434			if (rv != KERN_SUCCESS) {
435				mtx_unlock(&Giant);
436				return (EINVAL);
437			}
438			rv = vm_map_find(map, 0, 0,
439					 &start, end - start, FALSE,
440					 prot, VM_PROT_ALL, 0);
441			mtx_unlock(&Giant);
442			if (rv != KERN_SUCCESS)
443				return (EINVAL);
444			r.fd = fd;
445			r.buf = (void *) start;
446			r.nbyte = end - start;
447			r.offset = pos;
448			error = pread(td, &r);
449			if (error)
450				return (error);
451
452			td->td_retval[0] = addr;
453			return (0);
454		}
455		mtx_unlock(&Giant);
456		if (end == start) {
457			/*
458			 * After dealing with the ragged ends, there
459			 * might be none left.
460			 */
461			td->td_retval[0] = addr;
462			return (0);
463		}
464		addr = start;
465		len = end - start;
466	}
467#endif
468
469	ap.addr = (void *) addr;
470	ap.len = len;
471	ap.prot = prot;
472	ap.flags = flags;
473	ap.fd = fd;
474	ap.pos = pos;
475
476	return (mmap(td, &ap));
477}
478
479struct itimerval32 {
480	struct timeval32 it_interval;
481	struct timeval32 it_value;
482};
483
484CTASSERT(sizeof(struct itimerval32) == 16);
485
486int
487freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
488{
489	struct itimerval itv, oitv, *itvp;
490	struct itimerval32 i32;
491	int error;
492
493	if (uap->itv != NULL) {
494		error = copyin(uap->itv, &i32, sizeof(i32));
495		if (error)
496			return (error);
497		TV_CP(i32, itv, it_interval);
498		TV_CP(i32, itv, it_value);
499		itvp = &itv;
500	} else
501		itvp = NULL;
502	error = kern_setitimer(td, uap->which, itvp, &oitv);
503	if (error || uap->oitv == NULL)
504		return (error);
505	TV_CP(oitv, i32, it_interval);
506	TV_CP(oitv, i32, it_value);
507	return (copyout(&i32, uap->oitv, sizeof(i32)));
508}
509
510int
511freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
512{
513	struct itimerval itv;
514	struct itimerval32 i32;
515	int error;
516
517	error = kern_getitimer(td, uap->which, &itv);
518	if (error || uap->itv == NULL)
519		return (error);
520	TV_CP(itv, i32, it_interval);
521	TV_CP(itv, i32, it_value);
522	return (copyout(&i32, uap->itv, sizeof(i32)));
523}
524
525int
526freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
527{
528	struct timeval32 tv32;
529	struct timeval tv, *tvp;
530	int error;
531
532	if (uap->tv != NULL) {
533		error = copyin(uap->tv, &tv32, sizeof(tv32));
534		if (error)
535			return (error);
536		CP(tv32, tv, tv_sec);
537		CP(tv32, tv, tv_usec);
538		tvp = &tv;
539	} else
540		tvp = NULL;
541	/*
542	 * XXX big-endian needs to convert the fd_sets too.
543	 * XXX Do pointers need PTRIN()?
544	 */
545	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
546}
547
548struct kevent32 {
549	u_int32_t	ident;		/* identifier for this event */
550	short		filter;		/* filter for event */
551	u_short		flags;
552	u_int		fflags;
553	int32_t		data;
554	u_int32_t	udata;		/* opaque user data identifier */
555};
556
557CTASSERT(sizeof(struct kevent32) == 20);
558static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
559static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
560
561/*
562 * Copy 'count' items into the destination list pointed to by uap->eventlist.
563 */
564static int
565freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
566{
567	struct freebsd32_kevent_args *uap;
568	struct kevent32	ks32[KQ_NEVENTS];
569	int i, error = 0;
570
571	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
572	uap = (struct freebsd32_kevent_args *)arg;
573
574	for (i = 0; i < count; i++) {
575		CP(kevp[i], ks32[i], ident);
576		CP(kevp[i], ks32[i], filter);
577		CP(kevp[i], ks32[i], flags);
578		CP(kevp[i], ks32[i], fflags);
579		CP(kevp[i], ks32[i], data);
580		PTROUT_CP(kevp[i], ks32[i], udata);
581	}
582	error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
583	if (error == 0)
584		uap->eventlist += count;
585	return (error);
586}
587
588/*
589 * Copy 'count' items from the list pointed to by uap->changelist.
590 */
591static int
592freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
593{
594	struct freebsd32_kevent_args *uap;
595	struct kevent32	ks32[KQ_NEVENTS];
596	int i, error = 0;
597
598	KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
599	uap = (struct freebsd32_kevent_args *)arg;
600
601	error = copyin(uap->changelist, ks32, count * sizeof *ks32);
602	if (error)
603		goto done;
604	uap->changelist += count;
605
606	for (i = 0; i < count; i++) {
607		CP(ks32[i], kevp[i], ident);
608		CP(ks32[i], kevp[i], filter);
609		CP(ks32[i], kevp[i], flags);
610		CP(ks32[i], kevp[i], fflags);
611		CP(ks32[i], kevp[i], data);
612		PTRIN_CP(ks32[i], kevp[i], udata);
613	}
614done:
615	return (error);
616}
617
618int
619freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
620{
621	struct timespec32 ts32;
622	struct timespec ts, *tsp;
623	struct kevent_copyops k_ops = { uap,
624					freebsd32_kevent_copyout,
625					freebsd32_kevent_copyin};
626	int error;
627
628
629	if (uap->timeout) {
630		error = copyin(uap->timeout, &ts32, sizeof(ts32));
631		if (error)
632			return (error);
633		CP(ts32, ts, tv_sec);
634		CP(ts32, ts, tv_nsec);
635		tsp = &ts;
636	} else
637		tsp = NULL;
638	error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
639	    &k_ops, tsp);
640	return (error);
641}
642
643int
644freebsd32_gettimeofday(struct thread *td,
645		       struct freebsd32_gettimeofday_args *uap)
646{
647	struct timeval atv;
648	struct timeval32 atv32;
649	struct timezone rtz;
650	int error = 0;
651
652	if (uap->tp) {
653		microtime(&atv);
654		CP(atv, atv32, tv_sec);
655		CP(atv, atv32, tv_usec);
656		error = copyout(&atv32, uap->tp, sizeof (atv32));
657	}
658	if (error == 0 && uap->tzp != NULL) {
659		rtz.tz_minuteswest = tz_minuteswest;
660		rtz.tz_dsttime = tz_dsttime;
661		error = copyout(&rtz, uap->tzp, sizeof (rtz));
662	}
663	return (error);
664}
665
666int
667freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
668{
669	struct rusage32 s32;
670	struct rusage s;
671	int error;
672
673	error = kern_getrusage(td, uap->who, &s);
674	if (error)
675		return (error);
676	if (uap->rusage != NULL) {
677		TV_CP(s, s32, ru_utime);
678		TV_CP(s, s32, ru_stime);
679		CP(s, s32, ru_maxrss);
680		CP(s, s32, ru_ixrss);
681		CP(s, s32, ru_idrss);
682		CP(s, s32, ru_isrss);
683		CP(s, s32, ru_minflt);
684		CP(s, s32, ru_majflt);
685		CP(s, s32, ru_nswap);
686		CP(s, s32, ru_inblock);
687		CP(s, s32, ru_oublock);
688		CP(s, s32, ru_msgsnd);
689		CP(s, s32, ru_msgrcv);
690		CP(s, s32, ru_nsignals);
691		CP(s, s32, ru_nvcsw);
692		CP(s, s32, ru_nivcsw);
693		error = copyout(&s32, uap->rusage, sizeof(s32));
694	}
695	return (error);
696}
697
698struct iovec32 {
699	u_int32_t iov_base;
700	int	iov_len;
701};
702
703CTASSERT(sizeof(struct iovec32) == 8);
704
705static int
706freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
707{
708	struct iovec32 iov32;
709	struct iovec *iov;
710	struct uio *uio;
711	u_int iovlen;
712	int error, i;
713
714	*uiop = NULL;
715	if (iovcnt > UIO_MAXIOV)
716		return (EINVAL);
717	iovlen = iovcnt * sizeof(struct iovec);
718	uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
719	iov = (struct iovec *)(uio + 1);
720	for (i = 0; i < iovcnt; i++) {
721		error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
722		if (error) {
723			free(uio, M_IOV);
724			return (error);
725		}
726		iov[i].iov_base = PTRIN(iov32.iov_base);
727		iov[i].iov_len = iov32.iov_len;
728	}
729	uio->uio_iov = iov;
730	uio->uio_iovcnt = iovcnt;
731	uio->uio_segflg = UIO_USERSPACE;
732	uio->uio_offset = -1;
733	uio->uio_resid = 0;
734	for (i = 0; i < iovcnt; i++) {
735		if (iov->iov_len > INT_MAX - uio->uio_resid) {
736			free(uio, M_IOV);
737			return (EINVAL);
738		}
739		uio->uio_resid += iov->iov_len;
740		iov++;
741	}
742	*uiop = uio;
743	return (0);
744}
745
746int
747freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
748{
749	struct uio *auio;
750	int error;
751
752	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
753	if (error)
754		return (error);
755	error = kern_readv(td, uap->fd, auio);
756	free(auio, M_IOV);
757	return (error);
758}
759
760int
761freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
762{
763	struct uio *auio;
764	int error;
765
766	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
767	if (error)
768		return (error);
769	error = kern_writev(td, uap->fd, auio);
770	free(auio, M_IOV);
771	return (error);
772}
773
774int
775freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
776{
777	struct uio *auio;
778	int error;
779
780	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
781	if (error)
782		return (error);
783	error = kern_preadv(td, uap->fd, auio, uap->offset);
784	free(auio, M_IOV);
785	return (error);
786}
787
788int
789freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
790{
791	struct uio *auio;
792	int error;
793
794	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
795	if (error)
796		return (error);
797	error = kern_pwritev(td, uap->fd, auio, uap->offset);
798	free(auio, M_IOV);
799	return (error);
800}
801
802static int
803freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
804    int error)
805{
806	struct iovec32 iov32;
807	struct iovec *iov;
808	u_int iovlen;
809	int i;
810
811	*iovp = NULL;
812	if (iovcnt > UIO_MAXIOV)
813		return (error);
814	iovlen = iovcnt * sizeof(struct iovec);
815	iov = malloc(iovlen, M_IOV, M_WAITOK);
816	for (i = 0; i < iovcnt; i++) {
817		error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
818		if (error) {
819			free(iov, M_IOV);
820			return (error);
821		}
822		iov[i].iov_base = PTRIN(iov32.iov_base);
823		iov[i].iov_len = iov32.iov_len;
824	}
825	*iovp = iov;
826	return (0);
827}
828
829static int
830freebsd32_copyoutiov(struct iovec *iov, u_int iovcnt, struct iovec32 *iovp,
831    int error)
832{
833	struct iovec32 iov32;
834	int i;
835
836	if (iovcnt > UIO_MAXIOV)
837		return (error);
838	for (i = 0; i < iovcnt; i++) {
839		iov32.iov_base = PTROUT(iov[i].iov_base);
840		iov32.iov_len = iov[i].iov_len;
841		error = copyout(&iov32, &iovp[i], sizeof(iov32));
842		if (error)
843			return (error);
844	}
845	return (0);
846}
847
848
849struct msghdr32 {
850	u_int32_t	 msg_name;
851	socklen_t	 msg_namelen;
852	u_int32_t	 msg_iov;
853	int		 msg_iovlen;
854	u_int32_t	 msg_control;
855	socklen_t	 msg_controllen;
856	int		 msg_flags;
857};
858CTASSERT(sizeof(struct msghdr32) == 28);
859
860static int
861freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
862{
863	struct msghdr32 m32;
864	int error;
865
866	error = copyin(msg32, &m32, sizeof(m32));
867	if (error)
868		return (error);
869	msg->msg_name = PTRIN(m32.msg_name);
870	msg->msg_namelen = m32.msg_namelen;
871	msg->msg_iov = PTRIN(m32.msg_iov);
872	msg->msg_iovlen = m32.msg_iovlen;
873	msg->msg_control = PTRIN(m32.msg_control);
874	msg->msg_controllen = m32.msg_controllen;
875	msg->msg_flags = m32.msg_flags;
876	return (0);
877}
878
879static int
880freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
881{
882	struct msghdr32 m32;
883	int error;
884
885	m32.msg_name = PTROUT(msg->msg_name);
886	m32.msg_namelen = msg->msg_namelen;
887	m32.msg_iov = PTROUT(msg->msg_iov);
888	m32.msg_iovlen = msg->msg_iovlen;
889	m32.msg_control = PTROUT(msg->msg_control);
890	m32.msg_controllen = msg->msg_controllen;
891	m32.msg_flags = msg->msg_flags;
892	error = copyout(&m32, msg32, sizeof(m32));
893	return (error);
894}
895
896#define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
897#define FREEBSD32_ALIGN(p)	\
898	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
899#define	FREEBSD32_CMSG_SPACE(l)	\
900	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
901
902#define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
903				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
904static int
905freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
906{
907	struct cmsghdr *cm;
908	void *data;
909	socklen_t clen, datalen;
910	int error;
911	caddr_t ctlbuf;
912	int len, maxlen, copylen;
913	struct mbuf *m;
914	error = 0;
915
916	len    = msg->msg_controllen;
917	maxlen = msg->msg_controllen;
918	msg->msg_controllen = 0;
919
920	m = control;
921	ctlbuf = msg->msg_control;
922
923	while (m && len > 0) {
924		cm = mtod(m, struct cmsghdr *);
925		clen = m->m_len;
926
927		while (cm != NULL) {
928
929			if (sizeof(struct cmsghdr) > clen ||
930			    cm->cmsg_len > clen) {
931				error = EINVAL;
932				break;
933			}
934
935			data   = CMSG_DATA(cm);
936			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
937
938			/* Adjust message length */
939			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
940			    datalen;
941
942
943			/* Copy cmsghdr */
944			copylen = sizeof(struct cmsghdr);
945			if (len < copylen) {
946				msg->msg_flags |= MSG_CTRUNC;
947				copylen = len;
948			}
949
950			error = copyout(cm,ctlbuf,copylen);
951			if (error)
952				goto exit;
953
954			ctlbuf += FREEBSD32_ALIGN(copylen);
955			len    -= FREEBSD32_ALIGN(copylen);
956
957			if (len <= 0)
958				break;
959
960			/* Copy data */
961			copylen = datalen;
962			if (len < copylen) {
963				msg->msg_flags |= MSG_CTRUNC;
964				copylen = len;
965			}
966
967			error = copyout(data,ctlbuf,copylen);
968			if (error)
969				goto exit;
970
971			ctlbuf += FREEBSD32_ALIGN(copylen);
972			len    -= FREEBSD32_ALIGN(copylen);
973
974			if (CMSG_SPACE(datalen) < clen) {
975				clen -= CMSG_SPACE(datalen);
976				cm = (struct cmsghdr *)
977					((caddr_t)cm + CMSG_SPACE(datalen));
978			} else {
979				clen = 0;
980				cm = NULL;
981			}
982		}
983		m = m->m_next;
984	}
985
986	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
987
988exit:
989	return (error);
990
991}
992
993int
994freebsd32_recvmsg(td, uap)
995	struct thread *td;
996	struct freebsd32_recvmsg_args /* {
997		int	s;
998		struct	msghdr32 *msg;
999		int	flags;
1000	} */ *uap;
1001{
1002	struct msghdr msg;
1003	struct msghdr32 m32;
1004	struct iovec *uiov, *iov;
1005	struct mbuf *control = NULL;
1006	struct mbuf **controlp;
1007
1008	int error;
1009	error = copyin(uap->msg, &m32, sizeof(m32));
1010	if (error)
1011		return (error);
1012	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1013	if (error)
1014		return (error);
1015	error = freebsd32_copyiniov((struct iovec32 *)(uintptr_t)m32.msg_iov,
1016	    m32.msg_iovlen, &iov, EMSGSIZE);
1017	if (error)
1018		return (error);
1019	msg.msg_flags = uap->flags;
1020	uiov = msg.msg_iov;
1021	msg.msg_iov = iov;
1022
1023	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1024	error = kern_recvit(td, uap->s, &msg, NULL, UIO_USERSPACE, controlp);
1025	if (error == 0) {
1026		msg.msg_iov = uiov;
1027
1028		if (control != NULL)
1029			error = freebsd32_copy_msg_out(&msg, control);
1030
1031		if (error == 0)
1032			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1033
1034		if (error == 0)
1035			error = freebsd32_copyoutiov(iov, m32.msg_iovlen,
1036			    (struct iovec32 *)(uintptr_t)m32.msg_iov, EMSGSIZE);
1037	}
1038	free(iov, M_IOV);
1039
1040	if (control != NULL)
1041		m_freem(control);
1042
1043	return (error);
1044}
1045
1046
1047static int
1048freebsd32_convert_msg_in(struct mbuf **controlp)
1049{
1050	struct mbuf *control = *controlp;
1051	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1052	void *data;
1053	socklen_t clen = control->m_len, datalen;
1054	int error;
1055
1056	error = 0;
1057	*controlp = NULL;
1058
1059	while (cm != NULL) {
1060		if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1061			error = EINVAL;
1062			break;
1063		}
1064
1065		data = FREEBSD32_CMSG_DATA(cm);
1066		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1067
1068		*controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1069		    cm->cmsg_level);
1070		controlp = &(*controlp)->m_next;
1071
1072		if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1073			clen -= FREEBSD32_CMSG_SPACE(datalen);
1074			cm = (struct cmsghdr *)
1075				((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1076		} else {
1077			clen = 0;
1078			cm = NULL;
1079		}
1080	}
1081
1082	m_freem(control);
1083	return (error);
1084}
1085
1086
1087int
1088freebsd32_sendmsg(struct thread *td,
1089		  struct freebsd32_sendmsg_args *uap)
1090{
1091	struct msghdr msg;
1092	struct msghdr32 m32;
1093	struct iovec *iov;
1094	struct mbuf *control = NULL;
1095	struct sockaddr *to = NULL;
1096	int error;
1097
1098	error = copyin(uap->msg, &m32, sizeof(m32));
1099	if (error)
1100		return (error);
1101	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1102	if (error)
1103		return (error);
1104	error = freebsd32_copyiniov((struct iovec32 *)(uintptr_t)m32.msg_iov,
1105	    m32.msg_iovlen, &iov, EMSGSIZE);
1106	if (error)
1107		return (error);
1108	msg.msg_iov = iov;
1109	if (msg.msg_name != NULL) {
1110		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1111		if (error) {
1112			to = NULL;
1113			goto out;
1114		}
1115		msg.msg_name = to;
1116	}
1117
1118	if (msg.msg_control) {
1119		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1120			error = EINVAL;
1121			goto out;
1122		}
1123
1124		error = sockargs(&control, msg.msg_control,
1125		    msg.msg_controllen, MT_CONTROL);
1126		if (error)
1127			goto out;
1128
1129		error = freebsd32_convert_msg_in(&control);
1130		if (error)
1131			goto out;
1132	}
1133
1134	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1135	    UIO_USERSPACE);
1136
1137out:
1138	free(iov, M_IOV);
1139	if (to)
1140		free(to, M_SONAME);
1141	return (error);
1142}
1143
1144int
1145freebsd32_recvfrom(struct thread *td,
1146		   struct freebsd32_recvfrom_args *uap)
1147{
1148	struct msghdr msg;
1149	struct iovec aiov;
1150	int error;
1151
1152	if (uap->fromlenaddr) {
1153		error = copyin((void *)(uintptr_t)uap->fromlenaddr,
1154		    &msg.msg_namelen, sizeof(msg.msg_namelen));
1155		if (error)
1156			return (error);
1157	} else {
1158		msg.msg_namelen = 0;
1159	}
1160
1161	msg.msg_name = (void *)(uintptr_t)uap->from;
1162	msg.msg_iov = &aiov;
1163	msg.msg_iovlen = 1;
1164	aiov.iov_base = (void *)(uintptr_t)uap->buf;
1165	aiov.iov_len = uap->len;
1166	msg.msg_control = 0;
1167	msg.msg_flags = uap->flags;
1168	error = kern_recvit(td, uap->s, &msg,
1169	    (void *)(uintptr_t)uap->fromlenaddr, UIO_USERSPACE, NULL);
1170	return (error);
1171}
1172
1173int
1174freebsd32_settimeofday(struct thread *td,
1175		       struct freebsd32_settimeofday_args *uap)
1176{
1177	struct timeval32 tv32;
1178	struct timeval tv, *tvp;
1179	struct timezone tz, *tzp;
1180	int error;
1181
1182	if (uap->tv) {
1183		error = copyin(uap->tv, &tv32, sizeof(tv32));
1184		if (error)
1185			return (error);
1186		CP(tv32, tv, tv_sec);
1187		CP(tv32, tv, tv_usec);
1188		tvp = &tv;
1189	} else
1190		tvp = NULL;
1191	if (uap->tzp) {
1192		error = copyin(uap->tzp, &tz, sizeof(tz));
1193		if (error)
1194			return (error);
1195		tzp = &tz;
1196	} else
1197		tzp = NULL;
1198	return (kern_settimeofday(td, tvp, tzp));
1199}
1200
1201int
1202freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1203{
1204	struct timeval32 s32[2];
1205	struct timeval s[2], *sp;
1206	int error;
1207
1208	if (uap->tptr != NULL) {
1209		error = copyin(uap->tptr, s32, sizeof(s32));
1210		if (error)
1211			return (error);
1212		CP(s32[0], s[0], tv_sec);
1213		CP(s32[0], s[0], tv_usec);
1214		CP(s32[1], s[1], tv_sec);
1215		CP(s32[1], s[1], tv_usec);
1216		sp = s;
1217	} else
1218		sp = NULL;
1219	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1220}
1221
1222int
1223freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1224{
1225	struct timeval32 s32[2];
1226	struct timeval s[2], *sp;
1227	int error;
1228
1229	if (uap->tptr != NULL) {
1230		error = copyin(uap->tptr, s32, sizeof(s32));
1231		if (error)
1232			return (error);
1233		CP(s32[0], s[0], tv_sec);
1234		CP(s32[0], s[0], tv_usec);
1235		CP(s32[1], s[1], tv_sec);
1236		CP(s32[1], s[1], tv_usec);
1237		sp = s;
1238	} else
1239		sp = NULL;
1240	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1241}
1242
1243int
1244freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1245{
1246	struct timeval32 s32[2];
1247	struct timeval s[2], *sp;
1248	int error;
1249
1250	if (uap->tptr != NULL) {
1251		error = copyin(uap->tptr, s32, sizeof(s32));
1252		if (error)
1253			return (error);
1254		CP(s32[0], s[0], tv_sec);
1255		CP(s32[0], s[0], tv_usec);
1256		CP(s32[1], s[1], tv_sec);
1257		CP(s32[1], s[1], tv_usec);
1258		sp = s;
1259	} else
1260		sp = NULL;
1261	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1262}
1263
1264
1265int
1266freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1267{
1268	struct timeval32 tv32;
1269	struct timeval delta, olddelta, *deltap;
1270	int error;
1271
1272	if (uap->delta) {
1273		error = copyin(uap->delta, &tv32, sizeof(tv32));
1274		if (error)
1275			return (error);
1276		CP(tv32, delta, tv_sec);
1277		CP(tv32, delta, tv_usec);
1278		deltap = &delta;
1279	} else
1280		deltap = NULL;
1281	error = kern_adjtime(td, deltap, &olddelta);
1282	if (uap->olddelta && error == 0) {
1283		CP(olddelta, tv32, tv_sec);
1284		CP(olddelta, tv32, tv_usec);
1285		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1286	}
1287	return (error);
1288}
1289
1290#ifdef COMPAT_FREEBSD4
1291int
1292freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1293{
1294	struct statfs32 s32;
1295	struct statfs s;
1296	int error;
1297
1298	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1299	if (error)
1300		return (error);
1301	copy_statfs(&s, &s32);
1302	return (copyout(&s32, uap->buf, sizeof(s32)));
1303}
1304#endif
1305
1306#ifdef COMPAT_FREEBSD4
1307int
1308freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1309{
1310	struct statfs32 s32;
1311	struct statfs s;
1312	int error;
1313
1314	error = kern_fstatfs(td, uap->fd, &s);
1315	if (error)
1316		return (error);
1317	copy_statfs(&s, &s32);
1318	return (copyout(&s32, uap->buf, sizeof(s32)));
1319}
1320#endif
1321
1322#ifdef COMPAT_FREEBSD4
1323int
1324freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1325{
1326	struct statfs32 s32;
1327	struct statfs s;
1328	fhandle_t fh;
1329	int error;
1330
1331	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1332		return (error);
1333	error = kern_fhstatfs(td, fh, &s);
1334	if (error)
1335		return (error);
1336	copy_statfs(&s, &s32);
1337	return (copyout(&s32, uap->buf, sizeof(s32)));
1338}
1339#endif
1340
1341int
1342freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1343{
1344	/*
1345	 * Vector through to semsys if it is loaded.
1346	 */
1347	return sysent[SYS_semsys].sy_call(td, uap);
1348}
1349
1350int
1351freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1352{
1353	/*
1354	 * Vector through to msgsys if it is loaded.
1355	 */
1356	return sysent[SYS_msgsys].sy_call(td, uap);
1357}
1358
1359int
1360freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1361{
1362	/*
1363	 * Vector through to shmsys if it is loaded.
1364	 */
1365	return sysent[SYS_shmsys].sy_call(td, uap);
1366}
1367
1368int
1369freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1370{
1371	struct pread_args ap;
1372
1373	ap.fd = uap->fd;
1374	ap.buf = uap->buf;
1375	ap.nbyte = uap->nbyte;
1376	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1377	return (pread(td, &ap));
1378}
1379
1380int
1381freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1382{
1383	struct pwrite_args ap;
1384
1385	ap.fd = uap->fd;
1386	ap.buf = uap->buf;
1387	ap.nbyte = uap->nbyte;
1388	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1389	return (pwrite(td, &ap));
1390}
1391
1392int
1393freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1394{
1395	int error;
1396	struct lseek_args ap;
1397	off_t pos;
1398
1399	ap.fd = uap->fd;
1400	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1401	ap.whence = uap->whence;
1402	error = lseek(td, &ap);
1403	/* Expand the quad return into two parts for eax and edx */
1404	pos = *(off_t *)(td->td_retval);
1405	td->td_retval[0] = pos & 0xffffffff;	/* %eax */
1406	td->td_retval[1] = pos >> 32;		/* %edx */
1407	return error;
1408}
1409
1410int
1411freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1412{
1413	struct truncate_args ap;
1414
1415	ap.path = uap->path;
1416	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1417	return (truncate(td, &ap));
1418}
1419
1420int
1421freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1422{
1423	struct ftruncate_args ap;
1424
1425	ap.fd = uap->fd;
1426	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1427	return (ftruncate(td, &ap));
1428}
1429
1430struct sf_hdtr32 {
1431	uint32_t headers;
1432	int hdr_cnt;
1433	uint32_t trailers;
1434	int trl_cnt;
1435};
1436
1437static int
1438freebsd32_do_sendfile(struct thread *td,
1439    struct freebsd32_sendfile_args *uap, int compat)
1440{
1441	struct sendfile_args ap;
1442	struct sf_hdtr32 hdtr32;
1443	struct sf_hdtr hdtr;
1444	struct uio *hdr_uio, *trl_uio;
1445	struct iovec32 *iov32;
1446	int error;
1447
1448	hdr_uio = trl_uio = NULL;
1449
1450	ap.fd = uap->fd;
1451	ap.s = uap->s;
1452	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1453	ap.nbytes = uap->nbytes;
1454	ap.hdtr = (struct sf_hdtr *)uap->hdtr;		/* XXX not used */
1455	ap.sbytes = uap->sbytes;
1456	ap.flags = uap->flags;
1457
1458	if (uap->hdtr != NULL) {
1459		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1460		if (error)
1461			goto out;
1462		PTRIN_CP(hdtr32, hdtr, headers);
1463		CP(hdtr32, hdtr, hdr_cnt);
1464		PTRIN_CP(hdtr32, hdtr, trailers);
1465		CP(hdtr32, hdtr, trl_cnt);
1466
1467		if (hdtr.headers != NULL) {
1468			iov32 = (struct iovec32 *)(uintptr_t)hdtr32.headers;
1469			error = freebsd32_copyinuio(iov32,
1470			    hdtr32.hdr_cnt, &hdr_uio);
1471			if (error)
1472				goto out;
1473		}
1474		if (hdtr.trailers != NULL) {
1475			iov32 = (struct iovec32 *)(uintptr_t)hdtr32.trailers;
1476			error = freebsd32_copyinuio(iov32,
1477			    hdtr32.trl_cnt, &trl_uio);
1478			if (error)
1479				goto out;
1480		}
1481	}
1482
1483	error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1484out:
1485	if (hdr_uio)
1486		free(hdr_uio, M_IOV);
1487	if (trl_uio)
1488		free(trl_uio, M_IOV);
1489	return (error);
1490}
1491
1492#ifdef COMPAT_FREEBSD4
1493int
1494freebsd4_freebsd32_sendfile(struct thread *td,
1495    struct freebsd4_freebsd32_sendfile_args *uap)
1496{
1497	return (freebsd32_do_sendfile(td,
1498	    (struct freebsd32_sendfile_args *)uap, 1));
1499}
1500#endif
1501
1502int
1503freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1504{
1505
1506	return (freebsd32_do_sendfile(td, uap, 0));
1507}
1508
1509struct stat32 {
1510	dev_t	st_dev;
1511	ino_t	st_ino;
1512	mode_t	st_mode;
1513	nlink_t	st_nlink;
1514	uid_t	st_uid;
1515	gid_t	st_gid;
1516	dev_t	st_rdev;
1517	struct timespec32 st_atimespec;
1518	struct timespec32 st_mtimespec;
1519	struct timespec32 st_ctimespec;
1520	off_t	st_size;
1521	int64_t	st_blocks;
1522	u_int32_t st_blksize;
1523	u_int32_t st_flags;
1524	u_int32_t st_gen;
1525	struct timespec32 st_birthtimespec;
1526	unsigned int :(8 / 2) * (16 - (int)sizeof(struct timespec32));
1527	unsigned int :(8 / 2) * (16 - (int)sizeof(struct timespec32));
1528};
1529
1530
1531CTASSERT(sizeof(struct stat32) == 96);
1532
1533static void
1534copy_stat( struct stat *in, struct stat32 *out)
1535{
1536	CP(*in, *out, st_dev);
1537	CP(*in, *out, st_ino);
1538	CP(*in, *out, st_mode);
1539	CP(*in, *out, st_nlink);
1540	CP(*in, *out, st_uid);
1541	CP(*in, *out, st_gid);
1542	CP(*in, *out, st_rdev);
1543	TS_CP(*in, *out, st_atimespec);
1544	TS_CP(*in, *out, st_mtimespec);
1545	TS_CP(*in, *out, st_ctimespec);
1546	CP(*in, *out, st_size);
1547	CP(*in, *out, st_blocks);
1548	CP(*in, *out, st_blksize);
1549	CP(*in, *out, st_flags);
1550	CP(*in, *out, st_gen);
1551}
1552
1553int
1554freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1555{
1556	struct stat sb;
1557	struct stat32 sb32;
1558	int error;
1559
1560	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1561	if (error)
1562		return (error);
1563	copy_stat(&sb, &sb32);
1564	error = copyout(&sb32, uap->ub, sizeof (sb32));
1565	return (error);
1566}
1567
1568int
1569freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1570{
1571	struct stat ub;
1572	struct stat32 ub32;
1573	int error;
1574
1575	error = kern_fstat(td, uap->fd, &ub);
1576	if (error)
1577		return (error);
1578	copy_stat(&ub, &ub32);
1579	error = copyout(&ub32, uap->ub, sizeof(ub32));
1580	return (error);
1581}
1582
1583int
1584freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1585{
1586	struct stat sb;
1587	struct stat32 sb32;
1588	int error;
1589
1590	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1591	if (error)
1592		return (error);
1593	copy_stat(&sb, &sb32);
1594	error = copyout(&sb32, uap->ub, sizeof (sb32));
1595	return (error);
1596}
1597
1598/*
1599 * MPSAFE
1600 */
1601int
1602freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1603{
1604	int error, name[CTL_MAXNAME];
1605	size_t j, oldlen;
1606
1607	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1608		return (EINVAL);
1609 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1610 	if (error)
1611		return (error);
1612	mtx_lock(&Giant);
1613	if (uap->oldlenp)
1614		oldlen = fuword32(uap->oldlenp);
1615	else
1616		oldlen = 0;
1617	error = userland_sysctl(td, name, uap->namelen,
1618		uap->old, &oldlen, 1,
1619		uap->new, uap->newlen, &j, SCTL_MASK32);
1620	if (error && error != ENOMEM)
1621		goto done2;
1622	if (uap->oldlenp)
1623		suword32(uap->oldlenp, j);
1624done2:
1625	mtx_unlock(&Giant);
1626	return (error);
1627}
1628
1629struct sigaction32 {
1630	u_int32_t	sa_u;
1631	int		sa_flags;
1632	sigset_t	sa_mask;
1633};
1634
1635CTASSERT(sizeof(struct sigaction32) == 24);
1636
1637int
1638freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1639{
1640	struct sigaction32 s32;
1641	struct sigaction sa, osa, *sap;
1642	int error;
1643
1644	if (uap->act) {
1645		error = copyin(uap->act, &s32, sizeof(s32));
1646		if (error)
1647			return (error);
1648		sa.sa_handler = PTRIN(s32.sa_u);
1649		CP(s32, sa, sa_flags);
1650		CP(s32, sa, sa_mask);
1651		sap = &sa;
1652	} else
1653		sap = NULL;
1654	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1655	if (error == 0 && uap->oact != NULL) {
1656		s32.sa_u = PTROUT(osa.sa_handler);
1657		CP(osa, s32, sa_flags);
1658		CP(osa, s32, sa_mask);
1659		error = copyout(&s32, uap->oact, sizeof(s32));
1660	}
1661	return (error);
1662}
1663
1664#ifdef COMPAT_FREEBSD4
1665int
1666freebsd4_freebsd32_sigaction(struct thread *td,
1667			     struct freebsd4_freebsd32_sigaction_args *uap)
1668{
1669	struct sigaction32 s32;
1670	struct sigaction sa, osa, *sap;
1671	int error;
1672
1673	if (uap->act) {
1674		error = copyin(uap->act, &s32, sizeof(s32));
1675		if (error)
1676			return (error);
1677		sa.sa_handler = PTRIN(s32.sa_u);
1678		CP(s32, sa, sa_flags);
1679		CP(s32, sa, sa_mask);
1680		sap = &sa;
1681	} else
1682		sap = NULL;
1683	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
1684	if (error == 0 && uap->oact != NULL) {
1685		s32.sa_u = PTROUT(osa.sa_handler);
1686		CP(osa, s32, sa_flags);
1687		CP(osa, s32, sa_mask);
1688		error = copyout(&s32, uap->oact, sizeof(s32));
1689	}
1690	return (error);
1691}
1692#endif
1693
1694#ifdef COMPAT_43
1695struct osigaction32 {
1696	u_int32_t	sa_u;
1697	osigset_t	sa_mask;
1698	int		sa_flags;
1699};
1700
1701#define	ONSIG	32
1702
1703int
1704ofreebsd32_sigaction(struct thread *td,
1705			     struct ofreebsd32_sigaction_args *uap)
1706{
1707	struct osigaction32 s32;
1708	struct sigaction sa, osa, *sap;
1709	int error;
1710
1711	if (uap->signum <= 0 || uap->signum >= ONSIG)
1712		return (EINVAL);
1713
1714	if (uap->nsa) {
1715		error = copyin(uap->nsa, &s32, sizeof(s32));
1716		if (error)
1717			return (error);
1718		sa.sa_handler = PTRIN(s32.sa_u);
1719		CP(s32, sa, sa_flags);
1720		OSIG2SIG(s32.sa_mask, sa.sa_mask);
1721		sap = &sa;
1722	} else
1723		sap = NULL;
1724	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
1725	if (error == 0 && uap->osa != NULL) {
1726		s32.sa_u = PTROUT(osa.sa_handler);
1727		CP(osa, s32, sa_flags);
1728		SIG2OSIG(osa.sa_mask, s32.sa_mask);
1729		error = copyout(&s32, uap->osa, sizeof(s32));
1730	}
1731	return (error);
1732}
1733
1734int
1735ofreebsd32_sigprocmask(struct thread *td,
1736			       struct ofreebsd32_sigprocmask_args *uap)
1737{
1738	sigset_t set, oset;
1739	int error;
1740
1741	OSIG2SIG(uap->mask, set);
1742	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1743	SIG2OSIG(oset, td->td_retval[0]);
1744	return (error);
1745}
1746
1747int
1748ofreebsd32_sigpending(struct thread *td,
1749			      struct ofreebsd32_sigpending_args *uap)
1750{
1751	struct proc *p = td->td_proc;
1752	sigset_t siglist;
1753
1754	PROC_LOCK(p);
1755	siglist = p->p_siglist;
1756	SIGSETOR(siglist, td->td_siglist);
1757	PROC_UNLOCK(p);
1758	SIG2OSIG(siglist, td->td_retval[0]);
1759	return (0);
1760}
1761
1762struct sigvec32 {
1763	u_int32_t	sv_handler;
1764	int		sv_mask;
1765	int		sv_flags;
1766};
1767
1768int
1769ofreebsd32_sigvec(struct thread *td,
1770			  struct ofreebsd32_sigvec_args *uap)
1771{
1772	struct sigvec32 vec;
1773	struct sigaction sa, osa, *sap;
1774	int error;
1775
1776	if (uap->signum <= 0 || uap->signum >= ONSIG)
1777		return (EINVAL);
1778
1779	if (uap->nsv) {
1780		error = copyin(uap->nsv, &vec, sizeof(vec));
1781		if (error)
1782			return (error);
1783		sa.sa_handler = PTRIN(vec.sv_handler);
1784		OSIG2SIG(vec.sv_mask, sa.sa_mask);
1785		sa.sa_flags = vec.sv_flags;
1786		sa.sa_flags ^= SA_RESTART;
1787		sap = &sa;
1788	} else
1789		sap = NULL;
1790	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
1791	if (error == 0 && uap->osv != NULL) {
1792		vec.sv_handler = PTROUT(osa.sa_handler);
1793		SIG2OSIG(osa.sa_mask, vec.sv_mask);
1794		vec.sv_flags = osa.sa_flags;
1795		vec.sv_flags &= ~SA_NOCLDWAIT;
1796		vec.sv_flags ^= SA_RESTART;
1797		error = copyout(&vec, uap->osv, sizeof(vec));
1798	}
1799	return (error);
1800}
1801
1802int
1803ofreebsd32_sigblock(struct thread *td,
1804			    struct ofreebsd32_sigblock_args *uap)
1805{
1806	struct proc *p = td->td_proc;
1807	sigset_t set;
1808
1809	OSIG2SIG(uap->mask, set);
1810	SIG_CANTMASK(set);
1811	PROC_LOCK(p);
1812	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1813	SIGSETOR(td->td_sigmask, set);
1814	PROC_UNLOCK(p);
1815	return (0);
1816}
1817
1818int
1819ofreebsd32_sigsetmask(struct thread *td,
1820			      struct ofreebsd32_sigsetmask_args *uap)
1821{
1822	struct proc *p = td->td_proc;
1823	sigset_t set;
1824
1825	OSIG2SIG(uap->mask, set);
1826	SIG_CANTMASK(set);
1827	PROC_LOCK(p);
1828	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1829	SIGSETLO(td->td_sigmask, set);
1830	signotify(td);
1831	PROC_UNLOCK(p);
1832	return (0);
1833}
1834
1835int
1836ofreebsd32_sigsuspend(struct thread *td,
1837			      struct ofreebsd32_sigsuspend_args *uap)
1838{
1839	struct proc *p = td->td_proc;
1840	sigset_t mask;
1841
1842	PROC_LOCK(p);
1843	td->td_oldsigmask = td->td_sigmask;
1844	td->td_pflags |= TDP_OLDMASK;
1845	OSIG2SIG(uap->mask, mask);
1846	SIG_CANTMASK(mask);
1847	SIGSETLO(td->td_sigmask, mask);
1848	signotify(td);
1849	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1850		/* void */;
1851	PROC_UNLOCK(p);
1852	/* always return EINTR rather than ERESTART... */
1853	return (EINTR);
1854}
1855
1856struct sigstack32 {
1857	u_int32_t	ss_sp;
1858	int		ss_onstack;
1859};
1860
1861int
1862ofreebsd32_sigstack(struct thread *td,
1863			    struct ofreebsd32_sigstack_args *uap)
1864{
1865	struct sigstack32 s32;
1866	struct sigstack nss, oss;
1867	int error = 0;
1868
1869	if (uap->nss != NULL) {
1870		error = copyin(uap->nss, &s32, sizeof(s32));
1871		if (error)
1872			return (error);
1873		nss.ss_sp = PTRIN(s32.ss_sp);
1874		CP(s32, nss, ss_onstack);
1875	}
1876	oss.ss_sp = td->td_sigstk.ss_sp;
1877	oss.ss_onstack = sigonstack(cpu_getstack(td));
1878	if (uap->nss != NULL) {
1879		td->td_sigstk.ss_sp = nss.ss_sp;
1880		td->td_sigstk.ss_size = 0;
1881		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1882		td->td_pflags |= TDP_ALTSTACK;
1883	}
1884	if (uap->oss != NULL) {
1885		s32.ss_sp = PTROUT(oss.ss_sp);
1886		CP(oss, s32, ss_onstack);
1887		error = copyout(&s32, uap->oss, sizeof(s32));
1888	}
1889	return (error);
1890}
1891#endif
1892
1893int
1894freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
1895{
1896	struct timespec32 rmt32, rqt32;
1897	struct timespec rmt, rqt;
1898	int error;
1899
1900	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
1901	if (error)
1902		return (error);
1903
1904	CP(rqt32, rqt, tv_sec);
1905	CP(rqt32, rqt, tv_nsec);
1906
1907	if (uap->rmtp &&
1908	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
1909		return (EFAULT);
1910	error = kern_nanosleep(td, &rqt, &rmt);
1911	if (error && uap->rmtp) {
1912		int error2;
1913
1914		CP(rmt, rmt32, tv_sec);
1915		CP(rmt, rmt32, tv_nsec);
1916
1917		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
1918		if (error2)
1919			error = error2;
1920	}
1921	return (error);
1922}
1923
1924int
1925freebsd32_clock_gettime(struct thread *td,
1926			struct freebsd32_clock_gettime_args *uap)
1927{
1928	struct timespec	ats;
1929	struct timespec32 ats32;
1930	int error;
1931
1932	error = kern_clock_gettime(td, uap->clock_id, &ats);
1933	if (error == 0) {
1934		CP(ats, ats32, tv_sec);
1935		CP(ats, ats32, tv_nsec);
1936		error = copyout(&ats32, uap->tp, sizeof(ats32));
1937	}
1938	return (error);
1939}
1940
1941int
1942freebsd32_clock_settime(struct thread *td,
1943			struct freebsd32_clock_settime_args *uap)
1944{
1945	struct timespec	ats;
1946	struct timespec32 ats32;
1947	int error;
1948
1949	error = copyin(uap->tp, &ats32, sizeof(ats32));
1950	if (error)
1951		return (error);
1952	CP(ats32, ats, tv_sec);
1953	CP(ats32, ats, tv_nsec);
1954
1955	return (kern_clock_settime(td, uap->clock_id, &ats));
1956}
1957
1958int
1959freebsd32_clock_getres(struct thread *td,
1960		       struct freebsd32_clock_getres_args *uap)
1961{
1962	struct timespec	ts;
1963	struct timespec32 ts32;
1964	int error;
1965
1966	if (uap->tp == NULL)
1967		return (0);
1968	error = kern_clock_getres(td, uap->clock_id, &ts);
1969	if (error == 0) {
1970		CP(ts, ts32, tv_sec);
1971		CP(ts, ts32, tv_nsec);
1972		error = copyout(&ts32, uap->tp, sizeof(ts32));
1973	}
1974	return (error);
1975}
1976
1977#if 0
1978
1979int
1980freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
1981{
1982	int error;
1983	struct yyy32 *p32, s32;
1984	struct yyy *p = NULL, s;
1985
1986	if (uap->zzz) {
1987		error = copyin(uap->zzz, &s32, sizeof(s32));
1988		if (error)
1989			return (error);
1990		/* translate in */
1991		p = &s;
1992	}
1993	error = kern_xxx(td, p);
1994	if (error)
1995		return (error);
1996	if (uap->zzz) {
1997		/* translate out */
1998		error = copyout(&s32, p32, sizeof(s32));
1999	}
2000	return (error);
2001}
2002
2003#endif
2004