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