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