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