1 /* $OpenBSD: uvm_fault.c,v 1.97 2019/12/08 12:37:45 mpi Exp $ */ 2 /* $NetBSD: uvm_fault.c,v 1.51 2000/08/06 00:22:53 thorpej Exp $ */ 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp 29 */ 30 31 /* 32 * uvm_fault.c: fault handler 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/proc.h> 39 #include <sys/malloc.h> 40 #include <sys/mman.h> 41 42 #include <uvm/uvm.h> 43 44 /* 45 * 46 * a word on page faults: 47 * 48 * types of page faults we handle: 49 * 50 * CASE 1: upper layer faults CASE 2: lower layer faults 51 * 52 * CASE 1A CASE 1B CASE 2A CASE 2B 53 * read/write1 write>1 read/write +-cow_write/zero 54 * | | | | 55 * +--|--+ +--|--+ +-----+ + | + | +-----+ 56 * amap | V | | ----------->new| | | | ^ | 57 * +-----+ +-----+ +-----+ + | + | +--|--+ 58 * | | | 59 * +-----+ +-----+ +--|--+ | +--|--+ 60 * uobj | d/c | | d/c | | V | +----| | 61 * +-----+ +-----+ +-----+ +-----+ 62 * 63 * d/c = don't care 64 * 65 * case [0]: layerless fault 66 * no amap or uobj is present. this is an error. 67 * 68 * case [1]: upper layer fault [anon active] 69 * 1A: [read] or [write with anon->an_ref == 1] 70 * I/O takes place in top level anon and uobj is not touched. 71 * 1B: [write with anon->an_ref > 1] 72 * new anon is alloc'd and data is copied off ["COW"] 73 * 74 * case [2]: lower layer fault [uobj] 75 * 2A: [read on non-NULL uobj] or [write to non-copy_on_write area] 76 * I/O takes place directly in object. 77 * 2B: [write to copy_on_write] or [read on NULL uobj] 78 * data is "promoted" from uobj to a new anon. 79 * if uobj is null, then we zero fill. 80 * 81 * we follow the standard UVM locking protocol ordering: 82 * 83 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ) 84 * we hold a PG_BUSY page if we unlock for I/O 85 * 86 * 87 * the code is structured as follows: 88 * 89 * - init the "IN" params in the ufi structure 90 * ReFault: 91 * - do lookups [locks maps], check protection, handle needs_copy 92 * - check for case 0 fault (error) 93 * - establish "range" of fault 94 * - if we have an amap lock it and extract the anons 95 * - if sequential advice deactivate pages behind us 96 * - at the same time check pmap for unmapped areas and anon for pages 97 * that we could map in (and do map it if found) 98 * - check object for resident pages that we could map in 99 * - if (case 2) goto Case2 100 * - >>> handle case 1 101 * - ensure source anon is resident in RAM 102 * - if case 1B alloc new anon and copy from source 103 * - map the correct page in 104 * Case2: 105 * - >>> handle case 2 106 * - ensure source page is resident (if uobj) 107 * - if case 2B alloc new anon and copy from source (could be zero 108 * fill if uobj == NULL) 109 * - map the correct page in 110 * - done! 111 * 112 * note on paging: 113 * if we have to do I/O we place a PG_BUSY page in the correct object, 114 * unlock everything, and do the I/O. when I/O is done we must reverify 115 * the state of the world before assuming that our data structures are 116 * valid. [because mappings could change while the map is unlocked] 117 * 118 * alternative 1: unbusy the page in question and restart the page fault 119 * from the top (ReFault). this is easy but does not take advantage 120 * of the information that we already have from our previous lookup, 121 * although it is possible that the "hints" in the vm_map will help here. 122 * 123 * alternative 2: the system already keeps track of a "version" number of 124 * a map. [i.e. every time you write-lock a map (e.g. to change a 125 * mapping) you bump the version number up by one...] so, we can save 126 * the version number of the map before we release the lock and start I/O. 127 * then when I/O is done we can relock and check the version numbers 128 * to see if anything changed. this might save us some over 1 because 129 * we don't have to unbusy the page and may be less compares(?). 130 * 131 * alternative 3: put in backpointers or a way to "hold" part of a map 132 * in place while I/O is in progress. this could be complex to 133 * implement (especially with structures like amap that can be referenced 134 * by multiple map entries, and figuring out what should wait could be 135 * complex as well...). 136 * 137 * given that we are not currently multiprocessor or multithreaded we might 138 * as well choose alternative 2 now. maybe alternative 3 would be useful 139 * in the future. XXX keep in mind for future consideration//rechecking. 140 */ 141 142 /* 143 * local data structures 144 */ 145 struct uvm_advice { 146 int nback; 147 int nforw; 148 }; 149 150 /* 151 * page range array: set up in uvmfault_init(). 152 */ 153 static struct uvm_advice uvmadvice[MADV_MASK + 1]; 154 155 #define UVM_MAXRANGE 16 /* must be max() of nback+nforw+1 */ 156 157 /* 158 * private prototypes 159 */ 160 static void uvmfault_amapcopy(struct uvm_faultinfo *); 161 static __inline void uvmfault_anonflush(struct vm_anon **, int); 162 void uvmfault_unlockmaps(struct uvm_faultinfo *, boolean_t); 163 void uvmfault_update_stats(struct uvm_faultinfo *); 164 165 /* 166 * inline functions 167 */ 168 /* 169 * uvmfault_anonflush: try and deactivate pages in specified anons 170 * 171 * => does not have to deactivate page if it is busy 172 */ 173 static __inline void 174 uvmfault_anonflush(struct vm_anon **anons, int n) 175 { 176 int lcv; 177 struct vm_page *pg; 178 179 for (lcv = 0 ; lcv < n ; lcv++) { 180 if (anons[lcv] == NULL) 181 continue; 182 pg = anons[lcv]->an_page; 183 if (pg && (pg->pg_flags & PG_BUSY) == 0) { 184 uvm_lock_pageq(); 185 if (pg->wire_count == 0) { 186 pmap_page_protect(pg, PROT_NONE); 187 uvm_pagedeactivate(pg); 188 } 189 uvm_unlock_pageq(); 190 } 191 } 192 } 193 194 /* 195 * normal functions 196 */ 197 /* 198 * uvmfault_init: compute proper values for the uvmadvice[] array. 199 */ 200 void 201 uvmfault_init(void) 202 { 203 int npages; 204 205 npages = atop(16384); 206 if (npages > 0) { 207 KASSERT(npages <= UVM_MAXRANGE / 2); 208 uvmadvice[MADV_NORMAL].nforw = npages; 209 uvmadvice[MADV_NORMAL].nback = npages - 1; 210 } 211 212 npages = atop(32768); 213 if (npages > 0) { 214 KASSERT(npages <= UVM_MAXRANGE / 2); 215 uvmadvice[MADV_SEQUENTIAL].nforw = npages - 1; 216 uvmadvice[MADV_SEQUENTIAL].nback = npages; 217 } 218 } 219 220 /* 221 * uvmfault_amapcopy: clear "needs_copy" in a map. 222 * 223 * => if we are out of RAM we sleep (waiting for more) 224 */ 225 static void 226 uvmfault_amapcopy(struct uvm_faultinfo *ufi) 227 { 228 229 /* while we haven't done the job */ 230 while (1) { 231 /* no mapping? give up. */ 232 if (uvmfault_lookup(ufi, TRUE) == FALSE) 233 return; 234 235 /* copy if needed. */ 236 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) 237 amap_copy(ufi->map, ufi->entry, M_NOWAIT, 238 UVM_ET_ISSTACK(ufi->entry) ? FALSE : TRUE, 239 ufi->orig_rvaddr, ufi->orig_rvaddr + 1); 240 241 /* didn't work? must be out of RAM. sleep. */ 242 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) { 243 uvmfault_unlockmaps(ufi, TRUE); 244 uvm_wait("fltamapcopy"); 245 continue; 246 } 247 248 /* got it! */ 249 uvmfault_unlockmaps(ufi, TRUE); 250 return; 251 } 252 /*NOTREACHED*/ 253 } 254 255 /* 256 * uvmfault_anonget: get data in an anon into a non-busy, non-released 257 * page in that anon. 258 * 259 * => we don't move the page on the queues [gets moved later] 260 * => if we allocate a new page [we_own], it gets put on the queues. 261 * either way, the result is that the page is on the queues at return time 262 */ 263 int 264 uvmfault_anonget(struct uvm_faultinfo *ufi, struct vm_amap *amap, 265 struct vm_anon *anon) 266 { 267 boolean_t we_own; /* we own anon's page? */ 268 boolean_t locked; /* did we relock? */ 269 struct vm_page *pg; 270 int result; 271 272 result = 0; /* XXX shut up gcc */ 273 uvmexp.fltanget++; 274 /* bump rusage counters */ 275 if (anon->an_page) 276 curproc->p_ru.ru_minflt++; 277 else 278 curproc->p_ru.ru_majflt++; 279 280 /* loop until we get it, or fail. */ 281 while (1) { 282 we_own = FALSE; /* TRUE if we set PG_BUSY on a page */ 283 pg = anon->an_page; 284 285 /* page there? make sure it is not busy/released. */ 286 if (pg) { 287 KASSERT(pg->pg_flags & PQ_ANON); 288 KASSERT(pg->uanon == anon); 289 290 /* 291 * if the page is busy, we drop all the locks and 292 * try again. 293 */ 294 if ((pg->pg_flags & (PG_BUSY|PG_RELEASED)) == 0) 295 return (VM_PAGER_OK); 296 atomic_setbits_int(&pg->pg_flags, PG_WANTED); 297 uvmexp.fltpgwait++; 298 299 /* 300 * the last unlock must be an atomic unlock+wait on 301 * the owner of page 302 */ 303 uvmfault_unlockall(ufi, amap, NULL, NULL); 304 tsleep_nsec(pg, PVM, "anonget2", INFSLP); 305 /* ready to relock and try again */ 306 } else { 307 /* no page, we must try and bring it in. */ 308 pg = uvm_pagealloc(NULL, 0, anon, 0); 309 310 if (pg == NULL) { /* out of RAM. */ 311 uvmfault_unlockall(ufi, amap, NULL, anon); 312 uvmexp.fltnoram++; 313 uvm_wait("flt_noram1"); 314 /* ready to relock and try again */ 315 } else { 316 /* we set the PG_BUSY bit */ 317 we_own = TRUE; 318 uvmfault_unlockall(ufi, amap, NULL, anon); 319 320 /* 321 * we are passing a PG_BUSY+PG_FAKE+PG_CLEAN 322 * page into the uvm_swap_get function with 323 * all data structures unlocked. note that 324 * it is ok to read an_swslot here because 325 * we hold PG_BUSY on the page. 326 */ 327 uvmexp.pageins++; 328 result = uvm_swap_get(pg, anon->an_swslot, 329 PGO_SYNCIO); 330 331 /* 332 * we clean up after the i/o below in the 333 * "we_own" case 334 */ 335 /* ready to relock and try again */ 336 } 337 } 338 339 /* now relock and try again */ 340 locked = uvmfault_relock(ufi); 341 342 /* 343 * if we own the page (i.e. we set PG_BUSY), then we need 344 * to clean up after the I/O. there are three cases to 345 * consider: 346 * [1] page released during I/O: free anon and ReFault. 347 * [2] I/O not OK. free the page and cause the fault 348 * to fail. 349 * [3] I/O OK! activate the page and sync with the 350 * non-we_own case (i.e. drop anon lock if not locked). 351 */ 352 if (we_own) { 353 if (pg->pg_flags & PG_WANTED) { 354 wakeup(pg); 355 } 356 /* un-busy! */ 357 atomic_clearbits_int(&pg->pg_flags, 358 PG_WANTED|PG_BUSY|PG_FAKE); 359 UVM_PAGE_OWN(pg, NULL); 360 361 /* 362 * if we were RELEASED during I/O, then our anon is 363 * no longer part of an amap. we need to free the 364 * anon and try again. 365 */ 366 if (pg->pg_flags & PG_RELEASED) { 367 pmap_page_protect(pg, PROT_NONE); 368 uvm_anfree(anon); /* frees page for us */ 369 if (locked) 370 uvmfault_unlockall(ufi, amap, NULL, 371 NULL); 372 uvmexp.fltpgrele++; 373 return (VM_PAGER_REFAULT); /* refault! */ 374 } 375 376 if (result != VM_PAGER_OK) { 377 KASSERT(result != VM_PAGER_PEND); 378 379 /* remove page from anon */ 380 anon->an_page = NULL; 381 382 /* 383 * remove the swap slot from the anon 384 * and mark the anon as having no real slot. 385 * don't free the swap slot, thus preventing 386 * it from being used again. 387 */ 388 uvm_swap_markbad(anon->an_swslot, 1); 389 anon->an_swslot = SWSLOT_BAD; 390 391 /* 392 * note: page was never !PG_BUSY, so it 393 * can't be mapped and thus no need to 394 * pmap_page_protect it... 395 */ 396 uvm_lock_pageq(); 397 uvm_pagefree(pg); 398 uvm_unlock_pageq(); 399 400 if (locked) 401 uvmfault_unlockall(ufi, amap, NULL, 402 anon); 403 return (VM_PAGER_ERROR); 404 } 405 406 /* 407 * must be OK, clear modify (already PG_CLEAN) 408 * and activate 409 */ 410 pmap_clear_modify(pg); 411 uvm_lock_pageq(); 412 uvm_pageactivate(pg); 413 uvm_unlock_pageq(); 414 } 415 416 /* we were not able to relock. restart fault. */ 417 if (!locked) 418 return (VM_PAGER_REFAULT); 419 420 /* verify no one touched the amap and moved the anon on us. */ 421 if (ufi != NULL && 422 amap_lookup(&ufi->entry->aref, 423 ufi->orig_rvaddr - ufi->entry->start) != anon) { 424 425 uvmfault_unlockall(ufi, amap, NULL, anon); 426 return (VM_PAGER_REFAULT); 427 } 428 429 /* try it again! */ 430 uvmexp.fltanretry++; 431 continue; 432 433 } /* while (1) */ 434 /*NOTREACHED*/ 435 } 436 437 /* 438 * Update statistics after fault resolution. 439 * - maxrss 440 */ 441 void 442 uvmfault_update_stats(struct uvm_faultinfo *ufi) 443 { 444 struct vm_map *map; 445 struct proc *p; 446 vsize_t res; 447 448 map = ufi->orig_map; 449 450 /* 451 * If this is a nested pmap (eg, a virtual machine pmap managed 452 * by vmm(4) on amd64/i386), don't do any updating, just return. 453 * 454 * pmap_nested() on other archs is #defined to 0, so this is a 455 * no-op. 456 */ 457 if (pmap_nested(map->pmap)) 458 return; 459 460 /* Update the maxrss for the process. */ 461 if (map->flags & VM_MAP_ISVMSPACE) { 462 p = curproc; 463 KASSERT(p != NULL && &p->p_vmspace->vm_map == map); 464 465 res = pmap_resident_count(map->pmap); 466 /* Convert res from pages to kilobytes. */ 467 res <<= (PAGE_SHIFT - 10); 468 469 if (p->p_ru.ru_maxrss < res) 470 p->p_ru.ru_maxrss = res; 471 } 472 } 473 474 /* 475 * F A U L T - m a i n e n t r y p o i n t 476 */ 477 478 /* 479 * uvm_fault: page fault handler 480 * 481 * => called from MD code to resolve a page fault 482 * => VM data structures usually should be unlocked. however, it is 483 * possible to call here with the main map locked if the caller 484 * gets a write lock, sets it recursive, and then calls us (c.f. 485 * uvm_map_pageable). this should be avoided because it keeps 486 * the map locked off during I/O. 487 */ 488 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \ 489 ~PROT_WRITE : PROT_MASK) 490 int 491 uvm_fault(vm_map_t orig_map, vaddr_t vaddr, vm_fault_t fault_type, 492 vm_prot_t access_type) 493 { 494 struct uvm_faultinfo ufi; 495 vm_prot_t enter_prot; 496 boolean_t wired, narrow, promote, locked, shadowed; 497 int npages, nback, nforw, centeridx, result, lcv, gotpages, ret; 498 vaddr_t startva, currva; 499 voff_t uoff; 500 paddr_t pa, pa_flags; 501 struct vm_amap *amap; 502 struct uvm_object *uobj; 503 struct vm_anon *anons_store[UVM_MAXRANGE], **anons, *anon, *oanon; 504 struct vm_page *pages[UVM_MAXRANGE], *pg, *uobjpage; 505 506 anon = NULL; 507 pg = NULL; 508 509 uvmexp.faults++; /* XXX: locking? */ 510 511 /* init the IN parameters in the ufi */ 512 ufi.orig_map = orig_map; 513 ufi.orig_rvaddr = trunc_page(vaddr); 514 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */ 515 if (fault_type == VM_FAULT_WIRE) 516 narrow = TRUE; /* don't look for neighborhood 517 * pages on wire */ 518 else 519 narrow = FALSE; /* normal fault */ 520 521 /* "goto ReFault" means restart the page fault from ground zero. */ 522 ReFault: 523 /* lookup and lock the maps */ 524 if (uvmfault_lookup(&ufi, FALSE) == FALSE) { 525 return (EFAULT); 526 } 527 528 #ifdef DIAGNOSTIC 529 if ((ufi.map->flags & VM_MAP_PAGEABLE) == 0) 530 panic("uvm_fault: fault on non-pageable map (%p, 0x%lx)", 531 ufi.map, vaddr); 532 #endif 533 534 /* check protection */ 535 if ((ufi.entry->protection & access_type) != access_type) { 536 uvmfault_unlockmaps(&ufi, FALSE); 537 return (EACCES); 538 } 539 540 /* 541 * "enter_prot" is the protection we want to enter the page in at. 542 * for certain pages (e.g. copy-on-write pages) this protection can 543 * be more strict than ufi.entry->protection. "wired" means either 544 * the entry is wired or we are fault-wiring the pg. 545 */ 546 547 enter_prot = ufi.entry->protection; 548 pa_flags = UVM_ET_ISWC(ufi.entry) ? PMAP_WC : 0; 549 wired = VM_MAPENT_ISWIRED(ufi.entry) || (fault_type == VM_FAULT_WIRE); 550 if (wired) 551 access_type = enter_prot; /* full access for wired */ 552 553 /* handle "needs_copy" case. */ 554 if (UVM_ET_ISNEEDSCOPY(ufi.entry)) { 555 if ((access_type & PROT_WRITE) || 556 (ufi.entry->object.uvm_obj == NULL)) { 557 /* need to clear */ 558 uvmfault_unlockmaps(&ufi, FALSE); 559 uvmfault_amapcopy(&ufi); 560 uvmexp.fltamcopy++; 561 goto ReFault; 562 } else { 563 /* 564 * ensure that we pmap_enter page R/O since 565 * needs_copy is still true 566 */ 567 enter_prot &= ~PROT_WRITE; 568 } 569 } 570 571 /* identify the players */ 572 amap = ufi.entry->aref.ar_amap; /* top layer */ 573 uobj = ufi.entry->object.uvm_obj; /* bottom layer */ 574 575 /* 576 * check for a case 0 fault. if nothing backing the entry then 577 * error now. 578 */ 579 if (amap == NULL && uobj == NULL) { 580 uvmfault_unlockmaps(&ufi, FALSE); 581 return (EFAULT); 582 } 583 584 /* 585 * establish range of interest based on advice from mapper 586 * and then clip to fit map entry. note that we only want 587 * to do this the first time through the fault. if we 588 * ReFault we will disable this by setting "narrow" to true. 589 */ 590 if (narrow == FALSE) { 591 592 /* wide fault (!narrow) */ 593 nback = min(uvmadvice[ufi.entry->advice].nback, 594 (ufi.orig_rvaddr - ufi.entry->start) >> PAGE_SHIFT); 595 startva = ufi.orig_rvaddr - ((vsize_t)nback << PAGE_SHIFT); 596 nforw = min(uvmadvice[ufi.entry->advice].nforw, 597 ((ufi.entry->end - ufi.orig_rvaddr) >> 598 PAGE_SHIFT) - 1); 599 /* 600 * note: "-1" because we don't want to count the 601 * faulting page as forw 602 */ 603 npages = nback + nforw + 1; 604 centeridx = nback; 605 606 narrow = TRUE; /* ensure only once per-fault */ 607 } else { 608 /* narrow fault! */ 609 nback = nforw = 0; 610 startva = ufi.orig_rvaddr; 611 npages = 1; 612 centeridx = 0; 613 } 614 615 /* if we've got an amap, extract current anons. */ 616 if (amap) { 617 anons = anons_store; 618 amap_lookups(&ufi.entry->aref, startva - ufi.entry->start, 619 anons, npages); 620 } else { 621 anons = NULL; /* to be safe */ 622 } 623 624 /* 625 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages 626 * now and then forget about them (for the rest of the fault). 627 */ 628 if (ufi.entry->advice == MADV_SEQUENTIAL && nback != 0) { 629 /* flush back-page anons? */ 630 if (amap) 631 uvmfault_anonflush(anons, nback); 632 633 /* flush object? */ 634 if (uobj) { 635 uoff = (startva - ufi.entry->start) + ufi.entry->offset; 636 (void) uobj->pgops->pgo_flush(uobj, uoff, uoff + 637 ((vsize_t)nback << PAGE_SHIFT), PGO_DEACTIVATE); 638 } 639 640 /* now forget about the backpages */ 641 if (amap) 642 anons += nback; 643 startva += ((vsize_t)nback << PAGE_SHIFT); 644 npages -= nback; 645 centeridx = 0; 646 } 647 648 /* 649 * map in the backpages and frontpages we found in the amap in hopes 650 * of preventing future faults. we also init the pages[] array as 651 * we go. 652 */ 653 currva = startva; 654 shadowed = FALSE; 655 for (lcv = 0 ; lcv < npages ; lcv++, currva += PAGE_SIZE) { 656 /* 657 * dont play with VAs that are already mapped 658 * except for center) 659 */ 660 if (lcv != centeridx && 661 pmap_extract(ufi.orig_map->pmap, currva, &pa)) { 662 pages[lcv] = PGO_DONTCARE; 663 continue; 664 } 665 666 /* unmapped or center page. check if any anon at this level. */ 667 if (amap == NULL || anons[lcv] == NULL) { 668 pages[lcv] = NULL; 669 continue; 670 } 671 672 /* check for present page and map if possible. re-activate it. */ 673 pages[lcv] = PGO_DONTCARE; 674 if (lcv == centeridx) { /* save center for later! */ 675 shadowed = TRUE; 676 continue; 677 } 678 anon = anons[lcv]; 679 if (anon->an_page && 680 (anon->an_page->pg_flags & (PG_RELEASED|PG_BUSY)) == 0) { 681 uvm_lock_pageq(); 682 uvm_pageactivate(anon->an_page); /* reactivate */ 683 uvm_unlock_pageq(); 684 uvmexp.fltnamap++; 685 686 /* 687 * Since this isn't the page that's actually faulting, 688 * ignore pmap_enter() failures; it's not critical 689 * that we enter these right now. 690 */ 691 (void) pmap_enter(ufi.orig_map->pmap, currva, 692 VM_PAGE_TO_PHYS(anon->an_page) | pa_flags, 693 (anon->an_ref > 1) ? (enter_prot & ~PROT_WRITE) : 694 enter_prot, 695 PMAP_CANFAIL | 696 (VM_MAPENT_ISWIRED(ufi.entry) ? PMAP_WIRED : 0)); 697 } 698 } 699 if (npages > 1) 700 pmap_update(ufi.orig_map->pmap); 701 702 /* (shadowed == TRUE) if there is an anon at the faulting address */ 703 /* 704 * note that if we are really short of RAM we could sleep in the above 705 * call to pmap_enter. bad? 706 * 707 * XXX Actually, that is bad; pmap_enter() should just fail in that 708 * XXX case. --thorpej 709 */ 710 /* 711 * if the desired page is not shadowed by the amap and we have a 712 * backing object, then we check to see if the backing object would 713 * prefer to handle the fault itself (rather than letting us do it 714 * with the usual pgo_get hook). the backing object signals this by 715 * providing a pgo_fault routine. 716 */ 717 if (uobj && shadowed == FALSE && uobj->pgops->pgo_fault != NULL) { 718 result = uobj->pgops->pgo_fault(&ufi, startva, pages, npages, 719 centeridx, fault_type, access_type, 720 PGO_LOCKED); 721 722 if (result == VM_PAGER_OK) 723 return (0); /* pgo_fault did pmap enter */ 724 else if (result == VM_PAGER_REFAULT) 725 goto ReFault; /* try again! */ 726 else 727 return (EACCES); 728 } 729 730 /* 731 * now, if the desired page is not shadowed by the amap and we have 732 * a backing object that does not have a special fault routine, then 733 * we ask (with pgo_get) the object for resident pages that we care 734 * about and attempt to map them in. we do not let pgo_get block 735 * (PGO_LOCKED). 736 * 737 * ("get" has the option of doing a pmap_enter for us) 738 */ 739 if (uobj && shadowed == FALSE) { 740 uvmexp.fltlget++; 741 gotpages = npages; 742 (void) uobj->pgops->pgo_get(uobj, ufi.entry->offset + 743 (startva - ufi.entry->start), 744 pages, &gotpages, centeridx, 745 access_type & MASK(ufi.entry), 746 ufi.entry->advice, PGO_LOCKED); 747 748 /* check for pages to map, if we got any */ 749 uobjpage = NULL; 750 if (gotpages) { 751 currva = startva; 752 for (lcv = 0 ; lcv < npages ; 753 lcv++, currva += PAGE_SIZE) { 754 if (pages[lcv] == NULL || 755 pages[lcv] == PGO_DONTCARE) 756 continue; 757 758 KASSERT((pages[lcv]->pg_flags & PG_RELEASED) == 0); 759 760 /* 761 * if center page is resident and not 762 * PG_BUSY, then pgo_get made it PG_BUSY 763 * for us and gave us a handle to it. 764 * remember this page as "uobjpage." 765 * (for later use). 766 */ 767 if (lcv == centeridx) { 768 uobjpage = pages[lcv]; 769 continue; 770 } 771 772 /* 773 * note: calling pgo_get with locked data 774 * structures returns us pages which are 775 * neither busy nor released, so we don't 776 * need to check for this. we can just 777 * directly enter the page (after moving it 778 * to the head of the active queue [useful?]). 779 */ 780 781 uvm_lock_pageq(); 782 uvm_pageactivate(pages[lcv]); /* reactivate */ 783 uvm_unlock_pageq(); 784 uvmexp.fltnomap++; 785 786 /* 787 * Since this page isn't the page that's 788 * actually faulting, ignore pmap_enter() 789 * failures; it's not critical that we 790 * enter these right now. 791 */ 792 (void) pmap_enter(ufi.orig_map->pmap, currva, 793 VM_PAGE_TO_PHYS(pages[lcv]) | pa_flags, 794 enter_prot & MASK(ufi.entry), 795 PMAP_CANFAIL | 796 (wired ? PMAP_WIRED : 0)); 797 798 /* 799 * NOTE: page can't be PG_WANTED because 800 * we've held the lock the whole time 801 * we've had the handle. 802 */ 803 atomic_clearbits_int(&pages[lcv]->pg_flags, 804 PG_BUSY); 805 UVM_PAGE_OWN(pages[lcv], NULL); 806 } /* for "lcv" loop */ 807 pmap_update(ufi.orig_map->pmap); 808 } /* "gotpages" != 0 */ 809 /* note: object still _locked_ */ 810 } else { 811 uobjpage = NULL; 812 } 813 814 /* 815 * note that at this point we are done with any front or back pages. 816 * we are now going to focus on the center page (i.e. the one we've 817 * faulted on). if we have faulted on the top (anon) layer 818 * [i.e. case 1], then the anon we want is anons[centeridx] (we have 819 * not touched it yet). if we have faulted on the bottom (uobj) 820 * layer [i.e. case 2] and the page was both present and available, 821 * then we've got a pointer to it as "uobjpage" and we've already 822 * made it BUSY. 823 */ 824 /* 825 * there are four possible cases we must address: 1A, 1B, 2A, and 2B 826 */ 827 /* redirect case 2: if we are not shadowed, go to case 2. */ 828 if (shadowed == FALSE) 829 goto Case2; 830 831 /* handle case 1: fault on an anon in our amap */ 832 anon = anons[centeridx]; 833 834 /* 835 * no matter if we have case 1A or case 1B we are going to need to 836 * have the anon's memory resident. ensure that now. 837 */ 838 /* 839 * let uvmfault_anonget do the dirty work. 840 * also, if it is OK, then the anon's page is on the queues. 841 */ 842 result = uvmfault_anonget(&ufi, amap, anon); 843 switch (result) { 844 case VM_PAGER_OK: 845 break; 846 847 case VM_PAGER_REFAULT: 848 goto ReFault; 849 850 case VM_PAGER_ERROR: 851 /* 852 * An error occured while trying to bring in the 853 * page -- this is the only error we return right 854 * now. 855 */ 856 return (EACCES); /* XXX */ 857 default: 858 #ifdef DIAGNOSTIC 859 panic("uvm_fault: uvmfault_anonget -> %d", result); 860 #else 861 return (EACCES); 862 #endif 863 } 864 865 /* 866 * if we are case 1B then we will need to allocate a new blank 867 * anon to transfer the data into. note that we have a lock 868 * on anon, so no one can busy or release the page until we are done. 869 * also note that the ref count can't drop to zero here because 870 * it is > 1 and we are only dropping one ref. 871 * 872 * in the (hopefully very rare) case that we are out of RAM we 873 * will wait for more RAM, and refault. 874 * 875 * if we are out of anon VM we wait for RAM to become available. 876 */ 877 878 if ((access_type & PROT_WRITE) != 0 && anon->an_ref > 1) { 879 uvmexp.flt_acow++; 880 oanon = anon; /* oanon = old */ 881 anon = uvm_analloc(); 882 if (anon) { 883 pg = uvm_pagealloc(NULL, 0, anon, 0); 884 } 885 886 /* check for out of RAM */ 887 if (anon == NULL || pg == NULL) { 888 uvmfault_unlockall(&ufi, amap, NULL, oanon); 889 KASSERT(uvmexp.swpgonly <= uvmexp.swpages); 890 if (anon == NULL) 891 uvmexp.fltnoanon++; 892 else { 893 uvm_anfree(anon); 894 uvmexp.fltnoram++; 895 } 896 897 if (uvmexp.swpgonly == uvmexp.swpages) 898 return (ENOMEM); 899 900 /* out of RAM, wait for more */ 901 if (anon == NULL) 902 uvm_anwait(); 903 else 904 uvm_wait("flt_noram3"); 905 goto ReFault; 906 } 907 908 /* got all resources, replace anon with nanon */ 909 uvm_pagecopy(oanon->an_page, pg); /* pg now !PG_CLEAN */ 910 /* un-busy! new page */ 911 atomic_clearbits_int(&pg->pg_flags, PG_BUSY|PG_FAKE); 912 UVM_PAGE_OWN(pg, NULL); 913 ret = amap_add(&ufi.entry->aref, 914 ufi.orig_rvaddr - ufi.entry->start, anon, 1); 915 KASSERT(ret == 0); 916 917 /* deref: can not drop to zero here by defn! */ 918 oanon->an_ref--; 919 920 /* 921 * note: anon is _not_ locked, but we have the sole references 922 * to in from amap. 923 * thus, no one can get at it until we are done with it. 924 */ 925 } else { 926 uvmexp.flt_anon++; 927 oanon = anon; 928 pg = anon->an_page; 929 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */ 930 enter_prot = enter_prot & ~PROT_WRITE; 931 } 932 933 /* 934 * now map the page in ... 935 * XXX: old fault unlocks object before pmap_enter. this seems 936 * suspect since some other thread could blast the page out from 937 * under us between the unlock and the pmap_enter. 938 */ 939 if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, 940 VM_PAGE_TO_PHYS(pg) | pa_flags, enter_prot, 941 access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0)) != 0) { 942 /* 943 * No need to undo what we did; we can simply think of 944 * this as the pmap throwing away the mapping information. 945 * 946 * We do, however, have to go through the ReFault path, 947 * as the map may change while we're asleep. 948 */ 949 uvmfault_unlockall(&ufi, amap, NULL, oanon); 950 KASSERT(uvmexp.swpgonly <= uvmexp.swpages); 951 if (uvmexp.swpgonly == uvmexp.swpages) { 952 /* XXX instrumentation */ 953 return (ENOMEM); 954 } 955 /* XXX instrumentation */ 956 uvm_wait("flt_pmfail1"); 957 goto ReFault; 958 } 959 960 /* ... update the page queues. */ 961 uvm_lock_pageq(); 962 963 if (fault_type == VM_FAULT_WIRE) { 964 uvm_pagewire(pg); 965 /* 966 * since the now-wired page cannot be paged out, 967 * release its swap resources for others to use. 968 * since an anon with no swap cannot be PG_CLEAN, 969 * clear its clean flag now. 970 */ 971 atomic_clearbits_int(&pg->pg_flags, PG_CLEAN); 972 uvm_anon_dropswap(anon); 973 } else { 974 /* activate it */ 975 uvm_pageactivate(pg); 976 } 977 978 uvm_unlock_pageq(); 979 980 /* done case 1! finish up by unlocking everything and returning success */ 981 uvmfault_unlockall(&ufi, amap, NULL, oanon); 982 pmap_update(ufi.orig_map->pmap); 983 return (0); 984 985 986 Case2: 987 /* handle case 2: faulting on backing object or zero fill */ 988 /* 989 * note that uobjpage can not be PGO_DONTCARE at this point. we now 990 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we 991 * have a backing object, check and see if we are going to promote 992 * the data up to an anon during the fault. 993 */ 994 if (uobj == NULL) { 995 uobjpage = PGO_DONTCARE; 996 promote = TRUE; /* always need anon here */ 997 } else { 998 KASSERT(uobjpage != PGO_DONTCARE); 999 promote = (access_type & PROT_WRITE) && 1000 UVM_ET_ISCOPYONWRITE(ufi.entry); 1001 } 1002 1003 /* 1004 * if uobjpage is not null then we do not need to do I/O to get the 1005 * uobjpage. 1006 * 1007 * if uobjpage is null, then we need to ask the pager to 1008 * get the data for us. once we have the data, we need to reverify 1009 * the state the world. we are currently not holding any resources. 1010 */ 1011 if (uobjpage) { 1012 /* update rusage counters */ 1013 curproc->p_ru.ru_minflt++; 1014 } else { 1015 /* update rusage counters */ 1016 curproc->p_ru.ru_majflt++; 1017 1018 uvmfault_unlockall(&ufi, amap, NULL, NULL); 1019 1020 uvmexp.fltget++; 1021 gotpages = 1; 1022 uoff = (ufi.orig_rvaddr - ufi.entry->start) + ufi.entry->offset; 1023 result = uobj->pgops->pgo_get(uobj, uoff, &uobjpage, &gotpages, 1024 0, access_type & MASK(ufi.entry), ufi.entry->advice, 1025 PGO_SYNCIO); 1026 1027 /* recover from I/O */ 1028 if (result != VM_PAGER_OK) { 1029 KASSERT(result != VM_PAGER_PEND); 1030 1031 if (result == VM_PAGER_AGAIN) { 1032 tsleep_nsec(&lbolt, PVM, "fltagain2", INFSLP); 1033 goto ReFault; 1034 } 1035 1036 if (!UVM_ET_ISNOFAULT(ufi.entry)) 1037 return (EIO); 1038 1039 uobjpage = PGO_DONTCARE; 1040 promote = TRUE; 1041 } 1042 1043 /* re-verify the state of the world. */ 1044 locked = uvmfault_relock(&ufi); 1045 1046 /* 1047 * Re-verify that amap slot is still free. if there is 1048 * a problem, we clean up. 1049 */ 1050 if (locked && amap && amap_lookup(&ufi.entry->aref, 1051 ufi.orig_rvaddr - ufi.entry->start)) { 1052 if (locked) 1053 uvmfault_unlockall(&ufi, amap, NULL, NULL); 1054 locked = FALSE; 1055 } 1056 1057 /* didn't get the lock? release the page and retry. */ 1058 if (locked == FALSE && uobjpage != PGO_DONTCARE) { 1059 uvm_lock_pageq(); 1060 /* make sure it is in queues */ 1061 uvm_pageactivate(uobjpage); 1062 uvm_unlock_pageq(); 1063 1064 if (uobjpage->pg_flags & PG_WANTED) 1065 /* still holding object lock */ 1066 wakeup(uobjpage); 1067 atomic_clearbits_int(&uobjpage->pg_flags, 1068 PG_BUSY|PG_WANTED); 1069 UVM_PAGE_OWN(uobjpage, NULL); 1070 goto ReFault; 1071 } 1072 if (locked == FALSE) 1073 goto ReFault; 1074 1075 /* 1076 * we have the data in uobjpage which is PG_BUSY 1077 */ 1078 } 1079 1080 /* 1081 * notes: 1082 * - at this point uobjpage can not be NULL 1083 * - at this point uobjpage could be PG_WANTED (handle later) 1084 */ 1085 if (promote == FALSE) { 1086 /* 1087 * we are not promoting. if the mapping is COW ensure that we 1088 * don't give more access than we should (e.g. when doing a read 1089 * fault on a COPYONWRITE mapping we want to map the COW page in 1090 * R/O even though the entry protection could be R/W). 1091 * 1092 * set "pg" to the page we want to map in (uobjpage, usually) 1093 */ 1094 uvmexp.flt_obj++; 1095 if (UVM_ET_ISCOPYONWRITE(ufi.entry)) 1096 enter_prot &= ~PROT_WRITE; 1097 pg = uobjpage; /* map in the actual object */ 1098 1099 /* assert(uobjpage != PGO_DONTCARE) */ 1100 1101 /* 1102 * we are faulting directly on the page. 1103 */ 1104 } else { 1105 /* 1106 * if we are going to promote the data to an anon we 1107 * allocate a blank anon here and plug it into our amap. 1108 */ 1109 #ifdef DIAGNOSTIC 1110 if (amap == NULL) 1111 panic("uvm_fault: want to promote data, but no anon"); 1112 #endif 1113 1114 anon = uvm_analloc(); 1115 if (anon) { 1116 /* 1117 * In `Fill in data...' below, if 1118 * uobjpage == PGO_DONTCARE, we want 1119 * a zero'd, dirty page, so have 1120 * uvm_pagealloc() do that for us. 1121 */ 1122 pg = uvm_pagealloc(NULL, 0, anon, 1123 (uobjpage == PGO_DONTCARE) ? UVM_PGA_ZERO : 0); 1124 } 1125 1126 /* 1127 * out of memory resources? 1128 */ 1129 if (anon == NULL || pg == NULL) { 1130 /* arg! must unbusy our page and fail or sleep. */ 1131 if (uobjpage != PGO_DONTCARE) { 1132 uvm_lock_pageq(); 1133 uvm_pageactivate(uobjpage); 1134 uvm_unlock_pageq(); 1135 1136 if (uobjpage->pg_flags & PG_WANTED) 1137 wakeup(uobjpage); 1138 atomic_clearbits_int(&uobjpage->pg_flags, 1139 PG_BUSY|PG_WANTED); 1140 UVM_PAGE_OWN(uobjpage, NULL); 1141 } 1142 1143 /* unlock and fail ... */ 1144 uvmfault_unlockall(&ufi, amap, uobj, NULL); 1145 KASSERT(uvmexp.swpgonly <= uvmexp.swpages); 1146 if (anon == NULL) 1147 uvmexp.fltnoanon++; 1148 else { 1149 uvm_anfree(anon); 1150 uvmexp.fltnoram++; 1151 } 1152 1153 if (uvmexp.swpgonly == uvmexp.swpages) 1154 return (ENOMEM); 1155 1156 /* out of RAM, wait for more */ 1157 if (anon == NULL) 1158 uvm_anwait(); 1159 else 1160 uvm_wait("flt_noram5"); 1161 goto ReFault; 1162 } 1163 1164 /* fill in the data */ 1165 if (uobjpage != PGO_DONTCARE) { 1166 uvmexp.flt_prcopy++; 1167 /* copy page [pg now dirty] */ 1168 uvm_pagecopy(uobjpage, pg); 1169 1170 /* 1171 * promote to shared amap? make sure all sharing 1172 * procs see it 1173 */ 1174 if ((amap_flags(amap) & AMAP_SHARED) != 0) { 1175 pmap_page_protect(uobjpage, PROT_NONE); 1176 } 1177 1178 /* dispose of uobjpage. drop handle to uobj as well. */ 1179 if (uobjpage->pg_flags & PG_WANTED) 1180 wakeup(uobjpage); 1181 atomic_clearbits_int(&uobjpage->pg_flags, 1182 PG_BUSY|PG_WANTED); 1183 UVM_PAGE_OWN(uobjpage, NULL); 1184 uvm_lock_pageq(); 1185 uvm_pageactivate(uobjpage); 1186 uvm_unlock_pageq(); 1187 uobj = NULL; 1188 } else { 1189 uvmexp.flt_przero++; 1190 /* 1191 * Page is zero'd and marked dirty by uvm_pagealloc() 1192 * above. 1193 */ 1194 } 1195 1196 if (amap_add(&ufi.entry->aref, 1197 ufi.orig_rvaddr - ufi.entry->start, anon, 0)) { 1198 uvmfault_unlockall(&ufi, amap, NULL, oanon); 1199 KASSERT(uvmexp.swpgonly <= uvmexp.swpages); 1200 uvm_anfree(anon); 1201 uvmexp.fltnoamap++; 1202 1203 if (uvmexp.swpgonly == uvmexp.swpages) 1204 return (ENOMEM); 1205 1206 amap_populate(&ufi.entry->aref, 1207 ufi.orig_rvaddr - ufi.entry->start); 1208 goto ReFault; 1209 } 1210 } 1211 1212 /* note: pg is either the uobjpage or the new page in the new anon */ 1213 /* 1214 * all resources are present. we can now map it in and free our 1215 * resources. 1216 */ 1217 if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, 1218 VM_PAGE_TO_PHYS(pg) | pa_flags, enter_prot, 1219 access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0)) != 0) { 1220 /* 1221 * No need to undo what we did; we can simply think of 1222 * this as the pmap throwing away the mapping information. 1223 * 1224 * We do, however, have to go through the ReFault path, 1225 * as the map may change while we're asleep. 1226 */ 1227 if (pg->pg_flags & PG_WANTED) 1228 wakeup(pg); 1229 1230 atomic_clearbits_int(&pg->pg_flags, PG_BUSY|PG_FAKE|PG_WANTED); 1231 UVM_PAGE_OWN(pg, NULL); 1232 uvmfault_unlockall(&ufi, amap, uobj, NULL); 1233 KASSERT(uvmexp.swpgonly <= uvmexp.swpages); 1234 if (uvmexp.swpgonly == uvmexp.swpages) { 1235 /* XXX instrumentation */ 1236 return (ENOMEM); 1237 } 1238 /* XXX instrumentation */ 1239 uvm_wait("flt_pmfail2"); 1240 goto ReFault; 1241 } 1242 1243 uvm_lock_pageq(); 1244 1245 if (fault_type == VM_FAULT_WIRE) { 1246 uvm_pagewire(pg); 1247 if (pg->pg_flags & PQ_AOBJ) { 1248 /* 1249 * since the now-wired page cannot be paged out, 1250 * release its swap resources for others to use. 1251 * since an aobj page with no swap cannot be PG_CLEAN, 1252 * clear its clean flag now. 1253 */ 1254 atomic_clearbits_int(&pg->pg_flags, PG_CLEAN); 1255 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT); 1256 } 1257 } else { 1258 /* activate it */ 1259 uvm_pageactivate(pg); 1260 } 1261 uvm_unlock_pageq(); 1262 1263 if (pg->pg_flags & PG_WANTED) 1264 wakeup(pg); 1265 1266 atomic_clearbits_int(&pg->pg_flags, PG_BUSY|PG_FAKE|PG_WANTED); 1267 UVM_PAGE_OWN(pg, NULL); 1268 uvmfault_unlockall(&ufi, amap, uobj, NULL); 1269 pmap_update(ufi.orig_map->pmap); 1270 1271 return (0); 1272 } 1273 1274 1275 /* 1276 * uvm_fault_wire: wire down a range of virtual addresses in a map. 1277 * 1278 * => map may be read-locked by caller, but MUST NOT be write-locked. 1279 * => if map is read-locked, any operations which may cause map to 1280 * be write-locked in uvm_fault() must be taken care of by 1281 * the caller. See uvm_map_pageable(). 1282 */ 1283 int 1284 uvm_fault_wire(vm_map_t map, vaddr_t start, vaddr_t end, vm_prot_t access_type) 1285 { 1286 vaddr_t va; 1287 int rv; 1288 1289 /* 1290 * now fault it in a page at a time. if the fault fails then we have 1291 * to undo what we have done. note that in uvm_fault PROT_NONE 1292 * is replaced with the max protection if fault_type is VM_FAULT_WIRE. 1293 */ 1294 for (va = start ; va < end ; va += PAGE_SIZE) { 1295 rv = uvm_fault(map, va, VM_FAULT_WIRE, access_type); 1296 if (rv) { 1297 if (va != start) { 1298 uvm_fault_unwire(map, start, va); 1299 } 1300 return (rv); 1301 } 1302 } 1303 1304 return (0); 1305 } 1306 1307 /* 1308 * uvm_fault_unwire(): unwire range of virtual space. 1309 */ 1310 void 1311 uvm_fault_unwire(vm_map_t map, vaddr_t start, vaddr_t end) 1312 { 1313 1314 vm_map_lock_read(map); 1315 uvm_fault_unwire_locked(map, start, end); 1316 vm_map_unlock_read(map); 1317 } 1318 1319 /* 1320 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire(). 1321 * 1322 * => map must be at least read-locked. 1323 */ 1324 void 1325 uvm_fault_unwire_locked(vm_map_t map, vaddr_t start, vaddr_t end) 1326 { 1327 vm_map_entry_t entry, next; 1328 pmap_t pmap = vm_map_pmap(map); 1329 vaddr_t va; 1330 paddr_t pa; 1331 struct vm_page *pg; 1332 1333 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0); 1334 1335 /* 1336 * we assume that the area we are unwiring has actually been wired 1337 * in the first place. this means that we should be able to extract 1338 * the PAs from the pmap. we also lock out the page daemon so that 1339 * we can call uvm_pageunwire. 1340 */ 1341 uvm_lock_pageq(); 1342 1343 /* find the beginning map entry for the region. */ 1344 KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map)); 1345 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) 1346 panic("uvm_fault_unwire_locked: address not in map"); 1347 1348 for (va = start; va < end ; va += PAGE_SIZE) { 1349 if (pmap_extract(pmap, va, &pa) == FALSE) 1350 continue; 1351 1352 /* find the map entry for the current address. */ 1353 KASSERT(va >= entry->start); 1354 while (va >= entry->end) { 1355 next = RBT_NEXT(uvm_map_addr, entry); 1356 KASSERT(next != NULL && next->start <= entry->end); 1357 entry = next; 1358 } 1359 1360 /* if the entry is no longer wired, tell the pmap. */ 1361 if (VM_MAPENT_ISWIRED(entry) == 0) 1362 pmap_unwire(pmap, va); 1363 1364 pg = PHYS_TO_VM_PAGE(pa); 1365 if (pg) 1366 uvm_pageunwire(pg); 1367 } 1368 1369 uvm_unlock_pageq(); 1370 } 1371 1372 /* 1373 * uvmfault_unlockmaps: unlock the maps 1374 */ 1375 void 1376 uvmfault_unlockmaps(struct uvm_faultinfo *ufi, boolean_t write_locked) 1377 { 1378 /* 1379 * ufi can be NULL when this isn't really a fault, 1380 * but merely paging in anon data. 1381 */ 1382 if (ufi == NULL) { 1383 return; 1384 } 1385 1386 uvmfault_update_stats(ufi); 1387 if (write_locked) { 1388 vm_map_unlock(ufi->map); 1389 } else { 1390 vm_map_unlock_read(ufi->map); 1391 } 1392 } 1393 1394 /* 1395 * uvmfault_unlockall: unlock everything passed in. 1396 * 1397 * => maps must be read-locked (not write-locked). 1398 */ 1399 void 1400 uvmfault_unlockall(struct uvm_faultinfo *ufi, struct vm_amap *amap, 1401 struct uvm_object *uobj, struct vm_anon *anon) 1402 { 1403 1404 uvmfault_unlockmaps(ufi, FALSE); 1405 } 1406 1407 /* 1408 * uvmfault_lookup: lookup a virtual address in a map 1409 * 1410 * => caller must provide a uvm_faultinfo structure with the IN 1411 * params properly filled in 1412 * => we will lookup the map entry (handling submaps) as we go 1413 * => if the lookup is a success we will return with the maps locked 1414 * => if "write_lock" is TRUE, we write_lock the map, otherwise we only 1415 * get a read lock. 1416 * => note that submaps can only appear in the kernel and they are 1417 * required to use the same virtual addresses as the map they 1418 * are referenced by (thus address translation between the main 1419 * map and the submap is unnecessary). 1420 */ 1421 1422 boolean_t 1423 uvmfault_lookup(struct uvm_faultinfo *ufi, boolean_t write_lock) 1424 { 1425 vm_map_t tmpmap; 1426 1427 /* init ufi values for lookup. */ 1428 ufi->map = ufi->orig_map; 1429 ufi->size = ufi->orig_size; 1430 1431 /* 1432 * keep going down levels until we are done. note that there can 1433 * only be two levels so we won't loop very long. 1434 */ 1435 while (1) { 1436 if (ufi->orig_rvaddr < ufi->map->min_offset || 1437 ufi->orig_rvaddr >= ufi->map->max_offset) 1438 return(FALSE); 1439 1440 /* lock map */ 1441 if (write_lock) { 1442 vm_map_lock(ufi->map); 1443 } else { 1444 vm_map_lock_read(ufi->map); 1445 } 1446 1447 /* lookup */ 1448 if (!uvm_map_lookup_entry(ufi->map, ufi->orig_rvaddr, 1449 &ufi->entry)) { 1450 uvmfault_unlockmaps(ufi, write_lock); 1451 return(FALSE); 1452 } 1453 1454 /* reduce size if necessary */ 1455 if (ufi->entry->end - ufi->orig_rvaddr < ufi->size) 1456 ufi->size = ufi->entry->end - ufi->orig_rvaddr; 1457 1458 /* 1459 * submap? replace map with the submap and lookup again. 1460 * note: VAs in submaps must match VAs in main map. 1461 */ 1462 if (UVM_ET_ISSUBMAP(ufi->entry)) { 1463 tmpmap = ufi->entry->object.sub_map; 1464 uvmfault_unlockmaps(ufi, write_lock); 1465 ufi->map = tmpmap; 1466 continue; 1467 } 1468 1469 /* got it! */ 1470 ufi->mapv = ufi->map->timestamp; 1471 return(TRUE); 1472 1473 } 1474 /*NOTREACHED*/ 1475 } 1476 1477 /* 1478 * uvmfault_relock: attempt to relock the same version of the map 1479 * 1480 * => fault data structures should be unlocked before calling. 1481 * => if a success (TRUE) maps will be locked after call. 1482 */ 1483 boolean_t 1484 uvmfault_relock(struct uvm_faultinfo *ufi) 1485 { 1486 /* 1487 * ufi can be NULL when this isn't really a fault, 1488 * but merely paging in anon data. 1489 */ 1490 if (ufi == NULL) { 1491 return TRUE; 1492 } 1493 1494 uvmexp.fltrelck++; 1495 1496 /* 1497 * relock map. fail if version mismatch (in which case nothing 1498 * gets locked). 1499 */ 1500 vm_map_lock_read(ufi->map); 1501 if (ufi->mapv != ufi->map->timestamp) { 1502 vm_map_unlock_read(ufi->map); 1503 return(FALSE); 1504 } 1505 1506 uvmexp.fltrelckok++; 1507 return(TRUE); /* got it! */ 1508 } 1509