2 * Copyright (c) 2002 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_compat.h"
32 #include "opt_inet6.h"
34 #define __ELF_WORD_SIZE 32
36 #include <sys/param.h>
38 #include <sys/capsicum.h>
39 #include <sys/clock.h>
41 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/imgact.h>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
47 #include <sys/linker.h>
49 #include <sys/malloc.h>
50 #include <sys/file.h> /* Must come after sys/malloc.h */
51 #include <sys/imgact.h>
54 #include <sys/module.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/namei.h>
59 #include <sys/procctl.h>
60 #include <sys/reboot.h>
61 #include <sys/resource.h>
62 #include <sys/resourcevar.h>
63 #include <sys/selinfo.h>
64 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
65 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
66 #include <sys/signal.h>
67 #include <sys/signalvar.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
71 #include <sys/syscall.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
74 #include <sys/sysent.h>
75 #include <sys/sysproto.h>
76 #include <sys/systm.h>
78 #include <sys/unistd.h>
79 #include <sys/ucontext.h>
80 #include <sys/vnode.h>
88 #include <netinet/in.h>
92 #include <vm/vm_param.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_extern.h>
98 #include <machine/cpu.h>
99 #include <machine/elf.h>
101 #include <security/audit/audit.h>
103 #include <compat/freebsd32/freebsd32_util.h>
104 #include <compat/freebsd32/freebsd32.h>
105 #include <compat/freebsd32/freebsd32_ipc.h>
106 #include <compat/freebsd32/freebsd32_misc.h>
107 #include <compat/freebsd32/freebsd32_signal.h>
108 #include <compat/freebsd32/freebsd32_proto.h>
110 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
113 CTASSERT(sizeof(struct timeval32) == 8);
114 CTASSERT(sizeof(struct timespec32) == 8);
115 CTASSERT(sizeof(struct itimerval32) == 16);
117 CTASSERT(sizeof(struct statfs32) == 256);
119 CTASSERT(sizeof(struct rusage32) == 72);
121 CTASSERT(sizeof(struct sigaltstack32) == 12);
123 CTASSERT(sizeof(struct kevent32) == 56);
125 CTASSERT(sizeof(struct kevent32) == 64);
127 CTASSERT(sizeof(struct iovec32) == 8);
128 CTASSERT(sizeof(struct msghdr32) == 28);
130 CTASSERT(sizeof(struct stat32) == 208);
131 CTASSERT(sizeof(struct freebsd11_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);
456 #if defined(COMPAT_FREEBSD11)
458 freebsd11_freebsd32_mknod(struct thread *td,
459 struct freebsd11_freebsd32_mknod_args *uap)
462 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->mode,
467 freebsd11_freebsd32_mknodat(struct thread *td,
468 struct freebsd11_freebsd32_mknodat_args *uap)
471 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
474 #endif /* COMPAT_FREEBSD11 */
477 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
482 #if defined(__amd64__)
483 if (i386_read_exec && (prot & PROT_READ) != 0)
486 return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
491 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
496 #if defined(__amd64__)
497 if (i386_read_exec && (prot & PROT_READ))
501 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
502 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
505 #ifdef COMPAT_FREEBSD6
507 freebsd6_freebsd32_mmap(struct thread *td,
508 struct freebsd6_freebsd32_mmap_args *uap)
513 #if defined(__amd64__)
514 if (i386_read_exec && (prot & PROT_READ))
518 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
519 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
524 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
526 struct itimerval itv, oitv, *itvp;
527 struct itimerval32 i32;
530 if (uap->itv != NULL) {
531 error = copyin(uap->itv, &i32, sizeof(i32));
534 TV_CP(i32, itv, it_interval);
535 TV_CP(i32, itv, it_value);
539 error = kern_setitimer(td, uap->which, itvp, &oitv);
540 if (error || uap->oitv == NULL)
542 TV_CP(oitv, i32, it_interval);
543 TV_CP(oitv, i32, it_value);
544 return (copyout(&i32, uap->oitv, sizeof(i32)));
548 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
550 struct itimerval itv;
551 struct itimerval32 i32;
554 error = kern_getitimer(td, uap->which, &itv);
555 if (error || uap->itv == NULL)
557 TV_CP(itv, i32, it_interval);
558 TV_CP(itv, i32, it_value);
559 return (copyout(&i32, uap->itv, sizeof(i32)));
563 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
565 struct timeval32 tv32;
566 struct timeval tv, *tvp;
569 if (uap->tv != NULL) {
570 error = copyin(uap->tv, &tv32, sizeof(tv32));
573 CP(tv32, tv, tv_sec);
574 CP(tv32, tv, tv_usec);
579 * XXX Do pointers need PTRIN()?
581 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
582 sizeof(int32_t) * 8));
586 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
588 struct timespec32 ts32;
590 struct timeval tv, *tvp;
594 if (uap->ts != NULL) {
595 error = copyin(uap->ts, &ts32, sizeof(ts32));
598 CP(ts32, ts, tv_sec);
599 CP(ts32, ts, tv_nsec);
600 TIMESPEC_TO_TIMEVAL(&tv, &ts);
604 if (uap->sm != NULL) {
605 error = copyin(uap->sm, &set, sizeof(set));
612 * XXX Do pointers need PTRIN()?
614 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
615 uset, sizeof(int32_t) * 8);
620 * Copy 'count' items into the destination list pointed to by uap->eventlist.
623 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
625 struct freebsd32_kevent_args *uap;
626 struct kevent32 ks32[KQ_NEVENTS];
630 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
631 uap = (struct freebsd32_kevent_args *)arg;
633 for (i = 0; i < count; i++) {
634 CP(kevp[i], ks32[i], ident);
635 CP(kevp[i], ks32[i], filter);
636 CP(kevp[i], ks32[i], flags);
637 CP(kevp[i], ks32[i], fflags);
638 #if BYTE_ORDER == LITTLE_ENDIAN
639 ks32[i].data1 = kevp[i].data;
640 ks32[i].data2 = kevp[i].data >> 32;
642 ks32[i].data1 = kevp[i].data >> 32;
643 ks32[i].data2 = kevp[i].data;
645 PTROUT_CP(kevp[i], ks32[i], udata);
646 for (j = 0; j < nitems(kevp->ext); j++) {
648 #if BYTE_ORDER == LITTLE_ENDIAN
649 ks32[i].ext64[2 * j] = e;
650 ks32[i].ext64[2 * j + 1] = e >> 32;
652 ks32[i].ext64[2 * j] = e >> 32;
653 ks32[i].ext64[2 * j + 1] = e;
657 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
659 uap->eventlist += count;
664 * Copy 'count' items from the list pointed to by uap->changelist.
667 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
669 struct freebsd32_kevent_args *uap;
670 struct kevent32 ks32[KQ_NEVENTS];
674 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
675 uap = (struct freebsd32_kevent_args *)arg;
677 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
680 uap->changelist += count;
682 for (i = 0; i < count; i++) {
683 CP(ks32[i], kevp[i], ident);
684 CP(ks32[i], kevp[i], filter);
685 CP(ks32[i], kevp[i], flags);
686 CP(ks32[i], kevp[i], fflags);
687 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
688 PTRIN_CP(ks32[i], kevp[i], udata);
689 for (j = 0; j < nitems(kevp->ext); j++) {
690 #if BYTE_ORDER == LITTLE_ENDIAN
691 e = ks32[i].ext64[2 * j + 1];
693 e += ks32[i].ext64[2 * j];
695 e = ks32[i].ext64[2 * j];
697 e += ks32[i].ext64[2 * j + 1];
707 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
709 struct timespec32 ts32;
710 struct timespec ts, *tsp;
711 struct kevent_copyops k_ops = {
713 .k_copyout = freebsd32_kevent_copyout,
714 .k_copyin = freebsd32_kevent_copyin,
719 error = copyin(uap->timeout, &ts32, sizeof(ts32));
722 CP(ts32, ts, tv_sec);
723 CP(ts32, ts, tv_nsec);
727 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
732 #ifdef COMPAT_FREEBSD11
733 struct kevent32_freebsd11 {
734 u_int32_t ident; /* identifier for this event */
735 short filter; /* filter for event */
739 u_int32_t udata; /* opaque user data identifier */
743 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
745 struct freebsd11_freebsd32_kevent_args *uap;
746 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
749 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
750 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
752 for (i = 0; i < count; i++) {
753 CP(kevp[i], ks32[i], ident);
754 CP(kevp[i], ks32[i], filter);
755 CP(kevp[i], ks32[i], flags);
756 CP(kevp[i], ks32[i], fflags);
757 CP(kevp[i], ks32[i], data);
758 PTROUT_CP(kevp[i], ks32[i], udata);
760 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
762 uap->eventlist += count;
767 * Copy 'count' items from the list pointed to by uap->changelist.
770 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
772 struct freebsd11_freebsd32_kevent_args *uap;
773 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
776 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
777 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
779 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
782 uap->changelist += count;
784 for (i = 0; i < count; i++) {
785 CP(ks32[i], kevp[i], ident);
786 CP(ks32[i], kevp[i], filter);
787 CP(ks32[i], kevp[i], flags);
788 CP(ks32[i], kevp[i], fflags);
789 CP(ks32[i], kevp[i], data);
790 PTRIN_CP(ks32[i], kevp[i], udata);
791 for (j = 0; j < nitems(kevp->ext); j++)
799 freebsd11_freebsd32_kevent(struct thread *td,
800 struct freebsd11_freebsd32_kevent_args *uap)
802 struct timespec32 ts32;
803 struct timespec ts, *tsp;
804 struct kevent_copyops k_ops = {
806 .k_copyout = freebsd32_kevent11_copyout,
807 .k_copyin = freebsd32_kevent11_copyin,
812 error = copyin(uap->timeout, &ts32, sizeof(ts32));
815 CP(ts32, ts, tv_sec);
816 CP(ts32, ts, tv_nsec);
820 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
827 freebsd32_gettimeofday(struct thread *td,
828 struct freebsd32_gettimeofday_args *uap)
831 struct timeval32 atv32;
837 CP(atv, atv32, tv_sec);
838 CP(atv, atv32, tv_usec);
839 error = copyout(&atv32, uap->tp, sizeof (atv32));
841 if (error == 0 && uap->tzp != NULL) {
842 rtz.tz_minuteswest = tz_minuteswest;
843 rtz.tz_dsttime = tz_dsttime;
844 error = copyout(&rtz, uap->tzp, sizeof (rtz));
850 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
856 error = kern_getrusage(td, uap->who, &s);
859 if (uap->rusage != NULL) {
860 freebsd32_rusage_out(&s, &s32);
861 error = copyout(&s32, uap->rusage, sizeof(s32));
867 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
869 struct iovec32 iov32;
876 if (iovcnt > UIO_MAXIOV)
878 iovlen = iovcnt * sizeof(struct iovec);
879 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
880 iov = (struct iovec *)(uio + 1);
881 for (i = 0; i < iovcnt; i++) {
882 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
887 iov[i].iov_base = PTRIN(iov32.iov_base);
888 iov[i].iov_len = iov32.iov_len;
891 uio->uio_iovcnt = iovcnt;
892 uio->uio_segflg = UIO_USERSPACE;
893 uio->uio_offset = -1;
895 for (i = 0; i < iovcnt; i++) {
896 if (iov->iov_len > INT_MAX - uio->uio_resid) {
900 uio->uio_resid += iov->iov_len;
908 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
913 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
916 error = kern_readv(td, uap->fd, auio);
922 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
927 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
930 error = kern_writev(td, uap->fd, auio);
936 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
941 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
944 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
950 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
955 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
958 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
964 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
967 struct iovec32 iov32;
973 if (iovcnt > UIO_MAXIOV)
975 iovlen = iovcnt * sizeof(struct iovec);
976 iov = malloc(iovlen, M_IOV, M_WAITOK);
977 for (i = 0; i < iovcnt; i++) {
978 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
983 iov[i].iov_base = PTRIN(iov32.iov_base);
984 iov[i].iov_len = iov32.iov_len;
991 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
996 error = copyin(msg32, &m32, sizeof(m32));
999 msg->msg_name = PTRIN(m32.msg_name);
1000 msg->msg_namelen = m32.msg_namelen;
1001 msg->msg_iov = PTRIN(m32.msg_iov);
1002 msg->msg_iovlen = m32.msg_iovlen;
1003 msg->msg_control = PTRIN(m32.msg_control);
1004 msg->msg_controllen = m32.msg_controllen;
1005 msg->msg_flags = m32.msg_flags;
1010 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1012 struct msghdr32 m32;
1015 m32.msg_name = PTROUT(msg->msg_name);
1016 m32.msg_namelen = msg->msg_namelen;
1017 m32.msg_iov = PTROUT(msg->msg_iov);
1018 m32.msg_iovlen = msg->msg_iovlen;
1019 m32.msg_control = PTROUT(msg->msg_control);
1020 m32.msg_controllen = msg->msg_controllen;
1021 m32.msg_flags = msg->msg_flags;
1022 error = copyout(&m32, msg32, sizeof(m32));
1027 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
1029 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
1031 #define FREEBSD32_ALIGN(p) \
1032 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1033 #define FREEBSD32_CMSG_SPACE(l) \
1034 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1036 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
1037 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1039 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1043 socklen_t clen, datalen;
1046 int len, maxlen, copylen;
1050 len = msg->msg_controllen;
1051 maxlen = msg->msg_controllen;
1052 msg->msg_controllen = 0;
1055 ctlbuf = msg->msg_control;
1057 while (m && len > 0) {
1058 cm = mtod(m, struct cmsghdr *);
1061 while (cm != NULL) {
1063 if (sizeof(struct cmsghdr) > clen ||
1064 cm->cmsg_len > clen) {
1069 data = CMSG_DATA(cm);
1070 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1072 /* Adjust message length */
1073 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1078 copylen = sizeof(struct cmsghdr);
1079 if (len < copylen) {
1080 msg->msg_flags |= MSG_CTRUNC;
1084 error = copyout(cm,ctlbuf,copylen);
1088 ctlbuf += FREEBSD32_ALIGN(copylen);
1089 len -= FREEBSD32_ALIGN(copylen);
1096 if (len < copylen) {
1097 msg->msg_flags |= MSG_CTRUNC;
1101 error = copyout(data,ctlbuf,copylen);
1105 ctlbuf += FREEBSD32_ALIGN(copylen);
1106 len -= FREEBSD32_ALIGN(copylen);
1108 if (CMSG_SPACE(datalen) < clen) {
1109 clen -= CMSG_SPACE(datalen);
1110 cm = (struct cmsghdr *)
1111 ((caddr_t)cm + CMSG_SPACE(datalen));
1120 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1128 freebsd32_recvmsg(td, uap)
1130 struct freebsd32_recvmsg_args /* {
1132 struct msghdr32 *msg;
1137 struct msghdr32 m32;
1138 struct iovec *uiov, *iov;
1139 struct mbuf *control = NULL;
1140 struct mbuf **controlp;
1143 error = copyin(uap->msg, &m32, sizeof(m32));
1146 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1149 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1153 msg.msg_flags = uap->flags;
1157 controlp = (msg.msg_control != NULL) ? &control : NULL;
1158 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1162 if (control != NULL)
1163 error = freebsd32_copy_msg_out(&msg, control);
1165 msg.msg_controllen = 0;
1168 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1172 if (control != NULL)
1179 * Copy-in the array of control messages constructed using alignment
1180 * and padding suitable for a 32-bit environment and construct an
1181 * mbuf using alignment and padding suitable for a 64-bit kernel.
1182 * The alignment and padding are defined indirectly by CMSG_DATA(),
1183 * CMSG_SPACE() and CMSG_LEN().
1186 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1190 u_int idx, len, msglen;
1193 buflen = FREEBSD32_ALIGN(buflen);
1195 if (buflen > MCLBYTES)
1199 * Iterate over the buffer and get the length of each message
1200 * in there. This has 32-bit alignment and padding. Use it to
1201 * determine the length of these messages when using 64-bit
1202 * alignment and padding.
1206 while (idx < buflen) {
1207 error = copyin(buf + idx, &msglen, sizeof(msglen));
1210 if (msglen < sizeof(struct cmsghdr))
1212 msglen = FREEBSD32_ALIGN(msglen);
1213 if (idx + msglen > buflen)
1216 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1217 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1218 len += CMSG_ALIGN(msglen);
1224 m = m_get(M_WAITOK, MT_CONTROL);
1226 MCLGET(m, M_WAITOK);
1229 md = mtod(m, void *);
1230 while (buflen > 0) {
1231 error = copyin(buf, md, sizeof(struct cmsghdr));
1234 msglen = *(u_int *)md;
1235 msglen = FREEBSD32_ALIGN(msglen);
1237 /* Modify the message length to account for alignment. */
1238 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1239 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1241 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1242 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1243 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1245 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1247 error = copyin(buf, md, msglen);
1250 md = (char *)md + CMSG_ALIGN(msglen);
1264 freebsd32_sendmsg(struct thread *td,
1265 struct freebsd32_sendmsg_args *uap)
1268 struct msghdr32 m32;
1270 struct mbuf *control = NULL;
1271 struct sockaddr *to = NULL;
1274 error = copyin(uap->msg, &m32, sizeof(m32));
1277 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1280 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1285 if (msg.msg_name != NULL) {
1286 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1294 if (msg.msg_control) {
1295 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1300 error = freebsd32_copyin_control(&control, msg.msg_control,
1301 msg.msg_controllen);
1305 msg.msg_control = NULL;
1306 msg.msg_controllen = 0;
1309 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1320 freebsd32_recvfrom(struct thread *td,
1321 struct freebsd32_recvfrom_args *uap)
1327 if (uap->fromlenaddr) {
1328 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1329 sizeof(msg.msg_namelen));
1333 msg.msg_namelen = 0;
1336 msg.msg_name = PTRIN(uap->from);
1337 msg.msg_iov = &aiov;
1339 aiov.iov_base = PTRIN(uap->buf);
1340 aiov.iov_len = uap->len;
1341 msg.msg_control = NULL;
1342 msg.msg_flags = uap->flags;
1343 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1344 if (error == 0 && uap->fromlenaddr)
1345 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1346 sizeof (msg.msg_namelen));
1351 freebsd32_settimeofday(struct thread *td,
1352 struct freebsd32_settimeofday_args *uap)
1354 struct timeval32 tv32;
1355 struct timeval tv, *tvp;
1356 struct timezone tz, *tzp;
1360 error = copyin(uap->tv, &tv32, sizeof(tv32));
1363 CP(tv32, tv, tv_sec);
1364 CP(tv32, tv, tv_usec);
1369 error = copyin(uap->tzp, &tz, sizeof(tz));
1375 return (kern_settimeofday(td, tvp, tzp));
1379 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1381 struct timeval32 s32[2];
1382 struct timeval s[2], *sp;
1385 if (uap->tptr != NULL) {
1386 error = copyin(uap->tptr, s32, sizeof(s32));
1389 CP(s32[0], s[0], tv_sec);
1390 CP(s32[0], s[0], tv_usec);
1391 CP(s32[1], s[1], tv_sec);
1392 CP(s32[1], s[1], tv_usec);
1396 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1401 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1403 struct timeval32 s32[2];
1404 struct timeval s[2], *sp;
1407 if (uap->tptr != NULL) {
1408 error = copyin(uap->tptr, s32, sizeof(s32));
1411 CP(s32[0], s[0], tv_sec);
1412 CP(s32[0], s[0], tv_usec);
1413 CP(s32[1], s[1], tv_sec);
1414 CP(s32[1], s[1], tv_usec);
1418 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1422 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1424 struct timeval32 s32[2];
1425 struct timeval s[2], *sp;
1428 if (uap->tptr != NULL) {
1429 error = copyin(uap->tptr, s32, sizeof(s32));
1432 CP(s32[0], s[0], tv_sec);
1433 CP(s32[0], s[0], tv_usec);
1434 CP(s32[1], s[1], tv_sec);
1435 CP(s32[1], s[1], tv_usec);
1439 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1443 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1445 struct timeval32 s32[2];
1446 struct timeval s[2], *sp;
1449 if (uap->times != NULL) {
1450 error = copyin(uap->times, s32, sizeof(s32));
1453 CP(s32[0], s[0], tv_sec);
1454 CP(s32[0], s[0], tv_usec);
1455 CP(s32[1], s[1], tv_sec);
1456 CP(s32[1], s[1], tv_usec);
1460 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1465 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1467 struct timespec32 ts32[2];
1468 struct timespec ts[2], *tsp;
1471 if (uap->times != NULL) {
1472 error = copyin(uap->times, ts32, sizeof(ts32));
1475 CP(ts32[0], ts[0], tv_sec);
1476 CP(ts32[0], ts[0], tv_nsec);
1477 CP(ts32[1], ts[1], tv_sec);
1478 CP(ts32[1], ts[1], tv_nsec);
1482 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1486 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1488 struct timespec32 ts32[2];
1489 struct timespec ts[2], *tsp;
1492 if (uap->times != NULL) {
1493 error = copyin(uap->times, ts32, sizeof(ts32));
1496 CP(ts32[0], ts[0], tv_sec);
1497 CP(ts32[0], ts[0], tv_nsec);
1498 CP(ts32[1], ts[1], tv_sec);
1499 CP(ts32[1], ts[1], tv_nsec);
1503 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1504 tsp, UIO_SYSSPACE, uap->flag));
1508 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1510 struct timeval32 tv32;
1511 struct timeval delta, olddelta, *deltap;
1515 error = copyin(uap->delta, &tv32, sizeof(tv32));
1518 CP(tv32, delta, tv_sec);
1519 CP(tv32, delta, tv_usec);
1523 error = kern_adjtime(td, deltap, &olddelta);
1524 if (uap->olddelta && error == 0) {
1525 CP(olddelta, tv32, tv_sec);
1526 CP(olddelta, tv32, tv_usec);
1527 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1532 #ifdef COMPAT_FREEBSD4
1534 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1536 struct statfs32 s32;
1540 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1541 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1543 copy_statfs(sp, &s32);
1544 error = copyout(&s32, uap->buf, sizeof(s32));
1551 #ifdef COMPAT_FREEBSD4
1553 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1555 struct statfs32 s32;
1559 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1560 error = kern_fstatfs(td, uap->fd, sp);
1562 copy_statfs(sp, &s32);
1563 error = copyout(&s32, uap->buf, sizeof(s32));
1570 #ifdef COMPAT_FREEBSD4
1572 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1574 struct statfs32 s32;
1579 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1581 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1582 error = kern_fhstatfs(td, fh, sp);
1584 copy_statfs(sp, &s32);
1585 error = copyout(&s32, uap->buf, sizeof(s32));
1593 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1596 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1597 PAIR32TO64(off_t, uap->offset)));
1601 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1604 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1605 PAIR32TO64(off_t, uap->offset)));
1610 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1613 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1618 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1623 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1625 /* Expand the quad return into two parts for eax and edx */
1626 pos = td->td_uretoff.tdu_off;
1627 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1628 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1633 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1636 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1637 PAIR32TO64(off_t, uap->length)));
1641 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1644 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1649 ofreebsd32_getdirentries(struct thread *td,
1650 struct ofreebsd32_getdirentries_args *uap)
1652 struct ogetdirentries_args ap;
1659 ap.count = uap->count;
1661 error = kern_ogetdirentries(td, &ap, &loff);
1664 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1670 #if defined(COMPAT_FREEBSD11)
1672 freebsd11_freebsd32_getdirentries(struct thread *td,
1673 struct freebsd11_freebsd32_getdirentries_args *uap)
1679 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1683 if (uap->basep != NULL) {
1685 error = copyout(&base32, uap->basep, sizeof(int32_t));
1691 freebsd11_freebsd32_getdents(struct thread *td,
1692 struct freebsd11_freebsd32_getdents_args *uap)
1694 struct freebsd11_freebsd32_getdirentries_args ap;
1698 ap.count = uap->count;
1700 return (freebsd11_freebsd32_getdirentries(td, &ap));
1702 #endif /* COMPAT_FREEBSD11 */
1705 freebsd32_getdirentries(struct thread *td,
1706 struct freebsd32_getdirentries_args *uap)
1712 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1713 NULL, UIO_USERSPACE);
1716 if (uap->basep != NULL) {
1718 error = copyout(&base32, uap->basep, sizeof(int32_t));
1723 #ifdef COMPAT_FREEBSD6
1724 /* versions with the 'int pad' argument */
1726 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1729 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1730 PAIR32TO64(off_t, uap->offset)));
1734 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1737 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1738 PAIR32TO64(off_t, uap->offset)));
1742 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1747 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1749 /* Expand the quad return into two parts for eax and edx */
1750 pos = *(off_t *)(td->td_retval);
1751 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1752 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1757 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1760 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1761 PAIR32TO64(off_t, uap->length)));
1765 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1768 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1770 #endif /* COMPAT_FREEBSD6 */
1780 freebsd32_do_sendfile(struct thread *td,
1781 struct freebsd32_sendfile_args *uap, int compat)
1783 struct sf_hdtr32 hdtr32;
1784 struct sf_hdtr hdtr;
1785 struct uio *hdr_uio, *trl_uio;
1787 cap_rights_t rights;
1788 struct iovec32 *iov32;
1789 off_t offset, sbytes;
1792 offset = PAIR32TO64(off_t, uap->offset);
1796 hdr_uio = trl_uio = NULL;
1798 if (uap->hdtr != NULL) {
1799 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1802 PTRIN_CP(hdtr32, hdtr, headers);
1803 CP(hdtr32, hdtr, hdr_cnt);
1804 PTRIN_CP(hdtr32, hdtr, trailers);
1805 CP(hdtr32, hdtr, trl_cnt);
1807 if (hdtr.headers != NULL) {
1808 iov32 = PTRIN(hdtr32.headers);
1809 error = freebsd32_copyinuio(iov32,
1810 hdtr32.hdr_cnt, &hdr_uio);
1813 #ifdef COMPAT_FREEBSD4
1815 * In FreeBSD < 5.0 the nbytes to send also included
1816 * the header. If compat is specified subtract the
1817 * header size from nbytes.
1820 if (uap->nbytes > hdr_uio->uio_resid)
1821 uap->nbytes -= hdr_uio->uio_resid;
1827 if (hdtr.trailers != NULL) {
1828 iov32 = PTRIN(hdtr32.trailers);
1829 error = freebsd32_copyinuio(iov32,
1830 hdtr32.trl_cnt, &trl_uio);
1836 AUDIT_ARG_FD(uap->fd);
1838 if ((error = fget_read(td, uap->fd,
1839 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1842 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1843 uap->nbytes, &sbytes, uap->flags, td);
1846 if (uap->sbytes != NULL)
1847 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1851 free(hdr_uio, M_IOV);
1853 free(trl_uio, M_IOV);
1857 #ifdef COMPAT_FREEBSD4
1859 freebsd4_freebsd32_sendfile(struct thread *td,
1860 struct freebsd4_freebsd32_sendfile_args *uap)
1862 return (freebsd32_do_sendfile(td,
1863 (struct freebsd32_sendfile_args *)uap, 1));
1868 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1871 return (freebsd32_do_sendfile(td, uap, 0));
1875 copy_stat(struct stat *in, struct stat32 *out)
1878 CP(*in, *out, st_dev);
1879 CP(*in, *out, st_ino);
1880 CP(*in, *out, st_mode);
1881 CP(*in, *out, st_nlink);
1882 CP(*in, *out, st_uid);
1883 CP(*in, *out, st_gid);
1884 CP(*in, *out, st_rdev);
1885 TS_CP(*in, *out, st_atim);
1886 TS_CP(*in, *out, st_mtim);
1887 TS_CP(*in, *out, st_ctim);
1888 CP(*in, *out, st_size);
1889 CP(*in, *out, st_blocks);
1890 CP(*in, *out, st_blksize);
1891 CP(*in, *out, st_flags);
1892 CP(*in, *out, st_gen);
1893 TS_CP(*in, *out, st_birthtim);
1894 out->st_padding0 = 0;
1895 out->st_padding1 = 0;
1896 #ifdef __STAT32_TIME_T_EXT
1897 out->st_atim_ext = 0;
1898 out->st_mtim_ext = 0;
1899 out->st_ctim_ext = 0;
1900 out->st_btim_ext = 0;
1902 bzero(out->st_spare, sizeof(out->st_spare));
1907 copy_ostat(struct stat *in, struct ostat32 *out)
1910 CP(*in, *out, st_dev);
1911 CP(*in, *out, st_ino);
1912 CP(*in, *out, st_mode);
1913 CP(*in, *out, st_nlink);
1914 CP(*in, *out, st_uid);
1915 CP(*in, *out, st_gid);
1916 CP(*in, *out, st_rdev);
1917 CP(*in, *out, st_size);
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_blksize);
1922 CP(*in, *out, st_blocks);
1923 CP(*in, *out, st_flags);
1924 CP(*in, *out, st_gen);
1930 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1933 struct ostat32 sb32;
1936 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
1940 copy_ostat(&sb, &sb32);
1941 error = copyout(&sb32, uap->ub, sizeof (sb32));
1947 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
1953 error = kern_fstat(td, uap->fd, &ub);
1956 copy_stat(&ub, &ub32);
1957 error = copyout(&ub32, uap->ub, sizeof(ub32));
1963 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
1966 struct ostat32 ub32;
1969 error = kern_fstat(td, uap->fd, &ub);
1972 copy_ostat(&ub, &ub32);
1973 error = copyout(&ub32, uap->ub, sizeof(ub32));
1979 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
1985 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
1989 copy_stat(&ub, &ub32);
1990 error = copyout(&ub32, uap->buf, sizeof(ub32));
1996 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
1999 struct ostat32 sb32;
2002 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2003 UIO_USERSPACE, &sb, NULL);
2006 copy_ostat(&sb, &sb32);
2007 error = copyout(&sb32, uap->ub, sizeof (sb32));
2013 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2020 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2023 error = kern_fhstat(td, fh, &sb);
2026 copy_stat(&sb, &sb32);
2027 error = copyout(&sb32, uap->sb, sizeof (sb32));
2031 #if defined(COMPAT_FREEBSD11)
2032 extern int ino64_trunc_error;
2035 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2038 CP(*in, *out, st_ino);
2039 if (in->st_ino != out->st_ino) {
2040 switch (ino64_trunc_error) {
2047 out->st_ino = UINT32_MAX;
2051 CP(*in, *out, st_nlink);
2052 if (in->st_nlink != out->st_nlink) {
2053 switch (ino64_trunc_error) {
2060 out->st_nlink = UINT16_MAX;
2064 CP(*in, *out, st_dev);
2065 CP(*in, *out, st_mode);
2066 CP(*in, *out, st_uid);
2067 CP(*in, *out, st_gid);
2068 CP(*in, *out, st_rdev);
2069 TS_CP(*in, *out, st_atim);
2070 TS_CP(*in, *out, st_mtim);
2071 TS_CP(*in, *out, st_ctim);
2072 CP(*in, *out, st_size);
2073 CP(*in, *out, st_blocks);
2074 CP(*in, *out, st_blksize);
2075 CP(*in, *out, st_flags);
2076 CP(*in, *out, st_gen);
2077 TS_CP(*in, *out, st_birthtim);
2079 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2080 sizeof(*out) - offsetof(struct freebsd11_stat32,
2081 st_birthtim) - sizeof(out->st_birthtim));
2086 freebsd11_freebsd32_stat(struct thread *td,
2087 struct freebsd11_freebsd32_stat_args *uap)
2090 struct freebsd11_stat32 sb32;
2093 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2097 error = freebsd11_cvtstat32(&sb, &sb32);
2099 error = copyout(&sb32, uap->ub, sizeof (sb32));
2104 freebsd11_freebsd32_fstat(struct thread *td,
2105 struct freebsd11_freebsd32_fstat_args *uap)
2108 struct freebsd11_stat32 sb32;
2111 error = kern_fstat(td, uap->fd, &sb);
2114 error = freebsd11_cvtstat32(&sb, &sb32);
2116 error = copyout(&sb32, uap->ub, sizeof (sb32));
2121 freebsd11_freebsd32_fstatat(struct thread *td,
2122 struct freebsd11_freebsd32_fstatat_args *uap)
2125 struct freebsd11_stat32 sb32;
2128 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2132 error = freebsd11_cvtstat32(&sb, &sb32);
2134 error = copyout(&sb32, uap->buf, sizeof (sb32));
2139 freebsd11_freebsd32_lstat(struct thread *td,
2140 struct freebsd11_freebsd32_lstat_args *uap)
2143 struct freebsd11_stat32 sb32;
2146 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2147 UIO_USERSPACE, &sb, NULL);
2150 error = freebsd11_cvtstat32(&sb, &sb32);
2152 error = copyout(&sb32, uap->ub, sizeof (sb32));
2157 freebsd11_freebsd32_fhstat(struct thread *td,
2158 struct freebsd11_freebsd32_fhstat_args *uap)
2161 struct freebsd11_stat32 sb32;
2165 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2168 error = kern_fhstat(td, fh, &sb);
2171 error = freebsd11_cvtstat32(&sb, &sb32);
2173 error = copyout(&sb32, uap->sb, sizeof (sb32));
2179 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2181 int error, name[CTL_MAXNAME];
2185 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2187 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2191 error = fueword32(uap->oldlenp, &tmp);
2198 error = userland_sysctl(td, name, uap->namelen,
2199 uap->old, &oldlen, 1,
2200 uap->new, uap->newlen, &j, SCTL_MASK32);
2201 if (error && error != ENOMEM)
2204 suword32(uap->oldlenp, j);
2209 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2215 error = copyin(uap->jail, &version, sizeof(uint32_t));
2222 /* FreeBSD single IPv4 jails. */
2223 struct jail32_v0 j32_v0;
2225 bzero(&j, sizeof(struct jail));
2226 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2229 CP(j32_v0, j, version);
2230 PTRIN_CP(j32_v0, j, path);
2231 PTRIN_CP(j32_v0, j, hostname);
2232 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2238 * Version 1 was used by multi-IPv4 jail implementations
2239 * that never made it into the official kernel.
2243 case 2: /* JAIL_API_VERSION */
2245 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2248 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2251 CP(j32, j, version);
2252 PTRIN_CP(j32, j, path);
2253 PTRIN_CP(j32, j, hostname);
2254 PTRIN_CP(j32, j, jailname);
2257 PTRIN_CP(j32, j, ip4);
2258 PTRIN_CP(j32, j, ip6);
2263 /* Sci-Fi jails are not supported, sorry. */
2266 return (kern_jail(td, &j));
2270 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2275 /* Check that we have an even number of iovecs. */
2276 if (uap->iovcnt & 1)
2279 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2282 error = kern_jail_set(td, auio, uap->flags);
2288 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2290 struct iovec32 iov32;
2294 /* Check that we have an even number of iovecs. */
2295 if (uap->iovcnt & 1)
2298 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2301 error = kern_jail_get(td, auio, uap->flags);
2303 for (i = 0; i < uap->iovcnt; i++) {
2304 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2305 CP(auio->uio_iov[i], iov32, iov_len);
2306 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2315 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2317 struct sigaction32 s32;
2318 struct sigaction sa, osa, *sap;
2322 error = copyin(uap->act, &s32, sizeof(s32));
2325 sa.sa_handler = PTRIN(s32.sa_u);
2326 CP(s32, sa, sa_flags);
2327 CP(s32, sa, sa_mask);
2331 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2332 if (error == 0 && uap->oact != NULL) {
2333 s32.sa_u = PTROUT(osa.sa_handler);
2334 CP(osa, s32, sa_flags);
2335 CP(osa, s32, sa_mask);
2336 error = copyout(&s32, uap->oact, sizeof(s32));
2341 #ifdef COMPAT_FREEBSD4
2343 freebsd4_freebsd32_sigaction(struct thread *td,
2344 struct freebsd4_freebsd32_sigaction_args *uap)
2346 struct sigaction32 s32;
2347 struct sigaction sa, osa, *sap;
2351 error = copyin(uap->act, &s32, sizeof(s32));
2354 sa.sa_handler = PTRIN(s32.sa_u);
2355 CP(s32, sa, sa_flags);
2356 CP(s32, sa, sa_mask);
2360 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2361 if (error == 0 && uap->oact != NULL) {
2362 s32.sa_u = PTROUT(osa.sa_handler);
2363 CP(osa, s32, sa_flags);
2364 CP(osa, s32, sa_mask);
2365 error = copyout(&s32, uap->oact, sizeof(s32));
2372 struct osigaction32 {
2381 ofreebsd32_sigaction(struct thread *td,
2382 struct ofreebsd32_sigaction_args *uap)
2384 struct osigaction32 s32;
2385 struct sigaction sa, osa, *sap;
2388 if (uap->signum <= 0 || uap->signum >= ONSIG)
2392 error = copyin(uap->nsa, &s32, sizeof(s32));
2395 sa.sa_handler = PTRIN(s32.sa_u);
2396 CP(s32, sa, sa_flags);
2397 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2401 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2402 if (error == 0 && uap->osa != NULL) {
2403 s32.sa_u = PTROUT(osa.sa_handler);
2404 CP(osa, s32, sa_flags);
2405 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2406 error = copyout(&s32, uap->osa, sizeof(s32));
2412 ofreebsd32_sigprocmask(struct thread *td,
2413 struct ofreebsd32_sigprocmask_args *uap)
2418 OSIG2SIG(uap->mask, set);
2419 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2420 SIG2OSIG(oset, td->td_retval[0]);
2425 ofreebsd32_sigpending(struct thread *td,
2426 struct ofreebsd32_sigpending_args *uap)
2428 struct proc *p = td->td_proc;
2432 siglist = p->p_siglist;
2433 SIGSETOR(siglist, td->td_siglist);
2435 SIG2OSIG(siglist, td->td_retval[0]);
2440 u_int32_t sv_handler;
2446 ofreebsd32_sigvec(struct thread *td,
2447 struct ofreebsd32_sigvec_args *uap)
2449 struct sigvec32 vec;
2450 struct sigaction sa, osa, *sap;
2453 if (uap->signum <= 0 || uap->signum >= ONSIG)
2457 error = copyin(uap->nsv, &vec, sizeof(vec));
2460 sa.sa_handler = PTRIN(vec.sv_handler);
2461 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2462 sa.sa_flags = vec.sv_flags;
2463 sa.sa_flags ^= SA_RESTART;
2467 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2468 if (error == 0 && uap->osv != NULL) {
2469 vec.sv_handler = PTROUT(osa.sa_handler);
2470 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2471 vec.sv_flags = osa.sa_flags;
2472 vec.sv_flags &= ~SA_NOCLDWAIT;
2473 vec.sv_flags ^= SA_RESTART;
2474 error = copyout(&vec, uap->osv, sizeof(vec));
2480 ofreebsd32_sigblock(struct thread *td,
2481 struct ofreebsd32_sigblock_args *uap)
2485 OSIG2SIG(uap->mask, set);
2486 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2487 SIG2OSIG(oset, td->td_retval[0]);
2492 ofreebsd32_sigsetmask(struct thread *td,
2493 struct ofreebsd32_sigsetmask_args *uap)
2497 OSIG2SIG(uap->mask, set);
2498 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2499 SIG2OSIG(oset, td->td_retval[0]);
2504 ofreebsd32_sigsuspend(struct thread *td,
2505 struct ofreebsd32_sigsuspend_args *uap)
2509 OSIG2SIG(uap->mask, mask);
2510 return (kern_sigsuspend(td, mask));
2519 ofreebsd32_sigstack(struct thread *td,
2520 struct ofreebsd32_sigstack_args *uap)
2522 struct sigstack32 s32;
2523 struct sigstack nss, oss;
2524 int error = 0, unss;
2526 if (uap->nss != NULL) {
2527 error = copyin(uap->nss, &s32, sizeof(s32));
2530 nss.ss_sp = PTRIN(s32.ss_sp);
2531 CP(s32, nss, ss_onstack);
2536 oss.ss_sp = td->td_sigstk.ss_sp;
2537 oss.ss_onstack = sigonstack(cpu_getstack(td));
2539 td->td_sigstk.ss_sp = nss.ss_sp;
2540 td->td_sigstk.ss_size = 0;
2541 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2542 td->td_pflags |= TDP_ALTSTACK;
2544 if (uap->oss != NULL) {
2545 s32.ss_sp = PTROUT(oss.ss_sp);
2546 CP(oss, s32, ss_onstack);
2547 error = copyout(&s32, uap->oss, sizeof(s32));
2554 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2557 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2558 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2562 freebsd32_clock_nanosleep(struct thread *td,
2563 struct freebsd32_clock_nanosleep_args *uap)
2567 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2568 uap->rqtp, uap->rmtp);
2569 return (kern_posix_error(td, error));
2573 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2574 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2576 struct timespec32 rmt32, rqt32;
2577 struct timespec rmt, rqt;
2580 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2584 CP(rqt32, rqt, tv_sec);
2585 CP(rqt32, rqt, tv_nsec);
2587 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2588 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2590 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2591 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2594 CP(rmt, rmt32, tv_sec);
2595 CP(rmt, rmt32, tv_nsec);
2597 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2605 freebsd32_clock_gettime(struct thread *td,
2606 struct freebsd32_clock_gettime_args *uap)
2608 struct timespec ats;
2609 struct timespec32 ats32;
2612 error = kern_clock_gettime(td, uap->clock_id, &ats);
2614 CP(ats, ats32, tv_sec);
2615 CP(ats, ats32, tv_nsec);
2616 error = copyout(&ats32, uap->tp, sizeof(ats32));
2622 freebsd32_clock_settime(struct thread *td,
2623 struct freebsd32_clock_settime_args *uap)
2625 struct timespec ats;
2626 struct timespec32 ats32;
2629 error = copyin(uap->tp, &ats32, sizeof(ats32));
2632 CP(ats32, ats, tv_sec);
2633 CP(ats32, ats, tv_nsec);
2635 return (kern_clock_settime(td, uap->clock_id, &ats));
2639 freebsd32_clock_getres(struct thread *td,
2640 struct freebsd32_clock_getres_args *uap)
2643 struct timespec32 ts32;
2646 if (uap->tp == NULL)
2648 error = kern_clock_getres(td, uap->clock_id, &ts);
2650 CP(ts, ts32, tv_sec);
2651 CP(ts, ts32, tv_nsec);
2652 error = copyout(&ts32, uap->tp, sizeof(ts32));
2657 int freebsd32_ktimer_create(struct thread *td,
2658 struct freebsd32_ktimer_create_args *uap)
2660 struct sigevent32 ev32;
2661 struct sigevent ev, *evp;
2664 if (uap->evp == NULL) {
2668 error = copyin(uap->evp, &ev32, sizeof(ev32));
2671 error = convert_sigevent32(&ev32, &ev);
2675 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2677 error = copyout(&id, uap->timerid, sizeof(int));
2679 kern_ktimer_delete(td, id);
2685 freebsd32_ktimer_settime(struct thread *td,
2686 struct freebsd32_ktimer_settime_args *uap)
2688 struct itimerspec32 val32, oval32;
2689 struct itimerspec val, oval, *ovalp;
2692 error = copyin(uap->value, &val32, sizeof(val32));
2696 ovalp = uap->ovalue != NULL ? &oval : NULL;
2697 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2698 if (error == 0 && uap->ovalue != NULL) {
2699 ITS_CP(oval, oval32);
2700 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2706 freebsd32_ktimer_gettime(struct thread *td,
2707 struct freebsd32_ktimer_gettime_args *uap)
2709 struct itimerspec32 val32;
2710 struct itimerspec val;
2713 error = kern_ktimer_gettime(td, uap->timerid, &val);
2716 error = copyout(&val32, uap->value, sizeof(val32));
2722 freebsd32_clock_getcpuclockid2(struct thread *td,
2723 struct freebsd32_clock_getcpuclockid2_args *uap)
2728 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2729 uap->which, &clk_id);
2731 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2736 freebsd32_thr_new(struct thread *td,
2737 struct freebsd32_thr_new_args *uap)
2739 struct thr_param32 param32;
2740 struct thr_param param;
2743 if (uap->param_size < 0 ||
2744 uap->param_size > sizeof(struct thr_param32))
2746 bzero(¶m, sizeof(struct thr_param));
2747 bzero(¶m32, sizeof(struct thr_param32));
2748 error = copyin(uap->param, ¶m32, uap->param_size);
2751 param.start_func = PTRIN(param32.start_func);
2752 param.arg = PTRIN(param32.arg);
2753 param.stack_base = PTRIN(param32.stack_base);
2754 param.stack_size = param32.stack_size;
2755 param.tls_base = PTRIN(param32.tls_base);
2756 param.tls_size = param32.tls_size;
2757 param.child_tid = PTRIN(param32.child_tid);
2758 param.parent_tid = PTRIN(param32.parent_tid);
2759 param.flags = param32.flags;
2760 param.rtp = PTRIN(param32.rtp);
2761 param.spare[0] = PTRIN(param32.spare[0]);
2762 param.spare[1] = PTRIN(param32.spare[1]);
2763 param.spare[2] = PTRIN(param32.spare[2]);
2765 return (kern_thr_new(td, ¶m));
2769 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2771 struct timespec32 ts32;
2772 struct timespec ts, *tsp;
2777 if (uap->timeout != NULL) {
2778 error = copyin((const void *)uap->timeout, (void *)&ts32,
2779 sizeof(struct timespec32));
2782 ts.tv_sec = ts32.tv_sec;
2783 ts.tv_nsec = ts32.tv_nsec;
2786 return (kern_thr_suspend(td, tsp));
2790 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2792 bzero(dst, sizeof(*dst));
2793 dst->si_signo = src->si_signo;
2794 dst->si_errno = src->si_errno;
2795 dst->si_code = src->si_code;
2796 dst->si_pid = src->si_pid;
2797 dst->si_uid = src->si_uid;
2798 dst->si_status = src->si_status;
2799 dst->si_addr = (uintptr_t)src->si_addr;
2800 dst->si_value.sival_int = src->si_value.sival_int;
2801 dst->si_timerid = src->si_timerid;
2802 dst->si_overrun = src->si_overrun;
2805 #ifndef _FREEBSD32_SYSPROTO_H_
2806 struct freebsd32_sigqueue_args {
2809 /* union sigval32 */ int value;
2813 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2818 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2819 * On 64-bit little-endian ABIs, the low bits are the same.
2820 * In 64-bit big-endian ABIs, sival_int overlaps with
2821 * sival_ptr's HIGH bits. We choose to support sival_int
2822 * rather than sival_ptr in this case as it seems to be
2825 bzero(&sv, sizeof(sv));
2826 sv.sival_int = uap->value;
2828 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2832 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2834 struct timespec32 ts32;
2836 struct timespec *timeout;
2839 struct siginfo32 si32;
2843 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2846 ts.tv_sec = ts32.tv_sec;
2847 ts.tv_nsec = ts32.tv_nsec;
2852 error = copyin(uap->set, &set, sizeof(set));
2856 error = kern_sigtimedwait(td, set, &ksi, timeout);
2861 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2862 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2866 td->td_retval[0] = ksi.ksi_signo;
2874 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2877 struct siginfo32 si32;
2881 error = copyin(uap->set, &set, sizeof(set));
2885 error = kern_sigtimedwait(td, set, &ksi, NULL);
2890 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2891 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2894 td->td_retval[0] = ksi.ksi_signo;
2899 freebsd32_cpuset_setid(struct thread *td,
2900 struct freebsd32_cpuset_setid_args *uap)
2903 return (kern_cpuset_setid(td, uap->which,
2904 PAIR32TO64(id_t, uap->id), uap->setid));
2908 freebsd32_cpuset_getid(struct thread *td,
2909 struct freebsd32_cpuset_getid_args *uap)
2912 return (kern_cpuset_getid(td, uap->level, uap->which,
2913 PAIR32TO64(id_t, uap->id), uap->setid));
2917 freebsd32_cpuset_getaffinity(struct thread *td,
2918 struct freebsd32_cpuset_getaffinity_args *uap)
2921 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2922 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2926 freebsd32_cpuset_setaffinity(struct thread *td,
2927 struct freebsd32_cpuset_setaffinity_args *uap)
2930 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2931 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2935 freebsd32_nmount(struct thread *td,
2936 struct freebsd32_nmount_args /* {
2938 unsigned int iovcnt;
2947 * Mount flags are now 64-bits. On 32-bit archtectures only
2948 * 32-bits are passed in, but from here on everything handles
2949 * 64-bit flags correctly.
2953 AUDIT_ARG_FFLAGS(flags);
2956 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
2957 * userspace to set this flag, but we must filter it out if we want
2958 * MNT_UPDATE on the root file system to work.
2959 * MNT_ROOTFS should only be set by the kernel when mounting its
2962 flags &= ~MNT_ROOTFS;
2965 * check that we have an even number of iovec's
2966 * and that we have at least two options.
2968 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
2971 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2974 error = vfs_donmount(td, flags, auio);
2982 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
2984 struct yyy32 *p32, s32;
2985 struct yyy *p = NULL, s;
2990 error = copyin(uap->zzz, &s32, sizeof(s32));
2996 error = kern_xxx(td, p);
3001 error = copyout(&s32, p32, sizeof(s32));
3008 syscall32_register(int *offset, struct sysent *new_sysent,
3009 struct sysent *old_sysent, int flags)
3012 if ((flags & ~SY_THR_STATIC) != 0)
3015 if (*offset == NO_SYSCALL) {
3018 for (i = 1; i < SYS_MAXSYSCALL; ++i)
3019 if (freebsd32_sysent[i].sy_call ==
3020 (sy_call_t *)lkmnosys)
3022 if (i == SYS_MAXSYSCALL)
3025 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
3027 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
3028 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
3031 *old_sysent = freebsd32_sysent[*offset];
3032 freebsd32_sysent[*offset] = *new_sysent;
3033 atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
3038 syscall32_deregister(int *offset, struct sysent *old_sysent)
3044 freebsd32_sysent[*offset] = *old_sysent;
3049 syscall32_module_handler(struct module *mod, int what, void *arg)
3051 struct syscall_module_data *data = (struct syscall_module_data*)arg;
3057 error = syscall32_register(data->offset, data->new_sysent,
3058 &data->old_sysent, SY_THR_STATIC_KLD);
3060 /* Leave a mark so we know to safely unload below. */
3061 data->offset = NULL;
3064 ms.intval = *data->offset;
3066 module_setspecific(mod, &ms);
3069 error = data->chainevh(mod, what, data->chainarg);
3073 * MOD_LOAD failed, so just return without calling the
3074 * chained handler since we didn't pass along the MOD_LOAD
3077 if (data->offset == NULL)
3079 if (data->chainevh) {
3080 error = data->chainevh(mod, what, data->chainarg);
3084 error = syscall32_deregister(data->offset, &data->old_sysent);
3089 error = data->chainevh(mod, what, data->chainarg);
3095 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3097 struct syscall_helper_data *sd1;
3100 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
3101 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
3102 &sd1->old_sysent, flags);
3104 syscall32_helper_unregister(sd);
3107 sd1->registered = 1;
3113 syscall32_helper_unregister(struct syscall_helper_data *sd)
3115 struct syscall_helper_data *sd1;
3117 for (sd1 = sd; sd1->registered != 0; sd1++) {
3118 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
3119 sd1->registered = 0;
3125 freebsd32_copyout_strings(struct image_params *imgp)
3131 u_int32_t *stack_base;
3132 struct freebsd32_ps_strings *arginfo;
3133 char canary[sizeof(long) * 8];
3134 int32_t pagesizes32[MAXPAGESIZES];
3135 size_t execpath_len;
3139 * Calculate string base and vector table pointers.
3140 * Also deal with signal trampoline code for this exec type.
3142 if (imgp->execpath != NULL && imgp->auxargs != NULL)
3143 execpath_len = strlen(imgp->execpath) + 1;
3146 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3148 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3149 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3152 destp = (uintptr_t)arginfo;
3157 if (szsigcode != 0) {
3159 destp = rounddown2(destp, sizeof(uint32_t));
3160 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3165 * Copy the image path for the rtld.
3167 if (execpath_len != 0) {
3168 destp -= execpath_len;
3169 imgp->execpathp = destp;
3170 copyout(imgp->execpath, (void *)destp, execpath_len);
3174 * Prepare the canary for SSP.
3176 arc4rand(canary, sizeof(canary), 0);
3177 destp -= sizeof(canary);
3178 imgp->canary = destp;
3179 copyout(canary, (void *)destp, sizeof(canary));
3180 imgp->canarylen = sizeof(canary);
3183 * Prepare the pagesizes array.
3185 for (i = 0; i < MAXPAGESIZES; i++)
3186 pagesizes32[i] = (uint32_t)pagesizes[i];
3187 destp -= sizeof(pagesizes32);
3188 destp = rounddown2(destp, sizeof(uint32_t));
3189 imgp->pagesizes = destp;
3190 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3191 imgp->pagesizeslen = sizeof(pagesizes32);
3193 destp -= ARG_MAX - imgp->args->stringspace;
3194 destp = rounddown2(destp, sizeof(uint32_t));
3197 * If we have a valid auxargs ptr, prepare some room
3200 if (imgp->auxargs) {
3202 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
3203 * lower compatibility.
3205 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
3208 * The '+ 2' is for the null pointers at the end of each of
3209 * the arg and env vector sets,and imgp->auxarg_size is room
3210 * for argument of Runtime loader.
3212 vectp = (u_int32_t *) (destp - (imgp->args->argc +
3213 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
3217 * The '+ 2' is for the null pointers at the end of each of
3218 * the arg and env vector sets
3220 vectp = (u_int32_t *)(destp - (imgp->args->argc +
3221 imgp->args->envc + 2) * sizeof(u_int32_t));
3225 * vectp also becomes our initial stack base
3229 stringp = imgp->args->begin_argv;
3230 argc = imgp->args->argc;
3231 envc = imgp->args->envc;
3233 * Copy out strings - arguments and environment.
3235 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3238 * Fill in "ps_strings" struct for ps, w, etc.
3240 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3241 suword32(&arginfo->ps_nargvstr, argc);
3244 * Fill in argument portion of vector table.
3246 for (; argc > 0; --argc) {
3247 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3248 while (*stringp++ != 0)
3253 /* a null vector table pointer separates the argp's from the envp's */
3254 suword32(vectp++, 0);
3256 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3257 suword32(&arginfo->ps_nenvstr, envc);
3260 * Fill in environment portion of vector table.
3262 for (; envc > 0; --envc) {
3263 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3264 while (*stringp++ != 0)
3269 /* end of vector table is a null pointer */
3272 return ((register_t *)stack_base);
3276 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3278 struct kld_file_stat stat;
3279 struct kld32_file_stat stat32;
3282 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3285 if (version != sizeof(struct kld32_file_stat_1) &&
3286 version != sizeof(struct kld32_file_stat))
3289 error = kern_kldstat(td, uap->fileid, &stat);
3293 bcopy(&stat.name[0], &stat32.name[0], sizeof(stat.name));
3294 CP(stat, stat32, refs);
3295 CP(stat, stat32, id);
3296 PTROUT_CP(stat, stat32, address);
3297 CP(stat, stat32, size);
3298 bcopy(&stat.pathname[0], &stat32.pathname[0], sizeof(stat.pathname));
3299 return (copyout(&stat32, uap->stat, version));
3303 freebsd32_posix_fallocate(struct thread *td,
3304 struct freebsd32_posix_fallocate_args *uap)
3308 error = kern_posix_fallocate(td, uap->fd,
3309 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3310 return (kern_posix_error(td, error));
3314 freebsd32_posix_fadvise(struct thread *td,
3315 struct freebsd32_posix_fadvise_args *uap)
3319 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3320 PAIR32TO64(off_t, uap->len), uap->advice);
3321 return (kern_posix_error(td, error));
3325 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3328 CP(*sig32, *sig, sigev_notify);
3329 switch (sig->sigev_notify) {
3332 case SIGEV_THREAD_ID:
3333 CP(*sig32, *sig, sigev_notify_thread_id);
3336 CP(*sig32, *sig, sigev_signo);
3337 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3340 CP(*sig32, *sig, sigev_notify_kqueue);
3341 CP(*sig32, *sig, sigev_notify_kevent_flags);
3342 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3351 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3355 struct procctl_reaper_status rs;
3356 struct procctl_reaper_pids rp;
3357 struct procctl_reaper_kill rk;
3360 struct procctl_reaper_pids32 rp;
3362 int error, error1, flags;
3366 case PROC_TRACE_CTL:
3367 case PROC_TRAPCAP_CTL:
3368 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3373 case PROC_REAP_ACQUIRE:
3374 case PROC_REAP_RELEASE:
3375 if (uap->data != NULL)
3379 case PROC_REAP_STATUS:
3382 case PROC_REAP_GETPIDS:
3383 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3386 CP(x32.rp, x.rp, rp_count);
3387 PTRIN_CP(x32.rp, x.rp, rp_pids);
3390 case PROC_REAP_KILL:
3391 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3396 case PROC_TRACE_STATUS:
3397 case PROC_TRAPCAP_STATUS:
3403 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3406 case PROC_REAP_STATUS:
3408 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3410 case PROC_REAP_KILL:
3411 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3415 case PROC_TRACE_STATUS:
3416 case PROC_TRAPCAP_STATUS:
3418 error = copyout(&flags, uap->data, sizeof(flags));
3425 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3431 * Do unsigned conversion for arg when operation
3432 * interprets it as flags or pointer.
3434 case F_SETLK_REMOTE:
3443 tmp = (unsigned int)(uap->arg);
3449 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3453 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3455 struct timespec32 ts32;
3456 struct timespec ts, *tsp;
3460 if (uap->ts != NULL) {
3461 error = copyin(uap->ts, &ts32, sizeof(ts32));
3464 CP(ts32, ts, tv_sec);
3465 CP(ts32, ts, tv_nsec);
3469 if (uap->set != NULL) {
3470 error = copyin(uap->set, &set, sizeof(set));
3477 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));