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