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