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) == 56);
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];
628 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
629 uap = (struct freebsd32_kevent_args *)arg;
631 for (i = 0; i < count; i++) {
632 CP(kevp[i], ks32[i], ident);
633 CP(kevp[i], ks32[i], filter);
634 CP(kevp[i], ks32[i], flags);
635 CP(kevp[i], ks32[i], fflags);
636 #if BYTE_ORDER == LITTLE_ENDIAN
637 ks32[i].data1 = kevp[i].data;
638 ks32[i].data2 = kevp[i].data >> 32;
640 ks32[i].data1 = kevp[i].data >> 32;
641 ks32[i].data2 = kevp[i].data;
643 PTROUT_CP(kevp[i], ks32[i], udata);
644 for (j = 0; j < nitems(kevp->ext); j++) {
646 #if BYTE_ORDER == LITTLE_ENDIAN
647 ks32[i].ext64[2 * j] = e;
648 ks32[i].ext64[2 * j + 1] = e >> 32;
650 ks32[i].ext64[2 * j] = e >> 32;
651 ks32[i].ext64[2 * j + 1] = e;
655 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
657 uap->eventlist += count;
662 * Copy 'count' items from the list pointed to by uap->changelist.
665 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
667 struct freebsd32_kevent_args *uap;
668 struct kevent32 ks32[KQ_NEVENTS];
672 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
673 uap = (struct freebsd32_kevent_args *)arg;
675 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
678 uap->changelist += count;
680 for (i = 0; i < count; i++) {
681 CP(ks32[i], kevp[i], ident);
682 CP(ks32[i], kevp[i], filter);
683 CP(ks32[i], kevp[i], flags);
684 CP(ks32[i], kevp[i], fflags);
685 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
686 PTRIN_CP(ks32[i], kevp[i], udata);
687 for (j = 0; j < nitems(kevp->ext); j++) {
688 #if BYTE_ORDER == LITTLE_ENDIAN
689 e = ks32[i].ext64[2 * j + 1];
691 e += ks32[i].ext64[2 * j];
693 e = ks32[i].ext64[2 * j];
695 e += ks32[i].ext64[2 * j + 1];
705 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
707 struct timespec32 ts32;
708 struct timespec ts, *tsp;
709 struct kevent_copyops k_ops = {
711 .k_copyout = freebsd32_kevent_copyout,
712 .k_copyin = freebsd32_kevent_copyin,
717 error = copyin(uap->timeout, &ts32, sizeof(ts32));
720 CP(ts32, ts, tv_sec);
721 CP(ts32, ts, tv_nsec);
725 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
730 #ifdef COMPAT_FREEBSD11
731 struct kevent32_freebsd11 {
732 u_int32_t ident; /* identifier for this event */
733 short filter; /* filter for event */
737 u_int32_t udata; /* opaque user data identifier */
741 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
743 struct freebsd11_freebsd32_kevent_args *uap;
744 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
747 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
748 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
750 for (i = 0; i < count; i++) {
751 CP(kevp[i], ks32[i], ident);
752 CP(kevp[i], ks32[i], filter);
753 CP(kevp[i], ks32[i], flags);
754 CP(kevp[i], ks32[i], fflags);
755 CP(kevp[i], ks32[i], data);
756 PTROUT_CP(kevp[i], ks32[i], udata);
758 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
760 uap->eventlist += count;
765 * Copy 'count' items from the list pointed to by uap->changelist.
768 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
770 struct freebsd11_freebsd32_kevent_args *uap;
771 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
774 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
775 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
777 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
780 uap->changelist += count;
782 for (i = 0; i < count; i++) {
783 CP(ks32[i], kevp[i], ident);
784 CP(ks32[i], kevp[i], filter);
785 CP(ks32[i], kevp[i], flags);
786 CP(ks32[i], kevp[i], fflags);
787 CP(ks32[i], kevp[i], data);
788 PTRIN_CP(ks32[i], kevp[i], udata);
789 for (j = 0; j < nitems(kevp->ext); j++)
797 freebsd11_freebsd32_kevent(struct thread *td,
798 struct freebsd11_freebsd32_kevent_args *uap)
800 struct timespec32 ts32;
801 struct timespec ts, *tsp;
802 struct kevent_copyops k_ops = {
804 .k_copyout = freebsd32_kevent11_copyout,
805 .k_copyin = freebsd32_kevent11_copyin,
810 error = copyin(uap->timeout, &ts32, sizeof(ts32));
813 CP(ts32, ts, tv_sec);
814 CP(ts32, ts, tv_nsec);
818 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
825 freebsd32_gettimeofday(struct thread *td,
826 struct freebsd32_gettimeofday_args *uap)
829 struct timeval32 atv32;
835 CP(atv, atv32, tv_sec);
836 CP(atv, atv32, tv_usec);
837 error = copyout(&atv32, uap->tp, sizeof (atv32));
839 if (error == 0 && uap->tzp != NULL) {
840 rtz.tz_minuteswest = tz_minuteswest;
841 rtz.tz_dsttime = tz_dsttime;
842 error = copyout(&rtz, uap->tzp, sizeof (rtz));
848 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
854 error = kern_getrusage(td, uap->who, &s);
857 if (uap->rusage != NULL) {
858 freebsd32_rusage_out(&s, &s32);
859 error = copyout(&s32, uap->rusage, sizeof(s32));
865 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
867 struct iovec32 iov32;
874 if (iovcnt > UIO_MAXIOV)
876 iovlen = iovcnt * sizeof(struct iovec);
877 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
878 iov = (struct iovec *)(uio + 1);
879 for (i = 0; i < iovcnt; i++) {
880 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
885 iov[i].iov_base = PTRIN(iov32.iov_base);
886 iov[i].iov_len = iov32.iov_len;
889 uio->uio_iovcnt = iovcnt;
890 uio->uio_segflg = UIO_USERSPACE;
891 uio->uio_offset = -1;
893 for (i = 0; i < iovcnt; i++) {
894 if (iov->iov_len > INT_MAX - uio->uio_resid) {
898 uio->uio_resid += iov->iov_len;
906 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
911 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
914 error = kern_readv(td, uap->fd, auio);
920 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
925 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
928 error = kern_writev(td, uap->fd, auio);
934 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
939 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
942 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
948 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
953 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
956 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
962 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
965 struct iovec32 iov32;
971 if (iovcnt > UIO_MAXIOV)
973 iovlen = iovcnt * sizeof(struct iovec);
974 iov = malloc(iovlen, M_IOV, M_WAITOK);
975 for (i = 0; i < iovcnt; i++) {
976 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
981 iov[i].iov_base = PTRIN(iov32.iov_base);
982 iov[i].iov_len = iov32.iov_len;
989 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
994 error = copyin(msg32, &m32, sizeof(m32));
997 msg->msg_name = PTRIN(m32.msg_name);
998 msg->msg_namelen = m32.msg_namelen;
999 msg->msg_iov = PTRIN(m32.msg_iov);
1000 msg->msg_iovlen = m32.msg_iovlen;
1001 msg->msg_control = PTRIN(m32.msg_control);
1002 msg->msg_controllen = m32.msg_controllen;
1003 msg->msg_flags = m32.msg_flags;
1008 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1010 struct msghdr32 m32;
1013 m32.msg_name = PTROUT(msg->msg_name);
1014 m32.msg_namelen = msg->msg_namelen;
1015 m32.msg_iov = PTROUT(msg->msg_iov);
1016 m32.msg_iovlen = msg->msg_iovlen;
1017 m32.msg_control = PTROUT(msg->msg_control);
1018 m32.msg_controllen = msg->msg_controllen;
1019 m32.msg_flags = msg->msg_flags;
1020 error = copyout(&m32, msg32, sizeof(m32));
1025 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
1027 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
1029 #define FREEBSD32_ALIGN(p) \
1030 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1031 #define FREEBSD32_CMSG_SPACE(l) \
1032 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1034 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
1035 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1037 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1041 socklen_t clen, datalen;
1044 int len, maxlen, copylen;
1048 len = msg->msg_controllen;
1049 maxlen = msg->msg_controllen;
1050 msg->msg_controllen = 0;
1053 ctlbuf = msg->msg_control;
1055 while (m && len > 0) {
1056 cm = mtod(m, struct cmsghdr *);
1059 while (cm != NULL) {
1061 if (sizeof(struct cmsghdr) > clen ||
1062 cm->cmsg_len > clen) {
1067 data = CMSG_DATA(cm);
1068 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1070 /* Adjust message length */
1071 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1076 copylen = sizeof(struct cmsghdr);
1077 if (len < copylen) {
1078 msg->msg_flags |= MSG_CTRUNC;
1082 error = copyout(cm,ctlbuf,copylen);
1086 ctlbuf += FREEBSD32_ALIGN(copylen);
1087 len -= FREEBSD32_ALIGN(copylen);
1094 if (len < copylen) {
1095 msg->msg_flags |= MSG_CTRUNC;
1099 error = copyout(data,ctlbuf,copylen);
1103 ctlbuf += FREEBSD32_ALIGN(copylen);
1104 len -= FREEBSD32_ALIGN(copylen);
1106 if (CMSG_SPACE(datalen) < clen) {
1107 clen -= CMSG_SPACE(datalen);
1108 cm = (struct cmsghdr *)
1109 ((caddr_t)cm + CMSG_SPACE(datalen));
1118 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1126 freebsd32_recvmsg(td, uap)
1128 struct freebsd32_recvmsg_args /* {
1130 struct msghdr32 *msg;
1135 struct msghdr32 m32;
1136 struct iovec *uiov, *iov;
1137 struct mbuf *control = NULL;
1138 struct mbuf **controlp;
1141 error = copyin(uap->msg, &m32, sizeof(m32));
1144 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1147 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1151 msg.msg_flags = uap->flags;
1155 controlp = (msg.msg_control != NULL) ? &control : NULL;
1156 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1160 if (control != NULL)
1161 error = freebsd32_copy_msg_out(&msg, control);
1163 msg.msg_controllen = 0;
1166 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1170 if (control != NULL)
1177 * Copy-in the array of control messages constructed using alignment
1178 * and padding suitable for a 32-bit environment and construct an
1179 * mbuf using alignment and padding suitable for a 64-bit kernel.
1180 * The alignment and padding are defined indirectly by CMSG_DATA(),
1181 * CMSG_SPACE() and CMSG_LEN().
1184 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1188 u_int idx, len, msglen;
1191 buflen = FREEBSD32_ALIGN(buflen);
1193 if (buflen > MCLBYTES)
1197 * Iterate over the buffer and get the length of each message
1198 * in there. This has 32-bit alignment and padding. Use it to
1199 * determine the length of these messages when using 64-bit
1200 * alignment and padding.
1204 while (idx < buflen) {
1205 error = copyin(buf + idx, &msglen, sizeof(msglen));
1208 if (msglen < sizeof(struct cmsghdr))
1210 msglen = FREEBSD32_ALIGN(msglen);
1211 if (idx + msglen > buflen)
1214 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1215 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1216 len += CMSG_ALIGN(msglen);
1222 m = m_get(M_WAITOK, MT_CONTROL);
1224 MCLGET(m, M_WAITOK);
1227 md = mtod(m, void *);
1228 while (buflen > 0) {
1229 error = copyin(buf, md, sizeof(struct cmsghdr));
1232 msglen = *(u_int *)md;
1233 msglen = FREEBSD32_ALIGN(msglen);
1235 /* Modify the message length to account for alignment. */
1236 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1237 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1239 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1240 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1241 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1243 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1245 error = copyin(buf, md, msglen);
1248 md = (char *)md + CMSG_ALIGN(msglen);
1262 freebsd32_sendmsg(struct thread *td,
1263 struct freebsd32_sendmsg_args *uap)
1266 struct msghdr32 m32;
1268 struct mbuf *control = NULL;
1269 struct sockaddr *to = NULL;
1272 error = copyin(uap->msg, &m32, sizeof(m32));
1275 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1278 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1283 if (msg.msg_name != NULL) {
1284 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1292 if (msg.msg_control) {
1293 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1298 error = freebsd32_copyin_control(&control, msg.msg_control,
1299 msg.msg_controllen);
1303 msg.msg_control = NULL;
1304 msg.msg_controllen = 0;
1307 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1318 freebsd32_recvfrom(struct thread *td,
1319 struct freebsd32_recvfrom_args *uap)
1325 if (uap->fromlenaddr) {
1326 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1327 sizeof(msg.msg_namelen));
1331 msg.msg_namelen = 0;
1334 msg.msg_name = PTRIN(uap->from);
1335 msg.msg_iov = &aiov;
1337 aiov.iov_base = PTRIN(uap->buf);
1338 aiov.iov_len = uap->len;
1339 msg.msg_control = NULL;
1340 msg.msg_flags = uap->flags;
1341 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1342 if (error == 0 && uap->fromlenaddr)
1343 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1344 sizeof (msg.msg_namelen));
1349 freebsd32_settimeofday(struct thread *td,
1350 struct freebsd32_settimeofday_args *uap)
1352 struct timeval32 tv32;
1353 struct timeval tv, *tvp;
1354 struct timezone tz, *tzp;
1358 error = copyin(uap->tv, &tv32, sizeof(tv32));
1361 CP(tv32, tv, tv_sec);
1362 CP(tv32, tv, tv_usec);
1367 error = copyin(uap->tzp, &tz, sizeof(tz));
1373 return (kern_settimeofday(td, tvp, tzp));
1377 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1379 struct timeval32 s32[2];
1380 struct timeval s[2], *sp;
1383 if (uap->tptr != NULL) {
1384 error = copyin(uap->tptr, s32, sizeof(s32));
1387 CP(s32[0], s[0], tv_sec);
1388 CP(s32[0], s[0], tv_usec);
1389 CP(s32[1], s[1], tv_sec);
1390 CP(s32[1], s[1], tv_usec);
1394 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1399 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1401 struct timeval32 s32[2];
1402 struct timeval s[2], *sp;
1405 if (uap->tptr != NULL) {
1406 error = copyin(uap->tptr, s32, sizeof(s32));
1409 CP(s32[0], s[0], tv_sec);
1410 CP(s32[0], s[0], tv_usec);
1411 CP(s32[1], s[1], tv_sec);
1412 CP(s32[1], s[1], tv_usec);
1416 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1420 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1422 struct timeval32 s32[2];
1423 struct timeval s[2], *sp;
1426 if (uap->tptr != NULL) {
1427 error = copyin(uap->tptr, s32, sizeof(s32));
1430 CP(s32[0], s[0], tv_sec);
1431 CP(s32[0], s[0], tv_usec);
1432 CP(s32[1], s[1], tv_sec);
1433 CP(s32[1], s[1], tv_usec);
1437 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1441 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1443 struct timeval32 s32[2];
1444 struct timeval s[2], *sp;
1447 if (uap->times != NULL) {
1448 error = copyin(uap->times, s32, sizeof(s32));
1451 CP(s32[0], s[0], tv_sec);
1452 CP(s32[0], s[0], tv_usec);
1453 CP(s32[1], s[1], tv_sec);
1454 CP(s32[1], s[1], tv_usec);
1458 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1463 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1465 struct timespec32 ts32[2];
1466 struct timespec ts[2], *tsp;
1469 if (uap->times != NULL) {
1470 error = copyin(uap->times, ts32, sizeof(ts32));
1473 CP(ts32[0], ts[0], tv_sec);
1474 CP(ts32[0], ts[0], tv_nsec);
1475 CP(ts32[1], ts[1], tv_sec);
1476 CP(ts32[1], ts[1], tv_nsec);
1480 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1484 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1486 struct timespec32 ts32[2];
1487 struct timespec ts[2], *tsp;
1490 if (uap->times != NULL) {
1491 error = copyin(uap->times, ts32, sizeof(ts32));
1494 CP(ts32[0], ts[0], tv_sec);
1495 CP(ts32[0], ts[0], tv_nsec);
1496 CP(ts32[1], ts[1], tv_sec);
1497 CP(ts32[1], ts[1], tv_nsec);
1501 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1502 tsp, UIO_SYSSPACE, uap->flag));
1506 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1508 struct timeval32 tv32;
1509 struct timeval delta, olddelta, *deltap;
1513 error = copyin(uap->delta, &tv32, sizeof(tv32));
1516 CP(tv32, delta, tv_sec);
1517 CP(tv32, delta, tv_usec);
1521 error = kern_adjtime(td, deltap, &olddelta);
1522 if (uap->olddelta && error == 0) {
1523 CP(olddelta, tv32, tv_sec);
1524 CP(olddelta, tv32, tv_usec);
1525 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1530 #ifdef COMPAT_FREEBSD4
1532 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1534 struct statfs32 s32;
1538 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1539 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1541 copy_statfs(sp, &s32);
1542 error = copyout(&s32, uap->buf, sizeof(s32));
1549 #ifdef COMPAT_FREEBSD4
1551 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1553 struct statfs32 s32;
1557 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1558 error = kern_fstatfs(td, uap->fd, sp);
1560 copy_statfs(sp, &s32);
1561 error = copyout(&s32, uap->buf, sizeof(s32));
1568 #ifdef COMPAT_FREEBSD4
1570 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1572 struct statfs32 s32;
1577 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1579 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1580 error = kern_fhstatfs(td, fh, sp);
1582 copy_statfs(sp, &s32);
1583 error = copyout(&s32, uap->buf, sizeof(s32));
1591 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1594 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1595 PAIR32TO64(off_t, uap->offset)));
1599 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1602 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1603 PAIR32TO64(off_t, uap->offset)));
1608 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1611 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1616 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1621 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1623 /* Expand the quad return into two parts for eax and edx */
1624 pos = td->td_uretoff.tdu_off;
1625 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1626 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1631 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1634 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1635 PAIR32TO64(off_t, uap->length)));
1639 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1642 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1647 ofreebsd32_getdirentries(struct thread *td,
1648 struct ofreebsd32_getdirentries_args *uap)
1650 struct ogetdirentries_args ap;
1657 ap.count = uap->count;
1659 error = kern_ogetdirentries(td, &ap, &loff);
1662 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1668 #if defined(COMPAT_FREEBSD11)
1670 freebsd11_freebsd32_getdirentries(struct thread *td,
1671 struct freebsd11_freebsd32_getdirentries_args *uap)
1677 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1681 if (uap->basep != NULL) {
1683 error = copyout(&base32, uap->basep, sizeof(int32_t));
1689 freebsd11_freebsd32_getdents(struct thread *td,
1690 struct freebsd11_freebsd32_getdents_args *uap)
1692 struct freebsd11_freebsd32_getdirentries_args ap;
1696 ap.count = uap->count;
1698 return (freebsd11_freebsd32_getdirentries(td, &ap));
1700 #endif /* COMPAT_FREEBSD11 */
1703 freebsd32_getdirentries(struct thread *td,
1704 struct freebsd32_getdirentries_args *uap)
1710 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1711 NULL, UIO_USERSPACE);
1714 if (uap->basep != NULL) {
1716 error = copyout(&base32, uap->basep, sizeof(int32_t));
1721 #ifdef COMPAT_FREEBSD6
1722 /* versions with the 'int pad' argument */
1724 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1727 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1728 PAIR32TO64(off_t, uap->offset)));
1732 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1735 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1736 PAIR32TO64(off_t, uap->offset)));
1740 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1745 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1747 /* Expand the quad return into two parts for eax and edx */
1748 pos = *(off_t *)(td->td_retval);
1749 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1750 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1755 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1758 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1759 PAIR32TO64(off_t, uap->length)));
1763 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1766 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1768 #endif /* COMPAT_FREEBSD6 */
1778 freebsd32_do_sendfile(struct thread *td,
1779 struct freebsd32_sendfile_args *uap, int compat)
1781 struct sf_hdtr32 hdtr32;
1782 struct sf_hdtr hdtr;
1783 struct uio *hdr_uio, *trl_uio;
1785 cap_rights_t rights;
1786 struct iovec32 *iov32;
1787 off_t offset, sbytes;
1790 offset = PAIR32TO64(off_t, uap->offset);
1794 hdr_uio = trl_uio = NULL;
1796 if (uap->hdtr != NULL) {
1797 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1800 PTRIN_CP(hdtr32, hdtr, headers);
1801 CP(hdtr32, hdtr, hdr_cnt);
1802 PTRIN_CP(hdtr32, hdtr, trailers);
1803 CP(hdtr32, hdtr, trl_cnt);
1805 if (hdtr.headers != NULL) {
1806 iov32 = PTRIN(hdtr32.headers);
1807 error = freebsd32_copyinuio(iov32,
1808 hdtr32.hdr_cnt, &hdr_uio);
1811 #ifdef COMPAT_FREEBSD4
1813 * In FreeBSD < 5.0 the nbytes to send also included
1814 * the header. If compat is specified subtract the
1815 * header size from nbytes.
1818 if (uap->nbytes > hdr_uio->uio_resid)
1819 uap->nbytes -= hdr_uio->uio_resid;
1825 if (hdtr.trailers != NULL) {
1826 iov32 = PTRIN(hdtr32.trailers);
1827 error = freebsd32_copyinuio(iov32,
1828 hdtr32.trl_cnt, &trl_uio);
1834 AUDIT_ARG_FD(uap->fd);
1836 if ((error = fget_read(td, uap->fd,
1837 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1840 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1841 uap->nbytes, &sbytes, uap->flags, td);
1844 if (uap->sbytes != NULL)
1845 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1849 free(hdr_uio, M_IOV);
1851 free(trl_uio, M_IOV);
1855 #ifdef COMPAT_FREEBSD4
1857 freebsd4_freebsd32_sendfile(struct thread *td,
1858 struct freebsd4_freebsd32_sendfile_args *uap)
1860 return (freebsd32_do_sendfile(td,
1861 (struct freebsd32_sendfile_args *)uap, 1));
1866 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1869 return (freebsd32_do_sendfile(td, uap, 0));
1873 copy_stat(struct stat *in, struct stat32 *out)
1876 CP(*in, *out, st_dev);
1877 CP(*in, *out, st_ino);
1878 CP(*in, *out, st_mode);
1879 CP(*in, *out, st_nlink);
1880 CP(*in, *out, st_uid);
1881 CP(*in, *out, st_gid);
1882 CP(*in, *out, st_rdev);
1883 TS_CP(*in, *out, st_atim);
1884 TS_CP(*in, *out, st_mtim);
1885 TS_CP(*in, *out, st_ctim);
1886 CP(*in, *out, st_size);
1887 CP(*in, *out, st_blocks);
1888 CP(*in, *out, st_blksize);
1889 CP(*in, *out, st_flags);
1890 CP(*in, *out, st_gen);
1891 TS_CP(*in, *out, st_birthtim);
1892 out->st_padding0 = 0;
1893 out->st_padding1 = 0;
1894 #ifdef __STAT32_TIME_T_EXT
1895 out->st_atim_ext = 0;
1896 out->st_mtim_ext = 0;
1897 out->st_ctim_ext = 0;
1898 out->st_btim_ext = 0;
1900 bzero(out->st_spare, sizeof(out->st_spare));
1905 copy_ostat(struct stat *in, struct ostat32 *out)
1908 CP(*in, *out, st_dev);
1909 CP(*in, *out, st_ino);
1910 CP(*in, *out, st_mode);
1911 CP(*in, *out, st_nlink);
1912 CP(*in, *out, st_uid);
1913 CP(*in, *out, st_gid);
1914 CP(*in, *out, st_rdev);
1915 CP(*in, *out, st_size);
1916 TS_CP(*in, *out, st_atim);
1917 TS_CP(*in, *out, st_mtim);
1918 TS_CP(*in, *out, st_ctim);
1919 CP(*in, *out, st_blksize);
1920 CP(*in, *out, st_blocks);
1921 CP(*in, *out, st_flags);
1922 CP(*in, *out, st_gen);
1928 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1931 struct ostat32 sb32;
1934 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1938 copy_ostat(&sb, &sb32);
1939 error = copyout(&sb32, uap->ub, sizeof (sb32));
1945 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1951 error = kern_fstat(td, uap->fd, &ub);
1954 copy_stat(&ub, &ub32);
1955 error = copyout(&ub32, uap->ub, sizeof(ub32));
1961 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1964 struct ostat32 ub32;
1967 error = kern_fstat(td, uap->fd, &ub);
1970 copy_ostat(&ub, &ub32);
1971 error = copyout(&ub32, uap->ub, sizeof(ub32));
1977 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1983 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1987 copy_stat(&ub, &ub32);
1988 error = copyout(&ub32, uap->buf, sizeof(ub32));
1994 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1997 struct ostat32 sb32;
2000 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2001 UIO_USERSPACE, &sb, NULL);
2004 copy_ostat(&sb, &sb32);
2005 error = copyout(&sb32, uap->ub, sizeof (sb32));
2011 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2018 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2021 error = kern_fhstat(td, fh, &sb);
2024 copy_stat(&sb, &sb32);
2025 error = copyout(&sb32, uap->sb, sizeof (sb32));
2029 #if defined(COMPAT_FREEBSD11)
2030 extern int ino64_trunc_error;
2033 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2036 CP(*in, *out, st_ino);
2037 if (in->st_ino != out->st_ino) {
2038 switch (ino64_trunc_error) {
2045 out->st_ino = UINT32_MAX;
2049 CP(*in, *out, st_nlink);
2050 if (in->st_nlink != out->st_nlink) {
2051 switch (ino64_trunc_error) {
2058 out->st_nlink = UINT16_MAX;
2062 CP(*in, *out, st_dev);
2063 CP(*in, *out, st_mode);
2064 CP(*in, *out, st_uid);
2065 CP(*in, *out, st_gid);
2066 CP(*in, *out, st_rdev);
2067 TS_CP(*in, *out, st_atim);
2068 TS_CP(*in, *out, st_mtim);
2069 TS_CP(*in, *out, st_ctim);
2070 CP(*in, *out, st_size);
2071 CP(*in, *out, st_blocks);
2072 CP(*in, *out, st_blksize);
2073 CP(*in, *out, st_flags);
2074 CP(*in, *out, st_gen);
2075 TS_CP(*in, *out, st_birthtim);
2077 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2078 sizeof(*out) - offsetof(struct freebsd11_stat32,
2079 st_birthtim) - sizeof(out->st_birthtim));
2084 freebsd11_freebsd32_stat(struct thread *td,
2085 struct freebsd11_freebsd32_stat_args *uap)
2088 struct freebsd11_stat32 sb32;
2091 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2095 error = freebsd11_cvtstat32(&sb, &sb32);
2097 error = copyout(&sb32, uap->ub, sizeof (sb32));
2102 freebsd11_freebsd32_fstat(struct thread *td,
2103 struct freebsd11_freebsd32_fstat_args *uap)
2106 struct freebsd11_stat32 sb32;
2109 error = kern_fstat(td, uap->fd, &sb);
2112 error = freebsd11_cvtstat32(&sb, &sb32);
2114 error = copyout(&sb32, uap->ub, sizeof (sb32));
2119 freebsd11_freebsd32_fstatat(struct thread *td,
2120 struct freebsd11_freebsd32_fstatat_args *uap)
2123 struct freebsd11_stat32 sb32;
2126 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2130 error = freebsd11_cvtstat32(&sb, &sb32);
2132 error = copyout(&sb32, uap->buf, sizeof (sb32));
2137 freebsd11_freebsd32_lstat(struct thread *td,
2138 struct freebsd11_freebsd32_lstat_args *uap)
2141 struct freebsd11_stat32 sb32;
2144 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2145 UIO_USERSPACE, &sb, NULL);
2148 error = freebsd11_cvtstat32(&sb, &sb32);
2150 error = copyout(&sb32, uap->ub, sizeof (sb32));
2155 freebsd11_freebsd32_fhstat(struct thread *td,
2156 struct freebsd11_freebsd32_fhstat_args *uap)
2159 struct freebsd11_stat32 sb32;
2163 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2166 error = kern_fhstat(td, fh, &sb);
2169 error = freebsd11_cvtstat32(&sb, &sb32);
2171 error = copyout(&sb32, uap->sb, sizeof (sb32));
2177 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2179 int error, name[CTL_MAXNAME];
2183 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2185 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2189 error = fueword32(uap->oldlenp, &tmp);
2196 error = userland_sysctl(td, name, uap->namelen,
2197 uap->old, &oldlen, 1,
2198 uap->new, uap->newlen, &j, SCTL_MASK32);
2199 if (error && error != ENOMEM)
2202 suword32(uap->oldlenp, j);
2207 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2213 error = copyin(uap->jail, &version, sizeof(uint32_t));
2220 /* FreeBSD single IPv4 jails. */
2221 struct jail32_v0 j32_v0;
2223 bzero(&j, sizeof(struct jail));
2224 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2227 CP(j32_v0, j, version);
2228 PTRIN_CP(j32_v0, j, path);
2229 PTRIN_CP(j32_v0, j, hostname);
2230 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2236 * Version 1 was used by multi-IPv4 jail implementations
2237 * that never made it into the official kernel.
2241 case 2: /* JAIL_API_VERSION */
2243 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2246 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2249 CP(j32, j, version);
2250 PTRIN_CP(j32, j, path);
2251 PTRIN_CP(j32, j, hostname);
2252 PTRIN_CP(j32, j, jailname);
2255 PTRIN_CP(j32, j, ip4);
2256 PTRIN_CP(j32, j, ip6);
2261 /* Sci-Fi jails are not supported, sorry. */
2264 return (kern_jail(td, &j));
2268 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2273 /* Check that we have an even number of iovecs. */
2274 if (uap->iovcnt & 1)
2277 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2280 error = kern_jail_set(td, auio, uap->flags);
2286 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2288 struct iovec32 iov32;
2292 /* Check that we have an even number of iovecs. */
2293 if (uap->iovcnt & 1)
2296 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2299 error = kern_jail_get(td, auio, uap->flags);
2301 for (i = 0; i < uap->iovcnt; i++) {
2302 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2303 CP(auio->uio_iov[i], iov32, iov_len);
2304 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2313 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2315 struct sigaction32 s32;
2316 struct sigaction sa, osa, *sap;
2320 error = copyin(uap->act, &s32, sizeof(s32));
2323 sa.sa_handler = PTRIN(s32.sa_u);
2324 CP(s32, sa, sa_flags);
2325 CP(s32, sa, sa_mask);
2329 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2330 if (error == 0 && uap->oact != NULL) {
2331 s32.sa_u = PTROUT(osa.sa_handler);
2332 CP(osa, s32, sa_flags);
2333 CP(osa, s32, sa_mask);
2334 error = copyout(&s32, uap->oact, sizeof(s32));
2339 #ifdef COMPAT_FREEBSD4
2341 freebsd4_freebsd32_sigaction(struct thread *td,
2342 struct freebsd4_freebsd32_sigaction_args *uap)
2344 struct sigaction32 s32;
2345 struct sigaction sa, osa, *sap;
2349 error = copyin(uap->act, &s32, sizeof(s32));
2352 sa.sa_handler = PTRIN(s32.sa_u);
2353 CP(s32, sa, sa_flags);
2354 CP(s32, sa, sa_mask);
2358 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2359 if (error == 0 && uap->oact != NULL) {
2360 s32.sa_u = PTROUT(osa.sa_handler);
2361 CP(osa, s32, sa_flags);
2362 CP(osa, s32, sa_mask);
2363 error = copyout(&s32, uap->oact, sizeof(s32));
2370 struct osigaction32 {
2379 ofreebsd32_sigaction(struct thread *td,
2380 struct ofreebsd32_sigaction_args *uap)
2382 struct osigaction32 s32;
2383 struct sigaction sa, osa, *sap;
2386 if (uap->signum <= 0 || uap->signum >= ONSIG)
2390 error = copyin(uap->nsa, &s32, sizeof(s32));
2393 sa.sa_handler = PTRIN(s32.sa_u);
2394 CP(s32, sa, sa_flags);
2395 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2399 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2400 if (error == 0 && uap->osa != NULL) {
2401 s32.sa_u = PTROUT(osa.sa_handler);
2402 CP(osa, s32, sa_flags);
2403 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2404 error = copyout(&s32, uap->osa, sizeof(s32));
2410 ofreebsd32_sigprocmask(struct thread *td,
2411 struct ofreebsd32_sigprocmask_args *uap)
2416 OSIG2SIG(uap->mask, set);
2417 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2418 SIG2OSIG(oset, td->td_retval[0]);
2423 ofreebsd32_sigpending(struct thread *td,
2424 struct ofreebsd32_sigpending_args *uap)
2426 struct proc *p = td->td_proc;
2430 siglist = p->p_siglist;
2431 SIGSETOR(siglist, td->td_siglist);
2433 SIG2OSIG(siglist, td->td_retval[0]);
2438 u_int32_t sv_handler;
2444 ofreebsd32_sigvec(struct thread *td,
2445 struct ofreebsd32_sigvec_args *uap)
2447 struct sigvec32 vec;
2448 struct sigaction sa, osa, *sap;
2451 if (uap->signum <= 0 || uap->signum >= ONSIG)
2455 error = copyin(uap->nsv, &vec, sizeof(vec));
2458 sa.sa_handler = PTRIN(vec.sv_handler);
2459 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2460 sa.sa_flags = vec.sv_flags;
2461 sa.sa_flags ^= SA_RESTART;
2465 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2466 if (error == 0 && uap->osv != NULL) {
2467 vec.sv_handler = PTROUT(osa.sa_handler);
2468 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2469 vec.sv_flags = osa.sa_flags;
2470 vec.sv_flags &= ~SA_NOCLDWAIT;
2471 vec.sv_flags ^= SA_RESTART;
2472 error = copyout(&vec, uap->osv, sizeof(vec));
2478 ofreebsd32_sigblock(struct thread *td,
2479 struct ofreebsd32_sigblock_args *uap)
2483 OSIG2SIG(uap->mask, set);
2484 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2485 SIG2OSIG(oset, td->td_retval[0]);
2490 ofreebsd32_sigsetmask(struct thread *td,
2491 struct ofreebsd32_sigsetmask_args *uap)
2495 OSIG2SIG(uap->mask, set);
2496 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2497 SIG2OSIG(oset, td->td_retval[0]);
2502 ofreebsd32_sigsuspend(struct thread *td,
2503 struct ofreebsd32_sigsuspend_args *uap)
2507 OSIG2SIG(uap->mask, mask);
2508 return (kern_sigsuspend(td, mask));
2517 ofreebsd32_sigstack(struct thread *td,
2518 struct ofreebsd32_sigstack_args *uap)
2520 struct sigstack32 s32;
2521 struct sigstack nss, oss;
2522 int error = 0, unss;
2524 if (uap->nss != NULL) {
2525 error = copyin(uap->nss, &s32, sizeof(s32));
2528 nss.ss_sp = PTRIN(s32.ss_sp);
2529 CP(s32, nss, ss_onstack);
2534 oss.ss_sp = td->td_sigstk.ss_sp;
2535 oss.ss_onstack = sigonstack(cpu_getstack(td));
2537 td->td_sigstk.ss_sp = nss.ss_sp;
2538 td->td_sigstk.ss_size = 0;
2539 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2540 td->td_pflags |= TDP_ALTSTACK;
2542 if (uap->oss != NULL) {
2543 s32.ss_sp = PTROUT(oss.ss_sp);
2544 CP(oss, s32, ss_onstack);
2545 error = copyout(&s32, uap->oss, sizeof(s32));
2552 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2555 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2556 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2560 freebsd32_clock_nanosleep(struct thread *td,
2561 struct freebsd32_clock_nanosleep_args *uap)
2565 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2566 uap->rqtp, uap->rmtp);
2567 return (kern_posix_error(td, error));
2571 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2572 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2574 struct timespec32 rmt32, rqt32;
2575 struct timespec rmt, rqt;
2578 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2582 CP(rqt32, rqt, tv_sec);
2583 CP(rqt32, rqt, tv_nsec);
2585 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2586 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2588 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2589 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2592 CP(rmt, rmt32, tv_sec);
2593 CP(rmt, rmt32, tv_nsec);
2595 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2603 freebsd32_clock_gettime(struct thread *td,
2604 struct freebsd32_clock_gettime_args *uap)
2606 struct timespec ats;
2607 struct timespec32 ats32;
2610 error = kern_clock_gettime(td, uap->clock_id, &ats);
2612 CP(ats, ats32, tv_sec);
2613 CP(ats, ats32, tv_nsec);
2614 error = copyout(&ats32, uap->tp, sizeof(ats32));
2620 freebsd32_clock_settime(struct thread *td,
2621 struct freebsd32_clock_settime_args *uap)
2623 struct timespec ats;
2624 struct timespec32 ats32;
2627 error = copyin(uap->tp, &ats32, sizeof(ats32));
2630 CP(ats32, ats, tv_sec);
2631 CP(ats32, ats, tv_nsec);
2633 return (kern_clock_settime(td, uap->clock_id, &ats));
2637 freebsd32_clock_getres(struct thread *td,
2638 struct freebsd32_clock_getres_args *uap)
2641 struct timespec32 ts32;
2644 if (uap->tp == NULL)
2646 error = kern_clock_getres(td, uap->clock_id, &ts);
2648 CP(ts, ts32, tv_sec);
2649 CP(ts, ts32, tv_nsec);
2650 error = copyout(&ts32, uap->tp, sizeof(ts32));
2655 int freebsd32_ktimer_create(struct thread *td,
2656 struct freebsd32_ktimer_create_args *uap)
2658 struct sigevent32 ev32;
2659 struct sigevent ev, *evp;
2662 if (uap->evp == NULL) {
2666 error = copyin(uap->evp, &ev32, sizeof(ev32));
2669 error = convert_sigevent32(&ev32, &ev);
2673 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2675 error = copyout(&id, uap->timerid, sizeof(int));
2677 kern_ktimer_delete(td, id);
2683 freebsd32_ktimer_settime(struct thread *td,
2684 struct freebsd32_ktimer_settime_args *uap)
2686 struct itimerspec32 val32, oval32;
2687 struct itimerspec val, oval, *ovalp;
2690 error = copyin(uap->value, &val32, sizeof(val32));
2694 ovalp = uap->ovalue != NULL ? &oval : NULL;
2695 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2696 if (error == 0 && uap->ovalue != NULL) {
2697 ITS_CP(oval, oval32);
2698 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2704 freebsd32_ktimer_gettime(struct thread *td,
2705 struct freebsd32_ktimer_gettime_args *uap)
2707 struct itimerspec32 val32;
2708 struct itimerspec val;
2711 error = kern_ktimer_gettime(td, uap->timerid, &val);
2714 error = copyout(&val32, uap->value, sizeof(val32));
2720 freebsd32_clock_getcpuclockid2(struct thread *td,
2721 struct freebsd32_clock_getcpuclockid2_args *uap)
2726 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2727 uap->which, &clk_id);
2729 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2734 freebsd32_thr_new(struct thread *td,
2735 struct freebsd32_thr_new_args *uap)
2737 struct thr_param32 param32;
2738 struct thr_param param;
2741 if (uap->param_size < 0 ||
2742 uap->param_size > sizeof(struct thr_param32))
2744 bzero(¶m, sizeof(struct thr_param));
2745 bzero(¶m32, sizeof(struct thr_param32));
2746 error = copyin(uap->param, ¶m32, uap->param_size);
2749 param.start_func = PTRIN(param32.start_func);
2750 param.arg = PTRIN(param32.arg);
2751 param.stack_base = PTRIN(param32.stack_base);
2752 param.stack_size = param32.stack_size;
2753 param.tls_base = PTRIN(param32.tls_base);
2754 param.tls_size = param32.tls_size;
2755 param.child_tid = PTRIN(param32.child_tid);
2756 param.parent_tid = PTRIN(param32.parent_tid);
2757 param.flags = param32.flags;
2758 param.rtp = PTRIN(param32.rtp);
2759 param.spare[0] = PTRIN(param32.spare[0]);
2760 param.spare[1] = PTRIN(param32.spare[1]);
2761 param.spare[2] = PTRIN(param32.spare[2]);
2763 return (kern_thr_new(td, ¶m));
2767 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2769 struct timespec32 ts32;
2770 struct timespec ts, *tsp;
2775 if (uap->timeout != NULL) {
2776 error = copyin((const void *)uap->timeout, (void *)&ts32,
2777 sizeof(struct timespec32));
2780 ts.tv_sec = ts32.tv_sec;
2781 ts.tv_nsec = ts32.tv_nsec;
2784 return (kern_thr_suspend(td, tsp));
2788 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2790 bzero(dst, sizeof(*dst));
2791 dst->si_signo = src->si_signo;
2792 dst->si_errno = src->si_errno;
2793 dst->si_code = src->si_code;
2794 dst->si_pid = src->si_pid;
2795 dst->si_uid = src->si_uid;
2796 dst->si_status = src->si_status;
2797 dst->si_addr = (uintptr_t)src->si_addr;
2798 dst->si_value.sival_int = src->si_value.sival_int;
2799 dst->si_timerid = src->si_timerid;
2800 dst->si_overrun = src->si_overrun;
2803 #ifndef _FREEBSD32_SYSPROTO_H_
2804 struct freebsd32_sigqueue_args {
2807 /* union sigval32 */ int value;
2811 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2816 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2817 * On 64-bit little-endian ABIs, the low bits are the same.
2818 * In 64-bit big-endian ABIs, sival_int overlaps with
2819 * sival_ptr's HIGH bits. We choose to support sival_int
2820 * rather than sival_ptr in this case as it seems to be
2823 bzero(&sv, sizeof(sv));
2824 sv.sival_int = uap->value;
2826 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2830 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2832 struct timespec32 ts32;
2834 struct timespec *timeout;
2837 struct siginfo32 si32;
2841 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2844 ts.tv_sec = ts32.tv_sec;
2845 ts.tv_nsec = ts32.tv_nsec;
2850 error = copyin(uap->set, &set, sizeof(set));
2854 error = kern_sigtimedwait(td, set, &ksi, timeout);
2859 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2860 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2864 td->td_retval[0] = ksi.ksi_signo;
2872 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2875 struct siginfo32 si32;
2879 error = copyin(uap->set, &set, sizeof(set));
2883 error = kern_sigtimedwait(td, set, &ksi, NULL);
2888 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2889 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2892 td->td_retval[0] = ksi.ksi_signo;
2897 freebsd32_cpuset_setid(struct thread *td,
2898 struct freebsd32_cpuset_setid_args *uap)
2901 return (kern_cpuset_setid(td, uap->which,
2902 PAIR32TO64(id_t, uap->id), uap->setid));
2906 freebsd32_cpuset_getid(struct thread *td,
2907 struct freebsd32_cpuset_getid_args *uap)
2910 return (kern_cpuset_getid(td, uap->level, uap->which,
2911 PAIR32TO64(id_t, uap->id), uap->setid));
2915 freebsd32_cpuset_getaffinity(struct thread *td,
2916 struct freebsd32_cpuset_getaffinity_args *uap)
2919 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2920 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2924 freebsd32_cpuset_setaffinity(struct thread *td,
2925 struct freebsd32_cpuset_setaffinity_args *uap)
2928 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2929 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2933 freebsd32_nmount(struct thread *td,
2934 struct freebsd32_nmount_args /* {
2936 unsigned int iovcnt;
2945 * Mount flags are now 64-bits. On 32-bit archtectures only
2946 * 32-bits are passed in, but from here on everything handles
2947 * 64-bit flags correctly.
2951 AUDIT_ARG_FFLAGS(flags);
2954 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2955 * userspace to set this flag, but we must filter it out if we want
2956 * MNT_UPDATE on the root file system to work.
2957 * MNT_ROOTFS should only be set by the kernel when mounting its
2960 flags &= ~MNT_ROOTFS;
2963 * check that we have an even number of iovec's
2964 * and that we have at least two options.
2966 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2969 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2972 error = vfs_donmount(td, flags, auio);
2980 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2982 struct yyy32 *p32, s32;
2983 struct yyy *p = NULL, s;
2988 error = copyin(uap->zzz, &s32, sizeof(s32));
2994 error = kern_xxx(td, p);
2999 error = copyout(&s32, p32, sizeof(s32));
3006 syscall32_register(int *offset, struct sysent *new_sysent,
3007 struct sysent *old_sysent, int flags)
3010 if ((flags & ~SY_THR_STATIC) != 0)
3013 if (*offset == NO_SYSCALL) {
3016 for (i = 1; i < SYS_MAXSYSCALL; ++i)
3017 if (freebsd32_sysent[i].sy_call ==
3018 (sy_call_t *)lkmnosys)
3020 if (i == SYS_MAXSYSCALL)
3023 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
3025 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
3026 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
3029 *old_sysent = freebsd32_sysent[*offset];
3030 freebsd32_sysent[*offset] = *new_sysent;
3031 atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
3036 syscall32_deregister(int *offset, struct sysent *old_sysent)
3042 freebsd32_sysent[*offset] = *old_sysent;
3047 syscall32_module_handler(struct module *mod, int what, void *arg)
3049 struct syscall_module_data *data = (struct syscall_module_data*)arg;
3055 error = syscall32_register(data->offset, data->new_sysent,
3056 &data->old_sysent, SY_THR_STATIC_KLD);
3058 /* Leave a mark so we know to safely unload below. */
3059 data->offset = NULL;
3062 ms.intval = *data->offset;
3064 module_setspecific(mod, &ms);
3067 error = data->chainevh(mod, what, data->chainarg);
3071 * MOD_LOAD failed, so just return without calling the
3072 * chained handler since we didn't pass along the MOD_LOAD
3075 if (data->offset == NULL)
3077 if (data->chainevh) {
3078 error = data->chainevh(mod, what, data->chainarg);
3082 error = syscall32_deregister(data->offset, &data->old_sysent);
3087 error = data->chainevh(mod, what, data->chainarg);
3093 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3095 struct syscall_helper_data *sd1;
3098 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
3099 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
3100 &sd1->old_sysent, flags);
3102 syscall32_helper_unregister(sd);
3105 sd1->registered = 1;
3111 syscall32_helper_unregister(struct syscall_helper_data *sd)
3113 struct syscall_helper_data *sd1;
3115 for (sd1 = sd; sd1->registered != 0; sd1++) {
3116 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
3117 sd1->registered = 0;
3123 freebsd32_copyout_strings(struct image_params *imgp)
3129 u_int32_t *stack_base;
3130 struct freebsd32_ps_strings *arginfo;
3131 char canary[sizeof(long) * 8];
3132 int32_t pagesizes32[MAXPAGESIZES];
3133 size_t execpath_len;
3137 * Calculate string base and vector table pointers.
3138 * Also deal with signal trampoline code for this exec type.
3140 if (imgp->execpath != NULL && imgp->auxargs != NULL)
3141 execpath_len = strlen(imgp->execpath) + 1;
3144 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3146 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3147 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3150 destp = (uintptr_t)arginfo;
3155 if (szsigcode != 0) {
3157 destp = rounddown2(destp, sizeof(uint32_t));
3158 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3163 * Copy the image path for the rtld.
3165 if (execpath_len != 0) {
3166 destp -= execpath_len;
3167 imgp->execpathp = destp;
3168 copyout(imgp->execpath, (void *)destp, execpath_len);
3172 * Prepare the canary for SSP.
3174 arc4rand(canary, sizeof(canary), 0);
3175 destp -= sizeof(canary);
3176 imgp->canary = destp;
3177 copyout(canary, (void *)destp, sizeof(canary));
3178 imgp->canarylen = sizeof(canary);
3181 * Prepare the pagesizes array.
3183 for (i = 0; i < MAXPAGESIZES; i++)
3184 pagesizes32[i] = (uint32_t)pagesizes[i];
3185 destp -= sizeof(pagesizes32);
3186 destp = rounddown2(destp, sizeof(uint32_t));
3187 imgp->pagesizes = destp;
3188 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3189 imgp->pagesizeslen = sizeof(pagesizes32);
3191 destp -= ARG_MAX - imgp->args->stringspace;
3192 destp = rounddown2(destp, sizeof(uint32_t));
3195 * If we have a valid auxargs ptr, prepare some room
3198 if (imgp->auxargs) {
3200 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
3201 * lower compatibility.
3203 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
3206 * The '+ 2' is for the null pointers at the end of each of
3207 * the arg and env vector sets,and imgp->auxarg_size is room
3208 * for argument of Runtime loader.
3210 vectp = (u_int32_t *) (destp - (imgp->args->argc +
3211 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
3215 * The '+ 2' is for the null pointers at the end of each of
3216 * the arg and env vector sets
3218 vectp = (u_int32_t *)(destp - (imgp->args->argc +
3219 imgp->args->envc + 2) * sizeof(u_int32_t));
3223 * vectp also becomes our initial stack base
3227 stringp = imgp->args->begin_argv;
3228 argc = imgp->args->argc;
3229 envc = imgp->args->envc;
3231 * Copy out strings - arguments and environment.
3233 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3236 * Fill in "ps_strings" struct for ps, w, etc.
3238 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3239 suword32(&arginfo->ps_nargvstr, argc);
3242 * Fill in argument portion of vector table.
3244 for (; argc > 0; --argc) {
3245 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3246 while (*stringp++ != 0)
3251 /* a null vector table pointer separates the argp's from the envp's */
3252 suword32(vectp++, 0);
3254 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3255 suword32(&arginfo->ps_nenvstr, envc);
3258 * Fill in environment portion of vector table.
3260 for (; envc > 0; --envc) {
3261 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3262 while (*stringp++ != 0)
3267 /* end of vector table is a null pointer */
3270 return ((register_t *)stack_base);
3274 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3276 struct kld_file_stat stat;
3277 struct kld32_file_stat stat32;
3280 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3283 if (version != sizeof(struct kld32_file_stat_1) &&
3284 version != sizeof(struct kld32_file_stat))
3287 error = kern_kldstat(td, uap->fileid, &stat);
3291 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
3292 CP(stat, stat32, refs);
3293 CP(stat, stat32, id);
3294 PTROUT_CP(stat, stat32, address);
3295 CP(stat, stat32, size);
3296 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
3297 return (copyout(&stat32, uap->stat, version));
3301 freebsd32_posix_fallocate(struct thread *td,
3302 struct freebsd32_posix_fallocate_args *uap)
3306 error = kern_posix_fallocate(td, uap->fd,
3307 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3308 return (kern_posix_error(td, error));
3312 freebsd32_posix_fadvise(struct thread *td,
3313 struct freebsd32_posix_fadvise_args *uap)
3317 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3318 PAIR32TO64(off_t, uap->len), uap->advice);
3319 return (kern_posix_error(td, error));
3323 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3326 CP(*sig32, *sig, sigev_notify);
3327 switch (sig->sigev_notify) {
3330 case SIGEV_THREAD_ID:
3331 CP(*sig32, *sig, sigev_notify_thread_id);
3334 CP(*sig32, *sig, sigev_signo);
3335 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3338 CP(*sig32, *sig, sigev_notify_kqueue);
3339 CP(*sig32, *sig, sigev_notify_kevent_flags);
3340 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3349 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3353 struct procctl_reaper_status rs;
3354 struct procctl_reaper_pids rp;
3355 struct procctl_reaper_kill rk;
3358 struct procctl_reaper_pids32 rp;
3360 int error, error1, flags;
3364 case PROC_TRACE_CTL:
3365 case PROC_TRAPCAP_CTL:
3366 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3371 case PROC_REAP_ACQUIRE:
3372 case PROC_REAP_RELEASE:
3373 if (uap->data != NULL)
3377 case PROC_REAP_STATUS:
3380 case PROC_REAP_GETPIDS:
3381 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3384 CP(x32.rp, x.rp, rp_count);
3385 PTRIN_CP(x32.rp, x.rp, rp_pids);
3388 case PROC_REAP_KILL:
3389 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3394 case PROC_TRACE_STATUS:
3395 case PROC_TRAPCAP_STATUS:
3401 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3404 case PROC_REAP_STATUS:
3406 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3408 case PROC_REAP_KILL:
3409 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3413 case PROC_TRACE_STATUS:
3414 case PROC_TRAPCAP_STATUS:
3416 error = copyout(&flags, uap->data, sizeof(flags));
3423 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3429 * Do unsigned conversion for arg when operation
3430 * interprets it as flags or pointer.
3432 case F_SETLK_REMOTE:
3441 tmp = (unsigned int)(uap->arg);
3447 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3451 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3453 struct timespec32 ts32;
3454 struct timespec ts, *tsp;
3458 if (uap->ts != NULL) {
3459 error = copyin(uap->ts, &ts32, sizeof(ts32));
3462 CP(ts32, ts, tv_sec);
3463 CP(ts32, ts, tv_nsec);
3467 if (uap->set != NULL) {
3468 error = copyin(uap->set, &set, sizeof(set));
3475 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));