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