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