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