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