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