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