kernel - simplify vm pager ops, add pre-faulting for zero-fill pages.
[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 $
4ecf7cc9 70 * $DragonFly: src/sys/vm/vm_fault.c,v 1.47 2008/07/01 02:02:56 dillon 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>
bc823b32 87#include <sys/sysctl.h>
984263bc
MD
88
89#include <vm/vm.h>
90#include <vm/vm_param.h>
984263bc
MD
91#include <vm/pmap.h>
92#include <vm/vm_map.h>
93#include <vm/vm_object.h>
94#include <vm/vm_page.h>
95#include <vm/vm_pageout.h>
96#include <vm/vm_kern.h>
97#include <vm/vm_pager.h>
98#include <vm/vnode_pager.h>
99#include <vm/vm_extern.h>
654a39f0
MD
100
101#include <sys/thread2.h>
12e4aaff 102#include <vm/vm_page2.h>
984263bc 103
984263bc
MD
104struct faultstate {
105 vm_page_t m;
106 vm_object_t object;
107 vm_pindex_t pindex;
72579d2e 108 vm_prot_t prot;
984263bc 109 vm_page_t first_m;
568e6804 110 vm_object_t first_object;
72579d2e 111 vm_prot_t first_prot;
984263bc
MD
112 vm_map_t map;
113 vm_map_entry_t entry;
114 int lookup_still_valid;
568e6804
MD
115 int didlimit;
116 int hardfault;
568e6804
MD
117 int fault_flags;
118 int map_generation;
119 boolean_t wired;
984263bc
MD
120 struct vnode *vp;
121};
122
1b9d3514
MD
123static int vm_fast_fault = 1;
124SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0, "");
cf1bb2a8
MD
125static int debug_cluster = 0;
126SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
bc823b32 127
72579d2e 128static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t);
4e7c41c5 129static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t, int);
1b9d3514 130#if 0
568e6804 131static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
1b9d3514 132#endif
72579d2e 133static int vm_fault_ratelimit(struct vmspace *);
1b9d3514
MD
134static void vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry,
135 int prot);
568e6804 136
984263bc
MD
137static __inline void
138release_page(struct faultstate *fs)
139{
984263bc 140 vm_page_deactivate(fs->m);
17cde63e 141 vm_page_wakeup(fs->m);
984263bc
MD
142 fs->m = NULL;
143}
144
145static __inline void
146unlock_map(struct faultstate *fs)
147{
aa542ad5 148 if (fs->lookup_still_valid && fs->map) {
a108bf71 149 vm_map_lookup_done(fs->map, fs->entry, 0);
984263bc
MD
150 fs->lookup_still_valid = FALSE;
151 }
152}
153
75f59a66
MD
154/*
155 * Clean up after a successful call to vm_fault_object() so another call
156 * to vm_fault_object() can be made.
157 */
984263bc 158static void
75f59a66 159_cleanup_successful_fault(struct faultstate *fs, int relock)
984263bc 160{
984263bc
MD
161 if (fs->object != fs->first_object) {
162 vm_page_free(fs->first_m);
75f59a66 163 vm_object_pip_wakeup(fs->object);
984263bc
MD
164 fs->first_m = NULL;
165 }
75f59a66
MD
166 fs->object = fs->first_object;
167 if (relock && fs->lookup_still_valid == FALSE) {
aa542ad5
MD
168 if (fs->map)
169 vm_map_lock_read(fs->map);
75f59a66
MD
170 fs->lookup_still_valid = TRUE;
171 }
172}
173
174static void
175_unlock_things(struct faultstate *fs, int dealloc)
176{
177 vm_object_pip_wakeup(fs->first_object);
178 _cleanup_successful_fault(fs, 0);
984263bc
MD
179 if (dealloc) {
180 vm_object_deallocate(fs->first_object);
bc823b32 181 fs->first_object = NULL;
984263bc
MD
182 }
183 unlock_map(fs);
184 if (fs->vp != NULL) {
185 vput(fs->vp);
186 fs->vp = NULL;
187 }
188}
189
190#define unlock_things(fs) _unlock_things(fs, 0)
191#define unlock_and_deallocate(fs) _unlock_things(fs, 1)
75f59a66 192#define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
984263bc
MD
193
194/*
568e6804
MD
195 * TRYPAGER
196 *
197 * Determine if the pager for the current object *might* contain the page.
984263bc 198 *
568e6804
MD
199 * We only need to try the pager if this is not a default object (default
200 * objects are zero-fill and have no real pager), and if we are not taking
201 * a wiring fault or if the FS entry is wired.
984263bc 202 */
568e6804
MD
203#define TRYPAGER(fs) \
204 (fs->object->type != OBJT_DEFAULT && \
205 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
984263bc
MD
206
207/*
568e6804 208 * vm_fault:
984263bc 209 *
568e6804
MD
210 * Handle a page fault occuring at the given address, requiring the given
211 * permissions, in the map specified. If successful, the page is inserted
212 * into the associated physical map.
984263bc 213 *
568e6804 214 * NOTE: The given address should be truncated to the proper page address.
984263bc 215 *
568e6804
MD
216 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
217 * a standard error specifying why the fault is fatal is returned.
984263bc 218 *
568e6804
MD
219 * The map in question must be referenced, and remains so.
220 * The caller may hold no locks.
984263bc
MD
221 */
222int
223vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
224{
984263bc 225 int result;
72579d2e 226 vm_pindex_t first_pindex;
984263bc
MD
227 struct faultstate fs;
228
12e4aaff 229 mycpu->gd_cnt.v_vm_faults++;
568e6804
MD
230
231 fs.didlimit = 0;
232 fs.hardfault = 0;
233 fs.fault_flags = fault_flags;
984263bc 234
06ecca5a 235RetryFault:
984263bc 236 /*
568e6804
MD
237 * Find the vm_map_entry representing the backing store and resolve
238 * the top level object and page index. This may have the side
239 * effect of executing a copy-on-write on the map entry and/or
240 * creating a shadow object, but will not COW any actual VM pages.
241 *
242 * On success fs.map is left read-locked and various other fields
243 * are initialized but not otherwise referenced or locked.
244 *
4e7c41c5
MD
245 * NOTE! vm_map_lookup will try to upgrade the fault_type to
246 * VM_FAULT_WRITE if the map entry is a virtual page table and also
247 * writable, so we can set the 'A'accessed bit in the virtual page
248 * table entry.
984263bc
MD
249 */
250 fs.map = map;
568e6804
MD
251 result = vm_map_lookup(&fs.map, vaddr, fault_type,
252 &fs.entry, &fs.first_object,
72579d2e 253 &first_pindex, &fs.first_prot, &fs.wired);
568e6804
MD
254
255 /*
256 * If the lookup failed or the map protections are incompatible,
257 * the fault generally fails. However, if the caller is trying
258 * to do a user wiring we have more work to do.
259 */
260 if (result != KERN_SUCCESS) {
261 if (result != KERN_PROTECTION_FAILURE)
262 return result;
263 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
984263bc 264 return result;
984263bc
MD
265
266 /*
267 * If we are user-wiring a r/w segment, and it is COW, then
568e6804
MD
268 * we need to do the COW operation. Note that we don't
269 * currently COW RO sections now, because it is NOT desirable
984263bc
MD
270 * to COW .text. We simply keep .text from ever being COW'ed
271 * and take the heat that one cannot debug wired .text sections.
272 */
273 result = vm_map_lookup(&fs.map, vaddr,
568e6804
MD
274 VM_PROT_READ|VM_PROT_WRITE|
275 VM_PROT_OVERRIDE_WRITE,
276 &fs.entry, &fs.first_object,
72579d2e
MD
277 &first_pindex, &fs.first_prot,
278 &fs.wired);
568e6804 279 if (result != KERN_SUCCESS)
984263bc 280 return result;
984263bc
MD
281
282 /*
283 * If we don't COW now, on a user wire, the user will never
284 * be able to write to the mapping. If we don't make this
285 * restriction, the bookkeeping would be nearly impossible.
286 */
287 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
288 fs.entry->max_protection &= ~VM_PROT_WRITE;
289 }
290
568e6804
MD
291 /*
292 * fs.map is read-locked
293 *
294 * Misc checks. Save the map generation number to detect races.
295 */
296 fs.map_generation = fs.map->timestamp;
984263bc
MD
297
298 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
299 panic("vm_fault: fault on nofault entry, addr: %lx",
300 (u_long)vaddr);
301 }
302
c40f2b75
MD
303 /*
304 * A system map entry may return a NULL object. No object means
305 * no pager means an unrecoverable kernel fault.
306 */
307 if (fs.first_object == NULL) {
308 panic("vm_fault: unrecoverable fault at %p in entry %p",
309 (void *)vaddr, fs.entry);
310 }
311
984263bc
MD
312 /*
313 * Make a reference to this object to prevent its disposal while we
314 * are messing with it. Once we have the reference, the map is free
315 * to be diddled. Since objects reference their shadows (and copies),
316 * they will stay around as well.
317 *
318 * Bump the paging-in-progress count to prevent size changes (e.g.
319 * truncation operations) during I/O. This must be done after
320 * obtaining the vnode lock in order to avoid possible deadlocks.
321 */
322 vm_object_reference(fs.first_object);
323 fs.vp = vnode_pager_lock(fs.first_object);
324 vm_object_pip_add(fs.first_object, 1);
325
984263bc 326 fs.lookup_still_valid = TRUE;
984263bc 327 fs.first_m = NULL;
afeabdca 328 fs.object = fs.first_object; /* so unlock_and_deallocate works */
984263bc
MD
329
330 /*
568e6804 331 * If the entry is wired we cannot change the page protection.
984263bc 332 */
568e6804 333 if (fs.wired)
72579d2e 334 fault_type = fs.first_prot;
984263bc 335
568e6804 336 /*
75f59a66
MD
337 * The page we want is at (first_object, first_pindex), but if the
338 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
339 * page table to figure out the actual pindex.
340 *
341 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
342 * ONLY
568e6804 343 */
568e6804 344 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
72579d2e 345 result = vm_fault_vpagetable(&fs, &first_pindex,
4e7c41c5
MD
346 fs.entry->aux.master_pde,
347 fault_type);
568e6804
MD
348 if (result == KERN_TRY_AGAIN)
349 goto RetryFault;
75f59a66 350 if (result != KERN_SUCCESS)
568e6804 351 return (result);
568e6804 352 }
75f59a66 353
568e6804
MD
354 /*
355 * Now we have the actual (object, pindex), fault in the page. If
356 * vm_fault_object() fails it will unlock and deallocate the FS
75f59a66
MD
357 * data. If it succeeds everything remains locked and fs->object
358 * will have an additinal PIP count if it is not equal to
359 * fs->first_object
4e7c41c5
MD
360 *
361 * vm_fault_object will set fs->prot for the pmap operation. It is
362 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
363 * page can be safely written. However, it will force a read-only
364 * mapping for a read fault if the memory is managed by a virtual
365 * page table.
568e6804 366 */
72579d2e 367 result = vm_fault_object(&fs, first_pindex, fault_type);
afeabdca 368
568e6804
MD
369 if (result == KERN_TRY_AGAIN)
370 goto RetryFault;
371 if (result != KERN_SUCCESS)
372 return (result);
373
374 /*
75f59a66
MD
375 * On success vm_fault_object() does not unlock or deallocate, and fs.m
376 * will contain a busied page.
568e6804
MD
377 *
378 * Enter the page into the pmap and do pmap-related adjustments.
379 */
380 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
381
1b9d3514
MD
382 /*
383 * Burst in a few more pages if possible. The fs.map should still
384 * be locked.
385 */
386 if (fault_flags & VM_FAULT_BURST) {
387 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
388 fs.wired == 0) {
389 vm_prefault(fs.map->pmap, vaddr, fs.entry, fs.prot);
390 }
568e6804 391 }
1b9d3514 392 unlock_things(&fs);
568e6804
MD
393
394 vm_page_flag_clear(fs.m, PG_ZERO);
17cde63e 395 vm_page_flag_set(fs.m, PG_REFERENCED);
568e6804
MD
396
397 /*
398 * If the page is not wired down, then put it where the pageout daemon
399 * can find it.
400 */
401 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
402 if (fs.wired)
403 vm_page_wire(fs.m);
404 else
405 vm_page_unwire(fs.m, 1);
406 } else {
407 vm_page_activate(fs.m);
408 }
409
fde7ac71 410 if (curthread->td_lwp) {
568e6804 411 if (fs.hardfault) {
fde7ac71 412 curthread->td_lwp->lwp_ru.ru_majflt++;
568e6804 413 } else {
fde7ac71 414 curthread->td_lwp->lwp_ru.ru_minflt++;
568e6804
MD
415 }
416 }
417
418 /*
419 * Unlock everything, and return
420 */
421 vm_page_wakeup(fs.m);
422 vm_object_deallocate(fs.first_object);
423
424 return (KERN_SUCCESS);
425}
426
4e158347 427/*
5a0e2a66
MD
428 * Fault in the specified virtual address in the current process map,
429 * returning a held VM page or NULL. See vm_fault_page() for more
430 * information.
431 */
432vm_page_t
433vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
434{
287ebb09 435 struct lwp *lp = curthread->td_lwp;
5a0e2a66
MD
436 vm_page_t m;
437
287ebb09 438 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
5a0e2a66
MD
439 fault_type, VM_FAULT_NORMAL, errorp);
440 return(m);
441}
442
443/*
444 * Fault in the specified virtual address in the specified map, doing all
4e158347
MD
445 * necessary manipulation of the object store and all necessary I/O. Return
446 * a held VM page or NULL, and set *errorp. The related pmap is not
447 * updated.
448 *
5a0e2a66
MD
449 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
450 * and marked PG_REFERENCED as well.
17cde63e
MD
451 *
452 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
453 * error will be returned.
4e158347
MD
454 */
455vm_page_t
456vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
457 int fault_flags, int *errorp)
458{
4e158347
MD
459 vm_pindex_t first_pindex;
460 struct faultstate fs;
17cde63e
MD
461 int result;
462 vm_prot_t orig_fault_type = fault_type;
4e158347
MD
463
464 mycpu->gd_cnt.v_vm_faults++;
465
466 fs.didlimit = 0;
467 fs.hardfault = 0;
468 fs.fault_flags = fault_flags;
469 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
470
471RetryFault:
472 /*
473 * Find the vm_map_entry representing the backing store and resolve
474 * the top level object and page index. This may have the side
475 * effect of executing a copy-on-write on the map entry and/or
476 * creating a shadow object, but will not COW any actual VM pages.
477 *
478 * On success fs.map is left read-locked and various other fields
479 * are initialized but not otherwise referenced or locked.
480 *
481 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
482 * if the map entry is a virtual page table and also writable,
483 * so we can set the 'A'accessed bit in the virtual page table entry.
484 */
485 fs.map = map;
486 result = vm_map_lookup(&fs.map, vaddr, fault_type,
487 &fs.entry, &fs.first_object,
488 &first_pindex, &fs.first_prot, &fs.wired);
489
490 if (result != KERN_SUCCESS) {
491 *errorp = result;
492 return (NULL);
493 }
494
495 /*
496 * fs.map is read-locked
497 *
498 * Misc checks. Save the map generation number to detect races.
499 */
500 fs.map_generation = fs.map->timestamp;
501
502 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
503 panic("vm_fault: fault on nofault entry, addr: %lx",
504 (u_long)vaddr);
505 }
506
507 /*
508 * A system map entry may return a NULL object. No object means
509 * no pager means an unrecoverable kernel fault.
510 */
511 if (fs.first_object == NULL) {
512 panic("vm_fault: unrecoverable fault at %p in entry %p",
513 (void *)vaddr, fs.entry);
514 }
515
516 /*
517 * Make a reference to this object to prevent its disposal while we
518 * are messing with it. Once we have the reference, the map is free
519 * to be diddled. Since objects reference their shadows (and copies),
520 * they will stay around as well.
521 *
522 * Bump the paging-in-progress count to prevent size changes (e.g.
523 * truncation operations) during I/O. This must be done after
524 * obtaining the vnode lock in order to avoid possible deadlocks.
525 */
526 vm_object_reference(fs.first_object);
527 fs.vp = vnode_pager_lock(fs.first_object);
528 vm_object_pip_add(fs.first_object, 1);
529
530 fs.lookup_still_valid = TRUE;
531 fs.first_m = NULL;
532 fs.object = fs.first_object; /* so unlock_and_deallocate works */
533
534 /*
535 * If the entry is wired we cannot change the page protection.
536 */
537 if (fs.wired)
538 fault_type = fs.first_prot;
539
540 /*
541 * The page we want is at (first_object, first_pindex), but if the
542 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
543 * page table to figure out the actual pindex.
544 *
545 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
546 * ONLY
547 */
548 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
549 result = vm_fault_vpagetable(&fs, &first_pindex,
4e7c41c5
MD
550 fs.entry->aux.master_pde,
551 fault_type);
4e158347
MD
552 if (result == KERN_TRY_AGAIN)
553 goto RetryFault;
554 if (result != KERN_SUCCESS) {
555 *errorp = result;
556 return (NULL);
557 }
558 }
559
560 /*
561 * Now we have the actual (object, pindex), fault in the page. If
562 * vm_fault_object() fails it will unlock and deallocate the FS
563 * data. If it succeeds everything remains locked and fs->object
564 * will have an additinal PIP count if it is not equal to
565 * fs->first_object
566 */
567 result = vm_fault_object(&fs, first_pindex, fault_type);
568
569 if (result == KERN_TRY_AGAIN)
570 goto RetryFault;
571 if (result != KERN_SUCCESS) {
572 *errorp = result;
573 return(NULL);
574 }
575
17cde63e
MD
576 if ((orig_fault_type & VM_PROT_WRITE) &&
577 (fs.prot & VM_PROT_WRITE) == 0) {
578 *errorp = KERN_PROTECTION_FAILURE;
579 unlock_and_deallocate(&fs);
580 return(NULL);
581 }
582
4e158347
MD
583 /*
584 * On success vm_fault_object() does not unlock or deallocate, and fs.m
585 * will contain a busied page.
586 */
587 unlock_things(&fs);
588
589 /*
590 * Return a held page. We are not doing any pmap manipulation so do
5a0e2a66
MD
591 * not set PG_MAPPED. However, adjust the page flags according to
592 * the fault type because the caller may not use a managed pmapping
593 * (so we don't want to lose the fact that the page will be dirtied
594 * if a write fault was specified).
4e158347 595 */
5a0e2a66 596 vm_page_hold(fs.m);
4e158347 597 vm_page_flag_clear(fs.m, PG_ZERO);
5a0e2a66
MD
598 if (fault_type & VM_PROT_WRITE)
599 vm_page_dirty(fs.m);
4e158347 600
40d6ef3a
MD
601 /*
602 * Update the pmap. We really only have to do this if a COW
603 * occured to replace the read-only page with the new page. For
604 * now just do it unconditionally. XXX
605 */
606 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
17cde63e 607 vm_page_flag_set(fs.m, PG_REFERENCED);
40d6ef3a 608
4e158347
MD
609 /*
610 * Unbusy the page by activating it. It remains held and will not
611 * be reclaimed.
612 */
613 vm_page_activate(fs.m);
614
615 if (curthread->td_lwp) {
616 if (fs.hardfault) {
617 curthread->td_lwp->lwp_ru.ru_majflt++;
618 } else {
619 curthread->td_lwp->lwp_ru.ru_minflt++;
620 }
621 }
622
623 /*
624 * Unlock everything, and return the held page.
625 */
626 vm_page_wakeup(fs.m);
627 vm_object_deallocate(fs.first_object);
628
629 *errorp = 0;
630 return(fs.m);
631}
632
aa542ad5 633/*
17cde63e
MD
634 * Fault in the specified (object,offset), dirty the returned page as
635 * needed. If the requested fault_type cannot be done NULL and an
636 * error is returned.
aa542ad5
MD
637 */
638vm_page_t
639vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
640 vm_prot_t fault_type, int fault_flags, int *errorp)
641{
642 int result;
643 vm_pindex_t first_pindex;
644 struct faultstate fs;
645 struct vm_map_entry entry;
646
647 bzero(&entry, sizeof(entry));
648 entry.object.vm_object = object;
649 entry.maptype = VM_MAPTYPE_NORMAL;
650 entry.protection = entry.max_protection = fault_type;
651
652 fs.didlimit = 0;
653 fs.hardfault = 0;
654 fs.fault_flags = fault_flags;
655 fs.map = NULL;
656 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
657
658RetryFault:
659
660 fs.first_object = object;
661 first_pindex = OFF_TO_IDX(offset);
662 fs.entry = &entry;
663 fs.first_prot = fault_type;
664 fs.wired = 0;
665 /*fs.map_generation = 0; unused */
666
667 /*
668 * Make a reference to this object to prevent its disposal while we
669 * are messing with it. Once we have the reference, the map is free
670 * to be diddled. Since objects reference their shadows (and copies),
671 * they will stay around as well.
672 *
673 * Bump the paging-in-progress count to prevent size changes (e.g.
674 * truncation operations) during I/O. This must be done after
675 * obtaining the vnode lock in order to avoid possible deadlocks.
676 */
677 vm_object_reference(fs.first_object);
678 fs.vp = vnode_pager_lock(fs.first_object);
679 vm_object_pip_add(fs.first_object, 1);
680
681 fs.lookup_still_valid = TRUE;
682 fs.first_m = NULL;
683 fs.object = fs.first_object; /* so unlock_and_deallocate works */
684
685#if 0
686 /* XXX future - ability to operate on VM object using vpagetable */
687 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
688 result = vm_fault_vpagetable(&fs, &first_pindex,
689 fs.entry->aux.master_pde,
690 fault_type);
691 if (result == KERN_TRY_AGAIN)
692 goto RetryFault;
693 if (result != KERN_SUCCESS) {
694 *errorp = result;
695 return (NULL);
696 }
697 }
698#endif
699
700 /*
701 * Now we have the actual (object, pindex), fault in the page. If
702 * vm_fault_object() fails it will unlock and deallocate the FS
703 * data. If it succeeds everything remains locked and fs->object
704 * will have an additinal PIP count if it is not equal to
705 * fs->first_object
706 */
707 result = vm_fault_object(&fs, first_pindex, fault_type);
708
709 if (result == KERN_TRY_AGAIN)
710 goto RetryFault;
711 if (result != KERN_SUCCESS) {
712 *errorp = result;
713 return(NULL);
714 }
715
17cde63e
MD
716 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
717 *errorp = KERN_PROTECTION_FAILURE;
718 unlock_and_deallocate(&fs);
719 return(NULL);
720 }
721
aa542ad5
MD
722 /*
723 * On success vm_fault_object() does not unlock or deallocate, and fs.m
724 * will contain a busied page.
725 */
726 unlock_things(&fs);
727
728 /*
729 * Return a held page. We are not doing any pmap manipulation so do
730 * not set PG_MAPPED. However, adjust the page flags according to
731 * the fault type because the caller may not use a managed pmapping
732 * (so we don't want to lose the fact that the page will be dirtied
733 * if a write fault was specified).
734 */
735 vm_page_hold(fs.m);
736 vm_page_flag_clear(fs.m, PG_ZERO);
737 if (fault_type & VM_PROT_WRITE)
738 vm_page_dirty(fs.m);
739
740 /*
741 * Indicate that the page was accessed.
742 */
743 vm_page_flag_set(fs.m, PG_REFERENCED);
744
745 /*
746 * Unbusy the page by activating it. It remains held and will not
747 * be reclaimed.
748 */
749 vm_page_activate(fs.m);
750
751 if (curthread->td_lwp) {
752 if (fs.hardfault) {
753 mycpu->gd_cnt.v_vm_faults++;
754 curthread->td_lwp->lwp_ru.ru_majflt++;
755 } else {
756 curthread->td_lwp->lwp_ru.ru_minflt++;
757 }
758 }
759
760 /*
761 * Unlock everything, and return the held page.
762 */
763 vm_page_wakeup(fs.m);
764 vm_object_deallocate(fs.first_object);
765
766 *errorp = 0;
767 return(fs.m);
768}
769
afeabdca 770/*
72579d2e 771 * Translate the virtual page number (first_pindex) that is relative
afeabdca
MD
772 * to the address space into a logical page number that is relative to the
773 * backing object. Use the virtual page table pointed to by (vpte).
774 *
775 * This implements an N-level page table. Any level can terminate the
776 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
777 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
778 */
779static
780int
4e7c41c5
MD
781vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
782 vpte_t vpte, int fault_type)
afeabdca
MD
783{
784 struct sf_buf *sf;
785 int vshift = 32 - PAGE_SHIFT; /* page index bits remaining */
72579d2e 786 int result = KERN_SUCCESS;
4e7c41c5 787 vpte_t *ptep;
afeabdca
MD
788
789 for (;;) {
4e7c41c5
MD
790 /*
791 * We cannot proceed if the vpte is not valid, not readable
792 * for a read fault, or not writable for a write fault.
793 */
afeabdca
MD
794 if ((vpte & VPTE_V) == 0) {
795 unlock_and_deallocate(fs);
796 return (KERN_FAILURE);
797 }
4e7c41c5
MD
798 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
799 unlock_and_deallocate(fs);
800 return (KERN_FAILURE);
801 }
802 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
803 unlock_and_deallocate(fs);
804 return (KERN_FAILURE);
805 }
afeabdca
MD
806 if ((vpte & VPTE_PS) || vshift == 0)
807 break;
808 KKASSERT(vshift >= VPTE_PAGE_BITS);
809
810 /*
4e7c41c5
MD
811 * Get the page table page. Nominally we only read the page
812 * table, but since we are actively setting VPTE_M and VPTE_A,
813 * tell vm_fault_object() that we are writing it.
814 *
815 * There is currently no real need to optimize this.
afeabdca 816 */
0035dca9
MD
817 result = vm_fault_object(fs, vpte >> PAGE_SHIFT,
818 VM_PROT_READ|VM_PROT_WRITE);
afeabdca
MD
819 if (result != KERN_SUCCESS)
820 return (result);
821
822 /*
823 * Process the returned fs.m and look up the page table
824 * entry in the page table page.
825 */
826 vshift -= VPTE_PAGE_BITS;
827 sf = sf_buf_alloc(fs->m, SFB_CPUPRIVATE);
4e7c41c5
MD
828 ptep = ((vpte_t *)sf_buf_kva(sf) +
829 ((*pindex >> vshift) & VPTE_PAGE_MASK));
830 vpte = *ptep;
831
832 /*
833 * Page table write-back. If the vpte is valid for the
834 * requested operation, do a write-back to the page table.
835 *
836 * XXX VPTE_M is not set properly for page directory pages.
837 * It doesn't get set in the page directory if the page table
838 * is modified during a read access.
839 */
840 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
841 (vpte & VPTE_W)) {
0035dca9 842 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
4e7c41c5
MD
843 atomic_set_int(ptep, VPTE_M|VPTE_A);
844 vm_page_dirty(fs->m);
845 }
846 }
847 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
848 (vpte & VPTE_R)) {
849 if ((vpte & VPTE_A) == 0) {
850 atomic_set_int(ptep, VPTE_A);
851 vm_page_dirty(fs->m);
852 }
853 }
afeabdca
MD
854 sf_buf_free(sf);
855 vm_page_flag_set(fs->m, PG_REFERENCED);
856 vm_page_activate(fs->m);
857 vm_page_wakeup(fs->m);
858 cleanup_successful_fault(fs);
859 }
afeabdca
MD
860 /*
861 * Combine remaining address bits with the vpte.
862 */
72579d2e
MD
863 *pindex = (vpte >> PAGE_SHIFT) +
864 (*pindex & ((1 << vshift) - 1));
afeabdca
MD
865 return (KERN_SUCCESS);
866}
867
868
568e6804 869/*
72579d2e 870 * Do all operations required to fault-in (fs.first_object, pindex). Run
568e6804
MD
871 * through the shadow chain as necessary and do required COW or virtual
872 * copy operations. The caller has already fully resolved the vm_map_entry
873 * and, if appropriate, has created a copy-on-write layer. All we need to
874 * do is iterate the object chain.
875 *
876 * On failure (fs) is unlocked and deallocated and the caller may return or
75f59a66
MD
877 * retry depending on the failure code. On success (fs) is NOT unlocked or
878 * deallocated, fs.m will contained a resolved, busied page, and fs.object
879 * will have an additional PIP count if it is not equal to fs.first_object.
568e6804
MD
880 */
881static
882int
72579d2e
MD
883vm_fault_object(struct faultstate *fs,
884 vm_pindex_t first_pindex, vm_prot_t fault_type)
568e6804
MD
885{
886 vm_object_t next_object;
72579d2e 887 vm_pindex_t pindex;
568e6804 888
72579d2e
MD
889 fs->prot = fs->first_prot;
890 fs->object = fs->first_object;
891 pindex = first_pindex;
892
4e7c41c5
MD
893 /*
894 * If a read fault occurs we try to make the page writable if
895 * possible. There are three cases where we cannot make the
896 * page mapping writable:
897 *
898 * (1) The mapping is read-only or the VM object is read-only,
0035dca9 899 * fs->prot above will simply not have VM_PROT_WRITE set.
4e7c41c5
MD
900 *
901 * (2) If the mapping is a virtual page table we need to be able
70fc5283
MD
902 * to detect writes so we can set VPTE_M in the virtual page
903 * table.
4e7c41c5
MD
904 *
905 * (3) If the VM page is read-only or copy-on-write, upgrading would
906 * just result in an unnecessary COW fault.
0035dca9
MD
907 *
908 * VM_PROT_VPAGED is set if faulting via a virtual page table and
909 * causes adjustments to the 'M'odify bit to also turn off write
910 * access to force a re-fault.
4e7c41c5 911 */
0035dca9
MD
912 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
913 if ((fault_type & VM_PROT_WRITE) == 0)
914 fs->prot &= ~VM_PROT_WRITE;
4e7c41c5
MD
915 }
916
568e6804
MD
917 for (;;) {
918 /*
919 * If the object is dead, we stop here
920 */
921 if (fs->object->flags & OBJ_DEAD) {
922 unlock_and_deallocate(fs);
984263bc
MD
923 return (KERN_PROTECTION_FAILURE);
924 }
925
926 /*
06ecca5a
MD
927 * See if page is resident. spl protection is required
928 * to avoid an interrupt unbusy/free race against our
929 * lookup. We must hold the protection through a page
930 * allocation or busy.
984263bc 931 */
654a39f0 932 crit_enter();
72579d2e 933 fs->m = vm_page_lookup(fs->object, pindex);
568e6804 934 if (fs->m != NULL) {
06ecca5a 935 int queue;
984263bc
MD
936 /*
937 * Wait/Retry if the page is busy. We have to do this
938 * if the page is busy via either PG_BUSY or
939 * vm_page_t->busy because the vm_pager may be using
940 * vm_page_t->busy for pageouts ( and even pageins if
941 * it is the vnode pager ), and we could end up trying
942 * to pagein and pageout the same page simultaneously.
943 *
944 * We can theoretically allow the busy case on a read
945 * fault if the page is marked valid, but since such
946 * pages are typically already pmap'd, putting that
947 * special case in might be more effort then it is
948 * worth. We cannot under any circumstances mess
949 * around with a vm_page_t->busy page except, perhaps,
950 * to pmap it.
951 */
568e6804
MD
952 if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
953 unlock_things(fs);
954 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
12e4aaff 955 mycpu->gd_cnt.v_intrans++;
568e6804 956 vm_object_deallocate(fs->first_object);
bc823b32 957 fs->first_object = NULL;
654a39f0 958 crit_exit();
568e6804 959 return (KERN_TRY_AGAIN);
984263bc
MD
960 }
961
568e6804
MD
962 /*
963 * If reactivating a page from PQ_CACHE we may have
964 * to rate-limit.
965 */
966 queue = fs->m->queue;
967 vm_page_unqueue_nowakeup(fs->m);
984263bc 968
568e6804
MD
969 if ((queue - fs->m->pc) == PQ_CACHE &&
970 vm_page_count_severe()) {
971 vm_page_activate(fs->m);
972 unlock_and_deallocate(fs);
659c6a07 973 vm_waitpfault();
654a39f0 974 crit_exit();
568e6804 975 return (KERN_TRY_AGAIN);
984263bc
MD
976 }
977
978 /*
979 * Mark page busy for other processes, and the
980 * pagedaemon. If it still isn't completely valid
cf1bb2a8
MD
981 * (readable), or if a read-ahead-mark is set on
982 * the VM page, jump to readrest, else we found the
568e6804 983 * page and can return.
06ecca5a
MD
984 *
985 * We can release the spl once we have marked the
986 * page busy.
984263bc 987 */
568e6804 988 vm_page_busy(fs->m);
654a39f0 989 crit_exit();
06ecca5a 990
cf1bb2a8
MD
991 if (fs->m->object != &kernel_object) {
992 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
993 VM_PAGE_BITS_ALL) {
994 goto readrest;
995 }
996 if (fs->m->flags & PG_RAM) {
997 if (debug_cluster)
998 kprintf("R");
999 vm_page_flag_clear(fs->m, PG_RAM);
1000 goto readrest;
1001 }
984263bc 1002 }
568e6804 1003 break; /* break to PAGE HAS BEEN FOUND */
984263bc
MD
1004 }
1005
1006 /*
1007 * Page is not resident, If this is the search termination
1008 * or the pager might contain the page, allocate a new page.
06ecca5a 1009 *
568e6804 1010 * NOTE: We are still in a critical section.
984263bc 1011 */
568e6804
MD
1012 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1013 /*
1014 * If the page is beyond the object size we fail
1015 */
72579d2e 1016 if (pindex >= fs->object->size) {
654a39f0 1017 crit_exit();
568e6804 1018 unlock_and_deallocate(fs);
984263bc
MD
1019 return (KERN_PROTECTION_FAILURE);
1020 }
1021
46311ac2
MD
1022 /*
1023 * Ratelimit.
1024 */
568e6804
MD
1025 if (fs->didlimit == 0 && curproc != NULL) {
1026 int limticks;
1027
1028 limticks = vm_fault_ratelimit(curproc->p_vmspace);
46311ac2
MD
1029 if (limticks) {
1030 crit_exit();
568e6804 1031 unlock_and_deallocate(fs);
46311ac2 1032 tsleep(curproc, 0, "vmrate", limticks);
568e6804
MD
1033 fs->didlimit = 1;
1034 return (KERN_TRY_AGAIN);
46311ac2
MD
1035 }
1036 }
1037
984263bc
MD
1038 /*
1039 * Allocate a new page for this object/offset pair.
1040 */
568e6804 1041 fs->m = NULL;
984263bc 1042 if (!vm_page_count_severe()) {
72579d2e 1043 fs->m = vm_page_alloc(fs->object, pindex,
568e6804 1044 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
984263bc 1045 }
568e6804 1046 if (fs->m == NULL) {
654a39f0 1047 crit_exit();
568e6804 1048 unlock_and_deallocate(fs);
659c6a07 1049 vm_waitpfault();
568e6804 1050 return (KERN_TRY_AGAIN);
984263bc
MD
1051 }
1052 }
654a39f0 1053 crit_exit();
984263bc
MD
1054
1055readrest:
1056 /*
1b9d3514
MD
1057 * We have found an invalid or partially valid page, a
1058 * potentially fully valid page with a read-ahead mark,
1059 * or we have allocated a new page.
984263bc
MD
1060 *
1061 * Attempt to fault-in the page if there is a chance that the
1062 * pager has it, and potentially fault in additional pages
1063 * at the same time.
06ecca5a
MD
1064 *
1065 * We are NOT in splvm here and if TRYPAGER is true then
1066 * fs.m will be non-NULL and will be PG_BUSY for us.
984263bc
MD
1067 */
1068
568e6804 1069 if (TRYPAGER(fs)) {
984263bc 1070 int rv;
1b9d3514 1071 int seqaccess;
568e6804 1072 u_char behavior = vm_map_entry_behavior(fs->entry);
984263bc 1073
1b9d3514
MD
1074 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1075 seqaccess = 0;
1076 else
1077 seqaccess = -1;
984263bc 1078
1b9d3514
MD
1079 /*
1080 * If sequential access is detected then attempt
1081 * to deactivate/cache pages behind the scan to
1082 * prevent resource hogging.
1083 *
1084 * Use of PG_RAM to detect sequential access
1085 * also simulates multi-zone sequential access
1086 * detection for free.
1087 *
1088 * NOTE: Partially valid dirty pages cannot be
1089 * deactivated without causing NFS picemeal
1090 * writes to barf.
1091 */
568e6804 1092 if ((fs->first_object->type != OBJT_DEVICE) &&
984263bc
MD
1093 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1094 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1b9d3514 1095 (fs->m->flags & PG_RAM)))
984263bc 1096 ) {
1b9d3514
MD
1097 vm_pindex_t scan_pindex;
1098 int scan_count = 16;
1099
1100 if (first_pindex < 16) {
1101 scan_pindex = 0;
1102 scan_count = 0;
1103 } else {
1104 scan_pindex = first_pindex - 16;
1105 if (scan_pindex < 16)
1106 scan_count = scan_pindex;
1107 else
1108 scan_count = 16;
1109 }
984263bc 1110
654a39f0 1111 crit_enter();
1b9d3514 1112 while (scan_count) {
984263bc 1113 vm_page_t mt;
568e6804 1114
1b9d3514
MD
1115 mt = vm_page_lookup(fs->first_object,
1116 scan_pindex);
1117 if (mt == NULL ||
1118 (mt->valid != VM_PAGE_BITS_ALL)) {
984263bc 1119 break;
1b9d3514 1120 }
984263bc 1121 if (mt->busy ||
1b9d3514
MD
1122 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
1123 mt->hold_count ||
1124 mt->wire_count) {
1125 goto skip;
1126 }
984263bc
MD
1127 if (mt->dirty == 0)
1128 vm_page_test_dirty(mt);
1129 if (mt->dirty) {
17cde63e 1130 vm_page_busy(mt);
1b9d3514
MD
1131 vm_page_protect(mt,
1132 VM_PROT_NONE);
984263bc 1133 vm_page_deactivate(mt);
17cde63e 1134 vm_page_wakeup(mt);
984263bc
MD
1135 } else {
1136 vm_page_cache(mt);
1137 }
1b9d3514
MD
1138skip:
1139 --scan_count;
1140 --scan_pindex;
984263bc 1141 }
654a39f0 1142 crit_exit();
984263bc 1143
1b9d3514 1144 seqaccess = 1;
984263bc
MD
1145 }
1146
1147 /*
1b9d3514
MD
1148 * Avoid deadlocking against the map when doing I/O.
1149 * fs.object and the page is PG_BUSY'd.
984263bc 1150 */
1b9d3514 1151 unlock_map(fs);
984263bc
MD
1152
1153 /*
1b9d3514
MD
1154 * Acquire the page data. We still hold a ref on
1155 * fs.object and the page has been PG_BUSY's.
1156 *
1157 * The pager may replace the page (for example, in
1158 * order to enter a fictitious page into the
1159 * object). If it does so it is responsible for
1160 * cleaning up the passed page and properly setting
1161 * the new page PG_BUSY.
984263bc 1162 */
1b9d3514
MD
1163 if (vm_pager_has_page(fs->object, pindex)) {
1164 rv = vm_pager_get_page(fs->object, &fs->m,
1165 seqaccess);
568e6804
MD
1166 } else {
1167 rv = VM_PAGER_FAIL;
1168 }
984263bc
MD
1169
1170 if (rv == VM_PAGER_OK) {
984263bc
MD
1171 /*
1172 * Relookup in case pager changed page. Pager
1173 * is responsible for disposition of old page
1174 * if moved.
06ecca5a
MD
1175 *
1176 * XXX other code segments do relookups too.
1177 * It's a bad abstraction that needs to be
1178 * fixed/removed.
984263bc 1179 */
72579d2e 1180 fs->m = vm_page_lookup(fs->object, pindex);
568e6804
MD
1181 if (fs->m == NULL) {
1182 unlock_and_deallocate(fs);
1183 return (KERN_TRY_AGAIN);
984263bc
MD
1184 }
1185
568e6804 1186 ++fs->hardfault;
984263bc
MD
1187 break; /* break to PAGE HAS BEEN FOUND */
1188 }
568e6804 1189
984263bc
MD
1190 /*
1191 * Remove the bogus page (which does not exist at this
1192 * object/offset); before doing so, we must get back
1193 * our object lock to preserve our invariant.
1194 *
1195 * Also wake up any other process that may want to bring
1196 * in this page.
1197 *
1198 * If this is the top-level object, we must leave the
1199 * busy page to prevent another process from rushing
1200 * past us, and inserting the page in that object at
1201 * the same time that we are.
1202 */
a0bc8638
MD
1203 if (rv == VM_PAGER_ERROR) {
1204 if (curproc)
086c1d7e 1205 kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
a0bc8638 1206 else
086c1d7e 1207 kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
a0bc8638 1208 }
1b9d3514 1209
984263bc
MD
1210 /*
1211 * Data outside the range of the pager or an I/O error
a55afca2
MD
1212 *
1213 * The page may have been wired during the pagein,
1214 * e.g. by the buffer cache, and cannot simply be
1b9d3514 1215 * freed. Call vnode_pager_freepage() to deal with it.
984263bc
MD
1216 */
1217 /*
1218 * XXX - the check for kernel_map is a kludge to work
1219 * around having the machine panic on a kernel space
1220 * fault w/ I/O error.
1221 */
1b9d3514
MD
1222 if (((fs->map != &kernel_map) &&
1223 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
a55afca2 1224 vnode_pager_freepage(fs->m);
568e6804
MD
1225 fs->m = NULL;
1226 unlock_and_deallocate(fs);
1227 if (rv == VM_PAGER_ERROR)
1228 return (KERN_FAILURE);
1229 else
1230 return (KERN_PROTECTION_FAILURE);
1231 /* NOT REACHED */
984263bc 1232 }
568e6804 1233 if (fs->object != fs->first_object) {
a55afca2 1234 vnode_pager_freepage(fs->m);
568e6804 1235 fs->m = NULL;
984263bc
MD
1236 /*
1237 * XXX - we cannot just fall out at this
1238 * point, m has been freed and is invalid!
1239 */
1240 }
1241 }
1242
1243 /*
568e6804 1244 * We get here if the object has a default pager (or unwiring)
984263bc
MD
1245 * or the pager doesn't have the page.
1246 */
568e6804
MD
1247 if (fs->object == fs->first_object)
1248 fs->first_m = fs->m;
984263bc
MD
1249
1250 /*
1251 * Move on to the next object. Lock the next object before
1252 * unlocking the current one.
1253 */
72579d2e 1254 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
568e6804 1255 next_object = fs->object->backing_object;
984263bc
MD
1256 if (next_object == NULL) {
1257 /*
1258 * If there's no object left, fill the page in the top
1259 * object with zeros.
1260 */
568e6804
MD
1261 if (fs->object != fs->first_object) {
1262 vm_object_pip_wakeup(fs->object);
984263bc 1263
568e6804 1264 fs->object = fs->first_object;
72579d2e 1265 pindex = first_pindex;
568e6804 1266 fs->m = fs->first_m;
984263bc 1267 }
568e6804 1268 fs->first_m = NULL;
984263bc
MD
1269
1270 /*
1271 * Zero the page if necessary and mark it valid.
1272 */
568e6804
MD
1273 if ((fs->m->flags & PG_ZERO) == 0) {
1274 vm_page_zero_fill(fs->m);
984263bc 1275 } else {
12e4aaff 1276 mycpu->gd_cnt.v_ozfod++;
984263bc 1277 }
12e4aaff 1278 mycpu->gd_cnt.v_zfod++;
568e6804 1279 fs->m->valid = VM_PAGE_BITS_ALL;
984263bc
MD
1280 break; /* break to PAGE HAS BEEN FOUND */
1281 } else {
568e6804
MD
1282 if (fs->object != fs->first_object) {
1283 vm_object_pip_wakeup(fs->object);
984263bc 1284 }
568e6804
MD
1285 KASSERT(fs->object != next_object, ("object loop %p", next_object));
1286 fs->object = next_object;
1287 vm_object_pip_add(fs->object, 1);
984263bc
MD
1288 }
1289 }
1290
984263bc
MD
1291 /*
1292 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1293 * is held.]
1b9d3514 1294 *
984263bc
MD
1295 * If the page is being written, but isn't already owned by the
1296 * top-level object, we have to copy it into a new page owned by the
1297 * top-level object.
1298 */
1b9d3514
MD
1299 KASSERT((fs->m->flags & PG_BUSY) != 0,
1300 ("vm_fault: not busy after main loop"));
1301
568e6804 1302 if (fs->object != fs->first_object) {
984263bc
MD
1303 /*
1304 * We only really need to copy if we want to write it.
1305 */
984263bc
MD
1306 if (fault_type & VM_PROT_WRITE) {
1307 /*
1308 * This allows pages to be virtually copied from a
1309 * backing_object into the first_object, where the
1310 * backing object has no other refs to it, and cannot
1311 * gain any more refs. Instead of a bcopy, we just
1312 * move the page from the backing object to the
1313 * first object. Note that we must mark the page
1314 * dirty in the first object so that it will go out
1315 * to swap when needed.
1316 */
aa542ad5
MD
1317 if (
1318 /*
1319 * Map, if present, has not changed
1320 */
1321 (fs->map == NULL ||
1322 fs->map_generation == fs->map->timestamp) &&
984263bc
MD
1323 /*
1324 * Only one shadow object
1325 */
568e6804 1326 (fs->object->shadow_count == 1) &&
984263bc
MD
1327 /*
1328 * No COW refs, except us
1329 */
568e6804 1330 (fs->object->ref_count == 1) &&
984263bc
MD
1331 /*
1332 * No one else can look this object up
1333 */
568e6804 1334 (fs->object->handle == NULL) &&
984263bc
MD
1335 /*
1336 * No other ways to look the object up
1337 */
568e6804
MD
1338 ((fs->object->type == OBJT_DEFAULT) ||
1339 (fs->object->type == OBJT_SWAP)) &&
984263bc
MD
1340 /*
1341 * We don't chase down the shadow chain
1342 */
568e6804 1343 (fs->object == fs->first_object->backing_object) &&
984263bc
MD
1344
1345 /*
1346 * grab the lock if we need to
1347 */
568e6804 1348 (fs->lookup_still_valid ||
aa542ad5 1349 fs->map == NULL ||
568e6804 1350 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
984263bc
MD
1351 ) {
1352
568e6804 1353 fs->lookup_still_valid = 1;
984263bc
MD
1354 /*
1355 * get rid of the unnecessary page
1356 */
568e6804
MD
1357 vm_page_protect(fs->first_m, VM_PROT_NONE);
1358 vm_page_free(fs->first_m);
1359 fs->first_m = NULL;
984263bc
MD
1360
1361 /*
1362 * grab the page and put it into the
1363 * process'es object. The page is
1364 * automatically made dirty.
1365 */
72579d2e 1366 vm_page_rename(fs->m, fs->first_object, first_pindex);
568e6804
MD
1367 fs->first_m = fs->m;
1368 vm_page_busy(fs->first_m);
1369 fs->m = NULL;
12e4aaff 1370 mycpu->gd_cnt.v_cow_optim++;
984263bc
MD
1371 } else {
1372 /*
1373 * Oh, well, lets copy it.
1374 */
568e6804 1375 vm_page_copy(fs->m, fs->first_m);
10192bae 1376 vm_page_event(fs->m, VMEVENT_COW);
984263bc
MD
1377 }
1378
568e6804 1379 if (fs->m) {
984263bc
MD
1380 /*
1381 * We no longer need the old page or object.
1382 */
568e6804 1383 release_page(fs);
984263bc
MD
1384 }
1385
1386 /*
568e6804 1387 * fs->object != fs->first_object due to above
984263bc
MD
1388 * conditional
1389 */
568e6804 1390 vm_object_pip_wakeup(fs->object);
984263bc
MD
1391
1392 /*
1393 * Only use the new page below...
1394 */
1395
12e4aaff 1396 mycpu->gd_cnt.v_cow_faults++;
568e6804
MD
1397 fs->m = fs->first_m;
1398 fs->object = fs->first_object;
72579d2e 1399 pindex = first_pindex;
984263bc 1400 } else {
568e6804
MD
1401 /*
1402 * If it wasn't a write fault avoid having to copy
1403 * the page by mapping it read-only.
1404 */
1405 fs->prot &= ~VM_PROT_WRITE;
984263bc
MD
1406 }
1407 }
1408
1409 /*
568e6804
MD
1410 * We may have had to unlock a map to do I/O. If we did then
1411 * lookup_still_valid will be FALSE. If the map generation count
1412 * also changed then all sorts of things could have happened while
1413 * we were doing the I/O and we need to retry.
984263bc
MD
1414 */
1415
568e6804 1416 if (!fs->lookup_still_valid &&
aa542ad5 1417 fs->map != NULL &&
568e6804
MD
1418 (fs->map->timestamp != fs->map_generation)) {
1419 release_page(fs);
1420 unlock_and_deallocate(fs);
1421 return (KERN_TRY_AGAIN);
1422 }
1423
984263bc 1424 /*
17cde63e
MD
1425 * If the fault is a write, we know that this page is being
1426 * written NOW so dirty it explicitly to save on pmap_is_modified()
1427 * calls later.
1428 *
1429 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1430 * if the page is already dirty to prevent data written with
1431 * the expectation of being synced from not being synced.
1432 * Likewise if this entry does not request NOSYNC then make
1433 * sure the page isn't marked NOSYNC. Applications sharing
1434 * data should use the same flags to avoid ping ponging.
1435 *
1436 * Also tell the backing pager, if any, that it should remove
1437 * any swap backing since the page is now dirty.
984263bc 1438 */
568e6804 1439 if (fs->prot & VM_PROT_WRITE) {
568e6804 1440 vm_object_set_writeable_dirty(fs->m->object);
568e6804
MD
1441 if (fs->entry->eflags & MAP_ENTRY_NOSYNC) {
1442 if (fs->m->dirty == 0)
1443 vm_page_flag_set(fs->m, PG_NOSYNC);
984263bc 1444 } else {
568e6804 1445 vm_page_flag_clear(fs->m, PG_NOSYNC);
984263bc 1446 }
568e6804 1447 if (fs->fault_flags & VM_FAULT_DIRTY) {
654a39f0 1448 crit_enter();
568e6804
MD
1449 vm_page_dirty(fs->m);
1450 vm_pager_page_unswapped(fs->m);
654a39f0 1451 crit_exit();
984263bc
MD
1452 }
1453 }
1454
1455 /*
75f59a66
MD
1456 * Page had better still be busy. We are still locked up and
1457 * fs->object will have another PIP reference if it is not equal
1458 * to fs->first_object.
984263bc 1459 */
568e6804
MD
1460 KASSERT(fs->m->flags & PG_BUSY,
1461 ("vm_fault: page %p not busy!", fs->m));
984263bc 1462
984263bc
MD
1463 /*
1464 * Sanity check: page must be completely valid or it is not fit to
1465 * map into user space. vm_pager_get_pages() ensures this.
1466 */
568e6804
MD
1467 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1468 vm_page_zero_invalid(fs->m, TRUE);
086c1d7e 1469 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
984263bc
MD
1470 }
1471
984263bc 1472 return (KERN_SUCCESS);
984263bc
MD
1473}
1474
1475/*
f2d22ebf
MD
1476 * Wire down a range of virtual addresses in a map. The entry in question
1477 * should be marked in-transition and the map must be locked. We must
1478 * release the map temporarily while faulting-in the page to avoid a
1479 * deadlock. Note that the entry may be clipped while we are blocked but
1480 * will never be freed.
984263bc
MD
1481 */
1482int
f2d22ebf 1483vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
984263bc 1484{
f2d22ebf
MD
1485 boolean_t fictitious;
1486 vm_offset_t start;
1487 vm_offset_t end;
5f910b2f 1488 vm_offset_t va;
f2d22ebf 1489 vm_paddr_t pa;
5f910b2f 1490 pmap_t pmap;
984263bc
MD
1491 int rv;
1492
1493 pmap = vm_map_pmap(map);
f2d22ebf
MD
1494 start = entry->start;
1495 end = entry->end;
1496 fictitious = entry->object.vm_object &&
1497 (entry->object.vm_object->type == OBJT_DEVICE);
984263bc 1498
f2d22ebf
MD
1499 vm_map_unlock(map);
1500 map->timestamp++;
984263bc 1501
984263bc
MD
1502 /*
1503 * We simulate a fault to get the page and enter it in the physical
1504 * map.
1505 */
1506 for (va = start; va < end; va += PAGE_SIZE) {
f2d22ebf
MD
1507 if (user_wire) {
1508 rv = vm_fault(map, va, VM_PROT_READ,
1509 VM_FAULT_USER_WIRE);
1510 } else {
1511 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1512 VM_FAULT_CHANGE_WIRING);
1513 }
984263bc 1514 if (rv) {
f2d22ebf
MD
1515 while (va > start) {
1516 va -= PAGE_SIZE;
1517 if ((pa = pmap_extract(pmap, va)) == 0)
1518 continue;
1519 pmap_change_wiring(pmap, va, FALSE);
1520 if (!fictitious)
1521 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1522 }
f2d22ebf 1523 vm_map_lock(map);
984263bc
MD
1524 return (rv);
1525 }
1526 }
f2d22ebf 1527 vm_map_lock(map);
984263bc
MD
1528 return (KERN_SUCCESS);
1529}
1530
984263bc 1531/*
f2d22ebf
MD
1532 * Unwire a range of virtual addresses in a map. The map should be
1533 * locked.
984263bc
MD
1534 */
1535void
f2d22ebf 1536vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
984263bc 1537{
f2d22ebf
MD
1538 boolean_t fictitious;
1539 vm_offset_t start;
1540 vm_offset_t end;
6ef943a3
MD
1541 vm_offset_t va;
1542 vm_paddr_t pa;
5f910b2f 1543 pmap_t pmap;
984263bc
MD
1544
1545 pmap = vm_map_pmap(map);
f2d22ebf
MD
1546 start = entry->start;
1547 end = entry->end;
1548 fictitious = entry->object.vm_object &&
1549 (entry->object.vm_object->type == OBJT_DEVICE);
984263bc
MD
1550
1551 /*
1552 * Since the pages are wired down, we must be able to get their
1553 * mappings from the physical map system.
1554 */
984263bc
MD
1555 for (va = start; va < end; va += PAGE_SIZE) {
1556 pa = pmap_extract(pmap, va);
6ef943a3 1557 if (pa != 0) {
984263bc 1558 pmap_change_wiring(pmap, va, FALSE);
f2d22ebf
MD
1559 if (!fictitious)
1560 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
984263bc
MD
1561 }
1562 }
984263bc
MD
1563}
1564
46311ac2
MD
1565/*
1566 * Reduce the rate at which memory is allocated to a process based
1567 * on the perceived load on the VM system. As the load increases
1568 * the allocation burst rate goes down and the delay increases.
1569 *
1570 * Rate limiting does not apply when faulting active or inactive
1571 * pages. When faulting 'cache' pages, rate limiting only applies
1572 * if the system currently has a severe page deficit.
1573 *
1574 * XXX vm_pagesupply should be increased when a page is freed.
1575 *
1576 * We sleep up to 1/10 of a second.
1577 */
1578static int
1579vm_fault_ratelimit(struct vmspace *vmspace)
1580{
1581 if (vm_load_enable == 0)
1582 return(0);
1583 if (vmspace->vm_pagesupply > 0) {
1584 --vmspace->vm_pagesupply;
1585 return(0);
1586 }
1587#ifdef INVARIANTS
1588 if (vm_load_debug) {
086c1d7e 1589 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
46311ac2
MD
1590 vm_load,
1591 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1592 curproc->p_pid, curproc->p_comm);
1593 }
1594#endif
1595 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1596 return(vm_load * hz / 10000);
1597}
1598
984263bc
MD
1599/*
1600 * Routine:
1601 * vm_fault_copy_entry
1602 * Function:
1603 * Copy all of the pages from a wired-down map entry to another.
1604 *
1605 * In/out conditions:
1606 * The source and destination maps must be locked for write.
1607 * The source map entry must be wired down (or be a sharing map
1608 * entry corresponding to a main map entry that is wired down).
1609 */
1610
1611void
57e43348
MD
1612vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1613 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
984263bc
MD
1614{
1615 vm_object_t dst_object;
1616 vm_object_t src_object;
1617 vm_ooffset_t dst_offset;
1618 vm_ooffset_t src_offset;
1619 vm_prot_t prot;
1620 vm_offset_t vaddr;
1621 vm_page_t dst_m;
1622 vm_page_t src_m;
1623
1624#ifdef lint
1625 src_map++;
1626#endif /* lint */
1627
1628 src_object = src_entry->object.vm_object;
1629 src_offset = src_entry->offset;
1630
1631 /*
1632 * Create the top-level object for the destination entry. (Doesn't
1633 * actually shadow anything - we copy the pages directly.)
1634 */
53025830
MD
1635 vm_map_entry_allocate_object(dst_entry);
1636 dst_object = dst_entry->object.vm_object;
984263bc
MD
1637
1638 prot = dst_entry->max_protection;
1639
1640 /*
1641 * Loop through all of the pages in the entry's range, copying each
1642 * one from the source object (it should be there) to the destination
1643 * object.
1644 */
1645 for (vaddr = dst_entry->start, dst_offset = 0;
1646 vaddr < dst_entry->end;
1647 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1648
1649 /*
1650 * Allocate a page in the destination object
1651 */
1652 do {
1653 dst_m = vm_page_alloc(dst_object,
1654 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1655 if (dst_m == NULL) {
4ecf7cc9 1656 vm_wait(0);
984263bc
MD
1657 }
1658 } while (dst_m == NULL);
1659
1660 /*
1661 * Find the page in the source object, and copy it in.
1662 * (Because the source is wired down, the page will be in
1663 * memory.)
1664 */
1665 src_m = vm_page_lookup(src_object,
1666 OFF_TO_IDX(dst_offset + src_offset));
1667 if (src_m == NULL)
1668 panic("vm_fault_copy_wired: page missing");
1669
1670 vm_page_copy(src_m, dst_m);
10192bae 1671 vm_page_event(src_m, VMEVENT_COW);
984263bc
MD
1672
1673 /*
1674 * Enter it in the pmap...
1675 */
1676
1677 vm_page_flag_clear(dst_m, PG_ZERO);
1678 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
984263bc
MD
1679
1680 /*
1681 * Mark it no longer busy, and put it on the active list.
1682 */
1683 vm_page_activate(dst_m);
1684 vm_page_wakeup(dst_m);
1685 }
1686}
1687
1b9d3514 1688#if 0
984263bc
MD
1689
1690/*
1691 * This routine checks around the requested page for other pages that
1692 * might be able to be faulted in. This routine brackets the viable
1693 * pages for the pages to be paged in.
1694 *
1695 * Inputs:
1696 * m, rbehind, rahead
1697 *
1698 * Outputs:
1699 * marray (array of vm_page_t), reqpage (index of requested page)
1700 *
1701 * Return value:
1702 * number of pages in marray
1703 */
1704static int
57e43348 1705vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
bc823b32 1706 vm_page_t *marray, int *reqpage)
984263bc
MD
1707{
1708 int i,j;
1709 vm_object_t object;
1710 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1711 vm_page_t rtm;
1712 int cbehind, cahead;
1713
1714 object = m->object;
1715 pindex = m->pindex;
1716
1717 /*
1718 * we don't fault-ahead for device pager
1719 */
1720 if (object->type == OBJT_DEVICE) {
1721 *reqpage = 0;
1722 marray[0] = m;
1723 return 1;
1724 }
1725
1726 /*
1727 * if the requested page is not available, then give up now
1728 */
984263bc 1729 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
17cde63e 1730 *reqpage = 0; /* not used by caller, fix compiler warn */
984263bc
MD
1731 return 0;
1732 }
1733
1734 if ((cbehind == 0) && (cahead == 0)) {
1735 *reqpage = 0;
1736 marray[0] = m;
1737 return 1;
1738 }
1739
1740 if (rahead > cahead) {
1741 rahead = cahead;
1742 }
1743
1744 if (rbehind > cbehind) {
1745 rbehind = cbehind;
1746 }
1747
1748 /*
bc823b32
MD
1749 * Do not do any readahead if we have insufficient free memory.
1750 *
1751 * XXX code was broken disabled before and has instability
1752 * with this conditonal fixed, so shortcut for now.
984263bc 1753 */
bc823b32 1754 if (burst_fault == 0 || vm_page_count_severe()) {
984263bc
MD
1755 marray[0] = m;
1756 *reqpage = 0;
1757 return 1;
1758 }
1759
1760 /*
1761 * scan backward for the read behind pages -- in memory
06ecca5a
MD
1762 *
1763 * Assume that if the page is not found an interrupt will not
1764 * create it. Theoretically interrupts can only remove (busy)
1765 * pages, not create new associations.
984263bc
MD
1766 */
1767 if (pindex > 0) {
1768 if (rbehind > pindex) {
1769 rbehind = pindex;
1770 startpindex = 0;
1771 } else {
1772 startpindex = pindex - rbehind;
1773 }
1774
654a39f0 1775 crit_enter();
bc823b32
MD
1776 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
1777 if (vm_page_lookup(object, tpindex - 1))
984263bc
MD
1778 break;
1779 }
1780
bc823b32
MD
1781 i = 0;
1782 while (tpindex < pindex) {
1783 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
984263bc 1784 if (rtm == NULL) {
654a39f0 1785 crit_exit();
984263bc
MD
1786 for (j = 0; j < i; j++) {
1787 vm_page_free(marray[j]);
1788 }
1789 marray[0] = m;
1790 *reqpage = 0;
1791 return 1;
1792 }
984263bc 1793 marray[i] = rtm;
bc823b32
MD
1794 ++i;
1795 ++tpindex;
984263bc 1796 }
654a39f0 1797 crit_exit();
984263bc 1798 } else {
984263bc
MD
1799 i = 0;
1800 }
1801
bc823b32
MD
1802 /*
1803 * Assign requested page
1804 */
984263bc 1805 marray[i] = m;
984263bc 1806 *reqpage = i;
bc823b32 1807 ++i;
984263bc
MD
1808
1809 /*
bc823b32 1810 * Scan forwards for read-ahead pages
984263bc 1811 */
bc823b32 1812 tpindex = pindex + 1;
984263bc
MD
1813 endpindex = tpindex + rahead;
1814 if (endpindex > object->size)
1815 endpindex = object->size;
1816
654a39f0 1817 crit_enter();
bc823b32
MD
1818 while (tpindex < endpindex) {
1819 if (vm_page_lookup(object, tpindex))
984263bc 1820 break;
bc823b32
MD
1821 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1822 if (rtm == NULL)
984263bc 1823 break;
984263bc 1824 marray[i] = rtm;
bc823b32
MD
1825 ++i;
1826 ++tpindex;
984263bc 1827 }
654a39f0 1828 crit_exit();
984263bc 1829
bc823b32 1830 return (i);
984263bc 1831}
1b9d3514
MD
1832
1833#endif
1834
1835/*
1836 * vm_prefault() provides a quick way of clustering pagefaults into a
1837 * processes address space. It is a "cousin" of pmap_object_init_pt,
1838 * except it runs at page fault time instead of mmap time.
1839 *
1840 * This code used to be per-platform pmap_prefault(). It is now
1841 * machine-independent and enhanced to also pre-fault zero-fill pages
1842 * (see vm.fast_fault) as well as make them writable, which greatly
1843 * reduces the number of page faults programs incur.
1844 *
1845 * Application performance when pre-faulting zero-fill pages is heavily
1846 * dependent on the application. Very tiny applications like /bin/echo
1847 * lose a little performance while applications of any appreciable size
1848 * gain performance. Prefaulting multiple pages also reduces SMP
1849 * congestion and can improve SMP performance significantly.
1850 *
1851 * NOTE! prot may allow writing but this only applies to the top level
1852 * object. If we wind up mapping a page extracted from a backing
1853 * object we have to make sure it is read-only.
1854 *
1855 * NOTE! The caller has already handled any COW operations on the
1856 * vm_map_entry via the normal fault code. Do NOT call this
1857 * shortcut unless the normal fault code has run on this entry.
1858 */
1859#define PFBAK 4
1860#define PFFOR 4
1861#define PAGEORDER_SIZE (PFBAK+PFFOR)
1862
1863static int vm_prefault_pageorder[] = {
1864 -PAGE_SIZE, PAGE_SIZE,
1865 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
1866 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
1867 -4 * PAGE_SIZE, 4 * PAGE_SIZE
1868};
1869
1870static void
1871vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot)
1872{
1873 struct lwp *lp;
1874 vm_page_t m;
1875 vm_offset_t starta;
1876 vm_offset_t addr;
1877 vm_pindex_t index;
1878 vm_pindex_t pindex;
1879 vm_object_t object;
1880 int pprot;
1881 int i;
1882
1883 /*
1884 * We do not currently prefault mappings that use virtual page
1885 * tables. We do not prefault foreign pmaps.
1886 */
1887 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
1888 return;
1889 lp = curthread->td_lwp;
1890 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
1891 return;
1892
1893 object = entry->object.vm_object;
1894
1895 starta = addra - PFBAK * PAGE_SIZE;
1896 if (starta < entry->start)
1897 starta = entry->start;
1898 else if (starta > addra)
1899 starta = 0;
1900
1901 /*
1902 * critical section protection is required to maintain the
1903 * page/object association, interrupts can free pages and remove
1904 * them from their objects.
1905 */
1906 crit_enter();
1907 for (i = 0; i < PAGEORDER_SIZE; i++) {
1908 vm_object_t lobject;
1909
1910 addr = addra + vm_prefault_pageorder[i];
1911 if (addr > addra + (PFFOR * PAGE_SIZE))
1912 addr = 0;
1913
1914 if (addr < starta || addr >= entry->end)
1915 continue;
1916
1917 if (pmap_prefault_ok(pmap, addr) == 0)
1918 continue;
1919
1920 /*
1921 * Follow the VM object chain to obtain the page to be mapped
1922 * into the pmap.
1923 *
1924 * If we reach the terminal object without finding a page
1925 * and we determine it would be advantageous, then allocate
1926 * a zero-fill page for the base object. The base object
1927 * is guaranteed to be OBJT_DEFAULT for this case.
1928 */
1929 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1930 lobject = object;
1931 pindex = index;
1932 pprot = prot;
1933
1934 while ((m = vm_page_lookup(lobject, pindex)) == NULL) {
1935 if (lobject->type != OBJT_DEFAULT)
1936 break;
1937 if (lobject->backing_object == NULL) {
1938 if (vm_fast_fault == 0)
1939 break;
1940 if (vm_prefault_pageorder[i] < 0 ||
1941 (prot & VM_PROT_WRITE) == 0 ||
1942 vm_page_count_min(0)) {
1943 break;
1944 }
1945 m = vm_page_alloc(object, index,
1946 VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
1947
1948 if ((m->flags & PG_ZERO) == 0) {
1949 vm_page_zero_fill(m);
1950 } else {
1951 vm_page_flag_clear(m, PG_ZERO);
1952 mycpu->gd_cnt.v_ozfod++;
1953 }
1954 mycpu->gd_cnt.v_zfod++;
1955 m->valid = VM_PAGE_BITS_ALL;
1956 vm_page_wakeup(m);
1957 pprot = prot;
1958 /* lobject = object .. not needed */
1959 break;
1960 }
1961 if (lobject->backing_object_offset & PAGE_MASK)
1962 break;
1963 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1964 lobject = lobject->backing_object;
1965 pprot &= ~VM_PROT_WRITE;
1966 }
1967 /*
1968 * NOTE: lobject now invalid (if we did a zero-fill we didn't
1969 * bother assigning lobject = object).
1970 *
1971 * Give-up if the page is not available.
1972 */
1973 if (m == NULL)
1974 break;
1975
1976 /*
1977 * Do not conditionalize on PG_RAM. If pages are present in
1978 * the VM system we assume optimal caching. If caching is
1979 * not optimal the I/O gravy train will be restarted when we
1980 * hit an unavailable page. We do not want to try to restart
1981 * the gravy train now because we really don't know how much
1982 * of the object has been cached. The cost for restarting
1983 * the gravy train should be low (since accesses will likely
1984 * be I/O bound anyway).
1985 *
1986 * The object must be marked dirty if we are mapping a
1987 * writable page.
1988 */
1989 if (pprot & VM_PROT_WRITE)
1990 vm_object_set_writeable_dirty(m->object);
1991
1992 /*
1993 * Enter the page into the pmap if appropriate.
1994 */
1995 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
1996 (m->busy == 0) &&
1997 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
1998
1999 if ((m->queue - m->pc) == PQ_CACHE) {
2000 vm_page_deactivate(m);
2001 }
2002 vm_page_busy(m);
2003 pmap_enter(pmap, addr, m, pprot, 0);
2004 vm_page_wakeup(m);
2005 }
2006 }
2007 crit_exit();
2008}