uvm_fault.c
来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 1,896 行 · 第 1/4 页
C
1,896 行
if (uobj->pgops->pgo_releasepg(uobjpage,NULL)) /* unlock if still alive */ simple_unlock(&uobj->vmobjlock); goto ReFault; } uvm_lock_pageq(); /* make sure it is in queues */ uvm_pageactivate(uobjpage); uvm_unlock_pageq(); uobjpage->flags &= ~(PG_BUSY|PG_WANTED); UVM_PAGE_OWN(uobjpage, NULL); simple_unlock(&uobj->vmobjlock); goto ReFault; } /* * we have the data in uobjpage which is PG_BUSY and * !PG_RELEASED. we are holding object lock (so the page * can't be released on us). */ /* locked: maps(read), amap(if !null), uobj, uobjpage */ } /* * locked: * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj) */ /* * notes: * - at this point uobjpage can not be NULL * - at this point uobjpage can not be PG_RELEASED (since we checked * for it above) * - at this point uobjpage could be PG_WANTED (handle later) */ if (promote == FALSE) { /* * we are not promoting. if the mapping is COW ensure that we * don't give more access than we should (e.g. when doing a read * fault on a COPYONWRITE mapping we want to map the COW page in * R/O even though the entry protection could be R/W). * * set "pg" to the page we want to map in (uobjpage, usually) */ uvmexp.flt_obj++; if (UVM_ET_ISCOPYONWRITE(ufi.entry)) enter_prot &= ~VM_PROT_WRITE; pg = uobjpage; /* map in the actual object */ /* assert(uobjpage != PGO_DONTCARE) */ /* * we are faulting directly on the page. be careful * about writing to loaned pages... */ if (uobjpage->loan_count) { if ((access_type & VM_PROT_WRITE) == 0) { /* read fault: cap the protection at readonly */ /* cap! */ enter_prot = enter_prot & ~VM_PROT_WRITE; } else { /* write fault: must break the loan here */ /* alloc new un-owned page */ pg = uvm_pagealloc(NULL, 0, NULL, 0); if (pg == NULL) { /* * drop ownership of page, it can't * be released */ if (uobjpage->flags & PG_WANTED) wakeup(uobjpage); uobjpage->flags &= ~(PG_BUSY|PG_WANTED); UVM_PAGE_OWN(uobjpage, NULL); uvm_lock_pageq(); /* activate: we will need it later */ uvm_pageactivate(uobjpage); uvm_unlock_pageq(); uvmfault_unlockall(&ufi, amap, uobj, NULL); UVMHIST_LOG(maphist, " out of RAM breaking loan, waiting", 0,0,0,0); uvmexp.fltnoram++; uvm_wait("flt_noram4"); goto ReFault; } /* * copy the data from the old page to the new * one and clear the fake/clean flags on the * new page (keep it busy). force a reload * of the old page by clearing it from all * pmaps. then lock the page queues to * rename the pages. */ uvm_pagecopy(uobjpage, pg); /* old -> new */ pg->flags &= ~(PG_FAKE|PG_CLEAN); pmap_page_protect(uobjpage, VM_PROT_NONE); if (uobjpage->flags & PG_WANTED) wakeup(uobjpage); /* uobj still locked */ uobjpage->flags &= ~(PG_WANTED|PG_BUSY); UVM_PAGE_OWN(uobjpage, NULL); uvm_lock_pageq(); offset = uobjpage->offset; /* remove old page */ uvm_pagerealloc(uobjpage, NULL, 0); /* * at this point we have absolutely no * control over uobjpage */ /* install new page */ uvm_pagerealloc(pg, uobj, offset); uvm_unlock_pageq(); /* * done! loan is broken and "pg" is * PG_BUSY. it can now replace uobjpage. */ uobjpage = pg; } /* write fault case */ } /* if loan_count */ } else { /* * if we are going to promote the data to an anon we * allocate a blank anon here and plug it into our amap. */#if DIAGNOSTIC if (amap == NULL) panic("uvm_fault: want to promote data, but no anon");#endif anon = uvm_analloc(); if (anon) { /* * In `Fill in data...' below, if * uobjpage == PGO_DONTCARE, we want * a zero'd, dirty page, so have * uvm_pagealloc() do that for us. */ pg = uvm_pagealloc(NULL, 0, anon, (uobjpage == PGO_DONTCARE) ? UVM_PGA_ZERO : 0); } /* * out of memory resources? */ if (anon == NULL || pg == NULL) { /* * arg! must unbusy our page and fail or sleep. */ if (uobjpage != PGO_DONTCARE) { if (uobjpage->flags & PG_WANTED) /* still holding object lock */ wakeup(uobjpage); uvm_lock_pageq(); uvm_pageactivate(uobjpage); uvm_unlock_pageq(); uobjpage->flags &= ~(PG_BUSY|PG_WANTED); UVM_PAGE_OWN(uobjpage, NULL); } /* unlock and fail ... */ uvmfault_unlockall(&ufi, amap, uobj, NULL); KASSERT(uvmexp.swpgonly <= uvmexp.swpages); if (anon == NULL || uvmexp.swpgonly == uvmexp.swpages) { UVMHIST_LOG(maphist, " promote: out of VM", 0,0,0,0); uvmexp.fltnoanon++; return (KERN_RESOURCE_SHORTAGE); } UVMHIST_LOG(maphist, " out of RAM, waiting for more", 0,0,0,0); anon->an_ref--; uvm_anfree(anon); uvmexp.fltnoram++; uvm_wait("flt_noram5"); goto ReFault; } /* * fill in the data */ if (uobjpage != PGO_DONTCARE) { uvmexp.flt_prcopy++; /* copy page [pg now dirty] */ uvm_pagecopy(uobjpage, pg); /* * promote to shared amap? make sure all sharing * procs see it */ if ((amap_flags(amap) & AMAP_SHARED) != 0) { pmap_page_protect(uobjpage, VM_PROT_NONE); } /* * dispose of uobjpage. it can't be PG_RELEASED * since we still hold the object lock. * drop handle to uobj as well. */ if (uobjpage->flags & PG_WANTED) /* still have the obj lock */ wakeup(uobjpage); uobjpage->flags &= ~(PG_BUSY|PG_WANTED); UVM_PAGE_OWN(uobjpage, NULL); uvm_lock_pageq(); uvm_pageactivate(uobjpage); uvm_unlock_pageq(); simple_unlock(&uobj->vmobjlock); uobj = NULL; UVMHIST_LOG(maphist, " promote uobjpage 0x%x to anon/page 0x%x/0x%x", uobjpage, anon, pg, 0); } else { uvmexp.flt_przero++; /* * Page is zero'd and marked dirty by uvm_pagealloc() * above. */ UVMHIST_LOG(maphist," zero fill anon/page 0x%x/0%x", anon, pg, 0, 0); } amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start, anon, 0); } /* * locked: * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj) * * note: pg is either the uobjpage or the new page in the new anon */ /* * all resources are present. we can now map it in and free our * resources. */ UVMHIST_LOG(maphist, " MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d", ufi.orig_map->pmap, ufi.orig_rvaddr, pg, promote); KASSERT(access_type == VM_PROT_READ || (pg->flags & PG_RDONLY) == 0); if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg), pg->flags & PG_RDONLY ? VM_PROT_READ : enter_prot, access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0)) != KERN_SUCCESS) { /* * No need to undo what we did; we can simply think of * this as the pmap throwing away the mapping information. * * We do, however, have to go through the ReFault path, * as the map may change while we're asleep. */ if (pg->flags & PG_WANTED) wakeup(pg); /* lock still held */ /* * note that pg can't be PG_RELEASED since we did not drop * the object lock since the last time we checked. */ pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED); UVM_PAGE_OWN(pg, NULL); uvmfault_unlockall(&ufi, amap, uobj, NULL); KASSERT(uvmexp.swpgonly <= uvmexp.swpages); if (uvmexp.swpgonly == uvmexp.swpages) { UVMHIST_LOG(maphist, "<- failed. out of VM",0,0,0,0); /* XXX instrumentation */ return (KERN_RESOURCE_SHORTAGE); } /* XXX instrumentation */ uvm_wait("flt_pmfail2"); goto ReFault; } uvm_lock_pageq(); if (fault_type == VM_FAULT_WIRE) { uvm_pagewire(pg); if (pg->pqflags & PQ_AOBJ) { /* * since the now-wired page cannot be paged out, * release its swap resources for others to use. * since an aobj page with no swap cannot be PG_CLEAN, * clear its clean flag now. */ pg->flags &= ~(PG_CLEAN); uao_dropswap(uobj, pg->offset >> PAGE_SHIFT); } } else { /* activate it */ uvm_pageactivate(pg); } uvm_unlock_pageq(); if (pg->flags & PG_WANTED) wakeup(pg); /* lock still held */ /* * note that pg can't be PG_RELEASED since we did not drop the object * lock since the last time we checked. */ pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED); UVM_PAGE_OWN(pg, NULL); uvmfault_unlockall(&ufi, amap, uobj, NULL); UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0); return (KERN_SUCCESS);}/* * uvm_fault_wire: wire down a range of virtual addresses in a map. * * => map may be read-locked by caller, but MUST NOT be write-locked. * => if map is read-locked, any operations which may cause map to * be write-locked in uvm_fault() must be taken care of by * the caller. See uvm_map_pageable(). */intuvm_fault_wire(map, start, end, access_type) vm_map_t map; vaddr_t start, end; vm_prot_t access_type;{ vaddr_t va; pmap_t pmap; int rv; pmap = vm_map_pmap(map); /* * now fault it in a page at a time. if the fault fails then we have * to undo what we have done. note that in uvm_fault VM_PROT_NONE * is replaced with the max protection if fault_type is VM_FAULT_WIRE. */ for (va = start ; va < end ; va += PAGE_SIZE) { rv = uvm_fault(map, va, VM_FAULT_WIRE, access_type); if (rv) { if (va != start) { uvm_fault_unwire(map, start, va); } return (rv); } } return (KERN_SUCCESS);}/* * uvm_fault_unwire(): unwire range of virtual space. */voiduvm_fault_unwire(map, start, end) vm_map_t map; vaddr_t start, end;{ vm_map_lock_read(map); uvm_fault_unwire_locked(map, start, end); vm_map_unlock_read(map);}/* * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire(). * * => map must be at least read-locked. */voiduvm_fault_unwire_locked(map, start, end) vm_map_t map; vaddr_t start, end;{ vm_map_entry_t entry; pmap_t pmap = vm_map_pmap(map); vaddr_t va; paddr_t pa; struct vm_page *pg; KASSERT((map->flags & VM_MAP_INTRSAFE) == 0); /* * we assume that the area we are unwiring has actually been wired * in the first place. this means that we should be able to extract * the PAs from the pmap. we also lock out the page daemon so that * we can call uvm_pageunwire. */ uvm_lock_pageq(); /* * find the beginning map entry for the region. */#ifdef DIAGNOSTIC if (start < vm_map_min(map) || end > vm_map_max(map)) panic("uvm_fault_unwire_locked: address out of range");#endif if (uvm_map_lookup_entry(map, start, &entry) == FALSE) panic("uvm_fault_unwire_locked: address not in map"); for (va = start; va < end ; va += PAGE_SIZE) { if (pmap_extract(pmap, va, &pa) == FALSE) panic("uvm_fault_unwire_locked: unwiring " "non-wired memory"); /* * make sure the current entry is for the address we're * dealing with. if not, grab the next entry. */#ifdef DIAGNOSTIC if (va < entry->start) panic("uvm_fault_unwire_locked: hole 1");#endif if (va >= entry->end) {#ifdef DIAGNOSTIC if (entry->next == &map->header || entry->next->start > entry->end) panic("uvm_fault_unwire_locked: hole 2");#endif entry = entry->next; } /* * if the entry is no longer wired, tell the pmap. */ if (VM_MAPENT_ISWIRED(entry) == 0) pmap_unwire(pmap, va); pg = PHYS_TO_VM_PAGE(pa); if (pg) uvm_pageunwire(pg); } uvm_unlock_pageq();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?