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