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