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