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