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