2 * Copyright (c) 2002 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_compat.h"
32 #include "opt_inet6.h"
34 #define __ELF_WORD_SIZE 32
36 #include <sys/param.h>
38 #include <sys/clock.h>
40 #include <sys/fcntl.h>
41 #include <sys/filedesc.h>
42 #include <sys/imgact.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/linker.h>
48 #include <sys/malloc.h>
49 #include <sys/file.h> /* Must come after sys/malloc.h */
50 #include <sys/imgact.h>
53 #include <sys/module.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
56 #include <sys/namei.h>
58 #include <sys/reboot.h>
59 #include <sys/resource.h>
60 #include <sys/resourcevar.h>
61 #include <sys/selinfo.h>
62 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
63 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
64 #include <sys/signal.h>
65 #include <sys/signalvar.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
69 #include <sys/syscall.h>
70 #include <sys/syscallsubr.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
73 #include <sys/sysproto.h>
74 #include <sys/systm.h>
76 #include <sys/unistd.h>
77 #include <sys/ucontext.h>
78 #include <sys/vnode.h>
86 #include <netinet/in.h>
90 #include <vm/vm_param.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_extern.h>
96 #include <machine/cpu.h>
97 #include <machine/elf.h>
99 #include <security/audit/audit.h>
101 #include <compat/freebsd32/freebsd32_util.h>
102 #include <compat/freebsd32/freebsd32.h>
103 #include <compat/freebsd32/freebsd32_ipc.h>
104 #include <compat/freebsd32/freebsd32_signal.h>
105 #include <compat/freebsd32/freebsd32_proto.h>
108 CTASSERT(sizeof(struct timeval32) == 8);
109 CTASSERT(sizeof(struct timespec32) == 8);
110 CTASSERT(sizeof(struct itimerval32) == 16);
112 CTASSERT(sizeof(struct statfs32) == 256);
114 CTASSERT(sizeof(struct rusage32) == 72);
116 CTASSERT(sizeof(struct sigaltstack32) == 12);
117 CTASSERT(sizeof(struct kevent32) == 20);
118 CTASSERT(sizeof(struct iovec32) == 8);
119 CTASSERT(sizeof(struct msghdr32) == 28);
121 CTASSERT(sizeof(struct stat32) == 96);
123 CTASSERT(sizeof(struct sigaction32) == 24);
125 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
126 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
128 #if BYTE_ORDER == BIG_ENDIAN
129 #define PAIR32TO64(type, name) ((name ## 2) | ((type)(name ## 1) << 32))
133 #define PAIR32TO64(type, name) ((name ## 1) | ((type)(name ## 2) << 32))
139 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
142 TV_CP(*s, *s32, ru_utime);
143 TV_CP(*s, *s32, ru_stime);
144 CP(*s, *s32, ru_maxrss);
145 CP(*s, *s32, ru_ixrss);
146 CP(*s, *s32, ru_idrss);
147 CP(*s, *s32, ru_isrss);
148 CP(*s, *s32, ru_minflt);
149 CP(*s, *s32, ru_majflt);
150 CP(*s, *s32, ru_nswap);
151 CP(*s, *s32, ru_inblock);
152 CP(*s, *s32, ru_oublock);
153 CP(*s, *s32, ru_msgsnd);
154 CP(*s, *s32, ru_msgrcv);
155 CP(*s, *s32, ru_nsignals);
156 CP(*s, *s32, ru_nvcsw);
157 CP(*s, *s32, ru_nivcsw);
161 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
164 struct rusage32 ru32;
165 struct rusage ru, *rup;
167 if (uap->rusage != NULL)
171 error = kern_wait(td, uap->pid, &status, uap->options, rup);
174 if (uap->status != NULL)
175 error = copyout(&status, uap->status, sizeof(status));
176 if (uap->rusage != NULL && error == 0) {
177 freebsd32_rusage_out(&ru, &ru32);
178 error = copyout(&ru32, uap->rusage, sizeof(ru32));
183 #ifdef COMPAT_FREEBSD4
185 copy_statfs(struct statfs *in, struct statfs32 *out)
188 statfs_scale_blocks(in, INT32_MAX);
189 bzero(out, sizeof(*out));
190 CP(*in, *out, f_bsize);
191 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
192 CP(*in, *out, f_blocks);
193 CP(*in, *out, f_bfree);
194 CP(*in, *out, f_bavail);
195 out->f_files = MIN(in->f_files, INT32_MAX);
196 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
197 CP(*in, *out, f_fsid);
198 CP(*in, *out, f_owner);
199 CP(*in, *out, f_type);
200 CP(*in, *out, f_flags);
201 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
202 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
203 strlcpy(out->f_fstypename,
204 in->f_fstypename, MFSNAMELEN);
205 strlcpy(out->f_mntonname,
206 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
207 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
208 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
209 strlcpy(out->f_mntfromname,
210 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
214 #ifdef COMPAT_FREEBSD4
216 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
218 struct statfs *buf, *sp;
219 struct statfs32 stat32;
223 count = uap->bufsize / sizeof(struct statfs32);
224 size = count * sizeof(struct statfs);
225 error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
227 count = td->td_retval[0];
229 while (count > 0 && error == 0) {
230 copy_statfs(sp, &stat32);
231 error = copyout(&stat32, uap->buf, sizeof(stat32));
243 freebsd32_sigaltstack(struct thread *td,
244 struct freebsd32_sigaltstack_args *uap)
246 struct sigaltstack32 s32;
247 struct sigaltstack ss, oss, *ssp;
250 if (uap->ss != NULL) {
251 error = copyin(uap->ss, &s32, sizeof(s32));
254 PTRIN_CP(s32, ss, ss_sp);
255 CP(s32, ss, ss_size);
256 CP(s32, ss, ss_flags);
260 error = kern_sigaltstack(td, ssp, &oss);
261 if (error == 0 && uap->oss != NULL) {
262 PTROUT_CP(oss, s32, ss_sp);
263 CP(oss, s32, ss_size);
264 CP(oss, s32, ss_flags);
265 error = copyout(&s32, uap->oss, sizeof(s32));
271 * Custom version of exec_copyin_args() so that we can translate
275 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
276 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
283 bzero(args, sizeof(*args));
288 * Allocate demand-paged memory for the file name, argument, and
289 * environment strings.
291 error = exec_alloc_args(args);
296 * Copy the file name.
299 args->fname = args->buf;
300 error = (segflg == UIO_SYSSPACE) ?
301 copystr(fname, args->fname, PATH_MAX, &length) :
302 copyinstr(fname, args->fname, PATH_MAX, &length);
308 args->begin_argv = args->buf + length;
309 args->endp = args->begin_argv;
310 args->stringspace = ARG_MAX;
313 * extract arguments first
317 error = copyin(p32++, &arg, sizeof(arg));
323 error = copyinstr(argp, args->endp, args->stringspace, &length);
325 if (error == ENAMETOOLONG)
329 args->stringspace -= length;
330 args->endp += length;
334 args->begin_envv = args->endp;
337 * extract environment strings
342 error = copyin(p32++, &arg, sizeof(arg));
348 error = copyinstr(envp, args->endp, args->stringspace,
351 if (error == ENAMETOOLONG)
355 args->stringspace -= length;
356 args->endp += length;
364 exec_free_args(args);
369 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
371 struct image_args eargs;
374 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
375 uap->argv, uap->envv);
377 error = kern_execve(td, &eargs, NULL);
382 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
384 struct image_args eargs;
387 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
388 uap->argv, uap->envv);
391 error = kern_execve(td, &eargs, NULL);
398 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
399 int prot, int fd, off_t pos)
402 vm_map_entry_t entry;
405 map = &td->td_proc->p_vmspace->vm_map;
407 prot |= VM_PROT_WRITE;
409 if (vm_map_lookup_entry(map, start, &entry)) {
410 if ((entry->protection & prot) != prot) {
411 rv = vm_map_protect(map,
414 entry->protection | prot,
416 if (rv != KERN_SUCCESS)
420 vm_offset_t addr = trunc_page(start);
421 rv = vm_map_find(map, 0, 0,
422 &addr, PAGE_SIZE, FALSE, prot,
424 if (rv != KERN_SUCCESS)
431 r.buf = (void *) start;
432 r.nbyte = end - start;
434 return (sys_pread(td, &r));
436 while (start < end) {
437 subyte((void *) start, 0);
446 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
448 struct mprotect_args ap;
450 ap.addr = PTRIN(uap->addr);
453 #if defined(__amd64__) || defined(__ia64__)
454 if (i386_read_exec && (ap.prot & PROT_READ) != 0)
455 ap.prot |= PROT_EXEC;
457 return (sys_mprotect(td, &ap));
461 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
464 vm_offset_t addr = (vm_offset_t) uap->addr;
465 vm_size_t len = uap->len;
466 int prot = uap->prot;
467 int flags = uap->flags;
469 off_t pos = PAIR32TO64(off_t,uap->pos);
475 * Attempt to handle page size hassles.
477 pageoff = (pos & PAGE_MASK);
478 if (flags & MAP_FIXED) {
479 vm_offset_t start, end;
483 if (start != trunc_page(start)) {
484 error = freebsd32_mmap_partial(td, start,
485 round_page(start), prot,
488 pos += round_page(start) - start;
489 start = round_page(start);
491 if (end != round_page(end)) {
492 vm_offset_t t = trunc_page(end);
493 error = freebsd32_mmap_partial(td, t, end,
496 end = trunc_page(end);
498 if (end > start && fd != -1 && (pos & PAGE_MASK)) {
500 * We can't map this region at all. The specified
501 * address doesn't have the same alignment as the file
502 * position. Fake the mapping by simply reading the
503 * entire region into memory. First we need to make
504 * sure the region exists.
510 prot |= VM_PROT_WRITE;
511 map = &td->td_proc->p_vmspace->vm_map;
512 rv = vm_map_remove(map, start, end);
513 if (rv != KERN_SUCCESS)
515 rv = vm_map_find(map, 0, 0,
516 &start, end - start, FALSE,
517 prot, VM_PROT_ALL, 0);
518 if (rv != KERN_SUCCESS)
521 r.buf = (void *) start;
522 r.nbyte = end - start;
524 error = sys_pread(td, &r);
528 td->td_retval[0] = addr;
533 * After dealing with the ragged ends, there
534 * might be none left.
536 td->td_retval[0] = addr;
544 #if defined(__amd64__) || defined(__ia64__)
545 if (i386_read_exec && (prot & PROT_READ))
549 ap.addr = (void *) addr;
556 return (sys_mmap(td, &ap));
559 #ifdef COMPAT_FREEBSD6
561 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
563 struct freebsd32_mmap_args ap;
568 ap.flags = uap->flags;
573 return (freebsd32_mmap(td, &ap));
578 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
580 struct itimerval itv, oitv, *itvp;
581 struct itimerval32 i32;
584 if (uap->itv != NULL) {
585 error = copyin(uap->itv, &i32, sizeof(i32));
588 TV_CP(i32, itv, it_interval);
589 TV_CP(i32, itv, it_value);
593 error = kern_setitimer(td, uap->which, itvp, &oitv);
594 if (error || uap->oitv == NULL)
596 TV_CP(oitv, i32, it_interval);
597 TV_CP(oitv, i32, it_value);
598 return (copyout(&i32, uap->oitv, sizeof(i32)));
602 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
604 struct itimerval itv;
605 struct itimerval32 i32;
608 error = kern_getitimer(td, uap->which, &itv);
609 if (error || uap->itv == NULL)
611 TV_CP(itv, i32, it_interval);
612 TV_CP(itv, i32, it_value);
613 return (copyout(&i32, uap->itv, sizeof(i32)));
617 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
619 struct timeval32 tv32;
620 struct timeval tv, *tvp;
623 if (uap->tv != NULL) {
624 error = copyin(uap->tv, &tv32, sizeof(tv32));
627 CP(tv32, tv, tv_sec);
628 CP(tv32, tv, tv_usec);
633 * XXX Do pointers need PTRIN()?
635 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
636 sizeof(int32_t) * 8));
640 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
642 struct timespec32 ts32;
644 struct timeval tv, *tvp;
648 if (uap->ts != NULL) {
649 error = copyin(uap->ts, &ts32, sizeof(ts32));
652 CP(ts32, ts, tv_sec);
653 CP(ts32, ts, tv_nsec);
654 TIMESPEC_TO_TIMEVAL(&tv, &ts);
658 if (uap->sm != NULL) {
659 error = copyin(uap->sm, &set, sizeof(set));
666 * XXX Do pointers need PTRIN()?
668 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
669 uset, sizeof(int32_t) * 8);
674 * Copy 'count' items into the destination list pointed to by uap->eventlist.
677 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
679 struct freebsd32_kevent_args *uap;
680 struct kevent32 ks32[KQ_NEVENTS];
683 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
684 uap = (struct freebsd32_kevent_args *)arg;
686 for (i = 0; i < count; i++) {
687 CP(kevp[i], ks32[i], ident);
688 CP(kevp[i], ks32[i], filter);
689 CP(kevp[i], ks32[i], flags);
690 CP(kevp[i], ks32[i], fflags);
691 CP(kevp[i], ks32[i], data);
692 PTROUT_CP(kevp[i], ks32[i], udata);
694 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
696 uap->eventlist += count;
701 * Copy 'count' items from the list pointed to by uap->changelist.
704 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
706 struct freebsd32_kevent_args *uap;
707 struct kevent32 ks32[KQ_NEVENTS];
710 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
711 uap = (struct freebsd32_kevent_args *)arg;
713 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
716 uap->changelist += count;
718 for (i = 0; i < count; i++) {
719 CP(ks32[i], kevp[i], ident);
720 CP(ks32[i], kevp[i], filter);
721 CP(ks32[i], kevp[i], flags);
722 CP(ks32[i], kevp[i], fflags);
723 CP(ks32[i], kevp[i], data);
724 PTRIN_CP(ks32[i], kevp[i], udata);
731 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
733 struct timespec32 ts32;
734 struct timespec ts, *tsp;
735 struct kevent_copyops k_ops = { uap,
736 freebsd32_kevent_copyout,
737 freebsd32_kevent_copyin};
742 error = copyin(uap->timeout, &ts32, sizeof(ts32));
745 CP(ts32, ts, tv_sec);
746 CP(ts32, ts, tv_nsec);
750 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
756 freebsd32_gettimeofday(struct thread *td,
757 struct freebsd32_gettimeofday_args *uap)
760 struct timeval32 atv32;
766 CP(atv, atv32, tv_sec);
767 CP(atv, atv32, tv_usec);
768 error = copyout(&atv32, uap->tp, sizeof (atv32));
770 if (error == 0 && uap->tzp != NULL) {
771 rtz.tz_minuteswest = tz_minuteswest;
772 rtz.tz_dsttime = tz_dsttime;
773 error = copyout(&rtz, uap->tzp, sizeof (rtz));
779 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
785 error = kern_getrusage(td, uap->who, &s);
788 if (uap->rusage != NULL) {
789 freebsd32_rusage_out(&s, &s32);
790 error = copyout(&s32, uap->rusage, sizeof(s32));
796 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
798 struct iovec32 iov32;
805 if (iovcnt > UIO_MAXIOV)
807 iovlen = iovcnt * sizeof(struct iovec);
808 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
809 iov = (struct iovec *)(uio + 1);
810 for (i = 0; i < iovcnt; i++) {
811 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
816 iov[i].iov_base = PTRIN(iov32.iov_base);
817 iov[i].iov_len = iov32.iov_len;
820 uio->uio_iovcnt = iovcnt;
821 uio->uio_segflg = UIO_USERSPACE;
822 uio->uio_offset = -1;
824 for (i = 0; i < iovcnt; i++) {
825 if (iov->iov_len > INT_MAX - uio->uio_resid) {
829 uio->uio_resid += iov->iov_len;
837 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
842 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
845 error = kern_readv(td, uap->fd, auio);
851 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
856 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
859 error = kern_writev(td, uap->fd, auio);
865 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
870 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
873 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
879 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
884 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
887 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
893 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
896 struct iovec32 iov32;
902 if (iovcnt > UIO_MAXIOV)
904 iovlen = iovcnt * sizeof(struct iovec);
905 iov = malloc(iovlen, M_IOV, M_WAITOK);
906 for (i = 0; i < iovcnt; i++) {
907 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
912 iov[i].iov_base = PTRIN(iov32.iov_base);
913 iov[i].iov_len = iov32.iov_len;
920 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
925 error = copyin(msg32, &m32, sizeof(m32));
928 msg->msg_name = PTRIN(m32.msg_name);
929 msg->msg_namelen = m32.msg_namelen;
930 msg->msg_iov = PTRIN(m32.msg_iov);
931 msg->msg_iovlen = m32.msg_iovlen;
932 msg->msg_control = PTRIN(m32.msg_control);
933 msg->msg_controllen = m32.msg_controllen;
934 msg->msg_flags = m32.msg_flags;
939 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
944 m32.msg_name = PTROUT(msg->msg_name);
945 m32.msg_namelen = msg->msg_namelen;
946 m32.msg_iov = PTROUT(msg->msg_iov);
947 m32.msg_iovlen = msg->msg_iovlen;
948 m32.msg_control = PTROUT(msg->msg_control);
949 m32.msg_controllen = msg->msg_controllen;
950 m32.msg_flags = msg->msg_flags;
951 error = copyout(&m32, msg32, sizeof(m32));
956 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
958 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
960 #define FREEBSD32_ALIGN(p) \
961 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
962 #define FREEBSD32_CMSG_SPACE(l) \
963 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
965 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
966 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
968 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
972 socklen_t clen, datalen;
975 int len, maxlen, copylen;
979 len = msg->msg_controllen;
980 maxlen = msg->msg_controllen;
981 msg->msg_controllen = 0;
984 ctlbuf = msg->msg_control;
986 while (m && len > 0) {
987 cm = mtod(m, struct cmsghdr *);
992 if (sizeof(struct cmsghdr) > clen ||
993 cm->cmsg_len > clen) {
998 data = CMSG_DATA(cm);
999 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1001 /* Adjust message length */
1002 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1007 copylen = sizeof(struct cmsghdr);
1008 if (len < copylen) {
1009 msg->msg_flags |= MSG_CTRUNC;
1013 error = copyout(cm,ctlbuf,copylen);
1017 ctlbuf += FREEBSD32_ALIGN(copylen);
1018 len -= FREEBSD32_ALIGN(copylen);
1025 if (len < copylen) {
1026 msg->msg_flags |= MSG_CTRUNC;
1030 error = copyout(data,ctlbuf,copylen);
1034 ctlbuf += FREEBSD32_ALIGN(copylen);
1035 len -= FREEBSD32_ALIGN(copylen);
1037 if (CMSG_SPACE(datalen) < clen) {
1038 clen -= CMSG_SPACE(datalen);
1039 cm = (struct cmsghdr *)
1040 ((caddr_t)cm + CMSG_SPACE(datalen));
1049 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1057 freebsd32_recvmsg(td, uap)
1059 struct freebsd32_recvmsg_args /* {
1061 struct msghdr32 *msg;
1066 struct msghdr32 m32;
1067 struct iovec *uiov, *iov;
1068 struct mbuf *control = NULL;
1069 struct mbuf **controlp;
1072 error = copyin(uap->msg, &m32, sizeof(m32));
1075 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1078 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1082 msg.msg_flags = uap->flags;
1086 controlp = (msg.msg_control != NULL) ? &control : NULL;
1087 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1091 if (control != NULL)
1092 error = freebsd32_copy_msg_out(&msg, control);
1094 msg.msg_controllen = 0;
1097 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1101 if (control != NULL)
1109 freebsd32_convert_msg_in(struct mbuf **controlp)
1111 struct mbuf *control = *controlp;
1112 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1114 socklen_t clen = control->m_len, datalen;
1120 while (cm != NULL) {
1121 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
1126 data = FREEBSD32_CMSG_DATA(cm);
1127 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1129 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
1131 controlp = &(*controlp)->m_next;
1133 if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
1134 clen -= FREEBSD32_CMSG_SPACE(datalen);
1135 cm = (struct cmsghdr *)
1136 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
1149 freebsd32_sendmsg(struct thread *td,
1150 struct freebsd32_sendmsg_args *uap)
1153 struct msghdr32 m32;
1155 struct mbuf *control = NULL;
1156 struct sockaddr *to = NULL;
1159 error = copyin(uap->msg, &m32, sizeof(m32));
1162 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1165 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1170 if (msg.msg_name != NULL) {
1171 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1179 if (msg.msg_control) {
1180 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1185 error = sockargs(&control, msg.msg_control,
1186 msg.msg_controllen, MT_CONTROL);
1190 error = freebsd32_convert_msg_in(&control);
1195 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1206 freebsd32_recvfrom(struct thread *td,
1207 struct freebsd32_recvfrom_args *uap)
1213 if (uap->fromlenaddr) {
1214 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1215 sizeof(msg.msg_namelen));
1219 msg.msg_namelen = 0;
1222 msg.msg_name = PTRIN(uap->from);
1223 msg.msg_iov = &aiov;
1225 aiov.iov_base = PTRIN(uap->buf);
1226 aiov.iov_len = uap->len;
1227 msg.msg_control = NULL;
1228 msg.msg_flags = uap->flags;
1229 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1230 if (error == 0 && uap->fromlenaddr)
1231 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1232 sizeof (msg.msg_namelen));
1237 freebsd32_settimeofday(struct thread *td,
1238 struct freebsd32_settimeofday_args *uap)
1240 struct timeval32 tv32;
1241 struct timeval tv, *tvp;
1242 struct timezone tz, *tzp;
1246 error = copyin(uap->tv, &tv32, sizeof(tv32));
1249 CP(tv32, tv, tv_sec);
1250 CP(tv32, tv, tv_usec);
1255 error = copyin(uap->tzp, &tz, sizeof(tz));
1261 return (kern_settimeofday(td, tvp, tzp));
1265 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1267 struct timeval32 s32[2];
1268 struct timeval s[2], *sp;
1271 if (uap->tptr != NULL) {
1272 error = copyin(uap->tptr, s32, sizeof(s32));
1275 CP(s32[0], s[0], tv_sec);
1276 CP(s32[0], s[0], tv_usec);
1277 CP(s32[1], s[1], tv_sec);
1278 CP(s32[1], s[1], tv_usec);
1282 return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1286 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1288 struct timeval32 s32[2];
1289 struct timeval s[2], *sp;
1292 if (uap->tptr != NULL) {
1293 error = copyin(uap->tptr, s32, sizeof(s32));
1296 CP(s32[0], s[0], tv_sec);
1297 CP(s32[0], s[0], tv_usec);
1298 CP(s32[1], s[1], tv_sec);
1299 CP(s32[1], s[1], tv_usec);
1303 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1307 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1309 struct timeval32 s32[2];
1310 struct timeval s[2], *sp;
1313 if (uap->tptr != NULL) {
1314 error = copyin(uap->tptr, s32, sizeof(s32));
1317 CP(s32[0], s[0], tv_sec);
1318 CP(s32[0], s[0], tv_usec);
1319 CP(s32[1], s[1], tv_sec);
1320 CP(s32[1], s[1], tv_usec);
1324 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1328 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1330 struct timeval32 s32[2];
1331 struct timeval s[2], *sp;
1334 if (uap->times != NULL) {
1335 error = copyin(uap->times, s32, sizeof(s32));
1338 CP(s32[0], s[0], tv_sec);
1339 CP(s32[0], s[0], tv_usec);
1340 CP(s32[1], s[1], tv_sec);
1341 CP(s32[1], s[1], tv_usec);
1345 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1350 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1352 struct timeval32 tv32;
1353 struct timeval delta, olddelta, *deltap;
1357 error = copyin(uap->delta, &tv32, sizeof(tv32));
1360 CP(tv32, delta, tv_sec);
1361 CP(tv32, delta, tv_usec);
1365 error = kern_adjtime(td, deltap, &olddelta);
1366 if (uap->olddelta && error == 0) {
1367 CP(olddelta, tv32, tv_sec);
1368 CP(olddelta, tv32, tv_usec);
1369 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1374 #ifdef COMPAT_FREEBSD4
1376 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1378 struct statfs32 s32;
1382 error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
1385 copy_statfs(&s, &s32);
1386 return (copyout(&s32, uap->buf, sizeof(s32)));
1390 #ifdef COMPAT_FREEBSD4
1392 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1394 struct statfs32 s32;
1398 error = kern_fstatfs(td, uap->fd, &s);
1401 copy_statfs(&s, &s32);
1402 return (copyout(&s32, uap->buf, sizeof(s32)));
1406 #ifdef COMPAT_FREEBSD4
1408 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1410 struct statfs32 s32;
1415 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1417 error = kern_fhstatfs(td, fh, &s);
1420 copy_statfs(&s, &s32);
1421 return (copyout(&s32, uap->buf, sizeof(s32)));
1426 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1428 struct pread_args ap;
1432 ap.nbyte = uap->nbyte;
1433 ap.offset = PAIR32TO64(off_t,uap->offset);
1434 return (sys_pread(td, &ap));
1438 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1440 struct pwrite_args ap;
1444 ap.nbyte = uap->nbyte;
1445 ap.offset = PAIR32TO64(off_t,uap->offset);
1446 return (sys_pwrite(td, &ap));
1451 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1453 struct lseek_args nuap;
1456 nuap.offset = uap->offset;
1457 nuap.whence = uap->whence;
1458 return (sys_lseek(td, &nuap));
1463 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1466 struct lseek_args ap;
1470 ap.offset = PAIR32TO64(off_t,uap->offset);
1471 ap.whence = uap->whence;
1472 error = sys_lseek(td, &ap);
1473 /* Expand the quad return into two parts for eax and edx */
1474 pos = *(off_t *)(td->td_retval);
1475 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1476 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1481 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1483 struct truncate_args ap;
1485 ap.path = uap->path;
1486 ap.length = PAIR32TO64(off_t,uap->length);
1487 return (sys_truncate(td, &ap));
1491 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1493 struct ftruncate_args ap;
1496 ap.length = PAIR32TO64(off_t,uap->length);
1497 return (sys_ftruncate(td, &ap));
1502 ofreebsd32_getdirentries(struct thread *td,
1503 struct ofreebsd32_getdirentries_args *uap)
1505 struct ogetdirentries_args ap;
1512 ap.count = uap->count;
1514 error = kern_ogetdirentries(td, &ap, &loff);
1517 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1524 freebsd32_getdirentries(struct thread *td,
1525 struct freebsd32_getdirentries_args *uap)
1531 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1532 NULL, UIO_USERSPACE);
1535 if (uap->basep != NULL) {
1537 error = copyout(&base32, uap->basep, sizeof(int32_t));
1542 #ifdef COMPAT_FREEBSD6
1543 /* versions with the 'int pad' argument */
1545 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1547 struct pread_args ap;
1551 ap.nbyte = uap->nbyte;
1552 ap.offset = PAIR32TO64(off_t,uap->offset);
1553 return (sys_pread(td, &ap));
1557 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1559 struct pwrite_args ap;
1563 ap.nbyte = uap->nbyte;
1564 ap.offset = PAIR32TO64(off_t,uap->offset);
1565 return (sys_pwrite(td, &ap));
1569 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1572 struct lseek_args ap;
1576 ap.offset = PAIR32TO64(off_t,uap->offset);
1577 ap.whence = uap->whence;
1578 error = sys_lseek(td, &ap);
1579 /* Expand the quad return into two parts for eax and edx */
1580 pos = *(off_t *)(td->td_retval);
1581 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1582 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1587 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1589 struct truncate_args ap;
1591 ap.path = uap->path;
1592 ap.length = PAIR32TO64(off_t,uap->length);
1593 return (sys_truncate(td, &ap));
1597 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1599 struct ftruncate_args ap;
1602 ap.length = PAIR32TO64(off_t,uap->length);
1603 return (sys_ftruncate(td, &ap));
1605 #endif /* COMPAT_FREEBSD6 */
1615 freebsd32_do_sendfile(struct thread *td,
1616 struct freebsd32_sendfile_args *uap, int compat)
1618 struct sendfile_args ap;
1619 struct sf_hdtr32 hdtr32;
1620 struct sf_hdtr hdtr;
1621 struct uio *hdr_uio, *trl_uio;
1622 struct iovec32 *iov32;
1625 hdr_uio = trl_uio = NULL;
1629 ap.offset = PAIR32TO64(off_t,uap->offset);
1630 ap.nbytes = uap->nbytes;
1631 ap.hdtr = (struct sf_hdtr *)uap->hdtr; /* XXX not used */
1632 ap.sbytes = uap->sbytes;
1633 ap.flags = uap->flags;
1635 if (uap->hdtr != NULL) {
1636 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1639 PTRIN_CP(hdtr32, hdtr, headers);
1640 CP(hdtr32, hdtr, hdr_cnt);
1641 PTRIN_CP(hdtr32, hdtr, trailers);
1642 CP(hdtr32, hdtr, trl_cnt);
1644 if (hdtr.headers != NULL) {
1645 iov32 = PTRIN(hdtr32.headers);
1646 error = freebsd32_copyinuio(iov32,
1647 hdtr32.hdr_cnt, &hdr_uio);
1651 if (hdtr.trailers != NULL) {
1652 iov32 = PTRIN(hdtr32.trailers);
1653 error = freebsd32_copyinuio(iov32,
1654 hdtr32.trl_cnt, &trl_uio);
1660 error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
1663 free(hdr_uio, M_IOV);
1665 free(trl_uio, M_IOV);
1669 #ifdef COMPAT_FREEBSD4
1671 freebsd4_freebsd32_sendfile(struct thread *td,
1672 struct freebsd4_freebsd32_sendfile_args *uap)
1674 return (freebsd32_do_sendfile(td,
1675 (struct freebsd32_sendfile_args *)uap, 1));
1680 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1683 return (freebsd32_do_sendfile(td, uap, 0));
1687 copy_stat(struct stat *in, struct stat32 *out)
1690 CP(*in, *out, st_dev);
1691 CP(*in, *out, st_ino);
1692 CP(*in, *out, st_mode);
1693 CP(*in, *out, st_nlink);
1694 CP(*in, *out, st_uid);
1695 CP(*in, *out, st_gid);
1696 CP(*in, *out, st_rdev);
1697 TS_CP(*in, *out, st_atim);
1698 TS_CP(*in, *out, st_mtim);
1699 TS_CP(*in, *out, st_ctim);
1700 CP(*in, *out, st_size);
1701 CP(*in, *out, st_blocks);
1702 CP(*in, *out, st_blksize);
1703 CP(*in, *out, st_flags);
1704 CP(*in, *out, st_gen);
1705 TS_CP(*in, *out, st_birthtim);
1710 copy_ostat(struct stat *in, struct ostat32 *out)
1713 CP(*in, *out, st_dev);
1714 CP(*in, *out, st_ino);
1715 CP(*in, *out, st_mode);
1716 CP(*in, *out, st_nlink);
1717 CP(*in, *out, st_uid);
1718 CP(*in, *out, st_gid);
1719 CP(*in, *out, st_rdev);
1720 CP(*in, *out, st_size);
1721 TS_CP(*in, *out, st_atim);
1722 TS_CP(*in, *out, st_mtim);
1723 TS_CP(*in, *out, st_ctim);
1724 CP(*in, *out, st_blksize);
1725 CP(*in, *out, st_blocks);
1726 CP(*in, *out, st_flags);
1727 CP(*in, *out, st_gen);
1732 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1738 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1741 copy_stat(&sb, &sb32);
1742 error = copyout(&sb32, uap->ub, sizeof (sb32));
1748 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1751 struct ostat32 sb32;
1754 error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
1757 copy_ostat(&sb, &sb32);
1758 error = copyout(&sb32, uap->ub, sizeof (sb32));
1764 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1770 error = kern_fstat(td, uap->fd, &ub);
1773 copy_stat(&ub, &ub32);
1774 error = copyout(&ub32, uap->ub, sizeof(ub32));
1780 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1783 struct ostat32 ub32;
1786 error = kern_fstat(td, uap->fd, &ub);
1789 copy_ostat(&ub, &ub32);
1790 error = copyout(&ub32, uap->ub, sizeof(ub32));
1796 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1802 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE, &ub);
1805 copy_stat(&ub, &ub32);
1806 error = copyout(&ub32, uap->buf, sizeof(ub32));
1811 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1817 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1820 copy_stat(&sb, &sb32);
1821 error = copyout(&sb32, uap->ub, sizeof (sb32));
1827 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1830 struct ostat32 sb32;
1833 error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
1836 copy_ostat(&sb, &sb32);
1837 error = copyout(&sb32, uap->ub, sizeof (sb32));
1843 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1845 int error, name[CTL_MAXNAME];
1848 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1850 error = copyin(uap->name, name, uap->namelen * sizeof(int));
1854 oldlen = fuword32(uap->oldlenp);
1857 error = userland_sysctl(td, name, uap->namelen,
1858 uap->old, &oldlen, 1,
1859 uap->new, uap->newlen, &j, SCTL_MASK32);
1860 if (error && error != ENOMEM)
1863 suword32(uap->oldlenp, j);
1868 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1874 error = copyin(uap->jail, &version, sizeof(uint32_t));
1881 /* FreeBSD single IPv4 jails. */
1882 struct jail32_v0 j32_v0;
1884 bzero(&j, sizeof(struct jail));
1885 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1888 CP(j32_v0, j, version);
1889 PTRIN_CP(j32_v0, j, path);
1890 PTRIN_CP(j32_v0, j, hostname);
1891 j.ip4s = j32_v0.ip_number;
1897 * Version 1 was used by multi-IPv4 jail implementations
1898 * that never made it into the official kernel.
1902 case 2: /* JAIL_API_VERSION */
1904 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
1907 error = copyin(uap->jail, &j32, sizeof(struct jail32));
1910 CP(j32, j, version);
1911 PTRIN_CP(j32, j, path);
1912 PTRIN_CP(j32, j, hostname);
1913 PTRIN_CP(j32, j, jailname);
1916 PTRIN_CP(j32, j, ip4);
1917 PTRIN_CP(j32, j, ip6);
1922 /* Sci-Fi jails are not supported, sorry. */
1925 return (kern_jail(td, &j));
1929 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
1934 /* Check that we have an even number of iovecs. */
1935 if (uap->iovcnt & 1)
1938 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1941 error = kern_jail_set(td, auio, uap->flags);
1947 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
1949 struct iovec32 iov32;
1953 /* Check that we have an even number of iovecs. */
1954 if (uap->iovcnt & 1)
1957 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
1960 error = kern_jail_get(td, auio, uap->flags);
1962 for (i = 0; i < uap->iovcnt; i++) {
1963 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
1964 CP(auio->uio_iov[i], iov32, iov_len);
1965 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
1974 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
1976 struct sigaction32 s32;
1977 struct sigaction sa, osa, *sap;
1981 error = copyin(uap->act, &s32, sizeof(s32));
1984 sa.sa_handler = PTRIN(s32.sa_u);
1985 CP(s32, sa, sa_flags);
1986 CP(s32, sa, sa_mask);
1990 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
1991 if (error == 0 && uap->oact != NULL) {
1992 s32.sa_u = PTROUT(osa.sa_handler);
1993 CP(osa, s32, sa_flags);
1994 CP(osa, s32, sa_mask);
1995 error = copyout(&s32, uap->oact, sizeof(s32));
2000 #ifdef COMPAT_FREEBSD4
2002 freebsd4_freebsd32_sigaction(struct thread *td,
2003 struct freebsd4_freebsd32_sigaction_args *uap)
2005 struct sigaction32 s32;
2006 struct sigaction sa, osa, *sap;
2010 error = copyin(uap->act, &s32, sizeof(s32));
2013 sa.sa_handler = PTRIN(s32.sa_u);
2014 CP(s32, sa, sa_flags);
2015 CP(s32, sa, sa_mask);
2019 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2020 if (error == 0 && uap->oact != NULL) {
2021 s32.sa_u = PTROUT(osa.sa_handler);
2022 CP(osa, s32, sa_flags);
2023 CP(osa, s32, sa_mask);
2024 error = copyout(&s32, uap->oact, sizeof(s32));
2031 struct osigaction32 {
2040 ofreebsd32_sigaction(struct thread *td,
2041 struct ofreebsd32_sigaction_args *uap)
2043 struct osigaction32 s32;
2044 struct sigaction sa, osa, *sap;
2047 if (uap->signum <= 0 || uap->signum >= ONSIG)
2051 error = copyin(uap->nsa, &s32, sizeof(s32));
2054 sa.sa_handler = PTRIN(s32.sa_u);
2055 CP(s32, sa, sa_flags);
2056 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2060 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2061 if (error == 0 && uap->osa != NULL) {
2062 s32.sa_u = PTROUT(osa.sa_handler);
2063 CP(osa, s32, sa_flags);
2064 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2065 error = copyout(&s32, uap->osa, sizeof(s32));
2071 ofreebsd32_sigprocmask(struct thread *td,
2072 struct ofreebsd32_sigprocmask_args *uap)
2077 OSIG2SIG(uap->mask, set);
2078 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2079 SIG2OSIG(oset, td->td_retval[0]);
2084 ofreebsd32_sigpending(struct thread *td,
2085 struct ofreebsd32_sigpending_args *uap)
2087 struct proc *p = td->td_proc;
2091 siglist = p->p_siglist;
2092 SIGSETOR(siglist, td->td_siglist);
2094 SIG2OSIG(siglist, td->td_retval[0]);
2099 u_int32_t sv_handler;
2105 ofreebsd32_sigvec(struct thread *td,
2106 struct ofreebsd32_sigvec_args *uap)
2108 struct sigvec32 vec;
2109 struct sigaction sa, osa, *sap;
2112 if (uap->signum <= 0 || uap->signum >= ONSIG)
2116 error = copyin(uap->nsv, &vec, sizeof(vec));
2119 sa.sa_handler = PTRIN(vec.sv_handler);
2120 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2121 sa.sa_flags = vec.sv_flags;
2122 sa.sa_flags ^= SA_RESTART;
2126 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2127 if (error == 0 && uap->osv != NULL) {
2128 vec.sv_handler = PTROUT(osa.sa_handler);
2129 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2130 vec.sv_flags = osa.sa_flags;
2131 vec.sv_flags &= ~SA_NOCLDWAIT;
2132 vec.sv_flags ^= SA_RESTART;
2133 error = copyout(&vec, uap->osv, sizeof(vec));
2139 ofreebsd32_sigblock(struct thread *td,
2140 struct ofreebsd32_sigblock_args *uap)
2144 OSIG2SIG(uap->mask, set);
2145 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2146 SIG2OSIG(oset, td->td_retval[0]);
2151 ofreebsd32_sigsetmask(struct thread *td,
2152 struct ofreebsd32_sigsetmask_args *uap)
2156 OSIG2SIG(uap->mask, set);
2157 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2158 SIG2OSIG(oset, td->td_retval[0]);
2163 ofreebsd32_sigsuspend(struct thread *td,
2164 struct ofreebsd32_sigsuspend_args *uap)
2168 OSIG2SIG(uap->mask, mask);
2169 return (kern_sigsuspend(td, mask));
2178 ofreebsd32_sigstack(struct thread *td,
2179 struct ofreebsd32_sigstack_args *uap)
2181 struct sigstack32 s32;
2182 struct sigstack nss, oss;
2183 int error = 0, unss;
2185 if (uap->nss != NULL) {
2186 error = copyin(uap->nss, &s32, sizeof(s32));
2189 nss.ss_sp = PTRIN(s32.ss_sp);
2190 CP(s32, nss, ss_onstack);
2195 oss.ss_sp = td->td_sigstk.ss_sp;
2196 oss.ss_onstack = sigonstack(cpu_getstack(td));
2198 td->td_sigstk.ss_sp = nss.ss_sp;
2199 td->td_sigstk.ss_size = 0;
2200 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2201 td->td_pflags |= TDP_ALTSTACK;
2203 if (uap->oss != NULL) {
2204 s32.ss_sp = PTROUT(oss.ss_sp);
2205 CP(oss, s32, ss_onstack);
2206 error = copyout(&s32, uap->oss, sizeof(s32));
2213 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2215 struct timespec32 rmt32, rqt32;
2216 struct timespec rmt, rqt;
2219 error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
2223 CP(rqt32, rqt, tv_sec);
2224 CP(rqt32, rqt, tv_nsec);
2227 !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
2229 error = kern_nanosleep(td, &rqt, &rmt);
2230 if (error && uap->rmtp) {
2233 CP(rmt, rmt32, tv_sec);
2234 CP(rmt, rmt32, tv_nsec);
2236 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
2244 freebsd32_clock_gettime(struct thread *td,
2245 struct freebsd32_clock_gettime_args *uap)
2247 struct timespec ats;
2248 struct timespec32 ats32;
2251 error = kern_clock_gettime(td, uap->clock_id, &ats);
2253 CP(ats, ats32, tv_sec);
2254 CP(ats, ats32, tv_nsec);
2255 error = copyout(&ats32, uap->tp, sizeof(ats32));
2261 freebsd32_clock_settime(struct thread *td,
2262 struct freebsd32_clock_settime_args *uap)
2264 struct timespec ats;
2265 struct timespec32 ats32;
2268 error = copyin(uap->tp, &ats32, sizeof(ats32));
2271 CP(ats32, ats, tv_sec);
2272 CP(ats32, ats, tv_nsec);
2274 return (kern_clock_settime(td, uap->clock_id, &ats));
2278 freebsd32_clock_getres(struct thread *td,
2279 struct freebsd32_clock_getres_args *uap)
2282 struct timespec32 ts32;
2285 if (uap->tp == NULL)
2287 error = kern_clock_getres(td, uap->clock_id, &ts);
2289 CP(ts, ts32, tv_sec);
2290 CP(ts, ts32, tv_nsec);
2291 error = copyout(&ts32, uap->tp, sizeof(ts32));
2297 freebsd32_thr_new(struct thread *td,
2298 struct freebsd32_thr_new_args *uap)
2300 struct thr_param32 param32;
2301 struct thr_param param;
2304 if (uap->param_size < 0 ||
2305 uap->param_size > sizeof(struct thr_param32))
2307 bzero(¶m, sizeof(struct thr_param));
2308 bzero(¶m32, sizeof(struct thr_param32));
2309 error = copyin(uap->param, ¶m32, uap->param_size);
2312 param.start_func = PTRIN(param32.start_func);
2313 param.arg = PTRIN(param32.arg);
2314 param.stack_base = PTRIN(param32.stack_base);
2315 param.stack_size = param32.stack_size;
2316 param.tls_base = PTRIN(param32.tls_base);
2317 param.tls_size = param32.tls_size;
2318 param.child_tid = PTRIN(param32.child_tid);
2319 param.parent_tid = PTRIN(param32.parent_tid);
2320 param.flags = param32.flags;
2321 param.rtp = PTRIN(param32.rtp);
2322 param.spare[0] = PTRIN(param32.spare[0]);
2323 param.spare[1] = PTRIN(param32.spare[1]);
2324 param.spare[2] = PTRIN(param32.spare[2]);
2326 return (kern_thr_new(td, ¶m));
2330 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2332 struct timespec32 ts32;
2333 struct timespec ts, *tsp;
2338 if (uap->timeout != NULL) {
2339 error = copyin((const void *)uap->timeout, (void *)&ts32,
2340 sizeof(struct timespec32));
2343 ts.tv_sec = ts32.tv_sec;
2344 ts.tv_nsec = ts32.tv_nsec;
2347 return (kern_thr_suspend(td, tsp));
2351 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2353 bzero(dst, sizeof(*dst));
2354 dst->si_signo = src->si_signo;
2355 dst->si_errno = src->si_errno;
2356 dst->si_code = src->si_code;
2357 dst->si_pid = src->si_pid;
2358 dst->si_uid = src->si_uid;
2359 dst->si_status = src->si_status;
2360 dst->si_addr = (uintptr_t)src->si_addr;
2361 dst->si_value.sigval_int = src->si_value.sival_int;
2362 dst->si_timerid = src->si_timerid;
2363 dst->si_overrun = src->si_overrun;
2367 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2369 struct timespec32 ts32;
2371 struct timespec *timeout;
2374 struct siginfo32 si32;
2378 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2381 ts.tv_sec = ts32.tv_sec;
2382 ts.tv_nsec = ts32.tv_nsec;
2387 error = copyin(uap->set, &set, sizeof(set));
2391 error = kern_sigtimedwait(td, set, &ksi, timeout);
2396 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2397 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2401 td->td_retval[0] = ksi.ksi_signo;
2409 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2412 struct siginfo32 si32;
2416 error = copyin(uap->set, &set, sizeof(set));
2420 error = kern_sigtimedwait(td, set, &ksi, NULL);
2425 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2426 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2429 td->td_retval[0] = ksi.ksi_signo;
2434 freebsd32_cpuset_setid(struct thread *td,
2435 struct freebsd32_cpuset_setid_args *uap)
2437 struct cpuset_setid_args ap;
2439 ap.which = uap->which;
2440 ap.id = PAIR32TO64(id_t,uap->id);
2441 ap.setid = uap->setid;
2443 return (sys_cpuset_setid(td, &ap));
2447 freebsd32_cpuset_getid(struct thread *td,
2448 struct freebsd32_cpuset_getid_args *uap)
2450 struct cpuset_getid_args ap;
2452 ap.level = uap->level;
2453 ap.which = uap->which;
2454 ap.id = PAIR32TO64(id_t,uap->id);
2455 ap.setid = uap->setid;
2457 return (sys_cpuset_getid(td, &ap));
2461 freebsd32_cpuset_getaffinity(struct thread *td,
2462 struct freebsd32_cpuset_getaffinity_args *uap)
2464 struct cpuset_getaffinity_args ap;
2466 ap.level = uap->level;
2467 ap.which = uap->which;
2468 ap.id = PAIR32TO64(id_t,uap->id);
2469 ap.cpusetsize = uap->cpusetsize;
2470 ap.mask = uap->mask;
2472 return (sys_cpuset_getaffinity(td, &ap));
2476 freebsd32_cpuset_setaffinity(struct thread *td,
2477 struct freebsd32_cpuset_setaffinity_args *uap)
2479 struct cpuset_setaffinity_args ap;
2481 ap.level = uap->level;
2482 ap.which = uap->which;
2483 ap.id = PAIR32TO64(id_t,uap->id);
2484 ap.cpusetsize = uap->cpusetsize;
2485 ap.mask = uap->mask;
2487 return (sys_cpuset_setaffinity(td, &ap));
2491 freebsd32_nmount(struct thread *td,
2492 struct freebsd32_nmount_args /* {
2494 unsigned int iovcnt;
2503 * Mount flags are now 64-bits. On 32-bit archtectures only
2504 * 32-bits are passed in, but from here on everything handles
2505 * 64-bit flags correctly.
2509 AUDIT_ARG_FFLAGS(flags);
2512 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2513 * userspace to set this flag, but we must filter it out if we want
2514 * MNT_UPDATE on the root file system to work.
2515 * MNT_ROOTFS should only be set by the kernel when mounting its
2518 flags &= ~MNT_ROOTFS;
2521 * check that we have an even number of iovec's
2522 * and that we have at least two options.
2524 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2527 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2530 error = vfs_donmount(td, flags, auio);
2538 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2540 struct yyy32 *p32, s32;
2541 struct yyy *p = NULL, s;
2546 error = copyin(uap->zzz, &s32, sizeof(s32));
2552 error = kern_xxx(td, p);
2557 error = copyout(&s32, p32, sizeof(s32));
2564 syscall32_register(int *offset, struct sysent *new_sysent,
2565 struct sysent *old_sysent)
2567 if (*offset == NO_SYSCALL) {
2570 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2571 if (freebsd32_sysent[i].sy_call ==
2572 (sy_call_t *)lkmnosys)
2574 if (i == SYS_MAXSYSCALL)
2577 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2579 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2580 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2583 *old_sysent = freebsd32_sysent[*offset];
2584 freebsd32_sysent[*offset] = *new_sysent;
2589 syscall32_deregister(int *offset, struct sysent *old_sysent)
2593 freebsd32_sysent[*offset] = *old_sysent;
2598 syscall32_module_handler(struct module *mod, int what, void *arg)
2600 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2606 error = syscall32_register(data->offset, data->new_sysent,
2609 /* Leave a mark so we know to safely unload below. */
2610 data->offset = NULL;
2613 ms.intval = *data->offset;
2615 module_setspecific(mod, &ms);
2618 error = data->chainevh(mod, what, data->chainarg);
2622 * MOD_LOAD failed, so just return without calling the
2623 * chained handler since we didn't pass along the MOD_LOAD
2626 if (data->offset == NULL)
2628 if (data->chainevh) {
2629 error = data->chainevh(mod, what, data->chainarg);
2633 error = syscall32_deregister(data->offset, &data->old_sysent);
2638 error = data->chainevh(mod, what, data->chainarg);
2644 syscall32_helper_register(struct syscall_helper_data *sd)
2646 struct syscall_helper_data *sd1;
2649 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2650 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2653 syscall32_helper_unregister(sd);
2656 sd1->registered = 1;
2662 syscall32_helper_unregister(struct syscall_helper_data *sd)
2664 struct syscall_helper_data *sd1;
2666 for (sd1 = sd; sd1->registered != 0; sd1++) {
2667 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2668 sd1->registered = 0;
2674 freebsd32_copyout_strings(struct image_params *imgp)
2678 char *stringp, *destp;
2679 u_int32_t *stack_base;
2680 struct freebsd32_ps_strings *arginfo;
2681 char canary[sizeof(long) * 8];
2682 int32_t pagesizes32[MAXPAGESIZES];
2683 size_t execpath_len;
2687 * Calculate string base and vector table pointers.
2688 * Also deal with signal trampoline code for this exec type.
2690 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2691 execpath_len = strlen(imgp->execpath) + 1;
2694 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2696 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2697 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2700 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
2701 roundup(execpath_len, sizeof(char *)) -
2702 roundup(sizeof(canary), sizeof(char *)) -
2703 roundup(sizeof(pagesizes32), sizeof(char *)) -
2704 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
2710 copyout(imgp->proc->p_sysent->sv_sigcode,
2711 ((caddr_t)arginfo - szsigcode), szsigcode);
2714 * Copy the image path for the rtld.
2716 if (execpath_len != 0) {
2717 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
2718 copyout(imgp->execpath, (void *)imgp->execpathp,
2723 * Prepare the canary for SSP.
2725 arc4rand(canary, sizeof(canary), 0);
2726 imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len -
2728 copyout(canary, (void *)imgp->canary, sizeof(canary));
2729 imgp->canarylen = sizeof(canary);
2732 * Prepare the pagesizes array.
2734 for (i = 0; i < MAXPAGESIZES; i++)
2735 pagesizes32[i] = (uint32_t)pagesizes[i];
2736 imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len -
2737 roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32);
2738 copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32));
2739 imgp->pagesizeslen = sizeof(pagesizes32);
2742 * If we have a valid auxargs ptr, prepare some room
2745 if (imgp->auxargs) {
2747 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
2748 * lower compatibility.
2750 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
2753 * The '+ 2' is for the null pointers at the end of each of
2754 * the arg and env vector sets,and imgp->auxarg_size is room
2755 * for argument of Runtime loader.
2757 vectp = (u_int32_t *) (destp - (imgp->args->argc +
2758 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
2762 * The '+ 2' is for the null pointers at the end of each of
2763 * the arg and env vector sets
2765 vectp = (u_int32_t *)
2766 (destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t));
2769 * vectp also becomes our initial stack base
2773 stringp = imgp->args->begin_argv;
2774 argc = imgp->args->argc;
2775 envc = imgp->args->envc;
2777 * Copy out strings - arguments and environment.
2779 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
2782 * Fill in "ps_strings" struct for ps, w, etc.
2784 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2785 suword32(&arginfo->ps_nargvstr, argc);
2788 * Fill in argument portion of vector table.
2790 for (; argc > 0; --argc) {
2791 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2792 while (*stringp++ != 0)
2797 /* a null vector table pointer separates the argp's from the envp's */
2798 suword32(vectp++, 0);
2800 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
2801 suword32(&arginfo->ps_nenvstr, envc);
2804 * Fill in environment portion of vector table.
2806 for (; envc > 0; --envc) {
2807 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2808 while (*stringp++ != 0)
2813 /* end of vector table is a null pointer */
2816 return ((register_t *)stack_base);
2820 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
2822 struct kld_file_stat stat;
2823 struct kld32_file_stat stat32;
2826 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
2829 if (version != sizeof(struct kld32_file_stat_1) &&
2830 version != sizeof(struct kld32_file_stat))
2833 error = kern_kldstat(td, uap->fileid, &stat);
2837 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
2838 CP(stat, stat32, refs);
2839 CP(stat, stat32, id);
2840 PTROUT_CP(stat, stat32, address);
2841 CP(stat, stat32, size);
2842 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
2843 return (copyout(&stat32, uap->stat, version));
2847 freebsd32_posix_fallocate(struct thread *td,
2848 struct freebsd32_posix_fallocate_args *uap)
2851 return (kern_posix_fallocate(td, uap->fd,
2852 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len)));
2856 freebsd32_posix_fadvise(struct thread *td,
2857 struct freebsd32_posix_fadvise_args *uap)
2860 return (kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
2861 PAIR32TO64(off_t, uap->len), uap->advice));