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)
1907 extern int ino64_trunc_error;
1910 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
1913 CP(*in, *out, st_ino);
1914 if (in->st_ino != out->st_ino && ino64_trunc_error)
1916 CP(*in, *out, st_nlink);
1917 if (in->st_nlink != out->st_nlink && ino64_trunc_error)
1919 CP(*in, *out, st_dev);
1920 CP(*in, *out, st_mode);
1921 CP(*in, *out, st_uid);
1922 CP(*in, *out, st_gid);
1923 CP(*in, *out, st_rdev);
1924 TS_CP(*in, *out, st_atim);
1925 TS_CP(*in, *out, st_mtim);
1926 TS_CP(*in, *out, st_ctim);
1927 CP(*in, *out, st_size);
1928 CP(*in, *out, st_blocks);
1929 CP(*in, *out, st_blksize);
1930 CP(*in, *out, st_flags);
1931 CP(*in, *out, st_gen);
1932 TS_CP(*in, *out, st_birthtim);
1934 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
1935 sizeof(*out) - offsetof(struct freebsd11_stat32,
1936 st_birthtim) - sizeof(out->st_birthtim));
1941 freebsd11_freebsd32_stat(struct thread *td,
1942 struct freebsd11_freebsd32_stat_args *uap)
1945 struct freebsd11_stat32 sb32;
1948 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1952 error = freebsd11_cvtstat32(&sb, &sb32);
1954 error = copyout(&sb32, uap->ub, sizeof (sb32));
1959 freebsd11_freebsd32_fstat(struct thread *td,
1960 struct freebsd11_freebsd32_fstat_args *uap)
1963 struct freebsd11_stat32 sb32;
1966 error = kern_fstat(td, uap->fd, &sb);
1969 error = freebsd11_cvtstat32(&sb, &sb32);
1971 error = copyout(&sb32, uap->ub, sizeof (sb32));
1976 freebsd11_freebsd32_fstatat(struct thread *td,
1977 struct freebsd11_freebsd32_fstatat_args *uap)
1980 struct freebsd11_stat32 sb32;
1983 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1987 error = freebsd11_cvtstat32(&sb, &sb32);
1989 error = copyout(&sb32, uap->buf, sizeof (sb32));
1994 freebsd11_freebsd32_lstat(struct thread *td,
1995 struct freebsd11_freebsd32_lstat_args *uap)
1998 struct freebsd11_stat32 sb32;
2001 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2002 UIO_USERSPACE, &sb, NULL);
2005 error = freebsd11_cvtstat32(&sb, &sb32);
2007 error = copyout(&sb32, uap->ub, sizeof (sb32));
2012 freebsd11_freebsd32_fhstat(struct thread *td,
2013 struct freebsd11_freebsd32_fhstat_args *uap)
2016 struct freebsd11_stat32 sb32;
2020 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2023 error = kern_fhstat(td, fh, &sb);
2026 error = freebsd11_cvtstat32(&sb, &sb32);
2028 error = copyout(&sb32, uap->sb, sizeof (sb32));
2034 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2036 int error, name[CTL_MAXNAME];
2040 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2042 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2046 error = fueword32(uap->oldlenp, &tmp);
2053 error = userland_sysctl(td, name, uap->namelen,
2054 uap->old, &oldlen, 1,
2055 uap->new, uap->newlen, &j, SCTL_MASK32);
2056 if (error && error != ENOMEM)
2059 suword32(uap->oldlenp, j);
2064 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2070 error = copyin(uap->jail, &version, sizeof(uint32_t));
2077 /* FreeBSD single IPv4 jails. */
2078 struct jail32_v0 j32_v0;
2080 bzero(&j, sizeof(struct jail));
2081 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2084 CP(j32_v0, j, version);
2085 PTRIN_CP(j32_v0, j, path);
2086 PTRIN_CP(j32_v0, j, hostname);
2087 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2093 * Version 1 was used by multi-IPv4 jail implementations
2094 * that never made it into the official kernel.
2098 case 2: /* JAIL_API_VERSION */
2100 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2103 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2106 CP(j32, j, version);
2107 PTRIN_CP(j32, j, path);
2108 PTRIN_CP(j32, j, hostname);
2109 PTRIN_CP(j32, j, jailname);
2112 PTRIN_CP(j32, j, ip4);
2113 PTRIN_CP(j32, j, ip6);
2118 /* Sci-Fi jails are not supported, sorry. */
2121 return (kern_jail(td, &j));
2125 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2130 /* Check that we have an even number of iovecs. */
2131 if (uap->iovcnt & 1)
2134 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2137 error = kern_jail_set(td, auio, uap->flags);
2143 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2145 struct iovec32 iov32;
2149 /* Check that we have an even number of iovecs. */
2150 if (uap->iovcnt & 1)
2153 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2156 error = kern_jail_get(td, auio, uap->flags);
2158 for (i = 0; i < uap->iovcnt; i++) {
2159 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2160 CP(auio->uio_iov[i], iov32, iov_len);
2161 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2170 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2172 struct sigaction32 s32;
2173 struct sigaction sa, osa, *sap;
2177 error = copyin(uap->act, &s32, sizeof(s32));
2180 sa.sa_handler = PTRIN(s32.sa_u);
2181 CP(s32, sa, sa_flags);
2182 CP(s32, sa, sa_mask);
2186 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2187 if (error == 0 && uap->oact != NULL) {
2188 s32.sa_u = PTROUT(osa.sa_handler);
2189 CP(osa, s32, sa_flags);
2190 CP(osa, s32, sa_mask);
2191 error = copyout(&s32, uap->oact, sizeof(s32));
2196 #ifdef COMPAT_FREEBSD4
2198 freebsd4_freebsd32_sigaction(struct thread *td,
2199 struct freebsd4_freebsd32_sigaction_args *uap)
2201 struct sigaction32 s32;
2202 struct sigaction sa, osa, *sap;
2206 error = copyin(uap->act, &s32, sizeof(s32));
2209 sa.sa_handler = PTRIN(s32.sa_u);
2210 CP(s32, sa, sa_flags);
2211 CP(s32, sa, sa_mask);
2215 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2216 if (error == 0 && uap->oact != NULL) {
2217 s32.sa_u = PTROUT(osa.sa_handler);
2218 CP(osa, s32, sa_flags);
2219 CP(osa, s32, sa_mask);
2220 error = copyout(&s32, uap->oact, sizeof(s32));
2227 struct osigaction32 {
2236 ofreebsd32_sigaction(struct thread *td,
2237 struct ofreebsd32_sigaction_args *uap)
2239 struct osigaction32 s32;
2240 struct sigaction sa, osa, *sap;
2243 if (uap->signum <= 0 || uap->signum >= ONSIG)
2247 error = copyin(uap->nsa, &s32, sizeof(s32));
2250 sa.sa_handler = PTRIN(s32.sa_u);
2251 CP(s32, sa, sa_flags);
2252 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2256 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2257 if (error == 0 && uap->osa != NULL) {
2258 s32.sa_u = PTROUT(osa.sa_handler);
2259 CP(osa, s32, sa_flags);
2260 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2261 error = copyout(&s32, uap->osa, sizeof(s32));
2267 ofreebsd32_sigprocmask(struct thread *td,
2268 struct ofreebsd32_sigprocmask_args *uap)
2273 OSIG2SIG(uap->mask, set);
2274 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2275 SIG2OSIG(oset, td->td_retval[0]);
2280 ofreebsd32_sigpending(struct thread *td,
2281 struct ofreebsd32_sigpending_args *uap)
2283 struct proc *p = td->td_proc;
2287 siglist = p->p_siglist;
2288 SIGSETOR(siglist, td->td_siglist);
2290 SIG2OSIG(siglist, td->td_retval[0]);
2295 u_int32_t sv_handler;
2301 ofreebsd32_sigvec(struct thread *td,
2302 struct ofreebsd32_sigvec_args *uap)
2304 struct sigvec32 vec;
2305 struct sigaction sa, osa, *sap;
2308 if (uap->signum <= 0 || uap->signum >= ONSIG)
2312 error = copyin(uap->nsv, &vec, sizeof(vec));
2315 sa.sa_handler = PTRIN(vec.sv_handler);
2316 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2317 sa.sa_flags = vec.sv_flags;
2318 sa.sa_flags ^= SA_RESTART;
2322 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2323 if (error == 0 && uap->osv != NULL) {
2324 vec.sv_handler = PTROUT(osa.sa_handler);
2325 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2326 vec.sv_flags = osa.sa_flags;
2327 vec.sv_flags &= ~SA_NOCLDWAIT;
2328 vec.sv_flags ^= SA_RESTART;
2329 error = copyout(&vec, uap->osv, sizeof(vec));
2335 ofreebsd32_sigblock(struct thread *td,
2336 struct ofreebsd32_sigblock_args *uap)
2340 OSIG2SIG(uap->mask, set);
2341 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2342 SIG2OSIG(oset, td->td_retval[0]);
2347 ofreebsd32_sigsetmask(struct thread *td,
2348 struct ofreebsd32_sigsetmask_args *uap)
2352 OSIG2SIG(uap->mask, set);
2353 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2354 SIG2OSIG(oset, td->td_retval[0]);
2359 ofreebsd32_sigsuspend(struct thread *td,
2360 struct ofreebsd32_sigsuspend_args *uap)
2364 OSIG2SIG(uap->mask, mask);
2365 return (kern_sigsuspend(td, mask));
2374 ofreebsd32_sigstack(struct thread *td,
2375 struct ofreebsd32_sigstack_args *uap)
2377 struct sigstack32 s32;
2378 struct sigstack nss, oss;
2379 int error = 0, unss;
2381 if (uap->nss != NULL) {
2382 error = copyin(uap->nss, &s32, sizeof(s32));
2385 nss.ss_sp = PTRIN(s32.ss_sp);
2386 CP(s32, nss, ss_onstack);
2391 oss.ss_sp = td->td_sigstk.ss_sp;
2392 oss.ss_onstack = sigonstack(cpu_getstack(td));
2394 td->td_sigstk.ss_sp = nss.ss_sp;
2395 td->td_sigstk.ss_size = 0;
2396 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2397 td->td_pflags |= TDP_ALTSTACK;
2399 if (uap->oss != NULL) {
2400 s32.ss_sp = PTROUT(oss.ss_sp);
2401 CP(oss, s32, ss_onstack);
2402 error = copyout(&s32, uap->oss, sizeof(s32));
2409 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2412 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2413 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2417 freebsd32_clock_nanosleep(struct thread *td,
2418 struct freebsd32_clock_nanosleep_args *uap)
2422 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2423 uap->rqtp, uap->rmtp);
2424 return (kern_posix_error(td, error));
2428 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2429 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2431 struct timespec32 rmt32, rqt32;
2432 struct timespec rmt, rqt;
2435 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2439 CP(rqt32, rqt, tv_sec);
2440 CP(rqt32, rqt, tv_nsec);
2442 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2443 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2445 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2446 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2449 CP(rmt, rmt32, tv_sec);
2450 CP(rmt, rmt32, tv_nsec);
2452 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2460 freebsd32_clock_gettime(struct thread *td,
2461 struct freebsd32_clock_gettime_args *uap)
2463 struct timespec ats;
2464 struct timespec32 ats32;
2467 error = kern_clock_gettime(td, uap->clock_id, &ats);
2469 CP(ats, ats32, tv_sec);
2470 CP(ats, ats32, tv_nsec);
2471 error = copyout(&ats32, uap->tp, sizeof(ats32));
2477 freebsd32_clock_settime(struct thread *td,
2478 struct freebsd32_clock_settime_args *uap)
2480 struct timespec ats;
2481 struct timespec32 ats32;
2484 error = copyin(uap->tp, &ats32, sizeof(ats32));
2487 CP(ats32, ats, tv_sec);
2488 CP(ats32, ats, tv_nsec);
2490 return (kern_clock_settime(td, uap->clock_id, &ats));
2494 freebsd32_clock_getres(struct thread *td,
2495 struct freebsd32_clock_getres_args *uap)
2498 struct timespec32 ts32;
2501 if (uap->tp == NULL)
2503 error = kern_clock_getres(td, uap->clock_id, &ts);
2505 CP(ts, ts32, tv_sec);
2506 CP(ts, ts32, tv_nsec);
2507 error = copyout(&ts32, uap->tp, sizeof(ts32));
2512 int freebsd32_ktimer_create(struct thread *td,
2513 struct freebsd32_ktimer_create_args *uap)
2515 struct sigevent32 ev32;
2516 struct sigevent ev, *evp;
2519 if (uap->evp == NULL) {
2523 error = copyin(uap->evp, &ev32, sizeof(ev32));
2526 error = convert_sigevent32(&ev32, &ev);
2530 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2532 error = copyout(&id, uap->timerid, sizeof(int));
2534 kern_ktimer_delete(td, id);
2540 freebsd32_ktimer_settime(struct thread *td,
2541 struct freebsd32_ktimer_settime_args *uap)
2543 struct itimerspec32 val32, oval32;
2544 struct itimerspec val, oval, *ovalp;
2547 error = copyin(uap->value, &val32, sizeof(val32));
2551 ovalp = uap->ovalue != NULL ? &oval : NULL;
2552 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2553 if (error == 0 && uap->ovalue != NULL) {
2554 ITS_CP(oval, oval32);
2555 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2561 freebsd32_ktimer_gettime(struct thread *td,
2562 struct freebsd32_ktimer_gettime_args *uap)
2564 struct itimerspec32 val32;
2565 struct itimerspec val;
2568 error = kern_ktimer_gettime(td, uap->timerid, &val);
2571 error = copyout(&val32, uap->value, sizeof(val32));
2577 freebsd32_clock_getcpuclockid2(struct thread *td,
2578 struct freebsd32_clock_getcpuclockid2_args *uap)
2583 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2584 uap->which, &clk_id);
2586 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2591 freebsd32_thr_new(struct thread *td,
2592 struct freebsd32_thr_new_args *uap)
2594 struct thr_param32 param32;
2595 struct thr_param param;
2598 if (uap->param_size < 0 ||
2599 uap->param_size > sizeof(struct thr_param32))
2601 bzero(¶m, sizeof(struct thr_param));
2602 bzero(¶m32, sizeof(struct thr_param32));
2603 error = copyin(uap->param, ¶m32, uap->param_size);
2606 param.start_func = PTRIN(param32.start_func);
2607 param.arg = PTRIN(param32.arg);
2608 param.stack_base = PTRIN(param32.stack_base);
2609 param.stack_size = param32.stack_size;
2610 param.tls_base = PTRIN(param32.tls_base);
2611 param.tls_size = param32.tls_size;
2612 param.child_tid = PTRIN(param32.child_tid);
2613 param.parent_tid = PTRIN(param32.parent_tid);
2614 param.flags = param32.flags;
2615 param.rtp = PTRIN(param32.rtp);
2616 param.spare[0] = PTRIN(param32.spare[0]);
2617 param.spare[1] = PTRIN(param32.spare[1]);
2618 param.spare[2] = PTRIN(param32.spare[2]);
2620 return (kern_thr_new(td, ¶m));
2624 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2626 struct timespec32 ts32;
2627 struct timespec ts, *tsp;
2632 if (uap->timeout != NULL) {
2633 error = copyin((const void *)uap->timeout, (void *)&ts32,
2634 sizeof(struct timespec32));
2637 ts.tv_sec = ts32.tv_sec;
2638 ts.tv_nsec = ts32.tv_nsec;
2641 return (kern_thr_suspend(td, tsp));
2645 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2647 bzero(dst, sizeof(*dst));
2648 dst->si_signo = src->si_signo;
2649 dst->si_errno = src->si_errno;
2650 dst->si_code = src->si_code;
2651 dst->si_pid = src->si_pid;
2652 dst->si_uid = src->si_uid;
2653 dst->si_status = src->si_status;
2654 dst->si_addr = (uintptr_t)src->si_addr;
2655 dst->si_value.sival_int = src->si_value.sival_int;
2656 dst->si_timerid = src->si_timerid;
2657 dst->si_overrun = src->si_overrun;
2660 #ifndef _FREEBSD32_SYSPROTO_H_
2661 struct freebsd32_sigqueue_args {
2664 /* union sigval32 */ int value;
2668 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2673 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2674 * On 64-bit little-endian ABIs, the low bits are the same.
2675 * In 64-bit big-endian ABIs, sival_int overlaps with
2676 * sival_ptr's HIGH bits. We choose to support sival_int
2677 * rather than sival_ptr in this case as it seems to be
2680 bzero(&sv, sizeof(sv));
2681 sv.sival_int = uap->value;
2683 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2687 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2689 struct timespec32 ts32;
2691 struct timespec *timeout;
2694 struct siginfo32 si32;
2698 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2701 ts.tv_sec = ts32.tv_sec;
2702 ts.tv_nsec = ts32.tv_nsec;
2707 error = copyin(uap->set, &set, sizeof(set));
2711 error = kern_sigtimedwait(td, set, &ksi, timeout);
2716 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2717 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2721 td->td_retval[0] = ksi.ksi_signo;
2729 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2732 struct siginfo32 si32;
2736 error = copyin(uap->set, &set, sizeof(set));
2740 error = kern_sigtimedwait(td, set, &ksi, NULL);
2745 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2746 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2749 td->td_retval[0] = ksi.ksi_signo;
2754 freebsd32_cpuset_setid(struct thread *td,
2755 struct freebsd32_cpuset_setid_args *uap)
2758 return (kern_cpuset_setid(td, uap->which,
2759 PAIR32TO64(id_t, uap->id), uap->setid));
2763 freebsd32_cpuset_getid(struct thread *td,
2764 struct freebsd32_cpuset_getid_args *uap)
2767 return (kern_cpuset_getid(td, uap->level, uap->which,
2768 PAIR32TO64(id_t, uap->id), uap->setid));
2772 freebsd32_cpuset_getaffinity(struct thread *td,
2773 struct freebsd32_cpuset_getaffinity_args *uap)
2776 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2777 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2781 freebsd32_cpuset_setaffinity(struct thread *td,
2782 struct freebsd32_cpuset_setaffinity_args *uap)
2785 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2786 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2790 freebsd32_nmount(struct thread *td,
2791 struct freebsd32_nmount_args /* {
2793 unsigned int iovcnt;
2802 * Mount flags are now 64-bits. On 32-bit archtectures only
2803 * 32-bits are passed in, but from here on everything handles
2804 * 64-bit flags correctly.
2808 AUDIT_ARG_FFLAGS(flags);
2811 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2812 * userspace to set this flag, but we must filter it out if we want
2813 * MNT_UPDATE on the root file system to work.
2814 * MNT_ROOTFS should only be set by the kernel when mounting its
2817 flags &= ~MNT_ROOTFS;
2820 * check that we have an even number of iovec's
2821 * and that we have at least two options.
2823 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2826 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2829 error = vfs_donmount(td, flags, auio);
2837 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2839 struct yyy32 *p32, s32;
2840 struct yyy *p = NULL, s;
2845 error = copyin(uap->zzz, &s32, sizeof(s32));
2851 error = kern_xxx(td, p);
2856 error = copyout(&s32, p32, sizeof(s32));
2863 syscall32_register(int *offset, struct sysent *new_sysent,
2864 struct sysent *old_sysent, int flags)
2867 if ((flags & ~SY_THR_STATIC) != 0)
2870 if (*offset == NO_SYSCALL) {
2873 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2874 if (freebsd32_sysent[i].sy_call ==
2875 (sy_call_t *)lkmnosys)
2877 if (i == SYS_MAXSYSCALL)
2880 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2882 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2883 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2886 *old_sysent = freebsd32_sysent[*offset];
2887 freebsd32_sysent[*offset] = *new_sysent;
2888 atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2893 syscall32_deregister(int *offset, struct sysent *old_sysent)
2899 freebsd32_sysent[*offset] = *old_sysent;
2904 syscall32_module_handler(struct module *mod, int what, void *arg)
2906 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2912 error = syscall32_register(data->offset, data->new_sysent,
2913 &data->old_sysent, SY_THR_STATIC_KLD);
2915 /* Leave a mark so we know to safely unload below. */
2916 data->offset = NULL;
2919 ms.intval = *data->offset;
2921 module_setspecific(mod, &ms);
2924 error = data->chainevh(mod, what, data->chainarg);
2928 * MOD_LOAD failed, so just return without calling the
2929 * chained handler since we didn't pass along the MOD_LOAD
2932 if (data->offset == NULL)
2934 if (data->chainevh) {
2935 error = data->chainevh(mod, what, data->chainarg);
2939 error = syscall32_deregister(data->offset, &data->old_sysent);
2944 error = data->chainevh(mod, what, data->chainarg);
2950 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2952 struct syscall_helper_data *sd1;
2955 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2956 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2957 &sd1->old_sysent, flags);
2959 syscall32_helper_unregister(sd);
2962 sd1->registered = 1;
2968 syscall32_helper_unregister(struct syscall_helper_data *sd)
2970 struct syscall_helper_data *sd1;
2972 for (sd1 = sd; sd1->registered != 0; sd1++) {
2973 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2974 sd1->registered = 0;
2980 freebsd32_copyout_strings(struct image_params *imgp)
2986 u_int32_t *stack_base;
2987 struct freebsd32_ps_strings *arginfo;
2988 char canary[sizeof(long) * 8];
2989 int32_t pagesizes32[MAXPAGESIZES];
2990 size_t execpath_len;
2994 * Calculate string base and vector table pointers.
2995 * Also deal with signal trampoline code for this exec type.
2997 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2998 execpath_len = strlen(imgp->execpath) + 1;
3001 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3003 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3004 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3007 destp = (uintptr_t)arginfo;
3012 if (szsigcode != 0) {
3014 destp = rounddown2(destp, sizeof(uint32_t));
3015 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3020 * Copy the image path for the rtld.
3022 if (execpath_len != 0) {
3023 destp -= execpath_len;
3024 imgp->execpathp = destp;
3025 copyout(imgp->execpath, (void *)destp, execpath_len);
3029 * Prepare the canary for SSP.
3031 arc4rand(canary, sizeof(canary), 0);
3032 destp -= sizeof(canary);
3033 imgp->canary = destp;
3034 copyout(canary, (void *)destp, sizeof(canary));
3035 imgp->canarylen = sizeof(canary);
3038 * Prepare the pagesizes array.
3040 for (i = 0; i < MAXPAGESIZES; i++)
3041 pagesizes32[i] = (uint32_t)pagesizes[i];
3042 destp -= sizeof(pagesizes32);
3043 destp = rounddown2(destp, sizeof(uint32_t));
3044 imgp->pagesizes = destp;
3045 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3046 imgp->pagesizeslen = sizeof(pagesizes32);
3048 destp -= ARG_MAX - imgp->args->stringspace;
3049 destp = rounddown2(destp, sizeof(uint32_t));
3052 * If we have a valid auxargs ptr, prepare some room
3055 if (imgp->auxargs) {
3057 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
3058 * lower compatibility.
3060 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
3063 * The '+ 2' is for the null pointers at the end of each of
3064 * the arg and env vector sets,and imgp->auxarg_size is room
3065 * for argument of Runtime loader.
3067 vectp = (u_int32_t *) (destp - (imgp->args->argc +
3068 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
3072 * The '+ 2' is for the null pointers at the end of each of
3073 * the arg and env vector sets
3075 vectp = (u_int32_t *)(destp - (imgp->args->argc +
3076 imgp->args->envc + 2) * sizeof(u_int32_t));
3080 * vectp also becomes our initial stack base
3084 stringp = imgp->args->begin_argv;
3085 argc = imgp->args->argc;
3086 envc = imgp->args->envc;
3088 * Copy out strings - arguments and environment.
3090 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3093 * Fill in "ps_strings" struct for ps, w, etc.
3095 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3096 suword32(&arginfo->ps_nargvstr, argc);
3099 * Fill in argument portion of vector table.
3101 for (; argc > 0; --argc) {
3102 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3103 while (*stringp++ != 0)
3108 /* a null vector table pointer separates the argp's from the envp's */
3109 suword32(vectp++, 0);
3111 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3112 suword32(&arginfo->ps_nenvstr, envc);
3115 * Fill in environment portion of vector table.
3117 for (; envc > 0; --envc) {
3118 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3119 while (*stringp++ != 0)
3124 /* end of vector table is a null pointer */
3127 return ((register_t *)stack_base);
3131 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3133 struct kld_file_stat stat;
3134 struct kld32_file_stat stat32;
3137 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3140 if (version != sizeof(struct kld32_file_stat_1) &&
3141 version != sizeof(struct kld32_file_stat))
3144 error = kern_kldstat(td, uap->fileid, &stat);
3148 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
3149 CP(stat, stat32, refs);
3150 CP(stat, stat32, id);
3151 PTROUT_CP(stat, stat32, address);
3152 CP(stat, stat32, size);
3153 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
3154 return (copyout(&stat32, uap->stat, version));
3158 freebsd32_posix_fallocate(struct thread *td,
3159 struct freebsd32_posix_fallocate_args *uap)
3163 error = kern_posix_fallocate(td, uap->fd,
3164 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3165 return (kern_posix_error(td, error));
3169 freebsd32_posix_fadvise(struct thread *td,
3170 struct freebsd32_posix_fadvise_args *uap)
3174 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3175 PAIR32TO64(off_t, uap->len), uap->advice);
3176 return (kern_posix_error(td, error));
3180 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3183 CP(*sig32, *sig, sigev_notify);
3184 switch (sig->sigev_notify) {
3187 case SIGEV_THREAD_ID:
3188 CP(*sig32, *sig, sigev_notify_thread_id);
3191 CP(*sig32, *sig, sigev_signo);
3192 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3195 CP(*sig32, *sig, sigev_notify_kqueue);
3196 CP(*sig32, *sig, sigev_notify_kevent_flags);
3197 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3206 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3210 struct procctl_reaper_status rs;
3211 struct procctl_reaper_pids rp;
3212 struct procctl_reaper_kill rk;
3215 struct procctl_reaper_pids32 rp;
3217 int error, error1, flags;
3221 case PROC_TRACE_CTL:
3222 case PROC_TRAPCAP_CTL:
3223 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3228 case PROC_REAP_ACQUIRE:
3229 case PROC_REAP_RELEASE:
3230 if (uap->data != NULL)
3234 case PROC_REAP_STATUS:
3237 case PROC_REAP_GETPIDS:
3238 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3241 CP(x32.rp, x.rp, rp_count);
3242 PTRIN_CP(x32.rp, x.rp, rp_pids);
3245 case PROC_REAP_KILL:
3246 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3251 case PROC_TRACE_STATUS:
3252 case PROC_TRAPCAP_STATUS:
3258 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3261 case PROC_REAP_STATUS:
3263 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3265 case PROC_REAP_KILL:
3266 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3270 case PROC_TRACE_STATUS:
3271 case PROC_TRAPCAP_STATUS:
3273 error = copyout(&flags, uap->data, sizeof(flags));
3280 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3286 * Do unsigned conversion for arg when operation
3287 * interprets it as flags or pointer.
3289 case F_SETLK_REMOTE:
3298 tmp = (unsigned int)(uap->arg);
3304 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3308 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3310 struct timespec32 ts32;
3311 struct timespec ts, *tsp;
3315 if (uap->ts != NULL) {
3316 error = copyin(uap->ts, &ts32, sizeof(ts32));
3319 CP(ts32, ts, tv_sec);
3320 CP(ts32, ts, tv_nsec);
3324 if (uap->set != NULL) {
3325 error = copyin(uap->set, &set, sizeof(set));
3332 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));