1 /* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1994 John S. Dyson 5 * All rights reserved. 6 * Copyright (c) 1994 David Greenman 7 * All rights reserved. 8 * 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94 42 * 43 * 44 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 45 * All rights reserved. 46 * 47 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 48 * 49 * Permission to use, copy, modify and distribute this software and 50 * its documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 * 69 * $FreeBSD$ 70 */ 71 72 /* 73 * Page fault handling module. 74 */ 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/kernel.h> 78 #include <sys/lock.h> 79 #include <sys/mutex.h> 80 #include <sys/proc.h> 81 #include <sys/resourcevar.h> 82 #include <sys/sysctl.h> 83 #include <sys/vmmeter.h> 84 #include <sys/vnode.h> 85 86 #include <vm/vm.h> 87 #include <vm/vm_param.h> 88 #include <vm/pmap.h> 89 #include <vm/vm_map.h> 90 #include <vm/vm_object.h> 91 #include <vm/vm_page.h> 92 #include <vm/vm_pageout.h> 93 #include <vm/vm_kern.h> 94 #include <vm/vm_pager.h> 95 #include <vm/vnode_pager.h> 96 #include <vm/vm_extern.h> 97 98 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *); 99 100 #define VM_FAULT_READ_AHEAD 8 101 #define VM_FAULT_READ_BEHIND 7 102 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1) 103 104 struct faultstate { 105 vm_page_t m; 106 vm_object_t object; 107 vm_pindex_t pindex; 108 vm_page_t first_m; 109 vm_object_t first_object; 110 vm_pindex_t first_pindex; 111 vm_map_t map; 112 vm_map_entry_t entry; 113 int lookup_still_valid; 114 struct vnode *vp; 115 }; 116 117 static __inline void 118 release_page(struct faultstate *fs) 119 { 120 vm_page_lock_queues(); 121 vm_page_wakeup(fs->m); 122 vm_page_deactivate(fs->m); 123 vm_page_unlock_queues(); 124 fs->m = NULL; 125 } 126 127 static __inline void 128 unlock_map(struct faultstate *fs) 129 { 130 if (fs->lookup_still_valid) { 131 vm_map_lookup_done(fs->map, fs->entry); 132 fs->lookup_still_valid = FALSE; 133 } 134 } 135 136 static void 137 _unlock_things(struct faultstate *fs, int dealloc) 138 { 139 GIANT_REQUIRED; 140 vm_object_pip_wakeup(fs->object); 141 if (fs->object != fs->first_object) { 142 vm_page_lock_queues(); 143 vm_page_free(fs->first_m); 144 vm_page_unlock_queues(); 145 vm_object_pip_wakeup(fs->first_object); 146 fs->first_m = NULL; 147 } 148 if (dealloc) { 149 vm_object_deallocate(fs->first_object); 150 } 151 unlock_map(fs); 152 if (fs->vp != NULL) { 153 vput(fs->vp); 154 fs->vp = NULL; 155 } 156 } 157 158 #define unlock_things(fs) _unlock_things(fs, 0) 159 #define unlock_and_deallocate(fs) _unlock_things(fs, 1) 160 161 /* 162 * TRYPAGER - used by vm_fault to calculate whether the pager for the 163 * current object *might* contain the page. 164 * 165 * default objects are zero-fill, there is no real pager. 166 */ 167 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \ 168 (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired)) 169 170 /* 171 * vm_fault: 172 * 173 * Handle a page fault occurring at the given address, 174 * requiring the given permissions, in the map specified. 175 * If successful, the page is inserted into the 176 * associated physical map. 177 * 178 * NOTE: the given address should be truncated to the 179 * proper page address. 180 * 181 * KERN_SUCCESS is returned if the page fault is handled; otherwise, 182 * a standard error specifying why the fault is fatal is returned. 183 * 184 * 185 * The map in question must be referenced, and remains so. 186 * Caller may hold no locks. 187 */ 188 int 189 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 190 int fault_flags) 191 { 192 vm_prot_t prot; 193 int result; 194 boolean_t growstack, wired; 195 int map_generation; 196 vm_object_t next_object; 197 vm_page_t marray[VM_FAULT_READ]; 198 int hardfault; 199 int faultcount; 200 struct faultstate fs; 201 202 hardfault = 0; 203 growstack = TRUE; 204 atomic_add_int(&cnt.v_vm_faults, 1); 205 206 mtx_lock(&Giant); 207 RetryFault:; 208 209 /* 210 * Find the backing store object and offset into it to begin the 211 * search. 212 */ 213 fs.map = map; 214 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry, 215 &fs.first_object, &fs.first_pindex, &prot, &wired); 216 if (result != KERN_SUCCESS) { 217 if (result != KERN_PROTECTION_FAILURE || 218 (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) { 219 if (growstack && result == KERN_INVALID_ADDRESS && 220 map != kernel_map && curproc != NULL) { 221 result = vm_map_growstack(curproc, vaddr); 222 if (result != KERN_SUCCESS) { 223 mtx_unlock(&Giant); 224 return (KERN_FAILURE); 225 } 226 growstack = FALSE; 227 goto RetryFault; 228 } 229 mtx_unlock(&Giant); 230 return (result); 231 } 232 233 /* 234 * If we are user-wiring a r/w segment, and it is COW, then 235 * we need to do the COW operation. Note that we don't COW 236 * currently RO sections now, because it is NOT desirable 237 * to COW .text. We simply keep .text from ever being COW'ed 238 * and take the heat that one cannot debug wired .text sections. 239 */ 240 result = vm_map_lookup(&fs.map, vaddr, 241 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE, 242 &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired); 243 if (result != KERN_SUCCESS) { 244 mtx_unlock(&Giant); 245 return (result); 246 } 247 248 /* 249 * If we don't COW now, on a user wire, the user will never 250 * be able to write to the mapping. If we don't make this 251 * restriction, the bookkeeping would be nearly impossible. 252 * 253 * XXX The following assignment modifies the map without 254 * holding a write lock on it. 255 */ 256 if ((fs.entry->protection & VM_PROT_WRITE) == 0) 257 fs.entry->max_protection &= ~VM_PROT_WRITE; 258 } 259 260 map_generation = fs.map->timestamp; 261 262 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 263 panic("vm_fault: fault on nofault entry, addr: %lx", 264 (u_long)vaddr); 265 } 266 267 /* 268 * Make a reference to this object to prevent its disposal while we 269 * are messing with it. Once we have the reference, the map is free 270 * to be diddled. Since objects reference their shadows (and copies), 271 * they will stay around as well. 272 * 273 * Bump the paging-in-progress count to prevent size changes (e.g. 274 * truncation operations) during I/O. This must be done after 275 * obtaining the vnode lock in order to avoid possible deadlocks. 276 * 277 * XXX vnode_pager_lock() can block without releasing the map lock. 278 */ 279 vm_object_reference(fs.first_object); 280 fs.vp = vnode_pager_lock(fs.first_object); 281 vm_object_pip_add(fs.first_object, 1); 282 283 #ifdef ENABLE_VFS_IOOPT 284 if ((fault_type & VM_PROT_WRITE) && 285 (fs.first_object->type == OBJT_VNODE)) { 286 vm_freeze_copyopts(fs.first_object, 287 fs.first_pindex, fs.first_pindex + 1); 288 } 289 #endif 290 fs.lookup_still_valid = TRUE; 291 292 if (wired) 293 fault_type = prot; 294 295 fs.first_m = NULL; 296 297 /* 298 * Search for the page at object/offset. 299 */ 300 fs.object = fs.first_object; 301 fs.pindex = fs.first_pindex; 302 while (TRUE) { 303 /* 304 * If the object is dead, we stop here 305 */ 306 if (fs.object->flags & OBJ_DEAD) { 307 unlock_and_deallocate(&fs); 308 mtx_unlock(&Giant); 309 return (KERN_PROTECTION_FAILURE); 310 } 311 312 /* 313 * See if page is resident 314 */ 315 fs.m = vm_page_lookup(fs.object, fs.pindex); 316 if (fs.m != NULL) { 317 int queue, s; 318 319 /* 320 * check for page-based copy on write 321 */ 322 323 if ((fs.m->cow) && 324 (fault_type & VM_PROT_WRITE)) { 325 s = splvm(); 326 vm_page_cowfault(fs.m); 327 splx(s); 328 unlock_things(&fs); 329 goto RetryFault; 330 } 331 332 /* 333 * Wait/Retry if the page is busy. We have to do this 334 * if the page is busy via either PG_BUSY or 335 * vm_page_t->busy because the vm_pager may be using 336 * vm_page_t->busy for pageouts ( and even pageins if 337 * it is the vnode pager ), and we could end up trying 338 * to pagein and pageout the same page simultaneously. 339 * 340 * We can theoretically allow the busy case on a read 341 * fault if the page is marked valid, but since such 342 * pages are typically already pmap'd, putting that 343 * special case in might be more effort then it is 344 * worth. We cannot under any circumstances mess 345 * around with a vm_page_t->busy page except, perhaps, 346 * to pmap it. 347 */ 348 vm_page_lock_queues(); 349 if ((fs.m->flags & PG_BUSY) || fs.m->busy) { 350 vm_page_unlock_queues(); 351 unlock_things(&fs); 352 (void)vm_page_sleep_busy(fs.m, TRUE, "vmpfw"); 353 cnt.v_intrans++; 354 vm_object_deallocate(fs.first_object); 355 goto RetryFault; 356 } 357 queue = fs.m->queue; 358 359 s = splvm(); 360 vm_pageq_remove_nowakeup(fs.m); 361 splx(s); 362 363 if ((queue - fs.m->pc) == PQ_CACHE && vm_page_count_severe()) { 364 vm_page_activate(fs.m); 365 vm_page_unlock_queues(); 366 unlock_and_deallocate(&fs); 367 VM_WAITPFAULT; 368 goto RetryFault; 369 } 370 371 /* 372 * Mark page busy for other processes, and the 373 * pagedaemon. If it still isn't completely valid 374 * (readable), jump to readrest, else break-out ( we 375 * found the page ). 376 */ 377 vm_page_busy(fs.m); 378 vm_page_unlock_queues(); 379 if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) && 380 fs.m->object != kernel_object && fs.m->object != kmem_object) { 381 goto readrest; 382 } 383 384 break; 385 } 386 387 /* 388 * Page is not resident, If this is the search termination 389 * or the pager might contain the page, allocate a new page. 390 */ 391 if (TRYPAGER || fs.object == fs.first_object) { 392 if (fs.pindex >= fs.object->size) { 393 unlock_and_deallocate(&fs); 394 mtx_unlock(&Giant); 395 return (KERN_PROTECTION_FAILURE); 396 } 397 398 /* 399 * Allocate a new page for this object/offset pair. 400 */ 401 fs.m = NULL; 402 if (!vm_page_count_severe()) { 403 fs.m = vm_page_alloc(fs.object, fs.pindex, 404 (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO); 405 } 406 if (fs.m == NULL) { 407 unlock_and_deallocate(&fs); 408 VM_WAITPFAULT; 409 goto RetryFault; 410 } 411 } 412 413 readrest: 414 /* 415 * We have found a valid page or we have allocated a new page. 416 * The page thus may not be valid or may not be entirely 417 * valid. 418 * 419 * Attempt to fault-in the page if there is a chance that the 420 * pager has it, and potentially fault in additional pages 421 * at the same time. 422 */ 423 if (TRYPAGER) { 424 int rv; 425 int reqpage; 426 int ahead, behind; 427 u_char behavior = vm_map_entry_behavior(fs.entry); 428 429 if (behavior == MAP_ENTRY_BEHAV_RANDOM) { 430 ahead = 0; 431 behind = 0; 432 } else { 433 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT; 434 if (behind > VM_FAULT_READ_BEHIND) 435 behind = VM_FAULT_READ_BEHIND; 436 437 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1; 438 if (ahead > VM_FAULT_READ_AHEAD) 439 ahead = VM_FAULT_READ_AHEAD; 440 } 441 442 if ((fs.first_object->type != OBJT_DEVICE) && 443 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL || 444 (behavior != MAP_ENTRY_BEHAV_RANDOM && 445 fs.pindex >= fs.entry->lastr && 446 fs.pindex < fs.entry->lastr + VM_FAULT_READ)) 447 ) { 448 vm_pindex_t firstpindex, tmppindex; 449 450 if (fs.first_pindex < 2 * VM_FAULT_READ) 451 firstpindex = 0; 452 else 453 firstpindex = fs.first_pindex - 2 * VM_FAULT_READ; 454 455 vm_page_lock_queues(); 456 /* 457 * note: partially valid pages cannot be 458 * included in the lookahead - NFS piecemeal 459 * writes will barf on it badly. 460 */ 461 for (tmppindex = fs.first_pindex - 1; 462 tmppindex >= firstpindex; 463 --tmppindex) { 464 vm_page_t mt; 465 466 mt = vm_page_lookup(fs.first_object, tmppindex); 467 if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL)) 468 break; 469 if (mt->busy || 470 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) || 471 mt->hold_count || 472 mt->wire_count) 473 continue; 474 if (mt->dirty == 0) 475 vm_page_test_dirty(mt); 476 if (mt->dirty) { 477 vm_page_protect(mt, VM_PROT_NONE); 478 vm_page_deactivate(mt); 479 } else { 480 vm_page_cache(mt); 481 } 482 } 483 vm_page_unlock_queues(); 484 ahead += behind; 485 behind = 0; 486 } 487 488 /* 489 * now we find out if any other pages should be paged 490 * in at this time this routine checks to see if the 491 * pages surrounding this fault reside in the same 492 * object as the page for this fault. If they do, 493 * then they are faulted in also into the object. The 494 * array "marray" returned contains an array of 495 * vm_page_t structs where one of them is the 496 * vm_page_t passed to the routine. The reqpage 497 * return value is the index into the marray for the 498 * vm_page_t passed to the routine. 499 * 500 * fs.m plus the additional pages are PG_BUSY'd. 501 * 502 * XXX vm_fault_additional_pages() can block 503 * without releasing the map lock. 504 */ 505 faultcount = vm_fault_additional_pages( 506 fs.m, behind, ahead, marray, &reqpage); 507 508 /* 509 * update lastr imperfectly (we do not know how much 510 * getpages will actually read), but good enough. 511 * 512 * XXX The following assignment modifies the map 513 * without holding a write lock on it. 514 */ 515 fs.entry->lastr = fs.pindex + faultcount - behind; 516 517 /* 518 * Call the pager to retrieve the data, if any, after 519 * releasing the lock on the map. We hold a ref on 520 * fs.object and the pages are PG_BUSY'd. 521 */ 522 unlock_map(&fs); 523 524 rv = faultcount ? 525 vm_pager_get_pages(fs.object, marray, faultcount, 526 reqpage) : VM_PAGER_FAIL; 527 528 if (rv == VM_PAGER_OK) { 529 /* 530 * Found the page. Leave it busy while we play 531 * with it. 532 */ 533 534 /* 535 * Relookup in case pager changed page. Pager 536 * is responsible for disposition of old page 537 * if moved. 538 */ 539 fs.m = vm_page_lookup(fs.object, fs.pindex); 540 if (!fs.m) { 541 unlock_and_deallocate(&fs); 542 goto RetryFault; 543 } 544 545 hardfault++; 546 break; /* break to PAGE HAS BEEN FOUND */ 547 } 548 /* 549 * Remove the bogus page (which does not exist at this 550 * object/offset); before doing so, we must get back 551 * our object lock to preserve our invariant. 552 * 553 * Also wake up any other process that may want to bring 554 * in this page. 555 * 556 * If this is the top-level object, we must leave the 557 * busy page to prevent another process from rushing 558 * past us, and inserting the page in that object at 559 * the same time that we are. 560 */ 561 if (rv == VM_PAGER_ERROR) 562 printf("vm_fault: pager read error, pid %d (%s)\n", 563 curproc->p_pid, curproc->p_comm); 564 /* 565 * Data outside the range of the pager or an I/O error 566 */ 567 /* 568 * XXX - the check for kernel_map is a kludge to work 569 * around having the machine panic on a kernel space 570 * fault w/ I/O error. 571 */ 572 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) || 573 (rv == VM_PAGER_BAD)) { 574 vm_page_lock_queues(); 575 vm_page_free(fs.m); 576 vm_page_unlock_queues(); 577 fs.m = NULL; 578 unlock_and_deallocate(&fs); 579 mtx_unlock(&Giant); 580 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE); 581 } 582 if (fs.object != fs.first_object) { 583 vm_page_lock_queues(); 584 vm_page_free(fs.m); 585 vm_page_unlock_queues(); 586 fs.m = NULL; 587 /* 588 * XXX - we cannot just fall out at this 589 * point, m has been freed and is invalid! 590 */ 591 } 592 } 593 594 /* 595 * We get here if the object has default pager (or unwiring) 596 * or the pager doesn't have the page. 597 */ 598 if (fs.object == fs.first_object) 599 fs.first_m = fs.m; 600 601 /* 602 * Move on to the next object. Lock the next object before 603 * unlocking the current one. 604 */ 605 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset); 606 next_object = fs.object->backing_object; 607 if (next_object == NULL) { 608 /* 609 * If there's no object left, fill the page in the top 610 * object with zeros. 611 */ 612 if (fs.object != fs.first_object) { 613 vm_object_pip_wakeup(fs.object); 614 615 fs.object = fs.first_object; 616 fs.pindex = fs.first_pindex; 617 fs.m = fs.first_m; 618 } 619 fs.first_m = NULL; 620 621 /* 622 * Zero the page if necessary and mark it valid. 623 */ 624 if ((fs.m->flags & PG_ZERO) == 0) { 625 vm_page_zero_fill(fs.m); 626 } else { 627 cnt.v_ozfod++; 628 } 629 cnt.v_zfod++; 630 fs.m->valid = VM_PAGE_BITS_ALL; 631 break; /* break to PAGE HAS BEEN FOUND */ 632 } else { 633 if (fs.object != fs.first_object) { 634 vm_object_pip_wakeup(fs.object); 635 } 636 KASSERT(fs.object != next_object, ("object loop %p", next_object)); 637 fs.object = next_object; 638 vm_object_pip_add(fs.object, 1); 639 } 640 } 641 642 KASSERT((fs.m->flags & PG_BUSY) != 0, 643 ("vm_fault: not busy after main loop")); 644 645 /* 646 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 647 * is held.] 648 */ 649 650 /* 651 * If the page is being written, but isn't already owned by the 652 * top-level object, we have to copy it into a new page owned by the 653 * top-level object. 654 */ 655 if (fs.object != fs.first_object) { 656 /* 657 * We only really need to copy if we want to write it. 658 */ 659 if (fault_type & VM_PROT_WRITE) { 660 /* 661 * This allows pages to be virtually copied from a 662 * backing_object into the first_object, where the 663 * backing object has no other refs to it, and cannot 664 * gain any more refs. Instead of a bcopy, we just 665 * move the page from the backing object to the 666 * first object. Note that we must mark the page 667 * dirty in the first object so that it will go out 668 * to swap when needed. 669 */ 670 if (map_generation == fs.map->timestamp && 671 /* 672 * Only one shadow object 673 */ 674 (fs.object->shadow_count == 1) && 675 /* 676 * No COW refs, except us 677 */ 678 (fs.object->ref_count == 1) && 679 /* 680 * No one else can look this object up 681 */ 682 (fs.object->handle == NULL) && 683 /* 684 * No other ways to look the object up 685 */ 686 ((fs.object->type == OBJT_DEFAULT) || 687 (fs.object->type == OBJT_SWAP)) && 688 /* 689 * We don't chase down the shadow chain 690 */ 691 (fs.object == fs.first_object->backing_object) && 692 693 /* 694 * grab the lock if we need to 695 */ 696 (fs.lookup_still_valid || vm_map_trylock(fs.map))) { 697 698 fs.lookup_still_valid = 1; 699 /* 700 * get rid of the unnecessary page 701 */ 702 vm_page_lock_queues(); 703 vm_page_protect(fs.first_m, VM_PROT_NONE); 704 vm_page_free(fs.first_m); 705 vm_page_unlock_queues(); 706 fs.first_m = NULL; 707 708 /* 709 * grab the page and put it into the 710 * process'es object. The page is 711 * automatically made dirty. 712 */ 713 vm_page_rename(fs.m, fs.first_object, fs.first_pindex); 714 fs.first_m = fs.m; 715 vm_page_busy(fs.first_m); 716 fs.m = NULL; 717 cnt.v_cow_optim++; 718 } else { 719 /* 720 * Oh, well, lets copy it. 721 */ 722 vm_page_copy(fs.m, fs.first_m); 723 } 724 725 if (fs.m) { 726 /* 727 * We no longer need the old page or object. 728 */ 729 release_page(&fs); 730 } 731 732 /* 733 * fs.object != fs.first_object due to above 734 * conditional 735 */ 736 vm_object_pip_wakeup(fs.object); 737 738 /* 739 * Only use the new page below... 740 */ 741 cnt.v_cow_faults++; 742 fs.m = fs.first_m; 743 fs.object = fs.first_object; 744 fs.pindex = fs.first_pindex; 745 746 } else { 747 prot &= ~VM_PROT_WRITE; 748 } 749 } 750 751 /* 752 * We must verify that the maps have not changed since our last 753 * lookup. 754 */ 755 if (!fs.lookup_still_valid && 756 (fs.map->timestamp != map_generation)) { 757 vm_object_t retry_object; 758 vm_pindex_t retry_pindex; 759 vm_prot_t retry_prot; 760 761 /* 762 * Since map entries may be pageable, make sure we can take a 763 * page fault on them. 764 */ 765 766 /* 767 * Unlock vnode before the lookup to avoid deadlock. E.G. 768 * avoid a deadlock between the inode and exec_map that can 769 * occur due to locks being obtained in different orders. 770 */ 771 if (fs.vp != NULL) { 772 vput(fs.vp); 773 fs.vp = NULL; 774 } 775 776 if (fs.map->infork) { 777 release_page(&fs); 778 unlock_and_deallocate(&fs); 779 goto RetryFault; 780 } 781 782 /* 783 * To avoid trying to write_lock the map while another process 784 * has it read_locked (in vm_map_pageable), we do not try for 785 * write permission. If the page is still writable, we will 786 * get write permission. If it is not, or has been marked 787 * needs_copy, we enter the mapping without write permission, 788 * and will merely take another fault. 789 */ 790 result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE, 791 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 792 map_generation = fs.map->timestamp; 793 794 /* 795 * If we don't need the page any longer, put it on the active 796 * list (the easiest thing to do here). If no one needs it, 797 * pageout will grab it eventually. 798 */ 799 if (result != KERN_SUCCESS) { 800 release_page(&fs); 801 unlock_and_deallocate(&fs); 802 mtx_unlock(&Giant); 803 return (result); 804 } 805 fs.lookup_still_valid = TRUE; 806 807 if ((retry_object != fs.first_object) || 808 (retry_pindex != fs.first_pindex)) { 809 release_page(&fs); 810 unlock_and_deallocate(&fs); 811 goto RetryFault; 812 } 813 /* 814 * Check whether the protection has changed or the object has 815 * been copied while we left the map unlocked. Changing from 816 * read to write permission is OK - we leave the page 817 * write-protected, and catch the write fault. Changing from 818 * write to read permission means that we can't mark the page 819 * write-enabled after all. 820 */ 821 prot &= retry_prot; 822 } 823 824 /* 825 * Put this page into the physical map. We had to do the unlock above 826 * because pmap_enter may cause other faults. We don't put the page 827 * back on the active queue until later so that the page-out daemon 828 * won't find us (yet). 829 */ 830 831 if (prot & VM_PROT_WRITE) { 832 vm_page_flag_set(fs.m, PG_WRITEABLE); 833 vm_object_set_writeable_dirty(fs.m->object); 834 835 /* 836 * If the fault is a write, we know that this page is being 837 * written NOW so dirty it explicitly to save on 838 * pmap_is_modified() calls later. 839 * 840 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC 841 * if the page is already dirty to prevent data written with 842 * the expectation of being synced from not being synced. 843 * Likewise if this entry does not request NOSYNC then make 844 * sure the page isn't marked NOSYNC. Applications sharing 845 * data should use the same flags to avoid ping ponging. 846 * 847 * Also tell the backing pager, if any, that it should remove 848 * any swap backing since the page is now dirty. 849 */ 850 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) { 851 if (fs.m->dirty == 0) 852 vm_page_flag_set(fs.m, PG_NOSYNC); 853 } else { 854 vm_page_flag_clear(fs.m, PG_NOSYNC); 855 } 856 if (fault_flags & VM_FAULT_DIRTY) { 857 int s; 858 vm_page_dirty(fs.m); 859 s = splvm(); 860 vm_pager_page_unswapped(fs.m); 861 splx(s); 862 } 863 } 864 865 /* 866 * Page had better still be busy 867 */ 868 KASSERT(fs.m->flags & PG_BUSY, 869 ("vm_fault: page %p not busy!", fs.m)); 870 unlock_things(&fs); 871 872 /* 873 * Sanity check: page must be completely valid or it is not fit to 874 * map into user space. vm_pager_get_pages() ensures this. 875 */ 876 if (fs.m->valid != VM_PAGE_BITS_ALL) { 877 vm_page_zero_invalid(fs.m, TRUE); 878 printf("Warning: page %p partially invalid on fault\n", fs.m); 879 } 880 pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired); 881 if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) { 882 pmap_prefault(fs.map->pmap, vaddr, fs.entry); 883 } 884 vm_page_lock_queues(); 885 vm_page_flag_clear(fs.m, PG_ZERO); 886 vm_page_flag_set(fs.m, PG_REFERENCED); 887 888 /* 889 * If the page is not wired down, then put it where the pageout daemon 890 * can find it. 891 */ 892 if (fault_flags & VM_FAULT_WIRE_MASK) { 893 if (wired) 894 vm_page_wire(fs.m); 895 else 896 vm_page_unwire(fs.m, 1); 897 } else { 898 vm_page_activate(fs.m); 899 } 900 vm_page_wakeup(fs.m); 901 vm_page_unlock_queues(); 902 mtx_lock_spin(&sched_lock); 903 if (curproc && (curproc->p_sflag & PS_INMEM) && curproc->p_stats) { 904 if (hardfault) { 905 curproc->p_stats->p_ru.ru_majflt++; 906 } else { 907 curproc->p_stats->p_ru.ru_minflt++; 908 } 909 } 910 mtx_unlock_spin(&sched_lock); 911 912 /* 913 * Unlock everything, and return 914 */ 915 vm_object_deallocate(fs.first_object); 916 mtx_unlock(&Giant); 917 return (KERN_SUCCESS); 918 } 919 920 /* 921 * vm_fault_wire: 922 * 923 * Wire down a range of virtual addresses in a map. 924 */ 925 int 926 vm_fault_wire(map, start, end, user_wire) 927 vm_map_t map; 928 vm_offset_t start, end; 929 boolean_t user_wire; 930 { 931 vm_offset_t va; 932 int rv; 933 934 /* 935 * Inform the physical mapping system that the range of addresses may 936 * not fault, so that page tables and such can be locked down as well. 937 */ 938 pmap_pageable(map->pmap, start, end, FALSE); 939 940 /* 941 * We simulate a fault to get the page and enter it in the physical 942 * map. For user wiring, we only ask for read access on currently 943 * read-only sections. 944 */ 945 for (va = start; va < end; va += PAGE_SIZE) { 946 rv = vm_fault(map, va, 947 user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE, 948 user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING); 949 if (rv) { 950 if (va != start) 951 vm_fault_unwire(map, start, va); 952 return (rv); 953 } 954 } 955 return (KERN_SUCCESS); 956 } 957 958 /* 959 * vm_fault_unwire: 960 * 961 * Unwire a range of virtual addresses in a map. 962 */ 963 void 964 vm_fault_unwire(map, start, end) 965 vm_map_t map; 966 vm_offset_t start, end; 967 { 968 vm_offset_t va, pa; 969 pmap_t pmap; 970 971 pmap = vm_map_pmap(map); 972 973 mtx_lock(&Giant); 974 /* 975 * Since the pages are wired down, we must be able to get their 976 * mappings from the physical map system. 977 */ 978 for (va = start; va < end; va += PAGE_SIZE) { 979 pa = pmap_extract(pmap, va); 980 if (pa != (vm_offset_t) 0) { 981 pmap_change_wiring(pmap, va, FALSE); 982 vm_page_lock_queues(); 983 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1); 984 vm_page_unlock_queues(); 985 } 986 } 987 mtx_unlock(&Giant); 988 989 /* 990 * Inform the physical mapping system that the range of addresses may 991 * fault, so that page tables and such may be unwired themselves. 992 */ 993 pmap_pageable(pmap, start, end, TRUE); 994 } 995 996 /* 997 * Routine: 998 * vm_fault_copy_entry 999 * Function: 1000 * Copy all of the pages from a wired-down map entry to another. 1001 * 1002 * In/out conditions: 1003 * The source and destination maps must be locked for write. 1004 * The source map entry must be wired down (or be a sharing map 1005 * entry corresponding to a main map entry that is wired down). 1006 */ 1007 void 1008 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry) 1009 vm_map_t dst_map; 1010 vm_map_t src_map; 1011 vm_map_entry_t dst_entry; 1012 vm_map_entry_t src_entry; 1013 { 1014 vm_object_t dst_object; 1015 vm_object_t src_object; 1016 vm_ooffset_t dst_offset; 1017 vm_ooffset_t src_offset; 1018 vm_prot_t prot; 1019 vm_offset_t vaddr; 1020 vm_page_t dst_m; 1021 vm_page_t src_m; 1022 1023 #ifdef lint 1024 src_map++; 1025 #endif /* lint */ 1026 1027 src_object = src_entry->object.vm_object; 1028 src_offset = src_entry->offset; 1029 1030 /* 1031 * Create the top-level object for the destination entry. (Doesn't 1032 * actually shadow anything - we copy the pages directly.) 1033 */ 1034 dst_object = vm_object_allocate(OBJT_DEFAULT, 1035 (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start)); 1036 1037 dst_entry->object.vm_object = dst_object; 1038 dst_entry->offset = 0; 1039 1040 prot = dst_entry->max_protection; 1041 1042 /* 1043 * Loop through all of the pages in the entry's range, copying each 1044 * one from the source object (it should be there) to the destination 1045 * object. 1046 */ 1047 for (vaddr = dst_entry->start, dst_offset = 0; 1048 vaddr < dst_entry->end; 1049 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) { 1050 1051 /* 1052 * Allocate a page in the destination object 1053 */ 1054 do { 1055 dst_m = vm_page_alloc(dst_object, 1056 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL); 1057 if (dst_m == NULL) { 1058 VM_WAIT; 1059 } 1060 } while (dst_m == NULL); 1061 1062 /* 1063 * Find the page in the source object, and copy it in. 1064 * (Because the source is wired down, the page will be in 1065 * memory.) 1066 */ 1067 src_m = vm_page_lookup(src_object, 1068 OFF_TO_IDX(dst_offset + src_offset)); 1069 if (src_m == NULL) 1070 panic("vm_fault_copy_wired: page missing"); 1071 1072 vm_page_copy(src_m, dst_m); 1073 1074 /* 1075 * Enter it in the pmap... 1076 */ 1077 vm_page_flag_clear(dst_m, PG_ZERO); 1078 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE); 1079 vm_page_lock_queues(); 1080 vm_page_flag_set(dst_m, PG_WRITEABLE); 1081 1082 /* 1083 * Mark it no longer busy, and put it on the active list. 1084 */ 1085 vm_page_activate(dst_m); 1086 vm_page_wakeup(dst_m); 1087 vm_page_unlock_queues(); 1088 } 1089 } 1090 1091 1092 /* 1093 * This routine checks around the requested page for other pages that 1094 * might be able to be faulted in. This routine brackets the viable 1095 * pages for the pages to be paged in. 1096 * 1097 * Inputs: 1098 * m, rbehind, rahead 1099 * 1100 * Outputs: 1101 * marray (array of vm_page_t), reqpage (index of requested page) 1102 * 1103 * Return value: 1104 * number of pages in marray 1105 * 1106 * This routine can't block. 1107 */ 1108 static int 1109 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage) 1110 vm_page_t m; 1111 int rbehind; 1112 int rahead; 1113 vm_page_t *marray; 1114 int *reqpage; 1115 { 1116 int i,j; 1117 vm_object_t object; 1118 vm_pindex_t pindex, startpindex, endpindex, tpindex; 1119 vm_page_t rtm; 1120 int cbehind, cahead; 1121 1122 GIANT_REQUIRED; 1123 1124 object = m->object; 1125 pindex = m->pindex; 1126 1127 /* 1128 * we don't fault-ahead for device pager 1129 */ 1130 if (object->type == OBJT_DEVICE) { 1131 *reqpage = 0; 1132 marray[0] = m; 1133 return 1; 1134 } 1135 1136 /* 1137 * if the requested page is not available, then give up now 1138 */ 1139 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) { 1140 return 0; 1141 } 1142 1143 if ((cbehind == 0) && (cahead == 0)) { 1144 *reqpage = 0; 1145 marray[0] = m; 1146 return 1; 1147 } 1148 1149 if (rahead > cahead) { 1150 rahead = cahead; 1151 } 1152 1153 if (rbehind > cbehind) { 1154 rbehind = cbehind; 1155 } 1156 1157 /* 1158 * try to do any readahead that we might have free pages for. 1159 */ 1160 if ((rahead + rbehind) > 1161 ((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) { 1162 pagedaemon_wakeup(); 1163 marray[0] = m; 1164 *reqpage = 0; 1165 return 1; 1166 } 1167 1168 /* 1169 * scan backward for the read behind pages -- in memory 1170 */ 1171 if (pindex > 0) { 1172 if (rbehind > pindex) { 1173 rbehind = pindex; 1174 startpindex = 0; 1175 } else { 1176 startpindex = pindex - rbehind; 1177 } 1178 1179 for (tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) { 1180 if (vm_page_lookup(object, tpindex)) { 1181 startpindex = tpindex + 1; 1182 break; 1183 } 1184 if (tpindex == 0) 1185 break; 1186 } 1187 1188 for (i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) { 1189 1190 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 1191 if (rtm == NULL) { 1192 vm_page_lock_queues(); 1193 for (j = 0; j < i; j++) { 1194 vm_page_free(marray[j]); 1195 } 1196 vm_page_unlock_queues(); 1197 marray[0] = m; 1198 *reqpage = 0; 1199 return 1; 1200 } 1201 1202 marray[i] = rtm; 1203 } 1204 } else { 1205 startpindex = 0; 1206 i = 0; 1207 } 1208 1209 marray[i] = m; 1210 /* page offset of the required page */ 1211 *reqpage = i; 1212 1213 tpindex = pindex + 1; 1214 i++; 1215 1216 /* 1217 * scan forward for the read ahead pages 1218 */ 1219 endpindex = tpindex + rahead; 1220 if (endpindex > object->size) 1221 endpindex = object->size; 1222 1223 for (; tpindex < endpindex; i++, tpindex++) { 1224 1225 if (vm_page_lookup(object, tpindex)) { 1226 break; 1227 } 1228 1229 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 1230 if (rtm == NULL) { 1231 break; 1232 } 1233 1234 marray[i] = rtm; 1235 } 1236 1237 /* return number of bytes of pages */ 1238 return i; 1239 } 1240