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