uvm_fault.c revision 1.53 1 /* $NetBSD: uvm_fault.c,v 1.53 2001/01/23 01:56:16 thorpej Exp $ */
2
3 /*
4 *
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp
35 */
36
37 #include "opt_uvmhist.h"
38
39 /*
40 * uvm_fault.c: fault handler
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/malloc.h>
48 #include <sys/mman.h>
49 #include <sys/user.h>
50
51 #include <uvm/uvm.h>
52
53 /*
54 *
55 * a word on page faults:
56 *
57 * types of page faults we handle:
58 *
59 * CASE 1: upper layer faults CASE 2: lower layer faults
60 *
61 * CASE 1A CASE 1B CASE 2A CASE 2B
62 * read/write1 write>1 read/write +-cow_write/zero
63 * | | | |
64 * +--|--+ +--|--+ +-----+ + | + | +-----+
65 * amap | V | | ----------->new| | | | ^ |
66 * +-----+ +-----+ +-----+ + | + | +--|--+
67 * | | |
68 * +-----+ +-----+ +--|--+ | +--|--+
69 * uobj | d/c | | d/c | | V | +----| |
70 * +-----+ +-----+ +-----+ +-----+
71 *
72 * d/c = don't care
73 *
74 * case [0]: layerless fault
75 * no amap or uobj is present. this is an error.
76 *
77 * case [1]: upper layer fault [anon active]
78 * 1A: [read] or [write with anon->an_ref == 1]
79 * I/O takes place in top level anon and uobj is not touched.
80 * 1B: [write with anon->an_ref > 1]
81 * new anon is alloc'd and data is copied off ["COW"]
82 *
83 * case [2]: lower layer fault [uobj]
84 * 2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
85 * I/O takes place directly in object.
86 * 2B: [write to copy_on_write] or [read on NULL uobj]
87 * data is "promoted" from uobj to a new anon.
88 * if uobj is null, then we zero fill.
89 *
90 * we follow the standard UVM locking protocol ordering:
91 *
92 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
93 * we hold a PG_BUSY page if we unlock for I/O
94 *
95 *
96 * the code is structured as follows:
97 *
98 * - init the "IN" params in the ufi structure
99 * ReFault:
100 * - do lookups [locks maps], check protection, handle needs_copy
101 * - check for case 0 fault (error)
102 * - establish "range" of fault
103 * - if we have an amap lock it and extract the anons
104 * - if sequential advice deactivate pages behind us
105 * - at the same time check pmap for unmapped areas and anon for pages
106 * that we could map in (and do map it if found)
107 * - check object for resident pages that we could map in
108 * - if (case 2) goto Case2
109 * - >>> handle case 1
110 * - ensure source anon is resident in RAM
111 * - if case 1B alloc new anon and copy from source
112 * - map the correct page in
113 * Case2:
114 * - >>> handle case 2
115 * - ensure source page is resident (if uobj)
116 * - if case 2B alloc new anon and copy from source (could be zero
117 * fill if uobj == NULL)
118 * - map the correct page in
119 * - done!
120 *
121 * note on paging:
122 * if we have to do I/O we place a PG_BUSY page in the correct object,
123 * unlock everything, and do the I/O. when I/O is done we must reverify
124 * the state of the world before assuming that our data structures are
125 * valid. [because mappings could change while the map is unlocked]
126 *
127 * alternative 1: unbusy the page in question and restart the page fault
128 * from the top (ReFault). this is easy but does not take advantage
129 * of the information that we already have from our previous lookup,
130 * although it is possible that the "hints" in the vm_map will help here.
131 *
132 * alternative 2: the system already keeps track of a "version" number of
133 * a map. [i.e. every time you write-lock a map (e.g. to change a
134 * mapping) you bump the version number up by one...] so, we can save
135 * the version number of the map before we release the lock and start I/O.
136 * then when I/O is done we can relock and check the version numbers
137 * to see if anything changed. this might save us some over 1 because
138 * we don't have to unbusy the page and may be less compares(?).
139 *
140 * alternative 3: put in backpointers or a way to "hold" part of a map
141 * in place while I/O is in progress. this could be complex to
142 * implement (especially with structures like amap that can be referenced
143 * by multiple map entries, and figuring out what should wait could be
144 * complex as well...).
145 *
146 * given that we are not currently multiprocessor or multithreaded we might
147 * as well choose alternative 2 now. maybe alternative 3 would be useful
148 * in the future. XXX keep in mind for future consideration//rechecking.
149 */
150
151 /*
152 * local data structures
153 */
154
155 struct uvm_advice {
156 int advice;
157 int nback;
158 int nforw;
159 };
160
161 /*
162 * page range array:
163 * note: index in array must match "advice" value
164 * XXX: borrowed numbers from freebsd. do they work well for us?
165 */
166
167 static struct uvm_advice uvmadvice[] = {
168 { MADV_NORMAL, 3, 4 },
169 { MADV_RANDOM, 0, 0 },
170 { MADV_SEQUENTIAL, 8, 7},
171 };
172
173 #define UVM_MAXRANGE 16 /* must be max() of nback+nforw+1 */
174
175 /*
176 * private prototypes
177 */
178
179 static void uvmfault_amapcopy __P((struct uvm_faultinfo *));
180 static __inline void uvmfault_anonflush __P((struct vm_anon **, int));
181
182 /*
183 * inline functions
184 */
185
186 /*
187 * uvmfault_anonflush: try and deactivate pages in specified anons
188 *
189 * => does not have to deactivate page if it is busy
190 */
191
192 static __inline void
193 uvmfault_anonflush(anons, n)
194 struct vm_anon **anons;
195 int n;
196 {
197 int lcv;
198 struct vm_page *pg;
199
200 for (lcv = 0 ; lcv < n ; lcv++) {
201 if (anons[lcv] == NULL)
202 continue;
203 simple_lock(&anons[lcv]->an_lock);
204 pg = anons[lcv]->u.an_page;
205 if (pg && (pg->flags & PG_BUSY) == 0 && pg->loan_count == 0) {
206 uvm_lock_pageq();
207 if (pg->wire_count == 0) {
208 pmap_page_protect(pg, VM_PROT_NONE);
209 uvm_pagedeactivate(pg);
210 }
211 uvm_unlock_pageq();
212 }
213 simple_unlock(&anons[lcv]->an_lock);
214 }
215 }
216
217 /*
218 * normal functions
219 */
220
221 /*
222 * uvmfault_amapcopy: clear "needs_copy" in a map.
223 *
224 * => called with VM data structures unlocked (usually, see below)
225 * => we get a write lock on the maps and clear needs_copy for a VA
226 * => if we are out of RAM we sleep (waiting for more)
227 */
228
229 static void
230 uvmfault_amapcopy(ufi)
231 struct uvm_faultinfo *ufi;
232 {
233
234 /*
235 * while we haven't done the job
236 */
237
238 while (1) {
239
240 /*
241 * no mapping? give up.
242 */
243
244 if (uvmfault_lookup(ufi, TRUE) == FALSE)
245 return;
246
247 /*
248 * copy if needed.
249 */
250
251 if (UVM_ET_ISNEEDSCOPY(ufi->entry))
252 amap_copy(ufi->map, ufi->entry, M_NOWAIT, TRUE,
253 ufi->orig_rvaddr, ufi->orig_rvaddr + 1);
254
255 /*
256 * didn't work? must be out of RAM. unlock and sleep.
257 */
258
259 if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
260 uvmfault_unlockmaps(ufi, TRUE);
261 uvm_wait("fltamapcopy");
262 continue;
263 }
264
265 /*
266 * got it! unlock and return.
267 */
268
269 uvmfault_unlockmaps(ufi, TRUE);
270 return;
271 }
272 /*NOTREACHED*/
273 }
274
275 /*
276 * uvmfault_anonget: get data in an anon into a non-busy, non-released
277 * page in that anon.
278 *
279 * => maps, amap, and anon locked by caller.
280 * => if we fail (result != VM_PAGER_OK) we unlock everything.
281 * => if we are successful, we return with everything still locked.
282 * => we don't move the page on the queues [gets moved later]
283 * => if we allocate a new page [we_own], it gets put on the queues.
284 * either way, the result is that the page is on the queues at return time
285 * => for pages which are on loan from a uvm_object (and thus are not
286 * owned by the anon): if successful, we return with the owning object
287 * locked. the caller must unlock this object when it unlocks everything
288 * else.
289 */
290
291 int
292 uvmfault_anonget(ufi, amap, anon)
293 struct uvm_faultinfo *ufi;
294 struct vm_amap *amap;
295 struct vm_anon *anon;
296 {
297 boolean_t we_own; /* we own anon's page? */
298 boolean_t locked; /* did we relock? */
299 struct vm_page *pg;
300 int result;
301 UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist);
302
303 LOCK_ASSERT(simple_lock_held(&anon->an_lock));
304
305 result = 0; /* XXX shut up gcc */
306 uvmexp.fltanget++;
307 /* bump rusage counters */
308 if (anon->u.an_page)
309 curproc->p_addr->u_stats.p_ru.ru_minflt++;
310 else
311 curproc->p_addr->u_stats.p_ru.ru_majflt++;
312
313 /*
314 * loop until we get it, or fail.
315 */
316
317 while (1) {
318
319 we_own = FALSE; /* TRUE if we set PG_BUSY on a page */
320 pg = anon->u.an_page;
321
322 /*
323 * if there is a resident page and it is loaned, then anon
324 * may not own it. call out to uvm_anon_lockpage() to ensure
325 * the real owner of the page has been identified and locked.
326 */
327
328 if (pg && pg->loan_count)
329 pg = uvm_anon_lockloanpg(anon);
330
331 /*
332 * page there? make sure it is not busy/released.
333 */
334
335 if (pg) {
336
337 /*
338 * at this point, if the page has a uobject [meaning
339 * we have it on loan], then that uobject is locked
340 * by us! if the page is busy, we drop all the
341 * locks (including uobject) and try again.
342 */
343
344 if ((pg->flags & (PG_BUSY|PG_RELEASED)) == 0) {
345 UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
346 return (VM_PAGER_OK);
347 }
348 pg->flags |= PG_WANTED;
349 uvmexp.fltpgwait++;
350
351 /*
352 * the last unlock must be an atomic unlock+wait on
353 * the owner of page
354 */
355 if (pg->uobject) { /* owner is uobject ? */
356 uvmfault_unlockall(ufi, amap, NULL, anon);
357 UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
358 0,0,0);
359 UVM_UNLOCK_AND_WAIT(pg,
360 &pg->uobject->vmobjlock,
361 FALSE, "anonget1",0);
362 } else {
363 /* anon owns page */
364 uvmfault_unlockall(ufi, amap, NULL, NULL);
365 UVMHIST_LOG(maphist, " unlock+wait on anon",0,
366 0,0,0);
367 UVM_UNLOCK_AND_WAIT(pg,&anon->an_lock,0,
368 "anonget2",0);
369 }
370 /* ready to relock and try again */
371
372 } else {
373
374 /*
375 * no page, we must try and bring it in.
376 */
377 pg = uvm_pagealloc(NULL, 0, anon, 0);
378
379 if (pg == NULL) { /* out of RAM. */
380
381 uvmfault_unlockall(ufi, amap, NULL, anon);
382 uvmexp.fltnoram++;
383 UVMHIST_LOG(maphist, " noram -- UVM_WAIT",0,
384 0,0,0);
385 uvm_wait("flt_noram1");
386 /* ready to relock and try again */
387
388 } else {
389
390 /* we set the PG_BUSY bit */
391 we_own = TRUE;
392 uvmfault_unlockall(ufi, amap, NULL, anon);
393
394 /*
395 * we are passing a PG_BUSY+PG_FAKE+PG_CLEAN
396 * page into the uvm_swap_get function with
397 * all data structures unlocked. note that
398 * it is ok to read an_swslot here because
399 * we hold PG_BUSY on the page.
400 */
401 uvmexp.pageins++;
402 result = uvm_swap_get(pg, anon->an_swslot,
403 PGO_SYNCIO);
404
405 /*
406 * we clean up after the i/o below in the
407 * "we_own" case
408 */
409 /* ready to relock and try again */
410 }
411 }
412
413 /*
414 * now relock and try again
415 */
416
417 locked = uvmfault_relock(ufi);
418 if (locked && amap != NULL) {
419 amap_lock(amap);
420 }
421 if (locked || we_own)
422 simple_lock(&anon->an_lock);
423
424 /*
425 * if we own the page (i.e. we set PG_BUSY), then we need
426 * to clean up after the I/O. there are three cases to
427 * consider:
428 * [1] page released during I/O: free anon and ReFault.
429 * [2] I/O not OK. free the page and cause the fault
430 * to fail.
431 * [3] I/O OK! activate the page and sync with the
432 * non-we_own case (i.e. drop anon lock if not locked).
433 */
434
435 if (we_own) {
436
437 if (pg->flags & PG_WANTED) {
438 /* still holding object lock */
439 wakeup(pg);
440 }
441 /* un-busy! */
442 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
443 UVM_PAGE_OWN(pg, NULL);
444
445 /*
446 * if we were RELEASED during I/O, then our anon is
447 * no longer part of an amap. we need to free the
448 * anon and try again.
449 */
450 if (pg->flags & PG_RELEASED) {
451 pmap_page_protect(pg, VM_PROT_NONE);
452 simple_unlock(&anon->an_lock);
453 uvm_anfree(anon); /* frees page for us */
454 if (locked)
455 uvmfault_unlockall(ufi, amap, NULL,
456 NULL);
457 uvmexp.fltpgrele++;
458 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
459 return (VM_PAGER_REFAULT); /* refault! */
460 }
461
462 if (result != VM_PAGER_OK) {
463 KASSERT(result != VM_PAGER_PEND);
464
465 /* remove page from anon */
466 anon->u.an_page = NULL;
467
468 /*
469 * remove the swap slot from the anon
470 * and mark the anon as having no real slot.
471 * don't free the swap slot, thus preventing
472 * it from being used again.
473 */
474 uvm_swap_markbad(anon->an_swslot, 1);
475 anon->an_swslot = SWSLOT_BAD;
476
477 /*
478 * note: page was never !PG_BUSY, so it
479 * can't be mapped and thus no need to
480 * pmap_page_protect it...
481 */
482 uvm_lock_pageq();
483 uvm_pagefree(pg);
484 uvm_unlock_pageq();
485
486 if (locked)
487 uvmfault_unlockall(ufi, amap, NULL,
488 anon);
489 else
490 simple_unlock(&anon->an_lock);
491 UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
492 return (VM_PAGER_ERROR);
493 }
494
495 /*
496 * must be OK, clear modify (already PG_CLEAN)
497 * and activate
498 */
499 pmap_clear_modify(pg);
500 uvm_lock_pageq();
501 uvm_pageactivate(pg);
502 uvm_unlock_pageq();
503 if (!locked)
504 simple_unlock(&anon->an_lock);
505 }
506
507 /*
508 * we were not able to relock. restart fault.
509 */
510
511 if (!locked) {
512 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
513 return (VM_PAGER_REFAULT);
514 }
515
516 /*
517 * verify no one has touched the amap and moved the anon on us.
518 */
519
520 if (ufi != NULL &&
521 amap_lookup(&ufi->entry->aref,
522 ufi->orig_rvaddr - ufi->entry->start) != anon) {
523
524 uvmfault_unlockall(ufi, amap, NULL, anon);
525 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
526 return (VM_PAGER_REFAULT);
527 }
528
529 /*
530 * try it again!
531 */
532
533 uvmexp.fltanretry++;
534 continue;
535
536 } /* while (1) */
537
538 /*NOTREACHED*/
539 }
540
541 /*
542 * F A U L T - m a i n e n t r y p o i n t
543 */
544
545 /*
546 * uvm_fault: page fault handler
547 *
548 * => called from MD code to resolve a page fault
549 * => VM data structures usually should be unlocked. however, it is
550 * possible to call here with the main map locked if the caller
551 * gets a write lock, sets it recusive, and then calls us (c.f.
552 * uvm_map_pageable). this should be avoided because it keeps
553 * the map locked off during I/O.
554 */
555
556 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
557 ~VM_PROT_WRITE : VM_PROT_ALL)
558
559 int
560 uvm_fault(orig_map, vaddr, fault_type, access_type)
561 vm_map_t orig_map;
562 vaddr_t vaddr;
563 vm_fault_t fault_type;
564 vm_prot_t access_type;
565 {
566 struct uvm_faultinfo ufi;
567 vm_prot_t enter_prot;
568 boolean_t wired, narrow, promote, locked, shadowed;
569 int npages, nback, nforw, centeridx, result, lcv, gotpages;
570 vaddr_t startva, objaddr, currva, offset, uoff;
571 paddr_t pa;
572 struct vm_amap *amap;
573 struct uvm_object *uobj;
574 struct vm_anon *anons_store[UVM_MAXRANGE], **anons, *anon, *oanon;
575 struct vm_page *pages[UVM_MAXRANGE], *pg, *uobjpage;
576 UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist);
577
578 UVMHIST_LOG(maphist, "(map=0x%x, vaddr=0x%x, ft=%d, at=%d)",
579 orig_map, vaddr, fault_type, access_type);
580
581 anon = NULL;
582 pg = NULL;
583
584 uvmexp.faults++; /* XXX: locking? */
585
586 /*
587 * init the IN parameters in the ufi
588 */
589
590 ufi.orig_map = orig_map;
591 ufi.orig_rvaddr = trunc_page(vaddr);
592 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */
593 if (fault_type == VM_FAULT_WIRE)
594 narrow = TRUE; /* don't look for neighborhood
595 * pages on wire */
596 else
597 narrow = FALSE; /* normal fault */
598
599 /*
600 * before we do anything else, if this is a fault on a kernel
601 * address, check to see if the address is managed by an
602 * interrupt-safe map. If it is, we fail immediately. Intrsafe
603 * maps are never pageable, and this approach avoids an evil
604 * locking mess.
605 */
606 if (orig_map == kernel_map && uvmfault_check_intrsafe(&ufi)) {
607 UVMHIST_LOG(maphist, "<- VA 0x%lx in intrsafe map %p",
608 ufi.orig_rvaddr, ufi.map, 0, 0);
609 return (KERN_FAILURE);
610 }
611
612 /*
613 * "goto ReFault" means restart the page fault from ground zero.
614 */
615 ReFault:
616
617 /*
618 * lookup and lock the maps
619 */
620
621 if (uvmfault_lookup(&ufi, FALSE) == FALSE) {
622 UVMHIST_LOG(maphist, "<- no mapping @ 0x%x", vaddr, 0,0,0);
623 return (KERN_INVALID_ADDRESS);
624 }
625 /* locked: maps(read) */
626
627 /*
628 * check protection
629 */
630
631 if ((ufi.entry->protection & access_type) != access_type) {
632 UVMHIST_LOG(maphist,
633 "<- protection failure (prot=0x%x, access=0x%x)",
634 ufi.entry->protection, access_type, 0, 0);
635 uvmfault_unlockmaps(&ufi, FALSE);
636 return (KERN_PROTECTION_FAILURE);
637 }
638
639 /*
640 * if the map is not a pageable map, a page fault always fails.
641 */
642
643 if ((ufi.map->flags & VM_MAP_PAGEABLE) == 0) {
644 UVMHIST_LOG(maphist,
645 "<- map %p not pageable", ufi.map, 0, 0, 0);
646 uvmfault_unlockmaps(&ufi, FALSE);
647 return (KERN_FAILURE);
648 }
649
650 /*
651 * "enter_prot" is the protection we want to enter the page in at.
652 * for certain pages (e.g. copy-on-write pages) this protection can
653 * be more strict than ufi.entry->protection. "wired" means either
654 * the entry is wired or we are fault-wiring the pg.
655 */
656
657 enter_prot = ufi.entry->protection;
658 wired = VM_MAPENT_ISWIRED(ufi.entry) || (fault_type == VM_FAULT_WIRE);
659 if (wired)
660 access_type = enter_prot; /* full access for wired */
661
662 /*
663 * handle "needs_copy" case. if we need to copy the amap we will
664 * have to drop our readlock and relock it with a write lock. (we
665 * need a write lock to change anything in a map entry [e.g.
666 * needs_copy]).
667 */
668
669 if (UVM_ET_ISNEEDSCOPY(ufi.entry)) {
670 if ((access_type & VM_PROT_WRITE) ||
671 (ufi.entry->object.uvm_obj == NULL)) {
672 /* need to clear */
673 UVMHIST_LOG(maphist,
674 " need to clear needs_copy and refault",0,0,0,0);
675 uvmfault_unlockmaps(&ufi, FALSE);
676 uvmfault_amapcopy(&ufi);
677 uvmexp.fltamcopy++;
678 goto ReFault;
679
680 } else {
681
682 /*
683 * ensure that we pmap_enter page R/O since
684 * needs_copy is still true
685 */
686 enter_prot &= ~VM_PROT_WRITE;
687
688 }
689 }
690
691 /*
692 * identify the players
693 */
694
695 amap = ufi.entry->aref.ar_amap; /* top layer */
696 uobj = ufi.entry->object.uvm_obj; /* bottom layer */
697
698 /*
699 * check for a case 0 fault. if nothing backing the entry then
700 * error now.
701 */
702
703 if (amap == NULL && uobj == NULL) {
704 uvmfault_unlockmaps(&ufi, FALSE);
705 UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
706 return (KERN_INVALID_ADDRESS);
707 }
708
709 /*
710 * establish range of interest based on advice from mapper
711 * and then clip to fit map entry. note that we only want
712 * to do this the first time through the fault. if we
713 * ReFault we will disable this by setting "narrow" to true.
714 */
715
716 if (narrow == FALSE) {
717
718 /* wide fault (!narrow) */
719 KASSERT(uvmadvice[ufi.entry->advice].advice ==
720 ufi.entry->advice);
721 nback = min(uvmadvice[ufi.entry->advice].nback,
722 (ufi.orig_rvaddr - ufi.entry->start) >> PAGE_SHIFT);
723 startva = ufi.orig_rvaddr - (nback << PAGE_SHIFT);
724 nforw = min(uvmadvice[ufi.entry->advice].nforw,
725 ((ufi.entry->end - ufi.orig_rvaddr) >>
726 PAGE_SHIFT) - 1);
727 /*
728 * note: "-1" because we don't want to count the
729 * faulting page as forw
730 */
731 npages = nback + nforw + 1;
732 centeridx = nback;
733
734 narrow = TRUE; /* ensure only once per-fault */
735
736 } else {
737
738 /* narrow fault! */
739 nback = nforw = 0;
740 startva = ufi.orig_rvaddr;
741 npages = 1;
742 centeridx = 0;
743
744 }
745
746 /* locked: maps(read) */
747 UVMHIST_LOG(maphist, " narrow=%d, back=%d, forw=%d, startva=0x%x",
748 narrow, nback, nforw, startva);
749 UVMHIST_LOG(maphist, " entry=0x%x, amap=0x%x, obj=0x%x", ufi.entry,
750 amap, uobj, 0);
751
752 /*
753 * if we've got an amap, lock it and extract current anons.
754 */
755
756 if (amap) {
757 amap_lock(amap);
758 anons = anons_store;
759 amap_lookups(&ufi.entry->aref, startva - ufi.entry->start,
760 anons, npages);
761 } else {
762 anons = NULL; /* to be safe */
763 }
764
765 /* locked: maps(read), amap(if there) */
766
767 /*
768 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
769 * now and then forget about them (for the rest of the fault).
770 */
771
772 if (ufi.entry->advice == MADV_SEQUENTIAL) {
773
774 UVMHIST_LOG(maphist, " MADV_SEQUENTIAL: flushing backpages",
775 0,0,0,0);
776 /* flush back-page anons? */
777 if (amap)
778 uvmfault_anonflush(anons, nback);
779
780 /* flush object? */
781 if (uobj) {
782 objaddr =
783 (startva - ufi.entry->start) + ufi.entry->offset;
784 simple_lock(&uobj->vmobjlock);
785 (void) uobj->pgops->pgo_flush(uobj, objaddr, objaddr +
786 (nback << PAGE_SHIFT), PGO_DEACTIVATE);
787 simple_unlock(&uobj->vmobjlock);
788 }
789
790 /* now forget about the backpages */
791 if (amap)
792 anons += nback;
793 startva += (nback << PAGE_SHIFT);
794 npages -= nback;
795 nback = centeridx = 0;
796 }
797
798 /* locked: maps(read), amap(if there) */
799
800 /*
801 * map in the backpages and frontpages we found in the amap in hopes
802 * of preventing future faults. we also init the pages[] array as
803 * we go.
804 */
805
806 currva = startva;
807 shadowed = FALSE;
808 for (lcv = 0 ; lcv < npages ; lcv++, currva += PAGE_SIZE) {
809
810 /*
811 * dont play with VAs that are already mapped
812 * except for center)
813 */
814 if (lcv != centeridx &&
815 pmap_extract(ufi.orig_map->pmap, currva, &pa)) {
816 pages[lcv] = PGO_DONTCARE;
817 continue;
818 }
819
820 /*
821 * unmapped or center page. check if any anon at this level.
822 */
823 if (amap == NULL || anons[lcv] == NULL) {
824 pages[lcv] = NULL;
825 continue;
826 }
827
828 /*
829 * check for present page and map if possible. re-activate it.
830 */
831
832 pages[lcv] = PGO_DONTCARE;
833 if (lcv == centeridx) { /* save center for later! */
834 shadowed = TRUE;
835 continue;
836 }
837 anon = anons[lcv];
838 simple_lock(&anon->an_lock);
839 /* ignore loaned pages */
840 if (anon->u.an_page && anon->u.an_page->loan_count == 0 &&
841 (anon->u.an_page->flags & (PG_RELEASED|PG_BUSY)) == 0) {
842 uvm_lock_pageq();
843 uvm_pageactivate(anon->u.an_page); /* reactivate */
844 uvm_unlock_pageq();
845 UVMHIST_LOG(maphist,
846 " MAPPING: n anon: pm=0x%x, va=0x%x, pg=0x%x",
847 ufi.orig_map->pmap, currva, anon->u.an_page, 0);
848 uvmexp.fltnamap++;
849
850 /*
851 * Since this isn't the page that's actually faulting,
852 * ignore pmap_enter() failures; it's not critical
853 * that we enter these right now.
854 */
855
856 (void) pmap_enter(ufi.orig_map->pmap, currva,
857 VM_PAGE_TO_PHYS(anon->u.an_page),
858 (anon->an_ref > 1) ? (enter_prot & ~VM_PROT_WRITE) :
859 enter_prot,
860 PMAP_CANFAIL |
861 (VM_MAPENT_ISWIRED(ufi.entry) ? PMAP_WIRED : 0));
862 }
863 simple_unlock(&anon->an_lock);
864 }
865
866 /* locked: maps(read), amap(if there) */
867 /* (shadowed == TRUE) if there is an anon at the faulting address */
868 UVMHIST_LOG(maphist, " shadowed=%d, will_get=%d", shadowed,
869 (uobj && shadowed == FALSE),0,0);
870
871 /*
872 * note that if we are really short of RAM we could sleep in the above
873 * call to pmap_enter with everything locked. bad?
874 *
875 * XXX Actually, that is bad; pmap_enter() should just fail in that
876 * XXX case. --thorpej
877 */
878
879 /*
880 * if the desired page is not shadowed by the amap and we have a
881 * backing object, then we check to see if the backing object would
882 * prefer to handle the fault itself (rather than letting us do it
883 * with the usual pgo_get hook). the backing object signals this by
884 * providing a pgo_fault routine.
885 */
886
887 if (uobj && shadowed == FALSE && uobj->pgops->pgo_fault != NULL) {
888 simple_lock(&uobj->vmobjlock);
889
890 /* locked: maps(read), amap (if there), uobj */
891 result = uobj->pgops->pgo_fault(&ufi, startva, pages, npages,
892 centeridx, fault_type, access_type,
893 PGO_LOCKED|PGO_SYNCIO);
894
895 /* locked: nothing, pgo_fault has unlocked everything */
896
897 if (result == VM_PAGER_OK)
898 return (KERN_SUCCESS); /* pgo_fault did pmap enter */
899 else if (result == VM_PAGER_REFAULT)
900 goto ReFault; /* try again! */
901 else
902 return (KERN_PROTECTION_FAILURE);
903 }
904
905 /*
906 * now, if the desired page is not shadowed by the amap and we have
907 * a backing object that does not have a special fault routine, then
908 * we ask (with pgo_get) the object for resident pages that we care
909 * about and attempt to map them in. we do not let pgo_get block
910 * (PGO_LOCKED).
911 *
912 * ("get" has the option of doing a pmap_enter for us)
913 */
914
915 if (uobj && shadowed == FALSE) {
916 simple_lock(&uobj->vmobjlock);
917
918 /* locked (!shadowed): maps(read), amap (if there), uobj */
919 /*
920 * the following call to pgo_get does _not_ change locking state
921 */
922
923 uvmexp.fltlget++;
924 gotpages = npages;
925 (void) uobj->pgops->pgo_get(uobj, ufi.entry->offset +
926 (startva - ufi.entry->start),
927 pages, &gotpages, centeridx,
928 access_type & MASK(ufi.entry),
929 ufi.entry->advice, PGO_LOCKED);
930
931 /*
932 * check for pages to map, if we got any
933 */
934
935 uobjpage = NULL;
936
937 if (gotpages) {
938 currva = startva;
939 for (lcv = 0 ; lcv < npages ;
940 lcv++, currva += PAGE_SIZE) {
941
942 if (pages[lcv] == NULL ||
943 pages[lcv] == PGO_DONTCARE)
944 continue;
945
946 KASSERT((pages[lcv]->flags & PG_RELEASED) == 0);
947
948 /*
949 * if center page is resident and not
950 * PG_BUSY|PG_RELEASED then pgo_get
951 * made it PG_BUSY for us and gave
952 * us a handle to it. remember this
953 * page as "uobjpage." (for later use).
954 */
955
956 if (lcv == centeridx) {
957 uobjpage = pages[lcv];
958 UVMHIST_LOG(maphist, " got uobjpage "
959 "(0x%x) with locked get",
960 uobjpage, 0,0,0);
961 continue;
962 }
963
964 /*
965 * note: calling pgo_get with locked data
966 * structures returns us pages which are
967 * neither busy nor released, so we don't
968 * need to check for this. we can just
969 * directly enter the page (after moving it
970 * to the head of the active queue [useful?]).
971 */
972
973 uvm_lock_pageq();
974 uvm_pageactivate(pages[lcv]); /* reactivate */
975 uvm_unlock_pageq();
976 UVMHIST_LOG(maphist,
977 " MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x",
978 ufi.orig_map->pmap, currva, pages[lcv], 0);
979 uvmexp.fltnomap++;
980
981 /*
982 * Since this page isn't the page that's
983 * actually fauling, ignore pmap_enter()
984 * failures; it's not critical that we
985 * enter these right now.
986 */
987
988 (void) pmap_enter(ufi.orig_map->pmap, currva,
989 VM_PAGE_TO_PHYS(pages[lcv]),
990 pages[lcv]->flags & PG_RDONLY ?
991 VM_PROT_READ : enter_prot & MASK(ufi.entry),
992 PMAP_CANFAIL |
993 (wired ? PMAP_WIRED : 0));
994
995 /*
996 * NOTE: page can't be PG_WANTED or PG_RELEASED
997 * because we've held the lock the whole time
998 * we've had the handle.
999 */
1000
1001 pages[lcv]->flags &= ~(PG_BUSY); /* un-busy! */
1002 UVM_PAGE_OWN(pages[lcv], NULL);
1003 } /* for "lcv" loop */
1004 } /* "gotpages" != 0 */
1005 /* note: object still _locked_ */
1006 } else {
1007 uobjpage = NULL;
1008 }
1009
1010 /* locked (shadowed): maps(read), amap */
1011 /* locked (!shadowed): maps(read), amap(if there),
1012 uobj(if !null), uobjpage(if !null) */
1013
1014 /*
1015 * note that at this point we are done with any front or back pages.
1016 * we are now going to focus on the center page (i.e. the one we've
1017 * faulted on). if we have faulted on the top (anon) layer
1018 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
1019 * not touched it yet). if we have faulted on the bottom (uobj)
1020 * layer [i.e. case 2] and the page was both present and available,
1021 * then we've got a pointer to it as "uobjpage" and we've already
1022 * made it BUSY.
1023 */
1024
1025 /*
1026 * there are four possible cases we must address: 1A, 1B, 2A, and 2B
1027 */
1028
1029 /*
1030 * redirect case 2: if we are not shadowed, go to case 2.
1031 */
1032
1033 if (shadowed == FALSE)
1034 goto Case2;
1035
1036 /* locked: maps(read), amap */
1037
1038 /*
1039 * handle case 1: fault on an anon in our amap
1040 */
1041
1042 anon = anons[centeridx];
1043 UVMHIST_LOG(maphist, " case 1 fault: anon=0x%x", anon, 0,0,0);
1044 simple_lock(&anon->an_lock);
1045
1046 /* locked: maps(read), amap, anon */
1047
1048 /*
1049 * no matter if we have case 1A or case 1B we are going to need to
1050 * have the anon's memory resident. ensure that now.
1051 */
1052
1053 /*
1054 * let uvmfault_anonget do the dirty work.
1055 * if it fails (!OK) it will unlock everything for us.
1056 * if it succeeds, locks are still valid and locked.
1057 * also, if it is OK, then the anon's page is on the queues.
1058 * if the page is on loan from a uvm_object, then anonget will
1059 * lock that object for us if it does not fail.
1060 */
1061
1062 result = uvmfault_anonget(&ufi, amap, anon);
1063 switch (result) {
1064 case VM_PAGER_OK:
1065 break;
1066
1067 case VM_PAGER_REFAULT:
1068 goto ReFault;
1069
1070 case VM_PAGER_AGAIN:
1071 tsleep(&lbolt, PVM, "fltagain1", 0);
1072 goto ReFault;
1073
1074 default:
1075 #ifdef DIAGNOSTIC
1076 panic("uvm_fault: uvmfault_anonget -> %d", result);
1077 #else
1078 return (KERN_PROTECTION_FAILURE);
1079 #endif
1080 }
1081
1082 /*
1083 * uobj is non null if the page is on loan from an object (i.e. uobj)
1084 */
1085
1086 uobj = anon->u.an_page->uobject; /* locked by anonget if !NULL */
1087
1088 /* locked: maps(read), amap, anon, uobj(if one) */
1089
1090 /*
1091 * special handling for loaned pages
1092 */
1093
1094 if (anon->u.an_page->loan_count) {
1095
1096 if ((access_type & VM_PROT_WRITE) == 0) {
1097
1098 /*
1099 * for read faults on loaned pages we just cap the
1100 * protection at read-only.
1101 */
1102
1103 enter_prot = enter_prot & ~VM_PROT_WRITE;
1104
1105 } else {
1106 /*
1107 * note that we can't allow writes into a loaned page!
1108 *
1109 * if we have a write fault on a loaned page in an
1110 * anon then we need to look at the anon's ref count.
1111 * if it is greater than one then we are going to do
1112 * a normal copy-on-write fault into a new anon (this
1113 * is not a problem). however, if the reference count
1114 * is one (a case where we would normally allow a
1115 * write directly to the page) then we need to kill
1116 * the loan before we continue.
1117 */
1118
1119 /* >1 case is already ok */
1120 if (anon->an_ref == 1) {
1121
1122 /* get new un-owned replacement page */
1123 pg = uvm_pagealloc(NULL, 0, NULL, 0);
1124 if (pg == NULL) {
1125 uvmfault_unlockall(&ufi, amap, uobj,
1126 anon);
1127 uvm_wait("flt_noram2");
1128 goto ReFault;
1129 }
1130
1131 /*
1132 * copy data, kill loan, and drop uobj lock
1133 * (if any)
1134 */
1135 /* copy old -> new */
1136 uvm_pagecopy(anon->u.an_page, pg);
1137
1138 /* force reload */
1139 pmap_page_protect(anon->u.an_page,
1140 VM_PROT_NONE);
1141 uvm_lock_pageq(); /* KILL loan */
1142 if (uobj)
1143 /* if we were loaning */
1144 anon->u.an_page->loan_count--;
1145 anon->u.an_page->uanon = NULL;
1146 /* in case we owned */
1147 anon->u.an_page->pqflags &= ~PQ_ANON;
1148 uvm_unlock_pageq();
1149 if (uobj) {
1150 simple_unlock(&uobj->vmobjlock);
1151 uobj = NULL;
1152 }
1153
1154 /* install new page in anon */
1155 anon->u.an_page = pg;
1156 pg->uanon = anon;
1157 pg->pqflags |= PQ_ANON;
1158 pg->flags &= ~(PG_BUSY|PG_FAKE);
1159 UVM_PAGE_OWN(pg, NULL);
1160
1161 /* done! */
1162 } /* ref == 1 */
1163 } /* write fault */
1164 } /* loan count */
1165
1166 /*
1167 * if we are case 1B then we will need to allocate a new blank
1168 * anon to transfer the data into. note that we have a lock
1169 * on anon, so no one can busy or release the page until we are done.
1170 * also note that the ref count can't drop to zero here because
1171 * it is > 1 and we are only dropping one ref.
1172 *
1173 * in the (hopefully very rare) case that we are out of RAM we
1174 * will unlock, wait for more RAM, and refault.
1175 *
1176 * if we are out of anon VM we kill the process (XXX: could wait?).
1177 */
1178
1179 if ((access_type & VM_PROT_WRITE) != 0 && anon->an_ref > 1) {
1180
1181 UVMHIST_LOG(maphist, " case 1B: COW fault",0,0,0,0);
1182 uvmexp.flt_acow++;
1183 oanon = anon; /* oanon = old, locked anon */
1184 anon = uvm_analloc();
1185 if (anon) {
1186 simple_lock(&anon->an_lock);
1187 pg = uvm_pagealloc(NULL, 0, anon, 0);
1188 }
1189
1190 /* check for out of RAM */
1191 if (anon == NULL || pg == NULL) {
1192 if (anon) {
1193 anon->an_ref--;
1194 simple_unlock(&anon->an_lock);
1195 uvm_anfree(anon);
1196 }
1197 uvmfault_unlockall(&ufi, amap, uobj, oanon);
1198 KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
1199 if (anon == NULL || uvmexp.swpgonly == uvmexp.swpages) {
1200 UVMHIST_LOG(maphist,
1201 "<- failed. out of VM",0,0,0,0);
1202 uvmexp.fltnoanon++;
1203 return (KERN_RESOURCE_SHORTAGE);
1204 }
1205
1206 uvmexp.fltnoram++;
1207 uvm_wait("flt_noram3"); /* out of RAM, wait for more */
1208 goto ReFault;
1209 }
1210
1211 /* got all resources, replace anon with nanon */
1212
1213 uvm_pagecopy(oanon->u.an_page, pg); /* pg now !PG_CLEAN */
1214 pg->flags &= ~(PG_BUSY|PG_FAKE); /* un-busy! new page */
1215 UVM_PAGE_OWN(pg, NULL);
1216 amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start,
1217 anon, 1);
1218
1219 /* deref: can not drop to zero here by defn! */
1220 oanon->an_ref--;
1221
1222 /*
1223 * note: oanon is still locked, as is the new anon. we
1224 * need to check for this later when we unlock oanon; if
1225 * oanon != anon, we'll have to unlock anon, too.
1226 */
1227
1228 } else {
1229
1230 uvmexp.flt_anon++;
1231 oanon = anon; /* old, locked anon is same as anon */
1232 pg = anon->u.an_page;
1233 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */
1234 enter_prot = enter_prot & ~VM_PROT_WRITE;
1235
1236 }
1237
1238 /* locked: maps(read), amap, oanon, anon (if different from oanon) */
1239
1240 /*
1241 * now map the page in ...
1242 * XXX: old fault unlocks object before pmap_enter. this seems
1243 * suspect since some other thread could blast the page out from
1244 * under us between the unlock and the pmap_enter.
1245 */
1246
1247 UVMHIST_LOG(maphist, " MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x",
1248 ufi.orig_map->pmap, ufi.orig_rvaddr, pg, 0);
1249 if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),
1250 enter_prot, access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0))
1251 != KERN_SUCCESS) {
1252 /*
1253 * No need to undo what we did; we can simply think of
1254 * this as the pmap throwing away the mapping information.
1255 *
1256 * We do, however, have to go through the ReFault path,
1257 * as the map may change while we're asleep.
1258 */
1259 if (anon != oanon)
1260 simple_unlock(&anon->an_lock);
1261 uvmfault_unlockall(&ufi, amap, uobj, oanon);
1262 KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
1263 if (uvmexp.swpgonly == uvmexp.swpages) {
1264 UVMHIST_LOG(maphist,
1265 "<- failed. out of VM",0,0,0,0);
1266 /* XXX instrumentation */
1267 return (KERN_RESOURCE_SHORTAGE);
1268 }
1269 /* XXX instrumentation */
1270 uvm_wait("flt_pmfail1");
1271 goto ReFault;
1272 }
1273
1274 /*
1275 * ... update the page queues.
1276 */
1277
1278 uvm_lock_pageq();
1279
1280 if (fault_type == VM_FAULT_WIRE) {
1281 uvm_pagewire(pg);
1282
1283 /*
1284 * since the now-wired page cannot be paged out,
1285 * release its swap resources for others to use.
1286 * since an anon with no swap cannot be PG_CLEAN,
1287 * clear its clean flag now.
1288 */
1289
1290 pg->flags &= ~(PG_CLEAN);
1291 uvm_anon_dropswap(anon);
1292 } else {
1293 /* activate it */
1294 uvm_pageactivate(pg);
1295 }
1296
1297 uvm_unlock_pageq();
1298
1299 /*
1300 * done case 1! finish up by unlocking everything and returning success
1301 */
1302
1303 if (anon != oanon)
1304 simple_unlock(&anon->an_lock);
1305 uvmfault_unlockall(&ufi, amap, uobj, oanon);
1306 return (KERN_SUCCESS);
1307
1308
1309 Case2:
1310 /*
1311 * handle case 2: faulting on backing object or zero fill
1312 */
1313
1314 /*
1315 * locked:
1316 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
1317 */
1318
1319 /*
1320 * note that uobjpage can not be PGO_DONTCARE at this point. we now
1321 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we
1322 * have a backing object, check and see if we are going to promote
1323 * the data up to an anon during the fault.
1324 */
1325
1326 if (uobj == NULL) {
1327 uobjpage = PGO_DONTCARE;
1328 promote = TRUE; /* always need anon here */
1329 } else {
1330 KASSERT(uobjpage != PGO_DONTCARE);
1331 promote = (access_type & VM_PROT_WRITE) &&
1332 UVM_ET_ISCOPYONWRITE(ufi.entry);
1333 }
1334 UVMHIST_LOG(maphist, " case 2 fault: promote=%d, zfill=%d",
1335 promote, (uobj == NULL), 0,0);
1336
1337 /*
1338 * if uobjpage is not null then we do not need to do I/O to get the
1339 * uobjpage.
1340 *
1341 * if uobjpage is null, then we need to unlock and ask the pager to
1342 * get the data for us. once we have the data, we need to reverify
1343 * the state the world. we are currently not holding any resources.
1344 */
1345
1346 if (uobjpage) {
1347 /* update rusage counters */
1348 curproc->p_addr->u_stats.p_ru.ru_minflt++;
1349 } else {
1350 /* update rusage counters */
1351 curproc->p_addr->u_stats.p_ru.ru_majflt++;
1352
1353 /* locked: maps(read), amap(if there), uobj */
1354 uvmfault_unlockall(&ufi, amap, NULL, NULL);
1355 /* locked: uobj */
1356
1357 uvmexp.fltget++;
1358 gotpages = 1;
1359 uoff = (ufi.orig_rvaddr - ufi.entry->start) + ufi.entry->offset;
1360 result = uobj->pgops->pgo_get(uobj, uoff, &uobjpage, &gotpages,
1361 0, access_type & MASK(ufi.entry), ufi.entry->advice,
1362 PGO_SYNCIO);
1363
1364 /* locked: uobjpage(if result OK) */
1365
1366 /*
1367 * recover from I/O
1368 */
1369
1370 if (result != VM_PAGER_OK) {
1371 KASSERT(result != VM_PAGER_PEND);
1372
1373 if (result == VM_PAGER_AGAIN) {
1374 UVMHIST_LOG(maphist,
1375 " pgo_get says TRY AGAIN!",0,0,0,0);
1376 tsleep((caddr_t)&lbolt, PVM, "fltagain2", 0);
1377 goto ReFault;
1378 }
1379
1380 UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)",
1381 result, 0,0,0);
1382 return (KERN_PROTECTION_FAILURE); /* XXX i/o error */
1383 }
1384
1385 /* locked: uobjpage */
1386
1387 /*
1388 * re-verify the state of the world by first trying to relock
1389 * the maps. always relock the object.
1390 */
1391
1392 locked = uvmfault_relock(&ufi);
1393 if (locked && amap)
1394 amap_lock(amap);
1395 simple_lock(&uobj->vmobjlock);
1396
1397 /* locked(locked): maps(read), amap(if !null), uobj, uobjpage */
1398 /* locked(!locked): uobj, uobjpage */
1399
1400 /*
1401 * verify that the page has not be released and re-verify
1402 * that amap slot is still free. if there is a problem,
1403 * we unlock and clean up.
1404 */
1405
1406 if ((uobjpage->flags & PG_RELEASED) != 0 ||
1407 (locked && amap &&
1408 amap_lookup(&ufi.entry->aref,
1409 ufi.orig_rvaddr - ufi.entry->start))) {
1410 if (locked)
1411 uvmfault_unlockall(&ufi, amap, NULL, NULL);
1412 locked = FALSE;
1413 }
1414
1415 /*
1416 * didn't get the lock? release the page and retry.
1417 */
1418
1419 if (locked == FALSE) {
1420
1421 UVMHIST_LOG(maphist,
1422 " wasn't able to relock after fault: retry",
1423 0,0,0,0);
1424 if (uobjpage->flags & PG_WANTED)
1425 /* still holding object lock */
1426 wakeup(uobjpage);
1427
1428 if (uobjpage->flags & PG_RELEASED) {
1429 uvmexp.fltpgrele++;
1430 KASSERT(uobj->pgops->pgo_releasepg != NULL);
1431
1432 /* frees page */
1433 if (uobj->pgops->pgo_releasepg(uobjpage,NULL))
1434 /* unlock if still alive */
1435 simple_unlock(&uobj->vmobjlock);
1436 goto ReFault;
1437 }
1438
1439 uvm_lock_pageq();
1440 /* make sure it is in queues */
1441 uvm_pageactivate(uobjpage);
1442
1443 uvm_unlock_pageq();
1444 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1445 UVM_PAGE_OWN(uobjpage, NULL);
1446 simple_unlock(&uobj->vmobjlock);
1447 goto ReFault;
1448
1449 }
1450
1451 /*
1452 * we have the data in uobjpage which is PG_BUSY and
1453 * !PG_RELEASED. we are holding object lock (so the page
1454 * can't be released on us).
1455 */
1456
1457 /* locked: maps(read), amap(if !null), uobj, uobjpage */
1458 }
1459
1460 /*
1461 * locked:
1462 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
1463 */
1464
1465 /*
1466 * notes:
1467 * - at this point uobjpage can not be NULL
1468 * - at this point uobjpage can not be PG_RELEASED (since we checked
1469 * for it above)
1470 * - at this point uobjpage could be PG_WANTED (handle later)
1471 */
1472
1473 if (promote == FALSE) {
1474
1475 /*
1476 * we are not promoting. if the mapping is COW ensure that we
1477 * don't give more access than we should (e.g. when doing a read
1478 * fault on a COPYONWRITE mapping we want to map the COW page in
1479 * R/O even though the entry protection could be R/W).
1480 *
1481 * set "pg" to the page we want to map in (uobjpage, usually)
1482 */
1483
1484 /* no anon in this case. */
1485 anon = NULL;
1486
1487 uvmexp.flt_obj++;
1488 if (UVM_ET_ISCOPYONWRITE(ufi.entry))
1489 enter_prot &= ~VM_PROT_WRITE;
1490 pg = uobjpage; /* map in the actual object */
1491
1492 /* assert(uobjpage != PGO_DONTCARE) */
1493
1494 /*
1495 * we are faulting directly on the page. be careful
1496 * about writing to loaned pages...
1497 */
1498 if (uobjpage->loan_count) {
1499
1500 if ((access_type & VM_PROT_WRITE) == 0) {
1501 /* read fault: cap the protection at readonly */
1502 /* cap! */
1503 enter_prot = enter_prot & ~VM_PROT_WRITE;
1504 } else {
1505 /* write fault: must break the loan here */
1506
1507 /* alloc new un-owned page */
1508 pg = uvm_pagealloc(NULL, 0, NULL, 0);
1509
1510 if (pg == NULL) {
1511 /*
1512 * drop ownership of page, it can't
1513 * be released
1514 */
1515 if (uobjpage->flags & PG_WANTED)
1516 wakeup(uobjpage);
1517 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1518 UVM_PAGE_OWN(uobjpage, NULL);
1519
1520 uvm_lock_pageq();
1521 /* activate: we will need it later */
1522 uvm_pageactivate(uobjpage);
1523
1524 uvm_unlock_pageq();
1525 uvmfault_unlockall(&ufi, amap, uobj,
1526 NULL);
1527 UVMHIST_LOG(maphist,
1528 " out of RAM breaking loan, waiting",
1529 0,0,0,0);
1530 uvmexp.fltnoram++;
1531 uvm_wait("flt_noram4");
1532 goto ReFault;
1533 }
1534
1535 /*
1536 * copy the data from the old page to the new
1537 * one and clear the fake/clean flags on the
1538 * new page (keep it busy). force a reload
1539 * of the old page by clearing it from all
1540 * pmaps. then lock the page queues to
1541 * rename the pages.
1542 */
1543 uvm_pagecopy(uobjpage, pg); /* old -> new */
1544 pg->flags &= ~(PG_FAKE|PG_CLEAN);
1545 pmap_page_protect(uobjpage, VM_PROT_NONE);
1546 if (uobjpage->flags & PG_WANTED)
1547 wakeup(uobjpage);
1548 /* uobj still locked */
1549 uobjpage->flags &= ~(PG_WANTED|PG_BUSY);
1550 UVM_PAGE_OWN(uobjpage, NULL);
1551
1552 uvm_lock_pageq();
1553 offset = uobjpage->offset;
1554 /* remove old page */
1555 uvm_pagerealloc(uobjpage, NULL, 0);
1556
1557 /*
1558 * at this point we have absolutely no
1559 * control over uobjpage
1560 */
1561 /* install new page */
1562 uvm_pagerealloc(pg, uobj, offset);
1563 uvm_unlock_pageq();
1564
1565 /*
1566 * done! loan is broken and "pg" is
1567 * PG_BUSY. it can now replace uobjpage.
1568 */
1569
1570 uobjpage = pg;
1571
1572 } /* write fault case */
1573 } /* if loan_count */
1574
1575 } else {
1576
1577 /*
1578 * if we are going to promote the data to an anon we
1579 * allocate a blank anon here and plug it into our amap.
1580 */
1581 #if DIAGNOSTIC
1582 if (amap == NULL)
1583 panic("uvm_fault: want to promote data, but no anon");
1584 #endif
1585
1586 anon = uvm_analloc();
1587 if (anon) {
1588 /*
1589 * In `Fill in data...' below, if
1590 * uobjpage == PGO_DONTCARE, we want
1591 * a zero'd, dirty page, so have
1592 * uvm_pagealloc() do that for us.
1593 */
1594 simple_lock(&anon->an_lock);
1595 pg = uvm_pagealloc(NULL, 0, anon,
1596 (uobjpage == PGO_DONTCARE) ? UVM_PGA_ZERO : 0);
1597 }
1598
1599 /*
1600 * out of memory resources?
1601 */
1602 if (anon == NULL || pg == NULL) {
1603
1604 if (anon != NULL) {
1605 anon->an_ref--;
1606 simple_unlock(&anon->an_lock);
1607 uvm_anfree(anon);
1608 }
1609
1610 /*
1611 * arg! must unbusy our page and fail or sleep.
1612 */
1613 if (uobjpage != PGO_DONTCARE) {
1614 if (uobjpage->flags & PG_WANTED)
1615 /* still holding object lock */
1616 wakeup(uobjpage);
1617
1618 uvm_lock_pageq();
1619 uvm_pageactivate(uobjpage);
1620 uvm_unlock_pageq();
1621 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1622 UVM_PAGE_OWN(uobjpage, NULL);
1623 }
1624
1625 /* unlock and fail ... */
1626 uvmfault_unlockall(&ufi, amap, uobj, NULL);
1627 KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
1628 if (anon == NULL || uvmexp.swpgonly == uvmexp.swpages) {
1629 UVMHIST_LOG(maphist, " promote: out of VM",
1630 0,0,0,0);
1631 uvmexp.fltnoanon++;
1632 return (KERN_RESOURCE_SHORTAGE);
1633 }
1634
1635 UVMHIST_LOG(maphist, " out of RAM, waiting for more",
1636 0,0,0,0);
1637 uvmexp.fltnoram++;
1638 uvm_wait("flt_noram5");
1639 goto ReFault;
1640 }
1641
1642 /*
1643 * fill in the data
1644 */
1645
1646 if (uobjpage != PGO_DONTCARE) {
1647 uvmexp.flt_prcopy++;
1648 /* copy page [pg now dirty] */
1649 uvm_pagecopy(uobjpage, pg);
1650
1651 /*
1652 * promote to shared amap? make sure all sharing
1653 * procs see it
1654 */
1655 if ((amap_flags(amap) & AMAP_SHARED) != 0) {
1656 pmap_page_protect(uobjpage, VM_PROT_NONE);
1657 }
1658
1659 /*
1660 * dispose of uobjpage. it can't be PG_RELEASED
1661 * since we still hold the object lock.
1662 * drop handle to uobj as well.
1663 */
1664
1665 if (uobjpage->flags & PG_WANTED)
1666 /* still have the obj lock */
1667 wakeup(uobjpage);
1668 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1669 UVM_PAGE_OWN(uobjpage, NULL);
1670 uvm_lock_pageq();
1671 uvm_pageactivate(uobjpage);
1672 uvm_unlock_pageq();
1673 simple_unlock(&uobj->vmobjlock);
1674 uobj = NULL;
1675
1676 UVMHIST_LOG(maphist,
1677 " promote uobjpage 0x%x to anon/page 0x%x/0x%x",
1678 uobjpage, anon, pg, 0);
1679
1680 } else {
1681 uvmexp.flt_przero++;
1682 /*
1683 * Page is zero'd and marked dirty by uvm_pagealloc()
1684 * above.
1685 */
1686 UVMHIST_LOG(maphist," zero fill anon/page 0x%x/0%x",
1687 anon, pg, 0, 0);
1688 }
1689
1690 amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start,
1691 anon, 0);
1692 }
1693
1694 /*
1695 * locked:
1696 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj),
1697 * anon(if !null), pg(if anon)
1698 *
1699 * note: pg is either the uobjpage or the new page in the new anon
1700 */
1701
1702 /*
1703 * all resources are present. we can now map it in and free our
1704 * resources.
1705 */
1706
1707 UVMHIST_LOG(maphist,
1708 " MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d",
1709 ufi.orig_map->pmap, ufi.orig_rvaddr, pg, promote);
1710 KASSERT(access_type == VM_PROT_READ || (pg->flags & PG_RDONLY) == 0);
1711 if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),
1712 pg->flags & PG_RDONLY ? VM_PROT_READ : enter_prot,
1713 access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0))
1714 != KERN_SUCCESS) {
1715
1716 /*
1717 * No need to undo what we did; we can simply think of
1718 * this as the pmap throwing away the mapping information.
1719 *
1720 * We do, however, have to go through the ReFault path,
1721 * as the map may change while we're asleep.
1722 */
1723
1724 if (pg->flags & PG_WANTED)
1725 wakeup(pg); /* lock still held */
1726
1727 /*
1728 * note that pg can't be PG_RELEASED since we did not drop
1729 * the object lock since the last time we checked.
1730 */
1731
1732 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
1733 UVM_PAGE_OWN(pg, NULL);
1734 uvmfault_unlockall(&ufi, amap, uobj, anon);
1735 KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
1736 if (uvmexp.swpgonly == uvmexp.swpages) {
1737 UVMHIST_LOG(maphist,
1738 "<- failed. out of VM",0,0,0,0);
1739 /* XXX instrumentation */
1740 return (KERN_RESOURCE_SHORTAGE);
1741 }
1742 /* XXX instrumentation */
1743 uvm_wait("flt_pmfail2");
1744 goto ReFault;
1745 }
1746
1747 uvm_lock_pageq();
1748
1749 if (fault_type == VM_FAULT_WIRE) {
1750 uvm_pagewire(pg);
1751 if (pg->pqflags & PQ_AOBJ) {
1752
1753 /*
1754 * since the now-wired page cannot be paged out,
1755 * release its swap resources for others to use.
1756 * since an aobj page with no swap cannot be PG_CLEAN,
1757 * clear its clean flag now.
1758 */
1759
1760 pg->flags &= ~(PG_CLEAN);
1761 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
1762 }
1763 } else {
1764 /* activate it */
1765 uvm_pageactivate(pg);
1766 }
1767 uvm_unlock_pageq();
1768
1769 if (pg->flags & PG_WANTED)
1770 wakeup(pg); /* lock still held */
1771
1772 /*
1773 * note that pg can't be PG_RELEASED since we did not drop the object
1774 * lock since the last time we checked.
1775 */
1776
1777 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
1778 UVM_PAGE_OWN(pg, NULL);
1779 uvmfault_unlockall(&ufi, amap, uobj, anon);
1780
1781 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
1782 return (KERN_SUCCESS);
1783 }
1784
1785
1786 /*
1787 * uvm_fault_wire: wire down a range of virtual addresses in a map.
1788 *
1789 * => map may be read-locked by caller, but MUST NOT be write-locked.
1790 * => if map is read-locked, any operations which may cause map to
1791 * be write-locked in uvm_fault() must be taken care of by
1792 * the caller. See uvm_map_pageable().
1793 */
1794
1795 int
1796 uvm_fault_wire(map, start, end, access_type)
1797 vm_map_t map;
1798 vaddr_t start, end;
1799 vm_prot_t access_type;
1800 {
1801 vaddr_t va;
1802 pmap_t pmap;
1803 int rv;
1804
1805 pmap = vm_map_pmap(map);
1806
1807 /*
1808 * now fault it in a page at a time. if the fault fails then we have
1809 * to undo what we have done. note that in uvm_fault VM_PROT_NONE
1810 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
1811 */
1812
1813 for (va = start ; va < end ; va += PAGE_SIZE) {
1814 rv = uvm_fault(map, va, VM_FAULT_WIRE, access_type);
1815 if (rv) {
1816 if (va != start) {
1817 uvm_fault_unwire(map, start, va);
1818 }
1819 return (rv);
1820 }
1821 }
1822
1823 return (KERN_SUCCESS);
1824 }
1825
1826 /*
1827 * uvm_fault_unwire(): unwire range of virtual space.
1828 */
1829
1830 void
1831 uvm_fault_unwire(map, start, end)
1832 vm_map_t map;
1833 vaddr_t start, end;
1834 {
1835
1836 vm_map_lock_read(map);
1837 uvm_fault_unwire_locked(map, start, end);
1838 vm_map_unlock_read(map);
1839 }
1840
1841 /*
1842 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
1843 *
1844 * => map must be at least read-locked.
1845 */
1846
1847 void
1848 uvm_fault_unwire_locked(map, start, end)
1849 vm_map_t map;
1850 vaddr_t start, end;
1851 {
1852 vm_map_entry_t entry;
1853 pmap_t pmap = vm_map_pmap(map);
1854 vaddr_t va;
1855 paddr_t pa;
1856 struct vm_page *pg;
1857
1858 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0);
1859
1860 /*
1861 * we assume that the area we are unwiring has actually been wired
1862 * in the first place. this means that we should be able to extract
1863 * the PAs from the pmap. we also lock out the page daemon so that
1864 * we can call uvm_pageunwire.
1865 */
1866
1867 uvm_lock_pageq();
1868
1869 /*
1870 * find the beginning map entry for the region.
1871 */
1872 #ifdef DIAGNOSTIC
1873 if (start < vm_map_min(map) || end > vm_map_max(map))
1874 panic("uvm_fault_unwire_locked: address out of range");
1875 #endif
1876 if (uvm_map_lookup_entry(map, start, &entry) == FALSE)
1877 panic("uvm_fault_unwire_locked: address not in map");
1878
1879 for (va = start; va < end ; va += PAGE_SIZE) {
1880 if (pmap_extract(pmap, va, &pa) == FALSE)
1881 panic("uvm_fault_unwire_locked: unwiring "
1882 "non-wired memory");
1883
1884 /*
1885 * make sure the current entry is for the address we're
1886 * dealing with. if not, grab the next entry.
1887 */
1888 #ifdef DIAGNOSTIC
1889 if (va < entry->start)
1890 panic("uvm_fault_unwire_locked: hole 1");
1891 #endif
1892 if (va >= entry->end) {
1893 #ifdef DIAGNOSTIC
1894 if (entry->next == &map->header ||
1895 entry->next->start > entry->end)
1896 panic("uvm_fault_unwire_locked: hole 2");
1897 #endif
1898 entry = entry->next;
1899 }
1900
1901 /*
1902 * if the entry is no longer wired, tell the pmap.
1903 */
1904 if (VM_MAPENT_ISWIRED(entry) == 0)
1905 pmap_unwire(pmap, va);
1906
1907 pg = PHYS_TO_VM_PAGE(pa);
1908 if (pg)
1909 uvm_pageunwire(pg);
1910 }
1911
1912 uvm_unlock_pageq();
1913 }
1914