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

Cache object: 99427500cfaea3ebaeba88a0f609f3e6


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