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