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