xref: /dflybsd-src/sys/vm/vm_fault.c (revision 558a398b19696474173810daa238fb5df32df27b)
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  * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $
70  * $DragonFly: src/sys/vm/vm_fault.c,v 1.34 2007/01/01 22:51:18 corecode Exp $
71  */
72 
73 /*
74  *	Page fault handling module.
75  */
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/proc.h>
81 #include <sys/vnode.h>
82 #include <sys/resourcevar.h>
83 #include <sys/vmmeter.h>
84 #include <sys/vkernel.h>
85 #include <sys/sfbuf.h>
86 #include <sys/lock.h>
87 
88 #include <vm/vm.h>
89 #include <vm/vm_param.h>
90 #include <vm/pmap.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_kern.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/vm_extern.h>
99 
100 #include <sys/thread2.h>
101 #include <vm/vm_page2.h>
102 
103 #define VM_FAULT_READ_AHEAD 8
104 #define VM_FAULT_READ_BEHIND 7
105 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
106 
107 struct faultstate {
108 	vm_page_t m;
109 	vm_object_t object;
110 	vm_pindex_t pindex;
111 	vm_prot_t prot;
112 	vm_page_t first_m;
113 	vm_object_t first_object;
114 	vm_prot_t first_prot;
115 	vm_map_t map;
116 	vm_map_entry_t entry;
117 	int lookup_still_valid;
118 	int didlimit;
119 	int hardfault;
120 	int fault_flags;
121 	int map_generation;
122 	boolean_t wired;
123 	struct vnode *vp;
124 };
125 
126 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t);
127 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t);
128 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
129 static int vm_fault_ratelimit(struct vmspace *);
130 
131 static __inline void
132 release_page(struct faultstate *fs)
133 {
134 	vm_page_wakeup(fs->m);
135 	vm_page_deactivate(fs->m);
136 	fs->m = NULL;
137 }
138 
139 static __inline void
140 unlock_map(struct faultstate *fs)
141 {
142 	if (fs->lookup_still_valid) {
143 		vm_map_lookup_done(fs->map, fs->entry, 0);
144 		fs->lookup_still_valid = FALSE;
145 	}
146 }
147 
148 /*
149  * Clean up after a successful call to vm_fault_object() so another call
150  * to vm_fault_object() can be made.
151  */
152 static void
153 _cleanup_successful_fault(struct faultstate *fs, int relock)
154 {
155 	if (fs->object != fs->first_object) {
156 		vm_page_free(fs->first_m);
157 		vm_object_pip_wakeup(fs->object);
158 		fs->first_m = NULL;
159 	}
160 	fs->object = fs->first_object;
161 	if (relock && fs->lookup_still_valid == FALSE) {
162 		vm_map_lock_read(fs->map);
163 		fs->lookup_still_valid = TRUE;
164 	}
165 }
166 
167 static void
168 _unlock_things(struct faultstate *fs, int dealloc)
169 {
170 	vm_object_pip_wakeup(fs->first_object);
171 	_cleanup_successful_fault(fs, 0);
172 	if (dealloc) {
173 		vm_object_deallocate(fs->first_object);
174 	}
175 	unlock_map(fs);
176 	if (fs->vp != NULL) {
177 		vput(fs->vp);
178 		fs->vp = NULL;
179 	}
180 }
181 
182 #define unlock_things(fs) _unlock_things(fs, 0)
183 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
184 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
185 
186 /*
187  * TRYPAGER
188  *
189  * Determine if the pager for the current object *might* contain the page.
190  *
191  * We only need to try the pager if this is not a default object (default
192  * objects are zero-fill and have no real pager), and if we are not taking
193  * a wiring fault or if the FS entry is wired.
194  */
195 #define TRYPAGER(fs)	\
196 		(fs->object->type != OBJT_DEFAULT && \
197 		(((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
198 
199 /*
200  * vm_fault:
201  *
202  * Handle a page fault occuring at the given address, requiring the given
203  * permissions, in the map specified.  If successful, the page is inserted
204  * into the associated physical map.
205  *
206  * NOTE: The given address should be truncated to the proper page address.
207  *
208  * KERN_SUCCESS is returned if the page fault is handled; otherwise,
209  * a standard error specifying why the fault is fatal is returned.
210  *
211  * The map in question must be referenced, and remains so.
212  * The caller may hold no locks.
213  */
214 int
215 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
216 {
217 	int result;
218 	vm_pindex_t first_pindex;
219 	struct faultstate fs;
220 
221 	mycpu->gd_cnt.v_vm_faults++;
222 
223 	fs.didlimit = 0;
224 	fs.hardfault = 0;
225 	fs.fault_flags = fault_flags;
226 
227 RetryFault:
228 	/*
229 	 * Find the vm_map_entry representing the backing store and resolve
230 	 * the top level object and page index.  This may have the side
231 	 * effect of executing a copy-on-write on the map entry and/or
232 	 * creating a shadow object, but will not COW any actual VM pages.
233 	 *
234 	 * On success fs.map is left read-locked and various other fields
235 	 * are initialized but not otherwise referenced or locked.
236 	 *
237 	 * NOTE!  vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
238 	 * if the map entry is a virtual page table and also writable,
239 	 * so we can set the 'A'accessed bit in the virtual page table entry.
240 	 */
241 	fs.map = map;
242 	result = vm_map_lookup(&fs.map, vaddr, fault_type,
243 			       &fs.entry, &fs.first_object,
244 			       &first_pindex, &fs.first_prot, &fs.wired);
245 
246 	/*
247 	 * If the lookup failed or the map protections are incompatible,
248 	 * the fault generally fails.  However, if the caller is trying
249 	 * to do a user wiring we have more work to do.
250 	 */
251 	if (result != KERN_SUCCESS) {
252 		if (result != KERN_PROTECTION_FAILURE)
253 			return result;
254 		if ((fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
255 			return result;
256 
257 		/*
258    		 * If we are user-wiring a r/w segment, and it is COW, then
259    		 * we need to do the COW operation.  Note that we don't
260 		 * currently COW RO sections now, because it is NOT desirable
261    		 * to COW .text.  We simply keep .text from ever being COW'ed
262    		 * and take the heat that one cannot debug wired .text sections.
263    		 */
264 		result = vm_map_lookup(&fs.map, vaddr,
265 				       VM_PROT_READ|VM_PROT_WRITE|
266 				        VM_PROT_OVERRIDE_WRITE,
267 				       &fs.entry, &fs.first_object,
268 				       &first_pindex, &fs.first_prot,
269 				       &fs.wired);
270 		if (result != KERN_SUCCESS)
271 			return result;
272 
273 		/*
274 		 * If we don't COW now, on a user wire, the user will never
275 		 * be able to write to the mapping.  If we don't make this
276 		 * restriction, the bookkeeping would be nearly impossible.
277 		 */
278 		if ((fs.entry->protection & VM_PROT_WRITE) == 0)
279 			fs.entry->max_protection &= ~VM_PROT_WRITE;
280 	}
281 
282 	/*
283 	 * fs.map is read-locked
284 	 *
285 	 * Misc checks.  Save the map generation number to detect races.
286 	 */
287 	fs.map_generation = fs.map->timestamp;
288 
289 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
290 		panic("vm_fault: fault on nofault entry, addr: %lx",
291 		    (u_long)vaddr);
292 	}
293 
294 	/*
295 	 * A system map entry may return a NULL object.  No object means
296 	 * no pager means an unrecoverable kernel fault.
297 	 */
298 	if (fs.first_object == NULL) {
299 		panic("vm_fault: unrecoverable fault at %p in entry %p",
300 			(void *)vaddr, fs.entry);
301 	}
302 
303 	/*
304 	 * Make a reference to this object to prevent its disposal while we
305 	 * are messing with it.  Once we have the reference, the map is free
306 	 * to be diddled.  Since objects reference their shadows (and copies),
307 	 * they will stay around as well.
308 	 *
309 	 * Bump the paging-in-progress count to prevent size changes (e.g.
310 	 * truncation operations) during I/O.  This must be done after
311 	 * obtaining the vnode lock in order to avoid possible deadlocks.
312 	 */
313 	vm_object_reference(fs.first_object);
314 	fs.vp = vnode_pager_lock(fs.first_object);
315 	vm_object_pip_add(fs.first_object, 1);
316 
317 	fs.lookup_still_valid = TRUE;
318 	fs.first_m = NULL;
319 	fs.object = fs.first_object;	/* so unlock_and_deallocate works */
320 
321 	/*
322 	 * If the entry is wired we cannot change the page protection.
323 	 */
324 	if (fs.wired)
325 		fault_type = fs.first_prot;
326 
327 	/*
328 	 * The page we want is at (first_object, first_pindex), but if the
329 	 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
330 	 * page table to figure out the actual pindex.
331 	 *
332 	 * NOTE!  DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
333 	 * ONLY
334 	 */
335 	if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
336 		result = vm_fault_vpagetable(&fs, &first_pindex,
337 					     fs.entry->aux.master_pde);
338 		if (result == KERN_TRY_AGAIN)
339 			goto RetryFault;
340 		if (result != KERN_SUCCESS)
341 			return (result);
342 	}
343 
344 	/*
345 	 * Now we have the actual (object, pindex), fault in the page.  If
346 	 * vm_fault_object() fails it will unlock and deallocate the FS
347 	 * data.   If it succeeds everything remains locked and fs->object
348 	 * will have an additinal PIP count if it is not equal to
349 	 * fs->first_object
350 	 */
351 	result = vm_fault_object(&fs, first_pindex, fault_type);
352 
353 	if (result == KERN_TRY_AGAIN)
354 		goto RetryFault;
355 	if (result != KERN_SUCCESS)
356 		return (result);
357 
358 	/*
359 	 * On success vm_fault_object() does not unlock or deallocate, and fs.m
360 	 * will contain a busied page.
361 	 *
362 	 * Enter the page into the pmap and do pmap-related adjustments.
363 	 */
364 	unlock_things(&fs);
365 	pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
366 
367 	if (((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0) && (fs.wired == 0)) {
368 		pmap_prefault(fs.map->pmap, vaddr, fs.entry);
369 	}
370 
371 	vm_page_flag_clear(fs.m, PG_ZERO);
372 	vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
373 	if (fs.fault_flags & VM_FAULT_HOLD)
374 		vm_page_hold(fs.m);
375 
376 	/*
377 	 * If the page is not wired down, then put it where the pageout daemon
378 	 * can find it.
379 	 */
380 	if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
381 		if (fs.wired)
382 			vm_page_wire(fs.m);
383 		else
384 			vm_page_unwire(fs.m, 1);
385 	} else {
386 		vm_page_activate(fs.m);
387 	}
388 
389 	if (curthread->td_lwp) {
390 		if (fs.hardfault) {
391 			curthread->td_lwp->lwp_ru.ru_majflt++;
392 		} else {
393 			curthread->td_lwp->lwp_ru.ru_minflt++;
394 		}
395 	}
396 
397 	/*
398 	 * Unlock everything, and return
399 	 */
400 	vm_page_wakeup(fs.m);
401 	vm_object_deallocate(fs.first_object);
402 
403 	return (KERN_SUCCESS);
404 }
405 
406 /*
407  * Translate the virtual page number (first_pindex) that is relative
408  * to the address space into a logical page number that is relative to the
409  * backing object.  Use the virtual page table pointed to by (vpte).
410  *
411  * This implements an N-level page table.  Any level can terminate the
412  * scan by setting VPTE_PS.   A linear mapping is accomplished by setting
413  * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
414  */
415 static
416 int
417 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex, vpte_t vpte)
418 {
419 	struct sf_buf *sf;
420 	int vshift = 32 - PAGE_SHIFT;	/* page index bits remaining */
421 	int result = KERN_SUCCESS;
422 
423 	for (;;) {
424 		if ((vpte & VPTE_V) == 0) {
425 			unlock_and_deallocate(fs);
426 			return (KERN_FAILURE);
427 		}
428 		if ((vpte & VPTE_PS) || vshift == 0)
429 			break;
430 		KKASSERT(vshift >= VPTE_PAGE_BITS);
431 
432 		/*
433 		 * Get the page table page
434 		 */
435 		result = vm_fault_object(fs, vpte >> PAGE_SHIFT, VM_PROT_READ);
436 		if (result != KERN_SUCCESS)
437 			return (result);
438 
439 		/*
440 		 * Process the returned fs.m and look up the page table
441 		 * entry in the page table page.
442 		 */
443 		vshift -= VPTE_PAGE_BITS;
444 		sf = sf_buf_alloc(fs->m, SFB_CPUPRIVATE);
445 		vpte = *((vpte_t *)sf_buf_kva(sf) +
446 		       ((*pindex >> vshift) & VPTE_PAGE_MASK));
447 		sf_buf_free(sf);
448 		vm_page_flag_set(fs->m, PG_REFERENCED);
449 		vm_page_activate(fs->m);
450 		vm_page_wakeup(fs->m);
451 		cleanup_successful_fault(fs);
452 	}
453 	/*
454 	 * Combine remaining address bits with the vpte.
455 	 */
456 	*pindex = (vpte >> PAGE_SHIFT) +
457 		  (*pindex & ((1 << vshift) - 1));
458 	return (KERN_SUCCESS);
459 }
460 
461 
462 /*
463  * Do all operations required to fault-in (fs.first_object, pindex).  Run
464  * through the shadow chain as necessary and do required COW or virtual
465  * copy operations.  The caller has already fully resolved the vm_map_entry
466  * and, if appropriate, has created a copy-on-write layer.  All we need to
467  * do is iterate the object chain.
468  *
469  * On failure (fs) is unlocked and deallocated and the caller may return or
470  * retry depending on the failure code.  On success (fs) is NOT unlocked or
471  * deallocated, fs.m will contained a resolved, busied page, and fs.object
472  * will have an additional PIP count if it is not equal to fs.first_object.
473  */
474 static
475 int
476 vm_fault_object(struct faultstate *fs,
477 		vm_pindex_t first_pindex, vm_prot_t fault_type)
478 {
479 	vm_object_t next_object;
480 	vm_page_t marray[VM_FAULT_READ];
481 	vm_pindex_t pindex;
482 	int faultcount;
483 
484 	fs->prot = fs->first_prot;
485 	fs->object = fs->first_object;
486 	pindex = first_pindex;
487 
488 	for (;;) {
489 		/*
490 		 * If the object is dead, we stop here
491 		 */
492 		if (fs->object->flags & OBJ_DEAD) {
493 			unlock_and_deallocate(fs);
494 			return (KERN_PROTECTION_FAILURE);
495 		}
496 
497 		/*
498 		 * See if page is resident.  spl protection is required
499 		 * to avoid an interrupt unbusy/free race against our
500 		 * lookup.  We must hold the protection through a page
501 		 * allocation or busy.
502 		 */
503 		crit_enter();
504 		fs->m = vm_page_lookup(fs->object, pindex);
505 		if (fs->m != NULL) {
506 			int queue;
507 			/*
508 			 * Wait/Retry if the page is busy.  We have to do this
509 			 * if the page is busy via either PG_BUSY or
510 			 * vm_page_t->busy because the vm_pager may be using
511 			 * vm_page_t->busy for pageouts ( and even pageins if
512 			 * it is the vnode pager ), and we could end up trying
513 			 * to pagein and pageout the same page simultaneously.
514 			 *
515 			 * We can theoretically allow the busy case on a read
516 			 * fault if the page is marked valid, but since such
517 			 * pages are typically already pmap'd, putting that
518 			 * special case in might be more effort then it is
519 			 * worth.  We cannot under any circumstances mess
520 			 * around with a vm_page_t->busy page except, perhaps,
521 			 * to pmap it.
522 			 */
523 			if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
524 				unlock_things(fs);
525 				vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
526 				mycpu->gd_cnt.v_intrans++;
527 				vm_object_deallocate(fs->first_object);
528 				crit_exit();
529 				return (KERN_TRY_AGAIN);
530 			}
531 
532 			/*
533 			 * If reactivating a page from PQ_CACHE we may have
534 			 * to rate-limit.
535 			 */
536 			queue = fs->m->queue;
537 			vm_page_unqueue_nowakeup(fs->m);
538 
539 			if ((queue - fs->m->pc) == PQ_CACHE &&
540 			    vm_page_count_severe()) {
541 				vm_page_activate(fs->m);
542 				unlock_and_deallocate(fs);
543 				vm_waitpfault();
544 				crit_exit();
545 				return (KERN_TRY_AGAIN);
546 			}
547 
548 			/*
549 			 * Mark page busy for other processes, and the
550 			 * pagedaemon.  If it still isn't completely valid
551 			 * (readable), jump to readrest, else we found the
552 			 * page and can return.
553 			 *
554 			 * We can release the spl once we have marked the
555 			 * page busy.
556 			 */
557 			vm_page_busy(fs->m);
558 			crit_exit();
559 
560 			if (((fs->m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
561 			    fs->m->object != &kernel_object) {
562 				goto readrest;
563 			}
564 			break; /* break to PAGE HAS BEEN FOUND */
565 		}
566 
567 		/*
568 		 * Page is not resident, If this is the search termination
569 		 * or the pager might contain the page, allocate a new page.
570 		 *
571 		 * NOTE: We are still in a critical section.
572 		 */
573 		if (TRYPAGER(fs) || fs->object == fs->first_object) {
574 			/*
575 			 * If the page is beyond the object size we fail
576 			 */
577 			if (pindex >= fs->object->size) {
578 				crit_exit();
579 				unlock_and_deallocate(fs);
580 				return (KERN_PROTECTION_FAILURE);
581 			}
582 
583 			/*
584 			 * Ratelimit.
585 			 */
586 			if (fs->didlimit == 0 && curproc != NULL) {
587 				int limticks;
588 
589 				limticks = vm_fault_ratelimit(curproc->p_vmspace);
590 				if (limticks) {
591 					crit_exit();
592 					unlock_and_deallocate(fs);
593 					tsleep(curproc, 0, "vmrate", limticks);
594 					fs->didlimit = 1;
595 					return (KERN_TRY_AGAIN);
596 				}
597 			}
598 
599 			/*
600 			 * Allocate a new page for this object/offset pair.
601 			 */
602 			fs->m = NULL;
603 			if (!vm_page_count_severe()) {
604 				fs->m = vm_page_alloc(fs->object, pindex,
605 				    (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
606 			}
607 			if (fs->m == NULL) {
608 				crit_exit();
609 				unlock_and_deallocate(fs);
610 				vm_waitpfault();
611 				return (KERN_TRY_AGAIN);
612 			}
613 		}
614 		crit_exit();
615 
616 readrest:
617 		/*
618 		 * We have found a valid page or we have allocated a new page.
619 		 * The page thus may not be valid or may not be entirely
620 		 * valid.
621 		 *
622 		 * Attempt to fault-in the page if there is a chance that the
623 		 * pager has it, and potentially fault in additional pages
624 		 * at the same time.
625 		 *
626 		 * We are NOT in splvm here and if TRYPAGER is true then
627 		 * fs.m will be non-NULL and will be PG_BUSY for us.
628 		 */
629 
630 		if (TRYPAGER(fs)) {
631 			int rv;
632 			int reqpage;
633 			int ahead, behind;
634 			u_char behavior = vm_map_entry_behavior(fs->entry);
635 
636 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
637 				ahead = 0;
638 				behind = 0;
639 			} else {
640 				behind = pindex;
641 				if (behind > VM_FAULT_READ_BEHIND)
642 					behind = VM_FAULT_READ_BEHIND;
643 
644 				ahead = fs->object->size - pindex;
645 				if (ahead < 1)
646 					ahead = 1;
647 				if (ahead > VM_FAULT_READ_AHEAD)
648 					ahead = VM_FAULT_READ_AHEAD;
649 			}
650 
651 			if ((fs->first_object->type != OBJT_DEVICE) &&
652 			    (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
653                                 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
654                                 pindex >= fs->entry->lastr &&
655                                 pindex < fs->entry->lastr + VM_FAULT_READ))
656 			) {
657 				vm_pindex_t firstpindex, tmppindex;
658 
659 				if (first_pindex < 2 * VM_FAULT_READ)
660 					firstpindex = 0;
661 				else
662 					firstpindex = first_pindex - 2 * VM_FAULT_READ;
663 
664 				/*
665 				 * note: partially valid pages cannot be
666 				 * included in the lookahead - NFS piecemeal
667 				 * writes will barf on it badly.
668 				 *
669 				 * spl protection is required to avoid races
670 				 * between the lookup and an interrupt
671 				 * unbusy/free sequence occuring prior to
672 				 * our busy check.
673 				 */
674 				crit_enter();
675 				for (tmppindex = first_pindex - 1;
676 				    tmppindex >= firstpindex;
677 				    --tmppindex
678 				) {
679 					vm_page_t mt;
680 
681 					mt = vm_page_lookup(fs->first_object, tmppindex);
682 					if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
683 						break;
684 					if (mt->busy ||
685 						(mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
686 						mt->hold_count ||
687 						mt->wire_count)
688 						continue;
689 					if (mt->dirty == 0)
690 						vm_page_test_dirty(mt);
691 					if (mt->dirty) {
692 						vm_page_protect(mt, VM_PROT_NONE);
693 						vm_page_deactivate(mt);
694 					} else {
695 						vm_page_cache(mt);
696 					}
697 				}
698 				crit_exit();
699 
700 				ahead += behind;
701 				behind = 0;
702 			}
703 
704 			/*
705 			 * now we find out if any other pages should be paged
706 			 * in at this time this routine checks to see if the
707 			 * pages surrounding this fault reside in the same
708 			 * object as the page for this fault.  If they do,
709 			 * then they are faulted in also into the object.  The
710 			 * array "marray" returned contains an array of
711 			 * vm_page_t structs where one of them is the
712 			 * vm_page_t passed to the routine.  The reqpage
713 			 * return value is the index into the marray for the
714 			 * vm_page_t passed to the routine.
715 			 *
716 			 * fs.m plus the additional pages are PG_BUSY'd.
717 			 */
718 			faultcount = vm_fault_additional_pages(
719 			    fs->m, behind, ahead, marray, &reqpage);
720 
721 			/*
722 			 * update lastr imperfectly (we do not know how much
723 			 * getpages will actually read), but good enough.
724 			 */
725 			fs->entry->lastr = pindex + faultcount - behind;
726 
727 			/*
728 			 * Call the pager to retrieve the data, if any, after
729 			 * releasing the lock on the map.  We hold a ref on
730 			 * fs.object and the pages are PG_BUSY'd.
731 			 */
732 			unlock_map(fs);
733 
734 			if (faultcount) {
735 				rv = vm_pager_get_pages(fs->object, marray,
736 							faultcount, reqpage);
737 			} else {
738 				rv = VM_PAGER_FAIL;
739 			}
740 
741 			if (rv == VM_PAGER_OK) {
742 				/*
743 				 * Found the page. Leave it busy while we play
744 				 * with it.
745 				 */
746 
747 				/*
748 				 * Relookup in case pager changed page. Pager
749 				 * is responsible for disposition of old page
750 				 * if moved.
751 				 *
752 				 * XXX other code segments do relookups too.
753 				 * It's a bad abstraction that needs to be
754 				 * fixed/removed.
755 				 */
756 				fs->m = vm_page_lookup(fs->object, pindex);
757 				if (fs->m == NULL) {
758 					unlock_and_deallocate(fs);
759 					return (KERN_TRY_AGAIN);
760 				}
761 
762 				++fs->hardfault;
763 				break; /* break to PAGE HAS BEEN FOUND */
764 			}
765 
766 			/*
767 			 * Remove the bogus page (which does not exist at this
768 			 * object/offset); before doing so, we must get back
769 			 * our object lock to preserve our invariant.
770 			 *
771 			 * Also wake up any other process that may want to bring
772 			 * in this page.
773 			 *
774 			 * If this is the top-level object, we must leave the
775 			 * busy page to prevent another process from rushing
776 			 * past us, and inserting the page in that object at
777 			 * the same time that we are.
778 			 */
779 			if (rv == VM_PAGER_ERROR) {
780 				if (curproc)
781 					kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
782 				else
783 					kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
784 			}
785 			/*
786 			 * Data outside the range of the pager or an I/O error
787 			 */
788 			/*
789 			 * XXX - the check for kernel_map is a kludge to work
790 			 * around having the machine panic on a kernel space
791 			 * fault w/ I/O error.
792 			 */
793 			if (((fs->map != &kernel_map) && (rv == VM_PAGER_ERROR)) ||
794 				(rv == VM_PAGER_BAD)) {
795 				vm_page_free(fs->m);
796 				fs->m = NULL;
797 				unlock_and_deallocate(fs);
798 				if (rv == VM_PAGER_ERROR)
799 					return (KERN_FAILURE);
800 				else
801 					return (KERN_PROTECTION_FAILURE);
802 				/* NOT REACHED */
803 			}
804 			if (fs->object != fs->first_object) {
805 				vm_page_free(fs->m);
806 				fs->m = NULL;
807 				/*
808 				 * XXX - we cannot just fall out at this
809 				 * point, m has been freed and is invalid!
810 				 */
811 			}
812 		}
813 
814 		/*
815 		 * We get here if the object has a default pager (or unwiring)
816 		 * or the pager doesn't have the page.
817 		 */
818 		if (fs->object == fs->first_object)
819 			fs->first_m = fs->m;
820 
821 		/*
822 		 * Move on to the next object.  Lock the next object before
823 		 * unlocking the current one.
824 		 */
825 		pindex += OFF_TO_IDX(fs->object->backing_object_offset);
826 		next_object = fs->object->backing_object;
827 		if (next_object == NULL) {
828 			/*
829 			 * If there's no object left, fill the page in the top
830 			 * object with zeros.
831 			 */
832 			if (fs->object != fs->first_object) {
833 				vm_object_pip_wakeup(fs->object);
834 
835 				fs->object = fs->first_object;
836 				pindex = first_pindex;
837 				fs->m = fs->first_m;
838 			}
839 			fs->first_m = NULL;
840 
841 			/*
842 			 * Zero the page if necessary and mark it valid.
843 			 */
844 			if ((fs->m->flags & PG_ZERO) == 0) {
845 				vm_page_zero_fill(fs->m);
846 			} else {
847 				mycpu->gd_cnt.v_ozfod++;
848 			}
849 			mycpu->gd_cnt.v_zfod++;
850 			fs->m->valid = VM_PAGE_BITS_ALL;
851 			break;	/* break to PAGE HAS BEEN FOUND */
852 		} else {
853 			if (fs->object != fs->first_object) {
854 				vm_object_pip_wakeup(fs->object);
855 			}
856 			KASSERT(fs->object != next_object, ("object loop %p", next_object));
857 			fs->object = next_object;
858 			vm_object_pip_add(fs->object, 1);
859 		}
860 	}
861 
862 	KASSERT((fs->m->flags & PG_BUSY) != 0,
863 		("vm_fault: not busy after main loop"));
864 
865 	/*
866 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
867 	 * is held.]
868 	 */
869 
870 	/*
871 	 * If the page is being written, but isn't already owned by the
872 	 * top-level object, we have to copy it into a new page owned by the
873 	 * top-level object.
874 	 */
875 	if (fs->object != fs->first_object) {
876 		/*
877 		 * We only really need to copy if we want to write it.
878 		 */
879 		if (fault_type & VM_PROT_WRITE) {
880 			/*
881 			 * This allows pages to be virtually copied from a
882 			 * backing_object into the first_object, where the
883 			 * backing object has no other refs to it, and cannot
884 			 * gain any more refs.  Instead of a bcopy, we just
885 			 * move the page from the backing object to the
886 			 * first object.  Note that we must mark the page
887 			 * dirty in the first object so that it will go out
888 			 * to swap when needed.
889 			 */
890 			if (fs->map_generation == fs->map->timestamp &&
891 				/*
892 				 * Only one shadow object
893 				 */
894 				(fs->object->shadow_count == 1) &&
895 				/*
896 				 * No COW refs, except us
897 				 */
898 				(fs->object->ref_count == 1) &&
899 				/*
900 				 * No one else can look this object up
901 				 */
902 				(fs->object->handle == NULL) &&
903 				/*
904 				 * No other ways to look the object up
905 				 */
906 				((fs->object->type == OBJT_DEFAULT) ||
907 				 (fs->object->type == OBJT_SWAP)) &&
908 				/*
909 				 * We don't chase down the shadow chain
910 				 */
911 				(fs->object == fs->first_object->backing_object) &&
912 
913 				/*
914 				 * grab the lock if we need to
915 				 */
916 				(fs->lookup_still_valid ||
917 				 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
918 			    ) {
919 
920 				fs->lookup_still_valid = 1;
921 				/*
922 				 * get rid of the unnecessary page
923 				 */
924 				vm_page_protect(fs->first_m, VM_PROT_NONE);
925 				vm_page_free(fs->first_m);
926 				fs->first_m = NULL;
927 
928 				/*
929 				 * grab the page and put it into the
930 				 * process'es object.  The page is
931 				 * automatically made dirty.
932 				 */
933 				vm_page_rename(fs->m, fs->first_object, first_pindex);
934 				fs->first_m = fs->m;
935 				vm_page_busy(fs->first_m);
936 				fs->m = NULL;
937 				mycpu->gd_cnt.v_cow_optim++;
938 			} else {
939 				/*
940 				 * Oh, well, lets copy it.
941 				 */
942 				vm_page_copy(fs->m, fs->first_m);
943 			}
944 
945 			if (fs->m) {
946 				/*
947 				 * We no longer need the old page or object.
948 				 */
949 				release_page(fs);
950 			}
951 
952 			/*
953 			 * fs->object != fs->first_object due to above
954 			 * conditional
955 			 */
956 			vm_object_pip_wakeup(fs->object);
957 
958 			/*
959 			 * Only use the new page below...
960 			 */
961 
962 			mycpu->gd_cnt.v_cow_faults++;
963 			fs->m = fs->first_m;
964 			fs->object = fs->first_object;
965 			pindex = first_pindex;
966 		} else {
967 			/*
968 			 * If it wasn't a write fault avoid having to copy
969 			 * the page by mapping it read-only.
970 			 */
971 			fs->prot &= ~VM_PROT_WRITE;
972 		}
973 	}
974 
975 	/*
976 	 * We may have had to unlock a map to do I/O.  If we did then
977 	 * lookup_still_valid will be FALSE.  If the map generation count
978 	 * also changed then all sorts of things could have happened while
979 	 * we were doing the I/O and we need to retry.
980 	 */
981 
982 	if (!fs->lookup_still_valid &&
983 	    (fs->map->timestamp != fs->map_generation)) {
984 		release_page(fs);
985 		unlock_and_deallocate(fs);
986 		return (KERN_TRY_AGAIN);
987 	}
988 
989 	/*
990 	 * Put this page into the physical map. We had to do the unlock above
991 	 * because pmap_enter may cause other faults.   We don't put the page
992 	 * back on the active queue until later so that the page-out daemon
993 	 * won't find us (yet).
994 	 */
995 	if (fs->prot & VM_PROT_WRITE) {
996 		vm_page_flag_set(fs->m, PG_WRITEABLE);
997 		vm_object_set_writeable_dirty(fs->m->object);
998 
999 		/*
1000 		 * If the fault is a write, we know that this page is being
1001 		 * written NOW so dirty it explicitly to save on
1002 		 * pmap_is_modified() calls later.
1003 		 *
1004 		 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1005 		 * if the page is already dirty to prevent data written with
1006 		 * the expectation of being synced from not being synced.
1007 		 * Likewise if this entry does not request NOSYNC then make
1008 		 * sure the page isn't marked NOSYNC.  Applications sharing
1009 		 * data should use the same flags to avoid ping ponging.
1010 		 *
1011 		 * Also tell the backing pager, if any, that it should remove
1012 		 * any swap backing since the page is now dirty.
1013 		 */
1014 		if (fs->entry->eflags & MAP_ENTRY_NOSYNC) {
1015 			if (fs->m->dirty == 0)
1016 				vm_page_flag_set(fs->m, PG_NOSYNC);
1017 		} else {
1018 			vm_page_flag_clear(fs->m, PG_NOSYNC);
1019 		}
1020 		if (fs->fault_flags & VM_FAULT_DIRTY) {
1021 			crit_enter();
1022 			vm_page_dirty(fs->m);
1023 			vm_pager_page_unswapped(fs->m);
1024 			crit_exit();
1025 		}
1026 	}
1027 
1028 	/*
1029 	 * Page had better still be busy.  We are still locked up and
1030 	 * fs->object will have another PIP reference if it is not equal
1031 	 * to fs->first_object.
1032 	 */
1033 	KASSERT(fs->m->flags & PG_BUSY,
1034 		("vm_fault: page %p not busy!", fs->m));
1035 
1036 	/*
1037 	 * Sanity check: page must be completely valid or it is not fit to
1038 	 * map into user space.  vm_pager_get_pages() ensures this.
1039 	 */
1040 	if (fs->m->valid != VM_PAGE_BITS_ALL) {
1041 		vm_page_zero_invalid(fs->m, TRUE);
1042 		kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1043 	}
1044 
1045 	return (KERN_SUCCESS);
1046 }
1047 
1048 /*
1049  * quick version of vm_fault
1050  */
1051 int
1052 vm_fault_quick(caddr_t v, int prot)
1053 {
1054 	int r;
1055 
1056 	if (prot & VM_PROT_WRITE)
1057 		r = subyte(v, fubyte(v));
1058 	else
1059 		r = fubyte(v);
1060 	return(r);
1061 }
1062 
1063 /*
1064  * Wire down a range of virtual addresses in a map.  The entry in question
1065  * should be marked in-transition and the map must be locked.  We must
1066  * release the map temporarily while faulting-in the page to avoid a
1067  * deadlock.  Note that the entry may be clipped while we are blocked but
1068  * will never be freed.
1069  */
1070 int
1071 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1072 {
1073 	boolean_t fictitious;
1074 	vm_offset_t start;
1075 	vm_offset_t end;
1076 	vm_offset_t va;
1077 	vm_paddr_t pa;
1078 	pmap_t pmap;
1079 	int rv;
1080 
1081 	pmap = vm_map_pmap(map);
1082 	start = entry->start;
1083 	end = entry->end;
1084 	fictitious = entry->object.vm_object &&
1085 			(entry->object.vm_object->type == OBJT_DEVICE);
1086 
1087 	vm_map_unlock(map);
1088 	map->timestamp++;
1089 
1090 	/*
1091 	 * We simulate a fault to get the page and enter it in the physical
1092 	 * map.
1093 	 */
1094 	for (va = start; va < end; va += PAGE_SIZE) {
1095 		if (user_wire) {
1096 			rv = vm_fault(map, va, VM_PROT_READ,
1097 					VM_FAULT_USER_WIRE);
1098 		} else {
1099 			rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1100 					VM_FAULT_CHANGE_WIRING);
1101 		}
1102 		if (rv) {
1103 			while (va > start) {
1104 				va -= PAGE_SIZE;
1105 				if ((pa = pmap_extract(pmap, va)) == 0)
1106 					continue;
1107 				pmap_change_wiring(pmap, va, FALSE);
1108 				if (!fictitious)
1109 					vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1110 			}
1111 			vm_map_lock(map);
1112 			return (rv);
1113 		}
1114 	}
1115 	vm_map_lock(map);
1116 	return (KERN_SUCCESS);
1117 }
1118 
1119 /*
1120  * Unwire a range of virtual addresses in a map.  The map should be
1121  * locked.
1122  */
1123 void
1124 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1125 {
1126 	boolean_t fictitious;
1127 	vm_offset_t start;
1128 	vm_offset_t end;
1129 	vm_offset_t va;
1130 	vm_paddr_t pa;
1131 	pmap_t pmap;
1132 
1133 	pmap = vm_map_pmap(map);
1134 	start = entry->start;
1135 	end = entry->end;
1136 	fictitious = entry->object.vm_object &&
1137 			(entry->object.vm_object->type == OBJT_DEVICE);
1138 
1139 	/*
1140 	 * Since the pages are wired down, we must be able to get their
1141 	 * mappings from the physical map system.
1142 	 */
1143 	for (va = start; va < end; va += PAGE_SIZE) {
1144 		pa = pmap_extract(pmap, va);
1145 		if (pa != 0) {
1146 			pmap_change_wiring(pmap, va, FALSE);
1147 			if (!fictitious)
1148 				vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1149 		}
1150 	}
1151 }
1152 
1153 /*
1154  * Reduce the rate at which memory is allocated to a process based
1155  * on the perceived load on the VM system. As the load increases
1156  * the allocation burst rate goes down and the delay increases.
1157  *
1158  * Rate limiting does not apply when faulting active or inactive
1159  * pages.  When faulting 'cache' pages, rate limiting only applies
1160  * if the system currently has a severe page deficit.
1161  *
1162  * XXX vm_pagesupply should be increased when a page is freed.
1163  *
1164  * We sleep up to 1/10 of a second.
1165  */
1166 static int
1167 vm_fault_ratelimit(struct vmspace *vmspace)
1168 {
1169 	if (vm_load_enable == 0)
1170 		return(0);
1171 	if (vmspace->vm_pagesupply > 0) {
1172 		--vmspace->vm_pagesupply;
1173 		return(0);
1174 	}
1175 #ifdef INVARIANTS
1176 	if (vm_load_debug) {
1177 		kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1178 			vm_load,
1179 			(1000 - vm_load ) / 10, vm_load * hz / 10000,
1180 			curproc->p_pid, curproc->p_comm);
1181 	}
1182 #endif
1183 	vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1184 	return(vm_load * hz / 10000);
1185 }
1186 
1187 /*
1188  *	Routine:
1189  *		vm_fault_copy_entry
1190  *	Function:
1191  *		Copy all of the pages from a wired-down map entry to another.
1192  *
1193  *	In/out conditions:
1194  *		The source and destination maps must be locked for write.
1195  *		The source map entry must be wired down (or be a sharing map
1196  *		entry corresponding to a main map entry that is wired down).
1197  */
1198 
1199 void
1200 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1201     vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1202 {
1203 	vm_object_t dst_object;
1204 	vm_object_t src_object;
1205 	vm_ooffset_t dst_offset;
1206 	vm_ooffset_t src_offset;
1207 	vm_prot_t prot;
1208 	vm_offset_t vaddr;
1209 	vm_page_t dst_m;
1210 	vm_page_t src_m;
1211 
1212 #ifdef	lint
1213 	src_map++;
1214 #endif	/* lint */
1215 
1216 	src_object = src_entry->object.vm_object;
1217 	src_offset = src_entry->offset;
1218 
1219 	/*
1220 	 * Create the top-level object for the destination entry. (Doesn't
1221 	 * actually shadow anything - we copy the pages directly.)
1222 	 */
1223 	vm_map_entry_allocate_object(dst_entry);
1224 	dst_object = dst_entry->object.vm_object;
1225 
1226 	prot = dst_entry->max_protection;
1227 
1228 	/*
1229 	 * Loop through all of the pages in the entry's range, copying each
1230 	 * one from the source object (it should be there) to the destination
1231 	 * object.
1232 	 */
1233 	for (vaddr = dst_entry->start, dst_offset = 0;
1234 	    vaddr < dst_entry->end;
1235 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1236 
1237 		/*
1238 		 * Allocate a page in the destination object
1239 		 */
1240 		do {
1241 			dst_m = vm_page_alloc(dst_object,
1242 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1243 			if (dst_m == NULL) {
1244 				vm_wait();
1245 			}
1246 		} while (dst_m == NULL);
1247 
1248 		/*
1249 		 * Find the page in the source object, and copy it in.
1250 		 * (Because the source is wired down, the page will be in
1251 		 * memory.)
1252 		 */
1253 		src_m = vm_page_lookup(src_object,
1254 			OFF_TO_IDX(dst_offset + src_offset));
1255 		if (src_m == NULL)
1256 			panic("vm_fault_copy_wired: page missing");
1257 
1258 		vm_page_copy(src_m, dst_m);
1259 
1260 		/*
1261 		 * Enter it in the pmap...
1262 		 */
1263 
1264 		vm_page_flag_clear(dst_m, PG_ZERO);
1265 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1266 		vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
1267 
1268 		/*
1269 		 * Mark it no longer busy, and put it on the active list.
1270 		 */
1271 		vm_page_activate(dst_m);
1272 		vm_page_wakeup(dst_m);
1273 	}
1274 }
1275 
1276 
1277 /*
1278  * This routine checks around the requested page for other pages that
1279  * might be able to be faulted in.  This routine brackets the viable
1280  * pages for the pages to be paged in.
1281  *
1282  * Inputs:
1283  *	m, rbehind, rahead
1284  *
1285  * Outputs:
1286  *  marray (array of vm_page_t), reqpage (index of requested page)
1287  *
1288  * Return value:
1289  *  number of pages in marray
1290  */
1291 static int
1292 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1293     vm_page_t *marray, int *reqpage)
1294 {
1295 	int i,j;
1296 	vm_object_t object;
1297 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
1298 	vm_page_t rtm;
1299 	int cbehind, cahead;
1300 
1301 	object = m->object;
1302 	pindex = m->pindex;
1303 
1304 	/*
1305 	 * we don't fault-ahead for device pager
1306 	 */
1307 	if (object->type == OBJT_DEVICE) {
1308 		*reqpage = 0;
1309 		marray[0] = m;
1310 		return 1;
1311 	}
1312 
1313 	/*
1314 	 * if the requested page is not available, then give up now
1315 	 */
1316 
1317 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1318 		return 0;
1319 	}
1320 
1321 	if ((cbehind == 0) && (cahead == 0)) {
1322 		*reqpage = 0;
1323 		marray[0] = m;
1324 		return 1;
1325 	}
1326 
1327 	if (rahead > cahead) {
1328 		rahead = cahead;
1329 	}
1330 
1331 	if (rbehind > cbehind) {
1332 		rbehind = cbehind;
1333 	}
1334 
1335 	/*
1336 	 * try to do any readahead that we might have free pages for.
1337 	 */
1338 	if ((rahead + rbehind) >
1339 		((vmstats.v_free_count + vmstats.v_cache_count) - vmstats.v_free_reserved)) {
1340 		pagedaemon_wakeup();
1341 		marray[0] = m;
1342 		*reqpage = 0;
1343 		return 1;
1344 	}
1345 
1346 	/*
1347 	 * scan backward for the read behind pages -- in memory
1348 	 *
1349 	 * Assume that if the page is not found an interrupt will not
1350 	 * create it.  Theoretically interrupts can only remove (busy)
1351 	 * pages, not create new associations.
1352 	 */
1353 	if (pindex > 0) {
1354 		if (rbehind > pindex) {
1355 			rbehind = pindex;
1356 			startpindex = 0;
1357 		} else {
1358 			startpindex = pindex - rbehind;
1359 		}
1360 
1361 		crit_enter();
1362 		for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1363 			if (vm_page_lookup( object, tpindex)) {
1364 				startpindex = tpindex + 1;
1365 				break;
1366 			}
1367 			if (tpindex == 0)
1368 				break;
1369 		}
1370 
1371 		for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
1372 
1373 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1374 			if (rtm == NULL) {
1375 				crit_exit();
1376 				for (j = 0; j < i; j++) {
1377 					vm_page_free(marray[j]);
1378 				}
1379 				marray[0] = m;
1380 				*reqpage = 0;
1381 				return 1;
1382 			}
1383 
1384 			marray[i] = rtm;
1385 		}
1386 		crit_exit();
1387 	} else {
1388 		startpindex = 0;
1389 		i = 0;
1390 	}
1391 
1392 	marray[i] = m;
1393 	/* page offset of the required page */
1394 	*reqpage = i;
1395 
1396 	tpindex = pindex + 1;
1397 	i++;
1398 
1399 	/*
1400 	 * scan forward for the read ahead pages
1401 	 */
1402 	endpindex = tpindex + rahead;
1403 	if (endpindex > object->size)
1404 		endpindex = object->size;
1405 
1406 	crit_enter();
1407 	for( ; tpindex < endpindex; i++, tpindex++) {
1408 
1409 		if (vm_page_lookup(object, tpindex)) {
1410 			break;
1411 		}
1412 
1413 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1414 		if (rtm == NULL) {
1415 			break;
1416 		}
1417 
1418 		marray[i] = rtm;
1419 	}
1420 	crit_exit();
1421 
1422 	/* return number of bytes of pages */
1423 	return i;
1424 }
1425