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