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>
108 CTASSERT(sizeof(struct timeval32) == 8);
109 CTASSERT(sizeof(struct timespec32) == 8);
110 CTASSERT(sizeof(struct itimerval32) == 16);
112 CTASSERT(sizeof(struct statfs32) == 256);
114 CTASSERT(sizeof(struct rusage32) == 72);
116 CTASSERT(sizeof(struct sigaltstack32) == 12);
117 CTASSERT(sizeof(struct kevent32) == 20);
118 CTASSERT(sizeof(struct iovec32) == 8);
119 CTASSERT(sizeof(struct msghdr32) == 28);
121 CTASSERT(sizeof(struct stat32) == 96);
123 CTASSERT(sizeof(struct sigaction32) == 24);
125 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
126 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
128 #if BYTE_ORDER == BIG_ENDIAN
129 #define PAIR32TO64(type, name) ((name ## 2) | ((type)(name ## 1) << 32))
133 #define PAIR32TO64(type, name) ((name ## 1) | ((type)(name ## 2) << 32))
139 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
142 TV_CP(*s, *s32, ru_utime);
143 TV_CP(*s, *s32, ru_stime);
144 CP(*s, *s32, ru_maxrss);
145 CP(*s, *s32, ru_ixrss);
146 CP(*s, *s32, ru_idrss);
147 CP(*s, *s32, ru_isrss);
148 CP(*s, *s32, ru_minflt);
149 CP(*s, *s32, ru_majflt);
150 CP(*s, *s32, ru_nswap);
151 CP(*s, *s32, ru_inblock);
152 CP(*s, *s32, ru_oublock);
153 CP(*s, *s32, ru_msgsnd);
154 CP(*s, *s32, ru_msgrcv);
155 CP(*s, *s32, ru_nsignals);
156 CP(*s, *s32, ru_nvcsw);
157 CP(*s, *s32, ru_nivcsw);
161 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
164 struct rusage32 ru32;
165 struct rusage ru, *rup;
167 if (uap->rusage != NULL)
171 error = kern_wait(td, uap->pid, &status, uap->options, rup);
174 if (uap->status != NULL)
175 error = copyout(&status, uap->status, sizeof(status));
176 if (uap->rusage != NULL && error == 0) {
177 freebsd32_rusage_out(&ru, &ru32);
178 error = copyout(&ru32, uap->rusage, sizeof(ru32));
184 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
186 struct wrusage32 wru32;
187 struct __wrusage wru, *wrup;
188 struct siginfo32 si32;
189 struct __siginfo si, *sip;
192 if (uap->wrusage != NULL)
196 if (uap->info != NULL) {
198 bzero(sip, sizeof(*sip));
201 error = kern_wait6(td, uap->idtype, uap->id, &status, uap->options,
205 if (uap->status != NULL)
206 error = copyout(&status, uap->status, sizeof(status));
207 if (uap->wrusage != NULL && error == 0) {
208 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
209 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
210 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
212 if (uap->info != NULL && error == 0) {
213 siginfo_to_siginfo32 (&si, &si32);
214 error = copyout(&si32, uap->info, sizeof(si32));
219 #ifdef COMPAT_FREEBSD4
221 copy_statfs(struct statfs *in, struct statfs32 *out)
224 statfs_scale_blocks(in, INT32_MAX);
225 bzero(out, sizeof(*out));
226 CP(*in, *out, f_bsize);
227 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
228 CP(*in, *out, f_blocks);
229 CP(*in, *out, f_bfree);
230 CP(*in, *out, f_bavail);
231 out->f_files = MIN(in->f_files, INT32_MAX);
232 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
233 CP(*in, *out, f_fsid);
234 CP(*in, *out, f_owner);
235 CP(*in, *out, f_type);
236 CP(*in, *out, f_flags);
237 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
238 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
239 strlcpy(out->f_fstypename,
240 in->f_fstypename, MFSNAMELEN);
241 strlcpy(out->f_mntonname,
242 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
243 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
244 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
245 strlcpy(out->f_mntfromname,
246 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
250 #ifdef COMPAT_FREEBSD4
252 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
254 struct statfs *buf, *sp;
255 struct statfs32 stat32;
259 count = uap->bufsize / sizeof(struct statfs32);
260 size = count * sizeof(struct statfs);
261 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
263 count = td->td_retval[0];
265 while (count > 0 && error == 0) {
266 copy_statfs(sp, &stat32);
267 error = copyout(&stat32, uap->buf, sizeof(stat32));
279 freebsd32_sigaltstack(struct thread *td,
280 struct freebsd32_sigaltstack_args *uap)
282 struct sigaltstack32 s32;
283 struct sigaltstack ss, oss, *ssp;
286 if (uap->ss != NULL) {
287 error = copyin(uap->ss, &s32, sizeof(s32));
290 PTRIN_CP(s32, ss, ss_sp);
291 CP(s32, ss, ss_size);
292 CP(s32, ss, ss_flags);
296 error = kern_sigaltstack(td, ssp, &oss);
297 if (error == 0 && uap->oss != NULL) {
298 PTROUT_CP(oss, s32, ss_sp);
299 CP(oss, s32, ss_size);
300 CP(oss, s32, ss_flags);
301 error = copyout(&s32, uap->oss, sizeof(s32));
307 * Custom version of exec_copyin_args() so that we can translate
311 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
312 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
319 bzero(args, sizeof(*args));
324 * Allocate demand-paged memory for the file name, argument, and
325 * environment strings.
327 error = exec_alloc_args(args);
332 * Copy the file name.
335 args->fname = args->buf;
336 error = (segflg == UIO_SYSSPACE) ?
337 copystr(fname, args->fname, PATH_MAX, &length) :
338 copyinstr(fname, args->fname, PATH_MAX, &length);
344 args->begin_argv = args->buf + length;
345 args->endp = args->begin_argv;
346 args->stringspace = ARG_MAX;
349 * extract arguments first
353 error = copyin(p32++, &arg, sizeof(arg));
359 error = copyinstr(argp, args->endp, args->stringspace, &length);
361 if (error == ENAMETOOLONG)
365 args->stringspace -= length;
366 args->endp += length;
370 args->begin_envv = args->endp;
373 * extract environment strings
378 error = copyin(p32++, &arg, sizeof(arg));
384 error = copyinstr(envp, args->endp, args->stringspace,
387 if (error == ENAMETOOLONG)
391 args->stringspace -= length;
392 args->endp += length;
400 exec_free_args(args);
405 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
407 struct image_args eargs;
410 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
411 uap->argv, uap->envv);
413 error = kern_execve(td, &eargs, NULL);
418 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
420 struct image_args eargs;
423 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
424 uap->argv, uap->envv);
427 error = kern_execve(td, &eargs, NULL);
434 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
435 int prot, int fd, off_t pos)
438 vm_map_entry_t entry;
441 map = &td->td_proc->p_vmspace->vm_map;
443 prot |= VM_PROT_WRITE;
445 if (vm_map_lookup_entry(map, start, &entry)) {
446 if ((entry->protection & prot) != prot) {
447 rv = vm_map_protect(map,
450 entry->protection | prot,
452 if (rv != KERN_SUCCESS)
456 vm_offset_t addr = trunc_page(start);
457 rv = vm_map_find(map, 0, 0,
458 &addr, PAGE_SIZE, FALSE, prot,
460 if (rv != KERN_SUCCESS)
467 r.buf = (void *) start;
468 r.nbyte = end - start;
470 return (sys_pread(td, &r));
472 while (start < end) {
473 subyte((void *) start, 0);
482 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
484 struct mprotect_args ap;
486 ap.addr = PTRIN(uap->addr);
489 #if defined(__amd64__) || defined(__ia64__)
490 if (i386_read_exec && (ap.prot & PROT_READ) != 0)
491 ap.prot |= PROT_EXEC;
493 return (sys_mprotect(td, &ap));
497 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
500 vm_offset_t addr = (vm_offset_t) uap->addr;
501 vm_size_t len = uap->len;
502 int prot = uap->prot;
503 int flags = uap->flags;
505 off_t pos = PAIR32TO64(off_t,uap->pos);
511 * Attempt to handle page size hassles.
513 pageoff = (pos & PAGE_MASK);
514 if (flags & MAP_FIXED) {
515 vm_offset_t start, end;
519 if (start != trunc_page(start)) {
520 error = freebsd32_mmap_partial(td, start,
521 round_page(start), prot,
524 pos += round_page(start) - start;
525 start = round_page(start);
527 if (end != round_page(end)) {
528 vm_offset_t t = trunc_page(end);
529 error = freebsd32_mmap_partial(td, t, end,
532 end = trunc_page(end);
534 if (end > start && fd != -1 && (pos & PAGE_MASK)) {
536 * We can't map this region at all. The specified
537 * address doesn't have the same alignment as the file
538 * position. Fake the mapping by simply reading the
539 * entire region into memory. First we need to make
540 * sure the region exists.
546 prot |= VM_PROT_WRITE;
547 map = &td->td_proc->p_vmspace->vm_map;
548 rv = vm_map_remove(map, start, end);
549 if (rv != KERN_SUCCESS)
551 rv = vm_map_find(map, 0, 0,
552 &start, end - start, FALSE,
553 prot, VM_PROT_ALL, 0);
554 if (rv != KERN_SUCCESS)
557 r.buf = (void *) start;
558 r.nbyte = end - start;
560 error = sys_pread(td, &r);
564 td->td_retval[0] = addr;
569 * After dealing with the ragged ends, there
570 * might be none left.
572 td->td_retval[0] = addr;
580 #if defined(__amd64__) || defined(__ia64__)
581 if (i386_read_exec && (prot & PROT_READ))
585 ap.addr = (void *) addr;
592 return (sys_mmap(td, &ap));
595 #ifdef COMPAT_FREEBSD6
597 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
599 struct freebsd32_mmap_args ap;
604 ap.flags = uap->flags;
609 return (freebsd32_mmap(td, &ap));
614 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
616 struct itimerval itv, oitv, *itvp;
617 struct itimerval32 i32;
620 if (uap->itv != NULL) {
621 error = copyin(uap->itv, &i32, sizeof(i32));
624 TV_CP(i32, itv, it_interval);
625 TV_CP(i32, itv, it_value);
629 error = kern_setitimer(td, uap->which, itvp, &oitv);
630 if (error || uap->oitv == NULL)
632 TV_CP(oitv, i32, it_interval);
633 TV_CP(oitv, i32, it_value);
634 return (copyout(&i32, uap->oitv, sizeof(i32)));
638 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
640 struct itimerval itv;
641 struct itimerval32 i32;
644 error = kern_getitimer(td, uap->which, &itv);
645 if (error || uap->itv == NULL)
647 TV_CP(itv, i32, it_interval);
648 TV_CP(itv, i32, it_value);
649 return (copyout(&i32, uap->itv, sizeof(i32)));
653 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
655 struct timeval32 tv32;
656 struct timeval tv, *tvp;
659 if (uap->tv != NULL) {
660 error = copyin(uap->tv, &tv32, sizeof(tv32));
663 CP(tv32, tv, tv_sec);
664 CP(tv32, tv, tv_usec);
669 * XXX Do pointers need PTRIN()?
671 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
672 sizeof(int32_t) * 8));
676 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
678 struct timespec32 ts32;
680 struct timeval tv, *tvp;
684 if (uap->ts != NULL) {
685 error = copyin(uap->ts, &ts32, sizeof(ts32));
688 CP(ts32, ts, tv_sec);
689 CP(ts32, ts, tv_nsec);
690 TIMESPEC_TO_TIMEVAL(&tv, &ts);
694 if (uap->sm != NULL) {
695 error = copyin(uap->sm, &set, sizeof(set));
702 * XXX Do pointers need PTRIN()?
704 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
705 uset, sizeof(int32_t) * 8);
710 * Copy 'count' items into the destination list pointed to by uap->eventlist.
713 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
715 struct freebsd32_kevent_args *uap;
716 struct kevent32 ks32[KQ_NEVENTS];
719 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
720 uap = (struct freebsd32_kevent_args *)arg;
722 for (i = 0; i < count; i++) {
723 CP(kevp[i], ks32[i], ident);
724 CP(kevp[i], ks32[i], filter);
725 CP(kevp[i], ks32[i], flags);
726 CP(kevp[i], ks32[i], fflags);
727 CP(kevp[i], ks32[i], data);
728 PTROUT_CP(kevp[i], ks32[i], udata);
730 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
732 uap->eventlist += count;
737 * Copy 'count' items from the list pointed to by uap->changelist.
740 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
742 struct freebsd32_kevent_args *uap;
743 struct kevent32 ks32[KQ_NEVENTS];
746 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
747 uap = (struct freebsd32_kevent_args *)arg;
749 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
752 uap->changelist += count;
754 for (i = 0; i < count; i++) {
755 CP(ks32[i], kevp[i], ident);
756 CP(ks32[i], kevp[i], filter);
757 CP(ks32[i], kevp[i], flags);
758 CP(ks32[i], kevp[i], fflags);
759 CP(ks32[i], kevp[i], data);
760 PTRIN_CP(ks32[i], kevp[i], udata);
767 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
769 struct timespec32 ts32;
770 struct timespec ts, *tsp;
771 struct kevent_copyops k_ops = { uap,
772 freebsd32_kevent_copyout,
773 freebsd32_kevent_copyin};
778 error = copyin(uap->timeout, &ts32, sizeof(ts32));
781 CP(ts32, ts, tv_sec);
782 CP(ts32, ts, tv_nsec);
786 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
792 freebsd32_gettimeofday(struct thread *td,
793 struct freebsd32_gettimeofday_args *uap)
796 struct timeval32 atv32;
802 CP(atv, atv32, tv_sec);
803 CP(atv, atv32, tv_usec);
804 error = copyout(&atv32, uap->tp, sizeof (atv32));
806 if (error == 0 && uap->tzp != NULL) {
807 rtz.tz_minuteswest = tz_minuteswest;
808 rtz.tz_dsttime = tz_dsttime;
809 error = copyout(&rtz, uap->tzp, sizeof (rtz));
815 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
821 error = kern_getrusage(td, uap->who, &s);
824 if (uap->rusage != NULL) {
825 freebsd32_rusage_out(&s, &s32);
826 error = copyout(&s32, uap->rusage, sizeof(s32));
832 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
834 struct iovec32 iov32;
841 if (iovcnt > UIO_MAXIOV)
843 iovlen = iovcnt * sizeof(struct iovec);
844 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
845 iov = (struct iovec *)(uio + 1);
846 for (i = 0; i < iovcnt; i++) {
847 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
852 iov[i].iov_base = PTRIN(iov32.iov_base);
853 iov[i].iov_len = iov32.iov_len;
856 uio->uio_iovcnt = iovcnt;
857 uio->uio_segflg = UIO_USERSPACE;
858 uio->uio_offset = -1;
860 for (i = 0; i < iovcnt; i++) {
861 if (iov->iov_len > INT_MAX - uio->uio_resid) {
865 uio->uio_resid += iov->iov_len;
873 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
878 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
881 error = kern_readv(td, uap->fd, auio);
887 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
892 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
895 error = kern_writev(td, uap->fd, auio);
901 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
906 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
909 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
915 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
920 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
923 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
929 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
932 struct iovec32 iov32;
938 if (iovcnt > UIO_MAXIOV)
940 iovlen = iovcnt * sizeof(struct iovec);
941 iov = malloc(iovlen, M_IOV, M_WAITOK);
942 for (i = 0; i < iovcnt; i++) {
943 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
948 iov[i].iov_base = PTRIN(iov32.iov_base);
949 iov[i].iov_len = iov32.iov_len;
956 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
961 error = copyin(msg32, &m32, sizeof(m32));
964 msg->msg_name = PTRIN(m32.msg_name);
965 msg->msg_namelen = m32.msg_namelen;
966 msg->msg_iov = PTRIN(m32.msg_iov);
967 msg->msg_iovlen = m32.msg_iovlen;
968 msg->msg_control = PTRIN(m32.msg_control);
969 msg->msg_controllen = m32.msg_controllen;
970 msg->msg_flags = m32.msg_flags;
975 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
980 m32.msg_name = PTROUT(msg->msg_name);
981 m32.msg_namelen = msg->msg_namelen;
982 m32.msg_iov = PTROUT(msg->msg_iov);
983 m32.msg_iovlen = msg->msg_iovlen;
984 m32.msg_control = PTROUT(msg->msg_control);
985 m32.msg_controllen = msg->msg_controllen;
986 m32.msg_flags = msg->msg_flags;
987 error = copyout(&m32, msg32, sizeof(m32));
992 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
994 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
996 #define FREEBSD32_ALIGN(p) \
997 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
998 #define FREEBSD32_CMSG_SPACE(l) \
999 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1001 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
1002 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1004 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1008 socklen_t clen, datalen;
1011 int len, maxlen, copylen;
1015 len = msg->msg_controllen;
1016 maxlen = msg->msg_controllen;
1017 msg->msg_controllen = 0;
1020 ctlbuf = msg->msg_control;
1022 while (m && len > 0) {
1023 cm = mtod(m, struct cmsghdr *);
1026 while (cm != NULL) {
1028 if (sizeof(struct cmsghdr) > clen ||
1029 cm->cmsg_len > clen) {
1034 data = CMSG_DATA(cm);
1035 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1037 /* Adjust message length */
1038 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1043 copylen = sizeof(struct cmsghdr);
1044 if (len < copylen) {
1045 msg->msg_flags |= MSG_CTRUNC;
1049 error = copyout(cm,ctlbuf,copylen);
1053 ctlbuf += FREEBSD32_ALIGN(copylen);
1054 len -= FREEBSD32_ALIGN(copylen);
1061 if (len < copylen) {
1062 msg->msg_flags |= MSG_CTRUNC;
1066 error = copyout(data,ctlbuf,copylen);
1070 ctlbuf += FREEBSD32_ALIGN(copylen);
1071 len -= FREEBSD32_ALIGN(copylen);
1073 if (CMSG_SPACE(datalen) < clen) {
1074 clen -= CMSG_SPACE(datalen);
1075 cm = (struct cmsghdr *)
1076 ((caddr_t)cm + CMSG_SPACE(datalen));
1085 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1093 freebsd32_recvmsg(td, uap)
1095 struct freebsd32_recvmsg_args /* {
1097 struct msghdr32 *msg;
1102 struct msghdr32 m32;
1103 struct iovec *uiov, *iov;
1104 struct mbuf *control = NULL;
1105 struct mbuf **controlp;
1108 error = copyin(uap->msg, &m32, sizeof(m32));
1111 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1114 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1118 msg.msg_flags = uap->flags;
1122 controlp = (msg.msg_control != NULL) ? &control : NULL;
1123 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1127 if (control != NULL)
1128 error = freebsd32_copy_msg_out(&msg, control);
1130 msg.msg_controllen = 0;
1133 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1137 if (control != NULL)
1145 freebsd32_convert_msg_in(struct mbuf **controlp)
1147 struct mbuf *control = *controlp;
1148 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1150 socklen_t clen = control->m_len, datalen;
1156 while (cm != NULL) {
1157 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1162 data = FREEBSD32_CMSG_DATA(cm);
1163 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1165 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1167 controlp = &(*controlp)->m_next;
1169 if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1170 clen -= FREEBSD32_CMSG_SPACE(datalen);
1171 cm = (struct cmsghdr *)
1172 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1185 freebsd32_sendmsg(struct thread *td,
1186 struct freebsd32_sendmsg_args *uap)
1189 struct msghdr32 m32;
1191 struct mbuf *control = NULL;
1192 struct sockaddr *to = NULL;
1195 error = copyin(uap->msg, &m32, sizeof(m32));
1198 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1201 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1206 if (msg.msg_name != NULL) {
1207 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1215 if (msg.msg_control) {
1216 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1221 error = sockargs(&control, msg.msg_control,
1222 msg.msg_controllen, MT_CONTROL);
1226 error = freebsd32_convert_msg_in(&control);
1231 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1242 freebsd32_recvfrom(struct thread *td,
1243 struct freebsd32_recvfrom_args *uap)
1249 if (uap->fromlenaddr) {
1250 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1251 sizeof(msg.msg_namelen));
1255 msg.msg_namelen = 0;
1258 msg.msg_name = PTRIN(uap->from);
1259 msg.msg_iov = &aiov;
1261 aiov.iov_base = PTRIN(uap->buf);
1262 aiov.iov_len = uap->len;
1263 msg.msg_control = NULL;
1264 msg.msg_flags = uap->flags;
1265 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1266 if (error == 0 && uap->fromlenaddr)
1267 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1268 sizeof (msg.msg_namelen));
1273 freebsd32_settimeofday(struct thread *td,
1274 struct freebsd32_settimeofday_args *uap)
1276 struct timeval32 tv32;
1277 struct timeval tv, *tvp;
1278 struct timezone tz, *tzp;
1282 error = copyin(uap->tv, &tv32, sizeof(tv32));
1285 CP(tv32, tv, tv_sec);
1286 CP(tv32, tv, tv_usec);
1291 error = copyin(uap->tzp, &tz, sizeof(tz));
1297 return (kern_settimeofday(td, tvp, tzp));
1301 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1303 struct timeval32 s32[2];
1304 struct timeval s[2], *sp;
1307 if (uap->tptr != NULL) {
1308 error = copyin(uap->tptr, s32, sizeof(s32));
1311 CP(s32[0], s[0], tv_sec);
1312 CP(s32[0], s[0], tv_usec);
1313 CP(s32[1], s[1], tv_sec);
1314 CP(s32[1], s[1], tv_usec);
1318 return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1322 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1324 struct timeval32 s32[2];
1325 struct timeval s[2], *sp;
1328 if (uap->tptr != NULL) {
1329 error = copyin(uap->tptr, s32, sizeof(s32));
1332 CP(s32[0], s[0], tv_sec);
1333 CP(s32[0], s[0], tv_usec);
1334 CP(s32[1], s[1], tv_sec);
1335 CP(s32[1], s[1], tv_usec);
1339 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1343 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1345 struct timeval32 s32[2];
1346 struct timeval s[2], *sp;
1349 if (uap->tptr != NULL) {
1350 error = copyin(uap->tptr, s32, sizeof(s32));
1353 CP(s32[0], s[0], tv_sec);
1354 CP(s32[0], s[0], tv_usec);
1355 CP(s32[1], s[1], tv_sec);
1356 CP(s32[1], s[1], tv_usec);
1360 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1364 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1366 struct timeval32 s32[2];
1367 struct timeval s[2], *sp;
1370 if (uap->times != NULL) {
1371 error = copyin(uap->times, s32, sizeof(s32));
1374 CP(s32[0], s[0], tv_sec);
1375 CP(s32[0], s[0], tv_usec);
1376 CP(s32[1], s[1], tv_sec);
1377 CP(s32[1], s[1], tv_usec);
1381 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1386 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1388 struct timeval32 tv32;
1389 struct timeval delta, olddelta, *deltap;
1393 error = copyin(uap->delta, &tv32, sizeof(tv32));
1396 CP(tv32, delta, tv_sec);
1397 CP(tv32, delta, tv_usec);
1401 error = kern_adjtime(td, deltap, &olddelta);
1402 if (uap->olddelta && error == 0) {
1403 CP(olddelta, tv32, tv_sec);
1404 CP(olddelta, tv32, tv_usec);
1405 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1410 #ifdef COMPAT_FREEBSD4
1412 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1414 struct statfs32 s32;
1418 error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1421 copy_statfs(&s, &s32);
1422 return (copyout(&s32, uap->buf, sizeof(s32)));
1426 #ifdef COMPAT_FREEBSD4
1428 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1430 struct statfs32 s32;
1434 error = kern_fstatfs(td, uap->fd, &s);
1437 copy_statfs(&s, &s32);
1438 return (copyout(&s32, uap->buf, sizeof(s32)));
1442 #ifdef COMPAT_FREEBSD4
1444 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1446 struct statfs32 s32;
1451 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1453 error = kern_fhstatfs(td, fh, &s);
1456 copy_statfs(&s, &s32);
1457 return (copyout(&s32, uap->buf, sizeof(s32)));
1462 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1464 struct pread_args ap;
1468 ap.nbyte = uap->nbyte;
1469 ap.offset = PAIR32TO64(off_t,uap->offset);
1470 return (sys_pread(td, &ap));
1474 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1476 struct pwrite_args ap;
1480 ap.nbyte = uap->nbyte;
1481 ap.offset = PAIR32TO64(off_t,uap->offset);
1482 return (sys_pwrite(td, &ap));
1487 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1489 struct lseek_args nuap;
1492 nuap.offset = uap->offset;
1493 nuap.whence = uap->whence;
1494 return (sys_lseek(td, &nuap));
1499 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1502 struct lseek_args ap;
1506 ap.offset = PAIR32TO64(off_t,uap->offset);
1507 ap.whence = uap->whence;
1508 error = sys_lseek(td, &ap);
1509 /* Expand the quad return into two parts for eax and edx */
1510 pos = *(off_t *)(td->td_retval);
1511 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1512 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1517 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1519 struct truncate_args ap;
1521 ap.path = uap->path;
1522 ap.length = PAIR32TO64(off_t,uap->length);
1523 return (sys_truncate(td, &ap));
1527 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1529 struct ftruncate_args ap;
1532 ap.length = PAIR32TO64(off_t,uap->length);
1533 return (sys_ftruncate(td, &ap));
1538 ofreebsd32_getdirentries(struct thread *td,
1539 struct ofreebsd32_getdirentries_args *uap)
1541 struct ogetdirentries_args ap;
1548 ap.count = uap->count;
1550 error = kern_ogetdirentries(td, &ap, &loff);
1553 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1560 freebsd32_getdirentries(struct thread *td,
1561 struct freebsd32_getdirentries_args *uap)
1567 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1568 NULL, UIO_USERSPACE);
1571 if (uap->basep != NULL) {
1573 error = copyout(&base32, uap->basep, sizeof(int32_t));
1578 #ifdef COMPAT_FREEBSD6
1579 /* versions with the 'int pad' argument */
1581 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1583 struct pread_args ap;
1587 ap.nbyte = uap->nbyte;
1588 ap.offset = PAIR32TO64(off_t,uap->offset);
1589 return (sys_pread(td, &ap));
1593 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1595 struct pwrite_args ap;
1599 ap.nbyte = uap->nbyte;
1600 ap.offset = PAIR32TO64(off_t,uap->offset);
1601 return (sys_pwrite(td, &ap));
1605 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1608 struct lseek_args ap;
1612 ap.offset = PAIR32TO64(off_t,uap->offset);
1613 ap.whence = uap->whence;
1614 error = sys_lseek(td, &ap);
1615 /* Expand the quad return into two parts for eax and edx */
1616 pos = *(off_t *)(td->td_retval);
1617 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1618 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1623 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1625 struct truncate_args ap;
1627 ap.path = uap->path;
1628 ap.length = PAIR32TO64(off_t,uap->length);
1629 return (sys_truncate(td, &ap));
1633 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1635 struct ftruncate_args ap;
1638 ap.length = PAIR32TO64(off_t,uap->length);
1639 return (sys_ftruncate(td, &ap));
1641 #endif /* COMPAT_FREEBSD6 */
1651 freebsd32_do_sendfile(struct thread *td,
1652 struct freebsd32_sendfile_args *uap, int compat)
1654 struct sendfile_args ap;
1655 struct sf_hdtr32 hdtr32;
1656 struct sf_hdtr hdtr;
1657 struct uio *hdr_uio, *trl_uio;
1658 struct iovec32 *iov32;
1661 hdr_uio = trl_uio = NULL;
1665 ap.offset = PAIR32TO64(off_t,uap->offset);
1666 ap.nbytes = uap->nbytes;
1667 ap.hdtr = (struct sf_hdtr *)uap->hdtr; /* XXX not used */
1668 ap.sbytes = uap->sbytes;
1669 ap.flags = uap->flags;
1671 if (uap->hdtr != NULL) {
1672 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1675 PTRIN_CP(hdtr32, hdtr, headers);
1676 CP(hdtr32, hdtr, hdr_cnt);
1677 PTRIN_CP(hdtr32, hdtr, trailers);
1678 CP(hdtr32, hdtr, trl_cnt);
1680 if (hdtr.headers != NULL) {
1681 iov32 = PTRIN(hdtr32.headers);
1682 error = freebsd32_copyinuio(iov32,
1683 hdtr32.hdr_cnt, &hdr_uio);
1687 if (hdtr.trailers != NULL) {
1688 iov32 = PTRIN(hdtr32.trailers);
1689 error = freebsd32_copyinuio(iov32,
1690 hdtr32.trl_cnt, &trl_uio);
1696 error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1699 free(hdr_uio, M_IOV);
1701 free(trl_uio, M_IOV);
1705 #ifdef COMPAT_FREEBSD4
1707 freebsd4_freebsd32_sendfile(struct thread *td,
1708 struct freebsd4_freebsd32_sendfile_args *uap)
1710 return (freebsd32_do_sendfile(td,
1711 (struct freebsd32_sendfile_args *)uap, 1));
1716 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1719 return (freebsd32_do_sendfile(td, uap, 0));
1723 copy_stat(struct stat *in, struct stat32 *out)
1726 CP(*in, *out, st_dev);
1727 CP(*in, *out, st_ino);
1728 CP(*in, *out, st_mode);
1729 CP(*in, *out, st_nlink);
1730 CP(*in, *out, st_uid);
1731 CP(*in, *out, st_gid);
1732 CP(*in, *out, st_rdev);
1733 TS_CP(*in, *out, st_atim);
1734 TS_CP(*in, *out, st_mtim);
1735 TS_CP(*in, *out, st_ctim);
1736 CP(*in, *out, st_size);
1737 CP(*in, *out, st_blocks);
1738 CP(*in, *out, st_blksize);
1739 CP(*in, *out, st_flags);
1740 CP(*in, *out, st_gen);
1741 TS_CP(*in, *out, st_birthtim);
1746 copy_ostat(struct stat *in, struct ostat32 *out)
1749 CP(*in, *out, st_dev);
1750 CP(*in, *out, st_ino);
1751 CP(*in, *out, st_mode);
1752 CP(*in, *out, st_nlink);
1753 CP(*in, *out, st_uid);
1754 CP(*in, *out, st_gid);
1755 CP(*in, *out, st_rdev);
1756 CP(*in, *out, st_size);
1757 TS_CP(*in, *out, st_atim);
1758 TS_CP(*in, *out, st_mtim);
1759 TS_CP(*in, *out, st_ctim);
1760 CP(*in, *out, st_blksize);
1761 CP(*in, *out, st_blocks);
1762 CP(*in, *out, st_flags);
1763 CP(*in, *out, st_gen);
1768 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1774 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1777 copy_stat(&sb, &sb32);
1778 error = copyout(&sb32, uap->ub, sizeof (sb32));
1784 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1787 struct ostat32 sb32;
1790 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1793 copy_ostat(&sb, &sb32);
1794 error = copyout(&sb32, uap->ub, sizeof (sb32));
1800 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1806 error = kern_fstat(td, uap->fd, &ub);
1809 copy_stat(&ub, &ub32);
1810 error = copyout(&ub32, uap->ub, sizeof(ub32));
1816 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1819 struct ostat32 ub32;
1822 error = kern_fstat(td, uap->fd, &ub);
1825 copy_ostat(&ub, &ub32);
1826 error = copyout(&ub32, uap->ub, sizeof(ub32));
1832 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1838 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1841 copy_stat(&ub, &ub32);
1842 error = copyout(&ub32, uap->buf, sizeof(ub32));
1847 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1853 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1856 copy_stat(&sb, &sb32);
1857 error = copyout(&sb32, uap->ub, sizeof (sb32));
1863 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1866 struct ostat32 sb32;
1869 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1872 copy_ostat(&sb, &sb32);
1873 error = copyout(&sb32, uap->ub, sizeof (sb32));
1879 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1881 int error, name[CTL_MAXNAME];
1884 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1886 error = copyin(uap->name, name, uap->namelen * sizeof(int));
1890 oldlen = fuword32(uap->oldlenp);
1893 error = userland_sysctl(td, name, uap->namelen,
1894 uap->old, &oldlen, 1,
1895 uap->new, uap->newlen, &j, SCTL_MASK32);
1896 if (error && error != ENOMEM)
1899 suword32(uap->oldlenp, j);
1904 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1910 error = copyin(uap->jail, &version, sizeof(uint32_t));
1917 /* FreeBSD single IPv4 jails. */
1918 struct jail32_v0 j32_v0;
1920 bzero(&j, sizeof(struct jail));
1921 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1924 CP(j32_v0, j, version);
1925 PTRIN_CP(j32_v0, j, path);
1926 PTRIN_CP(j32_v0, j, hostname);
1927 j.ip4s = j32_v0.ip_number;
1933 * Version 1 was used by multi-IPv4 jail implementations
1934 * that never made it into the official kernel.
1938 case 2: /* JAIL_API_VERSION */
1940 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1943 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1946 CP(j32, j, version);
1947 PTRIN_CP(j32, j, path);
1948 PTRIN_CP(j32, j, hostname);
1949 PTRIN_CP(j32, j, jailname);
1952 PTRIN_CP(j32, j, ip4);
1953 PTRIN_CP(j32, j, ip6);
1958 /* Sci-Fi jails are not supported, sorry. */
1961 return (kern_jail(td, &j));
1965 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1970 /* Check that we have an even number of iovecs. */
1971 if (uap->iovcnt & 1)
1974 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1977 error = kern_jail_set(td, auio, uap->flags);
1983 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1985 struct iovec32 iov32;
1989 /* Check that we have an even number of iovecs. */
1990 if (uap->iovcnt & 1)
1993 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1996 error = kern_jail_get(td, auio, uap->flags);
1998 for (i = 0; i < uap->iovcnt; i++) {
1999 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2000 CP(auio->uio_iov[i], iov32, iov_len);
2001 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2010 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2012 struct sigaction32 s32;
2013 struct sigaction sa, osa, *sap;
2017 error = copyin(uap->act, &s32, sizeof(s32));
2020 sa.sa_handler = PTRIN(s32.sa_u);
2021 CP(s32, sa, sa_flags);
2022 CP(s32, sa, sa_mask);
2026 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2027 if (error == 0 && uap->oact != NULL) {
2028 s32.sa_u = PTROUT(osa.sa_handler);
2029 CP(osa, s32, sa_flags);
2030 CP(osa, s32, sa_mask);
2031 error = copyout(&s32, uap->oact, sizeof(s32));
2036 #ifdef COMPAT_FREEBSD4
2038 freebsd4_freebsd32_sigaction(struct thread *td,
2039 struct freebsd4_freebsd32_sigaction_args *uap)
2041 struct sigaction32 s32;
2042 struct sigaction sa, osa, *sap;
2046 error = copyin(uap->act, &s32, sizeof(s32));
2049 sa.sa_handler = PTRIN(s32.sa_u);
2050 CP(s32, sa, sa_flags);
2051 CP(s32, sa, sa_mask);
2055 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2056 if (error == 0 && uap->oact != NULL) {
2057 s32.sa_u = PTROUT(osa.sa_handler);
2058 CP(osa, s32, sa_flags);
2059 CP(osa, s32, sa_mask);
2060 error = copyout(&s32, uap->oact, sizeof(s32));
2067 struct osigaction32 {
2076 ofreebsd32_sigaction(struct thread *td,
2077 struct ofreebsd32_sigaction_args *uap)
2079 struct osigaction32 s32;
2080 struct sigaction sa, osa, *sap;
2083 if (uap->signum <= 0 || uap->signum >= ONSIG)
2087 error = copyin(uap->nsa, &s32, sizeof(s32));
2090 sa.sa_handler = PTRIN(s32.sa_u);
2091 CP(s32, sa, sa_flags);
2092 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2096 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2097 if (error == 0 && uap->osa != NULL) {
2098 s32.sa_u = PTROUT(osa.sa_handler);
2099 CP(osa, s32, sa_flags);
2100 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2101 error = copyout(&s32, uap->osa, sizeof(s32));
2107 ofreebsd32_sigprocmask(struct thread *td,
2108 struct ofreebsd32_sigprocmask_args *uap)
2113 OSIG2SIG(uap->mask, set);
2114 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2115 SIG2OSIG(oset, td->td_retval[0]);
2120 ofreebsd32_sigpending(struct thread *td,
2121 struct ofreebsd32_sigpending_args *uap)
2123 struct proc *p = td->td_proc;
2127 siglist = p->p_siglist;
2128 SIGSETOR(siglist, td->td_siglist);
2130 SIG2OSIG(siglist, td->td_retval[0]);
2135 u_int32_t sv_handler;
2141 ofreebsd32_sigvec(struct thread *td,
2142 struct ofreebsd32_sigvec_args *uap)
2144 struct sigvec32 vec;
2145 struct sigaction sa, osa, *sap;
2148 if (uap->signum <= 0 || uap->signum >= ONSIG)
2152 error = copyin(uap->nsv, &vec, sizeof(vec));
2155 sa.sa_handler = PTRIN(vec.sv_handler);
2156 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2157 sa.sa_flags = vec.sv_flags;
2158 sa.sa_flags ^= SA_RESTART;
2162 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2163 if (error == 0 && uap->osv != NULL) {
2164 vec.sv_handler = PTROUT(osa.sa_handler);
2165 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2166 vec.sv_flags = osa.sa_flags;
2167 vec.sv_flags &= ~SA_NOCLDWAIT;
2168 vec.sv_flags ^= SA_RESTART;
2169 error = copyout(&vec, uap->osv, sizeof(vec));
2175 ofreebsd32_sigblock(struct thread *td,
2176 struct ofreebsd32_sigblock_args *uap)
2180 OSIG2SIG(uap->mask, set);
2181 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2182 SIG2OSIG(oset, td->td_retval[0]);
2187 ofreebsd32_sigsetmask(struct thread *td,
2188 struct ofreebsd32_sigsetmask_args *uap)
2192 OSIG2SIG(uap->mask, set);
2193 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2194 SIG2OSIG(oset, td->td_retval[0]);
2199 ofreebsd32_sigsuspend(struct thread *td,
2200 struct ofreebsd32_sigsuspend_args *uap)
2204 OSIG2SIG(uap->mask, mask);
2205 return (kern_sigsuspend(td, mask));
2214 ofreebsd32_sigstack(struct thread *td,
2215 struct ofreebsd32_sigstack_args *uap)
2217 struct sigstack32 s32;
2218 struct sigstack nss, oss;
2219 int error = 0, unss;
2221 if (uap->nss != NULL) {
2222 error = copyin(uap->nss, &s32, sizeof(s32));
2225 nss.ss_sp = PTRIN(s32.ss_sp);
2226 CP(s32, nss, ss_onstack);
2231 oss.ss_sp = td->td_sigstk.ss_sp;
2232 oss.ss_onstack = sigonstack(cpu_getstack(td));
2234 td->td_sigstk.ss_sp = nss.ss_sp;
2235 td->td_sigstk.ss_size = 0;
2236 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2237 td->td_pflags |= TDP_ALTSTACK;
2239 if (uap->oss != NULL) {
2240 s32.ss_sp = PTROUT(oss.ss_sp);
2241 CP(oss, s32, ss_onstack);
2242 error = copyout(&s32, uap->oss, sizeof(s32));
2249 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2251 struct timespec32 rmt32, rqt32;
2252 struct timespec rmt, rqt;
2255 error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2259 CP(rqt32, rqt, tv_sec);
2260 CP(rqt32, rqt, tv_nsec);
2263 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2265 error = kern_nanosleep(td, &rqt, &rmt);
2266 if (error && uap->rmtp) {
2269 CP(rmt, rmt32, tv_sec);
2270 CP(rmt, rmt32, tv_nsec);
2272 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2280 freebsd32_clock_gettime(struct thread *td,
2281 struct freebsd32_clock_gettime_args *uap)
2283 struct timespec ats;
2284 struct timespec32 ats32;
2287 error = kern_clock_gettime(td, uap->clock_id, &ats);
2289 CP(ats, ats32, tv_sec);
2290 CP(ats, ats32, tv_nsec);
2291 error = copyout(&ats32, uap->tp, sizeof(ats32));
2297 freebsd32_clock_settime(struct thread *td,
2298 struct freebsd32_clock_settime_args *uap)
2300 struct timespec ats;
2301 struct timespec32 ats32;
2304 error = copyin(uap->tp, &ats32, sizeof(ats32));
2307 CP(ats32, ats, tv_sec);
2308 CP(ats32, ats, tv_nsec);
2310 return (kern_clock_settime(td, uap->clock_id, &ats));
2314 freebsd32_clock_getres(struct thread *td,
2315 struct freebsd32_clock_getres_args *uap)
2318 struct timespec32 ts32;
2321 if (uap->tp == NULL)
2323 error = kern_clock_getres(td, uap->clock_id, &ts);
2325 CP(ts, ts32, tv_sec);
2326 CP(ts, ts32, tv_nsec);
2327 error = copyout(&ts32, uap->tp, sizeof(ts32));
2333 freebsd32_thr_new(struct thread *td,
2334 struct freebsd32_thr_new_args *uap)
2336 struct thr_param32 param32;
2337 struct thr_param param;
2340 if (uap->param_size < 0 ||
2341 uap->param_size > sizeof(struct thr_param32))
2343 bzero(¶m, sizeof(struct thr_param));
2344 bzero(¶m32, sizeof(struct thr_param32));
2345 error = copyin(uap->param, ¶m32, uap->param_size);
2348 param.start_func = PTRIN(param32.start_func);
2349 param.arg = PTRIN(param32.arg);
2350 param.stack_base = PTRIN(param32.stack_base);
2351 param.stack_size = param32.stack_size;
2352 param.tls_base = PTRIN(param32.tls_base);
2353 param.tls_size = param32.tls_size;
2354 param.child_tid = PTRIN(param32.child_tid);
2355 param.parent_tid = PTRIN(param32.parent_tid);
2356 param.flags = param32.flags;
2357 param.rtp = PTRIN(param32.rtp);
2358 param.spare[0] = PTRIN(param32.spare[0]);
2359 param.spare[1] = PTRIN(param32.spare[1]);
2360 param.spare[2] = PTRIN(param32.spare[2]);
2362 return (kern_thr_new(td, ¶m));
2366 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2368 struct timespec32 ts32;
2369 struct timespec ts, *tsp;
2374 if (uap->timeout != NULL) {
2375 error = copyin((const void *)uap->timeout, (void *)&ts32,
2376 sizeof(struct timespec32));
2379 ts.tv_sec = ts32.tv_sec;
2380 ts.tv_nsec = ts32.tv_nsec;
2383 return (kern_thr_suspend(td, tsp));
2387 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2389 bzero(dst, sizeof(*dst));
2390 dst->si_signo = src->si_signo;
2391 dst->si_errno = src->si_errno;
2392 dst->si_code = src->si_code;
2393 dst->si_pid = src->si_pid;
2394 dst->si_uid = src->si_uid;
2395 dst->si_status = src->si_status;
2396 dst->si_addr = (uintptr_t)src->si_addr;
2397 dst->si_value.sigval_int = src->si_value.sival_int;
2398 dst->si_timerid = src->si_timerid;
2399 dst->si_overrun = src->si_overrun;
2403 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2405 struct timespec32 ts32;
2407 struct timespec *timeout;
2410 struct siginfo32 si32;
2414 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2417 ts.tv_sec = ts32.tv_sec;
2418 ts.tv_nsec = ts32.tv_nsec;
2423 error = copyin(uap->set, &set, sizeof(set));
2427 error = kern_sigtimedwait(td, set, &ksi, timeout);
2432 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2433 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2437 td->td_retval[0] = ksi.ksi_signo;
2445 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2448 struct siginfo32 si32;
2452 error = copyin(uap->set, &set, sizeof(set));
2456 error = kern_sigtimedwait(td, set, &ksi, NULL);
2461 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2462 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2465 td->td_retval[0] = ksi.ksi_signo;
2470 freebsd32_cpuset_setid(struct thread *td,
2471 struct freebsd32_cpuset_setid_args *uap)
2473 struct cpuset_setid_args ap;
2475 ap.which = uap->which;
2476 ap.id = PAIR32TO64(id_t,uap->id);
2477 ap.setid = uap->setid;
2479 return (sys_cpuset_setid(td, &ap));
2483 freebsd32_cpuset_getid(struct thread *td,
2484 struct freebsd32_cpuset_getid_args *uap)
2486 struct cpuset_getid_args ap;
2488 ap.level = uap->level;
2489 ap.which = uap->which;
2490 ap.id = PAIR32TO64(id_t,uap->id);
2491 ap.setid = uap->setid;
2493 return (sys_cpuset_getid(td, &ap));
2497 freebsd32_cpuset_getaffinity(struct thread *td,
2498 struct freebsd32_cpuset_getaffinity_args *uap)
2500 struct cpuset_getaffinity_args ap;
2502 ap.level = uap->level;
2503 ap.which = uap->which;
2504 ap.id = PAIR32TO64(id_t,uap->id);
2505 ap.cpusetsize = uap->cpusetsize;
2506 ap.mask = uap->mask;
2508 return (sys_cpuset_getaffinity(td, &ap));
2512 freebsd32_cpuset_setaffinity(struct thread *td,
2513 struct freebsd32_cpuset_setaffinity_args *uap)
2515 struct cpuset_setaffinity_args ap;
2517 ap.level = uap->level;
2518 ap.which = uap->which;
2519 ap.id = PAIR32TO64(id_t,uap->id);
2520 ap.cpusetsize = uap->cpusetsize;
2521 ap.mask = uap->mask;
2523 return (sys_cpuset_setaffinity(td, &ap));
2527 freebsd32_nmount(struct thread *td,
2528 struct freebsd32_nmount_args /* {
2530 unsigned int iovcnt;
2539 * Mount flags are now 64-bits. On 32-bit archtectures only
2540 * 32-bits are passed in, but from here on everything handles
2541 * 64-bit flags correctly.
2545 AUDIT_ARG_FFLAGS(flags);
2548 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2549 * userspace to set this flag, but we must filter it out if we want
2550 * MNT_UPDATE on the root file system to work.
2551 * MNT_ROOTFS should only be set by the kernel when mounting its
2554 flags &= ~MNT_ROOTFS;
2557 * check that we have an even number of iovec's
2558 * and that we have at least two options.
2560 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2563 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2566 error = vfs_donmount(td, flags, auio);
2574 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2576 struct yyy32 *p32, s32;
2577 struct yyy *p = NULL, s;
2582 error = copyin(uap->zzz, &s32, sizeof(s32));
2588 error = kern_xxx(td, p);
2593 error = copyout(&s32, p32, sizeof(s32));
2600 syscall32_register(int *offset, struct sysent *new_sysent,
2601 struct sysent *old_sysent)
2603 if (*offset == NO_SYSCALL) {
2606 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2607 if (freebsd32_sysent[i].sy_call ==
2608 (sy_call_t *)lkmnosys)
2610 if (i == SYS_MAXSYSCALL)
2613 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2615 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2616 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2619 *old_sysent = freebsd32_sysent[*offset];
2620 freebsd32_sysent[*offset] = *new_sysent;
2625 syscall32_deregister(int *offset, struct sysent *old_sysent)
2629 freebsd32_sysent[*offset] = *old_sysent;
2634 syscall32_module_handler(struct module *mod, int what, void *arg)
2636 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2642 error = syscall32_register(data->offset, data->new_sysent,
2645 /* Leave a mark so we know to safely unload below. */
2646 data->offset = NULL;
2649 ms.intval = *data->offset;
2651 module_setspecific(mod, &ms);
2654 error = data->chainevh(mod, what, data->chainarg);
2658 * MOD_LOAD failed, so just return without calling the
2659 * chained handler since we didn't pass along the MOD_LOAD
2662 if (data->offset == NULL)
2664 if (data->chainevh) {
2665 error = data->chainevh(mod, what, data->chainarg);
2669 error = syscall32_deregister(data->offset, &data->old_sysent);
2674 error = data->chainevh(mod, what, data->chainarg);
2680 syscall32_helper_register(struct syscall_helper_data *sd)
2682 struct syscall_helper_data *sd1;
2685 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2686 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2689 syscall32_helper_unregister(sd);
2692 sd1->registered = 1;
2698 syscall32_helper_unregister(struct syscall_helper_data *sd)
2700 struct syscall_helper_data *sd1;
2702 for (sd1 = sd; sd1->registered != 0; sd1++) {
2703 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2704 sd1->registered = 0;
2710 freebsd32_copyout_strings(struct image_params *imgp)
2714 char *stringp, *destp;
2715 u_int32_t *stack_base;
2716 struct freebsd32_ps_strings *arginfo;
2717 char canary[sizeof(long) * 8];
2718 int32_t pagesizes32[MAXPAGESIZES];
2719 size_t execpath_len;
2723 * Calculate string base and vector table pointers.
2724 * Also deal with signal trampoline code for this exec type.
2726 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2727 execpath_len = strlen(imgp->execpath) + 1;
2730 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2732 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2733 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2736 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2737 roundup(execpath_len, sizeof(char *)) -
2738 roundup(sizeof(canary), sizeof(char *)) -
2739 roundup(sizeof(pagesizes32), sizeof(char *)) -
2740 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2746 copyout(imgp->proc->p_sysent->sv_sigcode,
2747 ((caddr_t)arginfo - szsigcode), szsigcode);
2750 * Copy the image path for the rtld.
2752 if (execpath_len != 0) {
2753 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2754 copyout(imgp->execpath, (void *)imgp->execpathp,
2759 * Prepare the canary for SSP.
2761 arc4rand(canary, sizeof(canary), 0);
2762 imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2764 copyout(canary, (void *)imgp->canary, sizeof(canary));
2765 imgp->canarylen = sizeof(canary);
2768 * Prepare the pagesizes array.
2770 for (i = 0; i < MAXPAGESIZES; i++)
2771 pagesizes32[i] = (uint32_t)pagesizes[i];
2772 imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2773 roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2774 copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2775 imgp->pagesizeslen = sizeof(pagesizes32);
2778 * If we have a valid auxargs ptr, prepare some room
2781 if (imgp->auxargs) {
2783 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2784 * lower compatibility.
2786 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2789 * The '+ 2' is for the null pointers at the end of each of
2790 * the arg and env vector sets,and imgp->auxarg_size is room
2791 * for argument of Runtime loader.
2793 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2794 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2798 * The '+ 2' is for the null pointers at the end of each of
2799 * the arg and env vector sets
2801 vectp = (u_int32_t *)
2802 (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2805 * vectp also becomes our initial stack base
2809 stringp = imgp->args->begin_argv;
2810 argc = imgp->args->argc;
2811 envc = imgp->args->envc;
2813 * Copy out strings - arguments and environment.
2815 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2818 * Fill in "ps_strings" struct for ps, w, etc.
2820 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2821 suword32(&arginfo->ps_nargvstr, argc);
2824 * Fill in argument portion of vector table.
2826 for (; argc > 0; --argc) {
2827 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2828 while (*stringp++ != 0)
2833 /* a null vector table pointer separates the argp's from the envp's */
2834 suword32(vectp++, 0);
2836 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2837 suword32(&arginfo->ps_nenvstr, envc);
2840 * Fill in environment portion of vector table.
2842 for (; envc > 0; --envc) {
2843 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2844 while (*stringp++ != 0)
2849 /* end of vector table is a null pointer */
2852 return ((register_t *)stack_base);
2856 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2858 struct kld_file_stat stat;
2859 struct kld32_file_stat stat32;
2862 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2865 if (version != sizeof(struct kld32_file_stat_1) &&
2866 version != sizeof(struct kld32_file_stat))
2869 error = kern_kldstat(td, uap->fileid, &stat);
2873 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2874 CP(stat, stat32, refs);
2875 CP(stat, stat32, id);
2876 PTROUT_CP(stat, stat32, address);
2877 CP(stat, stat32, size);
2878 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2879 return (copyout(&stat32, uap->stat, version));
2883 freebsd32_posix_fallocate(struct thread *td,
2884 struct freebsd32_posix_fallocate_args *uap)
2887 return (kern_posix_fallocate(td, uap->fd,
2888 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2892 freebsd32_posix_fadvise(struct thread *td,
2893 struct freebsd32_posix_fadvise_args *uap)
2896 return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2897 PAIR32TO64(off_t, uap->len), uap->advice));