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