void vm_map_page(struct vm_translation_map *map, unsigned int va, unsigned int pa) { int vpindex = va / PAGE_SIZE; int pgdindex = vpindex / 1024; int pgtindex = vpindex % 1024; unsigned int *pgdir; unsigned int *pgtbl; struct list_node *other_map; unsigned int new_pgt; int old_flags; if (va >= KERNEL_BASE) { // Map into kernel space old_flags = acquire_spinlock_int(&kernel_space_lock); // The page tables for kernel space are shared by all page directories. // Check the first page directory to see if this is present. If not, // allocate a new one and stick it into all page directories. pgdir = (unsigned int*) PA_TO_VA(kernel_map.page_dir); if ((pgdir[pgdindex] & PAGE_PRESENT) == 0) { new_pgt = page_to_pa(vm_allocate_page()) | PAGE_PRESENT; list_for_each(&map_list, other_map, struct list_node) { pgdir = (unsigned int*) PA_TO_VA(((struct vm_translation_map*)other_map)->page_dir); pgdir[pgdindex] = new_pgt; } }
initcode static void DoReserveAreas(DWORD maxPfn) { /* Reserve a few key areas. maxPfn is used because we're only allocating up to it, we don't need to reserve ACPI memory because it's never looked at for allocation */ /* Reserve the first page of memory, used for BIOS data area. */ PageReserveArea(0, PAGE_SIZE); #if 0 /* * Reserve the extended BIOS data area. * "word at BIOS Data Area 40:0E is segment address of EBDA" */ WORD ebdaSeg=*(WORD*)PA_TO_VA(0x40E); if (ebdaSeg) { ebdaAddr=ebdaSeg*16; PageReserveArea(ebdaAddr,PAGE_SIZE); } #endif /* Reserve BIOS code/data areas */ PageReserveArea(0xA0000,0x100000-0xA0000); }
struct vm_translation_map *create_translation_map(void) { struct vm_translation_map *map; int old_flags; map = slab_alloc(&translation_map_slab); map->page_dir = page_to_pa(vm_allocate_page()); old_flags = acquire_spinlock_int(&kernel_space_lock); // Copy kernel page tables into new page directory memcpy((unsigned int*) PA_TO_VA(map->page_dir) + 768, (unsigned int*) PA_TO_VA(kernel_map.page_dir) + 768, 256 * sizeof(unsigned int)); map->asid = next_asid++; map->lock = 0; list_add_tail(&map_list, (struct list_node*) map); release_spinlock_int(&kernel_space_lock, old_flags); return map; }
void destroy_translation_map(struct vm_translation_map *map) { int i; unsigned int *pgdir; int old_flags; old_flags = acquire_spinlock_int(&kernel_space_lock); list_remove_node(map); release_spinlock_int(&kernel_space_lock, old_flags); // Free user space page tables pgdir = (unsigned int*) PA_TO_VA(map->page_dir); for (i = 0; i < 768; i++) { if (pgdir[i] & PAGE_PRESENT) dec_page_ref(pa_to_page(PAGE_ALIGN(pgdir[i]))); } dec_page_ref(pa_to_page(map->page_dir)); slab_free(&translation_map_slab, map); }
// // This is always called with the address space lock held, so the area is // guaranteed not to change. Returns 1 if it sucessfully satisfied the fault, 0 // if it failed for some reason. // static int soft_fault(struct vm_address_space *space, const struct vm_area *area, unsigned int address, int is_store) { int got; unsigned int page_flags; struct vm_page *source_page; struct vm_page *dummy_page = 0; unsigned int cache_offset; struct vm_cache *cache; int old_flags; int is_cow_page = 0; int size_to_read; VM_DEBUG("soft fault va %08x %s\n", address, is_store ? "store" : "load"); // XXX check area protections and fail if this shouldn't be allowed if (is_store && (area->flags & AREA_WRITABLE) == 0) { kprintf("store to read only area %s @%08x\n", area->name, address); return 0; } cache_offset = PAGE_ALIGN(address - area->low_address + area->cache_offset); old_flags = disable_interrupts(); lock_vm_cache(); assert(area->cache); for (cache = area->cache; cache; cache = cache->source) { VM_DEBUG("searching in cache %p\n", cache); source_page = lookup_cache_page(cache, cache_offset); if (source_page) break; if (cache->file && address - area->low_address < area->cache_length) { VM_DEBUG("reading page from file\n"); // Read the page from this cache. source_page = vm_allocate_page(); // Insert the page first so, if a collided fault occurs, it will not // load a different page (the vm cache lock protects the busy bit) source_page->busy = 1; insert_cache_page(cache, cache_offset, source_page); unlock_vm_cache(); restore_interrupts(old_flags); if (area->cache_length - cache_offset < PAGE_SIZE) size_to_read = area->cache_length - cache_offset; else size_to_read = PAGE_SIZE; got = read_file(cache->file, cache_offset, (void*) PA_TO_VA(page_to_pa(source_page)), size_to_read); if (got < 0) { kprintf("failed to read from file\n"); dec_page_ref(source_page); if (dummy_page != 0) { disable_interrupts(); lock_vm_cache(); remove_cache_page(dummy_page); unlock_vm_cache(); restore_interrupts(old_flags); dec_page_ref(dummy_page); } return 0; } // For BSS, clear out data past the end of the file if (size_to_read < PAGE_SIZE) { memset((char*) PA_TO_VA(page_to_pa(source_page)) + size_to_read, 0, PAGE_SIZE - size_to_read); } disable_interrupts(); lock_vm_cache(); source_page->busy = 0; break; } // Otherwise scan the next cache is_cow_page = 1; if (cache == area->cache) { // Insert a dummy page in the top level cache to catch collided faults. dummy_page = vm_allocate_page(); dummy_page->busy = 1; insert_cache_page(cache, cache_offset, dummy_page); } } if (source_page == 0) { assert(dummy_page != 0); VM_DEBUG("source page was not found, use empty page\n"); // No page found, just use the dummy page dummy_page->busy = 0; source_page = dummy_page; } else if (is_cow_page) { // is_cow_page means source_page belongs to another cache. assert(dummy_page != 0); if (is_store) { // The dummy page have the contents of the source page copied into it, // and will be inserted into the top cache (it's not really a dummy page // any more). memcpy((void*) PA_TO_VA(page_to_pa(dummy_page)), (void*) PA_TO_VA(page_to_pa(source_page)), PAGE_SIZE); VM_DEBUG("write copy page va %08x dest pa %08x source pa %08x\n", address, page_to_pa(dummy_page), page_to_pa(source_page)); source_page = dummy_page; dummy_page->busy = 0; } else { // We will map in the read-only page from the source cache. // Remove the dummy page from this cache (we do not insert // the page into this cache, because we don't own it page). remove_cache_page(dummy_page); dec_page_ref(dummy_page); VM_DEBUG("mapping read-only source page va %08x pa %08x\n", address, page_to_pa(source_page)); } } assert(source_page != 0); // Grab a ref because we are going to map this page inc_page_ref(source_page); unlock_vm_cache(); restore_interrupts(old_flags); // XXX busy wait for page to finish loading while (source_page->busy) reschedule(); if (is_store) source_page->dirty = 1; // XXX Locking? // It's possible two threads will fault on the same VA and end up mapping // the page twice. This is fine, because the code above ensures it will // be the same page. page_flags = PAGE_PRESENT; // If the page is clean, we will mark it not writable. This will fault // on the next write, allowing us to update the dirty flag. if ((area->flags & AREA_WRITABLE) != 0 && (source_page->dirty || is_store)) page_flags |= PAGE_WRITABLE; if (area->flags & AREA_EXECUTABLE) page_flags |= PAGE_EXECUTABLE; if (space == &kernel_address_space) page_flags |= PAGE_SUPERVISOR | PAGE_GLOBAL; vm_map_page(space->translation_map, address, page_to_pa(source_page) | page_flags); return 1; }