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

Cache object: 0d668212dcd36ff49b2782c3ec41a858


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