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