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

Cache object: f0956ad6b854507e2d691d14322bd6d5


[ 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.