uvm_fault.c revision 1.220 1 /* $NetBSD: uvm_fault.c,v 1.220 2020/03/20 18:50:09 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.220 2020/03/20 18:50:09 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(rw_write_held(anons[lcv]->an_lock));
198 pg = anons[lcv]->an_page;
199 if (pg && (pg->flags & PG_BUSY) == 0) {
200 uvm_pagelock(pg);
201 uvm_pagedeactivate(pg);
202 uvm_pageunlock(pg);
203 }
204 }
205 }
206
207 /*
208 * normal functions
209 */
210
211 /*
212 * uvmfault_amapcopy: clear "needs_copy" in a map.
213 *
214 * => called with VM data structures unlocked (usually, see below)
215 * => we get a write lock on the maps and clear needs_copy for a VA
216 * => if we are out of RAM we sleep (waiting for more)
217 */
218
219 static void
220 uvmfault_amapcopy(struct uvm_faultinfo *ufi)
221 {
222 for (;;) {
223
224 /*
225 * no mapping? give up.
226 */
227
228 if (uvmfault_lookup(ufi, true) == false)
229 return;
230
231 /*
232 * copy if needed.
233 */
234
235 if (UVM_ET_ISNEEDSCOPY(ufi->entry))
236 amap_copy(ufi->map, ufi->entry, AMAP_COPY_NOWAIT,
237 ufi->orig_rvaddr, ufi->orig_rvaddr + 1);
238
239 /*
240 * didn't work? must be out of RAM. unlock and sleep.
241 */
242
243 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
244 uvmfault_unlockmaps(ufi, true);
245 uvm_wait("fltamapcopy");
246 continue;
247 }
248
249 /*
250 * got it! unlock and return.
251 */
252
253 uvmfault_unlockmaps(ufi, true);
254 return;
255 }
256 /*NOTREACHED*/
257 }
258
259 /*
260 * uvmfault_anonget: get data in an anon into a non-busy, non-released
261 * page in that anon.
262 *
263 * => Map, amap and thus anon should be locked by caller.
264 * => If we fail, we unlock everything and error is returned.
265 * => If we are successful, return with everything still locked.
266 * => We do not move the page on the queues [gets moved later]. If we
267 * allocate a new page [we_own], it gets put on the queues. Either way,
268 * the result is that the page is on the queues at return time
269 * => For pages which are on loan from a uvm_object (and thus are not owned
270 * by the anon): if successful, return with the owning object locked.
271 * The caller must unlock this object when it unlocks everything else.
272 */
273
274 int
275 uvmfault_anonget(struct uvm_faultinfo *ufi, struct vm_amap *amap,
276 struct vm_anon *anon)
277 {
278 struct vm_page *pg;
279 int error;
280
281 UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist);
282 KASSERT(rw_write_held(anon->an_lock));
283 KASSERT(anon->an_lock == amap->am_lock);
284
285 /* Increment the counters.*/
286 cpu_count(CPU_COUNT_FLTANGET, 1);
287 if (anon->an_page) {
288 curlwp->l_ru.ru_minflt++;
289 } else {
290 curlwp->l_ru.ru_majflt++;
291 }
292 error = 0;
293
294 /*
295 * Loop until we get the anon data, or fail.
296 */
297
298 for (;;) {
299 bool we_own, locked;
300 /*
301 * Note: 'we_own' will become true if we set PG_BUSY on a page.
302 */
303 we_own = false;
304 pg = anon->an_page;
305
306 /*
307 * If there is a resident page and it is loaned, then anon
308 * may not own it. Call out to uvm_anon_lockloanpg() to
309 * identify and lock the real owner of the page.
310 */
311
312 if (pg && pg->loan_count)
313 pg = uvm_anon_lockloanpg(anon);
314
315 /*
316 * Is page resident? Make sure it is not busy/released.
317 */
318
319 if (pg) {
320
321 /*
322 * at this point, if the page has a uobject [meaning
323 * we have it on loan], then that uobject is locked
324 * by us! if the page is busy, we drop all the
325 * locks (including uobject) and try again.
326 */
327
328 if ((pg->flags & PG_BUSY) == 0) {
329 UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
330 return 0;
331 }
332 cpu_count(CPU_COUNT_FLTPGWAIT, 1);
333
334 /*
335 * The last unlock must be an atomic unlock and wait
336 * on the owner of page.
337 */
338
339 if (pg->uobject) {
340 /* Owner of page is UVM object. */
341 uvmfault_unlockall(ufi, amap, NULL);
342 UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
343 0,0,0);
344 uvm_pagewait(pg, pg->uobject->vmobjlock, "anonget1");
345 } else {
346 /* Owner of page is anon. */
347 uvmfault_unlockall(ufi, NULL, NULL);
348 UVMHIST_LOG(maphist, " unlock+wait on anon",0,
349 0,0,0);
350 uvm_pagewait(pg, anon->an_lock, "anonget2");
351 }
352 } else {
353 #if defined(VMSWAP)
354 /*
355 * No page, therefore allocate one.
356 */
357
358 pg = uvm_pagealloc(NULL,
359 ufi != NULL ? ufi->orig_rvaddr : 0,
360 anon, ufi != NULL ? UVM_FLAG_COLORMATCH : 0);
361 if (pg == NULL) {
362 /* Out of memory. Wait a little. */
363 uvmfault_unlockall(ufi, amap, NULL);
364 cpu_count(CPU_COUNT_FLTNORAM, 1);
365 UVMHIST_LOG(maphist, " noram -- UVM_WAIT",0,
366 0,0,0);
367 if (!uvm_reclaimable()) {
368 return ENOMEM;
369 }
370 uvm_wait("flt_noram1");
371 } else {
372 /* PG_BUSY bit is set. */
373 we_own = true;
374 uvmfault_unlockall(ufi, amap, NULL);
375
376 /*
377 * Pass a PG_BUSY+PG_FAKE clean page into
378 * the uvm_swap_get() function with all data
379 * structures unlocked. Note that it is OK
380 * to read an_swslot here, because we hold
381 * PG_BUSY on the page.
382 */
383 cpu_count(CPU_COUNT_PAGEINS, 1);
384 error = uvm_swap_get(pg, anon->an_swslot,
385 PGO_SYNCIO);
386
387 /*
388 * We clean up after the I/O below in the
389 * 'we_own' case.
390 */
391 }
392 #else
393 panic("%s: no page", __func__);
394 #endif /* defined(VMSWAP) */
395 }
396
397 /*
398 * Re-lock the map and anon.
399 */
400
401 locked = uvmfault_relock(ufi);
402 if (locked || we_own) {
403 rw_enter(anon->an_lock, RW_WRITER);
404 }
405
406 /*
407 * If we own the page (i.e. we set PG_BUSY), then we need
408 * to clean up after the I/O. There are three cases to
409 * consider:
410 *
411 * 1) Page was released during I/O: free anon and ReFault.
412 * 2) I/O not OK. Free the page and cause the fault to fail.
413 * 3) I/O OK! Activate the page and sync with the non-we_own
414 * case (i.e. drop anon lock if not locked).
415 */
416
417 if (we_own) {
418 #if defined(VMSWAP)
419 if (error) {
420
421 /*
422 * Remove the swap slot from the anon and
423 * mark the anon as having no real slot.
424 * Do not free the swap slot, thus preventing
425 * it from being used again.
426 */
427
428 if (anon->an_swslot > 0) {
429 uvm_swap_markbad(anon->an_swslot, 1);
430 }
431 anon->an_swslot = SWSLOT_BAD;
432
433 if ((pg->flags & PG_RELEASED) != 0) {
434 goto released;
435 }
436
437 /*
438 * Note: page was never !PG_BUSY, so it
439 * cannot be mapped and thus no need to
440 * pmap_page_protect() it.
441 */
442
443 uvm_pagefree(pg);
444
445 if (locked) {
446 uvmfault_unlockall(ufi, NULL, NULL);
447 }
448 rw_exit(anon->an_lock);
449 UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
450 return error;
451 }
452
453 if ((pg->flags & PG_RELEASED) != 0) {
454 released:
455 KASSERT(anon->an_ref == 0);
456
457 /*
458 * Released while we had unlocked amap.
459 */
460
461 if (locked) {
462 uvmfault_unlockall(ufi, NULL, NULL);
463 }
464 uvm_anon_release(anon);
465
466 if (error) {
467 UVMHIST_LOG(maphist,
468 "<- ERROR/RELEASED", 0,0,0,0);
469 return error;
470 }
471
472 UVMHIST_LOG(maphist, "<- RELEASED", 0,0,0,0);
473 return ERESTART;
474 }
475
476 /*
477 * We have successfully read the page, activate it.
478 */
479
480 uvm_pagelock(pg);
481 uvm_pageactivate(pg);
482 uvm_pagewakeup(pg);
483 uvm_pageunlock(pg);
484 pg->flags &= ~(PG_BUSY|PG_FAKE);
485 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_UNKNOWN);
486 UVM_PAGE_OWN(pg, NULL);
487 #else
488 panic("%s: we_own", __func__);
489 #endif /* defined(VMSWAP) */
490 }
491
492 /*
493 * We were not able to re-lock the map - restart the fault.
494 */
495
496 if (!locked) {
497 if (we_own) {
498 rw_exit(anon->an_lock);
499 }
500 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
501 return ERESTART;
502 }
503
504 /*
505 * Verify that no one has touched the amap and moved
506 * the anon on us.
507 */
508
509 if (ufi != NULL && amap_lookup(&ufi->entry->aref,
510 ufi->orig_rvaddr - ufi->entry->start) != anon) {
511
512 uvmfault_unlockall(ufi, amap, NULL);
513 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
514 return ERESTART;
515 }
516
517 /*
518 * Retry..
519 */
520
521 cpu_count(CPU_COUNT_FLTANRETRY, 1);
522 continue;
523 }
524 /*NOTREACHED*/
525 }
526
527 /*
528 * uvmfault_promote: promote data to a new anon. used for 1B and 2B.
529 *
530 * 1. allocate an anon and a page.
531 * 2. fill its contents.
532 * 3. put it into amap.
533 *
534 * => if we fail (result != 0) we unlock everything.
535 * => on success, return a new locked anon via 'nanon'.
536 * (*nanon)->an_page will be a resident, locked, dirty page.
537 * => it's caller's responsibility to put the promoted nanon->an_page to the
538 * page queue.
539 */
540
541 static int
542 uvmfault_promote(struct uvm_faultinfo *ufi,
543 struct vm_anon *oanon,
544 struct vm_page *uobjpage,
545 struct vm_anon **nanon, /* OUT: allocated anon */
546 struct vm_anon **spare)
547 {
548 struct vm_amap *amap = ufi->entry->aref.ar_amap;
549 struct uvm_object *uobj;
550 struct vm_anon *anon;
551 struct vm_page *pg;
552 struct vm_page *opg;
553 int error;
554 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
555
556 if (oanon) {
557 /* anon COW */
558 opg = oanon->an_page;
559 KASSERT(opg != NULL);
560 KASSERT(opg->uobject == NULL || opg->loan_count > 0);
561 } else if (uobjpage != PGO_DONTCARE) {
562 /* object-backed COW */
563 opg = uobjpage;
564 } else {
565 /* ZFOD */
566 opg = NULL;
567 }
568 if (opg != NULL) {
569 uobj = opg->uobject;
570 } else {
571 uobj = NULL;
572 }
573
574 KASSERT(amap != NULL);
575 KASSERT(uobjpage != NULL);
576 KASSERT(uobjpage == PGO_DONTCARE || (uobjpage->flags & PG_BUSY) != 0);
577 KASSERT(rw_write_held(amap->am_lock));
578 KASSERT(oanon == NULL || amap->am_lock == oanon->an_lock);
579 KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
580
581 if (*spare != NULL) {
582 anon = *spare;
583 *spare = NULL;
584 } else {
585 anon = uvm_analloc();
586 }
587 if (anon) {
588
589 /*
590 * The new anon is locked.
591 *
592 * if opg == NULL, we want a zero'd, dirty page,
593 * so have uvm_pagealloc() do that for us.
594 */
595
596 KASSERT(anon->an_lock == NULL);
597 anon->an_lock = amap->am_lock;
598 pg = uvm_pagealloc(NULL, ufi->orig_rvaddr, anon,
599 UVM_FLAG_COLORMATCH | (opg == NULL ? UVM_PGA_ZERO : 0));
600 if (pg == NULL) {
601 anon->an_lock = NULL;
602 }
603 } else {
604 pg = NULL;
605 }
606
607 /*
608 * out of memory resources?
609 */
610
611 if (pg == NULL) {
612 /* save anon for the next try. */
613 if (anon != NULL) {
614 *spare = anon;
615 }
616
617 /* unlock and fail ... */
618 uvm_page_unbusy(&uobjpage, 1);
619 uvmfault_unlockall(ufi, amap, uobj);
620 if (!uvm_reclaimable()) {
621 UVMHIST_LOG(maphist, "out of VM", 0,0,0,0);
622 cpu_count(CPU_COUNT_FLTNOANON, 1);
623 error = ENOMEM;
624 goto done;
625 }
626
627 UVMHIST_LOG(maphist, "out of RAM, waiting for more", 0,0,0,0);
628 cpu_count(CPU_COUNT_FLTNORAM, 1);
629 uvm_wait("flt_noram5");
630 error = ERESTART;
631 goto done;
632 }
633
634 /* copy page [pg now dirty] */
635 if (opg) {
636 uvm_pagecopy(opg, pg);
637 }
638 KASSERT(uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_DIRTY);
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 *);
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 rw_enter(uobj->vmobjlock, RW_WRITER);
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 @ %#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 = %#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, RW_WRITER);
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 || rw_write_held(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 rw_enter(uobj->vmobjlock, RW_WRITER);
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 bool entered;
1170 UVMHIST_FUNC("uvm_fault_upper_lookup"); UVMHIST_CALLED(maphist);
1171
1172 /* locked: maps(read), amap(if there) */
1173 KASSERT(amap == NULL || rw_write_held(amap->am_lock));
1174
1175 /*
1176 * map in the backpages and frontpages we found in the amap in hopes
1177 * of preventing future faults. we also init the pages[] array as
1178 * we go.
1179 */
1180
1181 currva = flt->startva;
1182 shadowed = false;
1183 entered = false;
1184 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
1185 /*
1186 * unmapped or center page. check if any anon at this level.
1187 */
1188 if (amap == NULL || anons[lcv] == NULL) {
1189 pages[lcv] = NULL;
1190 continue;
1191 }
1192
1193 /*
1194 * check for present page and map if possible. re-activate it.
1195 */
1196
1197 pages[lcv] = PGO_DONTCARE;
1198 if (lcv == flt->centeridx) { /* save center for later! */
1199 shadowed = true;
1200 continue;
1201 }
1202
1203 struct vm_anon *anon = anons[lcv];
1204 struct vm_page *pg = anon->an_page;
1205
1206 KASSERT(anon->an_lock == amap->am_lock);
1207
1208 /*
1209 * ignore loaned and busy pages.
1210 * don't play with VAs that are already mapped.
1211 */
1212
1213 if (pg && pg->loan_count == 0 && (pg->flags & PG_BUSY) == 0 &&
1214 !pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
1215 uvm_fault_upper_neighbor(ufi, flt, currva,
1216 pg, anon->an_ref > 1);
1217 entered = true;
1218 }
1219 }
1220 if (entered) {
1221 pmap_update(ufi->orig_map->pmap);
1222 }
1223
1224 /* locked: maps(read), amap(if there) */
1225 KASSERT(amap == NULL || rw_write_held(amap->am_lock));
1226 /* (shadowed == true) if there is an anon at the faulting address */
1227 UVMHIST_LOG(maphist, " shadowed=%jd, will_get=%jd", shadowed,
1228 (ufi->entry->object.uvm_obj && shadowed != false),0,0);
1229
1230 /*
1231 * note that if we are really short of RAM we could sleep in the above
1232 * call to pmap_enter with everything locked. bad?
1233 *
1234 * XXX Actually, that is bad; pmap_enter() should just fail in that
1235 * XXX case. --thorpej
1236 */
1237
1238 return 0;
1239 }
1240
1241 /*
1242 * uvm_fault_upper_neighbor: enter single upper neighbor page.
1243 *
1244 * => called with amap and anon locked.
1245 */
1246
1247 static void
1248 uvm_fault_upper_neighbor(
1249 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1250 vaddr_t currva, struct vm_page *pg, bool readonly)
1251 {
1252 UVMHIST_FUNC("uvm_fault_upper_neighbor"); UVMHIST_CALLED(maphist);
1253
1254 /* locked: amap, anon */
1255
1256 KASSERT(pg->uobject == NULL);
1257 KASSERT(pg->uanon != NULL);
1258 KASSERT(rw_write_held(pg->uanon->an_lock));
1259 KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);
1260
1261 uvm_pagelock(pg);
1262 uvm_pageenqueue(pg);
1263 uvm_pageunlock(pg);
1264 UVMHIST_LOG(maphist,
1265 " MAPPING: n anon: pm=%#jx, va=%#jx, pg=%#jx",
1266 (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
1267 cpu_count(CPU_COUNT_FLTNAMAP, 1);
1268
1269 /*
1270 * Since this page isn't the page that's actually faulting,
1271 * ignore pmap_enter() failures; it's not critical that we
1272 * enter these right now.
1273 */
1274
1275 (void) pmap_enter(ufi->orig_map->pmap, currva,
1276 VM_PAGE_TO_PHYS(pg),
1277 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
1278 flt->enter_prot,
1279 PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0));
1280 }
1281
1282 /*
1283 * uvm_fault_upper: handle upper fault.
1284 *
1285 * 1. acquire anon lock.
1286 * 2. get anon. let uvmfault_anonget do the dirty work.
1287 * 3. handle loan.
1288 * 4. dispatch direct or promote handlers.
1289 */
1290
1291 static int
1292 uvm_fault_upper(
1293 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1294 struct vm_anon **anons)
1295 {
1296 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1297 struct vm_anon * const anon = anons[flt->centeridx];
1298 struct uvm_object *uobj;
1299 int error;
1300 UVMHIST_FUNC("uvm_fault_upper"); UVMHIST_CALLED(maphist);
1301
1302 /* locked: maps(read), amap, anon */
1303 KASSERT(rw_write_held(amap->am_lock));
1304 KASSERT(anon->an_lock == amap->am_lock);
1305
1306 /*
1307 * handle case 1: fault on an anon in our amap
1308 */
1309
1310 UVMHIST_LOG(maphist, " case 1 fault: anon=%#jx",
1311 (uintptr_t)anon, 0, 0, 0);
1312
1313 /*
1314 * no matter if we have case 1A or case 1B we are going to need to
1315 * have the anon's memory resident. ensure that now.
1316 */
1317
1318 /*
1319 * let uvmfault_anonget do the dirty work.
1320 * if it fails (!OK) it will unlock everything for us.
1321 * if it succeeds, locks are still valid and locked.
1322 * also, if it is OK, then the anon's page is on the queues.
1323 * if the page is on loan from a uvm_object, then anonget will
1324 * lock that object for us if it does not fail.
1325 */
1326
1327 error = uvmfault_anonget(ufi, amap, anon);
1328 switch (error) {
1329 case 0:
1330 break;
1331
1332 case ERESTART:
1333 return ERESTART;
1334
1335 case EAGAIN:
1336 kpause("fltagain1", false, hz/2, NULL);
1337 return ERESTART;
1338
1339 default:
1340 return error;
1341 }
1342
1343 /*
1344 * uobj is non null if the page is on loan from an object (i.e. uobj)
1345 */
1346
1347 uobj = anon->an_page->uobject; /* locked by anonget if !NULL */
1348
1349 /* locked: maps(read), amap, anon, uobj(if one) */
1350 KASSERT(rw_write_held(amap->am_lock));
1351 KASSERT(anon->an_lock == amap->am_lock);
1352 KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
1353
1354 /*
1355 * special handling for loaned pages
1356 */
1357
1358 if (anon->an_page->loan_count) {
1359 error = uvm_fault_upper_loan(ufi, flt, anon, &uobj);
1360 if (error != 0)
1361 return error;
1362 }
1363
1364 /*
1365 * if we are case 1B then we will need to allocate a new blank
1366 * anon to transfer the data into. note that we have a lock
1367 * on anon, so no one can busy or release the page until we are done.
1368 * also note that the ref count can't drop to zero here because
1369 * it is > 1 and we are only dropping one ref.
1370 *
1371 * in the (hopefully very rare) case that we are out of RAM we
1372 * will unlock, wait for more RAM, and refault.
1373 *
1374 * if we are out of anon VM we kill the process (XXX: could wait?).
1375 */
1376
1377 if (flt->cow_now && anon->an_ref > 1) {
1378 flt->promote = true;
1379 error = uvm_fault_upper_promote(ufi, flt, uobj, anon);
1380 } else {
1381 error = uvm_fault_upper_direct(ufi, flt, uobj, anon);
1382 }
1383 return error;
1384 }
1385
1386 /*
1387 * uvm_fault_upper_loan: handle loaned upper page.
1388 *
1389 * 1. if not cow'ing now, simply adjust flt->enter_prot.
1390 * 2. if cow'ing now, and if ref count is 1, break loan.
1391 */
1392
1393 static int
1394 uvm_fault_upper_loan(
1395 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1396 struct vm_anon *anon, struct uvm_object **ruobj)
1397 {
1398 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1399 int error = 0;
1400 UVMHIST_FUNC("uvm_fault_upper_loan"); UVMHIST_CALLED(maphist);
1401
1402 if (!flt->cow_now) {
1403
1404 /*
1405 * for read faults on loaned pages we just cap the
1406 * protection at read-only.
1407 */
1408
1409 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1410
1411 } else {
1412 /*
1413 * note that we can't allow writes into a loaned page!
1414 *
1415 * if we have a write fault on a loaned page in an
1416 * anon then we need to look at the anon's ref count.
1417 * if it is greater than one then we are going to do
1418 * a normal copy-on-write fault into a new anon (this
1419 * is not a problem). however, if the reference count
1420 * is one (a case where we would normally allow a
1421 * write directly to the page) then we need to kill
1422 * the loan before we continue.
1423 */
1424
1425 /* >1 case is already ok */
1426 if (anon->an_ref == 1) {
1427 error = uvm_loanbreak_anon(anon, *ruobj);
1428 if (error != 0) {
1429 uvmfault_unlockall(ufi, amap, *ruobj);
1430 uvm_wait("flt_noram2");
1431 return ERESTART;
1432 }
1433 /* if we were a loan receiver uobj is gone */
1434 if (*ruobj)
1435 *ruobj = NULL;
1436 }
1437 }
1438 return error;
1439 }
1440
1441 /*
1442 * uvm_fault_upper_promote: promote upper page.
1443 *
1444 * 1. call uvmfault_promote.
1445 * 2. enqueue page.
1446 * 3. deref.
1447 * 4. pass page to uvm_fault_upper_enter.
1448 */
1449
1450 static int
1451 uvm_fault_upper_promote(
1452 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1453 struct uvm_object *uobj, struct vm_anon *anon)
1454 {
1455 struct vm_anon * const oanon = anon;
1456 struct vm_page *pg;
1457 int error;
1458 UVMHIST_FUNC("uvm_fault_upper_promote"); UVMHIST_CALLED(maphist);
1459
1460 UVMHIST_LOG(maphist, " case 1B: COW fault",0,0,0,0);
1461 cpu_count(CPU_COUNT_FLT_ACOW, 1);
1462
1463 error = uvmfault_promote(ufi, oanon, PGO_DONTCARE, &anon,
1464 &flt->anon_spare);
1465 switch (error) {
1466 case 0:
1467 break;
1468 case ERESTART:
1469 return ERESTART;
1470 default:
1471 return error;
1472 }
1473
1474 KASSERT(anon == NULL || anon->an_lock == oanon->an_lock);
1475
1476 pg = anon->an_page;
1477 /* uvm_fault_upper_done will activate the page */
1478 uvm_pagelock(pg);
1479 uvm_pageenqueue(pg);
1480 uvm_pageunlock(pg);
1481 pg->flags &= ~(PG_BUSY|PG_FAKE);
1482 UVM_PAGE_OWN(pg, NULL);
1483
1484 /* deref: can not drop to zero here by defn! */
1485 KASSERT(oanon->an_ref > 1);
1486 oanon->an_ref--;
1487
1488 /*
1489 * note: oanon is still locked, as is the new anon. we
1490 * need to check for this later when we unlock oanon; if
1491 * oanon != anon, we'll have to unlock anon, too.
1492 */
1493
1494 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
1495 }
1496
1497 /*
1498 * uvm_fault_upper_direct: handle direct fault.
1499 */
1500
1501 static int
1502 uvm_fault_upper_direct(
1503 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1504 struct uvm_object *uobj, struct vm_anon *anon)
1505 {
1506 struct vm_anon * const oanon = anon;
1507 struct vm_page *pg;
1508 UVMHIST_FUNC("uvm_fault_upper_direct"); UVMHIST_CALLED(maphist);
1509
1510 cpu_count(CPU_COUNT_FLT_ANON, 1);
1511 pg = anon->an_page;
1512 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */
1513 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
1514
1515 return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
1516 }
1517
1518 /*
1519 * uvm_fault_upper_enter: enter h/w mapping of upper page.
1520 */
1521
1522 static int
1523 uvm_fault_upper_enter(
1524 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1525 struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg,
1526 struct vm_anon *oanon)
1527 {
1528 struct pmap *pmap = ufi->orig_map->pmap;
1529 vaddr_t va = ufi->orig_rvaddr;
1530 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1531 UVMHIST_FUNC("uvm_fault_upper_enter"); UVMHIST_CALLED(maphist);
1532
1533 /* locked: maps(read), amap, oanon, anon(if different from oanon) */
1534 KASSERT(rw_write_held(amap->am_lock));
1535 KASSERT(anon->an_lock == amap->am_lock);
1536 KASSERT(oanon->an_lock == amap->am_lock);
1537 KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
1538 KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);
1539
1540 /*
1541 * now map the page in.
1542 */
1543
1544 UVMHIST_LOG(maphist,
1545 " MAPPING: anon: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
1546 (uintptr_t)pmap, va, (uintptr_t)pg, flt->promote);
1547 if (pmap_enter(pmap, va, VM_PAGE_TO_PHYS(pg),
1548 flt->enter_prot, flt->access_type | PMAP_CANFAIL |
1549 (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
1550
1551 /*
1552 * If pmap_enter() fails, it must not leave behind an existing
1553 * pmap entry. In particular, a now-stale entry for a different
1554 * page would leave the pmap inconsistent with the vm_map.
1555 * This is not to imply that pmap_enter() should remove an
1556 * existing mapping in such a situation (since that could create
1557 * different problems, eg. if the existing mapping is wired),
1558 * but rather that the pmap should be designed such that it
1559 * never needs to fail when the new mapping is replacing an
1560 * existing mapping and the new page has no existing mappings.
1561 */
1562
1563 KASSERT(!pmap_extract(pmap, va, NULL));
1564
1565 /*
1566 * No need to undo what we did; we can simply think of
1567 * this as the pmap throwing away the mapping information.
1568 *
1569 * We do, however, have to go through the ReFault path,
1570 * as the map may change while we're asleep.
1571 */
1572
1573 uvmfault_unlockall(ufi, amap, uobj);
1574 if (!uvm_reclaimable()) {
1575 UVMHIST_LOG(maphist,
1576 "<- failed. out of VM",0,0,0,0);
1577 /* XXX instrumentation */
1578 return ENOMEM;
1579 }
1580 /* XXX instrumentation */
1581 uvm_wait("flt_pmfail1");
1582 return ERESTART;
1583 }
1584
1585 uvm_fault_upper_done(ufi, flt, anon, pg);
1586
1587 /*
1588 * done case 1! finish up by unlocking everything and returning success
1589 */
1590
1591 pmap_update(pmap);
1592 uvmfault_unlockall(ufi, amap, uobj);
1593 return 0;
1594 }
1595
1596 /*
1597 * uvm_fault_upper_done: queue upper center page.
1598 */
1599
1600 static void
1601 uvm_fault_upper_done(
1602 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1603 struct vm_anon *anon, struct vm_page *pg)
1604 {
1605 const bool wire_paging = flt->wire_paging;
1606
1607 UVMHIST_FUNC("uvm_fault_upper_done"); UVMHIST_CALLED(maphist);
1608
1609 /*
1610 * ... update the page queues.
1611 */
1612
1613 uvm_pagelock(pg);
1614 if (wire_paging) {
1615 uvm_pagewire(pg);
1616 } else {
1617 uvm_pageactivate(pg);
1618 }
1619 uvm_pageunlock(pg);
1620
1621 if (wire_paging) {
1622 /*
1623 * since the now-wired page cannot be paged out,
1624 * release its swap resources for others to use.
1625 * and since an anon with no swap cannot be clean,
1626 * mark it dirty now.
1627 */
1628
1629 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
1630 uvm_anon_dropswap(anon);
1631 }
1632 }
1633
1634 /*
1635 * uvm_fault_lower: handle lower fault.
1636 *
1637 * 1. check uobj
1638 * 1.1. if null, ZFOD.
1639 * 1.2. if not null, look up unnmapped neighbor pages.
1640 * 2. for center page, check if promote.
1641 * 2.1. ZFOD always needs promotion.
1642 * 2.2. other uobjs, when entry is marked COW (usually MAP_PRIVATE vnode).
1643 * 3. if uobj is not ZFOD and page is not found, do i/o.
1644 * 4. dispatch either direct / promote fault.
1645 */
1646
1647 static int
1648 uvm_fault_lower(
1649 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
1650 struct vm_page **pages)
1651 {
1652 struct vm_amap *amap __diagused = ufi->entry->aref.ar_amap;
1653 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1654 struct vm_page *uobjpage;
1655 int error;
1656 UVMHIST_FUNC("uvm_fault_lower"); UVMHIST_CALLED(maphist);
1657
1658 /*
1659 * now, if the desired page is not shadowed by the amap and we have
1660 * a backing object that does not have a special fault routine, then
1661 * we ask (with pgo_get) the object for resident pages that we care
1662 * about and attempt to map them in. we do not let pgo_get block
1663 * (PGO_LOCKED).
1664 */
1665
1666 if (uobj == NULL) {
1667 /* zero fill; don't care neighbor pages */
1668 uobjpage = NULL;
1669 } else {
1670 uvm_fault_lower_lookup(ufi, flt, pages);
1671 uobjpage = pages[flt->centeridx];
1672 }
1673
1674 /*
1675 * note that at this point we are done with any front or back pages.
1676 * we are now going to focus on the center page (i.e. the one we've
1677 * faulted on). if we have faulted on the upper (anon) layer
1678 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
1679 * not touched it yet). if we have faulted on the bottom (uobj)
1680 * layer [i.e. case 2] and the page was both present and available,
1681 * then we've got a pointer to it as "uobjpage" and we've already
1682 * made it BUSY.
1683 */
1684
1685 /*
1686 * locked:
1687 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
1688 */
1689 KASSERT(amap == NULL || rw_write_held(amap->am_lock));
1690 KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
1691 KASSERT(uobjpage == NULL || (uobjpage->flags & PG_BUSY) != 0);
1692
1693 /*
1694 * note that uobjpage can not be PGO_DONTCARE at this point. we now
1695 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we
1696 * have a backing object, check and see if we are going to promote
1697 * the data up to an anon during the fault.
1698 */
1699
1700 if (uobj == NULL) {
1701 uobjpage = PGO_DONTCARE;
1702 flt->promote = true; /* always need anon here */
1703 } else {
1704 KASSERT(uobjpage != PGO_DONTCARE);
1705 flt->promote = flt->cow_now && UVM_ET_ISCOPYONWRITE(ufi->entry);
1706 }
1707 UVMHIST_LOG(maphist, " case 2 fault: promote=%jd, zfill=%jd",
1708 flt->promote, (uobj == NULL), 0,0);
1709
1710 /*
1711 * if uobjpage is not null then we do not need to do I/O to get the
1712 * uobjpage.
1713 *
1714 * if uobjpage is null, then we need to unlock and ask the pager to
1715 * get the data for us. once we have the data, we need to reverify
1716 * the state the world. we are currently not holding any resources.
1717 */
1718
1719 if (uobjpage) {
1720 /* update rusage counters */
1721 curlwp->l_ru.ru_minflt++;
1722 } else {
1723 error = uvm_fault_lower_io(ufi, flt, &uobj, &uobjpage);
1724 if (error != 0)
1725 return error;
1726 }
1727
1728 /*
1729 * locked:
1730 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
1731 */
1732 KASSERT(amap == NULL || rw_write_held(amap->am_lock));
1733 KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
1734 KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0);
1735
1736 /*
1737 * notes:
1738 * - at this point uobjpage can not be NULL
1739 * - at this point uobjpage can not be PG_RELEASED (since we checked
1740 * for it above)
1741 * - at this point uobjpage could be waited on (handle later)
1742 */
1743
1744 KASSERT(uobjpage != NULL);
1745 KASSERT(uobj == NULL || uobj == uobjpage->uobject);
1746 KASSERT(uobj == NULL || !UVM_OBJ_IS_CLEAN(uobjpage->uobject) ||
1747 uvm_pagegetdirty(uobjpage) == UVM_PAGE_STATUS_CLEAN);
1748
1749 if (!flt->promote) {
1750 error = uvm_fault_lower_direct(ufi, flt, uobj, uobjpage);
1751 } else {
1752 error = uvm_fault_lower_promote(ufi, flt, uobj, uobjpage);
1753 }
1754 return error;
1755 }
1756
1757 /*
1758 * uvm_fault_lower_lookup: look up on-memory uobj pages.
1759 *
1760 * 1. get on-memory pages.
1761 * 2. if failed, give up (get only center page later).
1762 * 3. if succeeded, enter h/w mapping of neighbor pages.
1763 */
1764
1765 static void
1766 uvm_fault_lower_lookup(
1767 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1768 struct vm_page **pages)
1769 {
1770 struct uvm_object *uobj = ufi->entry->object.uvm_obj;
1771 int lcv, gotpages;
1772 vaddr_t currva;
1773 UVMHIST_FUNC("uvm_fault_lower_lookup"); UVMHIST_CALLED(maphist);
1774
1775 rw_enter(uobj->vmobjlock, RW_WRITER);
1776 /* Locked: maps(read), amap(if there), uobj */
1777
1778 cpu_count(CPU_COUNT_FLTLGET, 1);
1779 gotpages = flt->npages;
1780 (void) uobj->pgops->pgo_get(uobj,
1781 ufi->entry->offset + flt->startva - ufi->entry->start,
1782 pages, &gotpages, flt->centeridx,
1783 flt->access_type & MASK(ufi->entry), ufi->entry->advice, PGO_LOCKED);
1784
1785 KASSERT(rw_write_held(uobj->vmobjlock));
1786
1787 /*
1788 * check for pages to map, if we got any
1789 */
1790
1791 if (gotpages == 0) {
1792 pages[flt->centeridx] = NULL;
1793 return;
1794 }
1795
1796 currva = flt->startva;
1797 for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
1798 struct vm_page *curpg;
1799
1800 curpg = pages[lcv];
1801 if (curpg == NULL || curpg == PGO_DONTCARE) {
1802 continue;
1803 }
1804 KASSERT(curpg->uobject == uobj);
1805
1806 /*
1807 * if center page is resident and not PG_BUSY|PG_RELEASED
1808 * then pgo_get made it PG_BUSY for us and gave us a handle
1809 * to it.
1810 */
1811
1812 if (lcv == flt->centeridx) {
1813 UVMHIST_LOG(maphist, " got uobjpage (%#jx) "
1814 "with locked get", (uintptr_t)curpg, 0, 0, 0);
1815 } else {
1816 uvm_fault_lower_neighbor(ufi, flt, currva, curpg);
1817 }
1818 }
1819 pmap_update(ufi->orig_map->pmap);
1820 }
1821
1822 /*
1823 * uvm_fault_lower_neighbor: enter h/w mapping of lower neighbor page.
1824 */
1825
1826 static void
1827 uvm_fault_lower_neighbor(
1828 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1829 vaddr_t currva, struct vm_page *pg)
1830 {
1831 const bool readonly = uvm_pagereadonly_p(pg) || pg->loan_count > 0;
1832 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1833
1834 /* locked: maps(read), amap(if there), uobj */
1835
1836 /*
1837 * calling pgo_get with PGO_LOCKED returns us pages which
1838 * are neither busy nor released, so we don't need to check
1839 * for this. we can just directly enter the pages.
1840 */
1841
1842 uvm_pagelock(pg);
1843 uvm_pageenqueue(pg);
1844 uvm_pageunlock(pg);
1845 UVMHIST_LOG(maphist,
1846 " MAPPING: n obj: pm=%#jx, va=%#jx, pg=%#jx",
1847 (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
1848 cpu_count(CPU_COUNT_FLTNOMAP, 1);
1849
1850 /*
1851 * Since this page isn't the page that's actually faulting,
1852 * ignore pmap_enter() failures; it's not critical that we
1853 * enter these right now.
1854 * NOTE: page can't be waited on or PG_RELEASED because we've
1855 * held the lock the whole time we've had the handle.
1856 */
1857 KASSERT((pg->flags & PG_PAGEOUT) == 0);
1858 KASSERT((pg->flags & PG_RELEASED) == 0);
1859 KASSERT(!UVM_OBJ_IS_CLEAN(pg->uobject) ||
1860 uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_CLEAN);
1861 pg->flags &= ~(PG_BUSY);
1862 UVM_PAGE_OWN(pg, NULL);
1863
1864 KASSERT(rw_write_held(pg->uobject->vmobjlock));
1865
1866 const vm_prot_t mapprot =
1867 readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
1868 flt->enter_prot & MASK(ufi->entry);
1869 const u_int mapflags =
1870 PMAP_CANFAIL | (flt->wire_mapping ? (mapprot | PMAP_WIRED) : 0);
1871 (void) pmap_enter(ufi->orig_map->pmap, currva,
1872 VM_PAGE_TO_PHYS(pg), mapprot, mapflags);
1873 }
1874
1875 /*
1876 * uvm_fault_lower_io: get lower page from backing store.
1877 *
1878 * 1. unlock everything, because i/o will block.
1879 * 2. call pgo_get.
1880 * 3. if failed, recover.
1881 * 4. if succeeded, relock everything and verify things.
1882 */
1883
1884 static int
1885 uvm_fault_lower_io(
1886 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
1887 struct uvm_object **ruobj, struct vm_page **ruobjpage)
1888 {
1889 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
1890 struct uvm_object *uobj = *ruobj;
1891 struct vm_page *pg;
1892 bool locked;
1893 int gotpages;
1894 int error;
1895 voff_t uoff;
1896 vm_prot_t access_type;
1897 int advice;
1898 UVMHIST_FUNC("uvm_fault_lower_io"); UVMHIST_CALLED(maphist);
1899
1900 /* update rusage counters */
1901 curlwp->l_ru.ru_majflt++;
1902
1903 /* grab everything we need from the entry before we unlock */
1904 uoff = (ufi->orig_rvaddr - ufi->entry->start) + ufi->entry->offset;
1905 access_type = flt->access_type & MASK(ufi->entry);
1906 advice = ufi->entry->advice;
1907
1908 /* Locked: maps(read), amap(if there), uobj */
1909 uvmfault_unlockall(ufi, amap, NULL);
1910
1911 /* Locked: uobj */
1912 KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
1913
1914 cpu_count(CPU_COUNT_FLTGET, 1);
1915 gotpages = 1;
1916 pg = NULL;
1917 error = uobj->pgops->pgo_get(uobj, uoff, &pg, &gotpages,
1918 0, access_type, advice, PGO_SYNCIO);
1919 /* locked: pg(if no error) */
1920
1921 /*
1922 * recover from I/O
1923 */
1924
1925 if (error) {
1926 if (error == EAGAIN) {
1927 UVMHIST_LOG(maphist,
1928 " pgo_get says TRY AGAIN!",0,0,0,0);
1929 kpause("fltagain2", false, hz/2, NULL);
1930 return ERESTART;
1931 }
1932
1933 #if 0
1934 KASSERT(error != ERESTART);
1935 #else
1936 /* XXXUEBS don't re-fault? */
1937 if (error == ERESTART)
1938 error = EIO;
1939 #endif
1940
1941 UVMHIST_LOG(maphist, "<- pgo_get failed (code %jd)",
1942 error, 0,0,0);
1943 return error;
1944 }
1945
1946 /*
1947 * re-verify the state of the world by first trying to relock
1948 * the maps. always relock the object.
1949 */
1950
1951 locked = uvmfault_relock(ufi);
1952 if (locked && amap)
1953 amap_lock(amap, RW_WRITER);
1954
1955 /* might be changed */
1956 uobj = pg->uobject;
1957
1958 rw_enter(uobj->vmobjlock, RW_WRITER);
1959 KASSERT((pg->flags & PG_BUSY) != 0);
1960
1961 uvm_pagelock(pg);
1962 uvm_pageactivate(pg);
1963 uvm_pageunlock(pg);
1964
1965 /* locked(locked): maps(read), amap(if !null), uobj, pg */
1966 /* locked(!locked): uobj, pg */
1967
1968 /*
1969 * verify that the page has not be released and re-verify
1970 * that amap slot is still free. if there is a problem,
1971 * we unlock and clean up.
1972 */
1973
1974 if ((pg->flags & PG_RELEASED) != 0 ||
1975 (locked && amap && amap_lookup(&ufi->entry->aref,
1976 ufi->orig_rvaddr - ufi->entry->start))) {
1977 if (locked)
1978 uvmfault_unlockall(ufi, amap, NULL);
1979 locked = false;
1980 }
1981
1982 /*
1983 * didn't get the lock? release the page and retry.
1984 */
1985
1986 if (locked == false) {
1987 UVMHIST_LOG(maphist,
1988 " wasn't able to relock after fault: retry",
1989 0,0,0,0);
1990 if ((pg->flags & PG_RELEASED) == 0) {
1991 pg->flags &= ~PG_BUSY;
1992 uvm_pagelock(pg);
1993 uvm_pagewakeup(pg);
1994 uvm_pageunlock(pg);
1995 UVM_PAGE_OWN(pg, NULL);
1996 } else {
1997 cpu_count(CPU_COUNT_FLTPGRELE, 1);
1998 uvm_pagefree(pg);
1999 }
2000 rw_exit(uobj->vmobjlock);
2001 return ERESTART;
2002 }
2003
2004 /*
2005 * we have the data in pg which is busy and
2006 * not released. we are holding object lock (so the page
2007 * can't be released on us).
2008 */
2009
2010 /* locked: maps(read), amap(if !null), uobj, pg */
2011
2012 *ruobj = uobj;
2013 *ruobjpage = pg;
2014 return 0;
2015 }
2016
2017 /*
2018 * uvm_fault_lower_direct: fault lower center page
2019 *
2020 * 1. adjust flt->enter_prot.
2021 * 2. if page is loaned, resolve.
2022 */
2023
2024 int
2025 uvm_fault_lower_direct(
2026 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2027 struct uvm_object *uobj, struct vm_page *uobjpage)
2028 {
2029 struct vm_page *pg;
2030 UVMHIST_FUNC("uvm_fault_lower_direct"); UVMHIST_CALLED(maphist);
2031
2032 /*
2033 * we are not promoting. if the mapping is COW ensure that we
2034 * don't give more access than we should (e.g. when doing a read
2035 * fault on a COPYONWRITE mapping we want to map the COW page in
2036 * R/O even though the entry protection could be R/W).
2037 *
2038 * set "pg" to the page we want to map in (uobjpage, usually)
2039 */
2040
2041 cpu_count(CPU_COUNT_FLT_OBJ, 1);
2042 if (UVM_ET_ISCOPYONWRITE(ufi->entry) ||
2043 UVM_OBJ_NEEDS_WRITEFAULT(uobjpage->uobject))
2044 flt->enter_prot &= ~VM_PROT_WRITE;
2045 pg = uobjpage; /* map in the actual object */
2046
2047 KASSERT(uobjpage != PGO_DONTCARE);
2048
2049 /*
2050 * we are faulting directly on the page. be careful
2051 * about writing to loaned pages...
2052 */
2053
2054 if (uobjpage->loan_count) {
2055 uvm_fault_lower_direct_loan(ufi, flt, uobj, &pg, &uobjpage);
2056 }
2057 KASSERT(pg == uobjpage);
2058
2059 KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0);
2060 return uvm_fault_lower_enter(ufi, flt, uobj, NULL, pg);
2061 }
2062
2063 /*
2064 * uvm_fault_lower_direct_loan: resolve loaned page.
2065 *
2066 * 1. if not cow'ing, adjust flt->enter_prot.
2067 * 2. if cow'ing, break loan.
2068 */
2069
2070 static int
2071 uvm_fault_lower_direct_loan(
2072 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2073 struct uvm_object *uobj, struct vm_page **rpg,
2074 struct vm_page **ruobjpage)
2075 {
2076 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2077 struct vm_page *pg;
2078 struct vm_page *uobjpage = *ruobjpage;
2079 UVMHIST_FUNC("uvm_fault_lower_direct_loan"); UVMHIST_CALLED(maphist);
2080
2081 if (!flt->cow_now) {
2082 /* read fault: cap the protection at readonly */
2083 /* cap! */
2084 flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
2085 } else {
2086 /* write fault: must break the loan here */
2087
2088 pg = uvm_loanbreak(uobjpage);
2089 if (pg == NULL) {
2090
2091 /*
2092 * drop ownership of page, it can't be released
2093 */
2094
2095 uvm_pagelock(uobjpage);
2096 uvm_pagewakeup(uobjpage);
2097 uvm_pageunlock(uobjpage);
2098 uobjpage->flags &= ~PG_BUSY;
2099 UVM_PAGE_OWN(uobjpage, NULL);
2100
2101 uvmfault_unlockall(ufi, amap, uobj);
2102 UVMHIST_LOG(maphist,
2103 " out of RAM breaking loan, waiting",
2104 0,0,0,0);
2105 cpu_count(CPU_COUNT_FLTNORAM, 1);
2106 uvm_wait("flt_noram4");
2107 return ERESTART;
2108 }
2109 *rpg = pg;
2110 *ruobjpage = pg;
2111 }
2112 return 0;
2113 }
2114
2115 /*
2116 * uvm_fault_lower_promote: promote lower page.
2117 *
2118 * 1. call uvmfault_promote.
2119 * 2. fill in data.
2120 * 3. if not ZFOD, dispose old page.
2121 */
2122
2123 int
2124 uvm_fault_lower_promote(
2125 struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
2126 struct uvm_object *uobj, struct vm_page *uobjpage)
2127 {
2128 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2129 struct vm_anon *anon;
2130 struct vm_page *pg;
2131 int error;
2132 UVMHIST_FUNC("uvm_fault_lower_promote"); UVMHIST_CALLED(maphist);
2133
2134 KASSERT(amap != NULL);
2135
2136 /*
2137 * If we are going to promote the data to an anon we
2138 * allocate a blank anon here and plug it into our amap.
2139 */
2140 error = uvmfault_promote(ufi, NULL, uobjpage,
2141 &anon, &flt->anon_spare);
2142 switch (error) {
2143 case 0:
2144 break;
2145 case ERESTART:
2146 return ERESTART;
2147 default:
2148 return error;
2149 }
2150
2151 pg = anon->an_page;
2152
2153 /*
2154 * Fill in the data.
2155 */
2156 KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0);
2157
2158 if (uobjpage != PGO_DONTCARE) {
2159 cpu_count(CPU_COUNT_FLT_PRCOPY, 1);
2160
2161 /*
2162 * promote to shared amap? make sure all sharing
2163 * procs see it
2164 */
2165
2166 if ((amap_flags(amap) & AMAP_SHARED) != 0) {
2167 pmap_page_protect(uobjpage, VM_PROT_NONE);
2168 /*
2169 * XXX: PAGE MIGHT BE WIRED!
2170 */
2171 }
2172
2173 /*
2174 * dispose of uobjpage. it can't be PG_RELEASED
2175 * since we still hold the object lock.
2176 */
2177
2178 uobjpage->flags &= ~PG_BUSY;
2179 uvm_pagelock(uobjpage);
2180 uvm_pagewakeup(uobjpage);
2181 uvm_pageunlock(uobjpage);
2182 UVM_PAGE_OWN(uobjpage, NULL);
2183
2184 UVMHIST_LOG(maphist,
2185 " promote uobjpage %#jx to anon/page %#jx/%#jx",
2186 (uintptr_t)uobjpage, (uintptr_t)anon, (uintptr_t)pg, 0);
2187
2188 } else {
2189 cpu_count(CPU_COUNT_FLT_PRZERO, 1);
2190
2191 /*
2192 * Page is zero'd and marked dirty by
2193 * uvmfault_promote().
2194 */
2195
2196 UVMHIST_LOG(maphist," zero fill anon/page %#jx/%#jx",
2197 (uintptr_t)anon, (uintptr_t)pg, 0, 0);
2198 }
2199
2200 return uvm_fault_lower_enter(ufi, flt, uobj, anon, pg);
2201 }
2202
2203 /*
2204 * uvm_fault_lower_enter: enter h/w mapping of lower page or anon page promoted
2205 * from the lower page.
2206 */
2207
2208 int
2209 uvm_fault_lower_enter(
2210 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
2211 struct uvm_object *uobj,
2212 struct vm_anon *anon, struct vm_page *pg)
2213 {
2214 struct vm_amap * const amap = ufi->entry->aref.ar_amap;
2215 const bool readonly = uvm_pagereadonly_p(pg);
2216 int error;
2217 UVMHIST_FUNC("uvm_fault_lower_enter"); UVMHIST_CALLED(maphist);
2218
2219 /*
2220 * Locked:
2221 *
2222 * maps(read), amap(if !null), uobj(if !null),
2223 * anon(if !null), pg(if anon), unlock_uobj(if !null)
2224 *
2225 * Note: pg is either the uobjpage or the new page in the new anon.
2226 */
2227 KASSERT(amap == NULL || rw_write_held(amap->am_lock));
2228 KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
2229 KASSERT(anon == NULL || anon->an_lock == amap->am_lock);
2230 KASSERT((pg->flags & PG_BUSY) != 0);
2231
2232 /*
2233 * all resources are present. we can now map it in and free our
2234 * resources.
2235 */
2236
2237 UVMHIST_LOG(maphist,
2238 " MAPPING: case2: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
2239 (uintptr_t)ufi->orig_map->pmap, ufi->orig_rvaddr,
2240 (uintptr_t)pg, flt->promote);
2241 KASSERTMSG((flt->access_type & VM_PROT_WRITE) == 0 || !readonly,
2242 "promote=%u cow_now=%u access_type=%x enter_prot=%x cow=%u "
2243 "entry=%p map=%p orig_rvaddr=%p pg=%p",
2244 flt->promote, flt->cow_now, flt->access_type, flt->enter_prot,
2245 UVM_ET_ISCOPYONWRITE(ufi->entry), ufi->entry, ufi->orig_map,
2246 (void *)ufi->orig_rvaddr, pg);
2247 KASSERT((flt->access_type & VM_PROT_WRITE) == 0 || !readonly);
2248 if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr,
2249 VM_PAGE_TO_PHYS(pg),
2250 readonly ? flt->enter_prot & ~VM_PROT_WRITE : flt->enter_prot,
2251 flt->access_type | PMAP_CANFAIL |
2252 (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
2253
2254 /*
2255 * No need to undo what we did; we can simply think of
2256 * this as the pmap throwing away the mapping information.
2257 *
2258 * We do, however, have to go through the ReFault path,
2259 * as the map may change while we're asleep.
2260 */
2261
2262 /*
2263 * ensure that the page is queued in the case that
2264 * we just promoted the page.
2265 */
2266
2267 uvm_pagelock(pg);
2268 uvm_pageenqueue(pg);
2269 uvm_pagewakeup(pg);
2270 uvm_pageunlock(pg);
2271
2272 /*
2273 * note that pg can't be PG_RELEASED since we did not drop
2274 * the object lock since the last time we checked.
2275 */
2276 KASSERT((pg->flags & PG_RELEASED) == 0);
2277 pg->flags &= ~(PG_BUSY|PG_FAKE);
2278 UVM_PAGE_OWN(pg, NULL);
2279
2280 uvmfault_unlockall(ufi, amap, uobj);
2281 if (!uvm_reclaimable()) {
2282 UVMHIST_LOG(maphist,
2283 "<- failed. out of VM",0,0,0,0);
2284 /* XXX instrumentation */
2285 error = ENOMEM;
2286 return error;
2287 }
2288 /* XXX instrumentation */
2289 uvm_wait("flt_pmfail2");
2290 return ERESTART;
2291 }
2292
2293 uvm_fault_lower_done(ufi, flt, uobj, pg);
2294
2295 /*
2296 * note that pg can't be PG_RELEASED since we did not drop the object
2297 * lock since the last time we checked.
2298 */
2299 KASSERT((pg->flags & PG_RELEASED) == 0);
2300 uvm_pagelock(pg);
2301 uvm_pagewakeup(pg);
2302 uvm_pageunlock(pg);
2303 pg->flags &= ~(PG_BUSY|PG_FAKE);
2304 UVM_PAGE_OWN(pg, NULL);
2305
2306 pmap_update(ufi->orig_map->pmap);
2307 uvmfault_unlockall(ufi, amap, uobj);
2308
2309 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
2310 return 0;
2311 }
2312
2313 /*
2314 * uvm_fault_lower_done: queue lower center page.
2315 */
2316
2317 void
2318 uvm_fault_lower_done(
2319 struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
2320 struct uvm_object *uobj, struct vm_page *pg)
2321 {
2322 bool dropswap = false;
2323
2324 UVMHIST_FUNC("uvm_fault_lower_done"); UVMHIST_CALLED(maphist);
2325
2326 uvm_pagelock(pg);
2327 if (flt->wire_paging) {
2328 uvm_pagewire(pg);
2329 if (pg->flags & PG_AOBJ) {
2330
2331 /*
2332 * since the now-wired page cannot be paged out,
2333 * release its swap resources for others to use.
2334 * since an aobj page with no swap cannot be clean,
2335 * mark it dirty now.
2336 */
2337
2338 KASSERT(uobj != NULL);
2339 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
2340 dropswap = true;
2341 }
2342 } else {
2343 uvm_pageactivate(pg);
2344 }
2345 uvm_pageunlock(pg);
2346
2347 if (dropswap) {
2348 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
2349 }
2350 }
2351
2352
2353 /*
2354 * uvm_fault_wire: wire down a range of virtual addresses in a map.
2355 *
2356 * => map may be read-locked by caller, but MUST NOT be write-locked.
2357 * => if map is read-locked, any operations which may cause map to
2358 * be write-locked in uvm_fault() must be taken care of by
2359 * the caller. See uvm_map_pageable().
2360 */
2361
2362 int
2363 uvm_fault_wire(struct vm_map *map, vaddr_t start, vaddr_t end,
2364 vm_prot_t access_type, int maxprot)
2365 {
2366 vaddr_t va;
2367 int error;
2368
2369 /*
2370 * now fault it in a page at a time. if the fault fails then we have
2371 * to undo what we have done. note that in uvm_fault VM_PROT_NONE
2372 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
2373 */
2374
2375 /*
2376 * XXX work around overflowing a vaddr_t. this prevents us from
2377 * wiring the last page in the address space, though.
2378 */
2379 if (start > end) {
2380 return EFAULT;
2381 }
2382
2383 for (va = start; va < end; va += PAGE_SIZE) {
2384 error = uvm_fault_internal(map, va, access_type,
2385 (maxprot ? UVM_FAULT_MAXPROT : 0) | UVM_FAULT_WIRE);
2386 if (error) {
2387 if (va != start) {
2388 uvm_fault_unwire(map, start, va);
2389 }
2390 return error;
2391 }
2392 }
2393 return 0;
2394 }
2395
2396 /*
2397 * uvm_fault_unwire(): unwire range of virtual space.
2398 */
2399
2400 void
2401 uvm_fault_unwire(struct vm_map *map, vaddr_t start, vaddr_t end)
2402 {
2403 vm_map_lock_read(map);
2404 uvm_fault_unwire_locked(map, start, end);
2405 vm_map_unlock_read(map);
2406 }
2407
2408 /*
2409 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
2410 *
2411 * => map must be at least read-locked.
2412 */
2413
2414 void
2415 uvm_fault_unwire_locked(struct vm_map *map, vaddr_t start, vaddr_t end)
2416 {
2417 struct vm_map_entry *entry, *oentry;
2418 pmap_t pmap = vm_map_pmap(map);
2419 vaddr_t va;
2420 paddr_t pa;
2421 struct vm_page *pg;
2422
2423 /*
2424 * we assume that the area we are unwiring has actually been wired
2425 * in the first place. this means that we should be able to extract
2426 * the PAs from the pmap. we also lock out the page daemon so that
2427 * we can call uvm_pageunwire.
2428 */
2429
2430 /*
2431 * find the beginning map entry for the region.
2432 */
2433
2434 KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map));
2435 if (uvm_map_lookup_entry(map, start, &entry) == false)
2436 panic("uvm_fault_unwire_locked: address not in map");
2437
2438 oentry = NULL;
2439 for (va = start; va < end; va += PAGE_SIZE) {
2440
2441 /*
2442 * find the map entry for the current address.
2443 */
2444
2445 KASSERT(va >= entry->start);
2446 while (va >= entry->end) {
2447 KASSERT(entry->next != &map->header &&
2448 entry->next->start <= entry->end);
2449 entry = entry->next;
2450 }
2451
2452 /*
2453 * lock it.
2454 */
2455
2456 if (entry != oentry) {
2457 if (oentry != NULL) {
2458 uvm_map_unlock_entry(oentry);
2459 }
2460 uvm_map_lock_entry(entry, RW_WRITER);
2461 oentry = entry;
2462 }
2463
2464 /*
2465 * if the entry is no longer wired, tell the pmap.
2466 */
2467
2468 if (!pmap_extract(pmap, va, &pa))
2469 continue;
2470
2471 if (VM_MAPENT_ISWIRED(entry) == 0)
2472 pmap_unwire(pmap, va);
2473
2474 pg = PHYS_TO_VM_PAGE(pa);
2475 if (pg) {
2476 uvm_pagelock(pg);
2477 uvm_pageunwire(pg);
2478 uvm_pageunlock(pg);
2479 }
2480 }
2481
2482 if (oentry != NULL) {
2483 uvm_map_unlock_entry(entry);
2484 }
2485 }
2486