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 CTASSERT(sizeof(struct timeval32) == 8);
108 CTASSERT(sizeof(struct timespec32) == 8);
109 CTASSERT(sizeof(struct itimerval32) == 16);
110 CTASSERT(sizeof(struct statfs32) == 256);
111 CTASSERT(sizeof(struct rusage32) == 72);
112 CTASSERT(sizeof(struct sigaltstack32) == 12);
113 CTASSERT(sizeof(struct kevent32) == 20);
114 CTASSERT(sizeof(struct iovec32) == 8);
115 CTASSERT(sizeof(struct msghdr32) == 28);
116 CTASSERT(sizeof(struct stat32) == 96);
117 CTASSERT(sizeof(struct sigaction32) == 24);
119 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
120 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
122 #if BYTE_ORDER == BIG_ENDIAN
123 #define PAIR32TO64(type, name) ((name ## 2) | ((type)(name ## 1) << 32))
127 #define PAIR32TO64(type, name) ((name ## 1) | ((type)(name ## 2) << 32))
133 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
136 TV_CP(*s, *s32, ru_utime);
137 TV_CP(*s, *s32, ru_stime);
138 CP(*s, *s32, ru_maxrss);
139 CP(*s, *s32, ru_ixrss);
140 CP(*s, *s32, ru_idrss);
141 CP(*s, *s32, ru_isrss);
142 CP(*s, *s32, ru_minflt);
143 CP(*s, *s32, ru_majflt);
144 CP(*s, *s32, ru_nswap);
145 CP(*s, *s32, ru_inblock);
146 CP(*s, *s32, ru_oublock);
147 CP(*s, *s32, ru_msgsnd);
148 CP(*s, *s32, ru_msgrcv);
149 CP(*s, *s32, ru_nsignals);
150 CP(*s, *s32, ru_nvcsw);
151 CP(*s, *s32, ru_nivcsw);
155 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
158 struct rusage32 ru32;
159 struct rusage ru, *rup;
161 if (uap->rusage != NULL)
165 error = kern_wait(td, uap->pid, &status, uap->options, rup);
168 if (uap->status != NULL)
169 error = copyout(&status, uap->status, sizeof(status));
170 if (uap->rusage != NULL && error == 0) {
171 freebsd32_rusage_out(&ru, &ru32);
172 error = copyout(&ru32, uap->rusage, sizeof(ru32));
178 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
180 struct wrusage32 wru32;
181 struct __wrusage wru, *wrup;
182 struct siginfo32 si32;
183 struct __siginfo si, *sip;
186 if (uap->wrusage != NULL)
190 if (uap->info != NULL) {
192 bzero(sip, sizeof(*sip));
195 error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
196 &status, uap->options, wrup, sip);
199 if (uap->status != NULL)
200 error = copyout(&status, uap->status, sizeof(status));
201 if (uap->wrusage != NULL && error == 0) {
202 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
203 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
204 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
206 if (uap->info != NULL && error == 0) {
207 siginfo_to_siginfo32 (&si, &si32);
208 error = copyout(&si32, uap->info, sizeof(si32));
213 #ifdef COMPAT_FREEBSD4
215 copy_statfs(struct statfs *in, struct statfs32 *out)
218 statfs_scale_blocks(in, INT32_MAX);
219 bzero(out, sizeof(*out));
220 CP(*in, *out, f_bsize);
221 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
222 CP(*in, *out, f_blocks);
223 CP(*in, *out, f_bfree);
224 CP(*in, *out, f_bavail);
225 out->f_files = MIN(in->f_files, INT32_MAX);
226 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
227 CP(*in, *out, f_fsid);
228 CP(*in, *out, f_owner);
229 CP(*in, *out, f_type);
230 CP(*in, *out, f_flags);
231 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
232 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
233 strlcpy(out->f_fstypename,
234 in->f_fstypename, MFSNAMELEN);
235 strlcpy(out->f_mntonname,
236 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
237 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
238 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
239 strlcpy(out->f_mntfromname,
240 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
244 #ifdef COMPAT_FREEBSD4
246 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
248 struct statfs *buf, *sp;
249 struct statfs32 stat32;
253 count = uap->bufsize / sizeof(struct statfs32);
254 size = count * sizeof(struct statfs);
255 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
257 count = td->td_retval[0];
259 while (count > 0 && error == 0) {
260 copy_statfs(sp, &stat32);
261 error = copyout(&stat32, uap->buf, sizeof(stat32));
273 freebsd32_sigaltstack(struct thread *td,
274 struct freebsd32_sigaltstack_args *uap)
276 struct sigaltstack32 s32;
277 struct sigaltstack ss, oss, *ssp;
280 if (uap->ss != NULL) {
281 error = copyin(uap->ss, &s32, sizeof(s32));
284 PTRIN_CP(s32, ss, ss_sp);
285 CP(s32, ss, ss_size);
286 CP(s32, ss, ss_flags);
290 error = kern_sigaltstack(td, ssp, &oss);
291 if (error == 0 && uap->oss != NULL) {
292 PTROUT_CP(oss, s32, ss_sp);
293 CP(oss, s32, ss_size);
294 CP(oss, s32, ss_flags);
295 error = copyout(&s32, uap->oss, sizeof(s32));
301 * Custom version of exec_copyin_args() so that we can translate
305 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
306 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
313 bzero(args, sizeof(*args));
318 * Allocate demand-paged memory for the file name, argument, and
319 * environment strings.
321 error = exec_alloc_args(args);
326 * Copy the file name.
329 args->fname = args->buf;
330 error = (segflg == UIO_SYSSPACE) ?
331 copystr(fname, args->fname, PATH_MAX, &length) :
332 copyinstr(fname, args->fname, PATH_MAX, &length);
338 args->begin_argv = args->buf + length;
339 args->endp = args->begin_argv;
340 args->stringspace = ARG_MAX;
343 * extract arguments first
347 error = copyin(p32++, &arg, sizeof(arg));
353 error = copyinstr(argp, args->endp, args->stringspace, &length);
355 if (error == ENAMETOOLONG)
359 args->stringspace -= length;
360 args->endp += length;
364 args->begin_envv = args->endp;
367 * extract environment strings
372 error = copyin(p32++, &arg, sizeof(arg));
378 error = copyinstr(envp, args->endp, args->stringspace,
381 if (error == ENAMETOOLONG)
385 args->stringspace -= length;
386 args->endp += length;
394 exec_free_args(args);
399 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
401 struct image_args eargs;
404 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
405 uap->argv, uap->envv);
407 error = kern_execve(td, &eargs, NULL);
412 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
414 struct image_args eargs;
417 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
418 uap->argv, uap->envv);
421 error = kern_execve(td, &eargs, NULL);
428 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
429 int prot, int fd, off_t pos)
432 vm_map_entry_t entry;
435 map = &td->td_proc->p_vmspace->vm_map;
437 prot |= VM_PROT_WRITE;
439 if (vm_map_lookup_entry(map, start, &entry)) {
440 if ((entry->protection & prot) != prot) {
441 rv = vm_map_protect(map,
444 entry->protection | prot,
446 if (rv != KERN_SUCCESS)
450 vm_offset_t addr = trunc_page(start);
451 rv = vm_map_find(map, 0, 0,
452 &addr, PAGE_SIZE, FALSE, prot,
454 if (rv != KERN_SUCCESS)
461 r.buf = (void *) start;
462 r.nbyte = end - start;
464 return (sys_pread(td, &r));
466 while (start < end) {
467 subyte((void *) start, 0);
476 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
478 struct mprotect_args ap;
480 ap.addr = PTRIN(uap->addr);
483 #if defined(__amd64__) || defined(__ia64__)
484 if (i386_read_exec && (ap.prot & PROT_READ) != 0)
485 ap.prot |= PROT_EXEC;
487 return (sys_mprotect(td, &ap));
491 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
494 vm_offset_t addr = (vm_offset_t) uap->addr;
495 vm_size_t len = uap->len;
496 int prot = uap->prot;
497 int flags = uap->flags;
499 off_t pos = PAIR32TO64(off_t,uap->pos);
505 * Attempt to handle page size hassles.
507 pageoff = (pos & PAGE_MASK);
508 if (flags & MAP_FIXED) {
509 vm_offset_t start, end;
513 if (start != trunc_page(start)) {
514 error = freebsd32_mmap_partial(td, start,
515 round_page(start), prot,
518 pos += round_page(start) - start;
519 start = round_page(start);
521 if (end != round_page(end)) {
522 vm_offset_t t = trunc_page(end);
523 error = freebsd32_mmap_partial(td, t, end,
526 end = trunc_page(end);
528 if (end > start && fd != -1 && (pos & PAGE_MASK)) {
530 * We can't map this region at all. The specified
531 * address doesn't have the same alignment as the file
532 * position. Fake the mapping by simply reading the
533 * entire region into memory. First we need to make
534 * sure the region exists.
540 prot |= VM_PROT_WRITE;
541 map = &td->td_proc->p_vmspace->vm_map;
542 rv = vm_map_remove(map, start, end);
543 if (rv != KERN_SUCCESS)
545 rv = vm_map_find(map, 0, 0,
546 &start, end - start, FALSE,
547 prot, VM_PROT_ALL, 0);
548 if (rv != KERN_SUCCESS)
551 r.buf = (void *) start;
552 r.nbyte = end - start;
554 error = sys_pread(td, &r);
558 td->td_retval[0] = addr;
563 * After dealing with the ragged ends, there
564 * might be none left.
566 td->td_retval[0] = addr;
574 #if defined(__amd64__) || defined(__ia64__)
575 if (i386_read_exec && (prot & PROT_READ))
579 ap.addr = (void *) addr;
586 return (sys_mmap(td, &ap));
589 #ifdef COMPAT_FREEBSD6
591 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
593 struct freebsd32_mmap_args ap;
598 ap.flags = uap->flags;
603 return (freebsd32_mmap(td, &ap));
608 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
610 struct itimerval itv, oitv, *itvp;
611 struct itimerval32 i32;
614 if (uap->itv != NULL) {
615 error = copyin(uap->itv, &i32, sizeof(i32));
618 TV_CP(i32, itv, it_interval);
619 TV_CP(i32, itv, it_value);
623 error = kern_setitimer(td, uap->which, itvp, &oitv);
624 if (error || uap->oitv == NULL)
626 TV_CP(oitv, i32, it_interval);
627 TV_CP(oitv, i32, it_value);
628 return (copyout(&i32, uap->oitv, sizeof(i32)));
632 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
634 struct itimerval itv;
635 struct itimerval32 i32;
638 error = kern_getitimer(td, uap->which, &itv);
639 if (error || uap->itv == NULL)
641 TV_CP(itv, i32, it_interval);
642 TV_CP(itv, i32, it_value);
643 return (copyout(&i32, uap->itv, sizeof(i32)));
647 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
649 struct timeval32 tv32;
650 struct timeval tv, *tvp;
653 if (uap->tv != NULL) {
654 error = copyin(uap->tv, &tv32, sizeof(tv32));
657 CP(tv32, tv, tv_sec);
658 CP(tv32, tv, tv_usec);
663 * XXX Do pointers need PTRIN()?
665 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
666 sizeof(int32_t) * 8));
670 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
672 struct timespec32 ts32;
674 struct timeval tv, *tvp;
678 if (uap->ts != NULL) {
679 error = copyin(uap->ts, &ts32, sizeof(ts32));
682 CP(ts32, ts, tv_sec);
683 CP(ts32, ts, tv_nsec);
684 TIMESPEC_TO_TIMEVAL(&tv, &ts);
688 if (uap->sm != NULL) {
689 error = copyin(uap->sm, &set, sizeof(set));
696 * XXX Do pointers need PTRIN()?
698 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
699 uset, sizeof(int32_t) * 8);
704 * Copy 'count' items into the destination list pointed to by uap->eventlist.
707 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
709 struct freebsd32_kevent_args *uap;
710 struct kevent32 ks32[KQ_NEVENTS];
713 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
714 uap = (struct freebsd32_kevent_args *)arg;
716 for (i = 0; i < count; i++) {
717 CP(kevp[i], ks32[i], ident);
718 CP(kevp[i], ks32[i], filter);
719 CP(kevp[i], ks32[i], flags);
720 CP(kevp[i], ks32[i], fflags);
721 CP(kevp[i], ks32[i], data);
722 PTROUT_CP(kevp[i], ks32[i], udata);
724 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
726 uap->eventlist += count;
731 * Copy 'count' items from the list pointed to by uap->changelist.
734 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
736 struct freebsd32_kevent_args *uap;
737 struct kevent32 ks32[KQ_NEVENTS];
740 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
741 uap = (struct freebsd32_kevent_args *)arg;
743 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
746 uap->changelist += count;
748 for (i = 0; i < count; i++) {
749 CP(ks32[i], kevp[i], ident);
750 CP(ks32[i], kevp[i], filter);
751 CP(ks32[i], kevp[i], flags);
752 CP(ks32[i], kevp[i], fflags);
753 CP(ks32[i], kevp[i], data);
754 PTRIN_CP(ks32[i], kevp[i], udata);
761 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
763 struct timespec32 ts32;
764 struct timespec ts, *tsp;
765 struct kevent_copyops k_ops = { uap,
766 freebsd32_kevent_copyout,
767 freebsd32_kevent_copyin};
772 error = copyin(uap->timeout, &ts32, sizeof(ts32));
775 CP(ts32, ts, tv_sec);
776 CP(ts32, ts, tv_nsec);
780 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
786 freebsd32_gettimeofday(struct thread *td,
787 struct freebsd32_gettimeofday_args *uap)
790 struct timeval32 atv32;
796 CP(atv, atv32, tv_sec);
797 CP(atv, atv32, tv_usec);
798 error = copyout(&atv32, uap->tp, sizeof (atv32));
800 if (error == 0 && uap->tzp != NULL) {
801 rtz.tz_minuteswest = tz_minuteswest;
802 rtz.tz_dsttime = tz_dsttime;
803 error = copyout(&rtz, uap->tzp, sizeof (rtz));
809 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
815 error = kern_getrusage(td, uap->who, &s);
818 if (uap->rusage != NULL) {
819 freebsd32_rusage_out(&s, &s32);
820 error = copyout(&s32, uap->rusage, sizeof(s32));
826 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
828 struct iovec32 iov32;
835 if (iovcnt > UIO_MAXIOV)
837 iovlen = iovcnt * sizeof(struct iovec);
838 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
839 iov = (struct iovec *)(uio + 1);
840 for (i = 0; i < iovcnt; i++) {
841 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
846 iov[i].iov_base = PTRIN(iov32.iov_base);
847 iov[i].iov_len = iov32.iov_len;
850 uio->uio_iovcnt = iovcnt;
851 uio->uio_segflg = UIO_USERSPACE;
852 uio->uio_offset = -1;
854 for (i = 0; i < iovcnt; i++) {
855 if (iov->iov_len > INT_MAX - uio->uio_resid) {
859 uio->uio_resid += iov->iov_len;
867 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
872 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
875 error = kern_readv(td, uap->fd, auio);
881 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
886 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
889 error = kern_writev(td, uap->fd, auio);
895 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
900 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
903 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
909 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
914 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
917 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
923 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
926 struct iovec32 iov32;
932 if (iovcnt > UIO_MAXIOV)
934 iovlen = iovcnt * sizeof(struct iovec);
935 iov = malloc(iovlen, M_IOV, M_WAITOK);
936 for (i = 0; i < iovcnt; i++) {
937 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
942 iov[i].iov_base = PTRIN(iov32.iov_base);
943 iov[i].iov_len = iov32.iov_len;
950 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
955 error = copyin(msg32, &m32, sizeof(m32));
958 msg->msg_name = PTRIN(m32.msg_name);
959 msg->msg_namelen = m32.msg_namelen;
960 msg->msg_iov = PTRIN(m32.msg_iov);
961 msg->msg_iovlen = m32.msg_iovlen;
962 msg->msg_control = PTRIN(m32.msg_control);
963 msg->msg_controllen = m32.msg_controllen;
964 msg->msg_flags = m32.msg_flags;
969 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
974 m32.msg_name = PTROUT(msg->msg_name);
975 m32.msg_namelen = msg->msg_namelen;
976 m32.msg_iov = PTROUT(msg->msg_iov);
977 m32.msg_iovlen = msg->msg_iovlen;
978 m32.msg_control = PTROUT(msg->msg_control);
979 m32.msg_controllen = msg->msg_controllen;
980 m32.msg_flags = msg->msg_flags;
981 error = copyout(&m32, msg32, sizeof(m32));
985 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
986 #define FREEBSD32_ALIGN(p) \
987 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
988 #define FREEBSD32_CMSG_SPACE(l) \
989 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
991 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
992 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
994 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
998 socklen_t clen, datalen;
1001 int len, maxlen, copylen;
1005 len = msg->msg_controllen;
1006 maxlen = msg->msg_controllen;
1007 msg->msg_controllen = 0;
1010 ctlbuf = msg->msg_control;
1012 while (m && len > 0) {
1013 cm = mtod(m, struct cmsghdr *);
1016 while (cm != NULL) {
1018 if (sizeof(struct cmsghdr) > clen ||
1019 cm->cmsg_len > clen) {
1024 data = CMSG_DATA(cm);
1025 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1027 /* Adjust message length */
1028 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1033 copylen = sizeof(struct cmsghdr);
1034 if (len < copylen) {
1035 msg->msg_flags |= MSG_CTRUNC;
1039 error = copyout(cm,ctlbuf,copylen);
1043 ctlbuf += FREEBSD32_ALIGN(copylen);
1044 len -= FREEBSD32_ALIGN(copylen);
1051 if (len < copylen) {
1052 msg->msg_flags |= MSG_CTRUNC;
1056 error = copyout(data,ctlbuf,copylen);
1060 ctlbuf += FREEBSD32_ALIGN(copylen);
1061 len -= FREEBSD32_ALIGN(copylen);
1063 if (CMSG_SPACE(datalen) < clen) {
1064 clen -= CMSG_SPACE(datalen);
1065 cm = (struct cmsghdr *)
1066 ((caddr_t)cm + CMSG_SPACE(datalen));
1075 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1083 freebsd32_recvmsg(td, uap)
1085 struct freebsd32_recvmsg_args /* {
1087 struct msghdr32 *msg;
1092 struct msghdr32 m32;
1093 struct iovec *uiov, *iov;
1094 struct mbuf *control = NULL;
1095 struct mbuf **controlp;
1098 error = copyin(uap->msg, &m32, sizeof(m32));
1101 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1104 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1108 msg.msg_flags = uap->flags;
1112 controlp = (msg.msg_control != NULL) ? &control : NULL;
1113 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1117 if (control != NULL)
1118 error = freebsd32_copy_msg_out(&msg, control);
1120 msg.msg_controllen = 0;
1123 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1127 if (control != NULL)
1135 freebsd32_convert_msg_in(struct mbuf **controlp)
1137 struct mbuf *control = *controlp;
1138 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1140 socklen_t clen = control->m_len, datalen;
1146 while (cm != NULL) {
1147 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1152 data = FREEBSD32_CMSG_DATA(cm);
1153 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1155 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1157 controlp = &(*controlp)->m_next;
1159 if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1160 clen -= FREEBSD32_CMSG_SPACE(datalen);
1161 cm = (struct cmsghdr *)
1162 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1175 freebsd32_sendmsg(struct thread *td,
1176 struct freebsd32_sendmsg_args *uap)
1179 struct msghdr32 m32;
1181 struct mbuf *control = NULL;
1182 struct sockaddr *to = NULL;
1185 error = copyin(uap->msg, &m32, sizeof(m32));
1188 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1191 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1196 if (msg.msg_name != NULL) {
1197 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1205 if (msg.msg_control) {
1206 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1211 error = sockargs(&control, msg.msg_control,
1212 msg.msg_controllen, MT_CONTROL);
1216 error = freebsd32_convert_msg_in(&control);
1221 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1232 freebsd32_recvfrom(struct thread *td,
1233 struct freebsd32_recvfrom_args *uap)
1239 if (uap->fromlenaddr) {
1240 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1241 sizeof(msg.msg_namelen));
1245 msg.msg_namelen = 0;
1248 msg.msg_name = PTRIN(uap->from);
1249 msg.msg_iov = &aiov;
1251 aiov.iov_base = PTRIN(uap->buf);
1252 aiov.iov_len = uap->len;
1253 msg.msg_control = NULL;
1254 msg.msg_flags = uap->flags;
1255 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1256 if (error == 0 && uap->fromlenaddr)
1257 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1258 sizeof (msg.msg_namelen));
1263 freebsd32_settimeofday(struct thread *td,
1264 struct freebsd32_settimeofday_args *uap)
1266 struct timeval32 tv32;
1267 struct timeval tv, *tvp;
1268 struct timezone tz, *tzp;
1272 error = copyin(uap->tv, &tv32, sizeof(tv32));
1275 CP(tv32, tv, tv_sec);
1276 CP(tv32, tv, tv_usec);
1281 error = copyin(uap->tzp, &tz, sizeof(tz));
1287 return (kern_settimeofday(td, tvp, tzp));
1291 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1293 struct timeval32 s32[2];
1294 struct timeval s[2], *sp;
1297 if (uap->tptr != NULL) {
1298 error = copyin(uap->tptr, s32, sizeof(s32));
1301 CP(s32[0], s[0], tv_sec);
1302 CP(s32[0], s[0], tv_usec);
1303 CP(s32[1], s[1], tv_sec);
1304 CP(s32[1], s[1], tv_usec);
1308 return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1312 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1314 struct timeval32 s32[2];
1315 struct timeval s[2], *sp;
1318 if (uap->tptr != NULL) {
1319 error = copyin(uap->tptr, s32, sizeof(s32));
1322 CP(s32[0], s[0], tv_sec);
1323 CP(s32[0], s[0], tv_usec);
1324 CP(s32[1], s[1], tv_sec);
1325 CP(s32[1], s[1], tv_usec);
1329 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1333 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1335 struct timeval32 s32[2];
1336 struct timeval s[2], *sp;
1339 if (uap->tptr != NULL) {
1340 error = copyin(uap->tptr, s32, sizeof(s32));
1343 CP(s32[0], s[0], tv_sec);
1344 CP(s32[0], s[0], tv_usec);
1345 CP(s32[1], s[1], tv_sec);
1346 CP(s32[1], s[1], tv_usec);
1350 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1354 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1356 struct timeval32 s32[2];
1357 struct timeval s[2], *sp;
1360 if (uap->times != NULL) {
1361 error = copyin(uap->times, s32, sizeof(s32));
1364 CP(s32[0], s[0], tv_sec);
1365 CP(s32[0], s[0], tv_usec);
1366 CP(s32[1], s[1], tv_sec);
1367 CP(s32[1], s[1], tv_usec);
1371 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1376 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1378 struct timeval32 tv32;
1379 struct timeval delta, olddelta, *deltap;
1383 error = copyin(uap->delta, &tv32, sizeof(tv32));
1386 CP(tv32, delta, tv_sec);
1387 CP(tv32, delta, tv_usec);
1391 error = kern_adjtime(td, deltap, &olddelta);
1392 if (uap->olddelta && error == 0) {
1393 CP(olddelta, tv32, tv_sec);
1394 CP(olddelta, tv32, tv_usec);
1395 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1400 #ifdef COMPAT_FREEBSD4
1402 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1404 struct statfs32 s32;
1408 error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1411 copy_statfs(&s, &s32);
1412 return (copyout(&s32, uap->buf, sizeof(s32)));
1416 #ifdef COMPAT_FREEBSD4
1418 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1420 struct statfs32 s32;
1424 error = kern_fstatfs(td, uap->fd, &s);
1427 copy_statfs(&s, &s32);
1428 return (copyout(&s32, uap->buf, sizeof(s32)));
1432 #ifdef COMPAT_FREEBSD4
1434 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1436 struct statfs32 s32;
1441 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1443 error = kern_fhstatfs(td, fh, &s);
1446 copy_statfs(&s, &s32);
1447 return (copyout(&s32, uap->buf, sizeof(s32)));
1452 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1454 struct pread_args ap;
1458 ap.nbyte = uap->nbyte;
1459 ap.offset = PAIR32TO64(off_t,uap->offset);
1460 return (sys_pread(td, &ap));
1464 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1466 struct pwrite_args ap;
1470 ap.nbyte = uap->nbyte;
1471 ap.offset = PAIR32TO64(off_t,uap->offset);
1472 return (sys_pwrite(td, &ap));
1477 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1479 struct lseek_args nuap;
1482 nuap.offset = uap->offset;
1483 nuap.whence = uap->whence;
1484 return (sys_lseek(td, &nuap));
1489 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1492 struct lseek_args ap;
1496 ap.offset = PAIR32TO64(off_t,uap->offset);
1497 ap.whence = uap->whence;
1498 error = sys_lseek(td, &ap);
1499 /* Expand the quad return into two parts for eax and edx */
1500 pos = *(off_t *)(td->td_retval);
1501 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1502 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1507 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1509 struct truncate_args ap;
1511 ap.path = uap->path;
1512 ap.length = PAIR32TO64(off_t,uap->length);
1513 return (sys_truncate(td, &ap));
1517 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1519 struct ftruncate_args ap;
1522 ap.length = PAIR32TO64(off_t,uap->length);
1523 return (sys_ftruncate(td, &ap));
1528 ofreebsd32_getdirentries(struct thread *td,
1529 struct ofreebsd32_getdirentries_args *uap)
1531 struct ogetdirentries_args ap;
1538 ap.count = uap->count;
1540 error = kern_ogetdirentries(td, &ap, &loff);
1543 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1550 freebsd32_getdirentries(struct thread *td,
1551 struct freebsd32_getdirentries_args *uap)
1557 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
1560 if (uap->basep != NULL) {
1562 error = copyout(&base32, uap->basep, sizeof(int32_t));
1567 #ifdef COMPAT_FREEBSD6
1568 /* versions with the 'int pad' argument */
1570 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1572 struct pread_args ap;
1576 ap.nbyte = uap->nbyte;
1577 ap.offset = PAIR32TO64(off_t,uap->offset);
1578 return (sys_pread(td, &ap));
1582 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1584 struct pwrite_args ap;
1588 ap.nbyte = uap->nbyte;
1589 ap.offset = PAIR32TO64(off_t,uap->offset);
1590 return (sys_pwrite(td, &ap));
1594 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1597 struct lseek_args ap;
1601 ap.offset = PAIR32TO64(off_t,uap->offset);
1602 ap.whence = uap->whence;
1603 error = sys_lseek(td, &ap);
1604 /* Expand the quad return into two parts for eax and edx */
1605 pos = *(off_t *)(td->td_retval);
1606 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1607 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1612 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1614 struct truncate_args ap;
1616 ap.path = uap->path;
1617 ap.length = PAIR32TO64(off_t,uap->length);
1618 return (sys_truncate(td, &ap));
1622 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1624 struct ftruncate_args ap;
1627 ap.length = PAIR32TO64(off_t,uap->length);
1628 return (sys_ftruncate(td, &ap));
1630 #endif /* COMPAT_FREEBSD6 */
1640 freebsd32_do_sendfile(struct thread *td,
1641 struct freebsd32_sendfile_args *uap, int compat)
1643 struct sendfile_args ap;
1644 struct sf_hdtr32 hdtr32;
1645 struct sf_hdtr hdtr;
1646 struct uio *hdr_uio, *trl_uio;
1647 struct iovec32 *iov32;
1650 hdr_uio = trl_uio = NULL;
1654 ap.offset = PAIR32TO64(off_t,uap->offset);
1655 ap.nbytes = uap->nbytes;
1656 ap.hdtr = (struct sf_hdtr *)uap->hdtr; /* XXX not used */
1657 ap.sbytes = uap->sbytes;
1658 ap.flags = uap->flags;
1660 if (uap->hdtr != NULL) {
1661 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1664 PTRIN_CP(hdtr32, hdtr, headers);
1665 CP(hdtr32, hdtr, hdr_cnt);
1666 PTRIN_CP(hdtr32, hdtr, trailers);
1667 CP(hdtr32, hdtr, trl_cnt);
1669 if (hdtr.headers != NULL) {
1670 iov32 = PTRIN(hdtr32.headers);
1671 error = freebsd32_copyinuio(iov32,
1672 hdtr32.hdr_cnt, &hdr_uio);
1676 if (hdtr.trailers != NULL) {
1677 iov32 = PTRIN(hdtr32.trailers);
1678 error = freebsd32_copyinuio(iov32,
1679 hdtr32.trl_cnt, &trl_uio);
1685 error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1688 free(hdr_uio, M_IOV);
1690 free(trl_uio, M_IOV);
1694 #ifdef COMPAT_FREEBSD4
1696 freebsd4_freebsd32_sendfile(struct thread *td,
1697 struct freebsd4_freebsd32_sendfile_args *uap)
1699 return (freebsd32_do_sendfile(td,
1700 (struct freebsd32_sendfile_args *)uap, 1));
1705 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1708 return (freebsd32_do_sendfile(td, uap, 0));
1712 copy_stat(struct stat *in, struct stat32 *out)
1715 CP(*in, *out, st_dev);
1716 CP(*in, *out, st_ino);
1717 CP(*in, *out, st_mode);
1718 CP(*in, *out, st_nlink);
1719 CP(*in, *out, st_uid);
1720 CP(*in, *out, st_gid);
1721 CP(*in, *out, st_rdev);
1722 TS_CP(*in, *out, st_atim);
1723 TS_CP(*in, *out, st_mtim);
1724 TS_CP(*in, *out, st_ctim);
1725 CP(*in, *out, st_size);
1726 CP(*in, *out, st_blocks);
1727 CP(*in, *out, st_blksize);
1728 CP(*in, *out, st_flags);
1729 CP(*in, *out, st_gen);
1730 TS_CP(*in, *out, st_birthtim);
1735 copy_ostat(struct stat *in, struct ostat32 *out)
1738 CP(*in, *out, st_dev);
1739 CP(*in, *out, st_ino);
1740 CP(*in, *out, st_mode);
1741 CP(*in, *out, st_nlink);
1742 CP(*in, *out, st_uid);
1743 CP(*in, *out, st_gid);
1744 CP(*in, *out, st_rdev);
1745 CP(*in, *out, st_size);
1746 TS_CP(*in, *out, st_atim);
1747 TS_CP(*in, *out, st_mtim);
1748 TS_CP(*in, *out, st_ctim);
1749 CP(*in, *out, st_blksize);
1750 CP(*in, *out, st_blocks);
1751 CP(*in, *out, st_flags);
1752 CP(*in, *out, st_gen);
1757 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1763 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1766 copy_stat(&sb, &sb32);
1767 error = copyout(&sb32, uap->ub, sizeof (sb32));
1773 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1776 struct ostat32 sb32;
1779 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1782 copy_ostat(&sb, &sb32);
1783 error = copyout(&sb32, uap->ub, sizeof (sb32));
1789 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1795 error = kern_fstat(td, uap->fd, &ub);
1798 copy_stat(&ub, &ub32);
1799 error = copyout(&ub32, uap->ub, sizeof(ub32));
1805 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1808 struct ostat32 ub32;
1811 error = kern_fstat(td, uap->fd, &ub);
1814 copy_ostat(&ub, &ub32);
1815 error = copyout(&ub32, uap->ub, sizeof(ub32));
1821 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1827 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1830 copy_stat(&ub, &ub32);
1831 error = copyout(&ub32, uap->buf, sizeof(ub32));
1836 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1842 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1845 copy_stat(&sb, &sb32);
1846 error = copyout(&sb32, uap->ub, sizeof (sb32));
1852 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1855 struct ostat32 sb32;
1858 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1861 copy_ostat(&sb, &sb32);
1862 error = copyout(&sb32, uap->ub, sizeof (sb32));
1868 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1870 int error, name[CTL_MAXNAME];
1873 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1875 error = copyin(uap->name, name, uap->namelen * sizeof(int));
1879 oldlen = fuword32(uap->oldlenp);
1882 error = userland_sysctl(td, name, uap->namelen,
1883 uap->old, &oldlen, 1,
1884 uap->new, uap->newlen, &j, SCTL_MASK32);
1885 if (error && error != ENOMEM)
1888 suword32(uap->oldlenp, j);
1893 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1899 error = copyin(uap->jail, &version, sizeof(uint32_t));
1906 /* FreeBSD single IPv4 jails. */
1907 struct jail32_v0 j32_v0;
1909 bzero(&j, sizeof(struct jail));
1910 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1913 CP(j32_v0, j, version);
1914 PTRIN_CP(j32_v0, j, path);
1915 PTRIN_CP(j32_v0, j, hostname);
1916 j.ip4s = j32_v0.ip_number;
1922 * Version 1 was used by multi-IPv4 jail implementations
1923 * that never made it into the official kernel.
1927 case 2: /* JAIL_API_VERSION */
1929 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1932 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1935 CP(j32, j, version);
1936 PTRIN_CP(j32, j, path);
1937 PTRIN_CP(j32, j, hostname);
1938 PTRIN_CP(j32, j, jailname);
1941 PTRIN_CP(j32, j, ip4);
1942 PTRIN_CP(j32, j, ip6);
1947 /* Sci-Fi jails are not supported, sorry. */
1950 return (kern_jail(td, &j));
1954 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1959 /* Check that we have an even number of iovecs. */
1960 if (uap->iovcnt & 1)
1963 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1966 error = kern_jail_set(td, auio, uap->flags);
1972 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1974 struct iovec32 iov32;
1978 /* Check that we have an even number of iovecs. */
1979 if (uap->iovcnt & 1)
1982 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1985 error = kern_jail_get(td, auio, uap->flags);
1987 for (i = 0; i < uap->iovcnt; i++) {
1988 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1989 CP(auio->uio_iov[i], iov32, iov_len);
1990 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1999 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2001 struct sigaction32 s32;
2002 struct sigaction sa, osa, *sap;
2006 error = copyin(uap->act, &s32, sizeof(s32));
2009 sa.sa_handler = PTRIN(s32.sa_u);
2010 CP(s32, sa, sa_flags);
2011 CP(s32, sa, sa_mask);
2015 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2016 if (error == 0 && uap->oact != NULL) {
2017 s32.sa_u = PTROUT(osa.sa_handler);
2018 CP(osa, s32, sa_flags);
2019 CP(osa, s32, sa_mask);
2020 error = copyout(&s32, uap->oact, sizeof(s32));
2025 #ifdef COMPAT_FREEBSD4
2027 freebsd4_freebsd32_sigaction(struct thread *td,
2028 struct freebsd4_freebsd32_sigaction_args *uap)
2030 struct sigaction32 s32;
2031 struct sigaction sa, osa, *sap;
2035 error = copyin(uap->act, &s32, sizeof(s32));
2038 sa.sa_handler = PTRIN(s32.sa_u);
2039 CP(s32, sa, sa_flags);
2040 CP(s32, sa, sa_mask);
2044 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2045 if (error == 0 && uap->oact != NULL) {
2046 s32.sa_u = PTROUT(osa.sa_handler);
2047 CP(osa, s32, sa_flags);
2048 CP(osa, s32, sa_mask);
2049 error = copyout(&s32, uap->oact, sizeof(s32));
2056 struct osigaction32 {
2065 ofreebsd32_sigaction(struct thread *td,
2066 struct ofreebsd32_sigaction_args *uap)
2068 struct osigaction32 s32;
2069 struct sigaction sa, osa, *sap;
2072 if (uap->signum <= 0 || uap->signum >= ONSIG)
2076 error = copyin(uap->nsa, &s32, sizeof(s32));
2079 sa.sa_handler = PTRIN(s32.sa_u);
2080 CP(s32, sa, sa_flags);
2081 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2085 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2086 if (error == 0 && uap->osa != NULL) {
2087 s32.sa_u = PTROUT(osa.sa_handler);
2088 CP(osa, s32, sa_flags);
2089 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2090 error = copyout(&s32, uap->osa, sizeof(s32));
2096 ofreebsd32_sigprocmask(struct thread *td,
2097 struct ofreebsd32_sigprocmask_args *uap)
2102 OSIG2SIG(uap->mask, set);
2103 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2104 SIG2OSIG(oset, td->td_retval[0]);
2109 ofreebsd32_sigpending(struct thread *td,
2110 struct ofreebsd32_sigpending_args *uap)
2112 struct proc *p = td->td_proc;
2116 siglist = p->p_siglist;
2117 SIGSETOR(siglist, td->td_siglist);
2119 SIG2OSIG(siglist, td->td_retval[0]);
2124 u_int32_t sv_handler;
2130 ofreebsd32_sigvec(struct thread *td,
2131 struct ofreebsd32_sigvec_args *uap)
2133 struct sigvec32 vec;
2134 struct sigaction sa, osa, *sap;
2137 if (uap->signum <= 0 || uap->signum >= ONSIG)
2141 error = copyin(uap->nsv, &vec, sizeof(vec));
2144 sa.sa_handler = PTRIN(vec.sv_handler);
2145 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2146 sa.sa_flags = vec.sv_flags;
2147 sa.sa_flags ^= SA_RESTART;
2151 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2152 if (error == 0 && uap->osv != NULL) {
2153 vec.sv_handler = PTROUT(osa.sa_handler);
2154 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2155 vec.sv_flags = osa.sa_flags;
2156 vec.sv_flags &= ~SA_NOCLDWAIT;
2157 vec.sv_flags ^= SA_RESTART;
2158 error = copyout(&vec, uap->osv, sizeof(vec));
2164 ofreebsd32_sigblock(struct thread *td,
2165 struct ofreebsd32_sigblock_args *uap)
2169 OSIG2SIG(uap->mask, set);
2170 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2171 SIG2OSIG(oset, td->td_retval[0]);
2176 ofreebsd32_sigsetmask(struct thread *td,
2177 struct ofreebsd32_sigsetmask_args *uap)
2181 OSIG2SIG(uap->mask, set);
2182 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2183 SIG2OSIG(oset, td->td_retval[0]);
2188 ofreebsd32_sigsuspend(struct thread *td,
2189 struct ofreebsd32_sigsuspend_args *uap)
2193 OSIG2SIG(uap->mask, mask);
2194 return (kern_sigsuspend(td, mask));
2203 ofreebsd32_sigstack(struct thread *td,
2204 struct ofreebsd32_sigstack_args *uap)
2206 struct sigstack32 s32;
2207 struct sigstack nss, oss;
2208 int error = 0, unss;
2210 if (uap->nss != NULL) {
2211 error = copyin(uap->nss, &s32, sizeof(s32));
2214 nss.ss_sp = PTRIN(s32.ss_sp);
2215 CP(s32, nss, ss_onstack);
2220 oss.ss_sp = td->td_sigstk.ss_sp;
2221 oss.ss_onstack = sigonstack(cpu_getstack(td));
2223 td->td_sigstk.ss_sp = nss.ss_sp;
2224 td->td_sigstk.ss_size = 0;
2225 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2226 td->td_pflags |= TDP_ALTSTACK;
2228 if (uap->oss != NULL) {
2229 s32.ss_sp = PTROUT(oss.ss_sp);
2230 CP(oss, s32, ss_onstack);
2231 error = copyout(&s32, uap->oss, sizeof(s32));
2238 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2240 struct timespec32 rmt32, rqt32;
2241 struct timespec rmt, rqt;
2244 error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2248 CP(rqt32, rqt, tv_sec);
2249 CP(rqt32, rqt, tv_nsec);
2252 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2254 error = kern_nanosleep(td, &rqt, &rmt);
2255 if (error && uap->rmtp) {
2258 CP(rmt, rmt32, tv_sec);
2259 CP(rmt, rmt32, tv_nsec);
2261 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2269 freebsd32_clock_gettime(struct thread *td,
2270 struct freebsd32_clock_gettime_args *uap)
2272 struct timespec ats;
2273 struct timespec32 ats32;
2276 error = kern_clock_gettime(td, uap->clock_id, &ats);
2278 CP(ats, ats32, tv_sec);
2279 CP(ats, ats32, tv_nsec);
2280 error = copyout(&ats32, uap->tp, sizeof(ats32));
2286 freebsd32_clock_settime(struct thread *td,
2287 struct freebsd32_clock_settime_args *uap)
2289 struct timespec ats;
2290 struct timespec32 ats32;
2293 error = copyin(uap->tp, &ats32, sizeof(ats32));
2296 CP(ats32, ats, tv_sec);
2297 CP(ats32, ats, tv_nsec);
2299 return (kern_clock_settime(td, uap->clock_id, &ats));
2303 freebsd32_clock_getres(struct thread *td,
2304 struct freebsd32_clock_getres_args *uap)
2307 struct timespec32 ts32;
2310 if (uap->tp == NULL)
2312 error = kern_clock_getres(td, uap->clock_id, &ts);
2314 CP(ts, ts32, tv_sec);
2315 CP(ts, ts32, tv_nsec);
2316 error = copyout(&ts32, uap->tp, sizeof(ts32));
2322 freebsd32_thr_new(struct thread *td,
2323 struct freebsd32_thr_new_args *uap)
2325 struct thr_param32 param32;
2326 struct thr_param param;
2329 if (uap->param_size < 0 ||
2330 uap->param_size > sizeof(struct thr_param32))
2332 bzero(¶m, sizeof(struct thr_param));
2333 bzero(¶m32, sizeof(struct thr_param32));
2334 error = copyin(uap->param, ¶m32, uap->param_size);
2337 param.start_func = PTRIN(param32.start_func);
2338 param.arg = PTRIN(param32.arg);
2339 param.stack_base = PTRIN(param32.stack_base);
2340 param.stack_size = param32.stack_size;
2341 param.tls_base = PTRIN(param32.tls_base);
2342 param.tls_size = param32.tls_size;
2343 param.child_tid = PTRIN(param32.child_tid);
2344 param.parent_tid = PTRIN(param32.parent_tid);
2345 param.flags = param32.flags;
2346 param.rtp = PTRIN(param32.rtp);
2347 param.spare[0] = PTRIN(param32.spare[0]);
2348 param.spare[1] = PTRIN(param32.spare[1]);
2349 param.spare[2] = PTRIN(param32.spare[2]);
2351 return (kern_thr_new(td, ¶m));
2355 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2357 struct timespec32 ts32;
2358 struct timespec ts, *tsp;
2363 if (uap->timeout != NULL) {
2364 error = copyin((const void *)uap->timeout, (void *)&ts32,
2365 sizeof(struct timespec32));
2368 ts.tv_sec = ts32.tv_sec;
2369 ts.tv_nsec = ts32.tv_nsec;
2372 return (kern_thr_suspend(td, tsp));
2376 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2378 bzero(dst, sizeof(*dst));
2379 dst->si_signo = src->si_signo;
2380 dst->si_errno = src->si_errno;
2381 dst->si_code = src->si_code;
2382 dst->si_pid = src->si_pid;
2383 dst->si_uid = src->si_uid;
2384 dst->si_status = src->si_status;
2385 dst->si_addr = (uintptr_t)src->si_addr;
2386 dst->si_value.sigval_int = src->si_value.sival_int;
2387 dst->si_timerid = src->si_timerid;
2388 dst->si_overrun = src->si_overrun;
2392 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2394 struct timespec32 ts32;
2396 struct timespec *timeout;
2399 struct siginfo32 si32;
2403 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2406 ts.tv_sec = ts32.tv_sec;
2407 ts.tv_nsec = ts32.tv_nsec;
2412 error = copyin(uap->set, &set, sizeof(set));
2416 error = kern_sigtimedwait(td, set, &ksi, timeout);
2421 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2422 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2426 td->td_retval[0] = ksi.ksi_signo;
2434 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2437 struct siginfo32 si32;
2441 error = copyin(uap->set, &set, sizeof(set));
2445 error = kern_sigtimedwait(td, set, &ksi, NULL);
2450 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2451 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2454 td->td_retval[0] = ksi.ksi_signo;
2459 freebsd32_cpuset_setid(struct thread *td,
2460 struct freebsd32_cpuset_setid_args *uap)
2462 struct cpuset_setid_args ap;
2464 ap.which = uap->which;
2465 ap.id = PAIR32TO64(id_t,uap->id);
2466 ap.setid = uap->setid;
2468 return (sys_cpuset_setid(td, &ap));
2472 freebsd32_cpuset_getid(struct thread *td,
2473 struct freebsd32_cpuset_getid_args *uap)
2475 struct cpuset_getid_args ap;
2477 ap.level = uap->level;
2478 ap.which = uap->which;
2479 ap.id = PAIR32TO64(id_t,uap->id);
2480 ap.setid = uap->setid;
2482 return (sys_cpuset_getid(td, &ap));
2486 freebsd32_cpuset_getaffinity(struct thread *td,
2487 struct freebsd32_cpuset_getaffinity_args *uap)
2489 struct cpuset_getaffinity_args ap;
2491 ap.level = uap->level;
2492 ap.which = uap->which;
2493 ap.id = PAIR32TO64(id_t,uap->id);
2494 ap.cpusetsize = uap->cpusetsize;
2495 ap.mask = uap->mask;
2497 return (sys_cpuset_getaffinity(td, &ap));
2501 freebsd32_cpuset_setaffinity(struct thread *td,
2502 struct freebsd32_cpuset_setaffinity_args *uap)
2504 struct cpuset_setaffinity_args ap;
2506 ap.level = uap->level;
2507 ap.which = uap->which;
2508 ap.id = PAIR32TO64(id_t,uap->id);
2509 ap.cpusetsize = uap->cpusetsize;
2510 ap.mask = uap->mask;
2512 return (sys_cpuset_setaffinity(td, &ap));
2516 freebsd32_nmount(struct thread *td,
2517 struct freebsd32_nmount_args /* {
2519 unsigned int iovcnt;
2528 * Mount flags are now 64-bits. On 32-bit archtectures only
2529 * 32-bits are passed in, but from here on everything handles
2530 * 64-bit flags correctly.
2534 AUDIT_ARG_FFLAGS(flags);
2537 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2538 * userspace to set this flag, but we must filter it out if we want
2539 * MNT_UPDATE on the root file system to work.
2540 * MNT_ROOTFS should only be set by the kernel when mounting its
2543 flags &= ~MNT_ROOTFS;
2546 * check that we have an even number of iovec's
2547 * and that we have at least two options.
2549 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2552 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2555 error = vfs_donmount(td, flags, auio);
2563 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2565 struct yyy32 *p32, s32;
2566 struct yyy *p = NULL, s;
2571 error = copyin(uap->zzz, &s32, sizeof(s32));
2577 error = kern_xxx(td, p);
2582 error = copyout(&s32, p32, sizeof(s32));
2589 syscall32_register(int *offset, struct sysent *new_sysent,
2590 struct sysent *old_sysent)
2592 if (*offset == NO_SYSCALL) {
2595 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2596 if (freebsd32_sysent[i].sy_call ==
2597 (sy_call_t *)lkmnosys)
2599 if (i == SYS_MAXSYSCALL)
2602 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2604 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2605 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2608 *old_sysent = freebsd32_sysent[*offset];
2609 freebsd32_sysent[*offset] = *new_sysent;
2614 syscall32_deregister(int *offset, struct sysent *old_sysent)
2618 freebsd32_sysent[*offset] = *old_sysent;
2623 syscall32_module_handler(struct module *mod, int what, void *arg)
2625 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2631 error = syscall32_register(data->offset, data->new_sysent,
2634 /* Leave a mark so we know to safely unload below. */
2635 data->offset = NULL;
2638 ms.intval = *data->offset;
2640 module_setspecific(mod, &ms);
2643 error = data->chainevh(mod, what, data->chainarg);
2647 * MOD_LOAD failed, so just return without calling the
2648 * chained handler since we didn't pass along the MOD_LOAD
2651 if (data->offset == NULL)
2653 if (data->chainevh) {
2654 error = data->chainevh(mod, what, data->chainarg);
2658 error = syscall32_deregister(data->offset, &data->old_sysent);
2663 error = data->chainevh(mod, what, data->chainarg);
2669 syscall32_helper_register(struct syscall_helper_data *sd)
2671 struct syscall_helper_data *sd1;
2674 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2675 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2678 syscall32_helper_unregister(sd);
2681 sd1->registered = 1;
2687 syscall32_helper_unregister(struct syscall_helper_data *sd)
2689 struct syscall_helper_data *sd1;
2691 for (sd1 = sd; sd1->registered != 0; sd1++) {
2692 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2693 sd1->registered = 0;
2699 freebsd32_copyout_strings(struct image_params *imgp)
2703 char *stringp, *destp;
2704 u_int32_t *stack_base;
2705 struct freebsd32_ps_strings *arginfo;
2706 char canary[sizeof(long) * 8];
2707 int32_t pagesizes32[MAXPAGESIZES];
2708 size_t execpath_len;
2712 * Calculate string base and vector table pointers.
2713 * Also deal with signal trampoline code for this exec type.
2715 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2716 execpath_len = strlen(imgp->execpath) + 1;
2719 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2721 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2722 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2725 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2726 roundup(execpath_len, sizeof(char *)) -
2727 roundup(sizeof(canary), sizeof(char *)) -
2728 roundup(sizeof(pagesizes32), sizeof(char *)) -
2729 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2735 copyout(imgp->proc->p_sysent->sv_sigcode,
2736 ((caddr_t)arginfo - szsigcode), szsigcode);
2739 * Copy the image path for the rtld.
2741 if (execpath_len != 0) {
2742 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2743 copyout(imgp->execpath, (void *)imgp->execpathp,
2748 * Prepare the canary for SSP.
2750 arc4rand(canary, sizeof(canary), 0);
2751 imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2753 copyout(canary, (void *)imgp->canary, sizeof(canary));
2754 imgp->canarylen = sizeof(canary);
2757 * Prepare the pagesizes array.
2759 for (i = 0; i < MAXPAGESIZES; i++)
2760 pagesizes32[i] = (uint32_t)pagesizes[i];
2761 imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2762 roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2763 copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2764 imgp->pagesizeslen = sizeof(pagesizes32);
2767 * If we have a valid auxargs ptr, prepare some room
2770 if (imgp->auxargs) {
2772 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2773 * lower compatibility.
2775 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2778 * The '+ 2' is for the null pointers at the end of each of
2779 * the arg and env vector sets,and imgp->auxarg_size is room
2780 * for argument of Runtime loader.
2782 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2783 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2787 * The '+ 2' is for the null pointers at the end of each of
2788 * the arg and env vector sets
2790 vectp = (u_int32_t *)
2791 (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2794 * vectp also becomes our initial stack base
2798 stringp = imgp->args->begin_argv;
2799 argc = imgp->args->argc;
2800 envc = imgp->args->envc;
2802 * Copy out strings - arguments and environment.
2804 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2807 * Fill in "ps_strings" struct for ps, w, etc.
2809 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2810 suword32(&arginfo->ps_nargvstr, argc);
2813 * Fill in argument portion of vector table.
2815 for (; argc > 0; --argc) {
2816 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2817 while (*stringp++ != 0)
2822 /* a null vector table pointer separates the argp's from the envp's */
2823 suword32(vectp++, 0);
2825 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2826 suword32(&arginfo->ps_nenvstr, envc);
2829 * Fill in environment portion of vector table.
2831 for (; envc > 0; --envc) {
2832 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2833 while (*stringp++ != 0)
2838 /* end of vector table is a null pointer */
2841 return ((register_t *)stack_base);
2845 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2847 struct kld_file_stat stat;
2848 struct kld32_file_stat stat32;
2851 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2854 if (version != sizeof(struct kld32_file_stat_1) &&
2855 version != sizeof(struct kld32_file_stat))
2858 error = kern_kldstat(td, uap->fileid, &stat);
2862 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2863 CP(stat, stat32, refs);
2864 CP(stat, stat32, id);
2865 PTROUT_CP(stat, stat32, address);
2866 CP(stat, stat32, size);
2867 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2868 return (copyout(&stat32, uap->stat, version));
2872 freebsd32_posix_fallocate(struct thread *td,
2873 struct freebsd32_posix_fallocate_args *uap)
2876 return (kern_posix_fallocate(td, uap->fd,
2877 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2881 freebsd32_posix_fadvise(struct thread *td,
2882 struct freebsd32_posix_fadvise_args *uap)
2885 return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2886 PAIR32TO64(off_t, uap->len), uap->advice));