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