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