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