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