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