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