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