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_kern.h>
91 #include <vm/vm_param.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_extern.h>
97 #include <machine/cpu.h>
98 #include <machine/elf.h>
100 #include <security/audit/audit.h>
102 #include <compat/freebsd32/freebsd32_util.h>
103 #include <compat/freebsd32/freebsd32.h>
104 #include <compat/freebsd32/freebsd32_ipc.h>
105 #include <compat/freebsd32/freebsd32_signal.h>
106 #include <compat/freebsd32/freebsd32_proto.h>
108 CTASSERT(sizeof(struct timeval32) == 8);
109 CTASSERT(sizeof(struct timespec32) == 8);
110 CTASSERT(sizeof(struct itimerval32) == 16);
111 CTASSERT(sizeof(struct statfs32) == 256);
112 CTASSERT(sizeof(struct rusage32) == 72);
113 CTASSERT(sizeof(struct sigaltstack32) == 12);
114 CTASSERT(sizeof(struct kevent32) == 20);
115 CTASSERT(sizeof(struct iovec32) == 8);
116 CTASSERT(sizeof(struct msghdr32) == 28);
117 CTASSERT(sizeof(struct stat32) == 96);
118 CTASSERT(sizeof(struct sigaction32) == 24);
120 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
121 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
123 #if BYTE_ORDER == BIG_ENDIAN
124 #define PAIR32TO64(type, name) ((name ## 2) | ((type)(name ## 1) << 32))
128 #define PAIR32TO64(type, name) ((name ## 1) | ((type)(name ## 2) << 32))
134 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
137 TV_CP(*s, *s32, ru_utime);
138 TV_CP(*s, *s32, ru_stime);
139 CP(*s, *s32, ru_maxrss);
140 CP(*s, *s32, ru_ixrss);
141 CP(*s, *s32, ru_idrss);
142 CP(*s, *s32, ru_isrss);
143 CP(*s, *s32, ru_minflt);
144 CP(*s, *s32, ru_majflt);
145 CP(*s, *s32, ru_nswap);
146 CP(*s, *s32, ru_inblock);
147 CP(*s, *s32, ru_oublock);
148 CP(*s, *s32, ru_msgsnd);
149 CP(*s, *s32, ru_msgrcv);
150 CP(*s, *s32, ru_nsignals);
151 CP(*s, *s32, ru_nvcsw);
152 CP(*s, *s32, ru_nivcsw);
156 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
159 struct rusage32 ru32;
160 struct rusage ru, *rup;
162 if (uap->rusage != NULL)
166 error = kern_wait(td, uap->pid, &status, uap->options, rup);
169 if (uap->status != NULL)
170 error = copyout(&status, uap->status, sizeof(status));
171 if (uap->rusage != NULL && error == 0) {
172 freebsd32_rusage_out(&ru, &ru32);
173 error = copyout(&ru32, uap->rusage, sizeof(ru32));
178 #ifdef COMPAT_FREEBSD4
180 copy_statfs(struct statfs *in, struct statfs32 *out)
183 statfs_scale_blocks(in, INT32_MAX);
184 bzero(out, sizeof(*out));
185 CP(*in, *out, f_bsize);
186 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
187 CP(*in, *out, f_blocks);
188 CP(*in, *out, f_bfree);
189 CP(*in, *out, f_bavail);
190 out->f_files = MIN(in->f_files, INT32_MAX);
191 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
192 CP(*in, *out, f_fsid);
193 CP(*in, *out, f_owner);
194 CP(*in, *out, f_type);
195 CP(*in, *out, f_flags);
196 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
197 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
198 strlcpy(out->f_fstypename,
199 in->f_fstypename, MFSNAMELEN);
200 strlcpy(out->f_mntonname,
201 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
202 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
203 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
204 strlcpy(out->f_mntfromname,
205 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
209 #ifdef COMPAT_FREEBSD4
211 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
213 struct statfs *buf, *sp;
214 struct statfs32 stat32;
218 count = uap->bufsize / sizeof(struct statfs32);
219 size = count * sizeof(struct statfs);
220 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
222 count = td->td_retval[0];
224 while (count > 0 && error == 0) {
225 copy_statfs(sp, &stat32);
226 error = copyout(&stat32, uap->buf, sizeof(stat32));
238 freebsd32_sigaltstack(struct thread *td,
239 struct freebsd32_sigaltstack_args *uap)
241 struct sigaltstack32 s32;
242 struct sigaltstack ss, oss, *ssp;
245 if (uap->ss != NULL) {
246 error = copyin(uap->ss, &s32, sizeof(s32));
249 PTRIN_CP(s32, ss, ss_sp);
250 CP(s32, ss, ss_size);
251 CP(s32, ss, ss_flags);
255 error = kern_sigaltstack(td, ssp, &oss);
256 if (error == 0 && uap->oss != NULL) {
257 PTROUT_CP(oss, s32, ss_sp);
258 CP(oss, s32, ss_size);
259 CP(oss, s32, ss_flags);
260 error = copyout(&s32, uap->oss, sizeof(s32));
266 * Custom version of exec_copyin_args() so that we can translate
270 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
271 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
278 bzero(args, sizeof(*args));
283 * Allocate temporary demand zeroed space for argument and
284 * environment strings
286 args->buf = (char *) kmem_alloc_wait(exec_map,
287 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
288 if (args->buf == NULL)
290 args->begin_argv = args->buf;
291 args->endp = args->begin_argv;
292 args->stringspace = ARG_MAX;
295 * Copy the file name.
298 args->fname = args->buf + ARG_MAX;
299 error = (segflg == UIO_SYSSPACE) ?
300 copystr(fname, args->fname, PATH_MAX, &length) :
301 copyinstr(fname, args->fname, PATH_MAX, &length);
308 * extract arguments first
312 error = copyin(p32++, &arg, sizeof(arg));
318 error = copyinstr(argp, args->endp, args->stringspace, &length);
320 if (error == ENAMETOOLONG)
324 args->stringspace -= length;
325 args->endp += length;
329 args->begin_envv = args->endp;
332 * extract environment strings
337 error = copyin(p32++, &arg, sizeof(arg));
343 error = copyinstr(envp, args->endp, args->stringspace,
346 if (error == ENAMETOOLONG)
350 args->stringspace -= length;
351 args->endp += length;
359 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
360 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
366 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
368 struct image_args eargs;
371 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
372 uap->argv, uap->envv);
374 error = kern_execve(td, &eargs, NULL);
379 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
381 struct image_args eargs;
384 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
385 uap->argv, uap->envv);
388 error = kern_execve(td, &eargs, NULL);
395 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
396 int prot, int fd, off_t pos)
399 vm_map_entry_t entry;
402 map = &td->td_proc->p_vmspace->vm_map;
404 prot |= VM_PROT_WRITE;
406 if (vm_map_lookup_entry(map, start, &entry)) {
407 if ((entry->protection & prot) != prot) {
408 rv = vm_map_protect(map,
411 entry->protection | prot,
413 if (rv != KERN_SUCCESS)
417 vm_offset_t addr = trunc_page(start);
418 rv = vm_map_find(map, 0, 0,
419 &addr, PAGE_SIZE, FALSE, prot,
421 if (rv != KERN_SUCCESS)
428 r.buf = (void *) start;
429 r.nbyte = end - start;
431 return (pread(td, &r));
433 while (start < end) {
434 subyte((void *) start, 0);
443 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
446 vm_offset_t addr = (vm_offset_t) uap->addr;
447 vm_size_t len = uap->len;
448 int prot = uap->prot;
449 int flags = uap->flags;
451 off_t pos = PAIR32TO64(off_t,uap->pos);
457 * Attempt to handle page size hassles.
459 pageoff = (pos & PAGE_MASK);
460 if (flags & MAP_FIXED) {
461 vm_offset_t start, end;
465 if (start != trunc_page(start)) {
466 error = freebsd32_mmap_partial(td, start,
467 round_page(start), prot,
470 pos += round_page(start) - start;
471 start = round_page(start);
473 if (end != round_page(end)) {
474 vm_offset_t t = trunc_page(end);
475 error = freebsd32_mmap_partial(td, t, end,
478 end = trunc_page(end);
480 if (end > start && fd != -1 && (pos & PAGE_MASK)) {
482 * We can't map this region at all. The specified
483 * address doesn't have the same alignment as the file
484 * position. Fake the mapping by simply reading the
485 * entire region into memory. First we need to make
486 * sure the region exists.
492 prot |= VM_PROT_WRITE;
493 map = &td->td_proc->p_vmspace->vm_map;
494 rv = vm_map_remove(map, start, end);
495 if (rv != KERN_SUCCESS)
497 rv = vm_map_find(map, 0, 0,
498 &start, end - start, FALSE,
499 prot, VM_PROT_ALL, 0);
500 if (rv != KERN_SUCCESS)
503 r.buf = (void *) start;
504 r.nbyte = end - start;
506 error = pread(td, &r);
510 td->td_retval[0] = addr;
515 * After dealing with the ragged ends, there
516 * might be none left.
518 td->td_retval[0] = addr;
526 ap.addr = (void *) addr;
533 return (mmap(td, &ap));
536 #ifdef COMPAT_FREEBSD6
538 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
540 struct freebsd32_mmap_args ap;
545 ap.flags = uap->flags;
550 return (freebsd32_mmap(td, &ap));
555 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
557 struct itimerval itv, oitv, *itvp;
558 struct itimerval32 i32;
561 if (uap->itv != NULL) {
562 error = copyin(uap->itv, &i32, sizeof(i32));
565 TV_CP(i32, itv, it_interval);
566 TV_CP(i32, itv, it_value);
570 error = kern_setitimer(td, uap->which, itvp, &oitv);
571 if (error || uap->oitv == NULL)
573 TV_CP(oitv, i32, it_interval);
574 TV_CP(oitv, i32, it_value);
575 return (copyout(&i32, uap->oitv, sizeof(i32)));
579 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
581 struct itimerval itv;
582 struct itimerval32 i32;
585 error = kern_getitimer(td, uap->which, &itv);
586 if (error || uap->itv == NULL)
588 TV_CP(itv, i32, it_interval);
589 TV_CP(itv, i32, it_value);
590 return (copyout(&i32, uap->itv, sizeof(i32)));
594 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
596 struct timeval32 tv32;
597 struct timeval tv, *tvp;
600 if (uap->tv != NULL) {
601 error = copyin(uap->tv, &tv32, sizeof(tv32));
604 CP(tv32, tv, tv_sec);
605 CP(tv32, tv, tv_usec);
610 * XXX Do pointers need PTRIN()?
612 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
613 sizeof(int32_t) * 8));
617 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
619 struct timespec32 ts32;
621 struct timeval tv, *tvp;
625 if (uap->ts != NULL) {
626 error = copyin(uap->ts, &ts32, sizeof(ts32));
629 CP(ts32, ts, tv_sec);
630 CP(ts32, ts, tv_nsec);
631 TIMESPEC_TO_TIMEVAL(&tv, &ts);
635 if (uap->sm != NULL) {
636 error = copyin(uap->sm, &set, sizeof(set));
643 * XXX Do pointers need PTRIN()?
645 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
646 uset, sizeof(int32_t) * 8);
651 * Copy 'count' items into the destination list pointed to by uap->eventlist.
654 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
656 struct freebsd32_kevent_args *uap;
657 struct kevent32 ks32[KQ_NEVENTS];
660 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
661 uap = (struct freebsd32_kevent_args *)arg;
663 for (i = 0; i < count; i++) {
664 CP(kevp[i], ks32[i], ident);
665 CP(kevp[i], ks32[i], filter);
666 CP(kevp[i], ks32[i], flags);
667 CP(kevp[i], ks32[i], fflags);
668 CP(kevp[i], ks32[i], data);
669 PTROUT_CP(kevp[i], ks32[i], udata);
671 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
673 uap->eventlist += count;
678 * Copy 'count' items from the list pointed to by uap->changelist.
681 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
683 struct freebsd32_kevent_args *uap;
684 struct kevent32 ks32[KQ_NEVENTS];
687 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
688 uap = (struct freebsd32_kevent_args *)arg;
690 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
693 uap->changelist += count;
695 for (i = 0; i < count; i++) {
696 CP(ks32[i], kevp[i], ident);
697 CP(ks32[i], kevp[i], filter);
698 CP(ks32[i], kevp[i], flags);
699 CP(ks32[i], kevp[i], fflags);
700 CP(ks32[i], kevp[i], data);
701 PTRIN_CP(ks32[i], kevp[i], udata);
708 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
710 struct timespec32 ts32;
711 struct timespec ts, *tsp;
712 struct kevent_copyops k_ops = { uap,
713 freebsd32_kevent_copyout,
714 freebsd32_kevent_copyin};
719 error = copyin(uap->timeout, &ts32, sizeof(ts32));
722 CP(ts32, ts, tv_sec);
723 CP(ts32, ts, tv_nsec);
727 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
733 freebsd32_gettimeofday(struct thread *td,
734 struct freebsd32_gettimeofday_args *uap)
737 struct timeval32 atv32;
743 CP(atv, atv32, tv_sec);
744 CP(atv, atv32, tv_usec);
745 error = copyout(&atv32, uap->tp, sizeof (atv32));
747 if (error == 0 && uap->tzp != NULL) {
748 rtz.tz_minuteswest = tz_minuteswest;
749 rtz.tz_dsttime = tz_dsttime;
750 error = copyout(&rtz, uap->tzp, sizeof (rtz));
756 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
762 error = kern_getrusage(td, uap->who, &s);
765 if (uap->rusage != NULL) {
766 freebsd32_rusage_out(&s, &s32);
767 error = copyout(&s32, uap->rusage, sizeof(s32));
773 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
775 struct iovec32 iov32;
782 if (iovcnt > UIO_MAXIOV)
784 iovlen = iovcnt * sizeof(struct iovec);
785 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
786 iov = (struct iovec *)(uio + 1);
787 for (i = 0; i < iovcnt; i++) {
788 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
793 iov[i].iov_base = PTRIN(iov32.iov_base);
794 iov[i].iov_len = iov32.iov_len;
797 uio->uio_iovcnt = iovcnt;
798 uio->uio_segflg = UIO_USERSPACE;
799 uio->uio_offset = -1;
801 for (i = 0; i < iovcnt; i++) {
802 if (iov->iov_len > INT_MAX - uio->uio_resid) {
806 uio->uio_resid += iov->iov_len;
814 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
819 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
822 error = kern_readv(td, uap->fd, auio);
828 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
833 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
836 error = kern_writev(td, uap->fd, auio);
842 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
847 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
850 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
856 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
861 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
864 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
870 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
873 struct iovec32 iov32;
879 if (iovcnt > UIO_MAXIOV)
881 iovlen = iovcnt * sizeof(struct iovec);
882 iov = malloc(iovlen, M_IOV, M_WAITOK);
883 for (i = 0; i < iovcnt; i++) {
884 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
889 iov[i].iov_base = PTRIN(iov32.iov_base);
890 iov[i].iov_len = iov32.iov_len;
897 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
902 error = copyin(msg32, &m32, sizeof(m32));
905 msg->msg_name = PTRIN(m32.msg_name);
906 msg->msg_namelen = m32.msg_namelen;
907 msg->msg_iov = PTRIN(m32.msg_iov);
908 msg->msg_iovlen = m32.msg_iovlen;
909 msg->msg_control = PTRIN(m32.msg_control);
910 msg->msg_controllen = m32.msg_controllen;
911 msg->msg_flags = m32.msg_flags;
916 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
921 m32.msg_name = PTROUT(msg->msg_name);
922 m32.msg_namelen = msg->msg_namelen;
923 m32.msg_iov = PTROUT(msg->msg_iov);
924 m32.msg_iovlen = msg->msg_iovlen;
925 m32.msg_control = PTROUT(msg->msg_control);
926 m32.msg_controllen = msg->msg_controllen;
927 m32.msg_flags = msg->msg_flags;
928 error = copyout(&m32, msg32, sizeof(m32));
932 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
933 #define FREEBSD32_ALIGN(p) \
934 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
935 #define FREEBSD32_CMSG_SPACE(l) \
936 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
938 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
939 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
941 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
945 socklen_t clen, datalen;
948 int len, maxlen, copylen;
952 len = msg->msg_controllen;
953 maxlen = msg->msg_controllen;
954 msg->msg_controllen = 0;
957 ctlbuf = msg->msg_control;
959 while (m && len > 0) {
960 cm = mtod(m, struct cmsghdr *);
965 if (sizeof(struct cmsghdr) > clen ||
966 cm->cmsg_len > clen) {
971 data = CMSG_DATA(cm);
972 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
974 /* Adjust message length */
975 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
980 copylen = sizeof(struct cmsghdr);
982 msg->msg_flags |= MSG_CTRUNC;
986 error = copyout(cm,ctlbuf,copylen);
990 ctlbuf += FREEBSD32_ALIGN(copylen);
991 len -= FREEBSD32_ALIGN(copylen);
999 msg->msg_flags |= MSG_CTRUNC;
1003 error = copyout(data,ctlbuf,copylen);
1007 ctlbuf += FREEBSD32_ALIGN(copylen);
1008 len -= FREEBSD32_ALIGN(copylen);
1010 if (CMSG_SPACE(datalen) < clen) {
1011 clen -= CMSG_SPACE(datalen);
1012 cm = (struct cmsghdr *)
1013 ((caddr_t)cm + CMSG_SPACE(datalen));
1022 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1030 freebsd32_recvmsg(td, uap)
1032 struct freebsd32_recvmsg_args /* {
1034 struct msghdr32 *msg;
1039 struct msghdr32 m32;
1040 struct iovec *uiov, *iov;
1041 struct mbuf *control = NULL;
1042 struct mbuf **controlp;
1045 error = copyin(uap->msg, &m32, sizeof(m32));
1048 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1051 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1055 msg.msg_flags = uap->flags;
1059 controlp = (msg.msg_control != NULL) ? &control : NULL;
1060 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1064 if (control != NULL)
1065 error = freebsd32_copy_msg_out(&msg, control);
1067 msg.msg_controllen = 0;
1070 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1074 if (control != NULL)
1082 freebsd32_convert_msg_in(struct mbuf **controlp)
1084 struct mbuf *control = *controlp;
1085 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1087 socklen_t clen = control->m_len, datalen;
1093 while (cm != NULL) {
1094 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1099 data = FREEBSD32_CMSG_DATA(cm);
1100 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1102 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1104 controlp = &(*controlp)->m_next;
1106 if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1107 clen -= FREEBSD32_CMSG_SPACE(datalen);
1108 cm = (struct cmsghdr *)
1109 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1122 freebsd32_sendmsg(struct thread *td,
1123 struct freebsd32_sendmsg_args *uap)
1126 struct msghdr32 m32;
1128 struct mbuf *control = NULL;
1129 struct sockaddr *to = NULL;
1132 error = copyin(uap->msg, &m32, sizeof(m32));
1135 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1138 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1143 if (msg.msg_name != NULL) {
1144 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1152 if (msg.msg_control) {
1153 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1158 error = sockargs(&control, msg.msg_control,
1159 msg.msg_controllen, MT_CONTROL);
1163 error = freebsd32_convert_msg_in(&control);
1168 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1179 freebsd32_recvfrom(struct thread *td,
1180 struct freebsd32_recvfrom_args *uap)
1186 if (uap->fromlenaddr) {
1187 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1188 sizeof(msg.msg_namelen));
1192 msg.msg_namelen = 0;
1195 msg.msg_name = PTRIN(uap->from);
1196 msg.msg_iov = &aiov;
1198 aiov.iov_base = PTRIN(uap->buf);
1199 aiov.iov_len = uap->len;
1200 msg.msg_control = NULL;
1201 msg.msg_flags = uap->flags;
1202 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1203 if (error == 0 && uap->fromlenaddr)
1204 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1205 sizeof (msg.msg_namelen));
1210 freebsd32_settimeofday(struct thread *td,
1211 struct freebsd32_settimeofday_args *uap)
1213 struct timeval32 tv32;
1214 struct timeval tv, *tvp;
1215 struct timezone tz, *tzp;
1219 error = copyin(uap->tv, &tv32, sizeof(tv32));
1222 CP(tv32, tv, tv_sec);
1223 CP(tv32, tv, tv_usec);
1228 error = copyin(uap->tzp, &tz, sizeof(tz));
1234 return (kern_settimeofday(td, tvp, tzp));
1238 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1240 struct timeval32 s32[2];
1241 struct timeval s[2], *sp;
1244 if (uap->tptr != NULL) {
1245 error = copyin(uap->tptr, s32, sizeof(s32));
1248 CP(s32[0], s[0], tv_sec);
1249 CP(s32[0], s[0], tv_usec);
1250 CP(s32[1], s[1], tv_sec);
1251 CP(s32[1], s[1], tv_usec);
1255 return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1259 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1261 struct timeval32 s32[2];
1262 struct timeval s[2], *sp;
1265 if (uap->tptr != NULL) {
1266 error = copyin(uap->tptr, s32, sizeof(s32));
1269 CP(s32[0], s[0], tv_sec);
1270 CP(s32[0], s[0], tv_usec);
1271 CP(s32[1], s[1], tv_sec);
1272 CP(s32[1], s[1], tv_usec);
1276 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1280 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1282 struct timeval32 s32[2];
1283 struct timeval s[2], *sp;
1286 if (uap->tptr != NULL) {
1287 error = copyin(uap->tptr, s32, sizeof(s32));
1290 CP(s32[0], s[0], tv_sec);
1291 CP(s32[0], s[0], tv_usec);
1292 CP(s32[1], s[1], tv_sec);
1293 CP(s32[1], s[1], tv_usec);
1297 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1301 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1303 struct timeval32 s32[2];
1304 struct timeval s[2], *sp;
1307 if (uap->times != NULL) {
1308 error = copyin(uap->times, s32, sizeof(s32));
1311 CP(s32[0], s[0], tv_sec);
1312 CP(s32[0], s[0], tv_usec);
1313 CP(s32[1], s[1], tv_sec);
1314 CP(s32[1], s[1], tv_usec);
1318 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1323 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1325 struct timeval32 tv32;
1326 struct timeval delta, olddelta, *deltap;
1330 error = copyin(uap->delta, &tv32, sizeof(tv32));
1333 CP(tv32, delta, tv_sec);
1334 CP(tv32, delta, tv_usec);
1338 error = kern_adjtime(td, deltap, &olddelta);
1339 if (uap->olddelta && error == 0) {
1340 CP(olddelta, tv32, tv_sec);
1341 CP(olddelta, tv32, tv_usec);
1342 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1347 #ifdef COMPAT_FREEBSD4
1349 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1351 struct statfs32 s32;
1355 error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1358 copy_statfs(&s, &s32);
1359 return (copyout(&s32, uap->buf, sizeof(s32)));
1363 #ifdef COMPAT_FREEBSD4
1365 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1367 struct statfs32 s32;
1371 error = kern_fstatfs(td, uap->fd, &s);
1374 copy_statfs(&s, &s32);
1375 return (copyout(&s32, uap->buf, sizeof(s32)));
1379 #ifdef COMPAT_FREEBSD4
1381 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1383 struct statfs32 s32;
1388 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1390 error = kern_fhstatfs(td, fh, &s);
1393 copy_statfs(&s, &s32);
1394 return (copyout(&s32, uap->buf, sizeof(s32)));
1399 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1401 struct pread_args ap;
1405 ap.nbyte = uap->nbyte;
1406 ap.offset = PAIR32TO64(off_t,uap->offset);
1407 return (pread(td, &ap));
1411 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1413 struct pwrite_args ap;
1417 ap.nbyte = uap->nbyte;
1418 ap.offset = PAIR32TO64(off_t,uap->offset);
1419 return (pwrite(td, &ap));
1423 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1426 struct lseek_args ap;
1430 ap.offset = PAIR32TO64(off_t,uap->offset);
1431 ap.whence = uap->whence;
1432 error = lseek(td, &ap);
1433 /* Expand the quad return into two parts for eax and edx */
1434 pos = *(off_t *)(td->td_retval);
1435 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1436 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1441 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1443 struct truncate_args ap;
1445 ap.path = uap->path;
1446 ap.length = PAIR32TO64(off_t,uap->length);
1447 return (truncate(td, &ap));
1451 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1453 struct ftruncate_args ap;
1456 ap.length = PAIR32TO64(off_t,uap->length);
1457 return (ftruncate(td, &ap));
1461 freebsd32_getdirentries(struct thread *td,
1462 struct freebsd32_getdirentries_args *uap)
1468 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
1471 if (uap->basep != NULL) {
1473 error = copyout(&base32, uap->basep, sizeof(int32_t));
1478 #ifdef COMPAT_FREEBSD6
1479 /* versions with the 'int pad' argument */
1481 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1483 struct pread_args ap;
1487 ap.nbyte = uap->nbyte;
1488 ap.offset = PAIR32TO64(off_t,uap->offset);
1489 return (pread(td, &ap));
1493 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1495 struct pwrite_args ap;
1499 ap.nbyte = uap->nbyte;
1500 ap.offset = PAIR32TO64(off_t,uap->offset);
1501 return (pwrite(td, &ap));
1505 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1508 struct lseek_args ap;
1512 ap.offset = PAIR32TO64(off_t,uap->offset);
1513 ap.whence = uap->whence;
1514 error = lseek(td, &ap);
1515 /* Expand the quad return into two parts for eax and edx */
1516 pos = *(off_t *)(td->td_retval);
1517 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1518 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1523 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1525 struct truncate_args ap;
1527 ap.path = uap->path;
1528 ap.length = PAIR32TO64(off_t,uap->length);
1529 return (truncate(td, &ap));
1533 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1535 struct ftruncate_args ap;
1538 ap.length = PAIR32TO64(off_t,uap->length);
1539 return (ftruncate(td, &ap));
1541 #endif /* COMPAT_FREEBSD6 */
1551 freebsd32_do_sendfile(struct thread *td,
1552 struct freebsd32_sendfile_args *uap, int compat)
1554 struct sendfile_args ap;
1555 struct sf_hdtr32 hdtr32;
1556 struct sf_hdtr hdtr;
1557 struct uio *hdr_uio, *trl_uio;
1558 struct iovec32 *iov32;
1561 hdr_uio = trl_uio = NULL;
1565 ap.offset = PAIR32TO64(off_t,uap->offset);
1566 ap.nbytes = uap->nbytes;
1567 ap.hdtr = (struct sf_hdtr *)uap->hdtr; /* XXX not used */
1568 ap.sbytes = uap->sbytes;
1569 ap.flags = uap->flags;
1571 if (uap->hdtr != NULL) {
1572 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1575 PTRIN_CP(hdtr32, hdtr, headers);
1576 CP(hdtr32, hdtr, hdr_cnt);
1577 PTRIN_CP(hdtr32, hdtr, trailers);
1578 CP(hdtr32, hdtr, trl_cnt);
1580 if (hdtr.headers != NULL) {
1581 iov32 = PTRIN(hdtr32.headers);
1582 error = freebsd32_copyinuio(iov32,
1583 hdtr32.hdr_cnt, &hdr_uio);
1587 if (hdtr.trailers != NULL) {
1588 iov32 = PTRIN(hdtr32.trailers);
1589 error = freebsd32_copyinuio(iov32,
1590 hdtr32.trl_cnt, &trl_uio);
1596 error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1599 free(hdr_uio, M_IOV);
1601 free(trl_uio, M_IOV);
1605 #ifdef COMPAT_FREEBSD4
1607 freebsd4_freebsd32_sendfile(struct thread *td,
1608 struct freebsd4_freebsd32_sendfile_args *uap)
1610 return (freebsd32_do_sendfile(td,
1611 (struct freebsd32_sendfile_args *)uap, 1));
1616 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1619 return (freebsd32_do_sendfile(td, uap, 0));
1623 copy_stat(struct stat *in, struct stat32 *out)
1626 CP(*in, *out, st_dev);
1627 CP(*in, *out, st_ino);
1628 CP(*in, *out, st_mode);
1629 CP(*in, *out, st_nlink);
1630 CP(*in, *out, st_uid);
1631 CP(*in, *out, st_gid);
1632 CP(*in, *out, st_rdev);
1633 TS_CP(*in, *out, st_atimespec);
1634 TS_CP(*in, *out, st_mtimespec);
1635 TS_CP(*in, *out, st_ctimespec);
1636 CP(*in, *out, st_size);
1637 CP(*in, *out, st_blocks);
1638 CP(*in, *out, st_blksize);
1639 CP(*in, *out, st_flags);
1640 CP(*in, *out, st_gen);
1641 TS_CP(*in, *out, st_birthtimespec);
1645 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1651 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1654 copy_stat(&sb, &sb32);
1655 error = copyout(&sb32, uap->ub, sizeof (sb32));
1660 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1666 error = kern_fstat(td, uap->fd, &ub);
1669 copy_stat(&ub, &ub32);
1670 error = copyout(&ub32, uap->ub, sizeof(ub32));
1675 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1681 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1684 copy_stat(&ub, &ub32);
1685 error = copyout(&ub32, uap->buf, sizeof(ub32));
1690 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1696 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1699 copy_stat(&sb, &sb32);
1700 error = copyout(&sb32, uap->ub, sizeof (sb32));
1708 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1710 int error, name[CTL_MAXNAME];
1713 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1715 error = copyin(uap->name, name, uap->namelen * sizeof(int));
1719 oldlen = fuword32(uap->oldlenp);
1722 error = userland_sysctl(td, name, uap->namelen,
1723 uap->old, &oldlen, 1,
1724 uap->new, uap->newlen, &j, SCTL_MASK32);
1725 if (error && error != ENOMEM)
1728 suword32(uap->oldlenp, j);
1733 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1739 error = copyin(uap->jail, &version, sizeof(uint32_t));
1746 /* FreeBSD single IPv4 jails. */
1747 struct jail32_v0 j32_v0;
1749 bzero(&j, sizeof(struct jail));
1750 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1753 CP(j32_v0, j, version);
1754 PTRIN_CP(j32_v0, j, path);
1755 PTRIN_CP(j32_v0, j, hostname);
1756 j.ip4s = j32_v0.ip_number;
1762 * Version 1 was used by multi-IPv4 jail implementations
1763 * that never made it into the official kernel.
1767 case 2: /* JAIL_API_VERSION */
1769 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1772 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1775 CP(j32, j, version);
1776 PTRIN_CP(j32, j, path);
1777 PTRIN_CP(j32, j, hostname);
1778 PTRIN_CP(j32, j, jailname);
1781 PTRIN_CP(j32, j, ip4);
1782 PTRIN_CP(j32, j, ip6);
1787 /* Sci-Fi jails are not supported, sorry. */
1790 return (kern_jail(td, &j));
1794 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1799 /* Check that we have an even number of iovecs. */
1800 if (uap->iovcnt & 1)
1803 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1806 error = kern_jail_set(td, auio, uap->flags);
1812 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1814 struct iovec32 iov32;
1818 /* Check that we have an even number of iovecs. */
1819 if (uap->iovcnt & 1)
1822 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1825 error = kern_jail_get(td, auio, uap->flags);
1827 for (i = 0; i < uap->iovcnt; i++) {
1828 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1829 CP(auio->uio_iov[i], iov32, iov_len);
1830 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1839 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1841 struct sigaction32 s32;
1842 struct sigaction sa, osa, *sap;
1846 error = copyin(uap->act, &s32, sizeof(s32));
1849 sa.sa_handler = PTRIN(s32.sa_u);
1850 CP(s32, sa, sa_flags);
1851 CP(s32, sa, sa_mask);
1855 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1856 if (error == 0 && uap->oact != NULL) {
1857 s32.sa_u = PTROUT(osa.sa_handler);
1858 CP(osa, s32, sa_flags);
1859 CP(osa, s32, sa_mask);
1860 error = copyout(&s32, uap->oact, sizeof(s32));
1865 #ifdef COMPAT_FREEBSD4
1867 freebsd4_freebsd32_sigaction(struct thread *td,
1868 struct freebsd4_freebsd32_sigaction_args *uap)
1870 struct sigaction32 s32;
1871 struct sigaction sa, osa, *sap;
1875 error = copyin(uap->act, &s32, sizeof(s32));
1878 sa.sa_handler = PTRIN(s32.sa_u);
1879 CP(s32, sa, sa_flags);
1880 CP(s32, sa, sa_mask);
1884 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
1885 if (error == 0 && uap->oact != NULL) {
1886 s32.sa_u = PTROUT(osa.sa_handler);
1887 CP(osa, s32, sa_flags);
1888 CP(osa, s32, sa_mask);
1889 error = copyout(&s32, uap->oact, sizeof(s32));
1896 struct osigaction32 {
1905 ofreebsd32_sigaction(struct thread *td,
1906 struct ofreebsd32_sigaction_args *uap)
1908 struct osigaction32 s32;
1909 struct sigaction sa, osa, *sap;
1912 if (uap->signum <= 0 || uap->signum >= ONSIG)
1916 error = copyin(uap->nsa, &s32, sizeof(s32));
1919 sa.sa_handler = PTRIN(s32.sa_u);
1920 CP(s32, sa, sa_flags);
1921 OSIG2SIG(s32.sa_mask, sa.sa_mask);
1925 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
1926 if (error == 0 && uap->osa != NULL) {
1927 s32.sa_u = PTROUT(osa.sa_handler);
1928 CP(osa, s32, sa_flags);
1929 SIG2OSIG(osa.sa_mask, s32.sa_mask);
1930 error = copyout(&s32, uap->osa, sizeof(s32));
1936 ofreebsd32_sigprocmask(struct thread *td,
1937 struct ofreebsd32_sigprocmask_args *uap)
1942 OSIG2SIG(uap->mask, set);
1943 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
1944 SIG2OSIG(oset, td->td_retval[0]);
1949 ofreebsd32_sigpending(struct thread *td,
1950 struct ofreebsd32_sigpending_args *uap)
1952 struct proc *p = td->td_proc;
1956 siglist = p->p_siglist;
1957 SIGSETOR(siglist, td->td_siglist);
1959 SIG2OSIG(siglist, td->td_retval[0]);
1964 u_int32_t sv_handler;
1970 ofreebsd32_sigvec(struct thread *td,
1971 struct ofreebsd32_sigvec_args *uap)
1973 struct sigvec32 vec;
1974 struct sigaction sa, osa, *sap;
1977 if (uap->signum <= 0 || uap->signum >= ONSIG)
1981 error = copyin(uap->nsv, &vec, sizeof(vec));
1984 sa.sa_handler = PTRIN(vec.sv_handler);
1985 OSIG2SIG(vec.sv_mask, sa.sa_mask);
1986 sa.sa_flags = vec.sv_flags;
1987 sa.sa_flags ^= SA_RESTART;
1991 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
1992 if (error == 0 && uap->osv != NULL) {
1993 vec.sv_handler = PTROUT(osa.sa_handler);
1994 SIG2OSIG(osa.sa_mask, vec.sv_mask);
1995 vec.sv_flags = osa.sa_flags;
1996 vec.sv_flags &= ~SA_NOCLDWAIT;
1997 vec.sv_flags ^= SA_RESTART;
1998 error = copyout(&vec, uap->osv, sizeof(vec));
2004 ofreebsd32_sigblock(struct thread *td,
2005 struct ofreebsd32_sigblock_args *uap)
2009 OSIG2SIG(uap->mask, set);
2010 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2011 SIG2OSIG(oset, td->td_retval[0]);
2016 ofreebsd32_sigsetmask(struct thread *td,
2017 struct ofreebsd32_sigsetmask_args *uap)
2021 OSIG2SIG(uap->mask, set);
2022 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2023 SIG2OSIG(oset, td->td_retval[0]);
2028 ofreebsd32_sigsuspend(struct thread *td,
2029 struct ofreebsd32_sigsuspend_args *uap)
2033 OSIG2SIG(uap->mask, mask);
2034 return (kern_sigsuspend(td, mask));
2043 ofreebsd32_sigstack(struct thread *td,
2044 struct ofreebsd32_sigstack_args *uap)
2046 struct sigstack32 s32;
2047 struct sigstack nss, oss;
2048 int error = 0, unss;
2050 if (uap->nss != NULL) {
2051 error = copyin(uap->nss, &s32, sizeof(s32));
2054 nss.ss_sp = PTRIN(s32.ss_sp);
2055 CP(s32, nss, ss_onstack);
2060 oss.ss_sp = td->td_sigstk.ss_sp;
2061 oss.ss_onstack = sigonstack(cpu_getstack(td));
2063 td->td_sigstk.ss_sp = nss.ss_sp;
2064 td->td_sigstk.ss_size = 0;
2065 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2066 td->td_pflags |= TDP_ALTSTACK;
2068 if (uap->oss != NULL) {
2069 s32.ss_sp = PTROUT(oss.ss_sp);
2070 CP(oss, s32, ss_onstack);
2071 error = copyout(&s32, uap->oss, sizeof(s32));
2078 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2080 struct timespec32 rmt32, rqt32;
2081 struct timespec rmt, rqt;
2084 error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2088 CP(rqt32, rqt, tv_sec);
2089 CP(rqt32, rqt, tv_nsec);
2092 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2094 error = kern_nanosleep(td, &rqt, &rmt);
2095 if (error && uap->rmtp) {
2098 CP(rmt, rmt32, tv_sec);
2099 CP(rmt, rmt32, tv_nsec);
2101 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2109 freebsd32_clock_gettime(struct thread *td,
2110 struct freebsd32_clock_gettime_args *uap)
2112 struct timespec ats;
2113 struct timespec32 ats32;
2116 error = kern_clock_gettime(td, uap->clock_id, &ats);
2118 CP(ats, ats32, tv_sec);
2119 CP(ats, ats32, tv_nsec);
2120 error = copyout(&ats32, uap->tp, sizeof(ats32));
2126 freebsd32_clock_settime(struct thread *td,
2127 struct freebsd32_clock_settime_args *uap)
2129 struct timespec ats;
2130 struct timespec32 ats32;
2133 error = copyin(uap->tp, &ats32, sizeof(ats32));
2136 CP(ats32, ats, tv_sec);
2137 CP(ats32, ats, tv_nsec);
2139 return (kern_clock_settime(td, uap->clock_id, &ats));
2143 freebsd32_clock_getres(struct thread *td,
2144 struct freebsd32_clock_getres_args *uap)
2147 struct timespec32 ts32;
2150 if (uap->tp == NULL)
2152 error = kern_clock_getres(td, uap->clock_id, &ts);
2154 CP(ts, ts32, tv_sec);
2155 CP(ts, ts32, tv_nsec);
2156 error = copyout(&ts32, uap->tp, sizeof(ts32));
2162 freebsd32_thr_new(struct thread *td,
2163 struct freebsd32_thr_new_args *uap)
2165 struct thr_param32 param32;
2166 struct thr_param param;
2169 if (uap->param_size < 0 ||
2170 uap->param_size > sizeof(struct thr_param32))
2172 bzero(¶m, sizeof(struct thr_param));
2173 bzero(¶m32, sizeof(struct thr_param32));
2174 error = copyin(uap->param, ¶m32, uap->param_size);
2177 param.start_func = PTRIN(param32.start_func);
2178 param.arg = PTRIN(param32.arg);
2179 param.stack_base = PTRIN(param32.stack_base);
2180 param.stack_size = param32.stack_size;
2181 param.tls_base = PTRIN(param32.tls_base);
2182 param.tls_size = param32.tls_size;
2183 param.child_tid = PTRIN(param32.child_tid);
2184 param.parent_tid = PTRIN(param32.parent_tid);
2185 param.flags = param32.flags;
2186 param.rtp = PTRIN(param32.rtp);
2187 param.spare[0] = PTRIN(param32.spare[0]);
2188 param.spare[1] = PTRIN(param32.spare[1]);
2189 param.spare[2] = PTRIN(param32.spare[2]);
2191 return (kern_thr_new(td, ¶m));
2195 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2197 struct timespec32 ts32;
2198 struct timespec ts, *tsp;
2203 if (uap->timeout != NULL) {
2204 error = copyin((const void *)uap->timeout, (void *)&ts32,
2205 sizeof(struct timespec32));
2208 ts.tv_sec = ts32.tv_sec;
2209 ts.tv_nsec = ts32.tv_nsec;
2212 return (kern_thr_suspend(td, tsp));
2216 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2218 bzero(dst, sizeof(*dst));
2219 dst->si_signo = src->si_signo;
2220 dst->si_errno = src->si_errno;
2221 dst->si_code = src->si_code;
2222 dst->si_pid = src->si_pid;
2223 dst->si_uid = src->si_uid;
2224 dst->si_status = src->si_status;
2225 dst->si_addr = (uintptr_t)src->si_addr;
2226 dst->si_value.sigval_int = src->si_value.sival_int;
2227 dst->si_timerid = src->si_timerid;
2228 dst->si_overrun = src->si_overrun;
2232 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2234 struct timespec32 ts32;
2236 struct timespec *timeout;
2239 struct siginfo32 si32;
2243 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2246 ts.tv_sec = ts32.tv_sec;
2247 ts.tv_nsec = ts32.tv_nsec;
2252 error = copyin(uap->set, &set, sizeof(set));
2256 error = kern_sigtimedwait(td, set, &ksi, timeout);
2261 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2262 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2266 td->td_retval[0] = ksi.ksi_signo;
2274 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2277 struct siginfo32 si32;
2281 error = copyin(uap->set, &set, sizeof(set));
2285 error = kern_sigtimedwait(td, set, &ksi, NULL);
2290 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2291 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2294 td->td_retval[0] = ksi.ksi_signo;
2299 freebsd32_cpuset_setid(struct thread *td,
2300 struct freebsd32_cpuset_setid_args *uap)
2302 struct cpuset_setid_args ap;
2304 ap.which = uap->which;
2305 ap.id = PAIR32TO64(id_t,uap->id);
2306 ap.setid = uap->setid;
2308 return (cpuset_setid(td, &ap));
2312 freebsd32_cpuset_getid(struct thread *td,
2313 struct freebsd32_cpuset_getid_args *uap)
2315 struct cpuset_getid_args ap;
2317 ap.level = uap->level;
2318 ap.which = uap->which;
2319 ap.id = PAIR32TO64(id_t,uap->id);
2320 ap.setid = uap->setid;
2322 return (cpuset_getid(td, &ap));
2326 freebsd32_cpuset_getaffinity(struct thread *td,
2327 struct freebsd32_cpuset_getaffinity_args *uap)
2329 struct cpuset_getaffinity_args ap;
2331 ap.level = uap->level;
2332 ap.which = uap->which;
2333 ap.id = PAIR32TO64(id_t,uap->id);
2334 ap.cpusetsize = uap->cpusetsize;
2335 ap.mask = uap->mask;
2337 return (cpuset_getaffinity(td, &ap));
2341 freebsd32_cpuset_setaffinity(struct thread *td,
2342 struct freebsd32_cpuset_setaffinity_args *uap)
2344 struct cpuset_setaffinity_args ap;
2346 ap.level = uap->level;
2347 ap.which = uap->which;
2348 ap.id = PAIR32TO64(id_t,uap->id);
2349 ap.cpusetsize = uap->cpusetsize;
2350 ap.mask = uap->mask;
2352 return (cpuset_setaffinity(td, &ap));
2356 freebsd32_nmount(struct thread *td,
2357 struct freebsd32_nmount_args /* {
2359 unsigned int iovcnt;
2366 AUDIT_ARG_FFLAGS(uap->flags);
2369 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2370 * userspace to set this flag, but we must filter it out if we want
2371 * MNT_UPDATE on the root file system to work.
2372 * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
2374 uap->flags &= ~MNT_ROOTFS;
2377 * check that we have an even number of iovec's
2378 * and that we have at least two options.
2380 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2383 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2386 error = vfs_donmount(td, uap->flags, auio);
2394 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2396 struct yyy32 *p32, s32;
2397 struct yyy *p = NULL, s;
2402 error = copyin(uap->zzz, &s32, sizeof(s32));
2408 error = kern_xxx(td, p);
2413 error = copyout(&s32, p32, sizeof(s32));
2420 syscall32_register(int *offset, struct sysent *new_sysent,
2421 struct sysent *old_sysent)
2423 if (*offset == NO_SYSCALL) {
2426 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2427 if (freebsd32_sysent[i].sy_call ==
2428 (sy_call_t *)lkmnosys)
2430 if (i == SYS_MAXSYSCALL)
2433 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2435 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2436 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2439 *old_sysent = freebsd32_sysent[*offset];
2440 freebsd32_sysent[*offset] = *new_sysent;
2445 syscall32_deregister(int *offset, struct sysent *old_sysent)
2449 freebsd32_sysent[*offset] = *old_sysent;
2454 syscall32_module_handler(struct module *mod, int what, void *arg)
2456 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2462 error = syscall32_register(data->offset, data->new_sysent,
2465 /* Leave a mark so we know to safely unload below. */
2466 data->offset = NULL;
2469 ms.intval = *data->offset;
2471 module_setspecific(mod, &ms);
2474 error = data->chainevh(mod, what, data->chainarg);
2478 * MOD_LOAD failed, so just return without calling the
2479 * chained handler since we didn't pass along the MOD_LOAD
2482 if (data->offset == NULL)
2484 if (data->chainevh) {
2485 error = data->chainevh(mod, what, data->chainarg);
2489 error = syscall32_deregister(data->offset, &data->old_sysent);
2494 error = data->chainevh(mod, what, data->chainarg);
2500 syscall32_helper_register(struct syscall_helper_data *sd)
2502 struct syscall_helper_data *sd1;
2505 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2506 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2509 syscall32_helper_unregister(sd);
2512 sd1->registered = 1;
2518 syscall32_helper_unregister(struct syscall_helper_data *sd)
2520 struct syscall_helper_data *sd1;
2522 for (sd1 = sd; sd1->registered != 0; sd1++) {
2523 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2524 sd1->registered = 0;
2530 freebsd32_copyout_strings(struct image_params *imgp)
2534 char *stringp, *destp;
2535 u_int32_t *stack_base;
2536 struct freebsd32_ps_strings *arginfo;
2537 size_t execpath_len;
2541 * Calculate string base and vector table pointers.
2542 * Also deal with signal trampoline code for this exec type.
2544 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2545 execpath_len = strlen(imgp->execpath) + 1;
2548 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2550 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2551 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2552 roundup(execpath_len, sizeof(char *)) -
2553 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2559 copyout(imgp->proc->p_sysent->sv_sigcode,
2560 ((caddr_t)arginfo - szsigcode), szsigcode);
2563 * Copy the image path for the rtld.
2565 if (execpath_len != 0) {
2566 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2567 copyout(imgp->execpath, (void *)imgp->execpathp,
2572 * If we have a valid auxargs ptr, prepare some room
2575 if (imgp->auxargs) {
2577 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2578 * lower compatibility.
2580 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2583 * The '+ 2' is for the null pointers at the end of each of
2584 * the arg and env vector sets,and imgp->auxarg_size is room
2585 * for argument of Runtime loader.
2587 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2588 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2592 * The '+ 2' is for the null pointers at the end of each of
2593 * the arg and env vector sets
2595 vectp = (u_int32_t *)
2596 (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2599 * vectp also becomes our initial stack base
2603 stringp = imgp->args->begin_argv;
2604 argc = imgp->args->argc;
2605 envc = imgp->args->envc;
2607 * Copy out strings - arguments and environment.
2609 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2612 * Fill in "ps_strings" struct for ps, w, etc.
2614 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2615 suword32(&arginfo->ps_nargvstr, argc);
2618 * Fill in argument portion of vector table.
2620 for (; argc > 0; --argc) {
2621 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2622 while (*stringp++ != 0)
2627 /* a null vector table pointer separates the argp's from the envp's */
2628 suword32(vectp++, 0);
2630 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2631 suword32(&arginfo->ps_nenvstr, envc);
2634 * Fill in environment portion of vector table.
2636 for (; envc > 0; --envc) {
2637 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2638 while (*stringp++ != 0)
2643 /* end of vector table is a null pointer */
2646 return ((register_t *)stack_base);
2650 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2652 struct kld_file_stat stat;
2653 struct kld32_file_stat stat32;
2656 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2659 if (version != sizeof(struct kld32_file_stat_1) &&
2660 version != sizeof(struct kld32_file_stat))
2663 error = kern_kldstat(td, uap->fileid, &stat);
2667 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2668 CP(stat, stat32, refs);
2669 CP(stat, stat32, id);
2670 PTROUT_CP(stat, stat32, address);
2671 CP(stat, stat32, size);
2672 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2673 return (copyout(&stat32, uap->stat, version));
2677 freebsd32_posix_fallocate(struct thread *td,
2678 struct freebsd32_posix_fallocate_args *uap)
2681 return (kern_posix_fallocate(td, uap->fd,
2682 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2686 freebsd32_posix_fadvise(struct thread *td,
2687 struct freebsd32_posix_fadvise_args *uap)
2690 return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2691 PAIR32TO64(off_t, uap->len), uap->advice));