The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/freebsd32/freebsd32_misc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2002 Doug Rabson
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   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.
   15  *
   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
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include "opt_inet.h"
   33 #include "opt_inet6.h"
   34 #include "opt_ktrace.h"
   35 
   36 #define __ELF_WORD_SIZE 32
   37 
   38 #ifdef COMPAT_FREEBSD11
   39 #define _WANT_FREEBSD11_KEVENT
   40 #endif
   41 
   42 #include <sys/param.h>
   43 #include <sys/bus.h>
   44 #include <sys/capsicum.h>
   45 #include <sys/clock.h>
   46 #include <sys/exec.h>
   47 #include <sys/fcntl.h>
   48 #include <sys/filedesc.h>
   49 #include <sys/imgact.h>
   50 #include <sys/jail.h>
   51 #include <sys/kernel.h>
   52 #include <sys/limits.h>
   53 #include <sys/linker.h>
   54 #include <sys/lock.h>
   55 #include <sys/malloc.h>
   56 #include <sys/file.h>           /* Must come after sys/malloc.h */
   57 #include <sys/imgact.h>
   58 #include <sys/mbuf.h>
   59 #include <sys/mman.h>
   60 #include <sys/module.h>
   61 #include <sys/mount.h>
   62 #include <sys/mutex.h>
   63 #include <sys/namei.h>
   64 #include <sys/proc.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>
   76 #include <sys/stat.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>
   83 #include <sys/thr.h>
   84 #include <sys/timex.h>
   85 #include <sys/unistd.h>
   86 #include <sys/ucontext.h>
   87 #include <sys/umtx.h>
   88 #include <sys/vnode.h>
   89 #include <sys/wait.h>
   90 #include <sys/ipc.h>
   91 #include <sys/msg.h>
   92 #include <sys/sem.h>
   93 #include <sys/shm.h>
   94 #ifdef KTRACE
   95 #include <sys/ktrace.h>
   96 #endif
   97 
   98 #ifdef INET
   99 #include <netinet/in.h>
  100 #endif
  101 
  102 #include <vm/vm.h>
  103 #include <vm/vm_param.h>
  104 #include <vm/pmap.h>
  105 #include <vm/vm_map.h>
  106 #include <vm/vm_object.h>
  107 #include <vm/vm_extern.h>
  108 
  109 #include <machine/cpu.h>
  110 #include <machine/elf.h>
  111 
  112 #include <security/audit/audit.h>
  113 
  114 #include <compat/freebsd32/freebsd32_util.h>
  115 #include <compat/freebsd32/freebsd32.h>
  116 #include <compat/freebsd32/freebsd32_ipc.h>
  117 #include <compat/freebsd32/freebsd32_misc.h>
  118 #include <compat/freebsd32/freebsd32_signal.h>
  119 #include <compat/freebsd32/freebsd32_proto.h>
  120 
  121 FEATURE(compat_freebsd_32bit, "Compatible with 32-bit FreeBSD");
  122 
  123 #ifdef __amd64__
  124 CTASSERT(sizeof(struct timeval32) == 8);
  125 CTASSERT(sizeof(struct timespec32) == 8);
  126 CTASSERT(sizeof(struct itimerval32) == 16);
  127 CTASSERT(sizeof(struct bintime32) == 12);
  128 #endif
  129 CTASSERT(sizeof(struct statfs32) == 256);
  130 #ifdef __amd64__
  131 CTASSERT(sizeof(struct rusage32) == 72);
  132 #endif
  133 CTASSERT(sizeof(struct sigaltstack32) == 12);
  134 #ifdef __amd64__
  135 CTASSERT(sizeof(struct kevent32) == 56);
  136 #else
  137 CTASSERT(sizeof(struct kevent32) == 64);
  138 #endif
  139 CTASSERT(sizeof(struct iovec32) == 8);
  140 CTASSERT(sizeof(struct msghdr32) == 28);
  141 #ifdef __amd64__
  142 CTASSERT(sizeof(struct stat32) == 208);
  143 CTASSERT(sizeof(struct freebsd11_stat32) == 96);
  144 #endif
  145 CTASSERT(sizeof(struct sigaction32) == 24);
  146 
  147 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
  148 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
  149 static int freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
  150     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp);
  151 
  152 void
  153 freebsd32_rusage_out(const struct rusage *s, struct rusage32 *s32)
  154 {
  155 
  156         TV_CP(*s, *s32, ru_utime);
  157         TV_CP(*s, *s32, ru_stime);
  158         CP(*s, *s32, ru_maxrss);
  159         CP(*s, *s32, ru_ixrss);
  160         CP(*s, *s32, ru_idrss);
  161         CP(*s, *s32, ru_isrss);
  162         CP(*s, *s32, ru_minflt);
  163         CP(*s, *s32, ru_majflt);
  164         CP(*s, *s32, ru_nswap);
  165         CP(*s, *s32, ru_inblock);
  166         CP(*s, *s32, ru_oublock);
  167         CP(*s, *s32, ru_msgsnd);
  168         CP(*s, *s32, ru_msgrcv);
  169         CP(*s, *s32, ru_nsignals);
  170         CP(*s, *s32, ru_nvcsw);
  171         CP(*s, *s32, ru_nivcsw);
  172 }
  173 
  174 int
  175 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
  176 {
  177         int error, status;
  178         struct rusage32 ru32;
  179         struct rusage ru, *rup;
  180 
  181         if (uap->rusage != NULL)
  182                 rup = &ru;
  183         else
  184                 rup = NULL;
  185         error = kern_wait(td, uap->pid, &status, uap->options, rup);
  186         if (error)
  187                 return (error);
  188         if (uap->status != NULL)
  189                 error = copyout(&status, uap->status, sizeof(status));
  190         if (uap->rusage != NULL && error == 0) {
  191                 freebsd32_rusage_out(&ru, &ru32);
  192                 error = copyout(&ru32, uap->rusage, sizeof(ru32));
  193         }
  194         return (error);
  195 }
  196 
  197 int
  198 freebsd32_wait6(struct thread *td, struct freebsd32_wait6_args *uap)
  199 {
  200         struct wrusage32 wru32;
  201         struct __wrusage wru, *wrup;
  202         struct siginfo32 si32;
  203         struct __siginfo si, *sip;
  204         int error, status;
  205 
  206         if (uap->wrusage != NULL)
  207                 wrup = &wru;
  208         else
  209                 wrup = NULL;
  210         if (uap->info != NULL) {
  211                 sip = &si;
  212                 bzero(sip, sizeof(*sip));
  213         } else
  214                 sip = NULL;
  215         error = kern_wait6(td, uap->idtype, PAIR32TO64(id_t, uap->id),
  216             &status, uap->options, wrup, sip);
  217         if (error != 0)
  218                 return (error);
  219         if (uap->status != NULL)
  220                 error = copyout(&status, uap->status, sizeof(status));
  221         if (uap->wrusage != NULL && error == 0) {
  222                 freebsd32_rusage_out(&wru.wru_self, &wru32.wru_self);
  223                 freebsd32_rusage_out(&wru.wru_children, &wru32.wru_children);
  224                 error = copyout(&wru32, uap->wrusage, sizeof(wru32));
  225         }
  226         if (uap->info != NULL && error == 0) {
  227                 siginfo_to_siginfo32 (&si, &si32);
  228                 error = copyout(&si32, uap->info, sizeof(si32));
  229         }
  230         return (error);
  231 }
  232 
  233 #ifdef COMPAT_FREEBSD4
  234 static void
  235 copy_statfs(struct statfs *in, struct statfs32 *out)
  236 {
  237 
  238         statfs_scale_blocks(in, INT32_MAX);
  239         bzero(out, sizeof(*out));
  240         CP(*in, *out, f_bsize);
  241         out->f_iosize = MIN(in->f_iosize, INT32_MAX);
  242         CP(*in, *out, f_blocks);
  243         CP(*in, *out, f_bfree);
  244         CP(*in, *out, f_bavail);
  245         out->f_files = MIN(in->f_files, INT32_MAX);
  246         out->f_ffree = MIN(in->f_ffree, INT32_MAX);
  247         CP(*in, *out, f_fsid);
  248         CP(*in, *out, f_owner);
  249         CP(*in, *out, f_type);
  250         CP(*in, *out, f_flags);
  251         out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
  252         out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
  253         strlcpy(out->f_fstypename,
  254               in->f_fstypename, MFSNAMELEN);
  255         strlcpy(out->f_mntonname,
  256               in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
  257         out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
  258         out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
  259         strlcpy(out->f_mntfromname,
  260               in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
  261 }
  262 #endif
  263 
  264 #ifdef COMPAT_FREEBSD4
  265 int
  266 freebsd4_freebsd32_getfsstat(struct thread *td,
  267     struct freebsd4_freebsd32_getfsstat_args *uap)
  268 {
  269         struct statfs *buf, *sp;
  270         struct statfs32 stat32;
  271         size_t count, size, copycount;
  272         int error;
  273 
  274         count = uap->bufsize / sizeof(struct statfs32);
  275         size = count * sizeof(struct statfs);
  276         error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, uap->mode);
  277         if (size > 0) {
  278                 sp = buf;
  279                 copycount = count;
  280                 while (copycount > 0 && error == 0) {
  281                         copy_statfs(sp, &stat32);
  282                         error = copyout(&stat32, uap->buf, sizeof(stat32));
  283                         sp++;
  284                         uap->buf++;
  285                         copycount--;
  286                 }
  287                 free(buf, M_STATFS);
  288         }
  289         if (error == 0)
  290                 td->td_retval[0] = count;
  291         return (error);
  292 }
  293 #endif
  294 
  295 #ifdef COMPAT_FREEBSD10
  296 int
  297 freebsd10_freebsd32_pipe(struct thread *td,
  298     struct freebsd10_freebsd32_pipe_args *uap) {
  299         
  300         return (freebsd10_pipe(td, (struct freebsd10_pipe_args*)uap));
  301 }
  302 #endif
  303 
  304 int
  305 freebsd32_sigaltstack(struct thread *td,
  306                       struct freebsd32_sigaltstack_args *uap)
  307 {
  308         struct sigaltstack32 s32;
  309         struct sigaltstack ss, oss, *ssp;
  310         int error;
  311 
  312         if (uap->ss != NULL) {
  313                 error = copyin(uap->ss, &s32, sizeof(s32));
  314                 if (error)
  315                         return (error);
  316                 PTRIN_CP(s32, ss, ss_sp);
  317                 CP(s32, ss, ss_size);
  318                 CP(s32, ss, ss_flags);
  319                 ssp = &ss;
  320         } else
  321                 ssp = NULL;
  322         error = kern_sigaltstack(td, ssp, &oss);
  323         if (error == 0 && uap->oss != NULL) {
  324                 PTROUT_CP(oss, s32, ss_sp);
  325                 CP(oss, s32, ss_size);
  326                 CP(oss, s32, ss_flags);
  327                 error = copyout(&s32, uap->oss, sizeof(s32));
  328         }
  329         return (error);
  330 }
  331 
  332 /*
  333  * Custom version of exec_copyin_args() so that we can translate
  334  * the pointers.
  335  */
  336 int
  337 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
  338     enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
  339 {
  340         char *argp, *envp;
  341         u_int32_t *p32, arg;
  342         size_t length;
  343         int error;
  344 
  345         bzero(args, sizeof(*args));
  346         if (argv == NULL)
  347                 return (EFAULT);
  348 
  349         /*
  350          * Allocate demand-paged memory for the file name, argument, and
  351          * environment strings.
  352          */
  353         error = exec_alloc_args(args);
  354         if (error != 0)
  355                 return (error);
  356 
  357         /*
  358          * Copy the file name.
  359          */
  360         if (fname != NULL) {
  361                 args->fname = args->buf;
  362                 error = (segflg == UIO_SYSSPACE) ?
  363                     copystr(fname, args->fname, PATH_MAX, &length) :
  364                     copyinstr(fname, args->fname, PATH_MAX, &length);
  365                 if (error != 0)
  366                         goto err_exit;
  367         } else
  368                 length = 0;
  369 
  370         args->begin_argv = args->buf + length;
  371         args->endp = args->begin_argv;
  372         args->stringspace = ARG_MAX;
  373 
  374         /*
  375          * extract arguments first
  376          */
  377         p32 = argv;
  378         for (;;) {
  379                 error = copyin(p32++, &arg, sizeof(arg));
  380                 if (error)
  381                         goto err_exit;
  382                 if (arg == 0)
  383                         break;
  384                 argp = PTRIN(arg);
  385                 error = copyinstr(argp, args->endp, args->stringspace, &length);
  386                 if (error) {
  387                         if (error == ENAMETOOLONG)
  388                                 error = E2BIG;
  389                         goto err_exit;
  390                 }
  391                 args->stringspace -= length;
  392                 args->endp += length;
  393                 args->argc++;
  394         }
  395                         
  396         args->begin_envv = args->endp;
  397 
  398         /*
  399          * extract environment strings
  400          */
  401         if (envv) {
  402                 p32 = envv;
  403                 for (;;) {
  404                         error = copyin(p32++, &arg, sizeof(arg));
  405                         if (error)
  406                                 goto err_exit;
  407                         if (arg == 0)
  408                                 break;
  409                         envp = PTRIN(arg);
  410                         error = copyinstr(envp, args->endp, args->stringspace,
  411                             &length);
  412                         if (error) {
  413                                 if (error == ENAMETOOLONG)
  414                                         error = E2BIG;
  415                                 goto err_exit;
  416                         }
  417                         args->stringspace -= length;
  418                         args->endp += length;
  419                         args->envc++;
  420                 }
  421         }
  422 
  423         return (0);
  424 
  425 err_exit:
  426         exec_free_args(args);
  427         return (error);
  428 }
  429 
  430 int
  431 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
  432 {
  433         struct image_args eargs;
  434         struct vmspace *oldvmspace;
  435         int error;
  436 
  437         error = pre_execve(td, &oldvmspace);
  438         if (error != 0)
  439                 return (error);
  440         error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
  441             uap->argv, uap->envv);
  442         if (error == 0)
  443                 error = kern_execve(td, &eargs, NULL, oldvmspace);
  444         post_execve(td, error, oldvmspace);
  445         AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
  446         return (error);
  447 }
  448 
  449 int
  450 freebsd32_fexecve(struct thread *td, struct freebsd32_fexecve_args *uap)
  451 {
  452         struct image_args eargs;
  453         struct vmspace *oldvmspace;
  454         int error;
  455 
  456         error = pre_execve(td, &oldvmspace);
  457         if (error != 0)
  458                 return (error);
  459         error = freebsd32_exec_copyin_args(&eargs, NULL, UIO_SYSSPACE,
  460             uap->argv, uap->envv);
  461         if (error == 0) {
  462                 eargs.fd = uap->fd;
  463                 error = kern_execve(td, &eargs, NULL, oldvmspace);
  464         }
  465         post_execve(td, error, oldvmspace);
  466         AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
  467         return (error);
  468 }
  469 
  470 
  471 int
  472 freebsd32_mknodat(struct thread *td, struct freebsd32_mknodat_args *uap)
  473 {
  474 
  475         return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE,
  476             uap->mode, PAIR32TO64(dev_t, uap->dev)));
  477 }
  478 
  479 int
  480 freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap)
  481 {
  482         int prot;
  483 
  484         prot = uap->prot;
  485 #if defined(__amd64__)
  486         if (i386_read_exec && (prot & PROT_READ) != 0)
  487                 prot |= PROT_EXEC;
  488 #endif
  489         return (kern_mprotect(td, (uintptr_t)PTRIN(uap->addr), uap->len,
  490             prot));
  491 }
  492 
  493 int
  494 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
  495 {
  496         int prot;
  497 
  498         prot = uap->prot;
  499 #if defined(__amd64__)
  500         if (i386_read_exec && (prot & PROT_READ))
  501                 prot |= PROT_EXEC;
  502 #endif
  503 
  504         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
  505             uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
  506 }
  507 
  508 #ifdef COMPAT_FREEBSD6
  509 int
  510 freebsd6_freebsd32_mmap(struct thread *td,
  511     struct freebsd6_freebsd32_mmap_args *uap)
  512 {
  513         int prot;
  514 
  515         prot = uap->prot;
  516 #if defined(__amd64__)
  517         if (i386_read_exec && (prot & PROT_READ))
  518                 prot |= PROT_EXEC;
  519 #endif
  520 
  521         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot,
  522             uap->flags, uap->fd, PAIR32TO64(off_t, uap->pos)));
  523 }
  524 #endif
  525 
  526 int
  527 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
  528 {
  529         struct itimerval itv, oitv, *itvp;      
  530         struct itimerval32 i32;
  531         int error;
  532 
  533         if (uap->itv != NULL) {
  534                 error = copyin(uap->itv, &i32, sizeof(i32));
  535                 if (error)
  536                         return (error);
  537                 TV_CP(i32, itv, it_interval);
  538                 TV_CP(i32, itv, it_value);
  539                 itvp = &itv;
  540         } else
  541                 itvp = NULL;
  542         error = kern_setitimer(td, uap->which, itvp, &oitv);
  543         if (error || uap->oitv == NULL)
  544                 return (error);
  545         TV_CP(oitv, i32, it_interval);
  546         TV_CP(oitv, i32, it_value);
  547         return (copyout(&i32, uap->oitv, sizeof(i32)));
  548 }
  549 
  550 int
  551 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
  552 {
  553         struct itimerval itv;
  554         struct itimerval32 i32;
  555         int error;
  556 
  557         error = kern_getitimer(td, uap->which, &itv);
  558         if (error || uap->itv == NULL)
  559                 return (error);
  560         TV_CP(itv, i32, it_interval);
  561         TV_CP(itv, i32, it_value);
  562         return (copyout(&i32, uap->itv, sizeof(i32)));
  563 }
  564 
  565 int
  566 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
  567 {
  568         struct timeval32 tv32;
  569         struct timeval tv, *tvp;
  570         int error;
  571 
  572         if (uap->tv != NULL) {
  573                 error = copyin(uap->tv, &tv32, sizeof(tv32));
  574                 if (error)
  575                         return (error);
  576                 CP(tv32, tv, tv_sec);
  577                 CP(tv32, tv, tv_usec);
  578                 tvp = &tv;
  579         } else
  580                 tvp = NULL;
  581         /*
  582          * XXX Do pointers need PTRIN()?
  583          */
  584         return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
  585             sizeof(int32_t) * 8));
  586 }
  587 
  588 int
  589 freebsd32_pselect(struct thread *td, struct freebsd32_pselect_args *uap)
  590 {
  591         struct timespec32 ts32;
  592         struct timespec ts;
  593         struct timeval tv, *tvp;
  594         sigset_t set, *uset;
  595         int error;
  596 
  597         if (uap->ts != NULL) {
  598                 error = copyin(uap->ts, &ts32, sizeof(ts32));
  599                 if (error != 0)
  600                         return (error);
  601                 CP(ts32, ts, tv_sec);
  602                 CP(ts32, ts, tv_nsec);
  603                 TIMESPEC_TO_TIMEVAL(&tv, &ts);
  604                 tvp = &tv;
  605         } else
  606                 tvp = NULL;
  607         if (uap->sm != NULL) {
  608                 error = copyin(uap->sm, &set, sizeof(set));
  609                 if (error != 0)
  610                         return (error);
  611                 uset = &set;
  612         } else
  613                 uset = NULL;
  614         /*
  615          * XXX Do pointers need PTRIN()?
  616          */
  617         error = kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
  618             uset, sizeof(int32_t) * 8);
  619         return (error);
  620 }
  621 
  622 /*
  623  * Copy 'count' items into the destination list pointed to by uap->eventlist.
  624  */
  625 static int
  626 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
  627 {
  628         struct freebsd32_kevent_args *uap;
  629         struct kevent32 ks32[KQ_NEVENTS];
  630         uint64_t e;
  631         int i, j, error;
  632 
  633         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
  634         uap = (struct freebsd32_kevent_args *)arg;
  635 
  636         for (i = 0; i < count; i++) {
  637                 CP(kevp[i], ks32[i], ident);
  638                 CP(kevp[i], ks32[i], filter);
  639                 CP(kevp[i], ks32[i], flags);
  640                 CP(kevp[i], ks32[i], fflags);
  641 #if BYTE_ORDER == LITTLE_ENDIAN
  642                 ks32[i].data1 = kevp[i].data;
  643                 ks32[i].data2 = kevp[i].data >> 32;
  644 #else
  645                 ks32[i].data1 = kevp[i].data >> 32;
  646                 ks32[i].data2 = kevp[i].data;
  647 #endif
  648                 PTROUT_CP(kevp[i], ks32[i], udata);
  649                 for (j = 0; j < nitems(kevp->ext); j++) {
  650                         e = kevp[i].ext[j];
  651 #if BYTE_ORDER == LITTLE_ENDIAN
  652                         ks32[i].ext64[2 * j] = e;
  653                         ks32[i].ext64[2 * j + 1] = e >> 32;
  654 #else
  655                         ks32[i].ext64[2 * j] = e >> 32;
  656                         ks32[i].ext64[2 * j + 1] = e;
  657 #endif
  658                 }
  659         }
  660         error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
  661         if (error == 0)
  662                 uap->eventlist += count;
  663         return (error);
  664 }
  665 
  666 /*
  667  * Copy 'count' items from the list pointed to by uap->changelist.
  668  */
  669 static int
  670 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
  671 {
  672         struct freebsd32_kevent_args *uap;
  673         struct kevent32 ks32[KQ_NEVENTS];
  674         uint64_t e;
  675         int i, j, error;
  676 
  677         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
  678         uap = (struct freebsd32_kevent_args *)arg;
  679 
  680         error = copyin(uap->changelist, ks32, count * sizeof *ks32);
  681         if (error)
  682                 goto done;
  683         uap->changelist += count;
  684 
  685         for (i = 0; i < count; i++) {
  686                 CP(ks32[i], kevp[i], ident);
  687                 CP(ks32[i], kevp[i], filter);
  688                 CP(ks32[i], kevp[i], flags);
  689                 CP(ks32[i], kevp[i], fflags);
  690                 kevp[i].data = PAIR32TO64(uint64_t, ks32[i].data);
  691                 PTRIN_CP(ks32[i], kevp[i], udata);
  692                 for (j = 0; j < nitems(kevp->ext); j++) {
  693 #if BYTE_ORDER == LITTLE_ENDIAN
  694                         e = ks32[i].ext64[2 * j + 1];
  695                         e <<= 32;
  696                         e += ks32[i].ext64[2 * j];
  697 #else
  698                         e = ks32[i].ext64[2 * j];
  699                         e <<= 32;
  700                         e += ks32[i].ext64[2 * j + 1];
  701 #endif
  702                         kevp[i].ext[j] = e;
  703                 }
  704         }
  705 done:
  706         return (error);
  707 }
  708 
  709 int
  710 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
  711 {
  712         struct timespec32 ts32;
  713         struct timespec ts, *tsp;
  714         struct kevent_copyops k_ops = {
  715                 .arg = uap,
  716                 .k_copyout = freebsd32_kevent_copyout,
  717                 .k_copyin = freebsd32_kevent_copyin,
  718         };
  719 #ifdef KTRACE
  720         struct kevent32 *eventlist = uap->eventlist;
  721 #endif
  722         int error;
  723 
  724         if (uap->timeout) {
  725                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
  726                 if (error)
  727                         return (error);
  728                 CP(ts32, ts, tv_sec);
  729                 CP(ts32, ts, tv_nsec);
  730                 tsp = &ts;
  731         } else
  732                 tsp = NULL;
  733 #ifdef KTRACE
  734         if (KTRPOINT(td, KTR_STRUCT_ARRAY))
  735                 ktrstructarray("kevent32", UIO_USERSPACE, uap->changelist,
  736                     uap->nchanges, sizeof(struct kevent32));
  737 #endif
  738         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
  739             &k_ops, tsp);
  740 #ifdef KTRACE
  741         if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
  742                 ktrstructarray("kevent32", UIO_USERSPACE, eventlist,
  743                     td->td_retval[0], sizeof(struct kevent32));
  744 #endif
  745         return (error);
  746 }
  747 
  748 #ifdef COMPAT_FREEBSD11
  749 static int
  750 freebsd32_kevent11_copyout(void *arg, struct kevent *kevp, int count)
  751 {
  752         struct freebsd11_freebsd32_kevent_args *uap;
  753         struct kevent32_freebsd11 ks32[KQ_NEVENTS];
  754         int i, error;
  755 
  756         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
  757         uap = (struct freebsd11_freebsd32_kevent_args *)arg;
  758 
  759         for (i = 0; i < count; i++) {
  760                 CP(kevp[i], ks32[i], ident);
  761                 CP(kevp[i], ks32[i], filter);
  762                 CP(kevp[i], ks32[i], flags);
  763                 CP(kevp[i], ks32[i], fflags);
  764                 CP(kevp[i], ks32[i], data);
  765                 PTROUT_CP(kevp[i], ks32[i], udata);
  766         }
  767         error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
  768         if (error == 0)
  769                 uap->eventlist += count;
  770         return (error);
  771 }
  772 
  773 /*
  774  * Copy 'count' items from the list pointed to by uap->changelist.
  775  */
  776 static int
  777 freebsd32_kevent11_copyin(void *arg, struct kevent *kevp, int count)
  778 {
  779         struct freebsd11_freebsd32_kevent_args *uap;
  780         struct kevent32_freebsd11 ks32[KQ_NEVENTS];
  781         int i, j, error;
  782 
  783         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
  784         uap = (struct freebsd11_freebsd32_kevent_args *)arg;
  785 
  786         error = copyin(uap->changelist, ks32, count * sizeof *ks32);
  787         if (error)
  788                 goto done;
  789         uap->changelist += count;
  790 
  791         for (i = 0; i < count; i++) {
  792                 CP(ks32[i], kevp[i], ident);
  793                 CP(ks32[i], kevp[i], filter);
  794                 CP(ks32[i], kevp[i], flags);
  795                 CP(ks32[i], kevp[i], fflags);
  796                 CP(ks32[i], kevp[i], data);
  797                 PTRIN_CP(ks32[i], kevp[i], udata);
  798                 for (j = 0; j < nitems(kevp->ext); j++)
  799                         kevp[i].ext[j] = 0;
  800         }
  801 done:
  802         return (error);
  803 }
  804 
  805 int
  806 freebsd11_freebsd32_kevent(struct thread *td,
  807     struct freebsd11_freebsd32_kevent_args *uap)
  808 {
  809         struct timespec32 ts32;
  810         struct timespec ts, *tsp;
  811         struct kevent_copyops k_ops = {
  812                 .arg = uap,
  813                 .k_copyout = freebsd32_kevent11_copyout,
  814                 .k_copyin = freebsd32_kevent11_copyin,
  815         };
  816 #ifdef KTRACE
  817         struct kevent32_freebsd11 *eventlist = uap->eventlist;
  818 #endif
  819         int error;
  820 
  821         if (uap->timeout) {
  822                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
  823                 if (error)
  824                         return (error);
  825                 CP(ts32, ts, tv_sec);
  826                 CP(ts32, ts, tv_nsec);
  827                 tsp = &ts;
  828         } else
  829                 tsp = NULL;
  830 #ifdef KTRACE
  831         if (KTRPOINT(td, KTR_STRUCT_ARRAY))
  832                 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
  833                     uap->changelist, uap->nchanges,
  834                     sizeof(struct kevent32_freebsd11));
  835 #endif
  836         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
  837             &k_ops, tsp);
  838 #ifdef KTRACE
  839         if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
  840                 ktrstructarray("kevent32_freebsd11", UIO_USERSPACE,
  841                     eventlist, td->td_retval[0],
  842                     sizeof(struct kevent32_freebsd11));
  843 #endif
  844         return (error);
  845 }
  846 #endif
  847 
  848 int
  849 freebsd32_gettimeofday(struct thread *td,
  850                        struct freebsd32_gettimeofday_args *uap)
  851 {
  852         struct timeval atv;
  853         struct timeval32 atv32;
  854         struct timezone rtz;
  855         int error = 0;
  856 
  857         if (uap->tp) {
  858                 microtime(&atv);
  859                 CP(atv, atv32, tv_sec);
  860                 CP(atv, atv32, tv_usec);
  861                 error = copyout(&atv32, uap->tp, sizeof (atv32));
  862         }
  863         if (error == 0 && uap->tzp != NULL) {
  864                 rtz.tz_minuteswest = tz_minuteswest;
  865                 rtz.tz_dsttime = tz_dsttime;
  866                 error = copyout(&rtz, uap->tzp, sizeof (rtz));
  867         }
  868         return (error);
  869 }
  870 
  871 int
  872 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
  873 {
  874         struct rusage32 s32;
  875         struct rusage s;
  876         int error;
  877 
  878         error = kern_getrusage(td, uap->who, &s);
  879         if (error == 0) {
  880                 freebsd32_rusage_out(&s, &s32);
  881                 error = copyout(&s32, uap->rusage, sizeof(s32));
  882         }
  883         return (error);
  884 }
  885 
  886 static int
  887 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
  888 {
  889         struct iovec32 iov32;
  890         struct iovec *iov;
  891         struct uio *uio;
  892         u_int iovlen;
  893         int error, i;
  894 
  895         *uiop = NULL;
  896         if (iovcnt > UIO_MAXIOV)
  897                 return (EINVAL);
  898         iovlen = iovcnt * sizeof(struct iovec);
  899         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
  900         iov = (struct iovec *)(uio + 1);
  901         for (i = 0; i < iovcnt; i++) {
  902                 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
  903                 if (error) {
  904                         free(uio, M_IOV);
  905                         return (error);
  906                 }
  907                 iov[i].iov_base = PTRIN(iov32.iov_base);
  908                 iov[i].iov_len = iov32.iov_len;
  909         }
  910         uio->uio_iov = iov;
  911         uio->uio_iovcnt = iovcnt;
  912         uio->uio_segflg = UIO_USERSPACE;
  913         uio->uio_offset = -1;
  914         uio->uio_resid = 0;
  915         for (i = 0; i < iovcnt; i++) {
  916                 if (iov->iov_len > INT_MAX - uio->uio_resid) {
  917                         free(uio, M_IOV);
  918                         return (EINVAL);
  919                 }
  920                 uio->uio_resid += iov->iov_len;
  921                 iov++;
  922         }
  923         *uiop = uio;
  924         return (0);
  925 }
  926 
  927 int
  928 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
  929 {
  930         struct uio *auio;
  931         int error;
  932 
  933         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  934         if (error)
  935                 return (error);
  936         error = kern_readv(td, uap->fd, auio);
  937         free(auio, M_IOV);
  938         return (error);
  939 }
  940 
  941 int
  942 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
  943 {
  944         struct uio *auio;
  945         int error;
  946 
  947         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  948         if (error)
  949                 return (error);
  950         error = kern_writev(td, uap->fd, auio);
  951         free(auio, M_IOV);
  952         return (error);
  953 }
  954 
  955 int
  956 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
  957 {
  958         struct uio *auio;
  959         int error;
  960 
  961         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  962         if (error)
  963                 return (error);
  964         error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
  965         free(auio, M_IOV);
  966         return (error);
  967 }
  968 
  969 int
  970 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
  971 {
  972         struct uio *auio;
  973         int error;
  974 
  975         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  976         if (error)
  977                 return (error);
  978         error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset));
  979         free(auio, M_IOV);
  980         return (error);
  981 }
  982 
  983 int
  984 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
  985     int error)
  986 {
  987         struct iovec32 iov32;
  988         struct iovec *iov;
  989         u_int iovlen;
  990         int i;
  991 
  992         *iovp = NULL;
  993         if (iovcnt > UIO_MAXIOV)
  994                 return (error);
  995         iovlen = iovcnt * sizeof(struct iovec);
  996         iov = malloc(iovlen, M_IOV, M_WAITOK);
  997         for (i = 0; i < iovcnt; i++) {
  998                 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
  999                 if (error) {
 1000                         free(iov, M_IOV);
 1001                         return (error);
 1002                 }
 1003                 iov[i].iov_base = PTRIN(iov32.iov_base);
 1004                 iov[i].iov_len = iov32.iov_len;
 1005         }
 1006         *iovp = iov;
 1007         return (0);
 1008 }
 1009 
 1010 static int
 1011 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
 1012 {
 1013         struct msghdr32 m32;
 1014         int error;
 1015 
 1016         error = copyin(msg32, &m32, sizeof(m32));
 1017         if (error)
 1018                 return (error);
 1019         msg->msg_name = PTRIN(m32.msg_name);
 1020         msg->msg_namelen = m32.msg_namelen;
 1021         msg->msg_iov = PTRIN(m32.msg_iov);
 1022         msg->msg_iovlen = m32.msg_iovlen;
 1023         msg->msg_control = PTRIN(m32.msg_control);
 1024         msg->msg_controllen = m32.msg_controllen;
 1025         msg->msg_flags = m32.msg_flags;
 1026         return (0);
 1027 }
 1028 
 1029 static int
 1030 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
 1031 {
 1032         struct msghdr32 m32;
 1033         int error;
 1034 
 1035         m32.msg_name = PTROUT(msg->msg_name);
 1036         m32.msg_namelen = msg->msg_namelen;
 1037         m32.msg_iov = PTROUT(msg->msg_iov);
 1038         m32.msg_iovlen = msg->msg_iovlen;
 1039         m32.msg_control = PTROUT(msg->msg_control);
 1040         m32.msg_controllen = msg->msg_controllen;
 1041         m32.msg_flags = msg->msg_flags;
 1042         error = copyout(&m32, msg32, sizeof(m32));
 1043         return (error);
 1044 }
 1045 
 1046 #ifndef __mips__
 1047 #define FREEBSD32_ALIGNBYTES    (sizeof(int) - 1)
 1048 #else
 1049 #define FREEBSD32_ALIGNBYTES    (sizeof(long) - 1)
 1050 #endif
 1051 #define FREEBSD32_ALIGN(p)      \
 1052         (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
 1053 #define FREEBSD32_CMSG_SPACE(l) \
 1054         (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
 1055 
 1056 #define FREEBSD32_CMSG_DATA(cmsg)       ((unsigned char *)(cmsg) + \
 1057                                  FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
 1058 
 1059 static size_t
 1060 freebsd32_cmsg_convert(const struct cmsghdr *cm, void *data, socklen_t datalen)
 1061 {
 1062         size_t copylen;
 1063         union {
 1064                 struct timespec32 ts;
 1065                 struct timeval32 tv;
 1066                 struct bintime32 bt;
 1067         } tmp32;
 1068 
 1069         union {
 1070                 struct timespec ts;
 1071                 struct timeval tv;
 1072                 struct bintime bt;
 1073         } *in;
 1074 
 1075         in = data;
 1076         copylen = 0;
 1077         switch (cm->cmsg_level) {
 1078         case SOL_SOCKET:
 1079                 switch (cm->cmsg_type) {
 1080                 case SCM_TIMESTAMP:
 1081                         TV_CP(*in, tmp32, tv);
 1082                         copylen = sizeof(tmp32.tv);
 1083                         break;
 1084 
 1085                 case SCM_BINTIME:
 1086                         BT_CP(*in, tmp32, bt);
 1087                         copylen = sizeof(tmp32.bt);
 1088                         break;
 1089 
 1090                 case SCM_REALTIME:
 1091                 case SCM_MONOTONIC:
 1092                         TS_CP(*in, tmp32, ts);
 1093                         copylen = sizeof(tmp32.ts);
 1094                         break;
 1095 
 1096                 default:
 1097                         break;
 1098                 }
 1099 
 1100         default:
 1101                 break;
 1102         }
 1103 
 1104         if (copylen == 0)
 1105                 return (datalen);
 1106 
 1107         KASSERT((datalen >= copylen), ("corrupted cmsghdr"));
 1108 
 1109         bcopy(&tmp32, data, copylen);
 1110         return (copylen);
 1111 }
 1112 
 1113 static int
 1114 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
 1115 {
 1116         struct cmsghdr *cm;
 1117         void *data;
 1118         socklen_t clen, datalen, datalen_out, oldclen;
 1119         int error;
 1120         caddr_t ctlbuf;
 1121         int len, maxlen, copylen;
 1122         struct mbuf *m;
 1123         error = 0;
 1124 
 1125         len    = msg->msg_controllen;
 1126         maxlen = msg->msg_controllen;
 1127         msg->msg_controllen = 0;
 1128 
 1129         ctlbuf = msg->msg_control;
 1130         for (m = control; m != NULL && len > 0; m = m->m_next) {
 1131                 cm = mtod(m, struct cmsghdr *);
 1132                 clen = m->m_len;
 1133                 while (cm != NULL) {
 1134                         if (sizeof(struct cmsghdr) > clen ||
 1135                             cm->cmsg_len > clen) {
 1136                                 error = EINVAL;
 1137                                 break;
 1138                         }
 1139 
 1140                         data   = CMSG_DATA(cm);
 1141                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
 1142                         datalen_out = freebsd32_cmsg_convert(cm, data, datalen);
 1143 
 1144                         /*
 1145                          * Copy out the message header.  Preserve the native
 1146                          * message size in case we need to inspect the message
 1147                          * contents later.
 1148                          */
 1149                         copylen = sizeof(struct cmsghdr);
 1150                         if (len < copylen) {
 1151                                 msg->msg_flags |= MSG_CTRUNC;
 1152                                 m_dispose_extcontrolm(m);
 1153                                 goto exit;
 1154                         }
 1155                         oldclen = cm->cmsg_len;
 1156                         cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
 1157                             datalen_out;
 1158                         error = copyout(cm, ctlbuf, copylen);
 1159                         cm->cmsg_len = oldclen;
 1160                         if (error != 0)
 1161                                 goto exit;
 1162 
 1163                         ctlbuf += FREEBSD32_ALIGN(copylen);
 1164                         len    -= FREEBSD32_ALIGN(copylen);
 1165 
 1166                         copylen = datalen_out;
 1167                         if (len < copylen) {
 1168                                 msg->msg_flags |= MSG_CTRUNC;
 1169                                 m_dispose_extcontrolm(m);
 1170                                 break;
 1171                         }
 1172 
 1173                         /* Copy out the message data. */
 1174                         error = copyout(data, ctlbuf, copylen);
 1175                         if (error)
 1176                                 goto exit;
 1177 
 1178                         ctlbuf += FREEBSD32_ALIGN(copylen);
 1179                         len    -= FREEBSD32_ALIGN(copylen);
 1180 
 1181                         if (CMSG_SPACE(datalen) < clen) {
 1182                                 clen -= CMSG_SPACE(datalen);
 1183                                 cm = (struct cmsghdr *)
 1184                                     ((caddr_t)cm + CMSG_SPACE(datalen));
 1185                         } else {
 1186                                 clen = 0;
 1187                                 cm = NULL;
 1188                         }
 1189 
 1190                         msg->msg_controllen +=
 1191                             FREEBSD32_CMSG_SPACE(datalen_out);
 1192                 }
 1193         }
 1194         if (len == 0 && m != NULL) {
 1195                 msg->msg_flags |= MSG_CTRUNC;
 1196                 m_dispose_extcontrolm(m);
 1197         }
 1198 
 1199 exit:
 1200         return (error);
 1201 }
 1202 
 1203 int
 1204 freebsd32_recvmsg(struct thread *td, struct freebsd32_recvmsg_args *uap)
 1205 {
 1206         struct msghdr msg;
 1207         struct iovec *uiov, *iov;
 1208         struct mbuf *control = NULL;
 1209         struct mbuf **controlp;
 1210         int error;
 1211 
 1212         error = freebsd32_copyinmsghdr(uap->msg, &msg);
 1213         if (error)
 1214                 return (error);
 1215         error = freebsd32_copyiniov((void *)msg.msg_iov, msg.msg_iovlen, &iov,
 1216             EMSGSIZE);
 1217         if (error)
 1218                 return (error);
 1219         msg.msg_flags = uap->flags;
 1220         uiov = msg.msg_iov;
 1221         msg.msg_iov = iov;
 1222 
 1223         controlp = (msg.msg_control != NULL) ?  &control : NULL;
 1224         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
 1225         if (error == 0) {
 1226                 msg.msg_iov = uiov;
 1227 
 1228                 if (control != NULL)
 1229                         error = freebsd32_copy_msg_out(&msg, control);
 1230                 else
 1231                         msg.msg_controllen = 0;
 1232 
 1233                 if (error == 0)
 1234                         error = freebsd32_copyoutmsghdr(&msg, uap->msg);
 1235         }
 1236         free(iov, M_IOV);
 1237 
 1238         if (control != NULL) {
 1239                 if (error != 0)
 1240                         m_dispose_extcontrolm(control);
 1241                 m_freem(control);
 1242         }
 1243 
 1244         return (error);
 1245 }
 1246 
 1247 /*
 1248  * Copy-in the array of control messages constructed using alignment
 1249  * and padding suitable for a 32-bit environment and construct an
 1250  * mbuf using alignment and padding suitable for a 64-bit kernel.
 1251  * The alignment and padding are defined indirectly by CMSG_DATA(),
 1252  * CMSG_SPACE() and CMSG_LEN().
 1253  */
 1254 static int
 1255 freebsd32_copyin_control(struct mbuf **mp, caddr_t buf, u_int buflen)
 1256 {
 1257         struct cmsghdr *cm;
 1258         struct mbuf *m;
 1259         void *in, *in1, *md;
 1260         u_int msglen, outlen;
 1261         int error;
 1262 
 1263         if (buflen > MCLBYTES)
 1264                 return (EINVAL);
 1265 
 1266         in = malloc(buflen, M_TEMP, M_WAITOK);
 1267         error = copyin(buf, in, buflen);
 1268         if (error != 0)
 1269                 goto out;
 1270 
 1271         /*
 1272          * Make a pass over the input buffer to determine the amount of space
 1273          * required for 64 bit-aligned copies of the control messages.
 1274          */
 1275         in1 = in;
 1276         outlen = 0;
 1277         while (buflen > 0) {
 1278                 if (buflen < sizeof(*cm)) {
 1279                         error = EINVAL;
 1280                         break;
 1281                 }
 1282                 cm = (struct cmsghdr *)in1;
 1283                 if (cm->cmsg_len < FREEBSD32_ALIGN(sizeof(*cm))) {
 1284                         error = EINVAL;
 1285                         break;
 1286                 }
 1287                 msglen = FREEBSD32_ALIGN(cm->cmsg_len);
 1288                 if (msglen > buflen || msglen < cm->cmsg_len) {
 1289                         error = EINVAL;
 1290                         break;
 1291                 }
 1292                 buflen -= msglen;
 1293 
 1294                 in1 = (char *)in1 + msglen;
 1295                 outlen += CMSG_ALIGN(sizeof(*cm)) +
 1296                     CMSG_ALIGN(msglen - FREEBSD32_ALIGN(sizeof(*cm)));
 1297         }
 1298         if (error == 0 && outlen > MCLBYTES) {
 1299                 /*
 1300                  * XXXMJ This implies that the upper limit on 32-bit aligned
 1301                  * control messages is less than MCLBYTES, and so we are not
 1302                  * perfectly compatible.  However, there is no platform
 1303                  * guarantee that mbuf clusters larger than MCLBYTES can be
 1304                  * allocated.
 1305                  */
 1306                 error = EINVAL;
 1307         }
 1308         if (error != 0)
 1309                 goto out;
 1310 
 1311         m = m_get2(outlen, M_WAITOK, MT_CONTROL, 0);
 1312         m->m_len = outlen;
 1313         md = mtod(m, void *);
 1314 
 1315         /*
 1316          * Make a second pass over input messages, copying them into the output
 1317          * buffer.
 1318          */
 1319         in1 = in;
 1320         while (outlen > 0) {
 1321                 /* Copy the message header and align the length field. */
 1322                 cm = md;
 1323                 memcpy(cm, in1, sizeof(*cm));
 1324                 msglen = cm->cmsg_len - FREEBSD32_ALIGN(sizeof(*cm));
 1325                 cm->cmsg_len = CMSG_ALIGN(sizeof(*cm)) + msglen;
 1326 
 1327                 /* Copy the message body. */
 1328                 in1 = (char *)in1 + FREEBSD32_ALIGN(sizeof(*cm));
 1329                 md = (char *)md + CMSG_ALIGN(sizeof(*cm));
 1330                 memcpy(md, in1, msglen);
 1331                 in1 = (char *)in1 + FREEBSD32_ALIGN(msglen);
 1332                 md = (char *)md + CMSG_ALIGN(msglen);
 1333                 KASSERT(outlen >= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen),
 1334                     ("outlen %u underflow, msglen %u", outlen, msglen));
 1335                 outlen -= CMSG_ALIGN(sizeof(*cm)) + CMSG_ALIGN(msglen);
 1336         }
 1337 
 1338         *mp = m;
 1339 out:
 1340         free(in, M_TEMP);
 1341         return (error);
 1342 }
 1343 
 1344 int
 1345 freebsd32_sendmsg(struct thread *td, struct freebsd32_sendmsg_args *uap)
 1346 {
 1347         struct msghdr msg;
 1348         struct iovec *iov;
 1349         struct mbuf *control = NULL;
 1350         struct sockaddr *to = NULL;
 1351         int error;
 1352 
 1353         error = freebsd32_copyinmsghdr(uap->msg, &msg);
 1354         if (error)
 1355                 return (error);
 1356         error = freebsd32_copyiniov((void *)msg.msg_iov, msg.msg_iovlen, &iov,
 1357             EMSGSIZE);
 1358         if (error)
 1359                 return (error);
 1360         msg.msg_iov = iov;
 1361         if (msg.msg_name != NULL) {
 1362                 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
 1363                 if (error) {
 1364                         to = NULL;
 1365                         goto out;
 1366                 }
 1367                 msg.msg_name = to;
 1368         }
 1369 
 1370         if (msg.msg_control) {
 1371                 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
 1372                         error = EINVAL;
 1373                         goto out;
 1374                 }
 1375 
 1376                 error = freebsd32_copyin_control(&control, msg.msg_control,
 1377                     msg.msg_controllen);
 1378                 if (error)
 1379                         goto out;
 1380 
 1381                 msg.msg_control = NULL;
 1382                 msg.msg_controllen = 0;
 1383         }
 1384 
 1385         error = kern_sendit(td, uap->s, &msg, uap->flags, control,
 1386             UIO_USERSPACE);
 1387 
 1388 out:
 1389         free(iov, M_IOV);
 1390         if (to)
 1391                 free(to, M_SONAME);
 1392         return (error);
 1393 }
 1394 
 1395 int
 1396 freebsd32_recvfrom(struct thread *td,
 1397                    struct freebsd32_recvfrom_args *uap)
 1398 {
 1399         struct msghdr msg;
 1400         struct iovec aiov;
 1401         int error;
 1402 
 1403         if (uap->fromlenaddr) {
 1404                 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
 1405                     sizeof(msg.msg_namelen));
 1406                 if (error)
 1407                         return (error);
 1408         } else {
 1409                 msg.msg_namelen = 0;
 1410         }
 1411 
 1412         msg.msg_name = PTRIN(uap->from);
 1413         msg.msg_iov = &aiov;
 1414         msg.msg_iovlen = 1;
 1415         aiov.iov_base = PTRIN(uap->buf);
 1416         aiov.iov_len = uap->len;
 1417         msg.msg_control = NULL;
 1418         msg.msg_flags = uap->flags;
 1419         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
 1420         if (error == 0 && uap->fromlenaddr)
 1421                 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
 1422                     sizeof (msg.msg_namelen));
 1423         return (error);
 1424 }
 1425 
 1426 int
 1427 freebsd32_settimeofday(struct thread *td,
 1428                        struct freebsd32_settimeofday_args *uap)
 1429 {
 1430         struct timeval32 tv32;
 1431         struct timeval tv, *tvp;
 1432         struct timezone tz, *tzp;
 1433         int error;
 1434 
 1435         if (uap->tv) {
 1436                 error = copyin(uap->tv, &tv32, sizeof(tv32));
 1437                 if (error)
 1438                         return (error);
 1439                 CP(tv32, tv, tv_sec);
 1440                 CP(tv32, tv, tv_usec);
 1441                 tvp = &tv;
 1442         } else
 1443                 tvp = NULL;
 1444         if (uap->tzp) {
 1445                 error = copyin(uap->tzp, &tz, sizeof(tz));
 1446                 if (error)
 1447                         return (error);
 1448                 tzp = &tz;
 1449         } else
 1450                 tzp = NULL;
 1451         return (kern_settimeofday(td, tvp, tzp));
 1452 }
 1453 
 1454 int
 1455 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
 1456 {
 1457         struct timeval32 s32[2];
 1458         struct timeval s[2], *sp;
 1459         int error;
 1460 
 1461         if (uap->tptr != NULL) {
 1462                 error = copyin(uap->tptr, s32, sizeof(s32));
 1463                 if (error)
 1464                         return (error);
 1465                 CP(s32[0], s[0], tv_sec);
 1466                 CP(s32[0], s[0], tv_usec);
 1467                 CP(s32[1], s[1], tv_sec);
 1468                 CP(s32[1], s[1], tv_usec);
 1469                 sp = s;
 1470         } else
 1471                 sp = NULL;
 1472         return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
 1473             sp, UIO_SYSSPACE));
 1474 }
 1475 
 1476 int
 1477 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
 1478 {
 1479         struct timeval32 s32[2];
 1480         struct timeval s[2], *sp;
 1481         int error;
 1482 
 1483         if (uap->tptr != NULL) {
 1484                 error = copyin(uap->tptr, s32, sizeof(s32));
 1485                 if (error)
 1486                         return (error);
 1487                 CP(s32[0], s[0], tv_sec);
 1488                 CP(s32[0], s[0], tv_usec);
 1489                 CP(s32[1], s[1], tv_sec);
 1490                 CP(s32[1], s[1], tv_usec);
 1491                 sp = s;
 1492         } else
 1493                 sp = NULL;
 1494         return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
 1495 }
 1496 
 1497 int
 1498 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
 1499 {
 1500         struct timeval32 s32[2];
 1501         struct timeval s[2], *sp;
 1502         int error;
 1503 
 1504         if (uap->tptr != NULL) {
 1505                 error = copyin(uap->tptr, s32, sizeof(s32));
 1506                 if (error)
 1507                         return (error);
 1508                 CP(s32[0], s[0], tv_sec);
 1509                 CP(s32[0], s[0], tv_usec);
 1510                 CP(s32[1], s[1], tv_sec);
 1511                 CP(s32[1], s[1], tv_usec);
 1512                 sp = s;
 1513         } else
 1514                 sp = NULL;
 1515         return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
 1516 }
 1517 
 1518 int
 1519 freebsd32_futimesat(struct thread *td, struct freebsd32_futimesat_args *uap)
 1520 {
 1521         struct timeval32 s32[2];
 1522         struct timeval s[2], *sp;
 1523         int error;
 1524 
 1525         if (uap->times != NULL) {
 1526                 error = copyin(uap->times, s32, sizeof(s32));
 1527                 if (error)
 1528                         return (error);
 1529                 CP(s32[0], s[0], tv_sec);
 1530                 CP(s32[0], s[0], tv_usec);
 1531                 CP(s32[1], s[1], tv_sec);
 1532                 CP(s32[1], s[1], tv_usec);
 1533                 sp = s;
 1534         } else
 1535                 sp = NULL;
 1536         return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
 1537                 sp, UIO_SYSSPACE));
 1538 }
 1539 
 1540 int
 1541 freebsd32_futimens(struct thread *td, struct freebsd32_futimens_args *uap)
 1542 {
 1543         struct timespec32 ts32[2];
 1544         struct timespec ts[2], *tsp;
 1545         int error;
 1546 
 1547         if (uap->times != NULL) {
 1548                 error = copyin(uap->times, ts32, sizeof(ts32));
 1549                 if (error)
 1550                         return (error);
 1551                 CP(ts32[0], ts[0], tv_sec);
 1552                 CP(ts32[0], ts[0], tv_nsec);
 1553                 CP(ts32[1], ts[1], tv_sec);
 1554                 CP(ts32[1], ts[1], tv_nsec);
 1555                 tsp = ts;
 1556         } else
 1557                 tsp = NULL;
 1558         return (kern_futimens(td, uap->fd, tsp, UIO_SYSSPACE));
 1559 }
 1560 
 1561 int
 1562 freebsd32_utimensat(struct thread *td, struct freebsd32_utimensat_args *uap)
 1563 {
 1564         struct timespec32 ts32[2];
 1565         struct timespec ts[2], *tsp;
 1566         int error;
 1567 
 1568         if (uap->times != NULL) {
 1569                 error = copyin(uap->times, ts32, sizeof(ts32));
 1570                 if (error)
 1571                         return (error);
 1572                 CP(ts32[0], ts[0], tv_sec);
 1573                 CP(ts32[0], ts[0], tv_nsec);
 1574                 CP(ts32[1], ts[1], tv_sec);
 1575                 CP(ts32[1], ts[1], tv_nsec);
 1576                 tsp = ts;
 1577         } else
 1578                 tsp = NULL;
 1579         return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
 1580             tsp, UIO_SYSSPACE, uap->flag));
 1581 }
 1582 
 1583 int
 1584 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
 1585 {
 1586         struct timeval32 tv32;
 1587         struct timeval delta, olddelta, *deltap;
 1588         int error;
 1589 
 1590         if (uap->delta) {
 1591                 error = copyin(uap->delta, &tv32, sizeof(tv32));
 1592                 if (error)
 1593                         return (error);
 1594                 CP(tv32, delta, tv_sec);
 1595                 CP(tv32, delta, tv_usec);
 1596                 deltap = &delta;
 1597         } else
 1598                 deltap = NULL;
 1599         error = kern_adjtime(td, deltap, &olddelta);
 1600         if (uap->olddelta && error == 0) {
 1601                 CP(olddelta, tv32, tv_sec);
 1602                 CP(olddelta, tv32, tv_usec);
 1603                 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
 1604         }
 1605         return (error);
 1606 }
 1607 
 1608 #ifdef COMPAT_FREEBSD4
 1609 int
 1610 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
 1611 {
 1612         struct statfs32 s32;
 1613         struct statfs *sp;
 1614         int error;
 1615 
 1616         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
 1617         error = kern_statfs(td, uap->path, UIO_USERSPACE, sp);
 1618         if (error == 0) {
 1619                 copy_statfs(sp, &s32);
 1620                 error = copyout(&s32, uap->buf, sizeof(s32));
 1621         }
 1622         free(sp, M_STATFS);
 1623         return (error);
 1624 }
 1625 #endif
 1626 
 1627 #ifdef COMPAT_FREEBSD4
 1628 int
 1629 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
 1630 {
 1631         struct statfs32 s32;
 1632         struct statfs *sp;
 1633         int error;
 1634 
 1635         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
 1636         error = kern_fstatfs(td, uap->fd, sp);
 1637         if (error == 0) {
 1638                 copy_statfs(sp, &s32);
 1639                 error = copyout(&s32, uap->buf, sizeof(s32));
 1640         }
 1641         free(sp, M_STATFS);
 1642         return (error);
 1643 }
 1644 #endif
 1645 
 1646 #ifdef COMPAT_FREEBSD4
 1647 int
 1648 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
 1649 {
 1650         struct statfs32 s32;
 1651         struct statfs *sp;
 1652         fhandle_t fh;
 1653         int error;
 1654 
 1655         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
 1656                 return (error);
 1657         sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
 1658         error = kern_fhstatfs(td, fh, sp);
 1659         if (error == 0) {
 1660                 copy_statfs(sp, &s32);
 1661                 error = copyout(&s32, uap->buf, sizeof(s32));
 1662         }
 1663         free(sp, M_STATFS);
 1664         return (error);
 1665 }
 1666 #endif
 1667 
 1668 int
 1669 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
 1670 {
 1671 
 1672         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
 1673             PAIR32TO64(off_t, uap->offset)));
 1674 }
 1675 
 1676 int
 1677 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
 1678 {
 1679 
 1680         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
 1681             PAIR32TO64(off_t, uap->offset)));
 1682 }
 1683 
 1684 #ifdef COMPAT_43
 1685 int
 1686 ofreebsd32_lseek(struct thread *td, struct ofreebsd32_lseek_args *uap)
 1687 {
 1688 
 1689         return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
 1690 }
 1691 #endif
 1692 
 1693 int
 1694 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
 1695 {
 1696         int error;
 1697         off_t pos;
 1698 
 1699         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
 1700             uap->whence);
 1701         /* Expand the quad return into two parts for eax and edx */
 1702         pos = td->td_uretoff.tdu_off;
 1703         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
 1704         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
 1705         return error;
 1706 }
 1707 
 1708 int
 1709 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
 1710 {
 1711 
 1712         return (kern_truncate(td, uap->path, UIO_USERSPACE,
 1713             PAIR32TO64(off_t, uap->length)));
 1714 }
 1715 
 1716 int
 1717 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
 1718 {
 1719 
 1720         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
 1721 }
 1722 
 1723 #ifdef COMPAT_43
 1724 int
 1725 ofreebsd32_getdirentries(struct thread *td,
 1726     struct ofreebsd32_getdirentries_args *uap)
 1727 {
 1728         struct ogetdirentries_args ap;
 1729         int error;
 1730         long loff;
 1731         int32_t loff_cut;
 1732 
 1733         ap.fd = uap->fd;
 1734         ap.buf = uap->buf;
 1735         ap.count = uap->count;
 1736         ap.basep = NULL;
 1737         error = kern_ogetdirentries(td, &ap, &loff);
 1738         if (error == 0) {
 1739                 loff_cut = loff;
 1740                 error = copyout(&loff_cut, uap->basep, sizeof(int32_t));
 1741         }
 1742         return (error);
 1743 }
 1744 #endif
 1745 
 1746 #if defined(COMPAT_FREEBSD11)
 1747 int
 1748 freebsd11_freebsd32_getdirentries(struct thread *td,
 1749     struct freebsd11_freebsd32_getdirentries_args *uap)
 1750 {
 1751         long base;
 1752         int32_t base32;
 1753         int error;
 1754 
 1755         error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
 1756             &base, NULL);
 1757         if (error)
 1758                 return (error);
 1759         if (uap->basep != NULL) {
 1760                 base32 = base;
 1761                 error = copyout(&base32, uap->basep, sizeof(int32_t));
 1762         }
 1763         return (error);
 1764 }
 1765 
 1766 int
 1767 freebsd11_freebsd32_getdents(struct thread *td,
 1768     struct freebsd11_freebsd32_getdents_args *uap)
 1769 {
 1770         struct freebsd11_freebsd32_getdirentries_args ap;
 1771 
 1772         ap.fd = uap->fd;
 1773         ap.buf = uap->buf;
 1774         ap.count = uap->count;
 1775         ap.basep = NULL;
 1776         return (freebsd11_freebsd32_getdirentries(td, &ap));
 1777 }
 1778 #endif /* COMPAT_FREEBSD11 */
 1779 
 1780 #ifdef COMPAT_FREEBSD6
 1781 /* versions with the 'int pad' argument */
 1782 int
 1783 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
 1784 {
 1785 
 1786         return (kern_pread(td, uap->fd, uap->buf, uap->nbyte,
 1787             PAIR32TO64(off_t, uap->offset)));
 1788 }
 1789 
 1790 int
 1791 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
 1792 {
 1793 
 1794         return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte,
 1795             PAIR32TO64(off_t, uap->offset)));
 1796 }
 1797 
 1798 int
 1799 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
 1800 {
 1801         int error;
 1802         off_t pos;
 1803 
 1804         error = kern_lseek(td, uap->fd, PAIR32TO64(off_t, uap->offset),
 1805             uap->whence);
 1806         /* Expand the quad return into two parts for eax and edx */
 1807         pos = *(off_t *)(td->td_retval);
 1808         td->td_retval[RETVAL_LO] = pos & 0xffffffff;    /* %eax */
 1809         td->td_retval[RETVAL_HI] = pos >> 32;           /* %edx */
 1810         return error;
 1811 }
 1812 
 1813 int
 1814 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
 1815 {
 1816 
 1817         return (kern_truncate(td, uap->path, UIO_USERSPACE,
 1818             PAIR32TO64(off_t, uap->length)));
 1819 }
 1820 
 1821 int
 1822 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
 1823 {
 1824 
 1825         return (kern_ftruncate(td, uap->fd, PAIR32TO64(off_t, uap->length)));
 1826 }
 1827 #endif /* COMPAT_FREEBSD6 */
 1828 
 1829 struct sf_hdtr32 {
 1830         uint32_t headers;
 1831         int hdr_cnt;
 1832         uint32_t trailers;
 1833         int trl_cnt;
 1834 };
 1835 
 1836 static int
 1837 freebsd32_do_sendfile(struct thread *td,
 1838     struct freebsd32_sendfile_args *uap, int compat)
 1839 {
 1840         struct sf_hdtr32 hdtr32;
 1841         struct sf_hdtr hdtr;
 1842         struct uio *hdr_uio, *trl_uio;
 1843         struct file *fp;
 1844         cap_rights_t rights;
 1845         struct iovec32 *iov32;
 1846         off_t offset, sbytes;
 1847         int error;
 1848 
 1849         offset = PAIR32TO64(off_t, uap->offset);
 1850         if (offset < 0)
 1851                 return (EINVAL);
 1852 
 1853         hdr_uio = trl_uio = NULL;
 1854 
 1855         if (uap->hdtr != NULL) {
 1856                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
 1857                 if (error)
 1858                         goto out;
 1859                 PTRIN_CP(hdtr32, hdtr, headers);
 1860                 CP(hdtr32, hdtr, hdr_cnt);
 1861                 PTRIN_CP(hdtr32, hdtr, trailers);
 1862                 CP(hdtr32, hdtr, trl_cnt);
 1863 
 1864                 if (hdtr.headers != NULL) {
 1865                         iov32 = PTRIN(hdtr32.headers);
 1866                         error = freebsd32_copyinuio(iov32,
 1867                             hdtr32.hdr_cnt, &hdr_uio);
 1868                         if (error)
 1869                                 goto out;
 1870 #ifdef COMPAT_FREEBSD4
 1871                         /*
 1872                          * In FreeBSD < 5.0 the nbytes to send also included
 1873                          * the header.  If compat is specified subtract the
 1874                          * header size from nbytes.
 1875                          */
 1876                         if (compat) {
 1877                                 if (uap->nbytes > hdr_uio->uio_resid)
 1878                                         uap->nbytes -= hdr_uio->uio_resid;
 1879                                 else
 1880                                         uap->nbytes = 0;
 1881                         }
 1882 #endif
 1883                 }
 1884                 if (hdtr.trailers != NULL) {
 1885                         iov32 = PTRIN(hdtr32.trailers);
 1886                         error = freebsd32_copyinuio(iov32,
 1887                             hdtr32.trl_cnt, &trl_uio);
 1888                         if (error)
 1889                                 goto out;
 1890                 }
 1891         }
 1892 
 1893         AUDIT_ARG_FD(uap->fd);
 1894 
 1895         if ((error = fget_read(td, uap->fd,
 1896             cap_rights_init(&rights, CAP_PREAD), &fp)) != 0)
 1897                 goto out;
 1898 
 1899         error = fo_sendfile(fp, uap->s, hdr_uio, trl_uio, offset,
 1900             uap->nbytes, &sbytes, uap->flags, td);
 1901         fdrop(fp, td);
 1902 
 1903         if (uap->sbytes != NULL)
 1904                 copyout(&sbytes, uap->sbytes, sizeof(off_t));
 1905 
 1906 out:
 1907         if (hdr_uio)
 1908                 free(hdr_uio, M_IOV);
 1909         if (trl_uio)
 1910                 free(trl_uio, M_IOV);
 1911         return (error);
 1912 }
 1913 
 1914 #ifdef COMPAT_FREEBSD4
 1915 int
 1916 freebsd4_freebsd32_sendfile(struct thread *td,
 1917     struct freebsd4_freebsd32_sendfile_args *uap)
 1918 {
 1919         return (freebsd32_do_sendfile(td,
 1920             (struct freebsd32_sendfile_args *)uap, 1));
 1921 }
 1922 #endif
 1923 
 1924 int
 1925 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
 1926 {
 1927 
 1928         return (freebsd32_do_sendfile(td, uap, 0));
 1929 }
 1930 
 1931 static void
 1932 copy_stat(struct stat *in, struct stat32 *out)
 1933 {
 1934 
 1935         CP(*in, *out, st_dev);
 1936         CP(*in, *out, st_ino);
 1937         CP(*in, *out, st_mode);
 1938         CP(*in, *out, st_nlink);
 1939         CP(*in, *out, st_uid);
 1940         CP(*in, *out, st_gid);
 1941         CP(*in, *out, st_rdev);
 1942         TS_CP(*in, *out, st_atim);
 1943         TS_CP(*in, *out, st_mtim);
 1944         TS_CP(*in, *out, st_ctim);
 1945         CP(*in, *out, st_size);
 1946         CP(*in, *out, st_blocks);
 1947         CP(*in, *out, st_blksize);
 1948         CP(*in, *out, st_flags);
 1949         CP(*in, *out, st_gen);
 1950         TS_CP(*in, *out, st_birthtim);
 1951         out->st_padding0 = 0;
 1952         out->st_padding1 = 0;
 1953 #ifdef __STAT32_TIME_T_EXT
 1954         out->st_atim_ext = 0;
 1955         out->st_mtim_ext = 0;
 1956         out->st_ctim_ext = 0;
 1957         out->st_btim_ext = 0;
 1958 #endif
 1959         bzero(out->st_spare, sizeof(out->st_spare));
 1960 }
 1961 
 1962 #ifdef COMPAT_43
 1963 static void
 1964 copy_ostat(struct stat *in, struct ostat32 *out)
 1965 {
 1966 
 1967         bzero(out, sizeof(*out));
 1968         CP(*in, *out, st_dev);
 1969         CP(*in, *out, st_ino);
 1970         CP(*in, *out, st_mode);
 1971         CP(*in, *out, st_nlink);
 1972         CP(*in, *out, st_uid);
 1973         CP(*in, *out, st_gid);
 1974         CP(*in, *out, st_rdev);
 1975         out->st_size = MIN(in->st_size, INT32_MAX);
 1976         TS_CP(*in, *out, st_atim);
 1977         TS_CP(*in, *out, st_mtim);
 1978         TS_CP(*in, *out, st_ctim);
 1979         CP(*in, *out, st_blksize);
 1980         CP(*in, *out, st_blocks);
 1981         CP(*in, *out, st_flags);
 1982         CP(*in, *out, st_gen);
 1983 }
 1984 #endif
 1985 
 1986 #ifdef COMPAT_43
 1987 int
 1988 ofreebsd32_stat(struct thread *td, struct ofreebsd32_stat_args *uap)
 1989 {
 1990         struct stat sb;
 1991         struct ostat32 sb32;
 1992         int error;
 1993 
 1994         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
 1995             &sb, NULL);
 1996         if (error)
 1997                 return (error);
 1998         copy_ostat(&sb, &sb32);
 1999         error = copyout(&sb32, uap->ub, sizeof (sb32));
 2000         return (error);
 2001 }
 2002 #endif
 2003 
 2004 int
 2005 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
 2006 {
 2007         struct stat ub;
 2008         struct stat32 ub32;
 2009         int error;
 2010 
 2011         error = kern_fstat(td, uap->fd, &ub);
 2012         if (error)
 2013                 return (error);
 2014         copy_stat(&ub, &ub32);
 2015         error = copyout(&ub32, uap->ub, sizeof(ub32));
 2016         return (error);
 2017 }
 2018 
 2019 #ifdef COMPAT_43
 2020 int
 2021 ofreebsd32_fstat(struct thread *td, struct ofreebsd32_fstat_args *uap)
 2022 {
 2023         struct stat ub;
 2024         struct ostat32 ub32;
 2025         int error;
 2026 
 2027         error = kern_fstat(td, uap->fd, &ub);
 2028         if (error)
 2029                 return (error);
 2030         copy_ostat(&ub, &ub32);
 2031         error = copyout(&ub32, uap->ub, sizeof(ub32));
 2032         return (error);
 2033 }
 2034 #endif
 2035 
 2036 int
 2037 freebsd32_fstatat(struct thread *td, struct freebsd32_fstatat_args *uap)
 2038 {
 2039         struct stat ub;
 2040         struct stat32 ub32;
 2041         int error;
 2042 
 2043         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
 2044             &ub, NULL);
 2045         if (error)
 2046                 return (error);
 2047         copy_stat(&ub, &ub32);
 2048         error = copyout(&ub32, uap->buf, sizeof(ub32));
 2049         return (error);
 2050 }
 2051 
 2052 #ifdef COMPAT_43
 2053 int
 2054 ofreebsd32_lstat(struct thread *td, struct ofreebsd32_lstat_args *uap)
 2055 {
 2056         struct stat sb;
 2057         struct ostat32 sb32;
 2058         int error;
 2059 
 2060         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
 2061             UIO_USERSPACE, &sb, NULL);
 2062         if (error)
 2063                 return (error);
 2064         copy_ostat(&sb, &sb32);
 2065         error = copyout(&sb32, uap->ub, sizeof (sb32));
 2066         return (error);
 2067 }
 2068 #endif
 2069 
 2070 int
 2071 freebsd32_fhstat(struct thread *td, struct freebsd32_fhstat_args *uap)
 2072 {
 2073         struct stat sb;
 2074         struct stat32 sb32;
 2075         struct fhandle fh;
 2076         int error;
 2077 
 2078         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
 2079         if (error != 0)
 2080                 return (error);
 2081         error = kern_fhstat(td, fh, &sb);
 2082         if (error != 0)
 2083                 return (error);
 2084         copy_stat(&sb, &sb32);
 2085         error = copyout(&sb32, uap->sb, sizeof (sb32));
 2086         return (error);
 2087 }
 2088 
 2089 #if defined(COMPAT_FREEBSD11)
 2090 extern int ino64_trunc_error;
 2091 
 2092 static int
 2093 freebsd11_cvtstat32(struct stat *in, struct freebsd11_stat32 *out)
 2094 {
 2095 
 2096         CP(*in, *out, st_ino);
 2097         if (in->st_ino != out->st_ino) {
 2098                 switch (ino64_trunc_error) {
 2099                 default:
 2100                 case 0:
 2101                         break;
 2102                 case 1:
 2103                         return (EOVERFLOW);
 2104                 case 2:
 2105                         out->st_ino = UINT32_MAX;
 2106                         break;
 2107                 }
 2108         }
 2109         CP(*in, *out, st_nlink);
 2110         if (in->st_nlink != out->st_nlink) {
 2111                 switch (ino64_trunc_error) {
 2112                 default:
 2113                 case 0:
 2114                         break;
 2115                 case 1:
 2116                         return (EOVERFLOW);
 2117                 case 2:
 2118                         out->st_nlink = UINT16_MAX;
 2119                         break;
 2120                 }
 2121         }
 2122         out->st_dev = in->st_dev;
 2123         if (out->st_dev != in->st_dev) {
 2124                 switch (ino64_trunc_error) {
 2125                 default:
 2126                         break;
 2127                 case 1:
 2128                         return (EOVERFLOW);
 2129                 }
 2130         }
 2131         CP(*in, *out, st_mode);
 2132         CP(*in, *out, st_uid);
 2133         CP(*in, *out, st_gid);
 2134         out->st_rdev = in->st_rdev;
 2135         if (out->st_rdev != in->st_rdev) {
 2136                 switch (ino64_trunc_error) {
 2137                 default:
 2138                         break;
 2139                 case 1:
 2140                         return (EOVERFLOW);
 2141                 }
 2142         }
 2143         TS_CP(*in, *out, st_atim);
 2144         TS_CP(*in, *out, st_mtim);
 2145         TS_CP(*in, *out, st_ctim);
 2146         CP(*in, *out, st_size);
 2147         CP(*in, *out, st_blocks);
 2148         CP(*in, *out, st_blksize);
 2149         CP(*in, *out, st_flags);
 2150         CP(*in, *out, st_gen);
 2151         TS_CP(*in, *out, st_birthtim);
 2152         out->st_lspare = 0;
 2153         bzero((char *)&out->st_birthtim + sizeof(out->st_birthtim),
 2154             sizeof(*out) - offsetof(struct freebsd11_stat32,
 2155             st_birthtim) - sizeof(out->st_birthtim));
 2156         return (0);
 2157 }
 2158 
 2159 int
 2160 freebsd11_freebsd32_stat(struct thread *td,
 2161     struct freebsd11_freebsd32_stat_args *uap)
 2162 {
 2163         struct stat sb;
 2164         struct freebsd11_stat32 sb32;
 2165         int error;
 2166 
 2167         error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE,
 2168             &sb, NULL);
 2169         if (error != 0)
 2170                 return (error);
 2171         error = freebsd11_cvtstat32(&sb, &sb32);
 2172         if (error == 0)
 2173                 error = copyout(&sb32, uap->ub, sizeof (sb32));
 2174         return (error);
 2175 }
 2176 
 2177 int
 2178 freebsd11_freebsd32_fstat(struct thread *td,
 2179     struct freebsd11_freebsd32_fstat_args *uap)
 2180 {
 2181         struct stat sb;
 2182         struct freebsd11_stat32 sb32;
 2183         int error;
 2184 
 2185         error = kern_fstat(td, uap->fd, &sb);
 2186         if (error != 0)
 2187                 return (error);
 2188         error = freebsd11_cvtstat32(&sb, &sb32);
 2189         if (error == 0)
 2190                 error = copyout(&sb32, uap->ub, sizeof (sb32));
 2191         return (error);
 2192 }
 2193 
 2194 int
 2195 freebsd11_freebsd32_fstatat(struct thread *td,
 2196     struct freebsd11_freebsd32_fstatat_args *uap)
 2197 {
 2198         struct stat sb;
 2199         struct freebsd11_stat32 sb32;
 2200         int error;
 2201 
 2202         error = kern_statat(td, uap->flag, uap->fd, uap->path, UIO_USERSPACE,
 2203             &sb, NULL);
 2204         if (error != 0)
 2205                 return (error);
 2206         error = freebsd11_cvtstat32(&sb, &sb32);
 2207         if (error == 0)
 2208                 error = copyout(&sb32, uap->buf, sizeof (sb32));
 2209         return (error);
 2210 }
 2211 
 2212 int
 2213 freebsd11_freebsd32_lstat(struct thread *td,
 2214     struct freebsd11_freebsd32_lstat_args *uap)
 2215 {
 2216         struct stat sb;
 2217         struct freebsd11_stat32 sb32;
 2218         int error;
 2219 
 2220         error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
 2221             UIO_USERSPACE, &sb, NULL);
 2222         if (error != 0)
 2223                 return (error);
 2224         error = freebsd11_cvtstat32(&sb, &sb32);
 2225         if (error == 0)
 2226                 error = copyout(&sb32, uap->ub, sizeof (sb32));
 2227         return (error);
 2228 }
 2229 
 2230 int
 2231 freebsd11_freebsd32_fhstat(struct thread *td,
 2232     struct freebsd11_freebsd32_fhstat_args *uap)
 2233 {
 2234         struct stat sb;
 2235         struct freebsd11_stat32 sb32;
 2236         struct fhandle fh;
 2237         int error;
 2238 
 2239         error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
 2240         if (error != 0)
 2241                 return (error);
 2242         error = kern_fhstat(td, fh, &sb);
 2243         if (error != 0)
 2244                 return (error);
 2245         error = freebsd11_cvtstat32(&sb, &sb32);
 2246         if (error == 0)
 2247                 error = copyout(&sb32, uap->sb, sizeof (sb32));
 2248         return (error);
 2249 }
 2250 #endif
 2251 
 2252 int
 2253 freebsd32___sysctl(struct thread *td, struct freebsd32___sysctl_args *uap)
 2254 {
 2255         int error, name[CTL_MAXNAME];
 2256         size_t j, oldlen;
 2257         uint32_t tmp;
 2258 
 2259         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
 2260                 return (EINVAL);
 2261         error = copyin(uap->name, name, uap->namelen * sizeof(int));
 2262         if (error)
 2263                 return (error);
 2264         if (uap->oldlenp) {
 2265                 error = fueword32(uap->oldlenp, &tmp);
 2266                 oldlen = tmp;
 2267         } else {
 2268                 oldlen = 0;
 2269         }
 2270         if (error != 0)
 2271                 return (EFAULT);
 2272         error = userland_sysctl(td, name, uap->namelen,
 2273                 uap->old, &oldlen, 1,
 2274                 uap->new, uap->newlen, &j, SCTL_MASK32);
 2275         if (error)
 2276                 return (error);
 2277         if (uap->oldlenp)
 2278                 suword32(uap->oldlenp, j);
 2279         return (0);
 2280 }
 2281 
 2282 int
 2283 freebsd32___sysctlbyname(struct thread *td,
 2284     struct freebsd32___sysctlbyname_args *uap)
 2285 {
 2286         size_t oldlen, rv;
 2287         int error;
 2288         uint32_t tmp;
 2289 
 2290         if (uap->oldlenp != NULL) {
 2291                 error = fueword32(uap->oldlenp, &tmp);
 2292                 oldlen = tmp;
 2293         } else {
 2294                 error = oldlen = 0;
 2295         }
 2296         if (error != 0)
 2297                 return (EFAULT);
 2298         error = kern___sysctlbyname(td, uap->name, uap->namelen, uap->old,
 2299             &oldlen, uap->new, uap->newlen, &rv, SCTL_MASK32, 1);
 2300         if (error != 0)
 2301                 return (error);
 2302         if (uap->oldlenp != NULL)
 2303                 error = suword32(uap->oldlenp, rv);
 2304 
 2305         return (error);
 2306 }
 2307 
 2308 int
 2309 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
 2310 {
 2311         uint32_t version;
 2312         int error;
 2313         struct jail j;
 2314 
 2315         error = copyin(uap->jail, &version, sizeof(uint32_t));
 2316         if (error)
 2317                 return (error);
 2318 
 2319         switch (version) {
 2320         case 0:
 2321         {
 2322                 /* FreeBSD single IPv4 jails. */
 2323                 struct jail32_v0 j32_v0;
 2324 
 2325                 bzero(&j, sizeof(struct jail));
 2326                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
 2327                 if (error)
 2328                         return (error);
 2329                 CP(j32_v0, j, version);
 2330                 PTRIN_CP(j32_v0, j, path);
 2331                 PTRIN_CP(j32_v0, j, hostname);
 2332                 j.ip4s = htonl(j32_v0.ip_number);       /* jail_v0 is host order */
 2333                 break;
 2334         }
 2335 
 2336         case 1:
 2337                 /*
 2338                  * Version 1 was used by multi-IPv4 jail implementations
 2339                  * that never made it into the official kernel.
 2340                  */
 2341                 return (EINVAL);
 2342 
 2343         case 2: /* JAIL_API_VERSION */
 2344         {
 2345                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
 2346                 struct jail32 j32;
 2347 
 2348                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
 2349                 if (error)
 2350                         return (error);
 2351                 CP(j32, j, version);
 2352                 PTRIN_CP(j32, j, path);
 2353                 PTRIN_CP(j32, j, hostname);
 2354                 PTRIN_CP(j32, j, jailname);
 2355                 CP(j32, j, ip4s);
 2356                 CP(j32, j, ip6s);
 2357                 PTRIN_CP(j32, j, ip4);
 2358                 PTRIN_CP(j32, j, ip6);
 2359                 break;
 2360         }
 2361 
 2362         default:
 2363                 /* Sci-Fi jails are not supported, sorry. */
 2364                 return (EINVAL);
 2365         }
 2366         return (kern_jail(td, &j));
 2367 }
 2368 
 2369 int
 2370 freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap)
 2371 {
 2372         struct uio *auio;
 2373         int error;
 2374 
 2375         /* Check that we have an even number of iovecs. */
 2376         if (uap->iovcnt & 1)
 2377                 return (EINVAL);
 2378 
 2379         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
 2380         if (error)
 2381                 return (error);
 2382         error = kern_jail_set(td, auio, uap->flags);
 2383         free(auio, M_IOV);
 2384         return (error);
 2385 }
 2386 
 2387 int
 2388 freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap)
 2389 {
 2390         struct iovec32 iov32;
 2391         struct uio *auio;
 2392         int error, i;
 2393 
 2394         /* Check that we have an even number of iovecs. */
 2395         if (uap->iovcnt & 1)
 2396                 return (EINVAL);
 2397 
 2398         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
 2399         if (error)
 2400                 return (error);
 2401         error = kern_jail_get(td, auio, uap->flags);
 2402         if (error == 0)
 2403                 for (i = 0; i < uap->iovcnt; i++) {
 2404                         PTROUT_CP(auio->uio_iov[i], iov32, iov_base);
 2405                         CP(auio->uio_iov[i], iov32, iov_len);
 2406                         error = copyout(&iov32, uap->iovp + i, sizeof(iov32));
 2407                         if (error != 0)
 2408                                 break;
 2409                 }
 2410         free(auio, M_IOV);
 2411         return (error);
 2412 }
 2413 
 2414 int
 2415 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
 2416 {
 2417         struct sigaction32 s32;
 2418         struct sigaction sa, osa, *sap;
 2419         int error;
 2420 
 2421         if (uap->act) {
 2422                 error = copyin(uap->act, &s32, sizeof(s32));
 2423                 if (error)
 2424                         return (error);
 2425                 sa.sa_handler = PTRIN(s32.sa_u);
 2426                 CP(s32, sa, sa_flags);
 2427                 CP(s32, sa, sa_mask);
 2428                 sap = &sa;
 2429         } else
 2430                 sap = NULL;
 2431         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
 2432         if (error == 0 && uap->oact != NULL) {
 2433                 s32.sa_u = PTROUT(osa.sa_handler);
 2434                 CP(osa, s32, sa_flags);
 2435                 CP(osa, s32, sa_mask);
 2436                 error = copyout(&s32, uap->oact, sizeof(s32));
 2437         }
 2438         return (error);
 2439 }
 2440 
 2441 #ifdef COMPAT_FREEBSD4
 2442 int
 2443 freebsd4_freebsd32_sigaction(struct thread *td,
 2444                              struct freebsd4_freebsd32_sigaction_args *uap)
 2445 {
 2446         struct sigaction32 s32;
 2447         struct sigaction sa, osa, *sap;
 2448         int error;
 2449 
 2450         if (uap->act) {
 2451                 error = copyin(uap->act, &s32, sizeof(s32));
 2452                 if (error)
 2453                         return (error);
 2454                 sa.sa_handler = PTRIN(s32.sa_u);
 2455                 CP(s32, sa, sa_flags);
 2456                 CP(s32, sa, sa_mask);
 2457                 sap = &sa;
 2458         } else
 2459                 sap = NULL;
 2460         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
 2461         if (error == 0 && uap->oact != NULL) {
 2462                 s32.sa_u = PTROUT(osa.sa_handler);
 2463                 CP(osa, s32, sa_flags);
 2464                 CP(osa, s32, sa_mask);
 2465                 error = copyout(&s32, uap->oact, sizeof(s32));
 2466         }
 2467         return (error);
 2468 }
 2469 #endif
 2470 
 2471 #ifdef COMPAT_43
 2472 struct osigaction32 {
 2473         u_int32_t       sa_u;
 2474         osigset_t       sa_mask;
 2475         int             sa_flags;
 2476 };
 2477 
 2478 #define ONSIG   32
 2479 
 2480 int
 2481 ofreebsd32_sigaction(struct thread *td,
 2482                              struct ofreebsd32_sigaction_args *uap)
 2483 {
 2484         struct osigaction32 s32;
 2485         struct sigaction sa, osa, *sap;
 2486         int error;
 2487 
 2488         if (uap->signum <= 0 || uap->signum >= ONSIG)
 2489                 return (EINVAL);
 2490 
 2491         if (uap->nsa) {
 2492                 error = copyin(uap->nsa, &s32, sizeof(s32));
 2493                 if (error)
 2494                         return (error);
 2495                 sa.sa_handler = PTRIN(s32.sa_u);
 2496                 CP(s32, sa, sa_flags);
 2497                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
 2498                 sap = &sa;
 2499         } else
 2500                 sap = NULL;
 2501         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
 2502         if (error == 0 && uap->osa != NULL) {
 2503                 s32.sa_u = PTROUT(osa.sa_handler);
 2504                 CP(osa, s32, sa_flags);
 2505                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
 2506                 error = copyout(&s32, uap->osa, sizeof(s32));
 2507         }
 2508         return (error);
 2509 }
 2510 
 2511 int
 2512 ofreebsd32_sigprocmask(struct thread *td,
 2513                                struct ofreebsd32_sigprocmask_args *uap)
 2514 {
 2515         sigset_t set, oset;
 2516         int error;
 2517 
 2518         OSIG2SIG(uap->mask, set);
 2519         error = kern_sigprocmask(td, uap->how, &set, &oset, SIGPROCMASK_OLD);
 2520         SIG2OSIG(oset, td->td_retval[0]);
 2521         return (error);
 2522 }
 2523 
 2524 int
 2525 ofreebsd32_sigpending(struct thread *td,
 2526                               struct ofreebsd32_sigpending_args *uap)
 2527 {
 2528         struct proc *p = td->td_proc;
 2529         sigset_t siglist;
 2530 
 2531         PROC_LOCK(p);
 2532         siglist = p->p_siglist;
 2533         SIGSETOR(siglist, td->td_siglist);
 2534         PROC_UNLOCK(p);
 2535         SIG2OSIG(siglist, td->td_retval[0]);
 2536         return (0);
 2537 }
 2538 
 2539 struct sigvec32 {
 2540         u_int32_t       sv_handler;
 2541         int             sv_mask;
 2542         int             sv_flags;
 2543 };
 2544 
 2545 int
 2546 ofreebsd32_sigvec(struct thread *td,
 2547                           struct ofreebsd32_sigvec_args *uap)
 2548 {
 2549         struct sigvec32 vec;
 2550         struct sigaction sa, osa, *sap;
 2551         int error;
 2552 
 2553         if (uap->signum <= 0 || uap->signum >= ONSIG)
 2554                 return (EINVAL);
 2555 
 2556         if (uap->nsv) {
 2557                 error = copyin(uap->nsv, &vec, sizeof(vec));
 2558                 if (error)
 2559                         return (error);
 2560                 sa.sa_handler = PTRIN(vec.sv_handler);
 2561                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
 2562                 sa.sa_flags = vec.sv_flags;
 2563                 sa.sa_flags ^= SA_RESTART;
 2564                 sap = &sa;
 2565         } else
 2566                 sap = NULL;
 2567         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
 2568         if (error == 0 && uap->osv != NULL) {
 2569                 vec.sv_handler = PTROUT(osa.sa_handler);
 2570                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
 2571                 vec.sv_flags = osa.sa_flags;
 2572                 vec.sv_flags &= ~SA_NOCLDWAIT;
 2573                 vec.sv_flags ^= SA_RESTART;
 2574                 error = copyout(&vec, uap->osv, sizeof(vec));
 2575         }
 2576         return (error);
 2577 }
 2578 
 2579 int
 2580 ofreebsd32_sigblock(struct thread *td,
 2581                             struct ofreebsd32_sigblock_args *uap)
 2582 {
 2583         sigset_t set, oset;
 2584 
 2585         OSIG2SIG(uap->mask, set);
 2586         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
 2587         SIG2OSIG(oset, td->td_retval[0]);
 2588         return (0);
 2589 }
 2590 
 2591 int
 2592 ofreebsd32_sigsetmask(struct thread *td,
 2593                               struct ofreebsd32_sigsetmask_args *uap)
 2594 {
 2595         sigset_t set, oset;
 2596 
 2597         OSIG2SIG(uap->mask, set);
 2598         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
 2599         SIG2OSIG(oset, td->td_retval[0]);
 2600         return (0);
 2601 }
 2602 
 2603 int
 2604 ofreebsd32_sigsuspend(struct thread *td,
 2605                               struct ofreebsd32_sigsuspend_args *uap)
 2606 {
 2607         sigset_t mask;
 2608 
 2609         OSIG2SIG(uap->mask, mask);
 2610         return (kern_sigsuspend(td, mask));
 2611 }
 2612 
 2613 struct sigstack32 {
 2614         u_int32_t       ss_sp;
 2615         int             ss_onstack;
 2616 };
 2617 
 2618 int
 2619 ofreebsd32_sigstack(struct thread *td,
 2620                             struct ofreebsd32_sigstack_args *uap)
 2621 {
 2622         struct sigstack32 s32;
 2623         struct sigstack nss, oss;
 2624         int error = 0, unss;
 2625 
 2626         if (uap->nss != NULL) {
 2627                 error = copyin(uap->nss, &s32, sizeof(s32));
 2628                 if (error)
 2629                         return (error);
 2630                 nss.ss_sp = PTRIN(s32.ss_sp);
 2631                 CP(s32, nss, ss_onstack);
 2632                 unss = 1;
 2633         } else {
 2634                 unss = 0;
 2635         }
 2636         oss.ss_sp = td->td_sigstk.ss_sp;
 2637         oss.ss_onstack = sigonstack(cpu_getstack(td));
 2638         if (unss) {
 2639                 td->td_sigstk.ss_sp = nss.ss_sp;
 2640                 td->td_sigstk.ss_size = 0;
 2641                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
 2642                 td->td_pflags |= TDP_ALTSTACK;
 2643         }
 2644         if (uap->oss != NULL) {
 2645                 s32.ss_sp = PTROUT(oss.ss_sp);
 2646                 CP(oss, s32, ss_onstack);
 2647                 error = copyout(&s32, uap->oss, sizeof(s32));
 2648         }
 2649         return (error);
 2650 }
 2651 #endif
 2652 
 2653 int
 2654 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
 2655 {
 2656 
 2657         return (freebsd32_user_clock_nanosleep(td, CLOCK_REALTIME,
 2658             TIMER_RELTIME, uap->rqtp, uap->rmtp));
 2659 }
 2660 
 2661 int
 2662 freebsd32_clock_nanosleep(struct thread *td,
 2663     struct freebsd32_clock_nanosleep_args *uap)
 2664 {
 2665         int error;
 2666 
 2667         error = freebsd32_user_clock_nanosleep(td, uap->clock_id, uap->flags,
 2668             uap->rqtp, uap->rmtp);
 2669         return (kern_posix_error(td, error));
 2670 }
 2671 
 2672 static int
 2673 freebsd32_user_clock_nanosleep(struct thread *td, clockid_t clock_id,
 2674     int flags, const struct timespec32 *ua_rqtp, struct timespec32 *ua_rmtp)
 2675 {
 2676         struct timespec32 rmt32, rqt32;
 2677         struct timespec rmt, rqt;
 2678         int error, error2;
 2679 
 2680         error = copyin(ua_rqtp, &rqt32, sizeof(rqt32));
 2681         if (error)
 2682                 return (error);
 2683 
 2684         CP(rqt32, rqt, tv_sec);
 2685         CP(rqt32, rqt, tv_nsec);
 2686 
 2687         error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
 2688         if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
 2689                 CP(rmt, rmt32, tv_sec);
 2690                 CP(rmt, rmt32, tv_nsec);
 2691 
 2692                 error2 = copyout(&rmt32, ua_rmtp, sizeof(rmt32));
 2693                 if (error2 != 0)
 2694                         error = error2;
 2695         }
 2696         return (error);
 2697 }
 2698 
 2699 int
 2700 freebsd32_clock_gettime(struct thread *td,
 2701                         struct freebsd32_clock_gettime_args *uap)
 2702 {
 2703         struct timespec ats;
 2704         struct timespec32 ats32;
 2705         int error;
 2706 
 2707         error = kern_clock_gettime(td, uap->clock_id, &ats);
 2708         if (error == 0) {
 2709                 CP(ats, ats32, tv_sec);
 2710                 CP(ats, ats32, tv_nsec);
 2711                 error = copyout(&ats32, uap->tp, sizeof(ats32));
 2712         }
 2713         return (error);
 2714 }
 2715 
 2716 int
 2717 freebsd32_clock_settime(struct thread *td,
 2718                         struct freebsd32_clock_settime_args *uap)
 2719 {
 2720         struct timespec ats;
 2721         struct timespec32 ats32;
 2722         int error;
 2723 
 2724         error = copyin(uap->tp, &ats32, sizeof(ats32));
 2725         if (error)
 2726                 return (error);
 2727         CP(ats32, ats, tv_sec);
 2728         CP(ats32, ats, tv_nsec);
 2729 
 2730         return (kern_clock_settime(td, uap->clock_id, &ats));
 2731 }
 2732 
 2733 int
 2734 freebsd32_clock_getres(struct thread *td,
 2735                        struct freebsd32_clock_getres_args *uap)
 2736 {
 2737         struct timespec ts;
 2738         struct timespec32 ts32;
 2739         int error;
 2740 
 2741         if (uap->tp == NULL)
 2742                 return (0);
 2743         error = kern_clock_getres(td, uap->clock_id, &ts);
 2744         if (error == 0) {
 2745                 CP(ts, ts32, tv_sec);
 2746                 CP(ts, ts32, tv_nsec);
 2747                 error = copyout(&ts32, uap->tp, sizeof(ts32));
 2748         }
 2749         return (error);
 2750 }
 2751 
 2752 int freebsd32_ktimer_create(struct thread *td,
 2753     struct freebsd32_ktimer_create_args *uap)
 2754 {
 2755         struct sigevent32 ev32;
 2756         struct sigevent ev, *evp;
 2757         int error, id;
 2758 
 2759         if (uap->evp == NULL) {
 2760                 evp = NULL;
 2761         } else {
 2762                 evp = &ev;
 2763                 error = copyin(uap->evp, &ev32, sizeof(ev32));
 2764                 if (error != 0)
 2765                         return (error);
 2766                 error = convert_sigevent32(&ev32, &ev);
 2767                 if (error != 0)
 2768                         return (error);
 2769         }
 2770         error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
 2771         if (error == 0) {
 2772                 error = copyout(&id, uap->timerid, sizeof(int));
 2773                 if (error != 0)
 2774                         kern_ktimer_delete(td, id);
 2775         }
 2776         return (error);
 2777 }
 2778 
 2779 int
 2780 freebsd32_ktimer_settime(struct thread *td,
 2781     struct freebsd32_ktimer_settime_args *uap)
 2782 {
 2783         struct itimerspec32 val32, oval32;
 2784         struct itimerspec val, oval, *ovalp;
 2785         int error;
 2786 
 2787         error = copyin(uap->value, &val32, sizeof(val32));
 2788         if (error != 0)
 2789                 return (error);
 2790         ITS_CP(val32, val);
 2791         ovalp = uap->ovalue != NULL ? &oval : NULL;
 2792         error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
 2793         if (error == 0 && uap->ovalue != NULL) {
 2794                 ITS_CP(oval, oval32);
 2795                 error = copyout(&oval32, uap->ovalue, sizeof(oval32));
 2796         }
 2797         return (error);
 2798 }
 2799 
 2800 int
 2801 freebsd32_ktimer_gettime(struct thread *td,
 2802     struct freebsd32_ktimer_gettime_args *uap)
 2803 {
 2804         struct itimerspec32 val32;
 2805         struct itimerspec val;
 2806         int error;
 2807 
 2808         error = kern_ktimer_gettime(td, uap->timerid, &val);
 2809         if (error == 0) {
 2810                 ITS_CP(val, val32);
 2811                 error = copyout(&val32, uap->value, sizeof(val32));
 2812         }
 2813         return (error);
 2814 }
 2815 
 2816 int
 2817 freebsd32_clock_getcpuclockid2(struct thread *td,
 2818     struct freebsd32_clock_getcpuclockid2_args *uap)
 2819 {
 2820         clockid_t clk_id;
 2821         int error;
 2822 
 2823         error = kern_clock_getcpuclockid2(td, PAIR32TO64(id_t, uap->id),
 2824             uap->which, &clk_id);
 2825         if (error == 0)
 2826                 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
 2827         return (error);
 2828 }
 2829 
 2830 int
 2831 freebsd32_thr_new(struct thread *td,
 2832                   struct freebsd32_thr_new_args *uap)
 2833 {
 2834         struct thr_param32 param32;
 2835         struct thr_param param;
 2836         int error;
 2837 
 2838         if (uap->param_size < 0 ||
 2839             uap->param_size > sizeof(struct thr_param32))
 2840                 return (EINVAL);
 2841         bzero(&param, sizeof(struct thr_param));
 2842         bzero(&param32, sizeof(struct thr_param32));
 2843         error = copyin(uap->param, &param32, uap->param_size);
 2844         if (error != 0)
 2845                 return (error);
 2846         param.start_func = PTRIN(param32.start_func);
 2847         param.arg = PTRIN(param32.arg);
 2848         param.stack_base = PTRIN(param32.stack_base);
 2849         param.stack_size = param32.stack_size;
 2850         param.tls_base = PTRIN(param32.tls_base);
 2851         param.tls_size = param32.tls_size;
 2852         param.child_tid = PTRIN(param32.child_tid);
 2853         param.parent_tid = PTRIN(param32.parent_tid);
 2854         param.flags = param32.flags;
 2855         param.rtp = PTRIN(param32.rtp);
 2856         param.spare[0] = PTRIN(param32.spare[0]);
 2857         param.spare[1] = PTRIN(param32.spare[1]);
 2858         param.spare[2] = PTRIN(param32.spare[2]);
 2859 
 2860         return (kern_thr_new(td, &param));
 2861 }
 2862 
 2863 int
 2864 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
 2865 {
 2866         struct timespec32 ts32;
 2867         struct timespec ts, *tsp;
 2868         int error;
 2869 
 2870         error = 0;
 2871         tsp = NULL;
 2872         if (uap->timeout != NULL) {
 2873                 error = copyin((const void *)uap->timeout, (void *)&ts32,
 2874                     sizeof(struct timespec32));
 2875                 if (error != 0)
 2876                         return (error);
 2877                 ts.tv_sec = ts32.tv_sec;
 2878                 ts.tv_nsec = ts32.tv_nsec;
 2879                 tsp = &ts;
 2880         }
 2881         return (kern_thr_suspend(td, tsp));
 2882 }
 2883 
 2884 void
 2885 siginfo_to_siginfo32(const siginfo_t *src, struct siginfo32 *dst)
 2886 {
 2887         bzero(dst, sizeof(*dst));
 2888         dst->si_signo = src->si_signo;
 2889         dst->si_errno = src->si_errno;
 2890         dst->si_code = src->si_code;
 2891         dst->si_pid = src->si_pid;
 2892         dst->si_uid = src->si_uid;
 2893         dst->si_status = src->si_status;
 2894         dst->si_addr = (uintptr_t)src->si_addr;
 2895         dst->si_value.sival_int = src->si_value.sival_int;
 2896         dst->si_timerid = src->si_timerid;
 2897         dst->si_overrun = src->si_overrun;
 2898 }
 2899 
 2900 #ifndef _FREEBSD32_SYSPROTO_H_
 2901 struct freebsd32_sigqueue_args {
 2902         pid_t pid;
 2903         int signum;
 2904         /* union sigval32 */ int value;
 2905 };
 2906 #endif
 2907 int
 2908 freebsd32_sigqueue(struct thread *td, struct freebsd32_sigqueue_args *uap)
 2909 {
 2910         union sigval sv;
 2911 
 2912         /*
 2913          * On 32-bit ABIs, sival_int and sival_ptr are the same.
 2914          * On 64-bit little-endian ABIs, the low bits are the same.
 2915          * In 64-bit big-endian ABIs, sival_int overlaps with
 2916          * sival_ptr's HIGH bits.  We choose to support sival_int
 2917          * rather than sival_ptr in this case as it seems to be
 2918          * more common.
 2919          */
 2920         bzero(&sv, sizeof(sv));
 2921         sv.sival_int = uap->value;
 2922 
 2923         return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
 2924 }
 2925 
 2926 int
 2927 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
 2928 {
 2929         struct timespec32 ts32;
 2930         struct timespec ts;
 2931         struct timespec *timeout;
 2932         sigset_t set;
 2933         ksiginfo_t ksi;
 2934         struct siginfo32 si32;
 2935         int error;
 2936 
 2937         if (uap->timeout) {
 2938                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
 2939                 if (error)
 2940                         return (error);
 2941                 ts.tv_sec = ts32.tv_sec;
 2942                 ts.tv_nsec = ts32.tv_nsec;
 2943                 timeout = &ts;
 2944         } else
 2945                 timeout = NULL;
 2946 
 2947         error = copyin(uap->set, &set, sizeof(set));
 2948         if (error)
 2949                 return (error);
 2950 
 2951         error = kern_sigtimedwait(td, set, &ksi, timeout);
 2952         if (error)
 2953                 return (error);
 2954 
 2955         if (uap->info) {
 2956                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
 2957                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
 2958         }
 2959 
 2960         if (error == 0)
 2961                 td->td_retval[0] = ksi.ksi_signo;
 2962         return (error);
 2963 }
 2964 
 2965 /*
 2966  * MPSAFE
 2967  */
 2968 int
 2969 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
 2970 {
 2971         ksiginfo_t ksi;
 2972         struct siginfo32 si32;
 2973         sigset_t set;
 2974         int error;
 2975 
 2976         error = copyin(uap->set, &set, sizeof(set));
 2977         if (error)
 2978                 return (error);
 2979 
 2980         error = kern_sigtimedwait(td, set, &ksi, NULL);
 2981         if (error)
 2982                 return (error);
 2983 
 2984         if (uap->info) {
 2985                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
 2986                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
 2987         }       
 2988         if (error == 0)
 2989                 td->td_retval[0] = ksi.ksi_signo;
 2990         return (error);
 2991 }
 2992 
 2993 int
 2994 freebsd32_cpuset_setid(struct thread *td,
 2995     struct freebsd32_cpuset_setid_args *uap)
 2996 {
 2997 
 2998         return (kern_cpuset_setid(td, uap->which,
 2999             PAIR32TO64(id_t, uap->id), uap->setid));
 3000 }
 3001 
 3002 int
 3003 freebsd32_cpuset_getid(struct thread *td,
 3004     struct freebsd32_cpuset_getid_args *uap)
 3005 {
 3006 
 3007         return (kern_cpuset_getid(td, uap->level, uap->which,
 3008             PAIR32TO64(id_t, uap->id), uap->setid));
 3009 }
 3010 
 3011 static int
 3012 copyin32_set(const void *u, void *k, size_t size)
 3013 {
 3014 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
 3015         int rv;
 3016         struct bitset *kb = k;
 3017         int *p;
 3018 
 3019         rv = copyin(u, k, size);
 3020         if (rv != 0)
 3021                 return (rv);
 3022 
 3023         p = (int *)kb->__bits;
 3024         /* Loop through swapping words.
 3025          * `size' is in bytes, we need bits. */
 3026         for (int i = 0; i < __bitset_words(size * 8); i++) {
 3027                 int tmp = p[0];
 3028                 p[0] = p[1];
 3029                 p[1] = tmp;
 3030                 p += 2;
 3031         }
 3032         return (0);
 3033 #else
 3034         return (copyin(u, k, size));
 3035 #endif
 3036 }
 3037 
 3038 static int
 3039 copyout32_set(const void *k, void *u, size_t size)
 3040 {
 3041 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
 3042         const struct bitset *kb = k;
 3043         struct bitset *ub = u;
 3044         const int *kp = (const int *)kb->__bits;
 3045         int *up = (int *)ub->__bits;
 3046         int rv;
 3047 
 3048         for (int i = 0; i < __bitset_words(CPU_SETSIZE); i++) {
 3049                 /* `size' is in bytes, we need bits. */
 3050                 for (int i = 0; i < __bitset_words(size * 8); i++) {
 3051                         rv = suword32(up, kp[1]);
 3052                         if (rv == 0)
 3053                                 rv = suword32(up + 1, kp[0]);
 3054                         if (rv != 0)
 3055                                 return (EFAULT);
 3056                 }
 3057         }
 3058         return (0);
 3059 #else
 3060         return (copyout(k, u, size));
 3061 #endif
 3062 }
 3063 
 3064 static const struct cpuset_copy_cb cpuset_copy32_cb = {
 3065         .cpuset_copyin = copyin32_set,
 3066         .cpuset_copyout = copyout32_set
 3067 };
 3068 
 3069 int
 3070 freebsd32_cpuset_getaffinity(struct thread *td,
 3071     struct freebsd32_cpuset_getaffinity_args *uap)
 3072 {
 3073 
 3074         return (kern_cpuset_getaffinity(td, uap->level, uap->which,
 3075             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask,
 3076             &cpuset_copy32_cb));
 3077 }
 3078 
 3079 int
 3080 freebsd32_cpuset_setaffinity(struct thread *td,
 3081     struct freebsd32_cpuset_setaffinity_args *uap)
 3082 {
 3083 
 3084         return (kern_cpuset_setaffinity(td, uap->level, uap->which,
 3085             PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask,
 3086             &cpuset_copy32_cb));
 3087 }
 3088 
 3089 int
 3090 freebsd32_cpuset_getdomain(struct thread *td,
 3091     struct freebsd32_cpuset_getdomain_args *uap)
 3092 {
 3093 
 3094         return (kern_cpuset_getdomain(td, uap->level, uap->which,
 3095             PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy,
 3096             &cpuset_copy32_cb));
 3097 }
 3098 
 3099 int
 3100 freebsd32_cpuset_setdomain(struct thread *td,
 3101     struct freebsd32_cpuset_setdomain_args *uap)
 3102 {
 3103 
 3104         return (kern_cpuset_setdomain(td, uap->level, uap->which,
 3105             PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy,
 3106             &cpuset_copy32_cb));
 3107 }
 3108 
 3109 int
 3110 freebsd32_nmount(struct thread *td,
 3111     struct freebsd32_nmount_args /* {
 3112         struct iovec *iovp;
 3113         unsigned int iovcnt;
 3114         int flags;
 3115     } */ *uap)
 3116 {
 3117         struct uio *auio;
 3118         uint64_t flags;
 3119         int error;
 3120 
 3121         /*
 3122          * Mount flags are now 64-bits. On 32-bit archtectures only
 3123          * 32-bits are passed in, but from here on everything handles
 3124          * 64-bit flags correctly.
 3125          */
 3126         flags = uap->flags;
 3127 
 3128         AUDIT_ARG_FFLAGS(flags);
 3129 
 3130         /*
 3131          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
 3132          * userspace to set this flag, but we must filter it out if we want
 3133          * MNT_UPDATE on the root file system to work.
 3134          * MNT_ROOTFS should only be set by the kernel when mounting its
 3135          * root file system.
 3136          */
 3137         flags &= ~MNT_ROOTFS;
 3138 
 3139         /*
 3140          * check that we have an even number of iovec's
 3141          * and that we have at least two options.
 3142          */
 3143         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
 3144                 return (EINVAL);
 3145 
 3146         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
 3147         if (error)
 3148                 return (error);
 3149         error = vfs_donmount(td, flags, auio);
 3150 
 3151         free(auio, M_IOV);
 3152         return error;
 3153 }
 3154 
 3155 #if 0
 3156 int
 3157 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
 3158 {
 3159         struct yyy32 *p32, s32;
 3160         struct yyy *p = NULL, s;
 3161         struct xxx_arg ap;
 3162         int error;
 3163 
 3164         if (uap->zzz) {
 3165                 error = copyin(uap->zzz, &s32, sizeof(s32));
 3166                 if (error)
 3167                         return (error);
 3168                 /* translate in */
 3169                 p = &s;
 3170         }
 3171         error = kern_xxx(td, p);
 3172         if (error)
 3173                 return (error);
 3174         if (uap->zzz) {
 3175                 /* translate out */
 3176                 error = copyout(&s32, p32, sizeof(s32));
 3177         }
 3178         return (error);
 3179 }
 3180 #endif
 3181 
 3182 int
 3183 syscall32_module_handler(struct module *mod, int what, void *arg)
 3184 {
 3185 
 3186         return (kern_syscall_module_handler(freebsd32_sysent, mod, what, arg));
 3187 }
 3188 
 3189 int
 3190 syscall32_helper_register(struct syscall_helper_data *sd, int flags)
 3191 {
 3192 
 3193         return (kern_syscall_helper_register(freebsd32_sysent, sd, flags));
 3194 }
 3195 
 3196 int
 3197 syscall32_helper_unregister(struct syscall_helper_data *sd)
 3198 {
 3199 
 3200         return (kern_syscall_helper_unregister(freebsd32_sysent, sd));
 3201 }
 3202 
 3203 register_t *
 3204 freebsd32_copyout_strings(struct image_params *imgp)
 3205 {
 3206         int argc, envc, i;
 3207         u_int32_t *vectp;
 3208         char *stringp;
 3209         uintptr_t destp;
 3210         u_int32_t *stack_base;
 3211         struct freebsd32_ps_strings *arginfo;
 3212         char canary[sizeof(long) * 8];
 3213         int32_t pagesizes32[MAXPAGESIZES];
 3214         size_t execpath_len;
 3215         int szsigcode;
 3216 
 3217         /*
 3218          * Calculate string base and vector table pointers.
 3219          * Also deal with signal trampoline code for this exec type.
 3220          */
 3221         if (imgp->execpath != NULL && imgp->auxargs != NULL)
 3222                 execpath_len = strlen(imgp->execpath) + 1;
 3223         else
 3224                 execpath_len = 0;
 3225         arginfo = (struct freebsd32_ps_strings *)curproc->p_sysent->
 3226             sv_psstrings;
 3227         if (imgp->proc->p_sysent->sv_sigcode_base == 0)
 3228                 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
 3229         else
 3230                 szsigcode = 0;
 3231         destp = (uintptr_t)arginfo;
 3232 
 3233         /*
 3234          * install sigcode
 3235          */
 3236         if (szsigcode != 0) {
 3237                 destp -= szsigcode;
 3238                 destp = rounddown2(destp, sizeof(uint32_t));
 3239                 copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
 3240                     szsigcode);
 3241         }
 3242 
 3243         /*
 3244          * Copy the image path for the rtld.
 3245          */
 3246         if (execpath_len != 0) {
 3247                 destp -= execpath_len;
 3248                 imgp->execpathp = destp;
 3249                 copyout(imgp->execpath, (void *)destp, execpath_len);
 3250         }
 3251 
 3252         /*
 3253          * Prepare the canary for SSP.
 3254          */
 3255         arc4rand(canary, sizeof(canary), 0);
 3256         destp -= sizeof(canary);
 3257         imgp->canary = destp;
 3258         copyout(canary, (void *)destp, sizeof(canary));
 3259         imgp->canarylen = sizeof(canary);
 3260 
 3261         /*
 3262          * Prepare the pagesizes array.
 3263          */
 3264         for (i = 0; i < MAXPAGESIZES; i++)
 3265                 pagesizes32[i] = (uint32_t)pagesizes[i];
 3266         destp -= sizeof(pagesizes32);
 3267         destp = rounddown2(destp, sizeof(uint32_t));
 3268         imgp->pagesizes = destp;
 3269         copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
 3270         imgp->pagesizeslen = sizeof(pagesizes32);
 3271 
 3272         destp -= ARG_MAX - imgp->args->stringspace;
 3273         destp = rounddown2(destp, sizeof(uint32_t));
 3274 
 3275         exec_stackgap(imgp, &destp);
 3276         vectp = (uint32_t *)destp;
 3277 
 3278         if (imgp->auxargs) {
 3279                 /*
 3280                  * Allocate room on the stack for the ELF auxargs
 3281                  * array.  It has up to AT_COUNT entries.
 3282                  */
 3283                 vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
 3284                     sizeof(*vectp));
 3285         }
 3286 
 3287         /*
 3288          * Allocate room for the argv[] and env vectors including the
 3289          * terminating NULL pointers.
 3290          */
 3291         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
 3292 
 3293         /*
 3294          * vectp also becomes our initial stack base
 3295          */
 3296         stack_base = vectp;
 3297 
 3298         stringp = imgp->args->begin_argv;
 3299         argc = imgp->args->argc;
 3300         envc = imgp->args->envc;
 3301         /*
 3302          * Copy out strings - arguments and environment.
 3303          */
 3304         copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
 3305 
 3306         /*
 3307          * Fill in "ps_strings" struct for ps, w, etc.
 3308          */
 3309         suword32(&arginfo->ps_argvstr, (u_int32_t)(intptr_t)vectp);
 3310         suword32(&arginfo->ps_nargvstr, argc);
 3311 
 3312         /*
 3313          * Fill in argument portion of vector table.
 3314          */
 3315         for (; argc > 0; --argc) {
 3316                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
 3317                 while (*stringp++ != 0)
 3318                         destp++;
 3319                 destp++;
 3320         }
 3321 
 3322         /* a null vector table pointer separates the argp's from the envp's */
 3323         suword32(vectp++, 0);
 3324 
 3325         suword32(&arginfo->ps_envstr, (u_int32_t)(intptr_t)vectp);
 3326         suword32(&arginfo->ps_nenvstr, envc);
 3327 
 3328         /*
 3329          * Fill in environment portion of vector table.
 3330          */
 3331         for (; envc > 0; --envc) {
 3332                 suword32(vectp++, (u_int32_t)(intptr_t)destp);
 3333                 while (*stringp++ != 0)
 3334                         destp++;
 3335                 destp++;
 3336         }
 3337 
 3338         /* end of vector table is a null pointer */
 3339         suword32(vectp, 0);
 3340 
 3341         return ((register_t *)stack_base);
 3342 }
 3343 
 3344 int
 3345 freebsd32_kldstat(struct thread *td, struct freebsd32_kldstat_args *uap)
 3346 {
 3347         struct kld_file_stat *stat;
 3348         struct kld32_file_stat *stat32;
 3349         int error, version;
 3350 
 3351         if ((error = copyin(&uap->stat->version, &version, sizeof(version)))
 3352             != 0)
 3353                 return (error);
 3354         if (version != sizeof(struct kld32_file_stat_1) &&
 3355             version != sizeof(struct kld32_file_stat))
 3356                 return (EINVAL);
 3357 
 3358         stat = malloc(sizeof(*stat), M_TEMP, M_WAITOK | M_ZERO);
 3359         stat32 = malloc(sizeof(*stat32), M_TEMP, M_WAITOK | M_ZERO);
 3360         error = kern_kldstat(td, uap->fileid, stat);
 3361         if (error == 0) {
 3362                 bcopy(&stat->name[0], &stat32->name[0], sizeof(stat->name));
 3363                 CP(*stat, *stat32, refs);
 3364                 CP(*stat, *stat32, id);
 3365                 PTROUT_CP(*stat, *stat32, address);
 3366                 CP(*stat, *stat32, size);
 3367                 bcopy(&stat->pathname[0], &stat32->pathname[0],
 3368                     sizeof(stat->pathname));
 3369                 stat32->version  = version;
 3370                 error = copyout(stat32, uap->stat, version);
 3371         }
 3372         free(stat, M_TEMP);
 3373         free(stat32, M_TEMP);
 3374         return (error);
 3375 }
 3376 
 3377 int
 3378 freebsd32_posix_fallocate(struct thread *td,
 3379     struct freebsd32_posix_fallocate_args *uap)
 3380 {
 3381         int error;
 3382 
 3383         error = kern_posix_fallocate(td, uap->fd,
 3384             PAIR32TO64(off_t, uap->offset), PAIR32TO64(off_t, uap->len));
 3385         return (kern_posix_error(td, error));
 3386 }
 3387 
 3388 int
 3389 freebsd32_posix_fadvise(struct thread *td,
 3390     struct freebsd32_posix_fadvise_args *uap)
 3391 {
 3392         int error;
 3393 
 3394         error = kern_posix_fadvise(td, uap->fd, PAIR32TO64(off_t, uap->offset),
 3395             PAIR32TO64(off_t, uap->len), uap->advice);
 3396         return (kern_posix_error(td, error));
 3397 }
 3398 
 3399 int
 3400 convert_sigevent32(struct sigevent32 *sig32, struct sigevent *sig)
 3401 {
 3402 
 3403         CP(*sig32, *sig, sigev_notify);
 3404         switch (sig->sigev_notify) {
 3405         case SIGEV_NONE:
 3406                 break;
 3407         case SIGEV_THREAD_ID:
 3408                 CP(*sig32, *sig, sigev_notify_thread_id);
 3409                 /* FALLTHROUGH */
 3410         case SIGEV_SIGNAL:
 3411                 CP(*sig32, *sig, sigev_signo);
 3412                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
 3413                 break;
 3414         case SIGEV_KEVENT:
 3415                 CP(*sig32, *sig, sigev_notify_kqueue);
 3416                 CP(*sig32, *sig, sigev_notify_kevent_flags);
 3417                 PTRIN_CP(*sig32, *sig, sigev_value.sival_ptr);
 3418                 break;
 3419         default:
 3420                 return (EINVAL);
 3421         }
 3422         return (0);
 3423 }
 3424 
 3425 int
 3426 freebsd32_procctl(struct thread *td, struct freebsd32_procctl_args *uap)
 3427 {
 3428         void *data;
 3429         union {
 3430                 struct procctl_reaper_status rs;
 3431                 struct procctl_reaper_pids rp;
 3432                 struct procctl_reaper_kill rk;
 3433         } x;
 3434         union {
 3435                 struct procctl_reaper_pids32 rp;
 3436         } x32;
 3437         int error, error1, flags, signum;
 3438 
 3439         if (uap->com >= PROC_PROCCTL_MD_MIN)
 3440                 return (cpu_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
 3441                     uap->com, PTRIN(uap->data)));
 3442 
 3443         switch (uap->com) {
 3444         case PROC_ASLR_CTL:
 3445         case PROC_SPROTECT:
 3446         case PROC_STACKGAP_CTL:
 3447         case PROC_TRACE_CTL:
 3448         case PROC_TRAPCAP_CTL:
 3449                 error = copyin(PTRIN(uap->data), &flags, sizeof(flags));
 3450                 if (error != 0)
 3451                         return (error);
 3452                 data = &flags;
 3453                 break;
 3454         case PROC_REAP_ACQUIRE:
 3455         case PROC_REAP_RELEASE:
 3456                 if (uap->data != NULL)
 3457                         return (EINVAL);
 3458                 data = NULL;
 3459                 break;
 3460         case PROC_REAP_STATUS:
 3461                 data = &x.rs;
 3462                 break;
 3463         case PROC_REAP_GETPIDS:
 3464                 error = copyin(uap->data, &x32.rp, sizeof(x32.rp));
 3465                 if (error != 0)
 3466                         return (error);
 3467                 CP(x32.rp, x.rp, rp_count);
 3468                 PTRIN_CP(x32.rp, x.rp, rp_pids);
 3469                 data = &x.rp;
 3470                 break;
 3471         case PROC_REAP_KILL:
 3472                 error = copyin(uap->data, &x.rk, sizeof(x.rk));
 3473                 if (error != 0)
 3474                         return (error);
 3475                 data = &x.rk;
 3476                 break;
 3477         case PROC_ASLR_STATUS:
 3478         case PROC_STACKGAP_STATUS:
 3479         case PROC_TRACE_STATUS:
 3480         case PROC_TRAPCAP_STATUS:
 3481                 data = &flags;
 3482                 break;
 3483         case PROC_PDEATHSIG_CTL:
 3484                 error = copyin(uap->data, &signum, sizeof(signum));
 3485                 if (error != 0)
 3486                         return (error);
 3487                 data = &signum;
 3488                 break;
 3489         case PROC_PDEATHSIG_STATUS:
 3490                 data = &signum;
 3491                 break;
 3492         default:
 3493                 return (EINVAL);
 3494         }
 3495         error = kern_procctl(td, uap->idtype, PAIR32TO64(id_t, uap->id),
 3496             uap->com, data);
 3497         switch (uap->com) {
 3498         case PROC_REAP_STATUS:
 3499                 if (error == 0)
 3500                         error = copyout(&x.rs, uap->data, sizeof(x.rs));
 3501                 break;
 3502         case PROC_REAP_KILL:
 3503                 error1 = copyout(&x.rk, uap->data, sizeof(x.rk));
 3504                 if (error == 0)
 3505                         error = error1;
 3506                 break;
 3507         case PROC_ASLR_STATUS:
 3508         case PROC_STACKGAP_STATUS:
 3509         case PROC_TRACE_STATUS:
 3510         case PROC_TRAPCAP_STATUS:
 3511                 if (error == 0)
 3512                         error = copyout(&flags, uap->data, sizeof(flags));
 3513                 break;
 3514         case PROC_PDEATHSIG_STATUS:
 3515                 if (error == 0)
 3516                         error = copyout(&signum, uap->data, sizeof(signum));
 3517                 break;
 3518         }
 3519         return (error);
 3520 }
 3521 
 3522 int
 3523 freebsd32_fcntl(struct thread *td, struct freebsd32_fcntl_args *uap)
 3524 {
 3525         long tmp;
 3526 
 3527         switch (uap->cmd) {
 3528         /*
 3529          * Do unsigned conversion for arg when operation
 3530          * interprets it as flags or pointer.
 3531          */
 3532         case F_SETLK_REMOTE:
 3533         case F_SETLKW:
 3534         case F_SETLK:
 3535         case F_GETLK:
 3536         case F_SETFD:
 3537         case F_SETFL:
 3538         case F_OGETLK:
 3539         case F_OSETLK:
 3540         case F_OSETLKW:
 3541                 tmp = (unsigned int)(uap->arg);
 3542                 break;
 3543         default:
 3544                 tmp = uap->arg;
 3545                 break;
 3546         }
 3547         return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, tmp));
 3548 }
 3549 
 3550 int
 3551 freebsd32_ppoll(struct thread *td, struct freebsd32_ppoll_args *uap)
 3552 {
 3553         struct timespec32 ts32;
 3554         struct timespec ts, *tsp;
 3555         sigset_t set, *ssp;
 3556         int error;
 3557 
 3558         if (uap->ts != NULL) {
 3559                 error = copyin(uap->ts, &ts32, sizeof(ts32));
 3560                 if (error != 0)
 3561                         return (error);
 3562                 CP(ts32, ts, tv_sec);
 3563                 CP(ts32, ts, tv_nsec);
 3564                 tsp = &ts;
 3565         } else
 3566                 tsp = NULL;
 3567         if (uap->set != NULL) {
 3568                 error = copyin(uap->set, &set, sizeof(set));
 3569                 if (error != 0)
 3570                         return (error);
 3571                 ssp = &set;
 3572         } else
 3573                 ssp = NULL;
 3574 
 3575         return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
 3576 }
 3577 
 3578 int
 3579 freebsd32_sched_rr_get_interval(struct thread *td,
 3580     struct freebsd32_sched_rr_get_interval_args *uap)
 3581 {
 3582         struct timespec ts;
 3583         struct timespec32 ts32;
 3584         int error;
 3585 
 3586         error = kern_sched_rr_get_interval(td, uap->pid, &ts);
 3587         if (error == 0) {
 3588                 CP(ts, ts32, tv_sec);
 3589                 CP(ts, ts32, tv_nsec);
 3590                 error = copyout(&ts32, uap->interval, sizeof(ts32));
 3591         }
 3592         return (error);
 3593 }
 3594 
 3595 static void
 3596 timex_to_32(struct timex32 *dst, struct timex *src)
 3597 {
 3598         CP(*src, *dst, modes);
 3599         CP(*src, *dst, offset);
 3600         CP(*src, *dst, freq);
 3601         CP(*src, *dst, maxerror);
 3602         CP(*src, *dst, esterror);
 3603         CP(*src, *dst, status);
 3604         CP(*src, *dst, constant);
 3605         CP(*src, *dst, precision);
 3606         CP(*src, *dst, tolerance);
 3607         CP(*src, *dst, ppsfreq);
 3608         CP(*src, *dst, jitter);
 3609         CP(*src, *dst, shift);
 3610         CP(*src, *dst, stabil);
 3611         CP(*src, *dst, jitcnt);
 3612         CP(*src, *dst, calcnt);
 3613         CP(*src, *dst, errcnt);
 3614         CP(*src, *dst, stbcnt);
 3615 }
 3616 
 3617 static void
 3618 timex_from_32(struct timex *dst, struct timex32 *src)
 3619 {
 3620         CP(*src, *dst, modes);
 3621         CP(*src, *dst, offset);
 3622         CP(*src, *dst, freq);
 3623         CP(*src, *dst, maxerror);
 3624         CP(*src, *dst, esterror);
 3625         CP(*src, *dst, status);
 3626         CP(*src, *dst, constant);
 3627         CP(*src, *dst, precision);
 3628         CP(*src, *dst, tolerance);
 3629         CP(*src, *dst, ppsfreq);
 3630         CP(*src, *dst, jitter);
 3631         CP(*src, *dst, shift);
 3632         CP(*src, *dst, stabil);
 3633         CP(*src, *dst, jitcnt);
 3634         CP(*src, *dst, calcnt);
 3635         CP(*src, *dst, errcnt);
 3636         CP(*src, *dst, stbcnt);
 3637 }
 3638 
 3639 int
 3640 freebsd32_ntp_adjtime(struct thread *td, struct freebsd32_ntp_adjtime_args *uap)
 3641 {
 3642         struct timex tx;
 3643         struct timex32 tx32;
 3644         int error, retval;
 3645 
 3646         error = copyin(uap->tp, &tx32, sizeof(tx32));
 3647         if (error == 0) {
 3648                 timex_from_32(&tx, &tx32);
 3649                 error = kern_ntp_adjtime(td, &tx, &retval);
 3650                 if (error == 0) {
 3651                         timex_to_32(&tx32, &tx);
 3652                         error = copyout(&tx32, uap->tp, sizeof(tx32));
 3653                         if (error == 0)
 3654                                 td->td_retval[0] = retval;
 3655                 }
 3656         }
 3657         return (error);
 3658 }

Cache object: 2e887dc2c32cd8bf4fb5a490db5cedb7


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.