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 */
1789 #ifdef COMPAT_FREEBSD6
1790 /* versions with the 'int pad' argument */
1792 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1795 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1796 PAIR32TO64(off_t, uap->offset)));
1800 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1803 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1804 PAIR32TO64(off_t, uap->offset)));
1808 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1813 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1815 /* Expand the quad return into two parts for eax and edx */
1816 pos = *(off_t *)(td->td_retval);
1817 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1818 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1823 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1826 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1827 PAIR32TO64(off_t, uap->length)));
1831 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1834 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1836 #endif /* COMPAT_FREEBSD6 */
1846 freebsd32_do_sendfile(struct thread *td,
1847 struct freebsd32_sendfile_args *uap, int compat)
1849 struct sf_hdtr32 hdtr32;
1850 struct sf_hdtr hdtr;
1851 struct uio *hdr_uio, *trl_uio;
1853 cap_rights_t rights;
1854 struct iovec32 *iov32;
1855 off_t offset, sbytes;
1858 offset = PAIR32TO64(off_t, uap->offset);
1862 hdr_uio = trl_uio = NULL;
1864 if (uap->hdtr != NULL) {
1865 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1868 PTRIN_CP(hdtr32, hdtr, headers);
1869 CP(hdtr32, hdtr, hdr_cnt);
1870 PTRIN_CP(hdtr32, hdtr, trailers);
1871 CP(hdtr32, hdtr, trl_cnt);
1873 if (hdtr.headers != NULL) {
1874 iov32 = PTRIN(hdtr32.headers);
1875 error = freebsd32_copyinuio(iov32,
1876 hdtr32.hdr_cnt, &hdr_uio);
1879 #ifdef COMPAT_FREEBSD4
1881 * In FreeBSD < 5.0 the nbytes to send also included
1882 * the header. If compat is specified subtract the
1883 * header size from nbytes.
1886 if (uap->nbytes > hdr_uio->uio_resid)
1887 uap->nbytes -= hdr_uio->uio_resid;
1893 if (hdtr.trailers != NULL) {
1894 iov32 = PTRIN(hdtr32.trailers);
1895 error = freebsd32_copyinuio(iov32,
1896 hdtr32.trl_cnt, &trl_uio);
1902 AUDIT_ARG_FD(uap->fd);
1904 if ((error = fget_read(td, uap->fd,
1905 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1908 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1909 uap->nbytes, &sbytes, uap->flags, td);
1912 if (uap->sbytes != NULL)
1913 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1917 free(hdr_uio, M_IOV);
1919 free(trl_uio, M_IOV);
1923 #ifdef COMPAT_FREEBSD4
1925 freebsd4_freebsd32_sendfile(struct thread *td,
1926 struct freebsd4_freebsd32_sendfile_args *uap)
1928 return (freebsd32_do_sendfile(td,
1929 (struct freebsd32_sendfile_args *)uap, 1));
1934 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1937 return (freebsd32_do_sendfile(td, uap, 0));
1941 copy_stat(struct stat *in, struct stat32 *out)
1944 CP(*in, *out, st_dev);
1945 CP(*in, *out, st_ino);
1946 CP(*in, *out, st_mode);
1947 CP(*in, *out, st_nlink);
1948 CP(*in, *out, st_uid);
1949 CP(*in, *out, st_gid);
1950 CP(*in, *out, st_rdev);
1951 TS_CP(*in, *out, st_atim);
1952 TS_CP(*in, *out, st_mtim);
1953 TS_CP(*in, *out, st_ctim);
1954 CP(*in, *out, st_size);
1955 CP(*in, *out, st_blocks);
1956 CP(*in, *out, st_blksize);
1957 CP(*in, *out, st_flags);
1958 CP(*in, *out, st_gen);
1959 TS_CP(*in, *out, st_birthtim);
1960 out->st_padding0 = 0;
1961 out->st_padding1 = 0;
1962 #ifdef __STAT32_TIME_T_EXT
1963 out->st_atim_ext = 0;
1964 out->st_mtim_ext = 0;
1965 out->st_ctim_ext = 0;
1966 out->st_btim_ext = 0;
1968 bzero(out->st_spare, sizeof(out->st_spare));
1973 copy_ostat(struct stat *in, struct ostat32 *out)
1976 CP(*in, *out, st_dev);
1977 CP(*in, *out, st_ino);
1978 CP(*in, *out, st_mode);
1979 CP(*in, *out, st_nlink);
1980 CP(*in, *out, st_uid);
1981 CP(*in, *out, st_gid);
1982 CP(*in, *out, st_rdev);
1983 CP(*in, *out, st_size);
1984 TS_CP(*in, *out, st_atim);
1985 TS_CP(*in, *out, st_mtim);
1986 TS_CP(*in, *out, st_ctim);
1987 CP(*in, *out, st_blksize);
1988 CP(*in, *out, st_blocks);
1989 CP(*in, *out, st_flags);
1990 CP(*in, *out, st_gen);
1996 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
1999 struct ostat32 sb32;
2002 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2006 copy_ostat(&sb, &sb32);
2007 error = copyout(&sb32, uap->ub, sizeof (sb32));
2013 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
2019 error = kern_fstat(td, uap->fd, &ub);
2022 copy_stat(&ub, &ub32);
2023 error = copyout(&ub32, uap->ub, sizeof(ub32));
2029 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2032 struct ostat32 ub32;
2035 error = kern_fstat(td, uap->fd, &ub);
2038 copy_ostat(&ub, &ub32);
2039 error = copyout(&ub32, uap->ub, sizeof(ub32));
2045 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2051 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2055 copy_stat(&ub, &ub32);
2056 error = copyout(&ub32, uap->buf, sizeof(ub32));
2062 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2065 struct ostat32 sb32;
2068 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2069 UIO_USERSPACE, &sb, NULL);
2072 copy_ostat(&sb, &sb32);
2073 error = copyout(&sb32, uap->ub, sizeof (sb32));
2079 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2086 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2089 error = kern_fhstat(td, fh, &sb);
2092 copy_stat(&sb, &sb32);
2093 error = copyout(&sb32, uap->sb, sizeof (sb32));
2097 #if defined(COMPAT_FREEBSD11)
2098 extern int ino64_trunc_error;
2101 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2104 CP(*in, *out, st_ino);
2105 if (in->st_ino != out->st_ino) {
2106 switch (ino64_trunc_error) {
2113 out->st_ino = UINT32_MAX;
2117 CP(*in, *out, st_nlink);
2118 if (in->st_nlink != out->st_nlink) {
2119 switch (ino64_trunc_error) {
2126 out->st_nlink = UINT16_MAX;
2130 CP(*in, *out, st_dev);
2131 CP(*in, *out, st_mode);
2132 CP(*in, *out, st_uid);
2133 CP(*in, *out, st_gid);
2134 CP(*in, *out, st_rdev);
2135 TS_CP(*in, *out, st_atim);
2136 TS_CP(*in, *out, st_mtim);
2137 TS_CP(*in, *out, st_ctim);
2138 CP(*in, *out, st_size);
2139 CP(*in, *out, st_blocks);
2140 CP(*in, *out, st_blksize);
2141 CP(*in, *out, st_flags);
2142 CP(*in, *out, st_gen);
2143 TS_CP(*in, *out, st_birthtim);
2145 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2146 sizeof(*out) - offsetof(struct freebsd11_stat32,
2147 st_birthtim) - sizeof(out->st_birthtim));
2152 freebsd11_freebsd32_stat(struct thread *td,
2153 struct freebsd11_freebsd32_stat_args *uap)
2156 struct freebsd11_stat32 sb32;
2159 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2163 error = freebsd11_cvtstat32(&sb, &sb32);
2165 error = copyout(&sb32, uap->ub, sizeof (sb32));
2170 freebsd11_freebsd32_fstat(struct thread *td,
2171 struct freebsd11_freebsd32_fstat_args *uap)
2174 struct freebsd11_stat32 sb32;
2177 error = kern_fstat(td, uap->fd, &sb);
2180 error = freebsd11_cvtstat32(&sb, &sb32);
2182 error = copyout(&sb32, uap->ub, sizeof (sb32));
2187 freebsd11_freebsd32_fstatat(struct thread *td,
2188 struct freebsd11_freebsd32_fstatat_args *uap)
2191 struct freebsd11_stat32 sb32;
2194 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2198 error = freebsd11_cvtstat32(&sb, &sb32);
2200 error = copyout(&sb32, uap->buf, sizeof (sb32));
2205 freebsd11_freebsd32_lstat(struct thread *td,
2206 struct freebsd11_freebsd32_lstat_args *uap)
2209 struct freebsd11_stat32 sb32;
2212 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2213 UIO_USERSPACE, &sb, NULL);
2216 error = freebsd11_cvtstat32(&sb, &sb32);
2218 error = copyout(&sb32, uap->ub, sizeof (sb32));
2223 freebsd11_freebsd32_fhstat(struct thread *td,
2224 struct freebsd11_freebsd32_fhstat_args *uap)
2227 struct freebsd11_stat32 sb32;
2231 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2234 error = kern_fhstat(td, fh, &sb);
2237 error = freebsd11_cvtstat32(&sb, &sb32);
2239 error = copyout(&sb32, uap->sb, sizeof (sb32));
2245 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2247 int error, name[CTL_MAXNAME];
2251 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2253 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2257 error = fueword32(uap->oldlenp, &tmp);
2264 error = userland_sysctl(td, name, uap->namelen,
2265 uap->old, &oldlen, 1,
2266 uap->new, uap->newlen, &j, SCTL_MASK32);
2267 if (error && error != ENOMEM)
2270 suword32(uap->oldlenp, j);
2275 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
2281 error = copyin(uap->jail, &version, sizeof(uint32_t));
2288 /* FreeBSD single IPv4 jails. */
2289 struct jail32_v0 j32_v0;
2291 bzero(&j, sizeof(struct jail));
2292 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2295 CP(j32_v0, j, version);
2296 PTRIN_CP(j32_v0, j, path);
2297 PTRIN_CP(j32_v0, j, hostname);
2298 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2304 * Version 1 was used by multi-IPv4 jail implementations
2305 * that never made it into the official kernel.
2309 case 2: /* JAIL_API_VERSION */
2311 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2314 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2317 CP(j32, j, version);
2318 PTRIN_CP(j32, j, path);
2319 PTRIN_CP(j32, j, hostname);
2320 PTRIN_CP(j32, j, jailname);
2323 PTRIN_CP(j32, j, ip4);
2324 PTRIN_CP(j32, j, ip6);
2329 /* Sci-Fi jails are not supported, sorry. */
2332 return (kern_jail(td, &j));
2336 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2341 /* Check that we have an even number of iovecs. */
2342 if (uap->iovcnt & 1)
2345 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2348 error = kern_jail_set(td, auio, uap->flags);
2354 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2356 struct iovec32 iov32;
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_get(td, auio, uap->flags);
2369 for (i = 0; i < uap->iovcnt; i++) {
2370 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2371 CP(auio->uio_iov[i], iov32, iov_len);
2372 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2381 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2383 struct sigaction32 s32;
2384 struct sigaction sa, osa, *sap;
2388 error = copyin(uap->act, &s32, sizeof(s32));
2391 sa.sa_handler = PTRIN(s32.sa_u);
2392 CP(s32, sa, sa_flags);
2393 CP(s32, sa, sa_mask);
2397 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2398 if (error == 0 && uap->oact != NULL) {
2399 s32.sa_u = PTROUT(osa.sa_handler);
2400 CP(osa, s32, sa_flags);
2401 CP(osa, s32, sa_mask);
2402 error = copyout(&s32, uap->oact, sizeof(s32));
2407 #ifdef COMPAT_FREEBSD4
2409 freebsd4_freebsd32_sigaction(struct thread *td,
2410 struct freebsd4_freebsd32_sigaction_args *uap)
2412 struct sigaction32 s32;
2413 struct sigaction sa, osa, *sap;
2417 error = copyin(uap->act, &s32, sizeof(s32));
2420 sa.sa_handler = PTRIN(s32.sa_u);
2421 CP(s32, sa, sa_flags);
2422 CP(s32, sa, sa_mask);
2426 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2427 if (error == 0 && uap->oact != NULL) {
2428 s32.sa_u = PTROUT(osa.sa_handler);
2429 CP(osa, s32, sa_flags);
2430 CP(osa, s32, sa_mask);
2431 error = copyout(&s32, uap->oact, sizeof(s32));
2438 struct osigaction32 {
2447 ofreebsd32_sigaction(struct thread *td,
2448 struct ofreebsd32_sigaction_args *uap)
2450 struct osigaction32 s32;
2451 struct sigaction sa, osa, *sap;
2454 if (uap->signum <= 0 || uap->signum >= ONSIG)
2458 error = copyin(uap->nsa, &s32, sizeof(s32));
2461 sa.sa_handler = PTRIN(s32.sa_u);
2462 CP(s32, sa, sa_flags);
2463 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2467 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2468 if (error == 0 && uap->osa != NULL) {
2469 s32.sa_u = PTROUT(osa.sa_handler);
2470 CP(osa, s32, sa_flags);
2471 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2472 error = copyout(&s32, uap->osa, sizeof(s32));
2478 ofreebsd32_sigprocmask(struct thread *td,
2479 struct ofreebsd32_sigprocmask_args *uap)
2484 OSIG2SIG(uap->mask, set);
2485 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2486 SIG2OSIG(oset, td->td_retval[0]);
2491 ofreebsd32_sigpending(struct thread *td,
2492 struct ofreebsd32_sigpending_args *uap)
2494 struct proc *p = td->td_proc;
2498 siglist = p->p_siglist;
2499 SIGSETOR(siglist, td->td_siglist);
2501 SIG2OSIG(siglist, td->td_retval[0]);
2506 u_int32_t sv_handler;
2512 ofreebsd32_sigvec(struct thread *td,
2513 struct ofreebsd32_sigvec_args *uap)
2515 struct sigvec32 vec;
2516 struct sigaction sa, osa, *sap;
2519 if (uap->signum <= 0 || uap->signum >= ONSIG)
2523 error = copyin(uap->nsv, &vec, sizeof(vec));
2526 sa.sa_handler = PTRIN(vec.sv_handler);
2527 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2528 sa.sa_flags = vec.sv_flags;
2529 sa.sa_flags ^= SA_RESTART;
2533 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2534 if (error == 0 && uap->osv != NULL) {
2535 vec.sv_handler = PTROUT(osa.sa_handler);
2536 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2537 vec.sv_flags = osa.sa_flags;
2538 vec.sv_flags &= ~SA_NOCLDWAIT;
2539 vec.sv_flags ^= SA_RESTART;
2540 error = copyout(&vec, uap->osv, sizeof(vec));
2546 ofreebsd32_sigblock(struct thread *td,
2547 struct ofreebsd32_sigblock_args *uap)
2551 OSIG2SIG(uap->mask, set);
2552 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2553 SIG2OSIG(oset, td->td_retval[0]);
2558 ofreebsd32_sigsetmask(struct thread *td,
2559 struct ofreebsd32_sigsetmask_args *uap)
2563 OSIG2SIG(uap->mask, set);
2564 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2565 SIG2OSIG(oset, td->td_retval[0]);
2570 ofreebsd32_sigsuspend(struct thread *td,
2571 struct ofreebsd32_sigsuspend_args *uap)
2575 OSIG2SIG(uap->mask, mask);
2576 return (kern_sigsuspend(td, mask));
2585 ofreebsd32_sigstack(struct thread *td,
2586 struct ofreebsd32_sigstack_args *uap)
2588 struct sigstack32 s32;
2589 struct sigstack nss, oss;
2590 int error = 0, unss;
2592 if (uap->nss != NULL) {
2593 error = copyin(uap->nss, &s32, sizeof(s32));
2596 nss.ss_sp = PTRIN(s32.ss_sp);
2597 CP(s32, nss, ss_onstack);
2602 oss.ss_sp = td->td_sigstk.ss_sp;
2603 oss.ss_onstack = sigonstack(cpu_getstack(td));
2605 td->td_sigstk.ss_sp = nss.ss_sp;
2606 td->td_sigstk.ss_size = 0;
2607 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2608 td->td_pflags |= TDP_ALTSTACK;
2610 if (uap->oss != NULL) {
2611 s32.ss_sp = PTROUT(oss.ss_sp);
2612 CP(oss, s32, ss_onstack);
2613 error = copyout(&s32, uap->oss, sizeof(s32));
2620 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2623 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2624 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2628 freebsd32_clock_nanosleep(struct thread *td,
2629 struct freebsd32_clock_nanosleep_args *uap)
2633 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2634 uap->rqtp, uap->rmtp);
2635 return (kern_posix_error(td, error));
2639 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2640 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2642 struct timespec32 rmt32, rqt32;
2643 struct timespec rmt, rqt;
2646 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2650 CP(rqt32, rqt, tv_sec);
2651 CP(rqt32, rqt, tv_nsec);
2653 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2654 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2656 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2657 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2660 CP(rmt, rmt32, tv_sec);
2661 CP(rmt, rmt32, tv_nsec);
2663 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2671 freebsd32_clock_gettime(struct thread *td,
2672 struct freebsd32_clock_gettime_args *uap)
2674 struct timespec ats;
2675 struct timespec32 ats32;
2678 error = kern_clock_gettime(td, uap->clock_id, &ats);
2680 CP(ats, ats32, tv_sec);
2681 CP(ats, ats32, tv_nsec);
2682 error = copyout(&ats32, uap->tp, sizeof(ats32));
2688 freebsd32_clock_settime(struct thread *td,
2689 struct freebsd32_clock_settime_args *uap)
2691 struct timespec ats;
2692 struct timespec32 ats32;
2695 error = copyin(uap->tp, &ats32, sizeof(ats32));
2698 CP(ats32, ats, tv_sec);
2699 CP(ats32, ats, tv_nsec);
2701 return (kern_clock_settime(td, uap->clock_id, &ats));
2705 freebsd32_clock_getres(struct thread *td,
2706 struct freebsd32_clock_getres_args *uap)
2709 struct timespec32 ts32;
2712 if (uap->tp == NULL)
2714 error = kern_clock_getres(td, uap->clock_id, &ts);
2716 CP(ts, ts32, tv_sec);
2717 CP(ts, ts32, tv_nsec);
2718 error = copyout(&ts32, uap->tp, sizeof(ts32));
2723 int freebsd32_ktimer_create(struct thread *td,
2724 struct freebsd32_ktimer_create_args *uap)
2726 struct sigevent32 ev32;
2727 struct sigevent ev, *evp;
2730 if (uap->evp == NULL) {
2734 error = copyin(uap->evp, &ev32, sizeof(ev32));
2737 error = convert_sigevent32(&ev32, &ev);
2741 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2743 error = copyout(&id, uap->timerid, sizeof(int));
2745 kern_ktimer_delete(td, id);
2751 freebsd32_ktimer_settime(struct thread *td,
2752 struct freebsd32_ktimer_settime_args *uap)
2754 struct itimerspec32 val32, oval32;
2755 struct itimerspec val, oval, *ovalp;
2758 error = copyin(uap->value, &val32, sizeof(val32));
2762 ovalp = uap->ovalue != NULL ? &oval : NULL;
2763 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2764 if (error == 0 && uap->ovalue != NULL) {
2765 ITS_CP(oval, oval32);
2766 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2772 freebsd32_ktimer_gettime(struct thread *td,
2773 struct freebsd32_ktimer_gettime_args *uap)
2775 struct itimerspec32 val32;
2776 struct itimerspec val;
2779 error = kern_ktimer_gettime(td, uap->timerid, &val);
2782 error = copyout(&val32, uap->value, sizeof(val32));
2788 freebsd32_clock_getcpuclockid2(struct thread *td,
2789 struct freebsd32_clock_getcpuclockid2_args *uap)
2794 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2795 uap->which, &clk_id);
2797 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2802 freebsd32_thr_new(struct thread *td,
2803 struct freebsd32_thr_new_args *uap)
2805 struct thr_param32 param32;
2806 struct thr_param param;
2809 if (uap->param_size < 0 ||
2810 uap->param_size > sizeof(struct thr_param32))
2812 bzero(¶m, sizeof(struct thr_param));
2813 bzero(¶m32, sizeof(struct thr_param32));
2814 error = copyin(uap->param, ¶m32, uap->param_size);
2817 param.start_func = PTRIN(param32.start_func);
2818 param.arg = PTRIN(param32.arg);
2819 param.stack_base = PTRIN(param32.stack_base);
2820 param.stack_size = param32.stack_size;
2821 param.tls_base = PTRIN(param32.tls_base);
2822 param.tls_size = param32.tls_size;
2823 param.child_tid = PTRIN(param32.child_tid);
2824 param.parent_tid = PTRIN(param32.parent_tid);
2825 param.flags = param32.flags;
2826 param.rtp = PTRIN(param32.rtp);
2827 param.spare[0] = PTRIN(param32.spare[0]);
2828 param.spare[1] = PTRIN(param32.spare[1]);
2829 param.spare[2] = PTRIN(param32.spare[2]);
2831 return (kern_thr_new(td, ¶m));
2835 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2837 struct timespec32 ts32;
2838 struct timespec ts, *tsp;
2843 if (uap->timeout != NULL) {
2844 error = copyin((const void *)uap->timeout, (void *)&ts32,
2845 sizeof(struct timespec32));
2848 ts.tv_sec = ts32.tv_sec;
2849 ts.tv_nsec = ts32.tv_nsec;
2852 return (kern_thr_suspend(td, tsp));
2856 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2858 bzero(dst, sizeof(*dst));
2859 dst->si_signo = src->si_signo;
2860 dst->si_errno = src->si_errno;
2861 dst->si_code = src->si_code;
2862 dst->si_pid = src->si_pid;
2863 dst->si_uid = src->si_uid;
2864 dst->si_status = src->si_status;
2865 dst->si_addr = (uintptr_t)src->si_addr;
2866 dst->si_value.sival_int = src->si_value.sival_int;
2867 dst->si_timerid = src->si_timerid;
2868 dst->si_overrun = src->si_overrun;
2871 #ifndef _FREEBSD32_SYSPROTO_H_
2872 struct freebsd32_sigqueue_args {
2875 /* union sigval32 */ int value;
2879 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2884 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2885 * On 64-bit little-endian ABIs, the low bits are the same.
2886 * In 64-bit big-endian ABIs, sival_int overlaps with
2887 * sival_ptr's HIGH bits. We choose to support sival_int
2888 * rather than sival_ptr in this case as it seems to be
2891 bzero(&sv, sizeof(sv));
2892 sv.sival_int = uap->value;
2894 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2898 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2900 struct timespec32 ts32;
2902 struct timespec *timeout;
2905 struct siginfo32 si32;
2909 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2912 ts.tv_sec = ts32.tv_sec;
2913 ts.tv_nsec = ts32.tv_nsec;
2918 error = copyin(uap->set, &set, sizeof(set));
2922 error = kern_sigtimedwait(td, set, &ksi, timeout);
2927 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2928 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2932 td->td_retval[0] = ksi.ksi_signo;
2940 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2943 struct siginfo32 si32;
2947 error = copyin(uap->set, &set, sizeof(set));
2951 error = kern_sigtimedwait(td, set, &ksi, NULL);
2956 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2957 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2960 td->td_retval[0] = ksi.ksi_signo;
2965 freebsd32_cpuset_setid(struct thread *td,
2966 struct freebsd32_cpuset_setid_args *uap)
2969 return (kern_cpuset_setid(td, uap->which,
2970 PAIR32TO64(id_t, uap->id), uap->setid));
2974 freebsd32_cpuset_getid(struct thread *td,
2975 struct freebsd32_cpuset_getid_args *uap)
2978 return (kern_cpuset_getid(td, uap->level, uap->which,
2979 PAIR32TO64(id_t, uap->id), uap->setid));
2983 freebsd32_cpuset_getaffinity(struct thread *td,
2984 struct freebsd32_cpuset_getaffinity_args *uap)
2987 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
2988 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
2992 freebsd32_cpuset_setaffinity(struct thread *td,
2993 struct freebsd32_cpuset_setaffinity_args *uap)
2996 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
2997 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3001 freebsd32_cpuset_getdomain(struct thread *td,
3002 struct freebsd32_cpuset_getdomain_args *uap)
3005 return (kern_cpuset_getdomain(td, uap->level, uap->which,
3006 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3010 freebsd32_cpuset_setdomain(struct thread *td,
3011 struct freebsd32_cpuset_setdomain_args *uap)
3014 return (kern_cpuset_setdomain(td, uap->level, uap->which,
3015 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3019 freebsd32_nmount(struct thread *td,
3020 struct freebsd32_nmount_args /* {
3022 unsigned int iovcnt;
3031 * Mount flags are now 64-bits. On 32-bit archtectures only
3032 * 32-bits are passed in, but from here on everything handles
3033 * 64-bit flags correctly.
3037 AUDIT_ARG_FFLAGS(flags);
3040 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
3041 * userspace to set this flag, but we must filter it out if we want
3042 * MNT_UPDATE on the root file system to work.
3043 * MNT_ROOTFS should only be set by the kernel when mounting its
3046 flags &= ~MNT_ROOTFS;
3049 * check that we have an even number of iovec's
3050 * and that we have at least two options.
3052 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3055 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3058 error = vfs_donmount(td, flags, auio);
3066 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3068 struct yyy32 *p32, s32;
3069 struct yyy *p = NULL, s;
3074 error = copyin(uap->zzz, &s32, sizeof(s32));
3080 error = kern_xxx(td, p);
3085 error = copyout(&s32, p32, sizeof(s32));
3092 syscall32_module_handler(struct module *mod, int what, void *arg)
3095 return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3099 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3102 return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3106 syscall32_helper_unregister(struct syscall_helper_data *sd)
3109 return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3113 freebsd32_copyout_strings(struct image_params *imgp)
3119 u_int32_t *stack_base;
3120 struct freebsd32_ps_strings *arginfo;
3121 char canary[sizeof(long) * 8];
3122 int32_t pagesizes32[MAXPAGESIZES];
3123 size_t execpath_len;
3127 * Calculate string base and vector table pointers.
3128 * Also deal with signal trampoline code for this exec type.
3130 if (imgp->execpath != NULL && imgp->auxargs != NULL)
3131 execpath_len = strlen(imgp->execpath) + 1;
3134 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3136 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3137 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3140 destp = (uintptr_t)arginfo;
3145 if (szsigcode != 0) {
3147 destp = rounddown2(destp, sizeof(uint32_t));
3148 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3153 * Copy the image path for the rtld.
3155 if (execpath_len != 0) {
3156 destp -= execpath_len;
3157 imgp->execpathp = destp;
3158 copyout(imgp->execpath, (void *)destp, execpath_len);
3162 * Prepare the canary for SSP.
3164 arc4rand(canary, sizeof(canary), 0);
3165 destp -= sizeof(canary);
3166 imgp->canary = destp;
3167 copyout(canary, (void *)destp, sizeof(canary));
3168 imgp->canarylen = sizeof(canary);
3171 * Prepare the pagesizes array.
3173 for (i = 0; i < MAXPAGESIZES; i++)
3174 pagesizes32[i] = (uint32_t)pagesizes[i];
3175 destp -= sizeof(pagesizes32);
3176 destp = rounddown2(destp, sizeof(uint32_t));
3177 imgp->pagesizes = destp;
3178 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3179 imgp->pagesizeslen = sizeof(pagesizes32);
3181 destp -= ARG_MAX - imgp->args->stringspace;
3182 destp = rounddown2(destp, sizeof(uint32_t));
3185 * If we have a valid auxargs ptr, prepare some room
3188 if (imgp->auxargs) {
3190 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
3191 * lower compatibility.
3193 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
3196 * The '+ 2' is for the null pointers at the end of each of
3197 * the arg and env vector sets,and imgp->auxarg_size is room
3198 * for argument of Runtime loader.
3200 vectp = (u_int32_t *) (destp - (imgp->args->argc +
3201 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
3205 * The '+ 2' is for the null pointers at the end of each of
3206 * the arg and env vector sets
3208 vectp = (u_int32_t *)(destp - (imgp->args->argc +
3209 imgp->args->envc + 2) * sizeof(u_int32_t));
3213 * vectp also becomes our initial stack base
3217 stringp = imgp->args->begin_argv;
3218 argc = imgp->args->argc;
3219 envc = imgp->args->envc;
3221 * Copy out strings - arguments and environment.
3223 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3226 * Fill in "ps_strings" struct for ps, w, etc.
3228 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3229 suword32(&arginfo->ps_nargvstr, argc);
3232 * Fill in argument portion of vector table.
3234 for (; argc > 0; --argc) {
3235 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3236 while (*stringp++ != 0)
3241 /* a null vector table pointer separates the argp's from the envp's */
3242 suword32(vectp++, 0);
3244 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3245 suword32(&arginfo->ps_nenvstr, envc);
3248 * Fill in environment portion of vector table.
3250 for (; envc > 0; --envc) {
3251 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3252 while (*stringp++ != 0)
3257 /* end of vector table is a null pointer */
3260 return ((register_t *)stack_base);
3264 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3266 struct kld_file_stat *stat;
3267 struct kld32_file_stat *stat32;
3270 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3273 if (version != sizeof(struct kld32_file_stat_1) &&
3274 version != sizeof(struct kld32_file_stat))
3277 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3278 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3279 error = kern_kldstat(td, uap->fileid, stat);
3281 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3282 CP(*stat, *stat32, refs);
3283 CP(*stat, *stat32, id);
3284 PTROUT_CP(*stat, *stat32, address);
3285 CP(*stat, *stat32, size);
3286 bcopy(&stat->pathname[0], &stat32->pathname[0],
3287 sizeof(stat->pathname));
3288 stat32->version = version;
3289 error = copyout(stat32, uap->stat, version);
3292 free(stat32, M_TEMP);
3297 freebsd32_posix_fallocate(struct thread *td,
3298 struct freebsd32_posix_fallocate_args *uap)
3302 error = kern_posix_fallocate(td, uap->fd,
3303 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3304 return (kern_posix_error(td, error));
3308 freebsd32_posix_fadvise(struct thread *td,
3309 struct freebsd32_posix_fadvise_args *uap)
3313 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3314 PAIR32TO64(off_t, uap->len), uap->advice);
3315 return (kern_posix_error(td, error));
3319 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3322 CP(*sig32, *sig, sigev_notify);
3323 switch (sig->sigev_notify) {
3326 case SIGEV_THREAD_ID:
3327 CP(*sig32, *sig, sigev_notify_thread_id);
3330 CP(*sig32, *sig, sigev_signo);
3331 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3334 CP(*sig32, *sig, sigev_notify_kqueue);
3335 CP(*sig32, *sig, sigev_notify_kevent_flags);
3336 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3345 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3349 struct procctl_reaper_status rs;
3350 struct procctl_reaper_pids rp;
3351 struct procctl_reaper_kill rk;
3354 struct procctl_reaper_pids32 rp;
3356 int error, error1, flags;
3360 case PROC_TRACE_CTL:
3361 case PROC_TRAPCAP_CTL:
3362 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3367 case PROC_REAP_ACQUIRE:
3368 case PROC_REAP_RELEASE:
3369 if (uap->data != NULL)
3373 case PROC_REAP_STATUS:
3376 case PROC_REAP_GETPIDS:
3377 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3380 CP(x32.rp, x.rp, rp_count);
3381 PTRIN_CP(x32.rp, x.rp, rp_pids);
3384 case PROC_REAP_KILL:
3385 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3390 case PROC_TRACE_STATUS:
3391 case PROC_TRAPCAP_STATUS:
3397 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3400 case PROC_REAP_STATUS:
3402 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3404 case PROC_REAP_KILL:
3405 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3409 case PROC_TRACE_STATUS:
3410 case PROC_TRAPCAP_STATUS:
3412 error = copyout(&flags, uap->data, sizeof(flags));
3419 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3425 * Do unsigned conversion for arg when operation
3426 * interprets it as flags or pointer.
3428 case F_SETLK_REMOTE:
3437 tmp = (unsigned int)(uap->arg);
3443 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3447 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3449 struct timespec32 ts32;
3450 struct timespec ts, *tsp;
3454 if (uap->ts != NULL) {
3455 error = copyin(uap->ts, &ts32, sizeof(ts32));
3458 CP(ts32, ts, tv_sec);
3459 CP(ts32, ts, tv_nsec);
3463 if (uap->set != NULL) {
3464 error = copyin(uap->set, &set, sizeof(set));
3471 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));