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