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