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