freebsd32_misc.c revision 232449
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 232449 2012-03-03 08:19:18Z 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#define FREEBSD32_ALIGNBYTES	(sizeof(int) - 1)
956#define FREEBSD32_ALIGN(p)	\
957	(((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
958#define	FREEBSD32_CMSG_SPACE(l)	\
959	(FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
960
961#define	FREEBSD32_CMSG_DATA(cmsg)	((unsigned char *)(cmsg) + \
962				 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
963static int
964freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
965{
966	struct cmsghdr *cm;
967	void *data;
968	socklen_t clen, datalen;
969	int error;
970	caddr_t ctlbuf;
971	int len, maxlen, copylen;
972	struct mbuf *m;
973	error = 0;
974
975	len    = msg->msg_controllen;
976	maxlen = msg->msg_controllen;
977	msg->msg_controllen = 0;
978
979	m = control;
980	ctlbuf = msg->msg_control;
981
982	while (m && len > 0) {
983		cm = mtod(m, struct cmsghdr *);
984		clen = m->m_len;
985
986		while (cm != NULL) {
987
988			if (sizeof(struct cmsghdr) > clen ||
989			    cm->cmsg_len > clen) {
990				error = EINVAL;
991				break;
992			}
993
994			data   = CMSG_DATA(cm);
995			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
996
997			/* Adjust message length */
998			cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
999			    datalen;
1000
1001
1002			/* Copy cmsghdr */
1003			copylen = sizeof(struct cmsghdr);
1004			if (len < copylen) {
1005				msg->msg_flags |= MSG_CTRUNC;
1006				copylen = len;
1007			}
1008
1009			error = copyout(cm,ctlbuf,copylen);
1010			if (error)
1011				goto exit;
1012
1013			ctlbuf += FREEBSD32_ALIGN(copylen);
1014			len    -= FREEBSD32_ALIGN(copylen);
1015
1016			if (len <= 0)
1017				break;
1018
1019			/* Copy data */
1020			copylen = datalen;
1021			if (len < copylen) {
1022				msg->msg_flags |= MSG_CTRUNC;
1023				copylen = len;
1024			}
1025
1026			error = copyout(data,ctlbuf,copylen);
1027			if (error)
1028				goto exit;
1029
1030			ctlbuf += FREEBSD32_ALIGN(copylen);
1031			len    -= FREEBSD32_ALIGN(copylen);
1032
1033			if (CMSG_SPACE(datalen) < clen) {
1034				clen -= CMSG_SPACE(datalen);
1035				cm = (struct cmsghdr *)
1036					((caddr_t)cm + CMSG_SPACE(datalen));
1037			} else {
1038				clen = 0;
1039				cm = NULL;
1040			}
1041		}
1042		m = m->m_next;
1043	}
1044
1045	msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
1046
1047exit:
1048	return (error);
1049
1050}
1051
1052int
1053freebsd32_recvmsg(td, uap)
1054	struct thread *td;
1055	struct freebsd32_recvmsg_args /* {
1056		int	s;
1057		struct	msghdr32 *msg;
1058		int	flags;
1059	} */ *uap;
1060{
1061	struct msghdr msg;
1062	struct msghdr32 m32;
1063	struct iovec *uiov, *iov;
1064	struct mbuf *control = NULL;
1065	struct mbuf **controlp;
1066
1067	int error;
1068	error = copyin(uap->msg, &m32, sizeof(m32));
1069	if (error)
1070		return (error);
1071	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1072	if (error)
1073		return (error);
1074	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1075	    EMSGSIZE);
1076	if (error)
1077		return (error);
1078	msg.msg_flags = uap->flags;
1079	uiov = msg.msg_iov;
1080	msg.msg_iov = iov;
1081
1082	controlp = (msg.msg_control != NULL) ?  &control : NULL;
1083	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1084	if (error == 0) {
1085		msg.msg_iov = uiov;
1086
1087		if (control != NULL)
1088			error = freebsd32_copy_msg_out(&msg, control);
1089		else
1090			msg.msg_controllen = 0;
1091
1092		if (error == 0)
1093			error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1094	}
1095	free(iov, M_IOV);
1096
1097	if (control != NULL)
1098		m_freem(control);
1099
1100	return (error);
1101}
1102
1103
1104static int
1105freebsd32_convert_msg_in(struct mbuf **controlp)
1106{
1107	struct mbuf *control = *controlp;
1108	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1109	void *data;
1110	socklen_t clen = control->m_len, datalen;
1111	int error;
1112
1113	error = 0;
1114	*controlp = NULL;
1115
1116	while (cm != NULL) {
1117		if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1118			error = EINVAL;
1119			break;
1120		}
1121
1122		data = FREEBSD32_CMSG_DATA(cm);
1123		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1124
1125		*controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1126		    cm->cmsg_level);
1127		controlp = &(*controlp)->m_next;
1128
1129		if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1130			clen -= FREEBSD32_CMSG_SPACE(datalen);
1131			cm = (struct cmsghdr *)
1132				((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1133		} else {
1134			clen = 0;
1135			cm = NULL;
1136		}
1137	}
1138
1139	m_freem(control);
1140	return (error);
1141}
1142
1143
1144int
1145freebsd32_sendmsg(struct thread *td,
1146		  struct freebsd32_sendmsg_args *uap)
1147{
1148	struct msghdr msg;
1149	struct msghdr32 m32;
1150	struct iovec *iov;
1151	struct mbuf *control = NULL;
1152	struct sockaddr *to = NULL;
1153	int error;
1154
1155	error = copyin(uap->msg, &m32, sizeof(m32));
1156	if (error)
1157		return (error);
1158	error = freebsd32_copyinmsghdr(uap->msg, &msg);
1159	if (error)
1160		return (error);
1161	error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1162	    EMSGSIZE);
1163	if (error)
1164		return (error);
1165	msg.msg_iov = iov;
1166	if (msg.msg_name != NULL) {
1167		error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1168		if (error) {
1169			to = NULL;
1170			goto out;
1171		}
1172		msg.msg_name = to;
1173	}
1174
1175	if (msg.msg_control) {
1176		if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1177			error = EINVAL;
1178			goto out;
1179		}
1180
1181		error = sockargs(&control, msg.msg_control,
1182		    msg.msg_controllen, MT_CONTROL);
1183		if (error)
1184			goto out;
1185
1186		error = freebsd32_convert_msg_in(&control);
1187		if (error)
1188			goto out;
1189	}
1190
1191	error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1192	    UIO_USERSPACE);
1193
1194out:
1195	free(iov, M_IOV);
1196	if (to)
1197		free(to, M_SONAME);
1198	return (error);
1199}
1200
1201int
1202freebsd32_recvfrom(struct thread *td,
1203		   struct freebsd32_recvfrom_args *uap)
1204{
1205	struct msghdr msg;
1206	struct iovec aiov;
1207	int error;
1208
1209	if (uap->fromlenaddr) {
1210		error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1211		    sizeof(msg.msg_namelen));
1212		if (error)
1213			return (error);
1214	} else {
1215		msg.msg_namelen = 0;
1216	}
1217
1218	msg.msg_name = PTRIN(uap->from);
1219	msg.msg_iov = &aiov;
1220	msg.msg_iovlen = 1;
1221	aiov.iov_base = PTRIN(uap->buf);
1222	aiov.iov_len = uap->len;
1223	msg.msg_control = NULL;
1224	msg.msg_flags = uap->flags;
1225	error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1226	if (error == 0 && uap->fromlenaddr)
1227		error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1228		    sizeof (msg.msg_namelen));
1229	return (error);
1230}
1231
1232int
1233freebsd32_settimeofday(struct thread *td,
1234		       struct freebsd32_settimeofday_args *uap)
1235{
1236	struct timeval32 tv32;
1237	struct timeval tv, *tvp;
1238	struct timezone tz, *tzp;
1239	int error;
1240
1241	if (uap->tv) {
1242		error = copyin(uap->tv, &tv32, sizeof(tv32));
1243		if (error)
1244			return (error);
1245		CP(tv32, tv, tv_sec);
1246		CP(tv32, tv, tv_usec);
1247		tvp = &tv;
1248	} else
1249		tvp = NULL;
1250	if (uap->tzp) {
1251		error = copyin(uap->tzp, &tz, sizeof(tz));
1252		if (error)
1253			return (error);
1254		tzp = &tz;
1255	} else
1256		tzp = NULL;
1257	return (kern_settimeofday(td, tvp, tzp));
1258}
1259
1260int
1261freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1262{
1263	struct timeval32 s32[2];
1264	struct timeval s[2], *sp;
1265	int error;
1266
1267	if (uap->tptr != NULL) {
1268		error = copyin(uap->tptr, s32, sizeof(s32));
1269		if (error)
1270			return (error);
1271		CP(s32[0], s[0], tv_sec);
1272		CP(s32[0], s[0], tv_usec);
1273		CP(s32[1], s[1], tv_sec);
1274		CP(s32[1], s[1], tv_usec);
1275		sp = s;
1276	} else
1277		sp = NULL;
1278	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1279}
1280
1281int
1282freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1283{
1284	struct timeval32 s32[2];
1285	struct timeval s[2], *sp;
1286	int error;
1287
1288	if (uap->tptr != NULL) {
1289		error = copyin(uap->tptr, s32, sizeof(s32));
1290		if (error)
1291			return (error);
1292		CP(s32[0], s[0], tv_sec);
1293		CP(s32[0], s[0], tv_usec);
1294		CP(s32[1], s[1], tv_sec);
1295		CP(s32[1], s[1], tv_usec);
1296		sp = s;
1297	} else
1298		sp = NULL;
1299	return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1300}
1301
1302int
1303freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1304{
1305	struct timeval32 s32[2];
1306	struct timeval s[2], *sp;
1307	int error;
1308
1309	if (uap->tptr != NULL) {
1310		error = copyin(uap->tptr, s32, sizeof(s32));
1311		if (error)
1312			return (error);
1313		CP(s32[0], s[0], tv_sec);
1314		CP(s32[0], s[0], tv_usec);
1315		CP(s32[1], s[1], tv_sec);
1316		CP(s32[1], s[1], tv_usec);
1317		sp = s;
1318	} else
1319		sp = NULL;
1320	return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1321}
1322
1323int
1324freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1325{
1326	struct timeval32 s32[2];
1327	struct timeval s[2], *sp;
1328	int error;
1329
1330	if (uap->times != NULL) {
1331		error = copyin(uap->times, s32, sizeof(s32));
1332		if (error)
1333			return (error);
1334		CP(s32[0], s[0], tv_sec);
1335		CP(s32[0], s[0], tv_usec);
1336		CP(s32[1], s[1], tv_sec);
1337		CP(s32[1], s[1], tv_usec);
1338		sp = s;
1339	} else
1340		sp = NULL;
1341	return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1342		sp, UIO_SYSSPACE));
1343}
1344
1345int
1346freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1347{
1348	struct timeval32 tv32;
1349	struct timeval delta, olddelta, *deltap;
1350	int error;
1351
1352	if (uap->delta) {
1353		error = copyin(uap->delta, &tv32, sizeof(tv32));
1354		if (error)
1355			return (error);
1356		CP(tv32, delta, tv_sec);
1357		CP(tv32, delta, tv_usec);
1358		deltap = &delta;
1359	} else
1360		deltap = NULL;
1361	error = kern_adjtime(td, deltap, &olddelta);
1362	if (uap->olddelta && error == 0) {
1363		CP(olddelta, tv32, tv_sec);
1364		CP(olddelta, tv32, tv_usec);
1365		error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1366	}
1367	return (error);
1368}
1369
1370#ifdef COMPAT_FREEBSD4
1371int
1372freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1373{
1374	struct statfs32 s32;
1375	struct statfs s;
1376	int error;
1377
1378	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1379	if (error)
1380		return (error);
1381	copy_statfs(&s, &s32);
1382	return (copyout(&s32, uap->buf, sizeof(s32)));
1383}
1384#endif
1385
1386#ifdef COMPAT_FREEBSD4
1387int
1388freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1389{
1390	struct statfs32 s32;
1391	struct statfs s;
1392	int error;
1393
1394	error = kern_fstatfs(td, uap->fd, &s);
1395	if (error)
1396		return (error);
1397	copy_statfs(&s, &s32);
1398	return (copyout(&s32, uap->buf, sizeof(s32)));
1399}
1400#endif
1401
1402#ifdef COMPAT_FREEBSD4
1403int
1404freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1405{
1406	struct statfs32 s32;
1407	struct statfs s;
1408	fhandle_t fh;
1409	int error;
1410
1411	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1412		return (error);
1413	error = kern_fhstatfs(td, fh, &s);
1414	if (error)
1415		return (error);
1416	copy_statfs(&s, &s32);
1417	return (copyout(&s32, uap->buf, sizeof(s32)));
1418}
1419#endif
1420
1421int
1422freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1423{
1424	struct pread_args ap;
1425
1426	ap.fd = uap->fd;
1427	ap.buf = uap->buf;
1428	ap.nbyte = uap->nbyte;
1429	ap.offset = PAIR32TO64(off_t,uap->offset);
1430	return (sys_pread(td, &ap));
1431}
1432
1433int
1434freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1435{
1436	struct pwrite_args ap;
1437
1438	ap.fd = uap->fd;
1439	ap.buf = uap->buf;
1440	ap.nbyte = uap->nbyte;
1441	ap.offset = PAIR32TO64(off_t,uap->offset);
1442	return (sys_pwrite(td, &ap));
1443}
1444
1445#ifdef COMPAT_43
1446int
1447ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1448{
1449	struct lseek_args nuap;
1450
1451	nuap.fd = uap->fd;
1452	nuap.offset = uap->offset;
1453	nuap.whence = uap->whence;
1454	return (sys_lseek(td, &nuap));
1455}
1456#endif
1457
1458int
1459freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1460{
1461	int error;
1462	struct lseek_args ap;
1463	off_t pos;
1464
1465	ap.fd = uap->fd;
1466	ap.offset = PAIR32TO64(off_t,uap->offset);
1467	ap.whence = uap->whence;
1468	error = sys_lseek(td, &ap);
1469	/* Expand the quad return into two parts for eax and edx */
1470	pos = *(off_t *)(td->td_retval);
1471	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1472	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1473	return error;
1474}
1475
1476int
1477freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1478{
1479	struct truncate_args ap;
1480
1481	ap.path = uap->path;
1482	ap.length = PAIR32TO64(off_t,uap->length);
1483	return (sys_truncate(td, &ap));
1484}
1485
1486int
1487freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1488{
1489	struct ftruncate_args ap;
1490
1491	ap.fd = uap->fd;
1492	ap.length = PAIR32TO64(off_t,uap->length);
1493	return (sys_ftruncate(td, &ap));
1494}
1495
1496#ifdef COMPAT_43
1497int
1498ofreebsd32_getdirentries(struct thread *td,
1499    struct ofreebsd32_getdirentries_args *uap)
1500{
1501	struct ogetdirentries_args ap;
1502	int error;
1503	long loff;
1504	int32_t loff_cut;
1505
1506	ap.fd = uap->fd;
1507	ap.buf = uap->buf;
1508	ap.count = uap->count;
1509	ap.basep = NULL;
1510	error = kern_ogetdirentries(td, &ap, &loff);
1511	if (error == 0) {
1512		loff_cut = loff;
1513		error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1514	}
1515	return (error);
1516}
1517#endif
1518
1519int
1520freebsd32_getdirentries(struct thread *td,
1521    struct freebsd32_getdirentries_args *uap)
1522{
1523	long base;
1524	int32_t base32;
1525	int error;
1526
1527	error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
1528	if (error)
1529		return (error);
1530	if (uap->basep != NULL) {
1531		base32 = base;
1532		error = copyout(&base32, uap->basep, sizeof(int32_t));
1533	}
1534	return (error);
1535}
1536
1537#ifdef COMPAT_FREEBSD6
1538/* versions with the 'int pad' argument */
1539int
1540freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1541{
1542	struct pread_args ap;
1543
1544	ap.fd = uap->fd;
1545	ap.buf = uap->buf;
1546	ap.nbyte = uap->nbyte;
1547	ap.offset = PAIR32TO64(off_t,uap->offset);
1548	return (sys_pread(td, &ap));
1549}
1550
1551int
1552freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1553{
1554	struct pwrite_args ap;
1555
1556	ap.fd = uap->fd;
1557	ap.buf = uap->buf;
1558	ap.nbyte = uap->nbyte;
1559	ap.offset = PAIR32TO64(off_t,uap->offset);
1560	return (sys_pwrite(td, &ap));
1561}
1562
1563int
1564freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1565{
1566	int error;
1567	struct lseek_args ap;
1568	off_t pos;
1569
1570	ap.fd = uap->fd;
1571	ap.offset = PAIR32TO64(off_t,uap->offset);
1572	ap.whence = uap->whence;
1573	error = sys_lseek(td, &ap);
1574	/* Expand the quad return into two parts for eax and edx */
1575	pos = *(off_t *)(td->td_retval);
1576	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1577	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1578	return error;
1579}
1580
1581int
1582freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1583{
1584	struct truncate_args ap;
1585
1586	ap.path = uap->path;
1587	ap.length = PAIR32TO64(off_t,uap->length);
1588	return (sys_truncate(td, &ap));
1589}
1590
1591int
1592freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1593{
1594	struct ftruncate_args ap;
1595
1596	ap.fd = uap->fd;
1597	ap.length = PAIR32TO64(off_t,uap->length);
1598	return (sys_ftruncate(td, &ap));
1599}
1600#endif /* COMPAT_FREEBSD6 */
1601
1602struct sf_hdtr32 {
1603	uint32_t headers;
1604	int hdr_cnt;
1605	uint32_t trailers;
1606	int trl_cnt;
1607};
1608
1609static int
1610freebsd32_do_sendfile(struct thread *td,
1611    struct freebsd32_sendfile_args *uap, int compat)
1612{
1613	struct sendfile_args ap;
1614	struct sf_hdtr32 hdtr32;
1615	struct sf_hdtr hdtr;
1616	struct uio *hdr_uio, *trl_uio;
1617	struct iovec32 *iov32;
1618	int error;
1619
1620	hdr_uio = trl_uio = NULL;
1621
1622	ap.fd = uap->fd;
1623	ap.s = uap->s;
1624	ap.offset = PAIR32TO64(off_t,uap->offset);
1625	ap.nbytes = uap->nbytes;
1626	ap.hdtr = (struct sf_hdtr *)uap->hdtr;		/* XXX not used */
1627	ap.sbytes = uap->sbytes;
1628	ap.flags = uap->flags;
1629
1630	if (uap->hdtr != NULL) {
1631		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1632		if (error)
1633			goto out;
1634		PTRIN_CP(hdtr32, hdtr, headers);
1635		CP(hdtr32, hdtr, hdr_cnt);
1636		PTRIN_CP(hdtr32, hdtr, trailers);
1637		CP(hdtr32, hdtr, trl_cnt);
1638
1639		if (hdtr.headers != NULL) {
1640			iov32 = PTRIN(hdtr32.headers);
1641			error = freebsd32_copyinuio(iov32,
1642			    hdtr32.hdr_cnt, &hdr_uio);
1643			if (error)
1644				goto out;
1645		}
1646		if (hdtr.trailers != NULL) {
1647			iov32 = PTRIN(hdtr32.trailers);
1648			error = freebsd32_copyinuio(iov32,
1649			    hdtr32.trl_cnt, &trl_uio);
1650			if (error)
1651				goto out;
1652		}
1653	}
1654
1655	error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1656out:
1657	if (hdr_uio)
1658		free(hdr_uio, M_IOV);
1659	if (trl_uio)
1660		free(trl_uio, M_IOV);
1661	return (error);
1662}
1663
1664#ifdef COMPAT_FREEBSD4
1665int
1666freebsd4_freebsd32_sendfile(struct thread *td,
1667    struct freebsd4_freebsd32_sendfile_args *uap)
1668{
1669	return (freebsd32_do_sendfile(td,
1670	    (struct freebsd32_sendfile_args *)uap, 1));
1671}
1672#endif
1673
1674int
1675freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1676{
1677
1678	return (freebsd32_do_sendfile(td, uap, 0));
1679}
1680
1681static void
1682copy_stat(struct stat *in, struct stat32 *out)
1683{
1684
1685	CP(*in, *out, st_dev);
1686	CP(*in, *out, st_ino);
1687	CP(*in, *out, st_mode);
1688	CP(*in, *out, st_nlink);
1689	CP(*in, *out, st_uid);
1690	CP(*in, *out, st_gid);
1691	CP(*in, *out, st_rdev);
1692	TS_CP(*in, *out, st_atim);
1693	TS_CP(*in, *out, st_mtim);
1694	TS_CP(*in, *out, st_ctim);
1695	CP(*in, *out, st_size);
1696	CP(*in, *out, st_blocks);
1697	CP(*in, *out, st_blksize);
1698	CP(*in, *out, st_flags);
1699	CP(*in, *out, st_gen);
1700	TS_CP(*in, *out, st_birthtim);
1701}
1702
1703#ifdef COMPAT_43
1704static void
1705copy_ostat(struct stat *in, struct ostat32 *out)
1706{
1707
1708	CP(*in, *out, st_dev);
1709	CP(*in, *out, st_ino);
1710	CP(*in, *out, st_mode);
1711	CP(*in, *out, st_nlink);
1712	CP(*in, *out, st_uid);
1713	CP(*in, *out, st_gid);
1714	CP(*in, *out, st_rdev);
1715	CP(*in, *out, st_size);
1716	TS_CP(*in, *out, st_atim);
1717	TS_CP(*in, *out, st_mtim);
1718	TS_CP(*in, *out, st_ctim);
1719	CP(*in, *out, st_blksize);
1720	CP(*in, *out, st_blocks);
1721	CP(*in, *out, st_flags);
1722	CP(*in, *out, st_gen);
1723}
1724#endif
1725
1726int
1727freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1728{
1729	struct stat sb;
1730	struct stat32 sb32;
1731	int error;
1732
1733	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1734	if (error)
1735		return (error);
1736	copy_stat(&sb, &sb32);
1737	error = copyout(&sb32, uap->ub, sizeof (sb32));
1738	return (error);
1739}
1740
1741#ifdef COMPAT_43
1742int
1743ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1744{
1745	struct stat sb;
1746	struct ostat32 sb32;
1747	int error;
1748
1749	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1750	if (error)
1751		return (error);
1752	copy_ostat(&sb, &sb32);
1753	error = copyout(&sb32, uap->ub, sizeof (sb32));
1754	return (error);
1755}
1756#endif
1757
1758int
1759freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1760{
1761	struct stat ub;
1762	struct stat32 ub32;
1763	int error;
1764
1765	error = kern_fstat(td, uap->fd, &ub);
1766	if (error)
1767		return (error);
1768	copy_stat(&ub, &ub32);
1769	error = copyout(&ub32, uap->ub, sizeof(ub32));
1770	return (error);
1771}
1772
1773#ifdef COMPAT_43
1774int
1775ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1776{
1777	struct stat ub;
1778	struct ostat32 ub32;
1779	int error;
1780
1781	error = kern_fstat(td, uap->fd, &ub);
1782	if (error)
1783		return (error);
1784	copy_ostat(&ub, &ub32);
1785	error = copyout(&ub32, uap->ub, sizeof(ub32));
1786	return (error);
1787}
1788#endif
1789
1790int
1791freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1792{
1793	struct stat ub;
1794	struct stat32 ub32;
1795	int error;
1796
1797	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1798	if (error)
1799		return (error);
1800	copy_stat(&ub, &ub32);
1801	error = copyout(&ub32, uap->buf, sizeof(ub32));
1802	return (error);
1803}
1804
1805int
1806freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1807{
1808	struct stat sb;
1809	struct stat32 sb32;
1810	int error;
1811
1812	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1813	if (error)
1814		return (error);
1815	copy_stat(&sb, &sb32);
1816	error = copyout(&sb32, uap->ub, sizeof (sb32));
1817	return (error);
1818}
1819
1820#ifdef COMPAT_43
1821int
1822ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1823{
1824	struct stat sb;
1825	struct ostat32 sb32;
1826	int error;
1827
1828	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1829	if (error)
1830		return (error);
1831	copy_ostat(&sb, &sb32);
1832	error = copyout(&sb32, uap->ub, sizeof (sb32));
1833	return (error);
1834}
1835#endif
1836
1837int
1838freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1839{
1840	int error, name[CTL_MAXNAME];
1841	size_t j, oldlen;
1842
1843	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1844		return (EINVAL);
1845 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1846 	if (error)
1847		return (error);
1848	if (uap->oldlenp)
1849		oldlen = fuword32(uap->oldlenp);
1850	else
1851		oldlen = 0;
1852	error = userland_sysctl(td, name, uap->namelen,
1853		uap->old, &oldlen, 1,
1854		uap->new, uap->newlen, &j, SCTL_MASK32);
1855	if (error && error != ENOMEM)
1856		return (error);
1857	if (uap->oldlenp)
1858		suword32(uap->oldlenp, j);
1859	return (0);
1860}
1861
1862int
1863freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1864{
1865	uint32_t version;
1866	int error;
1867	struct jail j;
1868
1869	error = copyin(uap->jail, &version, sizeof(uint32_t));
1870	if (error)
1871		return (error);
1872
1873	switch (version) {
1874	case 0:
1875	{
1876		/* FreeBSD single IPv4 jails. */
1877		struct jail32_v0 j32_v0;
1878
1879		bzero(&j, sizeof(struct jail));
1880		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1881		if (error)
1882			return (error);
1883		CP(j32_v0, j, version);
1884		PTRIN_CP(j32_v0, j, path);
1885		PTRIN_CP(j32_v0, j, hostname);
1886		j.ip4s = j32_v0.ip_number;
1887		break;
1888	}
1889
1890	case 1:
1891		/*
1892		 * Version 1 was used by multi-IPv4 jail implementations
1893		 * that never made it into the official kernel.
1894		 */
1895		return (EINVAL);
1896
1897	case 2:	/* JAIL_API_VERSION */
1898	{
1899		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
1900		struct jail32 j32;
1901
1902		error = copyin(uap->jail, &j32, sizeof(struct jail32));
1903		if (error)
1904			return (error);
1905		CP(j32, j, version);
1906		PTRIN_CP(j32, j, path);
1907		PTRIN_CP(j32, j, hostname);
1908		PTRIN_CP(j32, j, jailname);
1909		CP(j32, j, ip4s);
1910		CP(j32, j, ip6s);
1911		PTRIN_CP(j32, j, ip4);
1912		PTRIN_CP(j32, j, ip6);
1913		break;
1914	}
1915
1916	default:
1917		/* Sci-Fi jails are not supported, sorry. */
1918		return (EINVAL);
1919	}
1920	return (kern_jail(td, &j));
1921}
1922
1923int
1924freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1925{
1926	struct uio *auio;
1927	int error;
1928
1929	/* Check that we have an even number of iovecs. */
1930	if (uap->iovcnt & 1)
1931		return (EINVAL);
1932
1933	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1934	if (error)
1935		return (error);
1936	error = kern_jail_set(td, auio, uap->flags);
1937	free(auio, M_IOV);
1938	return (error);
1939}
1940
1941int
1942freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1943{
1944	struct iovec32 iov32;
1945	struct uio *auio;
1946	int error, i;
1947
1948	/* Check that we have an even number of iovecs. */
1949	if (uap->iovcnt & 1)
1950		return (EINVAL);
1951
1952	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1953	if (error)
1954		return (error);
1955	error = kern_jail_get(td, auio, uap->flags);
1956	if (error == 0)
1957		for (i = 0; i < uap->iovcnt; i++) {
1958			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1959			CP(auio->uio_iov[i], iov32, iov_len);
1960			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1961			if (error != 0)
1962				break;
1963		}
1964	free(auio, M_IOV);
1965	return (error);
1966}
1967
1968int
1969freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1970{
1971	struct sigaction32 s32;
1972	struct sigaction sa, osa, *sap;
1973	int error;
1974
1975	if (uap->act) {
1976		error = copyin(uap->act, &s32, sizeof(s32));
1977		if (error)
1978			return (error);
1979		sa.sa_handler = PTRIN(s32.sa_u);
1980		CP(s32, sa, sa_flags);
1981		CP(s32, sa, sa_mask);
1982		sap = &sa;
1983	} else
1984		sap = NULL;
1985	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1986	if (error == 0 && uap->oact != NULL) {
1987		s32.sa_u = PTROUT(osa.sa_handler);
1988		CP(osa, s32, sa_flags);
1989		CP(osa, s32, sa_mask);
1990		error = copyout(&s32, uap->oact, sizeof(s32));
1991	}
1992	return (error);
1993}
1994
1995#ifdef COMPAT_FREEBSD4
1996int
1997freebsd4_freebsd32_sigaction(struct thread *td,
1998			     struct freebsd4_freebsd32_sigaction_args *uap)
1999{
2000	struct sigaction32 s32;
2001	struct sigaction sa, osa, *sap;
2002	int error;
2003
2004	if (uap->act) {
2005		error = copyin(uap->act, &s32, sizeof(s32));
2006		if (error)
2007			return (error);
2008		sa.sa_handler = PTRIN(s32.sa_u);
2009		CP(s32, sa, sa_flags);
2010		CP(s32, sa, sa_mask);
2011		sap = &sa;
2012	} else
2013		sap = NULL;
2014	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2015	if (error == 0 && uap->oact != NULL) {
2016		s32.sa_u = PTROUT(osa.sa_handler);
2017		CP(osa, s32, sa_flags);
2018		CP(osa, s32, sa_mask);
2019		error = copyout(&s32, uap->oact, sizeof(s32));
2020	}
2021	return (error);
2022}
2023#endif
2024
2025#ifdef COMPAT_43
2026struct osigaction32 {
2027	u_int32_t	sa_u;
2028	osigset_t	sa_mask;
2029	int		sa_flags;
2030};
2031
2032#define	ONSIG	32
2033
2034int
2035ofreebsd32_sigaction(struct thread *td,
2036			     struct ofreebsd32_sigaction_args *uap)
2037{
2038	struct osigaction32 s32;
2039	struct sigaction sa, osa, *sap;
2040	int error;
2041
2042	if (uap->signum <= 0 || uap->signum >= ONSIG)
2043		return (EINVAL);
2044
2045	if (uap->nsa) {
2046		error = copyin(uap->nsa, &s32, sizeof(s32));
2047		if (error)
2048			return (error);
2049		sa.sa_handler = PTRIN(s32.sa_u);
2050		CP(s32, sa, sa_flags);
2051		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2052		sap = &sa;
2053	} else
2054		sap = NULL;
2055	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2056	if (error == 0 && uap->osa != NULL) {
2057		s32.sa_u = PTROUT(osa.sa_handler);
2058		CP(osa, s32, sa_flags);
2059		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2060		error = copyout(&s32, uap->osa, sizeof(s32));
2061	}
2062	return (error);
2063}
2064
2065int
2066ofreebsd32_sigprocmask(struct thread *td,
2067			       struct ofreebsd32_sigprocmask_args *uap)
2068{
2069	sigset_t set, oset;
2070	int error;
2071
2072	OSIG2SIG(uap->mask, set);
2073	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2074	SIG2OSIG(oset, td->td_retval[0]);
2075	return (error);
2076}
2077
2078int
2079ofreebsd32_sigpending(struct thread *td,
2080			      struct ofreebsd32_sigpending_args *uap)
2081{
2082	struct proc *p = td->td_proc;
2083	sigset_t siglist;
2084
2085	PROC_LOCK(p);
2086	siglist = p->p_siglist;
2087	SIGSETOR(siglist, td->td_siglist);
2088	PROC_UNLOCK(p);
2089	SIG2OSIG(siglist, td->td_retval[0]);
2090	return (0);
2091}
2092
2093struct sigvec32 {
2094	u_int32_t	sv_handler;
2095	int		sv_mask;
2096	int		sv_flags;
2097};
2098
2099int
2100ofreebsd32_sigvec(struct thread *td,
2101			  struct ofreebsd32_sigvec_args *uap)
2102{
2103	struct sigvec32 vec;
2104	struct sigaction sa, osa, *sap;
2105	int error;
2106
2107	if (uap->signum <= 0 || uap->signum >= ONSIG)
2108		return (EINVAL);
2109
2110	if (uap->nsv) {
2111		error = copyin(uap->nsv, &vec, sizeof(vec));
2112		if (error)
2113			return (error);
2114		sa.sa_handler = PTRIN(vec.sv_handler);
2115		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2116		sa.sa_flags = vec.sv_flags;
2117		sa.sa_flags ^= SA_RESTART;
2118		sap = &sa;
2119	} else
2120		sap = NULL;
2121	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2122	if (error == 0 && uap->osv != NULL) {
2123		vec.sv_handler = PTROUT(osa.sa_handler);
2124		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2125		vec.sv_flags = osa.sa_flags;
2126		vec.sv_flags &= ~SA_NOCLDWAIT;
2127		vec.sv_flags ^= SA_RESTART;
2128		error = copyout(&vec, uap->osv, sizeof(vec));
2129	}
2130	return (error);
2131}
2132
2133int
2134ofreebsd32_sigblock(struct thread *td,
2135			    struct ofreebsd32_sigblock_args *uap)
2136{
2137	sigset_t set, oset;
2138
2139	OSIG2SIG(uap->mask, set);
2140	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2141	SIG2OSIG(oset, td->td_retval[0]);
2142	return (0);
2143}
2144
2145int
2146ofreebsd32_sigsetmask(struct thread *td,
2147			      struct ofreebsd32_sigsetmask_args *uap)
2148{
2149	sigset_t set, oset;
2150
2151	OSIG2SIG(uap->mask, set);
2152	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2153	SIG2OSIG(oset, td->td_retval[0]);
2154	return (0);
2155}
2156
2157int
2158ofreebsd32_sigsuspend(struct thread *td,
2159			      struct ofreebsd32_sigsuspend_args *uap)
2160{
2161	sigset_t mask;
2162
2163	OSIG2SIG(uap->mask, mask);
2164	return (kern_sigsuspend(td, mask));
2165}
2166
2167struct sigstack32 {
2168	u_int32_t	ss_sp;
2169	int		ss_onstack;
2170};
2171
2172int
2173ofreebsd32_sigstack(struct thread *td,
2174			    struct ofreebsd32_sigstack_args *uap)
2175{
2176	struct sigstack32 s32;
2177	struct sigstack nss, oss;
2178	int error = 0, unss;
2179
2180	if (uap->nss != NULL) {
2181		error = copyin(uap->nss, &s32, sizeof(s32));
2182		if (error)
2183			return (error);
2184		nss.ss_sp = PTRIN(s32.ss_sp);
2185		CP(s32, nss, ss_onstack);
2186		unss = 1;
2187	} else {
2188		unss = 0;
2189	}
2190	oss.ss_sp = td->td_sigstk.ss_sp;
2191	oss.ss_onstack = sigonstack(cpu_getstack(td));
2192	if (unss) {
2193		td->td_sigstk.ss_sp = nss.ss_sp;
2194		td->td_sigstk.ss_size = 0;
2195		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2196		td->td_pflags |= TDP_ALTSTACK;
2197	}
2198	if (uap->oss != NULL) {
2199		s32.ss_sp = PTROUT(oss.ss_sp);
2200		CP(oss, s32, ss_onstack);
2201		error = copyout(&s32, uap->oss, sizeof(s32));
2202	}
2203	return (error);
2204}
2205#endif
2206
2207int
2208freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2209{
2210	struct timespec32 rmt32, rqt32;
2211	struct timespec rmt, rqt;
2212	int error;
2213
2214	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2215	if (error)
2216		return (error);
2217
2218	CP(rqt32, rqt, tv_sec);
2219	CP(rqt32, rqt, tv_nsec);
2220
2221	if (uap->rmtp &&
2222	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2223		return (EFAULT);
2224	error = kern_nanosleep(td, &rqt, &rmt);
2225	if (error && uap->rmtp) {
2226		int error2;
2227
2228		CP(rmt, rmt32, tv_sec);
2229		CP(rmt, rmt32, tv_nsec);
2230
2231		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2232		if (error2)
2233			error = error2;
2234	}
2235	return (error);
2236}
2237
2238int
2239freebsd32_clock_gettime(struct thread *td,
2240			struct freebsd32_clock_gettime_args *uap)
2241{
2242	struct timespec	ats;
2243	struct timespec32 ats32;
2244	int error;
2245
2246	error = kern_clock_gettime(td, uap->clock_id, &ats);
2247	if (error == 0) {
2248		CP(ats, ats32, tv_sec);
2249		CP(ats, ats32, tv_nsec);
2250		error = copyout(&ats32, uap->tp, sizeof(ats32));
2251	}
2252	return (error);
2253}
2254
2255int
2256freebsd32_clock_settime(struct thread *td,
2257			struct freebsd32_clock_settime_args *uap)
2258{
2259	struct timespec	ats;
2260	struct timespec32 ats32;
2261	int error;
2262
2263	error = copyin(uap->tp, &ats32, sizeof(ats32));
2264	if (error)
2265		return (error);
2266	CP(ats32, ats, tv_sec);
2267	CP(ats32, ats, tv_nsec);
2268
2269	return (kern_clock_settime(td, uap->clock_id, &ats));
2270}
2271
2272int
2273freebsd32_clock_getres(struct thread *td,
2274		       struct freebsd32_clock_getres_args *uap)
2275{
2276	struct timespec	ts;
2277	struct timespec32 ts32;
2278	int error;
2279
2280	if (uap->tp == NULL)
2281		return (0);
2282	error = kern_clock_getres(td, uap->clock_id, &ts);
2283	if (error == 0) {
2284		CP(ts, ts32, tv_sec);
2285		CP(ts, ts32, tv_nsec);
2286		error = copyout(&ts32, uap->tp, sizeof(ts32));
2287	}
2288	return (error);
2289}
2290
2291int
2292freebsd32_thr_new(struct thread *td,
2293		  struct freebsd32_thr_new_args *uap)
2294{
2295	struct thr_param32 param32;
2296	struct thr_param param;
2297	int error;
2298
2299	if (uap->param_size < 0 ||
2300	    uap->param_size > sizeof(struct thr_param32))
2301		return (EINVAL);
2302	bzero(&param, sizeof(struct thr_param));
2303	bzero(&param32, sizeof(struct thr_param32));
2304	error = copyin(uap->param, &param32, uap->param_size);
2305	if (error != 0)
2306		return (error);
2307	param.start_func = PTRIN(param32.start_func);
2308	param.arg = PTRIN(param32.arg);
2309	param.stack_base = PTRIN(param32.stack_base);
2310	param.stack_size = param32.stack_size;
2311	param.tls_base = PTRIN(param32.tls_base);
2312	param.tls_size = param32.tls_size;
2313	param.child_tid = PTRIN(param32.child_tid);
2314	param.parent_tid = PTRIN(param32.parent_tid);
2315	param.flags = param32.flags;
2316	param.rtp = PTRIN(param32.rtp);
2317	param.spare[0] = PTRIN(param32.spare[0]);
2318	param.spare[1] = PTRIN(param32.spare[1]);
2319	param.spare[2] = PTRIN(param32.spare[2]);
2320
2321	return (kern_thr_new(td, &param));
2322}
2323
2324int
2325freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2326{
2327	struct timespec32 ts32;
2328	struct timespec ts, *tsp;
2329	int error;
2330
2331	error = 0;
2332	tsp = NULL;
2333	if (uap->timeout != NULL) {
2334		error = copyin((const void *)uap->timeout, (void *)&ts32,
2335		    sizeof(struct timespec32));
2336		if (error != 0)
2337			return (error);
2338		ts.tv_sec = ts32.tv_sec;
2339		ts.tv_nsec = ts32.tv_nsec;
2340		tsp = &ts;
2341	}
2342	return (kern_thr_suspend(td, tsp));
2343}
2344
2345void
2346siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2347{
2348	bzero(dst, sizeof(*dst));
2349	dst->si_signo = src->si_signo;
2350	dst->si_errno = src->si_errno;
2351	dst->si_code = src->si_code;
2352	dst->si_pid = src->si_pid;
2353	dst->si_uid = src->si_uid;
2354	dst->si_status = src->si_status;
2355	dst->si_addr = (uintptr_t)src->si_addr;
2356	dst->si_value.sigval_int = src->si_value.sival_int;
2357	dst->si_timerid = src->si_timerid;
2358	dst->si_overrun = src->si_overrun;
2359}
2360
2361int
2362freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2363{
2364	struct timespec32 ts32;
2365	struct timespec ts;
2366	struct timespec *timeout;
2367	sigset_t set;
2368	ksiginfo_t ksi;
2369	struct siginfo32 si32;
2370	int error;
2371
2372	if (uap->timeout) {
2373		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2374		if (error)
2375			return (error);
2376		ts.tv_sec = ts32.tv_sec;
2377		ts.tv_nsec = ts32.tv_nsec;
2378		timeout = &ts;
2379	} else
2380		timeout = NULL;
2381
2382	error = copyin(uap->set, &set, sizeof(set));
2383	if (error)
2384		return (error);
2385
2386	error = kern_sigtimedwait(td, set, &ksi, timeout);
2387	if (error)
2388		return (error);
2389
2390	if (uap->info) {
2391		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2392		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2393	}
2394
2395	if (error == 0)
2396		td->td_retval[0] = ksi.ksi_signo;
2397	return (error);
2398}
2399
2400/*
2401 * MPSAFE
2402 */
2403int
2404freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2405{
2406	ksiginfo_t ksi;
2407	struct siginfo32 si32;
2408	sigset_t set;
2409	int error;
2410
2411	error = copyin(uap->set, &set, sizeof(set));
2412	if (error)
2413		return (error);
2414
2415	error = kern_sigtimedwait(td, set, &ksi, NULL);
2416	if (error)
2417		return (error);
2418
2419	if (uap->info) {
2420		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2421		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2422	}
2423	if (error == 0)
2424		td->td_retval[0] = ksi.ksi_signo;
2425	return (error);
2426}
2427
2428int
2429freebsd32_cpuset_setid(struct thread *td,
2430    struct freebsd32_cpuset_setid_args *uap)
2431{
2432	struct cpuset_setid_args ap;
2433
2434	ap.which = uap->which;
2435	ap.id = PAIR32TO64(id_t,uap->id);
2436	ap.setid = uap->setid;
2437
2438	return (sys_cpuset_setid(td, &ap));
2439}
2440
2441int
2442freebsd32_cpuset_getid(struct thread *td,
2443    struct freebsd32_cpuset_getid_args *uap)
2444{
2445	struct cpuset_getid_args ap;
2446
2447	ap.level = uap->level;
2448	ap.which = uap->which;
2449	ap.id = PAIR32TO64(id_t,uap->id);
2450	ap.setid = uap->setid;
2451
2452	return (sys_cpuset_getid(td, &ap));
2453}
2454
2455int
2456freebsd32_cpuset_getaffinity(struct thread *td,
2457    struct freebsd32_cpuset_getaffinity_args *uap)
2458{
2459	struct cpuset_getaffinity_args ap;
2460
2461	ap.level = uap->level;
2462	ap.which = uap->which;
2463	ap.id = PAIR32TO64(id_t,uap->id);
2464	ap.cpusetsize = uap->cpusetsize;
2465	ap.mask = uap->mask;
2466
2467	return (sys_cpuset_getaffinity(td, &ap));
2468}
2469
2470int
2471freebsd32_cpuset_setaffinity(struct thread *td,
2472    struct freebsd32_cpuset_setaffinity_args *uap)
2473{
2474	struct cpuset_setaffinity_args ap;
2475
2476	ap.level = uap->level;
2477	ap.which = uap->which;
2478	ap.id = PAIR32TO64(id_t,uap->id);
2479	ap.cpusetsize = uap->cpusetsize;
2480	ap.mask = uap->mask;
2481
2482	return (sys_cpuset_setaffinity(td, &ap));
2483}
2484
2485int
2486freebsd32_nmount(struct thread *td,
2487    struct freebsd32_nmount_args /* {
2488    	struct iovec *iovp;
2489    	unsigned int iovcnt;
2490    	int flags;
2491    } */ *uap)
2492{
2493	struct uio *auio;
2494	uint64_t flags;
2495	int error;
2496
2497	/*
2498	 * Mount flags are now 64-bits. On 32-bit archtectures only
2499	 * 32-bits are passed in, but from here on everything handles
2500	 * 64-bit flags correctly.
2501	 */
2502	flags = uap->flags;
2503
2504	AUDIT_ARG_FFLAGS(flags);
2505
2506	/*
2507	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2508	 * userspace to set this flag, but we must filter it out if we want
2509	 * MNT_UPDATE on the root file system to work.
2510	 * MNT_ROOTFS should only be set by the kernel when mounting its
2511	 * root file system.
2512	 */
2513	flags &= ~MNT_ROOTFS;
2514
2515	/*
2516	 * check that we have an even number of iovec's
2517	 * and that we have at least two options.
2518	 */
2519	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2520		return (EINVAL);
2521
2522	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2523	if (error)
2524		return (error);
2525	error = vfs_donmount(td, flags, auio);
2526
2527	free(auio, M_IOV);
2528	return error;
2529}
2530
2531#if 0
2532int
2533freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2534{
2535	struct yyy32 *p32, s32;
2536	struct yyy *p = NULL, s;
2537	struct xxx_arg ap;
2538	int error;
2539
2540	if (uap->zzz) {
2541		error = copyin(uap->zzz, &s32, sizeof(s32));
2542		if (error)
2543			return (error);
2544		/* translate in */
2545		p = &s;
2546	}
2547	error = kern_xxx(td, p);
2548	if (error)
2549		return (error);
2550	if (uap->zzz) {
2551		/* translate out */
2552		error = copyout(&s32, p32, sizeof(s32));
2553	}
2554	return (error);
2555}
2556#endif
2557
2558int
2559syscall32_register(int *offset, struct sysent *new_sysent,
2560    struct sysent *old_sysent)
2561{
2562	if (*offset == NO_SYSCALL) {
2563		int i;
2564
2565		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2566			if (freebsd32_sysent[i].sy_call ==
2567			    (sy_call_t *)lkmnosys)
2568				break;
2569		if (i == SYS_MAXSYSCALL)
2570			return (ENFILE);
2571		*offset = i;
2572	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2573		return (EINVAL);
2574	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2575	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2576		return (EEXIST);
2577
2578	*old_sysent = freebsd32_sysent[*offset];
2579	freebsd32_sysent[*offset] = *new_sysent;
2580	return 0;
2581}
2582
2583int
2584syscall32_deregister(int *offset, struct sysent *old_sysent)
2585{
2586
2587	if (*offset)
2588		freebsd32_sysent[*offset] = *old_sysent;
2589	return 0;
2590}
2591
2592int
2593syscall32_module_handler(struct module *mod, int what, void *arg)
2594{
2595	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2596	modspecific_t ms;
2597	int error;
2598
2599	switch (what) {
2600	case MOD_LOAD:
2601		error = syscall32_register(data->offset, data->new_sysent,
2602		    &data->old_sysent);
2603		if (error) {
2604			/* Leave a mark so we know to safely unload below. */
2605			data->offset = NULL;
2606			return error;
2607		}
2608		ms.intval = *data->offset;
2609		MOD_XLOCK;
2610		module_setspecific(mod, &ms);
2611		MOD_XUNLOCK;
2612		if (data->chainevh)
2613			error = data->chainevh(mod, what, data->chainarg);
2614		return (error);
2615	case MOD_UNLOAD:
2616		/*
2617		 * MOD_LOAD failed, so just return without calling the
2618		 * chained handler since we didn't pass along the MOD_LOAD
2619		 * event.
2620		 */
2621		if (data->offset == NULL)
2622			return (0);
2623		if (data->chainevh) {
2624			error = data->chainevh(mod, what, data->chainarg);
2625			if (error)
2626				return (error);
2627		}
2628		error = syscall32_deregister(data->offset, &data->old_sysent);
2629		return (error);
2630	default:
2631		error = EOPNOTSUPP;
2632		if (data->chainevh)
2633			error = data->chainevh(mod, what, data->chainarg);
2634		return (error);
2635	}
2636}
2637
2638int
2639syscall32_helper_register(struct syscall_helper_data *sd)
2640{
2641	struct syscall_helper_data *sd1;
2642	int error;
2643
2644	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2645		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2646		    &sd1->old_sysent);
2647		if (error != 0) {
2648			syscall32_helper_unregister(sd);
2649			return (error);
2650		}
2651		sd1->registered = 1;
2652	}
2653	return (0);
2654}
2655
2656int
2657syscall32_helper_unregister(struct syscall_helper_data *sd)
2658{
2659	struct syscall_helper_data *sd1;
2660
2661	for (sd1 = sd; sd1->registered != 0; sd1++) {
2662		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2663		sd1->registered = 0;
2664	}
2665	return (0);
2666}
2667
2668register_t *
2669freebsd32_copyout_strings(struct image_params *imgp)
2670{
2671	int argc, envc, i;
2672	u_int32_t *vectp;
2673	char *stringp, *destp;
2674	u_int32_t *stack_base;
2675	struct freebsd32_ps_strings *arginfo;
2676	char canary[sizeof(long) * 8];
2677	int32_t pagesizes32[MAXPAGESIZES];
2678	size_t execpath_len;
2679	int szsigcode;
2680
2681	/*
2682	 * Calculate string base and vector table pointers.
2683	 * Also deal with signal trampoline code for this exec type.
2684	 */
2685	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2686		execpath_len = strlen(imgp->execpath) + 1;
2687	else
2688		execpath_len = 0;
2689	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2690	    sv_psstrings;
2691	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2692		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2693	else
2694		szsigcode = 0;
2695	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2696	    roundup(execpath_len, sizeof(char *)) -
2697	    roundup(sizeof(canary), sizeof(char *)) -
2698	    roundup(sizeof(pagesizes32), sizeof(char *)) -
2699	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2700
2701	/*
2702	 * install sigcode
2703	 */
2704	if (szsigcode != 0)
2705		copyout(imgp->proc->p_sysent->sv_sigcode,
2706			((caddr_t)arginfo - szsigcode), szsigcode);
2707
2708	/*
2709	 * Copy the image path for the rtld.
2710	 */
2711	if (execpath_len != 0) {
2712		imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2713		copyout(imgp->execpath, (void *)imgp->execpathp,
2714		    execpath_len);
2715	}
2716
2717	/*
2718	 * Prepare the canary for SSP.
2719	 */
2720	arc4rand(canary, sizeof(canary), 0);
2721	imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2722	    sizeof(canary);
2723	copyout(canary, (void *)imgp->canary, sizeof(canary));
2724	imgp->canarylen = sizeof(canary);
2725
2726	/*
2727	 * Prepare the pagesizes array.
2728	 */
2729	for (i = 0; i < MAXPAGESIZES; i++)
2730		pagesizes32[i] = (uint32_t)pagesizes[i];
2731	imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2732	    roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2733	copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2734	imgp->pagesizeslen = sizeof(pagesizes32);
2735
2736	/*
2737	 * If we have a valid auxargs ptr, prepare some room
2738	 * on the stack.
2739	 */
2740	if (imgp->auxargs) {
2741		/*
2742		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2743		 * lower compatibility.
2744		 */
2745		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2746			: (AT_COUNT * 2);
2747		/*
2748		 * The '+ 2' is for the null pointers at the end of each of
2749		 * the arg and env vector sets,and imgp->auxarg_size is room
2750		 * for argument of Runtime loader.
2751		 */
2752		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2753		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2754		    sizeof(u_int32_t));
2755	} else
2756		/*
2757		 * The '+ 2' is for the null pointers at the end of each of
2758		 * the arg and env vector sets
2759		 */
2760		vectp = (u_int32_t *)
2761			(destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2762
2763	/*
2764	 * vectp also becomes our initial stack base
2765	 */
2766	stack_base = vectp;
2767
2768	stringp = imgp->args->begin_argv;
2769	argc = imgp->args->argc;
2770	envc = imgp->args->envc;
2771	/*
2772	 * Copy out strings - arguments and environment.
2773	 */
2774	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2775
2776	/*
2777	 * Fill in "ps_strings" struct for ps, w, etc.
2778	 */
2779	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2780	suword32(&arginfo->ps_nargvstr, argc);
2781
2782	/*
2783	 * Fill in argument portion of vector table.
2784	 */
2785	for (; argc > 0; --argc) {
2786		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2787		while (*stringp++ != 0)
2788			destp++;
2789		destp++;
2790	}
2791
2792	/* a null vector table pointer separates the argp's from the envp's */
2793	suword32(vectp++, 0);
2794
2795	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2796	suword32(&arginfo->ps_nenvstr, envc);
2797
2798	/*
2799	 * Fill in environment portion of vector table.
2800	 */
2801	for (; envc > 0; --envc) {
2802		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2803		while (*stringp++ != 0)
2804			destp++;
2805		destp++;
2806	}
2807
2808	/* end of vector table is a null pointer */
2809	suword32(vectp, 0);
2810
2811	return ((register_t *)stack_base);
2812}
2813
2814int
2815freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2816{
2817	struct kld_file_stat stat;
2818	struct kld32_file_stat stat32;
2819	int error, version;
2820
2821	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2822	    != 0)
2823		return (error);
2824	if (version != sizeof(struct kld32_file_stat_1) &&
2825	    version != sizeof(struct kld32_file_stat))
2826		return (EINVAL);
2827
2828	error = kern_kldstat(td, uap->fileid, &stat);
2829	if (error != 0)
2830		return (error);
2831
2832	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2833	CP(stat, stat32, refs);
2834	CP(stat, stat32, id);
2835	PTROUT_CP(stat, stat32, address);
2836	CP(stat, stat32, size);
2837	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2838	return (copyout(&stat32, uap->stat, version));
2839}
2840
2841int
2842freebsd32_posix_fallocate(struct thread *td,
2843    struct freebsd32_posix_fallocate_args *uap)
2844{
2845
2846	return (kern_posix_fallocate(td, uap->fd,
2847	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2848}
2849
2850int
2851freebsd32_posix_fadvise(struct thread *td,
2852    struct freebsd32_posix_fadvise_args *uap)
2853{
2854
2855	return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2856	    PAIR32TO64(off_t, uap->len), uap->advice));
2857}
2858