2 * Copyright (c) 2002 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_compat.h"
32 #include "opt_inet6.h"
34 #define __ELF_WORD_SIZE 32
36 #include <sys/param.h>
38 #include <sys/clock.h>
40 #include <sys/fcntl.h>
41 #include <sys/filedesc.h>
42 #include <sys/imgact.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/linker.h>
48 #include <sys/malloc.h>
49 #include <sys/file.h> /* Must come after sys/malloc.h */
50 #include <sys/imgact.h>
53 #include <sys/module.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
56 #include <sys/namei.h>
58 #include <sys/reboot.h>
59 #include <sys/resource.h>
60 #include <sys/resourcevar.h>
61 #include <sys/selinfo.h>
62 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
63 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
64 #include <sys/signal.h>
65 #include <sys/signalvar.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
69 #include <sys/syscall.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
73 #include <sys/sysproto.h>
74 #include <sys/systm.h>
76 #include <sys/unistd.h>
77 #include <sys/ucontext.h>
78 #include <sys/vnode.h>
86 #include <netinet/in.h>
90 #include <vm/vm_param.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_extern.h>
96 #include <machine/cpu.h>
97 #include <machine/elf.h>
99 #include <security/audit/audit.h>
101 #include <compat/freebsd32/freebsd32_util.h>
102 #include <compat/freebsd32/freebsd32.h>
103 #include <compat/freebsd32/freebsd32_ipc.h>
104 #include <compat/freebsd32/freebsd32_signal.h>
105 #include <compat/freebsd32/freebsd32_proto.h>
107 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
110 CTASSERT(sizeof(struct timeval32) == 8);
111 CTASSERT(sizeof(struct timespec32) == 8);
112 CTASSERT(sizeof(struct itimerval32) == 16);
114 CTASSERT(sizeof(struct statfs32) == 256);
116 CTASSERT(sizeof(struct rusage32) == 72);
118 CTASSERT(sizeof(struct sigaltstack32) == 12);
119 CTASSERT(sizeof(struct kevent32) == 20);
120 CTASSERT(sizeof(struct iovec32) == 8);
121 CTASSERT(sizeof(struct msghdr32) == 28);
123 CTASSERT(sizeof(struct stat32) == 96);
125 CTASSERT(sizeof(struct sigaction32) == 24);
127 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
128 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
130 #if BYTE_ORDER == BIG_ENDIAN
131 #define PAIR32TO64(type, name) ((name ## 2) | ((type)(name ## 1) << 32))
135 #define PAIR32TO64(type, name) ((name ## 1) | ((type)(name ## 2) << 32))
141 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
144 TV_CP(*s, *s32, ru_utime);
145 TV_CP(*s, *s32, ru_stime);
146 CP(*s, *s32, ru_maxrss);
147 CP(*s, *s32, ru_ixrss);
148 CP(*s, *s32, ru_idrss);
149 CP(*s, *s32, ru_isrss);
150 CP(*s, *s32, ru_minflt);
151 CP(*s, *s32, ru_majflt);
152 CP(*s, *s32, ru_nswap);
153 CP(*s, *s32, ru_inblock);
154 CP(*s, *s32, ru_oublock);
155 CP(*s, *s32, ru_msgsnd);
156 CP(*s, *s32, ru_msgrcv);
157 CP(*s, *s32, ru_nsignals);
158 CP(*s, *s32, ru_nvcsw);
159 CP(*s, *s32, ru_nivcsw);
163 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
166 struct rusage32 ru32;
167 struct rusage ru, *rup;
169 if (uap->rusage != NULL)
173 error = kern_wait(td, uap->pid, &status, uap->options, rup);
176 if (uap->status != NULL)
177 error = copyout(&status, uap->status, sizeof(status));
178 if (uap->rusage != NULL && error == 0) {
179 freebsd32_rusage_out(&ru, &ru32);
180 error = copyout(&ru32, uap->rusage, sizeof(ru32));
186 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
188 struct wrusage32 wru32;
189 struct __wrusage wru, *wrup;
190 struct siginfo32 si32;
191 struct __siginfo si, *sip;
194 if (uap->wrusage != NULL)
198 if (uap->info != NULL) {
200 bzero(sip, sizeof(*sip));
203 error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
204 &status, uap->options, wrup, sip);
207 if (uap->status != NULL)
208 error = copyout(&status, uap->status, sizeof(status));
209 if (uap->wrusage != NULL && error == 0) {
210 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
211 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
212 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
214 if (uap->info != NULL && error == 0) {
215 siginfo_to_siginfo32 (&si, &si32);
216 error = copyout(&si32, uap->info, sizeof(si32));
221 #ifdef COMPAT_FREEBSD4
223 copy_statfs(struct statfs *in, struct statfs32 *out)
226 statfs_scale_blocks(in, INT32_MAX);
227 bzero(out, sizeof(*out));
228 CP(*in, *out, f_bsize);
229 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
230 CP(*in, *out, f_blocks);
231 CP(*in, *out, f_bfree);
232 CP(*in, *out, f_bavail);
233 out->f_files = MIN(in->f_files, INT32_MAX);
234 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
235 CP(*in, *out, f_fsid);
236 CP(*in, *out, f_owner);
237 CP(*in, *out, f_type);
238 CP(*in, *out, f_flags);
239 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
240 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
241 strlcpy(out->f_fstypename,
242 in->f_fstypename, MFSNAMELEN);
243 strlcpy(out->f_mntonname,
244 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
245 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
246 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
247 strlcpy(out->f_mntfromname,
248 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
252 #ifdef COMPAT_FREEBSD4
254 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
256 struct statfs *buf, *sp;
257 struct statfs32 stat32;
261 count = uap->bufsize / sizeof(struct statfs32);
262 size = count * sizeof(struct statfs);
263 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
265 count = td->td_retval[0];
267 while (count > 0 && error == 0) {
268 copy_statfs(sp, &stat32);
269 error = copyout(&stat32, uap->buf, sizeof(stat32));
281 freebsd32_sigaltstack(struct thread *td,
282 struct freebsd32_sigaltstack_args *uap)
284 struct sigaltstack32 s32;
285 struct sigaltstack ss, oss, *ssp;
288 if (uap->ss != NULL) {
289 error = copyin(uap->ss, &s32, sizeof(s32));
292 PTRIN_CP(s32, ss, ss_sp);
293 CP(s32, ss, ss_size);
294 CP(s32, ss, ss_flags);
298 error = kern_sigaltstack(td, ssp, &oss);
299 if (error == 0 && uap->oss != NULL) {
300 PTROUT_CP(oss, s32, ss_sp);
301 CP(oss, s32, ss_size);
302 CP(oss, s32, ss_flags);
303 error = copyout(&s32, uap->oss, sizeof(s32));
309 * Custom version of exec_copyin_args() so that we can translate
313 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
314 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
321 bzero(args, sizeof(*args));
326 * Allocate demand-paged memory for the file name, argument, and
327 * environment strings.
329 error = exec_alloc_args(args);
334 * Copy the file name.
337 args->fname = args->buf;
338 error = (segflg == UIO_SYSSPACE) ?
339 copystr(fname, args->fname, PATH_MAX, &length) :
340 copyinstr(fname, args->fname, PATH_MAX, &length);
346 args->begin_argv = args->buf + length;
347 args->endp = args->begin_argv;
348 args->stringspace = ARG_MAX;
351 * extract arguments first
355 error = copyin(p32++, &arg, sizeof(arg));
361 error = copyinstr(argp, args->endp, args->stringspace, &length);
363 if (error == ENAMETOOLONG)
367 args->stringspace -= length;
368 args->endp += length;
372 args->begin_envv = args->endp;
375 * extract environment strings
380 error = copyin(p32++, &arg, sizeof(arg));
386 error = copyinstr(envp, args->endp, args->stringspace,
389 if (error == ENAMETOOLONG)
393 args->stringspace -= length;
394 args->endp += length;
402 exec_free_args(args);
407 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
409 struct image_args eargs;
412 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
413 uap->argv, uap->envv);
415 error = kern_execve(td, &eargs, NULL);
420 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
422 struct image_args eargs;
425 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
426 uap->argv, uap->envv);
429 error = kern_execve(td, &eargs, NULL);
436 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
437 int prot, int fd, off_t pos)
440 vm_map_entry_t entry;
443 map = &td->td_proc->p_vmspace->vm_map;
445 prot |= VM_PROT_WRITE;
447 if (vm_map_lookup_entry(map, start, &entry)) {
448 if ((entry->protection & prot) != prot) {
449 rv = vm_map_protect(map,
452 entry->protection | prot,
454 if (rv != KERN_SUCCESS)
458 vm_offset_t addr = trunc_page(start);
459 rv = vm_map_find(map, 0, 0,
460 &addr, PAGE_SIZE, FALSE, prot,
462 if (rv != KERN_SUCCESS)
469 r.buf = (void *) start;
470 r.nbyte = end - start;
472 return (sys_pread(td, &r));
474 while (start < end) {
475 subyte((void *) start, 0);
484 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
486 struct mprotect_args ap;
488 ap.addr = PTRIN(uap->addr);
491 #if defined(__amd64__) || defined(__ia64__)
492 if (i386_read_exec && (ap.prot & PROT_READ) != 0)
493 ap.prot |= PROT_EXEC;
495 return (sys_mprotect(td, &ap));
499 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
502 vm_offset_t addr = (vm_offset_t) uap->addr;
503 vm_size_t len = uap->len;
504 int prot = uap->prot;
505 int flags = uap->flags;
507 off_t pos = PAIR32TO64(off_t,uap->pos);
513 * Attempt to handle page size hassles.
515 pageoff = (pos & PAGE_MASK);
516 if (flags & MAP_FIXED) {
517 vm_offset_t start, end;
521 if (start != trunc_page(start)) {
522 error = freebsd32_mmap_partial(td, start,
523 round_page(start), prot,
526 pos += round_page(start) - start;
527 start = round_page(start);
529 if (end != round_page(end)) {
530 vm_offset_t t = trunc_page(end);
531 error = freebsd32_mmap_partial(td, t, end,
534 end = trunc_page(end);
536 if (end > start && fd != -1 && (pos & PAGE_MASK)) {
538 * We can't map this region at all. The specified
539 * address doesn't have the same alignment as the file
540 * position. Fake the mapping by simply reading the
541 * entire region into memory. First we need to make
542 * sure the region exists.
548 prot |= VM_PROT_WRITE;
549 map = &td->td_proc->p_vmspace->vm_map;
550 rv = vm_map_remove(map, start, end);
551 if (rv != KERN_SUCCESS)
553 rv = vm_map_find(map, 0, 0,
554 &start, end - start, FALSE,
555 prot, VM_PROT_ALL, 0);
556 if (rv != KERN_SUCCESS)
559 r.buf = (void *) start;
560 r.nbyte = end - start;
562 error = sys_pread(td, &r);
566 td->td_retval[0] = addr;
571 * After dealing with the ragged ends, there
572 * might be none left.
574 td->td_retval[0] = addr;
582 #if defined(__amd64__) || defined(__ia64__)
583 if (i386_read_exec && (prot & PROT_READ))
587 ap.addr = (void *) addr;
594 return (sys_mmap(td, &ap));
597 #ifdef COMPAT_FREEBSD6
599 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
601 struct freebsd32_mmap_args ap;
606 ap.flags = uap->flags;
611 return (freebsd32_mmap(td, &ap));
616 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
618 struct itimerval itv, oitv, *itvp;
619 struct itimerval32 i32;
622 if (uap->itv != NULL) {
623 error = copyin(uap->itv, &i32, sizeof(i32));
626 TV_CP(i32, itv, it_interval);
627 TV_CP(i32, itv, it_value);
631 error = kern_setitimer(td, uap->which, itvp, &oitv);
632 if (error || uap->oitv == NULL)
634 TV_CP(oitv, i32, it_interval);
635 TV_CP(oitv, i32, it_value);
636 return (copyout(&i32, uap->oitv, sizeof(i32)));
640 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
642 struct itimerval itv;
643 struct itimerval32 i32;
646 error = kern_getitimer(td, uap->which, &itv);
647 if (error || uap->itv == NULL)
649 TV_CP(itv, i32, it_interval);
650 TV_CP(itv, i32, it_value);
651 return (copyout(&i32, uap->itv, sizeof(i32)));
655 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
657 struct timeval32 tv32;
658 struct timeval tv, *tvp;
661 if (uap->tv != NULL) {
662 error = copyin(uap->tv, &tv32, sizeof(tv32));
665 CP(tv32, tv, tv_sec);
666 CP(tv32, tv, tv_usec);
671 * XXX Do pointers need PTRIN()?
673 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
674 sizeof(int32_t) * 8));
678 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
680 struct timespec32 ts32;
682 struct timeval tv, *tvp;
686 if (uap->ts != NULL) {
687 error = copyin(uap->ts, &ts32, sizeof(ts32));
690 CP(ts32, ts, tv_sec);
691 CP(ts32, ts, tv_nsec);
692 TIMESPEC_TO_TIMEVAL(&tv, &ts);
696 if (uap->sm != NULL) {
697 error = copyin(uap->sm, &set, sizeof(set));
704 * XXX Do pointers need PTRIN()?
706 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
707 uset, sizeof(int32_t) * 8);
712 * Copy 'count' items into the destination list pointed to by uap->eventlist.
715 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
717 struct freebsd32_kevent_args *uap;
718 struct kevent32 ks32[KQ_NEVENTS];
721 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
722 uap = (struct freebsd32_kevent_args *)arg;
724 for (i = 0; i < count; i++) {
725 CP(kevp[i], ks32[i], ident);
726 CP(kevp[i], ks32[i], filter);
727 CP(kevp[i], ks32[i], flags);
728 CP(kevp[i], ks32[i], fflags);
729 CP(kevp[i], ks32[i], data);
730 PTROUT_CP(kevp[i], ks32[i], udata);
732 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
734 uap->eventlist += count;
739 * Copy 'count' items from the list pointed to by uap->changelist.
742 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
744 struct freebsd32_kevent_args *uap;
745 struct kevent32 ks32[KQ_NEVENTS];
748 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
749 uap = (struct freebsd32_kevent_args *)arg;
751 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
754 uap->changelist += count;
756 for (i = 0; i < count; i++) {
757 CP(ks32[i], kevp[i], ident);
758 CP(ks32[i], kevp[i], filter);
759 CP(ks32[i], kevp[i], flags);
760 CP(ks32[i], kevp[i], fflags);
761 CP(ks32[i], kevp[i], data);
762 PTRIN_CP(ks32[i], kevp[i], udata);
769 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
771 struct timespec32 ts32;
772 struct timespec ts, *tsp;
773 struct kevent_copyops k_ops = { uap,
774 freebsd32_kevent_copyout,
775 freebsd32_kevent_copyin};
780 error = copyin(uap->timeout, &ts32, sizeof(ts32));
783 CP(ts32, ts, tv_sec);
784 CP(ts32, ts, tv_nsec);
788 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
794 freebsd32_gettimeofday(struct thread *td,
795 struct freebsd32_gettimeofday_args *uap)
798 struct timeval32 atv32;
804 CP(atv, atv32, tv_sec);
805 CP(atv, atv32, tv_usec);
806 error = copyout(&atv32, uap->tp, sizeof (atv32));
808 if (error == 0 && uap->tzp != NULL) {
809 rtz.tz_minuteswest = tz_minuteswest;
810 rtz.tz_dsttime = tz_dsttime;
811 error = copyout(&rtz, uap->tzp, sizeof (rtz));
817 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
823 error = kern_getrusage(td, uap->who, &s);
826 if (uap->rusage != NULL) {
827 freebsd32_rusage_out(&s, &s32);
828 error = copyout(&s32, uap->rusage, sizeof(s32));
834 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
836 struct iovec32 iov32;
843 if (iovcnt > UIO_MAXIOV)
845 iovlen = iovcnt * sizeof(struct iovec);
846 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
847 iov = (struct iovec *)(uio + 1);
848 for (i = 0; i < iovcnt; i++) {
849 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
854 iov[i].iov_base = PTRIN(iov32.iov_base);
855 iov[i].iov_len = iov32.iov_len;
858 uio->uio_iovcnt = iovcnt;
859 uio->uio_segflg = UIO_USERSPACE;
860 uio->uio_offset = -1;
862 for (i = 0; i < iovcnt; i++) {
863 if (iov->iov_len > INT_MAX - uio->uio_resid) {
867 uio->uio_resid += iov->iov_len;
875 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
880 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
883 error = kern_readv(td, uap->fd, auio);
889 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
894 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
897 error = kern_writev(td, uap->fd, auio);
903 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
908 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
911 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
917 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
922 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
925 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
931 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
934 struct iovec32 iov32;
940 if (iovcnt > UIO_MAXIOV)
942 iovlen = iovcnt * sizeof(struct iovec);
943 iov = malloc(iovlen, M_IOV, M_WAITOK);
944 for (i = 0; i < iovcnt; i++) {
945 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
950 iov[i].iov_base = PTRIN(iov32.iov_base);
951 iov[i].iov_len = iov32.iov_len;
958 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
963 error = copyin(msg32, &m32, sizeof(m32));
966 msg->msg_name = PTRIN(m32.msg_name);
967 msg->msg_namelen = m32.msg_namelen;
968 msg->msg_iov = PTRIN(m32.msg_iov);
969 msg->msg_iovlen = m32.msg_iovlen;
970 msg->msg_control = PTRIN(m32.msg_control);
971 msg->msg_controllen = m32.msg_controllen;
972 msg->msg_flags = m32.msg_flags;
977 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
982 m32.msg_name = PTROUT(msg->msg_name);
983 m32.msg_namelen = msg->msg_namelen;
984 m32.msg_iov = PTROUT(msg->msg_iov);
985 m32.msg_iovlen = msg->msg_iovlen;
986 m32.msg_control = PTROUT(msg->msg_control);
987 m32.msg_controllen = msg->msg_controllen;
988 m32.msg_flags = msg->msg_flags;
989 error = copyout(&m32, msg32, sizeof(m32));
994 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
996 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
998 #define FREEBSD32_ALIGN(p) \
999 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1000 #define FREEBSD32_CMSG_SPACE(l) \
1001 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1003 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
1004 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1006 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1010 socklen_t clen, datalen;
1013 int len, maxlen, copylen;
1017 len = msg->msg_controllen;
1018 maxlen = msg->msg_controllen;
1019 msg->msg_controllen = 0;
1022 ctlbuf = msg->msg_control;
1024 while (m && len > 0) {
1025 cm = mtod(m, struct cmsghdr *);
1028 while (cm != NULL) {
1030 if (sizeof(struct cmsghdr) > clen ||
1031 cm->cmsg_len > clen) {
1036 data = CMSG_DATA(cm);
1037 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1039 /* Adjust message length */
1040 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1045 copylen = sizeof(struct cmsghdr);
1046 if (len < copylen) {
1047 msg->msg_flags |= MSG_CTRUNC;
1051 error = copyout(cm,ctlbuf,copylen);
1055 ctlbuf += FREEBSD32_ALIGN(copylen);
1056 len -= FREEBSD32_ALIGN(copylen);
1063 if (len < copylen) {
1064 msg->msg_flags |= MSG_CTRUNC;
1068 error = copyout(data,ctlbuf,copylen);
1072 ctlbuf += FREEBSD32_ALIGN(copylen);
1073 len -= FREEBSD32_ALIGN(copylen);
1075 if (CMSG_SPACE(datalen) < clen) {
1076 clen -= CMSG_SPACE(datalen);
1077 cm = (struct cmsghdr *)
1078 ((caddr_t)cm + CMSG_SPACE(datalen));
1087 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1095 freebsd32_recvmsg(td, uap)
1097 struct freebsd32_recvmsg_args /* {
1099 struct msghdr32 *msg;
1104 struct msghdr32 m32;
1105 struct iovec *uiov, *iov;
1106 struct mbuf *control = NULL;
1107 struct mbuf **controlp;
1110 error = copyin(uap->msg, &m32, sizeof(m32));
1113 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1116 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1120 msg.msg_flags = uap->flags;
1124 controlp = (msg.msg_control != NULL) ? &control : NULL;
1125 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1129 if (control != NULL)
1130 error = freebsd32_copy_msg_out(&msg, control);
1132 msg.msg_controllen = 0;
1135 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1139 if (control != NULL)
1147 freebsd32_convert_msg_in(struct mbuf **controlp)
1149 struct mbuf *control = *controlp;
1150 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1152 socklen_t clen = control->m_len, datalen;
1158 while (cm != NULL) {
1159 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1164 data = FREEBSD32_CMSG_DATA(cm);
1165 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1167 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1169 controlp = &(*controlp)->m_next;
1171 if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1172 clen -= FREEBSD32_CMSG_SPACE(datalen);
1173 cm = (struct cmsghdr *)
1174 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1187 freebsd32_sendmsg(struct thread *td,
1188 struct freebsd32_sendmsg_args *uap)
1191 struct msghdr32 m32;
1193 struct mbuf *control = NULL;
1194 struct sockaddr *to = NULL;
1197 error = copyin(uap->msg, &m32, sizeof(m32));
1200 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1203 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1208 if (msg.msg_name != NULL) {
1209 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1217 if (msg.msg_control) {
1218 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1223 error = sockargs(&control, msg.msg_control,
1224 msg.msg_controllen, MT_CONTROL);
1228 error = freebsd32_convert_msg_in(&control);
1233 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1244 freebsd32_recvfrom(struct thread *td,
1245 struct freebsd32_recvfrom_args *uap)
1251 if (uap->fromlenaddr) {
1252 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1253 sizeof(msg.msg_namelen));
1257 msg.msg_namelen = 0;
1260 msg.msg_name = PTRIN(uap->from);
1261 msg.msg_iov = &aiov;
1263 aiov.iov_base = PTRIN(uap->buf);
1264 aiov.iov_len = uap->len;
1265 msg.msg_control = NULL;
1266 msg.msg_flags = uap->flags;
1267 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1268 if (error == 0 && uap->fromlenaddr)
1269 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1270 sizeof (msg.msg_namelen));
1275 freebsd32_settimeofday(struct thread *td,
1276 struct freebsd32_settimeofday_args *uap)
1278 struct timeval32 tv32;
1279 struct timeval tv, *tvp;
1280 struct timezone tz, *tzp;
1284 error = copyin(uap->tv, &tv32, sizeof(tv32));
1287 CP(tv32, tv, tv_sec);
1288 CP(tv32, tv, tv_usec);
1293 error = copyin(uap->tzp, &tz, sizeof(tz));
1299 return (kern_settimeofday(td, tvp, tzp));
1303 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1305 struct timeval32 s32[2];
1306 struct timeval s[2], *sp;
1309 if (uap->tptr != NULL) {
1310 error = copyin(uap->tptr, s32, sizeof(s32));
1313 CP(s32[0], s[0], tv_sec);
1314 CP(s32[0], s[0], tv_usec);
1315 CP(s32[1], s[1], tv_sec);
1316 CP(s32[1], s[1], tv_usec);
1320 return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1324 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1326 struct timeval32 s32[2];
1327 struct timeval s[2], *sp;
1330 if (uap->tptr != NULL) {
1331 error = copyin(uap->tptr, s32, sizeof(s32));
1334 CP(s32[0], s[0], tv_sec);
1335 CP(s32[0], s[0], tv_usec);
1336 CP(s32[1], s[1], tv_sec);
1337 CP(s32[1], s[1], tv_usec);
1341 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1345 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1347 struct timeval32 s32[2];
1348 struct timeval s[2], *sp;
1351 if (uap->tptr != NULL) {
1352 error = copyin(uap->tptr, s32, sizeof(s32));
1355 CP(s32[0], s[0], tv_sec);
1356 CP(s32[0], s[0], tv_usec);
1357 CP(s32[1], s[1], tv_sec);
1358 CP(s32[1], s[1], tv_usec);
1362 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1366 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1368 struct timeval32 s32[2];
1369 struct timeval s[2], *sp;
1372 if (uap->times != NULL) {
1373 error = copyin(uap->times, s32, sizeof(s32));
1376 CP(s32[0], s[0], tv_sec);
1377 CP(s32[0], s[0], tv_usec);
1378 CP(s32[1], s[1], tv_sec);
1379 CP(s32[1], s[1], tv_usec);
1383 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1388 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1390 struct timeval32 tv32;
1391 struct timeval delta, olddelta, *deltap;
1395 error = copyin(uap->delta, &tv32, sizeof(tv32));
1398 CP(tv32, delta, tv_sec);
1399 CP(tv32, delta, tv_usec);
1403 error = kern_adjtime(td, deltap, &olddelta);
1404 if (uap->olddelta && error == 0) {
1405 CP(olddelta, tv32, tv_sec);
1406 CP(olddelta, tv32, tv_usec);
1407 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1412 #ifdef COMPAT_FREEBSD4
1414 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1416 struct statfs32 s32;
1420 error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1423 copy_statfs(&s, &s32);
1424 return (copyout(&s32, uap->buf, sizeof(s32)));
1428 #ifdef COMPAT_FREEBSD4
1430 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1432 struct statfs32 s32;
1436 error = kern_fstatfs(td, uap->fd, &s);
1439 copy_statfs(&s, &s32);
1440 return (copyout(&s32, uap->buf, sizeof(s32)));
1444 #ifdef COMPAT_FREEBSD4
1446 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1448 struct statfs32 s32;
1453 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1455 error = kern_fhstatfs(td, fh, &s);
1458 copy_statfs(&s, &s32);
1459 return (copyout(&s32, uap->buf, sizeof(s32)));
1464 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1466 struct pread_args ap;
1470 ap.nbyte = uap->nbyte;
1471 ap.offset = PAIR32TO64(off_t,uap->offset);
1472 return (sys_pread(td, &ap));
1476 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1478 struct pwrite_args ap;
1482 ap.nbyte = uap->nbyte;
1483 ap.offset = PAIR32TO64(off_t,uap->offset);
1484 return (sys_pwrite(td, &ap));
1489 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1491 struct lseek_args nuap;
1494 nuap.offset = uap->offset;
1495 nuap.whence = uap->whence;
1496 return (sys_lseek(td, &nuap));
1501 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1504 struct lseek_args ap;
1508 ap.offset = PAIR32TO64(off_t,uap->offset);
1509 ap.whence = uap->whence;
1510 error = sys_lseek(td, &ap);
1511 /* Expand the quad return into two parts for eax and edx */
1512 pos = *(off_t *)(td->td_retval);
1513 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1514 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1519 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1521 struct truncate_args ap;
1523 ap.path = uap->path;
1524 ap.length = PAIR32TO64(off_t,uap->length);
1525 return (sys_truncate(td, &ap));
1529 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1531 struct ftruncate_args ap;
1534 ap.length = PAIR32TO64(off_t,uap->length);
1535 return (sys_ftruncate(td, &ap));
1540 ofreebsd32_getdirentries(struct thread *td,
1541 struct ofreebsd32_getdirentries_args *uap)
1543 struct ogetdirentries_args ap;
1550 ap.count = uap->count;
1552 error = kern_ogetdirentries(td, &ap, &loff);
1555 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1562 freebsd32_getdirentries(struct thread *td,
1563 struct freebsd32_getdirentries_args *uap)
1569 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1570 NULL, UIO_USERSPACE);
1573 if (uap->basep != NULL) {
1575 error = copyout(&base32, uap->basep, sizeof(int32_t));
1580 #ifdef COMPAT_FREEBSD6
1581 /* versions with the 'int pad' argument */
1583 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1585 struct pread_args ap;
1589 ap.nbyte = uap->nbyte;
1590 ap.offset = PAIR32TO64(off_t,uap->offset);
1591 return (sys_pread(td, &ap));
1595 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1597 struct pwrite_args ap;
1601 ap.nbyte = uap->nbyte;
1602 ap.offset = PAIR32TO64(off_t,uap->offset);
1603 return (sys_pwrite(td, &ap));
1607 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1610 struct lseek_args ap;
1614 ap.offset = PAIR32TO64(off_t,uap->offset);
1615 ap.whence = uap->whence;
1616 error = sys_lseek(td, &ap);
1617 /* Expand the quad return into two parts for eax and edx */
1618 pos = *(off_t *)(td->td_retval);
1619 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1620 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1625 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1627 struct truncate_args ap;
1629 ap.path = uap->path;
1630 ap.length = PAIR32TO64(off_t,uap->length);
1631 return (sys_truncate(td, &ap));
1635 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1637 struct ftruncate_args ap;
1640 ap.length = PAIR32TO64(off_t,uap->length);
1641 return (sys_ftruncate(td, &ap));
1643 #endif /* COMPAT_FREEBSD6 */
1653 freebsd32_do_sendfile(struct thread *td,
1654 struct freebsd32_sendfile_args *uap, int compat)
1656 struct sendfile_args ap;
1657 struct sf_hdtr32 hdtr32;
1658 struct sf_hdtr hdtr;
1659 struct uio *hdr_uio, *trl_uio;
1660 struct iovec32 *iov32;
1663 hdr_uio = trl_uio = NULL;
1667 ap.offset = PAIR32TO64(off_t,uap->offset);
1668 ap.nbytes = uap->nbytes;
1669 ap.hdtr = (struct sf_hdtr *)uap->hdtr; /* XXX not used */
1670 ap.sbytes = uap->sbytes;
1671 ap.flags = uap->flags;
1673 if (uap->hdtr != NULL) {
1674 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1677 PTRIN_CP(hdtr32, hdtr, headers);
1678 CP(hdtr32, hdtr, hdr_cnt);
1679 PTRIN_CP(hdtr32, hdtr, trailers);
1680 CP(hdtr32, hdtr, trl_cnt);
1682 if (hdtr.headers != NULL) {
1683 iov32 = PTRIN(hdtr32.headers);
1684 error = freebsd32_copyinuio(iov32,
1685 hdtr32.hdr_cnt, &hdr_uio);
1689 if (hdtr.trailers != NULL) {
1690 iov32 = PTRIN(hdtr32.trailers);
1691 error = freebsd32_copyinuio(iov32,
1692 hdtr32.trl_cnt, &trl_uio);
1698 error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1701 free(hdr_uio, M_IOV);
1703 free(trl_uio, M_IOV);
1707 #ifdef COMPAT_FREEBSD4
1709 freebsd4_freebsd32_sendfile(struct thread *td,
1710 struct freebsd4_freebsd32_sendfile_args *uap)
1712 return (freebsd32_do_sendfile(td,
1713 (struct freebsd32_sendfile_args *)uap, 1));
1718 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1721 return (freebsd32_do_sendfile(td, uap, 0));
1725 copy_stat(struct stat *in, struct stat32 *out)
1728 CP(*in, *out, st_dev);
1729 CP(*in, *out, st_ino);
1730 CP(*in, *out, st_mode);
1731 CP(*in, *out, st_nlink);
1732 CP(*in, *out, st_uid);
1733 CP(*in, *out, st_gid);
1734 CP(*in, *out, st_rdev);
1735 TS_CP(*in, *out, st_atim);
1736 TS_CP(*in, *out, st_mtim);
1737 TS_CP(*in, *out, st_ctim);
1738 CP(*in, *out, st_size);
1739 CP(*in, *out, st_blocks);
1740 CP(*in, *out, st_blksize);
1741 CP(*in, *out, st_flags);
1742 CP(*in, *out, st_gen);
1743 TS_CP(*in, *out, st_birthtim);
1748 copy_ostat(struct stat *in, struct ostat32 *out)
1751 CP(*in, *out, st_dev);
1752 CP(*in, *out, st_ino);
1753 CP(*in, *out, st_mode);
1754 CP(*in, *out, st_nlink);
1755 CP(*in, *out, st_uid);
1756 CP(*in, *out, st_gid);
1757 CP(*in, *out, st_rdev);
1758 CP(*in, *out, st_size);
1759 TS_CP(*in, *out, st_atim);
1760 TS_CP(*in, *out, st_mtim);
1761 TS_CP(*in, *out, st_ctim);
1762 CP(*in, *out, st_blksize);
1763 CP(*in, *out, st_blocks);
1764 CP(*in, *out, st_flags);
1765 CP(*in, *out, st_gen);
1770 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1776 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1779 copy_stat(&sb, &sb32);
1780 error = copyout(&sb32, uap->ub, sizeof (sb32));
1786 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1789 struct ostat32 sb32;
1792 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1795 copy_ostat(&sb, &sb32);
1796 error = copyout(&sb32, uap->ub, sizeof (sb32));
1802 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1808 error = kern_fstat(td, uap->fd, &ub);
1811 copy_stat(&ub, &ub32);
1812 error = copyout(&ub32, uap->ub, sizeof(ub32));
1818 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1821 struct ostat32 ub32;
1824 error = kern_fstat(td, uap->fd, &ub);
1827 copy_ostat(&ub, &ub32);
1828 error = copyout(&ub32, uap->ub, sizeof(ub32));
1834 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1840 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1843 copy_stat(&ub, &ub32);
1844 error = copyout(&ub32, uap->buf, sizeof(ub32));
1849 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1855 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1858 copy_stat(&sb, &sb32);
1859 error = copyout(&sb32, uap->ub, sizeof (sb32));
1865 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1868 struct ostat32 sb32;
1871 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1874 copy_ostat(&sb, &sb32);
1875 error = copyout(&sb32, uap->ub, sizeof (sb32));
1881 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1883 int error, name[CTL_MAXNAME];
1886 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1888 error = copyin(uap->name, name, uap->namelen * sizeof(int));
1892 oldlen = fuword32(uap->oldlenp);
1895 error = userland_sysctl(td, name, uap->namelen,
1896 uap->old, &oldlen, 1,
1897 uap->new, uap->newlen, &j, SCTL_MASK32);
1898 if (error && error != ENOMEM)
1901 suword32(uap->oldlenp, j);
1906 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1912 error = copyin(uap->jail, &version, sizeof(uint32_t));
1919 /* FreeBSD single IPv4 jails. */
1920 struct jail32_v0 j32_v0;
1922 bzero(&j, sizeof(struct jail));
1923 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1926 CP(j32_v0, j, version);
1927 PTRIN_CP(j32_v0, j, path);
1928 PTRIN_CP(j32_v0, j, hostname);
1929 j.ip4s = j32_v0.ip_number;
1935 * Version 1 was used by multi-IPv4 jail implementations
1936 * that never made it into the official kernel.
1940 case 2: /* JAIL_API_VERSION */
1942 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1945 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1948 CP(j32, j, version);
1949 PTRIN_CP(j32, j, path);
1950 PTRIN_CP(j32, j, hostname);
1951 PTRIN_CP(j32, j, jailname);
1954 PTRIN_CP(j32, j, ip4);
1955 PTRIN_CP(j32, j, ip6);
1960 /* Sci-Fi jails are not supported, sorry. */
1963 return (kern_jail(td, &j));
1967 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1972 /* Check that we have an even number of iovecs. */
1973 if (uap->iovcnt & 1)
1976 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1979 error = kern_jail_set(td, auio, uap->flags);
1985 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1987 struct iovec32 iov32;
1991 /* Check that we have an even number of iovecs. */
1992 if (uap->iovcnt & 1)
1995 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1998 error = kern_jail_get(td, auio, uap->flags);
2000 for (i = 0; i < uap->iovcnt; i++) {
2001 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2002 CP(auio->uio_iov[i], iov32, iov_len);
2003 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2012 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2014 struct sigaction32 s32;
2015 struct sigaction sa, osa, *sap;
2019 error = copyin(uap->act, &s32, sizeof(s32));
2022 sa.sa_handler = PTRIN(s32.sa_u);
2023 CP(s32, sa, sa_flags);
2024 CP(s32, sa, sa_mask);
2028 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2029 if (error == 0 && uap->oact != NULL) {
2030 s32.sa_u = PTROUT(osa.sa_handler);
2031 CP(osa, s32, sa_flags);
2032 CP(osa, s32, sa_mask);
2033 error = copyout(&s32, uap->oact, sizeof(s32));
2038 #ifdef COMPAT_FREEBSD4
2040 freebsd4_freebsd32_sigaction(struct thread *td,
2041 struct freebsd4_freebsd32_sigaction_args *uap)
2043 struct sigaction32 s32;
2044 struct sigaction sa, osa, *sap;
2048 error = copyin(uap->act, &s32, sizeof(s32));
2051 sa.sa_handler = PTRIN(s32.sa_u);
2052 CP(s32, sa, sa_flags);
2053 CP(s32, sa, sa_mask);
2057 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2058 if (error == 0 && uap->oact != NULL) {
2059 s32.sa_u = PTROUT(osa.sa_handler);
2060 CP(osa, s32, sa_flags);
2061 CP(osa, s32, sa_mask);
2062 error = copyout(&s32, uap->oact, sizeof(s32));
2069 struct osigaction32 {
2078 ofreebsd32_sigaction(struct thread *td,
2079 struct ofreebsd32_sigaction_args *uap)
2081 struct osigaction32 s32;
2082 struct sigaction sa, osa, *sap;
2085 if (uap->signum <= 0 || uap->signum >= ONSIG)
2089 error = copyin(uap->nsa, &s32, sizeof(s32));
2092 sa.sa_handler = PTRIN(s32.sa_u);
2093 CP(s32, sa, sa_flags);
2094 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2098 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2099 if (error == 0 && uap->osa != NULL) {
2100 s32.sa_u = PTROUT(osa.sa_handler);
2101 CP(osa, s32, sa_flags);
2102 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2103 error = copyout(&s32, uap->osa, sizeof(s32));
2109 ofreebsd32_sigprocmask(struct thread *td,
2110 struct ofreebsd32_sigprocmask_args *uap)
2115 OSIG2SIG(uap->mask, set);
2116 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2117 SIG2OSIG(oset, td->td_retval[0]);
2122 ofreebsd32_sigpending(struct thread *td,
2123 struct ofreebsd32_sigpending_args *uap)
2125 struct proc *p = td->td_proc;
2129 siglist = p->p_siglist;
2130 SIGSETOR(siglist, td->td_siglist);
2132 SIG2OSIG(siglist, td->td_retval[0]);
2137 u_int32_t sv_handler;
2143 ofreebsd32_sigvec(struct thread *td,
2144 struct ofreebsd32_sigvec_args *uap)
2146 struct sigvec32 vec;
2147 struct sigaction sa, osa, *sap;
2150 if (uap->signum <= 0 || uap->signum >= ONSIG)
2154 error = copyin(uap->nsv, &vec, sizeof(vec));
2157 sa.sa_handler = PTRIN(vec.sv_handler);
2158 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2159 sa.sa_flags = vec.sv_flags;
2160 sa.sa_flags ^= SA_RESTART;
2164 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2165 if (error == 0 && uap->osv != NULL) {
2166 vec.sv_handler = PTROUT(osa.sa_handler);
2167 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2168 vec.sv_flags = osa.sa_flags;
2169 vec.sv_flags &= ~SA_NOCLDWAIT;
2170 vec.sv_flags ^= SA_RESTART;
2171 error = copyout(&vec, uap->osv, sizeof(vec));
2177 ofreebsd32_sigblock(struct thread *td,
2178 struct ofreebsd32_sigblock_args *uap)
2182 OSIG2SIG(uap->mask, set);
2183 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2184 SIG2OSIG(oset, td->td_retval[0]);
2189 ofreebsd32_sigsetmask(struct thread *td,
2190 struct ofreebsd32_sigsetmask_args *uap)
2194 OSIG2SIG(uap->mask, set);
2195 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2196 SIG2OSIG(oset, td->td_retval[0]);
2201 ofreebsd32_sigsuspend(struct thread *td,
2202 struct ofreebsd32_sigsuspend_args *uap)
2206 OSIG2SIG(uap->mask, mask);
2207 return (kern_sigsuspend(td, mask));
2216 ofreebsd32_sigstack(struct thread *td,
2217 struct ofreebsd32_sigstack_args *uap)
2219 struct sigstack32 s32;
2220 struct sigstack nss, oss;
2221 int error = 0, unss;
2223 if (uap->nss != NULL) {
2224 error = copyin(uap->nss, &s32, sizeof(s32));
2227 nss.ss_sp = PTRIN(s32.ss_sp);
2228 CP(s32, nss, ss_onstack);
2233 oss.ss_sp = td->td_sigstk.ss_sp;
2234 oss.ss_onstack = sigonstack(cpu_getstack(td));
2236 td->td_sigstk.ss_sp = nss.ss_sp;
2237 td->td_sigstk.ss_size = 0;
2238 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2239 td->td_pflags |= TDP_ALTSTACK;
2241 if (uap->oss != NULL) {
2242 s32.ss_sp = PTROUT(oss.ss_sp);
2243 CP(oss, s32, ss_onstack);
2244 error = copyout(&s32, uap->oss, sizeof(s32));
2251 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2253 struct timespec32 rmt32, rqt32;
2254 struct timespec rmt, rqt;
2257 error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2261 CP(rqt32, rqt, tv_sec);
2262 CP(rqt32, rqt, tv_nsec);
2265 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2267 error = kern_nanosleep(td, &rqt, &rmt);
2268 if (error && uap->rmtp) {
2271 CP(rmt, rmt32, tv_sec);
2272 CP(rmt, rmt32, tv_nsec);
2274 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2282 freebsd32_clock_gettime(struct thread *td,
2283 struct freebsd32_clock_gettime_args *uap)
2285 struct timespec ats;
2286 struct timespec32 ats32;
2289 error = kern_clock_gettime(td, uap->clock_id, &ats);
2291 CP(ats, ats32, tv_sec);
2292 CP(ats, ats32, tv_nsec);
2293 error = copyout(&ats32, uap->tp, sizeof(ats32));
2299 freebsd32_clock_settime(struct thread *td,
2300 struct freebsd32_clock_settime_args *uap)
2302 struct timespec ats;
2303 struct timespec32 ats32;
2306 error = copyin(uap->tp, &ats32, sizeof(ats32));
2309 CP(ats32, ats, tv_sec);
2310 CP(ats32, ats, tv_nsec);
2312 return (kern_clock_settime(td, uap->clock_id, &ats));
2316 freebsd32_clock_getres(struct thread *td,
2317 struct freebsd32_clock_getres_args *uap)
2320 struct timespec32 ts32;
2323 if (uap->tp == NULL)
2325 error = kern_clock_getres(td, uap->clock_id, &ts);
2327 CP(ts, ts32, tv_sec);
2328 CP(ts, ts32, tv_nsec);
2329 error = copyout(&ts32, uap->tp, sizeof(ts32));
2335 freebsd32_clock_getcpuclockid2(struct thread *td,
2336 struct freebsd32_clock_getcpuclockid2_args *uap)
2341 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2342 uap->which, &clk_id);
2344 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2349 freebsd32_thr_new(struct thread *td,
2350 struct freebsd32_thr_new_args *uap)
2352 struct thr_param32 param32;
2353 struct thr_param param;
2356 if (uap->param_size < 0 ||
2357 uap->param_size > sizeof(struct thr_param32))
2359 bzero(¶m, sizeof(struct thr_param));
2360 bzero(¶m32, sizeof(struct thr_param32));
2361 error = copyin(uap->param, ¶m32, uap->param_size);
2364 param.start_func = PTRIN(param32.start_func);
2365 param.arg = PTRIN(param32.arg);
2366 param.stack_base = PTRIN(param32.stack_base);
2367 param.stack_size = param32.stack_size;
2368 param.tls_base = PTRIN(param32.tls_base);
2369 param.tls_size = param32.tls_size;
2370 param.child_tid = PTRIN(param32.child_tid);
2371 param.parent_tid = PTRIN(param32.parent_tid);
2372 param.flags = param32.flags;
2373 param.rtp = PTRIN(param32.rtp);
2374 param.spare[0] = PTRIN(param32.spare[0]);
2375 param.spare[1] = PTRIN(param32.spare[1]);
2376 param.spare[2] = PTRIN(param32.spare[2]);
2378 return (kern_thr_new(td, ¶m));
2382 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2384 struct timespec32 ts32;
2385 struct timespec ts, *tsp;
2390 if (uap->timeout != NULL) {
2391 error = copyin((const void *)uap->timeout, (void *)&ts32,
2392 sizeof(struct timespec32));
2395 ts.tv_sec = ts32.tv_sec;
2396 ts.tv_nsec = ts32.tv_nsec;
2399 return (kern_thr_suspend(td, tsp));
2403 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2405 bzero(dst, sizeof(*dst));
2406 dst->si_signo = src->si_signo;
2407 dst->si_errno = src->si_errno;
2408 dst->si_code = src->si_code;
2409 dst->si_pid = src->si_pid;
2410 dst->si_uid = src->si_uid;
2411 dst->si_status = src->si_status;
2412 dst->si_addr = (uintptr_t)src->si_addr;
2413 dst->si_value.sival_int = src->si_value.sival_int;
2414 dst->si_timerid = src->si_timerid;
2415 dst->si_overrun = src->si_overrun;
2419 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2421 struct timespec32 ts32;
2423 struct timespec *timeout;
2426 struct siginfo32 si32;
2430 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2433 ts.tv_sec = ts32.tv_sec;
2434 ts.tv_nsec = ts32.tv_nsec;
2439 error = copyin(uap->set, &set, sizeof(set));
2443 error = kern_sigtimedwait(td, set, &ksi, timeout);
2448 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2449 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2453 td->td_retval[0] = ksi.ksi_signo;
2461 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2464 struct siginfo32 si32;
2468 error = copyin(uap->set, &set, sizeof(set));
2472 error = kern_sigtimedwait(td, set, &ksi, NULL);
2477 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2478 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2481 td->td_retval[0] = ksi.ksi_signo;
2486 freebsd32_cpuset_setid(struct thread *td,
2487 struct freebsd32_cpuset_setid_args *uap)
2489 struct cpuset_setid_args ap;
2491 ap.which = uap->which;
2492 ap.id = PAIR32TO64(id_t,uap->id);
2493 ap.setid = uap->setid;
2495 return (sys_cpuset_setid(td, &ap));
2499 freebsd32_cpuset_getid(struct thread *td,
2500 struct freebsd32_cpuset_getid_args *uap)
2502 struct cpuset_getid_args ap;
2504 ap.level = uap->level;
2505 ap.which = uap->which;
2506 ap.id = PAIR32TO64(id_t,uap->id);
2507 ap.setid = uap->setid;
2509 return (sys_cpuset_getid(td, &ap));
2513 freebsd32_cpuset_getaffinity(struct thread *td,
2514 struct freebsd32_cpuset_getaffinity_args *uap)
2516 struct cpuset_getaffinity_args ap;
2518 ap.level = uap->level;
2519 ap.which = uap->which;
2520 ap.id = PAIR32TO64(id_t,uap->id);
2521 ap.cpusetsize = uap->cpusetsize;
2522 ap.mask = uap->mask;
2524 return (sys_cpuset_getaffinity(td, &ap));
2528 freebsd32_cpuset_setaffinity(struct thread *td,
2529 struct freebsd32_cpuset_setaffinity_args *uap)
2531 struct cpuset_setaffinity_args ap;
2533 ap.level = uap->level;
2534 ap.which = uap->which;
2535 ap.id = PAIR32TO64(id_t,uap->id);
2536 ap.cpusetsize = uap->cpusetsize;
2537 ap.mask = uap->mask;
2539 return (sys_cpuset_setaffinity(td, &ap));
2543 freebsd32_nmount(struct thread *td,
2544 struct freebsd32_nmount_args /* {
2546 unsigned int iovcnt;
2555 * Mount flags are now 64-bits. On 32-bit archtectures only
2556 * 32-bits are passed in, but from here on everything handles
2557 * 64-bit flags correctly.
2561 AUDIT_ARG_FFLAGS(flags);
2564 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2565 * userspace to set this flag, but we must filter it out if we want
2566 * MNT_UPDATE on the root file system to work.
2567 * MNT_ROOTFS should only be set by the kernel when mounting its
2570 flags &= ~MNT_ROOTFS;
2573 * check that we have an even number of iovec's
2574 * and that we have at least two options.
2576 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2579 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2582 error = vfs_donmount(td, flags, auio);
2590 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2592 struct yyy32 *p32, s32;
2593 struct yyy *p = NULL, s;
2598 error = copyin(uap->zzz, &s32, sizeof(s32));
2604 error = kern_xxx(td, p);
2609 error = copyout(&s32, p32, sizeof(s32));
2616 syscall32_register(int *offset, struct sysent *new_sysent,
2617 struct sysent *old_sysent)
2619 if (*offset == NO_SYSCALL) {
2622 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2623 if (freebsd32_sysent[i].sy_call ==
2624 (sy_call_t *)lkmnosys)
2626 if (i == SYS_MAXSYSCALL)
2629 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2631 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2632 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2635 *old_sysent = freebsd32_sysent[*offset];
2636 freebsd32_sysent[*offset] = *new_sysent;
2641 syscall32_deregister(int *offset, struct sysent *old_sysent)
2645 freebsd32_sysent[*offset] = *old_sysent;
2650 syscall32_module_handler(struct module *mod, int what, void *arg)
2652 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2658 error = syscall32_register(data->offset, data->new_sysent,
2661 /* Leave a mark so we know to safely unload below. */
2662 data->offset = NULL;
2665 ms.intval = *data->offset;
2667 module_setspecific(mod, &ms);
2670 error = data->chainevh(mod, what, data->chainarg);
2674 * MOD_LOAD failed, so just return without calling the
2675 * chained handler since we didn't pass along the MOD_LOAD
2678 if (data->offset == NULL)
2680 if (data->chainevh) {
2681 error = data->chainevh(mod, what, data->chainarg);
2685 error = syscall32_deregister(data->offset, &data->old_sysent);
2690 error = data->chainevh(mod, what, data->chainarg);
2696 syscall32_helper_register(struct syscall_helper_data *sd)
2698 struct syscall_helper_data *sd1;
2701 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2702 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2705 syscall32_helper_unregister(sd);
2708 sd1->registered = 1;
2714 syscall32_helper_unregister(struct syscall_helper_data *sd)
2716 struct syscall_helper_data *sd1;
2718 for (sd1 = sd; sd1->registered != 0; sd1++) {
2719 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2720 sd1->registered = 0;
2726 freebsd32_copyout_strings(struct image_params *imgp)
2730 char *stringp, *destp;
2731 u_int32_t *stack_base;
2732 struct freebsd32_ps_strings *arginfo;
2733 char canary[sizeof(long) * 8];
2734 int32_t pagesizes32[MAXPAGESIZES];
2735 size_t execpath_len;
2739 * Calculate string base and vector table pointers.
2740 * Also deal with signal trampoline code for this exec type.
2742 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2743 execpath_len = strlen(imgp->execpath) + 1;
2746 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2748 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2749 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2752 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2753 roundup(execpath_len, sizeof(char *)) -
2754 roundup(sizeof(canary), sizeof(char *)) -
2755 roundup(sizeof(pagesizes32), sizeof(char *)) -
2756 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2762 copyout(imgp->proc->p_sysent->sv_sigcode,
2763 ((caddr_t)arginfo - szsigcode), szsigcode);
2766 * Copy the image path for the rtld.
2768 if (execpath_len != 0) {
2769 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2770 copyout(imgp->execpath, (void *)imgp->execpathp,
2775 * Prepare the canary for SSP.
2777 arc4rand(canary, sizeof(canary), 0);
2778 imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2780 copyout(canary, (void *)imgp->canary, sizeof(canary));
2781 imgp->canarylen = sizeof(canary);
2784 * Prepare the pagesizes array.
2786 for (i = 0; i < MAXPAGESIZES; i++)
2787 pagesizes32[i] = (uint32_t)pagesizes[i];
2788 imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2789 roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2790 copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2791 imgp->pagesizeslen = sizeof(pagesizes32);
2794 * If we have a valid auxargs ptr, prepare some room
2797 if (imgp->auxargs) {
2799 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2800 * lower compatibility.
2802 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2805 * The '+ 2' is for the null pointers at the end of each of
2806 * the arg and env vector sets,and imgp->auxarg_size is room
2807 * for argument of Runtime loader.
2809 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2810 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2814 * The '+ 2' is for the null pointers at the end of each of
2815 * the arg and env vector sets
2817 vectp = (u_int32_t *)
2818 (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2821 * vectp also becomes our initial stack base
2825 stringp = imgp->args->begin_argv;
2826 argc = imgp->args->argc;
2827 envc = imgp->args->envc;
2829 * Copy out strings - arguments and environment.
2831 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2834 * Fill in "ps_strings" struct for ps, w, etc.
2836 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2837 suword32(&arginfo->ps_nargvstr, argc);
2840 * Fill in argument portion of vector table.
2842 for (; argc > 0; --argc) {
2843 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2844 while (*stringp++ != 0)
2849 /* a null vector table pointer separates the argp's from the envp's */
2850 suword32(vectp++, 0);
2852 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2853 suword32(&arginfo->ps_nenvstr, envc);
2856 * Fill in environment portion of vector table.
2858 for (; envc > 0; --envc) {
2859 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2860 while (*stringp++ != 0)
2865 /* end of vector table is a null pointer */
2868 return ((register_t *)stack_base);
2872 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2874 struct kld_file_stat stat;
2875 struct kld32_file_stat stat32;
2878 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2881 if (version != sizeof(struct kld32_file_stat_1) &&
2882 version != sizeof(struct kld32_file_stat))
2885 error = kern_kldstat(td, uap->fileid, &stat);
2889 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2890 CP(stat, stat32, refs);
2891 CP(stat, stat32, id);
2892 PTROUT_CP(stat, stat32, address);
2893 CP(stat, stat32, size);
2894 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2895 return (copyout(&stat32, uap->stat, version));
2899 freebsd32_posix_fallocate(struct thread *td,
2900 struct freebsd32_posix_fallocate_args *uap)
2903 return (kern_posix_fallocate(td, uap->fd,
2904 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2908 freebsd32_posix_fadvise(struct thread *td,
2909 struct freebsd32_posix_fadvise_args *uap)
2912 return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2913 PAIR32TO64(off_t, uap->len), uap->advice));