uvm_fault.c revision 1.166.2.27 1 /* $NetBSD: uvm_fault.c,v 1.166.2.27 2010/11/21 15:00:12 uebayasi Exp $ */
2
3 /*
4 *
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp
35 */
36
37 /*
38 * uvm_fault.c: fault handler
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.166.2.27 2010/11/21 15:00:12 uebayasi Exp $");
43
44 #include "opt_uvmhist.h"
45 #include "opt_xip.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/malloc.h>
52 #include <sys/mman.h>
53
54 #include <uvm/uvm.h>
55
56 /*
57 *
58 * a word on page faults:
59 *
60 * types of page faults we handle:
61 *
62 * CASE 1: upper layer faults CASE 2: lower layer faults
63 *
64 * CASE 1A CASE 1B CASE 2A CASE 2B
65 * read/write1 write>1 read/write +-cow_write/zero
66 * | | | |
67 * +--|--+ +--|--+ +-----+ + | + | +-----+
68 * amap | V | | ---------> new | | | | ^ |
69 * +-----+ +-----+ +-----+ + | + | +--|--+
70 * | | |
71 * +-----+ +-----+ +--|--+ | +--|--+
72 * uobj | d/c | | d/c | | V | +----+ |
73 * +-----+ +-----+ +-----+ +-----+
74 *
75 * d/c = don't care
76 *
77 * case [0]: layerless fault
78 * no amap or uobj is present. this is an error.
79 *
80 * case [1]: upper layer fault [anon active]
81 * 1A: [read] or [write with anon->an_ref == 1]
82 * I/O takes place in upper level anon and uobj is not touched.
83 * 1B: [write with anon->an_ref > 1]
84 * new anon is alloc'd and data is copied off ["COW"]
85 *
86 * case [2]: lower layer fault [uobj]
87 * 2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
88 * I/O takes place directly in object.
89 * 2B: [write to copy_on_write] or [read on NULL uobj]
90 * data is "promoted" from uobj to a new anon.
91 * if uobj is null, then we zero fill.
92 *
93 * we follow the standard UVM locking protocol ordering:
94 *
95 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
96 * we hold a PG_BUSY page if we unlock for I/O
97 *
98 *
99 * the code is structured as follows:
100 *
101 * - init the "IN" params in the ufi structure
102 * ReFault:
103 * - do lookups [locks maps], check protection, handle needs_copy
104 * - check for case 0 fault (error)
105 * - establish "range" of fault
106 * - if we have an amap lock it and extract the anons
107 * - if sequential advice deactivate pages behind us
108 * - at the same time check pmap for unmapped areas and anon for pages
109 * that we could map in (and do map it if found)
110 * - check object for resident pages that we could map in
111 * - if (case 2) goto Case2
112 * - >>> handle case 1
113 * - ensure source anon is resident in RAM
114 * - if case 1B alloc new anon and copy from source
115 * - map the correct page in
116 * Case2:
117 * - >>> handle case 2
118 * - ensure source page is resident (if uobj)
119 * - if case 2B alloc new anon and copy from source (could be zero
120 * fill if uobj == NULL)
121 * - map the correct page in
122 * - done!
123 *
124 * note on paging:
125 * if we have to do I/O we place a PG_BUSY page in the correct object,
126 * unlock everything, and do the I/O. when I/O is done we must reverify
127 * the state of the world before assuming that our data structures are
128 * valid. [because mappings could change while the map is unlocked]
129 *
130 * alternative 1: unbusy the page in question and restart the page fault
131 * from the top (ReFault). this is easy but does not take advantage
132 * of the information that we already have from our previous lookup,
133 * although it is possible that the "hints" in the vm_map will help here.
134 *
135 * alternative 2: the system already keeps track of a "version" number of
136 * a map. [i.e. every time you write-lock a map (e.g. to change a
137 * mapping) you bump the version number up by one...] so, we can save
138 * the version number of the map before we release the lock and start I/O.
139 * then when I/O is done we can relock and check the version numbers
140 * to see if anything changed. this might save us some over 1 because
141 * we don't have to unbusy the page and may be less compares(?).
142 *
143 * alternative 3: put in backpointers or a way to "hold" part of a map
144 * in place while I/O is in progress. this could be complex to
145 * implement (especially with structures like amap that can be referenced
146 * by multiple map entries, and figuring out what should wait could be
147 * complex as well...).
148 *
149 * we use alternative 2. given that we are multi-threaded now we may want
150 * to reconsider the choice.
151 */
152
153 /*
154 * local data structures
155 */
156
157 struct uvm_advice {
158 int advice;
159 int nback;
160 int nforw;
161 };
162
163 /*
164 * page range array:
165 * note: index in array must match "advice" value
166 * XXX: borrowed numbers from freebsd. do they work well for us?
167 */
168
169 static const struct uvm_advice uvmadvice[] = {
170 { MADV_NORMAL, 3, 4 },
171 { MADV_RANDOM, 0, 0 },
172 { MADV_SEQUENTIAL, 8, 7},
173 };
174
175 #define UVM_MAXRANGE 16 /* must be MAX() of nback+nforw+1 */
176
177 /*
178 * private prototypes
179 */
180
181 /*
182 * inline functions
183 */
184
185 /*
186 * uvmfault_anonflush: try and deactivate pages in specified anons
187 *
188 * => does not have to deactivate page if it is busy
189 */
190
191 static inline void
192 uvmfault_anonflush(struct vm_anon **anons, int n)
193 {
194 int lcv;
195 struct vm_page *pg;
196
197 for (lcv = 0; lcv < n; lcv++) {
198 if (anons[lcv] == NULL)
199 continue;
200 mutex_enter(&anons[lcv]->an_lock);
201 pg = anons[lcv]->an_page;
202 if (pg && (pg->flags & PG_BUSY) == 0) {
203 mutex_enter(&uvm_pageqlock);
204 if (pg->wire_count == 0) {
205 uvm_pagedeactivate(pg);
206 }
207 mutex_exit(&uvm_pageqlock);
208 }
209 mutex_exit(&anons[lcv]->an_lock);
210 }
211 }
212
213 /*
214 * normal functions
215 */
216
217 /*
218 * uvmfault_amapcopy: clear "needs_copy" in a map.
219 *
220 * => called with VM data structures unlocked (usually, see below)
221 * => we get a write lock on the maps and clear needs_copy for a VA
222 * => if we are out of RAM we sleep (waiting for more)
223 */
224
225 static void
226 uvmfault_amapcopy(struct uvm_faultinfo *ufi)
227 {
228 for (;;) {
229
230 /*
231 * no mapping? give up.
232 */
233
234 if (uvmfault_lookup(ufi, true) == false)
235 return;
236
237 /*
238 * copy if needed.
239 */
240
241 if (UVM_ET_ISNEEDSCOPY(ufi->entry))
242 amap_copy(ufi->map, ufi->entry, AMAP_COPY_NOWAIT,
243 ufi->orig_rvaddr, ufi->orig_rvaddr + 1);
244
245 /*
246 * didn't work? must be out of RAM. unlock and sleep.
247 */
248
249 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
250 uvmfault_unlockmaps(ufi, true);
251 uvm_wait("fltamapcopy");
252 continue;
253 }
254
255 /*
256 * got it! unlock and return.
257 */
258
259 uvmfault_unlockmaps(ufi, true);
260 return;
261 }
262 /*NOTREACHED*/
263 }
264
265 /*
266 * uvmfault_anonget: get data in an anon into a non-busy, non-released
267 * page in that anon.
268 *
269 * => maps, amap, and anon locked by caller.
270 * => if we fail (result != 0) we unlock everything.
271 * => if we are successful, we return with everything still locked.
272 * => we don't move the page on the queues [gets moved later]
273 * => if we allocate a new page [we_own], it gets put on the queues.
274 * either way, the result is that the page is on the queues at return time
275 * => for pages which are on loan from a uvm_object (and thus are not
276 * owned by the anon): if successful, we return with the owning object
277 * locked. the caller must unlock this object when it unlocks everything
278 * else.
279 */
280
281 int
282 uvmfault_anonget(struct uvm_faultinfo *ufi, struct vm_amap *amap,
283 struct vm_anon *anon)
284 {
285 bool we_own; /* we own anon's page? */
286 bool locked; /* did we relock? */
287 struct vm_page *pg;
288 int error;
289 UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist);
290
291 KASSERT(mutex_owned(&anon->an_lock));
292
293 error = 0;
294 uvmexp.fltanget++;
295 /* bump rusage counters */
296 if (anon->an_page)
297 curlwp->l_ru.ru_minflt++;
298 else
299 curlwp->l_ru.ru_majflt++;
300
301 /*
302 * loop until we get it, or fail.
303 */
304
305 for (;;) {
306 we_own = false; /* true if we set PG_BUSY on a page */
307 pg = anon->an_page;
308
309 /*
310 * if there is a resident page and it is loaned, then anon
311 * may not own it. call out to uvm_anon_lockpage() to ensure
312 * the real owner of the page has been identified and locked.
313 */
314
315 if (pg && pg->loan_count)
316 pg = uvm_anon_lockloanpg(anon);
317
318 /*
319 * page there? make sure it is not busy/released.
320 */
321
322 if (pg) {
323
324 /*
325 * at this point, if the page has a uobject [meaning
326 * we have it on loan], then that uobject is locked
327 * by us! if the page is busy, we drop all the
328 * locks (including uobject) and try again.
329 */
330
331 if ((pg->flags & PG_BUSY) == 0) {
332 UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
333 return (0);
334 }
335 pg->flags |= PG_WANTED;
336 uvmexp.fltpgwait++;
337
338 /*
339 * the last unlock must be an atomic unlock+wait on
340 * the owner of page
341 */
342
343 if (pg->uobject) { /* owner is uobject ? */
344 uvmfault_unlockall(ufi, amap, NULL, anon);
345 UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
346 0,0,0);
347 UVM_UNLOCK_AND_WAIT(pg,
348 &pg->uobject->vmobjlock,
349 false, "anonget1",0);
350 } else {
351 /* anon owns page */
352 uvmfault_unlockall(ufi, amap, NULL, NULL);
353 UVMHIST_LOG(maphist, " unlock+wait on anon",0,
354 0,0,0);
355 UVM_UNLOCK_AND_WAIT(pg,&anon->an_lock,0,
356 "anonget2",0);
357 }
358 } else {
359 #if defined(VMSWAP)
360
361 /*
362 * no page, we must try and bring it in.
363 */
364
365 pg = uvm_pagealloc(NULL, 0, anon, 0);
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 && uobjpage != PGO_HOLE) {
568 /* object-backed COW */
569 opg = uobjpage;
570 } else {
571 /* ZFOD or XIP hole */
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 == PGO_HOLE || (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, 0, anon,
624 (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 maxprot;
707 bool cow_now;
708 bool promote;
709 };
710
711 static inline int uvm_fault_check(
712 struct uvm_faultinfo *, struct uvm_faultctx *,
713 struct vm_anon ***, struct vm_page ***);
714
715 static int uvm_fault_upper(
716 struct uvm_faultinfo *, struct uvm_faultctx *,
717 struct vm_anon **);
718 static inline int uvm_fault_upper_lookup(
719 struct uvm_faultinfo *, struct uvm_faultctx *,
720 struct vm_anon **, struct vm_page **);
721 static inline void uvm_fault_upper_neighbor(
722 struct uvm_faultinfo *, struct uvm_faultctx *,
723 vaddr_t, struct vm_page *, bool);
724 static inline int uvm_fault_upper_loan(
725 struct uvm_faultinfo *, struct uvm_faultctx *,
726 struct vm_anon *, struct uvm_object **);
727 static inline int uvm_fault_upper_promote(
728 struct uvm_faultinfo *, struct uvm_faultctx *,
729 struct uvm_object *, struct vm_anon *);
730 static inline int uvm_fault_upper_direct(
731 struct uvm_faultinfo *, struct uvm_faultctx *,
732 struct uvm_object *, struct vm_anon *);
733 static int uvm_fault_upper_enter(
734 struct uvm_faultinfo *, struct uvm_faultctx *,
735 struct uvm_object *, struct vm_anon *,
736 struct vm_page *, struct vm_anon *);
737 static inline void uvm_fault_upper_done(
738 struct uvm_faultinfo *, struct uvm_faultctx *,
739 struct uvm_object *, struct vm_anon *,
740 struct vm_page *);
741
742 static int uvm_fault_lower(
743 struct uvm_faultinfo *, struct uvm_faultctx *,
744 struct vm_page **);
745 static inline void uvm_fault_lower_lookup(
746 struct uvm_faultinfo *, struct uvm_faultctx *,
747 struct vm_page **);
748 static inline void uvm_fault_lower_neighbor(
749 struct uvm_faultinfo *, struct uvm_faultctx *,
750 vaddr_t, struct vm_page *, bool);
751 static inline int uvm_fault_lower_io(
752 struct uvm_faultinfo *, struct uvm_faultctx *,
753 struct uvm_object **, struct vm_page **);
754 static inline int uvm_fault_lower_direct(
755 struct uvm_faultinfo *, struct uvm_faultctx *,
756 struct uvm_object *, struct vm_page *);
757 static inline int uvm_fault_lower_direct_loan(
758 struct uvm_faultinfo *, struct uvm_faultctx *,
759 struct uvm_object *, struct vm_page **,
760 struct vm_page **);
761 static inline int uvm_fault_lower_promote(
762 struct uvm_faultinfo *, struct uvm_faultctx *,
763 struct uvm_object *, struct vm_page *);
764 static int uvm_fault_lower_enter(
765 struct uvm_faultinfo *, struct uvm_faultctx *,
766 struct uvm_object *,
767 struct vm_anon *, struct vm_page *,
768 struct vm_page *);
769 static inline void uvm_fault_lower_done(
770 struct uvm_faultinfo *, struct uvm_faultctx *,
771 struct uvm_object *, struct vm_anon *,
772 struct vm_page *);
773
774 int
775 uvm_fault_internal(struct vm_map *orig_map, vaddr_t vaddr,
776 vm_prot_t access_type, int fault_flag)
777 {
778 struct uvm_faultinfo ufi;
779 struct uvm_faultctx flt = {
780 .access_type = access_type,
781
782 /* don't look for neighborhood * pages on "wire" fault */
783 .narrow = (fault_flag & UVM_FAULT_WIRE) != 0,
784
785 /* "wire" fault causes wiring of both mapping and paging */
786 .wire_mapping = (fault_flag & UVM_FAULT_WIRE) != 0,
787 .wire_paging = (fault_flag & UVM_FAULT_WIRE) != 0,
788
789 .maxprot = (fault_flag & UVM_FAULT_MAXPROT) != 0,
790 };
791 struct vm_anon *anons_store[UVM_MAXRANGE], **anons;
792 struct vm_page *pages_store[UVM_MAXRANGE], **pages;
793 int error;
794 UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist);
795
796 UVMHIST_LOG(maphist, "(map=0x%x, vaddr=0x%x, at=%d, ff=%d)",
797 orig_map, vaddr, access_type, fault_flag);
798
799 uvmexp.faults++; /* XXX: locking? */
800
801 /*
802 * init the IN parameters in the ufi
803 */
804
805 ufi.orig_map = orig_map;
806 ufi.orig_rvaddr = trunc_page(vaddr);
807 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */
808
809 error = ERESTART;
810 while (error == ERESTART) {
811 anons = anons_store;
812 pages = pages_store;
813
814 error = uvm_fault_check(&ufi, &flt, &anons, &pages);
815 if (error != 0)
816 continue;
817
818 error = uvm_fault_upper_lookup(&ufi, &flt, anons, pages);
819 if (error != 0)
820 continue;
821
822 if (pages[flt.centeridx] == PGO_DONTCARE)
823 error = uvm_fault_upper(&ufi, &flt, anons);
824 else {
825 struct uvm_object * const uobj = ufi.entry->object.uvm_obj;
826
827 if (uobj && uobj->pgops->pgo_fault != NULL) {
828 /*
829 * invoke "special" fault routine.
830 */
831 mutex_enter(&uobj->vmobjlock);
832 /* locked: maps(read), amap(if there), uobj */
833 error = uobj->pgops->pgo_fault(&ufi,
834 flt.startva, pages, flt.npages,
835 flt.centeridx, flt.access_type,
836 PGO_LOCKED|PGO_SYNCIO);
837
838 /* locked: nothing, pgo_fault has unlocked everything */
839
840 /*
841 * object fault routine responsible for pmap_update().
842 */
843 } else {
844 error = uvm_fault_lower(&ufi, &flt, pages);
845 }
846 }
847 }
848
849 if (flt.anon_spare != NULL) {
850 flt.anon_spare->an_ref--;
851 uvm_anfree(flt.anon_spare);
852 }
853 return error;
854 }
855
856 /*
857 * uvm_fault_check: check prot, handle needs-copy, etc.
858 *
859 * 1. lookup entry.
860 * 2. check protection.
861 * 3. adjust fault condition (mainly for simulated fault).
862 * 4. handle needs-copy (lazy amap copy).
863 * 5. establish range of interest for neighbor fault (aka pre-fault).
864 * 6. look up anons (if amap exists).
865 * 7. flush pages (if MADV_SEQUENTIAL)
866 *
867 * => called with nothing locked.
868 * => if we fail (result != 0) we unlock everything.
869 */
870
871 static int
872 uvm_fault_check(
873 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
874 struct vm_anon ***ranons, struct vm_page ***rpages)
875 {
876 struct vm_amap *amap;
877 struct uvm_object *uobj;
878 vm_prot_t check_prot;
879 int nback, nforw;
880 UVMHIST_FUNC("uvm_fault_check"); UVMHIST_CALLED(maphist);
881
882 /*
883 * lookup and lock the maps
884 */
885
886 if (uvmfault_lookup(ufi, false) == false) {
887 UVMHIST_LOG(maphist, "<- no mapping @ 0x%x", ufi->orig_rvaddr, 0,0,0);
888 return EFAULT;
889 }
890 /* locked: maps(read) */
891
892 #ifdef DIAGNOSTIC
893 if ((ufi->map->flags & VM_MAP_PAGEABLE) == 0) {
894 printf("Page fault on non-pageable map:\n");
895 printf("ufi->map = %p\n", ufi->map);
896 printf("ufi->orig_map = %p\n", ufi->orig_map);
897 printf("ufi->orig_rvaddr = 0x%lx\n", (u_long) ufi->orig_rvaddr);
898 panic("uvm_fault: (ufi->map->flags & VM_MAP_PAGEABLE) == 0");
899 }
900 #endif
901
902 /*
903 * check protection
904 */
905
906 check_prot = flt->maxprot ?
907 ufi->entry->max_protection : ufi->entry->protection;
908 if ((check_prot & flt->access_type) != flt->access_type) {
909 UVMHIST_LOG(maphist,
910 "<- protection failure (prot=0x%x, access=0x%x)",
911 ufi->entry->protection, flt->access_type, 0, 0);
912 uvmfault_unlockmaps(ufi, false);
913 return EACCES;
914 }
915
916 /*
917 * "enter_prot" is the protection we want to enter the page in at.
918 * for certain pages (e.g. copy-on-write pages) this protection can
919 * be more strict than ufi->entry->protection. "wired" means either
920 * the entry is wired or we are fault-wiring the pg.
921 */
922
923 flt->enter_prot = ufi->entry->protection;
924 if (VM_MAPENT_ISWIRED(ufi->entry))
925 flt->wire_mapping = true;
926
927 if (flt->wire_mapping) {
928 flt->access_type = flt->enter_prot; /* full access for wired */
929 flt->cow_now = (check_prot & VM_PROT_WRITE) != 0;
930 } else {
931 flt->cow_now = (flt->access_type & VM_PROT_WRITE) != 0;
932 }
933
934 flt->promote = false;
935
936 /*
937 * handle "needs_copy" case. if we need to copy the amap we will
938 * have to drop our readlock and relock it with a write lock. (we
939 * need a write lock to change anything in a map entry [e.g.
940 * needs_copy]).
941 */
942
943 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
944 if (flt->cow_now || (ufi->entry->object.uvm_obj == NULL)) {
945 KASSERT(!flt->maxprot);
946 /* need to clear */
947 UVMHIST_LOG(maphist,
948 " need to clear needs_copy and refault",0,0,0,0);
949 uvmfault_unlockmaps(ufi, false);
950 uvmfault_amapcopy(ufi);
951 uvmexp.fltamcopy++;
952 return ERESTART;
953
954 } else {
955
956 /*
957 * ensure that we pmap_enter page R/O since
958 * needs_copy is still true
959 */
960
961 flt->enter_prot &= ~VM_PROT_WRITE;
962 }
963 }
964
965 /*
966 * identify the players
967 */
968
969 amap = ufi->entry->aref.ar_amap; /* upper layer */
970 uobj = ufi->entry->object.uvm_obj; /* lower layer */
971
972 /*
973 * check for a case 0 fault. if nothing backing the entry then
974 * error now.
975 */
976
977 if (amap == NULL && uobj == NULL) {
978 uvmfault_unlockmaps(ufi, false);
979 UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
980 return EFAULT;
981 }
982
983 /*
984 * establish range of interest based on advice from mapper
985 * and then clip to fit map entry. note that we only want
986 * to do this the first time through the fault. if we
987 * ReFault we will disable this by setting "narrow" to true.
988 */
989
990 if (flt->narrow == false) {
991
992 /* wide fault (!narrow) */
993 KASSERT(uvmadvice[ufi->entry->advice].advice ==
994 ufi->entry->advice);
995 nback = MIN(uvmadvice[ufi->entry->advice].nback,
996 (ufi->orig_rvaddr - ufi->entry->start) >> PAGE_SHIFT);
997 flt->startva = ufi->orig_rvaddr - (nback << PAGE_SHIFT);
998 nforw = MIN(uvmadvice[ufi->entry->advice].nforw,
999 ((ufi->entry->end - ufi->orig_rvaddr) >>
1000 PAGE_SHIFT) - 1);
1001 /*
1002 * note: "-1" because we don't want to count the
1003 * faulting page as forw
1004 */
1005 flt->npages = nback + nforw + 1;
1006 flt->centeridx = nback;
1007
1008 flt->narrow = true; /* ensure only once per-fault */
1009
1010 } else {
1011
1012 /* narrow fault! */
1013 nback = nforw = 0;
1014 flt->startva = ufi->orig_rvaddr;
1015 flt->npages = 1;
1016 flt->centeridx = 0;
1017
1018 }
1019 /* offset from entry's start to pgs' start */
1020 const voff_t eoff = flt->startva - ufi->entry->start;
1021
1022 /* locked: maps(read) */
1023 UVMHIST_LOG(maphist, " narrow=%d, back=%d, forw=%d, startva=0x%x",
1024 flt->narrow, nback, nforw, flt->startva);
1025 UVMHIST_LOG(maphist, " entry=0x%x, amap=0x%x, obj=0x%x", ufi->entry,
1026 amap, uobj, 0);
1027
1028 /*
1029 * if we've got an amap, lock it and extract current anons.
1030 */
1031
1032 if (amap) {
1033 amap_lock(amap);
1034 amap_lookups(&ufi->entry->aref, eoff, *ranons, flt->npages);
1035 } else {
1036 *ranons = NULL; /* to be safe */
1037 }
1038
1039 /* locked: maps(read), amap(if there) */
1040 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1041
1042 /*
1043 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
1044 * now and then forget about them (for the rest of the fault).
1045 */
1046
1047 if (ufi->entry->advice == MADV_SEQUENTIAL && nback != 0) {
1048
1049 UVMHIST_LOG(maphist, " MADV_SEQUENTIAL: flushing backpages",
1050 0,0,0,0);
1051 /* flush back-page anons? */
1052 if (amap)
1053 uvmfault_anonflush(*ranons, nback);
1054
1055 /* flush object? */
1056 if (uobj) {
1057 voff_t uoff;
1058
1059 uoff = ufi->entry->offset + eoff;
1060 mutex_enter(&uobj->vmobjlock);
1061 (void) (uobj->pgops->pgo_put)(uobj, uoff, uoff +
1062 (nback << PAGE_SHIFT), PGO_DEACTIVATE);
1063 }
1064
1065 /* now forget about the backpages */
1066 if (amap)
1067 *ranons += nback;
1068 flt->startva += (nback << PAGE_SHIFT);
1069 flt->npages -= nback;
1070 flt->centeridx = 0;
1071 }
1072 /*
1073 * => startva is fixed
1074 * => npages is fixed
1075 */
1076
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, 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 * dont 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, 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, just mark enter_prot as read-only.
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,
1394 &anon, &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_pageactivate(pg);
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 oanon->an_ref--;
1413
1414 /*
1415 * note: oanon is still locked, as is the new anon. we
1416 * need to check for this later when we unlock oanon; if
1417 * oanon != anon, we'll have to unlock anon, too.
1418 */
1419
1420 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
1421 }
1422
1423 /*
1424 * uvm_fault_upper_direct: handle direct fault.
1425 */
1426
1427 static int
1428 uvm_fault_upper_direct(
1429 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1430 struct uvm_object *uobj, struct vm_anon *anon)
1431 {
1432 struct vm_anon * const oanon = anon;
1433 struct vm_page *pg;
1434 UVMHIST_FUNC("uvm_fault_upper_direct"); UVMHIST_CALLED(maphist);
1435
1436 uvmexp.flt_anon++;
1437 pg = anon->an_page;
1438 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */
1439 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1440
1441 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
1442 }
1443
1444 /*
1445 * uvm_fault_upper_enter: enter h/w mapping of upper page.
1446 */
1447
1448 static int
1449 uvm_fault_upper_enter(
1450 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1451 struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg,
1452 struct vm_anon *oanon)
1453 {
1454 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1455 UVMHIST_FUNC("uvm_fault_upper_enter"); UVMHIST_CALLED(maphist);
1456
1457 /* locked: maps(read), amap, oanon, anon(if different from oanon) */
1458 KASSERT(mutex_owned(&amap->am_l));
1459 KASSERT(mutex_owned(&anon->an_lock));
1460 KASSERT(mutex_owned(&oanon->an_lock));
1461
1462 /*
1463 * now map the page in.
1464 */
1465
1466 UVMHIST_LOG(maphist, " MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x, promote=%d",
1467 ufi->orig_map->pmap, ufi->orig_rvaddr, pg, flt->promote);
1468 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr, VM_PAGE_TO_PHYS(pg),
1469 flt->enter_prot, flt->access_type | PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0))
1470 != 0) {
1471
1472 /*
1473 * No need to undo what we did; we can simply think of
1474 * this as the pmap throwing away the mapping information.
1475 *
1476 * We do, however, have to go through the ReFault path,
1477 * as the map may change while we're asleep.
1478 */
1479
1480 if (anon != oanon)
1481 mutex_exit(&anon->an_lock);
1482 uvmfault_unlockall(ufi, amap, uobj, oanon);
1483 if (!uvm_reclaimable()) {
1484 UVMHIST_LOG(maphist,
1485 "<- failed. out of VM",0,0,0,0);
1486 /* XXX instrumentation */
1487 return ENOMEM;
1488 }
1489 /* XXX instrumentation */
1490 uvm_wait("flt_pmfail1");
1491 return ERESTART;
1492 }
1493
1494 uvm_fault_upper_done(ufi, flt, uobj, anon, pg);
1495
1496 /*
1497 * done case 1! finish up by unlocking everything and returning success
1498 */
1499
1500 if (anon != oanon) {
1501 mutex_exit(&anon->an_lock);
1502 }
1503 pmap_update(ufi->orig_map->pmap);
1504 uvmfault_unlockall(ufi, amap, uobj, oanon);
1505 return 0;
1506 }
1507
1508 /*
1509 * uvm_fault_upper_done: queue upper center page.
1510 */
1511
1512 static void
1513 uvm_fault_upper_done(
1514 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1515 struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg)
1516 {
1517 const bool wire_paging = flt->wire_paging;
1518
1519 UVMHIST_FUNC("uvm_fault_upper_done"); UVMHIST_CALLED(maphist);
1520
1521 /*
1522 * ... update the page queues.
1523 */
1524
1525 mutex_enter(&uvm_pageqlock);
1526 if (wire_paging) {
1527 uvm_pagewire(pg);
1528
1529 /*
1530 * since the now-wired page cannot be paged out,
1531 * release its swap resources for others to use.
1532 * since an anon with no swap cannot be PG_CLEAN,
1533 * clear its clean flag now.
1534 */
1535
1536 pg->flags &= ~(PG_CLEAN);
1537
1538 } else {
1539 uvm_pageactivate(pg);
1540 }
1541 mutex_exit(&uvm_pageqlock);
1542
1543 if (wire_paging) {
1544 uvm_anon_dropswap(anon);
1545 }
1546 }
1547
1548 /*
1549 * uvm_fault_lower: handle lower fault.
1550 *
1551 * 1. check uobj
1552 * 1.1. if null, ZFOD.
1553 * 1.2. if not null, look up unnmapped neighbor pages.
1554 * 2. for center page, check if promote.
1555 * 2.1. ZFOD always needs promotion.
1556 * 2.2. other uobjs, when entry is marked COW (usually MAP_PRIVATE vnode).
1557 * 3. if uobj is not ZFOD and page is not found, do i/o.
1558 * 4. dispatch either direct / promote fault.
1559 */
1560
1561 static int
1562 uvm_fault_lower(
1563 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1564 struct vm_page **pages)
1565 {
1566 #ifdef DIAGNOSTIC
1567 struct vm_amap *amap = ufi->entry->aref.ar_amap;
1568 #endif
1569 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1570 struct vm_page *uobjpage;
1571 int error;
1572 UVMHIST_FUNC("uvm_fault_lower"); UVMHIST_CALLED(maphist);
1573
1574 /* locked: maps(read), amap(if there), uobj(if !null) */
1575
1576 /*
1577 * now, if the desired page is not shadowed by the amap and we have
1578 * a backing object that does not have a special fault routine, then
1579 * we ask (with pgo_get) the object for resident pages that we care
1580 * about and attempt to map them in. we do not let pgo_get block
1581 * (PGO_LOCKED).
1582 */
1583
1584 if (uobj == NULL) {
1585 /* zero fill; don't care neighbor pages */
1586 uobjpage = NULL;
1587 } else {
1588 uvm_fault_lower_lookup(ufi, flt, pages);
1589 uobjpage = pages[flt->centeridx];
1590 }
1591
1592 /* locked: maps(read), amap(if there), uobj(if !null), uobjpage(if !null) */
1593 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1594 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
1595 KASSERT(uobjpage == NULL || (uobjpage->flags & PG_BUSY) != 0);
1596
1597 /*
1598 * note that at this point we are done with any front or back pages.
1599 * we are now going to focus on the center page (i.e. the one we've
1600 * faulted on). if we have faulted on the upper (anon) layer
1601 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
1602 * not touched it yet). if we have faulted on the bottom (uobj)
1603 * layer [i.e. case 2] and the page was both present and available,
1604 * then we've got a pointer to it as "uobjpage" and we've already
1605 * made it BUSY.
1606 */
1607
1608 /*
1609 * locked:
1610 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
1611 */
1612 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1613 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
1614 KASSERT(uobjpage == NULL || (uobjpage->flags & PG_BUSY) != 0);
1615
1616 /*
1617 * note that uobjpage can not be PGO_DONTCARE at this point. we now
1618 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we
1619 * have a backing object, check and see if we are going to promote
1620 * the data up to an anon during the fault.
1621 */
1622
1623 if (uobj == NULL) {
1624 uobjpage = PGO_DONTCARE;
1625 flt->promote = true; /* always need anon here */
1626 } else {
1627 KASSERT(uobjpage != PGO_DONTCARE);
1628 flt->promote = flt->cow_now && UVM_ET_ISCOPYONWRITE(ufi->entry);
1629 }
1630 UVMHIST_LOG(maphist, " case 2 fault: promote=%d, zfill=%d",
1631 flt->promote, (uobj == NULL), 0,0);
1632
1633 /*
1634 * if uobjpage is not null then we do not need to do I/O to get the
1635 * uobjpage.
1636 *
1637 * if uobjpage is null, then we need to unlock and ask the pager to
1638 * get the data for us. once we have the data, we need to reverify
1639 * the state the world. we are currently not holding any resources.
1640 */
1641
1642 if (uobjpage) {
1643 /* update rusage counters */
1644 curlwp->l_ru.ru_minflt++;
1645 } else {
1646 error = uvm_fault_lower_io(ufi, flt, &uobj, &uobjpage);
1647 if (error != 0)
1648 return error;
1649 }
1650
1651 /*
1652 * locked:
1653 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
1654 */
1655 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
1656 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
1657 KASSERT(uobj == NULL || uobjpage == PGO_HOLE || (uobjpage->flags & PG_BUSY) != 0);
1658
1659 /*
1660 * notes:
1661 * - at this point uobjpage can not be NULL
1662 * - at this point uobjpage can not be PG_RELEASED (since we checked
1663 * for it above)
1664 * - at this point uobjpage could be PG_WANTED (handle later)
1665 */
1666
1667 KASSERT(uobj == NULL || uobjpage == PGO_HOLE || (uobjpage->flags & PG_DEVICE) != 0 || uobj == uobjpage->uobject);
1668 KASSERT(uobj == NULL || uobjpage == PGO_HOLE || !UVM_OBJ_IS_CLEAN(uobjpage->uobject) ||
1669 (uobjpage->flags & PG_CLEAN) != 0);
1670
1671 if (flt->promote == false) {
1672 error = uvm_fault_lower_direct(ufi, flt, uobj, uobjpage);
1673 } else {
1674 error = uvm_fault_lower_promote(ufi, flt, uobj, uobjpage);
1675 }
1676 return error;
1677 }
1678
1679 /*
1680 * uvm_fault_lower_lookup: look up on-memory uobj pages.
1681 *
1682 * 1. get on-memory pages.
1683 * 2. if failed, give up (get only center page later).
1684 * 3. if succeeded, enter h/w mapping of neighbor pages.
1685 */
1686
1687 static void
1688 uvm_fault_lower_lookup(
1689 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1690 struct vm_page **pages)
1691 {
1692 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1693 int lcv, gotpages;
1694 vaddr_t currva;
1695 UVMHIST_FUNC("uvm_fault_lower_lookup"); UVMHIST_CALLED(maphist);
1696
1697 mutex_enter(&uobj->vmobjlock);
1698 /* locked: maps(read), amap(if there), uobj */
1699 /*
1700 * the following call to pgo_get does _not_ change locking state
1701 */
1702
1703 uvmexp.fltlget++;
1704 gotpages = flt->npages;
1705 (void) uobj->pgops->pgo_get(uobj,
1706 ufi->entry->offset + flt->startva - ufi->entry->start,
1707 pages, &gotpages, flt->centeridx,
1708 flt->access_type & MASK(ufi->entry), ufi->entry->advice, PGO_LOCKED);
1709
1710 /*
1711 * check for pages to map, if we got any
1712 */
1713
1714 if (gotpages == 0) {
1715 pages[flt->centeridx] = NULL;
1716 return;
1717 }
1718
1719 currva = flt->startva;
1720 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
1721 struct vm_page *curpg;
1722
1723 curpg = pages[lcv];
1724 if (curpg == NULL || curpg == PGO_DONTCARE) {
1725 continue;
1726 }
1727 KASSERT(curpg->uobject == uobj);
1728
1729 /*
1730 * if center page is resident and not PG_BUSY|PG_RELEASED
1731 * then pgo_get made it PG_BUSY for us and gave us a handle
1732 * to it.
1733 */
1734
1735 if (lcv == flt->centeridx) {
1736 UVMHIST_LOG(maphist, " got uobjpage "
1737 "(0x%x) with locked get",
1738 curpg, 0,0,0);
1739 } else {
1740 bool readonly = (curpg->flags & PG_RDONLY)
1741 || (curpg->loan_count > 0)
1742 || UVM_OBJ_NEEDS_WRITEFAULT(curpg->uobject);
1743
1744 uvm_fault_lower_neighbor(ufi, flt,
1745 currva, curpg, readonly);
1746 }
1747 }
1748 pmap_update(ufi->orig_map->pmap);
1749 }
1750
1751 /*
1752 * uvm_fault_lower_neighbor: enter h/w mapping of lower neighbor page.
1753 */
1754
1755 static void
1756 uvm_fault_lower_neighbor(
1757 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1758 vaddr_t currva, struct vm_page *pg, bool readonly)
1759 {
1760 UVMHIST_FUNC("uvm_fault_lower_neighor"); UVMHIST_CALLED(maphist);
1761
1762 /* locked: maps(read), amap(if there), uobj */
1763
1764 /*
1765 * calling pgo_get with PGO_LOCKED returns us pages which
1766 * are neither busy nor released, so we don't need to check
1767 * for this. we can just directly enter the pages.
1768 */
1769
1770 if (__predict_true((pg->flags & PG_DEVICE) == 0)) {
1771 mutex_enter(&uvm_pageqlock);
1772 uvm_pageenqueue(pg);
1773 mutex_exit(&uvm_pageqlock);
1774 }
1775 UVMHIST_LOG(maphist,
1776 " MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x",
1777 ufi->orig_map->pmap, currva, pg, 0);
1778 uvmexp.fltnomap++;
1779
1780 /*
1781 * Since this page isn't the page that's actually faulting,
1782 * ignore pmap_enter() failures; it's not critical that we
1783 * enter these right now.
1784 * NOTE: page can't be PG_WANTED or PG_RELEASED because we've
1785 * held the lock the whole time we've had the handle.
1786 */
1787 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1788 KASSERT((pg->flags & PG_RELEASED) == 0);
1789 KASSERT((pg->flags & PG_WANTED) == 0);
1790 KASSERT(!UVM_OBJ_IS_CLEAN(pg->uobject) ||
1791 (pg->flags & PG_CLEAN) != 0);
1792 pg->flags &= ~(PG_BUSY);
1793 UVM_PAGE_OWN(pg, NULL);
1794
1795 (void) pmap_enter(ufi->orig_map->pmap, currva,
1796 VM_PAGE_TO_PHYS(pg),
1797 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
1798 flt->enter_prot & MASK(ufi->entry),
1799 PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0));
1800 }
1801
1802 /*
1803 * uvm_fault_lower_io: get lower page from backing store.
1804 *
1805 * 1. unlock everything, because i/o will block.
1806 * 2. call pgo_get.
1807 * 3. if failed, recover.
1808 * 4. if succeeded, relock everything and verify things.
1809 */
1810
1811 static int
1812 uvm_fault_lower_io(
1813 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1814 struct uvm_object **ruobj, struct vm_page **ruobjpage)
1815 {
1816 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1817 struct uvm_object *uobj = *ruobj;
1818 struct vm_page *pg;
1819 bool locked;
1820 int gotpages;
1821 int error;
1822 voff_t uoff;
1823 UVMHIST_FUNC("uvm_fault_lower_io"); UVMHIST_CALLED(maphist);
1824
1825 /* update rusage counters */
1826 curlwp->l_ru.ru_majflt++;
1827
1828 /* locked: maps(read), amap(if there), uobj */
1829 uvmfault_unlockall(ufi, amap, NULL, NULL);
1830 /* locked: uobj */
1831
1832 uvmexp.fltget++;
1833 gotpages = 1;
1834 pg = NULL;
1835 uoff = (ufi->orig_rvaddr - ufi->entry->start) + ufi->entry->offset;
1836 error = uobj->pgops->pgo_get(uobj, uoff, &pg, &gotpages,
1837 0, flt->access_type & MASK(ufi->entry), ufi->entry->advice,
1838 PGO_SYNCIO);
1839 /* locked: pg(if no error) */
1840
1841 /*
1842 * recover from I/O
1843 */
1844
1845 if (error) {
1846 if (error == EAGAIN) {
1847 UVMHIST_LOG(maphist,
1848 " pgo_get says TRY AGAIN!",0,0,0,0);
1849 kpause("fltagain2", false, hz/2, NULL);
1850 return ERESTART;
1851 }
1852
1853 #if 0
1854 KASSERT(error != ERESTART);
1855 #else
1856 /* XXXUEBS don't re-fault? */
1857 if (error == ERESTART)
1858 error = EIO;
1859 #endif
1860
1861 UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)",
1862 error, 0,0,0);
1863 return error;
1864 }
1865
1866 /* locked: pg */
1867
1868 KASSERT(pg != 0);
1869
1870 if (pg != PGO_HOLE) {
1871 KASSERT((pg->flags & PG_BUSY) != 0);
1872
1873 if (pg != PGO_HOLE && (pg->flags & PG_DEVICE) == 0) {
1874 mutex_enter(&uvm_pageqlock);
1875 uvm_pageactivate(pg);
1876 mutex_exit(&uvm_pageqlock);
1877 }
1878 }
1879
1880 /*
1881 * re-verify the state of the world by first trying to relock
1882 * the maps. always relock the object.
1883 */
1884
1885 locked = uvmfault_relock(ufi);
1886 if (locked && amap)
1887 amap_lock(amap);
1888
1889 if (pg != PGO_HOLE) {
1890 /* might be changed */
1891 uobj = pg->uobject;
1892 } else {
1893 /* XIP hole page is shared, and it has no single owner */
1894 }
1895
1896 mutex_enter(&uobj->vmobjlock);
1897
1898 /* locked(locked): maps(read), amap(if !null), uobj, pg */
1899 /* locked(!locked): uobj, pg */
1900
1901 /*
1902 * verify that the page has not be released and re-verify
1903 * that amap slot is still free. if there is a problem,
1904 * we unlock and clean up.
1905 */
1906
1907 if ((pg != PGO_HOLE && (pg->flags & PG_RELEASED) != 0) ||
1908 (locked && amap && amap_lookup(&ufi->entry->aref,
1909 ufi->orig_rvaddr - ufi->entry->start))) {
1910 if (locked)
1911 uvmfault_unlockall(ufi, amap, NULL, NULL);
1912 locked = false;
1913 }
1914
1915 /*
1916 * didn't get the lock? release the page and retry.
1917 */
1918
1919 if (locked == false) {
1920 UVMHIST_LOG(maphist,
1921 " wasn't able to relock after fault: retry",
1922 0,0,0,0);
1923 if (pg != PGO_HOLE) {
1924 if (pg->flags & PG_WANTED) {
1925 wakeup(pg);
1926 }
1927 if (pg->flags & PG_RELEASED) {
1928 uvmexp.fltpgrele++;
1929 uvm_pagefree(pg);
1930 mutex_exit(&uobj->vmobjlock);
1931 return ERESTART;
1932 }
1933 pg->flags &= ~(PG_BUSY|PG_WANTED);
1934 UVM_PAGE_OWN(pg, NULL);
1935 }
1936 mutex_exit(&uobj->vmobjlock);
1937 return ERESTART;
1938 }
1939
1940 /*
1941 * we have the data in pg which is busy and
1942 * not released. we are holding object lock (so the page
1943 * can't be released on us).
1944 */
1945
1946 /* locked: maps(read), amap(if !null), uobj, pg */
1947
1948 *ruobj = uobj;
1949 *ruobjpage = pg;
1950 return 0;
1951 }
1952
1953 /*
1954 * uvm_fault_lower_direct: fault lower center page
1955 *
1956 * 1. adjust h/w mapping protection.
1957 * 2. if page is loaned, resolve.
1958 */
1959
1960 int
1961 uvm_fault_lower_direct(
1962 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1963 struct uvm_object *uobj, struct vm_page *uobjpage)
1964 {
1965 struct vm_page *pg;
1966 UVMHIST_FUNC("uvm_fault_lower_direct"); UVMHIST_CALLED(maphist);
1967
1968 /*
1969 * we are not promoting. if the mapping is COW ensure that we
1970 * don't give more access than we should (e.g. when doing a read
1971 * fault on a COPYONWRITE mapping we want to map the COW page in
1972 * R/O even though the entry protection could be R/W).
1973 *
1974 * set "pg" to the page we want to map in (uobjpage, usually)
1975 */
1976
1977 uvmexp.flt_obj++;
1978 if (UVM_ET_ISCOPYONWRITE(ufi->entry) ||
1979 uobjpage == PGO_HOLE ||
1980 UVM_OBJ_NEEDS_WRITEFAULT(uobjpage->uobject))
1981 flt->enter_prot &= ~VM_PROT_WRITE;
1982 pg = uobjpage; /* map in the actual object */
1983
1984 KASSERT(uobjpage != PGO_DONTCARE);
1985
1986 /*
1987 * we are faulting directly on the page. be careful
1988 * about writing to loaned pages...
1989 */
1990
1991 if (pg != PGO_HOLE && uobjpage->loan_count) {
1992 uvm_fault_lower_direct_loan(ufi, flt, uobj, &pg, &uobjpage);
1993 }
1994 KASSERT(pg == uobjpage);
1995
1996 return uvm_fault_lower_enter(ufi, flt, uobj, NULL, pg, uobjpage);
1997 }
1998
1999 /*
2000 * uvm_fault_lower_direct_loan: resolve loaned page.
2001 *
2002 * 1. if not cow'ing, adjust h/w mapping protection.
2003 * 2. if cow'ing, break loan.
2004 */
2005
2006 static int
2007 uvm_fault_lower_direct_loan(
2008 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2009 struct uvm_object *uobj, struct vm_page **rpg, struct vm_page **ruobjpage)
2010 {
2011 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2012 struct vm_page *pg;
2013 struct vm_page *uobjpage = *ruobjpage;
2014 UVMHIST_FUNC("uvm_fault_lower_direct_loan"); UVMHIST_CALLED(maphist);
2015
2016 if (!flt->cow_now) {
2017 /* read fault: cap the protection at readonly */
2018 /* cap! */
2019 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
2020 } else {
2021 /* write fault: must break the loan here */
2022
2023 pg = uvm_loanbreak(uobjpage);
2024 if (pg == NULL) {
2025
2026 /*
2027 * drop ownership of page, it can't be released
2028 */
2029
2030 if (uobjpage->flags & PG_WANTED)
2031 wakeup(uobjpage);
2032 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
2033 UVM_PAGE_OWN(uobjpage, NULL);
2034
2035 uvmfault_unlockall(ufi, amap, uobj, NULL);
2036 UVMHIST_LOG(maphist,
2037 " out of RAM breaking loan, waiting",
2038 0,0,0,0);
2039 uvmexp.fltnoram++;
2040 uvm_wait("flt_noram4");
2041 return ERESTART;
2042 }
2043 *rpg = pg;
2044 *ruobjpage = pg;
2045 }
2046 return 0;
2047 }
2048
2049 /*
2050 * uvm_fault_lower_promote: promote lower page.
2051 *
2052 * 1. call uvmfault_promote.
2053 * 2. fill in data.
2054 * 3. if not ZFOD, dispose old page.
2055 */
2056
2057 int
2058 uvm_fault_lower_promote(
2059 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2060 struct uvm_object *uobj, struct vm_page *uobjpage)
2061 {
2062 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2063 struct vm_anon *anon;
2064 struct vm_page *pg;
2065 int error;
2066 UVMHIST_FUNC("uvm_fault_lower_promote"); UVMHIST_CALLED(maphist);
2067
2068 /*
2069 * if we are going to promote the data to an anon we
2070 * allocate a blank anon here and plug it into our amap.
2071 */
2072 #ifdef DIAGNOSTIC
2073 if (amap == NULL)
2074 panic("uvm_fault: want to promote data, but no anon");
2075 #endif
2076 error = uvmfault_promote(ufi, NULL, uobjpage,
2077 &anon, &flt->anon_spare);
2078 switch (error) {
2079 case 0:
2080 break;
2081 case ERESTART:
2082 return ERESTART;
2083 default:
2084 return error;
2085 }
2086
2087 pg = anon->an_page;
2088
2089 /*
2090 * fill in the data
2091 */
2092
2093 if (uobjpage != PGO_DONTCARE && uobjpage != PGO_HOLE) {
2094 uvmexp.flt_prcopy++;
2095
2096 /*
2097 * promote to shared amap? make sure all sharing
2098 * procs see it
2099 */
2100
2101 if ((amap_flags(amap) & AMAP_SHARED) != 0) {
2102 pmap_page_protect(uobjpage, VM_PROT_NONE);
2103 /*
2104 * XXX: PAGE MIGHT BE WIRED!
2105 */
2106 }
2107
2108 /*
2109 * dispose of uobjpage. it can't be PG_RELEASED
2110 * since we still hold the object lock.
2111 * drop handle to uobj as well.
2112 */
2113
2114 if (uobjpage->flags & PG_WANTED)
2115 /* still have the obj lock */
2116 wakeup(uobjpage);
2117 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
2118 UVM_PAGE_OWN(uobjpage, NULL);
2119 mutex_exit(&uobj->vmobjlock);
2120 uobj = NULL;
2121
2122 UVMHIST_LOG(maphist,
2123 " promote uobjpage 0x%x to anon/page 0x%x/0x%x",
2124 uobjpage, anon, pg, 0);
2125
2126 } else {
2127 uvmexp.flt_przero++;
2128
2129 /*
2130 * Page is zero'd and marked dirty by
2131 * uvmfault_promote().
2132 */
2133
2134 UVMHIST_LOG(maphist," zero fill anon/page 0x%x/0%x%s",
2135 anon, pg, (pg == PGO_HOLE) ? " (xip hole)" : "", 0);
2136 }
2137
2138 return uvm_fault_lower_enter(ufi, flt, uobj, anon, pg, uobjpage);
2139 }
2140
2141 /*
2142 * uvm_fault_lower_enter: enter h/w mapping of lower page.
2143 */
2144
2145 int
2146 uvm_fault_lower_enter(
2147 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2148 struct uvm_object *uobj,
2149 struct vm_anon *anon, struct vm_page *pg, struct vm_page *uobjpage)
2150 {
2151 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2152 int error;
2153 UVMHIST_FUNC("uvm_fault_lower_enter"); UVMHIST_CALLED(maphist);
2154
2155 /*
2156 * locked:
2157 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj),
2158 * anon(if !null), pg(if anon)
2159 *
2160 * note: pg is either the uobjpage or the new page in the new anon
2161 */
2162 KASSERT(amap == NULL || mutex_owned(&amap->am_l));
2163 KASSERT(uobj == NULL || mutex_owned(&uobj->vmobjlock));
2164 KASSERT(uobj == NULL || uobjpage == PGO_HOLE || (uobjpage->flags & PG_BUSY) != 0);
2165 KASSERT(anon == NULL || mutex_owned(&anon->an_lock));
2166 KASSERT(pg == PGO_HOLE || (pg->flags & PG_BUSY) != 0);
2167
2168 #ifdef XIP
2169 if (pg == PGO_HOLE) {
2170 UVMHIST_LOG(maphist, "replacing PGO_HOLE with holepage",0,0,0,0);
2171 pg = uvm_page_holepage_alloc();
2172 UVMHIST_LOG(maphist,
2173 "PGO_HOLE replaced with pg %p (phys_addr=0x%lx)",
2174 pg, VM_PAGE_TO_PHYS(pg), 0, 0);
2175 KASSERT(pg != NULL);
2176 KASSERT((pg->flags & PG_RDONLY) != 0);
2177 }
2178 #endif
2179
2180 /*
2181 * all resources are present. we can now map it in and free our
2182 * resources.
2183 */
2184
2185 UVMHIST_LOG(maphist,
2186 " MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d",
2187 ufi->orig_map->pmap, ufi->orig_rvaddr, pg, flt->promote);
2188 KASSERT((flt->access_type & VM_PROT_WRITE) == 0 ||
2189 (pg->flags & PG_RDONLY) == 0);
2190 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr, VM_PAGE_TO_PHYS(pg),
2191 pg->flags & PG_RDONLY ? flt->enter_prot & ~VM_PROT_WRITE : flt->enter_prot,
2192 flt->access_type | PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
2193
2194 /*
2195 * No need to undo what we did; we can simply think of
2196 * this as the pmap throwing away the mapping information.
2197 *
2198 * We do, however, have to go through the ReFault path,
2199 * as the map may change while we're asleep.
2200 */
2201
2202 if (pg != uvm_page_holepage) {
2203 if (pg->flags & PG_WANTED)
2204 wakeup(pg);
2205
2206 /*
2207 * note that pg can't be PG_RELEASED since we did not drop
2208 * the object lock since the last time we checked.
2209 */
2210 KASSERT((pg->flags & PG_RELEASED) == 0);
2211
2212 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
2213 UVM_PAGE_OWN(pg, NULL);
2214 }
2215
2216 uvmfault_unlockall(ufi, amap, uobj, anon);
2217 if (!uvm_reclaimable()) {
2218 UVMHIST_LOG(maphist,
2219 "<- failed. out of VM",0,0,0,0);
2220 /* XXX instrumentation */
2221 error = ENOMEM;
2222 return error;
2223 }
2224 /* XXX instrumentation */
2225 uvm_wait("flt_pmfail2");
2226 return ERESTART;
2227 }
2228
2229 if (pg != uvm_page_holepage) {
2230 if (__predict_true((pg->flags & PG_DEVICE) == 0))
2231 uvm_fault_lower_done(ufi, flt, uobj, anon, pg);
2232
2233 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
2234 UVM_PAGE_OWN(pg, NULL);
2235 }
2236
2237 pmap_update(ufi->orig_map->pmap);
2238 uvmfault_unlockall(ufi, amap, uobj, anon);
2239
2240 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
2241 return 0;
2242 }
2243
2244 /*
2245 * uvm_fault_lower_done: queue lower center page.
2246 */
2247
2248 void
2249 uvm_fault_lower_done(
2250 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2251 struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg)
2252 {
2253 bool dropswap = false;
2254
2255 UVMHIST_FUNC("uvm_fault_lower_done"); UVMHIST_CALLED(maphist);
2256
2257 KASSERT(pg != uvm_page_holepage);
2258
2259 mutex_enter(&uvm_pageqlock);
2260 if (flt->wire_paging) {
2261 uvm_pagewire(pg);
2262 if (pg->pqflags & PQ_AOBJ) {
2263
2264 /*
2265 * since the now-wired page cannot be paged out,
2266 * release its swap resources for others to use.
2267 * since an aobj page with no swap cannot be PG_CLEAN,
2268 * clear its clean flag now.
2269 */
2270
2271 KASSERT(uobj != NULL);
2272 pg->flags &= ~(PG_CLEAN);
2273 dropswap = true;
2274 }
2275 } else {
2276 uvm_pageactivate(pg);
2277 }
2278 mutex_exit(&uvm_pageqlock);
2279
2280 if (dropswap) {
2281 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
2282 }
2283 if (pg->flags & PG_WANTED)
2284 wakeup(pg);
2285
2286 /*
2287 * note that pg can't be PG_RELEASED since we did not drop the object
2288 * lock since the last time we checked.
2289 */
2290 KASSERT((pg->flags & PG_RELEASED) == 0);
2291 }
2292
2293
2294 /*
2295 * uvm_fault_wire: wire down a range of virtual addresses in a map.
2296 *
2297 * => map may be read-locked by caller, but MUST NOT be write-locked.
2298 * => if map is read-locked, any operations which may cause map to
2299 * be write-locked in uvm_fault() must be taken care of by
2300 * the caller. See uvm_map_pageable().
2301 */
2302
2303 int
2304 uvm_fault_wire(struct vm_map *map, vaddr_t start, vaddr_t end,
2305 vm_prot_t access_type, int maxprot)
2306 {
2307 vaddr_t va;
2308 int error;
2309
2310 /*
2311 * now fault it in a page at a time. if the fault fails then we have
2312 * to undo what we have done. note that in uvm_fault VM_PROT_NONE
2313 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
2314 */
2315
2316 /*
2317 * XXX work around overflowing a vaddr_t. this prevents us from
2318 * wiring the last page in the address space, though.
2319 */
2320 if (start > end) {
2321 return EFAULT;
2322 }
2323
2324 for (va = start; va < end; va += PAGE_SIZE) {
2325 error = uvm_fault_internal(map, va, access_type,
2326 (maxprot ? UVM_FAULT_MAXPROT : 0) | UVM_FAULT_WIRE);
2327 if (error) {
2328 if (va != start) {
2329 uvm_fault_unwire(map, start, va);
2330 }
2331 return error;
2332 }
2333 }
2334 return 0;
2335 }
2336
2337 /*
2338 * uvm_fault_unwire(): unwire range of virtual space.
2339 */
2340
2341 void
2342 uvm_fault_unwire(struct vm_map *map, vaddr_t start, vaddr_t end)
2343 {
2344 vm_map_lock_read(map);
2345 uvm_fault_unwire_locked(map, start, end);
2346 vm_map_unlock_read(map);
2347 }
2348
2349 /*
2350 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
2351 *
2352 * => map must be at least read-locked.
2353 */
2354
2355 void
2356 uvm_fault_unwire_locked(struct vm_map *map, vaddr_t start, vaddr_t end)
2357 {
2358 struct vm_map_entry *entry;
2359 pmap_t pmap = vm_map_pmap(map);
2360 vaddr_t va;
2361 paddr_t pa;
2362 struct vm_page *pg;
2363
2364 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0);
2365
2366 /*
2367 * we assume that the area we are unwiring has actually been wired
2368 * in the first place. this means that we should be able to extract
2369 * the PAs from the pmap. we also lock out the page daemon so that
2370 * we can call uvm_pageunwire.
2371 */
2372
2373 mutex_enter(&uvm_pageqlock);
2374
2375 /*
2376 * find the beginning map entry for the region.
2377 */
2378
2379 KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map));
2380 if (uvm_map_lookup_entry(map, start, &entry) == false)
2381 panic("uvm_fault_unwire_locked: address not in map");
2382
2383 for (va = start; va < end; va += PAGE_SIZE) {
2384 if (pmap_extract(pmap, va, &pa) == false)
2385 continue;
2386
2387 /*
2388 * find the map entry for the current address.
2389 */
2390
2391 KASSERT(va >= entry->start);
2392 while (va >= entry->end) {
2393 KASSERT(entry->next != &map->header &&
2394 entry->next->start <= entry->end);
2395 entry = entry->next;
2396 }
2397
2398 /*
2399 * if the entry is no longer wired, tell the pmap.
2400 */
2401
2402 if (VM_MAPENT_ISWIRED(entry) == 0)
2403 pmap_unwire(pmap, va);
2404
2405 pg = PHYS_TO_VM_PAGE(pa);
2406 if (pg)
2407 uvm_pageunwire(pg);
2408 }
2409
2410 mutex_exit(&uvm_pageqlock);
2411 }
2412