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 didlimit; 119 int hardfault; 120 int fault_flags; 121 int map_generation; 122 boolean_t wired; 123 struct vnode *vp; 124 }; 125 126 static int vm_fast_fault = 1; 127 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0, 128 "Burst fault zero-fill regions"); 129 static int debug_cluster = 0; 130 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, ""); 131 132 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t); 133 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t, int); 134 #if 0 135 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *); 136 #endif 137 static int vm_fault_ratelimit(struct vmspace *); 138 static void vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, 139 int prot); 140 141 /* 142 * The caller must hold vm_token. 143 */ 144 static __inline void 145 release_page(struct faultstate *fs) 146 { 147 vm_page_deactivate(fs->m); 148 vm_page_wakeup(fs->m); 149 fs->m = NULL; 150 } 151 152 /* 153 * The caller must hold vm_token. 154 */ 155 static __inline void 156 unlock_map(struct faultstate *fs) 157 { 158 if (fs->lookup_still_valid && fs->map) { 159 vm_map_lookup_done(fs->map, fs->entry, 0); 160 fs->lookup_still_valid = FALSE; 161 } 162 } 163 164 /* 165 * Clean up after a successful call to vm_fault_object() so another call 166 * to vm_fault_object() can be made. 167 * 168 * The caller must hold vm_token. 169 */ 170 static void 171 _cleanup_successful_fault(struct faultstate *fs, int relock) 172 { 173 if (fs->object != fs->first_object) { 174 vm_page_free(fs->first_m); 175 vm_object_pip_wakeup(fs->object); 176 fs->first_m = NULL; 177 } 178 fs->object = fs->first_object; 179 if (relock && fs->lookup_still_valid == FALSE) { 180 if (fs->map) 181 vm_map_lock_read(fs->map); 182 fs->lookup_still_valid = TRUE; 183 } 184 } 185 186 /* 187 * The caller must hold vm_token. 188 */ 189 static void 190 _unlock_things(struct faultstate *fs, int dealloc) 191 { 192 vm_object_pip_wakeup(fs->first_object); 193 _cleanup_successful_fault(fs, 0); 194 if (dealloc) { 195 vm_object_deallocate(fs->first_object); 196 fs->first_object = NULL; 197 } 198 unlock_map(fs); 199 if (fs->vp != NULL) { 200 vput(fs->vp); 201 fs->vp = NULL; 202 } 203 } 204 205 #define unlock_things(fs) _unlock_things(fs, 0) 206 #define unlock_and_deallocate(fs) _unlock_things(fs, 1) 207 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1) 208 209 /* 210 * TRYPAGER 211 * 212 * Determine if the pager for the current object *might* contain the page. 213 * 214 * We only need to try the pager if this is not a default object (default 215 * objects are zero-fill and have no real pager), and if we are not taking 216 * a wiring fault or if the FS entry is wired. 217 */ 218 #define TRYPAGER(fs) \ 219 (fs->object->type != OBJT_DEFAULT && \ 220 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired)) 221 222 /* 223 * vm_fault: 224 * 225 * Handle a page fault occuring at the given address, requiring the given 226 * permissions, in the map specified. If successful, the page is inserted 227 * into the associated physical map. 228 * 229 * NOTE: The given address should be truncated to the proper page address. 230 * 231 * KERN_SUCCESS is returned if the page fault is handled; otherwise, 232 * a standard error specifying why the fault is fatal is returned. 233 * 234 * The map in question must be referenced, and remains so. 235 * The caller may hold no locks. 236 * No other requirements. 237 */ 238 int 239 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags) 240 { 241 int result; 242 vm_pindex_t first_pindex; 243 struct faultstate fs; 244 int growstack; 245 246 mycpu->gd_cnt.v_vm_faults++; 247 248 fs.didlimit = 0; 249 fs.hardfault = 0; 250 fs.fault_flags = fault_flags; 251 growstack = 1; 252 253 RetryFault: 254 /* 255 * Find the vm_map_entry representing the backing store and resolve 256 * the top level object and page index. This may have the side 257 * effect of executing a copy-on-write on the map entry and/or 258 * creating a shadow object, but will not COW any actual VM pages. 259 * 260 * On success fs.map is left read-locked and various other fields 261 * are initialized but not otherwise referenced or locked. 262 * 263 * NOTE! vm_map_lookup will try to upgrade the fault_type to 264 * VM_FAULT_WRITE if the map entry is a virtual page table and also 265 * writable, so we can set the 'A'accessed bit in the virtual page 266 * table entry. 267 */ 268 fs.map = map; 269 result = vm_map_lookup(&fs.map, vaddr, fault_type, 270 &fs.entry, &fs.first_object, 271 &first_pindex, &fs.first_prot, &fs.wired); 272 273 /* 274 * If the lookup failed or the map protections are incompatible, 275 * the fault generally fails. However, if the caller is trying 276 * to do a user wiring we have more work to do. 277 */ 278 if (result != KERN_SUCCESS) { 279 if (result != KERN_PROTECTION_FAILURE || 280 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) 281 { 282 if (result == KERN_INVALID_ADDRESS && growstack && 283 map != &kernel_map && curproc != NULL) { 284 result = vm_map_growstack(curproc, vaddr); 285 if (result != KERN_SUCCESS) 286 return (KERN_FAILURE); 287 growstack = 0; 288 goto RetryFault; 289 } 290 return (result); 291 } 292 293 /* 294 * If we are user-wiring a r/w segment, and it is COW, then 295 * we need to do the COW operation. Note that we don't 296 * currently COW RO sections now, because it is NOT desirable 297 * to COW .text. We simply keep .text from ever being COW'ed 298 * and take the heat that one cannot debug wired .text sections. 299 */ 300 result = vm_map_lookup(&fs.map, vaddr, 301 VM_PROT_READ|VM_PROT_WRITE| 302 VM_PROT_OVERRIDE_WRITE, 303 &fs.entry, &fs.first_object, 304 &first_pindex, &fs.first_prot, 305 &fs.wired); 306 if (result != KERN_SUCCESS) 307 return result; 308 309 /* 310 * If we don't COW now, on a user wire, the user will never 311 * be able to write to the mapping. If we don't make this 312 * restriction, the bookkeeping would be nearly impossible. 313 */ 314 if ((fs.entry->protection & VM_PROT_WRITE) == 0) 315 fs.entry->max_protection &= ~VM_PROT_WRITE; 316 } 317 318 /* 319 * fs.map is read-locked 320 * 321 * Misc checks. Save the map generation number to detect races. 322 */ 323 fs.map_generation = fs.map->timestamp; 324 325 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 326 panic("vm_fault: fault on nofault entry, addr: %lx", 327 (u_long)vaddr); 328 } 329 330 /* 331 * A system map entry may return a NULL object. No object means 332 * no pager means an unrecoverable kernel fault. 333 */ 334 if (fs.first_object == NULL) { 335 panic("vm_fault: unrecoverable fault at %p in entry %p", 336 (void *)vaddr, fs.entry); 337 } 338 339 /* 340 * Make a reference to this object to prevent its disposal while we 341 * are messing with it. Once we have the reference, the map is free 342 * to be diddled. Since objects reference their shadows (and copies), 343 * they will stay around as well. 344 * 345 * Bump the paging-in-progress count to prevent size changes (e.g. 346 * truncation operations) during I/O. This must be done after 347 * obtaining the vnode lock in order to avoid possible deadlocks. 348 * 349 * The vm_token is needed to manipulate the vm_object 350 */ 351 lwkt_gettoken(&vm_token); 352 vm_object_reference(fs.first_object); 353 fs.vp = vnode_pager_lock(fs.first_object); 354 vm_object_pip_add(fs.first_object, 1); 355 lwkt_reltoken(&vm_token); 356 357 fs.lookup_still_valid = TRUE; 358 fs.first_m = NULL; 359 fs.object = fs.first_object; /* so unlock_and_deallocate works */ 360 361 /* 362 * If the entry is wired we cannot change the page protection. 363 */ 364 if (fs.wired) 365 fault_type = fs.first_prot; 366 367 /* 368 * The page we want is at (first_object, first_pindex), but if the 369 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the 370 * page table to figure out the actual pindex. 371 * 372 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION 373 * ONLY 374 */ 375 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 376 result = vm_fault_vpagetable(&fs, &first_pindex, 377 fs.entry->aux.master_pde, 378 fault_type); 379 if (result == KERN_TRY_AGAIN) 380 goto RetryFault; 381 if (result != KERN_SUCCESS) 382 return (result); 383 } 384 385 /* 386 * Now we have the actual (object, pindex), fault in the page. If 387 * vm_fault_object() fails it will unlock and deallocate the FS 388 * data. If it succeeds everything remains locked and fs->object 389 * will have an additional PIP count if it is not equal to 390 * fs->first_object 391 * 392 * vm_fault_object will set fs->prot for the pmap operation. It is 393 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the 394 * page can be safely written. However, it will force a read-only 395 * mapping for a read fault if the memory is managed by a virtual 396 * page table. 397 */ 398 result = vm_fault_object(&fs, first_pindex, fault_type); 399 400 if (result == KERN_TRY_AGAIN) 401 goto RetryFault; 402 if (result != KERN_SUCCESS) 403 return (result); 404 405 /* 406 * On success vm_fault_object() does not unlock or deallocate, and fs.m 407 * will contain a busied page. 408 * 409 * Enter the page into the pmap and do pmap-related adjustments. 410 */ 411 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired); 412 413 /* 414 * Burst in a few more pages if possible. The fs.map should still 415 * be locked. 416 */ 417 if (fault_flags & VM_FAULT_BURST) { 418 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 && 419 fs.wired == 0) { 420 vm_prefault(fs.map->pmap, vaddr, fs.entry, fs.prot); 421 } 422 } 423 unlock_things(&fs); 424 425 vm_page_flag_clear(fs.m, PG_ZERO); 426 vm_page_flag_set(fs.m, PG_REFERENCED); 427 428 /* 429 * If the page is not wired down, then put it where the pageout daemon 430 * can find it. 431 * 432 * We do not really need to get vm_token here but since all the 433 * vm_*() calls have to doing it here improves efficiency. 434 */ 435 lwkt_gettoken(&vm_token); 436 if (fs.fault_flags & VM_FAULT_WIRE_MASK) { 437 if (fs.wired) 438 vm_page_wire(fs.m); 439 else 440 vm_page_unwire(fs.m, 1); 441 } else { 442 vm_page_activate(fs.m); 443 } 444 445 if (curthread->td_lwp) { 446 if (fs.hardfault) { 447 curthread->td_lwp->lwp_ru.ru_majflt++; 448 } else { 449 curthread->td_lwp->lwp_ru.ru_minflt++; 450 } 451 } 452 453 /* 454 * Unlock everything, and return 455 */ 456 vm_page_wakeup(fs.m); 457 vm_object_deallocate(fs.first_object); 458 lwkt_reltoken(&vm_token); 459 460 return (KERN_SUCCESS); 461 } 462 463 /* 464 * Fault in the specified virtual address in the current process map, 465 * returning a held VM page or NULL. See vm_fault_page() for more 466 * information. 467 * 468 * No requirements. 469 */ 470 vm_page_t 471 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp) 472 { 473 struct lwp *lp = curthread->td_lwp; 474 vm_page_t m; 475 476 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va, 477 fault_type, VM_FAULT_NORMAL, errorp); 478 return(m); 479 } 480 481 /* 482 * Fault in the specified virtual address in the specified map, doing all 483 * necessary manipulation of the object store and all necessary I/O. Return 484 * a held VM page or NULL, and set *errorp. The related pmap is not 485 * updated. 486 * 487 * The returned page will be properly dirtied if VM_PROT_WRITE was specified, 488 * and marked PG_REFERENCED as well. 489 * 490 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an 491 * error will be returned. 492 * 493 * No requirements. 494 */ 495 vm_page_t 496 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 497 int fault_flags, int *errorp) 498 { 499 vm_pindex_t first_pindex; 500 struct faultstate fs; 501 int result; 502 vm_prot_t orig_fault_type = fault_type; 503 504 mycpu->gd_cnt.v_vm_faults++; 505 506 fs.didlimit = 0; 507 fs.hardfault = 0; 508 fs.fault_flags = fault_flags; 509 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0); 510 511 RetryFault: 512 /* 513 * Find the vm_map_entry representing the backing store and resolve 514 * the top level object and page index. This may have the side 515 * effect of executing a copy-on-write on the map entry and/or 516 * creating a shadow object, but will not COW any actual VM pages. 517 * 518 * On success fs.map is left read-locked and various other fields 519 * are initialized but not otherwise referenced or locked. 520 * 521 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE 522 * if the map entry is a virtual page table and also writable, 523 * so we can set the 'A'accessed bit in the virtual page table entry. 524 */ 525 fs.map = map; 526 result = vm_map_lookup(&fs.map, vaddr, fault_type, 527 &fs.entry, &fs.first_object, 528 &first_pindex, &fs.first_prot, &fs.wired); 529 530 if (result != KERN_SUCCESS) { 531 *errorp = result; 532 return (NULL); 533 } 534 535 /* 536 * fs.map is read-locked 537 * 538 * Misc checks. Save the map generation number to detect races. 539 */ 540 fs.map_generation = fs.map->timestamp; 541 542 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 543 panic("vm_fault: fault on nofault entry, addr: %lx", 544 (u_long)vaddr); 545 } 546 547 /* 548 * A system map entry may return a NULL object. No object means 549 * no pager means an unrecoverable kernel fault. 550 */ 551 if (fs.first_object == NULL) { 552 panic("vm_fault: unrecoverable fault at %p in entry %p", 553 (void *)vaddr, fs.entry); 554 } 555 556 /* 557 * Make a reference to this object to prevent its disposal while we 558 * are messing with it. Once we have the reference, the map is free 559 * to be diddled. Since objects reference their shadows (and copies), 560 * they will stay around as well. 561 * 562 * Bump the paging-in-progress count to prevent size changes (e.g. 563 * truncation operations) during I/O. This must be done after 564 * obtaining the vnode lock in order to avoid possible deadlocks. 565 * 566 * The vm_token is needed to manipulate the vm_object 567 */ 568 lwkt_gettoken(&vm_token); 569 vm_object_reference(fs.first_object); 570 fs.vp = vnode_pager_lock(fs.first_object); 571 vm_object_pip_add(fs.first_object, 1); 572 lwkt_reltoken(&vm_token); 573 574 fs.lookup_still_valid = TRUE; 575 fs.first_m = NULL; 576 fs.object = fs.first_object; /* so unlock_and_deallocate works */ 577 578 /* 579 * If the entry is wired we cannot change the page protection. 580 */ 581 if (fs.wired) 582 fault_type = fs.first_prot; 583 584 /* 585 * The page we want is at (first_object, first_pindex), but if the 586 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the 587 * page table to figure out the actual pindex. 588 * 589 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION 590 * ONLY 591 */ 592 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 593 result = vm_fault_vpagetable(&fs, &first_pindex, 594 fs.entry->aux.master_pde, 595 fault_type); 596 if (result == KERN_TRY_AGAIN) 597 goto RetryFault; 598 if (result != KERN_SUCCESS) { 599 *errorp = result; 600 return (NULL); 601 } 602 } 603 604 /* 605 * Now we have the actual (object, pindex), fault in the page. If 606 * vm_fault_object() fails it will unlock and deallocate the FS 607 * data. If it succeeds everything remains locked and fs->object 608 * will have an additinal PIP count if it is not equal to 609 * fs->first_object 610 */ 611 result = vm_fault_object(&fs, first_pindex, fault_type); 612 613 if (result == KERN_TRY_AGAIN) 614 goto RetryFault; 615 if (result != KERN_SUCCESS) { 616 *errorp = result; 617 return(NULL); 618 } 619 620 if ((orig_fault_type & VM_PROT_WRITE) && 621 (fs.prot & VM_PROT_WRITE) == 0) { 622 *errorp = KERN_PROTECTION_FAILURE; 623 unlock_and_deallocate(&fs); 624 return(NULL); 625 } 626 627 /* 628 * On success vm_fault_object() does not unlock or deallocate, and fs.m 629 * will contain a busied page. 630 */ 631 unlock_things(&fs); 632 633 /* 634 * Return a held page. We are not doing any pmap manipulation so do 635 * not set PG_MAPPED. However, adjust the page flags according to 636 * the fault type because the caller may not use a managed pmapping 637 * (so we don't want to lose the fact that the page will be dirtied 638 * if a write fault was specified). 639 */ 640 lwkt_gettoken(&vm_token); 641 vm_page_hold(fs.m); 642 vm_page_flag_clear(fs.m, PG_ZERO); 643 if (fault_type & VM_PROT_WRITE) 644 vm_page_dirty(fs.m); 645 646 /* 647 * Update the pmap. We really only have to do this if a COW 648 * occured to replace the read-only page with the new page. For 649 * now just do it unconditionally. XXX 650 */ 651 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired); 652 vm_page_flag_set(fs.m, PG_REFERENCED); 653 654 /* 655 * Unbusy the page by activating it. It remains held and will not 656 * be reclaimed. 657 */ 658 vm_page_activate(fs.m); 659 660 if (curthread->td_lwp) { 661 if (fs.hardfault) { 662 curthread->td_lwp->lwp_ru.ru_majflt++; 663 } else { 664 curthread->td_lwp->lwp_ru.ru_minflt++; 665 } 666 } 667 668 /* 669 * Unlock everything, and return the held page. 670 */ 671 vm_page_wakeup(fs.m); 672 vm_object_deallocate(fs.first_object); 673 lwkt_reltoken(&vm_token); 674 675 *errorp = 0; 676 return(fs.m); 677 } 678 679 /* 680 * Fault in the specified (object,offset), dirty the returned page as 681 * needed. If the requested fault_type cannot be done NULL and an 682 * error is returned. 683 * 684 * A held (but not busied) page is returned. 685 * 686 * No requirements. 687 */ 688 vm_page_t 689 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset, 690 vm_prot_t fault_type, int fault_flags, int *errorp) 691 { 692 int result; 693 vm_pindex_t first_pindex; 694 struct faultstate fs; 695 struct vm_map_entry entry; 696 697 bzero(&entry, sizeof(entry)); 698 entry.object.vm_object = object; 699 entry.maptype = VM_MAPTYPE_NORMAL; 700 entry.protection = entry.max_protection = fault_type; 701 702 fs.didlimit = 0; 703 fs.hardfault = 0; 704 fs.fault_flags = fault_flags; 705 fs.map = NULL; 706 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0); 707 708 RetryFault: 709 710 fs.first_object = object; 711 first_pindex = OFF_TO_IDX(offset); 712 fs.entry = &entry; 713 fs.first_prot = fault_type; 714 fs.wired = 0; 715 /*fs.map_generation = 0; unused */ 716 717 /* 718 * Make a reference to this object to prevent its disposal while we 719 * are messing with it. Once we have the reference, the map is free 720 * to be diddled. Since objects reference their shadows (and copies), 721 * they will stay around as well. 722 * 723 * Bump the paging-in-progress count to prevent size changes (e.g. 724 * truncation operations) during I/O. This must be done after 725 * obtaining the vnode lock in order to avoid possible deadlocks. 726 */ 727 lwkt_gettoken(&vm_token); 728 vm_object_reference(fs.first_object); 729 fs.vp = vnode_pager_lock(fs.first_object); 730 vm_object_pip_add(fs.first_object, 1); 731 lwkt_reltoken(&vm_token); 732 733 fs.lookup_still_valid = TRUE; 734 fs.first_m = NULL; 735 fs.object = fs.first_object; /* so unlock_and_deallocate works */ 736 737 #if 0 738 /* XXX future - ability to operate on VM object using vpagetable */ 739 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) { 740 result = vm_fault_vpagetable(&fs, &first_pindex, 741 fs.entry->aux.master_pde, 742 fault_type); 743 if (result == KERN_TRY_AGAIN) 744 goto RetryFault; 745 if (result != KERN_SUCCESS) { 746 *errorp = result; 747 return (NULL); 748 } 749 } 750 #endif 751 752 /* 753 * Now we have the actual (object, pindex), fault in the page. If 754 * vm_fault_object() fails it will unlock and deallocate the FS 755 * data. If it succeeds everything remains locked and fs->object 756 * will have an additinal PIP count if it is not equal to 757 * fs->first_object 758 */ 759 result = vm_fault_object(&fs, first_pindex, fault_type); 760 761 if (result == KERN_TRY_AGAIN) 762 goto RetryFault; 763 if (result != KERN_SUCCESS) { 764 *errorp = result; 765 return(NULL); 766 } 767 768 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) { 769 *errorp = KERN_PROTECTION_FAILURE; 770 unlock_and_deallocate(&fs); 771 return(NULL); 772 } 773 774 /* 775 * On success vm_fault_object() does not unlock or deallocate, and fs.m 776 * will contain a busied page. 777 */ 778 unlock_things(&fs); 779 780 /* 781 * Return a held page. We are not doing any pmap manipulation so do 782 * not set PG_MAPPED. However, adjust the page flags according to 783 * the fault type because the caller may not use a managed pmapping 784 * (so we don't want to lose the fact that the page will be dirtied 785 * if a write fault was specified). 786 */ 787 lwkt_gettoken(&vm_token); 788 vm_page_hold(fs.m); 789 vm_page_flag_clear(fs.m, PG_ZERO); 790 if (fault_type & VM_PROT_WRITE) 791 vm_page_dirty(fs.m); 792 793 /* 794 * Indicate that the page was accessed. 795 */ 796 vm_page_flag_set(fs.m, PG_REFERENCED); 797 798 /* 799 * Unbusy the page by activating it. It remains held and will not 800 * be reclaimed. 801 */ 802 vm_page_activate(fs.m); 803 804 if (curthread->td_lwp) { 805 if (fs.hardfault) { 806 mycpu->gd_cnt.v_vm_faults++; 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 lwkt_reltoken(&vm_token); 819 820 *errorp = 0; 821 return(fs.m); 822 } 823 824 /* 825 * Translate the virtual page number (first_pindex) that is relative 826 * to the address space into a logical page number that is relative to the 827 * backing object. Use the virtual page table pointed to by (vpte). 828 * 829 * This implements an N-level page table. Any level can terminate the 830 * scan by setting VPTE_PS. A linear mapping is accomplished by setting 831 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP). 832 * 833 * No requirements (vm_token need not be held). 834 */ 835 static 836 int 837 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex, 838 vpte_t vpte, int fault_type) 839 { 840 struct lwbuf *lwb; 841 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */ 842 int result = KERN_SUCCESS; 843 vpte_t *ptep; 844 845 for (;;) { 846 /* 847 * We cannot proceed if the vpte is not valid, not readable 848 * for a read fault, or not writable for a write fault. 849 */ 850 if ((vpte & VPTE_V) == 0) { 851 unlock_and_deallocate(fs); 852 return (KERN_FAILURE); 853 } 854 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) { 855 unlock_and_deallocate(fs); 856 return (KERN_FAILURE); 857 } 858 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) { 859 unlock_and_deallocate(fs); 860 return (KERN_FAILURE); 861 } 862 if ((vpte & VPTE_PS) || vshift == 0) 863 break; 864 KKASSERT(vshift >= VPTE_PAGE_BITS); 865 866 /* 867 * Get the page table page. Nominally we only read the page 868 * table, but since we are actively setting VPTE_M and VPTE_A, 869 * tell vm_fault_object() that we are writing it. 870 * 871 * There is currently no real need to optimize this. 872 */ 873 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT, 874 VM_PROT_READ|VM_PROT_WRITE); 875 if (result != KERN_SUCCESS) 876 return (result); 877 878 /* 879 * Process the returned fs.m and look up the page table 880 * entry in the page table page. 881 */ 882 vshift -= VPTE_PAGE_BITS; 883 lwb = lwbuf_alloc(fs->m); 884 ptep = ((vpte_t *)lwbuf_kva(lwb) + 885 ((*pindex >> vshift) & VPTE_PAGE_MASK)); 886 vpte = *ptep; 887 888 /* 889 * Page table write-back. If the vpte is valid for the 890 * requested operation, do a write-back to the page table. 891 * 892 * XXX VPTE_M is not set properly for page directory pages. 893 * It doesn't get set in the page directory if the page table 894 * is modified during a read access. 895 */ 896 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) && 897 (vpte & VPTE_W)) { 898 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) { 899 atomic_set_long(ptep, VPTE_M | VPTE_A); 900 vm_page_dirty(fs->m); 901 } 902 } 903 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) && 904 (vpte & VPTE_R)) { 905 if ((vpte & VPTE_A) == 0) { 906 atomic_set_long(ptep, VPTE_A); 907 vm_page_dirty(fs->m); 908 } 909 } 910 lwbuf_free(lwb); 911 vm_page_flag_set(fs->m, PG_REFERENCED); 912 vm_page_activate(fs->m); 913 vm_page_wakeup(fs->m); 914 cleanup_successful_fault(fs); 915 } 916 /* 917 * Combine remaining address bits with the vpte. 918 */ 919 /* JG how many bits from each? */ 920 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) + 921 (*pindex & ((1L << vshift) - 1)); 922 return (KERN_SUCCESS); 923 } 924 925 926 /* 927 * This is the core of the vm_fault code. 928 * 929 * Do all operations required to fault-in (fs.first_object, pindex). Run 930 * through the shadow chain as necessary and do required COW or virtual 931 * copy operations. The caller has already fully resolved the vm_map_entry 932 * and, if appropriate, has created a copy-on-write layer. All we need to 933 * do is iterate the object chain. 934 * 935 * On failure (fs) is unlocked and deallocated and the caller may return or 936 * retry depending on the failure code. On success (fs) is NOT unlocked or 937 * deallocated, fs.m will contained a resolved, busied page, and fs.object 938 * will have an additional PIP count if it is not equal to fs.first_object. 939 * 940 * No requirements. 941 */ 942 static 943 int 944 vm_fault_object(struct faultstate *fs, 945 vm_pindex_t first_pindex, vm_prot_t fault_type) 946 { 947 vm_object_t next_object; 948 vm_pindex_t pindex; 949 950 fs->prot = fs->first_prot; 951 fs->object = fs->first_object; 952 pindex = first_pindex; 953 954 /* 955 * If a read fault occurs we try to make the page writable if 956 * possible. There are three cases where we cannot make the 957 * page mapping writable: 958 * 959 * (1) The mapping is read-only or the VM object is read-only, 960 * fs->prot above will simply not have VM_PROT_WRITE set. 961 * 962 * (2) If the mapping is a virtual page table we need to be able 963 * to detect writes so we can set VPTE_M in the virtual page 964 * table. 965 * 966 * (3) If the VM page is read-only or copy-on-write, upgrading would 967 * just result in an unnecessary COW fault. 968 * 969 * VM_PROT_VPAGED is set if faulting via a virtual page table and 970 * causes adjustments to the 'M'odify bit to also turn off write 971 * access to force a re-fault. 972 */ 973 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) { 974 if ((fault_type & VM_PROT_WRITE) == 0) 975 fs->prot &= ~VM_PROT_WRITE; 976 } 977 978 lwkt_gettoken(&vm_token); 979 980 for (;;) { 981 /* 982 * If the object is dead, we stop here 983 */ 984 if (fs->object->flags & OBJ_DEAD) { 985 unlock_and_deallocate(fs); 986 lwkt_reltoken(&vm_token); 987 return (KERN_PROTECTION_FAILURE); 988 } 989 990 /* 991 * See if page is resident. spl protection is required 992 * to avoid an interrupt unbusy/free race against our 993 * lookup. We must hold the protection through a page 994 * allocation or busy. 995 */ 996 crit_enter(); 997 fs->m = vm_page_lookup(fs->object, pindex); 998 if (fs->m != NULL) { 999 int queue; 1000 /* 1001 * Wait/Retry if the page is busy. We have to do this 1002 * if the page is busy via either PG_BUSY or 1003 * vm_page_t->busy because the vm_pager may be using 1004 * vm_page_t->busy for pageouts ( and even pageins if 1005 * it is the vnode pager ), and we could end up trying 1006 * to pagein and pageout the same page simultaneously. 1007 * 1008 * We can theoretically allow the busy case on a read 1009 * fault if the page is marked valid, but since such 1010 * pages are typically already pmap'd, putting that 1011 * special case in might be more effort then it is 1012 * worth. We cannot under any circumstances mess 1013 * around with a vm_page_t->busy page except, perhaps, 1014 * to pmap it. 1015 */ 1016 if ((fs->m->flags & PG_BUSY) || fs->m->busy) { 1017 unlock_things(fs); 1018 vm_page_sleep_busy(fs->m, TRUE, "vmpfw"); 1019 mycpu->gd_cnt.v_intrans++; 1020 vm_object_deallocate(fs->first_object); 1021 fs->first_object = NULL; 1022 lwkt_reltoken(&vm_token); 1023 crit_exit(); 1024 return (KERN_TRY_AGAIN); 1025 } 1026 1027 /* 1028 * If reactivating a page from PQ_CACHE we may have 1029 * to rate-limit. 1030 */ 1031 queue = fs->m->queue; 1032 vm_page_unqueue_nowakeup(fs->m); 1033 1034 if ((queue - fs->m->pc) == PQ_CACHE && 1035 vm_page_count_severe()) { 1036 vm_page_activate(fs->m); 1037 unlock_and_deallocate(fs); 1038 vm_waitpfault(); 1039 lwkt_reltoken(&vm_token); 1040 crit_exit(); 1041 return (KERN_TRY_AGAIN); 1042 } 1043 1044 /* 1045 * Mark page busy for other processes, and the 1046 * pagedaemon. If it still isn't completely valid 1047 * (readable), or if a read-ahead-mark is set on 1048 * the VM page, jump to readrest, else we found the 1049 * page and can return. 1050 * 1051 * We can release the spl once we have marked the 1052 * page busy. 1053 */ 1054 vm_page_busy(fs->m); 1055 crit_exit(); 1056 1057 if (fs->m->object != &kernel_object) { 1058 if ((fs->m->valid & VM_PAGE_BITS_ALL) != 1059 VM_PAGE_BITS_ALL) { 1060 goto readrest; 1061 } 1062 if (fs->m->flags & PG_RAM) { 1063 if (debug_cluster) 1064 kprintf("R"); 1065 vm_page_flag_clear(fs->m, PG_RAM); 1066 goto readrest; 1067 } 1068 } 1069 break; /* break to PAGE HAS BEEN FOUND */ 1070 } 1071 1072 /* 1073 * Page is not resident, If this is the search termination 1074 * or the pager might contain the page, allocate a new page. 1075 * 1076 * NOTE: We are still in a critical section. 1077 */ 1078 if (TRYPAGER(fs) || fs->object == fs->first_object) { 1079 /* 1080 * If the page is beyond the object size we fail 1081 */ 1082 if (pindex >= fs->object->size) { 1083 lwkt_reltoken(&vm_token); 1084 crit_exit(); 1085 unlock_and_deallocate(fs); 1086 return (KERN_PROTECTION_FAILURE); 1087 } 1088 1089 /* 1090 * Ratelimit. 1091 */ 1092 if (fs->didlimit == 0 && curproc != NULL) { 1093 int limticks; 1094 1095 limticks = vm_fault_ratelimit(curproc->p_vmspace); 1096 if (limticks) { 1097 lwkt_reltoken(&vm_token); 1098 crit_exit(); 1099 unlock_and_deallocate(fs); 1100 tsleep(curproc, 0, "vmrate", limticks); 1101 fs->didlimit = 1; 1102 return (KERN_TRY_AGAIN); 1103 } 1104 } 1105 1106 /* 1107 * Allocate a new page for this object/offset pair. 1108 */ 1109 fs->m = NULL; 1110 if (!vm_page_count_severe()) { 1111 fs->m = vm_page_alloc(fs->object, pindex, 1112 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO); 1113 } 1114 if (fs->m == NULL) { 1115 lwkt_reltoken(&vm_token); 1116 crit_exit(); 1117 unlock_and_deallocate(fs); 1118 vm_waitpfault(); 1119 return (KERN_TRY_AGAIN); 1120 } 1121 } 1122 crit_exit(); 1123 1124 readrest: 1125 /* 1126 * We have found an invalid or partially valid page, a 1127 * page with a read-ahead mark which might be partially or 1128 * fully valid (and maybe dirty too), or we have allocated 1129 * a new page. 1130 * 1131 * Attempt to fault-in the page if there is a chance that the 1132 * pager has it, and potentially fault in additional pages 1133 * at the same time. 1134 * 1135 * We are NOT in splvm here and if TRYPAGER is true then 1136 * fs.m will be non-NULL and will be PG_BUSY for us. 1137 */ 1138 if (TRYPAGER(fs)) { 1139 int rv; 1140 int seqaccess; 1141 u_char behavior = vm_map_entry_behavior(fs->entry); 1142 1143 if (behavior == MAP_ENTRY_BEHAV_RANDOM) 1144 seqaccess = 0; 1145 else 1146 seqaccess = -1; 1147 1148 /* 1149 * If sequential access is detected then attempt 1150 * to deactivate/cache pages behind the scan to 1151 * prevent resource hogging. 1152 * 1153 * Use of PG_RAM to detect sequential access 1154 * also simulates multi-zone sequential access 1155 * detection for free. 1156 * 1157 * NOTE: Partially valid dirty pages cannot be 1158 * deactivated without causing NFS picemeal 1159 * writes to barf. 1160 */ 1161 if ((fs->first_object->type != OBJT_DEVICE) && 1162 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL || 1163 (behavior != MAP_ENTRY_BEHAV_RANDOM && 1164 (fs->m->flags & PG_RAM))) 1165 ) { 1166 vm_pindex_t scan_pindex; 1167 int scan_count = 16; 1168 1169 if (first_pindex < 16) { 1170 scan_pindex = 0; 1171 scan_count = 0; 1172 } else { 1173 scan_pindex = first_pindex - 16; 1174 if (scan_pindex < 16) 1175 scan_count = scan_pindex; 1176 else 1177 scan_count = 16; 1178 } 1179 1180 crit_enter(); 1181 while (scan_count) { 1182 vm_page_t mt; 1183 1184 mt = vm_page_lookup(fs->first_object, 1185 scan_pindex); 1186 if (mt == NULL || 1187 (mt->valid != VM_PAGE_BITS_ALL)) { 1188 break; 1189 } 1190 if (mt->busy || 1191 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) || 1192 mt->hold_count || 1193 mt->wire_count) { 1194 goto skip; 1195 } 1196 if (mt->dirty == 0) 1197 vm_page_test_dirty(mt); 1198 if (mt->dirty) { 1199 vm_page_busy(mt); 1200 vm_page_protect(mt, 1201 VM_PROT_NONE); 1202 vm_page_deactivate(mt); 1203 vm_page_wakeup(mt); 1204 } else { 1205 vm_page_cache(mt); 1206 } 1207 skip: 1208 --scan_count; 1209 --scan_pindex; 1210 } 1211 crit_exit(); 1212 1213 seqaccess = 1; 1214 } 1215 1216 /* 1217 * Avoid deadlocking against the map when doing I/O. 1218 * fs.object and the page is PG_BUSY'd. 1219 */ 1220 unlock_map(fs); 1221 1222 /* 1223 * Acquire the page data. We still hold a ref on 1224 * fs.object and the page has been PG_BUSY's. 1225 * 1226 * The pager may replace the page (for example, in 1227 * order to enter a fictitious page into the 1228 * object). If it does so it is responsible for 1229 * cleaning up the passed page and properly setting 1230 * the new page PG_BUSY. 1231 * 1232 * If we got here through a PG_RAM read-ahead 1233 * mark the page may be partially dirty and thus 1234 * not freeable. Don't bother checking to see 1235 * if the pager has the page because we can't free 1236 * it anyway. We have to depend on the get_page 1237 * operation filling in any gaps whether there is 1238 * backing store or not. 1239 */ 1240 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess); 1241 1242 if (rv == VM_PAGER_OK) { 1243 /* 1244 * Relookup in case pager changed page. Pager 1245 * is responsible for disposition of old page 1246 * if moved. 1247 * 1248 * XXX other code segments do relookups too. 1249 * It's a bad abstraction that needs to be 1250 * fixed/removed. 1251 */ 1252 fs->m = vm_page_lookup(fs->object, pindex); 1253 if (fs->m == NULL) { 1254 lwkt_reltoken(&vm_token); 1255 unlock_and_deallocate(fs); 1256 return (KERN_TRY_AGAIN); 1257 } 1258 1259 ++fs->hardfault; 1260 break; /* break to PAGE HAS BEEN FOUND */ 1261 } 1262 1263 /* 1264 * Remove the bogus page (which does not exist at this 1265 * object/offset); before doing so, we must get back 1266 * our object lock to preserve our invariant. 1267 * 1268 * Also wake up any other process that may want to bring 1269 * in this page. 1270 * 1271 * If this is the top-level object, we must leave the 1272 * busy page to prevent another process from rushing 1273 * past us, and inserting the page in that object at 1274 * the same time that we are. 1275 */ 1276 if (rv == VM_PAGER_ERROR) { 1277 if (curproc) 1278 kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm); 1279 else 1280 kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm); 1281 } 1282 1283 /* 1284 * Data outside the range of the pager or an I/O error 1285 * 1286 * The page may have been wired during the pagein, 1287 * e.g. by the buffer cache, and cannot simply be 1288 * freed. Call vnode_pager_freepage() to deal with it. 1289 */ 1290 /* 1291 * XXX - the check for kernel_map is a kludge to work 1292 * around having the machine panic on a kernel space 1293 * fault w/ I/O error. 1294 */ 1295 if (((fs->map != &kernel_map) && 1296 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) { 1297 vnode_pager_freepage(fs->m); 1298 lwkt_reltoken(&vm_token); 1299 fs->m = NULL; 1300 unlock_and_deallocate(fs); 1301 if (rv == VM_PAGER_ERROR) 1302 return (KERN_FAILURE); 1303 else 1304 return (KERN_PROTECTION_FAILURE); 1305 /* NOT REACHED */ 1306 } 1307 if (fs->object != fs->first_object) { 1308 vnode_pager_freepage(fs->m); 1309 fs->m = NULL; 1310 /* 1311 * XXX - we cannot just fall out at this 1312 * point, m has been freed and is invalid! 1313 */ 1314 } 1315 } 1316 1317 /* 1318 * We get here if the object has a default pager (or unwiring) 1319 * or the pager doesn't have the page. 1320 */ 1321 if (fs->object == fs->first_object) 1322 fs->first_m = fs->m; 1323 1324 /* 1325 * Move on to the next object. Lock the next object before 1326 * unlocking the current one. 1327 */ 1328 pindex += OFF_TO_IDX(fs->object->backing_object_offset); 1329 next_object = fs->object->backing_object; 1330 if (next_object == NULL) { 1331 /* 1332 * If there's no object left, fill the page in the top 1333 * object with zeros. 1334 */ 1335 if (fs->object != fs->first_object) { 1336 vm_object_pip_wakeup(fs->object); 1337 1338 fs->object = fs->first_object; 1339 pindex = first_pindex; 1340 fs->m = fs->first_m; 1341 } 1342 fs->first_m = NULL; 1343 1344 /* 1345 * Zero the page if necessary and mark it valid. 1346 */ 1347 if ((fs->m->flags & PG_ZERO) == 0) { 1348 vm_page_zero_fill(fs->m); 1349 } else { 1350 mycpu->gd_cnt.v_ozfod++; 1351 } 1352 mycpu->gd_cnt.v_zfod++; 1353 fs->m->valid = VM_PAGE_BITS_ALL; 1354 break; /* break to PAGE HAS BEEN FOUND */ 1355 } 1356 if (fs->object != fs->first_object) { 1357 vm_object_pip_wakeup(fs->object); 1358 } 1359 KASSERT(fs->object != next_object, 1360 ("object loop %p", next_object)); 1361 fs->object = next_object; 1362 vm_object_pip_add(fs->object, 1); 1363 } 1364 1365 /* 1366 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 1367 * is held.] 1368 * 1369 * vm_token is still held 1370 * 1371 * If the page is being written, but isn't already owned by the 1372 * top-level object, we have to copy it into a new page owned by the 1373 * top-level object. 1374 */ 1375 KASSERT((fs->m->flags & PG_BUSY) != 0, 1376 ("vm_fault: not busy after main loop")); 1377 1378 if (fs->object != fs->first_object) { 1379 /* 1380 * We only really need to copy if we want to write it. 1381 */ 1382 if (fault_type & VM_PROT_WRITE) { 1383 /* 1384 * This allows pages to be virtually copied from a 1385 * backing_object into the first_object, where the 1386 * backing object has no other refs to it, and cannot 1387 * gain any more refs. Instead of a bcopy, we just 1388 * move the page from the backing object to the 1389 * first object. Note that we must mark the page 1390 * dirty in the first object so that it will go out 1391 * to swap when needed. 1392 */ 1393 if ( 1394 /* 1395 * Map, if present, has not changed 1396 */ 1397 (fs->map == NULL || 1398 fs->map_generation == fs->map->timestamp) && 1399 /* 1400 * Only one shadow object 1401 */ 1402 (fs->object->shadow_count == 1) && 1403 /* 1404 * No COW refs, except us 1405 */ 1406 (fs->object->ref_count == 1) && 1407 /* 1408 * No one else can look this object up 1409 */ 1410 (fs->object->handle == NULL) && 1411 /* 1412 * No other ways to look the object up 1413 */ 1414 ((fs->object->type == OBJT_DEFAULT) || 1415 (fs->object->type == OBJT_SWAP)) && 1416 /* 1417 * We don't chase down the shadow chain 1418 */ 1419 (fs->object == fs->first_object->backing_object) && 1420 1421 /* 1422 * grab the lock if we need to 1423 */ 1424 (fs->lookup_still_valid || 1425 fs->map == NULL || 1426 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0) 1427 ) { 1428 1429 fs->lookup_still_valid = 1; 1430 /* 1431 * get rid of the unnecessary page 1432 */ 1433 vm_page_protect(fs->first_m, VM_PROT_NONE); 1434 vm_page_free(fs->first_m); 1435 fs->first_m = NULL; 1436 1437 /* 1438 * grab the page and put it into the 1439 * process'es object. The page is 1440 * automatically made dirty. 1441 */ 1442 vm_page_rename(fs->m, fs->first_object, first_pindex); 1443 fs->first_m = fs->m; 1444 vm_page_busy(fs->first_m); 1445 fs->m = NULL; 1446 mycpu->gd_cnt.v_cow_optim++; 1447 } else { 1448 /* 1449 * Oh, well, lets copy it. 1450 */ 1451 vm_page_copy(fs->m, fs->first_m); 1452 vm_page_event(fs->m, VMEVENT_COW); 1453 } 1454 1455 if (fs->m) { 1456 /* 1457 * We no longer need the old page or object. 1458 */ 1459 release_page(fs); 1460 } 1461 1462 /* 1463 * fs->object != fs->first_object due to above 1464 * conditional 1465 */ 1466 vm_object_pip_wakeup(fs->object); 1467 1468 /* 1469 * Only use the new page below... 1470 */ 1471 1472 mycpu->gd_cnt.v_cow_faults++; 1473 fs->m = fs->first_m; 1474 fs->object = fs->first_object; 1475 pindex = first_pindex; 1476 } else { 1477 /* 1478 * If it wasn't a write fault avoid having to copy 1479 * the page by mapping it read-only. 1480 */ 1481 fs->prot &= ~VM_PROT_WRITE; 1482 } 1483 } 1484 1485 /* 1486 * We may have had to unlock a map to do I/O. If we did then 1487 * lookup_still_valid will be FALSE. If the map generation count 1488 * also changed then all sorts of things could have happened while 1489 * we were doing the I/O and we need to retry. 1490 */ 1491 1492 if (!fs->lookup_still_valid && 1493 fs->map != NULL && 1494 (fs->map->timestamp != fs->map_generation)) { 1495 release_page(fs); 1496 lwkt_reltoken(&vm_token); 1497 unlock_and_deallocate(fs); 1498 return (KERN_TRY_AGAIN); 1499 } 1500 1501 /* 1502 * If the fault is a write, we know that this page is being 1503 * written NOW so dirty it explicitly to save on pmap_is_modified() 1504 * calls later. 1505 * 1506 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC 1507 * if the page is already dirty to prevent data written with 1508 * the expectation of being synced from not being synced. 1509 * Likewise if this entry does not request NOSYNC then make 1510 * sure the page isn't marked NOSYNC. Applications sharing 1511 * data should use the same flags to avoid ping ponging. 1512 * 1513 * Also tell the backing pager, if any, that it should remove 1514 * any swap backing since the page is now dirty. 1515 */ 1516 if (fs->prot & VM_PROT_WRITE) { 1517 vm_object_set_writeable_dirty(fs->m->object); 1518 if (fs->entry->eflags & MAP_ENTRY_NOSYNC) { 1519 if (fs->m->dirty == 0) 1520 vm_page_flag_set(fs->m, PG_NOSYNC); 1521 } else { 1522 vm_page_flag_clear(fs->m, PG_NOSYNC); 1523 } 1524 if (fs->fault_flags & VM_FAULT_DIRTY) { 1525 crit_enter(); 1526 vm_page_dirty(fs->m); 1527 swap_pager_unswapped(fs->m); 1528 crit_exit(); 1529 } 1530 } 1531 1532 lwkt_reltoken(&vm_token); 1533 1534 /* 1535 * Page had better still be busy. We are still locked up and 1536 * fs->object will have another PIP reference if it is not equal 1537 * to fs->first_object. 1538 */ 1539 KASSERT(fs->m->flags & PG_BUSY, 1540 ("vm_fault: page %p not busy!", fs->m)); 1541 1542 /* 1543 * Sanity check: page must be completely valid or it is not fit to 1544 * map into user space. vm_pager_get_pages() ensures this. 1545 */ 1546 if (fs->m->valid != VM_PAGE_BITS_ALL) { 1547 vm_page_zero_invalid(fs->m, TRUE); 1548 kprintf("Warning: page %p partially invalid on fault\n", fs->m); 1549 } 1550 1551 return (KERN_SUCCESS); 1552 } 1553 1554 /* 1555 * Wire down a range of virtual addresses in a map. The entry in question 1556 * should be marked in-transition and the map must be locked. We must 1557 * release the map temporarily while faulting-in the page to avoid a 1558 * deadlock. Note that the entry may be clipped while we are blocked but 1559 * will never be freed. 1560 * 1561 * No requirements. 1562 */ 1563 int 1564 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire) 1565 { 1566 boolean_t fictitious; 1567 vm_offset_t start; 1568 vm_offset_t end; 1569 vm_offset_t va; 1570 vm_paddr_t pa; 1571 pmap_t pmap; 1572 int rv; 1573 1574 pmap = vm_map_pmap(map); 1575 start = entry->start; 1576 end = entry->end; 1577 fictitious = entry->object.vm_object && 1578 (entry->object.vm_object->type == OBJT_DEVICE); 1579 1580 lwkt_gettoken(&vm_token); 1581 vm_map_unlock(map); 1582 map->timestamp++; 1583 1584 /* 1585 * We simulate a fault to get the page and enter it in the physical 1586 * map. 1587 */ 1588 for (va = start; va < end; va += PAGE_SIZE) { 1589 if (user_wire) { 1590 rv = vm_fault(map, va, VM_PROT_READ, 1591 VM_FAULT_USER_WIRE); 1592 } else { 1593 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE, 1594 VM_FAULT_CHANGE_WIRING); 1595 } 1596 if (rv) { 1597 while (va > start) { 1598 va -= PAGE_SIZE; 1599 if ((pa = pmap_extract(pmap, va)) == 0) 1600 continue; 1601 pmap_change_wiring(pmap, va, FALSE); 1602 if (!fictitious) 1603 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1); 1604 } 1605 vm_map_lock(map); 1606 lwkt_reltoken(&vm_token); 1607 return (rv); 1608 } 1609 } 1610 vm_map_lock(map); 1611 lwkt_reltoken(&vm_token); 1612 return (KERN_SUCCESS); 1613 } 1614 1615 /* 1616 * Unwire a range of virtual addresses in a map. The map should be 1617 * locked. 1618 */ 1619 void 1620 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry) 1621 { 1622 boolean_t fictitious; 1623 vm_offset_t start; 1624 vm_offset_t end; 1625 vm_offset_t va; 1626 vm_paddr_t pa; 1627 pmap_t pmap; 1628 1629 pmap = vm_map_pmap(map); 1630 start = entry->start; 1631 end = entry->end; 1632 fictitious = entry->object.vm_object && 1633 (entry->object.vm_object->type == OBJT_DEVICE); 1634 1635 /* 1636 * Since the pages are wired down, we must be able to get their 1637 * mappings from the physical map system. 1638 */ 1639 lwkt_gettoken(&vm_token); 1640 for (va = start; va < end; va += PAGE_SIZE) { 1641 pa = pmap_extract(pmap, va); 1642 if (pa != 0) { 1643 pmap_change_wiring(pmap, va, FALSE); 1644 if (!fictitious) 1645 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1); 1646 } 1647 } 1648 lwkt_reltoken(&vm_token); 1649 } 1650 1651 /* 1652 * Reduce the rate at which memory is allocated to a process based 1653 * on the perceived load on the VM system. As the load increases 1654 * the allocation burst rate goes down and the delay increases. 1655 * 1656 * Rate limiting does not apply when faulting active or inactive 1657 * pages. When faulting 'cache' pages, rate limiting only applies 1658 * if the system currently has a severe page deficit. 1659 * 1660 * XXX vm_pagesupply should be increased when a page is freed. 1661 * 1662 * We sleep up to 1/10 of a second. 1663 */ 1664 static int 1665 vm_fault_ratelimit(struct vmspace *vmspace) 1666 { 1667 if (vm_load_enable == 0) 1668 return(0); 1669 if (vmspace->vm_pagesupply > 0) { 1670 --vmspace->vm_pagesupply; /* SMP race ok */ 1671 return(0); 1672 } 1673 #ifdef INVARIANTS 1674 if (vm_load_debug) { 1675 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n", 1676 vm_load, 1677 (1000 - vm_load ) / 10, vm_load * hz / 10000, 1678 curproc->p_pid, curproc->p_comm); 1679 } 1680 #endif 1681 vmspace->vm_pagesupply = (1000 - vm_load) / 10; 1682 return(vm_load * hz / 10000); 1683 } 1684 1685 /* 1686 * Copy all of the pages from a wired-down map entry to another. 1687 * 1688 * The source and destination maps must be locked for write. 1689 * The source map entry must be wired down (or be a sharing map 1690 * entry corresponding to a main map entry that is wired down). 1691 * 1692 * No other requirements. 1693 */ 1694 void 1695 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1696 vm_map_entry_t dst_entry, vm_map_entry_t src_entry) 1697 { 1698 vm_object_t dst_object; 1699 vm_object_t src_object; 1700 vm_ooffset_t dst_offset; 1701 vm_ooffset_t src_offset; 1702 vm_prot_t prot; 1703 vm_offset_t vaddr; 1704 vm_page_t dst_m; 1705 vm_page_t src_m; 1706 1707 #ifdef lint 1708 src_map++; 1709 #endif /* lint */ 1710 1711 src_object = src_entry->object.vm_object; 1712 src_offset = src_entry->offset; 1713 1714 /* 1715 * Create the top-level object for the destination entry. (Doesn't 1716 * actually shadow anything - we copy the pages directly.) 1717 */ 1718 vm_map_entry_allocate_object(dst_entry); 1719 dst_object = dst_entry->object.vm_object; 1720 1721 prot = dst_entry->max_protection; 1722 1723 /* 1724 * Loop through all of the pages in the entry's range, copying each 1725 * one from the source object (it should be there) to the destination 1726 * object. 1727 */ 1728 for (vaddr = dst_entry->start, dst_offset = 0; 1729 vaddr < dst_entry->end; 1730 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) { 1731 1732 /* 1733 * Allocate a page in the destination object 1734 */ 1735 do { 1736 dst_m = vm_page_alloc(dst_object, 1737 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL); 1738 if (dst_m == NULL) { 1739 vm_wait(0); 1740 } 1741 } while (dst_m == NULL); 1742 1743 /* 1744 * Find the page in the source object, and copy it in. 1745 * (Because the source is wired down, the page will be in 1746 * memory.) 1747 */ 1748 src_m = vm_page_lookup(src_object, 1749 OFF_TO_IDX(dst_offset + src_offset)); 1750 if (src_m == NULL) 1751 panic("vm_fault_copy_wired: page missing"); 1752 1753 vm_page_copy(src_m, dst_m); 1754 vm_page_event(src_m, VMEVENT_COW); 1755 1756 /* 1757 * Enter it in the pmap... 1758 */ 1759 1760 vm_page_flag_clear(dst_m, PG_ZERO); 1761 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE); 1762 1763 /* 1764 * Mark it no longer busy, and put it on the active list. 1765 */ 1766 vm_page_activate(dst_m); 1767 vm_page_wakeup(dst_m); 1768 } 1769 } 1770 1771 #if 0 1772 1773 /* 1774 * This routine checks around the requested page for other pages that 1775 * might be able to be faulted in. This routine brackets the viable 1776 * pages for the pages to be paged in. 1777 * 1778 * Inputs: 1779 * m, rbehind, rahead 1780 * 1781 * Outputs: 1782 * marray (array of vm_page_t), reqpage (index of requested page) 1783 * 1784 * Return value: 1785 * number of pages in marray 1786 */ 1787 static int 1788 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead, 1789 vm_page_t *marray, int *reqpage) 1790 { 1791 int i,j; 1792 vm_object_t object; 1793 vm_pindex_t pindex, startpindex, endpindex, tpindex; 1794 vm_page_t rtm; 1795 int cbehind, cahead; 1796 1797 object = m->object; 1798 pindex = m->pindex; 1799 1800 /* 1801 * we don't fault-ahead for device pager 1802 */ 1803 if (object->type == OBJT_DEVICE) { 1804 *reqpage = 0; 1805 marray[0] = m; 1806 return 1; 1807 } 1808 1809 /* 1810 * if the requested page is not available, then give up now 1811 */ 1812 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) { 1813 *reqpage = 0; /* not used by caller, fix compiler warn */ 1814 return 0; 1815 } 1816 1817 if ((cbehind == 0) && (cahead == 0)) { 1818 *reqpage = 0; 1819 marray[0] = m; 1820 return 1; 1821 } 1822 1823 if (rahead > cahead) { 1824 rahead = cahead; 1825 } 1826 1827 if (rbehind > cbehind) { 1828 rbehind = cbehind; 1829 } 1830 1831 /* 1832 * Do not do any readahead if we have insufficient free memory. 1833 * 1834 * XXX code was broken disabled before and has instability 1835 * with this conditonal fixed, so shortcut for now. 1836 */ 1837 if (burst_fault == 0 || vm_page_count_severe()) { 1838 marray[0] = m; 1839 *reqpage = 0; 1840 return 1; 1841 } 1842 1843 /* 1844 * scan backward for the read behind pages -- in memory 1845 * 1846 * Assume that if the page is not found an interrupt will not 1847 * create it. Theoretically interrupts can only remove (busy) 1848 * pages, not create new associations. 1849 */ 1850 if (pindex > 0) { 1851 if (rbehind > pindex) { 1852 rbehind = pindex; 1853 startpindex = 0; 1854 } else { 1855 startpindex = pindex - rbehind; 1856 } 1857 1858 crit_enter(); 1859 lwkt_gettoken(&vm_token); 1860 for (tpindex = pindex; tpindex > startpindex; --tpindex) { 1861 if (vm_page_lookup(object, tpindex - 1)) 1862 break; 1863 } 1864 1865 i = 0; 1866 while (tpindex < pindex) { 1867 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM); 1868 if (rtm == NULL) { 1869 lwkt_reltoken(&vm_token); 1870 crit_exit(); 1871 for (j = 0; j < i; j++) { 1872 vm_page_free(marray[j]); 1873 } 1874 marray[0] = m; 1875 *reqpage = 0; 1876 return 1; 1877 } 1878 marray[i] = rtm; 1879 ++i; 1880 ++tpindex; 1881 } 1882 lwkt_reltoken(&vm_token); 1883 crit_exit(); 1884 } else { 1885 i = 0; 1886 } 1887 1888 /* 1889 * Assign requested page 1890 */ 1891 marray[i] = m; 1892 *reqpage = i; 1893 ++i; 1894 1895 /* 1896 * Scan forwards for read-ahead pages 1897 */ 1898 tpindex = pindex + 1; 1899 endpindex = tpindex + rahead; 1900 if (endpindex > object->size) 1901 endpindex = object->size; 1902 1903 crit_enter(); 1904 lwkt_gettoken(&vm_token); 1905 while (tpindex < endpindex) { 1906 if (vm_page_lookup(object, tpindex)) 1907 break; 1908 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM); 1909 if (rtm == NULL) 1910 break; 1911 marray[i] = rtm; 1912 ++i; 1913 ++tpindex; 1914 } 1915 lwkt_reltoken(&vm_token); 1916 crit_exit(); 1917 1918 return (i); 1919 } 1920 1921 #endif 1922 1923 /* 1924 * vm_prefault() provides a quick way of clustering pagefaults into a 1925 * processes address space. It is a "cousin" of pmap_object_init_pt, 1926 * except it runs at page fault time instead of mmap time. 1927 * 1928 * This code used to be per-platform pmap_prefault(). It is now 1929 * machine-independent and enhanced to also pre-fault zero-fill pages 1930 * (see vm.fast_fault) as well as make them writable, which greatly 1931 * reduces the number of page faults programs incur. 1932 * 1933 * Application performance when pre-faulting zero-fill pages is heavily 1934 * dependent on the application. Very tiny applications like /bin/echo 1935 * lose a little performance while applications of any appreciable size 1936 * gain performance. Prefaulting multiple pages also reduces SMP 1937 * congestion and can improve SMP performance significantly. 1938 * 1939 * NOTE! prot may allow writing but this only applies to the top level 1940 * object. If we wind up mapping a page extracted from a backing 1941 * object we have to make sure it is read-only. 1942 * 1943 * NOTE! The caller has already handled any COW operations on the 1944 * vm_map_entry via the normal fault code. Do NOT call this 1945 * shortcut unless the normal fault code has run on this entry. 1946 * 1947 * No other requirements. 1948 */ 1949 #define PFBAK 4 1950 #define PFFOR 4 1951 #define PAGEORDER_SIZE (PFBAK+PFFOR) 1952 1953 static int vm_prefault_pageorder[] = { 1954 -PAGE_SIZE, PAGE_SIZE, 1955 -2 * PAGE_SIZE, 2 * PAGE_SIZE, 1956 -3 * PAGE_SIZE, 3 * PAGE_SIZE, 1957 -4 * PAGE_SIZE, 4 * PAGE_SIZE 1958 }; 1959 1960 static void 1961 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot) 1962 { 1963 struct lwp *lp; 1964 vm_page_t m; 1965 vm_offset_t starta; 1966 vm_offset_t addr; 1967 vm_pindex_t index; 1968 vm_pindex_t pindex; 1969 vm_object_t object; 1970 int pprot; 1971 int i; 1972 1973 /* 1974 * We do not currently prefault mappings that use virtual page 1975 * tables. We do not prefault foreign pmaps. 1976 */ 1977 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) 1978 return; 1979 lp = curthread->td_lwp; 1980 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace))) 1981 return; 1982 1983 object = entry->object.vm_object; 1984 1985 starta = addra - PFBAK * PAGE_SIZE; 1986 if (starta < entry->start) 1987 starta = entry->start; 1988 else if (starta > addra) 1989 starta = 0; 1990 1991 /* 1992 * critical section protection is required to maintain the 1993 * page/object association, interrupts can free pages and remove 1994 * them from their objects. 1995 */ 1996 crit_enter(); 1997 lwkt_gettoken(&vm_token); 1998 for (i = 0; i < PAGEORDER_SIZE; i++) { 1999 vm_object_t lobject; 2000 int allocated = 0; 2001 2002 addr = addra + vm_prefault_pageorder[i]; 2003 if (addr > addra + (PFFOR * PAGE_SIZE)) 2004 addr = 0; 2005 2006 if (addr < starta || addr >= entry->end) 2007 continue; 2008 2009 if (pmap_prefault_ok(pmap, addr) == 0) 2010 continue; 2011 2012 /* 2013 * Follow the VM object chain to obtain the page to be mapped 2014 * into the pmap. 2015 * 2016 * If we reach the terminal object without finding a page 2017 * and we determine it would be advantageous, then allocate 2018 * a zero-fill page for the base object. The base object 2019 * is guaranteed to be OBJT_DEFAULT for this case. 2020 * 2021 * In order to not have to check the pager via *haspage*() 2022 * we stop if any non-default object is encountered. e.g. 2023 * a vnode or swap object would stop the loop. 2024 */ 2025 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 2026 lobject = object; 2027 pindex = index; 2028 pprot = prot; 2029 2030 while ((m = vm_page_lookup(lobject, pindex)) == NULL) { 2031 if (lobject->type != OBJT_DEFAULT) 2032 break; 2033 if (lobject->backing_object == NULL) { 2034 if (vm_fast_fault == 0) 2035 break; 2036 if (vm_prefault_pageorder[i] < 0 || 2037 (prot & VM_PROT_WRITE) == 0 || 2038 vm_page_count_min(0)) { 2039 break; 2040 } 2041 /* note: allocate from base object */ 2042 m = vm_page_alloc(object, index, 2043 VM_ALLOC_NORMAL | VM_ALLOC_ZERO); 2044 2045 if ((m->flags & PG_ZERO) == 0) { 2046 vm_page_zero_fill(m); 2047 } else { 2048 vm_page_flag_clear(m, PG_ZERO); 2049 mycpu->gd_cnt.v_ozfod++; 2050 } 2051 mycpu->gd_cnt.v_zfod++; 2052 m->valid = VM_PAGE_BITS_ALL; 2053 allocated = 1; 2054 pprot = prot; 2055 /* lobject = object .. not needed */ 2056 break; 2057 } 2058 if (lobject->backing_object_offset & PAGE_MASK) 2059 break; 2060 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 2061 lobject = lobject->backing_object; 2062 pprot &= ~VM_PROT_WRITE; 2063 } 2064 /* 2065 * NOTE: lobject now invalid (if we did a zero-fill we didn't 2066 * bother assigning lobject = object). 2067 * 2068 * Give-up if the page is not available. 2069 */ 2070 if (m == NULL) 2071 break; 2072 2073 /* 2074 * Do not conditionalize on PG_RAM. If pages are present in 2075 * the VM system we assume optimal caching. If caching is 2076 * not optimal the I/O gravy train will be restarted when we 2077 * hit an unavailable page. We do not want to try to restart 2078 * the gravy train now because we really don't know how much 2079 * of the object has been cached. The cost for restarting 2080 * the gravy train should be low (since accesses will likely 2081 * be I/O bound anyway). 2082 * 2083 * The object must be marked dirty if we are mapping a 2084 * writable page. 2085 */ 2086 if (pprot & VM_PROT_WRITE) 2087 vm_object_set_writeable_dirty(m->object); 2088 2089 /* 2090 * Enter the page into the pmap if appropriate. If we had 2091 * allocated the page we have to place it on a queue. If not 2092 * we just have to make sure it isn't on the cache queue 2093 * (pages on the cache queue are not allowed to be mapped). 2094 */ 2095 if (allocated) { 2096 pmap_enter(pmap, addr, m, pprot, 0); 2097 vm_page_deactivate(m); 2098 vm_page_wakeup(m); 2099 } else if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 2100 (m->busy == 0) && 2101 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 2102 2103 if ((m->queue - m->pc) == PQ_CACHE) { 2104 vm_page_deactivate(m); 2105 } 2106 vm_page_busy(m); 2107 pmap_enter(pmap, addr, m, pprot, 0); 2108 vm_page_wakeup(m); 2109 } 2110 } 2111 lwkt_reltoken(&vm_token); 2112 crit_exit(); 2113 } 2114