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/linux/common/linux_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 /*      $NetBSD: linux_misc.c,v 1.201.6.1 2010/03/17 02:59:52 snj Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1995, 1998, 1999, 2008 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
    9  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30  * POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * Linux compatibility module. Try to deal with various Linux system calls.
   35  */
   36 
   37 /*
   38  * These functions have been moved to multiarch to allow
   39  * selection of which machines include them to be
   40  * determined by the individual files.linux_<arch> files.
   41  *
   42  * Function in multiarch:
   43  *      linux_sys_break                 : linux_break.c
   44  *      linux_sys_alarm                 : linux_misc_notalpha.c
   45  *      linux_sys_getresgid             : linux_misc_notalpha.c
   46  *      linux_sys_nice                  : linux_misc_notalpha.c
   47  *      linux_sys_readdir               : linux_misc_notalpha.c
   48  *      linux_sys_setresgid             : linux_misc_notalpha.c
   49  *      linux_sys_time                  : linux_misc_notalpha.c
   50  *      linux_sys_utime                 : linux_misc_notalpha.c
   51  *      linux_sys_waitpid               : linux_misc_notalpha.c
   52  *      linux_sys_old_mmap              : linux_oldmmap.c
   53  *      linux_sys_oldolduname           : linux_oldolduname.c
   54  *      linux_sys_oldselect             : linux_oldselect.c
   55  *      linux_sys_olduname              : linux_olduname.c
   56  *      linux_sys_pipe                  : linux_pipe.c
   57  */
   58 
   59 #include <sys/cdefs.h>
   60 __KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.201.6.1 2010/03/17 02:59:52 snj Exp $");
   61 
   62 #if defined(_KERNEL_OPT)
   63 #include "opt_ptrace.h"
   64 #endif
   65 
   66 #include <sys/param.h>
   67 #include <sys/systm.h>
   68 #include <sys/namei.h>
   69 #include <sys/proc.h>
   70 #include <sys/dirent.h>
   71 #include <sys/file.h>
   72 #include <sys/stat.h>
   73 #include <sys/filedesc.h>
   74 #include <sys/ioctl.h>
   75 #include <sys/kernel.h>
   76 #include <sys/malloc.h>
   77 #include <sys/mbuf.h>
   78 #include <sys/mman.h>
   79 #include <sys/mount.h>
   80 #include <sys/prot.h>
   81 #include <sys/reboot.h>
   82 #include <sys/resource.h>
   83 #include <sys/resourcevar.h>
   84 #include <sys/select.h>
   85 #include <sys/signal.h>
   86 #include <sys/signalvar.h>
   87 #include <sys/socket.h>
   88 #include <sys/time.h>
   89 #include <sys/times.h>
   90 #include <sys/vnode.h>
   91 #include <sys/uio.h>
   92 #include <sys/wait.h>
   93 #include <sys/utsname.h>
   94 #include <sys/unistd.h>
   95 #include <sys/vfs_syscalls.h>
   96 #include <sys/swap.h>           /* for SWAP_ON */
   97 #include <sys/sysctl.h>         /* for KERN_DOMAINNAME */
   98 #include <sys/kauth.h>
   99 
  100 #include <sys/ptrace.h>
  101 #include <machine/ptrace.h>
  102 
  103 #include <sys/syscall.h>
  104 #include <sys/syscallargs.h>
  105 
  106 #include <compat/linux/common/linux_machdep.h>
  107 #include <compat/linux/common/linux_types.h>
  108 #include <compat/linux/common/linux_signal.h>
  109 #include <compat/linux/common/linux_ipc.h>
  110 #include <compat/linux/common/linux_sem.h>
  111 
  112 #include <compat/linux/linux_syscallargs.h>
  113 
  114 #include <compat/linux/common/linux_fcntl.h>
  115 #include <compat/linux/common/linux_mmap.h>
  116 #include <compat/linux/common/linux_dirent.h>
  117 #include <compat/linux/common/linux_util.h>
  118 #include <compat/linux/common/linux_misc.h>
  119 #ifndef COMPAT_LINUX32
  120 #include <compat/linux/common/linux_statfs.h>
  121 #include <compat/linux/common/linux_limit.h>
  122 #endif
  123 #include <compat/linux/common/linux_ptrace.h>
  124 #include <compat/linux/common/linux_reboot.h>
  125 #include <compat/linux/common/linux_emuldata.h>
  126 
  127 #ifndef COMPAT_LINUX32
  128 const int linux_ptrace_request_map[] = {
  129         LINUX_PTRACE_TRACEME,   PT_TRACE_ME,
  130         LINUX_PTRACE_PEEKTEXT,  PT_READ_I,
  131         LINUX_PTRACE_PEEKDATA,  PT_READ_D,
  132         LINUX_PTRACE_POKETEXT,  PT_WRITE_I,
  133         LINUX_PTRACE_POKEDATA,  PT_WRITE_D,
  134         LINUX_PTRACE_CONT,      PT_CONTINUE,
  135         LINUX_PTRACE_KILL,      PT_KILL,
  136         LINUX_PTRACE_ATTACH,    PT_ATTACH,
  137         LINUX_PTRACE_DETACH,    PT_DETACH,
  138 # ifdef PT_STEP
  139         LINUX_PTRACE_SINGLESTEP,        PT_STEP,
  140 # endif
  141         LINUX_PTRACE_SYSCALL,   PT_SYSCALL,
  142         -1
  143 };
  144 
  145 const struct linux_mnttypes linux_fstypes[] = {
  146         { MOUNT_FFS,            LINUX_DEFAULT_SUPER_MAGIC       },
  147         { MOUNT_NFS,            LINUX_NFS_SUPER_MAGIC           },
  148         { MOUNT_MFS,            LINUX_DEFAULT_SUPER_MAGIC       },
  149         { MOUNT_MSDOS,          LINUX_MSDOS_SUPER_MAGIC         },
  150         { MOUNT_LFS,            LINUX_DEFAULT_SUPER_MAGIC       },
  151         { MOUNT_FDESC,          LINUX_DEFAULT_SUPER_MAGIC       },
  152         { MOUNT_PORTAL,         LINUX_DEFAULT_SUPER_MAGIC       },
  153         { MOUNT_NULL,           LINUX_DEFAULT_SUPER_MAGIC       },
  154         { MOUNT_OVERLAY,        LINUX_DEFAULT_SUPER_MAGIC       },
  155         { MOUNT_UMAP,           LINUX_DEFAULT_SUPER_MAGIC       },
  156         { MOUNT_KERNFS,         LINUX_DEFAULT_SUPER_MAGIC       },
  157         { MOUNT_PROCFS,         LINUX_PROC_SUPER_MAGIC          },
  158         { MOUNT_AFS,            LINUX_DEFAULT_SUPER_MAGIC       },
  159         { MOUNT_CD9660,         LINUX_ISOFS_SUPER_MAGIC         },
  160         { MOUNT_UNION,          LINUX_DEFAULT_SUPER_MAGIC       },
  161         { MOUNT_ADOSFS,         LINUX_ADFS_SUPER_MAGIC          },
  162         { MOUNT_EXT2FS,         LINUX_EXT2_SUPER_MAGIC          },
  163         { MOUNT_CFS,            LINUX_DEFAULT_SUPER_MAGIC       },
  164         { MOUNT_CODA,           LINUX_CODA_SUPER_MAGIC          },
  165         { MOUNT_FILECORE,       LINUX_DEFAULT_SUPER_MAGIC       },
  166         { MOUNT_NTFS,           LINUX_DEFAULT_SUPER_MAGIC       },
  167         { MOUNT_SMBFS,          LINUX_SMB_SUPER_MAGIC           },
  168         { MOUNT_PTYFS,          LINUX_DEVPTS_SUPER_MAGIC        },
  169         { MOUNT_TMPFS,          LINUX_TMPFS_SUPER_MAGIC         }
  170 };
  171 const int linux_fstypes_cnt = sizeof(linux_fstypes) / sizeof(linux_fstypes[0]);
  172 
  173 # ifdef DEBUG_LINUX
  174 #define DPRINTF(a)      uprintf a
  175 # else
  176 #define DPRINTF(a)
  177 # endif
  178 
  179 /* Local linux_misc.c functions: */
  180 static void linux_to_bsd_mmap_args(struct sys_mmap_args *,
  181     const struct linux_sys_mmap_args *);
  182 static int linux_mmap(struct lwp *, const struct linux_sys_mmap_args *,
  183     register_t *, off_t);
  184 
  185 
  186 /*
  187  * The information on a terminated (or stopped) process needs
  188  * to be converted in order for Linux binaries to get a valid signal
  189  * number out of it.
  190  */
  191 int
  192 bsd_to_linux_wstat(int st)
  193 {
  194 
  195         int sig;
  196 
  197         if (WIFSIGNALED(st)) {
  198                 sig = WTERMSIG(st);
  199                 if (sig >= 0 && sig < NSIG)
  200                         st= (st & ~0177) | native_to_linux_signo[sig];
  201         } else if (WIFSTOPPED(st)) {
  202                 sig = WSTOPSIG(st);
  203                 if (sig >= 0 && sig < NSIG)
  204                         st = (st & ~0xff00) |
  205                             (native_to_linux_signo[sig] << 8);
  206         }
  207         return st;
  208 }
  209 
  210 /*
  211  * wait4(2).  Passed on to the NetBSD call, surrounded by code to
  212  * reserve some space for a NetBSD-style wait status, and converting
  213  * it to what Linux wants.
  214  */
  215 int
  216 linux_sys_wait4(struct lwp *l, const struct linux_sys_wait4_args *uap, register_t *retval)
  217 {
  218         /* {
  219                 syscallarg(int) pid;
  220                 syscallarg(int *) status;
  221                 syscallarg(int) options;
  222                 syscallarg(struct rusage *) rusage;
  223         } */
  224         int error, status, options, linux_options, was_zombie;
  225         struct rusage ru;
  226         int pid = SCARG(uap, pid);
  227         proc_t *p;
  228 
  229         linux_options = SCARG(uap, options);
  230         options = WOPTSCHECKED;
  231         if (linux_options & ~(LINUX_WAIT4_KNOWNFLAGS))
  232                 return (EINVAL);
  233 
  234         if (linux_options & LINUX_WAIT4_WNOHANG)
  235                 options |= WNOHANG;
  236         if (linux_options & LINUX_WAIT4_WUNTRACED)
  237                 options |= WUNTRACED;
  238         if (linux_options & LINUX_WAIT4_WALL)
  239                 options |= WALLSIG;
  240         if (linux_options & LINUX_WAIT4_WCLONE)
  241                 options |= WALTSIG;
  242 # ifdef DIAGNOSTIC
  243         if (linux_options & LINUX_WAIT4_WNOTHREAD)
  244                 printf("WARNING: %s: linux process %d.%d called "
  245                        "waitpid with __WNOTHREAD set!",
  246                        __FILE__, l->l_proc->p_pid, l->l_lid);
  247 
  248 # endif
  249 
  250         error = do_sys_wait(l, &pid, &status, options,
  251             SCARG(uap, rusage) != NULL ? &ru : NULL, &was_zombie);
  252 
  253         retval[0] = pid;
  254         if (pid == 0)
  255                 return error;
  256 
  257         p = curproc;
  258         mutex_enter(p->p_lock);
  259         sigdelset(&p->p_sigpend.sp_set, SIGCHLD); /* XXXAD ksiginfo leak */
  260         mutex_exit(p->p_lock);
  261 
  262         if (SCARG(uap, rusage) != NULL)
  263                 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
  264 
  265         if (error == 0 && SCARG(uap, status) != NULL) {
  266                 status = bsd_to_linux_wstat(status);
  267                 error = copyout(&status, SCARG(uap, status), sizeof status);
  268         }
  269 
  270         return error;
  271 }
  272 
  273 /*
  274  * Linux brk(2). The check if the new address is >= the old one is
  275  * done in the kernel in Linux. NetBSD does it in the library.
  276  */
  277 int
  278 linux_sys_brk(struct lwp *l, const struct linux_sys_brk_args *uap, register_t *retval)
  279 {
  280         /* {
  281                 syscallarg(char *) nsize;
  282         } */
  283         struct proc *p = l->l_proc;
  284         char *nbrk = SCARG(uap, nsize);
  285         struct sys_obreak_args oba;
  286         struct vmspace *vm = p->p_vmspace;
  287         struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
  288 
  289         SCARG(&oba, nsize) = nbrk;
  290 
  291         if ((void *) nbrk > vm->vm_daddr && sys_obreak(l, &oba, retval) == 0)
  292                 ed->s->p_break = (char*)nbrk;
  293         else
  294                 nbrk = ed->s->p_break;
  295 
  296         retval[0] = (register_t)nbrk;
  297 
  298         return 0;
  299 }
  300 
  301 /*
  302  * Implement the fs stat functions. Straightforward.
  303  */
  304 int
  305 linux_sys_statfs(struct lwp *l, const struct linux_sys_statfs_args *uap, register_t *retval)
  306 {
  307         /* {
  308                 syscallarg(const char *) path;
  309                 syscallarg(struct linux_statfs *) sp;
  310         } */
  311         struct statvfs *sb;
  312         struct linux_statfs ltmp;
  313         int error;
  314 
  315         sb = STATVFSBUF_GET();
  316         error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb);
  317         if (error == 0) {
  318                 bsd_to_linux_statfs(sb, &ltmp);
  319                 error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
  320         }
  321         STATVFSBUF_PUT(sb);
  322 
  323         return error;
  324 }
  325 
  326 int
  327 linux_sys_fstatfs(struct lwp *l, const struct linux_sys_fstatfs_args *uap, register_t *retval)
  328 {
  329         /* {
  330                 syscallarg(int) fd;
  331                 syscallarg(struct linux_statfs *) sp;
  332         } */
  333         struct statvfs *sb;
  334         struct linux_statfs ltmp;
  335         int error;
  336 
  337         sb = STATVFSBUF_GET();
  338         error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
  339         if (error == 0) {
  340                 bsd_to_linux_statfs(sb, &ltmp);
  341                 error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
  342         }
  343         STATVFSBUF_PUT(sb);
  344 
  345         return error;
  346 }
  347 
  348 /*
  349  * uname(). Just copy the info from the various strings stored in the
  350  * kernel, and put it in the Linux utsname structure. That structure
  351  * is almost the same as the NetBSD one, only it has fields 65 characters
  352  * long, and an extra domainname field.
  353  */
  354 int
  355 linux_sys_uname(struct lwp *l, const struct linux_sys_uname_args *uap, register_t *retval)
  356 {
  357         /* {
  358                 syscallarg(struct linux_utsname *) up;
  359         } */
  360         struct linux_utsname luts;
  361 
  362         strlcpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
  363         strlcpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
  364         strlcpy(luts.l_release, linux_release, sizeof(luts.l_release));
  365         strlcpy(luts.l_version, linux_version, sizeof(luts.l_version));
  366         strlcpy(luts.l_machine, LINUX_UNAME_ARCH, sizeof(luts.l_machine));
  367         strlcpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
  368 
  369         return copyout(&luts, SCARG(uap, up), sizeof(luts));
  370 }
  371 
  372 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
  373 /* Used indirectly on: arm, i386, m68k */
  374 
  375 /*
  376  * New type Linux mmap call.
  377  * Only called directly on machines with >= 6 free regs.
  378  */
  379 int
  380 linux_sys_mmap(struct lwp *l, const struct linux_sys_mmap_args *uap, register_t *retval)
  381 {
  382         /* {
  383                 syscallarg(unsigned long) addr;
  384                 syscallarg(size_t) len;
  385                 syscallarg(int) prot;
  386                 syscallarg(int) flags;
  387                 syscallarg(int) fd;
  388                 syscallarg(linux_off_t) offset;
  389         } */
  390 
  391         if (SCARG(uap, offset) & PAGE_MASK)
  392                 return EINVAL;
  393 
  394         return linux_mmap(l, uap, retval, SCARG(uap, offset));
  395 }
  396 
  397 /*
  398  * Guts of most architectures' mmap64() implementations.  This shares
  399  * its list of arguments with linux_sys_mmap().
  400  *
  401  * The difference in linux_sys_mmap2() is that "offset" is actually
  402  * (offset / pagesize), not an absolute byte count.  This translation
  403  * to pagesize offsets is done inside glibc between the mmap64() call
  404  * point, and the actual syscall.
  405  */
  406 int
  407 linux_sys_mmap2(struct lwp *l, const struct linux_sys_mmap2_args *uap, register_t *retval)
  408 {
  409         /* {
  410                 syscallarg(unsigned long) addr;
  411                 syscallarg(size_t) len;
  412                 syscallarg(int) prot;
  413                 syscallarg(int) flags;
  414                 syscallarg(int) fd;
  415                 syscallarg(linux_off_t) offset;
  416         } */
  417 
  418         return linux_mmap(l, uap, retval,
  419             ((off_t)SCARG(uap, offset)) << PAGE_SHIFT);
  420 }
  421 
  422 /*
  423  * Massage arguments and call system mmap(2).
  424  */
  425 static int
  426 linux_mmap(struct lwp *l, const struct linux_sys_mmap_args *uap, register_t *retval, off_t offset)
  427 {
  428         struct sys_mmap_args cma;
  429         int error;
  430         size_t mmoff=0;
  431 
  432         linux_to_bsd_mmap_args(&cma, uap);
  433         SCARG(&cma, pos) = offset;
  434 
  435         if (SCARG(uap, flags) & LINUX_MAP_GROWSDOWN) {
  436                 /*
  437                  * Request for stack-like memory segment. On linux, this
  438                  * works by mmap()ping (small) segment, which is automatically
  439                  * extended when page fault happens below the currently
  440                  * allocated area. We emulate this by allocating (typically
  441                  * bigger) segment sized at current stack size limit, and
  442                  * offsetting the requested and returned address accordingly.
  443                  * Since physical pages are only allocated on-demand, this
  444                  * is effectively identical.
  445                  */
  446                 rlim_t ssl = l->l_proc->p_rlimit[RLIMIT_STACK].rlim_cur;
  447 
  448                 if (SCARG(&cma, len) < ssl) {
  449                         /* Compute the address offset */
  450                         mmoff = round_page(ssl) - SCARG(uap, len);
  451 
  452                         if (SCARG(&cma, addr))
  453                                 SCARG(&cma, addr) = (char *)SCARG(&cma, addr) - mmoff;
  454 
  455                         SCARG(&cma, len) = (size_t) ssl;
  456                 }
  457         }
  458 
  459         error = sys_mmap(l, &cma, retval);
  460         if (error)
  461                 return (error);
  462 
  463         /* Shift the returned address for stack-like segment if necessary */
  464         retval[0] += mmoff;
  465 
  466         return (0);
  467 }
  468 
  469 static void
  470 linux_to_bsd_mmap_args(struct sys_mmap_args *cma, const struct linux_sys_mmap_args *uap)
  471 {
  472         int flags = MAP_TRYFIXED, fl = SCARG(uap, flags);
  473 
  474         flags |= cvtto_bsd_mask(fl, LINUX_MAP_SHARED, MAP_SHARED);
  475         flags |= cvtto_bsd_mask(fl, LINUX_MAP_PRIVATE, MAP_PRIVATE);
  476         flags |= cvtto_bsd_mask(fl, LINUX_MAP_FIXED, MAP_FIXED);
  477         flags |= cvtto_bsd_mask(fl, LINUX_MAP_ANON, MAP_ANON);
  478         /* XXX XAX ERH: Any other flags here?  There are more defined... */
  479 
  480         SCARG(cma, addr) = (void *)SCARG(uap, addr);
  481         SCARG(cma, len) = SCARG(uap, len);
  482         SCARG(cma, prot) = SCARG(uap, prot);
  483         if (SCARG(cma, prot) & VM_PROT_WRITE) /* XXX */
  484                 SCARG(cma, prot) |= VM_PROT_READ;
  485         SCARG(cma, flags) = flags;
  486         SCARG(cma, fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
  487         SCARG(cma, pad) = 0;
  488 }
  489 
  490 #define LINUX_MREMAP_MAYMOVE    1
  491 #define LINUX_MREMAP_FIXED      2
  492 
  493 int
  494 linux_sys_mremap(struct lwp *l, const struct linux_sys_mremap_args *uap, register_t *retval)
  495 {
  496         /* {
  497                 syscallarg(void *) old_address;
  498                 syscallarg(size_t) old_size;
  499                 syscallarg(size_t) new_size;
  500                 syscallarg(u_long) flags;
  501         } */
  502 
  503         struct proc *p;
  504         struct vm_map *map;
  505         vaddr_t oldva;
  506         vaddr_t newva;
  507         size_t oldsize;
  508         size_t newsize;
  509         int flags;
  510         int uvmflags;
  511         int error;
  512 
  513         flags = SCARG(uap, flags);
  514         oldva = (vaddr_t)SCARG(uap, old_address);
  515         oldsize = round_page(SCARG(uap, old_size));
  516         newsize = round_page(SCARG(uap, new_size));
  517         if ((flags & ~(LINUX_MREMAP_FIXED|LINUX_MREMAP_MAYMOVE)) != 0) {
  518                 error = EINVAL;
  519                 goto done;
  520         }
  521         if ((flags & LINUX_MREMAP_FIXED) != 0) {
  522                 if ((flags & LINUX_MREMAP_MAYMOVE) == 0) {
  523                         error = EINVAL;
  524                         goto done;
  525                 }
  526 #if 0 /* notyet */
  527                 newva = SCARG(uap, new_address);
  528                 uvmflags = MAP_FIXED;
  529 #else /* notyet */
  530                 error = EOPNOTSUPP;
  531                 goto done;
  532 #endif /* notyet */
  533         } else if ((flags & LINUX_MREMAP_MAYMOVE) != 0) {
  534                 uvmflags = 0;
  535         } else {
  536                 newva = oldva;
  537                 uvmflags = MAP_FIXED;
  538         }
  539         p = l->l_proc;
  540         map = &p->p_vmspace->vm_map;
  541         error = uvm_mremap(map, oldva, oldsize, map, &newva, newsize, p,
  542             uvmflags);
  543 
  544 done:
  545         *retval = (error != 0) ? 0 : (register_t)newva;
  546         return error;
  547 }
  548 
  549 int
  550 linux_sys_mprotect(struct lwp *l, const struct linux_sys_mprotect_args *uap, register_t *retval)
  551 {
  552         /* {
  553                 syscallarg(const void *) start;
  554                 syscallarg(unsigned long) len;
  555                 syscallarg(int) prot;
  556         } */
  557         struct vm_map_entry *entry;
  558         struct vm_map *map;
  559         struct proc *p;
  560         vaddr_t end, start, len, stacklim;
  561         int prot, grows;
  562 
  563         start = (vaddr_t)SCARG(uap, start);
  564         len = round_page(SCARG(uap, len));
  565         prot = SCARG(uap, prot);
  566         grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
  567         prot &= ~grows;
  568         end = start + len;
  569 
  570         if (start & PAGE_MASK)
  571                 return EINVAL;
  572         if (end < start)
  573                 return EINVAL;
  574         if (end == start)
  575                 return 0;
  576 
  577         if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
  578                 return EINVAL;
  579         if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
  580                 return EINVAL;
  581 
  582         p = l->l_proc;
  583         map = &p->p_vmspace->vm_map;
  584         vm_map_lock(map);
  585 # ifdef notdef
  586         VM_MAP_RANGE_CHECK(map, start, end);
  587 # endif
  588         if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
  589                 vm_map_unlock(map);
  590                 return ENOMEM;
  591         }
  592 
  593         /*
  594          * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
  595          */
  596 
  597         stacklim = (vaddr_t)p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
  598         if (grows & LINUX_PROT_GROWSDOWN) {
  599                 if (USRSTACK - stacklim <= start && start < USRSTACK) {
  600                         start = USRSTACK - stacklim;
  601                 } else {
  602                         start = entry->start;
  603                 }
  604         } else if (grows & LINUX_PROT_GROWSUP) {
  605                 if (USRSTACK <= end && end < USRSTACK + stacklim) {
  606                         end = USRSTACK + stacklim;
  607                 } else {
  608                         end = entry->end;
  609                 }
  610         }
  611         vm_map_unlock(map);
  612         return uvm_map_protect(map, start, end, prot, FALSE);
  613 }
  614 
  615 /*
  616  * This code is partly stolen from src/lib/libc/compat-43/times.c
  617  */
  618 
  619 #define CONVTCK(r)      (r.tv_sec * hz + r.tv_usec / (1000000 / hz))
  620 
  621 int
  622 linux_sys_times(struct lwp *l, const struct linux_sys_times_args *uap, register_t *retval)
  623 {
  624         /* {
  625                 syscallarg(struct times *) tms;
  626         } */
  627         struct proc *p = l->l_proc;
  628         struct timeval t;
  629         int error;
  630 
  631         if (SCARG(uap, tms)) {
  632                 struct linux_tms ltms;
  633                 struct rusage ru;
  634 
  635                 mutex_enter(p->p_lock);
  636                 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
  637                 ltms.ltms_utime = CONVTCK(ru.ru_utime);
  638                 ltms.ltms_stime = CONVTCK(ru.ru_stime);
  639                 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
  640                 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
  641                 mutex_exit(p->p_lock);
  642 
  643                 if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
  644                         return error;
  645         }
  646 
  647         getmicrouptime(&t);
  648 
  649         retval[0] = ((linux_clock_t)(CONVTCK(t)));
  650         return 0;
  651 }
  652 
  653 #undef CONVTCK
  654 
  655 /*
  656  * Linux 'readdir' call. This code is mostly taken from the
  657  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
  658  * an attempt has been made to keep it a little cleaner (failing
  659  * miserably, because of the cruft needed if count 1 is passed).
  660  *
  661  * The d_off field should contain the offset of the next valid entry,
  662  * but in Linux it has the offset of the entry itself. We emulate
  663  * that bug here.
  664  *
  665  * Read in BSD-style entries, convert them, and copy them out.
  666  *
  667  * Note that this doesn't handle union-mounted filesystems.
  668  */
  669 int
  670 linux_sys_getdents(struct lwp *l, const struct linux_sys_getdents_args *uap, register_t *retval)
  671 {
  672         /* {
  673                 syscallarg(int) fd;
  674                 syscallarg(struct linux_dirent *) dent;
  675                 syscallarg(unsigned int) count;
  676         } */
  677         struct dirent *bdp;
  678         struct vnode *vp;
  679         char *inp, *tbuf;               /* BSD-format */
  680         int len, reclen;                /* BSD-format */
  681         char *outp;                     /* Linux-format */
  682         int resid, linux_reclen = 0;    /* Linux-format */
  683         struct file *fp;
  684         struct uio auio;
  685         struct iovec aiov;
  686         struct linux_dirent idb;
  687         off_t off;              /* true file offset */
  688         int buflen, error, eofflag, nbytes, oldcall;
  689         struct vattr va;
  690         off_t *cookiebuf = NULL, *cookie;
  691         int ncookies;
  692 
  693         /* fd_getvnode() will use the descriptor for us */
  694         if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
  695                 return (error);
  696 
  697         if ((fp->f_flag & FREAD) == 0) {
  698                 error = EBADF;
  699                 goto out1;
  700         }
  701 
  702         vp = (struct vnode *)fp->f_data;
  703         if (vp->v_type != VDIR) {
  704                 error = EINVAL;
  705                 goto out1;
  706         }
  707 
  708         if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
  709                 goto out1;
  710 
  711         nbytes = SCARG(uap, count);
  712         if (nbytes == 1) {      /* emulating old, broken behaviour */
  713                 nbytes = sizeof (idb);
  714                 buflen = max(va.va_blocksize, nbytes);
  715                 oldcall = 1;
  716         } else {
  717                 buflen = min(MAXBSIZE, nbytes);
  718                 if (buflen < va.va_blocksize)
  719                         buflen = va.va_blocksize;
  720                 oldcall = 0;
  721         }
  722         tbuf = malloc(buflen, M_TEMP, M_WAITOK);
  723 
  724         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  725         off = fp->f_offset;
  726 again:
  727         aiov.iov_base = tbuf;
  728         aiov.iov_len = buflen;
  729         auio.uio_iov = &aiov;
  730         auio.uio_iovcnt = 1;
  731         auio.uio_rw = UIO_READ;
  732         auio.uio_resid = buflen;
  733         auio.uio_offset = off;
  734         UIO_SETUP_SYSSPACE(&auio);
  735         /*
  736          * First we read into the malloc'ed buffer, then
  737          * we massage it into user space, one record at a time.
  738          */
  739         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
  740             &ncookies);
  741         if (error)
  742                 goto out;
  743 
  744         inp = tbuf;
  745         outp = (void *)SCARG(uap, dent);
  746         resid = nbytes;
  747         if ((len = buflen - auio.uio_resid) == 0)
  748                 goto eof;
  749 
  750         for (cookie = cookiebuf; len > 0; len -= reclen) {
  751                 bdp = (struct dirent *)inp;
  752                 reclen = bdp->d_reclen;
  753                 if (reclen & 3)
  754                         panic("linux_readdir");
  755                 if (bdp->d_fileno == 0) {
  756                         inp += reclen;  /* it is a hole; squish it out */
  757                         if (cookie)
  758                                 off = *cookie++;
  759                         else
  760                                 off += reclen;
  761                         continue;
  762                 }
  763                 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
  764                 if (reclen > len || resid < linux_reclen) {
  765                         /* entry too big for buffer, so just stop */
  766                         outp++;
  767                         break;
  768                 }
  769                 /*
  770                  * Massage in place to make a Linux-shaped dirent (otherwise
  771                  * we have to worry about touching user memory outside of
  772                  * the copyout() call).
  773                  */
  774                 idb.d_ino = bdp->d_fileno;
  775                 /*
  776                  * The old readdir() call misuses the offset and reclen fields.
  777                  */
  778                 if (oldcall) {
  779                         idb.d_off = (linux_off_t)linux_reclen;
  780                         idb.d_reclen = (u_short)bdp->d_namlen;
  781                 } else {
  782                         if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
  783                                 compat_offseterr(vp, "linux_getdents");
  784                                 error = EINVAL;
  785                                 goto out;
  786                         }
  787                         idb.d_off = (linux_off_t)off;
  788                         idb.d_reclen = (u_short)linux_reclen;
  789                 }
  790                 strcpy(idb.d_name, bdp->d_name);
  791                 if ((error = copyout((void *)&idb, outp, linux_reclen)))
  792                         goto out;
  793                 /* advance past this real entry */
  794                 inp += reclen;
  795                 if (cookie)
  796                         off = *cookie++; /* each entry points to itself */
  797                 else
  798                         off += reclen;
  799                 /* advance output past Linux-shaped entry */
  800                 outp += linux_reclen;
  801                 resid -= linux_reclen;
  802                 if (oldcall)
  803                         break;
  804         }
  805 
  806         /* if we squished out the whole block, try again */
  807         if (outp == (void *)SCARG(uap, dent)) {
  808                 if (cookiebuf)
  809                         free(cookiebuf, M_TEMP);
  810                 cookiebuf = NULL;
  811                 goto again;
  812         }
  813         fp->f_offset = off;     /* update the vnode offset */
  814 
  815         if (oldcall)
  816                 nbytes = resid + linux_reclen;
  817 
  818 eof:
  819         *retval = nbytes - resid;
  820 out:
  821         VOP_UNLOCK(vp, 0);
  822         if (cookiebuf)
  823                 free(cookiebuf, M_TEMP);
  824         free(tbuf, M_TEMP);
  825 out1:
  826         fd_putfile(SCARG(uap, fd));
  827         return error;
  828 }
  829 
  830 /*
  831  * Even when just using registers to pass arguments to syscalls you can
  832  * have 5 of them on the i386. So this newer version of select() does
  833  * this.
  834  */
  835 int
  836 linux_sys_select(struct lwp *l, const struct linux_sys_select_args *uap, register_t *retval)
  837 {
  838         /* {
  839                 syscallarg(int) nfds;
  840                 syscallarg(fd_set *) readfds;
  841                 syscallarg(fd_set *) writefds;
  842                 syscallarg(fd_set *) exceptfds;
  843                 syscallarg(struct timeval *) timeout;
  844         } */
  845 
  846         return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
  847             SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
  848 }
  849 
  850 /*
  851  * Common code for the old and new versions of select(). A couple of
  852  * things are important:
  853  * 1) return the amount of time left in the 'timeout' parameter
  854  * 2) select never returns ERESTART on Linux, always return EINTR
  855  */
  856 int
  857 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
  858         struct lwp *l;
  859         register_t *retval;
  860         int nfds;
  861         fd_set *readfds, *writefds, *exceptfds;
  862         struct timeval *timeout;
  863 {
  864         struct timeval tv0, tv1, utv, *tv = NULL;
  865         int error;
  866 
  867         /*
  868          * Store current time for computation of the amount of
  869          * time left.
  870          */
  871         if (timeout) {
  872                 if ((error = copyin(timeout, &utv, sizeof(utv))))
  873                         return error;
  874                 if (itimerfix(&utv)) {
  875                         /*
  876                          * The timeval was invalid.  Convert it to something
  877                          * valid that will act as it does under Linux.
  878                          */
  879                         utv.tv_sec += utv.tv_usec / 1000000;
  880                         utv.tv_usec %= 1000000;
  881                         if (utv.tv_usec < 0) {
  882                                 utv.tv_sec -= 1;
  883                                 utv.tv_usec += 1000000;
  884                         }
  885                         if (utv.tv_sec < 0)
  886                                 timerclear(&utv);
  887                 }
  888                 tv = &utv;
  889                 microtime(&tv0);
  890         }
  891 
  892         error = selcommon(l, retval, nfds, readfds, writefds, exceptfds,
  893             tv, NULL);
  894 
  895         if (error) {
  896                 /*
  897                  * See fs/select.c in the Linux kernel.  Without this,
  898                  * Maelstrom doesn't work.
  899                  */
  900                 if (error == ERESTART)
  901                         error = EINTR;
  902                 return error;
  903         }
  904 
  905         if (timeout) {
  906                 if (*retval) {
  907                         /*
  908                          * Compute how much time was left of the timeout,
  909                          * by subtracting the current time and the time
  910                          * before we started the call, and subtracting
  911                          * that result from the user-supplied value.
  912                          */
  913                         microtime(&tv1);
  914                         timersub(&tv1, &tv0, &tv1);
  915                         timersub(&utv, &tv1, &utv);
  916                         if (utv.tv_sec < 0)
  917                                 timerclear(&utv);
  918                 } else
  919                         timerclear(&utv);
  920                 if ((error = copyout(&utv, timeout, sizeof(utv))))
  921                         return error;
  922         }
  923 
  924         return 0;
  925 }
  926 
  927 /*
  928  * Set the 'personality' (emulation mode) for the current process. Only
  929  * accept the Linux personality here (0). This call is needed because
  930  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
  931  * ELF binaries run in Linux mode, not SVR4 mode.
  932  */
  933 int
  934 linux_sys_personality(struct lwp *l, const struct linux_sys_personality_args *uap, register_t *retval)
  935 {
  936         /* {
  937                 syscallarg(int) per;
  938         } */
  939 
  940         if (SCARG(uap, per) != 0)
  941                 return EINVAL;
  942         retval[0] = 0;
  943         return 0;
  944 }
  945 
  946 /*
  947  * We have nonexistent fsuid equal to uid.
  948  * If modification is requested, refuse.
  949  */
  950 int
  951 linux_sys_setfsuid(struct lwp *l, const struct linux_sys_setfsuid_args *uap, register_t *retval)
  952 {
  953          /* {
  954                  syscallarg(uid_t) uid;
  955          } */
  956          uid_t uid;
  957 
  958          uid = SCARG(uap, uid);
  959          if (kauth_cred_getuid(l->l_cred) != uid)
  960                  return sys_nosys(l, uap, retval);
  961 
  962          *retval = uid;
  963          return 0;
  964 }
  965 
  966 int
  967 linux_sys_setfsgid(struct lwp *l, const struct linux_sys_setfsgid_args *uap, register_t *retval)
  968 {
  969         /* {
  970                 syscallarg(gid_t) gid;
  971         } */
  972         gid_t gid;
  973 
  974         gid = SCARG(uap, gid);
  975         if (kauth_cred_getgid(l->l_cred) != gid)
  976                 return sys_nosys(l, uap, retval);
  977 
  978         *retval = gid;
  979         return 0;
  980 }
  981 
  982 int
  983 linux_sys_setresuid(struct lwp *l, const struct linux_sys_setresuid_args *uap, register_t *retval)
  984 {
  985         /* {
  986                 syscallarg(uid_t) ruid;
  987                 syscallarg(uid_t) euid;
  988                 syscallarg(uid_t) suid;
  989         } */
  990 
  991         /*
  992          * Note: These checks are a little different than the NetBSD
  993          * setreuid(2) call performs.  This precisely follows the
  994          * behavior of the Linux kernel.
  995          */
  996 
  997         return do_setresuid(l, SCARG(uap, ruid), SCARG(uap, euid),
  998                             SCARG(uap, suid),
  999                             ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
 1000                             ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
 1001                             ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
 1002 }
 1003 
 1004 int
 1005 linux_sys_getresuid(struct lwp *l, const struct linux_sys_getresuid_args *uap, register_t *retval)
 1006 {
 1007         /* {
 1008                 syscallarg(uid_t *) ruid;
 1009                 syscallarg(uid_t *) euid;
 1010                 syscallarg(uid_t *) suid;
 1011         } */
 1012         kauth_cred_t pc = l->l_cred;
 1013         int error;
 1014         uid_t uid;
 1015 
 1016         /*
 1017          * Linux copies these values out to userspace like so:
 1018          *
 1019          *      1. Copy out ruid.
 1020          *      2. If that succeeds, copy out euid.
 1021          *      3. If both of those succeed, copy out suid.
 1022          */
 1023         uid = kauth_cred_getuid(pc);
 1024         if ((error = copyout(&uid, SCARG(uap, ruid), sizeof(uid_t))) != 0)
 1025                 return (error);
 1026 
 1027         uid = kauth_cred_geteuid(pc);
 1028         if ((error = copyout(&uid, SCARG(uap, euid), sizeof(uid_t))) != 0)
 1029                 return (error);
 1030 
 1031         uid = kauth_cred_getsvuid(pc);
 1032 
 1033         return (copyout(&uid, SCARG(uap, suid), sizeof(uid_t)));
 1034 }
 1035 
 1036 int
 1037 linux_sys_ptrace(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
 1038 {
 1039         /* {
 1040                 i386, m68k, powerpc: T=int
 1041                 alpha, amd64: T=long
 1042                 syscallarg(T) request;
 1043                 syscallarg(T) pid;
 1044                 syscallarg(T) addr;
 1045                 syscallarg(T) data;
 1046         } */
 1047 #if defined(PTRACE) || defined(_LKM)
 1048         const int *ptr;
 1049         int request;
 1050         int error;
 1051 #ifdef _LKM
 1052 #define sys_ptrace (*sysent[SYS_ptrace].sy_call)
 1053 #endif
 1054 
 1055         ptr = linux_ptrace_request_map;
 1056         request = SCARG(uap, request);
 1057         while (*ptr != -1)
 1058                 if (*ptr++ == request) {
 1059                         struct sys_ptrace_args pta;
 1060 
 1061                         SCARG(&pta, req) = *ptr;
 1062                         SCARG(&pta, pid) = SCARG(uap, pid);
 1063                         SCARG(&pta, addr) = (void *)SCARG(uap, addr);
 1064                         SCARG(&pta, data) = SCARG(uap, data);
 1065 
 1066                         /*
 1067                          * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
 1068                          * to continue where the process left off previously.
 1069                          * The same thing is achieved by addr == (void *) 1
 1070                          * on NetBSD, so rewrite 'addr' appropriately.
 1071                          */
 1072                         if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
 1073                                 SCARG(&pta, addr) = (void *) 1;
 1074 
 1075                         error = sys_ptrace(l, &pta, retval);
 1076                         if (error)
 1077                                 return error;
 1078                         switch (request) {
 1079                         case LINUX_PTRACE_PEEKTEXT:
 1080                         case LINUX_PTRACE_PEEKDATA:
 1081                                 error = copyout (retval,
 1082                                     (void *)SCARG(uap, data), 
 1083                                     sizeof *retval);
 1084                                 *retval = SCARG(uap, data);
 1085                                 break;
 1086                         default:
 1087                                 break;
 1088                         }
 1089                         return error;
 1090                 }
 1091                 else
 1092                         ptr++;
 1093 
 1094         return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
 1095 #else
 1096         return ENOSYS;
 1097 #endif /* PTRACE || _LKM */
 1098 }
 1099 
 1100 int
 1101 linux_sys_reboot(struct lwp *l, const struct linux_sys_reboot_args *uap, register_t *retval)
 1102 {
 1103         /* {
 1104                 syscallarg(int) magic1;
 1105                 syscallarg(int) magic2;
 1106                 syscallarg(int) cmd;
 1107                 syscallarg(void *) arg;
 1108         } */
 1109         struct sys_reboot_args /* {
 1110                 syscallarg(int) opt;
 1111                 syscallarg(char *) bootstr;
 1112         } */ sra;
 1113         int error;
 1114 
 1115         if ((error = kauth_authorize_system(l->l_cred,
 1116             KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
 1117                 return(error);
 1118 
 1119         if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
 1120                 return(EINVAL);
 1121         if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
 1122             SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
 1123             SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
 1124                 return(EINVAL);
 1125 
 1126         switch ((unsigned long)SCARG(uap, cmd)) {
 1127         case LINUX_REBOOT_CMD_RESTART:
 1128                 SCARG(&sra, opt) = RB_AUTOBOOT;
 1129                 break;
 1130         case LINUX_REBOOT_CMD_HALT:
 1131                 SCARG(&sra, opt) = RB_HALT;
 1132                 break;
 1133         case LINUX_REBOOT_CMD_POWER_OFF:
 1134                 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
 1135                 break;
 1136         case LINUX_REBOOT_CMD_RESTART2:
 1137                 /* Reboot with an argument. */
 1138                 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
 1139                 SCARG(&sra, bootstr) = SCARG(uap, arg);
 1140                 break;
 1141         case LINUX_REBOOT_CMD_CAD_ON:
 1142                 return(EINVAL); /* We don't implement ctrl-alt-delete */
 1143         case LINUX_REBOOT_CMD_CAD_OFF:
 1144                 return(0);
 1145         default:
 1146                 return(EINVAL);
 1147         }
 1148 
 1149         return(sys_reboot(l, &sra, retval));
 1150 }
 1151 
 1152 /*
 1153  * Copy of compat_12_sys_swapon().
 1154  */
 1155 int
 1156 linux_sys_swapon(struct lwp *l, const struct linux_sys_swapon_args *uap, register_t *retval)
 1157 {
 1158         /* {
 1159                 syscallarg(const char *) name;
 1160         } */
 1161         struct sys_swapctl_args ua;
 1162 
 1163         SCARG(&ua, cmd) = SWAP_ON;
 1164         SCARG(&ua, arg) = (void *)__UNCONST(SCARG(uap, name));
 1165         SCARG(&ua, misc) = 0;   /* priority */
 1166         return (sys_swapctl(l, &ua, retval));
 1167 }
 1168 
 1169 /*
 1170  * Stop swapping to the file or block device specified by path.
 1171  */
 1172 int
 1173 linux_sys_swapoff(struct lwp *l, const struct linux_sys_swapoff_args *uap, register_t *retval)
 1174 {
 1175         /* {
 1176                 syscallarg(const char *) path;
 1177         } */
 1178         struct sys_swapctl_args ua;
 1179 
 1180         SCARG(&ua, cmd) = SWAP_OFF;
 1181         SCARG(&ua, arg) = __UNCONST(SCARG(uap, path)); /*XXXUNCONST*/
 1182         return (sys_swapctl(l, &ua, retval));
 1183 }
 1184 
 1185 /*
 1186  * Copy of compat_09_sys_setdomainname()
 1187  */
 1188 /* ARGSUSED */
 1189 int
 1190 linux_sys_setdomainname(struct lwp *l, const struct linux_sys_setdomainname_args *uap, register_t *retval)
 1191 {
 1192         /* {
 1193                 syscallarg(char *) domainname;
 1194                 syscallarg(int) len;
 1195         } */
 1196         int name[2];
 1197 
 1198         name[0] = CTL_KERN;
 1199         name[1] = KERN_DOMAINNAME;
 1200         return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, domainname),
 1201                             SCARG(uap, len), l));
 1202 }
 1203 
 1204 /*
 1205  * sysinfo()
 1206  */
 1207 /* ARGSUSED */
 1208 int
 1209 linux_sys_sysinfo(struct lwp *l, const struct linux_sys_sysinfo_args *uap, register_t *retval)
 1210 {
 1211         /* {
 1212                 syscallarg(struct linux_sysinfo *) arg;
 1213         } */
 1214         struct linux_sysinfo si;
 1215         struct loadavg *la;
 1216 
 1217         si.uptime = time_uptime;
 1218         la = &averunnable;
 1219         si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
 1220         si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
 1221         si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
 1222         si.totalram = ctob((u_long)physmem);
 1223         si.freeram = (u_long)uvmexp.free * uvmexp.pagesize;
 1224         si.sharedram = 0;       /* XXX */
 1225         si.bufferram = (u_long)uvmexp.filepages * uvmexp.pagesize;
 1226         si.totalswap = (u_long)uvmexp.swpages * uvmexp.pagesize;
 1227         si.freeswap = 
 1228             (u_long)(uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
 1229         si.procs = nprocs;
 1230 
 1231         /* The following are only present in newer Linux kernels. */
 1232         si.totalbig = 0;
 1233         si.freebig = 0;
 1234         si.mem_unit = 1;
 1235 
 1236         return (copyout(&si, SCARG(uap, arg), sizeof si));
 1237 }
 1238 
 1239 int
 1240 linux_sys_getrlimit(struct lwp *l, const struct linux_sys_getrlimit_args *uap, register_t *retval)
 1241 {
 1242         /* {
 1243                 syscallarg(int) which;
 1244 # ifdef LINUX_LARGEFILE64
 1245                 syscallarg(struct rlimit *) rlp;
 1246 # else
 1247                 syscallarg(struct orlimit *) rlp;
 1248 # endif
 1249         } */
 1250 # ifdef LINUX_LARGEFILE64
 1251         struct rlimit orl;
 1252 # else
 1253         struct orlimit orl;
 1254 # endif
 1255         int which;
 1256 
 1257         which = linux_to_bsd_limit(SCARG(uap, which));
 1258         if (which < 0)
 1259                 return -which;
 1260 
 1261         bsd_to_linux_rlimit(&orl, &l->l_proc->p_rlimit[which]);
 1262 
 1263         return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
 1264 }
 1265 
 1266 int
 1267 linux_sys_setrlimit(struct lwp *l, const struct linux_sys_setrlimit_args *uap, register_t *retval)
 1268 {
 1269         /* {
 1270                 syscallarg(int) which;
 1271 # ifdef LINUX_LARGEFILE64
 1272                 syscallarg(struct rlimit *) rlp;
 1273 # else
 1274                 syscallarg(struct orlimit *) rlp;
 1275 # endif
 1276         } */
 1277         struct rlimit rl;
 1278 # ifdef LINUX_LARGEFILE64
 1279         struct rlimit orl;
 1280 # else
 1281         struct orlimit orl;
 1282 # endif
 1283         int error;
 1284         int which;
 1285 
 1286         if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
 1287                 return error;
 1288 
 1289         which = linux_to_bsd_limit(SCARG(uap, which));
 1290         if (which < 0)
 1291                 return -which;
 1292 
 1293         linux_to_bsd_rlimit(&rl, &orl);
 1294         return dosetrlimit(l, l->l_proc, which, &rl);
 1295 }
 1296 
 1297 # if !defined(__mips__) && !defined(__amd64__)
 1298 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
 1299 int
 1300 linux_sys_ugetrlimit(struct lwp *l, const struct linux_sys_ugetrlimit_args *uap, register_t *retval)
 1301 {
 1302         return linux_sys_getrlimit(l, (const void *)uap, retval);
 1303 }
 1304 # endif
 1305 
 1306 /*
 1307  * This gets called for unsupported syscalls. The difference to sys_nosys()
 1308  * is that process does not get SIGSYS, the call just returns with ENOSYS.
 1309  * This is the way Linux does it and glibc depends on this behaviour.
 1310  */
 1311 int
 1312 linux_sys_nosys(struct lwp *l, const void *v, register_t *retval)
 1313 {
 1314         return (ENOSYS);
 1315 }
 1316 
 1317 int
 1318 linux_sys_getpriority(struct lwp *l, const struct linux_sys_getpriority_args *uap, register_t *retval)
 1319 {
 1320         /* {
 1321                 syscallarg(int) which;
 1322                 syscallarg(int) who;
 1323         } */
 1324         struct sys_getpriority_args bsa;
 1325         int error;
 1326 
 1327         SCARG(&bsa, which) = SCARG(uap, which);
 1328         SCARG(&bsa, who) = SCARG(uap, who);
 1329 
 1330         if ((error = sys_getpriority(l, &bsa, retval)))
 1331                 return error;
 1332 
 1333         *retval = NZERO - *retval;
 1334 
 1335         return 0;
 1336 }
 1337 
 1338 #endif /* !COMPAT_LINUX32 */

Cache object: fe328cc35c3d8cc7cc2727b7178ba6ff


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