xref: /original-bsd/sys/vm/vm_fault.c (revision 42c7e7a1)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * 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  * %sccs.include.redist.c%
9  *
10  *	@(#)vm_fault.c	7.13 (Berkeley) 06/05/92
11  *
12  *
13  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
14  * All rights reserved.
15  *
16  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
17  *
18  * Permission to use, copy, modify and distribute this software and
19  * its documentation is hereby granted, provided that both the copyright
20  * notice and this permission notice appear in all copies of the
21  * software, derivative works or modified versions, and any portions
22  * thereof, and that both notices appear in supporting documentation.
23  *
24  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
25  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
26  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
27  *
28  * Carnegie Mellon requests users of this software to return to
29  *
30  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
31  *  School of Computer Science
32  *  Carnegie Mellon University
33  *  Pittsburgh PA 15213-3890
34  *
35  * any improvements or extensions that they make and grant Carnegie the
36  * rights to redistribute these changes.
37  */
38 
39 /*
40  *	Page fault handling module.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_pageout.h>
49 
50 /*
51  *	vm_fault:
52  *
53  *	Handle a page fault occuring at the given address,
54  *	requiring the given permissions, in the map specified.
55  *	If successful, the page is inserted into the
56  *	associated physical map.
57  *
58  *	NOTE: the given address should be truncated to the
59  *	proper page address.
60  *
61  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
62  *	a standard error specifying why the fault is fatal is returned.
63  *
64  *
65  *	The map in question must be referenced, and remains so.
66  *	Caller may hold no locks.
67  */
68 int
69 vm_fault(map, vaddr, fault_type, change_wiring)
70 	vm_map_t	map;
71 	vm_offset_t	vaddr;
72 	vm_prot_t	fault_type;
73 	boolean_t	change_wiring;
74 {
75 	vm_object_t		first_object;
76 	vm_offset_t		first_offset;
77 	vm_map_entry_t		entry;
78 	register vm_object_t	object;
79 	register vm_offset_t	offset;
80 	register vm_page_t	m;
81 	vm_page_t		first_m;
82 	vm_prot_t		prot;
83 	int			result;
84 	boolean_t		wired;
85 	boolean_t		su;
86 	boolean_t		lookup_still_valid;
87 	boolean_t		page_exists;
88 	vm_page_t		old_m;
89 	vm_object_t		next_object;
90 
91 	cnt.v_vm_faults++;		/* needs lock XXX */
92 /*
93  *	Recovery actions
94  */
95 #define	FREE_PAGE(m)	{				\
96 	PAGE_WAKEUP(m);					\
97 	vm_page_lock_queues();				\
98 	vm_page_free(m);				\
99 	vm_page_unlock_queues();			\
100 }
101 
102 #define	RELEASE_PAGE(m)	{				\
103 	PAGE_WAKEUP(m);					\
104 	vm_page_lock_queues();				\
105 	vm_page_activate(m);				\
106 	vm_page_unlock_queues();			\
107 }
108 
109 #define	UNLOCK_MAP	{				\
110 	if (lookup_still_valid) {			\
111 		vm_map_lookup_done(map, entry);		\
112 		lookup_still_valid = FALSE;		\
113 	}						\
114 }
115 
116 #define	UNLOCK_THINGS	{				\
117 	object->paging_in_progress--;			\
118 	vm_object_unlock(object);			\
119 	if (object != first_object) {			\
120 		vm_object_lock(first_object);		\
121 		FREE_PAGE(first_m);			\
122 		first_object->paging_in_progress--;	\
123 		vm_object_unlock(first_object);		\
124 	}						\
125 	UNLOCK_MAP;					\
126 }
127 
128 #define	UNLOCK_AND_DEALLOCATE	{			\
129 	UNLOCK_THINGS;					\
130 	vm_object_deallocate(first_object);		\
131 }
132 
133     RetryFault: ;
134 
135 	/*
136 	 *	Find the backing store object and offset into
137 	 *	it to begin the search.
138 	 */
139 
140 	if ((result = vm_map_lookup(&map, vaddr, fault_type, &entry,
141 			&first_object, &first_offset,
142 			&prot, &wired, &su)) != KERN_SUCCESS) {
143 		return(result);
144 	}
145 	lookup_still_valid = TRUE;
146 
147 	if (wired)
148 		fault_type = prot;
149 
150 	first_m = NULL;
151 
152    	/*
153 	 *	Make a reference to this object to
154 	 *	prevent its disposal while we are messing with
155 	 *	it.  Once we have the reference, the map is free
156 	 *	to be diddled.  Since objects reference their
157 	 *	shadows (and copies), they will stay around as well.
158 	 */
159 
160 	vm_object_lock(first_object);
161 
162 	first_object->ref_count++;
163 	first_object->paging_in_progress++;
164 
165 	/*
166 	 *	INVARIANTS (through entire routine):
167 	 *
168 	 *	1)	At all times, we must either have the object
169 	 *		lock or a busy page in some object to prevent
170 	 *		some other thread from trying to bring in
171 	 *		the same page.
172 	 *
173 	 *		Note that we cannot hold any locks during the
174 	 *		pager access or when waiting for memory, so
175 	 *		we use a busy page then.
176 	 *
177 	 *		Note also that we aren't as concerned about
178 	 *		more than one thead attempting to pager_data_unlock
179 	 *		the same page at once, so we don't hold the page
180 	 *		as busy then, but do record the highest unlock
181 	 *		value so far.  [Unlock requests may also be delivered
182 	 *		out of order.]
183 	 *
184 	 *	2)	Once we have a busy page, we must remove it from
185 	 *		the pageout queues, so that the pageout daemon
186 	 *		will not grab it away.
187 	 *
188 	 *	3)	To prevent another thread from racing us down the
189 	 *		shadow chain and entering a new page in the top
190 	 *		object before we do, we must keep a busy page in
191 	 *		the top object while following the shadow chain.
192 	 *
193 	 *	4)	We must increment paging_in_progress on any object
194 	 *		for which we have a busy page, to prevent
195 	 *		vm_object_collapse from removing the busy page
196 	 *		without our noticing.
197 	 */
198 
199 	/*
200 	 *	Search for the page at object/offset.
201 	 */
202 
203 	object = first_object;
204 	offset = first_offset;
205 
206 	/*
207 	 *	See whether this page is resident
208 	 */
209 
210 	while (TRUE) {
211 		m = vm_page_lookup(object, offset);
212 		if (m != NULL) {
213 			/*
214 			 *	If the page is being brought in,
215 			 *	wait for it and then retry.
216 			 */
217 			if (m->busy) {
218 #ifdef DOTHREADS
219 				int	wait_result;
220 
221 				PAGE_ASSERT_WAIT(m, !change_wiring);
222 				UNLOCK_THINGS;
223 				thread_block();
224 				wait_result = current_thread()->wait_result;
225 				vm_object_deallocate(first_object);
226 				if (wait_result != THREAD_AWAKENED)
227 					return(KERN_SUCCESS);
228 				goto RetryFault;
229 #else
230 				PAGE_ASSERT_WAIT(m, !change_wiring);
231 				UNLOCK_THINGS;
232 				thread_block();
233 				vm_object_deallocate(first_object);
234 				goto RetryFault;
235 #endif
236 			}
237 
238 			if (m->absent)
239 				panic("vm_fault: absent");
240 
241 			/*
242 			 *	If the desired access to this page has
243 			 *	been locked out, request that it be unlocked.
244 			 */
245 
246 			if (fault_type & m->page_lock) {
247 #ifdef DOTHREADS
248 				int	wait_result;
249 
250 				if ((fault_type & m->unlock_request) != fault_type)
251 					panic("vm_fault: pager_data_unlock");
252 
253 				PAGE_ASSERT_WAIT(m, !change_wiring);
254 				UNLOCK_THINGS;
255 				thread_block();
256 				wait_result = current_thread()->wait_result;
257 				vm_object_deallocate(first_object);
258 				if (wait_result != THREAD_AWAKENED)
259 					return(KERN_SUCCESS);
260 				goto RetryFault;
261 #else
262 				if ((fault_type & m->unlock_request) != fault_type)
263 					panic("vm_fault: pager_data_unlock");
264 
265 				PAGE_ASSERT_WAIT(m, !change_wiring);
266 				UNLOCK_THINGS;
267 				thread_block();
268 				vm_object_deallocate(first_object);
269 				goto RetryFault;
270 #endif
271 			}
272 
273 			/*
274 			 *	Remove the page from the pageout daemon's
275 			 *	reach while we play with it.
276 			 */
277 
278 			vm_page_lock_queues();
279 			if (m->inactive) {
280 				queue_remove(&vm_page_queue_inactive, m,
281 						vm_page_t, pageq);
282 				m->inactive = FALSE;
283 				cnt.v_inactive_count--;
284 				cnt.v_reactivated++;
285 			}
286 
287 			if (m->active) {
288 				queue_remove(&vm_page_queue_active, m,
289 						vm_page_t, pageq);
290 				m->active = FALSE;
291 				cnt.v_active_count--;
292 			}
293 			vm_page_unlock_queues();
294 
295 			/*
296 			 *	Mark page busy for other threads.
297 			 */
298 			m->busy = TRUE;
299 			m->absent = FALSE;
300 			break;
301 		}
302 
303 		if (((object->pager != NULL) &&
304 				(!change_wiring || wired))
305 		    || (object == first_object)) {
306 
307 			/*
308 			 *	Allocate a new page for this object/offset
309 			 *	pair.
310 			 */
311 
312 			m = vm_page_alloc(object, offset);
313 
314 			if (m == NULL) {
315 				UNLOCK_AND_DEALLOCATE;
316 				VM_WAIT;
317 				goto RetryFault;
318 			}
319 		}
320 
321 		if ((object->pager != NULL) &&
322 				(!change_wiring || wired)) {
323 			int rv;
324 
325 			/*
326 			 *	Now that we have a busy page, we can
327 			 *	release the object lock.
328 			 */
329 			vm_object_unlock(object);
330 
331 			/*
332 			 *	Call the pager to retrieve the data, if any,
333 			 *	after releasing the lock on the map.
334 			 */
335 			UNLOCK_MAP;
336 
337 			rv = vm_pager_get(object->pager, m, TRUE);
338 			if (rv == VM_PAGER_OK) {
339 				/*
340 				 *	Found the page.
341 				 *	Leave it busy while we play with it.
342 				 */
343 				vm_object_lock(object);
344 
345 				/*
346 				 *	Relookup in case pager changed page.
347 				 *	Pager is responsible for disposition
348 				 *	of old page if moved.
349 				 */
350 				m = vm_page_lookup(object, offset);
351 
352 				cnt.v_pageins++;
353 				m->fake = FALSE;
354 				m->clean = TRUE;
355 				pmap_clear_modify(VM_PAGE_TO_PHYS(m));
356 				break;
357 			}
358 
359 			/*
360 			 *	Remove the bogus page (which does not
361 			 *	exist at this object/offset); before
362 			 *	doing so, we must get back our object
363 			 *	lock to preserve our invariant.
364 			 *
365 			 *	Also wake up any other thread that may want
366 			 *	to bring in this page.
367 			 *
368 			 *	If this is the top-level object, we must
369 			 *	leave the busy page to prevent another
370 			 *	thread from rushing past us, and inserting
371 			 *	the page in that object at the same time
372 			 *	that we are.
373 			 */
374 
375 			vm_object_lock(object);
376 			/*
377 			 * Data outside the range of the pager; an error
378 			 */
379 			if (rv == VM_PAGER_BAD) {
380 				FREE_PAGE(m);
381 				UNLOCK_AND_DEALLOCATE;
382 				return(KERN_PROTECTION_FAILURE); /* XXX */
383 			}
384 			if (object != first_object) {
385 				FREE_PAGE(m);
386 				/* note that `m' is not used after this */
387 			}
388 		}
389 
390 		/*
391 		 * We get here if the object has no pager (or unwiring)
392 		 * or the pager doesn't have the page.
393 		 */
394 		if (object == first_object)
395 			first_m = m;
396 
397 		/*
398 		 *	Move on to the next object.  Lock the next
399 		 *	object before unlocking the current one.
400 		 */
401 
402 		offset += object->shadow_offset;
403 		next_object = object->shadow;
404 		if (next_object == NULL) {
405 			/*
406 			 *	If there's no object left, fill the page
407 			 *	in the top object with zeros.
408 			 */
409 			if (object != first_object) {
410 				object->paging_in_progress--;
411 				vm_object_unlock(object);
412 
413 				object = first_object;
414 				offset = first_offset;
415 				m = first_m;
416 				vm_object_lock(object);
417 			}
418 			first_m = NULL;
419 
420 			vm_page_zero_fill(m);
421 			cnt.v_zfod++;
422 			m->fake = FALSE;
423 			m->absent = FALSE;
424 			break;
425 		}
426 		else {
427 			vm_object_lock(next_object);
428 			if (object != first_object)
429 				object->paging_in_progress--;
430 			vm_object_unlock(object);
431 			object = next_object;
432 			object->paging_in_progress++;
433 		}
434 	}
435 
436 	if (m->absent || m->active || m->inactive || !m->busy)
437 		panic("vm_fault: absent or active or inactive or not busy after main loop");
438 
439 	/*
440 	 *	PAGE HAS BEEN FOUND.
441 	 *	[Loop invariant still holds -- the object lock
442 	 *	is held.]
443 	 */
444 
445 	old_m = m;	/* save page that would be copied */
446 
447 	/*
448 	 *	If the page is being written, but isn't
449 	 *	already owned by the top-level object,
450 	 *	we have to copy it into a new page owned
451 	 *	by the top-level object.
452 	 */
453 
454 	if (object != first_object) {
455 	    	/*
456 		 *	We only really need to copy if we
457 		 *	want to write it.
458 		 */
459 
460 	    	if (fault_type & VM_PROT_WRITE) {
461 
462 			/*
463 			 *	If we try to collapse first_object at this
464 			 *	point, we may deadlock when we try to get
465 			 *	the lock on an intermediate object (since we
466 			 *	have the bottom object locked).  We can't
467 			 *	unlock the bottom object, because the page
468 			 *	we found may move (by collapse) if we do.
469 			 *
470 			 *	Instead, we first copy the page.  Then, when
471 			 *	we have no more use for the bottom object,
472 			 *	we unlock it and try to collapse.
473 			 *
474 			 *	Note that we copy the page even if we didn't
475 			 *	need to... that's the breaks.
476 			 */
477 
478 		    	/*
479 			 *	We already have an empty page in
480 			 *	first_object - use it.
481 			 */
482 
483 			vm_page_copy(m, first_m);
484 			first_m->fake = FALSE;
485 			first_m->absent = FALSE;
486 
487 			/*
488 			 *	If another map is truly sharing this
489 			 *	page with us, we have to flush all
490 			 *	uses of the original page, since we
491 			 *	can't distinguish those which want the
492 			 *	original from those which need the
493 			 *	new copy.
494 			 *
495 			 *	XXX If we know that only one map has
496 			 *	access to this page, then we could
497 			 *	avoid the pmap_page_protect() call.
498 			 */
499 
500 			vm_page_lock_queues();
501 			vm_page_activate(m);
502 			pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_NONE);
503 			vm_page_unlock_queues();
504 
505 			/*
506 			 *	We no longer need the old page or object.
507 			 */
508 			PAGE_WAKEUP(m);
509 			object->paging_in_progress--;
510 			vm_object_unlock(object);
511 
512 			/*
513 			 *	Only use the new page below...
514 			 */
515 
516 			cnt.v_cow_faults++;
517 			m = first_m;
518 			object = first_object;
519 			offset = first_offset;
520 
521 			/*
522 			 *	Now that we've gotten the copy out of the
523 			 *	way, let's try to collapse the top object.
524 			 */
525 			vm_object_lock(object);
526 			/*
527 			 *	But we have to play ugly games with
528 			 *	paging_in_progress to do that...
529 			 */
530 			object->paging_in_progress--;
531 			vm_object_collapse(object);
532 			object->paging_in_progress++;
533 		}
534 		else {
535 		    	prot &= (~VM_PROT_WRITE);
536 			m->copy_on_write = TRUE;
537 		}
538 	}
539 
540 	if (m->active || m->inactive)
541 		panic("vm_fault: active or inactive before copy object handling");
542 
543 	/*
544 	 *	If the page is being written, but hasn't been
545 	 *	copied to the copy-object, we have to copy it there.
546 	 */
547     RetryCopy:
548 	if (first_object->copy != NULL) {
549 		vm_object_t copy_object = first_object->copy;
550 		vm_offset_t copy_offset;
551 		vm_page_t copy_m;
552 
553 		/*
554 		 *	We only need to copy if we want to write it.
555 		 */
556 		if ((fault_type & VM_PROT_WRITE) == 0) {
557 			prot &= ~VM_PROT_WRITE;
558 			m->copy_on_write = TRUE;
559 		}
560 		else {
561 			/*
562 			 *	Try to get the lock on the copy_object.
563 			 */
564 			if (!vm_object_lock_try(copy_object)) {
565 				vm_object_unlock(object);
566 				/* should spin a bit here... */
567 				vm_object_lock(object);
568 				goto RetryCopy;
569 			}
570 
571 			/*
572 			 *	Make another reference to the copy-object,
573 			 *	to keep it from disappearing during the
574 			 *	copy.
575 			 */
576 			copy_object->ref_count++;
577 
578 			/*
579 			 *	Does the page exist in the copy?
580 			 */
581 			copy_offset = first_offset
582 				- copy_object->shadow_offset;
583 			copy_m = vm_page_lookup(copy_object, copy_offset);
584 			if (page_exists = (copy_m != NULL)) {
585 				if (copy_m->busy) {
586 #ifdef DOTHREADS
587 					int	wait_result;
588 
589 					/*
590 					 *	If the page is being brought
591 					 *	in, wait for it and then retry.
592 					 */
593 					PAGE_ASSERT_WAIT(copy_m, !change_wiring);
594 					RELEASE_PAGE(m);
595 					copy_object->ref_count--;
596 					vm_object_unlock(copy_object);
597 					UNLOCK_THINGS;
598 					thread_block();
599 					wait_result = current_thread()->wait_result;
600 					vm_object_deallocate(first_object);
601 					if (wait_result != THREAD_AWAKENED)
602 						return(KERN_SUCCESS);
603 					goto RetryFault;
604 #else
605 					/*
606 					 *	If the page is being brought
607 					 *	in, wait for it and then retry.
608 					 */
609 					PAGE_ASSERT_WAIT(copy_m, !change_wiring);
610 					RELEASE_PAGE(m);
611 					copy_object->ref_count--;
612 					vm_object_unlock(copy_object);
613 					UNLOCK_THINGS;
614 					thread_block();
615 					vm_object_deallocate(first_object);
616 					goto RetryFault;
617 #endif
618 				}
619 			}
620 
621 			/*
622 			 *	If the page is not in memory (in the object)
623 			 *	and the object has a pager, we have to check
624 			 *	if the pager has the data in secondary
625 			 *	storage.
626 			 */
627 			if (!page_exists) {
628 
629 				/*
630 				 *	If we don't allocate a (blank) page
631 				 *	here... another thread could try
632 				 *	to page it in, allocate a page, and
633 				 *	then block on the busy page in its
634 				 *	shadow (first_object).  Then we'd
635 				 *	trip over the busy page after we
636 				 *	found that the copy_object's pager
637 				 *	doesn't have the page...
638 				 */
639 				copy_m = vm_page_alloc(copy_object,
640 								copy_offset);
641 				if (copy_m == NULL) {
642 					/*
643 					 *	Wait for a page, then retry.
644 					 */
645 					RELEASE_PAGE(m);
646 					copy_object->ref_count--;
647 					vm_object_unlock(copy_object);
648 					UNLOCK_AND_DEALLOCATE;
649 					VM_WAIT;
650 					goto RetryFault;
651 				}
652 
653 			 	if (copy_object->pager != NULL) {
654 					vm_object_unlock(object);
655 					vm_object_unlock(copy_object);
656 					UNLOCK_MAP;
657 
658 					page_exists = vm_pager_has_page(
659 							copy_object->pager,
660 							(copy_offset + copy_object->paging_offset));
661 
662 					vm_object_lock(copy_object);
663 
664 					/*
665 					 * Since the map is unlocked, someone
666 					 * else could have copied this object
667 					 * and put a different copy_object
668 					 * between the two.  Or, the last
669 					 * reference to the copy-object (other
670 					 * than the one we have) may have
671 					 * disappeared - if that has happened,
672 					 * we don't need to make the copy.
673 					 */
674 					if (copy_object->shadow != object ||
675 					    copy_object->ref_count == 1) {
676 						/*
677 						 *	Gaah... start over!
678 						 */
679 						FREE_PAGE(copy_m);
680 						vm_object_unlock(copy_object);
681 						vm_object_deallocate(copy_object);
682 							/* may block */
683 						vm_object_lock(object);
684 						goto RetryCopy;
685 					}
686 					vm_object_lock(object);
687 
688 					if (page_exists) {
689 						/*
690 						 *	We didn't need the page
691 						 */
692 						FREE_PAGE(copy_m);
693 					}
694 				}
695 			}
696 			if (!page_exists) {
697 				/*
698 				 *	Must copy page into copy-object.
699 				 */
700 				vm_page_copy(m, copy_m);
701 				copy_m->fake = FALSE;
702 				copy_m->absent = FALSE;
703 
704 				/*
705 				 * Things to remember:
706 				 * 1. The copied page must be marked 'dirty'
707 				 *    so it will be paged out to the copy
708 				 *    object.
709 				 * 2. If the old page was in use by any users
710 				 *    of the copy-object, it must be removed
711 				 *    from all pmaps.  (We can't know which
712 				 *    pmaps use it.)
713 				 */
714 				vm_page_lock_queues();
715 				pmap_page_protect(VM_PAGE_TO_PHYS(old_m),
716 						  VM_PROT_NONE);
717 				copy_m->clean = FALSE;
718 				vm_page_activate(copy_m);	/* XXX */
719 				vm_page_unlock_queues();
720 
721 				PAGE_WAKEUP(copy_m);
722 			}
723 			/*
724 			 *	The reference count on copy_object must be
725 			 *	at least 2: one for our extra reference,
726 			 *	and at least one from the outside world
727 			 *	(we checked that when we last locked
728 			 *	copy_object).
729 			 */
730 			copy_object->ref_count--;
731 			vm_object_unlock(copy_object);
732 			m->copy_on_write = FALSE;
733 		}
734 	}
735 
736 	if (m->active || m->inactive)
737 		panic("vm_fault: active or inactive before retrying lookup");
738 
739 	/*
740 	 *	We must verify that the maps have not changed
741 	 *	since our last lookup.
742 	 */
743 
744 	if (!lookup_still_valid) {
745 		vm_object_t	retry_object;
746 		vm_offset_t	retry_offset;
747 		vm_prot_t	retry_prot;
748 
749 		/*
750 		 *	Since map entries may be pageable, make sure we can
751 		 *	take a page fault on them.
752 		 */
753 		vm_object_unlock(object);
754 
755 		/*
756 		 *	To avoid trying to write_lock the map while another
757 		 *	thread has it read_locked (in vm_map_pageable), we
758 		 *	do not try for write permission.  If the page is
759 		 *	still writable, we will get write permission.  If it
760 		 *	is not, or has been marked needs_copy, we enter the
761 		 *	mapping without write permission, and will merely
762 		 *	take another fault.
763 		 */
764 		result = vm_map_lookup(&map, vaddr,
765 				fault_type & ~VM_PROT_WRITE, &entry,
766 				&retry_object, &retry_offset, &retry_prot,
767 				&wired, &su);
768 
769 		vm_object_lock(object);
770 
771 		/*
772 		 *	If we don't need the page any longer, put it on the
773 		 *	active list (the easiest thing to do here).  If no
774 		 *	one needs it, pageout will grab it eventually.
775 		 */
776 
777 		if (result != KERN_SUCCESS) {
778 			RELEASE_PAGE(m);
779 			UNLOCK_AND_DEALLOCATE;
780 			return(result);
781 		}
782 
783 		lookup_still_valid = TRUE;
784 
785 		if ((retry_object != first_object) ||
786 				(retry_offset != first_offset)) {
787 			RELEASE_PAGE(m);
788 			UNLOCK_AND_DEALLOCATE;
789 			goto RetryFault;
790 		}
791 
792 		/*
793 		 *	Check whether the protection has changed or the object
794 		 *	has been copied while we left the map unlocked.
795 		 *	Changing from read to write permission is OK - we leave
796 		 *	the page write-protected, and catch the write fault.
797 		 *	Changing from write to read permission means that we
798 		 *	can't mark the page write-enabled after all.
799 		 */
800 		prot &= retry_prot;
801 		if (m->copy_on_write)
802 			prot &= ~VM_PROT_WRITE;
803 	}
804 
805 	/*
806 	 * (the various bits we're fiddling with here are locked by
807 	 * the object's lock)
808 	 */
809 
810 	/* XXX This distorts the meaning of the copy_on_write bit */
811 
812 	if (prot & VM_PROT_WRITE)
813 		m->copy_on_write = FALSE;
814 
815 	/*
816 	 *	It's critically important that a wired-down page be faulted
817 	 *	only once in each map for which it is wired.
818 	 */
819 
820 	if (m->active || m->inactive)
821 		panic("vm_fault: active or inactive before pmap_enter");
822 
823 	vm_object_unlock(object);
824 
825 	/*
826 	 *	Put this page into the physical map.
827 	 *	We had to do the unlock above because pmap_enter
828 	 *	may cause other faults.   We don't put the
829 	 *	page back on the active queue until later so
830 	 *	that the page-out daemon won't find us (yet).
831 	 */
832 
833 	pmap_enter(map->pmap, vaddr, VM_PAGE_TO_PHYS(m),
834 			prot & ~(m->page_lock), wired);
835 
836 	/*
837 	 *	If the page is not wired down, then put it where the
838 	 *	pageout daemon can find it.
839 	 */
840 	vm_object_lock(object);
841 	vm_page_lock_queues();
842 	if (change_wiring) {
843 		if (wired)
844 			vm_page_wire(m);
845 		else
846 			vm_page_unwire(m);
847 	}
848 	else
849 		vm_page_activate(m);
850 	vm_page_unlock_queues();
851 
852 	/*
853 	 *	Unlock everything, and return
854 	 */
855 
856 	PAGE_WAKEUP(m);
857 	UNLOCK_AND_DEALLOCATE;
858 
859 	return(KERN_SUCCESS);
860 
861 }
862 
863 /*
864  *	vm_fault_wire:
865  *
866  *	Wire down a range of virtual addresses in a map.
867  */
868 void vm_fault_wire(map, start, end)
869 	vm_map_t	map;
870 	vm_offset_t	start, end;
871 {
872 
873 	register vm_offset_t	va;
874 	register pmap_t		pmap;
875 
876 	pmap = vm_map_pmap(map);
877 
878 	/*
879 	 *	Inform the physical mapping system that the
880 	 *	range of addresses may not fault, so that
881 	 *	page tables and such can be locked down as well.
882 	 */
883 
884 	pmap_pageable(pmap, start, end, FALSE);
885 
886 	/*
887 	 *	We simulate a fault to get the page and enter it
888 	 *	in the physical map.
889 	 */
890 
891 	for (va = start; va < end; va += PAGE_SIZE) {
892 		(void) vm_fault(map, va, VM_PROT_NONE, TRUE);
893 	}
894 }
895 
896 
897 /*
898  *	vm_fault_unwire:
899  *
900  *	Unwire a range of virtual addresses in a map.
901  */
902 void vm_fault_unwire(map, start, end)
903 	vm_map_t	map;
904 	vm_offset_t	start, end;
905 {
906 
907 	register vm_offset_t	va, pa;
908 	register pmap_t		pmap;
909 
910 	pmap = vm_map_pmap(map);
911 
912 	/*
913 	 *	Since the pages are wired down, we must be able to
914 	 *	get their mappings from the physical map system.
915 	 */
916 
917 	vm_page_lock_queues();
918 
919 	for (va = start; va < end; va += PAGE_SIZE) {
920 		pa = pmap_extract(pmap, va);
921 		if (pa == (vm_offset_t) 0) {
922 			panic("unwire: page not in pmap");
923 		}
924 		pmap_change_wiring(pmap, va, FALSE);
925 		vm_page_unwire(PHYS_TO_VM_PAGE(pa));
926 	}
927 	vm_page_unlock_queues();
928 
929 	/*
930 	 *	Inform the physical mapping system that the range
931 	 *	of addresses may fault, so that page tables and
932 	 *	such may be unwired themselves.
933 	 */
934 
935 	pmap_pageable(pmap, start, end, TRUE);
936 
937 }
938 
939 /*
940  *	Routine:
941  *		vm_fault_copy_entry
942  *	Function:
943  *		Copy all of the pages from a wired-down map entry to another.
944  *
945  *	In/out conditions:
946  *		The source and destination maps must be locked for write.
947  *		The source map entry must be wired down (or be a sharing map
948  *		entry corresponding to a main map entry that is wired down).
949  */
950 
951 void vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
952 	vm_map_t	dst_map;
953 	vm_map_t	src_map;
954 	vm_map_entry_t	dst_entry;
955 	vm_map_entry_t	src_entry;
956 {
957 
958 	vm_object_t	dst_object;
959 	vm_object_t	src_object;
960 	vm_offset_t	dst_offset;
961 	vm_offset_t	src_offset;
962 	vm_prot_t	prot;
963 	vm_offset_t	vaddr;
964 	vm_page_t	dst_m;
965 	vm_page_t	src_m;
966 
967 #ifdef	lint
968 	src_map++;
969 #endif	lint
970 
971 	src_object = src_entry->object.vm_object;
972 	src_offset = src_entry->offset;
973 
974 	/*
975 	 *	Create the top-level object for the destination entry.
976 	 *	(Doesn't actually shadow anything - we copy the pages
977 	 *	directly.)
978 	 */
979 	dst_object = vm_object_allocate(
980 			(vm_size_t) (dst_entry->end - dst_entry->start));
981 
982 	dst_entry->object.vm_object = dst_object;
983 	dst_entry->offset = 0;
984 
985 	prot  = dst_entry->max_protection;
986 
987 	/*
988 	 *	Loop through all of the pages in the entry's range, copying
989 	 *	each one from the source object (it should be there) to the
990 	 *	destination object.
991 	 */
992 	for (vaddr = dst_entry->start, dst_offset = 0;
993 	     vaddr < dst_entry->end;
994 	     vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
995 
996 		/*
997 		 *	Allocate a page in the destination object
998 		 */
999 		vm_object_lock(dst_object);
1000 		do {
1001 			dst_m = vm_page_alloc(dst_object, dst_offset);
1002 			if (dst_m == NULL) {
1003 				vm_object_unlock(dst_object);
1004 				VM_WAIT;
1005 				vm_object_lock(dst_object);
1006 			}
1007 		} while (dst_m == NULL);
1008 
1009 		/*
1010 		 *	Find the page in the source object, and copy it in.
1011 		 *	(Because the source is wired down, the page will be
1012 		 *	in memory.)
1013 		 */
1014 		vm_object_lock(src_object);
1015 		src_m = vm_page_lookup(src_object, dst_offset + src_offset);
1016 		if (src_m == NULL)
1017 			panic("vm_fault_copy_wired: page missing");
1018 
1019 		vm_page_copy(src_m, dst_m);
1020 
1021 		/*
1022 		 *	Enter it in the pmap...
1023 		 */
1024 		vm_object_unlock(src_object);
1025 		vm_object_unlock(dst_object);
1026 
1027 		pmap_enter(dst_map->pmap, vaddr, VM_PAGE_TO_PHYS(dst_m),
1028 				prot, FALSE);
1029 
1030 		/*
1031 		 *	Mark it no longer busy, and put it on the active list.
1032 		 */
1033 		vm_object_lock(dst_object);
1034 		vm_page_lock_queues();
1035 		vm_page_activate(dst_m);
1036 		vm_page_unlock_queues();
1037 		PAGE_WAKEUP(dst_m);
1038 		vm_object_unlock(dst_object);
1039 	}
1040 
1041 }
1042