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/lock.h>
   42 #include <sys/mman.h>
   43 #include <sys/proc.h>
   44 #include <sys/resourcevar.h>
   45 #include <sys/rwlock.h>
   46 #include <sys/syscallsubr.h>
   47 #include <sys/sysent.h>
   48 #include <sys/sysproto.h>
   49 
   50 #include <vm/pmap.h>
   51 #include <vm/vm_extern.h>
   52 #include <vm/vm_map.h>
   53 #include <vm/vm_object.h>
   54 
   55 #include <compat/linux/linux_emul.h>
   56 #include <compat/linux/linux_mmap.h>
   57 #include <compat/linux/linux_persona.h>
   58 #include <compat/linux/linux_util.h>
   59 
   60 #define STACK_SIZE  (2 * 1024 * 1024)
   61 #define GUARD_SIZE  (4 * PAGE_SIZE)
   62 
   63 #if defined(__amd64__)
   64 static void linux_fixup_prot(struct thread *td, int *prot);
   65 #endif
   66 
   67 static int
   68 linux_mmap_check_fp(struct file *fp, int flags, int prot, int maxprot)
   69 {
   70 
   71         /* Linux mmap() just fails for O_WRONLY files */
   72         if ((fp->f_flag & FREAD) == 0)
   73                 return (EACCES);
   74 
   75         return (0);
   76 }
   77 
   78 int
   79 linux_mmap_common(struct thread *td, uintptr_t addr, size_t len, int prot,
   80     int flags, int fd, off_t pos)
   81 {
   82         struct mmap_req mr, mr_fixed;
   83         struct proc *p = td->td_proc;
   84         struct vmspace *vms = td->td_proc->p_vmspace;
   85         int bsd_flags, error;
   86         struct file *fp;
   87 
   88         LINUX_CTR6(mmap2, "0x%lx, %ld, %ld, 0x%08lx, %ld, 0x%lx",
   89             addr, len, prot, flags, fd, pos);
   90 
   91         error = 0;
   92         bsd_flags = 0;
   93         fp = NULL;
   94 
   95         /*
   96          * Linux mmap(2):
   97          * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
   98          */
   99         if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
  100                 return (EINVAL);
  101 
  102         if (flags & LINUX_MAP_SHARED)
  103                 bsd_flags |= MAP_SHARED;
  104         if (flags & LINUX_MAP_PRIVATE)
  105                 bsd_flags |= MAP_PRIVATE;
  106         if (flags & LINUX_MAP_FIXED)
  107                 bsd_flags |= MAP_FIXED;
  108         if (flags & LINUX_MAP_ANON) {
  109                 /* Enforce pos to be on page boundary, then ignore. */
  110                 if ((pos & PAGE_MASK) != 0)
  111                         return (EINVAL);
  112                 pos = 0;
  113                 bsd_flags |= MAP_ANON;
  114         } else
  115                 bsd_flags |= MAP_NOSYNC;
  116         if (flags & LINUX_MAP_GROWSDOWN)
  117                 bsd_flags |= MAP_STACK;
  118 
  119 #if defined(__amd64__)
  120         /*
  121          * According to the Linux mmap(2) man page, "MAP_32BIT flag
  122          * is ignored when MAP_FIXED is set."
  123          */
  124         if ((flags & LINUX_MAP_32BIT) && (flags & LINUX_MAP_FIXED) == 0)
  125                 bsd_flags |= MAP_32BIT;
  126 
  127         /*
  128          * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
  129          * on Linux/i386 if the binary requires executable stack.
  130          * We do this only for IA32 emulation as on native i386 this is does not
  131          * make sense without PAE.
  132          *
  133          * XXX. Linux checks that the file system is not mounted with noexec.
  134          */
  135         linux_fixup_prot(td, &prot);
  136 #endif
  137 
  138         /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
  139         fd = (bsd_flags & MAP_ANON) ? -1 : fd;
  140         if (flags & LINUX_MAP_GROWSDOWN) {
  141                 /*
  142                  * The Linux MAP_GROWSDOWN option does not limit auto
  143                  * growth of the region.  Linux mmap with this option
  144                  * takes as addr the initial BOS, and as len, the initial
  145                  * region size.  It can then grow down from addr without
  146                  * limit.  However, Linux threads has an implicit internal
  147                  * limit to stack size of STACK_SIZE.  Its just not
  148                  * enforced explicitly in Linux.  But, here we impose
  149                  * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
  150                  * region, since we can do this with our mmap.
  151                  *
  152                  * Our mmap with MAP_STACK takes addr as the maximum
  153                  * downsize limit on BOS, and as len the max size of
  154                  * the region.  It then maps the top SGROWSIZ bytes,
  155                  * and auto grows the region down, up to the limit
  156                  * in addr.
  157                  *
  158                  * If we don't use the MAP_STACK option, the effect
  159                  * of this code is to allocate a stack region of a
  160                  * fixed size of (STACK_SIZE - GUARD_SIZE).
  161                  */
  162 
  163                 if ((caddr_t)addr + len > vms->vm_maxsaddr) {
  164                         /*
  165                          * Some Linux apps will attempt to mmap
  166                          * thread stacks near the top of their
  167                          * address space.  If their TOS is greater
  168                          * than vm_maxsaddr, vm_map_growstack()
  169                          * will confuse the thread stack with the
  170                          * process stack and deliver a SEGV if they
  171                          * attempt to grow the thread stack past their
  172                          * current stacksize rlimit.  To avoid this,
  173                          * adjust vm_maxsaddr upwards to reflect
  174                          * the current stacksize rlimit rather
  175                          * than the maximum possible stacksize.
  176                          * It would be better to adjust the
  177                          * mmap'ed region, but some apps do not check
  178                          * mmap's return value.
  179                          */
  180                         PROC_LOCK(p);
  181                         vms->vm_maxsaddr = (char *)p->p_sysent->sv_usrstack -
  182                             lim_cur_proc(p, RLIMIT_STACK);
  183                         PROC_UNLOCK(p);
  184                 }
  185 
  186                 /*
  187                  * This gives us our maximum stack size and a new BOS.
  188                  * If we're using VM_STACK, then mmap will just map
  189                  * the top SGROWSIZ bytes, and let the stack grow down
  190                  * to the limit at BOS.  If we're not using VM_STACK
  191                  * we map the full stack, since we don't have a way
  192                  * to autogrow it.
  193                  */
  194                 if (len <= STACK_SIZE - GUARD_SIZE) {
  195                         addr = addr - (STACK_SIZE - GUARD_SIZE - len);
  196                         len = STACK_SIZE - GUARD_SIZE;
  197                 }
  198         }
  199 
  200         /*
  201          * FreeBSD is free to ignore the address hint if MAP_FIXED wasn't
  202          * passed.  However, some Linux applications, like the ART runtime,
  203          * depend on the hint.  If the MAP_FIXED wasn't passed, but the
  204          * address is not zero, try with MAP_FIXED and MAP_EXCL first,
  205          * and fall back to the normal behaviour if that fails.
  206          */
  207         mr = (struct mmap_req) {
  208                 .mr_hint = addr,
  209                 .mr_len = len,
  210                 .mr_prot = prot,
  211                 .mr_flags = bsd_flags,
  212                 .mr_fd = fd,
  213                 .mr_pos = pos,
  214                 .mr_check_fp_fn = linux_mmap_check_fp,
  215         };
  216         if (addr != 0 && (bsd_flags & MAP_FIXED) == 0 &&
  217             (bsd_flags & MAP_EXCL) == 0) {
  218                 mr_fixed = mr;
  219                 mr_fixed.mr_flags |= MAP_FIXED | MAP_EXCL;
  220                 error = kern_mmap_req(td, &mr_fixed);
  221                 if (error == 0)
  222                         goto out;
  223         }
  224 
  225         error = kern_mmap_req(td, &mr);
  226 out:
  227         LINUX_CTR2(mmap2, "return: %d (%p)", error, td->td_retval[0]);
  228 
  229         return (error);
  230 }
  231 
  232 int
  233 linux_mprotect_common(struct thread *td, uintptr_t addr, size_t len, int prot)
  234 {
  235 
  236         /* XXX Ignore PROT_GROWSDOWN and PROT_GROWSUP for now. */
  237         prot &= ~(LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
  238         if ((prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
  239                 return (EINVAL);
  240 
  241 #if defined(__amd64__)
  242         linux_fixup_prot(td, &prot);
  243 #endif
  244         return (kern_mprotect(td, addr, len, prot));
  245 }
  246 
  247 /*
  248  * Implement Linux madvise(MADV_DONTNEED), which has unusual semantics: for
  249  * anonymous memory, pages in the range are immediately discarded.
  250  */
  251 static int
  252 linux_madvise_dontneed(struct thread *td, vm_offset_t start, vm_offset_t end)
  253 {
  254         vm_map_t map;
  255         vm_map_entry_t entry;
  256         vm_object_t backing_object, object;
  257         vm_offset_t estart, eend;
  258         vm_pindex_t pstart, pend;
  259         int error;
  260 
  261         map = &td->td_proc->p_vmspace->vm_map;
  262 
  263         if (!vm_map_range_valid(map, start, end))
  264                 return (EINVAL);
  265         start = trunc_page(start);
  266         end = round_page(end);
  267 
  268         error = 0;
  269         vm_map_lock_read(map);
  270         if (!vm_map_lookup_entry(map, start, &entry))
  271                 entry = vm_map_entry_succ(entry);
  272         for (; entry->start < end; entry = vm_map_entry_succ(entry)) {
  273                 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
  274                         continue;
  275 
  276                 if (entry->wired_count != 0) {
  277                         error = EINVAL;
  278                         break;
  279                 }
  280 
  281                 object = entry->object.vm_object;
  282                 if (object == NULL)
  283                         continue;
  284                 if ((object->flags & (OBJ_UNMANAGED | OBJ_FICTITIOUS)) != 0)
  285                         continue;
  286 
  287                 pstart = OFF_TO_IDX(entry->offset);
  288                 if (start > entry->start) {
  289                         pstart += atop(start - entry->start);
  290                         estart = start;
  291                 } else {
  292                         estart = entry->start;
  293                 }
  294                 pend = OFF_TO_IDX(entry->offset) +
  295                     atop(entry->end - entry->start);
  296                 if (entry->end > end) {
  297                         pend -= atop(entry->end - end);
  298                         eend = end;
  299                 } else {
  300                         eend = entry->end;
  301                 }
  302 
  303                 if ((object->flags & (OBJ_ANON | OBJ_ONEMAPPING)) ==
  304                     (OBJ_ANON | OBJ_ONEMAPPING)) {
  305                         /*
  306                          * Singly-mapped anonymous memory is discarded.  This
  307                          * does not match Linux's semantics when the object
  308                          * belongs to a shadow chain of length > 1, since
  309                          * subsequent faults may retrieve pages from an
  310                          * intermediate anonymous object.  However, handling
  311                          * this case correctly introduces a fair bit of
  312                          * complexity.
  313                          */
  314                         VM_OBJECT_WLOCK(object);
  315                         if ((object->flags & OBJ_ONEMAPPING) != 0) {
  316                                 vm_object_collapse(object);
  317                                 vm_object_page_remove(object, pstart, pend, 0);
  318                                 backing_object = object->backing_object;
  319                                 if (backing_object != NULL &&
  320                                     (backing_object->flags & OBJ_ANON) != 0)
  321                                         linux_msg(td,
  322                                             "possibly incorrect MADV_DONTNEED");
  323                                 VM_OBJECT_WUNLOCK(object);
  324                                 continue;
  325                         }
  326                         VM_OBJECT_WUNLOCK(object);
  327                 }
  328 
  329                 /*
  330                  * Handle shared mappings.  Remove them outright instead of
  331                  * calling pmap_advise(), for consistency with Linux.
  332                  */
  333                 pmap_remove(map->pmap, estart, eend);
  334                 vm_object_madvise(object, pstart, pend, MADV_DONTNEED);
  335         }
  336         vm_map_unlock_read(map);
  337 
  338         return (error);
  339 }
  340 
  341 int
  342 linux_madvise_common(struct thread *td, uintptr_t addr, size_t len, int behav)
  343 {
  344 
  345         switch (behav) {
  346         case LINUX_MADV_NORMAL:
  347                 return (kern_madvise(td, addr, len, MADV_NORMAL));
  348         case LINUX_MADV_RANDOM:
  349                 return (kern_madvise(td, addr, len, MADV_RANDOM));
  350         case LINUX_MADV_SEQUENTIAL:
  351                 return (kern_madvise(td, addr, len, MADV_SEQUENTIAL));
  352         case LINUX_MADV_WILLNEED:
  353                 return (kern_madvise(td, addr, len, MADV_WILLNEED));
  354         case LINUX_MADV_DONTNEED:
  355                 return (linux_madvise_dontneed(td, addr, addr + len));
  356         case LINUX_MADV_FREE:
  357                 return (kern_madvise(td, addr, len, MADV_FREE));
  358         case LINUX_MADV_REMOVE:
  359                 linux_msg(curthread, "unsupported madvise MADV_REMOVE");
  360                 return (EINVAL);
  361         case LINUX_MADV_DONTFORK:
  362                 return (kern_minherit(td, addr, len, INHERIT_NONE));
  363         case LINUX_MADV_DOFORK:
  364                 return (kern_minherit(td, addr, len, INHERIT_COPY));
  365         case LINUX_MADV_MERGEABLE:
  366                 linux_msg(curthread, "unsupported madvise MADV_MERGEABLE");
  367                 return (EINVAL);
  368         case LINUX_MADV_UNMERGEABLE:
  369                 /* We don't merge anyway. */
  370                 return (0);
  371         case LINUX_MADV_HUGEPAGE:
  372                 /* Ignored; on FreeBSD huge pages are always on. */
  373                 return (0);
  374         case LINUX_MADV_NOHUGEPAGE:
  375 #if 0
  376                 /*
  377                  * Don't warn - Firefox uses it a lot, and in real Linux it's
  378                  * an optional feature.
  379                  */
  380                 linux_msg(curthread, "unsupported madvise MADV_NOHUGEPAGE");
  381 #endif
  382                 return (EINVAL);
  383         case LINUX_MADV_DONTDUMP:
  384                 return (kern_madvise(td, addr, len, MADV_NOCORE));
  385         case LINUX_MADV_DODUMP:
  386                 return (kern_madvise(td, addr, len, MADV_CORE));
  387         case LINUX_MADV_WIPEONFORK:
  388                 return (kern_minherit(td, addr, len, INHERIT_ZERO));
  389         case LINUX_MADV_KEEPONFORK:
  390                 return (kern_minherit(td, addr, len, INHERIT_COPY));
  391         case LINUX_MADV_HWPOISON:
  392                 linux_msg(curthread, "unsupported madvise MADV_HWPOISON");
  393                 return (EINVAL);
  394         case LINUX_MADV_SOFT_OFFLINE:
  395                 linux_msg(curthread, "unsupported madvise MADV_SOFT_OFFLINE");
  396                 return (EINVAL);
  397         case -1:
  398                 /*
  399                  * -1 is sometimes used as a dummy value to detect simplistic
  400                  * madvise(2) stub implementations.  This safeguard is used by
  401                  * BoringSSL, for example, before assuming MADV_WIPEONFORK is
  402                  * safe to use.  Don't produce an "unsupported" error message
  403                  * for this special dummy value, which is unlikely to be used
  404                  * by any new advisory behavior feature.
  405                  */
  406                 return (EINVAL);
  407         default:
  408                 linux_msg(curthread, "unsupported madvise behav %d", behav);
  409                 return (EINVAL);
  410         }
  411 }
  412 
  413 #if defined(__amd64__)
  414 static void
  415 linux_fixup_prot(struct thread *td, int *prot)
  416 {
  417         struct linux_pemuldata *pem;
  418 
  419         if (SV_PROC_FLAG(td->td_proc, SV_ILP32) && *prot & PROT_READ) {
  420                 pem = pem_find(td->td_proc);
  421                 if (pem->persona & LINUX_READ_IMPLIES_EXEC)
  422                         *prot |= PROT_EXEC;
  423         }
  424 
  425 }
  426 #endif

Cache object: 5facf048907b3aa2d8a5956db55c2039


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