uvm_fault.c revision 1.93 1 /* $NetBSD: uvm_fault.c,v 1.93 2005/04/27 15:19:17 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.93 2005/04/27 15:19:17 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(struct uvm_faultinfo *);
183 static __inline void uvmfault_anonflush(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 if (!uvm_reclaimable()) {
381 return ENOMEM;
382 }
383 uvm_wait("flt_noram1");
384 } else {
385 /* we set the PG_BUSY bit */
386 we_own = TRUE;
387 uvmfault_unlockall(ufi, amap, NULL, anon);
388
389 /*
390 * we are passing a PG_BUSY+PG_FAKE+PG_CLEAN
391 * page into the uvm_swap_get function with
392 * all data structures unlocked. note that
393 * it is ok to read an_swslot here because
394 * we hold PG_BUSY on the page.
395 */
396 uvmexp.pageins++;
397 error = uvm_swap_get(pg, anon->an_swslot,
398 PGO_SYNCIO);
399
400 /*
401 * we clean up after the i/o below in the
402 * "we_own" case
403 */
404 }
405 }
406
407 /*
408 * now relock and try again
409 */
410
411 locked = uvmfault_relock(ufi);
412 if (locked && amap != NULL) {
413 amap_lock(amap);
414 }
415 if (locked || we_own)
416 simple_lock(&anon->an_lock);
417
418 /*
419 * if we own the page (i.e. we set PG_BUSY), then we need
420 * to clean up after the I/O. there are three cases to
421 * consider:
422 * [1] page released during I/O: free anon and ReFault.
423 * [2] I/O not OK. free the page and cause the fault
424 * to fail.
425 * [3] I/O OK! activate the page and sync with the
426 * non-we_own case (i.e. drop anon lock if not locked).
427 */
428
429 if (we_own) {
430 if (pg->flags & PG_WANTED) {
431 wakeup(pg);
432 }
433 if (error) {
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 if (anon->an_swslot > 0)
443 uvm_swap_markbad(anon->an_swslot, 1);
444 anon->an_swslot = SWSLOT_BAD;
445
446 if ((pg->flags & PG_RELEASED) != 0)
447 goto released;
448
449 /*
450 * note: page was never !PG_BUSY, so it
451 * can't be mapped and thus no need to
452 * pmap_page_protect it...
453 */
454
455 uvm_lock_pageq();
456 uvm_pagefree(pg);
457 uvm_unlock_pageq();
458
459 if (locked)
460 uvmfault_unlockall(ufi, amap, NULL,
461 anon);
462 else
463 simple_unlock(&anon->an_lock);
464 UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
465 return error;
466 }
467
468 if ((pg->flags & PG_RELEASED) != 0) {
469 released:
470 KASSERT(anon->an_ref == 0);
471
472 /*
473 * released while we unlocked amap.
474 */
475
476 if (locked)
477 uvmfault_unlockall(ufi, amap, NULL,
478 NULL);
479
480 uvm_anon_release(anon);
481
482 if (error) {
483 UVMHIST_LOG(maphist,
484 "<- ERROR/RELEASED", 0,0,0,0);
485 return error;
486 }
487
488 UVMHIST_LOG(maphist, "<- RELEASED", 0,0,0,0);
489 return ERESTART;
490 }
491
492 /*
493 * we've successfully read the page, activate it.
494 */
495
496 uvm_lock_pageq();
497 uvm_pageactivate(pg);
498 uvm_unlock_pageq();
499 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
500 UVM_PAGE_OWN(pg, NULL);
501 if (!locked)
502 simple_unlock(&anon->an_lock);
503 }
504
505 /*
506 * we were not able to relock. restart fault.
507 */
508
509 if (!locked) {
510 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
511 return (ERESTART);
512 }
513
514 /*
515 * verify no one has touched the amap and moved the anon on us.
516 */
517
518 if (ufi != NULL &&
519 amap_lookup(&ufi->entry->aref,
520 ufi->orig_rvaddr - ufi->entry->start) != anon) {
521
522 uvmfault_unlockall(ufi, amap, NULL, anon);
523 UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
524 return (ERESTART);
525 }
526
527 /*
528 * try it again!
529 */
530
531 uvmexp.fltanretry++;
532 continue;
533 }
534 /*NOTREACHED*/
535 }
536
537 /*
538 * F A U L T - m a i n e n t r y p o i n t
539 */
540
541 /*
542 * uvm_fault: page fault handler
543 *
544 * => called from MD code to resolve a page fault
545 * => VM data structures usually should be unlocked. however, it is
546 * possible to call here with the main map locked if the caller
547 * gets a write lock, sets it recusive, and then calls us (c.f.
548 * uvm_map_pageable). this should be avoided because it keeps
549 * the map locked off during I/O.
550 * => MUST NEVER BE CALLED IN INTERRUPT CONTEXT
551 */
552
553 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \
554 ~VM_PROT_WRITE : VM_PROT_ALL)
555
556 int
557 uvm_fault(orig_map, vaddr, fault_type, access_type)
558 struct vm_map *orig_map;
559 vaddr_t vaddr;
560 vm_fault_t fault_type;
561 vm_prot_t access_type;
562 {
563 struct uvm_faultinfo ufi;
564 vm_prot_t enter_prot, check_prot;
565 boolean_t wired, narrow, promote, locked, shadowed, wire_fault, cow_now;
566 int npages, nback, nforw, centeridx, error, lcv, gotpages;
567 vaddr_t startva, currva;
568 voff_t uoff;
569 struct vm_amap *amap;
570 struct uvm_object *uobj;
571 struct vm_anon *anons_store[UVM_MAXRANGE], **anons, *anon, *oanon;
572 struct vm_page *pages[UVM_MAXRANGE], *pg, *uobjpage;
573 UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist);
574
575 UVMHIST_LOG(maphist, "(map=0x%x, vaddr=0x%x, ft=%d, at=%d)",
576 orig_map, vaddr, fault_type, access_type);
577
578 anon = NULL;
579 pg = NULL;
580
581 uvmexp.faults++; /* XXX: locking? */
582
583 /*
584 * init the IN parameters in the ufi
585 */
586
587 ufi.orig_map = orig_map;
588 ufi.orig_rvaddr = trunc_page(vaddr);
589 ufi.orig_size = PAGE_SIZE; /* can't get any smaller than this */
590 wire_fault = fault_type == VM_FAULT_WIRE ||
591 fault_type == VM_FAULT_WIREMAX;
592 if (wire_fault)
593 narrow = TRUE; /* don't look for neighborhood
594 * pages on wire */
595 else
596 narrow = FALSE; /* normal fault */
597
598 /*
599 * "goto ReFault" means restart the page fault from ground zero.
600 */
601 ReFault:
602
603 /*
604 * lookup and lock the maps
605 */
606
607 if (uvmfault_lookup(&ufi, FALSE) == FALSE) {
608 UVMHIST_LOG(maphist, "<- no mapping @ 0x%x", vaddr, 0,0,0);
609 return (EFAULT);
610 }
611 /* locked: maps(read) */
612
613 #ifdef DIAGNOSTIC
614 if ((ufi.map->flags & VM_MAP_PAGEABLE) == 0) {
615 printf("Page fault on non-pageable map:\n");
616 printf("ufi.map = %p\n", ufi.map);
617 printf("ufi.orig_map = %p\n", ufi.orig_map);
618 printf("ufi.orig_rvaddr = 0x%lx\n", (u_long) ufi.orig_rvaddr);
619 panic("uvm_fault: (ufi.map->flags & VM_MAP_PAGEABLE) == 0");
620 }
621 #endif
622
623 /*
624 * check protection
625 */
626
627 check_prot = fault_type == VM_FAULT_WIREMAX ?
628 ufi.entry->max_protection : ufi.entry->protection;
629 if ((check_prot & access_type) != access_type) {
630 UVMHIST_LOG(maphist,
631 "<- protection failure (prot=0x%x, access=0x%x)",
632 ufi.entry->protection, access_type, 0, 0);
633 uvmfault_unlockmaps(&ufi, FALSE);
634 return EACCES;
635 }
636
637 /*
638 * "enter_prot" is the protection we want to enter the page in at.
639 * for certain pages (e.g. copy-on-write pages) this protection can
640 * be more strict than ufi.entry->protection. "wired" means either
641 * the entry is wired or we are fault-wiring the pg.
642 */
643
644 enter_prot = ufi.entry->protection;
645 wired = VM_MAPENT_ISWIRED(ufi.entry) || wire_fault;
646 if (wired) {
647 access_type = enter_prot; /* full access for wired */
648 cow_now = (check_prot & VM_PROT_WRITE) != 0;
649 } else {
650 cow_now = (access_type & VM_PROT_WRITE) != 0;
651 }
652
653 /*
654 * handle "needs_copy" case. if we need to copy the amap we will
655 * have to drop our readlock and relock it with a write lock. (we
656 * need a write lock to change anything in a map entry [e.g.
657 * needs_copy]).
658 */
659
660 if (UVM_ET_ISNEEDSCOPY(ufi.entry)) {
661 KASSERT(fault_type != VM_FAULT_WIREMAX);
662 if (cow_now || (ufi.entry->object.uvm_obj == NULL)) {
663 /* need to clear */
664 UVMHIST_LOG(maphist,
665 " need to clear needs_copy and refault",0,0,0,0);
666 uvmfault_unlockmaps(&ufi, FALSE);
667 uvmfault_amapcopy(&ufi);
668 uvmexp.fltamcopy++;
669 goto ReFault;
670
671 } else {
672
673 /*
674 * ensure that we pmap_enter page R/O since
675 * needs_copy is still true
676 */
677
678 enter_prot &= ~VM_PROT_WRITE;
679 }
680 }
681
682 /*
683 * identify the players
684 */
685
686 amap = ufi.entry->aref.ar_amap; /* top layer */
687 uobj = ufi.entry->object.uvm_obj; /* bottom layer */
688
689 /*
690 * check for a case 0 fault. if nothing backing the entry then
691 * error now.
692 */
693
694 if (amap == NULL && uobj == NULL) {
695 uvmfault_unlockmaps(&ufi, FALSE);
696 UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
697 return (EFAULT);
698 }
699
700 /*
701 * establish range of interest based on advice from mapper
702 * and then clip to fit map entry. note that we only want
703 * to do this the first time through the fault. if we
704 * ReFault we will disable this by setting "narrow" to true.
705 */
706
707 if (narrow == FALSE) {
708
709 /* wide fault (!narrow) */
710 KASSERT(uvmadvice[ufi.entry->advice].advice ==
711 ufi.entry->advice);
712 nback = MIN(uvmadvice[ufi.entry->advice].nback,
713 (ufi.orig_rvaddr - ufi.entry->start) >> PAGE_SHIFT);
714 startva = ufi.orig_rvaddr - (nback << PAGE_SHIFT);
715 nforw = MIN(uvmadvice[ufi.entry->advice].nforw,
716 ((ufi.entry->end - ufi.orig_rvaddr) >>
717 PAGE_SHIFT) - 1);
718 /*
719 * note: "-1" because we don't want to count the
720 * faulting page as forw
721 */
722 npages = nback + nforw + 1;
723 centeridx = nback;
724
725 narrow = TRUE; /* ensure only once per-fault */
726
727 } else {
728
729 /* narrow fault! */
730 nback = nforw = 0;
731 startva = ufi.orig_rvaddr;
732 npages = 1;
733 centeridx = 0;
734
735 }
736
737 /* locked: maps(read) */
738 UVMHIST_LOG(maphist, " narrow=%d, back=%d, forw=%d, startva=0x%x",
739 narrow, nback, nforw, startva);
740 UVMHIST_LOG(maphist, " entry=0x%x, amap=0x%x, obj=0x%x", ufi.entry,
741 amap, uobj, 0);
742
743 /*
744 * if we've got an amap, lock it and extract current anons.
745 */
746
747 if (amap) {
748 amap_lock(amap);
749 anons = anons_store;
750 amap_lookups(&ufi.entry->aref, startva - ufi.entry->start,
751 anons, npages);
752 } else {
753 anons = NULL; /* to be safe */
754 }
755
756 /* locked: maps(read), amap(if there) */
757
758 /*
759 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
760 * now and then forget about them (for the rest of the fault).
761 */
762
763 if (ufi.entry->advice == MADV_SEQUENTIAL && nback != 0) {
764
765 UVMHIST_LOG(maphist, " MADV_SEQUENTIAL: flushing backpages",
766 0,0,0,0);
767 /* flush back-page anons? */
768 if (amap)
769 uvmfault_anonflush(anons, nback);
770
771 /* flush object? */
772 if (uobj) {
773 uoff = (startva - ufi.entry->start) + ufi.entry->offset;
774 simple_lock(&uobj->vmobjlock);
775 (void) (uobj->pgops->pgo_put)(uobj, uoff, uoff +
776 (nback << PAGE_SHIFT), PGO_DEACTIVATE);
777 }
778
779 /* now forget about the backpages */
780 if (amap)
781 anons += nback;
782 startva += (nback << PAGE_SHIFT);
783 npages -= nback;
784 nback = centeridx = 0;
785 }
786
787 /* locked: maps(read), amap(if there) */
788
789 /*
790 * map in the backpages and frontpages we found in the amap in hopes
791 * of preventing future faults. we also init the pages[] array as
792 * we go.
793 */
794
795 currva = startva;
796 shadowed = FALSE;
797 for (lcv = 0 ; lcv < npages ; lcv++, currva += PAGE_SIZE) {
798
799 /*
800 * dont play with VAs that are already mapped
801 * except for center)
802 */
803 if (lcv != centeridx &&
804 pmap_extract(ufi.orig_map->pmap, currva, NULL)) {
805 pages[lcv] = PGO_DONTCARE;
806 continue;
807 }
808
809 /*
810 * unmapped or center page. check if any anon at this level.
811 */
812 if (amap == NULL || anons[lcv] == NULL) {
813 pages[lcv] = NULL;
814 continue;
815 }
816
817 /*
818 * check for present page and map if possible. re-activate it.
819 */
820
821 pages[lcv] = PGO_DONTCARE;
822 if (lcv == centeridx) { /* save center for later! */
823 shadowed = TRUE;
824 continue;
825 }
826 anon = anons[lcv];
827 simple_lock(&anon->an_lock);
828 /* ignore loaned pages */
829 if (anon->u.an_page && anon->u.an_page->loan_count == 0 &&
830 (anon->u.an_page->flags & PG_BUSY) == 0) {
831 uvm_lock_pageq();
832 uvm_pageactivate(anon->u.an_page);
833 uvm_unlock_pageq();
834 UVMHIST_LOG(maphist,
835 " MAPPING: n anon: pm=0x%x, va=0x%x, pg=0x%x",
836 ufi.orig_map->pmap, currva, anon->u.an_page, 0);
837 uvmexp.fltnamap++;
838
839 /*
840 * Since this isn't the page that's actually faulting,
841 * ignore pmap_enter() failures; it's not critical
842 * that we enter these right now.
843 */
844
845 (void) pmap_enter(ufi.orig_map->pmap, currva,
846 VM_PAGE_TO_PHYS(anon->u.an_page),
847 (anon->an_ref > 1) ? (enter_prot & ~VM_PROT_WRITE) :
848 enter_prot,
849 PMAP_CANFAIL |
850 (VM_MAPENT_ISWIRED(ufi.entry) ? PMAP_WIRED : 0));
851 }
852 simple_unlock(&anon->an_lock);
853 pmap_update(ufi.orig_map->pmap);
854 }
855
856 /* locked: maps(read), amap(if there) */
857 /* (shadowed == TRUE) if there is an anon at the faulting address */
858 UVMHIST_LOG(maphist, " shadowed=%d, will_get=%d", shadowed,
859 (uobj && shadowed == FALSE),0,0);
860
861 /*
862 * note that if we are really short of RAM we could sleep in the above
863 * call to pmap_enter with everything locked. bad?
864 *
865 * XXX Actually, that is bad; pmap_enter() should just fail in that
866 * XXX case. --thorpej
867 */
868
869 /*
870 * if the desired page is not shadowed by the amap and we have a
871 * backing object, then we check to see if the backing object would
872 * prefer to handle the fault itself (rather than letting us do it
873 * with the usual pgo_get hook). the backing object signals this by
874 * providing a pgo_fault routine.
875 */
876
877 if (uobj && shadowed == FALSE && uobj->pgops->pgo_fault != NULL) {
878 simple_lock(&uobj->vmobjlock);
879
880 /* locked: maps(read), amap (if there), uobj */
881 error = uobj->pgops->pgo_fault(&ufi, startva, pages, npages,
882 centeridx, fault_type, access_type, PGO_LOCKED|PGO_SYNCIO);
883
884 /* locked: nothing, pgo_fault has unlocked everything */
885
886 if (error == ERESTART)
887 goto ReFault; /* try again! */
888 /*
889 * object fault routine responsible for pmap_update().
890 */
891 return error;
892 }
893
894 /*
895 * now, if the desired page is not shadowed by the amap and we have
896 * a backing object that does not have a special fault routine, then
897 * we ask (with pgo_get) the object for resident pages that we care
898 * about and attempt to map them in. we do not let pgo_get block
899 * (PGO_LOCKED).
900 */
901
902 if (uobj && shadowed == FALSE) {
903 simple_lock(&uobj->vmobjlock);
904
905 /* locked (!shadowed): maps(read), amap (if there), uobj */
906 /*
907 * the following call to pgo_get does _not_ change locking state
908 */
909
910 uvmexp.fltlget++;
911 gotpages = npages;
912 (void) uobj->pgops->pgo_get(uobj, ufi.entry->offset +
913 (startva - ufi.entry->start),
914 pages, &gotpages, centeridx,
915 access_type & MASK(ufi.entry),
916 ufi.entry->advice, PGO_LOCKED);
917
918 /*
919 * check for pages to map, if we got any
920 */
921
922 uobjpage = NULL;
923
924 if (gotpages) {
925 currva = startva;
926 for (lcv = 0; lcv < npages;
927 lcv++, currva += PAGE_SIZE) {
928 struct vm_page *curpg;
929 boolean_t readonly;
930
931 curpg = pages[lcv];
932 if (curpg == NULL || curpg == PGO_DONTCARE) {
933 continue;
934 }
935
936 /*
937 * if center page is resident and not
938 * PG_BUSY|PG_RELEASED then pgo_get
939 * made it PG_BUSY for us and gave
940 * us a handle to it. remember this
941 * page as "uobjpage." (for later use).
942 */
943
944 if (lcv == centeridx) {
945 uobjpage = curpg;
946 UVMHIST_LOG(maphist, " got uobjpage "
947 "(0x%x) with locked get",
948 uobjpage, 0,0,0);
949 continue;
950 }
951
952 /*
953 * calling pgo_get with PGO_LOCKED returns us
954 * pages which are neither busy nor released,
955 * so we don't need to check for this.
956 * we can just directly enter the pages.
957 */
958
959 uvm_lock_pageq();
960 uvm_pageactivate(curpg);
961 uvm_unlock_pageq();
962 UVMHIST_LOG(maphist,
963 " MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x",
964 ufi.orig_map->pmap, currva, curpg, 0);
965 uvmexp.fltnomap++;
966
967 /*
968 * Since this page isn't the page that's
969 * actually faulting, ignore pmap_enter()
970 * failures; it's not critical that we
971 * enter these right now.
972 */
973 KASSERT((curpg->flags & PG_PAGEOUT) == 0);
974 KASSERT((curpg->flags & PG_RELEASED) == 0);
975 readonly = (curpg->flags & PG_RDONLY)
976 || (curpg->loan_count > 0);
977
978 (void) pmap_enter(ufi.orig_map->pmap, currva,
979 VM_PAGE_TO_PHYS(curpg),
980 readonly ?
981 enter_prot & ~VM_PROT_WRITE :
982 enter_prot & MASK(ufi.entry),
983 PMAP_CANFAIL |
984 (wired ? PMAP_WIRED : 0));
985
986 /*
987 * NOTE: page can't be PG_WANTED or PG_RELEASED
988 * because we've held the lock the whole time
989 * we've had the handle.
990 */
991
992 curpg->flags &= ~(PG_BUSY);
993 UVM_PAGE_OWN(curpg, NULL);
994 }
995 pmap_update(ufi.orig_map->pmap);
996 }
997 } else {
998 uobjpage = NULL;
999 }
1000
1001 /* locked (shadowed): maps(read), amap */
1002 /* locked (!shadowed): maps(read), amap(if there),
1003 uobj(if !null), uobjpage(if !null) */
1004
1005 /*
1006 * note that at this point we are done with any front or back pages.
1007 * we are now going to focus on the center page (i.e. the one we've
1008 * faulted on). if we have faulted on the top (anon) layer
1009 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
1010 * not touched it yet). if we have faulted on the bottom (uobj)
1011 * layer [i.e. case 2] and the page was both present and available,
1012 * then we've got a pointer to it as "uobjpage" and we've already
1013 * made it BUSY.
1014 */
1015
1016 /*
1017 * there are four possible cases we must address: 1A, 1B, 2A, and 2B
1018 */
1019
1020 /*
1021 * redirect case 2: if we are not shadowed, go to case 2.
1022 */
1023
1024 if (shadowed == FALSE)
1025 goto Case2;
1026
1027 /* locked: maps(read), amap */
1028
1029 /*
1030 * handle case 1: fault on an anon in our amap
1031 */
1032
1033 anon = anons[centeridx];
1034 UVMHIST_LOG(maphist, " case 1 fault: anon=0x%x", anon, 0,0,0);
1035 simple_lock(&anon->an_lock);
1036
1037 /* locked: maps(read), amap, anon */
1038
1039 /*
1040 * no matter if we have case 1A or case 1B we are going to need to
1041 * have the anon's memory resident. ensure that now.
1042 */
1043
1044 /*
1045 * let uvmfault_anonget do the dirty work.
1046 * if it fails (!OK) it will unlock everything for us.
1047 * if it succeeds, locks are still valid and locked.
1048 * also, if it is OK, then the anon's page is on the queues.
1049 * if the page is on loan from a uvm_object, then anonget will
1050 * lock that object for us if it does not fail.
1051 */
1052
1053 error = uvmfault_anonget(&ufi, amap, anon);
1054 switch (error) {
1055 case 0:
1056 break;
1057
1058 case ERESTART:
1059 goto ReFault;
1060
1061 case EAGAIN:
1062 tsleep(&lbolt, PVM, "fltagain1", 0);
1063 goto ReFault;
1064
1065 default:
1066 return error;
1067 }
1068
1069 /*
1070 * uobj is non null if the page is on loan from an object (i.e. uobj)
1071 */
1072
1073 uobj = anon->u.an_page->uobject; /* locked by anonget if !NULL */
1074
1075 /* locked: maps(read), amap, anon, uobj(if one) */
1076
1077 /*
1078 * special handling for loaned pages
1079 */
1080
1081 if (anon->u.an_page->loan_count) {
1082
1083 if (!cow_now) {
1084
1085 /*
1086 * for read faults on loaned pages we just cap the
1087 * protection at read-only.
1088 */
1089
1090 enter_prot = enter_prot & ~VM_PROT_WRITE;
1091
1092 } else {
1093 /*
1094 * note that we can't allow writes into a loaned page!
1095 *
1096 * if we have a write fault on a loaned page in an
1097 * anon then we need to look at the anon's ref count.
1098 * if it is greater than one then we are going to do
1099 * a normal copy-on-write fault into a new anon (this
1100 * is not a problem). however, if the reference count
1101 * is one (a case where we would normally allow a
1102 * write directly to the page) then we need to kill
1103 * the loan before we continue.
1104 */
1105
1106 /* >1 case is already ok */
1107 if (anon->an_ref == 1) {
1108
1109 /* get new un-owned replacement page */
1110 pg = uvm_pagealloc(NULL, 0, NULL, 0);
1111 if (pg == NULL) {
1112 uvmfault_unlockall(&ufi, amap, uobj,
1113 anon);
1114 uvm_wait("flt_noram2");
1115 goto ReFault;
1116 }
1117
1118 /*
1119 * copy data, kill loan, and drop uobj lock
1120 * (if any)
1121 */
1122 /* copy old -> new */
1123 uvm_pagecopy(anon->u.an_page, pg);
1124
1125 /* force reload */
1126 pmap_page_protect(anon->u.an_page,
1127 VM_PROT_NONE);
1128 uvm_lock_pageq(); /* KILL loan */
1129
1130 anon->u.an_page->uanon = NULL;
1131 /* in case we owned */
1132 anon->u.an_page->pqflags &= ~PQ_ANON;
1133
1134 if (uobj) {
1135 /* if we were receiver of loan */
1136 anon->u.an_page->loan_count--;
1137 } else {
1138 /*
1139 * we were the lender (A->K); need
1140 * to remove the page from pageq's.
1141 */
1142 uvm_pagedequeue(anon->u.an_page);
1143 }
1144
1145 uvm_pageactivate(pg);
1146 uvm_unlock_pageq();
1147 if (uobj) {
1148 simple_unlock(&uobj->vmobjlock);
1149 uobj = NULL;
1150 }
1151
1152 /* install new page in anon */
1153 anon->u.an_page = pg;
1154 pg->uanon = anon;
1155 pg->pqflags |= PQ_ANON;
1156 pg->flags &= ~(PG_BUSY|PG_FAKE);
1157 UVM_PAGE_OWN(pg, NULL);
1158
1159 /* done! */
1160 } /* ref == 1 */
1161 } /* write fault */
1162 } /* loan count */
1163
1164 /*
1165 * if we are case 1B then we will need to allocate a new blank
1166 * anon to transfer the data into. note that we have a lock
1167 * on anon, so no one can busy or release the page until we are done.
1168 * also note that the ref count can't drop to zero here because
1169 * it is > 1 and we are only dropping one ref.
1170 *
1171 * in the (hopefully very rare) case that we are out of RAM we
1172 * will unlock, wait for more RAM, and refault.
1173 *
1174 * if we are out of anon VM we kill the process (XXX: could wait?).
1175 */
1176
1177 if (cow_now && anon->an_ref > 1) {
1178
1179 UVMHIST_LOG(maphist, " case 1B: COW fault",0,0,0,0);
1180 uvmexp.flt_acow++;
1181 oanon = anon; /* oanon = old, locked anon */
1182 anon = uvm_analloc();
1183 if (anon) {
1184 /* new anon is locked! */
1185 pg = uvm_pagealloc(NULL, 0, anon, 0);
1186 }
1187
1188 /* check for out of RAM */
1189 if (anon == NULL || pg == NULL) {
1190 if (anon) {
1191 anon->an_ref--;
1192 simple_unlock(&anon->an_lock);
1193 uvm_anfree(anon);
1194 }
1195 uvmfault_unlockall(&ufi, amap, uobj, oanon);
1196 if (anon == NULL || !uvm_reclaimable()) {
1197 UVMHIST_LOG(maphist,
1198 "<- failed. out of VM",0,0,0,0);
1199 uvmexp.fltnoanon++;
1200 return ENOMEM;
1201 }
1202
1203 uvmexp.fltnoram++;
1204 uvm_wait("flt_noram3"); /* out of RAM, wait for more */
1205 goto ReFault;
1206 }
1207
1208 /* got all resources, replace anon with nanon */
1209 uvm_pagecopy(oanon->u.an_page, pg);
1210 uvm_lock_pageq();
1211 uvm_pageactivate(pg);
1212 pg->flags &= ~(PG_BUSY|PG_FAKE);
1213 uvm_unlock_pageq();
1214 UVM_PAGE_OWN(pg, NULL);
1215 amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start,
1216 anon, TRUE);
1217
1218 /* deref: can not drop to zero here by defn! */
1219 oanon->an_ref--;
1220
1221 /*
1222 * note: oanon is still locked, as is the new anon. we
1223 * need to check for this later when we unlock oanon; if
1224 * oanon != anon, we'll have to unlock anon, too.
1225 */
1226
1227 } else {
1228
1229 uvmexp.flt_anon++;
1230 oanon = anon; /* old, locked anon is same as anon */
1231 pg = anon->u.an_page;
1232 if (anon->an_ref > 1) /* disallow writes to ref > 1 anons */
1233 enter_prot = enter_prot & ~VM_PROT_WRITE;
1234
1235 }
1236
1237 /* locked: maps(read), amap, oanon, anon (if different from oanon) */
1238
1239 /*
1240 * now map the page in.
1241 */
1242
1243 UVMHIST_LOG(maphist, " MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x",
1244 ufi.orig_map->pmap, ufi.orig_rvaddr, pg, 0);
1245 if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),
1246 enter_prot, access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0))
1247 != 0) {
1248
1249 /*
1250 * No need to undo what we did; we can simply think of
1251 * this as the pmap throwing away the mapping information.
1252 *
1253 * We do, however, have to go through the ReFault path,
1254 * as the map may change while we're asleep.
1255 */
1256
1257 if (anon != oanon)
1258 simple_unlock(&anon->an_lock);
1259 uvmfault_unlockall(&ufi, amap, uobj, oanon);
1260 if (!uvm_reclaimable()) {
1261 UVMHIST_LOG(maphist,
1262 "<- failed. out of VM",0,0,0,0);
1263 /* XXX instrumentation */
1264 return ENOMEM;
1265 }
1266 /* XXX instrumentation */
1267 uvm_wait("flt_pmfail1");
1268 goto ReFault;
1269 }
1270
1271 /*
1272 * ... update the page queues.
1273 */
1274
1275 uvm_lock_pageq();
1276 if (wire_fault) {
1277 uvm_pagewire(pg);
1278
1279 /*
1280 * since the now-wired page cannot be paged out,
1281 * release its swap resources for others to use.
1282 * since an anon with no swap cannot be PG_CLEAN,
1283 * clear its clean flag now.
1284 */
1285
1286 pg->flags &= ~(PG_CLEAN);
1287 uvm_anon_dropswap(anon);
1288 } else {
1289 uvm_pageactivate(pg);
1290 }
1291 uvm_unlock_pageq();
1292
1293 /*
1294 * done case 1! finish up by unlocking everything and returning success
1295 */
1296
1297 if (anon != oanon)
1298 simple_unlock(&anon->an_lock);
1299 uvmfault_unlockall(&ufi, amap, uobj, oanon);
1300 pmap_update(ufi.orig_map->pmap);
1301 return 0;
1302
1303 Case2:
1304 /*
1305 * handle case 2: faulting on backing object or zero fill
1306 */
1307
1308 /*
1309 * locked:
1310 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
1311 */
1312
1313 /*
1314 * note that uobjpage can not be PGO_DONTCARE at this point. we now
1315 * set uobjpage to PGO_DONTCARE if we are doing a zero fill. if we
1316 * have a backing object, check and see if we are going to promote
1317 * the data up to an anon during the fault.
1318 */
1319
1320 if (uobj == NULL) {
1321 uobjpage = PGO_DONTCARE;
1322 promote = TRUE; /* always need anon here */
1323 } else {
1324 KASSERT(uobjpage != PGO_DONTCARE);
1325 promote = cow_now && UVM_ET_ISCOPYONWRITE(ufi.entry);
1326 }
1327 UVMHIST_LOG(maphist, " case 2 fault: promote=%d, zfill=%d",
1328 promote, (uobj == NULL), 0,0);
1329
1330 /*
1331 * if uobjpage is not null then we do not need to do I/O to get the
1332 * uobjpage.
1333 *
1334 * if uobjpage is null, then we need to unlock and ask the pager to
1335 * get the data for us. once we have the data, we need to reverify
1336 * the state the world. we are currently not holding any resources.
1337 */
1338
1339 if (uobjpage) {
1340 /* update rusage counters */
1341 curproc->p_stats->p_ru.ru_minflt++;
1342 } else {
1343 /* update rusage counters */
1344 curproc->p_stats->p_ru.ru_majflt++;
1345
1346 /* locked: maps(read), amap(if there), uobj */
1347 uvmfault_unlockall(&ufi, amap, NULL, NULL);
1348 /* locked: uobj */
1349
1350 uvmexp.fltget++;
1351 gotpages = 1;
1352 uoff = (ufi.orig_rvaddr - ufi.entry->start) + ufi.entry->offset;
1353 error = uobj->pgops->pgo_get(uobj, uoff, &uobjpage, &gotpages,
1354 0, access_type & MASK(ufi.entry), ufi.entry->advice,
1355 PGO_SYNCIO);
1356 /* locked: uobjpage(if no error) */
1357
1358 /*
1359 * recover from I/O
1360 */
1361
1362 if (error) {
1363 if (error == EAGAIN) {
1364 UVMHIST_LOG(maphist,
1365 " pgo_get says TRY AGAIN!",0,0,0,0);
1366 tsleep(&lbolt, PVM, "fltagain2", 0);
1367 goto ReFault;
1368 }
1369
1370 UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)",
1371 error, 0,0,0);
1372 return error;
1373 }
1374
1375 /* locked: uobjpage */
1376
1377 uvm_lock_pageq();
1378 uvm_pageactivate(uobjpage);
1379 uvm_unlock_pageq();
1380
1381 /*
1382 * re-verify the state of the world by first trying to relock
1383 * the maps. always relock the object.
1384 */
1385
1386 locked = uvmfault_relock(&ufi);
1387 if (locked && amap)
1388 amap_lock(amap);
1389 simple_lock(&uobj->vmobjlock);
1390
1391 /* locked(locked): maps(read), amap(if !null), uobj, uobjpage */
1392 /* locked(!locked): uobj, uobjpage */
1393
1394 /*
1395 * verify that the page has not be released and re-verify
1396 * that amap slot is still free. if there is a problem,
1397 * we unlock and clean up.
1398 */
1399
1400 if ((uobjpage->flags & PG_RELEASED) != 0 ||
1401 (locked && amap &&
1402 amap_lookup(&ufi.entry->aref,
1403 ufi.orig_rvaddr - ufi.entry->start))) {
1404 if (locked)
1405 uvmfault_unlockall(&ufi, amap, NULL, NULL);
1406 locked = FALSE;
1407 }
1408
1409 /*
1410 * didn't get the lock? release the page and retry.
1411 */
1412
1413 if (locked == FALSE) {
1414 UVMHIST_LOG(maphist,
1415 " wasn't able to relock after fault: retry",
1416 0,0,0,0);
1417 if (uobjpage->flags & PG_WANTED)
1418 wakeup(uobjpage);
1419 if (uobjpage->flags & PG_RELEASED) {
1420 uvmexp.fltpgrele++;
1421 uvm_pagefree(uobjpage);
1422 goto ReFault;
1423 }
1424 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1425 UVM_PAGE_OWN(uobjpage, NULL);
1426 simple_unlock(&uobj->vmobjlock);
1427 goto ReFault;
1428 }
1429
1430 /*
1431 * we have the data in uobjpage which is busy and
1432 * not released. we are holding object lock (so the page
1433 * can't be released on us).
1434 */
1435
1436 /* locked: maps(read), amap(if !null), uobj, uobjpage */
1437 }
1438
1439 /*
1440 * locked:
1441 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
1442 */
1443
1444 /*
1445 * notes:
1446 * - at this point uobjpage can not be NULL
1447 * - at this point uobjpage can not be PG_RELEASED (since we checked
1448 * for it above)
1449 * - at this point uobjpage could be PG_WANTED (handle later)
1450 */
1451
1452 if (promote == FALSE) {
1453
1454 /*
1455 * we are not promoting. if the mapping is COW ensure that we
1456 * don't give more access than we should (e.g. when doing a read
1457 * fault on a COPYONWRITE mapping we want to map the COW page in
1458 * R/O even though the entry protection could be R/W).
1459 *
1460 * set "pg" to the page we want to map in (uobjpage, usually)
1461 */
1462
1463 /* no anon in this case. */
1464 anon = NULL;
1465
1466 uvmexp.flt_obj++;
1467 if (UVM_ET_ISCOPYONWRITE(ufi.entry))
1468 enter_prot &= ~VM_PROT_WRITE;
1469 pg = uobjpage; /* map in the actual object */
1470
1471 /* assert(uobjpage != PGO_DONTCARE) */
1472
1473 /*
1474 * we are faulting directly on the page. be careful
1475 * about writing to loaned pages...
1476 */
1477
1478 if (uobjpage->loan_count) {
1479 if (!cow_now) {
1480 /* read fault: cap the protection at readonly */
1481 /* cap! */
1482 enter_prot = enter_prot & ~VM_PROT_WRITE;
1483 } else {
1484 /* write fault: must break the loan here */
1485
1486 pg = uvm_loanbreak(uobjpage);
1487 if (pg == NULL) {
1488
1489 /*
1490 * drop ownership of page, it can't
1491 * be released
1492 */
1493
1494 if (uobjpage->flags & PG_WANTED)
1495 wakeup(uobjpage);
1496 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1497 UVM_PAGE_OWN(uobjpage, NULL);
1498
1499 uvmfault_unlockall(&ufi, amap, uobj,
1500 NULL);
1501 UVMHIST_LOG(maphist,
1502 " out of RAM breaking loan, waiting",
1503 0,0,0,0);
1504 uvmexp.fltnoram++;
1505 uvm_wait("flt_noram4");
1506 goto ReFault;
1507 }
1508 uobjpage = pg;
1509 }
1510 }
1511 } else {
1512
1513 /*
1514 * if we are going to promote the data to an anon we
1515 * allocate a blank anon here and plug it into our amap.
1516 */
1517 #if DIAGNOSTIC
1518 if (amap == NULL)
1519 panic("uvm_fault: want to promote data, but no anon");
1520 #endif
1521
1522 anon = uvm_analloc();
1523 if (anon) {
1524
1525 /*
1526 * The new anon is locked.
1527 *
1528 * In `Fill in data...' below, if
1529 * uobjpage == PGO_DONTCARE, we want
1530 * a zero'd, dirty page, so have
1531 * uvm_pagealloc() do that for us.
1532 */
1533
1534 pg = uvm_pagealloc(NULL, 0, anon,
1535 (uobjpage == PGO_DONTCARE) ? UVM_PGA_ZERO : 0);
1536 }
1537
1538 /*
1539 * out of memory resources?
1540 */
1541
1542 if (anon == NULL || pg == NULL) {
1543 if (anon != NULL) {
1544 anon->an_ref--;
1545 simple_unlock(&anon->an_lock);
1546 uvm_anfree(anon);
1547 }
1548
1549 /*
1550 * arg! must unbusy our page and fail or sleep.
1551 */
1552
1553 if (uobjpage != PGO_DONTCARE) {
1554 if (uobjpage->flags & PG_WANTED)
1555 /* still holding object lock */
1556 wakeup(uobjpage);
1557
1558 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1559 UVM_PAGE_OWN(uobjpage, NULL);
1560 }
1561
1562 /* unlock and fail ... */
1563 uvmfault_unlockall(&ufi, amap, uobj, NULL);
1564 if (anon == NULL || !uvm_reclaimable()) {
1565 UVMHIST_LOG(maphist, " promote: out of VM",
1566 0,0,0,0);
1567 uvmexp.fltnoanon++;
1568 return ENOMEM;
1569 }
1570
1571 UVMHIST_LOG(maphist, " out of RAM, waiting for more",
1572 0,0,0,0);
1573 uvmexp.fltnoram++;
1574 uvm_wait("flt_noram5");
1575 goto ReFault;
1576 }
1577
1578 /*
1579 * fill in the data
1580 */
1581
1582 if (uobjpage != PGO_DONTCARE) {
1583 uvmexp.flt_prcopy++;
1584 /* copy page [pg now dirty] */
1585 uvm_pagecopy(uobjpage, pg);
1586
1587 /*
1588 * promote to shared amap? make sure all sharing
1589 * procs see it
1590 */
1591
1592 if ((amap_flags(amap) & AMAP_SHARED) != 0) {
1593 pmap_page_protect(uobjpage, VM_PROT_NONE);
1594 /*
1595 * XXX: PAGE MIGHT BE WIRED!
1596 */
1597 }
1598
1599 /*
1600 * dispose of uobjpage. it can't be PG_RELEASED
1601 * since we still hold the object lock.
1602 * drop handle to uobj as well.
1603 */
1604
1605 if (uobjpage->flags & PG_WANTED)
1606 /* still have the obj lock */
1607 wakeup(uobjpage);
1608 uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
1609 UVM_PAGE_OWN(uobjpage, NULL);
1610 simple_unlock(&uobj->vmobjlock);
1611 uobj = NULL;
1612
1613 UVMHIST_LOG(maphist,
1614 " promote uobjpage 0x%x to anon/page 0x%x/0x%x",
1615 uobjpage, anon, pg, 0);
1616
1617 } else {
1618 uvmexp.flt_przero++;
1619
1620 /*
1621 * Page is zero'd and marked dirty by uvm_pagealloc()
1622 * above.
1623 */
1624
1625 UVMHIST_LOG(maphist," zero fill anon/page 0x%x/0%x",
1626 anon, pg, 0, 0);
1627 }
1628 amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start,
1629 anon, FALSE);
1630 }
1631
1632 /*
1633 * locked:
1634 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj),
1635 * anon(if !null), pg(if anon)
1636 *
1637 * note: pg is either the uobjpage or the new page in the new anon
1638 */
1639
1640 /*
1641 * all resources are present. we can now map it in and free our
1642 * resources.
1643 */
1644
1645 UVMHIST_LOG(maphist,
1646 " MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d",
1647 ufi.orig_map->pmap, ufi.orig_rvaddr, pg, promote);
1648 KASSERT((access_type & VM_PROT_WRITE) == 0 ||
1649 (pg->flags & PG_RDONLY) == 0);
1650 if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),
1651 pg->flags & PG_RDONLY ? enter_prot & ~VM_PROT_WRITE : enter_prot,
1652 access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0)) != 0) {
1653
1654 /*
1655 * No need to undo what we did; we can simply think of
1656 * this as the pmap throwing away the mapping information.
1657 *
1658 * We do, however, have to go through the ReFault path,
1659 * as the map may change while we're asleep.
1660 */
1661
1662 if (pg->flags & PG_WANTED)
1663 wakeup(pg);
1664
1665 /*
1666 * note that pg can't be PG_RELEASED since we did not drop
1667 * the object lock since the last time we checked.
1668 */
1669
1670 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
1671 UVM_PAGE_OWN(pg, NULL);
1672 uvmfault_unlockall(&ufi, amap, uobj, anon);
1673 if (!uvm_reclaimable()) {
1674 UVMHIST_LOG(maphist,
1675 "<- failed. out of VM",0,0,0,0);
1676 /* XXX instrumentation */
1677 return ENOMEM;
1678 }
1679 /* XXX instrumentation */
1680 uvm_wait("flt_pmfail2");
1681 goto ReFault;
1682 }
1683
1684 uvm_lock_pageq();
1685 if (wire_fault) {
1686 uvm_pagewire(pg);
1687 if (pg->pqflags & PQ_AOBJ) {
1688
1689 /*
1690 * since the now-wired page cannot be paged out,
1691 * release its swap resources for others to use.
1692 * since an aobj page with no swap cannot be PG_CLEAN,
1693 * clear its clean flag now.
1694 */
1695
1696 pg->flags &= ~(PG_CLEAN);
1697 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
1698 }
1699 } else {
1700 uvm_pageactivate(pg);
1701 }
1702 uvm_unlock_pageq();
1703 if (pg->flags & PG_WANTED)
1704 wakeup(pg);
1705
1706 /*
1707 * note that pg can't be PG_RELEASED since we did not drop the object
1708 * lock since the last time we checked.
1709 */
1710
1711 pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
1712 UVM_PAGE_OWN(pg, NULL);
1713 uvmfault_unlockall(&ufi, amap, uobj, anon);
1714 pmap_update(ufi.orig_map->pmap);
1715 UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
1716 return 0;
1717 }
1718
1719 /*
1720 * uvm_fault_wire: wire down a range of virtual addresses in a map.
1721 *
1722 * => map may be read-locked by caller, but MUST NOT be write-locked.
1723 * => if map is read-locked, any operations which may cause map to
1724 * be write-locked in uvm_fault() must be taken care of by
1725 * the caller. See uvm_map_pageable().
1726 */
1727
1728 int
1729 uvm_fault_wire(map, start, end, fault_type, access_type)
1730 struct vm_map *map;
1731 vaddr_t start, end;
1732 vm_fault_t fault_type;
1733 vm_prot_t access_type;
1734 {
1735 vaddr_t va;
1736 int error;
1737
1738 /*
1739 * now fault it in a page at a time. if the fault fails then we have
1740 * to undo what we have done. note that in uvm_fault VM_PROT_NONE
1741 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
1742 */
1743
1744 /*
1745 * XXX work around overflowing a vaddr_t. this prevents us from
1746 * wiring the last page in the address space, though.
1747 */
1748 if (start > end) {
1749 return EFAULT;
1750 }
1751
1752 for (va = start ; va < end ; va += PAGE_SIZE) {
1753 error = uvm_fault(map, va, fault_type, access_type);
1754 if (error) {
1755 if (va != start) {
1756 uvm_fault_unwire(map, start, va);
1757 }
1758 return error;
1759 }
1760 }
1761 return 0;
1762 }
1763
1764 /*
1765 * uvm_fault_unwire(): unwire range of virtual space.
1766 */
1767
1768 void
1769 uvm_fault_unwire(map, start, end)
1770 struct vm_map *map;
1771 vaddr_t start, end;
1772 {
1773 vm_map_lock_read(map);
1774 uvm_fault_unwire_locked(map, start, end);
1775 vm_map_unlock_read(map);
1776 }
1777
1778 /*
1779 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
1780 *
1781 * => map must be at least read-locked.
1782 */
1783
1784 void
1785 uvm_fault_unwire_locked(map, start, end)
1786 struct vm_map *map;
1787 vaddr_t start, end;
1788 {
1789 struct vm_map_entry *entry;
1790 pmap_t pmap = vm_map_pmap(map);
1791 vaddr_t va;
1792 paddr_t pa;
1793 struct vm_page *pg;
1794
1795 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0);
1796
1797 /*
1798 * we assume that the area we are unwiring has actually been wired
1799 * in the first place. this means that we should be able to extract
1800 * the PAs from the pmap. we also lock out the page daemon so that
1801 * we can call uvm_pageunwire.
1802 */
1803
1804 uvm_lock_pageq();
1805
1806 /*
1807 * find the beginning map entry for the region.
1808 */
1809
1810 KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map));
1811 if (uvm_map_lookup_entry(map, start, &entry) == FALSE)
1812 panic("uvm_fault_unwire_locked: address not in map");
1813
1814 for (va = start; va < end; va += PAGE_SIZE) {
1815 if (pmap_extract(pmap, va, &pa) == FALSE)
1816 continue;
1817
1818 /*
1819 * find the map entry for the current address.
1820 */
1821
1822 KASSERT(va >= entry->start);
1823 while (va >= entry->end) {
1824 KASSERT(entry->next != &map->header &&
1825 entry->next->start <= entry->end);
1826 entry = entry->next;
1827 }
1828
1829 /*
1830 * if the entry is no longer wired, tell the pmap.
1831 */
1832
1833 if (VM_MAPENT_ISWIRED(entry) == 0)
1834 pmap_unwire(pmap, va);
1835
1836 pg = PHYS_TO_VM_PAGE(pa);
1837 if (pg)
1838 uvm_pageunwire(pg);
1839 }
1840
1841 uvm_unlock_pageq();
1842 }
1843