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