freebsd32_misc.c revision 157285
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 157285 2006-03-30 07:42:32Z 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#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
837static int
838freebsd32_copyoutiov(struct iovec *iov, u_int iovcnt, struct iovec32 *iovp,
839    int error)
840{
841	struct iovec32 iov32;
842	int i;
843
844	if (iovcnt > UIO_MAXIOV)
845		return (error);
846	for (i = 0; i < iovcnt; i++) {
847		iov32.iov_base = PTROUT(iov[i].iov_base);
848		iov32.iov_len = iov[i].iov_len;
849		error = copyout(&iov32, &iovp[i], sizeof(iov32));
850		if (error)
851			return (error);
852	}
853	return (0);
854}
855
856
857struct msghdr32 {
858	u_int32_t	 msg_name;
859	socklen_t	 msg_namelen;
860	u_int32_t	 msg_iov;
861	int		 msg_iovlen;
862	u_int32_t	 msg_control;
863	socklen_t	 msg_controllen;
864	int		 msg_flags;
865};
866CTASSERT(sizeof(struct msghdr32) == 28);
867
868static int
869freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
870{
871	struct msghdr32 m32;
872	int error;
873
874	error = copyin(msg32, &m32, sizeof(m32));
875	if (error)
876		return (error);
877	msg->msg_name = PTRIN(m32.msg_name);
878	msg->msg_namelen = m32.msg_namelen;
879	msg->msg_iov = PTRIN(m32.msg_iov);
880	msg->msg_iovlen = m32.msg_iovlen;
881	msg->msg_control = PTRIN(m32.msg_control);
882	msg->msg_controllen = m32.msg_controllen;
883	msg->msg_flags = m32.msg_flags;
884	return (0);
885}
886
887static int
888freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
889{
890	struct msghdr32 m32;
891	int error;
892
893	m32.msg_name = PTROUT(msg->msg_name);
894	m32.msg_namelen = msg->msg_namelen;
895	m32.msg_iov = PTROUT(msg->msg_iov);
896	m32.msg_iovlen = msg->msg_iovlen;
897	m32.msg_control = PTROUT(msg->msg_control);
898	m32.msg_controllen = msg->msg_controllen;
899	m32.msg_flags = msg->msg_flags;
900	error = copyout(&m32, msg32, sizeof(m32));
901	return (error);
902}
903
904#define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
905#define FREEBSD32_ALIGN(p)	\
906	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
907#define	FREEBSD32_CMSG_SPACE(l)	\
908	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
909
910#define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
911				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
912static int
913freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
914{
915	struct cmsghdr *cm;
916	void *data;
917	socklen_t clen, datalen;
918	int error;
919	caddr_t ctlbuf;
920	int len, maxlen, copylen;
921	struct mbuf *m;
922	error = 0;
923
924	len    = msg->msg_controllen;
925	maxlen = msg->msg_controllen;
926	msg->msg_controllen = 0;
927
928	m = control;
929	ctlbuf = msg->msg_control;
930
931	while (m && len > 0) {
932		cm = mtod(m, struct cmsghdr *);
933		clen = m->m_len;
934
935		while (cm != NULL) {
936
937			if (sizeof(struct cmsghdr) > clen ||
938			    cm->cmsg_len > clen) {
939				error = EINVAL;
940				break;
941			}
942
943			data   = CMSG_DATA(cm);
944			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
945
946			/* Adjust message length */
947			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
948			    datalen;
949
950
951			/* Copy cmsghdr */
952			copylen = sizeof(struct cmsghdr);
953			if (len < copylen) {
954				msg->msg_flags |= MSG_CTRUNC;
955				copylen = len;
956			}
957
958			error = copyout(cm,ctlbuf,copylen);
959			if (error)
960				goto exit;
961
962			ctlbuf += FREEBSD32_ALIGN(copylen);
963			len    -= FREEBSD32_ALIGN(copylen);
964
965			if (len <= 0)
966				break;
967
968			/* Copy data */
969			copylen = datalen;
970			if (len < copylen) {
971				msg->msg_flags |= MSG_CTRUNC;
972				copylen = len;
973			}
974
975			error = copyout(data,ctlbuf,copylen);
976			if (error)
977				goto exit;
978
979			ctlbuf += FREEBSD32_ALIGN(copylen);
980			len    -= FREEBSD32_ALIGN(copylen);
981
982			if (CMSG_SPACE(datalen) < clen) {
983				clen -= CMSG_SPACE(datalen);
984				cm = (struct cmsghdr *)
985					((caddr_t)cm + CMSG_SPACE(datalen));
986			} else {
987				clen = 0;
988				cm = NULL;
989			}
990		}
991		m = m->m_next;
992	}
993
994	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
995
996exit:
997	return (error);
998
999}
1000
1001int
1002freebsd32_recvmsg(td, uap)
1003	struct thread *td;
1004	struct freebsd32_recvmsg_args /* {
1005		int	s;
1006		struct	msghdr32 *msg;
1007		int	flags;
1008	} */ *uap;
1009{
1010	struct msghdr msg;
1011	struct msghdr32 m32;
1012	struct iovec *uiov, *iov;
1013	struct mbuf *control = NULL;
1014	struct mbuf **controlp;
1015
1016	int error;
1017	error = copyin(uap->msg, &m32, sizeof(m32));
1018	if (error)
1019		return (error);
1020	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1021	if (error)
1022		return (error);
1023	error = freebsd32_copyiniov((struct iovec32 *)(uintptr_t)m32.msg_iov,
1024	    m32.msg_iovlen, &iov, EMSGSIZE);
1025	if (error)
1026		return (error);
1027	msg.msg_flags = uap->flags;
1028	uiov = msg.msg_iov;
1029	msg.msg_iov = iov;
1030
1031	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1032	error = kern_recvit(td, uap->s, &msg, NULL, UIO_USERSPACE, controlp);
1033	if (error == 0) {
1034		msg.msg_iov = uiov;
1035
1036		if (control != NULL)
1037			error = freebsd32_copy_msg_out(&msg, control);
1038
1039		if (error == 0)
1040			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1041
1042		if (error == 0)
1043			error = freebsd32_copyoutiov(iov, m32.msg_iovlen,
1044			    (struct iovec32 *)(uintptr_t)m32.msg_iov, EMSGSIZE);
1045	}
1046	free(iov, M_IOV);
1047
1048	if (control != NULL)
1049		m_freem(control);
1050
1051	return (error);
1052}
1053
1054
1055static int
1056freebsd32_convert_msg_in(struct mbuf **controlp)
1057{
1058	struct mbuf *control = *controlp;
1059	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1060	void *data;
1061	socklen_t clen = control->m_len, datalen;
1062	int error;
1063
1064	error = 0;
1065	*controlp = NULL;
1066
1067	while (cm != NULL) {
1068		if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1069			error = EINVAL;
1070			break;
1071		}
1072
1073		data = FREEBSD32_CMSG_DATA(cm);
1074		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1075
1076		*controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1077		    cm->cmsg_level);
1078		controlp = &(*controlp)->m_next;
1079
1080		if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1081			clen -= FREEBSD32_CMSG_SPACE(datalen);
1082			cm = (struct cmsghdr *)
1083				((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1084		} else {
1085			clen = 0;
1086			cm = NULL;
1087		}
1088	}
1089
1090	m_freem(control);
1091	return (error);
1092}
1093
1094
1095int
1096freebsd32_sendmsg(struct thread *td,
1097		  struct freebsd32_sendmsg_args *uap)
1098{
1099	struct msghdr msg;
1100	struct msghdr32 m32;
1101	struct iovec *iov;
1102	struct mbuf *control = NULL;
1103	struct sockaddr *to = NULL;
1104	int error;
1105
1106	error = copyin(uap->msg, &m32, sizeof(m32));
1107	if (error)
1108		return (error);
1109	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1110	if (error)
1111		return (error);
1112	error = freebsd32_copyiniov((struct iovec32 *)(uintptr_t)m32.msg_iov,
1113	    m32.msg_iovlen, &iov, EMSGSIZE);
1114	if (error)
1115		return (error);
1116	msg.msg_iov = iov;
1117	if (msg.msg_name != NULL) {
1118		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1119		if (error) {
1120			to = NULL;
1121			goto out;
1122		}
1123		msg.msg_name = to;
1124	}
1125
1126	if (msg.msg_control) {
1127		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1128			error = EINVAL;
1129			goto out;
1130		}
1131
1132		error = sockargs(&control, msg.msg_control,
1133		    msg.msg_controllen, MT_CONTROL);
1134		if (error)
1135			goto out;
1136
1137		error = freebsd32_convert_msg_in(&control);
1138		if (error)
1139			goto out;
1140	}
1141
1142	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1143	    UIO_USERSPACE);
1144
1145out:
1146	free(iov, M_IOV);
1147	if (to)
1148		free(to, M_SONAME);
1149	return (error);
1150}
1151
1152int
1153freebsd32_recvfrom(struct thread *td,
1154		   struct freebsd32_recvfrom_args *uap)
1155{
1156	struct msghdr msg;
1157	struct iovec aiov;
1158	int error;
1159
1160	if (uap->fromlenaddr) {
1161		error = copyin((void *)(uintptr_t)uap->fromlenaddr,
1162		    &msg.msg_namelen, sizeof(msg.msg_namelen));
1163		if (error)
1164			return (error);
1165	} else {
1166		msg.msg_namelen = 0;
1167	}
1168
1169	msg.msg_name = (void *)(uintptr_t)uap->from;
1170	msg.msg_iov = &aiov;
1171	msg.msg_iovlen = 1;
1172	aiov.iov_base = (void *)(uintptr_t)uap->buf;
1173	aiov.iov_len = uap->len;
1174	msg.msg_control = 0;
1175	msg.msg_flags = uap->flags;
1176	error = kern_recvit(td, uap->s, &msg,
1177	    (void *)(uintptr_t)uap->fromlenaddr, UIO_USERSPACE, NULL);
1178	return (error);
1179}
1180
1181int
1182freebsd32_settimeofday(struct thread *td,
1183		       struct freebsd32_settimeofday_args *uap)
1184{
1185	struct timeval32 tv32;
1186	struct timeval tv, *tvp;
1187	struct timezone tz, *tzp;
1188	int error;
1189
1190	if (uap->tv) {
1191		error = copyin(uap->tv, &tv32, sizeof(tv32));
1192		if (error)
1193			return (error);
1194		CP(tv32, tv, tv_sec);
1195		CP(tv32, tv, tv_usec);
1196		tvp = &tv;
1197	} else
1198		tvp = NULL;
1199	if (uap->tzp) {
1200		error = copyin(uap->tzp, &tz, sizeof(tz));
1201		if (error)
1202			return (error);
1203		tzp = &tz;
1204	} else
1205		tzp = NULL;
1206	return (kern_settimeofday(td, tvp, tzp));
1207}
1208
1209int
1210freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1211{
1212	struct timeval32 s32[2];
1213	struct timeval s[2], *sp;
1214	int error;
1215
1216	if (uap->tptr != NULL) {
1217		error = copyin(uap->tptr, s32, sizeof(s32));
1218		if (error)
1219			return (error);
1220		CP(s32[0], s[0], tv_sec);
1221		CP(s32[0], s[0], tv_usec);
1222		CP(s32[1], s[1], tv_sec);
1223		CP(s32[1], s[1], tv_usec);
1224		sp = s;
1225	} else
1226		sp = NULL;
1227	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1228}
1229
1230int
1231freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1232{
1233	struct timeval32 s32[2];
1234	struct timeval s[2], *sp;
1235	int error;
1236
1237	if (uap->tptr != NULL) {
1238		error = copyin(uap->tptr, s32, sizeof(s32));
1239		if (error)
1240			return (error);
1241		CP(s32[0], s[0], tv_sec);
1242		CP(s32[0], s[0], tv_usec);
1243		CP(s32[1], s[1], tv_sec);
1244		CP(s32[1], s[1], tv_usec);
1245		sp = s;
1246	} else
1247		sp = NULL;
1248	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1249}
1250
1251int
1252freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1253{
1254	struct timeval32 s32[2];
1255	struct timeval s[2], *sp;
1256	int error;
1257
1258	if (uap->tptr != NULL) {
1259		error = copyin(uap->tptr, s32, sizeof(s32));
1260		if (error)
1261			return (error);
1262		CP(s32[0], s[0], tv_sec);
1263		CP(s32[0], s[0], tv_usec);
1264		CP(s32[1], s[1], tv_sec);
1265		CP(s32[1], s[1], tv_usec);
1266		sp = s;
1267	} else
1268		sp = NULL;
1269	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1270}
1271
1272
1273int
1274freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1275{
1276	struct timeval32 tv32;
1277	struct timeval delta, olddelta, *deltap;
1278	int error;
1279
1280	if (uap->delta) {
1281		error = copyin(uap->delta, &tv32, sizeof(tv32));
1282		if (error)
1283			return (error);
1284		CP(tv32, delta, tv_sec);
1285		CP(tv32, delta, tv_usec);
1286		deltap = &delta;
1287	} else
1288		deltap = NULL;
1289	error = kern_adjtime(td, deltap, &olddelta);
1290	if (uap->olddelta && error == 0) {
1291		CP(olddelta, tv32, tv_sec);
1292		CP(olddelta, tv32, tv_usec);
1293		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1294	}
1295	return (error);
1296}
1297
1298#ifdef COMPAT_FREEBSD4
1299int
1300freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1301{
1302	struct statfs32 s32;
1303	struct statfs s;
1304	int error;
1305
1306	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1307	if (error)
1308		return (error);
1309	copy_statfs(&s, &s32);
1310	return (copyout(&s32, uap->buf, sizeof(s32)));
1311}
1312#endif
1313
1314#ifdef COMPAT_FREEBSD4
1315int
1316freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1317{
1318	struct statfs32 s32;
1319	struct statfs s;
1320	int error;
1321
1322	error = kern_fstatfs(td, uap->fd, &s);
1323	if (error)
1324		return (error);
1325	copy_statfs(&s, &s32);
1326	return (copyout(&s32, uap->buf, sizeof(s32)));
1327}
1328#endif
1329
1330#ifdef COMPAT_FREEBSD4
1331int
1332freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1333{
1334	struct statfs32 s32;
1335	struct statfs s;
1336	fhandle_t fh;
1337	int error;
1338
1339	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1340		return (error);
1341	error = kern_fhstatfs(td, fh, &s);
1342	if (error)
1343		return (error);
1344	copy_statfs(&s, &s32);
1345	return (copyout(&s32, uap->buf, sizeof(s32)));
1346}
1347#endif
1348
1349int
1350freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
1351{
1352	/*
1353	 * Vector through to semsys if it is loaded.
1354	 */
1355	return sysent[SYS_semsys].sy_call(td, uap);
1356}
1357
1358int
1359freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
1360{
1361	/*
1362	 * Vector through to msgsys if it is loaded.
1363	 */
1364	return sysent[SYS_msgsys].sy_call(td, uap);
1365}
1366
1367int
1368freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
1369{
1370
1371	switch (uap->which) {
1372	case 0:	{	/* shmat */
1373		struct shmat_args ap;
1374
1375		ap.shmid = uap->a2;
1376		ap.shmaddr = PTRIN(uap->a3);
1377		ap.shmflg = uap->a4;
1378		return (sysent[SYS_shmat].sy_call(td, &ap));
1379	}
1380	case 2: {	/* shmdt */
1381		struct shmdt_args ap;
1382
1383		ap.shmaddr = PTRIN(uap->a2);
1384		return (sysent[SYS_shmdt].sy_call(td, &ap));
1385	}
1386	case 3: {	/* shmget */
1387		struct shmget_args ap;
1388
1389		ap.key = uap->a2;
1390		ap.size = uap->a3;
1391		ap.shmflg = uap->a4;
1392		return (sysent[SYS_shmget].sy_call(td, &ap));
1393	}
1394	case 4: {	/* shmctl */
1395		struct freebsd32_shmctl_args ap;
1396
1397		ap.shmid = uap->a2;
1398		ap.cmd = uap->a3;
1399		ap.buf = PTRIN(uap->a4);
1400		return (freebsd32_shmctl(td, &ap));
1401	}
1402	case 1:		/* oshmctl */
1403	default:
1404		return (EINVAL);
1405	}
1406}
1407
1408struct ipc_perm32 {
1409	uint16_t	cuid;
1410	uint16_t	cgid;
1411	uint16_t	uid;
1412	uint16_t	gid;
1413	uint16_t	mode;
1414	uint16_t	seq;
1415	uint32_t	key;
1416};
1417struct shmid_ds32 {
1418	struct ipc_perm32 shm_perm;
1419	int32_t		shm_segsz;
1420	int32_t		shm_lpid;
1421	int32_t		shm_cpid;
1422	int16_t		shm_nattch;
1423	int32_t		shm_atime;
1424	int32_t		shm_dtime;
1425	int32_t		shm_ctime;
1426	uint32_t	shm_internal;
1427};
1428struct shm_info32 {
1429	int32_t		used_ids;
1430	uint32_t	shm_tot;
1431	uint32_t	shm_rss;
1432	uint32_t	shm_swp;
1433	uint32_t	swap_attempts;
1434	uint32_t	swap_successes;
1435};
1436struct shminfo32 {
1437	uint32_t	shmmax;
1438	uint32_t	shmmin;
1439	uint32_t	shmmni;
1440	uint32_t	shmseg;
1441	uint32_t	shmall;
1442};
1443
1444int
1445freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
1446{
1447	int error = 0;
1448	union {
1449		struct shmid_ds shmid_ds;
1450		struct shm_info shm_info;
1451		struct shminfo shminfo;
1452	} u;
1453	union {
1454		struct shmid_ds32 shmid_ds32;
1455		struct shm_info32 shm_info32;
1456		struct shminfo32 shminfo32;
1457	} u32;
1458	size_t sz;
1459
1460	if (uap->cmd == IPC_SET) {
1461		if ((error = copyin(uap->buf, &u32.shmid_ds32,
1462		    sizeof(u32.shmid_ds32))))
1463			goto done;
1464		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.cuid);
1465		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.cgid);
1466		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.uid);
1467		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.gid);
1468		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.mode);
1469		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.seq);
1470		CP(u32.shmid_ds32, u.shmid_ds, shm_perm.key);
1471		CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
1472		CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
1473		CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
1474		CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
1475		CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
1476		CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
1477		CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
1478		PTRIN_CP(u32.shmid_ds32, u.shmid_ds, shm_internal);
1479	}
1480
1481	error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
1482	if (error)
1483		goto done;
1484
1485	/* Cases in which we need to copyout */
1486	switch (uap->cmd) {
1487	case IPC_INFO:
1488		CP(u.shminfo, u32.shminfo32, shmmax);
1489		CP(u.shminfo, u32.shminfo32, shmmin);
1490		CP(u.shminfo, u32.shminfo32, shmmni);
1491		CP(u.shminfo, u32.shminfo32, shmseg);
1492		CP(u.shminfo, u32.shminfo32, shmall);
1493		error = copyout(&u32.shminfo32, uap->buf,
1494		    sizeof(u32.shminfo32));
1495		break;
1496	case SHM_INFO:
1497		CP(u.shm_info, u32.shm_info32, used_ids);
1498		CP(u.shm_info, u32.shm_info32, shm_rss);
1499		CP(u.shm_info, u32.shm_info32, shm_tot);
1500		CP(u.shm_info, u32.shm_info32, shm_swp);
1501		CP(u.shm_info, u32.shm_info32, swap_attempts);
1502		CP(u.shm_info, u32.shm_info32, swap_successes);
1503		error = copyout(&u32.shm_info32, uap->buf,
1504		    sizeof(u32.shm_info32));
1505		break;
1506	case SHM_STAT:
1507	case IPC_STAT:
1508		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.cuid);
1509		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.cgid);
1510		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.uid);
1511		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.gid);
1512		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.mode);
1513		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.seq);
1514		CP(u.shmid_ds, u32.shmid_ds32, shm_perm.key);
1515		CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
1516		CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
1517		CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
1518		CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
1519		CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
1520		CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
1521		CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
1522		PTROUT_CP(u.shmid_ds, u32.shmid_ds32, shm_internal);
1523		error = copyout(&u32.shmid_ds32, uap->buf,
1524		    sizeof(u32.shmid_ds32));
1525		break;
1526	}
1527
1528done:
1529	if (error) {
1530		/* Invalidate the return value */
1531		td->td_retval[0] = -1;
1532	}
1533	return (error);
1534}
1535
1536int
1537freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1538{
1539	struct pread_args ap;
1540
1541	ap.fd = uap->fd;
1542	ap.buf = uap->buf;
1543	ap.nbyte = uap->nbyte;
1544	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1545	return (pread(td, &ap));
1546}
1547
1548int
1549freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1550{
1551	struct pwrite_args ap;
1552
1553	ap.fd = uap->fd;
1554	ap.buf = uap->buf;
1555	ap.nbyte = uap->nbyte;
1556	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1557	return (pwrite(td, &ap));
1558}
1559
1560int
1561freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1562{
1563	int error;
1564	struct lseek_args ap;
1565	off_t pos;
1566
1567	ap.fd = uap->fd;
1568	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1569	ap.whence = uap->whence;
1570	error = lseek(td, &ap);
1571	/* Expand the quad return into two parts for eax and edx */
1572	pos = *(off_t *)(td->td_retval);
1573	td->td_retval[0] = pos & 0xffffffff;	/* %eax */
1574	td->td_retval[1] = pos >> 32;		/* %edx */
1575	return error;
1576}
1577
1578int
1579freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1580{
1581	struct truncate_args ap;
1582
1583	ap.path = uap->path;
1584	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1585	return (truncate(td, &ap));
1586}
1587
1588int
1589freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1590{
1591	struct ftruncate_args ap;
1592
1593	ap.fd = uap->fd;
1594	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1595	return (ftruncate(td, &ap));
1596}
1597
1598struct sf_hdtr32 {
1599	uint32_t headers;
1600	int hdr_cnt;
1601	uint32_t trailers;
1602	int trl_cnt;
1603};
1604
1605static int
1606freebsd32_do_sendfile(struct thread *td,
1607    struct freebsd32_sendfile_args *uap, int compat)
1608{
1609	struct sendfile_args ap;
1610	struct sf_hdtr32 hdtr32;
1611	struct sf_hdtr hdtr;
1612	struct uio *hdr_uio, *trl_uio;
1613	struct iovec32 *iov32;
1614	int error;
1615
1616	hdr_uio = trl_uio = NULL;
1617
1618	ap.fd = uap->fd;
1619	ap.s = uap->s;
1620	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1621	ap.nbytes = uap->nbytes;
1622	ap.hdtr = (struct sf_hdtr *)uap->hdtr;		/* XXX not used */
1623	ap.sbytes = uap->sbytes;
1624	ap.flags = uap->flags;
1625
1626	if (uap->hdtr != NULL) {
1627		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1628		if (error)
1629			goto out;
1630		PTRIN_CP(hdtr32, hdtr, headers);
1631		CP(hdtr32, hdtr, hdr_cnt);
1632		PTRIN_CP(hdtr32, hdtr, trailers);
1633		CP(hdtr32, hdtr, trl_cnt);
1634
1635		if (hdtr.headers != NULL) {
1636			iov32 = (struct iovec32 *)(uintptr_t)hdtr32.headers;
1637			error = freebsd32_copyinuio(iov32,
1638			    hdtr32.hdr_cnt, &hdr_uio);
1639			if (error)
1640				goto out;
1641		}
1642		if (hdtr.trailers != NULL) {
1643			iov32 = (struct iovec32 *)(uintptr_t)hdtr32.trailers;
1644			error = freebsd32_copyinuio(iov32,
1645			    hdtr32.trl_cnt, &trl_uio);
1646			if (error)
1647				goto out;
1648		}
1649	}
1650
1651	error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1652out:
1653	if (hdr_uio)
1654		free(hdr_uio, M_IOV);
1655	if (trl_uio)
1656		free(trl_uio, M_IOV);
1657	return (error);
1658}
1659
1660#ifdef COMPAT_FREEBSD4
1661int
1662freebsd4_freebsd32_sendfile(struct thread *td,
1663    struct freebsd4_freebsd32_sendfile_args *uap)
1664{
1665	return (freebsd32_do_sendfile(td,
1666	    (struct freebsd32_sendfile_args *)uap, 1));
1667}
1668#endif
1669
1670int
1671freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1672{
1673
1674	return (freebsd32_do_sendfile(td, uap, 0));
1675}
1676
1677struct stat32 {
1678	dev_t	st_dev;
1679	ino_t	st_ino;
1680	mode_t	st_mode;
1681	nlink_t	st_nlink;
1682	uid_t	st_uid;
1683	gid_t	st_gid;
1684	dev_t	st_rdev;
1685	struct timespec32 st_atimespec;
1686	struct timespec32 st_mtimespec;
1687	struct timespec32 st_ctimespec;
1688	off_t	st_size;
1689	int64_t	st_blocks;
1690	u_int32_t st_blksize;
1691	u_int32_t st_flags;
1692	u_int32_t st_gen;
1693	struct timespec32 st_birthtimespec;
1694	unsigned int :(8 / 2) * (16 - (int)sizeof(struct timespec32));
1695	unsigned int :(8 / 2) * (16 - (int)sizeof(struct timespec32));
1696};
1697
1698
1699CTASSERT(sizeof(struct stat32) == 96);
1700
1701static void
1702copy_stat( struct stat *in, struct stat32 *out)
1703{
1704	CP(*in, *out, st_dev);
1705	CP(*in, *out, st_ino);
1706	CP(*in, *out, st_mode);
1707	CP(*in, *out, st_nlink);
1708	CP(*in, *out, st_uid);
1709	CP(*in, *out, st_gid);
1710	CP(*in, *out, st_rdev);
1711	TS_CP(*in, *out, st_atimespec);
1712	TS_CP(*in, *out, st_mtimespec);
1713	TS_CP(*in, *out, st_ctimespec);
1714	CP(*in, *out, st_size);
1715	CP(*in, *out, st_blocks);
1716	CP(*in, *out, st_blksize);
1717	CP(*in, *out, st_flags);
1718	CP(*in, *out, st_gen);
1719}
1720
1721int
1722freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1723{
1724	struct stat sb;
1725	struct stat32 sb32;
1726	int error;
1727
1728	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1729	if (error)
1730		return (error);
1731	copy_stat(&sb, &sb32);
1732	error = copyout(&sb32, uap->ub, sizeof (sb32));
1733	return (error);
1734}
1735
1736int
1737freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1738{
1739	struct stat ub;
1740	struct stat32 ub32;
1741	int error;
1742
1743	error = kern_fstat(td, uap->fd, &ub);
1744	if (error)
1745		return (error);
1746	copy_stat(&ub, &ub32);
1747	error = copyout(&ub32, uap->ub, sizeof(ub32));
1748	return (error);
1749}
1750
1751int
1752freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1753{
1754	struct stat sb;
1755	struct stat32 sb32;
1756	int error;
1757
1758	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1759	if (error)
1760		return (error);
1761	copy_stat(&sb, &sb32);
1762	error = copyout(&sb32, uap->ub, sizeof (sb32));
1763	return (error);
1764}
1765
1766/*
1767 * MPSAFE
1768 */
1769int
1770freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1771{
1772	int error, name[CTL_MAXNAME];
1773	size_t j, oldlen;
1774
1775	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1776		return (EINVAL);
1777 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1778 	if (error)
1779		return (error);
1780	mtx_lock(&Giant);
1781	if (uap->oldlenp)
1782		oldlen = fuword32(uap->oldlenp);
1783	else
1784		oldlen = 0;
1785	error = userland_sysctl(td, name, uap->namelen,
1786		uap->old, &oldlen, 1,
1787		uap->new, uap->newlen, &j, SCTL_MASK32);
1788	if (error && error != ENOMEM)
1789		goto done2;
1790	if (uap->oldlenp)
1791		suword32(uap->oldlenp, j);
1792done2:
1793	mtx_unlock(&Giant);
1794	return (error);
1795}
1796
1797struct sigaction32 {
1798	u_int32_t	sa_u;
1799	int		sa_flags;
1800	sigset_t	sa_mask;
1801};
1802
1803CTASSERT(sizeof(struct sigaction32) == 24);
1804
1805int
1806freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1807{
1808	struct sigaction32 s32;
1809	struct sigaction sa, osa, *sap;
1810	int error;
1811
1812	if (uap->act) {
1813		error = copyin(uap->act, &s32, sizeof(s32));
1814		if (error)
1815			return (error);
1816		sa.sa_handler = PTRIN(s32.sa_u);
1817		CP(s32, sa, sa_flags);
1818		CP(s32, sa, sa_mask);
1819		sap = &sa;
1820	} else
1821		sap = NULL;
1822	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1823	if (error == 0 && uap->oact != NULL) {
1824		s32.sa_u = PTROUT(osa.sa_handler);
1825		CP(osa, s32, sa_flags);
1826		CP(osa, s32, sa_mask);
1827		error = copyout(&s32, uap->oact, sizeof(s32));
1828	}
1829	return (error);
1830}
1831
1832#ifdef COMPAT_FREEBSD4
1833int
1834freebsd4_freebsd32_sigaction(struct thread *td,
1835			     struct freebsd4_freebsd32_sigaction_args *uap)
1836{
1837	struct sigaction32 s32;
1838	struct sigaction sa, osa, *sap;
1839	int error;
1840
1841	if (uap->act) {
1842		error = copyin(uap->act, &s32, sizeof(s32));
1843		if (error)
1844			return (error);
1845		sa.sa_handler = PTRIN(s32.sa_u);
1846		CP(s32, sa, sa_flags);
1847		CP(s32, sa, sa_mask);
1848		sap = &sa;
1849	} else
1850		sap = NULL;
1851	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
1852	if (error == 0 && uap->oact != NULL) {
1853		s32.sa_u = PTROUT(osa.sa_handler);
1854		CP(osa, s32, sa_flags);
1855		CP(osa, s32, sa_mask);
1856		error = copyout(&s32, uap->oact, sizeof(s32));
1857	}
1858	return (error);
1859}
1860#endif
1861
1862#ifdef COMPAT_43
1863struct osigaction32 {
1864	u_int32_t	sa_u;
1865	osigset_t	sa_mask;
1866	int		sa_flags;
1867};
1868
1869#define	ONSIG	32
1870
1871int
1872ofreebsd32_sigaction(struct thread *td,
1873			     struct ofreebsd32_sigaction_args *uap)
1874{
1875	struct osigaction32 s32;
1876	struct sigaction sa, osa, *sap;
1877	int error;
1878
1879	if (uap->signum <= 0 || uap->signum >= ONSIG)
1880		return (EINVAL);
1881
1882	if (uap->nsa) {
1883		error = copyin(uap->nsa, &s32, sizeof(s32));
1884		if (error)
1885			return (error);
1886		sa.sa_handler = PTRIN(s32.sa_u);
1887		CP(s32, sa, sa_flags);
1888		OSIG2SIG(s32.sa_mask, sa.sa_mask);
1889		sap = &sa;
1890	} else
1891		sap = NULL;
1892	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
1893	if (error == 0 && uap->osa != NULL) {
1894		s32.sa_u = PTROUT(osa.sa_handler);
1895		CP(osa, s32, sa_flags);
1896		SIG2OSIG(osa.sa_mask, s32.sa_mask);
1897		error = copyout(&s32, uap->osa, sizeof(s32));
1898	}
1899	return (error);
1900}
1901
1902int
1903ofreebsd32_sigprocmask(struct thread *td,
1904			       struct ofreebsd32_sigprocmask_args *uap)
1905{
1906	sigset_t set, oset;
1907	int error;
1908
1909	OSIG2SIG(uap->mask, set);
1910	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1911	SIG2OSIG(oset, td->td_retval[0]);
1912	return (error);
1913}
1914
1915int
1916ofreebsd32_sigpending(struct thread *td,
1917			      struct ofreebsd32_sigpending_args *uap)
1918{
1919	struct proc *p = td->td_proc;
1920	sigset_t siglist;
1921
1922	PROC_LOCK(p);
1923	siglist = p->p_siglist;
1924	SIGSETOR(siglist, td->td_siglist);
1925	PROC_UNLOCK(p);
1926	SIG2OSIG(siglist, td->td_retval[0]);
1927	return (0);
1928}
1929
1930struct sigvec32 {
1931	u_int32_t	sv_handler;
1932	int		sv_mask;
1933	int		sv_flags;
1934};
1935
1936int
1937ofreebsd32_sigvec(struct thread *td,
1938			  struct ofreebsd32_sigvec_args *uap)
1939{
1940	struct sigvec32 vec;
1941	struct sigaction sa, osa, *sap;
1942	int error;
1943
1944	if (uap->signum <= 0 || uap->signum >= ONSIG)
1945		return (EINVAL);
1946
1947	if (uap->nsv) {
1948		error = copyin(uap->nsv, &vec, sizeof(vec));
1949		if (error)
1950			return (error);
1951		sa.sa_handler = PTRIN(vec.sv_handler);
1952		OSIG2SIG(vec.sv_mask, sa.sa_mask);
1953		sa.sa_flags = vec.sv_flags;
1954		sa.sa_flags ^= SA_RESTART;
1955		sap = &sa;
1956	} else
1957		sap = NULL;
1958	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
1959	if (error == 0 && uap->osv != NULL) {
1960		vec.sv_handler = PTROUT(osa.sa_handler);
1961		SIG2OSIG(osa.sa_mask, vec.sv_mask);
1962		vec.sv_flags = osa.sa_flags;
1963		vec.sv_flags &= ~SA_NOCLDWAIT;
1964		vec.sv_flags ^= SA_RESTART;
1965		error = copyout(&vec, uap->osv, sizeof(vec));
1966	}
1967	return (error);
1968}
1969
1970int
1971ofreebsd32_sigblock(struct thread *td,
1972			    struct ofreebsd32_sigblock_args *uap)
1973{
1974	struct proc *p = td->td_proc;
1975	sigset_t set;
1976
1977	OSIG2SIG(uap->mask, set);
1978	SIG_CANTMASK(set);
1979	PROC_LOCK(p);
1980	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1981	SIGSETOR(td->td_sigmask, set);
1982	PROC_UNLOCK(p);
1983	return (0);
1984}
1985
1986int
1987ofreebsd32_sigsetmask(struct thread *td,
1988			      struct ofreebsd32_sigsetmask_args *uap)
1989{
1990	struct proc *p = td->td_proc;
1991	sigset_t set;
1992
1993	OSIG2SIG(uap->mask, set);
1994	SIG_CANTMASK(set);
1995	PROC_LOCK(p);
1996	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1997	SIGSETLO(td->td_sigmask, set);
1998	signotify(td);
1999	PROC_UNLOCK(p);
2000	return (0);
2001}
2002
2003int
2004ofreebsd32_sigsuspend(struct thread *td,
2005			      struct ofreebsd32_sigsuspend_args *uap)
2006{
2007	struct proc *p = td->td_proc;
2008	sigset_t mask;
2009
2010	PROC_LOCK(p);
2011	td->td_oldsigmask = td->td_sigmask;
2012	td->td_pflags |= TDP_OLDMASK;
2013	OSIG2SIG(uap->mask, mask);
2014	SIG_CANTMASK(mask);
2015	SIGSETLO(td->td_sigmask, mask);
2016	signotify(td);
2017	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
2018		/* void */;
2019	PROC_UNLOCK(p);
2020	/* always return EINTR rather than ERESTART... */
2021	return (EINTR);
2022}
2023
2024struct sigstack32 {
2025	u_int32_t	ss_sp;
2026	int		ss_onstack;
2027};
2028
2029int
2030ofreebsd32_sigstack(struct thread *td,
2031			    struct ofreebsd32_sigstack_args *uap)
2032{
2033	struct sigstack32 s32;
2034	struct sigstack nss, oss;
2035	int error = 0;
2036
2037	if (uap->nss != NULL) {
2038		error = copyin(uap->nss, &s32, sizeof(s32));
2039		if (error)
2040			return (error);
2041		nss.ss_sp = PTRIN(s32.ss_sp);
2042		CP(s32, nss, ss_onstack);
2043	}
2044	oss.ss_sp = td->td_sigstk.ss_sp;
2045	oss.ss_onstack = sigonstack(cpu_getstack(td));
2046	if (uap->nss != NULL) {
2047		td->td_sigstk.ss_sp = nss.ss_sp;
2048		td->td_sigstk.ss_size = 0;
2049		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
2050		td->td_pflags |= TDP_ALTSTACK;
2051	}
2052	if (uap->oss != NULL) {
2053		s32.ss_sp = PTROUT(oss.ss_sp);
2054		CP(oss, s32, ss_onstack);
2055		error = copyout(&s32, uap->oss, sizeof(s32));
2056	}
2057	return (error);
2058}
2059#endif
2060
2061int
2062freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2063{
2064	struct timespec32 rmt32, rqt32;
2065	struct timespec rmt, rqt;
2066	int error;
2067
2068	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2069	if (error)
2070		return (error);
2071
2072	CP(rqt32, rqt, tv_sec);
2073	CP(rqt32, rqt, tv_nsec);
2074
2075	if (uap->rmtp &&
2076	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2077		return (EFAULT);
2078	error = kern_nanosleep(td, &rqt, &rmt);
2079	if (error && uap->rmtp) {
2080		int error2;
2081
2082		CP(rmt, rmt32, tv_sec);
2083		CP(rmt, rmt32, tv_nsec);
2084
2085		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2086		if (error2)
2087			error = error2;
2088	}
2089	return (error);
2090}
2091
2092int
2093freebsd32_clock_gettime(struct thread *td,
2094			struct freebsd32_clock_gettime_args *uap)
2095{
2096	struct timespec	ats;
2097	struct timespec32 ats32;
2098	int error;
2099
2100	error = kern_clock_gettime(td, uap->clock_id, &ats);
2101	if (error == 0) {
2102		CP(ats, ats32, tv_sec);
2103		CP(ats, ats32, tv_nsec);
2104		error = copyout(&ats32, uap->tp, sizeof(ats32));
2105	}
2106	return (error);
2107}
2108
2109int
2110freebsd32_clock_settime(struct thread *td,
2111			struct freebsd32_clock_settime_args *uap)
2112{
2113	struct timespec	ats;
2114	struct timespec32 ats32;
2115	int error;
2116
2117	error = copyin(uap->tp, &ats32, sizeof(ats32));
2118	if (error)
2119		return (error);
2120	CP(ats32, ats, tv_sec);
2121	CP(ats32, ats, tv_nsec);
2122
2123	return (kern_clock_settime(td, uap->clock_id, &ats));
2124}
2125
2126int
2127freebsd32_clock_getres(struct thread *td,
2128		       struct freebsd32_clock_getres_args *uap)
2129{
2130	struct timespec	ts;
2131	struct timespec32 ts32;
2132	int error;
2133
2134	if (uap->tp == NULL)
2135		return (0);
2136	error = kern_clock_getres(td, uap->clock_id, &ts);
2137	if (error == 0) {
2138		CP(ts, ts32, tv_sec);
2139		CP(ts, ts32, tv_nsec);
2140		error = copyout(&ts32, uap->tp, sizeof(ts32));
2141	}
2142	return (error);
2143}
2144
2145#if 0
2146
2147int
2148freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2149{
2150	int error;
2151	struct yyy32 *p32, s32;
2152	struct yyy *p = NULL, s;
2153
2154	if (uap->zzz) {
2155		error = copyin(uap->zzz, &s32, sizeof(s32));
2156		if (error)
2157			return (error);
2158		/* translate in */
2159		p = &s;
2160	}
2161	error = kern_xxx(td, p);
2162	if (error)
2163		return (error);
2164	if (uap->zzz) {
2165		/* translate out */
2166		error = copyout(&s32, p32, sizeof(s32));
2167	}
2168	return (error);
2169}
2170
2171#endif
2172