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