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  * Copyright (c) 2002 Doug Rabson
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_compat.h"
   31 
   32 #include <sys/param.h>
   33 #include <sys/bus.h>
   34 #include <sys/clock.h>
   35 #include <sys/exec.h>
   36 #include <sys/fcntl.h>
   37 #include <sys/filedesc.h>
   38 #include <sys/imgact.h>
   39 #include <sys/jail.h>
   40 #include <sys/kernel.h>
   41 #include <sys/limits.h>
   42 #include <sys/lock.h>
   43 #include <sys/malloc.h>
   44 #include <sys/file.h>           /* Must come after sys/malloc.h */
   45 #include <sys/mbuf.h>
   46 #include <sys/mman.h>
   47 #include <sys/module.h>
   48 #include <sys/mount.h>
   49 #include <sys/mutex.h>
   50 #include <sys/namei.h>
   51 #include <sys/proc.h>
   52 #include <sys/reboot.h>
   53 #include <sys/resource.h>
   54 #include <sys/resourcevar.h>
   55 #include <sys/selinfo.h>
   56 #include <sys/eventvar.h>       /* Must come after sys/selinfo.h */
   57 #include <sys/pipe.h>           /* Must come after sys/selinfo.h */
   58 #include <sys/signal.h>
   59 #include <sys/signalvar.h>
   60 #include <sys/socket.h>
   61 #include <sys/socketvar.h>
   62 #include <sys/stat.h>
   63 #include <sys/syscall.h>
   64 #include <sys/syscallsubr.h>
   65 #include <sys/sysctl.h>
   66 #include <sys/sysent.h>
   67 #include <sys/sysproto.h>
   68 #include <sys/systm.h>
   69 #include <sys/thr.h>
   70 #include <sys/unistd.h>
   71 #include <sys/ucontext.h>
   72 #include <sys/vnode.h>
   73 #include <sys/wait.h>
   74 #include <sys/ipc.h>
   75 #include <sys/msg.h>
   76 #include <sys/sem.h>
   77 #include <sys/shm.h>
   78 
   79 #include <vm/vm.h>
   80 #include <vm/vm_kern.h>
   81 #include <vm/vm_param.h>
   82 #include <vm/pmap.h>
   83 #include <vm/vm_map.h>
   84 #include <vm/vm_object.h>
   85 #include <vm/vm_extern.h>
   86 
   87 #include <machine/cpu.h>
   88 
   89 #include <security/audit/audit.h>
   90 
   91 #include <compat/freebsd32/freebsd32_util.h>
   92 #include <compat/freebsd32/freebsd32.h>
   93 #include <compat/freebsd32/freebsd32_ipc.h>
   94 #include <compat/freebsd32/freebsd32_signal.h>
   95 #include <compat/freebsd32/freebsd32_proto.h>
   96 
   97 CTASSERT(sizeof(struct timeval32) == 8);
   98 CTASSERT(sizeof(struct timespec32) == 8);
   99 CTASSERT(sizeof(struct itimerval32) == 16);
  100 CTASSERT(sizeof(struct statfs32) == 256);
  101 CTASSERT(sizeof(struct rusage32) == 72);
  102 CTASSERT(sizeof(struct sigaltstack32) == 12);
  103 CTASSERT(sizeof(struct kevent32) == 20);
  104 CTASSERT(sizeof(struct iovec32) == 8);
  105 CTASSERT(sizeof(struct msghdr32) == 28);
  106 CTASSERT(sizeof(struct stat32) == 96);
  107 CTASSERT(sizeof(struct sigaction32) == 24);
  108 
  109 static int freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count);
  110 static int freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count);
  111 
  112 int
  113 freebsd32_wait4(struct thread *td, struct freebsd32_wait4_args *uap)
  114 {
  115         int error, status;
  116         struct rusage32 ru32;
  117         struct rusage ru, *rup;
  118 
  119         if (uap->rusage != NULL)
  120                 rup = &ru;
  121         else
  122                 rup = NULL;
  123         error = kern_wait(td, uap->pid, &status, uap->options, rup);
  124         if (error)
  125                 return (error);
  126         if (uap->status != NULL)
  127                 error = copyout(&status, uap->status, sizeof(status));
  128         if (uap->rusage != NULL && error == 0) {
  129                 TV_CP(ru, ru32, ru_utime);
  130                 TV_CP(ru, ru32, ru_stime);
  131                 CP(ru, ru32, ru_maxrss);
  132                 CP(ru, ru32, ru_ixrss);
  133                 CP(ru, ru32, ru_idrss);
  134                 CP(ru, ru32, ru_isrss);
  135                 CP(ru, ru32, ru_minflt);
  136                 CP(ru, ru32, ru_majflt);
  137                 CP(ru, ru32, ru_nswap);
  138                 CP(ru, ru32, ru_inblock);
  139                 CP(ru, ru32, ru_oublock);
  140                 CP(ru, ru32, ru_msgsnd);
  141                 CP(ru, ru32, ru_msgrcv);
  142                 CP(ru, ru32, ru_nsignals);
  143                 CP(ru, ru32, ru_nvcsw);
  144                 CP(ru, ru32, ru_nivcsw);
  145                 error = copyout(&ru32, uap->rusage, sizeof(ru32));
  146         }
  147         return (error);
  148 }
  149 
  150 #ifdef COMPAT_FREEBSD4
  151 static void
  152 copy_statfs(struct statfs *in, struct statfs32 *out)
  153 {
  154 
  155         statfs_scale_blocks(in, INT32_MAX);
  156         bzero(out, sizeof(*out));
  157         CP(*in, *out, f_bsize);
  158         out->f_iosize = MIN(in->f_iosize, INT32_MAX);
  159         CP(*in, *out, f_blocks);
  160         CP(*in, *out, f_bfree);
  161         CP(*in, *out, f_bavail);
  162         out->f_files = MIN(in->f_files, INT32_MAX);
  163         out->f_ffree = MIN(in->f_ffree, INT32_MAX);
  164         CP(*in, *out, f_fsid);
  165         CP(*in, *out, f_owner);
  166         CP(*in, *out, f_type);
  167         CP(*in, *out, f_flags);
  168         out->f_syncwrites = MIN(in->f_syncwrites, INT32_MAX);
  169         out->f_asyncwrites = MIN(in->f_asyncwrites, INT32_MAX);
  170         strlcpy(out->f_fstypename,
  171               in->f_fstypename, MFSNAMELEN);
  172         strlcpy(out->f_mntonname,
  173               in->f_mntonname, min(MNAMELEN, FREEBSD4_MNAMELEN));
  174         out->f_syncreads = MIN(in->f_syncreads, INT32_MAX);
  175         out->f_asyncreads = MIN(in->f_asyncreads, INT32_MAX);
  176         strlcpy(out->f_mntfromname,
  177               in->f_mntfromname, min(MNAMELEN, FREEBSD4_MNAMELEN));
  178 }
  179 #endif
  180 
  181 #ifdef COMPAT_FREEBSD4
  182 int
  183 freebsd4_freebsd32_getfsstat(struct thread *td, struct freebsd4_freebsd32_getfsstat_args *uap)
  184 {
  185         struct statfs *buf, *sp;
  186         struct statfs32 stat32;
  187         size_t count, size;
  188         int error;
  189 
  190         count = uap->bufsize / sizeof(struct statfs32);
  191         size = count * sizeof(struct statfs);
  192         error = kern_getfsstat(td, &buf, size, UIO_SYSSPACE, uap->flags);
  193         if (size > 0) {
  194                 count = td->td_retval[0];
  195                 sp = buf;
  196                 while (count > 0 && error == 0) {
  197                         copy_statfs(sp, &stat32);
  198                         error = copyout(&stat32, uap->buf, sizeof(stat32));
  199                         sp++;
  200                         uap->buf++;
  201                         count--;
  202                 }
  203                 free(buf, M_TEMP);
  204         }
  205         return (error);
  206 }
  207 #endif
  208 
  209 int
  210 freebsd32_sigaltstack(struct thread *td,
  211                       struct freebsd32_sigaltstack_args *uap)
  212 {
  213         struct sigaltstack32 s32;
  214         struct sigaltstack ss, oss, *ssp;
  215         int error;
  216 
  217         if (uap->ss != NULL) {
  218                 error = copyin(uap->ss, &s32, sizeof(s32));
  219                 if (error)
  220                         return (error);
  221                 PTRIN_CP(s32, ss, ss_sp);
  222                 CP(s32, ss, ss_size);
  223                 CP(s32, ss, ss_flags);
  224                 ssp = &ss;
  225         } else
  226                 ssp = NULL;
  227         error = kern_sigaltstack(td, ssp, &oss);
  228         if (error == 0 && uap->oss != NULL) {
  229                 PTROUT_CP(oss, s32, ss_sp);
  230                 CP(oss, s32, ss_size);
  231                 CP(oss, s32, ss_flags);
  232                 error = copyout(&s32, uap->oss, sizeof(s32));
  233         }
  234         return (error);
  235 }
  236 
  237 /*
  238  * Custom version of exec_copyin_args() so that we can translate
  239  * the pointers.
  240  */
  241 static int
  242 freebsd32_exec_copyin_args(struct image_args *args, char *fname,
  243     enum uio_seg segflg, u_int32_t *argv, u_int32_t *envv)
  244 {
  245         char *argp, *envp;
  246         u_int32_t *p32, arg;
  247         size_t length;
  248         int error;
  249 
  250         bzero(args, sizeof(*args));
  251         if (argv == NULL)
  252                 return (EFAULT);
  253 
  254         /*
  255          * Allocate temporary demand zeroed space for argument and
  256          *      environment strings
  257          */
  258         args->buf = (char *) kmem_alloc_wait(exec_map,
  259             PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
  260         if (args->buf == NULL)
  261                 return (ENOMEM);
  262         args->begin_argv = args->buf;
  263         args->endp = args->begin_argv;
  264         args->stringspace = ARG_MAX;
  265 
  266         args->fname = args->buf + ARG_MAX;
  267 
  268         /*
  269          * Copy the file name.
  270          */
  271         error = (segflg == UIO_SYSSPACE) ?
  272             copystr(fname, args->fname, PATH_MAX, &length) :
  273             copyinstr(fname, args->fname, PATH_MAX, &length);
  274         if (error != 0)
  275                 goto err_exit;
  276 
  277         /*
  278          * extract arguments first
  279          */
  280         p32 = argv;
  281         for (;;) {
  282                 error = copyin(p32++, &arg, sizeof(arg));
  283                 if (error)
  284                         goto err_exit;
  285                 if (arg == 0)
  286                         break;
  287                 argp = PTRIN(arg);
  288                 error = copyinstr(argp, args->endp, args->stringspace, &length);
  289                 if (error) {
  290                         if (error == ENAMETOOLONG)
  291                                 error = E2BIG;
  292                         goto err_exit;
  293                 }
  294                 args->stringspace -= length;
  295                 args->endp += length;
  296                 args->argc++;
  297         }
  298                         
  299         args->begin_envv = args->endp;
  300 
  301         /*
  302          * extract environment strings
  303          */
  304         if (envv) {
  305                 p32 = envv;
  306                 for (;;) {
  307                         error = copyin(p32++, &arg, sizeof(arg));
  308                         if (error)
  309                                 goto err_exit;
  310                         if (arg == 0)
  311                                 break;
  312                         envp = PTRIN(arg);
  313                         error = copyinstr(envp, args->endp, args->stringspace,
  314                             &length);
  315                         if (error) {
  316                                 if (error == ENAMETOOLONG)
  317                                         error = E2BIG;
  318                                 goto err_exit;
  319                         }
  320                         args->stringspace -= length;
  321                         args->endp += length;
  322                         args->envc++;
  323                 }
  324         }
  325 
  326         return (0);
  327 
  328 err_exit:
  329         kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
  330             PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
  331         args->buf = NULL;
  332         return (error);
  333 }
  334 
  335 int
  336 freebsd32_execve(struct thread *td, struct freebsd32_execve_args *uap)
  337 {
  338         struct image_args eargs;
  339         int error;
  340 
  341         error = freebsd32_exec_copyin_args(&eargs, uap->fname, UIO_USERSPACE,
  342             uap->argv, uap->envv);
  343         if (error == 0)
  344                 error = kern_execve(td, &eargs, NULL);
  345         return (error);
  346 }
  347 
  348 #ifdef __ia64__
  349 static int
  350 freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end,
  351                        int prot, int fd, off_t pos)
  352 {
  353         vm_map_t map;
  354         vm_map_entry_t entry;
  355         int rv;
  356 
  357         map = &td->td_proc->p_vmspace->vm_map;
  358         if (fd != -1)
  359                 prot |= VM_PROT_WRITE;
  360 
  361         if (vm_map_lookup_entry(map, start, &entry)) {
  362                 if ((entry->protection & prot) != prot) {
  363                         rv = vm_map_protect(map,
  364                                             trunc_page(start),
  365                                             round_page(end),
  366                                             entry->protection | prot,
  367                                             FALSE);
  368                         if (rv != KERN_SUCCESS)
  369                                 return (EINVAL);
  370                 }
  371         } else {
  372                 vm_offset_t addr = trunc_page(start);
  373                 rv = vm_map_find(map, 0, 0,
  374                                  &addr, PAGE_SIZE, FALSE, prot,
  375                                  VM_PROT_ALL, 0);
  376                 if (rv != KERN_SUCCESS)
  377                         return (EINVAL);
  378         }
  379 
  380         if (fd != -1) {
  381                 struct pread_args r;
  382                 r.fd = fd;
  383                 r.buf = (void *) start;
  384                 r.nbyte = end - start;
  385                 r.offset = pos;
  386                 return (pread(td, &r));
  387         } else {
  388                 while (start < end) {
  389                         subyte((void *) start, 0);
  390                         start++;
  391                 }
  392                 return (0);
  393         }
  394 }
  395 #endif
  396 
  397 int
  398 freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap)
  399 {
  400         struct mmap_args ap;
  401         vm_offset_t addr = (vm_offset_t) uap->addr;
  402         vm_size_t len    = uap->len;
  403         int prot         = uap->prot;
  404         int flags        = uap->flags;
  405         int fd           = uap->fd;
  406         off_t pos        = (uap->poslo
  407                             | ((off_t)uap->poshi << 32));
  408 #ifdef __ia64__
  409         vm_size_t pageoff;
  410         int error;
  411 
  412         /*
  413          * Attempt to handle page size hassles.
  414          */
  415         pageoff = (pos & PAGE_MASK);
  416         if (flags & MAP_FIXED) {
  417                 vm_offset_t start, end;
  418                 start = addr;
  419                 end = addr + len;
  420 
  421                 if (start != trunc_page(start)) {
  422                         error = freebsd32_mmap_partial(td, start,
  423                                                        round_page(start), prot,
  424                                                        fd, pos);
  425                         if (fd != -1)
  426                                 pos += round_page(start) - start;
  427                         start = round_page(start);
  428                 }
  429                 if (end != round_page(end)) {
  430                         vm_offset_t t = trunc_page(end);
  431                         error = freebsd32_mmap_partial(td, t, end,
  432                                                   prot, fd,
  433                                                   pos + t - start);
  434                         end = trunc_page(end);
  435                 }
  436                 if (end > start && fd != -1 && (pos & PAGE_MASK)) {
  437                         /*
  438                          * We can't map this region at all. The specified
  439                          * address doesn't have the same alignment as the file
  440                          * position. Fake the mapping by simply reading the
  441                          * entire region into memory. First we need to make
  442                          * sure the region exists.
  443                          */
  444                         vm_map_t map;
  445                         struct pread_args r;
  446                         int rv;
  447 
  448                         prot |= VM_PROT_WRITE;
  449                         map = &td->td_proc->p_vmspace->vm_map;
  450                         rv = vm_map_remove(map, start, end);
  451                         if (rv != KERN_SUCCESS)
  452                                 return (EINVAL);
  453                         rv = vm_map_find(map, 0, 0,
  454                                          &start, end - start, FALSE,
  455                                          prot, VM_PROT_ALL, 0);
  456                         if (rv != KERN_SUCCESS)
  457                                 return (EINVAL);
  458                         r.fd = fd;
  459                         r.buf = (void *) start;
  460                         r.nbyte = end - start;
  461                         r.offset = pos;
  462                         error = pread(td, &r);
  463                         if (error)
  464                                 return (error);
  465 
  466                         td->td_retval[0] = addr;
  467                         return (0);
  468                 }
  469                 if (end == start) {
  470                         /*
  471                          * After dealing with the ragged ends, there
  472                          * might be none left.
  473                          */
  474                         td->td_retval[0] = addr;
  475                         return (0);
  476                 }
  477                 addr = start;
  478                 len = end - start;
  479         }
  480 #endif
  481 
  482         ap.addr = (void *) addr;
  483         ap.len = len;
  484         ap.prot = prot;
  485         ap.flags = flags;
  486         ap.fd = fd;
  487         ap.pos = pos;
  488 
  489         return (mmap(td, &ap));
  490 }
  491 
  492 #ifdef COMPAT_FREEBSD6
  493 int
  494 freebsd6_freebsd32_mmap(struct thread *td, struct freebsd6_freebsd32_mmap_args *uap)
  495 {
  496         struct freebsd32_mmap_args ap;
  497 
  498         ap.addr = uap->addr;
  499         ap.len = uap->len;
  500         ap.prot = uap->prot;
  501         ap.flags = uap->flags;
  502         ap.fd = uap->fd;
  503         ap.poslo = uap->poslo;
  504         ap.poshi = uap->poshi;
  505 
  506         return (freebsd32_mmap(td, &ap));
  507 }
  508 #endif
  509 
  510 int
  511 freebsd32_setitimer(struct thread *td, struct freebsd32_setitimer_args *uap)
  512 {
  513         struct itimerval itv, oitv, *itvp;      
  514         struct itimerval32 i32;
  515         int error;
  516 
  517         if (uap->itv != NULL) {
  518                 error = copyin(uap->itv, &i32, sizeof(i32));
  519                 if (error)
  520                         return (error);
  521                 TV_CP(i32, itv, it_interval);
  522                 TV_CP(i32, itv, it_value);
  523                 itvp = &itv;
  524         } else
  525                 itvp = NULL;
  526         error = kern_setitimer(td, uap->which, itvp, &oitv);
  527         if (error || uap->oitv == NULL)
  528                 return (error);
  529         TV_CP(oitv, i32, it_interval);
  530         TV_CP(oitv, i32, it_value);
  531         return (copyout(&i32, uap->oitv, sizeof(i32)));
  532 }
  533 
  534 int
  535 freebsd32_getitimer(struct thread *td, struct freebsd32_getitimer_args *uap)
  536 {
  537         struct itimerval itv;
  538         struct itimerval32 i32;
  539         int error;
  540 
  541         error = kern_getitimer(td, uap->which, &itv);
  542         if (error || uap->itv == NULL)
  543                 return (error);
  544         TV_CP(itv, i32, it_interval);
  545         TV_CP(itv, i32, it_value);
  546         return (copyout(&i32, uap->itv, sizeof(i32)));
  547 }
  548 
  549 int
  550 freebsd32_select(struct thread *td, struct freebsd32_select_args *uap)
  551 {
  552         struct timeval32 tv32;
  553         struct timeval tv, *tvp;
  554         int error;
  555 
  556         if (uap->tv != NULL) {
  557                 error = copyin(uap->tv, &tv32, sizeof(tv32));
  558                 if (error)
  559                         return (error);
  560                 CP(tv32, tv, tv_sec);
  561                 CP(tv32, tv, tv_usec);
  562                 tvp = &tv;
  563         } else
  564                 tvp = NULL;
  565         /*
  566          * XXX big-endian needs to convert the fd_sets too.
  567          * XXX Do pointers need PTRIN()?
  568          */
  569         return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
  570 }
  571 
  572 /*
  573  * Copy 'count' items into the destination list pointed to by uap->eventlist.
  574  */
  575 static int
  576 freebsd32_kevent_copyout(void *arg, struct kevent *kevp, int count)
  577 {
  578         struct freebsd32_kevent_args *uap;
  579         struct kevent32 ks32[KQ_NEVENTS];
  580         int i, error = 0;
  581 
  582         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
  583         uap = (struct freebsd32_kevent_args *)arg;
  584 
  585         for (i = 0; i < count; i++) {
  586                 CP(kevp[i], ks32[i], ident);
  587                 CP(kevp[i], ks32[i], filter);
  588                 CP(kevp[i], ks32[i], flags);
  589                 CP(kevp[i], ks32[i], fflags);
  590                 CP(kevp[i], ks32[i], data);
  591                 PTROUT_CP(kevp[i], ks32[i], udata);
  592         }
  593         error = copyout(ks32, uap->eventlist, count * sizeof *ks32);
  594         if (error == 0)
  595                 uap->eventlist += count;
  596         return (error);
  597 }
  598 
  599 /*
  600  * Copy 'count' items from the list pointed to by uap->changelist.
  601  */
  602 static int
  603 freebsd32_kevent_copyin(void *arg, struct kevent *kevp, int count)
  604 {
  605         struct freebsd32_kevent_args *uap;
  606         struct kevent32 ks32[KQ_NEVENTS];
  607         int i, error = 0;
  608 
  609         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
  610         uap = (struct freebsd32_kevent_args *)arg;
  611 
  612         error = copyin(uap->changelist, ks32, count * sizeof *ks32);
  613         if (error)
  614                 goto done;
  615         uap->changelist += count;
  616 
  617         for (i = 0; i < count; i++) {
  618                 CP(ks32[i], kevp[i], ident);
  619                 CP(ks32[i], kevp[i], filter);
  620                 CP(ks32[i], kevp[i], flags);
  621                 CP(ks32[i], kevp[i], fflags);
  622                 CP(ks32[i], kevp[i], data);
  623                 PTRIN_CP(ks32[i], kevp[i], udata);
  624         }
  625 done:
  626         return (error);
  627 }
  628 
  629 int
  630 freebsd32_kevent(struct thread *td, struct freebsd32_kevent_args *uap)
  631 {
  632         struct timespec32 ts32;
  633         struct timespec ts, *tsp;
  634         struct kevent_copyops k_ops = { uap,
  635                                         freebsd32_kevent_copyout,
  636                                         freebsd32_kevent_copyin};
  637         int error;
  638 
  639 
  640         if (uap->timeout) {
  641                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
  642                 if (error)
  643                         return (error);
  644                 CP(ts32, ts, tv_sec);
  645                 CP(ts32, ts, tv_nsec);
  646                 tsp = &ts;
  647         } else
  648                 tsp = NULL;
  649         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
  650             &k_ops, tsp);
  651         return (error);
  652 }
  653 
  654 int
  655 freebsd32_gettimeofday(struct thread *td,
  656                        struct freebsd32_gettimeofday_args *uap)
  657 {
  658         struct timeval atv;
  659         struct timeval32 atv32;
  660         struct timezone rtz;
  661         int error = 0;
  662 
  663         if (uap->tp) {
  664                 microtime(&atv);
  665                 CP(atv, atv32, tv_sec);
  666                 CP(atv, atv32, tv_usec);
  667                 error = copyout(&atv32, uap->tp, sizeof (atv32));
  668         }
  669         if (error == 0 && uap->tzp != NULL) {
  670                 rtz.tz_minuteswest = tz_minuteswest;
  671                 rtz.tz_dsttime = tz_dsttime;
  672                 error = copyout(&rtz, uap->tzp, sizeof (rtz));
  673         }
  674         return (error);
  675 }
  676 
  677 int
  678 freebsd32_getrusage(struct thread *td, struct freebsd32_getrusage_args *uap)
  679 {
  680         struct rusage32 s32;
  681         struct rusage s;
  682         int error;
  683 
  684         error = kern_getrusage(td, uap->who, &s);
  685         if (error)
  686                 return (error);
  687         if (uap->rusage != NULL) {
  688                 TV_CP(s, s32, ru_utime);
  689                 TV_CP(s, s32, ru_stime);
  690                 CP(s, s32, ru_maxrss);
  691                 CP(s, s32, ru_ixrss);
  692                 CP(s, s32, ru_idrss);
  693                 CP(s, s32, ru_isrss);
  694                 CP(s, s32, ru_minflt);
  695                 CP(s, s32, ru_majflt);
  696                 CP(s, s32, ru_nswap);
  697                 CP(s, s32, ru_inblock);
  698                 CP(s, s32, ru_oublock);
  699                 CP(s, s32, ru_msgsnd);
  700                 CP(s, s32, ru_msgrcv);
  701                 CP(s, s32, ru_nsignals);
  702                 CP(s, s32, ru_nvcsw);
  703                 CP(s, s32, ru_nivcsw);
  704                 error = copyout(&s32, uap->rusage, sizeof(s32));
  705         }
  706         return (error);
  707 }
  708 
  709 static int
  710 freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop)
  711 {
  712         struct iovec32 iov32;
  713         struct iovec *iov;
  714         struct uio *uio;
  715         u_int iovlen;
  716         int error, i;
  717 
  718         *uiop = NULL;
  719         if (iovcnt > UIO_MAXIOV)
  720                 return (EINVAL);
  721         iovlen = iovcnt * sizeof(struct iovec);
  722         uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK);
  723         iov = (struct iovec *)(uio + 1);
  724         for (i = 0; i < iovcnt; i++) {
  725                 error = copyin(&iovp[i], &iov32, sizeof(struct iovec32));
  726                 if (error) {
  727                         free(uio, M_IOV);
  728                         return (error);
  729                 }
  730                 iov[i].iov_base = PTRIN(iov32.iov_base);
  731                 iov[i].iov_len = iov32.iov_len;
  732         }
  733         uio->uio_iov = iov;
  734         uio->uio_iovcnt = iovcnt;
  735         uio->uio_segflg = UIO_USERSPACE;
  736         uio->uio_offset = -1;
  737         uio->uio_resid = 0;
  738         for (i = 0; i < iovcnt; i++) {
  739                 if (iov->iov_len > INT_MAX - uio->uio_resid) {
  740                         free(uio, M_IOV);
  741                         return (EINVAL);
  742                 }
  743                 uio->uio_resid += iov->iov_len;
  744                 iov++;
  745         }
  746         *uiop = uio;
  747         return (0);
  748 }
  749 
  750 int
  751 freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap)
  752 {
  753         struct uio *auio;
  754         int error;
  755 
  756         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  757         if (error)
  758                 return (error);
  759         error = kern_readv(td, uap->fd, auio);
  760         free(auio, M_IOV);
  761         return (error);
  762 }
  763 
  764 int
  765 freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap)
  766 {
  767         struct uio *auio;
  768         int error;
  769 
  770         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  771         if (error)
  772                 return (error);
  773         error = kern_writev(td, uap->fd, auio);
  774         free(auio, M_IOV);
  775         return (error);
  776 }
  777 
  778 int
  779 freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap)
  780 {
  781         struct uio *auio;
  782         int error;
  783 
  784         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  785         if (error)
  786                 return (error);
  787         error = kern_preadv(td, uap->fd, auio, uap->offset);
  788         free(auio, M_IOV);
  789         return (error);
  790 }
  791 
  792 int
  793 freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap)
  794 {
  795         struct uio *auio;
  796         int error;
  797 
  798         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
  799         if (error)
  800                 return (error);
  801         error = kern_pwritev(td, uap->fd, auio, uap->offset);
  802         free(auio, M_IOV);
  803         return (error);
  804 }
  805 
  806 static int
  807 freebsd32_copyiniov(struct iovec32 *iovp32, u_int iovcnt, struct iovec **iovp,
  808     int error)
  809 {
  810         struct iovec32 iov32;
  811         struct iovec *iov;
  812         u_int iovlen;
  813         int i;
  814 
  815         *iovp = NULL;
  816         if (iovcnt > UIO_MAXIOV)
  817                 return (error);
  818         iovlen = iovcnt * sizeof(struct iovec);
  819         iov = malloc(iovlen, M_IOV, M_WAITOK);
  820         for (i = 0; i < iovcnt; i++) {
  821                 error = copyin(&iovp32[i], &iov32, sizeof(struct iovec32));
  822                 if (error) {
  823                         free(iov, M_IOV);
  824                         return (error);
  825                 }
  826                 iov[i].iov_base = PTRIN(iov32.iov_base);
  827                 iov[i].iov_len = iov32.iov_len;
  828         }
  829         *iovp = iov;
  830         return (0);
  831 }
  832 
  833 static int
  834 freebsd32_copyinmsghdr(struct msghdr32 *msg32, struct msghdr *msg)
  835 {
  836         struct msghdr32 m32;
  837         int error;
  838 
  839         error = copyin(msg32, &m32, sizeof(m32));
  840         if (error)
  841                 return (error);
  842         msg->msg_name = PTRIN(m32.msg_name);
  843         msg->msg_namelen = m32.msg_namelen;
  844         msg->msg_iov = PTRIN(m32.msg_iov);
  845         msg->msg_iovlen = m32.msg_iovlen;
  846         msg->msg_control = PTRIN(m32.msg_control);
  847         msg->msg_controllen = m32.msg_controllen;
  848         msg->msg_flags = m32.msg_flags;
  849         return (0);
  850 }
  851 
  852 static int
  853 freebsd32_copyoutmsghdr(struct msghdr *msg, struct msghdr32 *msg32)
  854 {
  855         struct msghdr32 m32;
  856         int error;
  857 
  858         m32.msg_name = PTROUT(msg->msg_name);
  859         m32.msg_namelen = msg->msg_namelen;
  860         m32.msg_iov = PTROUT(msg->msg_iov);
  861         m32.msg_iovlen = msg->msg_iovlen;
  862         m32.msg_control = PTROUT(msg->msg_control);
  863         m32.msg_controllen = msg->msg_controllen;
  864         m32.msg_flags = msg->msg_flags;
  865         error = copyout(&m32, msg32, sizeof(m32));
  866         return (error);
  867 }
  868 
  869 #define FREEBSD32_ALIGNBYTES    (sizeof(int) - 1)
  870 #define FREEBSD32_ALIGN(p)      \
  871         (((u_long)(p) + FREEBSD32_ALIGNBYTES) & ~FREEBSD32_ALIGNBYTES)
  872 #define FREEBSD32_CMSG_SPACE(l) \
  873         (FREEBSD32_ALIGN(sizeof(struct cmsghdr)) + FREEBSD32_ALIGN(l))
  874 
  875 #define FREEBSD32_CMSG_DATA(cmsg)       ((unsigned char *)(cmsg) + \
  876                                  FREEBSD32_ALIGN(sizeof(struct cmsghdr)))
  877 static int
  878 freebsd32_copy_msg_out(struct msghdr *msg, struct mbuf *control)
  879 {
  880         struct cmsghdr *cm;
  881         void *data;
  882         socklen_t clen, datalen;
  883         int error;
  884         caddr_t ctlbuf;
  885         int len, maxlen, copylen;
  886         struct mbuf *m;
  887         error = 0;
  888 
  889         len    = msg->msg_controllen;
  890         maxlen = msg->msg_controllen;
  891         msg->msg_controllen = 0;
  892 
  893         m = control;
  894         ctlbuf = msg->msg_control;
  895       
  896         while (m && len > 0) {
  897                 cm = mtod(m, struct cmsghdr *);
  898                 clen = m->m_len;
  899 
  900                 while (cm != NULL) {
  901 
  902                         if (sizeof(struct cmsghdr) > clen ||
  903                             cm->cmsg_len > clen) {
  904                                 error = EINVAL;
  905                                 break;
  906                         }       
  907 
  908                         data   = CMSG_DATA(cm);
  909                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
  910 
  911                         /* Adjust message length */
  912                         cm->cmsg_len = FREEBSD32_ALIGN(sizeof(struct cmsghdr)) +
  913                             datalen;
  914 
  915 
  916                         /* Copy cmsghdr */
  917                         copylen = sizeof(struct cmsghdr);
  918                         if (len < copylen) {
  919                                 msg->msg_flags |= MSG_CTRUNC;
  920                                 copylen = len;
  921                         }
  922 
  923                         error = copyout(cm,ctlbuf,copylen);
  924                         if (error)
  925                                 goto exit;
  926 
  927                         ctlbuf += FREEBSD32_ALIGN(copylen);
  928                         len    -= FREEBSD32_ALIGN(copylen);
  929 
  930                         if (len <= 0)
  931                                 break;
  932 
  933                         /* Copy data */
  934                         copylen = datalen;
  935                         if (len < copylen) {
  936                                 msg->msg_flags |= MSG_CTRUNC;
  937                                 copylen = len;
  938                         }
  939 
  940                         error = copyout(data,ctlbuf,copylen);
  941                         if (error)
  942                                 goto exit;
  943 
  944                         ctlbuf += FREEBSD32_ALIGN(copylen);
  945                         len    -= FREEBSD32_ALIGN(copylen);
  946 
  947                         if (CMSG_SPACE(datalen) < clen) {
  948                                 clen -= CMSG_SPACE(datalen);
  949                                 cm = (struct cmsghdr *)
  950                                         ((caddr_t)cm + CMSG_SPACE(datalen));
  951                         } else {
  952                                 clen = 0;
  953                                 cm = NULL;
  954                         }
  955                 }       
  956                 m = m->m_next;
  957         }
  958 
  959         msg->msg_controllen = (len <= 0) ? maxlen :  ctlbuf - (caddr_t)msg->msg_control;
  960         
  961 exit:
  962         return (error);
  963 
  964 }
  965 
  966 int
  967 freebsd32_recvmsg(td, uap)
  968         struct thread *td;
  969         struct freebsd32_recvmsg_args /* {
  970                 int     s;
  971                 struct  msghdr32 *msg;
  972                 int     flags;
  973         } */ *uap;
  974 {
  975         struct msghdr msg;
  976         struct msghdr32 m32;
  977         struct iovec *uiov, *iov;
  978         struct mbuf *control = NULL;
  979         struct mbuf **controlp;
  980 
  981         int error;
  982         error = copyin(uap->msg, &m32, sizeof(m32));
  983         if (error)
  984                 return (error);
  985         error = freebsd32_copyinmsghdr(uap->msg, &msg);
  986         if (error)
  987                 return (error);
  988         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
  989             EMSGSIZE);
  990         if (error)
  991                 return (error);
  992         msg.msg_flags = uap->flags;
  993         uiov = msg.msg_iov;
  994         msg.msg_iov = iov;
  995 
  996         controlp = (msg.msg_control != NULL) ?  &control : NULL;
  997         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, controlp);
  998         if (error == 0) {
  999                 msg.msg_iov = uiov;
 1000                 
 1001                 if (control != NULL)
 1002                         error = freebsd32_copy_msg_out(&msg, control);
 1003                 
 1004                 if (error == 0)
 1005                         error = freebsd32_copyoutmsghdr(&msg, uap->msg);
 1006         }
 1007         free(iov, M_IOV);
 1008 
 1009         if (control != NULL)
 1010                 m_freem(control);
 1011 
 1012         return (error);
 1013 }
 1014 
 1015 
 1016 static int
 1017 freebsd32_convert_msg_in(struct mbuf **controlp)
 1018 {
 1019         struct mbuf *control = *controlp;
 1020         struct cmsghdr *cm = mtod(control, struct cmsghdr *);
 1021         void *data;
 1022         socklen_t clen = control->m_len, datalen;
 1023         int error;
 1024 
 1025         error = 0;
 1026         *controlp = NULL;
 1027 
 1028         while (cm != NULL) {
 1029                 if (sizeof(struct cmsghdr) > clen || cm->cmsg_len > clen) {
 1030                         error = EINVAL;
 1031                         break;
 1032                 }
 1033 
 1034                 data = FREEBSD32_CMSG_DATA(cm);
 1035                 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
 1036 
 1037                 *controlp = sbcreatecontrol(data, datalen, cm->cmsg_type,
 1038                     cm->cmsg_level);
 1039                 controlp = &(*controlp)->m_next;
 1040 
 1041                 if (FREEBSD32_CMSG_SPACE(datalen) < clen) {
 1042                         clen -= FREEBSD32_CMSG_SPACE(datalen);
 1043                         cm = (struct cmsghdr *)
 1044                                 ((caddr_t)cm + FREEBSD32_CMSG_SPACE(datalen));
 1045                 } else {
 1046                         clen = 0;
 1047                         cm = NULL;
 1048                 }
 1049         }
 1050 
 1051         m_freem(control);
 1052         return (error);
 1053 }
 1054 
 1055 
 1056 int
 1057 freebsd32_sendmsg(struct thread *td,
 1058                   struct freebsd32_sendmsg_args *uap)
 1059 {
 1060         struct msghdr msg;
 1061         struct msghdr32 m32;
 1062         struct iovec *iov;
 1063         struct mbuf *control = NULL;
 1064         struct sockaddr *to = NULL;
 1065         int error;
 1066 
 1067         error = copyin(uap->msg, &m32, sizeof(m32));
 1068         if (error)
 1069                 return (error);
 1070         error = freebsd32_copyinmsghdr(uap->msg, &msg);
 1071         if (error)
 1072                 return (error);
 1073         error = freebsd32_copyiniov(PTRIN(m32.msg_iov), m32.msg_iovlen, &iov,
 1074             EMSGSIZE);
 1075         if (error)
 1076                 return (error);
 1077         msg.msg_iov = iov;
 1078         if (msg.msg_name != NULL) {
 1079                 error = getsockaddr(&to, msg.msg_name, msg.msg_namelen);
 1080                 if (error) {
 1081                         to = NULL;
 1082                         goto out;
 1083                 }
 1084                 msg.msg_name = to;
 1085         }
 1086 
 1087         if (msg.msg_control) {
 1088                 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
 1089                         error = EINVAL;
 1090                         goto out;
 1091                 }
 1092 
 1093                 error = sockargs(&control, msg.msg_control,
 1094                     msg.msg_controllen, MT_CONTROL);
 1095                 if (error)
 1096                         goto out;
 1097                 
 1098                 error = freebsd32_convert_msg_in(&control);
 1099                 if (error)
 1100                         goto out;
 1101         }
 1102 
 1103         error = kern_sendit(td, uap->s, &msg, uap->flags, control,
 1104             UIO_USERSPACE);
 1105 
 1106 out:
 1107         free(iov, M_IOV);
 1108         if (to)
 1109                 free(to, M_SONAME);
 1110         return (error);
 1111 }
 1112 
 1113 int
 1114 freebsd32_recvfrom(struct thread *td,
 1115                    struct freebsd32_recvfrom_args *uap)
 1116 {
 1117         struct msghdr msg;
 1118         struct iovec aiov;
 1119         int error;
 1120 
 1121         if (uap->fromlenaddr) {
 1122                 error = copyin(PTRIN(uap->fromlenaddr), &msg.msg_namelen,
 1123                     sizeof(msg.msg_namelen));
 1124                 if (error)
 1125                         return (error);
 1126         } else {
 1127                 msg.msg_namelen = 0;
 1128         }
 1129 
 1130         msg.msg_name = PTRIN(uap->from);
 1131         msg.msg_iov = &aiov;
 1132         msg.msg_iovlen = 1;
 1133         aiov.iov_base = PTRIN(uap->buf);
 1134         aiov.iov_len = uap->len;
 1135         msg.msg_control = NULL;
 1136         msg.msg_flags = uap->flags;
 1137         error = kern_recvit(td, uap->s, &msg, UIO_USERSPACE, NULL);
 1138         if (error == 0 && uap->fromlenaddr)
 1139                 error = copyout(&msg.msg_namelen, PTRIN(uap->fromlenaddr),
 1140                     sizeof (msg.msg_namelen));
 1141         return (error);
 1142 }
 1143 
 1144 int
 1145 freebsd32_settimeofday(struct thread *td,
 1146                        struct freebsd32_settimeofday_args *uap)
 1147 {
 1148         struct timeval32 tv32;
 1149         struct timeval tv, *tvp;
 1150         struct timezone tz, *tzp;
 1151         int error;
 1152 
 1153         if (uap->tv) {
 1154                 error = copyin(uap->tv, &tv32, sizeof(tv32));
 1155                 if (error)
 1156                         return (error);
 1157                 CP(tv32, tv, tv_sec);
 1158                 CP(tv32, tv, tv_usec);
 1159                 tvp = &tv;
 1160         } else
 1161                 tvp = NULL;
 1162         if (uap->tzp) {
 1163                 error = copyin(uap->tzp, &tz, sizeof(tz));
 1164                 if (error)
 1165                         return (error);
 1166                 tzp = &tz;
 1167         } else
 1168                 tzp = NULL;
 1169         return (kern_settimeofday(td, tvp, tzp));
 1170 }
 1171 
 1172 int
 1173 freebsd32_utimes(struct thread *td, struct freebsd32_utimes_args *uap)
 1174 {
 1175         struct timeval32 s32[2];
 1176         struct timeval s[2], *sp;
 1177         int error;
 1178 
 1179         if (uap->tptr != NULL) {
 1180                 error = copyin(uap->tptr, s32, sizeof(s32));
 1181                 if (error)
 1182                         return (error);
 1183                 CP(s32[0], s[0], tv_sec);
 1184                 CP(s32[0], s[0], tv_usec);
 1185                 CP(s32[1], s[1], tv_sec);
 1186                 CP(s32[1], s[1], tv_usec);
 1187                 sp = s;
 1188         } else
 1189                 sp = NULL;
 1190         return (kern_utimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
 1191 }
 1192 
 1193 int
 1194 freebsd32_lutimes(struct thread *td, struct freebsd32_lutimes_args *uap)
 1195 {
 1196         struct timeval32 s32[2];
 1197         struct timeval s[2], *sp;
 1198         int error;
 1199 
 1200         if (uap->tptr != NULL) {
 1201                 error = copyin(uap->tptr, s32, sizeof(s32));
 1202                 if (error)
 1203                         return (error);
 1204                 CP(s32[0], s[0], tv_sec);
 1205                 CP(s32[0], s[0], tv_usec);
 1206                 CP(s32[1], s[1], tv_sec);
 1207                 CP(s32[1], s[1], tv_usec);
 1208                 sp = s;
 1209         } else
 1210                 sp = NULL;
 1211         return (kern_lutimes(td, uap->path, UIO_USERSPACE, sp, UIO_SYSSPACE));
 1212 }
 1213 
 1214 int
 1215 freebsd32_futimes(struct thread *td, struct freebsd32_futimes_args *uap)
 1216 {
 1217         struct timeval32 s32[2];
 1218         struct timeval s[2], *sp;
 1219         int error;
 1220 
 1221         if (uap->tptr != NULL) {
 1222                 error = copyin(uap->tptr, s32, sizeof(s32));
 1223                 if (error)
 1224                         return (error);
 1225                 CP(s32[0], s[0], tv_sec);
 1226                 CP(s32[0], s[0], tv_usec);
 1227                 CP(s32[1], s[1], tv_sec);
 1228                 CP(s32[1], s[1], tv_usec);
 1229                 sp = s;
 1230         } else
 1231                 sp = NULL;
 1232         return (kern_futimes(td, uap->fd, sp, UIO_SYSSPACE));
 1233 }
 1234 
 1235 
 1236 int
 1237 freebsd32_adjtime(struct thread *td, struct freebsd32_adjtime_args *uap)
 1238 {
 1239         struct timeval32 tv32;
 1240         struct timeval delta, olddelta, *deltap;
 1241         int error;
 1242 
 1243         if (uap->delta) {
 1244                 error = copyin(uap->delta, &tv32, sizeof(tv32));
 1245                 if (error)
 1246                         return (error);
 1247                 CP(tv32, delta, tv_sec);
 1248                 CP(tv32, delta, tv_usec);
 1249                 deltap = &delta;
 1250         } else
 1251                 deltap = NULL;
 1252         error = kern_adjtime(td, deltap, &olddelta);
 1253         if (uap->olddelta && error == 0) {
 1254                 CP(olddelta, tv32, tv_sec);
 1255                 CP(olddelta, tv32, tv_usec);
 1256                 error = copyout(&tv32, uap->olddelta, sizeof(tv32));
 1257         }
 1258         return (error);
 1259 }
 1260 
 1261 #ifdef COMPAT_FREEBSD4
 1262 int
 1263 freebsd4_freebsd32_statfs(struct thread *td, struct freebsd4_freebsd32_statfs_args *uap)
 1264 {
 1265         struct statfs32 s32;
 1266         struct statfs s;
 1267         int error;
 1268 
 1269         error = kern_statfs(td, uap->path, UIO_USERSPACE, &s);
 1270         if (error)
 1271                 return (error);
 1272         copy_statfs(&s, &s32);
 1273         return (copyout(&s32, uap->buf, sizeof(s32)));
 1274 }
 1275 #endif
 1276 
 1277 #ifdef COMPAT_FREEBSD4
 1278 int
 1279 freebsd4_freebsd32_fstatfs(struct thread *td, struct freebsd4_freebsd32_fstatfs_args *uap)
 1280 {
 1281         struct statfs32 s32;
 1282         struct statfs s;
 1283         int error;
 1284 
 1285         error = kern_fstatfs(td, uap->fd, &s);
 1286         if (error)
 1287                 return (error);
 1288         copy_statfs(&s, &s32);
 1289         return (copyout(&s32, uap->buf, sizeof(s32)));
 1290 }
 1291 #endif
 1292 
 1293 #ifdef COMPAT_FREEBSD4
 1294 int
 1295 freebsd4_freebsd32_fhstatfs(struct thread *td, struct freebsd4_freebsd32_fhstatfs_args *uap)
 1296 {
 1297         struct statfs32 s32;
 1298         struct statfs s;
 1299         fhandle_t fh;
 1300         int error;
 1301 
 1302         if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
 1303                 return (error);
 1304         error = kern_fhstatfs(td, fh, &s);
 1305         if (error)
 1306                 return (error);
 1307         copy_statfs(&s, &s32);
 1308         return (copyout(&s32, uap->buf, sizeof(s32)));
 1309 }
 1310 #endif
 1311 
 1312 static void
 1313 freebsd32_ipcperm_in(struct ipc_perm32 *ip32, struct ipc_perm *ip)
 1314 {
 1315 
 1316         CP(*ip32, *ip, cuid);
 1317         CP(*ip32, *ip, cgid);
 1318         CP(*ip32, *ip, uid);
 1319         CP(*ip32, *ip, gid);
 1320         CP(*ip32, *ip, mode);
 1321         CP(*ip32, *ip, seq);
 1322         CP(*ip32, *ip, key);
 1323 }
 1324 
 1325 static void
 1326 freebsd32_ipcperm_out(struct ipc_perm *ip, struct ipc_perm32 *ip32)
 1327 {
 1328 
 1329         CP(*ip, *ip32, cuid);
 1330         CP(*ip, *ip32, cgid);
 1331         CP(*ip, *ip32, uid);
 1332         CP(*ip, *ip32, gid);
 1333         CP(*ip, *ip32, mode);
 1334         CP(*ip, *ip32, seq);
 1335         CP(*ip, *ip32, key);
 1336 }
 1337 
 1338 int
 1339 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
 1340 {
 1341 
 1342         switch (uap->which) {
 1343         case 0:
 1344                 return (freebsd32_semctl(td,
 1345                     (struct freebsd32_semctl_args *)&uap->a2));
 1346         default:
 1347                 return (semsys(td, (struct semsys_args *)uap));
 1348         }
 1349 }
 1350 
 1351 int
 1352 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
 1353 {
 1354         struct semid_ds32 dsbuf32;
 1355         struct semid_ds dsbuf;
 1356         union semun semun;
 1357         union semun32 arg;
 1358         register_t rval;
 1359         int error;
 1360 
 1361         switch (uap->cmd) {
 1362         case SEM_STAT:
 1363         case IPC_SET:
 1364         case IPC_STAT:
 1365         case GETALL:
 1366         case SETVAL:
 1367         case SETALL:
 1368                 error = copyin(uap->arg, &arg, sizeof(arg));
 1369                 if (error)
 1370                         return (error);         
 1371                 break;
 1372         }
 1373 
 1374         switch (uap->cmd) {
 1375         case SEM_STAT:
 1376         case IPC_STAT:
 1377                 semun.buf = &dsbuf;
 1378                 break;
 1379         case IPC_SET:
 1380                 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
 1381                 if (error)
 1382                         return (error);
 1383                 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
 1384                 PTRIN_CP(dsbuf32, dsbuf, sem_base);
 1385                 CP(dsbuf32, dsbuf, sem_nsems);
 1386                 CP(dsbuf32, dsbuf, sem_otime);
 1387                 CP(dsbuf32, dsbuf, sem_pad1);
 1388                 CP(dsbuf32, dsbuf, sem_ctime);
 1389                 CP(dsbuf32, dsbuf, sem_pad2);
 1390                 CP(dsbuf32, dsbuf, sem_pad3[0]);
 1391                 CP(dsbuf32, dsbuf, sem_pad3[1]);
 1392                 CP(dsbuf32, dsbuf, sem_pad3[2]);
 1393                 CP(dsbuf32, dsbuf, sem_pad3[3]);
 1394                 semun.buf = &dsbuf;
 1395                 break;
 1396         case GETALL:
 1397         case SETALL:
 1398                 semun.array = PTRIN(arg.array);
 1399                 break;
 1400         case SETVAL:
 1401                 semun.val = arg.val;
 1402                 break;          
 1403         }
 1404 
 1405         error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
 1406             &rval);
 1407         if (error)
 1408                 return (error);
 1409 
 1410         switch (uap->cmd) {
 1411         case SEM_STAT:
 1412         case IPC_STAT:
 1413                 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
 1414                 PTROUT_CP(dsbuf, dsbuf32, sem_base);
 1415                 CP(dsbuf, dsbuf32, sem_nsems);
 1416                 CP(dsbuf, dsbuf32, sem_otime);
 1417                 CP(dsbuf, dsbuf32, sem_pad1);
 1418                 CP(dsbuf, dsbuf32, sem_ctime);
 1419                 CP(dsbuf, dsbuf32, sem_pad2);
 1420                 CP(dsbuf, dsbuf32, sem_pad3[0]);
 1421                 CP(dsbuf, dsbuf32, sem_pad3[1]);
 1422                 CP(dsbuf, dsbuf32, sem_pad3[2]);
 1423                 CP(dsbuf, dsbuf32, sem_pad3[3]);
 1424                 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
 1425                 break;
 1426         }
 1427 
 1428         if (error == 0)
 1429                 td->td_retval[0] = rval;
 1430         return (error);
 1431 }
 1432 
 1433 int
 1434 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
 1435 {
 1436 
 1437         switch (uap->which) {
 1438         case 0:
 1439                 return (freebsd32_msgctl(td,
 1440                     (struct freebsd32_msgctl_args *)&uap->a2));
 1441         case 2:
 1442                 return (freebsd32_msgsnd(td,
 1443                     (struct freebsd32_msgsnd_args *)&uap->a2));
 1444         case 3:
 1445                 return (freebsd32_msgrcv(td,
 1446                     (struct freebsd32_msgrcv_args *)&uap->a2));
 1447         default:
 1448                 return (msgsys(td, (struct msgsys_args *)uap));
 1449         }
 1450 }
 1451 
 1452 int
 1453 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
 1454 {
 1455         struct msqid_ds msqbuf;
 1456         struct msqid_ds32 msqbuf32;
 1457         int error;
 1458 
 1459         if (uap->cmd == IPC_SET) {
 1460                 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
 1461                 if (error)
 1462                         return (error);
 1463                 freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
 1464                 PTRIN_CP(msqbuf32, msqbuf, msg_first);
 1465                 PTRIN_CP(msqbuf32, msqbuf, msg_last);
 1466                 CP(msqbuf32, msqbuf, msg_cbytes);
 1467                 CP(msqbuf32, msqbuf, msg_qnum);
 1468                 CP(msqbuf32, msqbuf, msg_qbytes);
 1469                 CP(msqbuf32, msqbuf, msg_lspid);
 1470                 CP(msqbuf32, msqbuf, msg_lrpid);
 1471                 CP(msqbuf32, msqbuf, msg_stime);
 1472                 CP(msqbuf32, msqbuf, msg_pad1);
 1473                 CP(msqbuf32, msqbuf, msg_rtime);
 1474                 CP(msqbuf32, msqbuf, msg_pad2);
 1475                 CP(msqbuf32, msqbuf, msg_ctime);
 1476                 CP(msqbuf32, msqbuf, msg_pad3);
 1477                 CP(msqbuf32, msqbuf, msg_pad4[0]);
 1478                 CP(msqbuf32, msqbuf, msg_pad4[1]);
 1479                 CP(msqbuf32, msqbuf, msg_pad4[2]);
 1480                 CP(msqbuf32, msqbuf, msg_pad4[3]);
 1481         }
 1482         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
 1483         if (error)
 1484                 return (error);
 1485         if (uap->cmd == IPC_STAT) {
 1486                 freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
 1487                 PTROUT_CP(msqbuf, msqbuf32, msg_first);
 1488                 PTROUT_CP(msqbuf, msqbuf32, msg_last);
 1489                 CP(msqbuf, msqbuf32, msg_cbytes);
 1490                 CP(msqbuf, msqbuf32, msg_qnum);
 1491                 CP(msqbuf, msqbuf32, msg_qbytes);
 1492                 CP(msqbuf, msqbuf32, msg_lspid);
 1493                 CP(msqbuf, msqbuf32, msg_lrpid);
 1494                 CP(msqbuf, msqbuf32, msg_stime);
 1495                 CP(msqbuf, msqbuf32, msg_pad1);
 1496                 CP(msqbuf, msqbuf32, msg_rtime);
 1497                 CP(msqbuf, msqbuf32, msg_pad2);
 1498                 CP(msqbuf, msqbuf32, msg_ctime);
 1499                 CP(msqbuf, msqbuf32, msg_pad3);
 1500                 CP(msqbuf, msqbuf32, msg_pad4[0]);
 1501                 CP(msqbuf, msqbuf32, msg_pad4[1]);
 1502                 CP(msqbuf, msqbuf32, msg_pad4[2]);
 1503                 CP(msqbuf, msqbuf32, msg_pad4[3]);
 1504                 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
 1505         }
 1506         return (error);
 1507 }
 1508 
 1509 int
 1510 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
 1511 {
 1512         const void *msgp;
 1513         long mtype;
 1514         int32_t mtype32;
 1515         int error;
 1516 
 1517         msgp = PTRIN(uap->msgp);
 1518         if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
 1519                 return (error);
 1520         mtype = mtype32;
 1521         return (kern_msgsnd(td, uap->msqid,
 1522             (const char *)msgp + sizeof(mtype32),
 1523             uap->msgsz, uap->msgflg, mtype));
 1524 }
 1525 
 1526 int
 1527 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
 1528 {
 1529         void *msgp;
 1530         long mtype;
 1531         int32_t mtype32;
 1532         int error;
 1533 
 1534         msgp = PTRIN(uap->msgp);
 1535         if ((error = kern_msgrcv(td, uap->msqid,
 1536             (char *)msgp + sizeof(mtype32), uap->msgsz,
 1537             uap->msgtyp, uap->msgflg, &mtype)) != 0)
 1538                 return (error);
 1539         mtype32 = (int32_t)mtype;
 1540         return (copyout(&mtype32, msgp, sizeof(mtype32)));
 1541 }
 1542 
 1543 int
 1544 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
 1545 {
 1546 
 1547         switch (uap->which) {
 1548         case 0: {       /* shmat */
 1549                 struct shmat_args ap;
 1550 
 1551                 ap.shmid = uap->a2;
 1552                 ap.shmaddr = PTRIN(uap->a3);
 1553                 ap.shmflg = uap->a4;
 1554                 return (sysent[SYS_shmat].sy_call(td, &ap));
 1555         }
 1556         case 2: {       /* shmdt */
 1557                 struct shmdt_args ap;
 1558 
 1559                 ap.shmaddr = PTRIN(uap->a2);
 1560                 return (sysent[SYS_shmdt].sy_call(td, &ap));
 1561         }
 1562         case 3: {       /* shmget */
 1563                 struct shmget_args ap;
 1564 
 1565                 ap.key = uap->a2;
 1566                 ap.size = uap->a3;
 1567                 ap.shmflg = uap->a4;
 1568                 return (sysent[SYS_shmget].sy_call(td, &ap));
 1569         }
 1570         case 4: {       /* shmctl */
 1571                 struct freebsd32_shmctl_args ap;
 1572 
 1573                 ap.shmid = uap->a2;
 1574                 ap.cmd = uap->a3;
 1575                 ap.buf = PTRIN(uap->a4);
 1576                 return (freebsd32_shmctl(td, &ap));
 1577         }
 1578         case 1:         /* oshmctl */
 1579         default:
 1580                 return (EINVAL);
 1581         }
 1582 }
 1583 
 1584 int
 1585 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
 1586 {
 1587         int error = 0;
 1588         union {
 1589                 struct shmid_ds shmid_ds;
 1590                 struct shm_info shm_info;
 1591                 struct shminfo shminfo;
 1592         } u;
 1593         union {
 1594                 struct shmid_ds32 shmid_ds32;
 1595                 struct shm_info32 shm_info32;
 1596                 struct shminfo32 shminfo32;
 1597         } u32;
 1598         size_t sz;
 1599         
 1600         if (uap->cmd == IPC_SET) {
 1601                 if ((error = copyin(uap->buf, &u32.shmid_ds32,
 1602                     sizeof(u32.shmid_ds32))))
 1603                         goto done;
 1604                 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
 1605                     &u.shmid_ds.shm_perm);
 1606                 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
 1607                 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
 1608                 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
 1609                 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
 1610                 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
 1611                 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
 1612                 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
 1613                 PTRIN_CP(u32.shmid_ds32, u.shmid_ds, shm_internal);
 1614         }
 1615         
 1616         error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
 1617         if (error)
 1618                 goto done;
 1619         
 1620         /* Cases in which we need to copyout */
 1621         switch (uap->cmd) {
 1622         case IPC_INFO:
 1623                 CP(u.shminfo, u32.shminfo32, shmmax);
 1624                 CP(u.shminfo, u32.shminfo32, shmmin);
 1625                 CP(u.shminfo, u32.shminfo32, shmmni);
 1626                 CP(u.shminfo, u32.shminfo32, shmseg);
 1627                 CP(u.shminfo, u32.shminfo32, shmall);
 1628                 error = copyout(&u32.shminfo32, uap->buf,
 1629                     sizeof(u32.shminfo32));
 1630                 break;
 1631         case SHM_INFO:
 1632                 CP(u.shm_info, u32.shm_info32, used_ids);
 1633                 CP(u.shm_info, u32.shm_info32, shm_rss);
 1634                 CP(u.shm_info, u32.shm_info32, shm_tot);
 1635                 CP(u.shm_info, u32.shm_info32, shm_swp);
 1636                 CP(u.shm_info, u32.shm_info32, swap_attempts);
 1637                 CP(u.shm_info, u32.shm_info32, swap_successes);
 1638                 error = copyout(&u32.shm_info32, uap->buf,
 1639                     sizeof(u32.shm_info32));
 1640                 break;
 1641         case SHM_STAT:
 1642         case IPC_STAT:
 1643                 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
 1644                     &u32.shmid_ds32.shm_perm);
 1645                 CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
 1646                 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
 1647                 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
 1648                 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
 1649                 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
 1650                 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
 1651                 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
 1652                 PTROUT_CP(u.shmid_ds, u32.shmid_ds32, shm_internal);
 1653                 error = copyout(&u32.shmid_ds32, uap->buf,
 1654                     sizeof(u32.shmid_ds32));
 1655                 break;
 1656         }
 1657 
 1658 done:
 1659         if (error) {
 1660                 /* Invalidate the return value */
 1661                 td->td_retval[0] = -1;
 1662         }
 1663         return (error);
 1664 }
 1665 
 1666 int
 1667 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
 1668 {
 1669         struct pread_args ap;
 1670 
 1671         ap.fd = uap->fd;
 1672         ap.buf = uap->buf;
 1673         ap.nbyte = uap->nbyte;
 1674         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1675         return (pread(td, &ap));
 1676 }
 1677 
 1678 int
 1679 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
 1680 {
 1681         struct pwrite_args ap;
 1682 
 1683         ap.fd = uap->fd;
 1684         ap.buf = uap->buf;
 1685         ap.nbyte = uap->nbyte;
 1686         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1687         return (pwrite(td, &ap));
 1688 }
 1689 
 1690 int
 1691 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
 1692 {
 1693         int error;
 1694         struct lseek_args ap;
 1695         off_t pos;
 1696 
 1697         ap.fd = uap->fd;
 1698         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1699         ap.whence = uap->whence;
 1700         error = lseek(td, &ap);
 1701         /* Expand the quad return into two parts for eax and edx */
 1702         pos = *(off_t *)(td->td_retval);
 1703         td->td_retval[0] = pos & 0xffffffff;    /* %eax */
 1704         td->td_retval[1] = pos >> 32;           /* %edx */
 1705         return error;
 1706 }
 1707 
 1708 int
 1709 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
 1710 {
 1711         struct truncate_args ap;
 1712 
 1713         ap.path = uap->path;
 1714         ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
 1715         return (truncate(td, &ap));
 1716 }
 1717 
 1718 int
 1719 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
 1720 {
 1721         struct ftruncate_args ap;
 1722 
 1723         ap.fd = uap->fd;
 1724         ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
 1725         return (ftruncate(td, &ap));
 1726 }
 1727 
 1728 int
 1729 freebsd32_getdirentries(struct thread *td,
 1730     struct freebsd32_getdirentries_args *uap)
 1731 {
 1732         long base;
 1733         int32_t base32;
 1734         int error;
 1735 
 1736         error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
 1737         if (error)
 1738                 return (error);
 1739         if (uap->basep != NULL) {
 1740                 base32 = base;
 1741                 error = copyout(&base32, uap->basep, sizeof(int32_t));
 1742         }
 1743         return (error);
 1744 }
 1745 
 1746 #ifdef COMPAT_FREEBSD6
 1747 /* versions with the 'int pad' argument */
 1748 int
 1749 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
 1750 {
 1751         struct pread_args ap;
 1752 
 1753         ap.fd = uap->fd;
 1754         ap.buf = uap->buf;
 1755         ap.nbyte = uap->nbyte;
 1756         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1757         return (pread(td, &ap));
 1758 }
 1759 
 1760 int
 1761 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
 1762 {
 1763         struct pwrite_args ap;
 1764 
 1765         ap.fd = uap->fd;
 1766         ap.buf = uap->buf;
 1767         ap.nbyte = uap->nbyte;
 1768         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1769         return (pwrite(td, &ap));
 1770 }
 1771 
 1772 int
 1773 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
 1774 {
 1775         int error;
 1776         struct lseek_args ap;
 1777         off_t pos;
 1778 
 1779         ap.fd = uap->fd;
 1780         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1781         ap.whence = uap->whence;
 1782         error = lseek(td, &ap);
 1783         /* Expand the quad return into two parts for eax and edx */
 1784         pos = *(off_t *)(td->td_retval);
 1785         td->td_retval[0] = pos & 0xffffffff;    /* %eax */
 1786         td->td_retval[1] = pos >> 32;           /* %edx */
 1787         return error;
 1788 }
 1789 
 1790 int
 1791 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
 1792 {
 1793         struct truncate_args ap;
 1794 
 1795         ap.path = uap->path;
 1796         ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
 1797         return (truncate(td, &ap));
 1798 }
 1799 
 1800 int
 1801 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
 1802 {
 1803         struct ftruncate_args ap;
 1804 
 1805         ap.fd = uap->fd;
 1806         ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
 1807         return (ftruncate(td, &ap));
 1808 }
 1809 #endif /* COMPAT_FREEBSD6 */
 1810 
 1811 struct sf_hdtr32 {
 1812         uint32_t headers;
 1813         int hdr_cnt;
 1814         uint32_t trailers;
 1815         int trl_cnt;
 1816 };
 1817 
 1818 static int
 1819 freebsd32_do_sendfile(struct thread *td,
 1820     struct freebsd32_sendfile_args *uap, int compat)
 1821 {
 1822         struct sendfile_args ap;
 1823         struct sf_hdtr32 hdtr32;
 1824         struct sf_hdtr hdtr;
 1825         struct uio *hdr_uio, *trl_uio;
 1826         struct iovec32 *iov32;
 1827         int error;
 1828 
 1829         hdr_uio = trl_uio = NULL;
 1830 
 1831         ap.fd = uap->fd;
 1832         ap.s = uap->s;
 1833         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1834         ap.nbytes = uap->nbytes;
 1835         ap.hdtr = (struct sf_hdtr *)uap->hdtr;          /* XXX not used */
 1836         ap.sbytes = uap->sbytes;
 1837         ap.flags = uap->flags;
 1838 
 1839         if (uap->hdtr != NULL) {
 1840                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
 1841                 if (error)
 1842                         goto out;
 1843                 PTRIN_CP(hdtr32, hdtr, headers);
 1844                 CP(hdtr32, hdtr, hdr_cnt);
 1845                 PTRIN_CP(hdtr32, hdtr, trailers);
 1846                 CP(hdtr32, hdtr, trl_cnt);
 1847 
 1848                 if (hdtr.headers != NULL) {
 1849                         iov32 = PTRIN(hdtr32.headers);
 1850                         error = freebsd32_copyinuio(iov32,
 1851                             hdtr32.hdr_cnt, &hdr_uio);
 1852                         if (error)
 1853                                 goto out;
 1854                 }
 1855                 if (hdtr.trailers != NULL) {
 1856                         iov32 = PTRIN(hdtr32.trailers);
 1857                         error = freebsd32_copyinuio(iov32,
 1858                             hdtr32.trl_cnt, &trl_uio);
 1859                         if (error)
 1860                                 goto out;
 1861                 }
 1862         }
 1863 
 1864         error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
 1865 out:
 1866         if (hdr_uio)
 1867                 free(hdr_uio, M_IOV);
 1868         if (trl_uio)
 1869                 free(trl_uio, M_IOV);
 1870         return (error);
 1871 }
 1872 
 1873 #ifdef COMPAT_FREEBSD4
 1874 int
 1875 freebsd4_freebsd32_sendfile(struct thread *td,
 1876     struct freebsd4_freebsd32_sendfile_args *uap)
 1877 {
 1878         return (freebsd32_do_sendfile(td,
 1879             (struct freebsd32_sendfile_args *)uap, 1));
 1880 }
 1881 #endif
 1882 
 1883 int
 1884 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
 1885 {
 1886 
 1887         return (freebsd32_do_sendfile(td, uap, 0));
 1888 }
 1889 
 1890 static void
 1891 copy_stat( struct stat *in, struct stat32 *out)
 1892 {
 1893         CP(*in, *out, st_dev);
 1894         CP(*in, *out, st_ino);
 1895         CP(*in, *out, st_mode);
 1896         CP(*in, *out, st_nlink);
 1897         CP(*in, *out, st_uid);
 1898         CP(*in, *out, st_gid);
 1899         CP(*in, *out, st_rdev);
 1900         TS_CP(*in, *out, st_atimespec);
 1901         TS_CP(*in, *out, st_mtimespec);
 1902         TS_CP(*in, *out, st_ctimespec);
 1903         CP(*in, *out, st_size);
 1904         CP(*in, *out, st_blocks);
 1905         CP(*in, *out, st_blksize);
 1906         CP(*in, *out, st_flags);
 1907         CP(*in, *out, st_gen);
 1908 }
 1909 
 1910 int
 1911 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
 1912 {
 1913         struct stat sb;
 1914         struct stat32 sb32;
 1915         int error;
 1916 
 1917         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
 1918         if (error)
 1919                 return (error);
 1920         copy_stat(&sb, &sb32);
 1921         error = copyout(&sb32, uap->ub, sizeof (sb32));
 1922         return (error);
 1923 }
 1924 
 1925 int
 1926 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
 1927 {
 1928         struct stat ub;
 1929         struct stat32 ub32;
 1930         int error;
 1931 
 1932         error = kern_fstat(td, uap->fd, &ub);
 1933         if (error)
 1934                 return (error);
 1935         copy_stat(&ub, &ub32);
 1936         error = copyout(&ub32, uap->ub, sizeof(ub32));
 1937         return (error);
 1938 }
 1939 
 1940 int
 1941 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
 1942 {
 1943         struct stat sb;
 1944         struct stat32 sb32;
 1945         int error;
 1946 
 1947         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
 1948         if (error)
 1949                 return (error);
 1950         copy_stat(&sb, &sb32);
 1951         error = copyout(&sb32, uap->ub, sizeof (sb32));
 1952         return (error);
 1953 }
 1954 
 1955 /*
 1956  * MPSAFE
 1957  */
 1958 int
 1959 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
 1960 {
 1961         int error, name[CTL_MAXNAME];
 1962         size_t j, oldlen;
 1963 
 1964         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
 1965                 return (EINVAL);
 1966         error = copyin(uap->name, name, uap->namelen * sizeof(int));
 1967         if (error)
 1968                 return (error);
 1969         if (uap->oldlenp)
 1970                 oldlen = fuword32(uap->oldlenp);
 1971         else
 1972                 oldlen = 0;
 1973         error = userland_sysctl(td, name, uap->namelen,
 1974                 uap->old, &oldlen, 1,
 1975                 uap->new, uap->newlen, &j, SCTL_MASK32);
 1976         if (error && error != ENOMEM)
 1977                 return (error);
 1978         if (uap->oldlenp)
 1979                 suword32(uap->oldlenp, j);
 1980         return (0);
 1981 }
 1982 
 1983 int
 1984 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
 1985 {
 1986         uint32_t version;
 1987         int error;
 1988         struct jail j;
 1989 
 1990         error = copyin(uap->jail, &version, sizeof(uint32_t));
 1991         if (error)
 1992                 return (error);
 1993         switch (version) {
 1994         case 0: 
 1995         {
 1996                 /* FreeBSD single IPv4 jails. */
 1997                 struct jail32_v0 j32_v0;
 1998 
 1999                 bzero(&j, sizeof(struct jail));
 2000                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
 2001                 if (error)
 2002                         return (error);
 2003                 CP(j32_v0, j, version);
 2004                 PTRIN_CP(j32_v0, j, path);
 2005                 PTRIN_CP(j32_v0, j, hostname);
 2006                 j.ip4s = j32_v0.ip_number;
 2007                 break;
 2008         }
 2009 
 2010         case 1:
 2011                 /*
 2012                  * Version 1 was used by multi-IPv4 jail implementations
 2013                  * that never made it into the official kernel.
 2014                  */
 2015                 return (EINVAL);
 2016 
 2017         case 2: /* JAIL_API_VERSION */
 2018         {
 2019                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
 2020                 struct jail32 j32;
 2021 
 2022                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
 2023                 if (error)
 2024                         return (error);
 2025                 CP(j32, j, version);
 2026                 PTRIN_CP(j32, j, path);
 2027                 PTRIN_CP(j32, j, hostname);
 2028                 PTRIN_CP(j32, j, jailname);
 2029                 CP(j32, j, ip4s);
 2030                 CP(j32, j, ip6s);
 2031                 PTRIN_CP(j32, j, ip4);
 2032                 PTRIN_CP(j32, j, ip6);
 2033                 break;
 2034         }
 2035 
 2036         default:
 2037                 /* Sci-Fi jails are not supported, sorry. */
 2038                 return (EINVAL);
 2039         }
 2040         return (kern_jail(td, &j));
 2041 }
 2042 
 2043 int
 2044 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
 2045 {
 2046         struct sigaction32 s32;
 2047         struct sigaction sa, osa, *sap;
 2048         int error;
 2049 
 2050         if (uap->act) {
 2051                 error = copyin(uap->act, &s32, sizeof(s32));
 2052                 if (error)
 2053                         return (error);
 2054                 sa.sa_handler = PTRIN(s32.sa_u);
 2055                 CP(s32, sa, sa_flags);
 2056                 CP(s32, sa, sa_mask);
 2057                 sap = &sa;
 2058         } else
 2059                 sap = NULL;
 2060         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
 2061         if (error == 0 && uap->oact != NULL) {
 2062                 s32.sa_u = PTROUT(osa.sa_handler);
 2063                 CP(osa, s32, sa_flags);
 2064                 CP(osa, s32, sa_mask);
 2065                 error = copyout(&s32, uap->oact, sizeof(s32));
 2066         }
 2067         return (error);
 2068 }
 2069 
 2070 #ifdef COMPAT_FREEBSD4
 2071 int
 2072 freebsd4_freebsd32_sigaction(struct thread *td,
 2073                              struct freebsd4_freebsd32_sigaction_args *uap)
 2074 {
 2075         struct sigaction32 s32;
 2076         struct sigaction sa, osa, *sap;
 2077         int error;
 2078 
 2079         if (uap->act) {
 2080                 error = copyin(uap->act, &s32, sizeof(s32));
 2081                 if (error)
 2082                         return (error);
 2083                 sa.sa_handler = PTRIN(s32.sa_u);
 2084                 CP(s32, sa, sa_flags);
 2085                 CP(s32, sa, sa_mask);
 2086                 sap = &sa;
 2087         } else
 2088                 sap = NULL;
 2089         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
 2090         if (error == 0 && uap->oact != NULL) {
 2091                 s32.sa_u = PTROUT(osa.sa_handler);
 2092                 CP(osa, s32, sa_flags);
 2093                 CP(osa, s32, sa_mask);
 2094                 error = copyout(&s32, uap->oact, sizeof(s32));
 2095         }
 2096         return (error);
 2097 }
 2098 #endif
 2099 
 2100 #ifdef COMPAT_43
 2101 struct osigaction32 {
 2102         u_int32_t       sa_u;
 2103         osigset_t       sa_mask;
 2104         int             sa_flags;
 2105 };
 2106 
 2107 #define ONSIG   32
 2108 
 2109 int
 2110 ofreebsd32_sigaction(struct thread *td,
 2111                              struct ofreebsd32_sigaction_args *uap)
 2112 {
 2113         struct osigaction32 s32;
 2114         struct sigaction sa, osa, *sap;
 2115         int error;
 2116 
 2117         if (uap->signum <= 0 || uap->signum >= ONSIG)
 2118                 return (EINVAL);
 2119 
 2120         if (uap->nsa) {
 2121                 error = copyin(uap->nsa, &s32, sizeof(s32));
 2122                 if (error)
 2123                         return (error);
 2124                 sa.sa_handler = PTRIN(s32.sa_u);
 2125                 CP(s32, sa, sa_flags);
 2126                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
 2127                 sap = &sa;
 2128         } else
 2129                 sap = NULL;
 2130         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
 2131         if (error == 0 && uap->osa != NULL) {
 2132                 s32.sa_u = PTROUT(osa.sa_handler);
 2133                 CP(osa, s32, sa_flags);
 2134                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
 2135                 error = copyout(&s32, uap->osa, sizeof(s32));
 2136         }
 2137         return (error);
 2138 }
 2139 
 2140 int
 2141 ofreebsd32_sigprocmask(struct thread *td,
 2142                                struct ofreebsd32_sigprocmask_args *uap)
 2143 {
 2144         sigset_t set, oset;
 2145         int error;
 2146 
 2147         OSIG2SIG(uap->mask, set);
 2148         error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
 2149         SIG2OSIG(oset, td->td_retval[0]);
 2150         return (error);
 2151 }
 2152 
 2153 int
 2154 ofreebsd32_sigpending(struct thread *td,
 2155                               struct ofreebsd32_sigpending_args *uap)
 2156 {
 2157         struct proc *p = td->td_proc;
 2158         sigset_t siglist;
 2159 
 2160         PROC_LOCK(p);
 2161         siglist = p->p_siglist;
 2162         SIGSETOR(siglist, td->td_siglist);
 2163         PROC_UNLOCK(p);
 2164         SIG2OSIG(siglist, td->td_retval[0]);
 2165         return (0);
 2166 }
 2167 
 2168 struct sigvec32 {
 2169         u_int32_t       sv_handler;
 2170         int             sv_mask;
 2171         int             sv_flags;
 2172 };
 2173 
 2174 int
 2175 ofreebsd32_sigvec(struct thread *td,
 2176                           struct ofreebsd32_sigvec_args *uap)
 2177 {
 2178         struct sigvec32 vec;
 2179         struct sigaction sa, osa, *sap;
 2180         int error;
 2181 
 2182         if (uap->signum <= 0 || uap->signum >= ONSIG)
 2183                 return (EINVAL);
 2184 
 2185         if (uap->nsv) {
 2186                 error = copyin(uap->nsv, &vec, sizeof(vec));
 2187                 if (error)
 2188                         return (error);
 2189                 sa.sa_handler = PTRIN(vec.sv_handler);
 2190                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
 2191                 sa.sa_flags = vec.sv_flags;
 2192                 sa.sa_flags ^= SA_RESTART;
 2193                 sap = &sa;
 2194         } else
 2195                 sap = NULL;
 2196         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
 2197         if (error == 0 && uap->osv != NULL) {
 2198                 vec.sv_handler = PTROUT(osa.sa_handler);
 2199                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
 2200                 vec.sv_flags = osa.sa_flags;
 2201                 vec.sv_flags &= ~SA_NOCLDWAIT;
 2202                 vec.sv_flags ^= SA_RESTART;
 2203                 error = copyout(&vec, uap->osv, sizeof(vec));
 2204         }
 2205         return (error);
 2206 }
 2207 
 2208 int
 2209 ofreebsd32_sigblock(struct thread *td,
 2210                             struct ofreebsd32_sigblock_args *uap)
 2211 {
 2212         struct proc *p = td->td_proc;
 2213         sigset_t set;
 2214 
 2215         OSIG2SIG(uap->mask, set);
 2216         SIG_CANTMASK(set);
 2217         PROC_LOCK(p);
 2218         SIG2OSIG(td->td_sigmask, td->td_retval[0]);
 2219         SIGSETOR(td->td_sigmask, set);
 2220         PROC_UNLOCK(p);
 2221         return (0);
 2222 }
 2223 
 2224 int
 2225 ofreebsd32_sigsetmask(struct thread *td,
 2226                               struct ofreebsd32_sigsetmask_args *uap)
 2227 {
 2228         struct proc *p = td->td_proc;
 2229         sigset_t set;
 2230 
 2231         OSIG2SIG(uap->mask, set);
 2232         SIG_CANTMASK(set);
 2233         PROC_LOCK(p);
 2234         SIG2OSIG(td->td_sigmask, td->td_retval[0]);
 2235         SIGSETLO(td->td_sigmask, set);
 2236         signotify(td);
 2237         PROC_UNLOCK(p);
 2238         return (0);
 2239 }
 2240 
 2241 int
 2242 ofreebsd32_sigsuspend(struct thread *td,
 2243                               struct ofreebsd32_sigsuspend_args *uap)
 2244 {
 2245         struct proc *p = td->td_proc;
 2246         sigset_t mask;
 2247 
 2248         PROC_LOCK(p);
 2249         td->td_oldsigmask = td->td_sigmask;
 2250         td->td_pflags |= TDP_OLDMASK;
 2251         OSIG2SIG(uap->mask, mask);
 2252         SIG_CANTMASK(mask);
 2253         SIGSETLO(td->td_sigmask, mask);
 2254         signotify(td);
 2255         while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
 2256                 /* void */;
 2257         PROC_UNLOCK(p);
 2258         /* always return EINTR rather than ERESTART... */
 2259         return (EINTR);
 2260 }
 2261 
 2262 struct sigstack32 {
 2263         u_int32_t       ss_sp;
 2264         int             ss_onstack;
 2265 };
 2266 
 2267 int
 2268 ofreebsd32_sigstack(struct thread *td,
 2269                             struct ofreebsd32_sigstack_args *uap)
 2270 {
 2271         struct sigstack32 s32;
 2272         struct sigstack nss, oss;
 2273         int error = 0, unss;
 2274 
 2275         if (uap->nss != NULL) {
 2276                 error = copyin(uap->nss, &s32, sizeof(s32));
 2277                 if (error)
 2278                         return (error);
 2279                 nss.ss_sp = PTRIN(s32.ss_sp);
 2280                 CP(s32, nss, ss_onstack);
 2281                 unss = 1;
 2282         } else {
 2283                 unss = 0;
 2284         }
 2285         oss.ss_sp = td->td_sigstk.ss_sp;
 2286         oss.ss_onstack = sigonstack(cpu_getstack(td));
 2287         if (unss) {
 2288                 td->td_sigstk.ss_sp = nss.ss_sp;
 2289                 td->td_sigstk.ss_size = 0;
 2290                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
 2291                 td->td_pflags |= TDP_ALTSTACK;
 2292         }
 2293         if (uap->oss != NULL) {
 2294                 s32.ss_sp = PTROUT(oss.ss_sp);
 2295                 CP(oss, s32, ss_onstack);
 2296                 error = copyout(&s32, uap->oss, sizeof(s32));
 2297         }
 2298         return (error);
 2299 }
 2300 #endif
 2301 
 2302 int
 2303 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
 2304 {
 2305         struct timespec32 rmt32, rqt32;
 2306         struct timespec rmt, rqt;
 2307         int error;
 2308 
 2309         error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
 2310         if (error)
 2311                 return (error);
 2312 
 2313         CP(rqt32, rqt, tv_sec);
 2314         CP(rqt32, rqt, tv_nsec);
 2315 
 2316         if (uap->rmtp &&
 2317             !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
 2318                 return (EFAULT);
 2319         error = kern_nanosleep(td, &rqt, &rmt);
 2320         if (error && uap->rmtp) {
 2321                 int error2;
 2322 
 2323                 CP(rmt, rmt32, tv_sec);
 2324                 CP(rmt, rmt32, tv_nsec);
 2325 
 2326                 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
 2327                 if (error2)
 2328                         error = error2;
 2329         }
 2330         return (error);
 2331 }
 2332 
 2333 int
 2334 freebsd32_clock_gettime(struct thread *td,
 2335                         struct freebsd32_clock_gettime_args *uap)
 2336 {
 2337         struct timespec ats;
 2338         struct timespec32 ats32;
 2339         int error;
 2340 
 2341         error = kern_clock_gettime(td, uap->clock_id, &ats);
 2342         if (error == 0) {
 2343                 CP(ats, ats32, tv_sec);
 2344                 CP(ats, ats32, tv_nsec);
 2345                 error = copyout(&ats32, uap->tp, sizeof(ats32));
 2346         }
 2347         return (error);
 2348 }
 2349 
 2350 int
 2351 freebsd32_clock_settime(struct thread *td,
 2352                         struct freebsd32_clock_settime_args *uap)
 2353 {
 2354         struct timespec ats;
 2355         struct timespec32 ats32;
 2356         int error;
 2357 
 2358         error = copyin(uap->tp, &ats32, sizeof(ats32));
 2359         if (error)
 2360                 return (error);
 2361         CP(ats32, ats, tv_sec);
 2362         CP(ats32, ats, tv_nsec);
 2363 
 2364         return (kern_clock_settime(td, uap->clock_id, &ats));
 2365 }
 2366 
 2367 int
 2368 freebsd32_clock_getres(struct thread *td,
 2369                        struct freebsd32_clock_getres_args *uap)
 2370 {
 2371         struct timespec ts;
 2372         struct timespec32 ts32;
 2373         int error;
 2374 
 2375         if (uap->tp == NULL)
 2376                 return (0);
 2377         error = kern_clock_getres(td, uap->clock_id, &ts);
 2378         if (error == 0) {
 2379                 CP(ts, ts32, tv_sec);
 2380                 CP(ts, ts32, tv_nsec);
 2381                 error = copyout(&ts32, uap->tp, sizeof(ts32));
 2382         }
 2383         return (error);
 2384 }
 2385 
 2386 int
 2387 freebsd32_thr_new(struct thread *td,
 2388                   struct freebsd32_thr_new_args *uap)
 2389 {
 2390         struct thr_param32 param32;
 2391         struct thr_param param;
 2392         int error;
 2393 
 2394         if (uap->param_size < 0 ||
 2395             uap->param_size > sizeof(struct thr_param32))
 2396                 return (EINVAL);
 2397         bzero(&param, sizeof(struct thr_param));
 2398         bzero(&param32, sizeof(struct thr_param32));
 2399         error = copyin(uap->param, &param32, uap->param_size);
 2400         if (error != 0)
 2401                 return (error);
 2402         param.start_func = PTRIN(param32.start_func);
 2403         param.arg = PTRIN(param32.arg);
 2404         param.stack_base = PTRIN(param32.stack_base);
 2405         param.stack_size = param32.stack_size;
 2406         param.tls_base = PTRIN(param32.tls_base);
 2407         param.tls_size = param32.tls_size;
 2408         param.child_tid = PTRIN(param32.child_tid);
 2409         param.parent_tid = PTRIN(param32.parent_tid);
 2410         param.flags = param32.flags;
 2411         param.rtp = PTRIN(param32.rtp);
 2412         param.spare[0] = PTRIN(param32.spare[0]);
 2413         param.spare[1] = PTRIN(param32.spare[1]);
 2414         param.spare[2] = PTRIN(param32.spare[2]);
 2415 
 2416         return (kern_thr_new(td, &param));
 2417 }
 2418 
 2419 int
 2420 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
 2421 {
 2422         struct timespec32 ts32;
 2423         struct timespec ts, *tsp;
 2424         int error;
 2425 
 2426         error = 0;
 2427         tsp = NULL;
 2428         if (uap->timeout != NULL) {
 2429                 error = copyin((const void *)uap->timeout, (void *)&ts32,
 2430                     sizeof(struct timespec32));
 2431                 if (error != 0)
 2432                         return (error);
 2433                 ts.tv_sec = ts32.tv_sec;
 2434                 ts.tv_nsec = ts32.tv_nsec;
 2435                 tsp = &ts;
 2436         }
 2437         return (kern_thr_suspend(td, tsp));
 2438 }
 2439 
 2440 void
 2441 siginfo_to_siginfo32(siginfo_t *src, struct siginfo32 *dst)
 2442 {
 2443         bzero(dst, sizeof(*dst));
 2444         dst->si_signo = src->si_signo;
 2445         dst->si_errno = src->si_errno;
 2446         dst->si_code = src->si_code;
 2447         dst->si_pid = src->si_pid;
 2448         dst->si_uid = src->si_uid;
 2449         dst->si_status = src->si_status;
 2450         dst->si_addr = (uintptr_t)src->si_addr;
 2451         dst->si_value.sigval_int = src->si_value.sival_int;
 2452         dst->si_timerid = src->si_timerid;
 2453         dst->si_overrun = src->si_overrun;
 2454 }
 2455 
 2456 int
 2457 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
 2458 {
 2459         struct timespec32 ts32;
 2460         struct timespec ts;
 2461         struct timespec *timeout;
 2462         sigset_t set;
 2463         ksiginfo_t ksi;
 2464         struct siginfo32 si32;
 2465         int error;
 2466 
 2467         if (uap->timeout) {
 2468                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
 2469                 if (error)
 2470                         return (error);
 2471                 ts.tv_sec = ts32.tv_sec;
 2472                 ts.tv_nsec = ts32.tv_nsec;
 2473                 timeout = &ts;
 2474         } else
 2475                 timeout = NULL;
 2476 
 2477         error = copyin(uap->set, &set, sizeof(set));
 2478         if (error)
 2479                 return (error);
 2480 
 2481         error = kern_sigtimedwait(td, set, &ksi, timeout);
 2482         if (error)
 2483                 return (error);
 2484 
 2485         if (uap->info) {
 2486                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
 2487                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
 2488         }
 2489 
 2490         if (error == 0)
 2491                 td->td_retval[0] = ksi.ksi_signo;
 2492         return (error);
 2493 }
 2494 
 2495 /*
 2496  * MPSAFE
 2497  */
 2498 int
 2499 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
 2500 {
 2501         ksiginfo_t ksi;
 2502         struct siginfo32 si32;
 2503         sigset_t set;
 2504         int error;
 2505 
 2506         error = copyin(uap->set, &set, sizeof(set));
 2507         if (error)
 2508                 return (error);
 2509 
 2510         error = kern_sigtimedwait(td, set, &ksi, NULL);
 2511         if (error)
 2512                 return (error);
 2513 
 2514         if (uap->info) {
 2515                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
 2516                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
 2517         }       
 2518         if (error == 0)
 2519                 td->td_retval[0] = ksi.ksi_signo;
 2520         return (error);
 2521 }
 2522 
 2523 int
 2524 freebsd32_cpuset_setid(struct thread *td,
 2525     struct freebsd32_cpuset_setid_args *uap)
 2526 {
 2527         struct cpuset_setid_args ap;
 2528 
 2529         ap.which = uap->which;
 2530         ap.id = (uap->idlo | ((id_t)uap->idhi << 32));
 2531         ap.setid = uap->setid;
 2532 
 2533         return cpuset_setid(td, &ap);
 2534 }
 2535 
 2536 int
 2537 freebsd32_cpuset_getid(struct thread *td,
 2538     struct freebsd32_cpuset_getid_args *uap)
 2539 {
 2540         struct cpuset_getid_args ap;
 2541 
 2542         ap.level = uap->level;
 2543         ap.which = uap->which;
 2544         ap.id = (uap->idlo | ((id_t)uap->idhi << 32));
 2545         ap.setid = uap->setid;
 2546 
 2547         return cpuset_getid(td, &ap);
 2548 }
 2549 
 2550 int
 2551 freebsd32_cpuset_getaffinity(struct thread *td,
 2552     struct freebsd32_cpuset_getaffinity_args *uap)
 2553 {
 2554         struct cpuset_getaffinity_args ap;
 2555 
 2556         ap.level = uap->level;
 2557         ap.which = uap->which;
 2558         ap.id = (uap->idlo | ((id_t)uap->idhi << 32));
 2559         ap.cpusetsize = uap->cpusetsize;
 2560         ap.mask = uap->mask;
 2561 
 2562         return cpuset_getaffinity(td, &ap);
 2563 }
 2564 
 2565 int
 2566 freebsd32_cpuset_setaffinity(struct thread *td,
 2567     struct freebsd32_cpuset_setaffinity_args *uap)
 2568 {
 2569         struct cpuset_setaffinity_args ap;
 2570 
 2571         ap.level = uap->level;
 2572         ap.which = uap->which;
 2573         ap.id = (uap->idlo | ((id_t)uap->idhi << 32));
 2574         ap.cpusetsize = uap->cpusetsize;
 2575         ap.mask = uap->mask;
 2576 
 2577         return cpuset_setaffinity(td, &ap);
 2578 }
 2579 
 2580 int
 2581 freebsd32_nmount(struct thread *td,
 2582     struct freebsd32_nmount_args /* {
 2583         struct iovec *iovp;
 2584         unsigned int iovcnt;
 2585         int flags;
 2586     } */ *uap)
 2587 {
 2588         struct uio *auio;
 2589         struct iovec *iov;
 2590         int error, k;
 2591 
 2592         AUDIT_ARG(fflags, uap->flags);
 2593 
 2594         /*
 2595          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
 2596          * userspace to set this flag, but we must filter it out if we want
 2597          * MNT_UPDATE on the root file system to work.
 2598          * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
 2599          */
 2600         uap->flags &= ~MNT_ROOTFS;
 2601 
 2602         /*
 2603          * check that we have an even number of iovec's
 2604          * and that we have at least two options.
 2605          */
 2606         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
 2607                 return (EINVAL);
 2608 
 2609         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
 2610         if (error)
 2611                 return (error);
 2612         for (iov = auio->uio_iov, k = 0; k < uap->iovcnt; ++k, ++iov) {
 2613                 if (iov->iov_len > MMAXOPTIONLEN) {
 2614                         free(auio, M_IOV);
 2615                         return (EINVAL);
 2616                 }
 2617         }
 2618 
 2619         error = vfs_donmount(td, uap->flags, auio);
 2620         free(auio, M_IOV);
 2621         return error;
 2622 }
 2623 
 2624 #if 0
 2625 int
 2626 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
 2627 {
 2628         struct yyy32 *p32, s32;
 2629         struct yyy *p = NULL, s;
 2630         struct xxx_arg ap;
 2631         int error;
 2632 
 2633         if (uap->zzz) {
 2634                 error = copyin(uap->zzz, &s32, sizeof(s32));
 2635                 if (error)
 2636                         return (error);
 2637                 /* translate in */
 2638                 p = &s;
 2639         }
 2640         error = kern_xxx(td, p);
 2641         if (error)
 2642                 return (error);
 2643         if (uap->zzz) {
 2644                 /* translate out */
 2645                 error = copyout(&s32, p32, sizeof(s32));
 2646         }
 2647         return (error);
 2648 }
 2649 #endif
 2650 
 2651 int
 2652 syscall32_register(int *offset, struct sysent *new_sysent,
 2653     struct sysent *old_sysent)
 2654 {
 2655         if (*offset == NO_SYSCALL) {
 2656                 int i;
 2657 
 2658                 for (i = 1; i < SYS_MAXSYSCALL; ++i)
 2659                         if (freebsd32_sysent[i].sy_call ==
 2660                             (sy_call_t *)lkmnosys)
 2661                                 break;
 2662                 if (i == SYS_MAXSYSCALL)
 2663                         return (ENFILE);
 2664                 *offset = i;
 2665         } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
 2666                 return (EINVAL);
 2667         else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
 2668             freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
 2669                 return (EEXIST);
 2670 
 2671         *old_sysent = freebsd32_sysent[*offset];
 2672         freebsd32_sysent[*offset] = *new_sysent;
 2673         return 0;
 2674 }
 2675 
 2676 int
 2677 syscall32_deregister(int *offset, struct sysent *old_sysent)
 2678 {
 2679 
 2680         if (*offset)
 2681                 freebsd32_sysent[*offset] = *old_sysent;
 2682         return 0;
 2683 }
 2684 
 2685 int
 2686 syscall32_module_handler(struct module *mod, int what, void *arg)
 2687 {
 2688         struct syscall_module_data *data = (struct syscall_module_data*)arg;
 2689         modspecific_t ms;
 2690         int error;
 2691 
 2692         switch (what) {
 2693         case MOD_LOAD:
 2694                 error = syscall32_register(data->offset, data->new_sysent,
 2695                     &data->old_sysent);
 2696                 if (error) {
 2697                         /* Leave a mark so we know to safely unload below. */
 2698                         data->offset = NULL;
 2699                         return error;
 2700                 }
 2701                 ms.intval = *data->offset;
 2702                 MOD_XLOCK;
 2703                 module_setspecific(mod, &ms);
 2704                 MOD_XUNLOCK;
 2705                 if (data->chainevh)
 2706                         error = data->chainevh(mod, what, data->chainarg);
 2707                 return (error);
 2708         case MOD_UNLOAD:
 2709                 /*
 2710                  * MOD_LOAD failed, so just return without calling the
 2711                  * chained handler since we didn't pass along the MOD_LOAD
 2712                  * event.
 2713                  */
 2714                 if (data->offset == NULL)
 2715                         return (0);
 2716                 if (data->chainevh) {
 2717                         error = data->chainevh(mod, what, data->chainarg);
 2718                         if (error)
 2719                                 return (error);
 2720                 }
 2721                 error = syscall32_deregister(data->offset, &data->old_sysent);
 2722                 return (error);
 2723         default:
 2724                 error = EOPNOTSUPP;
 2725                 if (data->chainevh)
 2726                         error = data->chainevh(mod, what, data->chainarg);
 2727                 return (error);
 2728         }
 2729 }

Cache object: 0138c1aefb09fe0148fe4f4f7a749efc


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