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