xref: /dflybsd-src/sys/vm/vm_map.c (revision 48d201a5a8c1dab4aa7166b0812594c101fc43c3)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. 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  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
37  *
38  *
39  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  *
64  * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $
65  * $DragonFly: src/sys/vm/vm_map.c,v 1.32 2004/08/17 18:57:36 dillon Exp $
66  */
67 
68 /*
69  *	Virtual memory mapping module.
70  */
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/lock.h>
76 #include <sys/vmmeter.h>
77 #include <sys/mman.h>
78 #include <sys/vnode.h>
79 #include <sys/resourcevar.h>
80 #include <sys/shm.h>
81 
82 #include <vm/vm.h>
83 #include <vm/vm_param.h>
84 #include <vm/pmap.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_page.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/swap_pager.h>
92 #include <vm/vm_zone.h>
93 
94 #include <sys/thread2.h>
95 
96 /*
97  *	Virtual memory maps provide for the mapping, protection,
98  *	and sharing of virtual memory objects.  In addition,
99  *	this module provides for an efficient virtual copy of
100  *	memory from one map to another.
101  *
102  *	Synchronization is required prior to most operations.
103  *
104  *	Maps consist of an ordered doubly-linked list of simple
105  *	entries; a single hint is used to speed up lookups.
106  *
107  *	Since portions of maps are specified by start/end addresses,
108  *	which may not align with existing map entries, all
109  *	routines merely "clip" entries to these start/end values.
110  *	[That is, an entry is split into two, bordering at a
111  *	start or end value.]  Note that these clippings may not
112  *	always be necessary (as the two resulting entries are then
113  *	not changed); however, the clipping is done for convenience.
114  *
115  *	As mentioned above, virtual copy operations are performed
116  *	by copying VM object references from one map to
117  *	another, and then marking both regions as copy-on-write.
118  */
119 
120 /*
121  *	vm_map_startup:
122  *
123  *	Initialize the vm_map module.  Must be called before
124  *	any other vm_map routines.
125  *
126  *	Map and entry structures are allocated from the general
127  *	purpose memory pool with some exceptions:
128  *
129  *	- The kernel map and kmem submap are allocated statically.
130  *	- Kernel map entries are allocated out of a static pool.
131  *
132  *	These restrictions are necessary since malloc() uses the
133  *	maps and requires map entries.
134  */
135 
136 static struct vm_zone mapentzone_store, mapzone_store;
137 static vm_zone_t mapentzone, mapzone, vmspace_zone;
138 static struct vm_object mapentobj, mapobj;
139 
140 static struct vm_map_entry map_entry_init[MAX_MAPENT];
141 static struct vm_map map_init[MAX_KMAP];
142 
143 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *);
144 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *);
145 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
146 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *);
147 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *);
148 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t);
149 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t,
150 		vm_map_entry_t);
151 static void vm_map_split (vm_map_entry_t);
152 static void vm_map_unclip_range (vm_map_t map, vm_map_entry_t start_entry, vm_offset_t start, vm_offset_t end, int *count, int flags);
153 
154 void
155 vm_map_startup(void)
156 {
157 	mapzone = &mapzone_store;
158 	zbootinit(mapzone, "MAP", sizeof (struct vm_map),
159 		map_init, MAX_KMAP);
160 	mapentzone = &mapentzone_store;
161 	zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry),
162 		map_entry_init, MAX_MAPENT);
163 }
164 
165 /*
166  * Allocate a vmspace structure, including a vm_map and pmap,
167  * and initialize those structures.  The refcnt is set to 1.
168  * The remaining fields must be initialized by the caller.
169  */
170 struct vmspace *
171 vmspace_alloc(vm_offset_t min, vm_offset_t max)
172 {
173 	struct vmspace *vm;
174 
175 	vm = zalloc(vmspace_zone);
176 	vm_map_init(&vm->vm_map, min, max);
177 	pmap_pinit(vmspace_pmap(vm));
178 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
179 	vm->vm_refcnt = 1;
180 	vm->vm_shm = NULL;
181 	vm->vm_exitingcnt = 0;
182 	return (vm);
183 }
184 
185 void
186 vm_init2(void)
187 {
188 	zinitna(mapentzone, &mapentobj, NULL, 0, 0, ZONE_USE_RESERVE, 1);
189 	zinitna(mapzone, &mapobj, NULL, 0, 0, 0, 1);
190 	vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3);
191 	pmap_init2();
192 	vm_object_init2();
193 }
194 
195 static __inline void
196 vmspace_dofree(struct vmspace *vm)
197 {
198 	int count;
199 
200 	/*
201 	 * Make sure any SysV shm is freed, it might not have in
202 	 * exit1()
203 	 */
204 	shmexit(vm);
205 
206 	KKASSERT(vm->vm_upcalls == NULL);
207 
208 	/*
209 	 * Lock the map, to wait out all other references to it.
210 	 * Delete all of the mappings and pages they hold, then call
211 	 * the pmap module to reclaim anything left.
212 	 */
213 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
214 	vm_map_lock(&vm->vm_map);
215 	vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
216 		vm->vm_map.max_offset, &count);
217 	vm_map_unlock(&vm->vm_map);
218 	vm_map_entry_release(count);
219 
220 	pmap_release(vmspace_pmap(vm));
221 	zfree(vmspace_zone, vm);
222 }
223 
224 void
225 vmspace_free(struct vmspace *vm)
226 {
227 	if (vm->vm_refcnt == 0)
228 		panic("vmspace_free: attempt to free already freed vmspace");
229 
230 	if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
231 		vmspace_dofree(vm);
232 }
233 
234 void
235 vmspace_exitfree(struct proc *p)
236 {
237 	struct vmspace *vm;
238 
239 	vm = p->p_vmspace;
240 	p->p_vmspace = NULL;
241 
242 	/*
243 	 * cleanup by parent process wait()ing on exiting child.  vm_refcnt
244 	 * may not be 0 (e.g. fork() and child exits without exec()ing).
245 	 * exitingcnt may increment above 0 and drop back down to zero
246 	 * several times while vm_refcnt is held non-zero.  vm_refcnt
247 	 * may also increment above 0 and drop back down to zero several
248 	 * times while vm_exitingcnt is held non-zero.
249 	 *
250 	 * The last wait on the exiting child's vmspace will clean up
251 	 * the remainder of the vmspace.
252 	 */
253 	if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
254 		vmspace_dofree(vm);
255 }
256 
257 /*
258  * vmspace_swap_count() - count the approximate swap useage in pages for a
259  *			  vmspace.
260  *
261  *	Swap useage is determined by taking the proportional swap used by
262  *	VM objects backing the VM map.  To make up for fractional losses,
263  *	if the VM object has any swap use at all the associated map entries
264  *	count for at least 1 swap page.
265  */
266 int
267 vmspace_swap_count(struct vmspace *vmspace)
268 {
269 	vm_map_t map = &vmspace->vm_map;
270 	vm_map_entry_t cur;
271 	int count = 0;
272 
273 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
274 		vm_object_t object;
275 
276 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
277 		    (object = cur->object.vm_object) != NULL &&
278 		    object->type == OBJT_SWAP
279 		) {
280 			int n = (cur->end - cur->start) / PAGE_SIZE;
281 
282 			if (object->un_pager.swp.swp_bcount) {
283 				count += object->un_pager.swp.swp_bcount *
284 				    SWAP_META_PAGES * n / object->size + 1;
285 			}
286 		}
287 	}
288 	return(count);
289 }
290 
291 
292 /*
293  *	vm_map_create:
294  *
295  *	Creates and returns a new empty VM map with
296  *	the given physical map structure, and having
297  *	the given lower and upper address bounds.
298  */
299 vm_map_t
300 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
301 {
302 	vm_map_t result;
303 
304 	result = zalloc(mapzone);
305 	vm_map_init(result, min, max);
306 	result->pmap = pmap;
307 	return (result);
308 }
309 
310 /*
311  * Initialize an existing vm_map structure
312  * such as that in the vmspace structure.
313  * The pmap is set elsewhere.
314  */
315 void
316 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max)
317 {
318 	map->header.next = map->header.prev = &map->header;
319 	map->nentries = 0;
320 	map->size = 0;
321 	map->system_map = 0;
322 	map->infork = 0;
323 	map->min_offset = min;
324 	map->max_offset = max;
325 	map->first_free = &map->header;
326 	map->hint = &map->header;
327 	map->timestamp = 0;
328 	lockinit(&map->lock, 0, "thrd_sleep", 0, LK_NOPAUSE);
329 }
330 
331 /*
332  *      vm_map_entry_cpu_init:
333  *
334  *	Set an initial negative count so the first attempt to reserve
335  *	space preloads a bunch of vm_map_entry's for this cpu.  This
336  *	routine is called in early boot so we cannot just call
337  *	vm_map_entry_reserve().
338  *
339  *	May be called for a gd other then mycpu.
340  */
341 void
342 vm_map_entry_reserve_cpu_init(globaldata_t gd)
343 {
344 	gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2;
345 }
346 
347 /*
348  *	vm_map_entry_reserve:
349  *
350  *	Reserves vm_map_entry structures so code later on can manipulate
351  *	map_entry structures within a locked map without blocking trying
352  *	to allocate a new vm_map_entry.
353  */
354 int
355 vm_map_entry_reserve(int count)
356 {
357 	struct globaldata *gd = mycpu;
358 	vm_map_entry_t entry;
359 
360 	crit_enter();
361 
362 	/*
363 	 * Make sure we have enough structures in gd_vme_base to handle
364 	 * the reservation request.
365 	 */
366 	while (gd->gd_vme_avail < count) {
367 		entry = zalloc(mapentzone);
368 		entry->next = gd->gd_vme_base;
369 		gd->gd_vme_base = entry;
370 		++gd->gd_vme_avail;
371 	}
372 	gd->gd_vme_avail -= count;
373 	crit_exit();
374 	return(count);
375 }
376 
377 /*
378  *	vm_map_entry_release:
379  *
380  *	Releases previously reserved vm_map_entry structures that were not
381  *	used.  If we have too much junk in our per-cpu cache clean some of
382  *	it out.
383  */
384 void
385 vm_map_entry_release(int count)
386 {
387 	struct globaldata *gd = mycpu;
388 	vm_map_entry_t entry;
389 
390 	crit_enter();
391 	gd->gd_vme_avail += count;
392 	while (gd->gd_vme_avail > MAP_RESERVE_SLOP) {
393 		entry = gd->gd_vme_base;
394 		KKASSERT(entry != NULL);
395 		gd->gd_vme_base = entry->next;
396 		--gd->gd_vme_avail;
397 		crit_exit();
398 		zfree(mapentzone, entry);
399 		crit_enter();
400 	}
401 	crit_exit();
402 }
403 
404 /*
405  *	vm_map_entry_kreserve:
406  *
407  *	Reserve map entry structures for use in kernel_map or (if it exists)
408  *	kmem_map.  These entries have *ALREADY* been reserved on a per-cpu
409  *	basis when the map was inited.  This function is used by zalloc()
410  *	to avoid a recursion when zalloc() itself needs to allocate additional
411  *	kernel memory.
412  *
413  *	This function should only be used when the caller intends to later
414  *	call vm_map_entry_reserve() to 'normalize' the reserve cache.
415  */
416 int
417 vm_map_entry_kreserve(int count)
418 {
419 	struct globaldata *gd = mycpu;
420 
421 	crit_enter();
422 	gd->gd_vme_kdeficit += count;
423 	crit_exit();
424 	KKASSERT(gd->gd_vme_base != NULL);
425 	return(count);
426 }
427 
428 /*
429  *	vm_map_entry_krelease:
430  *
431  *	Release previously reserved map entries for kernel_map or kmem_map
432  *	use.  This routine determines how many entries were actually used and
433  *	replentishes the kernel reserve supply from vme_avail.
434  *
435  *	If there is insufficient supply vme_avail will go negative, which is
436  *	ok.  We cannot safely call zalloc in this function without getting
437  *	into a recursion deadlock.  zalloc() will call vm_map_entry_reserve()
438  *	to regenerate the lost entries.
439  */
440 void
441 vm_map_entry_krelease(int count)
442 {
443 	struct globaldata *gd = mycpu;
444 
445 	crit_enter();
446 	gd->gd_vme_kdeficit -= count;
447 	gd->gd_vme_avail -= gd->gd_vme_kdeficit;	/* can go negative */
448 	gd->gd_vme_kdeficit = 0;
449 	crit_exit();
450 }
451 
452 /*
453  *	vm_map_entry_create:	[ internal use only ]
454  *
455  *	Allocates a VM map entry for insertion.  No entry fields are filled
456  *	in.
457  *
458  *	This routine may be called from an interrupt thread but not a FAST
459  *	interrupt.  This routine may recurse the map lock.
460  */
461 static vm_map_entry_t
462 vm_map_entry_create(vm_map_t map, int *countp)
463 {
464 	struct globaldata *gd = mycpu;
465 	vm_map_entry_t entry;
466 
467 	KKASSERT(*countp > 0);
468 	--*countp;
469 	crit_enter();
470 	entry = gd->gd_vme_base;
471 	KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp));
472 	gd->gd_vme_base = entry->next;
473 	crit_exit();
474 	return(entry);
475 }
476 
477 /*
478  *	vm_map_entry_dispose:	[ internal use only ]
479  *
480  *	Dispose of a vm_map_entry that is no longer being referenced.  This
481  *	function may be called from an interrupt.
482  */
483 static void
484 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp)
485 {
486 	struct globaldata *gd = mycpu;
487 
488 	++*countp;
489 	crit_enter();
490 	entry->next = gd->gd_vme_base;
491 	gd->gd_vme_base = entry;
492 	crit_exit();
493 }
494 
495 
496 /*
497  *	vm_map_entry_{un,}link:
498  *
499  *	Insert/remove entries from maps.
500  */
501 static __inline void
502 vm_map_entry_link(vm_map_t map,
503 		  vm_map_entry_t after_where,
504 		  vm_map_entry_t entry)
505 {
506 	map->nentries++;
507 	entry->prev = after_where;
508 	entry->next = after_where->next;
509 	entry->next->prev = entry;
510 	after_where->next = entry;
511 }
512 
513 static __inline void
514 vm_map_entry_unlink(vm_map_t map,
515 		    vm_map_entry_t entry)
516 {
517 	vm_map_entry_t prev;
518 	vm_map_entry_t next;
519 
520 	if (entry->eflags & MAP_ENTRY_IN_TRANSITION)
521 		panic("vm_map_entry_unlink: attempt to mess with locked entry! %p", entry);
522 	prev = entry->prev;
523 	next = entry->next;
524 	next->prev = prev;
525 	prev->next = next;
526 	map->nentries--;
527 }
528 
529 /*
530  *	SAVE_HINT:
531  *
532  *	Saves the specified entry as the hint for
533  *	future lookups.
534  */
535 #define	SAVE_HINT(map,value) \
536 		(map)->hint = (value);
537 
538 /*
539  *	vm_map_lookup_entry:	[ internal use only ]
540  *
541  *	Finds the map entry containing (or
542  *	immediately preceding) the specified address
543  *	in the given map; the entry is returned
544  *	in the "entry" parameter.  The boolean
545  *	result indicates whether the address is
546  *	actually contained in the map.
547  */
548 boolean_t
549 vm_map_lookup_entry(vm_map_t map, vm_offset_t address,
550     vm_map_entry_t *entry /* OUT */)
551 {
552 	vm_map_entry_t cur;
553 	vm_map_entry_t last;
554 
555 	/*
556 	 * Start looking either from the head of the list, or from the hint.
557 	 */
558 
559 	cur = map->hint;
560 
561 	if (cur == &map->header)
562 		cur = cur->next;
563 
564 	if (address >= cur->start) {
565 		/*
566 		 * Go from hint to end of list.
567 		 *
568 		 * But first, make a quick check to see if we are already looking
569 		 * at the entry we want (which is usually the case). Note also
570 		 * that we don't need to save the hint here... it is the same
571 		 * hint (unless we are at the header, in which case the hint
572 		 * didn't buy us anything anyway).
573 		 */
574 		last = &map->header;
575 		if ((cur != last) && (cur->end > address)) {
576 			*entry = cur;
577 			return (TRUE);
578 		}
579 	} else {
580 		/*
581 		 * Go from start to hint, *inclusively*
582 		 */
583 		last = cur->next;
584 		cur = map->header.next;
585 	}
586 
587 	/*
588 	 * Search linearly
589 	 */
590 
591 	while (cur != last) {
592 		if (cur->end > address) {
593 			if (address >= cur->start) {
594 				/*
595 				 * Save this lookup for future hints, and
596 				 * return
597 				 */
598 
599 				*entry = cur;
600 				SAVE_HINT(map, cur);
601 				return (TRUE);
602 			}
603 			break;
604 		}
605 		cur = cur->next;
606 	}
607 	*entry = cur->prev;
608 	SAVE_HINT(map, *entry);
609 	return (FALSE);
610 }
611 
612 /*
613  *	vm_map_insert:
614  *
615  *	Inserts the given whole VM object into the target
616  *	map at the specified address range.  The object's
617  *	size should match that of the address range.
618  *
619  *	Requires that the map be locked, and leaves it so.  Requires that
620  *	sufficient vm_map_entry structures have been reserved and tracks
621  *	the use via countp.
622  *
623  *	If object is non-NULL, ref count must be bumped by caller
624  *	prior to making call to account for the new entry.
625  */
626 int
627 vm_map_insert(vm_map_t map, int *countp,
628 	      vm_object_t object, vm_ooffset_t offset,
629 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
630 	      int cow)
631 {
632 	vm_map_entry_t new_entry;
633 	vm_map_entry_t prev_entry;
634 	vm_map_entry_t temp_entry;
635 	vm_eflags_t protoeflags;
636 
637 	/*
638 	 * Check that the start and end points are not bogus.
639 	 */
640 
641 	if ((start < map->min_offset) || (end > map->max_offset) ||
642 	    (start >= end))
643 		return (KERN_INVALID_ADDRESS);
644 
645 	/*
646 	 * Find the entry prior to the proposed starting address; if it's part
647 	 * of an existing entry, this range is bogus.
648 	 */
649 
650 	if (vm_map_lookup_entry(map, start, &temp_entry))
651 		return (KERN_NO_SPACE);
652 
653 	prev_entry = temp_entry;
654 
655 	/*
656 	 * Assert that the next entry doesn't overlap the end point.
657 	 */
658 
659 	if ((prev_entry->next != &map->header) &&
660 	    (prev_entry->next->start < end))
661 		return (KERN_NO_SPACE);
662 
663 	protoeflags = 0;
664 
665 	if (cow & MAP_COPY_ON_WRITE)
666 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
667 
668 	if (cow & MAP_NOFAULT) {
669 		protoeflags |= MAP_ENTRY_NOFAULT;
670 
671 		KASSERT(object == NULL,
672 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
673 	}
674 	if (cow & MAP_DISABLE_SYNCER)
675 		protoeflags |= MAP_ENTRY_NOSYNC;
676 	if (cow & MAP_DISABLE_COREDUMP)
677 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
678 
679 	if (object) {
680 		/*
681 		 * When object is non-NULL, it could be shared with another
682 		 * process.  We have to set or clear OBJ_ONEMAPPING
683 		 * appropriately.
684 		 */
685 		if ((object->ref_count > 1) || (object->shadow_count != 0)) {
686 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
687 		}
688 	}
689 	else if ((prev_entry != &map->header) &&
690 		 (prev_entry->eflags == protoeflags) &&
691 		 (prev_entry->end == start) &&
692 		 (prev_entry->wired_count == 0) &&
693 		 ((prev_entry->object.vm_object == NULL) ||
694 		  vm_object_coalesce(prev_entry->object.vm_object,
695 				     OFF_TO_IDX(prev_entry->offset),
696 				     (vm_size_t)(prev_entry->end - prev_entry->start),
697 				     (vm_size_t)(end - prev_entry->end)))) {
698 		/*
699 		 * We were able to extend the object.  Determine if we
700 		 * can extend the previous map entry to include the
701 		 * new range as well.
702 		 */
703 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
704 		    (prev_entry->protection == prot) &&
705 		    (prev_entry->max_protection == max)) {
706 			map->size += (end - prev_entry->end);
707 			prev_entry->end = end;
708 			vm_map_simplify_entry(map, prev_entry, countp);
709 			return (KERN_SUCCESS);
710 		}
711 
712 		/*
713 		 * If we can extend the object but cannot extend the
714 		 * map entry, we have to create a new map entry.  We
715 		 * must bump the ref count on the extended object to
716 		 * account for it.  object may be NULL.
717 		 */
718 		object = prev_entry->object.vm_object;
719 		offset = prev_entry->offset +
720 			(prev_entry->end - prev_entry->start);
721 		vm_object_reference(object);
722 	}
723 
724 	/*
725 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
726 	 * in things like the buffer map where we manage kva but do not manage
727 	 * backing objects.
728 	 */
729 
730 	/*
731 	 * Create a new entry
732 	 */
733 
734 	new_entry = vm_map_entry_create(map, countp);
735 	new_entry->start = start;
736 	new_entry->end = end;
737 
738 	new_entry->eflags = protoeflags;
739 	new_entry->object.vm_object = object;
740 	new_entry->offset = offset;
741 	new_entry->avail_ssize = 0;
742 
743 	new_entry->inheritance = VM_INHERIT_DEFAULT;
744 	new_entry->protection = prot;
745 	new_entry->max_protection = max;
746 	new_entry->wired_count = 0;
747 
748 	/*
749 	 * Insert the new entry into the list
750 	 */
751 
752 	vm_map_entry_link(map, prev_entry, new_entry);
753 	map->size += new_entry->end - new_entry->start;
754 
755 	/*
756 	 * Update the free space hint
757 	 */
758 	if ((map->first_free == prev_entry) &&
759 	    (prev_entry->end >= new_entry->start)) {
760 		map->first_free = new_entry;
761 	}
762 
763 #if 0
764 	/*
765 	 * Temporarily removed to avoid MAP_STACK panic, due to
766 	 * MAP_STACK being a huge hack.  Will be added back in
767 	 * when MAP_STACK (and the user stack mapping) is fixed.
768 	 */
769 	/*
770 	 * It may be possible to simplify the entry
771 	 */
772 	vm_map_simplify_entry(map, new_entry, countp);
773 #endif
774 
775 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
776 		pmap_object_init_pt(map->pmap, start, prot,
777 				    object, OFF_TO_IDX(offset), end - start,
778 				    cow & MAP_PREFAULT_PARTIAL);
779 	}
780 
781 	return (KERN_SUCCESS);
782 }
783 
784 /*
785  * Find sufficient space for `length' bytes in the given map, starting at
786  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
787  *
788  * This function will returned an arbitrarily aligned pointer.  If no
789  * particular alignment is required you should pass align as 1.  Note that
790  * the map may return PAGE_SIZE aligned pointers if all the lengths used in
791  * the map are a multiple of PAGE_SIZE, even if you pass a smaller align
792  * argument.
793  *
794  * 'align' should be a power of 2 but is not required to be.
795  */
796 int
797 vm_map_findspace(
798 	vm_map_t map,
799 	vm_offset_t start,
800 	vm_size_t length,
801 	vm_offset_t align,
802 	vm_offset_t *addr)
803 {
804 	vm_map_entry_t entry, next;
805 	vm_offset_t end;
806 	vm_offset_t align_mask;
807 
808 	if (start < map->min_offset)
809 		start = map->min_offset;
810 	if (start > map->max_offset)
811 		return (1);
812 
813 	/*
814 	 * If the alignment is not a power of 2 we will have to use
815 	 * a mod/division, set align_mask to a special value.
816 	 */
817 	if ((align | (align - 1)) + 1 != (align << 1))
818 		align_mask = (vm_offset_t)-1;
819 	else
820 		align_mask = align - 1;
821 
822 retry:
823 	/*
824 	 * Look for the first possible address; if there's already something
825 	 * at this address, we have to start after it.
826 	 */
827 	if (start == map->min_offset) {
828 		if ((entry = map->first_free) != &map->header)
829 			start = entry->end;
830 	} else {
831 		vm_map_entry_t tmp;
832 
833 		if (vm_map_lookup_entry(map, start, &tmp))
834 			start = tmp->end;
835 		entry = tmp;
836 	}
837 
838 	/*
839 	 * Look through the rest of the map, trying to fit a new region in the
840 	 * gap between existing regions, or after the very last region.
841 	 */
842 	for (;; start = (entry = next)->end) {
843 		/*
844 		 * Adjust the proposed start by the requested alignment,
845 		 * be sure that we didn't wrap the address.
846 		 */
847 		if (align_mask == (vm_offset_t)-1)
848 			end = ((start + align - 1) / align) * align;
849 		else
850 			end = (start + align_mask) & ~align_mask;
851 		if (end < start)
852 			return (1);
853 		start = end;
854 		/*
855 		 * Find the end of the proposed new region.  Be sure we didn't
856 		 * go beyond the end of the map, or wrap around the address.
857 		 * Then check to see if this is the last entry or if the
858 		 * proposed end fits in the gap between this and the next
859 		 * entry.
860 		 */
861 		end = start + length;
862 		if (end > map->max_offset || end < start)
863 			return (1);
864 		next = entry->next;
865 		if (next == &map->header || next->start >= end)
866 			break;
867 	}
868 	SAVE_HINT(map, entry);
869 	if (map == kernel_map) {
870 		vm_offset_t ksize;
871 		if ((ksize = round_page(start + length)) > kernel_vm_end) {
872 			pmap_growkernel(ksize);
873 			goto retry;
874 		}
875 	}
876 	*addr = start;
877 	return (0);
878 }
879 
880 /*
881  *	vm_map_find finds an unallocated region in the target address
882  *	map with the given length.  The search is defined to be
883  *	first-fit from the specified address; the region found is
884  *	returned in the same parameter.
885  *
886  *	If object is non-NULL, ref count must be bumped by caller
887  *	prior to making call to account for the new entry.
888  */
889 int
890 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
891 	    vm_offset_t *addr,	/* IN/OUT */
892 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
893 	    vm_prot_t max, int cow)
894 {
895 	vm_offset_t start;
896 	int result;
897 	int count;
898 
899 	start = *addr;
900 
901 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
902 	vm_map_lock(map);
903 	if (find_space) {
904 		if (vm_map_findspace(map, start, length, 1, addr)) {
905 			vm_map_unlock(map);
906 			vm_map_entry_release(count);
907 			return (KERN_NO_SPACE);
908 		}
909 		start = *addr;
910 	}
911 	result = vm_map_insert(map, &count, object, offset,
912 		start, start + length, prot, max, cow);
913 	vm_map_unlock(map);
914 	vm_map_entry_release(count);
915 
916 	return (result);
917 }
918 
919 /*
920  *	vm_map_simplify_entry:
921  *
922  *	Simplify the given map entry by merging with either neighbor.  This
923  *	routine also has the ability to merge with both neighbors.
924  *
925  *	The map must be locked.
926  *
927  *	This routine guarentees that the passed entry remains valid (though
928  *	possibly extended).  When merging, this routine may delete one or
929  *	both neighbors.  No action is taken on entries which have their
930  *	in-transition flag set.
931  */
932 void
933 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp)
934 {
935 	vm_map_entry_t next, prev;
936 	vm_size_t prevsize, esize;
937 
938 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) {
939 		++mycpu->gd_cnt.v_intrans_coll;
940 		return;
941 	}
942 
943 	prev = entry->prev;
944 	if (prev != &map->header) {
945 		prevsize = prev->end - prev->start;
946 		if ( (prev->end == entry->start) &&
947 		     (prev->object.vm_object == entry->object.vm_object) &&
948 		     (!prev->object.vm_object ||
949 			(prev->offset + prevsize == entry->offset)) &&
950 		     (prev->eflags == entry->eflags) &&
951 		     (prev->protection == entry->protection) &&
952 		     (prev->max_protection == entry->max_protection) &&
953 		     (prev->inheritance == entry->inheritance) &&
954 		     (prev->wired_count == entry->wired_count)) {
955 			if (map->first_free == prev)
956 				map->first_free = entry;
957 			if (map->hint == prev)
958 				map->hint = entry;
959 			vm_map_entry_unlink(map, prev);
960 			entry->start = prev->start;
961 			entry->offset = prev->offset;
962 			if (prev->object.vm_object)
963 				vm_object_deallocate(prev->object.vm_object);
964 			vm_map_entry_dispose(map, prev, countp);
965 		}
966 	}
967 
968 	next = entry->next;
969 	if (next != &map->header) {
970 		esize = entry->end - entry->start;
971 		if ((entry->end == next->start) &&
972 		    (next->object.vm_object == entry->object.vm_object) &&
973 		     (!entry->object.vm_object ||
974 			(entry->offset + esize == next->offset)) &&
975 		    (next->eflags == entry->eflags) &&
976 		    (next->protection == entry->protection) &&
977 		    (next->max_protection == entry->max_protection) &&
978 		    (next->inheritance == entry->inheritance) &&
979 		    (next->wired_count == entry->wired_count)) {
980 			if (map->first_free == next)
981 				map->first_free = entry;
982 			if (map->hint == next)
983 				map->hint = entry;
984 			vm_map_entry_unlink(map, next);
985 			entry->end = next->end;
986 			if (next->object.vm_object)
987 				vm_object_deallocate(next->object.vm_object);
988 			vm_map_entry_dispose(map, next, countp);
989 	        }
990 	}
991 }
992 /*
993  *	vm_map_clip_start:	[ internal use only ]
994  *
995  *	Asserts that the given entry begins at or after
996  *	the specified address; if necessary,
997  *	it splits the entry into two.
998  */
999 #define vm_map_clip_start(map, entry, startaddr, countp) \
1000 { \
1001 	if (startaddr > entry->start) \
1002 		_vm_map_clip_start(map, entry, startaddr, countp); \
1003 }
1004 
1005 /*
1006  *	This routine is called only when it is known that
1007  *	the entry must be split.
1008  */
1009 static void
1010 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start, int *countp)
1011 {
1012 	vm_map_entry_t new_entry;
1013 
1014 	/*
1015 	 * Split off the front portion -- note that we must insert the new
1016 	 * entry BEFORE this one, so that this entry has the specified
1017 	 * starting address.
1018 	 */
1019 
1020 	vm_map_simplify_entry(map, entry, countp);
1021 
1022 	/*
1023 	 * If there is no object backing this entry, we might as well create
1024 	 * one now.  If we defer it, an object can get created after the map
1025 	 * is clipped, and individual objects will be created for the split-up
1026 	 * map.  This is a bit of a hack, but is also about the best place to
1027 	 * put this improvement.
1028 	 */
1029 
1030 	if (entry->object.vm_object == NULL && !map->system_map) {
1031 		vm_object_t object;
1032 		object = vm_object_allocate(OBJT_DEFAULT,
1033 				atop(entry->end - entry->start));
1034 		entry->object.vm_object = object;
1035 		entry->offset = 0;
1036 	}
1037 
1038 	new_entry = vm_map_entry_create(map, countp);
1039 	*new_entry = *entry;
1040 
1041 	new_entry->end = start;
1042 	entry->offset += (start - entry->start);
1043 	entry->start = start;
1044 
1045 	vm_map_entry_link(map, entry->prev, new_entry);
1046 
1047 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1048 		vm_object_reference(new_entry->object.vm_object);
1049 	}
1050 }
1051 
1052 /*
1053  *	vm_map_clip_end:	[ internal use only ]
1054  *
1055  *	Asserts that the given entry ends at or before
1056  *	the specified address; if necessary,
1057  *	it splits the entry into two.
1058  */
1059 
1060 #define vm_map_clip_end(map, entry, endaddr, countp) \
1061 { \
1062 	if (endaddr < entry->end) \
1063 		_vm_map_clip_end(map, entry, endaddr, countp); \
1064 }
1065 
1066 /*
1067  *	This routine is called only when it is known that
1068  *	the entry must be split.
1069  */
1070 static void
1071 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end, int *countp)
1072 {
1073 	vm_map_entry_t new_entry;
1074 
1075 	/*
1076 	 * If there is no object backing this entry, we might as well create
1077 	 * one now.  If we defer it, an object can get created after the map
1078 	 * is clipped, and individual objects will be created for the split-up
1079 	 * map.  This is a bit of a hack, but is also about the best place to
1080 	 * put this improvement.
1081 	 */
1082 
1083 	if (entry->object.vm_object == NULL && !map->system_map) {
1084 		vm_object_t object;
1085 		object = vm_object_allocate(OBJT_DEFAULT,
1086 				atop(entry->end - entry->start));
1087 		entry->object.vm_object = object;
1088 		entry->offset = 0;
1089 	}
1090 
1091 	/*
1092 	 * Create a new entry and insert it AFTER the specified entry
1093 	 */
1094 
1095 	new_entry = vm_map_entry_create(map, countp);
1096 	*new_entry = *entry;
1097 
1098 	new_entry->start = entry->end = end;
1099 	new_entry->offset += (end - entry->start);
1100 
1101 	vm_map_entry_link(map, entry, new_entry);
1102 
1103 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1104 		vm_object_reference(new_entry->object.vm_object);
1105 	}
1106 }
1107 
1108 /*
1109  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
1110  *
1111  *	Asserts that the starting and ending region
1112  *	addresses fall within the valid range of the map.
1113  */
1114 #define	VM_MAP_RANGE_CHECK(map, start, end)		\
1115 		{					\
1116 		if (start < vm_map_min(map))		\
1117 			start = vm_map_min(map);	\
1118 		if (end > vm_map_max(map))		\
1119 			end = vm_map_max(map);		\
1120 		if (start > end)			\
1121 			start = end;			\
1122 		}
1123 
1124 /*
1125  *	vm_map_transition_wait:	[ kernel use only ]
1126  *
1127  *	Used to block when an in-transition collison occurs.  The map
1128  *	is unlocked for the sleep and relocked before the return.
1129  */
1130 static
1131 void
1132 vm_map_transition_wait(vm_map_t map)
1133 {
1134 	vm_map_unlock(map);
1135 	tsleep(map, 0, "vment", 0);
1136 	vm_map_lock(map);
1137 }
1138 
1139 /*
1140  * CLIP_CHECK_BACK
1141  * CLIP_CHECK_FWD
1142  *
1143  *	When we do blocking operations with the map lock held it is
1144  *	possible that a clip might have occured on our in-transit entry,
1145  *	requiring an adjustment to the entry in our loop.  These macros
1146  *	help the pageable and clip_range code deal with the case.  The
1147  *	conditional costs virtually nothing if no clipping has occured.
1148  */
1149 
1150 #define CLIP_CHECK_BACK(entry, save_start)		\
1151     do {						\
1152 	    while (entry->start != save_start) {	\
1153 		    entry = entry->prev;		\
1154 		    KASSERT(entry != &map->header, ("bad entry clip")); \
1155 	    }						\
1156     } while(0)
1157 
1158 #define CLIP_CHECK_FWD(entry, save_end)			\
1159     do {						\
1160 	    while (entry->end != save_end) {		\
1161 		    entry = entry->next;		\
1162 		    KASSERT(entry != &map->header, ("bad entry clip")); \
1163 	    }						\
1164     } while(0)
1165 
1166 
1167 /*
1168  *	vm_map_clip_range:	[ kernel use only ]
1169  *
1170  *	Clip the specified range and return the base entry.  The
1171  *	range may cover several entries starting at the returned base
1172  *	and the first and last entry in the covering sequence will be
1173  *	properly clipped to the requested start and end address.
1174  *
1175  *	If no holes are allowed you should pass the MAP_CLIP_NO_HOLES
1176  *	flag.
1177  *
1178  *	The MAP_ENTRY_IN_TRANSITION flag will be set for the entries
1179  *	covered by the requested range.
1180  *
1181  *	The map must be exclusively locked on entry and will remain locked
1182  *	on return. If no range exists or the range contains holes and you
1183  *	specified that no holes were allowed, NULL will be returned.  This
1184  *	routine may temporarily unlock the map in order avoid a deadlock when
1185  *	sleeping.
1186  */
1187 static
1188 vm_map_entry_t
1189 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end,
1190 	int *countp, int flags)
1191 {
1192 	vm_map_entry_t start_entry;
1193 	vm_map_entry_t entry;
1194 
1195 	/*
1196 	 * Locate the entry and effect initial clipping.  The in-transition
1197 	 * case does not occur very often so do not try to optimize it.
1198 	 */
1199 again:
1200 	if (vm_map_lookup_entry(map, start, &start_entry) == FALSE)
1201 		return (NULL);
1202 	entry = start_entry;
1203 	if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1204 		entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1205 		++mycpu->gd_cnt.v_intrans_coll;
1206 		++mycpu->gd_cnt.v_intrans_wait;
1207 		vm_map_transition_wait(map);
1208 		/*
1209 		 * entry and/or start_entry may have been clipped while
1210 		 * we slept, or may have gone away entirely.  We have
1211 		 * to restart from the lookup.
1212 		 */
1213 		goto again;
1214 	}
1215 	/*
1216 	 * Since we hold an exclusive map lock we do not have to restart
1217 	 * after clipping, even though clipping may block in zalloc.
1218 	 */
1219 	vm_map_clip_start(map, entry, start, countp);
1220 	vm_map_clip_end(map, entry, end, countp);
1221 	entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1222 
1223 	/*
1224 	 * Scan entries covered by the range.  When working on the next
1225 	 * entry a restart need only re-loop on the current entry which
1226 	 * we have already locked, since 'next' may have changed.  Also,
1227 	 * even though entry is safe, it may have been clipped so we
1228 	 * have to iterate forwards through the clip after sleeping.
1229 	 */
1230 	while (entry->next != &map->header && entry->next->start < end) {
1231 		vm_map_entry_t next = entry->next;
1232 
1233 		if (flags & MAP_CLIP_NO_HOLES) {
1234 			if (next->start > entry->end) {
1235 				vm_map_unclip_range(map, start_entry,
1236 					start, entry->end, countp, flags);
1237 				return(NULL);
1238 			}
1239 		}
1240 
1241 		if (next->eflags & MAP_ENTRY_IN_TRANSITION) {
1242 			vm_offset_t save_end = entry->end;
1243 			next->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1244 			++mycpu->gd_cnt.v_intrans_coll;
1245 			++mycpu->gd_cnt.v_intrans_wait;
1246 			vm_map_transition_wait(map);
1247 
1248 			/*
1249 			 * clips might have occured while we blocked.
1250 			 */
1251 			CLIP_CHECK_FWD(entry, save_end);
1252 			CLIP_CHECK_BACK(start_entry, start);
1253 			continue;
1254 		}
1255 		/*
1256 		 * No restart necessary even though clip_end may block, we
1257 		 * are holding the map lock.
1258 		 */
1259 		vm_map_clip_end(map, next, end, countp);
1260 		next->eflags |= MAP_ENTRY_IN_TRANSITION;
1261 		entry = next;
1262 	}
1263 	if (flags & MAP_CLIP_NO_HOLES) {
1264 		if (entry->end != end) {
1265 			vm_map_unclip_range(map, start_entry,
1266 				start, entry->end, countp, flags);
1267 			return(NULL);
1268 		}
1269 	}
1270 	return(start_entry);
1271 }
1272 
1273 /*
1274  *	vm_map_unclip_range:	[ kernel use only ]
1275  *
1276  *	Undo the effect of vm_map_clip_range().  You should pass the same
1277  *	flags and the same range that you passed to vm_map_clip_range().
1278  *	This code will clear the in-transition flag on the entries and
1279  *	wake up anyone waiting.  This code will also simplify the sequence
1280  *	and attempt to merge it with entries before and after the sequence.
1281  *
1282  *	The map must be locked on entry and will remain locked on return.
1283  *
1284  *	Note that you should also pass the start_entry returned by
1285  *	vm_map_clip_range().  However, if you block between the two calls
1286  *	with the map unlocked please be aware that the start_entry may
1287  *	have been clipped and you may need to scan it backwards to find
1288  *	the entry corresponding with the original start address.  You are
1289  *	responsible for this, vm_map_unclip_range() expects the correct
1290  *	start_entry to be passed to it and will KASSERT otherwise.
1291  */
1292 static
1293 void
1294 vm_map_unclip_range(
1295 	vm_map_t map,
1296 	vm_map_entry_t start_entry,
1297 	vm_offset_t start,
1298 	vm_offset_t end,
1299 	int *countp,
1300 	int flags)
1301 {
1302 	vm_map_entry_t entry;
1303 
1304 	entry = start_entry;
1305 
1306 	KASSERT(entry->start == start, ("unclip_range: illegal base entry"));
1307 	while (entry != &map->header && entry->start < end) {
1308 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, ("in-transition flag not set during unclip on: %p", entry));
1309 		KASSERT(entry->end <= end, ("unclip_range: tail wasn't clipped"));
1310 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1311 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1312 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1313 			wakeup(map);
1314 		}
1315 		entry = entry->next;
1316 	}
1317 
1318 	/*
1319 	 * Simplification does not block so there is no restart case.
1320 	 */
1321 	entry = start_entry;
1322 	while (entry != &map->header && entry->start < end) {
1323 		vm_map_simplify_entry(map, entry, countp);
1324 		entry = entry->next;
1325 	}
1326 }
1327 
1328 /*
1329  *	vm_map_submap:		[ kernel use only ]
1330  *
1331  *	Mark the given range as handled by a subordinate map.
1332  *
1333  *	This range must have been created with vm_map_find,
1334  *	and no other operations may have been performed on this
1335  *	range prior to calling vm_map_submap.
1336  *
1337  *	Only a limited number of operations can be performed
1338  *	within this rage after calling vm_map_submap:
1339  *		vm_fault
1340  *	[Don't try vm_map_copy!]
1341  *
1342  *	To remove a submapping, one must first remove the
1343  *	range from the superior map, and then destroy the
1344  *	submap (if desired).  [Better yet, don't try it.]
1345  */
1346 int
1347 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap)
1348 {
1349 	vm_map_entry_t entry;
1350 	int result = KERN_INVALID_ARGUMENT;
1351 	int count;
1352 
1353 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1354 	vm_map_lock(map);
1355 
1356 	VM_MAP_RANGE_CHECK(map, start, end);
1357 
1358 	if (vm_map_lookup_entry(map, start, &entry)) {
1359 		vm_map_clip_start(map, entry, start, &count);
1360 	} else {
1361 		entry = entry->next;
1362 	}
1363 
1364 	vm_map_clip_end(map, entry, end, &count);
1365 
1366 	if ((entry->start == start) && (entry->end == end) &&
1367 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1368 	    (entry->object.vm_object == NULL)) {
1369 		entry->object.sub_map = submap;
1370 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1371 		result = KERN_SUCCESS;
1372 	}
1373 	vm_map_unlock(map);
1374 	vm_map_entry_release(count);
1375 
1376 	return (result);
1377 }
1378 
1379 /*
1380  *	vm_map_protect:
1381  *
1382  *	Sets the protection of the specified address
1383  *	region in the target map.  If "set_max" is
1384  *	specified, the maximum protection is to be set;
1385  *	otherwise, only the current protection is affected.
1386  */
1387 int
1388 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1389 	       vm_prot_t new_prot, boolean_t set_max)
1390 {
1391 	vm_map_entry_t current;
1392 	vm_map_entry_t entry;
1393 	int count;
1394 
1395 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1396 	vm_map_lock(map);
1397 
1398 	VM_MAP_RANGE_CHECK(map, start, end);
1399 
1400 	if (vm_map_lookup_entry(map, start, &entry)) {
1401 		vm_map_clip_start(map, entry, start, &count);
1402 	} else {
1403 		entry = entry->next;
1404 	}
1405 
1406 	/*
1407 	 * Make a first pass to check for protection violations.
1408 	 */
1409 
1410 	current = entry;
1411 	while ((current != &map->header) && (current->start < end)) {
1412 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1413 			vm_map_unlock(map);
1414 			vm_map_entry_release(count);
1415 			return (KERN_INVALID_ARGUMENT);
1416 		}
1417 		if ((new_prot & current->max_protection) != new_prot) {
1418 			vm_map_unlock(map);
1419 			vm_map_entry_release(count);
1420 			return (KERN_PROTECTION_FAILURE);
1421 		}
1422 		current = current->next;
1423 	}
1424 
1425 	/*
1426 	 * Go back and fix up protections. [Note that clipping is not
1427 	 * necessary the second time.]
1428 	 */
1429 	current = entry;
1430 
1431 	while ((current != &map->header) && (current->start < end)) {
1432 		vm_prot_t old_prot;
1433 
1434 		vm_map_clip_end(map, current, end, &count);
1435 
1436 		old_prot = current->protection;
1437 		if (set_max)
1438 			current->protection =
1439 			    (current->max_protection = new_prot) &
1440 			    old_prot;
1441 		else
1442 			current->protection = new_prot;
1443 
1444 		/*
1445 		 * Update physical map if necessary. Worry about copy-on-write
1446 		 * here -- CHECK THIS XXX
1447 		 */
1448 
1449 		if (current->protection != old_prot) {
1450 #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1451 							VM_PROT_ALL)
1452 
1453 			pmap_protect(map->pmap, current->start,
1454 			    current->end,
1455 			    current->protection & MASK(current));
1456 #undef	MASK
1457 		}
1458 
1459 		vm_map_simplify_entry(map, current, &count);
1460 
1461 		current = current->next;
1462 	}
1463 
1464 	vm_map_unlock(map);
1465 	vm_map_entry_release(count);
1466 	return (KERN_SUCCESS);
1467 }
1468 
1469 /*
1470  *	vm_map_madvise:
1471  *
1472  * 	This routine traverses a processes map handling the madvise
1473  *	system call.  Advisories are classified as either those effecting
1474  *	the vm_map_entry structure, or those effecting the underlying
1475  *	objects.
1476  */
1477 
1478 int
1479 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end, int behav)
1480 {
1481 	vm_map_entry_t current, entry;
1482 	int modify_map = 0;
1483 	int count;
1484 
1485 	/*
1486 	 * Some madvise calls directly modify the vm_map_entry, in which case
1487 	 * we need to use an exclusive lock on the map and we need to perform
1488 	 * various clipping operations.  Otherwise we only need a read-lock
1489 	 * on the map.
1490 	 */
1491 
1492 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1493 
1494 	switch(behav) {
1495 	case MADV_NORMAL:
1496 	case MADV_SEQUENTIAL:
1497 	case MADV_RANDOM:
1498 	case MADV_NOSYNC:
1499 	case MADV_AUTOSYNC:
1500 	case MADV_NOCORE:
1501 	case MADV_CORE:
1502 		modify_map = 1;
1503 		vm_map_lock(map);
1504 		break;
1505 	case MADV_WILLNEED:
1506 	case MADV_DONTNEED:
1507 	case MADV_FREE:
1508 		vm_map_lock_read(map);
1509 		break;
1510 	default:
1511 		vm_map_entry_release(count);
1512 		return (KERN_INVALID_ARGUMENT);
1513 	}
1514 
1515 	/*
1516 	 * Locate starting entry and clip if necessary.
1517 	 */
1518 
1519 	VM_MAP_RANGE_CHECK(map, start, end);
1520 
1521 	if (vm_map_lookup_entry(map, start, &entry)) {
1522 		if (modify_map)
1523 			vm_map_clip_start(map, entry, start, &count);
1524 	} else {
1525 		entry = entry->next;
1526 	}
1527 
1528 	if (modify_map) {
1529 		/*
1530 		 * madvise behaviors that are implemented in the vm_map_entry.
1531 		 *
1532 		 * We clip the vm_map_entry so that behavioral changes are
1533 		 * limited to the specified address range.
1534 		 */
1535 		for (current = entry;
1536 		     (current != &map->header) && (current->start < end);
1537 		     current = current->next
1538 		) {
1539 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1540 				continue;
1541 
1542 			vm_map_clip_end(map, current, end, &count);
1543 
1544 			switch (behav) {
1545 			case MADV_NORMAL:
1546 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1547 				break;
1548 			case MADV_SEQUENTIAL:
1549 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1550 				break;
1551 			case MADV_RANDOM:
1552 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1553 				break;
1554 			case MADV_NOSYNC:
1555 				current->eflags |= MAP_ENTRY_NOSYNC;
1556 				break;
1557 			case MADV_AUTOSYNC:
1558 				current->eflags &= ~MAP_ENTRY_NOSYNC;
1559 				break;
1560 			case MADV_NOCORE:
1561 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
1562 				break;
1563 			case MADV_CORE:
1564 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
1565 				break;
1566 			default:
1567 				break;
1568 			}
1569 			vm_map_simplify_entry(map, current, &count);
1570 		}
1571 		vm_map_unlock(map);
1572 	} else {
1573 		vm_pindex_t pindex;
1574 		int count;
1575 
1576 		/*
1577 		 * madvise behaviors that are implemented in the underlying
1578 		 * vm_object.
1579 		 *
1580 		 * Since we don't clip the vm_map_entry, we have to clip
1581 		 * the vm_object pindex and count.
1582 		 */
1583 		for (current = entry;
1584 		     (current != &map->header) && (current->start < end);
1585 		     current = current->next
1586 		) {
1587 			vm_offset_t useStart;
1588 
1589 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1590 				continue;
1591 
1592 			pindex = OFF_TO_IDX(current->offset);
1593 			count = atop(current->end - current->start);
1594 			useStart = current->start;
1595 
1596 			if (current->start < start) {
1597 				pindex += atop(start - current->start);
1598 				count -= atop(start - current->start);
1599 				useStart = start;
1600 			}
1601 			if (current->end > end)
1602 				count -= atop(current->end - end);
1603 
1604 			if (count <= 0)
1605 				continue;
1606 
1607 			vm_object_madvise(current->object.vm_object,
1608 					  pindex, count, behav);
1609 			if (behav == MADV_WILLNEED) {
1610 				pmap_object_init_pt(
1611 				    map->pmap,
1612 				    useStart,
1613 				    current->protection,
1614 				    current->object.vm_object,
1615 				    pindex,
1616 				    (count << PAGE_SHIFT),
1617 				    MAP_PREFAULT_MADVISE
1618 				);
1619 			}
1620 		}
1621 		vm_map_unlock_read(map);
1622 	}
1623 	vm_map_entry_release(count);
1624 	return(0);
1625 }
1626 
1627 
1628 /*
1629  *	vm_map_inherit:
1630  *
1631  *	Sets the inheritance of the specified address
1632  *	range in the target map.  Inheritance
1633  *	affects how the map will be shared with
1634  *	child maps at the time of vm_map_fork.
1635  */
1636 int
1637 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1638 	       vm_inherit_t new_inheritance)
1639 {
1640 	vm_map_entry_t entry;
1641 	vm_map_entry_t temp_entry;
1642 	int count;
1643 
1644 	switch (new_inheritance) {
1645 	case VM_INHERIT_NONE:
1646 	case VM_INHERIT_COPY:
1647 	case VM_INHERIT_SHARE:
1648 		break;
1649 	default:
1650 		return (KERN_INVALID_ARGUMENT);
1651 	}
1652 
1653 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1654 	vm_map_lock(map);
1655 
1656 	VM_MAP_RANGE_CHECK(map, start, end);
1657 
1658 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1659 		entry = temp_entry;
1660 		vm_map_clip_start(map, entry, start, &count);
1661 	} else
1662 		entry = temp_entry->next;
1663 
1664 	while ((entry != &map->header) && (entry->start < end)) {
1665 		vm_map_clip_end(map, entry, end, &count);
1666 
1667 		entry->inheritance = new_inheritance;
1668 
1669 		vm_map_simplify_entry(map, entry, &count);
1670 
1671 		entry = entry->next;
1672 	}
1673 	vm_map_unlock(map);
1674 	vm_map_entry_release(count);
1675 	return (KERN_SUCCESS);
1676 }
1677 
1678 /*
1679  * Implement the semantics of mlock
1680  */
1681 int
1682 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end,
1683     boolean_t new_pageable)
1684 {
1685 	vm_map_entry_t entry;
1686 	vm_map_entry_t start_entry;
1687 	vm_offset_t end;
1688 	int rv = KERN_SUCCESS;
1689 	int count;
1690 
1691 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1692 	vm_map_lock(map);
1693 	VM_MAP_RANGE_CHECK(map, start, real_end);
1694 	end = real_end;
1695 
1696 	start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1697 	if (start_entry == NULL) {
1698 		vm_map_unlock(map);
1699 		vm_map_entry_release(count);
1700 		return (KERN_INVALID_ADDRESS);
1701 	}
1702 
1703 	if (new_pageable == 0) {
1704 		entry = start_entry;
1705 		while ((entry != &map->header) && (entry->start < end)) {
1706 			vm_offset_t save_start;
1707 			vm_offset_t save_end;
1708 
1709 			/*
1710 			 * Already user wired or hard wired (trivial cases)
1711 			 */
1712 			if (entry->eflags & MAP_ENTRY_USER_WIRED) {
1713 				entry = entry->next;
1714 				continue;
1715 			}
1716 			if (entry->wired_count != 0) {
1717 				entry->wired_count++;
1718 				entry->eflags |= MAP_ENTRY_USER_WIRED;
1719 				entry = entry->next;
1720 				continue;
1721 			}
1722 
1723 			/*
1724 			 * A new wiring requires instantiation of appropriate
1725 			 * management structures and the faulting in of the
1726 			 * page.
1727 			 */
1728 			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1729 				int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1730 				if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) {
1731 
1732 					vm_object_shadow(&entry->object.vm_object,
1733 					    &entry->offset,
1734 					    atop(entry->end - entry->start));
1735 					entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1736 
1737 				} else if (entry->object.vm_object == NULL &&
1738 					   !map->system_map) {
1739 
1740 					entry->object.vm_object =
1741 					    vm_object_allocate(OBJT_DEFAULT,
1742 						atop(entry->end - entry->start));
1743 					entry->offset = (vm_offset_t) 0;
1744 
1745 				}
1746 			}
1747 			entry->wired_count++;
1748 			entry->eflags |= MAP_ENTRY_USER_WIRED;
1749 
1750 			/*
1751 			 * Now fault in the area.  Note that vm_fault_wire()
1752 			 * may release the map lock temporarily, it will be
1753 			 * relocked on return.  The in-transition
1754 			 * flag protects the entries.
1755 			 */
1756 			save_start = entry->start;
1757 			save_end = entry->end;
1758 			rv = vm_fault_wire(map, entry, TRUE);
1759 			if (rv) {
1760 				CLIP_CHECK_BACK(entry, save_start);
1761 				for (;;) {
1762 					KASSERT(entry->wired_count == 1, ("bad wired_count on entry"));
1763 					entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1764 					entry->wired_count = 0;
1765 					if (entry->end == save_end)
1766 						break;
1767 					entry = entry->next;
1768 					KASSERT(entry != &map->header, ("bad entry clip during backout"));
1769 				}
1770 				end = save_start;	/* unwire the rest */
1771 				break;
1772 			}
1773 			/*
1774 			 * note that even though the entry might have been
1775 			 * clipped, the USER_WIRED flag we set prevents
1776 			 * duplication so we do not have to do a
1777 			 * clip check.
1778 			 */
1779 			entry = entry->next;
1780 		}
1781 
1782 		/*
1783 		 * If we failed fall through to the unwiring section to
1784 		 * unwire what we had wired so far.  'end' has already
1785 		 * been adjusted.
1786 		 */
1787 		if (rv)
1788 			new_pageable = 1;
1789 
1790 		/*
1791 		 * start_entry might have been clipped if we unlocked the
1792 		 * map and blocked.  No matter how clipped it has gotten
1793 		 * there should be a fragment that is on our start boundary.
1794 		 */
1795 		CLIP_CHECK_BACK(start_entry, start);
1796 	}
1797 
1798 	/*
1799 	 * Deal with the unwiring case.
1800 	 */
1801 	if (new_pageable) {
1802 		/*
1803 		 * This is the unwiring case.  We must first ensure that the
1804 		 * range to be unwired is really wired down.  We know there
1805 		 * are no holes.
1806 		 */
1807 		entry = start_entry;
1808 		while ((entry != &map->header) && (entry->start < end)) {
1809 			if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
1810 				rv = KERN_INVALID_ARGUMENT;
1811 				goto done;
1812 			}
1813 			KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry));
1814 			entry = entry->next;
1815 		}
1816 
1817 		/*
1818 		 * Now decrement the wiring count for each region. If a region
1819 		 * becomes completely unwired, unwire its physical pages and
1820 		 * mappings.
1821 		 */
1822 		/*
1823 		 * The map entries are processed in a loop, checking to
1824 		 * make sure the entry is wired and asserting it has a wired
1825 		 * count. However, another loop was inserted more-or-less in
1826 		 * the middle of the unwiring path. This loop picks up the
1827 		 * "entry" loop variable from the first loop without first
1828 		 * setting it to start_entry. Naturally, the secound loop
1829 		 * is never entered and the pages backing the entries are
1830 		 * never unwired. This can lead to a leak of wired pages.
1831 		 */
1832 		entry = start_entry;
1833 		while ((entry != &map->header) && (entry->start < end)) {
1834 			KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED,
1835 				("expected USER_WIRED on entry %p", entry));
1836 			entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1837 			entry->wired_count--;
1838 			if (entry->wired_count == 0)
1839 				vm_fault_unwire(map, entry);
1840 			entry = entry->next;
1841 		}
1842 	}
1843 done:
1844 	vm_map_unclip_range(map, start_entry, start, real_end, &count,
1845 		MAP_CLIP_NO_HOLES);
1846 	map->timestamp++;
1847 	vm_map_unlock(map);
1848 	vm_map_entry_release(count);
1849 	return (rv);
1850 }
1851 
1852 /*
1853  *	vm_map_wire:
1854  *
1855  *	Sets the pageability of the specified address
1856  *	range in the target map.  Regions specified
1857  *	as not pageable require locked-down physical
1858  *	memory and physical page maps.
1859  *
1860  *	The map must not be locked, but a reference
1861  *	must remain to the map throughout the call.
1862  *
1863  *	This function may be called via the zalloc path and must properly
1864  *	reserve map entries for kernel_map.
1865  */
1866 int
1867 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags)
1868 {
1869 	vm_map_entry_t entry;
1870 	vm_map_entry_t start_entry;
1871 	vm_offset_t end;
1872 	int rv = KERN_SUCCESS;
1873 	int count;
1874 	int s;
1875 
1876 	if (kmflags & KM_KRESERVE)
1877 		count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
1878 	else
1879 		count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1880 	vm_map_lock(map);
1881 	VM_MAP_RANGE_CHECK(map, start, real_end);
1882 	end = real_end;
1883 
1884 	start_entry = vm_map_clip_range(map, start, end, &count, MAP_CLIP_NO_HOLES);
1885 	if (start_entry == NULL) {
1886 		vm_map_unlock(map);
1887 		rv = KERN_INVALID_ADDRESS;
1888 		goto failure;
1889 	}
1890 	if ((kmflags & KM_PAGEABLE) == 0) {
1891 		/*
1892 		 * Wiring.
1893 		 *
1894 		 * 1.  Holding the write lock, we create any shadow or zero-fill
1895 		 * objects that need to be created. Then we clip each map
1896 		 * entry to the region to be wired and increment its wiring
1897 		 * count.  We create objects before clipping the map entries
1898 		 * to avoid object proliferation.
1899 		 *
1900 		 * 2.  We downgrade to a read lock, and call vm_fault_wire to
1901 		 * fault in the pages for any newly wired area (wired_count is
1902 		 * 1).
1903 		 *
1904 		 * Downgrading to a read lock for vm_fault_wire avoids a
1905 		 * possible deadlock with another process that may have faulted
1906 		 * on one of the pages to be wired (it would mark the page busy,
1907 		 * blocking us, then in turn block on the map lock that we
1908 		 * hold).  Because of problems in the recursive lock package,
1909 		 * we cannot upgrade to a write lock in vm_map_lookup.  Thus,
1910 		 * any actions that require the write lock must be done
1911 		 * beforehand.  Because we keep the read lock on the map, the
1912 		 * copy-on-write status of the entries we modify here cannot
1913 		 * change.
1914 		 */
1915 
1916 		entry = start_entry;
1917 		while ((entry != &map->header) && (entry->start < end)) {
1918 			/*
1919 			 * Trivial case if the entry is already wired
1920 			 */
1921 			if (entry->wired_count) {
1922 				entry->wired_count++;
1923 				entry = entry->next;
1924 				continue;
1925 			}
1926 
1927 			/*
1928 			 * The entry is being newly wired, we have to setup
1929 			 * appropriate management structures.  A shadow
1930 			 * object is required for a copy-on-write region,
1931 			 * or a normal object for a zero-fill region.  We
1932 			 * do not have to do this for entries that point to sub
1933 			 * maps because we won't hold the lock on the sub map.
1934 			 */
1935 			if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1936 				int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY;
1937 				if (copyflag &&
1938 				    ((entry->protection & VM_PROT_WRITE) != 0)) {
1939 
1940 					vm_object_shadow(&entry->object.vm_object,
1941 					    &entry->offset,
1942 					    atop(entry->end - entry->start));
1943 					entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
1944 				} else if (entry->object.vm_object == NULL &&
1945 					   !map->system_map) {
1946 					entry->object.vm_object =
1947 					    vm_object_allocate(OBJT_DEFAULT,
1948 						atop(entry->end - entry->start));
1949 					entry->offset = (vm_offset_t) 0;
1950 				}
1951 			}
1952 
1953 			entry->wired_count++;
1954 			entry = entry->next;
1955 		}
1956 
1957 		/*
1958 		 * Pass 2.
1959 		 */
1960 
1961 		/*
1962 		 * HACK HACK HACK HACK
1963 		 *
1964 		 * Unlock the map to avoid deadlocks.  The in-transit flag
1965 		 * protects us from most changes but note that
1966 		 * clipping may still occur.  To prevent clipping from
1967 		 * occuring after the unlock, except for when we are
1968 		 * blocking in vm_fault_wire, we must run at splvm().
1969 		 * Otherwise our accesses to entry->start and entry->end
1970 		 * could be corrupted.  We have to set splvm() prior to
1971 		 * unlocking so start_entry does not change out from
1972 		 * under us at the very beginning of the loop.
1973 		 *
1974 		 * HACK HACK HACK HACK
1975 		 */
1976 
1977 		s = splvm();
1978 
1979 		entry = start_entry;
1980 		while (entry != &map->header && entry->start < end) {
1981 			/*
1982 			 * If vm_fault_wire fails for any page we need to undo
1983 			 * what has been done.  We decrement the wiring count
1984 			 * for those pages which have not yet been wired (now)
1985 			 * and unwire those that have (later).
1986 			 */
1987 			vm_offset_t save_start = entry->start;
1988 			vm_offset_t save_end = entry->end;
1989 
1990 			if (entry->wired_count == 1)
1991 				rv = vm_fault_wire(map, entry, FALSE);
1992 			if (rv) {
1993 				CLIP_CHECK_BACK(entry, save_start);
1994 				for (;;) {
1995 					KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly"));
1996 					entry->wired_count = 0;
1997 					if (entry->end == save_end)
1998 						break;
1999 					entry = entry->next;
2000 					KASSERT(entry != &map->header, ("bad entry clip during backout"));
2001 				}
2002 				end = save_start;
2003 				break;
2004 			}
2005 			CLIP_CHECK_FWD(entry, save_end);
2006 			entry = entry->next;
2007 		}
2008 		splx(s);
2009 
2010 		/*
2011 		 * If a failure occured undo everything by falling through
2012 		 * to the unwiring code.  'end' has already been adjusted
2013 		 * appropriately.
2014 		 */
2015 		if (rv)
2016 			kmflags |= KM_PAGEABLE;
2017 
2018 		/*
2019 		 * start_entry is still IN_TRANSITION but may have been
2020 		 * clipped since vm_fault_wire() unlocks and relocks the
2021 		 * map.  No matter how clipped it has gotten there should
2022 		 * be a fragment that is on our start boundary.
2023 		 */
2024 		CLIP_CHECK_BACK(start_entry, start);
2025 	}
2026 
2027 	if (kmflags & KM_PAGEABLE) {
2028 		/*
2029 		 * This is the unwiring case.  We must first ensure that the
2030 		 * range to be unwired is really wired down.  We know there
2031 		 * are no holes.
2032 		 */
2033 		entry = start_entry;
2034 		while ((entry != &map->header) && (entry->start < end)) {
2035 			if (entry->wired_count == 0) {
2036 				rv = KERN_INVALID_ARGUMENT;
2037 				goto done;
2038 			}
2039 			entry = entry->next;
2040 		}
2041 
2042 		/*
2043 		 * Now decrement the wiring count for each region. If a region
2044 		 * becomes completely unwired, unwire its physical pages and
2045 		 * mappings.
2046 		 */
2047 		entry = start_entry;
2048 		while ((entry != &map->header) && (entry->start < end)) {
2049 			entry->wired_count--;
2050 			if (entry->wired_count == 0)
2051 				vm_fault_unwire(map, entry);
2052 			entry = entry->next;
2053 		}
2054 	}
2055 done:
2056 	vm_map_unclip_range(map, start_entry, start, real_end, &count,
2057 		MAP_CLIP_NO_HOLES);
2058 	map->timestamp++;
2059 	vm_map_unlock(map);
2060 failure:
2061 	if (kmflags & KM_KRESERVE)
2062 		vm_map_entry_krelease(count);
2063 	else
2064 		vm_map_entry_release(count);
2065 	return (rv);
2066 }
2067 
2068 /*
2069  * vm_map_set_wired_quick()
2070  *
2071  *	Mark a newly allocated address range as wired but do not fault in
2072  *	the pages.  The caller is expected to load the pages into the object.
2073  *
2074  *	The map must be locked on entry and will remain locked on return.
2075  */
2076 void
2077 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size, int *countp)
2078 {
2079 	vm_map_entry_t scan;
2080 	vm_map_entry_t entry;
2081 
2082 	entry = vm_map_clip_range(map, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2083 	for (scan = entry; scan != &map->header && scan->start < addr + size; scan = scan->next) {
2084 	    KKASSERT(entry->wired_count == 0);
2085 	    entry->wired_count = 1;
2086 	}
2087 	vm_map_unclip_range(map, entry, addr, addr + size, countp, MAP_CLIP_NO_HOLES);
2088 }
2089 
2090 /*
2091  * vm_map_clean
2092  *
2093  * Push any dirty cached pages in the address range to their pager.
2094  * If syncio is TRUE, dirty pages are written synchronously.
2095  * If invalidate is TRUE, any cached pages are freed as well.
2096  *
2097  * Returns an error if any part of the specified range is not mapped.
2098  */
2099 int
2100 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end, boolean_t syncio,
2101     boolean_t invalidate)
2102 {
2103 	vm_map_entry_t current;
2104 	vm_map_entry_t entry;
2105 	vm_size_t size;
2106 	vm_object_t object;
2107 	vm_ooffset_t offset;
2108 
2109 	vm_map_lock_read(map);
2110 	VM_MAP_RANGE_CHECK(map, start, end);
2111 	if (!vm_map_lookup_entry(map, start, &entry)) {
2112 		vm_map_unlock_read(map);
2113 		return (KERN_INVALID_ADDRESS);
2114 	}
2115 	/*
2116 	 * Make a first pass to check for holes.
2117 	 */
2118 	for (current = entry; current->start < end; current = current->next) {
2119 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2120 			vm_map_unlock_read(map);
2121 			return (KERN_INVALID_ARGUMENT);
2122 		}
2123 		if (end > current->end &&
2124 		    (current->next == &map->header ||
2125 			current->end != current->next->start)) {
2126 			vm_map_unlock_read(map);
2127 			return (KERN_INVALID_ADDRESS);
2128 		}
2129 	}
2130 
2131 	if (invalidate)
2132 		pmap_remove(vm_map_pmap(map), start, end);
2133 	/*
2134 	 * Make a second pass, cleaning/uncaching pages from the indicated
2135 	 * objects as we go.
2136 	 */
2137 	for (current = entry; current->start < end; current = current->next) {
2138 		offset = current->offset + (start - current->start);
2139 		size = (end <= current->end ? end : current->end) - start;
2140 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2141 			vm_map_t smap;
2142 			vm_map_entry_t tentry;
2143 			vm_size_t tsize;
2144 
2145 			smap = current->object.sub_map;
2146 			vm_map_lock_read(smap);
2147 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2148 			tsize = tentry->end - offset;
2149 			if (tsize < size)
2150 				size = tsize;
2151 			object = tentry->object.vm_object;
2152 			offset = tentry->offset + (offset - tentry->start);
2153 			vm_map_unlock_read(smap);
2154 		} else {
2155 			object = current->object.vm_object;
2156 		}
2157 		/*
2158 		 * Note that there is absolutely no sense in writing out
2159 		 * anonymous objects, so we track down the vnode object
2160 		 * to write out.
2161 		 * We invalidate (remove) all pages from the address space
2162 		 * anyway, for semantic correctness.
2163 		 *
2164 		 * note: certain anonymous maps, such as MAP_NOSYNC maps,
2165 		 * may start out with a NULL object.
2166 		 */
2167 		while (object && object->backing_object) {
2168 			offset += object->backing_object_offset;
2169 			object = object->backing_object;
2170 			if (object->size < OFF_TO_IDX( offset + size))
2171 				size = IDX_TO_OFF(object->size) - offset;
2172 		}
2173 		if (object && (object->type == OBJT_VNODE) &&
2174 		    (current->protection & VM_PROT_WRITE)) {
2175 			/*
2176 			 * Flush pages if writing is allowed, invalidate them
2177 			 * if invalidation requested.  Pages undergoing I/O
2178 			 * will be ignored by vm_object_page_remove().
2179 			 *
2180 			 * We cannot lock the vnode and then wait for paging
2181 			 * to complete without deadlocking against vm_fault.
2182 			 * Instead we simply call vm_object_page_remove() and
2183 			 * allow it to block internally on a page-by-page
2184 			 * basis when it encounters pages undergoing async
2185 			 * I/O.
2186 			 */
2187 			int flags;
2188 
2189 			vm_object_reference(object);
2190 			vn_lock(object->handle, NULL,
2191 				LK_EXCLUSIVE | LK_RETRY, curthread);
2192 			flags = (syncio || invalidate) ? OBJPC_SYNC : 0;
2193 			flags |= invalidate ? OBJPC_INVAL : 0;
2194 			vm_object_page_clean(object,
2195 			    OFF_TO_IDX(offset),
2196 			    OFF_TO_IDX(offset + size + PAGE_MASK),
2197 			    flags);
2198 			VOP_UNLOCK(((struct vnode *)object->handle),
2199 				    NULL, 0, curthread);
2200 			vm_object_deallocate(object);
2201 		}
2202 		if (object && invalidate &&
2203 		   ((object->type == OBJT_VNODE) ||
2204 		    (object->type == OBJT_DEVICE))) {
2205 			int clean_only =
2206 				(object->type == OBJT_DEVICE) ? FALSE : TRUE;
2207 			vm_object_reference(object);
2208 			vm_object_page_remove(object,
2209 			    OFF_TO_IDX(offset),
2210 			    OFF_TO_IDX(offset + size + PAGE_MASK),
2211 			    clean_only);
2212 			vm_object_deallocate(object);
2213 		}
2214 		start += size;
2215 	}
2216 
2217 	vm_map_unlock_read(map);
2218 	return (KERN_SUCCESS);
2219 }
2220 
2221 /*
2222  *	vm_map_entry_unwire:	[ internal use only ]
2223  *
2224  *	Make the region specified by this entry pageable.
2225  *
2226  *	The map in question should be locked.
2227  *	[This is the reason for this routine's existence.]
2228  */
2229 static void
2230 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2231 {
2232 	entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2233 	entry->wired_count = 0;
2234 	vm_fault_unwire(map, entry);
2235 }
2236 
2237 /*
2238  *	vm_map_entry_delete:	[ internal use only ]
2239  *
2240  *	Deallocate the given entry from the target map.
2241  */
2242 static void
2243 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp)
2244 {
2245 	vm_map_entry_unlink(map, entry);
2246 	map->size -= entry->end - entry->start;
2247 
2248 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2249 		vm_object_deallocate(entry->object.vm_object);
2250 	}
2251 
2252 	vm_map_entry_dispose(map, entry, countp);
2253 }
2254 
2255 /*
2256  *	vm_map_delete:	[ internal use only ]
2257  *
2258  *	Deallocates the given address range from the target
2259  *	map.
2260  */
2261 int
2262 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp)
2263 {
2264 	vm_object_t object;
2265 	vm_map_entry_t entry;
2266 	vm_map_entry_t first_entry;
2267 
2268 	/*
2269 	 * Find the start of the region, and clip it
2270 	 */
2271 
2272 again:
2273 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2274 		entry = first_entry->next;
2275 	} else {
2276 		entry = first_entry;
2277 		vm_map_clip_start(map, entry, start, countp);
2278 		/*
2279 		 * Fix the lookup hint now, rather than each time though the
2280 		 * loop.
2281 		 */
2282 		SAVE_HINT(map, entry->prev);
2283 	}
2284 
2285 	/*
2286 	 * Save the free space hint
2287 	 */
2288 
2289 	if (entry == &map->header) {
2290 		map->first_free = &map->header;
2291 	} else if (map->first_free->start >= start) {
2292 		map->first_free = entry->prev;
2293 	}
2294 
2295 	/*
2296 	 * Step through all entries in this region
2297 	 */
2298 
2299 	while ((entry != &map->header) && (entry->start < end)) {
2300 		vm_map_entry_t next;
2301 		vm_offset_t s, e;
2302 		vm_pindex_t offidxstart, offidxend, count;
2303 
2304 		/*
2305 		 * If we hit an in-transition entry we have to sleep and
2306 		 * retry.  It's easier (and not really slower) to just retry
2307 		 * since this case occurs so rarely and the hint is already
2308 		 * pointing at the right place.  We have to reset the
2309 		 * start offset so as not to accidently delete an entry
2310 		 * another process just created in vacated space.
2311 		 */
2312 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2313 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2314 			start = entry->start;
2315 			++mycpu->gd_cnt.v_intrans_coll;
2316 			++mycpu->gd_cnt.v_intrans_wait;
2317 			vm_map_transition_wait(map);
2318 			goto again;
2319 		}
2320 		vm_map_clip_end(map, entry, end, countp);
2321 
2322 		s = entry->start;
2323 		e = entry->end;
2324 		next = entry->next;
2325 
2326 		offidxstart = OFF_TO_IDX(entry->offset);
2327 		count = OFF_TO_IDX(e - s);
2328 		object = entry->object.vm_object;
2329 
2330 		/*
2331 		 * Unwire before removing addresses from the pmap; otherwise,
2332 		 * unwiring will put the entries back in the pmap.
2333 		 */
2334 		if (entry->wired_count != 0)
2335 			vm_map_entry_unwire(map, entry);
2336 
2337 		offidxend = offidxstart + count;
2338 
2339 		if ((object == kernel_object) || (object == kmem_object)) {
2340 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2341 		} else {
2342 			pmap_remove(map->pmap, s, e);
2343 			if (object != NULL &&
2344 			    object->ref_count != 1 &&
2345 			    (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING &&
2346 			    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
2347 				vm_object_collapse(object);
2348 				vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2349 				if (object->type == OBJT_SWAP) {
2350 					swap_pager_freespace(object, offidxstart, count);
2351 				}
2352 				if (offidxend >= object->size &&
2353 				    offidxstart < object->size) {
2354 					object->size = offidxstart;
2355 				}
2356 			}
2357 		}
2358 
2359 		/*
2360 		 * Delete the entry (which may delete the object) only after
2361 		 * removing all pmap entries pointing to its pages.
2362 		 * (Otherwise, its page frames may be reallocated, and any
2363 		 * modify bits will be set in the wrong object!)
2364 		 */
2365 		vm_map_entry_delete(map, entry, countp);
2366 		entry = next;
2367 	}
2368 	return (KERN_SUCCESS);
2369 }
2370 
2371 /*
2372  *	vm_map_remove:
2373  *
2374  *	Remove the given address range from the target map.
2375  *	This is the exported form of vm_map_delete.
2376  */
2377 int
2378 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2379 {
2380 	int result;
2381 	int count;
2382 
2383 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2384 	vm_map_lock(map);
2385 	VM_MAP_RANGE_CHECK(map, start, end);
2386 	result = vm_map_delete(map, start, end, &count);
2387 	vm_map_unlock(map);
2388 	vm_map_entry_release(count);
2389 
2390 	return (result);
2391 }
2392 
2393 /*
2394  *	vm_map_check_protection:
2395  *
2396  *	Assert that the target map allows the specified
2397  *	privilege on the entire address region given.
2398  *	The entire region must be allocated.
2399  */
2400 boolean_t
2401 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2402 			vm_prot_t protection)
2403 {
2404 	vm_map_entry_t entry;
2405 	vm_map_entry_t tmp_entry;
2406 
2407 	if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
2408 		return (FALSE);
2409 	}
2410 	entry = tmp_entry;
2411 
2412 	while (start < end) {
2413 		if (entry == &map->header) {
2414 			return (FALSE);
2415 		}
2416 		/*
2417 		 * No holes allowed!
2418 		 */
2419 
2420 		if (start < entry->start) {
2421 			return (FALSE);
2422 		}
2423 		/*
2424 		 * Check protection associated with entry.
2425 		 */
2426 
2427 		if ((entry->protection & protection) != protection) {
2428 			return (FALSE);
2429 		}
2430 		/* go to next entry */
2431 
2432 		start = entry->end;
2433 		entry = entry->next;
2434 	}
2435 	return (TRUE);
2436 }
2437 
2438 /*
2439  * Split the pages in a map entry into a new object.  This affords
2440  * easier removal of unused pages, and keeps object inheritance from
2441  * being a negative impact on memory usage.
2442  */
2443 static void
2444 vm_map_split(vm_map_entry_t entry)
2445 {
2446 	vm_page_t m;
2447 	vm_object_t orig_object, new_object, source;
2448 	vm_offset_t s, e;
2449 	vm_pindex_t offidxstart, offidxend, idx;
2450 	vm_size_t size;
2451 	vm_ooffset_t offset;
2452 
2453 	orig_object = entry->object.vm_object;
2454 	if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP)
2455 		return;
2456 	if (orig_object->ref_count <= 1)
2457 		return;
2458 
2459 	offset = entry->offset;
2460 	s = entry->start;
2461 	e = entry->end;
2462 
2463 	offidxstart = OFF_TO_IDX(offset);
2464 	offidxend = offidxstart + OFF_TO_IDX(e - s);
2465 	size = offidxend - offidxstart;
2466 
2467 	new_object = vm_pager_allocate(orig_object->type,
2468 		NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL);
2469 	if (new_object == NULL)
2470 		return;
2471 
2472 	source = orig_object->backing_object;
2473 	if (source != NULL) {
2474 		vm_object_reference(source);	/* Referenced by new_object */
2475 		LIST_INSERT_HEAD(&source->shadow_head,
2476 				  new_object, shadow_list);
2477 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
2478 		new_object->backing_object_offset =
2479 			orig_object->backing_object_offset + IDX_TO_OFF(offidxstart);
2480 		new_object->backing_object = source;
2481 		source->shadow_count++;
2482 		source->generation++;
2483 	}
2484 
2485 	for (idx = 0; idx < size; idx++) {
2486 		vm_page_t m;
2487 		int ss;		/* s used */
2488 
2489 		/*
2490 		 * splvm protection is required to avoid a race between
2491 		 * the lookup and an interrupt/unbusy/free and our busy
2492 		 * check.
2493 		 */
2494 		ss = splvm();
2495 	retry:
2496 		m = vm_page_lookup(orig_object, offidxstart + idx);
2497 		if (m == NULL) {
2498 			splx(ss);
2499 			continue;
2500 		}
2501 
2502 		/*
2503 		 * We must wait for pending I/O to complete before we can
2504 		 * rename the page.
2505 		 *
2506 		 * We do not have to VM_PROT_NONE the page as mappings should
2507 		 * not be changed by this operation.
2508 		 */
2509 		if (vm_page_sleep_busy(m, TRUE, "spltwt"))
2510 			goto retry;
2511 		vm_page_busy(m);
2512 		vm_page_rename(m, new_object, idx);
2513 		/* page automatically made dirty by rename and cache handled */
2514 		vm_page_busy(m);
2515 		splx(ss);
2516 	}
2517 
2518 	if (orig_object->type == OBJT_SWAP) {
2519 		vm_object_pip_add(orig_object, 1);
2520 		/*
2521 		 * copy orig_object pages into new_object
2522 		 * and destroy unneeded pages in
2523 		 * shadow object.
2524 		 */
2525 		swap_pager_copy(orig_object, new_object, offidxstart, 0);
2526 		vm_object_pip_wakeup(orig_object);
2527 	}
2528 
2529 	/*
2530 	 * Wakeup the pages we played with.  No spl protection is needed
2531 	 * for a simple wakeup.
2532 	 */
2533 	for (idx = 0; idx < size; idx++) {
2534 		m = vm_page_lookup(new_object, idx);
2535 		if (m)
2536 			vm_page_wakeup(m);
2537 	}
2538 
2539 	entry->object.vm_object = new_object;
2540 	entry->offset = 0LL;
2541 	vm_object_deallocate(orig_object);
2542 }
2543 
2544 /*
2545  *	vm_map_copy_entry:
2546  *
2547  *	Copies the contents of the source entry to the destination
2548  *	entry.  The entries *must* be aligned properly.
2549  */
2550 static void
2551 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map,
2552 	vm_map_entry_t src_entry, vm_map_entry_t dst_entry)
2553 {
2554 	vm_object_t src_object;
2555 
2556 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2557 		return;
2558 
2559 	if (src_entry->wired_count == 0) {
2560 
2561 		/*
2562 		 * If the source entry is marked needs_copy, it is already
2563 		 * write-protected.
2564 		 */
2565 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2566 			pmap_protect(src_map->pmap,
2567 			    src_entry->start,
2568 			    src_entry->end,
2569 			    src_entry->protection & ~VM_PROT_WRITE);
2570 		}
2571 
2572 		/*
2573 		 * Make a copy of the object.
2574 		 */
2575 		if ((src_object = src_entry->object.vm_object) != NULL) {
2576 
2577 			if ((src_object->handle == NULL) &&
2578 				(src_object->type == OBJT_DEFAULT ||
2579 				 src_object->type == OBJT_SWAP)) {
2580 				vm_object_collapse(src_object);
2581 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2582 					vm_map_split(src_entry);
2583 					src_object = src_entry->object.vm_object;
2584 				}
2585 			}
2586 
2587 			vm_object_reference(src_object);
2588 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2589 			dst_entry->object.vm_object = src_object;
2590 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2591 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2592 			dst_entry->offset = src_entry->offset;
2593 		} else {
2594 			dst_entry->object.vm_object = NULL;
2595 			dst_entry->offset = 0;
2596 		}
2597 
2598 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2599 		    dst_entry->end - dst_entry->start, src_entry->start);
2600 	} else {
2601 		/*
2602 		 * Of course, wired down pages can't be set copy-on-write.
2603 		 * Cause wired pages to be copied into the new map by
2604 		 * simulating faults (the new pages are pageable)
2605 		 */
2606 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2607 	}
2608 }
2609 
2610 /*
2611  * vmspace_fork:
2612  * Create a new process vmspace structure and vm_map
2613  * based on those of an existing process.  The new map
2614  * is based on the old map, according to the inheritance
2615  * values on the regions in that map.
2616  *
2617  * The source map must not be locked.
2618  */
2619 struct vmspace *
2620 vmspace_fork(struct vmspace *vm1)
2621 {
2622 	struct vmspace *vm2;
2623 	vm_map_t old_map = &vm1->vm_map;
2624 	vm_map_t new_map;
2625 	vm_map_entry_t old_entry;
2626 	vm_map_entry_t new_entry;
2627 	vm_object_t object;
2628 	int count;
2629 
2630 	vm_map_lock(old_map);
2631 	old_map->infork = 1;
2632 
2633 	/*
2634 	 * XXX Note: upcalls are not copied.
2635 	 */
2636 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2637 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2638 	    (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy);
2639 	new_map = &vm2->vm_map;	/* XXX */
2640 	new_map->timestamp = 1;
2641 
2642 	count = 0;
2643 	old_entry = old_map->header.next;
2644 	while (old_entry != &old_map->header) {
2645 		++count;
2646 		old_entry = old_entry->next;
2647 	}
2648 
2649 	count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT);
2650 
2651 	old_entry = old_map->header.next;
2652 	while (old_entry != &old_map->header) {
2653 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2654 			panic("vm_map_fork: encountered a submap");
2655 
2656 		switch (old_entry->inheritance) {
2657 		case VM_INHERIT_NONE:
2658 			break;
2659 
2660 		case VM_INHERIT_SHARE:
2661 			/*
2662 			 * Clone the entry, creating the shared object if necessary.
2663 			 */
2664 			object = old_entry->object.vm_object;
2665 			if (object == NULL) {
2666 				object = vm_object_allocate(OBJT_DEFAULT,
2667 					atop(old_entry->end - old_entry->start));
2668 				old_entry->object.vm_object = object;
2669 				old_entry->offset = (vm_offset_t) 0;
2670 			}
2671 
2672 			/*
2673 			 * Add the reference before calling vm_object_shadow
2674 			 * to insure that a shadow object is created.
2675 			 */
2676 			vm_object_reference(object);
2677 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2678 				vm_object_shadow(&old_entry->object.vm_object,
2679 					&old_entry->offset,
2680 					atop(old_entry->end - old_entry->start));
2681 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2682 				/* Transfer the second reference too. */
2683 				vm_object_reference(
2684 				    old_entry->object.vm_object);
2685 				vm_object_deallocate(object);
2686 				object = old_entry->object.vm_object;
2687 			}
2688 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2689 
2690 			/*
2691 			 * Clone the entry, referencing the shared object.
2692 			 */
2693 			new_entry = vm_map_entry_create(new_map, &count);
2694 			*new_entry = *old_entry;
2695 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2696 			new_entry->wired_count = 0;
2697 
2698 			/*
2699 			 * Insert the entry into the new map -- we know we're
2700 			 * inserting at the end of the new map.
2701 			 */
2702 
2703 			vm_map_entry_link(new_map, new_map->header.prev,
2704 			    new_entry);
2705 
2706 			/*
2707 			 * Update the physical map
2708 			 */
2709 
2710 			pmap_copy(new_map->pmap, old_map->pmap,
2711 			    new_entry->start,
2712 			    (old_entry->end - old_entry->start),
2713 			    old_entry->start);
2714 			break;
2715 
2716 		case VM_INHERIT_COPY:
2717 			/*
2718 			 * Clone the entry and link into the map.
2719 			 */
2720 			new_entry = vm_map_entry_create(new_map, &count);
2721 			*new_entry = *old_entry;
2722 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2723 			new_entry->wired_count = 0;
2724 			new_entry->object.vm_object = NULL;
2725 			vm_map_entry_link(new_map, new_map->header.prev,
2726 			    new_entry);
2727 			vm_map_copy_entry(old_map, new_map, old_entry,
2728 			    new_entry);
2729 			break;
2730 		}
2731 		old_entry = old_entry->next;
2732 	}
2733 
2734 	new_map->size = old_map->size;
2735 	old_map->infork = 0;
2736 	vm_map_unlock(old_map);
2737 	vm_map_entry_release(count);
2738 
2739 	return (vm2);
2740 }
2741 
2742 int
2743 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
2744 	      vm_prot_t prot, vm_prot_t max, int cow)
2745 {
2746 	vm_map_entry_t prev_entry;
2747 	vm_map_entry_t new_stack_entry;
2748 	vm_size_t      init_ssize;
2749 	int            rv;
2750 	int		count;
2751 
2752 	if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS)
2753 		return (KERN_NO_SPACE);
2754 
2755 	if (max_ssize < sgrowsiz)
2756 		init_ssize = max_ssize;
2757 	else
2758 		init_ssize = sgrowsiz;
2759 
2760 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2761 	vm_map_lock(map);
2762 
2763 	/* If addr is already mapped, no go */
2764 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
2765 		vm_map_unlock(map);
2766 		vm_map_entry_release(count);
2767 		return (KERN_NO_SPACE);
2768 	}
2769 
2770 	/* If we would blow our VMEM resource limit, no go */
2771 	if (map->size + init_ssize >
2772 	    curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2773 		vm_map_unlock(map);
2774 		vm_map_entry_release(count);
2775 		return (KERN_NO_SPACE);
2776 	}
2777 
2778 	/* If we can't accomodate max_ssize in the current mapping,
2779 	 * no go.  However, we need to be aware that subsequent user
2780 	 * mappings might map into the space we have reserved for
2781 	 * stack, and currently this space is not protected.
2782 	 *
2783 	 * Hopefully we will at least detect this condition
2784 	 * when we try to grow the stack.
2785 	 */
2786 	if ((prev_entry->next != &map->header) &&
2787 	    (prev_entry->next->start < addrbos + max_ssize)) {
2788 		vm_map_unlock(map);
2789 		vm_map_entry_release(count);
2790 		return (KERN_NO_SPACE);
2791 	}
2792 
2793 	/* We initially map a stack of only init_ssize.  We will
2794 	 * grow as needed later.  Since this is to be a grow
2795 	 * down stack, we map at the top of the range.
2796 	 *
2797 	 * Note: we would normally expect prot and max to be
2798 	 * VM_PROT_ALL, and cow to be 0.  Possibly we should
2799 	 * eliminate these as input parameters, and just
2800 	 * pass these values here in the insert call.
2801 	 */
2802 	rv = vm_map_insert(map, &count,
2803 			   NULL, 0, addrbos + max_ssize - init_ssize,
2804 	                   addrbos + max_ssize, prot, max, cow);
2805 
2806 	/* Now set the avail_ssize amount */
2807 	if (rv == KERN_SUCCESS){
2808 		if (prev_entry != &map->header)
2809 			vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count);
2810 		new_stack_entry = prev_entry->next;
2811 		if (new_stack_entry->end   != addrbos + max_ssize ||
2812 		    new_stack_entry->start != addrbos + max_ssize - init_ssize)
2813 			panic ("Bad entry start/end for new stack entry");
2814 		else
2815 			new_stack_entry->avail_ssize = max_ssize - init_ssize;
2816 	}
2817 
2818 	vm_map_unlock(map);
2819 	vm_map_entry_release(count);
2820 	return (rv);
2821 }
2822 
2823 /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
2824  * desired address is already mapped, or if we successfully grow
2825  * the stack.  Also returns KERN_SUCCESS if addr is outside the
2826  * stack range (this is strange, but preserves compatibility with
2827  * the grow function in vm_machdep.c).
2828  */
2829 int
2830 vm_map_growstack (struct proc *p, vm_offset_t addr)
2831 {
2832 	vm_map_entry_t prev_entry;
2833 	vm_map_entry_t stack_entry;
2834 	vm_map_entry_t new_stack_entry;
2835 	struct vmspace *vm = p->p_vmspace;
2836 	vm_map_t map = &vm->vm_map;
2837 	vm_offset_t    end;
2838 	int grow_amount;
2839 	int rv = KERN_SUCCESS;
2840 	int is_procstack;
2841 	int use_read_lock = 1;
2842 	int count;
2843 
2844 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
2845 Retry:
2846 	if (use_read_lock)
2847 		vm_map_lock_read(map);
2848 	else
2849 		vm_map_lock(map);
2850 
2851 	/* If addr is already in the entry range, no need to grow.*/
2852 	if (vm_map_lookup_entry(map, addr, &prev_entry))
2853 		goto done;
2854 
2855 	if ((stack_entry = prev_entry->next) == &map->header)
2856 		goto done;
2857 	if (prev_entry == &map->header)
2858 		end = stack_entry->start - stack_entry->avail_ssize;
2859 	else
2860 		end = prev_entry->end;
2861 
2862 	/* This next test mimics the old grow function in vm_machdep.c.
2863 	 * It really doesn't quite make sense, but we do it anyway
2864 	 * for compatibility.
2865 	 *
2866 	 * If not growable stack, return success.  This signals the
2867 	 * caller to proceed as he would normally with normal vm.
2868 	 */
2869 	if (stack_entry->avail_ssize < 1 ||
2870 	    addr >= stack_entry->start ||
2871 	    addr <  stack_entry->start - stack_entry->avail_ssize) {
2872 		goto done;
2873 	}
2874 
2875 	/* Find the minimum grow amount */
2876 	grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE);
2877 	if (grow_amount > stack_entry->avail_ssize) {
2878 		rv = KERN_NO_SPACE;
2879 		goto done;
2880 	}
2881 
2882 	/* If there is no longer enough space between the entries
2883 	 * nogo, and adjust the available space.  Note: this
2884 	 * should only happen if the user has mapped into the
2885 	 * stack area after the stack was created, and is
2886 	 * probably an error.
2887 	 *
2888 	 * This also effectively destroys any guard page the user
2889 	 * might have intended by limiting the stack size.
2890 	 */
2891 	if (grow_amount > stack_entry->start - end) {
2892 		if (use_read_lock && vm_map_lock_upgrade(map)) {
2893 			use_read_lock = 0;
2894 			goto Retry;
2895 		}
2896 		use_read_lock = 0;
2897 		stack_entry->avail_ssize = stack_entry->start - end;
2898 		rv = KERN_NO_SPACE;
2899 		goto done;
2900 	}
2901 
2902 	is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr;
2903 
2904 	/* If this is the main process stack, see if we're over the
2905 	 * stack limit.
2906 	 */
2907 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2908 			     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2909 		rv = KERN_NO_SPACE;
2910 		goto done;
2911 	}
2912 
2913 	/* Round up the grow amount modulo SGROWSIZ */
2914 	grow_amount = roundup (grow_amount, sgrowsiz);
2915 	if (grow_amount > stack_entry->avail_ssize) {
2916 		grow_amount = stack_entry->avail_ssize;
2917 	}
2918 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount >
2919 	                     p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
2920 		grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur -
2921 		              ctob(vm->vm_ssize);
2922 	}
2923 
2924 	/* If we would blow our VMEM resource limit, no go */
2925 	if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
2926 		rv = KERN_NO_SPACE;
2927 		goto done;
2928 	}
2929 
2930 	if (use_read_lock && vm_map_lock_upgrade(map)) {
2931 		use_read_lock = 0;
2932 		goto Retry;
2933 	}
2934 	use_read_lock = 0;
2935 
2936 	/* Get the preliminary new entry start value */
2937 	addr = stack_entry->start - grow_amount;
2938 
2939 	/* If this puts us into the previous entry, cut back our growth
2940 	 * to the available space.  Also, see the note above.
2941 	 */
2942 	if (addr < end) {
2943 		stack_entry->avail_ssize = stack_entry->start - end;
2944 		addr = end;
2945 	}
2946 
2947 	rv = vm_map_insert(map, &count,
2948 			   NULL, 0, addr, stack_entry->start,
2949 			   VM_PROT_ALL,
2950 			   VM_PROT_ALL,
2951 			   0);
2952 
2953 	/* Adjust the available stack space by the amount we grew. */
2954 	if (rv == KERN_SUCCESS) {
2955 		if (prev_entry != &map->header)
2956 			vm_map_clip_end(map, prev_entry, addr, &count);
2957 		new_stack_entry = prev_entry->next;
2958 		if (new_stack_entry->end   != stack_entry->start  ||
2959 		    new_stack_entry->start != addr)
2960 			panic ("Bad stack grow start/end in new stack entry");
2961 		else {
2962 			new_stack_entry->avail_ssize = stack_entry->avail_ssize -
2963 							(new_stack_entry->end -
2964 							 new_stack_entry->start);
2965 			if (is_procstack)
2966 				vm->vm_ssize += btoc(new_stack_entry->end -
2967 						     new_stack_entry->start);
2968 		}
2969 	}
2970 
2971 done:
2972 	if (use_read_lock)
2973 		vm_map_unlock_read(map);
2974 	else
2975 		vm_map_unlock(map);
2976 	vm_map_entry_release(count);
2977 	return (rv);
2978 }
2979 
2980 /*
2981  * Unshare the specified VM space for exec.  If other processes are
2982  * mapped to it, then create a new one.  The new vmspace is null.
2983  */
2984 
2985 void
2986 vmspace_exec(struct proc *p, struct vmspace *vmcopy)
2987 {
2988 	struct vmspace *oldvmspace = p->p_vmspace;
2989 	struct vmspace *newvmspace;
2990 	vm_map_t map = &p->p_vmspace->vm_map;
2991 
2992 	/*
2993 	 * If we are execing a resident vmspace we fork it, otherwise
2994 	 * we create a new vmspace.  Note that exitingcnt and upcalls
2995 	 * are not copied to the new vmspace.
2996 	 */
2997 	if (vmcopy)  {
2998 	    newvmspace = vmspace_fork(vmcopy);
2999 	} else {
3000 	    newvmspace = vmspace_alloc(map->min_offset, map->max_offset);
3001 	    bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
3002 		(caddr_t)&oldvmspace->vm_endcopy -
3003 		    (caddr_t)&oldvmspace->vm_startcopy);
3004 	}
3005 
3006 	/*
3007 	 * This code is written like this for prototype purposes.  The
3008 	 * goal is to avoid running down the vmspace here, but let the
3009 	 * other process's that are still using the vmspace to finally
3010 	 * run it down.  Even though there is little or no chance of blocking
3011 	 * here, it is a good idea to keep this form for future mods.
3012 	 */
3013 	p->p_vmspace = newvmspace;
3014 	pmap_pinit2(vmspace_pmap(newvmspace));
3015 	if (p == curproc)
3016 		pmap_activate(p);
3017 	vmspace_free(oldvmspace);
3018 }
3019 
3020 /*
3021  * Unshare the specified VM space for forcing COW.  This
3022  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3023  *
3024  * The exitingcnt test is not strictly necessary but has been
3025  * included for code sanity (to make the code a bit more deterministic).
3026  */
3027 
3028 void
3029 vmspace_unshare(struct proc *p)
3030 {
3031 	struct vmspace *oldvmspace = p->p_vmspace;
3032 	struct vmspace *newvmspace;
3033 
3034 	if (oldvmspace->vm_refcnt == 1 && oldvmspace->vm_exitingcnt == 0)
3035 		return;
3036 	newvmspace = vmspace_fork(oldvmspace);
3037 	p->p_vmspace = newvmspace;
3038 	pmap_pinit2(vmspace_pmap(newvmspace));
3039 	if (p == curproc)
3040 		pmap_activate(p);
3041 	vmspace_free(oldvmspace);
3042 }
3043 
3044 /*
3045  *	vm_map_lookup:
3046  *
3047  *	Finds the VM object, offset, and
3048  *	protection for a given virtual address in the
3049  *	specified map, assuming a page fault of the
3050  *	type specified.
3051  *
3052  *	Leaves the map in question locked for read; return
3053  *	values are guaranteed until a vm_map_lookup_done
3054  *	call is performed.  Note that the map argument
3055  *	is in/out; the returned map must be used in
3056  *	the call to vm_map_lookup_done.
3057  *
3058  *	A handle (out_entry) is returned for use in
3059  *	vm_map_lookup_done, to make that fast.
3060  *
3061  *	If a lookup is requested with "write protection"
3062  *	specified, the map may be changed to perform virtual
3063  *	copying operations, although the data referenced will
3064  *	remain the same.
3065  */
3066 int
3067 vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3068 	      vm_offset_t vaddr,
3069 	      vm_prot_t fault_typea,
3070 	      vm_map_entry_t *out_entry,	/* OUT */
3071 	      vm_object_t *object,		/* OUT */
3072 	      vm_pindex_t *pindex,		/* OUT */
3073 	      vm_prot_t *out_prot,		/* OUT */
3074 	      boolean_t *wired)			/* OUT */
3075 {
3076 	vm_map_entry_t entry;
3077 	vm_map_t map = *var_map;
3078 	vm_prot_t prot;
3079 	vm_prot_t fault_type = fault_typea;
3080 	int use_read_lock = 1;
3081 	int rv = KERN_SUCCESS;
3082 
3083 RetryLookup:
3084 	if (use_read_lock)
3085 		vm_map_lock_read(map);
3086 	else
3087 		vm_map_lock(map);
3088 
3089 	/*
3090 	 * If the map has an interesting hint, try it before calling full
3091 	 * blown lookup routine.
3092 	 */
3093 	entry = map->hint;
3094 	*out_entry = entry;
3095 
3096 	if ((entry == &map->header) ||
3097 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
3098 		vm_map_entry_t tmp_entry;
3099 
3100 		/*
3101 		 * Entry was either not a valid hint, or the vaddr was not
3102 		 * contained in the entry, so do a full lookup.
3103 		 */
3104 		if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) {
3105 			rv = KERN_INVALID_ADDRESS;
3106 			goto done;
3107 		}
3108 
3109 		entry = tmp_entry;
3110 		*out_entry = entry;
3111 	}
3112 
3113 	/*
3114 	 * Handle submaps.
3115 	 */
3116 
3117 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3118 		vm_map_t old_map = map;
3119 
3120 		*var_map = map = entry->object.sub_map;
3121 		if (use_read_lock)
3122 			vm_map_unlock_read(old_map);
3123 		else
3124 			vm_map_unlock(old_map);
3125 		use_read_lock = 1;
3126 		goto RetryLookup;
3127 	}
3128 
3129 	/*
3130 	 * Check whether this task is allowed to have this page.
3131 	 * Note the special case for MAP_ENTRY_COW
3132 	 * pages with an override.  This is to implement a forced
3133 	 * COW for debuggers.
3134 	 */
3135 
3136 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
3137 		prot = entry->max_protection;
3138 	else
3139 		prot = entry->protection;
3140 
3141 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3142 	if ((fault_type & prot) != fault_type) {
3143 		rv = KERN_PROTECTION_FAILURE;
3144 		goto done;
3145 	}
3146 
3147 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3148 	    (entry->eflags & MAP_ENTRY_COW) &&
3149 	    (fault_type & VM_PROT_WRITE) &&
3150 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3151 		rv = KERN_PROTECTION_FAILURE;
3152 		goto done;
3153 	}
3154 
3155 	/*
3156 	 * If this page is not pageable, we have to get it for all possible
3157 	 * accesses.
3158 	 */
3159 
3160 	*wired = (entry->wired_count != 0);
3161 	if (*wired)
3162 		prot = fault_type = entry->protection;
3163 
3164 	/*
3165 	 * If the entry was copy-on-write, we either ...
3166 	 */
3167 
3168 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3169 		/*
3170 		 * If we want to write the page, we may as well handle that
3171 		 * now since we've got the map locked.
3172 		 *
3173 		 * If we don't need to write the page, we just demote the
3174 		 * permissions allowed.
3175 		 */
3176 
3177 		if (fault_type & VM_PROT_WRITE) {
3178 			/*
3179 			 * Make a new object, and place it in the object
3180 			 * chain.  Note that no new references have appeared
3181 			 * -- one just moved from the map to the new
3182 			 * object.
3183 			 */
3184 
3185 			if (use_read_lock && vm_map_lock_upgrade(map)) {
3186 				use_read_lock = 0;
3187 				goto RetryLookup;
3188 			}
3189 			use_read_lock = 0;
3190 
3191 			vm_object_shadow(
3192 			    &entry->object.vm_object,
3193 			    &entry->offset,
3194 			    atop(entry->end - entry->start));
3195 
3196 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3197 		} else {
3198 			/*
3199 			 * We're attempting to read a copy-on-write page --
3200 			 * don't allow writes.
3201 			 */
3202 
3203 			prot &= ~VM_PROT_WRITE;
3204 		}
3205 	}
3206 
3207 	/*
3208 	 * Create an object if necessary.
3209 	 */
3210 	if (entry->object.vm_object == NULL &&
3211 	    !map->system_map) {
3212 		if (use_read_lock && vm_map_lock_upgrade(map))  {
3213 			use_read_lock = 0;
3214 			goto RetryLookup;
3215 		}
3216 		use_read_lock = 0;
3217 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3218 		    atop(entry->end - entry->start));
3219 		entry->offset = 0;
3220 	}
3221 
3222 	/*
3223 	 * Return the object/offset from this entry.  If the entry was
3224 	 * copy-on-write or empty, it has been fixed up.
3225 	 */
3226 
3227 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3228 	*object = entry->object.vm_object;
3229 
3230 	/*
3231 	 * Return whether this is the only map sharing this data.  On
3232 	 * success we return with a read lock held on the map.  On failure
3233 	 * we return with the map unlocked.
3234 	 */
3235 	*out_prot = prot;
3236 done:
3237 	if (rv == KERN_SUCCESS) {
3238 		if (use_read_lock == 0)
3239 			vm_map_lock_downgrade(map);
3240 	} else if (use_read_lock) {
3241 		vm_map_unlock_read(map);
3242 	} else {
3243 		vm_map_unlock(map);
3244 	}
3245 	return (rv);
3246 }
3247 
3248 /*
3249  *	vm_map_lookup_done:
3250  *
3251  *	Releases locks acquired by a vm_map_lookup
3252  *	(according to the handle returned by that lookup).
3253  */
3254 
3255 void
3256 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count)
3257 {
3258 	/*
3259 	 * Unlock the main-level map
3260 	 */
3261 	vm_map_unlock_read(map);
3262 	if (count)
3263 		vm_map_entry_release(count);
3264 }
3265 
3266 #ifdef ENABLE_VFS_IOOPT
3267 
3268 /*
3269  * Implement uiomove with VM operations.  This handles (and collateral changes)
3270  * support every combination of source object modification, and COW type
3271  * operations.
3272  *
3273  * XXX this is extremely dangerous, enabling this option is NOT recommended.
3274  */
3275 int
3276 vm_uiomove(vm_map_t mapa, vm_object_t srcobject, off_t cp, int cnta,
3277     vm_offset_t uaddra, int *npages)
3278 {
3279 	vm_map_t map;
3280 	vm_object_t first_object, oldobject, object;
3281 	vm_map_entry_t entry;
3282 	vm_prot_t prot;
3283 	boolean_t wired;
3284 	int tcnt, rv;
3285 	vm_offset_t uaddr, start, end, tend;
3286 	vm_pindex_t first_pindex, osize, oindex;
3287 	off_t ooffset;
3288 	int cnt;
3289 	int count;
3290 	int s;
3291 
3292 	if (npages)
3293 		*npages = 0;
3294 
3295 	cnt = cnta;
3296 	uaddr = uaddra;
3297 
3298 	while (cnt > 0) {
3299 		map = mapa;
3300 
3301 		count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
3302 
3303 		if ((vm_map_lookup(&map, uaddr,
3304 			VM_PROT_READ, &entry, &first_object,
3305 			&first_pindex, &prot, &wired)) != KERN_SUCCESS) {
3306 			return EFAULT;
3307 		}
3308 
3309 		vm_map_clip_start(map, entry, uaddr, &count);
3310 
3311 		tcnt = cnt;
3312 		tend = uaddr + tcnt;
3313 		if (tend > entry->end) {
3314 			tcnt = entry->end - uaddr;
3315 			tend = entry->end;
3316 		}
3317 
3318 		vm_map_clip_end(map, entry, tend, &count);
3319 
3320 		start = entry->start;
3321 		end = entry->end;
3322 
3323 		osize = atop(tcnt);
3324 
3325 		oindex = OFF_TO_IDX(cp);
3326 		if (npages) {
3327 			vm_pindex_t idx;
3328 
3329 			/*
3330 			 * spl protection is needed to avoid a race between
3331 			 * the lookup and an interrupt/unbusy/free occuring
3332 			 * prior to our busy check.
3333 			 */
3334 			s = splvm();
3335 			for (idx = 0; idx < osize; idx++) {
3336 				vm_page_t m;
3337 				if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) {
3338 					splx(s);
3339 					vm_map_lookup_done(map, entry, count);
3340 					return 0;
3341 				}
3342 				/*
3343 				 * disallow busy or invalid pages, but allow
3344 				 * m->busy pages if they are entirely valid.
3345 				 */
3346 				if ((m->flags & PG_BUSY) ||
3347 					((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) {
3348 					splx(s);
3349 					vm_map_lookup_done(map, entry, count);
3350 					return 0;
3351 				}
3352 			}
3353 			splx(s);
3354 		}
3355 
3356 /*
3357  * If we are changing an existing map entry, just redirect
3358  * the object, and change mappings.
3359  */
3360 		if ((first_object->type == OBJT_VNODE) &&
3361 			((oldobject = entry->object.vm_object) == first_object)) {
3362 
3363 			if ((entry->offset != cp) || (oldobject != srcobject)) {
3364 				/*
3365    				* Remove old window into the file
3366    				*/
3367 				pmap_remove (map->pmap, uaddr, tend);
3368 
3369 				/*
3370    				* Force copy on write for mmaped regions
3371    				*/
3372 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3373 
3374 				/*
3375    				* Point the object appropriately
3376    				*/
3377 				if (oldobject != srcobject) {
3378 
3379 				/*
3380    				* Set the object optimization hint flag
3381    				*/
3382 					vm_object_set_flag(srcobject, OBJ_OPT);
3383 					vm_object_reference(srcobject);
3384 					entry->object.vm_object = srcobject;
3385 
3386 					if (oldobject) {
3387 						vm_object_deallocate(oldobject);
3388 					}
3389 				}
3390 
3391 				entry->offset = cp;
3392 				map->timestamp++;
3393 			} else {
3394 				pmap_remove (map->pmap, uaddr, tend);
3395 			}
3396 
3397 		} else if ((first_object->ref_count == 1) &&
3398 			(first_object->size == osize) &&
3399 			((first_object->type == OBJT_DEFAULT) ||
3400 				(first_object->type == OBJT_SWAP)) ) {
3401 
3402 			oldobject = first_object->backing_object;
3403 
3404 			if ((first_object->backing_object_offset != cp) ||
3405 				(oldobject != srcobject)) {
3406 				/*
3407    				* Remove old window into the file
3408    				*/
3409 				pmap_remove (map->pmap, uaddr, tend);
3410 
3411 				/*
3412 				 * Remove unneeded old pages
3413 				 */
3414 				vm_object_page_remove(first_object, 0, 0, 0);
3415 
3416 				/*
3417 				 * Invalidate swap space
3418 				 */
3419 				if (first_object->type == OBJT_SWAP) {
3420 					swap_pager_freespace(first_object,
3421 						0,
3422 						first_object->size);
3423 				}
3424 
3425 				/*
3426    				* Force copy on write for mmaped regions
3427    				*/
3428 				vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3429 
3430 				/*
3431    				* Point the object appropriately
3432    				*/
3433 				if (oldobject != srcobject) {
3434 
3435 				/*
3436    				* Set the object optimization hint flag
3437    				*/
3438 					vm_object_set_flag(srcobject, OBJ_OPT);
3439 					vm_object_reference(srcobject);
3440 
3441 					if (oldobject) {
3442 						LIST_REMOVE(
3443 							first_object, shadow_list);
3444 						oldobject->shadow_count--;
3445 						/* XXX bump generation? */
3446 						vm_object_deallocate(oldobject);
3447 					}
3448 
3449 					LIST_INSERT_HEAD(&srcobject->shadow_head,
3450 						first_object, shadow_list);
3451 					srcobject->shadow_count++;
3452 					/* XXX bump generation? */
3453 
3454 					first_object->backing_object = srcobject;
3455 				}
3456 				first_object->backing_object_offset = cp;
3457 				map->timestamp++;
3458 			} else {
3459 				pmap_remove (map->pmap, uaddr, tend);
3460 			}
3461 /*
3462  * Otherwise, we have to do a logical mmap.
3463  */
3464 		} else {
3465 
3466 			vm_object_set_flag(srcobject, OBJ_OPT);
3467 			vm_object_reference(srcobject);
3468 
3469 			pmap_remove (map->pmap, uaddr, tend);
3470 
3471 			vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize);
3472 			vm_map_lock_upgrade(map);
3473 
3474 			if (entry == &map->header) {
3475 				map->first_free = &map->header;
3476 			} else if (map->first_free->start >= start) {
3477 				map->first_free = entry->prev;
3478 			}
3479 
3480 			SAVE_HINT(map, entry->prev);
3481 			vm_map_entry_delete(map, entry, &count);
3482 
3483 			object = srcobject;
3484 			ooffset = cp;
3485 
3486 			rv = vm_map_insert(map, &count,
3487 				object, ooffset, start, tend,
3488 				VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE);
3489 
3490 			if (rv != KERN_SUCCESS)
3491 				panic("vm_uiomove: could not insert new entry: %d", rv);
3492 		}
3493 
3494 /*
3495  * Map the window directly, if it is already in memory
3496  */
3497 		pmap_object_init_pt(map->pmap, uaddr, entry->protection,
3498 			srcobject, oindex, tcnt, 0);
3499 
3500 		map->timestamp++;
3501 		vm_map_unlock(map);
3502 		vm_map_entry_release(count);
3503 
3504 		cnt -= tcnt;
3505 		uaddr += tcnt;
3506 		cp += tcnt;
3507 		if (npages)
3508 			*npages += osize;
3509 	}
3510 	return 0;
3511 }
3512 
3513 #endif
3514 
3515 /*
3516  * Performs the copy_on_write operations necessary to allow the virtual copies
3517  * into user space to work.  This has to be called for write(2) system calls
3518  * from other processes, file unlinking, and file size shrinkage.
3519  */
3520 void
3521 vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa)
3522 {
3523 	int rv;
3524 	vm_object_t robject;
3525 	vm_pindex_t idx;
3526 
3527 	if ((object == NULL) ||
3528 		((object->flags & OBJ_OPT) == 0))
3529 		return;
3530 
3531 	if (object->shadow_count > object->ref_count)
3532 		panic("vm_freeze_copyopts: sc > rc");
3533 
3534 	while ((robject = LIST_FIRST(&object->shadow_head)) != NULL) {
3535 		vm_pindex_t bo_pindex;
3536 		vm_page_t m_in, m_out;
3537 
3538 		bo_pindex = OFF_TO_IDX(robject->backing_object_offset);
3539 
3540 		vm_object_reference(robject);
3541 
3542 		vm_object_pip_wait(robject, "objfrz");
3543 
3544 		if (robject->ref_count == 1) {
3545 			vm_object_deallocate(robject);
3546 			continue;
3547 		}
3548 
3549 		vm_object_pip_add(robject, 1);
3550 
3551 		for (idx = 0; idx < robject->size; idx++) {
3552 
3553 			m_out = vm_page_grab(robject, idx,
3554 					    VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3555 
3556 			if (m_out->valid == 0) {
3557 				m_in = vm_page_grab(object, bo_pindex + idx,
3558 					    VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3559 				if (m_in->valid == 0) {
3560 					rv = vm_pager_get_pages(object, &m_in, 1, 0);
3561 					if (rv != VM_PAGER_OK) {
3562 						printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex);
3563 						continue;
3564 					}
3565 					vm_page_deactivate(m_in);
3566 				}
3567 
3568 				vm_page_protect(m_in, VM_PROT_NONE);
3569 				pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out));
3570 				m_out->valid = m_in->valid;
3571 				vm_page_dirty(m_out);
3572 				vm_page_activate(m_out);
3573 				vm_page_wakeup(m_in);
3574 			}
3575 			vm_page_wakeup(m_out);
3576 		}
3577 
3578 		object->shadow_count--;
3579 		object->ref_count--;
3580 		LIST_REMOVE(robject, shadow_list);
3581 		robject->backing_object = NULL;
3582 		robject->backing_object_offset = 0;
3583 
3584 		vm_object_pip_wakeup(robject);
3585 		vm_object_deallocate(robject);
3586 	}
3587 
3588 	vm_object_clear_flag(object, OBJ_OPT);
3589 }
3590 
3591 #include "opt_ddb.h"
3592 #ifdef DDB
3593 #include <sys/kernel.h>
3594 
3595 #include <ddb/ddb.h>
3596 
3597 /*
3598  *	vm_map_print:	[ debug ]
3599  */
3600 DB_SHOW_COMMAND(map, vm_map_print)
3601 {
3602 	static int nlines;
3603 	/* XXX convert args. */
3604 	vm_map_t map = (vm_map_t)addr;
3605 	boolean_t full = have_addr;
3606 
3607 	vm_map_entry_t entry;
3608 
3609 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3610 	    (void *)map,
3611 	    (void *)map->pmap, map->nentries, map->timestamp);
3612 	nlines++;
3613 
3614 	if (!full && db_indent)
3615 		return;
3616 
3617 	db_indent += 2;
3618 	for (entry = map->header.next; entry != &map->header;
3619 	    entry = entry->next) {
3620 		db_iprintf("map entry %p: start=%p, end=%p\n",
3621 		    (void *)entry, (void *)entry->start, (void *)entry->end);
3622 		nlines++;
3623 		{
3624 			static char *inheritance_name[4] =
3625 			{"share", "copy", "none", "donate_copy"};
3626 
3627 			db_iprintf(" prot=%x/%x/%s",
3628 			    entry->protection,
3629 			    entry->max_protection,
3630 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3631 			if (entry->wired_count != 0)
3632 				db_printf(", wired");
3633 		}
3634 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3635 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3636 			db_printf(", share=%p, offset=0x%lx\n",
3637 			    (void *)entry->object.sub_map,
3638 			    (long)entry->offset);
3639 			nlines++;
3640 			if ((entry->prev == &map->header) ||
3641 			    (entry->prev->object.sub_map !=
3642 				entry->object.sub_map)) {
3643 				db_indent += 2;
3644 				vm_map_print((db_expr_t)(intptr_t)
3645 					     entry->object.sub_map,
3646 					     full, 0, (char *)0);
3647 				db_indent -= 2;
3648 			}
3649 		} else {
3650 			/* XXX no %qd in kernel.  Truncate entry->offset. */
3651 			db_printf(", object=%p, offset=0x%lx",
3652 			    (void *)entry->object.vm_object,
3653 			    (long)entry->offset);
3654 			if (entry->eflags & MAP_ENTRY_COW)
3655 				db_printf(", copy (%s)",
3656 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3657 			db_printf("\n");
3658 			nlines++;
3659 
3660 			if ((entry->prev == &map->header) ||
3661 			    (entry->prev->object.vm_object !=
3662 				entry->object.vm_object)) {
3663 				db_indent += 2;
3664 				vm_object_print((db_expr_t)(intptr_t)
3665 						entry->object.vm_object,
3666 						full, 0, (char *)0);
3667 				nlines += 4;
3668 				db_indent -= 2;
3669 			}
3670 		}
3671 	}
3672 	db_indent -= 2;
3673 	if (db_indent == 0)
3674 		nlines = 0;
3675 }
3676 
3677 
3678 DB_SHOW_COMMAND(procvm, procvm)
3679 {
3680 	struct proc *p;
3681 
3682 	if (have_addr) {
3683 		p = (struct proc *) addr;
3684 	} else {
3685 		p = curproc;
3686 	}
3687 
3688 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3689 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3690 	    (void *)vmspace_pmap(p->p_vmspace));
3691 
3692 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
3693 }
3694 
3695 #endif /* DDB */
3696