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