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