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));
177 #ifdef COMPAT_FREEBSD4
179 copy_statfs(struct statfs *in, struct statfs32 *out)
182 statfs_scale_blocks(in, INT32_MAX);
183 bzero(out, sizeof(*out));
184 CP(*in, *out, f_bsize);
185 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
186 CP(*in, *out, f_blocks);
187 CP(*in, *out, f_bfree);
188 CP(*in, *out, f_bavail);
189 out->f_files = MIN(in->f_files, INT32_MAX);
190 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
191 CP(*in, *out, f_fsid);
192 CP(*in, *out, f_owner);
193 CP(*in, *out, f_type);
194 CP(*in, *out, f_flags);
195 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
196 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
197 strlcpy(out->f_fstypename,
198 in->f_fstypename, MFSNAMELEN);
199 strlcpy(out->f_mntonname,
200 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
201 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
202 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
203 strlcpy(out->f_mntfromname,
204 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
208 #ifdef COMPAT_FREEBSD4
210 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
212 struct statfs *buf, *sp;
213 struct statfs32 stat32;
217 count = uap->bufsize / sizeof(struct statfs32);
218 size = count * sizeof(struct statfs);
219 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
221 count = td->td_retval[0];
223 while (count > 0 && error == 0) {
224 copy_statfs(sp, &stat32);
225 error = copyout(&stat32, uap->buf, sizeof(stat32));
237 freebsd32_sigaltstack(struct thread *td,
238 struct freebsd32_sigaltstack_args *uap)
240 struct sigaltstack32 s32;
241 struct sigaltstack ss, oss, *ssp;
244 if (uap->ss != NULL) {
245 error = copyin(uap->ss, &s32, sizeof(s32));
248 PTRIN_CP(s32, ss, ss_sp);
249 CP(s32, ss, ss_size);
250 CP(s32, ss, ss_flags);
254 error = kern_sigaltstack(td, ssp, &oss);
255 if (error == 0 && uap->oss != NULL) {
256 PTROUT_CP(oss, s32, ss_sp);
257 CP(oss, s32, ss_size);
258 CP(oss, s32, ss_flags);
259 error = copyout(&s32, uap->oss, sizeof(s32));
265 * Custom version of exec_copyin_args() so that we can translate
269 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
270 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
277 bzero(args, sizeof(*args));
282 * Allocate demand-paged memory for the file name, argument, and
283 * environment strings.
285 error = exec_alloc_args(args);
290 * Copy the file name.
293 args->fname = args->buf;
294 error = (segflg == UIO_SYSSPACE) ?
295 copystr(fname, args->fname, PATH_MAX, &length) :
296 copyinstr(fname, args->fname, PATH_MAX, &length);
302 args->begin_argv = args->buf + length;
303 args->endp = args->begin_argv;
304 args->stringspace = ARG_MAX;
307 * extract arguments first
311 error = copyin(p32++, &arg, sizeof(arg));
317 error = copyinstr(argp, args->endp, args->stringspace, &length);
319 if (error == ENAMETOOLONG)
323 args->stringspace -= length;
324 args->endp += length;
328 args->begin_envv = args->endp;
331 * extract environment strings
336 error = copyin(p32++, &arg, sizeof(arg));
342 error = copyinstr(envp, args->endp, args->stringspace,
345 if (error == ENAMETOOLONG)
349 args->stringspace -= length;
350 args->endp += length;
358 exec_free_args(args);
363 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
365 struct image_args eargs;
368 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
369 uap->argv, uap->envv);
371 error = kern_execve(td, &eargs, NULL);
376 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
378 struct image_args eargs;
381 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
382 uap->argv, uap->envv);
385 error = kern_execve(td, &eargs, NULL);
392 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
393 int prot, int fd, off_t pos)
396 vm_map_entry_t entry;
399 map = &td->td_proc->p_vmspace->vm_map;
401 prot |= VM_PROT_WRITE;
403 if (vm_map_lookup_entry(map, start, &entry)) {
404 if ((entry->protection & prot) != prot) {
405 rv = vm_map_protect(map,
408 entry->protection | prot,
410 if (rv != KERN_SUCCESS)
414 vm_offset_t addr = trunc_page(start);
415 rv = vm_map_find(map, 0, 0,
416 &addr, PAGE_SIZE, FALSE, prot,
418 if (rv != KERN_SUCCESS)
425 r.buf = (void *) start;
426 r.nbyte = end - start;
428 return (sys_pread(td, &r));
430 while (start < end) {
431 subyte((void *) start, 0);
440 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
443 vm_offset_t addr = (vm_offset_t) uap->addr;
444 vm_size_t len = uap->len;
445 int prot = uap->prot;
446 int flags = uap->flags;
448 off_t pos = PAIR32TO64(off_t,uap->pos);
454 * Attempt to handle page size hassles.
456 pageoff = (pos & PAGE_MASK);
457 if (flags & MAP_FIXED) {
458 vm_offset_t start, end;
462 if (start != trunc_page(start)) {
463 error = freebsd32_mmap_partial(td, start,
464 round_page(start), prot,
467 pos += round_page(start) - start;
468 start = round_page(start);
470 if (end != round_page(end)) {
471 vm_offset_t t = trunc_page(end);
472 error = freebsd32_mmap_partial(td, t, end,
475 end = trunc_page(end);
477 if (end > start && fd != -1 && (pos & PAGE_MASK)) {
479 * We can't map this region at all. The specified
480 * address doesn't have the same alignment as the file
481 * position. Fake the mapping by simply reading the
482 * entire region into memory. First we need to make
483 * sure the region exists.
489 prot |= VM_PROT_WRITE;
490 map = &td->td_proc->p_vmspace->vm_map;
491 rv = vm_map_remove(map, start, end);
492 if (rv != KERN_SUCCESS)
494 rv = vm_map_find(map, 0, 0,
495 &start, end - start, FALSE,
496 prot, VM_PROT_ALL, 0);
497 if (rv != KERN_SUCCESS)
500 r.buf = (void *) start;
501 r.nbyte = end - start;
503 error = sys_pread(td, &r);
507 td->td_retval[0] = addr;
512 * After dealing with the ragged ends, there
513 * might be none left.
515 td->td_retval[0] = addr;
523 ap.addr = (void *) addr;
530 return (sys_mmap(td, &ap));
533 #ifdef COMPAT_FREEBSD6
535 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
537 struct freebsd32_mmap_args ap;
542 ap.flags = uap->flags;
547 return (freebsd32_mmap(td, &ap));
552 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
554 struct itimerval itv, oitv, *itvp;
555 struct itimerval32 i32;
558 if (uap->itv != NULL) {
559 error = copyin(uap->itv, &i32, sizeof(i32));
562 TV_CP(i32, itv, it_interval);
563 TV_CP(i32, itv, it_value);
567 error = kern_setitimer(td, uap->which, itvp, &oitv);
568 if (error || uap->oitv == NULL)
570 TV_CP(oitv, i32, it_interval);
571 TV_CP(oitv, i32, it_value);
572 return (copyout(&i32, uap->oitv, sizeof(i32)));
576 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
578 struct itimerval itv;
579 struct itimerval32 i32;
582 error = kern_getitimer(td, uap->which, &itv);
583 if (error || uap->itv == NULL)
585 TV_CP(itv, i32, it_interval);
586 TV_CP(itv, i32, it_value);
587 return (copyout(&i32, uap->itv, sizeof(i32)));
591 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
593 struct timeval32 tv32;
594 struct timeval tv, *tvp;
597 if (uap->tv != NULL) {
598 error = copyin(uap->tv, &tv32, sizeof(tv32));
601 CP(tv32, tv, tv_sec);
602 CP(tv32, tv, tv_usec);
607 * XXX Do pointers need PTRIN()?
609 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
610 sizeof(int32_t) * 8));
614 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
616 struct timespec32 ts32;
618 struct timeval tv, *tvp;
622 if (uap->ts != NULL) {
623 error = copyin(uap->ts, &ts32, sizeof(ts32));
626 CP(ts32, ts, tv_sec);
627 CP(ts32, ts, tv_nsec);
628 TIMESPEC_TO_TIMEVAL(&tv, &ts);
632 if (uap->sm != NULL) {
633 error = copyin(uap->sm, &set, sizeof(set));
640 * XXX Do pointers need PTRIN()?
642 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
643 uset, sizeof(int32_t) * 8);
648 * Copy 'count' items into the destination list pointed to by uap->eventlist.
651 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
653 struct freebsd32_kevent_args *uap;
654 struct kevent32 ks32[KQ_NEVENTS];
657 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
658 uap = (struct freebsd32_kevent_args *)arg;
660 for (i = 0; i < count; i++) {
661 CP(kevp[i], ks32[i], ident);
662 CP(kevp[i], ks32[i], filter);
663 CP(kevp[i], ks32[i], flags);
664 CP(kevp[i], ks32[i], fflags);
665 CP(kevp[i], ks32[i], data);
666 PTROUT_CP(kevp[i], ks32[i], udata);
668 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
670 uap->eventlist += count;
675 * Copy 'count' items from the list pointed to by uap->changelist.
678 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
680 struct freebsd32_kevent_args *uap;
681 struct kevent32 ks32[KQ_NEVENTS];
684 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
685 uap = (struct freebsd32_kevent_args *)arg;
687 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
690 uap->changelist += count;
692 for (i = 0; i < count; i++) {
693 CP(ks32[i], kevp[i], ident);
694 CP(ks32[i], kevp[i], filter);
695 CP(ks32[i], kevp[i], flags);
696 CP(ks32[i], kevp[i], fflags);
697 CP(ks32[i], kevp[i], data);
698 PTRIN_CP(ks32[i], kevp[i], udata);
705 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
707 struct timespec32 ts32;
708 struct timespec ts, *tsp;
709 struct kevent_copyops k_ops = { uap,
710 freebsd32_kevent_copyout,
711 freebsd32_kevent_copyin};
716 error = copyin(uap->timeout, &ts32, sizeof(ts32));
719 CP(ts32, ts, tv_sec);
720 CP(ts32, ts, tv_nsec);
724 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
730 freebsd32_gettimeofday(struct thread *td,
731 struct freebsd32_gettimeofday_args *uap)
734 struct timeval32 atv32;
740 CP(atv, atv32, tv_sec);
741 CP(atv, atv32, tv_usec);
742 error = copyout(&atv32, uap->tp, sizeof (atv32));
744 if (error == 0 && uap->tzp != NULL) {
745 rtz.tz_minuteswest = tz_minuteswest;
746 rtz.tz_dsttime = tz_dsttime;
747 error = copyout(&rtz, uap->tzp, sizeof (rtz));
753 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
759 error = kern_getrusage(td, uap->who, &s);
762 if (uap->rusage != NULL) {
763 freebsd32_rusage_out(&s, &s32);
764 error = copyout(&s32, uap->rusage, sizeof(s32));
770 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
772 struct iovec32 iov32;
779 if (iovcnt > UIO_MAXIOV)
781 iovlen = iovcnt * sizeof(struct iovec);
782 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
783 iov = (struct iovec *)(uio + 1);
784 for (i = 0; i < iovcnt; i++) {
785 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
790 iov[i].iov_base = PTRIN(iov32.iov_base);
791 iov[i].iov_len = iov32.iov_len;
794 uio->uio_iovcnt = iovcnt;
795 uio->uio_segflg = UIO_USERSPACE;
796 uio->uio_offset = -1;
798 for (i = 0; i < iovcnt; i++) {
799 if (iov->iov_len > INT_MAX - uio->uio_resid) {
803 uio->uio_resid += iov->iov_len;
811 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
816 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
819 error = kern_readv(td, uap->fd, auio);
825 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
830 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
833 error = kern_writev(td, uap->fd, auio);
839 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
844 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
847 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
853 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
858 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
861 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
867 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
870 struct iovec32 iov32;
876 if (iovcnt > UIO_MAXIOV)
878 iovlen = iovcnt * sizeof(struct iovec);
879 iov = malloc(iovlen, M_IOV, M_WAITOK);
880 for (i = 0; i < iovcnt; i++) {
881 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
886 iov[i].iov_base = PTRIN(iov32.iov_base);
887 iov[i].iov_len = iov32.iov_len;
894 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
899 error = copyin(msg32, &m32, sizeof(m32));
902 msg->msg_name = PTRIN(m32.msg_name);
903 msg->msg_namelen = m32.msg_namelen;
904 msg->msg_iov = PTRIN(m32.msg_iov);
905 msg->msg_iovlen = m32.msg_iovlen;
906 msg->msg_control = PTRIN(m32.msg_control);
907 msg->msg_controllen = m32.msg_controllen;
908 msg->msg_flags = m32.msg_flags;
913 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
918 m32.msg_name = PTROUT(msg->msg_name);
919 m32.msg_namelen = msg->msg_namelen;
920 m32.msg_iov = PTROUT(msg->msg_iov);
921 m32.msg_iovlen = msg->msg_iovlen;
922 m32.msg_control = PTROUT(msg->msg_control);
923 m32.msg_controllen = msg->msg_controllen;
924 m32.msg_flags = msg->msg_flags;
925 error = copyout(&m32, msg32, sizeof(m32));
929 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
930 #define FREEBSD32_ALIGN(p) \
931 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
932 #define FREEBSD32_CMSG_SPACE(l) \
933 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
935 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
936 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
938 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
942 socklen_t clen, datalen;
945 int len, maxlen, copylen;
949 len = msg->msg_controllen;
950 maxlen = msg->msg_controllen;
951 msg->msg_controllen = 0;
954 ctlbuf = msg->msg_control;
956 while (m && len > 0) {
957 cm = mtod(m, struct cmsghdr *);
962 if (sizeof(struct cmsghdr) > clen ||
963 cm->cmsg_len > clen) {
968 data = CMSG_DATA(cm);
969 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
971 /* Adjust message length */
972 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
977 copylen = sizeof(struct cmsghdr);
979 msg->msg_flags |= MSG_CTRUNC;
983 error = copyout(cm,ctlbuf,copylen);
987 ctlbuf += FREEBSD32_ALIGN(copylen);
988 len -= FREEBSD32_ALIGN(copylen);
996 msg->msg_flags |= MSG_CTRUNC;
1000 error = copyout(data,ctlbuf,copylen);
1004 ctlbuf += FREEBSD32_ALIGN(copylen);
1005 len -= FREEBSD32_ALIGN(copylen);
1007 if (CMSG_SPACE(datalen) < clen) {
1008 clen -= CMSG_SPACE(datalen);
1009 cm = (struct cmsghdr *)
1010 ((caddr_t)cm + CMSG_SPACE(datalen));
1019 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1027 freebsd32_recvmsg(td, uap)
1029 struct freebsd32_recvmsg_args /* {
1031 struct msghdr32 *msg;
1036 struct msghdr32 m32;
1037 struct iovec *uiov, *iov;
1038 struct mbuf *control = NULL;
1039 struct mbuf **controlp;
1042 error = copyin(uap->msg, &m32, sizeof(m32));
1045 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1048 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1052 msg.msg_flags = uap->flags;
1056 controlp = (msg.msg_control != NULL) ? &control : NULL;
1057 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1061 if (control != NULL)
1062 error = freebsd32_copy_msg_out(&msg, control);
1064 msg.msg_controllen = 0;
1067 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1071 if (control != NULL)
1079 freebsd32_convert_msg_in(struct mbuf **controlp)
1081 struct mbuf *control = *controlp;
1082 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1084 socklen_t clen = control->m_len, datalen;
1090 while (cm != NULL) {
1091 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1096 data = FREEBSD32_CMSG_DATA(cm);
1097 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1099 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1101 controlp = &(*controlp)->m_next;
1103 if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1104 clen -= FREEBSD32_CMSG_SPACE(datalen);
1105 cm = (struct cmsghdr *)
1106 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1119 freebsd32_sendmsg(struct thread *td,
1120 struct freebsd32_sendmsg_args *uap)
1123 struct msghdr32 m32;
1125 struct mbuf *control = NULL;
1126 struct sockaddr *to = NULL;
1129 error = copyin(uap->msg, &m32, sizeof(m32));
1132 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1135 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1140 if (msg.msg_name != NULL) {
1141 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1149 if (msg.msg_control) {
1150 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1155 error = sockargs(&control, msg.msg_control,
1156 msg.msg_controllen, MT_CONTROL);
1160 error = freebsd32_convert_msg_in(&control);
1165 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1176 freebsd32_recvfrom(struct thread *td,
1177 struct freebsd32_recvfrom_args *uap)
1183 if (uap->fromlenaddr) {
1184 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1185 sizeof(msg.msg_namelen));
1189 msg.msg_namelen = 0;
1192 msg.msg_name = PTRIN(uap->from);
1193 msg.msg_iov = &aiov;
1195 aiov.iov_base = PTRIN(uap->buf);
1196 aiov.iov_len = uap->len;
1197 msg.msg_control = NULL;
1198 msg.msg_flags = uap->flags;
1199 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1200 if (error == 0 && uap->fromlenaddr)
1201 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1202 sizeof (msg.msg_namelen));
1207 freebsd32_settimeofday(struct thread *td,
1208 struct freebsd32_settimeofday_args *uap)
1210 struct timeval32 tv32;
1211 struct timeval tv, *tvp;
1212 struct timezone tz, *tzp;
1216 error = copyin(uap->tv, &tv32, sizeof(tv32));
1219 CP(tv32, tv, tv_sec);
1220 CP(tv32, tv, tv_usec);
1225 error = copyin(uap->tzp, &tz, sizeof(tz));
1231 return (kern_settimeofday(td, tvp, tzp));
1235 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1237 struct timeval32 s32[2];
1238 struct timeval s[2], *sp;
1241 if (uap->tptr != NULL) {
1242 error = copyin(uap->tptr, s32, sizeof(s32));
1245 CP(s32[0], s[0], tv_sec);
1246 CP(s32[0], s[0], tv_usec);
1247 CP(s32[1], s[1], tv_sec);
1248 CP(s32[1], s[1], tv_usec);
1252 return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1256 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1258 struct timeval32 s32[2];
1259 struct timeval s[2], *sp;
1262 if (uap->tptr != NULL) {
1263 error = copyin(uap->tptr, s32, sizeof(s32));
1266 CP(s32[0], s[0], tv_sec);
1267 CP(s32[0], s[0], tv_usec);
1268 CP(s32[1], s[1], tv_sec);
1269 CP(s32[1], s[1], tv_usec);
1273 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1277 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1279 struct timeval32 s32[2];
1280 struct timeval s[2], *sp;
1283 if (uap->tptr != NULL) {
1284 error = copyin(uap->tptr, s32, sizeof(s32));
1287 CP(s32[0], s[0], tv_sec);
1288 CP(s32[0], s[0], tv_usec);
1289 CP(s32[1], s[1], tv_sec);
1290 CP(s32[1], s[1], tv_usec);
1294 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1298 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1300 struct timeval32 s32[2];
1301 struct timeval s[2], *sp;
1304 if (uap->times != NULL) {
1305 error = copyin(uap->times, s32, sizeof(s32));
1308 CP(s32[0], s[0], tv_sec);
1309 CP(s32[0], s[0], tv_usec);
1310 CP(s32[1], s[1], tv_sec);
1311 CP(s32[1], s[1], tv_usec);
1315 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1320 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1322 struct timeval32 tv32;
1323 struct timeval delta, olddelta, *deltap;
1327 error = copyin(uap->delta, &tv32, sizeof(tv32));
1330 CP(tv32, delta, tv_sec);
1331 CP(tv32, delta, tv_usec);
1335 error = kern_adjtime(td, deltap, &olddelta);
1336 if (uap->olddelta && error == 0) {
1337 CP(olddelta, tv32, tv_sec);
1338 CP(olddelta, tv32, tv_usec);
1339 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1344 #ifdef COMPAT_FREEBSD4
1346 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1348 struct statfs32 s32;
1352 error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1355 copy_statfs(&s, &s32);
1356 return (copyout(&s32, uap->buf, sizeof(s32)));
1360 #ifdef COMPAT_FREEBSD4
1362 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1364 struct statfs32 s32;
1368 error = kern_fstatfs(td, uap->fd, &s);
1371 copy_statfs(&s, &s32);
1372 return (copyout(&s32, uap->buf, sizeof(s32)));
1376 #ifdef COMPAT_FREEBSD4
1378 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1380 struct statfs32 s32;
1385 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1387 error = kern_fhstatfs(td, fh, &s);
1390 copy_statfs(&s, &s32);
1391 return (copyout(&s32, uap->buf, sizeof(s32)));
1396 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1398 struct pread_args ap;
1402 ap.nbyte = uap->nbyte;
1403 ap.offset = PAIR32TO64(off_t,uap->offset);
1404 return (sys_pread(td, &ap));
1408 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1410 struct pwrite_args ap;
1414 ap.nbyte = uap->nbyte;
1415 ap.offset = PAIR32TO64(off_t,uap->offset);
1416 return (sys_pwrite(td, &ap));
1421 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1423 struct lseek_args nuap;
1426 nuap.offset = uap->offset;
1427 nuap.whence = uap->whence;
1428 return (sys_lseek(td, &nuap));
1433 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1436 struct lseek_args ap;
1440 ap.offset = PAIR32TO64(off_t,uap->offset);
1441 ap.whence = uap->whence;
1442 error = sys_lseek(td, &ap);
1443 /* Expand the quad return into two parts for eax and edx */
1444 pos = *(off_t *)(td->td_retval);
1445 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1446 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1451 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1453 struct truncate_args ap;
1455 ap.path = uap->path;
1456 ap.length = PAIR32TO64(off_t,uap->length);
1457 return (sys_truncate(td, &ap));
1461 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1463 struct ftruncate_args ap;
1466 ap.length = PAIR32TO64(off_t,uap->length);
1467 return (sys_ftruncate(td, &ap));
1472 ofreebsd32_getdirentries(struct thread *td,
1473 struct ofreebsd32_getdirentries_args *uap)
1475 struct ogetdirentries_args ap;
1482 ap.count = uap->count;
1484 error = kern_ogetdirentries(td, &ap, &loff);
1487 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1494 freebsd32_getdirentries(struct thread *td,
1495 struct freebsd32_getdirentries_args *uap)
1501 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
1504 if (uap->basep != NULL) {
1506 error = copyout(&base32, uap->basep, sizeof(int32_t));
1511 #ifdef COMPAT_FREEBSD6
1512 /* versions with the 'int pad' argument */
1514 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1516 struct pread_args ap;
1520 ap.nbyte = uap->nbyte;
1521 ap.offset = PAIR32TO64(off_t,uap->offset);
1522 return (sys_pread(td, &ap));
1526 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1528 struct pwrite_args ap;
1532 ap.nbyte = uap->nbyte;
1533 ap.offset = PAIR32TO64(off_t,uap->offset);
1534 return (sys_pwrite(td, &ap));
1538 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1541 struct lseek_args ap;
1545 ap.offset = PAIR32TO64(off_t,uap->offset);
1546 ap.whence = uap->whence;
1547 error = sys_lseek(td, &ap);
1548 /* Expand the quad return into two parts for eax and edx */
1549 pos = *(off_t *)(td->td_retval);
1550 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1551 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1556 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1558 struct truncate_args ap;
1560 ap.path = uap->path;
1561 ap.length = PAIR32TO64(off_t,uap->length);
1562 return (sys_truncate(td, &ap));
1566 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1568 struct ftruncate_args ap;
1571 ap.length = PAIR32TO64(off_t,uap->length);
1572 return (sys_ftruncate(td, &ap));
1574 #endif /* COMPAT_FREEBSD6 */
1584 freebsd32_do_sendfile(struct thread *td,
1585 struct freebsd32_sendfile_args *uap, int compat)
1587 struct sendfile_args ap;
1588 struct sf_hdtr32 hdtr32;
1589 struct sf_hdtr hdtr;
1590 struct uio *hdr_uio, *trl_uio;
1591 struct iovec32 *iov32;
1594 hdr_uio = trl_uio = NULL;
1598 ap.offset = PAIR32TO64(off_t,uap->offset);
1599 ap.nbytes = uap->nbytes;
1600 ap.hdtr = (struct sf_hdtr *)uap->hdtr; /* XXX not used */
1601 ap.sbytes = uap->sbytes;
1602 ap.flags = uap->flags;
1604 if (uap->hdtr != NULL) {
1605 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1608 PTRIN_CP(hdtr32, hdtr, headers);
1609 CP(hdtr32, hdtr, hdr_cnt);
1610 PTRIN_CP(hdtr32, hdtr, trailers);
1611 CP(hdtr32, hdtr, trl_cnt);
1613 if (hdtr.headers != NULL) {
1614 iov32 = PTRIN(hdtr32.headers);
1615 error = freebsd32_copyinuio(iov32,
1616 hdtr32.hdr_cnt, &hdr_uio);
1620 if (hdtr.trailers != NULL) {
1621 iov32 = PTRIN(hdtr32.trailers);
1622 error = freebsd32_copyinuio(iov32,
1623 hdtr32.trl_cnt, &trl_uio);
1629 error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1632 free(hdr_uio, M_IOV);
1634 free(trl_uio, M_IOV);
1638 #ifdef COMPAT_FREEBSD4
1640 freebsd4_freebsd32_sendfile(struct thread *td,
1641 struct freebsd4_freebsd32_sendfile_args *uap)
1643 return (freebsd32_do_sendfile(td,
1644 (struct freebsd32_sendfile_args *)uap, 1));
1649 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1652 return (freebsd32_do_sendfile(td, uap, 0));
1656 copy_stat(struct stat *in, struct stat32 *out)
1659 CP(*in, *out, st_dev);
1660 CP(*in, *out, st_ino);
1661 CP(*in, *out, st_mode);
1662 CP(*in, *out, st_nlink);
1663 CP(*in, *out, st_uid);
1664 CP(*in, *out, st_gid);
1665 CP(*in, *out, st_rdev);
1666 TS_CP(*in, *out, st_atim);
1667 TS_CP(*in, *out, st_mtim);
1668 TS_CP(*in, *out, st_ctim);
1669 CP(*in, *out, st_size);
1670 CP(*in, *out, st_blocks);
1671 CP(*in, *out, st_blksize);
1672 CP(*in, *out, st_flags);
1673 CP(*in, *out, st_gen);
1674 TS_CP(*in, *out, st_birthtim);
1679 copy_ostat(struct stat *in, struct ostat32 *out)
1682 CP(*in, *out, st_dev);
1683 CP(*in, *out, st_ino);
1684 CP(*in, *out, st_mode);
1685 CP(*in, *out, st_nlink);
1686 CP(*in, *out, st_uid);
1687 CP(*in, *out, st_gid);
1688 CP(*in, *out, st_rdev);
1689 CP(*in, *out, st_size);
1690 TS_CP(*in, *out, st_atim);
1691 TS_CP(*in, *out, st_mtim);
1692 TS_CP(*in, *out, st_ctim);
1693 CP(*in, *out, st_blksize);
1694 CP(*in, *out, st_blocks);
1695 CP(*in, *out, st_flags);
1696 CP(*in, *out, st_gen);
1701 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1707 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1710 copy_stat(&sb, &sb32);
1711 error = copyout(&sb32, uap->ub, sizeof (sb32));
1717 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1720 struct ostat32 sb32;
1723 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1726 copy_ostat(&sb, &sb32);
1727 error = copyout(&sb32, uap->ub, sizeof (sb32));
1733 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1739 error = kern_fstat(td, uap->fd, &ub);
1742 copy_stat(&ub, &ub32);
1743 error = copyout(&ub32, uap->ub, sizeof(ub32));
1749 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1752 struct ostat32 ub32;
1755 error = kern_fstat(td, uap->fd, &ub);
1758 copy_ostat(&ub, &ub32);
1759 error = copyout(&ub32, uap->ub, sizeof(ub32));
1765 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1771 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1774 copy_stat(&ub, &ub32);
1775 error = copyout(&ub32, uap->buf, sizeof(ub32));
1780 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1786 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1789 copy_stat(&sb, &sb32);
1790 error = copyout(&sb32, uap->ub, sizeof (sb32));
1796 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1799 struct ostat32 sb32;
1802 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1805 copy_ostat(&sb, &sb32);
1806 error = copyout(&sb32, uap->ub, sizeof (sb32));
1812 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1814 int error, name[CTL_MAXNAME];
1817 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1819 error = copyin(uap->name, name, uap->namelen * sizeof(int));
1823 oldlen = fuword32(uap->oldlenp);
1826 error = userland_sysctl(td, name, uap->namelen,
1827 uap->old, &oldlen, 1,
1828 uap->new, uap->newlen, &j, SCTL_MASK32);
1829 if (error && error != ENOMEM)
1832 suword32(uap->oldlenp, j);
1837 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1843 error = copyin(uap->jail, &version, sizeof(uint32_t));
1850 /* FreeBSD single IPv4 jails. */
1851 struct jail32_v0 j32_v0;
1853 bzero(&j, sizeof(struct jail));
1854 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1857 CP(j32_v0, j, version);
1858 PTRIN_CP(j32_v0, j, path);
1859 PTRIN_CP(j32_v0, j, hostname);
1860 j.ip4s = j32_v0.ip_number;
1866 * Version 1 was used by multi-IPv4 jail implementations
1867 * that never made it into the official kernel.
1871 case 2: /* JAIL_API_VERSION */
1873 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1876 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1879 CP(j32, j, version);
1880 PTRIN_CP(j32, j, path);
1881 PTRIN_CP(j32, j, hostname);
1882 PTRIN_CP(j32, j, jailname);
1885 PTRIN_CP(j32, j, ip4);
1886 PTRIN_CP(j32, j, ip6);
1891 /* Sci-Fi jails are not supported, sorry. */
1894 return (kern_jail(td, &j));
1898 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1903 /* Check that we have an even number of iovecs. */
1904 if (uap->iovcnt & 1)
1907 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1910 error = kern_jail_set(td, auio, uap->flags);
1916 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1918 struct iovec32 iov32;
1922 /* Check that we have an even number of iovecs. */
1923 if (uap->iovcnt & 1)
1926 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1929 error = kern_jail_get(td, auio, uap->flags);
1931 for (i = 0; i < uap->iovcnt; i++) {
1932 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1933 CP(auio->uio_iov[i], iov32, iov_len);
1934 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1943 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1945 struct sigaction32 s32;
1946 struct sigaction sa, osa, *sap;
1950 error = copyin(uap->act, &s32, sizeof(s32));
1953 sa.sa_handler = PTRIN(s32.sa_u);
1954 CP(s32, sa, sa_flags);
1955 CP(s32, sa, sa_mask);
1959 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1960 if (error == 0 && uap->oact != NULL) {
1961 s32.sa_u = PTROUT(osa.sa_handler);
1962 CP(osa, s32, sa_flags);
1963 CP(osa, s32, sa_mask);
1964 error = copyout(&s32, uap->oact, sizeof(s32));
1969 #ifdef COMPAT_FREEBSD4
1971 freebsd4_freebsd32_sigaction(struct thread *td,
1972 struct freebsd4_freebsd32_sigaction_args *uap)
1974 struct sigaction32 s32;
1975 struct sigaction sa, osa, *sap;
1979 error = copyin(uap->act, &s32, sizeof(s32));
1982 sa.sa_handler = PTRIN(s32.sa_u);
1983 CP(s32, sa, sa_flags);
1984 CP(s32, sa, sa_mask);
1988 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
1989 if (error == 0 && uap->oact != NULL) {
1990 s32.sa_u = PTROUT(osa.sa_handler);
1991 CP(osa, s32, sa_flags);
1992 CP(osa, s32, sa_mask);
1993 error = copyout(&s32, uap->oact, sizeof(s32));
2000 struct osigaction32 {
2009 ofreebsd32_sigaction(struct thread *td,
2010 struct ofreebsd32_sigaction_args *uap)
2012 struct osigaction32 s32;
2013 struct sigaction sa, osa, *sap;
2016 if (uap->signum <= 0 || uap->signum >= ONSIG)
2020 error = copyin(uap->nsa, &s32, sizeof(s32));
2023 sa.sa_handler = PTRIN(s32.sa_u);
2024 CP(s32, sa, sa_flags);
2025 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2029 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2030 if (error == 0 && uap->osa != NULL) {
2031 s32.sa_u = PTROUT(osa.sa_handler);
2032 CP(osa, s32, sa_flags);
2033 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2034 error = copyout(&s32, uap->osa, sizeof(s32));
2040 ofreebsd32_sigprocmask(struct thread *td,
2041 struct ofreebsd32_sigprocmask_args *uap)
2046 OSIG2SIG(uap->mask, set);
2047 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2048 SIG2OSIG(oset, td->td_retval[0]);
2053 ofreebsd32_sigpending(struct thread *td,
2054 struct ofreebsd32_sigpending_args *uap)
2056 struct proc *p = td->td_proc;
2060 siglist = p->p_siglist;
2061 SIGSETOR(siglist, td->td_siglist);
2063 SIG2OSIG(siglist, td->td_retval[0]);
2068 u_int32_t sv_handler;
2074 ofreebsd32_sigvec(struct thread *td,
2075 struct ofreebsd32_sigvec_args *uap)
2077 struct sigvec32 vec;
2078 struct sigaction sa, osa, *sap;
2081 if (uap->signum <= 0 || uap->signum >= ONSIG)
2085 error = copyin(uap->nsv, &vec, sizeof(vec));
2088 sa.sa_handler = PTRIN(vec.sv_handler);
2089 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2090 sa.sa_flags = vec.sv_flags;
2091 sa.sa_flags ^= SA_RESTART;
2095 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2096 if (error == 0 && uap->osv != NULL) {
2097 vec.sv_handler = PTROUT(osa.sa_handler);
2098 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2099 vec.sv_flags = osa.sa_flags;
2100 vec.sv_flags &= ~SA_NOCLDWAIT;
2101 vec.sv_flags ^= SA_RESTART;
2102 error = copyout(&vec, uap->osv, sizeof(vec));
2108 ofreebsd32_sigblock(struct thread *td,
2109 struct ofreebsd32_sigblock_args *uap)
2113 OSIG2SIG(uap->mask, set);
2114 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2115 SIG2OSIG(oset, td->td_retval[0]);
2120 ofreebsd32_sigsetmask(struct thread *td,
2121 struct ofreebsd32_sigsetmask_args *uap)
2125 OSIG2SIG(uap->mask, set);
2126 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2127 SIG2OSIG(oset, td->td_retval[0]);
2132 ofreebsd32_sigsuspend(struct thread *td,
2133 struct ofreebsd32_sigsuspend_args *uap)
2137 OSIG2SIG(uap->mask, mask);
2138 return (kern_sigsuspend(td, mask));
2147 ofreebsd32_sigstack(struct thread *td,
2148 struct ofreebsd32_sigstack_args *uap)
2150 struct sigstack32 s32;
2151 struct sigstack nss, oss;
2152 int error = 0, unss;
2154 if (uap->nss != NULL) {
2155 error = copyin(uap->nss, &s32, sizeof(s32));
2158 nss.ss_sp = PTRIN(s32.ss_sp);
2159 CP(s32, nss, ss_onstack);
2164 oss.ss_sp = td->td_sigstk.ss_sp;
2165 oss.ss_onstack = sigonstack(cpu_getstack(td));
2167 td->td_sigstk.ss_sp = nss.ss_sp;
2168 td->td_sigstk.ss_size = 0;
2169 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2170 td->td_pflags |= TDP_ALTSTACK;
2172 if (uap->oss != NULL) {
2173 s32.ss_sp = PTROUT(oss.ss_sp);
2174 CP(oss, s32, ss_onstack);
2175 error = copyout(&s32, uap->oss, sizeof(s32));
2182 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2184 struct timespec32 rmt32, rqt32;
2185 struct timespec rmt, rqt;
2188 error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2192 CP(rqt32, rqt, tv_sec);
2193 CP(rqt32, rqt, tv_nsec);
2196 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2198 error = kern_nanosleep(td, &rqt, &rmt);
2199 if (error && uap->rmtp) {
2202 CP(rmt, rmt32, tv_sec);
2203 CP(rmt, rmt32, tv_nsec);
2205 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2213 freebsd32_clock_gettime(struct thread *td,
2214 struct freebsd32_clock_gettime_args *uap)
2216 struct timespec ats;
2217 struct timespec32 ats32;
2220 error = kern_clock_gettime(td, uap->clock_id, &ats);
2222 CP(ats, ats32, tv_sec);
2223 CP(ats, ats32, tv_nsec);
2224 error = copyout(&ats32, uap->tp, sizeof(ats32));
2230 freebsd32_clock_settime(struct thread *td,
2231 struct freebsd32_clock_settime_args *uap)
2233 struct timespec ats;
2234 struct timespec32 ats32;
2237 error = copyin(uap->tp, &ats32, sizeof(ats32));
2240 CP(ats32, ats, tv_sec);
2241 CP(ats32, ats, tv_nsec);
2243 return (kern_clock_settime(td, uap->clock_id, &ats));
2247 freebsd32_clock_getres(struct thread *td,
2248 struct freebsd32_clock_getres_args *uap)
2251 struct timespec32 ts32;
2254 if (uap->tp == NULL)
2256 error = kern_clock_getres(td, uap->clock_id, &ts);
2258 CP(ts, ts32, tv_sec);
2259 CP(ts, ts32, tv_nsec);
2260 error = copyout(&ts32, uap->tp, sizeof(ts32));
2266 freebsd32_thr_new(struct thread *td,
2267 struct freebsd32_thr_new_args *uap)
2269 struct thr_param32 param32;
2270 struct thr_param param;
2273 if (uap->param_size < 0 ||
2274 uap->param_size > sizeof(struct thr_param32))
2276 bzero(¶m, sizeof(struct thr_param));
2277 bzero(¶m32, sizeof(struct thr_param32));
2278 error = copyin(uap->param, ¶m32, uap->param_size);
2281 param.start_func = PTRIN(param32.start_func);
2282 param.arg = PTRIN(param32.arg);
2283 param.stack_base = PTRIN(param32.stack_base);
2284 param.stack_size = param32.stack_size;
2285 param.tls_base = PTRIN(param32.tls_base);
2286 param.tls_size = param32.tls_size;
2287 param.child_tid = PTRIN(param32.child_tid);
2288 param.parent_tid = PTRIN(param32.parent_tid);
2289 param.flags = param32.flags;
2290 param.rtp = PTRIN(param32.rtp);
2291 param.spare[0] = PTRIN(param32.spare[0]);
2292 param.spare[1] = PTRIN(param32.spare[1]);
2293 param.spare[2] = PTRIN(param32.spare[2]);
2295 return (kern_thr_new(td, ¶m));
2299 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2301 struct timespec32 ts32;
2302 struct timespec ts, *tsp;
2307 if (uap->timeout != NULL) {
2308 error = copyin((const void *)uap->timeout, (void *)&ts32,
2309 sizeof(struct timespec32));
2312 ts.tv_sec = ts32.tv_sec;
2313 ts.tv_nsec = ts32.tv_nsec;
2316 return (kern_thr_suspend(td, tsp));
2320 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2322 bzero(dst, sizeof(*dst));
2323 dst->si_signo = src->si_signo;
2324 dst->si_errno = src->si_errno;
2325 dst->si_code = src->si_code;
2326 dst->si_pid = src->si_pid;
2327 dst->si_uid = src->si_uid;
2328 dst->si_status = src->si_status;
2329 dst->si_addr = (uintptr_t)src->si_addr;
2330 dst->si_value.sigval_int = src->si_value.sival_int;
2331 dst->si_timerid = src->si_timerid;
2332 dst->si_overrun = src->si_overrun;
2336 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2338 struct timespec32 ts32;
2340 struct timespec *timeout;
2343 struct siginfo32 si32;
2347 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2350 ts.tv_sec = ts32.tv_sec;
2351 ts.tv_nsec = ts32.tv_nsec;
2356 error = copyin(uap->set, &set, sizeof(set));
2360 error = kern_sigtimedwait(td, set, &ksi, timeout);
2365 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2366 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2370 td->td_retval[0] = ksi.ksi_signo;
2378 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2381 struct siginfo32 si32;
2385 error = copyin(uap->set, &set, sizeof(set));
2389 error = kern_sigtimedwait(td, set, &ksi, NULL);
2394 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2395 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2398 td->td_retval[0] = ksi.ksi_signo;
2403 freebsd32_cpuset_setid(struct thread *td,
2404 struct freebsd32_cpuset_setid_args *uap)
2406 struct cpuset_setid_args ap;
2408 ap.which = uap->which;
2409 ap.id = PAIR32TO64(id_t,uap->id);
2410 ap.setid = uap->setid;
2412 return (sys_cpuset_setid(td, &ap));
2416 freebsd32_cpuset_getid(struct thread *td,
2417 struct freebsd32_cpuset_getid_args *uap)
2419 struct cpuset_getid_args ap;
2421 ap.level = uap->level;
2422 ap.which = uap->which;
2423 ap.id = PAIR32TO64(id_t,uap->id);
2424 ap.setid = uap->setid;
2426 return (sys_cpuset_getid(td, &ap));
2430 freebsd32_cpuset_getaffinity(struct thread *td,
2431 struct freebsd32_cpuset_getaffinity_args *uap)
2433 struct cpuset_getaffinity_args ap;
2435 ap.level = uap->level;
2436 ap.which = uap->which;
2437 ap.id = PAIR32TO64(id_t,uap->id);
2438 ap.cpusetsize = uap->cpusetsize;
2439 ap.mask = uap->mask;
2441 return (sys_cpuset_getaffinity(td, &ap));
2445 freebsd32_cpuset_setaffinity(struct thread *td,
2446 struct freebsd32_cpuset_setaffinity_args *uap)
2448 struct cpuset_setaffinity_args ap;
2450 ap.level = uap->level;
2451 ap.which = uap->which;
2452 ap.id = PAIR32TO64(id_t,uap->id);
2453 ap.cpusetsize = uap->cpusetsize;
2454 ap.mask = uap->mask;
2456 return (sys_cpuset_setaffinity(td, &ap));
2460 freebsd32_nmount(struct thread *td,
2461 struct freebsd32_nmount_args /* {
2463 unsigned int iovcnt;
2470 AUDIT_ARG_FFLAGS(uap->flags);
2473 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2474 * userspace to set this flag, but we must filter it out if we want
2475 * MNT_UPDATE on the root file system to work.
2476 * MNT_ROOTFS should only be set by the kernel when mounting its
2479 uap->flags &= ~MNT_ROOTFS;
2482 * check that we have an even number of iovec's
2483 * and that we have at least two options.
2485 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2488 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2491 error = vfs_donmount(td, uap->flags, auio);
2499 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2501 struct yyy32 *p32, s32;
2502 struct yyy *p = NULL, s;
2507 error = copyin(uap->zzz, &s32, sizeof(s32));
2513 error = kern_xxx(td, p);
2518 error = copyout(&s32, p32, sizeof(s32));
2525 syscall32_register(int *offset, struct sysent *new_sysent,
2526 struct sysent *old_sysent)
2528 if (*offset == NO_SYSCALL) {
2531 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2532 if (freebsd32_sysent[i].sy_call ==
2533 (sy_call_t *)lkmnosys)
2535 if (i == SYS_MAXSYSCALL)
2538 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2540 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2541 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2544 *old_sysent = freebsd32_sysent[*offset];
2545 freebsd32_sysent[*offset] = *new_sysent;
2550 syscall32_deregister(int *offset, struct sysent *old_sysent)
2554 freebsd32_sysent[*offset] = *old_sysent;
2559 syscall32_module_handler(struct module *mod, int what, void *arg)
2561 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2567 error = syscall32_register(data->offset, data->new_sysent,
2570 /* Leave a mark so we know to safely unload below. */
2571 data->offset = NULL;
2574 ms.intval = *data->offset;
2576 module_setspecific(mod, &ms);
2579 error = data->chainevh(mod, what, data->chainarg);
2583 * MOD_LOAD failed, so just return without calling the
2584 * chained handler since we didn't pass along the MOD_LOAD
2587 if (data->offset == NULL)
2589 if (data->chainevh) {
2590 error = data->chainevh(mod, what, data->chainarg);
2594 error = syscall32_deregister(data->offset, &data->old_sysent);
2599 error = data->chainevh(mod, what, data->chainarg);
2605 syscall32_helper_register(struct syscall_helper_data *sd)
2607 struct syscall_helper_data *sd1;
2610 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2611 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2614 syscall32_helper_unregister(sd);
2617 sd1->registered = 1;
2623 syscall32_helper_unregister(struct syscall_helper_data *sd)
2625 struct syscall_helper_data *sd1;
2627 for (sd1 = sd; sd1->registered != 0; sd1++) {
2628 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2629 sd1->registered = 0;
2635 freebsd32_copyout_strings(struct image_params *imgp)
2639 char *stringp, *destp;
2640 u_int32_t *stack_base;
2641 struct freebsd32_ps_strings *arginfo;
2642 char canary[sizeof(long) * 8];
2643 int32_t pagesizes32[MAXPAGESIZES];
2644 size_t execpath_len;
2648 * Calculate string base and vector table pointers.
2649 * Also deal with signal trampoline code for this exec type.
2651 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2652 execpath_len = strlen(imgp->execpath) + 1;
2655 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2657 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2658 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2661 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2662 roundup(execpath_len, sizeof(char *)) -
2663 roundup(sizeof(canary), sizeof(char *)) -
2664 roundup(sizeof(pagesizes32), sizeof(char *)) -
2665 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2671 copyout(imgp->proc->p_sysent->sv_sigcode,
2672 ((caddr_t)arginfo - szsigcode), szsigcode);
2675 * Copy the image path for the rtld.
2677 if (execpath_len != 0) {
2678 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2679 copyout(imgp->execpath, (void *)imgp->execpathp,
2684 * Prepare the canary for SSP.
2686 arc4rand(canary, sizeof(canary), 0);
2687 imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2689 copyout(canary, (void *)imgp->canary, sizeof(canary));
2690 imgp->canarylen = sizeof(canary);
2693 * Prepare the pagesizes array.
2695 for (i = 0; i < MAXPAGESIZES; i++)
2696 pagesizes32[i] = (uint32_t)pagesizes[i];
2697 imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2698 roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2699 copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2700 imgp->pagesizeslen = sizeof(pagesizes32);
2703 * If we have a valid auxargs ptr, prepare some room
2706 if (imgp->auxargs) {
2708 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2709 * lower compatibility.
2711 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2714 * The '+ 2' is for the null pointers at the end of each of
2715 * the arg and env vector sets,and imgp->auxarg_size is room
2716 * for argument of Runtime loader.
2718 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2719 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2723 * The '+ 2' is for the null pointers at the end of each of
2724 * the arg and env vector sets
2726 vectp = (u_int32_t *)
2727 (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2730 * vectp also becomes our initial stack base
2734 stringp = imgp->args->begin_argv;
2735 argc = imgp->args->argc;
2736 envc = imgp->args->envc;
2738 * Copy out strings - arguments and environment.
2740 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2743 * Fill in "ps_strings" struct for ps, w, etc.
2745 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2746 suword32(&arginfo->ps_nargvstr, argc);
2749 * Fill in argument portion of vector table.
2751 for (; argc > 0; --argc) {
2752 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2753 while (*stringp++ != 0)
2758 /* a null vector table pointer separates the argp's from the envp's */
2759 suword32(vectp++, 0);
2761 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2762 suword32(&arginfo->ps_nenvstr, envc);
2765 * Fill in environment portion of vector table.
2767 for (; envc > 0; --envc) {
2768 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2769 while (*stringp++ != 0)
2774 /* end of vector table is a null pointer */
2777 return ((register_t *)stack_base);
2781 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2783 struct kld_file_stat stat;
2784 struct kld32_file_stat stat32;
2787 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2790 if (version != sizeof(struct kld32_file_stat_1) &&
2791 version != sizeof(struct kld32_file_stat))
2794 error = kern_kldstat(td, uap->fileid, &stat);
2798 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2799 CP(stat, stat32, refs);
2800 CP(stat, stat32, id);
2801 PTROUT_CP(stat, stat32, address);
2802 CP(stat, stat32, size);
2803 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2804 return (copyout(&stat32, uap->stat, version));
2808 freebsd32_posix_fallocate(struct thread *td,
2809 struct freebsd32_posix_fallocate_args *uap)
2812 return (kern_posix_fallocate(td, uap->fd,
2813 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2817 freebsd32_posix_fadvise(struct thread *td,
2818 struct freebsd32_posix_fadvise_args *uap)
2821 return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2822 PAIR32TO64(off_t, uap->len), uap->advice));