freebsd32_misc.c revision 142934
1/*-
2 * Copyright (c) 2002 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/compat/freebsd32/freebsd32_misc.c 142934 2005-03-01 17:45:55Z ps $");
29
30#include "opt_compat.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/bus.h>
35#include <sys/exec.h>
36#include <sys/fcntl.h>
37#include <sys/filedesc.h>
38#include <sys/namei.h>
39#include <sys/imgact.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/malloc.h>
43#include <sys/file.h>		/* Must come after sys/malloc.h */
44#include <sys/mman.h>
45#include <sys/module.h>
46#include <sys/mount.h>
47#include <sys/mutex.h>
48#include <sys/namei.h>
49#include <sys/param.h>
50#include <sys/proc.h>
51#include <sys/reboot.h>
52#include <sys/resource.h>
53#include <sys/resourcevar.h>
54#include <sys/selinfo.h>
55#include <sys/pipe.h>		/* Must come after sys/selinfo.h */
56#include <sys/signal.h>
57#include <sys/signalvar.h>
58#include <sys/socket.h>
59#include <sys/socketvar.h>
60#include <sys/stat.h>
61#include <sys/syscallsubr.h>
62#include <sys/sysctl.h>
63#include <sys/sysent.h>
64#include <sys/sysproto.h>
65#include <sys/systm.h>
66#include <sys/unistd.h>
67#include <sys/vnode.h>
68#include <sys/wait.h>
69
70#include <vm/vm.h>
71#include <vm/vm_kern.h>
72#include <vm/vm_param.h>
73#include <vm/pmap.h>
74#include <vm/vm_map.h>
75#include <vm/vm_object.h>
76#include <vm/vm_extern.h>
77
78#include <compat/freebsd32/freebsd32_util.h>
79#include <compat/freebsd32/freebsd32.h>
80#include <compat/freebsd32/freebsd32_proto.h>
81
82CTASSERT(sizeof(struct timeval32) == 8);
83CTASSERT(sizeof(struct timespec32) == 8);
84CTASSERT(sizeof(struct statfs32) == 256);
85CTASSERT(sizeof(struct rusage32) == 72);
86
87int
88freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
89{
90	int error, status;
91	struct rusage32 ru32;
92	struct rusage ru, *rup;
93
94	if (uap->rusage != NULL)
95		rup = &ru;
96	else
97		rup = NULL;
98	error = kern_wait(td, uap->pid, &status, uap->options, rup);
99	if (error)
100		return (error);
101	if (uap->status != NULL)
102		error = copyout(&status, uap->status, sizeof(status));
103	if (uap->rusage != NULL && error == 0) {
104		TV_CP(ru, ru32, ru_utime);
105		TV_CP(ru, ru32, ru_stime);
106		CP(ru, ru32, ru_maxrss);
107		CP(ru, ru32, ru_ixrss);
108		CP(ru, ru32, ru_idrss);
109		CP(ru, ru32, ru_isrss);
110		CP(ru, ru32, ru_minflt);
111		CP(ru, ru32, ru_majflt);
112		CP(ru, ru32, ru_nswap);
113		CP(ru, ru32, ru_inblock);
114		CP(ru, ru32, ru_oublock);
115		CP(ru, ru32, ru_msgsnd);
116		CP(ru, ru32, ru_msgrcv);
117		CP(ru, ru32, ru_nsignals);
118		CP(ru, ru32, ru_nvcsw);
119		CP(ru, ru32, ru_nivcsw);
120		error = copyout(&ru32, uap->rusage, sizeof(ru32));
121	}
122	return (error);
123}
124
125#ifdef COMPAT_FREEBSD4
126static void
127copy_statfs(struct statfs *in, struct statfs32 *out)
128{
129	CP(*in, *out, f_bsize);
130	CP(*in, *out, f_iosize);
131	CP(*in, *out, f_blocks);
132	CP(*in, *out, f_bfree);
133	CP(*in, *out, f_bavail);
134	CP(*in, *out, f_files);
135	CP(*in, *out, f_ffree);
136	CP(*in, *out, f_fsid);
137	CP(*in, *out, f_owner);
138	CP(*in, *out, f_type);
139	CP(*in, *out, f_flags);
140	CP(*in, *out, f_flags);
141	CP(*in, *out, f_syncwrites);
142	CP(*in, *out, f_asyncwrites);
143	bcopy(in->f_fstypename,
144	      out->f_fstypename, MFSNAMELEN);
145	bcopy(in->f_mntonname,
146	      out->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
147	CP(*in, *out, f_syncreads);
148	CP(*in, *out, f_asyncreads);
149	bcopy(in->f_mntfromname,
150	      out->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
151}
152#endif
153
154#ifdef COMPAT_FREEBSD4
155int
156freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
157{
158	int error;
159	caddr_t sg;
160	struct statfs32 *sp32, stat32;
161	struct statfs *sp = NULL, stat;
162	int maxcount, count, i;
163
164	sp32 = uap->buf;
165	maxcount = uap->bufsize / sizeof(struct statfs32);
166
167	if (sp32) {
168		sg = stackgap_init();
169		sp = stackgap_alloc(&sg, sizeof(struct statfs) * maxcount);
170		uap->buf = (struct statfs32 *)sp;
171	}
172	error = getfsstat(td, (struct getfsstat_args *) uap);
173	if (sp32 && !error) {
174		count = td->td_retval[0];
175		for (i = 0; i < count; i++) {
176			error = copyin(&sp[i], &stat, sizeof(stat));
177			if (error)
178				return (error);
179			copy_statfs(&stat, &stat32);
180			error = copyout(&stat32, &sp32[i], sizeof(stat32));
181			if (error)
182				return (error);
183		}
184	}
185	return (error);
186}
187#endif
188
189struct sigaltstack32 {
190	u_int32_t	ss_sp;
191	u_int32_t	ss_size;
192	int		ss_flags;
193};
194
195CTASSERT(sizeof(struct sigaltstack32) == 12);
196
197int
198freebsd32_sigaltstack(struct thread *td,
199		      struct freebsd32_sigaltstack_args *uap)
200{
201	struct sigaltstack32 s32;
202	struct sigaltstack ss, oss, *ssp;
203	int error;
204
205	if (uap->ss != NULL) {
206		error = copyin(uap->ss, &s32, sizeof(s32));
207		if (error)
208			return (error);
209		PTRIN_CP(s32, ss, ss_sp);
210		CP(s32, ss, ss_size);
211		CP(s32, ss, ss_flags);
212		ssp = &ss;
213	} else
214		ssp = NULL;
215	error = kern_sigaltstack(td, ssp, &oss);
216	if (error == 0 && uap->oss != NULL) {
217		PTROUT_CP(oss, s32, ss_sp);
218		CP(oss, s32, ss_size);
219		CP(oss, s32, ss_flags);
220		error = copyout(&s32, uap->oss, sizeof(s32));
221	}
222	return (error);
223}
224
225/*
226 * Custom version of exec_copyin_args() so that we can translate
227 * the pointers.
228 */
229static int
230freebsd32_exec_copyin_args(struct image_args *args, char *fname,
231    enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
232{
233	char *argp, *envp;
234	u_int32_t *p32, arg;
235	size_t length;
236	int error;
237
238	bzero(args, sizeof(*args));
239	if (argv == NULL)
240		return (EFAULT);
241
242	/*
243	 * Allocate temporary demand zeroed space for argument and
244	 *	environment strings
245	 */
246	args->buf = (char *) kmem_alloc_wait(exec_map, PATH_MAX + ARG_MAX);
247	if (args->buf == NULL)
248		return (ENOMEM);
249	args->begin_argv = args->buf;
250	args->endp = args->begin_argv;
251	args->stringspace = ARG_MAX;
252
253	args->fname = args->buf + ARG_MAX;
254
255	/*
256	 * Copy the file name.
257	 */
258	error = (segflg == UIO_SYSSPACE) ?
259	    copystr(fname, args->fname, PATH_MAX, &length) :
260	    copyinstr(fname, args->fname, PATH_MAX, &length);
261	if (error != 0)
262		return (error);
263
264	/*
265	 * extract arguments first
266	 */
267	p32 = argv;
268	for (;;) {
269		error = copyin(p32++, &arg, sizeof(arg));
270		if (error)
271			return (error);
272		if (arg == 0)
273			break;
274		argp = PTRIN(arg);
275		error = copyinstr(argp, args->endp, args->stringspace, &length);
276		if (error) {
277			if (error == ENAMETOOLONG)
278				return (E2BIG);
279			else
280				return (error);
281		}
282		args->stringspace -= length;
283		args->endp += length;
284		args->argc++;
285	}
286
287	args->begin_envv = args->endp;
288
289	/*
290	 * extract environment strings
291	 */
292	if (envv) {
293		p32 = envv;
294		for (;;) {
295			error = copyin(p32++, &arg, sizeof(arg));
296			if (error)
297				return (error);
298			if (arg == 0)
299				break;
300			envp = PTRIN(arg);
301			error = copyinstr(envp, args->endp, args->stringspace,
302			    &length);
303			if (error) {
304				if (error == ENAMETOOLONG)
305					return (E2BIG);
306				else
307					return (error);
308			}
309			args->stringspace -= length;
310			args->endp += length;
311			args->envc++;
312		}
313	}
314
315	return (0);
316}
317
318int
319freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
320{
321	struct image_args eargs;
322	int error;
323
324	error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
325	    uap->argv, uap->envv);
326	if (error == 0)
327		error = kern_execve(td, &eargs, NULL);
328	exec_free_args(&eargs);
329	return (error);
330}
331
332#ifdef __ia64__
333static int
334freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
335		       int prot, int fd, off_t pos)
336{
337	vm_map_t map;
338	vm_map_entry_t entry;
339	int rv;
340
341	map = &td->td_proc->p_vmspace->vm_map;
342	if (fd != -1)
343		prot |= VM_PROT_WRITE;
344
345	if (vm_map_lookup_entry(map, start, &entry)) {
346		if ((entry->protection & prot) != prot) {
347			rv = vm_map_protect(map,
348					    trunc_page(start),
349					    round_page(end),
350					    entry->protection | prot,
351					    FALSE);
352			if (rv != KERN_SUCCESS)
353				return (EINVAL);
354		}
355	} else {
356		vm_offset_t addr = trunc_page(start);
357		rv = vm_map_find(map, 0, 0,
358				 &addr, PAGE_SIZE, FALSE, prot,
359				 VM_PROT_ALL, 0);
360		if (rv != KERN_SUCCESS)
361			return (EINVAL);
362	}
363
364	if (fd != -1) {
365		struct pread_args r;
366		r.fd = fd;
367		r.buf = (void *) start;
368		r.nbyte = end - start;
369		r.offset = pos;
370		return (pread(td, &r));
371	} else {
372		while (start < end) {
373			subyte((void *) start, 0);
374			start++;
375		}
376		return (0);
377	}
378}
379#endif
380
381int
382freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
383{
384	struct mmap_args ap;
385	vm_offset_t addr = (vm_offset_t) uap->addr;
386	vm_size_t len	 = uap->len;
387	int prot	 = uap->prot;
388	int flags	 = uap->flags;
389	int fd		 = uap->fd;
390	off_t pos	 = (uap->poslo
391			    | ((off_t)uap->poshi << 32));
392#ifdef __ia64__
393	vm_size_t pageoff;
394	int error;
395
396	/*
397	 * Attempt to handle page size hassles.
398	 */
399	pageoff = (pos & PAGE_MASK);
400	if (flags & MAP_FIXED) {
401		vm_offset_t start, end;
402		start = addr;
403		end = addr + len;
404
405		if (start != trunc_page(start)) {
406			error = freebsd32_mmap_partial(td, start,
407						       round_page(start), prot,
408						       fd, pos);
409			if (fd != -1)
410				pos += round_page(start) - start;
411			start = round_page(start);
412		}
413		if (end != round_page(end)) {
414			vm_offset_t t = trunc_page(end);
415			error = freebsd32_mmap_partial(td, t, end,
416						  prot, fd,
417						  pos + t - start);
418			end = trunc_page(end);
419		}
420		if (end > start && fd != -1 && (pos & PAGE_MASK)) {
421			/*
422			 * We can't map this region at all. The specified
423			 * address doesn't have the same alignment as the file
424			 * position. Fake the mapping by simply reading the
425			 * entire region into memory. First we need to make
426			 * sure the region exists.
427			 */
428			vm_map_t map;
429			struct pread_args r;
430			int rv;
431
432			prot |= VM_PROT_WRITE;
433			map = &td->td_proc->p_vmspace->vm_map;
434			rv = vm_map_remove(map, start, end);
435			if (rv != KERN_SUCCESS)
436				return (EINVAL);
437			rv = vm_map_find(map, 0, 0,
438					 &start, end - start, FALSE,
439					 prot, VM_PROT_ALL, 0);
440			if (rv != KERN_SUCCESS)
441				return (EINVAL);
442			r.fd = fd;
443			r.buf = (void *) start;
444			r.nbyte = end - start;
445			r.offset = pos;
446			error = pread(td, &r);
447			if (error)
448				return (error);
449
450			td->td_retval[0] = addr;
451			return (0);
452		}
453		if (end == start) {
454			/*
455			 * After dealing with the ragged ends, there
456			 * might be none left.
457			 */
458			td->td_retval[0] = addr;
459			return (0);
460		}
461		addr = start;
462		len = end - start;
463	}
464#endif
465
466	ap.addr = (void *) addr;
467	ap.len = len;
468	ap.prot = prot;
469	ap.flags = flags;
470	ap.fd = fd;
471	ap.pos = pos;
472
473	return (mmap(td, &ap));
474}
475
476struct itimerval32 {
477	struct timeval32 it_interval;
478	struct timeval32 it_value;
479};
480
481CTASSERT(sizeof(struct itimerval32) == 16);
482
483int
484freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
485{
486	struct itimerval itv, oitv, *itvp;
487	struct itimerval32 i32;
488	int error;
489
490	if (uap->itv != NULL) {
491		error = copyin(uap->itv, &i32, sizeof(i32));
492		if (error)
493			return (error);
494		TV_CP(i32, itv, it_interval);
495		TV_CP(i32, itv, it_value);
496		itvp = &itv;
497	} else
498		itvp = NULL;
499	error = kern_setitimer(td, uap->which, itvp, &oitv);
500	if (error || uap->oitv == NULL)
501		return (error);
502	TV_CP(oitv, i32, it_interval);
503	TV_CP(oitv, i32, it_value);
504	return (copyout(&i32, uap->oitv, sizeof(i32)));
505}
506
507int
508freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
509{
510	struct itimerval itv;
511	struct itimerval32 i32;
512	int error;
513
514	error = kern_getitimer(td, uap->which, &itv);
515	if (error || uap->itv == NULL)
516		return (error);
517	TV_CP(itv, i32, it_interval);
518	TV_CP(itv, i32, it_value);
519	return (copyout(&i32, uap->itv, sizeof(i32)));
520}
521
522int
523freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
524{
525	struct timeval32 tv32;
526	struct timeval tv, *tvp;
527	int error;
528
529	if (uap->tv != NULL) {
530		error = copyin(uap->tv, &tv32, sizeof(tv32));
531		if (error)
532			return (error);
533		CP(tv32, tv, tv_sec);
534		CP(tv32, tv, tv_usec);
535		tvp = &tv;
536	} else
537		tvp = NULL;
538	/*
539	 * XXX big-endian needs to convert the fd_sets too.
540	 * XXX Do pointers need PTRIN()?
541	 */
542	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
543}
544
545struct kevent32 {
546	u_int32_t	ident;		/* identifier for this event */
547	short		filter;		/* filter for event */
548	u_short		flags;
549	u_int		fflags;
550	int32_t		data;
551	u_int32_t	udata;		/* opaque user data identifier */
552};
553
554CTASSERT(sizeof(struct kevent32) == 20);
555
556int
557freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
558{
559	struct timespec32 ts32;
560	struct timespec ts, *tsp;
561	struct kevent *ks;
562	struct kevent32 ks32;
563	struct kevent *changes, *events;
564	int error, i;
565
566
567	if (uap->timeout) {
568		error = copyin(uap->timeout, &ts32, sizeof(ts32));
569		if (error)
570			return (error);
571		CP(ts32, ts, tv_sec);
572		CP(ts32, ts, tv_nsec);
573		tsp = &ts;
574	} else
575		tsp = NULL;
576	if (uap->changelist && uap->nchanges > 0) {
577		changes = malloc(sizeof(struct kevent) * uap->nchanges, M_TEMP,
578		    M_WAITOK);
579		for (i = 0; i < uap->nchanges; i++) {
580			error = copyin(&uap->changelist[i], &ks32,
581			    sizeof(ks32));
582			if (error) {
583				free(changes, M_TEMP);
584				return (error);
585			}
586			ks = &changes[i];
587			CP(ks32, *ks, ident);
588			CP(ks32, *ks, filter);
589			CP(ks32, *ks, flags);
590			CP(ks32, *ks, fflags);
591			CP(ks32, *ks, data);
592			PTRIN_CP(ks32, *ks, udata);
593		}
594	} else
595		changes = NULL;
596	if (uap->eventlist && uap->nevents > 0)
597		events = malloc(sizeof(struct kevent) * uap->nevents, M_TEMP,
598		    M_WAITOK);
599	else
600		events = NULL;
601	error = kern_kevent(td, uap->fd, changes, uap->nchanges, UIO_SYSSPACE,
602	    events, uap->nevents, UIO_SYSSPACE, tsp);
603	free(changes, M_TEMP);
604	if (uap->eventlist && events && td->td_retval[0] > 0) {
605		for (i = 0; i < td->td_retval[0]; i++) {
606			ks = &events[i];
607			CP(*ks, ks32, ident);
608			CP(*ks, ks32, filter);
609			CP(*ks, ks32, flags);
610			CP(*ks, ks32, fflags);
611			CP(*ks, ks32, data);
612			PTROUT_CP(*ks, ks32, udata);
613			error = copyout(&ks32, &uap->eventlist[i],
614			    sizeof(ks32));
615			if (error)
616				break;
617		}
618	}
619	if (events)
620		free(events, M_TEMP);
621	return (error);
622}
623
624int
625freebsd32_gettimeofday(struct thread *td,
626		       struct freebsd32_gettimeofday_args *uap)
627{
628	struct timeval atv;
629	struct timeval32 atv32;
630	struct timezone rtz;
631	int error = 0;
632
633	if (uap->tp) {
634		microtime(&atv);
635		CP(atv, atv32, tv_sec);
636		CP(atv, atv32, tv_usec);
637		error = copyout(&atv32, uap->tp, sizeof (atv32));
638	}
639	if (error == 0 && uap->tzp != NULL) {
640		rtz.tz_minuteswest = tz_minuteswest;
641		rtz.tz_dsttime = tz_dsttime;
642		error = copyout(&rtz, uap->tzp, sizeof (rtz));
643	}
644	return (error);
645}
646
647int
648freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
649{
650	struct rusage32 s32;
651	struct rusage s;
652	int error;
653
654	error = kern_getrusage(td, uap->who, &s);
655	if (error)
656		return (error);
657	if (uap->rusage != NULL) {
658		TV_CP(s, s32, ru_utime);
659		TV_CP(s, s32, ru_stime);
660		CP(s, s32, ru_maxrss);
661		CP(s, s32, ru_ixrss);
662		CP(s, s32, ru_idrss);
663		CP(s, s32, ru_isrss);
664		CP(s, s32, ru_minflt);
665		CP(s, s32, ru_majflt);
666		CP(s, s32, ru_nswap);
667		CP(s, s32, ru_inblock);
668		CP(s, s32, ru_oublock);
669		CP(s, s32, ru_msgsnd);
670		CP(s, s32, ru_msgrcv);
671		CP(s, s32, ru_nsignals);
672		CP(s, s32, ru_nvcsw);
673		CP(s, s32, ru_nivcsw);
674		error = copyout(&s32, uap->rusage, sizeof(s32));
675	}
676	return (error);
677}
678
679struct iovec32 {
680	u_int32_t iov_base;
681	int	iov_len;
682};
683#define	STACKGAPLEN	400
684
685CTASSERT(sizeof(struct iovec32) == 8);
686
687int
688freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
689{
690	int error, osize, nsize, i;
691	caddr_t sg;
692	struct readv_args /* {
693		syscallarg(int) fd;
694		syscallarg(struct iovec *) iovp;
695		syscallarg(u_int) iovcnt;
696	} */ a;
697	struct iovec32 *oio;
698	struct iovec *nio;
699
700	sg = stackgap_init();
701
702	if (uap->iovcnt > (STACKGAPLEN / sizeof (struct iovec)))
703		return (EINVAL);
704
705	osize = uap->iovcnt * sizeof (struct iovec32);
706	nsize = uap->iovcnt * sizeof (struct iovec);
707
708	oio = malloc(osize, M_TEMP, M_WAITOK);
709	nio = malloc(nsize, M_TEMP, M_WAITOK);
710
711	error = 0;
712	if ((error = copyin(uap->iovp, oio, osize)))
713		goto punt;
714	for (i = 0; i < uap->iovcnt; i++) {
715		nio[i].iov_base = PTRIN(oio[i].iov_base);
716		nio[i].iov_len = oio[i].iov_len;
717	}
718
719	a.fd = uap->fd;
720	a.iovp = stackgap_alloc(&sg, nsize);
721	a.iovcnt = uap->iovcnt;
722
723	if ((error = copyout(nio, (caddr_t)a.iovp, nsize)))
724		goto punt;
725	error = readv(td, &a);
726
727punt:
728	free(oio, M_TEMP);
729	free(nio, M_TEMP);
730	return (error);
731}
732
733int
734freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
735{
736	int error, i, nsize, osize;
737	caddr_t sg;
738	struct writev_args /* {
739		syscallarg(int) fd;
740		syscallarg(struct iovec *) iovp;
741		syscallarg(u_int) iovcnt;
742	} */ a;
743	struct iovec32 *oio;
744	struct iovec *nio;
745
746	sg = stackgap_init();
747
748	if (uap->iovcnt > (STACKGAPLEN / sizeof (struct iovec)))
749		return (EINVAL);
750
751	osize = uap->iovcnt * sizeof (struct iovec32);
752	nsize = uap->iovcnt * sizeof (struct iovec);
753
754	oio = malloc(osize, M_TEMP, M_WAITOK);
755	nio = malloc(nsize, M_TEMP, M_WAITOK);
756
757	error = 0;
758	if ((error = copyin(uap->iovp, oio, osize)))
759		goto punt;
760	for (i = 0; i < uap->iovcnt; i++) {
761		nio[i].iov_base = PTRIN(oio[i].iov_base);
762		nio[i].iov_len = oio[i].iov_len;
763	}
764
765	a.fd = uap->fd;
766	a.iovp = stackgap_alloc(&sg, nsize);
767	a.iovcnt = uap->iovcnt;
768
769	if ((error = copyout(nio, (caddr_t)a.iovp, nsize)))
770		goto punt;
771	error = writev(td, &a);
772
773punt:
774	free(oio, M_TEMP);
775	free(nio, M_TEMP);
776	return (error);
777}
778
779int
780freebsd32_settimeofday(struct thread *td,
781		       struct freebsd32_settimeofday_args *uap)
782{
783	int error;
784	caddr_t sg;
785	struct timeval32 *p32, s32;
786	struct timeval *p = NULL, s;
787
788	p32 = uap->tv;
789	if (p32) {
790		sg = stackgap_init();
791		p = stackgap_alloc(&sg, sizeof(struct timeval));
792		uap->tv = (struct timeval32 *)p;
793		error = copyin(p32, &s32, sizeof(s32));
794		if (error)
795			return (error);
796		CP(s32, s, tv_sec);
797		CP(s32, s, tv_usec);
798		error = copyout(&s, p, sizeof(s));
799		if (error)
800			return (error);
801	}
802	return (settimeofday(td, (struct settimeofday_args *) uap));
803}
804
805int
806freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
807{
808	struct timeval32 s32[2];
809	struct timeval s[2], *sp;
810	int error;
811
812	if (uap->tptr != NULL) {
813		error = copyin(uap->tptr, s32, sizeof(s32));
814		if (error)
815			return (error);
816		CP(s32[0], s[0], tv_sec);
817		CP(s32[0], s[0], tv_usec);
818		CP(s32[1], s[1], tv_sec);
819		CP(s32[1], s[1], tv_usec);
820		sp = s;
821	} else
822		sp = NULL;
823	return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
824}
825
826int
827freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
828{
829	int error;
830	caddr_t sg;
831	struct timeval32 *p32, *op32, s32;
832	struct timeval *p = NULL, *op = NULL, s;
833
834	p32 = uap->delta;
835	if (p32) {
836		sg = stackgap_init();
837		p = stackgap_alloc(&sg, sizeof(struct timeval));
838		uap->delta = (struct timeval32 *)p;
839		error = copyin(p32, &s32, sizeof(s32));
840		if (error)
841			return (error);
842		CP(s32, s, tv_sec);
843		CP(s32, s, tv_usec);
844		error = copyout(&s, p, sizeof(s));
845		if (error)
846			return (error);
847	}
848	op32 = uap->olddelta;
849	if (op32) {
850		sg = stackgap_init();
851		op = stackgap_alloc(&sg, sizeof(struct timeval));
852		uap->olddelta = (struct timeval32 *)op;
853	}
854	error = adjtime(td, (struct adjtime_args *) uap);
855	if (error)
856		return error;
857	if (op32) {
858		error = copyin(op, &s, sizeof(s));
859		if (error)
860			return (error);
861		CP(s, s32, tv_sec);
862		CP(s, s32, tv_usec);
863		error = copyout(&s32, op32, sizeof(s32));
864	}
865	return (error);
866}
867
868#ifdef COMPAT_FREEBSD4
869int
870freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
871{
872	struct statfs32 s32;
873	struct statfs s;
874	int error;
875
876	error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
877	if (error)
878		return (error);
879	copy_statfs(&s, &s32);
880	return (copyout(&s32, uap->buf, sizeof(s32)));
881}
882#endif
883
884#ifdef COMPAT_FREEBSD4
885int
886freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
887{
888	struct statfs32 s32;
889	struct statfs s;
890	int error;
891
892	error = kern_fstatfs(td, uap->fd, &s);
893	if (error)
894		return (error);
895	copy_statfs(&s, &s32);
896	return (copyout(&s32, uap->buf, sizeof(s32)));
897}
898#endif
899
900#ifdef COMPAT_FREEBSD4
901int
902freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
903{
904	struct statfs32 s32;
905	struct statfs s;
906	fhandle_t fh;
907	int error;
908
909	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
910		return (error);
911	error = kern_fhstatfs(td, fh, &s);
912	if (error)
913		return (error);
914	copy_statfs(&s, &s32);
915	return (copyout(&s32, uap->buf, sizeof(s32)));
916}
917#endif
918
919int
920freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
921{
922	/*
923	 * Vector through to semsys if it is loaded.
924	 */
925	return sysent[169].sy_call(td, uap);
926}
927
928int
929freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
930{
931	/*
932	 * Vector through to msgsys if it is loaded.
933	 */
934	return sysent[170].sy_call(td, uap);
935}
936
937int
938freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
939{
940	/*
941	 * Vector through to shmsys if it is loaded.
942	 */
943	return sysent[171].sy_call(td, uap);
944}
945
946int
947freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
948{
949	struct pread_args ap;
950
951	ap.fd = uap->fd;
952	ap.buf = uap->buf;
953	ap.nbyte = uap->nbyte;
954	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
955	return (pread(td, &ap));
956}
957
958int
959freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
960{
961	struct pwrite_args ap;
962
963	ap.fd = uap->fd;
964	ap.buf = uap->buf;
965	ap.nbyte = uap->nbyte;
966	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
967	return (pwrite(td, &ap));
968}
969
970int
971freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
972{
973	int error;
974	struct lseek_args ap;
975	off_t pos;
976
977	ap.fd = uap->fd;
978	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
979	ap.whence = uap->whence;
980	error = lseek(td, &ap);
981	/* Expand the quad return into two parts for eax and edx */
982	pos = *(off_t *)(td->td_retval);
983	td->td_retval[0] = pos & 0xffffffff;	/* %eax */
984	td->td_retval[1] = pos >> 32;		/* %edx */
985	return error;
986}
987
988int
989freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
990{
991	struct truncate_args ap;
992
993	ap.path = uap->path;
994	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
995	return (truncate(td, &ap));
996}
997
998int
999freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1000{
1001	struct ftruncate_args ap;
1002
1003	ap.fd = uap->fd;
1004	ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
1005	return (ftruncate(td, &ap));
1006}
1007
1008#ifdef COMPAT_FREEBSD4
1009int
1010freebsd4_freebsd32_sendfile(struct thread *td,
1011    struct freebsd4_freebsd32_sendfile_args *uap)
1012{
1013	struct freebsd4_sendfile_args ap;
1014
1015	ap.fd = uap->fd;
1016	ap.s = uap->s;
1017	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1018	ap.nbytes = uap->nbytes;	/* XXX check */
1019	ap.hdtr = uap->hdtr;		/* XXX check */
1020	ap.sbytes = uap->sbytes;	/* XXX FIXME!! */
1021	ap.flags = uap->flags;
1022	return (freebsd4_sendfile(td, &ap));
1023}
1024#endif
1025
1026int
1027freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1028{
1029	struct sendfile_args ap;
1030
1031	ap.fd = uap->fd;
1032	ap.s = uap->s;
1033	ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
1034	ap.nbytes = uap->nbytes;	/* XXX check */
1035	ap.hdtr = uap->hdtr;		/* XXX check */
1036	ap.sbytes = uap->sbytes;	/* XXX FIXME!! */
1037	ap.flags = uap->flags;
1038	return (sendfile(td, &ap));
1039}
1040
1041struct stat32 {
1042	dev_t	st_dev;
1043	ino_t	st_ino;
1044	mode_t	st_mode;
1045	nlink_t	st_nlink;
1046	uid_t	st_uid;
1047	gid_t	st_gid;
1048	dev_t	st_rdev;
1049	struct timespec32 st_atimespec;
1050	struct timespec32 st_mtimespec;
1051	struct timespec32 st_ctimespec;
1052	off_t	st_size;
1053	int64_t	st_blocks;
1054	u_int32_t st_blksize;
1055	u_int32_t st_flags;
1056	u_int32_t st_gen;
1057	struct timespec32 st_birthtimespec;
1058	unsigned int :(8 / 2) * (16 - (int)sizeof(struct timespec32));
1059	unsigned int :(8 / 2) * (16 - (int)sizeof(struct timespec32));
1060};
1061
1062
1063CTASSERT(sizeof(struct stat32) == 96);
1064
1065static void
1066copy_stat( struct stat *in, struct stat32 *out)
1067{
1068	CP(*in, *out, st_dev);
1069	CP(*in, *out, st_ino);
1070	CP(*in, *out, st_mode);
1071	CP(*in, *out, st_nlink);
1072	CP(*in, *out, st_uid);
1073	CP(*in, *out, st_gid);
1074	CP(*in, *out, st_rdev);
1075	TS_CP(*in, *out, st_atimespec);
1076	TS_CP(*in, *out, st_mtimespec);
1077	TS_CP(*in, *out, st_ctimespec);
1078	CP(*in, *out, st_size);
1079	CP(*in, *out, st_blocks);
1080	CP(*in, *out, st_blksize);
1081	CP(*in, *out, st_flags);
1082	CP(*in, *out, st_gen);
1083}
1084
1085int
1086freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1087{
1088	struct stat sb;
1089	struct stat32 sb32;
1090	int error;
1091
1092	error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1093	if (error)
1094		return (error);
1095	copy_stat(&sb, &sb32);
1096	error = copyout(&sb32, uap->ub, sizeof (sb32));
1097	return (error);
1098}
1099
1100int
1101freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1102{
1103	struct stat ub;
1104	struct stat32 ub32;
1105	int error;
1106
1107	error = kern_fstat(td, uap->fd, &ub);
1108	if (error)
1109		return (error);
1110	copy_stat(&ub, &ub32);
1111	error = copyout(&ub32, uap->ub, sizeof(ub32));
1112	return (error);
1113}
1114
1115int
1116freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1117{
1118	struct stat sb;
1119	struct stat32 sb32;
1120	int error;
1121
1122	error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1123	if (error)
1124		return (error);
1125	copy_stat(&sb, &sb32);
1126	error = copyout(&sb32, uap->ub, sizeof (sb32));
1127	return (error);
1128}
1129
1130/*
1131 * MPSAFE
1132 */
1133int
1134freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1135{
1136	int error, name[CTL_MAXNAME];
1137	size_t j, oldlen;
1138
1139	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1140		return (EINVAL);
1141 	error = copyin(uap->name, name, uap->namelen * sizeof(int));
1142 	if (error)
1143		return (error);
1144	mtx_lock(&Giant);
1145	if (uap->oldlenp)
1146		oldlen = fuword32(uap->oldlenp);
1147	else
1148		oldlen = 0;
1149	error = userland_sysctl(td, name, uap->namelen,
1150		uap->old, &oldlen, 1,
1151		uap->new, uap->newlen, &j, SCTL_MASK32);
1152	if (error && error != ENOMEM)
1153		goto done2;
1154	if (uap->oldlenp)
1155		suword32(uap->oldlenp, j);
1156done2:
1157	mtx_unlock(&Giant);
1158	return (error);
1159}
1160
1161struct sigaction32 {
1162	u_int32_t	sa_u;
1163	int		sa_flags;
1164	sigset_t	sa_mask;
1165};
1166
1167CTASSERT(sizeof(struct sigaction32) == 24);
1168
1169int
1170freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1171{
1172	struct sigaction32 s32;
1173	struct sigaction sa, osa, *sap;
1174	int error;
1175
1176	if (uap->act) {
1177		error = copyin(uap->act, &s32, sizeof(s32));
1178		if (error)
1179			return (error);
1180		sa.sa_handler = PTRIN(s32.sa_u);
1181		CP(s32, sa, sa_flags);
1182		CP(s32, sa, sa_mask);
1183		sap = &sa;
1184	} else
1185		sap = NULL;
1186	error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1187	if (error != 0 && uap->oact != NULL) {
1188		s32.sa_u = PTROUT(osa.sa_handler);
1189		CP(osa, s32, sa_flags);
1190		CP(osa, s32, sa_mask);
1191		error = copyout(&s32, uap->oact, sizeof(s32));
1192	}
1193	return (error);
1194}
1195
1196#ifdef COMPAT_FREEBSD4
1197int
1198freebsd4_freebsd32_sigaction(struct thread *td,
1199			     struct freebsd4_freebsd32_sigaction_args *uap)
1200{
1201	struct sigaction32 s32;
1202	struct sigaction sa, osa, *sap;
1203	int error;
1204
1205	if (uap->act) {
1206		error = copyin(uap->act, &s32, sizeof(s32));
1207		if (error)
1208			return (error);
1209		sa.sa_handler = PTRIN(s32.sa_u);
1210		CP(s32, sa, sa_flags);
1211		CP(s32, sa, sa_mask);
1212		sap = &sa;
1213	} else
1214		sap = NULL;
1215	error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
1216	if (error != 0 && uap->oact != NULL) {
1217		s32.sa_u = PTROUT(osa.sa_handler);
1218		CP(osa, s32, sa_flags);
1219		CP(osa, s32, sa_mask);
1220		error = copyout(&s32, uap->oact, sizeof(s32));
1221	}
1222	return (error);
1223}
1224#endif
1225
1226int
1227freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
1228{
1229	struct timespec32 rmt32, rqt32;
1230	struct timespec rmt, rqt;
1231	int error;
1232
1233	error = copyin(uap->rqtp, &rqt32, sizeof(rqt));
1234	if (error)
1235		return (error);
1236
1237	CP(rqt32, rqt, tv_sec);
1238	CP(rqt32, rqt, tv_nsec);
1239
1240	if (uap->rmtp &&
1241	    !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
1242		return (EFAULT);
1243	error = kern_nanosleep(td, &rqt, &rmt);
1244	if (error && uap->rmtp) {
1245		int error2;
1246
1247		CP(rmt, rmt32, tv_sec);
1248		CP(rmt, rmt32, tv_nsec);
1249
1250		error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt));
1251		if (error2)
1252			error = error2;
1253	}
1254	return (error);
1255}
1256
1257#if 0
1258
1259int
1260freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
1261{
1262	int error;
1263	caddr_t sg;
1264	struct yyy32 *p32, s32;
1265	struct yyy *p = NULL, s;
1266
1267	p32 = uap->zzz;
1268	if (p32) {
1269		sg = stackgap_init();
1270		p = stackgap_alloc(&sg, sizeof(struct yyy));
1271		uap->zzz = (struct yyy32 *)p;
1272		error = copyin(p32, &s32, sizeof(s32));
1273		if (error)
1274			return (error);
1275		/* translate in */
1276		error = copyout(&s, p, sizeof(s));
1277		if (error)
1278			return (error);
1279	}
1280	error = xxx(td, (struct xxx_args *) uap);
1281	if (error)
1282		return (error);
1283	if (p32) {
1284		error = copyin(p, &s, sizeof(s));
1285		if (error)
1286			return (error);
1287		/* translate out */
1288		error = copyout(&s32, p32, sizeof(s32));
1289	}
1290	return (error);
1291}
1292
1293#endif
1294