2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
34 #include "opt_ktrace.h"
36 #define __ELF_WORD_SIZE 32
38 #ifdef COMPAT_FREEBSD11
39 #define _WANT_FREEBSD11_KEVENT
42 #include <sys/param.h>
44 #include <sys/capsicum.h>
45 #include <sys/clock.h>
47 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/imgact.h>
51 #include <sys/kernel.h>
52 #include <sys/limits.h>
53 #include <sys/linker.h>
55 #include <sys/malloc.h>
56 #include <sys/file.h> /* Must come after sys/malloc.h */
57 #include <sys/imgact.h>
60 #include <sys/module.h>
61 #include <sys/mount.h>
62 #include <sys/mutex.h>
63 #include <sys/namei.h>
65 #include <sys/procctl.h>
66 #include <sys/reboot.h>
67 #include <sys/resource.h>
68 #include <sys/resourcevar.h>
69 #include <sys/selinfo.h>
70 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
71 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
72 #include <sys/signal.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/syscall.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <sys/sysent.h>
81 #include <sys/sysproto.h>
82 #include <sys/systm.h>
84 #include <sys/unistd.h>
85 #include <sys/ucontext.h>
86 #include <sys/vnode.h>
93 #include <sys/ktrace.h>
97 #include <netinet/in.h>
101 #include <vm/vm_param.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_extern.h>
107 #include <machine/cpu.h>
108 #include <machine/elf.h>
110 #include <machine/md_var.h>
113 #include <security/audit/audit.h>
115 #include <compat/freebsd32/freebsd32_util.h>
116 #include <compat/freebsd32/freebsd32.h>
117 #include <compat/freebsd32/freebsd32_ipc.h>
118 #include <compat/freebsd32/freebsd32_misc.h>
119 #include <compat/freebsd32/freebsd32_signal.h>
120 #include <compat/freebsd32/freebsd32_proto.h>
122 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
125 CTASSERT(sizeof(struct timeval32) == 8);
126 CTASSERT(sizeof(struct timespec32) == 8);
127 CTASSERT(sizeof(struct itimerval32) == 16);
128 CTASSERT(sizeof(struct bintime32) == 12);
130 CTASSERT(sizeof(struct statfs32) == 256);
132 CTASSERT(sizeof(struct rusage32) == 72);
134 CTASSERT(sizeof(struct sigaltstack32) == 12);
136 CTASSERT(sizeof(struct kevent32) == 56);
138 CTASSERT(sizeof(struct kevent32) == 64);
140 CTASSERT(sizeof(struct iovec32) == 8);
141 CTASSERT(sizeof(struct msghdr32) == 28);
143 CTASSERT(sizeof(struct stat32) == 208);
144 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
146 CTASSERT(sizeof(struct sigaction32) == 24);
148 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
149 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
150 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
151 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
154 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
157 TV_CP(*s, *s32, ru_utime);
158 TV_CP(*s, *s32, ru_stime);
159 CP(*s, *s32, ru_maxrss);
160 CP(*s, *s32, ru_ixrss);
161 CP(*s, *s32, ru_idrss);
162 CP(*s, *s32, ru_isrss);
163 CP(*s, *s32, ru_minflt);
164 CP(*s, *s32, ru_majflt);
165 CP(*s, *s32, ru_nswap);
166 CP(*s, *s32, ru_inblock);
167 CP(*s, *s32, ru_oublock);
168 CP(*s, *s32, ru_msgsnd);
169 CP(*s, *s32, ru_msgrcv);
170 CP(*s, *s32, ru_nsignals);
171 CP(*s, *s32, ru_nvcsw);
172 CP(*s, *s32, ru_nivcsw);
176 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
179 struct rusage32 ru32;
180 struct rusage ru, *rup;
182 if (uap->rusage != NULL)
186 error = kern_wait(td, uap->pid, &status, uap->options, rup);
189 if (uap->status != NULL)
190 error = copyout(&status, uap->status, sizeof(status));
191 if (uap->rusage != NULL && error == 0) {
192 freebsd32_rusage_out(&ru, &ru32);
193 error = copyout(&ru32, uap->rusage, sizeof(ru32));
199 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
201 struct wrusage32 wru32;
202 struct __wrusage wru, *wrup;
203 struct siginfo32 si32;
204 struct __siginfo si, *sip;
207 if (uap->wrusage != NULL)
211 if (uap->info != NULL) {
213 bzero(sip, sizeof(*sip));
216 error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
217 &status, uap->options, wrup, sip);
220 if (uap->status != NULL)
221 error = copyout(&status, uap->status, sizeof(status));
222 if (uap->wrusage != NULL && error == 0) {
223 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
224 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
225 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
227 if (uap->info != NULL && error == 0) {
228 siginfo_to_siginfo32 (&si, &si32);
229 error = copyout(&si32, uap->info, sizeof(si32));
234 #ifdef COMPAT_FREEBSD4
236 copy_statfs(struct statfs *in, struct statfs32 *out)
239 statfs_scale_blocks(in, INT32_MAX);
240 bzero(out, sizeof(*out));
241 CP(*in, *out, f_bsize);
242 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
243 CP(*in, *out, f_blocks);
244 CP(*in, *out, f_bfree);
245 CP(*in, *out, f_bavail);
246 out->f_files = MIN(in->f_files, INT32_MAX);
247 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
248 CP(*in, *out, f_fsid);
249 CP(*in, *out, f_owner);
250 CP(*in, *out, f_type);
251 CP(*in, *out, f_flags);
252 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
253 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
254 strlcpy(out->f_fstypename,
255 in->f_fstypename, MFSNAMELEN);
256 strlcpy(out->f_mntonname,
257 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
258 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
259 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
260 strlcpy(out->f_mntfromname,
261 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
265 #ifdef COMPAT_FREEBSD4
267 freebsd4_freebsd32_getfsstat(struct thread *td,
268 struct freebsd4_freebsd32_getfsstat_args *uap)
270 struct statfs *buf, *sp;
271 struct statfs32 stat32;
272 size_t count, size, copycount;
275 count = uap->bufsize / sizeof(struct statfs32);
276 size = count * sizeof(struct statfs);
277 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
281 while (copycount > 0 && error == 0) {
282 copy_statfs(sp, &stat32);
283 error = copyout(&stat32, uap->buf, sizeof(stat32));
291 td->td_retval[0] = count;
296 #ifdef COMPAT_FREEBSD10
298 freebsd10_freebsd32_pipe(struct thread *td,
299 struct freebsd10_freebsd32_pipe_args *uap) {
301 return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
306 freebsd32_sigaltstack(struct thread *td,
307 struct freebsd32_sigaltstack_args *uap)
309 struct sigaltstack32 s32;
310 struct sigaltstack ss, oss, *ssp;
313 if (uap->ss != NULL) {
314 error = copyin(uap->ss, &s32, sizeof(s32));
317 PTRIN_CP(s32, ss, ss_sp);
318 CP(s32, ss, ss_size);
319 CP(s32, ss, ss_flags);
323 error = kern_sigaltstack(td, ssp, &oss);
324 if (error == 0 && uap->oss != NULL) {
325 PTROUT_CP(oss, s32, ss_sp);
326 CP(oss, s32, ss_size);
327 CP(oss, s32, ss_flags);
328 error = copyout(&s32, uap->oss, sizeof(s32));
334 * Custom version of exec_copyin_args() so that we can translate
338 freebsd32_exec_copyin_args(struct image_args *args, const char *fname,
339 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
345 bzero(args, sizeof(*args));
350 * Allocate demand-paged memory for the file name, argument, and
351 * environment strings.
353 error = exec_alloc_args(args);
358 * Copy the file name.
360 error = exec_args_add_fname(args, fname, segflg);
365 * extract arguments first
369 error = copyin(p32++, &arg, sizeof(arg));
375 error = exec_args_add_arg(args, argp, UIO_USERSPACE);
381 * extract environment strings
386 error = copyin(p32++, &arg, sizeof(arg));
392 error = exec_args_add_env(args, envp, UIO_USERSPACE);
401 exec_free_args(args);
406 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
408 struct image_args eargs;
409 struct vmspace *oldvmspace;
412 error = pre_execve(td, &oldvmspace);
415 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
416 uap->argv, uap->envv);
418 error = kern_execve(td, &eargs, NULL);
419 post_execve(td, error, oldvmspace);
424 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
426 struct image_args eargs;
427 struct vmspace *oldvmspace;
430 error = pre_execve(td, &oldvmspace);
433 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
434 uap->argv, uap->envv);
437 error = kern_execve(td, &eargs, NULL);
439 post_execve(td, error, oldvmspace);
445 freebsd32_mknodat(struct thread *td, struct freebsd32_mknodat_args *uap)
448 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE,
449 uap->mode, PAIR32TO64(dev_t, uap->dev)));
453 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
458 #if defined(__amd64__)
459 if (i386_read_exec && (prot & PROT_READ) != 0)
462 return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
467 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
472 #if defined(__amd64__)
473 if (i386_read_exec && (prot & PROT_READ))
477 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
478 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
481 #ifdef COMPAT_FREEBSD6
483 freebsd6_freebsd32_mmap(struct thread *td,
484 struct freebsd6_freebsd32_mmap_args *uap)
489 #if defined(__amd64__)
490 if (i386_read_exec && (prot & PROT_READ))
494 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
495 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
500 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
502 struct itimerval itv, oitv, *itvp;
503 struct itimerval32 i32;
506 if (uap->itv != NULL) {
507 error = copyin(uap->itv, &i32, sizeof(i32));
510 TV_CP(i32, itv, it_interval);
511 TV_CP(i32, itv, it_value);
515 error = kern_setitimer(td, uap->which, itvp, &oitv);
516 if (error || uap->oitv == NULL)
518 TV_CP(oitv, i32, it_interval);
519 TV_CP(oitv, i32, it_value);
520 return (copyout(&i32, uap->oitv, sizeof(i32)));
524 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
526 struct itimerval itv;
527 struct itimerval32 i32;
530 error = kern_getitimer(td, uap->which, &itv);
531 if (error || uap->itv == NULL)
533 TV_CP(itv, i32, it_interval);
534 TV_CP(itv, i32, it_value);
535 return (copyout(&i32, uap->itv, sizeof(i32)));
539 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
541 struct timeval32 tv32;
542 struct timeval tv, *tvp;
545 if (uap->tv != NULL) {
546 error = copyin(uap->tv, &tv32, sizeof(tv32));
549 CP(tv32, tv, tv_sec);
550 CP(tv32, tv, tv_usec);
555 * XXX Do pointers need PTRIN()?
557 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
558 sizeof(int32_t) * 8));
562 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
564 struct timespec32 ts32;
566 struct timeval tv, *tvp;
570 if (uap->ts != NULL) {
571 error = copyin(uap->ts, &ts32, sizeof(ts32));
574 CP(ts32, ts, tv_sec);
575 CP(ts32, ts, tv_nsec);
576 TIMESPEC_TO_TIMEVAL(&tv, &ts);
580 if (uap->sm != NULL) {
581 error = copyin(uap->sm, &set, sizeof(set));
588 * XXX Do pointers need PTRIN()?
590 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
591 uset, sizeof(int32_t) * 8);
596 * Copy 'count' items into the destination list pointed to by uap->eventlist.
599 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
601 struct freebsd32_kevent_args *uap;
602 struct kevent32 ks32[KQ_NEVENTS];
606 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
607 uap = (struct freebsd32_kevent_args *)arg;
609 for (i = 0; i < count; i++) {
610 CP(kevp[i], ks32[i], ident);
611 CP(kevp[i], ks32[i], filter);
612 CP(kevp[i], ks32[i], flags);
613 CP(kevp[i], ks32[i], fflags);
614 #if BYTE_ORDER == LITTLE_ENDIAN
615 ks32[i].data1 = kevp[i].data;
616 ks32[i].data2 = kevp[i].data >> 32;
618 ks32[i].data1 = kevp[i].data >> 32;
619 ks32[i].data2 = kevp[i].data;
621 PTROUT_CP(kevp[i], ks32[i], udata);
622 for (j = 0; j < nitems(kevp->ext); j++) {
624 #if BYTE_ORDER == LITTLE_ENDIAN
625 ks32[i].ext64[2 * j] = e;
626 ks32[i].ext64[2 * j + 1] = e >> 32;
628 ks32[i].ext64[2 * j] = e >> 32;
629 ks32[i].ext64[2 * j + 1] = e;
633 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
635 uap->eventlist += count;
640 * Copy 'count' items from the list pointed to by uap->changelist.
643 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
645 struct freebsd32_kevent_args *uap;
646 struct kevent32 ks32[KQ_NEVENTS];
650 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
651 uap = (struct freebsd32_kevent_args *)arg;
653 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
656 uap->changelist += count;
658 for (i = 0; i < count; i++) {
659 CP(ks32[i], kevp[i], ident);
660 CP(ks32[i], kevp[i], filter);
661 CP(ks32[i], kevp[i], flags);
662 CP(ks32[i], kevp[i], fflags);
663 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
664 PTRIN_CP(ks32[i], kevp[i], udata);
665 for (j = 0; j < nitems(kevp->ext); j++) {
666 #if BYTE_ORDER == LITTLE_ENDIAN
667 e = ks32[i].ext64[2 * j + 1];
669 e += ks32[i].ext64[2 * j];
671 e = ks32[i].ext64[2 * j];
673 e += ks32[i].ext64[2 * j + 1];
683 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
685 struct timespec32 ts32;
686 struct timespec ts, *tsp;
687 struct kevent_copyops k_ops = {
689 .k_copyout = freebsd32_kevent_copyout,
690 .k_copyin = freebsd32_kevent_copyin,
693 struct kevent32 *eventlist = uap->eventlist;
698 error = copyin(uap->timeout, &ts32, sizeof(ts32));
701 CP(ts32, ts, tv_sec);
702 CP(ts32, ts, tv_nsec);
707 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
708 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
709 uap->nchanges, sizeof(struct kevent32));
711 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
714 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
715 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
716 td->td_retval[0], sizeof(struct kevent32));
721 #ifdef COMPAT_FREEBSD11
723 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
725 struct freebsd11_freebsd32_kevent_args *uap;
726 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
729 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
730 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
732 for (i = 0; i < count; i++) {
733 CP(kevp[i], ks32[i], ident);
734 CP(kevp[i], ks32[i], filter);
735 CP(kevp[i], ks32[i], flags);
736 CP(kevp[i], ks32[i], fflags);
737 CP(kevp[i], ks32[i], data);
738 PTROUT_CP(kevp[i], ks32[i], udata);
740 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
742 uap->eventlist += count;
747 * Copy 'count' items from the list pointed to by uap->changelist.
750 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
752 struct freebsd11_freebsd32_kevent_args *uap;
753 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
756 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
757 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
759 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
762 uap->changelist += count;
764 for (i = 0; i < count; i++) {
765 CP(ks32[i], kevp[i], ident);
766 CP(ks32[i], kevp[i], filter);
767 CP(ks32[i], kevp[i], flags);
768 CP(ks32[i], kevp[i], fflags);
769 CP(ks32[i], kevp[i], data);
770 PTRIN_CP(ks32[i], kevp[i], udata);
771 for (j = 0; j < nitems(kevp->ext); j++)
779 freebsd11_freebsd32_kevent(struct thread *td,
780 struct freebsd11_freebsd32_kevent_args *uap)
782 struct timespec32 ts32;
783 struct timespec ts, *tsp;
784 struct kevent_copyops k_ops = {
786 .k_copyout = freebsd32_kevent11_copyout,
787 .k_copyin = freebsd32_kevent11_copyin,
790 struct kevent32_freebsd11 *eventlist = uap->eventlist;
795 error = copyin(uap->timeout, &ts32, sizeof(ts32));
798 CP(ts32, ts, tv_sec);
799 CP(ts32, ts, tv_nsec);
804 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
805 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
806 uap->changelist, uap->nchanges,
807 sizeof(struct kevent32_freebsd11));
809 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
812 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
813 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
814 eventlist, td->td_retval[0],
815 sizeof(struct kevent32_freebsd11));
822 freebsd32_gettimeofday(struct thread *td,
823 struct freebsd32_gettimeofday_args *uap)
826 struct timeval32 atv32;
832 CP(atv, atv32, tv_sec);
833 CP(atv, atv32, tv_usec);
834 error = copyout(&atv32, uap->tp, sizeof (atv32));
836 if (error == 0 && uap->tzp != NULL) {
837 rtz.tz_minuteswest = 0;
839 error = copyout(&rtz, uap->tzp, sizeof (rtz));
845 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
851 error = kern_getrusage(td, uap->who, &s);
853 freebsd32_rusage_out(&s, &s32);
854 error = copyout(&s32, uap->rusage, sizeof(s32));
860 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
862 struct iovec32 iov32;
869 if (iovcnt > UIO_MAXIOV)
871 iovlen = iovcnt * sizeof(struct iovec);
872 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
873 iov = (struct iovec *)(uio + 1);
874 for (i = 0; i < iovcnt; i++) {
875 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
880 iov[i].iov_base = PTRIN(iov32.iov_base);
881 iov[i].iov_len = iov32.iov_len;
884 uio->uio_iovcnt = iovcnt;
885 uio->uio_segflg = UIO_USERSPACE;
886 uio->uio_offset = -1;
888 for (i = 0; i < iovcnt; i++) {
889 if (iov->iov_len > INT_MAX - uio->uio_resid) {
893 uio->uio_resid += iov->iov_len;
901 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
906 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
909 error = kern_readv(td, uap->fd, auio);
915 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
920 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
923 error = kern_writev(td, uap->fd, auio);
929 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
934 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
937 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
943 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
948 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
951 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
957 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
960 struct iovec32 iov32;
966 if (iovcnt > UIO_MAXIOV)
968 iovlen = iovcnt * sizeof(struct iovec);
969 iov = malloc(iovlen, M_IOV, M_WAITOK);
970 for (i = 0; i < iovcnt; i++) {
971 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
976 iov[i].iov_base = PTRIN(iov32.iov_base);
977 iov[i].iov_len = iov32.iov_len;
984 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
989 error = copyin(msg32, &m32, sizeof(m32));
992 msg->msg_name = PTRIN(m32.msg_name);
993 msg->msg_namelen = m32.msg_namelen;
994 msg->msg_iov = PTRIN(m32.msg_iov);
995 msg->msg_iovlen = m32.msg_iovlen;
996 msg->msg_control = PTRIN(m32.msg_control);
997 msg->msg_controllen = m32.msg_controllen;
998 msg->msg_flags = m32.msg_flags;
1003 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1005 struct msghdr32 m32;
1008 m32.msg_name = PTROUT(msg->msg_name);
1009 m32.msg_namelen = msg->msg_namelen;
1010 m32.msg_iov = PTROUT(msg->msg_iov);
1011 m32.msg_iovlen = msg->msg_iovlen;
1012 m32.msg_control = PTROUT(msg->msg_control);
1013 m32.msg_controllen = msg->msg_controllen;
1014 m32.msg_flags = msg->msg_flags;
1015 error = copyout(&m32, msg32, sizeof(m32));
1020 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
1022 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
1024 #define FREEBSD32_ALIGN(p) \
1025 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1026 #define FREEBSD32_CMSG_SPACE(l) \
1027 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1029 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
1030 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1033 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
1037 struct timespec32 ts;
1038 struct timeval32 tv;
1039 struct bintime32 bt;
1050 switch (cm->cmsg_level) {
1052 switch (cm->cmsg_type) {
1054 TV_CP(*in, tmp32, tv);
1055 copylen = sizeof(tmp32.tv);
1059 BT_CP(*in, tmp32, bt);
1060 copylen = sizeof(tmp32.bt);
1065 TS_CP(*in, tmp32, ts);
1066 copylen = sizeof(tmp32.ts);
1080 KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
1082 bcopy(&tmp32, data, copylen);
1087 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1091 socklen_t clen, datalen, datalen_out, oldclen;
1094 int len, maxlen, copylen;
1098 len = msg->msg_controllen;
1099 maxlen = msg->msg_controllen;
1100 msg->msg_controllen = 0;
1102 ctlbuf = msg->msg_control;
1103 for (m = control; m != NULL && len > 0; m = m->m_next) {
1104 cm = mtod(m, struct cmsghdr *);
1106 while (cm != NULL) {
1107 if (sizeof(struct cmsghdr) > clen ||
1108 cm->cmsg_len > clen) {
1113 data = CMSG_DATA(cm);
1114 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1115 datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
1118 * Copy out the message header. Preserve the native
1119 * message size in case we need to inspect the message
1122 copylen = sizeof(struct cmsghdr);
1123 if (len < copylen) {
1124 msg->msg_flags |= MSG_CTRUNC;
1125 m_dispose_extcontrolm(m);
1128 oldclen = cm->cmsg_len;
1129 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1131 error = copyout(cm, ctlbuf, copylen);
1132 cm->cmsg_len = oldclen;
1136 ctlbuf += FREEBSD32_ALIGN(copylen);
1137 len -= FREEBSD32_ALIGN(copylen);
1139 copylen = datalen_out;
1140 if (len < copylen) {
1141 msg->msg_flags |= MSG_CTRUNC;
1142 m_dispose_extcontrolm(m);
1146 /* Copy out the message data. */
1147 error = copyout(data, ctlbuf, copylen);
1151 ctlbuf += FREEBSD32_ALIGN(copylen);
1152 len -= FREEBSD32_ALIGN(copylen);
1154 if (CMSG_SPACE(datalen) < clen) {
1155 clen -= CMSG_SPACE(datalen);
1156 cm = (struct cmsghdr *)
1157 ((caddr_t)cm + CMSG_SPACE(datalen));
1163 msg->msg_controllen +=
1164 FREEBSD32_CMSG_SPACE(datalen_out);
1167 if (len == 0 && m != NULL) {
1168 msg->msg_flags |= MSG_CTRUNC;
1169 m_dispose_extcontrolm(m);
1177 freebsd32_recvmsg(td, uap)
1179 struct freebsd32_recvmsg_args /* {
1181 struct msghdr32 *msg;
1186 struct msghdr32 m32;
1187 struct iovec *uiov, *iov;
1188 struct mbuf *control = NULL;
1189 struct mbuf **controlp;
1192 error = copyin(uap->msg, &m32, sizeof(m32));
1195 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1198 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1202 msg.msg_flags = uap->flags;
1206 controlp = (msg.msg_control != NULL) ? &control : NULL;
1207 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1211 if (control != NULL)
1212 error = freebsd32_copy_msg_out(&msg, control);
1214 msg.msg_controllen = 0;
1217 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1221 if (control != NULL) {
1223 m_dispose_extcontrolm(control);
1231 * Copy-in the array of control messages constructed using alignment
1232 * and padding suitable for a 32-bit environment and construct an
1233 * mbuf using alignment and padding suitable for a 64-bit kernel.
1234 * The alignment and padding are defined indirectly by CMSG_DATA(),
1235 * CMSG_SPACE() and CMSG_LEN().
1238 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1242 u_int idx, len, msglen;
1245 buflen = FREEBSD32_ALIGN(buflen);
1247 if (buflen > MCLBYTES)
1251 * Iterate over the buffer and get the length of each message
1252 * in there. This has 32-bit alignment and padding. Use it to
1253 * determine the length of these messages when using 64-bit
1254 * alignment and padding.
1258 while (idx < buflen) {
1259 error = copyin(buf + idx, &msglen, sizeof(msglen));
1262 if (msglen < sizeof(struct cmsghdr))
1264 msglen = FREEBSD32_ALIGN(msglen);
1265 if (idx + msglen > buflen)
1268 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1269 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1270 len += CMSG_ALIGN(msglen);
1276 m = m_get(M_WAITOK, MT_CONTROL);
1278 MCLGET(m, M_WAITOK);
1281 md = mtod(m, void *);
1282 while (buflen > 0) {
1283 error = copyin(buf, md, sizeof(struct cmsghdr));
1286 msglen = *(u_int *)md;
1287 msglen = FREEBSD32_ALIGN(msglen);
1289 /* Modify the message length to account for alignment. */
1290 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1291 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1293 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1294 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1295 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1297 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1299 error = copyin(buf, md, msglen);
1302 md = (char *)md + CMSG_ALIGN(msglen);
1316 freebsd32_sendmsg(struct thread *td,
1317 struct freebsd32_sendmsg_args *uap)
1320 struct msghdr32 m32;
1322 struct mbuf *control = NULL;
1323 struct sockaddr *to = NULL;
1326 error = copyin(uap->msg, &m32, sizeof(m32));
1329 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1332 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1337 if (msg.msg_name != NULL) {
1338 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1346 if (msg.msg_control) {
1347 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1352 error = freebsd32_copyin_control(&control, msg.msg_control,
1353 msg.msg_controllen);
1357 msg.msg_control = NULL;
1358 msg.msg_controllen = 0;
1361 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1372 freebsd32_recvfrom(struct thread *td,
1373 struct freebsd32_recvfrom_args *uap)
1379 if (uap->fromlenaddr) {
1380 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1381 sizeof(msg.msg_namelen));
1385 msg.msg_namelen = 0;
1388 msg.msg_name = PTRIN(uap->from);
1389 msg.msg_iov = &aiov;
1391 aiov.iov_base = PTRIN(uap->buf);
1392 aiov.iov_len = uap->len;
1393 msg.msg_control = NULL;
1394 msg.msg_flags = uap->flags;
1395 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1396 if (error == 0 && uap->fromlenaddr)
1397 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1398 sizeof (msg.msg_namelen));
1403 freebsd32_settimeofday(struct thread *td,
1404 struct freebsd32_settimeofday_args *uap)
1406 struct timeval32 tv32;
1407 struct timeval tv, *tvp;
1408 struct timezone tz, *tzp;
1412 error = copyin(uap->tv, &tv32, sizeof(tv32));
1415 CP(tv32, tv, tv_sec);
1416 CP(tv32, tv, tv_usec);
1421 error = copyin(uap->tzp, &tz, sizeof(tz));
1427 return (kern_settimeofday(td, tvp, tzp));
1431 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1433 struct timeval32 s32[2];
1434 struct timeval s[2], *sp;
1437 if (uap->tptr != NULL) {
1438 error = copyin(uap->tptr, s32, sizeof(s32));
1441 CP(s32[0], s[0], tv_sec);
1442 CP(s32[0], s[0], tv_usec);
1443 CP(s32[1], s[1], tv_sec);
1444 CP(s32[1], s[1], tv_usec);
1448 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1453 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1455 struct timeval32 s32[2];
1456 struct timeval s[2], *sp;
1459 if (uap->tptr != NULL) {
1460 error = copyin(uap->tptr, s32, sizeof(s32));
1463 CP(s32[0], s[0], tv_sec);
1464 CP(s32[0], s[0], tv_usec);
1465 CP(s32[1], s[1], tv_sec);
1466 CP(s32[1], s[1], tv_usec);
1470 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1474 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1476 struct timeval32 s32[2];
1477 struct timeval s[2], *sp;
1480 if (uap->tptr != NULL) {
1481 error = copyin(uap->tptr, s32, sizeof(s32));
1484 CP(s32[0], s[0], tv_sec);
1485 CP(s32[0], s[0], tv_usec);
1486 CP(s32[1], s[1], tv_sec);
1487 CP(s32[1], s[1], tv_usec);
1491 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1495 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1497 struct timeval32 s32[2];
1498 struct timeval s[2], *sp;
1501 if (uap->times != NULL) {
1502 error = copyin(uap->times, s32, sizeof(s32));
1505 CP(s32[0], s[0], tv_sec);
1506 CP(s32[0], s[0], tv_usec);
1507 CP(s32[1], s[1], tv_sec);
1508 CP(s32[1], s[1], tv_usec);
1512 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1517 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1519 struct timespec32 ts32[2];
1520 struct timespec ts[2], *tsp;
1523 if (uap->times != NULL) {
1524 error = copyin(uap->times, ts32, sizeof(ts32));
1527 CP(ts32[0], ts[0], tv_sec);
1528 CP(ts32[0], ts[0], tv_nsec);
1529 CP(ts32[1], ts[1], tv_sec);
1530 CP(ts32[1], ts[1], tv_nsec);
1534 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1538 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1540 struct timespec32 ts32[2];
1541 struct timespec ts[2], *tsp;
1544 if (uap->times != NULL) {
1545 error = copyin(uap->times, ts32, sizeof(ts32));
1548 CP(ts32[0], ts[0], tv_sec);
1549 CP(ts32[0], ts[0], tv_nsec);
1550 CP(ts32[1], ts[1], tv_sec);
1551 CP(ts32[1], ts[1], tv_nsec);
1555 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1556 tsp, UIO_SYSSPACE, uap->flag));
1560 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1562 struct timeval32 tv32;
1563 struct timeval delta, olddelta, *deltap;
1567 error = copyin(uap->delta, &tv32, sizeof(tv32));
1570 CP(tv32, delta, tv_sec);
1571 CP(tv32, delta, tv_usec);
1575 error = kern_adjtime(td, deltap, &olddelta);
1576 if (uap->olddelta && error == 0) {
1577 CP(olddelta, tv32, tv_sec);
1578 CP(olddelta, tv32, tv_usec);
1579 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1584 #ifdef COMPAT_FREEBSD4
1586 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1588 struct statfs32 s32;
1592 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1593 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1595 copy_statfs(sp, &s32);
1596 error = copyout(&s32, uap->buf, sizeof(s32));
1603 #ifdef COMPAT_FREEBSD4
1605 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1607 struct statfs32 s32;
1611 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1612 error = kern_fstatfs(td, uap->fd, sp);
1614 copy_statfs(sp, &s32);
1615 error = copyout(&s32, uap->buf, sizeof(s32));
1622 #ifdef COMPAT_FREEBSD4
1624 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1626 struct statfs32 s32;
1631 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1633 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1634 error = kern_fhstatfs(td, fh, sp);
1636 copy_statfs(sp, &s32);
1637 error = copyout(&s32, uap->buf, sizeof(s32));
1645 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1648 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1649 PAIR32TO64(off_t, uap->offset)));
1653 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1656 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1657 PAIR32TO64(off_t, uap->offset)));
1662 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1665 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1670 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1675 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1677 /* Expand the quad return into two parts for eax and edx */
1678 pos = td->td_uretoff.tdu_off;
1679 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1680 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1685 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1688 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1689 PAIR32TO64(off_t, uap->length)));
1693 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1696 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1701 ofreebsd32_getdirentries(struct thread *td,
1702 struct ofreebsd32_getdirentries_args *uap)
1704 struct ogetdirentries_args ap;
1711 ap.count = uap->count;
1713 error = kern_ogetdirentries(td, &ap, &loff);
1716 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1722 #if defined(COMPAT_FREEBSD11)
1724 freebsd11_freebsd32_getdirentries(struct thread *td,
1725 struct freebsd11_freebsd32_getdirentries_args *uap)
1731 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1735 if (uap->basep != NULL) {
1737 error = copyout(&base32, uap->basep, sizeof(int32_t));
1743 freebsd11_freebsd32_getdents(struct thread *td,
1744 struct freebsd11_freebsd32_getdents_args *uap)
1746 struct freebsd11_freebsd32_getdirentries_args ap;
1750 ap.count = uap->count;
1752 return (freebsd11_freebsd32_getdirentries(td, &ap));
1754 #endif /* COMPAT_FREEBSD11 */
1756 #ifdef COMPAT_FREEBSD6
1757 /* versions with the 'int pad' argument */
1759 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1762 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1763 PAIR32TO64(off_t, uap->offset)));
1767 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1770 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1771 PAIR32TO64(off_t, uap->offset)));
1775 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1780 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1782 /* Expand the quad return into two parts for eax and edx */
1783 pos = *(off_t *)(td->td_retval);
1784 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1785 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1790 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1793 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1794 PAIR32TO64(off_t, uap->length)));
1798 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1801 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1803 #endif /* COMPAT_FREEBSD6 */
1813 freebsd32_do_sendfile(struct thread *td,
1814 struct freebsd32_sendfile_args *uap, int compat)
1816 struct sf_hdtr32 hdtr32;
1817 struct sf_hdtr hdtr;
1818 struct uio *hdr_uio, *trl_uio;
1820 cap_rights_t rights;
1821 struct iovec32 *iov32;
1822 off_t offset, sbytes;
1825 offset = PAIR32TO64(off_t, uap->offset);
1829 hdr_uio = trl_uio = NULL;
1831 if (uap->hdtr != NULL) {
1832 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1835 PTRIN_CP(hdtr32, hdtr, headers);
1836 CP(hdtr32, hdtr, hdr_cnt);
1837 PTRIN_CP(hdtr32, hdtr, trailers);
1838 CP(hdtr32, hdtr, trl_cnt);
1840 if (hdtr.headers != NULL) {
1841 iov32 = PTRIN(hdtr32.headers);
1842 error = freebsd32_copyinuio(iov32,
1843 hdtr32.hdr_cnt, &hdr_uio);
1846 #ifdef COMPAT_FREEBSD4
1848 * In FreeBSD < 5.0 the nbytes to send also included
1849 * the header. If compat is specified subtract the
1850 * header size from nbytes.
1853 if (uap->nbytes > hdr_uio->uio_resid)
1854 uap->nbytes -= hdr_uio->uio_resid;
1860 if (hdtr.trailers != NULL) {
1861 iov32 = PTRIN(hdtr32.trailers);
1862 error = freebsd32_copyinuio(iov32,
1863 hdtr32.trl_cnt, &trl_uio);
1869 AUDIT_ARG_FD(uap->fd);
1871 if ((error = fget_read(td, uap->fd,
1872 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1875 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1876 uap->nbytes, &sbytes, uap->flags, td);
1879 if (uap->sbytes != NULL)
1880 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1884 free(hdr_uio, M_IOV);
1886 free(trl_uio, M_IOV);
1890 #ifdef COMPAT_FREEBSD4
1892 freebsd4_freebsd32_sendfile(struct thread *td,
1893 struct freebsd4_freebsd32_sendfile_args *uap)
1895 return (freebsd32_do_sendfile(td,
1896 (struct freebsd32_sendfile_args *)uap, 1));
1901 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1904 return (freebsd32_do_sendfile(td, uap, 0));
1908 copy_stat(struct stat *in, struct stat32 *out)
1911 CP(*in, *out, st_dev);
1912 CP(*in, *out, st_ino);
1913 CP(*in, *out, st_mode);
1914 CP(*in, *out, st_nlink);
1915 CP(*in, *out, st_uid);
1916 CP(*in, *out, st_gid);
1917 CP(*in, *out, st_rdev);
1918 TS_CP(*in, *out, st_atim);
1919 TS_CP(*in, *out, st_mtim);
1920 TS_CP(*in, *out, st_ctim);
1921 CP(*in, *out, st_size);
1922 CP(*in, *out, st_blocks);
1923 CP(*in, *out, st_blksize);
1924 CP(*in, *out, st_flags);
1925 CP(*in, *out, st_gen);
1926 TS_CP(*in, *out, st_birthtim);
1927 out->st_padding0 = 0;
1928 out->st_padding1 = 0;
1929 #ifdef __STAT32_TIME_T_EXT
1930 out->st_atim_ext = 0;
1931 out->st_mtim_ext = 0;
1932 out->st_ctim_ext = 0;
1933 out->st_btim_ext = 0;
1935 bzero(out->st_spare, sizeof(out->st_spare));
1940 copy_ostat(struct stat *in, struct ostat32 *out)
1943 bzero(out, sizeof(*out));
1944 CP(*in, *out, st_dev);
1945 CP(*in, *out, st_ino);
1946 CP(*in, *out, st_mode);
1947 CP(*in, *out, st_nlink);
1948 CP(*in, *out, st_uid);
1949 CP(*in, *out, st_gid);
1950 CP(*in, *out, st_rdev);
1951 out->st_size = MIN(in->st_size, INT32_MAX);
1952 TS_CP(*in, *out, st_atim);
1953 TS_CP(*in, *out, st_mtim);
1954 TS_CP(*in, *out, st_ctim);
1955 CP(*in, *out, st_blksize);
1956 CP(*in, *out, st_blocks);
1957 CP(*in, *out, st_flags);
1958 CP(*in, *out, st_gen);
1964 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1967 struct ostat32 sb32;
1970 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1974 copy_ostat(&sb, &sb32);
1975 error = copyout(&sb32, uap->ub, sizeof (sb32));
1981 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1987 error = kern_fstat(td, uap->fd, &ub);
1990 copy_stat(&ub, &ub32);
1991 error = copyout(&ub32, uap->ub, sizeof(ub32));
1997 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2000 struct ostat32 ub32;
2003 error = kern_fstat(td, uap->fd, &ub);
2006 copy_ostat(&ub, &ub32);
2007 error = copyout(&ub32, uap->ub, sizeof(ub32));
2013 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2019 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2023 copy_stat(&ub, &ub32);
2024 error = copyout(&ub32, uap->buf, sizeof(ub32));
2030 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2033 struct ostat32 sb32;
2036 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2037 UIO_USERSPACE, &sb, NULL);
2040 copy_ostat(&sb, &sb32);
2041 error = copyout(&sb32, uap->ub, sizeof (sb32));
2047 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2054 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2057 error = kern_fhstat(td, fh, &sb);
2060 copy_stat(&sb, &sb32);
2061 error = copyout(&sb32, uap->sb, sizeof (sb32));
2065 #if defined(COMPAT_FREEBSD11)
2066 extern int ino64_trunc_error;
2069 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2072 CP(*in, *out, st_ino);
2073 if (in->st_ino != out->st_ino) {
2074 switch (ino64_trunc_error) {
2081 out->st_ino = UINT32_MAX;
2085 CP(*in, *out, st_nlink);
2086 if (in->st_nlink != out->st_nlink) {
2087 switch (ino64_trunc_error) {
2094 out->st_nlink = UINT16_MAX;
2098 out->st_dev = in->st_dev;
2099 if (out->st_dev != in->st_dev) {
2100 switch (ino64_trunc_error) {
2107 CP(*in, *out, st_mode);
2108 CP(*in, *out, st_uid);
2109 CP(*in, *out, st_gid);
2110 out->st_rdev = in->st_rdev;
2111 if (out->st_rdev != in->st_rdev) {
2112 switch (ino64_trunc_error) {
2119 TS_CP(*in, *out, st_atim);
2120 TS_CP(*in, *out, st_mtim);
2121 TS_CP(*in, *out, st_ctim);
2122 CP(*in, *out, st_size);
2123 CP(*in, *out, st_blocks);
2124 CP(*in, *out, st_blksize);
2125 CP(*in, *out, st_flags);
2126 CP(*in, *out, st_gen);
2127 TS_CP(*in, *out, st_birthtim);
2129 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2130 sizeof(*out) - offsetof(struct freebsd11_stat32,
2131 st_birthtim) - sizeof(out->st_birthtim));
2136 freebsd11_freebsd32_stat(struct thread *td,
2137 struct freebsd11_freebsd32_stat_args *uap)
2140 struct freebsd11_stat32 sb32;
2143 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2147 error = freebsd11_cvtstat32(&sb, &sb32);
2149 error = copyout(&sb32, uap->ub, sizeof (sb32));
2154 freebsd11_freebsd32_fstat(struct thread *td,
2155 struct freebsd11_freebsd32_fstat_args *uap)
2158 struct freebsd11_stat32 sb32;
2161 error = kern_fstat(td, uap->fd, &sb);
2164 error = freebsd11_cvtstat32(&sb, &sb32);
2166 error = copyout(&sb32, uap->ub, sizeof (sb32));
2171 freebsd11_freebsd32_fstatat(struct thread *td,
2172 struct freebsd11_freebsd32_fstatat_args *uap)
2175 struct freebsd11_stat32 sb32;
2178 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2182 error = freebsd11_cvtstat32(&sb, &sb32);
2184 error = copyout(&sb32, uap->buf, sizeof (sb32));
2189 freebsd11_freebsd32_lstat(struct thread *td,
2190 struct freebsd11_freebsd32_lstat_args *uap)
2193 struct freebsd11_stat32 sb32;
2196 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2197 UIO_USERSPACE, &sb, NULL);
2200 error = freebsd11_cvtstat32(&sb, &sb32);
2202 error = copyout(&sb32, uap->ub, sizeof (sb32));
2207 freebsd11_freebsd32_fhstat(struct thread *td,
2208 struct freebsd11_freebsd32_fhstat_args *uap)
2211 struct freebsd11_stat32 sb32;
2215 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2218 error = kern_fhstat(td, fh, &sb);
2221 error = freebsd11_cvtstat32(&sb, &sb32);
2223 error = copyout(&sb32, uap->sb, sizeof (sb32));
2229 freebsd32___sysctl(struct thread *td, struct freebsd32___sysctl_args *uap)
2231 int error, name[CTL_MAXNAME];
2235 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2237 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2241 error = fueword32(uap->oldlenp, &tmp);
2248 error = userland_sysctl(td, name, uap->namelen,
2249 uap->old, &oldlen, 1,
2250 uap->new, uap->newlen, &j, SCTL_MASK32);
2254 suword32(uap->oldlenp, j);
2259 freebsd32___sysctlbyname(struct thread *td,
2260 struct freebsd32___sysctlbyname_args *uap)
2266 if (uap->oldlenp != NULL) {
2267 error = fueword32(uap->oldlenp, &tmp);
2274 error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
2275 &oldlen, uap->new, uap->newlen, &rv, SCTL_MASK32, 1);
2278 if (uap->oldlenp != NULL)
2279 error = suword32(uap->oldlenp, rv);
2285 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2291 error = copyin(uap->jail, &version, sizeof(uint32_t));
2298 /* FreeBSD single IPv4 jails. */
2299 struct jail32_v0 j32_v0;
2301 bzero(&j, sizeof(struct jail));
2302 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2305 CP(j32_v0, j, version);
2306 PTRIN_CP(j32_v0, j, path);
2307 PTRIN_CP(j32_v0, j, hostname);
2308 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2314 * Version 1 was used by multi-IPv4 jail implementations
2315 * that never made it into the official kernel.
2319 case 2: /* JAIL_API_VERSION */
2321 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2324 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2327 CP(j32, j, version);
2328 PTRIN_CP(j32, j, path);
2329 PTRIN_CP(j32, j, hostname);
2330 PTRIN_CP(j32, j, jailname);
2333 PTRIN_CP(j32, j, ip4);
2334 PTRIN_CP(j32, j, ip6);
2339 /* Sci-Fi jails are not supported, sorry. */
2342 return (kern_jail(td, &j));
2346 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2351 /* Check that we have an even number of iovecs. */
2352 if (uap->iovcnt & 1)
2355 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2358 error = kern_jail_set(td, auio, uap->flags);
2364 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2366 struct iovec32 iov32;
2370 /* Check that we have an even number of iovecs. */
2371 if (uap->iovcnt & 1)
2374 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2377 error = kern_jail_get(td, auio, uap->flags);
2379 for (i = 0; i < uap->iovcnt; i++) {
2380 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2381 CP(auio->uio_iov[i], iov32, iov_len);
2382 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2391 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2393 struct sigaction32 s32;
2394 struct sigaction sa, osa, *sap;
2398 error = copyin(uap->act, &s32, sizeof(s32));
2401 sa.sa_handler = PTRIN(s32.sa_u);
2402 CP(s32, sa, sa_flags);
2403 CP(s32, sa, sa_mask);
2407 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2408 if (error == 0 && uap->oact != NULL) {
2409 s32.sa_u = PTROUT(osa.sa_handler);
2410 CP(osa, s32, sa_flags);
2411 CP(osa, s32, sa_mask);
2412 error = copyout(&s32, uap->oact, sizeof(s32));
2417 #ifdef COMPAT_FREEBSD4
2419 freebsd4_freebsd32_sigaction(struct thread *td,
2420 struct freebsd4_freebsd32_sigaction_args *uap)
2422 struct sigaction32 s32;
2423 struct sigaction sa, osa, *sap;
2427 error = copyin(uap->act, &s32, sizeof(s32));
2430 sa.sa_handler = PTRIN(s32.sa_u);
2431 CP(s32, sa, sa_flags);
2432 CP(s32, sa, sa_mask);
2436 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2437 if (error == 0 && uap->oact != NULL) {
2438 s32.sa_u = PTROUT(osa.sa_handler);
2439 CP(osa, s32, sa_flags);
2440 CP(osa, s32, sa_mask);
2441 error = copyout(&s32, uap->oact, sizeof(s32));
2448 struct osigaction32 {
2457 ofreebsd32_sigaction(struct thread *td,
2458 struct ofreebsd32_sigaction_args *uap)
2460 struct osigaction32 s32;
2461 struct sigaction sa, osa, *sap;
2464 if (uap->signum <= 0 || uap->signum >= ONSIG)
2468 error = copyin(uap->nsa, &s32, sizeof(s32));
2471 sa.sa_handler = PTRIN(s32.sa_u);
2472 CP(s32, sa, sa_flags);
2473 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2477 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2478 if (error == 0 && uap->osa != NULL) {
2479 s32.sa_u = PTROUT(osa.sa_handler);
2480 CP(osa, s32, sa_flags);
2481 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2482 error = copyout(&s32, uap->osa, sizeof(s32));
2488 ofreebsd32_sigprocmask(struct thread *td,
2489 struct ofreebsd32_sigprocmask_args *uap)
2494 OSIG2SIG(uap->mask, set);
2495 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2496 SIG2OSIG(oset, td->td_retval[0]);
2501 ofreebsd32_sigpending(struct thread *td,
2502 struct ofreebsd32_sigpending_args *uap)
2504 struct proc *p = td->td_proc;
2508 siglist = p->p_siglist;
2509 SIGSETOR(siglist, td->td_siglist);
2511 SIG2OSIG(siglist, td->td_retval[0]);
2516 u_int32_t sv_handler;
2522 ofreebsd32_sigvec(struct thread *td,
2523 struct ofreebsd32_sigvec_args *uap)
2525 struct sigvec32 vec;
2526 struct sigaction sa, osa, *sap;
2529 if (uap->signum <= 0 || uap->signum >= ONSIG)
2533 error = copyin(uap->nsv, &vec, sizeof(vec));
2536 sa.sa_handler = PTRIN(vec.sv_handler);
2537 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2538 sa.sa_flags = vec.sv_flags;
2539 sa.sa_flags ^= SA_RESTART;
2543 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2544 if (error == 0 && uap->osv != NULL) {
2545 vec.sv_handler = PTROUT(osa.sa_handler);
2546 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2547 vec.sv_flags = osa.sa_flags;
2548 vec.sv_flags &= ~SA_NOCLDWAIT;
2549 vec.sv_flags ^= SA_RESTART;
2550 error = copyout(&vec, uap->osv, sizeof(vec));
2556 ofreebsd32_sigblock(struct thread *td,
2557 struct ofreebsd32_sigblock_args *uap)
2561 OSIG2SIG(uap->mask, set);
2562 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2563 SIG2OSIG(oset, td->td_retval[0]);
2568 ofreebsd32_sigsetmask(struct thread *td,
2569 struct ofreebsd32_sigsetmask_args *uap)
2573 OSIG2SIG(uap->mask, set);
2574 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2575 SIG2OSIG(oset, td->td_retval[0]);
2580 ofreebsd32_sigsuspend(struct thread *td,
2581 struct ofreebsd32_sigsuspend_args *uap)
2585 OSIG2SIG(uap->mask, mask);
2586 return (kern_sigsuspend(td, mask));
2595 ofreebsd32_sigstack(struct thread *td,
2596 struct ofreebsd32_sigstack_args *uap)
2598 struct sigstack32 s32;
2599 struct sigstack nss, oss;
2600 int error = 0, unss;
2602 if (uap->nss != NULL) {
2603 error = copyin(uap->nss, &s32, sizeof(s32));
2606 nss.ss_sp = PTRIN(s32.ss_sp);
2607 CP(s32, nss, ss_onstack);
2612 oss.ss_sp = td->td_sigstk.ss_sp;
2613 oss.ss_onstack = sigonstack(cpu_getstack(td));
2615 td->td_sigstk.ss_sp = nss.ss_sp;
2616 td->td_sigstk.ss_size = 0;
2617 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2618 td->td_pflags |= TDP_ALTSTACK;
2620 if (uap->oss != NULL) {
2621 s32.ss_sp = PTROUT(oss.ss_sp);
2622 CP(oss, s32, ss_onstack);
2623 error = copyout(&s32, uap->oss, sizeof(s32));
2630 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2633 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2634 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2638 freebsd32_clock_nanosleep(struct thread *td,
2639 struct freebsd32_clock_nanosleep_args *uap)
2643 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2644 uap->rqtp, uap->rmtp);
2645 return (kern_posix_error(td, error));
2649 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2650 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2652 struct timespec32 rmt32, rqt32;
2653 struct timespec rmt, rqt;
2656 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2660 CP(rqt32, rqt, tv_sec);
2661 CP(rqt32, rqt, tv_nsec);
2663 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2664 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2666 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2667 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2670 CP(rmt, rmt32, tv_sec);
2671 CP(rmt, rmt32, tv_nsec);
2673 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2681 freebsd32_clock_gettime(struct thread *td,
2682 struct freebsd32_clock_gettime_args *uap)
2684 struct timespec ats;
2685 struct timespec32 ats32;
2688 error = kern_clock_gettime(td, uap->clock_id, &ats);
2690 CP(ats, ats32, tv_sec);
2691 CP(ats, ats32, tv_nsec);
2692 error = copyout(&ats32, uap->tp, sizeof(ats32));
2698 freebsd32_clock_settime(struct thread *td,
2699 struct freebsd32_clock_settime_args *uap)
2701 struct timespec ats;
2702 struct timespec32 ats32;
2705 error = copyin(uap->tp, &ats32, sizeof(ats32));
2708 CP(ats32, ats, tv_sec);
2709 CP(ats32, ats, tv_nsec);
2711 return (kern_clock_settime(td, uap->clock_id, &ats));
2715 freebsd32_clock_getres(struct thread *td,
2716 struct freebsd32_clock_getres_args *uap)
2719 struct timespec32 ts32;
2722 if (uap->tp == NULL)
2724 error = kern_clock_getres(td, uap->clock_id, &ts);
2726 CP(ts, ts32, tv_sec);
2727 CP(ts, ts32, tv_nsec);
2728 error = copyout(&ts32, uap->tp, sizeof(ts32));
2733 int freebsd32_ktimer_create(struct thread *td,
2734 struct freebsd32_ktimer_create_args *uap)
2736 struct sigevent32 ev32;
2737 struct sigevent ev, *evp;
2740 if (uap->evp == NULL) {
2744 error = copyin(uap->evp, &ev32, sizeof(ev32));
2747 error = convert_sigevent32(&ev32, &ev);
2751 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2753 error = copyout(&id, uap->timerid, sizeof(int));
2755 kern_ktimer_delete(td, id);
2761 freebsd32_ktimer_settime(struct thread *td,
2762 struct freebsd32_ktimer_settime_args *uap)
2764 struct itimerspec32 val32, oval32;
2765 struct itimerspec val, oval, *ovalp;
2768 error = copyin(uap->value, &val32, sizeof(val32));
2772 ovalp = uap->ovalue != NULL ? &oval : NULL;
2773 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2774 if (error == 0 && uap->ovalue != NULL) {
2775 ITS_CP(oval, oval32);
2776 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2782 freebsd32_ktimer_gettime(struct thread *td,
2783 struct freebsd32_ktimer_gettime_args *uap)
2785 struct itimerspec32 val32;
2786 struct itimerspec val;
2789 error = kern_ktimer_gettime(td, uap->timerid, &val);
2792 error = copyout(&val32, uap->value, sizeof(val32));
2798 freebsd32_clock_getcpuclockid2(struct thread *td,
2799 struct freebsd32_clock_getcpuclockid2_args *uap)
2804 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2805 uap->which, &clk_id);
2807 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2812 freebsd32_thr_new(struct thread *td,
2813 struct freebsd32_thr_new_args *uap)
2815 struct thr_param32 param32;
2816 struct thr_param param;
2819 if (uap->param_size < 0 ||
2820 uap->param_size > sizeof(struct thr_param32))
2822 bzero(¶m, sizeof(struct thr_param));
2823 bzero(¶m32, sizeof(struct thr_param32));
2824 error = copyin(uap->param, ¶m32, uap->param_size);
2827 param.start_func = PTRIN(param32.start_func);
2828 param.arg = PTRIN(param32.arg);
2829 param.stack_base = PTRIN(param32.stack_base);
2830 param.stack_size = param32.stack_size;
2831 param.tls_base = PTRIN(param32.tls_base);
2832 param.tls_size = param32.tls_size;
2833 param.child_tid = PTRIN(param32.child_tid);
2834 param.parent_tid = PTRIN(param32.parent_tid);
2835 param.flags = param32.flags;
2836 param.rtp = PTRIN(param32.rtp);
2837 param.spare[0] = PTRIN(param32.spare[0]);
2838 param.spare[1] = PTRIN(param32.spare[1]);
2839 param.spare[2] = PTRIN(param32.spare[2]);
2841 return (kern_thr_new(td, ¶m));
2845 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2847 struct timespec32 ts32;
2848 struct timespec ts, *tsp;
2853 if (uap->timeout != NULL) {
2854 error = copyin((const void *)uap->timeout, (void *)&ts32,
2855 sizeof(struct timespec32));
2858 ts.tv_sec = ts32.tv_sec;
2859 ts.tv_nsec = ts32.tv_nsec;
2862 return (kern_thr_suspend(td, tsp));
2866 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2868 bzero(dst, sizeof(*dst));
2869 dst->si_signo = src->si_signo;
2870 dst->si_errno = src->si_errno;
2871 dst->si_code = src->si_code;
2872 dst->si_pid = src->si_pid;
2873 dst->si_uid = src->si_uid;
2874 dst->si_status = src->si_status;
2875 dst->si_addr = (uintptr_t)src->si_addr;
2876 dst->si_value.sival_int = src->si_value.sival_int;
2877 dst->si_timerid = src->si_timerid;
2878 dst->si_overrun = src->si_overrun;
2881 #ifndef _FREEBSD32_SYSPROTO_H_
2882 struct freebsd32_sigqueue_args {
2885 /* union sigval32 */ int value;
2889 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2894 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2895 * On 64-bit little-endian ABIs, the low bits are the same.
2896 * In 64-bit big-endian ABIs, sival_int overlaps with
2897 * sival_ptr's HIGH bits. We choose to support sival_int
2898 * rather than sival_ptr in this case as it seems to be
2901 bzero(&sv, sizeof(sv));
2902 sv.sival_int = uap->value;
2904 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2908 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2910 struct timespec32 ts32;
2912 struct timespec *timeout;
2915 struct siginfo32 si32;
2919 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2922 ts.tv_sec = ts32.tv_sec;
2923 ts.tv_nsec = ts32.tv_nsec;
2928 error = copyin(uap->set, &set, sizeof(set));
2932 error = kern_sigtimedwait(td, set, &ksi, timeout);
2937 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2938 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2942 td->td_retval[0] = ksi.ksi_signo;
2950 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2953 struct siginfo32 si32;
2957 error = copyin(uap->set, &set, sizeof(set));
2961 error = kern_sigtimedwait(td, set, &ksi, NULL);
2966 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2967 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2970 td->td_retval[0] = ksi.ksi_signo;
2975 freebsd32_cpuset_setid(struct thread *td,
2976 struct freebsd32_cpuset_setid_args *uap)
2979 return (kern_cpuset_setid(td, uap->which,
2980 PAIR32TO64(id_t, uap->id), uap->setid));
2984 freebsd32_cpuset_getid(struct thread *td,
2985 struct freebsd32_cpuset_getid_args *uap)
2988 return (kern_cpuset_getid(td, uap->level, uap->which,
2989 PAIR32TO64(id_t, uap->id), uap->setid));
2993 freebsd32_cpuset_getaffinity(struct thread *td,
2994 struct freebsd32_cpuset_getaffinity_args *uap)
2997 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2998 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3002 freebsd32_cpuset_setaffinity(struct thread *td,
3003 struct freebsd32_cpuset_setaffinity_args *uap)
3006 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
3007 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3011 freebsd32_cpuset_getdomain(struct thread *td,
3012 struct freebsd32_cpuset_getdomain_args *uap)
3015 return (kern_cpuset_getdomain(td, uap->level, uap->which,
3016 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3020 freebsd32_cpuset_setdomain(struct thread *td,
3021 struct freebsd32_cpuset_setdomain_args *uap)
3024 return (kern_cpuset_setdomain(td, uap->level, uap->which,
3025 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3029 freebsd32_nmount(struct thread *td,
3030 struct freebsd32_nmount_args /* {
3032 unsigned int iovcnt;
3041 * Mount flags are now 64-bits. On 32-bit archtectures only
3042 * 32-bits are passed in, but from here on everything handles
3043 * 64-bit flags correctly.
3047 AUDIT_ARG_FFLAGS(flags);
3050 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
3051 * userspace to set this flag, but we must filter it out if we want
3052 * MNT_UPDATE on the root file system to work.
3053 * MNT_ROOTFS should only be set by the kernel when mounting its
3056 flags &= ~MNT_ROOTFS;
3059 * check that we have an even number of iovec's
3060 * and that we have at least two options.
3062 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3065 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3068 error = vfs_donmount(td, flags, auio);
3076 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3078 struct yyy32 *p32, s32;
3079 struct yyy *p = NULL, s;
3084 error = copyin(uap->zzz, &s32, sizeof(s32));
3090 error = kern_xxx(td, p);
3095 error = copyout(&s32, p32, sizeof(s32));
3102 syscall32_module_handler(struct module *mod, int what, void *arg)
3105 return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3109 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3112 return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3116 syscall32_helper_unregister(struct syscall_helper_data *sd)
3119 return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3123 freebsd32_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
3128 uintptr_t destp, ustringp;
3129 struct freebsd32_ps_strings *arginfo;
3130 char canary[sizeof(long) * 8];
3131 int32_t pagesizes32[MAXPAGESIZES];
3132 size_t execpath_len;
3133 int error, szsigcode;
3136 * Calculate string base and vector table pointers.
3137 * Also deal with signal trampoline code for this exec type.
3139 if (imgp->execpath != NULL && imgp->auxargs != NULL)
3140 execpath_len = strlen(imgp->execpath) + 1;
3143 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3145 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3146 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3149 destp = (uintptr_t)arginfo;
3154 if (szsigcode != 0) {
3156 destp = rounddown2(destp, sizeof(uint32_t));
3157 error = copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3164 * Copy the image path for the rtld.
3166 if (execpath_len != 0) {
3167 destp -= execpath_len;
3168 imgp->execpathp = destp;
3169 error = copyout(imgp->execpath, (void *)destp, execpath_len);
3175 * Prepare the canary for SSP.
3177 arc4rand(canary, sizeof(canary), 0);
3178 destp -= sizeof(canary);
3179 imgp->canary = destp;
3180 error = copyout(canary, (void *)destp, sizeof(canary));
3183 imgp->canarylen = sizeof(canary);
3186 * Prepare the pagesizes array.
3188 for (i = 0; i < MAXPAGESIZES; i++)
3189 pagesizes32[i] = (uint32_t)pagesizes[i];
3190 destp -= sizeof(pagesizes32);
3191 destp = rounddown2(destp, sizeof(uint32_t));
3192 imgp->pagesizes = destp;
3193 error = copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3196 imgp->pagesizeslen = sizeof(pagesizes32);
3199 * Allocate room for the argument and environment strings.
3201 destp -= ARG_MAX - imgp->args->stringspace;
3202 destp = rounddown2(destp, sizeof(uint32_t));
3205 if (imgp->sysent->sv_stackgap != NULL)
3206 imgp->sysent->sv_stackgap(imgp, &destp);
3208 if (imgp->auxargs) {
3210 * Allocate room on the stack for the ELF auxargs
3211 * array. It has up to AT_COUNT entries.
3213 destp -= AT_COUNT * sizeof(Elf32_Auxinfo);
3214 destp = rounddown2(destp, sizeof(uint32_t));
3217 vectp = (uint32_t *)destp;
3220 * Allocate room for the argv[] and env vectors including the
3221 * terminating NULL pointers.
3223 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3226 * vectp also becomes our initial stack base
3228 *stack_base = (uintptr_t)vectp;
3230 stringp = imgp->args->begin_argv;
3231 argc = imgp->args->argc;
3232 envc = imgp->args->envc;
3234 * Copy out strings - arguments and environment.
3236 error = copyout(stringp, (void *)ustringp,
3237 ARG_MAX - imgp->args->stringspace);
3242 * Fill in "ps_strings" struct for ps, w, etc.
3244 if (suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp) != 0 ||
3245 suword32(&arginfo->ps_nargvstr, argc) != 0)
3249 * Fill in argument portion of vector table.
3251 for (; argc > 0; --argc) {
3252 if (suword32(vectp++, ustringp) != 0)
3254 while (*stringp++ != 0)
3259 /* a null vector table pointer separates the argp's from the envp's */
3260 if (suword32(vectp++, 0) != 0)
3263 if (suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp) != 0 ||
3264 suword32(&arginfo->ps_nenvstr, envc) != 0)
3268 * Fill in environment portion of vector table.
3270 for (; envc > 0; --envc) {
3271 if (suword32(vectp++, ustringp) != 0)
3273 while (*stringp++ != 0)
3278 /* end of vector table is a null pointer */
3279 if (suword32(vectp, 0) != 0)
3282 if (imgp->auxargs) {
3284 error = imgp->sysent->sv_copyout_auxargs(imgp,
3294 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3296 struct kld_file_stat *stat;
3297 struct kld32_file_stat *stat32;
3300 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3303 if (version != sizeof(struct kld32_file_stat_1) &&
3304 version != sizeof(struct kld32_file_stat))
3307 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3308 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3309 error = kern_kldstat(td, uap->fileid, stat);
3311 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3312 CP(*stat, *stat32, refs);
3313 CP(*stat, *stat32, id);
3314 PTROUT_CP(*stat, *stat32, address);
3315 CP(*stat, *stat32, size);
3316 bcopy(&stat->pathname[0], &stat32->pathname[0],
3317 sizeof(stat->pathname));
3318 stat32->version = version;
3319 error = copyout(stat32, uap->stat, version);
3322 free(stat32, M_TEMP);
3327 freebsd32_posix_fallocate(struct thread *td,
3328 struct freebsd32_posix_fallocate_args *uap)
3332 error = kern_posix_fallocate(td, uap->fd,
3333 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3334 return (kern_posix_error(td, error));
3338 freebsd32_posix_fadvise(struct thread *td,
3339 struct freebsd32_posix_fadvise_args *uap)
3343 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3344 PAIR32TO64(off_t, uap->len), uap->advice);
3345 return (kern_posix_error(td, error));
3349 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3352 CP(*sig32, *sig, sigev_notify);
3353 switch (sig->sigev_notify) {
3356 case SIGEV_THREAD_ID:
3357 CP(*sig32, *sig, sigev_notify_thread_id);
3360 CP(*sig32, *sig, sigev_signo);
3361 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3364 CP(*sig32, *sig, sigev_notify_kqueue);
3365 CP(*sig32, *sig, sigev_notify_kevent_flags);
3366 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3375 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3379 struct procctl_reaper_status rs;
3380 struct procctl_reaper_pids rp;
3381 struct procctl_reaper_kill rk;
3384 struct procctl_reaper_pids32 rp;
3386 int error, error1, flags, signum;
3388 if (uap->com >= PROC_PROCCTL_MD_MIN)
3389 return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3390 uap->com, PTRIN(uap->data)));
3394 case PROC_PROTMAX_CTL:
3396 case PROC_STACKGAP_CTL:
3397 case PROC_TRACE_CTL:
3398 case PROC_TRAPCAP_CTL:
3399 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3404 case PROC_REAP_ACQUIRE:
3405 case PROC_REAP_RELEASE:
3406 if (uap->data != NULL)
3410 case PROC_REAP_STATUS:
3413 case PROC_REAP_GETPIDS:
3414 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3417 CP(x32.rp, x.rp, rp_count);
3418 PTRIN_CP(x32.rp, x.rp, rp_pids);
3421 case PROC_REAP_KILL:
3422 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3427 case PROC_ASLR_STATUS:
3428 case PROC_PROTMAX_STATUS:
3429 case PROC_STACKGAP_STATUS:
3430 case PROC_TRACE_STATUS:
3431 case PROC_TRAPCAP_STATUS:
3434 case PROC_PDEATHSIG_CTL:
3435 error = copyin(uap->data, &signum, sizeof(signum));
3440 case PROC_PDEATHSIG_STATUS:
3446 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3449 case PROC_REAP_STATUS:
3451 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3453 case PROC_REAP_KILL:
3454 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3458 case PROC_ASLR_STATUS:
3459 case PROC_PROTMAX_STATUS:
3460 case PROC_STACKGAP_STATUS:
3461 case PROC_TRACE_STATUS:
3462 case PROC_TRAPCAP_STATUS:
3464 error = copyout(&flags, uap->data, sizeof(flags));
3466 case PROC_PDEATHSIG_STATUS:
3468 error = copyout(&signum, uap->data, sizeof(signum));
3475 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3481 * Do unsigned conversion for arg when operation
3482 * interprets it as flags or pointer.
3484 case F_SETLK_REMOTE:
3493 tmp = (unsigned int)(uap->arg);
3499 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3503 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3505 struct timespec32 ts32;
3506 struct timespec ts, *tsp;
3510 if (uap->ts != NULL) {
3511 error = copyin(uap->ts, &ts32, sizeof(ts32));
3514 CP(ts32, ts, tv_sec);
3515 CP(ts32, ts, tv_nsec);
3519 if (uap->set != NULL) {
3520 error = copyin(uap->set, &set, sizeof(set));
3527 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3531 freebsd32_sched_rr_get_interval(struct thread *td,
3532 struct freebsd32_sched_rr_get_interval_args *uap)
3535 struct timespec32 ts32;
3538 error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3540 CP(ts, ts32, tv_sec);
3541 CP(ts, ts32, tv_nsec);
3542 error = copyout(&ts32, uap->interval, sizeof(ts32));