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