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