xref: /netbsd-src/sys/uvm/uvm_map.c (revision daf6c4152fcddc27c445489775ed1f66ab4ea9a9)
1 /*	$NetBSD: uvm_map.c,v 1.295 2011/02/02 15:25:27 chuck 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. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)vm_map.c    8.3 (Berkeley) 1/12/94
37  * from: Id: uvm_map.c,v 1.1.2.27 1998/02/07 01:16:54 chs Exp
38  *
39  *
40  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
41  * All rights reserved.
42  *
43  * Permission to use, copy, modify and distribute this software and
44  * its documentation is hereby granted, provided that both the copyright
45  * notice and this permission notice appear in all copies of the
46  * software, derivative works or modified versions, and any portions
47  * thereof, and that both notices appear in supporting documentation.
48  *
49  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
50  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
51  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52  *
53  * Carnegie Mellon requests users of this software to return to
54  *
55  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
56  *  School of Computer Science
57  *  Carnegie Mellon University
58  *  Pittsburgh PA 15213-3890
59  *
60  * any improvements or extensions that they make and grant Carnegie the
61  * rights to redistribute these changes.
62  */
63 
64 /*
65  * uvm_map.c: uvm map operations
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.295 2011/02/02 15:25:27 chuck Exp $");
70 
71 #include "opt_ddb.h"
72 #include "opt_uvmhist.h"
73 #include "opt_uvm.h"
74 #include "opt_sysv.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/mman.h>
79 #include <sys/proc.h>
80 #include <sys/malloc.h>
81 #include <sys/pool.h>
82 #include <sys/kernel.h>
83 #include <sys/mount.h>
84 #include <sys/vnode.h>
85 #include <sys/lockdebug.h>
86 #include <sys/atomic.h>
87 #ifndef __USER_VA0_IS_SAFE
88 #include <sys/sysctl.h>
89 #include <sys/kauth.h>
90 #include "opt_user_va0_disable_default.h"
91 #endif
92 
93 #ifdef SYSVSHM
94 #include <sys/shm.h>
95 #endif
96 
97 #include <uvm/uvm.h>
98 #include <uvm/uvm_readahead.h>
99 
100 #if defined(DDB) || defined(DEBUGPRINT)
101 #include <uvm/uvm_ddb.h>
102 #endif
103 
104 #if !defined(UVMMAP_COUNTERS)
105 
106 #define	UVMMAP_EVCNT_DEFINE(name)	/* nothing */
107 #define UVMMAP_EVCNT_INCR(ev)		/* nothing */
108 #define UVMMAP_EVCNT_DECR(ev)		/* nothing */
109 
110 #else /* defined(UVMMAP_NOCOUNTERS) */
111 
112 #include <sys/evcnt.h>
113 #define	UVMMAP_EVCNT_DEFINE(name) \
114 struct evcnt uvmmap_evcnt_##name = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, \
115     "uvmmap", #name); \
116 EVCNT_ATTACH_STATIC(uvmmap_evcnt_##name);
117 #define	UVMMAP_EVCNT_INCR(ev)		uvmmap_evcnt_##ev.ev_count++
118 #define	UVMMAP_EVCNT_DECR(ev)		uvmmap_evcnt_##ev.ev_count--
119 
120 #endif /* defined(UVMMAP_NOCOUNTERS) */
121 
122 UVMMAP_EVCNT_DEFINE(ubackmerge)
123 UVMMAP_EVCNT_DEFINE(uforwmerge)
124 UVMMAP_EVCNT_DEFINE(ubimerge)
125 UVMMAP_EVCNT_DEFINE(unomerge)
126 UVMMAP_EVCNT_DEFINE(kbackmerge)
127 UVMMAP_EVCNT_DEFINE(kforwmerge)
128 UVMMAP_EVCNT_DEFINE(kbimerge)
129 UVMMAP_EVCNT_DEFINE(knomerge)
130 UVMMAP_EVCNT_DEFINE(map_call)
131 UVMMAP_EVCNT_DEFINE(mlk_call)
132 UVMMAP_EVCNT_DEFINE(mlk_hint)
133 UVMMAP_EVCNT_DEFINE(mlk_list)
134 UVMMAP_EVCNT_DEFINE(mlk_tree)
135 UVMMAP_EVCNT_DEFINE(mlk_treeloop)
136 UVMMAP_EVCNT_DEFINE(mlk_listloop)
137 
138 UVMMAP_EVCNT_DEFINE(uke_alloc)
139 UVMMAP_EVCNT_DEFINE(uke_free)
140 UVMMAP_EVCNT_DEFINE(ukh_alloc)
141 UVMMAP_EVCNT_DEFINE(ukh_free)
142 
143 const char vmmapbsy[] = "vmmapbsy";
144 
145 /*
146  * cache for vmspace structures.
147  */
148 
149 static struct pool_cache uvm_vmspace_cache;
150 
151 /*
152  * cache for dynamically-allocated map entries.
153  */
154 
155 static struct pool_cache uvm_map_entry_cache;
156 
157 MALLOC_DEFINE(M_VMMAP, "VM map", "VM map structures");
158 MALLOC_DEFINE(M_VMPMAP, "VM pmap", "VM pmap");
159 
160 #ifdef PMAP_GROWKERNEL
161 /*
162  * This global represents the end of the kernel virtual address
163  * space.  If we want to exceed this, we must grow the kernel
164  * virtual address space dynamically.
165  *
166  * Note, this variable is locked by kernel_map's lock.
167  */
168 vaddr_t uvm_maxkaddr;
169 #endif
170 
171 #ifndef __USER_VA0_IS_SAFE
172 #ifndef __USER_VA0_DISABLE_DEFAULT
173 #define __USER_VA0_DISABLE_DEFAULT 1
174 #endif
175 #ifdef USER_VA0_DISABLE_DEFAULT /* kernel config option overrides */
176 #undef __USER_VA0_DISABLE_DEFAULT
177 #define __USER_VA0_DISABLE_DEFAULT USER_VA0_DISABLE_DEFAULT
178 #endif
179 static int user_va0_disable = __USER_VA0_DISABLE_DEFAULT;
180 #endif
181 
182 /*
183  * macros
184  */
185 
186 /*
187  * VM_MAP_USE_KMAPENT: determine if uvm_kmapent_alloc/free is used
188  * for the vm_map.
189  */
190 extern struct vm_map *pager_map; /* XXX */
191 #define	VM_MAP_USE_KMAPENT_FLAGS(flags) \
192 	(((flags) & VM_MAP_INTRSAFE) != 0)
193 #define	VM_MAP_USE_KMAPENT(map) \
194 	(VM_MAP_USE_KMAPENT_FLAGS((map)->flags) || (map) == kernel_map)
195 
196 /*
197  * UVM_ET_ISCOMPATIBLE: check some requirements for map entry merging
198  */
199 
200 #define	UVM_ET_ISCOMPATIBLE(ent, type, uobj, meflags, \
201     prot, maxprot, inh, adv, wire) \
202 	((ent)->etype == (type) && \
203 	(((ent)->flags ^ (meflags)) & (UVM_MAP_NOMERGE | UVM_MAP_QUANTUM)) \
204 	== 0 && \
205 	(ent)->object.uvm_obj == (uobj) && \
206 	(ent)->protection == (prot) && \
207 	(ent)->max_protection == (maxprot) && \
208 	(ent)->inheritance == (inh) && \
209 	(ent)->advice == (adv) && \
210 	(ent)->wired_count == (wire))
211 
212 /*
213  * uvm_map_entry_link: insert entry into a map
214  *
215  * => map must be locked
216  */
217 #define uvm_map_entry_link(map, after_where, entry) do { \
218 	uvm_mapent_check(entry); \
219 	(map)->nentries++; \
220 	(entry)->prev = (after_where); \
221 	(entry)->next = (after_where)->next; \
222 	(entry)->prev->next = (entry); \
223 	(entry)->next->prev = (entry); \
224 	uvm_rb_insert((map), (entry)); \
225 } while (/*CONSTCOND*/ 0)
226 
227 /*
228  * uvm_map_entry_unlink: remove entry from a map
229  *
230  * => map must be locked
231  */
232 #define uvm_map_entry_unlink(map, entry) do { \
233 	KASSERT((entry) != (map)->first_free); \
234 	KASSERT((entry) != (map)->hint); \
235 	uvm_mapent_check(entry); \
236 	(map)->nentries--; \
237 	(entry)->next->prev = (entry)->prev; \
238 	(entry)->prev->next = (entry)->next; \
239 	uvm_rb_remove((map), (entry)); \
240 } while (/*CONSTCOND*/ 0)
241 
242 /*
243  * SAVE_HINT: saves the specified entry as the hint for future lookups.
244  *
245  * => map need not be locked.
246  */
247 #define SAVE_HINT(map, check, value) do { \
248 	if ((map)->hint == (check)) \
249 		(map)->hint = (value); \
250 } while (/*CONSTCOND*/ 0)
251 
252 /*
253  * clear_hints: ensure that hints don't point to the entry.
254  *
255  * => map must be write-locked.
256  */
257 static void
258 clear_hints(struct vm_map *map, struct vm_map_entry *ent)
259 {
260 
261 	SAVE_HINT(map, ent, ent->prev);
262 	if (map->first_free == ent) {
263 		map->first_free = ent->prev;
264 	}
265 }
266 
267 /*
268  * VM_MAP_RANGE_CHECK: check and correct range
269  *
270  * => map must at least be read locked
271  */
272 
273 #define VM_MAP_RANGE_CHECK(map, start, end) do { \
274 	if (start < vm_map_min(map))		\
275 		start = vm_map_min(map);	\
276 	if (end > vm_map_max(map))		\
277 		end = vm_map_max(map);		\
278 	if (start > end)			\
279 		start = end;			\
280 } while (/*CONSTCOND*/ 0)
281 
282 /*
283  * local prototypes
284  */
285 
286 static struct vm_map_entry *
287 		uvm_mapent_alloc(struct vm_map *, int);
288 static struct vm_map_entry *
289 		uvm_mapent_alloc_split(struct vm_map *,
290 		    const struct vm_map_entry *, int,
291 		    struct uvm_mapent_reservation *);
292 static void	uvm_mapent_copy(struct vm_map_entry *, struct vm_map_entry *);
293 static void	uvm_mapent_free(struct vm_map_entry *);
294 #if defined(DEBUG)
295 static void	_uvm_mapent_check(const struct vm_map_entry *, const char *,
296 		    int);
297 #define	uvm_mapent_check(map)	_uvm_mapent_check(map, __FILE__, __LINE__)
298 #else /* defined(DEBUG) */
299 #define	uvm_mapent_check(e)	/* nothing */
300 #endif /* defined(DEBUG) */
301 static struct vm_map_entry *
302 		uvm_kmapent_alloc(struct vm_map *, int);
303 static void	uvm_kmapent_free(struct vm_map_entry *);
304 static vsize_t	uvm_kmapent_overhead(vsize_t);
305 
306 static void	uvm_map_entry_unwire(struct vm_map *, struct vm_map_entry *);
307 static void	uvm_map_reference_amap(struct vm_map_entry *, int);
308 static int	uvm_map_space_avail(vaddr_t *, vsize_t, voff_t, vsize_t, int,
309 		    struct vm_map_entry *);
310 static void	uvm_map_unreference_amap(struct vm_map_entry *, int);
311 
312 int _uvm_map_sanity(struct vm_map *);
313 int _uvm_tree_sanity(struct vm_map *);
314 static vsize_t uvm_rb_maxgap(const struct vm_map_entry *);
315 
316 #define	ROOT_ENTRY(map)		((struct vm_map_entry *)(map)->rb_tree.rbt_root)
317 #define	LEFT_ENTRY(entry)	((struct vm_map_entry *)(entry)->rb_node.rb_left)
318 #define	RIGHT_ENTRY(entry)	((struct vm_map_entry *)(entry)->rb_node.rb_right)
319 #define	PARENT_ENTRY(map, entry) \
320 	(ROOT_ENTRY(map) == (entry) \
321 	    ? NULL : (struct vm_map_entry *)RB_FATHER(&(entry)->rb_node))
322 
323 static int
324 uvm_map_compare_nodes(void *ctx, const void *nparent, const void *nkey)
325 {
326 	const struct vm_map_entry *eparent = nparent;
327 	const struct vm_map_entry *ekey = nkey;
328 
329 	KASSERT(eparent->start < ekey->start || eparent->start >= ekey->end);
330 	KASSERT(ekey->start < eparent->start || ekey->start >= eparent->end);
331 
332 	if (eparent->start < ekey->start)
333 		return -1;
334 	if (eparent->end >= ekey->start)
335 		return 1;
336 	return 0;
337 }
338 
339 static int
340 uvm_map_compare_key(void *ctx, const void *nparent, const void *vkey)
341 {
342 	const struct vm_map_entry *eparent = nparent;
343 	const vaddr_t va = *(const vaddr_t *) vkey;
344 
345 	if (eparent->start < va)
346 		return -1;
347 	if (eparent->end >= va)
348 		return 1;
349 	return 0;
350 }
351 
352 static const rb_tree_ops_t uvm_map_tree_ops = {
353 	.rbto_compare_nodes = uvm_map_compare_nodes,
354 	.rbto_compare_key = uvm_map_compare_key,
355 	.rbto_node_offset = offsetof(struct vm_map_entry, rb_node),
356 	.rbto_context = NULL
357 };
358 
359 /*
360  * uvm_rb_gap: return the gap size between our entry and next entry.
361  */
362 static inline vsize_t
363 uvm_rb_gap(const struct vm_map_entry *entry)
364 {
365 
366 	KASSERT(entry->next != NULL);
367 	return entry->next->start - entry->end;
368 }
369 
370 static vsize_t
371 uvm_rb_maxgap(const struct vm_map_entry *entry)
372 {
373 	struct vm_map_entry *child;
374 	vsize_t maxgap = entry->gap;
375 
376 	/*
377 	 * We need maxgap to be the largest gap of us or any of our
378 	 * descendents.  Since each of our children's maxgap is the
379 	 * cached value of their largest gap of themselves or their
380 	 * descendents, we can just use that value and avoid recursing
381 	 * down the tree to calculate it.
382 	 */
383 	if ((child = LEFT_ENTRY(entry)) != NULL && maxgap < child->maxgap)
384 		maxgap = child->maxgap;
385 
386 	if ((child = RIGHT_ENTRY(entry)) != NULL && maxgap < child->maxgap)
387 		maxgap = child->maxgap;
388 
389 	return maxgap;
390 }
391 
392 static void
393 uvm_rb_fixup(struct vm_map *map, struct vm_map_entry *entry)
394 {
395 	struct vm_map_entry *parent;
396 
397 	KASSERT(entry->gap == uvm_rb_gap(entry));
398 	entry->maxgap = uvm_rb_maxgap(entry);
399 
400 	while ((parent = PARENT_ENTRY(map, entry)) != NULL) {
401 		struct vm_map_entry *brother;
402 		vsize_t maxgap = parent->gap;
403 		unsigned int which;
404 
405 		KDASSERT(parent->gap == uvm_rb_gap(parent));
406 		if (maxgap < entry->maxgap)
407 			maxgap = entry->maxgap;
408 		/*
409 		 * Since we work towards the root, we know entry's maxgap
410 		 * value is OK, but its brothers may now be out-of-date due
411 		 * to rebalancing.  So refresh it.
412 		 */
413 		which = RB_POSITION(&entry->rb_node) ^ RB_DIR_OTHER;
414 		brother = (struct vm_map_entry *)parent->rb_node.rb_nodes[which];
415 		if (brother != NULL) {
416 			KDASSERT(brother->gap == uvm_rb_gap(brother));
417 			brother->maxgap = uvm_rb_maxgap(brother);
418 			if (maxgap < brother->maxgap)
419 				maxgap = brother->maxgap;
420 		}
421 
422 		parent->maxgap = maxgap;
423 		entry = parent;
424 	}
425 }
426 
427 static void
428 uvm_rb_insert(struct vm_map *map, struct vm_map_entry *entry)
429 {
430 	struct vm_map_entry *ret;
431 
432 	entry->gap = entry->maxgap = uvm_rb_gap(entry);
433 	if (entry->prev != &map->header)
434 		entry->prev->gap = uvm_rb_gap(entry->prev);
435 
436 	ret = rb_tree_insert_node(&map->rb_tree, entry);
437 	KASSERTMSG(ret == entry,
438 	    ("uvm_rb_insert: map %p: duplicate entry %p", map, ret)
439 	);
440 
441 	/*
442 	 * If the previous entry is not our immediate left child, then it's an
443 	 * ancestor and will be fixed up on the way to the root.  We don't
444 	 * have to check entry->prev against &map->header since &map->header
445 	 * will never be in the tree.
446 	 */
447 	uvm_rb_fixup(map,
448 	    LEFT_ENTRY(entry) == entry->prev ? entry->prev : entry);
449 }
450 
451 static void
452 uvm_rb_remove(struct vm_map *map, struct vm_map_entry *entry)
453 {
454 	struct vm_map_entry *prev_parent = NULL, *next_parent = NULL;
455 
456 	/*
457 	 * If we are removing an interior node, then an adjacent node will
458 	 * be used to replace its position in the tree.  Therefore we will
459 	 * need to fixup the tree starting at the parent of the replacement
460 	 * node.  So record their parents for later use.
461 	 */
462 	if (entry->prev != &map->header)
463 		prev_parent = PARENT_ENTRY(map, entry->prev);
464 	if (entry->next != &map->header)
465 		next_parent = PARENT_ENTRY(map, entry->next);
466 
467 	rb_tree_remove_node(&map->rb_tree, entry);
468 
469 	/*
470 	 * If the previous node has a new parent, fixup the tree starting
471 	 * at the previous node's old parent.
472 	 */
473 	if (entry->prev != &map->header) {
474 		/*
475 		 * Update the previous entry's gap due to our absence.
476 		 */
477 		entry->prev->gap = uvm_rb_gap(entry->prev);
478 		uvm_rb_fixup(map, entry->prev);
479 		if (prev_parent != NULL
480 		    && prev_parent != entry
481 		    && prev_parent != PARENT_ENTRY(map, entry->prev))
482 			uvm_rb_fixup(map, prev_parent);
483 	}
484 
485 	/*
486 	 * If the next node has a new parent, fixup the tree starting
487 	 * at the next node's old parent.
488 	 */
489 	if (entry->next != &map->header) {
490 		uvm_rb_fixup(map, entry->next);
491 		if (next_parent != NULL
492 		    && next_parent != entry
493 		    && next_parent != PARENT_ENTRY(map, entry->next))
494 			uvm_rb_fixup(map, next_parent);
495 	}
496 }
497 
498 #if defined(DEBUG)
499 int uvm_debug_check_map = 0;
500 int uvm_debug_check_rbtree = 0;
501 #define uvm_map_check(map, name) \
502 	_uvm_map_check((map), (name), __FILE__, __LINE__)
503 static void
504 _uvm_map_check(struct vm_map *map, const char *name,
505     const char *file, int line)
506 {
507 
508 	if ((uvm_debug_check_map && _uvm_map_sanity(map)) ||
509 	    (uvm_debug_check_rbtree && _uvm_tree_sanity(map))) {
510 		panic("uvm_map_check failed: \"%s\" map=%p (%s:%d)",
511 		    name, map, file, line);
512 	}
513 }
514 #else /* defined(DEBUG) */
515 #define uvm_map_check(map, name)	/* nothing */
516 #endif /* defined(DEBUG) */
517 
518 #if defined(DEBUG) || defined(DDB)
519 int
520 _uvm_map_sanity(struct vm_map *map)
521 {
522 	bool first_free_found = false;
523 	bool hint_found = false;
524 	const struct vm_map_entry *e;
525 	struct vm_map_entry *hint = map->hint;
526 
527 	e = &map->header;
528 	for (;;) {
529 		if (map->first_free == e) {
530 			first_free_found = true;
531 		} else if (!first_free_found && e->next->start > e->end) {
532 			printf("first_free %p should be %p\n",
533 			    map->first_free, e);
534 			return -1;
535 		}
536 		if (hint == e) {
537 			hint_found = true;
538 		}
539 
540 		e = e->next;
541 		if (e == &map->header) {
542 			break;
543 		}
544 	}
545 	if (!first_free_found) {
546 		printf("stale first_free\n");
547 		return -1;
548 	}
549 	if (!hint_found) {
550 		printf("stale hint\n");
551 		return -1;
552 	}
553 	return 0;
554 }
555 
556 int
557 _uvm_tree_sanity(struct vm_map *map)
558 {
559 	struct vm_map_entry *tmp, *trtmp;
560 	int n = 0, i = 1;
561 
562 	for (tmp = map->header.next; tmp != &map->header; tmp = tmp->next) {
563 		if (tmp->gap != uvm_rb_gap(tmp)) {
564 			printf("%d/%d gap %lx != %lx %s\n",
565 			    n + 1, map->nentries,
566 			    (ulong)tmp->gap, (ulong)uvm_rb_gap(tmp),
567 			    tmp->next == &map->header ? "(last)" : "");
568 			goto error;
569 		}
570 		/*
571 		 * If any entries are out of order, tmp->gap will be unsigned
572 		 * and will likely exceed the size of the map.
573 		 */
574 		if (tmp->gap >= vm_map_max(map) - vm_map_min(map)) {
575 			printf("too large gap %zu\n", (size_t)tmp->gap);
576 			goto error;
577 		}
578 		n++;
579 	}
580 
581 	if (n != map->nentries) {
582 		printf("nentries: %d vs %d\n", n, map->nentries);
583 		goto error;
584 	}
585 
586 	trtmp = NULL;
587 	for (tmp = map->header.next; tmp != &map->header; tmp = tmp->next) {
588 		if (tmp->maxgap != uvm_rb_maxgap(tmp)) {
589 			printf("maxgap %lx != %lx\n",
590 			    (ulong)tmp->maxgap,
591 			    (ulong)uvm_rb_maxgap(tmp));
592 			goto error;
593 		}
594 		if (trtmp != NULL && trtmp->start >= tmp->start) {
595 			printf("corrupt: 0x%"PRIxVADDR"x >= 0x%"PRIxVADDR"x\n",
596 			    trtmp->start, tmp->start);
597 			goto error;
598 		}
599 
600 		trtmp = tmp;
601 	}
602 
603 	for (tmp = map->header.next; tmp != &map->header;
604 	    tmp = tmp->next, i++) {
605 		trtmp = rb_tree_iterate(&map->rb_tree, tmp, RB_DIR_LEFT);
606 		if (trtmp == NULL)
607 			trtmp = &map->header;
608 		if (tmp->prev != trtmp) {
609 			printf("lookup: %d: %p->prev=%p: %p\n",
610 			    i, tmp, tmp->prev, trtmp);
611 			goto error;
612 		}
613 		trtmp = rb_tree_iterate(&map->rb_tree, tmp, RB_DIR_RIGHT);
614 		if (trtmp == NULL)
615 			trtmp = &map->header;
616 		if (tmp->next != trtmp) {
617 			printf("lookup: %d: %p->next=%p: %p\n",
618 			    i, tmp, tmp->next, trtmp);
619 			goto error;
620 		}
621 		trtmp = rb_tree_find_node(&map->rb_tree, &tmp->start);
622 		if (trtmp != tmp) {
623 			printf("lookup: %d: %p - %p: %p\n", i, tmp, trtmp,
624 			    PARENT_ENTRY(map, tmp));
625 			goto error;
626 		}
627 	}
628 
629 	return (0);
630  error:
631 	return (-1);
632 }
633 #endif /* defined(DEBUG) || defined(DDB) */
634 
635 #ifdef DIAGNOSTIC
636 static struct vm_map *uvm_kmapent_map(struct vm_map_entry *);
637 #endif
638 
639 /*
640  * vm_map_lock: acquire an exclusive (write) lock on a map.
641  *
642  * => Note that "intrsafe" maps use only exclusive, spin locks.
643  *
644  * => The locking protocol provides for guaranteed upgrade from shared ->
645  *    exclusive by whichever thread currently has the map marked busy.
646  *    See "LOCKING PROTOCOL NOTES" in uvm_map.h.  This is horrible; among
647  *    other problems, it defeats any fairness guarantees provided by RW
648  *    locks.
649  */
650 
651 void
652 vm_map_lock(struct vm_map *map)
653 {
654 
655 	if ((map->flags & VM_MAP_INTRSAFE) != 0) {
656 		mutex_spin_enter(&map->mutex);
657 		return;
658 	}
659 
660 	for (;;) {
661 		rw_enter(&map->lock, RW_WRITER);
662 		if (map->busy == NULL)
663 			break;
664 		if (map->busy == curlwp)
665 			break;
666 		mutex_enter(&map->misc_lock);
667 		rw_exit(&map->lock);
668 		if (map->busy != NULL)
669 			cv_wait(&map->cv, &map->misc_lock);
670 		mutex_exit(&map->misc_lock);
671 	}
672 
673 	map->timestamp++;
674 }
675 
676 /*
677  * vm_map_lock_try: try to lock a map, failing if it is already locked.
678  */
679 
680 bool
681 vm_map_lock_try(struct vm_map *map)
682 {
683 
684 	if ((map->flags & VM_MAP_INTRSAFE) != 0)
685 		return mutex_tryenter(&map->mutex);
686 	if (!rw_tryenter(&map->lock, RW_WRITER))
687 		return false;
688 	if (map->busy != NULL) {
689 		rw_exit(&map->lock);
690 		return false;
691 	}
692 
693 	map->timestamp++;
694 	return true;
695 }
696 
697 /*
698  * vm_map_unlock: release an exclusive lock on a map.
699  */
700 
701 void
702 vm_map_unlock(struct vm_map *map)
703 {
704 
705 	if ((map->flags & VM_MAP_INTRSAFE) != 0)
706 		mutex_spin_exit(&map->mutex);
707 	else {
708 		KASSERT(rw_write_held(&map->lock));
709 		KASSERT(map->busy == NULL || map->busy == curlwp);
710 		rw_exit(&map->lock);
711 	}
712 }
713 
714 /*
715  * vm_map_unbusy: mark the map as unbusy, and wake any waiters that
716  *     want an exclusive lock.
717  */
718 
719 void
720 vm_map_unbusy(struct vm_map *map)
721 {
722 
723 	KASSERT(map->busy == curlwp);
724 
725 	/*
726 	 * Safe to clear 'busy' and 'waiters' with only a read lock held:
727 	 *
728 	 * o they can only be set with a write lock held
729 	 * o writers are blocked out with a read or write hold
730 	 * o at any time, only one thread owns the set of values
731 	 */
732 	mutex_enter(&map->misc_lock);
733 	map->busy = NULL;
734 	cv_broadcast(&map->cv);
735 	mutex_exit(&map->misc_lock);
736 }
737 
738 /*
739  * vm_map_lock_read: acquire a shared (read) lock on a map.
740  */
741 
742 void
743 vm_map_lock_read(struct vm_map *map)
744 {
745 
746 	KASSERT((map->flags & VM_MAP_INTRSAFE) == 0);
747 
748 	rw_enter(&map->lock, RW_READER);
749 }
750 
751 /*
752  * vm_map_unlock_read: release a shared lock on a map.
753  */
754 
755 void
756 vm_map_unlock_read(struct vm_map *map)
757 {
758 
759 	KASSERT((map->flags & VM_MAP_INTRSAFE) == 0);
760 
761 	rw_exit(&map->lock);
762 }
763 
764 /*
765  * vm_map_busy: mark a map as busy.
766  *
767  * => the caller must hold the map write locked
768  */
769 
770 void
771 vm_map_busy(struct vm_map *map)
772 {
773 
774 	KASSERT(rw_write_held(&map->lock));
775 	KASSERT(map->busy == NULL);
776 
777 	map->busy = curlwp;
778 }
779 
780 /*
781  * vm_map_locked_p: return true if the map is write locked.
782  *
783  * => only for debug purposes like KASSERTs.
784  * => should not be used to verify that a map is not locked.
785  */
786 
787 bool
788 vm_map_locked_p(struct vm_map *map)
789 {
790 
791 	if ((map->flags & VM_MAP_INTRSAFE) != 0) {
792 		return mutex_owned(&map->mutex);
793 	} else {
794 		return rw_write_held(&map->lock);
795 	}
796 }
797 
798 /*
799  * uvm_mapent_alloc: allocate a map entry
800  */
801 
802 static struct vm_map_entry *
803 uvm_mapent_alloc(struct vm_map *map, int flags)
804 {
805 	struct vm_map_entry *me;
806 	int pflags = (flags & UVM_FLAG_NOWAIT) ? PR_NOWAIT : PR_WAITOK;
807 	UVMHIST_FUNC("uvm_mapent_alloc"); UVMHIST_CALLED(maphist);
808 
809 	if (VM_MAP_USE_KMAPENT(map)) {
810 		me = uvm_kmapent_alloc(map, flags);
811 	} else {
812 		me = pool_cache_get(&uvm_map_entry_cache, pflags);
813 		if (__predict_false(me == NULL))
814 			return NULL;
815 		me->flags = 0;
816 	}
817 
818 	UVMHIST_LOG(maphist, "<- new entry=0x%x [kentry=%d]", me,
819 	    ((map->flags & VM_MAP_INTRSAFE) != 0 || map == kernel_map), 0, 0);
820 	return (me);
821 }
822 
823 /*
824  * uvm_mapent_alloc_split: allocate a map entry for clipping.
825  *
826  * => map must be locked by caller if UVM_MAP_QUANTUM is set.
827  */
828 
829 static struct vm_map_entry *
830 uvm_mapent_alloc_split(struct vm_map *map,
831     const struct vm_map_entry *old_entry, int flags,
832     struct uvm_mapent_reservation *umr)
833 {
834 	struct vm_map_entry *me;
835 
836 	KASSERT(!VM_MAP_USE_KMAPENT(map) ||
837 	    (old_entry->flags & UVM_MAP_QUANTUM) || !UMR_EMPTY(umr));
838 
839 	if (old_entry->flags & UVM_MAP_QUANTUM) {
840 		struct vm_map_kernel *vmk = vm_map_to_kernel(map);
841 
842 		KASSERT(vm_map_locked_p(map));
843 		me = vmk->vmk_merged_entries;
844 		KASSERT(me);
845 		vmk->vmk_merged_entries = me->next;
846 		KASSERT(me->flags & UVM_MAP_QUANTUM);
847 	} else {
848 		me = uvm_mapent_alloc(map, flags);
849 	}
850 
851 	return me;
852 }
853 
854 /*
855  * uvm_mapent_free: free map entry
856  */
857 
858 static void
859 uvm_mapent_free(struct vm_map_entry *me)
860 {
861 	UVMHIST_FUNC("uvm_mapent_free"); UVMHIST_CALLED(maphist);
862 
863 	UVMHIST_LOG(maphist,"<- freeing map entry=0x%x [flags=%d]",
864 		me, me->flags, 0, 0);
865 	if (me->flags & UVM_MAP_KERNEL) {
866 		uvm_kmapent_free(me);
867 	} else {
868 		pool_cache_put(&uvm_map_entry_cache, me);
869 	}
870 }
871 
872 /*
873  * uvm_mapent_free_merged: free merged map entry
874  *
875  * => keep the entry if needed.
876  * => caller shouldn't hold map locked if VM_MAP_USE_KMAPENT(map) is true.
877  * => map should be locked if UVM_MAP_QUANTUM is set.
878  */
879 
880 static void
881 uvm_mapent_free_merged(struct vm_map *map, struct vm_map_entry *me)
882 {
883 
884 	KASSERT(!(me->flags & UVM_MAP_KERNEL) || uvm_kmapent_map(me) == map);
885 
886 	if (me->flags & UVM_MAP_QUANTUM) {
887 		/*
888 		 * keep this entry for later splitting.
889 		 */
890 		struct vm_map_kernel *vmk;
891 
892 		KASSERT(vm_map_locked_p(map));
893 		KASSERT(VM_MAP_IS_KERNEL(map));
894 		KASSERT(!VM_MAP_USE_KMAPENT(map) ||
895 		    (me->flags & UVM_MAP_KERNEL));
896 
897 		vmk = vm_map_to_kernel(map);
898 		me->next = vmk->vmk_merged_entries;
899 		vmk->vmk_merged_entries = me;
900 	} else {
901 		uvm_mapent_free(me);
902 	}
903 }
904 
905 /*
906  * uvm_mapent_copy: copy a map entry, preserving flags
907  */
908 
909 static inline void
910 uvm_mapent_copy(struct vm_map_entry *src, struct vm_map_entry *dst)
911 {
912 
913 	memcpy(dst, src, ((char *)&src->uvm_map_entry_stop_copy) -
914 	    ((char *)src));
915 }
916 
917 /*
918  * uvm_mapent_overhead: calculate maximum kva overhead necessary for
919  * map entries.
920  *
921  * => size and flags are the same as uvm_km_suballoc's ones.
922  */
923 
924 vsize_t
925 uvm_mapent_overhead(vsize_t size, int flags)
926 {
927 
928 	if (VM_MAP_USE_KMAPENT_FLAGS(flags)) {
929 		return uvm_kmapent_overhead(size);
930 	}
931 	return 0;
932 }
933 
934 #if defined(DEBUG)
935 static void
936 _uvm_mapent_check(const struct vm_map_entry *entry, const char *file, int line)
937 {
938 
939 	if (entry->start >= entry->end) {
940 		goto bad;
941 	}
942 	if (UVM_ET_ISOBJ(entry)) {
943 		if (entry->object.uvm_obj == NULL) {
944 			goto bad;
945 		}
946 	} else if (UVM_ET_ISSUBMAP(entry)) {
947 		if (entry->object.sub_map == NULL) {
948 			goto bad;
949 		}
950 	} else {
951 		if (entry->object.uvm_obj != NULL ||
952 		    entry->object.sub_map != NULL) {
953 			goto bad;
954 		}
955 	}
956 	if (!UVM_ET_ISOBJ(entry)) {
957 		if (entry->offset != 0) {
958 			goto bad;
959 		}
960 	}
961 
962 	return;
963 
964 bad:
965 	panic("%s: bad entry %p (%s:%d)", __func__, entry, file, line);
966 }
967 #endif /* defined(DEBUG) */
968 
969 /*
970  * uvm_map_entry_unwire: unwire a map entry
971  *
972  * => map should be locked by caller
973  */
974 
975 static inline void
976 uvm_map_entry_unwire(struct vm_map *map, struct vm_map_entry *entry)
977 {
978 
979 	entry->wired_count = 0;
980 	uvm_fault_unwire_locked(map, entry->start, entry->end);
981 }
982 
983 
984 /*
985  * wrapper for calling amap_ref()
986  */
987 static inline void
988 uvm_map_reference_amap(struct vm_map_entry *entry, int flags)
989 {
990 
991 	amap_ref(entry->aref.ar_amap, entry->aref.ar_pageoff,
992 	    (entry->end - entry->start) >> PAGE_SHIFT, flags);
993 }
994 
995 
996 /*
997  * wrapper for calling amap_unref()
998  */
999 static inline void
1000 uvm_map_unreference_amap(struct vm_map_entry *entry, int flags)
1001 {
1002 
1003 	amap_unref(entry->aref.ar_amap, entry->aref.ar_pageoff,
1004 	    (entry->end - entry->start) >> PAGE_SHIFT, flags);
1005 }
1006 
1007 
1008 /*
1009  * uvm_map_init: init mapping system at boot time.
1010  */
1011 
1012 void
1013 uvm_map_init(void)
1014 {
1015 #if defined(UVMHIST)
1016 	static struct uvm_history_ent maphistbuf[100];
1017 	static struct uvm_history_ent pdhistbuf[100];
1018 #endif
1019 
1020 	/*
1021 	 * first, init logging system.
1022 	 */
1023 
1024 	UVMHIST_FUNC("uvm_map_init");
1025 	UVMHIST_INIT_STATIC(maphist, maphistbuf);
1026 	UVMHIST_INIT_STATIC(pdhist, pdhistbuf);
1027 	UVMHIST_CALLED(maphist);
1028 	UVMHIST_LOG(maphist,"<starting uvm map system>", 0, 0, 0, 0);
1029 
1030 	/*
1031 	 * initialize the global lock for kernel map entry.
1032 	 */
1033 
1034 	mutex_init(&uvm_kentry_lock, MUTEX_DRIVER, IPL_VM);
1035 
1036 	/*
1037 	 * initialize caches.
1038 	 */
1039 
1040 	pool_cache_bootstrap(&uvm_map_entry_cache, sizeof(struct vm_map_entry),
1041 	    0, 0, 0, "vmmpepl", NULL, IPL_NONE, NULL, NULL, NULL);
1042 	pool_cache_bootstrap(&uvm_vmspace_cache, sizeof(struct vmspace),
1043 	    0, 0, 0, "vmsppl", NULL, IPL_NONE, NULL, NULL, NULL);
1044 }
1045 
1046 /*
1047  * clippers
1048  */
1049 
1050 /*
1051  * uvm_mapent_splitadj: adjust map entries for splitting, after uvm_mapent_copy.
1052  */
1053 
1054 static void
1055 uvm_mapent_splitadj(struct vm_map_entry *entry1, struct vm_map_entry *entry2,
1056     vaddr_t splitat)
1057 {
1058 	vaddr_t adj;
1059 
1060 	KASSERT(entry1->start < splitat);
1061 	KASSERT(splitat < entry1->end);
1062 
1063 	adj = splitat - entry1->start;
1064 	entry1->end = entry2->start = splitat;
1065 
1066 	if (entry1->aref.ar_amap) {
1067 		amap_splitref(&entry1->aref, &entry2->aref, adj);
1068 	}
1069 	if (UVM_ET_ISSUBMAP(entry1)) {
1070 		/* ... unlikely to happen, but play it safe */
1071 		 uvm_map_reference(entry1->object.sub_map);
1072 	} else if (UVM_ET_ISOBJ(entry1)) {
1073 		KASSERT(entry1->object.uvm_obj != NULL); /* suppress coverity */
1074 		entry2->offset += adj;
1075 		if (entry1->object.uvm_obj->pgops &&
1076 		    entry1->object.uvm_obj->pgops->pgo_reference)
1077 			entry1->object.uvm_obj->pgops->pgo_reference(
1078 			    entry1->object.uvm_obj);
1079 	}
1080 }
1081 
1082 /*
1083  * uvm_map_clip_start: ensure that the entry begins at or after
1084  *	the starting address, if it doesn't we split the entry.
1085  *
1086  * => caller should use UVM_MAP_CLIP_START macro rather than calling
1087  *    this directly
1088  * => map must be locked by caller
1089  */
1090 
1091 void
1092 uvm_map_clip_start(struct vm_map *map, struct vm_map_entry *entry,
1093     vaddr_t start, struct uvm_mapent_reservation *umr)
1094 {
1095 	struct vm_map_entry *new_entry;
1096 
1097 	/* uvm_map_simplify_entry(map, entry); */ /* XXX */
1098 
1099 	uvm_map_check(map, "clip_start entry");
1100 	uvm_mapent_check(entry);
1101 
1102 	/*
1103 	 * Split off the front portion.  note that we must insert the new
1104 	 * entry BEFORE this one, so that this entry has the specified
1105 	 * starting address.
1106 	 */
1107 	new_entry = uvm_mapent_alloc_split(map, entry, 0, umr);
1108 	uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
1109 	uvm_mapent_splitadj(new_entry, entry, start);
1110 	uvm_map_entry_link(map, entry->prev, new_entry);
1111 
1112 	uvm_map_check(map, "clip_start leave");
1113 }
1114 
1115 /*
1116  * uvm_map_clip_end: ensure that the entry ends at or before
1117  *	the ending address, if it does't we split the reference
1118  *
1119  * => caller should use UVM_MAP_CLIP_END macro rather than calling
1120  *    this directly
1121  * => map must be locked by caller
1122  */
1123 
1124 void
1125 uvm_map_clip_end(struct vm_map *map, struct vm_map_entry *entry, vaddr_t end,
1126     struct uvm_mapent_reservation *umr)
1127 {
1128 	struct vm_map_entry *new_entry;
1129 
1130 	uvm_map_check(map, "clip_end entry");
1131 	uvm_mapent_check(entry);
1132 
1133 	/*
1134 	 *	Create a new entry and insert it
1135 	 *	AFTER the specified entry
1136 	 */
1137 	new_entry = uvm_mapent_alloc_split(map, entry, 0, umr);
1138 	uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */
1139 	uvm_mapent_splitadj(entry, new_entry, end);
1140 	uvm_map_entry_link(map, entry, new_entry);
1141 
1142 	uvm_map_check(map, "clip_end leave");
1143 }
1144 
1145 static void
1146 vm_map_drain(struct vm_map *map, uvm_flag_t flags)
1147 {
1148 
1149 	if (!VM_MAP_IS_KERNEL(map)) {
1150 		return;
1151 	}
1152 
1153 	uvm_km_va_drain(map, flags);
1154 }
1155 
1156 /*
1157  *   M A P   -   m a i n   e n t r y   p o i n t
1158  */
1159 /*
1160  * uvm_map: establish a valid mapping in a map
1161  *
1162  * => assume startp is page aligned.
1163  * => assume size is a multiple of PAGE_SIZE.
1164  * => assume sys_mmap provides enough of a "hint" to have us skip
1165  *	over text/data/bss area.
1166  * => map must be unlocked (we will lock it)
1167  * => <uobj,uoffset> value meanings (4 cases):
1168  *	 [1] <NULL,uoffset>		== uoffset is a hint for PMAP_PREFER
1169  *	 [2] <NULL,UVM_UNKNOWN_OFFSET>	== don't PMAP_PREFER
1170  *	 [3] <uobj,uoffset>		== normal mapping
1171  *	 [4] <uobj,UVM_UNKNOWN_OFFSET>	== uvm_map finds offset based on VA
1172  *
1173  *    case [4] is for kernel mappings where we don't know the offset until
1174  *    we've found a virtual address.   note that kernel object offsets are
1175  *    always relative to vm_map_min(kernel_map).
1176  *
1177  * => if `align' is non-zero, we align the virtual address to the specified
1178  *	alignment.
1179  *	this is provided as a mechanism for large pages.
1180  *
1181  * => XXXCDC: need way to map in external amap?
1182  */
1183 
1184 int
1185 uvm_map(struct vm_map *map, vaddr_t *startp /* IN/OUT */, vsize_t size,
1186     struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags)
1187 {
1188 	struct uvm_map_args args;
1189 	struct vm_map_entry *new_entry;
1190 	int error;
1191 
1192 	KASSERT((flags & UVM_FLAG_QUANTUM) == 0 || VM_MAP_IS_KERNEL(map));
1193 	KASSERT((size & PAGE_MASK) == 0);
1194 
1195 #ifndef __USER_VA0_IS_SAFE
1196 	if ((flags & UVM_FLAG_FIXED) && *startp == 0 &&
1197 	    !VM_MAP_IS_KERNEL(map) && user_va0_disable)
1198 		return EACCES;
1199 #endif
1200 
1201 	/*
1202 	 * for pager_map, allocate the new entry first to avoid sleeping
1203 	 * for memory while we have the map locked.
1204 	 *
1205 	 * Also, because we allocate entries for in-kernel maps
1206 	 * a bit differently (cf. uvm_kmapent_alloc/free), we need to
1207 	 * allocate them before locking the map.
1208 	 */
1209 
1210 	new_entry = NULL;
1211 	if (VM_MAP_USE_KMAPENT(map) || (flags & UVM_FLAG_QUANTUM) ||
1212 	    map == pager_map) {
1213 		new_entry = uvm_mapent_alloc(map, (flags & UVM_FLAG_NOWAIT));
1214 		if (__predict_false(new_entry == NULL))
1215 			return ENOMEM;
1216 		if (flags & UVM_FLAG_QUANTUM)
1217 			new_entry->flags |= UVM_MAP_QUANTUM;
1218 	}
1219 	if (map == pager_map)
1220 		flags |= UVM_FLAG_NOMERGE;
1221 
1222 	error = uvm_map_prepare(map, *startp, size, uobj, uoffset, align,
1223 	    flags, &args);
1224 	if (!error) {
1225 		error = uvm_map_enter(map, &args, new_entry);
1226 		*startp = args.uma_start;
1227 	} else if (new_entry) {
1228 		uvm_mapent_free(new_entry);
1229 	}
1230 
1231 #if defined(DEBUG)
1232 	if (!error && VM_MAP_IS_KERNEL(map)) {
1233 		uvm_km_check_empty(map, *startp, *startp + size);
1234 	}
1235 #endif /* defined(DEBUG) */
1236 
1237 	return error;
1238 }
1239 
1240 int
1241 uvm_map_prepare(struct vm_map *map, vaddr_t start, vsize_t size,
1242     struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags,
1243     struct uvm_map_args *args)
1244 {
1245 	struct vm_map_entry *prev_entry;
1246 	vm_prot_t prot = UVM_PROTECTION(flags);
1247 	vm_prot_t maxprot = UVM_MAXPROTECTION(flags);
1248 
1249 	UVMHIST_FUNC("uvm_map_prepare");
1250 	UVMHIST_CALLED(maphist);
1251 
1252 	UVMHIST_LOG(maphist, "(map=0x%x, start=0x%x, size=%d, flags=0x%x)",
1253 	    map, start, size, flags);
1254 	UVMHIST_LOG(maphist, "  uobj/offset 0x%x/%d", uobj, uoffset,0,0);
1255 
1256 	/*
1257 	 * detect a popular device driver bug.
1258 	 */
1259 
1260 	KASSERT(doing_shutdown || curlwp != NULL ||
1261 	    (map->flags & VM_MAP_INTRSAFE));
1262 
1263 	/*
1264 	 * zero-sized mapping doesn't make any sense.
1265 	 */
1266 	KASSERT(size > 0);
1267 
1268 	KASSERT((~flags & (UVM_FLAG_NOWAIT | UVM_FLAG_WAITVA)) != 0);
1269 
1270 	uvm_map_check(map, "map entry");
1271 
1272 	/*
1273 	 * check sanity of protection code
1274 	 */
1275 
1276 	if ((prot & maxprot) != prot) {
1277 		UVMHIST_LOG(maphist, "<- prot. failure:  prot=0x%x, max=0x%x",
1278 		prot, maxprot,0,0);
1279 		return EACCES;
1280 	}
1281 
1282 	/*
1283 	 * figure out where to put new VM range
1284 	 */
1285 
1286 retry:
1287 	if (vm_map_lock_try(map) == false) {
1288 		if ((flags & UVM_FLAG_TRYLOCK) != 0 &&
1289 		    (map->flags & VM_MAP_INTRSAFE) == 0) {
1290 			return EAGAIN;
1291 		}
1292 		vm_map_lock(map); /* could sleep here */
1293 	}
1294 	prev_entry = uvm_map_findspace(map, start, size, &start,
1295 	    uobj, uoffset, align, flags);
1296 	if (prev_entry == NULL) {
1297 		unsigned int timestamp;
1298 
1299 		timestamp = map->timestamp;
1300 		UVMHIST_LOG(maphist,"waiting va timestamp=0x%x",
1301 			    timestamp,0,0,0);
1302 		map->flags |= VM_MAP_WANTVA;
1303 		vm_map_unlock(map);
1304 
1305 		/*
1306 		 * try to reclaim kva and wait until someone does unmap.
1307 		 * fragile locking here, so we awaken every second to
1308 		 * recheck the condition.
1309 		 */
1310 
1311 		vm_map_drain(map, flags);
1312 
1313 		mutex_enter(&map->misc_lock);
1314 		while ((map->flags & VM_MAP_WANTVA) != 0 &&
1315 		   map->timestamp == timestamp) {
1316 			if ((flags & UVM_FLAG_WAITVA) == 0) {
1317 				mutex_exit(&map->misc_lock);
1318 				UVMHIST_LOG(maphist,
1319 				    "<- uvm_map_findspace failed!", 0,0,0,0);
1320 				return ENOMEM;
1321 			} else {
1322 				cv_timedwait(&map->cv, &map->misc_lock, hz);
1323 			}
1324 		}
1325 		mutex_exit(&map->misc_lock);
1326 		goto retry;
1327 	}
1328 
1329 #ifdef PMAP_GROWKERNEL
1330 	/*
1331 	 * If the kernel pmap can't map the requested space,
1332 	 * then allocate more resources for it.
1333 	 */
1334 	if (map == kernel_map && uvm_maxkaddr < (start + size))
1335 		uvm_maxkaddr = pmap_growkernel(start + size);
1336 #endif
1337 
1338 	UVMMAP_EVCNT_INCR(map_call);
1339 
1340 	/*
1341 	 * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER
1342 	 * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET.   in
1343 	 * either case we want to zero it  before storing it in the map entry
1344 	 * (because it looks strange and confusing when debugging...)
1345 	 *
1346 	 * if uobj is not null
1347 	 *   if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping
1348 	 *      and we do not need to change uoffset.
1349 	 *   if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset
1350 	 *      now (based on the starting address of the map).   this case is
1351 	 *      for kernel object mappings where we don't know the offset until
1352 	 *      the virtual address is found (with uvm_map_findspace).   the
1353 	 *      offset is the distance we are from the start of the map.
1354 	 */
1355 
1356 	if (uobj == NULL) {
1357 		uoffset = 0;
1358 	} else {
1359 		if (uoffset == UVM_UNKNOWN_OFFSET) {
1360 			KASSERT(UVM_OBJ_IS_KERN_OBJECT(uobj));
1361 			uoffset = start - vm_map_min(kernel_map);
1362 		}
1363 	}
1364 
1365 	args->uma_flags = flags;
1366 	args->uma_prev = prev_entry;
1367 	args->uma_start = start;
1368 	args->uma_size = size;
1369 	args->uma_uobj = uobj;
1370 	args->uma_uoffset = uoffset;
1371 
1372 	UVMHIST_LOG(maphist, "<- done!", 0,0,0,0);
1373 	return 0;
1374 }
1375 
1376 int
1377 uvm_map_enter(struct vm_map *map, const struct uvm_map_args *args,
1378     struct vm_map_entry *new_entry)
1379 {
1380 	struct vm_map_entry *prev_entry = args->uma_prev;
1381 	struct vm_map_entry *dead = NULL;
1382 
1383 	const uvm_flag_t flags = args->uma_flags;
1384 	const vm_prot_t prot = UVM_PROTECTION(flags);
1385 	const vm_prot_t maxprot = UVM_MAXPROTECTION(flags);
1386 	const vm_inherit_t inherit = UVM_INHERIT(flags);
1387 	const int amapwaitflag = (flags & UVM_FLAG_NOWAIT) ?
1388 	    AMAP_EXTEND_NOWAIT : 0;
1389 	const int advice = UVM_ADVICE(flags);
1390 	const int meflagval = (flags & UVM_FLAG_QUANTUM) ?
1391 	    UVM_MAP_QUANTUM : 0;
1392 
1393 	vaddr_t start = args->uma_start;
1394 	vsize_t size = args->uma_size;
1395 	struct uvm_object *uobj = args->uma_uobj;
1396 	voff_t uoffset = args->uma_uoffset;
1397 
1398 	const int kmap = (vm_map_pmap(map) == pmap_kernel());
1399 	int merged = 0;
1400 	int error;
1401 	int newetype;
1402 
1403 	UVMHIST_FUNC("uvm_map_enter");
1404 	UVMHIST_CALLED(maphist);
1405 
1406 	UVMHIST_LOG(maphist, "(map=0x%x, start=0x%x, size=%d, flags=0x%x)",
1407 	    map, start, size, flags);
1408 	UVMHIST_LOG(maphist, "  uobj/offset 0x%x/%d", uobj, uoffset,0,0);
1409 
1410 	KASSERT(map->hint == prev_entry); /* bimerge case assumes this */
1411 
1412 	if (flags & UVM_FLAG_QUANTUM) {
1413 		KASSERT(new_entry);
1414 		KASSERT(new_entry->flags & UVM_MAP_QUANTUM);
1415 	}
1416 
1417 	if (uobj)
1418 		newetype = UVM_ET_OBJ;
1419 	else
1420 		newetype = 0;
1421 
1422 	if (flags & UVM_FLAG_COPYONW) {
1423 		newetype |= UVM_ET_COPYONWRITE;
1424 		if ((flags & UVM_FLAG_OVERLAY) == 0)
1425 			newetype |= UVM_ET_NEEDSCOPY;
1426 	}
1427 
1428 	/*
1429 	 * try and insert in map by extending previous entry, if possible.
1430 	 * XXX: we don't try and pull back the next entry.   might be useful
1431 	 * for a stack, but we are currently allocating our stack in advance.
1432 	 */
1433 
1434 	if (flags & UVM_FLAG_NOMERGE)
1435 		goto nomerge;
1436 
1437 	if (prev_entry->end == start &&
1438 	    prev_entry != &map->header &&
1439 	    UVM_ET_ISCOMPATIBLE(prev_entry, newetype, uobj, meflagval,
1440 	    prot, maxprot, inherit, advice, 0)) {
1441 
1442 		if (uobj && prev_entry->offset +
1443 		    (prev_entry->end - prev_entry->start) != uoffset)
1444 			goto forwardmerge;
1445 
1446 		/*
1447 		 * can't extend a shared amap.  note: no need to lock amap to
1448 		 * look at refs since we don't care about its exact value.
1449 		 * if it is one (i.e. we have only reference) it will stay there
1450 		 */
1451 
1452 		if (prev_entry->aref.ar_amap &&
1453 		    amap_refs(prev_entry->aref.ar_amap) != 1) {
1454 			goto forwardmerge;
1455 		}
1456 
1457 		if (prev_entry->aref.ar_amap) {
1458 			error = amap_extend(prev_entry, size,
1459 			    amapwaitflag | AMAP_EXTEND_FORWARDS);
1460 			if (error)
1461 				goto nomerge;
1462 		}
1463 
1464 		if (kmap) {
1465 			UVMMAP_EVCNT_INCR(kbackmerge);
1466 		} else {
1467 			UVMMAP_EVCNT_INCR(ubackmerge);
1468 		}
1469 		UVMHIST_LOG(maphist,"  starting back merge", 0, 0, 0, 0);
1470 
1471 		/*
1472 		 * drop our reference to uobj since we are extending a reference
1473 		 * that we already have (the ref count can not drop to zero).
1474 		 */
1475 
1476 		if (uobj && uobj->pgops->pgo_detach)
1477 			uobj->pgops->pgo_detach(uobj);
1478 
1479 		/*
1480 		 * Now that we've merged the entries, note that we've grown
1481 		 * and our gap has shrunk.  Then fix the tree.
1482 		 */
1483 		prev_entry->end += size;
1484 		prev_entry->gap -= size;
1485 		uvm_rb_fixup(map, prev_entry);
1486 
1487 		uvm_map_check(map, "map backmerged");
1488 
1489 		UVMHIST_LOG(maphist,"<- done (via backmerge)!", 0, 0, 0, 0);
1490 		merged++;
1491 	}
1492 
1493 forwardmerge:
1494 	if (prev_entry->next->start == (start + size) &&
1495 	    prev_entry->next != &map->header &&
1496 	    UVM_ET_ISCOMPATIBLE(prev_entry->next, newetype, uobj, meflagval,
1497 	    prot, maxprot, inherit, advice, 0)) {
1498 
1499 		if (uobj && prev_entry->next->offset != uoffset + size)
1500 			goto nomerge;
1501 
1502 		/*
1503 		 * can't extend a shared amap.  note: no need to lock amap to
1504 		 * look at refs since we don't care about its exact value.
1505 		 * if it is one (i.e. we have only reference) it will stay there.
1506 		 *
1507 		 * note that we also can't merge two amaps, so if we
1508 		 * merged with the previous entry which has an amap,
1509 		 * and the next entry also has an amap, we give up.
1510 		 *
1511 		 * Interesting cases:
1512 		 * amap, new, amap -> give up second merge (single fwd extend)
1513 		 * amap, new, none -> double forward extend (extend again here)
1514 		 * none, new, amap -> double backward extend (done here)
1515 		 * uobj, new, amap -> single backward extend (done here)
1516 		 *
1517 		 * XXX should we attempt to deal with someone refilling
1518 		 * the deallocated region between two entries that are
1519 		 * backed by the same amap (ie, arefs is 2, "prev" and
1520 		 * "next" refer to it, and adding this allocation will
1521 		 * close the hole, thus restoring arefs to 1 and
1522 		 * deallocating the "next" vm_map_entry)?  -- @@@
1523 		 */
1524 
1525 		if (prev_entry->next->aref.ar_amap &&
1526 		    (amap_refs(prev_entry->next->aref.ar_amap) != 1 ||
1527 		     (merged && prev_entry->aref.ar_amap))) {
1528 			goto nomerge;
1529 		}
1530 
1531 		if (merged) {
1532 			/*
1533 			 * Try to extend the amap of the previous entry to
1534 			 * cover the next entry as well.  If it doesn't work
1535 			 * just skip on, don't actually give up, since we've
1536 			 * already completed the back merge.
1537 			 */
1538 			if (prev_entry->aref.ar_amap) {
1539 				if (amap_extend(prev_entry,
1540 				    prev_entry->next->end -
1541 				    prev_entry->next->start,
1542 				    amapwaitflag | AMAP_EXTEND_FORWARDS))
1543 					goto nomerge;
1544 			}
1545 
1546 			/*
1547 			 * Try to extend the amap of the *next* entry
1548 			 * back to cover the new allocation *and* the
1549 			 * previous entry as well (the previous merge
1550 			 * didn't have an amap already otherwise we
1551 			 * wouldn't be checking here for an amap).  If
1552 			 * it doesn't work just skip on, again, don't
1553 			 * actually give up, since we've already
1554 			 * completed the back merge.
1555 			 */
1556 			else if (prev_entry->next->aref.ar_amap) {
1557 				if (amap_extend(prev_entry->next,
1558 				    prev_entry->end -
1559 				    prev_entry->start,
1560 				    amapwaitflag | AMAP_EXTEND_BACKWARDS))
1561 					goto nomerge;
1562 			}
1563 		} else {
1564 			/*
1565 			 * Pull the next entry's amap backwards to cover this
1566 			 * new allocation.
1567 			 */
1568 			if (prev_entry->next->aref.ar_amap) {
1569 				error = amap_extend(prev_entry->next, size,
1570 				    amapwaitflag | AMAP_EXTEND_BACKWARDS);
1571 				if (error)
1572 					goto nomerge;
1573 			}
1574 		}
1575 
1576 		if (merged) {
1577 			if (kmap) {
1578 				UVMMAP_EVCNT_DECR(kbackmerge);
1579 				UVMMAP_EVCNT_INCR(kbimerge);
1580 			} else {
1581 				UVMMAP_EVCNT_DECR(ubackmerge);
1582 				UVMMAP_EVCNT_INCR(ubimerge);
1583 			}
1584 		} else {
1585 			if (kmap) {
1586 				UVMMAP_EVCNT_INCR(kforwmerge);
1587 			} else {
1588 				UVMMAP_EVCNT_INCR(uforwmerge);
1589 			}
1590 		}
1591 		UVMHIST_LOG(maphist,"  starting forward merge", 0, 0, 0, 0);
1592 
1593 		/*
1594 		 * drop our reference to uobj since we are extending a reference
1595 		 * that we already have (the ref count can not drop to zero).
1596 		 * (if merged, we've already detached)
1597 		 */
1598 		if (uobj && uobj->pgops->pgo_detach && !merged)
1599 			uobj->pgops->pgo_detach(uobj);
1600 
1601 		if (merged) {
1602 			dead = prev_entry->next;
1603 			prev_entry->end = dead->end;
1604 			uvm_map_entry_unlink(map, dead);
1605 			if (dead->aref.ar_amap != NULL) {
1606 				prev_entry->aref = dead->aref;
1607 				dead->aref.ar_amap = NULL;
1608 			}
1609 		} else {
1610 			prev_entry->next->start -= size;
1611 			if (prev_entry != &map->header) {
1612 				prev_entry->gap -= size;
1613 				KASSERT(prev_entry->gap == uvm_rb_gap(prev_entry));
1614 				uvm_rb_fixup(map, prev_entry);
1615 			}
1616 			if (uobj)
1617 				prev_entry->next->offset = uoffset;
1618 		}
1619 
1620 		uvm_map_check(map, "map forwardmerged");
1621 
1622 		UVMHIST_LOG(maphist,"<- done forwardmerge", 0, 0, 0, 0);
1623 		merged++;
1624 	}
1625 
1626 nomerge:
1627 	if (!merged) {
1628 		UVMHIST_LOG(maphist,"  allocating new map entry", 0, 0, 0, 0);
1629 		if (kmap) {
1630 			UVMMAP_EVCNT_INCR(knomerge);
1631 		} else {
1632 			UVMMAP_EVCNT_INCR(unomerge);
1633 		}
1634 
1635 		/*
1636 		 * allocate new entry and link it in.
1637 		 */
1638 
1639 		if (new_entry == NULL) {
1640 			new_entry = uvm_mapent_alloc(map,
1641 				(flags & UVM_FLAG_NOWAIT));
1642 			if (__predict_false(new_entry == NULL)) {
1643 				error = ENOMEM;
1644 				goto done;
1645 			}
1646 		}
1647 		new_entry->start = start;
1648 		new_entry->end = new_entry->start + size;
1649 		new_entry->object.uvm_obj = uobj;
1650 		new_entry->offset = uoffset;
1651 
1652 		new_entry->etype = newetype;
1653 
1654 		if (flags & UVM_FLAG_NOMERGE) {
1655 			new_entry->flags |= UVM_MAP_NOMERGE;
1656 		}
1657 
1658 		new_entry->protection = prot;
1659 		new_entry->max_protection = maxprot;
1660 		new_entry->inheritance = inherit;
1661 		new_entry->wired_count = 0;
1662 		new_entry->advice = advice;
1663 		if (flags & UVM_FLAG_OVERLAY) {
1664 
1665 			/*
1666 			 * to_add: for BSS we overallocate a little since we
1667 			 * are likely to extend
1668 			 */
1669 
1670 			vaddr_t to_add = (flags & UVM_FLAG_AMAPPAD) ?
1671 				UVM_AMAP_CHUNK << PAGE_SHIFT : 0;
1672 			struct vm_amap *amap = amap_alloc(size, to_add,
1673 			    (flags & UVM_FLAG_NOWAIT));
1674 			if (__predict_false(amap == NULL)) {
1675 				error = ENOMEM;
1676 				goto done;
1677 			}
1678 			new_entry->aref.ar_pageoff = 0;
1679 			new_entry->aref.ar_amap = amap;
1680 		} else {
1681 			new_entry->aref.ar_pageoff = 0;
1682 			new_entry->aref.ar_amap = NULL;
1683 		}
1684 		uvm_map_entry_link(map, prev_entry, new_entry);
1685 
1686 		/*
1687 		 * Update the free space hint
1688 		 */
1689 
1690 		if ((map->first_free == prev_entry) &&
1691 		    (prev_entry->end >= new_entry->start))
1692 			map->first_free = new_entry;
1693 
1694 		new_entry = NULL;
1695 	}
1696 
1697 	map->size += size;
1698 
1699 	UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
1700 
1701 	error = 0;
1702 done:
1703 	if ((flags & UVM_FLAG_QUANTUM) == 0) {
1704 		/*
1705 		 * vmk_merged_entries is locked by the map's lock.
1706 		 */
1707 		vm_map_unlock(map);
1708 	}
1709 	if (new_entry && error == 0) {
1710 		KDASSERT(merged);
1711 		uvm_mapent_free_merged(map, new_entry);
1712 		new_entry = NULL;
1713 	}
1714 	if (dead) {
1715 		KDASSERT(merged);
1716 		uvm_mapent_free_merged(map, dead);
1717 	}
1718 	if ((flags & UVM_FLAG_QUANTUM) != 0) {
1719 		vm_map_unlock(map);
1720 	}
1721 	if (new_entry != NULL) {
1722 		uvm_mapent_free(new_entry);
1723 	}
1724 	return error;
1725 }
1726 
1727 /*
1728  * uvm_map_lookup_entry_bytree: lookup an entry in tree
1729  */
1730 
1731 static inline bool
1732 uvm_map_lookup_entry_bytree(struct vm_map *map, vaddr_t address,
1733     struct vm_map_entry **entry	/* OUT */)
1734 {
1735 	struct vm_map_entry *prev = &map->header;
1736 	struct vm_map_entry *cur = ROOT_ENTRY(map);
1737 
1738 	while (cur) {
1739 		UVMMAP_EVCNT_INCR(mlk_treeloop);
1740 		if (address >= cur->start) {
1741 			if (address < cur->end) {
1742 				*entry = cur;
1743 				return true;
1744 			}
1745 			prev = cur;
1746 			cur = RIGHT_ENTRY(cur);
1747 		} else
1748 			cur = LEFT_ENTRY(cur);
1749 	}
1750 	*entry = prev;
1751 	return false;
1752 }
1753 
1754 /*
1755  * uvm_map_lookup_entry: find map entry at or before an address
1756  *
1757  * => map must at least be read-locked by caller
1758  * => entry is returned in "entry"
1759  * => return value is true if address is in the returned entry
1760  */
1761 
1762 bool
1763 uvm_map_lookup_entry(struct vm_map *map, vaddr_t address,
1764     struct vm_map_entry **entry	/* OUT */)
1765 {
1766 	struct vm_map_entry *cur;
1767 	bool use_tree = false;
1768 	UVMHIST_FUNC("uvm_map_lookup_entry");
1769 	UVMHIST_CALLED(maphist);
1770 
1771 	UVMHIST_LOG(maphist,"(map=0x%x,addr=0x%x,ent=0x%x)",
1772 	    map, address, entry, 0);
1773 
1774 	/*
1775 	 * start looking either from the head of the
1776 	 * list, or from the hint.
1777 	 */
1778 
1779 	cur = map->hint;
1780 
1781 	if (cur == &map->header)
1782 		cur = cur->next;
1783 
1784 	UVMMAP_EVCNT_INCR(mlk_call);
1785 	if (address >= cur->start) {
1786 
1787 		/*
1788 		 * go from hint to end of list.
1789 		 *
1790 		 * but first, make a quick check to see if
1791 		 * we are already looking at the entry we
1792 		 * want (which is usually the case).
1793 		 * note also that we don't need to save the hint
1794 		 * here... it is the same hint (unless we are
1795 		 * at the header, in which case the hint didn't
1796 		 * buy us anything anyway).
1797 		 */
1798 
1799 		if (cur != &map->header && cur->end > address) {
1800 			UVMMAP_EVCNT_INCR(mlk_hint);
1801 			*entry = cur;
1802 			UVMHIST_LOG(maphist,"<- got it via hint (0x%x)",
1803 			    cur, 0, 0, 0);
1804 			uvm_mapent_check(*entry);
1805 			return (true);
1806 		}
1807 
1808 		if (map->nentries > 15)
1809 			use_tree = true;
1810 	} else {
1811 
1812 		/*
1813 		 * invalid hint.  use tree.
1814 		 */
1815 		use_tree = true;
1816 	}
1817 
1818 	uvm_map_check(map, __func__);
1819 
1820 	if (use_tree) {
1821 		/*
1822 		 * Simple lookup in the tree.  Happens when the hint is
1823 		 * invalid, or nentries reach a threshold.
1824 		 */
1825 		UVMMAP_EVCNT_INCR(mlk_tree);
1826 		if (uvm_map_lookup_entry_bytree(map, address, entry)) {
1827 			goto got;
1828 		} else {
1829 			goto failed;
1830 		}
1831 	}
1832 
1833 	/*
1834 	 * search linearly
1835 	 */
1836 
1837 	UVMMAP_EVCNT_INCR(mlk_list);
1838 	while (cur != &map->header) {
1839 		UVMMAP_EVCNT_INCR(mlk_listloop);
1840 		if (cur->end > address) {
1841 			if (address >= cur->start) {
1842 				/*
1843 				 * save this lookup for future
1844 				 * hints, and return
1845 				 */
1846 
1847 				*entry = cur;
1848 got:
1849 				SAVE_HINT(map, map->hint, *entry);
1850 				UVMHIST_LOG(maphist,"<- search got it (0x%x)",
1851 					cur, 0, 0, 0);
1852 				KDASSERT((*entry)->start <= address);
1853 				KDASSERT(address < (*entry)->end);
1854 				uvm_mapent_check(*entry);
1855 				return (true);
1856 			}
1857 			break;
1858 		}
1859 		cur = cur->next;
1860 	}
1861 	*entry = cur->prev;
1862 failed:
1863 	SAVE_HINT(map, map->hint, *entry);
1864 	UVMHIST_LOG(maphist,"<- failed!",0,0,0,0);
1865 	KDASSERT((*entry) == &map->header || (*entry)->end <= address);
1866 	KDASSERT((*entry)->next == &map->header ||
1867 	    address < (*entry)->next->start);
1868 	return (false);
1869 }
1870 
1871 /*
1872  * See if the range between start and start + length fits in the gap
1873  * entry->next->start and entry->end.  Returns 1 if fits, 0 if doesn't
1874  * fit, and -1 address wraps around.
1875  */
1876 static int
1877 uvm_map_space_avail(vaddr_t *start, vsize_t length, voff_t uoffset,
1878     vsize_t align, int topdown, struct vm_map_entry *entry)
1879 {
1880 	vaddr_t end;
1881 
1882 #ifdef PMAP_PREFER
1883 	/*
1884 	 * push start address forward as needed to avoid VAC alias problems.
1885 	 * we only do this if a valid offset is specified.
1886 	 */
1887 
1888 	if (uoffset != UVM_UNKNOWN_OFFSET)
1889 		PMAP_PREFER(uoffset, start, length, topdown);
1890 #endif
1891 	if (align != 0) {
1892 		if ((*start & (align - 1)) != 0) {
1893 			if (topdown)
1894 				*start &= ~(align - 1);
1895 			else
1896 				*start = roundup(*start, align);
1897 		}
1898 		/*
1899 		 * XXX Should we PMAP_PREFER() here again?
1900 		 * eh...i think we're okay
1901 		 */
1902 	}
1903 
1904 	/*
1905 	 * Find the end of the proposed new region.  Be sure we didn't
1906 	 * wrap around the address; if so, we lose.  Otherwise, if the
1907 	 * proposed new region fits before the next entry, we win.
1908 	 */
1909 
1910 	end = *start + length;
1911 	if (end < *start)
1912 		return (-1);
1913 
1914 	if (entry->next->start >= end && *start >= entry->end)
1915 		return (1);
1916 
1917 	return (0);
1918 }
1919 
1920 /*
1921  * uvm_map_findspace: find "length" sized space in "map".
1922  *
1923  * => "hint" is a hint about where we want it, unless UVM_FLAG_FIXED is
1924  *	set in "flags" (in which case we insist on using "hint").
1925  * => "result" is VA returned
1926  * => uobj/uoffset are to be used to handle VAC alignment, if required
1927  * => if "align" is non-zero, we attempt to align to that value.
1928  * => caller must at least have read-locked map
1929  * => returns NULL on failure, or pointer to prev. map entry if success
1930  * => note this is a cross between the old vm_map_findspace and vm_map_find
1931  */
1932 
1933 struct vm_map_entry *
1934 uvm_map_findspace(struct vm_map *map, vaddr_t hint, vsize_t length,
1935     vaddr_t *result /* OUT */, struct uvm_object *uobj, voff_t uoffset,
1936     vsize_t align, int flags)
1937 {
1938 	struct vm_map_entry *entry;
1939 	struct vm_map_entry *child, *prev, *tmp;
1940 	vaddr_t orig_hint;
1941 	const int topdown = map->flags & VM_MAP_TOPDOWN;
1942 	UVMHIST_FUNC("uvm_map_findspace");
1943 	UVMHIST_CALLED(maphist);
1944 
1945 	UVMHIST_LOG(maphist, "(map=0x%x, hint=0x%x, len=%d, flags=0x%x)",
1946 	    map, hint, length, flags);
1947 	KASSERT((align & (align - 1)) == 0);
1948 	KASSERT((flags & UVM_FLAG_FIXED) == 0 || align == 0);
1949 
1950 	uvm_map_check(map, "map_findspace entry");
1951 
1952 	/*
1953 	 * remember the original hint.  if we are aligning, then we
1954 	 * may have to try again with no alignment constraint if
1955 	 * we fail the first time.
1956 	 */
1957 
1958 	orig_hint = hint;
1959 	if (hint < vm_map_min(map)) {	/* check ranges ... */
1960 		if (flags & UVM_FLAG_FIXED) {
1961 			UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0);
1962 			return (NULL);
1963 		}
1964 		hint = vm_map_min(map);
1965 	}
1966 	if (hint > vm_map_max(map)) {
1967 		UVMHIST_LOG(maphist,"<- VA 0x%x > range [0x%x->0x%x]",
1968 		    hint, vm_map_min(map), vm_map_max(map), 0);
1969 		return (NULL);
1970 	}
1971 
1972 	/*
1973 	 * Look for the first possible address; if there's already
1974 	 * something at this address, we have to start after it.
1975 	 */
1976 
1977 	/*
1978 	 * @@@: there are four, no, eight cases to consider.
1979 	 *
1980 	 * 0: found,     fixed,     bottom up -> fail
1981 	 * 1: found,     fixed,     top down  -> fail
1982 	 * 2: found,     not fixed, bottom up -> start after entry->end,
1983 	 *                                       loop up
1984 	 * 3: found,     not fixed, top down  -> start before entry->start,
1985 	 *                                       loop down
1986 	 * 4: not found, fixed,     bottom up -> check entry->next->start, fail
1987 	 * 5: not found, fixed,     top down  -> check entry->next->start, fail
1988 	 * 6: not found, not fixed, bottom up -> check entry->next->start,
1989 	 *                                       loop up
1990 	 * 7: not found, not fixed, top down  -> check entry->next->start,
1991 	 *                                       loop down
1992 	 *
1993 	 * as you can see, it reduces to roughly five cases, and that
1994 	 * adding top down mapping only adds one unique case (without
1995 	 * it, there would be four cases).
1996 	 */
1997 
1998 	if ((flags & UVM_FLAG_FIXED) == 0 && hint == vm_map_min(map)) {
1999 		entry = map->first_free;
2000 	} else {
2001 		if (uvm_map_lookup_entry(map, hint, &entry)) {
2002 			/* "hint" address already in use ... */
2003 			if (flags & UVM_FLAG_FIXED) {
2004 				UVMHIST_LOG(maphist, "<- fixed & VA in use",
2005 				    0, 0, 0, 0);
2006 				return (NULL);
2007 			}
2008 			if (topdown)
2009 				/* Start from lower gap. */
2010 				entry = entry->prev;
2011 		} else if (flags & UVM_FLAG_FIXED) {
2012 			if (entry->next->start >= hint + length &&
2013 			    hint + length > hint)
2014 				goto found;
2015 
2016 			/* "hint" address is gap but too small */
2017 			UVMHIST_LOG(maphist, "<- fixed mapping failed",
2018 			    0, 0, 0, 0);
2019 			return (NULL); /* only one shot at it ... */
2020 		} else {
2021 			/*
2022 			 * See if given hint fits in this gap.
2023 			 */
2024 			switch (uvm_map_space_avail(&hint, length,
2025 			    uoffset, align, topdown, entry)) {
2026 			case 1:
2027 				goto found;
2028 			case -1:
2029 				goto wraparound;
2030 			}
2031 
2032 			if (topdown) {
2033 				/*
2034 				 * Still there is a chance to fit
2035 				 * if hint > entry->end.
2036 				 */
2037 			} else {
2038 				/* Start from higher gap. */
2039 				entry = entry->next;
2040 				if (entry == &map->header)
2041 					goto notfound;
2042 				goto nextgap;
2043 			}
2044 		}
2045 	}
2046 
2047 	/*
2048 	 * Note that all UVM_FLAGS_FIXED case is already handled.
2049 	 */
2050 	KDASSERT((flags & UVM_FLAG_FIXED) == 0);
2051 
2052 	/* Try to find the space in the red-black tree */
2053 
2054 	/* Check slot before any entry */
2055 	hint = topdown ? entry->next->start - length : entry->end;
2056 	switch (uvm_map_space_avail(&hint, length, uoffset, align,
2057 	    topdown, entry)) {
2058 	case 1:
2059 		goto found;
2060 	case -1:
2061 		goto wraparound;
2062 	}
2063 
2064 nextgap:
2065 	KDASSERT((flags & UVM_FLAG_FIXED) == 0);
2066 	/* If there is not enough space in the whole tree, we fail */
2067 	tmp = ROOT_ENTRY(map);
2068 	if (tmp == NULL || tmp->maxgap < length)
2069 		goto notfound;
2070 
2071 	prev = NULL; /* previous candidate */
2072 
2073 	/* Find an entry close to hint that has enough space */
2074 	for (; tmp;) {
2075 		KASSERT(tmp->next->start == tmp->end + tmp->gap);
2076 		if (topdown) {
2077 			if (tmp->next->start < hint + length &&
2078 			    (prev == NULL || tmp->end > prev->end)) {
2079 				if (tmp->gap >= length)
2080 					prev = tmp;
2081 				else if ((child = LEFT_ENTRY(tmp)) != NULL
2082 				    && child->maxgap >= length)
2083 					prev = tmp;
2084 			}
2085 		} else {
2086 			if (tmp->end >= hint &&
2087 			    (prev == NULL || tmp->end < prev->end)) {
2088 				if (tmp->gap >= length)
2089 					prev = tmp;
2090 				else if ((child = RIGHT_ENTRY(tmp)) != NULL
2091 				    && child->maxgap >= length)
2092 					prev = tmp;
2093 			}
2094 		}
2095 		if (tmp->next->start < hint + length)
2096 			child = RIGHT_ENTRY(tmp);
2097 		else if (tmp->end > hint)
2098 			child = LEFT_ENTRY(tmp);
2099 		else {
2100 			if (tmp->gap >= length)
2101 				break;
2102 			if (topdown)
2103 				child = LEFT_ENTRY(tmp);
2104 			else
2105 				child = RIGHT_ENTRY(tmp);
2106 		}
2107 		if (child == NULL || child->maxgap < length)
2108 			break;
2109 		tmp = child;
2110 	}
2111 
2112 	if (tmp != NULL && tmp->start < hint && hint < tmp->next->start) {
2113 		/*
2114 		 * Check if the entry that we found satifies the
2115 		 * space requirement
2116 		 */
2117 		if (topdown) {
2118 			if (hint > tmp->next->start - length)
2119 				hint = tmp->next->start - length;
2120 		} else {
2121 			if (hint < tmp->end)
2122 				hint = tmp->end;
2123 		}
2124 		switch (uvm_map_space_avail(&hint, length, uoffset, align,
2125 		    topdown, tmp)) {
2126 		case 1:
2127 			entry = tmp;
2128 			goto found;
2129 		case -1:
2130 			goto wraparound;
2131 		}
2132 		if (tmp->gap >= length)
2133 			goto listsearch;
2134 	}
2135 	if (prev == NULL)
2136 		goto notfound;
2137 
2138 	if (topdown) {
2139 		KASSERT(orig_hint >= prev->next->start - length ||
2140 		    prev->next->start - length > prev->next->start);
2141 		hint = prev->next->start - length;
2142 	} else {
2143 		KASSERT(orig_hint <= prev->end);
2144 		hint = prev->end;
2145 	}
2146 	switch (uvm_map_space_avail(&hint, length, uoffset, align,
2147 	    topdown, prev)) {
2148 	case 1:
2149 		entry = prev;
2150 		goto found;
2151 	case -1:
2152 		goto wraparound;
2153 	}
2154 	if (prev->gap >= length)
2155 		goto listsearch;
2156 
2157 	if (topdown)
2158 		tmp = LEFT_ENTRY(prev);
2159 	else
2160 		tmp = RIGHT_ENTRY(prev);
2161 	for (;;) {
2162 		KASSERT(tmp && tmp->maxgap >= length);
2163 		if (topdown)
2164 			child = RIGHT_ENTRY(tmp);
2165 		else
2166 			child = LEFT_ENTRY(tmp);
2167 		if (child && child->maxgap >= length) {
2168 			tmp = child;
2169 			continue;
2170 		}
2171 		if (tmp->gap >= length)
2172 			break;
2173 		if (topdown)
2174 			tmp = LEFT_ENTRY(tmp);
2175 		else
2176 			tmp = RIGHT_ENTRY(tmp);
2177 	}
2178 
2179 	if (topdown) {
2180 		KASSERT(orig_hint >= tmp->next->start - length ||
2181 		    tmp->next->start - length > tmp->next->start);
2182 		hint = tmp->next->start - length;
2183 	} else {
2184 		KASSERT(orig_hint <= tmp->end);
2185 		hint = tmp->end;
2186 	}
2187 	switch (uvm_map_space_avail(&hint, length, uoffset, align,
2188 	    topdown, tmp)) {
2189 	case 1:
2190 		entry = tmp;
2191 		goto found;
2192 	case -1:
2193 		goto wraparound;
2194 	}
2195 
2196 	/*
2197 	 * The tree fails to find an entry because of offset or alignment
2198 	 * restrictions.  Search the list instead.
2199 	 */
2200  listsearch:
2201 	/*
2202 	 * Look through the rest of the map, trying to fit a new region in
2203 	 * the gap between existing regions, or after the very last region.
2204 	 * note: entry->end = base VA of current gap,
2205 	 *	 entry->next->start = VA of end of current gap
2206 	 */
2207 
2208 	for (;;) {
2209 		/* Update hint for current gap. */
2210 		hint = topdown ? entry->next->start - length : entry->end;
2211 
2212 		/* See if it fits. */
2213 		switch (uvm_map_space_avail(&hint, length, uoffset, align,
2214 		    topdown, entry)) {
2215 		case 1:
2216 			goto found;
2217 		case -1:
2218 			goto wraparound;
2219 		}
2220 
2221 		/* Advance to next/previous gap */
2222 		if (topdown) {
2223 			if (entry == &map->header) {
2224 				UVMHIST_LOG(maphist, "<- failed (off start)",
2225 				    0,0,0,0);
2226 				goto notfound;
2227 			}
2228 			entry = entry->prev;
2229 		} else {
2230 			entry = entry->next;
2231 			if (entry == &map->header) {
2232 				UVMHIST_LOG(maphist, "<- failed (off end)",
2233 				    0,0,0,0);
2234 				goto notfound;
2235 			}
2236 		}
2237 	}
2238 
2239  found:
2240 	SAVE_HINT(map, map->hint, entry);
2241 	*result = hint;
2242 	UVMHIST_LOG(maphist,"<- got it!  (result=0x%x)", hint, 0,0,0);
2243 	KASSERT( topdown || hint >= orig_hint);
2244 	KASSERT(!topdown || hint <= orig_hint);
2245 	KASSERT(entry->end <= hint);
2246 	KASSERT(hint + length <= entry->next->start);
2247 	return (entry);
2248 
2249  wraparound:
2250 	UVMHIST_LOG(maphist, "<- failed (wrap around)", 0,0,0,0);
2251 
2252 	return (NULL);
2253 
2254  notfound:
2255 	UVMHIST_LOG(maphist, "<- failed (notfound)", 0,0,0,0);
2256 
2257 	return (NULL);
2258 }
2259 
2260 /*
2261  *   U N M A P   -   m a i n   h e l p e r   f u n c t i o n s
2262  */
2263 
2264 /*
2265  * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop")
2266  *
2267  * => caller must check alignment and size
2268  * => map must be locked by caller
2269  * => we return a list of map entries that we've remove from the map
2270  *    in "entry_list"
2271  */
2272 
2273 void
2274 uvm_unmap_remove(struct vm_map *map, vaddr_t start, vaddr_t end,
2275     struct vm_map_entry **entry_list /* OUT */,
2276     struct uvm_mapent_reservation *umr, int flags)
2277 {
2278 	struct vm_map_entry *entry, *first_entry, *next;
2279 	vaddr_t len;
2280 	UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist);
2281 
2282 	UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)",
2283 	    map, start, end, 0);
2284 	VM_MAP_RANGE_CHECK(map, start, end);
2285 
2286 	uvm_map_check(map, "unmap_remove entry");
2287 
2288 	/*
2289 	 * find first entry
2290 	 */
2291 
2292 	if (uvm_map_lookup_entry(map, start, &first_entry) == true) {
2293 		/* clip and go... */
2294 		entry = first_entry;
2295 		UVM_MAP_CLIP_START(map, entry, start, umr);
2296 		/* critical!  prevents stale hint */
2297 		SAVE_HINT(map, entry, entry->prev);
2298 	} else {
2299 		entry = first_entry->next;
2300 	}
2301 
2302 	/*
2303 	 * Save the free space hint
2304 	 */
2305 
2306 	if (map->first_free != &map->header && map->first_free->start >= start)
2307 		map->first_free = entry->prev;
2308 
2309 	/*
2310 	 * note: we now re-use first_entry for a different task.  we remove
2311 	 * a number of map entries from the map and save them in a linked
2312 	 * list headed by "first_entry".  once we remove them from the map
2313 	 * the caller should unlock the map and drop the references to the
2314 	 * backing objects [c.f. uvm_unmap_detach].  the object is to
2315 	 * separate unmapping from reference dropping.  why?
2316 	 *   [1] the map has to be locked for unmapping
2317 	 *   [2] the map need not be locked for reference dropping
2318 	 *   [3] dropping references may trigger pager I/O, and if we hit
2319 	 *       a pager that does synchronous I/O we may have to wait for it.
2320 	 *   [4] we would like all waiting for I/O to occur with maps unlocked
2321 	 *       so that we don't block other threads.
2322 	 */
2323 
2324 	first_entry = NULL;
2325 	*entry_list = NULL;
2326 
2327 	/*
2328 	 * break up the area into map entry sized regions and unmap.  note
2329 	 * that all mappings have to be removed before we can even consider
2330 	 * dropping references to amaps or VM objects (otherwise we could end
2331 	 * up with a mapping to a page on the free list which would be very bad)
2332 	 */
2333 
2334 	while ((entry != &map->header) && (entry->start < end)) {
2335 		KASSERT((entry->flags & UVM_MAP_FIRST) == 0);
2336 
2337 		UVM_MAP_CLIP_END(map, entry, end, umr);
2338 		next = entry->next;
2339 		len = entry->end - entry->start;
2340 
2341 		/*
2342 		 * unwire before removing addresses from the pmap; otherwise
2343 		 * unwiring will put the entries back into the pmap (XXX).
2344 		 */
2345 
2346 		if (VM_MAPENT_ISWIRED(entry)) {
2347 			uvm_map_entry_unwire(map, entry);
2348 		}
2349 		if (flags & UVM_FLAG_VAONLY) {
2350 
2351 			/* nothing */
2352 
2353 		} else if ((map->flags & VM_MAP_PAGEABLE) == 0) {
2354 
2355 			/*
2356 			 * if the map is non-pageable, any pages mapped there
2357 			 * must be wired and entered with pmap_kenter_pa(),
2358 			 * and we should free any such pages immediately.
2359 			 * this is mostly used for kmem_map.
2360 			 */
2361 			KASSERT(vm_map_pmap(map) == pmap_kernel());
2362 
2363 			if ((entry->flags & UVM_MAP_KMAPENT) == 0) {
2364 				uvm_km_pgremove_intrsafe(map, entry->start,
2365 				    entry->end);
2366 				pmap_kremove(entry->start, len);
2367 			}
2368 		} else if (UVM_ET_ISOBJ(entry) &&
2369 			   UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) {
2370 			KASSERT(vm_map_pmap(map) == pmap_kernel());
2371 
2372 			/*
2373 			 * note: kernel object mappings are currently used in
2374 			 * two ways:
2375 			 *  [1] "normal" mappings of pages in the kernel object
2376 			 *  [2] uvm_km_valloc'd allocations in which we
2377 			 *      pmap_enter in some non-kernel-object page
2378 			 *      (e.g. vmapbuf).
2379 			 *
2380 			 * for case [1], we need to remove the mapping from
2381 			 * the pmap and then remove the page from the kernel
2382 			 * object (because, once pages in a kernel object are
2383 			 * unmapped they are no longer needed, unlike, say,
2384 			 * a vnode where you might want the data to persist
2385 			 * until flushed out of a queue).
2386 			 *
2387 			 * for case [2], we need to remove the mapping from
2388 			 * the pmap.  there shouldn't be any pages at the
2389 			 * specified offset in the kernel object [but it
2390 			 * doesn't hurt to call uvm_km_pgremove just to be
2391 			 * safe?]
2392 			 *
2393 			 * uvm_km_pgremove currently does the following:
2394 			 *   for pages in the kernel object in range:
2395 			 *     - drops the swap slot
2396 			 *     - uvm_pagefree the page
2397 			 */
2398 
2399 			/*
2400 			 * remove mappings from pmap and drop the pages
2401 			 * from the object.  offsets are always relative
2402 			 * to vm_map_min(kernel_map).
2403 			 */
2404 
2405 			pmap_remove(pmap_kernel(), entry->start,
2406 			    entry->start + len);
2407 			uvm_km_pgremove(entry->start, entry->end);
2408 
2409 			/*
2410 			 * null out kernel_object reference, we've just
2411 			 * dropped it
2412 			 */
2413 
2414 			entry->etype &= ~UVM_ET_OBJ;
2415 			entry->object.uvm_obj = NULL;
2416 		} else if (UVM_ET_ISOBJ(entry) || entry->aref.ar_amap) {
2417 
2418 			/*
2419 			 * remove mappings the standard way.
2420 			 */
2421 
2422 			pmap_remove(map->pmap, entry->start, entry->end);
2423 		}
2424 
2425 #if defined(DEBUG)
2426 		if ((entry->flags & UVM_MAP_KMAPENT) == 0) {
2427 
2428 			/*
2429 			 * check if there's remaining mapping,
2430 			 * which is a bug in caller.
2431 			 */
2432 
2433 			vaddr_t va;
2434 			for (va = entry->start; va < entry->end;
2435 			    va += PAGE_SIZE) {
2436 				if (pmap_extract(vm_map_pmap(map), va, NULL)) {
2437 					panic("uvm_unmap_remove: has mapping");
2438 				}
2439 			}
2440 
2441 			if (VM_MAP_IS_KERNEL(map)) {
2442 				uvm_km_check_empty(map, entry->start,
2443 				    entry->end);
2444 			}
2445 		}
2446 #endif /* defined(DEBUG) */
2447 
2448 		/*
2449 		 * remove entry from map and put it on our list of entries
2450 		 * that we've nuked.  then go to next entry.
2451 		 */
2452 
2453 		UVMHIST_LOG(maphist, "  removed map entry 0x%x", entry, 0, 0,0);
2454 
2455 		/* critical!  prevents stale hint */
2456 		SAVE_HINT(map, entry, entry->prev);
2457 
2458 		uvm_map_entry_unlink(map, entry);
2459 		KASSERT(map->size >= len);
2460 		map->size -= len;
2461 		entry->prev = NULL;
2462 		entry->next = first_entry;
2463 		first_entry = entry;
2464 		entry = next;
2465 	}
2466 
2467 	/*
2468 	 * Note: if map is dying, leave pmap_update() for pmap_destroy(),
2469 	 * which will be called later.
2470 	 */
2471 	if ((map->flags & VM_MAP_DYING) == 0) {
2472 		pmap_update(vm_map_pmap(map));
2473 	} else {
2474 		KASSERT(vm_map_pmap(map) != pmap_kernel());
2475 	}
2476 
2477 	uvm_map_check(map, "unmap_remove leave");
2478 
2479 	/*
2480 	 * now we've cleaned up the map and are ready for the caller to drop
2481 	 * references to the mapped objects.
2482 	 */
2483 
2484 	*entry_list = first_entry;
2485 	UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0);
2486 
2487 	if (map->flags & VM_MAP_WANTVA) {
2488 		mutex_enter(&map->misc_lock);
2489 		map->flags &= ~VM_MAP_WANTVA;
2490 		cv_broadcast(&map->cv);
2491 		mutex_exit(&map->misc_lock);
2492 	}
2493 }
2494 
2495 /*
2496  * uvm_unmap_detach: drop references in a chain of map entries
2497  *
2498  * => we will free the map entries as we traverse the list.
2499  */
2500 
2501 void
2502 uvm_unmap_detach(struct vm_map_entry *first_entry, int flags)
2503 {
2504 	struct vm_map_entry *next_entry;
2505 	UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist);
2506 
2507 	while (first_entry) {
2508 		KASSERT(!VM_MAPENT_ISWIRED(first_entry));
2509 		UVMHIST_LOG(maphist,
2510 		    "  detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d",
2511 		    first_entry, first_entry->aref.ar_amap,
2512 		    first_entry->object.uvm_obj,
2513 		    UVM_ET_ISSUBMAP(first_entry));
2514 
2515 		/*
2516 		 * drop reference to amap, if we've got one
2517 		 */
2518 
2519 		if (first_entry->aref.ar_amap)
2520 			uvm_map_unreference_amap(first_entry, flags);
2521 
2522 		/*
2523 		 * drop reference to our backing object, if we've got one
2524 		 */
2525 
2526 		KASSERT(!UVM_ET_ISSUBMAP(first_entry));
2527 		if (UVM_ET_ISOBJ(first_entry) &&
2528 		    first_entry->object.uvm_obj->pgops->pgo_detach) {
2529 			(*first_entry->object.uvm_obj->pgops->pgo_detach)
2530 				(first_entry->object.uvm_obj);
2531 		}
2532 		next_entry = first_entry->next;
2533 		uvm_mapent_free(first_entry);
2534 		first_entry = next_entry;
2535 	}
2536 	UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
2537 }
2538 
2539 /*
2540  *   E X T R A C T I O N   F U N C T I O N S
2541  */
2542 
2543 /*
2544  * uvm_map_reserve: reserve space in a vm_map for future use.
2545  *
2546  * => we reserve space in a map by putting a dummy map entry in the
2547  *    map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE)
2548  * => map should be unlocked (we will write lock it)
2549  * => we return true if we were able to reserve space
2550  * => XXXCDC: should be inline?
2551  */
2552 
2553 int
2554 uvm_map_reserve(struct vm_map *map, vsize_t size,
2555     vaddr_t offset	/* hint for pmap_prefer */,
2556     vsize_t align	/* alignment */,
2557     vaddr_t *raddr	/* IN:hint, OUT: reserved VA */,
2558     uvm_flag_t flags	/* UVM_FLAG_FIXED or 0 */)
2559 {
2560 	UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist);
2561 
2562 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)",
2563 	    map,size,offset,raddr);
2564 
2565 	size = round_page(size);
2566 
2567 	/*
2568 	 * reserve some virtual space.
2569 	 */
2570 
2571 	if (uvm_map(map, raddr, size, NULL, offset, align,
2572 	    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
2573 	    UVM_ADV_RANDOM, UVM_FLAG_NOMERGE|flags)) != 0) {
2574 	    UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
2575 		return (false);
2576 	}
2577 
2578 	UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0);
2579 	return (true);
2580 }
2581 
2582 /*
2583  * uvm_map_replace: replace a reserved (blank) area of memory with
2584  * real mappings.
2585  *
2586  * => caller must WRITE-LOCK the map
2587  * => we return true if replacement was a success
2588  * => we expect the newents chain to have nnewents entrys on it and
2589  *    we expect newents->prev to point to the last entry on the list
2590  * => note newents is allowed to be NULL
2591  */
2592 
2593 static int
2594 uvm_map_replace(struct vm_map *map, vaddr_t start, vaddr_t end,
2595     struct vm_map_entry *newents, int nnewents, vsize_t nsize,
2596     struct vm_map_entry **oldentryp)
2597 {
2598 	struct vm_map_entry *oldent, *last;
2599 
2600 	uvm_map_check(map, "map_replace entry");
2601 
2602 	/*
2603 	 * first find the blank map entry at the specified address
2604 	 */
2605 
2606 	if (!uvm_map_lookup_entry(map, start, &oldent)) {
2607 		return (false);
2608 	}
2609 
2610 	/*
2611 	 * check to make sure we have a proper blank entry
2612 	 */
2613 
2614 	if (end < oldent->end && !VM_MAP_USE_KMAPENT(map)) {
2615 		UVM_MAP_CLIP_END(map, oldent, end, NULL);
2616 	}
2617 	if (oldent->start != start || oldent->end != end ||
2618 	    oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) {
2619 		return (false);
2620 	}
2621 
2622 #ifdef DIAGNOSTIC
2623 
2624 	/*
2625 	 * sanity check the newents chain
2626 	 */
2627 
2628 	{
2629 		struct vm_map_entry *tmpent = newents;
2630 		int nent = 0;
2631 		vsize_t sz = 0;
2632 		vaddr_t cur = start;
2633 
2634 		while (tmpent) {
2635 			nent++;
2636 			sz += tmpent->end - tmpent->start;
2637 			if (tmpent->start < cur)
2638 				panic("uvm_map_replace1");
2639 			if (tmpent->start >= tmpent->end || tmpent->end > end) {
2640 				panic("uvm_map_replace2: "
2641 				    "tmpent->start=0x%"PRIxVADDR
2642 				    ", tmpent->end=0x%"PRIxVADDR
2643 				    ", end=0x%"PRIxVADDR,
2644 				    tmpent->start, tmpent->end, end);
2645 			}
2646 			cur = tmpent->end;
2647 			if (tmpent->next) {
2648 				if (tmpent->next->prev != tmpent)
2649 					panic("uvm_map_replace3");
2650 			} else {
2651 				if (newents->prev != tmpent)
2652 					panic("uvm_map_replace4");
2653 			}
2654 			tmpent = tmpent->next;
2655 		}
2656 		if (nent != nnewents)
2657 			panic("uvm_map_replace5");
2658 		if (sz != nsize)
2659 			panic("uvm_map_replace6");
2660 	}
2661 #endif
2662 
2663 	/*
2664 	 * map entry is a valid blank!   replace it.   (this does all the
2665 	 * work of map entry link/unlink...).
2666 	 */
2667 
2668 	if (newents) {
2669 		last = newents->prev;
2670 
2671 		/* critical: flush stale hints out of map */
2672 		SAVE_HINT(map, map->hint, newents);
2673 		if (map->first_free == oldent)
2674 			map->first_free = last;
2675 
2676 		last->next = oldent->next;
2677 		last->next->prev = last;
2678 
2679 		/* Fix RB tree */
2680 		uvm_rb_remove(map, oldent);
2681 
2682 		newents->prev = oldent->prev;
2683 		newents->prev->next = newents;
2684 		map->nentries = map->nentries + (nnewents - 1);
2685 
2686 		/* Fixup the RB tree */
2687 		{
2688 			int i;
2689 			struct vm_map_entry *tmp;
2690 
2691 			tmp = newents;
2692 			for (i = 0; i < nnewents && tmp; i++) {
2693 				uvm_rb_insert(map, tmp);
2694 				tmp = tmp->next;
2695 			}
2696 		}
2697 	} else {
2698 		/* NULL list of new entries: just remove the old one */
2699 		clear_hints(map, oldent);
2700 		uvm_map_entry_unlink(map, oldent);
2701 	}
2702 	map->size -= end - start - nsize;
2703 
2704 	uvm_map_check(map, "map_replace leave");
2705 
2706 	/*
2707 	 * now we can free the old blank entry and return.
2708 	 */
2709 
2710 	*oldentryp = oldent;
2711 	return (true);
2712 }
2713 
2714 /*
2715  * uvm_map_extract: extract a mapping from a map and put it somewhere
2716  *	(maybe removing the old mapping)
2717  *
2718  * => maps should be unlocked (we will write lock them)
2719  * => returns 0 on success, error code otherwise
2720  * => start must be page aligned
2721  * => len must be page sized
2722  * => flags:
2723  *      UVM_EXTRACT_REMOVE: remove mappings from srcmap
2724  *      UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only)
2725  *      UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs
2726  *      UVM_EXTRACT_FIXPROT: set prot to maxprot as we go
2727  *    >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<<
2728  *    >>>NOTE: QREF's must be unmapped via the QREF path, thus should only
2729  *             be used from within the kernel in a kernel level map <<<
2730  */
2731 
2732 int
2733 uvm_map_extract(struct vm_map *srcmap, vaddr_t start, vsize_t len,
2734     struct vm_map *dstmap, vaddr_t *dstaddrp, int flags)
2735 {
2736 	vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge;
2737 	struct vm_map_entry *chain, *endchain, *entry, *orig_entry, *newentry,
2738 	    *deadentry, *oldentry;
2739 	struct vm_map_entry *resentry = NULL; /* a dummy reservation entry */
2740 	vsize_t elen;
2741 	int nchain, error, copy_ok;
2742 	vsize_t nsize;
2743 	UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist);
2744 
2745 	UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start,
2746 	    len,0);
2747 	UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0);
2748 
2749 	/*
2750 	 * step 0: sanity check: start must be on a page boundary, length
2751 	 * must be page sized.  can't ask for CONTIG/QREF if you asked for
2752 	 * REMOVE.
2753 	 */
2754 
2755 	KASSERT((start & PAGE_MASK) == 0 && (len & PAGE_MASK) == 0);
2756 	KASSERT((flags & UVM_EXTRACT_REMOVE) == 0 ||
2757 		(flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) == 0);
2758 
2759 	/*
2760 	 * step 1: reserve space in the target map for the extracted area
2761 	 */
2762 
2763 	if ((flags & UVM_EXTRACT_RESERVED) == 0) {
2764 		dstaddr = vm_map_min(dstmap);
2765 		if (!uvm_map_reserve(dstmap, len, start, 0, &dstaddr, 0))
2766 			return (ENOMEM);
2767 		*dstaddrp = dstaddr;	/* pass address back to caller */
2768 		UVMHIST_LOG(maphist, "  dstaddr=0x%x", dstaddr,0,0,0);
2769 	} else {
2770 		dstaddr = *dstaddrp;
2771 	}
2772 
2773 	/*
2774 	 * step 2: setup for the extraction process loop by init'ing the
2775 	 * map entry chain, locking src map, and looking up the first useful
2776 	 * entry in the map.
2777 	 */
2778 
2779 	end = start + len;
2780 	newend = dstaddr + len;
2781 	chain = endchain = NULL;
2782 	nchain = 0;
2783 	nsize = 0;
2784 	vm_map_lock(srcmap);
2785 
2786 	if (uvm_map_lookup_entry(srcmap, start, &entry)) {
2787 
2788 		/* "start" is within an entry */
2789 		if (flags & UVM_EXTRACT_QREF) {
2790 
2791 			/*
2792 			 * for quick references we don't clip the entry, so
2793 			 * the entry may map space "before" the starting
2794 			 * virtual address... this is the "fudge" factor
2795 			 * (which can be non-zero only the first time
2796 			 * through the "while" loop in step 3).
2797 			 */
2798 
2799 			fudge = start - entry->start;
2800 		} else {
2801 
2802 			/*
2803 			 * normal reference: we clip the map to fit (thus
2804 			 * fudge is zero)
2805 			 */
2806 
2807 			UVM_MAP_CLIP_START(srcmap, entry, start, NULL);
2808 			SAVE_HINT(srcmap, srcmap->hint, entry->prev);
2809 			fudge = 0;
2810 		}
2811 	} else {
2812 
2813 		/* "start" is not within an entry ... skip to next entry */
2814 		if (flags & UVM_EXTRACT_CONTIG) {
2815 			error = EINVAL;
2816 			goto bad;    /* definite hole here ... */
2817 		}
2818 
2819 		entry = entry->next;
2820 		fudge = 0;
2821 	}
2822 
2823 	/* save values from srcmap for step 6 */
2824 	orig_entry = entry;
2825 	orig_fudge = fudge;
2826 
2827 	/*
2828 	 * step 3: now start looping through the map entries, extracting
2829 	 * as we go.
2830 	 */
2831 
2832 	while (entry->start < end && entry != &srcmap->header) {
2833 
2834 		/* if we are not doing a quick reference, clip it */
2835 		if ((flags & UVM_EXTRACT_QREF) == 0)
2836 			UVM_MAP_CLIP_END(srcmap, entry, end, NULL);
2837 
2838 		/* clear needs_copy (allow chunking) */
2839 		if (UVM_ET_ISNEEDSCOPY(entry)) {
2840 			amap_copy(srcmap, entry,
2841 			    AMAP_COPY_NOWAIT|AMAP_COPY_NOMERGE, start, end);
2842 			if (UVM_ET_ISNEEDSCOPY(entry)) {  /* failed? */
2843 				error = ENOMEM;
2844 				goto bad;
2845 			}
2846 
2847 			/* amap_copy could clip (during chunk)!  update fudge */
2848 			if (fudge) {
2849 				fudge = start - entry->start;
2850 				orig_fudge = fudge;
2851 			}
2852 		}
2853 
2854 		/* calculate the offset of this from "start" */
2855 		oldoffset = (entry->start + fudge) - start;
2856 
2857 		/* allocate a new map entry */
2858 		newentry = uvm_mapent_alloc(dstmap, 0);
2859 		if (newentry == NULL) {
2860 			error = ENOMEM;
2861 			goto bad;
2862 		}
2863 
2864 		/* set up new map entry */
2865 		newentry->next = NULL;
2866 		newentry->prev = endchain;
2867 		newentry->start = dstaddr + oldoffset;
2868 		newentry->end =
2869 		    newentry->start + (entry->end - (entry->start + fudge));
2870 		if (newentry->end > newend || newentry->end < newentry->start)
2871 			newentry->end = newend;
2872 		newentry->object.uvm_obj = entry->object.uvm_obj;
2873 		if (newentry->object.uvm_obj) {
2874 			if (newentry->object.uvm_obj->pgops->pgo_reference)
2875 				newentry->object.uvm_obj->pgops->
2876 				    pgo_reference(newentry->object.uvm_obj);
2877 				newentry->offset = entry->offset + fudge;
2878 		} else {
2879 			newentry->offset = 0;
2880 		}
2881 		newentry->etype = entry->etype;
2882 		newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ?
2883 			entry->max_protection : entry->protection;
2884 		newentry->max_protection = entry->max_protection;
2885 		newentry->inheritance = entry->inheritance;
2886 		newentry->wired_count = 0;
2887 		newentry->aref.ar_amap = entry->aref.ar_amap;
2888 		if (newentry->aref.ar_amap) {
2889 			newentry->aref.ar_pageoff =
2890 			    entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT);
2891 			uvm_map_reference_amap(newentry, AMAP_SHARED |
2892 			    ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0));
2893 		} else {
2894 			newentry->aref.ar_pageoff = 0;
2895 		}
2896 		newentry->advice = entry->advice;
2897 		if ((flags & UVM_EXTRACT_QREF) != 0) {
2898 			newentry->flags |= UVM_MAP_NOMERGE;
2899 		}
2900 
2901 		/* now link it on the chain */
2902 		nchain++;
2903 		nsize += newentry->end - newentry->start;
2904 		if (endchain == NULL) {
2905 			chain = endchain = newentry;
2906 		} else {
2907 			endchain->next = newentry;
2908 			endchain = newentry;
2909 		}
2910 
2911 		/* end of 'while' loop! */
2912 		if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end &&
2913 		    (entry->next == &srcmap->header ||
2914 		    entry->next->start != entry->end)) {
2915 			error = EINVAL;
2916 			goto bad;
2917 		}
2918 		entry = entry->next;
2919 		fudge = 0;
2920 	}
2921 
2922 	/*
2923 	 * step 4: close off chain (in format expected by uvm_map_replace)
2924 	 */
2925 
2926 	if (chain)
2927 		chain->prev = endchain;
2928 
2929 	/*
2930 	 * step 5: attempt to lock the dest map so we can pmap_copy.
2931 	 * note usage of copy_ok:
2932 	 *   1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5)
2933 	 *   0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7
2934 	 */
2935 
2936 	if (srcmap == dstmap || vm_map_lock_try(dstmap) == true) {
2937 		copy_ok = 1;
2938 		if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
2939 		    nchain, nsize, &resentry)) {
2940 			if (srcmap != dstmap)
2941 				vm_map_unlock(dstmap);
2942 			error = EIO;
2943 			goto bad;
2944 		}
2945 	} else {
2946 		copy_ok = 0;
2947 		/* replace defered until step 7 */
2948 	}
2949 
2950 	/*
2951 	 * step 6: traverse the srcmap a second time to do the following:
2952 	 *  - if we got a lock on the dstmap do pmap_copy
2953 	 *  - if UVM_EXTRACT_REMOVE remove the entries
2954 	 * we make use of orig_entry and orig_fudge (saved in step 2)
2955 	 */
2956 
2957 	if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) {
2958 
2959 		/* purge possible stale hints from srcmap */
2960 		if (flags & UVM_EXTRACT_REMOVE) {
2961 			SAVE_HINT(srcmap, srcmap->hint, orig_entry->prev);
2962 			if (srcmap->first_free != &srcmap->header &&
2963 			    srcmap->first_free->start >= start)
2964 				srcmap->first_free = orig_entry->prev;
2965 		}
2966 
2967 		entry = orig_entry;
2968 		fudge = orig_fudge;
2969 		deadentry = NULL;	/* for UVM_EXTRACT_REMOVE */
2970 
2971 		while (entry->start < end && entry != &srcmap->header) {
2972 			if (copy_ok) {
2973 				oldoffset = (entry->start + fudge) - start;
2974 				elen = MIN(end, entry->end) -
2975 				    (entry->start + fudge);
2976 				pmap_copy(dstmap->pmap, srcmap->pmap,
2977 				    dstaddr + oldoffset, elen,
2978 				    entry->start + fudge);
2979 			}
2980 
2981 			/* we advance "entry" in the following if statement */
2982 			if (flags & UVM_EXTRACT_REMOVE) {
2983 				pmap_remove(srcmap->pmap, entry->start,
2984 						entry->end);
2985 				oldentry = entry;	/* save entry */
2986 				entry = entry->next;	/* advance */
2987 				uvm_map_entry_unlink(srcmap, oldentry);
2988 							/* add to dead list */
2989 				oldentry->next = deadentry;
2990 				deadentry = oldentry;
2991 			} else {
2992 				entry = entry->next;		/* advance */
2993 			}
2994 
2995 			/* end of 'while' loop */
2996 			fudge = 0;
2997 		}
2998 		pmap_update(srcmap->pmap);
2999 
3000 		/*
3001 		 * unlock dstmap.  we will dispose of deadentry in
3002 		 * step 7 if needed
3003 		 */
3004 
3005 		if (copy_ok && srcmap != dstmap)
3006 			vm_map_unlock(dstmap);
3007 
3008 	} else {
3009 		deadentry = NULL;
3010 	}
3011 
3012 	/*
3013 	 * step 7: we are done with the source map, unlock.   if copy_ok
3014 	 * is 0 then we have not replaced the dummy mapping in dstmap yet
3015 	 * and we need to do so now.
3016 	 */
3017 
3018 	vm_map_unlock(srcmap);
3019 	if ((flags & UVM_EXTRACT_REMOVE) && deadentry)
3020 		uvm_unmap_detach(deadentry, 0);   /* dispose of old entries */
3021 
3022 	/* now do the replacement if we didn't do it in step 5 */
3023 	if (copy_ok == 0) {
3024 		vm_map_lock(dstmap);
3025 		error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain,
3026 		    nchain, nsize, &resentry);
3027 		vm_map_unlock(dstmap);
3028 
3029 		if (error == false) {
3030 			error = EIO;
3031 			goto bad2;
3032 		}
3033 	}
3034 
3035 	if (resentry != NULL)
3036 		uvm_mapent_free(resentry);
3037 
3038 	return (0);
3039 
3040 	/*
3041 	 * bad: failure recovery
3042 	 */
3043 bad:
3044 	vm_map_unlock(srcmap);
3045 bad2:			/* src already unlocked */
3046 	if (chain)
3047 		uvm_unmap_detach(chain,
3048 		    (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0);
3049 
3050 	if (resentry != NULL)
3051 		uvm_mapent_free(resentry);
3052 
3053 	if ((flags & UVM_EXTRACT_RESERVED) == 0) {
3054 		uvm_unmap(dstmap, dstaddr, dstaddr+len);   /* ??? */
3055 	}
3056 	return (error);
3057 }
3058 
3059 /* end of extraction functions */
3060 
3061 /*
3062  * uvm_map_submap: punch down part of a map into a submap
3063  *
3064  * => only the kernel_map is allowed to be submapped
3065  * => the purpose of submapping is to break up the locking granularity
3066  *	of a larger map
3067  * => the range specified must have been mapped previously with a uvm_map()
3068  *	call [with uobj==NULL] to create a blank map entry in the main map.
3069  *	[And it had better still be blank!]
3070  * => maps which contain submaps should never be copied or forked.
3071  * => to remove a submap, use uvm_unmap() on the main map
3072  *	and then uvm_map_deallocate() the submap.
3073  * => main map must be unlocked.
3074  * => submap must have been init'd and have a zero reference count.
3075  *	[need not be locked as we don't actually reference it]
3076  */
3077 
3078 int
3079 uvm_map_submap(struct vm_map *map, vaddr_t start, vaddr_t end,
3080     struct vm_map *submap)
3081 {
3082 	struct vm_map_entry *entry;
3083 	struct uvm_mapent_reservation umr;
3084 	int error;
3085 
3086 	uvm_mapent_reserve(map, &umr, 2, 0);
3087 
3088 	vm_map_lock(map);
3089 	VM_MAP_RANGE_CHECK(map, start, end);
3090 
3091 	if (uvm_map_lookup_entry(map, start, &entry)) {
3092 		UVM_MAP_CLIP_START(map, entry, start, &umr);
3093 		UVM_MAP_CLIP_END(map, entry, end, &umr);	/* to be safe */
3094 	} else {
3095 		entry = NULL;
3096 	}
3097 
3098 	if (entry != NULL &&
3099 	    entry->start == start && entry->end == end &&
3100 	    entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL &&
3101 	    !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) {
3102 		entry->etype |= UVM_ET_SUBMAP;
3103 		entry->object.sub_map = submap;
3104 		entry->offset = 0;
3105 		uvm_map_reference(submap);
3106 		error = 0;
3107 	} else {
3108 		error = EINVAL;
3109 	}
3110 	vm_map_unlock(map);
3111 
3112 	uvm_mapent_unreserve(map, &umr);
3113 
3114 	return error;
3115 }
3116 
3117 /*
3118  * uvm_map_setup_kernel: init in-kernel map
3119  *
3120  * => map must not be in service yet.
3121  */
3122 
3123 void
3124 uvm_map_setup_kernel(struct vm_map_kernel *map,
3125     vaddr_t vmin, vaddr_t vmax, int flags)
3126 {
3127 
3128 	uvm_map_setup(&map->vmk_map, vmin, vmax, flags);
3129 	callback_head_init(&map->vmk_reclaim_callback, IPL_VM);
3130 	LIST_INIT(&map->vmk_kentry_free);
3131 	map->vmk_merged_entries = NULL;
3132 }
3133 
3134 
3135 /*
3136  * uvm_map_protect: change map protection
3137  *
3138  * => set_max means set max_protection.
3139  * => map must be unlocked.
3140  */
3141 
3142 #define MASK(entry)	(UVM_ET_ISCOPYONWRITE(entry) ? \
3143 			 ~VM_PROT_WRITE : VM_PROT_ALL)
3144 
3145 int
3146 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
3147     vm_prot_t new_prot, bool set_max)
3148 {
3149 	struct vm_map_entry *current, *entry;
3150 	int error = 0;
3151 	UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist);
3152 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)",
3153 		    map, start, end, new_prot);
3154 
3155 	vm_map_lock(map);
3156 	VM_MAP_RANGE_CHECK(map, start, end);
3157 	if (uvm_map_lookup_entry(map, start, &entry)) {
3158 		UVM_MAP_CLIP_START(map, entry, start, NULL);
3159 	} else {
3160 		entry = entry->next;
3161 	}
3162 
3163 	/*
3164 	 * make a first pass to check for protection violations.
3165 	 */
3166 
3167 	current = entry;
3168 	while ((current != &map->header) && (current->start < end)) {
3169 		if (UVM_ET_ISSUBMAP(current)) {
3170 			error = EINVAL;
3171 			goto out;
3172 		}
3173 		if ((new_prot & current->max_protection) != new_prot) {
3174 			error = EACCES;
3175 			goto out;
3176 		}
3177 		/*
3178 		 * Don't allow VM_PROT_EXECUTE to be set on entries that
3179 		 * point to vnodes that are associated with a NOEXEC file
3180 		 * system.
3181 		 */
3182 		if (UVM_ET_ISOBJ(current) &&
3183 		    UVM_OBJ_IS_VNODE(current->object.uvm_obj)) {
3184 			struct vnode *vp =
3185 			    (struct vnode *) current->object.uvm_obj;
3186 
3187 			if ((new_prot & VM_PROT_EXECUTE) != 0 &&
3188 			    (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
3189 				error = EACCES;
3190 				goto out;
3191 			}
3192 		}
3193 
3194 		current = current->next;
3195 	}
3196 
3197 	/* go back and fix up protections (no need to clip this time). */
3198 
3199 	current = entry;
3200 	while ((current != &map->header) && (current->start < end)) {
3201 		vm_prot_t old_prot;
3202 
3203 		UVM_MAP_CLIP_END(map, current, end, NULL);
3204 		old_prot = current->protection;
3205 		if (set_max)
3206 			current->protection =
3207 			    (current->max_protection = new_prot) & old_prot;
3208 		else
3209 			current->protection = new_prot;
3210 
3211 		/*
3212 		 * update physical map if necessary.  worry about copy-on-write
3213 		 * here -- CHECK THIS XXX
3214 		 */
3215 
3216 		if (current->protection != old_prot) {
3217 			/* update pmap! */
3218 			pmap_protect(map->pmap, current->start, current->end,
3219 			    current->protection & MASK(entry));
3220 
3221 			/*
3222 			 * If this entry points at a vnode, and the
3223 			 * protection includes VM_PROT_EXECUTE, mark
3224 			 * the vnode as VEXECMAP.
3225 			 */
3226 			if (UVM_ET_ISOBJ(current)) {
3227 				struct uvm_object *uobj =
3228 				    current->object.uvm_obj;
3229 
3230 				if (UVM_OBJ_IS_VNODE(uobj) &&
3231 				    (current->protection & VM_PROT_EXECUTE)) {
3232 					vn_markexec((struct vnode *) uobj);
3233 				}
3234 			}
3235 		}
3236 
3237 		/*
3238 		 * If the map is configured to lock any future mappings,
3239 		 * wire this entry now if the old protection was VM_PROT_NONE
3240 		 * and the new protection is not VM_PROT_NONE.
3241 		 */
3242 
3243 		if ((map->flags & VM_MAP_WIREFUTURE) != 0 &&
3244 		    VM_MAPENT_ISWIRED(entry) == 0 &&
3245 		    old_prot == VM_PROT_NONE &&
3246 		    new_prot != VM_PROT_NONE) {
3247 			if (uvm_map_pageable(map, entry->start,
3248 			    entry->end, false,
3249 			    UVM_LK_ENTER|UVM_LK_EXIT) != 0) {
3250 
3251 				/*
3252 				 * If locking the entry fails, remember the
3253 				 * error if it's the first one.  Note we
3254 				 * still continue setting the protection in
3255 				 * the map, but will return the error
3256 				 * condition regardless.
3257 				 *
3258 				 * XXX Ignore what the actual error is,
3259 				 * XXX just call it a resource shortage
3260 				 * XXX so that it doesn't get confused
3261 				 * XXX what uvm_map_protect() itself would
3262 				 * XXX normally return.
3263 				 */
3264 
3265 				error = ENOMEM;
3266 			}
3267 		}
3268 		current = current->next;
3269 	}
3270 	pmap_update(map->pmap);
3271 
3272  out:
3273 	vm_map_unlock(map);
3274 
3275 	UVMHIST_LOG(maphist, "<- done, error=%d",error,0,0,0);
3276 	return error;
3277 }
3278 
3279 #undef  MASK
3280 
3281 /*
3282  * uvm_map_inherit: set inheritance code for range of addrs in map.
3283  *
3284  * => map must be unlocked
3285  * => note that the inherit code is used during a "fork".  see fork
3286  *	code for details.
3287  */
3288 
3289 int
3290 uvm_map_inherit(struct vm_map *map, vaddr_t start, vaddr_t end,
3291     vm_inherit_t new_inheritance)
3292 {
3293 	struct vm_map_entry *entry, *temp_entry;
3294 	UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist);
3295 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)",
3296 	    map, start, end, new_inheritance);
3297 
3298 	switch (new_inheritance) {
3299 	case MAP_INHERIT_NONE:
3300 	case MAP_INHERIT_COPY:
3301 	case MAP_INHERIT_SHARE:
3302 		break;
3303 	default:
3304 		UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
3305 		return EINVAL;
3306 	}
3307 
3308 	vm_map_lock(map);
3309 	VM_MAP_RANGE_CHECK(map, start, end);
3310 	if (uvm_map_lookup_entry(map, start, &temp_entry)) {
3311 		entry = temp_entry;
3312 		UVM_MAP_CLIP_START(map, entry, start, NULL);
3313 	}  else {
3314 		entry = temp_entry->next;
3315 	}
3316 	while ((entry != &map->header) && (entry->start < end)) {
3317 		UVM_MAP_CLIP_END(map, entry, end, NULL);
3318 		entry->inheritance = new_inheritance;
3319 		entry = entry->next;
3320 	}
3321 	vm_map_unlock(map);
3322 	UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
3323 	return 0;
3324 }
3325 
3326 /*
3327  * uvm_map_advice: set advice code for range of addrs in map.
3328  *
3329  * => map must be unlocked
3330  */
3331 
3332 int
3333 uvm_map_advice(struct vm_map *map, vaddr_t start, vaddr_t end, int new_advice)
3334 {
3335 	struct vm_map_entry *entry, *temp_entry;
3336 	UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist);
3337 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)",
3338 	    map, start, end, new_advice);
3339 
3340 	vm_map_lock(map);
3341 	VM_MAP_RANGE_CHECK(map, start, end);
3342 	if (uvm_map_lookup_entry(map, start, &temp_entry)) {
3343 		entry = temp_entry;
3344 		UVM_MAP_CLIP_START(map, entry, start, NULL);
3345 	} else {
3346 		entry = temp_entry->next;
3347 	}
3348 
3349 	/*
3350 	 * XXXJRT: disallow holes?
3351 	 */
3352 
3353 	while ((entry != &map->header) && (entry->start < end)) {
3354 		UVM_MAP_CLIP_END(map, entry, end, NULL);
3355 
3356 		switch (new_advice) {
3357 		case MADV_NORMAL:
3358 		case MADV_RANDOM:
3359 		case MADV_SEQUENTIAL:
3360 			/* nothing special here */
3361 			break;
3362 
3363 		default:
3364 			vm_map_unlock(map);
3365 			UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0);
3366 			return EINVAL;
3367 		}
3368 		entry->advice = new_advice;
3369 		entry = entry->next;
3370 	}
3371 
3372 	vm_map_unlock(map);
3373 	UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
3374 	return 0;
3375 }
3376 
3377 /*
3378  * uvm_map_willneed: apply MADV_WILLNEED
3379  */
3380 
3381 int
3382 uvm_map_willneed(struct vm_map *map, vaddr_t start, vaddr_t end)
3383 {
3384 	struct vm_map_entry *entry;
3385 	UVMHIST_FUNC("uvm_map_willneed"); UVMHIST_CALLED(maphist);
3386 	UVMHIST_LOG(maphist,"(map=0x%lx,start=0x%lx,end=0x%lx)",
3387 	    map, start, end, 0);
3388 
3389 	vm_map_lock_read(map);
3390 	VM_MAP_RANGE_CHECK(map, start, end);
3391 	if (!uvm_map_lookup_entry(map, start, &entry)) {
3392 		entry = entry->next;
3393 	}
3394 	while (entry->start < end) {
3395 		struct vm_amap * const amap = entry->aref.ar_amap;
3396 		struct uvm_object * const uobj = entry->object.uvm_obj;
3397 
3398 		KASSERT(entry != &map->header);
3399 		KASSERT(start < entry->end);
3400 		/*
3401 		 * XXX IMPLEMENT ME.
3402 		 * Should invent a "weak" mode for uvm_fault()
3403 		 * which would only do the PGO_LOCKED pgo_get().
3404 		 *
3405 		 * for now, we handle only the easy but common case.
3406 		 */
3407 		if (UVM_ET_ISOBJ(entry) && amap == NULL && uobj != NULL) {
3408 			off_t offset;
3409 			off_t size;
3410 
3411 			offset = entry->offset;
3412 			if (start < entry->start) {
3413 				offset += entry->start - start;
3414 			}
3415 			size = entry->offset + (entry->end - entry->start);
3416 			if (entry->end < end) {
3417 				size -= end - entry->end;
3418 			}
3419 			uvm_readahead(uobj, offset, size);
3420 		}
3421 		entry = entry->next;
3422 	}
3423 	vm_map_unlock_read(map);
3424 	UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0);
3425 	return 0;
3426 }
3427 
3428 /*
3429  * uvm_map_pageable: sets the pageability of a range in a map.
3430  *
3431  * => wires map entries.  should not be used for transient page locking.
3432  *	for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()).
3433  * => regions specified as not pageable require lock-down (wired) memory
3434  *	and page tables.
3435  * => map must never be read-locked
3436  * => if islocked is true, map is already write-locked
3437  * => we always unlock the map, since we must downgrade to a read-lock
3438  *	to call uvm_fault_wire()
3439  * => XXXCDC: check this and try and clean it up.
3440  */
3441 
3442 int
3443 uvm_map_pageable(struct vm_map *map, vaddr_t start, vaddr_t end,
3444     bool new_pageable, int lockflags)
3445 {
3446 	struct vm_map_entry *entry, *start_entry, *failed_entry;
3447 	int rv;
3448 #ifdef DIAGNOSTIC
3449 	u_int timestamp_save;
3450 #endif
3451 	UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist);
3452 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)",
3453 		    map, start, end, new_pageable);
3454 	KASSERT(map->flags & VM_MAP_PAGEABLE);
3455 
3456 	if ((lockflags & UVM_LK_ENTER) == 0)
3457 		vm_map_lock(map);
3458 	VM_MAP_RANGE_CHECK(map, start, end);
3459 
3460 	/*
3461 	 * only one pageability change may take place at one time, since
3462 	 * uvm_fault_wire assumes it will be called only once for each
3463 	 * wiring/unwiring.  therefore, we have to make sure we're actually
3464 	 * changing the pageability for the entire region.  we do so before
3465 	 * making any changes.
3466 	 */
3467 
3468 	if (uvm_map_lookup_entry(map, start, &start_entry) == false) {
3469 		if ((lockflags & UVM_LK_EXIT) == 0)
3470 			vm_map_unlock(map);
3471 
3472 		UVMHIST_LOG(maphist,"<- done (fault)",0,0,0,0);
3473 		return EFAULT;
3474 	}
3475 	entry = start_entry;
3476 
3477 	/*
3478 	 * handle wiring and unwiring separately.
3479 	 */
3480 
3481 	if (new_pageable) {		/* unwire */
3482 		UVM_MAP_CLIP_START(map, entry, start, NULL);
3483 
3484 		/*
3485 		 * unwiring.  first ensure that the range to be unwired is
3486 		 * really wired down and that there are no holes.
3487 		 */
3488 
3489 		while ((entry != &map->header) && (entry->start < end)) {
3490 			if (entry->wired_count == 0 ||
3491 			    (entry->end < end &&
3492 			     (entry->next == &map->header ||
3493 			      entry->next->start > entry->end))) {
3494 				if ((lockflags & UVM_LK_EXIT) == 0)
3495 					vm_map_unlock(map);
3496 				UVMHIST_LOG(maphist, "<- done (INVAL)",0,0,0,0);
3497 				return EINVAL;
3498 			}
3499 			entry = entry->next;
3500 		}
3501 
3502 		/*
3503 		 * POSIX 1003.1b - a single munlock call unlocks a region,
3504 		 * regardless of the number of mlock calls made on that
3505 		 * region.
3506 		 */
3507 
3508 		entry = start_entry;
3509 		while ((entry != &map->header) && (entry->start < end)) {
3510 			UVM_MAP_CLIP_END(map, entry, end, NULL);
3511 			if (VM_MAPENT_ISWIRED(entry))
3512 				uvm_map_entry_unwire(map, entry);
3513 			entry = entry->next;
3514 		}
3515 		if ((lockflags & UVM_LK_EXIT) == 0)
3516 			vm_map_unlock(map);
3517 		UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
3518 		return 0;
3519 	}
3520 
3521 	/*
3522 	 * wire case: in two passes [XXXCDC: ugly block of code here]
3523 	 *
3524 	 * 1: holding the write lock, we create any anonymous maps that need
3525 	 *    to be created.  then we clip each map entry to the region to
3526 	 *    be wired and increment its wiring count.
3527 	 *
3528 	 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault
3529 	 *    in the pages for any newly wired area (wired_count == 1).
3530 	 *
3531 	 *    downgrading to a read lock for uvm_fault_wire avoids a possible
3532 	 *    deadlock with another thread that may have faulted on one of
3533 	 *    the pages to be wired (it would mark the page busy, blocking
3534 	 *    us, then in turn block on the map lock that we hold).  because
3535 	 *    of problems in the recursive lock package, we cannot upgrade
3536 	 *    to a write lock in vm_map_lookup.  thus, any actions that
3537 	 *    require the write lock must be done beforehand.  because we
3538 	 *    keep the read lock on the map, the copy-on-write status of the
3539 	 *    entries we modify here cannot change.
3540 	 */
3541 
3542 	while ((entry != &map->header) && (entry->start < end)) {
3543 		if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3544 
3545 			/*
3546 			 * perform actions of vm_map_lookup that need the
3547 			 * write lock on the map: create an anonymous map
3548 			 * for a copy-on-write region, or an anonymous map
3549 			 * for a zero-fill region.  (XXXCDC: submap case
3550 			 * ok?)
3551 			 */
3552 
3553 			if (!UVM_ET_ISSUBMAP(entry)) {  /* not submap */
3554 				if (UVM_ET_ISNEEDSCOPY(entry) &&
3555 				    ((entry->max_protection & VM_PROT_WRITE) ||
3556 				     (entry->object.uvm_obj == NULL))) {
3557 					amap_copy(map, entry, 0, start, end);
3558 					/* XXXCDC: wait OK? */
3559 				}
3560 			}
3561 		}
3562 		UVM_MAP_CLIP_START(map, entry, start, NULL);
3563 		UVM_MAP_CLIP_END(map, entry, end, NULL);
3564 		entry->wired_count++;
3565 
3566 		/*
3567 		 * Check for holes
3568 		 */
3569 
3570 		if (entry->protection == VM_PROT_NONE ||
3571 		    (entry->end < end &&
3572 		     (entry->next == &map->header ||
3573 		      entry->next->start > entry->end))) {
3574 
3575 			/*
3576 			 * found one.  amap creation actions do not need to
3577 			 * be undone, but the wired counts need to be restored.
3578 			 */
3579 
3580 			while (entry != &map->header && entry->end > start) {
3581 				entry->wired_count--;
3582 				entry = entry->prev;
3583 			}
3584 			if ((lockflags & UVM_LK_EXIT) == 0)
3585 				vm_map_unlock(map);
3586 			UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0);
3587 			return EINVAL;
3588 		}
3589 		entry = entry->next;
3590 	}
3591 
3592 	/*
3593 	 * Pass 2.
3594 	 */
3595 
3596 #ifdef DIAGNOSTIC
3597 	timestamp_save = map->timestamp;
3598 #endif
3599 	vm_map_busy(map);
3600 	vm_map_unlock(map);
3601 
3602 	rv = 0;
3603 	entry = start_entry;
3604 	while (entry != &map->header && entry->start < end) {
3605 		if (entry->wired_count == 1) {
3606 			rv = uvm_fault_wire(map, entry->start, entry->end,
3607 			    entry->max_protection, 1);
3608 			if (rv) {
3609 
3610 				/*
3611 				 * wiring failed.  break out of the loop.
3612 				 * we'll clean up the map below, once we
3613 				 * have a write lock again.
3614 				 */
3615 
3616 				break;
3617 			}
3618 		}
3619 		entry = entry->next;
3620 	}
3621 
3622 	if (rv) {	/* failed? */
3623 
3624 		/*
3625 		 * Get back to an exclusive (write) lock.
3626 		 */
3627 
3628 		vm_map_lock(map);
3629 		vm_map_unbusy(map);
3630 
3631 #ifdef DIAGNOSTIC
3632 		if (timestamp_save + 1 != map->timestamp)
3633 			panic("uvm_map_pageable: stale map");
3634 #endif
3635 
3636 		/*
3637 		 * first drop the wiring count on all the entries
3638 		 * which haven't actually been wired yet.
3639 		 */
3640 
3641 		failed_entry = entry;
3642 		while (entry != &map->header && entry->start < end) {
3643 			entry->wired_count--;
3644 			entry = entry->next;
3645 		}
3646 
3647 		/*
3648 		 * now, unwire all the entries that were successfully
3649 		 * wired above.
3650 		 */
3651 
3652 		entry = start_entry;
3653 		while (entry != failed_entry) {
3654 			entry->wired_count--;
3655 			if (VM_MAPENT_ISWIRED(entry) == 0)
3656 				uvm_map_entry_unwire(map, entry);
3657 			entry = entry->next;
3658 		}
3659 		if ((lockflags & UVM_LK_EXIT) == 0)
3660 			vm_map_unlock(map);
3661 		UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0);
3662 		return (rv);
3663 	}
3664 
3665 	if ((lockflags & UVM_LK_EXIT) == 0) {
3666 		vm_map_unbusy(map);
3667 	} else {
3668 
3669 		/*
3670 		 * Get back to an exclusive (write) lock.
3671 		 */
3672 
3673 		vm_map_lock(map);
3674 		vm_map_unbusy(map);
3675 	}
3676 
3677 	UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
3678 	return 0;
3679 }
3680 
3681 /*
3682  * uvm_map_pageable_all: special case of uvm_map_pageable - affects
3683  * all mapped regions.
3684  *
3685  * => map must not be locked.
3686  * => if no flags are specified, all regions are unwired.
3687  * => XXXJRT: has some of the same problems as uvm_map_pageable() above.
3688  */
3689 
3690 int
3691 uvm_map_pageable_all(struct vm_map *map, int flags, vsize_t limit)
3692 {
3693 	struct vm_map_entry *entry, *failed_entry;
3694 	vsize_t size;
3695 	int rv;
3696 #ifdef DIAGNOSTIC
3697 	u_int timestamp_save;
3698 #endif
3699 	UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist);
3700 	UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0);
3701 
3702 	KASSERT(map->flags & VM_MAP_PAGEABLE);
3703 
3704 	vm_map_lock(map);
3705 
3706 	/*
3707 	 * handle wiring and unwiring separately.
3708 	 */
3709 
3710 	if (flags == 0) {			/* unwire */
3711 
3712 		/*
3713 		 * POSIX 1003.1b -- munlockall unlocks all regions,
3714 		 * regardless of how many times mlockall has been called.
3715 		 */
3716 
3717 		for (entry = map->header.next; entry != &map->header;
3718 		     entry = entry->next) {
3719 			if (VM_MAPENT_ISWIRED(entry))
3720 				uvm_map_entry_unwire(map, entry);
3721 		}
3722 		map->flags &= ~VM_MAP_WIREFUTURE;
3723 		vm_map_unlock(map);
3724 		UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0);
3725 		return 0;
3726 	}
3727 
3728 	if (flags & MCL_FUTURE) {
3729 
3730 		/*
3731 		 * must wire all future mappings; remember this.
3732 		 */
3733 
3734 		map->flags |= VM_MAP_WIREFUTURE;
3735 	}
3736 
3737 	if ((flags & MCL_CURRENT) == 0) {
3738 
3739 		/*
3740 		 * no more work to do!
3741 		 */
3742 
3743 		UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0);
3744 		vm_map_unlock(map);
3745 		return 0;
3746 	}
3747 
3748 	/*
3749 	 * wire case: in three passes [XXXCDC: ugly block of code here]
3750 	 *
3751 	 * 1: holding the write lock, count all pages mapped by non-wired
3752 	 *    entries.  if this would cause us to go over our limit, we fail.
3753 	 *
3754 	 * 2: still holding the write lock, we create any anonymous maps that
3755 	 *    need to be created.  then we increment its wiring count.
3756 	 *
3757 	 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault
3758 	 *    in the pages for any newly wired area (wired_count == 1).
3759 	 *
3760 	 *    downgrading to a read lock for uvm_fault_wire avoids a possible
3761 	 *    deadlock with another thread that may have faulted on one of
3762 	 *    the pages to be wired (it would mark the page busy, blocking
3763 	 *    us, then in turn block on the map lock that we hold).  because
3764 	 *    of problems in the recursive lock package, we cannot upgrade
3765 	 *    to a write lock in vm_map_lookup.  thus, any actions that
3766 	 *    require the write lock must be done beforehand.  because we
3767 	 *    keep the read lock on the map, the copy-on-write status of the
3768 	 *    entries we modify here cannot change.
3769 	 */
3770 
3771 	for (size = 0, entry = map->header.next; entry != &map->header;
3772 	     entry = entry->next) {
3773 		if (entry->protection != VM_PROT_NONE &&
3774 		    VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3775 			size += entry->end - entry->start;
3776 		}
3777 	}
3778 
3779 	if (atop(size) + uvmexp.wired > uvmexp.wiredmax) {
3780 		vm_map_unlock(map);
3781 		return ENOMEM;
3782 	}
3783 
3784 	if (limit != 0 &&
3785 	    (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) {
3786 		vm_map_unlock(map);
3787 		return ENOMEM;
3788 	}
3789 
3790 	/*
3791 	 * Pass 2.
3792 	 */
3793 
3794 	for (entry = map->header.next; entry != &map->header;
3795 	     entry = entry->next) {
3796 		if (entry->protection == VM_PROT_NONE)
3797 			continue;
3798 		if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */
3799 
3800 			/*
3801 			 * perform actions of vm_map_lookup that need the
3802 			 * write lock on the map: create an anonymous map
3803 			 * for a copy-on-write region, or an anonymous map
3804 			 * for a zero-fill region.  (XXXCDC: submap case
3805 			 * ok?)
3806 			 */
3807 
3808 			if (!UVM_ET_ISSUBMAP(entry)) {	/* not submap */
3809 				if (UVM_ET_ISNEEDSCOPY(entry) &&
3810 				    ((entry->max_protection & VM_PROT_WRITE) ||
3811 				     (entry->object.uvm_obj == NULL))) {
3812 					amap_copy(map, entry, 0, entry->start,
3813 					    entry->end);
3814 					/* XXXCDC: wait OK? */
3815 				}
3816 			}
3817 		}
3818 		entry->wired_count++;
3819 	}
3820 
3821 	/*
3822 	 * Pass 3.
3823 	 */
3824 
3825 #ifdef DIAGNOSTIC
3826 	timestamp_save = map->timestamp;
3827 #endif
3828 	vm_map_busy(map);
3829 	vm_map_unlock(map);
3830 
3831 	rv = 0;
3832 	for (entry = map->header.next; entry != &map->header;
3833 	     entry = entry->next) {
3834 		if (entry->wired_count == 1) {
3835 			rv = uvm_fault_wire(map, entry->start, entry->end,
3836 			    entry->max_protection, 1);
3837 			if (rv) {
3838 
3839 				/*
3840 				 * wiring failed.  break out of the loop.
3841 				 * we'll clean up the map below, once we
3842 				 * have a write lock again.
3843 				 */
3844 
3845 				break;
3846 			}
3847 		}
3848 	}
3849 
3850 	if (rv) {
3851 
3852 		/*
3853 		 * Get back an exclusive (write) lock.
3854 		 */
3855 
3856 		vm_map_lock(map);
3857 		vm_map_unbusy(map);
3858 
3859 #ifdef DIAGNOSTIC
3860 		if (timestamp_save + 1 != map->timestamp)
3861 			panic("uvm_map_pageable_all: stale map");
3862 #endif
3863 
3864 		/*
3865 		 * first drop the wiring count on all the entries
3866 		 * which haven't actually been wired yet.
3867 		 *
3868 		 * Skip VM_PROT_NONE entries like we did above.
3869 		 */
3870 
3871 		failed_entry = entry;
3872 		for (/* nothing */; entry != &map->header;
3873 		     entry = entry->next) {
3874 			if (entry->protection == VM_PROT_NONE)
3875 				continue;
3876 			entry->wired_count--;
3877 		}
3878 
3879 		/*
3880 		 * now, unwire all the entries that were successfully
3881 		 * wired above.
3882 		 *
3883 		 * Skip VM_PROT_NONE entries like we did above.
3884 		 */
3885 
3886 		for (entry = map->header.next; entry != failed_entry;
3887 		     entry = entry->next) {
3888 			if (entry->protection == VM_PROT_NONE)
3889 				continue;
3890 			entry->wired_count--;
3891 			if (VM_MAPENT_ISWIRED(entry))
3892 				uvm_map_entry_unwire(map, entry);
3893 		}
3894 		vm_map_unlock(map);
3895 		UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0);
3896 		return (rv);
3897 	}
3898 
3899 	vm_map_unbusy(map);
3900 
3901 	UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0);
3902 	return 0;
3903 }
3904 
3905 /*
3906  * uvm_map_clean: clean out a map range
3907  *
3908  * => valid flags:
3909  *   if (flags & PGO_CLEANIT): dirty pages are cleaned first
3910  *   if (flags & PGO_SYNCIO): dirty pages are written synchronously
3911  *   if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean
3912  *   if (flags & PGO_FREE): any cached pages are freed after clean
3913  * => returns an error if any part of the specified range isn't mapped
3914  * => never a need to flush amap layer since the anonymous memory has
3915  *	no permanent home, but may deactivate pages there
3916  * => called from sys_msync() and sys_madvise()
3917  * => caller must not write-lock map (read OK).
3918  * => we may sleep while cleaning if SYNCIO [with map read-locked]
3919  */
3920 
3921 int
3922 uvm_map_clean(struct vm_map *map, vaddr_t start, vaddr_t end, int flags)
3923 {
3924 	struct vm_map_entry *current, *entry;
3925 	struct uvm_object *uobj;
3926 	struct vm_amap *amap;
3927 	struct vm_anon *anon;
3928 	struct vm_page *pg;
3929 	vaddr_t offset;
3930 	vsize_t size;
3931 	voff_t uoff;
3932 	int error, refs;
3933 	UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist);
3934 
3935 	UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)",
3936 		    map, start, end, flags);
3937 	KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) !=
3938 		(PGO_FREE|PGO_DEACTIVATE));
3939 
3940 	vm_map_lock_read(map);
3941 	VM_MAP_RANGE_CHECK(map, start, end);
3942 	if (uvm_map_lookup_entry(map, start, &entry) == false) {
3943 		vm_map_unlock_read(map);
3944 		return EFAULT;
3945 	}
3946 
3947 	/*
3948 	 * Make a first pass to check for holes and wiring problems.
3949 	 */
3950 
3951 	for (current = entry; current->start < end; current = current->next) {
3952 		if (UVM_ET_ISSUBMAP(current)) {
3953 			vm_map_unlock_read(map);
3954 			return EINVAL;
3955 		}
3956 		if ((flags & PGO_FREE) != 0 && VM_MAPENT_ISWIRED(entry)) {
3957 			vm_map_unlock_read(map);
3958 			return EBUSY;
3959 		}
3960 		if (end <= current->end) {
3961 			break;
3962 		}
3963 		if (current->end != current->next->start) {
3964 			vm_map_unlock_read(map);
3965 			return EFAULT;
3966 		}
3967 	}
3968 
3969 	error = 0;
3970 	for (current = entry; start < end; current = current->next) {
3971 		amap = current->aref.ar_amap;	/* upper layer */
3972 		uobj = current->object.uvm_obj;	/* lower layer */
3973 		KASSERT(start >= current->start);
3974 
3975 		/*
3976 		 * No amap cleaning necessary if:
3977 		 *
3978 		 *	(1) There's no amap.
3979 		 *
3980 		 *	(2) We're not deactivating or freeing pages.
3981 		 */
3982 
3983 		if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0)
3984 			goto flush_object;
3985 
3986 		amap_lock(amap);
3987 		offset = start - current->start;
3988 		size = MIN(end, current->end) - start;
3989 		for ( ; size != 0; size -= PAGE_SIZE, offset += PAGE_SIZE) {
3990 			anon = amap_lookup(&current->aref, offset);
3991 			if (anon == NULL)
3992 				continue;
3993 
3994 			mutex_enter(&anon->an_lock);
3995 			pg = anon->an_page;
3996 			if (pg == NULL) {
3997 				mutex_exit(&anon->an_lock);
3998 				continue;
3999 			}
4000 
4001 			switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
4002 
4003 			/*
4004 			 * In these first 3 cases, we just deactivate the page.
4005 			 */
4006 
4007 			case PGO_CLEANIT|PGO_FREE:
4008 			case PGO_CLEANIT|PGO_DEACTIVATE:
4009 			case PGO_DEACTIVATE:
4010  deactivate_it:
4011 				/*
4012 				 * skip the page if it's loaned or wired,
4013 				 * since it shouldn't be on a paging queue
4014 				 * at all in these cases.
4015 				 */
4016 
4017 				mutex_enter(&uvm_pageqlock);
4018 				if (pg->loan_count != 0 ||
4019 				    pg->wire_count != 0) {
4020 					mutex_exit(&uvm_pageqlock);
4021 					mutex_exit(&anon->an_lock);
4022 					continue;
4023 				}
4024 				KASSERT(pg->uanon == anon);
4025 				uvm_pagedeactivate(pg);
4026 				mutex_exit(&uvm_pageqlock);
4027 				mutex_exit(&anon->an_lock);
4028 				continue;
4029 
4030 			case PGO_FREE:
4031 
4032 				/*
4033 				 * If there are multiple references to
4034 				 * the amap, just deactivate the page.
4035 				 */
4036 
4037 				if (amap_refs(amap) > 1)
4038 					goto deactivate_it;
4039 
4040 				/* skip the page if it's wired */
4041 				if (pg->wire_count != 0) {
4042 					mutex_exit(&anon->an_lock);
4043 					continue;
4044 				}
4045 				amap_unadd(&current->aref, offset);
4046 				refs = --anon->an_ref;
4047 				mutex_exit(&anon->an_lock);
4048 				if (refs == 0)
4049 					uvm_anfree(anon);
4050 				continue;
4051 			}
4052 		}
4053 		amap_unlock(amap);
4054 
4055  flush_object:
4056 		/*
4057 		 * flush pages if we've got a valid backing object.
4058 		 * note that we must always clean object pages before
4059 		 * freeing them since otherwise we could reveal stale
4060 		 * data from files.
4061 		 */
4062 
4063 		uoff = current->offset + (start - current->start);
4064 		size = MIN(end, current->end) - start;
4065 		if (uobj != NULL) {
4066 			mutex_enter(&uobj->vmobjlock);
4067 			if (uobj->pgops->pgo_put != NULL)
4068 				error = (uobj->pgops->pgo_put)(uobj, uoff,
4069 				    uoff + size, flags | PGO_CLEANIT);
4070 			else
4071 				error = 0;
4072 		}
4073 		start += size;
4074 	}
4075 	vm_map_unlock_read(map);
4076 	return (error);
4077 }
4078 
4079 
4080 /*
4081  * uvm_map_checkprot: check protection in map
4082  *
4083  * => must allow specified protection in a fully allocated region.
4084  * => map must be read or write locked by caller.
4085  */
4086 
4087 bool
4088 uvm_map_checkprot(struct vm_map *map, vaddr_t start, vaddr_t end,
4089     vm_prot_t protection)
4090 {
4091 	struct vm_map_entry *entry;
4092 	struct vm_map_entry *tmp_entry;
4093 
4094 	if (!uvm_map_lookup_entry(map, start, &tmp_entry)) {
4095 		return (false);
4096 	}
4097 	entry = tmp_entry;
4098 	while (start < end) {
4099 		if (entry == &map->header) {
4100 			return (false);
4101 		}
4102 
4103 		/*
4104 		 * no holes allowed
4105 		 */
4106 
4107 		if (start < entry->start) {
4108 			return (false);
4109 		}
4110 
4111 		/*
4112 		 * check protection associated with entry
4113 		 */
4114 
4115 		if ((entry->protection & protection) != protection) {
4116 			return (false);
4117 		}
4118 		start = entry->end;
4119 		entry = entry->next;
4120 	}
4121 	return (true);
4122 }
4123 
4124 /*
4125  * uvmspace_alloc: allocate a vmspace structure.
4126  *
4127  * - structure includes vm_map and pmap
4128  * - XXX: no locking on this structure
4129  * - refcnt set to 1, rest must be init'd by caller
4130  */
4131 struct vmspace *
4132 uvmspace_alloc(vaddr_t vmin, vaddr_t vmax)
4133 {
4134 	struct vmspace *vm;
4135 	UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist);
4136 
4137 	vm = pool_cache_get(&uvm_vmspace_cache, PR_WAITOK);
4138 	uvmspace_init(vm, NULL, vmin, vmax);
4139 	UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0);
4140 	return (vm);
4141 }
4142 
4143 /*
4144  * uvmspace_init: initialize a vmspace structure.
4145  *
4146  * - XXX: no locking on this structure
4147  * - refcnt set to 1, rest must be init'd by caller
4148  */
4149 void
4150 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t vmin, vaddr_t vmax)
4151 {
4152 	UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist);
4153 
4154 	memset(vm, 0, sizeof(*vm));
4155 	uvm_map_setup(&vm->vm_map, vmin, vmax, VM_MAP_PAGEABLE
4156 #ifdef __USING_TOPDOWN_VM
4157 	    | VM_MAP_TOPDOWN
4158 #endif
4159 	    );
4160 	if (pmap)
4161 		pmap_reference(pmap);
4162 	else
4163 		pmap = pmap_create();
4164 	vm->vm_map.pmap = pmap;
4165 	vm->vm_refcnt = 1;
4166 	UVMHIST_LOG(maphist,"<- done",0,0,0,0);
4167 }
4168 
4169 /*
4170  * uvmspace_share: share a vmspace between two processes
4171  *
4172  * - used for vfork, threads(?)
4173  */
4174 
4175 void
4176 uvmspace_share(struct proc *p1, struct proc *p2)
4177 {
4178 
4179 	uvmspace_addref(p1->p_vmspace);
4180 	p2->p_vmspace = p1->p_vmspace;
4181 }
4182 
4183 #if 0
4184 
4185 /*
4186  * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace
4187  *
4188  * - XXX: no locking on vmspace
4189  */
4190 
4191 void
4192 uvmspace_unshare(struct lwp *l)
4193 {
4194 	struct proc *p = l->l_proc;
4195 	struct vmspace *nvm, *ovm = p->p_vmspace;
4196 
4197 	if (ovm->vm_refcnt == 1)
4198 		/* nothing to do: vmspace isn't shared in the first place */
4199 		return;
4200 
4201 	/* make a new vmspace, still holding old one */
4202 	nvm = uvmspace_fork(ovm);
4203 
4204 	kpreempt_disable();
4205 	pmap_deactivate(l);		/* unbind old vmspace */
4206 	p->p_vmspace = nvm;
4207 	pmap_activate(l);		/* switch to new vmspace */
4208 	kpreempt_enable();
4209 
4210 	uvmspace_free(ovm);		/* drop reference to old vmspace */
4211 }
4212 
4213 #endif
4214 
4215 /*
4216  * uvmspace_exec: the process wants to exec a new program
4217  */
4218 
4219 void
4220 uvmspace_exec(struct lwp *l, vaddr_t start, vaddr_t end)
4221 {
4222 	struct proc *p = l->l_proc;
4223 	struct vmspace *nvm, *ovm = p->p_vmspace;
4224 	struct vm_map *map = &ovm->vm_map;
4225 
4226 #ifdef __sparc__
4227 	/* XXX cgd 960926: the sparc #ifdef should be a MD hook */
4228 	kill_user_windows(l);   /* before stack addresses go away */
4229 #endif
4230 #ifdef __HAVE_CPU_VMSPACE_EXEC
4231 	cpu_vmspace_exec(l, start, end);
4232 #endif
4233 
4234 	/*
4235 	 * see if more than one process is using this vmspace...
4236 	 */
4237 
4238 	if (ovm->vm_refcnt == 1) {
4239 
4240 		/*
4241 		 * if p is the only process using its vmspace then we can safely
4242 		 * recycle that vmspace for the program that is being exec'd.
4243 		 */
4244 
4245 #ifdef SYSVSHM
4246 		/*
4247 		 * SYSV SHM semantics require us to kill all segments on an exec
4248 		 */
4249 
4250 		if (ovm->vm_shm)
4251 			shmexit(ovm);
4252 #endif
4253 
4254 		/*
4255 		 * POSIX 1003.1b -- "lock future mappings" is revoked
4256 		 * when a process execs another program image.
4257 		 */
4258 
4259 		map->flags &= ~VM_MAP_WIREFUTURE;
4260 
4261 		/*
4262 		 * now unmap the old program
4263 		 */
4264 
4265 		pmap_remove_all(map->pmap);
4266 		uvm_unmap(map, vm_map_min(map), vm_map_max(map));
4267 		KASSERT(map->header.prev == &map->header);
4268 		KASSERT(map->nentries == 0);
4269 
4270 		/*
4271 		 * resize the map
4272 		 */
4273 
4274 		vm_map_setmin(map, start);
4275 		vm_map_setmax(map, end);
4276 	} else {
4277 
4278 		/*
4279 		 * p's vmspace is being shared, so we can't reuse it for p since
4280 		 * it is still being used for others.   allocate a new vmspace
4281 		 * for p
4282 		 */
4283 
4284 		nvm = uvmspace_alloc(start, end);
4285 
4286 		/*
4287 		 * install new vmspace and drop our ref to the old one.
4288 		 */
4289 
4290 		kpreempt_disable();
4291 		pmap_deactivate(l);
4292 		p->p_vmspace = nvm;
4293 		pmap_activate(l);
4294 		kpreempt_enable();
4295 
4296 		uvmspace_free(ovm);
4297 	}
4298 }
4299 
4300 /*
4301  * uvmspace_addref: add a referece to a vmspace.
4302  */
4303 
4304 void
4305 uvmspace_addref(struct vmspace *vm)
4306 {
4307 	struct vm_map *map = &vm->vm_map;
4308 
4309 	KASSERT((map->flags & VM_MAP_DYING) == 0);
4310 
4311 	mutex_enter(&map->misc_lock);
4312 	KASSERT(vm->vm_refcnt > 0);
4313 	vm->vm_refcnt++;
4314 	mutex_exit(&map->misc_lock);
4315 }
4316 
4317 /*
4318  * uvmspace_free: free a vmspace data structure
4319  */
4320 
4321 void
4322 uvmspace_free(struct vmspace *vm)
4323 {
4324 	struct vm_map_entry *dead_entries;
4325 	struct vm_map *map = &vm->vm_map;
4326 	int n;
4327 
4328 	UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist);
4329 
4330 	UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0);
4331 	mutex_enter(&map->misc_lock);
4332 	n = --vm->vm_refcnt;
4333 	mutex_exit(&map->misc_lock);
4334 	if (n > 0)
4335 		return;
4336 
4337 	/*
4338 	 * at this point, there should be no other references to the map.
4339 	 * delete all of the mappings, then destroy the pmap.
4340 	 */
4341 
4342 	map->flags |= VM_MAP_DYING;
4343 	pmap_remove_all(map->pmap);
4344 #ifdef SYSVSHM
4345 	/* Get rid of any SYSV shared memory segments. */
4346 	if (vm->vm_shm != NULL)
4347 		shmexit(vm);
4348 #endif
4349 	if (map->nentries) {
4350 		uvm_unmap_remove(map, vm_map_min(map), vm_map_max(map),
4351 		    &dead_entries, NULL, 0);
4352 		if (dead_entries != NULL)
4353 			uvm_unmap_detach(dead_entries, 0);
4354 	}
4355 	KASSERT(map->nentries == 0);
4356 	KASSERT(map->size == 0);
4357 	mutex_destroy(&map->misc_lock);
4358 	mutex_destroy(&map->mutex);
4359 	rw_destroy(&map->lock);
4360 	cv_destroy(&map->cv);
4361 	pmap_destroy(map->pmap);
4362 	pool_cache_put(&uvm_vmspace_cache, vm);
4363 }
4364 
4365 /*
4366  *   F O R K   -   m a i n   e n t r y   p o i n t
4367  */
4368 /*
4369  * uvmspace_fork: fork a process' main map
4370  *
4371  * => create a new vmspace for child process from parent.
4372  * => parent's map must not be locked.
4373  */
4374 
4375 struct vmspace *
4376 uvmspace_fork(struct vmspace *vm1)
4377 {
4378 	struct vmspace *vm2;
4379 	struct vm_map *old_map = &vm1->vm_map;
4380 	struct vm_map *new_map;
4381 	struct vm_map_entry *old_entry;
4382 	struct vm_map_entry *new_entry;
4383 	UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist);
4384 
4385 	vm_map_lock(old_map);
4386 
4387 	vm2 = uvmspace_alloc(vm_map_min(old_map), vm_map_max(old_map));
4388 	memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy,
4389 	    (char *) (vm1 + 1) - (char *) &vm1->vm_startcopy);
4390 	new_map = &vm2->vm_map;		  /* XXX */
4391 
4392 	old_entry = old_map->header.next;
4393 	new_map->size = old_map->size;
4394 
4395 	/*
4396 	 * go entry-by-entry
4397 	 */
4398 
4399 	while (old_entry != &old_map->header) {
4400 
4401 		/*
4402 		 * first, some sanity checks on the old entry
4403 		 */
4404 
4405 		KASSERT(!UVM_ET_ISSUBMAP(old_entry));
4406 		KASSERT(UVM_ET_ISCOPYONWRITE(old_entry) ||
4407 			!UVM_ET_ISNEEDSCOPY(old_entry));
4408 
4409 		switch (old_entry->inheritance) {
4410 		case MAP_INHERIT_NONE:
4411 
4412 			/*
4413 			 * drop the mapping, modify size
4414 			 */
4415 			new_map->size -= old_entry->end - old_entry->start;
4416 			break;
4417 
4418 		case MAP_INHERIT_SHARE:
4419 
4420 			/*
4421 			 * share the mapping: this means we want the old and
4422 			 * new entries to share amaps and backing objects.
4423 			 */
4424 			/*
4425 			 * if the old_entry needs a new amap (due to prev fork)
4426 			 * then we need to allocate it now so that we have
4427 			 * something we own to share with the new_entry.   [in
4428 			 * other words, we need to clear needs_copy]
4429 			 */
4430 
4431 			if (UVM_ET_ISNEEDSCOPY(old_entry)) {
4432 				/* get our own amap, clears needs_copy */
4433 				amap_copy(old_map, old_entry, AMAP_COPY_NOCHUNK,
4434 				    0, 0);
4435 				/* XXXCDC: WAITOK??? */
4436 			}
4437 
4438 			new_entry = uvm_mapent_alloc(new_map, 0);
4439 			/* old_entry -> new_entry */
4440 			uvm_mapent_copy(old_entry, new_entry);
4441 
4442 			/* new pmap has nothing wired in it */
4443 			new_entry->wired_count = 0;
4444 
4445 			/*
4446 			 * gain reference to object backing the map (can't
4447 			 * be a submap, already checked this case).
4448 			 */
4449 
4450 			if (new_entry->aref.ar_amap)
4451 				uvm_map_reference_amap(new_entry, AMAP_SHARED);
4452 
4453 			if (new_entry->object.uvm_obj &&
4454 			    new_entry->object.uvm_obj->pgops->pgo_reference)
4455 				new_entry->object.uvm_obj->
4456 				    pgops->pgo_reference(
4457 				        new_entry->object.uvm_obj);
4458 
4459 			/* insert entry at end of new_map's entry list */
4460 			uvm_map_entry_link(new_map, new_map->header.prev,
4461 			    new_entry);
4462 
4463 			break;
4464 
4465 		case MAP_INHERIT_COPY:
4466 
4467 			/*
4468 			 * copy-on-write the mapping (using mmap's
4469 			 * MAP_PRIVATE semantics)
4470 			 *
4471 			 * allocate new_entry, adjust reference counts.
4472 			 * (note that new references are read-only).
4473 			 */
4474 
4475 			new_entry = uvm_mapent_alloc(new_map, 0);
4476 			/* old_entry -> new_entry */
4477 			uvm_mapent_copy(old_entry, new_entry);
4478 
4479 			if (new_entry->aref.ar_amap)
4480 				uvm_map_reference_amap(new_entry, 0);
4481 
4482 			if (new_entry->object.uvm_obj &&
4483 			    new_entry->object.uvm_obj->pgops->pgo_reference)
4484 				new_entry->object.uvm_obj->pgops->pgo_reference
4485 				    (new_entry->object.uvm_obj);
4486 
4487 			/* new pmap has nothing wired in it */
4488 			new_entry->wired_count = 0;
4489 
4490 			new_entry->etype |=
4491 			    (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY);
4492 			uvm_map_entry_link(new_map, new_map->header.prev,
4493 			    new_entry);
4494 
4495 			/*
4496 			 * the new entry will need an amap.  it will either
4497 			 * need to be copied from the old entry or created
4498 			 * from scratch (if the old entry does not have an
4499 			 * amap).  can we defer this process until later
4500 			 * (by setting "needs_copy") or do we need to copy
4501 			 * the amap now?
4502 			 *
4503 			 * we must copy the amap now if any of the following
4504 			 * conditions hold:
4505 			 * 1. the old entry has an amap and that amap is
4506 			 *    being shared.  this means that the old (parent)
4507 			 *    process is sharing the amap with another
4508 			 *    process.  if we do not clear needs_copy here
4509 			 *    we will end up in a situation where both the
4510 			 *    parent and child process are refering to the
4511 			 *    same amap with "needs_copy" set.  if the
4512 			 *    parent write-faults, the fault routine will
4513 			 *    clear "needs_copy" in the parent by allocating
4514 			 *    a new amap.   this is wrong because the
4515 			 *    parent is supposed to be sharing the old amap
4516 			 *    and the new amap will break that.
4517 			 *
4518 			 * 2. if the old entry has an amap and a non-zero
4519 			 *    wire count then we are going to have to call
4520 			 *    amap_cow_now to avoid page faults in the
4521 			 *    parent process.   since amap_cow_now requires
4522 			 *    "needs_copy" to be clear we might as well
4523 			 *    clear it here as well.
4524 			 *
4525 			 */
4526 
4527 			if (old_entry->aref.ar_amap != NULL) {
4528 				if ((amap_flags(old_entry->aref.ar_amap) &
4529 				     AMAP_SHARED) != 0 ||
4530 				    VM_MAPENT_ISWIRED(old_entry)) {
4531 
4532 					amap_copy(new_map, new_entry,
4533 					    AMAP_COPY_NOCHUNK, 0, 0);
4534 					/* XXXCDC: M_WAITOK ... ok? */
4535 				}
4536 			}
4537 
4538 			/*
4539 			 * if the parent's entry is wired down, then the
4540 			 * parent process does not want page faults on
4541 			 * access to that memory.  this means that we
4542 			 * cannot do copy-on-write because we can't write
4543 			 * protect the old entry.   in this case we
4544 			 * resolve all copy-on-write faults now, using
4545 			 * amap_cow_now.   note that we have already
4546 			 * allocated any needed amap (above).
4547 			 */
4548 
4549 			if (VM_MAPENT_ISWIRED(old_entry)) {
4550 
4551 			  /*
4552 			   * resolve all copy-on-write faults now
4553 			   * (note that there is nothing to do if
4554 			   * the old mapping does not have an amap).
4555 			   */
4556 			  if (old_entry->aref.ar_amap)
4557 			    amap_cow_now(new_map, new_entry);
4558 
4559 			} else {
4560 
4561 			  /*
4562 			   * setup mappings to trigger copy-on-write faults
4563 			   * we must write-protect the parent if it has
4564 			   * an amap and it is not already "needs_copy"...
4565 			   * if it is already "needs_copy" then the parent
4566 			   * has already been write-protected by a previous
4567 			   * fork operation.
4568 			   */
4569 
4570 			  if (old_entry->aref.ar_amap &&
4571 			      !UVM_ET_ISNEEDSCOPY(old_entry)) {
4572 			      if (old_entry->max_protection & VM_PROT_WRITE) {
4573 				pmap_protect(old_map->pmap,
4574 					     old_entry->start,
4575 					     old_entry->end,
4576 					     old_entry->protection &
4577 					     ~VM_PROT_WRITE);
4578 			      }
4579 			      old_entry->etype |= UVM_ET_NEEDSCOPY;
4580 			  }
4581 			}
4582 			break;
4583 		}  /* end of switch statement */
4584 		old_entry = old_entry->next;
4585 	}
4586 
4587 	pmap_update(old_map->pmap);
4588 	vm_map_unlock(old_map);
4589 
4590 #ifdef SYSVSHM
4591 	if (vm1->vm_shm)
4592 		shmfork(vm1, vm2);
4593 #endif
4594 
4595 #ifdef PMAP_FORK
4596 	pmap_fork(vm1->vm_map.pmap, vm2->vm_map.pmap);
4597 #endif
4598 
4599 	UVMHIST_LOG(maphist,"<- done",0,0,0,0);
4600 	return (vm2);
4601 }
4602 
4603 
4604 /*
4605  * in-kernel map entry allocation.
4606  */
4607 
4608 struct uvm_kmapent_hdr {
4609 	LIST_ENTRY(uvm_kmapent_hdr) ukh_listq;
4610 	int ukh_nused;
4611 	struct vm_map_entry *ukh_freelist;
4612 	struct vm_map *ukh_map;
4613 	struct vm_map_entry ukh_entries[0];
4614 };
4615 
4616 #define	UVM_KMAPENT_CHUNK				\
4617 	((PAGE_SIZE - sizeof(struct uvm_kmapent_hdr))	\
4618 	/ sizeof(struct vm_map_entry))
4619 
4620 #define	UVM_KHDR_FIND(entry)	\
4621 	((struct uvm_kmapent_hdr *)(((vaddr_t)entry) & ~PAGE_MASK))
4622 
4623 
4624 #ifdef DIAGNOSTIC
4625 static struct vm_map *
4626 uvm_kmapent_map(struct vm_map_entry *entry)
4627 {
4628 	const struct uvm_kmapent_hdr *ukh;
4629 
4630 	ukh = UVM_KHDR_FIND(entry);
4631 	return ukh->ukh_map;
4632 }
4633 #endif
4634 
4635 static inline struct vm_map_entry *
4636 uvm_kmapent_get(struct uvm_kmapent_hdr *ukh)
4637 {
4638 	struct vm_map_entry *entry;
4639 
4640 	KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK);
4641 	KASSERT(ukh->ukh_nused >= 0);
4642 
4643 	entry = ukh->ukh_freelist;
4644 	if (entry) {
4645 		KASSERT((entry->flags & (UVM_MAP_KERNEL | UVM_MAP_KMAPENT))
4646 		    == UVM_MAP_KERNEL);
4647 		ukh->ukh_freelist = entry->next;
4648 		ukh->ukh_nused++;
4649 		KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK);
4650 	} else {
4651 		KASSERT(ukh->ukh_nused == UVM_KMAPENT_CHUNK);
4652 	}
4653 
4654 	return entry;
4655 }
4656 
4657 static inline void
4658 uvm_kmapent_put(struct uvm_kmapent_hdr *ukh, struct vm_map_entry *entry)
4659 {
4660 
4661 	KASSERT((entry->flags & (UVM_MAP_KERNEL | UVM_MAP_KMAPENT))
4662 	    == UVM_MAP_KERNEL);
4663 	KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK);
4664 	KASSERT(ukh->ukh_nused > 0);
4665 	KASSERT(ukh->ukh_freelist != NULL ||
4666 	    ukh->ukh_nused == UVM_KMAPENT_CHUNK);
4667 	KASSERT(ukh->ukh_freelist == NULL ||
4668 	    ukh->ukh_nused < UVM_KMAPENT_CHUNK);
4669 
4670 	ukh->ukh_nused--;
4671 	entry->next = ukh->ukh_freelist;
4672 	ukh->ukh_freelist = entry;
4673 }
4674 
4675 /*
4676  * uvm_kmapent_alloc: allocate a map entry for in-kernel map
4677  */
4678 
4679 static struct vm_map_entry *
4680 uvm_kmapent_alloc(struct vm_map *map, int flags)
4681 {
4682 	struct vm_page *pg;
4683 	struct uvm_kmapent_hdr *ukh;
4684 	struct vm_map_entry *entry;
4685 #ifndef PMAP_MAP_POOLPAGE
4686 	struct uvm_map_args args;
4687 	uvm_flag_t mapflags = UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
4688 	    UVM_INH_NONE, UVM_ADV_RANDOM, flags | UVM_FLAG_NOMERGE);
4689 	int error;
4690 #endif
4691 	vaddr_t va;
4692 	int i;
4693 
4694 	KDASSERT(UVM_KMAPENT_CHUNK > 2);
4695 	KDASSERT(kernel_map != NULL);
4696 	KASSERT(vm_map_pmap(map) == pmap_kernel());
4697 
4698 	UVMMAP_EVCNT_INCR(uke_alloc);
4699 	entry = NULL;
4700 again:
4701 	/*
4702 	 * try to grab an entry from freelist.
4703 	 */
4704 	mutex_spin_enter(&uvm_kentry_lock);
4705 	ukh = LIST_FIRST(&vm_map_to_kernel(map)->vmk_kentry_free);
4706 	if (ukh) {
4707 		entry = uvm_kmapent_get(ukh);
4708 		if (ukh->ukh_nused == UVM_KMAPENT_CHUNK)
4709 			LIST_REMOVE(ukh, ukh_listq);
4710 	}
4711 	mutex_spin_exit(&uvm_kentry_lock);
4712 
4713 	if (entry)
4714 		return entry;
4715 
4716 	/*
4717 	 * there's no free entry for this vm_map.
4718 	 * now we need to allocate some vm_map_entry.
4719 	 * for simplicity, always allocate one page chunk of them at once.
4720 	 */
4721 
4722 	pg = uvm_pagealloc(NULL, 0, NULL,
4723 	    (flags & UVM_KMF_NOWAIT) != 0 ? UVM_PGA_USERESERVE : 0);
4724 	if (__predict_false(pg == NULL)) {
4725 		if (flags & UVM_FLAG_NOWAIT)
4726 			return NULL;
4727 		uvm_wait("kme_alloc");
4728 		goto again;
4729 	}
4730 
4731 #ifdef PMAP_MAP_POOLPAGE
4732 	va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
4733 	KASSERT(va != 0);
4734 #else
4735 	error = uvm_map_prepare(map, 0, PAGE_SIZE, NULL, UVM_UNKNOWN_OFFSET,
4736 	    0, mapflags, &args);
4737 	if (error) {
4738 		uvm_pagefree(pg);
4739 		return NULL;
4740 	}
4741 
4742 	va = args.uma_start;
4743 
4744 	pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
4745 	    VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
4746 	pmap_update(vm_map_pmap(map));
4747 
4748 #endif
4749 	ukh = (void *)va;
4750 
4751 	/*
4752 	 * use the last entry for ukh itsself.
4753 	 */
4754 
4755 	i = UVM_KMAPENT_CHUNK - 1;
4756 #ifndef PMAP_MAP_POOLPAGE
4757 	entry = &ukh->ukh_entries[i--];
4758 	entry->flags = UVM_MAP_KERNEL | UVM_MAP_KMAPENT;
4759 	error = uvm_map_enter(map, &args, entry);
4760 	KASSERT(error == 0);
4761 #endif
4762 
4763 	ukh->ukh_nused = UVM_KMAPENT_CHUNK;
4764 	ukh->ukh_map = map;
4765 	ukh->ukh_freelist = NULL;
4766 	for (; i >= 1; i--) {
4767 		struct vm_map_entry *xentry = &ukh->ukh_entries[i];
4768 
4769 		xentry->flags = UVM_MAP_KERNEL;
4770 		uvm_kmapent_put(ukh, xentry);
4771 	}
4772 #ifdef PMAP_MAP_POOLPAGE
4773 	KASSERT(ukh->ukh_nused == 1);
4774 #else
4775 	KASSERT(ukh->ukh_nused == 2);
4776 #endif
4777 
4778 	mutex_spin_enter(&uvm_kentry_lock);
4779 	LIST_INSERT_HEAD(&vm_map_to_kernel(map)->vmk_kentry_free,
4780 	    ukh, ukh_listq);
4781 	mutex_spin_exit(&uvm_kentry_lock);
4782 
4783 	/*
4784 	 * return first entry.
4785 	 */
4786 
4787 	entry = &ukh->ukh_entries[0];
4788 	entry->flags = UVM_MAP_KERNEL;
4789 	UVMMAP_EVCNT_INCR(ukh_alloc);
4790 
4791 	return entry;
4792 }
4793 
4794 /*
4795  * uvm_mapent_free: free map entry for in-kernel map
4796  */
4797 
4798 static void
4799 uvm_kmapent_free(struct vm_map_entry *entry)
4800 {
4801 	struct uvm_kmapent_hdr *ukh;
4802 	struct vm_page *pg;
4803 	struct vm_map *map;
4804 #ifndef PMAP_UNMAP_POOLPAGE
4805 	struct pmap *pmap;
4806 	struct vm_map_entry *deadentry;
4807 #endif
4808 	vaddr_t va;
4809 	paddr_t pa;
4810 
4811 	UVMMAP_EVCNT_INCR(uke_free);
4812 	ukh = UVM_KHDR_FIND(entry);
4813 	map = ukh->ukh_map;
4814 
4815 	mutex_spin_enter(&uvm_kentry_lock);
4816 	uvm_kmapent_put(ukh, entry);
4817 #ifdef PMAP_UNMAP_POOLPAGE
4818 	if (ukh->ukh_nused > 0) {
4819 #else
4820 	if (ukh->ukh_nused > 1) {
4821 #endif
4822 		if (ukh->ukh_nused == UVM_KMAPENT_CHUNK - 1)
4823 			LIST_INSERT_HEAD(
4824 			    &vm_map_to_kernel(map)->vmk_kentry_free,
4825 			    ukh, ukh_listq);
4826 		mutex_spin_exit(&uvm_kentry_lock);
4827 		return;
4828 	}
4829 
4830 	/*
4831 	 * now we can free this ukh.
4832 	 *
4833 	 * however, keep an empty ukh to avoid ping-pong.
4834 	 */
4835 
4836 	if (LIST_FIRST(&vm_map_to_kernel(map)->vmk_kentry_free) == ukh &&
4837 	    LIST_NEXT(ukh, ukh_listq) == NULL) {
4838 		mutex_spin_exit(&uvm_kentry_lock);
4839 		return;
4840 	}
4841 	LIST_REMOVE(ukh, ukh_listq);
4842 	mutex_spin_exit(&uvm_kentry_lock);
4843 
4844 	va = (vaddr_t)ukh;
4845 
4846 #ifdef PMAP_UNMAP_POOLPAGE
4847 	KASSERT(ukh->ukh_nused == 0);
4848 	pa = PMAP_UNMAP_POOLPAGE(va);
4849 	KASSERT(pa != 0);
4850 #else
4851 	KASSERT(ukh->ukh_nused == 1);
4852 
4853 	/*
4854 	 * remove map entry for ukh itsself.
4855 	 */
4856 
4857 	KASSERT((va & PAGE_MASK) == 0);
4858 	vm_map_lock(map);
4859 	uvm_unmap_remove(map, va, va + PAGE_SIZE, &deadentry, NULL, 0);
4860 	KASSERT(deadentry->flags & UVM_MAP_KERNEL);
4861 	KASSERT(deadentry->flags & UVM_MAP_KMAPENT);
4862 	KASSERT(deadentry->next == NULL);
4863 	KASSERT(deadentry == &ukh->ukh_entries[UVM_KMAPENT_CHUNK - 1]);
4864 
4865 	/*
4866 	 * unmap the page from pmap and free it.
4867 	 */
4868 
4869 	pmap = vm_map_pmap(map);
4870 	KASSERT(pmap == pmap_kernel());
4871 	if (!pmap_extract(pmap, va, &pa))
4872 		panic("%s: no mapping", __func__);
4873 	pmap_kremove(va, PAGE_SIZE);
4874 	pmap_update(vm_map_pmap(map));
4875 	vm_map_unlock(map);
4876 #endif /* !PMAP_UNMAP_POOLPAGE */
4877 	pg = PHYS_TO_VM_PAGE(pa);
4878 	uvm_pagefree(pg);
4879 	UVMMAP_EVCNT_INCR(ukh_free);
4880 }
4881 
4882 static vsize_t
4883 uvm_kmapent_overhead(vsize_t size)
4884 {
4885 
4886 	/*
4887 	 * - the max number of unmerged entries is howmany(size, PAGE_SIZE)
4888 	 *   as the min allocation unit is PAGE_SIZE.
4889 	 * - UVM_KMAPENT_CHUNK "kmapent"s are allocated from a page.
4890 	 *   one of them are used to map the page itself.
4891 	 */
4892 
4893 	return howmany(howmany(size, PAGE_SIZE), (UVM_KMAPENT_CHUNK - 1)) *
4894 	    PAGE_SIZE;
4895 }
4896 
4897 /*
4898  * map entry reservation
4899  */
4900 
4901 /*
4902  * uvm_mapent_reserve: reserve map entries for clipping before locking map.
4903  *
4904  * => needed when unmapping entries allocated without UVM_FLAG_QUANTUM.
4905  * => caller shouldn't hold map locked.
4906  */
4907 int
4908 uvm_mapent_reserve(struct vm_map *map, struct uvm_mapent_reservation *umr,
4909     int nentries, int flags)
4910 {
4911 
4912 	umr->umr_nentries = 0;
4913 
4914 	if ((flags & UVM_FLAG_QUANTUM) != 0)
4915 		return 0;
4916 
4917 	if (!VM_MAP_USE_KMAPENT(map))
4918 		return 0;
4919 
4920 	while (nentries--) {
4921 		struct vm_map_entry *ent;
4922 		ent = uvm_kmapent_alloc(map, flags);
4923 		if (!ent) {
4924 			uvm_mapent_unreserve(map, umr);
4925 			return ENOMEM;
4926 		}
4927 		UMR_PUTENTRY(umr, ent);
4928 	}
4929 
4930 	return 0;
4931 }
4932 
4933 /*
4934  * uvm_mapent_unreserve:
4935  *
4936  * => caller shouldn't hold map locked.
4937  * => never fail or sleep.
4938  */
4939 void
4940 uvm_mapent_unreserve(struct vm_map *map, struct uvm_mapent_reservation *umr)
4941 {
4942 
4943 	while (!UMR_EMPTY(umr))
4944 		uvm_kmapent_free(UMR_GETENTRY(umr));
4945 }
4946 
4947 /*
4948  * uvm_mapent_trymerge: try to merge an entry with its neighbors.
4949  *
4950  * => called with map locked.
4951  * => return non zero if successfully merged.
4952  */
4953 
4954 int
4955 uvm_mapent_trymerge(struct vm_map *map, struct vm_map_entry *entry, int flags)
4956 {
4957 	struct uvm_object *uobj;
4958 	struct vm_map_entry *next;
4959 	struct vm_map_entry *prev;
4960 	vsize_t size;
4961 	int merged = 0;
4962 	bool copying;
4963 	int newetype;
4964 
4965 	if (VM_MAP_USE_KMAPENT(map)) {
4966 		return 0;
4967 	}
4968 	if (entry->aref.ar_amap != NULL) {
4969 		return 0;
4970 	}
4971 	if ((entry->flags & UVM_MAP_NOMERGE) != 0) {
4972 		return 0;
4973 	}
4974 
4975 	uobj = entry->object.uvm_obj;
4976 	size = entry->end - entry->start;
4977 	copying = (flags & UVM_MERGE_COPYING) != 0;
4978 	newetype = copying ? (entry->etype & ~UVM_ET_NEEDSCOPY) : entry->etype;
4979 
4980 	next = entry->next;
4981 	if (next != &map->header &&
4982 	    next->start == entry->end &&
4983 	    ((copying && next->aref.ar_amap != NULL &&
4984 	    amap_refs(next->aref.ar_amap) == 1) ||
4985 	    (!copying && next->aref.ar_amap == NULL)) &&
4986 	    UVM_ET_ISCOMPATIBLE(next, newetype,
4987 	    uobj, entry->flags, entry->protection,
4988 	    entry->max_protection, entry->inheritance, entry->advice,
4989 	    entry->wired_count) &&
4990 	    (uobj == NULL || entry->offset + size == next->offset)) {
4991 		int error;
4992 
4993 		if (copying) {
4994 			error = amap_extend(next, size,
4995 			    AMAP_EXTEND_NOWAIT|AMAP_EXTEND_BACKWARDS);
4996 		} else {
4997 			error = 0;
4998 		}
4999 		if (error == 0) {
5000 			if (uobj) {
5001 				if (uobj->pgops->pgo_detach) {
5002 					uobj->pgops->pgo_detach(uobj);
5003 				}
5004 			}
5005 
5006 			entry->end = next->end;
5007 			clear_hints(map, next);
5008 			uvm_map_entry_unlink(map, next);
5009 			if (copying) {
5010 				entry->aref = next->aref;
5011 				entry->etype &= ~UVM_ET_NEEDSCOPY;
5012 			}
5013 			uvm_map_check(map, "trymerge forwardmerge");
5014 			uvm_mapent_free_merged(map, next);
5015 			merged++;
5016 		}
5017 	}
5018 
5019 	prev = entry->prev;
5020 	if (prev != &map->header &&
5021 	    prev->end == entry->start &&
5022 	    ((copying && !merged && prev->aref.ar_amap != NULL &&
5023 	    amap_refs(prev->aref.ar_amap) == 1) ||
5024 	    (!copying && prev->aref.ar_amap == NULL)) &&
5025 	    UVM_ET_ISCOMPATIBLE(prev, newetype,
5026 	    uobj, entry->flags, entry->protection,
5027 	    entry->max_protection, entry->inheritance, entry->advice,
5028 	    entry->wired_count) &&
5029 	    (uobj == NULL ||
5030 	    prev->offset + prev->end - prev->start == entry->offset)) {
5031 		int error;
5032 
5033 		if (copying) {
5034 			error = amap_extend(prev, size,
5035 			    AMAP_EXTEND_NOWAIT|AMAP_EXTEND_FORWARDS);
5036 		} else {
5037 			error = 0;
5038 		}
5039 		if (error == 0) {
5040 			if (uobj) {
5041 				if (uobj->pgops->pgo_detach) {
5042 					uobj->pgops->pgo_detach(uobj);
5043 				}
5044 				entry->offset = prev->offset;
5045 			}
5046 
5047 			entry->start = prev->start;
5048 			clear_hints(map, prev);
5049 			uvm_map_entry_unlink(map, prev);
5050 			if (copying) {
5051 				entry->aref = prev->aref;
5052 				entry->etype &= ~UVM_ET_NEEDSCOPY;
5053 			}
5054 			uvm_map_check(map, "trymerge backmerge");
5055 			uvm_mapent_free_merged(map, prev);
5056 			merged++;
5057 		}
5058 	}
5059 
5060 	return merged;
5061 }
5062 
5063 /*
5064  * uvm_map_create: create map
5065  */
5066 
5067 struct vm_map *
5068 uvm_map_create(pmap_t pmap, vaddr_t vmin, vaddr_t vmax, int flags)
5069 {
5070 	struct vm_map *result;
5071 
5072 	result = malloc(sizeof(struct vm_map), M_VMMAP, M_WAITOK);
5073 	uvm_map_setup(result, vmin, vmax, flags);
5074 	result->pmap = pmap;
5075 	return(result);
5076 }
5077 
5078 /*
5079  * uvm_map_setup: init map
5080  *
5081  * => map must not be in service yet.
5082  */
5083 
5084 void
5085 uvm_map_setup(struct vm_map *map, vaddr_t vmin, vaddr_t vmax, int flags)
5086 {
5087 	int ipl;
5088 
5089 	rb_tree_init(&map->rb_tree, &uvm_map_tree_ops);
5090 	map->header.next = map->header.prev = &map->header;
5091 	map->nentries = 0;
5092 	map->size = 0;
5093 	map->ref_count = 1;
5094 	vm_map_setmin(map, vmin);
5095 	vm_map_setmax(map, vmax);
5096 	map->flags = flags;
5097 	map->first_free = &map->header;
5098 	map->hint = &map->header;
5099 	map->timestamp = 0;
5100 	map->busy = NULL;
5101 
5102 	if ((flags & VM_MAP_INTRSAFE) != 0) {
5103 		ipl = IPL_VM;
5104 	} else {
5105 		ipl = IPL_NONE;
5106 	}
5107 
5108 	rw_init(&map->lock);
5109 	cv_init(&map->cv, "vm_map");
5110 	mutex_init(&map->misc_lock, MUTEX_DRIVER, ipl);
5111 	mutex_init(&map->mutex, MUTEX_DRIVER, ipl);
5112 }
5113 
5114 
5115 /*
5116  *   U N M A P   -   m a i n   e n t r y   p o i n t
5117  */
5118 
5119 /*
5120  * uvm_unmap1: remove mappings from a vm_map (from "start" up to "stop")
5121  *
5122  * => caller must check alignment and size
5123  * => map must be unlocked (we will lock it)
5124  * => flags is UVM_FLAG_QUANTUM or 0.
5125  */
5126 
5127 void
5128 uvm_unmap1(struct vm_map *map, vaddr_t start, vaddr_t end, int flags)
5129 {
5130 	struct vm_map_entry *dead_entries;
5131 	struct uvm_mapent_reservation umr;
5132 	UVMHIST_FUNC("uvm_unmap"); UVMHIST_CALLED(maphist);
5133 
5134 	UVMHIST_LOG(maphist, "  (map=0x%x, start=0x%x, end=0x%x)",
5135 	    map, start, end, 0);
5136 	if (map == kernel_map) {
5137 		LOCKDEBUG_MEM_CHECK((void *)start, end - start);
5138 	}
5139 	/*
5140 	 * work now done by helper functions.   wipe the pmap's and then
5141 	 * detach from the dead entries...
5142 	 */
5143 	uvm_mapent_reserve(map, &umr, 2, flags);
5144 	vm_map_lock(map);
5145 	uvm_unmap_remove(map, start, end, &dead_entries, &umr, flags);
5146 	vm_map_unlock(map);
5147 	uvm_mapent_unreserve(map, &umr);
5148 
5149 	if (dead_entries != NULL)
5150 		uvm_unmap_detach(dead_entries, 0);
5151 
5152 	UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
5153 }
5154 
5155 
5156 /*
5157  * uvm_map_reference: add reference to a map
5158  *
5159  * => map need not be locked (we use misc_lock).
5160  */
5161 
5162 void
5163 uvm_map_reference(struct vm_map *map)
5164 {
5165 	mutex_enter(&map->misc_lock);
5166 	map->ref_count++;
5167 	mutex_exit(&map->misc_lock);
5168 }
5169 
5170 struct vm_map_kernel *
5171 vm_map_to_kernel(struct vm_map *map)
5172 {
5173 
5174 	KASSERT(VM_MAP_IS_KERNEL(map));
5175 
5176 	return (struct vm_map_kernel *)map;
5177 }
5178 
5179 bool
5180 vm_map_starved_p(struct vm_map *map)
5181 {
5182 
5183 	if ((map->flags & VM_MAP_WANTVA) != 0) {
5184 		return true;
5185 	}
5186 	/* XXX */
5187 	if ((vm_map_max(map) - vm_map_min(map)) / 16 * 15 < map->size) {
5188 		return true;
5189 	}
5190 	return false;
5191 }
5192 
5193 #if defined(DDB) || defined(DEBUGPRINT)
5194 
5195 /*
5196  * uvm_map_printit: actually prints the map
5197  */
5198 
5199 void
5200 uvm_map_printit(struct vm_map *map, bool full,
5201     void (*pr)(const char *, ...))
5202 {
5203 	struct vm_map_entry *entry;
5204 
5205 	(*pr)("MAP %p: [0x%lx->0x%lx]\n", map, vm_map_min(map),
5206 	    vm_map_max(map));
5207 	(*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n",
5208 	    map->nentries, map->size, map->ref_count, map->timestamp,
5209 	    map->flags);
5210 	(*pr)("\tpmap=%p(resident=%ld, wired=%ld)\n", map->pmap,
5211 	    pmap_resident_count(map->pmap), pmap_wired_count(map->pmap));
5212 	if (!full)
5213 		return;
5214 	for (entry = map->header.next; entry != &map->header;
5215 	    entry = entry->next) {
5216 		(*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n",
5217 		    entry, entry->start, entry->end, entry->object.uvm_obj,
5218 		    (long long)entry->offset, entry->aref.ar_amap,
5219 		    entry->aref.ar_pageoff);
5220 		(*pr)(
5221 		    "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, "
5222 		    "wc=%d, adv=%d\n",
5223 		    (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
5224 		    (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
5225 		    (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
5226 		    entry->protection, entry->max_protection,
5227 		    entry->inheritance, entry->wired_count, entry->advice);
5228 	}
5229 }
5230 
5231 void
5232 uvm_whatis(uintptr_t addr, void (*pr)(const char *, ...))
5233 {
5234 	struct vm_map *map;
5235 
5236 	for (map = kernel_map;;) {
5237 		struct vm_map_entry *entry;
5238 
5239 		if (!uvm_map_lookup_entry_bytree(map, (vaddr_t)addr, &entry)) {
5240 			break;
5241 		}
5242 		(*pr)("%p is %p+%zu from VMMAP %p\n",
5243 		    (void *)addr, (void *)entry->start,
5244 		    (size_t)(addr - (uintptr_t)entry->start), map);
5245 		if (!UVM_ET_ISSUBMAP(entry)) {
5246 			break;
5247 		}
5248 		map = entry->object.sub_map;
5249 	}
5250 }
5251 
5252 #endif /* DDB || DEBUGPRINT */
5253 
5254 #ifndef __USER_VA0_IS_SAFE
5255 static int
5256 sysctl_user_va0_disable(SYSCTLFN_ARGS)
5257 {
5258 	struct sysctlnode node;
5259 	int t, error;
5260 
5261 	node = *rnode;
5262 	node.sysctl_data = &t;
5263 	t = user_va0_disable;
5264 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
5265 	if (error || newp == NULL)
5266 		return (error);
5267 
5268 	/* lower only at securelevel < 1 */
5269 	if (!t && user_va0_disable &&
5270 	    kauth_authorize_system(l->l_cred,
5271 				   KAUTH_SYSTEM_CHSYSFLAGS /* XXX */, 0,
5272 				   NULL, NULL, NULL))
5273 		return EPERM;
5274 
5275 	user_va0_disable = !!t;
5276 	return 0;
5277 }
5278 
5279 SYSCTL_SETUP(sysctl_uvmmap_setup, "sysctl uvmmap setup")
5280 {
5281 
5282         sysctl_createv(clog, 0, NULL, NULL,
5283                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
5284                        CTLTYPE_INT, "user_va0_disable",
5285                        SYSCTL_DESCR("Disable VA 0"),
5286                        sysctl_user_va0_disable, 0, &user_va0_disable, 0,
5287                        CTL_VM, CTL_CREATE, CTL_EOL);
5288 }
5289 #endif
5290