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/vm/vm_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) 1988 University of Utah.
    3  * Copyright (c) 1991, 1993
    4  *      The Regents of the University of California.  All rights reserved.
    5  *
    6  * This code is derived from software contributed to Berkeley by
    7  * the Systems Programming Group of the University of Utah Computer
    8  * Science Department.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
   35  *
   36  *      @(#)vm_mmap.c   8.4 (Berkeley) 1/12/94
   37  */
   38 
   39 /*
   40  * Mapped file (mmap) interface to VM
   41  */
   42 
   43 #include <sys/cdefs.h>
   44 __FBSDID("$FreeBSD: releng/11.1/sys/vm/vm_mmap.c 320763 2017-07-07 06:29:18Z kib $");
   45 
   46 #include "opt_compat.h"
   47 #include "opt_hwpmc_hooks.h"
   48 #include "opt_vm.h"
   49 
   50 #include <sys/param.h>
   51 #include <sys/systm.h>
   52 #include <sys/capsicum.h>
   53 #include <sys/kernel.h>
   54 #include <sys/lock.h>
   55 #include <sys/mutex.h>
   56 #include <sys/sysproto.h>
   57 #include <sys/filedesc.h>
   58 #include <sys/priv.h>
   59 #include <sys/proc.h>
   60 #include <sys/procctl.h>
   61 #include <sys/racct.h>
   62 #include <sys/resource.h>
   63 #include <sys/resourcevar.h>
   64 #include <sys/rwlock.h>
   65 #include <sys/sysctl.h>
   66 #include <sys/vnode.h>
   67 #include <sys/fcntl.h>
   68 #include <sys/file.h>
   69 #include <sys/mman.h>
   70 #include <sys/mount.h>
   71 #include <sys/conf.h>
   72 #include <sys/stat.h>
   73 #include <sys/syscallsubr.h>
   74 #include <sys/sysent.h>
   75 #include <sys/vmmeter.h>
   76 
   77 #include <security/audit/audit.h>
   78 #include <security/mac/mac_framework.h>
   79 
   80 #include <vm/vm.h>
   81 #include <vm/vm_param.h>
   82 #include <vm/pmap.h>
   83 #include <vm/vm_map.h>
   84 #include <vm/vm_object.h>
   85 #include <vm/vm_page.h>
   86 #include <vm/vm_pager.h>
   87 #include <vm/vm_pageout.h>
   88 #include <vm/vm_extern.h>
   89 #include <vm/vm_page.h>
   90 #include <vm/vnode_pager.h>
   91 
   92 #ifdef HWPMC_HOOKS
   93 #include <sys/pmckern.h>
   94 #endif
   95 
   96 int old_mlock = 0;
   97 SYSCTL_INT(_vm, OID_AUTO, old_mlock, CTLFLAG_RWTUN, &old_mlock, 0,
   98     "Do not apply RLIMIT_MEMLOCK on mlockall");
   99 
  100 #ifdef MAP_32BIT
  101 #define MAP_32BIT_MAX_ADDR      ((vm_offset_t)1 << 31)
  102 #endif
  103 
  104 #ifndef _SYS_SYSPROTO_H_
  105 struct sbrk_args {
  106         int incr;
  107 };
  108 #endif
  109 
  110 int
  111 sys_sbrk(struct thread *td, struct sbrk_args *uap)
  112 {
  113         /* Not yet implemented */
  114         return (EOPNOTSUPP);
  115 }
  116 
  117 #ifndef _SYS_SYSPROTO_H_
  118 struct sstk_args {
  119         int incr;
  120 };
  121 #endif
  122 
  123 int
  124 sys_sstk(struct thread *td, struct sstk_args *uap)
  125 {
  126         /* Not yet implemented */
  127         return (EOPNOTSUPP);
  128 }
  129 
  130 #if defined(COMPAT_43)
  131 #ifndef _SYS_SYSPROTO_H_
  132 struct getpagesize_args {
  133         int dummy;
  134 };
  135 #endif
  136 
  137 int
  138 ogetpagesize(struct thread *td, struct getpagesize_args *uap)
  139 {
  140 
  141         td->td_retval[0] = PAGE_SIZE;
  142         return (0);
  143 }
  144 #endif                          /* COMPAT_43 */
  145 
  146 
  147 /*
  148  * Memory Map (mmap) system call.  Note that the file offset
  149  * and address are allowed to be NOT page aligned, though if
  150  * the MAP_FIXED flag it set, both must have the same remainder
  151  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
  152  * page-aligned, the actual mapping starts at trunc_page(addr)
  153  * and the return value is adjusted up by the page offset.
  154  *
  155  * Generally speaking, only character devices which are themselves
  156  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
  157  * there would be no cache coherency between a descriptor and a VM mapping
  158  * both to the same character device.
  159  */
  160 #ifndef _SYS_SYSPROTO_H_
  161 struct mmap_args {
  162         void *addr;
  163         size_t len;
  164         int prot;
  165         int flags;
  166         int fd;
  167         long pad;
  168         off_t pos;
  169 };
  170 #endif
  171 
  172 int
  173 sys_mmap(struct thread *td, struct mmap_args *uap)
  174 {
  175 
  176         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
  177             uap->flags, uap->fd, uap->pos));
  178 }
  179 
  180 int
  181 kern_mmap(struct thread *td, uintptr_t addr0, size_t size, int prot, int flags,
  182     int fd, off_t pos)
  183 {
  184         struct vmspace *vms;
  185         struct file *fp;
  186         vm_offset_t addr;
  187         vm_size_t pageoff;
  188         vm_prot_t cap_maxprot;
  189         int align, error;
  190         cap_rights_t rights;
  191 
  192         vms = td->td_proc->p_vmspace;
  193         fp = NULL;
  194         AUDIT_ARG_FD(fd);
  195         addr = addr0;
  196 
  197         /*
  198          * Ignore old flags that used to be defined but did not do anything.
  199          */
  200         flags &= ~(MAP_RESERVED0020 | MAP_RESERVED0040);
  201         
  202         /*
  203          * Enforce the constraints.
  204          * Mapping of length 0 is only allowed for old binaries.
  205          * Anonymous mapping shall specify -1 as filedescriptor and
  206          * zero position for new code. Be nice to ancient a.out
  207          * binaries and correct pos for anonymous mapping, since old
  208          * ld.so sometimes issues anonymous map requests with non-zero
  209          * pos.
  210          */
  211         if (!SV_CURPROC_FLAG(SV_AOUT)) {
  212                 if ((size == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
  213                     ((flags & MAP_ANON) != 0 && (fd != -1 || pos != 0)))
  214                         return (EINVAL);
  215         } else {
  216                 if ((flags & MAP_ANON) != 0)
  217                         pos = 0;
  218         }
  219 
  220         if (flags & MAP_STACK) {
  221                 if ((fd != -1) ||
  222                     ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
  223                         return (EINVAL);
  224                 flags |= MAP_ANON;
  225                 pos = 0;
  226         }
  227         if ((flags & ~(MAP_SHARED | MAP_PRIVATE | MAP_FIXED | MAP_HASSEMAPHORE |
  228             MAP_STACK | MAP_NOSYNC | MAP_ANON | MAP_EXCL | MAP_NOCORE |
  229             MAP_PREFAULT_READ | MAP_GUARD |
  230 #ifdef MAP_32BIT
  231             MAP_32BIT |
  232 #endif
  233             MAP_ALIGNMENT_MASK)) != 0)
  234                 return (EINVAL);
  235         if ((flags & (MAP_EXCL | MAP_FIXED)) == MAP_EXCL)
  236                 return (EINVAL);
  237         if ((flags & (MAP_SHARED | MAP_PRIVATE)) == (MAP_SHARED | MAP_PRIVATE))
  238                 return (EINVAL);
  239         if (prot != PROT_NONE &&
  240             (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
  241                 return (EINVAL);
  242         if ((flags & MAP_GUARD) != 0 && (prot != PROT_NONE || fd != -1 ||
  243             pos != 0 || (flags & (MAP_SHARED | MAP_PRIVATE | MAP_PREFAULT |
  244             MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0))
  245                 return (EINVAL);
  246 
  247         /*
  248          * Align the file position to a page boundary,
  249          * and save its page offset component.
  250          */
  251         pageoff = (pos & PAGE_MASK);
  252         pos -= pageoff;
  253 
  254         /* Adjust size for rounding (on both ends). */
  255         size += pageoff;                        /* low end... */
  256         size = (vm_size_t) round_page(size);    /* hi end */
  257 
  258         /* Ensure alignment is at least a page and fits in a pointer. */
  259         align = flags & MAP_ALIGNMENT_MASK;
  260         if (align != 0 && align != MAP_ALIGNED_SUPER &&
  261             (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY ||
  262             align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
  263                 return (EINVAL);
  264 
  265         /*
  266          * Check for illegal addresses.  Watch out for address wrap... Note
  267          * that VM_*_ADDRESS are not constants due to casts (argh).
  268          */
  269         if (flags & MAP_FIXED) {
  270                 /*
  271                  * The specified address must have the same remainder
  272                  * as the file offset taken modulo PAGE_SIZE, so it
  273                  * should be aligned after adjustment by pageoff.
  274                  */
  275                 addr -= pageoff;
  276                 if (addr & PAGE_MASK)
  277                         return (EINVAL);
  278 
  279                 /* Address range must be all in user VM space. */
  280                 if (addr < vm_map_min(&vms->vm_map) ||
  281                     addr + size > vm_map_max(&vms->vm_map))
  282                         return (EINVAL);
  283                 if (addr + size < addr)
  284                         return (EINVAL);
  285 #ifdef MAP_32BIT
  286                 if (flags & MAP_32BIT && addr + size > MAP_32BIT_MAX_ADDR)
  287                         return (EINVAL);
  288         } else if (flags & MAP_32BIT) {
  289                 /*
  290                  * For MAP_32BIT, override the hint if it is too high and
  291                  * do not bother moving the mapping past the heap (since
  292                  * the heap is usually above 2GB).
  293                  */
  294                 if (addr + size > MAP_32BIT_MAX_ADDR)
  295                         addr = 0;
  296 #endif
  297         } else {
  298                 /*
  299                  * XXX for non-fixed mappings where no hint is provided or
  300                  * the hint would fall in the potential heap space,
  301                  * place it after the end of the largest possible heap.
  302                  *
  303                  * There should really be a pmap call to determine a reasonable
  304                  * location.
  305                  */
  306                 if (addr == 0 ||
  307                     (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
  308                     addr < round_page((vm_offset_t)vms->vm_daddr +
  309                     lim_max(td, RLIMIT_DATA))))
  310                         addr = round_page((vm_offset_t)vms->vm_daddr +
  311                             lim_max(td, RLIMIT_DATA));
  312         }
  313         if (size == 0) {
  314                 /*
  315                  * Return success without mapping anything for old
  316                  * binaries that request a page-aligned mapping of
  317                  * length 0.  For modern binaries, this function
  318                  * returns an error earlier.
  319                  */
  320                 error = 0;
  321         } else if ((flags & MAP_GUARD) != 0) {
  322                 error = vm_mmap_object(&vms->vm_map, &addr, size, VM_PROT_NONE,
  323                     VM_PROT_NONE, flags, NULL, pos, FALSE, td);
  324         } else if ((flags & MAP_ANON) != 0) {
  325                 /*
  326                  * Mapping blank space is trivial.
  327                  *
  328                  * This relies on VM_PROT_* matching PROT_*.
  329                  */
  330                 error = vm_mmap_object(&vms->vm_map, &addr, size, prot,
  331                     VM_PROT_ALL, flags, NULL, pos, FALSE, td);
  332         } else {
  333                 /*
  334                  * Mapping file, get fp for validation and don't let the
  335                  * descriptor disappear on us if we block. Check capability
  336                  * rights, but also return the maximum rights to be combined
  337                  * with maxprot later.
  338                  */
  339                 cap_rights_init(&rights, CAP_MMAP);
  340                 if (prot & PROT_READ)
  341                         cap_rights_set(&rights, CAP_MMAP_R);
  342                 if ((flags & MAP_SHARED) != 0) {
  343                         if (prot & PROT_WRITE)
  344                                 cap_rights_set(&rights, CAP_MMAP_W);
  345                 }
  346                 if (prot & PROT_EXEC)
  347                         cap_rights_set(&rights, CAP_MMAP_X);
  348                 error = fget_mmap(td, fd, &rights, &cap_maxprot, &fp);
  349                 if (error != 0)
  350                         goto done;
  351                 if ((flags & (MAP_SHARED | MAP_PRIVATE)) == 0 &&
  352                     td->td_proc->p_osrel >= P_OSREL_MAP_FSTRICT) {
  353                         error = EINVAL;
  354                         goto done;
  355                 }
  356 
  357                 /* This relies on VM_PROT_* matching PROT_*. */
  358                 error = fo_mmap(fp, &vms->vm_map, &addr, size, prot,
  359                     cap_maxprot, flags, pos, td);
  360         }
  361 
  362         if (error == 0)
  363                 td->td_retval[0] = (register_t) (addr + pageoff);
  364 done:
  365         if (fp)
  366                 fdrop(fp, td);
  367 
  368         return (error);
  369 }
  370 
  371 #if defined(COMPAT_FREEBSD6)
  372 int
  373 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
  374 {
  375 
  376         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, uap->prot,
  377             uap->flags, uap->fd, uap->pos));
  378 }
  379 #endif
  380 
  381 #ifdef COMPAT_43
  382 #ifndef _SYS_SYSPROTO_H_
  383 struct ommap_args {
  384         caddr_t addr;
  385         int len;
  386         int prot;
  387         int flags;
  388         int fd;
  389         long pos;
  390 };
  391 #endif
  392 int
  393 ommap(struct thread *td, struct ommap_args *uap)
  394 {
  395         static const char cvtbsdprot[8] = {
  396                 0,
  397                 PROT_EXEC,
  398                 PROT_WRITE,
  399                 PROT_EXEC | PROT_WRITE,
  400                 PROT_READ,
  401                 PROT_EXEC | PROT_READ,
  402                 PROT_WRITE | PROT_READ,
  403                 PROT_EXEC | PROT_WRITE | PROT_READ,
  404         };
  405         int flags, prot;
  406 
  407 #define OMAP_ANON       0x0002
  408 #define OMAP_COPY       0x0020
  409 #define OMAP_SHARED     0x0010
  410 #define OMAP_FIXED      0x0100
  411 
  412         prot = cvtbsdprot[uap->prot & 0x7];
  413 #ifdef COMPAT_FREEBSD32
  414 #if defined(__amd64__)
  415         if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
  416             prot != 0)
  417                 prot |= PROT_EXEC;
  418 #endif
  419 #endif
  420         flags = 0;
  421         if (uap->flags & OMAP_ANON)
  422                 flags |= MAP_ANON;
  423         if (uap->flags & OMAP_COPY)
  424                 flags |= MAP_COPY;
  425         if (uap->flags & OMAP_SHARED)
  426                 flags |= MAP_SHARED;
  427         else
  428                 flags |= MAP_PRIVATE;
  429         if (uap->flags & OMAP_FIXED)
  430                 flags |= MAP_FIXED;
  431         return (kern_mmap(td, (uintptr_t)uap->addr, uap->len, prot, flags,
  432             uap->fd, uap->pos));
  433 }
  434 #endif                          /* COMPAT_43 */
  435 
  436 
  437 #ifndef _SYS_SYSPROTO_H_
  438 struct msync_args {
  439         void *addr;
  440         size_t len;
  441         int flags;
  442 };
  443 #endif
  444 int
  445 sys_msync(struct thread *td, struct msync_args *uap)
  446 {
  447 
  448         return (kern_msync(td, (uintptr_t)uap->addr, uap->len, uap->flags));
  449 }
  450 
  451 int
  452 kern_msync(struct thread *td, uintptr_t addr0, size_t size, int flags)
  453 {
  454         vm_offset_t addr;
  455         vm_size_t pageoff;
  456         vm_map_t map;
  457         int rv;
  458 
  459         addr = addr0;
  460         pageoff = (addr & PAGE_MASK);
  461         addr -= pageoff;
  462         size += pageoff;
  463         size = (vm_size_t) round_page(size);
  464         if (addr + size < addr)
  465                 return (EINVAL);
  466 
  467         if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
  468                 return (EINVAL);
  469 
  470         map = &td->td_proc->p_vmspace->vm_map;
  471 
  472         /*
  473          * Clean the pages and interpret the return value.
  474          */
  475         rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
  476             (flags & MS_INVALIDATE) != 0);
  477         switch (rv) {
  478         case KERN_SUCCESS:
  479                 return (0);
  480         case KERN_INVALID_ADDRESS:
  481                 return (ENOMEM);
  482         case KERN_INVALID_ARGUMENT:
  483                 return (EBUSY);
  484         case KERN_FAILURE:
  485                 return (EIO);
  486         default:
  487                 return (EINVAL);
  488         }
  489 }
  490 
  491 #ifndef _SYS_SYSPROTO_H_
  492 struct munmap_args {
  493         void *addr;
  494         size_t len;
  495 };
  496 #endif
  497 int
  498 sys_munmap(struct thread *td, struct munmap_args *uap)
  499 {
  500 
  501         return (kern_munmap(td, (uintptr_t)uap->addr, uap->len));
  502 }
  503 
  504 int
  505 kern_munmap(struct thread *td, uintptr_t addr0, size_t size)
  506 {
  507 #ifdef HWPMC_HOOKS
  508         struct pmckern_map_out pkm;
  509         vm_map_entry_t entry;
  510         bool pmc_handled;
  511 #endif
  512         vm_offset_t addr;
  513         vm_size_t pageoff;
  514         vm_map_t map;
  515 
  516         if (size == 0)
  517                 return (EINVAL);
  518 
  519         addr = addr0;
  520         pageoff = (addr & PAGE_MASK);
  521         addr -= pageoff;
  522         size += pageoff;
  523         size = (vm_size_t) round_page(size);
  524         if (addr + size < addr)
  525                 return (EINVAL);
  526 
  527         /*
  528          * Check for illegal addresses.  Watch out for address wrap...
  529          */
  530         map = &td->td_proc->p_vmspace->vm_map;
  531         if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
  532                 return (EINVAL);
  533         vm_map_lock(map);
  534 #ifdef HWPMC_HOOKS
  535         pmc_handled = false;
  536         if (PMC_HOOK_INSTALLED(PMC_FN_MUNMAP)) {
  537                 pmc_handled = true;
  538                 /*
  539                  * Inform hwpmc if the address range being unmapped contains
  540                  * an executable region.
  541                  */
  542                 pkm.pm_address = (uintptr_t) NULL;
  543                 if (vm_map_lookup_entry(map, addr, &entry)) {
  544                         for (;
  545                             entry != &map->header && entry->start < addr + size;
  546                             entry = entry->next) {
  547                                 if (vm_map_check_protection(map, entry->start,
  548                                         entry->end, VM_PROT_EXECUTE) == TRUE) {
  549                                         pkm.pm_address = (uintptr_t) addr;
  550                                         pkm.pm_size = (size_t) size;
  551                                         break;
  552                                 }
  553                         }
  554                 }
  555         }
  556 #endif
  557         vm_map_delete(map, addr, addr + size);
  558 
  559 #ifdef HWPMC_HOOKS
  560         if (__predict_false(pmc_handled)) {
  561                 /* downgrade the lock to prevent a LOR with the pmc-sx lock */
  562                 vm_map_lock_downgrade(map);
  563                 if (pkm.pm_address != (uintptr_t) NULL)
  564                         PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
  565                 vm_map_unlock_read(map);
  566         } else
  567 #endif
  568                 vm_map_unlock(map);
  569 
  570         /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
  571         return (0);
  572 }
  573 
  574 #ifndef _SYS_SYSPROTO_H_
  575 struct mprotect_args {
  576         const void *addr;
  577         size_t len;
  578         int prot;
  579 };
  580 #endif
  581 int
  582 sys_mprotect(struct thread *td, struct mprotect_args *uap)
  583 {
  584 
  585         return (kern_mprotect(td, (uintptr_t)uap->addr, uap->len, uap->prot));
  586 }
  587 
  588 int
  589 kern_mprotect(struct thread *td, uintptr_t addr0, size_t size, int prot)
  590 {
  591         vm_offset_t addr;
  592         vm_size_t pageoff;
  593 
  594         addr = addr0;
  595         prot = (prot & VM_PROT_ALL);
  596         pageoff = (addr & PAGE_MASK);
  597         addr -= pageoff;
  598         size += pageoff;
  599         size = (vm_size_t) round_page(size);
  600         if (addr + size < addr)
  601                 return (EINVAL);
  602 
  603         switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
  604             addr + size, prot, FALSE)) {
  605         case KERN_SUCCESS:
  606                 return (0);
  607         case KERN_PROTECTION_FAILURE:
  608                 return (EACCES);
  609         case KERN_RESOURCE_SHORTAGE:
  610                 return (ENOMEM);
  611         }
  612         return (EINVAL);
  613 }
  614 
  615 #ifndef _SYS_SYSPROTO_H_
  616 struct minherit_args {
  617         void *addr;
  618         size_t len;
  619         int inherit;
  620 };
  621 #endif
  622 int
  623 sys_minherit(struct thread *td, struct minherit_args *uap)
  624 {
  625         vm_offset_t addr;
  626         vm_size_t size, pageoff;
  627         vm_inherit_t inherit;
  628 
  629         addr = (vm_offset_t)uap->addr;
  630         size = uap->len;
  631         inherit = uap->inherit;
  632 
  633         pageoff = (addr & PAGE_MASK);
  634         addr -= pageoff;
  635         size += pageoff;
  636         size = (vm_size_t) round_page(size);
  637         if (addr + size < addr)
  638                 return (EINVAL);
  639 
  640         switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
  641             addr + size, inherit)) {
  642         case KERN_SUCCESS:
  643                 return (0);
  644         case KERN_PROTECTION_FAILURE:
  645                 return (EACCES);
  646         }
  647         return (EINVAL);
  648 }
  649 
  650 #ifndef _SYS_SYSPROTO_H_
  651 struct madvise_args {
  652         void *addr;
  653         size_t len;
  654         int behav;
  655 };
  656 #endif
  657 
  658 int
  659 sys_madvise(struct thread *td, struct madvise_args *uap)
  660 {
  661 
  662         return (kern_madvise(td, (uintptr_t)uap->addr, uap->len, uap->behav));
  663 }
  664 
  665 int
  666 kern_madvise(struct thread *td, uintptr_t addr0, size_t len, int behav)
  667 {
  668         vm_map_t map;
  669         vm_offset_t addr, end, start;
  670         int flags;
  671 
  672         /*
  673          * Check for our special case, advising the swap pager we are
  674          * "immortal."
  675          */
  676         if (behav == MADV_PROTECT) {
  677                 flags = PPROT_SET;
  678                 return (kern_procctl(td, P_PID, td->td_proc->p_pid,
  679                     PROC_SPROTECT, &flags));
  680         }
  681 
  682         /*
  683          * Check for illegal behavior
  684          */
  685         if (behav < 0 || behav > MADV_CORE)
  686                 return (EINVAL);
  687         /*
  688          * Check for illegal addresses.  Watch out for address wrap... Note
  689          * that VM_*_ADDRESS are not constants due to casts (argh).
  690          */
  691         map = &td->td_proc->p_vmspace->vm_map;
  692         addr = addr0;
  693         if (addr < vm_map_min(map) || addr + len > vm_map_max(map))
  694                 return (EINVAL);
  695         if ((addr + len) < addr)
  696                 return (EINVAL);
  697 
  698         /*
  699          * Since this routine is only advisory, we default to conservative
  700          * behavior.
  701          */
  702         start = trunc_page(addr);
  703         end = round_page(addr + len);
  704 
  705         if (vm_map_madvise(map, start, end, behav))
  706                 return (EINVAL);
  707         return (0);
  708 }
  709 
  710 #ifndef _SYS_SYSPROTO_H_
  711 struct mincore_args {
  712         const void *addr;
  713         size_t len;
  714         char *vec;
  715 };
  716 #endif
  717 
  718 int
  719 sys_mincore(struct thread *td, struct mincore_args *uap)
  720 {
  721 
  722         return (kern_mincore(td, (uintptr_t)uap->addr, uap->len, uap->vec));
  723 }
  724 
  725 int
  726 kern_mincore(struct thread *td, uintptr_t addr0, size_t len, char *vec)
  727 {
  728         vm_offset_t addr, first_addr;
  729         vm_offset_t end, cend;
  730         pmap_t pmap;
  731         vm_map_t map;
  732         int error = 0;
  733         int vecindex, lastvecindex;
  734         vm_map_entry_t current;
  735         vm_map_entry_t entry;
  736         vm_object_t object;
  737         vm_paddr_t locked_pa;
  738         vm_page_t m;
  739         vm_pindex_t pindex;
  740         int mincoreinfo;
  741         unsigned int timestamp;
  742         boolean_t locked;
  743 
  744         /*
  745          * Make sure that the addresses presented are valid for user
  746          * mode.
  747          */
  748         first_addr = addr = trunc_page(addr0);
  749         end = addr + (vm_size_t)round_page(len);
  750         map = &td->td_proc->p_vmspace->vm_map;
  751         if (end > vm_map_max(map) || end < addr)
  752                 return (ENOMEM);
  753 
  754         pmap = vmspace_pmap(td->td_proc->p_vmspace);
  755 
  756         vm_map_lock_read(map);
  757 RestartScan:
  758         timestamp = map->timestamp;
  759 
  760         if (!vm_map_lookup_entry(map, addr, &entry)) {
  761                 vm_map_unlock_read(map);
  762                 return (ENOMEM);
  763         }
  764 
  765         /*
  766          * Do this on a map entry basis so that if the pages are not
  767          * in the current processes address space, we can easily look
  768          * up the pages elsewhere.
  769          */
  770         lastvecindex = -1;
  771         for (current = entry;
  772             (current != &map->header) && (current->start < end);
  773             current = current->next) {
  774 
  775                 /*
  776                  * check for contiguity
  777                  */
  778                 if (current->end < end &&
  779                     (entry->next == &map->header ||
  780                      current->next->start > current->end)) {
  781                         vm_map_unlock_read(map);
  782                         return (ENOMEM);
  783                 }
  784 
  785                 /*
  786                  * ignore submaps (for now) or null objects
  787                  */
  788                 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
  789                         current->object.vm_object == NULL)
  790                         continue;
  791 
  792                 /*
  793                  * limit this scan to the current map entry and the
  794                  * limits for the mincore call
  795                  */
  796                 if (addr < current->start)
  797                         addr = current->start;
  798                 cend = current->end;
  799                 if (cend > end)
  800                         cend = end;
  801 
  802                 /*
  803                  * scan this entry one page at a time
  804                  */
  805                 while (addr < cend) {
  806                         /*
  807                          * Check pmap first, it is likely faster, also
  808                          * it can provide info as to whether we are the
  809                          * one referencing or modifying the page.
  810                          */
  811                         object = NULL;
  812                         locked_pa = 0;
  813                 retry:
  814                         m = NULL;
  815                         mincoreinfo = pmap_mincore(pmap, addr, &locked_pa);
  816                         if (locked_pa != 0) {
  817                                 /*
  818                                  * The page is mapped by this process but not
  819                                  * both accessed and modified.  It is also
  820                                  * managed.  Acquire the object lock so that
  821                                  * other mappings might be examined.
  822                                  */
  823                                 m = PHYS_TO_VM_PAGE(locked_pa);
  824                                 if (m->object != object) {
  825                                         if (object != NULL)
  826                                                 VM_OBJECT_WUNLOCK(object);
  827                                         object = m->object;
  828                                         locked = VM_OBJECT_TRYWLOCK(object);
  829                                         vm_page_unlock(m);
  830                                         if (!locked) {
  831                                                 VM_OBJECT_WLOCK(object);
  832                                                 vm_page_lock(m);
  833                                                 goto retry;
  834                                         }
  835                                 } else
  836                                         vm_page_unlock(m);
  837                                 KASSERT(m->valid == VM_PAGE_BITS_ALL,
  838                                     ("mincore: page %p is mapped but invalid",
  839                                     m));
  840                         } else if (mincoreinfo == 0) {
  841                                 /*
  842                                  * The page is not mapped by this process.  If
  843                                  * the object implements managed pages, then
  844                                  * determine if the page is resident so that
  845                                  * the mappings might be examined.
  846                                  */
  847                                 if (current->object.vm_object != object) {
  848                                         if (object != NULL)
  849                                                 VM_OBJECT_WUNLOCK(object);
  850                                         object = current->object.vm_object;
  851                                         VM_OBJECT_WLOCK(object);
  852                                 }
  853                                 if (object->type == OBJT_DEFAULT ||
  854                                     object->type == OBJT_SWAP ||
  855                                     object->type == OBJT_VNODE) {
  856                                         pindex = OFF_TO_IDX(current->offset +
  857                                             (addr - current->start));
  858                                         m = vm_page_lookup(object, pindex);
  859                                         if (m != NULL && m->valid == 0)
  860                                                 m = NULL;
  861                                         if (m != NULL)
  862                                                 mincoreinfo = MINCORE_INCORE;
  863                                 }
  864                         }
  865                         if (m != NULL) {
  866                                 /* Examine other mappings to the page. */
  867                                 if (m->dirty == 0 && pmap_is_modified(m))
  868                                         vm_page_dirty(m);
  869                                 if (m->dirty != 0)
  870                                         mincoreinfo |= MINCORE_MODIFIED_OTHER;
  871                                 /*
  872                                  * The first test for PGA_REFERENCED is an
  873                                  * optimization.  The second test is
  874                                  * required because a concurrent pmap
  875                                  * operation could clear the last reference
  876                                  * and set PGA_REFERENCED before the call to
  877                                  * pmap_is_referenced(). 
  878                                  */
  879                                 if ((m->aflags & PGA_REFERENCED) != 0 ||
  880                                     pmap_is_referenced(m) ||
  881                                     (m->aflags & PGA_REFERENCED) != 0)
  882                                         mincoreinfo |= MINCORE_REFERENCED_OTHER;
  883                         }
  884                         if (object != NULL)
  885                                 VM_OBJECT_WUNLOCK(object);
  886 
  887                         /*
  888                          * subyte may page fault.  In case it needs to modify
  889                          * the map, we release the lock.
  890                          */
  891                         vm_map_unlock_read(map);
  892 
  893                         /*
  894                          * calculate index into user supplied byte vector
  895                          */
  896                         vecindex = atop(addr - first_addr);
  897 
  898                         /*
  899                          * If we have skipped map entries, we need to make sure that
  900                          * the byte vector is zeroed for those skipped entries.
  901                          */
  902                         while ((lastvecindex + 1) < vecindex) {
  903                                 ++lastvecindex;
  904                                 error = subyte(vec + lastvecindex, 0);
  905                                 if (error) {
  906                                         error = EFAULT;
  907                                         goto done2;
  908                                 }
  909                         }
  910 
  911                         /*
  912                          * Pass the page information to the user
  913                          */
  914                         error = subyte(vec + vecindex, mincoreinfo);
  915                         if (error) {
  916                                 error = EFAULT;
  917                                 goto done2;
  918                         }
  919 
  920                         /*
  921                          * If the map has changed, due to the subyte, the previous
  922                          * output may be invalid.
  923                          */
  924                         vm_map_lock_read(map);
  925                         if (timestamp != map->timestamp)
  926                                 goto RestartScan;
  927 
  928                         lastvecindex = vecindex;
  929                         addr += PAGE_SIZE;
  930                 }
  931         }
  932 
  933         /*
  934          * subyte may page fault.  In case it needs to modify
  935          * the map, we release the lock.
  936          */
  937         vm_map_unlock_read(map);
  938 
  939         /*
  940          * Zero the last entries in the byte vector.
  941          */
  942         vecindex = atop(end - first_addr);
  943         while ((lastvecindex + 1) < vecindex) {
  944                 ++lastvecindex;
  945                 error = subyte(vec + lastvecindex, 0);
  946                 if (error) {
  947                         error = EFAULT;
  948                         goto done2;
  949                 }
  950         }
  951 
  952         /*
  953          * If the map has changed, due to the subyte, the previous
  954          * output may be invalid.
  955          */
  956         vm_map_lock_read(map);
  957         if (timestamp != map->timestamp)
  958                 goto RestartScan;
  959         vm_map_unlock_read(map);
  960 done2:
  961         return (error);
  962 }
  963 
  964 #ifndef _SYS_SYSPROTO_H_
  965 struct mlock_args {
  966         const void *addr;
  967         size_t len;
  968 };
  969 #endif
  970 int
  971 sys_mlock(struct thread *td, struct mlock_args *uap)
  972 {
  973 
  974         return (kern_mlock(td->td_proc, td->td_ucred,
  975             __DECONST(uintptr_t, uap->addr), uap->len));
  976 }
  977 
  978 int
  979 kern_mlock(struct proc *proc, struct ucred *cred, uintptr_t addr0, size_t len)
  980 {
  981         vm_offset_t addr, end, last, start;
  982         vm_size_t npages, size;
  983         vm_map_t map;
  984         unsigned long nsize;
  985         int error;
  986 
  987         error = priv_check_cred(cred, PRIV_VM_MLOCK, 0);
  988         if (error)
  989                 return (error);
  990         addr = addr0;
  991         size = len;
  992         last = addr + size;
  993         start = trunc_page(addr);
  994         end = round_page(last);
  995         if (last < addr || end < addr)
  996                 return (EINVAL);
  997         npages = atop(end - start);
  998         if (npages > vm_page_max_wired)
  999                 return (ENOMEM);
 1000         map = &proc->p_vmspace->vm_map;
 1001         PROC_LOCK(proc);
 1002         nsize = ptoa(npages + pmap_wired_count(map->pmap));
 1003         if (nsize > lim_cur_proc(proc, RLIMIT_MEMLOCK)) {
 1004                 PROC_UNLOCK(proc);
 1005                 return (ENOMEM);
 1006         }
 1007         PROC_UNLOCK(proc);
 1008         if (npages + vm_cnt.v_wire_count > vm_page_max_wired)
 1009                 return (EAGAIN);
 1010 #ifdef RACCT
 1011         if (racct_enable) {
 1012                 PROC_LOCK(proc);
 1013                 error = racct_set(proc, RACCT_MEMLOCK, nsize);
 1014                 PROC_UNLOCK(proc);
 1015                 if (error != 0)
 1016                         return (ENOMEM);
 1017         }
 1018 #endif
 1019         error = vm_map_wire(map, start, end,
 1020             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
 1021 #ifdef RACCT
 1022         if (racct_enable && error != KERN_SUCCESS) {
 1023                 PROC_LOCK(proc);
 1024                 racct_set(proc, RACCT_MEMLOCK,
 1025                     ptoa(pmap_wired_count(map->pmap)));
 1026                 PROC_UNLOCK(proc);
 1027         }
 1028 #endif
 1029         return (error == KERN_SUCCESS ? 0 : ENOMEM);
 1030 }
 1031 
 1032 #ifndef _SYS_SYSPROTO_H_
 1033 struct mlockall_args {
 1034         int     how;
 1035 };
 1036 #endif
 1037 
 1038 int
 1039 sys_mlockall(struct thread *td, struct mlockall_args *uap)
 1040 {
 1041         vm_map_t map;
 1042         int error;
 1043 
 1044         map = &td->td_proc->p_vmspace->vm_map;
 1045         error = priv_check(td, PRIV_VM_MLOCK);
 1046         if (error)
 1047                 return (error);
 1048 
 1049         if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
 1050                 return (EINVAL);
 1051 
 1052         /*
 1053          * If wiring all pages in the process would cause it to exceed
 1054          * a hard resource limit, return ENOMEM.
 1055          */
 1056         if (!old_mlock && uap->how & MCL_CURRENT) {
 1057                 PROC_LOCK(td->td_proc);
 1058                 if (map->size > lim_cur(td, RLIMIT_MEMLOCK)) {
 1059                         PROC_UNLOCK(td->td_proc);
 1060                         return (ENOMEM);
 1061                 }
 1062                 PROC_UNLOCK(td->td_proc);
 1063         }
 1064 #ifdef RACCT
 1065         if (racct_enable) {
 1066                 PROC_LOCK(td->td_proc);
 1067                 error = racct_set(td->td_proc, RACCT_MEMLOCK, map->size);
 1068                 PROC_UNLOCK(td->td_proc);
 1069                 if (error != 0)
 1070                         return (ENOMEM);
 1071         }
 1072 #endif
 1073 
 1074         if (uap->how & MCL_FUTURE) {
 1075                 vm_map_lock(map);
 1076                 vm_map_modflags(map, MAP_WIREFUTURE, 0);
 1077                 vm_map_unlock(map);
 1078                 error = 0;
 1079         }
 1080 
 1081         if (uap->how & MCL_CURRENT) {
 1082                 /*
 1083                  * P1003.1-2001 mandates that all currently mapped pages
 1084                  * will be memory resident and locked (wired) upon return
 1085                  * from mlockall(). vm_map_wire() will wire pages, by
 1086                  * calling vm_fault_wire() for each page in the region.
 1087                  */
 1088                 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
 1089                     VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
 1090                 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
 1091         }
 1092 #ifdef RACCT
 1093         if (racct_enable && error != KERN_SUCCESS) {
 1094                 PROC_LOCK(td->td_proc);
 1095                 racct_set(td->td_proc, RACCT_MEMLOCK,
 1096                     ptoa(pmap_wired_count(map->pmap)));
 1097                 PROC_UNLOCK(td->td_proc);
 1098         }
 1099 #endif
 1100 
 1101         return (error);
 1102 }
 1103 
 1104 #ifndef _SYS_SYSPROTO_H_
 1105 struct munlockall_args {
 1106         register_t dummy;
 1107 };
 1108 #endif
 1109 
 1110 int
 1111 sys_munlockall(struct thread *td, struct munlockall_args *uap)
 1112 {
 1113         vm_map_t map;
 1114         int error;
 1115 
 1116         map = &td->td_proc->p_vmspace->vm_map;
 1117         error = priv_check(td, PRIV_VM_MUNLOCK);
 1118         if (error)
 1119                 return (error);
 1120 
 1121         /* Clear the MAP_WIREFUTURE flag from this vm_map. */
 1122         vm_map_lock(map);
 1123         vm_map_modflags(map, 0, MAP_WIREFUTURE);
 1124         vm_map_unlock(map);
 1125 
 1126         /* Forcibly unwire all pages. */
 1127         error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
 1128             VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
 1129 #ifdef RACCT
 1130         if (racct_enable && error == KERN_SUCCESS) {
 1131                 PROC_LOCK(td->td_proc);
 1132                 racct_set(td->td_proc, RACCT_MEMLOCK, 0);
 1133                 PROC_UNLOCK(td->td_proc);
 1134         }
 1135 #endif
 1136 
 1137         return (error);
 1138 }
 1139 
 1140 #ifndef _SYS_SYSPROTO_H_
 1141 struct munlock_args {
 1142         const void *addr;
 1143         size_t len;
 1144 };
 1145 #endif
 1146 int
 1147 sys_munlock(struct thread *td, struct munlock_args *uap)
 1148 {
 1149 
 1150         return (kern_munlock(td, (uintptr_t)uap->addr, uap->len));
 1151 }
 1152 
 1153 int
 1154 kern_munlock(struct thread *td, uintptr_t addr0, size_t size)
 1155 {
 1156         vm_offset_t addr, end, last, start;
 1157 #ifdef RACCT
 1158         vm_map_t map;
 1159 #endif
 1160         int error;
 1161 
 1162         error = priv_check(td, PRIV_VM_MUNLOCK);
 1163         if (error)
 1164                 return (error);
 1165         addr = addr0;
 1166         last = addr + size;
 1167         start = trunc_page(addr);
 1168         end = round_page(last);
 1169         if (last < addr || end < addr)
 1170                 return (EINVAL);
 1171         error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
 1172             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
 1173 #ifdef RACCT
 1174         if (racct_enable && error == KERN_SUCCESS) {
 1175                 PROC_LOCK(td->td_proc);
 1176                 map = &td->td_proc->p_vmspace->vm_map;
 1177                 racct_set(td->td_proc, RACCT_MEMLOCK,
 1178                     ptoa(pmap_wired_count(map->pmap)));
 1179                 PROC_UNLOCK(td->td_proc);
 1180         }
 1181 #endif
 1182         return (error == KERN_SUCCESS ? 0 : ENOMEM);
 1183 }
 1184 
 1185 /*
 1186  * vm_mmap_vnode()
 1187  *
 1188  * Helper function for vm_mmap.  Perform sanity check specific for mmap
 1189  * operations on vnodes.
 1190  */
 1191 int
 1192 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
 1193     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
 1194     struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp,
 1195     boolean_t *writecounted)
 1196 {
 1197         struct vattr va;
 1198         vm_object_t obj;
 1199         vm_offset_t foff;
 1200         struct ucred *cred;
 1201         int error, flags, locktype;
 1202 
 1203         cred = td->td_ucred;
 1204         if ((*maxprotp & VM_PROT_WRITE) && (*flagsp & MAP_SHARED))
 1205                 locktype = LK_EXCLUSIVE;
 1206         else
 1207                 locktype = LK_SHARED;
 1208         if ((error = vget(vp, locktype, td)) != 0)
 1209                 return (error);
 1210         AUDIT_ARG_VNODE1(vp);
 1211         foff = *foffp;
 1212         flags = *flagsp;
 1213         obj = vp->v_object;
 1214         if (vp->v_type == VREG) {
 1215                 /*
 1216                  * Get the proper underlying object
 1217                  */
 1218                 if (obj == NULL) {
 1219                         error = EINVAL;
 1220                         goto done;
 1221                 }
 1222                 if (obj->type == OBJT_VNODE && obj->handle != vp) {
 1223                         vput(vp);
 1224                         vp = (struct vnode *)obj->handle;
 1225                         /*
 1226                          * Bypass filesystems obey the mpsafety of the
 1227                          * underlying fs.  Tmpfs never bypasses.
 1228                          */
 1229                         error = vget(vp, locktype, td);
 1230                         if (error != 0)
 1231                                 return (error);
 1232                 }
 1233                 if (locktype == LK_EXCLUSIVE) {
 1234                         *writecounted = TRUE;
 1235                         vnode_pager_update_writecount(obj, 0, objsize);
 1236                 }
 1237         } else {
 1238                 error = EINVAL;
 1239                 goto done;
 1240         }
 1241         if ((error = VOP_GETATTR(vp, &va, cred)))
 1242                 goto done;
 1243 #ifdef MAC
 1244         /* This relies on VM_PROT_* matching PROT_*. */
 1245         error = mac_vnode_check_mmap(cred, vp, (int)prot, flags);
 1246         if (error != 0)
 1247                 goto done;
 1248 #endif
 1249         if ((flags & MAP_SHARED) != 0) {
 1250                 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
 1251                         if (prot & VM_PROT_WRITE) {
 1252                                 error = EPERM;
 1253                                 goto done;
 1254                         }
 1255                         *maxprotp &= ~VM_PROT_WRITE;
 1256                 }
 1257         }
 1258         /*
 1259          * If it is a regular file without any references
 1260          * we do not need to sync it.
 1261          * Adjust object size to be the size of actual file.
 1262          */
 1263         objsize = round_page(va.va_size);
 1264         if (va.va_nlink == 0)
 1265                 flags |= MAP_NOSYNC;
 1266         if (obj->type == OBJT_VNODE) {
 1267                 obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff,
 1268                     cred);
 1269                 if (obj == NULL) {
 1270                         error = ENOMEM;
 1271                         goto done;
 1272                 }
 1273         } else {
 1274                 KASSERT(obj->type == OBJT_DEFAULT || obj->type == OBJT_SWAP,
 1275                     ("wrong object type"));
 1276                 VM_OBJECT_WLOCK(obj);
 1277                 vm_object_reference_locked(obj);
 1278 #if VM_NRESERVLEVEL > 0
 1279                 vm_object_color(obj, 0);
 1280 #endif
 1281                 VM_OBJECT_WUNLOCK(obj);
 1282         }
 1283         *objp = obj;
 1284         *flagsp = flags;
 1285 
 1286         vfs_mark_atime(vp, cred);
 1287 
 1288 done:
 1289         if (error != 0 && *writecounted) {
 1290                 *writecounted = FALSE;
 1291                 vnode_pager_update_writecount(obj, objsize, 0);
 1292         }
 1293         vput(vp);
 1294         return (error);
 1295 }
 1296 
 1297 /*
 1298  * vm_mmap_cdev()
 1299  *
 1300  * Helper function for vm_mmap.  Perform sanity check specific for mmap
 1301  * operations on cdevs.
 1302  */
 1303 int
 1304 vm_mmap_cdev(struct thread *td, vm_size_t objsize, vm_prot_t prot,
 1305     vm_prot_t *maxprotp, int *flagsp, struct cdev *cdev, struct cdevsw *dsw,
 1306     vm_ooffset_t *foff, vm_object_t *objp)
 1307 {
 1308         vm_object_t obj;
 1309         int error, flags;
 1310 
 1311         flags = *flagsp;
 1312 
 1313         if (dsw->d_flags & D_MMAP_ANON) {
 1314                 *objp = NULL;
 1315                 *foff = 0;
 1316                 *maxprotp = VM_PROT_ALL;
 1317                 *flagsp |= MAP_ANON;
 1318                 return (0);
 1319         }
 1320         /*
 1321          * cdevs do not provide private mappings of any kind.
 1322          */
 1323         if ((*maxprotp & VM_PROT_WRITE) == 0 &&
 1324             (prot & VM_PROT_WRITE) != 0)
 1325                 return (EACCES);
 1326         if (flags & (MAP_PRIVATE|MAP_COPY))
 1327                 return (EINVAL);
 1328         /*
 1329          * Force device mappings to be shared.
 1330          */
 1331         flags |= MAP_SHARED;
 1332 #ifdef MAC_XXX
 1333         error = mac_cdev_check_mmap(td->td_ucred, cdev, (int)prot);
 1334         if (error != 0)
 1335                 return (error);
 1336 #endif
 1337         /*
 1338          * First, try d_mmap_single().  If that is not implemented
 1339          * (returns ENODEV), fall back to using the device pager.
 1340          * Note that d_mmap_single() must return a reference to the
 1341          * object (it needs to bump the reference count of the object
 1342          * it returns somehow).
 1343          *
 1344          * XXX assumes VM_PROT_* == PROT_*
 1345          */
 1346         error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
 1347         if (error != ENODEV)
 1348                 return (error);
 1349         obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
 1350             td->td_ucred);
 1351         if (obj == NULL)
 1352                 return (EINVAL);
 1353         *objp = obj;
 1354         *flagsp = flags;
 1355         return (0);
 1356 }
 1357 
 1358 /*
 1359  * vm_mmap()
 1360  *
 1361  * Internal version of mmap used by exec, sys5 shared memory, and
 1362  * various device drivers.  Handle is either a vnode pointer, a
 1363  * character device, or NULL for MAP_ANON.
 1364  */
 1365 int
 1366 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
 1367         vm_prot_t maxprot, int flags,
 1368         objtype_t handle_type, void *handle,
 1369         vm_ooffset_t foff)
 1370 {
 1371         vm_object_t object;
 1372         struct thread *td = curthread;
 1373         int error;
 1374         boolean_t writecounted;
 1375 
 1376         if (size == 0)
 1377                 return (EINVAL);
 1378 
 1379         size = round_page(size);
 1380         object = NULL;
 1381         writecounted = FALSE;
 1382 
 1383         /*
 1384          * Lookup/allocate object.
 1385          */
 1386         switch (handle_type) {
 1387         case OBJT_DEVICE: {
 1388                 struct cdevsw *dsw;
 1389                 struct cdev *cdev;
 1390                 int ref;
 1391 
 1392                 cdev = handle;
 1393                 dsw = dev_refthread(cdev, &ref);
 1394                 if (dsw == NULL)
 1395                         return (ENXIO);
 1396                 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, cdev,
 1397                     dsw, &foff, &object);
 1398                 dev_relthread(cdev, ref);
 1399                 break;
 1400         }
 1401         case OBJT_VNODE:
 1402                 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
 1403                     handle, &foff, &object, &writecounted);
 1404                 break;
 1405         case OBJT_DEFAULT:
 1406                 if (handle == NULL) {
 1407                         error = 0;
 1408                         break;
 1409                 }
 1410                 /* FALLTHROUGH */
 1411         default:
 1412                 error = EINVAL;
 1413                 break;
 1414         }
 1415         if (error)
 1416                 return (error);
 1417 
 1418         error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
 1419             foff, writecounted, td);
 1420         if (error != 0 && object != NULL) {
 1421                 /*
 1422                  * If this mapping was accounted for in the vnode's
 1423                  * writecount, then undo that now.
 1424                  */
 1425                 if (writecounted)
 1426                         vnode_pager_release_writecount(object, 0, size);
 1427                 vm_object_deallocate(object);
 1428         }
 1429         return (error);
 1430 }
 1431 
 1432 /*
 1433  * Internal version of mmap that maps a specific VM object into an
 1434  * map.  Called by mmap for MAP_ANON, vm_mmap, shm_mmap, and vn_mmap.
 1435  */
 1436 int
 1437 vm_mmap_object(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
 1438     vm_prot_t maxprot, int flags, vm_object_t object, vm_ooffset_t foff,
 1439     boolean_t writecounted, struct thread *td)
 1440 {
 1441         boolean_t curmap, fitit;
 1442         vm_offset_t max_addr;
 1443         int docow, error, findspace, rv;
 1444 
 1445         curmap = map == &td->td_proc->p_vmspace->vm_map;
 1446         if (curmap) {
 1447                 PROC_LOCK(td->td_proc);
 1448                 if (map->size + size > lim_cur_proc(td->td_proc, RLIMIT_VMEM)) {
 1449                         PROC_UNLOCK(td->td_proc);
 1450                         return (ENOMEM);
 1451                 }
 1452                 if (racct_set(td->td_proc, RACCT_VMEM, map->size + size)) {
 1453                         PROC_UNLOCK(td->td_proc);
 1454                         return (ENOMEM);
 1455                 }
 1456                 if (!old_mlock && map->flags & MAP_WIREFUTURE) {
 1457                         if (ptoa(pmap_wired_count(map->pmap)) + size >
 1458                             lim_cur_proc(td->td_proc, RLIMIT_MEMLOCK)) {
 1459                                 racct_set_force(td->td_proc, RACCT_VMEM,
 1460                                     map->size);
 1461                                 PROC_UNLOCK(td->td_proc);
 1462                                 return (ENOMEM);
 1463                         }
 1464                         error = racct_set(td->td_proc, RACCT_MEMLOCK,
 1465                             ptoa(pmap_wired_count(map->pmap)) + size);
 1466                         if (error != 0) {
 1467                                 racct_set_force(td->td_proc, RACCT_VMEM,
 1468                                     map->size);
 1469                                 PROC_UNLOCK(td->td_proc);
 1470                                 return (error);
 1471                         }
 1472                 }
 1473                 PROC_UNLOCK(td->td_proc);
 1474         }
 1475 
 1476         /*
 1477          * We currently can only deal with page aligned file offsets.
 1478          * The mmap() system call already enforces this by subtracting
 1479          * the page offset from the file offset, but checking here
 1480          * catches errors in device drivers (e.g. d_single_mmap()
 1481          * callbacks) and other internal mapping requests (such as in
 1482          * exec).
 1483          */
 1484         if (foff & PAGE_MASK)
 1485                 return (EINVAL);
 1486 
 1487         if ((flags & MAP_FIXED) == 0) {
 1488                 fitit = TRUE;
 1489                 *addr = round_page(*addr);
 1490         } else {
 1491                 if (*addr != trunc_page(*addr))
 1492                         return (EINVAL);
 1493                 fitit = FALSE;
 1494         }
 1495 
 1496         if (flags & MAP_ANON) {
 1497                 if (object != NULL || foff != 0)
 1498                         return (EINVAL);
 1499                 docow = 0;
 1500         } else if (flags & MAP_PREFAULT_READ)
 1501                 docow = MAP_PREFAULT;
 1502         else
 1503                 docow = MAP_PREFAULT_PARTIAL;
 1504 
 1505         if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
 1506                 docow |= MAP_COPY_ON_WRITE;
 1507         if (flags & MAP_NOSYNC)
 1508                 docow |= MAP_DISABLE_SYNCER;
 1509         if (flags & MAP_NOCORE)
 1510                 docow |= MAP_DISABLE_COREDUMP;
 1511         /* Shared memory is also shared with children. */
 1512         if (flags & MAP_SHARED)
 1513                 docow |= MAP_INHERIT_SHARE;
 1514         if (writecounted)
 1515                 docow |= MAP_VN_WRITECOUNT;
 1516         if (flags & MAP_STACK) {
 1517                 if (object != NULL)
 1518                         return (EINVAL);
 1519                 docow |= MAP_STACK_GROWS_DOWN;
 1520         }
 1521         if ((flags & MAP_EXCL) != 0)
 1522                 docow |= MAP_CHECK_EXCL;
 1523         if ((flags & MAP_GUARD) != 0)
 1524                 docow |= MAP_CREATE_GUARD;
 1525 
 1526         if (fitit) {
 1527                 if ((flags & MAP_ALIGNMENT_MASK) == MAP_ALIGNED_SUPER)
 1528                         findspace = VMFS_SUPER_SPACE;
 1529                 else if ((flags & MAP_ALIGNMENT_MASK) != 0)
 1530                         findspace = VMFS_ALIGNED_SPACE(flags >>
 1531                             MAP_ALIGNMENT_SHIFT);
 1532                 else
 1533                         findspace = VMFS_OPTIMAL_SPACE;
 1534                 max_addr = 0;
 1535 #ifdef MAP_32BIT
 1536                 if ((flags & MAP_32BIT) != 0)
 1537                         max_addr = MAP_32BIT_MAX_ADDR;
 1538 #endif
 1539                 if (curmap) {
 1540                         rv = vm_map_find_min(map, object, foff, addr, size,
 1541                             round_page((vm_offset_t)td->td_proc->p_vmspace->
 1542                             vm_daddr + lim_max(td, RLIMIT_DATA)), max_addr,
 1543                             findspace, prot, maxprot, docow);
 1544                 } else {
 1545                         rv = vm_map_find(map, object, foff, addr, size,
 1546                             max_addr, findspace, prot, maxprot, docow);
 1547                 }
 1548         } else {
 1549                 rv = vm_map_fixed(map, object, foff, *addr, size,
 1550                     prot, maxprot, docow);
 1551         }
 1552 
 1553         if (rv == KERN_SUCCESS) {
 1554                 /*
 1555                  * If the process has requested that all future mappings
 1556                  * be wired, then heed this.
 1557                  */
 1558                 if (map->flags & MAP_WIREFUTURE) {
 1559                         vm_map_wire(map, *addr, *addr + size,
 1560                             VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
 1561                             VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
 1562                 }
 1563         }
 1564         return (vm_mmap_to_errno(rv));
 1565 }
 1566 
 1567 /*
 1568  * Translate a Mach VM return code to zero on success or the appropriate errno
 1569  * on failure.
 1570  */
 1571 int
 1572 vm_mmap_to_errno(int rv)
 1573 {
 1574 
 1575         switch (rv) {
 1576         case KERN_SUCCESS:
 1577                 return (0);
 1578         case KERN_INVALID_ADDRESS:
 1579         case KERN_NO_SPACE:
 1580                 return (ENOMEM);
 1581         case KERN_PROTECTION_FAILURE:
 1582                 return (EACCES);
 1583         default:
 1584                 return (EINVAL);
 1585         }
 1586 }

Cache object: f3a2c574f3644733f35a0a4364bec9ef


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