Home | History | Annotate | Line # | Download | only in uvm
uvm_map.c revision 1.106
      1 /*	$NetBSD: uvm_map.c,v 1.106 2001/09/15 20:36:46 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * Copyright (c) 1991, 1993, The Regents of the University of California.
      6  *
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * The Mach Operating System project at Carnegie-Mellon University.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by Charles D. Cranor,
     23  *      Washington University, the University of California, Berkeley and
     24  *      its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)vm_map.c    8.3 (Berkeley) 1/12/94
     42  * from: Id: uvm_map.c,v 1.1.2.27 1998/02/07 01:16:54 chs Exp
     43  *
     44  *
     45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     46  * All rights reserved.
     47  *
     48  * Permission to use, copy, modify and distribute this software and
     49  * its documentation is hereby granted, provided that both the copyright
     50  * notice and this permission notice appear in all copies of the
     51  * software, derivative works or modified versions, and any portions
     52  * thereof, and that both notices appear in supporting documentation.
     53  *
     54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     57  *
     58  * Carnegie Mellon requests users of this software to return to
     59  *
     60  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     61  *  School of Computer Science
     62  *  Carnegie Mellon University
     63  *  Pittsburgh PA 15213-3890
     64  *
     65  * any improvements or extensions that they make and grant Carnegie the
     66  * rights to redistribute these changes.
     67  */
     68 
     69 #include "opt_ddb.h"
     70 #include "opt_uvmhist.h"
     71 #include "opt_sysv.h"
     72 
     73 /*
     74  * uvm_map.c: uvm map operations
     75  */
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/mman.h>
     80 #include <sys/proc.h>
     81 #include <sys/malloc.h>
     82 #include <sys/pool.h>
     83 #include <sys/kernel.h>
     84 
     85 #ifdef SYSVSHM
     86 #include <sys/shm.h>
     87 #endif
     88 
     89 #define UVM_MAP
     90 #include <uvm/uvm.h>
     91 
     92 #ifdef DDB
     93 #include <uvm/uvm_ddb.h>
     94 #endif
     95 
     96 extern struct vm_map *pager_map;
     97 
     98 struct uvm_cnt uvm_map_call, map_backmerge, map_forwmerge;
     99 struct uvm_cnt uvm_mlk_call, uvm_mlk_hint;
    100 const char vmmapbsy[] = "vmmapbsy";
    101 
    102 /*
    103  * pool for vmspace structures.
    104  */
    105 
    106 struct pool uvm_vmspace_pool;
    107 
    108 /*
    109  * pool for dynamically-allocated map entries.
    110  */
    111 
    112 struct pool uvm_map_entry_pool;
    113 struct pool uvm_map_entry_kmem_pool;
    114 
    115 #ifdef PMAP_GROWKERNEL
    116 /*
    117  * This global represents the end of the kernel virtual address
    118  * space.  If we want to exceed this, we must grow the kernel
    119  * virtual address space dynamically.
    120  *
    121  * Note, this variable is locked by kernel_map's lock.
    122  */
    123 vaddr_t uvm_maxkaddr;
    124 #endif
    125 
    126 /*
    127  * macros
    128  */
    129 
    130 /*
    131  * uvm_map_entry_link: insert entry into a map
    132  *
    133  * => map must be locked
    134  */
    135 #define uvm_map_entry_link(map, after_where, entry) do { \
    136 	(map)->nentries++; \
    137 	(entry)->prev = (after_where); \
    138 	(entry)->next = (after_where)->next; \
    139 	(entry)->prev->next = (entry); \
    140 	(entry)->next->prev = (entry); \
    141 } while (0)
    142 
    143 /*
    144  * uvm_map_entry_unlink: remove entry from a map
    145  *
    146  * => map must be locked
    147  */
    148 #define uvm_map_entry_unlink(map, entry) do { \
    149 	(map)->nentries--; \
    150 	(entry)->next->prev = (entry)->prev; \
    151 	(entry)->prev->next = (entry)->next; \
    152 } while (0)
    153 
    154 /*
    155  * SAVE_HINT: saves the specified entry as the hint for future lookups.
    156  *
    157  * => map need not be locked (protected by hint_lock).
    158  */
    159 #define SAVE_HINT(map,check,value) do { \
    160 	simple_lock(&(map)->hint_lock); \
    161 	if ((map)->hint == (check)) \
    162 		(map)->hint = (value); \
    163 	simple_unlock(&(map)->hint_lock); \
    164 } while (0)
    165 
    166 /*
    167  * VM_MAP_RANGE_CHECK: check and correct range
    168  *
    169  * => map must at least be read locked
    170  */
    171 
    172 #define VM_MAP_RANGE_CHECK(map, start, end) do { \
    173 	if (start < vm_map_min(map)) 		\
    174 		start = vm_map_min(map);        \
    175 	if (end > vm_map_max(map))              \
    176 		end = vm_map_max(map);          \
    177 	if (start > end)                        \
    178 		start = end;                    \
    179 } while (0)
    180 
    181 /*
    182  * local prototypes
    183  */
    184 
    185 static struct vm_map_entry *uvm_mapent_alloc __P((struct vm_map *));
    186 static void uvm_mapent_copy __P((struct vm_map_entry *, struct vm_map_entry *));
    187 static void uvm_mapent_free __P((struct vm_map_entry *));
    188 static void uvm_map_entry_unwire __P((struct vm_map *, struct vm_map_entry *));
    189 static void uvm_map_reference_amap __P((struct vm_map_entry *, int));
    190 static void uvm_map_unreference_amap __P((struct vm_map_entry *, int));
    191 
    192 /*
    193  * local inlines
    194  */
    195 
    196 /*
    197  * uvm_mapent_alloc: allocate a map entry
    198  */
    199 
    200 static __inline struct vm_map_entry *
    201 uvm_mapent_alloc(map)
    202 	struct vm_map *map;
    203 {
    204 	struct vm_map_entry *me;
    205 	int s;
    206 	UVMHIST_FUNC("uvm_mapent_alloc"); UVMHIST_CALLED(maphist);
    207 
    208 	if (map->flags & VM_MAP_INTRSAFE || cold) {
    209 		s = splvm();
    210 		simple_lock(&uvm.kentry_lock);
    211 		me = uvm.kentry_free;
    212 		if (me) uvm.kentry_free = me->next;
    213 		simple_unlock(&uvm.kentry_lock);
    214 		splx(s);
    215 		if (me == NULL) {
    216 			panic("uvm_mapent_alloc: out of static map entries, "
    217 			      "check MAX_KMAPENT (currently %d)",
    218 			      MAX_KMAPENT);
    219 		}
    220 		me->flags = UVM_MAP_STATIC;
    221 	} else if (map == kernel_map) {
    222 		me = pool_get(&uvm_map_entry_kmem_pool, PR_WAITOK);
    223 		me->flags = UVM_MAP_KMEM;
    224 	} else {
    225 		me = pool_get(&uvm_map_entry_pool, PR_WAITOK);
    226 		me->flags = 0;
    227 	}
    228 
    229 	UVMHIST_LOG(maphist, "<- new entry=0x%x [kentry=%d]", me,
    230 	    ((map->flags & VM_MAP_INTRSAFE) != 0 || map == kernel_map), 0, 0);
    231 	return(me);
    232 }
    233 
    234 /*
    235  * uvm_mapent_free: free map entry
    236  */
    237 
    238 static __inline void
    239 uvm_mapent_free(me)
    240 	struct vm_map_entry *me;
    241 {
    242 	int s;
    243 	UVMHIST_FUNC("uvm_mapent_free"); UVMHIST_CALLED(maphist);
    244 
    245 	UVMHIST_LOG(maphist,"<- freeing map entry=0x%x [flags=%d]",
    246 		me, me->flags, 0, 0);
    247 	if (me->flags & UVM_MAP_STATIC) {
    248 		s = splvm();
    249 		simple_lock(&uvm.kentry_lock);
    250 		me->next = uvm.kentry_free;
    251 		uvm.kentry_free = me;
    252 		simple_unlock(&uvm.kentry_lock);
    253 		splx(s);
    254 	} else if (me->flags & UVM_MAP_KMEM) {
    255 		pool_put(&uvm_map_entry_kmem_pool, me);
    256 	} else {
    257 		pool_put(&uvm_map_entry_pool, me);
    258 	}
    259 }
    260 
    261 /*
    262  * uvm_mapent_copy: copy a map entry, preserving flags
    263  */
    264 
    265 static __inline void
    266 uvm_mapent_copy(src, dst)
    267 	struct vm_map_entry *src;
    268 	struct vm_map_entry *dst;
    269 {
    270 	memcpy(dst, src, ((char *)&src->uvm_map_entry_stop_copy) -
    271 	   ((char *)src));
    272 }
    273 
    274 /*
    275  * uvm_map_entry_unwire: unwire a map entry
    276  *
    277  * => map should be locked by caller
    278  */
    279 
    280 static __inline void
    281 uvm_map_entry_unwire(map, entry)
    282 	struct vm_map *map;
    283 	struct vm_map_entry *entry;
    284 {
    285 	entry->wired_count = 0;
    286 	uvm_fault_unwire_locked(map, entry->start, entry->end);
    287 }
    288 
    289 
    290 /*
    291  * wrapper for calling amap_ref()
    292  */
    293 static __inline void
    294 uvm_map_reference_amap(entry, flags)
    295 	struct vm_map_entry *entry;
    296 	int flags;
    297 {
    298 	amap_ref(entry->aref.ar_amap, entry->aref.ar_pageoff,
    299 	     (entry->end - entry->start) >> PAGE_SHIFT, flags);
    300 }
    301 
    302 
    303 /*
    304  * wrapper for calling amap_unref()
    305  */
    306 static __inline void
    307 uvm_map_unreference_amap(entry, flags)
    308 	struct vm_map_entry *entry;
    309 	int flags;
    310 {
    311 	amap_unref(entry->aref.ar_amap, entry->aref.ar_pageoff,
    312 	     (entry->end - entry->start) >> PAGE_SHIFT, flags);
    313 }
    314 
    315 
    316 /*
    317  * uvm_map_init: init mapping system at boot time.   note that we allocate
    318  * and init the static pool of struct vm_map_entry *'s for the kernel here.
    319  */
    320 
    321 void
    322 uvm_map_init()
    323 {
    324 	static struct vm_map_entry kernel_map_entry[MAX_KMAPENT];
    325 #if defined(UVMHIST)
    326 	static struct uvm_history_ent maphistbuf[100];
    327 	static struct uvm_history_ent pdhistbuf[100];
    328 #endif
    329 	int lcv;
    330 
    331 	/*
    332 	 * first, init logging system.
    333 	 */
    334 
    335 	UVMHIST_FUNC("uvm_map_init");
    336 	UVMHIST_INIT_STATIC(maphist, maphistbuf);
    337 	UVMHIST_INIT_STATIC(pdhist, pdhistbuf);
    338 	UVMHIST_CALLED(maphist);
    339 	UVMHIST_LOG(maphist,"<starting uvm map system>", 0, 0, 0, 0);
    340 	UVMCNT_INIT(uvm_map_call,  UVMCNT_CNT, 0,
    341 	    "# uvm_map() successful calls", 0);
    342 	UVMCNT_INIT(map_backmerge, UVMCNT_CNT, 0, "# uvm_map() back merges", 0);
    343 	UVMCNT_INIT(map_forwmerge, UVMCNT_CNT, 0, "# uvm_map() missed forward",
    344 	    0);
    345 	UVMCNT_INIT(uvm_mlk_call,  UVMCNT_CNT, 0, "# map lookup calls", 0);
    346 	UVMCNT_INIT(uvm_mlk_hint,  UVMCNT_CNT, 0, "# map lookup hint hits", 0);
    347 
    348 	/*
    349 	 * now set up static pool of kernel map entrys ...
    350 	 */
    351 
    352 	simple_lock_init(&uvm.kentry_lock);
    353 	uvm.kentry_free = NULL;
    354 	for (lcv = 0 ; lcv < MAX_KMAPENT ; lcv++) {
    355 		kernel_map_entry[lcv].next = uvm.kentry_free;
    356 		uvm.kentry_free = &kernel_map_entry[lcv];
    357 	}
    358 
    359 	/*
    360 	 * initialize the map-related pools.
    361 	 */
    362 	pool_init(&uvm_vmspace_pool, sizeof(struct vmspace),
    363 	    0, 0, 0, "vmsppl", 0,
    364 	    pool_page_alloc_nointr, pool_page_free_nointr, M_VMMAP);
    365 	pool_init(&uvm_map_entry_pool, sizeof(struct vm_map_entry),
    366 	    0, 0, 0, "vmmpepl", 0,
    367 	    pool_page_alloc_nointr, pool_page_free_nointr, M_VMMAP);
    368 	pool_init(&uvm_map_entry_kmem_pool, sizeof(struct vm_map_entry),
    369 	    0, 0, 0, "vmmpekpl", 0, NULL, NULL, M_VMMAP);
    370 }
    371 
    372 /*
    373  * clippers
    374  */
    375 
    376 /*
    377  * uvm_map_clip_start: ensure that the entry begins at or after
    378  *	the starting address, if it doesn't we split the entry.
    379  *
    380  * => caller should use UVM_MAP_CLIP_START macro rather than calling
    381  *    this directly
    382  * => map must be locked by caller
    383  */
    384 
    385 void
    386 uvm_map_clip_start(map, entry, start)
    387 	struct vm_map *map;
    388 	struct vm_map_entry *entry;
    389 	vaddr_t start;
    390 {
    391 	struct vm_map_entry *new_entry;
    392 	vaddr_t new_adj;
    393 
    394 	/* uvm_map_simplify_entry(map, entry); */ /* XXX */
    395 
    396 	/*
    397 	 * Split off the front portion.  note that we must insert the new
    398 	 * entry BEFORE this one, so that this entry has the specified
    399 	 * starting address.
    400 	 */
    401 
    402 	new_entry = uvm_mapent_alloc(map);
    403 	uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
    404 
    405 	new_entry->end = start;
    406 	new_adj = start - new_entry->start;
    407 	if (entry->object.uvm_obj)
    408 		entry->offset += new_adj;	/* shift start over */
    409 	entry->start = start;
    410 
    411 	if (new_entry->aref.ar_amap) {
    412 		amap_splitref(&new_entry->aref, &entry->aref, new_adj);
    413 	}
    414 
    415 	uvm_map_entry_link(map, entry->prev, new_entry);
    416 
    417 	if (UVM_ET_ISSUBMAP(entry)) {
    418 		/* ... unlikely to happen, but play it safe */
    419 		 uvm_map_reference(new_entry->object.sub_map);
    420 	} else {
    421 		if (UVM_ET_ISOBJ(entry) &&
    422 		    entry->object.uvm_obj->pgops &&
    423 		    entry->object.uvm_obj->pgops->pgo_reference)
    424 			entry->object.uvm_obj->pgops->pgo_reference(
    425 			    entry->object.uvm_obj);
    426 	}
    427 }
    428 
    429 /*
    430  * uvm_map_clip_end: ensure that the entry ends at or before
    431  *	the ending address, if it does't we split the reference
    432  *
    433  * => caller should use UVM_MAP_CLIP_END macro rather than calling
    434  *    this directly
    435  * => map must be locked by caller
    436  */
    437 
    438 void
    439 uvm_map_clip_end(map, entry, end)
    440 	struct vm_map *map;
    441 	struct vm_map_entry *entry;
    442 	vaddr_t	end;
    443 {
    444 	struct vm_map_entry *	new_entry;
    445 	vaddr_t new_adj; /* #bytes we move start forward */
    446 
    447 	/*
    448 	 *	Create a new entry and insert it
    449 	 *	AFTER the specified entry
    450 	 */
    451 
    452 	new_entry = uvm_mapent_alloc(map);
    453 	uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
    454 
    455 	new_entry->start = entry->end = end;
    456 	new_adj = end - entry->start;
    457 	if (new_entry->object.uvm_obj)
    458 		new_entry->offset += new_adj;
    459 
    460 	if (entry->aref.ar_amap)
    461 		amap_splitref(&entry->aref, &new_entry->aref, new_adj);
    462 
    463 	uvm_map_entry_link(map, entry, new_entry);
    464 
    465 	if (UVM_ET_ISSUBMAP(entry)) {
    466 		/* ... unlikely to happen, but play it safe */
    467 	 	uvm_map_reference(new_entry->object.sub_map);
    468 	} else {
    469 		if (UVM_ET_ISOBJ(entry) &&
    470 		    entry->object.uvm_obj->pgops &&
    471 		    entry->object.uvm_obj->pgops->pgo_reference)
    472 			entry->object.uvm_obj->pgops->pgo_reference(
    473 			    entry->object.uvm_obj);
    474 	}
    475 }
    476 
    477 
    478 /*
    479  *   M A P   -   m a i n   e n t r y   p o i n t
    480  */
    481 /*
    482  * uvm_map: establish a valid mapping in a map
    483  *
    484  * => assume startp is page aligned.
    485  * => assume size is a multiple of PAGE_SIZE.
    486  * => assume sys_mmap provides enough of a "hint" to have us skip
    487  *	over text/data/bss area.
    488  * => map must be unlocked (we will lock it)
    489  * => <uobj,uoffset> value meanings (4 cases):
    490  *	 [1] <NULL,uoffset> 		== uoffset is a hint for PMAP_PREFER
    491  *	 [2] <NULL,UVM_UNKNOWN_OFFSET>	== don't PMAP_PREFER
    492  *	 [3] <uobj,uoffset>		== normal mapping
    493  *	 [4] <uobj,UVM_UNKNOWN_OFFSET>	== uvm_map finds offset based on VA
    494  *
    495  *    case [4] is for kernel mappings where we don't know the offset until
    496  *    we've found a virtual address.   note that kernel object offsets are
    497  *    always relative to vm_map_min(kernel_map).
    498  *
    499  * => if `align' is non-zero, we try to align the virtual address to
    500  *	the specified alignment.  this is only a hint; if we can't
    501  *	do it, the address will be unaligned.  this is provided as
    502  *	a mechanism for large pages.
    503  *
    504  * => XXXCDC: need way to map in external amap?
    505  */
    506 
    507 int
    508 uvm_map(map, startp, size, uobj, uoffset, align, flags)
    509 	struct vm_map *map;
    510 	vaddr_t *startp;	/* IN/OUT */
    511 	vsize_t size;
    512 	struct uvm_object *uobj;
    513 	voff_t uoffset;
    514 	vsize_t align;
    515 	uvm_flag_t flags;
    516 {
    517 	struct vm_map_entry *prev_entry, *new_entry;
    518 	vm_prot_t prot = UVM_PROTECTION(flags), maxprot =
    519 	    UVM_MAXPROTECTION(flags);
    520 	vm_inherit_t inherit = UVM_INHERIT(flags);
    521 	int advice = UVM_ADVICE(flags);
    522 	UVMHIST_FUNC("uvm_map");
    523 	UVMHIST_CALLED(maphist);
    524 
    525 	UVMHIST_LOG(maphist, "(map=0x%x, *startp=0x%x, size=%d, flags=0x%x)",
    526 	    map, *startp, size, flags);
    527 	UVMHIST_LOG(maphist, "  uobj/offset 0x%x/%d", uobj, uoffset,0,0);
    528 
    529 	/*
    530 	 * check sanity of protection code
    531 	 */
    532 
    533 	if ((prot & maxprot) != prot) {
    534 		UVMHIST_LOG(maphist, "<- prot. failure:  prot=0x%x, max=0x%x",
    535 		prot, maxprot,0,0);
    536 		return EACCES;
    537 	}
    538 
    539 	/*
    540 	 * for pager_map, allocate the new entry first to avoid sleeping
    541 	 * for memory while we have the map locked.
    542 	 */
    543 
    544 	new_entry = NULL;
    545 	if (map == pager_map) {
    546 		new_entry = uvm_mapent_alloc(map);
    547 	}
    548 
    549 	/*
    550 	 * figure out where to put new VM range
    551 	 */
    552 
    553 	if (vm_map_lock_try(map) == FALSE) {
    554 		if (flags & UVM_FLAG_TRYLOCK) {
    555 			if (new_entry) {
    556 				uvm_mapent_free(new_entry);
    557 			}
    558 			return EAGAIN;
    559 		}
    560 		vm_map_lock(map); /* could sleep here */
    561 	}
    562 	if ((prev_entry = uvm_map_findspace(map, *startp, size, startp,
    563 	    uobj, uoffset, align, flags)) == NULL) {
    564 		UVMHIST_LOG(maphist,"<- uvm_map_findspace failed!",0,0,0,0);
    565 		vm_map_unlock(map);
    566 		if (new_entry) {
    567 			uvm_mapent_free(new_entry);
    568 		}
    569 		return ENOMEM;
    570 	}
    571 
    572 #ifdef PMAP_GROWKERNEL
    573 	{
    574 		/*
    575 		 * If the kernel pmap can't map the requested space,
    576 		 * then allocate more resources for it.
    577 		 */
    578 		if (map == kernel_map && uvm_maxkaddr < (*startp + size))
    579 			uvm_maxkaddr = pmap_growkernel(*startp + size);
    580 	}
    581 #endif
    582 
    583 	UVMCNT_INCR(uvm_map_call);
    584 
    585 	/*
    586 	 * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER
    587 	 * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET.   in
    588 	 * either case we want to zero it  before storing it in the map entry
    589 	 * (because it looks strange and confusing when debugging...)
    590 	 *
    591 	 * if uobj is not null
    592 	 *   if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping
    593 	 *      and we do not need to change uoffset.
    594 	 *   if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset
    595 	 *      now (based on the starting address of the map).   this case is
    596 	 *      for kernel object mappings where we don't know the offset until
    597 	 *      the virtual address is found (with uvm_map_findspace).   the
    598 	 *      offset is the distance we are from the start of the map.
    599 	 */
    600 
    601 	if (uobj == NULL) {
    602 		uoffset = 0;
    603 	} else {
    604 		if (uoffset == UVM_UNKNOWN_OFFSET) {
    605 			KASSERT(UVM_OBJ_IS_KERN_OBJECT(uobj));
    606 			uoffset = *startp - vm_map_min(kernel_map);
    607 		}
    608 	}
    609 
    610 	/*
    611 	 * try and insert in map by extending previous entry, if possible.
    612 	 * XXX: we don't try and pull back the next entry.   might be useful
    613 	 * for a stack, but we are currently allocating our stack in advance.
    614 	 */
    615 
    616 	if ((flags & UVM_FLAG_NOMERGE) == 0 &&
    617 	    prev_entry->end == *startp && prev_entry != &map->header &&
    618 	    prev_entry->object.uvm_obj == uobj) {
    619 
    620 		if (uobj && prev_entry->offset +
    621 		    (prev_entry->end - prev_entry->start) != uoffset)
    622 			goto nomerge;
    623 
    624 		if (UVM_ET_ISSUBMAP(prev_entry))
    625 			goto nomerge;
    626 
    627 		if (prev_entry->protection != prot ||
    628 		    prev_entry->max_protection != maxprot)
    629 			goto nomerge;
    630 
    631 		if (prev_entry->inheritance != inherit ||
    632 		    prev_entry->advice != advice)
    633 			goto nomerge;
    634 
    635 		/* wiring status must match (new area is unwired) */
    636 		if (VM_MAPENT_ISWIRED(prev_entry))
    637 			goto nomerge;
    638 
    639 		/*
    640 		 * can't extend a shared amap.  note: no need to lock amap to
    641 		 * look at refs since we don't care about its exact value.
    642 		 * if it is one (i.e. we have only reference) it will stay there
    643 		 */
    644 
    645 		if (prev_entry->aref.ar_amap &&
    646 		    amap_refs(prev_entry->aref.ar_amap) != 1) {
    647 			goto nomerge;
    648 		}
    649 
    650 		/* got it! */
    651 
    652 		UVMCNT_INCR(map_backmerge);
    653 		UVMHIST_LOG(maphist,"  starting back merge", 0, 0, 0, 0);
    654 
    655 		/*
    656 		 * drop our reference to uobj since we are extending a reference
    657 		 * that we already have (the ref count can not drop to zero).
    658 		 */
    659 		if (uobj && uobj->pgops->pgo_detach)
    660 			uobj->pgops->pgo_detach(uobj);
    661 
    662 		if (prev_entry->aref.ar_amap) {
    663 			amap_extend(prev_entry, size);
    664 		}
    665 
    666 		prev_entry->end += size;
    667 		map->size += size;
    668 
    669 		UVMHIST_LOG(maphist,"<- done (via backmerge)!", 0, 0, 0, 0);
    670 		vm_map_unlock(map);
    671 		if (new_entry) {
    672 			uvm_mapent_free(new_entry);
    673 		}
    674 		return 0;
    675 	}
    676 
    677 nomerge:
    678 	UVMHIST_LOG(maphist,"  allocating new map entry", 0, 0, 0, 0);
    679 
    680 	/*
    681 	 * check for possible forward merge (which we don't do) and count
    682 	 * the number of times we missed a *possible* chance to merge more
    683 	 */
    684 
    685 	if ((flags & UVM_FLAG_NOMERGE) == 0 &&
    686 	    prev_entry->next != &map->header &&
    687 	    prev_entry->next->start == (*startp + size))
    688 		UVMCNT_INCR(map_forwmerge);
    689 
    690 	/*
    691 	 * allocate new entry and link it in.
    692 	 */
    693 
    694 	if (new_entry == NULL) {
    695 		new_entry = uvm_mapent_alloc(map);
    696 	}
    697 	new_entry->start = *startp;
    698 	new_entry->end = new_entry->start + size;
    699 	new_entry->object.uvm_obj = uobj;
    700 	new_entry->offset = uoffset;
    701 
    702 	if (uobj)
    703 		new_entry->etype = UVM_ET_OBJ;
    704 	else
    705 		new_entry->etype = 0;
    706 
    707 	if (flags & UVM_FLAG_COPYONW) {
    708 		new_entry->etype |= UVM_ET_COPYONWRITE;
    709 		if ((flags & UVM_FLAG_OVERLAY) == 0)
    710 			new_entry->etype |= UVM_ET_NEEDSCOPY;
    711 	}
    712 
    713 	new_entry->protection = prot;
    714 	new_entry->max_protection = maxprot;
    715 	new_entry->inheritance = inherit;
    716 	new_entry->wired_count = 0;
    717 	new_entry->advice = advice;
    718 	if (flags & UVM_FLAG_OVERLAY) {
    719 
    720 		/*
    721 		 * to_add: for BSS we overallocate a little since we
    722 		 * are likely to extend
    723 		 */
    724 
    725 		vaddr_t to_add = (flags & UVM_FLAG_AMAPPAD) ?
    726 			UVM_AMAP_CHUNK << PAGE_SHIFT : 0;
    727 		struct vm_amap *amap = amap_alloc(size, to_add, M_WAITOK);
    728 		new_entry->aref.ar_pageoff = 0;
    729 		new_entry->aref.ar_amap = amap;
    730 	} else {
    731 		new_entry->aref.ar_pageoff = 0;
    732 		new_entry->aref.ar_amap = NULL;
    733 	}
    734 	uvm_map_entry_link(map, prev_entry, new_entry);
    735 	map->size += size;
    736 
    737 	/*
    738 	 * Update the free space hint
    739 	 */
    740 
    741 	if ((map->first_free == prev_entry) &&
    742 	    (prev_entry->end >= new_entry->start))
    743 		map->first_free = new_entry;
    744 
    745 	UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
    746 	vm_map_unlock(map);
    747 	return 0;
    748 }
    749 
    750 /*
    751  * uvm_map_lookup_entry: find map entry at or before an address
    752  *
    753  * => map must at least be read-locked by caller
    754  * => entry is returned in "entry"
    755  * => return value is true if address is in the returned entry
    756  */
    757 
    758 boolean_t
    759 uvm_map_lookup_entry(map, address, entry)
    760 	struct vm_map *map;
    761 	vaddr_t	address;
    762 	struct vm_map_entry **entry;		/* OUT */
    763 {
    764 	struct vm_map_entry *cur;
    765 	struct vm_map_entry *last;
    766 	UVMHIST_FUNC("uvm_map_lookup_entry");
    767 	UVMHIST_CALLED(maphist);
    768 
    769 	UVMHIST_LOG(maphist,"(map=0x%x,addr=0x%x,ent=0x%x)",
    770 	    map, address, entry, 0);
    771 
    772 	/*
    773 	 * start looking either from the head of the
    774 	 * list, or from the hint.
    775 	 */
    776 
    777 	simple_lock(&map->hint_lock);
    778 	cur = map->hint;
    779 	simple_unlock(&map->hint_lock);
    780 
    781 	if (cur == &map->header)
    782 		cur = cur->next;
    783 
    784 	UVMCNT_INCR(uvm_mlk_call);
    785 	if (address >= cur->start) {
    786 
    787 	    	/*
    788 		 * go from hint to end of list.
    789 		 *
    790 		 * but first, make a quick check to see if
    791 		 * we are already looking at the entry we
    792 		 * want (which is usually the case).
    793 		 * note also that we don't need to save the hint
    794 		 * here... it is the same hint (unless we are
    795 		 * at the header, in which case the hint didn't
    796 		 * buy us anything anyway).
    797 		 */
    798 
    799 		last = &map->header;
    800 		if ((cur != last) && (cur->end > address)) {
    801 			UVMCNT_INCR(uvm_mlk_hint);
    802 			*entry = cur;
    803 			UVMHIST_LOG(maphist,"<- got it via hint (0x%x)",
    804 			    cur, 0, 0, 0);
    805 			return (TRUE);
    806 		}
    807 	} else {
    808 
    809 	    	/*
    810 		 * go from start to hint, *inclusively*
    811 		 */
    812 
    813 		last = cur->next;
    814 		cur = map->header.next;
    815 	}
    816 
    817 	/*
    818 	 * search linearly
    819 	 */
    820 
    821 	while (cur != last) {
    822 		if (cur->end > address) {
    823 			if (address >= cur->start) {
    824 			    	/*
    825 				 * save this lookup for future
    826 				 * hints, and return
    827 				 */
    828 
    829 				*entry = cur;
    830 				SAVE_HINT(map, map->hint, cur);
    831 				UVMHIST_LOG(maphist,"<- search got it (0x%x)",
    832 					cur, 0, 0, 0);
    833 				return (TRUE);
    834 			}
    835 			break;
    836 		}
    837 		cur = cur->next;
    838 	}
    839 	*entry = cur->prev;
    840 	SAVE_HINT(map, map->hint, *entry);
    841 	UVMHIST_LOG(maphist,"<- failed!",0,0,0,0);
    842 	return (FALSE);
    843 }
    844 
    845 /*
    846  * uvm_map_findspace: find "length" sized space in "map".
    847  *
    848  * => "hint" is a hint about where we want it, unless FINDSPACE_FIXED is
    849  *	set (in which case we insist on using "hint").
    850  * => "result" is VA returned
    851  * => uobj/uoffset are to be used to handle VAC alignment, if required
    852  * => if `align' is non-zero, we attempt to align to that value.
    853  * => caller must at least have read-locked map
    854  * => returns NULL on failure, or pointer to prev. map entry if success
    855  * => note this is a cross between the old vm_map_findspace and vm_map_find
    856  */
    857 
    858 struct vm_map_entry *
    859 uvm_map_findspace(map, hint, length, result, uobj, uoffset, align, flags)
    860 	struct vm_map *map;
    861 	vaddr_t hint;
    862 	vsize_t length;
    863 	vaddr_t *result; /* OUT */
    864 	struct uvm_object *uobj;
    865 	voff_t uoffset;
    866 	vsize_t align;
    867 	int flags;
    868 {
    869 	struct vm_map_entry *entry, *next, *tmp;
    870 	vaddr_t end, orig_hint;
    871 	UVMHIST_FUNC("uvm_map_findspace");
    872 	UVMHIST_CALLED(maphist);
    873 
    874 	UVMHIST_LOG(maphist, "(map=0x%x, hint=0x%x, len=%d, flags=0x%x)",
    875 		    map, hint, length, flags);
    876 	KASSERT((align & (align - 1)) == 0);
    877 	KASSERT((flags & UVM_FLAG_FIXED) == 0 || align == 0);
    878 
    879 	/*
    880 	 * remember the original hint.  if we are aligning, then we
    881 	 * may have to try again with no alignment constraint if
    882 	 * we fail the first time.
    883 	 */
    884 
    885 	orig_hint = hint;
    886 	if (hint < map->min_offset) {	/* check ranges ... */
    887 		if (flags & UVM_FLAG_FIXED) {
    888 			UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0);
    889 			return(NULL);
    890 		}
    891 		hint = map->min_offset;
    892 	}
    893 	if (hint > map->max_offset) {
    894 		UVMHIST_LOG(maphist,"<- VA 0x%x > range [0x%x->0x%x]",
    895 				hint, map->min_offset, map->max_offset, 0);
    896 		return(NULL);
    897 	}
    898 
    899 	/*
    900 	 * Look for the first possible address; if there's already
    901 	 * something at this address, we have to start after it.
    902 	 */
    903 
    904 	if ((flags & UVM_FLAG_FIXED) == 0 && hint == map->min_offset) {
    905 		if ((entry = map->first_free) != &map->header)
    906 			hint = entry->end;
    907 	} else {
    908 		if (uvm_map_lookup_entry(map, hint, &tmp)) {
    909 			/* "hint" address already in use ... */
    910 			if (flags & UVM_FLAG_FIXED) {
    911 				UVMHIST_LOG(maphist,"<- fixed & VA in use",
    912 				    0, 0, 0, 0);
    913 				return(NULL);
    914 			}
    915 			hint = tmp->end;
    916 		}
    917 		entry = tmp;
    918 	}
    919 
    920 	/*
    921 	 * Look through the rest of the map, trying to fit a new region in
    922 	 * the gap between existing regions, or after the very last region.
    923 	 * note: entry->end   = base VA of current gap,
    924 	 *	 next->start  = VA of end of current gap
    925 	 */
    926 
    927 	for (;; hint = (entry = next)->end) {
    928 
    929 		/*
    930 		 * Find the end of the proposed new region.  Be sure we didn't
    931 		 * go beyond the end of the map, or wrap around the address;
    932 		 * if so, we lose.  Otherwise, if this is the last entry, or
    933 		 * if the proposed new region fits before the next entry, we
    934 		 * win.
    935 		 */
    936 
    937 #ifdef PMAP_PREFER
    938 		/*
    939 		 * push hint forward as needed to avoid VAC alias problems.
    940 		 * we only do this if a valid offset is specified.
    941 		 */
    942 
    943 		if ((flags & UVM_FLAG_FIXED) == 0 &&
    944 		    uoffset != UVM_UNKNOWN_OFFSET)
    945 			PMAP_PREFER(uoffset, &hint);
    946 #endif
    947 		if (align != 0) {
    948 			if ((hint & (align - 1)) != 0)
    949 				hint = roundup(hint, align);
    950 			/*
    951 			 * XXX Should we PMAP_PREFER() here again?
    952 			 */
    953 		}
    954 		end = hint + length;
    955 		if (end > map->max_offset || end < hint) {
    956 			UVMHIST_LOG(maphist,"<- failed (off end)", 0,0,0,0);
    957 			if (align != 0) {
    958 				UVMHIST_LOG(maphist,
    959 				    "calling recursively, no align",
    960 				    0,0,0,0);
    961 				return (uvm_map_findspace(map, orig_hint,
    962 				    length, result, uobj, uoffset, 0, flags));
    963 			}
    964 			return (NULL);
    965 		}
    966 		next = entry->next;
    967 		if (next == &map->header || next->start >= end)
    968 			break;
    969 		if (flags & UVM_FLAG_FIXED) {
    970 			UVMHIST_LOG(maphist,"<- fixed mapping failed", 0,0,0,0);
    971 			return(NULL); /* only one shot at it ... */
    972 		}
    973 	}
    974 	SAVE_HINT(map, map->hint, entry);
    975 	*result = hint;
    976 	UVMHIST_LOG(maphist,"<- got it!  (result=0x%x)", hint, 0,0,0);
    977 	return (entry);
    978 }
    979 
    980 /*
    981  *   U N M A P   -   m a i n   h e l p e r   f u n c t i o n s
    982  */
    983 
    984 /*
    985  * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
    986  *
    987  * => caller must check alignment and size
    988  * => map must be locked by caller
    989  * => we return a list of map entries that we've remove from the map
    990  *    in "entry_list"
    991  */
    992 
    993 void
    994 uvm_unmap_remove(map, start, end, entry_list)
    995 	struct vm_map *map;
    996 	vaddr_t start, end;
    997 	struct vm_map_entry **entry_list;	/* OUT */
    998 {
    999 	struct vm_map_entry *entry, *first_entry, *next;
   1000 	vaddr_t len;
   1001 	UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist);
   1002 
   1003 	UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)",
   1004 	    map, start, end, 0);
   1005 	VM_MAP_RANGE_CHECK(map, start, end);
   1006 
   1007 	/*
   1008 	 * find first entry
   1009 	 */
   1010 
   1011 	if (uvm_map_lookup_entry(map, start, &first_entry) == TRUE) {
   1012 		/* clip and go... */
   1013 		entry = first_entry;
   1014 		UVM_MAP_CLIP_START(map, entry, start);
   1015 		/* critical!  prevents stale hint */
   1016 		SAVE_HINT(map, entry, entry->prev);
   1017 	} else {
   1018 		entry = first_entry->next;
   1019 	}
   1020 
   1021 	/*
   1022 	 * Save the free space hint
   1023 	 */
   1024 
   1025 	if (map->first_free->start >= start)
   1026 		map->first_free = entry->prev;
   1027 
   1028 	/*
   1029 	 * note: we now re-use first_entry for a different task.  we remove
   1030 	 * a number of map entries from the map and save them in a linked
   1031 	 * list headed by "first_entry".  once we remove them from the map
   1032 	 * the caller should unlock the map and drop the references to the
   1033 	 * backing objects [c.f. uvm_unmap_detach].  the object is to
   1034 	 * separate unmapping from reference dropping.  why?
   1035 	 *   [1] the map has to be locked for unmapping
   1036 	 *   [2] the map need not be locked for reference dropping
   1037 	 *   [3] dropping references may trigger pager I/O, and if we hit
   1038 	 *       a pager that does synchronous I/O we may have to wait for it.
   1039 	 *   [4] we would like all waiting for I/O to occur with maps unlocked
   1040 	 *       so that we don't block other threads.
   1041 	 */
   1042 
   1043 	first_entry = NULL;
   1044 	*entry_list = NULL;
   1045 
   1046 	/*
   1047 	 * break up the area into map entry sized regions and unmap.  note
   1048 	 * that all mappings have to be removed before we can even consider
   1049 	 * dropping references to amaps or VM objects (otherwise we could end
   1050 	 * up with a mapping to a page on the free list which would be very bad)
   1051 	 */
   1052 
   1053 	while ((entry != &map->header) && (entry->start < end)) {
   1054 		UVM_MAP_CLIP_END(map, entry, end);
   1055 		next = entry->next;
   1056 		len = entry->end - entry->start;
   1057 
   1058 		/*
   1059 		 * unwire before removing addresses from the pmap; otherwise
   1060 		 * unwiring will put the entries back into the pmap (XXX).
   1061 		 */
   1062 
   1063 		if (VM_MAPENT_ISWIRED(entry)) {
   1064 			uvm_map_entry_unwire(map, entry);
   1065 		}
   1066 		if ((map->flags & VM_MAP_PAGEABLE) == 0) {
   1067 
   1068 			/*
   1069 			 * if the map is non-pageable, any pages mapped there
   1070 			 * must be wired and entered with pmap_kenter_pa(),
   1071 			 * and we should free any such pages immediately.
   1072 			 * this is mostly used for kmem_map and mb_map.
   1073 			 */
   1074 
   1075 			uvm_km_pgremove_intrsafe(entry->start, entry->end);
   1076 			pmap_kremove(entry->start, len);
   1077 		} else if (UVM_ET_ISOBJ(entry) &&
   1078 			   UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) {
   1079 			KASSERT(vm_map_pmap(map) == pmap_kernel());
   1080 
   1081 			/*
   1082 			 * note: kernel object mappings are currently used in
   1083 			 * two ways:
   1084 			 *  [1] "normal" mappings of pages in the kernel object
   1085 			 *  [2] uvm_km_valloc'd allocations in which we
   1086 			 *      pmap_enter in some non-kernel-object page
   1087 			 *      (e.g. vmapbuf).
   1088 			 *
   1089 			 * for case [1], we need to remove the mapping from
   1090 			 * the pmap and then remove the page from the kernel
   1091 			 * object (because, once pages in a kernel object are
   1092 			 * unmapped they are no longer needed, unlike, say,
   1093 			 * a vnode where you might want the data to persist
   1094 			 * until flushed out of a queue).
   1095 			 *
   1096 			 * for case [2], we need to remove the mapping from
   1097 			 * the pmap.  there shouldn't be any pages at the
   1098 			 * specified offset in the kernel object [but it
   1099 			 * doesn't hurt to call uvm_km_pgremove just to be
   1100 			 * safe?]
   1101 			 *
   1102 			 * uvm_km_pgremove currently does the following:
   1103 			 *   for pages in the kernel object in range:
   1104 			 *     - drops the swap slot
   1105 			 *     - uvm_pagefree the page
   1106 			 */
   1107 
   1108 			/*
   1109 			 * remove mappings from pmap and drop the pages
   1110 			 * from the object.  offsets are always relative
   1111 			 * to vm_map_min(kernel_map).
   1112 			 */
   1113 
   1114 			pmap_remove(pmap_kernel(), entry->start,
   1115 			    entry->start + len);
   1116 			uvm_km_pgremove(entry->object.uvm_obj,
   1117 			    entry->start - vm_map_min(kernel_map),
   1118 			    entry->end - vm_map_min(kernel_map));
   1119 
   1120 			/*
   1121 			 * null out kernel_object reference, we've just
   1122 			 * dropped it
   1123 			 */
   1124 
   1125 			entry->etype &= ~UVM_ET_OBJ;
   1126 			entry->object.uvm_obj = NULL;
   1127 		} else if (UVM_ET_ISOBJ(entry) || entry->aref.ar_amap) {
   1128 
   1129 			/*
   1130 		 	 * remove mappings the standard way.
   1131 		 	 */
   1132 
   1133 			pmap_remove(map->pmap, entry->start, entry->end);
   1134 		}
   1135 
   1136 		/*
   1137 		 * remove entry from map and put it on our list of entries
   1138 		 * that we've nuked.  then go to next entry.
   1139 		 */
   1140 
   1141 		UVMHIST_LOG(maphist, "  removed map entry 0x%x", entry, 0, 0,0);
   1142 
   1143 		/* critical!  prevents stale hint */
   1144 		SAVE_HINT(map, entry, entry->prev);
   1145 
   1146 		uvm_map_entry_unlink(map, entry);
   1147 		map->size -= len;
   1148 		entry->next = first_entry;
   1149 		first_entry = entry;
   1150 		entry = next;
   1151 	}
   1152 	pmap_update(vm_map_pmap(map));
   1153 
   1154 	/*
   1155 	 * now we've cleaned up the map and are ready for the caller to drop
   1156 	 * references to the mapped objects.
   1157 	 */
   1158 
   1159 	*entry_list = first_entry;
   1160 	UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
   1161 }
   1162 
   1163 /*
   1164  * uvm_unmap_detach: drop references in a chain of map entries
   1165  *
   1166  * => we will free the map entries as we traverse the list.
   1167  */
   1168 
   1169 void
   1170 uvm_unmap_detach(first_entry, flags)
   1171 	struct vm_map_entry *first_entry;
   1172 	int flags;
   1173 {
   1174 	struct vm_map_entry *next_entry;
   1175 	UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist);
   1176 
   1177 	while (first_entry) {
   1178 		KASSERT(!VM_MAPENT_ISWIRED(first_entry));
   1179 		UVMHIST_LOG(maphist,
   1180 		    "  detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d",
   1181 		    first_entry, first_entry->aref.ar_amap,
   1182 		    first_entry->object.uvm_obj,
   1183 		    UVM_ET_ISSUBMAP(first_entry));
   1184 
   1185 		/*
   1186 		 * drop reference to amap, if we've got one
   1187 		 */
   1188 
   1189 		if (first_entry->aref.ar_amap)
   1190 			uvm_map_unreference_amap(first_entry, flags);
   1191 
   1192 		/*
   1193 		 * drop reference to our backing object, if we've got one
   1194 		 */
   1195 
   1196 		if (UVM_ET_ISSUBMAP(first_entry)) {
   1197 			/* ... unlikely to happen, but play it safe */
   1198 			uvm_map_deallocate(first_entry->object.sub_map);
   1199 		} else {
   1200 			if (UVM_ET_ISOBJ(first_entry) &&
   1201 			    first_entry->object.uvm_obj->pgops->pgo_detach)
   1202 				first_entry->object.uvm_obj->pgops->
   1203 				    pgo_detach(first_entry->object.uvm_obj);
   1204 		}
   1205 		next_entry = first_entry->next;
   1206 		uvm_mapent_free(first_entry);
   1207 		first_entry = next_entry;
   1208 	}
   1209 	UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
   1210 }
   1211 
   1212 /*
   1213  *   E X T R A C T I O N   F U N C T I O N S
   1214  */
   1215 
   1216 /*
   1217  * uvm_map_reserve: reserve space in a vm_map for future use.
   1218  *
   1219  * => we reserve space in a map by putting a dummy map entry in the
   1220  *    map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
   1221  * => map should be unlocked (we will write lock it)
   1222  * => we return true if we were able to reserve space
   1223  * => XXXCDC: should be inline?
   1224  */
   1225 
   1226 int
   1227 uvm_map_reserve(map, size, offset, align, raddr)
   1228 	struct vm_map *map;
   1229 	vsize_t size;
   1230 	vaddr_t offset;	/* hint for pmap_prefer */
   1231 	vsize_t align;	/* alignment hint */
   1232 	vaddr_t *raddr;	/* IN:hint, OUT: reserved VA */
   1233 {
   1234 	UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist);
   1235 
   1236 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)",
   1237 	      map,size,offset,raddr);
   1238 
   1239 	size = round_page(size);
   1240 	if (*raddr < vm_map_min(map))
   1241 		*raddr = vm_map_min(map);                /* hint */
   1242 
   1243 	/*
   1244 	 * reserve some virtual space.
   1245 	 */
   1246 
   1247 	if (uvm_map(map, raddr, size, NULL, offset, 0,
   1248 	    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
   1249 	    UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
   1250 	    UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
   1251 		return (FALSE);
   1252 	}
   1253 
   1254 	UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0);
   1255 	return (TRUE);
   1256 }
   1257 
   1258 /*
   1259  * uvm_map_replace: replace a reserved (blank) area of memory with
   1260  * real mappings.
   1261  *
   1262  * => caller must WRITE-LOCK the map
   1263  * => we return TRUE if replacement was a success
   1264  * => we expect the newents chain to have nnewents entrys on it and
   1265  *    we expect newents->prev to point to the last entry on the list
   1266  * => note newents is allowed to be NULL
   1267  */
   1268 
   1269 int
   1270 uvm_map_replace(map, start, end, newents, nnewents)
   1271 	struct vm_map *map;
   1272 	vaddr_t start, end;
   1273 	struct vm_map_entry *newents;
   1274 	int nnewents;
   1275 {
   1276 	struct vm_map_entry *oldent, *last;
   1277 
   1278 	/*
   1279 	 * first find the blank map entry at the specified address
   1280 	 */
   1281 
   1282 	if (!uvm_map_lookup_entry(map, start, &oldent)) {
   1283 		return(FALSE);
   1284 	}
   1285 
   1286 	/*
   1287 	 * check to make sure we have a proper blank entry
   1288 	 */
   1289 
   1290 	if (oldent->start != start || oldent->end != end ||
   1291 	    oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) {
   1292 		return (FALSE);
   1293 	}
   1294 
   1295 #ifdef DIAGNOSTIC
   1296 
   1297 	/*
   1298 	 * sanity check the newents chain
   1299 	 */
   1300 
   1301 	{
   1302 		struct vm_map_entry *tmpent = newents;
   1303 		int nent = 0;
   1304 		vaddr_t cur = start;
   1305 
   1306 		while (tmpent) {
   1307 			nent++;
   1308 			if (tmpent->start < cur)
   1309 				panic("uvm_map_replace1");
   1310 			if (tmpent->start > tmpent->end || tmpent->end > end) {
   1311 		printf("tmpent->start=0x%lx, tmpent->end=0x%lx, end=0x%lx\n",
   1312 			    tmpent->start, tmpent->end, end);
   1313 				panic("uvm_map_replace2");
   1314 			}
   1315 			cur = tmpent->end;
   1316 			if (tmpent->next) {
   1317 				if (tmpent->next->prev != tmpent)
   1318 					panic("uvm_map_replace3");
   1319 			} else {
   1320 				if (newents->prev != tmpent)
   1321 					panic("uvm_map_replace4");
   1322 			}
   1323 			tmpent = tmpent->next;
   1324 		}
   1325 		if (nent != nnewents)
   1326 			panic("uvm_map_replace5");
   1327 	}
   1328 #endif
   1329 
   1330 	/*
   1331 	 * map entry is a valid blank!   replace it.   (this does all the
   1332 	 * work of map entry link/unlink...).
   1333 	 */
   1334 
   1335 	if (newents) {
   1336 		last = newents->prev;
   1337 
   1338 		/* critical: flush stale hints out of map */
   1339 		SAVE_HINT(map, map->hint, newents);
   1340 		if (map->first_free == oldent)
   1341 			map->first_free = last;
   1342 
   1343 		last->next = oldent->next;
   1344 		last->next->prev = last;
   1345 		newents->prev = oldent->prev;
   1346 		newents->prev->next = newents;
   1347 		map->nentries = map->nentries + (nnewents - 1);
   1348 
   1349 	} else {
   1350 
   1351 		/* critical: flush stale hints out of map */
   1352 		SAVE_HINT(map, map->hint, oldent->prev);
   1353 		if (map->first_free == oldent)
   1354 			map->first_free = oldent->prev;
   1355 
   1356 		/* NULL list of new entries: just remove the old one */
   1357 		uvm_map_entry_unlink(map, oldent);
   1358 	}
   1359 
   1360 
   1361 	/*
   1362 	 * now we can free the old blank entry, unlock the map and return.
   1363 	 */
   1364 
   1365 	uvm_mapent_free(oldent);
   1366 	return(TRUE);
   1367 }
   1368 
   1369 /*
   1370  * uvm_map_extract: extract a mapping from a map and put it somewhere
   1371  *	(maybe removing the old mapping)
   1372  *
   1373  * => maps should be unlocked (we will write lock them)
   1374  * => returns 0 on success, error code otherwise
   1375  * => start must be page aligned
   1376  * => len must be page sized
   1377  * => flags:
   1378  *      UVM_EXTRACT_REMOVE: remove mappings from srcmap
   1379  *      UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
   1380  *      UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
   1381  *      UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
   1382  *    >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
   1383  *    >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
   1384  *             be used from within the kernel in a kernel level map <<<
   1385  */
   1386 
   1387 int
   1388 uvm_map_extract(srcmap, start, len, dstmap, dstaddrp, flags)
   1389 	struct vm_map *srcmap, *dstmap;
   1390 	vaddr_t start, *dstaddrp;
   1391 	vsize_t len;
   1392 	int flags;
   1393 {
   1394 	vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge,
   1395 	    oldstart;
   1396 	struct vm_map_entry *chain, *endchain, *entry, *orig_entry, *newentry,
   1397 	    *deadentry, *oldentry;
   1398 	vsize_t elen;
   1399 	int nchain, error, copy_ok;
   1400 	UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist);
   1401 
   1402 	UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start,
   1403 	    len,0);
   1404 	UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0);
   1405 
   1406 	/*
   1407 	 * step 0: sanity check: start must be on a page boundary, length
   1408 	 * must be page sized.  can't ask for CONTIG/QREF if you asked for
   1409 	 * REMOVE.
   1410 	 */
   1411 
   1412 	KASSERT((start & PAGE_MASK) == 0 && (len & PAGE_MASK) == 0);
   1413 	KASSERT((flags & UVM_EXTRACT_REMOVE) == 0 ||
   1414 		(flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) == 0);
   1415 
   1416 	/*
   1417 	 * step 1: reserve space in the target map for the extracted area
   1418 	 */
   1419 
   1420 	dstaddr = vm_map_min(dstmap);
   1421 	if (uvm_map_reserve(dstmap, len, start, 0, &dstaddr) == FALSE)
   1422 		return(ENOMEM);
   1423 	*dstaddrp = dstaddr;	/* pass address back to caller */
   1424 	UVMHIST_LOG(maphist, "  dstaddr=0x%x", dstaddr,0,0,0);
   1425 
   1426 	/*
   1427 	 * step 2: setup for the extraction process loop by init'ing the
   1428 	 * map entry chain, locking src map, and looking up the first useful
   1429 	 * entry in the map.
   1430 	 */
   1431 
   1432 	end = start + len;
   1433 	newend = dstaddr + len;
   1434 	chain = endchain = NULL;
   1435 	nchain = 0;
   1436 	vm_map_lock(srcmap);
   1437 
   1438 	if (uvm_map_lookup_entry(srcmap, start, &entry)) {
   1439 
   1440 		/* "start" is within an entry */
   1441 		if (flags & UVM_EXTRACT_QREF) {
   1442 
   1443 			/*
   1444 			 * for quick references we don't clip the entry, so
   1445 			 * the entry may map space "before" the starting
   1446 			 * virtual address... this is the "fudge" factor
   1447 			 * (which can be non-zero only the first time
   1448 			 * through the "while" loop in step 3).
   1449 			 */
   1450 
   1451 			fudge = start - entry->start;
   1452 		} else {
   1453 
   1454 			/*
   1455 			 * normal reference: we clip the map to fit (thus
   1456 			 * fudge is zero)
   1457 			 */
   1458 
   1459 			UVM_MAP_CLIP_START(srcmap, entry, start);
   1460 			SAVE_HINT(srcmap, srcmap->hint, entry->prev);
   1461 			fudge = 0;
   1462 		}
   1463 	} else {
   1464 
   1465 		/* "start" is not within an entry ... skip to next entry */
   1466 		if (flags & UVM_EXTRACT_CONTIG) {
   1467 			error = EINVAL;
   1468 			goto bad;    /* definite hole here ... */
   1469 		}
   1470 
   1471 		entry = entry->next;
   1472 		fudge = 0;
   1473 	}
   1474 
   1475 	/* save values from srcmap for step 6 */
   1476 	orig_entry = entry;
   1477 	orig_fudge = fudge;
   1478 
   1479 	/*
   1480 	 * step 3: now start looping through the map entries, extracting
   1481 	 * as we go.
   1482 	 */
   1483 
   1484 	while (entry->start < end && entry != &srcmap->header) {
   1485 
   1486 		/* if we are not doing a quick reference, clip it */
   1487 		if ((flags & UVM_EXTRACT_QREF) == 0)
   1488 			UVM_MAP_CLIP_END(srcmap, entry, end);
   1489 
   1490 		/* clear needs_copy (allow chunking) */
   1491 		if (UVM_ET_ISNEEDSCOPY(entry)) {
   1492 			if (fudge)
   1493 				oldstart = entry->start;
   1494 			else
   1495 				oldstart = 0;	/* XXX: gcc */
   1496 			amap_copy(srcmap, entry, M_NOWAIT, TRUE, start, end);
   1497 			if (UVM_ET_ISNEEDSCOPY(entry)) {  /* failed? */
   1498 				error = ENOMEM;
   1499 				goto bad;
   1500 			}
   1501 
   1502 			/* amap_copy could clip (during chunk)!  update fudge */
   1503 			if (fudge) {
   1504 				fudge = fudge - (entry->start - oldstart);
   1505 				orig_fudge = fudge;
   1506 			}
   1507 		}
   1508 
   1509 		/* calculate the offset of this from "start" */
   1510 		oldoffset = (entry->start + fudge) - start;
   1511 
   1512 		/* allocate a new map entry */
   1513 		newentry = uvm_mapent_alloc(dstmap);
   1514 		if (newentry == NULL) {
   1515 			error = ENOMEM;
   1516 			goto bad;
   1517 		}
   1518 
   1519 		/* set up new map entry */
   1520 		newentry->next = NULL;
   1521 		newentry->prev = endchain;
   1522 		newentry->start = dstaddr + oldoffset;
   1523 		newentry->end =
   1524 		    newentry->start + (entry->end - (entry->start + fudge));
   1525 		if (newentry->end > newend || newentry->end < newentry->start)
   1526 			newentry->end = newend;
   1527 		newentry->object.uvm_obj = entry->object.uvm_obj;
   1528 		if (newentry->object.uvm_obj) {
   1529 			if (newentry->object.uvm_obj->pgops->pgo_reference)
   1530 				newentry->object.uvm_obj->pgops->
   1531 				    pgo_reference(newentry->object.uvm_obj);
   1532 				newentry->offset = entry->offset + fudge;
   1533 		} else {
   1534 			newentry->offset = 0;
   1535 		}
   1536 		newentry->etype = entry->etype;
   1537 		newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ?
   1538 			entry->max_protection : entry->protection;
   1539 		newentry->max_protection = entry->max_protection;
   1540 		newentry->inheritance = entry->inheritance;
   1541 		newentry->wired_count = 0;
   1542 		newentry->aref.ar_amap = entry->aref.ar_amap;
   1543 		if (newentry->aref.ar_amap) {
   1544 			newentry->aref.ar_pageoff =
   1545 			    entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT);
   1546 			uvm_map_reference_amap(newentry, AMAP_SHARED |
   1547 			    ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0));
   1548 		} else {
   1549 			newentry->aref.ar_pageoff = 0;
   1550 		}
   1551 		newentry->advice = entry->advice;
   1552 
   1553 		/* now link it on the chain */
   1554 		nchain++;
   1555 		if (endchain == NULL) {
   1556 			chain = endchain = newentry;
   1557 		} else {
   1558 			endchain->next = newentry;
   1559 			endchain = newentry;
   1560 		}
   1561 
   1562 		/* end of 'while' loop! */
   1563 		if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end &&
   1564 		    (entry->next == &srcmap->header ||
   1565 		    entry->next->start != entry->end)) {
   1566 			error = EINVAL;
   1567 			goto bad;
   1568 		}
   1569 		entry = entry->next;
   1570 		fudge = 0;
   1571 	}
   1572 
   1573 	/*
   1574 	 * step 4: close off chain (in format expected by uvm_map_replace)
   1575 	 */
   1576 
   1577 	if (chain)
   1578 		chain->prev = endchain;
   1579 
   1580 	/*
   1581 	 * step 5: attempt to lock the dest map so we can pmap_copy.
   1582 	 * note usage of copy_ok:
   1583 	 *   1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
   1584 	 *   0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
   1585 	 */
   1586 
   1587 	if (srcmap == dstmap || vm_map_lock_try(dstmap) == TRUE) {
   1588 		copy_ok = 1;
   1589 		if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
   1590 		    nchain)) {
   1591 			if (srcmap != dstmap)
   1592 				vm_map_unlock(dstmap);
   1593 			error = EIO;
   1594 			goto bad;
   1595 		}
   1596 	} else {
   1597 		copy_ok = 0;
   1598 		/* replace defered until step 7 */
   1599 	}
   1600 
   1601 	/*
   1602 	 * step 6: traverse the srcmap a second time to do the following:
   1603 	 *  - if we got a lock on the dstmap do pmap_copy
   1604 	 *  - if UVM_EXTRACT_REMOVE remove the entries
   1605 	 * we make use of orig_entry and orig_fudge (saved in step 2)
   1606 	 */
   1607 
   1608 	if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) {
   1609 
   1610 		/* purge possible stale hints from srcmap */
   1611 		if (flags & UVM_EXTRACT_REMOVE) {
   1612 			SAVE_HINT(srcmap, srcmap->hint, orig_entry->prev);
   1613 			if (srcmap->first_free->start >= start)
   1614 				srcmap->first_free = orig_entry->prev;
   1615 		}
   1616 
   1617 		entry = orig_entry;
   1618 		fudge = orig_fudge;
   1619 		deadentry = NULL;	/* for UVM_EXTRACT_REMOVE */
   1620 
   1621 		while (entry->start < end && entry != &srcmap->header) {
   1622 			if (copy_ok) {
   1623 				oldoffset = (entry->start + fudge) - start;
   1624 				elen = MIN(end, entry->end) -
   1625 				    (entry->start + fudge);
   1626 				pmap_copy(dstmap->pmap, srcmap->pmap,
   1627 				    dstaddr + oldoffset, elen,
   1628 				    entry->start + fudge);
   1629 			}
   1630 
   1631 			/* we advance "entry" in the following if statement */
   1632 			if (flags & UVM_EXTRACT_REMOVE) {
   1633 				pmap_remove(srcmap->pmap, entry->start,
   1634 						entry->end);
   1635         			oldentry = entry;	/* save entry */
   1636         			entry = entry->next;	/* advance */
   1637 				uvm_map_entry_unlink(srcmap, oldentry);
   1638 							/* add to dead list */
   1639 				oldentry->next = deadentry;
   1640 				deadentry = oldentry;
   1641       			} else {
   1642         			entry = entry->next;		/* advance */
   1643 			}
   1644 
   1645 			/* end of 'while' loop */
   1646 			fudge = 0;
   1647 		}
   1648 		pmap_update(srcmap->pmap);
   1649 
   1650 		/*
   1651 		 * unlock dstmap.  we will dispose of deadentry in
   1652 		 * step 7 if needed
   1653 		 */
   1654 
   1655 		if (copy_ok && srcmap != dstmap)
   1656 			vm_map_unlock(dstmap);
   1657 
   1658 	} else {
   1659 		deadentry = NULL;
   1660 	}
   1661 
   1662 	/*
   1663 	 * step 7: we are done with the source map, unlock.   if copy_ok
   1664 	 * is 0 then we have not replaced the dummy mapping in dstmap yet
   1665 	 * and we need to do so now.
   1666 	 */
   1667 
   1668 	vm_map_unlock(srcmap);
   1669 	if ((flags & UVM_EXTRACT_REMOVE) && deadentry)
   1670 		uvm_unmap_detach(deadentry, 0);   /* dispose of old entries */
   1671 
   1672 	/* now do the replacement if we didn't do it in step 5 */
   1673 	if (copy_ok == 0) {
   1674 		vm_map_lock(dstmap);
   1675 		error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
   1676 		    nchain);
   1677 		vm_map_unlock(dstmap);
   1678 
   1679 		if (error == FALSE) {
   1680 			error = EIO;
   1681 			goto bad2;
   1682 		}
   1683 	}
   1684 	return(0);
   1685 
   1686 	/*
   1687 	 * bad: failure recovery
   1688 	 */
   1689 bad:
   1690 	vm_map_unlock(srcmap);
   1691 bad2:			/* src already unlocked */
   1692 	if (chain)
   1693 		uvm_unmap_detach(chain,
   1694 		    (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0);
   1695 	uvm_unmap(dstmap, dstaddr, dstaddr+len);   /* ??? */
   1696 	return(error);
   1697 }
   1698 
   1699 /* end of extraction functions */
   1700 
   1701 /*
   1702  * uvm_map_submap: punch down part of a map into a submap
   1703  *
   1704  * => only the kernel_map is allowed to be submapped
   1705  * => the purpose of submapping is to break up the locking granularity
   1706  *	of a larger map
   1707  * => the range specified must have been mapped previously with a uvm_map()
   1708  *	call [with uobj==NULL] to create a blank map entry in the main map.
   1709  *	[And it had better still be blank!]
   1710  * => maps which contain submaps should never be copied or forked.
   1711  * => to remove a submap, use uvm_unmap() on the main map
   1712  *	and then uvm_map_deallocate() the submap.
   1713  * => main map must be unlocked.
   1714  * => submap must have been init'd and have a zero reference count.
   1715  *	[need not be locked as we don't actually reference it]
   1716  */
   1717 
   1718 int
   1719 uvm_map_submap(map, start, end, submap)
   1720 	struct vm_map *map, *submap;
   1721 	vaddr_t start, end;
   1722 {
   1723 	struct vm_map_entry *entry;
   1724 	int error;
   1725 
   1726 	vm_map_lock(map);
   1727 	VM_MAP_RANGE_CHECK(map, start, end);
   1728 
   1729 	if (uvm_map_lookup_entry(map, start, &entry)) {
   1730 		UVM_MAP_CLIP_START(map, entry, start);
   1731 		UVM_MAP_CLIP_END(map, entry, end);		/* to be safe */
   1732 	} else {
   1733 		entry = NULL;
   1734 	}
   1735 
   1736 	if (entry != NULL &&
   1737 	    entry->start == start && entry->end == end &&
   1738 	    entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL &&
   1739 	    !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) {
   1740 		entry->etype |= UVM_ET_SUBMAP;
   1741 		entry->object.sub_map = submap;
   1742 		entry->offset = 0;
   1743 		uvm_map_reference(submap);
   1744 		error = 0;
   1745 	} else {
   1746 		error = EINVAL;
   1747 	}
   1748 	vm_map_unlock(map);
   1749 	return error;
   1750 }
   1751 
   1752 
   1753 /*
   1754  * uvm_map_protect: change map protection
   1755  *
   1756  * => set_max means set max_protection.
   1757  * => map must be unlocked.
   1758  */
   1759 
   1760 #define MASK(entry)     (UVM_ET_ISCOPYONWRITE(entry) ? \
   1761 			 ~VM_PROT_WRITE : VM_PROT_ALL)
   1762 
   1763 int
   1764 uvm_map_protect(map, start, end, new_prot, set_max)
   1765 	struct vm_map *map;
   1766 	vaddr_t start, end;
   1767 	vm_prot_t new_prot;
   1768 	boolean_t set_max;
   1769 {
   1770 	struct vm_map_entry *current, *entry;
   1771 	int error = 0;
   1772 	UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist);
   1773 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)",
   1774 		    map, start, end, new_prot);
   1775 
   1776 	vm_map_lock(map);
   1777 	VM_MAP_RANGE_CHECK(map, start, end);
   1778 	if (uvm_map_lookup_entry(map, start, &entry)) {
   1779 		UVM_MAP_CLIP_START(map, entry, start);
   1780 	} else {
   1781 		entry = entry->next;
   1782 	}
   1783 
   1784 	/*
   1785 	 * make a first pass to check for protection violations.
   1786 	 */
   1787 
   1788 	current = entry;
   1789 	while ((current != &map->header) && (current->start < end)) {
   1790 		if (UVM_ET_ISSUBMAP(current)) {
   1791 			error = EINVAL;
   1792 			goto out;
   1793 		}
   1794 		if ((new_prot & current->max_protection) != new_prot) {
   1795 			error = EACCES;
   1796 			goto out;
   1797 		}
   1798 		current = current->next;
   1799 	}
   1800 
   1801 	/* go back and fix up protections (no need to clip this time). */
   1802 
   1803 	current = entry;
   1804 	while ((current != &map->header) && (current->start < end)) {
   1805 		vm_prot_t old_prot;
   1806 
   1807 		UVM_MAP_CLIP_END(map, current, end);
   1808 		old_prot = current->protection;
   1809 		if (set_max)
   1810 			current->protection =
   1811 			    (current->max_protection = new_prot) & old_prot;
   1812 		else
   1813 			current->protection = new_prot;
   1814 
   1815 		/*
   1816 		 * update physical map if necessary.  worry about copy-on-write
   1817 		 * here -- CHECK THIS XXX
   1818 		 */
   1819 
   1820 		if (current->protection != old_prot) {
   1821 			/* update pmap! */
   1822 			pmap_protect(map->pmap, current->start, current->end,
   1823 			    current->protection & MASK(entry));
   1824 		}
   1825 
   1826 		/*
   1827 		 * If the map is configured to lock any future mappings,
   1828 		 * wire this entry now if the old protection was VM_PROT_NONE
   1829 		 * and the new protection is not VM_PROT_NONE.
   1830 		 */
   1831 
   1832 		if ((map->flags & VM_MAP_WIREFUTURE) != 0 &&
   1833 		    VM_MAPENT_ISWIRED(entry) == 0 &&
   1834 		    old_prot == VM_PROT_NONE &&
   1835 		    new_prot != VM_PROT_NONE) {
   1836 			if (uvm_map_pageable(map, entry->start,
   1837 			    entry->end, FALSE,
   1838 			    UVM_LK_ENTER|UVM_LK_EXIT) != 0) {
   1839 
   1840 				/*
   1841 				 * If locking the entry fails, remember the
   1842 				 * error if it's the first one.  Note we
   1843 				 * still continue setting the protection in
   1844 				 * the map, but will return the error
   1845 				 * condition regardless.
   1846 				 *
   1847 				 * XXX Ignore what the actual error is,
   1848 				 * XXX just call it a resource shortage
   1849 				 * XXX so that it doesn't get confused
   1850 				 * XXX what uvm_map_protect() itself would
   1851 				 * XXX normally return.
   1852 				 */
   1853 
   1854 				error = ENOMEM;
   1855 			}
   1856 		}
   1857 		current = current->next;
   1858 	}
   1859 	pmap_update(map->pmap);
   1860 
   1861  out:
   1862 	vm_map_unlock(map);
   1863 	UVMHIST_LOG(maphist, "<- done, error=%d",error,0,0,0);
   1864 	return error;
   1865 }
   1866 
   1867 #undef  MASK
   1868 
   1869 /*
   1870  * uvm_map_inherit: set inheritance code for range of addrs in map.
   1871  *
   1872  * => map must be unlocked
   1873  * => note that the inherit code is used during a "fork".  see fork
   1874  *	code for details.
   1875  */
   1876 
   1877 int
   1878 uvm_map_inherit(map, start, end, new_inheritance)
   1879 	struct vm_map *map;
   1880 	vaddr_t start;
   1881 	vaddr_t end;
   1882 	vm_inherit_t new_inheritance;
   1883 {
   1884 	struct vm_map_entry *entry, *temp_entry;
   1885 	UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist);
   1886 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)",
   1887 	    map, start, end, new_inheritance);
   1888 
   1889 	switch (new_inheritance) {
   1890 	case MAP_INHERIT_NONE:
   1891 	case MAP_INHERIT_COPY:
   1892 	case MAP_INHERIT_SHARE:
   1893 		break;
   1894 	default:
   1895 		UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
   1896 		return EINVAL;
   1897 	}
   1898 
   1899 	vm_map_lock(map);
   1900 	VM_MAP_RANGE_CHECK(map, start, end);
   1901 	if (uvm_map_lookup_entry(map, start, &temp_entry)) {
   1902 		entry = temp_entry;
   1903 		UVM_MAP_CLIP_START(map, entry, start);
   1904 	}  else {
   1905 		entry = temp_entry->next;
   1906 	}
   1907 	while ((entry != &map->header) && (entry->start < end)) {
   1908 		UVM_MAP_CLIP_END(map, entry, end);
   1909 		entry->inheritance = new_inheritance;
   1910 		entry = entry->next;
   1911 	}
   1912 	vm_map_unlock(map);
   1913 	UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
   1914 	return 0;
   1915 }
   1916 
   1917 /*
   1918  * uvm_map_advice: set advice code for range of addrs in map.
   1919  *
   1920  * => map must be unlocked
   1921  */
   1922 
   1923 int
   1924 uvm_map_advice(map, start, end, new_advice)
   1925 	struct vm_map *map;
   1926 	vaddr_t start;
   1927 	vaddr_t end;
   1928 	int new_advice;
   1929 {
   1930 	struct vm_map_entry *entry, *temp_entry;
   1931 	UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist);
   1932 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)",
   1933 	    map, start, end, new_advice);
   1934 
   1935 	vm_map_lock(map);
   1936 	VM_MAP_RANGE_CHECK(map, start, end);
   1937 	if (uvm_map_lookup_entry(map, start, &temp_entry)) {
   1938 		entry = temp_entry;
   1939 		UVM_MAP_CLIP_START(map, entry, start);
   1940 	} else {
   1941 		entry = temp_entry->next;
   1942 	}
   1943 
   1944 	/*
   1945 	 * XXXJRT: disallow holes?
   1946 	 */
   1947 
   1948 	while ((entry != &map->header) && (entry->start < end)) {
   1949 		UVM_MAP_CLIP_END(map, entry, end);
   1950 
   1951 		switch (new_advice) {
   1952 		case MADV_NORMAL:
   1953 		case MADV_RANDOM:
   1954 		case MADV_SEQUENTIAL:
   1955 			/* nothing special here */
   1956 			break;
   1957 
   1958 		default:
   1959 			vm_map_unlock(map);
   1960 			UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
   1961 			return EINVAL;
   1962 		}
   1963 		entry->advice = new_advice;
   1964 		entry = entry->next;
   1965 	}
   1966 
   1967 	vm_map_unlock(map);
   1968 	UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
   1969 	return 0;
   1970 }
   1971 
   1972 /*
   1973  * uvm_map_pageable: sets the pageability of a range in a map.
   1974  *
   1975  * => wires map entries.  should not be used for transient page locking.
   1976  *	for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()).
   1977  * => regions sepcified as not pageable require lock-down (wired) memory
   1978  *	and page tables.
   1979  * => map must never be read-locked
   1980  * => if islocked is TRUE, map is already write-locked
   1981  * => we always unlock the map, since we must downgrade to a read-lock
   1982  *	to call uvm_fault_wire()
   1983  * => XXXCDC: check this and try and clean it up.
   1984  */
   1985 
   1986 int
   1987 uvm_map_pageable(map, start, end, new_pageable, lockflags)
   1988 	struct vm_map *map;
   1989 	vaddr_t start, end;
   1990 	boolean_t new_pageable;
   1991 	int lockflags;
   1992 {
   1993 	struct vm_map_entry *entry, *start_entry, *failed_entry;
   1994 	int rv;
   1995 #ifdef DIAGNOSTIC
   1996 	u_int timestamp_save;
   1997 #endif
   1998 	UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist);
   1999 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)",
   2000 		    map, start, end, new_pageable);
   2001 	KASSERT(map->flags & VM_MAP_PAGEABLE);
   2002 
   2003 	if ((lockflags & UVM_LK_ENTER) == 0)
   2004 		vm_map_lock(map);
   2005 	VM_MAP_RANGE_CHECK(map, start, end);
   2006 
   2007 	/*
   2008 	 * only one pageability change may take place at one time, since
   2009 	 * uvm_fault_wire assumes it will be called only once for each
   2010 	 * wiring/unwiring.  therefore, we have to make sure we're actually
   2011 	 * changing the pageability for the entire region.  we do so before
   2012 	 * making any changes.
   2013 	 */
   2014 
   2015 	if (uvm_map_lookup_entry(map, start, &start_entry) == FALSE) {
   2016 		if ((lockflags & UVM_LK_EXIT) == 0)
   2017 			vm_map_unlock(map);
   2018 
   2019 		UVMHIST_LOG(maphist,"<- done (fault)",0,0,0,0);
   2020 		return EFAULT;
   2021 	}
   2022 	entry = start_entry;
   2023 
   2024 	/*
   2025 	 * handle wiring and unwiring separately.
   2026 	 */
   2027 
   2028 	if (new_pageable) {		/* unwire */
   2029 		UVM_MAP_CLIP_START(map, entry, start);
   2030 
   2031 		/*
   2032 		 * unwiring.  first ensure that the range to be unwired is
   2033 		 * really wired down and that there are no holes.
   2034 		 */
   2035 
   2036 		while ((entry != &map->header) && (entry->start < end)) {
   2037 			if (entry->wired_count == 0 ||
   2038 			    (entry->end < end &&
   2039 			     (entry->next == &map->header ||
   2040 			      entry->next->start > entry->end))) {
   2041 				if ((lockflags & UVM_LK_EXIT) == 0)
   2042 					vm_map_unlock(map);
   2043 				UVMHIST_LOG(maphist, "<- done (INVAL)",0,0,0,0);
   2044 				return EINVAL;
   2045 			}
   2046 			entry = entry->next;
   2047 		}
   2048 
   2049 		/*
   2050 		 * POSIX 1003.1b - a single munlock call unlocks a region,
   2051 		 * regardless of the number of mlock calls made on that
   2052 		 * region.
   2053 		 */
   2054 
   2055 		entry = start_entry;
   2056 		while ((entry != &map->header) && (entry->start < end)) {
   2057 			UVM_MAP_CLIP_END(map, entry, end);
   2058 			if (VM_MAPENT_ISWIRED(entry))
   2059 				uvm_map_entry_unwire(map, entry);
   2060 			entry = entry->next;
   2061 		}
   2062 		if ((lockflags & UVM_LK_EXIT) == 0)
   2063 			vm_map_unlock(map);
   2064 		UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
   2065 		return 0;
   2066 	}
   2067 
   2068 	/*
   2069 	 * wire case: in two passes [XXXCDC: ugly block of code here]
   2070 	 *
   2071 	 * 1: holding the write lock, we create any anonymous maps that need
   2072 	 *    to be created.  then we clip each map entry to the region to
   2073 	 *    be wired and increment its wiring count.
   2074 	 *
   2075 	 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
   2076 	 *    in the pages for any newly wired area (wired_count == 1).
   2077 	 *
   2078 	 *    downgrading to a read lock for uvm_fault_wire avoids a possible
   2079 	 *    deadlock with another thread that may have faulted on one of
   2080 	 *    the pages to be wired (it would mark the page busy, blocking
   2081 	 *    us, then in turn block on the map lock that we hold).  because
   2082 	 *    of problems in the recursive lock package, we cannot upgrade
   2083 	 *    to a write lock in vm_map_lookup.  thus, any actions that
   2084 	 *    require the write lock must be done beforehand.  because we
   2085 	 *    keep the read lock on the map, the copy-on-write status of the
   2086 	 *    entries we modify here cannot change.
   2087 	 */
   2088 
   2089 	while ((entry != &map->header) && (entry->start < end)) {
   2090 		if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
   2091 
   2092 			/*
   2093 			 * perform actions of vm_map_lookup that need the
   2094 			 * write lock on the map: create an anonymous map
   2095 			 * for a copy-on-write region, or an anonymous map
   2096 			 * for a zero-fill region.  (XXXCDC: submap case
   2097 			 * ok?)
   2098 			 */
   2099 
   2100 			if (!UVM_ET_ISSUBMAP(entry)) {  /* not submap */
   2101 				if (UVM_ET_ISNEEDSCOPY(entry) &&
   2102 				    ((entry->protection & VM_PROT_WRITE) ||
   2103 				     (entry->object.uvm_obj == NULL))) {
   2104 					amap_copy(map, entry, M_WAITOK, TRUE,
   2105 					    start, end);
   2106 					/* XXXCDC: wait OK? */
   2107 				}
   2108 			}
   2109 		}
   2110 		UVM_MAP_CLIP_START(map, entry, start);
   2111 		UVM_MAP_CLIP_END(map, entry, end);
   2112 		entry->wired_count++;
   2113 
   2114 		/*
   2115 		 * Check for holes
   2116 		 */
   2117 
   2118 		if (entry->protection == VM_PROT_NONE ||
   2119 		    (entry->end < end &&
   2120 		     (entry->next == &map->header ||
   2121 		      entry->next->start > entry->end))) {
   2122 
   2123 			/*
   2124 			 * found one.  amap creation actions do not need to
   2125 			 * be undone, but the wired counts need to be restored.
   2126 			 */
   2127 
   2128 			while (entry != &map->header && entry->end > start) {
   2129 				entry->wired_count--;
   2130 				entry = entry->prev;
   2131 			}
   2132 			if ((lockflags & UVM_LK_EXIT) == 0)
   2133 				vm_map_unlock(map);
   2134 			UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0);
   2135 			return EINVAL;
   2136 		}
   2137 		entry = entry->next;
   2138 	}
   2139 
   2140 	/*
   2141 	 * Pass 2.
   2142 	 */
   2143 
   2144 #ifdef DIAGNOSTIC
   2145 	timestamp_save = map->timestamp;
   2146 #endif
   2147 	vm_map_busy(map);
   2148 	vm_map_downgrade(map);
   2149 
   2150 	rv = 0;
   2151 	entry = start_entry;
   2152 	while (entry != &map->header && entry->start < end) {
   2153 		if (entry->wired_count == 1) {
   2154 			rv = uvm_fault_wire(map, entry->start, entry->end,
   2155 			    entry->protection);
   2156 			if (rv) {
   2157 
   2158 				/*
   2159 				 * wiring failed.  break out of the loop.
   2160 				 * we'll clean up the map below, once we
   2161 				 * have a write lock again.
   2162 				 */
   2163 
   2164 				break;
   2165 			}
   2166 		}
   2167 		entry = entry->next;
   2168 	}
   2169 
   2170 	if (rv) {        /* failed? */
   2171 
   2172 		/*
   2173 		 * Get back to an exclusive (write) lock.
   2174 		 */
   2175 
   2176 		vm_map_upgrade(map);
   2177 		vm_map_unbusy(map);
   2178 
   2179 #ifdef DIAGNOSTIC
   2180 		if (timestamp_save != map->timestamp)
   2181 			panic("uvm_map_pageable: stale map");
   2182 #endif
   2183 
   2184 		/*
   2185 		 * first drop the wiring count on all the entries
   2186 		 * which haven't actually been wired yet.
   2187 		 */
   2188 
   2189 		failed_entry = entry;
   2190 		while (entry != &map->header && entry->start < end) {
   2191 			entry->wired_count--;
   2192 			entry = entry->next;
   2193 		}
   2194 
   2195 		/*
   2196 		 * now, unwire all the entries that were successfully
   2197 		 * wired above.
   2198 		 */
   2199 
   2200 		entry = start_entry;
   2201 		while (entry != failed_entry) {
   2202 			entry->wired_count--;
   2203 			if (VM_MAPENT_ISWIRED(entry) == 0)
   2204 				uvm_map_entry_unwire(map, entry);
   2205 			entry = entry->next;
   2206 		}
   2207 		if ((lockflags & UVM_LK_EXIT) == 0)
   2208 			vm_map_unlock(map);
   2209 		UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0);
   2210 		return(rv);
   2211 	}
   2212 
   2213 	/* We are holding a read lock here. */
   2214 	if ((lockflags & UVM_LK_EXIT) == 0) {
   2215 		vm_map_unbusy(map);
   2216 		vm_map_unlock_read(map);
   2217 	} else {
   2218 
   2219 		/*
   2220 		 * Get back to an exclusive (write) lock.
   2221 		 */
   2222 
   2223 		vm_map_upgrade(map);
   2224 		vm_map_unbusy(map);
   2225 	}
   2226 
   2227 	UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
   2228 	return 0;
   2229 }
   2230 
   2231 /*
   2232  * uvm_map_pageable_all: special case of uvm_map_pageable - affects
   2233  * all mapped regions.
   2234  *
   2235  * => map must not be locked.
   2236  * => if no flags are specified, all regions are unwired.
   2237  * => XXXJRT: has some of the same problems as uvm_map_pageable() above.
   2238  */
   2239 
   2240 int
   2241 uvm_map_pageable_all(map, flags, limit)
   2242 	struct vm_map *map;
   2243 	int flags;
   2244 	vsize_t limit;
   2245 {
   2246 	struct vm_map_entry *entry, *failed_entry;
   2247 	vsize_t size;
   2248 	int rv;
   2249 #ifdef DIAGNOSTIC
   2250 	u_int timestamp_save;
   2251 #endif
   2252 	UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist);
   2253 	UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0);
   2254 
   2255 	KASSERT(map->flags & VM_MAP_PAGEABLE);
   2256 
   2257 	vm_map_lock(map);
   2258 
   2259 	/*
   2260 	 * handle wiring and unwiring separately.
   2261 	 */
   2262 
   2263 	if (flags == 0) {			/* unwire */
   2264 
   2265 		/*
   2266 		 * POSIX 1003.1b -- munlockall unlocks all regions,
   2267 		 * regardless of how many times mlockall has been called.
   2268 		 */
   2269 
   2270 		for (entry = map->header.next; entry != &map->header;
   2271 		     entry = entry->next) {
   2272 			if (VM_MAPENT_ISWIRED(entry))
   2273 				uvm_map_entry_unwire(map, entry);
   2274 		}
   2275 		vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
   2276 		vm_map_unlock(map);
   2277 		UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
   2278 		return 0;
   2279 	}
   2280 
   2281 	if (flags & MCL_FUTURE) {
   2282 
   2283 		/*
   2284 		 * must wire all future mappings; remember this.
   2285 		 */
   2286 
   2287 		vm_map_modflags(map, VM_MAP_WIREFUTURE, 0);
   2288 	}
   2289 
   2290 	if ((flags & MCL_CURRENT) == 0) {
   2291 
   2292 		/*
   2293 		 * no more work to do!
   2294 		 */
   2295 
   2296 		UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0);
   2297 		vm_map_unlock(map);
   2298 		return 0;
   2299 	}
   2300 
   2301 	/*
   2302 	 * wire case: in three passes [XXXCDC: ugly block of code here]
   2303 	 *
   2304 	 * 1: holding the write lock, count all pages mapped by non-wired
   2305 	 *    entries.  if this would cause us to go over our limit, we fail.
   2306 	 *
   2307 	 * 2: still holding the write lock, we create any anonymous maps that
   2308 	 *    need to be created.  then we increment its wiring count.
   2309 	 *
   2310 	 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault
   2311 	 *    in the pages for any newly wired area (wired_count == 1).
   2312 	 *
   2313 	 *    downgrading to a read lock for uvm_fault_wire avoids a possible
   2314 	 *    deadlock with another thread that may have faulted on one of
   2315 	 *    the pages to be wired (it would mark the page busy, blocking
   2316 	 *    us, then in turn block on the map lock that we hold).  because
   2317 	 *    of problems in the recursive lock package, we cannot upgrade
   2318 	 *    to a write lock in vm_map_lookup.  thus, any actions that
   2319 	 *    require the write lock must be done beforehand.  because we
   2320 	 *    keep the read lock on the map, the copy-on-write status of the
   2321 	 *    entries we modify here cannot change.
   2322 	 */
   2323 
   2324 	for (size = 0, entry = map->header.next; entry != &map->header;
   2325 	     entry = entry->next) {
   2326 		if (entry->protection != VM_PROT_NONE &&
   2327 		    VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
   2328 			size += entry->end - entry->start;
   2329 		}
   2330 	}
   2331 
   2332 	if (atop(size) + uvmexp.wired > uvmexp.wiredmax) {
   2333 		vm_map_unlock(map);
   2334 		return ENOMEM;
   2335 	}
   2336 
   2337 	/* XXX non-pmap_wired_count case must be handled by caller */
   2338 #ifdef pmap_wired_count
   2339 	if (limit != 0 &&
   2340 	    (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) {
   2341 		vm_map_unlock(map);
   2342 		return ENOMEM;
   2343 	}
   2344 #endif
   2345 
   2346 	/*
   2347 	 * Pass 2.
   2348 	 */
   2349 
   2350 	for (entry = map->header.next; entry != &map->header;
   2351 	     entry = entry->next) {
   2352 		if (entry->protection == VM_PROT_NONE)
   2353 			continue;
   2354 		if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
   2355 
   2356 			/*
   2357 			 * perform actions of vm_map_lookup that need the
   2358 			 * write lock on the map: create an anonymous map
   2359 			 * for a copy-on-write region, or an anonymous map
   2360 			 * for a zero-fill region.  (XXXCDC: submap case
   2361 			 * ok?)
   2362 			 */
   2363 
   2364 			if (!UVM_ET_ISSUBMAP(entry)) {	/* not submap */
   2365 				if (UVM_ET_ISNEEDSCOPY(entry) &&
   2366 				    ((entry->protection & VM_PROT_WRITE) ||
   2367 				     (entry->object.uvm_obj == NULL))) {
   2368 					amap_copy(map, entry, M_WAITOK, TRUE,
   2369 					    entry->start, entry->end);
   2370 					/* XXXCDC: wait OK? */
   2371 				}
   2372 			}
   2373 		}
   2374 		entry->wired_count++;
   2375 	}
   2376 
   2377 	/*
   2378 	 * Pass 3.
   2379 	 */
   2380 
   2381 #ifdef DIAGNOSTIC
   2382 	timestamp_save = map->timestamp;
   2383 #endif
   2384 	vm_map_busy(map);
   2385 	vm_map_downgrade(map);
   2386 
   2387 	rv = 0;
   2388 	for (entry = map->header.next; entry != &map->header;
   2389 	     entry = entry->next) {
   2390 		if (entry->wired_count == 1) {
   2391 			rv = uvm_fault_wire(map, entry->start, entry->end,
   2392 			     entry->protection);
   2393 			if (rv) {
   2394 
   2395 				/*
   2396 				 * wiring failed.  break out of the loop.
   2397 				 * we'll clean up the map below, once we
   2398 				 * have a write lock again.
   2399 				 */
   2400 
   2401 				break;
   2402 			}
   2403 		}
   2404 	}
   2405 
   2406 	if (rv) {
   2407 
   2408 		/*
   2409 		 * Get back an exclusive (write) lock.
   2410 		 */
   2411 
   2412 		vm_map_upgrade(map);
   2413 		vm_map_unbusy(map);
   2414 
   2415 #ifdef DIAGNOSTIC
   2416 		if (timestamp_save != map->timestamp)
   2417 			panic("uvm_map_pageable_all: stale map");
   2418 #endif
   2419 
   2420 		/*
   2421 		 * first drop the wiring count on all the entries
   2422 		 * which haven't actually been wired yet.
   2423 		 *
   2424 		 * Skip VM_PROT_NONE entries like we did above.
   2425 		 */
   2426 
   2427 		failed_entry = entry;
   2428 		for (/* nothing */; entry != &map->header;
   2429 		     entry = entry->next) {
   2430 			if (entry->protection == VM_PROT_NONE)
   2431 				continue;
   2432 			entry->wired_count--;
   2433 		}
   2434 
   2435 		/*
   2436 		 * now, unwire all the entries that were successfully
   2437 		 * wired above.
   2438 		 *
   2439 		 * Skip VM_PROT_NONE entries like we did above.
   2440 		 */
   2441 
   2442 		for (entry = map->header.next; entry != failed_entry;
   2443 		     entry = entry->next) {
   2444 			if (entry->protection == VM_PROT_NONE)
   2445 				continue;
   2446 			entry->wired_count--;
   2447 			if (VM_MAPENT_ISWIRED(entry))
   2448 				uvm_map_entry_unwire(map, entry);
   2449 		}
   2450 		vm_map_unlock(map);
   2451 		UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0);
   2452 		return (rv);
   2453 	}
   2454 
   2455 	/* We are holding a read lock here. */
   2456 	vm_map_unbusy(map);
   2457 	vm_map_unlock_read(map);
   2458 
   2459 	UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
   2460 	return 0;
   2461 }
   2462 
   2463 /*
   2464  * uvm_map_clean: clean out a map range
   2465  *
   2466  * => valid flags:
   2467  *   if (flags & PGO_CLEANIT): dirty pages are cleaned first
   2468  *   if (flags & PGO_SYNCIO): dirty pages are written synchronously
   2469  *   if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
   2470  *   if (flags & PGO_FREE): any cached pages are freed after clean
   2471  * => returns an error if any part of the specified range isn't mapped
   2472  * => never a need to flush amap layer since the anonymous memory has
   2473  *	no permanent home, but may deactivate pages there
   2474  * => called from sys_msync() and sys_madvise()
   2475  * => caller must not write-lock map (read OK).
   2476  * => we may sleep while cleaning if SYNCIO [with map read-locked]
   2477  */
   2478 
   2479 int
   2480 uvm_map_clean(map, start, end, flags)
   2481 	struct vm_map *map;
   2482 	vaddr_t start, end;
   2483 	int flags;
   2484 {
   2485 	struct vm_map_entry *current, *entry;
   2486 	struct uvm_object *uobj;
   2487 	struct vm_amap *amap;
   2488 	struct vm_anon *anon;
   2489 	struct vm_page *pg;
   2490 	vaddr_t offset;
   2491 	vsize_t size;
   2492 	int error, refs;
   2493 	UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist);
   2494 
   2495 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)",
   2496 		    map, start, end, flags);
   2497 	KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) !=
   2498 		(PGO_FREE|PGO_DEACTIVATE));
   2499 
   2500 	vm_map_lock_read(map);
   2501 	VM_MAP_RANGE_CHECK(map, start, end);
   2502 	if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
   2503 		vm_map_unlock_read(map);
   2504 		return EFAULT;
   2505 	}
   2506 
   2507 	/*
   2508 	 * Make a first pass to check for holes.
   2509 	 */
   2510 
   2511 	for (current = entry; current->start < end; current = current->next) {
   2512 		if (UVM_ET_ISSUBMAP(current)) {
   2513 			vm_map_unlock_read(map);
   2514 			return EINVAL;
   2515 		}
   2516 		if (end <= current->end) {
   2517 			break;
   2518 		}
   2519 		if (current->end != current->next->start) {
   2520 			vm_map_unlock_read(map);
   2521 			return EFAULT;
   2522 		}
   2523 	}
   2524 
   2525 	error = 0;
   2526 	for (current = entry; start < end; current = current->next) {
   2527 		amap = current->aref.ar_amap;	/* top layer */
   2528 		uobj = current->object.uvm_obj;	/* bottom layer */
   2529 		KASSERT(start >= current->start);
   2530 
   2531 		/*
   2532 		 * No amap cleaning necessary if:
   2533 		 *
   2534 		 *	(1) There's no amap.
   2535 		 *
   2536 		 *	(2) We're not deactivating or freeing pages.
   2537 		 */
   2538 
   2539 		if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0)
   2540 			goto flush_object;
   2541 
   2542 		amap_lock(amap);
   2543 		offset = start - current->start;
   2544 		size = MIN(end, current->end) - start;
   2545 		for ( ; size != 0; size -= PAGE_SIZE, offset += PAGE_SIZE) {
   2546 			anon = amap_lookup(&current->aref, offset);
   2547 			if (anon == NULL)
   2548 				continue;
   2549 
   2550 			simple_lock(&anon->an_lock);
   2551 
   2552 			pg = anon->u.an_page;
   2553 			if (pg == NULL) {
   2554 				simple_unlock(&anon->an_lock);
   2555 				continue;
   2556 			}
   2557 
   2558 			switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
   2559 
   2560 			/*
   2561 			 * XXX In these first 3 cases, we always just
   2562 			 * XXX deactivate the page.  We may want to
   2563 			 * XXX handle the different cases more
   2564 			 * XXX specifically, in the future.
   2565 			 */
   2566 
   2567 			case PGO_CLEANIT|PGO_FREE:
   2568 			case PGO_CLEANIT|PGO_DEACTIVATE:
   2569 			case PGO_DEACTIVATE:
   2570  deactivate_it:
   2571 				/* skip the page if it's loaned or wired */
   2572 				if (pg->loan_count != 0 ||
   2573 				    pg->wire_count != 0) {
   2574 					simple_unlock(&anon->an_lock);
   2575 					continue;
   2576 				}
   2577 
   2578 				uvm_lock_pageq();
   2579 
   2580 				/*
   2581 				 * skip the page if it's not actually owned
   2582 				 * by the anon (may simply be loaned to the
   2583 				 * anon).
   2584 				 */
   2585 
   2586 				if ((pg->pqflags & PQ_ANON) == 0) {
   2587 					KASSERT(pg->uobject == NULL);
   2588 					uvm_unlock_pageq();
   2589 					simple_unlock(&anon->an_lock);
   2590 					continue;
   2591 				}
   2592 				KASSERT(pg->uanon == anon);
   2593 
   2594 				/* ...and deactivate the page. */
   2595 				pmap_clear_reference(pg);
   2596 				uvm_pagedeactivate(pg);
   2597 
   2598 				uvm_unlock_pageq();
   2599 				simple_unlock(&anon->an_lock);
   2600 				continue;
   2601 
   2602 			case PGO_FREE:
   2603 
   2604 				/*
   2605 				 * If there are multiple references to
   2606 				 * the amap, just deactivate the page.
   2607 				 */
   2608 
   2609 				if (amap_refs(amap) > 1)
   2610 					goto deactivate_it;
   2611 
   2612 				/* XXX skip the page if it's wired */
   2613 				if (pg->wire_count != 0) {
   2614 					simple_unlock(&anon->an_lock);
   2615 					continue;
   2616 				}
   2617 				amap_unadd(&current->aref, offset);
   2618 				refs = --anon->an_ref;
   2619 				simple_unlock(&anon->an_lock);
   2620 				if (refs == 0)
   2621 					uvm_anfree(anon);
   2622 				continue;
   2623 			}
   2624 		}
   2625 		amap_unlock(amap);
   2626 
   2627  flush_object:
   2628 		/*
   2629 		 * flush pages if we've got a valid backing object.
   2630 		 */
   2631 
   2632 		offset = current->offset + (start - current->start);
   2633 		size = MIN(end, current->end) - start;
   2634 		if (uobj != NULL) {
   2635 			simple_lock(&uobj->vmobjlock);
   2636 			error = (uobj->pgops->pgo_put)(uobj, offset,
   2637 			    offset + size, flags);
   2638 		}
   2639 		start += size;
   2640 	}
   2641 	vm_map_unlock_read(map);
   2642 	return (error);
   2643 }
   2644 
   2645 
   2646 /*
   2647  * uvm_map_checkprot: check protection in map
   2648  *
   2649  * => must allow specified protection in a fully allocated region.
   2650  * => map must be read or write locked by caller.
   2651  */
   2652 
   2653 boolean_t
   2654 uvm_map_checkprot(map, start, end, protection)
   2655 	struct vm_map * map;
   2656 	vaddr_t start, end;
   2657 	vm_prot_t protection;
   2658 {
   2659 	struct vm_map_entry *entry;
   2660 	struct vm_map_entry *tmp_entry;
   2661 
   2662 	if (!uvm_map_lookup_entry(map, start, &tmp_entry)) {
   2663 		return(FALSE);
   2664 	}
   2665 	entry = tmp_entry;
   2666 	while (start < end) {
   2667 		if (entry == &map->header) {
   2668 			return(FALSE);
   2669 		}
   2670 
   2671 		/*
   2672 		 * no holes allowed
   2673 		 */
   2674 
   2675 		if (start < entry->start) {
   2676 			return(FALSE);
   2677 		}
   2678 
   2679 		/*
   2680 		 * check protection associated with entry
   2681 		 */
   2682 
   2683 		if ((entry->protection & protection) != protection) {
   2684 			return(FALSE);
   2685 		}
   2686 		start = entry->end;
   2687 		entry = entry->next;
   2688 	}
   2689 	return(TRUE);
   2690 }
   2691 
   2692 /*
   2693  * uvmspace_alloc: allocate a vmspace structure.
   2694  *
   2695  * - structure includes vm_map and pmap
   2696  * - XXX: no locking on this structure
   2697  * - refcnt set to 1, rest must be init'd by caller
   2698  */
   2699 struct vmspace *
   2700 uvmspace_alloc(min, max)
   2701 	vaddr_t min, max;
   2702 {
   2703 	struct vmspace *vm;
   2704 	UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist);
   2705 
   2706 	vm = pool_get(&uvm_vmspace_pool, PR_WAITOK);
   2707 	uvmspace_init(vm, NULL, min, max);
   2708 	UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0);
   2709 	return (vm);
   2710 }
   2711 
   2712 /*
   2713  * uvmspace_init: initialize a vmspace structure.
   2714  *
   2715  * - XXX: no locking on this structure
   2716  * - refcnt set to 1, rest must me init'd by caller
   2717  */
   2718 void
   2719 uvmspace_init(vm, pmap, min, max)
   2720 	struct vmspace *vm;
   2721 	struct pmap *pmap;
   2722 	vaddr_t min, max;
   2723 {
   2724 	UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist);
   2725 
   2726 	memset(vm, 0, sizeof(*vm));
   2727 	uvm_map_setup(&vm->vm_map, min, max, VM_MAP_PAGEABLE);
   2728 	if (pmap)
   2729 		pmap_reference(pmap);
   2730 	else
   2731 		pmap = pmap_create();
   2732 	vm->vm_map.pmap = pmap;
   2733 	vm->vm_refcnt = 1;
   2734 	UVMHIST_LOG(maphist,"<- done",0,0,0,0);
   2735 }
   2736 
   2737 /*
   2738  * uvmspace_share: share a vmspace between two proceses
   2739  *
   2740  * - XXX: no locking on vmspace
   2741  * - used for vfork, threads(?)
   2742  */
   2743 
   2744 void
   2745 uvmspace_share(p1, p2)
   2746 	struct proc *p1, *p2;
   2747 {
   2748 	p2->p_vmspace = p1->p_vmspace;
   2749 	p1->p_vmspace->vm_refcnt++;
   2750 }
   2751 
   2752 /*
   2753  * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace
   2754  *
   2755  * - XXX: no locking on vmspace
   2756  */
   2757 
   2758 void
   2759 uvmspace_unshare(p)
   2760 	struct proc *p;
   2761 {
   2762 	struct vmspace *nvm, *ovm = p->p_vmspace;
   2763 
   2764 	if (ovm->vm_refcnt == 1)
   2765 		/* nothing to do: vmspace isn't shared in the first place */
   2766 		return;
   2767 
   2768 	/* make a new vmspace, still holding old one */
   2769 	nvm = uvmspace_fork(ovm);
   2770 
   2771 	pmap_deactivate(p);		/* unbind old vmspace */
   2772 	p->p_vmspace = nvm;
   2773 	pmap_activate(p);		/* switch to new vmspace */
   2774 
   2775 	uvmspace_free(ovm);		/* drop reference to old vmspace */
   2776 }
   2777 
   2778 /*
   2779  * uvmspace_exec: the process wants to exec a new program
   2780  *
   2781  * - XXX: no locking on vmspace
   2782  */
   2783 
   2784 void
   2785 uvmspace_exec(p, start, end)
   2786 	struct proc *p;
   2787 	vaddr_t start, end;
   2788 {
   2789 	struct vmspace *nvm, *ovm = p->p_vmspace;
   2790 	struct vm_map *map = &ovm->vm_map;
   2791 
   2792 #ifdef __sparc__
   2793 	/* XXX cgd 960926: the sparc #ifdef should be a MD hook */
   2794 	kill_user_windows(p);   /* before stack addresses go away */
   2795 #endif
   2796 
   2797 	/*
   2798 	 * see if more than one process is using this vmspace...
   2799 	 */
   2800 
   2801 	if (ovm->vm_refcnt == 1) {
   2802 
   2803 		/*
   2804 		 * if p is the only process using its vmspace then we can safely
   2805 		 * recycle that vmspace for the program that is being exec'd.
   2806 		 */
   2807 
   2808 #ifdef SYSVSHM
   2809 		/*
   2810 		 * SYSV SHM semantics require us to kill all segments on an exec
   2811 		 */
   2812 
   2813 		if (ovm->vm_shm)
   2814 			shmexit(ovm);
   2815 #endif
   2816 
   2817 		/*
   2818 		 * POSIX 1003.1b -- "lock future mappings" is revoked
   2819 		 * when a process execs another program image.
   2820 		 */
   2821 
   2822 		vm_map_lock(map);
   2823 		vm_map_modflags(map, 0, VM_MAP_WIREFUTURE);
   2824 		vm_map_unlock(map);
   2825 
   2826 		/*
   2827 		 * now unmap the old program
   2828 		 */
   2829 
   2830 		uvm_unmap(map, map->min_offset, map->max_offset);
   2831 
   2832 		/*
   2833 		 * resize the map
   2834 		 */
   2835 
   2836 		vm_map_lock(map);
   2837 		map->min_offset = start;
   2838 		map->max_offset = end;
   2839 		vm_map_unlock(map);
   2840 	} else {
   2841 
   2842 		/*
   2843 		 * p's vmspace is being shared, so we can't reuse it for p since
   2844 		 * it is still being used for others.   allocate a new vmspace
   2845 		 * for p
   2846 		 */
   2847 
   2848 		nvm = uvmspace_alloc(start, end);
   2849 
   2850 		/*
   2851 		 * install new vmspace and drop our ref to the old one.
   2852 		 */
   2853 
   2854 		pmap_deactivate(p);
   2855 		p->p_vmspace = nvm;
   2856 		pmap_activate(p);
   2857 
   2858 		uvmspace_free(ovm);
   2859 	}
   2860 }
   2861 
   2862 /*
   2863  * uvmspace_free: free a vmspace data structure
   2864  *
   2865  * - XXX: no locking on vmspace
   2866  */
   2867 
   2868 void
   2869 uvmspace_free(vm)
   2870 	struct vmspace *vm;
   2871 {
   2872 	struct vm_map_entry *dead_entries;
   2873 	UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist);
   2874 
   2875 	UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0);
   2876 	if (--vm->vm_refcnt == 0) {
   2877 
   2878 		/*
   2879 		 * lock the map, to wait out all other references to it.  delete
   2880 		 * all of the mappings and pages they hold, then call the pmap
   2881 		 * module to reclaim anything left.
   2882 		 */
   2883 
   2884 #ifdef SYSVSHM
   2885 		/* Get rid of any SYSV shared memory segments. */
   2886 		if (vm->vm_shm != NULL)
   2887 			shmexit(vm);
   2888 #endif
   2889 		vm_map_lock(&vm->vm_map);
   2890 		if (vm->vm_map.nentries) {
   2891 			uvm_unmap_remove(&vm->vm_map,
   2892 			    vm->vm_map.min_offset, vm->vm_map.max_offset,
   2893 			    &dead_entries);
   2894 			if (dead_entries != NULL)
   2895 				uvm_unmap_detach(dead_entries, 0);
   2896 		}
   2897 		pmap_destroy(vm->vm_map.pmap);
   2898 		vm->vm_map.pmap = NULL;
   2899 		pool_put(&uvm_vmspace_pool, vm);
   2900 	}
   2901 	UVMHIST_LOG(maphist,"<- done", 0,0,0,0);
   2902 }
   2903 
   2904 /*
   2905  *   F O R K   -   m a i n   e n t r y   p o i n t
   2906  */
   2907 /*
   2908  * uvmspace_fork: fork a process' main map
   2909  *
   2910  * => create a new vmspace for child process from parent.
   2911  * => parent's map must not be locked.
   2912  */
   2913 
   2914 struct vmspace *
   2915 uvmspace_fork(vm1)
   2916 	struct vmspace *vm1;
   2917 {
   2918 	struct vmspace *vm2;
   2919 	struct vm_map *old_map = &vm1->vm_map;
   2920 	struct vm_map *new_map;
   2921 	struct vm_map_entry *old_entry;
   2922 	struct vm_map_entry *new_entry;
   2923 	pmap_t new_pmap;
   2924 	boolean_t protect_child;
   2925 	UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist);
   2926 
   2927 	vm_map_lock(old_map);
   2928 
   2929 	vm2 = uvmspace_alloc(old_map->min_offset, old_map->max_offset);
   2930 	memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy,
   2931 	(caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy);
   2932 	new_map = &vm2->vm_map;		  /* XXX */
   2933 	new_pmap = new_map->pmap;
   2934 
   2935 	old_entry = old_map->header.next;
   2936 
   2937 	/*
   2938 	 * go entry-by-entry
   2939 	 */
   2940 
   2941 	while (old_entry != &old_map->header) {
   2942 
   2943 		/*
   2944 		 * first, some sanity checks on the old entry
   2945 		 */
   2946 
   2947 		KASSERT(!UVM_ET_ISSUBMAP(old_entry));
   2948 		KASSERT(UVM_ET_ISCOPYONWRITE(old_entry) ||
   2949 			!UVM_ET_ISNEEDSCOPY(old_entry));
   2950 
   2951 		switch (old_entry->inheritance) {
   2952 		case MAP_INHERIT_NONE:
   2953 
   2954 			/*
   2955 			 * drop the mapping
   2956 			 */
   2957 
   2958 			break;
   2959 
   2960 		case MAP_INHERIT_SHARE:
   2961 
   2962 			/*
   2963 			 * share the mapping: this means we want the old and
   2964 			 * new entries to share amaps and backing objects.
   2965 			 */
   2966 			/*
   2967 			 * if the old_entry needs a new amap (due to prev fork)
   2968 			 * then we need to allocate it now so that we have
   2969 			 * something we own to share with the new_entry.   [in
   2970 			 * other words, we need to clear needs_copy]
   2971 			 */
   2972 
   2973 			if (UVM_ET_ISNEEDSCOPY(old_entry)) {
   2974 				/* get our own amap, clears needs_copy */
   2975 				amap_copy(old_map, old_entry, M_WAITOK, FALSE,
   2976 				    0, 0);
   2977 				/* XXXCDC: WAITOK??? */
   2978 			}
   2979 
   2980 			new_entry = uvm_mapent_alloc(new_map);
   2981 			/* old_entry -> new_entry */
   2982 			uvm_mapent_copy(old_entry, new_entry);
   2983 
   2984 			/* new pmap has nothing wired in it */
   2985 			new_entry->wired_count = 0;
   2986 
   2987 			/*
   2988 			 * gain reference to object backing the map (can't
   2989 			 * be a submap, already checked this case).
   2990 			 */
   2991 
   2992 			if (new_entry->aref.ar_amap)
   2993 				uvm_map_reference_amap(new_entry, AMAP_SHARED);
   2994 
   2995 			if (new_entry->object.uvm_obj &&
   2996 			    new_entry->object.uvm_obj->pgops->pgo_reference)
   2997 				new_entry->object.uvm_obj->
   2998 				    pgops->pgo_reference(
   2999 				        new_entry->object.uvm_obj);
   3000 
   3001 			/* insert entry at end of new_map's entry list */
   3002 			uvm_map_entry_link(new_map, new_map->header.prev,
   3003 			    new_entry);
   3004 
   3005 			/*
   3006 			 * pmap_copy the mappings: this routine is optional
   3007 			 * but if it is there it will reduce the number of
   3008 			 * page faults in the new proc.
   3009 			 */
   3010 
   3011 			pmap_copy(new_pmap, old_map->pmap, new_entry->start,
   3012 			    (old_entry->end - old_entry->start),
   3013 			    old_entry->start);
   3014 
   3015 			break;
   3016 
   3017 		case MAP_INHERIT_COPY:
   3018 
   3019 			/*
   3020 			 * copy-on-write the mapping (using mmap's
   3021 			 * MAP_PRIVATE semantics)
   3022 			 *
   3023 			 * allocate new_entry, adjust reference counts.
   3024 			 * (note that new references are read-only).
   3025 			 */
   3026 
   3027 			new_entry = uvm_mapent_alloc(new_map);
   3028 			/* old_entry -> new_entry */
   3029 			uvm_mapent_copy(old_entry, new_entry);
   3030 
   3031 			if (new_entry->aref.ar_amap)
   3032 				uvm_map_reference_amap(new_entry, 0);
   3033 
   3034 			if (new_entry->object.uvm_obj &&
   3035 			    new_entry->object.uvm_obj->pgops->pgo_reference)
   3036 				new_entry->object.uvm_obj->pgops->pgo_reference
   3037 				    (new_entry->object.uvm_obj);
   3038 
   3039 			/* new pmap has nothing wired in it */
   3040 			new_entry->wired_count = 0;
   3041 
   3042 			new_entry->etype |=
   3043 			    (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY);
   3044 			uvm_map_entry_link(new_map, new_map->header.prev,
   3045 			    new_entry);
   3046 
   3047 			/*
   3048 			 * the new entry will need an amap.  it will either
   3049 			 * need to be copied from the old entry or created
   3050 			 * from scratch (if the old entry does not have an
   3051 			 * amap).  can we defer this process until later
   3052 			 * (by setting "needs_copy") or do we need to copy
   3053 			 * the amap now?
   3054 			 *
   3055 			 * we must copy the amap now if any of the following
   3056 			 * conditions hold:
   3057 			 * 1. the old entry has an amap and that amap is
   3058 			 *    being shared.  this means that the old (parent)
   3059 			 *    process is sharing the amap with another
   3060 			 *    process.  if we do not clear needs_copy here
   3061 			 *    we will end up in a situation where both the
   3062 			 *    parent and child process are refering to the
   3063 			 *    same amap with "needs_copy" set.  if the
   3064 			 *    parent write-faults, the fault routine will
   3065 			 *    clear "needs_copy" in the parent by allocating
   3066 			 *    a new amap.   this is wrong because the
   3067 			 *    parent is supposed to be sharing the old amap
   3068 			 *    and the new amap will break that.
   3069 			 *
   3070 			 * 2. if the old entry has an amap and a non-zero
   3071 			 *    wire count then we are going to have to call
   3072 			 *    amap_cow_now to avoid page faults in the
   3073 			 *    parent process.   since amap_cow_now requires
   3074 			 *    "needs_copy" to be clear we might as well
   3075 			 *    clear it here as well.
   3076 			 *
   3077 			 */
   3078 
   3079 			if (old_entry->aref.ar_amap != NULL) {
   3080 				if ((amap_flags(old_entry->aref.ar_amap) &
   3081 				     AMAP_SHARED) != 0 ||
   3082 				    VM_MAPENT_ISWIRED(old_entry)) {
   3083 
   3084 					amap_copy(new_map, new_entry, M_WAITOK,
   3085 					    FALSE, 0, 0);
   3086 					/* XXXCDC: M_WAITOK ... ok? */
   3087 				}
   3088 			}
   3089 
   3090 			/*
   3091 			 * if the parent's entry is wired down, then the
   3092 			 * parent process does not want page faults on
   3093 			 * access to that memory.  this means that we
   3094 			 * cannot do copy-on-write because we can't write
   3095 			 * protect the old entry.   in this case we
   3096 			 * resolve all copy-on-write faults now, using
   3097 			 * amap_cow_now.   note that we have already
   3098 			 * allocated any needed amap (above).
   3099 			 */
   3100 
   3101 			if (VM_MAPENT_ISWIRED(old_entry)) {
   3102 
   3103 			  /*
   3104 			   * resolve all copy-on-write faults now
   3105 			   * (note that there is nothing to do if
   3106 			   * the old mapping does not have an amap).
   3107 			   * XXX: is it worthwhile to bother with pmap_copy
   3108 			   * in this case?
   3109 			   */
   3110 			  if (old_entry->aref.ar_amap)
   3111 			    amap_cow_now(new_map, new_entry);
   3112 
   3113 			} else {
   3114 
   3115 			  /*
   3116 			   * setup mappings to trigger copy-on-write faults
   3117 			   * we must write-protect the parent if it has
   3118 			   * an amap and it is not already "needs_copy"...
   3119 			   * if it is already "needs_copy" then the parent
   3120 			   * has already been write-protected by a previous
   3121 			   * fork operation.
   3122 			   *
   3123 			   * if we do not write-protect the parent, then
   3124 			   * we must be sure to write-protect the child
   3125 			   * after the pmap_copy() operation.
   3126 			   *
   3127 			   * XXX: pmap_copy should have some way of telling
   3128 			   * us that it didn't do anything so we can avoid
   3129 			   * calling pmap_protect needlessly.
   3130 			   */
   3131 
   3132 			  if (old_entry->aref.ar_amap) {
   3133 
   3134 			    if (!UVM_ET_ISNEEDSCOPY(old_entry)) {
   3135 			      if (old_entry->max_protection & VM_PROT_WRITE) {
   3136 				pmap_protect(old_map->pmap,
   3137 					     old_entry->start,
   3138 					     old_entry->end,
   3139 					     old_entry->protection &
   3140 					     ~VM_PROT_WRITE);
   3141 				pmap_update(old_map->pmap);
   3142 			      }
   3143 			      old_entry->etype |= UVM_ET_NEEDSCOPY;
   3144 			    }
   3145 
   3146 			    /*
   3147 			     * parent must now be write-protected
   3148 			     */
   3149 			    protect_child = FALSE;
   3150 			  } else {
   3151 
   3152 			    /*
   3153 			     * we only need to protect the child if the
   3154 			     * parent has write access.
   3155 			     */
   3156 			    if (old_entry->max_protection & VM_PROT_WRITE)
   3157 			      protect_child = TRUE;
   3158 			    else
   3159 			      protect_child = FALSE;
   3160 
   3161 			  }
   3162 
   3163 			  /*
   3164 			   * copy the mappings
   3165 			   * XXX: need a way to tell if this does anything
   3166 			   */
   3167 
   3168 			  pmap_copy(new_pmap, old_map->pmap,
   3169 				    new_entry->start,
   3170 				    (old_entry->end - old_entry->start),
   3171 				    old_entry->start);
   3172 
   3173 			  /*
   3174 			   * protect the child's mappings if necessary
   3175 			   */
   3176 			  if (protect_child) {
   3177 			    pmap_protect(new_pmap, new_entry->start,
   3178 					 new_entry->end,
   3179 					 new_entry->protection &
   3180 					          ~VM_PROT_WRITE);
   3181 			    pmap_update(new_pmap);
   3182 			  }
   3183 
   3184 			}
   3185 			break;
   3186 		}  /* end of switch statement */
   3187 		old_entry = old_entry->next;
   3188 	}
   3189 
   3190 	new_map->size = old_map->size;
   3191 	vm_map_unlock(old_map);
   3192 
   3193 #ifdef SYSVSHM
   3194 	if (vm1->vm_shm)
   3195 		shmfork(vm1, vm2);
   3196 #endif
   3197 
   3198 #ifdef PMAP_FORK
   3199 	pmap_fork(vm1->vm_map.pmap, vm2->vm_map.pmap);
   3200 #endif
   3201 
   3202 	UVMHIST_LOG(maphist,"<- done",0,0,0,0);
   3203 	return(vm2);
   3204 }
   3205 
   3206 
   3207 #if defined(DDB)
   3208 
   3209 /*
   3210  * DDB hooks
   3211  */
   3212 
   3213 /*
   3214  * uvm_map_printit: actually prints the map
   3215  */
   3216 
   3217 void
   3218 uvm_map_printit(map, full, pr)
   3219 	struct vm_map *map;
   3220 	boolean_t full;
   3221 	void (*pr) __P((const char *, ...));
   3222 {
   3223 	struct vm_map_entry *entry;
   3224 
   3225 	(*pr)("MAP %p: [0x%lx->0x%lx]\n", map, map->min_offset,map->max_offset);
   3226 	(*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n",
   3227 	    map->nentries, map->size, map->ref_count, map->timestamp,
   3228 	    map->flags);
   3229 #ifdef pmap_resident_count
   3230 	(*pr)("\tpmap=%p(resident=%d)\n", map->pmap,
   3231 	    pmap_resident_count(map->pmap));
   3232 #else
   3233 	/* XXXCDC: this should be required ... */
   3234 	(*pr)("\tpmap=%p(resident=<<NOT SUPPORTED!!!>>)\n", map->pmap);
   3235 #endif
   3236 	if (!full)
   3237 		return;
   3238 	for (entry = map->header.next; entry != &map->header;
   3239 	    entry = entry->next) {
   3240 		(*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n",
   3241 		    entry, entry->start, entry->end, entry->object.uvm_obj,
   3242 		    (long long)entry->offset, entry->aref.ar_amap,
   3243 		    entry->aref.ar_pageoff);
   3244 		(*pr)(
   3245 		    "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, "
   3246 		    "wc=%d, adv=%d\n",
   3247 		    (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
   3248 		    (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
   3249 		    (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
   3250 		    entry->protection, entry->max_protection,
   3251 		    entry->inheritance, entry->wired_count, entry->advice);
   3252 	}
   3253 }
   3254 
   3255 /*
   3256  * uvm_object_printit: actually prints the object
   3257  */
   3258 
   3259 void
   3260 uvm_object_printit(uobj, full, pr)
   3261 	struct uvm_object *uobj;
   3262 	boolean_t full;
   3263 	void (*pr) __P((const char *, ...));
   3264 {
   3265 	struct vm_page *pg;
   3266 	int cnt = 0;
   3267 
   3268 	(*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ",
   3269 	    uobj, uobj->vmobjlock.lock_data, uobj->pgops, uobj->uo_npages);
   3270 	if (UVM_OBJ_IS_KERN_OBJECT(uobj))
   3271 		(*pr)("refs=<SYSTEM>\n");
   3272 	else
   3273 		(*pr)("refs=%d\n", uobj->uo_refs);
   3274 
   3275 	if (!full) {
   3276 		return;
   3277 	}
   3278 	(*pr)("  PAGES <pg,offset>:\n  ");
   3279 	TAILQ_FOREACH(pg, &uobj->memq, listq) {
   3280 		cnt++;
   3281 		(*pr)("<%p,0x%llx> ", pg, (long long)pg->offset);
   3282 		if ((cnt % 3) == 0) {
   3283 			(*pr)("\n  ");
   3284 		}
   3285 	}
   3286 	if ((cnt % 3) != 0) {
   3287 		(*pr)("\n");
   3288 	}
   3289 }
   3290 
   3291 /*
   3292  * uvm_page_printit: actually print the page
   3293  */
   3294 
   3295 static const char page_flagbits[] =
   3296 	"\20\1BUSY\2WANTED\3TABLED\4CLEAN\5PAGEOUT\6RELEASED\7FAKE\10RDONLY"
   3297 	"\11ZERO\15PAGER1";
   3298 static const char page_pqflagbits[] =
   3299 	"\20\1FREE\2INACTIVE\3ACTIVE\5ANON\6AOBJ";
   3300 
   3301 void
   3302 uvm_page_printit(pg, full, pr)
   3303 	struct vm_page *pg;
   3304 	boolean_t full;
   3305 	void (*pr) __P((const char *, ...));
   3306 {
   3307 	struct vm_page *tpg;
   3308 	struct uvm_object *uobj;
   3309 	struct pglist *pgl;
   3310 	char pgbuf[128];
   3311 	char pqbuf[128];
   3312 
   3313 	(*pr)("PAGE %p:\n", pg);
   3314 	bitmask_snprintf(pg->flags, page_flagbits, pgbuf, sizeof(pgbuf));
   3315 	bitmask_snprintf(pg->pqflags, page_pqflagbits, pqbuf, sizeof(pqbuf));
   3316 	(*pr)("  flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n",
   3317 	    pgbuf, pqbuf, pg->wire_count, (long)pg->phys_addr);
   3318 	(*pr)("  uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n",
   3319 	    pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count);
   3320 #if defined(UVM_PAGE_TRKOWN)
   3321 	if (pg->flags & PG_BUSY)
   3322 		(*pr)("  owning process = %d, tag=%s\n",
   3323 		    pg->owner, pg->owner_tag);
   3324 	else
   3325 		(*pr)("  page not busy, no owner\n");
   3326 #else
   3327 	(*pr)("  [page ownership tracking disabled]\n");
   3328 #endif
   3329 
   3330 	if (!full)
   3331 		return;
   3332 
   3333 	/* cross-verify object/anon */
   3334 	if ((pg->pqflags & PQ_FREE) == 0) {
   3335 		if (pg->pqflags & PQ_ANON) {
   3336 			if (pg->uanon == NULL || pg->uanon->u.an_page != pg)
   3337 			    (*pr)("  >>> ANON DOES NOT POINT HERE <<< (%p)\n",
   3338 				(pg->uanon) ? pg->uanon->u.an_page : NULL);
   3339 			else
   3340 				(*pr)("  anon backpointer is OK\n");
   3341 		} else {
   3342 			uobj = pg->uobject;
   3343 			if (uobj) {
   3344 				(*pr)("  checking object list\n");
   3345 				TAILQ_FOREACH(tpg, &uobj->memq, listq) {
   3346 					if (tpg == pg) {
   3347 						break;
   3348 					}
   3349 				}
   3350 				if (tpg)
   3351 					(*pr)("  page found on object list\n");
   3352 				else
   3353 			(*pr)("  >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
   3354 			}
   3355 		}
   3356 	}
   3357 
   3358 	/* cross-verify page queue */
   3359 	if (pg->pqflags & PQ_FREE) {
   3360 		int fl = uvm_page_lookup_freelist(pg);
   3361 		int color = VM_PGCOLOR_BUCKET(pg);
   3362 		pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[
   3363 		    ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN];
   3364 	} else if (pg->pqflags & PQ_INACTIVE) {
   3365 		pgl = &uvm.page_inactive;
   3366 	} else if (pg->pqflags & PQ_ACTIVE) {
   3367 		pgl = &uvm.page_active;
   3368  	} else {
   3369 		pgl = NULL;
   3370 	}
   3371 
   3372 	if (pgl) {
   3373 		(*pr)("  checking pageq list\n");
   3374 		TAILQ_FOREACH(tpg, pgl, pageq) {
   3375 			if (tpg == pg) {
   3376 				break;
   3377 			}
   3378 		}
   3379 		if (tpg)
   3380 			(*pr)("  page found on pageq list\n");
   3381 		else
   3382 			(*pr)("  >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
   3383 	}
   3384 }
   3385 #endif
   3386