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