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