xref: /freebsd/sys/vm/vm_fault.c (revision 0ea3482342b4d7d6e71f3007ce4dafe445c639fd)
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.35 1995/11/02 06:42:47 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 				hardfault++;
361 				break;
362 			}
363 			/*
364 			 * Remove the bogus page (which does not exist at this
365 			 * object/offset); before doing so, we must get back
366 			 * our object lock to preserve our invariant.
367 			 *
368 			 * Also wake up any other process that may want to bring
369 			 * in this page.
370 			 *
371 			 * If this is the top-level object, we must leave the
372 			 * busy page to prevent another process from rushing
373 			 * past us, and inserting the page in that object at
374 			 * the same time that we are.
375 			 */
376 
377 			if (rv == VM_PAGER_ERROR)
378 				printf("vm_fault: pager input (probably hardware) error, PID %d failure\n",
379 				    curproc->p_pid);
380 			/*
381 			 * Data outside the range of the pager or an I/O error
382 			 */
383 			/*
384 			 * XXX - the check for kernel_map is a kludge to work
385 			 * around having the machine panic on a kernel space
386 			 * fault w/ I/O error.
387 			 */
388 			if (((map != kernel_map) && (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
389 				FREE_PAGE(m);
390 				UNLOCK_AND_DEALLOCATE;
391 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
392 			}
393 			if (object != first_object) {
394 				FREE_PAGE(m);
395 				/*
396 				 * XXX - we cannot just fall out at this
397 				 * point, m has been freed and is invalid!
398 				 */
399 			}
400 		}
401 		/*
402 		 * We get here if the object has default pager (or unwiring) or the
403 		 * pager doesn't have the page.
404 		 */
405 		if (object == first_object)
406 			first_m = m;
407 
408 		/*
409 		 * Move on to the next object.  Lock the next object before
410 		 * unlocking the current one.
411 		 */
412 
413 		offset += object->backing_object_offset;
414 		next_object = object->backing_object;
415 		if (next_object == NULL) {
416 			/*
417 			 * If there's no object left, fill the page in the top
418 			 * object with zeros.
419 			 */
420 			if (object != first_object) {
421 				vm_object_pip_wakeup(object);
422 
423 				object = first_object;
424 				offset = first_offset;
425 				m = first_m;
426 			}
427 			first_m = NULL;
428 
429 			if ((m->flags & PG_ZERO) == 0)
430 				vm_page_zero_fill(m);
431 			m->valid = VM_PAGE_BITS_ALL;
432 			cnt.v_zfod++;
433 			break;
434 		} else {
435 			if (object != first_object) {
436 				vm_object_pip_wakeup(object);
437 			}
438 			object = next_object;
439 			object->paging_in_progress++;
440 		}
441 	}
442 
443 	if ((m->flags & PG_BUSY) == 0)
444 		panic("vm_fault: not busy after main loop");
445 
446 	/*
447 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
448 	 * is held.]
449 	 */
450 
451 	old_m = m;	/* save page that would be copied */
452 
453 	/*
454 	 * If the page is being written, but isn't already owned by the
455 	 * top-level object, we have to copy it into a new page owned by the
456 	 * top-level object.
457 	 */
458 
459 	if (object != first_object) {
460 		/*
461 		 * We only really need to copy if we want to write it.
462 		 */
463 
464 		if (fault_type & VM_PROT_WRITE) {
465 
466 			/*
467 			 * If we try to collapse first_object at this point,
468 			 * we may deadlock when we try to get the lock on an
469 			 * intermediate object (since we have the bottom
470 			 * object locked).  We can't unlock the bottom object,
471 			 * because the page we found may move (by collapse) if
472 			 * we do.
473 			 *
474 			 * Instead, we first copy the page.  Then, when we have
475 			 * no more use for the bottom object, we unlock it and
476 			 * try to collapse.
477 			 *
478 			 * Note that we copy the page even if we didn't need
479 			 * to... that's the breaks.
480 			 */
481 
482 			/*
483 			 * We already have an empty page in first_object - use
484 			 * it.
485 			 */
486 
487 			vm_page_copy(m, first_m);
488 			first_m->valid = VM_PAGE_BITS_ALL;
489 
490 			/*
491 			 * If another map is truly sharing this page with us,
492 			 * we have to flush all uses of the original page,
493 			 * since we can't distinguish those which want the
494 			 * original from those which need the new copy.
495 			 *
496 			 * XXX If we know that only one map has access to this
497 			 * page, then we could avoid the pmap_page_protect()
498 			 * call.
499 			 */
500 
501 			if ((m->flags & PG_ACTIVE) == 0)
502 				vm_page_activate(m);
503 			vm_page_protect(m, VM_PROT_NONE);
504 
505 			/*
506 			 * We no longer need the old page or object.
507 			 */
508 			PAGE_WAKEUP(m);
509 			vm_object_pip_wakeup(object);
510 
511 			/*
512 			 * Only use the new page below...
513 			 */
514 
515 			cnt.v_cow_faults++;
516 			m = first_m;
517 			object = first_object;
518 			offset = first_offset;
519 
520 			/*
521 			 * Now that we've gotten the copy out of the way,
522 			 * let's try to collapse the top object.
523 			 *
524 			 * But we have to play ugly games with
525 			 * paging_in_progress to do that...
526 			 */
527 			vm_object_pip_wakeup(object);
528 			vm_object_collapse(object);
529 			object->paging_in_progress++;
530 		} else {
531 			prot &= ~VM_PROT_WRITE;
532 		}
533 	}
534 
535 	/*
536 	 * We must verify that the maps have not changed since our last
537 	 * lookup.
538 	 */
539 
540 	if (!lookup_still_valid) {
541 		vm_object_t retry_object;
542 		vm_offset_t retry_offset;
543 		vm_prot_t retry_prot;
544 
545 		/*
546 		 * Since map entries may be pageable, make sure we can take a
547 		 * page fault on them.
548 		 */
549 
550 		/*
551 		 * To avoid trying to write_lock the map while another process
552 		 * has it read_locked (in vm_map_pageable), we do not try for
553 		 * write permission.  If the page is still writable, we will
554 		 * get write permission.  If it is not, or has been marked
555 		 * needs_copy, we enter the mapping without write permission,
556 		 * and will merely take another fault.
557 		 */
558 		result = vm_map_lookup(&map, vaddr, fault_type & ~VM_PROT_WRITE,
559 		    &entry, &retry_object, &retry_offset, &retry_prot, &wired, &su);
560 
561 		/*
562 		 * If we don't need the page any longer, put it on the active
563 		 * list (the easiest thing to do here).  If no one needs it,
564 		 * pageout will grab it eventually.
565 		 */
566 
567 		if (result != KERN_SUCCESS) {
568 			RELEASE_PAGE(m);
569 			UNLOCK_AND_DEALLOCATE;
570 			return (result);
571 		}
572 		lookup_still_valid = TRUE;
573 
574 		if ((retry_object != first_object) ||
575 		    (retry_offset != first_offset)) {
576 			RELEASE_PAGE(m);
577 			UNLOCK_AND_DEALLOCATE;
578 			goto RetryFault;
579 		}
580 		/*
581 		 * Check whether the protection has changed or the object has
582 		 * been copied while we left the map unlocked. Changing from
583 		 * read to write permission is OK - we leave the page
584 		 * write-protected, and catch the write fault. Changing from
585 		 * write to read permission means that we can't mark the page
586 		 * write-enabled after all.
587 		 */
588 		prot &= retry_prot;
589 	}
590 	/*
591 	 * (the various bits we're fiddling with here are locked by the
592 	 * object's lock)
593 	 */
594 
595 	/*
596 	 * It's critically important that a wired-down page be faulted only
597 	 * once in each map for which it is wired.
598 	 */
599 
600 	/*
601 	 * Put this page into the physical map. We had to do the unlock above
602 	 * because pmap_enter may cause other faults.   We don't put the page
603 	 * back on the active queue until later so that the page-out daemon
604 	 * won't find us (yet).
605 	 */
606 
607 	if (prot & VM_PROT_WRITE) {
608 		m->flags |= PG_WRITEABLE;
609 		m->object->flags |= OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY;
610 		/*
611 		 * If the fault is a write, we know that this page is being
612 		 * written NOW. This will save on the pmap_is_modified() calls
613 		 * later.
614 		 */
615 		if (fault_type & VM_PROT_WRITE) {
616 			m->dirty = VM_PAGE_BITS_ALL;
617 		}
618 	}
619 
620 	m->flags |= PG_MAPPED|PG_REFERENCED;
621 	m->flags &= ~PG_ZERO;
622 
623 	pmap_enter(map->pmap, vaddr, VM_PAGE_TO_PHYS(m), prot, wired);
624 #if 0
625 	if (change_wiring == 0 && wired == 0)
626 		pmap_prefault(map->pmap, vaddr, entry, first_object);
627 #endif
628 
629 	/*
630 	 * If the page is not wired down, then put it where the pageout daemon
631 	 * can find it.
632 	 */
633 	if (change_wiring) {
634 		if (wired)
635 			vm_page_wire(m);
636 		else
637 			vm_page_unwire(m);
638 	} else {
639 		if ((m->flags & PG_ACTIVE) == 0)
640 			vm_page_activate(m);
641 	}
642 
643 	if (curproc && (curproc->p_flag & P_INMEM) && curproc->p_stats) {
644 		if (hardfault) {
645 			curproc->p_stats->p_ru.ru_majflt++;
646 		} else {
647 			curproc->p_stats->p_ru.ru_minflt++;
648 		}
649 	}
650 
651 	if ((m->flags & PG_BUSY) == 0)
652 		printf("page not busy: %d\n", m->offset);
653 	/*
654 	 * Unlock everything, and return
655 	 */
656 
657 	PAGE_WAKEUP(m);
658 	UNLOCK_AND_DEALLOCATE;
659 
660 	return (KERN_SUCCESS);
661 
662 }
663 
664 /*
665  *	vm_fault_wire:
666  *
667  *	Wire down a range of virtual addresses in a map.
668  */
669 int
670 vm_fault_wire(map, start, end)
671 	vm_map_t map;
672 	vm_offset_t start, end;
673 {
674 
675 	register vm_offset_t va;
676 	register pmap_t pmap;
677 	int rv;
678 
679 	pmap = vm_map_pmap(map);
680 
681 	/*
682 	 * Inform the physical mapping system that the range of addresses may
683 	 * not fault, so that page tables and such can be locked down as well.
684 	 */
685 
686 	pmap_pageable(pmap, start, end, FALSE);
687 
688 	/*
689 	 * We simulate a fault to get the page and enter it in the physical
690 	 * map.
691 	 */
692 
693 	for (va = start; va < end; va += PAGE_SIZE) {
694 
695 		while( curproc != pageproc &&
696 			(cnt.v_free_count <= cnt.v_pageout_free_min))
697 			VM_WAIT;
698 
699 		rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE, TRUE);
700 		if (rv) {
701 			if (va != start)
702 				vm_fault_unwire(map, start, va);
703 			return (rv);
704 		}
705 	}
706 	return (KERN_SUCCESS);
707 }
708 
709 
710 /*
711  *	vm_fault_unwire:
712  *
713  *	Unwire a range of virtual addresses in a map.
714  */
715 void
716 vm_fault_unwire(map, start, end)
717 	vm_map_t map;
718 	vm_offset_t start, end;
719 {
720 
721 	register vm_offset_t va, pa;
722 	register pmap_t pmap;
723 
724 	pmap = vm_map_pmap(map);
725 
726 	/*
727 	 * Since the pages are wired down, we must be able to get their
728 	 * mappings from the physical map system.
729 	 */
730 
731 	for (va = start; va < end; va += PAGE_SIZE) {
732 		pa = pmap_extract(pmap, va);
733 		if (pa == (vm_offset_t) 0) {
734 			panic("unwire: page not in pmap");
735 		}
736 		pmap_change_wiring(pmap, va, FALSE);
737 		vm_page_unwire(PHYS_TO_VM_PAGE(pa));
738 	}
739 
740 	/*
741 	 * Inform the physical mapping system that the range of addresses may
742 	 * fault, so that page tables and such may be unwired themselves.
743 	 */
744 
745 	pmap_pageable(pmap, start, end, TRUE);
746 
747 }
748 
749 /*
750  *	Routine:
751  *		vm_fault_copy_entry
752  *	Function:
753  *		Copy all of the pages from a wired-down map entry to another.
754  *
755  *	In/out conditions:
756  *		The source and destination maps must be locked for write.
757  *		The source map entry must be wired down (or be a sharing map
758  *		entry corresponding to a main map entry that is wired down).
759  */
760 
761 void
762 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
763 	vm_map_t dst_map;
764 	vm_map_t src_map;
765 	vm_map_entry_t dst_entry;
766 	vm_map_entry_t src_entry;
767 {
768 	vm_object_t dst_object;
769 	vm_object_t src_object;
770 	vm_offset_t dst_offset;
771 	vm_offset_t src_offset;
772 	vm_prot_t prot;
773 	vm_offset_t vaddr;
774 	vm_page_t dst_m;
775 	vm_page_t src_m;
776 
777 #ifdef	lint
778 	src_map++;
779 #endif	/* lint */
780 
781 	src_object = src_entry->object.vm_object;
782 	src_offset = src_entry->offset;
783 
784 	/*
785 	 * Create the top-level object for the destination entry. (Doesn't
786 	 * actually shadow anything - we copy the pages directly.)
787 	 */
788 	dst_object = vm_object_allocate(OBJT_DEFAULT,
789 	    (vm_size_t) (dst_entry->end - dst_entry->start));
790 
791 	dst_entry->object.vm_object = dst_object;
792 	dst_entry->offset = 0;
793 
794 	prot = dst_entry->max_protection;
795 
796 	/*
797 	 * Loop through all of the pages in the entry's range, copying each
798 	 * one from the source object (it should be there) to the destination
799 	 * object.
800 	 */
801 	for (vaddr = dst_entry->start, dst_offset = 0;
802 	    vaddr < dst_entry->end;
803 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
804 
805 		/*
806 		 * Allocate a page in the destination object
807 		 */
808 		do {
809 			dst_m = vm_page_alloc(dst_object, dst_offset, VM_ALLOC_NORMAL);
810 			if (dst_m == NULL) {
811 				VM_WAIT;
812 			}
813 		} while (dst_m == NULL);
814 
815 		/*
816 		 * Find the page in the source object, and copy it in.
817 		 * (Because the source is wired down, the page will be in
818 		 * memory.)
819 		 */
820 		src_m = vm_page_lookup(src_object, dst_offset + src_offset);
821 		if (src_m == NULL)
822 			panic("vm_fault_copy_wired: page missing");
823 
824 		vm_page_copy(src_m, dst_m);
825 
826 		/*
827 		 * Enter it in the pmap...
828 		 */
829 
830 		dst_m->flags |= PG_WRITEABLE|PG_MAPPED;
831 		dst_m->flags &= ~PG_ZERO;
832 		pmap_enter(dst_map->pmap, vaddr, VM_PAGE_TO_PHYS(dst_m),
833 		    prot, FALSE);
834 
835 		/*
836 		 * Mark it no longer busy, and put it on the active list.
837 		 */
838 		vm_page_activate(dst_m);
839 		PAGE_WAKEUP(dst_m);
840 	}
841 }
842 
843 
844 /*
845  * This routine checks around the requested page for other pages that
846  * might be able to be faulted in.  This routine brackets the viable
847  * pages for the pages to be paged in.
848  *
849  * Inputs:
850  *	m, rbehind, rahead
851  *
852  * Outputs:
853  *  marray (array of vm_page_t), reqpage (index of requested page)
854  *
855  * Return value:
856  *  number of pages in marray
857  */
858 int
859 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
860 	vm_page_t m;
861 	int rbehind;
862 	int rahead;
863 	vm_page_t *marray;
864 	int *reqpage;
865 {
866 	int i;
867 	vm_object_t object;
868 	vm_offset_t offset, startoffset, endoffset, toffset, size;
869 	vm_page_t rtm;
870 	int treqpage;
871 	int cbehind, cahead;
872 
873 	object = m->object;
874 	offset = m->offset;
875 
876 	/*
877 	 * if the requested page is not available, then give up now
878 	 */
879 
880 	if (!vm_pager_has_page(object,
881 		object->paging_offset + offset, &cbehind, &cahead))
882 		return 0;
883 
884 	if ((cbehind == 0) && (cahead == 0)) {
885 		*reqpage = 0;
886 		marray[0] = m;
887 		return 1;
888 	}
889 
890 	if (rahead > cahead) {
891 		rahead = cahead;
892 	}
893 
894 	if (rbehind > cbehind) {
895 		rbehind = cbehind;
896 	}
897 
898 	/*
899 	 * try to do any readahead that we might have free pages for.
900 	 */
901 	if ((rahead + rbehind) >
902 		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
903 		pagedaemon_wakeup();
904 		*reqpage = 0;
905 		marray[0] = m;
906 		return 1;
907 	}
908 
909 	/*
910 	 * scan backward for the read behind pages -- in memory or on disk not
911 	 * in same object
912 	 */
913 	toffset = offset - PAGE_SIZE;
914 	if (toffset < offset) {
915 		if (rbehind * PAGE_SIZE > offset)
916 			rbehind = offset / PAGE_SIZE;
917 		startoffset = offset - rbehind * PAGE_SIZE;
918 		while (toffset >= startoffset) {
919 			if (vm_page_lookup( object, toffset)) {
920 				startoffset = toffset + PAGE_SIZE;
921 				break;
922 			}
923 			if (toffset == 0)
924 				break;
925 			toffset -= PAGE_SIZE;
926 		}
927 	} else {
928 		startoffset = offset;
929 	}
930 
931 	/*
932 	 * scan forward for the read ahead pages -- in memory or on disk not
933 	 * in same object
934 	 */
935 	toffset = offset + PAGE_SIZE;
936 	endoffset = offset + (rahead + 1) * PAGE_SIZE;
937 	if (endoffset > object->size)
938 		endoffset = object->size;
939 	while (toffset < endoffset) {
940 		if ( vm_page_lookup(object, toffset)) {
941 			break;
942 		}
943 		toffset += PAGE_SIZE;
944 	}
945 	endoffset = toffset;
946 
947 	/* calculate number of bytes of pages */
948 	size = (endoffset - startoffset) / PAGE_SIZE;
949 
950 	/* calculate the page offset of the required page */
951 	treqpage = (offset - startoffset) / PAGE_SIZE;
952 
953 	/* see if we have space (again) */
954 	if ((cnt.v_free_count + cnt.v_cache_count) >
955 		(cnt.v_free_reserved + size)) {
956 		/*
957 		 * get our pages and don't block for them
958 		 */
959 		for (i = 0; i < size; i++) {
960 			if (i != treqpage) {
961 				rtm = vm_page_alloc(object,
962 					startoffset + i * PAGE_SIZE,
963 					VM_ALLOC_NORMAL);
964 				if (rtm == NULL) {
965 					if (i < treqpage) {
966 						int j;
967 						for (j = 0; j < i; j++) {
968 							FREE_PAGE(marray[j]);
969 						}
970 						*reqpage = 0;
971 						marray[0] = m;
972 						return 1;
973 					} else {
974 						size = i;
975 						*reqpage = treqpage;
976 						return size;
977 					}
978 				}
979 				marray[i] = rtm;
980 			} else {
981 				marray[i] = m;
982 			}
983 		}
984 
985 		*reqpage = treqpage;
986 		return size;
987 	}
988 	*reqpage = 0;
989 	marray[0] = m;
990 	return 1;
991 }
992