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