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