1 /* $NetBSD: uvm_fault.c,v 1.180 2011/01/06 05:51:57 enami Exp $ */ 2 3 /* 4 * 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Charles D. Cranor and 19 * Washington University. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp 35 */ 36 37 /* 38 * uvm_fault.c: fault handler 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.180 2011/01/06 05:51:57 enami Exp $"); 43 44 #include "opt_uvmhist.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/proc.h> 50 #include <sys/malloc.h> 51 #include <sys/mman.h> 52 53 #include <uvm/uvm.h> 54 55 /* 56 * 57 * a word on page faults: 58 * 59 * types of page faults we handle: 60 * 61 * CASE 1: upper layer faults CASE 2: lower layer faults 62 * 63 * CASE 1A CASE 1B CASE 2A CASE 2B 64 * read/write1 write>1 read/write +-cow_write/zero 65 * | | | | 66 * +--|--+ +--|--+ +-----+ + | + | +-----+ 67 * amap | V | | ---------> new | | | | ^ | 68 * +-----+ +-----+ +-----+ + | + | +--|--+ 69 * | | | 70 * +-----+ +-----+ +--|--+ | +--|--+ 71 * uobj | d/c | | d/c | | V | +----+ | 72 * +-----+ +-----+ +-----+ +-----+ 73 * 74 * d/c = don't care 75 * 76 * case [0]: layerless fault 77 * no amap or uobj is present. this is an error. 78 * 79 * case [1]: upper layer fault [anon active] 80 * 1A: [read] or [write with anon->an_ref == 1] 81 * I/O takes place in upper level anon and uobj is not touched. 82 * 1B: [write with anon->an_ref > 1] 83 * new anon is alloc'd and data is copied off ["COW"] 84 * 85 * case [2]: lower layer fault [uobj] 86 * 2A: [read on non-NULL uobj] or [write to non-copy_on_write area] 87 * I/O takes place directly in object. 88 * 2B: [write to copy_on_write] or [read on NULL uobj] 89 * data is "promoted" from uobj to a new anon. 90 * if uobj is null, then we zero fill. 91 * 92 * we follow the standard UVM locking protocol ordering: 93 * 94 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ) 95 * we hold a PG_BUSY page if we unlock for I/O 96 * 97 * 98 * the code is structured as follows: 99 * 100 * - init the "IN" params in the ufi structure 101 * ReFault: (ERESTART returned to the loop in uvm_fault_internal) 102 * - do lookups [locks maps], check protection, handle needs_copy 103 * - check for case 0 fault (error) 104 * - establish "range" of fault 105 * - if we have an amap lock it and extract the anons 106 * - if sequential advice deactivate pages behind us 107 * - at the same time check pmap for unmapped areas and anon for pages 108 * that we could map in (and do map it if found) 109 * - check object for resident pages that we could map in 110 * - if (case 2) goto Case2 111 * - >>> handle case 1 112 * - ensure source anon is resident in RAM 113 * - if case 1B alloc new anon and copy from source 114 * - map the correct page in 115 * Case2: 116 * - >>> handle case 2 117 * - ensure source page is resident (if uobj) 118 * - if case 2B alloc new anon and copy from source (could be zero 119 * fill if uobj == NULL) 120 * - map the correct page in 121 * - done! 122 * 123 * note on paging: 124 * if we have to do I/O we place a PG_BUSY page in the correct object, 125 * unlock everything, and do the I/O. when I/O is done we must reverify 126 * the state of the world before assuming that our data structures are 127 * valid. [because mappings could change while the map is unlocked] 128 * 129 * alternative 1: unbusy the page in question and restart the page fault 130 * from the top (ReFault). this is easy but does not take advantage 131 * of the information that we already have from our previous lookup, 132 * although it is possible that the "hints" in the vm_map will help here. 133 * 134 * alternative 2: the system already keeps track of a "version" number of 135 * a map. [i.e. every time you write-lock a map (e.g. to change a 136 * mapping) you bump the version number up by one...] so, we can save 137 * the version number of the map before we release the lock and start I/O. 138 * then when I/O is done we can relock and check the version numbers 139 * to see if anything changed. this might save us some over 1 because 140 * we don't have to unbusy the page and may be less compares(?). 141 * 142 * alternative 3: put in backpointers or a way to "hold" part of a map 143 * in place while I/O is in progress. this could be complex to 144 * implement (especially with structures like amap that can be referenced 145 * by multiple map entries, and figuring out what should wait could be 146 * complex as well...). 147 * 148 * we use alternative 2. given that we are multi-threaded now we may want 149 * to reconsider the choice. 150 */ 151 152 /* 153 * local data structures 154 */ 155 156 struct uvm_advice { 157 int advice; 158 int nback; 159 int nforw; 160 }; 161 162 /* 163 * page range array: 164 * note: index in array must match "advice" value 165 * XXX: borrowed numbers from freebsd. do they work well for us? 166 */ 167 168 static const struct uvm_advice uvmadvice[] = { 169 { MADV_NORMAL, 3, 4 }, 170 { MADV_RANDOM, 0, 0 }, 171 { MADV_SEQUENTIAL, 8, 7}, 172 }; 173 174 #define UVM_MAXRANGE 16 /* must be MAX() of nback+nforw+1 */ 175 176 /* 177 * private prototypes 178 */ 179 180 /* 181 * inline functions 182 */ 183 184 /* 185 * uvmfault_anonflush: try and deactivate pages in specified anons 186 * 187 * => does not have to deactivate page if it is busy 188 */ 189 190 static inline void 191 uvmfault_anonflush(struct vm_anon **anons, int n) 192 { 193 int lcv; 194 struct vm_page *pg; 195 196 for (lcv = 0; lcv < n; lcv++) { 197 if (anons[lcv] == NULL) 198 continue; 199 mutex_enter(&anons[lcv]->an_lock); 200 pg = anons[lcv]->an_page; 201 if (pg && (pg->flags & PG_BUSY) == 0) { 202 mutex_enter(&uvm_pageqlock); 203 if (pg->wire_count == 0) { 204 uvm_pagedeactivate(pg); 205 } 206 mutex_exit(&uvm_pageqlock); 207 } 208 mutex_exit(&anons[lcv]->an_lock); 209 } 210 } 211 212 /* 213 * normal functions 214 */ 215 216 /* 217 * uvmfault_amapcopy: clear "needs_copy" in a map. 218 * 219 * => called with VM data structures unlocked (usually, see below) 220 * => we get a write lock on the maps and clear needs_copy for a VA 221 * => if we are out of RAM we sleep (waiting for more) 222 */ 223 224 static void 225 uvmfault_amapcopy(struct uvm_faultinfo *ufi) 226 { 227 for (;;) { 228 229 /* 230 * no mapping? give up. 231 */ 232 233 if (uvmfault_lookup(ufi, true) == false) 234 return; 235 236 /* 237 * copy if needed. 238 */ 239 240 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) 241 amap_copy(ufi->map, ufi->entry, AMAP_COPY_NOWAIT, 242 ufi->orig_rvaddr, ufi->orig_rvaddr + 1); 243 244 /* 245 * didn't work? must be out of RAM. unlock and sleep. 246 */ 247 248 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) { 249 uvmfault_unlockmaps(ufi, true); 250 uvm_wait("fltamapcopy"); 251 continue; 252 } 253 254 /* 255 * got it! unlock and return. 256 */ 257 258 uvmfault_unlockmaps(ufi, true); 259 return; 260 } 261 /*NOTREACHED*/ 262 } 263 264 /* 265 * uvmfault_anonget: get data in an anon into a non-busy, non-released 266 * page in that anon. 267 * 268 * => maps, amap, and anon locked by caller. 269 * => if we fail (result != 0) we unlock everything. 270 * => if we are successful, we return with everything still locked. 271 * => we don't move the page on the queues [gets moved later] 272 * => if we allocate a new page [we_own], it gets put on the queues. 273 * either way, the result is that the page is on the queues at return time 274 * => for pages which are on loan from a uvm_object (and thus are not 275 * owned by the anon): if successful, we return with the owning object 276 * locked. the caller must unlock this object when it unlocks everything 277 * else. 278 */ 279 280 int 281 uvmfault_anonget(struct uvm_faultinfo *ufi, struct vm_amap *amap, 282 struct vm_anon *anon) 283 { 284 bool we_own; /* we own anon's page? */ 285 bool locked; /* did we relock? */ 286 struct vm_page *pg; 287 int error; 288 UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist); 289 290 KASSERT(mutex_owned(&anon->an_lock)); 291 292 error = 0; 293 uvmexp.fltanget++; 294 /* bump rusage counters */ 295 if (anon->an_page) 296 curlwp->l_ru.ru_minflt++; 297 else 298 curlwp->l_ru.ru_majflt++; 299 300 /* 301 * loop until we get it, or fail. 302 */ 303 304 for (;;) { 305 we_own = false; /* true if we set PG_BUSY on a page */ 306 pg = anon->an_page; 307 308 /* 309 * if there is a resident page and it is loaned, then anon 310 * may not own it. call out to uvm_anon_lockpage() to ensure 311 * the real owner of the page has been identified and locked. 312 */ 313 314 if (pg && pg->loan_count) 315 pg = uvm_anon_lockloanpg(anon); 316 317 /* 318 * page there? make sure it is not busy/released. 319 */ 320 321 if (pg) { 322 323 /* 324 * at this point, if the page has a uobject [meaning 325 * we have it on loan], then that uobject is locked 326 * by us! if the page is busy, we drop all the 327 * locks (including uobject) and try again. 328 */ 329 330 if ((pg->flags & PG_BUSY) == 0) { 331 UVMHIST_LOG(maphist, "<- OK",0,0,0,0); 332 return (0); 333 } 334 pg->flags |= PG_WANTED; 335 uvmexp.fltpgwait++; 336 337 /* 338 * the last unlock must be an atomic unlock+wait on 339 * the owner of page 340 */ 341 342 if (pg->uobject) { /* owner is uobject ? */ 343 uvmfault_unlockall(ufi, amap, NULL, anon); 344 UVMHIST_LOG(maphist, " unlock+wait on uobj",0, 345 0,0,0); 346 UVM_UNLOCK_AND_WAIT(pg, 347 &pg->uobject->vmobjlock, 348 false, "anonget1",0); 349 } else { 350 /* anon owns page */ 351 uvmfault_unlockall(ufi, amap, NULL, NULL); 352 UVMHIST_LOG(maphist, " unlock+wait on anon",0, 353 0,0,0); 354 UVM_UNLOCK_AND_WAIT(pg,&anon->an_lock,0, 355 "anonget2",0); 356 } 357 } else { 358 #if defined(VMSWAP) 359 360 /* 361 * no page, we must try and bring it in. 362 */ 363 364 pg = uvm_pagealloc(NULL, 365 ufi != NULL ? ufi->orig_rvaddr : 0, 366 anon, UVM_FLAG_COLORMATCH); 367 if (pg == NULL) { /* out of RAM. */ 368 uvmfault_unlockall(ufi, amap, NULL, anon); 369 uvmexp.fltnoram++; 370 UVMHIST_LOG(maphist, " noram -- UVM_WAIT",0, 371 0,0,0); 372 if (!uvm_reclaimable()) { 373 return ENOMEM; 374 } 375 uvm_wait("flt_noram1"); 376 } else { 377 /* we set the PG_BUSY bit */ 378 we_own = true; 379 uvmfault_unlockall(ufi, amap, NULL, anon); 380 381 /* 382 * we are passing a PG_BUSY+PG_FAKE+PG_CLEAN 383 * page into the uvm_swap_get function with 384 * all data structures unlocked. note that 385 * it is ok to read an_swslot here because 386 * we hold PG_BUSY on the page. 387 */ 388 uvmexp.pageins++; 389 error = uvm_swap_get(pg, anon->an_swslot, 390 PGO_SYNCIO); 391 392 /* 393 * we clean up after the i/o below in the 394 * "we_own" case 395 */ 396 } 397 #else /* defined(VMSWAP) */ 398 panic("%s: no page", __func__); 399 #endif /* defined(VMSWAP) */ 400 } 401 402 /* 403 * now relock and try again 404 */ 405 406 locked = uvmfault_relock(ufi); 407 if (locked && amap != NULL) { 408 amap_lock(amap); 409 } 410 if (locked || we_own) 411 mutex_enter(&anon->an_lock); 412 413 /* 414 * if we own the page (i.e. we set PG_BUSY), then we need 415 * to clean up after the I/O. there are three cases to 416 * consider: 417 * [1] page released during I/O: free anon and ReFault. 418 * [2] I/O not OK. free the page and cause the fault 419 * to fail. 420 * [3] I/O OK! activate the page and sync with the 421 * non-we_own case (i.e. drop anon lock if not locked). 422 */ 423 424 if (we_own) { 425 #if defined(VMSWAP) 426 if (pg->flags & PG_WANTED) { 427 wakeup(pg); 428 } 429 if (error) { 430 431 /* 432 * remove the swap slot from the anon 433 * and mark the anon as having no real slot. 434 * don't free the swap slot, thus preventing 435 * it from being used again. 436 */ 437 438 if (anon->an_swslot > 0) 439 uvm_swap_markbad(anon->an_swslot, 1); 440 anon->an_swslot = SWSLOT_BAD; 441 442 if ((pg->flags & PG_RELEASED) != 0) 443 goto released; 444 445 /* 446 * note: page was never !PG_BUSY, so it 447 * can't be mapped and thus no need to 448 * pmap_page_protect it... 449 */ 450 451 mutex_enter(&uvm_pageqlock); 452 uvm_pagefree(pg); 453 mutex_exit(&uvm_pageqlock); 454 455 if (locked) 456 uvmfault_unlockall(ufi, amap, NULL, 457 anon); 458 else 459 mutex_exit(&anon->an_lock); 460 UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0); 461 return error; 462 } 463 464 if ((pg->flags & PG_RELEASED) != 0) { 465 released: 466 KASSERT(anon->an_ref == 0); 467 468 /* 469 * released while we unlocked amap. 470 */ 471 472 if (locked) 473 uvmfault_unlockall(ufi, amap, NULL, 474 NULL); 475 476 uvm_anon_release(anon); 477 478 if (error) { 479 UVMHIST_LOG(maphist, 480 "<- ERROR/RELEASED", 0,0,0,0); 481 return error; 482 } 483 484 UVMHIST_LOG(maphist, "<- RELEASED", 0,0,0,0); 485 return ERESTART; 486 } 487 488 /* 489 * we've successfully read the page, activate it. 490 */ 491 492 mutex_enter(&uvm_pageqlock); 493 uvm_pageactivate(pg); 494 mutex_exit(&uvm_pageqlock); 495 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE); 496 UVM_PAGE_OWN(pg, NULL); 497 if (!locked) 498 mutex_exit(&anon->an_lock); 499 #else /* defined(VMSWAP) */ 500 panic("%s: we_own", __func__); 501 #endif /* defined(VMSWAP) */ 502 } 503 504 /* 505 * we were not able to relock. restart fault. 506 */ 507 508 if (!locked) { 509 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0); 510 return (ERESTART); 511 } 512 513 /* 514 * verify no one has touched the amap and moved the anon on us. 515 */ 516 517 if (ufi != NULL && 518 amap_lookup(&ufi->entry->aref, 519 ufi->orig_rvaddr - ufi->entry->start) != anon) { 520 521 uvmfault_unlockall(ufi, amap, NULL, anon); 522 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0); 523 return (ERESTART); 524 } 525 526 /* 527 * try it again! 528 */ 529 530 uvmexp.fltanretry++; 531 continue; 532 } 533 /*NOTREACHED*/ 534 } 535 536 /* 537 * uvmfault_promote: promote data to a new anon. used for 1B and 2B. 538 * 539 * 1. allocate an anon and a page. 540 * 2. fill its contents. 541 * 3. put it into amap. 542 * 543 * => if we fail (result != 0) we unlock everything. 544 * => on success, return a new locked anon via 'nanon'. 545 * (*nanon)->an_page will be a resident, locked, dirty page. 546 */ 547 548 static int 549 uvmfault_promote(struct uvm_faultinfo *ufi, 550 struct vm_anon *oanon, 551 struct vm_page *uobjpage, 552 struct vm_anon **nanon, /* OUT: allocated anon */ 553 struct vm_anon **spare) 554 { 555 struct vm_amap *amap = ufi->entry->aref.ar_amap; 556 struct uvm_object *uobj; 557 struct vm_anon *anon; 558 struct vm_page *pg; 559 struct vm_page *opg; 560 int error; 561 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist); 562 563 if (oanon) { 564 /* anon COW */ 565 opg = oanon->an_page; 566 KASSERT(opg != NULL); 567 KASSERT(opg->uobject == NULL || opg->loan_count > 0); 568 } else if (uobjpage != PGO_DONTCARE) { 569 /* object-backed COW */ 570 opg = uobjpage; 571 } else { 572 /* ZFOD */ 573 opg = NULL; 574 } 575 if (opg != NULL) { 576 uobj = opg->uobject; 577 } else { 578 uobj = NULL; 579 } 580 581 KASSERT(amap != NULL); 582 KASSERT(uobjpage != NULL); 583 KASSERT(uobjpage == PGO_DONTCARE || (uobjpage->flags & PG_BUSY) != 0); 584 KASSERT(mutex_owned(&amap->am_l)); 585 KASSERT(oanon == NULL || mutex_owned(&oanon->an_lock)); 586 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock)); 587 #if 0 588 KASSERT(*spare == NULL || !mutex_owned(&(*spare)->an_lock)); 589 #endif 590 591 if (*spare != NULL) { 592 anon = *spare; 593 *spare = NULL; 594 mutex_enter(&anon->an_lock); 595 } else if (ufi->map != kernel_map) { 596 anon = uvm_analloc(); 597 } else { 598 UVMHIST_LOG(maphist, "kernel_map, unlock and retry", 0,0,0,0); 599 600 /* 601 * we can't allocate anons with kernel_map locked. 602 */ 603 604 uvm_page_unbusy(&uobjpage, 1); 605 uvmfault_unlockall(ufi, amap, uobj, oanon); 606 607 *spare = uvm_analloc(); 608 if (*spare == NULL) { 609 goto nomem; 610 } 611 mutex_exit(&(*spare)->an_lock); 612 error = ERESTART; 613 goto done; 614 } 615 if (anon) { 616 617 /* 618 * The new anon is locked. 619 * 620 * if opg == NULL, we want a zero'd, dirty page, 621 * so have uvm_pagealloc() do that for us. 622 */ 623 624 pg = uvm_pagealloc(NULL, ufi->orig_rvaddr, anon, 625 UVM_FLAG_COLORMATCH | (opg == NULL ? UVM_PGA_ZERO : 0)); 626 } else { 627 pg = NULL; 628 } 629 630 /* 631 * out of memory resources? 632 */ 633 634 if (pg == NULL) { 635 /* save anon for the next try. */ 636 if (anon != NULL) { 637 mutex_exit(&anon->an_lock); 638 *spare = anon; 639 } 640 641 /* unlock and fail ... */ 642 uvm_page_unbusy(&uobjpage, 1); 643 uvmfault_unlockall(ufi, amap, uobj, oanon); 644 nomem: 645 if (!uvm_reclaimable()) { 646 UVMHIST_LOG(maphist, "out of VM", 0,0,0,0); 647 uvmexp.fltnoanon++; 648 error = ENOMEM; 649 goto done; 650 } 651 652 UVMHIST_LOG(maphist, "out of RAM, waiting for more", 0,0,0,0); 653 uvmexp.fltnoram++; 654 uvm_wait("flt_noram5"); 655 error = ERESTART; 656 goto done; 657 } 658 659 /* copy page [pg now dirty] */ 660 if (opg) { 661 uvm_pagecopy(opg, pg); 662 } 663 664 amap_add(&ufi->entry->aref, ufi->orig_rvaddr - ufi->entry->start, anon, 665 oanon != NULL); 666 667 *nanon = anon; 668 error = 0; 669 done: 670 return error; 671 } 672 673 674 /* 675 * F A U L T - m a i n e n t r y p o i n t 676 */ 677 678 /* 679 * uvm_fault: page fault handler 680 * 681 * => called from MD code to resolve a page fault 682 * => VM data structures usually should be unlocked. however, it is 683 * possible to call here with the main map locked if the caller 684 * gets a write lock, sets it recusive, and then calls us (c.f. 685 * uvm_map_pageable). this should be avoided because it keeps 686 * the map locked off during I/O. 687 * => MUST NEVER BE CALLED IN INTERRUPT CONTEXT 688 */ 689 690 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \ 691 ~VM_PROT_WRITE : VM_PROT_ALL) 692 693 /* fault_flag values passed from uvm_fault_wire to uvm_fault_internal */ 694 #define UVM_FAULT_WIRE (1 << 0) 695 #define UVM_FAULT_MAXPROT (1 << 1) 696 697 struct uvm_faultctx { 698 vm_prot_t access_type; 699 vm_prot_t enter_prot; 700 vaddr_t startva; 701 int npages; 702 int centeridx; 703 struct vm_anon *anon_spare; 704 bool wire_mapping; 705 bool narrow; 706 bool wire_paging; 707 bool cow_now; 708 bool promote; 709 }; 710 711 static inline int uvm_fault_check( 712 struct uvm_faultinfo *, struct uvm_faultctx *, 713 struct vm_anon ***, bool); 714 715 static int uvm_fault_upper( 716 struct uvm_faultinfo *, struct uvm_faultctx *, 717 struct vm_anon **); 718 static inline int uvm_fault_upper_lookup( 719 struct uvm_faultinfo *, const struct uvm_faultctx *, 720 struct vm_anon **, struct vm_page **); 721 static inline void uvm_fault_upper_neighbor( 722 struct uvm_faultinfo *, const struct uvm_faultctx *, 723 vaddr_t, struct vm_page *, bool); 724 static inline int uvm_fault_upper_loan( 725 struct uvm_faultinfo *, struct uvm_faultctx *, 726 struct vm_anon *, struct uvm_object **); 727 static inline int uvm_fault_upper_promote( 728 struct uvm_faultinfo *, struct uvm_faultctx *, 729 struct uvm_object *, struct vm_anon *); 730 static inline int uvm_fault_upper_direct( 731 struct uvm_faultinfo *, struct uvm_faultctx *, 732 struct uvm_object *, struct vm_anon *); 733 static int uvm_fault_upper_enter( 734 struct uvm_faultinfo *, const struct uvm_faultctx *, 735 struct uvm_object *, struct vm_anon *, 736 struct vm_page *, struct vm_anon *); 737 static inline void uvm_fault_upper_done( 738 struct uvm_faultinfo *, const struct uvm_faultctx *, 739 struct vm_anon *, struct vm_page *); 740 741 static int uvm_fault_lower( 742 struct uvm_faultinfo *, struct uvm_faultctx *, 743 struct vm_page **); 744 static inline void uvm_fault_lower_lookup( 745 struct uvm_faultinfo *, const struct uvm_faultctx *, 746 struct vm_page **); 747 static inline void uvm_fault_lower_neighbor( 748 struct uvm_faultinfo *, const struct uvm_faultctx *, 749 vaddr_t, struct vm_page *, bool); 750 static inline int uvm_fault_lower_io( 751 struct uvm_faultinfo *, const struct uvm_faultctx *, 752 struct uvm_object **, struct vm_page **); 753 static inline int uvm_fault_lower_direct( 754 struct uvm_faultinfo *, struct uvm_faultctx *, 755 struct uvm_object *, struct vm_page *); 756 static inline int uvm_fault_lower_direct_loan( 757 struct uvm_faultinfo *, struct uvm_faultctx *, 758 struct uvm_object *, struct vm_page **, 759 struct vm_page **); 760 static inline int uvm_fault_lower_promote( 761 struct uvm_faultinfo *, struct uvm_faultctx *, 762 struct uvm_object *, struct vm_page *); 763 static int uvm_fault_lower_enter( 764 struct uvm_faultinfo *, const struct uvm_faultctx *, 765 struct uvm_object *, 766 struct vm_anon *, struct vm_page *, 767 struct vm_page *); 768 static inline void uvm_fault_lower_done( 769 struct uvm_faultinfo *, const struct uvm_faultctx *, 770 struct uvm_object *, struct vm_page *); 771 772 int 773 uvm_fault_internal(struct vm_map *orig_map, vaddr_t vaddr, 774 vm_prot_t access_type, int fault_flag) 775 { 776 struct uvm_faultinfo ufi; 777 struct uvm_faultctx flt = { 778 .access_type = access_type, 779 780 /* don't look for neighborhood * pages on "wire" fault */ 781 .narrow = (fault_flag & UVM_FAULT_WIRE) != 0, 782 783 /* "wire" fault causes wiring of both mapping and paging */ 784 .wire_mapping = (fault_flag & UVM_FAULT_WIRE) != 0, 785 .wire_paging = (fault_flag & UVM_FAULT_WIRE) != 0, 786 }; 787 const bool maxprot = (fault_flag & UVM_FAULT_MAXPROT) != 0; 788 struct vm_anon *anons_store[UVM_MAXRANGE], **anons; 789 struct vm_page *pages_store[UVM_MAXRANGE], **pages; 790 int error; 791 UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist); 792 793 UVMHIST_LOG(maphist, "(map=0x%x, vaddr=0x%x, at=%d, ff=%d)", 794 orig_map, vaddr, access_type, fault_flag); 795 796 curcpu()->ci_data.cpu_nfault++; 797 798 /* 799 * init the IN parameters in the ufi 800 */ 801 802 ufi.orig_map = orig_map; 803 ufi.orig_rvaddr = trunc_page(vaddr); 804 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */ 805 806 error = ERESTART; 807 while (error == ERESTART) { 808 anons = anons_store; 809 pages = pages_store; 810 811 error = uvm_fault_check(&ufi, &flt, &anons, maxprot); 812 if (error != 0) 813 continue; 814 815 error = uvm_fault_upper_lookup(&ufi, &flt, anons, pages); 816 if (error != 0) 817 continue; 818 819 if (pages[flt.centeridx] == PGO_DONTCARE) 820 error = uvm_fault_upper(&ufi, &flt, anons); 821 else { 822 struct uvm_object * const uobj = 823 ufi.entry->object.uvm_obj; 824 825 if (uobj && uobj->pgops->pgo_fault != NULL) { 826 /* 827 * invoke "special" fault routine. 828 */ 829 mutex_enter(&uobj->vmobjlock); 830 /* locked: maps(read), amap(if there), uobj */ 831 error = uobj->pgops->pgo_fault(&ufi, 832 flt.startva, pages, flt.npages, 833 flt.centeridx, flt.access_type, 834 PGO_LOCKED|PGO_SYNCIO); 835 836 /* 837 * locked: nothing, pgo_fault has unlocked 838 * everything 839 */ 840 841 /* 842 * object fault routine responsible for 843 * pmap_update(). 844 */ 845 } else { 846 error = uvm_fault_lower(&ufi, &flt, pages); 847 } 848 } 849 } 850 851 if (flt.anon_spare != NULL) { 852 flt.anon_spare->an_ref--; 853 uvm_anfree(flt.anon_spare); 854 } 855 return error; 856 } 857 858 /* 859 * uvm_fault_check: check prot, handle needs-copy, etc. 860 * 861 * 1. lookup entry. 862 * 2. check protection. 863 * 3. adjust fault condition (mainly for simulated fault). 864 * 4. handle needs-copy (lazy amap copy). 865 * 5. establish range of interest for neighbor fault (aka pre-fault). 866 * 6. look up anons (if amap exists). 867 * 7. flush pages (if MADV_SEQUENTIAL) 868 * 869 * => called with nothing locked. 870 * => if we fail (result != 0) we unlock everything. 871 * => initialize/adjust many members of flt. 872 */ 873 874 static int 875 uvm_fault_check( 876 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 877 struct vm_anon ***ranons, bool maxprot) 878 { 879 struct vm_amap *amap; 880 struct uvm_object *uobj; 881 vm_prot_t check_prot; 882 int nback, nforw; 883 UVMHIST_FUNC("uvm_fault_check"); UVMHIST_CALLED(maphist); 884 885 /* 886 * lookup and lock the maps 887 */ 888 889 if (uvmfault_lookup(ufi, false) == false) { 890 UVMHIST_LOG(maphist, "<- no mapping @ 0x%x", ufi->orig_rvaddr, 891 0,0,0); 892 return EFAULT; 893 } 894 /* locked: maps(read) */ 895 896 #ifdef DIAGNOSTIC 897 if ((ufi->map->flags & VM_MAP_PAGEABLE) == 0) { 898 printf("Page fault on non-pageable map:\n"); 899 printf("ufi->map = %p\n", ufi->map); 900 printf("ufi->orig_map = %p\n", ufi->orig_map); 901 printf("ufi->orig_rvaddr = 0x%lx\n", (u_long) ufi->orig_rvaddr); 902 panic("uvm_fault: (ufi->map->flags & VM_MAP_PAGEABLE) == 0"); 903 } 904 #endif 905 906 /* 907 * check protection 908 */ 909 910 check_prot = maxprot ? 911 ufi->entry->max_protection : ufi->entry->protection; 912 if ((check_prot & flt->access_type) != flt->access_type) { 913 UVMHIST_LOG(maphist, 914 "<- protection failure (prot=0x%x, access=0x%x)", 915 ufi->entry->protection, flt->access_type, 0, 0); 916 uvmfault_unlockmaps(ufi, false); 917 return EACCES; 918 } 919 920 /* 921 * "enter_prot" is the protection we want to enter the page in at. 922 * for certain pages (e.g. copy-on-write pages) this protection can 923 * be more strict than ufi->entry->protection. "wired" means either 924 * the entry is wired or we are fault-wiring the pg. 925 */ 926 927 flt->enter_prot = ufi->entry->protection; 928 if (VM_MAPENT_ISWIRED(ufi->entry)) 929 flt->wire_mapping = true; 930 931 if (flt->wire_mapping) { 932 flt->access_type = flt->enter_prot; /* full access for wired */ 933 flt->cow_now = (check_prot & VM_PROT_WRITE) != 0; 934 } else { 935 flt->cow_now = (flt->access_type & VM_PROT_WRITE) != 0; 936 } 937 938 flt->promote = false; 939 940 /* 941 * handle "needs_copy" case. if we need to copy the amap we will 942 * have to drop our readlock and relock it with a write lock. (we 943 * need a write lock to change anything in a map entry [e.g. 944 * needs_copy]). 945 */ 946 947 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) { 948 if (flt->cow_now || (ufi->entry->object.uvm_obj == NULL)) { 949 KASSERT(!maxprot); 950 /* need to clear */ 951 UVMHIST_LOG(maphist, 952 " need to clear needs_copy and refault",0,0,0,0); 953 uvmfault_unlockmaps(ufi, false); 954 uvmfault_amapcopy(ufi); 955 uvmexp.fltamcopy++; 956 return ERESTART; 957 958 } else { 959 960 /* 961 * ensure that we pmap_enter page R/O since 962 * needs_copy is still true 963 */ 964 965 flt->enter_prot &= ~VM_PROT_WRITE; 966 } 967 } 968 969 /* 970 * identify the players 971 */ 972 973 amap = ufi->entry->aref.ar_amap; /* upper layer */ 974 uobj = ufi->entry->object.uvm_obj; /* lower layer */ 975 976 /* 977 * check for a case 0 fault. if nothing backing the entry then 978 * error now. 979 */ 980 981 if (amap == NULL && uobj == NULL) { 982 uvmfault_unlockmaps(ufi, false); 983 UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0); 984 return EFAULT; 985 } 986 987 /* 988 * establish range of interest based on advice from mapper 989 * and then clip to fit map entry. note that we only want 990 * to do this the first time through the fault. if we 991 * ReFault we will disable this by setting "narrow" to true. 992 */ 993 994 if (flt->narrow == false) { 995 996 /* wide fault (!narrow) */ 997 KASSERT(uvmadvice[ufi->entry->advice].advice == 998 ufi->entry->advice); 999 nback = MIN(uvmadvice[ufi->entry->advice].nback, 1000 (ufi->orig_rvaddr - ufi->entry->start) >> PAGE_SHIFT); 1001 flt->startva = ufi->orig_rvaddr - (nback << PAGE_SHIFT); 1002 /* 1003 * note: "-1" because we don't want to count the 1004 * faulting page as forw 1005 */ 1006 nforw = MIN(uvmadvice[ufi->entry->advice].nforw, 1007 ((ufi->entry->end - ufi->orig_rvaddr) >> 1008 PAGE_SHIFT) - 1); 1009 flt->npages = nback + nforw + 1; 1010 flt->centeridx = nback; 1011 1012 flt->narrow = true; /* ensure only once per-fault */ 1013 1014 } else { 1015 1016 /* narrow fault! */ 1017 nback = nforw = 0; 1018 flt->startva = ufi->orig_rvaddr; 1019 flt->npages = 1; 1020 flt->centeridx = 0; 1021 1022 } 1023 /* offset from entry's start to pgs' start */ 1024 const voff_t eoff = flt->startva - ufi->entry->start; 1025 1026 /* locked: maps(read) */ 1027 UVMHIST_LOG(maphist, " narrow=%d, back=%d, forw=%d, startva=0x%x", 1028 flt->narrow, nback, nforw, flt->startva); 1029 UVMHIST_LOG(maphist, " entry=0x%x, amap=0x%x, obj=0x%x", ufi->entry, 1030 amap, uobj, 0); 1031 1032 /* 1033 * if we've got an amap, lock it and extract current anons. 1034 */ 1035 1036 if (amap) { 1037 amap_lock(amap); 1038 amap_lookups(&ufi->entry->aref, eoff, *ranons, flt->npages); 1039 } else { 1040 *ranons = NULL; /* to be safe */ 1041 } 1042 1043 /* locked: maps(read), amap(if there) */ 1044 KASSERT(amap == NULL || mutex_owned(&amap->am_l)); 1045 1046 /* 1047 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages 1048 * now and then forget about them (for the rest of the fault). 1049 */ 1050 1051 if (ufi->entry->advice == MADV_SEQUENTIAL && nback != 0) { 1052 1053 UVMHIST_LOG(maphist, " MADV_SEQUENTIAL: flushing backpages", 1054 0,0,0,0); 1055 /* flush back-page anons? */ 1056 if (amap) 1057 uvmfault_anonflush(*ranons, nback); 1058 1059 /* flush object? */ 1060 if (uobj) { 1061 voff_t uoff; 1062 1063 uoff = ufi->entry->offset + eoff; 1064 mutex_enter(&uobj->vmobjlock); 1065 (void) (uobj->pgops->pgo_put)(uobj, uoff, uoff + 1066 (nback << PAGE_SHIFT), PGO_DEACTIVATE); 1067 } 1068 1069 /* now forget about the backpages */ 1070 if (amap) 1071 *ranons += nback; 1072 flt->startva += (nback << PAGE_SHIFT); 1073 flt->npages -= nback; 1074 flt->centeridx = 0; 1075 } 1076 /* 1077 * => startva is fixed 1078 * => npages is fixed 1079 */ 1080 KASSERT(flt->startva <= ufi->orig_rvaddr); 1081 KASSERT(ufi->orig_rvaddr + ufi->orig_size <= 1082 flt->startva + (flt->npages << PAGE_SHIFT)); 1083 return 0; 1084 } 1085 1086 /* 1087 * uvm_fault_upper_lookup: look up existing h/w mapping and amap. 1088 * 1089 * iterate range of interest: 1090 * 1. check if h/w mapping exists. if yes, we don't care 1091 * 2. check if anon exists. if not, page is lower. 1092 * 3. if anon exists, enter h/w mapping for neighbors. 1093 * 1094 * => called with amap locked (if exists). 1095 */ 1096 1097 static int 1098 uvm_fault_upper_lookup( 1099 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 1100 struct vm_anon **anons, struct vm_page **pages) 1101 { 1102 struct vm_amap *amap = ufi->entry->aref.ar_amap; 1103 int lcv; 1104 vaddr_t currva; 1105 bool shadowed; 1106 UVMHIST_FUNC("uvm_fault_upper_lookup"); UVMHIST_CALLED(maphist); 1107 1108 /* locked: maps(read), amap(if there) */ 1109 KASSERT(amap == NULL || mutex_owned(&amap->am_l)); 1110 1111 /* 1112 * map in the backpages and frontpages we found in the amap in hopes 1113 * of preventing future faults. we also init the pages[] array as 1114 * we go. 1115 */ 1116 1117 currva = flt->startva; 1118 shadowed = false; 1119 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) { 1120 /* 1121 * don't play with VAs that are already mapped 1122 * (except for center) 1123 */ 1124 if (lcv != flt->centeridx && 1125 pmap_extract(ufi->orig_map->pmap, currva, NULL)) { 1126 pages[lcv] = PGO_DONTCARE; 1127 continue; 1128 } 1129 1130 /* 1131 * unmapped or center page. check if any anon at this level. 1132 */ 1133 if (amap == NULL || anons[lcv] == NULL) { 1134 pages[lcv] = NULL; 1135 continue; 1136 } 1137 1138 /* 1139 * check for present page and map if possible. re-activate it. 1140 */ 1141 1142 pages[lcv] = PGO_DONTCARE; 1143 if (lcv == flt->centeridx) { /* save center for later! */ 1144 shadowed = true; 1145 } else { 1146 struct vm_anon *anon = anons[lcv]; 1147 1148 mutex_enter(&anon->an_lock); 1149 struct vm_page *pg = anon->an_page; 1150 1151 /* ignore loaned and busy pages */ 1152 if (pg != NULL && pg->loan_count == 0 && 1153 (pg->flags & PG_BUSY) == 0) 1154 uvm_fault_upper_neighbor(ufi, flt, currva, 1155 pg, anon->an_ref > 1); 1156 mutex_exit(&anon->an_lock); 1157 } 1158 } 1159 1160 /* locked: maps(read), amap(if there) */ 1161 KASSERT(amap == NULL || mutex_owned(&amap->am_l)); 1162 /* (shadowed == true) if there is an anon at the faulting address */ 1163 UVMHIST_LOG(maphist, " shadowed=%d, will_get=%d", shadowed, 1164 (ufi->entry->object.uvm_obj && shadowed != false),0,0); 1165 1166 /* 1167 * note that if we are really short of RAM we could sleep in the above 1168 * call to pmap_enter with everything locked. bad? 1169 * 1170 * XXX Actually, that is bad; pmap_enter() should just fail in that 1171 * XXX case. --thorpej 1172 */ 1173 1174 return 0; 1175 } 1176 1177 /* 1178 * uvm_fault_upper_neighbor: enter single lower neighbor page. 1179 * 1180 * => called with amap and anon locked. 1181 */ 1182 1183 static void 1184 uvm_fault_upper_neighbor( 1185 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 1186 vaddr_t currva, struct vm_page *pg, bool readonly) 1187 { 1188 UVMHIST_FUNC("uvm_fault_upper_neighbor"); UVMHIST_CALLED(maphist); 1189 1190 /* locked: amap, anon */ 1191 1192 mutex_enter(&uvm_pageqlock); 1193 uvm_pageenqueue(pg); 1194 mutex_exit(&uvm_pageqlock); 1195 UVMHIST_LOG(maphist, 1196 " MAPPING: n anon: pm=0x%x, va=0x%x, pg=0x%x", 1197 ufi->orig_map->pmap, currva, pg, 0); 1198 uvmexp.fltnamap++; 1199 1200 /* 1201 * Since this page isn't the page that's actually faulting, 1202 * ignore pmap_enter() failures; it's not critical that we 1203 * enter these right now. 1204 */ 1205 1206 (void) pmap_enter(ufi->orig_map->pmap, currva, 1207 VM_PAGE_TO_PHYS(pg), 1208 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) : 1209 flt->enter_prot, 1210 PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0)); 1211 1212 pmap_update(ufi->orig_map->pmap); 1213 } 1214 1215 /* 1216 * uvm_fault_upper: handle upper fault. 1217 * 1218 * 1. acquire anon lock. 1219 * 2. get anon. let uvmfault_anonget do the dirty work. 1220 * 3. handle loan. 1221 * 4. dispatch direct or promote handlers. 1222 */ 1223 1224 static int 1225 uvm_fault_upper( 1226 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 1227 struct vm_anon **anons) 1228 { 1229 struct vm_amap * const amap = ufi->entry->aref.ar_amap; 1230 struct vm_anon * const anon = anons[flt->centeridx]; 1231 struct uvm_object *uobj; 1232 int error; 1233 UVMHIST_FUNC("uvm_fault_upper"); UVMHIST_CALLED(maphist); 1234 1235 /* locked: maps(read), amap */ 1236 KASSERT(mutex_owned(&amap->am_l)); 1237 1238 /* 1239 * handle case 1: fault on an anon in our amap 1240 */ 1241 1242 UVMHIST_LOG(maphist, " case 1 fault: anon=0x%x", anon, 0,0,0); 1243 mutex_enter(&anon->an_lock); 1244 1245 /* locked: maps(read), amap, anon */ 1246 KASSERT(mutex_owned(&amap->am_l)); 1247 KASSERT(mutex_owned(&anon->an_lock)); 1248 1249 /* 1250 * no matter if we have case 1A or case 1B we are going to need to 1251 * have the anon's memory resident. ensure that now. 1252 */ 1253 1254 /* 1255 * let uvmfault_anonget do the dirty work. 1256 * if it fails (!OK) it will unlock everything for us. 1257 * if it succeeds, locks are still valid and locked. 1258 * also, if it is OK, then the anon's page is on the queues. 1259 * if the page is on loan from a uvm_object, then anonget will 1260 * lock that object for us if it does not fail. 1261 */ 1262 1263 error = uvmfault_anonget(ufi, amap, anon); 1264 switch (error) { 1265 case 0: 1266 break; 1267 1268 case ERESTART: 1269 return ERESTART; 1270 1271 case EAGAIN: 1272 kpause("fltagain1", false, hz/2, NULL); 1273 return ERESTART; 1274 1275 default: 1276 return error; 1277 } 1278 1279 /* 1280 * uobj is non null if the page is on loan from an object (i.e. uobj) 1281 */ 1282 1283 uobj = anon->an_page->uobject; /* locked by anonget if !NULL */ 1284 1285 /* locked: maps(read), amap, anon, uobj(if one) */ 1286 KASSERT(mutex_owned(&amap->am_l)); 1287 KASSERT(mutex_owned(&anon->an_lock)); 1288 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock)); 1289 1290 /* 1291 * special handling for loaned pages 1292 */ 1293 1294 if (anon->an_page->loan_count) { 1295 error = uvm_fault_upper_loan(ufi, flt, anon, &uobj); 1296 if (error != 0) 1297 return error; 1298 } 1299 1300 /* 1301 * if we are case 1B then we will need to allocate a new blank 1302 * anon to transfer the data into. note that we have a lock 1303 * on anon, so no one can busy or release the page until we are done. 1304 * also note that the ref count can't drop to zero here because 1305 * it is > 1 and we are only dropping one ref. 1306 * 1307 * in the (hopefully very rare) case that we are out of RAM we 1308 * will unlock, wait for more RAM, and refault. 1309 * 1310 * if we are out of anon VM we kill the process (XXX: could wait?). 1311 */ 1312 1313 if (flt->cow_now && anon->an_ref > 1) { 1314 flt->promote = true; 1315 error = uvm_fault_upper_promote(ufi, flt, uobj, anon); 1316 } else { 1317 error = uvm_fault_upper_direct(ufi, flt, uobj, anon); 1318 } 1319 return error; 1320 } 1321 1322 /* 1323 * uvm_fault_upper_loan: handle loaned upper page. 1324 * 1325 * 1. if not cow'ing now, simply adjust flt->enter_prot. 1326 * 2. if cow'ing now, and if ref count is 1, break loan. 1327 */ 1328 1329 static int 1330 uvm_fault_upper_loan( 1331 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 1332 struct vm_anon *anon, struct uvm_object **ruobj) 1333 { 1334 struct vm_amap * const amap = ufi->entry->aref.ar_amap; 1335 int error = 0; 1336 UVMHIST_FUNC("uvm_fault_upper_loan"); UVMHIST_CALLED(maphist); 1337 1338 if (!flt->cow_now) { 1339 1340 /* 1341 * for read faults on loaned pages we just cap the 1342 * protection at read-only. 1343 */ 1344 1345 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE; 1346 1347 } else { 1348 /* 1349 * note that we can't allow writes into a loaned page! 1350 * 1351 * if we have a write fault on a loaned page in an 1352 * anon then we need to look at the anon's ref count. 1353 * if it is greater than one then we are going to do 1354 * a normal copy-on-write fault into a new anon (this 1355 * is not a problem). however, if the reference count 1356 * is one (a case where we would normally allow a 1357 * write directly to the page) then we need to kill 1358 * the loan before we continue. 1359 */ 1360 1361 /* >1 case is already ok */ 1362 if (anon->an_ref == 1) { 1363 error = uvm_loanbreak_anon(anon, *ruobj); 1364 if (error != 0) { 1365 uvmfault_unlockall(ufi, amap, *ruobj, anon); 1366 uvm_wait("flt_noram2"); 1367 return ERESTART; 1368 } 1369 /* if we were a loan reciever uobj is gone */ 1370 if (*ruobj) 1371 *ruobj = NULL; 1372 } 1373 } 1374 return error; 1375 } 1376 1377 /* 1378 * uvm_fault_upper_promote: promote upper page. 1379 * 1380 * 1. call uvmfault_promote. 1381 * 2. enqueue page. 1382 * 3. deref. 1383 * 4. pass page to uvm_fault_upper_enter. 1384 */ 1385 1386 static int 1387 uvm_fault_upper_promote( 1388 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 1389 struct uvm_object *uobj, struct vm_anon *anon) 1390 { 1391 struct vm_anon * const oanon = anon; 1392 struct vm_page *pg; 1393 int error; 1394 UVMHIST_FUNC("uvm_fault_upper_promote"); UVMHIST_CALLED(maphist); 1395 1396 UVMHIST_LOG(maphist, " case 1B: COW fault",0,0,0,0); 1397 uvmexp.flt_acow++; 1398 1399 error = uvmfault_promote(ufi, oanon, PGO_DONTCARE, &anon, 1400 &flt->anon_spare); 1401 switch (error) { 1402 case 0: 1403 break; 1404 case ERESTART: 1405 return ERESTART; 1406 default: 1407 return error; 1408 } 1409 1410 pg = anon->an_page; 1411 mutex_enter(&uvm_pageqlock); 1412 uvm_pageactivate(pg); 1413 mutex_exit(&uvm_pageqlock); 1414 pg->flags &= ~(PG_BUSY|PG_FAKE); 1415 UVM_PAGE_OWN(pg, NULL); 1416 1417 /* deref: can not drop to zero here by defn! */ 1418 oanon->an_ref--; 1419 1420 /* 1421 * note: oanon is still locked, as is the new anon. we 1422 * need to check for this later when we unlock oanon; if 1423 * oanon != anon, we'll have to unlock anon, too. 1424 */ 1425 1426 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon); 1427 } 1428 1429 /* 1430 * uvm_fault_upper_direct: handle direct fault. 1431 */ 1432 1433 static int 1434 uvm_fault_upper_direct( 1435 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 1436 struct uvm_object *uobj, struct vm_anon *anon) 1437 { 1438 struct vm_anon * const oanon = anon; 1439 struct vm_page *pg; 1440 UVMHIST_FUNC("uvm_fault_upper_direct"); UVMHIST_CALLED(maphist); 1441 1442 uvmexp.flt_anon++; 1443 pg = anon->an_page; 1444 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */ 1445 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE; 1446 1447 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon); 1448 } 1449 1450 /* 1451 * uvm_fault_upper_enter: enter h/w mapping of upper page. 1452 */ 1453 1454 static int 1455 uvm_fault_upper_enter( 1456 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 1457 struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg, 1458 struct vm_anon *oanon) 1459 { 1460 struct vm_amap * const amap = ufi->entry->aref.ar_amap; 1461 UVMHIST_FUNC("uvm_fault_upper_enter"); UVMHIST_CALLED(maphist); 1462 1463 /* locked: maps(read), amap, oanon, anon(if different from oanon) */ 1464 KASSERT(mutex_owned(&amap->am_l)); 1465 KASSERT(mutex_owned(&anon->an_lock)); 1466 KASSERT(mutex_owned(&oanon->an_lock)); 1467 1468 /* 1469 * now map the page in. 1470 */ 1471 1472 UVMHIST_LOG(maphist, 1473 " MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x, promote=%d", 1474 ufi->orig_map->pmap, ufi->orig_rvaddr, pg, flt->promote); 1475 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr, 1476 VM_PAGE_TO_PHYS(pg), 1477 flt->enter_prot, flt->access_type | PMAP_CANFAIL | 1478 (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) { 1479 1480 /* 1481 * No need to undo what we did; we can simply think of 1482 * this as the pmap throwing away the mapping information. 1483 * 1484 * We do, however, have to go through the ReFault path, 1485 * as the map may change while we're asleep. 1486 */ 1487 1488 if (anon != oanon) 1489 mutex_exit(&anon->an_lock); 1490 uvmfault_unlockall(ufi, amap, uobj, oanon); 1491 if (!uvm_reclaimable()) { 1492 UVMHIST_LOG(maphist, 1493 "<- failed. out of VM",0,0,0,0); 1494 /* XXX instrumentation */ 1495 return ENOMEM; 1496 } 1497 /* XXX instrumentation */ 1498 uvm_wait("flt_pmfail1"); 1499 return ERESTART; 1500 } 1501 1502 uvm_fault_upper_done(ufi, flt, anon, pg); 1503 1504 /* 1505 * done case 1! finish up by unlocking everything and returning success 1506 */ 1507 1508 if (anon != oanon) { 1509 mutex_exit(&anon->an_lock); 1510 } 1511 pmap_update(ufi->orig_map->pmap); 1512 uvmfault_unlockall(ufi, amap, uobj, oanon); 1513 return 0; 1514 } 1515 1516 /* 1517 * uvm_fault_upper_done: queue upper center page. 1518 */ 1519 1520 static void 1521 uvm_fault_upper_done( 1522 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 1523 struct vm_anon *anon, struct vm_page *pg) 1524 { 1525 const bool wire_paging = flt->wire_paging; 1526 1527 UVMHIST_FUNC("uvm_fault_upper_done"); UVMHIST_CALLED(maphist); 1528 1529 /* 1530 * ... update the page queues. 1531 */ 1532 1533 mutex_enter(&uvm_pageqlock); 1534 if (wire_paging) { 1535 uvm_pagewire(pg); 1536 1537 /* 1538 * since the now-wired page cannot be paged out, 1539 * release its swap resources for others to use. 1540 * since an anon with no swap cannot be PG_CLEAN, 1541 * clear its clean flag now. 1542 */ 1543 1544 pg->flags &= ~(PG_CLEAN); 1545 1546 } else { 1547 uvm_pageactivate(pg); 1548 } 1549 mutex_exit(&uvm_pageqlock); 1550 1551 if (wire_paging) { 1552 uvm_anon_dropswap(anon); 1553 } 1554 } 1555 1556 /* 1557 * uvm_fault_lower: handle lower fault. 1558 * 1559 * 1. check uobj 1560 * 1.1. if null, ZFOD. 1561 * 1.2. if not null, look up unnmapped neighbor pages. 1562 * 2. for center page, check if promote. 1563 * 2.1. ZFOD always needs promotion. 1564 * 2.2. other uobjs, when entry is marked COW (usually MAP_PRIVATE vnode). 1565 * 3. if uobj is not ZFOD and page is not found, do i/o. 1566 * 4. dispatch either direct / promote fault. 1567 */ 1568 1569 static int 1570 uvm_fault_lower( 1571 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 1572 struct vm_page **pages) 1573 { 1574 #ifdef DIAGNOSTIC 1575 struct vm_amap *amap = ufi->entry->aref.ar_amap; 1576 #endif 1577 struct uvm_object *uobj = ufi->entry->object.uvm_obj; 1578 struct vm_page *uobjpage; 1579 int error; 1580 UVMHIST_FUNC("uvm_fault_lower"); UVMHIST_CALLED(maphist); 1581 1582 /* locked: maps(read), amap(if there), uobj(if !null) */ 1583 1584 /* 1585 * now, if the desired page is not shadowed by the amap and we have 1586 * a backing object that does not have a special fault routine, then 1587 * we ask (with pgo_get) the object for resident pages that we care 1588 * about and attempt to map them in. we do not let pgo_get block 1589 * (PGO_LOCKED). 1590 */ 1591 1592 if (uobj == NULL) { 1593 /* zero fill; don't care neighbor pages */ 1594 uobjpage = NULL; 1595 } else { 1596 uvm_fault_lower_lookup(ufi, flt, pages); 1597 uobjpage = pages[flt->centeridx]; 1598 } 1599 1600 /* 1601 * note that at this point we are done with any front or back pages. 1602 * we are now going to focus on the center page (i.e. the one we've 1603 * faulted on). if we have faulted on the upper (anon) layer 1604 * [i.e. case 1], then the anon we want is anons[centeridx] (we have 1605 * not touched it yet). if we have faulted on the bottom (uobj) 1606 * layer [i.e. case 2] and the page was both present and available, 1607 * then we've got a pointer to it as "uobjpage" and we've already 1608 * made it BUSY. 1609 */ 1610 1611 /* 1612 * locked: 1613 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null) 1614 */ 1615 KASSERT(amap == NULL || mutex_owned(&amap->am_l)); 1616 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock)); 1617 KASSERT(uobjpage == NULL || (uobjpage->flags & PG_BUSY) != 0); 1618 1619 /* 1620 * note that uobjpage can not be PGO_DONTCARE at this point. we now 1621 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we 1622 * have a backing object, check and see if we are going to promote 1623 * the data up to an anon during the fault. 1624 */ 1625 1626 if (uobj == NULL) { 1627 uobjpage = PGO_DONTCARE; 1628 flt->promote = true; /* always need anon here */ 1629 } else { 1630 KASSERT(uobjpage != PGO_DONTCARE); 1631 flt->promote = flt->cow_now && UVM_ET_ISCOPYONWRITE(ufi->entry); 1632 } 1633 UVMHIST_LOG(maphist, " case 2 fault: promote=%d, zfill=%d", 1634 flt->promote, (uobj == NULL), 0,0); 1635 1636 /* 1637 * if uobjpage is not null then we do not need to do I/O to get the 1638 * uobjpage. 1639 * 1640 * if uobjpage is null, then we need to unlock and ask the pager to 1641 * get the data for us. once we have the data, we need to reverify 1642 * the state the world. we are currently not holding any resources. 1643 */ 1644 1645 if (uobjpage) { 1646 /* update rusage counters */ 1647 curlwp->l_ru.ru_minflt++; 1648 } else { 1649 error = uvm_fault_lower_io(ufi, flt, &uobj, &uobjpage); 1650 if (error != 0) 1651 return error; 1652 } 1653 1654 /* 1655 * locked: 1656 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj) 1657 */ 1658 KASSERT(amap == NULL || mutex_owned(&amap->am_l)); 1659 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock)); 1660 KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0); 1661 1662 /* 1663 * notes: 1664 * - at this point uobjpage can not be NULL 1665 * - at this point uobjpage can not be PG_RELEASED (since we checked 1666 * for it above) 1667 * - at this point uobjpage could be PG_WANTED (handle later) 1668 */ 1669 1670 KASSERT(uobjpage != NULL); 1671 KASSERT(uobj == NULL || uobj == uobjpage->uobject); 1672 KASSERT(uobj == NULL || !UVM_OBJ_IS_CLEAN(uobjpage->uobject) || 1673 (uobjpage->flags & PG_CLEAN) != 0); 1674 1675 if (!flt->promote) { 1676 error = uvm_fault_lower_direct(ufi, flt, uobj, uobjpage); 1677 } else { 1678 error = uvm_fault_lower_promote(ufi, flt, uobj, uobjpage); 1679 } 1680 return error; 1681 } 1682 1683 /* 1684 * uvm_fault_lower_lookup: look up on-memory uobj pages. 1685 * 1686 * 1. get on-memory pages. 1687 * 2. if failed, give up (get only center page later). 1688 * 3. if succeeded, enter h/w mapping of neighbor pages. 1689 */ 1690 1691 static void 1692 uvm_fault_lower_lookup( 1693 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 1694 struct vm_page **pages) 1695 { 1696 struct uvm_object *uobj = ufi->entry->object.uvm_obj; 1697 int lcv, gotpages; 1698 vaddr_t currva; 1699 UVMHIST_FUNC("uvm_fault_lower_lookup"); UVMHIST_CALLED(maphist); 1700 1701 mutex_enter(&uobj->vmobjlock); 1702 /* locked: maps(read), amap(if there), uobj */ 1703 /* 1704 * the following call to pgo_get does _not_ change locking state 1705 */ 1706 1707 uvmexp.fltlget++; 1708 gotpages = flt->npages; 1709 (void) uobj->pgops->pgo_get(uobj, 1710 ufi->entry->offset + flt->startva - ufi->entry->start, 1711 pages, &gotpages, flt->centeridx, 1712 flt->access_type & MASK(ufi->entry), ufi->entry->advice, PGO_LOCKED); 1713 1714 /* 1715 * check for pages to map, if we got any 1716 */ 1717 1718 if (gotpages == 0) { 1719 pages[flt->centeridx] = NULL; 1720 return; 1721 } 1722 1723 currva = flt->startva; 1724 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) { 1725 struct vm_page *curpg; 1726 1727 curpg = pages[lcv]; 1728 if (curpg == NULL || curpg == PGO_DONTCARE) { 1729 continue; 1730 } 1731 KASSERT(curpg->uobject == uobj); 1732 1733 /* 1734 * if center page is resident and not PG_BUSY|PG_RELEASED 1735 * then pgo_get made it PG_BUSY for us and gave us a handle 1736 * to it. 1737 */ 1738 1739 if (lcv == flt->centeridx) { 1740 UVMHIST_LOG(maphist, " got uobjpage " 1741 "(0x%x) with locked get", 1742 curpg, 0,0,0); 1743 } else { 1744 bool readonly = (curpg->flags & PG_RDONLY) 1745 || (curpg->loan_count > 0) 1746 || UVM_OBJ_NEEDS_WRITEFAULT(curpg->uobject); 1747 1748 uvm_fault_lower_neighbor(ufi, flt, 1749 currva, curpg, readonly); 1750 } 1751 } 1752 pmap_update(ufi->orig_map->pmap); 1753 } 1754 1755 /* 1756 * uvm_fault_lower_neighbor: enter h/w mapping of lower neighbor page. 1757 */ 1758 1759 static void 1760 uvm_fault_lower_neighbor( 1761 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 1762 vaddr_t currva, struct vm_page *pg, bool readonly) 1763 { 1764 UVMHIST_FUNC("uvm_fault_lower_neighor"); UVMHIST_CALLED(maphist); 1765 1766 /* locked: maps(read), amap(if there), uobj */ 1767 1768 /* 1769 * calling pgo_get with PGO_LOCKED returns us pages which 1770 * are neither busy nor released, so we don't need to check 1771 * for this. we can just directly enter the pages. 1772 */ 1773 1774 mutex_enter(&uvm_pageqlock); 1775 uvm_pageenqueue(pg); 1776 mutex_exit(&uvm_pageqlock); 1777 UVMHIST_LOG(maphist, 1778 " MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x", 1779 ufi->orig_map->pmap, currva, pg, 0); 1780 uvmexp.fltnomap++; 1781 1782 /* 1783 * Since this page isn't the page that's actually faulting, 1784 * ignore pmap_enter() failures; it's not critical that we 1785 * enter these right now. 1786 * NOTE: page can't be PG_WANTED or PG_RELEASED because we've 1787 * held the lock the whole time we've had the handle. 1788 */ 1789 KASSERT((pg->flags & PG_PAGEOUT) == 0); 1790 KASSERT((pg->flags & PG_RELEASED) == 0); 1791 KASSERT((pg->flags & PG_WANTED) == 0); 1792 KASSERT(!UVM_OBJ_IS_CLEAN(pg->uobject) || 1793 (pg->flags & PG_CLEAN) != 0); 1794 pg->flags &= ~(PG_BUSY); 1795 UVM_PAGE_OWN(pg, NULL); 1796 1797 (void) pmap_enter(ufi->orig_map->pmap, currva, 1798 VM_PAGE_TO_PHYS(pg), 1799 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) : 1800 flt->enter_prot & MASK(ufi->entry), 1801 PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0)); 1802 } 1803 1804 /* 1805 * uvm_fault_lower_io: get lower page from backing store. 1806 * 1807 * 1. unlock everything, because i/o will block. 1808 * 2. call pgo_get. 1809 * 3. if failed, recover. 1810 * 4. if succeeded, relock everything and verify things. 1811 */ 1812 1813 static int 1814 uvm_fault_lower_io( 1815 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 1816 struct uvm_object **ruobj, struct vm_page **ruobjpage) 1817 { 1818 struct vm_amap * const amap = ufi->entry->aref.ar_amap; 1819 struct uvm_object *uobj = *ruobj; 1820 struct vm_page *pg; 1821 bool locked; 1822 int gotpages; 1823 int error; 1824 voff_t uoff; 1825 UVMHIST_FUNC("uvm_fault_lower_io"); UVMHIST_CALLED(maphist); 1826 1827 /* update rusage counters */ 1828 curlwp->l_ru.ru_majflt++; 1829 1830 /* locked: maps(read), amap(if there), uobj */ 1831 uvmfault_unlockall(ufi, amap, NULL, NULL); 1832 /* locked: uobj */ 1833 1834 uvmexp.fltget++; 1835 gotpages = 1; 1836 pg = NULL; 1837 uoff = (ufi->orig_rvaddr - ufi->entry->start) + ufi->entry->offset; 1838 error = uobj->pgops->pgo_get(uobj, uoff, &pg, &gotpages, 1839 0, flt->access_type & MASK(ufi->entry), ufi->entry->advice, 1840 PGO_SYNCIO); 1841 /* locked: pg(if no error) */ 1842 1843 /* 1844 * recover from I/O 1845 */ 1846 1847 if (error) { 1848 if (error == EAGAIN) { 1849 UVMHIST_LOG(maphist, 1850 " pgo_get says TRY AGAIN!",0,0,0,0); 1851 kpause("fltagain2", false, hz/2, NULL); 1852 return ERESTART; 1853 } 1854 1855 #if 0 1856 KASSERT(error != ERESTART); 1857 #else 1858 /* XXXUEBS don't re-fault? */ 1859 if (error == ERESTART) 1860 error = EIO; 1861 #endif 1862 1863 UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)", 1864 error, 0,0,0); 1865 return error; 1866 } 1867 1868 /* locked: pg */ 1869 1870 KASSERT((pg->flags & PG_BUSY) != 0); 1871 1872 mutex_enter(&uvm_pageqlock); 1873 uvm_pageactivate(pg); 1874 mutex_exit(&uvm_pageqlock); 1875 1876 /* 1877 * re-verify the state of the world by first trying to relock 1878 * the maps. always relock the object. 1879 */ 1880 1881 locked = uvmfault_relock(ufi); 1882 if (locked && amap) 1883 amap_lock(amap); 1884 1885 /* might be changed */ 1886 uobj = pg->uobject; 1887 1888 mutex_enter(&uobj->vmobjlock); 1889 1890 /* locked(locked): maps(read), amap(if !null), uobj, pg */ 1891 /* locked(!locked): uobj, pg */ 1892 1893 /* 1894 * verify that the page has not be released and re-verify 1895 * that amap slot is still free. if there is a problem, 1896 * we unlock and clean up. 1897 */ 1898 1899 if ((pg->flags & PG_RELEASED) != 0 || 1900 (locked && amap && amap_lookup(&ufi->entry->aref, 1901 ufi->orig_rvaddr - ufi->entry->start))) { 1902 if (locked) 1903 uvmfault_unlockall(ufi, amap, NULL, NULL); 1904 locked = false; 1905 } 1906 1907 /* 1908 * didn't get the lock? release the page and retry. 1909 */ 1910 1911 if (locked == false) { 1912 UVMHIST_LOG(maphist, 1913 " wasn't able to relock after fault: retry", 1914 0,0,0,0); 1915 if (pg->flags & PG_WANTED) { 1916 wakeup(pg); 1917 } 1918 if (pg->flags & PG_RELEASED) { 1919 uvmexp.fltpgrele++; 1920 uvm_pagefree(pg); 1921 mutex_exit(&uobj->vmobjlock); 1922 return ERESTART; 1923 } 1924 pg->flags &= ~(PG_BUSY|PG_WANTED); 1925 UVM_PAGE_OWN(pg, NULL); 1926 mutex_exit(&uobj->vmobjlock); 1927 return ERESTART; 1928 } 1929 1930 /* 1931 * we have the data in pg which is busy and 1932 * not released. we are holding object lock (so the page 1933 * can't be released on us). 1934 */ 1935 1936 /* locked: maps(read), amap(if !null), uobj, pg */ 1937 1938 *ruobj = uobj; 1939 *ruobjpage = pg; 1940 return 0; 1941 } 1942 1943 /* 1944 * uvm_fault_lower_direct: fault lower center page 1945 * 1946 * 1. adjust flt->enter_prot. 1947 * 2. if page is loaned, resolve. 1948 */ 1949 1950 int 1951 uvm_fault_lower_direct( 1952 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 1953 struct uvm_object *uobj, struct vm_page *uobjpage) 1954 { 1955 struct vm_page *pg; 1956 UVMHIST_FUNC("uvm_fault_lower_direct"); UVMHIST_CALLED(maphist); 1957 1958 /* 1959 * we are not promoting. if the mapping is COW ensure that we 1960 * don't give more access than we should (e.g. when doing a read 1961 * fault on a COPYONWRITE mapping we want to map the COW page in 1962 * R/O even though the entry protection could be R/W). 1963 * 1964 * set "pg" to the page we want to map in (uobjpage, usually) 1965 */ 1966 1967 uvmexp.flt_obj++; 1968 if (UVM_ET_ISCOPYONWRITE(ufi->entry) || 1969 UVM_OBJ_NEEDS_WRITEFAULT(uobjpage->uobject)) 1970 flt->enter_prot &= ~VM_PROT_WRITE; 1971 pg = uobjpage; /* map in the actual object */ 1972 1973 KASSERT(uobjpage != PGO_DONTCARE); 1974 1975 /* 1976 * we are faulting directly on the page. be careful 1977 * about writing to loaned pages... 1978 */ 1979 1980 if (uobjpage->loan_count) { 1981 uvm_fault_lower_direct_loan(ufi, flt, uobj, &pg, &uobjpage); 1982 } 1983 KASSERT(pg == uobjpage); 1984 1985 return uvm_fault_lower_enter(ufi, flt, uobj, NULL, pg, uobjpage); 1986 } 1987 1988 /* 1989 * uvm_fault_lower_direct_loan: resolve loaned page. 1990 * 1991 * 1. if not cow'ing, adjust flt->enter_prot. 1992 * 2. if cow'ing, break loan. 1993 */ 1994 1995 static int 1996 uvm_fault_lower_direct_loan( 1997 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 1998 struct uvm_object *uobj, struct vm_page **rpg, 1999 struct vm_page **ruobjpage) 2000 { 2001 struct vm_amap * const amap = ufi->entry->aref.ar_amap; 2002 struct vm_page *pg; 2003 struct vm_page *uobjpage = *ruobjpage; 2004 UVMHIST_FUNC("uvm_fault_lower_direct_loan"); UVMHIST_CALLED(maphist); 2005 2006 if (!flt->cow_now) { 2007 /* read fault: cap the protection at readonly */ 2008 /* cap! */ 2009 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE; 2010 } else { 2011 /* write fault: must break the loan here */ 2012 2013 pg = uvm_loanbreak(uobjpage); 2014 if (pg == NULL) { 2015 2016 /* 2017 * drop ownership of page, it can't be released 2018 */ 2019 2020 if (uobjpage->flags & PG_WANTED) 2021 wakeup(uobjpage); 2022 uobjpage->flags &= ~(PG_BUSY|PG_WANTED); 2023 UVM_PAGE_OWN(uobjpage, NULL); 2024 2025 uvmfault_unlockall(ufi, amap, uobj, NULL); 2026 UVMHIST_LOG(maphist, 2027 " out of RAM breaking loan, waiting", 2028 0,0,0,0); 2029 uvmexp.fltnoram++; 2030 uvm_wait("flt_noram4"); 2031 return ERESTART; 2032 } 2033 *rpg = pg; 2034 *ruobjpage = pg; 2035 } 2036 return 0; 2037 } 2038 2039 /* 2040 * uvm_fault_lower_promote: promote lower page. 2041 * 2042 * 1. call uvmfault_promote. 2043 * 2. fill in data. 2044 * 3. if not ZFOD, dispose old page. 2045 */ 2046 2047 int 2048 uvm_fault_lower_promote( 2049 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt, 2050 struct uvm_object *uobj, struct vm_page *uobjpage) 2051 { 2052 struct vm_amap * const amap = ufi->entry->aref.ar_amap; 2053 struct vm_anon *anon; 2054 struct vm_page *pg; 2055 int error; 2056 UVMHIST_FUNC("uvm_fault_lower_promote"); UVMHIST_CALLED(maphist); 2057 2058 /* 2059 * if we are going to promote the data to an anon we 2060 * allocate a blank anon here and plug it into our amap. 2061 */ 2062 #if DIAGNOSTIC 2063 if (amap == NULL) 2064 panic("uvm_fault: want to promote data, but no anon"); 2065 #endif 2066 error = uvmfault_promote(ufi, NULL, uobjpage, 2067 &anon, &flt->anon_spare); 2068 switch (error) { 2069 case 0: 2070 break; 2071 case ERESTART: 2072 return ERESTART; 2073 default: 2074 return error; 2075 } 2076 2077 pg = anon->an_page; 2078 2079 /* 2080 * fill in the data 2081 */ 2082 2083 if (uobjpage != PGO_DONTCARE) { 2084 uvmexp.flt_prcopy++; 2085 2086 /* 2087 * promote to shared amap? make sure all sharing 2088 * procs see it 2089 */ 2090 2091 if ((amap_flags(amap) & AMAP_SHARED) != 0) { 2092 pmap_page_protect(uobjpage, VM_PROT_NONE); 2093 /* 2094 * XXX: PAGE MIGHT BE WIRED! 2095 */ 2096 } 2097 2098 /* 2099 * dispose of uobjpage. it can't be PG_RELEASED 2100 * since we still hold the object lock. 2101 * drop handle to uobj as well. 2102 */ 2103 2104 if (uobjpage->flags & PG_WANTED) 2105 /* still have the obj lock */ 2106 wakeup(uobjpage); 2107 uobjpage->flags &= ~(PG_BUSY|PG_WANTED); 2108 UVM_PAGE_OWN(uobjpage, NULL); 2109 mutex_exit(&uobj->vmobjlock); 2110 uobj = NULL; 2111 2112 UVMHIST_LOG(maphist, 2113 " promote uobjpage 0x%x to anon/page 0x%x/0x%x", 2114 uobjpage, anon, pg, 0); 2115 2116 } else { 2117 uvmexp.flt_przero++; 2118 2119 /* 2120 * Page is zero'd and marked dirty by 2121 * uvmfault_promote(). 2122 */ 2123 2124 UVMHIST_LOG(maphist," zero fill anon/page 0x%x/0%x", 2125 anon, pg, 0, 0); 2126 } 2127 2128 return uvm_fault_lower_enter(ufi, flt, uobj, anon, pg, uobjpage); 2129 } 2130 2131 /* 2132 * uvm_fault_lower_enter: enter h/w mapping of lower page. 2133 */ 2134 2135 int 2136 uvm_fault_lower_enter( 2137 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 2138 struct uvm_object *uobj, 2139 struct vm_anon *anon, struct vm_page *pg, struct vm_page *uobjpage) 2140 { 2141 struct vm_amap * const amap = ufi->entry->aref.ar_amap; 2142 int error; 2143 UVMHIST_FUNC("uvm_fault_lower_enter"); UVMHIST_CALLED(maphist); 2144 2145 /* 2146 * locked: 2147 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj), 2148 * anon(if !null), pg(if anon) 2149 * 2150 * note: pg is either the uobjpage or the new page in the new anon 2151 */ 2152 KASSERT(amap == NULL || mutex_owned(&amap->am_l)); 2153 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock)); 2154 KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0); 2155 KASSERT(anon == NULL || mutex_owned(&anon->an_lock)); 2156 KASSERT((pg->flags & PG_BUSY) != 0); 2157 2158 /* 2159 * all resources are present. we can now map it in and free our 2160 * resources. 2161 */ 2162 2163 UVMHIST_LOG(maphist, 2164 " MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d", 2165 ufi->orig_map->pmap, ufi->orig_rvaddr, pg, flt->promote); 2166 KASSERT((flt->access_type & VM_PROT_WRITE) == 0 || 2167 (pg->flags & PG_RDONLY) == 0); 2168 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr, 2169 VM_PAGE_TO_PHYS(pg), 2170 (pg->flags & PG_RDONLY) != 0 ? 2171 flt->enter_prot & ~VM_PROT_WRITE : flt->enter_prot, 2172 flt->access_type | PMAP_CANFAIL | 2173 (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) { 2174 2175 /* 2176 * No need to undo what we did; we can simply think of 2177 * this as the pmap throwing away the mapping information. 2178 * 2179 * We do, however, have to go through the ReFault path, 2180 * as the map may change while we're asleep. 2181 */ 2182 2183 if (pg->flags & PG_WANTED) 2184 wakeup(pg); 2185 2186 /* 2187 * note that pg can't be PG_RELEASED since we did not drop 2188 * the object lock since the last time we checked. 2189 */ 2190 KASSERT((pg->flags & PG_RELEASED) == 0); 2191 2192 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED); 2193 UVM_PAGE_OWN(pg, NULL); 2194 2195 uvmfault_unlockall(ufi, amap, uobj, anon); 2196 if (!uvm_reclaimable()) { 2197 UVMHIST_LOG(maphist, 2198 "<- failed. out of VM",0,0,0,0); 2199 /* XXX instrumentation */ 2200 error = ENOMEM; 2201 return error; 2202 } 2203 /* XXX instrumentation */ 2204 uvm_wait("flt_pmfail2"); 2205 return ERESTART; 2206 } 2207 2208 uvm_fault_lower_done(ufi, flt, uobj, pg); 2209 2210 /* 2211 * note that pg can't be PG_RELEASED since we did not drop the object 2212 * lock since the last time we checked. 2213 */ 2214 KASSERT((pg->flags & PG_RELEASED) == 0); 2215 if (pg->flags & PG_WANTED) 2216 wakeup(pg); 2217 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED); 2218 UVM_PAGE_OWN(pg, NULL); 2219 2220 pmap_update(ufi->orig_map->pmap); 2221 uvmfault_unlockall(ufi, amap, uobj, anon); 2222 2223 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0); 2224 return 0; 2225 } 2226 2227 /* 2228 * uvm_fault_lower_done: queue lower center page. 2229 */ 2230 2231 void 2232 uvm_fault_lower_done( 2233 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt, 2234 struct uvm_object *uobj, struct vm_page *pg) 2235 { 2236 bool dropswap = false; 2237 2238 UVMHIST_FUNC("uvm_fault_lower_done"); UVMHIST_CALLED(maphist); 2239 2240 mutex_enter(&uvm_pageqlock); 2241 if (flt->wire_paging) { 2242 uvm_pagewire(pg); 2243 if (pg->pqflags & PQ_AOBJ) { 2244 2245 /* 2246 * since the now-wired page cannot be paged out, 2247 * release its swap resources for others to use. 2248 * since an aobj page with no swap cannot be PG_CLEAN, 2249 * clear its clean flag now. 2250 */ 2251 2252 KASSERT(uobj != NULL); 2253 pg->flags &= ~(PG_CLEAN); 2254 dropswap = true; 2255 } 2256 } else { 2257 uvm_pageactivate(pg); 2258 } 2259 mutex_exit(&uvm_pageqlock); 2260 2261 if (dropswap) { 2262 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT); 2263 } 2264 } 2265 2266 2267 /* 2268 * uvm_fault_wire: wire down a range of virtual addresses in a map. 2269 * 2270 * => map may be read-locked by caller, but MUST NOT be write-locked. 2271 * => if map is read-locked, any operations which may cause map to 2272 * be write-locked in uvm_fault() must be taken care of by 2273 * the caller. See uvm_map_pageable(). 2274 */ 2275 2276 int 2277 uvm_fault_wire(struct vm_map *map, vaddr_t start, vaddr_t end, 2278 vm_prot_t access_type, int maxprot) 2279 { 2280 vaddr_t va; 2281 int error; 2282 2283 /* 2284 * now fault it in a page at a time. if the fault fails then we have 2285 * to undo what we have done. note that in uvm_fault VM_PROT_NONE 2286 * is replaced with the max protection if fault_type is VM_FAULT_WIRE. 2287 */ 2288 2289 /* 2290 * XXX work around overflowing a vaddr_t. this prevents us from 2291 * wiring the last page in the address space, though. 2292 */ 2293 if (start > end) { 2294 return EFAULT; 2295 } 2296 2297 for (va = start; va < end; va += PAGE_SIZE) { 2298 error = uvm_fault_internal(map, va, access_type, 2299 (maxprot ? UVM_FAULT_MAXPROT : 0) | UVM_FAULT_WIRE); 2300 if (error) { 2301 if (va != start) { 2302 uvm_fault_unwire(map, start, va); 2303 } 2304 return error; 2305 } 2306 } 2307 return 0; 2308 } 2309 2310 /* 2311 * uvm_fault_unwire(): unwire range of virtual space. 2312 */ 2313 2314 void 2315 uvm_fault_unwire(struct vm_map *map, vaddr_t start, vaddr_t end) 2316 { 2317 vm_map_lock_read(map); 2318 uvm_fault_unwire_locked(map, start, end); 2319 vm_map_unlock_read(map); 2320 } 2321 2322 /* 2323 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire(). 2324 * 2325 * => map must be at least read-locked. 2326 */ 2327 2328 void 2329 uvm_fault_unwire_locked(struct vm_map *map, vaddr_t start, vaddr_t end) 2330 { 2331 struct vm_map_entry *entry; 2332 pmap_t pmap = vm_map_pmap(map); 2333 vaddr_t va; 2334 paddr_t pa; 2335 struct vm_page *pg; 2336 2337 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0); 2338 2339 /* 2340 * we assume that the area we are unwiring has actually been wired 2341 * in the first place. this means that we should be able to extract 2342 * the PAs from the pmap. we also lock out the page daemon so that 2343 * we can call uvm_pageunwire. 2344 */ 2345 2346 mutex_enter(&uvm_pageqlock); 2347 2348 /* 2349 * find the beginning map entry for the region. 2350 */ 2351 2352 KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map)); 2353 if (uvm_map_lookup_entry(map, start, &entry) == false) 2354 panic("uvm_fault_unwire_locked: address not in map"); 2355 2356 for (va = start; va < end; va += PAGE_SIZE) { 2357 if (pmap_extract(pmap, va, &pa) == false) 2358 continue; 2359 2360 /* 2361 * find the map entry for the current address. 2362 */ 2363 2364 KASSERT(va >= entry->start); 2365 while (va >= entry->end) { 2366 KASSERT(entry->next != &map->header && 2367 entry->next->start <= entry->end); 2368 entry = entry->next; 2369 } 2370 2371 /* 2372 * if the entry is no longer wired, tell the pmap. 2373 */ 2374 2375 if (VM_MAPENT_ISWIRED(entry) == 0) 2376 pmap_unwire(pmap, va); 2377 2378 pg = PHYS_TO_VM_PAGE(pa); 2379 if (pg) 2380 uvm_pageunwire(pg); 2381 } 2382 2383 mutex_exit(&uvm_pageqlock); 2384 } 2385