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) {
1915 switch (ino64_trunc_error) {
1922 out->st_ino = UINT32_MAX;
1926 CP(*in, *out, st_nlink);
1927 if (in->st_nlink != out->st_nlink) {
1928 switch (ino64_trunc_error) {
1935 out->st_nlink = UINT16_MAX;
1939 CP(*in, *out, st_dev);
1940 CP(*in, *out, st_mode);
1941 CP(*in, *out, st_uid);
1942 CP(*in, *out, st_gid);
1943 CP(*in, *out, st_rdev);
1944 TS_CP(*in, *out, st_atim);
1945 TS_CP(*in, *out, st_mtim);
1946 TS_CP(*in, *out, st_ctim);
1947 CP(*in, *out, st_size);
1948 CP(*in, *out, st_blocks);
1949 CP(*in, *out, st_blksize);
1950 CP(*in, *out, st_flags);
1951 CP(*in, *out, st_gen);
1952 TS_CP(*in, *out, st_birthtim);
1954 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
1955 sizeof(*out) - offsetof(struct freebsd11_stat32,
1956 st_birthtim) - sizeof(out->st_birthtim));
1961 freebsd11_freebsd32_stat(struct thread *td,
1962 struct freebsd11_freebsd32_stat_args *uap)
1965 struct freebsd11_stat32 sb32;
1968 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1972 error = freebsd11_cvtstat32(&sb, &sb32);
1974 error = copyout(&sb32, uap->ub, sizeof (sb32));
1979 freebsd11_freebsd32_fstat(struct thread *td,
1980 struct freebsd11_freebsd32_fstat_args *uap)
1983 struct freebsd11_stat32 sb32;
1986 error = kern_fstat(td, uap->fd, &sb);
1989 error = freebsd11_cvtstat32(&sb, &sb32);
1991 error = copyout(&sb32, uap->ub, sizeof (sb32));
1996 freebsd11_freebsd32_fstatat(struct thread *td,
1997 struct freebsd11_freebsd32_fstatat_args *uap)
2000 struct freebsd11_stat32 sb32;
2003 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2007 error = freebsd11_cvtstat32(&sb, &sb32);
2009 error = copyout(&sb32, uap->buf, sizeof (sb32));
2014 freebsd11_freebsd32_lstat(struct thread *td,
2015 struct freebsd11_freebsd32_lstat_args *uap)
2018 struct freebsd11_stat32 sb32;
2021 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2022 UIO_USERSPACE, &sb, NULL);
2025 error = freebsd11_cvtstat32(&sb, &sb32);
2027 error = copyout(&sb32, uap->ub, sizeof (sb32));
2032 freebsd11_freebsd32_fhstat(struct thread *td,
2033 struct freebsd11_freebsd32_fhstat_args *uap)
2036 struct freebsd11_stat32 sb32;
2040 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2043 error = kern_fhstat(td, fh, &sb);
2046 error = freebsd11_cvtstat32(&sb, &sb32);
2048 error = copyout(&sb32, uap->sb, sizeof (sb32));
2054 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2056 int error, name[CTL_MAXNAME];
2060 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2062 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2066 error = fueword32(uap->oldlenp, &tmp);
2073 error = userland_sysctl(td, name, uap->namelen,
2074 uap->old, &oldlen, 1,
2075 uap->new, uap->newlen, &j, SCTL_MASK32);
2076 if (error && error != ENOMEM)
2079 suword32(uap->oldlenp, j);
2084 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2090 error = copyin(uap->jail, &version, sizeof(uint32_t));
2097 /* FreeBSD single IPv4 jails. */
2098 struct jail32_v0 j32_v0;
2100 bzero(&j, sizeof(struct jail));
2101 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2104 CP(j32_v0, j, version);
2105 PTRIN_CP(j32_v0, j, path);
2106 PTRIN_CP(j32_v0, j, hostname);
2107 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2113 * Version 1 was used by multi-IPv4 jail implementations
2114 * that never made it into the official kernel.
2118 case 2: /* JAIL_API_VERSION */
2120 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2123 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2126 CP(j32, j, version);
2127 PTRIN_CP(j32, j, path);
2128 PTRIN_CP(j32, j, hostname);
2129 PTRIN_CP(j32, j, jailname);
2132 PTRIN_CP(j32, j, ip4);
2133 PTRIN_CP(j32, j, ip6);
2138 /* Sci-Fi jails are not supported, sorry. */
2141 return (kern_jail(td, &j));
2145 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2150 /* Check that we have an even number of iovecs. */
2151 if (uap->iovcnt & 1)
2154 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2157 error = kern_jail_set(td, auio, uap->flags);
2163 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2165 struct iovec32 iov32;
2169 /* Check that we have an even number of iovecs. */
2170 if (uap->iovcnt & 1)
2173 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2176 error = kern_jail_get(td, auio, uap->flags);
2178 for (i = 0; i < uap->iovcnt; i++) {
2179 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2180 CP(auio->uio_iov[i], iov32, iov_len);
2181 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2190 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2192 struct sigaction32 s32;
2193 struct sigaction sa, osa, *sap;
2197 error = copyin(uap->act, &s32, sizeof(s32));
2200 sa.sa_handler = PTRIN(s32.sa_u);
2201 CP(s32, sa, sa_flags);
2202 CP(s32, sa, sa_mask);
2206 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2207 if (error == 0 && uap->oact != NULL) {
2208 s32.sa_u = PTROUT(osa.sa_handler);
2209 CP(osa, s32, sa_flags);
2210 CP(osa, s32, sa_mask);
2211 error = copyout(&s32, uap->oact, sizeof(s32));
2216 #ifdef COMPAT_FREEBSD4
2218 freebsd4_freebsd32_sigaction(struct thread *td,
2219 struct freebsd4_freebsd32_sigaction_args *uap)
2221 struct sigaction32 s32;
2222 struct sigaction sa, osa, *sap;
2226 error = copyin(uap->act, &s32, sizeof(s32));
2229 sa.sa_handler = PTRIN(s32.sa_u);
2230 CP(s32, sa, sa_flags);
2231 CP(s32, sa, sa_mask);
2235 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2236 if (error == 0 && uap->oact != NULL) {
2237 s32.sa_u = PTROUT(osa.sa_handler);
2238 CP(osa, s32, sa_flags);
2239 CP(osa, s32, sa_mask);
2240 error = copyout(&s32, uap->oact, sizeof(s32));
2247 struct osigaction32 {
2256 ofreebsd32_sigaction(struct thread *td,
2257 struct ofreebsd32_sigaction_args *uap)
2259 struct osigaction32 s32;
2260 struct sigaction sa, osa, *sap;
2263 if (uap->signum <= 0 || uap->signum >= ONSIG)
2267 error = copyin(uap->nsa, &s32, sizeof(s32));
2270 sa.sa_handler = PTRIN(s32.sa_u);
2271 CP(s32, sa, sa_flags);
2272 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2276 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2277 if (error == 0 && uap->osa != NULL) {
2278 s32.sa_u = PTROUT(osa.sa_handler);
2279 CP(osa, s32, sa_flags);
2280 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2281 error = copyout(&s32, uap->osa, sizeof(s32));
2287 ofreebsd32_sigprocmask(struct thread *td,
2288 struct ofreebsd32_sigprocmask_args *uap)
2293 OSIG2SIG(uap->mask, set);
2294 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2295 SIG2OSIG(oset, td->td_retval[0]);
2300 ofreebsd32_sigpending(struct thread *td,
2301 struct ofreebsd32_sigpending_args *uap)
2303 struct proc *p = td->td_proc;
2307 siglist = p->p_siglist;
2308 SIGSETOR(siglist, td->td_siglist);
2310 SIG2OSIG(siglist, td->td_retval[0]);
2315 u_int32_t sv_handler;
2321 ofreebsd32_sigvec(struct thread *td,
2322 struct ofreebsd32_sigvec_args *uap)
2324 struct sigvec32 vec;
2325 struct sigaction sa, osa, *sap;
2328 if (uap->signum <= 0 || uap->signum >= ONSIG)
2332 error = copyin(uap->nsv, &vec, sizeof(vec));
2335 sa.sa_handler = PTRIN(vec.sv_handler);
2336 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2337 sa.sa_flags = vec.sv_flags;
2338 sa.sa_flags ^= SA_RESTART;
2342 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2343 if (error == 0 && uap->osv != NULL) {
2344 vec.sv_handler = PTROUT(osa.sa_handler);
2345 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2346 vec.sv_flags = osa.sa_flags;
2347 vec.sv_flags &= ~SA_NOCLDWAIT;
2348 vec.sv_flags ^= SA_RESTART;
2349 error = copyout(&vec, uap->osv, sizeof(vec));
2355 ofreebsd32_sigblock(struct thread *td,
2356 struct ofreebsd32_sigblock_args *uap)
2360 OSIG2SIG(uap->mask, set);
2361 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2362 SIG2OSIG(oset, td->td_retval[0]);
2367 ofreebsd32_sigsetmask(struct thread *td,
2368 struct ofreebsd32_sigsetmask_args *uap)
2372 OSIG2SIG(uap->mask, set);
2373 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2374 SIG2OSIG(oset, td->td_retval[0]);
2379 ofreebsd32_sigsuspend(struct thread *td,
2380 struct ofreebsd32_sigsuspend_args *uap)
2384 OSIG2SIG(uap->mask, mask);
2385 return (kern_sigsuspend(td, mask));
2394 ofreebsd32_sigstack(struct thread *td,
2395 struct ofreebsd32_sigstack_args *uap)
2397 struct sigstack32 s32;
2398 struct sigstack nss, oss;
2399 int error = 0, unss;
2401 if (uap->nss != NULL) {
2402 error = copyin(uap->nss, &s32, sizeof(s32));
2405 nss.ss_sp = PTRIN(s32.ss_sp);
2406 CP(s32, nss, ss_onstack);
2411 oss.ss_sp = td->td_sigstk.ss_sp;
2412 oss.ss_onstack = sigonstack(cpu_getstack(td));
2414 td->td_sigstk.ss_sp = nss.ss_sp;
2415 td->td_sigstk.ss_size = 0;
2416 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2417 td->td_pflags |= TDP_ALTSTACK;
2419 if (uap->oss != NULL) {
2420 s32.ss_sp = PTROUT(oss.ss_sp);
2421 CP(oss, s32, ss_onstack);
2422 error = copyout(&s32, uap->oss, sizeof(s32));
2429 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2432 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2433 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2437 freebsd32_clock_nanosleep(struct thread *td,
2438 struct freebsd32_clock_nanosleep_args *uap)
2442 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2443 uap->rqtp, uap->rmtp);
2444 return (kern_posix_error(td, error));
2448 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2449 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2451 struct timespec32 rmt32, rqt32;
2452 struct timespec rmt, rqt;
2455 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2459 CP(rqt32, rqt, tv_sec);
2460 CP(rqt32, rqt, tv_nsec);
2462 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2463 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2465 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2466 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2469 CP(rmt, rmt32, tv_sec);
2470 CP(rmt, rmt32, tv_nsec);
2472 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2480 freebsd32_clock_gettime(struct thread *td,
2481 struct freebsd32_clock_gettime_args *uap)
2483 struct timespec ats;
2484 struct timespec32 ats32;
2487 error = kern_clock_gettime(td, uap->clock_id, &ats);
2489 CP(ats, ats32, tv_sec);
2490 CP(ats, ats32, tv_nsec);
2491 error = copyout(&ats32, uap->tp, sizeof(ats32));
2497 freebsd32_clock_settime(struct thread *td,
2498 struct freebsd32_clock_settime_args *uap)
2500 struct timespec ats;
2501 struct timespec32 ats32;
2504 error = copyin(uap->tp, &ats32, sizeof(ats32));
2507 CP(ats32, ats, tv_sec);
2508 CP(ats32, ats, tv_nsec);
2510 return (kern_clock_settime(td, uap->clock_id, &ats));
2514 freebsd32_clock_getres(struct thread *td,
2515 struct freebsd32_clock_getres_args *uap)
2518 struct timespec32 ts32;
2521 if (uap->tp == NULL)
2523 error = kern_clock_getres(td, uap->clock_id, &ts);
2525 CP(ts, ts32, tv_sec);
2526 CP(ts, ts32, tv_nsec);
2527 error = copyout(&ts32, uap->tp, sizeof(ts32));
2532 int freebsd32_ktimer_create(struct thread *td,
2533 struct freebsd32_ktimer_create_args *uap)
2535 struct sigevent32 ev32;
2536 struct sigevent ev, *evp;
2539 if (uap->evp == NULL) {
2543 error = copyin(uap->evp, &ev32, sizeof(ev32));
2546 error = convert_sigevent32(&ev32, &ev);
2550 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2552 error = copyout(&id, uap->timerid, sizeof(int));
2554 kern_ktimer_delete(td, id);
2560 freebsd32_ktimer_settime(struct thread *td,
2561 struct freebsd32_ktimer_settime_args *uap)
2563 struct itimerspec32 val32, oval32;
2564 struct itimerspec val, oval, *ovalp;
2567 error = copyin(uap->value, &val32, sizeof(val32));
2571 ovalp = uap->ovalue != NULL ? &oval : NULL;
2572 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2573 if (error == 0 && uap->ovalue != NULL) {
2574 ITS_CP(oval, oval32);
2575 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2581 freebsd32_ktimer_gettime(struct thread *td,
2582 struct freebsd32_ktimer_gettime_args *uap)
2584 struct itimerspec32 val32;
2585 struct itimerspec val;
2588 error = kern_ktimer_gettime(td, uap->timerid, &val);
2591 error = copyout(&val32, uap->value, sizeof(val32));
2597 freebsd32_clock_getcpuclockid2(struct thread *td,
2598 struct freebsd32_clock_getcpuclockid2_args *uap)
2603 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2604 uap->which, &clk_id);
2606 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2611 freebsd32_thr_new(struct thread *td,
2612 struct freebsd32_thr_new_args *uap)
2614 struct thr_param32 param32;
2615 struct thr_param param;
2618 if (uap->param_size < 0 ||
2619 uap->param_size > sizeof(struct thr_param32))
2621 bzero(¶m, sizeof(struct thr_param));
2622 bzero(¶m32, sizeof(struct thr_param32));
2623 error = copyin(uap->param, ¶m32, uap->param_size);
2626 param.start_func = PTRIN(param32.start_func);
2627 param.arg = PTRIN(param32.arg);
2628 param.stack_base = PTRIN(param32.stack_base);
2629 param.stack_size = param32.stack_size;
2630 param.tls_base = PTRIN(param32.tls_base);
2631 param.tls_size = param32.tls_size;
2632 param.child_tid = PTRIN(param32.child_tid);
2633 param.parent_tid = PTRIN(param32.parent_tid);
2634 param.flags = param32.flags;
2635 param.rtp = PTRIN(param32.rtp);
2636 param.spare[0] = PTRIN(param32.spare[0]);
2637 param.spare[1] = PTRIN(param32.spare[1]);
2638 param.spare[2] = PTRIN(param32.spare[2]);
2640 return (kern_thr_new(td, ¶m));
2644 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2646 struct timespec32 ts32;
2647 struct timespec ts, *tsp;
2652 if (uap->timeout != NULL) {
2653 error = copyin((const void *)uap->timeout, (void *)&ts32,
2654 sizeof(struct timespec32));
2657 ts.tv_sec = ts32.tv_sec;
2658 ts.tv_nsec = ts32.tv_nsec;
2661 return (kern_thr_suspend(td, tsp));
2665 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2667 bzero(dst, sizeof(*dst));
2668 dst->si_signo = src->si_signo;
2669 dst->si_errno = src->si_errno;
2670 dst->si_code = src->si_code;
2671 dst->si_pid = src->si_pid;
2672 dst->si_uid = src->si_uid;
2673 dst->si_status = src->si_status;
2674 dst->si_addr = (uintptr_t)src->si_addr;
2675 dst->si_value.sival_int = src->si_value.sival_int;
2676 dst->si_timerid = src->si_timerid;
2677 dst->si_overrun = src->si_overrun;
2680 #ifndef _FREEBSD32_SYSPROTO_H_
2681 struct freebsd32_sigqueue_args {
2684 /* union sigval32 */ int value;
2688 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2693 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2694 * On 64-bit little-endian ABIs, the low bits are the same.
2695 * In 64-bit big-endian ABIs, sival_int overlaps with
2696 * sival_ptr's HIGH bits. We choose to support sival_int
2697 * rather than sival_ptr in this case as it seems to be
2700 bzero(&sv, sizeof(sv));
2701 sv.sival_int = uap->value;
2703 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2707 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2709 struct timespec32 ts32;
2711 struct timespec *timeout;
2714 struct siginfo32 si32;
2718 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2721 ts.tv_sec = ts32.tv_sec;
2722 ts.tv_nsec = ts32.tv_nsec;
2727 error = copyin(uap->set, &set, sizeof(set));
2731 error = kern_sigtimedwait(td, set, &ksi, timeout);
2736 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2737 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2741 td->td_retval[0] = ksi.ksi_signo;
2749 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2752 struct siginfo32 si32;
2756 error = copyin(uap->set, &set, sizeof(set));
2760 error = kern_sigtimedwait(td, set, &ksi, NULL);
2765 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2766 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2769 td->td_retval[0] = ksi.ksi_signo;
2774 freebsd32_cpuset_setid(struct thread *td,
2775 struct freebsd32_cpuset_setid_args *uap)
2778 return (kern_cpuset_setid(td, uap->which,
2779 PAIR32TO64(id_t, uap->id), uap->setid));
2783 freebsd32_cpuset_getid(struct thread *td,
2784 struct freebsd32_cpuset_getid_args *uap)
2787 return (kern_cpuset_getid(td, uap->level, uap->which,
2788 PAIR32TO64(id_t, uap->id), uap->setid));
2792 freebsd32_cpuset_getaffinity(struct thread *td,
2793 struct freebsd32_cpuset_getaffinity_args *uap)
2796 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2797 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2801 freebsd32_cpuset_setaffinity(struct thread *td,
2802 struct freebsd32_cpuset_setaffinity_args *uap)
2805 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2806 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2810 freebsd32_nmount(struct thread *td,
2811 struct freebsd32_nmount_args /* {
2813 unsigned int iovcnt;
2822 * Mount flags are now 64-bits. On 32-bit archtectures only
2823 * 32-bits are passed in, but from here on everything handles
2824 * 64-bit flags correctly.
2828 AUDIT_ARG_FFLAGS(flags);
2831 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2832 * userspace to set this flag, but we must filter it out if we want
2833 * MNT_UPDATE on the root file system to work.
2834 * MNT_ROOTFS should only be set by the kernel when mounting its
2837 flags &= ~MNT_ROOTFS;
2840 * check that we have an even number of iovec's
2841 * and that we have at least two options.
2843 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2846 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2849 error = vfs_donmount(td, flags, auio);
2857 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2859 struct yyy32 *p32, s32;
2860 struct yyy *p = NULL, s;
2865 error = copyin(uap->zzz, &s32, sizeof(s32));
2871 error = kern_xxx(td, p);
2876 error = copyout(&s32, p32, sizeof(s32));
2883 syscall32_register(int *offset, struct sysent *new_sysent,
2884 struct sysent *old_sysent, int flags)
2887 if ((flags & ~SY_THR_STATIC) != 0)
2890 if (*offset == NO_SYSCALL) {
2893 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2894 if (freebsd32_sysent[i].sy_call ==
2895 (sy_call_t *)lkmnosys)
2897 if (i == SYS_MAXSYSCALL)
2900 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2902 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2903 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2906 *old_sysent = freebsd32_sysent[*offset];
2907 freebsd32_sysent[*offset] = *new_sysent;
2908 atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2913 syscall32_deregister(int *offset, struct sysent *old_sysent)
2919 freebsd32_sysent[*offset] = *old_sysent;
2924 syscall32_module_handler(struct module *mod, int what, void *arg)
2926 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2932 error = syscall32_register(data->offset, data->new_sysent,
2933 &data->old_sysent, SY_THR_STATIC_KLD);
2935 /* Leave a mark so we know to safely unload below. */
2936 data->offset = NULL;
2939 ms.intval = *data->offset;
2941 module_setspecific(mod, &ms);
2944 error = data->chainevh(mod, what, data->chainarg);
2948 * MOD_LOAD failed, so just return without calling the
2949 * chained handler since we didn't pass along the MOD_LOAD
2952 if (data->offset == NULL)
2954 if (data->chainevh) {
2955 error = data->chainevh(mod, what, data->chainarg);
2959 error = syscall32_deregister(data->offset, &data->old_sysent);
2964 error = data->chainevh(mod, what, data->chainarg);
2970 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2972 struct syscall_helper_data *sd1;
2975 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2976 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2977 &sd1->old_sysent, flags);
2979 syscall32_helper_unregister(sd);
2982 sd1->registered = 1;
2988 syscall32_helper_unregister(struct syscall_helper_data *sd)
2990 struct syscall_helper_data *sd1;
2992 for (sd1 = sd; sd1->registered != 0; sd1++) {
2993 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2994 sd1->registered = 0;
3000 freebsd32_copyout_strings(struct image_params *imgp)
3006 u_int32_t *stack_base;
3007 struct freebsd32_ps_strings *arginfo;
3008 char canary[sizeof(long) * 8];
3009 int32_t pagesizes32[MAXPAGESIZES];
3010 size_t execpath_len;
3014 * Calculate string base and vector table pointers.
3015 * Also deal with signal trampoline code for this exec type.
3017 if (imgp->execpath != NULL && imgp->auxargs != NULL)
3018 execpath_len = strlen(imgp->execpath) + 1;
3021 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3023 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3024 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3027 destp = (uintptr_t)arginfo;
3032 if (szsigcode != 0) {
3034 destp = rounddown2(destp, sizeof(uint32_t));
3035 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3040 * Copy the image path for the rtld.
3042 if (execpath_len != 0) {
3043 destp -= execpath_len;
3044 imgp->execpathp = destp;
3045 copyout(imgp->execpath, (void *)destp, execpath_len);
3049 * Prepare the canary for SSP.
3051 arc4rand(canary, sizeof(canary), 0);
3052 destp -= sizeof(canary);
3053 imgp->canary = destp;
3054 copyout(canary, (void *)destp, sizeof(canary));
3055 imgp->canarylen = sizeof(canary);
3058 * Prepare the pagesizes array.
3060 for (i = 0; i < MAXPAGESIZES; i++)
3061 pagesizes32[i] = (uint32_t)pagesizes[i];
3062 destp -= sizeof(pagesizes32);
3063 destp = rounddown2(destp, sizeof(uint32_t));
3064 imgp->pagesizes = destp;
3065 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3066 imgp->pagesizeslen = sizeof(pagesizes32);
3068 destp -= ARG_MAX - imgp->args->stringspace;
3069 destp = rounddown2(destp, sizeof(uint32_t));
3072 * If we have a valid auxargs ptr, prepare some room
3075 if (imgp->auxargs) {
3077 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
3078 * lower compatibility.
3080 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
3083 * The '+ 2' is for the null pointers at the end of each of
3084 * the arg and env vector sets,and imgp->auxarg_size is room
3085 * for argument of Runtime loader.
3087 vectp = (u_int32_t *) (destp - (imgp->args->argc +
3088 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
3092 * The '+ 2' is for the null pointers at the end of each of
3093 * the arg and env vector sets
3095 vectp = (u_int32_t *)(destp - (imgp->args->argc +
3096 imgp->args->envc + 2) * sizeof(u_int32_t));
3100 * vectp also becomes our initial stack base
3104 stringp = imgp->args->begin_argv;
3105 argc = imgp->args->argc;
3106 envc = imgp->args->envc;
3108 * Copy out strings - arguments and environment.
3110 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3113 * Fill in "ps_strings" struct for ps, w, etc.
3115 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3116 suword32(&arginfo->ps_nargvstr, argc);
3119 * Fill in argument portion of vector table.
3121 for (; argc > 0; --argc) {
3122 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3123 while (*stringp++ != 0)
3128 /* a null vector table pointer separates the argp's from the envp's */
3129 suword32(vectp++, 0);
3131 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3132 suword32(&arginfo->ps_nenvstr, envc);
3135 * Fill in environment portion of vector table.
3137 for (; envc > 0; --envc) {
3138 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3139 while (*stringp++ != 0)
3144 /* end of vector table is a null pointer */
3147 return ((register_t *)stack_base);
3151 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3153 struct kld_file_stat stat;
3154 struct kld32_file_stat stat32;
3157 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3160 if (version != sizeof(struct kld32_file_stat_1) &&
3161 version != sizeof(struct kld32_file_stat))
3164 error = kern_kldstat(td, uap->fileid, &stat);
3168 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
3169 CP(stat, stat32, refs);
3170 CP(stat, stat32, id);
3171 PTROUT_CP(stat, stat32, address);
3172 CP(stat, stat32, size);
3173 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
3174 return (copyout(&stat32, uap->stat, version));
3178 freebsd32_posix_fallocate(struct thread *td,
3179 struct freebsd32_posix_fallocate_args *uap)
3183 error = kern_posix_fallocate(td, uap->fd,
3184 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3185 return (kern_posix_error(td, error));
3189 freebsd32_posix_fadvise(struct thread *td,
3190 struct freebsd32_posix_fadvise_args *uap)
3194 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3195 PAIR32TO64(off_t, uap->len), uap->advice);
3196 return (kern_posix_error(td, error));
3200 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3203 CP(*sig32, *sig, sigev_notify);
3204 switch (sig->sigev_notify) {
3207 case SIGEV_THREAD_ID:
3208 CP(*sig32, *sig, sigev_notify_thread_id);
3211 CP(*sig32, *sig, sigev_signo);
3212 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3215 CP(*sig32, *sig, sigev_notify_kqueue);
3216 CP(*sig32, *sig, sigev_notify_kevent_flags);
3217 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3226 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3230 struct procctl_reaper_status rs;
3231 struct procctl_reaper_pids rp;
3232 struct procctl_reaper_kill rk;
3235 struct procctl_reaper_pids32 rp;
3237 int error, error1, flags;
3241 case PROC_TRACE_CTL:
3242 case PROC_TRAPCAP_CTL:
3243 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3248 case PROC_REAP_ACQUIRE:
3249 case PROC_REAP_RELEASE:
3250 if (uap->data != NULL)
3254 case PROC_REAP_STATUS:
3257 case PROC_REAP_GETPIDS:
3258 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3261 CP(x32.rp, x.rp, rp_count);
3262 PTRIN_CP(x32.rp, x.rp, rp_pids);
3265 case PROC_REAP_KILL:
3266 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3271 case PROC_TRACE_STATUS:
3272 case PROC_TRAPCAP_STATUS:
3278 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3281 case PROC_REAP_STATUS:
3283 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3285 case PROC_REAP_KILL:
3286 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3290 case PROC_TRACE_STATUS:
3291 case PROC_TRAPCAP_STATUS:
3293 error = copyout(&flags, uap->data, sizeof(flags));
3300 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3306 * Do unsigned conversion for arg when operation
3307 * interprets it as flags or pointer.
3309 case F_SETLK_REMOTE:
3318 tmp = (unsigned int)(uap->arg);
3324 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3328 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3330 struct timespec32 ts32;
3331 struct timespec ts, *tsp;
3335 if (uap->ts != NULL) {
3336 error = copyin(uap->ts, &ts32, sizeof(ts32));
3339 CP(ts32, ts, tv_sec);
3340 CP(ts32, ts, tv_nsec);
3344 if (uap->set != NULL) {
3345 error = copyin(uap->set, &set, sizeof(set));
3352 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));