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