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