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