freebsd32_misc.c revision 235886
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 235886 2012-05-24 08:00:26Z gleb $");
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	    NULL, UIO_USERSPACE);
1533	if (error)
1534		return (error);
1535	if (uap->basep != NULL) {
1536		base32 = base;
1537		error = copyout(&base32, uap->basep, sizeof(int32_t));
1538	}
1539	return (error);
1540}
1541
1542#ifdef COMPAT_FREEBSD6
1543/* versions with the 'int pad' argument */
1544int
1545freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1546{
1547	struct pread_args ap;
1548
1549	ap.fd = uap->fd;
1550	ap.buf = uap->buf;
1551	ap.nbyte = uap->nbyte;
1552	ap.offset = PAIR32TO64(off_t,uap->offset);
1553	return (sys_pread(td, &ap));
1554}
1555
1556int
1557freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1558{
1559	struct pwrite_args ap;
1560
1561	ap.fd = uap->fd;
1562	ap.buf = uap->buf;
1563	ap.nbyte = uap->nbyte;
1564	ap.offset = PAIR32TO64(off_t,uap->offset);
1565	return (sys_pwrite(td, &ap));
1566}
1567
1568int
1569freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1570{
1571	int error;
1572	struct lseek_args ap;
1573	off_t pos;
1574
1575	ap.fd = uap->fd;
1576	ap.offset = PAIR32TO64(off_t,uap->offset);
1577	ap.whence = uap->whence;
1578	error = sys_lseek(td, &ap);
1579	/* Expand the quad return into two parts for eax and edx */
1580	pos = *(off_t *)(td->td_retval);
1581	td->td_retval[RETVAL_LO] = pos & 0xffffffff;	/* %eax */
1582	td->td_retval[RETVAL_HI] = pos >> 32;		/* %edx */
1583	return error;
1584}
1585
1586int
1587freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1588{
1589	struct truncate_args ap;
1590
1591	ap.path = uap->path;
1592	ap.length = PAIR32TO64(off_t,uap->length);
1593	return (sys_truncate(td, &ap));
1594}
1595
1596int
1597freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1598{
1599	struct ftruncate_args ap;
1600
1601	ap.fd = uap->fd;
1602	ap.length = PAIR32TO64(off_t,uap->length);
1603	return (sys_ftruncate(td, &ap));
1604}
1605#endif /* COMPAT_FREEBSD6 */
1606
1607struct sf_hdtr32 {
1608	uint32_t headers;
1609	int hdr_cnt;
1610	uint32_t trailers;
1611	int trl_cnt;
1612};
1613
1614static int
1615freebsd32_do_sendfile(struct thread *td,
1616    struct freebsd32_sendfile_args *uap, int compat)
1617{
1618	struct sendfile_args ap;
1619	struct sf_hdtr32 hdtr32;
1620	struct sf_hdtr hdtr;
1621	struct uio *hdr_uio, *trl_uio;
1622	struct iovec32 *iov32;
1623	int error;
1624
1625	hdr_uio = trl_uio = NULL;
1626
1627	ap.fd = uap->fd;
1628	ap.s = uap->s;
1629	ap.offset = PAIR32TO64(off_t,uap->offset);
1630	ap.nbytes = uap->nbytes;
1631	ap.hdtr = (struct sf_hdtr *)uap->hdtr;		/* XXX not used */
1632	ap.sbytes = uap->sbytes;
1633	ap.flags = uap->flags;
1634
1635	if (uap->hdtr != NULL) {
1636		error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1637		if (error)
1638			goto out;
1639		PTRIN_CP(hdtr32, hdtr, headers);
1640		CP(hdtr32, hdtr, hdr_cnt);
1641		PTRIN_CP(hdtr32, hdtr, trailers);
1642		CP(hdtr32, hdtr, trl_cnt);
1643
1644		if (hdtr.headers != NULL) {
1645			iov32 = PTRIN(hdtr32.headers);
1646			error = freebsd32_copyinuio(iov32,
1647			    hdtr32.hdr_cnt, &hdr_uio);
1648			if (error)
1649				goto out;
1650		}
1651		if (hdtr.trailers != NULL) {
1652			iov32 = PTRIN(hdtr32.trailers);
1653			error = freebsd32_copyinuio(iov32,
1654			    hdtr32.trl_cnt, &trl_uio);
1655			if (error)
1656				goto out;
1657		}
1658	}
1659
1660	error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1661out:
1662	if (hdr_uio)
1663		free(hdr_uio, M_IOV);
1664	if (trl_uio)
1665		free(trl_uio, M_IOV);
1666	return (error);
1667}
1668
1669#ifdef COMPAT_FREEBSD4
1670int
1671freebsd4_freebsd32_sendfile(struct thread *td,
1672    struct freebsd4_freebsd32_sendfile_args *uap)
1673{
1674	return (freebsd32_do_sendfile(td,
1675	    (struct freebsd32_sendfile_args *)uap, 1));
1676}
1677#endif
1678
1679int
1680freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1681{
1682
1683	return (freebsd32_do_sendfile(td, uap, 0));
1684}
1685
1686static void
1687copy_stat(struct stat *in, struct stat32 *out)
1688{
1689
1690	CP(*in, *out, st_dev);
1691	CP(*in, *out, st_ino);
1692	CP(*in, *out, st_mode);
1693	CP(*in, *out, st_nlink);
1694	CP(*in, *out, st_uid);
1695	CP(*in, *out, st_gid);
1696	CP(*in, *out, st_rdev);
1697	TS_CP(*in, *out, st_atim);
1698	TS_CP(*in, *out, st_mtim);
1699	TS_CP(*in, *out, st_ctim);
1700	CP(*in, *out, st_size);
1701	CP(*in, *out, st_blocks);
1702	CP(*in, *out, st_blksize);
1703	CP(*in, *out, st_flags);
1704	CP(*in, *out, st_gen);
1705	TS_CP(*in, *out, st_birthtim);
1706}
1707
1708#ifdef COMPAT_43
1709static void
1710copy_ostat(struct stat *in, struct ostat32 *out)
1711{
1712
1713	CP(*in, *out, st_dev);
1714	CP(*in, *out, st_ino);
1715	CP(*in, *out, st_mode);
1716	CP(*in, *out, st_nlink);
1717	CP(*in, *out, st_uid);
1718	CP(*in, *out, st_gid);
1719	CP(*in, *out, st_rdev);
1720	CP(*in, *out, st_size);
1721	TS_CP(*in, *out, st_atim);
1722	TS_CP(*in, *out, st_mtim);
1723	TS_CP(*in, *out, st_ctim);
1724	CP(*in, *out, st_blksize);
1725	CP(*in, *out, st_blocks);
1726	CP(*in, *out, st_flags);
1727	CP(*in, *out, st_gen);
1728}
1729#endif
1730
1731int
1732freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1733{
1734	struct stat sb;
1735	struct stat32 sb32;
1736	int error;
1737
1738	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1739	if (error)
1740		return (error);
1741	copy_stat(&sb, &sb32);
1742	error = copyout(&sb32, uap->ub, sizeof (sb32));
1743	return (error);
1744}
1745
1746#ifdef COMPAT_43
1747int
1748ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1749{
1750	struct stat sb;
1751	struct ostat32 sb32;
1752	int error;
1753
1754	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1755	if (error)
1756		return (error);
1757	copy_ostat(&sb, &sb32);
1758	error = copyout(&sb32, uap->ub, sizeof (sb32));
1759	return (error);
1760}
1761#endif
1762
1763int
1764freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1765{
1766	struct stat ub;
1767	struct stat32 ub32;
1768	int error;
1769
1770	error = kern_fstat(td, uap->fd, &ub);
1771	if (error)
1772		return (error);
1773	copy_stat(&ub, &ub32);
1774	error = copyout(&ub32, uap->ub, sizeof(ub32));
1775	return (error);
1776}
1777
1778#ifdef COMPAT_43
1779int
1780ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1781{
1782	struct stat ub;
1783	struct ostat32 ub32;
1784	int error;
1785
1786	error = kern_fstat(td, uap->fd, &ub);
1787	if (error)
1788		return (error);
1789	copy_ostat(&ub, &ub32);
1790	error = copyout(&ub32, uap->ub, sizeof(ub32));
1791	return (error);
1792}
1793#endif
1794
1795int
1796freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1797{
1798	struct stat ub;
1799	struct stat32 ub32;
1800	int error;
1801
1802	error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1803	if (error)
1804		return (error);
1805	copy_stat(&ub, &ub32);
1806	error = copyout(&ub32, uap->buf, sizeof(ub32));
1807	return (error);
1808}
1809
1810int
1811freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1812{
1813	struct stat sb;
1814	struct stat32 sb32;
1815	int error;
1816
1817	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1818	if (error)
1819		return (error);
1820	copy_stat(&sb, &sb32);
1821	error = copyout(&sb32, uap->ub, sizeof (sb32));
1822	return (error);
1823}
1824
1825#ifdef COMPAT_43
1826int
1827ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1828{
1829	struct stat sb;
1830	struct ostat32 sb32;
1831	int error;
1832
1833	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1834	if (error)
1835		return (error);
1836	copy_ostat(&sb, &sb32);
1837	error = copyout(&sb32, uap->ub, sizeof (sb32));
1838	return (error);
1839}
1840#endif
1841
1842int
1843freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1844{
1845	int error, name[CTL_MAXNAME];
1846	size_t j, oldlen;
1847
1848	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1849		return (EINVAL);
1850 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1851 	if (error)
1852		return (error);
1853	if (uap->oldlenp)
1854		oldlen = fuword32(uap->oldlenp);
1855	else
1856		oldlen = 0;
1857	error = userland_sysctl(td, name, uap->namelen,
1858		uap->old, &oldlen, 1,
1859		uap->new, uap->newlen, &j, SCTL_MASK32);
1860	if (error && error != ENOMEM)
1861		return (error);
1862	if (uap->oldlenp)
1863		suword32(uap->oldlenp, j);
1864	return (0);
1865}
1866
1867int
1868freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1869{
1870	uint32_t version;
1871	int error;
1872	struct jail j;
1873
1874	error = copyin(uap->jail, &version, sizeof(uint32_t));
1875	if (error)
1876		return (error);
1877
1878	switch (version) {
1879	case 0:
1880	{
1881		/* FreeBSD single IPv4 jails. */
1882		struct jail32_v0 j32_v0;
1883
1884		bzero(&j, sizeof(struct jail));
1885		error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1886		if (error)
1887			return (error);
1888		CP(j32_v0, j, version);
1889		PTRIN_CP(j32_v0, j, path);
1890		PTRIN_CP(j32_v0, j, hostname);
1891		j.ip4s = j32_v0.ip_number;
1892		break;
1893	}
1894
1895	case 1:
1896		/*
1897		 * Version 1 was used by multi-IPv4 jail implementations
1898		 * that never made it into the official kernel.
1899		 */
1900		return (EINVAL);
1901
1902	case 2:	/* JAIL_API_VERSION */
1903	{
1904		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
1905		struct jail32 j32;
1906
1907		error = copyin(uap->jail, &j32, sizeof(struct jail32));
1908		if (error)
1909			return (error);
1910		CP(j32, j, version);
1911		PTRIN_CP(j32, j, path);
1912		PTRIN_CP(j32, j, hostname);
1913		PTRIN_CP(j32, j, jailname);
1914		CP(j32, j, ip4s);
1915		CP(j32, j, ip6s);
1916		PTRIN_CP(j32, j, ip4);
1917		PTRIN_CP(j32, j, ip6);
1918		break;
1919	}
1920
1921	default:
1922		/* Sci-Fi jails are not supported, sorry. */
1923		return (EINVAL);
1924	}
1925	return (kern_jail(td, &j));
1926}
1927
1928int
1929freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1930{
1931	struct uio *auio;
1932	int error;
1933
1934	/* Check that we have an even number of iovecs. */
1935	if (uap->iovcnt & 1)
1936		return (EINVAL);
1937
1938	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1939	if (error)
1940		return (error);
1941	error = kern_jail_set(td, auio, uap->flags);
1942	free(auio, M_IOV);
1943	return (error);
1944}
1945
1946int
1947freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1948{
1949	struct iovec32 iov32;
1950	struct uio *auio;
1951	int error, i;
1952
1953	/* Check that we have an even number of iovecs. */
1954	if (uap->iovcnt & 1)
1955		return (EINVAL);
1956
1957	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1958	if (error)
1959		return (error);
1960	error = kern_jail_get(td, auio, uap->flags);
1961	if (error == 0)
1962		for (i = 0; i < uap->iovcnt; i++) {
1963			PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1964			CP(auio->uio_iov[i], iov32, iov_len);
1965			error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1966			if (error != 0)
1967				break;
1968		}
1969	free(auio, M_IOV);
1970	return (error);
1971}
1972
1973int
1974freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1975{
1976	struct sigaction32 s32;
1977	struct sigaction sa, osa, *sap;
1978	int error;
1979
1980	if (uap->act) {
1981		error = copyin(uap->act, &s32, sizeof(s32));
1982		if (error)
1983			return (error);
1984		sa.sa_handler = PTRIN(s32.sa_u);
1985		CP(s32, sa, sa_flags);
1986		CP(s32, sa, sa_mask);
1987		sap = &sa;
1988	} else
1989		sap = NULL;
1990	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1991	if (error == 0 && uap->oact != NULL) {
1992		s32.sa_u = PTROUT(osa.sa_handler);
1993		CP(osa, s32, sa_flags);
1994		CP(osa, s32, sa_mask);
1995		error = copyout(&s32, uap->oact, sizeof(s32));
1996	}
1997	return (error);
1998}
1999
2000#ifdef COMPAT_FREEBSD4
2001int
2002freebsd4_freebsd32_sigaction(struct thread *td,
2003			     struct freebsd4_freebsd32_sigaction_args *uap)
2004{
2005	struct sigaction32 s32;
2006	struct sigaction sa, osa, *sap;
2007	int error;
2008
2009	if (uap->act) {
2010		error = copyin(uap->act, &s32, sizeof(s32));
2011		if (error)
2012			return (error);
2013		sa.sa_handler = PTRIN(s32.sa_u);
2014		CP(s32, sa, sa_flags);
2015		CP(s32, sa, sa_mask);
2016		sap = &sa;
2017	} else
2018		sap = NULL;
2019	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2020	if (error == 0 && uap->oact != NULL) {
2021		s32.sa_u = PTROUT(osa.sa_handler);
2022		CP(osa, s32, sa_flags);
2023		CP(osa, s32, sa_mask);
2024		error = copyout(&s32, uap->oact, sizeof(s32));
2025	}
2026	return (error);
2027}
2028#endif
2029
2030#ifdef COMPAT_43
2031struct osigaction32 {
2032	u_int32_t	sa_u;
2033	osigset_t	sa_mask;
2034	int		sa_flags;
2035};
2036
2037#define	ONSIG	32
2038
2039int
2040ofreebsd32_sigaction(struct thread *td,
2041			     struct ofreebsd32_sigaction_args *uap)
2042{
2043	struct osigaction32 s32;
2044	struct sigaction sa, osa, *sap;
2045	int error;
2046
2047	if (uap->signum <= 0 || uap->signum >= ONSIG)
2048		return (EINVAL);
2049
2050	if (uap->nsa) {
2051		error = copyin(uap->nsa, &s32, sizeof(s32));
2052		if (error)
2053			return (error);
2054		sa.sa_handler = PTRIN(s32.sa_u);
2055		CP(s32, sa, sa_flags);
2056		OSIG2SIG(s32.sa_mask, sa.sa_mask);
2057		sap = &sa;
2058	} else
2059		sap = NULL;
2060	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2061	if (error == 0 && uap->osa != NULL) {
2062		s32.sa_u = PTROUT(osa.sa_handler);
2063		CP(osa, s32, sa_flags);
2064		SIG2OSIG(osa.sa_mask, s32.sa_mask);
2065		error = copyout(&s32, uap->osa, sizeof(s32));
2066	}
2067	return (error);
2068}
2069
2070int
2071ofreebsd32_sigprocmask(struct thread *td,
2072			       struct ofreebsd32_sigprocmask_args *uap)
2073{
2074	sigset_t set, oset;
2075	int error;
2076
2077	OSIG2SIG(uap->mask, set);
2078	error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2079	SIG2OSIG(oset, td->td_retval[0]);
2080	return (error);
2081}
2082
2083int
2084ofreebsd32_sigpending(struct thread *td,
2085			      struct ofreebsd32_sigpending_args *uap)
2086{
2087	struct proc *p = td->td_proc;
2088	sigset_t siglist;
2089
2090	PROC_LOCK(p);
2091	siglist = p->p_siglist;
2092	SIGSETOR(siglist, td->td_siglist);
2093	PROC_UNLOCK(p);
2094	SIG2OSIG(siglist, td->td_retval[0]);
2095	return (0);
2096}
2097
2098struct sigvec32 {
2099	u_int32_t	sv_handler;
2100	int		sv_mask;
2101	int		sv_flags;
2102};
2103
2104int
2105ofreebsd32_sigvec(struct thread *td,
2106			  struct ofreebsd32_sigvec_args *uap)
2107{
2108	struct sigvec32 vec;
2109	struct sigaction sa, osa, *sap;
2110	int error;
2111
2112	if (uap->signum <= 0 || uap->signum >= ONSIG)
2113		return (EINVAL);
2114
2115	if (uap->nsv) {
2116		error = copyin(uap->nsv, &vec, sizeof(vec));
2117		if (error)
2118			return (error);
2119		sa.sa_handler = PTRIN(vec.sv_handler);
2120		OSIG2SIG(vec.sv_mask, sa.sa_mask);
2121		sa.sa_flags = vec.sv_flags;
2122		sa.sa_flags ^= SA_RESTART;
2123		sap = &sa;
2124	} else
2125		sap = NULL;
2126	error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2127	if (error == 0 && uap->osv != NULL) {
2128		vec.sv_handler = PTROUT(osa.sa_handler);
2129		SIG2OSIG(osa.sa_mask, vec.sv_mask);
2130		vec.sv_flags = osa.sa_flags;
2131		vec.sv_flags &= ~SA_NOCLDWAIT;
2132		vec.sv_flags ^= SA_RESTART;
2133		error = copyout(&vec, uap->osv, sizeof(vec));
2134	}
2135	return (error);
2136}
2137
2138int
2139ofreebsd32_sigblock(struct thread *td,
2140			    struct ofreebsd32_sigblock_args *uap)
2141{
2142	sigset_t set, oset;
2143
2144	OSIG2SIG(uap->mask, set);
2145	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2146	SIG2OSIG(oset, td->td_retval[0]);
2147	return (0);
2148}
2149
2150int
2151ofreebsd32_sigsetmask(struct thread *td,
2152			      struct ofreebsd32_sigsetmask_args *uap)
2153{
2154	sigset_t set, oset;
2155
2156	OSIG2SIG(uap->mask, set);
2157	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2158	SIG2OSIG(oset, td->td_retval[0]);
2159	return (0);
2160}
2161
2162int
2163ofreebsd32_sigsuspend(struct thread *td,
2164			      struct ofreebsd32_sigsuspend_args *uap)
2165{
2166	sigset_t mask;
2167
2168	OSIG2SIG(uap->mask, mask);
2169	return (kern_sigsuspend(td, mask));
2170}
2171
2172struct sigstack32 {
2173	u_int32_t	ss_sp;
2174	int		ss_onstack;
2175};
2176
2177int
2178ofreebsd32_sigstack(struct thread *td,
2179			    struct ofreebsd32_sigstack_args *uap)
2180{
2181	struct sigstack32 s32;
2182	struct sigstack nss, oss;
2183	int error = 0, unss;
2184
2185	if (uap->nss != NULL) {
2186		error = copyin(uap->nss, &s32, sizeof(s32));
2187		if (error)
2188			return (error);
2189		nss.ss_sp = PTRIN(s32.ss_sp);
2190		CP(s32, nss, ss_onstack);
2191		unss = 1;
2192	} else {
2193		unss = 0;
2194	}
2195	oss.ss_sp = td->td_sigstk.ss_sp;
2196	oss.ss_onstack = sigonstack(cpu_getstack(td));
2197	if (unss) {
2198		td->td_sigstk.ss_sp = nss.ss_sp;
2199		td->td_sigstk.ss_size = 0;
2200		td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2201		td->td_pflags |= TDP_ALTSTACK;
2202	}
2203	if (uap->oss != NULL) {
2204		s32.ss_sp = PTROUT(oss.ss_sp);
2205		CP(oss, s32, ss_onstack);
2206		error = copyout(&s32, uap->oss, sizeof(s32));
2207	}
2208	return (error);
2209}
2210#endif
2211
2212int
2213freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2214{
2215	struct timespec32 rmt32, rqt32;
2216	struct timespec rmt, rqt;
2217	int error;
2218
2219	error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2220	if (error)
2221		return (error);
2222
2223	CP(rqt32, rqt, tv_sec);
2224	CP(rqt32, rqt, tv_nsec);
2225
2226	if (uap->rmtp &&
2227	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2228		return (EFAULT);
2229	error = kern_nanosleep(td, &rqt, &rmt);
2230	if (error && uap->rmtp) {
2231		int error2;
2232
2233		CP(rmt, rmt32, tv_sec);
2234		CP(rmt, rmt32, tv_nsec);
2235
2236		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2237		if (error2)
2238			error = error2;
2239	}
2240	return (error);
2241}
2242
2243int
2244freebsd32_clock_gettime(struct thread *td,
2245			struct freebsd32_clock_gettime_args *uap)
2246{
2247	struct timespec	ats;
2248	struct timespec32 ats32;
2249	int error;
2250
2251	error = kern_clock_gettime(td, uap->clock_id, &ats);
2252	if (error == 0) {
2253		CP(ats, ats32, tv_sec);
2254		CP(ats, ats32, tv_nsec);
2255		error = copyout(&ats32, uap->tp, sizeof(ats32));
2256	}
2257	return (error);
2258}
2259
2260int
2261freebsd32_clock_settime(struct thread *td,
2262			struct freebsd32_clock_settime_args *uap)
2263{
2264	struct timespec	ats;
2265	struct timespec32 ats32;
2266	int error;
2267
2268	error = copyin(uap->tp, &ats32, sizeof(ats32));
2269	if (error)
2270		return (error);
2271	CP(ats32, ats, tv_sec);
2272	CP(ats32, ats, tv_nsec);
2273
2274	return (kern_clock_settime(td, uap->clock_id, &ats));
2275}
2276
2277int
2278freebsd32_clock_getres(struct thread *td,
2279		       struct freebsd32_clock_getres_args *uap)
2280{
2281	struct timespec	ts;
2282	struct timespec32 ts32;
2283	int error;
2284
2285	if (uap->tp == NULL)
2286		return (0);
2287	error = kern_clock_getres(td, uap->clock_id, &ts);
2288	if (error == 0) {
2289		CP(ts, ts32, tv_sec);
2290		CP(ts, ts32, tv_nsec);
2291		error = copyout(&ts32, uap->tp, sizeof(ts32));
2292	}
2293	return (error);
2294}
2295
2296int
2297freebsd32_thr_new(struct thread *td,
2298		  struct freebsd32_thr_new_args *uap)
2299{
2300	struct thr_param32 param32;
2301	struct thr_param param;
2302	int error;
2303
2304	if (uap->param_size < 0 ||
2305	    uap->param_size > sizeof(struct thr_param32))
2306		return (EINVAL);
2307	bzero(&param, sizeof(struct thr_param));
2308	bzero(&param32, sizeof(struct thr_param32));
2309	error = copyin(uap->param, &param32, uap->param_size);
2310	if (error != 0)
2311		return (error);
2312	param.start_func = PTRIN(param32.start_func);
2313	param.arg = PTRIN(param32.arg);
2314	param.stack_base = PTRIN(param32.stack_base);
2315	param.stack_size = param32.stack_size;
2316	param.tls_base = PTRIN(param32.tls_base);
2317	param.tls_size = param32.tls_size;
2318	param.child_tid = PTRIN(param32.child_tid);
2319	param.parent_tid = PTRIN(param32.parent_tid);
2320	param.flags = param32.flags;
2321	param.rtp = PTRIN(param32.rtp);
2322	param.spare[0] = PTRIN(param32.spare[0]);
2323	param.spare[1] = PTRIN(param32.spare[1]);
2324	param.spare[2] = PTRIN(param32.spare[2]);
2325
2326	return (kern_thr_new(td, &param));
2327}
2328
2329int
2330freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2331{
2332	struct timespec32 ts32;
2333	struct timespec ts, *tsp;
2334	int error;
2335
2336	error = 0;
2337	tsp = NULL;
2338	if (uap->timeout != NULL) {
2339		error = copyin((const void *)uap->timeout, (void *)&ts32,
2340		    sizeof(struct timespec32));
2341		if (error != 0)
2342			return (error);
2343		ts.tv_sec = ts32.tv_sec;
2344		ts.tv_nsec = ts32.tv_nsec;
2345		tsp = &ts;
2346	}
2347	return (kern_thr_suspend(td, tsp));
2348}
2349
2350void
2351siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2352{
2353	bzero(dst, sizeof(*dst));
2354	dst->si_signo = src->si_signo;
2355	dst->si_errno = src->si_errno;
2356	dst->si_code = src->si_code;
2357	dst->si_pid = src->si_pid;
2358	dst->si_uid = src->si_uid;
2359	dst->si_status = src->si_status;
2360	dst->si_addr = (uintptr_t)src->si_addr;
2361	dst->si_value.sigval_int = src->si_value.sival_int;
2362	dst->si_timerid = src->si_timerid;
2363	dst->si_overrun = src->si_overrun;
2364}
2365
2366int
2367freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2368{
2369	struct timespec32 ts32;
2370	struct timespec ts;
2371	struct timespec *timeout;
2372	sigset_t set;
2373	ksiginfo_t ksi;
2374	struct siginfo32 si32;
2375	int error;
2376
2377	if (uap->timeout) {
2378		error = copyin(uap->timeout, &ts32, sizeof(ts32));
2379		if (error)
2380			return (error);
2381		ts.tv_sec = ts32.tv_sec;
2382		ts.tv_nsec = ts32.tv_nsec;
2383		timeout = &ts;
2384	} else
2385		timeout = NULL;
2386
2387	error = copyin(uap->set, &set, sizeof(set));
2388	if (error)
2389		return (error);
2390
2391	error = kern_sigtimedwait(td, set, &ksi, timeout);
2392	if (error)
2393		return (error);
2394
2395	if (uap->info) {
2396		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2397		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2398	}
2399
2400	if (error == 0)
2401		td->td_retval[0] = ksi.ksi_signo;
2402	return (error);
2403}
2404
2405/*
2406 * MPSAFE
2407 */
2408int
2409freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2410{
2411	ksiginfo_t ksi;
2412	struct siginfo32 si32;
2413	sigset_t set;
2414	int error;
2415
2416	error = copyin(uap->set, &set, sizeof(set));
2417	if (error)
2418		return (error);
2419
2420	error = kern_sigtimedwait(td, set, &ksi, NULL);
2421	if (error)
2422		return (error);
2423
2424	if (uap->info) {
2425		siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2426		error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2427	}
2428	if (error == 0)
2429		td->td_retval[0] = ksi.ksi_signo;
2430	return (error);
2431}
2432
2433int
2434freebsd32_cpuset_setid(struct thread *td,
2435    struct freebsd32_cpuset_setid_args *uap)
2436{
2437	struct cpuset_setid_args ap;
2438
2439	ap.which = uap->which;
2440	ap.id = PAIR32TO64(id_t,uap->id);
2441	ap.setid = uap->setid;
2442
2443	return (sys_cpuset_setid(td, &ap));
2444}
2445
2446int
2447freebsd32_cpuset_getid(struct thread *td,
2448    struct freebsd32_cpuset_getid_args *uap)
2449{
2450	struct cpuset_getid_args ap;
2451
2452	ap.level = uap->level;
2453	ap.which = uap->which;
2454	ap.id = PAIR32TO64(id_t,uap->id);
2455	ap.setid = uap->setid;
2456
2457	return (sys_cpuset_getid(td, &ap));
2458}
2459
2460int
2461freebsd32_cpuset_getaffinity(struct thread *td,
2462    struct freebsd32_cpuset_getaffinity_args *uap)
2463{
2464	struct cpuset_getaffinity_args ap;
2465
2466	ap.level = uap->level;
2467	ap.which = uap->which;
2468	ap.id = PAIR32TO64(id_t,uap->id);
2469	ap.cpusetsize = uap->cpusetsize;
2470	ap.mask = uap->mask;
2471
2472	return (sys_cpuset_getaffinity(td, &ap));
2473}
2474
2475int
2476freebsd32_cpuset_setaffinity(struct thread *td,
2477    struct freebsd32_cpuset_setaffinity_args *uap)
2478{
2479	struct cpuset_setaffinity_args ap;
2480
2481	ap.level = uap->level;
2482	ap.which = uap->which;
2483	ap.id = PAIR32TO64(id_t,uap->id);
2484	ap.cpusetsize = uap->cpusetsize;
2485	ap.mask = uap->mask;
2486
2487	return (sys_cpuset_setaffinity(td, &ap));
2488}
2489
2490int
2491freebsd32_nmount(struct thread *td,
2492    struct freebsd32_nmount_args /* {
2493    	struct iovec *iovp;
2494    	unsigned int iovcnt;
2495    	int flags;
2496    } */ *uap)
2497{
2498	struct uio *auio;
2499	uint64_t flags;
2500	int error;
2501
2502	/*
2503	 * Mount flags are now 64-bits. On 32-bit archtectures only
2504	 * 32-bits are passed in, but from here on everything handles
2505	 * 64-bit flags correctly.
2506	 */
2507	flags = uap->flags;
2508
2509	AUDIT_ARG_FFLAGS(flags);
2510
2511	/*
2512	 * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
2513	 * userspace to set this flag, but we must filter it out if we want
2514	 * MNT_UPDATE on the root file system to work.
2515	 * MNT_ROOTFS should only be set by the kernel when mounting its
2516	 * root file system.
2517	 */
2518	flags &= ~MNT_ROOTFS;
2519
2520	/*
2521	 * check that we have an even number of iovec's
2522	 * and that we have at least two options.
2523	 */
2524	if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2525		return (EINVAL);
2526
2527	error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2528	if (error)
2529		return (error);
2530	error = vfs_donmount(td, flags, auio);
2531
2532	free(auio, M_IOV);
2533	return error;
2534}
2535
2536#if 0
2537int
2538freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2539{
2540	struct yyy32 *p32, s32;
2541	struct yyy *p = NULL, s;
2542	struct xxx_arg ap;
2543	int error;
2544
2545	if (uap->zzz) {
2546		error = copyin(uap->zzz, &s32, sizeof(s32));
2547		if (error)
2548			return (error);
2549		/* translate in */
2550		p = &s;
2551	}
2552	error = kern_xxx(td, p);
2553	if (error)
2554		return (error);
2555	if (uap->zzz) {
2556		/* translate out */
2557		error = copyout(&s32, p32, sizeof(s32));
2558	}
2559	return (error);
2560}
2561#endif
2562
2563int
2564syscall32_register(int *offset, struct sysent *new_sysent,
2565    struct sysent *old_sysent)
2566{
2567	if (*offset == NO_SYSCALL) {
2568		int i;
2569
2570		for (i = 1; i < SYS_MAXSYSCALL; ++i)
2571			if (freebsd32_sysent[i].sy_call ==
2572			    (sy_call_t *)lkmnosys)
2573				break;
2574		if (i == SYS_MAXSYSCALL)
2575			return (ENFILE);
2576		*offset = i;
2577	} else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2578		return (EINVAL);
2579	else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2580	    freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2581		return (EEXIST);
2582
2583	*old_sysent = freebsd32_sysent[*offset];
2584	freebsd32_sysent[*offset] = *new_sysent;
2585	return 0;
2586}
2587
2588int
2589syscall32_deregister(int *offset, struct sysent *old_sysent)
2590{
2591
2592	if (*offset)
2593		freebsd32_sysent[*offset] = *old_sysent;
2594	return 0;
2595}
2596
2597int
2598syscall32_module_handler(struct module *mod, int what, void *arg)
2599{
2600	struct syscall_module_data *data = (struct syscall_module_data*)arg;
2601	modspecific_t ms;
2602	int error;
2603
2604	switch (what) {
2605	case MOD_LOAD:
2606		error = syscall32_register(data->offset, data->new_sysent,
2607		    &data->old_sysent);
2608		if (error) {
2609			/* Leave a mark so we know to safely unload below. */
2610			data->offset = NULL;
2611			return error;
2612		}
2613		ms.intval = *data->offset;
2614		MOD_XLOCK;
2615		module_setspecific(mod, &ms);
2616		MOD_XUNLOCK;
2617		if (data->chainevh)
2618			error = data->chainevh(mod, what, data->chainarg);
2619		return (error);
2620	case MOD_UNLOAD:
2621		/*
2622		 * MOD_LOAD failed, so just return without calling the
2623		 * chained handler since we didn't pass along the MOD_LOAD
2624		 * event.
2625		 */
2626		if (data->offset == NULL)
2627			return (0);
2628		if (data->chainevh) {
2629			error = data->chainevh(mod, what, data->chainarg);
2630			if (error)
2631				return (error);
2632		}
2633		error = syscall32_deregister(data->offset, &data->old_sysent);
2634		return (error);
2635	default:
2636		error = EOPNOTSUPP;
2637		if (data->chainevh)
2638			error = data->chainevh(mod, what, data->chainarg);
2639		return (error);
2640	}
2641}
2642
2643int
2644syscall32_helper_register(struct syscall_helper_data *sd)
2645{
2646	struct syscall_helper_data *sd1;
2647	int error;
2648
2649	for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2650		error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2651		    &sd1->old_sysent);
2652		if (error != 0) {
2653			syscall32_helper_unregister(sd);
2654			return (error);
2655		}
2656		sd1->registered = 1;
2657	}
2658	return (0);
2659}
2660
2661int
2662syscall32_helper_unregister(struct syscall_helper_data *sd)
2663{
2664	struct syscall_helper_data *sd1;
2665
2666	for (sd1 = sd; sd1->registered != 0; sd1++) {
2667		syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2668		sd1->registered = 0;
2669	}
2670	return (0);
2671}
2672
2673register_t *
2674freebsd32_copyout_strings(struct image_params *imgp)
2675{
2676	int argc, envc, i;
2677	u_int32_t *vectp;
2678	char *stringp, *destp;
2679	u_int32_t *stack_base;
2680	struct freebsd32_ps_strings *arginfo;
2681	char canary[sizeof(long) * 8];
2682	int32_t pagesizes32[MAXPAGESIZES];
2683	size_t execpath_len;
2684	int szsigcode;
2685
2686	/*
2687	 * Calculate string base and vector table pointers.
2688	 * Also deal with signal trampoline code for this exec type.
2689	 */
2690	if (imgp->execpath != NULL && imgp->auxargs != NULL)
2691		execpath_len = strlen(imgp->execpath) + 1;
2692	else
2693		execpath_len = 0;
2694	arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2695	    sv_psstrings;
2696	if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2697		szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2698	else
2699		szsigcode = 0;
2700	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2701	    roundup(execpath_len, sizeof(char *)) -
2702	    roundup(sizeof(canary), sizeof(char *)) -
2703	    roundup(sizeof(pagesizes32), sizeof(char *)) -
2704	    roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2705
2706	/*
2707	 * install sigcode
2708	 */
2709	if (szsigcode != 0)
2710		copyout(imgp->proc->p_sysent->sv_sigcode,
2711			((caddr_t)arginfo - szsigcode), szsigcode);
2712
2713	/*
2714	 * Copy the image path for the rtld.
2715	 */
2716	if (execpath_len != 0) {
2717		imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2718		copyout(imgp->execpath, (void *)imgp->execpathp,
2719		    execpath_len);
2720	}
2721
2722	/*
2723	 * Prepare the canary for SSP.
2724	 */
2725	arc4rand(canary, sizeof(canary), 0);
2726	imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2727	    sizeof(canary);
2728	copyout(canary, (void *)imgp->canary, sizeof(canary));
2729	imgp->canarylen = sizeof(canary);
2730
2731	/*
2732	 * Prepare the pagesizes array.
2733	 */
2734	for (i = 0; i < MAXPAGESIZES; i++)
2735		pagesizes32[i] = (uint32_t)pagesizes[i];
2736	imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2737	    roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2738	copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2739	imgp->pagesizeslen = sizeof(pagesizes32);
2740
2741	/*
2742	 * If we have a valid auxargs ptr, prepare some room
2743	 * on the stack.
2744	 */
2745	if (imgp->auxargs) {
2746		/*
2747		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2748		 * lower compatibility.
2749		 */
2750		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2751			: (AT_COUNT * 2);
2752		/*
2753		 * The '+ 2' is for the null pointers at the end of each of
2754		 * the arg and env vector sets,and imgp->auxarg_size is room
2755		 * for argument of Runtime loader.
2756		 */
2757		vectp = (u_int32_t *) (destp - (imgp->args->argc +
2758		    imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2759		    sizeof(u_int32_t));
2760	} else
2761		/*
2762		 * The '+ 2' is for the null pointers at the end of each of
2763		 * the arg and env vector sets
2764		 */
2765		vectp = (u_int32_t *)
2766			(destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2767
2768	/*
2769	 * vectp also becomes our initial stack base
2770	 */
2771	stack_base = vectp;
2772
2773	stringp = imgp->args->begin_argv;
2774	argc = imgp->args->argc;
2775	envc = imgp->args->envc;
2776	/*
2777	 * Copy out strings - arguments and environment.
2778	 */
2779	copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2780
2781	/*
2782	 * Fill in "ps_strings" struct for ps, w, etc.
2783	 */
2784	suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2785	suword32(&arginfo->ps_nargvstr, argc);
2786
2787	/*
2788	 * Fill in argument portion of vector table.
2789	 */
2790	for (; argc > 0; --argc) {
2791		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2792		while (*stringp++ != 0)
2793			destp++;
2794		destp++;
2795	}
2796
2797	/* a null vector table pointer separates the argp's from the envp's */
2798	suword32(vectp++, 0);
2799
2800	suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2801	suword32(&arginfo->ps_nenvstr, envc);
2802
2803	/*
2804	 * Fill in environment portion of vector table.
2805	 */
2806	for (; envc > 0; --envc) {
2807		suword32(vectp++, (u_int32_t)(intptr_t)destp);
2808		while (*stringp++ != 0)
2809			destp++;
2810		destp++;
2811	}
2812
2813	/* end of vector table is a null pointer */
2814	suword32(vectp, 0);
2815
2816	return ((register_t *)stack_base);
2817}
2818
2819int
2820freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2821{
2822	struct kld_file_stat stat;
2823	struct kld32_file_stat stat32;
2824	int error, version;
2825
2826	if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2827	    != 0)
2828		return (error);
2829	if (version != sizeof(struct kld32_file_stat_1) &&
2830	    version != sizeof(struct kld32_file_stat))
2831		return (EINVAL);
2832
2833	error = kern_kldstat(td, uap->fileid, &stat);
2834	if (error != 0)
2835		return (error);
2836
2837	bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2838	CP(stat, stat32, refs);
2839	CP(stat, stat32, id);
2840	PTROUT_CP(stat, stat32, address);
2841	CP(stat, stat32, size);
2842	bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2843	return (copyout(&stat32, uap->stat, version));
2844}
2845
2846int
2847freebsd32_posix_fallocate(struct thread *td,
2848    struct freebsd32_posix_fallocate_args *uap)
2849{
2850
2851	return (kern_posix_fallocate(td, uap->fd,
2852	    PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2853}
2854
2855int
2856freebsd32_posix_fadvise(struct thread *td,
2857    struct freebsd32_posix_fadvise_args *uap)
2858{
2859
2860	return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2861	    PAIR32TO64(off_t, uap->len), uap->advice));
2862}
2863