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/8.4/sys/vm/vm_mmap.c 239592 2012-08-22 20:34:23Z kib $");
   45 
   46 #include "opt_compat.h"
   47 #include "opt_hwpmc_hooks.h"
   48 
   49 #include <sys/param.h>
   50 #include <sys/systm.h>
   51 #include <sys/lock.h>
   52 #include <sys/mutex.h>
   53 #include <sys/sysproto.h>
   54 #include <sys/filedesc.h>
   55 #include <sys/priv.h>
   56 #include <sys/proc.h>
   57 #include <sys/resource.h>
   58 #include <sys/resourcevar.h>
   59 #include <sys/vnode.h>
   60 #include <sys/fcntl.h>
   61 #include <sys/file.h>
   62 #include <sys/mman.h>
   63 #include <sys/mount.h>
   64 #include <sys/conf.h>
   65 #include <sys/stat.h>
   66 #include <sys/sysent.h>
   67 #include <sys/vmmeter.h>
   68 
   69 #include <security/mac/mac_framework.h>
   70 
   71 #include <vm/vm.h>
   72 #include <vm/vm_param.h>
   73 #include <vm/pmap.h>
   74 #include <vm/vm_map.h>
   75 #include <vm/vm_object.h>
   76 #include <vm/vm_page.h>
   77 #include <vm/vm_pager.h>
   78 #include <vm/vm_pageout.h>
   79 #include <vm/vm_extern.h>
   80 #include <vm/vm_page.h>
   81 
   82 #ifdef HWPMC_HOOKS
   83 #include <sys/pmckern.h>
   84 #endif
   85 
   86 #ifndef _SYS_SYSPROTO_H_
   87 struct sbrk_args {
   88         int incr;
   89 };
   90 #endif
   91 
   92 static int vm_mmap_vnode(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
   93     int *, struct vnode *, vm_ooffset_t *, vm_object_t *);
   94 static int vm_mmap_cdev(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
   95     int *, struct cdev *, vm_ooffset_t *, vm_object_t *);
   96 static int vm_mmap_shm(struct thread *, vm_size_t, vm_prot_t, vm_prot_t *,
   97     int *, struct shmfd *, vm_ooffset_t, vm_object_t *);
   98 
   99 /*
  100  * MPSAFE
  101  */
  102 /* ARGSUSED */
  103 int
  104 sbrk(td, uap)
  105         struct thread *td;
  106         struct sbrk_args *uap;
  107 {
  108         /* Not yet implemented */
  109         return (EOPNOTSUPP);
  110 }
  111 
  112 #ifndef _SYS_SYSPROTO_H_
  113 struct sstk_args {
  114         int incr;
  115 };
  116 #endif
  117 
  118 /*
  119  * MPSAFE
  120  */
  121 /* ARGSUSED */
  122 int
  123 sstk(td, uap)
  124         struct thread *td;
  125         struct sstk_args *uap;
  126 {
  127         /* Not yet implemented */
  128         return (EOPNOTSUPP);
  129 }
  130 
  131 #if defined(COMPAT_43)
  132 #ifndef _SYS_SYSPROTO_H_
  133 struct getpagesize_args {
  134         int dummy;
  135 };
  136 #endif
  137 
  138 /* ARGSUSED */
  139 int
  140 ogetpagesize(td, uap)
  141         struct thread *td;
  142         struct getpagesize_args *uap;
  143 {
  144         /* MP SAFE */
  145         td->td_retval[0] = PAGE_SIZE;
  146         return (0);
  147 }
  148 #endif                          /* COMPAT_43 */
  149 
  150 
  151 /*
  152  * Memory Map (mmap) system call.  Note that the file offset
  153  * and address are allowed to be NOT page aligned, though if
  154  * the MAP_FIXED flag it set, both must have the same remainder
  155  * modulo the PAGE_SIZE (POSIX 1003.1b).  If the address is not
  156  * page-aligned, the actual mapping starts at trunc_page(addr)
  157  * and the return value is adjusted up by the page offset.
  158  *
  159  * Generally speaking, only character devices which are themselves
  160  * memory-based, such as a video framebuffer, can be mmap'd.  Otherwise
  161  * there would be no cache coherency between a descriptor and a VM mapping
  162  * both to the same character device.
  163  */
  164 #ifndef _SYS_SYSPROTO_H_
  165 struct mmap_args {
  166         void *addr;
  167         size_t len;
  168         int prot;
  169         int flags;
  170         int fd;
  171         long pad;
  172         off_t pos;
  173 };
  174 #endif
  175 
  176 /*
  177  * MPSAFE
  178  */
  179 int
  180 mmap(td, uap)
  181         struct thread *td;
  182         struct mmap_args *uap;
  183 {
  184 #ifdef HWPMC_HOOKS
  185         struct pmckern_map_in pkm;
  186 #endif
  187         struct file *fp;
  188         struct vnode *vp;
  189         vm_offset_t addr;
  190         vm_size_t size, pageoff;
  191         vm_prot_t prot, maxprot;
  192         void *handle;
  193         objtype_t handle_type;
  194         int flags, error;
  195         off_t pos;
  196         struct vmspace *vms = td->td_proc->p_vmspace;
  197 
  198         addr = (vm_offset_t) uap->addr;
  199         size = uap->len;
  200         prot = uap->prot & VM_PROT_ALL;
  201         flags = uap->flags;
  202         pos = uap->pos;
  203 
  204         fp = NULL;
  205 
  206         /*
  207          * Enforce the constraints.
  208          * Mapping of length 0 is only allowed for old binaries.
  209          * Anonymous mapping shall specify -1 as filedescriptor and
  210          * zero position for new code. Be nice to ancient a.out
  211          * binaries and correct pos for anonymous mapping, since old
  212          * ld.so sometimes issues anonymous map requests with non-zero
  213          * pos.
  214          */
  215         if (!SV_CURPROC_FLAG(SV_AOUT)) {
  216                 if ((uap->len == 0 && curproc->p_osrel >= P_OSREL_MAP_ANON) ||
  217                     ((flags & MAP_ANON) != 0 && (uap->fd != -1 || pos != 0)))
  218                         return (EINVAL);
  219         } else {
  220                 if ((flags & MAP_ANON) != 0)
  221                         pos = 0;
  222         }
  223 
  224         if (flags & MAP_STACK) {
  225                 if ((uap->fd != -1) ||
  226                     ((prot & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)))
  227                         return (EINVAL);
  228                 flags |= MAP_ANON;
  229                 pos = 0;
  230         }
  231 
  232         /*
  233          * Align the file position to a page boundary,
  234          * and save its page offset component.
  235          */
  236         pageoff = (pos & PAGE_MASK);
  237         pos -= pageoff;
  238 
  239         /* Adjust size for rounding (on both ends). */
  240         size += pageoff;                        /* low end... */
  241         size = (vm_size_t) round_page(size);    /* hi end */
  242 
  243         /*
  244          * Check for illegal addresses.  Watch out for address wrap... Note
  245          * that VM_*_ADDRESS are not constants due to casts (argh).
  246          */
  247         if (flags & MAP_FIXED) {
  248                 /*
  249                  * The specified address must have the same remainder
  250                  * as the file offset taken modulo PAGE_SIZE, so it
  251                  * should be aligned after adjustment by pageoff.
  252                  */
  253                 addr -= pageoff;
  254                 if (addr & PAGE_MASK)
  255                         return (EINVAL);
  256                 /* Address range must be all in user VM space. */
  257                 if (addr < vm_map_min(&vms->vm_map) ||
  258                     addr + size > vm_map_max(&vms->vm_map))
  259                         return (EINVAL);
  260                 if (addr + size < addr)
  261                         return (EINVAL);
  262         } else {
  263         /*
  264          * XXX for non-fixed mappings where no hint is provided or
  265          * the hint would fall in the potential heap space,
  266          * place it after the end of the largest possible heap.
  267          *
  268          * There should really be a pmap call to determine a reasonable
  269          * location.
  270          */
  271                 PROC_LOCK(td->td_proc);
  272                 if (addr == 0 ||
  273                     (addr >= round_page((vm_offset_t)vms->vm_taddr) &&
  274                     addr < round_page((vm_offset_t)vms->vm_daddr +
  275                     lim_max(td->td_proc, RLIMIT_DATA))))
  276                         addr = round_page((vm_offset_t)vms->vm_daddr +
  277                             lim_max(td->td_proc, RLIMIT_DATA));
  278                 PROC_UNLOCK(td->td_proc);
  279         }
  280         if (flags & MAP_ANON) {
  281                 /*
  282                  * Mapping blank space is trivial.
  283                  */
  284                 handle = NULL;
  285                 handle_type = OBJT_DEFAULT;
  286                 maxprot = VM_PROT_ALL;
  287         } else {
  288                 /*
  289                  * Mapping file, get fp for validation and
  290                  * don't let the descriptor disappear on us if we block.
  291                  */
  292                 if ((error = fget(td, uap->fd, &fp)) != 0)
  293                         goto done;
  294                 if (fp->f_type == DTYPE_SHM) {
  295                         handle = fp->f_data;
  296                         handle_type = OBJT_SWAP;
  297                         maxprot = VM_PROT_NONE;
  298 
  299                         /* FREAD should always be set. */
  300                         if (fp->f_flag & FREAD)
  301                                 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
  302                         if (fp->f_flag & FWRITE)
  303                                 maxprot |= VM_PROT_WRITE;
  304                         goto map;
  305                 }
  306                 if (fp->f_type != DTYPE_VNODE) {
  307                         error = ENODEV;
  308                         goto done;
  309                 }
  310 #if defined(COMPAT_FREEBSD7) || defined(COMPAT_FREEBSD6) || \
  311     defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4)
  312                 /*
  313                  * POSIX shared-memory objects are defined to have
  314                  * kernel persistence, and are not defined to support
  315                  * read(2)/write(2) -- or even open(2).  Thus, we can
  316                  * use MAP_ASYNC to trade on-disk coherence for speed.
  317                  * The shm_open(3) library routine turns on the FPOSIXSHM
  318                  * flag to request this behavior.
  319                  */
  320                 if (fp->f_flag & FPOSIXSHM)
  321                         flags |= MAP_NOSYNC;
  322 #endif
  323                 vp = fp->f_vnode;
  324                 /*
  325                  * Ensure that file and memory protections are
  326                  * compatible.  Note that we only worry about
  327                  * writability if mapping is shared; in this case,
  328                  * current and max prot are dictated by the open file.
  329                  * XXX use the vnode instead?  Problem is: what
  330                  * credentials do we use for determination? What if
  331                  * proc does a setuid?
  332                  */
  333                 if (vp->v_mount != NULL && vp->v_mount->mnt_flag & MNT_NOEXEC)
  334                         maxprot = VM_PROT_NONE;
  335                 else
  336                         maxprot = VM_PROT_EXECUTE;
  337                 if (fp->f_flag & FREAD) {
  338                         maxprot |= VM_PROT_READ;
  339                 } else if (prot & PROT_READ) {
  340                         error = EACCES;
  341                         goto done;
  342                 }
  343                 /*
  344                  * If we are sharing potential changes (either via
  345                  * MAP_SHARED or via the implicit sharing of character
  346                  * device mappings), and we are trying to get write
  347                  * permission although we opened it without asking
  348                  * for it, bail out.
  349                  */
  350                 if ((flags & MAP_SHARED) != 0) {
  351                         if ((fp->f_flag & FWRITE) != 0) {
  352                                 maxprot |= VM_PROT_WRITE;
  353                         } else if ((prot & PROT_WRITE) != 0) {
  354                                 error = EACCES;
  355                                 goto done;
  356                         }
  357                 } else if (vp->v_type != VCHR || (fp->f_flag & FWRITE) != 0) {
  358                         maxprot |= VM_PROT_WRITE;
  359                 }
  360                 handle = (void *)vp;
  361                 handle_type = OBJT_VNODE;
  362         }
  363 map:
  364         td->td_fpop = fp;
  365         error = vm_mmap(&vms->vm_map, &addr, size, prot, maxprot,
  366             flags, handle_type, handle, pos);
  367         td->td_fpop = NULL;
  368 #ifdef HWPMC_HOOKS
  369         /* inform hwpmc(4) if an executable is being mapped */
  370         if (error == 0 && handle_type == OBJT_VNODE &&
  371             (prot & PROT_EXEC)) {
  372                 pkm.pm_file = handle;
  373                 pkm.pm_address = (uintptr_t) addr;
  374                 PMC_CALL_HOOK(td, PMC_FN_MMAP, (void *) &pkm);
  375         }
  376 #endif
  377         if (error == 0)
  378                 td->td_retval[0] = (register_t) (addr + pageoff);
  379 done:
  380         if (fp)
  381                 fdrop(fp, td);
  382 
  383         return (error);
  384 }
  385 
  386 int
  387 freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
  388 {
  389         struct mmap_args oargs;
  390 
  391         oargs.addr = uap->addr;
  392         oargs.len = uap->len;
  393         oargs.prot = uap->prot;
  394         oargs.flags = uap->flags;
  395         oargs.fd = uap->fd;
  396         oargs.pos = uap->pos;
  397         return (mmap(td, &oargs));
  398 }
  399 
  400 #ifdef COMPAT_43
  401 #ifndef _SYS_SYSPROTO_H_
  402 struct ommap_args {
  403         caddr_t addr;
  404         int len;
  405         int prot;
  406         int flags;
  407         int fd;
  408         long pos;
  409 };
  410 #endif
  411 int
  412 ommap(td, uap)
  413         struct thread *td;
  414         struct ommap_args *uap;
  415 {
  416         struct mmap_args nargs;
  417         static const char cvtbsdprot[8] = {
  418                 0,
  419                 PROT_EXEC,
  420                 PROT_WRITE,
  421                 PROT_EXEC | PROT_WRITE,
  422                 PROT_READ,
  423                 PROT_EXEC | PROT_READ,
  424                 PROT_WRITE | PROT_READ,
  425                 PROT_EXEC | PROT_WRITE | PROT_READ,
  426         };
  427 
  428 #define OMAP_ANON       0x0002
  429 #define OMAP_COPY       0x0020
  430 #define OMAP_SHARED     0x0010
  431 #define OMAP_FIXED      0x0100
  432 
  433         nargs.addr = uap->addr;
  434         nargs.len = uap->len;
  435         nargs.prot = cvtbsdprot[uap->prot & 0x7];
  436         nargs.flags = 0;
  437         if (uap->flags & OMAP_ANON)
  438                 nargs.flags |= MAP_ANON;
  439         if (uap->flags & OMAP_COPY)
  440                 nargs.flags |= MAP_COPY;
  441         if (uap->flags & OMAP_SHARED)
  442                 nargs.flags |= MAP_SHARED;
  443         else
  444                 nargs.flags |= MAP_PRIVATE;
  445         if (uap->flags & OMAP_FIXED)
  446                 nargs.flags |= MAP_FIXED;
  447         nargs.fd = uap->fd;
  448         nargs.pos = uap->pos;
  449         return (mmap(td, &nargs));
  450 }
  451 #endif                          /* COMPAT_43 */
  452 
  453 
  454 #ifndef _SYS_SYSPROTO_H_
  455 struct msync_args {
  456         void *addr;
  457         size_t len;
  458         int flags;
  459 };
  460 #endif
  461 /*
  462  * MPSAFE
  463  */
  464 int
  465 msync(td, uap)
  466         struct thread *td;
  467         struct msync_args *uap;
  468 {
  469         vm_offset_t addr;
  470         vm_size_t size, pageoff;
  471         int flags;
  472         vm_map_t map;
  473         int rv;
  474 
  475         addr = (vm_offset_t) uap->addr;
  476         size = uap->len;
  477         flags = uap->flags;
  478 
  479         pageoff = (addr & PAGE_MASK);
  480         addr -= pageoff;
  481         size += pageoff;
  482         size = (vm_size_t) round_page(size);
  483         if (addr + size < addr)
  484                 return (EINVAL);
  485 
  486         if ((flags & (MS_ASYNC|MS_INVALIDATE)) == (MS_ASYNC|MS_INVALIDATE))
  487                 return (EINVAL);
  488 
  489         map = &td->td_proc->p_vmspace->vm_map;
  490 
  491         /*
  492          * Clean the pages and interpret the return value.
  493          */
  494         rv = vm_map_sync(map, addr, addr + size, (flags & MS_ASYNC) == 0,
  495             (flags & MS_INVALIDATE) != 0);
  496         switch (rv) {
  497         case KERN_SUCCESS:
  498                 return (0);
  499         case KERN_INVALID_ADDRESS:
  500                 return (EINVAL);        /* Sun returns ENOMEM? */
  501         case KERN_INVALID_ARGUMENT:
  502                 return (EBUSY);
  503         case KERN_FAILURE:
  504                 return (EIO);
  505         default:
  506                 return (EINVAL);
  507         }
  508 }
  509 
  510 #ifndef _SYS_SYSPROTO_H_
  511 struct munmap_args {
  512         void *addr;
  513         size_t len;
  514 };
  515 #endif
  516 /*
  517  * MPSAFE
  518  */
  519 int
  520 munmap(td, uap)
  521         struct thread *td;
  522         struct munmap_args *uap;
  523 {
  524 #ifdef HWPMC_HOOKS
  525         struct pmckern_map_out pkm;
  526         vm_map_entry_t entry;
  527 #endif
  528         vm_offset_t addr;
  529         vm_size_t size, pageoff;
  530         vm_map_t map;
  531 
  532         addr = (vm_offset_t) uap->addr;
  533         size = uap->len;
  534         if (size == 0)
  535                 return (EINVAL);
  536 
  537         pageoff = (addr & PAGE_MASK);
  538         addr -= pageoff;
  539         size += pageoff;
  540         size = (vm_size_t) round_page(size);
  541         if (addr + size < addr)
  542                 return (EINVAL);
  543 
  544         /*
  545          * Check for illegal addresses.  Watch out for address wrap...
  546          */
  547         map = &td->td_proc->p_vmspace->vm_map;
  548         if (addr < vm_map_min(map) || addr + size > vm_map_max(map))
  549                 return (EINVAL);
  550         vm_map_lock(map);
  551 #ifdef HWPMC_HOOKS
  552         /*
  553          * Inform hwpmc if the address range being unmapped contains
  554          * an executable region.
  555          */
  556         pkm.pm_address = (uintptr_t) NULL;
  557         if (vm_map_lookup_entry(map, addr, &entry)) {
  558                 for (;
  559                      entry != &map->header && entry->start < addr + size;
  560                      entry = entry->next) {
  561                         if (vm_map_check_protection(map, entry->start,
  562                                 entry->end, VM_PROT_EXECUTE) == TRUE) {
  563                                 pkm.pm_address = (uintptr_t) addr;
  564                                 pkm.pm_size = (size_t) size;
  565                                 break;
  566                         }
  567                 }
  568         }
  569 #endif
  570         vm_map_delete(map, addr, addr + size);
  571 
  572 #ifdef HWPMC_HOOKS
  573         /* downgrade the lock to prevent a LOR with the pmc-sx lock */
  574         vm_map_lock_downgrade(map);
  575         if (pkm.pm_address != (uintptr_t) NULL)
  576                 PMC_CALL_HOOK(td, PMC_FN_MUNMAP, (void *) &pkm);
  577         vm_map_unlock_read(map);
  578 #else
  579         vm_map_unlock(map);
  580 #endif
  581         /* vm_map_delete returns nothing but KERN_SUCCESS anyway */
  582         return (0);
  583 }
  584 
  585 #ifndef _SYS_SYSPROTO_H_
  586 struct mprotect_args {
  587         const void *addr;
  588         size_t len;
  589         int prot;
  590 };
  591 #endif
  592 /*
  593  * MPSAFE
  594  */
  595 int
  596 mprotect(td, uap)
  597         struct thread *td;
  598         struct mprotect_args *uap;
  599 {
  600         vm_offset_t addr;
  601         vm_size_t size, pageoff;
  602         vm_prot_t prot;
  603 
  604         addr = (vm_offset_t) uap->addr;
  605         size = uap->len;
  606         prot = uap->prot & VM_PROT_ALL;
  607 
  608         pageoff = (addr & PAGE_MASK);
  609         addr -= pageoff;
  610         size += pageoff;
  611         size = (vm_size_t) round_page(size);
  612         if (addr + size < addr)
  613                 return (EINVAL);
  614 
  615         switch (vm_map_protect(&td->td_proc->p_vmspace->vm_map, addr,
  616             addr + size, prot, FALSE)) {
  617         case KERN_SUCCESS:
  618                 return (0);
  619         case KERN_PROTECTION_FAILURE:
  620                 return (EACCES);
  621         case KERN_RESOURCE_SHORTAGE:
  622                 return (ENOMEM);
  623         }
  624         return (EINVAL);
  625 }
  626 
  627 #ifndef _SYS_SYSPROTO_H_
  628 struct minherit_args {
  629         void *addr;
  630         size_t len;
  631         int inherit;
  632 };
  633 #endif
  634 /*
  635  * MPSAFE
  636  */
  637 int
  638 minherit(td, uap)
  639         struct thread *td;
  640         struct minherit_args *uap;
  641 {
  642         vm_offset_t addr;
  643         vm_size_t size, pageoff;
  644         vm_inherit_t inherit;
  645 
  646         addr = (vm_offset_t)uap->addr;
  647         size = uap->len;
  648         inherit = uap->inherit;
  649 
  650         pageoff = (addr & PAGE_MASK);
  651         addr -= pageoff;
  652         size += pageoff;
  653         size = (vm_size_t) round_page(size);
  654         if (addr + size < addr)
  655                 return (EINVAL);
  656 
  657         switch (vm_map_inherit(&td->td_proc->p_vmspace->vm_map, addr,
  658             addr + size, inherit)) {
  659         case KERN_SUCCESS:
  660                 return (0);
  661         case KERN_PROTECTION_FAILURE:
  662                 return (EACCES);
  663         }
  664         return (EINVAL);
  665 }
  666 
  667 #ifndef _SYS_SYSPROTO_H_
  668 struct madvise_args {
  669         void *addr;
  670         size_t len;
  671         int behav;
  672 };
  673 #endif
  674 
  675 /*
  676  * MPSAFE
  677  */
  678 /* ARGSUSED */
  679 int
  680 madvise(td, uap)
  681         struct thread *td;
  682         struct madvise_args *uap;
  683 {
  684         vm_offset_t start, end;
  685         vm_map_t map;
  686         struct proc *p;
  687         int error;
  688 
  689         /*
  690          * Check for our special case, advising the swap pager we are
  691          * "immortal."
  692          */
  693         if (uap->behav == MADV_PROTECT) {
  694                 error = priv_check(td, PRIV_VM_MADV_PROTECT);
  695                 if (error == 0) {
  696                         p = td->td_proc;
  697                         PROC_LOCK(p);
  698                         p->p_flag |= P_PROTECTED;
  699                         PROC_UNLOCK(p);
  700                 }
  701                 return (error);
  702         }
  703         /*
  704          * Check for illegal behavior
  705          */
  706         if (uap->behav < 0 || uap->behav > MADV_CORE)
  707                 return (EINVAL);
  708         /*
  709          * Check for illegal addresses.  Watch out for address wrap... Note
  710          * that VM_*_ADDRESS are not constants due to casts (argh).
  711          */
  712         map = &td->td_proc->p_vmspace->vm_map;
  713         if ((vm_offset_t)uap->addr < vm_map_min(map) ||
  714             (vm_offset_t)uap->addr + uap->len > vm_map_max(map))
  715                 return (EINVAL);
  716         if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
  717                 return (EINVAL);
  718 
  719         /*
  720          * Since this routine is only advisory, we default to conservative
  721          * behavior.
  722          */
  723         start = trunc_page((vm_offset_t) uap->addr);
  724         end = round_page((vm_offset_t) uap->addr + uap->len);
  725 
  726         if (vm_map_madvise(map, start, end, uap->behav))
  727                 return (EINVAL);
  728         return (0);
  729 }
  730 
  731 #ifndef _SYS_SYSPROTO_H_
  732 struct mincore_args {
  733         const void *addr;
  734         size_t len;
  735         char *vec;
  736 };
  737 #endif
  738 
  739 /*
  740  * MPSAFE
  741  */
  742 /* ARGSUSED */
  743 int
  744 mincore(td, uap)
  745         struct thread *td;
  746         struct mincore_args *uap;
  747 {
  748         vm_offset_t addr, first_addr;
  749         vm_offset_t end, cend;
  750         pmap_t pmap;
  751         vm_map_t map;
  752         char *vec;
  753         int error = 0;
  754         int vecindex, lastvecindex;
  755         vm_map_entry_t current;
  756         vm_map_entry_t entry;
  757         int mincoreinfo;
  758         unsigned int timestamp;
  759 
  760         /*
  761          * Make sure that the addresses presented are valid for user
  762          * mode.
  763          */
  764         first_addr = addr = trunc_page((vm_offset_t) uap->addr);
  765         end = addr + (vm_size_t)round_page(uap->len);
  766         map = &td->td_proc->p_vmspace->vm_map;
  767         if (end > vm_map_max(map) || end < addr)
  768                 return (ENOMEM);
  769 
  770         /*
  771          * Address of byte vector
  772          */
  773         vec = uap->vec;
  774 
  775         pmap = vmspace_pmap(td->td_proc->p_vmspace);
  776 
  777         vm_map_lock_read(map);
  778 RestartScan:
  779         timestamp = map->timestamp;
  780 
  781         if (!vm_map_lookup_entry(map, addr, &entry)) {
  782                 vm_map_unlock_read(map);
  783                 return (ENOMEM);
  784         }
  785 
  786         /*
  787          * Do this on a map entry basis so that if the pages are not
  788          * in the current processes address space, we can easily look
  789          * up the pages elsewhere.
  790          */
  791         lastvecindex = -1;
  792         for (current = entry;
  793             (current != &map->header) && (current->start < end);
  794             current = current->next) {
  795 
  796                 /*
  797                  * check for contiguity
  798                  */
  799                 if (current->end < end &&
  800                     (entry->next == &map->header ||
  801                      current->next->start > current->end)) {
  802                         vm_map_unlock_read(map);
  803                         return (ENOMEM);
  804                 }
  805 
  806                 /*
  807                  * ignore submaps (for now) or null objects
  808                  */
  809                 if ((current->eflags & MAP_ENTRY_IS_SUB_MAP) ||
  810                         current->object.vm_object == NULL)
  811                         continue;
  812 
  813                 /*
  814                  * limit this scan to the current map entry and the
  815                  * limits for the mincore call
  816                  */
  817                 if (addr < current->start)
  818                         addr = current->start;
  819                 cend = current->end;
  820                 if (cend > end)
  821                         cend = end;
  822 
  823                 /*
  824                  * scan this entry one page at a time
  825                  */
  826                 while (addr < cend) {
  827                         /*
  828                          * Check pmap first, it is likely faster, also
  829                          * it can provide info as to whether we are the
  830                          * one referencing or modifying the page.
  831                          */
  832                         mincoreinfo = pmap_mincore(pmap, addr);
  833                         if (!mincoreinfo) {
  834                                 vm_pindex_t pindex;
  835                                 vm_ooffset_t offset;
  836                                 vm_page_t m;
  837                                 /*
  838                                  * calculate the page index into the object
  839                                  */
  840                                 offset = current->offset + (addr - current->start);
  841                                 pindex = OFF_TO_IDX(offset);
  842                                 VM_OBJECT_LOCK(current->object.vm_object);
  843                                 m = vm_page_lookup(current->object.vm_object,
  844                                         pindex);
  845                                 /*
  846                                  * if the page is resident, then gather information about
  847                                  * it.
  848                                  */
  849                                 if (m != NULL && m->valid != 0) {
  850                                         mincoreinfo = MINCORE_INCORE;
  851                                         vm_page_lock_queues();
  852                                         if (m->dirty ||
  853                                                 pmap_is_modified(m))
  854                                                 mincoreinfo |= MINCORE_MODIFIED_OTHER;
  855                                         if ((m->flags & PG_REFERENCED) ||
  856                                                 pmap_ts_referenced(m)) {
  857                                                 vm_page_flag_set(m, PG_REFERENCED);
  858                                                 mincoreinfo |= MINCORE_REFERENCED_OTHER;
  859                                         }
  860                                         vm_page_unlock_queues();
  861                                 }
  862                                 VM_OBJECT_UNLOCK(current->object.vm_object);
  863                         }
  864 
  865                         /*
  866                          * subyte may page fault.  In case it needs to modify
  867                          * the map, we release the lock.
  868                          */
  869                         vm_map_unlock_read(map);
  870 
  871                         /*
  872                          * calculate index into user supplied byte vector
  873                          */
  874                         vecindex = OFF_TO_IDX(addr - first_addr);
  875 
  876                         /*
  877                          * If we have skipped map entries, we need to make sure that
  878                          * the byte vector is zeroed for those skipped entries.
  879                          */
  880                         while ((lastvecindex + 1) < vecindex) {
  881                                 error = subyte(vec + lastvecindex, 0);
  882                                 if (error) {
  883                                         error = EFAULT;
  884                                         goto done2;
  885                                 }
  886                                 ++lastvecindex;
  887                         }
  888 
  889                         /*
  890                          * Pass the page information to the user
  891                          */
  892                         error = subyte(vec + vecindex, mincoreinfo);
  893                         if (error) {
  894                                 error = EFAULT;
  895                                 goto done2;
  896                         }
  897 
  898                         /*
  899                          * If the map has changed, due to the subyte, the previous
  900                          * output may be invalid.
  901                          */
  902                         vm_map_lock_read(map);
  903                         if (timestamp != map->timestamp)
  904                                 goto RestartScan;
  905 
  906                         lastvecindex = vecindex;
  907                         addr += PAGE_SIZE;
  908                 }
  909         }
  910 
  911         /*
  912          * subyte may page fault.  In case it needs to modify
  913          * the map, we release the lock.
  914          */
  915         vm_map_unlock_read(map);
  916 
  917         /*
  918          * Zero the last entries in the byte vector.
  919          */
  920         vecindex = OFF_TO_IDX(end - first_addr);
  921         while ((lastvecindex + 1) < vecindex) {
  922                 error = subyte(vec + lastvecindex, 0);
  923                 if (error) {
  924                         error = EFAULT;
  925                         goto done2;
  926                 }
  927                 ++lastvecindex;
  928         }
  929 
  930         /*
  931          * If the map has changed, due to the subyte, the previous
  932          * output may be invalid.
  933          */
  934         vm_map_lock_read(map);
  935         if (timestamp != map->timestamp)
  936                 goto RestartScan;
  937         vm_map_unlock_read(map);
  938 done2:
  939         return (error);
  940 }
  941 
  942 #ifndef _SYS_SYSPROTO_H_
  943 struct mlock_args {
  944         const void *addr;
  945         size_t len;
  946 };
  947 #endif
  948 /*
  949  * MPSAFE
  950  */
  951 int
  952 mlock(td, uap)
  953         struct thread *td;
  954         struct mlock_args *uap;
  955 {
  956         struct proc *proc;
  957         vm_offset_t addr, end, last, start;
  958         vm_size_t npages, size;
  959         int error;
  960 
  961         error = priv_check(td, PRIV_VM_MLOCK);
  962         if (error)
  963                 return (error);
  964         addr = (vm_offset_t)uap->addr;
  965         size = uap->len;
  966         last = addr + size;
  967         start = trunc_page(addr);
  968         end = round_page(last);
  969         if (last < addr || end < addr)
  970                 return (EINVAL);
  971         npages = atop(end - start);
  972         if (npages > vm_page_max_wired)
  973                 return (ENOMEM);
  974         proc = td->td_proc;
  975         PROC_LOCK(proc);
  976         if (ptoa(npages +
  977             pmap_wired_count(vm_map_pmap(&proc->p_vmspace->vm_map))) >
  978             lim_cur(proc, RLIMIT_MEMLOCK)) {
  979                 PROC_UNLOCK(proc);
  980                 return (ENOMEM);
  981         }
  982         PROC_UNLOCK(proc);
  983         if (npages + cnt.v_wire_count > vm_page_max_wired)
  984                 return (EAGAIN);
  985         error = vm_map_wire(&proc->p_vmspace->vm_map, start, end,
  986             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
  987         return (error == KERN_SUCCESS ? 0 : ENOMEM);
  988 }
  989 
  990 #ifndef _SYS_SYSPROTO_H_
  991 struct mlockall_args {
  992         int     how;
  993 };
  994 #endif
  995 
  996 /*
  997  * MPSAFE
  998  */
  999 int
 1000 mlockall(td, uap)
 1001         struct thread *td;
 1002         struct mlockall_args *uap;
 1003 {
 1004         vm_map_t map;
 1005         int error;
 1006 
 1007         map = &td->td_proc->p_vmspace->vm_map;
 1008         error = 0;
 1009 
 1010         if ((uap->how == 0) || ((uap->how & ~(MCL_CURRENT|MCL_FUTURE)) != 0))
 1011                 return (EINVAL);
 1012 
 1013 #if 0
 1014         /*
 1015          * If wiring all pages in the process would cause it to exceed
 1016          * a hard resource limit, return ENOMEM.
 1017          */
 1018         PROC_LOCK(td->td_proc);
 1019         if (map->size - ptoa(pmap_wired_count(vm_map_pmap(map)) >
 1020                 lim_cur(td->td_proc, RLIMIT_MEMLOCK))) {
 1021                 PROC_UNLOCK(td->td_proc);
 1022                 return (ENOMEM);
 1023         }
 1024         PROC_UNLOCK(td->td_proc);
 1025 #else
 1026         error = priv_check(td, PRIV_VM_MLOCK);
 1027         if (error)
 1028                 return (error);
 1029 #endif
 1030 
 1031         if (uap->how & MCL_FUTURE) {
 1032                 vm_map_lock(map);
 1033                 vm_map_modflags(map, MAP_WIREFUTURE, 0);
 1034                 vm_map_unlock(map);
 1035                 error = 0;
 1036         }
 1037 
 1038         if (uap->how & MCL_CURRENT) {
 1039                 /*
 1040                  * P1003.1-2001 mandates that all currently mapped pages
 1041                  * will be memory resident and locked (wired) upon return
 1042                  * from mlockall(). vm_map_wire() will wire pages, by
 1043                  * calling vm_fault_wire() for each page in the region.
 1044                  */
 1045                 error = vm_map_wire(map, vm_map_min(map), vm_map_max(map),
 1046                     VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
 1047                 error = (error == KERN_SUCCESS ? 0 : EAGAIN);
 1048         }
 1049 
 1050         return (error);
 1051 }
 1052 
 1053 #ifndef _SYS_SYSPROTO_H_
 1054 struct munlockall_args {
 1055         register_t dummy;
 1056 };
 1057 #endif
 1058 
 1059 /*
 1060  * MPSAFE
 1061  */
 1062 int
 1063 munlockall(td, uap)
 1064         struct thread *td;
 1065         struct munlockall_args *uap;
 1066 {
 1067         vm_map_t map;
 1068         int error;
 1069 
 1070         map = &td->td_proc->p_vmspace->vm_map;
 1071         error = priv_check(td, PRIV_VM_MUNLOCK);
 1072         if (error)
 1073                 return (error);
 1074 
 1075         /* Clear the MAP_WIREFUTURE flag from this vm_map. */
 1076         vm_map_lock(map);
 1077         vm_map_modflags(map, 0, MAP_WIREFUTURE);
 1078         vm_map_unlock(map);
 1079 
 1080         /* Forcibly unwire all pages. */
 1081         error = vm_map_unwire(map, vm_map_min(map), vm_map_max(map),
 1082             VM_MAP_WIRE_USER|VM_MAP_WIRE_HOLESOK);
 1083 
 1084         return (error);
 1085 }
 1086 
 1087 #ifndef _SYS_SYSPROTO_H_
 1088 struct munlock_args {
 1089         const void *addr;
 1090         size_t len;
 1091 };
 1092 #endif
 1093 /*
 1094  * MPSAFE
 1095  */
 1096 int
 1097 munlock(td, uap)
 1098         struct thread *td;
 1099         struct munlock_args *uap;
 1100 {
 1101         vm_offset_t addr, end, last, start;
 1102         vm_size_t size;
 1103         int error;
 1104 
 1105         error = priv_check(td, PRIV_VM_MUNLOCK);
 1106         if (error)
 1107                 return (error);
 1108         addr = (vm_offset_t)uap->addr;
 1109         size = uap->len;
 1110         last = addr + size;
 1111         start = trunc_page(addr);
 1112         end = round_page(last);
 1113         if (last < addr || end < addr)
 1114                 return (EINVAL);
 1115         error = vm_map_unwire(&td->td_proc->p_vmspace->vm_map, start, end,
 1116             VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
 1117         return (error == KERN_SUCCESS ? 0 : ENOMEM);
 1118 }
 1119 
 1120 /*
 1121  * vm_mmap_vnode()
 1122  *
 1123  * MPSAFE
 1124  *
 1125  * Helper function for vm_mmap.  Perform sanity check specific for mmap
 1126  * operations on vnodes.
 1127  */
 1128 int
 1129 vm_mmap_vnode(struct thread *td, vm_size_t objsize,
 1130     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
 1131     struct vnode *vp, vm_ooffset_t *foffp, vm_object_t *objp)
 1132 {
 1133         struct vattr va;
 1134         vm_object_t obj;
 1135         vm_offset_t foff;
 1136         struct mount *mp;
 1137         struct ucred *cred;
 1138         int error, flags;
 1139         int vfslocked;
 1140 
 1141         mp = vp->v_mount;
 1142         cred = td->td_ucred;
 1143         vfslocked = VFS_LOCK_GIANT(mp);
 1144         if ((error = vget(vp, LK_SHARED, td)) != 0) {
 1145                 VFS_UNLOCK_GIANT(vfslocked);
 1146                 return (error);
 1147         }
 1148         foff = *foffp;
 1149         flags = *flagsp;
 1150         obj = vp->v_object;
 1151         if (vp->v_type == VREG) {
 1152                 /*
 1153                  * Get the proper underlying object
 1154                  */
 1155                 if (obj == NULL) {
 1156                         error = EINVAL;
 1157                         goto done;
 1158                 }
 1159                 if (obj->handle != vp) {
 1160                         vput(vp);
 1161                         vp = (struct vnode*)obj->handle;
 1162                         vget(vp, LK_SHARED, td);
 1163                 }
 1164         } else if (vp->v_type == VCHR) {
 1165                 error = vm_mmap_cdev(td, objsize, prot, maxprotp, flagsp,
 1166                     vp->v_rdev, foffp, objp);
 1167                 if (error == 0)
 1168                         goto mark_atime;
 1169                 goto done;
 1170         } else {
 1171                 error = EINVAL;
 1172                 goto done;
 1173         }
 1174         if ((error = VOP_GETATTR(vp, &va, cred)))
 1175                 goto done;
 1176 #ifdef MAC
 1177         error = mac_vnode_check_mmap(cred, vp, prot, flags);
 1178         if (error != 0)
 1179                 goto done;
 1180 #endif
 1181         if ((flags & MAP_SHARED) != 0) {
 1182                 if ((va.va_flags & (SF_SNAPSHOT|IMMUTABLE|APPEND)) != 0) {
 1183                         if (prot & PROT_WRITE) {
 1184                                 error = EPERM;
 1185                                 goto done;
 1186                         }
 1187                         *maxprotp &= ~VM_PROT_WRITE;
 1188                 }
 1189         }
 1190         /*
 1191          * If it is a regular file without any references
 1192          * we do not need to sync it.
 1193          * Adjust object size to be the size of actual file.
 1194          */
 1195         objsize = round_page(va.va_size);
 1196         if (va.va_nlink == 0)
 1197                 flags |= MAP_NOSYNC;
 1198         obj = vm_pager_allocate(OBJT_VNODE, vp, objsize, prot, foff, td->td_ucred);
 1199         if (obj == NULL) {
 1200                 error = ENOMEM;
 1201                 goto done;
 1202         }
 1203         *objp = obj;
 1204         *flagsp = flags;
 1205 
 1206 mark_atime:
 1207         vfs_mark_atime(vp, cred);
 1208 
 1209 done:
 1210         vput(vp);
 1211         VFS_UNLOCK_GIANT(vfslocked);
 1212         return (error);
 1213 }
 1214 
 1215 /*
 1216  * vm_mmap_cdev()
 1217  *
 1218  * MPSAFE
 1219  *
 1220  * Helper function for vm_mmap.  Perform sanity check specific for mmap
 1221  * operations on cdevs.
 1222  */
 1223 int
 1224 vm_mmap_cdev(struct thread *td, vm_size_t objsize,
 1225     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
 1226     struct cdev *cdev, vm_ooffset_t *foff, vm_object_t *objp)
 1227 {
 1228         vm_object_t obj;
 1229         struct cdevsw *dsw;
 1230         int error, flags, ref;
 1231 
 1232         flags = *flagsp;
 1233 
 1234         dsw = dev_refthread(cdev, &ref);
 1235         if (dsw == NULL)
 1236                 return (ENXIO);
 1237         if (dsw->d_flags & D_MMAP_ANON) {
 1238                 dev_relthread(cdev, ref);
 1239                 *maxprotp = VM_PROT_ALL;
 1240                 *flagsp |= MAP_ANON;
 1241                 return (0);
 1242         }
 1243         /*
 1244          * cdevs do not provide private mappings of any kind.
 1245          */
 1246         if ((*maxprotp & VM_PROT_WRITE) == 0 &&
 1247             (prot & PROT_WRITE) != 0) {
 1248                 dev_relthread(cdev, ref);
 1249                 return (EACCES);
 1250         }
 1251         if (flags & (MAP_PRIVATE|MAP_COPY)) {
 1252                 dev_relthread(cdev, ref);
 1253                 return (EINVAL);
 1254         }
 1255         /*
 1256          * Force device mappings to be shared.
 1257          */
 1258         flags |= MAP_SHARED;
 1259 #ifdef MAC_XXX
 1260         error = mac_cdev_check_mmap(td->td_ucred, cdev, prot);
 1261         if (error != 0) {
 1262                 dev_relthread(cdev, ref);
 1263                 return (error);
 1264         }
 1265 #endif
 1266         /*
 1267          * First, try d_mmap_single().  If that is not implemented
 1268          * (returns ENODEV), fall back to using the device pager.
 1269          * Note that d_mmap_single() must return a reference to the
 1270          * object (it needs to bump the reference count of the object
 1271          * it returns somehow).
 1272          *
 1273          * XXX assumes VM_PROT_* == PROT_*
 1274          */
 1275         error = dsw->d_mmap_single(cdev, foff, objsize, objp, (int)prot);
 1276         dev_relthread(cdev, ref);
 1277         if (error != ENODEV)
 1278                 return (error);
 1279         obj = vm_pager_allocate(OBJT_DEVICE, cdev, objsize, prot, *foff,
 1280             td->td_ucred);
 1281         if (obj == NULL)
 1282                 return (EINVAL);
 1283         *objp = obj;
 1284         *flagsp = flags;
 1285         return (0);
 1286 }
 1287 
 1288 /*
 1289  * vm_mmap_shm()
 1290  *
 1291  * MPSAFE
 1292  *
 1293  * Helper function for vm_mmap.  Perform sanity check specific for mmap
 1294  * operations on shm file descriptors.
 1295  */
 1296 int
 1297 vm_mmap_shm(struct thread *td, vm_size_t objsize,
 1298     vm_prot_t prot, vm_prot_t *maxprotp, int *flagsp,
 1299     struct shmfd *shmfd, vm_ooffset_t foff, vm_object_t *objp)
 1300 {
 1301         int error;
 1302 
 1303         if ((*flagsp & MAP_SHARED) != 0 &&
 1304             (*maxprotp & VM_PROT_WRITE) == 0 &&
 1305             (prot & PROT_WRITE) != 0)
 1306                 return (EACCES);
 1307 #ifdef MAC
 1308         error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, *flagsp);
 1309         if (error != 0)
 1310                 return (error);
 1311 #endif
 1312         error = shm_mmap(shmfd, objsize, foff, objp);
 1313         if (error)
 1314                 return (error);
 1315         return (0);
 1316 }
 1317 
 1318 /*
 1319  * vm_mmap()
 1320  *
 1321  * MPSAFE
 1322  *
 1323  * Internal version of mmap.  Currently used by mmap, exec, and sys5
 1324  * shared memory.  Handle is either a vnode pointer or NULL for MAP_ANON.
 1325  */
 1326 int
 1327 vm_mmap(vm_map_t map, vm_offset_t *addr, vm_size_t size, vm_prot_t prot,
 1328         vm_prot_t maxprot, int flags,
 1329         objtype_t handle_type, void *handle,
 1330         vm_ooffset_t foff)
 1331 {
 1332         boolean_t fitit;
 1333         vm_object_t object = NULL;
 1334         int rv = KERN_SUCCESS;
 1335         int docow, error;
 1336         struct thread *td = curthread;
 1337 
 1338         if (size == 0)
 1339                 return (0);
 1340 
 1341         size = round_page(size);
 1342 
 1343         PROC_LOCK(td->td_proc);
 1344         if (td->td_proc->p_vmspace->vm_map.size + size >
 1345             lim_cur(td->td_proc, RLIMIT_VMEM)) {
 1346                 PROC_UNLOCK(td->td_proc);
 1347                 return (ENOMEM);
 1348         }
 1349         PROC_UNLOCK(td->td_proc);
 1350 
 1351         /*
 1352          * We currently can only deal with page aligned file offsets.
 1353          * The check is here rather than in the syscall because the
 1354          * kernel calls this function internally for other mmaping
 1355          * operations (such as in exec) and non-aligned offsets will
 1356          * cause pmap inconsistencies...so we want to be sure to
 1357          * disallow this in all cases.
 1358          */
 1359         if (foff & PAGE_MASK)
 1360                 return (EINVAL);
 1361 
 1362         if ((flags & MAP_FIXED) == 0) {
 1363                 fitit = TRUE;
 1364                 *addr = round_page(*addr);
 1365         } else {
 1366                 if (*addr != trunc_page(*addr))
 1367                         return (EINVAL);
 1368                 fitit = FALSE;
 1369         }
 1370         /*
 1371          * Lookup/allocate object.
 1372          */
 1373         switch (handle_type) {
 1374         case OBJT_DEVICE:
 1375                 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags,
 1376                     handle, &foff, &object);
 1377                 break;
 1378         case OBJT_VNODE:
 1379                 error = vm_mmap_vnode(td, size, prot, &maxprot, &flags,
 1380                     handle, &foff, &object);
 1381                 break;
 1382         case OBJT_SWAP:
 1383                 error = vm_mmap_shm(td, size, prot, &maxprot, &flags,
 1384                     handle, foff, &object);
 1385                 break;
 1386         case OBJT_DEFAULT:
 1387                 if (handle == NULL) {
 1388                         error = 0;
 1389                         break;
 1390                 }
 1391                 /* FALLTHROUGH */
 1392         default:
 1393                 error = EINVAL;
 1394                 break;
 1395         }
 1396         if (error)
 1397                 return (error);
 1398         if (flags & MAP_ANON) {
 1399                 object = NULL;
 1400                 docow = 0;
 1401                 /*
 1402                  * Unnamed anonymous regions always start at 0.
 1403                  */
 1404                 if (handle == 0)
 1405                         foff = 0;
 1406         } else if (flags & MAP_PREFAULT_READ)
 1407                 docow = MAP_PREFAULT;
 1408         else
 1409                 docow = MAP_PREFAULT_PARTIAL;
 1410 
 1411         if ((flags & (MAP_ANON|MAP_SHARED)) == 0)
 1412                 docow |= MAP_COPY_ON_WRITE;
 1413         if (flags & MAP_NOSYNC)
 1414                 docow |= MAP_DISABLE_SYNCER;
 1415         if (flags & MAP_NOCORE)
 1416                 docow |= MAP_DISABLE_COREDUMP;
 1417 
 1418         if (flags & MAP_STACK)
 1419                 rv = vm_map_stack(map, *addr, size, prot, maxprot,
 1420                     docow | MAP_STACK_GROWS_DOWN);
 1421         else if (fitit)
 1422                 rv = vm_map_find(map, object, foff, addr, size,
 1423                     object != NULL && object->type == OBJT_DEVICE ?
 1424                     VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, prot, maxprot, docow);
 1425         else
 1426                 rv = vm_map_fixed(map, object, foff, *addr, size,
 1427                                  prot, maxprot, docow);
 1428 
 1429         if (rv != KERN_SUCCESS) {
 1430                 /*
 1431                  * Lose the object reference. Will destroy the
 1432                  * object if it's an unnamed anonymous mapping
 1433                  * or named anonymous without other references.
 1434                  */
 1435                 vm_object_deallocate(object);
 1436         } else if (flags & MAP_SHARED) {
 1437                 /*
 1438                  * Shared memory is also shared with children.
 1439                  */
 1440                 rv = vm_map_inherit(map, *addr, *addr + size, VM_INHERIT_SHARE);
 1441                 if (rv != KERN_SUCCESS)
 1442                         (void) vm_map_remove(map, *addr, *addr + size);
 1443         }
 1444 
 1445         /*
 1446          * If the process has requested that all future mappings
 1447          * be wired, then heed this.
 1448          */
 1449         if ((rv == KERN_SUCCESS) && (map->flags & MAP_WIREFUTURE))
 1450                 vm_map_wire(map, *addr, *addr + size,
 1451                     VM_MAP_WIRE_USER | ((flags & MAP_STACK) ?
 1452                     VM_MAP_WIRE_HOLESOK : VM_MAP_WIRE_NOHOLES));
 1453 
 1454         return (vm_mmap_to_errno(rv));
 1455 }
 1456 
 1457 int
 1458 vm_mmap_to_errno(int rv)
 1459 {
 1460 
 1461         switch (rv) {
 1462         case KERN_SUCCESS:
 1463                 return (0);
 1464         case KERN_INVALID_ADDRESS:
 1465         case KERN_NO_SPACE:
 1466                 return (ENOMEM);
 1467         case KERN_PROTECTION_FAILURE:
 1468                 return (EACCES);
 1469         default:
 1470                 return (EINVAL);
 1471         }
 1472 }

Cache object: 235b0bd648b1804b4509431424ad7393


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