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_map.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) 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * The Mach Operating System project at Carnegie-Mellon University.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      from: @(#)vm_map.c      8.3 (Berkeley) 1/12/94
   33  *
   34  *
   35  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
   36  * All rights reserved.
   37  *
   38  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
   39  *
   40  * Permission to use, copy, modify and distribute this software and
   41  * its documentation is hereby granted, provided that both the copyright
   42  * notice and this permission notice appear in all copies of the
   43  * software, derivative works or modified versions, and any portions
   44  * thereof, and that both notices appear in supporting documentation.
   45  *
   46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
   47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
   48  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   49  *
   50  * Carnegie Mellon requests users of this software to return to
   51  *
   52  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   53  *  School of Computer Science
   54  *  Carnegie Mellon University
   55  *  Pittsburgh PA 15213-3890
   56  *
   57  * any improvements or extensions that they make and grant Carnegie the
   58  * rights to redistribute these changes.
   59  */
   60 
   61 /*
   62  *      Virtual memory mapping module.
   63  */
   64 
   65 #include <sys/cdefs.h>
   66 __FBSDID("$FreeBSD$");
   67 
   68 #include <sys/param.h>
   69 #include <sys/systm.h>
   70 #include <sys/ktr.h>
   71 #include <sys/lock.h>
   72 #include <sys/mutex.h>
   73 #include <sys/proc.h>
   74 #include <sys/vmmeter.h>
   75 #include <sys/mman.h>
   76 #include <sys/vnode.h>
   77 #include <sys/resourcevar.h>
   78 #include <sys/file.h>
   79 #include <sys/sysent.h>
   80 #include <sys/shm.h>
   81 
   82 #include <vm/vm.h>
   83 #include <vm/vm_param.h>
   84 #include <vm/pmap.h>
   85 #include <vm/vm_map.h>
   86 #include <vm/vm_page.h>
   87 #include <vm/vm_object.h>
   88 #include <vm/vm_pager.h>
   89 #include <vm/vm_kern.h>
   90 #include <vm/vm_extern.h>
   91 #include <vm/swap_pager.h>
   92 #include <vm/uma.h>
   93 
   94 /*
   95  *      Virtual memory maps provide for the mapping, protection,
   96  *      and sharing of virtual memory objects.  In addition,
   97  *      this module provides for an efficient virtual copy of
   98  *      memory from one map to another.
   99  *
  100  *      Synchronization is required prior to most operations.
  101  *
  102  *      Maps consist of an ordered doubly-linked list of simple
  103  *      entries; a single hint is used to speed up lookups.
  104  *
  105  *      Since portions of maps are specified by start/end addresses,
  106  *      which may not align with existing map entries, all
  107  *      routines merely "clip" entries to these start/end values.
  108  *      [That is, an entry is split into two, bordering at a
  109  *      start or end value.]  Note that these clippings may not
  110  *      always be necessary (as the two resulting entries are then
  111  *      not changed); however, the clipping is done for convenience.
  112  *
  113  *      As mentioned above, virtual copy operations are performed
  114  *      by copying VM object references from one map to
  115  *      another, and then marking both regions as copy-on-write.
  116  */
  117 
  118 /*
  119  *      vm_map_startup:
  120  *
  121  *      Initialize the vm_map module.  Must be called before
  122  *      any other vm_map routines.
  123  *
  124  *      Map and entry structures are allocated from the general
  125  *      purpose memory pool with some exceptions:
  126  *
  127  *      - The kernel map and kmem submap are allocated statically.
  128  *      - Kernel map entries are allocated out of a static pool.
  129  *
  130  *      These restrictions are necessary since malloc() uses the
  131  *      maps and requires map entries.
  132  */
  133 
  134 static struct mtx map_sleep_mtx;
  135 static uma_zone_t mapentzone;
  136 static uma_zone_t kmapentzone;
  137 static uma_zone_t mapzone;
  138 static uma_zone_t vmspace_zone;
  139 static struct vm_object kmapentobj;
  140 static int vmspace_zinit(void *mem, int size, int flags);
  141 static void vmspace_zfini(void *mem, int size);
  142 static int vm_map_zinit(void *mem, int ize, int flags);
  143 static void vm_map_zfini(void *mem, int size);
  144 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
  145 
  146 #ifdef INVARIANTS
  147 static void vm_map_zdtor(void *mem, int size, void *arg);
  148 static void vmspace_zdtor(void *mem, int size, void *arg);
  149 #endif
  150 
  151 /* 
  152  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
  153  * stable.
  154  */
  155 #define PROC_VMSPACE_LOCK(p) do { } while (0)
  156 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
  157 
  158 /*
  159  *      VM_MAP_RANGE_CHECK:     [ internal use only ]
  160  *
  161  *      Asserts that the starting and ending region
  162  *      addresses fall within the valid range of the map.
  163  */
  164 #define VM_MAP_RANGE_CHECK(map, start, end)             \
  165                 {                                       \
  166                 if (start < vm_map_min(map))            \
  167                         start = vm_map_min(map);        \
  168                 if (end > vm_map_max(map))              \
  169                         end = vm_map_max(map);          \
  170                 if (start > end)                        \
  171                         start = end;                    \
  172                 }
  173 
  174 void
  175 vm_map_startup(void)
  176 {
  177         mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
  178         mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
  179 #ifdef INVARIANTS
  180             vm_map_zdtor,
  181 #else
  182             NULL,
  183 #endif
  184             vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  185         uma_prealloc(mapzone, MAX_KMAP);
  186         kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
  187             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
  188             UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
  189         uma_prealloc(kmapentzone, MAX_KMAPENT);
  190         mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
  191             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
  192 }
  193 
  194 static void
  195 vmspace_zfini(void *mem, int size)
  196 {
  197         struct vmspace *vm;
  198 
  199         vm = (struct vmspace *)mem;
  200         vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
  201 }
  202 
  203 static int
  204 vmspace_zinit(void *mem, int size, int flags)
  205 {
  206         struct vmspace *vm;
  207 
  208         vm = (struct vmspace *)mem;
  209 
  210         vm->vm_map.pmap = NULL;
  211         (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
  212         return (0);
  213 }
  214 
  215 static void
  216 vm_map_zfini(void *mem, int size)
  217 {
  218         vm_map_t map;
  219 
  220         map = (vm_map_t)mem;
  221         mtx_destroy(&map->system_mtx);
  222         sx_destroy(&map->lock);
  223 }
  224 
  225 static int
  226 vm_map_zinit(void *mem, int size, int flags)
  227 {
  228         vm_map_t map;
  229 
  230         map = (vm_map_t)mem;
  231         map->nentries = 0;
  232         map->size = 0;
  233         mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
  234         sx_init(&map->lock, "user map");
  235         return (0);
  236 }
  237 
  238 #ifdef INVARIANTS
  239 static void
  240 vmspace_zdtor(void *mem, int size, void *arg)
  241 {
  242         struct vmspace *vm;
  243 
  244         vm = (struct vmspace *)mem;
  245 
  246         vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
  247 }
  248 static void
  249 vm_map_zdtor(void *mem, int size, void *arg)
  250 {
  251         vm_map_t map;
  252 
  253         map = (vm_map_t)mem;
  254         KASSERT(map->nentries == 0,
  255             ("map %p nentries == %d on free.",
  256             map, map->nentries));
  257         KASSERT(map->size == 0,
  258             ("map %p size == %lu on free.",
  259             map, (unsigned long)map->size));
  260 }
  261 #endif  /* INVARIANTS */
  262 
  263 /*
  264  * Allocate a vmspace structure, including a vm_map and pmap,
  265  * and initialize those structures.  The refcnt is set to 1.
  266  */
  267 struct vmspace *
  268 vmspace_alloc(min, max)
  269         vm_offset_t min, max;
  270 {
  271         struct vmspace *vm;
  272 
  273         vm = uma_zalloc(vmspace_zone, M_WAITOK);
  274         if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
  275                 uma_zfree(vmspace_zone, vm);
  276                 return (NULL);
  277         }
  278         CTR1(KTR_VM, "vmspace_alloc: %p", vm);
  279         _vm_map_init(&vm->vm_map, min, max);
  280         vm->vm_map.pmap = vmspace_pmap(vm);             /* XXX */
  281         vm->vm_refcnt = 1;
  282         vm->vm_shm = NULL;
  283         vm->vm_swrss = 0;
  284         vm->vm_tsize = 0;
  285         vm->vm_dsize = 0;
  286         vm->vm_ssize = 0;
  287         vm->vm_taddr = 0;
  288         vm->vm_daddr = 0;
  289         vm->vm_maxsaddr = 0;
  290         return (vm);
  291 }
  292 
  293 void
  294 vm_init2(void)
  295 {
  296         uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
  297             (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
  298              maxproc * 2 + maxfiles);
  299         vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
  300 #ifdef INVARIANTS
  301             vmspace_zdtor,
  302 #else
  303             NULL,
  304 #endif
  305             vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  306 }
  307 
  308 static inline void
  309 vmspace_dofree(struct vmspace *vm)
  310 {
  311         CTR1(KTR_VM, "vmspace_free: %p", vm);
  312 
  313         /*
  314          * Make sure any SysV shm is freed, it might not have been in
  315          * exit1().
  316          */
  317         shmexit(vm);
  318 
  319         /*
  320          * Lock the map, to wait out all other references to it.
  321          * Delete all of the mappings and pages they hold, then call
  322          * the pmap module to reclaim anything left.
  323          */
  324         (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
  325             vm->vm_map.max_offset);
  326 
  327         /*
  328          * XXX Comment out the pmap_release call for now. The
  329          * vmspace_zone is marked as UMA_ZONE_NOFREE, and bugs cause
  330          * pmap.resident_count to be != 0 on exit sometimes.
  331          */
  332 /*      pmap_release(vmspace_pmap(vm)); */
  333         uma_zfree(vmspace_zone, vm);
  334 }
  335 
  336 void
  337 vmspace_free(struct vmspace *vm)
  338 {
  339         int refcnt;
  340 
  341         if (vm->vm_refcnt == 0)
  342                 panic("vmspace_free: attempt to free already freed vmspace");
  343 
  344         do
  345                 refcnt = vm->vm_refcnt;
  346         while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
  347         if (refcnt == 1)
  348                 vmspace_dofree(vm);
  349 }
  350 
  351 void
  352 vmspace_exitfree(struct proc *p)
  353 {
  354         struct vmspace *vm;
  355 
  356         PROC_VMSPACE_LOCK(p);
  357         vm = p->p_vmspace;
  358         p->p_vmspace = NULL;
  359         PROC_VMSPACE_UNLOCK(p);
  360         KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
  361         vmspace_free(vm);
  362 }
  363 
  364 void
  365 vmspace_exit(struct thread *td)
  366 {
  367         int refcnt;
  368         struct vmspace *vm;
  369         struct proc *p;
  370 
  371         /*
  372          * Release user portion of address space.
  373          * This releases references to vnodes,
  374          * which could cause I/O if the file has been unlinked.
  375          * Need to do this early enough that we can still sleep.
  376          *
  377          * The last exiting process to reach this point releases as
  378          * much of the environment as it can. vmspace_dofree() is the
  379          * slower fallback in case another process had a temporary
  380          * reference to the vmspace.
  381          */
  382 
  383         p = td->td_proc;
  384         vm = p->p_vmspace;
  385         atomic_add_int(&vmspace0.vm_refcnt, 1);
  386         do {
  387                 refcnt = vm->vm_refcnt;
  388                 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
  389                         /* Switch now since other proc might free vmspace */
  390                         PROC_VMSPACE_LOCK(p);
  391                         p->p_vmspace = &vmspace0;
  392                         PROC_VMSPACE_UNLOCK(p);
  393                         pmap_activate(td);
  394                 }
  395         } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
  396         if (refcnt == 1) {
  397                 if (p->p_vmspace != vm) {
  398                         /* vmspace not yet freed, switch back */
  399                         PROC_VMSPACE_LOCK(p);
  400                         p->p_vmspace = vm;
  401                         PROC_VMSPACE_UNLOCK(p);
  402                         pmap_activate(td);
  403                 }
  404                 pmap_remove_pages(vmspace_pmap(vm));
  405                 /* Switch now since this proc will free vmspace */
  406                 PROC_VMSPACE_LOCK(p);
  407                 p->p_vmspace = &vmspace0;
  408                 PROC_VMSPACE_UNLOCK(p);
  409                 pmap_activate(td);
  410                 vmspace_dofree(vm);
  411         }
  412 }
  413 
  414 /* Acquire reference to vmspace owned by another process. */
  415 
  416 struct vmspace *
  417 vmspace_acquire_ref(struct proc *p)
  418 {
  419         struct vmspace *vm;
  420         int refcnt;
  421 
  422         PROC_VMSPACE_LOCK(p);
  423         vm = p->p_vmspace;
  424         if (vm == NULL) {
  425                 PROC_VMSPACE_UNLOCK(p);
  426                 return (NULL);
  427         }
  428         do {
  429                 refcnt = vm->vm_refcnt;
  430                 if (refcnt <= 0) {      /* Avoid 0->1 transition */
  431                         PROC_VMSPACE_UNLOCK(p);
  432                         return (NULL);
  433                 }
  434         } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
  435         if (vm != p->p_vmspace) {
  436                 PROC_VMSPACE_UNLOCK(p);
  437                 vmspace_free(vm);
  438                 return (NULL);
  439         }
  440         PROC_VMSPACE_UNLOCK(p);
  441         return (vm);
  442 }
  443 
  444 void
  445 _vm_map_lock(vm_map_t map, const char *file, int line)
  446 {
  447 
  448         if (map->system_map)
  449                 _mtx_lock_flags(&map->system_mtx, 0, file, line);
  450         else
  451                 (void)_sx_xlock(&map->lock, 0, file, line);
  452         map->timestamp++;
  453 }
  454 
  455 void
  456 _vm_map_unlock(vm_map_t map, const char *file, int line)
  457 {
  458 
  459         if (map->system_map)
  460                 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
  461         else
  462                 _sx_xunlock(&map->lock, file, line);
  463 }
  464 
  465 void
  466 _vm_map_lock_read(vm_map_t map, const char *file, int line)
  467 {
  468 
  469         if (map->system_map)
  470                 _mtx_lock_flags(&map->system_mtx, 0, file, line);
  471         else
  472                 (void)_sx_xlock(&map->lock, 0, file, line);
  473 }
  474 
  475 void
  476 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
  477 {
  478 
  479         if (map->system_map)
  480                 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
  481         else
  482                 _sx_xunlock(&map->lock, file, line);
  483 }
  484 
  485 int
  486 _vm_map_trylock(vm_map_t map, const char *file, int line)
  487 {
  488         int error;
  489 
  490         error = map->system_map ?
  491             !_mtx_trylock(&map->system_mtx, 0, file, line) :
  492             !_sx_try_xlock(&map->lock, file, line);
  493         if (error == 0)
  494                 map->timestamp++;
  495         return (error == 0);
  496 }
  497 
  498 int
  499 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
  500 {
  501         int error;
  502 
  503         error = map->system_map ?
  504             !_mtx_trylock(&map->system_mtx, 0, file, line) :
  505             !_sx_try_xlock(&map->lock, file, line);
  506         return (error == 0);
  507 }
  508 
  509 int
  510 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
  511 {
  512 
  513 #ifdef INVARIANTS
  514         if (map->system_map) {
  515                 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
  516         } else
  517                 _sx_assert(&map->lock, SX_XLOCKED, file, line);
  518 #endif
  519         map->timestamp++;
  520         return (0);
  521 }
  522 
  523 void
  524 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
  525 {
  526 
  527 #ifdef INVARIANTS
  528         if (map->system_map) {
  529                 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
  530         } else
  531                 _sx_assert(&map->lock, SX_XLOCKED, file, line);
  532 #endif
  533 }
  534 
  535 /*
  536  *      vm_map_unlock_and_wait:
  537  */
  538 int
  539 vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
  540 {
  541 
  542         mtx_lock(&map_sleep_mtx);
  543         vm_map_unlock(map);
  544         return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
  545 }
  546 
  547 /*
  548  *      vm_map_wakeup:
  549  */
  550 void
  551 vm_map_wakeup(vm_map_t map)
  552 {
  553 
  554         /*
  555          * Acquire and release map_sleep_mtx to prevent a wakeup()
  556          * from being performed (and lost) between the vm_map_unlock()
  557          * and the msleep() in vm_map_unlock_and_wait().
  558          */
  559         mtx_lock(&map_sleep_mtx);
  560         mtx_unlock(&map_sleep_mtx);
  561         wakeup(&map->root);
  562 }
  563 
  564 long
  565 vmspace_resident_count(struct vmspace *vmspace)
  566 {
  567         return pmap_resident_count(vmspace_pmap(vmspace));
  568 }
  569 
  570 long
  571 vmspace_wired_count(struct vmspace *vmspace)
  572 {
  573         return pmap_wired_count(vmspace_pmap(vmspace));
  574 }
  575 
  576 /*
  577  *      vm_map_create:
  578  *
  579  *      Creates and returns a new empty VM map with
  580  *      the given physical map structure, and having
  581  *      the given lower and upper address bounds.
  582  */
  583 vm_map_t
  584 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
  585 {
  586         vm_map_t result;
  587 
  588         result = uma_zalloc(mapzone, M_WAITOK);
  589         CTR1(KTR_VM, "vm_map_create: %p", result);
  590         _vm_map_init(result, min, max);
  591         result->pmap = pmap;
  592         return (result);
  593 }
  594 
  595 /*
  596  * Initialize an existing vm_map structure
  597  * such as that in the vmspace structure.
  598  * The pmap is set elsewhere.
  599  */
  600 static void
  601 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
  602 {
  603 
  604         map->header.next = map->header.prev = &map->header;
  605         map->needs_wakeup = FALSE;
  606         map->system_map = 0;
  607         map->min_offset = min;
  608         map->max_offset = max;
  609         map->flags = 0;
  610         map->root = NULL;
  611         map->timestamp = 0;
  612 }
  613 
  614 void
  615 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
  616 {
  617         _vm_map_init(map, min, max);
  618         mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
  619         sx_init(&map->lock, "user map");
  620 }
  621 
  622 /*
  623  *      vm_map_entry_dispose:   [ internal use only ]
  624  *
  625  *      Inverse of vm_map_entry_create.
  626  */
  627 static void
  628 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
  629 {
  630         uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
  631 }
  632 
  633 /*
  634  *      vm_map_entry_create:    [ internal use only ]
  635  *
  636  *      Allocates a VM map entry for insertion.
  637  *      No entry fields are filled in.
  638  */
  639 static vm_map_entry_t
  640 vm_map_entry_create(vm_map_t map)
  641 {
  642         vm_map_entry_t new_entry;
  643 
  644         if (map->system_map)
  645                 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
  646         else
  647                 new_entry = uma_zalloc(mapentzone, M_WAITOK);
  648         if (new_entry == NULL)
  649                 panic("vm_map_entry_create: kernel resources exhausted");
  650         return (new_entry);
  651 }
  652 
  653 /*
  654  *      vm_map_entry_set_behavior:
  655  *
  656  *      Set the expected access behavior, either normal, random, or
  657  *      sequential.
  658  */
  659 static inline void
  660 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
  661 {
  662         entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
  663             (behavior & MAP_ENTRY_BEHAV_MASK);
  664 }
  665 
  666 /*
  667  *      vm_map_entry_set_max_free:
  668  *
  669  *      Set the max_free field in a vm_map_entry.
  670  */
  671 static inline void
  672 vm_map_entry_set_max_free(vm_map_entry_t entry)
  673 {
  674 
  675         entry->max_free = entry->adj_free;
  676         if (entry->left != NULL && entry->left->max_free > entry->max_free)
  677                 entry->max_free = entry->left->max_free;
  678         if (entry->right != NULL && entry->right->max_free > entry->max_free)
  679                 entry->max_free = entry->right->max_free;
  680 }
  681 
  682 /*
  683  *      vm_map_entry_splay:
  684  *
  685  *      The Sleator and Tarjan top-down splay algorithm with the
  686  *      following variation.  Max_free must be computed bottom-up, so
  687  *      on the downward pass, maintain the left and right spines in
  688  *      reverse order.  Then, make a second pass up each side to fix
  689  *      the pointers and compute max_free.  The time bound is O(log n)
  690  *      amortized.
  691  *
  692  *      The new root is the vm_map_entry containing "addr", or else an
  693  *      adjacent entry (lower or higher) if addr is not in the tree.
  694  *
  695  *      The map must be locked, and leaves it so.
  696  *
  697  *      Returns: the new root.
  698  */
  699 static vm_map_entry_t
  700 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
  701 {
  702         vm_map_entry_t llist, rlist;
  703         vm_map_entry_t ltree, rtree;
  704         vm_map_entry_t y;
  705 
  706         /* Special case of empty tree. */
  707         if (root == NULL)
  708                 return (root);
  709 
  710         /*
  711          * Pass One: Splay down the tree until we find addr or a NULL
  712          * pointer where addr would go.  llist and rlist are the two
  713          * sides in reverse order (bottom-up), with llist linked by
  714          * the right pointer and rlist linked by the left pointer in
  715          * the vm_map_entry.  Wait until Pass Two to set max_free on
  716          * the two spines.
  717          */
  718         llist = NULL;
  719         rlist = NULL;
  720         for (;;) {
  721                 /* root is never NULL in here. */
  722                 if (addr < root->start) {
  723                         y = root->left;
  724                         if (y == NULL)
  725                                 break;
  726                         if (addr < y->start && y->left != NULL) {
  727                                 /* Rotate right and put y on rlist. */
  728                                 root->left = y->right;
  729                                 y->right = root;
  730                                 vm_map_entry_set_max_free(root);
  731                                 root = y->left;
  732                                 y->left = rlist;
  733                                 rlist = y;
  734                         } else {
  735                                 /* Put root on rlist. */
  736                                 root->left = rlist;
  737                                 rlist = root;
  738                                 root = y;
  739                         }
  740                 } else {
  741                         y = root->right;
  742                         if (addr < root->end || y == NULL)
  743                                 break;
  744                         if (addr >= y->end && y->right != NULL) {
  745                                 /* Rotate left and put y on llist. */
  746                                 root->right = y->left;
  747                                 y->left = root;
  748                                 vm_map_entry_set_max_free(root);
  749                                 root = y->right;
  750                                 y->right = llist;
  751                                 llist = y;
  752                         } else {
  753                                 /* Put root on llist. */
  754                                 root->right = llist;
  755                                 llist = root;
  756                                 root = y;
  757                         }
  758                 }
  759         }
  760 
  761         /*
  762          * Pass Two: Walk back up the two spines, flip the pointers
  763          * and set max_free.  The subtrees of the root go at the
  764          * bottom of llist and rlist.
  765          */
  766         ltree = root->left;
  767         while (llist != NULL) {
  768                 y = llist->right;
  769                 llist->right = ltree;
  770                 vm_map_entry_set_max_free(llist);
  771                 ltree = llist;
  772                 llist = y;
  773         }
  774         rtree = root->right;
  775         while (rlist != NULL) {
  776                 y = rlist->left;
  777                 rlist->left = rtree;
  778                 vm_map_entry_set_max_free(rlist);
  779                 rtree = rlist;
  780                 rlist = y;
  781         }
  782 
  783         /*
  784          * Final assembly: add ltree and rtree as subtrees of root.
  785          */
  786         root->left = ltree;
  787         root->right = rtree;
  788         vm_map_entry_set_max_free(root);
  789 
  790         return (root);
  791 }
  792 
  793 /*
  794  *      vm_map_entry_{un,}link:
  795  *
  796  *      Insert/remove entries from maps.
  797  */
  798 static void
  799 vm_map_entry_link(vm_map_t map,
  800                   vm_map_entry_t after_where,
  801                   vm_map_entry_t entry)
  802 {
  803 
  804         CTR4(KTR_VM,
  805             "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
  806             map->nentries, entry, after_where);
  807         map->nentries++;
  808         entry->prev = after_where;
  809         entry->next = after_where->next;
  810         entry->next->prev = entry;
  811         after_where->next = entry;
  812 
  813         if (after_where != &map->header) {
  814                 if (after_where != map->root)
  815                         vm_map_entry_splay(after_where->start, map->root);
  816                 entry->right = after_where->right;
  817                 entry->left = after_where;
  818                 after_where->right = NULL;
  819                 after_where->adj_free = entry->start - after_where->end;
  820                 vm_map_entry_set_max_free(after_where);
  821         } else {
  822                 entry->right = map->root;
  823                 entry->left = NULL;
  824         }
  825         entry->adj_free = (entry->next == &map->header ? map->max_offset :
  826             entry->next->start) - entry->end;
  827         vm_map_entry_set_max_free(entry);
  828         map->root = entry;
  829 }
  830 
  831 static void
  832 vm_map_entry_unlink(vm_map_t map,
  833                     vm_map_entry_t entry)
  834 {
  835         vm_map_entry_t next, prev, root;
  836 
  837         if (entry != map->root)
  838                 vm_map_entry_splay(entry->start, map->root);
  839         if (entry->left == NULL)
  840                 root = entry->right;
  841         else {
  842                 root = vm_map_entry_splay(entry->start, entry->left);
  843                 root->right = entry->right;
  844                 root->adj_free = (entry->next == &map->header ? map->max_offset :
  845                     entry->next->start) - root->end;
  846                 vm_map_entry_set_max_free(root);
  847         }
  848         map->root = root;
  849 
  850         prev = entry->prev;
  851         next = entry->next;
  852         next->prev = prev;
  853         prev->next = next;
  854         map->nentries--;
  855         CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
  856             map->nentries, entry);
  857 }
  858 
  859 /*
  860  *      vm_map_entry_resize_free:
  861  *
  862  *      Recompute the amount of free space following a vm_map_entry
  863  *      and propagate that value up the tree.  Call this function after
  864  *      resizing a map entry in-place, that is, without a call to
  865  *      vm_map_entry_link() or _unlink().
  866  *
  867  *      The map must be locked, and leaves it so.
  868  */
  869 static void
  870 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
  871 {
  872 
  873         /*
  874          * Using splay trees without parent pointers, propagating
  875          * max_free up the tree is done by moving the entry to the
  876          * root and making the change there.
  877          */
  878         if (entry != map->root)
  879                 map->root = vm_map_entry_splay(entry->start, map->root);
  880 
  881         entry->adj_free = (entry->next == &map->header ? map->max_offset :
  882             entry->next->start) - entry->end;
  883         vm_map_entry_set_max_free(entry);
  884 }
  885 
  886 /*
  887  *      vm_map_lookup_entry:    [ internal use only ]
  888  *
  889  *      Finds the map entry containing (or
  890  *      immediately preceding) the specified address
  891  *      in the given map; the entry is returned
  892  *      in the "entry" parameter.  The boolean
  893  *      result indicates whether the address is
  894  *      actually contained in the map.
  895  */
  896 boolean_t
  897 vm_map_lookup_entry(
  898         vm_map_t map,
  899         vm_offset_t address,
  900         vm_map_entry_t *entry)  /* OUT */
  901 {
  902         vm_map_entry_t cur;
  903 
  904         cur = vm_map_entry_splay(address, map->root);
  905         if (cur == NULL)
  906                 *entry = &map->header;
  907         else {
  908                 map->root = cur;
  909 
  910                 if (address >= cur->start) {
  911                         *entry = cur;
  912                         if (cur->end > address)
  913                                 return (TRUE);
  914                 } else
  915                         *entry = cur->prev;
  916         }
  917         return (FALSE);
  918 }
  919 
  920 /*
  921  *      vm_map_insert:
  922  *
  923  *      Inserts the given whole VM object into the target
  924  *      map at the specified address range.  The object's
  925  *      size should match that of the address range.
  926  *
  927  *      Requires that the map be locked, and leaves it so.
  928  *
  929  *      If object is non-NULL, ref count must be bumped by caller
  930  *      prior to making call to account for the new entry.
  931  */
  932 int
  933 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
  934               vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
  935               int cow)
  936 {
  937         vm_map_entry_t new_entry;
  938         vm_map_entry_t prev_entry;
  939         vm_map_entry_t temp_entry;
  940         vm_eflags_t protoeflags;
  941 
  942         /*
  943          * Check that the start and end points are not bogus.
  944          */
  945         if ((start < map->min_offset) || (end > map->max_offset) ||
  946             (start >= end))
  947                 return (KERN_INVALID_ADDRESS);
  948 
  949         /*
  950          * Find the entry prior to the proposed starting address; if it's part
  951          * of an existing entry, this range is bogus.
  952          */
  953         if (vm_map_lookup_entry(map, start, &temp_entry))
  954                 return (KERN_NO_SPACE);
  955 
  956         prev_entry = temp_entry;
  957 
  958         /*
  959          * Assert that the next entry doesn't overlap the end point.
  960          */
  961         if ((prev_entry->next != &map->header) &&
  962             (prev_entry->next->start < end))
  963                 return (KERN_NO_SPACE);
  964 
  965         protoeflags = 0;
  966 
  967         if (cow & MAP_COPY_ON_WRITE)
  968                 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
  969 
  970         if (cow & MAP_NOFAULT) {
  971                 protoeflags |= MAP_ENTRY_NOFAULT;
  972 
  973                 KASSERT(object == NULL,
  974                         ("vm_map_insert: paradoxical MAP_NOFAULT request"));
  975         }
  976         if (cow & MAP_DISABLE_SYNCER)
  977                 protoeflags |= MAP_ENTRY_NOSYNC;
  978         if (cow & MAP_DISABLE_COREDUMP)
  979                 protoeflags |= MAP_ENTRY_NOCOREDUMP;
  980 
  981         if (object != NULL) {
  982                 /*
  983                  * OBJ_ONEMAPPING must be cleared unless this mapping
  984                  * is trivially proven to be the only mapping for any
  985                  * of the object's pages.  (Object granularity
  986                  * reference counting is insufficient to recognize
  987                  * aliases with precision.)
  988                  */
  989                 VM_OBJECT_LOCK(object);
  990                 if (object->ref_count > 1 || object->shadow_count != 0)
  991                         vm_object_clear_flag(object, OBJ_ONEMAPPING);
  992                 VM_OBJECT_UNLOCK(object);
  993         }
  994         else if ((prev_entry != &map->header) &&
  995                  (prev_entry->eflags == protoeflags) &&
  996                  (prev_entry->end == start) &&
  997                  (prev_entry->wired_count == 0) &&
  998                  ((prev_entry->object.vm_object == NULL) ||
  999                   vm_object_coalesce(prev_entry->object.vm_object,
 1000                                      prev_entry->offset,
 1001                                      (vm_size_t)(prev_entry->end - prev_entry->start),
 1002                                      (vm_size_t)(end - prev_entry->end)))) {
 1003                 /*
 1004                  * We were able to extend the object.  Determine if we
 1005                  * can extend the previous map entry to include the
 1006                  * new range as well.
 1007                  */
 1008                 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
 1009                     (prev_entry->protection == prot) &&
 1010                     (prev_entry->max_protection == max)) {
 1011                         map->size += (end - prev_entry->end);
 1012                         prev_entry->end = end;
 1013                         vm_map_entry_resize_free(map, prev_entry);
 1014                         vm_map_simplify_entry(map, prev_entry);
 1015                         return (KERN_SUCCESS);
 1016                 }
 1017 
 1018                 /*
 1019                  * If we can extend the object but cannot extend the
 1020                  * map entry, we have to create a new map entry.  We
 1021                  * must bump the ref count on the extended object to
 1022                  * account for it.  object may be NULL.
 1023                  */
 1024                 object = prev_entry->object.vm_object;
 1025                 offset = prev_entry->offset +
 1026                         (prev_entry->end - prev_entry->start);
 1027                 vm_object_reference(object);
 1028         }
 1029 
 1030         /*
 1031          * NOTE: if conditionals fail, object can be NULL here.  This occurs
 1032          * in things like the buffer map where we manage kva but do not manage
 1033          * backing objects.
 1034          */
 1035 
 1036         /*
 1037          * Create a new entry
 1038          */
 1039         new_entry = vm_map_entry_create(map);
 1040         new_entry->start = start;
 1041         new_entry->end = end;
 1042 
 1043         new_entry->eflags = protoeflags;
 1044         new_entry->object.vm_object = object;
 1045         new_entry->offset = offset;
 1046         new_entry->avail_ssize = 0;
 1047 
 1048         new_entry->inheritance = VM_INHERIT_DEFAULT;
 1049         new_entry->protection = prot;
 1050         new_entry->max_protection = max;
 1051         new_entry->wired_count = 0;
 1052 
 1053         /*
 1054          * Insert the new entry into the list
 1055          */
 1056         vm_map_entry_link(map, prev_entry, new_entry);
 1057         map->size += new_entry->end - new_entry->start;
 1058 
 1059 #if 0
 1060         /*
 1061          * Temporarily removed to avoid MAP_STACK panic, due to
 1062          * MAP_STACK being a huge hack.  Will be added back in
 1063          * when MAP_STACK (and the user stack mapping) is fixed.
 1064          */
 1065         /*
 1066          * It may be possible to simplify the entry
 1067          */
 1068         vm_map_simplify_entry(map, new_entry);
 1069 #endif
 1070 
 1071         if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
 1072                 vm_map_pmap_enter(map, start, prot,
 1073                                     object, OFF_TO_IDX(offset), end - start,
 1074                                     cow & MAP_PREFAULT_PARTIAL);
 1075         }
 1076 
 1077         return (KERN_SUCCESS);
 1078 }
 1079 
 1080 /*
 1081  *      vm_map_findspace:
 1082  *
 1083  *      Find the first fit (lowest VM address) for "length" free bytes
 1084  *      beginning at address >= start in the given map.
 1085  *
 1086  *      In a vm_map_entry, "adj_free" is the amount of free space
 1087  *      adjacent (higher address) to this entry, and "max_free" is the
 1088  *      maximum amount of contiguous free space in its subtree.  This
 1089  *      allows finding a free region in one path down the tree, so
 1090  *      O(log n) amortized with splay trees.
 1091  *
 1092  *      The map must be locked, and leaves it so.
 1093  *
 1094  *      Returns: 0 on success, and starting address in *addr,
 1095  *               1 if insufficient space.
 1096  */
 1097 int
 1098 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
 1099     vm_offset_t *addr)  /* OUT */
 1100 {
 1101         vm_map_entry_t entry;
 1102         vm_offset_t end, st;
 1103 
 1104         /*
 1105          * Request must fit within min/max VM address and must avoid
 1106          * address wrap.
 1107          */
 1108         if (start < map->min_offset)
 1109                 start = map->min_offset;
 1110         if (start + length > map->max_offset || start + length < start)
 1111                 return (1);
 1112 
 1113         /* Empty tree means wide open address space. */
 1114         if (map->root == NULL) {
 1115                 *addr = start;
 1116                 goto found;
 1117         }
 1118 
 1119         /*
 1120          * After splay, if start comes before root node, then there
 1121          * must be a gap from start to the root.
 1122          */
 1123         map->root = vm_map_entry_splay(start, map->root);
 1124         if (start + length <= map->root->start) {
 1125                 *addr = start;
 1126                 goto found;
 1127         }
 1128 
 1129         /*
 1130          * Root is the last node that might begin its gap before
 1131          * start, and this is the last comparison where address
 1132          * wrap might be a problem.
 1133          */
 1134         st = (start > map->root->end) ? start : map->root->end;
 1135         if (length <= map->root->end + map->root->adj_free - st) {
 1136                 *addr = st;
 1137                 goto found;
 1138         }
 1139 
 1140         /* With max_free, can immediately tell if no solution. */
 1141         entry = map->root->right;
 1142         if (entry == NULL || length > entry->max_free)
 1143                 return (1);
 1144 
 1145         /*
 1146          * Search the right subtree in the order: left subtree, root,
 1147          * right subtree (first fit).  The previous splay implies that
 1148          * all regions in the right subtree have addresses > start.
 1149          */
 1150         while (entry != NULL) {
 1151                 if (entry->left != NULL && entry->left->max_free >= length)
 1152                         entry = entry->left;
 1153                 else if (entry->adj_free >= length) {
 1154                         *addr = entry->end;
 1155                         goto found;
 1156                 } else
 1157                         entry = entry->right;
 1158         }
 1159 
 1160         /* Can't get here, so panic if we do. */
 1161         panic("vm_map_findspace: max_free corrupt");
 1162 
 1163 found:
 1164         /* Expand the kernel pmap, if necessary. */
 1165         if (map == kernel_map) {
 1166                 end = round_page(*addr + length);
 1167                 if (end > kernel_vm_end)
 1168                         pmap_growkernel(end);
 1169         }
 1170         return (0);
 1171 }
 1172 
 1173 int
 1174 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
 1175     vm_offset_t *addr /* IN/OUT */, vm_size_t length, vm_prot_t prot,
 1176     vm_prot_t max, int cow)
 1177 {
 1178         vm_offset_t start, end;
 1179         int result;
 1180 
 1181         start = *addr;
 1182         vm_map_lock(map);
 1183         end = start + length;
 1184         VM_MAP_RANGE_CHECK(map, start, end);
 1185         (void) vm_map_delete(map, start, end);
 1186         result = vm_map_insert(map, object, offset, start, end, prot,
 1187             max, cow);
 1188         vm_map_unlock(map);
 1189         return (result);
 1190 }
 1191 
 1192 /*
 1193  *      vm_map_find finds an unallocated region in the target address
 1194  *      map with the given length.  The search is defined to be
 1195  *      first-fit from the specified address; the region found is
 1196  *      returned in the same parameter.
 1197  *
 1198  *      If object is non-NULL, ref count must be bumped by caller
 1199  *      prior to making call to account for the new entry.
 1200  */
 1201 int
 1202 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
 1203             vm_offset_t *addr,  /* IN/OUT */
 1204             vm_size_t length, boolean_t find_space, vm_prot_t prot,
 1205             vm_prot_t max, int cow)
 1206 {
 1207         vm_offset_t start;
 1208         int result;
 1209 
 1210         start = *addr;
 1211         vm_map_lock(map);
 1212         if (find_space) {
 1213                 if (vm_map_findspace(map, start, length, addr)) {
 1214                         vm_map_unlock(map);
 1215                         return (KERN_NO_SPACE);
 1216                 }
 1217                 start = *addr;
 1218         }
 1219         result = vm_map_insert(map, object, offset,
 1220                 start, start + length, prot, max, cow);
 1221         vm_map_unlock(map);
 1222         return (result);
 1223 }
 1224 
 1225 /*
 1226  *      vm_map_simplify_entry:
 1227  *
 1228  *      Simplify the given map entry by merging with either neighbor.  This
 1229  *      routine also has the ability to merge with both neighbors.
 1230  *
 1231  *      The map must be locked.
 1232  *
 1233  *      This routine guarentees that the passed entry remains valid (though
 1234  *      possibly extended).  When merging, this routine may delete one or
 1235  *      both neighbors.
 1236  */
 1237 void
 1238 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
 1239 {
 1240         vm_map_entry_t next, prev;
 1241         vm_size_t prevsize, esize;
 1242 
 1243         if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
 1244                 return;
 1245 
 1246         prev = entry->prev;
 1247         if (prev != &map->header) {
 1248                 prevsize = prev->end - prev->start;
 1249                 if ( (prev->end == entry->start) &&
 1250                      (prev->object.vm_object == entry->object.vm_object) &&
 1251                      (!prev->object.vm_object ||
 1252                         (prev->offset + prevsize == entry->offset)) &&
 1253                      (prev->eflags == entry->eflags) &&
 1254                      (prev->protection == entry->protection) &&
 1255                      (prev->max_protection == entry->max_protection) &&
 1256                      (prev->inheritance == entry->inheritance) &&
 1257                      (prev->wired_count == entry->wired_count)) {
 1258                         vm_map_entry_unlink(map, prev);
 1259                         entry->start = prev->start;
 1260                         entry->offset = prev->offset;
 1261                         if (entry->prev != &map->header)
 1262                                 vm_map_entry_resize_free(map, entry->prev);
 1263                         if (prev->object.vm_object)
 1264                                 vm_object_deallocate(prev->object.vm_object);
 1265                         vm_map_entry_dispose(map, prev);
 1266                 }
 1267         }
 1268 
 1269         next = entry->next;
 1270         if (next != &map->header) {
 1271                 esize = entry->end - entry->start;
 1272                 if ((entry->end == next->start) &&
 1273                     (next->object.vm_object == entry->object.vm_object) &&
 1274                      (!entry->object.vm_object ||
 1275                         (entry->offset + esize == next->offset)) &&
 1276                     (next->eflags == entry->eflags) &&
 1277                     (next->protection == entry->protection) &&
 1278                     (next->max_protection == entry->max_protection) &&
 1279                     (next->inheritance == entry->inheritance) &&
 1280                     (next->wired_count == entry->wired_count)) {
 1281                         vm_map_entry_unlink(map, next);
 1282                         entry->end = next->end;
 1283                         vm_map_entry_resize_free(map, entry);
 1284                         if (next->object.vm_object)
 1285                                 vm_object_deallocate(next->object.vm_object);
 1286                         vm_map_entry_dispose(map, next);
 1287                 }
 1288         }
 1289 }
 1290 /*
 1291  *      vm_map_clip_start:      [ internal use only ]
 1292  *
 1293  *      Asserts that the given entry begins at or after
 1294  *      the specified address; if necessary,
 1295  *      it splits the entry into two.
 1296  */
 1297 #define vm_map_clip_start(map, entry, startaddr) \
 1298 { \
 1299         if (startaddr > entry->start) \
 1300                 _vm_map_clip_start(map, entry, startaddr); \
 1301 }
 1302 
 1303 /*
 1304  *      This routine is called only when it is known that
 1305  *      the entry must be split.
 1306  */
 1307 static void
 1308 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
 1309 {
 1310         vm_map_entry_t new_entry;
 1311 
 1312         /*
 1313          * Split off the front portion -- note that we must insert the new
 1314          * entry BEFORE this one, so that this entry has the specified
 1315          * starting address.
 1316          */
 1317         vm_map_simplify_entry(map, entry);
 1318 
 1319         /*
 1320          * If there is no object backing this entry, we might as well create
 1321          * one now.  If we defer it, an object can get created after the map
 1322          * is clipped, and individual objects will be created for the split-up
 1323          * map.  This is a bit of a hack, but is also about the best place to
 1324          * put this improvement.
 1325          */
 1326         if (entry->object.vm_object == NULL && !map->system_map) {
 1327                 vm_object_t object;
 1328                 object = vm_object_allocate(OBJT_DEFAULT,
 1329                                 atop(entry->end - entry->start));
 1330                 entry->object.vm_object = object;
 1331                 entry->offset = 0;
 1332         }
 1333 
 1334         new_entry = vm_map_entry_create(map);
 1335         *new_entry = *entry;
 1336 
 1337         new_entry->end = start;
 1338         entry->offset += (start - entry->start);
 1339         entry->start = start;
 1340 
 1341         vm_map_entry_link(map, entry->prev, new_entry);
 1342 
 1343         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
 1344                 vm_object_reference(new_entry->object.vm_object);
 1345         }
 1346 }
 1347 
 1348 /*
 1349  *      vm_map_clip_end:        [ internal use only ]
 1350  *
 1351  *      Asserts that the given entry ends at or before
 1352  *      the specified address; if necessary,
 1353  *      it splits the entry into two.
 1354  */
 1355 #define vm_map_clip_end(map, entry, endaddr) \
 1356 { \
 1357         if ((endaddr) < (entry->end)) \
 1358                 _vm_map_clip_end((map), (entry), (endaddr)); \
 1359 }
 1360 
 1361 /*
 1362  *      This routine is called only when it is known that
 1363  *      the entry must be split.
 1364  */
 1365 static void
 1366 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
 1367 {
 1368         vm_map_entry_t new_entry;
 1369 
 1370         /*
 1371          * If there is no object backing this entry, we might as well create
 1372          * one now.  If we defer it, an object can get created after the map
 1373          * is clipped, and individual objects will be created for the split-up
 1374          * map.  This is a bit of a hack, but is also about the best place to
 1375          * put this improvement.
 1376          */
 1377         if (entry->object.vm_object == NULL && !map->system_map) {
 1378                 vm_object_t object;
 1379                 object = vm_object_allocate(OBJT_DEFAULT,
 1380                                 atop(entry->end - entry->start));
 1381                 entry->object.vm_object = object;
 1382                 entry->offset = 0;
 1383         }
 1384 
 1385         /*
 1386          * Create a new entry and insert it AFTER the specified entry
 1387          */
 1388         new_entry = vm_map_entry_create(map);
 1389         *new_entry = *entry;
 1390 
 1391         new_entry->start = entry->end = end;
 1392         new_entry->offset += (end - entry->start);
 1393 
 1394         vm_map_entry_link(map, entry, new_entry);
 1395 
 1396         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
 1397                 vm_object_reference(new_entry->object.vm_object);
 1398         }
 1399 }
 1400 
 1401 /*
 1402  *      vm_map_submap:          [ kernel use only ]
 1403  *
 1404  *      Mark the given range as handled by a subordinate map.
 1405  *
 1406  *      This range must have been created with vm_map_find,
 1407  *      and no other operations may have been performed on this
 1408  *      range prior to calling vm_map_submap.
 1409  *
 1410  *      Only a limited number of operations can be performed
 1411  *      within this rage after calling vm_map_submap:
 1412  *              vm_fault
 1413  *      [Don't try vm_map_copy!]
 1414  *
 1415  *      To remove a submapping, one must first remove the
 1416  *      range from the superior map, and then destroy the
 1417  *      submap (if desired).  [Better yet, don't try it.]
 1418  */
 1419 int
 1420 vm_map_submap(
 1421         vm_map_t map,
 1422         vm_offset_t start,
 1423         vm_offset_t end,
 1424         vm_map_t submap)
 1425 {
 1426         vm_map_entry_t entry;
 1427         int result = KERN_INVALID_ARGUMENT;
 1428 
 1429         vm_map_lock(map);
 1430 
 1431         VM_MAP_RANGE_CHECK(map, start, end);
 1432 
 1433         if (vm_map_lookup_entry(map, start, &entry)) {
 1434                 vm_map_clip_start(map, entry, start);
 1435         } else
 1436                 entry = entry->next;
 1437 
 1438         vm_map_clip_end(map, entry, end);
 1439 
 1440         if ((entry->start == start) && (entry->end == end) &&
 1441             ((entry->eflags & MAP_ENTRY_COW) == 0) &&
 1442             (entry->object.vm_object == NULL)) {
 1443                 entry->object.sub_map = submap;
 1444                 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
 1445                 result = KERN_SUCCESS;
 1446         }
 1447         vm_map_unlock(map);
 1448 
 1449         return (result);
 1450 }
 1451 
 1452 /*
 1453  * The maximum number of pages to map
 1454  */
 1455 #define MAX_INIT_PT     96
 1456 
 1457 /*
 1458  *      vm_map_pmap_enter:
 1459  *
 1460  *      Preload read-only mappings for the given object's resident pages into
 1461  *      the given map.  This eliminates the soft faults on process startup and
 1462  *      immediately after an mmap(2).  Unless the given flags include
 1463  *      MAP_PREFAULT_MADVISE, cached pages are not reactivated and mapped.
 1464  */
 1465 void
 1466 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
 1467     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
 1468 {
 1469         vm_offset_t start;
 1470         vm_page_t p, p_start;
 1471         vm_pindex_t psize, tmpidx;
 1472         boolean_t are_queues_locked;
 1473 
 1474         if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
 1475                 return;
 1476         VM_OBJECT_LOCK(object);
 1477         if (object->type == OBJT_DEVICE) {
 1478                 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
 1479                 goto unlock_return;
 1480         }
 1481 
 1482         psize = atop(size);
 1483 
 1484         if (object->type != OBJT_VNODE ||
 1485             ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
 1486              (object->resident_page_count > MAX_INIT_PT))) {
 1487                 goto unlock_return;
 1488         }
 1489 
 1490         if (psize + pindex > object->size) {
 1491                 if (object->size < pindex)
 1492                         goto unlock_return;
 1493                 psize = object->size - pindex;
 1494         }
 1495 
 1496         are_queues_locked = FALSE;
 1497         start = 0;
 1498         p_start = NULL;
 1499 
 1500         if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
 1501                 if (p->pindex < pindex) {
 1502                         p = vm_page_splay(pindex, object->root);
 1503                         if ((object->root = p)->pindex < pindex)
 1504                                 p = TAILQ_NEXT(p, listq);
 1505                 }
 1506         }
 1507         /*
 1508          * Assert: the variable p is either (1) the page with the
 1509          * least pindex greater than or equal to the parameter pindex
 1510          * or (2) NULL.
 1511          */
 1512         for (;
 1513              p != NULL && (tmpidx = p->pindex - pindex) < psize;
 1514              p = TAILQ_NEXT(p, listq)) {
 1515                 /*
 1516                  * don't allow an madvise to blow away our really
 1517                  * free pages allocating pv entries.
 1518                  */
 1519                 if ((flags & MAP_PREFAULT_MADVISE) &&
 1520                     cnt.v_free_count < cnt.v_free_reserved) {
 1521                         psize = tmpidx;
 1522                         break;
 1523                 }
 1524                 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
 1525                     (p->busy == 0)) {
 1526                         if (p_start == NULL) {
 1527                                 start = addr + ptoa(tmpidx);
 1528                                 p_start = p;
 1529                         }
 1530                 } else if (p_start != NULL) {
 1531                         if (!are_queues_locked) {
 1532                                 are_queues_locked = TRUE;
 1533                                 vm_page_lock_queues();
 1534                         }
 1535                         pmap_enter_object(map->pmap, start, addr +
 1536                             ptoa(tmpidx), p_start, prot);
 1537                         p_start = NULL;
 1538                 }
 1539         }
 1540         if (p_start != NULL) {
 1541                 if (!are_queues_locked) {
 1542                         are_queues_locked = TRUE;
 1543                         vm_page_lock_queues();
 1544                 }
 1545                 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
 1546                     p_start, prot);
 1547         }
 1548         if (are_queues_locked)
 1549                 vm_page_unlock_queues();
 1550 unlock_return:
 1551         VM_OBJECT_UNLOCK(object);
 1552 }
 1553 
 1554 /*
 1555  *      vm_map_protect:
 1556  *
 1557  *      Sets the protection of the specified address
 1558  *      region in the target map.  If "set_max" is
 1559  *      specified, the maximum protection is to be set;
 1560  *      otherwise, only the current protection is affected.
 1561  */
 1562 int
 1563 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
 1564                vm_prot_t new_prot, boolean_t set_max)
 1565 {
 1566         vm_map_entry_t current;
 1567         vm_map_entry_t entry;
 1568 
 1569         vm_map_lock(map);
 1570 
 1571         VM_MAP_RANGE_CHECK(map, start, end);
 1572 
 1573         if (vm_map_lookup_entry(map, start, &entry)) {
 1574                 vm_map_clip_start(map, entry, start);
 1575         } else {
 1576                 entry = entry->next;
 1577         }
 1578 
 1579         /*
 1580          * Make a first pass to check for protection violations.
 1581          */
 1582         current = entry;
 1583         while ((current != &map->header) && (current->start < end)) {
 1584                 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
 1585                         vm_map_unlock(map);
 1586                         return (KERN_INVALID_ARGUMENT);
 1587                 }
 1588                 if ((new_prot & current->max_protection) != new_prot) {
 1589                         vm_map_unlock(map);
 1590                         return (KERN_PROTECTION_FAILURE);
 1591                 }
 1592                 current = current->next;
 1593         }
 1594 
 1595         /*
 1596          * Go back and fix up protections. [Note that clipping is not
 1597          * necessary the second time.]
 1598          */
 1599         current = entry;
 1600         while ((current != &map->header) && (current->start < end)) {
 1601                 vm_prot_t old_prot;
 1602 
 1603                 vm_map_clip_end(map, current, end);
 1604 
 1605                 old_prot = current->protection;
 1606                 if (set_max)
 1607                         current->protection =
 1608                             (current->max_protection = new_prot) &
 1609                             old_prot;
 1610                 else
 1611                         current->protection = new_prot;
 1612 
 1613                 /*
 1614                  * Update physical map if necessary. Worry about copy-on-write
 1615                  * here -- CHECK THIS XXX
 1616                  */
 1617                 if (current->protection != old_prot) {
 1618 #define MASK(entry)     (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
 1619                                                         VM_PROT_ALL)
 1620                         pmap_protect(map->pmap, current->start,
 1621                             current->end,
 1622                             current->protection & MASK(current));
 1623 #undef  MASK
 1624                 }
 1625                 vm_map_simplify_entry(map, current);
 1626                 current = current->next;
 1627         }
 1628         vm_map_unlock(map);
 1629         return (KERN_SUCCESS);
 1630 }
 1631 
 1632 /*
 1633  *      vm_map_madvise:
 1634  *
 1635  *      This routine traverses a processes map handling the madvise
 1636  *      system call.  Advisories are classified as either those effecting
 1637  *      the vm_map_entry structure, or those effecting the underlying
 1638  *      objects.
 1639  */
 1640 int
 1641 vm_map_madvise(
 1642         vm_map_t map,
 1643         vm_offset_t start,
 1644         vm_offset_t end,
 1645         int behav)
 1646 {
 1647         vm_map_entry_t current, entry;
 1648         int modify_map = 0;
 1649 
 1650         /*
 1651          * Some madvise calls directly modify the vm_map_entry, in which case
 1652          * we need to use an exclusive lock on the map and we need to perform
 1653          * various clipping operations.  Otherwise we only need a read-lock
 1654          * on the map.
 1655          */
 1656         switch(behav) {
 1657         case MADV_NORMAL:
 1658         case MADV_SEQUENTIAL:
 1659         case MADV_RANDOM:
 1660         case MADV_NOSYNC:
 1661         case MADV_AUTOSYNC:
 1662         case MADV_NOCORE:
 1663         case MADV_CORE:
 1664                 modify_map = 1;
 1665                 vm_map_lock(map);
 1666                 break;
 1667         case MADV_WILLNEED:
 1668         case MADV_DONTNEED:
 1669         case MADV_FREE:
 1670                 vm_map_lock_read(map);
 1671                 break;
 1672         default:
 1673                 return (KERN_INVALID_ARGUMENT);
 1674         }
 1675 
 1676         /*
 1677          * Locate starting entry and clip if necessary.
 1678          */
 1679         VM_MAP_RANGE_CHECK(map, start, end);
 1680 
 1681         if (vm_map_lookup_entry(map, start, &entry)) {
 1682                 if (modify_map)
 1683                         vm_map_clip_start(map, entry, start);
 1684         } else {
 1685                 entry = entry->next;
 1686         }
 1687 
 1688         if (modify_map) {
 1689                 /*
 1690                  * madvise behaviors that are implemented in the vm_map_entry.
 1691                  *
 1692                  * We clip the vm_map_entry so that behavioral changes are
 1693                  * limited to the specified address range.
 1694                  */
 1695                 for (current = entry;
 1696                      (current != &map->header) && (current->start < end);
 1697                      current = current->next
 1698                 ) {
 1699                         if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
 1700                                 continue;
 1701 
 1702                         vm_map_clip_end(map, current, end);
 1703 
 1704                         switch (behav) {
 1705                         case MADV_NORMAL:
 1706                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
 1707                                 break;
 1708                         case MADV_SEQUENTIAL:
 1709                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
 1710                                 break;
 1711                         case MADV_RANDOM:
 1712                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
 1713                                 break;
 1714                         case MADV_NOSYNC:
 1715                                 current->eflags |= MAP_ENTRY_NOSYNC;
 1716                                 break;
 1717                         case MADV_AUTOSYNC:
 1718                                 current->eflags &= ~MAP_ENTRY_NOSYNC;
 1719                                 break;
 1720                         case MADV_NOCORE:
 1721                                 current->eflags |= MAP_ENTRY_NOCOREDUMP;
 1722                                 break;
 1723                         case MADV_CORE:
 1724                                 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
 1725                                 break;
 1726                         default:
 1727                                 break;
 1728                         }
 1729                         vm_map_simplify_entry(map, current);
 1730                 }
 1731                 vm_map_unlock(map);
 1732         } else {
 1733                 vm_pindex_t pindex;
 1734                 int count;
 1735 
 1736                 /*
 1737                  * madvise behaviors that are implemented in the underlying
 1738                  * vm_object.
 1739                  *
 1740                  * Since we don't clip the vm_map_entry, we have to clip
 1741                  * the vm_object pindex and count.
 1742                  */
 1743                 for (current = entry;
 1744                      (current != &map->header) && (current->start < end);
 1745                      current = current->next
 1746                 ) {
 1747                         vm_offset_t useStart;
 1748 
 1749                         if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
 1750                                 continue;
 1751 
 1752                         pindex = OFF_TO_IDX(current->offset);
 1753                         count = atop(current->end - current->start);
 1754                         useStart = current->start;
 1755 
 1756                         if (current->start < start) {
 1757                                 pindex += atop(start - current->start);
 1758                                 count -= atop(start - current->start);
 1759                                 useStart = start;
 1760                         }
 1761                         if (current->end > end)
 1762                                 count -= atop(current->end - end);
 1763 
 1764                         if (count <= 0)
 1765                                 continue;
 1766 
 1767                         vm_object_madvise(current->object.vm_object,
 1768                                           pindex, count, behav);
 1769                         if (behav == MADV_WILLNEED) {
 1770                                 vm_map_pmap_enter(map,
 1771                                     useStart,
 1772                                     current->protection,
 1773                                     current->object.vm_object,
 1774                                     pindex,
 1775                                     (count << PAGE_SHIFT),
 1776                                     MAP_PREFAULT_MADVISE
 1777                                 );
 1778                         }
 1779                 }
 1780                 vm_map_unlock_read(map);
 1781         }
 1782         return (0);
 1783 }
 1784 
 1785 
 1786 /*
 1787  *      vm_map_inherit:
 1788  *
 1789  *      Sets the inheritance of the specified address
 1790  *      range in the target map.  Inheritance
 1791  *      affects how the map will be shared with
 1792  *      child maps at the time of vm_map_fork.
 1793  */
 1794 int
 1795 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
 1796                vm_inherit_t new_inheritance)
 1797 {
 1798         vm_map_entry_t entry;
 1799         vm_map_entry_t temp_entry;
 1800 
 1801         switch (new_inheritance) {
 1802         case VM_INHERIT_NONE:
 1803         case VM_INHERIT_COPY:
 1804         case VM_INHERIT_SHARE:
 1805                 break;
 1806         default:
 1807                 return (KERN_INVALID_ARGUMENT);
 1808         }
 1809         vm_map_lock(map);
 1810         VM_MAP_RANGE_CHECK(map, start, end);
 1811         if (vm_map_lookup_entry(map, start, &temp_entry)) {
 1812                 entry = temp_entry;
 1813                 vm_map_clip_start(map, entry, start);
 1814         } else
 1815                 entry = temp_entry->next;
 1816         while ((entry != &map->header) && (entry->start < end)) {
 1817                 vm_map_clip_end(map, entry, end);
 1818                 entry->inheritance = new_inheritance;
 1819                 vm_map_simplify_entry(map, entry);
 1820                 entry = entry->next;
 1821         }
 1822         vm_map_unlock(map);
 1823         return (KERN_SUCCESS);
 1824 }
 1825 
 1826 /*
 1827  *      vm_map_unwire:
 1828  *
 1829  *      Implements both kernel and user unwiring.
 1830  */
 1831 int
 1832 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
 1833     int flags)
 1834 {
 1835         vm_map_entry_t entry, first_entry, tmp_entry;
 1836         vm_offset_t saved_start;
 1837         unsigned int last_timestamp;
 1838         int rv;
 1839         boolean_t need_wakeup, result, user_unwire;
 1840 
 1841         user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
 1842         vm_map_lock(map);
 1843         VM_MAP_RANGE_CHECK(map, start, end);
 1844         if (!vm_map_lookup_entry(map, start, &first_entry)) {
 1845                 if (flags & VM_MAP_WIRE_HOLESOK)
 1846                         first_entry = first_entry->next;
 1847                 else {
 1848                         vm_map_unlock(map);
 1849                         return (KERN_INVALID_ADDRESS);
 1850                 }
 1851         }
 1852         last_timestamp = map->timestamp;
 1853         entry = first_entry;
 1854         while (entry != &map->header && entry->start < end) {
 1855                 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
 1856                         /*
 1857                          * We have not yet clipped the entry.
 1858                          */
 1859                         saved_start = (start >= entry->start) ? start :
 1860                             entry->start;
 1861                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
 1862                         if (vm_map_unlock_and_wait(map, user_unwire)) {
 1863                                 /*
 1864                                  * Allow interruption of user unwiring?
 1865                                  */
 1866                         }
 1867                         vm_map_lock(map);
 1868                         if (last_timestamp+1 != map->timestamp) {
 1869                                 /*
 1870                                  * Look again for the entry because the map was
 1871                                  * modified while it was unlocked.
 1872                                  * Specifically, the entry may have been
 1873                                  * clipped, merged, or deleted.
 1874                                  */
 1875                                 if (!vm_map_lookup_entry(map, saved_start,
 1876                                     &tmp_entry)) {
 1877                                         if (flags & VM_MAP_WIRE_HOLESOK)
 1878                                                 tmp_entry = tmp_entry->next;
 1879                                         else {
 1880                                                 if (saved_start == start) {
 1881                                                         /*
 1882                                                          * First_entry has been deleted.
 1883                                                          */
 1884                                                         vm_map_unlock(map);
 1885                                                         return (KERN_INVALID_ADDRESS);
 1886                                                 }
 1887                                                 end = saved_start;
 1888                                                 rv = KERN_INVALID_ADDRESS;
 1889                                                 goto done;
 1890                                         }
 1891                                 }
 1892                                 if (entry == first_entry)
 1893                                         first_entry = tmp_entry;
 1894                                 else
 1895                                         first_entry = NULL;
 1896                                 entry = tmp_entry;
 1897                         }
 1898                         last_timestamp = map->timestamp;
 1899                         continue;
 1900                 }
 1901                 vm_map_clip_start(map, entry, start);
 1902                 vm_map_clip_end(map, entry, end);
 1903                 /*
 1904                  * Mark the entry in case the map lock is released.  (See
 1905                  * above.)
 1906                  */
 1907                 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
 1908                 /*
 1909                  * Check the map for holes in the specified region.
 1910                  * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
 1911                  */
 1912                 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
 1913                     (entry->end < end && (entry->next == &map->header ||
 1914                     entry->next->start > entry->end))) {
 1915                         end = entry->end;
 1916                         rv = KERN_INVALID_ADDRESS;
 1917                         goto done;
 1918                 }
 1919                 /*
 1920                  * If system unwiring, require that the entry is system wired.
 1921                  */
 1922                 if (!user_unwire &&
 1923                     vm_map_entry_system_wired_count(entry) == 0) {
 1924                         end = entry->end;
 1925                         rv = KERN_INVALID_ARGUMENT;
 1926                         goto done;
 1927                 }
 1928                 entry = entry->next;
 1929         }
 1930         rv = KERN_SUCCESS;
 1931 done:
 1932         need_wakeup = FALSE;
 1933         if (first_entry == NULL) {
 1934                 result = vm_map_lookup_entry(map, start, &first_entry);
 1935                 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
 1936                         first_entry = first_entry->next;
 1937                 else
 1938                         KASSERT(result, ("vm_map_unwire: lookup failed"));
 1939         }
 1940         entry = first_entry;
 1941         while (entry != &map->header && entry->start < end) {
 1942                 if (rv == KERN_SUCCESS && (!user_unwire ||
 1943                     (entry->eflags & MAP_ENTRY_USER_WIRED))) {
 1944                         if (user_unwire)
 1945                                 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
 1946                         entry->wired_count--;
 1947                         if (entry->wired_count == 0) {
 1948                                 /*
 1949                                  * Retain the map lock.
 1950                                  */
 1951                                 vm_fault_unwire(map, entry->start, entry->end,
 1952                                     entry->object.vm_object != NULL &&
 1953                                     entry->object.vm_object->type == OBJT_DEVICE);
 1954                         }
 1955                 }
 1956                 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
 1957                         ("vm_map_unwire: in-transition flag missing"));
 1958                 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
 1959                 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
 1960                         entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
 1961                         need_wakeup = TRUE;
 1962                 }
 1963                 vm_map_simplify_entry(map, entry);
 1964                 entry = entry->next;
 1965         }
 1966         vm_map_unlock(map);
 1967         if (need_wakeup)
 1968                 vm_map_wakeup(map);
 1969         return (rv);
 1970 }
 1971 
 1972 /*
 1973  *      vm_map_wire:
 1974  *
 1975  *      Implements both kernel and user wiring.
 1976  */
 1977 int
 1978 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
 1979     int flags)
 1980 {
 1981         vm_map_entry_t entry, first_entry, tmp_entry;
 1982         vm_offset_t saved_end, saved_start;
 1983         unsigned int last_timestamp;
 1984         int rv;
 1985         boolean_t fictitious, need_wakeup, result, user_wire;
 1986 
 1987         user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
 1988         vm_map_lock(map);
 1989         VM_MAP_RANGE_CHECK(map, start, end);
 1990         if (!vm_map_lookup_entry(map, start, &first_entry)) {
 1991                 if (flags & VM_MAP_WIRE_HOLESOK)
 1992                         first_entry = first_entry->next;
 1993                 else {
 1994                         vm_map_unlock(map);
 1995                         return (KERN_INVALID_ADDRESS);
 1996                 }
 1997         }
 1998         last_timestamp = map->timestamp;
 1999         entry = first_entry;
 2000         while (entry != &map->header && entry->start < end) {
 2001                 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
 2002                         /*
 2003                          * We have not yet clipped the entry.
 2004                          */
 2005                         saved_start = (start >= entry->start) ? start :
 2006                             entry->start;
 2007                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
 2008                         if (vm_map_unlock_and_wait(map, user_wire)) {
 2009                                 /*
 2010                                  * Allow interruption of user wiring?
 2011                                  */
 2012                         }
 2013                         vm_map_lock(map);
 2014                         if (last_timestamp + 1 != map->timestamp) {
 2015                                 /*
 2016                                  * Look again for the entry because the map was
 2017                                  * modified while it was unlocked.
 2018                                  * Specifically, the entry may have been
 2019                                  * clipped, merged, or deleted.
 2020                                  */
 2021                                 if (!vm_map_lookup_entry(map, saved_start,
 2022                                     &tmp_entry)) {
 2023                                         if (flags & VM_MAP_WIRE_HOLESOK)
 2024                                                 tmp_entry = tmp_entry->next;
 2025                                         else {
 2026                                                 if (saved_start == start) {
 2027                                                         /*
 2028                                                          * first_entry has been deleted.
 2029                                                          */
 2030                                                         vm_map_unlock(map);
 2031                                                         return (KERN_INVALID_ADDRESS);
 2032                                                 }
 2033                                                 end = saved_start;
 2034                                                 rv = KERN_INVALID_ADDRESS;
 2035                                                 goto done;
 2036                                         }
 2037                                 }
 2038                                 if (entry == first_entry)
 2039                                         first_entry = tmp_entry;
 2040                                 else
 2041                                         first_entry = NULL;
 2042                                 entry = tmp_entry;
 2043                         }
 2044                         last_timestamp = map->timestamp;
 2045                         continue;
 2046                 }
 2047                 vm_map_clip_start(map, entry, start);
 2048                 vm_map_clip_end(map, entry, end);
 2049                 /*
 2050                  * Mark the entry in case the map lock is released.  (See
 2051                  * above.)
 2052                  */
 2053                 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
 2054                 /*
 2055                  *
 2056                  */
 2057                 if (entry->wired_count == 0) {
 2058                         entry->wired_count++;
 2059                         saved_start = entry->start;
 2060                         saved_end = entry->end;
 2061                         fictitious = entry->object.vm_object != NULL &&
 2062                             entry->object.vm_object->type == OBJT_DEVICE;
 2063                         /*
 2064                          * Release the map lock, relying on the in-transition
 2065                          * mark.
 2066                          */
 2067                         vm_map_unlock(map);
 2068                         rv = vm_fault_wire(map, saved_start, saved_end,
 2069                             user_wire, fictitious);
 2070                         vm_map_lock(map);
 2071                         if (last_timestamp + 1 != map->timestamp) {
 2072                                 /*
 2073                                  * Look again for the entry because the map was
 2074                                  * modified while it was unlocked.  The entry
 2075                                  * may have been clipped, but NOT merged or
 2076                                  * deleted.
 2077                                  */
 2078                                 result = vm_map_lookup_entry(map, saved_start,
 2079                                     &tmp_entry);
 2080                                 KASSERT(result, ("vm_map_wire: lookup failed"));
 2081                                 if (entry == first_entry)
 2082                                         first_entry = tmp_entry;
 2083                                 else
 2084                                         first_entry = NULL;
 2085                                 entry = tmp_entry;
 2086                                 while (entry->end < saved_end) {
 2087                                         if (rv != KERN_SUCCESS) {
 2088                                                 KASSERT(entry->wired_count == 1,
 2089                                                     ("vm_map_wire: bad count"));
 2090                                                 entry->wired_count = -1;
 2091                                         }
 2092                                         entry = entry->next;
 2093                                 }
 2094                         }
 2095                         last_timestamp = map->timestamp;
 2096                         if (rv != KERN_SUCCESS) {
 2097                                 KASSERT(entry->wired_count == 1,
 2098                                     ("vm_map_wire: bad count"));
 2099                                 /*
 2100                                  * Assign an out-of-range value to represent
 2101                                  * the failure to wire this entry.
 2102                                  */
 2103                                 entry->wired_count = -1;
 2104                                 end = entry->end;
 2105                                 goto done;
 2106                         }
 2107                 } else if (!user_wire ||
 2108                            (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
 2109                         entry->wired_count++;
 2110                 }
 2111                 /*
 2112                  * Check the map for holes in the specified region.
 2113                  * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
 2114                  */
 2115                 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
 2116                     (entry->end < end && (entry->next == &map->header ||
 2117                     entry->next->start > entry->end))) {
 2118                         end = entry->end;
 2119                         rv = KERN_INVALID_ADDRESS;
 2120                         goto done;
 2121                 }
 2122                 entry = entry->next;
 2123         }
 2124         rv = KERN_SUCCESS;
 2125 done:
 2126         need_wakeup = FALSE;
 2127         if (first_entry == NULL) {
 2128                 result = vm_map_lookup_entry(map, start, &first_entry);
 2129                 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
 2130                         first_entry = first_entry->next;
 2131                 else
 2132                         KASSERT(result, ("vm_map_wire: lookup failed"));
 2133         }
 2134         entry = first_entry;
 2135         while (entry != &map->header && entry->start < end) {
 2136                 if (rv == KERN_SUCCESS) {
 2137                         if (user_wire)
 2138                                 entry->eflags |= MAP_ENTRY_USER_WIRED;
 2139                 } else if (entry->wired_count == -1) {
 2140                         /*
 2141                          * Wiring failed on this entry.  Thus, unwiring is
 2142                          * unnecessary.
 2143                          */
 2144                         entry->wired_count = 0;
 2145                 } else {
 2146                         if (!user_wire ||
 2147                             (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
 2148                                 entry->wired_count--;
 2149                         if (entry->wired_count == 0) {
 2150                                 /*
 2151                                  * Retain the map lock.
 2152                                  */
 2153                                 vm_fault_unwire(map, entry->start, entry->end,
 2154                                     entry->object.vm_object != NULL &&
 2155                                     entry->object.vm_object->type == OBJT_DEVICE);
 2156                         }
 2157                 }
 2158                 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
 2159                         ("vm_map_wire: in-transition flag missing"));
 2160                 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
 2161                 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
 2162                         entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
 2163                         need_wakeup = TRUE;
 2164                 }
 2165                 vm_map_simplify_entry(map, entry);
 2166                 entry = entry->next;
 2167         }
 2168         vm_map_unlock(map);
 2169         if (need_wakeup)
 2170                 vm_map_wakeup(map);
 2171         return (rv);
 2172 }
 2173 
 2174 /*
 2175  * vm_map_sync
 2176  *
 2177  * Push any dirty cached pages in the address range to their pager.
 2178  * If syncio is TRUE, dirty pages are written synchronously.
 2179  * If invalidate is TRUE, any cached pages are freed as well.
 2180  *
 2181  * If the size of the region from start to end is zero, we are
 2182  * supposed to flush all modified pages within the region containing
 2183  * start.  Unfortunately, a region can be split or coalesced with
 2184  * neighboring regions, making it difficult to determine what the
 2185  * original region was.  Therefore, we approximate this requirement by
 2186  * flushing the current region containing start.
 2187  *
 2188  * Returns an error if any part of the specified range is not mapped.
 2189  */
 2190 int
 2191 vm_map_sync(
 2192         vm_map_t map,
 2193         vm_offset_t start,
 2194         vm_offset_t end,
 2195         boolean_t syncio,
 2196         boolean_t invalidate)
 2197 {
 2198         vm_map_entry_t current;
 2199         vm_map_entry_t entry;
 2200         vm_size_t size;
 2201         vm_object_t object;
 2202         vm_ooffset_t offset;
 2203 
 2204         vm_map_lock_read(map);
 2205         VM_MAP_RANGE_CHECK(map, start, end);
 2206         if (!vm_map_lookup_entry(map, start, &entry)) {
 2207                 vm_map_unlock_read(map);
 2208                 return (KERN_INVALID_ADDRESS);
 2209         } else if (start == end) {
 2210                 start = entry->start;
 2211                 end = entry->end;
 2212         }
 2213         /*
 2214          * Make a first pass to check for user-wired memory and holes.
 2215          */
 2216         for (current = entry; current != &map->header && current->start < end;
 2217             current = current->next) {
 2218                 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
 2219                         vm_map_unlock_read(map);
 2220                         return (KERN_INVALID_ARGUMENT);
 2221                 }
 2222                 if (end > current->end &&
 2223                     (current->next == &map->header ||
 2224                         current->end != current->next->start)) {
 2225                         vm_map_unlock_read(map);
 2226                         return (KERN_INVALID_ADDRESS);
 2227                 }
 2228         }
 2229 
 2230         if (invalidate)
 2231                 pmap_remove(map->pmap, start, end);
 2232 
 2233         /*
 2234          * Make a second pass, cleaning/uncaching pages from the indicated
 2235          * objects as we go.
 2236          */
 2237         for (current = entry; current != &map->header && current->start < end;
 2238             current = current->next) {
 2239                 offset = current->offset + (start - current->start);
 2240                 size = (end <= current->end ? end : current->end) - start;
 2241                 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
 2242                         vm_map_t smap;
 2243                         vm_map_entry_t tentry;
 2244                         vm_size_t tsize;
 2245 
 2246                         smap = current->object.sub_map;
 2247                         vm_map_lock_read(smap);
 2248                         (void) vm_map_lookup_entry(smap, offset, &tentry);
 2249                         tsize = tentry->end - offset;
 2250                         if (tsize < size)
 2251                                 size = tsize;
 2252                         object = tentry->object.vm_object;
 2253                         offset = tentry->offset + (offset - tentry->start);
 2254                         vm_map_unlock_read(smap);
 2255                 } else {
 2256                         object = current->object.vm_object;
 2257                 }
 2258                 vm_object_sync(object, offset, size, syncio, invalidate);
 2259                 start += size;
 2260         }
 2261 
 2262         vm_map_unlock_read(map);
 2263         return (KERN_SUCCESS);
 2264 }
 2265 
 2266 /*
 2267  *      vm_map_entry_unwire:    [ internal use only ]
 2268  *
 2269  *      Make the region specified by this entry pageable.
 2270  *
 2271  *      The map in question should be locked.
 2272  *      [This is the reason for this routine's existence.]
 2273  */
 2274 static void
 2275 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
 2276 {
 2277         vm_fault_unwire(map, entry->start, entry->end,
 2278             entry->object.vm_object != NULL &&
 2279             entry->object.vm_object->type == OBJT_DEVICE);
 2280         entry->wired_count = 0;
 2281 }
 2282 
 2283 /*
 2284  *      vm_map_entry_delete:    [ internal use only ]
 2285  *
 2286  *      Deallocate the given entry from the target map.
 2287  */
 2288 static void
 2289 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
 2290 {
 2291         vm_object_t object;
 2292         vm_pindex_t offidxstart, offidxend, count;
 2293 
 2294         vm_map_entry_unlink(map, entry);
 2295         map->size -= entry->end - entry->start;
 2296 
 2297         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
 2298             (object = entry->object.vm_object) != NULL) {
 2299                 count = OFF_TO_IDX(entry->end - entry->start);
 2300                 offidxstart = OFF_TO_IDX(entry->offset);
 2301                 offidxend = offidxstart + count;
 2302                 VM_OBJECT_LOCK(object);
 2303                 if (object->ref_count != 1 &&
 2304                     ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
 2305                     object == kernel_object || object == kmem_object)) {
 2306                         vm_object_collapse(object);
 2307                         vm_object_page_remove(object, offidxstart, offidxend, FALSE);
 2308                         if (object->type == OBJT_SWAP)
 2309                                 swap_pager_freespace(object, offidxstart, count);
 2310                         if (offidxend >= object->size &&
 2311                             offidxstart < object->size)
 2312                                 object->size = offidxstart;
 2313                 }
 2314                 VM_OBJECT_UNLOCK(object);
 2315                 vm_object_deallocate(object);
 2316         }
 2317 
 2318         vm_map_entry_dispose(map, entry);
 2319 }
 2320 
 2321 /*
 2322  *      vm_map_delete:  [ internal use only ]
 2323  *
 2324  *      Deallocates the given address range from the target
 2325  *      map.
 2326  */
 2327 int
 2328 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
 2329 {
 2330         vm_map_entry_t entry;
 2331         vm_map_entry_t first_entry;
 2332 
 2333         /*
 2334          * Find the start of the region, and clip it
 2335          */
 2336         if (!vm_map_lookup_entry(map, start, &first_entry))
 2337                 entry = first_entry->next;
 2338         else {
 2339                 entry = first_entry;
 2340                 vm_map_clip_start(map, entry, start);
 2341         }
 2342 
 2343         /*
 2344          * Step through all entries in this region
 2345          */
 2346         while ((entry != &map->header) && (entry->start < end)) {
 2347                 vm_map_entry_t next;
 2348 
 2349                 /*
 2350                  * Wait for wiring or unwiring of an entry to complete.
 2351                  * Also wait for any system wirings to disappear on
 2352                  * user maps.
 2353                  */
 2354                 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
 2355                     (vm_map_pmap(map) != kernel_pmap &&
 2356                     vm_map_entry_system_wired_count(entry) != 0)) {
 2357                         unsigned int last_timestamp;
 2358                         vm_offset_t saved_start;
 2359                         vm_map_entry_t tmp_entry;
 2360 
 2361                         saved_start = entry->start;
 2362                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
 2363                         last_timestamp = map->timestamp;
 2364                         (void) vm_map_unlock_and_wait(map, FALSE);
 2365                         vm_map_lock(map);
 2366                         if (last_timestamp + 1 != map->timestamp) {
 2367                                 /*
 2368                                  * Look again for the entry because the map was
 2369                                  * modified while it was unlocked.
 2370                                  * Specifically, the entry may have been
 2371                                  * clipped, merged, or deleted.
 2372                                  */
 2373                                 if (!vm_map_lookup_entry(map, saved_start,
 2374                                                          &tmp_entry))
 2375                                         entry = tmp_entry->next;
 2376                                 else {
 2377                                         entry = tmp_entry;
 2378                                         vm_map_clip_start(map, entry,
 2379                                                           saved_start);
 2380                                 }
 2381                         }
 2382                         continue;
 2383                 }
 2384                 vm_map_clip_end(map, entry, end);
 2385 
 2386                 next = entry->next;
 2387 
 2388                 /*
 2389                  * Unwire before removing addresses from the pmap; otherwise,
 2390                  * unwiring will put the entries back in the pmap.
 2391                  */
 2392                 if (entry->wired_count != 0) {
 2393                         vm_map_entry_unwire(map, entry);
 2394                 }
 2395 
 2396                 pmap_remove(map->pmap, entry->start, entry->end);
 2397 
 2398                 /*
 2399                  * Delete the entry (which may delete the object) only after
 2400                  * removing all pmap entries pointing to its pages.
 2401                  * (Otherwise, its page frames may be reallocated, and any
 2402                  * modify bits will be set in the wrong object!)
 2403                  */
 2404                 vm_map_entry_delete(map, entry);
 2405                 entry = next;
 2406         }
 2407         return (KERN_SUCCESS);
 2408 }
 2409 
 2410 /*
 2411  *      vm_map_remove:
 2412  *
 2413  *      Remove the given address range from the target map.
 2414  *      This is the exported form of vm_map_delete.
 2415  */
 2416 int
 2417 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
 2418 {
 2419         int result;
 2420 
 2421         vm_map_lock(map);
 2422         VM_MAP_RANGE_CHECK(map, start, end);
 2423         result = vm_map_delete(map, start, end);
 2424         vm_map_unlock(map);
 2425         return (result);
 2426 }
 2427 
 2428 /*
 2429  *      vm_map_check_protection:
 2430  *
 2431  *      Assert that the target map allows the specified privilege on the
 2432  *      entire address region given.  The entire region must be allocated.
 2433  *
 2434  *      WARNING!  This code does not and should not check whether the
 2435  *      contents of the region is accessible.  For example a smaller file
 2436  *      might be mapped into a larger address space.
 2437  *
 2438  *      NOTE!  This code is also called by munmap().
 2439  *
 2440  *      The map must be locked.  A read lock is sufficient.
 2441  */
 2442 boolean_t
 2443 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
 2444                         vm_prot_t protection)
 2445 {
 2446         vm_map_entry_t entry;
 2447         vm_map_entry_t tmp_entry;
 2448 
 2449         if (!vm_map_lookup_entry(map, start, &tmp_entry))
 2450                 return (FALSE);
 2451         entry = tmp_entry;
 2452 
 2453         while (start < end) {
 2454                 if (entry == &map->header)
 2455                         return (FALSE);
 2456                 /*
 2457                  * No holes allowed!
 2458                  */
 2459                 if (start < entry->start)
 2460                         return (FALSE);
 2461                 /*
 2462                  * Check protection associated with entry.
 2463                  */
 2464                 if ((entry->protection & protection) != protection)
 2465                         return (FALSE);
 2466                 /* go to next entry */
 2467                 start = entry->end;
 2468                 entry = entry->next;
 2469         }
 2470         return (TRUE);
 2471 }
 2472 
 2473 /*
 2474  *      vm_map_copy_entry:
 2475  *
 2476  *      Copies the contents of the source entry to the destination
 2477  *      entry.  The entries *must* be aligned properly.
 2478  */
 2479 static void
 2480 vm_map_copy_entry(
 2481         vm_map_t src_map,
 2482         vm_map_t dst_map,
 2483         vm_map_entry_t src_entry,
 2484         vm_map_entry_t dst_entry)
 2485 {
 2486         vm_object_t src_object;
 2487 
 2488         if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
 2489                 return;
 2490 
 2491         if (src_entry->wired_count == 0) {
 2492 
 2493                 /*
 2494                  * If the source entry is marked needs_copy, it is already
 2495                  * write-protected.
 2496                  */
 2497                 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
 2498                         pmap_protect(src_map->pmap,
 2499                             src_entry->start,
 2500                             src_entry->end,
 2501                             src_entry->protection & ~VM_PROT_WRITE);
 2502                 }
 2503 
 2504                 /*
 2505                  * Make a copy of the object.
 2506                  */
 2507                 if ((src_object = src_entry->object.vm_object) != NULL) {
 2508                         VM_OBJECT_LOCK(src_object);
 2509                         if ((src_object->handle == NULL) &&
 2510                                 (src_object->type == OBJT_DEFAULT ||
 2511                                  src_object->type == OBJT_SWAP)) {
 2512                                 vm_object_collapse(src_object);
 2513                                 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
 2514                                         vm_object_split(src_entry);
 2515                                         src_object = src_entry->object.vm_object;
 2516                                 }
 2517                         }
 2518                         vm_object_reference_locked(src_object);
 2519                         vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
 2520                         VM_OBJECT_UNLOCK(src_object);
 2521                         dst_entry->object.vm_object = src_object;
 2522                         src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
 2523                         dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
 2524                         dst_entry->offset = src_entry->offset;
 2525                 } else {
 2526                         dst_entry->object.vm_object = NULL;
 2527                         dst_entry->offset = 0;
 2528                 }
 2529 
 2530                 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
 2531                     dst_entry->end - dst_entry->start, src_entry->start);
 2532         } else {
 2533                 /*
 2534                  * Of course, wired down pages can't be set copy-on-write.
 2535                  * Cause wired pages to be copied into the new map by
 2536                  * simulating faults (the new pages are pageable)
 2537                  */
 2538                 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
 2539         }
 2540 }
 2541 
 2542 /*
 2543  * vmspace_map_entry_forked:
 2544  * Update the newly-forked vmspace each time a map entry is inherited
 2545  * or copied.  The values for vm_dsize and vm_tsize are approximate
 2546  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
 2547  */
 2548 static void
 2549 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
 2550     vm_map_entry_t entry)
 2551 {
 2552         vm_size_t entrysize;
 2553         vm_offset_t newend;
 2554 
 2555         entrysize = entry->end - entry->start;
 2556         vm2->vm_map.size += entrysize;
 2557         if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
 2558                 vm2->vm_ssize += btoc(entrysize);
 2559         } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
 2560             entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
 2561                 newend = MIN(entry->end,
 2562                     (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
 2563                 vm2->vm_dsize += btoc(newend - entry->start);
 2564         } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
 2565             entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
 2566                 newend = MIN(entry->end,
 2567                     (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
 2568                 vm2->vm_tsize += btoc(newend - entry->start);
 2569         }
 2570 }
 2571 
 2572 /*
 2573  * vmspace_fork:
 2574  * Create a new process vmspace structure and vm_map
 2575  * based on those of an existing process.  The new map
 2576  * is based on the old map, according to the inheritance
 2577  * values on the regions in that map.
 2578  *
 2579  * XXX It might be worth coalescing the entries added to the new vmspace.
 2580  *
 2581  * The source map must not be locked.
 2582  */
 2583 struct vmspace *
 2584 vmspace_fork(struct vmspace *vm1)
 2585 {
 2586         struct vmspace *vm2;
 2587         vm_map_t old_map = &vm1->vm_map;
 2588         vm_map_t new_map;
 2589         vm_map_entry_t old_entry;
 2590         vm_map_entry_t new_entry;
 2591         vm_object_t object;
 2592 
 2593         vm_map_lock(old_map);
 2594 
 2595         vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
 2596         if (vm2 == NULL)
 2597                 goto unlock_and_return;
 2598         vm2->vm_taddr = vm1->vm_taddr;
 2599         vm2->vm_daddr = vm1->vm_daddr;
 2600         vm2->vm_maxsaddr = vm1->vm_maxsaddr;
 2601         new_map = &vm2->vm_map; /* XXX */
 2602         new_map->timestamp = 1;
 2603 
 2604         old_entry = old_map->header.next;
 2605 
 2606         while (old_entry != &old_map->header) {
 2607                 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 2608                         panic("vm_map_fork: encountered a submap");
 2609 
 2610                 switch (old_entry->inheritance) {
 2611                 case VM_INHERIT_NONE:
 2612                         break;
 2613 
 2614                 case VM_INHERIT_SHARE:
 2615                         /*
 2616                          * Clone the entry, creating the shared object if necessary.
 2617                          */
 2618                         object = old_entry->object.vm_object;
 2619                         if (object == NULL) {
 2620                                 object = vm_object_allocate(OBJT_DEFAULT,
 2621                                         atop(old_entry->end - old_entry->start));
 2622                                 old_entry->object.vm_object = object;
 2623                                 old_entry->offset = 0;
 2624                         }
 2625 
 2626                         /*
 2627                          * Add the reference before calling vm_object_shadow
 2628                          * to insure that a shadow object is created.
 2629                          */
 2630                         vm_object_reference(object);
 2631                         if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
 2632                                 vm_object_shadow(&old_entry->object.vm_object,
 2633                                         &old_entry->offset,
 2634                                         atop(old_entry->end - old_entry->start));
 2635                                 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
 2636                                 /* Transfer the second reference too. */
 2637                                 vm_object_reference(
 2638                                     old_entry->object.vm_object);
 2639                                 vm_object_deallocate(object);
 2640                                 object = old_entry->object.vm_object;
 2641                         }
 2642                         VM_OBJECT_LOCK(object);
 2643                         vm_object_clear_flag(object, OBJ_ONEMAPPING);
 2644                         VM_OBJECT_UNLOCK(object);
 2645 
 2646                         /*
 2647                          * Clone the entry, referencing the shared object.
 2648                          */
 2649                         new_entry = vm_map_entry_create(new_map);
 2650                         *new_entry = *old_entry;
 2651                         new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
 2652                         new_entry->wired_count = 0;
 2653 
 2654                         /*
 2655                          * Insert the entry into the new map -- we know we're
 2656                          * inserting at the end of the new map.
 2657                          */
 2658                         vm_map_entry_link(new_map, new_map->header.prev,
 2659                             new_entry);
 2660                         vmspace_map_entry_forked(vm1, vm2, new_entry);
 2661 
 2662                         /*
 2663                          * Update the physical map
 2664                          */
 2665                         pmap_copy(new_map->pmap, old_map->pmap,
 2666                             new_entry->start,
 2667                             (old_entry->end - old_entry->start),
 2668                             old_entry->start);
 2669                         break;
 2670 
 2671                 case VM_INHERIT_COPY:
 2672                         /*
 2673                          * Clone the entry and link into the map.
 2674                          */
 2675                         new_entry = vm_map_entry_create(new_map);
 2676                         *new_entry = *old_entry;
 2677                         new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
 2678                         new_entry->wired_count = 0;
 2679                         new_entry->object.vm_object = NULL;
 2680                         vm_map_entry_link(new_map, new_map->header.prev,
 2681                             new_entry);
 2682                         vmspace_map_entry_forked(vm1, vm2, new_entry);
 2683                         vm_map_copy_entry(old_map, new_map, old_entry,
 2684                             new_entry);
 2685                         break;
 2686                 }
 2687                 old_entry = old_entry->next;
 2688         }
 2689 unlock_and_return:
 2690         vm_map_unlock(old_map);
 2691 
 2692         return (vm2);
 2693 }
 2694 
 2695 int
 2696 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
 2697     vm_prot_t prot, vm_prot_t max, int cow)
 2698 {
 2699         vm_map_entry_t new_entry, prev_entry;
 2700         vm_offset_t bot, top;
 2701         vm_size_t init_ssize;
 2702         int orient, rv;
 2703         rlim_t vmemlim;
 2704 
 2705         /*
 2706          * The stack orientation is piggybacked with the cow argument.
 2707          * Extract it into orient and mask the cow argument so that we
 2708          * don't pass it around further.
 2709          * NOTE: We explicitly allow bi-directional stacks.
 2710          */
 2711         orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
 2712         cow &= ~orient;
 2713         KASSERT(orient != 0, ("No stack grow direction"));
 2714 
 2715         if (addrbos < vm_map_min(map) ||
 2716             addrbos > vm_map_max(map) ||
 2717             addrbos + max_ssize < addrbos)
 2718                 return (KERN_NO_SPACE);
 2719 
 2720         init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
 2721 
 2722         PROC_LOCK(curthread->td_proc);
 2723         vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
 2724         PROC_UNLOCK(curthread->td_proc);
 2725 
 2726         vm_map_lock(map);
 2727 
 2728         /* If addr is already mapped, no go */
 2729         if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
 2730                 vm_map_unlock(map);
 2731                 return (KERN_NO_SPACE);
 2732         }
 2733 
 2734         /* If we would blow our VMEM resource limit, no go */
 2735         if (map->size + init_ssize > vmemlim) {
 2736                 vm_map_unlock(map);
 2737                 return (KERN_NO_SPACE);
 2738         }
 2739 
 2740         /*
 2741          * If we can't accomodate max_ssize in the current mapping, no go.
 2742          * However, we need to be aware that subsequent user mappings might
 2743          * map into the space we have reserved for stack, and currently this
 2744          * space is not protected.
 2745          *
 2746          * Hopefully we will at least detect this condition when we try to
 2747          * grow the stack.
 2748          */
 2749         if ((prev_entry->next != &map->header) &&
 2750             (prev_entry->next->start < addrbos + max_ssize)) {
 2751                 vm_map_unlock(map);
 2752                 return (KERN_NO_SPACE);
 2753         }
 2754 
 2755         /*
 2756          * We initially map a stack of only init_ssize.  We will grow as
 2757          * needed later.  Depending on the orientation of the stack (i.e.
 2758          * the grow direction) we either map at the top of the range, the
 2759          * bottom of the range or in the middle.
 2760          *
 2761          * Note: we would normally expect prot and max to be VM_PROT_ALL,
 2762          * and cow to be 0.  Possibly we should eliminate these as input
 2763          * parameters, and just pass these values here in the insert call.
 2764          */
 2765         if (orient == MAP_STACK_GROWS_DOWN)
 2766                 bot = addrbos + max_ssize - init_ssize;
 2767         else if (orient == MAP_STACK_GROWS_UP)
 2768                 bot = addrbos;
 2769         else
 2770                 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
 2771         top = bot + init_ssize;
 2772         rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
 2773 
 2774         /* Now set the avail_ssize amount. */
 2775         if (rv == KERN_SUCCESS) {
 2776                 if (prev_entry != &map->header)
 2777                         vm_map_clip_end(map, prev_entry, bot);
 2778                 new_entry = prev_entry->next;
 2779                 if (new_entry->end != top || new_entry->start != bot)
 2780                         panic("Bad entry start/end for new stack entry");
 2781 
 2782                 new_entry->avail_ssize = max_ssize - init_ssize;
 2783                 if (orient & MAP_STACK_GROWS_DOWN)
 2784                         new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
 2785                 if (orient & MAP_STACK_GROWS_UP)
 2786                         new_entry->eflags |= MAP_ENTRY_GROWS_UP;
 2787         }
 2788 
 2789         vm_map_unlock(map);
 2790         return (rv);
 2791 }
 2792 
 2793 /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
 2794  * desired address is already mapped, or if we successfully grow
 2795  * the stack.  Also returns KERN_SUCCESS if addr is outside the
 2796  * stack range (this is strange, but preserves compatibility with
 2797  * the grow function in vm_machdep.c).
 2798  */
 2799 int
 2800 vm_map_growstack(struct proc *p, vm_offset_t addr)
 2801 {
 2802         vm_map_entry_t next_entry, prev_entry;
 2803         vm_map_entry_t new_entry, stack_entry;
 2804         struct vmspace *vm = p->p_vmspace;
 2805         vm_map_t map = &vm->vm_map;
 2806         vm_offset_t end;
 2807         size_t grow_amount, max_grow;
 2808         rlim_t stacklim, vmemlim;
 2809         int is_procstack, rv;
 2810 
 2811 Retry:
 2812         PROC_LOCK(p);
 2813         stacklim = lim_cur(p, RLIMIT_STACK);
 2814         vmemlim = lim_cur(p, RLIMIT_VMEM);
 2815         PROC_UNLOCK(p);
 2816 
 2817         vm_map_lock_read(map);
 2818 
 2819         /* If addr is already in the entry range, no need to grow.*/
 2820         if (vm_map_lookup_entry(map, addr, &prev_entry)) {
 2821                 vm_map_unlock_read(map);
 2822                 return (KERN_SUCCESS);
 2823         }
 2824 
 2825         next_entry = prev_entry->next;
 2826         if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
 2827                 /*
 2828                  * This entry does not grow upwards. Since the address lies
 2829                  * beyond this entry, the next entry (if one exists) has to
 2830                  * be a downward growable entry. The entry list header is
 2831                  * never a growable entry, so it suffices to check the flags.
 2832                  */
 2833                 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
 2834                         vm_map_unlock_read(map);
 2835                         return (KERN_SUCCESS);
 2836                 }
 2837                 stack_entry = next_entry;
 2838         } else {
 2839                 /*
 2840                  * This entry grows upward. If the next entry does not at
 2841                  * least grow downwards, this is the entry we need to grow.
 2842                  * otherwise we have two possible choices and we have to
 2843                  * select one.
 2844                  */
 2845                 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
 2846                         /*
 2847                          * We have two choices; grow the entry closest to
 2848                          * the address to minimize the amount of growth.
 2849                          */
 2850                         if (addr - prev_entry->end <= next_entry->start - addr)
 2851                                 stack_entry = prev_entry;
 2852                         else
 2853                                 stack_entry = next_entry;
 2854                 } else
 2855                         stack_entry = prev_entry;
 2856         }
 2857 
 2858         if (stack_entry == next_entry) {
 2859                 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
 2860                 KASSERT(addr < stack_entry->start, ("foo"));
 2861                 end = (prev_entry != &map->header) ? prev_entry->end :
 2862                     stack_entry->start - stack_entry->avail_ssize;
 2863                 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
 2864                 max_grow = stack_entry->start - end;
 2865         } else {
 2866                 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
 2867                 KASSERT(addr >= stack_entry->end, ("foo"));
 2868                 end = (next_entry != &map->header) ? next_entry->start :
 2869                     stack_entry->end + stack_entry->avail_ssize;
 2870                 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
 2871                 max_grow = end - stack_entry->end;
 2872         }
 2873 
 2874         if (grow_amount > stack_entry->avail_ssize) {
 2875                 vm_map_unlock_read(map);
 2876                 return (KERN_NO_SPACE);
 2877         }
 2878 
 2879         /*
 2880          * If there is no longer enough space between the entries nogo, and
 2881          * adjust the available space.  Note: this  should only happen if the
 2882          * user has mapped into the stack area after the stack was created,
 2883          * and is probably an error.
 2884          *
 2885          * This also effectively destroys any guard page the user might have
 2886          * intended by limiting the stack size.
 2887          */
 2888         if (grow_amount > max_grow) {
 2889                 if (vm_map_lock_upgrade(map))
 2890                         goto Retry;
 2891 
 2892                 stack_entry->avail_ssize = max_grow;
 2893 
 2894                 vm_map_unlock(map);
 2895                 return (KERN_NO_SPACE);
 2896         }
 2897 
 2898         is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
 2899 
 2900         /*
 2901          * If this is the main process stack, see if we're over the stack
 2902          * limit.
 2903          */
 2904         if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
 2905                 vm_map_unlock_read(map);
 2906                 return (KERN_NO_SPACE);
 2907         }
 2908 
 2909         /* Round up the grow amount modulo SGROWSIZ */
 2910         grow_amount = roundup (grow_amount, sgrowsiz);
 2911         if (grow_amount > stack_entry->avail_ssize)
 2912                 grow_amount = stack_entry->avail_ssize;
 2913         if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
 2914                 grow_amount = stacklim - ctob(vm->vm_ssize);
 2915         }
 2916 
 2917         /* If we would blow our VMEM resource limit, no go */
 2918         if (map->size + grow_amount > vmemlim) {
 2919                 vm_map_unlock_read(map);
 2920                 return (KERN_NO_SPACE);
 2921         }
 2922 
 2923         if (vm_map_lock_upgrade(map))
 2924                 goto Retry;
 2925 
 2926         if (stack_entry == next_entry) {
 2927                 /*
 2928                  * Growing downward.
 2929                  */
 2930                 /* Get the preliminary new entry start value */
 2931                 addr = stack_entry->start - grow_amount;
 2932 
 2933                 /*
 2934                  * If this puts us into the previous entry, cut back our
 2935                  * growth to the available space. Also, see the note above.
 2936                  */
 2937                 if (addr < end) {
 2938                         stack_entry->avail_ssize = max_grow;
 2939                         addr = end;
 2940                 }
 2941 
 2942                 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
 2943                     p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
 2944 
 2945                 /* Adjust the available stack space by the amount we grew. */
 2946                 if (rv == KERN_SUCCESS) {
 2947                         if (prev_entry != &map->header)
 2948                                 vm_map_clip_end(map, prev_entry, addr);
 2949                         new_entry = prev_entry->next;
 2950                         KASSERT(new_entry == stack_entry->prev, ("foo"));
 2951                         KASSERT(new_entry->end == stack_entry->start, ("foo"));
 2952                         KASSERT(new_entry->start == addr, ("foo"));
 2953                         grow_amount = new_entry->end - new_entry->start;
 2954                         new_entry->avail_ssize = stack_entry->avail_ssize -
 2955                             grow_amount;
 2956                         stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
 2957                         new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
 2958                 }
 2959         } else {
 2960                 /*
 2961                  * Growing upward.
 2962                  */
 2963                 addr = stack_entry->end + grow_amount;
 2964 
 2965                 /*
 2966                  * If this puts us into the next entry, cut back our growth
 2967                  * to the available space. Also, see the note above.
 2968                  */
 2969                 if (addr > end) {
 2970                         stack_entry->avail_ssize = end - stack_entry->end;
 2971                         addr = end;
 2972                 }
 2973 
 2974                 grow_amount = addr - stack_entry->end;
 2975 
 2976                 /* Grow the underlying object if applicable. */
 2977                 if (stack_entry->object.vm_object == NULL ||
 2978                     vm_object_coalesce(stack_entry->object.vm_object,
 2979                     stack_entry->offset,
 2980                     (vm_size_t)(stack_entry->end - stack_entry->start),
 2981                     (vm_size_t)grow_amount)) {
 2982                         map->size += (addr - stack_entry->end);
 2983                         /* Update the current entry. */
 2984                         stack_entry->end = addr;
 2985                         stack_entry->avail_ssize -= grow_amount;
 2986                         vm_map_entry_resize_free(map, stack_entry);
 2987                         rv = KERN_SUCCESS;
 2988 
 2989                         if (next_entry != &map->header)
 2990                                 vm_map_clip_start(map, next_entry, addr);
 2991                 } else
 2992                         rv = KERN_FAILURE;
 2993         }
 2994 
 2995         if (rv == KERN_SUCCESS && is_procstack)
 2996                 vm->vm_ssize += btoc(grow_amount);
 2997 
 2998         vm_map_unlock(map);
 2999 
 3000         /*
 3001          * Heed the MAP_WIREFUTURE flag if it was set for this process.
 3002          */
 3003         if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
 3004                 vm_map_wire(map,
 3005                     (stack_entry == next_entry) ? addr : addr - grow_amount,
 3006                     (stack_entry == next_entry) ? stack_entry->start : addr,
 3007                     (p->p_flag & P_SYSTEM)
 3008                     ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
 3009                     : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
 3010         }
 3011 
 3012         return (rv);
 3013 }
 3014 
 3015 /*
 3016  * Unshare the specified VM space for exec.  If other processes are
 3017  * mapped to it, then create a new one.  The new vmspace is null.
 3018  */
 3019 int
 3020 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
 3021 {
 3022         struct vmspace *oldvmspace = p->p_vmspace;
 3023         struct vmspace *newvmspace;
 3024 
 3025         newvmspace = vmspace_alloc(minuser, maxuser);
 3026         if (newvmspace == NULL)
 3027                 return (ENOMEM);
 3028         newvmspace->vm_swrss = oldvmspace->vm_swrss;
 3029         /*
 3030          * This code is written like this for prototype purposes.  The
 3031          * goal is to avoid running down the vmspace here, but let the
 3032          * other process's that are still using the vmspace to finally
 3033          * run it down.  Even though there is little or no chance of blocking
 3034          * here, it is a good idea to keep this form for future mods.
 3035          */
 3036         PROC_VMSPACE_LOCK(p);
 3037         p->p_vmspace = newvmspace;
 3038         PROC_VMSPACE_UNLOCK(p);
 3039         if (p == curthread->td_proc)            /* XXXKSE ? */
 3040                 pmap_activate(curthread);
 3041         vmspace_free(oldvmspace);
 3042         return (0);
 3043 }
 3044 
 3045 /*
 3046  * Unshare the specified VM space for forcing COW.  This
 3047  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
 3048  */
 3049 int
 3050 vmspace_unshare(struct proc *p)
 3051 {
 3052         struct vmspace *oldvmspace = p->p_vmspace;
 3053         struct vmspace *newvmspace;
 3054 
 3055         if (oldvmspace->vm_refcnt == 1)
 3056                 return (0);
 3057         newvmspace = vmspace_fork(oldvmspace);
 3058         if (newvmspace == NULL)
 3059                 return (ENOMEM);
 3060         PROC_VMSPACE_LOCK(p);
 3061         p->p_vmspace = newvmspace;
 3062         PROC_VMSPACE_UNLOCK(p);
 3063         if (p == curthread->td_proc)            /* XXXKSE ? */
 3064                 pmap_activate(curthread);
 3065         vmspace_free(oldvmspace);
 3066         return (0);
 3067 }
 3068 
 3069 /*
 3070  *      vm_map_lookup:
 3071  *
 3072  *      Finds the VM object, offset, and
 3073  *      protection for a given virtual address in the
 3074  *      specified map, assuming a page fault of the
 3075  *      type specified.
 3076  *
 3077  *      Leaves the map in question locked for read; return
 3078  *      values are guaranteed until a vm_map_lookup_done
 3079  *      call is performed.  Note that the map argument
 3080  *      is in/out; the returned map must be used in
 3081  *      the call to vm_map_lookup_done.
 3082  *
 3083  *      A handle (out_entry) is returned for use in
 3084  *      vm_map_lookup_done, to make that fast.
 3085  *
 3086  *      If a lookup is requested with "write protection"
 3087  *      specified, the map may be changed to perform virtual
 3088  *      copying operations, although the data referenced will
 3089  *      remain the same.
 3090  */
 3091 int
 3092 vm_map_lookup(vm_map_t *var_map,                /* IN/OUT */
 3093               vm_offset_t vaddr,
 3094               vm_prot_t fault_typea,
 3095               vm_map_entry_t *out_entry,        /* OUT */
 3096               vm_object_t *object,              /* OUT */
 3097               vm_pindex_t *pindex,              /* OUT */
 3098               vm_prot_t *out_prot,              /* OUT */
 3099               boolean_t *wired)                 /* OUT */
 3100 {
 3101         vm_map_entry_t entry;
 3102         vm_map_t map = *var_map;
 3103         vm_prot_t prot;
 3104         vm_prot_t fault_type = fault_typea;
 3105 
 3106 RetryLookup:;
 3107         /*
 3108          * Lookup the faulting address.
 3109          */
 3110 
 3111         vm_map_lock_read(map);
 3112 #define RETURN(why) \
 3113                 { \
 3114                 vm_map_unlock_read(map); \
 3115                 return (why); \
 3116                 }
 3117 
 3118         /*
 3119          * If the map has an interesting hint, try it before calling full
 3120          * blown lookup routine.
 3121          */
 3122         entry = map->root;
 3123         *out_entry = entry;
 3124         if (entry == NULL ||
 3125             (vaddr < entry->start) || (vaddr >= entry->end)) {
 3126                 /*
 3127                  * Entry was either not a valid hint, or the vaddr was not
 3128                  * contained in the entry, so do a full lookup.
 3129                  */
 3130                 if (!vm_map_lookup_entry(map, vaddr, out_entry))
 3131                         RETURN(KERN_INVALID_ADDRESS);
 3132 
 3133                 entry = *out_entry;
 3134         }
 3135 
 3136         /*
 3137          * Handle submaps.
 3138          */
 3139         if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
 3140                 vm_map_t old_map = map;
 3141 
 3142                 *var_map = map = entry->object.sub_map;
 3143                 vm_map_unlock_read(old_map);
 3144                 goto RetryLookup;
 3145         }
 3146 
 3147         /*
 3148          * Check whether this task is allowed to have this page.
 3149          * Note the special case for MAP_ENTRY_COW
 3150          * pages with an override.  This is to implement a forced
 3151          * COW for debuggers.
 3152          */
 3153         if (fault_type & VM_PROT_OVERRIDE_WRITE)
 3154                 prot = entry->max_protection;
 3155         else
 3156                 prot = entry->protection;
 3157         fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
 3158         if ((fault_type & prot) != fault_type) {
 3159                         RETURN(KERN_PROTECTION_FAILURE);
 3160         }
 3161         if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
 3162             (entry->eflags & MAP_ENTRY_COW) &&
 3163             (fault_type & VM_PROT_WRITE) &&
 3164             (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
 3165                 RETURN(KERN_PROTECTION_FAILURE);
 3166         }
 3167 
 3168         /*
 3169          * If this page is not pageable, we have to get it for all possible
 3170          * accesses.
 3171          */
 3172         *wired = (entry->wired_count != 0);
 3173         if (*wired)
 3174                 prot = fault_type = entry->protection;
 3175 
 3176         /*
 3177          * If the entry was copy-on-write, we either ...
 3178          */
 3179         if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
 3180                 /*
 3181                  * If we want to write the page, we may as well handle that
 3182                  * now since we've got the map locked.
 3183                  *
 3184                  * If we don't need to write the page, we just demote the
 3185                  * permissions allowed.
 3186                  */
 3187                 if (fault_type & VM_PROT_WRITE) {
 3188                         /*
 3189                          * Make a new object, and place it in the object
 3190                          * chain.  Note that no new references have appeared
 3191                          * -- one just moved from the map to the new
 3192                          * object.
 3193                          */
 3194                         if (vm_map_lock_upgrade(map))
 3195                                 goto RetryLookup;
 3196 
 3197                         vm_object_shadow(
 3198                             &entry->object.vm_object,
 3199                             &entry->offset,
 3200                             atop(entry->end - entry->start));
 3201                         entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
 3202 
 3203                         vm_map_lock_downgrade(map);
 3204                 } else {
 3205                         /*
 3206                          * We're attempting to read a copy-on-write page --
 3207                          * don't allow writes.
 3208                          */
 3209                         prot &= ~VM_PROT_WRITE;
 3210                 }
 3211         }
 3212 
 3213         /*
 3214          * Create an object if necessary.
 3215          */
 3216         if (entry->object.vm_object == NULL &&
 3217             !map->system_map) {
 3218                 if (vm_map_lock_upgrade(map))
 3219                         goto RetryLookup;
 3220                 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
 3221                     atop(entry->end - entry->start));
 3222                 entry->offset = 0;
 3223                 vm_map_lock_downgrade(map);
 3224         }
 3225 
 3226         /*
 3227          * Return the object/offset from this entry.  If the entry was
 3228          * copy-on-write or empty, it has been fixed up.
 3229          */
 3230         *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
 3231         *object = entry->object.vm_object;
 3232 
 3233         *out_prot = prot;
 3234         return (KERN_SUCCESS);
 3235 
 3236 #undef  RETURN
 3237 }
 3238 
 3239 /*
 3240  *      vm_map_lookup_locked:
 3241  *
 3242  *      Lookup the faulting address.  A version of vm_map_lookup that returns 
 3243  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
 3244  */
 3245 int
 3246 vm_map_lookup_locked(vm_map_t *var_map,         /* IN/OUT */
 3247                      vm_offset_t vaddr,
 3248                      vm_prot_t fault_typea,
 3249                      vm_map_entry_t *out_entry, /* OUT */
 3250                      vm_object_t *object,       /* OUT */
 3251                      vm_pindex_t *pindex,       /* OUT */
 3252                      vm_prot_t *out_prot,       /* OUT */
 3253                      boolean_t *wired)          /* OUT */
 3254 {
 3255         vm_map_entry_t entry;
 3256         vm_map_t map = *var_map;
 3257         vm_prot_t prot;
 3258         vm_prot_t fault_type = fault_typea;
 3259 
 3260         /*
 3261          * If the map has an interesting hint, try it before calling full
 3262          * blown lookup routine.
 3263          */
 3264         entry = map->root;
 3265         *out_entry = entry;
 3266         if (entry == NULL ||
 3267             (vaddr < entry->start) || (vaddr >= entry->end)) {
 3268                 /*
 3269                  * Entry was either not a valid hint, or the vaddr was not
 3270                  * contained in the entry, so do a full lookup.
 3271                  */
 3272                 if (!vm_map_lookup_entry(map, vaddr, out_entry))
 3273                         return (KERN_INVALID_ADDRESS);
 3274 
 3275                 entry = *out_entry;
 3276         }
 3277 
 3278         /*
 3279          * Fail if the entry refers to a submap.
 3280          */
 3281         if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 3282                 return (KERN_FAILURE);
 3283 
 3284         /*
 3285          * Check whether this task is allowed to have this page.
 3286          * Note the special case for MAP_ENTRY_COW
 3287          * pages with an override.  This is to implement a forced
 3288          * COW for debuggers.
 3289          */
 3290         if (fault_type & VM_PROT_OVERRIDE_WRITE)
 3291                 prot = entry->max_protection;
 3292         else
 3293                 prot = entry->protection;
 3294         fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
 3295         if ((fault_type & prot) != fault_type)
 3296                 return (KERN_PROTECTION_FAILURE);
 3297         if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
 3298             (entry->eflags & MAP_ENTRY_COW) &&
 3299             (fault_type & VM_PROT_WRITE) &&
 3300             (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
 3301                 return (KERN_PROTECTION_FAILURE);
 3302 
 3303         /*
 3304          * If this page is not pageable, we have to get it for all possible
 3305          * accesses.
 3306          */
 3307         *wired = (entry->wired_count != 0);
 3308         if (*wired)
 3309                 prot = fault_type = entry->protection;
 3310 
 3311         if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
 3312                 /*
 3313                  * Fail if the entry was copy-on-write for a write fault.
 3314                  */
 3315                 if (fault_type & VM_PROT_WRITE)
 3316                         return (KERN_FAILURE);
 3317                 /*
 3318                  * We're attempting to read a copy-on-write page --
 3319                  * don't allow writes.
 3320                  */
 3321                 prot &= ~VM_PROT_WRITE;
 3322         }
 3323 
 3324         /*
 3325          * Fail if an object should be created.
 3326          */
 3327         if (entry->object.vm_object == NULL && !map->system_map)
 3328                 return (KERN_FAILURE);
 3329 
 3330         /*
 3331          * Return the object/offset from this entry.  If the entry was
 3332          * copy-on-write or empty, it has been fixed up.
 3333          */
 3334         *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
 3335         *object = entry->object.vm_object;
 3336 
 3337         *out_prot = prot;
 3338         return (KERN_SUCCESS);
 3339 }
 3340 
 3341 /*
 3342  *      vm_map_lookup_done:
 3343  *
 3344  *      Releases locks acquired by a vm_map_lookup
 3345  *      (according to the handle returned by that lookup).
 3346  */
 3347 void
 3348 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
 3349 {
 3350         /*
 3351          * Unlock the main-level map
 3352          */
 3353         vm_map_unlock_read(map);
 3354 }
 3355 
 3356 #include "opt_ddb.h"
 3357 #ifdef DDB
 3358 #include <sys/kernel.h>
 3359 
 3360 #include <ddb/ddb.h>
 3361 
 3362 /*
 3363  *      vm_map_print:   [ debug ]
 3364  */
 3365 DB_SHOW_COMMAND(map, vm_map_print)
 3366 {
 3367         static int nlines;
 3368         /* XXX convert args. */
 3369         vm_map_t map = (vm_map_t)addr;
 3370         boolean_t full = have_addr;
 3371 
 3372         vm_map_entry_t entry;
 3373 
 3374         db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
 3375             (void *)map,
 3376             (void *)map->pmap, map->nentries, map->timestamp);
 3377         nlines++;
 3378 
 3379         if (!full && db_indent)
 3380                 return;
 3381 
 3382         db_indent += 2;
 3383         for (entry = map->header.next; entry != &map->header;
 3384             entry = entry->next) {
 3385                 db_iprintf("map entry %p: start=%p, end=%p\n",
 3386                     (void *)entry, (void *)entry->start, (void *)entry->end);
 3387                 nlines++;
 3388                 {
 3389                         static char *inheritance_name[4] =
 3390                         {"share", "copy", "none", "donate_copy"};
 3391 
 3392                         db_iprintf(" prot=%x/%x/%s",
 3393                             entry->protection,
 3394                             entry->max_protection,
 3395                             inheritance_name[(int)(unsigned char)entry->inheritance]);
 3396                         if (entry->wired_count != 0)
 3397                                 db_printf(", wired");
 3398                 }
 3399                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
 3400                         db_printf(", share=%p, offset=0x%jx\n",
 3401                             (void *)entry->object.sub_map,
 3402                             (uintmax_t)entry->offset);
 3403                         nlines++;
 3404                         if ((entry->prev == &map->header) ||
 3405                             (entry->prev->object.sub_map !=
 3406                                 entry->object.sub_map)) {
 3407                                 db_indent += 2;
 3408                                 vm_map_print((db_expr_t)(intptr_t)
 3409                                              entry->object.sub_map,
 3410                                              full, 0, (char *)0);
 3411                                 db_indent -= 2;
 3412                         }
 3413                 } else {
 3414                         db_printf(", object=%p, offset=0x%jx",
 3415                             (void *)entry->object.vm_object,
 3416                             (uintmax_t)entry->offset);
 3417                         if (entry->eflags & MAP_ENTRY_COW)
 3418                                 db_printf(", copy (%s)",
 3419                                     (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
 3420                         db_printf("\n");
 3421                         nlines++;
 3422 
 3423                         if ((entry->prev == &map->header) ||
 3424                             (entry->prev->object.vm_object !=
 3425                                 entry->object.vm_object)) {
 3426                                 db_indent += 2;
 3427                                 vm_object_print((db_expr_t)(intptr_t)
 3428                                                 entry->object.vm_object,
 3429                                                 full, 0, (char *)0);
 3430                                 nlines += 4;
 3431                                 db_indent -= 2;
 3432                         }
 3433                 }
 3434         }
 3435         db_indent -= 2;
 3436         if (db_indent == 0)
 3437                 nlines = 0;
 3438 }
 3439 
 3440 
 3441 DB_SHOW_COMMAND(procvm, procvm)
 3442 {
 3443         struct proc *p;
 3444 
 3445         if (have_addr) {
 3446                 p = (struct proc *) addr;
 3447         } else {
 3448                 p = curproc;
 3449         }
 3450 
 3451         db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
 3452             (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
 3453             (void *)vmspace_pmap(p->p_vmspace));
 3454 
 3455         vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
 3456 }
 3457 
 3458 #endif /* DDB */

Cache object: e1eed8452fd83e7bb7d153da126a0c62


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