145748Smckusick /* 247663Smckusick * Copyright (c) 1991 Regents of the University of California. 345748Smckusick * All rights reserved. 445748Smckusick * 547663Smckusick * This code is derived from software contributed to Berkeley by 647663Smckusick * The Mach Operating System project at Carnegie-Mellon University. 747663Smckusick * 847663Smckusick * %sccs.include.redist.c% 947663Smckusick * 10*50911Smckusick * @(#)vm_fault.c 7.8 (Berkeley) 08/28/91 1147663Smckusick * 1247663Smckusick * 1347663Smckusick * Copyright (c) 1987, 1990 Carnegie-Mellon University. 1447663Smckusick * All rights reserved. 1547663Smckusick * 1647592Smckusick * Authors: Avadis Tevanian, Jr., Michael Wayne Young 1747592Smckusick * 1847592Smckusick * Permission to use, copy, modify and distribute this software and 1947592Smckusick * its documentation is hereby granted, provided that both the copyright 2047592Smckusick * notice and this permission notice appear in all copies of the 2147592Smckusick * software, derivative works or modified versions, and any portions 2247592Smckusick * thereof, and that both notices appear in supporting documentation. 2347592Smckusick * 2447592Smckusick * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 2547592Smckusick * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 2647592Smckusick * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 2747592Smckusick * 2847663Smckusick * Carnegie Mellon requests users of this software to return to 2945748Smckusick * 3047663Smckusick * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 3147663Smckusick * School of Computer Science 3247663Smckusick * Carnegie Mellon University 3347663Smckusick * Pittsburgh PA 15213-3890 3447662Smckusick * 3547663Smckusick * any improvements or extensions that they make and grant Carnegie the 3647663Smckusick * rights to redistribute these changes. 3745748Smckusick */ 3845748Smckusick 3945748Smckusick /* 4045748Smckusick * Page fault handling module. 4145748Smckusick */ 4245748Smckusick 4345748Smckusick #include "param.h" 4445748Smckusick 4548386Skarels #include "vm.h" 4648386Skarels #include "vm_page.h" 4748386Skarels #include "vm_pageout.h" 4848386Skarels 4945748Smckusick /* 5045748Smckusick * vm_fault: 5145748Smckusick * 5245748Smckusick * Handle a page fault occuring at the given address, 5345748Smckusick * requiring the given permissions, in the map specified. 5445748Smckusick * If successful, the page is inserted into the 5545748Smckusick * associated physical map. 5645748Smckusick * 5745748Smckusick * NOTE: the given address should be truncated to the 5845748Smckusick * proper page address. 5945748Smckusick * 6045748Smckusick * KERN_SUCCESS is returned if the page fault is handled; otherwise, 6145748Smckusick * a standard error specifying why the fault is fatal is returned. 6245748Smckusick * 6345748Smckusick * 6445748Smckusick * The map in question must be referenced, and remains so. 6545748Smckusick * Caller may hold no locks. 6645748Smckusick */ 6745748Smckusick vm_fault(map, vaddr, fault_type, change_wiring) 6845748Smckusick vm_map_t map; 6945748Smckusick vm_offset_t vaddr; 7045748Smckusick vm_prot_t fault_type; 7145748Smckusick boolean_t change_wiring; 7245748Smckusick { 7345748Smckusick vm_object_t first_object; 7445748Smckusick vm_offset_t first_offset; 7545748Smckusick vm_map_entry_t entry; 7645748Smckusick register vm_object_t object; 7745748Smckusick register vm_offset_t offset; 7845748Smckusick register vm_page_t m; 7945748Smckusick vm_page_t first_m; 8045748Smckusick vm_prot_t prot; 8145748Smckusick int result; 8245748Smckusick boolean_t wired; 8345748Smckusick boolean_t su; 8445748Smckusick boolean_t lookup_still_valid; 8545748Smckusick boolean_t page_exists; 8645748Smckusick vm_page_t old_m; 8745748Smckusick vm_object_t next_object; 8845748Smckusick 89*50911Smckusick cnt.v_vm_faults++; /* needs lock XXX */ 9045748Smckusick /* 9145748Smckusick * Recovery actions 9245748Smckusick */ 9345748Smckusick #define FREE_PAGE(m) { \ 9445748Smckusick PAGE_WAKEUP(m); \ 9545748Smckusick vm_page_lock_queues(); \ 9645748Smckusick vm_page_free(m); \ 9745748Smckusick vm_page_unlock_queues(); \ 9845748Smckusick } 9945748Smckusick 10045748Smckusick #define RELEASE_PAGE(m) { \ 10145748Smckusick PAGE_WAKEUP(m); \ 10245748Smckusick vm_page_lock_queues(); \ 10345748Smckusick vm_page_activate(m); \ 10445748Smckusick vm_page_unlock_queues(); \ 10545748Smckusick } 10645748Smckusick 10745748Smckusick #define UNLOCK_MAP { \ 10845748Smckusick if (lookup_still_valid) { \ 10945748Smckusick vm_map_lookup_done(map, entry); \ 11045748Smckusick lookup_still_valid = FALSE; \ 11145748Smckusick } \ 11245748Smckusick } 11345748Smckusick 11445748Smckusick #define UNLOCK_THINGS { \ 11545748Smckusick object->paging_in_progress--; \ 11645748Smckusick vm_object_unlock(object); \ 11745748Smckusick if (object != first_object) { \ 11845748Smckusick vm_object_lock(first_object); \ 11945748Smckusick FREE_PAGE(first_m); \ 12045748Smckusick first_object->paging_in_progress--; \ 12145748Smckusick vm_object_unlock(first_object); \ 12245748Smckusick } \ 12345748Smckusick UNLOCK_MAP; \ 12445748Smckusick } 12545748Smckusick 12645748Smckusick #define UNLOCK_AND_DEALLOCATE { \ 12745748Smckusick UNLOCK_THINGS; \ 12845748Smckusick vm_object_deallocate(first_object); \ 12945748Smckusick } 13045748Smckusick 13145748Smckusick RetryFault: ; 13245748Smckusick 13345748Smckusick /* 13445748Smckusick * Find the backing store object and offset into 13545748Smckusick * it to begin the search. 13645748Smckusick */ 13745748Smckusick 13845748Smckusick if ((result = vm_map_lookup(&map, vaddr, fault_type, &entry, 13945748Smckusick &first_object, &first_offset, 14045748Smckusick &prot, &wired, &su)) != KERN_SUCCESS) { 14145748Smckusick return(result); 14245748Smckusick } 14345748Smckusick lookup_still_valid = TRUE; 14445748Smckusick 14545748Smckusick if (wired) 14645748Smckusick fault_type = prot; 14745748Smckusick 14848386Skarels first_m = NULL; 14945748Smckusick 15045748Smckusick /* 15145748Smckusick * Make a reference to this object to 15245748Smckusick * prevent its disposal while we are messing with 15345748Smckusick * it. Once we have the reference, the map is free 15445748Smckusick * to be diddled. Since objects reference their 15545748Smckusick * shadows (and copies), they will stay around as well. 15645748Smckusick */ 15745748Smckusick 15845748Smckusick vm_object_lock(first_object); 15945748Smckusick 16045748Smckusick first_object->ref_count++; 16145748Smckusick first_object->paging_in_progress++; 16245748Smckusick 16345748Smckusick /* 16445748Smckusick * INVARIANTS (through entire routine): 16545748Smckusick * 16645748Smckusick * 1) At all times, we must either have the object 16745748Smckusick * lock or a busy page in some object to prevent 16845748Smckusick * some other thread from trying to bring in 16945748Smckusick * the same page. 17045748Smckusick * 17145748Smckusick * Note that we cannot hold any locks during the 17245748Smckusick * pager access or when waiting for memory, so 17345748Smckusick * we use a busy page then. 17445748Smckusick * 17545748Smckusick * Note also that we aren't as concerned about 17645748Smckusick * more than one thead attempting to pager_data_unlock 17745748Smckusick * the same page at once, so we don't hold the page 17845748Smckusick * as busy then, but do record the highest unlock 17945748Smckusick * value so far. [Unlock requests may also be delivered 18045748Smckusick * out of order.] 18145748Smckusick * 18245748Smckusick * 2) Once we have a busy page, we must remove it from 18345748Smckusick * the pageout queues, so that the pageout daemon 18445748Smckusick * will not grab it away. 18545748Smckusick * 18645748Smckusick * 3) To prevent another thread from racing us down the 18745748Smckusick * shadow chain and entering a new page in the top 18845748Smckusick * object before we do, we must keep a busy page in 18945748Smckusick * the top object while following the shadow chain. 19045748Smckusick * 19145748Smckusick * 4) We must increment paging_in_progress on any object 19245748Smckusick * for which we have a busy page, to prevent 19345748Smckusick * vm_object_collapse from removing the busy page 19445748Smckusick * without our noticing. 19545748Smckusick */ 19645748Smckusick 19745748Smckusick /* 19845748Smckusick * Search for the page at object/offset. 19945748Smckusick */ 20045748Smckusick 20145748Smckusick object = first_object; 20245748Smckusick offset = first_offset; 20345748Smckusick 20445748Smckusick /* 20545748Smckusick * See whether this page is resident 20645748Smckusick */ 20745748Smckusick 20845748Smckusick while (TRUE) { 20945748Smckusick m = vm_page_lookup(object, offset); 21048386Skarels if (m != NULL) { 21145748Smckusick /* 21245748Smckusick * If the page is being brought in, 21345748Smckusick * wait for it and then retry. 21445748Smckusick */ 21545748Smckusick if (m->busy) { 21645748Smckusick #ifdef DOTHREADS 21745748Smckusick int wait_result; 21845748Smckusick 21945748Smckusick PAGE_ASSERT_WAIT(m, !change_wiring); 22045748Smckusick UNLOCK_THINGS; 22145748Smckusick thread_block(); 22245748Smckusick wait_result = current_thread()->wait_result; 22345748Smckusick vm_object_deallocate(first_object); 22445748Smckusick if (wait_result != THREAD_AWAKENED) 22545748Smckusick return(KERN_SUCCESS); 22645748Smckusick goto RetryFault; 22745748Smckusick #else 22845748Smckusick PAGE_ASSERT_WAIT(m, !change_wiring); 22945748Smckusick UNLOCK_THINGS; 23045748Smckusick thread_block(); 23145748Smckusick vm_object_deallocate(first_object); 23245748Smckusick goto RetryFault; 23345748Smckusick #endif 23445748Smckusick } 23545748Smckusick 23645748Smckusick if (m->absent) 23745748Smckusick panic("vm_fault: absent"); 23845748Smckusick 23945748Smckusick /* 24045748Smckusick * If the desired access to this page has 24145748Smckusick * been locked out, request that it be unlocked. 24245748Smckusick */ 24345748Smckusick 24445748Smckusick if (fault_type & m->page_lock) { 24545748Smckusick #ifdef DOTHREADS 24645748Smckusick int wait_result; 24745748Smckusick 24845748Smckusick if ((fault_type & m->unlock_request) != fault_type) 24945748Smckusick panic("vm_fault: pager_data_unlock"); 25045748Smckusick 25145748Smckusick PAGE_ASSERT_WAIT(m, !change_wiring); 25245748Smckusick UNLOCK_THINGS; 25345748Smckusick thread_block(); 25445748Smckusick wait_result = current_thread()->wait_result; 25545748Smckusick vm_object_deallocate(first_object); 25645748Smckusick if (wait_result != THREAD_AWAKENED) 25745748Smckusick return(KERN_SUCCESS); 25845748Smckusick goto RetryFault; 25945748Smckusick #else 26045748Smckusick if ((fault_type & m->unlock_request) != fault_type) 26145748Smckusick panic("vm_fault: pager_data_unlock"); 26245748Smckusick 26345748Smckusick PAGE_ASSERT_WAIT(m, !change_wiring); 26445748Smckusick UNLOCK_THINGS; 26545748Smckusick thread_block(); 26645748Smckusick vm_object_deallocate(first_object); 26745748Smckusick goto RetryFault; 26845748Smckusick #endif 26945748Smckusick } 27045748Smckusick 27145748Smckusick /* 27245748Smckusick * Remove the page from the pageout daemon's 27345748Smckusick * reach while we play with it. 27445748Smckusick */ 27545748Smckusick 27645748Smckusick vm_page_lock_queues(); 27745748Smckusick if (m->inactive) { 27845748Smckusick queue_remove(&vm_page_queue_inactive, m, 27945748Smckusick vm_page_t, pageq); 28045748Smckusick m->inactive = FALSE; 281*50911Smckusick cnt.v_inactive_count--; 282*50911Smckusick cnt.v_reactivated++; 28345748Smckusick } 28445748Smckusick 28545748Smckusick if (m->active) { 28645748Smckusick queue_remove(&vm_page_queue_active, m, 28745748Smckusick vm_page_t, pageq); 28845748Smckusick m->active = FALSE; 289*50911Smckusick cnt.v_active_count--; 29045748Smckusick } 29145748Smckusick vm_page_unlock_queues(); 29245748Smckusick 29345748Smckusick /* 29445748Smckusick * Mark page busy for other threads. 29545748Smckusick */ 29645748Smckusick m->busy = TRUE; 29745748Smckusick m->absent = FALSE; 29845748Smckusick break; 29945748Smckusick } 30045748Smckusick 30148386Skarels if (((object->pager != NULL) && 30245748Smckusick (!change_wiring || wired)) 30345748Smckusick || (object == first_object)) { 30445748Smckusick 30545748Smckusick /* 30645748Smckusick * Allocate a new page for this object/offset 30745748Smckusick * pair. 30845748Smckusick */ 30945748Smckusick 31045748Smckusick m = vm_page_alloc(object, offset); 31145748Smckusick 31248386Skarels if (m == NULL) { 31345748Smckusick UNLOCK_AND_DEALLOCATE; 31445748Smckusick VM_WAIT; 31545748Smckusick goto RetryFault; 31645748Smckusick } 31745748Smckusick } 31845748Smckusick 31948386Skarels if ((object->pager != NULL) && 32045748Smckusick (!change_wiring || wired)) { 32145748Smckusick int rv; 32245748Smckusick 32345748Smckusick /* 32445748Smckusick * Now that we have a busy page, we can 32545748Smckusick * release the object lock. 32645748Smckusick */ 32745748Smckusick vm_object_unlock(object); 32845748Smckusick 32945748Smckusick /* 33045748Smckusick * Call the pager to retrieve the data, if any, 33145748Smckusick * after releasing the lock on the map. 33245748Smckusick */ 33345748Smckusick UNLOCK_MAP; 33445748Smckusick 33545748Smckusick rv = vm_pager_get(object->pager, m, TRUE); 33645748Smckusick if (rv == VM_PAGER_OK) { 33745748Smckusick /* 33845748Smckusick * Found the page. 33945748Smckusick * Leave it busy while we play with it. 34045748Smckusick */ 34145748Smckusick vm_object_lock(object); 34245748Smckusick 34345748Smckusick /* 34445748Smckusick * Relookup in case pager changed page. 34545748Smckusick * Pager is responsible for disposition 34645748Smckusick * of old page if moved. 34745748Smckusick */ 34845748Smckusick m = vm_page_lookup(object, offset); 34945748Smckusick 350*50911Smckusick cnt.v_pageins++; 35145748Smckusick m->fake = FALSE; 35245748Smckusick pmap_clear_modify(VM_PAGE_TO_PHYS(m)); 35345748Smckusick break; 35445748Smckusick } 35545748Smckusick 35645748Smckusick /* 35745748Smckusick * Remove the bogus page (which does not 35845748Smckusick * exist at this object/offset); before 35945748Smckusick * doing so, we must get back our object 36045748Smckusick * lock to preserve our invariant. 36145748Smckusick * 36245748Smckusick * Also wake up any other thread that may want 36345748Smckusick * to bring in this page. 36445748Smckusick * 36545748Smckusick * If this is the top-level object, we must 36645748Smckusick * leave the busy page to prevent another 36745748Smckusick * thread from rushing past us, and inserting 36845748Smckusick * the page in that object at the same time 36945748Smckusick * that we are. 37045748Smckusick */ 37145748Smckusick 37245748Smckusick vm_object_lock(object); 37345748Smckusick /* 37445748Smckusick * Data outside the range of the pager; an error 37545748Smckusick */ 37645748Smckusick if (rv == VM_PAGER_BAD) { 37745748Smckusick FREE_PAGE(m); 37845748Smckusick UNLOCK_AND_DEALLOCATE; 37945748Smckusick return(KERN_PROTECTION_FAILURE); /* XXX */ 38045748Smckusick } 38145748Smckusick if (object != first_object) { 38245748Smckusick FREE_PAGE(m); 38345748Smckusick /* 38445748Smckusick * XXX - we cannot just fall out at this 38545748Smckusick * point, m has been freed and is invalid! 38645748Smckusick */ 38745748Smckusick } 38845748Smckusick } 38945748Smckusick 39045748Smckusick /* 39145748Smckusick * We get here if the object has no pager (or unwiring) 39245748Smckusick * or the pager doesn't have the page. 39345748Smckusick */ 39445748Smckusick if (object == first_object) 39545748Smckusick first_m = m; 39645748Smckusick 39745748Smckusick /* 39845748Smckusick * Move on to the next object. Lock the next 39945748Smckusick * object before unlocking the current one. 40045748Smckusick */ 40145748Smckusick 40245748Smckusick offset += object->shadow_offset; 40345748Smckusick next_object = object->shadow; 40448386Skarels if (next_object == NULL) { 40545748Smckusick /* 40645748Smckusick * If there's no object left, fill the page 40745748Smckusick * in the top object with zeros. 40845748Smckusick */ 40945748Smckusick if (object != first_object) { 41045748Smckusick object->paging_in_progress--; 41145748Smckusick vm_object_unlock(object); 41245748Smckusick 41345748Smckusick object = first_object; 41445748Smckusick offset = first_offset; 41545748Smckusick m = first_m; 41645748Smckusick vm_object_lock(object); 41745748Smckusick } 41848386Skarels first_m = NULL; 41945748Smckusick 42045748Smckusick vm_page_zero_fill(m); 421*50911Smckusick cnt.v_zfod++; 42245748Smckusick m->fake = FALSE; 42345748Smckusick m->absent = FALSE; 42445748Smckusick break; 42545748Smckusick } 42645748Smckusick else { 42745748Smckusick vm_object_lock(next_object); 42845748Smckusick if (object != first_object) 42945748Smckusick object->paging_in_progress--; 43045748Smckusick vm_object_unlock(object); 43145748Smckusick object = next_object; 43245748Smckusick object->paging_in_progress++; 43345748Smckusick } 43445748Smckusick } 43545748Smckusick 43645748Smckusick if (m->absent || m->active || m->inactive || !m->busy) 43745748Smckusick panic("vm_fault: absent or active or inactive or not busy after main loop"); 43845748Smckusick 43945748Smckusick /* 44045748Smckusick * PAGE HAS BEEN FOUND. 44145748Smckusick * [Loop invariant still holds -- the object lock 44245748Smckusick * is held.] 44345748Smckusick */ 44445748Smckusick 44545748Smckusick old_m = m; /* save page that would be copied */ 44645748Smckusick 44745748Smckusick /* 44845748Smckusick * If the page is being written, but isn't 44945748Smckusick * already owned by the top-level object, 45045748Smckusick * we have to copy it into a new page owned 45145748Smckusick * by the top-level object. 45245748Smckusick */ 45345748Smckusick 45445748Smckusick if (object != first_object) { 45545748Smckusick /* 45645748Smckusick * We only really need to copy if we 45745748Smckusick * want to write it. 45845748Smckusick */ 45945748Smckusick 46045748Smckusick if (fault_type & VM_PROT_WRITE) { 46145748Smckusick 46245748Smckusick /* 46345748Smckusick * If we try to collapse first_object at this 46445748Smckusick * point, we may deadlock when we try to get 46545748Smckusick * the lock on an intermediate object (since we 46645748Smckusick * have the bottom object locked). We can't 46745748Smckusick * unlock the bottom object, because the page 46845748Smckusick * we found may move (by collapse) if we do. 46945748Smckusick * 47045748Smckusick * Instead, we first copy the page. Then, when 47145748Smckusick * we have no more use for the bottom object, 47245748Smckusick * we unlock it and try to collapse. 47345748Smckusick * 47445748Smckusick * Note that we copy the page even if we didn't 47545748Smckusick * need to... that's the breaks. 47645748Smckusick */ 47745748Smckusick 47845748Smckusick /* 47945748Smckusick * We already have an empty page in 48045748Smckusick * first_object - use it. 48145748Smckusick */ 48245748Smckusick 48345748Smckusick vm_page_copy(m, first_m); 48445748Smckusick first_m->fake = FALSE; 48545748Smckusick first_m->absent = FALSE; 48645748Smckusick 48745748Smckusick /* 48845748Smckusick * If another map is truly sharing this 48945748Smckusick * page with us, we have to flush all 49045748Smckusick * uses of the original page, since we 49145748Smckusick * can't distinguish those which want the 49245748Smckusick * original from those which need the 49345748Smckusick * new copy. 49449288Shibler * 49549288Shibler * XXX If we know that only one map has 49649288Shibler * access to this page, then we could 49749288Shibler * avoid the pmap_page_protect() call. 49845748Smckusick */ 49945748Smckusick 50045748Smckusick vm_page_lock_queues(); 50149288Shibler vm_page_deactivate(m); 50249288Shibler pmap_page_protect(VM_PAGE_TO_PHYS(m), VM_PROT_NONE); 50345748Smckusick vm_page_unlock_queues(); 50445748Smckusick 50545748Smckusick /* 50645748Smckusick * We no longer need the old page or object. 50745748Smckusick */ 50845748Smckusick PAGE_WAKEUP(m); 50945748Smckusick object->paging_in_progress--; 51045748Smckusick vm_object_unlock(object); 51145748Smckusick 51245748Smckusick /* 51345748Smckusick * Only use the new page below... 51445748Smckusick */ 51545748Smckusick 516*50911Smckusick cnt.v_cow_faults++; 51745748Smckusick m = first_m; 51845748Smckusick object = first_object; 51945748Smckusick offset = first_offset; 52045748Smckusick 52145748Smckusick /* 52245748Smckusick * Now that we've gotten the copy out of the 52345748Smckusick * way, let's try to collapse the top object. 52445748Smckusick */ 52545748Smckusick vm_object_lock(object); 52645748Smckusick /* 52745748Smckusick * But we have to play ugly games with 52845748Smckusick * paging_in_progress to do that... 52945748Smckusick */ 53045748Smckusick object->paging_in_progress--; 53145748Smckusick vm_object_collapse(object); 53245748Smckusick object->paging_in_progress++; 53345748Smckusick } 53445748Smckusick else { 53545748Smckusick prot &= (~VM_PROT_WRITE); 53645748Smckusick m->copy_on_write = TRUE; 53745748Smckusick } 53845748Smckusick } 53945748Smckusick 54045748Smckusick if (m->active || m->inactive) 54145748Smckusick panic("vm_fault: active or inactive before copy object handling"); 54245748Smckusick 54345748Smckusick /* 54445748Smckusick * If the page is being written, but hasn't been 54545748Smckusick * copied to the copy-object, we have to copy it there. 54645748Smckusick */ 54745748Smckusick RetryCopy: 54848386Skarels if (first_object->copy != NULL) { 54948386Skarels vm_object_t copy_object = first_object->copy; 55048386Skarels vm_offset_t copy_offset; 55148386Skarels vm_page_t copy_m; 55245748Smckusick 55345748Smckusick /* 55445748Smckusick * We only need to copy if we want to write it. 55545748Smckusick */ 55645748Smckusick if ((fault_type & VM_PROT_WRITE) == 0) { 55745748Smckusick prot &= ~VM_PROT_WRITE; 55845748Smckusick m->copy_on_write = TRUE; 55945748Smckusick } 56045748Smckusick else { 56145748Smckusick /* 56245748Smckusick * Try to get the lock on the copy_object. 56345748Smckusick */ 56445748Smckusick if (!vm_object_lock_try(copy_object)) { 56545748Smckusick vm_object_unlock(object); 56645748Smckusick /* should spin a bit here... */ 56745748Smckusick vm_object_lock(object); 56845748Smckusick goto RetryCopy; 56945748Smckusick } 57045748Smckusick 57145748Smckusick /* 57245748Smckusick * Make another reference to the copy-object, 57345748Smckusick * to keep it from disappearing during the 57445748Smckusick * copy. 57545748Smckusick */ 57645748Smckusick copy_object->ref_count++; 57745748Smckusick 57845748Smckusick /* 57945748Smckusick * Does the page exist in the copy? 58045748Smckusick */ 58145748Smckusick copy_offset = first_offset 58245748Smckusick - copy_object->shadow_offset; 58345748Smckusick copy_m = vm_page_lookup(copy_object, copy_offset); 58448386Skarels if (page_exists = (copy_m != NULL)) { 58545748Smckusick if (copy_m->busy) { 58645748Smckusick #ifdef DOTHREADS 58745748Smckusick int wait_result; 58845748Smckusick 58945748Smckusick /* 59045748Smckusick * If the page is being brought 59145748Smckusick * in, wait for it and then retry. 59245748Smckusick */ 59345748Smckusick PAGE_ASSERT_WAIT(copy_m, !change_wiring); 59445748Smckusick RELEASE_PAGE(m); 59545748Smckusick copy_object->ref_count--; 59645748Smckusick vm_object_unlock(copy_object); 59745748Smckusick UNLOCK_THINGS; 59845748Smckusick thread_block(); 59945748Smckusick wait_result = current_thread()->wait_result; 60045748Smckusick vm_object_deallocate(first_object); 60145748Smckusick if (wait_result != THREAD_AWAKENED) 60245748Smckusick return(KERN_SUCCESS); 60345748Smckusick goto RetryFault; 60445748Smckusick #else 60545748Smckusick /* 60645748Smckusick * If the page is being brought 60745748Smckusick * in, wait for it and then retry. 60845748Smckusick */ 60945748Smckusick PAGE_ASSERT_WAIT(copy_m, !change_wiring); 61045748Smckusick RELEASE_PAGE(m); 61145748Smckusick copy_object->ref_count--; 61245748Smckusick vm_object_unlock(copy_object); 61345748Smckusick UNLOCK_THINGS; 61445748Smckusick thread_block(); 61545748Smckusick vm_object_deallocate(first_object); 61645748Smckusick goto RetryFault; 61745748Smckusick #endif 61845748Smckusick } 61945748Smckusick } 62045748Smckusick 62145748Smckusick /* 62245748Smckusick * If the page is not in memory (in the object) 62345748Smckusick * and the object has a pager, we have to check 62445748Smckusick * if the pager has the data in secondary 62545748Smckusick * storage. 62645748Smckusick */ 62745748Smckusick if (!page_exists) { 62845748Smckusick 62945748Smckusick /* 63045748Smckusick * If we don't allocate a (blank) page 63145748Smckusick * here... another thread could try 63245748Smckusick * to page it in, allocate a page, and 63345748Smckusick * then block on the busy page in its 63445748Smckusick * shadow (first_object). Then we'd 63545748Smckusick * trip over the busy page after we 63645748Smckusick * found that the copy_object's pager 63745748Smckusick * doesn't have the page... 63845748Smckusick */ 63945748Smckusick copy_m = vm_page_alloc(copy_object, 64045748Smckusick copy_offset); 64148386Skarels if (copy_m == NULL) { 64245748Smckusick /* 64345748Smckusick * Wait for a page, then retry. 64445748Smckusick */ 64545748Smckusick RELEASE_PAGE(m); 64645748Smckusick copy_object->ref_count--; 64745748Smckusick vm_object_unlock(copy_object); 64845748Smckusick UNLOCK_AND_DEALLOCATE; 64945748Smckusick VM_WAIT; 65045748Smckusick goto RetryFault; 65145748Smckusick } 65245748Smckusick 65348386Skarels if (copy_object->pager != NULL) { 65445748Smckusick vm_object_unlock(object); 65545748Smckusick vm_object_unlock(copy_object); 65645748Smckusick UNLOCK_MAP; 65745748Smckusick 65845748Smckusick page_exists = vm_pager_has_page( 65945748Smckusick copy_object->pager, 66045748Smckusick (copy_offset + copy_object->paging_offset)); 66145748Smckusick 66245748Smckusick vm_object_lock(copy_object); 66345748Smckusick 66445748Smckusick /* 66545748Smckusick * Since the map is unlocked, someone 66645748Smckusick * else could have copied this object 66745748Smckusick * and put a different copy_object 66845748Smckusick * between the two. Or, the last 66945748Smckusick * reference to the copy-object (other 67045748Smckusick * than the one we have) may have 67145748Smckusick * disappeared - if that has happened, 67245748Smckusick * we don't need to make the copy. 67345748Smckusick */ 67445748Smckusick if (copy_object->shadow != object || 67545748Smckusick copy_object->ref_count == 1) { 67645748Smckusick /* 67745748Smckusick * Gaah... start over! 67845748Smckusick */ 67945748Smckusick FREE_PAGE(copy_m); 68045748Smckusick vm_object_unlock(copy_object); 68145748Smckusick vm_object_deallocate(copy_object); 68245748Smckusick /* may block */ 68345748Smckusick vm_object_lock(object); 68445748Smckusick goto RetryCopy; 68545748Smckusick } 68645748Smckusick vm_object_lock(object); 68745748Smckusick 68845748Smckusick if (page_exists) { 68945748Smckusick /* 69045748Smckusick * We didn't need the page 69145748Smckusick */ 69245748Smckusick FREE_PAGE(copy_m); 69345748Smckusick } 69445748Smckusick } 69545748Smckusick } 69645748Smckusick if (!page_exists) { 69745748Smckusick /* 69845748Smckusick * Must copy page into copy-object. 69945748Smckusick */ 70045748Smckusick vm_page_copy(m, copy_m); 70145748Smckusick copy_m->fake = FALSE; 70245748Smckusick copy_m->absent = FALSE; 70345748Smckusick 70445748Smckusick /* 70545748Smckusick * Things to remember: 70645748Smckusick * 1. The copied page must be marked 'dirty' 70745748Smckusick * so it will be paged out to the copy 70845748Smckusick * object. 70945748Smckusick * 2. If the old page was in use by any users 71045748Smckusick * of the copy-object, it must be removed 71145748Smckusick * from all pmaps. (We can't know which 71245748Smckusick * pmaps use it.) 71345748Smckusick */ 71445748Smckusick vm_page_lock_queues(); 71549288Shibler pmap_page_protect(VM_PAGE_TO_PHYS(old_m), 71649288Shibler VM_PROT_NONE); 71745748Smckusick copy_m->clean = FALSE; 71845748Smckusick vm_page_activate(copy_m); /* XXX */ 71945748Smckusick vm_page_unlock_queues(); 72045748Smckusick 72145748Smckusick PAGE_WAKEUP(copy_m); 72245748Smckusick } 72345748Smckusick /* 72445748Smckusick * The reference count on copy_object must be 72545748Smckusick * at least 2: one for our extra reference, 72645748Smckusick * and at least one from the outside world 72745748Smckusick * (we checked that when we last locked 72845748Smckusick * copy_object). 72945748Smckusick */ 73045748Smckusick copy_object->ref_count--; 73145748Smckusick vm_object_unlock(copy_object); 73245748Smckusick m->copy_on_write = FALSE; 73345748Smckusick } 73445748Smckusick } 73545748Smckusick 73645748Smckusick if (m->active || m->inactive) 73745748Smckusick panic("vm_fault: active or inactive before retrying lookup"); 73845748Smckusick 73945748Smckusick /* 74045748Smckusick * We must verify that the maps have not changed 74145748Smckusick * since our last lookup. 74245748Smckusick */ 74345748Smckusick 74445748Smckusick if (!lookup_still_valid) { 74545748Smckusick vm_object_t retry_object; 74645748Smckusick vm_offset_t retry_offset; 74745748Smckusick vm_prot_t retry_prot; 74845748Smckusick 74945748Smckusick /* 75045748Smckusick * Since map entries may be pageable, make sure we can 75145748Smckusick * take a page fault on them. 75245748Smckusick */ 75345748Smckusick vm_object_unlock(object); 75445748Smckusick 75545748Smckusick /* 75645748Smckusick * To avoid trying to write_lock the map while another 75745748Smckusick * thread has it read_locked (in vm_map_pageable), we 75845748Smckusick * do not try for write permission. If the page is 75945748Smckusick * still writable, we will get write permission. If it 76045748Smckusick * is not, or has been marked needs_copy, we enter the 76145748Smckusick * mapping without write permission, and will merely 76245748Smckusick * take another fault. 76345748Smckusick */ 76445748Smckusick result = vm_map_lookup(&map, vaddr, 76545748Smckusick fault_type & ~VM_PROT_WRITE, &entry, 76645748Smckusick &retry_object, &retry_offset, &retry_prot, 76745748Smckusick &wired, &su); 76845748Smckusick 76945748Smckusick vm_object_lock(object); 77045748Smckusick 77145748Smckusick /* 77245748Smckusick * If we don't need the page any longer, put it on the 77345748Smckusick * active list (the easiest thing to do here). If no 77445748Smckusick * one needs it, pageout will grab it eventually. 77545748Smckusick */ 77645748Smckusick 77745748Smckusick if (result != KERN_SUCCESS) { 77845748Smckusick RELEASE_PAGE(m); 77945748Smckusick UNLOCK_AND_DEALLOCATE; 78045748Smckusick return(result); 78145748Smckusick } 78245748Smckusick 78345748Smckusick lookup_still_valid = TRUE; 78445748Smckusick 78545748Smckusick if ((retry_object != first_object) || 78645748Smckusick (retry_offset != first_offset)) { 78745748Smckusick RELEASE_PAGE(m); 78845748Smckusick UNLOCK_AND_DEALLOCATE; 78945748Smckusick goto RetryFault; 79045748Smckusick } 79145748Smckusick 79245748Smckusick /* 79345748Smckusick * Check whether the protection has changed or the object 79445748Smckusick * has been copied while we left the map unlocked. 79545748Smckusick * Changing from read to write permission is OK - we leave 79645748Smckusick * the page write-protected, and catch the write fault. 79745748Smckusick * Changing from write to read permission means that we 79845748Smckusick * can't mark the page write-enabled after all. 79945748Smckusick */ 80045748Smckusick prot &= retry_prot; 80145748Smckusick if (m->copy_on_write) 80245748Smckusick prot &= ~VM_PROT_WRITE; 80345748Smckusick } 80445748Smckusick 80545748Smckusick /* 80645748Smckusick * (the various bits we're fiddling with here are locked by 80745748Smckusick * the object's lock) 80845748Smckusick */ 80945748Smckusick 81045748Smckusick /* XXX This distorts the meaning of the copy_on_write bit */ 81145748Smckusick 81245748Smckusick if (prot & VM_PROT_WRITE) 81345748Smckusick m->copy_on_write = FALSE; 81445748Smckusick 81545748Smckusick /* 81645748Smckusick * It's critically important that a wired-down page be faulted 81745748Smckusick * only once in each map for which it is wired. 81845748Smckusick */ 81945748Smckusick 82045748Smckusick if (m->active || m->inactive) 82145748Smckusick panic("vm_fault: active or inactive before pmap_enter"); 82245748Smckusick 82345748Smckusick vm_object_unlock(object); 82445748Smckusick 82545748Smckusick /* 82645748Smckusick * Put this page into the physical map. 82745748Smckusick * We had to do the unlock above because pmap_enter 82845748Smckusick * may cause other faults. We don't put the 82945748Smckusick * page back on the active queue until later so 83045748Smckusick * that the page-out daemon won't find us (yet). 83145748Smckusick */ 83245748Smckusick 83345748Smckusick pmap_enter(map->pmap, vaddr, VM_PAGE_TO_PHYS(m), 83445748Smckusick prot & ~(m->page_lock), wired); 83545748Smckusick 83645748Smckusick /* 83745748Smckusick * If the page is not wired down, then put it where the 83845748Smckusick * pageout daemon can find it. 83945748Smckusick */ 84045748Smckusick vm_object_lock(object); 84145748Smckusick vm_page_lock_queues(); 84245748Smckusick if (change_wiring) { 84345748Smckusick if (wired) 84445748Smckusick vm_page_wire(m); 84545748Smckusick else 84645748Smckusick vm_page_unwire(m); 84745748Smckusick } 84845748Smckusick else 84945748Smckusick vm_page_activate(m); 85045748Smckusick vm_page_unlock_queues(); 85145748Smckusick 85245748Smckusick /* 85345748Smckusick * Unlock everything, and return 85445748Smckusick */ 85545748Smckusick 85645748Smckusick PAGE_WAKEUP(m); 85745748Smckusick UNLOCK_AND_DEALLOCATE; 85845748Smckusick 85945748Smckusick return(KERN_SUCCESS); 86045748Smckusick 86145748Smckusick } 86245748Smckusick 86345748Smckusick /* 86445748Smckusick * vm_fault_wire: 86545748Smckusick * 86645748Smckusick * Wire down a range of virtual addresses in a map. 86745748Smckusick */ 86845748Smckusick void vm_fault_wire(map, start, end) 86945748Smckusick vm_map_t map; 87045748Smckusick vm_offset_t start, end; 87145748Smckusick { 87245748Smckusick 87345748Smckusick register vm_offset_t va; 87445748Smckusick register pmap_t pmap; 87545748Smckusick 87645748Smckusick pmap = vm_map_pmap(map); 87745748Smckusick 87845748Smckusick /* 87945748Smckusick * Inform the physical mapping system that the 88045748Smckusick * range of addresses may not fault, so that 88145748Smckusick * page tables and such can be locked down as well. 88245748Smckusick */ 88345748Smckusick 88445748Smckusick pmap_pageable(pmap, start, end, FALSE); 88545748Smckusick 88645748Smckusick /* 88745748Smckusick * We simulate a fault to get the page and enter it 88845748Smckusick * in the physical map. 88945748Smckusick */ 89045748Smckusick 89145748Smckusick for (va = start; va < end; va += PAGE_SIZE) { 89245748Smckusick (void) vm_fault(map, va, VM_PROT_NONE, TRUE); 89345748Smckusick } 89445748Smckusick } 89545748Smckusick 89645748Smckusick 89745748Smckusick /* 89845748Smckusick * vm_fault_unwire: 89945748Smckusick * 90045748Smckusick * Unwire a range of virtual addresses in a map. 90145748Smckusick */ 90245748Smckusick void vm_fault_unwire(map, start, end) 90345748Smckusick vm_map_t map; 90445748Smckusick vm_offset_t start, end; 90545748Smckusick { 90645748Smckusick 90745748Smckusick register vm_offset_t va, pa; 90845748Smckusick register pmap_t pmap; 90945748Smckusick 91045748Smckusick pmap = vm_map_pmap(map); 91145748Smckusick 91245748Smckusick /* 91345748Smckusick * Since the pages are wired down, we must be able to 91445748Smckusick * get their mappings from the physical map system. 91545748Smckusick */ 91645748Smckusick 91745748Smckusick vm_page_lock_queues(); 91845748Smckusick 91945748Smckusick for (va = start; va < end; va += PAGE_SIZE) { 92045748Smckusick pa = pmap_extract(pmap, va); 92145748Smckusick if (pa == (vm_offset_t) 0) { 92245748Smckusick panic("unwire: page not in pmap"); 92345748Smckusick } 92445748Smckusick pmap_change_wiring(pmap, va, FALSE); 92545748Smckusick vm_page_unwire(PHYS_TO_VM_PAGE(pa)); 92645748Smckusick } 92745748Smckusick vm_page_unlock_queues(); 92845748Smckusick 92945748Smckusick /* 93045748Smckusick * Inform the physical mapping system that the range 93145748Smckusick * of addresses may fault, so that page tables and 93245748Smckusick * such may be unwired themselves. 93345748Smckusick */ 93445748Smckusick 93545748Smckusick pmap_pageable(pmap, start, end, TRUE); 93645748Smckusick 93745748Smckusick } 93845748Smckusick 93945748Smckusick /* 94045748Smckusick * Routine: 94145748Smckusick * vm_fault_copy_entry 94245748Smckusick * Function: 94345748Smckusick * Copy all of the pages from a wired-down map entry to another. 94445748Smckusick * 94545748Smckusick * In/out conditions: 94645748Smckusick * The source and destination maps must be locked for write. 94745748Smckusick * The source map entry must be wired down (or be a sharing map 94845748Smckusick * entry corresponding to a main map entry that is wired down). 94945748Smckusick */ 95045748Smckusick 95145748Smckusick void vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry) 95245748Smckusick vm_map_t dst_map; 95345748Smckusick vm_map_t src_map; 95445748Smckusick vm_map_entry_t dst_entry; 95545748Smckusick vm_map_entry_t src_entry; 95645748Smckusick { 95745748Smckusick 95845748Smckusick vm_object_t dst_object; 95945748Smckusick vm_object_t src_object; 96045748Smckusick vm_offset_t dst_offset; 96145748Smckusick vm_offset_t src_offset; 96245748Smckusick vm_prot_t prot; 96345748Smckusick vm_offset_t vaddr; 96445748Smckusick vm_page_t dst_m; 96545748Smckusick vm_page_t src_m; 96645748Smckusick 96745748Smckusick #ifdef lint 96845748Smckusick src_map++; 96945748Smckusick #endif lint 97045748Smckusick 97145748Smckusick src_object = src_entry->object.vm_object; 97245748Smckusick src_offset = src_entry->offset; 97345748Smckusick 97445748Smckusick /* 97545748Smckusick * Create the top-level object for the destination entry. 97645748Smckusick * (Doesn't actually shadow anything - we copy the pages 97745748Smckusick * directly.) 97845748Smckusick */ 97945748Smckusick dst_object = vm_object_allocate( 98045748Smckusick (vm_size_t) (dst_entry->end - dst_entry->start)); 98145748Smckusick 98245748Smckusick dst_entry->object.vm_object = dst_object; 98345748Smckusick dst_entry->offset = 0; 98445748Smckusick 98545748Smckusick prot = dst_entry->max_protection; 98645748Smckusick 98745748Smckusick /* 98845748Smckusick * Loop through all of the pages in the entry's range, copying 98945748Smckusick * each one from the source object (it should be there) to the 99045748Smckusick * destination object. 99145748Smckusick */ 99245748Smckusick for (vaddr = dst_entry->start, dst_offset = 0; 99345748Smckusick vaddr < dst_entry->end; 99445748Smckusick vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) { 99545748Smckusick 99645748Smckusick /* 99745748Smckusick * Allocate a page in the destination object 99845748Smckusick */ 99945748Smckusick vm_object_lock(dst_object); 100045748Smckusick do { 100145748Smckusick dst_m = vm_page_alloc(dst_object, dst_offset); 100248386Skarels if (dst_m == NULL) { 100345748Smckusick vm_object_unlock(dst_object); 100445748Smckusick VM_WAIT; 100545748Smckusick vm_object_lock(dst_object); 100645748Smckusick } 100748386Skarels } while (dst_m == NULL); 100845748Smckusick 100945748Smckusick /* 101045748Smckusick * Find the page in the source object, and copy it in. 101145748Smckusick * (Because the source is wired down, the page will be 101245748Smckusick * in memory.) 101345748Smckusick */ 101445748Smckusick vm_object_lock(src_object); 101545748Smckusick src_m = vm_page_lookup(src_object, dst_offset + src_offset); 101648386Skarels if (src_m == NULL) 101745748Smckusick panic("vm_fault_copy_wired: page missing"); 101845748Smckusick 101945748Smckusick vm_page_copy(src_m, dst_m); 102045748Smckusick 102145748Smckusick /* 102245748Smckusick * Enter it in the pmap... 102345748Smckusick */ 102445748Smckusick vm_object_unlock(src_object); 102545748Smckusick vm_object_unlock(dst_object); 102645748Smckusick 102745748Smckusick pmap_enter(dst_map->pmap, vaddr, VM_PAGE_TO_PHYS(dst_m), 102845748Smckusick prot, FALSE); 102945748Smckusick 103045748Smckusick /* 103145748Smckusick * Mark it no longer busy, and put it on the active list. 103245748Smckusick */ 103345748Smckusick vm_object_lock(dst_object); 103445748Smckusick vm_page_lock_queues(); 103545748Smckusick vm_page_activate(dst_m); 103645748Smckusick vm_page_unlock_queues(); 103745748Smckusick PAGE_WAKEUP(dst_m); 103845748Smckusick vm_object_unlock(dst_object); 103945748Smckusick } 104045748Smckusick 104145748Smckusick } 1042