uvm_fault.c revision 1.227 1 /* $NetBSD: uvm_fault.c,v 1.227 2020/05/17 19:38:17 ad Exp $ */
2
3 /*
4 * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp
28 */
29
30 /*
31 * uvm_fault.c: fault handler
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.227 2020/05/17 19:38:17 ad Exp $");
36
37 #include "opt_uvmhist.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/atomic.h>
42 #include <sys/kernel.h>
43 #include <sys/mman.h>
44
45 #include <uvm/uvm.h>
46 #include <uvm/uvm_pdpolicy.h>
47
48 /*
49 *
50 * a word on page faults:
51 *
52 * types of page faults we handle:
53 *
54 * CASE 1: upper layer faults CASE 2: lower layer faults
55 *
56 * CASE 1A CASE 1B CASE 2A CASE 2B
57 * read/write1 write>1 read/write +-cow_write/zero
58 * | | | |
59 * +--|--+ +--|--+ +-----+ + | + | +-----+
60 * amap | V | | ---------> new | | | | ^ |
61 * +-----+ +-----+ +-----+ + | + | +--|--+
62 * | | |
63 * +-----+ +-----+ +--|--+ | +--|--+
64 * uobj | d/c | | d/c | | V | +----+ |
65 * +-----+ +-----+ +-----+ +-----+
66 *
67 * d/c = don't care
68 *
69 * case [0]: layerless fault
70 * no amap or uobj is present. this is an error.
71 *
72 * case [1]: upper layer fault [anon active]
73 * 1A: [read] or [write with anon->an_ref == 1]
74 * I/O takes place in upper level anon and uobj is not touched.
75 * 1B: [write with anon->an_ref > 1]
76 * new anon is alloc'd and data is copied off ["COW"]
77 *
78 * case [2]: lower layer fault [uobj]
79 * 2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
80 * I/O takes place directly in object.
81 * 2B: [write to copy_on_write] or [read on NULL uobj]
82 * data is "promoted" from uobj to a new anon.
83 * if uobj is null, then we zero fill.
84 *
85 * we follow the standard UVM locking protocol ordering:
86 *
87 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
88 * we hold a PG_BUSY page if we unlock for I/O
89 *
90 *
91 * the code is structured as follows:
92 *
93 * - init the "IN" params in the ufi structure
94 * ReFault: (ERESTART returned to the loop in uvm_fault_internal)
95 * - do lookups [locks maps], check protection, handle needs_copy
96 * - check for case 0 fault (error)
97 * - establish "range" of fault
98 * - if we have an amap lock it and extract the anons
99 * - if sequential advice deactivate pages behind us
100 * - at the same time check pmap for unmapped areas and anon for pages
101 * that we could map in (and do map it if found)
102 * - check object for resident pages that we could map in
103 * - if (case 2) goto Case2
104 * - >>> handle case 1
105 * - ensure source anon is resident in RAM
106 * - if case 1B alloc new anon and copy from source
107 * - map the correct page in
108 * Case2:
109 * - >>> handle case 2
110 * - ensure source page is resident (if uobj)
111 * - if case 2B alloc new anon and copy from source (could be zero
112 * fill if uobj == NULL)
113 * - map the correct page in
114 * - done!
115 *
116 * note on paging:
117 * if we have to do I/O we place a PG_BUSY page in the correct object,
118 * unlock everything, and do the I/O. when I/O is done we must reverify
119 * the state of the world before assuming that our data structures are
120 * valid. [because mappings could change while the map is unlocked]
121 *
122 * alternative 1: unbusy the page in question and restart the page fault
123 * from the top (ReFault). this is easy but does not take advantage
124 * of the information that we already have from our previous lookup,
125 * although it is possible that the "hints" in the vm_map will help here.
126 *
127 * alternative 2: the system already keeps track of a "version" number of
128 * a map. [i.e. every time you write-lock a map (e.g. to change a
129 * mapping) you bump the version number up by one...] so, we can save
130 * the version number of the map before we release the lock and start I/O.
131 * then when I/O is done we can relock and check the version numbers
132 * to see if anything changed. this might save us some over 1 because
133 * we don't have to unbusy the page and may be less compares(?).
134 *
135 * alternative 3: put in backpointers or a way to "hold" part of a map
136 * in place while I/O is in progress. this could be complex to
137 * implement (especially with structures like amap that can be referenced
138 * by multiple map entries, and figuring out what should wait could be
139 * complex as well...).
140 *
141 * we use alternative 2. given that we are multi-threaded now we may want
142 * to reconsider the choice.
143 */
144
145 /*
146 * local data structures
147 */
148
149 struct uvm_advice {
150 int advice;
151 int nback;
152 int nforw;
153 };
154
155 /*
156 * page range array:
157 * note: index in array must match "advice" value
158 * XXX: borrowed numbers from freebsd. do they work well for us?
159 */
160
161 static const struct uvm_advice uvmadvice[] = {
162 { UVM_ADV_NORMAL, 3, 4 },
163 { UVM_ADV_RANDOM, 0, 0 },
164 { UVM_ADV_SEQUENTIAL, 8, 7},
165 };
166
167 #define UVM_MAXRANGE 16 /* must be MAX() of nback+nforw+1 */
168
169 /*
170 * private prototypes
171 */
172
173 /*
174 * externs from other modules
175 */
176
177 extern int start_init_exec; /* Is init_main() done / init running? */
178
179 /*
180 * inline functions
181 */
182
183 /*
184 * uvmfault_anonflush: try and deactivate pages in specified anons
185 *
186 * => does not have to deactivate page if it is busy
187 */
188
189 static inline void
190 uvmfault_anonflush(struct vm_anon **anons, int n)
191 {
192 int lcv;
193 struct vm_page *pg;
194
195 for (lcv = 0; lcv < n; lcv++) {
196 if (anons[lcv] == NULL)
197 continue;
198 KASSERT(rw_lock_held(anons[lcv]->an_lock));
199 pg = anons[lcv]->an_page;
200 if (pg && (pg->flags & PG_BUSY) == 0) {
201 uvm_pagelock(pg);
202 uvm_pagedeactivate(pg);
203 uvm_pageunlock(pg);
204 }
205 }
206 }
207
208 /*
209 * normal functions
210 */
211
212 /*
213 * uvmfault_amapcopy: clear "needs_copy" in a map.
214 *
215 * => called with VM data structures unlocked (usually, see below)
216 * => we get a write lock on the maps and clear needs_copy for a VA
217 * => if we are out of RAM we sleep (waiting for more)
218 */
219
220 static void
221 uvmfault_amapcopy(struct uvm_faultinfo *ufi)
222 {
223 for (;;) {
224
225 /*
226 * no mapping? give up.
227 */
228
229 if (uvmfault_lookup(ufi, true) == false)
230 return;
231
232 /*
233 * copy if needed.
234 */
235
236 if (UVM_ET_ISNEEDSCOPY(ufi->entry))
237 amap_copy(ufi->map, ufi->entry, AMAP_COPY_NOWAIT,
238 ufi->orig_rvaddr, ufi->orig_rvaddr + 1);
239
240 /*
241 * didn't work? must be out of RAM. unlock and sleep.
242 */
243
244 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
245 uvmfault_unlockmaps(ufi, true);
246 uvm_wait("fltamapcopy");
247 continue;
248 }
249
250 /*
251 * got it! unlock and return.
252 */
253
254 uvmfault_unlockmaps(ufi, true);
255 return;
256 }
257 /*NOTREACHED*/
258 }
259
260 /*
261 * uvmfault_anonget: get data in an anon into a non-busy, non-released
262 * page in that anon.
263 *
264 * => Map, amap and thus anon should be locked by caller.
265 * => If we fail, we unlock everything and error is returned.
266 * => If we are successful, return with everything still locked.
267 * => We do not move the page on the queues [gets moved later]. If we
268 * allocate a new page [we_own], it gets put on the queues. Either way,
269 * the result is that the page is on the queues at return time
270 * => For pages which are on loan from a uvm_object (and thus are not owned
271 * by the anon): if successful, return with the owning object locked.
272 * The caller must unlock this object when it unlocks everything else.
273 */
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 krw_t lock_type;
281 int error;
282
283 UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist);
284 KASSERT(rw_lock_held(anon->an_lock));
285 KASSERT(anon->an_lock == amap->am_lock);
286
287 /* Increment the counters.*/
288 cpu_count(CPU_COUNT_FLTANGET, 1);
289 if (anon->an_page) {
290 curlwp->l_ru.ru_minflt++;
291 } else {
292 curlwp->l_ru.ru_majflt++;
293 }
294 error = 0;
295
296 /*
297 * Loop until we get the anon data, or fail.
298 */
299
300 for (;;) {
301 bool we_own, locked;
302 /*
303 * Note: 'we_own' will become true if we set PG_BUSY on a page.
304 */
305 we_own = false;
306 pg = anon->an_page;
307
308 /*
309 * If there is a resident page and it is loaned, then anon
310 * may not own it. Call out to uvm_anon_lockloanpg() to
311 * identify and lock the real owner of the page.
312 */
313
314 if (pg && pg->loan_count)
315 pg = uvm_anon_lockloanpg(anon);
316
317 /*
318 * Is page resident? Make sure it is not busy/released.
319 */
320
321 lock_type = rw_lock_op(anon->an_lock);
322 if (pg) {
323
324 /*
325 * at this point, if the page has a uobject [meaning
326 * we have it on loan], then that uobject is locked
327 * by us! if the page is busy, we drop all the
328 * locks (including uobject) and try again.
329 */
330
331 if ((pg->flags & PG_BUSY) == 0) {
332 UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
333 return 0;
334 }
335 cpu_count(CPU_COUNT_FLTPGWAIT, 1);
336
337 /*
338 * The last unlock must be an atomic unlock and wait
339 * on the owner of page.
340 */
341
342 if (pg->uobject) {
343 /* Owner of page is UVM object. */
344 uvmfault_unlockall(ufi, amap, NULL);
345 UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
346 0,0,0);
347 uvm_pagewait(pg, pg->uobject->vmobjlock, "anonget1");
348 } else {
349 /* Owner of page is anon. */
350 uvmfault_unlockall(ufi, NULL, NULL);
351 UVMHIST_LOG(maphist, " unlock+wait on anon",0,
352 0,0,0);
353 uvm_pagewait(pg, anon->an_lock, "anonget2");
354 }
355 } else {
356 #if defined(VMSWAP)
357 /*
358 * No page, therefore allocate one. A write lock is
359 * required for this. If the caller didn't supply
360 * one, fail now and have them retry.
361 */
362
363 if (lock_type == RW_READER) {
364 return ENOLCK;
365 }
366 pg = uvm_pagealloc(NULL,
367 ufi != NULL ? ufi->orig_rvaddr : 0,
368 anon, ufi != NULL ? UVM_FLAG_COLORMATCH : 0);
369 if (pg == NULL) {
370 /* Out of memory. Wait a little. */
371 uvmfault_unlockall(ufi, amap, NULL);
372 cpu_count(CPU_COUNT_FLTNORAM, 1);
373 UVMHIST_LOG(maphist, " noram -- UVM_WAIT",0,
374 0,0,0);
375 if (!uvm_reclaimable()) {
376 return ENOMEM;
377 }
378 uvm_wait("flt_noram1");
379 } else {
380 /* PG_BUSY bit is set. */
381 we_own = true;
382 uvmfault_unlockall(ufi, amap, NULL);
383
384 /*
385 * Pass a PG_BUSY+PG_FAKE clean page into
386 * the uvm_swap_get() function with all data
387 * structures unlocked. Note that it is OK
388 * to read an_swslot here, because we hold
389 * PG_BUSY on the page.
390 */
391 cpu_count(CPU_COUNT_PAGEINS, 1);
392 error = uvm_swap_get(pg, anon->an_swslot,
393 PGO_SYNCIO);
394
395 /*
396 * We clean up after the I/O below in the
397 * 'we_own' case.
398 */
399 }
400 #else
401 panic("%s: no page", __func__);
402 #endif /* defined(VMSWAP) */
403 }
404
405 /*
406 * Re-lock the map and anon.
407 */
408
409 locked = uvmfault_relock(ufi);
410 if (locked || we_own) {
411 rw_enter(anon->an_lock, lock_type);
412 }
413
414 /*
415 * If we own the page (i.e. we set PG_BUSY), then we need
416 * to clean up after the I/O. There are three cases to
417 * consider:
418 *
419 * 1) Page was released during I/O: free anon and ReFault.
420 * 2) I/O not OK. Free the page and cause the fault to fail.
421 * 3) I/O OK! Activate the page and sync with the non-we_own
422 * case (i.e. drop anon lock if not locked).
423 */
424
425 if (we_own) {
426 KASSERT(lock_type == RW_WRITER);
427 #if defined(VMSWAP)
428 if (error) {
429
430 /*
431 * Remove the swap slot from the anon and
432 * mark the anon as having no real slot.
433 * Do not free the swap slot, thus preventing
434 * it from being used again.
435 */
436
437 if (anon->an_swslot > 0) {
438 uvm_swap_markbad(anon->an_swslot, 1);
439 }
440 anon->an_swslot = SWSLOT_BAD;
441
442 if ((pg->flags & PG_RELEASED) != 0) {
443 goto released;
444 }
445
446 /*
447 * Note: page was never !PG_BUSY, so it
448 * cannot be mapped and thus no need to
449 * pmap_page_protect() it.
450 */
451
452 uvm_pagefree(pg);
453
454 if (locked) {
455 uvmfault_unlockall(ufi, NULL, NULL);
456 }
457 rw_exit(anon->an_lock);
458 UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
459 return error;
460 }
461
462 if ((pg->flags & PG_RELEASED) != 0) {
463 released:
464 KASSERT(anon->an_ref == 0);
465
466 /*
467 * Released while we had unlocked amap.
468 */
469
470 if (locked) {
471 uvmfault_unlockall(ufi, NULL, NULL);
472 }
473 uvm_anon_release(anon);
474
475 if (error) {
476 UVMHIST_LOG(maphist,
477 "<- ERROR/RELEASED", 0,0,0,0);
478 return error;
479 }
480
481 UVMHIST_LOG(maphist, "<- RELEASED", 0,0,0,0);
482 return ERESTART;
483 }
484
485 /*
486 * We have successfully read the page, activate it.
487 */
488
489 uvm_pagelock(pg);
490 uvm_pageactivate(pg);
491 uvm_pagewakeup(pg);
492 uvm_pageunlock(pg);
493 pg->flags &= ~(PG_BUSY|PG_FAKE);
494 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_UNKNOWN);
495 UVM_PAGE_OWN(pg, NULL);
496 #else
497 panic("%s: we_own", __func__);
498 #endif /* defined(VMSWAP) */
499 }
500
501 /*
502 * We were not able to re-lock the map - restart the fault.
503 */
504
505 if (!locked) {
506 if (we_own) {
507 rw_exit(anon->an_lock);
508 }
509 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
510 return ERESTART;
511 }
512
513 /*
514 * Verify that no one has touched the amap and moved
515 * the anon on us.
516 */
517
518 if (ufi != NULL && amap_lookup(&ufi->entry->aref,
519 ufi->orig_rvaddr - ufi->entry->start) != anon) {
520
521 uvmfault_unlockall(ufi, amap, NULL);
522 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
523 return ERESTART;
524 }
525
526 /*
527 * Retry..
528 */
529
530 cpu_count(CPU_COUNT_FLTANRETRY, 1);
531 continue;
532 }
533 /*NOTREACHED*/
534 }
535
536 /*
537 * uvmfault_promote: promote data to a new anon. used for 1B and 2B.
538 *
539 * 1. allocate an anon and a page.
540 * 2. fill its contents.
541 * 3. put it into amap.
542 *
543 * => if we fail (result != 0) we unlock everything.
544 * => on success, return a new locked anon via 'nanon'.
545 * (*nanon)->an_page will be a resident, locked, dirty page.
546 * => it's caller's responsibility to put the promoted nanon->an_page to the
547 * page queue.
548 */
549
550 static int
551 uvmfault_promote(struct uvm_faultinfo *ufi,
552 struct vm_anon *oanon,
553 struct vm_page *uobjpage,
554 struct vm_anon **nanon, /* OUT: allocated anon */
555 struct vm_anon **spare)
556 {
557 struct vm_amap *amap = ufi->entry->aref.ar_amap;
558 struct uvm_object *uobj;
559 struct vm_anon *anon;
560 struct vm_page *pg;
561 struct vm_page *opg;
562 int error;
563 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
564
565 if (oanon) {
566 /* anon COW */
567 opg = oanon->an_page;
568 KASSERT(opg != NULL);
569 KASSERT(opg->uobject == NULL || opg->loan_count > 0);
570 } else if (uobjpage != PGO_DONTCARE) {
571 /* object-backed COW */
572 opg = uobjpage;
573 KASSERT(rw_lock_held(opg->uobject->vmobjlock));
574 } else {
575 /* ZFOD */
576 opg = NULL;
577 }
578 if (opg != NULL) {
579 uobj = opg->uobject;
580 } else {
581 uobj = NULL;
582 }
583
584 KASSERT(amap != NULL);
585 KASSERT(uobjpage != NULL);
586 KASSERT(rw_write_held(amap->am_lock));
587 KASSERT(oanon == NULL || amap->am_lock == oanon->an_lock);
588 KASSERT(uobj == NULL || rw_lock_held(uobj->vmobjlock));
589
590 if (*spare != NULL) {
591 anon = *spare;
592 *spare = NULL;
593 } else {
594 anon = uvm_analloc();
595 }
596 if (anon) {
597
598 /*
599 * The new anon is locked.
600 *
601 * if opg == NULL, we want a zero'd, dirty page,
602 * so have uvm_pagealloc() do that for us.
603 */
604
605 KASSERT(anon->an_lock == NULL);
606 anon->an_lock = amap->am_lock;
607 pg = uvm_pagealloc(NULL, ufi->orig_rvaddr, anon,
608 UVM_FLAG_COLORMATCH | (opg == NULL ? UVM_PGA_ZERO : 0));
609 if (pg == NULL) {
610 anon->an_lock = NULL;
611 }
612 } else {
613 pg = NULL;
614 }
615
616 /*
617 * out of memory resources?
618 */
619
620 if (pg == NULL) {
621 /* save anon for the next try. */
622 if (anon != NULL) {
623 *spare = anon;
624 }
625
626 /* unlock and fail ... */
627 uvmfault_unlockall(ufi, amap, uobj);
628 if (!uvm_reclaimable()) {
629 UVMHIST_LOG(maphist, "out of VM", 0,0,0,0);
630 cpu_count(CPU_COUNT_FLTNOANON, 1);
631 error = ENOMEM;
632 goto done;
633 }
634
635 UVMHIST_LOG(maphist, "out of RAM, waiting for more", 0,0,0,0);
636 cpu_count(CPU_COUNT_FLTNORAM, 1);
637 uvm_wait("flt_noram5");
638 error = ERESTART;
639 goto done;
640 }
641
642 /* copy page [pg now dirty] */
643 if (opg) {
644 uvm_pagecopy(opg, pg);
645 }
646 KASSERT(uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_DIRTY);
647
648 amap_add(&ufi->entry->aref, ufi->orig_rvaddr - ufi->entry->start, anon,
649 oanon != NULL);
650
651 /*
652 * from this point on am_lock won't be dropped until the page is
653 * entered, so it's safe to unbusy the page up front.
654 *
655 * uvm_fault_{upper,lower}_done will activate or enqueue the page.
656 */
657
658 pg = anon->an_page;
659 pg->flags &= ~(PG_BUSY|PG_FAKE);
660 UVM_PAGE_OWN(pg, NULL);
661
662 *nanon = anon;
663 error = 0;
664 done:
665 return error;
666 }
667
668 /*
669 * Update statistics after fault resolution.
670 * - maxrss
671 */
672 void
673 uvmfault_update_stats(struct uvm_faultinfo *ufi)
674 {
675 struct vm_map *map;
676 struct vmspace *vm;
677 struct proc *p;
678 vsize_t res;
679
680 map = ufi->orig_map;
681
682 p = curproc;
683 KASSERT(p != NULL);
684 vm = p->p_vmspace;
685
686 if (&vm->vm_map != map)
687 return;
688
689 res = pmap_resident_count(map->pmap);
690 if (vm->vm_rssmax < res)
691 vm->vm_rssmax = res;
692 }
693
694 /*
695 * F A U L T - m a i n e n t r y p o i n t
696 */
697
698 /*
699 * uvm_fault: page fault handler
700 *
701 * => called from MD code to resolve a page fault
702 * => VM data structures usually should be unlocked. however, it is
703 * possible to call here with the main map locked if the caller
704 * gets a write lock, sets it recusive, and then calls us (c.f.
705 * uvm_map_pageable). this should be avoided because it keeps
706 * the map locked off during I/O.
707 * => MUST NEVER BE CALLED IN INTERRUPT CONTEXT
708 */
709
710 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
711 ~VM_PROT_WRITE : VM_PROT_ALL)
712
713 /* fault_flag values passed from uvm_fault_wire to uvm_fault_internal */
714 #define UVM_FAULT_WIRE (1 << 0)
715 #define UVM_FAULT_MAXPROT (1 << 1)
716
717 struct uvm_faultctx {
718
719 /*
720 * the following members are set up by uvm_fault_check() and
721 * read-only after that.
722 *
723 * note that narrow is used by uvm_fault_check() to change
724 * the behaviour after ERESTART.
725 *
726 * most of them might change after RESTART if the underlying
727 * map entry has been changed behind us. an exception is
728 * wire_paging, which does never change.
729 */
730 vm_prot_t access_type;
731 vaddr_t startva;
732 int npages;
733 int centeridx;
734 bool narrow; /* work on a single requested page only */
735 bool wire_mapping; /* request a PMAP_WIRED mapping
736 (UVM_FAULT_WIRE or VM_MAPENT_ISWIRED) */
737 bool wire_paging; /* request uvm_pagewire
738 (true for UVM_FAULT_WIRE) */
739 bool cow_now; /* VM_PROT_WRITE is actually requested
740 (ie. should break COW and page loaning) */
741
742 /*
743 * enter_prot is set up by uvm_fault_check() and clamped
744 * (ie. drop the VM_PROT_WRITE bit) in various places in case
745 * of !cow_now.
746 */
747 vm_prot_t enter_prot; /* prot at which we want to enter pages in */
748
749 /*
750 * the following member is for uvmfault_promote() and ERESTART.
751 */
752 struct vm_anon *anon_spare;
753
754 /*
755 * the folloing is actually a uvm_fault_lower() internal.
756 * it's here merely for debugging.
757 * (or due to the mechanical separation of the function?)
758 */
759 bool promote;
760
761 /*
762 * type of lock to acquire on objects in both layers.
763 */
764 krw_t lower_lock_type;
765 krw_t upper_lock_type;
766 };
767
768 static inline int uvm_fault_check(
769 struct uvm_faultinfo *, struct uvm_faultctx *,
770 struct vm_anon ***, bool);
771
772 static int uvm_fault_upper(
773 struct uvm_faultinfo *, struct uvm_faultctx *,
774 struct vm_anon **);
775 static inline int uvm_fault_upper_lookup(
776 struct uvm_faultinfo *, const struct uvm_faultctx *,
777 struct vm_anon **, struct vm_page **);
778 static inline void uvm_fault_upper_neighbor(
779 struct uvm_faultinfo *, const struct uvm_faultctx *,
780 vaddr_t, struct vm_page *, bool);
781 static inline int uvm_fault_upper_loan(
782 struct uvm_faultinfo *, struct uvm_faultctx *,
783 struct vm_anon *, struct uvm_object **);
784 static inline int uvm_fault_upper_promote(
785 struct uvm_faultinfo *, struct uvm_faultctx *,
786 struct uvm_object *, struct vm_anon *);
787 static inline int uvm_fault_upper_direct(
788 struct uvm_faultinfo *, struct uvm_faultctx *,
789 struct uvm_object *, struct vm_anon *);
790 static int uvm_fault_upper_enter(
791 struct uvm_faultinfo *, const struct uvm_faultctx *,
792 struct uvm_object *, struct vm_anon *,
793 struct vm_page *, struct vm_anon *);
794 static inline void uvm_fault_upper_done(
795 struct uvm_faultinfo *, const struct uvm_faultctx *,
796 struct vm_anon *, struct vm_page *);
797
798 static int uvm_fault_lower(
799 struct uvm_faultinfo *, struct uvm_faultctx *,
800 struct vm_page **);
801 static inline void uvm_fault_lower_lookup(
802 struct uvm_faultinfo *, const struct uvm_faultctx *,
803 struct vm_page **);
804 static inline void uvm_fault_lower_neighbor(
805 struct uvm_faultinfo *, const struct uvm_faultctx *,
806 vaddr_t, struct vm_page *);
807 static inline int uvm_fault_lower_io(
808 struct uvm_faultinfo *, struct uvm_faultctx *,
809 struct uvm_object **, struct vm_page **);
810 static inline int uvm_fault_lower_direct(
811 struct uvm_faultinfo *, struct uvm_faultctx *,
812 struct uvm_object *, struct vm_page *);
813 static inline int uvm_fault_lower_direct_loan(
814 struct uvm_faultinfo *, struct uvm_faultctx *,
815 struct uvm_object *, struct vm_page **,
816 struct vm_page **);
817 static inline int uvm_fault_lower_promote(
818 struct uvm_faultinfo *, struct uvm_faultctx *,
819 struct uvm_object *, struct vm_page *);
820 static int uvm_fault_lower_enter(
821 struct uvm_faultinfo *, const struct uvm_faultctx *,
822 struct uvm_object *,
823 struct vm_anon *, struct vm_page *);
824 static inline void uvm_fault_lower_done(
825 struct uvm_faultinfo *, const struct uvm_faultctx *,
826 struct uvm_object *, struct vm_page *);
827
828 int
829 uvm_fault_internal(struct vm_map *orig_map, vaddr_t vaddr,
830 vm_prot_t access_type, int fault_flag)
831 {
832 struct uvm_faultinfo ufi;
833 struct uvm_faultctx flt = {
834 .access_type = access_type,
835
836 /* don't look for neighborhood * pages on "wire" fault */
837 .narrow = (fault_flag & UVM_FAULT_WIRE) != 0,
838
839 /* "wire" fault causes wiring of both mapping and paging */
840 .wire_mapping = (fault_flag & UVM_FAULT_WIRE) != 0,
841 .wire_paging = (fault_flag & UVM_FAULT_WIRE) != 0,
842
843 /*
844 * default lock type to acquire on upper & lower layer
845 * objects: reader. this can be upgraded at any point
846 * during the fault from read -> write and uvm_faultctx
847 * changed to match, but is never downgraded write -> read.
848 */
849 #ifdef __HAVE_UNLOCKED_PMAP /* XXX temporary */
850 .upper_lock_type = RW_WRITER,
851 .lower_lock_type = RW_WRITER,
852 #else
853 .upper_lock_type = RW_READER,
854 .lower_lock_type = RW_READER,
855 #endif
856 };
857 const bool maxprot = (fault_flag & UVM_FAULT_MAXPROT) != 0;
858 struct vm_anon *anons_store[UVM_MAXRANGE], **anons;
859 struct vm_page *pages_store[UVM_MAXRANGE], **pages;
860 int error;
861
862 UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist);
863
864 UVMHIST_LOG(maphist, "(map=%#jx, vaddr=%#jx, at=%jd, ff=%jd)",
865 (uintptr_t)orig_map, vaddr, access_type, fault_flag);
866
867 /* Don't count anything until user interaction is possible */
868 kpreempt_disable();
869 if (__predict_true(start_init_exec)) {
870 struct cpu_info *ci = curcpu();
871 CPU_COUNT(CPU_COUNT_NFAULT, 1);
872 /* Don't flood RNG subsystem with samples. */
873 if (++(ci->ci_faultrng) == 503) {
874 ci->ci_faultrng = 0;
875 rnd_add_uint32(&curcpu()->ci_data.cpu_uvm->rs,
876 sizeof(vaddr_t) == sizeof(uint32_t) ?
877 (uint32_t)vaddr : sizeof(vaddr_t) ==
878 sizeof(uint64_t) ?
879 (uint32_t)vaddr :
880 (uint32_t)ci->ci_counts[CPU_COUNT_NFAULT]);
881 }
882 }
883 kpreempt_enable();
884
885 /*
886 * init the IN parameters in the ufi
887 */
888
889 ufi.orig_map = orig_map;
890 ufi.orig_rvaddr = trunc_page(vaddr);
891 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */
892
893 error = ERESTART;
894 while (error == ERESTART) { /* ReFault: */
895 anons = anons_store;
896 pages = pages_store;
897
898 error = uvm_fault_check(&ufi, &flt, &anons, maxprot);
899 if (error != 0)
900 continue;
901
902 error = uvm_fault_upper_lookup(&ufi, &flt, anons, pages);
903 if (error != 0)
904 continue;
905
906 if (pages[flt.centeridx] == PGO_DONTCARE)
907 error = uvm_fault_upper(&ufi, &flt, anons);
908 else {
909 struct uvm_object * const uobj =
910 ufi.entry->object.uvm_obj;
911
912 if (uobj && uobj->pgops->pgo_fault != NULL) {
913 /*
914 * invoke "special" fault routine.
915 */
916 rw_enter(uobj->vmobjlock, RW_WRITER);
917 /* locked: maps(read), amap(if there), uobj */
918 error = uobj->pgops->pgo_fault(&ufi,
919 flt.startva, pages, flt.npages,
920 flt.centeridx, flt.access_type,
921 PGO_LOCKED|PGO_SYNCIO);
922
923 /*
924 * locked: nothing, pgo_fault has unlocked
925 * everything
926 */
927
928 /*
929 * object fault routine responsible for
930 * pmap_update().
931 */
932
933 /*
934 * Wake up the pagedaemon if the fault method
935 * failed for lack of memory but some can be
936 * reclaimed.
937 */
938 if (error == ENOMEM && uvm_reclaimable()) {
939 uvm_wait("pgo_fault");
940 error = ERESTART;
941 }
942 } else {
943 error = uvm_fault_lower(&ufi, &flt, pages);
944 }
945 }
946 }
947
948 if (flt.anon_spare != NULL) {
949 flt.anon_spare->an_ref--;
950 KASSERT(flt.anon_spare->an_ref == 0);
951 KASSERT(flt.anon_spare->an_lock == NULL);
952 uvm_anfree(flt.anon_spare);
953 }
954 return error;
955 }
956
957 /*
958 * uvm_fault_check: check prot, handle needs-copy, etc.
959 *
960 * 1. lookup entry.
961 * 2. check protection.
962 * 3. adjust fault condition (mainly for simulated fault).
963 * 4. handle needs-copy (lazy amap copy).
964 * 5. establish range of interest for neighbor fault (aka pre-fault).
965 * 6. look up anons (if amap exists).
966 * 7. flush pages (if MADV_SEQUENTIAL)
967 *
968 * => called with nothing locked.
969 * => if we fail (result != 0) we unlock everything.
970 * => initialize/adjust many members of flt.
971 */
972
973 static int
974 uvm_fault_check(
975 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
976 struct vm_anon ***ranons, bool maxprot)
977 {
978 struct vm_amap *amap;
979 struct uvm_object *uobj;
980 vm_prot_t check_prot;
981 int nback, nforw;
982 UVMHIST_FUNC("uvm_fault_check"); UVMHIST_CALLED(maphist);
983
984 /*
985 * lookup and lock the maps
986 */
987
988 if (uvmfault_lookup(ufi, false) == false) {
989 UVMHIST_LOG(maphist, "<- no mapping @ %#jx", ufi->orig_rvaddr,
990 0,0,0);
991 return EFAULT;
992 }
993 /* locked: maps(read) */
994
995 #ifdef DIAGNOSTIC
996 if ((ufi->map->flags & VM_MAP_PAGEABLE) == 0) {
997 printf("Page fault on non-pageable map:\n");
998 printf("ufi->map = %p\n", ufi->map);
999 printf("ufi->orig_map = %p\n", ufi->orig_map);
1000 printf("ufi->orig_rvaddr = %#lx\n", (u_long) ufi->orig_rvaddr);
1001 panic("uvm_fault: (ufi->map->flags & VM_MAP_PAGEABLE) == 0");
1002 }
1003 #endif
1004
1005 /*
1006 * check protection
1007 */
1008
1009 check_prot = maxprot ?
1010 ufi->entry->max_protection : ufi->entry->protection;
1011 if ((check_prot & flt->access_type) != flt->access_type) {
1012 UVMHIST_LOG(maphist,
1013 "<- protection failure (prot=%#jx, access=%#jx)",
1014 ufi->entry->protection, flt->access_type, 0, 0);
1015 uvmfault_unlockmaps(ufi, false);
1016 return EFAULT;
1017 }
1018
1019 /*
1020 * "enter_prot" is the protection we want to enter the page in at.
1021 * for certain pages (e.g. copy-on-write pages) this protection can
1022 * be more strict than ufi->entry->protection. "wired" means either
1023 * the entry is wired or we are fault-wiring the pg.
1024 */
1025
1026 flt->enter_prot = ufi->entry->protection;
1027 if (VM_MAPENT_ISWIRED(ufi->entry)) {
1028 flt->wire_mapping = true;
1029 flt->wire_paging = true;
1030 flt->narrow = true;
1031 }
1032
1033 if (flt->wire_mapping) {
1034 flt->access_type = flt->enter_prot; /* full access for wired */
1035 flt->cow_now = (check_prot & VM_PROT_WRITE) != 0;
1036 } else {
1037 flt->cow_now = (flt->access_type & VM_PROT_WRITE) != 0;
1038 }
1039
1040 if (flt->wire_paging) {
1041 /* wiring pages requires a write lock. */
1042 flt->upper_lock_type = RW_WRITER;
1043 flt->lower_lock_type = RW_WRITER;
1044 }
1045
1046 flt->promote = false;
1047
1048 /*
1049 * handle "needs_copy" case. if we need to copy the amap we will
1050 * have to drop our readlock and relock it with a write lock. (we
1051 * need a write lock to change anything in a map entry [e.g.
1052 * needs_copy]).
1053 */
1054
1055 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
1056 if (flt->cow_now || (ufi->entry->object.uvm_obj == NULL)) {
1057 KASSERT(!maxprot);
1058 /* need to clear */
1059 UVMHIST_LOG(maphist,
1060 " need to clear needs_copy and refault",0,0,0,0);
1061 uvmfault_unlockmaps(ufi, false);
1062 uvmfault_amapcopy(ufi);
1063 cpu_count(CPU_COUNT_FLTAMCOPY, 1);
1064 return ERESTART;
1065
1066 } else {
1067
1068 /*
1069 * ensure that we pmap_enter page R/O since
1070 * needs_copy is still true
1071 */
1072
1073 flt->enter_prot &= ~VM_PROT_WRITE;
1074 }
1075 }
1076
1077 /*
1078 * identify the players
1079 */
1080
1081 amap = ufi->entry->aref.ar_amap; /* upper layer */
1082 uobj = ufi->entry->object.uvm_obj; /* lower layer */
1083
1084 /*
1085 * check for a case 0 fault. if nothing backing the entry then
1086 * error now.
1087 */
1088
1089 if (amap == NULL && uobj == NULL) {
1090 uvmfault_unlockmaps(ufi, false);
1091 UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
1092 return EFAULT;
1093 }
1094
1095 /*
1096 * for a case 2B fault waste no time on adjacent pages because
1097 * they are likely already entered.
1098 */
1099
1100 if (uobj != NULL && amap != NULL &&
1101 (flt->access_type & VM_PROT_WRITE) != 0) {
1102 /* wide fault (!narrow) */
1103 flt->narrow = true;
1104 }
1105
1106 /*
1107 * establish range of interest based on advice from mapper
1108 * and then clip to fit map entry. note that we only want
1109 * to do this the first time through the fault. if we
1110 * ReFault we will disable this by setting "narrow" to true.
1111 */
1112
1113 if (flt->narrow == false) {
1114
1115 /* wide fault (!narrow) */
1116 KASSERT(uvmadvice[ufi->entry->advice].advice ==
1117 ufi->entry->advice);
1118 nback = MIN(uvmadvice[ufi->entry->advice].nback,
1119 (ufi->orig_rvaddr - ufi->entry->start) >> PAGE_SHIFT);
1120 flt->startva = ufi->orig_rvaddr - (nback << PAGE_SHIFT);
1121 /*
1122 * note: "-1" because we don't want to count the
1123 * faulting page as forw
1124 */
1125 nforw = MIN(uvmadvice[ufi->entry->advice].nforw,
1126 ((ufi->entry->end - ufi->orig_rvaddr) >>
1127 PAGE_SHIFT) - 1);
1128 flt->npages = nback + nforw + 1;
1129 flt->centeridx = nback;
1130
1131 flt->narrow = true; /* ensure only once per-fault */
1132
1133 } else {
1134
1135 /* narrow fault! */
1136 nback = nforw = 0;
1137 flt->startva = ufi->orig_rvaddr;
1138 flt->npages = 1;
1139 flt->centeridx = 0;
1140
1141 }
1142 /* offset from entry's start to pgs' start */
1143 const voff_t eoff = flt->startva - ufi->entry->start;
1144
1145 /* locked: maps(read) */
1146 UVMHIST_LOG(maphist, " narrow=%jd, back=%jd, forw=%jd, startva=%#jx",
1147 flt->narrow, nback, nforw, flt->startva);
1148 UVMHIST_LOG(maphist, " entry=%#jx, amap=%#jx, obj=%#jx",
1149 (uintptr_t)ufi->entry, (uintptr_t)amap, (uintptr_t)uobj, 0);
1150
1151 /*
1152 * guess at the most suitable lock types to acquire.
1153 * if we've got an amap then lock it and extract current anons.
1154 */
1155
1156 if (amap) {
1157 if ((amap_flags(amap) & AMAP_SHARED) == 0) {
1158 /*
1159 * the amap isn't shared. get a writer lock to
1160 * avoid the cost of upgrading the lock later if
1161 * needed.
1162 *
1163 * XXX nice for PostgreSQL, but consider threads.
1164 */
1165 flt->upper_lock_type = RW_WRITER;
1166 } else if ((flt->access_type & VM_PROT_WRITE) != 0) {
1167 /*
1168 * assume we're about to COW.
1169 */
1170 flt->upper_lock_type = RW_WRITER;
1171 }
1172 amap_lock(amap, flt->upper_lock_type);
1173 amap_lookups(&ufi->entry->aref, eoff, *ranons, flt->npages);
1174 } else {
1175 if ((flt->access_type & VM_PROT_WRITE) != 0) {
1176 /*
1177 * we are about to dirty the object and that
1178 * requires a write lock.
1179 */
1180 flt->lower_lock_type = RW_WRITER;
1181 }
1182 *ranons = NULL; /* to be safe */
1183 }
1184
1185 /* locked: maps(read), amap(if there) */
1186 KASSERT(amap == NULL ||
1187 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1188
1189 /*
1190 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
1191 * now and then forget about them (for the rest of the fault).
1192 */
1193
1194 if (ufi->entry->advice == MADV_SEQUENTIAL && nback != 0) {
1195
1196 UVMHIST_LOG(maphist, " MADV_SEQUENTIAL: flushing backpages",
1197 0,0,0,0);
1198 /* flush back-page anons? */
1199 if (amap)
1200 uvmfault_anonflush(*ranons, nback);
1201
1202 /*
1203 * flush object? change lock type to RW_WRITER, to avoid
1204 * excessive competition between read/write locks if many
1205 * threads doing "sequential access".
1206 */
1207 if (uobj) {
1208 voff_t uoff;
1209
1210 flt->lower_lock_type = RW_WRITER;
1211 uoff = ufi->entry->offset + eoff;
1212 rw_enter(uobj->vmobjlock, RW_WRITER);
1213 (void) (uobj->pgops->pgo_put)(uobj, uoff, uoff +
1214 (nback << PAGE_SHIFT), PGO_DEACTIVATE);
1215 }
1216
1217 /* now forget about the backpages */
1218 if (amap)
1219 *ranons += nback;
1220 flt->startva += (nback << PAGE_SHIFT);
1221 flt->npages -= nback;
1222 flt->centeridx = 0;
1223 }
1224 /*
1225 * => startva is fixed
1226 * => npages is fixed
1227 */
1228 KASSERT(flt->startva <= ufi->orig_rvaddr);
1229 KASSERT(ufi->orig_rvaddr + ufi->orig_size <=
1230 flt->startva + (flt->npages << PAGE_SHIFT));
1231 return 0;
1232 }
1233
1234 /*
1235 * uvm_fault_upper_upgrade: upgrade upper lock, reader -> writer
1236 */
1237
1238 static inline int
1239 uvm_fault_upper_upgrade(struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1240 struct vm_amap *amap, struct uvm_object *uobj)
1241 {
1242 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1243
1244 KASSERT(amap != NULL);
1245 KASSERT(flt->upper_lock_type == rw_lock_op(amap->am_lock));
1246
1247 /*
1248 * fast path.
1249 */
1250
1251 if (__predict_true(flt->upper_lock_type == RW_WRITER)) {
1252 return 0;
1253 }
1254
1255 /*
1256 * otherwise try for the upgrade. if we don't get it, unlock
1257 * everything, restart the fault and next time around get a writer
1258 * lock.
1259 */
1260
1261 flt->upper_lock_type = RW_WRITER;
1262 if (__predict_false(!rw_tryupgrade(amap->am_lock))) {
1263 uvmfault_unlockall(ufi, amap, uobj);
1264 cpu_count(CPU_COUNT_FLTNOUP, 1);
1265 UVMHIST_LOG(maphist, " !upgrade upper", 0, 0,0,0);
1266 return ERESTART;
1267 }
1268 cpu_count(CPU_COUNT_FLTUP, 1);
1269 KASSERT(flt->upper_lock_type == rw_lock_op(amap->am_lock));
1270 return 0;
1271 }
1272
1273 /*
1274 * uvm_fault_upper_lookup: look up existing h/w mapping and amap.
1275 *
1276 * iterate range of interest:
1277 * 1. check if h/w mapping exists. if yes, we don't care
1278 * 2. check if anon exists. if not, page is lower.
1279 * 3. if anon exists, enter h/w mapping for neighbors.
1280 *
1281 * => called with amap locked (if exists).
1282 */
1283
1284 static int
1285 uvm_fault_upper_lookup(
1286 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1287 struct vm_anon **anons, struct vm_page **pages)
1288 {
1289 struct vm_amap *amap = ufi->entry->aref.ar_amap;
1290 int lcv;
1291 vaddr_t currva;
1292 bool shadowed __unused;
1293 bool entered;
1294 UVMHIST_FUNC("uvm_fault_upper_lookup"); UVMHIST_CALLED(maphist);
1295
1296 /* locked: maps(read), amap(if there) */
1297 KASSERT(amap == NULL ||
1298 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1299
1300 /*
1301 * map in the backpages and frontpages we found in the amap in hopes
1302 * of preventing future faults. we also init the pages[] array as
1303 * we go.
1304 */
1305
1306 currva = flt->startva;
1307 shadowed = false;
1308 entered = false;
1309 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
1310 /*
1311 * unmapped or center page. check if any anon at this level.
1312 */
1313 if (amap == NULL || anons[lcv] == NULL) {
1314 pages[lcv] = NULL;
1315 continue;
1316 }
1317
1318 /*
1319 * check for present page and map if possible.
1320 */
1321
1322 pages[lcv] = PGO_DONTCARE;
1323 if (lcv == flt->centeridx) { /* save center for later! */
1324 shadowed = true;
1325 continue;
1326 }
1327
1328 struct vm_anon *anon = anons[lcv];
1329 struct vm_page *pg = anon->an_page;
1330
1331 KASSERT(anon->an_lock == amap->am_lock);
1332
1333 /*
1334 * ignore loaned and busy pages.
1335 * don't play with VAs that are already mapped.
1336 */
1337
1338 if (pg && pg->loan_count == 0 && (pg->flags & PG_BUSY) == 0 &&
1339 !pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
1340 uvm_fault_upper_neighbor(ufi, flt, currva,
1341 pg, anon->an_ref > 1);
1342 entered = true;
1343 }
1344 }
1345 if (entered) {
1346 pmap_update(ufi->orig_map->pmap);
1347 }
1348
1349 /* locked: maps(read), amap(if there) */
1350 KASSERT(amap == NULL ||
1351 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1352 /* (shadowed == true) if there is an anon at the faulting address */
1353 UVMHIST_LOG(maphist, " shadowed=%jd, will_get=%jd", shadowed,
1354 (ufi->entry->object.uvm_obj && shadowed != false),0,0);
1355
1356 return 0;
1357 }
1358
1359 /*
1360 * uvm_fault_upper_neighbor: enter single upper neighbor page.
1361 *
1362 * => called with amap and anon locked.
1363 */
1364
1365 static void
1366 uvm_fault_upper_neighbor(
1367 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1368 vaddr_t currva, struct vm_page *pg, bool readonly)
1369 {
1370 UVMHIST_FUNC("uvm_fault_upper_neighbor"); UVMHIST_CALLED(maphist);
1371
1372 /* locked: amap, anon */
1373
1374 KASSERT(pg->uobject == NULL);
1375 KASSERT(pg->uanon != NULL);
1376 KASSERT(rw_lock_op(pg->uanon->an_lock) == flt->upper_lock_type);
1377 KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);
1378
1379 /*
1380 * there wasn't a direct fault on the page, so avoid the cost of
1381 * activating it.
1382 */
1383
1384 if (!uvmpdpol_pageisqueued_p(pg) && pg->wire_count == 0) {
1385 uvm_pagelock(pg);
1386 uvm_pageenqueue(pg);
1387 uvm_pageunlock(pg);
1388 }
1389
1390 UVMHIST_LOG(maphist,
1391 " MAPPING: n anon: pm=%#jx, va=%#jx, pg=%#jx",
1392 (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
1393 cpu_count(CPU_COUNT_FLTNAMAP, 1);
1394
1395 /*
1396 * Since this page isn't the page that's actually faulting,
1397 * ignore pmap_enter() failures; it's not critical that we
1398 * enter these right now.
1399 */
1400
1401 (void) pmap_enter(ufi->orig_map->pmap, currva,
1402 VM_PAGE_TO_PHYS(pg),
1403 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
1404 flt->enter_prot,
1405 PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0));
1406 }
1407
1408 /*
1409 * uvm_fault_upper: handle upper fault.
1410 *
1411 * 1. acquire anon lock.
1412 * 2. get anon. let uvmfault_anonget do the dirty work.
1413 * 3. handle loan.
1414 * 4. dispatch direct or promote handlers.
1415 */
1416
1417 static int
1418 uvm_fault_upper(
1419 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1420 struct vm_anon **anons)
1421 {
1422 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1423 struct vm_anon * const anon = anons[flt->centeridx];
1424 struct uvm_object *uobj;
1425 int error;
1426 UVMHIST_FUNC("uvm_fault_upper"); UVMHIST_CALLED(maphist);
1427
1428 /* locked: maps(read), amap, anon */
1429 KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1430 KASSERT(anon->an_lock == amap->am_lock);
1431
1432 /*
1433 * handle case 1: fault on an anon in our amap
1434 */
1435
1436 UVMHIST_LOG(maphist, " case 1 fault: anon=%#jx",
1437 (uintptr_t)anon, 0, 0, 0);
1438
1439 /*
1440 * no matter if we have case 1A or case 1B we are going to need to
1441 * have the anon's memory resident. ensure that now.
1442 */
1443
1444 /*
1445 * let uvmfault_anonget do the dirty work.
1446 * if it fails (!OK) it will unlock everything for us.
1447 * if it succeeds, locks are still valid and locked.
1448 * also, if it is OK, then the anon's page is on the queues.
1449 * if the page is on loan from a uvm_object, then anonget will
1450 * lock that object for us if it does not fail.
1451 */
1452 retry:
1453 error = uvmfault_anonget(ufi, amap, anon);
1454 switch (error) {
1455 case 0:
1456 break;
1457
1458 case ERESTART:
1459 return ERESTART;
1460
1461 case EAGAIN:
1462 kpause("fltagain1", false, hz/2, NULL);
1463 return ERESTART;
1464
1465 case ENOLCK:
1466 /* it needs a write lock: retry */
1467 error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
1468 if (error != 0) {
1469 return error;
1470 }
1471 KASSERT(rw_write_held(amap->am_lock));
1472 goto retry;
1473
1474 default:
1475 return error;
1476 }
1477
1478 /*
1479 * uobj is non null if the page is on loan from an object (i.e. uobj)
1480 */
1481
1482 uobj = anon->an_page->uobject; /* locked by anonget if !NULL */
1483
1484 /* locked: maps(read), amap, anon, uobj(if one) */
1485 KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1486 KASSERT(anon->an_lock == amap->am_lock);
1487 KASSERT(uobj == NULL ||
1488 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1489
1490 /*
1491 * special handling for loaned pages
1492 */
1493
1494 if (anon->an_page->loan_count) {
1495 error = uvm_fault_upper_loan(ufi, flt, anon, &uobj);
1496 if (error != 0)
1497 return error;
1498 }
1499
1500 /*
1501 * if we are case 1B then we will need to allocate a new blank
1502 * anon to transfer the data into. note that we have a lock
1503 * on anon, so no one can busy or release the page until we are done.
1504 * also note that the ref count can't drop to zero here because
1505 * it is > 1 and we are only dropping one ref.
1506 *
1507 * in the (hopefully very rare) case that we are out of RAM we
1508 * will unlock, wait for more RAM, and refault.
1509 *
1510 * if we are out of anon VM we kill the process (XXX: could wait?).
1511 */
1512
1513 if (flt->cow_now && anon->an_ref > 1) {
1514 flt->promote = true;
1515 error = uvm_fault_upper_promote(ufi, flt, uobj, anon);
1516 } else {
1517 error = uvm_fault_upper_direct(ufi, flt, uobj, anon);
1518 }
1519 return error;
1520 }
1521
1522 /*
1523 * uvm_fault_upper_loan: handle loaned upper page.
1524 *
1525 * 1. if not cow'ing now, simply adjust flt->enter_prot.
1526 * 2. if cow'ing now, and if ref count is 1, break loan.
1527 */
1528
1529 static int
1530 uvm_fault_upper_loan(
1531 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1532 struct vm_anon *anon, struct uvm_object **ruobj)
1533 {
1534 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1535 int error = 0;
1536 UVMHIST_FUNC("uvm_fault_upper_loan"); UVMHIST_CALLED(maphist);
1537
1538 if (!flt->cow_now) {
1539
1540 /*
1541 * for read faults on loaned pages we just cap the
1542 * protection at read-only.
1543 */
1544
1545 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1546
1547 } else {
1548 /*
1549 * note that we can't allow writes into a loaned page!
1550 *
1551 * if we have a write fault on a loaned page in an
1552 * anon then we need to look at the anon's ref count.
1553 * if it is greater than one then we are going to do
1554 * a normal copy-on-write fault into a new anon (this
1555 * is not a problem). however, if the reference count
1556 * is one (a case where we would normally allow a
1557 * write directly to the page) then we need to kill
1558 * the loan before we continue.
1559 */
1560
1561 /* >1 case is already ok */
1562 if (anon->an_ref == 1) {
1563 /* breaking loan requires a write lock. */
1564 error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
1565 if (error != 0) {
1566 return error;
1567 }
1568 KASSERT(rw_write_held(amap->am_lock));
1569
1570 error = uvm_loanbreak_anon(anon, *ruobj);
1571 if (error != 0) {
1572 uvmfault_unlockall(ufi, amap, *ruobj);
1573 uvm_wait("flt_noram2");
1574 return ERESTART;
1575 }
1576 /* if we were a loan receiver uobj is gone */
1577 if (*ruobj)
1578 *ruobj = NULL;
1579 }
1580 }
1581 return error;
1582 }
1583
1584 /*
1585 * uvm_fault_upper_promote: promote upper page.
1586 *
1587 * 1. call uvmfault_promote.
1588 * 2. enqueue page.
1589 * 3. deref.
1590 * 4. pass page to uvm_fault_upper_enter.
1591 */
1592
1593 static int
1594 uvm_fault_upper_promote(
1595 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1596 struct uvm_object *uobj, struct vm_anon *anon)
1597 {
1598 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1599 struct vm_anon * const oanon = anon;
1600 struct vm_page *pg;
1601 int error;
1602 UVMHIST_FUNC("uvm_fault_upper_promote"); UVMHIST_CALLED(maphist);
1603
1604 UVMHIST_LOG(maphist, " case 1B: COW fault",0,0,0,0);
1605 cpu_count(CPU_COUNT_FLT_ACOW, 1);
1606
1607 /* promoting requires a write lock. */
1608 error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
1609 if (error != 0) {
1610 return error;
1611 }
1612 KASSERT(rw_write_held(amap->am_lock));
1613
1614 error = uvmfault_promote(ufi, oanon, PGO_DONTCARE, &anon,
1615 &flt->anon_spare);
1616 switch (error) {
1617 case 0:
1618 break;
1619 case ERESTART:
1620 return ERESTART;
1621 default:
1622 return error;
1623 }
1624 pg = anon->an_page;
1625
1626 KASSERT(anon->an_lock == oanon->an_lock);
1627 KASSERT((pg->flags & (PG_BUSY | PG_FAKE)) == 0);
1628
1629 /* deref: can not drop to zero here by defn! */
1630 KASSERT(oanon->an_ref > 1);
1631 oanon->an_ref--;
1632
1633 /*
1634 * note: oanon is still locked, as is the new anon. we
1635 * need to check for this later when we unlock oanon; if
1636 * oanon != anon, we'll have to unlock anon, too.
1637 */
1638
1639 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
1640 }
1641
1642 /*
1643 * uvm_fault_upper_direct: handle direct fault.
1644 */
1645
1646 static int
1647 uvm_fault_upper_direct(
1648 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1649 struct uvm_object *uobj, struct vm_anon *anon)
1650 {
1651 struct vm_anon * const oanon = anon;
1652 struct vm_page *pg;
1653 UVMHIST_FUNC("uvm_fault_upper_direct"); UVMHIST_CALLED(maphist);
1654
1655 cpu_count(CPU_COUNT_FLT_ANON, 1);
1656 pg = anon->an_page;
1657 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */
1658 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1659
1660 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
1661 }
1662
1663 /*
1664 * uvm_fault_upper_enter: enter h/w mapping of upper page.
1665 */
1666
1667 static int
1668 uvm_fault_upper_enter(
1669 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1670 struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg,
1671 struct vm_anon *oanon)
1672 {
1673 struct pmap *pmap = ufi->orig_map->pmap;
1674 vaddr_t va = ufi->orig_rvaddr;
1675 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1676 UVMHIST_FUNC("uvm_fault_upper_enter"); UVMHIST_CALLED(maphist);
1677
1678 /* locked: maps(read), amap, oanon, anon(if different from oanon) */
1679 KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1680 KASSERT(anon->an_lock == amap->am_lock);
1681 KASSERT(oanon->an_lock == amap->am_lock);
1682 KASSERT(uobj == NULL ||
1683 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1684 KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);
1685
1686 /*
1687 * now map the page in.
1688 */
1689
1690 UVMHIST_LOG(maphist,
1691 " MAPPING: anon: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
1692 (uintptr_t)pmap, va, (uintptr_t)pg, flt->promote);
1693 if (pmap_enter(pmap, va, VM_PAGE_TO_PHYS(pg),
1694 flt->enter_prot, flt->access_type | PMAP_CANFAIL |
1695 (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
1696
1697 /*
1698 * If pmap_enter() fails, it must not leave behind an existing
1699 * pmap entry. In particular, a now-stale entry for a different
1700 * page would leave the pmap inconsistent with the vm_map.
1701 * This is not to imply that pmap_enter() should remove an
1702 * existing mapping in such a situation (since that could create
1703 * different problems, eg. if the existing mapping is wired),
1704 * but rather that the pmap should be designed such that it
1705 * never needs to fail when the new mapping is replacing an
1706 * existing mapping and the new page has no existing mappings.
1707 *
1708 * XXX This can't be asserted safely any more because many
1709 * LWPs and/or many processes could simultaneously fault on
1710 * the same VA and some might succeed.
1711 */
1712
1713 /* KASSERT(!pmap_extract(pmap, va, NULL)); */
1714
1715 /*
1716 * ensure that the page is queued in the case that
1717 * we just promoted.
1718 */
1719
1720 uvm_pagelock(pg);
1721 uvm_pageenqueue(pg);
1722 uvm_pageunlock(pg);
1723
1724 /*
1725 * No need to undo what we did; we can simply think of
1726 * this as the pmap throwing away the mapping information.
1727 *
1728 * We do, however, have to go through the ReFault path,
1729 * as the map may change while we're asleep.
1730 */
1731
1732 uvmfault_unlockall(ufi, amap, uobj);
1733 if (!uvm_reclaimable()) {
1734 UVMHIST_LOG(maphist,
1735 "<- failed. out of VM",0,0,0,0);
1736 /* XXX instrumentation */
1737 return ENOMEM;
1738 }
1739 /* XXX instrumentation */
1740 uvm_wait("flt_pmfail1");
1741 return ERESTART;
1742 }
1743
1744 uvm_fault_upper_done(ufi, flt, anon, pg);
1745
1746 /*
1747 * done case 1! finish up by unlocking everything and returning success
1748 */
1749
1750 pmap_update(pmap);
1751 uvmfault_unlockall(ufi, amap, uobj);
1752 return 0;
1753 }
1754
1755 /*
1756 * uvm_fault_upper_done: queue upper center page.
1757 */
1758
1759 static void
1760 uvm_fault_upper_done(
1761 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1762 struct vm_anon *anon, struct vm_page *pg)
1763 {
1764 const bool wire_paging = flt->wire_paging;
1765
1766 UVMHIST_FUNC("uvm_fault_upper_done"); UVMHIST_CALLED(maphist);
1767
1768 /*
1769 * ... update the page queues.
1770 */
1771
1772 if (wire_paging) {
1773 uvm_pagelock(pg);
1774 uvm_pagewire(pg);
1775 uvm_pageunlock(pg);
1776
1777 /*
1778 * since the now-wired page cannot be paged out,
1779 * release its swap resources for others to use.
1780 * and since an anon with no swap cannot be clean,
1781 * mark it dirty now.
1782 */
1783
1784 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
1785 uvm_anon_dropswap(anon);
1786 } else if (uvmpdpol_pageactivate_p(pg)) {
1787 /*
1788 * avoid re-activating the page unless needed,
1789 * to avoid false sharing on multiprocessor.
1790 */
1791
1792 uvm_pagelock(pg);
1793 uvm_pageactivate(pg);
1794 uvm_pageunlock(pg);
1795 }
1796 }
1797
1798 /*
1799 * uvm_fault_lower_upgrade: upgrade lower lock, reader -> writer
1800 */
1801
1802 static inline int
1803 uvm_fault_lower_upgrade(struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1804 struct vm_amap *amap, struct uvm_object *uobj, struct vm_page *uobjpage)
1805 {
1806
1807 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1808
1809 KASSERT(uobj != NULL);
1810 KASSERT(flt->lower_lock_type == rw_lock_op(uobj->vmobjlock));
1811
1812 /*
1813 * fast path.
1814 */
1815
1816 if (__predict_true(flt->lower_lock_type == RW_WRITER)) {
1817 return 0;
1818 }
1819
1820 /*
1821 * otherwise try for the upgrade. if we don't get it, unlock
1822 * everything, restart the fault and next time around get a writer
1823 * lock.
1824 */
1825
1826 flt->lower_lock_type = RW_WRITER;
1827 if (__predict_false(!rw_tryupgrade(uobj->vmobjlock))) {
1828 uvmfault_unlockall(ufi, amap, uobj);
1829 cpu_count(CPU_COUNT_FLTNOUP, 1);
1830 UVMHIST_LOG(maphist, " !upgrade lower", 0, 0,0,0);
1831 return ERESTART;
1832 }
1833 cpu_count(CPU_COUNT_FLTUP, 1);
1834 KASSERT(flt->lower_lock_type == rw_lock_op(uobj->vmobjlock));
1835 return 0;
1836 }
1837
1838 /*
1839 * uvm_fault_lower: handle lower fault.
1840 *
1841 * 1. check uobj
1842 * 1.1. if null, ZFOD.
1843 * 1.2. if not null, look up unnmapped neighbor pages.
1844 * 2. for center page, check if promote.
1845 * 2.1. ZFOD always needs promotion.
1846 * 2.2. other uobjs, when entry is marked COW (usually MAP_PRIVATE vnode).
1847 * 3. if uobj is not ZFOD and page is not found, do i/o.
1848 * 4. dispatch either direct / promote fault.
1849 */
1850
1851 static int
1852 uvm_fault_lower(
1853 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1854 struct vm_page **pages)
1855 {
1856 struct vm_amap *amap __diagused = ufi->entry->aref.ar_amap;
1857 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1858 struct vm_page *uobjpage;
1859 int error;
1860 UVMHIST_FUNC("uvm_fault_lower"); UVMHIST_CALLED(maphist);
1861
1862 /*
1863 * now, if the desired page is not shadowed by the amap and we have
1864 * a backing object that does not have a special fault routine, then
1865 * we ask (with pgo_get) the object for resident pages that we care
1866 * about and attempt to map them in. we do not let pgo_get block
1867 * (PGO_LOCKED).
1868 */
1869
1870 if (uobj == NULL) {
1871 /* zero fill; don't care neighbor pages */
1872 uobjpage = NULL;
1873 } else {
1874 uvm_fault_lower_lookup(ufi, flt, pages);
1875 uobjpage = pages[flt->centeridx];
1876 }
1877
1878 /*
1879 * note that at this point we are done with any front or back pages.
1880 * we are now going to focus on the center page (i.e. the one we've
1881 * faulted on). if we have faulted on the upper (anon) layer
1882 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
1883 * not touched it yet). if we have faulted on the bottom (uobj)
1884 * layer [i.e. case 2] and the page was both present and available,
1885 * then we've got a pointer to it as "uobjpage" and we've already
1886 * made it BUSY.
1887 */
1888
1889 /*
1890 * locked:
1891 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
1892 */
1893 KASSERT(amap == NULL ||
1894 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1895 KASSERT(uobj == NULL ||
1896 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1897
1898 /*
1899 * note that uobjpage can not be PGO_DONTCARE at this point. we now
1900 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we
1901 * have a backing object, check and see if we are going to promote
1902 * the data up to an anon during the fault.
1903 */
1904
1905 if (uobj == NULL) {
1906 uobjpage = PGO_DONTCARE;
1907 flt->promote = true; /* always need anon here */
1908 } else {
1909 KASSERT(uobjpage != PGO_DONTCARE);
1910 flt->promote = flt->cow_now && UVM_ET_ISCOPYONWRITE(ufi->entry);
1911 }
1912 UVMHIST_LOG(maphist, " case 2 fault: promote=%jd, zfill=%jd",
1913 flt->promote, (uobj == NULL), 0,0);
1914
1915 /*
1916 * if uobjpage is not null then we do not need to do I/O to get the
1917 * uobjpage.
1918 *
1919 * if uobjpage is null, then we need to unlock and ask the pager to
1920 * get the data for us. once we have the data, we need to reverify
1921 * the state the world. we are currently not holding any resources.
1922 */
1923
1924 if (uobjpage) {
1925 /* update rusage counters */
1926 curlwp->l_ru.ru_minflt++;
1927 } else {
1928 error = uvm_fault_lower_io(ufi, flt, &uobj, &uobjpage);
1929 if (error != 0)
1930 return error;
1931 }
1932
1933 /*
1934 * locked:
1935 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
1936 */
1937 KASSERT(amap == NULL ||
1938 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
1939 KASSERT(uobj == NULL ||
1940 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1941
1942 /*
1943 * notes:
1944 * - at this point uobjpage can not be NULL
1945 * - at this point uobjpage can not be PG_RELEASED (since we checked
1946 * for it above)
1947 * - at this point uobjpage could be waited on (handle later)
1948 * - uobjpage can be from a different object if tmpfs (vnode vs UAO)
1949 */
1950
1951 KASSERT(uobjpage != NULL);
1952 KASSERT(uobj == NULL ||
1953 uobjpage->uobject->vmobjlock == uobj->vmobjlock);
1954 KASSERT(uobj == NULL || !UVM_OBJ_IS_CLEAN(uobjpage->uobject) ||
1955 uvm_pagegetdirty(uobjpage) == UVM_PAGE_STATUS_CLEAN);
1956
1957 if (!flt->promote) {
1958 error = uvm_fault_lower_direct(ufi, flt, uobj, uobjpage);
1959 } else {
1960 error = uvm_fault_lower_promote(ufi, flt, uobj, uobjpage);
1961 }
1962 return error;
1963 }
1964
1965 /*
1966 * uvm_fault_lower_lookup: look up on-memory uobj pages.
1967 *
1968 * 1. get on-memory pages.
1969 * 2. if failed, give up (get only center page later).
1970 * 3. if succeeded, enter h/w mapping of neighbor pages.
1971 */
1972
1973 static void
1974 uvm_fault_lower_lookup(
1975 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1976 struct vm_page **pages)
1977 {
1978 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1979 int lcv, gotpages;
1980 vaddr_t currva;
1981 bool entered;
1982 UVMHIST_FUNC("uvm_fault_lower_lookup"); UVMHIST_CALLED(maphist);
1983
1984 rw_enter(uobj->vmobjlock, flt->lower_lock_type);
1985
1986 /*
1987 * Locked: maps(read), amap(if there), uobj
1988 */
1989
1990 cpu_count(CPU_COUNT_FLTLGET, 1);
1991 gotpages = flt->npages;
1992 (void) uobj->pgops->pgo_get(uobj,
1993 ufi->entry->offset + flt->startva - ufi->entry->start,
1994 pages, &gotpages, flt->centeridx,
1995 flt->access_type & MASK(ufi->entry), ufi->entry->advice,
1996 PGO_LOCKED);
1997
1998 KASSERT(rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
1999
2000 /*
2001 * check for pages to map, if we got any
2002 */
2003
2004 if (gotpages == 0) {
2005 pages[flt->centeridx] = NULL;
2006 return;
2007 }
2008
2009 entered = false;
2010 currva = flt->startva;
2011 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
2012 struct vm_page *curpg;
2013
2014 curpg = pages[lcv];
2015 if (curpg == NULL || curpg == PGO_DONTCARE) {
2016 continue;
2017 }
2018
2019 /*
2020 * in the case of tmpfs, the pages might be from a different
2021 * uvm_object. just make sure that they have the same lock.
2022 */
2023
2024 KASSERT(curpg->uobject->vmobjlock == uobj->vmobjlock);
2025 KASSERT((curpg->flags & PG_BUSY) == 0);
2026
2027 /*
2028 * leave the centre page for later. don't screw with
2029 * existing mappings (needless & expensive).
2030 */
2031
2032 if (lcv == flt->centeridx) {
2033 UVMHIST_LOG(maphist, " got uobjpage (%#jx) "
2034 "with locked get", (uintptr_t)curpg, 0, 0, 0);
2035 } else if (!pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
2036 uvm_fault_lower_neighbor(ufi, flt, currva, curpg);
2037 entered = true;
2038 }
2039 }
2040 if (entered) {
2041 pmap_update(ufi->orig_map->pmap);
2042 }
2043 }
2044
2045 /*
2046 * uvm_fault_lower_neighbor: enter h/w mapping of lower neighbor page.
2047 */
2048
2049 static void
2050 uvm_fault_lower_neighbor(
2051 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
2052 vaddr_t currva, struct vm_page *pg)
2053 {
2054 const bool readonly = uvm_pagereadonly_p(pg) || pg->loan_count > 0;
2055 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
2056
2057 /* locked: maps(read), amap(if there), uobj */
2058
2059 /*
2060 * calling pgo_get with PGO_LOCKED returns us pages which
2061 * are neither busy nor released, so we don't need to check
2062 * for this. we can just directly enter the pages.
2063 *
2064 * there wasn't a direct fault on the page, so avoid the cost of
2065 * activating it.
2066 */
2067
2068 if (!uvmpdpol_pageisqueued_p(pg) && pg->wire_count == 0) {
2069 uvm_pagelock(pg);
2070 uvm_pageenqueue(pg);
2071 uvm_pageunlock(pg);
2072 }
2073
2074 UVMHIST_LOG(maphist,
2075 " MAPPING: n obj: pm=%#jx, va=%#jx, pg=%#jx",
2076 (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
2077 cpu_count(CPU_COUNT_FLTNOMAP, 1);
2078
2079 /*
2080 * Since this page isn't the page that's actually faulting,
2081 * ignore pmap_enter() failures; it's not critical that we
2082 * enter these right now.
2083 * NOTE: page can't be waited on or PG_RELEASED because we've
2084 * held the lock the whole time we've had the handle.
2085 */
2086 KASSERT((pg->flags & PG_PAGEOUT) == 0);
2087 KASSERT((pg->flags & PG_RELEASED) == 0);
2088 KASSERT(!UVM_OBJ_IS_CLEAN(pg->uobject) ||
2089 uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_CLEAN);
2090 KASSERT((pg->flags & PG_BUSY) == 0);
2091 KASSERT(rw_lock_op(pg->uobject->vmobjlock) == flt->lower_lock_type);
2092
2093 const vm_prot_t mapprot =
2094 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
2095 flt->enter_prot & MASK(ufi->entry);
2096 const u_int mapflags =
2097 PMAP_CANFAIL | (flt->wire_mapping ? (mapprot | PMAP_WIRED) : 0);
2098 (void) pmap_enter(ufi->orig_map->pmap, currva,
2099 VM_PAGE_TO_PHYS(pg), mapprot, mapflags);
2100 }
2101
2102 /*
2103 * uvm_fault_lower_io: get lower page from backing store.
2104 *
2105 * 1. unlock everything, because i/o will block.
2106 * 2. call pgo_get.
2107 * 3. if failed, recover.
2108 * 4. if succeeded, relock everything and verify things.
2109 */
2110
2111 static int
2112 uvm_fault_lower_io(
2113 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2114 struct uvm_object **ruobj, struct vm_page **ruobjpage)
2115 {
2116 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2117 struct uvm_object *uobj = *ruobj;
2118 struct vm_page *pg;
2119 bool locked;
2120 int gotpages;
2121 int error;
2122 voff_t uoff;
2123 vm_prot_t access_type;
2124 int advice;
2125 UVMHIST_FUNC("uvm_fault_lower_io"); UVMHIST_CALLED(maphist);
2126
2127 /* update rusage counters */
2128 curlwp->l_ru.ru_majflt++;
2129
2130 /* grab everything we need from the entry before we unlock */
2131 uoff = (ufi->orig_rvaddr - ufi->entry->start) + ufi->entry->offset;
2132 access_type = flt->access_type & MASK(ufi->entry);
2133 advice = ufi->entry->advice;
2134
2135 /* Locked: maps(read), amap(if there), uobj */
2136 KASSERT(rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
2137
2138 /* Upgrade to a write lock if needed. */
2139 error = uvm_fault_lower_upgrade(ufi, flt, amap, uobj, NULL);
2140 if (error != 0) {
2141 return error;
2142 }
2143 uvmfault_unlockall(ufi, amap, NULL);
2144
2145 /* Locked: uobj(write) */
2146 KASSERT(rw_write_held(uobj->vmobjlock));
2147
2148 cpu_count(CPU_COUNT_FLTGET, 1);
2149 gotpages = 1;
2150 pg = NULL;
2151 error = uobj->pgops->pgo_get(uobj, uoff, &pg, &gotpages,
2152 0, access_type, advice, PGO_SYNCIO);
2153 /* locked: pg(if no error) */
2154
2155 /*
2156 * recover from I/O
2157 */
2158
2159 if (error) {
2160 if (error == EAGAIN) {
2161 UVMHIST_LOG(maphist,
2162 " pgo_get says TRY AGAIN!",0,0,0,0);
2163 kpause("fltagain2", false, hz/2, NULL);
2164 return ERESTART;
2165 }
2166
2167 #if 0
2168 KASSERT(error != ERESTART);
2169 #else
2170 /* XXXUEBS don't re-fault? */
2171 if (error == ERESTART)
2172 error = EIO;
2173 #endif
2174
2175 UVMHIST_LOG(maphist, "<- pgo_get failed (code %jd)",
2176 error, 0,0,0);
2177 return error;
2178 }
2179
2180 /*
2181 * re-verify the state of the world by first trying to relock
2182 * the maps. always relock the object.
2183 */
2184
2185 locked = uvmfault_relock(ufi);
2186 if (locked && amap)
2187 amap_lock(amap, flt->upper_lock_type);
2188
2189 /* might be changed */
2190 uobj = pg->uobject;
2191
2192 rw_enter(uobj->vmobjlock, flt->lower_lock_type);
2193 KASSERT((pg->flags & PG_BUSY) != 0);
2194 KASSERT(flt->lower_lock_type == RW_WRITER);
2195
2196 uvm_pagelock(pg);
2197 uvm_pageactivate(pg);
2198 uvm_pageunlock(pg);
2199
2200 /* locked(locked): maps(read), amap(if !null), uobj, pg */
2201 /* locked(!locked): uobj, pg */
2202
2203 /*
2204 * verify that the page has not be released and re-verify
2205 * that amap slot is still free. if there is a problem,
2206 * we unlock and clean up.
2207 */
2208
2209 if ((pg->flags & PG_RELEASED) != 0 ||
2210 (locked && amap && amap_lookup(&ufi->entry->aref,
2211 ufi->orig_rvaddr - ufi->entry->start))) {
2212 if (locked)
2213 uvmfault_unlockall(ufi, amap, NULL);
2214 locked = false;
2215 }
2216
2217 /*
2218 * unbusy/release the page.
2219 */
2220
2221 if ((pg->flags & PG_RELEASED) == 0) {
2222 pg->flags &= ~PG_BUSY;
2223 uvm_pagelock(pg);
2224 uvm_pagewakeup(pg);
2225 uvm_pageunlock(pg);
2226 UVM_PAGE_OWN(pg, NULL);
2227 } else {
2228 cpu_count(CPU_COUNT_FLTPGRELE, 1);
2229 uvm_pagefree(pg);
2230 }
2231
2232 /*
2233 * didn't get the lock? retry.
2234 */
2235
2236 if (locked == false) {
2237 UVMHIST_LOG(maphist,
2238 " wasn't able to relock after fault: retry",
2239 0,0,0,0);
2240 rw_exit(uobj->vmobjlock);
2241 return ERESTART;
2242 }
2243
2244 /*
2245 * we have the data in pg. we are holding object lock (so the page
2246 * can't be released on us).
2247 */
2248
2249 /* locked: maps(read), amap(if !null), uobj */
2250
2251 *ruobj = uobj;
2252 *ruobjpage = pg;
2253 return 0;
2254 }
2255
2256 /*
2257 * uvm_fault_lower_direct: fault lower center page
2258 *
2259 * 1. adjust flt->enter_prot.
2260 * 2. if page is loaned, resolve.
2261 */
2262
2263 int
2264 uvm_fault_lower_direct(
2265 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2266 struct uvm_object *uobj, struct vm_page *uobjpage)
2267 {
2268 struct vm_page *pg;
2269 UVMHIST_FUNC("uvm_fault_lower_direct"); UVMHIST_CALLED(maphist);
2270
2271 /*
2272 * we are not promoting. if the mapping is COW ensure that we
2273 * don't give more access than we should (e.g. when doing a read
2274 * fault on a COPYONWRITE mapping we want to map the COW page in
2275 * R/O even though the entry protection could be R/W).
2276 *
2277 * set "pg" to the page we want to map in (uobjpage, usually)
2278 */
2279
2280 cpu_count(CPU_COUNT_FLT_OBJ, 1);
2281 if (UVM_ET_ISCOPYONWRITE(ufi->entry) ||
2282 UVM_OBJ_NEEDS_WRITEFAULT(uobjpage->uobject))
2283 flt->enter_prot &= ~VM_PROT_WRITE;
2284 pg = uobjpage; /* map in the actual object */
2285
2286 KASSERT(uobjpage != PGO_DONTCARE);
2287
2288 /*
2289 * we are faulting directly on the page. be careful
2290 * about writing to loaned pages...
2291 */
2292
2293 if (uobjpage->loan_count) {
2294 uvm_fault_lower_direct_loan(ufi, flt, uobj, &pg, &uobjpage);
2295 }
2296 KASSERT(pg == uobjpage);
2297 KASSERT((pg->flags & PG_BUSY) == 0);
2298 return uvm_fault_lower_enter(ufi, flt, uobj, NULL, pg);
2299 }
2300
2301 /*
2302 * uvm_fault_lower_direct_loan: resolve loaned page.
2303 *
2304 * 1. if not cow'ing, adjust flt->enter_prot.
2305 * 2. if cow'ing, break loan.
2306 */
2307
2308 static int
2309 uvm_fault_lower_direct_loan(
2310 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2311 struct uvm_object *uobj, struct vm_page **rpg,
2312 struct vm_page **ruobjpage)
2313 {
2314 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2315 struct vm_page *pg;
2316 struct vm_page *uobjpage = *ruobjpage;
2317 int error;
2318 UVMHIST_FUNC("uvm_fault_lower_direct_loan"); UVMHIST_CALLED(maphist);
2319
2320 if (!flt->cow_now) {
2321 /* read fault: cap the protection at readonly */
2322 /* cap! */
2323 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
2324 } else {
2325 /*
2326 * write fault: must break the loan here. to do this
2327 * we need a write lock on the object.
2328 */
2329
2330 error = uvm_fault_lower_upgrade(ufi, flt, amap, uobj, uobjpage);
2331 if (error != 0) {
2332 return error;
2333 }
2334 KASSERT(rw_write_held(uobj->vmobjlock));
2335
2336 pg = uvm_loanbreak(uobjpage);
2337 if (pg == NULL) {
2338
2339 uvmfault_unlockall(ufi, amap, uobj);
2340 UVMHIST_LOG(maphist,
2341 " out of RAM breaking loan, waiting",
2342 0,0,0,0);
2343 cpu_count(CPU_COUNT_FLTNORAM, 1);
2344 uvm_wait("flt_noram4");
2345 return ERESTART;
2346 }
2347 *rpg = pg;
2348 *ruobjpage = pg;
2349
2350 /*
2351 * drop ownership of page while still holding object lock,
2352 * which won't be dropped until the page is entered.
2353 */
2354
2355 uvm_pagelock(pg);
2356 uvm_pagewakeup(pg);
2357 uvm_pageunlock(pg);
2358 pg->flags &= ~PG_BUSY;
2359 UVM_PAGE_OWN(pg, NULL);
2360 }
2361 return 0;
2362 }
2363
2364 /*
2365 * uvm_fault_lower_promote: promote lower page.
2366 *
2367 * 1. call uvmfault_promote.
2368 * 2. fill in data.
2369 * 3. if not ZFOD, dispose old page.
2370 */
2371
2372 int
2373 uvm_fault_lower_promote(
2374 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2375 struct uvm_object *uobj, struct vm_page *uobjpage)
2376 {
2377 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2378 struct vm_anon *anon;
2379 struct vm_page *pg;
2380 int error;
2381 UVMHIST_FUNC("uvm_fault_lower_promote"); UVMHIST_CALLED(maphist);
2382
2383 KASSERT(amap != NULL);
2384
2385 /* promoting requires a write lock. */
2386 error = uvm_fault_upper_upgrade(ufi, flt, amap, uobj);
2387 if (error != 0) {
2388 return error;
2389 }
2390 KASSERT(rw_write_held(amap->am_lock));
2391 KASSERT(uobj == NULL ||
2392 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
2393
2394 /*
2395 * If we are going to promote the data to an anon we
2396 * allocate a blank anon here and plug it into our amap.
2397 */
2398 error = uvmfault_promote(ufi, NULL, uobjpage, &anon, &flt->anon_spare);
2399 switch (error) {
2400 case 0:
2401 break;
2402 case ERESTART:
2403 return ERESTART;
2404 default:
2405 return error;
2406 }
2407
2408 pg = anon->an_page;
2409
2410 /*
2411 * Fill in the data.
2412 */
2413
2414 if (uobjpage != PGO_DONTCARE) {
2415 cpu_count(CPU_COUNT_FLT_PRCOPY, 1);
2416
2417 /*
2418 * promote to shared amap? make sure all sharing
2419 * procs see it
2420 */
2421
2422 if ((amap_flags(amap) & AMAP_SHARED) != 0) {
2423 pmap_page_protect(uobjpage, VM_PROT_NONE);
2424 /*
2425 * XXX: PAGE MIGHT BE WIRED!
2426 */
2427 }
2428
2429 UVMHIST_LOG(maphist,
2430 " promote uobjpage %#jx to anon/page %#jx/%#jx",
2431 (uintptr_t)uobjpage, (uintptr_t)anon, (uintptr_t)pg, 0);
2432
2433 } else {
2434 cpu_count(CPU_COUNT_FLT_PRZERO, 1);
2435
2436 /*
2437 * Page is zero'd and marked dirty by
2438 * uvmfault_promote().
2439 */
2440
2441 UVMHIST_LOG(maphist," zero fill anon/page %#jx/%#jx",
2442 (uintptr_t)anon, (uintptr_t)pg, 0, 0);
2443 }
2444
2445 return uvm_fault_lower_enter(ufi, flt, uobj, anon, pg);
2446 }
2447
2448 /*
2449 * uvm_fault_lower_enter: enter h/w mapping of lower page or anon page promoted
2450 * from the lower page.
2451 */
2452
2453 int
2454 uvm_fault_lower_enter(
2455 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
2456 struct uvm_object *uobj,
2457 struct vm_anon *anon, struct vm_page *pg)
2458 {
2459 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2460 const bool readonly = uvm_pagereadonly_p(pg);
2461 int error;
2462 UVMHIST_FUNC("uvm_fault_lower_enter"); UVMHIST_CALLED(maphist);
2463
2464 /*
2465 * Locked:
2466 *
2467 * maps(read), amap(if !null), uobj(if !null),
2468 * anon(if !null), pg(if anon), unlock_uobj(if !null)
2469 *
2470 * anon must be write locked (promotion). uobj can be either.
2471 *
2472 * Note: pg is either the uobjpage or the new page in the new anon.
2473 */
2474
2475 KASSERT(amap == NULL ||
2476 rw_lock_op(amap->am_lock) == flt->upper_lock_type);
2477 KASSERT(uobj == NULL ||
2478 rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
2479 KASSERT(anon == NULL || anon->an_lock == amap->am_lock);
2480
2481 /*
2482 * note that pg can't be PG_RELEASED or PG_BUSY since we did
2483 * not drop the object lock since the last time we checked.
2484 */
2485
2486 KASSERT((pg->flags & PG_RELEASED) == 0);
2487 KASSERT((pg->flags & PG_BUSY) == 0);
2488
2489 /*
2490 * all resources are present. we can now map it in and free our
2491 * resources.
2492 */
2493
2494 UVMHIST_LOG(maphist,
2495 " MAPPING: case2: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
2496 (uintptr_t)ufi->orig_map->pmap, ufi->orig_rvaddr,
2497 (uintptr_t)pg, flt->promote);
2498 KASSERTMSG((flt->access_type & VM_PROT_WRITE) == 0 || !readonly,
2499 "promote=%u cow_now=%u access_type=%x enter_prot=%x cow=%u "
2500 "entry=%p map=%p orig_rvaddr=%p pg=%p",
2501 flt->promote, flt->cow_now, flt->access_type, flt->enter_prot,
2502 UVM_ET_ISCOPYONWRITE(ufi->entry), ufi->entry, ufi->orig_map,
2503 (void *)ufi->orig_rvaddr, pg);
2504 KASSERT((flt->access_type & VM_PROT_WRITE) == 0 || !readonly);
2505 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr,
2506 VM_PAGE_TO_PHYS(pg),
2507 readonly ? flt->enter_prot & ~VM_PROT_WRITE : flt->enter_prot,
2508 flt->access_type | PMAP_CANFAIL |
2509 (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
2510
2511 /*
2512 * No need to undo what we did; we can simply think of
2513 * this as the pmap throwing away the mapping information.
2514 *
2515 * We do, however, have to go through the ReFault path,
2516 * as the map may change while we're asleep.
2517 */
2518
2519 /*
2520 * ensure that the page is queued in the case that
2521 * we just promoted the page.
2522 */
2523
2524 if (anon != NULL) {
2525 uvm_pagelock(pg);
2526 uvm_pageenqueue(pg);
2527 uvm_pagewakeup(pg);
2528 uvm_pageunlock(pg);
2529 }
2530
2531 uvmfault_unlockall(ufi, amap, uobj);
2532 if (!uvm_reclaimable()) {
2533 UVMHIST_LOG(maphist,
2534 "<- failed. out of VM",0,0,0,0);
2535 /* XXX instrumentation */
2536 error = ENOMEM;
2537 return error;
2538 }
2539 /* XXX instrumentation */
2540 uvm_wait("flt_pmfail2");
2541 return ERESTART;
2542 }
2543
2544 uvm_fault_lower_done(ufi, flt, uobj, pg);
2545 pmap_update(ufi->orig_map->pmap);
2546 uvmfault_unlockall(ufi, amap, uobj);
2547
2548 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
2549 return 0;
2550 }
2551
2552 /*
2553 * uvm_fault_lower_done: queue lower center page.
2554 */
2555
2556 void
2557 uvm_fault_lower_done(
2558 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
2559 struct uvm_object *uobj, struct vm_page *pg)
2560 {
2561
2562 UVMHIST_FUNC("uvm_fault_lower_done"); UVMHIST_CALLED(maphist);
2563
2564 if (flt->wire_paging) {
2565 uvm_pagelock(pg);
2566 uvm_pagewire(pg);
2567 uvm_pageunlock(pg);
2568 if (pg->flags & PG_AOBJ) {
2569
2570 /*
2571 * since the now-wired page cannot be paged out,
2572 * release its swap resources for others to use.
2573 * since an aobj page with no swap cannot be clean,
2574 * mark it dirty now.
2575 *
2576 * use pg->uobject here. if the page is from a
2577 * tmpfs vnode, the pages are backed by its UAO and
2578 * not the vnode.
2579 */
2580
2581 KASSERT(uobj != NULL);
2582 KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
2583 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
2584 uao_dropswap(pg->uobject, pg->offset >> PAGE_SHIFT);
2585 }
2586 } else if (uvmpdpol_pageactivate_p(pg)) {
2587 /*
2588 * avoid re-activating the page unless needed,
2589 * to avoid false sharing on multiprocessor.
2590 */
2591
2592 uvm_pagelock(pg);
2593 uvm_pageactivate(pg);
2594 uvm_pageunlock(pg);
2595 }
2596 }
2597
2598
2599 /*
2600 * uvm_fault_wire: wire down a range of virtual addresses in a map.
2601 *
2602 * => map may be read-locked by caller, but MUST NOT be write-locked.
2603 * => if map is read-locked, any operations which may cause map to
2604 * be write-locked in uvm_fault() must be taken care of by
2605 * the caller. See uvm_map_pageable().
2606 */
2607
2608 int
2609 uvm_fault_wire(struct vm_map *map, vaddr_t start, vaddr_t end,
2610 vm_prot_t access_type, int maxprot)
2611 {
2612 vaddr_t va;
2613 int error;
2614
2615 /*
2616 * now fault it in a page at a time. if the fault fails then we have
2617 * to undo what we have done. note that in uvm_fault VM_PROT_NONE
2618 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
2619 */
2620
2621 /*
2622 * XXX work around overflowing a vaddr_t. this prevents us from
2623 * wiring the last page in the address space, though.
2624 */
2625 if (start > end) {
2626 return EFAULT;
2627 }
2628
2629 for (va = start; va < end; va += PAGE_SIZE) {
2630 error = uvm_fault_internal(map, va, access_type,
2631 (maxprot ? UVM_FAULT_MAXPROT : 0) | UVM_FAULT_WIRE);
2632 if (error) {
2633 if (va != start) {
2634 uvm_fault_unwire(map, start, va);
2635 }
2636 return error;
2637 }
2638 }
2639 return 0;
2640 }
2641
2642 /*
2643 * uvm_fault_unwire(): unwire range of virtual space.
2644 */
2645
2646 void
2647 uvm_fault_unwire(struct vm_map *map, vaddr_t start, vaddr_t end)
2648 {
2649 vm_map_lock_read(map);
2650 uvm_fault_unwire_locked(map, start, end);
2651 vm_map_unlock_read(map);
2652 }
2653
2654 /*
2655 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
2656 *
2657 * => map must be at least read-locked.
2658 */
2659
2660 void
2661 uvm_fault_unwire_locked(struct vm_map *map, vaddr_t start, vaddr_t end)
2662 {
2663 struct vm_map_entry *entry, *oentry;
2664 pmap_t pmap = vm_map_pmap(map);
2665 vaddr_t va;
2666 paddr_t pa;
2667 struct vm_page *pg;
2668
2669 /*
2670 * we assume that the area we are unwiring has actually been wired
2671 * in the first place. this means that we should be able to extract
2672 * the PAs from the pmap. we also lock out the page daemon so that
2673 * we can call uvm_pageunwire.
2674 */
2675
2676 /*
2677 * find the beginning map entry for the region.
2678 */
2679
2680 KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map));
2681 if (uvm_map_lookup_entry(map, start, &entry) == false)
2682 panic("uvm_fault_unwire_locked: address not in map");
2683
2684 oentry = NULL;
2685 for (va = start; va < end; va += PAGE_SIZE) {
2686
2687 /*
2688 * find the map entry for the current address.
2689 */
2690
2691 KASSERT(va >= entry->start);
2692 while (va >= entry->end) {
2693 KASSERT(entry->next != &map->header &&
2694 entry->next->start <= entry->end);
2695 entry = entry->next;
2696 }
2697
2698 /*
2699 * lock it.
2700 */
2701
2702 if (entry != oentry) {
2703 if (oentry != NULL) {
2704 uvm_map_unlock_entry(oentry);
2705 }
2706 uvm_map_lock_entry(entry, RW_WRITER);
2707 oentry = entry;
2708 }
2709
2710 /*
2711 * if the entry is no longer wired, tell the pmap.
2712 */
2713
2714 if (!pmap_extract(pmap, va, &pa))
2715 continue;
2716
2717 if (VM_MAPENT_ISWIRED(entry) == 0)
2718 pmap_unwire(pmap, va);
2719
2720 pg = PHYS_TO_VM_PAGE(pa);
2721 if (pg) {
2722 uvm_pagelock(pg);
2723 uvm_pageunwire(pg);
2724 uvm_pageunlock(pg);
2725 }
2726 }
2727
2728 if (oentry != NULL) {
2729 uvm_map_unlock_entry(entry);
2730 }
2731 }
2732