1 /* $NetBSD: uvm_fault.c,v 1.128 2009/12/05 22:34:43 pooka Exp $ */
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
34 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp
38 * uvm_fault.c: fault handler
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.128 2009/12/05 22:34:43 pooka Exp $");
44 #include "opt_uvmhist.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
57 * a word on page faults:
59 * types of page faults we handle:
61 * CASE 1: upper layer faults CASE 2: lower layer faults
63 * CASE 1A CASE 1B CASE 2A CASE 2B
64 * read/write1 write>1 read/write +-cow_write/zero
66 * +--|--+ +--|--+ +-----+ + | + | +-----+
67 * amap | V | | ---------> new | | | | ^ |
68 * +-----+ +-----+ +-----+ + | + | +--|--+
70 * +-----+ +-----+ +--|--+ | +--|--+
71 * uobj | d/c | | d/c | | V | +----+ |
72 * +-----+ +-----+ +-----+ +-----+
76 * case [0]: layerless fault
77 * no amap or uobj is present. this is an error.
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"]
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.
92 * we follow the standard UVM locking protocol ordering:
94 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
95 * we hold a PG_BUSY page if we unlock for I/O
98 * the code is structured as follows:
100 * - init the "IN" params in the ufi structure
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
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
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]
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.
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(?).
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...).
148 * we use alternative 2. given that we are multi-threaded now we may want
149 * to reconsider the choice.
153 * local data structures
164 * note: index in array must match "advice" value
165 * XXX: borrowed numbers from freebsd. do they work well for us?
168 static const struct uvm_advice uvmadvice
[] = {
169 { MADV_NORMAL
, 3, 4 },
170 { MADV_RANDOM
, 0, 0 },
171 { MADV_SEQUENTIAL
, 8, 7},
174 #define UVM_MAXRANGE 16 /* must be MAX() of nback+nforw+1 */
185 * uvmfault_anonflush: try and deactivate pages in specified anons
187 * => does not have to deactivate page if it is busy
191 uvmfault_anonflush(struct vm_anon
**anons
, int n
)
196 for (lcv
= 0 ; lcv
< n
; lcv
++) {
197 if (anons
[lcv
] == NULL
)
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
);
206 mutex_exit(&uvm_pageqlock
);
208 mutex_exit(&anons
[lcv
]->an_lock
);
217 * uvmfault_amapcopy: clear "needs_copy" in a map.
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)
225 uvmfault_amapcopy(struct uvm_faultinfo
*ufi
)
230 * no mapping? give up.
233 if (uvmfault_lookup(ufi
, true) == false)
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);
245 * didn't work? must be out of RAM. unlock and sleep.
248 if (UVM_ET_ISNEEDSCOPY(ufi
->entry
)) {
249 uvmfault_unlockmaps(ufi
, true);
250 uvm_wait("fltamapcopy");
255 * got it! unlock and return.
258 uvmfault_unlockmaps(ufi
, true);
265 * uvmfault_anonget: get data in an anon into a non-busy, non-released
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
281 uvmfault_anonget(struct uvm_faultinfo
*ufi
, struct vm_amap
*amap
,
282 struct vm_anon
*anon
)
284 bool we_own
; /* we own anon's page? */
285 bool locked
; /* did we relock? */
288 UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist
);
290 KASSERT(mutex_owned(&anon
->an_lock
));
294 /* bump rusage counters */
296 curlwp
->l_ru
.ru_minflt
++;
298 curlwp
->l_ru
.ru_majflt
++;
301 * loop until we get it, or fail.
305 we_own
= false; /* true if we set PG_BUSY on a page */
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.
314 if (pg
&& pg
->loan_count
)
315 pg
= uvm_anon_lockloanpg(anon
);
318 * page there? make sure it is not busy/released.
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.
330 if ((pg
->flags
& PG_BUSY
) == 0) {
331 UVMHIST_LOG(maphist
, "<- OK",0,0,0,0);
334 pg
->flags
|= PG_WANTED
;
338 * the last unlock must be an atomic unlock+wait on
342 if (pg
->uobject
) { /* owner is uobject ? */
343 uvmfault_unlockall(ufi
, amap
, NULL
, anon
);
344 UVMHIST_LOG(maphist
, " unlock+wait on uobj",0,
346 UVM_UNLOCK_AND_WAIT(pg
,
347 &pg
->uobject
->vmobjlock
,
348 false, "anonget1",0);
351 uvmfault_unlockall(ufi
, amap
, NULL
, NULL
);
352 UVMHIST_LOG(maphist
, " unlock+wait on anon",0,
354 UVM_UNLOCK_AND_WAIT(pg
,&anon
->an_lock
,0,
361 * no page, we must try and bring it in.
364 pg
= uvm_pagealloc(NULL
, 0, anon
, 0);
365 if (pg
== NULL
) { /* out of RAM. */
366 uvmfault_unlockall(ufi
, amap
, NULL
, anon
);
368 UVMHIST_LOG(maphist
, " noram -- UVM_WAIT",0,
370 if (!uvm_reclaimable()) {
373 uvm_wait("flt_noram1");
375 /* we set the PG_BUSY bit */
377 uvmfault_unlockall(ufi
, amap
, NULL
, anon
);
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.
387 error
= uvm_swap_get(pg
, anon
->an_swslot
,
391 * we clean up after the i/o below in the
395 #else /* defined(VMSWAP) */
396 panic("%s: no page", __func__
);
397 #endif /* defined(VMSWAP) */
401 * now relock and try again
404 locked
= uvmfault_relock(ufi
);
405 if (locked
&& amap
!= NULL
) {
408 if (locked
|| we_own
)
409 mutex_enter(&anon
->an_lock
);
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
415 * [1] page released during I/O: free anon and ReFault.
416 * [2] I/O not OK. free the page and cause the fault
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).
424 if (pg
->flags
& PG_WANTED
) {
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.
436 if (anon
->an_swslot
> 0)
437 uvm_swap_markbad(anon
->an_swslot
, 1);
438 anon
->an_swslot
= SWSLOT_BAD
;
440 if ((pg
->flags
& PG_RELEASED
) != 0)
444 * note: page was never !PG_BUSY, so it
445 * can't be mapped and thus no need to
446 * pmap_page_protect it...
449 mutex_enter(&uvm_pageqlock
);
451 mutex_exit(&uvm_pageqlock
);
454 uvmfault_unlockall(ufi
, amap
, NULL
,
457 mutex_exit(&anon
->an_lock
);
458 UVMHIST_LOG(maphist
, "<- ERROR", 0,0,0,0);
462 if ((pg
->flags
& PG_RELEASED
) != 0) {
464 KASSERT(anon
->an_ref
== 0);
467 * released while we unlocked amap.
471 uvmfault_unlockall(ufi
, amap
, NULL
,
474 uvm_anon_release(anon
);
478 "<- ERROR/RELEASED", 0,0,0,0);
482 UVMHIST_LOG(maphist
, "<- RELEASED", 0,0,0,0);
487 * we've successfully read the page, activate it.
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
);
496 mutex_exit(&anon
->an_lock
);
497 #else /* defined(VMSWAP) */
498 panic("%s: we_own", __func__
);
499 #endif /* defined(VMSWAP) */
503 * we were not able to relock. restart fault.
507 UVMHIST_LOG(maphist
, "<- REFAULT", 0,0,0,0);
512 * verify no one has touched the amap and moved the anon on us.
516 amap_lookup(&ufi
->entry
->aref
,
517 ufi
->orig_rvaddr
- ufi
->entry
->start
) != anon
) {
519 uvmfault_unlockall(ufi
, amap
, NULL
, anon
);
520 UVMHIST_LOG(maphist
, "<- REFAULT", 0,0,0,0);
535 * uvmfault_promote: promote data to a new anon. used for 1B and 2B.
537 * 1. allocate an anon and a page.
538 * 2. fill its contents.
539 * 3. put it into amap.
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.
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
)
553 struct vm_amap
*amap
= ufi
->entry
->aref
.ar_amap
;
554 struct uvm_object
*uobj
;
555 struct vm_anon
*anon
;
559 UVMHIST_FUNC(__func__
); UVMHIST_CALLED(maphist
);
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 */
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
));
586 KASSERT(*spare
== NULL
|| !mutex_owned(&(*spare
)->an_lock
));
589 if (*spare
!= NULL
) {
592 mutex_enter(&anon
->an_lock
);
593 } else if (ufi
->map
!= kernel_map
) {
594 anon
= uvm_analloc();
596 UVMHIST_LOG(maphist
, "kernel_map, unlock and retry", 0,0,0,0);
599 * we can't allocate anons with kernel_map locked.
602 uvm_page_unbusy(&uobjpage
, 1);
603 uvmfault_unlockall(ufi
, amap
, uobj
, oanon
);
605 *spare
= uvm_analloc();
606 if (*spare
== NULL
) {
609 mutex_exit(&(*spare
)->an_lock
);
616 * The new anon is locked.
618 * if opg == NULL, we want a zero'd, dirty page,
619 * so have uvm_pagealloc() do that for us.
622 pg
= uvm_pagealloc(NULL
, 0, anon
,
623 (opg
== NULL
) ? UVM_PGA_ZERO
: 0);
629 * out of memory resources?
633 /* save anon for the next try. */
635 mutex_exit(&anon
->an_lock
);
639 /* unlock and fail ... */
640 uvm_page_unbusy(&uobjpage
, 1);
641 uvmfault_unlockall(ufi
, amap
, uobj
, oanon
);
643 if (!uvm_reclaimable()) {
644 UVMHIST_LOG(maphist
, "out of VM", 0,0,0,0);
650 UVMHIST_LOG(maphist
, "out of RAM, waiting for more", 0,0,0,0);
652 uvm_wait("flt_noram5");
657 /* copy page [pg now dirty] */
659 uvm_pagecopy(opg
, pg
);
662 amap_add(&ufi
->entry
->aref
, ufi
->orig_rvaddr
- ufi
->entry
->start
, anon
,
673 * F A U L T - m a i n e n t r y p o i n t
677 * uvm_fault: page fault handler
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
688 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
689 ~VM_PROT_WRITE : VM_PROT_ALL)
691 /* fault_flag values passed from uvm_fault_wire to uvm_fault_internal */
692 #define UVM_FAULT_WIRE 1
693 #define UVM_FAULT_WIREMAX 2
696 uvm_fault_internal(struct vm_map
*orig_map
, vaddr_t vaddr
,
697 vm_prot_t access_type
, int fault_flag
)
699 struct uvm_faultinfo ufi
;
700 vm_prot_t enter_prot
, check_prot
;
701 bool wired
, narrow
, promote
, locked
, shadowed
, wire_fault
, cow_now
;
702 int npages
, nback
, nforw
, centeridx
, error
, lcv
, gotpages
;
703 vaddr_t startva
, currva
;
705 struct vm_amap
*amap
;
706 struct uvm_object
*uobj
;
707 struct vm_anon
*anons_store
[UVM_MAXRANGE
], **anons
, *anon
, *oanon
;
708 struct vm_anon
*anon_spare
;
709 struct vm_page
*pages
[UVM_MAXRANGE
], *pg
, *uobjpage
;
710 UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist
);
712 UVMHIST_LOG(maphist
, "(map=0x%x, vaddr=0x%x, at=%d, ff=%d)",
713 orig_map
, vaddr
, access_type
, fault_flag
);
715 anon
= anon_spare
= NULL
;
718 uvmexp
.faults
++; /* XXX: locking? */
721 * init the IN parameters in the ufi
724 ufi
.orig_map
= orig_map
;
725 ufi
.orig_rvaddr
= trunc_page(vaddr
);
726 ufi
.orig_size
= PAGE_SIZE
; /* can't get any smaller than this */
727 wire_fault
= (fault_flag
> 0);
729 narrow
= true; /* don't look for neighborhood
732 narrow
= false; /* normal fault */
735 * "goto ReFault" means restart the page fault from ground zero.
740 * lookup and lock the maps
743 if (uvmfault_lookup(&ufi
, false) == false) {
744 UVMHIST_LOG(maphist
, "<- no mapping @ 0x%x", vaddr
, 0,0,0);
748 /* locked: maps(read) */
751 if ((ufi
.map
->flags
& VM_MAP_PAGEABLE
) == 0) {
752 printf("Page fault on non-pageable map:\n");
753 printf("ufi.map = %p\n", ufi
.map
);
754 printf("ufi.orig_map = %p\n", ufi
.orig_map
);
755 printf("ufi.orig_rvaddr = 0x%lx\n", (u_long
) ufi
.orig_rvaddr
);
756 panic("uvm_fault: (ufi.map->flags & VM_MAP_PAGEABLE) == 0");
764 check_prot
= fault_flag
== UVM_FAULT_WIREMAX
?
765 ufi
.entry
->max_protection
: ufi
.entry
->protection
;
766 if ((check_prot
& access_type
) != access_type
) {
768 "<- protection failure (prot=0x%x, access=0x%x)",
769 ufi
.entry
->protection
, access_type
, 0, 0);
770 uvmfault_unlockmaps(&ufi
, false);
776 * "enter_prot" is the protection we want to enter the page in at.
777 * for certain pages (e.g. copy-on-write pages) this protection can
778 * be more strict than ufi.entry->protection. "wired" means either
779 * the entry is wired or we are fault-wiring the pg.
782 enter_prot
= ufi
.entry
->protection
;
783 wired
= VM_MAPENT_ISWIRED(ufi
.entry
) || wire_fault
;
785 access_type
= enter_prot
; /* full access for wired */
786 cow_now
= (check_prot
& VM_PROT_WRITE
) != 0;
788 cow_now
= (access_type
& VM_PROT_WRITE
) != 0;
792 * handle "needs_copy" case. if we need to copy the amap we will
793 * have to drop our readlock and relock it with a write lock. (we
794 * need a write lock to change anything in a map entry [e.g.
798 if (UVM_ET_ISNEEDSCOPY(ufi
.entry
)) {
799 if (cow_now
|| (ufi
.entry
->object
.uvm_obj
== NULL
)) {
800 KASSERT(fault_flag
!= UVM_FAULT_WIREMAX
);
803 " need to clear needs_copy and refault",0,0,0,0);
804 uvmfault_unlockmaps(&ufi
, false);
805 uvmfault_amapcopy(&ufi
);
812 * ensure that we pmap_enter page R/O since
813 * needs_copy is still true
816 enter_prot
&= ~VM_PROT_WRITE
;
821 * identify the players
824 amap
= ufi
.entry
->aref
.ar_amap
; /* upper layer */
825 uobj
= ufi
.entry
->object
.uvm_obj
; /* lower layer */
828 * check for a case 0 fault. if nothing backing the entry then
832 if (amap
== NULL
&& uobj
== NULL
) {
833 uvmfault_unlockmaps(&ufi
, false);
834 UVMHIST_LOG(maphist
,"<- no backing store, no overlay",0,0,0,0);
840 * establish range of interest based on advice from mapper
841 * and then clip to fit map entry. note that we only want
842 * to do this the first time through the fault. if we
843 * ReFault we will disable this by setting "narrow" to true.
846 if (narrow
== false) {
848 /* wide fault (!narrow) */
849 KASSERT(uvmadvice
[ufi
.entry
->advice
].advice
==
851 nback
= MIN(uvmadvice
[ufi
.entry
->advice
].nback
,
852 (ufi
.orig_rvaddr
- ufi
.entry
->start
) >> PAGE_SHIFT
);
853 startva
= ufi
.orig_rvaddr
- (nback
<< PAGE_SHIFT
);
854 nforw
= MIN(uvmadvice
[ufi
.entry
->advice
].nforw
,
855 ((ufi
.entry
->end
- ufi
.orig_rvaddr
) >>
858 * note: "-1" because we don't want to count the
859 * faulting page as forw
861 npages
= nback
+ nforw
+ 1;
864 narrow
= true; /* ensure only once per-fault */
870 startva
= ufi
.orig_rvaddr
;
876 /* locked: maps(read) */
877 UVMHIST_LOG(maphist
, " narrow=%d, back=%d, forw=%d, startva=0x%x",
878 narrow
, nback
, nforw
, startva
);
879 UVMHIST_LOG(maphist
, " entry=0x%x, amap=0x%x, obj=0x%x", ufi
.entry
,
883 * if we've got an amap, lock it and extract current anons.
889 amap_lookups(&ufi
.entry
->aref
, startva
- ufi
.entry
->start
,
892 anons
= NULL
; /* to be safe */
895 /* locked: maps(read), amap(if there) */
896 KASSERT(amap
== NULL
|| mutex_owned(&amap
->am_l
));
899 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
900 * now and then forget about them (for the rest of the fault).
903 if (ufi
.entry
->advice
== MADV_SEQUENTIAL
&& nback
!= 0) {
905 UVMHIST_LOG(maphist
, " MADV_SEQUENTIAL: flushing backpages",
907 /* flush back-page anons? */
909 uvmfault_anonflush(anons
, nback
);
913 uoff
= (startva
- ufi
.entry
->start
) + ufi
.entry
->offset
;
914 mutex_enter(&uobj
->vmobjlock
);
915 (void) (uobj
->pgops
->pgo_put
)(uobj
, uoff
, uoff
+
916 (nback
<< PAGE_SHIFT
), PGO_DEACTIVATE
);
919 /* now forget about the backpages */
922 startva
+= (nback
<< PAGE_SHIFT
);
924 nback
= centeridx
= 0;
927 /* locked: maps(read), amap(if there) */
928 KASSERT(amap
== NULL
|| mutex_owned(&amap
->am_l
));
931 * map in the backpages and frontpages we found in the amap in hopes
932 * of preventing future faults. we also init the pages[] array as
938 for (lcv
= 0 ; lcv
< npages
; lcv
++, currva
+= PAGE_SIZE
) {
941 * dont play with VAs that are already mapped
944 if (lcv
!= centeridx
&&
945 pmap_extract(ufi
.orig_map
->pmap
, currva
, NULL
)) {
946 pages
[lcv
] = PGO_DONTCARE
;
951 * unmapped or center page. check if any anon at this level.
953 if (amap
== NULL
|| anons
[lcv
] == NULL
) {
959 * check for present page and map if possible. re-activate it.
962 pages
[lcv
] = PGO_DONTCARE
;
963 if (lcv
== centeridx
) { /* save center for later! */
968 mutex_enter(&anon
->an_lock
);
969 /* ignore loaned pages */
970 if (anon
->an_page
&& anon
->an_page
->loan_count
== 0 &&
971 (anon
->an_page
->flags
& PG_BUSY
) == 0) {
972 mutex_enter(&uvm_pageqlock
);
973 uvm_pageenqueue(anon
->an_page
);
974 mutex_exit(&uvm_pageqlock
);
976 " MAPPING: n anon: pm=0x%x, va=0x%x, pg=0x%x",
977 ufi
.orig_map
->pmap
, currva
, anon
->an_page
, 0);
981 * Since this isn't the page that's actually faulting,
982 * ignore pmap_enter() failures; it's not critical
983 * that we enter these right now.
986 (void) pmap_enter(ufi
.orig_map
->pmap
, currva
,
987 VM_PAGE_TO_PHYS(anon
->an_page
),
988 (anon
->an_ref
> 1) ? (enter_prot
& ~VM_PROT_WRITE
) :
991 (VM_MAPENT_ISWIRED(ufi
.entry
) ? PMAP_WIRED
: 0));
993 pmap_update(ufi
.orig_map
->pmap
);
994 mutex_exit(&anon
->an_lock
);
997 /* locked: maps(read), amap(if there) */
998 KASSERT(amap
== NULL
|| mutex_owned(&amap
->am_l
));
999 /* (shadowed == true) if there is an anon at the faulting address */
1000 UVMHIST_LOG(maphist
, " shadowed=%d, will_get=%d", shadowed
,
1001 (uobj
&& shadowed
== false),0,0);
1004 * note that if we are really short of RAM we could sleep in the above
1005 * call to pmap_enter with everything locked. bad?
1007 * XXX Actually, that is bad; pmap_enter() should just fail in that
1008 * XXX case. --thorpej
1012 * if the desired page is not shadowed by the amap and we have a
1013 * backing object, then we check to see if the backing object would
1014 * prefer to handle the fault itself (rather than letting us do it
1015 * with the usual pgo_get hook). the backing object signals this by
1016 * providing a pgo_fault routine.
1019 if (uobj
&& shadowed
== false && uobj
->pgops
->pgo_fault
!= NULL
) {
1020 mutex_enter(&uobj
->vmobjlock
);
1021 /* locked: maps(read), amap (if there), uobj */
1022 error
= uobj
->pgops
->pgo_fault(&ufi
, startva
, pages
, npages
,
1023 centeridx
, access_type
, PGO_LOCKED
|PGO_SYNCIO
);
1025 /* locked: nothing, pgo_fault has unlocked everything */
1027 if (error
== ERESTART
)
1028 goto ReFault
; /* try again! */
1030 * object fault routine responsible for pmap_update().
1036 * now, if the desired page is not shadowed by the amap and we have
1037 * a backing object that does not have a special fault routine, then
1038 * we ask (with pgo_get) the object for resident pages that we care
1039 * about and attempt to map them in. we do not let pgo_get block
1043 if (uobj
&& shadowed
== false) {
1044 mutex_enter(&uobj
->vmobjlock
);
1045 /* locked (!shadowed): maps(read), amap (if there), uobj */
1047 * the following call to pgo_get does _not_ change locking state
1052 (void) uobj
->pgops
->pgo_get(uobj
, ufi
.entry
->offset
+
1053 (startva
- ufi
.entry
->start
),
1054 pages
, &gotpages
, centeridx
,
1055 access_type
& MASK(ufi
.entry
),
1056 ufi
.entry
->advice
, PGO_LOCKED
);
1059 * check for pages to map, if we got any
1066 for (lcv
= 0; lcv
< npages
;
1067 lcv
++, currva
+= PAGE_SIZE
) {
1068 struct vm_page
*curpg
;
1072 if (curpg
== NULL
|| curpg
== PGO_DONTCARE
) {
1075 KASSERT(curpg
->uobject
== uobj
);
1078 * if center page is resident and not
1079 * PG_BUSY|PG_RELEASED then pgo_get
1080 * made it PG_BUSY for us and gave
1081 * us a handle to it. remember this
1082 * page as "uobjpage." (for later use).
1085 if (lcv
== centeridx
) {
1087 UVMHIST_LOG(maphist
, " got uobjpage "
1088 "(0x%x) with locked get",
1094 * calling pgo_get with PGO_LOCKED returns us
1095 * pages which are neither busy nor released,
1096 * so we don't need to check for this.
1097 * we can just directly enter the pages.
1100 mutex_enter(&uvm_pageqlock
);
1101 uvm_pageenqueue(curpg
);
1102 mutex_exit(&uvm_pageqlock
);
1103 UVMHIST_LOG(maphist
,
1104 " MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x",
1105 ufi
.orig_map
->pmap
, currva
, curpg
, 0);
1109 * Since this page isn't the page that's
1110 * actually faulting, ignore pmap_enter()
1111 * failures; it's not critical that we
1112 * enter these right now.
1114 KASSERT((curpg
->flags
& PG_PAGEOUT
) == 0);
1115 KASSERT((curpg
->flags
& PG_RELEASED
) == 0);
1116 KASSERT(!UVM_OBJ_IS_CLEAN(curpg
->uobject
) ||
1117 (curpg
->flags
& PG_CLEAN
) != 0);
1118 readonly
= (curpg
->flags
& PG_RDONLY
)
1119 || (curpg
->loan_count
> 0)
1120 || UVM_OBJ_NEEDS_WRITEFAULT(curpg
->uobject
);
1122 (void) pmap_enter(ufi
.orig_map
->pmap
, currva
,
1123 VM_PAGE_TO_PHYS(curpg
),
1125 enter_prot
& ~VM_PROT_WRITE
:
1126 enter_prot
& MASK(ufi
.entry
),
1128 (wired
? PMAP_WIRED
: 0));
1131 * NOTE: page can't be PG_WANTED or PG_RELEASED
1132 * because we've held the lock the whole time
1133 * we've had the handle.
1135 KASSERT((curpg
->flags
& PG_WANTED
) == 0);
1136 KASSERT((curpg
->flags
& PG_RELEASED
) == 0);
1138 curpg
->flags
&= ~(PG_BUSY
);
1139 UVM_PAGE_OWN(curpg
, NULL
);
1141 pmap_update(ufi
.orig_map
->pmap
);
1147 /* locked (shadowed): maps(read), amap */
1148 /* locked (!shadowed): maps(read), amap(if there),
1149 uobj(if !null), uobjpage(if !null) */
1151 KASSERT(mutex_owned(&amap
->am_l
));
1153 KASSERT(amap
== NULL
|| mutex_owned(&amap
->am_l
));
1154 KASSERT(uobj
== NULL
|| mutex_owned(&uobj
->vmobjlock
));
1155 KASSERT(uobjpage
== NULL
|| (uobjpage
->flags
& PG_BUSY
) != 0);
1159 * note that at this point we are done with any front or back pages.
1160 * we are now going to focus on the center page (i.e. the one we've
1161 * faulted on). if we have faulted on the upper (anon) layer
1162 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
1163 * not touched it yet). if we have faulted on the bottom (uobj)
1164 * layer [i.e. case 2] and the page was both present and available,
1165 * then we've got a pointer to it as "uobjpage" and we've already
1170 * there are four possible cases we must address: 1A, 1B, 2A, and 2B
1174 * redirect case 2: if we are not shadowed, go to case 2.
1177 if (shadowed
== false)
1180 /* locked: maps(read), amap */
1183 * handle case 1: fault on an anon in our amap
1186 anon
= anons
[centeridx
];
1187 UVMHIST_LOG(maphist
, " case 1 fault: anon=0x%x", anon
, 0,0,0);
1188 mutex_enter(&anon
->an_lock
);
1190 /* locked: maps(read), amap, anon */
1191 KASSERT(mutex_owned(&amap
->am_l
));
1192 KASSERT(mutex_owned(&anon
->an_lock
));
1195 * no matter if we have case 1A or case 1B we are going to need to
1196 * have the anon's memory resident. ensure that now.
1200 * let uvmfault_anonget do the dirty work.
1201 * if it fails (!OK) it will unlock everything for us.
1202 * if it succeeds, locks are still valid and locked.
1203 * also, if it is OK, then the anon's page is on the queues.
1204 * if the page is on loan from a uvm_object, then anonget will
1205 * lock that object for us if it does not fail.
1208 error
= uvmfault_anonget(&ufi
, amap
, anon
);
1217 kpause("fltagain1", false, hz
/2, NULL
);
1225 * uobj is non null if the page is on loan from an object (i.e. uobj)
1228 uobj
= anon
->an_page
->uobject
; /* locked by anonget if !NULL */
1230 /* locked: maps(read), amap, anon, uobj(if one) */
1231 KASSERT(mutex_owned(&amap
->am_l
));
1232 KASSERT(mutex_owned(&anon
->an_lock
));
1233 KASSERT(uobj
== NULL
|| mutex_owned(&uobj
->vmobjlock
));
1236 * special handling for loaned pages
1239 if (anon
->an_page
->loan_count
) {
1244 * for read faults on loaned pages we just cap the
1245 * protection at read-only.
1248 enter_prot
= enter_prot
& ~VM_PROT_WRITE
;
1252 * note that we can't allow writes into a loaned page!
1254 * if we have a write fault on a loaned page in an
1255 * anon then we need to look at the anon's ref count.
1256 * if it is greater than one then we are going to do
1257 * a normal copy-on-write fault into a new anon (this
1258 * is not a problem). however, if the reference count
1259 * is one (a case where we would normally allow a
1260 * write directly to the page) then we need to kill
1261 * the loan before we continue.
1264 /* >1 case is already ok */
1265 if (anon
->an_ref
== 1) {
1267 /* get new un-owned replacement page */
1268 pg
= uvm_pagealloc(NULL
, 0, NULL
, 0);
1270 uvmfault_unlockall(&ufi
, amap
, uobj
,
1272 uvm_wait("flt_noram2");
1277 * copy data, kill loan, and drop uobj lock
1280 /* copy old -> new */
1281 uvm_pagecopy(anon
->an_page
, pg
);
1284 pmap_page_protect(anon
->an_page
, VM_PROT_NONE
);
1285 mutex_enter(&uvm_pageqlock
); /* KILL loan */
1287 anon
->an_page
->uanon
= NULL
;
1288 /* in case we owned */
1289 anon
->an_page
->pqflags
&= ~PQ_ANON
;
1292 /* if we were receiver of loan */
1293 anon
->an_page
->loan_count
--;
1296 * we were the lender (A->K); need
1297 * to remove the page from pageq's.
1299 uvm_pagedequeue(anon
->an_page
);
1303 mutex_exit(&uobj
->vmobjlock
);
1307 /* install new page in anon */
1310 pg
->pqflags
|= PQ_ANON
;
1312 uvm_pageactivate(pg
);
1313 mutex_exit(&uvm_pageqlock
);
1315 pg
->flags
&= ~(PG_BUSY
|PG_FAKE
);
1316 UVM_PAGE_OWN(pg
, NULL
);
1324 * if we are case 1B then we will need to allocate a new blank
1325 * anon to transfer the data into. note that we have a lock
1326 * on anon, so no one can busy or release the page until we are done.
1327 * also note that the ref count can't drop to zero here because
1328 * it is > 1 and we are only dropping one ref.
1330 * in the (hopefully very rare) case that we are out of RAM we
1331 * will unlock, wait for more RAM, and refault.
1333 * if we are out of anon VM we kill the process (XXX: could wait?).
1336 if (cow_now
&& anon
->an_ref
> 1) {
1338 UVMHIST_LOG(maphist
, " case 1B: COW fault",0,0,0,0);
1340 oanon
= anon
; /* oanon = old, locked anon */
1342 error
= uvmfault_promote(&ufi
, oanon
, PGO_DONTCARE
,
1343 &anon
, &anon_spare
);
1354 mutex_enter(&uvm_pageqlock
);
1355 uvm_pageactivate(pg
);
1356 mutex_exit(&uvm_pageqlock
);
1357 pg
->flags
&= ~(PG_BUSY
|PG_FAKE
);
1358 UVM_PAGE_OWN(pg
, NULL
);
1360 /* deref: can not drop to zero here by defn! */
1364 * note: oanon is still locked, as is the new anon. we
1365 * need to check for this later when we unlock oanon; if
1366 * oanon != anon, we'll have to unlock anon, too.
1372 oanon
= anon
; /* old, locked anon is same as anon */
1374 if (anon
->an_ref
> 1) /* disallow writes to ref > 1 anons */
1375 enter_prot
= enter_prot
& ~VM_PROT_WRITE
;
1379 /* locked: maps(read), amap, oanon, anon (if different from oanon) */
1380 KASSERT(mutex_owned(&amap
->am_l
));
1381 KASSERT(mutex_owned(&anon
->an_lock
));
1382 KASSERT(mutex_owned(&oanon
->an_lock
));
1385 * now map the page in.
1388 UVMHIST_LOG(maphist
, " MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x",
1389 ufi
.orig_map
->pmap
, ufi
.orig_rvaddr
, pg
, 0);
1390 if (pmap_enter(ufi
.orig_map
->pmap
, ufi
.orig_rvaddr
, VM_PAGE_TO_PHYS(pg
),
1391 enter_prot
, access_type
| PMAP_CANFAIL
| (wired
? PMAP_WIRED
: 0))
1395 * No need to undo what we did; we can simply think of
1396 * this as the pmap throwing away the mapping information.
1398 * We do, however, have to go through the ReFault path,
1399 * as the map may change while we're asleep.
1403 mutex_exit(&anon
->an_lock
);
1404 uvmfault_unlockall(&ufi
, amap
, uobj
, oanon
);
1405 if (!uvm_reclaimable()) {
1406 UVMHIST_LOG(maphist
,
1407 "<- failed. out of VM",0,0,0,0);
1408 /* XXX instrumentation */
1412 /* XXX instrumentation */
1413 uvm_wait("flt_pmfail1");
1418 * ... update the page queues.
1421 mutex_enter(&uvm_pageqlock
);
1426 * since the now-wired page cannot be paged out,
1427 * release its swap resources for others to use.
1428 * since an anon with no swap cannot be PG_CLEAN,
1429 * clear its clean flag now.
1432 pg
->flags
&= ~(PG_CLEAN
);
1433 uvm_anon_dropswap(anon
);
1435 uvm_pageactivate(pg
);
1437 mutex_exit(&uvm_pageqlock
);
1440 * done case 1! finish up by unlocking everything and returning success
1444 mutex_exit(&anon
->an_lock
);
1445 uvmfault_unlockall(&ufi
, amap
, uobj
, oanon
);
1446 pmap_update(ufi
.orig_map
->pmap
);
1452 * handle case 2: faulting on backing object or zero fill
1457 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
1459 KASSERT(amap
== NULL
|| mutex_owned(&amap
->am_l
));
1460 KASSERT(uobj
== NULL
|| mutex_owned(&uobj
->vmobjlock
));
1461 KASSERT(uobjpage
== NULL
|| (uobjpage
->flags
& PG_BUSY
) != 0);
1464 * note that uobjpage can not be PGO_DONTCARE at this point. we now
1465 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we
1466 * have a backing object, check and see if we are going to promote
1467 * the data up to an anon during the fault.
1471 uobjpage
= PGO_DONTCARE
;
1472 promote
= true; /* always need anon here */
1474 KASSERT(uobjpage
!= PGO_DONTCARE
);
1475 promote
= cow_now
&& UVM_ET_ISCOPYONWRITE(ufi
.entry
);
1477 UVMHIST_LOG(maphist
, " case 2 fault: promote=%d, zfill=%d",
1478 promote
, (uobj
== NULL
), 0,0);
1481 * if uobjpage is not null then we do not need to do I/O to get the
1484 * if uobjpage is null, then we need to unlock and ask the pager to
1485 * get the data for us. once we have the data, we need to reverify
1486 * the state the world. we are currently not holding any resources.
1490 /* update rusage counters */
1491 curlwp
->l_ru
.ru_minflt
++;
1493 /* update rusage counters */
1494 curlwp
->l_ru
.ru_majflt
++;
1496 /* locked: maps(read), amap(if there), uobj */
1497 uvmfault_unlockall(&ufi
, amap
, NULL
, NULL
);
1502 uoff
= (ufi
.orig_rvaddr
- ufi
.entry
->start
) + ufi
.entry
->offset
;
1503 error
= uobj
->pgops
->pgo_get(uobj
, uoff
, &uobjpage
, &gotpages
,
1504 0, access_type
& MASK(ufi
.entry
), ufi
.entry
->advice
,
1506 /* locked: uobjpage(if no error) */
1507 KASSERT(error
!= 0 || (uobjpage
->flags
& PG_BUSY
) != 0);
1514 if (error
== EAGAIN
) {
1515 UVMHIST_LOG(maphist
,
1516 " pgo_get says TRY AGAIN!",0,0,0,0);
1517 kpause("fltagain2", false, hz
/2, NULL
);
1521 UVMHIST_LOG(maphist
, "<- pgo_get failed (code %d)",
1526 /* locked: uobjpage */
1528 mutex_enter(&uvm_pageqlock
);
1529 uvm_pageactivate(uobjpage
);
1530 mutex_exit(&uvm_pageqlock
);
1533 * re-verify the state of the world by first trying to relock
1534 * the maps. always relock the object.
1537 locked
= uvmfault_relock(&ufi
);
1540 uobj
= uobjpage
->uobject
;
1541 mutex_enter(&uobj
->vmobjlock
);
1543 /* locked(locked): maps(read), amap(if !null), uobj, uobjpage */
1544 /* locked(!locked): uobj, uobjpage */
1547 * verify that the page has not be released and re-verify
1548 * that amap slot is still free. if there is a problem,
1549 * we unlock and clean up.
1552 if ((uobjpage
->flags
& PG_RELEASED
) != 0 ||
1554 amap_lookup(&ufi
.entry
->aref
,
1555 ufi
.orig_rvaddr
- ufi
.entry
->start
))) {
1557 uvmfault_unlockall(&ufi
, amap
, NULL
, NULL
);
1562 * didn't get the lock? release the page and retry.
1565 if (locked
== false) {
1566 UVMHIST_LOG(maphist
,
1567 " wasn't able to relock after fault: retry",
1569 if (uobjpage
->flags
& PG_WANTED
)
1571 if (uobjpage
->flags
& PG_RELEASED
) {
1573 uvm_pagefree(uobjpage
);
1576 uobjpage
->flags
&= ~(PG_BUSY
|PG_WANTED
);
1577 UVM_PAGE_OWN(uobjpage
, NULL
);
1578 mutex_exit(&uobj
->vmobjlock
);
1583 * we have the data in uobjpage which is busy and
1584 * not released. we are holding object lock (so the page
1585 * can't be released on us).
1588 /* locked: maps(read), amap(if !null), uobj, uobjpage */
1593 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
1595 KASSERT(amap
== NULL
|| mutex_owned(&amap
->am_l
));
1596 KASSERT(uobj
== NULL
|| mutex_owned(&uobj
->vmobjlock
));
1597 KASSERT(uobj
== NULL
|| (uobjpage
->flags
& PG_BUSY
) != 0);
1601 * - at this point uobjpage can not be NULL
1602 * - at this point uobjpage can not be PG_RELEASED (since we checked
1604 * - at this point uobjpage could be PG_WANTED (handle later)
1607 KASSERT(uobj
== NULL
|| uobj
== uobjpage
->uobject
);
1608 KASSERT(uobj
== NULL
|| !UVM_OBJ_IS_CLEAN(uobjpage
->uobject
) ||
1609 (uobjpage
->flags
& PG_CLEAN
) != 0);
1610 if (promote
== false) {
1613 * we are not promoting. if the mapping is COW ensure that we
1614 * don't give more access than we should (e.g. when doing a read
1615 * fault on a COPYONWRITE mapping we want to map the COW page in
1616 * R/O even though the entry protection could be R/W).
1618 * set "pg" to the page we want to map in (uobjpage, usually)
1621 /* no anon in this case. */
1625 if (UVM_ET_ISCOPYONWRITE(ufi
.entry
) ||
1626 UVM_OBJ_NEEDS_WRITEFAULT(uobjpage
->uobject
))
1627 enter_prot
&= ~VM_PROT_WRITE
;
1628 pg
= uobjpage
; /* map in the actual object */
1630 KASSERT(uobjpage
!= PGO_DONTCARE
);
1633 * we are faulting directly on the page. be careful
1634 * about writing to loaned pages...
1637 if (uobjpage
->loan_count
) {
1639 /* read fault: cap the protection at readonly */
1641 enter_prot
= enter_prot
& ~VM_PROT_WRITE
;
1643 /* write fault: must break the loan here */
1645 pg
= uvm_loanbreak(uobjpage
);
1649 * drop ownership of page, it can't
1653 if (uobjpage
->flags
& PG_WANTED
)
1655 uobjpage
->flags
&= ~(PG_BUSY
|PG_WANTED
);
1656 UVM_PAGE_OWN(uobjpage
, NULL
);
1658 uvmfault_unlockall(&ufi
, amap
, uobj
,
1660 UVMHIST_LOG(maphist
,
1661 " out of RAM breaking loan, waiting",
1664 uvm_wait("flt_noram4");
1673 * if we are going to promote the data to an anon we
1674 * allocate a blank anon here and plug it into our amap.
1678 panic("uvm_fault: want to promote data, but no anon");
1680 error
= uvmfault_promote(&ufi
, NULL
, uobjpage
,
1681 &anon
, &anon_spare
);
1697 if (uobjpage
!= PGO_DONTCARE
) {
1698 uvmexp
.flt_prcopy
++;
1701 * promote to shared amap? make sure all sharing
1705 if ((amap_flags(amap
) & AMAP_SHARED
) != 0) {
1706 pmap_page_protect(uobjpage
, VM_PROT_NONE
);
1708 * XXX: PAGE MIGHT BE WIRED!
1713 * dispose of uobjpage. it can't be PG_RELEASED
1714 * since we still hold the object lock.
1715 * drop handle to uobj as well.
1718 if (uobjpage
->flags
& PG_WANTED
)
1719 /* still have the obj lock */
1721 uobjpage
->flags
&= ~(PG_BUSY
|PG_WANTED
);
1722 UVM_PAGE_OWN(uobjpage
, NULL
);
1723 mutex_exit(&uobj
->vmobjlock
);
1726 UVMHIST_LOG(maphist
,
1727 " promote uobjpage 0x%x to anon/page 0x%x/0x%x",
1728 uobjpage
, anon
, pg
, 0);
1731 uvmexp
.flt_przero
++;
1734 * Page is zero'd and marked dirty by
1735 * uvmfault_promote().
1738 UVMHIST_LOG(maphist
," zero fill anon/page 0x%x/0%x",
1745 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj),
1746 * anon(if !null), pg(if anon)
1748 * note: pg is either the uobjpage or the new page in the new anon
1750 KASSERT(amap
== NULL
|| mutex_owned(&amap
->am_l
));
1751 KASSERT(uobj
== NULL
|| mutex_owned(&uobj
->vmobjlock
));
1752 KASSERT(uobj
== NULL
|| (uobjpage
->flags
& PG_BUSY
) != 0);
1753 KASSERT(anon
== NULL
|| mutex_owned(&anon
->an_lock
));
1754 KASSERT((pg
->flags
& PG_BUSY
) != 0);
1757 * all resources are present. we can now map it in and free our
1761 UVMHIST_LOG(maphist
,
1762 " MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d",
1763 ufi
.orig_map
->pmap
, ufi
.orig_rvaddr
, pg
, promote
);
1764 KASSERT((access_type
& VM_PROT_WRITE
) == 0 ||
1765 (pg
->flags
& PG_RDONLY
) == 0);
1766 if (pmap_enter(ufi
.orig_map
->pmap
, ufi
.orig_rvaddr
, VM_PAGE_TO_PHYS(pg
),
1767 pg
->flags
& PG_RDONLY
? enter_prot
& ~VM_PROT_WRITE
: enter_prot
,
1768 access_type
| PMAP_CANFAIL
| (wired
? PMAP_WIRED
: 0)) != 0) {
1771 * No need to undo what we did; we can simply think of
1772 * this as the pmap throwing away the mapping information.
1774 * We do, however, have to go through the ReFault path,
1775 * as the map may change while we're asleep.
1778 if (pg
->flags
& PG_WANTED
)
1782 * note that pg can't be PG_RELEASED since we did not drop
1783 * the object lock since the last time we checked.
1785 KASSERT((pg
->flags
& PG_RELEASED
) == 0);
1787 pg
->flags
&= ~(PG_BUSY
|PG_FAKE
|PG_WANTED
);
1788 UVM_PAGE_OWN(pg
, NULL
);
1789 uvmfault_unlockall(&ufi
, amap
, uobj
, anon
);
1790 if (!uvm_reclaimable()) {
1791 UVMHIST_LOG(maphist
,
1792 "<- failed. out of VM",0,0,0,0);
1793 /* XXX instrumentation */
1797 /* XXX instrumentation */
1798 uvm_wait("flt_pmfail2");
1802 mutex_enter(&uvm_pageqlock
);
1805 if (pg
->pqflags
& PQ_AOBJ
) {
1808 * since the now-wired page cannot be paged out,
1809 * release its swap resources for others to use.
1810 * since an aobj page with no swap cannot be PG_CLEAN,
1811 * clear its clean flag now.
1814 KASSERT(uobj
!= NULL
);
1815 pg
->flags
&= ~(PG_CLEAN
);
1816 uao_dropswap(uobj
, pg
->offset
>> PAGE_SHIFT
);
1819 uvm_pageactivate(pg
);
1821 mutex_exit(&uvm_pageqlock
);
1822 if (pg
->flags
& PG_WANTED
)
1826 * note that pg can't be PG_RELEASED since we did not drop the object
1827 * lock since the last time we checked.
1829 KASSERT((pg
->flags
& PG_RELEASED
) == 0);
1831 pg
->flags
&= ~(PG_BUSY
|PG_FAKE
|PG_WANTED
);
1832 UVM_PAGE_OWN(pg
, NULL
);
1833 uvmfault_unlockall(&ufi
, amap
, uobj
, anon
);
1834 pmap_update(ufi
.orig_map
->pmap
);
1835 UVMHIST_LOG(maphist
, "<- done (SUCCESS!)",0,0,0,0);
1838 if (anon_spare
!= NULL
) {
1839 anon_spare
->an_ref
--;
1840 uvm_anfree(anon_spare
);
1847 * uvm_fault_wire: wire down a range of virtual addresses in a map.
1849 * => map may be read-locked by caller, but MUST NOT be write-locked.
1850 * => if map is read-locked, any operations which may cause map to
1851 * be write-locked in uvm_fault() must be taken care of by
1852 * the caller. See uvm_map_pageable().
1856 uvm_fault_wire(struct vm_map
*map
, vaddr_t start
, vaddr_t end
,
1857 vm_prot_t access_type
, int wiremax
)
1863 * now fault it in a page at a time. if the fault fails then we have
1864 * to undo what we have done. note that in uvm_fault VM_PROT_NONE
1865 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
1869 * XXX work around overflowing a vaddr_t. this prevents us from
1870 * wiring the last page in the address space, though.
1876 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
1877 error
= uvm_fault_internal(map
, va
, access_type
,
1878 wiremax
? UVM_FAULT_WIREMAX
: UVM_FAULT_WIRE
);
1881 uvm_fault_unwire(map
, start
, va
);
1890 * uvm_fault_unwire(): unwire range of virtual space.
1894 uvm_fault_unwire(struct vm_map
*map
, vaddr_t start
, vaddr_t end
)
1896 vm_map_lock_read(map
);
1897 uvm_fault_unwire_locked(map
, start
, end
);
1898 vm_map_unlock_read(map
);
1902 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
1904 * => map must be at least read-locked.
1908 uvm_fault_unwire_locked(struct vm_map
*map
, vaddr_t start
, vaddr_t end
)
1910 struct vm_map_entry
*entry
;
1911 pmap_t pmap
= vm_map_pmap(map
);
1916 KASSERT((map
->flags
& VM_MAP_INTRSAFE
) == 0);
1919 * we assume that the area we are unwiring has actually been wired
1920 * in the first place. this means that we should be able to extract
1921 * the PAs from the pmap. we also lock out the page daemon so that
1922 * we can call uvm_pageunwire.
1925 mutex_enter(&uvm_pageqlock
);
1928 * find the beginning map entry for the region.
1931 KASSERT(start
>= vm_map_min(map
) && end
<= vm_map_max(map
));
1932 if (uvm_map_lookup_entry(map
, start
, &entry
) == false)
1933 panic("uvm_fault_unwire_locked: address not in map");
1935 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
1936 if (pmap_extract(pmap
, va
, &pa
) == false)
1940 * find the map entry for the current address.
1943 KASSERT(va
>= entry
->start
);
1944 while (va
>= entry
->end
) {
1945 KASSERT(entry
->next
!= &map
->header
&&
1946 entry
->next
->start
<= entry
->end
);
1947 entry
= entry
->next
;
1951 * if the entry is no longer wired, tell the pmap.
1954 if (VM_MAPENT_ISWIRED(entry
) == 0)
1955 pmap_unwire(pmap
, va
);
1957 pg
= PHYS_TO_VM_PAGE(pa
);
1962 mutex_exit(&uvm_pageqlock
);