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