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