freebsd32_misc.c revision 277610
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 277610 2015-01-23 21:07:08Z jilles $");
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_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1235	    sp, UIO_SYSSPACE));
1236}
1237
1238int
1239freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1240{
1241	struct timeval32 s32[2];
1242	struct timeval s[2], *sp;
1243	int error;
1244
1245	if (uap->tptr != NULL) {
1246		error = copyin(uap->tptr, s32, sizeof(s32));
1247		if (error)
1248			return (error);
1249		CP(s32[0], s[0], tv_sec);
1250		CP(s32[0], s[0], tv_usec);
1251		CP(s32[1], s[1], tv_sec);
1252		CP(s32[1], s[1], tv_usec);
1253		sp = s;
1254	} else
1255		sp = NULL;
1256	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1257}
1258
1259int
1260freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1261{
1262	struct timeval32 s32[2];
1263	struct timeval s[2], *sp;
1264	int error;
1265
1266	if (uap->tptr != NULL) {
1267		error = copyin(uap->tptr, s32, sizeof(s32));
1268		if (error)
1269			return (error);
1270		CP(s32[0], s[0], tv_sec);
1271		CP(s32[0], s[0], tv_usec);
1272		CP(s32[1], s[1], tv_sec);
1273		CP(s32[1], s[1], tv_usec);
1274		sp = s;
1275	} else
1276		sp = NULL;
1277	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1278}
1279
1280int
1281freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1282{
1283	struct timeval32 s32[2];
1284	struct timeval s[2], *sp;
1285	int error;
1286
1287	if (uap->times != NULL) {
1288		error = copyin(uap->times, s32, sizeof(s32));
1289		if (error)
1290			return (error);
1291		CP(s32[0], s[0], tv_sec);
1292		CP(s32[0], s[0], tv_usec);
1293		CP(s32[1], s[1], tv_sec);
1294		CP(s32[1], s[1], tv_usec);
1295		sp = s;
1296	} else
1297		sp = NULL;
1298	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1299		sp, UIO_SYSSPACE));
1300}
1301
1302int
1303freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1304{
1305	struct timespec32 ts32[2];
1306	struct timespec ts[2], *tsp;
1307	int error;
1308
1309	if (uap->times != NULL) {
1310		error = copyin(uap->times, ts32, sizeof(ts32));
1311		if (error)
1312			return (error);
1313		CP(ts32[0], ts[0], tv_sec);
1314		CP(ts32[0], ts[0], tv_nsec);
1315		CP(ts32[1], ts[1], tv_sec);
1316		CP(ts32[1], ts[1], tv_nsec);
1317		tsp = ts;
1318	} else
1319		tsp = NULL;
1320	return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1321}
1322
1323int
1324freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1325{
1326	struct timespec32 ts32[2];
1327	struct timespec ts[2], *tsp;
1328	int error;
1329
1330	if (uap->times != NULL) {
1331		error = copyin(uap->times, ts32, sizeof(ts32));
1332		if (error)
1333			return (error);
1334		CP(ts32[0], ts[0], tv_sec);
1335		CP(ts32[0], ts[0], tv_nsec);
1336		CP(ts32[1], ts[1], tv_sec);
1337		CP(ts32[1], ts[1], tv_nsec);
1338		tsp = ts;
1339	} else
1340		tsp = NULL;
1341	return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1342	    tsp, UIO_SYSSPACE, uap->flag));
1343}
1344
1345int
1346freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1347{
1348	struct timeval32 tv32;
1349	struct timeval delta, olddelta, *deltap;
1350	int error;
1351
1352	if (uap->delta) {
1353		error = copyin(uap->delta, &tv32, sizeof(tv32));
1354		if (error)
1355			return (error);
1356		CP(tv32, delta, tv_sec);
1357		CP(tv32, delta, tv_usec);
1358		deltap = &delta;
1359	} else
1360		deltap = NULL;
1361	error = kern_adjtime(td, deltap, &olddelta);
1362	if (uap->olddelta && error == 0) {
1363		CP(olddelta, tv32, tv_sec);
1364		CP(olddelta, tv32, tv_usec);
1365		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1366	}
1367	return (error);
1368}
1369
1370#ifdef COMPAT_FREEBSD4
1371int
1372freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1373{
1374	struct statfs32 s32;
1375	struct statfs s;
1376	int error;
1377
1378	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1379	if (error)
1380		return (error);
1381	copy_statfs(&s, &s32);
1382	return (copyout(&s32, uap->buf, sizeof(s32)));
1383}
1384#endif
1385
1386#ifdef COMPAT_FREEBSD4
1387int
1388freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1389{
1390	struct statfs32 s32;
1391	struct statfs s;
1392	int error;
1393
1394	error = kern_fstatfs(td, uap->fd, &s);
1395	if (error)
1396		return (error);
1397	copy_statfs(&s, &s32);
1398	return (copyout(&s32, uap->buf, sizeof(s32)));
1399}
1400#endif
1401
1402#ifdef COMPAT_FREEBSD4
1403int
1404freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1405{
1406	struct statfs32 s32;
1407	struct statfs s;
1408	fhandle_t fh;
1409	int error;
1410
1411	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1412		return (error);
1413	error = kern_fhstatfs(td, fh, &s);
1414	if (error)
1415		return (error);
1416	copy_statfs(&s, &s32);
1417	return (copyout(&s32, uap->buf, sizeof(s32)));
1418}
1419#endif
1420
1421int
1422freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1423{
1424	struct pread_args ap;
1425
1426	ap.fd = uap->fd;
1427	ap.buf = uap->buf;
1428	ap.nbyte = uap->nbyte;
1429	ap.offset = PAIR32TO64(off_t,uap->offset);
1430	return (sys_pread(td, &ap));
1431}
1432
1433int
1434freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1435{
1436	struct pwrite_args ap;
1437
1438	ap.fd = uap->fd;
1439	ap.buf = uap->buf;
1440	ap.nbyte = uap->nbyte;
1441	ap.offset = PAIR32TO64(off_t,uap->offset);
1442	return (sys_pwrite(td, &ap));
1443}
1444
1445#ifdef COMPAT_43
1446int
1447ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1448{
1449	struct lseek_args nuap;
1450
1451	nuap.fd = uap->fd;
1452	nuap.offset = uap->offset;
1453	nuap.whence = uap->whence;
1454	return (sys_lseek(td, &nuap));
1455}
1456#endif
1457
1458int
1459freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1460{
1461	int error;
1462	struct lseek_args ap;
1463	off_t pos;
1464
1465	ap.fd = uap->fd;
1466	ap.offset = PAIR32TO64(off_t,uap->offset);
1467	ap.whence = uap->whence;
1468	error = sys_lseek(td, &ap);
1469	/* Expand the quad return into two parts for eax and edx */
1470	pos = td->td_uretoff.tdu_off;
1471	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1472	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1473	return error;
1474}
1475
1476int
1477freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1478{
1479	struct truncate_args ap;
1480
1481	ap.path = uap->path;
1482	ap.length = PAIR32TO64(off_t,uap->length);
1483	return (sys_truncate(td, &ap));
1484}
1485
1486int
1487freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1488{
1489	struct ftruncate_args ap;
1490
1491	ap.fd = uap->fd;
1492	ap.length = PAIR32TO64(off_t,uap->length);
1493	return (sys_ftruncate(td, &ap));
1494}
1495
1496#ifdef COMPAT_43
1497int
1498ofreebsd32_getdirentries(struct thread *td,
1499    struct ofreebsd32_getdirentries_args *uap)
1500{
1501	struct ogetdirentries_args ap;
1502	int error;
1503	long loff;
1504	int32_t loff_cut;
1505
1506	ap.fd = uap->fd;
1507	ap.buf = uap->buf;
1508	ap.count = uap->count;
1509	ap.basep = NULL;
1510	error = kern_ogetdirentries(td, &ap, &loff);
1511	if (error == 0) {
1512		loff_cut = loff;
1513		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1514	}
1515	return (error);
1516}
1517#endif
1518
1519int
1520freebsd32_getdirentries(struct thread *td,
1521    struct freebsd32_getdirentries_args *uap)
1522{
1523	long base;
1524	int32_t base32;
1525	int error;
1526
1527	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1528	    NULL, UIO_USERSPACE);
1529	if (error)
1530		return (error);
1531	if (uap->basep != NULL) {
1532		base32 = base;
1533		error = copyout(&base32, uap->basep, sizeof(int32_t));
1534	}
1535	return (error);
1536}
1537
1538#ifdef COMPAT_FREEBSD6
1539/* versions with the 'int pad' argument */
1540int
1541freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1542{
1543	struct pread_args ap;
1544
1545	ap.fd = uap->fd;
1546	ap.buf = uap->buf;
1547	ap.nbyte = uap->nbyte;
1548	ap.offset = PAIR32TO64(off_t,uap->offset);
1549	return (sys_pread(td, &ap));
1550}
1551
1552int
1553freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1554{
1555	struct pwrite_args ap;
1556
1557	ap.fd = uap->fd;
1558	ap.buf = uap->buf;
1559	ap.nbyte = uap->nbyte;
1560	ap.offset = PAIR32TO64(off_t,uap->offset);
1561	return (sys_pwrite(td, &ap));
1562}
1563
1564int
1565freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1566{
1567	int error;
1568	struct lseek_args ap;
1569	off_t pos;
1570
1571	ap.fd = uap->fd;
1572	ap.offset = PAIR32TO64(off_t,uap->offset);
1573	ap.whence = uap->whence;
1574	error = sys_lseek(td, &ap);
1575	/* Expand the quad return into two parts for eax and edx */
1576	pos = *(off_t *)(td->td_retval);
1577	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1578	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1579	return error;
1580}
1581
1582int
1583freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1584{
1585	struct truncate_args ap;
1586
1587	ap.path = uap->path;
1588	ap.length = PAIR32TO64(off_t,uap->length);
1589	return (sys_truncate(td, &ap));
1590}
1591
1592int
1593freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1594{
1595	struct ftruncate_args ap;
1596
1597	ap.fd = uap->fd;
1598	ap.length = PAIR32TO64(off_t,uap->length);
1599	return (sys_ftruncate(td, &ap));
1600}
1601#endif /* COMPAT_FREEBSD6 */
1602
1603struct sf_hdtr32 {
1604	uint32_t headers;
1605	int hdr_cnt;
1606	uint32_t trailers;
1607	int trl_cnt;
1608};
1609
1610static int
1611freebsd32_do_sendfile(struct thread *td,
1612    struct freebsd32_sendfile_args *uap, int compat)
1613{
1614	struct sf_hdtr32 hdtr32;
1615	struct sf_hdtr hdtr;
1616	struct uio *hdr_uio, *trl_uio;
1617	struct file *fp;
1618	cap_rights_t rights;
1619	struct iovec32 *iov32;
1620	off_t offset, sbytes;
1621	int error;
1622
1623	offset = PAIR32TO64(off_t, uap->offset);
1624	if (offset < 0)
1625		return (EINVAL);
1626
1627	hdr_uio = trl_uio = NULL;
1628
1629	if (uap->hdtr != NULL) {
1630		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1631		if (error)
1632			goto out;
1633		PTRIN_CP(hdtr32, hdtr, headers);
1634		CP(hdtr32, hdtr, hdr_cnt);
1635		PTRIN_CP(hdtr32, hdtr, trailers);
1636		CP(hdtr32, hdtr, trl_cnt);
1637
1638		if (hdtr.headers != NULL) {
1639			iov32 = PTRIN(hdtr32.headers);
1640			error = freebsd32_copyinuio(iov32,
1641			    hdtr32.hdr_cnt, &hdr_uio);
1642			if (error)
1643				goto out;
1644		}
1645		if (hdtr.trailers != NULL) {
1646			iov32 = PTRIN(hdtr32.trailers);
1647			error = freebsd32_copyinuio(iov32,
1648			    hdtr32.trl_cnt, &trl_uio);
1649			if (error)
1650				goto out;
1651		}
1652	}
1653
1654	AUDIT_ARG_FD(uap->fd);
1655
1656	if ((error = fget_read(td, uap->fd,
1657	    cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1658		goto out;
1659
1660	error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1661	    uap->nbytes, &sbytes, uap->flags, compat ? SFK_COMPAT : 0, td);
1662	fdrop(fp, td);
1663
1664	if (uap->sbytes != NULL)
1665		copyout(&sbytes, uap->sbytes, sizeof(off_t));
1666
1667out:
1668	if (hdr_uio)
1669		free(hdr_uio, M_IOV);
1670	if (trl_uio)
1671		free(trl_uio, M_IOV);
1672	return (error);
1673}
1674
1675#ifdef COMPAT_FREEBSD4
1676int
1677freebsd4_freebsd32_sendfile(struct thread *td,
1678    struct freebsd4_freebsd32_sendfile_args *uap)
1679{
1680	return (freebsd32_do_sendfile(td,
1681	    (struct freebsd32_sendfile_args *)uap, 1));
1682}
1683#endif
1684
1685int
1686freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1687{
1688
1689	return (freebsd32_do_sendfile(td, uap, 0));
1690}
1691
1692static void
1693copy_stat(struct stat *in, struct stat32 *out)
1694{
1695
1696	CP(*in, *out, st_dev);
1697	CP(*in, *out, st_ino);
1698	CP(*in, *out, st_mode);
1699	CP(*in, *out, st_nlink);
1700	CP(*in, *out, st_uid);
1701	CP(*in, *out, st_gid);
1702	CP(*in, *out, st_rdev);
1703	TS_CP(*in, *out, st_atim);
1704	TS_CP(*in, *out, st_mtim);
1705	TS_CP(*in, *out, st_ctim);
1706	CP(*in, *out, st_size);
1707	CP(*in, *out, st_blocks);
1708	CP(*in, *out, st_blksize);
1709	CP(*in, *out, st_flags);
1710	CP(*in, *out, st_gen);
1711	TS_CP(*in, *out, st_birthtim);
1712}
1713
1714#ifdef COMPAT_43
1715static void
1716copy_ostat(struct stat *in, struct ostat32 *out)
1717{
1718
1719	CP(*in, *out, st_dev);
1720	CP(*in, *out, st_ino);
1721	CP(*in, *out, st_mode);
1722	CP(*in, *out, st_nlink);
1723	CP(*in, *out, st_uid);
1724	CP(*in, *out, st_gid);
1725	CP(*in, *out, st_rdev);
1726	CP(*in, *out, st_size);
1727	TS_CP(*in, *out, st_atim);
1728	TS_CP(*in, *out, st_mtim);
1729	TS_CP(*in, *out, st_ctim);
1730	CP(*in, *out, st_blksize);
1731	CP(*in, *out, st_blocks);
1732	CP(*in, *out, st_flags);
1733	CP(*in, *out, st_gen);
1734}
1735#endif
1736
1737int
1738freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1739{
1740	struct stat sb;
1741	struct stat32 sb32;
1742	int error;
1743
1744	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1745	    &sb, NULL);
1746	if (error)
1747		return (error);
1748	copy_stat(&sb, &sb32);
1749	error = copyout(&sb32, uap->ub, sizeof (sb32));
1750	return (error);
1751}
1752
1753#ifdef COMPAT_43
1754int
1755ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1756{
1757	struct stat sb;
1758	struct ostat32 sb32;
1759	int error;
1760
1761	error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1762	    &sb, NULL);
1763	if (error)
1764		return (error);
1765	copy_ostat(&sb, &sb32);
1766	error = copyout(&sb32, uap->ub, sizeof (sb32));
1767	return (error);
1768}
1769#endif
1770
1771int
1772freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1773{
1774	struct stat ub;
1775	struct stat32 ub32;
1776	int error;
1777
1778	error = kern_fstat(td, uap->fd, &ub);
1779	if (error)
1780		return (error);
1781	copy_stat(&ub, &ub32);
1782	error = copyout(&ub32, uap->ub, sizeof(ub32));
1783	return (error);
1784}
1785
1786#ifdef COMPAT_43
1787int
1788ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1789{
1790	struct stat ub;
1791	struct ostat32 ub32;
1792	int error;
1793
1794	error = kern_fstat(td, uap->fd, &ub);
1795	if (error)
1796		return (error);
1797	copy_ostat(&ub, &ub32);
1798	error = copyout(&ub32, uap->ub, sizeof(ub32));
1799	return (error);
1800}
1801#endif
1802
1803int
1804freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1805{
1806	struct stat ub;
1807	struct stat32 ub32;
1808	int error;
1809
1810	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1811	    &ub, NULL);
1812	if (error)
1813		return (error);
1814	copy_stat(&ub, &ub32);
1815	error = copyout(&ub32, uap->buf, sizeof(ub32));
1816	return (error);
1817}
1818
1819int
1820freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1821{
1822	struct stat sb;
1823	struct stat32 sb32;
1824	int error;
1825
1826	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1827	    UIO_USERSPACE, &sb, NULL);
1828	if (error)
1829		return (error);
1830	copy_stat(&sb, &sb32);
1831	error = copyout(&sb32, uap->ub, sizeof (sb32));
1832	return (error);
1833}
1834
1835#ifdef COMPAT_43
1836int
1837ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1838{
1839	struct stat sb;
1840	struct ostat32 sb32;
1841	int error;
1842
1843	error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1844	    UIO_USERSPACE, &sb, NULL);
1845	if (error)
1846		return (error);
1847	copy_ostat(&sb, &sb32);
1848	error = copyout(&sb32, uap->ub, sizeof (sb32));
1849	return (error);
1850}
1851#endif
1852
1853int
1854freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1855{
1856	int error, name[CTL_MAXNAME];
1857	size_t j, oldlen;
1858	uint32_t tmp;
1859
1860	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1861		return (EINVAL);
1862 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1863 	if (error)
1864		return (error);
1865	if (uap->oldlenp) {
1866		error = fueword32(uap->oldlenp, &tmp);
1867		oldlen = tmp;
1868	} else {
1869		oldlen = 0;
1870	}
1871	if (error != 0)
1872		return (EFAULT);
1873	error = userland_sysctl(td, name, uap->namelen,
1874		uap->old, &oldlen, 1,
1875		uap->new, uap->newlen, &j, SCTL_MASK32);
1876	if (error && error != ENOMEM)
1877		return (error);
1878	if (uap->oldlenp)
1879		suword32(uap->oldlenp, j);
1880	return (0);
1881}
1882
1883int
1884freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1885{
1886	uint32_t version;
1887	int error;
1888	struct jail j;
1889
1890	error = copyin(uap->jail, &version, sizeof(uint32_t));
1891	if (error)
1892		return (error);
1893
1894	switch (version) {
1895	case 0:
1896	{
1897		/* FreeBSD single IPv4 jails. */
1898		struct jail32_v0 j32_v0;
1899
1900		bzero(&j, sizeof(struct jail));
1901		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1902		if (error)
1903			return (error);
1904		CP(j32_v0, j, version);
1905		PTRIN_CP(j32_v0, j, path);
1906		PTRIN_CP(j32_v0, j, hostname);
1907		j.ip4s = htonl(j32_v0.ip_number);	/* jail_v0 is host order */
1908		break;
1909	}
1910
1911	case 1:
1912		/*
1913		 * Version 1 was used by multi-IPv4 jail implementations
1914		 * that never made it into the official kernel.
1915		 */
1916		return (EINVAL);
1917
1918	case 2:	/* JAIL_API_VERSION */
1919	{
1920		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
1921		struct jail32 j32;
1922
1923		error = copyin(uap->jail, &j32, sizeof(struct jail32));
1924		if (error)
1925			return (error);
1926		CP(j32, j, version);
1927		PTRIN_CP(j32, j, path);
1928		PTRIN_CP(j32, j, hostname);
1929		PTRIN_CP(j32, j, jailname);
1930		CP(j32, j, ip4s);
1931		CP(j32, j, ip6s);
1932		PTRIN_CP(j32, j, ip4);
1933		PTRIN_CP(j32, j, ip6);
1934		break;
1935	}
1936
1937	default:
1938		/* Sci-Fi jails are not supported, sorry. */
1939		return (EINVAL);
1940	}
1941	return (kern_jail(td, &j));
1942}
1943
1944int
1945freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1946{
1947	struct uio *auio;
1948	int error;
1949
1950	/* Check that we have an even number of iovecs. */
1951	if (uap->iovcnt & 1)
1952		return (EINVAL);
1953
1954	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1955	if (error)
1956		return (error);
1957	error = kern_jail_set(td, auio, uap->flags);
1958	free(auio, M_IOV);
1959	return (error);
1960}
1961
1962int
1963freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1964{
1965	struct iovec32 iov32;
1966	struct uio *auio;
1967	int error, i;
1968
1969	/* Check that we have an even number of iovecs. */
1970	if (uap->iovcnt & 1)
1971		return (EINVAL);
1972
1973	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1974	if (error)
1975		return (error);
1976	error = kern_jail_get(td, auio, uap->flags);
1977	if (error == 0)
1978		for (i = 0; i < uap->iovcnt; i++) {
1979			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1980			CP(auio->uio_iov[i], iov32, iov_len);
1981			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1982			if (error != 0)
1983				break;
1984		}
1985	free(auio, M_IOV);
1986	return (error);
1987}
1988
1989int
1990freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1991{
1992	struct sigaction32 s32;
1993	struct sigaction sa, osa, *sap;
1994	int error;
1995
1996	if (uap->act) {
1997		error = copyin(uap->act, &s32, sizeof(s32));
1998		if (error)
1999			return (error);
2000		sa.sa_handler = PTRIN(s32.sa_u);
2001		CP(s32, sa, sa_flags);
2002		CP(s32, sa, sa_mask);
2003		sap = &sa;
2004	} else
2005		sap = NULL;
2006	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2007	if (error == 0 && uap->oact != NULL) {
2008		s32.sa_u = PTROUT(osa.sa_handler);
2009		CP(osa, s32, sa_flags);
2010		CP(osa, s32, sa_mask);
2011		error = copyout(&s32, uap->oact, sizeof(s32));
2012	}
2013	return (error);
2014}
2015
2016#ifdef COMPAT_FREEBSD4
2017int
2018freebsd4_freebsd32_sigaction(struct thread *td,
2019			     struct freebsd4_freebsd32_sigaction_args *uap)
2020{
2021	struct sigaction32 s32;
2022	struct sigaction sa, osa, *sap;
2023	int error;
2024
2025	if (uap->act) {
2026		error = copyin(uap->act, &s32, sizeof(s32));
2027		if (error)
2028			return (error);
2029		sa.sa_handler = PTRIN(s32.sa_u);
2030		CP(s32, sa, sa_flags);
2031		CP(s32, sa, sa_mask);
2032		sap = &sa;
2033	} else
2034		sap = NULL;
2035	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2036	if (error == 0 && uap->oact != NULL) {
2037		s32.sa_u = PTROUT(osa.sa_handler);
2038		CP(osa, s32, sa_flags);
2039		CP(osa, s32, sa_mask);
2040		error = copyout(&s32, uap->oact, sizeof(s32));
2041	}
2042	return (error);
2043}
2044#endif
2045
2046#ifdef COMPAT_43
2047struct osigaction32 {
2048	u_int32_t	sa_u;
2049	osigset_t	sa_mask;
2050	int		sa_flags;
2051};
2052
2053#define	ONSIG	32
2054
2055int
2056ofreebsd32_sigaction(struct thread *td,
2057			     struct ofreebsd32_sigaction_args *uap)
2058{
2059	struct osigaction32 s32;
2060	struct sigaction sa, osa, *sap;
2061	int error;
2062
2063	if (uap->signum <= 0 || uap->signum >= ONSIG)
2064		return (EINVAL);
2065
2066	if (uap->nsa) {
2067		error = copyin(uap->nsa, &s32, sizeof(s32));
2068		if (error)
2069			return (error);
2070		sa.sa_handler = PTRIN(s32.sa_u);
2071		CP(s32, sa, sa_flags);
2072		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2073		sap = &sa;
2074	} else
2075		sap = NULL;
2076	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2077	if (error == 0 && uap->osa != NULL) {
2078		s32.sa_u = PTROUT(osa.sa_handler);
2079		CP(osa, s32, sa_flags);
2080		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2081		error = copyout(&s32, uap->osa, sizeof(s32));
2082	}
2083	return (error);
2084}
2085
2086int
2087ofreebsd32_sigprocmask(struct thread *td,
2088			       struct ofreebsd32_sigprocmask_args *uap)
2089{
2090	sigset_t set, oset;
2091	int error;
2092
2093	OSIG2SIG(uap->mask, set);
2094	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2095	SIG2OSIG(oset, td->td_retval[0]);
2096	return (error);
2097}
2098
2099int
2100ofreebsd32_sigpending(struct thread *td,
2101			      struct ofreebsd32_sigpending_args *uap)
2102{
2103	struct proc *p = td->td_proc;
2104	sigset_t siglist;
2105
2106	PROC_LOCK(p);
2107	siglist = p->p_siglist;
2108	SIGSETOR(siglist, td->td_siglist);
2109	PROC_UNLOCK(p);
2110	SIG2OSIG(siglist, td->td_retval[0]);
2111	return (0);
2112}
2113
2114struct sigvec32 {
2115	u_int32_t	sv_handler;
2116	int		sv_mask;
2117	int		sv_flags;
2118};
2119
2120int
2121ofreebsd32_sigvec(struct thread *td,
2122			  struct ofreebsd32_sigvec_args *uap)
2123{
2124	struct sigvec32 vec;
2125	struct sigaction sa, osa, *sap;
2126	int error;
2127
2128	if (uap->signum <= 0 || uap->signum >= ONSIG)
2129		return (EINVAL);
2130
2131	if (uap->nsv) {
2132		error = copyin(uap->nsv, &vec, sizeof(vec));
2133		if (error)
2134			return (error);
2135		sa.sa_handler = PTRIN(vec.sv_handler);
2136		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2137		sa.sa_flags = vec.sv_flags;
2138		sa.sa_flags ^= SA_RESTART;
2139		sap = &sa;
2140	} else
2141		sap = NULL;
2142	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2143	if (error == 0 && uap->osv != NULL) {
2144		vec.sv_handler = PTROUT(osa.sa_handler);
2145		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2146		vec.sv_flags = osa.sa_flags;
2147		vec.sv_flags &= ~SA_NOCLDWAIT;
2148		vec.sv_flags ^= SA_RESTART;
2149		error = copyout(&vec, uap->osv, sizeof(vec));
2150	}
2151	return (error);
2152}
2153
2154int
2155ofreebsd32_sigblock(struct thread *td,
2156			    struct ofreebsd32_sigblock_args *uap)
2157{
2158	sigset_t set, oset;
2159
2160	OSIG2SIG(uap->mask, set);
2161	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2162	SIG2OSIG(oset, td->td_retval[0]);
2163	return (0);
2164}
2165
2166int
2167ofreebsd32_sigsetmask(struct thread *td,
2168			      struct ofreebsd32_sigsetmask_args *uap)
2169{
2170	sigset_t set, oset;
2171
2172	OSIG2SIG(uap->mask, set);
2173	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2174	SIG2OSIG(oset, td->td_retval[0]);
2175	return (0);
2176}
2177
2178int
2179ofreebsd32_sigsuspend(struct thread *td,
2180			      struct ofreebsd32_sigsuspend_args *uap)
2181{
2182	sigset_t mask;
2183
2184	OSIG2SIG(uap->mask, mask);
2185	return (kern_sigsuspend(td, mask));
2186}
2187
2188struct sigstack32 {
2189	u_int32_t	ss_sp;
2190	int		ss_onstack;
2191};
2192
2193int
2194ofreebsd32_sigstack(struct thread *td,
2195			    struct ofreebsd32_sigstack_args *uap)
2196{
2197	struct sigstack32 s32;
2198	struct sigstack nss, oss;
2199	int error = 0, unss;
2200
2201	if (uap->nss != NULL) {
2202		error = copyin(uap->nss, &s32, sizeof(s32));
2203		if (error)
2204			return (error);
2205		nss.ss_sp = PTRIN(s32.ss_sp);
2206		CP(s32, nss, ss_onstack);
2207		unss = 1;
2208	} else {
2209		unss = 0;
2210	}
2211	oss.ss_sp = td->td_sigstk.ss_sp;
2212	oss.ss_onstack = sigonstack(cpu_getstack(td));
2213	if (unss) {
2214		td->td_sigstk.ss_sp = nss.ss_sp;
2215		td->td_sigstk.ss_size = 0;
2216		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2217		td->td_pflags |= TDP_ALTSTACK;
2218	}
2219	if (uap->oss != NULL) {
2220		s32.ss_sp = PTROUT(oss.ss_sp);
2221		CP(oss, s32, ss_onstack);
2222		error = copyout(&s32, uap->oss, sizeof(s32));
2223	}
2224	return (error);
2225}
2226#endif
2227
2228int
2229freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2230{
2231	struct timespec32 rmt32, rqt32;
2232	struct timespec rmt, rqt;
2233	int error;
2234
2235	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2236	if (error)
2237		return (error);
2238
2239	CP(rqt32, rqt, tv_sec);
2240	CP(rqt32, rqt, tv_nsec);
2241
2242	if (uap->rmtp &&
2243	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2244		return (EFAULT);
2245	error = kern_nanosleep(td, &rqt, &rmt);
2246	if (error && uap->rmtp) {
2247		int error2;
2248
2249		CP(rmt, rmt32, tv_sec);
2250		CP(rmt, rmt32, tv_nsec);
2251
2252		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2253		if (error2)
2254			error = error2;
2255	}
2256	return (error);
2257}
2258
2259int
2260freebsd32_clock_gettime(struct thread *td,
2261			struct freebsd32_clock_gettime_args *uap)
2262{
2263	struct timespec	ats;
2264	struct timespec32 ats32;
2265	int error;
2266
2267	error = kern_clock_gettime(td, uap->clock_id, &ats);
2268	if (error == 0) {
2269		CP(ats, ats32, tv_sec);
2270		CP(ats, ats32, tv_nsec);
2271		error = copyout(&ats32, uap->tp, sizeof(ats32));
2272	}
2273	return (error);
2274}
2275
2276int
2277freebsd32_clock_settime(struct thread *td,
2278			struct freebsd32_clock_settime_args *uap)
2279{
2280	struct timespec	ats;
2281	struct timespec32 ats32;
2282	int error;
2283
2284	error = copyin(uap->tp, &ats32, sizeof(ats32));
2285	if (error)
2286		return (error);
2287	CP(ats32, ats, tv_sec);
2288	CP(ats32, ats, tv_nsec);
2289
2290	return (kern_clock_settime(td, uap->clock_id, &ats));
2291}
2292
2293int
2294freebsd32_clock_getres(struct thread *td,
2295		       struct freebsd32_clock_getres_args *uap)
2296{
2297	struct timespec	ts;
2298	struct timespec32 ts32;
2299	int error;
2300
2301	if (uap->tp == NULL)
2302		return (0);
2303	error = kern_clock_getres(td, uap->clock_id, &ts);
2304	if (error == 0) {
2305		CP(ts, ts32, tv_sec);
2306		CP(ts, ts32, tv_nsec);
2307		error = copyout(&ts32, uap->tp, sizeof(ts32));
2308	}
2309	return (error);
2310}
2311
2312int freebsd32_ktimer_create(struct thread *td,
2313    struct freebsd32_ktimer_create_args *uap)
2314{
2315	struct sigevent32 ev32;
2316	struct sigevent ev, *evp;
2317	int error, id;
2318
2319	if (uap->evp == NULL) {
2320		evp = NULL;
2321	} else {
2322		evp = &ev;
2323		error = copyin(uap->evp, &ev32, sizeof(ev32));
2324		if (error != 0)
2325			return (error);
2326		error = convert_sigevent32(&ev32, &ev);
2327		if (error != 0)
2328			return (error);
2329	}
2330	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2331	if (error == 0) {
2332		error = copyout(&id, uap->timerid, sizeof(int));
2333		if (error != 0)
2334			kern_ktimer_delete(td, id);
2335	}
2336	return (error);
2337}
2338
2339int
2340freebsd32_ktimer_settime(struct thread *td,
2341    struct freebsd32_ktimer_settime_args *uap)
2342{
2343	struct itimerspec32 val32, oval32;
2344	struct itimerspec val, oval, *ovalp;
2345	int error;
2346
2347	error = copyin(uap->value, &val32, sizeof(val32));
2348	if (error != 0)
2349		return (error);
2350	ITS_CP(val32, val);
2351	ovalp = uap->ovalue != NULL ? &oval : NULL;
2352	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2353	if (error == 0 && uap->ovalue != NULL) {
2354		ITS_CP(oval, oval32);
2355		error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2356	}
2357	return (error);
2358}
2359
2360int
2361freebsd32_ktimer_gettime(struct thread *td,
2362    struct freebsd32_ktimer_gettime_args *uap)
2363{
2364	struct itimerspec32 val32;
2365	struct itimerspec val;
2366	int error;
2367
2368	error = kern_ktimer_gettime(td, uap->timerid, &val);
2369	if (error == 0) {
2370		ITS_CP(val, val32);
2371		error = copyout(&val32, uap->value, sizeof(val32));
2372	}
2373	return (error);
2374}
2375
2376int
2377freebsd32_clock_getcpuclockid2(struct thread *td,
2378    struct freebsd32_clock_getcpuclockid2_args *uap)
2379{
2380	clockid_t clk_id;
2381	int error;
2382
2383	error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2384	    uap->which, &clk_id);
2385	if (error == 0)
2386		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2387	return (error);
2388}
2389
2390int
2391freebsd32_thr_new(struct thread *td,
2392		  struct freebsd32_thr_new_args *uap)
2393{
2394	struct thr_param32 param32;
2395	struct thr_param param;
2396	int error;
2397
2398	if (uap->param_size < 0 ||
2399	    uap->param_size > sizeof(struct thr_param32))
2400		return (EINVAL);
2401	bzero(&param, sizeof(struct thr_param));
2402	bzero(&param32, sizeof(struct thr_param32));
2403	error = copyin(uap->param, &param32, uap->param_size);
2404	if (error != 0)
2405		return (error);
2406	param.start_func = PTRIN(param32.start_func);
2407	param.arg = PTRIN(param32.arg);
2408	param.stack_base = PTRIN(param32.stack_base);
2409	param.stack_size = param32.stack_size;
2410	param.tls_base = PTRIN(param32.tls_base);
2411	param.tls_size = param32.tls_size;
2412	param.child_tid = PTRIN(param32.child_tid);
2413	param.parent_tid = PTRIN(param32.parent_tid);
2414	param.flags = param32.flags;
2415	param.rtp = PTRIN(param32.rtp);
2416	param.spare[0] = PTRIN(param32.spare[0]);
2417	param.spare[1] = PTRIN(param32.spare[1]);
2418	param.spare[2] = PTRIN(param32.spare[2]);
2419
2420	return (kern_thr_new(td, &param));
2421}
2422
2423int
2424freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2425{
2426	struct timespec32 ts32;
2427	struct timespec ts, *tsp;
2428	int error;
2429
2430	error = 0;
2431	tsp = NULL;
2432	if (uap->timeout != NULL) {
2433		error = copyin((const void *)uap->timeout, (void *)&ts32,
2434		    sizeof(struct timespec32));
2435		if (error != 0)
2436			return (error);
2437		ts.tv_sec = ts32.tv_sec;
2438		ts.tv_nsec = ts32.tv_nsec;
2439		tsp = &ts;
2440	}
2441	return (kern_thr_suspend(td, tsp));
2442}
2443
2444void
2445siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2446{
2447	bzero(dst, sizeof(*dst));
2448	dst->si_signo = src->si_signo;
2449	dst->si_errno = src->si_errno;
2450	dst->si_code = src->si_code;
2451	dst->si_pid = src->si_pid;
2452	dst->si_uid = src->si_uid;
2453	dst->si_status = src->si_status;
2454	dst->si_addr = (uintptr_t)src->si_addr;
2455	dst->si_value.sival_int = src->si_value.sival_int;
2456	dst->si_timerid = src->si_timerid;
2457	dst->si_overrun = src->si_overrun;
2458}
2459
2460int
2461freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2462{
2463	struct timespec32 ts32;
2464	struct timespec ts;
2465	struct timespec *timeout;
2466	sigset_t set;
2467	ksiginfo_t ksi;
2468	struct siginfo32 si32;
2469	int error;
2470
2471	if (uap->timeout) {
2472		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2473		if (error)
2474			return (error);
2475		ts.tv_sec = ts32.tv_sec;
2476		ts.tv_nsec = ts32.tv_nsec;
2477		timeout = &ts;
2478	} else
2479		timeout = NULL;
2480
2481	error = copyin(uap->set, &set, sizeof(set));
2482	if (error)
2483		return (error);
2484
2485	error = kern_sigtimedwait(td, set, &ksi, timeout);
2486	if (error)
2487		return (error);
2488
2489	if (uap->info) {
2490		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2491		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2492	}
2493
2494	if (error == 0)
2495		td->td_retval[0] = ksi.ksi_signo;
2496	return (error);
2497}
2498
2499/*
2500 * MPSAFE
2501 */
2502int
2503freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2504{
2505	ksiginfo_t ksi;
2506	struct siginfo32 si32;
2507	sigset_t set;
2508	int error;
2509
2510	error = copyin(uap->set, &set, sizeof(set));
2511	if (error)
2512		return (error);
2513
2514	error = kern_sigtimedwait(td, set, &ksi, NULL);
2515	if (error)
2516		return (error);
2517
2518	if (uap->info) {
2519		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2520		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2521	}
2522	if (error == 0)
2523		td->td_retval[0] = ksi.ksi_signo;
2524	return (error);
2525}
2526
2527int
2528freebsd32_cpuset_setid(struct thread *td,
2529    struct freebsd32_cpuset_setid_args *uap)
2530{
2531	struct cpuset_setid_args ap;
2532
2533	ap.which = uap->which;
2534	ap.id = PAIR32TO64(id_t,uap->id);
2535	ap.setid = uap->setid;
2536
2537	return (sys_cpuset_setid(td, &ap));
2538}
2539
2540int
2541freebsd32_cpuset_getid(struct thread *td,
2542    struct freebsd32_cpuset_getid_args *uap)
2543{
2544	struct cpuset_getid_args ap;
2545
2546	ap.level = uap->level;
2547	ap.which = uap->which;
2548	ap.id = PAIR32TO64(id_t,uap->id);
2549	ap.setid = uap->setid;
2550
2551	return (sys_cpuset_getid(td, &ap));
2552}
2553
2554int
2555freebsd32_cpuset_getaffinity(struct thread *td,
2556    struct freebsd32_cpuset_getaffinity_args *uap)
2557{
2558	struct cpuset_getaffinity_args ap;
2559
2560	ap.level = uap->level;
2561	ap.which = uap->which;
2562	ap.id = PAIR32TO64(id_t,uap->id);
2563	ap.cpusetsize = uap->cpusetsize;
2564	ap.mask = uap->mask;
2565
2566	return (sys_cpuset_getaffinity(td, &ap));
2567}
2568
2569int
2570freebsd32_cpuset_setaffinity(struct thread *td,
2571    struct freebsd32_cpuset_setaffinity_args *uap)
2572{
2573	struct cpuset_setaffinity_args ap;
2574
2575	ap.level = uap->level;
2576	ap.which = uap->which;
2577	ap.id = PAIR32TO64(id_t,uap->id);
2578	ap.cpusetsize = uap->cpusetsize;
2579	ap.mask = uap->mask;
2580
2581	return (sys_cpuset_setaffinity(td, &ap));
2582}
2583
2584int
2585freebsd32_nmount(struct thread *td,
2586    struct freebsd32_nmount_args /* {
2587    	struct iovec *iovp;
2588    	unsigned int iovcnt;
2589    	int flags;
2590    } */ *uap)
2591{
2592	struct uio *auio;
2593	uint64_t flags;
2594	int error;
2595
2596	/*
2597	 * Mount flags are now 64-bits. On 32-bit archtectures only
2598	 * 32-bits are passed in, but from here on everything handles
2599	 * 64-bit flags correctly.
2600	 */
2601	flags = uap->flags;
2602
2603	AUDIT_ARG_FFLAGS(flags);
2604
2605	/*
2606	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2607	 * userspace to set this flag, but we must filter it out if we want
2608	 * MNT_UPDATE on the root file system to work.
2609	 * MNT_ROOTFS should only be set by the kernel when mounting its
2610	 * root file system.
2611	 */
2612	flags &= ~MNT_ROOTFS;
2613
2614	/*
2615	 * check that we have an even number of iovec's
2616	 * and that we have at least two options.
2617	 */
2618	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2619		return (EINVAL);
2620
2621	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2622	if (error)
2623		return (error);
2624	error = vfs_donmount(td, flags, auio);
2625
2626	free(auio, M_IOV);
2627	return error;
2628}
2629
2630#if 0
2631int
2632freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2633{
2634	struct yyy32 *p32, s32;
2635	struct yyy *p = NULL, s;
2636	struct xxx_arg ap;
2637	int error;
2638
2639	if (uap->zzz) {
2640		error = copyin(uap->zzz, &s32, sizeof(s32));
2641		if (error)
2642			return (error);
2643		/* translate in */
2644		p = &s;
2645	}
2646	error = kern_xxx(td, p);
2647	if (error)
2648		return (error);
2649	if (uap->zzz) {
2650		/* translate out */
2651		error = copyout(&s32, p32, sizeof(s32));
2652	}
2653	return (error);
2654}
2655#endif
2656
2657int
2658syscall32_register(int *offset, struct sysent *new_sysent,
2659    struct sysent *old_sysent, int flags)
2660{
2661
2662	if ((flags & ~SY_THR_STATIC) != 0)
2663		return (EINVAL);
2664
2665	if (*offset == NO_SYSCALL) {
2666		int i;
2667
2668		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2669			if (freebsd32_sysent[i].sy_call ==
2670			    (sy_call_t *)lkmnosys)
2671				break;
2672		if (i == SYS_MAXSYSCALL)
2673			return (ENFILE);
2674		*offset = i;
2675	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2676		return (EINVAL);
2677	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2678	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2679		return (EEXIST);
2680
2681	*old_sysent = freebsd32_sysent[*offset];
2682	freebsd32_sysent[*offset] = *new_sysent;
2683	atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2684	return (0);
2685}
2686
2687int
2688syscall32_deregister(int *offset, struct sysent *old_sysent)
2689{
2690
2691	if (*offset == 0)
2692		return (0);
2693
2694	freebsd32_sysent[*offset] = *old_sysent;
2695	return (0);
2696}
2697
2698int
2699syscall32_module_handler(struct module *mod, int what, void *arg)
2700{
2701	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2702	modspecific_t ms;
2703	int error;
2704
2705	switch (what) {
2706	case MOD_LOAD:
2707		error = syscall32_register(data->offset, data->new_sysent,
2708		    &data->old_sysent, SY_THR_STATIC_KLD);
2709		if (error) {
2710			/* Leave a mark so we know to safely unload below. */
2711			data->offset = NULL;
2712			return error;
2713		}
2714		ms.intval = *data->offset;
2715		MOD_XLOCK;
2716		module_setspecific(mod, &ms);
2717		MOD_XUNLOCK;
2718		if (data->chainevh)
2719			error = data->chainevh(mod, what, data->chainarg);
2720		return (error);
2721	case MOD_UNLOAD:
2722		/*
2723		 * MOD_LOAD failed, so just return without calling the
2724		 * chained handler since we didn't pass along the MOD_LOAD
2725		 * event.
2726		 */
2727		if (data->offset == NULL)
2728			return (0);
2729		if (data->chainevh) {
2730			error = data->chainevh(mod, what, data->chainarg);
2731			if (error)
2732				return (error);
2733		}
2734		error = syscall32_deregister(data->offset, &data->old_sysent);
2735		return (error);
2736	default:
2737		error = EOPNOTSUPP;
2738		if (data->chainevh)
2739			error = data->chainevh(mod, what, data->chainarg);
2740		return (error);
2741	}
2742}
2743
2744int
2745syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2746{
2747	struct syscall_helper_data *sd1;
2748	int error;
2749
2750	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2751		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2752		    &sd1->old_sysent, flags);
2753		if (error != 0) {
2754			syscall32_helper_unregister(sd);
2755			return (error);
2756		}
2757		sd1->registered = 1;
2758	}
2759	return (0);
2760}
2761
2762int
2763syscall32_helper_unregister(struct syscall_helper_data *sd)
2764{
2765	struct syscall_helper_data *sd1;
2766
2767	for (sd1 = sd; sd1->registered != 0; sd1++) {
2768		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2769		sd1->registered = 0;
2770	}
2771	return (0);
2772}
2773
2774register_t *
2775freebsd32_copyout_strings(struct image_params *imgp)
2776{
2777	int argc, envc, i;
2778	u_int32_t *vectp;
2779	char *stringp;
2780	uintptr_t destp;
2781	u_int32_t *stack_base;
2782	struct freebsd32_ps_strings *arginfo;
2783	char canary[sizeof(long) * 8];
2784	int32_t pagesizes32[MAXPAGESIZES];
2785	size_t execpath_len;
2786	int szsigcode;
2787
2788	/*
2789	 * Calculate string base and vector table pointers.
2790	 * Also deal with signal trampoline code for this exec type.
2791	 */
2792	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2793		execpath_len = strlen(imgp->execpath) + 1;
2794	else
2795		execpath_len = 0;
2796	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2797	    sv_psstrings;
2798	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2799		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2800	else
2801		szsigcode = 0;
2802	destp =	(uintptr_t)arginfo;
2803
2804	/*
2805	 * install sigcode
2806	 */
2807	if (szsigcode != 0) {
2808		destp -= szsigcode;
2809		destp = rounddown2(destp, sizeof(uint32_t));
2810		copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2811		    szsigcode);
2812	}
2813
2814	/*
2815	 * Copy the image path for the rtld.
2816	 */
2817	if (execpath_len != 0) {
2818		destp -= execpath_len;
2819		imgp->execpathp = destp;
2820		copyout(imgp->execpath, (void *)destp, execpath_len);
2821	}
2822
2823	/*
2824	 * Prepare the canary for SSP.
2825	 */
2826	arc4rand(canary, sizeof(canary), 0);
2827	destp -= sizeof(canary);
2828	imgp->canary = destp;
2829	copyout(canary, (void *)destp, sizeof(canary));
2830	imgp->canarylen = sizeof(canary);
2831
2832	/*
2833	 * Prepare the pagesizes array.
2834	 */
2835	for (i = 0; i < MAXPAGESIZES; i++)
2836		pagesizes32[i] = (uint32_t)pagesizes[i];
2837	destp -= sizeof(pagesizes32);
2838	destp = rounddown2(destp, sizeof(uint32_t));
2839	imgp->pagesizes = destp;
2840	copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2841	imgp->pagesizeslen = sizeof(pagesizes32);
2842
2843	destp -= ARG_MAX - imgp->args->stringspace;
2844	destp = rounddown2(destp, sizeof(uint32_t));
2845
2846	/*
2847	 * If we have a valid auxargs ptr, prepare some room
2848	 * on the stack.
2849	 */
2850	if (imgp->auxargs) {
2851		/*
2852		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2853		 * lower compatibility.
2854		 */
2855		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2856			: (AT_COUNT * 2);
2857		/*
2858		 * The '+ 2' is for the null pointers at the end of each of
2859		 * the arg and env vector sets,and imgp->auxarg_size is room
2860		 * for argument of Runtime loader.
2861		 */
2862		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2863		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2864		    sizeof(u_int32_t));
2865	} else {
2866		/*
2867		 * The '+ 2' is for the null pointers at the end of each of
2868		 * the arg and env vector sets
2869		 */
2870		vectp = (u_int32_t *)(destp - (imgp->args->argc +
2871		    imgp->args->envc + 2) * sizeof(u_int32_t));
2872	}
2873
2874	/*
2875	 * vectp also becomes our initial stack base
2876	 */
2877	stack_base = vectp;
2878
2879	stringp = imgp->args->begin_argv;
2880	argc = imgp->args->argc;
2881	envc = imgp->args->envc;
2882	/*
2883	 * Copy out strings - arguments and environment.
2884	 */
2885	copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2886
2887	/*
2888	 * Fill in "ps_strings" struct for ps, w, etc.
2889	 */
2890	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2891	suword32(&arginfo->ps_nargvstr, argc);
2892
2893	/*
2894	 * Fill in argument portion of vector table.
2895	 */
2896	for (; argc > 0; --argc) {
2897		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2898		while (*stringp++ != 0)
2899			destp++;
2900		destp++;
2901	}
2902
2903	/* a null vector table pointer separates the argp's from the envp's */
2904	suword32(vectp++, 0);
2905
2906	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2907	suword32(&arginfo->ps_nenvstr, envc);
2908
2909	/*
2910	 * Fill in environment portion of vector table.
2911	 */
2912	for (; envc > 0; --envc) {
2913		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2914		while (*stringp++ != 0)
2915			destp++;
2916		destp++;
2917	}
2918
2919	/* end of vector table is a null pointer */
2920	suword32(vectp, 0);
2921
2922	return ((register_t *)stack_base);
2923}
2924
2925int
2926freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2927{
2928	struct kld_file_stat stat;
2929	struct kld32_file_stat stat32;
2930	int error, version;
2931
2932	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2933	    != 0)
2934		return (error);
2935	if (version != sizeof(struct kld32_file_stat_1) &&
2936	    version != sizeof(struct kld32_file_stat))
2937		return (EINVAL);
2938
2939	error = kern_kldstat(td, uap->fileid, &stat);
2940	if (error != 0)
2941		return (error);
2942
2943	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2944	CP(stat, stat32, refs);
2945	CP(stat, stat32, id);
2946	PTROUT_CP(stat, stat32, address);
2947	CP(stat, stat32, size);
2948	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2949	return (copyout(&stat32, uap->stat, version));
2950}
2951
2952int
2953freebsd32_posix_fallocate(struct thread *td,
2954    struct freebsd32_posix_fallocate_args *uap)
2955{
2956
2957	td->td_retval[0] = kern_posix_fallocate(td, uap->fd,
2958	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
2959	return (0);
2960}
2961
2962int
2963freebsd32_posix_fadvise(struct thread *td,
2964    struct freebsd32_posix_fadvise_args *uap)
2965{
2966
2967	td->td_retval[0] = kern_posix_fadvise(td, uap->fd,
2968	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len),
2969	    uap->advice);
2970	return (0);
2971}
2972
2973int
2974convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
2975{
2976
2977	CP(*sig32, *sig, sigev_notify);
2978	switch (sig->sigev_notify) {
2979	case SIGEV_NONE:
2980		break;
2981	case SIGEV_THREAD_ID:
2982		CP(*sig32, *sig, sigev_notify_thread_id);
2983		/* FALLTHROUGH */
2984	case SIGEV_SIGNAL:
2985		CP(*sig32, *sig, sigev_signo);
2986		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2987		break;
2988	case SIGEV_KEVENT:
2989		CP(*sig32, *sig, sigev_notify_kqueue);
2990		CP(*sig32, *sig, sigev_notify_kevent_flags);
2991		PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
2992		break;
2993	default:
2994		return (EINVAL);
2995	}
2996	return (0);
2997}
2998
2999int
3000freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3001{
3002	void *data;
3003	union {
3004		struct procctl_reaper_status rs;
3005		struct procctl_reaper_pids rp;
3006		struct procctl_reaper_kill rk;
3007	} x;
3008	union {
3009		struct procctl_reaper_pids32 rp;
3010	} x32;
3011	int error, error1, flags;
3012
3013	switch (uap->com) {
3014	case PROC_SPROTECT:
3015	case PROC_TRACE_CTL:
3016		error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3017		if (error != 0)
3018			return (error);
3019		data = &flags;
3020		break;
3021	case PROC_REAP_ACQUIRE:
3022	case PROC_REAP_RELEASE:
3023		if (uap->data != NULL)
3024			return (EINVAL);
3025		data = NULL;
3026		break;
3027	case PROC_REAP_STATUS:
3028		data = &x.rs;
3029		break;
3030	case PROC_REAP_GETPIDS:
3031		error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3032		if (error != 0)
3033			return (error);
3034		CP(x32.rp, x.rp, rp_count);
3035		PTRIN_CP(x32.rp, x.rp, rp_pids);
3036		data = &x.rp;
3037		break;
3038	case PROC_REAP_KILL:
3039		error = copyin(uap->data, &x.rk, sizeof(x.rk));
3040		if (error != 0)
3041			return (error);
3042		data = &x.rk;
3043		break;
3044	case PROC_TRACE_STATUS:
3045		data = &flags;
3046		break;
3047	default:
3048		return (EINVAL);
3049	}
3050	error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3051	    uap->com, data);
3052	switch (uap->com) {
3053	case PROC_REAP_STATUS:
3054		if (error == 0)
3055			error = copyout(&x.rs, uap->data, sizeof(x.rs));
3056		break;
3057	case PROC_REAP_KILL:
3058		error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3059		if (error == 0)
3060			error = error1;
3061		break;
3062	case PROC_TRACE_STATUS:
3063		if (error == 0)
3064			error = copyout(&flags, uap->data, sizeof(flags));
3065		break;
3066	}
3067	return (error);
3068}
3069
3070int
3071freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3072{
3073	long tmp;
3074
3075	switch (uap->cmd) {
3076	/*
3077	 * Do unsigned conversion for arg when operation
3078	 * interprets it as flags or pointer.
3079	 */
3080	case F_SETLK_REMOTE:
3081	case F_SETLKW:
3082	case F_SETLK:
3083	case F_GETLK:
3084	case F_SETFD:
3085	case F_SETFL:
3086	case F_OGETLK:
3087	case F_OSETLK:
3088	case F_OSETLKW:
3089		tmp = (unsigned int)(uap->arg);
3090		break;
3091	default:
3092		tmp = uap->arg;
3093		break;
3094	}
3095	return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3096}
3097
3098int
3099freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3100{
3101	struct timespec32 ts32;
3102	struct timespec ts, *tsp;
3103	sigset_t set, *ssp;
3104	int error;
3105
3106	if (uap->ts != NULL) {
3107		error = copyin(uap->ts, &ts32, sizeof(ts32));
3108		if (error != 0)
3109			return (error);
3110		CP(ts32, ts, tv_sec);
3111		CP(ts32, ts, tv_nsec);
3112		tsp = &ts;
3113	} else
3114		tsp = NULL;
3115	if (uap->set != NULL) {
3116		error = copyin(uap->set, &set, sizeof(set));
3117		if (error != 0)
3118			return (error);
3119		ssp = &set;
3120	} else
3121		ssp = NULL;
3122
3123	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3124}
3125