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