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/linux_mmap.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) 2004 Tim J. Robbins
    3  * Copyright (c) 2002 Doug Rabson
    4  * Copyright (c) 2000 Marcel Moolenaar
    5  * Copyright (c) 1994-1995 Søren Schmidt
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer
   13  *    in this position and unchanged.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 3. The name of the author may not be used to endorse or promote products
   18  *    derived from this software without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   30  *
   31  * $FreeBSD$
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD$");
   36 
   37 #include <sys/capsicum.h>
   38 #include <sys/file.h>
   39 #include <sys/imgact.h>
   40 #include <sys/ktr.h>
   41 #include <sys/mman.h>
   42 #include <sys/proc.h>
   43 #include <sys/resourcevar.h>
   44 #include <sys/syscallsubr.h>
   45 #include <sys/sysent.h>
   46 #include <sys/sysproto.h>
   47 
   48 #include <vm/pmap.h>
   49 #include <vm/vm_extern.h>
   50 #include <vm/vm_map.h>
   51 
   52 #include <compat/linux/linux_emul.h>
   53 #include <compat/linux/linux_mmap.h>
   54 #include <compat/linux/linux_persona.h>
   55 #include <compat/linux/linux_util.h>
   56 
   57 
   58 #define STACK_SIZE  (2 * 1024 * 1024)
   59 #define GUARD_SIZE  (4 * PAGE_SIZE)
   60 
   61 #if defined(__amd64__)
   62 static void linux_fixup_prot(struct thread *td, int *prot);
   63 #endif
   64 
   65 static int
   66 linux_mmap_check_fp(struct file *fp, int flags, int prot, int maxprot)
   67 {
   68 
   69         /* Linux mmap() just fails for O_WRONLY files */
   70         if ((fp->f_flag & FREAD) == 0)
   71                 return (EACCES);
   72 
   73         return (0);
   74 }
   75 
   76 int
   77 linux_mmap_common(struct thread *td, uintptr_t addr, size_t len, int prot,
   78     int flags, int fd, off_t pos)
   79 {
   80         struct proc *p = td->td_proc;
   81         struct vmspace *vms = td->td_proc->p_vmspace;
   82         int bsd_flags, error;
   83         struct file *fp;
   84 
   85         LINUX_CTR6(mmap2, "0x%lx, %ld, %ld, 0x%08lx, %ld, 0x%lx",
   86             addr, len, prot, flags, fd, pos);
   87 
   88         error = 0;
   89         bsd_flags = 0;
   90         fp = NULL;
   91 
   92         /*
   93          * Linux mmap(2):
   94          * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
   95          */
   96         if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
   97                 return (EINVAL);
   98 
   99         if (flags & LINUX_MAP_SHARED)
  100                 bsd_flags |= MAP_SHARED;
  101         if (flags & LINUX_MAP_PRIVATE)
  102                 bsd_flags |= MAP_PRIVATE;
  103         if (flags & LINUX_MAP_FIXED)
  104                 bsd_flags |= MAP_FIXED;
  105         if (flags & LINUX_MAP_ANON) {
  106                 /* Enforce pos to be on page boundary, then ignore. */
  107                 if ((pos & PAGE_MASK) != 0)
  108                         return (EINVAL);
  109                 pos = 0;
  110                 bsd_flags |= MAP_ANON;
  111         } else
  112                 bsd_flags |= MAP_NOSYNC;
  113         if (flags & LINUX_MAP_GROWSDOWN)
  114                 bsd_flags |= MAP_STACK;
  115 
  116         /*
  117          * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
  118          * on Linux/i386 if the binary requires executable stack.
  119          * We do this only for IA32 emulation as on native i386 this is does not
  120          * make sense without PAE.
  121          *
  122          * XXX. Linux checks that the file system is not mounted with noexec.
  123          */
  124 #if defined(__amd64__)
  125         linux_fixup_prot(td, &prot);
  126 #endif
  127 
  128         /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
  129         fd = (bsd_flags & MAP_ANON) ? -1 : fd;
  130         if (flags & LINUX_MAP_GROWSDOWN) {
  131                 /*
  132                  * The Linux MAP_GROWSDOWN option does not limit auto
  133                  * growth of the region.  Linux mmap with this option
  134                  * takes as addr the initial BOS, and as len, the initial
  135                  * region size.  It can then grow down from addr without
  136                  * limit.  However, Linux threads has an implicit internal
  137                  * limit to stack size of STACK_SIZE.  Its just not
  138                  * enforced explicitly in Linux.  But, here we impose
  139                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
  140                  * region, since we can do this with our mmap.
  141                  *
  142                  * Our mmap with MAP_STACK takes addr as the maximum
  143                  * downsize limit on BOS, and as len the max size of
  144                  * the region.  It then maps the top SGROWSIZ bytes,
  145                  * and auto grows the region down, up to the limit
  146                  * in addr.
  147                  *
  148                  * If we don't use the MAP_STACK option, the effect
  149                  * of this code is to allocate a stack region of a
  150                  * fixed size of (STACK_SIZE - GUARD_SIZE).
  151                  */
  152 
  153                 if ((caddr_t)addr + len > vms->vm_maxsaddr) {
  154                         /*
  155                          * Some Linux apps will attempt to mmap
  156                          * thread stacks near the top of their
  157                          * address space.  If their TOS is greater
  158                          * than vm_maxsaddr, vm_map_growstack()
  159                          * will confuse the thread stack with the
  160                          * process stack and deliver a SEGV if they
  161                          * attempt to grow the thread stack past their
  162                          * current stacksize rlimit.  To avoid this,
  163                          * adjust vm_maxsaddr upwards to reflect
  164                          * the current stacksize rlimit rather
  165                          * than the maximum possible stacksize.
  166                          * It would be better to adjust the
  167                          * mmap'ed region, but some apps do not check
  168                          * mmap's return value.
  169                          */
  170                         PROC_LOCK(p);
  171                         vms->vm_maxsaddr = (char *)p->p_sysent->sv_usrstack -
  172                             lim_cur_proc(p, RLIMIT_STACK);
  173                         PROC_UNLOCK(p);
  174                 }
  175 
  176                 /*
  177                  * This gives us our maximum stack size and a new BOS.
  178                  * If we're using VM_STACK, then mmap will just map
  179                  * the top SGROWSIZ bytes, and let the stack grow down
  180                  * to the limit at BOS.  If we're not using VM_STACK
  181                  * we map the full stack, since we don't have a way
  182                  * to autogrow it.
  183                  */
  184                 if (len <= STACK_SIZE - GUARD_SIZE) {
  185                         addr = addr - (STACK_SIZE - GUARD_SIZE - len);
  186                         len = STACK_SIZE - GUARD_SIZE;
  187                 }
  188         }
  189 
  190         /*
  191          * FreeBSD is free to ignore the address hint if MAP_FIXED wasn't
  192          * passed.  However, some Linux applications, like the ART runtime,
  193          * depend on the hint.  If the MAP_FIXED wasn't passed, but the
  194          * address is not zero, try with MAP_FIXED and MAP_EXCL first,
  195          * and fall back to the normal behaviour if that fails.
  196          */
  197         if (addr != 0 && (bsd_flags & MAP_FIXED) == 0 &&
  198             (bsd_flags & MAP_EXCL) == 0) {
  199                 error = kern_mmap_fpcheck(td, addr, len, prot,
  200                     bsd_flags | MAP_FIXED | MAP_EXCL, fd, pos,
  201                     linux_mmap_check_fp);
  202                 if (error == 0)
  203                         goto out;
  204         }
  205 
  206         error = kern_mmap_fpcheck(td, addr, len, prot, bsd_flags, fd, pos,
  207             linux_mmap_check_fp);
  208 out:
  209         LINUX_CTR2(mmap2, "return: %d (%p)", error, td->td_retval[0]);
  210 
  211         return (error);
  212 }
  213 
  214 int
  215 linux_mprotect_common(struct thread *td, uintptr_t addr, size_t len, int prot)
  216 {
  217 
  218         /* XXX Ignore PROT_GROWSDOWN and PROT_GROWSUP for now. */
  219         prot &= ~(LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
  220         if ((prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
  221                 return (EINVAL);
  222 
  223 #if defined(__amd64__)
  224         linux_fixup_prot(td, &prot);
  225 #endif
  226         return (kern_mprotect(td, addr, len, prot));
  227 }
  228 
  229 #if defined(__amd64__)
  230 static void
  231 linux_fixup_prot(struct thread *td, int *prot)
  232 {
  233         struct linux_pemuldata *pem;
  234 
  235         if (SV_PROC_FLAG(td->td_proc, SV_ILP32) && *prot & PROT_READ) {
  236                 pem = pem_find(td->td_proc);
  237                 if (pem->persona & LINUX_READ_IMPLIES_EXEC)
  238                         *prot |= PROT_EXEC;
  239         }
  240 
  241 }
  242 #endif

Cache object: 086ceeac3d687d7aed8c5a1314db8571


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