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