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