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