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