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