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: releng/7.3/sys/vm/vm_map.c 203820 2010-02-13 11:24:11Z kib $");
   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 static struct mtx map_sleep_mtx;
  119 static uma_zone_t mapentzone;
  120 static uma_zone_t kmapentzone;
  121 static uma_zone_t mapzone;
  122 static uma_zone_t vmspace_zone;
  123 static struct vm_object kmapentobj;
  124 static int vmspace_zinit(void *mem, int size, int flags);
  125 static void vmspace_zfini(void *mem, int size);
  126 static int vm_map_zinit(void *mem, int ize, int flags);
  127 static void vm_map_zfini(void *mem, int size);
  128 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
  129 
  130 #ifdef INVARIANTS
  131 static void vm_map_zdtor(void *mem, int size, void *arg);
  132 static void vmspace_zdtor(void *mem, int size, void *arg);
  133 #endif
  134 
  135 /* 
  136  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
  137  * stable.
  138  */
  139 #define PROC_VMSPACE_LOCK(p) do { } while (0)
  140 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
  141 
  142 /*
  143  *      VM_MAP_RANGE_CHECK:     [ internal use only ]
  144  *
  145  *      Asserts that the starting and ending region
  146  *      addresses fall within the valid range of the map.
  147  */
  148 #define VM_MAP_RANGE_CHECK(map, start, end)             \
  149                 {                                       \
  150                 if (start < vm_map_min(map))            \
  151                         start = vm_map_min(map);        \
  152                 if (end > vm_map_max(map))              \
  153                         end = vm_map_max(map);          \
  154                 if (start > end)                        \
  155                         start = end;                    \
  156                 }
  157 
  158 /*
  159  *      vm_map_startup:
  160  *
  161  *      Initialize the vm_map module.  Must be called before
  162  *      any other vm_map routines.
  163  *
  164  *      Map and entry structures are allocated from the general
  165  *      purpose memory pool with some exceptions:
  166  *
  167  *      - The kernel map and kmem submap are allocated statically.
  168  *      - Kernel map entries are allocated out of a static pool.
  169  *
  170  *      These restrictions are necessary since malloc() uses the
  171  *      maps and requires map entries.
  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 - VM_MIN_KERNEL_ADDRESS) / 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 start, vm_size_t length, vm_prot_t prot,
 1176     vm_prot_t max, int cow)
 1177 {
 1178         vm_offset_t end;
 1179         int result;
 1180 
 1181         vm_map_lock(map);
 1182         end = start + length;
 1183         VM_MAP_RANGE_CHECK(map, start, end);
 1184         (void) vm_map_delete(map, start, end);
 1185         result = vm_map_insert(map, object, offset, start, end, prot,
 1186             max, cow);
 1187         vm_map_unlock(map);
 1188         return (result);
 1189 }
 1190 
 1191 /*
 1192  *      vm_map_find finds an unallocated region in the target address
 1193  *      map with the given length.  The search is defined to be
 1194  *      first-fit from the specified address; the region found is
 1195  *      returned in the same parameter.
 1196  *
 1197  *      If object is non-NULL, ref count must be bumped by caller
 1198  *      prior to making call to account for the new entry.
 1199  */
 1200 int
 1201 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
 1202             vm_offset_t *addr,  /* IN/OUT */
 1203             vm_size_t length, int find_space, vm_prot_t prot,
 1204             vm_prot_t max, int cow)
 1205 {
 1206         vm_offset_t start;
 1207         int result;
 1208 
 1209         start = *addr;
 1210         vm_map_lock(map);
 1211         do {
 1212                 if (find_space != VMFS_NO_SPACE) {
 1213                         if (vm_map_findspace(map, start, length, addr)) {
 1214                                 vm_map_unlock(map);
 1215                                 return (KERN_NO_SPACE);
 1216                         }
 1217                         if (find_space == VMFS_ALIGNED_SPACE)
 1218                                 pmap_align_superpage(object, offset, addr,
 1219                                     length);
 1220                         start = *addr;
 1221                 }
 1222                 result = vm_map_insert(map, object, offset, start, start +
 1223                     length, prot, max, cow);
 1224         } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE);
 1225         vm_map_unlock(map);
 1226         return (result);
 1227 }
 1228 
 1229 /*
 1230  *      vm_map_simplify_entry:
 1231  *
 1232  *      Simplify the given map entry by merging with either neighbor.  This
 1233  *      routine also has the ability to merge with both neighbors.
 1234  *
 1235  *      The map must be locked.
 1236  *
 1237  *      This routine guarentees that the passed entry remains valid (though
 1238  *      possibly extended).  When merging, this routine may delete one or
 1239  *      both neighbors.
 1240  */
 1241 void
 1242 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
 1243 {
 1244         vm_map_entry_t next, prev;
 1245         vm_size_t prevsize, esize;
 1246 
 1247         if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
 1248                 return;
 1249 
 1250         prev = entry->prev;
 1251         if (prev != &map->header) {
 1252                 prevsize = prev->end - prev->start;
 1253                 if ( (prev->end == entry->start) &&
 1254                      (prev->object.vm_object == entry->object.vm_object) &&
 1255                      (!prev->object.vm_object ||
 1256                         (prev->offset + prevsize == entry->offset)) &&
 1257                      (prev->eflags == entry->eflags) &&
 1258                      (prev->protection == entry->protection) &&
 1259                      (prev->max_protection == entry->max_protection) &&
 1260                      (prev->inheritance == entry->inheritance) &&
 1261                      (prev->wired_count == entry->wired_count)) {
 1262                         vm_map_entry_unlink(map, prev);
 1263                         entry->start = prev->start;
 1264                         entry->offset = prev->offset;
 1265                         if (entry->prev != &map->header)
 1266                                 vm_map_entry_resize_free(map, entry->prev);
 1267                         if (prev->object.vm_object)
 1268                                 vm_object_deallocate(prev->object.vm_object);
 1269                         vm_map_entry_dispose(map, prev);
 1270                 }
 1271         }
 1272 
 1273         next = entry->next;
 1274         if (next != &map->header) {
 1275                 esize = entry->end - entry->start;
 1276                 if ((entry->end == next->start) &&
 1277                     (next->object.vm_object == entry->object.vm_object) &&
 1278                      (!entry->object.vm_object ||
 1279                         (entry->offset + esize == next->offset)) &&
 1280                     (next->eflags == entry->eflags) &&
 1281                     (next->protection == entry->protection) &&
 1282                     (next->max_protection == entry->max_protection) &&
 1283                     (next->inheritance == entry->inheritance) &&
 1284                     (next->wired_count == entry->wired_count)) {
 1285                         vm_map_entry_unlink(map, next);
 1286                         entry->end = next->end;
 1287                         vm_map_entry_resize_free(map, entry);
 1288                         if (next->object.vm_object)
 1289                                 vm_object_deallocate(next->object.vm_object);
 1290                         vm_map_entry_dispose(map, next);
 1291                 }
 1292         }
 1293 }
 1294 /*
 1295  *      vm_map_clip_start:      [ internal use only ]
 1296  *
 1297  *      Asserts that the given entry begins at or after
 1298  *      the specified address; if necessary,
 1299  *      it splits the entry into two.
 1300  */
 1301 #define vm_map_clip_start(map, entry, startaddr) \
 1302 { \
 1303         if (startaddr > entry->start) \
 1304                 _vm_map_clip_start(map, entry, startaddr); \
 1305 }
 1306 
 1307 /*
 1308  *      This routine is called only when it is known that
 1309  *      the entry must be split.
 1310  */
 1311 static void
 1312 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
 1313 {
 1314         vm_map_entry_t new_entry;
 1315 
 1316         /*
 1317          * Split off the front portion -- note that we must insert the new
 1318          * entry BEFORE this one, so that this entry has the specified
 1319          * starting address.
 1320          */
 1321         vm_map_simplify_entry(map, entry);
 1322 
 1323         /*
 1324          * If there is no object backing this entry, we might as well create
 1325          * one now.  If we defer it, an object can get created after the map
 1326          * is clipped, and individual objects will be created for the split-up
 1327          * map.  This is a bit of a hack, but is also about the best place to
 1328          * put this improvement.
 1329          */
 1330         if (entry->object.vm_object == NULL && !map->system_map) {
 1331                 vm_object_t object;
 1332                 object = vm_object_allocate(OBJT_DEFAULT,
 1333                                 atop(entry->end - entry->start));
 1334                 entry->object.vm_object = object;
 1335                 entry->offset = 0;
 1336         }
 1337 
 1338         new_entry = vm_map_entry_create(map);
 1339         *new_entry = *entry;
 1340 
 1341         new_entry->end = start;
 1342         entry->offset += (start - entry->start);
 1343         entry->start = start;
 1344 
 1345         vm_map_entry_link(map, entry->prev, new_entry);
 1346 
 1347         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
 1348                 vm_object_reference(new_entry->object.vm_object);
 1349         }
 1350 }
 1351 
 1352 /*
 1353  *      vm_map_clip_end:        [ internal use only ]
 1354  *
 1355  *      Asserts that the given entry ends at or before
 1356  *      the specified address; if necessary,
 1357  *      it splits the entry into two.
 1358  */
 1359 #define vm_map_clip_end(map, entry, endaddr) \
 1360 { \
 1361         if ((endaddr) < (entry->end)) \
 1362                 _vm_map_clip_end((map), (entry), (endaddr)); \
 1363 }
 1364 
 1365 /*
 1366  *      This routine is called only when it is known that
 1367  *      the entry must be split.
 1368  */
 1369 static void
 1370 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
 1371 {
 1372         vm_map_entry_t new_entry;
 1373 
 1374         /*
 1375          * If there is no object backing this entry, we might as well create
 1376          * one now.  If we defer it, an object can get created after the map
 1377          * is clipped, and individual objects will be created for the split-up
 1378          * map.  This is a bit of a hack, but is also about the best place to
 1379          * put this improvement.
 1380          */
 1381         if (entry->object.vm_object == NULL && !map->system_map) {
 1382                 vm_object_t object;
 1383                 object = vm_object_allocate(OBJT_DEFAULT,
 1384                                 atop(entry->end - entry->start));
 1385                 entry->object.vm_object = object;
 1386                 entry->offset = 0;
 1387         }
 1388 
 1389         /*
 1390          * Create a new entry and insert it AFTER the specified entry
 1391          */
 1392         new_entry = vm_map_entry_create(map);
 1393         *new_entry = *entry;
 1394 
 1395         new_entry->start = entry->end = end;
 1396         new_entry->offset += (end - entry->start);
 1397 
 1398         vm_map_entry_link(map, entry, new_entry);
 1399 
 1400         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
 1401                 vm_object_reference(new_entry->object.vm_object);
 1402         }
 1403 }
 1404 
 1405 /*
 1406  *      vm_map_submap:          [ kernel use only ]
 1407  *
 1408  *      Mark the given range as handled by a subordinate map.
 1409  *
 1410  *      This range must have been created with vm_map_find,
 1411  *      and no other operations may have been performed on this
 1412  *      range prior to calling vm_map_submap.
 1413  *
 1414  *      Only a limited number of operations can be performed
 1415  *      within this rage after calling vm_map_submap:
 1416  *              vm_fault
 1417  *      [Don't try vm_map_copy!]
 1418  *
 1419  *      To remove a submapping, one must first remove the
 1420  *      range from the superior map, and then destroy the
 1421  *      submap (if desired).  [Better yet, don't try it.]
 1422  */
 1423 int
 1424 vm_map_submap(
 1425         vm_map_t map,
 1426         vm_offset_t start,
 1427         vm_offset_t end,
 1428         vm_map_t submap)
 1429 {
 1430         vm_map_entry_t entry;
 1431         int result = KERN_INVALID_ARGUMENT;
 1432 
 1433         vm_map_lock(map);
 1434 
 1435         VM_MAP_RANGE_CHECK(map, start, end);
 1436 
 1437         if (vm_map_lookup_entry(map, start, &entry)) {
 1438                 vm_map_clip_start(map, entry, start);
 1439         } else
 1440                 entry = entry->next;
 1441 
 1442         vm_map_clip_end(map, entry, end);
 1443 
 1444         if ((entry->start == start) && (entry->end == end) &&
 1445             ((entry->eflags & MAP_ENTRY_COW) == 0) &&
 1446             (entry->object.vm_object == NULL)) {
 1447                 entry->object.sub_map = submap;
 1448                 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
 1449                 result = KERN_SUCCESS;
 1450         }
 1451         vm_map_unlock(map);
 1452 
 1453         return (result);
 1454 }
 1455 
 1456 /*
 1457  * The maximum number of pages to map
 1458  */
 1459 #define MAX_INIT_PT     96
 1460 
 1461 /*
 1462  *      vm_map_pmap_enter:
 1463  *
 1464  *      Preload read-only mappings for the given object's resident pages into
 1465  *      the given map.  This eliminates the soft faults on process startup and
 1466  *      immediately after an mmap(2).  Unless the given flags include
 1467  *      MAP_PREFAULT_MADVISE, cached pages are not reactivated and mapped.
 1468  */
 1469 void
 1470 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
 1471     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
 1472 {
 1473         vm_offset_t start;
 1474         vm_page_t p, p_start;
 1475         vm_pindex_t psize, tmpidx;
 1476         boolean_t are_queues_locked;
 1477 
 1478         if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
 1479                 return;
 1480         VM_OBJECT_LOCK(object);
 1481         if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
 1482                 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
 1483                 goto unlock_return;
 1484         }
 1485 
 1486         psize = atop(size);
 1487 
 1488         if ((flags & MAP_PREFAULT_PARTIAL) && psize > MAX_INIT_PT &&
 1489             object->resident_page_count > MAX_INIT_PT)
 1490                 goto unlock_return;
 1491 
 1492         if (psize + pindex > object->size) {
 1493                 if (object->size < pindex)
 1494                         goto unlock_return;
 1495                 psize = object->size - pindex;
 1496         }
 1497 
 1498         are_queues_locked = FALSE;
 1499         start = 0;
 1500         p_start = NULL;
 1501 
 1502         if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
 1503                 if (p->pindex < pindex) {
 1504                         p = vm_page_splay(pindex, object->root);
 1505                         if ((object->root = p)->pindex < pindex)
 1506                                 p = TAILQ_NEXT(p, listq);
 1507                 }
 1508         }
 1509         /*
 1510          * Assert: the variable p is either (1) the page with the
 1511          * least pindex greater than or equal to the parameter pindex
 1512          * or (2) NULL.
 1513          */
 1514         for (;
 1515              p != NULL && (tmpidx = p->pindex - pindex) < psize;
 1516              p = TAILQ_NEXT(p, listq)) {
 1517                 /*
 1518                  * don't allow an madvise to blow away our really
 1519                  * free pages allocating pv entries.
 1520                  */
 1521                 if ((flags & MAP_PREFAULT_MADVISE) &&
 1522                     cnt.v_free_count < cnt.v_free_reserved) {
 1523                         psize = tmpidx;
 1524                         break;
 1525                 }
 1526                 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
 1527                     (p->busy == 0)) {
 1528                         if (p_start == NULL) {
 1529                                 start = addr + ptoa(tmpidx);
 1530                                 p_start = p;
 1531                         }
 1532                 } else if (p_start != NULL) {
 1533                         if (!are_queues_locked) {
 1534                                 are_queues_locked = TRUE;
 1535                                 vm_page_lock_queues();
 1536                         }
 1537                         pmap_enter_object(map->pmap, start, addr +
 1538                             ptoa(tmpidx), p_start, prot);
 1539                         p_start = NULL;
 1540                 }
 1541         }
 1542         if (p_start != NULL) {
 1543                 if (!are_queues_locked) {
 1544                         are_queues_locked = TRUE;
 1545                         vm_page_lock_queues();
 1546                 }
 1547                 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
 1548                     p_start, prot);
 1549         }
 1550         if (are_queues_locked)
 1551                 vm_page_unlock_queues();
 1552 unlock_return:
 1553         VM_OBJECT_UNLOCK(object);
 1554 }
 1555 
 1556 /*
 1557  *      vm_map_protect:
 1558  *
 1559  *      Sets the protection of the specified address
 1560  *      region in the target map.  If "set_max" is
 1561  *      specified, the maximum protection is to be set;
 1562  *      otherwise, only the current protection is affected.
 1563  */
 1564 int
 1565 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
 1566                vm_prot_t new_prot, boolean_t set_max)
 1567 {
 1568         vm_map_entry_t current;
 1569         vm_map_entry_t entry;
 1570 
 1571         vm_map_lock(map);
 1572 
 1573         VM_MAP_RANGE_CHECK(map, start, end);
 1574 
 1575         if (vm_map_lookup_entry(map, start, &entry)) {
 1576                 vm_map_clip_start(map, entry, start);
 1577         } else {
 1578                 entry = entry->next;
 1579         }
 1580 
 1581         /*
 1582          * Make a first pass to check for protection violations.
 1583          */
 1584         current = entry;
 1585         while ((current != &map->header) && (current->start < end)) {
 1586                 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
 1587                         vm_map_unlock(map);
 1588                         return (KERN_INVALID_ARGUMENT);
 1589                 }
 1590                 if ((new_prot & current->max_protection) != new_prot) {
 1591                         vm_map_unlock(map);
 1592                         return (KERN_PROTECTION_FAILURE);
 1593                 }
 1594                 current = current->next;
 1595         }
 1596 
 1597         /*
 1598          * Go back and fix up protections. [Note that clipping is not
 1599          * necessary the second time.]
 1600          */
 1601         current = entry;
 1602         while ((current != &map->header) && (current->start < end)) {
 1603                 vm_prot_t old_prot;
 1604 
 1605                 vm_map_clip_end(map, current, end);
 1606 
 1607                 old_prot = current->protection;
 1608                 if (set_max)
 1609                         current->protection =
 1610                             (current->max_protection = new_prot) &
 1611                             old_prot;
 1612                 else
 1613                         current->protection = new_prot;
 1614 
 1615                 /*
 1616                  * Update physical map if necessary. Worry about copy-on-write
 1617                  * here -- CHECK THIS XXX
 1618                  */
 1619                 if (current->protection != old_prot) {
 1620 #define MASK(entry)     (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
 1621                                                         VM_PROT_ALL)
 1622                         pmap_protect(map->pmap, current->start,
 1623                             current->end,
 1624                             current->protection & MASK(current));
 1625 #undef  MASK
 1626                 }
 1627                 vm_map_simplify_entry(map, current);
 1628                 current = current->next;
 1629         }
 1630         vm_map_unlock(map);
 1631         return (KERN_SUCCESS);
 1632 }
 1633 
 1634 /*
 1635  *      vm_map_madvise:
 1636  *
 1637  *      This routine traverses a processes map handling the madvise
 1638  *      system call.  Advisories are classified as either those effecting
 1639  *      the vm_map_entry structure, or those effecting the underlying
 1640  *      objects.
 1641  */
 1642 int
 1643 vm_map_madvise(
 1644         vm_map_t map,
 1645         vm_offset_t start,
 1646         vm_offset_t end,
 1647         int behav)
 1648 {
 1649         vm_map_entry_t current, entry;
 1650         int modify_map = 0;
 1651 
 1652         /*
 1653          * Some madvise calls directly modify the vm_map_entry, in which case
 1654          * we need to use an exclusive lock on the map and we need to perform
 1655          * various clipping operations.  Otherwise we only need a read-lock
 1656          * on the map.
 1657          */
 1658         switch(behav) {
 1659         case MADV_NORMAL:
 1660         case MADV_SEQUENTIAL:
 1661         case MADV_RANDOM:
 1662         case MADV_NOSYNC:
 1663         case MADV_AUTOSYNC:
 1664         case MADV_NOCORE:
 1665         case MADV_CORE:
 1666                 modify_map = 1;
 1667                 vm_map_lock(map);
 1668                 break;
 1669         case MADV_WILLNEED:
 1670         case MADV_DONTNEED:
 1671         case MADV_FREE:
 1672                 vm_map_lock_read(map);
 1673                 break;
 1674         default:
 1675                 return (KERN_INVALID_ARGUMENT);
 1676         }
 1677 
 1678         /*
 1679          * Locate starting entry and clip if necessary.
 1680          */
 1681         VM_MAP_RANGE_CHECK(map, start, end);
 1682 
 1683         if (vm_map_lookup_entry(map, start, &entry)) {
 1684                 if (modify_map)
 1685                         vm_map_clip_start(map, entry, start);
 1686         } else {
 1687                 entry = entry->next;
 1688         }
 1689 
 1690         if (modify_map) {
 1691                 /*
 1692                  * madvise behaviors that are implemented in the vm_map_entry.
 1693                  *
 1694                  * We clip the vm_map_entry so that behavioral changes are
 1695                  * limited to the specified address range.
 1696                  */
 1697                 for (current = entry;
 1698                      (current != &map->header) && (current->start < end);
 1699                      current = current->next
 1700                 ) {
 1701                         if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
 1702                                 continue;
 1703 
 1704                         vm_map_clip_end(map, current, end);
 1705 
 1706                         switch (behav) {
 1707                         case MADV_NORMAL:
 1708                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
 1709                                 break;
 1710                         case MADV_SEQUENTIAL:
 1711                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
 1712                                 break;
 1713                         case MADV_RANDOM:
 1714                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
 1715                                 break;
 1716                         case MADV_NOSYNC:
 1717                                 current->eflags |= MAP_ENTRY_NOSYNC;
 1718                                 break;
 1719                         case MADV_AUTOSYNC:
 1720                                 current->eflags &= ~MAP_ENTRY_NOSYNC;
 1721                                 break;
 1722                         case MADV_NOCORE:
 1723                                 current->eflags |= MAP_ENTRY_NOCOREDUMP;
 1724                                 break;
 1725                         case MADV_CORE:
 1726                                 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
 1727                                 break;
 1728                         default:
 1729                                 break;
 1730                         }
 1731                         vm_map_simplify_entry(map, current);
 1732                 }
 1733                 vm_map_unlock(map);
 1734         } else {
 1735                 vm_pindex_t pindex;
 1736                 int count;
 1737 
 1738                 /*
 1739                  * madvise behaviors that are implemented in the underlying
 1740                  * vm_object.
 1741                  *
 1742                  * Since we don't clip the vm_map_entry, we have to clip
 1743                  * the vm_object pindex and count.
 1744                  */
 1745                 for (current = entry;
 1746                      (current != &map->header) && (current->start < end);
 1747                      current = current->next
 1748                 ) {
 1749                         vm_offset_t useStart;
 1750 
 1751                         if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
 1752                                 continue;
 1753 
 1754                         pindex = OFF_TO_IDX(current->offset);
 1755                         count = atop(current->end - current->start);
 1756                         useStart = current->start;
 1757 
 1758                         if (current->start < start) {
 1759                                 pindex += atop(start - current->start);
 1760                                 count -= atop(start - current->start);
 1761                                 useStart = start;
 1762                         }
 1763                         if (current->end > end)
 1764                                 count -= atop(current->end - end);
 1765 
 1766                         if (count <= 0)
 1767                                 continue;
 1768 
 1769                         vm_object_madvise(current->object.vm_object,
 1770                                           pindex, count, behav);
 1771                         if (behav == MADV_WILLNEED) {
 1772                                 vm_map_pmap_enter(map,
 1773                                     useStart,
 1774                                     current->protection,
 1775                                     current->object.vm_object,
 1776                                     pindex,
 1777                                     (count << PAGE_SHIFT),
 1778                                     MAP_PREFAULT_MADVISE
 1779                                 );
 1780                         }
 1781                 }
 1782                 vm_map_unlock_read(map);
 1783         }
 1784         return (0);
 1785 }
 1786 
 1787 
 1788 /*
 1789  *      vm_map_inherit:
 1790  *
 1791  *      Sets the inheritance of the specified address
 1792  *      range in the target map.  Inheritance
 1793  *      affects how the map will be shared with
 1794  *      child maps at the time of vm_map_fork.
 1795  */
 1796 int
 1797 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
 1798                vm_inherit_t new_inheritance)
 1799 {
 1800         vm_map_entry_t entry;
 1801         vm_map_entry_t temp_entry;
 1802 
 1803         switch (new_inheritance) {
 1804         case VM_INHERIT_NONE:
 1805         case VM_INHERIT_COPY:
 1806         case VM_INHERIT_SHARE:
 1807                 break;
 1808         default:
 1809                 return (KERN_INVALID_ARGUMENT);
 1810         }
 1811         vm_map_lock(map);
 1812         VM_MAP_RANGE_CHECK(map, start, end);
 1813         if (vm_map_lookup_entry(map, start, &temp_entry)) {
 1814                 entry = temp_entry;
 1815                 vm_map_clip_start(map, entry, start);
 1816         } else
 1817                 entry = temp_entry->next;
 1818         while ((entry != &map->header) && (entry->start < end)) {
 1819                 vm_map_clip_end(map, entry, end);
 1820                 entry->inheritance = new_inheritance;
 1821                 vm_map_simplify_entry(map, entry);
 1822                 entry = entry->next;
 1823         }
 1824         vm_map_unlock(map);
 1825         return (KERN_SUCCESS);
 1826 }
 1827 
 1828 /*
 1829  *      vm_map_unwire:
 1830  *
 1831  *      Implements both kernel and user unwiring.
 1832  */
 1833 int
 1834 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
 1835     int flags)
 1836 {
 1837         vm_map_entry_t entry, first_entry, tmp_entry;
 1838         vm_offset_t saved_start;
 1839         unsigned int last_timestamp;
 1840         int rv;
 1841         boolean_t need_wakeup, result, user_unwire;
 1842 
 1843         user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
 1844         vm_map_lock(map);
 1845         VM_MAP_RANGE_CHECK(map, start, end);
 1846         if (!vm_map_lookup_entry(map, start, &first_entry)) {
 1847                 if (flags & VM_MAP_WIRE_HOLESOK)
 1848                         first_entry = first_entry->next;
 1849                 else {
 1850                         vm_map_unlock(map);
 1851                         return (KERN_INVALID_ADDRESS);
 1852                 }
 1853         }
 1854         last_timestamp = map->timestamp;
 1855         entry = first_entry;
 1856         while (entry != &map->header && entry->start < end) {
 1857                 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
 1858                         /*
 1859                          * We have not yet clipped the entry.
 1860                          */
 1861                         saved_start = (start >= entry->start) ? start :
 1862                             entry->start;
 1863                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
 1864                         if (vm_map_unlock_and_wait(map, user_unwire)) {
 1865                                 /*
 1866                                  * Allow interruption of user unwiring?
 1867                                  */
 1868                         }
 1869                         vm_map_lock(map);
 1870                         if (last_timestamp+1 != map->timestamp) {
 1871                                 /*
 1872                                  * Look again for the entry because the map was
 1873                                  * modified while it was unlocked.
 1874                                  * Specifically, the entry may have been
 1875                                  * clipped, merged, or deleted.
 1876                                  */
 1877                                 if (!vm_map_lookup_entry(map, saved_start,
 1878                                     &tmp_entry)) {
 1879                                         if (flags & VM_MAP_WIRE_HOLESOK)
 1880                                                 tmp_entry = tmp_entry->next;
 1881                                         else {
 1882                                                 if (saved_start == start) {
 1883                                                         /*
 1884                                                          * First_entry has been deleted.
 1885                                                          */
 1886                                                         vm_map_unlock(map);
 1887                                                         return (KERN_INVALID_ADDRESS);
 1888                                                 }
 1889                                                 end = saved_start;
 1890                                                 rv = KERN_INVALID_ADDRESS;
 1891                                                 goto done;
 1892                                         }
 1893                                 }
 1894                                 if (entry == first_entry)
 1895                                         first_entry = tmp_entry;
 1896                                 else
 1897                                         first_entry = NULL;
 1898                                 entry = tmp_entry;
 1899                         }
 1900                         last_timestamp = map->timestamp;
 1901                         continue;
 1902                 }
 1903                 vm_map_clip_start(map, entry, start);
 1904                 vm_map_clip_end(map, entry, end);
 1905                 /*
 1906                  * Mark the entry in case the map lock is released.  (See
 1907                  * above.)
 1908                  */
 1909                 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
 1910                 /*
 1911                  * Check the map for holes in the specified region.
 1912                  * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
 1913                  */
 1914                 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
 1915                     (entry->end < end && (entry->next == &map->header ||
 1916                     entry->next->start > entry->end))) {
 1917                         end = entry->end;
 1918                         rv = KERN_INVALID_ADDRESS;
 1919                         goto done;
 1920                 }
 1921                 /*
 1922                  * If system unwiring, require that the entry is system wired.
 1923                  */
 1924                 if (!user_unwire &&
 1925                     vm_map_entry_system_wired_count(entry) == 0) {
 1926                         end = entry->end;
 1927                         rv = KERN_INVALID_ARGUMENT;
 1928                         goto done;
 1929                 }
 1930                 entry = entry->next;
 1931         }
 1932         rv = KERN_SUCCESS;
 1933 done:
 1934         need_wakeup = FALSE;
 1935         if (first_entry == NULL) {
 1936                 result = vm_map_lookup_entry(map, start, &first_entry);
 1937                 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
 1938                         first_entry = first_entry->next;
 1939                 else
 1940                         KASSERT(result, ("vm_map_unwire: lookup failed"));
 1941         }
 1942         entry = first_entry;
 1943         while (entry != &map->header && entry->start < end) {
 1944                 if (rv == KERN_SUCCESS && (!user_unwire ||
 1945                     (entry->eflags & MAP_ENTRY_USER_WIRED))) {
 1946                         if (user_unwire)
 1947                                 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
 1948                         entry->wired_count--;
 1949                         if (entry->wired_count == 0) {
 1950                                 /*
 1951                                  * Retain the map lock.
 1952                                  */
 1953                                 vm_fault_unwire(map, entry->start, entry->end,
 1954                                     entry->object.vm_object != NULL &&
 1955                                     (entry->object.vm_object->type == OBJT_DEVICE ||
 1956                                     entry->object.vm_object->type == OBJT_SG));
 1957                         }
 1958                 }
 1959                 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
 1960                         ("vm_map_unwire: in-transition flag missing"));
 1961                 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
 1962                 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
 1963                         entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
 1964                         need_wakeup = TRUE;
 1965                 }
 1966                 vm_map_simplify_entry(map, entry);
 1967                 entry = entry->next;
 1968         }
 1969         vm_map_unlock(map);
 1970         if (need_wakeup)
 1971                 vm_map_wakeup(map);
 1972         return (rv);
 1973 }
 1974 
 1975 /*
 1976  *      vm_map_wire:
 1977  *
 1978  *      Implements both kernel and user wiring.
 1979  */
 1980 int
 1981 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
 1982     int flags)
 1983 {
 1984         vm_map_entry_t entry, first_entry, tmp_entry;
 1985         vm_offset_t saved_end, saved_start;
 1986         unsigned int last_timestamp;
 1987         int rv;
 1988         boolean_t fictitious, need_wakeup, result, user_wire;
 1989 
 1990         user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
 1991         vm_map_lock(map);
 1992         VM_MAP_RANGE_CHECK(map, start, end);
 1993         if (!vm_map_lookup_entry(map, start, &first_entry)) {
 1994                 if (flags & VM_MAP_WIRE_HOLESOK)
 1995                         first_entry = first_entry->next;
 1996                 else {
 1997                         vm_map_unlock(map);
 1998                         return (KERN_INVALID_ADDRESS);
 1999                 }
 2000         }
 2001         last_timestamp = map->timestamp;
 2002         entry = first_entry;
 2003         while (entry != &map->header && entry->start < end) {
 2004                 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
 2005                         /*
 2006                          * We have not yet clipped the entry.
 2007                          */
 2008                         saved_start = (start >= entry->start) ? start :
 2009                             entry->start;
 2010                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
 2011                         if (vm_map_unlock_and_wait(map, user_wire)) {
 2012                                 /*
 2013                                  * Allow interruption of user wiring?
 2014                                  */
 2015                         }
 2016                         vm_map_lock(map);
 2017                         if (last_timestamp + 1 != map->timestamp) {
 2018                                 /*
 2019                                  * Look again for the entry because the map was
 2020                                  * modified while it was unlocked.
 2021                                  * Specifically, the entry may have been
 2022                                  * clipped, merged, or deleted.
 2023                                  */
 2024                                 if (!vm_map_lookup_entry(map, saved_start,
 2025                                     &tmp_entry)) {
 2026                                         if (flags & VM_MAP_WIRE_HOLESOK)
 2027                                                 tmp_entry = tmp_entry->next;
 2028                                         else {
 2029                                                 if (saved_start == start) {
 2030                                                         /*
 2031                                                          * first_entry has been deleted.
 2032                                                          */
 2033                                                         vm_map_unlock(map);
 2034                                                         return (KERN_INVALID_ADDRESS);
 2035                                                 }
 2036                                                 end = saved_start;
 2037                                                 rv = KERN_INVALID_ADDRESS;
 2038                                                 goto done;
 2039                                         }
 2040                                 }
 2041                                 if (entry == first_entry)
 2042                                         first_entry = tmp_entry;
 2043                                 else
 2044                                         first_entry = NULL;
 2045                                 entry = tmp_entry;
 2046                         }
 2047                         last_timestamp = map->timestamp;
 2048                         continue;
 2049                 }
 2050                 vm_map_clip_start(map, entry, start);
 2051                 vm_map_clip_end(map, entry, end);
 2052                 /*
 2053                  * Mark the entry in case the map lock is released.  (See
 2054                  * above.)
 2055                  */
 2056                 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
 2057                 /*
 2058                  *
 2059                  */
 2060                 if (entry->wired_count == 0) {
 2061                         if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
 2062                             == 0) {
 2063                                 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
 2064                                 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
 2065                                         end = entry->end;
 2066                                         rv = KERN_INVALID_ADDRESS;
 2067                                         goto done;
 2068                                 }
 2069                                 goto next_entry;
 2070                         }
 2071                         entry->wired_count++;
 2072                         saved_start = entry->start;
 2073                         saved_end = entry->end;
 2074                         fictitious = entry->object.vm_object != NULL &&
 2075                             (entry->object.vm_object->type == OBJT_DEVICE ||
 2076                             entry->object.vm_object->type == OBJT_SG);
 2077                         /*
 2078                          * Release the map lock, relying on the in-transition
 2079                          * mark.
 2080                          */
 2081                         vm_map_unlock(map);
 2082                         rv = vm_fault_wire(map, saved_start, saved_end,
 2083                             user_wire, fictitious);
 2084                         vm_map_lock(map);
 2085                         if (last_timestamp + 1 != map->timestamp) {
 2086                                 /*
 2087                                  * Look again for the entry because the map was
 2088                                  * modified while it was unlocked.  The entry
 2089                                  * may have been clipped, but NOT merged or
 2090                                  * deleted.
 2091                                  */
 2092                                 result = vm_map_lookup_entry(map, saved_start,
 2093                                     &tmp_entry);
 2094                                 KASSERT(result, ("vm_map_wire: lookup failed"));
 2095                                 if (entry == first_entry)
 2096                                         first_entry = tmp_entry;
 2097                                 else
 2098                                         first_entry = NULL;
 2099                                 entry = tmp_entry;
 2100                                 while (entry->end < saved_end) {
 2101                                         if (rv != KERN_SUCCESS) {
 2102                                                 KASSERT(entry->wired_count == 1,
 2103                                                     ("vm_map_wire: bad count"));
 2104                                                 entry->wired_count = -1;
 2105                                         }
 2106                                         entry = entry->next;
 2107                                 }
 2108                         }
 2109                         last_timestamp = map->timestamp;
 2110                         if (rv != KERN_SUCCESS) {
 2111                                 KASSERT(entry->wired_count == 1,
 2112                                     ("vm_map_wire: bad count"));
 2113                                 /*
 2114                                  * Assign an out-of-range value to represent
 2115                                  * the failure to wire this entry.
 2116                                  */
 2117                                 entry->wired_count = -1;
 2118                                 end = entry->end;
 2119                                 goto done;
 2120                         }
 2121                 } else if (!user_wire ||
 2122                            (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
 2123                         entry->wired_count++;
 2124                 }
 2125                 /*
 2126                  * Check the map for holes in the specified region.
 2127                  * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
 2128                  */
 2129         next_entry:
 2130                 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
 2131                     (entry->end < end && (entry->next == &map->header ||
 2132                     entry->next->start > entry->end))) {
 2133                         end = entry->end;
 2134                         rv = KERN_INVALID_ADDRESS;
 2135                         goto done;
 2136                 }
 2137                 entry = entry->next;
 2138         }
 2139         rv = KERN_SUCCESS;
 2140 done:
 2141         need_wakeup = FALSE;
 2142         if (first_entry == NULL) {
 2143                 result = vm_map_lookup_entry(map, start, &first_entry);
 2144                 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
 2145                         first_entry = first_entry->next;
 2146                 else
 2147                         KASSERT(result, ("vm_map_wire: lookup failed"));
 2148         }
 2149         entry = first_entry;
 2150         while (entry != &map->header && entry->start < end) {
 2151                 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
 2152                         goto next_entry_done;
 2153                 if (rv == KERN_SUCCESS) {
 2154                         if (user_wire)
 2155                                 entry->eflags |= MAP_ENTRY_USER_WIRED;
 2156                 } else if (entry->wired_count == -1) {
 2157                         /*
 2158                          * Wiring failed on this entry.  Thus, unwiring is
 2159                          * unnecessary.
 2160                          */
 2161                         entry->wired_count = 0;
 2162                 } else {
 2163                         if (!user_wire ||
 2164                             (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
 2165                                 entry->wired_count--;
 2166                         if (entry->wired_count == 0) {
 2167                                 /*
 2168                                  * Retain the map lock.
 2169                                  */
 2170                                 vm_fault_unwire(map, entry->start, entry->end,
 2171                                     entry->object.vm_object != NULL &&
 2172                                     (entry->object.vm_object->type == OBJT_DEVICE ||
 2173                                     entry->object.vm_object->type == OBJT_SG));
 2174                         }
 2175                 }
 2176         next_entry_done:
 2177                 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
 2178                         ("vm_map_wire: in-transition flag missing"));
 2179                 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
 2180                 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
 2181                         entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
 2182                         need_wakeup = TRUE;
 2183                 }
 2184                 vm_map_simplify_entry(map, entry);
 2185                 entry = entry->next;
 2186         }
 2187         vm_map_unlock(map);
 2188         if (need_wakeup)
 2189                 vm_map_wakeup(map);
 2190         return (rv);
 2191 }
 2192 
 2193 /*
 2194  * vm_map_sync
 2195  *
 2196  * Push any dirty cached pages in the address range to their pager.
 2197  * If syncio is TRUE, dirty pages are written synchronously.
 2198  * If invalidate is TRUE, any cached pages are freed as well.
 2199  *
 2200  * If the size of the region from start to end is zero, we are
 2201  * supposed to flush all modified pages within the region containing
 2202  * start.  Unfortunately, a region can be split or coalesced with
 2203  * neighboring regions, making it difficult to determine what the
 2204  * original region was.  Therefore, we approximate this requirement by
 2205  * flushing the current region containing start.
 2206  *
 2207  * Returns an error if any part of the specified range is not mapped.
 2208  */
 2209 int
 2210 vm_map_sync(
 2211         vm_map_t map,
 2212         vm_offset_t start,
 2213         vm_offset_t end,
 2214         boolean_t syncio,
 2215         boolean_t invalidate)
 2216 {
 2217         vm_map_entry_t current;
 2218         vm_map_entry_t entry;
 2219         vm_size_t size;
 2220         vm_object_t object;
 2221         vm_ooffset_t offset;
 2222 
 2223         vm_map_lock_read(map);
 2224         VM_MAP_RANGE_CHECK(map, start, end);
 2225         if (!vm_map_lookup_entry(map, start, &entry)) {
 2226                 vm_map_unlock_read(map);
 2227                 return (KERN_INVALID_ADDRESS);
 2228         } else if (start == end) {
 2229                 start = entry->start;
 2230                 end = entry->end;
 2231         }
 2232         /*
 2233          * Make a first pass to check for user-wired memory and holes.
 2234          */
 2235         for (current = entry; current != &map->header && current->start < end;
 2236             current = current->next) {
 2237                 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
 2238                         vm_map_unlock_read(map);
 2239                         return (KERN_INVALID_ARGUMENT);
 2240                 }
 2241                 if (end > current->end &&
 2242                     (current->next == &map->header ||
 2243                         current->end != current->next->start)) {
 2244                         vm_map_unlock_read(map);
 2245                         return (KERN_INVALID_ADDRESS);
 2246                 }
 2247         }
 2248 
 2249         if (invalidate)
 2250                 pmap_remove(map->pmap, start, end);
 2251 
 2252         /*
 2253          * Make a second pass, cleaning/uncaching pages from the indicated
 2254          * objects as we go.
 2255          */
 2256         for (current = entry; current != &map->header && current->start < end;
 2257             current = current->next) {
 2258                 offset = current->offset + (start - current->start);
 2259                 size = (end <= current->end ? end : current->end) - start;
 2260                 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
 2261                         vm_map_t smap;
 2262                         vm_map_entry_t tentry;
 2263                         vm_size_t tsize;
 2264 
 2265                         smap = current->object.sub_map;
 2266                         vm_map_lock_read(smap);
 2267                         (void) vm_map_lookup_entry(smap, offset, &tentry);
 2268                         tsize = tentry->end - offset;
 2269                         if (tsize < size)
 2270                                 size = tsize;
 2271                         object = tentry->object.vm_object;
 2272                         offset = tentry->offset + (offset - tentry->start);
 2273                         vm_map_unlock_read(smap);
 2274                 } else {
 2275                         object = current->object.vm_object;
 2276                 }
 2277                 vm_object_sync(object, offset, size, syncio, invalidate);
 2278                 start += size;
 2279         }
 2280 
 2281         vm_map_unlock_read(map);
 2282         return (KERN_SUCCESS);
 2283 }
 2284 
 2285 /*
 2286  *      vm_map_entry_unwire:    [ internal use only ]
 2287  *
 2288  *      Make the region specified by this entry pageable.
 2289  *
 2290  *      The map in question should be locked.
 2291  *      [This is the reason for this routine's existence.]
 2292  */
 2293 static void
 2294 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
 2295 {
 2296         vm_fault_unwire(map, entry->start, entry->end,
 2297             entry->object.vm_object != NULL &&
 2298             (entry->object.vm_object->type == OBJT_DEVICE ||
 2299             entry->object.vm_object->type == OBJT_SG));
 2300         entry->wired_count = 0;
 2301 }
 2302 
 2303 /*
 2304  *      vm_map_entry_delete:    [ internal use only ]
 2305  *
 2306  *      Deallocate the given entry from the target map.
 2307  */
 2308 static void
 2309 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
 2310 {
 2311         vm_object_t object;
 2312         vm_pindex_t offidxstart, offidxend, count;
 2313 
 2314         vm_map_entry_unlink(map, entry);
 2315         map->size -= entry->end - entry->start;
 2316 
 2317         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
 2318             (object = entry->object.vm_object) != NULL) {
 2319                 count = OFF_TO_IDX(entry->end - entry->start);
 2320                 offidxstart = OFF_TO_IDX(entry->offset);
 2321                 offidxend = offidxstart + count;
 2322                 VM_OBJECT_LOCK(object);
 2323                 if (object->ref_count != 1 &&
 2324                     ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
 2325                     object == kernel_object || object == kmem_object)) {
 2326                         vm_object_collapse(object);
 2327                         vm_object_page_remove(object, offidxstart, offidxend, FALSE);
 2328                         if (object->type == OBJT_SWAP)
 2329                                 swap_pager_freespace(object, offidxstart, count);
 2330                         if (offidxend >= object->size &&
 2331                             offidxstart < object->size)
 2332                                 object->size = offidxstart;
 2333                 }
 2334                 VM_OBJECT_UNLOCK(object);
 2335                 vm_object_deallocate(object);
 2336         }
 2337 
 2338         vm_map_entry_dispose(map, entry);
 2339 }
 2340 
 2341 /*
 2342  *      vm_map_delete:  [ internal use only ]
 2343  *
 2344  *      Deallocates the given address range from the target
 2345  *      map.
 2346  */
 2347 int
 2348 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
 2349 {
 2350         vm_map_entry_t entry;
 2351         vm_map_entry_t first_entry;
 2352 
 2353         /*
 2354          * Find the start of the region, and clip it
 2355          */
 2356         if (!vm_map_lookup_entry(map, start, &first_entry))
 2357                 entry = first_entry->next;
 2358         else {
 2359                 entry = first_entry;
 2360                 vm_map_clip_start(map, entry, start);
 2361         }
 2362 
 2363         /*
 2364          * Step through all entries in this region
 2365          */
 2366         while ((entry != &map->header) && (entry->start < end)) {
 2367                 vm_map_entry_t next;
 2368 
 2369                 /*
 2370                  * Wait for wiring or unwiring of an entry to complete.
 2371                  * Also wait for any system wirings to disappear on
 2372                  * user maps.
 2373                  */
 2374                 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
 2375                     (vm_map_pmap(map) != kernel_pmap &&
 2376                     vm_map_entry_system_wired_count(entry) != 0)) {
 2377                         unsigned int last_timestamp;
 2378                         vm_offset_t saved_start;
 2379                         vm_map_entry_t tmp_entry;
 2380 
 2381                         saved_start = entry->start;
 2382                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
 2383                         last_timestamp = map->timestamp;
 2384                         (void) vm_map_unlock_and_wait(map, FALSE);
 2385                         vm_map_lock(map);
 2386                         if (last_timestamp + 1 != map->timestamp) {
 2387                                 /*
 2388                                  * Look again for the entry because the map was
 2389                                  * modified while it was unlocked.
 2390                                  * Specifically, the entry may have been
 2391                                  * clipped, merged, or deleted.
 2392                                  */
 2393                                 if (!vm_map_lookup_entry(map, saved_start,
 2394                                                          &tmp_entry))
 2395                                         entry = tmp_entry->next;
 2396                                 else {
 2397                                         entry = tmp_entry;
 2398                                         vm_map_clip_start(map, entry,
 2399                                                           saved_start);
 2400                                 }
 2401                         }
 2402                         continue;
 2403                 }
 2404                 vm_map_clip_end(map, entry, end);
 2405 
 2406                 next = entry->next;
 2407 
 2408                 /*
 2409                  * Unwire before removing addresses from the pmap; otherwise,
 2410                  * unwiring will put the entries back in the pmap.
 2411                  */
 2412                 if (entry->wired_count != 0) {
 2413                         vm_map_entry_unwire(map, entry);
 2414                 }
 2415 
 2416                 pmap_remove(map->pmap, entry->start, entry->end);
 2417 
 2418                 /*
 2419                  * Delete the entry (which may delete the object) only after
 2420                  * removing all pmap entries pointing to its pages.
 2421                  * (Otherwise, its page frames may be reallocated, and any
 2422                  * modify bits will be set in the wrong object!)
 2423                  */
 2424                 vm_map_entry_delete(map, entry);
 2425                 entry = next;
 2426         }
 2427         return (KERN_SUCCESS);
 2428 }
 2429 
 2430 /*
 2431  *      vm_map_remove:
 2432  *
 2433  *      Remove the given address range from the target map.
 2434  *      This is the exported form of vm_map_delete.
 2435  */
 2436 int
 2437 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
 2438 {
 2439         int result;
 2440 
 2441         vm_map_lock(map);
 2442         VM_MAP_RANGE_CHECK(map, start, end);
 2443         result = vm_map_delete(map, start, end);
 2444         vm_map_unlock(map);
 2445         return (result);
 2446 }
 2447 
 2448 /*
 2449  *      vm_map_check_protection:
 2450  *
 2451  *      Assert that the target map allows the specified privilege on the
 2452  *      entire address region given.  The entire region must be allocated.
 2453  *
 2454  *      WARNING!  This code does not and should not check whether the
 2455  *      contents of the region is accessible.  For example a smaller file
 2456  *      might be mapped into a larger address space.
 2457  *
 2458  *      NOTE!  This code is also called by munmap().
 2459  *
 2460  *      The map must be locked.  A read lock is sufficient.
 2461  */
 2462 boolean_t
 2463 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
 2464                         vm_prot_t protection)
 2465 {
 2466         vm_map_entry_t entry;
 2467         vm_map_entry_t tmp_entry;
 2468 
 2469         if (!vm_map_lookup_entry(map, start, &tmp_entry))
 2470                 return (FALSE);
 2471         entry = tmp_entry;
 2472 
 2473         while (start < end) {
 2474                 if (entry == &map->header)
 2475                         return (FALSE);
 2476                 /*
 2477                  * No holes allowed!
 2478                  */
 2479                 if (start < entry->start)
 2480                         return (FALSE);
 2481                 /*
 2482                  * Check protection associated with entry.
 2483                  */
 2484                 if ((entry->protection & protection) != protection)
 2485                         return (FALSE);
 2486                 /* go to next entry */
 2487                 start = entry->end;
 2488                 entry = entry->next;
 2489         }
 2490         return (TRUE);
 2491 }
 2492 
 2493 /*
 2494  *      vm_map_copy_entry:
 2495  *
 2496  *      Copies the contents of the source entry to the destination
 2497  *      entry.  The entries *must* be aligned properly.
 2498  */
 2499 static void
 2500 vm_map_copy_entry(
 2501         vm_map_t src_map,
 2502         vm_map_t dst_map,
 2503         vm_map_entry_t src_entry,
 2504         vm_map_entry_t dst_entry)
 2505 {
 2506         vm_object_t src_object;
 2507 
 2508         if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
 2509                 return;
 2510 
 2511         if (src_entry->wired_count == 0) {
 2512 
 2513                 /*
 2514                  * If the source entry is marked needs_copy, it is already
 2515                  * write-protected.
 2516                  */
 2517                 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
 2518                         pmap_protect(src_map->pmap,
 2519                             src_entry->start,
 2520                             src_entry->end,
 2521                             src_entry->protection & ~VM_PROT_WRITE);
 2522                 }
 2523 
 2524                 /*
 2525                  * Make a copy of the object.
 2526                  */
 2527                 if ((src_object = src_entry->object.vm_object) != NULL) {
 2528                         VM_OBJECT_LOCK(src_object);
 2529                         if ((src_object->handle == NULL) &&
 2530                                 (src_object->type == OBJT_DEFAULT ||
 2531                                  src_object->type == OBJT_SWAP)) {
 2532                                 vm_object_collapse(src_object);
 2533                                 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
 2534                                         vm_object_split(src_entry);
 2535                                         src_object = src_entry->object.vm_object;
 2536                                 }
 2537                         }
 2538                         vm_object_reference_locked(src_object);
 2539                         vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
 2540                         VM_OBJECT_UNLOCK(src_object);
 2541                         dst_entry->object.vm_object = src_object;
 2542                         src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
 2543                         dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
 2544                         dst_entry->offset = src_entry->offset;
 2545                 } else {
 2546                         dst_entry->object.vm_object = NULL;
 2547                         dst_entry->offset = 0;
 2548                 }
 2549 
 2550                 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
 2551                     dst_entry->end - dst_entry->start, src_entry->start);
 2552         } else {
 2553                 /*
 2554                  * Of course, wired down pages can't be set copy-on-write.
 2555                  * Cause wired pages to be copied into the new map by
 2556                  * simulating faults (the new pages are pageable)
 2557                  */
 2558                 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
 2559         }
 2560 }
 2561 
 2562 /*
 2563  * vmspace_map_entry_forked:
 2564  * Update the newly-forked vmspace each time a map entry is inherited
 2565  * or copied.  The values for vm_dsize and vm_tsize are approximate
 2566  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
 2567  */
 2568 static void
 2569 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
 2570     vm_map_entry_t entry)
 2571 {
 2572         vm_size_t entrysize;
 2573         vm_offset_t newend;
 2574 
 2575         entrysize = entry->end - entry->start;
 2576         vm2->vm_map.size += entrysize;
 2577         if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
 2578                 vm2->vm_ssize += btoc(entrysize);
 2579         } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
 2580             entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
 2581                 newend = MIN(entry->end,
 2582                     (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
 2583                 vm2->vm_dsize += btoc(newend - entry->start);
 2584         } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
 2585             entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
 2586                 newend = MIN(entry->end,
 2587                     (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
 2588                 vm2->vm_tsize += btoc(newend - entry->start);
 2589         }
 2590 }
 2591 
 2592 /*
 2593  * vmspace_fork:
 2594  * Create a new process vmspace structure and vm_map
 2595  * based on those of an existing process.  The new map
 2596  * is based on the old map, according to the inheritance
 2597  * values on the regions in that map.
 2598  *
 2599  * XXX It might be worth coalescing the entries added to the new vmspace.
 2600  *
 2601  * The source map must not be locked.
 2602  */
 2603 struct vmspace *
 2604 vmspace_fork(struct vmspace *vm1)
 2605 {
 2606         struct vmspace *vm2;
 2607         vm_map_t old_map = &vm1->vm_map;
 2608         vm_map_t new_map;
 2609         vm_map_entry_t old_entry;
 2610         vm_map_entry_t new_entry;
 2611         vm_object_t object;
 2612 
 2613         vm_map_lock(old_map);
 2614 
 2615         vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
 2616         if (vm2 == NULL)
 2617                 goto unlock_and_return;
 2618         vm2->vm_taddr = vm1->vm_taddr;
 2619         vm2->vm_daddr = vm1->vm_daddr;
 2620         vm2->vm_maxsaddr = vm1->vm_maxsaddr;
 2621         new_map = &vm2->vm_map; /* XXX */
 2622         new_map->timestamp = 1;
 2623 
 2624         old_entry = old_map->header.next;
 2625 
 2626         while (old_entry != &old_map->header) {
 2627                 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 2628                         panic("vm_map_fork: encountered a submap");
 2629 
 2630                 switch (old_entry->inheritance) {
 2631                 case VM_INHERIT_NONE:
 2632                         break;
 2633 
 2634                 case VM_INHERIT_SHARE:
 2635                         /*
 2636                          * Clone the entry, creating the shared object if necessary.
 2637                          */
 2638                         object = old_entry->object.vm_object;
 2639                         if (object == NULL) {
 2640                                 object = vm_object_allocate(OBJT_DEFAULT,
 2641                                         atop(old_entry->end - old_entry->start));
 2642                                 old_entry->object.vm_object = object;
 2643                                 old_entry->offset = 0;
 2644                         }
 2645 
 2646                         /*
 2647                          * Add the reference before calling vm_object_shadow
 2648                          * to insure that a shadow object is created.
 2649                          */
 2650                         vm_object_reference(object);
 2651                         if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
 2652                                 vm_object_shadow(&old_entry->object.vm_object,
 2653                                         &old_entry->offset,
 2654                                         atop(old_entry->end - old_entry->start));
 2655                                 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
 2656                                 /* Transfer the second reference too. */
 2657                                 vm_object_reference(
 2658                                     old_entry->object.vm_object);
 2659                                 vm_object_deallocate(object);
 2660                                 object = old_entry->object.vm_object;
 2661                         }
 2662                         VM_OBJECT_LOCK(object);
 2663                         vm_object_clear_flag(object, OBJ_ONEMAPPING);
 2664                         VM_OBJECT_UNLOCK(object);
 2665 
 2666                         /*
 2667                          * Clone the entry, referencing the shared object.
 2668                          */
 2669                         new_entry = vm_map_entry_create(new_map);
 2670                         *new_entry = *old_entry;
 2671                         new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
 2672                             MAP_ENTRY_IN_TRANSITION);
 2673                         new_entry->wired_count = 0;
 2674 
 2675                         /*
 2676                          * Insert the entry into the new map -- we know we're
 2677                          * inserting at the end of the new map.
 2678                          */
 2679                         vm_map_entry_link(new_map, new_map->header.prev,
 2680                             new_entry);
 2681                         vmspace_map_entry_forked(vm1, vm2, new_entry);
 2682 
 2683                         /*
 2684                          * Update the physical map
 2685                          */
 2686                         pmap_copy(new_map->pmap, old_map->pmap,
 2687                             new_entry->start,
 2688                             (old_entry->end - old_entry->start),
 2689                             old_entry->start);
 2690                         break;
 2691 
 2692                 case VM_INHERIT_COPY:
 2693                         /*
 2694                          * Clone the entry and link into the map.
 2695                          */
 2696                         new_entry = vm_map_entry_create(new_map);
 2697                         *new_entry = *old_entry;
 2698                         new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
 2699                             MAP_ENTRY_IN_TRANSITION);
 2700                         new_entry->wired_count = 0;
 2701                         new_entry->object.vm_object = NULL;
 2702                         vm_map_entry_link(new_map, new_map->header.prev,
 2703                             new_entry);
 2704                         vmspace_map_entry_forked(vm1, vm2, new_entry);
 2705                         vm_map_copy_entry(old_map, new_map, old_entry,
 2706                             new_entry);
 2707                         break;
 2708                 }
 2709                 old_entry = old_entry->next;
 2710         }
 2711 unlock_and_return:
 2712         vm_map_unlock(old_map);
 2713 
 2714         return (vm2);
 2715 }
 2716 
 2717 int
 2718 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
 2719     vm_prot_t prot, vm_prot_t max, int cow)
 2720 {
 2721         vm_map_entry_t new_entry, prev_entry;
 2722         vm_offset_t bot, top;
 2723         vm_size_t init_ssize;
 2724         int orient, rv;
 2725         rlim_t vmemlim;
 2726 
 2727         /*
 2728          * The stack orientation is piggybacked with the cow argument.
 2729          * Extract it into orient and mask the cow argument so that we
 2730          * don't pass it around further.
 2731          * NOTE: We explicitly allow bi-directional stacks.
 2732          */
 2733         orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
 2734         cow &= ~orient;
 2735         KASSERT(orient != 0, ("No stack grow direction"));
 2736 
 2737         if (addrbos < vm_map_min(map) ||
 2738             addrbos > vm_map_max(map) ||
 2739             addrbos + max_ssize < addrbos)
 2740                 return (KERN_NO_SPACE);
 2741 
 2742         init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
 2743 
 2744         PROC_LOCK(curthread->td_proc);
 2745         vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
 2746         PROC_UNLOCK(curthread->td_proc);
 2747 
 2748         vm_map_lock(map);
 2749 
 2750         /* If addr is already mapped, no go */
 2751         if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
 2752                 vm_map_unlock(map);
 2753                 return (KERN_NO_SPACE);
 2754         }
 2755 
 2756         /* If we would blow our VMEM resource limit, no go */
 2757         if (map->size + init_ssize > vmemlim) {
 2758                 vm_map_unlock(map);
 2759                 return (KERN_NO_SPACE);
 2760         }
 2761 
 2762         /*
 2763          * If we can't accomodate max_ssize in the current mapping, no go.
 2764          * However, we need to be aware that subsequent user mappings might
 2765          * map into the space we have reserved for stack, and currently this
 2766          * space is not protected.
 2767          *
 2768          * Hopefully we will at least detect this condition when we try to
 2769          * grow the stack.
 2770          */
 2771         if ((prev_entry->next != &map->header) &&
 2772             (prev_entry->next->start < addrbos + max_ssize)) {
 2773                 vm_map_unlock(map);
 2774                 return (KERN_NO_SPACE);
 2775         }
 2776 
 2777         /*
 2778          * We initially map a stack of only init_ssize.  We will grow as
 2779          * needed later.  Depending on the orientation of the stack (i.e.
 2780          * the grow direction) we either map at the top of the range, the
 2781          * bottom of the range or in the middle.
 2782          *
 2783          * Note: we would normally expect prot and max to be VM_PROT_ALL,
 2784          * and cow to be 0.  Possibly we should eliminate these as input
 2785          * parameters, and just pass these values here in the insert call.
 2786          */
 2787         if (orient == MAP_STACK_GROWS_DOWN)
 2788                 bot = addrbos + max_ssize - init_ssize;
 2789         else if (orient == MAP_STACK_GROWS_UP)
 2790                 bot = addrbos;
 2791         else
 2792                 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
 2793         top = bot + init_ssize;
 2794         rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
 2795 
 2796         /* Now set the avail_ssize amount. */
 2797         if (rv == KERN_SUCCESS) {
 2798                 if (prev_entry != &map->header)
 2799                         vm_map_clip_end(map, prev_entry, bot);
 2800                 new_entry = prev_entry->next;
 2801                 if (new_entry->end != top || new_entry->start != bot)
 2802                         panic("Bad entry start/end for new stack entry");
 2803 
 2804                 new_entry->avail_ssize = max_ssize - init_ssize;
 2805                 if (orient & MAP_STACK_GROWS_DOWN)
 2806                         new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
 2807                 if (orient & MAP_STACK_GROWS_UP)
 2808                         new_entry->eflags |= MAP_ENTRY_GROWS_UP;
 2809         }
 2810 
 2811         vm_map_unlock(map);
 2812         return (rv);
 2813 }
 2814 
 2815 /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
 2816  * desired address is already mapped, or if we successfully grow
 2817  * the stack.  Also returns KERN_SUCCESS if addr is outside the
 2818  * stack range (this is strange, but preserves compatibility with
 2819  * the grow function in vm_machdep.c).
 2820  */
 2821 int
 2822 vm_map_growstack(struct proc *p, vm_offset_t addr)
 2823 {
 2824         vm_map_entry_t next_entry, prev_entry;
 2825         vm_map_entry_t new_entry, stack_entry;
 2826         struct vmspace *vm = p->p_vmspace;
 2827         vm_map_t map = &vm->vm_map;
 2828         vm_offset_t end;
 2829         size_t grow_amount, max_grow;
 2830         rlim_t stacklim, vmemlim;
 2831         int is_procstack, rv;
 2832 
 2833 Retry:
 2834         PROC_LOCK(p);
 2835         stacklim = lim_cur(p, RLIMIT_STACK);
 2836         vmemlim = lim_cur(p, RLIMIT_VMEM);
 2837         PROC_UNLOCK(p);
 2838 
 2839         vm_map_lock_read(map);
 2840 
 2841         /* If addr is already in the entry range, no need to grow.*/
 2842         if (vm_map_lookup_entry(map, addr, &prev_entry)) {
 2843                 vm_map_unlock_read(map);
 2844                 return (KERN_SUCCESS);
 2845         }
 2846 
 2847         next_entry = prev_entry->next;
 2848         if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
 2849                 /*
 2850                  * This entry does not grow upwards. Since the address lies
 2851                  * beyond this entry, the next entry (if one exists) has to
 2852                  * be a downward growable entry. The entry list header is
 2853                  * never a growable entry, so it suffices to check the flags.
 2854                  */
 2855                 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
 2856                         vm_map_unlock_read(map);
 2857                         return (KERN_SUCCESS);
 2858                 }
 2859                 stack_entry = next_entry;
 2860         } else {
 2861                 /*
 2862                  * This entry grows upward. If the next entry does not at
 2863                  * least grow downwards, this is the entry we need to grow.
 2864                  * otherwise we have two possible choices and we have to
 2865                  * select one.
 2866                  */
 2867                 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
 2868                         /*
 2869                          * We have two choices; grow the entry closest to
 2870                          * the address to minimize the amount of growth.
 2871                          */
 2872                         if (addr - prev_entry->end <= next_entry->start - addr)
 2873                                 stack_entry = prev_entry;
 2874                         else
 2875                                 stack_entry = next_entry;
 2876                 } else
 2877                         stack_entry = prev_entry;
 2878         }
 2879 
 2880         if (stack_entry == next_entry) {
 2881                 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
 2882                 KASSERT(addr < stack_entry->start, ("foo"));
 2883                 end = (prev_entry != &map->header) ? prev_entry->end :
 2884                     stack_entry->start - stack_entry->avail_ssize;
 2885                 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
 2886                 max_grow = stack_entry->start - end;
 2887         } else {
 2888                 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
 2889                 KASSERT(addr >= stack_entry->end, ("foo"));
 2890                 end = (next_entry != &map->header) ? next_entry->start :
 2891                     stack_entry->end + stack_entry->avail_ssize;
 2892                 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
 2893                 max_grow = end - stack_entry->end;
 2894         }
 2895 
 2896         if (grow_amount > stack_entry->avail_ssize) {
 2897                 vm_map_unlock_read(map);
 2898                 return (KERN_NO_SPACE);
 2899         }
 2900 
 2901         /*
 2902          * If there is no longer enough space between the entries nogo, and
 2903          * adjust the available space.  Note: this  should only happen if the
 2904          * user has mapped into the stack area after the stack was created,
 2905          * and is probably an error.
 2906          *
 2907          * This also effectively destroys any guard page the user might have
 2908          * intended by limiting the stack size.
 2909          */
 2910         if (grow_amount > max_grow) {
 2911                 if (vm_map_lock_upgrade(map))
 2912                         goto Retry;
 2913 
 2914                 stack_entry->avail_ssize = max_grow;
 2915 
 2916                 vm_map_unlock(map);
 2917                 return (KERN_NO_SPACE);
 2918         }
 2919 
 2920         is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
 2921 
 2922         /*
 2923          * If this is the main process stack, see if we're over the stack
 2924          * limit.
 2925          */
 2926         if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
 2927                 vm_map_unlock_read(map);
 2928                 return (KERN_NO_SPACE);
 2929         }
 2930 
 2931         /* Round up the grow amount modulo SGROWSIZ */
 2932         grow_amount = roundup (grow_amount, sgrowsiz);
 2933         if (grow_amount > stack_entry->avail_ssize)
 2934                 grow_amount = stack_entry->avail_ssize;
 2935         if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
 2936                 grow_amount = stacklim - ctob(vm->vm_ssize);
 2937         }
 2938 
 2939         /* If we would blow our VMEM resource limit, no go */
 2940         if (map->size + grow_amount > vmemlim) {
 2941                 vm_map_unlock_read(map);
 2942                 return (KERN_NO_SPACE);
 2943         }
 2944 
 2945         if (vm_map_lock_upgrade(map))
 2946                 goto Retry;
 2947 
 2948         if (stack_entry == next_entry) {
 2949                 /*
 2950                  * Growing downward.
 2951                  */
 2952                 /* Get the preliminary new entry start value */
 2953                 addr = stack_entry->start - grow_amount;
 2954 
 2955                 /*
 2956                  * If this puts us into the previous entry, cut back our
 2957                  * growth to the available space. Also, see the note above.
 2958                  */
 2959                 if (addr < end) {
 2960                         stack_entry->avail_ssize = max_grow;
 2961                         addr = end;
 2962                 }
 2963 
 2964                 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
 2965                     p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
 2966 
 2967                 /* Adjust the available stack space by the amount we grew. */
 2968                 if (rv == KERN_SUCCESS) {
 2969                         if (prev_entry != &map->header)
 2970                                 vm_map_clip_end(map, prev_entry, addr);
 2971                         new_entry = prev_entry->next;
 2972                         KASSERT(new_entry == stack_entry->prev, ("foo"));
 2973                         KASSERT(new_entry->end == stack_entry->start, ("foo"));
 2974                         KASSERT(new_entry->start == addr, ("foo"));
 2975                         grow_amount = new_entry->end - new_entry->start;
 2976                         new_entry->avail_ssize = stack_entry->avail_ssize -
 2977                             grow_amount;
 2978                         stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
 2979                         new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
 2980                 }
 2981         } else {
 2982                 /*
 2983                  * Growing upward.
 2984                  */
 2985                 addr = stack_entry->end + grow_amount;
 2986 
 2987                 /*
 2988                  * If this puts us into the next entry, cut back our growth
 2989                  * to the available space. Also, see the note above.
 2990                  */
 2991                 if (addr > end) {
 2992                         stack_entry->avail_ssize = end - stack_entry->end;
 2993                         addr = end;
 2994                 }
 2995 
 2996                 grow_amount = addr - stack_entry->end;
 2997 
 2998                 /* Grow the underlying object if applicable. */
 2999                 if (stack_entry->object.vm_object == NULL ||
 3000                     vm_object_coalesce(stack_entry->object.vm_object,
 3001                     stack_entry->offset,
 3002                     (vm_size_t)(stack_entry->end - stack_entry->start),
 3003                     (vm_size_t)grow_amount)) {
 3004                         map->size += (addr - stack_entry->end);
 3005                         /* Update the current entry. */
 3006                         stack_entry->end = addr;
 3007                         stack_entry->avail_ssize -= grow_amount;
 3008                         vm_map_entry_resize_free(map, stack_entry);
 3009                         rv = KERN_SUCCESS;
 3010 
 3011                         if (next_entry != &map->header)
 3012                                 vm_map_clip_start(map, next_entry, addr);
 3013                 } else
 3014                         rv = KERN_FAILURE;
 3015         }
 3016 
 3017         if (rv == KERN_SUCCESS && is_procstack)
 3018                 vm->vm_ssize += btoc(grow_amount);
 3019 
 3020         vm_map_unlock(map);
 3021 
 3022         /*
 3023          * Heed the MAP_WIREFUTURE flag if it was set for this process.
 3024          */
 3025         if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
 3026                 vm_map_wire(map,
 3027                     (stack_entry == next_entry) ? addr : addr - grow_amount,
 3028                     (stack_entry == next_entry) ? stack_entry->start : addr,
 3029                     (p->p_flag & P_SYSTEM)
 3030                     ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
 3031                     : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
 3032         }
 3033 
 3034         return (rv);
 3035 }
 3036 
 3037 /*
 3038  * Unshare the specified VM space for exec.  If other processes are
 3039  * mapped to it, then create a new one.  The new vmspace is null.
 3040  */
 3041 int
 3042 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
 3043 {
 3044         struct vmspace *oldvmspace = p->p_vmspace;
 3045         struct vmspace *newvmspace;
 3046 
 3047         newvmspace = vmspace_alloc(minuser, maxuser);
 3048         if (newvmspace == NULL)
 3049                 return (ENOMEM);
 3050         newvmspace->vm_swrss = oldvmspace->vm_swrss;
 3051         /*
 3052          * This code is written like this for prototype purposes.  The
 3053          * goal is to avoid running down the vmspace here, but let the
 3054          * other process's that are still using the vmspace to finally
 3055          * run it down.  Even though there is little or no chance of blocking
 3056          * here, it is a good idea to keep this form for future mods.
 3057          */
 3058         PROC_VMSPACE_LOCK(p);
 3059         p->p_vmspace = newvmspace;
 3060         PROC_VMSPACE_UNLOCK(p);
 3061         if (p == curthread->td_proc)            /* XXXKSE ? */
 3062                 pmap_activate(curthread);
 3063         vmspace_free(oldvmspace);
 3064         return (0);
 3065 }
 3066 
 3067 /*
 3068  * Unshare the specified VM space for forcing COW.  This
 3069  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
 3070  */
 3071 int
 3072 vmspace_unshare(struct proc *p)
 3073 {
 3074         struct vmspace *oldvmspace = p->p_vmspace;
 3075         struct vmspace *newvmspace;
 3076 
 3077         if (oldvmspace->vm_refcnt == 1)
 3078                 return (0);
 3079         newvmspace = vmspace_fork(oldvmspace);
 3080         if (newvmspace == NULL)
 3081                 return (ENOMEM);
 3082         PROC_VMSPACE_LOCK(p);
 3083         p->p_vmspace = newvmspace;
 3084         PROC_VMSPACE_UNLOCK(p);
 3085         if (p == curthread->td_proc)            /* XXXKSE ? */
 3086                 pmap_activate(curthread);
 3087         vmspace_free(oldvmspace);
 3088         return (0);
 3089 }
 3090 
 3091 /*
 3092  *      vm_map_lookup:
 3093  *
 3094  *      Finds the VM object, offset, and
 3095  *      protection for a given virtual address in the
 3096  *      specified map, assuming a page fault of the
 3097  *      type specified.
 3098  *
 3099  *      Leaves the map in question locked for read; return
 3100  *      values are guaranteed until a vm_map_lookup_done
 3101  *      call is performed.  Note that the map argument
 3102  *      is in/out; the returned map must be used in
 3103  *      the call to vm_map_lookup_done.
 3104  *
 3105  *      A handle (out_entry) is returned for use in
 3106  *      vm_map_lookup_done, to make that fast.
 3107  *
 3108  *      If a lookup is requested with "write protection"
 3109  *      specified, the map may be changed to perform virtual
 3110  *      copying operations, although the data referenced will
 3111  *      remain the same.
 3112  */
 3113 int
 3114 vm_map_lookup(vm_map_t *var_map,                /* IN/OUT */
 3115               vm_offset_t vaddr,
 3116               vm_prot_t fault_typea,
 3117               vm_map_entry_t *out_entry,        /* OUT */
 3118               vm_object_t *object,              /* OUT */
 3119               vm_pindex_t *pindex,              /* OUT */
 3120               vm_prot_t *out_prot,              /* OUT */
 3121               boolean_t *wired)                 /* OUT */
 3122 {
 3123         vm_map_entry_t entry;
 3124         vm_map_t map = *var_map;
 3125         vm_prot_t prot;
 3126         vm_prot_t fault_type = fault_typea;
 3127 
 3128 RetryLookup:;
 3129         /*
 3130          * Lookup the faulting address.
 3131          */
 3132 
 3133         vm_map_lock_read(map);
 3134 #define RETURN(why) \
 3135                 { \
 3136                 vm_map_unlock_read(map); \
 3137                 return (why); \
 3138                 }
 3139 
 3140         /*
 3141          * If the map has an interesting hint, try it before calling full
 3142          * blown lookup routine.
 3143          */
 3144         entry = map->root;
 3145         *out_entry = entry;
 3146         if (entry == NULL ||
 3147             (vaddr < entry->start) || (vaddr >= entry->end)) {
 3148                 /*
 3149                  * Entry was either not a valid hint, or the vaddr was not
 3150                  * contained in the entry, so do a full lookup.
 3151                  */
 3152                 if (!vm_map_lookup_entry(map, vaddr, out_entry))
 3153                         RETURN(KERN_INVALID_ADDRESS);
 3154 
 3155                 entry = *out_entry;
 3156         }
 3157 
 3158         /*
 3159          * Handle submaps.
 3160          */
 3161         if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
 3162                 vm_map_t old_map = map;
 3163 
 3164                 *var_map = map = entry->object.sub_map;
 3165                 vm_map_unlock_read(old_map);
 3166                 goto RetryLookup;
 3167         }
 3168 
 3169         /*
 3170          * Check whether this task is allowed to have this page.
 3171          * Note the special case for MAP_ENTRY_COW
 3172          * pages with an override.  This is to implement a forced
 3173          * COW for debuggers.
 3174          */
 3175         if (fault_type & VM_PROT_OVERRIDE_WRITE)
 3176                 prot = entry->max_protection;
 3177         else
 3178                 prot = entry->protection;
 3179         fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
 3180         if ((fault_type & prot) != fault_type) {
 3181                         RETURN(KERN_PROTECTION_FAILURE);
 3182         }
 3183         if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
 3184             (entry->eflags & MAP_ENTRY_COW) &&
 3185             (fault_type & VM_PROT_WRITE) &&
 3186             (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
 3187                 RETURN(KERN_PROTECTION_FAILURE);
 3188         }
 3189 
 3190         /*
 3191          * If this page is not pageable, we have to get it for all possible
 3192          * accesses.
 3193          */
 3194         *wired = (entry->wired_count != 0);
 3195         if (*wired)
 3196                 prot = fault_type = entry->protection;
 3197 
 3198         /*
 3199          * If the entry was copy-on-write, we either ...
 3200          */
 3201         if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
 3202                 /*
 3203                  * If we want to write the page, we may as well handle that
 3204                  * now since we've got the map locked.
 3205                  *
 3206                  * If we don't need to write the page, we just demote the
 3207                  * permissions allowed.
 3208                  */
 3209                 if (fault_type & VM_PROT_WRITE) {
 3210                         /*
 3211                          * Make a new object, and place it in the object
 3212                          * chain.  Note that no new references have appeared
 3213                          * -- one just moved from the map to the new
 3214                          * object.
 3215                          */
 3216                         if (vm_map_lock_upgrade(map))
 3217                                 goto RetryLookup;
 3218 
 3219                         vm_object_shadow(
 3220                             &entry->object.vm_object,
 3221                             &entry->offset,
 3222                             atop(entry->end - entry->start));
 3223                         entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
 3224 
 3225                         vm_map_lock_downgrade(map);
 3226                 } else {
 3227                         /*
 3228                          * We're attempting to read a copy-on-write page --
 3229                          * don't allow writes.
 3230                          */
 3231                         prot &= ~VM_PROT_WRITE;
 3232                 }
 3233         }
 3234 
 3235         /*
 3236          * Create an object if necessary.
 3237          */
 3238         if (entry->object.vm_object == NULL &&
 3239             !map->system_map) {
 3240                 if (vm_map_lock_upgrade(map))
 3241                         goto RetryLookup;
 3242                 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
 3243                     atop(entry->end - entry->start));
 3244                 entry->offset = 0;
 3245                 vm_map_lock_downgrade(map);
 3246         }
 3247 
 3248         /*
 3249          * Return the object/offset from this entry.  If the entry was
 3250          * copy-on-write or empty, it has been fixed up.
 3251          */
 3252         *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
 3253         *object = entry->object.vm_object;
 3254 
 3255         *out_prot = prot;
 3256         return (KERN_SUCCESS);
 3257 
 3258 #undef  RETURN
 3259 }
 3260 
 3261 /*
 3262  *      vm_map_lookup_locked:
 3263  *
 3264  *      Lookup the faulting address.  A version of vm_map_lookup that returns 
 3265  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
 3266  */
 3267 int
 3268 vm_map_lookup_locked(vm_map_t *var_map,         /* IN/OUT */
 3269                      vm_offset_t vaddr,
 3270                      vm_prot_t fault_typea,
 3271                      vm_map_entry_t *out_entry, /* OUT */
 3272                      vm_object_t *object,       /* OUT */
 3273                      vm_pindex_t *pindex,       /* OUT */
 3274                      vm_prot_t *out_prot,       /* OUT */
 3275                      boolean_t *wired)          /* OUT */
 3276 {
 3277         vm_map_entry_t entry;
 3278         vm_map_t map = *var_map;
 3279         vm_prot_t prot;
 3280         vm_prot_t fault_type = fault_typea;
 3281 
 3282         /*
 3283          * If the map has an interesting hint, try it before calling full
 3284          * blown lookup routine.
 3285          */
 3286         entry = map->root;
 3287         *out_entry = entry;
 3288         if (entry == NULL ||
 3289             (vaddr < entry->start) || (vaddr >= entry->end)) {
 3290                 /*
 3291                  * Entry was either not a valid hint, or the vaddr was not
 3292                  * contained in the entry, so do a full lookup.
 3293                  */
 3294                 if (!vm_map_lookup_entry(map, vaddr, out_entry))
 3295                         return (KERN_INVALID_ADDRESS);
 3296 
 3297                 entry = *out_entry;
 3298         }
 3299 
 3300         /*
 3301          * Fail if the entry refers to a submap.
 3302          */
 3303         if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
 3304                 return (KERN_FAILURE);
 3305 
 3306         /*
 3307          * Check whether this task is allowed to have this page.
 3308          * Note the special case for MAP_ENTRY_COW
 3309          * pages with an override.  This is to implement a forced
 3310          * COW for debuggers.
 3311          */
 3312         if (fault_type & VM_PROT_OVERRIDE_WRITE)
 3313                 prot = entry->max_protection;
 3314         else
 3315                 prot = entry->protection;
 3316         fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
 3317         if ((fault_type & prot) != fault_type)
 3318                 return (KERN_PROTECTION_FAILURE);
 3319         if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
 3320             (entry->eflags & MAP_ENTRY_COW) &&
 3321             (fault_type & VM_PROT_WRITE) &&
 3322             (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
 3323                 return (KERN_PROTECTION_FAILURE);
 3324 
 3325         /*
 3326          * If this page is not pageable, we have to get it for all possible
 3327          * accesses.
 3328          */
 3329         *wired = (entry->wired_count != 0);
 3330         if (*wired)
 3331                 prot = fault_type = entry->protection;
 3332 
 3333         if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
 3334                 /*
 3335                  * Fail if the entry was copy-on-write for a write fault.
 3336                  */
 3337                 if (fault_type & VM_PROT_WRITE)
 3338                         return (KERN_FAILURE);
 3339                 /*
 3340                  * We're attempting to read a copy-on-write page --
 3341                  * don't allow writes.
 3342                  */
 3343                 prot &= ~VM_PROT_WRITE;
 3344         }
 3345 
 3346         /*
 3347          * Fail if an object should be created.
 3348          */
 3349         if (entry->object.vm_object == NULL && !map->system_map)
 3350                 return (KERN_FAILURE);
 3351 
 3352         /*
 3353          * Return the object/offset from this entry.  If the entry was
 3354          * copy-on-write or empty, it has been fixed up.
 3355          */
 3356         *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
 3357         *object = entry->object.vm_object;
 3358 
 3359         *out_prot = prot;
 3360         return (KERN_SUCCESS);
 3361 }
 3362 
 3363 /*
 3364  *      vm_map_lookup_done:
 3365  *
 3366  *      Releases locks acquired by a vm_map_lookup
 3367  *      (according to the handle returned by that lookup).
 3368  */
 3369 void
 3370 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
 3371 {
 3372         /*
 3373          * Unlock the main-level map
 3374          */
 3375         vm_map_unlock_read(map);
 3376 }
 3377 
 3378 #include "opt_ddb.h"
 3379 #ifdef DDB
 3380 #include <sys/kernel.h>
 3381 
 3382 #include <ddb/ddb.h>
 3383 
 3384 /*
 3385  *      vm_map_print:   [ debug ]
 3386  */
 3387 DB_SHOW_COMMAND(map, vm_map_print)
 3388 {
 3389         static int nlines;
 3390         /* XXX convert args. */
 3391         vm_map_t map = (vm_map_t)addr;
 3392         boolean_t full = have_addr;
 3393 
 3394         vm_map_entry_t entry;
 3395 
 3396         db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
 3397             (void *)map,
 3398             (void *)map->pmap, map->nentries, map->timestamp);
 3399         nlines++;
 3400 
 3401         if (!full && db_indent)
 3402                 return;
 3403 
 3404         db_indent += 2;
 3405         for (entry = map->header.next; entry != &map->header;
 3406             entry = entry->next) {
 3407                 db_iprintf("map entry %p: start=%p, end=%p\n",
 3408                     (void *)entry, (void *)entry->start, (void *)entry->end);
 3409                 nlines++;
 3410                 {
 3411                         static char *inheritance_name[4] =
 3412                         {"share", "copy", "none", "donate_copy"};
 3413 
 3414                         db_iprintf(" prot=%x/%x/%s",
 3415                             entry->protection,
 3416                             entry->max_protection,
 3417                             inheritance_name[(int)(unsigned char)entry->inheritance]);
 3418                         if (entry->wired_count != 0)
 3419                                 db_printf(", wired");
 3420                 }
 3421                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
 3422                         db_printf(", share=%p, offset=0x%jx\n",
 3423                             (void *)entry->object.sub_map,
 3424                             (uintmax_t)entry->offset);
 3425                         nlines++;
 3426                         if ((entry->prev == &map->header) ||
 3427                             (entry->prev->object.sub_map !=
 3428                                 entry->object.sub_map)) {
 3429                                 db_indent += 2;
 3430                                 vm_map_print((db_expr_t)(intptr_t)
 3431                                              entry->object.sub_map,
 3432                                              full, 0, (char *)0);
 3433                                 db_indent -= 2;
 3434                         }
 3435                 } else {
 3436                         db_printf(", object=%p, offset=0x%jx",
 3437                             (void *)entry->object.vm_object,
 3438                             (uintmax_t)entry->offset);
 3439                         if (entry->eflags & MAP_ENTRY_COW)
 3440                                 db_printf(", copy (%s)",
 3441                                     (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
 3442                         db_printf("\n");
 3443                         nlines++;
 3444 
 3445                         if ((entry->prev == &map->header) ||
 3446                             (entry->prev->object.vm_object !=
 3447                                 entry->object.vm_object)) {
 3448                                 db_indent += 2;
 3449                                 vm_object_print((db_expr_t)(intptr_t)
 3450                                                 entry->object.vm_object,
 3451                                                 full, 0, (char *)0);
 3452                                 nlines += 4;
 3453                                 db_indent -= 2;
 3454                         }
 3455                 }
 3456         }
 3457         db_indent -= 2;
 3458         if (db_indent == 0)
 3459                 nlines = 0;
 3460 }
 3461 
 3462 
 3463 DB_SHOW_COMMAND(procvm, procvm)
 3464 {
 3465         struct proc *p;
 3466 
 3467         if (have_addr) {
 3468                 p = (struct proc *) addr;
 3469         } else {
 3470                 p = curproc;
 3471         }
 3472 
 3473         db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
 3474             (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
 3475             (void *)vmspace_pmap(p->p_vmspace));
 3476 
 3477         vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
 3478 }
 3479 
 3480 #endif /* DDB */

Cache object: f008dbb77562a487bda7ab4c52ef9684


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