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