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