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