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