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