1 /* 2 * Copyright (c) 2003-2019 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * --- 35 * 36 * Copyright (c) 1991, 1993 37 * The Regents of the University of California. All rights reserved. 38 * Copyright (c) 1994 John S. Dyson 39 * All rights reserved. 40 * Copyright (c) 1994 David Greenman 41 * All rights reserved. 42 * 43 * 44 * This code is derived from software contributed to Berkeley by 45 * The Mach Operating System project at Carnegie-Mellon University. 46 * 47 * Redistribution and use in source and binary forms, with or without 48 * modification, are permitted provided that the following conditions 49 * are met: 50 * 1. Redistributions of source code must retain the above copyright 51 * notice, this list of conditions and the following disclaimer. 52 * 2. Redistributions in binary form must reproduce the above copyright 53 * notice, this list of conditions and the following disclaimer in the 54 * documentation and/or other materials provided with the distribution. 55 * 3. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * --- 72 * 73 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 74 * All rights reserved. 75 * 76 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 77 * 78 * Permission to use, copy, modify and distribute this software and 79 * its documentation is hereby granted, provided that both the copyright 80 * notice and this permission notice appear in all copies of the 81 * software, derivative works or modified versions, and any portions 82 * thereof, and that both notices appear in supporting documentation. 83 * 84 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 85 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 86 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 87 * 88 * Carnegie Mellon requests users of this software to return to 89 * 90 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 91 * School of Computer Science 92 * Carnegie Mellon University 93 * Pittsburgh PA 15213-3890 94 * 95 * any improvements or extensions that they make and grant Carnegie the 96 * rights to redistribute these changes. 97 */ 98 99 /* 100 * Page fault handling module. 101 */ 102 103 #include <sys/param.h> 104 #include <sys/systm.h> 105 #include <sys/kernel.h> 106 #include <sys/proc.h> 107 #include <sys/vnode.h> 108 #include <sys/resourcevar.h> 109 #include <sys/vmmeter.h> 110 #include <sys/vkernel.h> 111 #include <sys/lock.h> 112 #include <sys/sysctl.h> 113 114 #include <cpu/lwbuf.h> 115 116 #include <vm/vm.h> 117 #include <vm/vm_param.h> 118 #include <vm/pmap.h> 119 #include <vm/vm_map.h> 120 #include <vm/vm_object.h> 121 #include <vm/vm_page.h> 122 #include <vm/vm_pageout.h> 123 #include <vm/vm_kern.h> 124 #include <vm/vm_pager.h> 125 #include <vm/vnode_pager.h> 126 #include <vm/swap_pager.h> 127 #include <vm/vm_extern.h> 128 129 #include <vm/vm_page2.h> 130 131 struct faultstate { 132 vm_page_t m; 133 vm_map_backing_t ba; 134 vm_prot_t prot; 135 vm_page_t first_m; 136 vm_map_backing_t first_ba; 137 vm_prot_t first_prot; 138 vm_map_t map; 139 vm_map_entry_t entry; 140 int lookup_still_valid; /* 0=inv 1=valid/rel -1=valid/atomic */ 141 int hardfault; 142 int fault_flags; 143 int shared; 144 int msoftonly; 145 int first_shared; 146 int wflags; 147 int first_ba_held; /* 0=unlocked 1=locked/rel -1=lock/atomic */ 148 struct vnode *vp; 149 }; 150 151 __read_mostly static int debug_fault = 0; 152 SYSCTL_INT(_vm, OID_AUTO, debug_fault, CTLFLAG_RW, &debug_fault, 0, ""); 153 __read_mostly static int debug_cluster = 0; 154 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, ""); 155 #if 0 156 static int virtual_copy_enable = 1; 157 SYSCTL_INT(_vm, OID_AUTO, virtual_copy_enable, CTLFLAG_RW, 158 &virtual_copy_enable, 0, ""); 159 #endif 160 __read_mostly int vm_shared_fault = 1; 161 TUNABLE_INT("vm.shared_fault", &vm_shared_fault); 162 SYSCTL_INT(_vm, OID_AUTO, shared_fault, CTLFLAG_RW, 163 &vm_shared_fault, 0, "Allow shared token on vm_object"); 164 __read_mostly static int vm_fault_quick_enable = 0; 165 TUNABLE_INT("vm.fault_quick", &vm_fault_quick_enable); 166 SYSCTL_INT(_vm, OID_AUTO, fault_quick, CTLFLAG_RW, 167 &vm_fault_quick_enable, 0, "Allow fast vm_fault shortcut"); 168 #ifdef VM_FAULT_QUICK_DEBUG 169 static long vm_fault_quick_success_count = 0; 170 SYSCTL_LONG(_vm, OID_AUTO, fault_quick_success_count, CTLFLAG_RW, 171 &vm_fault_quick_success_count, 0, ""); 172 static long vm_fault_quick_failure_count1 = 0; 173 SYSCTL_LONG(_vm, OID_AUTO, fault_quick_failure_count1, CTLFLAG_RW, 174 &vm_fault_quick_failure_count1, 0, ""); 175 static long vm_fault_quick_failure_count2 = 0; 176 SYSCTL_LONG(_vm, OID_AUTO, fault_quick_failure_count2, CTLFLAG_RW, 177 &vm_fault_quick_failure_count2, 0, ""); 178 static long vm_fault_quick_failure_count3 = 0; 179 SYSCTL_LONG(_vm, OID_AUTO, fault_quick_failure_count3, CTLFLAG_RW, 180 &vm_fault_quick_failure_count3, 0, ""); 181 static long vm_fault_quick_failure_count4 = 0; 182 SYSCTL_LONG(_vm, OID_AUTO, fault_quick_failure_count4, CTLFLAG_RW, 183 &vm_fault_quick_failure_count4, 0, ""); 184 #endif 185 186 static int vm_fault_quick(struct faultstate *fs, vm_pindex_t first_pindex, 187 vm_prot_t fault_type); 188 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t, int); 189 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, 190 vpte_t, int, int); 191 #if 0 192 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *); 193 #endif 194 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry); 195 static void vm_prefault(pmap_t pmap, vm_offset_t addra, 196 vm_map_entry_t entry, int prot, int fault_flags); 197 static void vm_prefault_quick(pmap_t pmap, vm_offset_t addra, 198 vm_map_entry_t entry, int prot, int fault_flags); 199 200 static __inline void 201 release_page(struct faultstate *fs) 202 { 203 vm_page_deactivate(fs->m); 204 vm_page_wakeup(fs->m); 205 fs->m = NULL; 206 } 207 208 static __inline void 209 unlock_map(struct faultstate *fs) 210 { 211 if (fs->ba != fs->first_ba) 212 vm_object_drop(fs->ba->object); 213 if (fs->first_ba && fs->first_ba_held == 1) { 214 vm_object_drop(fs->first_ba->object); 215 fs->first_ba_held = 0; 216 fs->first_ba = NULL; 217 } 218 fs->ba = NULL; 219 220 /* 221 * NOTE: If lookup_still_valid == -1 the map is assumed to be locked 222 * and caller expects it to remain locked atomically. 223 */ 224 if (fs->lookup_still_valid == 1 && fs->map) { 225 vm_map_lookup_done(fs->map, fs->entry, 0); 226 fs->lookup_still_valid = 0; 227 fs->entry = NULL; 228 } 229 } 230 231 /* 232 * Clean up after a successful call to vm_fault_object() so another call 233 * to vm_fault_object() can be made. 234 */ 235 static void 236 cleanup_fault(struct faultstate *fs) 237 { 238 /* 239 * We allocated a junk page for a COW operation that did 240 * not occur, the page must be freed. 241 */ 242 if (fs->ba != fs->first_ba) { 243 KKASSERT(fs->first_shared == 0); 244 245 /* 246 * first_m could be completely valid and we got here 247 * because of a PG_RAM, don't mistakenly free it! 248 */ 249 if ((fs->first_m->valid & VM_PAGE_BITS_ALL) == 250 VM_PAGE_BITS_ALL) { 251 vm_page_wakeup(fs->first_m); 252 } else { 253 vm_page_free(fs->first_m); 254 } 255 vm_object_pip_wakeup(fs->ba->object); 256 fs->first_m = NULL; 257 258 /* 259 * Reset fs->ba (used by vm_fault_vpagetahble() without 260 * calling unlock_map(), so we need a little duplication. 261 */ 262 vm_object_drop(fs->ba->object); 263 fs->ba = fs->first_ba; 264 } 265 } 266 267 static void 268 unlock_things(struct faultstate *fs) 269 { 270 cleanup_fault(fs); 271 unlock_map(fs); 272 if (fs->vp != NULL) { 273 vput(fs->vp); 274 fs->vp = NULL; 275 } 276 } 277 278 #if 0 279 /* 280 * Virtual copy tests. Used by the fault code to determine if a 281 * page can be moved from an orphan vm_object into its shadow 282 * instead of copying its contents. 283 */ 284 static __inline int 285 virtual_copy_test(struct faultstate *fs) 286 { 287 /* 288 * Must be holding exclusive locks 289 */ 290 if (fs->first_shared || fs->shared || virtual_copy_enable == 0) 291 return 0; 292 293 /* 294 * Map, if present, has not changed 295 */ 296 if (fs->map && fs->map_generation != fs->map->timestamp) 297 return 0; 298 299 /* 300 * No refs, except us 301 */ 302 if (fs->ba->object->ref_count != 1) 303 return 0; 304 305 /* 306 * No one else can look this object up 307 */ 308 if (fs->ba->object->handle != NULL) 309 return 0; 310 311 /* 312 * No other ways to look the object up 313 */ 314 if (fs->ba->object->type != OBJT_DEFAULT && 315 fs->ba->object->type != OBJT_SWAP) 316 return 0; 317 318 /* 319 * We don't chase down the shadow chain 320 */ 321 if (fs->ba != fs->first_ba->backing_ba) 322 return 0; 323 324 return 1; 325 } 326 327 static __inline int 328 virtual_copy_ok(struct faultstate *fs) 329 { 330 if (virtual_copy_test(fs)) { 331 /* 332 * Grab the lock and re-test changeable items. 333 */ 334 if (fs->lookup_still_valid == 0 && fs->map) { 335 if (lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT)) 336 return 0; 337 fs->lookup_still_valid = 1; 338 if (virtual_copy_test(fs)) { 339 fs->map_generation = ++fs->map->timestamp; 340 return 1; 341 } 342 fs->lookup_still_valid = 0; 343 lockmgr(&fs->map->lock, LK_RELEASE); 344 } 345 } 346 return 0; 347 } 348 #endif 349 350 /* 351 * TRYPAGER 352 * 353 * Determine if the pager for the current object *might* contain the page. 354 * 355 * We only need to try the pager if this is not a default object (default 356 * objects are zero-fill and have no real pager), and if we are not taking 357 * a wiring fault or if the FS entry is wired. 358 */ 359 #define TRYPAGER(fs) \ 360 (fs->ba->object->type != OBJT_DEFAULT && \ 361 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || \ 362 (fs->wflags & FW_WIRED))) 363 364 /* 365 * vm_fault: 366 * 367 * Handle a page fault occuring at the given address, requiring the given 368 * permissions, in the map specified. If successful, the page is inserted 369 * into the associated physical map. 370 * 371 * NOTE: The given address should be truncated to the proper page address. 372 * 373 * KERN_SUCCESS is returned if the page fault is handled; otherwise, 374 * a standard error specifying why the fault is fatal is returned. 375 * 376 * The map in question must be referenced, and remains so. 377 * The caller may hold no locks. 378 * No other requirements. 379 */ 380 int 381 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags) 382 { 383 int result; 384 vm_pindex_t first_pindex; 385 struct faultstate fs; 386 struct lwp *lp; 387 struct proc *p; 388 thread_t td; 389 struct vm_map_ilock ilock; 390 int didilock; 391 int growstack; 392 int retry = 0; 393 int inherit_prot; 394 395 inherit_prot = fault_type & VM_PROT_NOSYNC; 396 fs.hardfault = 0; 397 fs.fault_flags = fault_flags; 398 fs.vp = NULL; 399 fs.shared = vm_shared_fault; 400 fs.first_shared = vm_shared_fault; 401 growstack = 1; 402 403 /* 404 * vm_map interactions 405 */ 406 td = curthread; 407 if ((lp = td->td_lwp) != NULL) 408 lp->lwp_flags |= LWP_PAGING; 409 410 RetryFault: 411 /* 412 * vm_fault_quick() can shortcut us. 413 */ 414 fs.msoftonly = 0; 415 fs.first_ba_held = 0; 416 417 /* 418 * Find the vm_map_entry representing the backing store and resolve 419 * the top level object and page index. This may have the side 420 * effect of executing a copy-on-write on the map entry, 421 * creating a shadow object, or splitting an anonymous entry for 422 * performance, but will not COW any actual VM pages. 423 * 424 * On success fs.map is left read-locked and various other fields 425 * are initialized but not otherwise referenced or locked. 426 * 427 * NOTE! vm_map_lookup will try to upgrade the fault_type to 428 * VM_FAULT_WRITE if the map entry is a virtual page table 429 * and also writable, so we can set the 'A'accessed bit in 430 * the virtual page table entry. 431 */ 432 fs.map = map; 433 result = vm_map_lookup(&fs.map, vaddr, fault_type, 434 &fs.entry, &fs.first_ba, 435 &first_pindex, &fs.first_prot, &fs.wflags); 436 437 /* 438 * If the lookup failed or the map protections are incompatible, 439 * the fault generally fails. 440 * 441 * The failure could be due to TDF_NOFAULT if vm_map_lookup() 442 * tried to do a COW fault. 443 * 444 * If the caller is trying to do a user wiring we have more work 445 * to do. 446 */ 447 if (result != KERN_SUCCESS) { 448 if (result == KERN_FAILURE_NOFAULT) { 449 result = KERN_FAILURE; 450 goto done; 451 } 452 if (result != KERN_PROTECTION_FAILURE || 453 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) 454 { 455 if (result == KERN_INVALID_ADDRESS && growstack && 456 map != &kernel_map && curproc != NULL) { 457 result = vm_map_growstack(map, vaddr); 458 if (result == KERN_SUCCESS) { 459 growstack = 0; 460 ++retry; 461 goto RetryFault; 462 } 463 result = KERN_FAILURE; 464 } 465 goto done; 466 } 467 468 /* 469 * If we are user-wiring a r/w segment, and it is COW, then 470 * we need to do the COW operation. Note that we don't 471 * currently COW RO sections now, because it is NOT desirable 472 * to COW .text. We simply keep .text from ever being COW'ed 473 * and take the heat that one cannot debug wired .text sections. 474 * 475 * XXX Try to allow the above by specifying OVERRIDE_WRITE. 476 */ 477 result = vm_map_lookup(&fs.map, vaddr, 478 VM_PROT_READ|VM_PROT_WRITE| 479 VM_PROT_OVERRIDE_WRITE, 480 &fs.entry, &fs.first_ba, 481 &first_pindex, &fs.first_prot, 482 &fs.wflags); 483 if (result != KERN_SUCCESS) { 484 /* could also be KERN_FAILURE_NOFAULT */ 485 result = KERN_FAILURE; 486 goto done; 487 } 488 489 /* 490 * If we don't COW now, on a user wire, the user will never 491 * be able to write to the mapping. If we don't make this 492 * restriction, the bookkeeping would be nearly impossible. 493 * 494 * XXX We have a shared lock, this will have a MP race but 495 * I don't see how it can hurt anything. 496 */ 497 if ((fs.entry->protection & VM_PROT_WRITE) == 0) { 498 atomic_clear_char(&fs.entry->max_protection, 499 VM_PROT_WRITE); 500 } 501 } 502 503 /* 504 * fs.map is read-locked 505 * 506 * Misc checks. Save the map generation number to detect races. 507 */ 508 fs.lookup_still_valid = 1; 509 fs.first_m = NULL; 510 fs.ba = fs.first_ba; /* so unlock_things() works */ 511 fs.prot = fs.first_prot; /* default (used by uksmap) */ 512 513 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) { 514 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 515 panic("vm_fault: fault on nofault entry, addr: %p", 516 (void *)vaddr); 517 } 518 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) && 519 vaddr >= fs.entry->start && 520 vaddr < fs.entry->start + PAGE_SIZE) { 521 panic("vm_fault: fault on stack guard, addr: %p", 522 (void *)vaddr); 523 } 524 } 525 526 /* 527 * A user-kernel shared map has no VM object and bypasses 528 * everything. We execute the uksmap function with a temporary 529 * fictitious vm_page. The address is directly mapped with no 530 * management. 531 */ 532 if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) { 533 struct vm_page fakem; 534 535 bzero(&fakem, sizeof(fakem)); 536 fakem.pindex = first_pindex; 537 fakem.flags = PG_FICTITIOUS | PG_UNMANAGED; 538 fakem.busy_count = PBUSY_LOCKED; 539 fakem.valid = VM_PAGE_BITS_ALL; 540 fakem.pat_mode = VM_MEMATTR_DEFAULT; 541 if (fs.entry->ba.uksmap(fs.entry->aux.dev, &fakem)) { 542 result = KERN_FAILURE; 543 unlock_things(&fs); 544 goto done2; 545 } 546 pmap_enter(fs.map->pmap, vaddr, &fakem, fs.prot | inherit_prot, 547 (fs.wflags & FW_WIRED), fs.entry); 548 goto done_success; 549 } 550 551 /* 552 * A system map entry may return a NULL object. No object means 553 * no pager means an unrecoverable kernel fault. 554 */ 555 if (fs.first_ba == NULL) { 556 panic("vm_fault: unrecoverable fault at %p in entry %p", 557 (void *)vaddr, fs.entry); 558 } 559 560 /* 561 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT 562 * is set. 563 * 564 * Unfortunately a deadlock can occur if we are forced to page-in 565 * from swap, but diving all the way into the vm_pager_get_page() 566 * function to find out is too much. Just check the object type. 567 * 568 * The deadlock is a CAM deadlock on a busy VM page when trying 569 * to finish an I/O if another process gets stuck in 570 * vop_helper_read_shortcut() due to a swap fault. 571 */ 572 if ((td->td_flags & TDF_NOFAULT) && 573 (retry || 574 fs.first_ba->object->type == OBJT_VNODE || 575 fs.first_ba->object->type == OBJT_SWAP || 576 fs.first_ba->backing_ba)) { 577 result = KERN_FAILURE; 578 unlock_things(&fs); 579 goto done2; 580 } 581 582 /* 583 * If the entry is wired we cannot change the page protection. 584 */ 585 if (fs.wflags & FW_WIRED) 586 fault_type = fs.first_prot; 587 588 /* 589 * We generally want to avoid unnecessary exclusive modes on backing 590 * and terminal objects because this can seriously interfere with 591 * heavily fork()'d processes (particularly /bin/sh scripts). 592 * 593 * However, we also want to avoid unnecessary retries due to needed 594 * shared->exclusive promotion for common faults. Exclusive mode is 595 * always needed if any page insertion, rename, or free occurs in an 596 * object (and also indirectly if any I/O is done). 597 * 598 * The main issue here is going to be fs.first_shared. If the 599 * first_object has a backing object which isn't shadowed and the 600 * process is single-threaded we might as well use an exclusive 601 * lock/chain right off the bat. 602 */ 603 #if 0 604 /* WORK IN PROGRESS, CODE REMOVED */ 605 if (fs.first_shared && fs.first_object->backing_object && 606 LIST_EMPTY(&fs.first_object->shadow_head) && 607 td->td_proc && td->td_proc->p_nthreads == 1) { 608 fs.first_shared = 0; 609 } 610 #endif 611 612 /* 613 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object 614 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but 615 * we can try shared first. 616 */ 617 if (fault_flags & VM_FAULT_UNSWAP) 618 fs.first_shared = 0; 619 620 /* 621 * Try to shortcut the entire mess and run the fault lockless. 622 */ 623 if (vm_fault_quick_enable && 624 vm_fault_quick(&fs, first_pindex, fault_type) == KERN_SUCCESS) { 625 didilock = 0; 626 fault_flags &= ~VM_FAULT_BURST; 627 goto success; 628 } 629 630 /* 631 * Exclusive heuristic (alloc page vs page exists) 632 */ 633 if (fs.first_ba->flags & VM_MAP_BACK_EXCL_HEUR) 634 fs.first_shared = 0; 635 636 /* 637 * Obtain a top-level object lock, shared or exclusive depending 638 * on fs.first_shared. If a shared lock winds up being insufficient 639 * we will retry with an exclusive lock. 640 * 641 * The vnode pager lock is always shared. 642 */ 643 if (fs.first_shared) 644 vm_object_hold_shared(fs.first_ba->object); 645 else 646 vm_object_hold(fs.first_ba->object); 647 if (fs.vp == NULL) 648 fs.vp = vnode_pager_lock(fs.first_ba); 649 fs.first_ba_held = 1; 650 651 /* 652 * The page we want is at (first_object, first_pindex), but if the 653 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the 654 * page table to figure out the actual pindex. 655 * 656 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION 657 * ONLY 658 */ 659 didilock = 0; 660 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 661 vm_map_interlock(fs.map, &ilock, vaddr, vaddr + PAGE_SIZE); 662 didilock = 1; 663 result = vm_fault_vpagetable(&fs, &first_pindex, 664 fs.entry->aux.master_pde, 665 fault_type, 1); 666 if (result == KERN_TRY_AGAIN) { 667 vm_map_deinterlock(fs.map, &ilock); 668 ++retry; 669 goto RetryFault; 670 } 671 if (result != KERN_SUCCESS) { 672 vm_map_deinterlock(fs.map, &ilock); 673 goto done; 674 } 675 } 676 677 /* 678 * Now we have the actual (object, pindex), fault in the page. If 679 * vm_fault_object() fails it will unlock and deallocate the FS 680 * data. If it succeeds everything remains locked and fs->ba->object 681 * will have an additional PIP count if fs->ba != fs->first_ba. 682 * 683 * vm_fault_object will set fs->prot for the pmap operation. It is 684 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the 685 * page can be safely written. However, it will force a read-only 686 * mapping for a read fault if the memory is managed by a virtual 687 * page table. 688 * 689 * If the fault code uses the shared object lock shortcut 690 * we must not try to burst (we can't allocate VM pages). 691 */ 692 result = vm_fault_object(&fs, first_pindex, fault_type, 1); 693 694 if (debug_fault > 0) { 695 --debug_fault; 696 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x " 697 "fs.m=%p fs.prot=%02x fs.wflags=%02x fs.entry=%p\n", 698 result, (intmax_t)vaddr, fault_type, fault_flags, 699 fs.m, fs.prot, fs.wflags, fs.entry); 700 } 701 702 if (result == KERN_TRY_AGAIN) { 703 if (didilock) 704 vm_map_deinterlock(fs.map, &ilock); 705 ++retry; 706 goto RetryFault; 707 } 708 if (result != KERN_SUCCESS) { 709 if (didilock) 710 vm_map_deinterlock(fs.map, &ilock); 711 goto done; 712 } 713 714 success: 715 /* 716 * On success vm_fault_object() does not unlock or deallocate, and fs.m 717 * will contain a busied page. It does drop fs->ba if appropriate. 718 * 719 * Enter the page into the pmap and do pmap-related adjustments. 720 * 721 * WARNING! Soft-busied fs.m's can only be manipulated in limited 722 * ways. 723 */ 724 KKASSERT(fs.lookup_still_valid != 0); 725 vm_page_flag_set(fs.m, PG_REFERENCED); 726 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot | inherit_prot, 727 fs.wflags & FW_WIRED, fs.entry); 728 729 if (didilock) 730 vm_map_deinterlock(fs.map, &ilock); 731 732 /* 733 * If the page is not wired down, then put it where the pageout daemon 734 * can find it. 735 * 736 * NOTE: We cannot safely wire, unwire, or adjust queues for a 737 * soft-busied page. 738 */ 739 if (fs.msoftonly) { 740 KKASSERT(fs.m->busy_count & PBUSY_MASK); 741 KKASSERT((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0); 742 vm_page_sbusy_drop(fs.m); 743 } else { 744 if (fs.fault_flags & VM_FAULT_WIRE_MASK) { 745 if (fs.wflags & FW_WIRED) 746 vm_page_wire(fs.m); 747 else 748 vm_page_unwire(fs.m, 1); 749 } else { 750 vm_page_activate(fs.m); 751 } 752 KKASSERT(fs.m->busy_count & PBUSY_LOCKED); 753 vm_page_wakeup(fs.m); 754 } 755 756 /* 757 * Burst in a few more pages if possible. The fs.map should still 758 * be locked. To avoid interlocking against a vnode->getblk 759 * operation we had to be sure to unbusy our primary vm_page above 760 * first. 761 * 762 * A normal burst can continue down backing store, only execute 763 * if we are holding an exclusive lock, otherwise the exclusive 764 * locks the burst code gets might cause excessive SMP collisions. 765 * 766 * A quick burst can be utilized when there is no backing object 767 * (i.e. a shared file mmap). 768 */ 769 if ((fault_flags & VM_FAULT_BURST) && 770 (fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 && 771 (fs.wflags & FW_WIRED) == 0) { 772 if (fs.first_shared == 0 && fs.shared == 0) { 773 vm_prefault(fs.map->pmap, vaddr, 774 fs.entry, fs.prot, fault_flags); 775 } else { 776 vm_prefault_quick(fs.map->pmap, vaddr, 777 fs.entry, fs.prot, fault_flags); 778 } 779 } 780 781 done_success: 782 mycpu->gd_cnt.v_vm_faults++; 783 if (td->td_lwp) 784 ++td->td_lwp->lwp_ru.ru_minflt; 785 786 /* 787 * Unlock everything, and return 788 */ 789 unlock_things(&fs); 790 791 if (td->td_lwp) { 792 if (fs.hardfault) { 793 td->td_lwp->lwp_ru.ru_majflt++; 794 } else { 795 td->td_lwp->lwp_ru.ru_minflt++; 796 } 797 } 798 799 /*vm_object_deallocate(fs.first_ba->object);*/ 800 /*fs.m = NULL; */ 801 802 result = KERN_SUCCESS; 803 done: 804 if (fs.first_ba && fs.first_ba->object && fs.first_ba_held == 1) { 805 vm_object_drop(fs.first_ba->object); 806 fs.first_ba_held = 0; 807 } 808 done2: 809 if (lp) 810 lp->lwp_flags &= ~LWP_PAGING; 811 812 #if !defined(NO_SWAPPING) 813 /* 814 * Check the process RSS limit and force deactivation and 815 * (asynchronous) paging if necessary. This is a complex operation, 816 * only do it for direct user-mode faults, for now. 817 * 818 * To reduce overhead implement approximately a ~16MB hysteresis. 819 */ 820 p = td->td_proc; 821 if ((fault_flags & VM_FAULT_USERMODE) && lp && 822 p->p_limit && map->pmap && vm_pageout_memuse_mode >= 1 && 823 map != &kernel_map) { 824 vm_pindex_t limit; 825 vm_pindex_t size; 826 827 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur, 828 p->p_rlimit[RLIMIT_RSS].rlim_max)); 829 size = pmap_resident_tlnw_count(map->pmap); 830 if (limit >= 0 && size > 4096 && size - 4096 >= limit) { 831 vm_pageout_map_deactivate_pages(map, limit); 832 } 833 } 834 #endif 835 836 if (result != KERN_SUCCESS && debug_fault < 0) { 837 kprintf("VM_FAULT %d:%d (%s) result %d " 838 "addr=%jx type=%02x flags=%02x " 839 "fs.m=%p fs.prot=%02x fs.wflags=%02x fs.entry=%p\n", 840 (curthread->td_proc ? curthread->td_proc->p_pid : -1), 841 (curthread->td_lwp ? curthread->td_lwp->lwp_tid : -1), 842 curthread->td_comm, 843 result, 844 (intmax_t)vaddr, fault_type, fault_flags, 845 fs.m, fs.prot, fs.wflags, fs.entry); 846 while (debug_fault < 0 && (debug_fault & 1)) 847 tsleep(&debug_fault, 0, "DEBUG", hz); 848 } 849 850 return (result); 851 } 852 853 /* 854 * Attempt a lockless vm_fault() shortcut. The stars have to align for this 855 * to work. But if it does we can get our page only soft-busied and not 856 * have to touch the vm_object or vnode locks at all. 857 */ 858 static 859 int 860 vm_fault_quick(struct faultstate *fs, vm_pindex_t first_pindex, 861 vm_prot_t fault_type) 862 { 863 vm_page_t m; 864 vm_object_t obj; /* NOT LOCKED */ 865 866 /* 867 * Don't waste time if the object is only being used by one vm_map. 868 * 869 * WARNING! We can't rely on obj->ref_count here because it might 870 * be part of a shared ba chain, and we can't rely on 871 * ba->refs for the same reason. The combination of it 872 * being the ba embedded in the entry (aka first_ba) AND 873 * ref_count == 1 would work, but OBJ_ONEMAPPING is better 874 * because it will remain flagged even when ref_count > 1 875 * for situations where entries are clipped. 876 */ 877 obj = fs->first_ba->object; 878 if (obj->flags & OBJ_ONEMAPPING) 879 return KERN_FAILURE; 880 881 /* 882 * This will try to wire/unwire a page, which can't be done with 883 * a soft-busied page. 884 */ 885 if (fs->fault_flags & VM_FAULT_WIRE_MASK) 886 return KERN_FAILURE; 887 888 /* 889 * Ick, can't handle this 890 */ 891 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) { 892 #ifdef VM_FAULT_QUICK_DEBUG 893 ++vm_fault_quick_failure_count1; 894 #endif 895 return KERN_FAILURE; 896 } 897 898 /* 899 * Ok, try to get the vm_page quickly via the hash table. The 900 * page will be soft-busied on success (NOT hard-busied). 901 */ 902 m = vm_page_hash_get(obj, first_pindex); 903 if (m == NULL) { 904 #ifdef VM_FAULT_QUICK_DEBUG 905 ++vm_fault_quick_failure_count2; 906 #endif 907 return KERN_FAILURE; 908 } 909 if ((obj->flags & OBJ_DEAD) || 910 m->valid != VM_PAGE_BITS_ALL || 911 m->queue - m->pc == PQ_CACHE || 912 (m->flags & PG_SWAPPED)) { 913 vm_page_sbusy_drop(m); 914 #ifdef VM_FAULT_QUICK_DEBUG 915 ++vm_fault_quick_failure_count3; 916 #endif 917 return KERN_FAILURE; 918 } 919 920 /* 921 * The page is already fully valid, ACTIVE, and is not PG_SWAPPED. 922 * 923 * Don't map the page writable when emulating the dirty bit, a 924 * fault must be taken for proper emulation (vkernel). 925 */ 926 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace && 927 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) { 928 if ((fault_type & VM_PROT_WRITE) == 0) 929 fs->prot &= ~VM_PROT_WRITE; 930 } 931 932 /* 933 * If this is a write fault the object and the page must already 934 * be writable. Since we don't hold an object lock and only a 935 * soft-busy on the page, we cannot manipulate the object or 936 * the page state (other than the page queue). 937 */ 938 if (fs->prot & VM_PROT_WRITE) { 939 if ((obj->flags & (OBJ_WRITEABLE | OBJ_MIGHTBEDIRTY)) != 940 (OBJ_WRITEABLE | OBJ_MIGHTBEDIRTY) || 941 m->dirty != VM_PAGE_BITS_ALL) { 942 vm_page_sbusy_drop(m); 943 #ifdef VM_FAULT_QUICK_DEBUG 944 ++vm_fault_quick_failure_count4; 945 #endif 946 return KERN_FAILURE; 947 } 948 vm_set_nosync(m, fs->entry); 949 } 950 951 /* 952 * Even though we are only soft-busied we can still move pages 953 * around in the normal queue(s). The soft-busy prevents the 954 * page from being removed from the object, etc (normal operation). 955 */ 956 vm_page_activate(m); 957 fs->m = m; 958 fs->msoftonly = 1; 959 #ifdef VM_FAULT_QUICK_DEBUG 960 ++vm_fault_quick_success_count; 961 #endif 962 963 return KERN_SUCCESS; 964 } 965 966 /* 967 * Fault in the specified virtual address in the current process map, 968 * returning a held VM page or NULL. See vm_fault_page() for more 969 * information. 970 * 971 * No requirements. 972 */ 973 vm_page_t 974 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, 975 int *errorp, int *busyp) 976 { 977 struct lwp *lp = curthread->td_lwp; 978 vm_page_t m; 979 980 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va, 981 fault_type, VM_FAULT_NORMAL, 982 errorp, busyp); 983 return(m); 984 } 985 986 /* 987 * Fault in the specified virtual address in the specified map, doing all 988 * necessary manipulation of the object store and all necessary I/O. Return 989 * a held VM page or NULL, and set *errorp. The related pmap is not 990 * updated. 991 * 992 * If busyp is not NULL then *busyp will be set to TRUE if this routine 993 * decides to return a busied page (aka VM_PROT_WRITE), or FALSE if it 994 * does not (VM_PROT_WRITE not specified or busyp is NULL). If busyp is 995 * NULL the returned page is only held. 996 * 997 * If the caller has no intention of writing to the page's contents, busyp 998 * can be passed as NULL along with VM_PROT_WRITE to force a COW operation 999 * without busying the page. 1000 * 1001 * The returned page will also be marked PG_REFERENCED. 1002 * 1003 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an 1004 * error will be returned. 1005 * 1006 * No requirements. 1007 */ 1008 vm_page_t 1009 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 1010 int fault_flags, int *errorp, int *busyp) 1011 { 1012 vm_pindex_t first_pindex; 1013 struct faultstate fs; 1014 int result; 1015 int retry; 1016 int growstack; 1017 int didcow; 1018 vm_prot_t orig_fault_type = fault_type; 1019 1020 retry = 0; 1021 didcow = 0; 1022 fs.hardfault = 0; 1023 fs.fault_flags = fault_flags; 1024 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0); 1025 1026 /* 1027 * Dive the pmap (concurrency possible). If we find the 1028 * appropriate page we can terminate early and quickly. 1029 * 1030 * This works great for normal programs but will always return 1031 * NULL for host lookups of vkernel maps in VMM mode. 1032 * 1033 * NOTE: pmap_fault_page_quick() might not busy the page. If 1034 * VM_PROT_WRITE is set in fault_type and pmap_fault_page_quick() 1035 * returns non-NULL, it will safely dirty the returned vm_page_t 1036 * for us. We cannot safely dirty it here (it might not be 1037 * busy). 1038 */ 1039 fs.m = pmap_fault_page_quick(map->pmap, vaddr, fault_type, busyp); 1040 if (fs.m) { 1041 *errorp = 0; 1042 return(fs.m); 1043 } 1044 1045 /* 1046 * Otherwise take a concurrency hit and do a formal page 1047 * fault. 1048 */ 1049 fs.vp = NULL; 1050 fs.shared = vm_shared_fault; 1051 fs.first_shared = vm_shared_fault; 1052 fs.msoftonly = 0; 1053 growstack = 1; 1054 1055 /* 1056 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object 1057 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but 1058 * we can try shared first. 1059 */ 1060 if (fault_flags & VM_FAULT_UNSWAP) { 1061 fs.first_shared = 0; 1062 } 1063 1064 RetryFault: 1065 /* 1066 * Find the vm_map_entry representing the backing store and resolve 1067 * the top level object and page index. This may have the side 1068 * effect of executing a copy-on-write on the map entry and/or 1069 * creating a shadow object, but will not COW any actual VM pages. 1070 * 1071 * On success fs.map is left read-locked and various other fields 1072 * are initialized but not otherwise referenced or locked. 1073 * 1074 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE 1075 * if the map entry is a virtual page table and also writable, 1076 * so we can set the 'A'accessed bit in the virtual page table 1077 * entry. 1078 */ 1079 fs.map = map; 1080 fs.first_ba_held = 0; 1081 result = vm_map_lookup(&fs.map, vaddr, fault_type, 1082 &fs.entry, &fs.first_ba, 1083 &first_pindex, &fs.first_prot, &fs.wflags); 1084 1085 if (result != KERN_SUCCESS) { 1086 if (result == KERN_FAILURE_NOFAULT) { 1087 *errorp = KERN_FAILURE; 1088 fs.m = NULL; 1089 goto done; 1090 } 1091 if (result != KERN_PROTECTION_FAILURE || 1092 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) 1093 { 1094 if (result == KERN_INVALID_ADDRESS && growstack && 1095 map != &kernel_map && curproc != NULL) { 1096 result = vm_map_growstack(map, vaddr); 1097 if (result == KERN_SUCCESS) { 1098 growstack = 0; 1099 ++retry; 1100 goto RetryFault; 1101 } 1102 result = KERN_FAILURE; 1103 } 1104 fs.m = NULL; 1105 *errorp = result; 1106 goto done; 1107 } 1108 1109 /* 1110 * If we are user-wiring a r/w segment, and it is COW, then 1111 * we need to do the COW operation. Note that we don't 1112 * currently COW RO sections now, because it is NOT desirable 1113 * to COW .text. We simply keep .text from ever being COW'ed 1114 * and take the heat that one cannot debug wired .text sections. 1115 */ 1116 result = vm_map_lookup(&fs.map, vaddr, 1117 VM_PROT_READ|VM_PROT_WRITE| 1118 VM_PROT_OVERRIDE_WRITE, 1119 &fs.entry, &fs.first_ba, 1120 &first_pindex, &fs.first_prot, 1121 &fs.wflags); 1122 if (result != KERN_SUCCESS) { 1123 /* could also be KERN_FAILURE_NOFAULT */ 1124 *errorp = KERN_FAILURE; 1125 fs.m = NULL; 1126 goto done; 1127 } 1128 1129 /* 1130 * If we don't COW now, on a user wire, the user will never 1131 * be able to write to the mapping. If we don't make this 1132 * restriction, the bookkeeping would be nearly impossible. 1133 * 1134 * XXX We have a shared lock, this will have a MP race but 1135 * I don't see how it can hurt anything. 1136 */ 1137 if ((fs.entry->protection & VM_PROT_WRITE) == 0) { 1138 atomic_clear_char(&fs.entry->max_protection, 1139 VM_PROT_WRITE); 1140 } 1141 } 1142 1143 /* 1144 * fs.map is read-locked 1145 * 1146 * Misc checks. Save the map generation number to detect races. 1147 */ 1148 fs.lookup_still_valid = 1; 1149 fs.first_m = NULL; 1150 fs.ba = fs.first_ba; 1151 1152 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 1153 panic("vm_fault: fault on nofault entry, addr: %lx", 1154 (u_long)vaddr); 1155 } 1156 1157 /* 1158 * A user-kernel shared map has no VM object and bypasses 1159 * everything. We execute the uksmap function with a temporary 1160 * fictitious vm_page. The address is directly mapped with no 1161 * management. 1162 */ 1163 if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) { 1164 struct vm_page fakem; 1165 1166 bzero(&fakem, sizeof(fakem)); 1167 fakem.pindex = first_pindex; 1168 fakem.flags = PG_FICTITIOUS | PG_UNMANAGED; 1169 fakem.busy_count = PBUSY_LOCKED; 1170 fakem.valid = VM_PAGE_BITS_ALL; 1171 fakem.pat_mode = VM_MEMATTR_DEFAULT; 1172 if (fs.entry->ba.uksmap(fs.entry->aux.dev, &fakem)) { 1173 *errorp = KERN_FAILURE; 1174 fs.m = NULL; 1175 unlock_things(&fs); 1176 goto done2; 1177 } 1178 fs.m = PHYS_TO_VM_PAGE(fakem.phys_addr); 1179 vm_page_hold(fs.m); 1180 if (busyp) 1181 *busyp = 0; /* don't need to busy R or W */ 1182 unlock_things(&fs); 1183 *errorp = 0; 1184 goto done; 1185 } 1186 1187 1188 /* 1189 * A system map entry may return a NULL object. No object means 1190 * no pager means an unrecoverable kernel fault. 1191 */ 1192 if (fs.first_ba == NULL) { 1193 panic("vm_fault: unrecoverable fault at %p in entry %p", 1194 (void *)vaddr, fs.entry); 1195 } 1196 1197 /* 1198 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT 1199 * is set. 1200 * 1201 * Unfortunately a deadlock can occur if we are forced to page-in 1202 * from swap, but diving all the way into the vm_pager_get_page() 1203 * function to find out is too much. Just check the object type. 1204 */ 1205 if ((curthread->td_flags & TDF_NOFAULT) && 1206 (retry || 1207 fs.first_ba->object->type == OBJT_VNODE || 1208 fs.first_ba->object->type == OBJT_SWAP || 1209 fs.first_ba->backing_ba)) { 1210 *errorp = KERN_FAILURE; 1211 unlock_things(&fs); 1212 fs.m = NULL; 1213 goto done2; 1214 } 1215 1216 /* 1217 * If the entry is wired we cannot change the page protection. 1218 */ 1219 if (fs.wflags & FW_WIRED) 1220 fault_type = fs.first_prot; 1221 1222 /* 1223 * Make a reference to this object to prevent its disposal while we 1224 * are messing with it. Once we have the reference, the map is free 1225 * to be diddled. Since objects reference their shadows (and copies), 1226 * they will stay around as well. 1227 * 1228 * The reference should also prevent an unexpected collapse of the 1229 * parent that might move pages from the current object into the 1230 * parent unexpectedly, resulting in corruption. 1231 * 1232 * Bump the paging-in-progress count to prevent size changes (e.g. 1233 * truncation operations) during I/O. This must be done after 1234 * obtaining the vnode lock in order to avoid possible deadlocks. 1235 */ 1236 if (fs.first_ba->flags & VM_MAP_BACK_EXCL_HEUR) 1237 fs.first_shared = 0; 1238 1239 if (fs.first_shared) 1240 vm_object_hold_shared(fs.first_ba->object); 1241 else 1242 vm_object_hold(fs.first_ba->object); 1243 fs.first_ba_held = 1; 1244 if (fs.vp == NULL) 1245 fs.vp = vnode_pager_lock(fs.first_ba); /* shared */ 1246 1247 /* 1248 * The page we want is at (first_object, first_pindex), but if the 1249 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the 1250 * page table to figure out the actual pindex. 1251 * 1252 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION 1253 * ONLY 1254 */ 1255 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 1256 result = vm_fault_vpagetable(&fs, &first_pindex, 1257 fs.entry->aux.master_pde, 1258 fault_type, 1); 1259 if (result == KERN_TRY_AGAIN) { 1260 ++retry; 1261 goto RetryFault; 1262 } 1263 if (result != KERN_SUCCESS) { 1264 *errorp = result; 1265 fs.m = NULL; 1266 goto done; 1267 } 1268 } 1269 1270 /* 1271 * Now we have the actual (object, pindex), fault in the page. If 1272 * vm_fault_object() fails it will unlock and deallocate the FS 1273 * data. If it succeeds everything remains locked and fs->ba->object 1274 * will have an additinal PIP count if fs->ba != fs->first_ba. 1275 */ 1276 fs.m = NULL; 1277 result = vm_fault_object(&fs, first_pindex, fault_type, 1); 1278 1279 if (result == KERN_TRY_AGAIN) { 1280 KKASSERT(fs.first_ba_held == 0); 1281 ++retry; 1282 didcow |= fs.wflags & FW_DIDCOW; 1283 goto RetryFault; 1284 } 1285 if (result != KERN_SUCCESS) { 1286 *errorp = result; 1287 fs.m = NULL; 1288 goto done; 1289 } 1290 1291 if ((orig_fault_type & VM_PROT_WRITE) && 1292 (fs.prot & VM_PROT_WRITE) == 0) { 1293 *errorp = KERN_PROTECTION_FAILURE; 1294 unlock_things(&fs); 1295 fs.m = NULL; 1296 goto done; 1297 } 1298 1299 /* 1300 * Generally speaking we don't want to update the pmap because 1301 * this routine can be called many times for situations that do 1302 * not require updating the pmap, not to mention the page might 1303 * already be in the pmap. 1304 * 1305 * However, if our vm_map_lookup() results in a COW, we need to 1306 * at least remove the pte from the pmap to guarantee proper 1307 * visibility of modifications made to the process. For example, 1308 * modifications made by vkernel uiocopy/related routines and 1309 * modifications made by ptrace(). 1310 */ 1311 vm_page_flag_set(fs.m, PG_REFERENCED); 1312 #if 0 1313 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, 1314 fs.wflags & FW_WIRED, NULL); 1315 mycpu->gd_cnt.v_vm_faults++; 1316 if (curthread->td_lwp) 1317 ++curthread->td_lwp->lwp_ru.ru_minflt; 1318 #endif 1319 if ((fs.wflags | didcow) | FW_DIDCOW) { 1320 pmap_remove(fs.map->pmap, 1321 vaddr & ~PAGE_MASK, 1322 (vaddr & ~PAGE_MASK) + PAGE_SIZE); 1323 } 1324 1325 /* 1326 * On success vm_fault_object() does not unlock or deallocate, and fs.m 1327 * will contain a busied page. So we must unlock here after having 1328 * messed with the pmap. 1329 */ 1330 unlock_things(&fs); 1331 1332 /* 1333 * Return a held page. We are not doing any pmap manipulation so do 1334 * not set PG_MAPPED. However, adjust the page flags according to 1335 * the fault type because the caller may not use a managed pmapping 1336 * (so we don't want to lose the fact that the page will be dirtied 1337 * if a write fault was specified). 1338 */ 1339 if (fault_type & VM_PROT_WRITE) 1340 vm_page_dirty(fs.m); 1341 vm_page_activate(fs.m); 1342 1343 if (curthread->td_lwp) { 1344 if (fs.hardfault) { 1345 curthread->td_lwp->lwp_ru.ru_majflt++; 1346 } else { 1347 curthread->td_lwp->lwp_ru.ru_minflt++; 1348 } 1349 } 1350 1351 /* 1352 * Unlock everything, and return the held or busied page. 1353 */ 1354 if (busyp) { 1355 if (fault_type & VM_PROT_WRITE) { 1356 vm_page_dirty(fs.m); 1357 *busyp = 1; 1358 } else { 1359 *busyp = 0; 1360 vm_page_hold(fs.m); 1361 vm_page_wakeup(fs.m); 1362 } 1363 } else { 1364 vm_page_hold(fs.m); 1365 vm_page_wakeup(fs.m); 1366 } 1367 /*vm_object_deallocate(fs.first_ba->object);*/ 1368 *errorp = 0; 1369 1370 done: 1371 KKASSERT(fs.first_ba_held == 0); 1372 done2: 1373 return(fs.m); 1374 } 1375 1376 /* 1377 * Fault in the specified (object,offset), dirty the returned page as 1378 * needed. If the requested fault_type cannot be done NULL and an 1379 * error is returned. 1380 * 1381 * A held (but not busied) page is returned. 1382 * 1383 * The passed in object must be held as specified by the shared 1384 * argument. 1385 */ 1386 vm_page_t 1387 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset, 1388 vm_prot_t fault_type, int fault_flags, 1389 int *sharedp, int *errorp) 1390 { 1391 int result; 1392 vm_pindex_t first_pindex; 1393 struct faultstate fs; 1394 struct vm_map_entry entry; 1395 1396 /* 1397 * Since we aren't actually faulting the page into a 1398 * pmap we can just fake the entry.ba. 1399 */ 1400 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1401 bzero(&entry, sizeof(entry)); 1402 entry.maptype = VM_MAPTYPE_NORMAL; 1403 entry.protection = entry.max_protection = fault_type; 1404 entry.ba.backing_ba = NULL; 1405 entry.ba.object = object; 1406 entry.ba.offset = 0; 1407 1408 fs.hardfault = 0; 1409 fs.fault_flags = fault_flags; 1410 fs.map = NULL; 1411 fs.shared = vm_shared_fault; 1412 fs.first_shared = *sharedp; 1413 fs.msoftonly = 0; 1414 fs.vp = NULL; 1415 fs.first_ba_held = -1; /* object held across call, prevent drop */ 1416 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0); 1417 1418 /* 1419 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object 1420 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but 1421 * we can try shared first. 1422 */ 1423 if (fs.first_shared && (fault_flags & VM_FAULT_UNSWAP)) { 1424 fs.first_shared = 0; 1425 vm_object_upgrade(object); 1426 } 1427 1428 /* 1429 * Retry loop as needed (typically for shared->exclusive transitions) 1430 */ 1431 RetryFault: 1432 *sharedp = fs.first_shared; 1433 first_pindex = OFF_TO_IDX(offset); 1434 fs.first_ba = &entry.ba; 1435 fs.ba = fs.first_ba; 1436 fs.entry = &entry; 1437 fs.first_prot = fault_type; 1438 fs.wflags = 0; 1439 1440 /* 1441 * Make a reference to this object to prevent its disposal while we 1442 * are messing with it. Once we have the reference, the map is free 1443 * to be diddled. Since objects reference their shadows (and copies), 1444 * they will stay around as well. 1445 * 1446 * The reference should also prevent an unexpected collapse of the 1447 * parent that might move pages from the current object into the 1448 * parent unexpectedly, resulting in corruption. 1449 * 1450 * Bump the paging-in-progress count to prevent size changes (e.g. 1451 * truncation operations) during I/O. This must be done after 1452 * obtaining the vnode lock in order to avoid possible deadlocks. 1453 */ 1454 if (fs.vp == NULL) 1455 fs.vp = vnode_pager_lock(fs.first_ba); 1456 1457 fs.lookup_still_valid = 1; 1458 fs.first_m = NULL; 1459 1460 #if 0 1461 /* XXX future - ability to operate on VM object using vpagetable */ 1462 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 1463 result = vm_fault_vpagetable(&fs, &first_pindex, 1464 fs.entry->aux.master_pde, 1465 fault_type, 0); 1466 if (result == KERN_TRY_AGAIN) { 1467 if (fs.first_shared == 0 && *sharedp) 1468 vm_object_upgrade(object); 1469 goto RetryFault; 1470 } 1471 if (result != KERN_SUCCESS) { 1472 *errorp = result; 1473 return (NULL); 1474 } 1475 } 1476 #endif 1477 1478 /* 1479 * Now we have the actual (object, pindex), fault in the page. If 1480 * vm_fault_object() fails it will unlock and deallocate the FS 1481 * data. If it succeeds everything remains locked and fs->ba->object 1482 * will have an additinal PIP count if fs->ba != fs->first_ba. 1483 * 1484 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_ba intact. 1485 * We may have to upgrade its lock to handle the requested fault. 1486 */ 1487 result = vm_fault_object(&fs, first_pindex, fault_type, 0); 1488 1489 if (result == KERN_TRY_AGAIN) { 1490 if (fs.first_shared == 0 && *sharedp) 1491 vm_object_upgrade(object); 1492 goto RetryFault; 1493 } 1494 if (result != KERN_SUCCESS) { 1495 *errorp = result; 1496 return(NULL); 1497 } 1498 1499 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) { 1500 *errorp = KERN_PROTECTION_FAILURE; 1501 unlock_things(&fs); 1502 return(NULL); 1503 } 1504 1505 /* 1506 * On success vm_fault_object() does not unlock or deallocate, so we 1507 * do it here. Note that the returned fs.m will be busied. 1508 */ 1509 unlock_things(&fs); 1510 1511 /* 1512 * Return a held page. We are not doing any pmap manipulation so do 1513 * not set PG_MAPPED. However, adjust the page flags according to 1514 * the fault type because the caller may not use a managed pmapping 1515 * (so we don't want to lose the fact that the page will be dirtied 1516 * if a write fault was specified). 1517 */ 1518 vm_page_hold(fs.m); 1519 vm_page_activate(fs.m); 1520 if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY)) 1521 vm_page_dirty(fs.m); 1522 if (fault_flags & VM_FAULT_UNSWAP) 1523 swap_pager_unswapped(fs.m); 1524 1525 /* 1526 * Indicate that the page was accessed. 1527 */ 1528 vm_page_flag_set(fs.m, PG_REFERENCED); 1529 1530 if (curthread->td_lwp) { 1531 if (fs.hardfault) { 1532 curthread->td_lwp->lwp_ru.ru_majflt++; 1533 } else { 1534 curthread->td_lwp->lwp_ru.ru_minflt++; 1535 } 1536 } 1537 1538 /* 1539 * Unlock everything, and return the held page. 1540 */ 1541 vm_page_wakeup(fs.m); 1542 /*vm_object_deallocate(fs.first_ba->object);*/ 1543 1544 *errorp = 0; 1545 return(fs.m); 1546 } 1547 1548 /* 1549 * Translate the virtual page number (first_pindex) that is relative 1550 * to the address space into a logical page number that is relative to the 1551 * backing object. Use the virtual page table pointed to by (vpte). 1552 * 1553 * Possibly downgrade the protection based on the vpte bits. 1554 * 1555 * This implements an N-level page table. Any level can terminate the 1556 * scan by setting VPTE_PS. A linear mapping is accomplished by setting 1557 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP). 1558 */ 1559 static 1560 int 1561 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex, 1562 vpte_t vpte, int fault_type, int allow_nofault) 1563 { 1564 struct lwbuf *lwb; 1565 struct lwbuf lwb_cache; 1566 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */ 1567 int result; 1568 vpte_t *ptep; 1569 1570 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_ba->object)); 1571 for (;;) { 1572 /* 1573 * We cannot proceed if the vpte is not valid, not readable 1574 * for a read fault, not writable for a write fault, or 1575 * not executable for an instruction execution fault. 1576 */ 1577 if ((vpte & VPTE_V) == 0) { 1578 unlock_things(fs); 1579 return (KERN_FAILURE); 1580 } 1581 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW) == 0) { 1582 unlock_things(fs); 1583 return (KERN_FAILURE); 1584 } 1585 if ((fault_type & VM_PROT_EXECUTE) && (vpte & VPTE_NX)) { 1586 unlock_things(fs); 1587 return (KERN_FAILURE); 1588 } 1589 if ((vpte & VPTE_PS) || vshift == 0) 1590 break; 1591 1592 /* 1593 * Get the page table page. Nominally we only read the page 1594 * table, but since we are actively setting VPTE_M and VPTE_A, 1595 * tell vm_fault_object() that we are writing it. 1596 * 1597 * There is currently no real need to optimize this. 1598 */ 1599 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT, 1600 VM_PROT_READ|VM_PROT_WRITE, 1601 allow_nofault); 1602 if (result != KERN_SUCCESS) 1603 return (result); 1604 1605 /* 1606 * Process the returned fs.m and look up the page table 1607 * entry in the page table page. 1608 */ 1609 vshift -= VPTE_PAGE_BITS; 1610 lwb = lwbuf_alloc(fs->m, &lwb_cache); 1611 ptep = ((vpte_t *)lwbuf_kva(lwb) + 1612 ((*pindex >> vshift) & VPTE_PAGE_MASK)); 1613 vm_page_activate(fs->m); 1614 1615 /* 1616 * Page table write-back - entire operation including 1617 * validation of the pte must be atomic to avoid races 1618 * against the vkernel changing the pte. 1619 * 1620 * If the vpte is valid for the* requested operation, do 1621 * a write-back to the page table. 1622 * 1623 * XXX VPTE_M is not set properly for page directory pages. 1624 * It doesn't get set in the page directory if the page table 1625 * is modified during a read access. 1626 */ 1627 for (;;) { 1628 vpte_t nvpte; 1629 1630 /* 1631 * Reload for the cmpset, but make sure the pte is 1632 * still valid. 1633 */ 1634 vpte = *ptep; 1635 cpu_ccfence(); 1636 nvpte = vpte; 1637 1638 if ((vpte & VPTE_V) == 0) 1639 break; 1640 1641 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW)) 1642 nvpte |= VPTE_M | VPTE_A; 1643 if (fault_type & (VM_PROT_READ | VM_PROT_EXECUTE)) 1644 nvpte |= VPTE_A; 1645 if (vpte == nvpte) 1646 break; 1647 if (atomic_cmpset_long(ptep, vpte, nvpte)) { 1648 vm_page_dirty(fs->m); 1649 break; 1650 } 1651 } 1652 lwbuf_free(lwb); 1653 vm_page_flag_set(fs->m, PG_REFERENCED); 1654 vm_page_wakeup(fs->m); 1655 fs->m = NULL; 1656 cleanup_fault(fs); 1657 } 1658 1659 /* 1660 * When the vkernel sets VPTE_RW it expects the real kernel to 1661 * reflect VPTE_M back when the page is modified via the mapping. 1662 * In order to accomplish this the real kernel must map the page 1663 * read-only for read faults and use write faults to reflect VPTE_M 1664 * back. 1665 * 1666 * Once VPTE_M has been set, the real kernel's pte allows writing. 1667 * If the vkernel clears VPTE_M the vkernel must be sure to 1668 * MADV_INVAL the real kernel's mappings to force the real kernel 1669 * to re-fault on the next write so oit can set VPTE_M again. 1670 */ 1671 if ((fault_type & VM_PROT_WRITE) == 0 && 1672 (vpte & (VPTE_RW | VPTE_M)) != (VPTE_RW | VPTE_M)) { 1673 fs->first_prot &= ~VM_PROT_WRITE; 1674 } 1675 1676 /* 1677 * Disable EXECUTE perms if NX bit is set. 1678 */ 1679 if (vpte & VPTE_NX) 1680 fs->first_prot &= ~VM_PROT_EXECUTE; 1681 1682 /* 1683 * Combine remaining address bits with the vpte. 1684 */ 1685 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) + 1686 (*pindex & ((1L << vshift) - 1)); 1687 return (KERN_SUCCESS); 1688 } 1689 1690 1691 /* 1692 * This is the core of the vm_fault code. 1693 * 1694 * Do all operations required to fault-in (fs.first_ba->object, pindex). 1695 * Run through the backing store as necessary and do required COW or virtual 1696 * copy operations. The caller has already fully resolved the vm_map_entry 1697 * and, if appropriate, has created a copy-on-write layer. All we need to 1698 * do is iterate the object chain. 1699 * 1700 * On failure (fs) is unlocked and deallocated and the caller may return or 1701 * retry depending on the failure code. On success (fs) is NOT unlocked or 1702 * deallocated, fs.m will contained a resolved, busied page, and fs.ba's 1703 * object will have an additional PIP count if it is not equal to 1704 * fs.first_ba. 1705 * 1706 * If locks based on fs->first_shared or fs->shared are insufficient, 1707 * clear the appropriate field(s) and return RETRY. COWs require that 1708 * first_shared be 0, while page allocations (or frees) require that 1709 * shared be 0. Renames require that both be 0. 1710 * 1711 * NOTE! fs->[first_]shared might be set with VM_FAULT_DIRTY also set. 1712 * we will have to retry with it exclusive if the vm_page is 1713 * PG_SWAPPED. 1714 * 1715 * fs->first_ba->object must be held on call. 1716 */ 1717 static 1718 int 1719 vm_fault_object(struct faultstate *fs, vm_pindex_t first_pindex, 1720 vm_prot_t fault_type, int allow_nofault) 1721 { 1722 vm_map_backing_t next_ba; 1723 vm_pindex_t pindex; 1724 int error; 1725 1726 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_ba->object)); 1727 fs->prot = fs->first_prot; 1728 pindex = first_pindex; 1729 KKASSERT(fs->ba == fs->first_ba); 1730 1731 vm_object_pip_add(fs->first_ba->object, 1); 1732 1733 /* 1734 * If a read fault occurs we try to upgrade the page protection 1735 * and make it also writable if possible. There are three cases 1736 * where we cannot make the page mapping writable: 1737 * 1738 * (1) The mapping is read-only or the VM object is read-only, 1739 * fs->prot above will simply not have VM_PROT_WRITE set. 1740 * 1741 * (2) If the mapping is a virtual page table fs->first_prot will 1742 * have already been properly adjusted by vm_fault_vpagetable(). 1743 * to detect writes so we can set VPTE_M in the virtual page 1744 * table. Used by vkernels. 1745 * 1746 * (3) If the VM page is read-only or copy-on-write, upgrading would 1747 * just result in an unnecessary COW fault. 1748 * 1749 * (4) If the pmap specifically requests A/M bit emulation, downgrade 1750 * here. 1751 */ 1752 #if 0 1753 /* see vpagetable code */ 1754 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) { 1755 if ((fault_type & VM_PROT_WRITE) == 0) 1756 fs->prot &= ~VM_PROT_WRITE; 1757 } 1758 #endif 1759 1760 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace && 1761 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) { 1762 if ((fault_type & VM_PROT_WRITE) == 0) 1763 fs->prot &= ~VM_PROT_WRITE; 1764 } 1765 1766 /* vm_object_hold(fs->ba->object); implied b/c ba == first_ba */ 1767 1768 for (;;) { 1769 /* 1770 * If the object is dead, we stop here 1771 */ 1772 if (fs->ba->object->flags & OBJ_DEAD) { 1773 vm_object_pip_wakeup(fs->first_ba->object); 1774 unlock_things(fs); 1775 return (KERN_PROTECTION_FAILURE); 1776 } 1777 1778 /* 1779 * See if the page is resident. Wait/Retry if the page is 1780 * busy (lots of stuff may have changed so we can't continue 1781 * in that case). 1782 * 1783 * We can theoretically allow the soft-busy case on a read 1784 * fault if the page is marked valid, but since such 1785 * pages are typically already pmap'd, putting that 1786 * special case in might be more effort then it is 1787 * worth. We cannot under any circumstances mess 1788 * around with a vm_page_t->busy page except, perhaps, 1789 * to pmap it. 1790 */ 1791 fs->m = vm_page_lookup_busy_try(fs->ba->object, pindex, 1792 TRUE, &error); 1793 if (error) { 1794 vm_object_pip_wakeup(fs->first_ba->object); 1795 unlock_things(fs); 1796 vm_page_sleep_busy(fs->m, TRUE, "vmpfw"); 1797 mycpu->gd_cnt.v_intrans++; 1798 fs->m = NULL; 1799 return (KERN_TRY_AGAIN); 1800 } 1801 if (fs->m) { 1802 /* 1803 * The page is busied for us. 1804 * 1805 * If reactivating a page from PQ_CACHE we may have 1806 * to rate-limit. 1807 */ 1808 int queue = fs->m->queue; 1809 vm_page_unqueue_nowakeup(fs->m); 1810 1811 if ((queue - fs->m->pc) == PQ_CACHE && 1812 vm_page_count_severe()) { 1813 vm_page_activate(fs->m); 1814 vm_page_wakeup(fs->m); 1815 fs->m = NULL; 1816 vm_object_pip_wakeup(fs->first_ba->object); 1817 unlock_things(fs); 1818 if (allow_nofault == 0 || 1819 (curthread->td_flags & TDF_NOFAULT) == 0) { 1820 thread_t td; 1821 1822 vm_wait_pfault(); 1823 td = curthread; 1824 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL)) 1825 return (KERN_PROTECTION_FAILURE); 1826 } 1827 return (KERN_TRY_AGAIN); 1828 } 1829 1830 /* 1831 * If it still isn't completely valid (readable), 1832 * or if a read-ahead-mark is set on the VM page, 1833 * jump to readrest, else we found the page and 1834 * can return. 1835 * 1836 * We can release the spl once we have marked the 1837 * page busy. 1838 */ 1839 if (fs->m->object != &kernel_object) { 1840 if ((fs->m->valid & VM_PAGE_BITS_ALL) != 1841 VM_PAGE_BITS_ALL) { 1842 goto readrest; 1843 } 1844 if (fs->m->flags & PG_RAM) { 1845 if (debug_cluster) 1846 kprintf("R"); 1847 vm_page_flag_clear(fs->m, PG_RAM); 1848 goto readrest; 1849 } 1850 } 1851 fs->first_ba->flags &= ~VM_MAP_BACK_EXCL_HEUR; 1852 break; /* break to PAGE HAS BEEN FOUND */ 1853 } 1854 1855 /* 1856 * Page is not resident, If this is the search termination 1857 * or the pager might contain the page, allocate a new page. 1858 */ 1859 if (TRYPAGER(fs) || fs->ba == fs->first_ba) { 1860 /* 1861 * If this is a SWAP object we can use the shared 1862 * lock to check existence of a swap block. If 1863 * there isn't one we can skip to the next object. 1864 * 1865 * However, if this is the first object we allocate 1866 * a page now just in case we need to copy to it 1867 * later. 1868 */ 1869 if (fs->ba != fs->first_ba && 1870 fs->ba->object->type == OBJT_SWAP) { 1871 if (swap_pager_haspage_locked(fs->ba->object, 1872 pindex) == 0) { 1873 goto next; 1874 } 1875 } 1876 1877 /* 1878 * Allocating, must be exclusive. 1879 */ 1880 fs->first_ba->flags |= VM_MAP_BACK_EXCL_HEUR; 1881 if (fs->ba == fs->first_ba && fs->first_shared) { 1882 fs->first_shared = 0; 1883 vm_object_pip_wakeup(fs->first_ba->object); 1884 unlock_things(fs); 1885 return (KERN_TRY_AGAIN); 1886 } 1887 if (fs->ba != fs->first_ba && fs->shared) { 1888 fs->first_shared = 0; 1889 fs->shared = 0; 1890 vm_object_pip_wakeup(fs->first_ba->object); 1891 unlock_things(fs); 1892 return (KERN_TRY_AGAIN); 1893 } 1894 1895 /* 1896 * If the page is beyond the object size we fail 1897 */ 1898 if (pindex >= fs->ba->object->size) { 1899 vm_object_pip_wakeup(fs->first_ba->object); 1900 unlock_things(fs); 1901 return (KERN_PROTECTION_FAILURE); 1902 } 1903 1904 /* 1905 * Allocate a new page for this object/offset pair. 1906 * 1907 * It is possible for the allocation to race, so 1908 * handle the case. 1909 */ 1910 fs->m = NULL; 1911 if (!vm_page_count_severe()) { 1912 fs->m = vm_page_alloc(fs->ba->object, pindex, 1913 ((fs->vp || fs->ba->backing_ba) ? 1914 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL : 1915 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL | 1916 VM_ALLOC_USE_GD | VM_ALLOC_ZERO)); 1917 } 1918 if (fs->m == NULL) { 1919 vm_object_pip_wakeup(fs->first_ba->object); 1920 unlock_things(fs); 1921 if (allow_nofault == 0 || 1922 (curthread->td_flags & TDF_NOFAULT) == 0) { 1923 thread_t td; 1924 1925 vm_wait_pfault(); 1926 td = curthread; 1927 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL)) 1928 return (KERN_PROTECTION_FAILURE); 1929 } 1930 return (KERN_TRY_AGAIN); 1931 } 1932 1933 /* 1934 * Fall through to readrest. We have a new page which 1935 * will have to be paged (since m->valid will be 0). 1936 */ 1937 } 1938 1939 readrest: 1940 /* 1941 * We have found an invalid or partially valid page, a 1942 * page with a read-ahead mark which might be partially or 1943 * fully valid (and maybe dirty too), or we have allocated 1944 * a new page. 1945 * 1946 * Attempt to fault-in the page if there is a chance that the 1947 * pager has it, and potentially fault in additional pages 1948 * at the same time. 1949 * 1950 * If TRYPAGER is true then fs.m will be non-NULL and busied 1951 * for us. 1952 */ 1953 if (TRYPAGER(fs)) { 1954 u_char behavior = vm_map_entry_behavior(fs->entry); 1955 vm_object_t object; 1956 vm_page_t first_m; 1957 int seqaccess; 1958 int rv; 1959 1960 if (behavior == MAP_ENTRY_BEHAV_RANDOM) 1961 seqaccess = 0; 1962 else 1963 seqaccess = -1; 1964 1965 /* 1966 * Doing I/O may synchronously insert additional 1967 * pages so we can't be shared at this point either. 1968 * 1969 * NOTE: We can't free fs->m here in the allocated 1970 * case (fs->ba != fs->first_ba) as this 1971 * would require an exclusively locked 1972 * VM object. 1973 */ 1974 if (fs->ba == fs->first_ba && fs->first_shared) { 1975 vm_page_deactivate(fs->m); 1976 vm_page_wakeup(fs->m); 1977 fs->m = NULL; 1978 fs->first_shared = 0; 1979 vm_object_pip_wakeup(fs->first_ba->object); 1980 unlock_things(fs); 1981 return (KERN_TRY_AGAIN); 1982 } 1983 if (fs->ba != fs->first_ba && fs->shared) { 1984 vm_page_deactivate(fs->m); 1985 vm_page_wakeup(fs->m); 1986 fs->m = NULL; 1987 fs->first_shared = 0; 1988 fs->shared = 0; 1989 vm_object_pip_wakeup(fs->first_ba->object); 1990 unlock_things(fs); 1991 return (KERN_TRY_AGAIN); 1992 } 1993 1994 object = fs->ba->object; 1995 first_m = NULL; 1996 1997 /* object is held, no more access to entry or ba's */ 1998 1999 /* 2000 * Acquire the page data. We still hold object 2001 * and the page has been BUSY's. 2002 * 2003 * We own the page, but we must re-issue the lookup 2004 * because the pager may have replaced it (for example, 2005 * in order to enter a fictitious page into the 2006 * object). In this situation the pager will have 2007 * cleaned up the old page and left the new one 2008 * busy for us. 2009 * 2010 * If we got here through a PG_RAM read-ahead 2011 * mark the page may be partially dirty and thus 2012 * not freeable. Don't bother checking to see 2013 * if the pager has the page because we can't free 2014 * it anyway. We have to depend on the get_page 2015 * operation filling in any gaps whether there is 2016 * backing store or not. 2017 * 2018 * We must dispose of the page (fs->m) and also 2019 * possibly first_m (the fronting layer). If 2020 * this is a write fault leave the page intact 2021 * because we will probably have to copy fs->m 2022 * to fs->first_m on the retry. If this is a 2023 * read fault we probably won't need the page. 2024 */ 2025 rv = vm_pager_get_page(object, &fs->m, seqaccess); 2026 2027 if (rv == VM_PAGER_OK) { 2028 ++fs->hardfault; 2029 fs->m = vm_page_lookup(object, pindex); 2030 if (fs->m) { 2031 vm_page_activate(fs->m); 2032 vm_page_wakeup(fs->m); 2033 fs->m = NULL; 2034 } 2035 2036 if (fs->m) { 2037 /* have page */ 2038 break; 2039 } 2040 vm_object_pip_wakeup(fs->first_ba->object); 2041 unlock_things(fs); 2042 return (KERN_TRY_AGAIN); 2043 } 2044 2045 /* 2046 * If the pager doesn't have the page, continue on 2047 * to the next object. Retain the vm_page if this 2048 * is the first object, we may need to copy into 2049 * it later. 2050 */ 2051 if (rv == VM_PAGER_FAIL) { 2052 if (fs->ba != fs->first_ba) { 2053 vm_page_free(fs->m); 2054 fs->m = NULL; 2055 } 2056 goto next; 2057 } 2058 2059 /* 2060 * Remove the bogus page (which does not exist at this 2061 * object/offset). 2062 * 2063 * Also wake up any other process that may want to bring 2064 * in this page. 2065 * 2066 * If this is the top-level object, we must leave the 2067 * busy page to prevent another process from rushing 2068 * past us, and inserting the page in that object at 2069 * the same time that we are. 2070 */ 2071 if (rv == VM_PAGER_ERROR) { 2072 if (curproc) { 2073 kprintf("vm_fault: pager read error, " 2074 "pid %d (%s)\n", 2075 curproc->p_pid, 2076 curproc->p_comm); 2077 } else { 2078 kprintf("vm_fault: pager read error, " 2079 "thread %p (%s)\n", 2080 curthread, 2081 curthread->td_comm); 2082 } 2083 } 2084 2085 /* 2086 * I/O error or data outside pager's range. 2087 */ 2088 if (fs->m) { 2089 vnode_pager_freepage(fs->m); 2090 fs->m = NULL; 2091 } 2092 if (first_m) { 2093 vm_page_free(first_m); 2094 first_m = NULL; /* safety */ 2095 } 2096 vm_object_pip_wakeup(object); 2097 unlock_things(fs); 2098 2099 switch(rv) { 2100 case VM_PAGER_ERROR: 2101 return (KERN_FAILURE); 2102 case VM_PAGER_BAD: 2103 return (KERN_PROTECTION_FAILURE); 2104 default: 2105 return (KERN_PROTECTION_FAILURE); 2106 } 2107 2108 #if 0 2109 /* 2110 * Data outside the range of the pager or an I/O error 2111 * 2112 * The page may have been wired during the pagein, 2113 * e.g. by the buffer cache, and cannot simply be 2114 * freed. Call vnode_pager_freepage() to deal with it. 2115 * 2116 * The object is not held shared so we can safely 2117 * free the page. 2118 */ 2119 if (fs->ba != fs->first_ba) { 2120 2121 /* 2122 * XXX - we cannot just fall out at this 2123 * point, m has been freed and is invalid! 2124 */ 2125 } 2126 2127 /* 2128 * XXX - the check for kernel_map is a kludge to work 2129 * around having the machine panic on a kernel space 2130 * fault w/ I/O error. 2131 */ 2132 if (((fs->map != &kernel_map) && 2133 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) { 2134 if (fs->m) { 2135 /* from just above */ 2136 KKASSERT(fs->first_shared == 0); 2137 vnode_pager_freepage(fs->m); 2138 fs->m = NULL; 2139 } 2140 /* NOT REACHED */ 2141 } 2142 #endif 2143 } 2144 2145 next: 2146 /* 2147 * We get here if the object has a default pager (or unwiring) 2148 * or the pager doesn't have the page. 2149 * 2150 * fs->first_m will be used for the COW unless we find a 2151 * deeper page to be mapped read-only, in which case the 2152 * unlock*(fs) will free first_m. 2153 */ 2154 if (fs->ba == fs->first_ba) 2155 fs->first_m = fs->m; 2156 2157 /* 2158 * Move on to the next object. The chain lock should prevent 2159 * the backing_object from getting ripped out from under us. 2160 * 2161 * The object lock for the next object is governed by 2162 * fs->shared. 2163 */ 2164 if ((next_ba = fs->ba->backing_ba) != NULL) { 2165 if (fs->shared) 2166 vm_object_hold_shared(next_ba->object); 2167 else 2168 vm_object_hold(next_ba->object); 2169 KKASSERT(next_ba == fs->ba->backing_ba); 2170 pindex += OFF_TO_IDX(next_ba->offset); 2171 } 2172 2173 if (next_ba == NULL) { 2174 /* 2175 * If there's no object left, fill the page in the top 2176 * object with zeros. 2177 */ 2178 if (fs->ba != fs->first_ba) { 2179 vm_object_pip_wakeup(fs->ba->object); 2180 vm_object_drop(fs->ba->object); 2181 fs->ba = fs->first_ba; 2182 pindex = first_pindex; 2183 fs->m = fs->first_m; 2184 } 2185 fs->first_m = NULL; 2186 2187 /* 2188 * Zero the page and mark it valid. 2189 */ 2190 vm_page_zero_fill(fs->m); 2191 mycpu->gd_cnt.v_zfod++; 2192 fs->m->valid = VM_PAGE_BITS_ALL; 2193 break; /* break to PAGE HAS BEEN FOUND */ 2194 } 2195 if (fs->ba != fs->first_ba) { 2196 vm_object_pip_wakeup(fs->ba->object); 2197 vm_object_lock_swap(); /* flip ba/next_ba */ 2198 vm_object_drop(fs->ba->object); 2199 } 2200 fs->ba = next_ba; 2201 vm_object_pip_add(next_ba->object, 1); 2202 } 2203 2204 /* 2205 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 2206 * is held.] 2207 * 2208 * object still held. 2209 * vm_map may not be locked (determined by fs->lookup_still_valid) 2210 * 2211 * local shared variable may be different from fs->shared. 2212 * 2213 * If the page is being written, but isn't already owned by the 2214 * top-level object, we have to copy it into a new page owned by the 2215 * top-level object. 2216 */ 2217 KASSERT((fs->m->busy_count & PBUSY_LOCKED) != 0, 2218 ("vm_fault: not busy after main loop")); 2219 2220 if (fs->ba != fs->first_ba) { 2221 /* 2222 * We only really need to copy if we want to write it. 2223 */ 2224 if (fault_type & VM_PROT_WRITE) { 2225 #if 0 2226 /* CODE REFACTOR IN PROGRESS, REMOVE OPTIMIZATION */ 2227 /* 2228 * This allows pages to be virtually copied from a 2229 * backing_object into the first_object, where the 2230 * backing object has no other refs to it, and cannot 2231 * gain any more refs. Instead of a bcopy, we just 2232 * move the page from the backing object to the 2233 * first object. Note that we must mark the page 2234 * dirty in the first object so that it will go out 2235 * to swap when needed. 2236 */ 2237 if (virtual_copy_ok(fs)) { 2238 /* 2239 * (first_m) and (m) are both busied. We have 2240 * move (m) into (first_m)'s object/pindex 2241 * in an atomic fashion, then free (first_m). 2242 * 2243 * first_object is held so second remove 2244 * followed by the rename should wind 2245 * up being atomic. vm_page_free() might 2246 * block so we don't do it until after the 2247 * rename. 2248 */ 2249 vm_page_protect(fs->first_m, VM_PROT_NONE); 2250 vm_page_remove(fs->first_m); 2251 vm_page_rename(fs->m, 2252 fs->first_ba->object, 2253 first_pindex); 2254 vm_page_free(fs->first_m); 2255 fs->first_m = fs->m; 2256 fs->m = NULL; 2257 mycpu->gd_cnt.v_cow_optim++; 2258 } else 2259 #endif 2260 { 2261 /* 2262 * Oh, well, lets copy it. 2263 * 2264 * Why are we unmapping the original page 2265 * here? Well, in short, not all accessors 2266 * of user memory go through the pmap. The 2267 * procfs code doesn't have access user memory 2268 * via a local pmap, so vm_fault_page*() 2269 * can't call pmap_enter(). And the umtx*() 2270 * code may modify the COW'd page via a DMAP 2271 * or kernel mapping and not via the pmap, 2272 * leaving the original page still mapped 2273 * read-only into the pmap. 2274 * 2275 * So we have to remove the page from at 2276 * least the current pmap if it is in it. 2277 * 2278 * We used to just remove it from all pmaps 2279 * but that creates inefficiencies on SMP, 2280 * particularly for COW program & library 2281 * mappings that are concurrently exec'd. 2282 * Only remove the page from the current 2283 * pmap. 2284 */ 2285 KKASSERT(fs->first_shared == 0); 2286 vm_page_copy(fs->m, fs->first_m); 2287 /*vm_page_protect(fs->m, VM_PROT_NONE);*/ 2288 pmap_remove_specific( 2289 &curthread->td_lwp->lwp_vmspace->vm_pmap, 2290 fs->m); 2291 } 2292 2293 /* 2294 * We no longer need the old page or object. 2295 */ 2296 if (fs->m) 2297 release_page(fs); 2298 2299 /* 2300 * fs->ba != fs->first_ba due to above conditional 2301 */ 2302 vm_object_pip_wakeup(fs->ba->object); 2303 vm_object_drop(fs->ba->object); 2304 fs->ba = fs->first_ba; 2305 2306 /* 2307 * Only use the new page below... 2308 */ 2309 mycpu->gd_cnt.v_cow_faults++; 2310 fs->m = fs->first_m; 2311 pindex = first_pindex; 2312 } else { 2313 /* 2314 * If it wasn't a write fault avoid having to copy 2315 * the page by mapping it read-only from backing 2316 * store. The process is not allowed to modify 2317 * backing pages. 2318 */ 2319 fs->prot &= ~VM_PROT_WRITE; 2320 } 2321 } 2322 2323 /* 2324 * Relock the map if necessary, then check the generation count. 2325 * relock_map() will update fs->timestamp to account for the 2326 * relocking if necessary. 2327 * 2328 * If the count has changed after relocking then all sorts of 2329 * crap may have happened and we have to retry. 2330 * 2331 * NOTE: The relock_map() can fail due to a deadlock against 2332 * the vm_page we are holding BUSY. 2333 */ 2334 KKASSERT(fs->lookup_still_valid != 0); 2335 #if 0 2336 if (fs->lookup_still_valid == 0 && fs->map) { 2337 if (relock_map(fs) || 2338 fs->map->timestamp != fs->map_generation) { 2339 release_page(fs); 2340 vm_object_pip_wakeup(fs->first_ba->object); 2341 unlock_things(fs); 2342 return (KERN_TRY_AGAIN); 2343 } 2344 } 2345 #endif 2346 2347 /* 2348 * If the fault is a write, we know that this page is being 2349 * written NOW so dirty it explicitly to save on pmap_is_modified() 2350 * calls later. 2351 * 2352 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC 2353 * if the page is already dirty to prevent data written with 2354 * the expectation of being synced from not being synced. 2355 * Likewise if this entry does not request NOSYNC then make 2356 * sure the page isn't marked NOSYNC. Applications sharing 2357 * data should use the same flags to avoid ping ponging. 2358 * 2359 * Also tell the backing pager, if any, that it should remove 2360 * any swap backing since the page is now dirty. 2361 */ 2362 vm_page_activate(fs->m); 2363 if (fs->prot & VM_PROT_WRITE) { 2364 vm_object_set_writeable_dirty(fs->m->object); 2365 vm_set_nosync(fs->m, fs->entry); 2366 if (fs->fault_flags & VM_FAULT_DIRTY) { 2367 vm_page_dirty(fs->m); 2368 if (fs->m->flags & PG_SWAPPED) { 2369 /* 2370 * If the page is swapped out we have to call 2371 * swap_pager_unswapped() which requires an 2372 * exclusive object lock. If we are shared, 2373 * we must clear the shared flag and retry. 2374 */ 2375 if ((fs->ba == fs->first_ba && 2376 fs->first_shared) || 2377 (fs->ba != fs->first_ba && fs->shared)) { 2378 vm_page_wakeup(fs->m); 2379 fs->m = NULL; 2380 if (fs->ba == fs->first_ba) 2381 fs->first_shared = 0; 2382 else 2383 fs->shared = 0; 2384 vm_object_pip_wakeup( 2385 fs->first_ba->object); 2386 unlock_things(fs); 2387 return (KERN_TRY_AGAIN); 2388 } 2389 swap_pager_unswapped(fs->m); 2390 } 2391 } 2392 } 2393 2394 /* 2395 * We found our page at backing layer ba. Leave the layer state 2396 * intact. 2397 */ 2398 2399 vm_object_pip_wakeup(fs->first_ba->object); 2400 #if 0 2401 if (fs->ba != fs->first_ba) 2402 vm_object_drop(fs->ba->object); 2403 #endif 2404 2405 /* 2406 * Page had better still be busy. We are still locked up and 2407 * fs->ba->object will have another PIP reference for the case 2408 * where fs->ba != fs->first_ba. 2409 */ 2410 KASSERT(fs->m->busy_count & PBUSY_LOCKED, 2411 ("vm_fault: page %p not busy!", fs->m)); 2412 2413 /* 2414 * Sanity check: page must be completely valid or it is not fit to 2415 * map into user space. vm_pager_get_pages() ensures this. 2416 */ 2417 if (fs->m->valid != VM_PAGE_BITS_ALL) { 2418 vm_page_zero_invalid(fs->m, TRUE); 2419 kprintf("Warning: page %p partially invalid on fault\n", fs->m); 2420 } 2421 2422 return (KERN_SUCCESS); 2423 } 2424 2425 /* 2426 * Wire down a range of virtual addresses in a map. The entry in question 2427 * should be marked in-transition and the map must be locked. We must 2428 * release the map temporarily while faulting-in the page to avoid a 2429 * deadlock. Note that the entry may be clipped while we are blocked but 2430 * will never be freed. 2431 * 2432 * map must be locked on entry. 2433 */ 2434 int 2435 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, 2436 boolean_t user_wire, int kmflags) 2437 { 2438 boolean_t fictitious; 2439 vm_offset_t start; 2440 vm_offset_t end; 2441 vm_offset_t va; 2442 pmap_t pmap; 2443 int rv; 2444 int wire_prot; 2445 int fault_flags; 2446 vm_page_t m; 2447 2448 if (user_wire) { 2449 wire_prot = VM_PROT_READ; 2450 fault_flags = VM_FAULT_USER_WIRE; 2451 } else { 2452 wire_prot = VM_PROT_READ | VM_PROT_WRITE; 2453 fault_flags = VM_FAULT_CHANGE_WIRING; 2454 } 2455 if (kmflags & KM_NOTLBSYNC) 2456 wire_prot |= VM_PROT_NOSYNC; 2457 2458 pmap = vm_map_pmap(map); 2459 start = entry->start; 2460 end = entry->end; 2461 2462 switch(entry->maptype) { 2463 case VM_MAPTYPE_NORMAL: 2464 case VM_MAPTYPE_VPAGETABLE: 2465 fictitious = entry->ba.object && 2466 ((entry->ba.object->type == OBJT_DEVICE) || 2467 (entry->ba.object->type == OBJT_MGTDEVICE)); 2468 break; 2469 case VM_MAPTYPE_UKSMAP: 2470 fictitious = TRUE; 2471 break; 2472 default: 2473 fictitious = FALSE; 2474 break; 2475 } 2476 2477 if (entry->eflags & MAP_ENTRY_KSTACK) 2478 start += PAGE_SIZE; 2479 map->timestamp++; 2480 vm_map_unlock(map); 2481 2482 /* 2483 * We simulate a fault to get the page and enter it in the physical 2484 * map. 2485 */ 2486 for (va = start; va < end; va += PAGE_SIZE) { 2487 rv = vm_fault(map, va, wire_prot, fault_flags); 2488 if (rv) { 2489 while (va > start) { 2490 va -= PAGE_SIZE; 2491 m = pmap_unwire(pmap, va); 2492 if (m && !fictitious) { 2493 vm_page_busy_wait(m, FALSE, "vmwrpg"); 2494 vm_page_unwire(m, 1); 2495 vm_page_wakeup(m); 2496 } 2497 } 2498 goto done; 2499 } 2500 } 2501 rv = KERN_SUCCESS; 2502 done: 2503 vm_map_lock(map); 2504 2505 return (rv); 2506 } 2507 2508 /* 2509 * Unwire a range of virtual addresses in a map. The map should be 2510 * locked. 2511 */ 2512 void 2513 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry) 2514 { 2515 boolean_t fictitious; 2516 vm_offset_t start; 2517 vm_offset_t end; 2518 vm_offset_t va; 2519 pmap_t pmap; 2520 vm_page_t m; 2521 2522 pmap = vm_map_pmap(map); 2523 start = entry->start; 2524 end = entry->end; 2525 fictitious = entry->ba.object && 2526 ((entry->ba.object->type == OBJT_DEVICE) || 2527 (entry->ba.object->type == OBJT_MGTDEVICE)); 2528 if (entry->eflags & MAP_ENTRY_KSTACK) 2529 start += PAGE_SIZE; 2530 2531 /* 2532 * Since the pages are wired down, we must be able to get their 2533 * mappings from the physical map system. 2534 */ 2535 for (va = start; va < end; va += PAGE_SIZE) { 2536 m = pmap_unwire(pmap, va); 2537 if (m && !fictitious) { 2538 vm_page_busy_wait(m, FALSE, "vmwrpg"); 2539 vm_page_unwire(m, 1); 2540 vm_page_wakeup(m); 2541 } 2542 } 2543 } 2544 2545 /* 2546 * Simulate write faults to bring all data into the head object, return 2547 * KERN_SUCCESS on success (which should be always unless the system runs 2548 * out of memory). 2549 * 2550 * The caller will handle destroying the backing_ba's. 2551 */ 2552 int 2553 vm_fault_collapse(vm_map_t map, vm_map_entry_t entry) 2554 { 2555 struct faultstate fs; 2556 vm_ooffset_t scan; 2557 vm_pindex_t pindex; 2558 vm_object_t object; 2559 int rv; 2560 int all_shadowed; 2561 2562 bzero(&fs, sizeof(fs)); 2563 object = entry->ba.object; 2564 2565 fs.first_prot = entry->max_protection | /* optional VM_PROT_EXECUTE */ 2566 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE; 2567 fs.fault_flags = VM_FAULT_NORMAL; 2568 fs.map = map; 2569 fs.entry = entry; 2570 fs.lookup_still_valid = -1; /* leave map atomically locked */ 2571 fs.first_ba = &entry->ba; 2572 fs.first_ba_held = -1; /* leave object held */ 2573 2574 /* fs.hardfault */ 2575 2576 vm_object_hold(object); 2577 rv = KERN_SUCCESS; 2578 2579 scan = entry->start; 2580 all_shadowed = 1; 2581 2582 while (scan < entry->end) { 2583 pindex = OFF_TO_IDX(entry->ba.offset + (scan - entry->start)); 2584 2585 if (vm_page_lookup(object, pindex)) { 2586 scan += PAGE_SIZE; 2587 continue; 2588 } 2589 2590 all_shadowed = 0; 2591 fs.ba = fs.first_ba; 2592 fs.prot = fs.first_prot; 2593 2594 rv = vm_fault_object(&fs, pindex, fs.first_prot, 1); 2595 if (rv == KERN_TRY_AGAIN) 2596 continue; 2597 if (rv != KERN_SUCCESS) 2598 break; 2599 vm_page_flag_set(fs.m, PG_REFERENCED); 2600 vm_page_activate(fs.m); 2601 vm_page_wakeup(fs.m); 2602 scan += PAGE_SIZE; 2603 } 2604 KKASSERT(entry->ba.object == object); 2605 vm_object_drop(object); 2606 2607 /* 2608 * If the fronting object did not have every page we have to clear 2609 * the pmap range due to the pages being changed so we can fault-in 2610 * the proper pages. 2611 */ 2612 if (all_shadowed == 0) 2613 pmap_remove(map->pmap, entry->start, entry->end); 2614 2615 return rv; 2616 } 2617 2618 /* 2619 * Copy all of the pages from one map entry to another. If the source 2620 * is wired down we just use vm_page_lookup(). If not we use 2621 * vm_fault_object(). 2622 * 2623 * The source and destination maps must be locked for write. 2624 * The source and destination maps token must be held 2625 * 2626 * No other requirements. 2627 * 2628 * XXX do segment optimization 2629 */ 2630 void 2631 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 2632 vm_map_entry_t dst_entry, vm_map_entry_t src_entry) 2633 { 2634 vm_object_t dst_object; 2635 vm_object_t src_object; 2636 vm_ooffset_t dst_offset; 2637 vm_ooffset_t src_offset; 2638 vm_prot_t prot; 2639 vm_offset_t vaddr; 2640 vm_page_t dst_m; 2641 vm_page_t src_m; 2642 2643 src_object = src_entry->ba.object; 2644 src_offset = src_entry->ba.offset; 2645 2646 /* 2647 * Create the top-level object for the destination entry. (Doesn't 2648 * actually shadow anything - we copy the pages directly.) 2649 */ 2650 vm_map_entry_allocate_object(dst_entry); 2651 dst_object = dst_entry->ba.object; 2652 2653 prot = dst_entry->max_protection; 2654 2655 /* 2656 * Loop through all of the pages in the entry's range, copying each 2657 * one from the source object (it should be there) to the destination 2658 * object. 2659 */ 2660 vm_object_hold(src_object); 2661 vm_object_hold(dst_object); 2662 2663 for (vaddr = dst_entry->start, dst_offset = 0; 2664 vaddr < dst_entry->end; 2665 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) { 2666 2667 /* 2668 * Allocate a page in the destination object 2669 */ 2670 do { 2671 dst_m = vm_page_alloc(dst_object, 2672 OFF_TO_IDX(dst_offset), 2673 VM_ALLOC_NORMAL); 2674 if (dst_m == NULL) { 2675 vm_wait(0); 2676 } 2677 } while (dst_m == NULL); 2678 2679 /* 2680 * Find the page in the source object, and copy it in. 2681 * (Because the source is wired down, the page will be in 2682 * memory.) 2683 */ 2684 src_m = vm_page_lookup(src_object, 2685 OFF_TO_IDX(dst_offset + src_offset)); 2686 if (src_m == NULL) 2687 panic("vm_fault_copy_wired: page missing"); 2688 2689 vm_page_copy(src_m, dst_m); 2690 2691 /* 2692 * Enter it in the pmap... 2693 */ 2694 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE, dst_entry); 2695 2696 /* 2697 * Mark it no longer busy, and put it on the active list. 2698 */ 2699 vm_page_activate(dst_m); 2700 vm_page_wakeup(dst_m); 2701 } 2702 vm_object_drop(dst_object); 2703 vm_object_drop(src_object); 2704 } 2705 2706 #if 0 2707 2708 /* 2709 * This routine checks around the requested page for other pages that 2710 * might be able to be faulted in. This routine brackets the viable 2711 * pages for the pages to be paged in. 2712 * 2713 * Inputs: 2714 * m, rbehind, rahead 2715 * 2716 * Outputs: 2717 * marray (array of vm_page_t), reqpage (index of requested page) 2718 * 2719 * Return value: 2720 * number of pages in marray 2721 */ 2722 static int 2723 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead, 2724 vm_page_t *marray, int *reqpage) 2725 { 2726 int i,j; 2727 vm_object_t object; 2728 vm_pindex_t pindex, startpindex, endpindex, tpindex; 2729 vm_page_t rtm; 2730 int cbehind, cahead; 2731 2732 object = m->object; 2733 pindex = m->pindex; 2734 2735 /* 2736 * we don't fault-ahead for device pager 2737 */ 2738 if ((object->type == OBJT_DEVICE) || 2739 (object->type == OBJT_MGTDEVICE)) { 2740 *reqpage = 0; 2741 marray[0] = m; 2742 return 1; 2743 } 2744 2745 /* 2746 * if the requested page is not available, then give up now 2747 */ 2748 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) { 2749 *reqpage = 0; /* not used by caller, fix compiler warn */ 2750 return 0; 2751 } 2752 2753 if ((cbehind == 0) && (cahead == 0)) { 2754 *reqpage = 0; 2755 marray[0] = m; 2756 return 1; 2757 } 2758 2759 if (rahead > cahead) { 2760 rahead = cahead; 2761 } 2762 2763 if (rbehind > cbehind) { 2764 rbehind = cbehind; 2765 } 2766 2767 /* 2768 * Do not do any readahead if we have insufficient free memory. 2769 * 2770 * XXX code was broken disabled before and has instability 2771 * with this conditonal fixed, so shortcut for now. 2772 */ 2773 if (burst_fault == 0 || vm_page_count_severe()) { 2774 marray[0] = m; 2775 *reqpage = 0; 2776 return 1; 2777 } 2778 2779 /* 2780 * scan backward for the read behind pages -- in memory 2781 * 2782 * Assume that if the page is not found an interrupt will not 2783 * create it. Theoretically interrupts can only remove (busy) 2784 * pages, not create new associations. 2785 */ 2786 if (pindex > 0) { 2787 if (rbehind > pindex) { 2788 rbehind = pindex; 2789 startpindex = 0; 2790 } else { 2791 startpindex = pindex - rbehind; 2792 } 2793 2794 vm_object_hold(object); 2795 for (tpindex = pindex; tpindex > startpindex; --tpindex) { 2796 if (vm_page_lookup(object, tpindex - 1)) 2797 break; 2798 } 2799 2800 i = 0; 2801 while (tpindex < pindex) { 2802 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM | 2803 VM_ALLOC_NULL_OK); 2804 if (rtm == NULL) { 2805 for (j = 0; j < i; j++) { 2806 vm_page_free(marray[j]); 2807 } 2808 vm_object_drop(object); 2809 marray[0] = m; 2810 *reqpage = 0; 2811 return 1; 2812 } 2813 marray[i] = rtm; 2814 ++i; 2815 ++tpindex; 2816 } 2817 vm_object_drop(object); 2818 } else { 2819 i = 0; 2820 } 2821 2822 /* 2823 * Assign requested page 2824 */ 2825 marray[i] = m; 2826 *reqpage = i; 2827 ++i; 2828 2829 /* 2830 * Scan forwards for read-ahead pages 2831 */ 2832 tpindex = pindex + 1; 2833 endpindex = tpindex + rahead; 2834 if (endpindex > object->size) 2835 endpindex = object->size; 2836 2837 vm_object_hold(object); 2838 while (tpindex < endpindex) { 2839 if (vm_page_lookup(object, tpindex)) 2840 break; 2841 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM | 2842 VM_ALLOC_NULL_OK); 2843 if (rtm == NULL) 2844 break; 2845 marray[i] = rtm; 2846 ++i; 2847 ++tpindex; 2848 } 2849 vm_object_drop(object); 2850 2851 return (i); 2852 } 2853 2854 #endif 2855 2856 /* 2857 * vm_prefault() provides a quick way of clustering pagefaults into a 2858 * processes address space. It is a "cousin" of pmap_object_init_pt, 2859 * except it runs at page fault time instead of mmap time. 2860 * 2861 * vm.fast_fault Enables pre-faulting zero-fill pages 2862 * 2863 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to 2864 * prefault. Scan stops in either direction when 2865 * a page is found to already exist. 2866 * 2867 * This code used to be per-platform pmap_prefault(). It is now 2868 * machine-independent and enhanced to also pre-fault zero-fill pages 2869 * (see vm.fast_fault) as well as make them writable, which greatly 2870 * reduces the number of page faults programs incur. 2871 * 2872 * Application performance when pre-faulting zero-fill pages is heavily 2873 * dependent on the application. Very tiny applications like /bin/echo 2874 * lose a little performance while applications of any appreciable size 2875 * gain performance. Prefaulting multiple pages also reduces SMP 2876 * congestion and can improve SMP performance significantly. 2877 * 2878 * NOTE! prot may allow writing but this only applies to the top level 2879 * object. If we wind up mapping a page extracted from a backing 2880 * object we have to make sure it is read-only. 2881 * 2882 * NOTE! The caller has already handled any COW operations on the 2883 * vm_map_entry via the normal fault code. Do NOT call this 2884 * shortcut unless the normal fault code has run on this entry. 2885 * 2886 * The related map must be locked. 2887 * No other requirements. 2888 */ 2889 __read_mostly static int vm_prefault_pages = 8; 2890 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0, 2891 "Maximum number of pages to pre-fault"); 2892 __read_mostly static int vm_fast_fault = 1; 2893 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0, 2894 "Burst fault zero-fill regions"); 2895 2896 /* 2897 * Set PG_NOSYNC if the map entry indicates so, but only if the page 2898 * is not already dirty by other means. This will prevent passive 2899 * filesystem syncing as well as 'sync' from writing out the page. 2900 */ 2901 static void 2902 vm_set_nosync(vm_page_t m, vm_map_entry_t entry) 2903 { 2904 if (entry->eflags & MAP_ENTRY_NOSYNC) { 2905 if (m->dirty == 0) 2906 vm_page_flag_set(m, PG_NOSYNC); 2907 } else { 2908 vm_page_flag_clear(m, PG_NOSYNC); 2909 } 2910 } 2911 2912 static void 2913 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot, 2914 int fault_flags) 2915 { 2916 vm_map_backing_t ba; /* first ba */ 2917 struct lwp *lp; 2918 vm_page_t m; 2919 vm_offset_t addr; 2920 vm_pindex_t index; 2921 vm_pindex_t pindex; 2922 vm_object_t object; 2923 int pprot; 2924 int i; 2925 int noneg; 2926 int nopos; 2927 int maxpages; 2928 2929 /* 2930 * Get stable max count value, disabled if set to 0 2931 */ 2932 maxpages = vm_prefault_pages; 2933 cpu_ccfence(); 2934 if (maxpages <= 0) 2935 return; 2936 2937 /* 2938 * We do not currently prefault mappings that use virtual page 2939 * tables. We do not prefault foreign pmaps. 2940 */ 2941 if (entry->maptype != VM_MAPTYPE_NORMAL) 2942 return; 2943 lp = curthread->td_lwp; 2944 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace))) 2945 return; 2946 2947 /* 2948 * Limit pre-fault count to 1024 pages. 2949 */ 2950 if (maxpages > 1024) 2951 maxpages = 1024; 2952 2953 ba = &entry->ba; 2954 object = entry->ba.object; 2955 KKASSERT(object != NULL); 2956 2957 /* 2958 * NOTE: VM_FAULT_DIRTY allowed later so must hold object exclusively 2959 * now (or do something more complex XXX). 2960 */ 2961 vm_object_hold(object); 2962 2963 noneg = 0; 2964 nopos = 0; 2965 for (i = 0; i < maxpages; ++i) { 2966 vm_object_t lobject; 2967 vm_object_t nobject; 2968 vm_map_backing_t last_ba; /* last ba */ 2969 vm_map_backing_t next_ba; /* last ba */ 2970 int allocated = 0; 2971 int error; 2972 2973 /* 2974 * This can eat a lot of time on a heavily contended 2975 * machine so yield on the tick if needed. 2976 */ 2977 if ((i & 7) == 7) 2978 lwkt_yield(); 2979 2980 /* 2981 * Calculate the page to pre-fault, stopping the scan in 2982 * each direction separately if the limit is reached. 2983 */ 2984 if (i & 1) { 2985 if (noneg) 2986 continue; 2987 addr = addra - ((i + 1) >> 1) * PAGE_SIZE; 2988 } else { 2989 if (nopos) 2990 continue; 2991 addr = addra + ((i + 2) >> 1) * PAGE_SIZE; 2992 } 2993 if (addr < entry->start) { 2994 noneg = 1; 2995 if (noneg && nopos) 2996 break; 2997 continue; 2998 } 2999 if (addr >= entry->end) { 3000 nopos = 1; 3001 if (noneg && nopos) 3002 break; 3003 continue; 3004 } 3005 3006 /* 3007 * Skip pages already mapped, and stop scanning in that 3008 * direction. When the scan terminates in both directions 3009 * we are done. 3010 */ 3011 if (pmap_prefault_ok(pmap, addr) == 0) { 3012 if (i & 1) 3013 noneg = 1; 3014 else 3015 nopos = 1; 3016 if (noneg && nopos) 3017 break; 3018 continue; 3019 } 3020 3021 /* 3022 * Follow the backing layers to obtain the page to be mapped 3023 * into the pmap. 3024 * 3025 * If we reach the terminal object without finding a page 3026 * and we determine it would be advantageous, then allocate 3027 * a zero-fill page for the base object. The base object 3028 * is guaranteed to be OBJT_DEFAULT for this case. 3029 * 3030 * In order to not have to check the pager via *haspage*() 3031 * we stop if any non-default object is encountered. e.g. 3032 * a vnode or swap object would stop the loop. 3033 */ 3034 index = ((addr - entry->start) + entry->ba.offset) >> 3035 PAGE_SHIFT; 3036 last_ba = ba; 3037 lobject = object; 3038 pindex = index; 3039 pprot = prot; 3040 3041 /*vm_object_hold(lobject); implied */ 3042 3043 while ((m = vm_page_lookup_busy_try(lobject, pindex, 3044 TRUE, &error)) == NULL) { 3045 if (lobject->type != OBJT_DEFAULT) 3046 break; 3047 if ((next_ba = last_ba->backing_ba) == NULL) { 3048 if (vm_fast_fault == 0) 3049 break; 3050 if ((prot & VM_PROT_WRITE) == 0 || 3051 vm_page_count_min(0)) { 3052 break; 3053 } 3054 3055 /* 3056 * NOTE: Allocated from base object 3057 */ 3058 m = vm_page_alloc(object, index, 3059 VM_ALLOC_NORMAL | 3060 VM_ALLOC_ZERO | 3061 VM_ALLOC_USE_GD | 3062 VM_ALLOC_NULL_OK); 3063 if (m == NULL) 3064 break; 3065 allocated = 1; 3066 pprot = prot; 3067 /* lobject = object .. not needed */ 3068 break; 3069 } 3070 if (next_ba->offset & PAGE_MASK) 3071 break; 3072 nobject = next_ba->object; 3073 vm_object_hold(nobject); 3074 pindex += next_ba->offset >> PAGE_SHIFT; 3075 if (last_ba != ba) { 3076 vm_object_lock_swap(); 3077 vm_object_drop(lobject); 3078 } 3079 lobject = nobject; 3080 last_ba = next_ba; 3081 pprot &= ~VM_PROT_WRITE; 3082 } 3083 3084 /* 3085 * NOTE: A non-NULL (m) will be associated with lobject if 3086 * it was found there, otherwise it is probably a 3087 * zero-fill page associated with the base object. 3088 * 3089 * Give-up if no page is available. 3090 */ 3091 if (m == NULL) { 3092 if (last_ba != ba) 3093 vm_object_drop(lobject); 3094 break; 3095 } 3096 3097 /* 3098 * The object must be marked dirty if we are mapping a 3099 * writable page. m->object is either lobject or object, 3100 * both of which are still held. Do this before we 3101 * potentially drop the object. 3102 */ 3103 if (pprot & VM_PROT_WRITE) 3104 vm_object_set_writeable_dirty(m->object); 3105 3106 /* 3107 * Do not conditionalize on PG_RAM. If pages are present in 3108 * the VM system we assume optimal caching. If caching is 3109 * not optimal the I/O gravy train will be restarted when we 3110 * hit an unavailable page. We do not want to try to restart 3111 * the gravy train now because we really don't know how much 3112 * of the object has been cached. The cost for restarting 3113 * the gravy train should be low (since accesses will likely 3114 * be I/O bound anyway). 3115 */ 3116 if (last_ba != ba) 3117 vm_object_drop(lobject); 3118 3119 /* 3120 * Enter the page into the pmap if appropriate. If we had 3121 * allocated the page we have to place it on a queue. If not 3122 * we just have to make sure it isn't on the cache queue 3123 * (pages on the cache queue are not allowed to be mapped). 3124 */ 3125 if (allocated) { 3126 /* 3127 * Page must be zerod. 3128 */ 3129 vm_page_zero_fill(m); 3130 mycpu->gd_cnt.v_zfod++; 3131 m->valid = VM_PAGE_BITS_ALL; 3132 3133 /* 3134 * Handle dirty page case 3135 */ 3136 if (pprot & VM_PROT_WRITE) 3137 vm_set_nosync(m, entry); 3138 pmap_enter(pmap, addr, m, pprot, 0, entry); 3139 mycpu->gd_cnt.v_vm_faults++; 3140 if (curthread->td_lwp) 3141 ++curthread->td_lwp->lwp_ru.ru_minflt; 3142 vm_page_deactivate(m); 3143 if (pprot & VM_PROT_WRITE) { 3144 /*vm_object_set_writeable_dirty(m->object);*/ 3145 vm_set_nosync(m, entry); 3146 if (fault_flags & VM_FAULT_DIRTY) { 3147 vm_page_dirty(m); 3148 /*XXX*/ 3149 swap_pager_unswapped(m); 3150 } 3151 } 3152 vm_page_wakeup(m); 3153 } else if (error) { 3154 /* couldn't busy page, no wakeup */ 3155 } else if ( 3156 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 3157 (m->flags & PG_FICTITIOUS) == 0) { 3158 /* 3159 * A fully valid page not undergoing soft I/O can 3160 * be immediately entered into the pmap. 3161 */ 3162 if ((m->queue - m->pc) == PQ_CACHE) 3163 vm_page_deactivate(m); 3164 if (pprot & VM_PROT_WRITE) { 3165 /*vm_object_set_writeable_dirty(m->object);*/ 3166 vm_set_nosync(m, entry); 3167 if (fault_flags & VM_FAULT_DIRTY) { 3168 vm_page_dirty(m); 3169 /*XXX*/ 3170 swap_pager_unswapped(m); 3171 } 3172 } 3173 if (pprot & VM_PROT_WRITE) 3174 vm_set_nosync(m, entry); 3175 pmap_enter(pmap, addr, m, pprot, 0, entry); 3176 mycpu->gd_cnt.v_vm_faults++; 3177 if (curthread->td_lwp) 3178 ++curthread->td_lwp->lwp_ru.ru_minflt; 3179 vm_page_wakeup(m); 3180 } else { 3181 vm_page_wakeup(m); 3182 } 3183 } 3184 vm_object_drop(object); 3185 } 3186 3187 /* 3188 * Object can be held shared 3189 */ 3190 static void 3191 vm_prefault_quick(pmap_t pmap, vm_offset_t addra, 3192 vm_map_entry_t entry, int prot, int fault_flags) 3193 { 3194 struct lwp *lp; 3195 vm_page_t m; 3196 vm_offset_t addr; 3197 vm_pindex_t pindex; 3198 vm_object_t object; 3199 int i; 3200 int noneg; 3201 int nopos; 3202 int maxpages; 3203 3204 /* 3205 * Get stable max count value, disabled if set to 0 3206 */ 3207 maxpages = vm_prefault_pages; 3208 cpu_ccfence(); 3209 if (maxpages <= 0) 3210 return; 3211 3212 /* 3213 * We do not currently prefault mappings that use virtual page 3214 * tables. We do not prefault foreign pmaps. 3215 */ 3216 if (entry->maptype != VM_MAPTYPE_NORMAL) 3217 return; 3218 lp = curthread->td_lwp; 3219 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace))) 3220 return; 3221 object = entry->ba.object; 3222 if (entry->ba.backing_ba != NULL) 3223 return; 3224 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 3225 3226 /* 3227 * Limit pre-fault count to 1024 pages. 3228 */ 3229 if (maxpages > 1024) 3230 maxpages = 1024; 3231 3232 noneg = 0; 3233 nopos = 0; 3234 for (i = 0; i < maxpages; ++i) { 3235 int error; 3236 3237 /* 3238 * Calculate the page to pre-fault, stopping the scan in 3239 * each direction separately if the limit is reached. 3240 */ 3241 if (i & 1) { 3242 if (noneg) 3243 continue; 3244 addr = addra - ((i + 1) >> 1) * PAGE_SIZE; 3245 } else { 3246 if (nopos) 3247 continue; 3248 addr = addra + ((i + 2) >> 1) * PAGE_SIZE; 3249 } 3250 if (addr < entry->start) { 3251 noneg = 1; 3252 if (noneg && nopos) 3253 break; 3254 continue; 3255 } 3256 if (addr >= entry->end) { 3257 nopos = 1; 3258 if (noneg && nopos) 3259 break; 3260 continue; 3261 } 3262 3263 /* 3264 * Follow the VM object chain to obtain the page to be mapped 3265 * into the pmap. This version of the prefault code only 3266 * works with terminal objects. 3267 * 3268 * The page must already exist. If we encounter a problem 3269 * we stop here. 3270 * 3271 * WARNING! We cannot call swap_pager_unswapped() or insert 3272 * a new vm_page with a shared token. 3273 */ 3274 pindex = ((addr - entry->start) + entry->ba.offset) >> 3275 PAGE_SHIFT; 3276 3277 /* 3278 * Skip pages already mapped, and stop scanning in that 3279 * direction. When the scan terminates in both directions 3280 * we are done. 3281 */ 3282 if (pmap_prefault_ok(pmap, addr) == 0) { 3283 if (i & 1) 3284 noneg = 1; 3285 else 3286 nopos = 1; 3287 if (noneg && nopos) 3288 break; 3289 continue; 3290 } 3291 3292 /* 3293 * Shortcut the read-only mapping case using the far more 3294 * efficient vm_page_lookup_sbusy_try() function. This 3295 * allows us to acquire the page soft-busied only which 3296 * is especially nice for concurrent execs of the same 3297 * program. 3298 * 3299 * The lookup function also validates page suitability 3300 * (all valid bits set, and not fictitious). 3301 * 3302 * If the page is in PQ_CACHE we have to fall-through 3303 * and hard-busy it so we can move it out of PQ_CACHE. 3304 */ 3305 if ((prot & VM_PROT_WRITE) == 0) { 3306 m = vm_page_lookup_sbusy_try(object, pindex, 3307 0, PAGE_SIZE); 3308 if (m == NULL) 3309 break; 3310 if ((m->queue - m->pc) != PQ_CACHE) { 3311 pmap_enter(pmap, addr, m, prot, 0, entry); 3312 mycpu->gd_cnt.v_vm_faults++; 3313 if (curthread->td_lwp) 3314 ++curthread->td_lwp->lwp_ru.ru_minflt; 3315 vm_page_sbusy_drop(m); 3316 continue; 3317 } 3318 vm_page_sbusy_drop(m); 3319 } 3320 3321 /* 3322 * Fallback to normal vm_page lookup code. This code 3323 * hard-busies the page. Not only that, but the page 3324 * can remain in that state for a significant period 3325 * time due to pmap_enter()'s overhead. 3326 */ 3327 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error); 3328 if (m == NULL || error) 3329 break; 3330 3331 /* 3332 * Stop if the page cannot be trivially entered into the 3333 * pmap. 3334 */ 3335 if (((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) || 3336 (m->flags & PG_FICTITIOUS) || 3337 ((m->flags & PG_SWAPPED) && 3338 (prot & VM_PROT_WRITE) && 3339 (fault_flags & VM_FAULT_DIRTY))) { 3340 vm_page_wakeup(m); 3341 break; 3342 } 3343 3344 /* 3345 * Enter the page into the pmap. The object might be held 3346 * shared so we can't do any (serious) modifying operation 3347 * on it. 3348 */ 3349 if ((m->queue - m->pc) == PQ_CACHE) 3350 vm_page_deactivate(m); 3351 if (prot & VM_PROT_WRITE) { 3352 vm_object_set_writeable_dirty(m->object); 3353 vm_set_nosync(m, entry); 3354 if (fault_flags & VM_FAULT_DIRTY) { 3355 vm_page_dirty(m); 3356 /* can't happeen due to conditional above */ 3357 /* swap_pager_unswapped(m); */ 3358 } 3359 } 3360 pmap_enter(pmap, addr, m, prot, 0, entry); 3361 mycpu->gd_cnt.v_vm_faults++; 3362 if (curthread->td_lwp) 3363 ++curthread->td_lwp->lwp_ru.ru_minflt; 3364 vm_page_wakeup(m); 3365 } 3366 } 3367