2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
34 #include "opt_ktrace.h"
36 #define __ELF_WORD_SIZE 32
38 #ifdef COMPAT_FREEBSD11
39 #define _WANT_FREEBSD11_KEVENT
42 #include <sys/param.h>
44 #include <sys/capsicum.h>
45 #include <sys/clock.h>
47 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/imgact.h>
51 #include <sys/kernel.h>
52 #include <sys/limits.h>
53 #include <sys/linker.h>
55 #include <sys/malloc.h>
56 #include <sys/file.h> /* Must come after sys/malloc.h */
57 #include <sys/imgact.h>
60 #include <sys/module.h>
61 #include <sys/mount.h>
62 #include <sys/mutex.h>
63 #include <sys/namei.h>
65 #include <sys/procctl.h>
66 #include <sys/reboot.h>
67 #include <sys/resource.h>
68 #include <sys/resourcevar.h>
69 #include <sys/selinfo.h>
70 #include <sys/eventvar.h> /* Must come after sys/selinfo.h */
71 #include <sys/pipe.h> /* Must come after sys/selinfo.h */
72 #include <sys/signal.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
77 #include <sys/syscall.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <sys/sysent.h>
81 #include <sys/sysproto.h>
82 #include <sys/systm.h>
84 #include <sys/unistd.h>
85 #include <sys/ucontext.h>
86 #include <sys/vnode.h>
93 #include <sys/ktrace.h>
97 #include <netinet/in.h>
101 #include <vm/vm_param.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_extern.h>
107 #include <machine/cpu.h>
108 #include <machine/elf.h>
110 #include <security/audit/audit.h>
112 #include <compat/freebsd32/freebsd32_util.h>
113 #include <compat/freebsd32/freebsd32.h>
114 #include <compat/freebsd32/freebsd32_ipc.h>
115 #include <compat/freebsd32/freebsd32_misc.h>
116 #include <compat/freebsd32/freebsd32_signal.h>
117 #include <compat/freebsd32/freebsd32_proto.h>
119 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
122 CTASSERT(sizeof(struct timeval32) == 8);
123 CTASSERT(sizeof(struct timespec32) == 8);
124 CTASSERT(sizeof(struct itimerval32) == 16);
125 CTASSERT(sizeof(struct bintime32) == 12);
127 CTASSERT(sizeof(struct statfs32) == 256);
129 CTASSERT(sizeof(struct rusage32) == 72);
131 CTASSERT(sizeof(struct sigaltstack32) == 12);
133 CTASSERT(sizeof(struct kevent32) == 56);
135 CTASSERT(sizeof(struct kevent32) == 64);
137 CTASSERT(sizeof(struct iovec32) == 8);
138 CTASSERT(sizeof(struct msghdr32) == 28);
140 CTASSERT(sizeof(struct stat32) == 208);
141 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
143 CTASSERT(sizeof(struct sigaction32) == 24);
145 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
146 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
147 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
148 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
151 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
154 TV_CP(*s, *s32, ru_utime);
155 TV_CP(*s, *s32, ru_stime);
156 CP(*s, *s32, ru_maxrss);
157 CP(*s, *s32, ru_ixrss);
158 CP(*s, *s32, ru_idrss);
159 CP(*s, *s32, ru_isrss);
160 CP(*s, *s32, ru_minflt);
161 CP(*s, *s32, ru_majflt);
162 CP(*s, *s32, ru_nswap);
163 CP(*s, *s32, ru_inblock);
164 CP(*s, *s32, ru_oublock);
165 CP(*s, *s32, ru_msgsnd);
166 CP(*s, *s32, ru_msgrcv);
167 CP(*s, *s32, ru_nsignals);
168 CP(*s, *s32, ru_nvcsw);
169 CP(*s, *s32, ru_nivcsw);
173 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
176 struct rusage32 ru32;
177 struct rusage ru, *rup;
179 if (uap->rusage != NULL)
183 error = kern_wait(td, uap->pid, &status, uap->options, rup);
186 if (uap->status != NULL)
187 error = copyout(&status, uap->status, sizeof(status));
188 if (uap->rusage != NULL && error == 0) {
189 freebsd32_rusage_out(&ru, &ru32);
190 error = copyout(&ru32, uap->rusage, sizeof(ru32));
196 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
198 struct wrusage32 wru32;
199 struct __wrusage wru, *wrup;
200 struct siginfo32 si32;
201 struct __siginfo si, *sip;
204 if (uap->wrusage != NULL)
208 if (uap->info != NULL) {
210 bzero(sip, sizeof(*sip));
213 error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
214 &status, uap->options, wrup, sip);
217 if (uap->status != NULL)
218 error = copyout(&status, uap->status, sizeof(status));
219 if (uap->wrusage != NULL && error == 0) {
220 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
221 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
222 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
224 if (uap->info != NULL && error == 0) {
225 siginfo_to_siginfo32 (&si, &si32);
226 error = copyout(&si32, uap->info, sizeof(si32));
231 #ifdef COMPAT_FREEBSD4
233 copy_statfs(struct statfs *in, struct statfs32 *out)
236 statfs_scale_blocks(in, INT32_MAX);
237 bzero(out, sizeof(*out));
238 CP(*in, *out, f_bsize);
239 out->f_iosize = MIN(in->f_iosize, INT32_MAX);
240 CP(*in, *out, f_blocks);
241 CP(*in, *out, f_bfree);
242 CP(*in, *out, f_bavail);
243 out->f_files = MIN(in->f_files, INT32_MAX);
244 out->f_ffree = MIN(in->f_ffree, INT32_MAX);
245 CP(*in, *out, f_fsid);
246 CP(*in, *out, f_owner);
247 CP(*in, *out, f_type);
248 CP(*in, *out, f_flags);
249 out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
250 out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
251 strlcpy(out->f_fstypename,
252 in->f_fstypename, MFSNAMELEN);
253 strlcpy(out->f_mntonname,
254 in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
255 out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
256 out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
257 strlcpy(out->f_mntfromname,
258 in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
262 #ifdef COMPAT_FREEBSD4
264 freebsd4_freebsd32_getfsstat(struct thread *td,
265 struct freebsd4_freebsd32_getfsstat_args *uap)
267 struct statfs *buf, *sp;
268 struct statfs32 stat32;
269 size_t count, size, copycount;
272 count = uap->bufsize / sizeof(struct statfs32);
273 size = count * sizeof(struct statfs);
274 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
278 while (copycount > 0 && error == 0) {
279 copy_statfs(sp, &stat32);
280 error = copyout(&stat32, uap->buf, sizeof(stat32));
288 td->td_retval[0] = count;
293 #ifdef COMPAT_FREEBSD10
295 freebsd10_freebsd32_pipe(struct thread *td,
296 struct freebsd10_freebsd32_pipe_args *uap) {
298 return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
303 freebsd32_sigaltstack(struct thread *td,
304 struct freebsd32_sigaltstack_args *uap)
306 struct sigaltstack32 s32;
307 struct sigaltstack ss, oss, *ssp;
310 if (uap->ss != NULL) {
311 error = copyin(uap->ss, &s32, sizeof(s32));
314 PTRIN_CP(s32, ss, ss_sp);
315 CP(s32, ss, ss_size);
316 CP(s32, ss, ss_flags);
320 error = kern_sigaltstack(td, ssp, &oss);
321 if (error == 0 && uap->oss != NULL) {
322 PTROUT_CP(oss, s32, ss_sp);
323 CP(oss, s32, ss_size);
324 CP(oss, s32, ss_flags);
325 error = copyout(&s32, uap->oss, sizeof(s32));
331 * Custom version of exec_copyin_args() so that we can translate
335 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
336 enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
343 bzero(args, sizeof(*args));
348 * Allocate demand-paged memory for the file name, argument, and
349 * environment strings.
351 error = exec_alloc_args(args);
356 * Copy the file name.
359 args->fname = args->buf;
360 error = (segflg == UIO_SYSSPACE) ?
361 copystr(fname, args->fname, PATH_MAX, &length) :
362 copyinstr(fname, args->fname, PATH_MAX, &length);
368 args->begin_argv = args->buf + length;
369 args->endp = args->begin_argv;
370 args->stringspace = ARG_MAX;
373 * extract arguments first
377 error = copyin(p32++, &arg, sizeof(arg));
383 error = copyinstr(argp, args->endp, args->stringspace, &length);
385 if (error == ENAMETOOLONG)
389 args->stringspace -= length;
390 args->endp += length;
394 args->begin_envv = args->endp;
397 * extract environment strings
402 error = copyin(p32++, &arg, sizeof(arg));
408 error = copyinstr(envp, args->endp, args->stringspace,
411 if (error == ENAMETOOLONG)
415 args->stringspace -= length;
416 args->endp += length;
424 exec_free_args(args);
429 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
431 struct image_args eargs;
432 struct vmspace *oldvmspace;
435 error = pre_execve(td, &oldvmspace);
438 error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
439 uap->argv, uap->envv);
441 error = kern_execve(td, &eargs, NULL);
442 post_execve(td, error, oldvmspace);
447 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
449 struct image_args eargs;
450 struct vmspace *oldvmspace;
453 error = pre_execve(td, &oldvmspace);
456 error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
457 uap->argv, uap->envv);
460 error = kern_execve(td, &eargs, NULL);
462 post_execve(td, error, oldvmspace);
466 #if defined(COMPAT_FREEBSD11)
468 freebsd11_freebsd32_mknod(struct thread *td,
469 struct freebsd11_freebsd32_mknod_args *uap)
472 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->mode,
477 freebsd11_freebsd32_mknodat(struct thread *td,
478 struct freebsd11_freebsd32_mknodat_args *uap)
481 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
484 #endif /* COMPAT_FREEBSD11 */
487 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
492 #if defined(__amd64__)
493 if (i386_read_exec && (prot & PROT_READ) != 0)
496 return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
501 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
506 #if defined(__amd64__)
507 if (i386_read_exec && (prot & PROT_READ))
511 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
512 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
515 #ifdef COMPAT_FREEBSD6
517 freebsd6_freebsd32_mmap(struct thread *td,
518 struct freebsd6_freebsd32_mmap_args *uap)
523 #if defined(__amd64__)
524 if (i386_read_exec && (prot & PROT_READ))
528 return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
529 uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
534 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
536 struct itimerval itv, oitv, *itvp;
537 struct itimerval32 i32;
540 if (uap->itv != NULL) {
541 error = copyin(uap->itv, &i32, sizeof(i32));
544 TV_CP(i32, itv, it_interval);
545 TV_CP(i32, itv, it_value);
549 error = kern_setitimer(td, uap->which, itvp, &oitv);
550 if (error || uap->oitv == NULL)
552 TV_CP(oitv, i32, it_interval);
553 TV_CP(oitv, i32, it_value);
554 return (copyout(&i32, uap->oitv, sizeof(i32)));
558 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
560 struct itimerval itv;
561 struct itimerval32 i32;
564 error = kern_getitimer(td, uap->which, &itv);
565 if (error || uap->itv == NULL)
567 TV_CP(itv, i32, it_interval);
568 TV_CP(itv, i32, it_value);
569 return (copyout(&i32, uap->itv, sizeof(i32)));
573 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
575 struct timeval32 tv32;
576 struct timeval tv, *tvp;
579 if (uap->tv != NULL) {
580 error = copyin(uap->tv, &tv32, sizeof(tv32));
583 CP(tv32, tv, tv_sec);
584 CP(tv32, tv, tv_usec);
589 * XXX Do pointers need PTRIN()?
591 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
592 sizeof(int32_t) * 8));
596 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
598 struct timespec32 ts32;
600 struct timeval tv, *tvp;
604 if (uap->ts != NULL) {
605 error = copyin(uap->ts, &ts32, sizeof(ts32));
608 CP(ts32, ts, tv_sec);
609 CP(ts32, ts, tv_nsec);
610 TIMESPEC_TO_TIMEVAL(&tv, &ts);
614 if (uap->sm != NULL) {
615 error = copyin(uap->sm, &set, sizeof(set));
622 * XXX Do pointers need PTRIN()?
624 error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
625 uset, sizeof(int32_t) * 8);
630 * Copy 'count' items into the destination list pointed to by uap->eventlist.
633 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
635 struct freebsd32_kevent_args *uap;
636 struct kevent32 ks32[KQ_NEVENTS];
640 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
641 uap = (struct freebsd32_kevent_args *)arg;
643 for (i = 0; i < count; i++) {
644 CP(kevp[i], ks32[i], ident);
645 CP(kevp[i], ks32[i], filter);
646 CP(kevp[i], ks32[i], flags);
647 CP(kevp[i], ks32[i], fflags);
648 #if BYTE_ORDER == LITTLE_ENDIAN
649 ks32[i].data1 = kevp[i].data;
650 ks32[i].data2 = kevp[i].data >> 32;
652 ks32[i].data1 = kevp[i].data >> 32;
653 ks32[i].data2 = kevp[i].data;
655 PTROUT_CP(kevp[i], ks32[i], udata);
656 for (j = 0; j < nitems(kevp->ext); j++) {
658 #if BYTE_ORDER == LITTLE_ENDIAN
659 ks32[i].ext64[2 * j] = e;
660 ks32[i].ext64[2 * j + 1] = e >> 32;
662 ks32[i].ext64[2 * j] = e >> 32;
663 ks32[i].ext64[2 * j + 1] = e;
667 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
669 uap->eventlist += count;
674 * Copy 'count' items from the list pointed to by uap->changelist.
677 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
679 struct freebsd32_kevent_args *uap;
680 struct kevent32 ks32[KQ_NEVENTS];
684 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
685 uap = (struct freebsd32_kevent_args *)arg;
687 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
690 uap->changelist += count;
692 for (i = 0; i < count; i++) {
693 CP(ks32[i], kevp[i], ident);
694 CP(ks32[i], kevp[i], filter);
695 CP(ks32[i], kevp[i], flags);
696 CP(ks32[i], kevp[i], fflags);
697 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
698 PTRIN_CP(ks32[i], kevp[i], udata);
699 for (j = 0; j < nitems(kevp->ext); j++) {
700 #if BYTE_ORDER == LITTLE_ENDIAN
701 e = ks32[i].ext64[2 * j + 1];
703 e += ks32[i].ext64[2 * j];
705 e = ks32[i].ext64[2 * j];
707 e += ks32[i].ext64[2 * j + 1];
717 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
719 struct timespec32 ts32;
720 struct timespec ts, *tsp;
721 struct kevent_copyops k_ops = {
723 .k_copyout = freebsd32_kevent_copyout,
724 .k_copyin = freebsd32_kevent_copyin,
727 struct kevent32 *eventlist = uap->eventlist;
732 error = copyin(uap->timeout, &ts32, sizeof(ts32));
735 CP(ts32, ts, tv_sec);
736 CP(ts32, ts, tv_nsec);
741 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
742 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
743 uap->nchanges, sizeof(struct kevent32));
745 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
748 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
749 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
750 td->td_retval[0], sizeof(struct kevent32));
755 #ifdef COMPAT_FREEBSD11
757 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
759 struct freebsd11_freebsd32_kevent_args *uap;
760 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
763 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
764 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
766 for (i = 0; i < count; i++) {
767 CP(kevp[i], ks32[i], ident);
768 CP(kevp[i], ks32[i], filter);
769 CP(kevp[i], ks32[i], flags);
770 CP(kevp[i], ks32[i], fflags);
771 CP(kevp[i], ks32[i], data);
772 PTROUT_CP(kevp[i], ks32[i], udata);
774 error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
776 uap->eventlist += count;
781 * Copy 'count' items from the list pointed to by uap->changelist.
784 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
786 struct freebsd11_freebsd32_kevent_args *uap;
787 struct kevent32_freebsd11 ks32[KQ_NEVENTS];
790 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
791 uap = (struct freebsd11_freebsd32_kevent_args *)arg;
793 error = copyin(uap->changelist, ks32, count * sizeof *ks32);
796 uap->changelist += count;
798 for (i = 0; i < count; i++) {
799 CP(ks32[i], kevp[i], ident);
800 CP(ks32[i], kevp[i], filter);
801 CP(ks32[i], kevp[i], flags);
802 CP(ks32[i], kevp[i], fflags);
803 CP(ks32[i], kevp[i], data);
804 PTRIN_CP(ks32[i], kevp[i], udata);
805 for (j = 0; j < nitems(kevp->ext); j++)
813 freebsd11_freebsd32_kevent(struct thread *td,
814 struct freebsd11_freebsd32_kevent_args *uap)
816 struct timespec32 ts32;
817 struct timespec ts, *tsp;
818 struct kevent_copyops k_ops = {
820 .k_copyout = freebsd32_kevent11_copyout,
821 .k_copyin = freebsd32_kevent11_copyin,
824 struct kevent32_freebsd11 *eventlist = uap->eventlist;
829 error = copyin(uap->timeout, &ts32, sizeof(ts32));
832 CP(ts32, ts, tv_sec);
833 CP(ts32, ts, tv_nsec);
838 if (KTRPOINT(td, KTR_STRUCT_ARRAY))
839 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
840 uap->changelist, uap->nchanges,
841 sizeof(struct kevent32_freebsd11));
843 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
846 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
847 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
848 eventlist, td->td_retval[0],
849 sizeof(struct kevent32_freebsd11));
856 freebsd32_gettimeofday(struct thread *td,
857 struct freebsd32_gettimeofday_args *uap)
860 struct timeval32 atv32;
866 CP(atv, atv32, tv_sec);
867 CP(atv, atv32, tv_usec);
868 error = copyout(&atv32, uap->tp, sizeof (atv32));
870 if (error == 0 && uap->tzp != NULL) {
871 rtz.tz_minuteswest = tz_minuteswest;
872 rtz.tz_dsttime = tz_dsttime;
873 error = copyout(&rtz, uap->tzp, sizeof (rtz));
879 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
885 error = kern_getrusage(td, uap->who, &s);
887 freebsd32_rusage_out(&s, &s32);
888 error = copyout(&s32, uap->rusage, sizeof(s32));
894 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
896 struct iovec32 iov32;
903 if (iovcnt > UIO_MAXIOV)
905 iovlen = iovcnt * sizeof(struct iovec);
906 uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
907 iov = (struct iovec *)(uio + 1);
908 for (i = 0; i < iovcnt; i++) {
909 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
914 iov[i].iov_base = PTRIN(iov32.iov_base);
915 iov[i].iov_len = iov32.iov_len;
918 uio->uio_iovcnt = iovcnt;
919 uio->uio_segflg = UIO_USERSPACE;
920 uio->uio_offset = -1;
922 for (i = 0; i < iovcnt; i++) {
923 if (iov->iov_len > INT_MAX - uio->uio_resid) {
927 uio->uio_resid += iov->iov_len;
935 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
940 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
943 error = kern_readv(td, uap->fd, auio);
949 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
954 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
957 error = kern_writev(td, uap->fd, auio);
963 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
968 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
971 error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
977 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
982 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
985 error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
991 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
994 struct iovec32 iov32;
1000 if (iovcnt > UIO_MAXIOV)
1002 iovlen = iovcnt * sizeof(struct iovec);
1003 iov = malloc(iovlen, M_IOV, M_WAITOK);
1004 for (i = 0; i < iovcnt; i++) {
1005 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
1010 iov[i].iov_base = PTRIN(iov32.iov_base);
1011 iov[i].iov_len = iov32.iov_len;
1018 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
1020 struct msghdr32 m32;
1023 error = copyin(msg32, &m32, sizeof(m32));
1026 msg->msg_name = PTRIN(m32.msg_name);
1027 msg->msg_namelen = m32.msg_namelen;
1028 msg->msg_iov = PTRIN(m32.msg_iov);
1029 msg->msg_iovlen = m32.msg_iovlen;
1030 msg->msg_control = PTRIN(m32.msg_control);
1031 msg->msg_controllen = m32.msg_controllen;
1032 msg->msg_flags = m32.msg_flags;
1037 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
1039 struct msghdr32 m32;
1042 m32.msg_name = PTROUT(msg->msg_name);
1043 m32.msg_namelen = msg->msg_namelen;
1044 m32.msg_iov = PTROUT(msg->msg_iov);
1045 m32.msg_iovlen = msg->msg_iovlen;
1046 m32.msg_control = PTROUT(msg->msg_control);
1047 m32.msg_controllen = msg->msg_controllen;
1048 m32.msg_flags = msg->msg_flags;
1049 error = copyout(&m32, msg32, sizeof(m32));
1054 #define FREEBSD32_ALIGNBYTES (sizeof(int) - 1)
1056 #define FREEBSD32_ALIGNBYTES (sizeof(long) - 1)
1058 #define FREEBSD32_ALIGN(p) \
1059 (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
1060 #define FREEBSD32_CMSG_SPACE(l) \
1061 (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
1063 #define FREEBSD32_CMSG_DATA(cmsg) ((unsigned char *)(cmsg) + \
1064 FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
1067 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
1071 struct timespec32 ts;
1072 struct timeval32 tv;
1073 struct bintime32 bt;
1084 switch (cm->cmsg_level) {
1086 switch (cm->cmsg_type) {
1088 TV_CP(*in, tmp32, tv);
1089 copylen = sizeof(tmp32.tv);
1093 BT_CP(*in, tmp32, bt);
1094 copylen = sizeof(tmp32.bt);
1099 TS_CP(*in, tmp32, ts);
1100 copylen = sizeof(tmp32.ts);
1114 KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
1116 bcopy(&tmp32, data, copylen);
1121 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
1125 socklen_t clen, datalen, datalen_out, oldclen;
1128 int len, maxlen, copylen;
1132 len = msg->msg_controllen;
1133 maxlen = msg->msg_controllen;
1134 msg->msg_controllen = 0;
1136 ctlbuf = msg->msg_control;
1137 for (m = control; m != NULL && len > 0; m = m->m_next) {
1138 cm = mtod(m, struct cmsghdr *);
1140 while (cm != NULL) {
1141 if (sizeof(struct cmsghdr) > clen ||
1142 cm->cmsg_len > clen) {
1147 data = CMSG_DATA(cm);
1148 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1149 datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
1152 * Copy out the message header. Preserve the native
1153 * message size in case we need to inspect the message
1156 copylen = sizeof(struct cmsghdr);
1157 if (len < copylen) {
1158 msg->msg_flags |= MSG_CTRUNC;
1159 m_dispose_extcontrolm(m);
1162 oldclen = cm->cmsg_len;
1163 cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
1165 error = copyout(cm, ctlbuf, copylen);
1166 cm->cmsg_len = oldclen;
1170 ctlbuf += FREEBSD32_ALIGN(copylen);
1171 len -= FREEBSD32_ALIGN(copylen);
1173 copylen = datalen_out;
1174 if (len < copylen) {
1175 msg->msg_flags |= MSG_CTRUNC;
1176 m_dispose_extcontrolm(m);
1180 /* Copy out the message data. */
1181 error = copyout(data, ctlbuf, copylen);
1185 ctlbuf += FREEBSD32_ALIGN(copylen);
1186 len -= FREEBSD32_ALIGN(copylen);
1188 if (CMSG_SPACE(datalen) < clen) {
1189 clen -= CMSG_SPACE(datalen);
1190 cm = (struct cmsghdr *)
1191 ((caddr_t)cm + CMSG_SPACE(datalen));
1197 msg->msg_controllen += FREEBSD32_ALIGN(sizeof(*cm)) +
1201 if (len == 0 && m != NULL) {
1202 msg->msg_flags |= MSG_CTRUNC;
1203 m_dispose_extcontrolm(m);
1211 freebsd32_recvmsg(td, uap)
1213 struct freebsd32_recvmsg_args /* {
1215 struct msghdr32 *msg;
1220 struct msghdr32 m32;
1221 struct iovec *uiov, *iov;
1222 struct mbuf *control = NULL;
1223 struct mbuf **controlp;
1226 error = copyin(uap->msg, &m32, sizeof(m32));
1229 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1232 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1236 msg.msg_flags = uap->flags;
1240 controlp = (msg.msg_control != NULL) ? &control : NULL;
1241 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
1245 if (control != NULL)
1246 error = freebsd32_copy_msg_out(&msg, control);
1248 msg.msg_controllen = 0;
1251 error = freebsd32_copyoutmsghdr(&msg, uap->msg);
1255 if (control != NULL) {
1257 m_dispose_extcontrolm(control);
1265 * Copy-in the array of control messages constructed using alignment
1266 * and padding suitable for a 32-bit environment and construct an
1267 * mbuf using alignment and padding suitable for a 64-bit kernel.
1268 * The alignment and padding are defined indirectly by CMSG_DATA(),
1269 * CMSG_SPACE() and CMSG_LEN().
1272 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
1276 u_int idx, len, msglen;
1279 buflen = FREEBSD32_ALIGN(buflen);
1281 if (buflen > MCLBYTES)
1285 * Iterate over the buffer and get the length of each message
1286 * in there. This has 32-bit alignment and padding. Use it to
1287 * determine the length of these messages when using 64-bit
1288 * alignment and padding.
1292 while (idx < buflen) {
1293 error = copyin(buf + idx, &msglen, sizeof(msglen));
1296 if (msglen < sizeof(struct cmsghdr))
1298 msglen = FREEBSD32_ALIGN(msglen);
1299 if (idx + msglen > buflen)
1302 msglen += CMSG_ALIGN(sizeof(struct cmsghdr)) -
1303 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1304 len += CMSG_ALIGN(msglen);
1310 m = m_get(M_WAITOK, MT_CONTROL);
1312 MCLGET(m, M_WAITOK);
1315 md = mtod(m, void *);
1316 while (buflen > 0) {
1317 error = copyin(buf, md, sizeof(struct cmsghdr));
1320 msglen = *(u_int *)md;
1321 msglen = FREEBSD32_ALIGN(msglen);
1323 /* Modify the message length to account for alignment. */
1324 *(u_int *)md = msglen + CMSG_ALIGN(sizeof(struct cmsghdr)) -
1325 FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1327 md = (char *)md + CMSG_ALIGN(sizeof(struct cmsghdr));
1328 buf += FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1329 buflen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1331 msglen -= FREEBSD32_ALIGN(sizeof(struct cmsghdr));
1333 error = copyin(buf, md, msglen);
1336 md = (char *)md + CMSG_ALIGN(msglen);
1350 freebsd32_sendmsg(struct thread *td,
1351 struct freebsd32_sendmsg_args *uap)
1354 struct msghdr32 m32;
1356 struct mbuf *control = NULL;
1357 struct sockaddr *to = NULL;
1360 error = copyin(uap->msg, &m32, sizeof(m32));
1363 error = freebsd32_copyinmsghdr(uap->msg, &msg);
1366 error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
1371 if (msg.msg_name != NULL) {
1372 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
1380 if (msg.msg_control) {
1381 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
1386 error = freebsd32_copyin_control(&control, msg.msg_control,
1387 msg.msg_controllen);
1391 msg.msg_control = NULL;
1392 msg.msg_controllen = 0;
1395 error = kern_sendit(td, uap->s, &msg, uap->flags, control,
1406 freebsd32_recvfrom(struct thread *td,
1407 struct freebsd32_recvfrom_args *uap)
1413 if (uap->fromlenaddr) {
1414 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
1415 sizeof(msg.msg_namelen));
1419 msg.msg_namelen = 0;
1422 msg.msg_name = PTRIN(uap->from);
1423 msg.msg_iov = &aiov;
1425 aiov.iov_base = PTRIN(uap->buf);
1426 aiov.iov_len = uap->len;
1427 msg.msg_control = NULL;
1428 msg.msg_flags = uap->flags;
1429 error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
1430 if (error == 0 && uap->fromlenaddr)
1431 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
1432 sizeof (msg.msg_namelen));
1437 freebsd32_settimeofday(struct thread *td,
1438 struct freebsd32_settimeofday_args *uap)
1440 struct timeval32 tv32;
1441 struct timeval tv, *tvp;
1442 struct timezone tz, *tzp;
1446 error = copyin(uap->tv, &tv32, sizeof(tv32));
1449 CP(tv32, tv, tv_sec);
1450 CP(tv32, tv, tv_usec);
1455 error = copyin(uap->tzp, &tz, sizeof(tz));
1461 return (kern_settimeofday(td, tvp, tzp));
1465 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
1467 struct timeval32 s32[2];
1468 struct timeval s[2], *sp;
1471 if (uap->tptr != NULL) {
1472 error = copyin(uap->tptr, s32, sizeof(s32));
1475 CP(s32[0], s[0], tv_sec);
1476 CP(s32[0], s[0], tv_usec);
1477 CP(s32[1], s[1], tv_sec);
1478 CP(s32[1], s[1], tv_usec);
1482 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1487 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
1489 struct timeval32 s32[2];
1490 struct timeval s[2], *sp;
1493 if (uap->tptr != NULL) {
1494 error = copyin(uap->tptr, s32, sizeof(s32));
1497 CP(s32[0], s[0], tv_sec);
1498 CP(s32[0], s[0], tv_usec);
1499 CP(s32[1], s[1], tv_sec);
1500 CP(s32[1], s[1], tv_usec);
1504 return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
1508 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
1510 struct timeval32 s32[2];
1511 struct timeval s[2], *sp;
1514 if (uap->tptr != NULL) {
1515 error = copyin(uap->tptr, s32, sizeof(s32));
1518 CP(s32[0], s[0], tv_sec);
1519 CP(s32[0], s[0], tv_usec);
1520 CP(s32[1], s[1], tv_sec);
1521 CP(s32[1], s[1], tv_usec);
1525 return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
1529 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
1531 struct timeval32 s32[2];
1532 struct timeval s[2], *sp;
1535 if (uap->times != NULL) {
1536 error = copyin(uap->times, s32, sizeof(s32));
1539 CP(s32[0], s[0], tv_sec);
1540 CP(s32[0], s[0], tv_usec);
1541 CP(s32[1], s[1], tv_sec);
1542 CP(s32[1], s[1], tv_usec);
1546 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
1551 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
1553 struct timespec32 ts32[2];
1554 struct timespec ts[2], *tsp;
1557 if (uap->times != NULL) {
1558 error = copyin(uap->times, ts32, sizeof(ts32));
1561 CP(ts32[0], ts[0], tv_sec);
1562 CP(ts32[0], ts[0], tv_nsec);
1563 CP(ts32[1], ts[1], tv_sec);
1564 CP(ts32[1], ts[1], tv_nsec);
1568 return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
1572 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
1574 struct timespec32 ts32[2];
1575 struct timespec ts[2], *tsp;
1578 if (uap->times != NULL) {
1579 error = copyin(uap->times, ts32, sizeof(ts32));
1582 CP(ts32[0], ts[0], tv_sec);
1583 CP(ts32[0], ts[0], tv_nsec);
1584 CP(ts32[1], ts[1], tv_sec);
1585 CP(ts32[1], ts[1], tv_nsec);
1589 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
1590 tsp, UIO_SYSSPACE, uap->flag));
1594 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
1596 struct timeval32 tv32;
1597 struct timeval delta, olddelta, *deltap;
1601 error = copyin(uap->delta, &tv32, sizeof(tv32));
1604 CP(tv32, delta, tv_sec);
1605 CP(tv32, delta, tv_usec);
1609 error = kern_adjtime(td, deltap, &olddelta);
1610 if (uap->olddelta && error == 0) {
1611 CP(olddelta, tv32, tv_sec);
1612 CP(olddelta, tv32, tv_usec);
1613 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
1618 #ifdef COMPAT_FREEBSD4
1620 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
1622 struct statfs32 s32;
1626 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1627 error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
1629 copy_statfs(sp, &s32);
1630 error = copyout(&s32, uap->buf, sizeof(s32));
1637 #ifdef COMPAT_FREEBSD4
1639 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
1641 struct statfs32 s32;
1645 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1646 error = kern_fstatfs(td, uap->fd, sp);
1648 copy_statfs(sp, &s32);
1649 error = copyout(&s32, uap->buf, sizeof(s32));
1656 #ifdef COMPAT_FREEBSD4
1658 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
1660 struct statfs32 s32;
1665 if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
1667 sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
1668 error = kern_fhstatfs(td, fh, sp);
1670 copy_statfs(sp, &s32);
1671 error = copyout(&s32, uap->buf, sizeof(s32));
1679 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
1682 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1683 PAIR32TO64(off_t, uap->offset)));
1687 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
1690 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1691 PAIR32TO64(off_t, uap->offset)));
1696 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
1699 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
1704 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
1709 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1711 /* Expand the quad return into two parts for eax and edx */
1712 pos = td->td_uretoff.tdu_off;
1713 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1714 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1719 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
1722 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1723 PAIR32TO64(off_t, uap->length)));
1727 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
1730 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1735 ofreebsd32_getdirentries(struct thread *td,
1736 struct ofreebsd32_getdirentries_args *uap)
1738 struct ogetdirentries_args ap;
1745 ap.count = uap->count;
1747 error = kern_ogetdirentries(td, &ap, &loff);
1750 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
1756 #if defined(COMPAT_FREEBSD11)
1758 freebsd11_freebsd32_getdirentries(struct thread *td,
1759 struct freebsd11_freebsd32_getdirentries_args *uap)
1765 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
1769 if (uap->basep != NULL) {
1771 error = copyout(&base32, uap->basep, sizeof(int32_t));
1777 freebsd11_freebsd32_getdents(struct thread *td,
1778 struct freebsd11_freebsd32_getdents_args *uap)
1780 struct freebsd11_freebsd32_getdirentries_args ap;
1784 ap.count = uap->count;
1786 return (freebsd11_freebsd32_getdirentries(td, &ap));
1788 #endif /* COMPAT_FREEBSD11 */
1790 #ifdef COMPAT_FREEBSD6
1791 /* versions with the 'int pad' argument */
1793 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
1796 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
1797 PAIR32TO64(off_t, uap->offset)));
1801 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
1804 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
1805 PAIR32TO64(off_t, uap->offset)));
1809 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
1814 error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
1816 /* Expand the quad return into two parts for eax and edx */
1817 pos = *(off_t *)(td->td_retval);
1818 td->td_retval[RETVAL_LO] = pos & 0xffffffff; /* %eax */
1819 td->td_retval[RETVAL_HI] = pos >> 32; /* %edx */
1824 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
1827 return (kern_truncate(td, uap->path, UIO_USERSPACE,
1828 PAIR32TO64(off_t, uap->length)));
1832 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
1835 return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
1837 #endif /* COMPAT_FREEBSD6 */
1847 freebsd32_do_sendfile(struct thread *td,
1848 struct freebsd32_sendfile_args *uap, int compat)
1850 struct sf_hdtr32 hdtr32;
1851 struct sf_hdtr hdtr;
1852 struct uio *hdr_uio, *trl_uio;
1854 cap_rights_t rights;
1855 struct iovec32 *iov32;
1856 off_t offset, sbytes;
1859 offset = PAIR32TO64(off_t, uap->offset);
1863 hdr_uio = trl_uio = NULL;
1865 if (uap->hdtr != NULL) {
1866 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
1869 PTRIN_CP(hdtr32, hdtr, headers);
1870 CP(hdtr32, hdtr, hdr_cnt);
1871 PTRIN_CP(hdtr32, hdtr, trailers);
1872 CP(hdtr32, hdtr, trl_cnt);
1874 if (hdtr.headers != NULL) {
1875 iov32 = PTRIN(hdtr32.headers);
1876 error = freebsd32_copyinuio(iov32,
1877 hdtr32.hdr_cnt, &hdr_uio);
1880 #ifdef COMPAT_FREEBSD4
1882 * In FreeBSD < 5.0 the nbytes to send also included
1883 * the header. If compat is specified subtract the
1884 * header size from nbytes.
1887 if (uap->nbytes > hdr_uio->uio_resid)
1888 uap->nbytes -= hdr_uio->uio_resid;
1894 if (hdtr.trailers != NULL) {
1895 iov32 = PTRIN(hdtr32.trailers);
1896 error = freebsd32_copyinuio(iov32,
1897 hdtr32.trl_cnt, &trl_uio);
1903 AUDIT_ARG_FD(uap->fd);
1905 if ((error = fget_read(td, uap->fd,
1906 cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
1909 error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
1910 uap->nbytes, &sbytes, uap->flags, td);
1913 if (uap->sbytes != NULL)
1914 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1918 free(hdr_uio, M_IOV);
1920 free(trl_uio, M_IOV);
1924 #ifdef COMPAT_FREEBSD4
1926 freebsd4_freebsd32_sendfile(struct thread *td,
1927 struct freebsd4_freebsd32_sendfile_args *uap)
1929 return (freebsd32_do_sendfile(td,
1930 (struct freebsd32_sendfile_args *)uap, 1));
1935 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
1938 return (freebsd32_do_sendfile(td, uap, 0));
1942 copy_stat(struct stat *in, struct stat32 *out)
1945 CP(*in, *out, st_dev);
1946 CP(*in, *out, st_ino);
1947 CP(*in, *out, st_mode);
1948 CP(*in, *out, st_nlink);
1949 CP(*in, *out, st_uid);
1950 CP(*in, *out, st_gid);
1951 CP(*in, *out, st_rdev);
1952 TS_CP(*in, *out, st_atim);
1953 TS_CP(*in, *out, st_mtim);
1954 TS_CP(*in, *out, st_ctim);
1955 CP(*in, *out, st_size);
1956 CP(*in, *out, st_blocks);
1957 CP(*in, *out, st_blksize);
1958 CP(*in, *out, st_flags);
1959 CP(*in, *out, st_gen);
1960 TS_CP(*in, *out, st_birthtim);
1961 out->st_padding0 = 0;
1962 out->st_padding1 = 0;
1963 #ifdef __STAT32_TIME_T_EXT
1964 out->st_atim_ext = 0;
1965 out->st_mtim_ext = 0;
1966 out->st_ctim_ext = 0;
1967 out->st_btim_ext = 0;
1969 bzero(out->st_spare, sizeof(out->st_spare));
1974 copy_ostat(struct stat *in, struct ostat32 *out)
1977 bzero(out, sizeof(*out));
1978 CP(*in, *out, st_dev);
1979 CP(*in, *out, st_ino);
1980 CP(*in, *out, st_mode);
1981 CP(*in, *out, st_nlink);
1982 CP(*in, *out, st_uid);
1983 CP(*in, *out, st_gid);
1984 CP(*in, *out, st_rdev);
1985 out->st_size = MIN(in->st_size, INT32_MAX);
1986 TS_CP(*in, *out, st_atim);
1987 TS_CP(*in, *out, st_mtim);
1988 TS_CP(*in, *out, st_ctim);
1989 CP(*in, *out, st_blksize);
1990 CP(*in, *out, st_blocks);
1991 CP(*in, *out, st_flags);
1992 CP(*in, *out, st_gen);
1998 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
2001 struct ostat32 sb32;
2004 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2008 copy_ostat(&sb, &sb32);
2009 error = copyout(&sb32, uap->ub, sizeof (sb32));
2015 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
2021 error = kern_fstat(td, uap->fd, &ub);
2024 copy_stat(&ub, &ub32);
2025 error = copyout(&ub32, uap->ub, sizeof(ub32));
2031 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
2034 struct ostat32 ub32;
2037 error = kern_fstat(td, uap->fd, &ub);
2040 copy_ostat(&ub, &ub32);
2041 error = copyout(&ub32, uap->ub, sizeof(ub32));
2047 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
2053 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2057 copy_stat(&ub, &ub32);
2058 error = copyout(&ub32, uap->buf, sizeof(ub32));
2064 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
2067 struct ostat32 sb32;
2070 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2071 UIO_USERSPACE, &sb, NULL);
2074 copy_ostat(&sb, &sb32);
2075 error = copyout(&sb32, uap->ub, sizeof (sb32));
2081 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
2088 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2091 error = kern_fhstat(td, fh, &sb);
2094 copy_stat(&sb, &sb32);
2095 error = copyout(&sb32, uap->sb, sizeof (sb32));
2099 #if defined(COMPAT_FREEBSD11)
2100 extern int ino64_trunc_error;
2103 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
2106 CP(*in, *out, st_ino);
2107 if (in->st_ino != out->st_ino) {
2108 switch (ino64_trunc_error) {
2115 out->st_ino = UINT32_MAX;
2119 CP(*in, *out, st_nlink);
2120 if (in->st_nlink != out->st_nlink) {
2121 switch (ino64_trunc_error) {
2128 out->st_nlink = UINT16_MAX;
2132 out->st_dev = in->st_dev;
2133 if (out->st_dev != in->st_dev) {
2134 switch (ino64_trunc_error) {
2141 CP(*in, *out, st_mode);
2142 CP(*in, *out, st_uid);
2143 CP(*in, *out, st_gid);
2144 out->st_rdev = in->st_rdev;
2145 if (out->st_rdev != in->st_rdev) {
2146 switch (ino64_trunc_error) {
2153 TS_CP(*in, *out, st_atim);
2154 TS_CP(*in, *out, st_mtim);
2155 TS_CP(*in, *out, st_ctim);
2156 CP(*in, *out, st_size);
2157 CP(*in, *out, st_blocks);
2158 CP(*in, *out, st_blksize);
2159 CP(*in, *out, st_flags);
2160 CP(*in, *out, st_gen);
2161 TS_CP(*in, *out, st_birthtim);
2163 bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
2164 sizeof(*out) - offsetof(struct freebsd11_stat32,
2165 st_birthtim) - sizeof(out->st_birthtim));
2170 freebsd11_freebsd32_stat(struct thread *td,
2171 struct freebsd11_freebsd32_stat_args *uap)
2174 struct freebsd11_stat32 sb32;
2177 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
2181 error = freebsd11_cvtstat32(&sb, &sb32);
2183 error = copyout(&sb32, uap->ub, sizeof (sb32));
2188 freebsd11_freebsd32_fstat(struct thread *td,
2189 struct freebsd11_freebsd32_fstat_args *uap)
2192 struct freebsd11_stat32 sb32;
2195 error = kern_fstat(td, uap->fd, &sb);
2198 error = freebsd11_cvtstat32(&sb, &sb32);
2200 error = copyout(&sb32, uap->ub, sizeof (sb32));
2205 freebsd11_freebsd32_fstatat(struct thread *td,
2206 struct freebsd11_freebsd32_fstatat_args *uap)
2209 struct freebsd11_stat32 sb32;
2212 error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
2216 error = freebsd11_cvtstat32(&sb, &sb32);
2218 error = copyout(&sb32, uap->buf, sizeof (sb32));
2223 freebsd11_freebsd32_lstat(struct thread *td,
2224 struct freebsd11_freebsd32_lstat_args *uap)
2227 struct freebsd11_stat32 sb32;
2230 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2231 UIO_USERSPACE, &sb, NULL);
2234 error = freebsd11_cvtstat32(&sb, &sb32);
2236 error = copyout(&sb32, uap->ub, sizeof (sb32));
2241 freebsd11_freebsd32_fhstat(struct thread *td,
2242 struct freebsd11_freebsd32_fhstat_args *uap)
2245 struct freebsd11_stat32 sb32;
2249 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2252 error = kern_fhstat(td, fh, &sb);
2255 error = freebsd11_cvtstat32(&sb, &sb32);
2257 error = copyout(&sb32, uap->sb, sizeof (sb32));
2263 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
2265 int error, name[CTL_MAXNAME];
2269 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
2271 error = copyin(uap->name, name, uap->namelen * sizeof(int));
2275 error = fueword32(uap->oldlenp, &tmp);
2282 error = userland_sysctl(td, name, uap->namelen,
2283 uap->old, &oldlen, 1,
2284 uap->new, uap->newlen, &j, SCTL_MASK32);
2288 suword32(uap->oldlenp, j);
2292 #ifdef COMPAT_FREEBSD11
2294 freebsd11_freebsd32_jail(struct thread *td,
2295 struct freebsd11_freebsd32_jail_args *uap)
2301 error = copyin(uap->jail, &version, sizeof(uint32_t));
2308 /* FreeBSD single IPv4 jails. */
2309 struct jail32_v0 j32_v0;
2311 bzero(&j, sizeof(struct jail));
2312 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
2315 CP(j32_v0, j, version);
2316 PTRIN_CP(j32_v0, j, path);
2317 PTRIN_CP(j32_v0, j, hostname);
2318 j.ip4s = htonl(j32_v0.ip_number); /* jail_v0 is host order */
2324 * Version 1 was used by multi-IPv4 jail implementations
2325 * that never made it into the official kernel.
2329 case 2: /* JAIL_API_VERSION */
2331 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
2334 error = copyin(uap->jail, &j32, sizeof(struct jail32));
2337 CP(j32, j, version);
2338 PTRIN_CP(j32, j, path);
2339 PTRIN_CP(j32, j, hostname);
2340 PTRIN_CP(j32, j, jailname);
2343 PTRIN_CP(j32, j, ip4);
2344 PTRIN_CP(j32, j, ip6);
2349 /* Sci-Fi jails are not supported, sorry. */
2352 return (freebsd11_kern_jail(td, &j));
2354 #endif /* COMPAT_FREEBSD11 */
2357 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
2362 /* Check that we have an even number of iovecs. */
2363 if (uap->iovcnt & 1)
2366 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2369 error = kern_jail_set(td, auio, uap->flags);
2375 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
2377 struct iovec32 iov32;
2381 /* Check that we have an even number of iovecs. */
2382 if (uap->iovcnt & 1)
2385 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
2388 error = kern_jail_get(td, auio, uap->flags);
2390 for (i = 0; i < uap->iovcnt; i++) {
2391 PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
2392 CP(auio->uio_iov[i], iov32, iov_len);
2393 error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
2402 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
2404 struct sigaction32 s32;
2405 struct sigaction sa, osa, *sap;
2409 error = copyin(uap->act, &s32, sizeof(s32));
2412 sa.sa_handler = PTRIN(s32.sa_u);
2413 CP(s32, sa, sa_flags);
2414 CP(s32, sa, sa_mask);
2418 error = kern_sigaction(td, uap->sig, sap, &osa, 0);
2419 if (error == 0 && uap->oact != NULL) {
2420 s32.sa_u = PTROUT(osa.sa_handler);
2421 CP(osa, s32, sa_flags);
2422 CP(osa, s32, sa_mask);
2423 error = copyout(&s32, uap->oact, sizeof(s32));
2428 #ifdef COMPAT_FREEBSD4
2430 freebsd4_freebsd32_sigaction(struct thread *td,
2431 struct freebsd4_freebsd32_sigaction_args *uap)
2433 struct sigaction32 s32;
2434 struct sigaction sa, osa, *sap;
2438 error = copyin(uap->act, &s32, sizeof(s32));
2441 sa.sa_handler = PTRIN(s32.sa_u);
2442 CP(s32, sa, sa_flags);
2443 CP(s32, sa, sa_mask);
2447 error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
2448 if (error == 0 && uap->oact != NULL) {
2449 s32.sa_u = PTROUT(osa.sa_handler);
2450 CP(osa, s32, sa_flags);
2451 CP(osa, s32, sa_mask);
2452 error = copyout(&s32, uap->oact, sizeof(s32));
2459 struct osigaction32 {
2468 ofreebsd32_sigaction(struct thread *td,
2469 struct ofreebsd32_sigaction_args *uap)
2471 struct osigaction32 s32;
2472 struct sigaction sa, osa, *sap;
2475 if (uap->signum <= 0 || uap->signum >= ONSIG)
2479 error = copyin(uap->nsa, &s32, sizeof(s32));
2482 sa.sa_handler = PTRIN(s32.sa_u);
2483 CP(s32, sa, sa_flags);
2484 OSIG2SIG(s32.sa_mask, sa.sa_mask);
2488 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2489 if (error == 0 && uap->osa != NULL) {
2490 s32.sa_u = PTROUT(osa.sa_handler);
2491 CP(osa, s32, sa_flags);
2492 SIG2OSIG(osa.sa_mask, s32.sa_mask);
2493 error = copyout(&s32, uap->osa, sizeof(s32));
2499 ofreebsd32_sigprocmask(struct thread *td,
2500 struct ofreebsd32_sigprocmask_args *uap)
2505 OSIG2SIG(uap->mask, set);
2506 error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
2507 SIG2OSIG(oset, td->td_retval[0]);
2512 ofreebsd32_sigpending(struct thread *td,
2513 struct ofreebsd32_sigpending_args *uap)
2515 struct proc *p = td->td_proc;
2519 siglist = p->p_siglist;
2520 SIGSETOR(siglist, td->td_siglist);
2522 SIG2OSIG(siglist, td->td_retval[0]);
2527 u_int32_t sv_handler;
2533 ofreebsd32_sigvec(struct thread *td,
2534 struct ofreebsd32_sigvec_args *uap)
2536 struct sigvec32 vec;
2537 struct sigaction sa, osa, *sap;
2540 if (uap->signum <= 0 || uap->signum >= ONSIG)
2544 error = copyin(uap->nsv, &vec, sizeof(vec));
2547 sa.sa_handler = PTRIN(vec.sv_handler);
2548 OSIG2SIG(vec.sv_mask, sa.sa_mask);
2549 sa.sa_flags = vec.sv_flags;
2550 sa.sa_flags ^= SA_RESTART;
2554 error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
2555 if (error == 0 && uap->osv != NULL) {
2556 vec.sv_handler = PTROUT(osa.sa_handler);
2557 SIG2OSIG(osa.sa_mask, vec.sv_mask);
2558 vec.sv_flags = osa.sa_flags;
2559 vec.sv_flags &= ~SA_NOCLDWAIT;
2560 vec.sv_flags ^= SA_RESTART;
2561 error = copyout(&vec, uap->osv, sizeof(vec));
2567 ofreebsd32_sigblock(struct thread *td,
2568 struct ofreebsd32_sigblock_args *uap)
2572 OSIG2SIG(uap->mask, set);
2573 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
2574 SIG2OSIG(oset, td->td_retval[0]);
2579 ofreebsd32_sigsetmask(struct thread *td,
2580 struct ofreebsd32_sigsetmask_args *uap)
2584 OSIG2SIG(uap->mask, set);
2585 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
2586 SIG2OSIG(oset, td->td_retval[0]);
2591 ofreebsd32_sigsuspend(struct thread *td,
2592 struct ofreebsd32_sigsuspend_args *uap)
2596 OSIG2SIG(uap->mask, mask);
2597 return (kern_sigsuspend(td, mask));
2606 ofreebsd32_sigstack(struct thread *td,
2607 struct ofreebsd32_sigstack_args *uap)
2609 struct sigstack32 s32;
2610 struct sigstack nss, oss;
2611 int error = 0, unss;
2613 if (uap->nss != NULL) {
2614 error = copyin(uap->nss, &s32, sizeof(s32));
2617 nss.ss_sp = PTRIN(s32.ss_sp);
2618 CP(s32, nss, ss_onstack);
2623 oss.ss_sp = td->td_sigstk.ss_sp;
2624 oss.ss_onstack = sigonstack(cpu_getstack(td));
2626 td->td_sigstk.ss_sp = nss.ss_sp;
2627 td->td_sigstk.ss_size = 0;
2628 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
2629 td->td_pflags |= TDP_ALTSTACK;
2631 if (uap->oss != NULL) {
2632 s32.ss_sp = PTROUT(oss.ss_sp);
2633 CP(oss, s32, ss_onstack);
2634 error = copyout(&s32, uap->oss, sizeof(s32));
2641 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
2644 return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
2645 TIMER_RELTIME, uap->rqtp, uap->rmtp));
2649 freebsd32_clock_nanosleep(struct thread *td,
2650 struct freebsd32_clock_nanosleep_args *uap)
2654 error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
2655 uap->rqtp, uap->rmtp);
2656 return (kern_posix_error(td, error));
2660 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
2661 int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
2663 struct timespec32 rmt32, rqt32;
2664 struct timespec rmt, rqt;
2667 error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
2671 CP(rqt32, rqt, tv_sec);
2672 CP(rqt32, rqt, tv_nsec);
2674 if (ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0 &&
2675 !useracc(ua_rmtp, sizeof(rmt32), VM_PROT_WRITE))
2677 error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
2678 if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
2681 CP(rmt, rmt32, tv_sec);
2682 CP(rmt, rmt32, tv_nsec);
2684 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
2692 freebsd32_clock_gettime(struct thread *td,
2693 struct freebsd32_clock_gettime_args *uap)
2695 struct timespec ats;
2696 struct timespec32 ats32;
2699 error = kern_clock_gettime(td, uap->clock_id, &ats);
2701 CP(ats, ats32, tv_sec);
2702 CP(ats, ats32, tv_nsec);
2703 error = copyout(&ats32, uap->tp, sizeof(ats32));
2709 freebsd32_clock_settime(struct thread *td,
2710 struct freebsd32_clock_settime_args *uap)
2712 struct timespec ats;
2713 struct timespec32 ats32;
2716 error = copyin(uap->tp, &ats32, sizeof(ats32));
2719 CP(ats32, ats, tv_sec);
2720 CP(ats32, ats, tv_nsec);
2722 return (kern_clock_settime(td, uap->clock_id, &ats));
2726 freebsd32_clock_getres(struct thread *td,
2727 struct freebsd32_clock_getres_args *uap)
2730 struct timespec32 ts32;
2733 if (uap->tp == NULL)
2735 error = kern_clock_getres(td, uap->clock_id, &ts);
2737 CP(ts, ts32, tv_sec);
2738 CP(ts, ts32, tv_nsec);
2739 error = copyout(&ts32, uap->tp, sizeof(ts32));
2744 int freebsd32_ktimer_create(struct thread *td,
2745 struct freebsd32_ktimer_create_args *uap)
2747 struct sigevent32 ev32;
2748 struct sigevent ev, *evp;
2751 if (uap->evp == NULL) {
2755 error = copyin(uap->evp, &ev32, sizeof(ev32));
2758 error = convert_sigevent32(&ev32, &ev);
2762 error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
2764 error = copyout(&id, uap->timerid, sizeof(int));
2766 kern_ktimer_delete(td, id);
2772 freebsd32_ktimer_settime(struct thread *td,
2773 struct freebsd32_ktimer_settime_args *uap)
2775 struct itimerspec32 val32, oval32;
2776 struct itimerspec val, oval, *ovalp;
2779 error = copyin(uap->value, &val32, sizeof(val32));
2783 ovalp = uap->ovalue != NULL ? &oval : NULL;
2784 error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
2785 if (error == 0 && uap->ovalue != NULL) {
2786 ITS_CP(oval, oval32);
2787 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
2793 freebsd32_ktimer_gettime(struct thread *td,
2794 struct freebsd32_ktimer_gettime_args *uap)
2796 struct itimerspec32 val32;
2797 struct itimerspec val;
2800 error = kern_ktimer_gettime(td, uap->timerid, &val);
2803 error = copyout(&val32, uap->value, sizeof(val32));
2809 freebsd32_clock_getcpuclockid2(struct thread *td,
2810 struct freebsd32_clock_getcpuclockid2_args *uap)
2815 error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
2816 uap->which, &clk_id);
2818 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
2823 freebsd32_thr_new(struct thread *td,
2824 struct freebsd32_thr_new_args *uap)
2826 struct thr_param32 param32;
2827 struct thr_param param;
2830 if (uap->param_size < 0 ||
2831 uap->param_size > sizeof(struct thr_param32))
2833 bzero(¶m, sizeof(struct thr_param));
2834 bzero(¶m32, sizeof(struct thr_param32));
2835 error = copyin(uap->param, ¶m32, uap->param_size);
2838 param.start_func = PTRIN(param32.start_func);
2839 param.arg = PTRIN(param32.arg);
2840 param.stack_base = PTRIN(param32.stack_base);
2841 param.stack_size = param32.stack_size;
2842 param.tls_base = PTRIN(param32.tls_base);
2843 param.tls_size = param32.tls_size;
2844 param.child_tid = PTRIN(param32.child_tid);
2845 param.parent_tid = PTRIN(param32.parent_tid);
2846 param.flags = param32.flags;
2847 param.rtp = PTRIN(param32.rtp);
2848 param.spare[0] = PTRIN(param32.spare[0]);
2849 param.spare[1] = PTRIN(param32.spare[1]);
2850 param.spare[2] = PTRIN(param32.spare[2]);
2852 return (kern_thr_new(td, ¶m));
2856 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
2858 struct timespec32 ts32;
2859 struct timespec ts, *tsp;
2864 if (uap->timeout != NULL) {
2865 error = copyin((const void *)uap->timeout, (void *)&ts32,
2866 sizeof(struct timespec32));
2869 ts.tv_sec = ts32.tv_sec;
2870 ts.tv_nsec = ts32.tv_nsec;
2873 return (kern_thr_suspend(td, tsp));
2877 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
2879 bzero(dst, sizeof(*dst));
2880 dst->si_signo = src->si_signo;
2881 dst->si_errno = src->si_errno;
2882 dst->si_code = src->si_code;
2883 dst->si_pid = src->si_pid;
2884 dst->si_uid = src->si_uid;
2885 dst->si_status = src->si_status;
2886 dst->si_addr = (uintptr_t)src->si_addr;
2887 dst->si_value.sival_int = src->si_value.sival_int;
2888 dst->si_timerid = src->si_timerid;
2889 dst->si_overrun = src->si_overrun;
2892 #ifndef _FREEBSD32_SYSPROTO_H_
2893 struct freebsd32_sigqueue_args {
2896 /* union sigval32 */ int value;
2900 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
2905 * On 32-bit ABIs, sival_int and sival_ptr are the same.
2906 * On 64-bit little-endian ABIs, the low bits are the same.
2907 * In 64-bit big-endian ABIs, sival_int overlaps with
2908 * sival_ptr's HIGH bits. We choose to support sival_int
2909 * rather than sival_ptr in this case as it seems to be
2912 bzero(&sv, sizeof(sv));
2913 sv.sival_int = uap->value;
2915 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2919 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
2921 struct timespec32 ts32;
2923 struct timespec *timeout;
2926 struct siginfo32 si32;
2930 error = copyin(uap->timeout, &ts32, sizeof(ts32));
2933 ts.tv_sec = ts32.tv_sec;
2934 ts.tv_nsec = ts32.tv_nsec;
2939 error = copyin(uap->set, &set, sizeof(set));
2943 error = kern_sigtimedwait(td, set, &ksi, timeout);
2948 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2949 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2953 td->td_retval[0] = ksi.ksi_signo;
2961 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
2964 struct siginfo32 si32;
2968 error = copyin(uap->set, &set, sizeof(set));
2972 error = kern_sigtimedwait(td, set, &ksi, NULL);
2977 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
2978 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
2981 td->td_retval[0] = ksi.ksi_signo;
2986 freebsd32_cpuset_setid(struct thread *td,
2987 struct freebsd32_cpuset_setid_args *uap)
2990 return (kern_cpuset_setid(td, uap->which,
2991 PAIR32TO64(id_t, uap->id), uap->setid));
2995 freebsd32_cpuset_getid(struct thread *td,
2996 struct freebsd32_cpuset_getid_args *uap)
2999 return (kern_cpuset_getid(td, uap->level, uap->which,
3000 PAIR32TO64(id_t, uap->id), uap->setid));
3004 freebsd32_cpuset_getaffinity(struct thread *td,
3005 struct freebsd32_cpuset_getaffinity_args *uap)
3008 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
3009 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3013 freebsd32_cpuset_setaffinity(struct thread *td,
3014 struct freebsd32_cpuset_setaffinity_args *uap)
3017 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
3018 PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask));
3022 freebsd32_cpuset_getdomain(struct thread *td,
3023 struct freebsd32_cpuset_getdomain_args *uap)
3026 return (kern_cpuset_getdomain(td, uap->level, uap->which,
3027 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3031 freebsd32_cpuset_setdomain(struct thread *td,
3032 struct freebsd32_cpuset_setdomain_args *uap)
3035 return (kern_cpuset_setdomain(td, uap->level, uap->which,
3036 PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy));
3040 freebsd32_nmount(struct thread *td,
3041 struct freebsd32_nmount_args /* {
3043 unsigned int iovcnt;
3052 * Mount flags are now 64-bits. On 32-bit archtectures only
3053 * 32-bits are passed in, but from here on everything handles
3054 * 64-bit flags correctly.
3058 AUDIT_ARG_FFLAGS(flags);
3061 * Filter out MNT_ROOTFS. We do not want clients of nmount() in
3062 * userspace to set this flag, but we must filter it out if we want
3063 * MNT_UPDATE on the root file system to work.
3064 * MNT_ROOTFS should only be set by the kernel when mounting its
3067 flags &= ~MNT_ROOTFS;
3070 * check that we have an even number of iovec's
3071 * and that we have at least two options.
3073 if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
3076 error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
3079 error = vfs_donmount(td, flags, auio);
3087 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
3089 struct yyy32 *p32, s32;
3090 struct yyy *p = NULL, s;
3095 error = copyin(uap->zzz, &s32, sizeof(s32));
3101 error = kern_xxx(td, p);
3106 error = copyout(&s32, p32, sizeof(s32));
3113 syscall32_module_handler(struct module *mod, int what, void *arg)
3116 return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
3120 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
3123 return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
3127 syscall32_helper_unregister(struct syscall_helper_data *sd)
3130 return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
3134 freebsd32_copyout_strings(struct image_params *imgp)
3140 u_int32_t *stack_base;
3141 struct freebsd32_ps_strings *arginfo;
3142 char canary[sizeof(long) * 8];
3143 int32_t pagesizes32[MAXPAGESIZES];
3144 size_t execpath_len;
3148 * Calculate string base and vector table pointers.
3149 * Also deal with signal trampoline code for this exec type.
3151 if (imgp->execpath != NULL && imgp->auxargs != NULL)
3152 execpath_len = strlen(imgp->execpath) + 1;
3155 arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
3157 if (imgp->proc->p_sysent->sv_sigcode_base == 0)
3158 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
3161 destp = (uintptr_t)arginfo;
3166 if (szsigcode != 0) {
3168 destp = rounddown2(destp, sizeof(uint32_t));
3169 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
3174 * Copy the image path for the rtld.
3176 if (execpath_len != 0) {
3177 destp -= execpath_len;
3178 imgp->execpathp = destp;
3179 copyout(imgp->execpath, (void *)destp, execpath_len);
3183 * Prepare the canary for SSP.
3185 arc4rand(canary, sizeof(canary), 0);
3186 destp -= sizeof(canary);
3187 imgp->canary = destp;
3188 copyout(canary, (void *)destp, sizeof(canary));
3189 imgp->canarylen = sizeof(canary);
3192 * Prepare the pagesizes array.
3194 for (i = 0; i < MAXPAGESIZES; i++)
3195 pagesizes32[i] = (uint32_t)pagesizes[i];
3196 destp -= sizeof(pagesizes32);
3197 destp = rounddown2(destp, sizeof(uint32_t));
3198 imgp->pagesizes = destp;
3199 copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
3200 imgp->pagesizeslen = sizeof(pagesizes32);
3202 destp -= ARG_MAX - imgp->args->stringspace;
3203 destp = rounddown2(destp, sizeof(uint32_t));
3205 vectp = (uint32_t *)destp;
3206 if (imgp->auxargs) {
3208 * Allocate room on the stack for the ELF auxargs
3209 * array. It has up to AT_COUNT entries.
3211 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
3216 * Allocate room for the argv[] and env vectors including the
3217 * terminating NULL pointers.
3219 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
3222 * vectp also becomes our initial stack base
3226 stringp = imgp->args->begin_argv;
3227 argc = imgp->args->argc;
3228 envc = imgp->args->envc;
3230 * Copy out strings - arguments and environment.
3232 copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
3235 * Fill in "ps_strings" struct for ps, w, etc.
3237 suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
3238 suword32(&arginfo->ps_nargvstr, argc);
3241 * Fill in argument portion of vector table.
3243 for (; argc > 0; --argc) {
3244 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3245 while (*stringp++ != 0)
3250 /* a null vector table pointer separates the argp's from the envp's */
3251 suword32(vectp++, 0);
3253 suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
3254 suword32(&arginfo->ps_nenvstr, envc);
3257 * Fill in environment portion of vector table.
3259 for (; envc > 0; --envc) {
3260 suword32(vectp++, (u_int32_t)(intptr_t)destp);
3261 while (*stringp++ != 0)
3266 /* end of vector table is a null pointer */
3269 return ((register_t *)stack_base);
3273 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
3275 struct kld_file_stat *stat;
3276 struct kld32_file_stat *stat32;
3279 if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
3282 if (version != sizeof(struct kld32_file_stat_1) &&
3283 version != sizeof(struct kld32_file_stat))
3286 stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
3287 stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
3288 error = kern_kldstat(td, uap->fileid, stat);
3290 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
3291 CP(*stat, *stat32, refs);
3292 CP(*stat, *stat32, id);
3293 PTROUT_CP(*stat, *stat32, address);
3294 CP(*stat, *stat32, size);
3295 bcopy(&stat->pathname[0], &stat32->pathname[0],
3296 sizeof(stat->pathname));
3297 stat32->version = version;
3298 error = copyout(stat32, uap->stat, version);
3301 free(stat32, M_TEMP);
3306 freebsd32_posix_fallocate(struct thread *td,
3307 struct freebsd32_posix_fallocate_args *uap)
3311 error = kern_posix_fallocate(td, uap->fd,
3312 PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
3313 return (kern_posix_error(td, error));
3317 freebsd32_posix_fadvise(struct thread *td,
3318 struct freebsd32_posix_fadvise_args *uap)
3322 error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
3323 PAIR32TO64(off_t, uap->len), uap->advice);
3324 return (kern_posix_error(td, error));
3328 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
3331 CP(*sig32, *sig, sigev_notify);
3332 switch (sig->sigev_notify) {
3335 case SIGEV_THREAD_ID:
3336 CP(*sig32, *sig, sigev_notify_thread_id);
3339 CP(*sig32, *sig, sigev_signo);
3340 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3343 CP(*sig32, *sig, sigev_notify_kqueue);
3344 CP(*sig32, *sig, sigev_notify_kevent_flags);
3345 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
3354 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
3358 struct procctl_reaper_status rs;
3359 struct procctl_reaper_pids rp;
3360 struct procctl_reaper_kill rk;
3363 struct procctl_reaper_pids32 rp;
3365 int error, error1, flags, signum;
3369 case PROC_TRACE_CTL:
3370 case PROC_TRAPCAP_CTL:
3371 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
3376 case PROC_REAP_ACQUIRE:
3377 case PROC_REAP_RELEASE:
3378 if (uap->data != NULL)
3382 case PROC_REAP_STATUS:
3385 case PROC_REAP_GETPIDS:
3386 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
3389 CP(x32.rp, x.rp, rp_count);
3390 PTRIN_CP(x32.rp, x.rp, rp_pids);
3393 case PROC_REAP_KILL:
3394 error = copyin(uap->data, &x.rk, sizeof(x.rk));
3399 case PROC_TRACE_STATUS:
3400 case PROC_TRAPCAP_STATUS:
3403 case PROC_PDEATHSIG_CTL:
3404 error = copyin(uap->data, &signum, sizeof(signum));
3409 case PROC_PDEATHSIG_STATUS:
3415 error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
3418 case PROC_REAP_STATUS:
3420 error = copyout(&x.rs, uap->data, sizeof(x.rs));
3422 case PROC_REAP_KILL:
3423 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
3427 case PROC_TRACE_STATUS:
3428 case PROC_TRAPCAP_STATUS:
3430 error = copyout(&flags, uap->data, sizeof(flags));
3432 case PROC_PDEATHSIG_STATUS:
3434 error = copyout(&signum, uap->data, sizeof(signum));
3441 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
3447 * Do unsigned conversion for arg when operation
3448 * interprets it as flags or pointer.
3450 case F_SETLK_REMOTE:
3459 tmp = (unsigned int)(uap->arg);
3465 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
3469 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
3471 struct timespec32 ts32;
3472 struct timespec ts, *tsp;
3476 if (uap->ts != NULL) {
3477 error = copyin(uap->ts, &ts32, sizeof(ts32));
3480 CP(ts32, ts, tv_sec);
3481 CP(ts32, ts, tv_nsec);
3485 if (uap->set != NULL) {
3486 error = copyin(uap->set, &set, sizeof(set));
3493 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
3497 freebsd32_sched_rr_get_interval(struct thread *td,
3498 struct freebsd32_sched_rr_get_interval_args *uap)
3501 struct timespec32 ts32;
3504 error = kern_sched_rr_get_interval(td, uap->pid, &ts);
3506 CP(ts, ts32, tv_sec);
3507 CP(ts, ts32, tv_nsec);
3508 error = copyout(&ts32, uap->interval, sizeof(ts32));