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"
33 #include "opt_ktrace.h"
35 #define __ELF_WORD_SIZE 32
37 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/clock.h>
42 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/imgact.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/linker.h>
50 #include <sys/malloc.h>
51 #include <sys/file.h> /* Must come after sys/malloc.h */
52 #include <sys/imgact.h>
55 #include <sys/module.h>
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
60 #include <sys/procctl.h>
61 #include <sys/reboot.h>
62 #include <sys/resource.h>
63 #include <sys/resourcevar.h>
64 #include <sys/selinfo.h>
65 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
66 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
67 #include <sys/signal.h>
68 #include <sys/signalvar.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
72 #include <sys/syscall.h>
73 #include <sys/syscallsubr.h>
74 #include <sys/sysctl.h>
75 #include <sys/sysent.h>
76 #include <sys/sysproto.h>
77 #include <sys/systm.h>
79 #include <sys/unistd.h>
80 #include <sys/ucontext.h>
81 #include <sys/vnode.h>
88 #include <sys/ktrace.h>
92 #include <netinet/in.h>
96 #include <vm/vm_param.h>
98 #include <vm/vm_map.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_extern.h>
102 #include <machine/cpu.h>
103 #include <machine/elf.h>
105 #include <security/audit/audit.h>
107 #include <compat/freebsd32/freebsd32_util.h>
108 #include <compat/freebsd32/freebsd32.h>
109 #include <compat/freebsd32/freebsd32_ipc.h>
110 #include <compat/freebsd32/freebsd32_misc.h>
111 #include <compat/freebsd32/freebsd32_signal.h>
112 #include <compat/freebsd32/freebsd32_proto.h>
114 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
117 CTASSERT(sizeof(struct timeval32) == 8);
118 CTASSERT(sizeof(struct timespec32) == 8);
119 CTASSERT(sizeof(struct itimerval32) == 16);
120 CTASSERT(sizeof(struct bintime32) == 12);
122 CTASSERT(sizeof(struct statfs32) == 256);
124 CTASSERT(sizeof(struct rusage32) == 72);
126 CTASSERT(sizeof(struct sigaltstack32) == 12);
127 CTASSERT(sizeof(struct kevent32) == 20);
128 CTASSERT(sizeof(struct iovec32) == 8);
129 CTASSERT(sizeof(struct msghdr32) == 28);
131 CTASSERT(sizeof(struct stat32) == 96);
133 CTASSERT(sizeof(struct sigaction32) == 24);
135 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
136 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
137 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
138 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
141 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
144 TV_CP(*s, *s32, ru_utime);
145 TV_CP(*s, *s32, ru_stime);
146 CP(*s, *s32, ru_maxrss);
147 CP(*s, *s32, ru_ixrss);
148 CP(*s, *s32, ru_idrss);
149 CP(*s, *s32, ru_isrss);
150 CP(*s, *s32, ru_minflt);
151 CP(*s, *s32, ru_majflt);
152 CP(*s, *s32, ru_nswap);
153 CP(*s, *s32, ru_inblock);
154 CP(*s, *s32, ru_oublock);
155 CP(*s, *s32, ru_msgsnd);
156 CP(*s, *s32, ru_msgrcv);
157 CP(*s, *s32, ru_nsignals);
158 CP(*s, *s32, ru_nvcsw);
159 CP(*s, *s32, ru_nivcsw);
163 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
166 struct rusage32 ru32;
167 struct rusage ru, *rup;
169 if (uap->rusage != NULL)
173 error = kern_wait(td, uap->pid, &status, uap->options, rup);
176 if (uap->status != NULL)
177 error = copyout(&status, uap->status, sizeof(status));
178 if (uap->rusage != NULL && error == 0) {
179 freebsd32_rusage_out(&ru, &ru32);
180 error = copyout(&ru32, uap->rusage, sizeof(ru32));
186 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
188 struct wrusage32 wru32;
189 struct __wrusage wru, *wrup;
190 struct siginfo32 si32;
191 struct __siginfo si, *sip;
194 if (uap->wrusage != NULL)
198 if (uap->info != NULL) {
200 bzero(sip, sizeof(*sip));
203 error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
204 &status, uap->options, wrup, sip);
207 if (uap->status != NULL)
208 error = copyout(&status, uap->status, sizeof(status));
209 if (uap->wrusage != NULL && error == 0) {
210 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
211 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
212 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
214 if (uap->info != NULL && error == 0) {
215 siginfo_to_siginfo32 (&si, &si32);
216 error = copyout(&si32, uap->info, sizeof(si32));
221 #ifdef COMPAT_FREEBSD4
223 copy_statfs(struct statfs *in, struct statfs32 *out)
226 statfs_scale_blocks(in, INT32_MAX);
227 bzero(out, sizeof(*out));
228 CP(*in, *out, f_bsize);
229 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
230 CP(*in, *out, f_blocks);
231 CP(*in, *out, f_bfree);
232 CP(*in, *out, f_bavail);
233 out->f_files = MIN(in->f_files, INT32_MAX);
234 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
235 CP(*in, *out, f_fsid);
236 CP(*in, *out, f_owner);
237 CP(*in, *out, f_type);
238 CP(*in, *out, f_flags);
239 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
240 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
241 strlcpy(out->f_fstypename,
242 in->f_fstypename, MFSNAMELEN);
243 strlcpy(out->f_mntonname,
244 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
245 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
246 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
247 strlcpy(out->f_mntfromname,
248 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
252 #ifdef COMPAT_FREEBSD4
254 freebsd4_freebsd32_getfsstat(struct thread *td,
255 struct freebsd4_freebsd32_getfsstat_args *uap)
257 struct statfs *buf, *sp;
258 struct statfs32 stat32;
259 size_t count, size, copycount;
262 count = uap->bufsize / sizeof(struct statfs32);
263 size = count * sizeof(struct statfs);
264 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
268 while (copycount > 0 && error == 0) {
269 copy_statfs(sp, &stat32);
270 error = copyout(&stat32, uap->buf, sizeof(stat32));
278 td->td_retval[0] = count;
283 #ifdef COMPAT_FREEBSD10
285 freebsd10_freebsd32_pipe(struct thread *td,
286 struct freebsd10_freebsd32_pipe_args *uap) {
288 return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
293 freebsd32_sigaltstack(struct thread *td,
294 struct freebsd32_sigaltstack_args *uap)
296 struct sigaltstack32 s32;
297 struct sigaltstack ss, oss, *ssp;
300 if (uap->ss != NULL) {
301 error = copyin(uap->ss, &s32, sizeof(s32));
304 PTRIN_CP(s32, ss, ss_sp);
305 CP(s32, ss, ss_size);
306 CP(s32, ss, ss_flags);
310 error = kern_sigaltstack(td, ssp, &oss);
311 if (error == 0 && uap->oss != NULL) {
312 PTROUT_CP(oss, s32, ss_sp);
313 CP(oss, s32, ss_size);
314 CP(oss, s32, ss_flags);
315 error = copyout(&s32, uap->oss, sizeof(s32));
321 * Custom version of exec_copyin_args() so that we can translate
325 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
326 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
333 bzero(args, sizeof(*args));
338 * Allocate demand-paged memory for the file name, argument, and
339 * environment strings.
341 error = exec_alloc_args(args);
346 * Copy the file name.
349 args->fname = args->buf;
350 error = (segflg == UIO_SYSSPACE) ?
351 copystr(fname, args->fname, PATH_MAX, &length) :
352 copyinstr(fname, args->fname, PATH_MAX, &length);
358 args->begin_argv = args->buf + length;
359 args->endp = args->begin_argv;
360 args->stringspace = ARG_MAX;
363 * extract arguments first
367 error = copyin(p32++, &arg, sizeof(arg));
373 error = copyinstr(argp, args->endp, args->stringspace, &length);
375 if (error == ENAMETOOLONG)
379 args->stringspace -= length;
380 args->endp += length;
384 args->begin_envv = args->endp;
387 * extract environment strings
392 error = copyin(p32++, &arg, sizeof(arg));
398 error = copyinstr(envp, args->endp, args->stringspace,
401 if (error == ENAMETOOLONG)
405 args->stringspace -= length;
406 args->endp += length;
414 exec_free_args(args);
419 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
421 struct image_args eargs;
422 struct vmspace *oldvmspace;
425 error = pre_execve(td, &oldvmspace);
428 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
429 uap->argv, uap->envv);
431 error = kern_execve(td, &eargs, NULL);
432 post_execve(td, error, oldvmspace);
437 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
439 struct image_args eargs;
440 struct vmspace *oldvmspace;
443 error = pre_execve(td, &oldvmspace);
446 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
447 uap->argv, uap->envv);
450 error = kern_execve(td, &eargs, NULL);
452 post_execve(td, error, oldvmspace);
457 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
462 #if defined(__amd64__)
463 if (i386_read_exec && (prot & PROT_READ) != 0)
466 return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
471 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
476 #if defined(__amd64__)
477 if (i386_read_exec && (prot & PROT_READ))
481 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
482 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
485 #ifdef COMPAT_FREEBSD6
487 freebsd6_freebsd32_mmap(struct thread *td,
488 struct freebsd6_freebsd32_mmap_args *uap)
493 #if defined(__amd64__)
494 if (i386_read_exec && (prot & PROT_READ))
498 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
499 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
504 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
506 struct itimerval itv, oitv, *itvp;
507 struct itimerval32 i32;
510 if (uap->itv != NULL) {
511 error = copyin(uap->itv, &i32, sizeof(i32));
514 TV_CP(i32, itv, it_interval);
515 TV_CP(i32, itv, it_value);
519 error = kern_setitimer(td, uap->which, itvp, &oitv);
520 if (error || uap->oitv == NULL)
522 TV_CP(oitv, i32, it_interval);
523 TV_CP(oitv, i32, it_value);
524 return (copyout(&i32, uap->oitv, sizeof(i32)));
528 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
530 struct itimerval itv;
531 struct itimerval32 i32;
534 error = kern_getitimer(td, uap->which, &itv);
535 if (error || uap->itv == NULL)
537 TV_CP(itv, i32, it_interval);
538 TV_CP(itv, i32, it_value);
539 return (copyout(&i32, uap->itv, sizeof(i32)));
543 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
545 struct timeval32 tv32;
546 struct timeval tv, *tvp;
549 if (uap->tv != NULL) {
550 error = copyin(uap->tv, &tv32, sizeof(tv32));
553 CP(tv32, tv, tv_sec);
554 CP(tv32, tv, tv_usec);
559 * XXX Do pointers need PTRIN()?
561 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
562 sizeof(int32_t) * 8));
566 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
568 struct timespec32 ts32;
570 struct timeval tv, *tvp;
574 if (uap->ts != NULL) {
575 error = copyin(uap->ts, &ts32, sizeof(ts32));
578 CP(ts32, ts, tv_sec);
579 CP(ts32, ts, tv_nsec);
580 TIMESPEC_TO_TIMEVAL(&tv, &ts);
584 if (uap->sm != NULL) {
585 error = copyin(uap->sm, &set, sizeof(set));
592 * XXX Do pointers need PTRIN()?
594 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
595 uset, sizeof(int32_t) * 8);
600 * Copy 'count' items into the destination list pointed to by uap->eventlist.
603 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
605 struct freebsd32_kevent_args *uap;
606 struct kevent32 ks32[KQ_NEVENTS];
609 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
610 uap = (struct freebsd32_kevent_args *)arg;
612 for (i = 0; i < count; i++) {
613 CP(kevp[i], ks32[i], ident);
614 CP(kevp[i], ks32[i], filter);
615 CP(kevp[i], ks32[i], flags);
616 CP(kevp[i], ks32[i], fflags);
617 CP(kevp[i], ks32[i], data);
618 PTROUT_CP(kevp[i], ks32[i], udata);
620 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
622 uap->eventlist += count;
627 * Copy 'count' items from the list pointed to by uap->changelist.
630 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
632 struct freebsd32_kevent_args *uap;
633 struct kevent32 ks32[KQ_NEVENTS];
636 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
637 uap = (struct freebsd32_kevent_args *)arg;
639 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
642 uap->changelist += count;
644 for (i = 0; i < count; i++) {
645 CP(ks32[i], kevp[i], ident);
646 CP(ks32[i], kevp[i], filter);
647 CP(ks32[i], kevp[i], flags);
648 CP(ks32[i], kevp[i], fflags);
649 CP(ks32[i], kevp[i], data);
650 PTRIN_CP(ks32[i], kevp[i], udata);
657 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
659 struct timespec32 ts32;
660 struct timespec ts, *tsp;
661 struct kevent_copyops k_ops = {
663 .k_copyout = freebsd32_kevent_copyout,
664 .k_copyin = freebsd32_kevent_copyin,
667 struct kevent32 *eventlist = uap->eventlist;
673 error = copyin(uap->timeout, &ts32, sizeof(ts32));
676 CP(ts32, ts, tv_sec);
677 CP(ts32, ts, tv_nsec);
682 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
683 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
684 uap->nchanges, sizeof(struct kevent32));
686 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
689 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
690 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
691 td->td_retval[0], sizeof(struct kevent32));
697 freebsd32_gettimeofday(struct thread *td,
698 struct freebsd32_gettimeofday_args *uap)
701 struct timeval32 atv32;
707 CP(atv, atv32, tv_sec);
708 CP(atv, atv32, tv_usec);
709 error = copyout(&atv32, uap->tp, sizeof (atv32));
711 if (error == 0 && uap->tzp != NULL) {
712 rtz.tz_minuteswest = tz_minuteswest;
713 rtz.tz_dsttime = tz_dsttime;
714 error = copyout(&rtz, uap->tzp, sizeof (rtz));
720 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
726 error = kern_getrusage(td, uap->who, &s);
728 freebsd32_rusage_out(&s, &s32);
729 error = copyout(&s32, uap->rusage, sizeof(s32));
735 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
737 struct iovec32 iov32;
744 if (iovcnt > UIO_MAXIOV)
746 iovlen = iovcnt * sizeof(struct iovec);
747 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
748 iov = (struct iovec *)(uio + 1);
749 for (i = 0; i < iovcnt; i++) {
750 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
755 iov[i].iov_base = PTRIN(iov32.iov_base);
756 iov[i].iov_len = iov32.iov_len;
759 uio->uio_iovcnt = iovcnt;
760 uio->uio_segflg = UIO_USERSPACE;
761 uio->uio_offset = -1;
763 for (i = 0; i < iovcnt; i++) {
764 if (iov->iov_len > INT_MAX - uio->uio_resid) {
768 uio->uio_resid += iov->iov_len;
776 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
781 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
784 error = kern_readv(td, uap->fd, auio);
790 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
795 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
798 error = kern_writev(td, uap->fd, auio);
804 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
809 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
812 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
818 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
823 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
826 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
832 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
835 struct iovec32 iov32;
841 if (iovcnt > UIO_MAXIOV)
843 iovlen = iovcnt * sizeof(struct iovec);
844 iov = malloc(iovlen, M_IOV, M_WAITOK);
845 for (i = 0; i < iovcnt; i++) {
846 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
851 iov[i].iov_base = PTRIN(iov32.iov_base);
852 iov[i].iov_len = iov32.iov_len;
859 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
864 error = copyin(msg32, &m32, sizeof(m32));
867 msg->msg_name = PTRIN(m32.msg_name);
868 msg->msg_namelen = m32.msg_namelen;
869 msg->msg_iov = PTRIN(m32.msg_iov);
870 msg->msg_iovlen = m32.msg_iovlen;
871 msg->msg_control = PTRIN(m32.msg_control);
872 msg->msg_controllen = m32.msg_controllen;
873 msg->msg_flags = m32.msg_flags;
878 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
883 m32.msg_name = PTROUT(msg->msg_name);
884 m32.msg_namelen = msg->msg_namelen;
885 m32.msg_iov = PTROUT(msg->msg_iov);
886 m32.msg_iovlen = msg->msg_iovlen;
887 m32.msg_control = PTROUT(msg->msg_control);
888 m32.msg_controllen = msg->msg_controllen;
889 m32.msg_flags = msg->msg_flags;
890 error = copyout(&m32, msg32, sizeof(m32));
895 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
897 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
899 #define FREEBSD32_ALIGN(p) \
900 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
901 #define FREEBSD32_CMSG_SPACE(l) \
902 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
904 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
905 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
908 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
912 struct timespec32 ts;
925 switch (cm->cmsg_level) {
927 switch (cm->cmsg_type) {
929 TV_CP(*in, tmp32, tv);
930 copylen = sizeof(tmp32.tv);
934 BT_CP(*in, tmp32, bt);
935 copylen = sizeof(tmp32.bt);
940 TS_CP(*in, tmp32, ts);
941 copylen = sizeof(tmp32.ts);
955 KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
957 bcopy(&tmp32, data, copylen);
962 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
966 socklen_t clen, datalen, datalen_out, oldclen;
969 int len, maxlen, copylen;
973 len = msg->msg_controllen;
974 maxlen = msg->msg_controllen;
975 msg->msg_controllen = 0;
977 ctlbuf = msg->msg_control;
978 for (m = control; m != NULL && len > 0; m = m->m_next) {
979 cm = mtod(m, struct cmsghdr *);
982 if (sizeof(struct cmsghdr) > clen ||
983 cm->cmsg_len > clen) {
988 data = CMSG_DATA(cm);
989 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
990 datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
993 * Copy out the message header. Preserve the native
994 * message size in case we need to inspect the message
997 copylen = sizeof(struct cmsghdr);
999 msg->msg_flags |= MSG_CTRUNC;
1000 m_dispose_extcontrolm(m);
1003 oldclen = cm->cmsg_len;
1004 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1006 error = copyout(cm, ctlbuf, copylen);
1007 cm->cmsg_len = oldclen;
1011 ctlbuf += FREEBSD32_ALIGN(copylen);
1012 len -= FREEBSD32_ALIGN(copylen);
1014 copylen = datalen_out;
1015 if (len < copylen) {
1016 msg->msg_flags |= MSG_CTRUNC;
1017 m_dispose_extcontrolm(m);
1021 /* Copy out the message data. */
1022 error = copyout(data, ctlbuf, copylen);
1026 ctlbuf += FREEBSD32_ALIGN(copylen);
1027 len -= FREEBSD32_ALIGN(copylen);
1029 if (CMSG_SPACE(datalen) < clen) {
1030 clen -= CMSG_SPACE(datalen);
1031 cm = (struct cmsghdr *)
1032 ((caddr_t)cm + CMSG_SPACE(datalen));
1038 msg->msg_controllen +=
1039 FREEBSD32_CMSG_SPACE(datalen_out);
1042 if (len == 0 && m != NULL) {
1043 msg->msg_flags |= MSG_CTRUNC;
1044 m_dispose_extcontrolm(m);
1052 freebsd32_recvmsg(td, uap)
1054 struct freebsd32_recvmsg_args /* {
1056 struct msghdr32 *msg;
1061 struct msghdr32 m32;
1062 struct iovec *uiov, *iov;
1063 struct mbuf *control = NULL;
1064 struct mbuf **controlp;
1067 error = copyin(uap->msg, &m32, sizeof(m32));
1070 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1073 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1077 msg.msg_flags = uap->flags;
1081 controlp = (msg.msg_control != NULL) ? &control : NULL;
1082 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1086 if (control != NULL)
1087 error = freebsd32_copy_msg_out(&msg, control);
1089 msg.msg_controllen = 0;
1092 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1096 if (control != NULL) {
1098 m_dispose_extcontrolm(control);
1106 * Copy-in the array of control messages constructed using alignment
1107 * and padding suitable for a 32-bit environment and construct an
1108 * mbuf using alignment and padding suitable for a 64-bit kernel.
1109 * The alignment and padding are defined indirectly by CMSG_DATA(),
1110 * CMSG_SPACE() and CMSG_LEN().
1113 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1117 void *in, *in1, *md;
1118 u_int msglen, outlen;
1121 if (buflen > MCLBYTES)
1124 in = malloc(buflen, M_TEMP, M_WAITOK);
1125 error = copyin(buf, in, buflen);
1130 * Make a pass over the input buffer to determine the amount of space
1131 * required for 64 bit-aligned copies of the control messages.
1135 while (buflen > 0) {
1136 if (buflen < sizeof(*cm)) {
1140 cm = (struct cmsghdr *)in1;
1141 if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm))) {
1145 msglen = FREEBSD32_ALIGN(cm->cmsg_len);
1146 if (msglen > buflen || msglen < cm->cmsg_len) {
1152 in1 = (char *)in1 + msglen;
1153 outlen += CMSG_ALIGN(sizeof(*cm)) +
1154 CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm)));
1156 if (error == 0 && outlen > MCLBYTES) {
1158 * XXXMJ This implies that the upper limit on 32-bit aligned
1159 * control messages is less than MCLBYTES, and so we are not
1160 * perfectly compatible. However, there is no platform
1161 * guarantee that mbuf clusters larger than MCLBYTES can be
1169 m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0);
1171 md = mtod(m, void *);
1174 * Make a second pass over input messages, copying them into the output
1178 while (outlen > 0) {
1179 /* Copy the message header and align the length field. */
1181 memcpy(cm, in1, sizeof(*cm));
1182 msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm));
1183 cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen;
1185 /* Copy the message body. */
1186 in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm));
1187 md = (char *)md + CMSG_ALIGN(sizeof(*cm));
1188 memcpy(md, in1, msglen);
1189 in1 = (char *)in1 + FREEBSD32_ALIGN(msglen);
1190 md = (char *)md + CMSG_ALIGN(msglen);
1191 KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen),
1192 ("outlen %u underflow, msglen %u", outlen, msglen));
1193 outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen);
1203 freebsd32_sendmsg(struct thread *td,
1204 struct freebsd32_sendmsg_args *uap)
1207 struct msghdr32 m32;
1209 struct mbuf *control = NULL;
1210 struct sockaddr *to = NULL;
1213 error = copyin(uap->msg, &m32, sizeof(m32));
1216 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1219 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1224 if (msg.msg_name != NULL) {
1225 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1233 if (msg.msg_control) {
1234 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1239 error = freebsd32_copyin_control(&control, msg.msg_control,
1240 msg.msg_controllen);
1244 msg.msg_control = NULL;
1245 msg.msg_controllen = 0;
1248 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1259 freebsd32_recvfrom(struct thread *td,
1260 struct freebsd32_recvfrom_args *uap)
1266 if (uap->fromlenaddr) {
1267 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1268 sizeof(msg.msg_namelen));
1272 msg.msg_namelen = 0;
1275 msg.msg_name = PTRIN(uap->from);
1276 msg.msg_iov = &aiov;
1278 aiov.iov_base = PTRIN(uap->buf);
1279 aiov.iov_len = uap->len;
1280 msg.msg_control = NULL;
1281 msg.msg_flags = uap->flags;
1282 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1283 if (error == 0 && uap->fromlenaddr)
1284 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1285 sizeof (msg.msg_namelen));
1290 freebsd32_settimeofday(struct thread *td,
1291 struct freebsd32_settimeofday_args *uap)
1293 struct timeval32 tv32;
1294 struct timeval tv, *tvp;
1295 struct timezone tz, *tzp;
1299 error = copyin(uap->tv, &tv32, sizeof(tv32));
1302 CP(tv32, tv, tv_sec);
1303 CP(tv32, tv, tv_usec);
1308 error = copyin(uap->tzp, &tz, sizeof(tz));
1314 return (kern_settimeofday(td, tvp, tzp));
1318 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1320 struct timeval32 s32[2];
1321 struct timeval s[2], *sp;
1324 if (uap->tptr != NULL) {
1325 error = copyin(uap->tptr, s32, sizeof(s32));
1328 CP(s32[0], s[0], tv_sec);
1329 CP(s32[0], s[0], tv_usec);
1330 CP(s32[1], s[1], tv_sec);
1331 CP(s32[1], s[1], tv_usec);
1335 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1340 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1342 struct timeval32 s32[2];
1343 struct timeval s[2], *sp;
1346 if (uap->tptr != NULL) {
1347 error = copyin(uap->tptr, s32, sizeof(s32));
1350 CP(s32[0], s[0], tv_sec);
1351 CP(s32[0], s[0], tv_usec);
1352 CP(s32[1], s[1], tv_sec);
1353 CP(s32[1], s[1], tv_usec);
1357 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1361 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1363 struct timeval32 s32[2];
1364 struct timeval s[2], *sp;
1367 if (uap->tptr != NULL) {
1368 error = copyin(uap->tptr, s32, sizeof(s32));
1371 CP(s32[0], s[0], tv_sec);
1372 CP(s32[0], s[0], tv_usec);
1373 CP(s32[1], s[1], tv_sec);
1374 CP(s32[1], s[1], tv_usec);
1378 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1382 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1384 struct timeval32 s32[2];
1385 struct timeval s[2], *sp;
1388 if (uap->times != NULL) {
1389 error = copyin(uap->times, s32, sizeof(s32));
1392 CP(s32[0], s[0], tv_sec);
1393 CP(s32[0], s[0], tv_usec);
1394 CP(s32[1], s[1], tv_sec);
1395 CP(s32[1], s[1], tv_usec);
1399 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1404 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1406 struct timespec32 ts32[2];
1407 struct timespec ts[2], *tsp;
1410 if (uap->times != NULL) {
1411 error = copyin(uap->times, ts32, sizeof(ts32));
1414 CP(ts32[0], ts[0], tv_sec);
1415 CP(ts32[0], ts[0], tv_nsec);
1416 CP(ts32[1], ts[1], tv_sec);
1417 CP(ts32[1], ts[1], tv_nsec);
1421 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1425 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1427 struct timespec32 ts32[2];
1428 struct timespec ts[2], *tsp;
1431 if (uap->times != NULL) {
1432 error = copyin(uap->times, ts32, sizeof(ts32));
1435 CP(ts32[0], ts[0], tv_sec);
1436 CP(ts32[0], ts[0], tv_nsec);
1437 CP(ts32[1], ts[1], tv_sec);
1438 CP(ts32[1], ts[1], tv_nsec);
1442 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1443 tsp, UIO_SYSSPACE, uap->flag));
1447 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1449 struct timeval32 tv32;
1450 struct timeval delta, olddelta, *deltap;
1454 error = copyin(uap->delta, &tv32, sizeof(tv32));
1457 CP(tv32, delta, tv_sec);
1458 CP(tv32, delta, tv_usec);
1462 error = kern_adjtime(td, deltap, &olddelta);
1463 if (uap->olddelta && error == 0) {
1464 CP(olddelta, tv32, tv_sec);
1465 CP(olddelta, tv32, tv_usec);
1466 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1471 #ifdef COMPAT_FREEBSD4
1473 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1475 struct statfs32 s32;
1479 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1480 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1482 copy_statfs(sp, &s32);
1483 error = copyout(&s32, uap->buf, sizeof(s32));
1490 #ifdef COMPAT_FREEBSD4
1492 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1494 struct statfs32 s32;
1498 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1499 error = kern_fstatfs(td, uap->fd, sp);
1501 copy_statfs(sp, &s32);
1502 error = copyout(&s32, uap->buf, sizeof(s32));
1509 #ifdef COMPAT_FREEBSD4
1511 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1513 struct statfs32 s32;
1518 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1520 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1521 error = kern_fhstatfs(td, fh, sp);
1523 copy_statfs(sp, &s32);
1524 error = copyout(&s32, uap->buf, sizeof(s32));
1532 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1535 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1536 PAIR32TO64(off_t, uap->offset)));
1540 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1543 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1544 PAIR32TO64(off_t, uap->offset)));
1549 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1552 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1557 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1562 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1564 /* Expand the quad return into two parts for eax and edx */
1565 pos = td->td_uretoff.tdu_off;
1566 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1567 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1572 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1575 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1576 PAIR32TO64(off_t, uap->length)));
1580 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1583 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1588 ofreebsd32_getdirentries(struct thread *td,
1589 struct ofreebsd32_getdirentries_args *uap)
1591 struct ogetdirentries_args ap;
1598 ap.count = uap->count;
1600 error = kern_ogetdirentries(td, &ap, &loff);
1603 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1610 freebsd32_getdirentries(struct thread *td,
1611 struct freebsd32_getdirentries_args *uap)
1617 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1618 NULL, UIO_USERSPACE);
1621 if (uap->basep != NULL) {
1623 error = copyout(&base32, uap->basep, sizeof(int32_t));
1628 #ifdef COMPAT_FREEBSD6
1629 /* versions with the 'int pad' argument */
1631 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1634 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1635 PAIR32TO64(off_t, uap->offset)));
1639 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1642 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1643 PAIR32TO64(off_t, uap->offset)));
1647 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1652 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1654 /* Expand the quad return into two parts for eax and edx */
1655 pos = *(off_t *)(td->td_retval);
1656 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1657 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1662 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1665 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1666 PAIR32TO64(off_t, uap->length)));
1670 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1673 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1675 #endif /* COMPAT_FREEBSD6 */
1685 freebsd32_do_sendfile(struct thread *td,
1686 struct freebsd32_sendfile_args *uap, int compat)
1688 struct sf_hdtr32 hdtr32;
1689 struct sf_hdtr hdtr;
1690 struct uio *hdr_uio, *trl_uio;
1692 cap_rights_t rights;
1693 struct iovec32 *iov32;
1694 off_t offset, sbytes;
1697 offset = PAIR32TO64(off_t, uap->offset);
1701 hdr_uio = trl_uio = NULL;
1703 if (uap->hdtr != NULL) {
1704 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1707 PTRIN_CP(hdtr32, hdtr, headers);
1708 CP(hdtr32, hdtr, hdr_cnt);
1709 PTRIN_CP(hdtr32, hdtr, trailers);
1710 CP(hdtr32, hdtr, trl_cnt);
1712 if (hdtr.headers != NULL) {
1713 iov32 = PTRIN(hdtr32.headers);
1714 error = freebsd32_copyinuio(iov32,
1715 hdtr32.hdr_cnt, &hdr_uio);
1718 #ifdef COMPAT_FREEBSD4
1720 * In FreeBSD < 5.0 the nbytes to send also included
1721 * the header. If compat is specified subtract the
1722 * header size from nbytes.
1725 if (uap->nbytes > hdr_uio->uio_resid)
1726 uap->nbytes -= hdr_uio->uio_resid;
1732 if (hdtr.trailers != NULL) {
1733 iov32 = PTRIN(hdtr32.trailers);
1734 error = freebsd32_copyinuio(iov32,
1735 hdtr32.trl_cnt, &trl_uio);
1741 AUDIT_ARG_FD(uap->fd);
1743 if ((error = fget_read(td, uap->fd,
1744 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1747 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1748 uap->nbytes, &sbytes, uap->flags, td);
1751 if (uap->sbytes != NULL)
1752 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1756 free(hdr_uio, M_IOV);
1758 free(trl_uio, M_IOV);
1762 #ifdef COMPAT_FREEBSD4
1764 freebsd4_freebsd32_sendfile(struct thread *td,
1765 struct freebsd4_freebsd32_sendfile_args *uap)
1767 return (freebsd32_do_sendfile(td,
1768 (struct freebsd32_sendfile_args *)uap, 1));
1773 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1776 return (freebsd32_do_sendfile(td, uap, 0));
1780 copy_stat(struct stat *in, struct stat32 *out)
1783 CP(*in, *out, st_dev);
1784 CP(*in, *out, st_ino);
1785 CP(*in, *out, st_mode);
1786 CP(*in, *out, st_nlink);
1787 CP(*in, *out, st_uid);
1788 CP(*in, *out, st_gid);
1789 CP(*in, *out, st_rdev);
1790 TS_CP(*in, *out, st_atim);
1791 TS_CP(*in, *out, st_mtim);
1792 TS_CP(*in, *out, st_ctim);
1793 CP(*in, *out, st_size);
1794 CP(*in, *out, st_blocks);
1795 CP(*in, *out, st_blksize);
1796 CP(*in, *out, st_flags);
1797 CP(*in, *out, st_gen);
1798 TS_CP(*in, *out, st_birthtim);
1803 copy_ostat(struct stat *in, struct ostat32 *out)
1806 CP(*in, *out, st_dev);
1807 CP(*in, *out, st_ino);
1808 CP(*in, *out, st_mode);
1809 CP(*in, *out, st_nlink);
1810 CP(*in, *out, st_uid);
1811 CP(*in, *out, st_gid);
1812 CP(*in, *out, st_rdev);
1813 CP(*in, *out, st_size);
1814 TS_CP(*in, *out, st_atim);
1815 TS_CP(*in, *out, st_mtim);
1816 TS_CP(*in, *out, st_ctim);
1817 CP(*in, *out, st_blksize);
1818 CP(*in, *out, st_blocks);
1819 CP(*in, *out, st_flags);
1820 CP(*in, *out, st_gen);
1825 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
1831 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1835 copy_stat(&sb, &sb32);
1836 error = copyout(&sb32, uap->ub, sizeof (sb32));
1842 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1845 struct ostat32 sb32;
1848 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1852 copy_ostat(&sb, &sb32);
1853 error = copyout(&sb32, uap->ub, sizeof (sb32));
1859 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1865 error = kern_fstat(td, uap->fd, &ub);
1868 copy_stat(&ub, &ub32);
1869 error = copyout(&ub32, uap->ub, sizeof(ub32));
1875 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1878 struct ostat32 ub32;
1881 error = kern_fstat(td, uap->fd, &ub);
1884 copy_ostat(&ub, &ub32);
1885 error = copyout(&ub32, uap->ub, sizeof(ub32));
1891 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1897 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1901 copy_stat(&ub, &ub32);
1902 error = copyout(&ub32, uap->buf, sizeof(ub32));
1907 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
1913 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1914 UIO_USERSPACE, &sb, NULL);
1917 copy_stat(&sb, &sb32);
1918 error = copyout(&sb32, uap->ub, sizeof (sb32));
1924 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1927 struct ostat32 sb32;
1930 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
1931 UIO_USERSPACE, &sb, NULL);
1934 copy_ostat(&sb, &sb32);
1935 error = copyout(&sb32, uap->ub, sizeof (sb32));
1941 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
1943 int error, name[CTL_MAXNAME];
1947 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1949 error = copyin(uap->name, name, uap->namelen * sizeof(int));
1953 error = fueword32(uap->oldlenp, &tmp);
1960 error = userland_sysctl(td, name, uap->namelen,
1961 uap->old, &oldlen, 1,
1962 uap->new, uap->newlen, &j, SCTL_MASK32);
1963 if (error && error != ENOMEM)
1966 suword32(uap->oldlenp, j);
1971 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
1977 error = copyin(uap->jail, &version, sizeof(uint32_t));
1984 /* FreeBSD single IPv4 jails. */
1985 struct jail32_v0 j32_v0;
1987 bzero(&j, sizeof(struct jail));
1988 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
1991 CP(j32_v0, j, version);
1992 PTRIN_CP(j32_v0, j, path);
1993 PTRIN_CP(j32_v0, j, hostname);
1994 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2000 * Version 1 was used by multi-IPv4 jail implementations
2001 * that never made it into the official kernel.
2005 case 2: /* JAIL_API_VERSION */
2007 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2010 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2013 CP(j32, j, version);
2014 PTRIN_CP(j32, j, path);
2015 PTRIN_CP(j32, j, hostname);
2016 PTRIN_CP(j32, j, jailname);
2019 PTRIN_CP(j32, j, ip4);
2020 PTRIN_CP(j32, j, ip6);
2025 /* Sci-Fi jails are not supported, sorry. */
2028 return (kern_jail(td, &j));
2032 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2037 /* Check that we have an even number of iovecs. */
2038 if (uap->iovcnt & 1)
2041 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2044 error = kern_jail_set(td, auio, uap->flags);
2050 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2052 struct iovec32 iov32;
2056 /* Check that we have an even number of iovecs. */
2057 if (uap->iovcnt & 1)
2060 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2063 error = kern_jail_get(td, auio, uap->flags);
2065 for (i = 0; i < uap->iovcnt; i++) {
2066 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2067 CP(auio->uio_iov[i], iov32, iov_len);
2068 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2077 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2079 struct sigaction32 s32;
2080 struct sigaction sa, osa, *sap;
2084 error = copyin(uap->act, &s32, sizeof(s32));
2087 sa.sa_handler = PTRIN(s32.sa_u);
2088 CP(s32, sa, sa_flags);
2089 CP(s32, sa, sa_mask);
2093 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2094 if (error == 0 && uap->oact != NULL) {
2095 s32.sa_u = PTROUT(osa.sa_handler);
2096 CP(osa, s32, sa_flags);
2097 CP(osa, s32, sa_mask);
2098 error = copyout(&s32, uap->oact, sizeof(s32));
2103 #ifdef COMPAT_FREEBSD4
2105 freebsd4_freebsd32_sigaction(struct thread *td,
2106 struct freebsd4_freebsd32_sigaction_args *uap)
2108 struct sigaction32 s32;
2109 struct sigaction sa, osa, *sap;
2113 error = copyin(uap->act, &s32, sizeof(s32));
2116 sa.sa_handler = PTRIN(s32.sa_u);
2117 CP(s32, sa, sa_flags);
2118 CP(s32, sa, sa_mask);
2122 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2123 if (error == 0 && uap->oact != NULL) {
2124 s32.sa_u = PTROUT(osa.sa_handler);
2125 CP(osa, s32, sa_flags);
2126 CP(osa, s32, sa_mask);
2127 error = copyout(&s32, uap->oact, sizeof(s32));
2134 struct osigaction32 {
2143 ofreebsd32_sigaction(struct thread *td,
2144 struct ofreebsd32_sigaction_args *uap)
2146 struct osigaction32 s32;
2147 struct sigaction sa, osa, *sap;
2150 if (uap->signum <= 0 || uap->signum >= ONSIG)
2154 error = copyin(uap->nsa, &s32, sizeof(s32));
2157 sa.sa_handler = PTRIN(s32.sa_u);
2158 CP(s32, sa, sa_flags);
2159 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2163 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2164 if (error == 0 && uap->osa != NULL) {
2165 s32.sa_u = PTROUT(osa.sa_handler);
2166 CP(osa, s32, sa_flags);
2167 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2168 error = copyout(&s32, uap->osa, sizeof(s32));
2174 ofreebsd32_sigprocmask(struct thread *td,
2175 struct ofreebsd32_sigprocmask_args *uap)
2180 OSIG2SIG(uap->mask, set);
2181 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2182 SIG2OSIG(oset, td->td_retval[0]);
2187 ofreebsd32_sigpending(struct thread *td,
2188 struct ofreebsd32_sigpending_args *uap)
2190 struct proc *p = td->td_proc;
2194 siglist = p->p_siglist;
2195 SIGSETOR(siglist, td->td_siglist);
2197 SIG2OSIG(siglist, td->td_retval[0]);
2202 u_int32_t sv_handler;
2208 ofreebsd32_sigvec(struct thread *td,
2209 struct ofreebsd32_sigvec_args *uap)
2211 struct sigvec32 vec;
2212 struct sigaction sa, osa, *sap;
2215 if (uap->signum <= 0 || uap->signum >= ONSIG)
2219 error = copyin(uap->nsv, &vec, sizeof(vec));
2222 sa.sa_handler = PTRIN(vec.sv_handler);
2223 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2224 sa.sa_flags = vec.sv_flags;
2225 sa.sa_flags ^= SA_RESTART;
2229 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2230 if (error == 0 && uap->osv != NULL) {
2231 vec.sv_handler = PTROUT(osa.sa_handler);
2232 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2233 vec.sv_flags = osa.sa_flags;
2234 vec.sv_flags &= ~SA_NOCLDWAIT;
2235 vec.sv_flags ^= SA_RESTART;
2236 error = copyout(&vec, uap->osv, sizeof(vec));
2242 ofreebsd32_sigblock(struct thread *td,
2243 struct ofreebsd32_sigblock_args *uap)
2247 OSIG2SIG(uap->mask, set);
2248 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2249 SIG2OSIG(oset, td->td_retval[0]);
2254 ofreebsd32_sigsetmask(struct thread *td,
2255 struct ofreebsd32_sigsetmask_args *uap)
2259 OSIG2SIG(uap->mask, set);
2260 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2261 SIG2OSIG(oset, td->td_retval[0]);
2266 ofreebsd32_sigsuspend(struct thread *td,
2267 struct ofreebsd32_sigsuspend_args *uap)
2271 OSIG2SIG(uap->mask, mask);
2272 return (kern_sigsuspend(td, mask));
2281 ofreebsd32_sigstack(struct thread *td,
2282 struct ofreebsd32_sigstack_args *uap)
2284 struct sigstack32 s32;
2285 struct sigstack nss, oss;
2286 int error = 0, unss;
2288 if (uap->nss != NULL) {
2289 error = copyin(uap->nss, &s32, sizeof(s32));
2292 nss.ss_sp = PTRIN(s32.ss_sp);
2293 CP(s32, nss, ss_onstack);
2298 oss.ss_sp = td->td_sigstk.ss_sp;
2299 oss.ss_onstack = sigonstack(cpu_getstack(td));
2301 td->td_sigstk.ss_sp = nss.ss_sp;
2302 td->td_sigstk.ss_size = 0;
2303 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2304 td->td_pflags |= TDP_ALTSTACK;
2306 if (uap->oss != NULL) {
2307 s32.ss_sp = PTROUT(oss.ss_sp);
2308 CP(oss, s32, ss_onstack);
2309 error = copyout(&s32, uap->oss, sizeof(s32));
2316 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2319 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2320 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2324 freebsd32_clock_nanosleep(struct thread *td,
2325 struct freebsd32_clock_nanosleep_args *uap)
2329 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2330 uap->rqtp, uap->rmtp);
2331 return (kern_posix_error(td, error));
2335 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2336 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2338 struct timespec32 rmt32, rqt32;
2339 struct timespec rmt, rqt;
2342 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2346 CP(rqt32, rqt, tv_sec);
2347 CP(rqt32, rqt, tv_nsec);
2349 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2350 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2352 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2353 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2356 CP(rmt, rmt32, tv_sec);
2357 CP(rmt, rmt32, tv_nsec);
2359 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2367 freebsd32_clock_gettime(struct thread *td,
2368 struct freebsd32_clock_gettime_args *uap)
2370 struct timespec ats;
2371 struct timespec32 ats32;
2374 error = kern_clock_gettime(td, uap->clock_id, &ats);
2376 CP(ats, ats32, tv_sec);
2377 CP(ats, ats32, tv_nsec);
2378 error = copyout(&ats32, uap->tp, sizeof(ats32));
2384 freebsd32_clock_settime(struct thread *td,
2385 struct freebsd32_clock_settime_args *uap)
2387 struct timespec ats;
2388 struct timespec32 ats32;
2391 error = copyin(uap->tp, &ats32, sizeof(ats32));
2394 CP(ats32, ats, tv_sec);
2395 CP(ats32, ats, tv_nsec);
2397 return (kern_clock_settime(td, uap->clock_id, &ats));
2401 freebsd32_clock_getres(struct thread *td,
2402 struct freebsd32_clock_getres_args *uap)
2405 struct timespec32 ts32;
2408 if (uap->tp == NULL)
2410 error = kern_clock_getres(td, uap->clock_id, &ts);
2412 CP(ts, ts32, tv_sec);
2413 CP(ts, ts32, tv_nsec);
2414 error = copyout(&ts32, uap->tp, sizeof(ts32));
2419 int freebsd32_ktimer_create(struct thread *td,
2420 struct freebsd32_ktimer_create_args *uap)
2422 struct sigevent32 ev32;
2423 struct sigevent ev, *evp;
2426 if (uap->evp == NULL) {
2430 error = copyin(uap->evp, &ev32, sizeof(ev32));
2433 error = convert_sigevent32(&ev32, &ev);
2437 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2439 error = copyout(&id, uap->timerid, sizeof(int));
2441 kern_ktimer_delete(td, id);
2447 freebsd32_ktimer_settime(struct thread *td,
2448 struct freebsd32_ktimer_settime_args *uap)
2450 struct itimerspec32 val32, oval32;
2451 struct itimerspec val, oval, *ovalp;
2454 error = copyin(uap->value, &val32, sizeof(val32));
2458 ovalp = uap->ovalue != NULL ? &oval : NULL;
2459 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2460 if (error == 0 && uap->ovalue != NULL) {
2461 ITS_CP(oval, oval32);
2462 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2468 freebsd32_ktimer_gettime(struct thread *td,
2469 struct freebsd32_ktimer_gettime_args *uap)
2471 struct itimerspec32 val32;
2472 struct itimerspec val;
2475 error = kern_ktimer_gettime(td, uap->timerid, &val);
2478 error = copyout(&val32, uap->value, sizeof(val32));
2484 freebsd32_clock_getcpuclockid2(struct thread *td,
2485 struct freebsd32_clock_getcpuclockid2_args *uap)
2490 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2491 uap->which, &clk_id);
2493 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2498 freebsd32_thr_new(struct thread *td,
2499 struct freebsd32_thr_new_args *uap)
2501 struct thr_param32 param32;
2502 struct thr_param param;
2505 if (uap->param_size < 0 ||
2506 uap->param_size > sizeof(struct thr_param32))
2508 bzero(¶m, sizeof(struct thr_param));
2509 bzero(¶m32, sizeof(struct thr_param32));
2510 error = copyin(uap->param, ¶m32, uap->param_size);
2513 param.start_func = PTRIN(param32.start_func);
2514 param.arg = PTRIN(param32.arg);
2515 param.stack_base = PTRIN(param32.stack_base);
2516 param.stack_size = param32.stack_size;
2517 param.tls_base = PTRIN(param32.tls_base);
2518 param.tls_size = param32.tls_size;
2519 param.child_tid = PTRIN(param32.child_tid);
2520 param.parent_tid = PTRIN(param32.parent_tid);
2521 param.flags = param32.flags;
2522 param.rtp = PTRIN(param32.rtp);
2523 param.spare[0] = PTRIN(param32.spare[0]);
2524 param.spare[1] = PTRIN(param32.spare[1]);
2525 param.spare[2] = PTRIN(param32.spare[2]);
2527 return (kern_thr_new(td, ¶m));
2531 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2533 struct timespec32 ts32;
2534 struct timespec ts, *tsp;
2539 if (uap->timeout != NULL) {
2540 error = copyin((const void *)uap->timeout, (void *)&ts32,
2541 sizeof(struct timespec32));
2544 ts.tv_sec = ts32.tv_sec;
2545 ts.tv_nsec = ts32.tv_nsec;
2548 return (kern_thr_suspend(td, tsp));
2552 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2554 bzero(dst, sizeof(*dst));
2555 dst->si_signo = src->si_signo;
2556 dst->si_errno = src->si_errno;
2557 dst->si_code = src->si_code;
2558 dst->si_pid = src->si_pid;
2559 dst->si_uid = src->si_uid;
2560 dst->si_status = src->si_status;
2561 dst->si_addr = (uintptr_t)src->si_addr;
2562 dst->si_value.sival_int = src->si_value.sival_int;
2563 dst->si_timerid = src->si_timerid;
2564 dst->si_overrun = src->si_overrun;
2567 #ifndef _FREEBSD32_SYSPROTO_H_
2568 struct freebsd32_sigqueue_args {
2571 /* union sigval32 */ int value;
2575 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2580 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2581 * On 64-bit little-endian ABIs, the low bits are the same.
2582 * In 64-bit big-endian ABIs, sival_int overlaps with
2583 * sival_ptr's HIGH bits. We choose to support sival_int
2584 * rather than sival_ptr in this case as it seems to be
2587 bzero(&sv, sizeof(sv));
2588 sv.sival_int = uap->value;
2590 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2594 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2596 struct timespec32 ts32;
2598 struct timespec *timeout;
2601 struct siginfo32 si32;
2605 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2608 ts.tv_sec = ts32.tv_sec;
2609 ts.tv_nsec = ts32.tv_nsec;
2614 error = copyin(uap->set, &set, sizeof(set));
2618 error = kern_sigtimedwait(td, set, &ksi, timeout);
2623 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2624 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2628 td->td_retval[0] = ksi.ksi_signo;
2636 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2639 struct siginfo32 si32;
2643 error = copyin(uap->set, &set, sizeof(set));
2647 error = kern_sigtimedwait(td, set, &ksi, NULL);
2652 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2653 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2656 td->td_retval[0] = ksi.ksi_signo;
2661 freebsd32_cpuset_setid(struct thread *td,
2662 struct freebsd32_cpuset_setid_args *uap)
2665 return (kern_cpuset_setid(td, uap->which,
2666 PAIR32TO64(id_t, uap->id), uap->setid));
2670 freebsd32_cpuset_getid(struct thread *td,
2671 struct freebsd32_cpuset_getid_args *uap)
2674 return (kern_cpuset_getid(td, uap->level, uap->which,
2675 PAIR32TO64(id_t, uap->id), uap->setid));
2679 freebsd32_cpuset_getaffinity(struct thread *td,
2680 struct freebsd32_cpuset_getaffinity_args *uap)
2683 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2684 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2688 freebsd32_cpuset_setaffinity(struct thread *td,
2689 struct freebsd32_cpuset_setaffinity_args *uap)
2692 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2693 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2697 freebsd32_nmount(struct thread *td,
2698 struct freebsd32_nmount_args /* {
2700 unsigned int iovcnt;
2709 * Mount flags are now 64-bits. On 32-bit archtectures only
2710 * 32-bits are passed in, but from here on everything handles
2711 * 64-bit flags correctly.
2715 AUDIT_ARG_FFLAGS(flags);
2718 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2719 * userspace to set this flag, but we must filter it out if we want
2720 * MNT_UPDATE on the root file system to work.
2721 * MNT_ROOTFS should only be set by the kernel when mounting its
2724 flags &= ~MNT_ROOTFS;
2727 * check that we have an even number of iovec's
2728 * and that we have at least two options.
2730 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2733 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2736 error = vfs_donmount(td, flags, auio);
2744 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2746 struct yyy32 *p32, s32;
2747 struct yyy *p = NULL, s;
2752 error = copyin(uap->zzz, &s32, sizeof(s32));
2758 error = kern_xxx(td, p);
2763 error = copyout(&s32, p32, sizeof(s32));
2770 syscall32_register(int *offset, struct sysent *new_sysent,
2771 struct sysent *old_sysent, int flags)
2774 if ((flags & ~SY_THR_STATIC) != 0)
2777 if (*offset == NO_SYSCALL) {
2780 for (i = 1; i < SYS_MAXSYSCALL; ++i)
2781 if (freebsd32_sysent[i].sy_call ==
2782 (sy_call_t *)lkmnosys)
2784 if (i == SYS_MAXSYSCALL)
2787 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
2789 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
2790 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
2793 *old_sysent = freebsd32_sysent[*offset];
2794 freebsd32_sysent[*offset] = *new_sysent;
2795 atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
2800 syscall32_deregister(int *offset, struct sysent *old_sysent)
2806 freebsd32_sysent[*offset] = *old_sysent;
2811 syscall32_module_handler(struct module *mod, int what, void *arg)
2813 struct syscall_module_data *data = (struct syscall_module_data*)arg;
2819 error = syscall32_register(data->offset, data->new_sysent,
2820 &data->old_sysent, SY_THR_STATIC_KLD);
2822 /* Leave a mark so we know to safely unload below. */
2823 data->offset = NULL;
2826 ms.intval = *data->offset;
2828 module_setspecific(mod, &ms);
2831 error = data->chainevh(mod, what, data->chainarg);
2835 * MOD_LOAD failed, so just return without calling the
2836 * chained handler since we didn't pass along the MOD_LOAD
2839 if (data->offset == NULL)
2841 if (data->chainevh) {
2842 error = data->chainevh(mod, what, data->chainarg);
2846 error = syscall32_deregister(data->offset, &data->old_sysent);
2851 error = data->chainevh(mod, what, data->chainarg);
2857 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
2859 struct syscall_helper_data *sd1;
2862 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
2863 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
2864 &sd1->old_sysent, flags);
2866 syscall32_helper_unregister(sd);
2869 sd1->registered = 1;
2875 syscall32_helper_unregister(struct syscall_helper_data *sd)
2877 struct syscall_helper_data *sd1;
2879 for (sd1 = sd; sd1->registered != 0; sd1++) {
2880 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
2881 sd1->registered = 0;
2887 freebsd32_copyout_strings(struct image_params *imgp)
2893 u_int32_t *stack_base;
2894 struct freebsd32_ps_strings *arginfo;
2895 char canary[sizeof(long) * 8];
2896 int32_t pagesizes32[MAXPAGESIZES];
2897 size_t execpath_len;
2901 * Calculate string base and vector table pointers.
2902 * Also deal with signal trampoline code for this exec type.
2904 if (imgp->execpath != NULL && imgp->auxargs != NULL)
2905 execpath_len = strlen(imgp->execpath) + 1;
2908 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
2910 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
2911 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
2914 destp = (uintptr_t)arginfo;
2919 if (szsigcode != 0) {
2921 destp = rounddown2(destp, sizeof(uint32_t));
2922 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
2927 * Copy the image path for the rtld.
2929 if (execpath_len != 0) {
2930 destp -= execpath_len;
2931 imgp->execpathp = destp;
2932 copyout(imgp->execpath, (void *)destp, execpath_len);
2936 * Prepare the canary for SSP.
2938 arc4rand(canary, sizeof(canary), 0);
2939 destp -= sizeof(canary);
2940 imgp->canary = destp;
2941 copyout(canary, (void *)destp, sizeof(canary));
2942 imgp->canarylen = sizeof(canary);
2945 * Prepare the pagesizes array.
2947 for (i = 0; i < MAXPAGESIZES; i++)
2948 pagesizes32[i] = (uint32_t)pagesizes[i];
2949 destp -= sizeof(pagesizes32);
2950 destp = rounddown2(destp, sizeof(uint32_t));
2951 imgp->pagesizes = destp;
2952 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
2953 imgp->pagesizeslen = sizeof(pagesizes32);
2955 destp -= ARG_MAX - imgp->args->stringspace;
2956 destp = rounddown2(destp, sizeof(uint32_t));
2958 vectp = (uint32_t *)destp;
2959 if (imgp->auxargs) {
2961 * Allocate room on the stack for the ELF auxargs
2962 * array. It has up to AT_COUNT entries.
2964 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
2969 * Allocate room for the argv[] and env vectors including the
2970 * terminating NULL pointers.
2972 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
2975 * vectp also becomes our initial stack base
2979 stringp = imgp->args->begin_argv;
2980 argc = imgp->args->argc;
2981 envc = imgp->args->envc;
2983 * Copy out strings - arguments and environment.
2985 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
2988 * Fill in "ps_strings" struct for ps, w, etc.
2990 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
2991 suword32(&arginfo->ps_nargvstr, argc);
2994 * Fill in argument portion of vector table.
2996 for (; argc > 0; --argc) {
2997 suword32(vectp++, (u_int32_t)(intptr_t)destp);
2998 while (*stringp++ != 0)
3003 /* a null vector table pointer separates the argp's from the envp's */
3004 suword32(vectp++, 0);
3006 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3007 suword32(&arginfo->ps_nenvstr, envc);
3010 * Fill in environment portion of vector table.
3012 for (; envc > 0; --envc) {
3013 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3014 while (*stringp++ != 0)
3019 /* end of vector table is a null pointer */
3022 return ((register_t *)stack_base);
3026 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3028 struct kld_file_stat *stat;
3029 struct kld32_file_stat *stat32;
3032 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3035 if (version != sizeof(struct kld32_file_stat_1) &&
3036 version != sizeof(struct kld32_file_stat))
3039 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3040 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3041 error = kern_kldstat(td, uap->fileid, stat);
3043 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3044 CP(*stat, *stat32, refs);
3045 CP(*stat, *stat32, id);
3046 PTROUT_CP(*stat, *stat32, address);
3047 CP(*stat, *stat32, size);
3048 bcopy(&stat->pathname[0], &stat32->pathname[0],
3049 sizeof(stat->pathname));
3050 stat32->version = version;
3051 error = copyout(stat32, uap->stat, version);
3054 free(stat32, M_TEMP);
3059 freebsd32_posix_fallocate(struct thread *td,
3060 struct freebsd32_posix_fallocate_args *uap)
3064 error = kern_posix_fallocate(td, uap->fd,
3065 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3066 return (kern_posix_error(td, error));
3070 freebsd32_posix_fadvise(struct thread *td,
3071 struct freebsd32_posix_fadvise_args *uap)
3075 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3076 PAIR32TO64(off_t, uap->len), uap->advice);
3077 return (kern_posix_error(td, error));
3081 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3084 CP(*sig32, *sig, sigev_notify);
3085 switch (sig->sigev_notify) {
3088 case SIGEV_THREAD_ID:
3089 CP(*sig32, *sig, sigev_notify_thread_id);
3092 CP(*sig32, *sig, sigev_signo);
3093 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3096 CP(*sig32, *sig, sigev_notify_kqueue);
3097 CP(*sig32, *sig, sigev_notify_kevent_flags);
3098 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3107 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3111 struct procctl_reaper_status rs;
3112 struct procctl_reaper_pids rp;
3113 struct procctl_reaper_kill rk;
3116 struct procctl_reaper_pids32 rp;
3118 int error, error1, flags, signum;
3122 case PROC_TRACE_CTL:
3123 case PROC_TRAPCAP_CTL:
3124 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3129 case PROC_REAP_ACQUIRE:
3130 case PROC_REAP_RELEASE:
3131 if (uap->data != NULL)
3135 case PROC_REAP_STATUS:
3138 case PROC_REAP_GETPIDS:
3139 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3142 CP(x32.rp, x.rp, rp_count);
3143 PTRIN_CP(x32.rp, x.rp, rp_pids);
3146 case PROC_REAP_KILL:
3147 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3152 case PROC_TRACE_STATUS:
3153 case PROC_TRAPCAP_STATUS:
3156 case PROC_PDEATHSIG_CTL:
3157 error = copyin(uap->data, &signum, sizeof(signum));
3162 case PROC_PDEATHSIG_STATUS:
3168 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3171 case PROC_REAP_STATUS:
3173 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3175 case PROC_REAP_KILL:
3176 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3180 case PROC_TRACE_STATUS:
3181 case PROC_TRAPCAP_STATUS:
3183 error = copyout(&flags, uap->data, sizeof(flags));
3185 case PROC_PDEATHSIG_STATUS:
3187 error = copyout(&signum, uap->data, sizeof(signum));
3194 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3200 * Do unsigned conversion for arg when operation
3201 * interprets it as flags or pointer.
3203 case F_SETLK_REMOTE:
3212 tmp = (unsigned int)(uap->arg);
3218 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3222 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3224 struct timespec32 ts32;
3225 struct timespec ts, *tsp;
3229 if (uap->ts != NULL) {
3230 error = copyin(uap->ts, &ts32, sizeof(ts32));
3233 CP(ts32, ts, tv_sec);
3234 CP(ts32, ts, tv_nsec);
3238 if (uap->set != NULL) {
3239 error = copyin(uap->set, &set, sizeof(set));
3246 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3250 freebsd32_sched_rr_get_interval(struct thread *td,
3251 struct freebsd32_sched_rr_get_interval_args *uap)
3254 struct timespec32 ts32;
3257 error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3259 CP(ts, ts32, tv_sec);
3260 CP(ts, ts32, tv_nsec);
3261 error = copyout(&ts32, uap->interval, sizeof(ts32));