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