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