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