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