1 /* $OpenBSD: uvm_fault.c,v 1.18 2001/08/11 10:57:22 art Exp $ */ 2 /* $NetBSD: uvm_fault.c,v 1.48 2000/04/10 01:17:41 thorpej Exp $ */ 3 4 /* 5 * 6 * Copyright (c) 1997 Charles D. Cranor and Washington University. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Charles D. Cranor and 20 * Washington University. 21 * 4. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp 36 */ 37 38 /* 39 * uvm_fault.c: fault handler 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/proc.h> 46 #include <sys/malloc.h> 47 #include <sys/mman.h> 48 #include <sys/user.h> 49 50 #include <vm/vm.h> 51 #include <vm/vm_page.h> 52 #include <vm/vm_kern.h> 53 54 #include <uvm/uvm.h> 55 56 /* 57 * 58 * a word on page faults: 59 * 60 * types of page faults we handle: 61 * 62 * CASE 1: upper layer faults CASE 2: lower layer faults 63 * 64 * CASE 1A CASE 1B CASE 2A CASE 2B 65 * read/write1 write>1 read/write +-cow_write/zero 66 * | | | | 67 * +--|--+ +--|--+ +-----+ + | + | +-----+ 68 * amap | V | | ----------->new| | | | ^ | 69 * +-----+ +-----+ +-----+ + | + | +--|--+ 70 * | | | 71 * +-----+ +-----+ +--|--+ | +--|--+ 72 * uobj | d/c | | d/c | | V | +----| | 73 * +-----+ +-----+ +-----+ +-----+ 74 * 75 * d/c = don't care 76 * 77 * case [0]: layerless fault 78 * no amap or uobj is present. this is an error. 79 * 80 * case [1]: upper layer fault [anon active] 81 * 1A: [read] or [write with anon->an_ref == 1] 82 * I/O takes place in top level anon and uobj is not touched. 83 * 1B: [write with anon->an_ref > 1] 84 * new anon is alloc'd and data is copied off ["COW"] 85 * 86 * case [2]: lower layer fault [uobj] 87 * 2A: [read on non-NULL uobj] or [write to non-copy_on_write area] 88 * I/O takes place directly in object. 89 * 2B: [write to copy_on_write] or [read on NULL uobj] 90 * data is "promoted" from uobj to a new anon. 91 * if uobj is null, then we zero fill. 92 * 93 * we follow the standard UVM locking protocol ordering: 94 * 95 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ) 96 * we hold a PG_BUSY page if we unlock for I/O 97 * 98 * 99 * the code is structured as follows: 100 * 101 * - init the "IN" params in the ufi structure 102 * ReFault: 103 * - do lookups [locks maps], check protection, handle needs_copy 104 * - check for case 0 fault (error) 105 * - establish "range" of fault 106 * - if we have an amap lock it and extract the anons 107 * - if sequential advice deactivate pages behind us 108 * - at the same time check pmap for unmapped areas and anon for pages 109 * that we could map in (and do map it if found) 110 * - check object for resident pages that we could map in 111 * - if (case 2) goto Case2 112 * - >>> handle case 1 113 * - ensure source anon is resident in RAM 114 * - if case 1B alloc new anon and copy from source 115 * - map the correct page in 116 * Case2: 117 * - >>> handle case 2 118 * - ensure source page is resident (if uobj) 119 * - if case 2B alloc new anon and copy from source (could be zero 120 * fill if uobj == NULL) 121 * - map the correct page in 122 * - done! 123 * 124 * note on paging: 125 * if we have to do I/O we place a PG_BUSY page in the correct object, 126 * unlock everything, and do the I/O. when I/O is done we must reverify 127 * the state of the world before assuming that our data structures are 128 * valid. [because mappings could change while the map is unlocked] 129 * 130 * alternative 1: unbusy the page in question and restart the page fault 131 * from the top (ReFault). this is easy but does not take advantage 132 * of the information that we already have from our previous lookup, 133 * although it is possible that the "hints" in the vm_map will help here. 134 * 135 * alternative 2: the system already keeps track of a "version" number of 136 * a map. [i.e. every time you write-lock a map (e.g. to change a 137 * mapping) you bump the version number up by one...] so, we can save 138 * the version number of the map before we release the lock and start I/O. 139 * then when I/O is done we can relock and check the version numbers 140 * to see if anything changed. this might save us some over 1 because 141 * we don't have to unbusy the page and may be less compares(?). 142 * 143 * alternative 3: put in backpointers or a way to "hold" part of a map 144 * in place while I/O is in progress. this could be complex to 145 * implement (especially with structures like amap that can be referenced 146 * by multiple map entries, and figuring out what should wait could be 147 * complex as well...). 148 * 149 * given that we are not currently multiprocessor or multithreaded we might 150 * as well choose alternative 2 now. maybe alternative 3 would be useful 151 * in the future. XXX keep in mind for future consideration//rechecking. 152 */ 153 154 /* 155 * local data structures 156 */ 157 158 struct uvm_advice { 159 int advice; 160 int nback; 161 int nforw; 162 }; 163 164 /* 165 * page range array: 166 * note: index in array must match "advice" value 167 * XXX: borrowed numbers from freebsd. do they work well for us? 168 */ 169 170 static struct uvm_advice uvmadvice[] = { 171 { MADV_NORMAL, 3, 4 }, 172 { MADV_RANDOM, 0, 0 }, 173 { MADV_SEQUENTIAL, 8, 7}, 174 }; 175 176 #define UVM_MAXRANGE 16 /* must be max() of nback+nforw+1 */ 177 178 /* 179 * private prototypes 180 */ 181 182 static void uvmfault_amapcopy __P((struct uvm_faultinfo *)); 183 static __inline void uvmfault_anonflush __P((struct vm_anon **, int)); 184 185 /* 186 * inline functions 187 */ 188 189 /* 190 * uvmfault_anonflush: try and deactivate pages in specified anons 191 * 192 * => does not have to deactivate page if it is busy 193 */ 194 195 static __inline void 196 uvmfault_anonflush(anons, n) 197 struct vm_anon **anons; 198 int n; 199 { 200 int lcv; 201 struct vm_page *pg; 202 203 for (lcv = 0 ; lcv < n ; lcv++) { 204 if (anons[lcv] == NULL) 205 continue; 206 simple_lock(&anons[lcv]->an_lock); 207 pg = anons[lcv]->u.an_page; 208 if (pg && (pg->flags & PG_BUSY) == 0 && pg->loan_count == 0) { 209 uvm_lock_pageq(); 210 if (pg->wire_count == 0) { 211 pmap_page_protect(pg, VM_PROT_NONE); 212 uvm_pagedeactivate(pg); 213 } 214 uvm_unlock_pageq(); 215 } 216 simple_unlock(&anons[lcv]->an_lock); 217 } 218 } 219 220 /* 221 * normal functions 222 */ 223 224 /* 225 * uvmfault_amapcopy: clear "needs_copy" in a map. 226 * 227 * => called with VM data structures unlocked (usually, see below) 228 * => we get a write lock on the maps and clear needs_copy for a VA 229 * => if we are out of RAM we sleep (waiting for more) 230 */ 231 232 static void 233 uvmfault_amapcopy(ufi) 234 struct uvm_faultinfo *ufi; 235 { 236 237 /* 238 * while we haven't done the job 239 */ 240 241 while (1) { 242 243 /* 244 * no mapping? give up. 245 */ 246 247 if (uvmfault_lookup(ufi, TRUE) == FALSE) 248 return; 249 250 /* 251 * copy if needed. 252 */ 253 254 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) 255 amap_copy(ufi->map, ufi->entry, M_NOWAIT, TRUE, 256 ufi->orig_rvaddr, ufi->orig_rvaddr + 1); 257 258 /* 259 * didn't work? must be out of RAM. unlock and sleep. 260 */ 261 262 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) { 263 uvmfault_unlockmaps(ufi, TRUE); 264 uvm_wait("fltamapcopy"); 265 continue; 266 } 267 268 /* 269 * got it! unlock and return. 270 */ 271 272 uvmfault_unlockmaps(ufi, TRUE); 273 return; 274 } 275 /*NOTREACHED*/ 276 } 277 278 /* 279 * uvmfault_anonget: get data in an anon into a non-busy, non-released 280 * page in that anon. 281 * 282 * => maps, amap, and anon locked by caller. 283 * => if we fail (result != VM_PAGER_OK) we unlock everything except anon. 284 * => if we are successful, we return with everything still locked. 285 * => we don't move the page on the queues [gets moved later] 286 * => if we allocate a new page [we_own], it gets put on the queues. 287 * either way, the result is that the page is on the queues at return time 288 * => for pages which are on loan from a uvm_object (and thus are not 289 * owned by the anon): if successful, we return with the owning object 290 * locked. the caller must unlock this object when it unlocks everything 291 * else. 292 */ 293 294 int 295 uvmfault_anonget(ufi, amap, anon) 296 struct uvm_faultinfo *ufi; 297 struct vm_amap *amap; 298 struct vm_anon *anon; 299 { 300 boolean_t we_own; /* we own anon's page? */ 301 boolean_t locked; /* did we relock? */ 302 struct vm_page *pg; 303 int result; 304 UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist); 305 306 result = 0; /* XXX shut up gcc */ 307 uvmexp.fltanget++; 308 /* bump rusage counters */ 309 if (anon->u.an_page) 310 curproc->p_addr->u_stats.p_ru.ru_minflt++; 311 else 312 curproc->p_addr->u_stats.p_ru.ru_majflt++; 313 314 /* 315 * loop until we get it, or fail. 316 */ 317 318 while (1) { 319 320 we_own = FALSE; /* TRUE if we set PG_BUSY on a page */ 321 pg = anon->u.an_page; 322 323 /* 324 * if there is a resident page and it is loaned, then anon 325 * may not own it. call out to uvm_anon_lockpage() to ensure 326 * the real owner of the page has been identified and locked. 327 */ 328 329 if (pg && pg->loan_count) 330 pg = uvm_anon_lockloanpg(anon); 331 332 /* 333 * page there? make sure it is not busy/released. 334 */ 335 336 if (pg) { 337 338 /* 339 * at this point, if the page has a uobject [meaning 340 * we have it on loan], then that uobject is locked 341 * by us! if the page is busy, we drop all the 342 * locks (including uobject) and try again. 343 */ 344 345 if ((pg->flags & (PG_BUSY|PG_RELEASED)) == 0) { 346 UVMHIST_LOG(maphist, "<- OK",0,0,0,0); 347 return (VM_PAGER_OK); 348 } 349 pg->flags |= PG_WANTED; 350 uvmexp.fltpgwait++; 351 352 /* 353 * the last unlock must be an atomic unlock+wait on 354 * the owner of page 355 */ 356 if (pg->uobject) { /* owner is uobject ? */ 357 uvmfault_unlockall(ufi, amap, NULL, anon); 358 UVMHIST_LOG(maphist, " unlock+wait on uobj",0, 359 0,0,0); 360 UVM_UNLOCK_AND_WAIT(pg, 361 &pg->uobject->vmobjlock, 362 FALSE, "anonget1",0); 363 } else { 364 /* anon owns page */ 365 uvmfault_unlockall(ufi, amap, NULL, NULL); 366 UVMHIST_LOG(maphist, " unlock+wait on anon",0, 367 0,0,0); 368 UVM_UNLOCK_AND_WAIT(pg,&anon->an_lock,0, 369 "anonget2",0); 370 } 371 /* ready to relock and try again */ 372 373 } else { 374 375 /* 376 * no page, we must try and bring it in. 377 */ 378 pg = uvm_pagealloc(NULL, 0, anon, 0); 379 380 if (pg == NULL) { /* out of RAM. */ 381 382 uvmfault_unlockall(ufi, amap, NULL, anon); 383 uvmexp.fltnoram++; 384 UVMHIST_LOG(maphist, " noram -- UVM_WAIT",0, 385 0,0,0); 386 uvm_wait("flt_noram1"); 387 /* ready to relock and try again */ 388 389 } else { 390 391 /* we set the PG_BUSY bit */ 392 we_own = TRUE; 393 uvmfault_unlockall(ufi, amap, NULL, anon); 394 395 /* 396 * we are passing a PG_BUSY+PG_FAKE+PG_CLEAN 397 * page into the uvm_swap_get function with 398 * all data structures unlocked. note that 399 * it is ok to read an_swslot here because 400 * we hold PG_BUSY on the page. 401 */ 402 uvmexp.pageins++; 403 result = uvm_swap_get(pg, anon->an_swslot, 404 PGO_SYNCIO); 405 406 /* 407 * we clean up after the i/o below in the 408 * "we_own" case 409 */ 410 /* ready to relock and try again */ 411 } 412 } 413 414 /* 415 * now relock and try again 416 */ 417 418 locked = uvmfault_relock(ufi); 419 if (locked && amap != NULL) { 420 amap_lock(amap); 421 } 422 if (locked || we_own) 423 simple_lock(&anon->an_lock); 424 425 /* 426 * if we own the page (i.e. we set PG_BUSY), then we need 427 * to clean up after the I/O. there are three cases to 428 * consider: 429 * [1] page released during I/O: free anon and ReFault. 430 * [2] I/O not OK. free the page and cause the fault 431 * to fail. 432 * [3] I/O OK! activate the page and sync with the 433 * non-we_own case (i.e. drop anon lock if not locked). 434 */ 435 436 if (we_own) { 437 438 if (pg->flags & PG_WANTED) { 439 /* still holding object lock */ 440 wakeup(pg); 441 } 442 /* un-busy! */ 443 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE); 444 UVM_PAGE_OWN(pg, NULL); 445 446 /* 447 * if we were RELEASED during I/O, then our anon is 448 * no longer part of an amap. we need to free the 449 * anon and try again. 450 */ 451 if (pg->flags & PG_RELEASED) { 452 pmap_page_protect(pg, VM_PROT_NONE); 453 simple_unlock(&anon->an_lock); 454 uvm_anfree(anon); /* frees page for us */ 455 if (locked) 456 uvmfault_unlockall(ufi, amap, NULL, 457 NULL); 458 uvmexp.fltpgrele++; 459 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0); 460 return (VM_PAGER_REFAULT); /* refault! */ 461 } 462 463 if (result != VM_PAGER_OK) { 464 #ifdef DIAGNOSTIC 465 if (result == VM_PAGER_PEND) { 466 panic("uvmfault_anonget: " 467 "got PENDING for non-async I/O"); 468 } 469 #endif 470 /* remove page from anon */ 471 anon->u.an_page = NULL; 472 473 /* 474 * remove the swap slot from the anon 475 * and mark the anon as having no real slot. 476 * don't free the swap slot, thus preventing 477 * it from being used again. 478 */ 479 uvm_swap_markbad(anon->an_swslot, 1); 480 anon->an_swslot = SWSLOT_BAD; 481 482 /* 483 * note: page was never !PG_BUSY, so it 484 * can't be mapped and thus no need to 485 * pmap_page_protect it... 486 */ 487 uvm_lock_pageq(); 488 uvm_pagefree(pg); 489 uvm_unlock_pageq(); 490 491 if (locked) 492 uvmfault_unlockall(ufi, amap, NULL, 493 anon); 494 else 495 simple_unlock(&anon->an_lock); 496 UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0); 497 return (VM_PAGER_ERROR); 498 } 499 500 /* 501 * must be OK, clear modify (already PG_CLEAN) 502 * and activate 503 */ 504 pmap_clear_modify(pg); 505 uvm_lock_pageq(); 506 uvm_pageactivate(pg); 507 uvm_unlock_pageq(); 508 if (!locked) 509 simple_unlock(&anon->an_lock); 510 } 511 512 /* 513 * we were not able to relock. restart fault. 514 */ 515 516 if (!locked) { 517 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0); 518 return (VM_PAGER_REFAULT); 519 } 520 521 /* 522 * verify no one has touched the amap and moved the anon on us. 523 */ 524 525 if (ufi != NULL && 526 amap_lookup(&ufi->entry->aref, 527 ufi->orig_rvaddr - ufi->entry->start) != anon) { 528 529 uvmfault_unlockall(ufi, amap, NULL, anon); 530 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0); 531 return (VM_PAGER_REFAULT); 532 } 533 534 /* 535 * try it again! 536 */ 537 538 uvmexp.fltanretry++; 539 continue; 540 541 } /* while (1) */ 542 543 /*NOTREACHED*/ 544 } 545 546 /* 547 * F A U L T - m a i n e n t r y p o i n t 548 */ 549 550 /* 551 * uvm_fault: page fault handler 552 * 553 * => called from MD code to resolve a page fault 554 * => VM data structures usually should be unlocked. however, it is 555 * possible to call here with the main map locked if the caller 556 * gets a write lock, sets it recusive, and then calls us (c.f. 557 * uvm_map_pageable). this should be avoided because it keeps 558 * the map locked off during I/O. 559 */ 560 561 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \ 562 ~VM_PROT_WRITE : VM_PROT_ALL) 563 564 int 565 uvm_fault(orig_map, vaddr, fault_type, access_type) 566 vm_map_t orig_map; 567 vaddr_t vaddr; 568 vm_fault_t fault_type; 569 vm_prot_t access_type; 570 { 571 struct uvm_faultinfo ufi; 572 vm_prot_t enter_prot; 573 boolean_t wired, narrow, promote, locked, shadowed; 574 int npages, nback, nforw, centeridx, result, lcv, gotpages; 575 vaddr_t startva, objaddr, currva, offset; 576 paddr_t pa; 577 struct vm_amap *amap; 578 struct uvm_object *uobj; 579 struct vm_anon *anons_store[UVM_MAXRANGE], **anons, *anon, *oanon; 580 struct vm_page *pages[UVM_MAXRANGE], *pg, *uobjpage; 581 UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist); 582 583 UVMHIST_LOG(maphist, "(map=0x%x, vaddr=0x%x, ft=%d, at=%d)", 584 orig_map, vaddr, fault_type, access_type); 585 586 anon = NULL; /* XXX: shut up gcc */ 587 588 uvmexp.faults++; /* XXX: locking? */ 589 590 /* 591 * init the IN parameters in the ufi 592 */ 593 594 ufi.orig_map = orig_map; 595 ufi.orig_rvaddr = trunc_page(vaddr); 596 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */ 597 if (fault_type == VM_FAULT_WIRE) 598 narrow = TRUE; /* don't look for neighborhood 599 * pages on wire */ 600 else 601 narrow = FALSE; /* normal fault */ 602 603 /* 604 * before we do anything else, if this is a fault on a kernel 605 * address, check to see if the address is managed by an 606 * interrupt-safe map. If it is, we fail immediately. Intrsafe 607 * maps are never pageable, and this approach avoids an evil 608 * locking mess. 609 */ 610 if (orig_map == kernel_map && uvmfault_check_intrsafe(&ufi)) { 611 UVMHIST_LOG(maphist, "<- VA 0x%lx in intrsafe map %p", 612 ufi.orig_rvaddr, ufi.map, 0, 0); 613 return (KERN_FAILURE); 614 } 615 616 /* 617 * "goto ReFault" means restart the page fault from ground zero. 618 */ 619 ReFault: 620 621 /* 622 * lookup and lock the maps 623 */ 624 625 if (uvmfault_lookup(&ufi, FALSE) == FALSE) { 626 UVMHIST_LOG(maphist, "<- no mapping @ 0x%x", vaddr, 0,0,0); 627 return (KERN_INVALID_ADDRESS); 628 } 629 /* locked: maps(read) */ 630 631 /* 632 * check protection 633 */ 634 635 if ((ufi.entry->protection & access_type) != access_type) { 636 UVMHIST_LOG(maphist, 637 "<- protection failure (prot=0x%x, access=0x%x)", 638 ufi.entry->protection, access_type, 0, 0); 639 uvmfault_unlockmaps(&ufi, FALSE); 640 return (KERN_PROTECTION_FAILURE); 641 } 642 643 /* 644 * if the map is not a pageable map, a page fault always fails. 645 */ 646 647 if ((ufi.map->flags & VM_MAP_PAGEABLE) == 0) { 648 UVMHIST_LOG(maphist, 649 "<- map %p not pageable", ufi.map, 0, 0, 0); 650 uvmfault_unlockmaps(&ufi, FALSE); 651 return (KERN_FAILURE); 652 } 653 654 /* 655 * "enter_prot" is the protection we want to enter the page in at. 656 * for certain pages (e.g. copy-on-write pages) this protection can 657 * be more strict than ufi.entry->protection. "wired" means either 658 * the entry is wired or we are fault-wiring the pg. 659 */ 660 661 enter_prot = ufi.entry->protection; 662 wired = VM_MAPENT_ISWIRED(ufi.entry) || (fault_type == VM_FAULT_WIRE); 663 if (wired) 664 access_type = enter_prot; /* full access for wired */ 665 666 /* 667 * handle "needs_copy" case. if we need to copy the amap we will 668 * have to drop our readlock and relock it with a write lock. (we 669 * need a write lock to change anything in a map entry [e.g. 670 * needs_copy]). 671 */ 672 673 if (UVM_ET_ISNEEDSCOPY(ufi.entry)) { 674 if ((access_type & VM_PROT_WRITE) || 675 (ufi.entry->object.uvm_obj == NULL)) { 676 /* need to clear */ 677 UVMHIST_LOG(maphist, 678 " need to clear needs_copy and refault",0,0,0,0); 679 uvmfault_unlockmaps(&ufi, FALSE); 680 uvmfault_amapcopy(&ufi); 681 uvmexp.fltamcopy++; 682 goto ReFault; 683 684 } else { 685 686 /* 687 * ensure that we pmap_enter page R/O since 688 * needs_copy is still true 689 */ 690 enter_prot &= ~VM_PROT_WRITE; 691 692 } 693 } 694 695 /* 696 * identify the players 697 */ 698 699 amap = ufi.entry->aref.ar_amap; /* top layer */ 700 uobj = ufi.entry->object.uvm_obj; /* bottom layer */ 701 702 /* 703 * check for a case 0 fault. if nothing backing the entry then 704 * error now. 705 */ 706 707 if (amap == NULL && uobj == NULL) { 708 uvmfault_unlockmaps(&ufi, FALSE); 709 UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0); 710 return (KERN_INVALID_ADDRESS); 711 } 712 713 /* 714 * establish range of interest based on advice from mapper 715 * and then clip to fit map entry. note that we only want 716 * to do this the first time through the fault. if we 717 * ReFault we will disable this by setting "narrow" to true. 718 */ 719 720 if (narrow == FALSE) { 721 722 /* wide fault (!narrow) */ 723 #ifdef DIAGNOSTIC 724 if (uvmadvice[ufi.entry->advice].advice != ufi.entry->advice) 725 panic("fault: advice mismatch!"); 726 #endif 727 nback = min(uvmadvice[ufi.entry->advice].nback, 728 (ufi.orig_rvaddr - ufi.entry->start) >> PAGE_SHIFT); 729 startva = ufi.orig_rvaddr - (nback << PAGE_SHIFT); 730 nforw = min(uvmadvice[ufi.entry->advice].nforw, 731 ((ufi.entry->end - ufi.orig_rvaddr) >> 732 PAGE_SHIFT) - 1); 733 /* 734 * note: "-1" because we don't want to count the 735 * faulting page as forw 736 */ 737 npages = nback + nforw + 1; 738 centeridx = nback; 739 740 narrow = TRUE; /* ensure only once per-fault */ 741 742 } else { 743 744 /* narrow fault! */ 745 nback = nforw = 0; 746 startva = ufi.orig_rvaddr; 747 npages = 1; 748 centeridx = 0; 749 750 } 751 752 /* locked: maps(read) */ 753 UVMHIST_LOG(maphist, " narrow=%d, back=%d, forw=%d, startva=0x%x", 754 narrow, nback, nforw, startva); 755 UVMHIST_LOG(maphist, " entry=0x%x, amap=0x%x, obj=0x%x", ufi.entry, 756 amap, uobj, 0); 757 758 /* 759 * if we've got an amap, lock it and extract current anons. 760 */ 761 762 if (amap) { 763 amap_lock(amap); 764 anons = anons_store; 765 amap_lookups(&ufi.entry->aref, startva - ufi.entry->start, 766 anons, npages); 767 } else { 768 anons = NULL; /* to be safe */ 769 } 770 771 /* locked: maps(read), amap(if there) */ 772 773 /* 774 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages 775 * now and then forget about them (for the rest of the fault). 776 */ 777 778 if (ufi.entry->advice == MADV_SEQUENTIAL) { 779 780 UVMHIST_LOG(maphist, " MADV_SEQUENTIAL: flushing backpages", 781 0,0,0,0); 782 /* flush back-page anons? */ 783 if (amap) 784 uvmfault_anonflush(anons, nback); 785 786 /* flush object? */ 787 if (uobj) { 788 objaddr = 789 (startva - ufi.entry->start) + ufi.entry->offset; 790 simple_lock(&uobj->vmobjlock); 791 (void) uobj->pgops->pgo_flush(uobj, objaddr, objaddr + 792 (nback << PAGE_SHIFT), PGO_DEACTIVATE); 793 simple_unlock(&uobj->vmobjlock); 794 } 795 796 /* now forget about the backpages */ 797 if (amap) 798 anons += nback; 799 startva = startva + (nback << PAGE_SHIFT); 800 npages -= nback; 801 nback = centeridx = 0; 802 } 803 804 /* locked: maps(read), amap(if there) */ 805 806 /* 807 * map in the backpages and frontpages we found in the amap in hopes 808 * of preventing future faults. we also init the pages[] array as 809 * we go. 810 */ 811 812 currva = startva; 813 shadowed = FALSE; 814 for (lcv = 0 ; lcv < npages ; lcv++, currva += PAGE_SIZE) { 815 816 /* 817 * dont play with VAs that are already mapped 818 * except for center) 819 */ 820 if (lcv != centeridx) { 821 if (pmap_extract(ufi.orig_map->pmap, currva, &pa) == 822 TRUE) { 823 pages[lcv] = PGO_DONTCARE; 824 continue; 825 } 826 } 827 828 /* 829 * unmapped or center page. check if any anon at this level. 830 */ 831 if (amap == NULL || anons[lcv] == NULL) { 832 pages[lcv] = NULL; 833 continue; 834 } 835 836 /* 837 * check for present page and map if possible. re-activate it. 838 */ 839 840 pages[lcv] = PGO_DONTCARE; 841 if (lcv == centeridx) { /* save center for later! */ 842 shadowed = TRUE; 843 continue; 844 } 845 anon = anons[lcv]; 846 simple_lock(&anon->an_lock); 847 /* ignore loaned pages */ 848 if (anon->u.an_page && anon->u.an_page->loan_count == 0 && 849 (anon->u.an_page->flags & (PG_RELEASED|PG_BUSY)) == 0) { 850 uvm_lock_pageq(); 851 uvm_pageactivate(anon->u.an_page); /* reactivate */ 852 uvm_unlock_pageq(); 853 UVMHIST_LOG(maphist, 854 " MAPPING: n anon: pm=0x%x, va=0x%x, pg=0x%x", 855 ufi.orig_map->pmap, currva, anon->u.an_page, 0); 856 uvmexp.fltnamap++; 857 /* 858 * Since this isn't the page that's actually faulting, 859 * ignore pmap_enter() failures; it's not critical 860 * that we enter these right now. 861 */ 862 (void) pmap_enter(ufi.orig_map->pmap, currva, 863 VM_PAGE_TO_PHYS(anon->u.an_page), 864 (anon->an_ref > 1) ? (enter_prot & ~VM_PROT_WRITE) : 865 enter_prot, 866 PMAP_CANFAIL | 867 (VM_MAPENT_ISWIRED(ufi.entry) ? PMAP_WIRED : 0)); 868 } 869 simple_unlock(&anon->an_lock); 870 } 871 872 /* locked: maps(read), amap(if there) */ 873 /* (shadowed == TRUE) if there is an anon at the faulting address */ 874 UVMHIST_LOG(maphist, " shadowed=%d, will_get=%d", shadowed, 875 (uobj && shadowed == FALSE),0,0); 876 877 /* 878 * note that if we are really short of RAM we could sleep in the above 879 * call to pmap_enter with everything locked. bad? 880 * 881 * XXX Actually, that is bad; pmap_enter() should just fail in that 882 * XXX case. --thorpej 883 */ 884 885 /* 886 * if the desired page is not shadowed by the amap and we have a 887 * backing object, then we check to see if the backing object would 888 * prefer to handle the fault itself (rather than letting us do it 889 * with the usual pgo_get hook). the backing object signals this by 890 * providing a pgo_fault routine. 891 */ 892 893 if (uobj && shadowed == FALSE && uobj->pgops->pgo_fault != NULL) { 894 895 simple_lock(&uobj->vmobjlock); 896 897 /* locked: maps(read), amap (if there), uobj */ 898 result = uobj->pgops->pgo_fault(&ufi, startva, pages, npages, 899 centeridx, fault_type, access_type, 900 PGO_LOCKED); 901 /* locked: nothing, pgo_fault has unlocked everything */ 902 903 if (result == VM_PAGER_OK) 904 return (KERN_SUCCESS); /* pgo_fault did pmap enter */ 905 else if (result == VM_PAGER_REFAULT) 906 goto ReFault; /* try again! */ 907 else 908 return (KERN_PROTECTION_FAILURE); 909 } 910 911 /* 912 * now, if the desired page is not shadowed by the amap and we have 913 * a backing object that does not have a special fault routine, then 914 * we ask (with pgo_get) the object for resident pages that we care 915 * about and attempt to map them in. we do not let pgo_get block 916 * (PGO_LOCKED). 917 * 918 * ("get" has the option of doing a pmap_enter for us) 919 */ 920 921 if (uobj && shadowed == FALSE) { 922 simple_lock(&uobj->vmobjlock); 923 924 /* locked (!shadowed): maps(read), amap (if there), uobj */ 925 /* 926 * the following call to pgo_get does _not_ change locking state 927 */ 928 929 uvmexp.fltlget++; 930 gotpages = npages; 931 result = uobj->pgops->pgo_get(uobj, ufi.entry->offset + 932 (startva - ufi.entry->start), 933 pages, &gotpages, centeridx, 934 access_type & MASK(ufi.entry), 935 ufi.entry->advice, PGO_LOCKED); 936 937 /* 938 * check for pages to map, if we got any 939 */ 940 941 uobjpage = NULL; 942 943 if (gotpages) { 944 currva = startva; 945 for (lcv = 0 ; lcv < npages ; 946 lcv++, currva += PAGE_SIZE) { 947 948 if (pages[lcv] == NULL || 949 pages[lcv] == PGO_DONTCARE) 950 continue; 951 952 #ifdef DIAGNOSTIC 953 /* 954 * pager sanity check: pgo_get with 955 * PGO_LOCKED should never return a 956 * released page to us. 957 */ 958 if (pages[lcv]->flags & PG_RELEASED) 959 panic("uvm_fault: pgo_get PGO_LOCKED gave us a RELEASED page"); 960 #endif 961 962 /* 963 * if center page is resident and not 964 * PG_BUSY|PG_RELEASED then pgo_get 965 * made it PG_BUSY for us and gave 966 * us a handle to it. remember this 967 * page as "uobjpage." (for later use). 968 */ 969 970 if (lcv == centeridx) { 971 uobjpage = pages[lcv]; 972 UVMHIST_LOG(maphist, " got uobjpage (0x%x) with locked get", 973 uobjpage, 0,0,0); 974 continue; 975 } 976 977 /* 978 * note: calling pgo_get with locked data 979 * structures returns us pages which are 980 * neither busy nor released, so we don't 981 * need to check for this. we can just 982 * directly enter the page (after moving it 983 * to the head of the active queue [useful?]). 984 */ 985 986 uvm_lock_pageq(); 987 uvm_pageactivate(pages[lcv]); /* reactivate */ 988 uvm_unlock_pageq(); 989 UVMHIST_LOG(maphist, 990 " MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x", 991 ufi.orig_map->pmap, currva, pages[lcv], 0); 992 uvmexp.fltnomap++; 993 /* 994 * Since this page isn't the page that's 995 * actually fauling, ignore pmap_enter() 996 * failures; it's not critical that we 997 * enter these right now. 998 */ 999 (void) pmap_enter(ufi.orig_map->pmap, currva, 1000 VM_PAGE_TO_PHYS(pages[lcv]), 1001 enter_prot & MASK(ufi.entry), 1002 PMAP_CANFAIL | 1003 (wired ? PMAP_WIRED : 0)); 1004 1005 /* 1006 * NOTE: page can't be PG_WANTED or PG_RELEASED 1007 * because we've held the lock the whole time 1008 * we've had the handle. 1009 */ 1010 pages[lcv]->flags &= ~(PG_BUSY); /* un-busy! */ 1011 UVM_PAGE_OWN(pages[lcv], NULL); 1012 1013 /* done! */ 1014 } /* for "lcv" loop */ 1015 } /* "gotpages" != 0 */ 1016 1017 /* note: object still _locked_ */ 1018 } else { 1019 1020 uobjpage = NULL; 1021 1022 } 1023 1024 /* locked (shadowed): maps(read), amap */ 1025 /* locked (!shadowed): maps(read), amap(if there), 1026 uobj(if !null), uobjpage(if !null) */ 1027 1028 /* 1029 * note that at this point we are done with any front or back pages. 1030 * we are now going to focus on the center page (i.e. the one we've 1031 * faulted on). if we have faulted on the top (anon) layer 1032 * [i.e. case 1], then the anon we want is anons[centeridx] (we have 1033 * not touched it yet). if we have faulted on the bottom (uobj) 1034 * layer [i.e. case 2] and the page was both present and available, 1035 * then we've got a pointer to it as "uobjpage" and we've already 1036 * made it BUSY. 1037 */ 1038 1039 /* 1040 * there are four possible cases we must address: 1A, 1B, 2A, and 2B 1041 */ 1042 1043 /* 1044 * redirect case 2: if we are not shadowed, go to case 2. 1045 */ 1046 1047 if (shadowed == FALSE) 1048 goto Case2; 1049 1050 /* locked: maps(read), amap */ 1051 1052 /* 1053 * handle case 1: fault on an anon in our amap 1054 */ 1055 1056 anon = anons[centeridx]; 1057 UVMHIST_LOG(maphist, " case 1 fault: anon=0x%x", anon, 0,0,0); 1058 simple_lock(&anon->an_lock); 1059 1060 /* locked: maps(read), amap, anon */ 1061 1062 /* 1063 * no matter if we have case 1A or case 1B we are going to need to 1064 * have the anon's memory resident. ensure that now. 1065 */ 1066 1067 /* 1068 * let uvmfault_anonget do the dirty work. 1069 * if it fails (!OK) it will unlock all but the anon for us. 1070 * if it succeeds, locks are still valid and locked. 1071 * also, if it is OK, then the anon's page is on the queues. 1072 * if the page is on loan from a uvm_object, then anonget will 1073 * lock that object for us if it does not fail. 1074 */ 1075 1076 result = uvmfault_anonget(&ufi, amap, anon); 1077 if (result != VM_PAGER_OK) { 1078 simple_unlock(&anon->an_lock); 1079 } 1080 1081 if (result == VM_PAGER_REFAULT) 1082 goto ReFault; 1083 1084 if (result == VM_PAGER_AGAIN) { 1085 tsleep((caddr_t)&lbolt, PVM, "fltagain1", 0); 1086 goto ReFault; 1087 } 1088 1089 if (result != VM_PAGER_OK) 1090 return (KERN_PROTECTION_FAILURE); /* XXX??? */ 1091 1092 /* 1093 * uobj is non null if the page is on loan from an object (i.e. uobj) 1094 */ 1095 1096 uobj = anon->u.an_page->uobject; /* locked by anonget if !NULL */ 1097 1098 /* locked: maps(read), amap, anon, uobj(if one) */ 1099 1100 /* 1101 * special handling for loaned pages 1102 */ 1103 if (anon->u.an_page->loan_count) { 1104 1105 if ((access_type & VM_PROT_WRITE) == 0) { 1106 1107 /* 1108 * for read faults on loaned pages we just cap the 1109 * protection at read-only. 1110 */ 1111 1112 enter_prot = enter_prot & ~VM_PROT_WRITE; 1113 1114 } else { 1115 /* 1116 * note that we can't allow writes into a loaned page! 1117 * 1118 * if we have a write fault on a loaned page in an 1119 * anon then we need to look at the anon's ref count. 1120 * if it is greater than one then we are going to do 1121 * a normal copy-on-write fault into a new anon (this 1122 * is not a problem). however, if the reference count 1123 * is one (a case where we would normally allow a 1124 * write directly to the page) then we need to kill 1125 * the loan before we continue. 1126 */ 1127 1128 /* >1 case is already ok */ 1129 if (anon->an_ref == 1) { 1130 1131 /* get new un-owned replacement page */ 1132 pg = uvm_pagealloc(NULL, 0, NULL, 0); 1133 if (pg == NULL) { 1134 uvmfault_unlockall(&ufi, amap, uobj, 1135 anon); 1136 uvm_wait("flt_noram2"); 1137 goto ReFault; 1138 } 1139 1140 /* 1141 * copy data, kill loan, and drop uobj lock 1142 * (if any) 1143 */ 1144 /* copy old -> new */ 1145 uvm_pagecopy(anon->u.an_page, pg); 1146 1147 /* force reload */ 1148 pmap_page_protect(anon->u.an_page, 1149 VM_PROT_NONE); 1150 uvm_lock_pageq(); /* KILL loan */ 1151 if (uobj) 1152 /* if we were loaning */ 1153 anon->u.an_page->loan_count--; 1154 anon->u.an_page->uanon = NULL; 1155 /* in case we owned */ 1156 anon->u.an_page->pqflags &= ~PQ_ANON; 1157 uvm_unlock_pageq(); 1158 if (uobj) { 1159 simple_unlock(&uobj->vmobjlock); 1160 uobj = NULL; 1161 } 1162 1163 /* install new page in anon */ 1164 anon->u.an_page = pg; 1165 pg->uanon = anon; 1166 pg->pqflags |= PQ_ANON; 1167 pg->flags &= ~(PG_BUSY|PG_FAKE); 1168 UVM_PAGE_OWN(pg, NULL); 1169 1170 /* done! */ 1171 } /* ref == 1 */ 1172 } /* write fault */ 1173 } /* loan count */ 1174 1175 /* 1176 * if we are case 1B then we will need to allocate a new blank 1177 * anon to transfer the data into. note that we have a lock 1178 * on anon, so no one can busy or release the page until we are done. 1179 * also note that the ref count can't drop to zero here because 1180 * it is > 1 and we are only dropping one ref. 1181 * 1182 * in the (hopefully very rare) case that we are out of RAM we 1183 * will unlock, wait for more RAM, and refault. 1184 * 1185 * if we are out of anon VM we kill the process (XXX: could wait?). 1186 */ 1187 1188 if ((access_type & VM_PROT_WRITE) != 0 && anon->an_ref > 1) { 1189 1190 UVMHIST_LOG(maphist, " case 1B: COW fault",0,0,0,0); 1191 uvmexp.flt_acow++; 1192 oanon = anon; /* oanon = old, locked anon */ 1193 anon = uvm_analloc(); 1194 if (anon) 1195 pg = uvm_pagealloc(NULL, 0, anon, 0); 1196 #ifdef __GNUC__ 1197 else 1198 pg = NULL; /* XXX: gcc */ 1199 #endif 1200 1201 /* check for out of RAM */ 1202 if (anon == NULL || pg == NULL) { 1203 if (anon) 1204 uvm_anfree(anon); 1205 uvmfault_unlockall(&ufi, amap, uobj, oanon); 1206 #ifdef DIAGNOSTIC 1207 if (uvmexp.swpgonly > uvmexp.swpages) { 1208 panic("uvmexp.swpgonly botch"); 1209 } 1210 #endif 1211 if (anon == NULL || uvmexp.swpgonly == uvmexp.swpages) { 1212 UVMHIST_LOG(maphist, 1213 "<- failed. out of VM",0,0,0,0); 1214 uvmexp.fltnoanon++; 1215 return (KERN_RESOURCE_SHORTAGE); 1216 } 1217 1218 uvmexp.fltnoram++; 1219 uvm_wait("flt_noram3"); /* out of RAM, wait for more */ 1220 goto ReFault; 1221 } 1222 1223 /* got all resources, replace anon with nanon */ 1224 1225 uvm_pagecopy(oanon->u.an_page, pg); /* pg now !PG_CLEAN */ 1226 pg->flags &= ~(PG_BUSY|PG_FAKE); /* un-busy! new page */ 1227 UVM_PAGE_OWN(pg, NULL); 1228 amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start, 1229 anon, 1); 1230 1231 /* deref: can not drop to zero here by defn! */ 1232 oanon->an_ref--; 1233 1234 /* 1235 * note: oanon still locked. anon is _not_ locked, but we 1236 * have the sole references to in from amap which _is_ locked. 1237 * thus, no one can get at it until we are done with it. 1238 */ 1239 1240 } else { 1241 1242 uvmexp.flt_anon++; 1243 oanon = anon; /* old, locked anon is same as anon */ 1244 pg = anon->u.an_page; 1245 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */ 1246 enter_prot = enter_prot & ~VM_PROT_WRITE; 1247 1248 } 1249 1250 /* locked: maps(read), amap, anon */ 1251 1252 /* 1253 * now map the page in ... 1254 * XXX: old fault unlocks object before pmap_enter. this seems 1255 * suspect since some other thread could blast the page out from 1256 * under us between the unlock and the pmap_enter. 1257 */ 1258 1259 UVMHIST_LOG(maphist, " MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x", 1260 ufi.orig_map->pmap, ufi.orig_rvaddr, pg, 0); 1261 if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg), 1262 enter_prot, access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0)) 1263 != KERN_SUCCESS) { 1264 /* 1265 * No need to undo what we did; we can simply think of 1266 * this as the pmap throwing away the mapping information. 1267 * 1268 * We do, however, have to go through the ReFault path, 1269 * as the map may change while we're asleep. 1270 */ 1271 uvmfault_unlockall(&ufi, amap, uobj, oanon); 1272 #ifdef DIAGNOSTIC 1273 if (uvmexp.swpgonly > uvmexp.swpages) 1274 panic("uvmexp.swpgonly botch"); 1275 #endif 1276 if (uvmexp.swpgonly == uvmexp.swpages) { 1277 UVMHIST_LOG(maphist, 1278 "<- failed. out of VM",0,0,0,0); 1279 /* XXX instrumentation */ 1280 return (KERN_RESOURCE_SHORTAGE); 1281 } 1282 /* XXX instrumentation */ 1283 uvm_wait("flt_pmfail1"); 1284 goto ReFault; 1285 } 1286 1287 /* 1288 * ... update the page queues. 1289 */ 1290 1291 uvm_lock_pageq(); 1292 1293 if (fault_type == VM_FAULT_WIRE) { 1294 uvm_pagewire(pg); 1295 1296 /* 1297 * since the now-wired page cannot be paged out, 1298 * release its swap resources for others to use. 1299 * since an anon with no swap cannot be PG_CLEAN, 1300 * clear its clean flag now. 1301 */ 1302 1303 pg->flags &= ~(PG_CLEAN); 1304 uvm_anon_dropswap(anon); 1305 } else { 1306 /* activate it */ 1307 uvm_pageactivate(pg); 1308 } 1309 1310 uvm_unlock_pageq(); 1311 1312 /* 1313 * done case 1! finish up by unlocking everything and returning success 1314 */ 1315 1316 uvmfault_unlockall(&ufi, amap, uobj, oanon); 1317 return (KERN_SUCCESS); 1318 1319 1320 Case2: 1321 /* 1322 * handle case 2: faulting on backing object or zero fill 1323 */ 1324 1325 /* 1326 * locked: 1327 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null) 1328 */ 1329 1330 /* 1331 * note that uobjpage can not be PGO_DONTCARE at this point. we now 1332 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we 1333 * have a backing object, check and see if we are going to promote 1334 * the data up to an anon during the fault. 1335 */ 1336 1337 if (uobj == NULL) { 1338 uobjpage = PGO_DONTCARE; 1339 promote = TRUE; /* always need anon here */ 1340 } else { 1341 /* assert(uobjpage != PGO_DONTCARE) */ 1342 promote = (access_type & VM_PROT_WRITE) && 1343 UVM_ET_ISCOPYONWRITE(ufi.entry); 1344 } 1345 UVMHIST_LOG(maphist, " case 2 fault: promote=%d, zfill=%d", 1346 promote, (uobj == NULL), 0,0); 1347 1348 /* 1349 * if uobjpage is not null then we do not need to do I/O to get the 1350 * uobjpage. 1351 * 1352 * if uobjpage is null, then we need to unlock and ask the pager to 1353 * get the data for us. once we have the data, we need to reverify 1354 * the state the world. we are currently not holding any resources. 1355 */ 1356 1357 if (uobjpage) { 1358 /* update rusage counters */ 1359 curproc->p_addr->u_stats.p_ru.ru_minflt++; 1360 } else { 1361 /* update rusage counters */ 1362 curproc->p_addr->u_stats.p_ru.ru_majflt++; 1363 1364 /* locked: maps(read), amap(if there), uobj */ 1365 uvmfault_unlockall(&ufi, amap, NULL, NULL); 1366 /* locked: uobj */ 1367 1368 uvmexp.fltget++; 1369 gotpages = 1; 1370 result = uobj->pgops->pgo_get(uobj, 1371 (ufi.orig_rvaddr - ufi.entry->start) + ufi.entry->offset, 1372 &uobjpage, &gotpages, 0, 1373 access_type & MASK(ufi.entry), 1374 ufi.entry->advice, 0); 1375 1376 /* locked: uobjpage(if result OK) */ 1377 1378 /* 1379 * recover from I/O 1380 */ 1381 1382 if (result != VM_PAGER_OK) { 1383 #ifdef DIAGNOSTIC 1384 if (result == VM_PAGER_PEND) 1385 panic("uvm_fault: pgo_get got PENDing " 1386 "on non-async I/O"); 1387 #endif 1388 1389 if (result == VM_PAGER_AGAIN) { 1390 UVMHIST_LOG(maphist, 1391 " pgo_get says TRY AGAIN!",0,0,0,0); 1392 tsleep((caddr_t)&lbolt, PVM, "fltagain2", 0); 1393 goto ReFault; 1394 } 1395 1396 UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)", 1397 result, 0,0,0); 1398 return (KERN_PROTECTION_FAILURE); /* XXX i/o error */ 1399 } 1400 1401 /* locked: uobjpage */ 1402 1403 /* 1404 * re-verify the state of the world by first trying to relock 1405 * the maps. always relock the object. 1406 */ 1407 1408 locked = uvmfault_relock(&ufi); 1409 if (locked && amap) 1410 amap_lock(amap); 1411 simple_lock(&uobj->vmobjlock); 1412 1413 /* locked(locked): maps(read), amap(if !null), uobj, uobjpage */ 1414 /* locked(!locked): uobj, uobjpage */ 1415 1416 /* 1417 * verify that the page has not be released and re-verify 1418 * that amap slot is still free. if there is a problem, 1419 * we unlock and clean up. 1420 */ 1421 1422 if ((uobjpage->flags & PG_RELEASED) != 0 || 1423 (locked && amap && 1424 amap_lookup(&ufi.entry->aref, 1425 ufi.orig_rvaddr - ufi.entry->start))) { 1426 if (locked) 1427 uvmfault_unlockall(&ufi, amap, NULL, NULL); 1428 locked = FALSE; 1429 } 1430 1431 /* 1432 * didn't get the lock? release the page and retry. 1433 */ 1434 1435 if (locked == FALSE) { 1436 1437 UVMHIST_LOG(maphist, 1438 " wasn't able to relock after fault: retry", 1439 0,0,0,0); 1440 if (uobjpage->flags & PG_WANTED) 1441 /* still holding object lock */ 1442 wakeup(uobjpage); 1443 1444 if (uobjpage->flags & PG_RELEASED) { 1445 uvmexp.fltpgrele++; 1446 #ifdef DIAGNOSTIC 1447 if (uobj->pgops->pgo_releasepg == NULL) 1448 panic("uvm_fault: object has no " 1449 "releasepg function"); 1450 #endif 1451 /* frees page */ 1452 if (uobj->pgops->pgo_releasepg(uobjpage,NULL)) 1453 /* unlock if still alive */ 1454 simple_unlock(&uobj->vmobjlock); 1455 goto ReFault; 1456 } 1457 1458 uvm_lock_pageq(); 1459 /* make sure it is in queues */ 1460 uvm_pageactivate(uobjpage); 1461 1462 uvm_unlock_pageq(); 1463 uobjpage->flags &= ~(PG_BUSY|PG_WANTED); 1464 UVM_PAGE_OWN(uobjpage, NULL); 1465 simple_unlock(&uobj->vmobjlock); 1466 goto ReFault; 1467 1468 } 1469 1470 /* 1471 * we have the data in uobjpage which is PG_BUSY and 1472 * !PG_RELEASED. we are holding object lock (so the page 1473 * can't be released on us). 1474 */ 1475 1476 /* locked: maps(read), amap(if !null), uobj, uobjpage */ 1477 1478 } 1479 1480 /* 1481 * locked: 1482 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj) 1483 */ 1484 1485 /* 1486 * notes: 1487 * - at this point uobjpage can not be NULL 1488 * - at this point uobjpage can not be PG_RELEASED (since we checked 1489 * for it above) 1490 * - at this point uobjpage could be PG_WANTED (handle later) 1491 */ 1492 1493 if (promote == FALSE) { 1494 1495 /* 1496 * we are not promoting. if the mapping is COW ensure that we 1497 * don't give more access than we should (e.g. when doing a read 1498 * fault on a COPYONWRITE mapping we want to map the COW page in 1499 * R/O even though the entry protection could be R/W). 1500 * 1501 * set "pg" to the page we want to map in (uobjpage, usually) 1502 */ 1503 1504 uvmexp.flt_obj++; 1505 if (UVM_ET_ISCOPYONWRITE(ufi.entry)) 1506 enter_prot &= ~VM_PROT_WRITE; 1507 pg = uobjpage; /* map in the actual object */ 1508 1509 /* assert(uobjpage != PGO_DONTCARE) */ 1510 1511 /* 1512 * we are faulting directly on the page. be careful 1513 * about writing to loaned pages... 1514 */ 1515 if (uobjpage->loan_count) { 1516 1517 if ((access_type & VM_PROT_WRITE) == 0) { 1518 /* read fault: cap the protection at readonly */ 1519 /* cap! */ 1520 enter_prot = enter_prot & ~VM_PROT_WRITE; 1521 } else { 1522 /* write fault: must break the loan here */ 1523 1524 /* alloc new un-owned page */ 1525 pg = uvm_pagealloc(NULL, 0, NULL, 0); 1526 1527 if (pg == NULL) { 1528 /* 1529 * drop ownership of page, it can't 1530 * be released 1531 */ 1532 if (uobjpage->flags & PG_WANTED) 1533 wakeup(uobjpage); 1534 uobjpage->flags &= ~(PG_BUSY|PG_WANTED); 1535 UVM_PAGE_OWN(uobjpage, NULL); 1536 1537 uvm_lock_pageq(); 1538 /* activate: we will need it later */ 1539 uvm_pageactivate(uobjpage); 1540 1541 uvm_unlock_pageq(); 1542 uvmfault_unlockall(&ufi, amap, uobj, 1543 NULL); 1544 UVMHIST_LOG(maphist, 1545 " out of RAM breaking loan, waiting", 1546 0,0,0,0); 1547 uvmexp.fltnoram++; 1548 uvm_wait("flt_noram4"); 1549 goto ReFault; 1550 } 1551 1552 /* 1553 * copy the data from the old page to the new 1554 * one and clear the fake/clean flags on the 1555 * new page (keep it busy). force a reload 1556 * of the old page by clearing it from all 1557 * pmaps. then lock the page queues to 1558 * rename the pages. 1559 */ 1560 uvm_pagecopy(uobjpage, pg); /* old -> new */ 1561 pg->flags &= ~(PG_FAKE|PG_CLEAN); 1562 pmap_page_protect(uobjpage, VM_PROT_NONE); 1563 if (uobjpage->flags & PG_WANTED) 1564 wakeup(uobjpage); 1565 /* uobj still locked */ 1566 uobjpage->flags &= ~(PG_WANTED|PG_BUSY); 1567 UVM_PAGE_OWN(uobjpage, NULL); 1568 1569 uvm_lock_pageq(); 1570 offset = uobjpage->offset; 1571 /* remove old page */ 1572 uvm_pagerealloc(uobjpage, NULL, 0); 1573 1574 /* 1575 * at this point we have absolutely no 1576 * control over uobjpage 1577 */ 1578 /* install new page */ 1579 uvm_pagerealloc(pg, uobj, offset); 1580 uvm_unlock_pageq(); 1581 1582 /* 1583 * done! loan is broken and "pg" is 1584 * PG_BUSY. it can now replace uobjpage. 1585 */ 1586 1587 uobjpage = pg; 1588 1589 } /* write fault case */ 1590 } /* if loan_count */ 1591 1592 } else { 1593 1594 /* 1595 * if we are going to promote the data to an anon we 1596 * allocate a blank anon here and plug it into our amap. 1597 */ 1598 #if DIAGNOSTIC 1599 if (amap == NULL) 1600 panic("uvm_fault: want to promote data, but no anon"); 1601 #endif 1602 1603 anon = uvm_analloc(); 1604 if (anon) { 1605 /* 1606 * In `Fill in data...' below, if 1607 * uobjpage == PGO_DONTCARE, we want 1608 * a zero'd, dirty page, so have 1609 * uvm_pagealloc() do that for us. 1610 */ 1611 pg = uvm_pagealloc(NULL, 0, anon, 1612 (uobjpage == PGO_DONTCARE) ? UVM_PGA_ZERO : 0); 1613 } 1614 #ifdef __GNUC__ 1615 else 1616 pg = NULL; /* XXX: gcc */ 1617 #endif 1618 1619 /* 1620 * out of memory resources? 1621 */ 1622 if (anon == NULL || pg == NULL) { 1623 1624 /* 1625 * arg! must unbusy our page and fail or sleep. 1626 */ 1627 if (uobjpage != PGO_DONTCARE) { 1628 if (uobjpage->flags & PG_WANTED) 1629 /* still holding object lock */ 1630 wakeup(uobjpage); 1631 1632 uvm_lock_pageq(); 1633 /* make sure it is in queues */ 1634 uvm_pageactivate(uobjpage); 1635 uvm_unlock_pageq(); 1636 /* un-busy! (still locked) */ 1637 uobjpage->flags &= ~(PG_BUSY|PG_WANTED); 1638 UVM_PAGE_OWN(uobjpage, NULL); 1639 } 1640 1641 /* unlock and fail ... */ 1642 uvmfault_unlockall(&ufi, amap, uobj, NULL); 1643 #ifdef DIAGNOSTIC 1644 if (uvmexp.swpgonly > uvmexp.swpages) { 1645 panic("uvmexp.swpgonly botch"); 1646 } 1647 #endif 1648 if (anon == NULL || uvmexp.swpgonly == uvmexp.swpages) { 1649 UVMHIST_LOG(maphist, " promote: out of VM", 1650 0,0,0,0); 1651 uvmexp.fltnoanon++; 1652 return (KERN_RESOURCE_SHORTAGE); 1653 } 1654 1655 UVMHIST_LOG(maphist, " out of RAM, waiting for more", 1656 0,0,0,0); 1657 uvm_anfree(anon); 1658 uvmexp.fltnoram++; 1659 uvm_wait("flt_noram5"); 1660 goto ReFault; 1661 } 1662 1663 /* 1664 * fill in the data 1665 */ 1666 1667 if (uobjpage != PGO_DONTCARE) { 1668 uvmexp.flt_prcopy++; 1669 /* copy page [pg now dirty] */ 1670 uvm_pagecopy(uobjpage, pg); 1671 1672 /* 1673 * promote to shared amap? make sure all sharing 1674 * procs see it 1675 */ 1676 if ((amap_flags(amap) & AMAP_SHARED) != 0) { 1677 pmap_page_protect(uobjpage, VM_PROT_NONE); 1678 } 1679 1680 /* 1681 * dispose of uobjpage. it can't be PG_RELEASED 1682 * since we still hold the object lock. drop 1683 * handle to uobj as well. 1684 */ 1685 1686 if (uobjpage->flags & PG_WANTED) 1687 /* still have the obj lock */ 1688 wakeup(uobjpage); 1689 uobjpage->flags &= ~(PG_BUSY|PG_WANTED); 1690 UVM_PAGE_OWN(uobjpage, NULL); 1691 uvm_lock_pageq(); 1692 uvm_pageactivate(uobjpage); /* put it back */ 1693 uvm_unlock_pageq(); 1694 simple_unlock(&uobj->vmobjlock); 1695 uobj = NULL; 1696 UVMHIST_LOG(maphist, 1697 " promote uobjpage 0x%x to anon/page 0x%x/0x%x", 1698 uobjpage, anon, pg, 0); 1699 1700 } else { 1701 uvmexp.flt_przero++; 1702 /* 1703 * Page is zero'd and marked dirty by uvm_pagealloc() 1704 * above. 1705 */ 1706 UVMHIST_LOG(maphist," zero fill anon/page 0x%x/0%x", 1707 anon, pg, 0, 0); 1708 } 1709 1710 amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start, 1711 anon, 0); 1712 1713 } 1714 1715 /* 1716 * locked: 1717 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj) 1718 * 1719 * note: pg is either the uobjpage or the new page in the new anon 1720 */ 1721 1722 /* 1723 * all resources are present. we can now map it in and free our 1724 * resources. 1725 */ 1726 1727 UVMHIST_LOG(maphist, 1728 " MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d", 1729 ufi.orig_map->pmap, ufi.orig_rvaddr, pg, promote); 1730 if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg), 1731 enter_prot, access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0)) 1732 != KERN_SUCCESS) { 1733 /* 1734 * No need to undo what we did; we can simply think of 1735 * this as the pmap throwing away the mapping information. 1736 * 1737 * We do, however, have to go through the ReFault path, 1738 * as the map may change while we're asleep. 1739 */ 1740 if (pg->flags & PG_WANTED) 1741 wakeup(pg); /* lock still held */ 1742 1743 /* 1744 * note that pg can't be PG_RELEASED since we did not drop 1745 * the object lock since the last time we checked. 1746 */ 1747 1748 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED); 1749 UVM_PAGE_OWN(pg, NULL); 1750 uvmfault_unlockall(&ufi, amap, uobj, NULL); 1751 #ifdef DIAGNOSTIC 1752 if (uvmexp.swpgonly > uvmexp.swpages) 1753 panic("uvmexp.swpgonly botch"); 1754 #endif 1755 if (uvmexp.swpgonly == uvmexp.swpages) { 1756 UVMHIST_LOG(maphist, 1757 "<- failed. out of VM",0,0,0,0); 1758 /* XXX instrumentation */ 1759 return (KERN_RESOURCE_SHORTAGE); 1760 } 1761 /* XXX instrumentation */ 1762 uvm_wait("flt_pmfail2"); 1763 goto ReFault; 1764 } 1765 1766 uvm_lock_pageq(); 1767 1768 if (fault_type == VM_FAULT_WIRE) { 1769 uvm_pagewire(pg); 1770 if (pg->pqflags & PQ_AOBJ) { 1771 1772 /* 1773 * since the now-wired page cannot be paged out, 1774 * release its swap resources for others to use. 1775 * since an aobj page with no swap cannot be PG_CLEAN, 1776 * clear its clean flag now. 1777 */ 1778 1779 pg->flags &= ~(PG_CLEAN); 1780 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT); 1781 } 1782 } else { 1783 /* activate it */ 1784 uvm_pageactivate(pg); 1785 } 1786 1787 uvm_unlock_pageq(); 1788 1789 if (pg->flags & PG_WANTED) 1790 wakeup(pg); /* lock still held */ 1791 1792 /* 1793 * note that pg can't be PG_RELEASED since we did not drop the object 1794 * lock since the last time we checked. 1795 */ 1796 1797 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED); 1798 UVM_PAGE_OWN(pg, NULL); 1799 uvmfault_unlockall(&ufi, amap, uobj, NULL); 1800 1801 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0); 1802 return (KERN_SUCCESS); 1803 } 1804 1805 1806 /* 1807 * uvm_fault_wire: wire down a range of virtual addresses in a map. 1808 * 1809 * => map may be read-locked by caller, but MUST NOT be write-locked. 1810 * => if map is read-locked, any operations which may cause map to 1811 * be write-locked in uvm_fault() must be taken care of by 1812 * the caller. See uvm_map_pageable(). 1813 */ 1814 1815 int 1816 uvm_fault_wire(map, start, end, access_type) 1817 vm_map_t map; 1818 vaddr_t start, end; 1819 vm_prot_t access_type; 1820 { 1821 vaddr_t va; 1822 pmap_t pmap; 1823 int rv; 1824 1825 pmap = vm_map_pmap(map); 1826 1827 /* 1828 * now fault it in a page at a time. if the fault fails then we have 1829 * to undo what we have done. note that in uvm_fault VM_PROT_NONE 1830 * is replaced with the max protection if fault_type is VM_FAULT_WIRE. 1831 */ 1832 1833 for (va = start ; va < end ; va += PAGE_SIZE) { 1834 rv = uvm_fault(map, va, VM_FAULT_WIRE, access_type); 1835 if (rv) { 1836 if (va != start) { 1837 uvm_fault_unwire(map, start, va); 1838 } 1839 return (rv); 1840 } 1841 } 1842 1843 return (KERN_SUCCESS); 1844 } 1845 1846 /* 1847 * uvm_fault_unwire(): unwire range of virtual space. 1848 */ 1849 1850 void 1851 uvm_fault_unwire(map, start, end) 1852 vm_map_t map; 1853 vaddr_t start, end; 1854 { 1855 1856 vm_map_lock_read(map); 1857 uvm_fault_unwire_locked(map, start, end); 1858 vm_map_unlock_read(map); 1859 } 1860 1861 /* 1862 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire(). 1863 * 1864 * => map must be at least read-locked. 1865 */ 1866 1867 void 1868 uvm_fault_unwire_locked(map, start, end) 1869 vm_map_t map; 1870 vaddr_t start, end; 1871 { 1872 vm_map_entry_t entry; 1873 pmap_t pmap = vm_map_pmap(map); 1874 vaddr_t va; 1875 paddr_t pa; 1876 struct vm_page *pg; 1877 1878 #ifdef DIAGNOSTIC 1879 if (map->flags & VM_MAP_INTRSAFE) 1880 panic("uvm_fault_unwire_locked: intrsafe map"); 1881 #endif 1882 1883 /* 1884 * we assume that the area we are unwiring has actually been wired 1885 * in the first place. this means that we should be able to extract 1886 * the PAs from the pmap. we also lock out the page daemon so that 1887 * we can call uvm_pageunwire. 1888 */ 1889 1890 uvm_lock_pageq(); 1891 1892 /* 1893 * find the beginning map entry for the region. 1894 */ 1895 #ifdef DIAGNOSTIC 1896 if (start < vm_map_min(map) || end > vm_map_max(map)) 1897 panic("uvm_fault_unwire_locked: address out of range"); 1898 #endif 1899 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) 1900 panic("uvm_fault_unwire_locked: address not in map"); 1901 1902 for (va = start; va < end ; va += PAGE_SIZE) { 1903 if (pmap_extract(pmap, va, &pa) == FALSE) 1904 panic("uvm_fault_unwire_locked: unwiring " 1905 "non-wired memory"); 1906 1907 /* 1908 * make sure the current entry is for the address we're 1909 * dealing with. if not, grab the next entry. 1910 */ 1911 #ifdef DIAGNOSTIC 1912 if (va < entry->start) 1913 panic("uvm_fault_unwire_locked: hole 1"); 1914 #endif 1915 if (va >= entry->end) { 1916 #ifdef DIAGNOSTIC 1917 if (entry->next == &map->header || 1918 entry->next->start > entry->end) 1919 panic("uvm_fault_unwire_locked: hole 2"); 1920 #endif 1921 entry = entry->next; 1922 } 1923 1924 /* 1925 * if the entry is no longer wired, tell the pmap. 1926 */ 1927 if (VM_MAPENT_ISWIRED(entry) == 0) 1928 pmap_unwire(pmap, va); 1929 1930 pg = PHYS_TO_VM_PAGE(pa); 1931 if (pg) 1932 uvm_pageunwire(pg); 1933 } 1934 1935 uvm_unlock_pageq(); 1936 } 1937