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