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