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