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