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