2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
34 #include "opt_inet6.h"
35 #include "opt_ktrace.h"
37 #define __ELF_WORD_SIZE 32
39 #ifdef COMPAT_FREEBSD11
40 #define _WANT_FREEBSD11_KEVENT
43 #include <sys/param.h>
45 #include <sys/capsicum.h>
46 #include <sys/clock.h>
48 #include <sys/fcntl.h>
49 #include <sys/filedesc.h>
50 #include <sys/imgact.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/linker.h>
56 #include <sys/malloc.h>
57 #include <sys/file.h> /* Must come after sys/malloc.h */
58 #include <sys/imgact.h>
61 #include <sys/module.h>
62 #include <sys/mount.h>
63 #include <sys/mutex.h>
64 #include <sys/namei.h>
66 #include <sys/procctl.h>
67 #include <sys/reboot.h>
68 #include <sys/resource.h>
69 #include <sys/resourcevar.h>
70 #include <sys/selinfo.h>
71 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
72 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
73 #include <sys/signal.h>
74 #include <sys/signalvar.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
78 #include <sys/syscall.h>
79 #include <sys/syscallsubr.h>
80 #include <sys/sysctl.h>
81 #include <sys/sysent.h>
82 #include <sys/sysproto.h>
83 #include <sys/systm.h>
85 #include <sys/unistd.h>
86 #include <sys/ucontext.h>
87 #include <sys/vnode.h>
94 #include <sys/ktrace.h>
98 #include <netinet/in.h>
102 #include <vm/vm_param.h>
104 #include <vm/vm_map.h>
105 #include <vm/vm_object.h>
106 #include <vm/vm_extern.h>
108 #include <machine/cpu.h>
109 #include <machine/elf.h>
111 #include <security/audit/audit.h>
113 #include <compat/freebsd32/freebsd32_util.h>
114 #include <compat/freebsd32/freebsd32.h>
115 #include <compat/freebsd32/freebsd32_ipc.h>
116 #include <compat/freebsd32/freebsd32_misc.h>
117 #include <compat/freebsd32/freebsd32_signal.h>
118 #include <compat/freebsd32/freebsd32_proto.h>
120 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
123 CTASSERT(sizeof(struct timeval32) == 8);
124 CTASSERT(sizeof(struct timespec32) == 8);
125 CTASSERT(sizeof(struct itimerval32) == 16);
126 CTASSERT(sizeof(struct bintime32) == 12);
128 CTASSERT(sizeof(struct statfs32) == 256);
130 CTASSERT(sizeof(struct rusage32) == 72);
132 CTASSERT(sizeof(struct sigaltstack32) == 12);
134 CTASSERT(sizeof(struct kevent32) == 56);
136 CTASSERT(sizeof(struct kevent32) == 64);
138 CTASSERT(sizeof(struct iovec32) == 8);
139 CTASSERT(sizeof(struct msghdr32) == 28);
141 CTASSERT(sizeof(struct stat32) == 208);
142 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
144 CTASSERT(sizeof(struct sigaction32) == 24);
146 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
147 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
148 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
149 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
152 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
155 TV_CP(*s, *s32, ru_utime);
156 TV_CP(*s, *s32, ru_stime);
157 CP(*s, *s32, ru_maxrss);
158 CP(*s, *s32, ru_ixrss);
159 CP(*s, *s32, ru_idrss);
160 CP(*s, *s32, ru_isrss);
161 CP(*s, *s32, ru_minflt);
162 CP(*s, *s32, ru_majflt);
163 CP(*s, *s32, ru_nswap);
164 CP(*s, *s32, ru_inblock);
165 CP(*s, *s32, ru_oublock);
166 CP(*s, *s32, ru_msgsnd);
167 CP(*s, *s32, ru_msgrcv);
168 CP(*s, *s32, ru_nsignals);
169 CP(*s, *s32, ru_nvcsw);
170 CP(*s, *s32, ru_nivcsw);
174 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
177 struct rusage32 ru32;
178 struct rusage ru, *rup;
180 if (uap->rusage != NULL)
184 error = kern_wait(td, uap->pid, &status, uap->options, rup);
187 if (uap->status != NULL)
188 error = copyout(&status, uap->status, sizeof(status));
189 if (uap->rusage != NULL && error == 0) {
190 freebsd32_rusage_out(&ru, &ru32);
191 error = copyout(&ru32, uap->rusage, sizeof(ru32));
197 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
199 struct wrusage32 wru32;
200 struct __wrusage wru, *wrup;
201 struct siginfo32 si32;
202 struct __siginfo si, *sip;
205 if (uap->wrusage != NULL)
209 if (uap->info != NULL) {
211 bzero(sip, sizeof(*sip));
214 error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
215 &status, uap->options, wrup, sip);
218 if (uap->status != NULL)
219 error = copyout(&status, uap->status, sizeof(status));
220 if (uap->wrusage != NULL && error == 0) {
221 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
222 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
223 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
225 if (uap->info != NULL && error == 0) {
226 siginfo_to_siginfo32 (&si, &si32);
227 error = copyout(&si32, uap->info, sizeof(si32));
232 #ifdef COMPAT_FREEBSD4
234 copy_statfs(struct statfs *in, struct statfs32 *out)
237 statfs_scale_blocks(in, INT32_MAX);
238 bzero(out, sizeof(*out));
239 CP(*in, *out, f_bsize);
240 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
241 CP(*in, *out, f_blocks);
242 CP(*in, *out, f_bfree);
243 CP(*in, *out, f_bavail);
244 out->f_files = MIN(in->f_files, INT32_MAX);
245 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
246 CP(*in, *out, f_fsid);
247 CP(*in, *out, f_owner);
248 CP(*in, *out, f_type);
249 CP(*in, *out, f_flags);
250 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
251 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
252 strlcpy(out->f_fstypename,
253 in->f_fstypename, MFSNAMELEN);
254 strlcpy(out->f_mntonname,
255 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
256 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
257 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
258 strlcpy(out->f_mntfromname,
259 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
263 #ifdef COMPAT_FREEBSD4
265 freebsd4_freebsd32_getfsstat(struct thread *td,
266 struct freebsd4_freebsd32_getfsstat_args *uap)
268 struct statfs *buf, *sp;
269 struct statfs32 stat32;
270 size_t count, size, copycount;
273 count = uap->bufsize / sizeof(struct statfs32);
274 size = count * sizeof(struct statfs);
275 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
279 while (copycount > 0 && error == 0) {
280 copy_statfs(sp, &stat32);
281 error = copyout(&stat32, uap->buf, sizeof(stat32));
289 td->td_retval[0] = count;
294 #ifdef COMPAT_FREEBSD10
296 freebsd10_freebsd32_pipe(struct thread *td,
297 struct freebsd10_freebsd32_pipe_args *uap) {
299 return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
304 freebsd32_sigaltstack(struct thread *td,
305 struct freebsd32_sigaltstack_args *uap)
307 struct sigaltstack32 s32;
308 struct sigaltstack ss, oss, *ssp;
311 if (uap->ss != NULL) {
312 error = copyin(uap->ss, &s32, sizeof(s32));
315 PTRIN_CP(s32, ss, ss_sp);
316 CP(s32, ss, ss_size);
317 CP(s32, ss, ss_flags);
321 error = kern_sigaltstack(td, ssp, &oss);
322 if (error == 0 && uap->oss != NULL) {
323 PTROUT_CP(oss, s32, ss_sp);
324 CP(oss, s32, ss_size);
325 CP(oss, s32, ss_flags);
326 error = copyout(&s32, uap->oss, sizeof(s32));
332 * Custom version of exec_copyin_args() so that we can translate
336 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
337 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
344 bzero(args, sizeof(*args));
349 * Allocate demand-paged memory for the file name, argument, and
350 * environment strings.
352 error = exec_alloc_args(args);
357 * Copy the file name.
360 args->fname = args->buf;
361 error = (segflg == UIO_SYSSPACE) ?
362 copystr(fname, args->fname, PATH_MAX, &length) :
363 copyinstr(fname, args->fname, PATH_MAX, &length);
369 args->begin_argv = args->buf + length;
370 args->endp = args->begin_argv;
371 args->stringspace = ARG_MAX;
374 * extract arguments first
378 error = copyin(p32++, &arg, sizeof(arg));
384 error = copyinstr(argp, args->endp, args->stringspace, &length);
386 if (error == ENAMETOOLONG)
390 args->stringspace -= length;
391 args->endp += length;
395 args->begin_envv = args->endp;
398 * extract environment strings
403 error = copyin(p32++, &arg, sizeof(arg));
409 error = copyinstr(envp, args->endp, args->stringspace,
412 if (error == ENAMETOOLONG)
416 args->stringspace -= length;
417 args->endp += length;
425 exec_free_args(args);
430 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
432 struct image_args eargs;
433 struct vmspace *oldvmspace;
436 error = pre_execve(td, &oldvmspace);
439 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
440 uap->argv, uap->envv);
442 error = kern_execve(td, &eargs, NULL);
443 post_execve(td, error, oldvmspace);
448 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
450 struct image_args eargs;
451 struct vmspace *oldvmspace;
454 error = pre_execve(td, &oldvmspace);
457 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
458 uap->argv, uap->envv);
461 error = kern_execve(td, &eargs, NULL);
463 post_execve(td, error, oldvmspace);
467 #if defined(COMPAT_FREEBSD11)
469 freebsd11_freebsd32_mknod(struct thread *td,
470 struct freebsd11_freebsd32_mknod_args *uap)
473 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->mode,
478 freebsd11_freebsd32_mknodat(struct thread *td,
479 struct freebsd11_freebsd32_mknodat_args *uap)
482 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
485 #endif /* COMPAT_FREEBSD11 */
488 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
493 #if defined(__amd64__)
494 if (i386_read_exec && (prot & PROT_READ) != 0)
497 return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
502 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
507 #if defined(__amd64__)
508 if (i386_read_exec && (prot & PROT_READ))
512 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
513 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
516 #ifdef COMPAT_FREEBSD6
518 freebsd6_freebsd32_mmap(struct thread *td,
519 struct freebsd6_freebsd32_mmap_args *uap)
524 #if defined(__amd64__)
525 if (i386_read_exec && (prot & PROT_READ))
529 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
530 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
535 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
537 struct itimerval itv, oitv, *itvp;
538 struct itimerval32 i32;
541 if (uap->itv != NULL) {
542 error = copyin(uap->itv, &i32, sizeof(i32));
545 TV_CP(i32, itv, it_interval);
546 TV_CP(i32, itv, it_value);
550 error = kern_setitimer(td, uap->which, itvp, &oitv);
551 if (error || uap->oitv == NULL)
553 TV_CP(oitv, i32, it_interval);
554 TV_CP(oitv, i32, it_value);
555 return (copyout(&i32, uap->oitv, sizeof(i32)));
559 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
561 struct itimerval itv;
562 struct itimerval32 i32;
565 error = kern_getitimer(td, uap->which, &itv);
566 if (error || uap->itv == NULL)
568 TV_CP(itv, i32, it_interval);
569 TV_CP(itv, i32, it_value);
570 return (copyout(&i32, uap->itv, sizeof(i32)));
574 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
576 struct timeval32 tv32;
577 struct timeval tv, *tvp;
580 if (uap->tv != NULL) {
581 error = copyin(uap->tv, &tv32, sizeof(tv32));
584 CP(tv32, tv, tv_sec);
585 CP(tv32, tv, tv_usec);
590 * XXX Do pointers need PTRIN()?
592 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
593 sizeof(int32_t) * 8));
597 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
599 struct timespec32 ts32;
601 struct timeval tv, *tvp;
605 if (uap->ts != NULL) {
606 error = copyin(uap->ts, &ts32, sizeof(ts32));
609 CP(ts32, ts, tv_sec);
610 CP(ts32, ts, tv_nsec);
611 TIMESPEC_TO_TIMEVAL(&tv, &ts);
615 if (uap->sm != NULL) {
616 error = copyin(uap->sm, &set, sizeof(set));
623 * XXX Do pointers need PTRIN()?
625 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
626 uset, sizeof(int32_t) * 8);
631 * Copy 'count' items into the destination list pointed to by uap->eventlist.
634 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
636 struct freebsd32_kevent_args *uap;
637 struct kevent32 ks32[KQ_NEVENTS];
641 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
642 uap = (struct freebsd32_kevent_args *)arg;
644 for (i = 0; i < count; i++) {
645 CP(kevp[i], ks32[i], ident);
646 CP(kevp[i], ks32[i], filter);
647 CP(kevp[i], ks32[i], flags);
648 CP(kevp[i], ks32[i], fflags);
649 #if BYTE_ORDER == LITTLE_ENDIAN
650 ks32[i].data1 = kevp[i].data;
651 ks32[i].data2 = kevp[i].data >> 32;
653 ks32[i].data1 = kevp[i].data >> 32;
654 ks32[i].data2 = kevp[i].data;
656 PTROUT_CP(kevp[i], ks32[i], udata);
657 for (j = 0; j < nitems(kevp->ext); j++) {
659 #if BYTE_ORDER == LITTLE_ENDIAN
660 ks32[i].ext64[2 * j] = e;
661 ks32[i].ext64[2 * j + 1] = e >> 32;
663 ks32[i].ext64[2 * j] = e >> 32;
664 ks32[i].ext64[2 * j + 1] = e;
668 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
670 uap->eventlist += count;
675 * Copy 'count' items from the list pointed to by uap->changelist.
678 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
680 struct freebsd32_kevent_args *uap;
681 struct kevent32 ks32[KQ_NEVENTS];
685 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
686 uap = (struct freebsd32_kevent_args *)arg;
688 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
691 uap->changelist += count;
693 for (i = 0; i < count; i++) {
694 CP(ks32[i], kevp[i], ident);
695 CP(ks32[i], kevp[i], filter);
696 CP(ks32[i], kevp[i], flags);
697 CP(ks32[i], kevp[i], fflags);
698 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
699 PTRIN_CP(ks32[i], kevp[i], udata);
700 for (j = 0; j < nitems(kevp->ext); j++) {
701 #if BYTE_ORDER == LITTLE_ENDIAN
702 e = ks32[i].ext64[2 * j + 1];
704 e += ks32[i].ext64[2 * j];
706 e = ks32[i].ext64[2 * j];
708 e += ks32[i].ext64[2 * j + 1];
718 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
720 struct timespec32 ts32;
721 struct timespec ts, *tsp;
722 struct kevent_copyops k_ops = {
724 .k_copyout = freebsd32_kevent_copyout,
725 .k_copyin = freebsd32_kevent_copyin,
728 struct kevent32 *eventlist = uap->eventlist;
733 error = copyin(uap->timeout, &ts32, sizeof(ts32));
736 CP(ts32, ts, tv_sec);
737 CP(ts32, ts, tv_nsec);
742 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
743 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
744 uap->nchanges, sizeof(struct kevent32));
746 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
749 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
750 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
751 td->td_retval[0], sizeof(struct kevent32));
756 #ifdef COMPAT_FREEBSD11
758 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
760 struct freebsd11_freebsd32_kevent_args *uap;
761 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
764 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
765 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
767 for (i = 0; i < count; i++) {
768 CP(kevp[i], ks32[i], ident);
769 CP(kevp[i], ks32[i], filter);
770 CP(kevp[i], ks32[i], flags);
771 CP(kevp[i], ks32[i], fflags);
772 CP(kevp[i], ks32[i], data);
773 PTROUT_CP(kevp[i], ks32[i], udata);
775 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
777 uap->eventlist += count;
782 * Copy 'count' items from the list pointed to by uap->changelist.
785 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
787 struct freebsd11_freebsd32_kevent_args *uap;
788 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
791 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
792 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
794 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
797 uap->changelist += count;
799 for (i = 0; i < count; i++) {
800 CP(ks32[i], kevp[i], ident);
801 CP(ks32[i], kevp[i], filter);
802 CP(ks32[i], kevp[i], flags);
803 CP(ks32[i], kevp[i], fflags);
804 CP(ks32[i], kevp[i], data);
805 PTRIN_CP(ks32[i], kevp[i], udata);
806 for (j = 0; j < nitems(kevp->ext); j++)
814 freebsd11_freebsd32_kevent(struct thread *td,
815 struct freebsd11_freebsd32_kevent_args *uap)
817 struct timespec32 ts32;
818 struct timespec ts, *tsp;
819 struct kevent_copyops k_ops = {
821 .k_copyout = freebsd32_kevent11_copyout,
822 .k_copyin = freebsd32_kevent11_copyin,
825 struct kevent32_freebsd11 *eventlist = uap->eventlist;
830 error = copyin(uap->timeout, &ts32, sizeof(ts32));
833 CP(ts32, ts, tv_sec);
834 CP(ts32, ts, tv_nsec);
839 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
840 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
841 uap->changelist, uap->nchanges,
842 sizeof(struct kevent32_freebsd11));
844 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
847 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
848 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
849 eventlist, td->td_retval[0],
850 sizeof(struct kevent32_freebsd11));
857 freebsd32_gettimeofday(struct thread *td,
858 struct freebsd32_gettimeofday_args *uap)
861 struct timeval32 atv32;
867 CP(atv, atv32, tv_sec);
868 CP(atv, atv32, tv_usec);
869 error = copyout(&atv32, uap->tp, sizeof (atv32));
871 if (error == 0 && uap->tzp != NULL) {
872 rtz.tz_minuteswest = tz_minuteswest;
873 rtz.tz_dsttime = tz_dsttime;
874 error = copyout(&rtz, uap->tzp, sizeof (rtz));
880 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
886 error = kern_getrusage(td, uap->who, &s);
889 if (uap->rusage != NULL) {
890 freebsd32_rusage_out(&s, &s32);
891 error = copyout(&s32, uap->rusage, sizeof(s32));
897 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
899 struct iovec32 iov32;
906 if (iovcnt > UIO_MAXIOV)
908 iovlen = iovcnt * sizeof(struct iovec);
909 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
910 iov = (struct iovec *)(uio + 1);
911 for (i = 0; i < iovcnt; i++) {
912 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
917 iov[i].iov_base = PTRIN(iov32.iov_base);
918 iov[i].iov_len = iov32.iov_len;
921 uio->uio_iovcnt = iovcnt;
922 uio->uio_segflg = UIO_USERSPACE;
923 uio->uio_offset = -1;
925 for (i = 0; i < iovcnt; i++) {
926 if (iov->iov_len > INT_MAX - uio->uio_resid) {
930 uio->uio_resid += iov->iov_len;
938 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
943 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
946 error = kern_readv(td, uap->fd, auio);
952 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
957 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
960 error = kern_writev(td, uap->fd, auio);
966 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
971 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
974 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
980 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
985 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
988 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
994 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
997 struct iovec32 iov32;
1003 if (iovcnt > UIO_MAXIOV)
1005 iovlen = iovcnt * sizeof(struct iovec);
1006 iov = malloc(iovlen, M_IOV, M_WAITOK);
1007 for (i = 0; i < iovcnt; i++) {
1008 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
1013 iov[i].iov_base = PTRIN(iov32.iov_base);
1014 iov[i].iov_len = iov32.iov_len;
1021 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
1023 struct msghdr32 m32;
1026 error = copyin(msg32, &m32, sizeof(m32));
1029 msg->msg_name = PTRIN(m32.msg_name);
1030 msg->msg_namelen = m32.msg_namelen;
1031 msg->msg_iov = PTRIN(m32.msg_iov);
1032 msg->msg_iovlen = m32.msg_iovlen;
1033 msg->msg_control = PTRIN(m32.msg_control);
1034 msg->msg_controllen = m32.msg_controllen;
1035 msg->msg_flags = m32.msg_flags;
1040 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1042 struct msghdr32 m32;
1045 m32.msg_name = PTROUT(msg->msg_name);
1046 m32.msg_namelen = msg->msg_namelen;
1047 m32.msg_iov = PTROUT(msg->msg_iov);
1048 m32.msg_iovlen = msg->msg_iovlen;
1049 m32.msg_control = PTROUT(msg->msg_control);
1050 m32.msg_controllen = msg->msg_controllen;
1051 m32.msg_flags = msg->msg_flags;
1052 error = copyout(&m32, msg32, sizeof(m32));
1057 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
1059 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
1061 #define FREEBSD32_ALIGN(p) \
1062 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1063 #define FREEBSD32_CMSG_SPACE(l) \
1064 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1066 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
1067 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1070 freebsd32_cmsg_convert(struct cmsghdr *cm, void *data, socklen_t datalen)
1074 struct timespec32 ts;
1075 struct timeval32 tv;
1076 struct bintime32 bt;
1087 switch (cm->cmsg_level) {
1089 switch (cm->cmsg_type) {
1091 TV_CP(*in, tmp32, tv);
1092 copylen = sizeof(tmp32.tv);
1096 BT_CP(*in, tmp32, bt);
1097 copylen = sizeof(tmp32.bt);
1102 TS_CP(*in, tmp32, ts);
1103 copylen = sizeof(tmp32.ts);
1117 KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
1119 bcopy(&tmp32, data, copylen);
1124 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1128 socklen_t clen, datalen, datalen_out;
1131 int len, maxlen, copylen;
1135 len = msg->msg_controllen;
1136 maxlen = msg->msg_controllen;
1137 msg->msg_controllen = 0;
1140 ctlbuf = msg->msg_control;
1142 while (m && len > 0) {
1143 cm = mtod(m, struct cmsghdr *);
1146 while (cm != NULL) {
1148 if (sizeof(struct cmsghdr) > clen ||
1149 cm->cmsg_len > clen) {
1154 data = CMSG_DATA(cm);
1155 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1156 datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
1158 /* Adjust message length */
1159 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1163 copylen = sizeof(struct cmsghdr);
1164 if (len < copylen) {
1165 msg->msg_flags |= MSG_CTRUNC;
1169 error = copyout(cm, ctlbuf, copylen);
1173 ctlbuf += FREEBSD32_ALIGN(copylen);
1174 len -= FREEBSD32_ALIGN(copylen);
1180 copylen = datalen_out;
1181 if (len < copylen) {
1182 msg->msg_flags |= MSG_CTRUNC;
1186 error = copyout(data, ctlbuf, copylen);
1190 ctlbuf += FREEBSD32_ALIGN(copylen);
1191 len -= FREEBSD32_ALIGN(copylen);
1193 if (CMSG_SPACE(datalen) < clen) {
1194 clen -= CMSG_SPACE(datalen);
1195 cm = (struct cmsghdr *)
1196 ((caddr_t)cm + CMSG_SPACE(datalen));
1205 msg->msg_controllen = (len <= 0) ? maxlen : ctlbuf - (caddr_t)msg->msg_control;
1213 freebsd32_recvmsg(td, uap)
1215 struct freebsd32_recvmsg_args /* {
1217 struct msghdr32 *msg;
1222 struct msghdr32 m32;
1223 struct iovec *uiov, *iov;
1224 struct mbuf *control = NULL;
1225 struct mbuf **controlp;
1228 error = copyin(uap->msg, &m32, sizeof(m32));
1231 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1234 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1238 msg.msg_flags = uap->flags;
1242 controlp = (msg.msg_control != NULL) ? &control : NULL;
1243 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1247 if (control != NULL)
1248 error = freebsd32_copy_msg_out(&msg, control);
1250 msg.msg_controllen = 0;
1253 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1257 if (control != NULL)
1264 * Copy-in the array of control messages constructed using alignment
1265 * and padding suitable for a 32-bit environment and construct an
1266 * mbuf using alignment and padding suitable for a 64-bit kernel.
1267 * The alignment and padding are defined indirectly by CMSG_DATA(),
1268 * CMSG_SPACE() and CMSG_LEN().
1271 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1275 u_int idx, len, msglen;
1278 buflen = FREEBSD32_ALIGN(buflen);
1280 if (buflen > MCLBYTES)
1284 * Iterate over the buffer and get the length of each message
1285 * in there. This has 32-bit alignment and padding. Use it to
1286 * determine the length of these messages when using 64-bit
1287 * alignment and padding.
1291 while (idx < buflen) {
1292 error = copyin(buf + idx, &msglen, sizeof(msglen));
1295 if (msglen < sizeof(struct cmsghdr))
1297 msglen = FREEBSD32_ALIGN(msglen);
1298 if (idx + msglen > buflen)
1301 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1302 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1303 len += CMSG_ALIGN(msglen);
1309 m = m_get(M_WAITOK, MT_CONTROL);
1311 MCLGET(m, M_WAITOK);
1314 md = mtod(m, void *);
1315 while (buflen > 0) {
1316 error = copyin(buf, md, sizeof(struct cmsghdr));
1319 msglen = *(u_int *)md;
1320 msglen = FREEBSD32_ALIGN(msglen);
1322 /* Modify the message length to account for alignment. */
1323 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1324 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1326 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1327 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1328 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1330 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1332 error = copyin(buf, md, msglen);
1335 md = (char *)md + CMSG_ALIGN(msglen);
1349 freebsd32_sendmsg(struct thread *td,
1350 struct freebsd32_sendmsg_args *uap)
1353 struct msghdr32 m32;
1355 struct mbuf *control = NULL;
1356 struct sockaddr *to = NULL;
1359 error = copyin(uap->msg, &m32, sizeof(m32));
1362 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1365 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1370 if (msg.msg_name != NULL) {
1371 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1379 if (msg.msg_control) {
1380 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1385 error = freebsd32_copyin_control(&control, msg.msg_control,
1386 msg.msg_controllen);
1390 msg.msg_control = NULL;
1391 msg.msg_controllen = 0;
1394 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1405 freebsd32_recvfrom(struct thread *td,
1406 struct freebsd32_recvfrom_args *uap)
1412 if (uap->fromlenaddr) {
1413 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1414 sizeof(msg.msg_namelen));
1418 msg.msg_namelen = 0;
1421 msg.msg_name = PTRIN(uap->from);
1422 msg.msg_iov = &aiov;
1424 aiov.iov_base = PTRIN(uap->buf);
1425 aiov.iov_len = uap->len;
1426 msg.msg_control = NULL;
1427 msg.msg_flags = uap->flags;
1428 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1429 if (error == 0 && uap->fromlenaddr)
1430 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1431 sizeof (msg.msg_namelen));
1436 freebsd32_settimeofday(struct thread *td,
1437 struct freebsd32_settimeofday_args *uap)
1439 struct timeval32 tv32;
1440 struct timeval tv, *tvp;
1441 struct timezone tz, *tzp;
1445 error = copyin(uap->tv, &tv32, sizeof(tv32));
1448 CP(tv32, tv, tv_sec);
1449 CP(tv32, tv, tv_usec);
1454 error = copyin(uap->tzp, &tz, sizeof(tz));
1460 return (kern_settimeofday(td, tvp, tzp));
1464 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1466 struct timeval32 s32[2];
1467 struct timeval s[2], *sp;
1470 if (uap->tptr != NULL) {
1471 error = copyin(uap->tptr, s32, sizeof(s32));
1474 CP(s32[0], s[0], tv_sec);
1475 CP(s32[0], s[0], tv_usec);
1476 CP(s32[1], s[1], tv_sec);
1477 CP(s32[1], s[1], tv_usec);
1481 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1486 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1488 struct timeval32 s32[2];
1489 struct timeval s[2], *sp;
1492 if (uap->tptr != NULL) {
1493 error = copyin(uap->tptr, s32, sizeof(s32));
1496 CP(s32[0], s[0], tv_sec);
1497 CP(s32[0], s[0], tv_usec);
1498 CP(s32[1], s[1], tv_sec);
1499 CP(s32[1], s[1], tv_usec);
1503 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1507 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1509 struct timeval32 s32[2];
1510 struct timeval s[2], *sp;
1513 if (uap->tptr != NULL) {
1514 error = copyin(uap->tptr, s32, sizeof(s32));
1517 CP(s32[0], s[0], tv_sec);
1518 CP(s32[0], s[0], tv_usec);
1519 CP(s32[1], s[1], tv_sec);
1520 CP(s32[1], s[1], tv_usec);
1524 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1528 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1530 struct timeval32 s32[2];
1531 struct timeval s[2], *sp;
1534 if (uap->times != NULL) {
1535 error = copyin(uap->times, s32, sizeof(s32));
1538 CP(s32[0], s[0], tv_sec);
1539 CP(s32[0], s[0], tv_usec);
1540 CP(s32[1], s[1], tv_sec);
1541 CP(s32[1], s[1], tv_usec);
1545 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1550 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1552 struct timespec32 ts32[2];
1553 struct timespec ts[2], *tsp;
1556 if (uap->times != NULL) {
1557 error = copyin(uap->times, ts32, sizeof(ts32));
1560 CP(ts32[0], ts[0], tv_sec);
1561 CP(ts32[0], ts[0], tv_nsec);
1562 CP(ts32[1], ts[1], tv_sec);
1563 CP(ts32[1], ts[1], tv_nsec);
1567 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1571 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1573 struct timespec32 ts32[2];
1574 struct timespec ts[2], *tsp;
1577 if (uap->times != NULL) {
1578 error = copyin(uap->times, ts32, sizeof(ts32));
1581 CP(ts32[0], ts[0], tv_sec);
1582 CP(ts32[0], ts[0], tv_nsec);
1583 CP(ts32[1], ts[1], tv_sec);
1584 CP(ts32[1], ts[1], tv_nsec);
1588 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1589 tsp, UIO_SYSSPACE, uap->flag));
1593 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1595 struct timeval32 tv32;
1596 struct timeval delta, olddelta, *deltap;
1600 error = copyin(uap->delta, &tv32, sizeof(tv32));
1603 CP(tv32, delta, tv_sec);
1604 CP(tv32, delta, tv_usec);
1608 error = kern_adjtime(td, deltap, &olddelta);
1609 if (uap->olddelta && error == 0) {
1610 CP(olddelta, tv32, tv_sec);
1611 CP(olddelta, tv32, tv_usec);
1612 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1617 #ifdef COMPAT_FREEBSD4
1619 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1621 struct statfs32 s32;
1625 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1626 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1628 copy_statfs(sp, &s32);
1629 error = copyout(&s32, uap->buf, sizeof(s32));
1636 #ifdef COMPAT_FREEBSD4
1638 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1640 struct statfs32 s32;
1644 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1645 error = kern_fstatfs(td, uap->fd, sp);
1647 copy_statfs(sp, &s32);
1648 error = copyout(&s32, uap->buf, sizeof(s32));
1655 #ifdef COMPAT_FREEBSD4
1657 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1659 struct statfs32 s32;
1664 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1666 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1667 error = kern_fhstatfs(td, fh, sp);
1669 copy_statfs(sp, &s32);
1670 error = copyout(&s32, uap->buf, sizeof(s32));
1678 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1681 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1682 PAIR32TO64(off_t, uap->offset)));
1686 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1689 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1690 PAIR32TO64(off_t, uap->offset)));
1695 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1698 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1703 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1708 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1710 /* Expand the quad return into two parts for eax and edx */
1711 pos = td->td_uretoff.tdu_off;
1712 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1713 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1718 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1721 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1722 PAIR32TO64(off_t, uap->length)));
1726 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1729 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1734 ofreebsd32_getdirentries(struct thread *td,
1735 struct ofreebsd32_getdirentries_args *uap)
1737 struct ogetdirentries_args ap;
1744 ap.count = uap->count;
1746 error = kern_ogetdirentries(td, &ap, &loff);
1749 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1755 #if defined(COMPAT_FREEBSD11)
1757 freebsd11_freebsd32_getdirentries(struct thread *td,
1758 struct freebsd11_freebsd32_getdirentries_args *uap)
1764 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1768 if (uap->basep != NULL) {
1770 error = copyout(&base32, uap->basep, sizeof(int32_t));
1776 freebsd11_freebsd32_getdents(struct thread *td,
1777 struct freebsd11_freebsd32_getdents_args *uap)
1779 struct freebsd11_freebsd32_getdirentries_args ap;
1783 ap.count = uap->count;
1785 return (freebsd11_freebsd32_getdirentries(td, &ap));
1787 #endif /* COMPAT_FREEBSD11 */
1790 freebsd32_getdirentries(struct thread *td,
1791 struct freebsd32_getdirentries_args *uap)
1797 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
1798 NULL, UIO_USERSPACE);
1801 if (uap->basep != NULL) {
1803 error = copyout(&base32, uap->basep, sizeof(int32_t));
1808 #ifdef COMPAT_FREEBSD6
1809 /* versions with the 'int pad' argument */
1811 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1814 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1815 PAIR32TO64(off_t, uap->offset)));
1819 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1822 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1823 PAIR32TO64(off_t, uap->offset)));
1827 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1832 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1834 /* Expand the quad return into two parts for eax and edx */
1835 pos = *(off_t *)(td->td_retval);
1836 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1837 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1842 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1845 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1846 PAIR32TO64(off_t, uap->length)));
1850 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1853 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1855 #endif /* COMPAT_FREEBSD6 */
1865 freebsd32_do_sendfile(struct thread *td,
1866 struct freebsd32_sendfile_args *uap, int compat)
1868 struct sf_hdtr32 hdtr32;
1869 struct sf_hdtr hdtr;
1870 struct uio *hdr_uio, *trl_uio;
1872 cap_rights_t rights;
1873 struct iovec32 *iov32;
1874 off_t offset, sbytes;
1877 offset = PAIR32TO64(off_t, uap->offset);
1881 hdr_uio = trl_uio = NULL;
1883 if (uap->hdtr != NULL) {
1884 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1887 PTRIN_CP(hdtr32, hdtr, headers);
1888 CP(hdtr32, hdtr, hdr_cnt);
1889 PTRIN_CP(hdtr32, hdtr, trailers);
1890 CP(hdtr32, hdtr, trl_cnt);
1892 if (hdtr.headers != NULL) {
1893 iov32 = PTRIN(hdtr32.headers);
1894 error = freebsd32_copyinuio(iov32,
1895 hdtr32.hdr_cnt, &hdr_uio);
1898 #ifdef COMPAT_FREEBSD4
1900 * In FreeBSD < 5.0 the nbytes to send also included
1901 * the header. If compat is specified subtract the
1902 * header size from nbytes.
1905 if (uap->nbytes > hdr_uio->uio_resid)
1906 uap->nbytes -= hdr_uio->uio_resid;
1912 if (hdtr.trailers != NULL) {
1913 iov32 = PTRIN(hdtr32.trailers);
1914 error = freebsd32_copyinuio(iov32,
1915 hdtr32.trl_cnt, &trl_uio);
1921 AUDIT_ARG_FD(uap->fd);
1923 if ((error = fget_read(td, uap->fd,
1924 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1927 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1928 uap->nbytes, &sbytes, uap->flags, td);
1931 if (uap->sbytes != NULL)
1932 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1936 free(hdr_uio, M_IOV);
1938 free(trl_uio, M_IOV);
1942 #ifdef COMPAT_FREEBSD4
1944 freebsd4_freebsd32_sendfile(struct thread *td,
1945 struct freebsd4_freebsd32_sendfile_args *uap)
1947 return (freebsd32_do_sendfile(td,
1948 (struct freebsd32_sendfile_args *)uap, 1));
1953 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1956 return (freebsd32_do_sendfile(td, uap, 0));
1960 copy_stat(struct stat *in, struct stat32 *out)
1963 CP(*in, *out, st_dev);
1964 CP(*in, *out, st_ino);
1965 CP(*in, *out, st_mode);
1966 CP(*in, *out, st_nlink);
1967 CP(*in, *out, st_uid);
1968 CP(*in, *out, st_gid);
1969 CP(*in, *out, st_rdev);
1970 TS_CP(*in, *out, st_atim);
1971 TS_CP(*in, *out, st_mtim);
1972 TS_CP(*in, *out, st_ctim);
1973 CP(*in, *out, st_size);
1974 CP(*in, *out, st_blocks);
1975 CP(*in, *out, st_blksize);
1976 CP(*in, *out, st_flags);
1977 CP(*in, *out, st_gen);
1978 TS_CP(*in, *out, st_birthtim);
1979 out->st_padding0 = 0;
1980 out->st_padding1 = 0;
1981 #ifdef __STAT32_TIME_T_EXT
1982 out->st_atim_ext = 0;
1983 out->st_mtim_ext = 0;
1984 out->st_ctim_ext = 0;
1985 out->st_btim_ext = 0;
1987 bzero(out->st_spare, sizeof(out->st_spare));
1992 copy_ostat(struct stat *in, struct ostat32 *out)
1995 CP(*in, *out, st_dev);
1996 CP(*in, *out, st_ino);
1997 CP(*in, *out, st_mode);
1998 CP(*in, *out, st_nlink);
1999 CP(*in, *out, st_uid);
2000 CP(*in, *out, st_gid);
2001 CP(*in, *out, st_rdev);
2002 CP(*in, *out, st_size);
2003 TS_CP(*in, *out, st_atim);
2004 TS_CP(*in, *out, st_mtim);
2005 TS_CP(*in, *out, st_ctim);
2006 CP(*in, *out, st_blksize);
2007 CP(*in, *out, st_blocks);
2008 CP(*in, *out, st_flags);
2009 CP(*in, *out, st_gen);
2015 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
2018 struct ostat32 sb32;
2021 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2025 copy_ostat(&sb, &sb32);
2026 error = copyout(&sb32, uap->ub, sizeof (sb32));
2032 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
2038 error = kern_fstat(td, uap->fd, &ub);
2041 copy_stat(&ub, &ub32);
2042 error = copyout(&ub32, uap->ub, sizeof(ub32));
2048 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2051 struct ostat32 ub32;
2054 error = kern_fstat(td, uap->fd, &ub);
2057 copy_ostat(&ub, &ub32);
2058 error = copyout(&ub32, uap->ub, sizeof(ub32));
2064 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2070 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2074 copy_stat(&ub, &ub32);
2075 error = copyout(&ub32, uap->buf, sizeof(ub32));
2081 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2084 struct ostat32 sb32;
2087 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2088 UIO_USERSPACE, &sb, NULL);
2091 copy_ostat(&sb, &sb32);
2092 error = copyout(&sb32, uap->ub, sizeof (sb32));
2098 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2105 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2108 error = kern_fhstat(td, fh, &sb);
2111 copy_stat(&sb, &sb32);
2112 error = copyout(&sb32, uap->sb, sizeof (sb32));
2116 #if defined(COMPAT_FREEBSD11)
2117 extern int ino64_trunc_error;
2120 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2123 CP(*in, *out, st_ino);
2124 if (in->st_ino != out->st_ino) {
2125 switch (ino64_trunc_error) {
2132 out->st_ino = UINT32_MAX;
2136 CP(*in, *out, st_nlink);
2137 if (in->st_nlink != out->st_nlink) {
2138 switch (ino64_trunc_error) {
2145 out->st_nlink = UINT16_MAX;
2149 CP(*in, *out, st_dev);
2150 CP(*in, *out, st_mode);
2151 CP(*in, *out, st_uid);
2152 CP(*in, *out, st_gid);
2153 CP(*in, *out, st_rdev);
2154 TS_CP(*in, *out, st_atim);
2155 TS_CP(*in, *out, st_mtim);
2156 TS_CP(*in, *out, st_ctim);
2157 CP(*in, *out, st_size);
2158 CP(*in, *out, st_blocks);
2159 CP(*in, *out, st_blksize);
2160 CP(*in, *out, st_flags);
2161 CP(*in, *out, st_gen);
2162 TS_CP(*in, *out, st_birthtim);
2164 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2165 sizeof(*out) - offsetof(struct freebsd11_stat32,
2166 st_birthtim) - sizeof(out->st_birthtim));
2171 freebsd11_freebsd32_stat(struct thread *td,
2172 struct freebsd11_freebsd32_stat_args *uap)
2175 struct freebsd11_stat32 sb32;
2178 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2182 error = freebsd11_cvtstat32(&sb, &sb32);
2184 error = copyout(&sb32, uap->ub, sizeof (sb32));
2189 freebsd11_freebsd32_fstat(struct thread *td,
2190 struct freebsd11_freebsd32_fstat_args *uap)
2193 struct freebsd11_stat32 sb32;
2196 error = kern_fstat(td, uap->fd, &sb);
2199 error = freebsd11_cvtstat32(&sb, &sb32);
2201 error = copyout(&sb32, uap->ub, sizeof (sb32));
2206 freebsd11_freebsd32_fstatat(struct thread *td,
2207 struct freebsd11_freebsd32_fstatat_args *uap)
2210 struct freebsd11_stat32 sb32;
2213 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2217 error = freebsd11_cvtstat32(&sb, &sb32);
2219 error = copyout(&sb32, uap->buf, sizeof (sb32));
2224 freebsd11_freebsd32_lstat(struct thread *td,
2225 struct freebsd11_freebsd32_lstat_args *uap)
2228 struct freebsd11_stat32 sb32;
2231 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2232 UIO_USERSPACE, &sb, NULL);
2235 error = freebsd11_cvtstat32(&sb, &sb32);
2237 error = copyout(&sb32, uap->ub, sizeof (sb32));
2242 freebsd11_freebsd32_fhstat(struct thread *td,
2243 struct freebsd11_freebsd32_fhstat_args *uap)
2246 struct freebsd11_stat32 sb32;
2250 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2253 error = kern_fhstat(td, fh, &sb);
2256 error = freebsd11_cvtstat32(&sb, &sb32);
2258 error = copyout(&sb32, uap->sb, sizeof (sb32));
2264 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2266 int error, name[CTL_MAXNAME];
2270 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2272 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2276 error = fueword32(uap->oldlenp, &tmp);
2283 error = userland_sysctl(td, name, uap->namelen,
2284 uap->old, &oldlen, 1,
2285 uap->new, uap->newlen, &j, SCTL_MASK32);
2286 if (error && error != ENOMEM)
2289 suword32(uap->oldlenp, j);
2294 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2300 error = copyin(uap->jail, &version, sizeof(uint32_t));
2307 /* FreeBSD single IPv4 jails. */
2308 struct jail32_v0 j32_v0;
2310 bzero(&j, sizeof(struct jail));
2311 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2314 CP(j32_v0, j, version);
2315 PTRIN_CP(j32_v0, j, path);
2316 PTRIN_CP(j32_v0, j, hostname);
2317 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2323 * Version 1 was used by multi-IPv4 jail implementations
2324 * that never made it into the official kernel.
2328 case 2: /* JAIL_API_VERSION */
2330 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2333 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2336 CP(j32, j, version);
2337 PTRIN_CP(j32, j, path);
2338 PTRIN_CP(j32, j, hostname);
2339 PTRIN_CP(j32, j, jailname);
2342 PTRIN_CP(j32, j, ip4);
2343 PTRIN_CP(j32, j, ip6);
2348 /* Sci-Fi jails are not supported, sorry. */
2351 return (kern_jail(td, &j));
2355 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2360 /* Check that we have an even number of iovecs. */
2361 if (uap->iovcnt & 1)
2364 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2367 error = kern_jail_set(td, auio, uap->flags);
2373 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2375 struct iovec32 iov32;
2379 /* Check that we have an even number of iovecs. */
2380 if (uap->iovcnt & 1)
2383 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2386 error = kern_jail_get(td, auio, uap->flags);
2388 for (i = 0; i < uap->iovcnt; i++) {
2389 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2390 CP(auio->uio_iov[i], iov32, iov_len);
2391 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2400 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2402 struct sigaction32 s32;
2403 struct sigaction sa, osa, *sap;
2407 error = copyin(uap->act, &s32, sizeof(s32));
2410 sa.sa_handler = PTRIN(s32.sa_u);
2411 CP(s32, sa, sa_flags);
2412 CP(s32, sa, sa_mask);
2416 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2417 if (error == 0 && uap->oact != NULL) {
2418 s32.sa_u = PTROUT(osa.sa_handler);
2419 CP(osa, s32, sa_flags);
2420 CP(osa, s32, sa_mask);
2421 error = copyout(&s32, uap->oact, sizeof(s32));
2426 #ifdef COMPAT_FREEBSD4
2428 freebsd4_freebsd32_sigaction(struct thread *td,
2429 struct freebsd4_freebsd32_sigaction_args *uap)
2431 struct sigaction32 s32;
2432 struct sigaction sa, osa, *sap;
2436 error = copyin(uap->act, &s32, sizeof(s32));
2439 sa.sa_handler = PTRIN(s32.sa_u);
2440 CP(s32, sa, sa_flags);
2441 CP(s32, sa, sa_mask);
2445 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2446 if (error == 0 && uap->oact != NULL) {
2447 s32.sa_u = PTROUT(osa.sa_handler);
2448 CP(osa, s32, sa_flags);
2449 CP(osa, s32, sa_mask);
2450 error = copyout(&s32, uap->oact, sizeof(s32));
2457 struct osigaction32 {
2466 ofreebsd32_sigaction(struct thread *td,
2467 struct ofreebsd32_sigaction_args *uap)
2469 struct osigaction32 s32;
2470 struct sigaction sa, osa, *sap;
2473 if (uap->signum <= 0 || uap->signum >= ONSIG)
2477 error = copyin(uap->nsa, &s32, sizeof(s32));
2480 sa.sa_handler = PTRIN(s32.sa_u);
2481 CP(s32, sa, sa_flags);
2482 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2486 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2487 if (error == 0 && uap->osa != NULL) {
2488 s32.sa_u = PTROUT(osa.sa_handler);
2489 CP(osa, s32, sa_flags);
2490 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2491 error = copyout(&s32, uap->osa, sizeof(s32));
2497 ofreebsd32_sigprocmask(struct thread *td,
2498 struct ofreebsd32_sigprocmask_args *uap)
2503 OSIG2SIG(uap->mask, set);
2504 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2505 SIG2OSIG(oset, td->td_retval[0]);
2510 ofreebsd32_sigpending(struct thread *td,
2511 struct ofreebsd32_sigpending_args *uap)
2513 struct proc *p = td->td_proc;
2517 siglist = p->p_siglist;
2518 SIGSETOR(siglist, td->td_siglist);
2520 SIG2OSIG(siglist, td->td_retval[0]);
2525 u_int32_t sv_handler;
2531 ofreebsd32_sigvec(struct thread *td,
2532 struct ofreebsd32_sigvec_args *uap)
2534 struct sigvec32 vec;
2535 struct sigaction sa, osa, *sap;
2538 if (uap->signum <= 0 || uap->signum >= ONSIG)
2542 error = copyin(uap->nsv, &vec, sizeof(vec));
2545 sa.sa_handler = PTRIN(vec.sv_handler);
2546 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2547 sa.sa_flags = vec.sv_flags;
2548 sa.sa_flags ^= SA_RESTART;
2552 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2553 if (error == 0 && uap->osv != NULL) {
2554 vec.sv_handler = PTROUT(osa.sa_handler);
2555 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2556 vec.sv_flags = osa.sa_flags;
2557 vec.sv_flags &= ~SA_NOCLDWAIT;
2558 vec.sv_flags ^= SA_RESTART;
2559 error = copyout(&vec, uap->osv, sizeof(vec));
2565 ofreebsd32_sigblock(struct thread *td,
2566 struct ofreebsd32_sigblock_args *uap)
2570 OSIG2SIG(uap->mask, set);
2571 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2572 SIG2OSIG(oset, td->td_retval[0]);
2577 ofreebsd32_sigsetmask(struct thread *td,
2578 struct ofreebsd32_sigsetmask_args *uap)
2582 OSIG2SIG(uap->mask, set);
2583 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2584 SIG2OSIG(oset, td->td_retval[0]);
2589 ofreebsd32_sigsuspend(struct thread *td,
2590 struct ofreebsd32_sigsuspend_args *uap)
2594 OSIG2SIG(uap->mask, mask);
2595 return (kern_sigsuspend(td, mask));
2604 ofreebsd32_sigstack(struct thread *td,
2605 struct ofreebsd32_sigstack_args *uap)
2607 struct sigstack32 s32;
2608 struct sigstack nss, oss;
2609 int error = 0, unss;
2611 if (uap->nss != NULL) {
2612 error = copyin(uap->nss, &s32, sizeof(s32));
2615 nss.ss_sp = PTRIN(s32.ss_sp);
2616 CP(s32, nss, ss_onstack);
2621 oss.ss_sp = td->td_sigstk.ss_sp;
2622 oss.ss_onstack = sigonstack(cpu_getstack(td));
2624 td->td_sigstk.ss_sp = nss.ss_sp;
2625 td->td_sigstk.ss_size = 0;
2626 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2627 td->td_pflags |= TDP_ALTSTACK;
2629 if (uap->oss != NULL) {
2630 s32.ss_sp = PTROUT(oss.ss_sp);
2631 CP(oss, s32, ss_onstack);
2632 error = copyout(&s32, uap->oss, sizeof(s32));
2639 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2642 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2643 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2647 freebsd32_clock_nanosleep(struct thread *td,
2648 struct freebsd32_clock_nanosleep_args *uap)
2652 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2653 uap->rqtp, uap->rmtp);
2654 return (kern_posix_error(td, error));
2658 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2659 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2661 struct timespec32 rmt32, rqt32;
2662 struct timespec rmt, rqt;
2665 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2669 CP(rqt32, rqt, tv_sec);
2670 CP(rqt32, rqt, tv_nsec);
2672 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2673 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2675 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2676 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2679 CP(rmt, rmt32, tv_sec);
2680 CP(rmt, rmt32, tv_nsec);
2682 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2690 freebsd32_clock_gettime(struct thread *td,
2691 struct freebsd32_clock_gettime_args *uap)
2693 struct timespec ats;
2694 struct timespec32 ats32;
2697 error = kern_clock_gettime(td, uap->clock_id, &ats);
2699 CP(ats, ats32, tv_sec);
2700 CP(ats, ats32, tv_nsec);
2701 error = copyout(&ats32, uap->tp, sizeof(ats32));
2707 freebsd32_clock_settime(struct thread *td,
2708 struct freebsd32_clock_settime_args *uap)
2710 struct timespec ats;
2711 struct timespec32 ats32;
2714 error = copyin(uap->tp, &ats32, sizeof(ats32));
2717 CP(ats32, ats, tv_sec);
2718 CP(ats32, ats, tv_nsec);
2720 return (kern_clock_settime(td, uap->clock_id, &ats));
2724 freebsd32_clock_getres(struct thread *td,
2725 struct freebsd32_clock_getres_args *uap)
2728 struct timespec32 ts32;
2731 if (uap->tp == NULL)
2733 error = kern_clock_getres(td, uap->clock_id, &ts);
2735 CP(ts, ts32, tv_sec);
2736 CP(ts, ts32, tv_nsec);
2737 error = copyout(&ts32, uap->tp, sizeof(ts32));
2742 int freebsd32_ktimer_create(struct thread *td,
2743 struct freebsd32_ktimer_create_args *uap)
2745 struct sigevent32 ev32;
2746 struct sigevent ev, *evp;
2749 if (uap->evp == NULL) {
2753 error = copyin(uap->evp, &ev32, sizeof(ev32));
2756 error = convert_sigevent32(&ev32, &ev);
2760 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2762 error = copyout(&id, uap->timerid, sizeof(int));
2764 kern_ktimer_delete(td, id);
2770 freebsd32_ktimer_settime(struct thread *td,
2771 struct freebsd32_ktimer_settime_args *uap)
2773 struct itimerspec32 val32, oval32;
2774 struct itimerspec val, oval, *ovalp;
2777 error = copyin(uap->value, &val32, sizeof(val32));
2781 ovalp = uap->ovalue != NULL ? &oval : NULL;
2782 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2783 if (error == 0 && uap->ovalue != NULL) {
2784 ITS_CP(oval, oval32);
2785 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2791 freebsd32_ktimer_gettime(struct thread *td,
2792 struct freebsd32_ktimer_gettime_args *uap)
2794 struct itimerspec32 val32;
2795 struct itimerspec val;
2798 error = kern_ktimer_gettime(td, uap->timerid, &val);
2801 error = copyout(&val32, uap->value, sizeof(val32));
2807 freebsd32_clock_getcpuclockid2(struct thread *td,
2808 struct freebsd32_clock_getcpuclockid2_args *uap)
2813 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2814 uap->which, &clk_id);
2816 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2821 freebsd32_thr_new(struct thread *td,
2822 struct freebsd32_thr_new_args *uap)
2824 struct thr_param32 param32;
2825 struct thr_param param;
2828 if (uap->param_size < 0 ||
2829 uap->param_size > sizeof(struct thr_param32))
2831 bzero(¶m, sizeof(struct thr_param));
2832 bzero(¶m32, sizeof(struct thr_param32));
2833 error = copyin(uap->param, ¶m32, uap->param_size);
2836 param.start_func = PTRIN(param32.start_func);
2837 param.arg = PTRIN(param32.arg);
2838 param.stack_base = PTRIN(param32.stack_base);
2839 param.stack_size = param32.stack_size;
2840 param.tls_base = PTRIN(param32.tls_base);
2841 param.tls_size = param32.tls_size;
2842 param.child_tid = PTRIN(param32.child_tid);
2843 param.parent_tid = PTRIN(param32.parent_tid);
2844 param.flags = param32.flags;
2845 param.rtp = PTRIN(param32.rtp);
2846 param.spare[0] = PTRIN(param32.spare[0]);
2847 param.spare[1] = PTRIN(param32.spare[1]);
2848 param.spare[2] = PTRIN(param32.spare[2]);
2850 return (kern_thr_new(td, ¶m));
2854 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2856 struct timespec32 ts32;
2857 struct timespec ts, *tsp;
2862 if (uap->timeout != NULL) {
2863 error = copyin((const void *)uap->timeout, (void *)&ts32,
2864 sizeof(struct timespec32));
2867 ts.tv_sec = ts32.tv_sec;
2868 ts.tv_nsec = ts32.tv_nsec;
2871 return (kern_thr_suspend(td, tsp));
2875 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2877 bzero(dst, sizeof(*dst));
2878 dst->si_signo = src->si_signo;
2879 dst->si_errno = src->si_errno;
2880 dst->si_code = src->si_code;
2881 dst->si_pid = src->si_pid;
2882 dst->si_uid = src->si_uid;
2883 dst->si_status = src->si_status;
2884 dst->si_addr = (uintptr_t)src->si_addr;
2885 dst->si_value.sival_int = src->si_value.sival_int;
2886 dst->si_timerid = src->si_timerid;
2887 dst->si_overrun = src->si_overrun;
2890 #ifndef _FREEBSD32_SYSPROTO_H_
2891 struct freebsd32_sigqueue_args {
2894 /* union sigval32 */ int value;
2898 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2903 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2904 * On 64-bit little-endian ABIs, the low bits are the same.
2905 * In 64-bit big-endian ABIs, sival_int overlaps with
2906 * sival_ptr's HIGH bits. We choose to support sival_int
2907 * rather than sival_ptr in this case as it seems to be
2910 bzero(&sv, sizeof(sv));
2911 sv.sival_int = uap->value;
2913 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2917 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2919 struct timespec32 ts32;
2921 struct timespec *timeout;
2924 struct siginfo32 si32;
2928 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2931 ts.tv_sec = ts32.tv_sec;
2932 ts.tv_nsec = ts32.tv_nsec;
2937 error = copyin(uap->set, &set, sizeof(set));
2941 error = kern_sigtimedwait(td, set, &ksi, timeout);
2946 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2947 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2951 td->td_retval[0] = ksi.ksi_signo;
2959 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2962 struct siginfo32 si32;
2966 error = copyin(uap->set, &set, sizeof(set));
2970 error = kern_sigtimedwait(td, set, &ksi, NULL);
2975 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2976 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2979 td->td_retval[0] = ksi.ksi_signo;
2984 freebsd32_cpuset_setid(struct thread *td,
2985 struct freebsd32_cpuset_setid_args *uap)
2988 return (kern_cpuset_setid(td, uap->which,
2989 PAIR32TO64(id_t, uap->id), uap->setid));
2993 freebsd32_cpuset_getid(struct thread *td,
2994 struct freebsd32_cpuset_getid_args *uap)
2997 return (kern_cpuset_getid(td, uap->level, uap->which,
2998 PAIR32TO64(id_t, uap->id), uap->setid));
3002 freebsd32_cpuset_getaffinity(struct thread *td,
3003 struct freebsd32_cpuset_getaffinity_args *uap)
3006 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
3007 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3011 freebsd32_cpuset_setaffinity(struct thread *td,
3012 struct freebsd32_cpuset_setaffinity_args *uap)
3015 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
3016 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3020 freebsd32_cpuset_getdomain(struct thread *td,
3021 struct freebsd32_cpuset_getdomain_args *uap)
3024 return (kern_cpuset_getdomain(td, uap->level, uap->which,
3025 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3029 freebsd32_cpuset_setdomain(struct thread *td,
3030 struct freebsd32_cpuset_setdomain_args *uap)
3033 return (kern_cpuset_setdomain(td, uap->level, uap->which,
3034 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3038 freebsd32_nmount(struct thread *td,
3039 struct freebsd32_nmount_args /* {
3041 unsigned int iovcnt;
3050 * Mount flags are now 64-bits. On 32-bit archtectures only
3051 * 32-bits are passed in, but from here on everything handles
3052 * 64-bit flags correctly.
3056 AUDIT_ARG_FFLAGS(flags);
3059 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
3060 * userspace to set this flag, but we must filter it out if we want
3061 * MNT_UPDATE on the root file system to work.
3062 * MNT_ROOTFS should only be set by the kernel when mounting its
3065 flags &= ~MNT_ROOTFS;
3068 * check that we have an even number of iovec's
3069 * and that we have at least two options.
3071 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3074 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3077 error = vfs_donmount(td, flags, auio);
3085 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3087 struct yyy32 *p32, s32;
3088 struct yyy *p = NULL, s;
3093 error = copyin(uap->zzz, &s32, sizeof(s32));
3099 error = kern_xxx(td, p);
3104 error = copyout(&s32, p32, sizeof(s32));
3111 syscall32_register(int *offset, struct sysent *new_sysent,
3112 struct sysent *old_sysent, int flags)
3115 if ((flags & ~SY_THR_STATIC) != 0)
3118 if (*offset == NO_SYSCALL) {
3121 for (i = 1; i < SYS_MAXSYSCALL; ++i)
3122 if (freebsd32_sysent[i].sy_call ==
3123 (sy_call_t *)lkmnosys)
3125 if (i == SYS_MAXSYSCALL)
3128 } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
3130 else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
3131 freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
3134 *old_sysent = freebsd32_sysent[*offset];
3135 freebsd32_sysent[*offset] = *new_sysent;
3136 atomic_store_rel_32(&freebsd32_sysent[*offset].sy_thrcnt, flags);
3141 syscall32_deregister(int *offset, struct sysent *old_sysent)
3147 freebsd32_sysent[*offset] = *old_sysent;
3152 syscall32_module_handler(struct module *mod, int what, void *arg)
3154 struct syscall_module_data *data = (struct syscall_module_data*)arg;
3160 error = syscall32_register(data->offset, data->new_sysent,
3161 &data->old_sysent, SY_THR_STATIC_KLD);
3163 /* Leave a mark so we know to safely unload below. */
3164 data->offset = NULL;
3167 ms.intval = *data->offset;
3169 module_setspecific(mod, &ms);
3172 error = data->chainevh(mod, what, data->chainarg);
3176 * MOD_LOAD failed, so just return without calling the
3177 * chained handler since we didn't pass along the MOD_LOAD
3180 if (data->offset == NULL)
3182 if (data->chainevh) {
3183 error = data->chainevh(mod, what, data->chainarg);
3187 error = syscall32_deregister(data->offset, &data->old_sysent);
3192 error = data->chainevh(mod, what, data->chainarg);
3198 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3200 struct syscall_helper_data *sd1;
3203 for (sd1 = sd; sd1->syscall_no != NO_SYSCALL; sd1++) {
3204 error = syscall32_register(&sd1->syscall_no, &sd1->new_sysent,
3205 &sd1->old_sysent, flags);
3207 syscall32_helper_unregister(sd);
3210 sd1->registered = 1;
3216 syscall32_helper_unregister(struct syscall_helper_data *sd)
3218 struct syscall_helper_data *sd1;
3220 for (sd1 = sd; sd1->registered != 0; sd1++) {
3221 syscall32_deregister(&sd1->syscall_no, &sd1->old_sysent);
3222 sd1->registered = 0;
3228 freebsd32_copyout_strings(struct image_params *imgp)
3234 u_int32_t *stack_base;
3235 struct freebsd32_ps_strings *arginfo;
3236 char canary[sizeof(long) * 8];
3237 int32_t pagesizes32[MAXPAGESIZES];
3238 size_t execpath_len;
3242 * Calculate string base and vector table pointers.
3243 * Also deal with signal trampoline code for this exec type.
3245 if (imgp->execpath != NULL && imgp->auxargs != NULL)
3246 execpath_len = strlen(imgp->execpath) + 1;
3249 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3251 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3252 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3255 destp = (uintptr_t)arginfo;
3260 if (szsigcode != 0) {
3262 destp = rounddown2(destp, sizeof(uint32_t));
3263 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3268 * Copy the image path for the rtld.
3270 if (execpath_len != 0) {
3271 destp -= execpath_len;
3272 imgp->execpathp = destp;
3273 copyout(imgp->execpath, (void *)destp, execpath_len);
3277 * Prepare the canary for SSP.
3279 arc4rand(canary, sizeof(canary), 0);
3280 destp -= sizeof(canary);
3281 imgp->canary = destp;
3282 copyout(canary, (void *)destp, sizeof(canary));
3283 imgp->canarylen = sizeof(canary);
3286 * Prepare the pagesizes array.
3288 for (i = 0; i < MAXPAGESIZES; i++)
3289 pagesizes32[i] = (uint32_t)pagesizes[i];
3290 destp -= sizeof(pagesizes32);
3291 destp = rounddown2(destp, sizeof(uint32_t));
3292 imgp->pagesizes = destp;
3293 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3294 imgp->pagesizeslen = sizeof(pagesizes32);
3296 destp -= ARG_MAX - imgp->args->stringspace;
3297 destp = rounddown2(destp, sizeof(uint32_t));
3300 * If we have a valid auxargs ptr, prepare some room
3303 if (imgp->auxargs) {
3305 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
3306 * lower compatibility.
3308 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
3311 * The '+ 2' is for the null pointers at the end of each of
3312 * the arg and env vector sets,and imgp->auxarg_size is room
3313 * for argument of Runtime loader.
3315 vectp = (u_int32_t *) (destp - (imgp->args->argc +
3316 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
3320 * The '+ 2' is for the null pointers at the end of each of
3321 * the arg and env vector sets
3323 vectp = (u_int32_t *)(destp - (imgp->args->argc +
3324 imgp->args->envc + 2) * sizeof(u_int32_t));
3328 * vectp also becomes our initial stack base
3332 stringp = imgp->args->begin_argv;
3333 argc = imgp->args->argc;
3334 envc = imgp->args->envc;
3336 * Copy out strings - arguments and environment.
3338 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3341 * Fill in "ps_strings" struct for ps, w, etc.
3343 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3344 suword32(&arginfo->ps_nargvstr, argc);
3347 * Fill in argument portion of vector table.
3349 for (; argc > 0; --argc) {
3350 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3351 while (*stringp++ != 0)
3356 /* a null vector table pointer separates the argp's from the envp's */
3357 suword32(vectp++, 0);
3359 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3360 suword32(&arginfo->ps_nenvstr, envc);
3363 * Fill in environment portion of vector table.
3365 for (; envc > 0; --envc) {
3366 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3367 while (*stringp++ != 0)
3372 /* end of vector table is a null pointer */
3375 return ((register_t *)stack_base);
3379 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3381 struct kld_file_stat *stat;
3382 struct kld32_file_stat *stat32;
3385 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3388 if (version != sizeof(struct kld32_file_stat_1) &&
3389 version != sizeof(struct kld32_file_stat))
3392 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3393 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3394 error = kern_kldstat(td, uap->fileid, stat);
3396 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3397 CP(*stat, *stat32, refs);
3398 CP(*stat, *stat32, id);
3399 PTROUT_CP(*stat, *stat32, address);
3400 CP(*stat, *stat32, size);
3401 bcopy(&stat->pathname[0], &stat32->pathname[0],
3402 sizeof(stat->pathname));
3403 error = copyout(stat32, uap->stat, version);
3406 free(stat32, M_TEMP);
3411 freebsd32_posix_fallocate(struct thread *td,
3412 struct freebsd32_posix_fallocate_args *uap)
3416 error = kern_posix_fallocate(td, uap->fd,
3417 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3418 return (kern_posix_error(td, error));
3422 freebsd32_posix_fadvise(struct thread *td,
3423 struct freebsd32_posix_fadvise_args *uap)
3427 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3428 PAIR32TO64(off_t, uap->len), uap->advice);
3429 return (kern_posix_error(td, error));
3433 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3436 CP(*sig32, *sig, sigev_notify);
3437 switch (sig->sigev_notify) {
3440 case SIGEV_THREAD_ID:
3441 CP(*sig32, *sig, sigev_notify_thread_id);
3444 CP(*sig32, *sig, sigev_signo);
3445 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3448 CP(*sig32, *sig, sigev_notify_kqueue);
3449 CP(*sig32, *sig, sigev_notify_kevent_flags);
3450 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3459 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3463 struct procctl_reaper_status rs;
3464 struct procctl_reaper_pids rp;
3465 struct procctl_reaper_kill rk;
3468 struct procctl_reaper_pids32 rp;
3470 int error, error1, flags;
3474 case PROC_TRACE_CTL:
3475 case PROC_TRAPCAP_CTL:
3476 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3481 case PROC_REAP_ACQUIRE:
3482 case PROC_REAP_RELEASE:
3483 if (uap->data != NULL)
3487 case PROC_REAP_STATUS:
3490 case PROC_REAP_GETPIDS:
3491 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3494 CP(x32.rp, x.rp, rp_count);
3495 PTRIN_CP(x32.rp, x.rp, rp_pids);
3498 case PROC_REAP_KILL:
3499 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3504 case PROC_TRACE_STATUS:
3505 case PROC_TRAPCAP_STATUS:
3511 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3514 case PROC_REAP_STATUS:
3516 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3518 case PROC_REAP_KILL:
3519 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3523 case PROC_TRACE_STATUS:
3524 case PROC_TRAPCAP_STATUS:
3526 error = copyout(&flags, uap->data, sizeof(flags));
3533 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3539 * Do unsigned conversion for arg when operation
3540 * interprets it as flags or pointer.
3542 case F_SETLK_REMOTE:
3551 tmp = (unsigned int)(uap->arg);
3557 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3561 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3563 struct timespec32 ts32;
3564 struct timespec ts, *tsp;
3568 if (uap->ts != NULL) {
3569 error = copyin(uap->ts, &ts32, sizeof(ts32));
3572 CP(ts32, ts, tv_sec);
3573 CP(ts32, ts, tv_nsec);
3577 if (uap->set != NULL) {
3578 error = copyin(uap->set, &set, sizeof(set));
3585 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));