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/capsicum.h>
39 #include <sys/clock.h>
41 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/imgact.h>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
47 #include <sys/linker.h>
49 #include <sys/malloc.h>
50 #include <sys/file.h> /* Must come after sys/malloc.h */
51 #include <sys/imgact.h>
54 #include <sys/module.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/namei.h>
59 #include <sys/procctl.h>
60 #include <sys/reboot.h>
61 #include <sys/resource.h>
62 #include <sys/resourcevar.h>
63 #include <sys/selinfo.h>
64 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
65 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
66 #include <sys/signal.h>
67 #include <sys/signalvar.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
71 #include <sys/syscall.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
74 #include <sys/sysent.h>
75 #include <sys/sysproto.h>
76 #include <sys/systm.h>
78 #include <sys/unistd.h>
79 #include <sys/ucontext.h>
80 #include <sys/vnode.h>
88 #include <netinet/in.h>
92 #include <vm/vm_param.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_extern.h>
98 #include <machine/cpu.h>
99 #include <machine/elf.h>
101 #include <security/audit/audit.h>
103 #include <compat/freebsd32/freebsd32_util.h>
104 #include <compat/freebsd32/freebsd32.h>
105 #include <compat/freebsd32/freebsd32_ipc.h>
106 #include <compat/freebsd32/freebsd32_misc.h>
107 #include <compat/freebsd32/freebsd32_signal.h>
108 #include <compat/freebsd32/freebsd32_proto.h>
110 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
113 CTASSERT(sizeof(struct timeval32) == 8);
114 CTASSERT(sizeof(struct timespec32) == 8);
115 CTASSERT(sizeof(struct itimerval32) == 16);
117 CTASSERT(sizeof(struct statfs32) == 256);
119 CTASSERT(sizeof(struct rusage32) == 72);
121 CTASSERT(sizeof(struct sigaltstack32) == 12);
122 CTASSERT(sizeof(struct kevent32) == 20);
123 CTASSERT(sizeof(struct iovec32) == 8);
124 CTASSERT(sizeof(struct msghdr32) == 28);
126 CTASSERT(sizeof(struct stat32) == 208);
129 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
131 CTASSERT(sizeof(struct sigaction32) == 24);
133 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
134 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
135 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
136 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
139 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
142 TV_CP(*s, *s32, ru_utime);
143 TV_CP(*s, *s32, ru_stime);
144 CP(*s, *s32, ru_maxrss);
145 CP(*s, *s32, ru_ixrss);
146 CP(*s, *s32, ru_idrss);
147 CP(*s, *s32, ru_isrss);
148 CP(*s, *s32, ru_minflt);
149 CP(*s, *s32, ru_majflt);
150 CP(*s, *s32, ru_nswap);
151 CP(*s, *s32, ru_inblock);
152 CP(*s, *s32, ru_oublock);
153 CP(*s, *s32, ru_msgsnd);
154 CP(*s, *s32, ru_msgrcv);
155 CP(*s, *s32, ru_nsignals);
156 CP(*s, *s32, ru_nvcsw);
157 CP(*s, *s32, ru_nivcsw);
161 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
164 struct rusage32 ru32;
165 struct rusage ru, *rup;
167 if (uap->rusage != NULL)
171 error = kern_wait(td, uap->pid, &status, uap->options, rup);
174 if (uap->status != NULL)
175 error = copyout(&status, uap->status, sizeof(status));
176 if (uap->rusage != NULL && error == 0) {
177 freebsd32_rusage_out(&ru, &ru32);
178 error = copyout(&ru32, uap->rusage, sizeof(ru32));
184 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
186 struct wrusage32 wru32;
187 struct __wrusage wru, *wrup;
188 struct siginfo32 si32;
189 struct __siginfo si, *sip;
192 if (uap->wrusage != NULL)
196 if (uap->info != NULL) {
198 bzero(sip, sizeof(*sip));
201 error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
202 &status, uap->options, wrup, sip);
205 if (uap->status != NULL)
206 error = copyout(&status, uap->status, sizeof(status));
207 if (uap->wrusage != NULL && error == 0) {
208 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
209 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
210 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
212 if (uap->info != NULL && error == 0) {
213 siginfo_to_siginfo32 (&si, &si32);
214 error = copyout(&si32, uap->info, sizeof(si32));
219 #ifdef COMPAT_FREEBSD4
221 copy_statfs(struct statfs *in, struct statfs32 *out)
224 statfs_scale_blocks(in, INT32_MAX);
225 bzero(out, sizeof(*out));
226 CP(*in, *out, f_bsize);
227 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
228 CP(*in, *out, f_blocks);
229 CP(*in, *out, f_bfree);
230 CP(*in, *out, f_bavail);
231 out->f_files = MIN(in->f_files, INT32_MAX);
232 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
233 CP(*in, *out, f_fsid);
234 CP(*in, *out, f_owner);
235 CP(*in, *out, f_type);
236 CP(*in, *out, f_flags);
237 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
238 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
239 strlcpy(out->f_fstypename,
240 in->f_fstypename, MFSNAMELEN);
241 strlcpy(out->f_mntonname,
242 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
243 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
244 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
245 strlcpy(out->f_mntfromname,
246 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
250 #ifdef COMPAT_FREEBSD4
252 freebsd4_freebsd32_getfsstat(struct thread *td,
253 struct freebsd4_freebsd32_getfsstat_args *uap)
255 struct statfs *buf, *sp;
256 struct statfs32 stat32;
257 size_t count, size, copycount;
260 count = uap->bufsize / sizeof(struct statfs32);
261 size = count * sizeof(struct statfs);
262 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
266 while (copycount > 0 && error == 0) {
267 copy_statfs(sp, &stat32);
268 error = copyout(&stat32, uap->buf, sizeof(stat32));
276 td->td_retval[0] = count;
281 #ifdef COMPAT_FREEBSD10
283 freebsd10_freebsd32_pipe(struct thread *td,
284 struct freebsd10_freebsd32_pipe_args *uap) {
286 return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
291 freebsd32_sigaltstack(struct thread *td,
292 struct freebsd32_sigaltstack_args *uap)
294 struct sigaltstack32 s32;
295 struct sigaltstack ss, oss, *ssp;
298 if (uap->ss != NULL) {
299 error = copyin(uap->ss, &s32, sizeof(s32));
302 PTRIN_CP(s32, ss, ss_sp);
303 CP(s32, ss, ss_size);
304 CP(s32, ss, ss_flags);
308 error = kern_sigaltstack(td, ssp, &oss);
309 if (error == 0 && uap->oss != NULL) {
310 PTROUT_CP(oss, s32, ss_sp);
311 CP(oss, s32, ss_size);
312 CP(oss, s32, ss_flags);
313 error = copyout(&s32, uap->oss, sizeof(s32));
319 * Custom version of exec_copyin_args() so that we can translate
323 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
324 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
331 bzero(args, sizeof(*args));
336 * Allocate demand-paged memory for the file name, argument, and
337 * environment strings.
339 error = exec_alloc_args(args);
344 * Copy the file name.
347 args->fname = args->buf;
348 error = (segflg == UIO_SYSSPACE) ?
349 copystr(fname, args->fname, PATH_MAX, &length) :
350 copyinstr(fname, args->fname, PATH_MAX, &length);
356 args->begin_argv = args->buf + length;
357 args->endp = args->begin_argv;
358 args->stringspace = ARG_MAX;
361 * extract arguments first
365 error = copyin(p32++, &arg, sizeof(arg));
371 error = copyinstr(argp, args->endp, args->stringspace, &length);
373 if (error == ENAMETOOLONG)
377 args->stringspace -= length;
378 args->endp += length;
382 args->begin_envv = args->endp;
385 * extract environment strings
390 error = copyin(p32++, &arg, sizeof(arg));
396 error = copyinstr(envp, args->endp, args->stringspace,
399 if (error == ENAMETOOLONG)
403 args->stringspace -= length;
404 args->endp += length;
412 exec_free_args(args);
417 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
419 struct image_args eargs;
420 struct vmspace *oldvmspace;
423 error = pre_execve(td, &oldvmspace);
426 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
427 uap->argv, uap->envv);
429 error = kern_execve(td, &eargs, NULL);
430 post_execve(td, error, oldvmspace);
435 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
437 struct image_args eargs;
438 struct vmspace *oldvmspace;
441 error = pre_execve(td, &oldvmspace);
444 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
445 uap->argv, uap->envv);
448 error = kern_execve(td, &eargs, NULL);
450 post_execve(td, error, oldvmspace);
454 #if defined(COMPAT_FREEBSD11)
456 freebsd11_freebsd32_mknod(struct thread *td,
457 struct freebsd11_freebsd32_mknod_args *uap)
460 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->mode,
465 freebsd11_freebsd32_mknodat(struct thread *td,
466 struct freebsd11_freebsd32_mknodat_args *uap)
469 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
472 #endif /* COMPAT_FREEBSD11 */
475 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
480 #if defined(__amd64__)
481 if (i386_read_exec && (prot & PROT_READ) != 0)
484 return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
489 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
494 #if defined(__amd64__)
495 if (i386_read_exec && (prot & PROT_READ))
499 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
500 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
503 #ifdef COMPAT_FREEBSD6
505 freebsd6_freebsd32_mmap(struct thread *td,
506 struct freebsd6_freebsd32_mmap_args *uap)
511 #if defined(__amd64__)
512 if (i386_read_exec && (prot & PROT_READ))
516 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
517 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
522 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
524 struct itimerval itv, oitv, *itvp;
525 struct itimerval32 i32;
528 if (uap->itv != NULL) {
529 error = copyin(uap->itv, &i32, sizeof(i32));
532 TV_CP(i32, itv, it_interval);
533 TV_CP(i32, itv, it_value);
537 error = kern_setitimer(td, uap->which, itvp, &oitv);
538 if (error || uap->oitv == NULL)
540 TV_CP(oitv, i32, it_interval);
541 TV_CP(oitv, i32, it_value);
542 return (copyout(&i32, uap->oitv, sizeof(i32)));
546 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
548 struct itimerval itv;
549 struct itimerval32 i32;
552 error = kern_getitimer(td, uap->which, &itv);
553 if (error || uap->itv == NULL)
555 TV_CP(itv, i32, it_interval);
556 TV_CP(itv, i32, it_value);
557 return (copyout(&i32, uap->itv, sizeof(i32)));
561 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
563 struct timeval32 tv32;
564 struct timeval tv, *tvp;
567 if (uap->tv != NULL) {
568 error = copyin(uap->tv, &tv32, sizeof(tv32));
571 CP(tv32, tv, tv_sec);
572 CP(tv32, tv, tv_usec);
577 * XXX Do pointers need PTRIN()?
579 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
580 sizeof(int32_t) * 8));
584 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
586 struct timespec32 ts32;
588 struct timeval tv, *tvp;
592 if (uap->ts != NULL) {
593 error = copyin(uap->ts, &ts32, sizeof(ts32));
596 CP(ts32, ts, tv_sec);
597 CP(ts32, ts, tv_nsec);
598 TIMESPEC_TO_TIMEVAL(&tv, &ts);
602 if (uap->sm != NULL) {
603 error = copyin(uap->sm, &set, sizeof(set));
610 * XXX Do pointers need PTRIN()?
612 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
613 uset, sizeof(int32_t) * 8);
618 * Copy 'count' items into the destination list pointed to by uap->eventlist.
621 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
623 struct freebsd32_kevent_args *uap;
624 struct kevent32 ks32[KQ_NEVENTS];
627 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
628 uap = (struct freebsd32_kevent_args *)arg;
630 for (i = 0; i < count; i++) {
631 CP(kevp[i], ks32[i], ident);
632 CP(kevp[i], ks32[i], filter);
633 CP(kevp[i], ks32[i], flags);
634 CP(kevp[i], ks32[i], fflags);
635 CP(kevp[i], ks32[i], data);
636 PTROUT_CP(kevp[i], ks32[i], udata);
638 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
640 uap->eventlist += count;
645 * Copy 'count' items from the list pointed to by uap->changelist.
648 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
650 struct freebsd32_kevent_args *uap;
651 struct kevent32 ks32[KQ_NEVENTS];
654 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
655 uap = (struct freebsd32_kevent_args *)arg;
657 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
660 uap->changelist += count;
662 for (i = 0; i < count; i++) {
663 CP(ks32[i], kevp[i], ident);
664 CP(ks32[i], kevp[i], filter);
665 CP(ks32[i], kevp[i], flags);
666 CP(ks32[i], kevp[i], fflags);
667 CP(ks32[i], kevp[i], data);
668 PTRIN_CP(ks32[i], kevp[i], udata);
675 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
677 struct timespec32 ts32;
678 struct timespec ts, *tsp;
679 struct kevent_copyops k_ops = {
681 .k_copyout = freebsd32_kevent_copyout,
682 .k_copyin = freebsd32_kevent_copyin,
688 error = copyin(uap->timeout, &ts32, sizeof(ts32));
691 CP(ts32, ts, tv_sec);
692 CP(ts32, ts, tv_nsec);
696 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
702 freebsd32_gettimeofday(struct thread *td,
703 struct freebsd32_gettimeofday_args *uap)
706 struct timeval32 atv32;
712 CP(atv, atv32, tv_sec);
713 CP(atv, atv32, tv_usec);
714 error = copyout(&atv32, uap->tp, sizeof (atv32));
716 if (error == 0 && uap->tzp != NULL) {
717 rtz.tz_minuteswest = tz_minuteswest;
718 rtz.tz_dsttime = tz_dsttime;
719 error = copyout(&rtz, uap->tzp, sizeof (rtz));
725 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
731 error = kern_getrusage(td, uap->who, &s);
734 if (uap->rusage != NULL) {
735 freebsd32_rusage_out(&s, &s32);
736 error = copyout(&s32, uap->rusage, sizeof(s32));
742 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
744 struct iovec32 iov32;
751 if (iovcnt > UIO_MAXIOV)
753 iovlen = iovcnt * sizeof(struct iovec);
754 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
755 iov = (struct iovec *)(uio + 1);
756 for (i = 0; i < iovcnt; i++) {
757 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
762 iov[i].iov_base = PTRIN(iov32.iov_base);
763 iov[i].iov_len = iov32.iov_len;
766 uio->uio_iovcnt = iovcnt;
767 uio->uio_segflg = UIO_USERSPACE;
768 uio->uio_offset = -1;
770 for (i = 0; i < iovcnt; i++) {
771 if (iov->iov_len > INT_MAX - uio->uio_resid) {
775 uio->uio_resid += iov->iov_len;
783 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
788 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
791 error = kern_readv(td, uap->fd, auio);
797 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
802 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
805 error = kern_writev(td, uap->fd, auio);
811 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
816 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
819 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
825 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
830 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
833 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
839 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
842 struct iovec32 iov32;
848 if (iovcnt > UIO_MAXIOV)
850 iovlen = iovcnt * sizeof(struct iovec);
851 iov = malloc(iovlen, M_IOV, M_WAITOK);
852 for (i = 0; i < iovcnt; i++) {
853 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
858 iov[i].iov_base = PTRIN(iov32.iov_base);
859 iov[i].iov_len = iov32.iov_len;
866 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
871 error = copyin(msg32, &m32, sizeof(m32));
874 msg->msg_name = PTRIN(m32.msg_name);
875 msg->msg_namelen = m32.msg_namelen;
876 msg->msg_iov = PTRIN(m32.msg_iov);
877 msg->msg_iovlen = m32.msg_iovlen;
878 msg->msg_control = PTRIN(m32.msg_control);
879 msg->msg_controllen = m32.msg_controllen;
880 msg->msg_flags = m32.msg_flags;
885 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
890 m32.msg_name = PTROUT(msg->msg_name);
891 m32.msg_namelen = msg->msg_namelen;
892 m32.msg_iov = PTROUT(msg->msg_iov);
893 m32.msg_iovlen = msg->msg_iovlen;
894 m32.msg_control = PTROUT(msg->msg_control);
895 m32.msg_controllen = msg->msg_controllen;
896 m32.msg_flags = msg->msg_flags;
897 error = copyout(&m32, msg32, sizeof(m32));
902 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
904 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
906 #define FREEBSD32_ALIGN(p) \
907 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
908 #define FREEBSD32_CMSG_SPACE(l) \
909 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
911 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
912 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
914 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
918 socklen_t clen, datalen;
921 int len, maxlen, copylen;
925 len = msg->msg_controllen;
926 maxlen = msg->msg_controllen;
927 msg->msg_controllen = 0;
930 ctlbuf = msg->msg_control;
932 while (m && len > 0) {
933 cm = mtod(m, struct cmsghdr *);
938 if (sizeof(struct cmsghdr) > clen ||
939 cm->cmsg_len > clen) {
944 data = CMSG_DATA(cm);
945 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
947 /* Adjust message length */
948 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
953 copylen = sizeof(struct cmsghdr);
955 msg->msg_flags |= MSG_CTRUNC;
959 error = copyout(cm,ctlbuf,copylen);
963 ctlbuf += FREEBSD32_ALIGN(copylen);
964 len -= FREEBSD32_ALIGN(copylen);
972 msg->msg_flags |= MSG_CTRUNC;
976 error = copyout(data,ctlbuf,copylen);
980 ctlbuf += FREEBSD32_ALIGN(copylen);
981 len -= FREEBSD32_ALIGN(copylen);
983 if (CMSG_SPACE(datalen) < clen) {
984 clen -= CMSG_SPACE(datalen);
985 cm = (struct cmsghdr *)
986 ((caddr_t)cm + CMSG_SPACE(datalen));
995 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1003 freebsd32_recvmsg(td, uap)
1005 struct freebsd32_recvmsg_args /* {
1007 struct msghdr32 *msg;
1012 struct msghdr32 m32;
1013 struct iovec *uiov, *iov;
1014 struct mbuf *control = NULL;
1015 struct mbuf **controlp;
1018 error = copyin(uap->msg, &m32, sizeof(m32));
1021 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1024 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1028 msg.msg_flags = uap->flags;
1032 controlp = (msg.msg_control != NULL) ? &control : NULL;
1033 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1037 if (control != NULL)
1038 error = freebsd32_copy_msg_out(&msg, control);
1040 msg.msg_controllen = 0;
1043 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1047 if (control != NULL)
1054 * Copy-in the array of control messages constructed using alignment
1055 * and padding suitable for a 32-bit environment and construct an
1056 * mbuf using alignment and padding suitable for a 64-bit kernel.
1057 * The alignment and padding are defined indirectly by CMSG_DATA(),
1058 * CMSG_SPACE() and CMSG_LEN().
1061 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1065 u_int idx, len, msglen;
1068 buflen = FREEBSD32_ALIGN(buflen);
1070 if (buflen > MCLBYTES)
1074 * Iterate over the buffer and get the length of each message
1075 * in there. This has 32-bit alignment and padding. Use it to
1076 * determine the length of these messages when using 64-bit
1077 * alignment and padding.
1081 while (idx < buflen) {
1082 error = copyin(buf + idx, &msglen, sizeof(msglen));
1085 if (msglen < sizeof(struct cmsghdr))
1087 msglen = FREEBSD32_ALIGN(msglen);
1088 if (idx + msglen > buflen)
1091 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1092 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1093 len += CMSG_ALIGN(msglen);
1099 m = m_get(M_WAITOK, MT_CONTROL);
1101 MCLGET(m, M_WAITOK);
1104 md = mtod(m, void *);
1105 while (buflen > 0) {
1106 error = copyin(buf, md, sizeof(struct cmsghdr));
1109 msglen = *(u_int *)md;
1110 msglen = FREEBSD32_ALIGN(msglen);
1112 /* Modify the message length to account for alignment. */
1113 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1114 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1116 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1117 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1118 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1120 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1122 error = copyin(buf, md, msglen);
1125 md = (char *)md + CMSG_ALIGN(msglen);
1139 freebsd32_sendmsg(struct thread *td,
1140 struct freebsd32_sendmsg_args *uap)
1143 struct msghdr32 m32;
1145 struct mbuf *control = NULL;
1146 struct sockaddr *to = NULL;
1149 error = copyin(uap->msg, &m32, sizeof(m32));
1152 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1155 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1160 if (msg.msg_name != NULL) {
1161 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1169 if (msg.msg_control) {
1170 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1175 error = freebsd32_copyin_control(&control, msg.msg_control,
1176 msg.msg_controllen);
1180 msg.msg_control = NULL;
1181 msg.msg_controllen = 0;
1184 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1195 freebsd32_recvfrom(struct thread *td,
1196 struct freebsd32_recvfrom_args *uap)
1202 if (uap->fromlenaddr) {
1203 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1204 sizeof(msg.msg_namelen));
1208 msg.msg_namelen = 0;
1211 msg.msg_name = PTRIN(uap->from);
1212 msg.msg_iov = &aiov;
1214 aiov.iov_base = PTRIN(uap->buf);
1215 aiov.iov_len = uap->len;
1216 msg.msg_control = NULL;
1217 msg.msg_flags = uap->flags;
1218 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1219 if (error == 0 && uap->fromlenaddr)
1220 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1221 sizeof (msg.msg_namelen));
1226 freebsd32_settimeofday(struct thread *td,
1227 struct freebsd32_settimeofday_args *uap)
1229 struct timeval32 tv32;
1230 struct timeval tv, *tvp;
1231 struct timezone tz, *tzp;
1235 error = copyin(uap->tv, &tv32, sizeof(tv32));
1238 CP(tv32, tv, tv_sec);
1239 CP(tv32, tv, tv_usec);
1244 error = copyin(uap->tzp, &tz, sizeof(tz));
1250 return (kern_settimeofday(td, tvp, tzp));
1254 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1256 struct timeval32 s32[2];
1257 struct timeval s[2], *sp;
1260 if (uap->tptr != NULL) {
1261 error = copyin(uap->tptr, s32, sizeof(s32));
1264 CP(s32[0], s[0], tv_sec);
1265 CP(s32[0], s[0], tv_usec);
1266 CP(s32[1], s[1], tv_sec);
1267 CP(s32[1], s[1], tv_usec);
1271 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1276 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1278 struct timeval32 s32[2];
1279 struct timeval s[2], *sp;
1282 if (uap->tptr != NULL) {
1283 error = copyin(uap->tptr, s32, sizeof(s32));
1286 CP(s32[0], s[0], tv_sec);
1287 CP(s32[0], s[0], tv_usec);
1288 CP(s32[1], s[1], tv_sec);
1289 CP(s32[1], s[1], tv_usec);
1293 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1297 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1299 struct timeval32 s32[2];
1300 struct timeval s[2], *sp;
1303 if (uap->tptr != NULL) {
1304 error = copyin(uap->tptr, s32, sizeof(s32));
1307 CP(s32[0], s[0], tv_sec);
1308 CP(s32[0], s[0], tv_usec);
1309 CP(s32[1], s[1], tv_sec);
1310 CP(s32[1], s[1], tv_usec);
1314 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1318 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1320 struct timeval32 s32[2];
1321 struct timeval s[2], *sp;
1324 if (uap->times != NULL) {
1325 error = copyin(uap->times, s32, sizeof(s32));
1328 CP(s32[0], s[0], tv_sec);
1329 CP(s32[0], s[0], tv_usec);
1330 CP(s32[1], s[1], tv_sec);
1331 CP(s32[1], s[1], tv_usec);
1335 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1340 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1342 struct timespec32 ts32[2];
1343 struct timespec ts[2], *tsp;
1346 if (uap->times != NULL) {
1347 error = copyin(uap->times, ts32, sizeof(ts32));
1350 CP(ts32[0], ts[0], tv_sec);
1351 CP(ts32[0], ts[0], tv_nsec);
1352 CP(ts32[1], ts[1], tv_sec);
1353 CP(ts32[1], ts[1], tv_nsec);
1357 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1361 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1363 struct timespec32 ts32[2];
1364 struct timespec ts[2], *tsp;
1367 if (uap->times != NULL) {
1368 error = copyin(uap->times, ts32, sizeof(ts32));
1371 CP(ts32[0], ts[0], tv_sec);
1372 CP(ts32[0], ts[0], tv_nsec);
1373 CP(ts32[1], ts[1], tv_sec);
1374 CP(ts32[1], ts[1], tv_nsec);
1378 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1379 tsp, UIO_SYSSPACE, uap->flag));
1383 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1385 struct timeval32 tv32;
1386 struct timeval delta, olddelta, *deltap;
1390 error = copyin(uap->delta, &tv32, sizeof(tv32));
1393 CP(tv32, delta, tv_sec);
1394 CP(tv32, delta, tv_usec);
1398 error = kern_adjtime(td, deltap, &olddelta);
1399 if (uap->olddelta && error == 0) {
1400 CP(olddelta, tv32, tv_sec);
1401 CP(olddelta, tv32, tv_usec);
1402 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1407 #ifdef COMPAT_FREEBSD4
1409 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1411 struct statfs32 s32;
1415 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1416 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1418 copy_statfs(sp, &s32);
1419 error = copyout(&s32, uap->buf, sizeof(s32));
1426 #ifdef COMPAT_FREEBSD4
1428 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1430 struct statfs32 s32;
1434 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1435 error = kern_fstatfs(td, uap->fd, sp);
1437 copy_statfs(sp, &s32);
1438 error = copyout(&s32, uap->buf, sizeof(s32));
1445 #ifdef COMPAT_FREEBSD4
1447 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1449 struct statfs32 s32;
1454 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1456 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1457 error = kern_fhstatfs(td, fh, sp);
1459 copy_statfs(sp, &s32);
1460 error = copyout(&s32, uap->buf, sizeof(s32));
1468 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1471 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1472 PAIR32TO64(off_t, uap->offset)));
1476 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1479 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1480 PAIR32TO64(off_t, uap->offset)));
1485 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1488 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1493 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1498 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1500 /* Expand the quad return into two parts for eax and edx */
1501 pos = td->td_uretoff.tdu_off;
1502 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1503 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1508 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1511 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1512 PAIR32TO64(off_t, uap->length)));
1516 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1519 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1524 ofreebsd32_getdirentries(struct thread *td,
1525 struct ofreebsd32_getdirentries_args *uap)
1527 struct ogetdirentries_args ap;
1534 ap.count = uap->count;
1536 error = kern_ogetdirentries(td, &ap, &loff);
1539 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1545 #if defined(COMPAT_FREEBSD11)
1547 freebsd11_freebsd32_getdirentries(struct thread *td,
1548 struct freebsd11_freebsd32_getdirentries_args *uap)
1554 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1558 if (uap->basep != NULL) {
1560 error = copyout(&base32, uap->basep, sizeof(int32_t));
1566 freebsd11_freebsd32_getdents(struct thread *td,
1567 struct freebsd11_freebsd32_getdents_args *uap)
1569 struct freebsd11_freebsd32_getdirentries_args ap;
1573 ap.count = uap->count;
1575 return (freebsd11_freebsd32_getdirentries(td, &ap));
1577 #endif /* COMPAT_FREEBSD11 */
1580 freebsd32_getdirentries(struct thread *td,
1581 struct freebsd32_getdirentries_args *uap)
1587 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1588 NULL, UIO_USERSPACE);
1591 if (uap->basep != NULL) {
1593 error = copyout(&base32, uap->basep, sizeof(int32_t));
1598 #ifdef COMPAT_FREEBSD6
1599 /* versions with the 'int pad' argument */
1601 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1604 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1605 PAIR32TO64(off_t, uap->offset)));
1609 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1612 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1613 PAIR32TO64(off_t, uap->offset)));
1617 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1622 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1624 /* Expand the quad return into two parts for eax and edx */
1625 pos = *(off_t *)(td->td_retval);
1626 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1627 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1632 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1635 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1636 PAIR32TO64(off_t, uap->length)));
1640 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1643 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1645 #endif /* COMPAT_FREEBSD6 */
1655 freebsd32_do_sendfile(struct thread *td,
1656 struct freebsd32_sendfile_args *uap, int compat)
1658 struct sf_hdtr32 hdtr32;
1659 struct sf_hdtr hdtr;
1660 struct uio *hdr_uio, *trl_uio;
1662 cap_rights_t rights;
1663 struct iovec32 *iov32;
1664 off_t offset, sbytes;
1667 offset = PAIR32TO64(off_t, uap->offset);
1671 hdr_uio = trl_uio = NULL;
1673 if (uap->hdtr != NULL) {
1674 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1677 PTRIN_CP(hdtr32, hdtr, headers);
1678 CP(hdtr32, hdtr, hdr_cnt);
1679 PTRIN_CP(hdtr32, hdtr, trailers);
1680 CP(hdtr32, hdtr, trl_cnt);
1682 if (hdtr.headers != NULL) {
1683 iov32 = PTRIN(hdtr32.headers);
1684 error = freebsd32_copyinuio(iov32,
1685 hdtr32.hdr_cnt, &hdr_uio);
1688 #ifdef COMPAT_FREEBSD4
1690 * In FreeBSD < 5.0 the nbytes to send also included
1691 * the header. If compat is specified subtract the
1692 * header size from nbytes.
1695 if (uap->nbytes > hdr_uio->uio_resid)
1696 uap->nbytes -= hdr_uio->uio_resid;
1702 if (hdtr.trailers != NULL) {
1703 iov32 = PTRIN(hdtr32.trailers);
1704 error = freebsd32_copyinuio(iov32,
1705 hdtr32.trl_cnt, &trl_uio);
1711 AUDIT_ARG_FD(uap->fd);
1713 if ((error = fget_read(td, uap->fd,
1714 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1717 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1718 uap->nbytes, &sbytes, uap->flags, td);
1721 if (uap->sbytes != NULL)
1722 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1726 free(hdr_uio, M_IOV);
1728 free(trl_uio, M_IOV);
1732 #ifdef COMPAT_FREEBSD4
1734 freebsd4_freebsd32_sendfile(struct thread *td,
1735 struct freebsd4_freebsd32_sendfile_args *uap)
1737 return (freebsd32_do_sendfile(td,
1738 (struct freebsd32_sendfile_args *)uap, 1));
1743 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1746 return (freebsd32_do_sendfile(td, uap, 0));
1750 copy_stat(struct stat *in, struct stat32 *out)
1753 CP(*in, *out, st_dev);
1754 CP(*in, *out, st_ino);
1755 CP(*in, *out, st_mode);
1756 CP(*in, *out, st_nlink);
1757 CP(*in, *out, st_uid);
1758 CP(*in, *out, st_gid);
1759 CP(*in, *out, st_rdev);
1760 TS_CP(*in, *out, st_atim);
1761 TS_CP(*in, *out, st_mtim);
1762 TS_CP(*in, *out, st_ctim);
1763 CP(*in, *out, st_size);
1764 CP(*in, *out, st_blocks);
1765 CP(*in, *out, st_blksize);
1766 CP(*in, *out, st_flags);
1767 CP(*in, *out, st_gen);
1768 TS_CP(*in, *out, st_birthtim);
1769 out->st_padding0 = 0;
1770 out->st_padding1 = 0;
1771 #ifdef __STAT32_TIME_T_EXT
1772 out->st_atim_ext = 0;
1773 out->st_mtim_ext = 0;
1774 out->st_ctim_ext = 0;
1775 out->st_btim_ext = 0;
1777 bzero(out->st_spare, sizeof(out->st_spare));
1782 copy_ostat(struct stat *in, struct ostat32 *out)
1785 CP(*in, *out, st_dev);
1786 CP(*in, *out, st_ino);
1787 CP(*in, *out, st_mode);
1788 CP(*in, *out, st_nlink);
1789 CP(*in, *out, st_uid);
1790 CP(*in, *out, st_gid);
1791 CP(*in, *out, st_rdev);
1792 CP(*in, *out, st_size);
1793 TS_CP(*in, *out, st_atim);
1794 TS_CP(*in, *out, st_mtim);
1795 TS_CP(*in, *out, st_ctim);
1796 CP(*in, *out, st_blksize);
1797 CP(*in, *out, st_blocks);
1798 CP(*in, *out, st_flags);
1799 CP(*in, *out, st_gen);
1805 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1808 struct ostat32 sb32;
1811 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1815 copy_ostat(&sb, &sb32);
1816 error = copyout(&sb32, uap->ub, sizeof (sb32));
1822 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1828 error = kern_fstat(td, uap->fd, &ub);
1831 copy_stat(&ub, &ub32);
1832 error = copyout(&ub32, uap->ub, sizeof(ub32));
1838 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1841 struct ostat32 ub32;
1844 error = kern_fstat(td, uap->fd, &ub);
1847 copy_ostat(&ub, &ub32);
1848 error = copyout(&ub32, uap->ub, sizeof(ub32));
1854 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1860 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1864 copy_stat(&ub, &ub32);
1865 error = copyout(&ub32, uap->buf, sizeof(ub32));
1871 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1874 struct ostat32 sb32;
1877 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1878 UIO_USERSPACE, &sb, NULL);
1881 copy_ostat(&sb, &sb32);
1882 error = copyout(&sb32, uap->ub, sizeof (sb32));
1888 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
1895 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
1898 error = kern_fhstat(td, fh, &sb);
1901 copy_stat(&sb, &sb32);
1902 error = copyout(&sb32, uap->sb, sizeof (sb32));
1906 #if defined(COMPAT_FREEBSD11)
1908 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
1911 CP(*in, *out, st_ino);
1912 CP(*in, *out, st_nlink);
1913 CP(*in, *out, st_dev);
1914 CP(*in, *out, st_mode);
1915 CP(*in, *out, st_uid);
1916 CP(*in, *out, st_gid);
1917 CP(*in, *out, st_rdev);
1918 TS_CP(*in, *out, st_atim);
1919 TS_CP(*in, *out, st_mtim);
1920 TS_CP(*in, *out, st_ctim);
1921 CP(*in, *out, st_size);
1922 CP(*in, *out, st_blocks);
1923 CP(*in, *out, st_blksize);
1924 CP(*in, *out, st_flags);
1925 CP(*in, *out, st_gen);
1926 TS_CP(*in, *out, st_birthtim);
1928 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
1929 sizeof(*out) - offsetof(struct freebsd11_stat32,
1930 st_birthtim) - sizeof(out->st_birthtim));
1934 freebsd11_freebsd32_stat(struct thread *td,
1935 struct freebsd11_freebsd32_stat_args *uap)
1938 struct freebsd11_stat32 sb32;
1941 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1945 freebsd11_cvtstat32(&sb, &sb32);
1946 error = copyout(&sb32, uap->ub, sizeof (sb32));
1951 freebsd11_freebsd32_fstat(struct thread *td,
1952 struct freebsd11_freebsd32_fstat_args *uap)
1955 struct freebsd11_stat32 sb32;
1958 error = kern_fstat(td, uap->fd, &sb);
1961 freebsd11_cvtstat32(&sb, &sb32);
1962 error = copyout(&sb32, uap->ub, sizeof (sb32));
1967 freebsd11_freebsd32_fstatat(struct thread *td,
1968 struct freebsd11_freebsd32_fstatat_args *uap)
1971 struct freebsd11_stat32 sb32;
1974 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1978 freebsd11_cvtstat32(&sb, &sb32);
1979 error = copyout(&sb32, uap->buf, sizeof (sb32));
1984 freebsd11_freebsd32_lstat(struct thread *td,
1985 struct freebsd11_freebsd32_lstat_args *uap)
1988 struct freebsd11_stat32 sb32;
1991 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1992 UIO_USERSPACE, &sb, NULL);
1995 freebsd11_cvtstat32(&sb, &sb32);
1996 error = copyout(&sb32, uap->ub, sizeof (sb32));
2001 freebsd11_freebsd32_fhstat(struct thread *td,
2002 struct freebsd11_freebsd32_fhstat_args *uap)
2005 struct freebsd11_stat32 sb32;
2009 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2012 error = kern_fhstat(td, fh, &sb);
2015 freebsd11_cvtstat32(&sb, &sb32);
2016 error = copyout(&sb32, uap->sb, sizeof (sb32));
2022 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2024 int error, name[CTL_MAXNAME];
2028 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2030 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2034 error = fueword32(uap->oldlenp, &tmp);
2041 error = userland_sysctl(td, name, uap->namelen,
2042 uap->old, &oldlen, 1,
2043 uap->new, uap->newlen, &j, SCTL_MASK32);
2044 if (error && error != ENOMEM)
2047 suword32(uap->oldlenp, j);
2052 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2058 error = copyin(uap->jail, &version, sizeof(uint32_t));
2065 /* FreeBSD single IPv4 jails. */
2066 struct jail32_v0 j32_v0;
2068 bzero(&j, sizeof(struct jail));
2069 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2072 CP(j32_v0, j, version);
2073 PTRIN_CP(j32_v0, j, path);
2074 PTRIN_CP(j32_v0, j, hostname);
2075 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2081 * Version 1 was used by multi-IPv4 jail implementations
2082 * that never made it into the official kernel.
2086 case 2: /* JAIL_API_VERSION */
2088 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2091 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2094 CP(j32, j, version);
2095 PTRIN_CP(j32, j, path);
2096 PTRIN_CP(j32, j, hostname);
2097 PTRIN_CP(j32, j, jailname);
2100 PTRIN_CP(j32, j, ip4);
2101 PTRIN_CP(j32, j, ip6);
2106 /* Sci-Fi jails are not supported, sorry. */
2109 return (kern_jail(td, &j));
2113 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2118 /* Check that we have an even number of iovecs. */
2119 if (uap->iovcnt & 1)
2122 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2125 error = kern_jail_set(td, auio, uap->flags);
2131 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2133 struct iovec32 iov32;
2137 /* Check that we have an even number of iovecs. */
2138 if (uap->iovcnt & 1)
2141 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2144 error = kern_jail_get(td, auio, uap->flags);
2146 for (i = 0; i < uap->iovcnt; i++) {
2147 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2148 CP(auio->uio_iov[i], iov32, iov_len);
2149 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2158 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2160 struct sigaction32 s32;
2161 struct sigaction sa, osa, *sap;
2165 error = copyin(uap->act, &s32, sizeof(s32));
2168 sa.sa_handler = PTRIN(s32.sa_u);
2169 CP(s32, sa, sa_flags);
2170 CP(s32, sa, sa_mask);
2174 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2175 if (error == 0 && uap->oact != NULL) {
2176 s32.sa_u = PTROUT(osa.sa_handler);
2177 CP(osa, s32, sa_flags);
2178 CP(osa, s32, sa_mask);
2179 error = copyout(&s32, uap->oact, sizeof(s32));
2184 #ifdef COMPAT_FREEBSD4
2186 freebsd4_freebsd32_sigaction(struct thread *td,
2187 struct freebsd4_freebsd32_sigaction_args *uap)
2189 struct sigaction32 s32;
2190 struct sigaction sa, osa, *sap;
2194 error = copyin(uap->act, &s32, sizeof(s32));
2197 sa.sa_handler = PTRIN(s32.sa_u);
2198 CP(s32, sa, sa_flags);
2199 CP(s32, sa, sa_mask);
2203 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2204 if (error == 0 && uap->oact != NULL) {
2205 s32.sa_u = PTROUT(osa.sa_handler);
2206 CP(osa, s32, sa_flags);
2207 CP(osa, s32, sa_mask);
2208 error = copyout(&s32, uap->oact, sizeof(s32));
2215 struct osigaction32 {
2224 ofreebsd32_sigaction(struct thread *td,
2225 struct ofreebsd32_sigaction_args *uap)
2227 struct osigaction32 s32;
2228 struct sigaction sa, osa, *sap;
2231 if (uap->signum <= 0 || uap->signum >= ONSIG)
2235 error = copyin(uap->nsa, &s32, sizeof(s32));
2238 sa.sa_handler = PTRIN(s32.sa_u);
2239 CP(s32, sa, sa_flags);
2240 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2244 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2245 if (error == 0 && uap->osa != NULL) {
2246 s32.sa_u = PTROUT(osa.sa_handler);
2247 CP(osa, s32, sa_flags);
2248 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2249 error = copyout(&s32, uap->osa, sizeof(s32));
2255 ofreebsd32_sigprocmask(struct thread *td,
2256 struct ofreebsd32_sigprocmask_args *uap)
2261 OSIG2SIG(uap->mask, set);
2262 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2263 SIG2OSIG(oset, td->td_retval[0]);
2268 ofreebsd32_sigpending(struct thread *td,
2269 struct ofreebsd32_sigpending_args *uap)
2271 struct proc *p = td->td_proc;
2275 siglist = p->p_siglist;
2276 SIGSETOR(siglist, td->td_siglist);
2278 SIG2OSIG(siglist, td->td_retval[0]);
2283 u_int32_t sv_handler;
2289 ofreebsd32_sigvec(struct thread *td,
2290 struct ofreebsd32_sigvec_args *uap)
2292 struct sigvec32 vec;
2293 struct sigaction sa, osa, *sap;
2296 if (uap->signum <= 0 || uap->signum >= ONSIG)
2300 error = copyin(uap->nsv, &vec, sizeof(vec));
2303 sa.sa_handler = PTRIN(vec.sv_handler);
2304 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2305 sa.sa_flags = vec.sv_flags;
2306 sa.sa_flags ^= SA_RESTART;
2310 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2311 if (error == 0 && uap->osv != NULL) {
2312 vec.sv_handler = PTROUT(osa.sa_handler);
2313 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2314 vec.sv_flags = osa.sa_flags;
2315 vec.sv_flags &= ~SA_NOCLDWAIT;
2316 vec.sv_flags ^= SA_RESTART;
2317 error = copyout(&vec, uap->osv, sizeof(vec));
2323 ofreebsd32_sigblock(struct thread *td,
2324 struct ofreebsd32_sigblock_args *uap)
2328 OSIG2SIG(uap->mask, set);
2329 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2330 SIG2OSIG(oset, td->td_retval[0]);
2335 ofreebsd32_sigsetmask(struct thread *td,
2336 struct ofreebsd32_sigsetmask_args *uap)
2340 OSIG2SIG(uap->mask, set);
2341 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2342 SIG2OSIG(oset, td->td_retval[0]);
2347 ofreebsd32_sigsuspend(struct thread *td,
2348 struct ofreebsd32_sigsuspend_args *uap)
2352 OSIG2SIG(uap->mask, mask);
2353 return (kern_sigsuspend(td, mask));
2362 ofreebsd32_sigstack(struct thread *td,
2363 struct ofreebsd32_sigstack_args *uap)
2365 struct sigstack32 s32;
2366 struct sigstack nss, oss;
2367 int error = 0, unss;
2369 if (uap->nss != NULL) {
2370 error = copyin(uap->nss, &s32, sizeof(s32));
2373 nss.ss_sp = PTRIN(s32.ss_sp);
2374 CP(s32, nss, ss_onstack);
2379 oss.ss_sp = td->td_sigstk.ss_sp;
2380 oss.ss_onstack = sigonstack(cpu_getstack(td));
2382 td->td_sigstk.ss_sp = nss.ss_sp;
2383 td->td_sigstk.ss_size = 0;
2384 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2385 td->td_pflags |= TDP_ALTSTACK;
2387 if (uap->oss != NULL) {
2388 s32.ss_sp = PTROUT(oss.ss_sp);
2389 CP(oss, s32, ss_onstack);
2390 error = copyout(&s32, uap->oss, sizeof(s32));
2397 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2400 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2401 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2405 freebsd32_clock_nanosleep(struct thread *td,
2406 struct freebsd32_clock_nanosleep_args *uap)
2410 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2411 uap->rqtp, uap->rmtp);
2412 return (kern_posix_error(td, error));
2416 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2417 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2419 struct timespec32 rmt32, rqt32;
2420 struct timespec rmt, rqt;
2423 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2427 CP(rqt32, rqt, tv_sec);
2428 CP(rqt32, rqt, tv_nsec);
2430 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2431 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2433 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2434 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2437 CP(rmt, rmt32, tv_sec);
2438 CP(rmt, rmt32, tv_nsec);
2440 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2448 freebsd32_clock_gettime(struct thread *td,
2449 struct freebsd32_clock_gettime_args *uap)
2451 struct timespec ats;
2452 struct timespec32 ats32;
2455 error = kern_clock_gettime(td, uap->clock_id, &ats);
2457 CP(ats, ats32, tv_sec);
2458 CP(ats, ats32, tv_nsec);
2459 error = copyout(&ats32, uap->tp, sizeof(ats32));
2465 freebsd32_clock_settime(struct thread *td,
2466 struct freebsd32_clock_settime_args *uap)
2468 struct timespec ats;
2469 struct timespec32 ats32;
2472 error = copyin(uap->tp, &ats32, sizeof(ats32));
2475 CP(ats32, ats, tv_sec);
2476 CP(ats32, ats, tv_nsec);
2478 return (kern_clock_settime(td, uap->clock_id, &ats));
2482 freebsd32_clock_getres(struct thread *td,
2483 struct freebsd32_clock_getres_args *uap)
2486 struct timespec32 ts32;
2489 if (uap->tp == NULL)
2491 error = kern_clock_getres(td, uap->clock_id, &ts);
2493 CP(ts, ts32, tv_sec);
2494 CP(ts, ts32, tv_nsec);
2495 error = copyout(&ts32, uap->tp, sizeof(ts32));
2500 int freebsd32_ktimer_create(struct thread *td,
2501 struct freebsd32_ktimer_create_args *uap)
2503 struct sigevent32 ev32;
2504 struct sigevent ev, *evp;
2507 if (uap->evp == NULL) {
2511 error = copyin(uap->evp, &ev32, sizeof(ev32));
2514 error = convert_sigevent32(&ev32, &ev);
2518 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2520 error = copyout(&id, uap->timerid, sizeof(int));
2522 kern_ktimer_delete(td, id);
2528 freebsd32_ktimer_settime(struct thread *td,
2529 struct freebsd32_ktimer_settime_args *uap)
2531 struct itimerspec32 val32, oval32;
2532 struct itimerspec val, oval, *ovalp;
2535 error = copyin(uap->value, &val32, sizeof(val32));
2539 ovalp = uap->ovalue != NULL ? &oval : NULL;
2540 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2541 if (error == 0 && uap->ovalue != NULL) {
2542 ITS_CP(oval, oval32);
2543 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2549 freebsd32_ktimer_gettime(struct thread *td,
2550 struct freebsd32_ktimer_gettime_args *uap)
2552 struct itimerspec32 val32;
2553 struct itimerspec val;
2556 error = kern_ktimer_gettime(td, uap->timerid, &val);
2559 error = copyout(&val32, uap->value, sizeof(val32));
2565 freebsd32_clock_getcpuclockid2(struct thread *td,
2566 struct freebsd32_clock_getcpuclockid2_args *uap)
2571 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2572 uap->which, &clk_id);
2574 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2579 freebsd32_thr_new(struct thread *td,
2580 struct freebsd32_thr_new_args *uap)
2582 struct thr_param32 param32;
2583 struct thr_param param;
2586 if (uap->param_size < 0 ||
2587 uap->param_size > sizeof(struct thr_param32))
2589 bzero(¶m, sizeof(struct thr_param));
2590 bzero(¶m32, sizeof(struct thr_param32));
2591 error = copyin(uap->param, ¶m32, uap->param_size);
2594 param.start_func = PTRIN(param32.start_func);
2595 param.arg = PTRIN(param32.arg);
2596 param.stack_base = PTRIN(param32.stack_base);
2597 param.stack_size = param32.stack_size;
2598 param.tls_base = PTRIN(param32.tls_base);
2599 param.tls_size = param32.tls_size;
2600 param.child_tid = PTRIN(param32.child_tid);
2601 param.parent_tid = PTRIN(param32.parent_tid);
2602 param.flags = param32.flags;
2603 param.rtp = PTRIN(param32.rtp);
2604 param.spare[0] = PTRIN(param32.spare[0]);
2605 param.spare[1] = PTRIN(param32.spare[1]);
2606 param.spare[2] = PTRIN(param32.spare[2]);
2608 return (kern_thr_new(td, ¶m));
2612 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2614 struct timespec32 ts32;
2615 struct timespec ts, *tsp;
2620 if (uap->timeout != NULL) {
2621 error = copyin((const void *)uap->timeout, (void *)&ts32,
2622 sizeof(struct timespec32));
2625 ts.tv_sec = ts32.tv_sec;
2626 ts.tv_nsec = ts32.tv_nsec;
2629 return (kern_thr_suspend(td, tsp));
2633 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2635 bzero(dst, sizeof(*dst));
2636 dst->si_signo = src->si_signo;
2637 dst->si_errno = src->si_errno;
2638 dst->si_code = src->si_code;
2639 dst->si_pid = src->si_pid;
2640 dst->si_uid = src->si_uid;
2641 dst->si_status = src->si_status;
2642 dst->si_addr = (uintptr_t)src->si_addr;
2643 dst->si_value.sival_int = src->si_value.sival_int;
2644 dst->si_timerid = src->si_timerid;
2645 dst->si_overrun = src->si_overrun;
2648 #ifndef _FREEBSD32_SYSPROTO_H_
2649 struct freebsd32_sigqueue_args {
2652 /* union sigval32 */ int value;
2656 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2661 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2662 * On 64-bit little-endian ABIs, the low bits are the same.
2663 * In 64-bit big-endian ABIs, sival_int overlaps with
2664 * sival_ptr's HIGH bits. We choose to support sival_int
2665 * rather than sival_ptr in this case as it seems to be
2668 bzero(&sv, sizeof(sv));
2669 sv.sival_int = uap->value;
2671 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2675 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2677 struct timespec32 ts32;
2679 struct timespec *timeout;
2682 struct siginfo32 si32;
2686 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2689 ts.tv_sec = ts32.tv_sec;
2690 ts.tv_nsec = ts32.tv_nsec;
2695 error = copyin(uap->set, &set, sizeof(set));
2699 error = kern_sigtimedwait(td, set, &ksi, timeout);
2704 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2705 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2709 td->td_retval[0] = ksi.ksi_signo;
2717 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2720 struct siginfo32 si32;
2724 error = copyin(uap->set, &set, sizeof(set));
2728 error = kern_sigtimedwait(td, set, &ksi, NULL);
2733 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2734 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2737 td->td_retval[0] = ksi.ksi_signo;
2742 freebsd32_cpuset_setid(struct thread *td,
2743 struct freebsd32_cpuset_setid_args *uap)
2746 return (kern_cpuset_setid(td, uap->which,
2747 PAIR32TO64(id_t, uap->id), uap->setid));
2751 freebsd32_cpuset_getid(struct thread *td,
2752 struct freebsd32_cpuset_getid_args *uap)
2755 return (kern_cpuset_getid(td, uap->level, uap->which,
2756 PAIR32TO64(id_t, uap->id), uap->setid));
2760 freebsd32_cpuset_getaffinity(struct thread *td,
2761 struct freebsd32_cpuset_getaffinity_args *uap)
2764 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2765 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2769 freebsd32_cpuset_setaffinity(struct thread *td,
2770 struct freebsd32_cpuset_setaffinity_args *uap)
2773 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2774 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2778 freebsd32_nmount(struct thread *td,
2779 struct freebsd32_nmount_args /* {
2781 unsigned int iovcnt;
2790 * Mount flags are now 64-bits. On 32-bit archtectures only
2791 * 32-bits are passed in, but from here on everything handles
2792 * 64-bit flags correctly.
2796 AUDIT_ARG_FFLAGS(flags);
2799 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2800 * userspace to set this flag, but we must filter it out if we want
2801 * MNT_UPDATE on the root file system to work.
2802 * MNT_ROOTFS should only be set by the kernel when mounting its
2805 flags &= ~MNT_ROOTFS;
2808 * check that we have an even number of iovec's
2809 * and that we have at least two options.
2811 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2814 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2817 error = vfs_donmount(td, flags, auio);
2825 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2827 struct yyy32 *p32, s32;
2828 struct yyy *p = NULL, s;
2833 error = copyin(uap->zzz, &s32, sizeof(s32));
2839 error = kern_xxx(td, p);
2844 error = copyout(&s32, p32, sizeof(s32));
2851 syscall32_register(int *offset, struct sysent *new_sysent,
2852 struct sysent *old_sysent, int flags)
2855 if ((flags & ~SY_THR_STATIC) != 0)
2858 if (*offset == NO_SYSCALL) {
2861 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2862 if (freebsd32_sysent[i].sy_call ==
2863 (sy_call_t *)lkmnosys)
2865 if (i == SYS_MAXSYSCALL)
2868 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2870 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2871 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2874 *old_sysent = freebsd32_sysent[*offset];
2875 freebsd32_sysent[*offset] = *new_sysent;
2876 atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2881 syscall32_deregister(int *offset, struct sysent *old_sysent)
2887 freebsd32_sysent[*offset] = *old_sysent;
2892 syscall32_module_handler(struct module *mod, int what, void *arg)
2894 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2900 error = syscall32_register(data->offset, data->new_sysent,
2901 &data->old_sysent, SY_THR_STATIC_KLD);
2903 /* Leave a mark so we know to safely unload below. */
2904 data->offset = NULL;
2907 ms.intval = *data->offset;
2909 module_setspecific(mod, &ms);
2912 error = data->chainevh(mod, what, data->chainarg);
2916 * MOD_LOAD failed, so just return without calling the
2917 * chained handler since we didn't pass along the MOD_LOAD
2920 if (data->offset == NULL)
2922 if (data->chainevh) {
2923 error = data->chainevh(mod, what, data->chainarg);
2927 error = syscall32_deregister(data->offset, &data->old_sysent);
2932 error = data->chainevh(mod, what, data->chainarg);
2938 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2940 struct syscall_helper_data *sd1;
2943 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2944 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2945 &sd1->old_sysent, flags);
2947 syscall32_helper_unregister(sd);
2950 sd1->registered = 1;
2956 syscall32_helper_unregister(struct syscall_helper_data *sd)
2958 struct syscall_helper_data *sd1;
2960 for (sd1 = sd; sd1->registered != 0; sd1++) {
2961 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2962 sd1->registered = 0;
2968 freebsd32_copyout_strings(struct image_params *imgp)
2974 u_int32_t *stack_base;
2975 struct freebsd32_ps_strings *arginfo;
2976 char canary[sizeof(long) * 8];
2977 int32_t pagesizes32[MAXPAGESIZES];
2978 size_t execpath_len;
2982 * Calculate string base and vector table pointers.
2983 * Also deal with signal trampoline code for this exec type.
2985 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2986 execpath_len = strlen(imgp->execpath) + 1;
2989 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2991 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2992 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2995 destp = (uintptr_t)arginfo;
3000 if (szsigcode != 0) {
3002 destp = rounddown2(destp, sizeof(uint32_t));
3003 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3008 * Copy the image path for the rtld.
3010 if (execpath_len != 0) {
3011 destp -= execpath_len;
3012 imgp->execpathp = destp;
3013 copyout(imgp->execpath, (void *)destp, execpath_len);
3017 * Prepare the canary for SSP.
3019 arc4rand(canary, sizeof(canary), 0);
3020 destp -= sizeof(canary);
3021 imgp->canary = destp;
3022 copyout(canary, (void *)destp, sizeof(canary));
3023 imgp->canarylen = sizeof(canary);
3026 * Prepare the pagesizes array.
3028 for (i = 0; i < MAXPAGESIZES; i++)
3029 pagesizes32[i] = (uint32_t)pagesizes[i];
3030 destp -= sizeof(pagesizes32);
3031 destp = rounddown2(destp, sizeof(uint32_t));
3032 imgp->pagesizes = destp;
3033 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3034 imgp->pagesizeslen = sizeof(pagesizes32);
3036 destp -= ARG_MAX - imgp->args->stringspace;
3037 destp = rounddown2(destp, sizeof(uint32_t));
3040 * If we have a valid auxargs ptr, prepare some room
3043 if (imgp->auxargs) {
3045 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
3046 * lower compatibility.
3048 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
3051 * The '+ 2' is for the null pointers at the end of each of
3052 * the arg and env vector sets,and imgp->auxarg_size is room
3053 * for argument of Runtime loader.
3055 vectp = (u_int32_t *) (destp - (imgp->args->argc +
3056 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
3060 * The '+ 2' is for the null pointers at the end of each of
3061 * the arg and env vector sets
3063 vectp = (u_int32_t *)(destp - (imgp->args->argc +
3064 imgp->args->envc + 2) * sizeof(u_int32_t));
3068 * vectp also becomes our initial stack base
3072 stringp = imgp->args->begin_argv;
3073 argc = imgp->args->argc;
3074 envc = imgp->args->envc;
3076 * Copy out strings - arguments and environment.
3078 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3081 * Fill in "ps_strings" struct for ps, w, etc.
3083 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3084 suword32(&arginfo->ps_nargvstr, argc);
3087 * Fill in argument portion of vector table.
3089 for (; argc > 0; --argc) {
3090 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3091 while (*stringp++ != 0)
3096 /* a null vector table pointer separates the argp's from the envp's */
3097 suword32(vectp++, 0);
3099 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3100 suword32(&arginfo->ps_nenvstr, envc);
3103 * Fill in environment portion of vector table.
3105 for (; envc > 0; --envc) {
3106 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3107 while (*stringp++ != 0)
3112 /* end of vector table is a null pointer */
3115 return ((register_t *)stack_base);
3119 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3121 struct kld_file_stat stat;
3122 struct kld32_file_stat stat32;
3125 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3128 if (version != sizeof(struct kld32_file_stat_1) &&
3129 version != sizeof(struct kld32_file_stat))
3132 error = kern_kldstat(td, uap->fileid, &stat);
3136 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
3137 CP(stat, stat32, refs);
3138 CP(stat, stat32, id);
3139 PTROUT_CP(stat, stat32, address);
3140 CP(stat, stat32, size);
3141 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
3142 return (copyout(&stat32, uap->stat, version));
3146 freebsd32_posix_fallocate(struct thread *td,
3147 struct freebsd32_posix_fallocate_args *uap)
3151 error = kern_posix_fallocate(td, uap->fd,
3152 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3153 return (kern_posix_error(td, error));
3157 freebsd32_posix_fadvise(struct thread *td,
3158 struct freebsd32_posix_fadvise_args *uap)
3162 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3163 PAIR32TO64(off_t, uap->len), uap->advice);
3164 return (kern_posix_error(td, error));
3168 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3171 CP(*sig32, *sig, sigev_notify);
3172 switch (sig->sigev_notify) {
3175 case SIGEV_THREAD_ID:
3176 CP(*sig32, *sig, sigev_notify_thread_id);
3179 CP(*sig32, *sig, sigev_signo);
3180 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3183 CP(*sig32, *sig, sigev_notify_kqueue);
3184 CP(*sig32, *sig, sigev_notify_kevent_flags);
3185 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3194 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3198 struct procctl_reaper_status rs;
3199 struct procctl_reaper_pids rp;
3200 struct procctl_reaper_kill rk;
3203 struct procctl_reaper_pids32 rp;
3205 int error, error1, flags;
3209 case PROC_TRACE_CTL:
3210 case PROC_TRAPCAP_CTL:
3211 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3216 case PROC_REAP_ACQUIRE:
3217 case PROC_REAP_RELEASE:
3218 if (uap->data != NULL)
3222 case PROC_REAP_STATUS:
3225 case PROC_REAP_GETPIDS:
3226 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3229 CP(x32.rp, x.rp, rp_count);
3230 PTRIN_CP(x32.rp, x.rp, rp_pids);
3233 case PROC_REAP_KILL:
3234 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3239 case PROC_TRACE_STATUS:
3240 case PROC_TRAPCAP_STATUS:
3246 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3249 case PROC_REAP_STATUS:
3251 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3253 case PROC_REAP_KILL:
3254 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3258 case PROC_TRACE_STATUS:
3259 case PROC_TRAPCAP_STATUS:
3261 error = copyout(&flags, uap->data, sizeof(flags));
3268 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3274 * Do unsigned conversion for arg when operation
3275 * interprets it as flags or pointer.
3277 case F_SETLK_REMOTE:
3286 tmp = (unsigned int)(uap->arg);
3292 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3296 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3298 struct timespec32 ts32;
3299 struct timespec ts, *tsp;
3303 if (uap->ts != NULL) {
3304 error = copyin(uap->ts, &ts32, sizeof(ts32));
3307 CP(ts32, ts, tv_sec);
3308 CP(ts32, ts, tv_nsec);
3312 if (uap->set != NULL) {
3313 error = copyin(uap->set, &set, sizeof(set));
3320 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));