uvm_fault.c revision 1.141 1 /* $NetBSD: uvm_fault.c,v 1.141 2010/02/01 08:16:32 uebayasi Exp $ */
2
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp
35 */
36
37 /*
38 * uvm_fault.c: fault handler
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.141 2010/02/01 08:16:32 uebayasi Exp $");
43
44 #include "opt_uvmhist.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/malloc.h>
51 #include <sys/mman.h>
52
53 #include <uvm/uvm.h>
54
55 /*
56 *
57 * a word on page faults:
58 *
59 * types of page faults we handle:
60 *
61 * CASE 1: upper layer faults CASE 2: lower layer faults
62 *
63 * CASE 1A CASE 1B CASE 2A CASE 2B
64 * read/write1 write>1 read/write +-cow_write/zero
65 * | | | |
66 * +--|--+ +--|--+ +-----+ + | + | +-----+
67 * amap | V | | ---------> new | | | | ^ |
68 * +-----+ +-----+ +-----+ + | + | +--|--+
69 * | | |
70 * +-----+ +-----+ +--|--+ | +--|--+
71 * uobj | d/c | | d/c | | V | +----+ |
72 * +-----+ +-----+ +-----+ +-----+
73 *
74 * d/c = don't care
75 *
76 * case [0]: layerless fault
77 * no amap or uobj is present. this is an error.
78 *
79 * case [1]: upper layer fault [anon active]
80 * 1A: [read] or [write with anon->an_ref == 1]
81 * I/O takes place in upper level anon and uobj is not touched.
82 * 1B: [write with anon->an_ref > 1]
83 * new anon is alloc'd and data is copied off ["COW"]
84 *
85 * case [2]: lower layer fault [uobj]
86 * 2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
87 * I/O takes place directly in object.
88 * 2B: [write to copy_on_write] or [read on NULL uobj]
89 * data is "promoted" from uobj to a new anon.
90 * if uobj is null, then we zero fill.
91 *
92 * we follow the standard UVM locking protocol ordering:
93 *
94 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
95 * we hold a PG_BUSY page if we unlock for I/O
96 *
97 *
98 * the code is structured as follows:
99 *
100 * - init the "IN" params in the ufi structure
101 * ReFault:
102 * - do lookups [locks maps], check protection, handle needs_copy
103 * - check for case 0 fault (error)
104 * - establish "range" of fault
105 * - if we have an amap lock it and extract the anons
106 * - if sequential advice deactivate pages behind us
107 * - at the same time check pmap for unmapped areas and anon for pages
108 * that we could map in (and do map it if found)
109 * - check object for resident pages that we could map in
110 * - if (case 2) goto Case2
111 * - >>> handle case 1
112 * - ensure source anon is resident in RAM
113 * - if case 1B alloc new anon and copy from source
114 * - map the correct page in
115 * Case2:
116 * - >>> handle case 2
117 * - ensure source page is resident (if uobj)
118 * - if case 2B alloc new anon and copy from source (could be zero
119 * fill if uobj == NULL)
120 * - map the correct page in
121 * - done!
122 *
123 * note on paging:
124 * if we have to do I/O we place a PG_BUSY page in the correct object,
125 * unlock everything, and do the I/O. when I/O is done we must reverify
126 * the state of the world before assuming that our data structures are
127 * valid. [because mappings could change while the map is unlocked]
128 *
129 * alternative 1: unbusy the page in question and restart the page fault
130 * from the top (ReFault). this is easy but does not take advantage
131 * of the information that we already have from our previous lookup,
132 * although it is possible that the "hints" in the vm_map will help here.
133 *
134 * alternative 2: the system already keeps track of a "version" number of
135 * a map. [i.e. every time you write-lock a map (e.g. to change a
136 * mapping) you bump the version number up by one...] so, we can save
137 * the version number of the map before we release the lock and start I/O.
138 * then when I/O is done we can relock and check the version numbers
139 * to see if anything changed. this might save us some over 1 because
140 * we don't have to unbusy the page and may be less compares(?).
141 *
142 * alternative 3: put in backpointers or a way to "hold" part of a map
143 * in place while I/O is in progress. this could be complex to
144 * implement (especially with structures like amap that can be referenced
145 * by multiple map entries, and figuring out what should wait could be
146 * complex as well...).
147 *
148 * we use alternative 2. given that we are multi-threaded now we may want
149 * to reconsider the choice.
150 */
151
152 /*
153 * local data structures
154 */
155
156 struct uvm_advice {
157 int advice;
158 int nback;
159 int nforw;
160 };
161
162 /*
163 * page range array:
164 * note: index in array must match "advice" value
165 * XXX: borrowed numbers from freebsd. do they work well for us?
166 */
167
168 static const struct uvm_advice uvmadvice[] = {
169 { MADV_NORMAL, 3, 4 },
170 { MADV_RANDOM, 0, 0 },
171 { MADV_SEQUENTIAL, 8, 7},
172 };
173
174 #define UVM_MAXRANGE 16 /* must be MAX() of nback+nforw+1 */
175
176 /*
177 * private prototypes
178 */
179
180 /*
181 * inline functions
182 */
183
184 /*
185 * uvmfault_anonflush: try and deactivate pages in specified anons
186 *
187 * => does not have to deactivate page if it is busy
188 */
189
190 static inline void
191 uvmfault_anonflush(struct vm_anon **anons, int n)
192 {
193 int lcv;
194 struct vm_page *pg;
195
196 for (lcv = 0 ; lcv < n ; lcv++) {
197 if (anons[lcv] == NULL)
198 continue;
199 mutex_enter(&anons[lcv]->an_lock);
200 pg = anons[lcv]->an_page;
201 if (pg && (pg->flags & PG_BUSY) == 0) {
202 mutex_enter(&uvm_pageqlock);
203 if (pg->wire_count == 0) {
204 uvm_pagedeactivate(pg);
205 }
206 mutex_exit(&uvm_pageqlock);
207 }
208 mutex_exit(&anons[lcv]->an_lock);
209 }
210 }
211
212 /*
213 * normal functions
214 */
215
216 /*
217 * uvmfault_amapcopy: clear "needs_copy" in a map.
218 *
219 * => called with VM data structures unlocked (usually, see below)
220 * => we get a write lock on the maps and clear needs_copy for a VA
221 * => if we are out of RAM we sleep (waiting for more)
222 */
223
224 static void
225 uvmfault_amapcopy(struct uvm_faultinfo *ufi)
226 {
227 for (;;) {
228
229 /*
230 * no mapping? give up.
231 */
232
233 if (uvmfault_lookup(ufi, true) == false)
234 return;
235
236 /*
237 * copy if needed.
238 */
239
240 if (UVM_ET_ISNEEDSCOPY(ufi->entry))
241 amap_copy(ufi->map, ufi->entry, AMAP_COPY_NOWAIT,
242 ufi->orig_rvaddr, ufi->orig_rvaddr + 1);
243
244 /*
245 * didn't work? must be out of RAM. unlock and sleep.
246 */
247
248 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
249 uvmfault_unlockmaps(ufi, true);
250 uvm_wait("fltamapcopy");
251 continue;
252 }
253
254 /*
255 * got it! unlock and return.
256 */
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 * => maps, amap, and anon locked by caller.
269 * => if we fail (result != 0) we unlock everything.
270 * => if we are successful, we return with everything still locked.
271 * => we don't move the page on the queues [gets moved later]
272 * => if we allocate a new page [we_own], it gets put on the queues.
273 * either way, the result is that the page is on the queues at return time
274 * => for pages which are on loan from a uvm_object (and thus are not
275 * owned by the anon): if successful, we return with the owning object
276 * locked. the caller must unlock this object when it unlocks everything
277 * else.
278 */
279
280 int
281 uvmfault_anonget(struct uvm_faultinfo *ufi, struct vm_amap *amap,
282 struct vm_anon *anon)
283 {
284 bool we_own; /* we own anon's page? */
285 bool locked; /* did we relock? */
286 struct vm_page *pg;
287 int error;
288 UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist);
289
290 KASSERT(mutex_owned(&anon->an_lock));
291
292 error = 0;
293 uvmexp.fltanget++;
294 /* bump rusage counters */
295 if (anon->an_page)
296 curlwp->l_ru.ru_minflt++;
297 else
298 curlwp->l_ru.ru_majflt++;
299
300 /*
301 * loop until we get it, or fail.
302 */
303
304 for (;;) {
305 we_own = false; /* true if we set PG_BUSY on a page */
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_lockpage() to ensure
311 * the real owner of the page has been identified and locked.
312 */
313
314 if (pg && pg->loan_count)
315 pg = uvm_anon_lockloanpg(anon);
316
317 /*
318 * page there? make sure it is not busy/released.
319 */
320
321 if (pg) {
322
323 /*
324 * at this point, if the page has a uobject [meaning
325 * we have it on loan], then that uobject is locked
326 * by us! if the page is busy, we drop all the
327 * locks (including uobject) and try again.
328 */
329
330 if ((pg->flags & PG_BUSY) == 0) {
331 UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
332 return (0);
333 }
334 pg->flags |= PG_WANTED;
335 uvmexp.fltpgwait++;
336
337 /*
338 * the last unlock must be an atomic unlock+wait on
339 * the owner of page
340 */
341
342 if (pg->uobject) { /* owner is uobject ? */
343 uvmfault_unlockall(ufi, amap, NULL, anon);
344 UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
345 0,0,0);
346 UVM_UNLOCK_AND_WAIT(pg,
347 &pg->uobject->vmobjlock,
348 false, "anonget1",0);
349 } else {
350 /* anon owns page */
351 uvmfault_unlockall(ufi, amap, NULL, NULL);
352 UVMHIST_LOG(maphist, " unlock+wait on anon",0,
353 0,0,0);
354 UVM_UNLOCK_AND_WAIT(pg,&anon->an_lock,0,
355 "anonget2",0);
356 }
357 } else {
358 #if defined(VMSWAP)
359
360 /*
361 * no page, we must try and bring it in.
362 */
363
364 pg = uvm_pagealloc(NULL, 0, anon, 0);
365 if (pg == NULL) { /* out of RAM. */
366 uvmfault_unlockall(ufi, amap, NULL, anon);
367 uvmexp.fltnoram++;
368 UVMHIST_LOG(maphist, " noram -- UVM_WAIT",0,
369 0,0,0);
370 if (!uvm_reclaimable()) {
371 return ENOMEM;
372 }
373 uvm_wait("flt_noram1");
374 } else {
375 /* we set the PG_BUSY bit */
376 we_own = true;
377 uvmfault_unlockall(ufi, amap, NULL, anon);
378
379 /*
380 * we are passing a PG_BUSY+PG_FAKE+PG_CLEAN
381 * page into the uvm_swap_get function with
382 * all data structures unlocked. note that
383 * it is ok to read an_swslot here because
384 * we hold PG_BUSY on the page.
385 */
386 uvmexp.pageins++;
387 error = uvm_swap_get(pg, anon->an_swslot,
388 PGO_SYNCIO);
389
390 /*
391 * we clean up after the i/o below in the
392 * "we_own" case
393 */
394 }
395 #else /* defined(VMSWAP) */
396 panic("%s: no page", __func__);
397 #endif /* defined(VMSWAP) */
398 }
399
400 /*
401 * now relock and try again
402 */
403
404 locked = uvmfault_relock(ufi);
405 if (locked && amap != NULL) {
406 amap_lock(amap);
407 }
408 if (locked || we_own)
409 mutex_enter(&anon->an_lock);
410
411 /*
412 * if we own the page (i.e. we set PG_BUSY), then we need
413 * to clean up after the I/O. there are three cases to
414 * consider:
415 * [1] page released during I/O: free anon and ReFault.
416 * [2] I/O not OK. free the page and cause the fault
417 * to fail.
418 * [3] I/O OK! activate the page and sync with the
419 * non-we_own case (i.e. drop anon lock if not locked).
420 */
421
422 if (we_own) {
423 #if defined(VMSWAP)
424 if (pg->flags & PG_WANTED) {
425 wakeup(pg);
426 }
427 if (error) {
428
429 /*
430 * remove the swap slot from the anon
431 * and mark the anon as having no real slot.
432 * don't free the swap slot, thus preventing
433 * it from being used again.
434 */
435
436 if (anon->an_swslot > 0)
437 uvm_swap_markbad(anon->an_swslot, 1);
438 anon->an_swslot = SWSLOT_BAD;
439
440 if ((pg->flags & PG_RELEASED) != 0)
441 goto released;
442
443 /*
444 * note: page was never !PG_BUSY, so it
445 * can't be mapped and thus no need to
446 * pmap_page_protect it...
447 */
448
449 mutex_enter(&uvm_pageqlock);
450 uvm_pagefree(pg);
451 mutex_exit(&uvm_pageqlock);
452
453 if (locked)
454 uvmfault_unlockall(ufi, amap, NULL,
455 anon);
456 else
457 mutex_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 unlocked amap.
468 */
469
470 if (locked)
471 uvmfault_unlockall(ufi, amap, NULL,
472 NULL);
473
474 uvm_anon_release(anon);
475
476 if (error) {
477 UVMHIST_LOG(maphist,
478 "<- ERROR/RELEASED", 0,0,0,0);
479 return error;
480 }
481
482 UVMHIST_LOG(maphist, "<- RELEASED", 0,0,0,0);
483 return ERESTART;
484 }
485
486 /*
487 * we've successfully read the page, activate it.
488 */
489
490 mutex_enter(&uvm_pageqlock);
491 uvm_pageactivate(pg);
492 mutex_exit(&uvm_pageqlock);
493 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
494 UVM_PAGE_OWN(pg, NULL);
495 if (!locked)
496 mutex_exit(&anon->an_lock);
497 #else /* defined(VMSWAP) */
498 panic("%s: we_own", __func__);
499 #endif /* defined(VMSWAP) */
500 }
501
502 /*
503 * we were not able to relock. restart fault.
504 */
505
506 if (!locked) {
507 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
508 return (ERESTART);
509 }
510
511 /*
512 * verify no one has touched the amap and moved the anon on us.
513 */
514
515 if (ufi != NULL &&
516 amap_lookup(&ufi->entry->aref,
517 ufi->orig_rvaddr - ufi->entry->start) != anon) {
518
519 uvmfault_unlockall(ufi, amap, NULL, anon);
520 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
521 return (ERESTART);
522 }
523
524 /*
525 * try it again!
526 */
527
528 uvmexp.fltanretry++;
529 continue;
530 }
531 /*NOTREACHED*/
532 }
533
534 /*
535 * uvmfault_promote: promote data to a new anon. used for 1B and 2B.
536 *
537 * 1. allocate an anon and a page.
538 * 2. fill its contents.
539 * 3. put it into amap.
540 *
541 * => if we fail (result != 0) we unlock everything.
542 * => on success, return a new locked anon via 'nanon'.
543 * (*nanon)->an_page will be a resident, locked, dirty page.
544 */
545
546 static int
547 uvmfault_promote(struct uvm_faultinfo *ufi,
548 struct vm_anon *oanon,
549 struct vm_page *uobjpage,
550 struct vm_anon **nanon, /* OUT: allocated anon */
551 struct vm_anon **spare)
552 {
553 struct vm_amap *amap = ufi->entry->aref.ar_amap;
554 struct uvm_object *uobj;
555 struct vm_anon *anon;
556 struct vm_page *pg;
557 struct vm_page *opg;
558 int error;
559 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
560
561 if (oanon) {
562 /* anon COW */
563 opg = oanon->an_page;
564 KASSERT(opg != NULL);
565 KASSERT(opg->uobject == NULL || opg->loan_count > 0);
566 } else if (uobjpage != PGO_DONTCARE) {
567 /* object-backed COW */
568 opg = uobjpage;
569 } else {
570 /* ZFOD */
571 opg = NULL;
572 }
573 if (opg != NULL) {
574 uobj = opg->uobject;
575 } else {
576 uobj = NULL;
577 }
578
579 KASSERT(amap != NULL);
580 KASSERT(uobjpage != NULL);
581 KASSERT(uobjpage == PGO_DONTCARE || (uobjpage->flags & PG_BUSY) != 0);
582 KASSERT(mutex_owned(&amap->am_l));
583 KASSERT(oanon == NULL || mutex_owned(&oanon->an_lock));
584 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
585 #if 0
586 KASSERT(*spare == NULL || !mutex_owned(&(*spare)->an_lock));
587 #endif
588
589 if (*spare != NULL) {
590 anon = *spare;
591 *spare = NULL;
592 mutex_enter(&anon->an_lock);
593 } else if (ufi->map != kernel_map) {
594 anon = uvm_analloc();
595 } else {
596 UVMHIST_LOG(maphist, "kernel_map, unlock and retry", 0,0,0,0);
597
598 /*
599 * we can't allocate anons with kernel_map locked.
600 */
601
602 uvm_page_unbusy(&uobjpage, 1);
603 uvmfault_unlockall(ufi, amap, uobj, oanon);
604
605 *spare = uvm_analloc();
606 if (*spare == NULL) {
607 goto nomem;
608 }
609 mutex_exit(&(*spare)->an_lock);
610 error = ERESTART;
611 goto done;
612 }
613 if (anon) {
614
615 /*
616 * The new anon is locked.
617 *
618 * if opg == NULL, we want a zero'd, dirty page,
619 * so have uvm_pagealloc() do that for us.
620 */
621
622 pg = uvm_pagealloc(NULL, 0, anon,
623 (opg == NULL) ? UVM_PGA_ZERO : 0);
624 } else {
625 pg = NULL;
626 }
627
628 /*
629 * out of memory resources?
630 */
631
632 if (pg == NULL) {
633 /* save anon for the next try. */
634 if (anon != NULL) {
635 mutex_exit(&anon->an_lock);
636 *spare = anon;
637 }
638
639 /* unlock and fail ... */
640 uvm_page_unbusy(&uobjpage, 1);
641 uvmfault_unlockall(ufi, amap, uobj, oanon);
642 nomem:
643 if (!uvm_reclaimable()) {
644 UVMHIST_LOG(maphist, "out of VM", 0,0,0,0);
645 uvmexp.fltnoanon++;
646 error = ENOMEM;
647 goto done;
648 }
649
650 UVMHIST_LOG(maphist, "out of RAM, waiting for more", 0,0,0,0);
651 uvmexp.fltnoram++;
652 uvm_wait("flt_noram5");
653 error = ERESTART;
654 goto done;
655 }
656
657 /* copy page [pg now dirty] */
658 if (opg) {
659 uvm_pagecopy(opg, pg);
660 }
661
662 amap_add(&ufi->entry->aref, ufi->orig_rvaddr - ufi->entry->start, anon,
663 oanon != NULL);
664
665 *nanon = anon;
666 error = 0;
667 done:
668 return error;
669 }
670
671
672 /*
673 * F A U L T - m a i n e n t r y p o i n t
674 */
675
676 /*
677 * uvm_fault: page fault handler
678 *
679 * => called from MD code to resolve a page fault
680 * => VM data structures usually should be unlocked. however, it is
681 * possible to call here with the main map locked if the caller
682 * gets a write lock, sets it recusive, and then calls us (c.f.
683 * uvm_map_pageable). this should be avoided because it keeps
684 * the map locked off during I/O.
685 * => MUST NEVER BE CALLED IN INTERRUPT CONTEXT
686 */
687
688 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
689 ~VM_PROT_WRITE : VM_PROT_ALL)
690
691 /* fault_flag values passed from uvm_fault_wire to uvm_fault_internal */
692 #define UVM_FAULT_WIRE (1 << 0)
693 #define UVM_FAULT_MAXPROT (1 << 1)
694
695 struct uvm_faultctx {
696 vm_prot_t access_type;
697 vm_prot_t enter_prot;
698 bool wired;
699 bool narrow;
700 bool shadowed;
701 bool wire_fault;
702 bool maxprot;
703 bool cow_now;
704 int npages;
705 int centeridx;
706 vaddr_t startva;
707 struct vm_anon *anon_spare;
708 };
709
710 static int
711 uvm_fault_check(
712 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
713 struct vm_anon ***ranons, struct vm_page ***rpages);
714 typedef int
715 uvm_fault_subfunc_t(
716 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
717 struct vm_anon **anons, struct vm_page **pages);
718 static uvm_fault_subfunc_t uvm_fault_upper_lookup;
719 static uvm_fault_subfunc_t uvm_fault_upper;
720 static uvm_fault_subfunc_t uvm_fault_lower;
721 static uvm_fault_subfunc_t uvm_fault_lower_special;
722 static uvm_fault_subfunc_t uvm_fault_lower_generic_lookup;
723 static uvm_fault_subfunc_t uvm_fault_lower_generic;
724 static uvm_fault_subfunc_t uvm_fault_lower_generic1;
725 static uvm_fault_subfunc_t uvm_fault_lower_generic2;
726
727 int
728 uvm_fault_internal(struct vm_map *orig_map, vaddr_t vaddr,
729 vm_prot_t access_type, int fault_flag)
730 {
731 struct uvm_faultinfo ufi;
732 struct uvm_faultctx flt = {
733 .access_type = access_type,
734 .wire_fault = (fault_flag & UVM_FAULT_WIRE) != 0,
735 .maxprot = (fault_flag & UVM_FAULT_MAXPROT) != 0,
736 };
737 struct vm_anon *anons_store[UVM_MAXRANGE], **anons;
738 struct vm_page *pages_store[UVM_MAXRANGE], **pages;
739 int error;
740 UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist);
741
742 UVMHIST_LOG(maphist, "(map=0x%x, vaddr=0x%x, at=%d, ff=%d)",
743 orig_map, vaddr, access_type, fault_flag);
744
745 uvmexp.faults++; /* XXX: locking? */
746
747 /*
748 * init the IN parameters in the ufi
749 */
750
751 ufi.orig_map = orig_map;
752 ufi.orig_rvaddr = trunc_page(vaddr);
753 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */
754 if (flt.wire_fault)
755 flt.narrow = true; /* don't look for neighborhood
756 * pages on wire */
757 else
758 flt.narrow = false; /* normal fault */
759
760 /*
761 * "goto ReFault" means restart the page fault from ground zero.
762 */
763 ReFault:
764
765 anons = anons_store;
766 pages = pages_store;
767
768 error = uvm_fault_check(&ufi, &flt, &anons, &pages);
769 if (error == ERESTART)
770 goto ReFault;
771 else if (error)
772 goto done;
773
774 error = uvm_fault_upper_lookup(&ufi, &flt, anons, pages);
775 if (error == ERESTART)
776 goto ReFault;
777 else if (error)
778 goto done;
779
780 if (flt.shadowed == true)
781 error = uvm_fault_upper(&ufi, &flt, anons, pages);
782 else
783 error = uvm_fault_lower(&ufi, &flt, anons, pages);
784
785 if (error == ERESTART)
786 goto ReFault;
787
788 done:
789 if (flt.anon_spare != NULL) {
790 flt.anon_spare->an_ref--;
791 uvm_anfree(flt.anon_spare);
792 }
793 return error;
794 }
795
796 int
797 uvm_fault_check(
798 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
799 struct vm_anon ***ranons, struct vm_page ***rpages)
800 {
801 struct vm_amap *amap;
802 struct uvm_object *uobj;
803 vm_prot_t check_prot;
804 int nback, nforw;
805
806 /*
807 * lookup and lock the maps
808 */
809
810 if (uvmfault_lookup(ufi, false) == false) {
811 UVMHIST_LOG(maphist, "<- no mapping @ 0x%x", vaddr, 0,0,0);
812 return EFAULT;
813 }
814 /* locked: maps(read) */
815
816 #ifdef DIAGNOSTIC
817 if ((ufi->map->flags & VM_MAP_PAGEABLE) == 0) {
818 printf("Page fault on non-pageable map:\n");
819 printf("ufi->map = %p\n", ufi->map);
820 printf("ufi->orig_map = %p\n", ufi->orig_map);
821 printf("ufi->orig_rvaddr = 0x%lx\n", (u_long) ufi->orig_rvaddr);
822 panic("uvm_fault: (ufi->map->flags & VM_MAP_PAGEABLE) == 0");
823 }
824 #endif
825
826 /*
827 * check protection
828 */
829
830 check_prot = flt->maxprot ?
831 ufi->entry->max_protection : ufi->entry->protection;
832 if ((check_prot & flt->access_type) != flt->access_type) {
833 UVMHIST_LOG(maphist,
834 "<- protection failure (prot=0x%x, access=0x%x)",
835 ufi->entry->protection, flt->access_type, 0, 0);
836 uvmfault_unlockmaps(ufi, false);
837 return EACCES;
838 }
839
840 /*
841 * "enter_prot" is the protection we want to enter the page in at.
842 * for certain pages (e.g. copy-on-write pages) this protection can
843 * be more strict than ufi->entry->protection. "wired" means either
844 * the entry is wired or we are fault-wiring the pg.
845 */
846
847 flt->enter_prot = ufi->entry->protection;
848 flt->wired = VM_MAPENT_ISWIRED(ufi->entry) || flt->wire_fault;
849 if (flt->wired) {
850 flt->access_type = flt->enter_prot; /* full access for wired */
851 flt->cow_now = (check_prot & VM_PROT_WRITE) != 0;
852 } else {
853 flt->cow_now = (flt->access_type & VM_PROT_WRITE) != 0;
854 }
855
856 /*
857 * handle "needs_copy" case. if we need to copy the amap we will
858 * have to drop our readlock and relock it with a write lock. (we
859 * need a write lock to change anything in a map entry [e.g.
860 * needs_copy]).
861 */
862
863 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
864 if (flt->cow_now || (ufi->entry->object.uvm_obj == NULL)) {
865 KASSERT(!flt->maxprot);
866 /* need to clear */
867 UVMHIST_LOG(maphist,
868 " need to clear needs_copy and refault",0,0,0,0);
869 uvmfault_unlockmaps(ufi, false);
870 uvmfault_amapcopy(ufi);
871 uvmexp.fltamcopy++;
872 return ERESTART;
873
874 } else {
875
876 /*
877 * ensure that we pmap_enter page R/O since
878 * needs_copy is still true
879 */
880
881 flt->enter_prot &= ~VM_PROT_WRITE;
882 }
883 }
884
885 /*
886 * identify the players
887 */
888
889 amap = ufi->entry->aref.ar_amap; /* upper layer */
890 uobj = ufi->entry->object.uvm_obj; /* lower layer */
891
892 /*
893 * check for a case 0 fault. if nothing backing the entry then
894 * error now.
895 */
896
897 if (amap == NULL && uobj == NULL) {
898 uvmfault_unlockmaps(ufi, false);
899 UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
900 return EFAULT;
901 }
902
903 /*
904 * establish range of interest based on advice from mapper
905 * and then clip to fit map entry. note that we only want
906 * to do this the first time through the fault. if we
907 * ReFault we will disable this by setting "narrow" to true.
908 */
909
910 if (flt->narrow == false) {
911
912 /* wide fault (!narrow) */
913 KASSERT(uvmadvice[ufi->entry->advice].advice ==
914 ufi->entry->advice);
915 nback = MIN(uvmadvice[ufi->entry->advice].nback,
916 (ufi->orig_rvaddr - ufi->entry->start) >> PAGE_SHIFT);
917 flt->startva = ufi->orig_rvaddr - (nback << PAGE_SHIFT);
918 nforw = MIN(uvmadvice[ufi->entry->advice].nforw,
919 ((ufi->entry->end - ufi->orig_rvaddr) >>
920 PAGE_SHIFT) - 1);
921 /*
922 * note: "-1" because we don't want to count the
923 * faulting page as forw
924 */
925 flt->npages = nback + nforw + 1;
926 flt->centeridx = nback;
927
928 flt->narrow = true; /* ensure only once per-fault */
929
930 } else {
931
932 /* narrow fault! */
933 nback = nforw = 0;
934 flt->startva = ufi->orig_rvaddr;
935 flt->npages = 1;
936 flt->centeridx = 0;
937
938 }
939 /* offset from entry's start to pgs' start */
940 const voff_t eoff = flt->startva - ufi->entry->start;
941
942 /* locked: maps(read) */
943 UVMHIST_LOG(maphist, " narrow=%d, back=%d, forw=%d, startva=0x%x",
944 flt->narrow, nback, nforw, flt->startva);
945 UVMHIST_LOG(maphist, " entry=0x%x, amap=0x%x, obj=0x%x", ufi->entry,
946 amap, uobj, 0);
947
948 /*
949 * if we've got an amap, lock it and extract current anons.
950 */
951
952 if (amap) {
953 amap_lock(amap);
954 amap_lookups(&ufi->entry->aref, eoff, *ranons, flt->npages);
955 } else {
956 *ranons = NULL; /* to be safe */
957 }
958
959 /* locked: maps(read), amap(if there) */
960 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
961
962 /*
963 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
964 * now and then forget about them (for the rest of the fault).
965 */
966
967 if (ufi->entry->advice == MADV_SEQUENTIAL && nback != 0) {
968
969 UVMHIST_LOG(maphist, " MADV_SEQUENTIAL: flushing backpages",
970 0,0,0,0);
971 /* flush back-page anons? */
972 if (amap)
973 uvmfault_anonflush(*ranons, nback);
974
975 /* flush object? */
976 if (uobj) {
977 voff_t uoff;
978
979 uoff = ufi->entry->offset + eoff;
980 mutex_enter(&uobj->vmobjlock);
981 (void) (uobj->pgops->pgo_put)(uobj, uoff, uoff +
982 (nback << PAGE_SHIFT), PGO_DEACTIVATE);
983 }
984
985 /* now forget about the backpages */
986 if (amap)
987 *ranons += nback;
988 #if 0
989 /* XXXUEBS */
990 if (uobj)
991 *rpages += nback;
992 #endif
993 flt->startva += (nback << PAGE_SHIFT);
994 flt->npages -= nback;
995 flt->centeridx = 0;
996 }
997 /*
998 * => startva is fixed
999 * => npages is fixed
1000 */
1001
1002 return 0;
1003 }
1004
1005 int
1006 uvm_fault_upper_lookup(
1007 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1008 struct vm_anon **anons, struct vm_page **pages)
1009 {
1010 struct vm_amap *amap = ufi->entry->aref.ar_amap;
1011 int lcv;
1012 vaddr_t currva;
1013
1014 /* locked: maps(read), amap(if there) */
1015 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1016
1017 /*
1018 * map in the backpages and frontpages we found in the amap in hopes
1019 * of preventing future faults. we also init the pages[] array as
1020 * we go.
1021 */
1022
1023 currva = flt->startva;
1024 flt->shadowed = false;
1025 for (lcv = 0 ; lcv < flt->npages ; lcv++, currva += PAGE_SIZE) {
1026 struct vm_anon *anon;
1027
1028 /*
1029 * dont play with VAs that are already mapped
1030 * except for center)
1031 */
1032 if (lcv != flt->centeridx &&
1033 pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
1034 pages[lcv] = PGO_DONTCARE;
1035 continue;
1036 }
1037
1038 /*
1039 * unmapped or center page. check if any anon at this level.
1040 */
1041 if (amap == NULL || anons[lcv] == NULL) {
1042 pages[lcv] = NULL;
1043 continue;
1044 }
1045
1046 /*
1047 * check for present page and map if possible. re-activate it.
1048 */
1049
1050 pages[lcv] = PGO_DONTCARE;
1051 if (lcv == flt->centeridx) { /* save center for later! */
1052 flt->shadowed = true;
1053 continue;
1054 }
1055 anon = anons[lcv];
1056 mutex_enter(&anon->an_lock);
1057 /* ignore loaned pages */
1058 if (anon->an_page && anon->an_page->loan_count == 0 &&
1059 (anon->an_page->flags & PG_BUSY) == 0) {
1060 mutex_enter(&uvm_pageqlock);
1061 uvm_pageenqueue(anon->an_page);
1062 mutex_exit(&uvm_pageqlock);
1063 UVMHIST_LOG(maphist,
1064 " MAPPING: n anon: pm=0x%x, va=0x%x, pg=0x%x",
1065 ufi->orig_map->pmap, currva, anon->an_page, 0);
1066 uvmexp.fltnamap++;
1067
1068 /*
1069 * Since this isn't the page that's actually faulting,
1070 * ignore pmap_enter() failures; it's not critical
1071 * that we enter these right now.
1072 */
1073
1074 (void) pmap_enter(ufi->orig_map->pmap, currva,
1075 VM_PAGE_TO_PHYS(anon->an_page),
1076 (anon->an_ref > 1) ? (flt->enter_prot & ~VM_PROT_WRITE) :
1077 flt->enter_prot,
1078 PMAP_CANFAIL |
1079 (VM_MAPENT_ISWIRED(ufi->entry) ? PMAP_WIRED : 0));
1080 }
1081 pmap_update(ufi->orig_map->pmap);
1082 mutex_exit(&anon->an_lock);
1083 }
1084
1085 /* locked: maps(read), amap(if there) */
1086 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1087 /* (shadowed == true) if there is an anon at the faulting address */
1088 UVMHIST_LOG(maphist, " shadowed=%d, will_get=%d", flt->shadowed,
1089 (uobj && flt->shadowed == false),0,0);
1090
1091 /*
1092 * note that if we are really short of RAM we could sleep in the above
1093 * call to pmap_enter with everything locked. bad?
1094 *
1095 * XXX Actually, that is bad; pmap_enter() should just fail in that
1096 * XXX case. --thorpej
1097 */
1098
1099 return 0;
1100 }
1101
1102 static int
1103 uvm_fault_lower(
1104 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1105 struct vm_anon **anons, struct vm_page **pages)
1106 {
1107 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1108 int error;
1109
1110 /*
1111 * if the desired page is not shadowed by the amap and we have a
1112 * backing object, then we check to see if the backing object would
1113 * prefer to handle the fault itself (rather than letting us do it
1114 * with the usual pgo_get hook). the backing object signals this by
1115 * providing a pgo_fault routine.
1116 */
1117
1118 if (uobj && uobj->pgops->pgo_fault != NULL) {
1119 error = uvm_fault_lower_special(ufi, flt, anons, pages);
1120 } else {
1121 error = uvm_fault_lower_generic(ufi, flt, anons, pages);
1122 }
1123 return error;
1124 }
1125
1126 static int
1127 uvm_fault_lower_special(
1128 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1129 struct vm_anon **anons, struct vm_page **pages)
1130 {
1131 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1132 int error;
1133
1134 mutex_enter(&uobj->vmobjlock);
1135 /* locked: maps(read), amap (if there), uobj */
1136 error = uobj->pgops->pgo_fault(ufi, flt->startva, pages, flt->npages,
1137 flt->centeridx, flt->access_type, PGO_LOCKED|PGO_SYNCIO);
1138
1139 /* locked: nothing, pgo_fault has unlocked everything */
1140
1141 if (error == ERESTART)
1142 error = ERESTART; /* try again! */
1143 /*
1144 * object fault routine responsible for pmap_update().
1145 */
1146
1147 return error;
1148 }
1149
1150 static int
1151 uvm_fault_lower_generic(
1152 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1153 struct vm_anon **anons, struct vm_page **pages)
1154 {
1155 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1156
1157 /*
1158 * now, if the desired page is not shadowed by the amap and we have
1159 * a backing object that does not have a special fault routine, then
1160 * we ask (with pgo_get) the object for resident pages that we care
1161 * about and attempt to map them in. we do not let pgo_get block
1162 * (PGO_LOCKED).
1163 */
1164
1165 if (uobj == NULL) {
1166 /* zero fill; don't care neighbor pages */
1167 pages[flt->centeridx] = NULL;
1168 } else {
1169 uvm_fault_lower_generic_lookup(ufi, flt, anons, pages);
1170 }
1171 return uvm_fault_lower_generic1(ufi, flt, anons, pages);
1172 }
1173
1174 static int
1175 uvm_fault_lower_generic_lookup(
1176 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1177 struct vm_anon **anons, struct vm_page **pages)
1178 {
1179 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1180 int lcv, gotpages;
1181 vaddr_t currva;
1182
1183 mutex_enter(&uobj->vmobjlock);
1184 /* locked (!shadowed): maps(read), amap (if there), uobj */
1185 /*
1186 * the following call to pgo_get does _not_ change locking state
1187 */
1188
1189 uvmexp.fltlget++;
1190 gotpages = flt->npages;
1191 (void) uobj->pgops->pgo_get(uobj, ufi->entry->offset + flt->startva - ufi->entry->start,
1192 pages, &gotpages, flt->centeridx,
1193 flt->access_type & MASK(ufi->entry),
1194 ufi->entry->advice, PGO_LOCKED);
1195
1196 /*
1197 * check for pages to map, if we got any
1198 */
1199
1200 if (gotpages == 0) {
1201 pages[flt->centeridx] = NULL;
1202 return 0;
1203 }
1204
1205 currva = flt->startva;
1206 for (lcv = 0; lcv < flt->npages;
1207 lcv++, currva += PAGE_SIZE) {
1208 struct vm_page *curpg;
1209 bool readonly;
1210
1211 curpg = pages[lcv];
1212 if (curpg == NULL || curpg == PGO_DONTCARE) {
1213 continue;
1214 }
1215 KASSERT(curpg->uobject == uobj);
1216
1217 /*
1218 * if center page is resident and not
1219 * PG_BUSY|PG_RELEASED then pgo_get
1220 * made it PG_BUSY for us and gave
1221 * us a handle to it. remember this
1222 * page as "uobjpage." (for later use).
1223 */
1224
1225 if (lcv == flt->centeridx) {
1226 UVMHIST_LOG(maphist, " got uobjpage "
1227 "(0x%x) with locked get",
1228 curpg, 0,0,0);
1229 continue;
1230 }
1231
1232 /*
1233 * calling pgo_get with PGO_LOCKED returns us
1234 * pages which are neither busy nor released,
1235 * so we don't need to check for this.
1236 * we can just directly enter the pages.
1237 */
1238
1239 mutex_enter(&uvm_pageqlock);
1240 uvm_pageenqueue(curpg);
1241 mutex_exit(&uvm_pageqlock);
1242 UVMHIST_LOG(maphist,
1243 " MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x",
1244 ufi->orig_map->pmap, currva, curpg, 0);
1245 uvmexp.fltnomap++;
1246
1247 /*
1248 * Since this page isn't the page that's
1249 * actually faulting, ignore pmap_enter()
1250 * failures; it's not critical that we
1251 * enter these right now.
1252 */
1253 KASSERT((curpg->flags & PG_PAGEOUT) == 0);
1254 KASSERT((curpg->flags & PG_RELEASED) == 0);
1255 KASSERT(!UVM_OBJ_IS_CLEAN(curpg->uobject) ||
1256 (curpg->flags & PG_CLEAN) != 0);
1257 readonly = (curpg->flags & PG_RDONLY)
1258 || (curpg->loan_count > 0)
1259 || UVM_OBJ_NEEDS_WRITEFAULT(curpg->uobject);
1260
1261 (void) pmap_enter(ufi->orig_map->pmap, currva,
1262 VM_PAGE_TO_PHYS(curpg),
1263 readonly ?
1264 flt->enter_prot & ~VM_PROT_WRITE :
1265 flt->enter_prot & MASK(ufi->entry),
1266 PMAP_CANFAIL |
1267 (flt->wired ? PMAP_WIRED : 0));
1268
1269 /*
1270 * NOTE: page can't be PG_WANTED or PG_RELEASED
1271 * because we've held the lock the whole time
1272 * we've had the handle.
1273 */
1274 KASSERT((curpg->flags & PG_WANTED) == 0);
1275 KASSERT((curpg->flags & PG_RELEASED) == 0);
1276
1277 curpg->flags &= ~(PG_BUSY);
1278 UVM_PAGE_OWN(curpg, NULL);
1279 }
1280 pmap_update(ufi->orig_map->pmap);
1281 return 0;
1282 }
1283
1284 static int
1285 uvm_fault_lower_generic1(
1286 struct uvm_faultinfo *ufi, 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 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1291 struct vm_page *uobjpage = pages[flt->centeridx];
1292
1293 /* locked: maps(read), amap(if there), uobj(if !null), uobjpage(if !null) */
1294 KASSERT(!flt->shadowed);
1295 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1296 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
1297 KASSERT(uobjpage == NULL || (uobjpage->flags & PG_BUSY) != 0);
1298
1299 /*
1300 * note that at this point we are done with any front or back pages.
1301 * we are now going to focus on the center page (i.e. the one we've
1302 * faulted on). if we have faulted on the upper (anon) layer
1303 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
1304 * not touched it yet). if we have faulted on the bottom (uobj)
1305 * layer [i.e. case 2] and the page was both present and available,
1306 * then we've got a pointer to it as "uobjpage" and we've already
1307 * made it BUSY.
1308 */
1309
1310 /*
1311 * there are four possible cases we must address: 1A, 1B, 2A, and 2B
1312 */
1313
1314 /*
1315 * redirect case 2: if we are not shadowed, go to case 2.
1316 */
1317
1318 return uvm_fault_lower_generic2(ufi, flt, anons, pages);
1319 }
1320
1321 static int
1322 uvm_fault_upper(
1323 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1324 struct vm_anon **anons, struct vm_page **pages)
1325 {
1326 struct vm_amap *amap = ufi->entry->aref.ar_amap;
1327 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1328 struct vm_anon *anon, *oanon;
1329 int error;
1330
1331 /* locked: maps(read), amap */
1332 KASSERT(mutex_owned(&amap->am_l));
1333
1334 /*
1335 * handle case 1: fault on an anon in our amap
1336 */
1337
1338 anon = anons[flt->centeridx];
1339 UVMHIST_LOG(maphist, " case 1 fault: anon=0x%x", anon, 0,0,0);
1340 mutex_enter(&anon->an_lock);
1341
1342 /* locked: maps(read), amap, anon */
1343 KASSERT(mutex_owned(&amap->am_l));
1344 KASSERT(mutex_owned(&anon->an_lock));
1345
1346 /*
1347 * no matter if we have case 1A or case 1B we are going to need to
1348 * have the anon's memory resident. ensure that now.
1349 */
1350
1351 /*
1352 * let uvmfault_anonget do the dirty work.
1353 * if it fails (!OK) it will unlock everything for us.
1354 * if it succeeds, locks are still valid and locked.
1355 * also, if it is OK, then the anon's page is on the queues.
1356 * if the page is on loan from a uvm_object, then anonget will
1357 * lock that object for us if it does not fail.
1358 */
1359
1360 error = uvmfault_anonget(ufi, amap, anon);
1361 switch (error) {
1362 case 0:
1363 break;
1364
1365 case ERESTART:
1366 return ERESTART;
1367
1368 case EAGAIN:
1369 kpause("fltagain1", false, hz/2, NULL);
1370 return ERESTART;
1371
1372 default:
1373 return error;
1374 }
1375
1376 /*
1377 * uobj is non null if the page is on loan from an object (i.e. uobj)
1378 */
1379
1380 uobj = anon->an_page->uobject; /* locked by anonget if !NULL */
1381
1382 /* locked: maps(read), amap, anon, uobj(if one) */
1383 KASSERT(mutex_owned(&amap->am_l));
1384 KASSERT(mutex_owned(&anon->an_lock));
1385 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
1386
1387 /*
1388 * special handling for loaned pages
1389 */
1390
1391 if (anon->an_page->loan_count) {
1392
1393 if (!flt->cow_now) {
1394
1395 /*
1396 * for read faults on loaned pages we just cap the
1397 * protection at read-only.
1398 */
1399
1400 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1401
1402 } else {
1403 /*
1404 * note that we can't allow writes into a loaned page!
1405 *
1406 * if we have a write fault on a loaned page in an
1407 * anon then we need to look at the anon's ref count.
1408 * if it is greater than one then we are going to do
1409 * a normal copy-on-write fault into a new anon (this
1410 * is not a problem). however, if the reference count
1411 * is one (a case where we would normally allow a
1412 * write directly to the page) then we need to kill
1413 * the loan before we continue.
1414 */
1415
1416 /* >1 case is already ok */
1417 if (anon->an_ref == 1) {
1418 struct vm_page *pg;
1419
1420 /* get new un-owned replacement page */
1421 pg = uvm_pagealloc(NULL, 0, NULL, 0);
1422 if (pg == NULL) {
1423 uvmfault_unlockall(ufi, amap, uobj,
1424 anon);
1425 uvm_wait("flt_noram2");
1426 return ERESTART;
1427 }
1428
1429 /*
1430 * copy data, kill loan, and drop uobj lock
1431 * (if any)
1432 */
1433 /* copy old -> new */
1434 uvm_pagecopy(anon->an_page, pg);
1435
1436 /* force reload */
1437 pmap_page_protect(anon->an_page, VM_PROT_NONE);
1438 mutex_enter(&uvm_pageqlock); /* KILL loan */
1439
1440 anon->an_page->uanon = NULL;
1441 /* in case we owned */
1442 anon->an_page->pqflags &= ~PQ_ANON;
1443
1444 if (uobj) {
1445 /* if we were receiver of loan */
1446 anon->an_page->loan_count--;
1447 } else {
1448 /*
1449 * we were the lender (A->K); need
1450 * to remove the page from pageq's.
1451 */
1452 uvm_pagedequeue(anon->an_page);
1453 }
1454
1455 if (uobj) {
1456 mutex_exit(&uobj->vmobjlock);
1457 uobj = NULL;
1458 }
1459
1460 /* install new page in anon */
1461 anon->an_page = pg;
1462 pg->uanon = anon;
1463 pg->pqflags |= PQ_ANON;
1464
1465 uvm_pageactivate(pg);
1466 mutex_exit(&uvm_pageqlock);
1467
1468 pg->flags &= ~(PG_BUSY|PG_FAKE);
1469 UVM_PAGE_OWN(pg, NULL);
1470
1471 /* done! */
1472 } /* ref == 1 */
1473 } /* write fault */
1474 } /* loan count */
1475
1476 /*
1477 * if we are case 1B then we will need to allocate a new blank
1478 * anon to transfer the data into. note that we have a lock
1479 * on anon, so no one can busy or release the page until we are done.
1480 * also note that the ref count can't drop to zero here because
1481 * it is > 1 and we are only dropping one ref.
1482 *
1483 * in the (hopefully very rare) case that we are out of RAM we
1484 * will unlock, wait for more RAM, and refault.
1485 *
1486 * if we are out of anon VM we kill the process (XXX: could wait?).
1487 */
1488
1489 struct vm_page *pg;
1490 if (flt->cow_now && anon->an_ref > 1) {
1491
1492 UVMHIST_LOG(maphist, " case 1B: COW fault",0,0,0,0);
1493 uvmexp.flt_acow++;
1494 oanon = anon; /* oanon = old, locked anon */
1495
1496 error = uvmfault_promote(ufi, oanon, PGO_DONTCARE,
1497 &anon, &flt->anon_spare);
1498 switch (error) {
1499 case 0:
1500 break;
1501 case ERESTART:
1502 return ERESTART;
1503 default:
1504 return error;
1505 }
1506
1507 pg = anon->an_page;
1508 mutex_enter(&uvm_pageqlock);
1509 uvm_pageactivate(pg);
1510 mutex_exit(&uvm_pageqlock);
1511 pg->flags &= ~(PG_BUSY|PG_FAKE);
1512 UVM_PAGE_OWN(pg, NULL);
1513
1514 /* deref: can not drop to zero here by defn! */
1515 oanon->an_ref--;
1516
1517 /*
1518 * note: oanon is still locked, as is the new anon. we
1519 * need to check for this later when we unlock oanon; if
1520 * oanon != anon, we'll have to unlock anon, too.
1521 */
1522
1523 } else {
1524
1525 uvmexp.flt_anon++;
1526 oanon = anon; /* old, locked anon is same as anon */
1527 pg = anon->an_page;
1528 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */
1529 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1530
1531 }
1532
1533 /* locked: maps(read), amap, oanon, anon (if different from oanon) */
1534 KASSERT(mutex_owned(&amap->am_l));
1535 KASSERT(mutex_owned(&anon->an_lock));
1536 KASSERT(mutex_owned(&oanon->an_lock));
1537
1538 /*
1539 * now map the page in.
1540 */
1541
1542 UVMHIST_LOG(maphist, " MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x",
1543 ufi->orig_map->pmap, ufi->orig_rvaddr, pg, 0);
1544 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr, VM_PAGE_TO_PHYS(pg),
1545 flt->enter_prot, flt->access_type | PMAP_CANFAIL | (flt->wired ? PMAP_WIRED : 0))
1546 != 0) {
1547
1548 /*
1549 * No need to undo what we did; we can simply think of
1550 * this as the pmap throwing away the mapping information.
1551 *
1552 * We do, however, have to go through the ReFault path,
1553 * as the map may change while we're asleep.
1554 */
1555
1556 if (anon != oanon)
1557 mutex_exit(&anon->an_lock);
1558 uvmfault_unlockall(ufi, amap, uobj, oanon);
1559 if (!uvm_reclaimable()) {
1560 UVMHIST_LOG(maphist,
1561 "<- failed. out of VM",0,0,0,0);
1562 /* XXX instrumentation */
1563 error = ENOMEM;
1564 return error;
1565 }
1566 /* XXX instrumentation */
1567 uvm_wait("flt_pmfail1");
1568 return ERESTART;
1569 }
1570
1571 /*
1572 * ... update the page queues.
1573 */
1574
1575 mutex_enter(&uvm_pageqlock);
1576 if (flt->wire_fault) {
1577 uvm_pagewire(pg);
1578
1579 /*
1580 * since the now-wired page cannot be paged out,
1581 * release its swap resources for others to use.
1582 * since an anon with no swap cannot be PG_CLEAN,
1583 * clear its clean flag now.
1584 */
1585
1586 pg->flags &= ~(PG_CLEAN);
1587 uvm_anon_dropswap(anon);
1588 } else {
1589 uvm_pageactivate(pg);
1590 }
1591 mutex_exit(&uvm_pageqlock);
1592
1593 /*
1594 * done case 1! finish up by unlocking everything and returning success
1595 */
1596
1597 if (anon != oanon)
1598 mutex_exit(&anon->an_lock);
1599 uvmfault_unlockall(ufi, amap, uobj, oanon);
1600 pmap_update(ufi->orig_map->pmap);
1601 return 0;
1602 }
1603
1604 static int
1605 uvm_fault_lower_generic2(
1606 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1607 struct vm_anon **anons, struct vm_page **pages)
1608 {
1609 struct vm_amap *amap = ufi->entry->aref.ar_amap;
1610 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1611 struct vm_page *uobjpage = pages[flt->centeridx];
1612 struct vm_anon *anon;
1613 bool promote;
1614 int error;
1615
1616 /*
1617 * handle case 2: faulting on backing object or zero fill
1618 */
1619
1620 /*
1621 * locked:
1622 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
1623 */
1624 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1625 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
1626 KASSERT(uobjpage == NULL || (uobjpage->flags & PG_BUSY) != 0);
1627
1628 /*
1629 * note that uobjpage can not be PGO_DONTCARE at this point. we now
1630 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we
1631 * have a backing object, check and see if we are going to promote
1632 * the data up to an anon during the fault.
1633 */
1634
1635 if (uobj == NULL) {
1636 uobjpage = PGO_DONTCARE;
1637 promote = true; /* always need anon here */
1638 } else {
1639 KASSERT(uobjpage != PGO_DONTCARE);
1640 promote = flt->cow_now && UVM_ET_ISCOPYONWRITE(ufi->entry);
1641 }
1642 UVMHIST_LOG(maphist, " case 2 fault: promote=%d, zfill=%d",
1643 promote, (uobj == NULL), 0,0);
1644
1645 /*
1646 * if uobjpage is not null then we do not need to do I/O to get the
1647 * uobjpage.
1648 *
1649 * if uobjpage is null, then we need to unlock and ask the pager to
1650 * get the data for us. once we have the data, we need to reverify
1651 * the state the world. we are currently not holding any resources.
1652 */
1653
1654 if (uobjpage) {
1655 /* update rusage counters */
1656 curlwp->l_ru.ru_minflt++;
1657 } else {
1658 bool locked;
1659 int gotpages;
1660 voff_t uoff;
1661
1662 /* update rusage counters */
1663 curlwp->l_ru.ru_majflt++;
1664
1665 /* locked: maps(read), amap(if there), uobj */
1666 uvmfault_unlockall(ufi, amap, NULL, NULL);
1667 /* locked: uobj */
1668
1669 uvmexp.fltget++;
1670 gotpages = 1;
1671 uoff = (ufi->orig_rvaddr - ufi->entry->start) + ufi->entry->offset;
1672 error = uobj->pgops->pgo_get(uobj, uoff, &uobjpage, &gotpages,
1673 0, flt->access_type & MASK(ufi->entry), ufi->entry->advice,
1674 PGO_SYNCIO);
1675 /* locked: uobjpage(if no error) */
1676 KASSERT(error != 0 || (uobjpage->flags & PG_BUSY) != 0);
1677
1678 /*
1679 * recover from I/O
1680 */
1681
1682 if (error) {
1683 if (error == EAGAIN) {
1684 UVMHIST_LOG(maphist,
1685 " pgo_get says TRY AGAIN!",0,0,0,0);
1686 kpause("fltagain2", false, hz/2, NULL);
1687 return ERESTART;
1688 }
1689
1690 #if 0
1691 KASSERT(error != ERESTART);
1692 #else
1693 /* XXXUEBS don't re-fault? */
1694 if (error == ERESTART)
1695 error = EIO;
1696 #endif
1697
1698 UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)",
1699 error, 0,0,0);
1700 return error;
1701 }
1702
1703 /* locked: uobjpage */
1704
1705 mutex_enter(&uvm_pageqlock);
1706 uvm_pageactivate(uobjpage);
1707 mutex_exit(&uvm_pageqlock);
1708
1709 /*
1710 * re-verify the state of the world by first trying to relock
1711 * the maps. always relock the object.
1712 */
1713
1714 locked = uvmfault_relock(ufi);
1715 if (locked && amap)
1716 amap_lock(amap);
1717 uobj = uobjpage->uobject;
1718 mutex_enter(&uobj->vmobjlock);
1719
1720 /* locked(locked): maps(read), amap(if !null), uobj, uobjpage */
1721 /* locked(!locked): uobj, uobjpage */
1722
1723 /*
1724 * verify that the page has not be released and re-verify
1725 * that amap slot is still free. if there is a problem,
1726 * we unlock and clean up.
1727 */
1728
1729 if ((uobjpage->flags & PG_RELEASED) != 0 ||
1730 (locked && amap &&
1731 amap_lookup(&ufi->entry->aref,
1732 ufi->orig_rvaddr - ufi->entry->start))) {
1733 if (locked)
1734 uvmfault_unlockall(ufi, amap, NULL, NULL);
1735 locked = false;
1736 }
1737
1738 /*
1739 * didn't get the lock? release the page and retry.
1740 */
1741
1742 if (locked == false) {
1743 UVMHIST_LOG(maphist,
1744 " wasn't able to relock after fault: retry",
1745 0,0,0,0);
1746 if (uobjpage->flags & PG_WANTED)
1747 wakeup(uobjpage);
1748 if (uobjpage->flags & PG_RELEASED) {
1749 uvmexp.fltpgrele++;
1750 uvm_pagefree(uobjpage);
1751 return ERESTART;
1752 }
1753 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1754 UVM_PAGE_OWN(uobjpage, NULL);
1755 mutex_exit(&uobj->vmobjlock);
1756 return ERESTART;
1757 }
1758
1759 /*
1760 * we have the data in uobjpage which is busy and
1761 * not released. we are holding object lock (so the page
1762 * can't be released on us).
1763 */
1764
1765 /* locked: maps(read), amap(if !null), uobj, uobjpage */
1766 }
1767
1768 /*
1769 * locked:
1770 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
1771 */
1772 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1773 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
1774 KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0);
1775
1776 /*
1777 * notes:
1778 * - at this point uobjpage can not be NULL
1779 * - at this point uobjpage can not be PG_RELEASED (since we checked
1780 * for it above)
1781 * - at this point uobjpage could be PG_WANTED (handle later)
1782 */
1783
1784 KASSERT(uobj == NULL || uobj == uobjpage->uobject);
1785 KASSERT(uobj == NULL || !UVM_OBJ_IS_CLEAN(uobjpage->uobject) ||
1786 (uobjpage->flags & PG_CLEAN) != 0);
1787 struct vm_page *pg;
1788 if (promote == false) {
1789
1790 /*
1791 * we are not promoting. if the mapping is COW ensure that we
1792 * don't give more access than we should (e.g. when doing a read
1793 * fault on a COPYONWRITE mapping we want to map the COW page in
1794 * R/O even though the entry protection could be R/W).
1795 *
1796 * set "pg" to the page we want to map in (uobjpage, usually)
1797 */
1798
1799 /* no anon in this case. */
1800 anon = NULL;
1801
1802 uvmexp.flt_obj++;
1803 if (UVM_ET_ISCOPYONWRITE(ufi->entry) ||
1804 UVM_OBJ_NEEDS_WRITEFAULT(uobjpage->uobject))
1805 flt->enter_prot &= ~VM_PROT_WRITE;
1806 pg = uobjpage; /* map in the actual object */
1807
1808 KASSERT(uobjpage != PGO_DONTCARE);
1809
1810 /*
1811 * we are faulting directly on the page. be careful
1812 * about writing to loaned pages...
1813 */
1814
1815 if (uobjpage->loan_count) {
1816 if (!flt->cow_now) {
1817 /* read fault: cap the protection at readonly */
1818 /* cap! */
1819 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1820 } else {
1821 /* write fault: must break the loan here */
1822
1823 pg = uvm_loanbreak(uobjpage);
1824 if (pg == NULL) {
1825
1826 /*
1827 * drop ownership of page, it can't
1828 * be released
1829 */
1830
1831 if (uobjpage->flags & PG_WANTED)
1832 wakeup(uobjpage);
1833 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1834 UVM_PAGE_OWN(uobjpage, NULL);
1835
1836 uvmfault_unlockall(ufi, amap, uobj,
1837 NULL);
1838 UVMHIST_LOG(maphist,
1839 " out of RAM breaking loan, waiting",
1840 0,0,0,0);
1841 uvmexp.fltnoram++;
1842 uvm_wait("flt_noram4");
1843 return ERESTART;
1844 }
1845 uobjpage = pg;
1846 }
1847 }
1848 } else {
1849
1850 /*
1851 * if we are going to promote the data to an anon we
1852 * allocate a blank anon here and plug it into our amap.
1853 */
1854 #if DIAGNOSTIC
1855 if (amap == NULL)
1856 panic("uvm_fault: want to promote data, but no anon");
1857 #endif
1858 error = uvmfault_promote(ufi, NULL, uobjpage,
1859 &anon, &flt->anon_spare);
1860 switch (error) {
1861 case 0:
1862 break;
1863 case ERESTART:
1864 return ERESTART;
1865 default:
1866 return error;
1867 }
1868
1869 pg = anon->an_page;
1870
1871 /*
1872 * fill in the data
1873 */
1874
1875 if (uobjpage != PGO_DONTCARE) {
1876 uvmexp.flt_prcopy++;
1877
1878 /*
1879 * promote to shared amap? make sure all sharing
1880 * procs see it
1881 */
1882
1883 if ((amap_flags(amap) & AMAP_SHARED) != 0) {
1884 pmap_page_protect(uobjpage, VM_PROT_NONE);
1885 /*
1886 * XXX: PAGE MIGHT BE WIRED!
1887 */
1888 }
1889
1890 /*
1891 * dispose of uobjpage. it can't be PG_RELEASED
1892 * since we still hold the object lock.
1893 * drop handle to uobj as well.
1894 */
1895
1896 if (uobjpage->flags & PG_WANTED)
1897 /* still have the obj lock */
1898 wakeup(uobjpage);
1899 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1900 UVM_PAGE_OWN(uobjpage, NULL);
1901 mutex_exit(&uobj->vmobjlock);
1902 uobj = NULL;
1903
1904 UVMHIST_LOG(maphist,
1905 " promote uobjpage 0x%x to anon/page 0x%x/0x%x",
1906 uobjpage, anon, pg, 0);
1907
1908 } else {
1909 uvmexp.flt_przero++;
1910
1911 /*
1912 * Page is zero'd and marked dirty by
1913 * uvmfault_promote().
1914 */
1915
1916 UVMHIST_LOG(maphist," zero fill anon/page 0x%x/0%x",
1917 anon, pg, 0, 0);
1918 }
1919 }
1920
1921 /*
1922 * locked:
1923 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj),
1924 * anon(if !null), pg(if anon)
1925 *
1926 * note: pg is either the uobjpage or the new page in the new anon
1927 */
1928 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1929 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
1930 KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0);
1931 KASSERT(anon == NULL || mutex_owned(&anon->an_lock));
1932 KASSERT((pg->flags & PG_BUSY) != 0);
1933
1934 /*
1935 * all resources are present. we can now map it in and free our
1936 * resources.
1937 */
1938
1939 UVMHIST_LOG(maphist,
1940 " MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d",
1941 ufi->orig_map->pmap, ufi->orig_rvaddr, pg, promote);
1942 KASSERT((flt->access_type & VM_PROT_WRITE) == 0 ||
1943 (pg->flags & PG_RDONLY) == 0);
1944 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr, VM_PAGE_TO_PHYS(pg),
1945 pg->flags & PG_RDONLY ? flt->enter_prot & ~VM_PROT_WRITE : flt->enter_prot,
1946 flt->access_type | PMAP_CANFAIL | (flt->wired ? PMAP_WIRED : 0)) != 0) {
1947
1948 /*
1949 * No need to undo what we did; we can simply think of
1950 * this as the pmap throwing away the mapping information.
1951 *
1952 * We do, however, have to go through the ReFault path,
1953 * as the map may change while we're asleep.
1954 */
1955
1956 if (pg->flags & PG_WANTED)
1957 wakeup(pg);
1958
1959 /*
1960 * note that pg can't be PG_RELEASED since we did not drop
1961 * the object lock since the last time we checked.
1962 */
1963 KASSERT((pg->flags & PG_RELEASED) == 0);
1964
1965 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
1966 UVM_PAGE_OWN(pg, NULL);
1967 uvmfault_unlockall(ufi, amap, uobj, anon);
1968 if (!uvm_reclaimable()) {
1969 UVMHIST_LOG(maphist,
1970 "<- failed. out of VM",0,0,0,0);
1971 /* XXX instrumentation */
1972 error = ENOMEM;
1973 return error;
1974 }
1975 /* XXX instrumentation */
1976 uvm_wait("flt_pmfail2");
1977 return ERESTART;
1978 }
1979
1980 mutex_enter(&uvm_pageqlock);
1981 if (flt->wire_fault) {
1982 uvm_pagewire(pg);
1983 if (pg->pqflags & PQ_AOBJ) {
1984
1985 /*
1986 * since the now-wired page cannot be paged out,
1987 * release its swap resources for others to use.
1988 * since an aobj page with no swap cannot be PG_CLEAN,
1989 * clear its clean flag now.
1990 */
1991
1992 KASSERT(uobj != NULL);
1993 pg->flags &= ~(PG_CLEAN);
1994 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
1995 }
1996 } else {
1997 uvm_pageactivate(pg);
1998 }
1999 mutex_exit(&uvm_pageqlock);
2000 if (pg->flags & PG_WANTED)
2001 wakeup(pg);
2002
2003 /*
2004 * note that pg can't be PG_RELEASED since we did not drop the object
2005 * lock since the last time we checked.
2006 */
2007 KASSERT((pg->flags & PG_RELEASED) == 0);
2008
2009 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
2010 UVM_PAGE_OWN(pg, NULL);
2011 uvmfault_unlockall(ufi, amap, uobj, anon);
2012 pmap_update(ufi->orig_map->pmap);
2013 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
2014 return 0;
2015 }
2016
2017
2018 /*
2019 * uvm_fault_wire: wire down a range of virtual addresses in a map.
2020 *
2021 * => map may be read-locked by caller, but MUST NOT be write-locked.
2022 * => if map is read-locked, any operations which may cause map to
2023 * be write-locked in uvm_fault() must be taken care of by
2024 * the caller. See uvm_map_pageable().
2025 */
2026
2027 int
2028 uvm_fault_wire(struct vm_map *map, vaddr_t start, vaddr_t end,
2029 vm_prot_t access_type, int maxprot)
2030 {
2031 vaddr_t va;
2032 int error;
2033
2034 /*
2035 * now fault it in a page at a time. if the fault fails then we have
2036 * to undo what we have done. note that in uvm_fault VM_PROT_NONE
2037 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
2038 */
2039
2040 /*
2041 * XXX work around overflowing a vaddr_t. this prevents us from
2042 * wiring the last page in the address space, though.
2043 */
2044 if (start > end) {
2045 return EFAULT;
2046 }
2047
2048 for (va = start ; va < end ; va += PAGE_SIZE) {
2049 error = uvm_fault_internal(map, va, access_type,
2050 (maxprot ? UVM_FAULT_MAXPROT : 0) | UVM_FAULT_WIRE);
2051 if (error) {
2052 if (va != start) {
2053 uvm_fault_unwire(map, start, va);
2054 }
2055 return error;
2056 }
2057 }
2058 return 0;
2059 }
2060
2061 /*
2062 * uvm_fault_unwire(): unwire range of virtual space.
2063 */
2064
2065 void
2066 uvm_fault_unwire(struct vm_map *map, vaddr_t start, vaddr_t end)
2067 {
2068 vm_map_lock_read(map);
2069 uvm_fault_unwire_locked(map, start, end);
2070 vm_map_unlock_read(map);
2071 }
2072
2073 /*
2074 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
2075 *
2076 * => map must be at least read-locked.
2077 */
2078
2079 void
2080 uvm_fault_unwire_locked(struct vm_map *map, vaddr_t start, vaddr_t end)
2081 {
2082 struct vm_map_entry *entry;
2083 pmap_t pmap = vm_map_pmap(map);
2084 vaddr_t va;
2085 paddr_t pa;
2086 struct vm_page *pg;
2087
2088 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0);
2089
2090 /*
2091 * we assume that the area we are unwiring has actually been wired
2092 * in the first place. this means that we should be able to extract
2093 * the PAs from the pmap. we also lock out the page daemon so that
2094 * we can call uvm_pageunwire.
2095 */
2096
2097 mutex_enter(&uvm_pageqlock);
2098
2099 /*
2100 * find the beginning map entry for the region.
2101 */
2102
2103 KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map));
2104 if (uvm_map_lookup_entry(map, start, &entry) == false)
2105 panic("uvm_fault_unwire_locked: address not in map");
2106
2107 for (va = start; va < end; va += PAGE_SIZE) {
2108 if (pmap_extract(pmap, va, &pa) == false)
2109 continue;
2110
2111 /*
2112 * find the map entry for the current address.
2113 */
2114
2115 KASSERT(va >= entry->start);
2116 while (va >= entry->end) {
2117 KASSERT(entry->next != &map->header &&
2118 entry->next->start <= entry->end);
2119 entry = entry->next;
2120 }
2121
2122 /*
2123 * if the entry is no longer wired, tell the pmap.
2124 */
2125
2126 if (VM_MAPENT_ISWIRED(entry) == 0)
2127 pmap_unwire(pmap, va);
2128
2129 pg = PHYS_TO_VM_PAGE(pa);
2130 if (pg)
2131 uvm_pageunwire(pg);
2132 }
2133
2134 mutex_exit(&uvm_pageqlock);
2135 }
2136