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_old_in(struct ipc_perm32_old *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_old_out(struct ipc_perm *ip, struct ipc_perm32_old *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 static void
 1339 freebsd32_ipcperm_in(struct ipc_perm32 *ip32, struct ipc_perm *ip)
 1340 {
 1341 
 1342         CP(*ip32, *ip, cuid);
 1343         CP(*ip32, *ip, cgid);
 1344         CP(*ip32, *ip, uid);
 1345         CP(*ip32, *ip, gid);
 1346         CP(*ip32, *ip, mode);
 1347         CP(*ip32, *ip, seq);
 1348         CP(*ip32, *ip, key);
 1349 }
 1350 
 1351 static void
 1352 freebsd32_ipcperm_out(struct ipc_perm *ip, struct ipc_perm32 *ip32)
 1353 {
 1354 
 1355         CP(*ip, *ip32, cuid);
 1356         CP(*ip, *ip32, cgid);
 1357         CP(*ip, *ip32, uid);
 1358         CP(*ip, *ip32, gid);
 1359         CP(*ip, *ip32, mode);
 1360         CP(*ip, *ip32, seq);
 1361         CP(*ip, *ip32, key);
 1362 }
 1363 
 1364 int
 1365 freebsd32_semsys(struct thread *td, struct freebsd32_semsys_args *uap)
 1366 {
 1367 
 1368         switch (uap->which) {
 1369         case 0:
 1370                 return (freebsd7_freebsd32_semctl(td,
 1371                     (struct freebsd7_freebsd32_semctl_args *)&uap->a2));
 1372         default:
 1373                 return (semsys(td, (struct semsys_args *)uap));
 1374         }
 1375 }
 1376 
 1377 int
 1378 freebsd7_freebsd32_semctl(struct thread *td,
 1379     struct freebsd7_freebsd32_semctl_args *uap)
 1380 {
 1381         struct semid_ds32_old dsbuf32;
 1382         struct semid_ds dsbuf;
 1383         union semun semun;
 1384         union semun32 arg;
 1385         register_t rval;
 1386         int error;
 1387 
 1388         switch (uap->cmd) {
 1389         case SEM_STAT:
 1390         case IPC_SET:
 1391         case IPC_STAT:
 1392         case GETALL:
 1393         case SETVAL:
 1394         case SETALL:
 1395                 error = copyin(uap->arg, &arg, sizeof(arg));
 1396                 if (error)
 1397                         return (error);         
 1398                 break;
 1399         }
 1400 
 1401         switch (uap->cmd) {
 1402         case SEM_STAT:
 1403         case IPC_STAT:
 1404                 semun.buf = &dsbuf;
 1405                 break;
 1406         case IPC_SET:
 1407                 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
 1408                 if (error)
 1409                         return (error);
 1410                 freebsd32_ipcperm_old_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
 1411                 PTRIN_CP(dsbuf32, dsbuf, sem_base);
 1412                 CP(dsbuf32, dsbuf, sem_nsems);
 1413                 CP(dsbuf32, dsbuf, sem_otime);
 1414                 CP(dsbuf32, dsbuf, sem_ctime);
 1415                 semun.buf = &dsbuf;
 1416                 break;
 1417         case GETALL:
 1418         case SETALL:
 1419                 semun.array = PTRIN(arg.array);
 1420                 break;
 1421         case SETVAL:
 1422                 semun.val = arg.val;
 1423                 break;
 1424         }
 1425 
 1426         error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
 1427             &rval);
 1428         if (error)
 1429                 return (error);
 1430 
 1431         switch (uap->cmd) {
 1432         case SEM_STAT:
 1433         case IPC_STAT:
 1434                 bzero(&dsbuf32, sizeof(dsbuf32));
 1435                 freebsd32_ipcperm_old_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
 1436                 PTROUT_CP(dsbuf, dsbuf32, sem_base);
 1437                 CP(dsbuf, dsbuf32, sem_nsems);
 1438                 CP(dsbuf, dsbuf32, sem_otime);
 1439                 CP(dsbuf, dsbuf32, sem_ctime);
 1440                 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
 1441                 break;
 1442         }
 1443 
 1444         if (error == 0)
 1445                 td->td_retval[0] = rval;
 1446         return (error);
 1447 }
 1448 
 1449 int
 1450 freebsd32_semctl(struct thread *td, struct freebsd32_semctl_args *uap)
 1451 {
 1452         struct semid_ds32 dsbuf32;
 1453         struct semid_ds dsbuf;
 1454         union semun semun;
 1455         union semun32 arg;
 1456         register_t rval;
 1457         int error;
 1458 
 1459         switch (uap->cmd) {
 1460         case SEM_STAT:
 1461         case IPC_SET:
 1462         case IPC_STAT:
 1463         case GETALL:
 1464         case SETVAL:
 1465         case SETALL:
 1466                 error = copyin(uap->arg, &arg, sizeof(arg));
 1467                 if (error)
 1468                         return (error);         
 1469                 break;
 1470         }
 1471 
 1472         switch (uap->cmd) {
 1473         case SEM_STAT:
 1474         case IPC_STAT:
 1475                 semun.buf = &dsbuf;
 1476                 break;
 1477         case IPC_SET:
 1478                 error = copyin(PTRIN(arg.buf), &dsbuf32, sizeof(dsbuf32));
 1479                 if (error)
 1480                         return (error);
 1481                 freebsd32_ipcperm_in(&dsbuf32.sem_perm, &dsbuf.sem_perm);
 1482                 PTRIN_CP(dsbuf32, dsbuf, sem_base);
 1483                 CP(dsbuf32, dsbuf, sem_nsems);
 1484                 CP(dsbuf32, dsbuf, sem_otime);
 1485                 CP(dsbuf32, dsbuf, sem_ctime);
 1486                 semun.buf = &dsbuf;
 1487                 break;
 1488         case GETALL:
 1489         case SETALL:
 1490                 semun.array = PTRIN(arg.array);
 1491                 break;
 1492         case SETVAL:
 1493                 semun.val = arg.val;
 1494                 break;          
 1495         }
 1496 
 1497         error = kern_semctl(td, uap->semid, uap->semnum, uap->cmd, &semun,
 1498             &rval);
 1499         if (error)
 1500                 return (error);
 1501 
 1502         switch (uap->cmd) {
 1503         case SEM_STAT:
 1504         case IPC_STAT:
 1505                 bzero(&dsbuf32, sizeof(dsbuf32));
 1506                 freebsd32_ipcperm_out(&dsbuf.sem_perm, &dsbuf32.sem_perm);
 1507                 PTROUT_CP(dsbuf, dsbuf32, sem_base);
 1508                 CP(dsbuf, dsbuf32, sem_nsems);
 1509                 CP(dsbuf, dsbuf32, sem_otime);
 1510                 CP(dsbuf, dsbuf32, sem_ctime);
 1511                 error = copyout(&dsbuf32, PTRIN(arg.buf), sizeof(dsbuf32));
 1512                 break;
 1513         }
 1514 
 1515         if (error == 0)
 1516                 td->td_retval[0] = rval;
 1517         return (error);
 1518 }
 1519 
 1520 int
 1521 freebsd32_msgsys(struct thread *td, struct freebsd32_msgsys_args *uap)
 1522 {
 1523 
 1524         switch (uap->which) {
 1525         case 0:
 1526                 return (freebsd7_freebsd32_msgctl(td,
 1527                     (struct freebsd7_freebsd32_msgctl_args *)&uap->a2));
 1528         case 2:
 1529                 return (freebsd32_msgsnd(td,
 1530                     (struct freebsd32_msgsnd_args *)&uap->a2));
 1531         case 3:
 1532                 return (freebsd32_msgrcv(td,
 1533                     (struct freebsd32_msgrcv_args *)&uap->a2));
 1534         default:
 1535                 return (msgsys(td, (struct msgsys_args *)uap));
 1536         }
 1537 }
 1538 
 1539 int
 1540 freebsd7_freebsd32_msgctl(struct thread *td,
 1541     struct freebsd7_freebsd32_msgctl_args *uap)
 1542 {
 1543         struct msqid_ds msqbuf;
 1544         struct msqid_ds32_old msqbuf32;
 1545         int error;
 1546 
 1547         if (uap->cmd == IPC_SET) {
 1548                 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
 1549                 if (error)
 1550                         return (error);
 1551                 freebsd32_ipcperm_old_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
 1552                 PTRIN_CP(msqbuf32, msqbuf, msg_first);
 1553                 PTRIN_CP(msqbuf32, msqbuf, msg_last);
 1554                 CP(msqbuf32, msqbuf, msg_cbytes);
 1555                 CP(msqbuf32, msqbuf, msg_qnum);
 1556                 CP(msqbuf32, msqbuf, msg_qbytes);
 1557                 CP(msqbuf32, msqbuf, msg_lspid);
 1558                 CP(msqbuf32, msqbuf, msg_lrpid);
 1559                 CP(msqbuf32, msqbuf, msg_stime);
 1560                 CP(msqbuf32, msqbuf, msg_rtime);
 1561                 CP(msqbuf32, msqbuf, msg_ctime);
 1562         }
 1563         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
 1564         if (error)
 1565                 return (error);
 1566         if (uap->cmd == IPC_STAT) {
 1567                 bzero(&msqbuf32, sizeof(msqbuf32));
 1568                 freebsd32_ipcperm_old_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
 1569                 PTROUT_CP(msqbuf, msqbuf32, msg_first);
 1570                 PTROUT_CP(msqbuf, msqbuf32, msg_last);
 1571                 CP(msqbuf, msqbuf32, msg_cbytes);
 1572                 CP(msqbuf, msqbuf32, msg_qnum);
 1573                 CP(msqbuf, msqbuf32, msg_qbytes);
 1574                 CP(msqbuf, msqbuf32, msg_lspid);
 1575                 CP(msqbuf, msqbuf32, msg_lrpid);
 1576                 CP(msqbuf, msqbuf32, msg_stime);
 1577                 CP(msqbuf, msqbuf32, msg_rtime);
 1578                 CP(msqbuf, msqbuf32, msg_ctime);
 1579                 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
 1580         }
 1581         return (error);
 1582 }
 1583 
 1584 int
 1585 freebsd32_msgctl(struct thread *td, struct freebsd32_msgctl_args *uap)
 1586 {
 1587         struct msqid_ds msqbuf;
 1588         struct msqid_ds32 msqbuf32;
 1589         int error;
 1590 
 1591         if (uap->cmd == IPC_SET) {
 1592                 error = copyin(uap->buf, &msqbuf32, sizeof(msqbuf32));
 1593                 if (error)
 1594                         return (error);
 1595                 freebsd32_ipcperm_in(&msqbuf32.msg_perm, &msqbuf.msg_perm);
 1596                 PTRIN_CP(msqbuf32, msqbuf, msg_first);
 1597                 PTRIN_CP(msqbuf32, msqbuf, msg_last);
 1598                 CP(msqbuf32, msqbuf, msg_cbytes);
 1599                 CP(msqbuf32, msqbuf, msg_qnum);
 1600                 CP(msqbuf32, msqbuf, msg_qbytes);
 1601                 CP(msqbuf32, msqbuf, msg_lspid);
 1602                 CP(msqbuf32, msqbuf, msg_lrpid);
 1603                 CP(msqbuf32, msqbuf, msg_stime);
 1604                 CP(msqbuf32, msqbuf, msg_rtime);
 1605                 CP(msqbuf32, msqbuf, msg_ctime);
 1606         }
 1607         error = kern_msgctl(td, uap->msqid, uap->cmd, &msqbuf);
 1608         if (error)
 1609                 return (error);
 1610         if (uap->cmd == IPC_STAT) {
 1611                 freebsd32_ipcperm_out(&msqbuf.msg_perm, &msqbuf32.msg_perm);
 1612                 PTROUT_CP(msqbuf, msqbuf32, msg_first);
 1613                 PTROUT_CP(msqbuf, msqbuf32, msg_last);
 1614                 CP(msqbuf, msqbuf32, msg_cbytes);
 1615                 CP(msqbuf, msqbuf32, msg_qnum);
 1616                 CP(msqbuf, msqbuf32, msg_qbytes);
 1617                 CP(msqbuf, msqbuf32, msg_lspid);
 1618                 CP(msqbuf, msqbuf32, msg_lrpid);
 1619                 CP(msqbuf, msqbuf32, msg_stime);
 1620                 CP(msqbuf, msqbuf32, msg_rtime);
 1621                 CP(msqbuf, msqbuf32, msg_ctime);
 1622                 error = copyout(&msqbuf32, uap->buf, sizeof(struct msqid_ds32));
 1623         }
 1624         return (error);
 1625 }
 1626 
 1627 int
 1628 freebsd32_msgsnd(struct thread *td, struct freebsd32_msgsnd_args *uap)
 1629 {
 1630         const void *msgp;
 1631         long mtype;
 1632         int32_t mtype32;
 1633         int error;
 1634 
 1635         msgp = PTRIN(uap->msgp);
 1636         if ((error = copyin(msgp, &mtype32, sizeof(mtype32))) != 0)
 1637                 return (error);
 1638         mtype = mtype32;
 1639         return (kern_msgsnd(td, uap->msqid,
 1640             (const char *)msgp + sizeof(mtype32),
 1641             uap->msgsz, uap->msgflg, mtype));
 1642 }
 1643 
 1644 int
 1645 freebsd32_msgrcv(struct thread *td, struct freebsd32_msgrcv_args *uap)
 1646 {
 1647         void *msgp;
 1648         long mtype;
 1649         int32_t mtype32;
 1650         int error;
 1651 
 1652         msgp = PTRIN(uap->msgp);
 1653         if ((error = kern_msgrcv(td, uap->msqid,
 1654             (char *)msgp + sizeof(mtype32), uap->msgsz,
 1655             uap->msgtyp, uap->msgflg, &mtype)) != 0)
 1656                 return (error);
 1657         mtype32 = (int32_t)mtype;
 1658         return (copyout(&mtype32, msgp, sizeof(mtype32)));
 1659 }
 1660 
 1661 int
 1662 freebsd32_shmsys(struct thread *td, struct freebsd32_shmsys_args *uap)
 1663 {
 1664 
 1665         switch (uap->which) {
 1666         case 0: {       /* shmat */
 1667                 struct shmat_args ap;
 1668 
 1669                 ap.shmid = uap->a2;
 1670                 ap.shmaddr = PTRIN(uap->a3);
 1671                 ap.shmflg = uap->a4;
 1672                 return (sysent[SYS_shmat].sy_call(td, &ap));
 1673         }
 1674         case 2: {       /* shmdt */
 1675                 struct shmdt_args ap;
 1676 
 1677                 ap.shmaddr = PTRIN(uap->a2);
 1678                 return (sysent[SYS_shmdt].sy_call(td, &ap));
 1679         }
 1680         case 3: {       /* shmget */
 1681                 struct shmget_args ap;
 1682 
 1683                 ap.key = uap->a2;
 1684                 ap.size = uap->a3;
 1685                 ap.shmflg = uap->a4;
 1686                 return (sysent[SYS_shmget].sy_call(td, &ap));
 1687         }
 1688         case 4: {       /* shmctl */
 1689                 struct freebsd7_freebsd32_shmctl_args ap;
 1690 
 1691                 ap.shmid = uap->a2;
 1692                 ap.cmd = uap->a3;
 1693                 ap.buf = PTRIN(uap->a4);
 1694                 return (freebsd7_freebsd32_shmctl(td, &ap));
 1695         }
 1696         case 1:         /* oshmctl */
 1697         default:
 1698                 return (EINVAL);
 1699         }
 1700 }
 1701 
 1702 int
 1703 freebsd7_freebsd32_shmctl(struct thread *td,
 1704     struct freebsd7_freebsd32_shmctl_args *uap)
 1705 {
 1706         int error = 0;
 1707         union {
 1708                 struct shmid_ds shmid_ds;
 1709                 struct shm_info shm_info;
 1710                 struct shminfo shminfo;
 1711         } u;
 1712         union {
 1713                 struct shmid_ds32_old shmid_ds32;
 1714                 struct shm_info32 shm_info32;
 1715                 struct shminfo32 shminfo32;
 1716         } u32;
 1717         size_t sz;
 1718 
 1719         if (uap->cmd == IPC_SET) {
 1720                 if ((error = copyin(uap->buf, &u32.shmid_ds32,
 1721                     sizeof(u32.shmid_ds32))))
 1722                         goto done;
 1723                 freebsd32_ipcperm_old_in(&u32.shmid_ds32.shm_perm,
 1724                     &u.shmid_ds.shm_perm);
 1725                 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
 1726                 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
 1727                 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
 1728                 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
 1729                 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
 1730                 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
 1731                 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
 1732         }
 1733         
 1734         error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
 1735         if (error)
 1736                 goto done;
 1737         
 1738         /* Cases in which we need to copyout */
 1739         switch (uap->cmd) {
 1740         case IPC_INFO:
 1741                 CP(u.shminfo, u32.shminfo32, shmmax);
 1742                 CP(u.shminfo, u32.shminfo32, shmmin);
 1743                 CP(u.shminfo, u32.shminfo32, shmmni);
 1744                 CP(u.shminfo, u32.shminfo32, shmseg);
 1745                 CP(u.shminfo, u32.shminfo32, shmall);
 1746                 error = copyout(&u32.shminfo32, uap->buf,
 1747                     sizeof(u32.shminfo32));
 1748                 break;
 1749         case SHM_INFO:
 1750                 CP(u.shm_info, u32.shm_info32, used_ids);
 1751                 CP(u.shm_info, u32.shm_info32, shm_rss);
 1752                 CP(u.shm_info, u32.shm_info32, shm_tot);
 1753                 CP(u.shm_info, u32.shm_info32, shm_swp);
 1754                 CP(u.shm_info, u32.shm_info32, swap_attempts);
 1755                 CP(u.shm_info, u32.shm_info32, swap_successes);
 1756                 error = copyout(&u32.shm_info32, uap->buf,
 1757                     sizeof(u32.shm_info32));
 1758                 break;
 1759         case SHM_STAT:
 1760         case IPC_STAT:
 1761                 freebsd32_ipcperm_old_out(&u.shmid_ds.shm_perm,
 1762                     &u32.shmid_ds32.shm_perm);
 1763                 if (u.shmid_ds.shm_segsz > INT32_MAX)
 1764                         u32.shmid_ds32.shm_segsz = INT32_MAX;
 1765                 else
 1766                         CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
 1767                 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
 1768                 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
 1769                 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
 1770                 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
 1771                 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
 1772                 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
 1773                 u32.shmid_ds32.shm_internal = 0;
 1774                 error = copyout(&u32.shmid_ds32, uap->buf,
 1775                     sizeof(u32.shmid_ds32));
 1776                 break;
 1777         }
 1778 
 1779 done:
 1780         if (error) {
 1781                 /* Invalidate the return value */
 1782                 td->td_retval[0] = -1;
 1783         }
 1784         return (error);
 1785 }
 1786 
 1787 int
 1788 freebsd32_shmctl(struct thread *td, struct freebsd32_shmctl_args *uap)
 1789 {
 1790         int error = 0;
 1791         union {
 1792                 struct shmid_ds shmid_ds;
 1793                 struct shm_info shm_info;
 1794                 struct shminfo shminfo;
 1795         } u;
 1796         union {
 1797                 struct shmid_ds32 shmid_ds32;
 1798                 struct shm_info32 shm_info32;
 1799                 struct shminfo32 shminfo32;
 1800         } u32;
 1801         size_t sz;
 1802         
 1803         if (uap->cmd == IPC_SET) {
 1804                 if ((error = copyin(uap->buf, &u32.shmid_ds32,
 1805                     sizeof(u32.shmid_ds32))))
 1806                         goto done;
 1807                 freebsd32_ipcperm_in(&u32.shmid_ds32.shm_perm,
 1808                     &u.shmid_ds.shm_perm);
 1809                 CP(u32.shmid_ds32, u.shmid_ds, shm_segsz);
 1810                 CP(u32.shmid_ds32, u.shmid_ds, shm_lpid);
 1811                 CP(u32.shmid_ds32, u.shmid_ds, shm_cpid);
 1812                 CP(u32.shmid_ds32, u.shmid_ds, shm_nattch);
 1813                 CP(u32.shmid_ds32, u.shmid_ds, shm_atime);
 1814                 CP(u32.shmid_ds32, u.shmid_ds, shm_dtime);
 1815                 CP(u32.shmid_ds32, u.shmid_ds, shm_ctime);
 1816         }
 1817         
 1818         error = kern_shmctl(td, uap->shmid, uap->cmd, (void *)&u, &sz);
 1819         if (error)
 1820                 goto done;
 1821         
 1822         /* Cases in which we need to copyout */
 1823         switch (uap->cmd) {
 1824         case IPC_INFO:
 1825                 CP(u.shminfo, u32.shminfo32, shmmax);
 1826                 CP(u.shminfo, u32.shminfo32, shmmin);
 1827                 CP(u.shminfo, u32.shminfo32, shmmni);
 1828                 CP(u.shminfo, u32.shminfo32, shmseg);
 1829                 CP(u.shminfo, u32.shminfo32, shmall);
 1830                 error = copyout(&u32.shminfo32, uap->buf,
 1831                     sizeof(u32.shminfo32));
 1832                 break;
 1833         case SHM_INFO:
 1834                 CP(u.shm_info, u32.shm_info32, used_ids);
 1835                 CP(u.shm_info, u32.shm_info32, shm_rss);
 1836                 CP(u.shm_info, u32.shm_info32, shm_tot);
 1837                 CP(u.shm_info, u32.shm_info32, shm_swp);
 1838                 CP(u.shm_info, u32.shm_info32, swap_attempts);
 1839                 CP(u.shm_info, u32.shm_info32, swap_successes);
 1840                 error = copyout(&u32.shm_info32, uap->buf,
 1841                     sizeof(u32.shm_info32));
 1842                 break;
 1843         case SHM_STAT:
 1844         case IPC_STAT:
 1845                 freebsd32_ipcperm_out(&u.shmid_ds.shm_perm,
 1846                     &u32.shmid_ds32.shm_perm);
 1847                 if (u.shmid_ds.shm_segsz > INT32_MAX)
 1848                         u32.shmid_ds32.shm_segsz = INT32_MAX;
 1849                 else
 1850                         CP(u.shmid_ds, u32.shmid_ds32, shm_segsz);
 1851                 CP(u.shmid_ds, u32.shmid_ds32, shm_lpid);
 1852                 CP(u.shmid_ds, u32.shmid_ds32, shm_cpid);
 1853                 CP(u.shmid_ds, u32.shmid_ds32, shm_nattch);
 1854                 CP(u.shmid_ds, u32.shmid_ds32, shm_atime);
 1855                 CP(u.shmid_ds, u32.shmid_ds32, shm_dtime);
 1856                 CP(u.shmid_ds, u32.shmid_ds32, shm_ctime);
 1857                 error = copyout(&u32.shmid_ds32, uap->buf,
 1858                     sizeof(u32.shmid_ds32));
 1859                 break;
 1860         }
 1861 
 1862 done:
 1863         if (error) {
 1864                 /* Invalidate the return value */
 1865                 td->td_retval[0] = -1;
 1866         }
 1867         return (error);
 1868 }
 1869 
 1870 int
 1871 freebsd32_pread(struct thread *td, struct freebsd32_pread_args *uap)
 1872 {
 1873         struct pread_args ap;
 1874 
 1875         ap.fd = uap->fd;
 1876         ap.buf = uap->buf;
 1877         ap.nbyte = uap->nbyte;
 1878         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1879         return (pread(td, &ap));
 1880 }
 1881 
 1882 int
 1883 freebsd32_pwrite(struct thread *td, struct freebsd32_pwrite_args *uap)
 1884 {
 1885         struct pwrite_args ap;
 1886 
 1887         ap.fd = uap->fd;
 1888         ap.buf = uap->buf;
 1889         ap.nbyte = uap->nbyte;
 1890         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1891         return (pwrite(td, &ap));
 1892 }
 1893 
 1894 int
 1895 freebsd32_lseek(struct thread *td, struct freebsd32_lseek_args *uap)
 1896 {
 1897         int error;
 1898         struct lseek_args ap;
 1899         off_t pos;
 1900 
 1901         ap.fd = uap->fd;
 1902         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1903         ap.whence = uap->whence;
 1904         error = lseek(td, &ap);
 1905         /* Expand the quad return into two parts for eax and edx */
 1906         pos = *(off_t *)(td->td_retval);
 1907         td->td_retval[0] = pos & 0xffffffff;    /* %eax */
 1908         td->td_retval[1] = pos >> 32;           /* %edx */
 1909         return error;
 1910 }
 1911 
 1912 int
 1913 freebsd32_truncate(struct thread *td, struct freebsd32_truncate_args *uap)
 1914 {
 1915         struct truncate_args ap;
 1916 
 1917         ap.path = uap->path;
 1918         ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
 1919         return (truncate(td, &ap));
 1920 }
 1921 
 1922 int
 1923 freebsd32_ftruncate(struct thread *td, struct freebsd32_ftruncate_args *uap)
 1924 {
 1925         struct ftruncate_args ap;
 1926 
 1927         ap.fd = uap->fd;
 1928         ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
 1929         return (ftruncate(td, &ap));
 1930 }
 1931 
 1932 int
 1933 freebsd32_getdirentries(struct thread *td,
 1934     struct freebsd32_getdirentries_args *uap)
 1935 {
 1936         long base;
 1937         int32_t base32;
 1938         int error;
 1939 
 1940         error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base);
 1941         if (error)
 1942                 return (error);
 1943         if (uap->basep != NULL) {
 1944                 base32 = base;
 1945                 error = copyout(&base32, uap->basep, sizeof(int32_t));
 1946         }
 1947         return (error);
 1948 }
 1949 
 1950 #ifdef COMPAT_FREEBSD6
 1951 /* versions with the 'int pad' argument */
 1952 int
 1953 freebsd6_freebsd32_pread(struct thread *td, struct freebsd6_freebsd32_pread_args *uap)
 1954 {
 1955         struct pread_args ap;
 1956 
 1957         ap.fd = uap->fd;
 1958         ap.buf = uap->buf;
 1959         ap.nbyte = uap->nbyte;
 1960         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1961         return (pread(td, &ap));
 1962 }
 1963 
 1964 int
 1965 freebsd6_freebsd32_pwrite(struct thread *td, struct freebsd6_freebsd32_pwrite_args *uap)
 1966 {
 1967         struct pwrite_args ap;
 1968 
 1969         ap.fd = uap->fd;
 1970         ap.buf = uap->buf;
 1971         ap.nbyte = uap->nbyte;
 1972         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1973         return (pwrite(td, &ap));
 1974 }
 1975 
 1976 int
 1977 freebsd6_freebsd32_lseek(struct thread *td, struct freebsd6_freebsd32_lseek_args *uap)
 1978 {
 1979         int error;
 1980         struct lseek_args ap;
 1981         off_t pos;
 1982 
 1983         ap.fd = uap->fd;
 1984         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 1985         ap.whence = uap->whence;
 1986         error = lseek(td, &ap);
 1987         /* Expand the quad return into two parts for eax and edx */
 1988         pos = *(off_t *)(td->td_retval);
 1989         td->td_retval[0] = pos & 0xffffffff;    /* %eax */
 1990         td->td_retval[1] = pos >> 32;           /* %edx */
 1991         return error;
 1992 }
 1993 
 1994 int
 1995 freebsd6_freebsd32_truncate(struct thread *td, struct freebsd6_freebsd32_truncate_args *uap)
 1996 {
 1997         struct truncate_args ap;
 1998 
 1999         ap.path = uap->path;
 2000         ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
 2001         return (truncate(td, &ap));
 2002 }
 2003 
 2004 int
 2005 freebsd6_freebsd32_ftruncate(struct thread *td, struct freebsd6_freebsd32_ftruncate_args *uap)
 2006 {
 2007         struct ftruncate_args ap;
 2008 
 2009         ap.fd = uap->fd;
 2010         ap.length = (uap->lengthlo | ((off_t)uap->lengthhi << 32));
 2011         return (ftruncate(td, &ap));
 2012 }
 2013 #endif /* COMPAT_FREEBSD6 */
 2014 
 2015 struct sf_hdtr32 {
 2016         uint32_t headers;
 2017         int hdr_cnt;
 2018         uint32_t trailers;
 2019         int trl_cnt;
 2020 };
 2021 
 2022 static int
 2023 freebsd32_do_sendfile(struct thread *td,
 2024     struct freebsd32_sendfile_args *uap, int compat)
 2025 {
 2026         struct sendfile_args ap;
 2027         struct sf_hdtr32 hdtr32;
 2028         struct sf_hdtr hdtr;
 2029         struct uio *hdr_uio, *trl_uio;
 2030         struct iovec32 *iov32;
 2031         int error;
 2032 
 2033         hdr_uio = trl_uio = NULL;
 2034 
 2035         ap.fd = uap->fd;
 2036         ap.s = uap->s;
 2037         ap.offset = (uap->offsetlo | ((off_t)uap->offsethi << 32));
 2038         ap.nbytes = uap->nbytes;
 2039         ap.hdtr = (struct sf_hdtr *)uap->hdtr;          /* XXX not used */
 2040         ap.sbytes = uap->sbytes;
 2041         ap.flags = uap->flags;
 2042 
 2043         if (uap->hdtr != NULL) {
 2044                 error = copyin(uap->hdtr, &hdtr32, sizeof(hdtr32));
 2045                 if (error)
 2046                         goto out;
 2047                 PTRIN_CP(hdtr32, hdtr, headers);
 2048                 CP(hdtr32, hdtr, hdr_cnt);
 2049                 PTRIN_CP(hdtr32, hdtr, trailers);
 2050                 CP(hdtr32, hdtr, trl_cnt);
 2051 
 2052                 if (hdtr.headers != NULL) {
 2053                         iov32 = PTRIN(hdtr32.headers);
 2054                         error = freebsd32_copyinuio(iov32,
 2055                             hdtr32.hdr_cnt, &hdr_uio);
 2056                         if (error)
 2057                                 goto out;
 2058                 }
 2059                 if (hdtr.trailers != NULL) {
 2060                         iov32 = PTRIN(hdtr32.trailers);
 2061                         error = freebsd32_copyinuio(iov32,
 2062                             hdtr32.trl_cnt, &trl_uio);
 2063                         if (error)
 2064                                 goto out;
 2065                 }
 2066         }
 2067 
 2068         error = kern_sendfile(td, &ap, hdr_uio, trl_uio, compat);
 2069 out:
 2070         if (hdr_uio)
 2071                 free(hdr_uio, M_IOV);
 2072         if (trl_uio)
 2073                 free(trl_uio, M_IOV);
 2074         return (error);
 2075 }
 2076 
 2077 #ifdef COMPAT_FREEBSD4
 2078 int
 2079 freebsd4_freebsd32_sendfile(struct thread *td,
 2080     struct freebsd4_freebsd32_sendfile_args *uap)
 2081 {
 2082         return (freebsd32_do_sendfile(td,
 2083             (struct freebsd32_sendfile_args *)uap, 1));
 2084 }
 2085 #endif
 2086 
 2087 int
 2088 freebsd32_sendfile(struct thread *td, struct freebsd32_sendfile_args *uap)
 2089 {
 2090 
 2091         return (freebsd32_do_sendfile(td, uap, 0));
 2092 }
 2093 
 2094 static void
 2095 copy_stat( struct stat *in, struct stat32 *out)
 2096 {
 2097         CP(*in, *out, st_dev);
 2098         CP(*in, *out, st_ino);
 2099         CP(*in, *out, st_mode);
 2100         CP(*in, *out, st_nlink);
 2101         CP(*in, *out, st_uid);
 2102         CP(*in, *out, st_gid);
 2103         CP(*in, *out, st_rdev);
 2104         TS_CP(*in, *out, st_atimespec);
 2105         TS_CP(*in, *out, st_mtimespec);
 2106         TS_CP(*in, *out, st_ctimespec);
 2107         CP(*in, *out, st_size);
 2108         CP(*in, *out, st_blocks);
 2109         CP(*in, *out, st_blksize);
 2110         CP(*in, *out, st_flags);
 2111         CP(*in, *out, st_gen);
 2112 }
 2113 
 2114 int
 2115 freebsd32_stat(struct thread *td, struct freebsd32_stat_args *uap)
 2116 {
 2117         struct stat sb;
 2118         struct stat32 sb32;
 2119         int error;
 2120 
 2121         error = kern_stat(td, uap->path, UIO_USERSPACE, &sb);
 2122         if (error)
 2123                 return (error);
 2124         copy_stat(&sb, &sb32);
 2125         error = copyout(&sb32, uap->ub, sizeof (sb32));
 2126         return (error);
 2127 }
 2128 
 2129 int
 2130 freebsd32_fstat(struct thread *td, struct freebsd32_fstat_args *uap)
 2131 {
 2132         struct stat ub;
 2133         struct stat32 ub32;
 2134         int error;
 2135 
 2136         error = kern_fstat(td, uap->fd, &ub);
 2137         if (error)
 2138                 return (error);
 2139         copy_stat(&ub, &ub32);
 2140         error = copyout(&ub32, uap->ub, sizeof(ub32));
 2141         return (error);
 2142 }
 2143 
 2144 int
 2145 freebsd32_lstat(struct thread *td, struct freebsd32_lstat_args *uap)
 2146 {
 2147         struct stat sb;
 2148         struct stat32 sb32;
 2149         int error;
 2150 
 2151         error = kern_lstat(td, uap->path, UIO_USERSPACE, &sb);
 2152         if (error)
 2153                 return (error);
 2154         copy_stat(&sb, &sb32);
 2155         error = copyout(&sb32, uap->ub, sizeof (sb32));
 2156         return (error);
 2157 }
 2158 
 2159 /*
 2160  * MPSAFE
 2161  */
 2162 int
 2163 freebsd32_sysctl(struct thread *td, struct freebsd32_sysctl_args *uap)
 2164 {
 2165         int error, name[CTL_MAXNAME];
 2166         size_t j, oldlen;
 2167 
 2168         if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
 2169                 return (EINVAL);
 2170         error = copyin(uap->name, name, uap->namelen * sizeof(int));
 2171         if (error)
 2172                 return (error);
 2173         if (uap->oldlenp)
 2174                 oldlen = fuword32(uap->oldlenp);
 2175         else
 2176                 oldlen = 0;
 2177         error = userland_sysctl(td, name, uap->namelen,
 2178                 uap->old, &oldlen, 1,
 2179                 uap->new, uap->newlen, &j, SCTL_MASK32);
 2180         if (error && error != ENOMEM)
 2181                 return (error);
 2182         if (uap->oldlenp)
 2183                 suword32(uap->oldlenp, j);
 2184         return (0);
 2185 }
 2186 
 2187 int
 2188 freebsd32_jail(struct thread *td, struct freebsd32_jail_args *uap)
 2189 {
 2190         uint32_t version;
 2191         int error;
 2192         struct jail j;
 2193 
 2194         error = copyin(uap->jail, &version, sizeof(uint32_t));
 2195         if (error)
 2196                 return (error);
 2197         switch (version) {
 2198         case 0: 
 2199         {
 2200                 /* FreeBSD single IPv4 jails. */
 2201                 struct jail32_v0 j32_v0;
 2202 
 2203                 bzero(&j, sizeof(struct jail));
 2204                 error = copyin(uap->jail, &j32_v0, sizeof(struct jail32_v0));
 2205                 if (error)
 2206                         return (error);
 2207                 CP(j32_v0, j, version);
 2208                 PTRIN_CP(j32_v0, j, path);
 2209                 PTRIN_CP(j32_v0, j, hostname);
 2210                 j.ip4s = j32_v0.ip_number;
 2211                 break;
 2212         }
 2213 
 2214         case 1:
 2215                 /*
 2216                  * Version 1 was used by multi-IPv4 jail implementations
 2217                  * that never made it into the official kernel.
 2218                  */
 2219                 return (EINVAL);
 2220 
 2221         case 2: /* JAIL_API_VERSION */
 2222         {
 2223                 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
 2224                 struct jail32 j32;
 2225 
 2226                 error = copyin(uap->jail, &j32, sizeof(struct jail32));
 2227                 if (error)
 2228                         return (error);
 2229                 CP(j32, j, version);
 2230                 PTRIN_CP(j32, j, path);
 2231                 PTRIN_CP(j32, j, hostname);
 2232                 PTRIN_CP(j32, j, jailname);
 2233                 CP(j32, j, ip4s);
 2234                 CP(j32, j, ip6s);
 2235                 PTRIN_CP(j32, j, ip4);
 2236                 PTRIN_CP(j32, j, ip6);
 2237                 break;
 2238         }
 2239 
 2240         default:
 2241                 /* Sci-Fi jails are not supported, sorry. */
 2242                 return (EINVAL);
 2243         }
 2244         return (kern_jail(td, &j));
 2245 }
 2246 
 2247 int
 2248 freebsd32_sigaction(struct thread *td, struct freebsd32_sigaction_args *uap)
 2249 {
 2250         struct sigaction32 s32;
 2251         struct sigaction sa, osa, *sap;
 2252         int error;
 2253 
 2254         if (uap->act) {
 2255                 error = copyin(uap->act, &s32, sizeof(s32));
 2256                 if (error)
 2257                         return (error);
 2258                 sa.sa_handler = PTRIN(s32.sa_u);
 2259                 CP(s32, sa, sa_flags);
 2260                 CP(s32, sa, sa_mask);
 2261                 sap = &sa;
 2262         } else
 2263                 sap = NULL;
 2264         error = kern_sigaction(td, uap->sig, sap, &osa, 0);
 2265         if (error == 0 && uap->oact != NULL) {
 2266                 s32.sa_u = PTROUT(osa.sa_handler);
 2267                 CP(osa, s32, sa_flags);
 2268                 CP(osa, s32, sa_mask);
 2269                 error = copyout(&s32, uap->oact, sizeof(s32));
 2270         }
 2271         return (error);
 2272 }
 2273 
 2274 #ifdef COMPAT_FREEBSD4
 2275 int
 2276 freebsd4_freebsd32_sigaction(struct thread *td,
 2277                              struct freebsd4_freebsd32_sigaction_args *uap)
 2278 {
 2279         struct sigaction32 s32;
 2280         struct sigaction sa, osa, *sap;
 2281         int error;
 2282 
 2283         if (uap->act) {
 2284                 error = copyin(uap->act, &s32, sizeof(s32));
 2285                 if (error)
 2286                         return (error);
 2287                 sa.sa_handler = PTRIN(s32.sa_u);
 2288                 CP(s32, sa, sa_flags);
 2289                 CP(s32, sa, sa_mask);
 2290                 sap = &sa;
 2291         } else
 2292                 sap = NULL;
 2293         error = kern_sigaction(td, uap->sig, sap, &osa, KSA_FREEBSD4);
 2294         if (error == 0 && uap->oact != NULL) {
 2295                 s32.sa_u = PTROUT(osa.sa_handler);
 2296                 CP(osa, s32, sa_flags);
 2297                 CP(osa, s32, sa_mask);
 2298                 error = copyout(&s32, uap->oact, sizeof(s32));
 2299         }
 2300         return (error);
 2301 }
 2302 #endif
 2303 
 2304 #ifdef COMPAT_43
 2305 struct osigaction32 {
 2306         u_int32_t       sa_u;
 2307         osigset_t       sa_mask;
 2308         int             sa_flags;
 2309 };
 2310 
 2311 #define ONSIG   32
 2312 
 2313 int
 2314 ofreebsd32_sigaction(struct thread *td,
 2315                              struct ofreebsd32_sigaction_args *uap)
 2316 {
 2317         struct osigaction32 s32;
 2318         struct sigaction sa, osa, *sap;
 2319         int error;
 2320 
 2321         if (uap->signum <= 0 || uap->signum >= ONSIG)
 2322                 return (EINVAL);
 2323 
 2324         if (uap->nsa) {
 2325                 error = copyin(uap->nsa, &s32, sizeof(s32));
 2326                 if (error)
 2327                         return (error);
 2328                 sa.sa_handler = PTRIN(s32.sa_u);
 2329                 CP(s32, sa, sa_flags);
 2330                 OSIG2SIG(s32.sa_mask, sa.sa_mask);
 2331                 sap = &sa;
 2332         } else
 2333                 sap = NULL;
 2334         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
 2335         if (error == 0 && uap->osa != NULL) {
 2336                 s32.sa_u = PTROUT(osa.sa_handler);
 2337                 CP(osa, s32, sa_flags);
 2338                 SIG2OSIG(osa.sa_mask, s32.sa_mask);
 2339                 error = copyout(&s32, uap->osa, sizeof(s32));
 2340         }
 2341         return (error);
 2342 }
 2343 
 2344 int
 2345 ofreebsd32_sigprocmask(struct thread *td,
 2346                                struct ofreebsd32_sigprocmask_args *uap)
 2347 {
 2348         sigset_t set, oset;
 2349         int error;
 2350 
 2351         OSIG2SIG(uap->mask, set);
 2352         error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
 2353         SIG2OSIG(oset, td->td_retval[0]);
 2354         return (error);
 2355 }
 2356 
 2357 int
 2358 ofreebsd32_sigpending(struct thread *td,
 2359                               struct ofreebsd32_sigpending_args *uap)
 2360 {
 2361         struct proc *p = td->td_proc;
 2362         sigset_t siglist;
 2363 
 2364         PROC_LOCK(p);
 2365         siglist = p->p_siglist;
 2366         SIGSETOR(siglist, td->td_siglist);
 2367         PROC_UNLOCK(p);
 2368         SIG2OSIG(siglist, td->td_retval[0]);
 2369         return (0);
 2370 }
 2371 
 2372 struct sigvec32 {
 2373         u_int32_t       sv_handler;
 2374         int             sv_mask;
 2375         int             sv_flags;
 2376 };
 2377 
 2378 int
 2379 ofreebsd32_sigvec(struct thread *td,
 2380                           struct ofreebsd32_sigvec_args *uap)
 2381 {
 2382         struct sigvec32 vec;
 2383         struct sigaction sa, osa, *sap;
 2384         int error;
 2385 
 2386         if (uap->signum <= 0 || uap->signum >= ONSIG)
 2387                 return (EINVAL);
 2388 
 2389         if (uap->nsv) {
 2390                 error = copyin(uap->nsv, &vec, sizeof(vec));
 2391                 if (error)
 2392                         return (error);
 2393                 sa.sa_handler = PTRIN(vec.sv_handler);
 2394                 OSIG2SIG(vec.sv_mask, sa.sa_mask);
 2395                 sa.sa_flags = vec.sv_flags;
 2396                 sa.sa_flags ^= SA_RESTART;
 2397                 sap = &sa;
 2398         } else
 2399                 sap = NULL;
 2400         error = kern_sigaction(td, uap->signum, sap, &osa, KSA_OSIGSET);
 2401         if (error == 0 && uap->osv != NULL) {
 2402                 vec.sv_handler = PTROUT(osa.sa_handler);
 2403                 SIG2OSIG(osa.sa_mask, vec.sv_mask);
 2404                 vec.sv_flags = osa.sa_flags;
 2405                 vec.sv_flags &= ~SA_NOCLDWAIT;
 2406                 vec.sv_flags ^= SA_RESTART;
 2407                 error = copyout(&vec, uap->osv, sizeof(vec));
 2408         }
 2409         return (error);
 2410 }
 2411 
 2412 int
 2413 ofreebsd32_sigblock(struct thread *td,
 2414                             struct ofreebsd32_sigblock_args *uap)
 2415 {
 2416         struct proc *p = td->td_proc;
 2417         sigset_t set;
 2418 
 2419         OSIG2SIG(uap->mask, set);
 2420         SIG_CANTMASK(set);
 2421         PROC_LOCK(p);
 2422         SIG2OSIG(td->td_sigmask, td->td_retval[0]);
 2423         SIGSETOR(td->td_sigmask, set);
 2424         PROC_UNLOCK(p);
 2425         return (0);
 2426 }
 2427 
 2428 int
 2429 ofreebsd32_sigsetmask(struct thread *td,
 2430                               struct ofreebsd32_sigsetmask_args *uap)
 2431 {
 2432         struct proc *p = td->td_proc;
 2433         sigset_t set;
 2434 
 2435         OSIG2SIG(uap->mask, set);
 2436         SIG_CANTMASK(set);
 2437         PROC_LOCK(p);
 2438         SIG2OSIG(td->td_sigmask, td->td_retval[0]);
 2439         SIGSETLO(td->td_sigmask, set);
 2440         signotify(td);
 2441         PROC_UNLOCK(p);
 2442         return (0);
 2443 }
 2444 
 2445 int
 2446 ofreebsd32_sigsuspend(struct thread *td,
 2447                               struct ofreebsd32_sigsuspend_args *uap)
 2448 {
 2449         struct proc *p = td->td_proc;
 2450         sigset_t mask;
 2451 
 2452         PROC_LOCK(p);
 2453         td->td_oldsigmask = td->td_sigmask;
 2454         td->td_pflags |= TDP_OLDMASK;
 2455         OSIG2SIG(uap->mask, mask);
 2456         SIG_CANTMASK(mask);
 2457         SIGSETLO(td->td_sigmask, mask);
 2458         signotify(td);
 2459         while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
 2460                 /* void */;
 2461         PROC_UNLOCK(p);
 2462         /* always return EINTR rather than ERESTART... */
 2463         return (EINTR);
 2464 }
 2465 
 2466 struct sigstack32 {
 2467         u_int32_t       ss_sp;
 2468         int             ss_onstack;
 2469 };
 2470 
 2471 int
 2472 ofreebsd32_sigstack(struct thread *td,
 2473                             struct ofreebsd32_sigstack_args *uap)
 2474 {
 2475         struct sigstack32 s32;
 2476         struct sigstack nss, oss;
 2477         int error = 0, unss;
 2478 
 2479         if (uap->nss != NULL) {
 2480                 error = copyin(uap->nss, &s32, sizeof(s32));
 2481                 if (error)
 2482                         return (error);
 2483                 nss.ss_sp = PTRIN(s32.ss_sp);
 2484                 CP(s32, nss, ss_onstack);
 2485                 unss = 1;
 2486         } else {
 2487                 unss = 0;
 2488         }
 2489         oss.ss_sp = td->td_sigstk.ss_sp;
 2490         oss.ss_onstack = sigonstack(cpu_getstack(td));
 2491         if (unss) {
 2492                 td->td_sigstk.ss_sp = nss.ss_sp;
 2493                 td->td_sigstk.ss_size = 0;
 2494                 td->td_sigstk.ss_flags |= (nss.ss_onstack & SS_ONSTACK);
 2495                 td->td_pflags |= TDP_ALTSTACK;
 2496         }
 2497         if (uap->oss != NULL) {
 2498                 s32.ss_sp = PTROUT(oss.ss_sp);
 2499                 CP(oss, s32, ss_onstack);
 2500                 error = copyout(&s32, uap->oss, sizeof(s32));
 2501         }
 2502         return (error);
 2503 }
 2504 #endif
 2505 
 2506 int
 2507 freebsd32_nanosleep(struct thread *td, struct freebsd32_nanosleep_args *uap)
 2508 {
 2509         struct timespec32 rmt32, rqt32;
 2510         struct timespec rmt, rqt;
 2511         int error;
 2512 
 2513         error = copyin(uap->rqtp, &rqt32, sizeof(rqt32));
 2514         if (error)
 2515                 return (error);
 2516 
 2517         CP(rqt32, rqt, tv_sec);
 2518         CP(rqt32, rqt, tv_nsec);
 2519 
 2520         if (uap->rmtp &&
 2521             !useracc((caddr_t)uap->rmtp, sizeof(rmt), VM_PROT_WRITE))
 2522                 return (EFAULT);
 2523         error = kern_nanosleep(td, &rqt, &rmt);
 2524         if (error && uap->rmtp) {
 2525                 int error2;
 2526 
 2527                 CP(rmt, rmt32, tv_sec);
 2528                 CP(rmt, rmt32, tv_nsec);
 2529 
 2530                 error2 = copyout(&rmt32, uap->rmtp, sizeof(rmt32));
 2531                 if (error2)
 2532                         error = error2;
 2533         }
 2534         return (error);
 2535 }
 2536 
 2537 int
 2538 freebsd32_clock_gettime(struct thread *td,
 2539                         struct freebsd32_clock_gettime_args *uap)
 2540 {
 2541         struct timespec ats;
 2542         struct timespec32 ats32;
 2543         int error;
 2544 
 2545         error = kern_clock_gettime(td, uap->clock_id, &ats);
 2546         if (error == 0) {
 2547                 CP(ats, ats32, tv_sec);
 2548                 CP(ats, ats32, tv_nsec);
 2549                 error = copyout(&ats32, uap->tp, sizeof(ats32));
 2550         }
 2551         return (error);
 2552 }
 2553 
 2554 int
 2555 freebsd32_clock_settime(struct thread *td,
 2556                         struct freebsd32_clock_settime_args *uap)
 2557 {
 2558         struct timespec ats;
 2559         struct timespec32 ats32;
 2560         int error;
 2561 
 2562         error = copyin(uap->tp, &ats32, sizeof(ats32));
 2563         if (error)
 2564                 return (error);
 2565         CP(ats32, ats, tv_sec);
 2566         CP(ats32, ats, tv_nsec);
 2567 
 2568         return (kern_clock_settime(td, uap->clock_id, &ats));
 2569 }
 2570 
 2571 int
 2572 freebsd32_clock_getres(struct thread *td,
 2573                        struct freebsd32_clock_getres_args *uap)
 2574 {
 2575         struct timespec ts;
 2576         struct timespec32 ts32;
 2577         int error;
 2578 
 2579         if (uap->tp == NULL)
 2580                 return (0);
 2581         error = kern_clock_getres(td, uap->clock_id, &ts);
 2582         if (error == 0) {
 2583                 CP(ts, ts32, tv_sec);
 2584                 CP(ts, ts32, tv_nsec);
 2585                 error = copyout(&ts32, uap->tp, sizeof(ts32));
 2586         }
 2587         return (error);
 2588 }
 2589 
 2590 int
 2591 freebsd32_thr_new(struct thread *td,
 2592                   struct freebsd32_thr_new_args *uap)
 2593 {
 2594         struct thr_param32 param32;
 2595         struct thr_param param;
 2596         int error;
 2597 
 2598         if (uap->param_size < 0 ||
 2599             uap->param_size > sizeof(struct thr_param32))
 2600                 return (EINVAL);
 2601         bzero(&param, sizeof(struct thr_param));
 2602         bzero(&param32, sizeof(struct thr_param32));
 2603         error = copyin(uap->param, &param32, uap->param_size);
 2604         if (error != 0)
 2605                 return (error);
 2606         param.start_func = PTRIN(param32.start_func);
 2607         param.arg = PTRIN(param32.arg);
 2608         param.stack_base = PTRIN(param32.stack_base);
 2609         param.stack_size = param32.stack_size;
 2610         param.tls_base = PTRIN(param32.tls_base);
 2611         param.tls_size = param32.tls_size;
 2612         param.child_tid = PTRIN(param32.child_tid);
 2613         param.parent_tid = PTRIN(param32.parent_tid);
 2614         param.flags = param32.flags;
 2615         param.rtp = PTRIN(param32.rtp);
 2616         param.spare[0] = PTRIN(param32.spare[0]);
 2617         param.spare[1] = PTRIN(param32.spare[1]);
 2618         param.spare[2] = PTRIN(param32.spare[2]);
 2619 
 2620         return (kern_thr_new(td, &param));
 2621 }
 2622 
 2623 int
 2624 freebsd32_thr_suspend(struct thread *td, struct freebsd32_thr_suspend_args *uap)
 2625 {
 2626         struct timespec32 ts32;
 2627         struct timespec ts, *tsp;
 2628         int error;
 2629 
 2630         error = 0;
 2631         tsp = NULL;
 2632         if (uap->timeout != NULL) {
 2633                 error = copyin((const void *)uap->timeout, (void *)&ts32,
 2634                     sizeof(struct timespec32));
 2635                 if (error != 0)
 2636                         return (error);
 2637                 ts.tv_sec = ts32.tv_sec;
 2638                 ts.tv_nsec = ts32.tv_nsec;
 2639                 tsp = &ts;
 2640         }
 2641         return (kern_thr_suspend(td, tsp));
 2642 }
 2643 
 2644 void
 2645 siginfo_to_siginfo32(siginfo_t *src, struct siginfo32 *dst)
 2646 {
 2647         bzero(dst, sizeof(*dst));
 2648         dst->si_signo = src->si_signo;
 2649         dst->si_errno = src->si_errno;
 2650         dst->si_code = src->si_code;
 2651         dst->si_pid = src->si_pid;
 2652         dst->si_uid = src->si_uid;
 2653         dst->si_status = src->si_status;
 2654         dst->si_addr = (uintptr_t)src->si_addr;
 2655         dst->si_value.sigval_int = src->si_value.sival_int;
 2656         dst->si_timerid = src->si_timerid;
 2657         dst->si_overrun = src->si_overrun;
 2658 }
 2659 
 2660 int
 2661 freebsd32_sigtimedwait(struct thread *td, struct freebsd32_sigtimedwait_args *uap)
 2662 {
 2663         struct timespec32 ts32;
 2664         struct timespec ts;
 2665         struct timespec *timeout;
 2666         sigset_t set;
 2667         ksiginfo_t ksi;
 2668         struct siginfo32 si32;
 2669         int error;
 2670 
 2671         if (uap->timeout) {
 2672                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
 2673                 if (error)
 2674                         return (error);
 2675                 ts.tv_sec = ts32.tv_sec;
 2676                 ts.tv_nsec = ts32.tv_nsec;
 2677                 timeout = &ts;
 2678         } else
 2679                 timeout = NULL;
 2680 
 2681         error = copyin(uap->set, &set, sizeof(set));
 2682         if (error)
 2683                 return (error);
 2684 
 2685         error = kern_sigtimedwait(td, set, &ksi, timeout);
 2686         if (error)
 2687                 return (error);
 2688 
 2689         if (uap->info) {
 2690                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
 2691                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
 2692         }
 2693 
 2694         if (error == 0)
 2695                 td->td_retval[0] = ksi.ksi_signo;
 2696         return (error);
 2697 }
 2698 
 2699 /*
 2700  * MPSAFE
 2701  */
 2702 int
 2703 freebsd32_sigwaitinfo(struct thread *td, struct freebsd32_sigwaitinfo_args *uap)
 2704 {
 2705         ksiginfo_t ksi;
 2706         struct siginfo32 si32;
 2707         sigset_t set;
 2708         int error;
 2709 
 2710         error = copyin(uap->set, &set, sizeof(set));
 2711         if (error)
 2712                 return (error);
 2713 
 2714         error = kern_sigtimedwait(td, set, &ksi, NULL);
 2715         if (error)
 2716                 return (error);
 2717 
 2718         if (uap->info) {
 2719                 siginfo_to_siginfo32(&ksi.ksi_info, &si32);
 2720                 error = copyout(&si32, uap->info, sizeof(struct siginfo32));
 2721         }       
 2722         if (error == 0)
 2723                 td->td_retval[0] = ksi.ksi_signo;
 2724         return (error);
 2725 }
 2726 
 2727 int
 2728 freebsd32_cpuset_setid(struct thread *td,
 2729     struct freebsd32_cpuset_setid_args *uap)
 2730 {
 2731         struct cpuset_setid_args ap;
 2732 
 2733         ap.which = uap->which;
 2734         ap.id = (uap->idlo | ((id_t)uap->idhi << 32));
 2735         ap.setid = uap->setid;
 2736 
 2737         return cpuset_setid(td, &ap);
 2738 }
 2739 
 2740 int
 2741 freebsd32_cpuset_getid(struct thread *td,
 2742     struct freebsd32_cpuset_getid_args *uap)
 2743 {
 2744         struct cpuset_getid_args ap;
 2745 
 2746         ap.level = uap->level;
 2747         ap.which = uap->which;
 2748         ap.id = (uap->idlo | ((id_t)uap->idhi << 32));
 2749         ap.setid = uap->setid;
 2750 
 2751         return cpuset_getid(td, &ap);
 2752 }
 2753 
 2754 int
 2755 freebsd32_cpuset_getaffinity(struct thread *td,
 2756     struct freebsd32_cpuset_getaffinity_args *uap)
 2757 {
 2758         struct cpuset_getaffinity_args ap;
 2759 
 2760         ap.level = uap->level;
 2761         ap.which = uap->which;
 2762         ap.id = (uap->idlo | ((id_t)uap->idhi << 32));
 2763         ap.cpusetsize = uap->cpusetsize;
 2764         ap.mask = uap->mask;
 2765 
 2766         return cpuset_getaffinity(td, &ap);
 2767 }
 2768 
 2769 int
 2770 freebsd32_cpuset_setaffinity(struct thread *td,
 2771     struct freebsd32_cpuset_setaffinity_args *uap)
 2772 {
 2773         struct cpuset_setaffinity_args ap;
 2774 
 2775         ap.level = uap->level;
 2776         ap.which = uap->which;
 2777         ap.id = (uap->idlo | ((id_t)uap->idhi << 32));
 2778         ap.cpusetsize = uap->cpusetsize;
 2779         ap.mask = uap->mask;
 2780 
 2781         return cpuset_setaffinity(td, &ap);
 2782 }
 2783 
 2784 int
 2785 freebsd32_nmount(struct thread *td,
 2786     struct freebsd32_nmount_args /* {
 2787         struct iovec *iovp;
 2788         unsigned int iovcnt;
 2789         int flags;
 2790     } */ *uap)
 2791 {
 2792         struct uio *auio;
 2793         struct iovec *iov;
 2794         int error, k;
 2795 
 2796         AUDIT_ARG(fflags, uap->flags);
 2797 
 2798         /*
 2799          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
 2800          * userspace to set this flag, but we must filter it out if we want
 2801          * MNT_UPDATE on the root file system to work.
 2802          * MNT_ROOTFS should only be set in the kernel in vfs_mountroot_try().
 2803          */
 2804         uap->flags &= ~MNT_ROOTFS;
 2805 
 2806         /*
 2807          * check that we have an even number of iovec's
 2808          * and that we have at least two options.
 2809          */
 2810         if ((uap->iovcnt & 1) || (uap->iovcnt < 4))
 2811                 return (EINVAL);
 2812 
 2813         error = freebsd32_copyinuio(uap->iovp, uap->iovcnt, &auio);
 2814         if (error)
 2815                 return (error);
 2816         for (iov = auio->uio_iov, k = 0; k < uap->iovcnt; ++k, ++iov) {
 2817                 if (iov->iov_len > MMAXOPTIONLEN) {
 2818                         free(auio, M_IOV);
 2819                         return (EINVAL);
 2820                 }
 2821         }
 2822 
 2823         error = vfs_donmount(td, uap->flags, auio);
 2824         free(auio, M_IOV);
 2825         return error;
 2826 }
 2827 
 2828 #if 0
 2829 int
 2830 freebsd32_xxx(struct thread *td, struct freebsd32_xxx_args *uap)
 2831 {
 2832         struct yyy32 *p32, s32;
 2833         struct yyy *p = NULL, s;
 2834         struct xxx_arg ap;
 2835         int error;
 2836 
 2837         if (uap->zzz) {
 2838                 error = copyin(uap->zzz, &s32, sizeof(s32));
 2839                 if (error)
 2840                         return (error);
 2841                 /* translate in */
 2842                 p = &s;
 2843         }
 2844         error = kern_xxx(td, p);
 2845         if (error)
 2846                 return (error);
 2847         if (uap->zzz) {
 2848                 /* translate out */
 2849                 error = copyout(&s32, p32, sizeof(s32));
 2850         }
 2851         return (error);
 2852 }
 2853 #endif
 2854 
 2855 int
 2856 syscall32_register(int *offset, struct sysent *new_sysent,
 2857     struct sysent *old_sysent)
 2858 {
 2859         if (*offset == NO_SYSCALL) {
 2860                 int i;
 2861 
 2862                 for (i = 1; i < SYS_MAXSYSCALL; ++i)
 2863                         if (freebsd32_sysent[i].sy_call ==
 2864                             (sy_call_t *)lkmnosys)
 2865                                 break;
 2866                 if (i == SYS_MAXSYSCALL)
 2867                         return (ENFILE);
 2868                 *offset = i;
 2869         } else if (*offset < 0 || *offset >= SYS_MAXSYSCALL)
 2870                 return (EINVAL);
 2871         else if (freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmnosys &&
 2872             freebsd32_sysent[*offset].sy_call != (sy_call_t *)lkmressys)
 2873                 return (EEXIST);
 2874 
 2875         *old_sysent = freebsd32_sysent[*offset];
 2876         freebsd32_sysent[*offset] = *new_sysent;
 2877         return 0;
 2878 }
 2879 
 2880 int
 2881 syscall32_deregister(int *offset, struct sysent *old_sysent)
 2882 {
 2883 
 2884         if (*offset)
 2885                 freebsd32_sysent[*offset] = *old_sysent;
 2886         return 0;
 2887 }
 2888 
 2889 int
 2890 syscall32_module_handler(struct module *mod, int what, void *arg)
 2891 {
 2892         struct syscall_module_data *data = (struct syscall_module_data*)arg;
 2893         modspecific_t ms;
 2894         int error;
 2895 
 2896         switch (what) {
 2897         case MOD_LOAD:
 2898                 error = syscall32_register(data->offset, data->new_sysent,
 2899                     &data->old_sysent);
 2900                 if (error) {
 2901                         /* Leave a mark so we know to safely unload below. */
 2902                         data->offset = NULL;
 2903                         return error;
 2904                 }
 2905                 ms.intval = *data->offset;
 2906                 MOD_XLOCK;
 2907                 module_setspecific(mod, &ms);
 2908                 MOD_XUNLOCK;
 2909                 if (data->chainevh)
 2910                         error = data->chainevh(mod, what, data->chainarg);
 2911                 return (error);
 2912         case MOD_UNLOAD:
 2913                 /*
 2914                  * MOD_LOAD failed, so just return without calling the
 2915                  * chained handler since we didn't pass along the MOD_LOAD
 2916                  * event.
 2917                  */
 2918                 if (data->offset == NULL)
 2919                         return (0);
 2920                 if (data->chainevh) {
 2921                         error = data->chainevh(mod, what, data->chainarg);
 2922                         if (error)
 2923                                 return (error);
 2924                 }
 2925                 error = syscall32_deregister(data->offset, &data->old_sysent);
 2926                 return (error);
 2927         default:
 2928                 error = EOPNOTSUPP;
 2929                 if (data->chainevh)
 2930                         error = data->chainevh(mod, what, data->chainarg);
 2931                 return (error);
 2932         }
 2933 }

Cache object: e97e1bc92306e9679d0e8e2e08cb80ed


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