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