X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmm_paging.c;h=439585ed8e7a760b0febdc3878d794c9ce51060a;hb=ddd9b6c1a6d0051ba8eae32d25492145c8ca5995;hp=4784e90abc0f05d9514a20dee8f7b10a0e64f613;hpb=f4ba2c6bbcd720a0cd5f9ca9ca0b691656011fa7;p=palacios-OLD.git diff --git a/palacios/src/palacios/vmm_paging.c b/palacios/src/palacios/vmm_paging.c index 4784e90..439585e 100644 --- a/palacios/src/palacios/vmm_paging.c +++ b/palacios/src/palacios/vmm_paging.c @@ -62,8 +62,8 @@ void delete_page_tables_64(pml4e64_t * pml4) { } -int translate_guest_pt_32(struct guest_info * info, addr_t guest_cr3, addr_t vaddr, addr_t * paddr) { - addr_t guest_pde_pa = CR3_TO_PDE32_PA((void *)guest_cr3); +int v3_translate_guest_pt_32(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, addr_t * paddr) { + addr_t guest_pde_pa = CR3_TO_PDE32_PA(guest_cr3); pde32_t * guest_pde = 0; addr_t guest_pte_pa = 0; @@ -74,213 +74,859 @@ int translate_guest_pt_32(struct guest_info * info, addr_t guest_cr3, addr_t vad } switch (pde32_lookup(guest_pde, vaddr, &guest_pte_pa)) { - case PDE32_ENTRY_NOT_PRESENT: + case PT_ENTRY_NOT_PRESENT: *paddr = 0; return -1; - case PDE32_ENTRY_LARGE_PAGE: + case PT_ENTRY_LARGE_PAGE: *paddr = guest_pte_pa; return 0; - case PDE32_ENTRY_PTE32: + case PT_ENTRY_PAGE: { - pte32_t * guest_pte; + pte32_t * guest_pte = NULL; + if (guest_pa_to_host_va(info, guest_pte_pa, (addr_t*)&guest_pte) == -1) { PrintError("Could not get virtual address of Guest PTE32 (PA=%p)\n", (void *)guest_pte_pa); return -1; } - if (pte32_lookup(guest_pte, vaddr, paddr) == -1) { + if (pte32_lookup(guest_pte, vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { return -1; } + + return 0; } } - return 0; + // should never get here + return -1; } -int translate_host_pt_32(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { - pde32_t * host_pde = (pde32_t *)CR3_TO_PDE32_VA((void *)host_cr3); - pte32_t * host_pte = 0; - - switch (pde32_lookup(host_pde, vaddr, (addr_t *)&host_pte)) { - case PDE32_ENTRY_NOT_PRESENT: - *paddr = 0; +int v3_translate_guest_pt_32pae(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, addr_t * paddr) { + addr_t guest_pdpe_pa = CR3_TO_PDPE32PAE_PA(guest_cr3); + pdpe32pae_t * guest_pdpe = 0; + addr_t guest_pde_pa = 0; + + if (guest_pa_to_host_va(info, guest_pdpe_pa, (addr_t*)&guest_pdpe) == -1) { + PrintError("Could not get virtual address of Guest PDPE32PAE (PA=%p)\n", + (void *)guest_pdpe_pa); return -1; - case PDE32_ENTRY_LARGE_PAGE: - *paddr = (addr_t)host_pte; - return 0; - case PDE32_ENTRY_PTE32: - if (pte32_lookup(host_pte, vaddr, paddr) == -1) { + } + + switch (pdpe32pae_lookup(guest_pdpe, vaddr, &guest_pde_pa)) + { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_PAGE: + { + pde32pae_t * guest_pde = NULL; + addr_t guest_pte_pa = 0; + + if (guest_pa_to_host_va(info, guest_pde_pa, (addr_t *)&guest_pde) == -1) { + PrintError("Could not get virtual Address of Guest PDE32PAE (PA=%p)\n", + (void *)guest_pde_pa); + return -1; + } + + switch (pde32pae_lookup(guest_pde, vaddr, &guest_pte_pa)) + { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_LARGE_PAGE: + *paddr = guest_pte_pa; + return 0; + case PT_ENTRY_PAGE: + { + pte32pae_t * guest_pte = NULL; + + if (guest_pa_to_host_va(info, guest_pte_pa, (addr_t *)&guest_pte) == -1) { + PrintError("Could not get virtual Address of Guest PTE32PAE (PA=%p)\n", + (void *)guest_pte_pa); + return -1; + } + + if (pte32pae_lookup(guest_pte, vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { + return -1; + } + + return 0; + } + } + } + default: return -1; } - } - return 0; + // should never get here + return -1; } +int v3_translate_guest_pt_64(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, addr_t * paddr) { + addr_t guest_pml4_pa = CR3_TO_PML4E64_PA(guest_cr3); + pml4e64_t * guest_pmle = 0; + addr_t guest_pdpe_pa = 0; -int translate_host_pt_32pae(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { - pde32_t * host_pde = (pde32_t *)CR3_TO_PDE32_VA((void *)host_cr3); - pte32_t * host_pte = 0; - - switch (pde32_lookup(host_pde, vaddr, (addr_t *)&host_pte)) { - case PDE32_ENTRY_NOT_PRESENT: + if (guest_pa_to_host_va(info, guest_pml4_pa, (addr_t*)&guest_pmle) == -1) { + PrintError("Could not get virtual address of Guest PML4E64 (PA=%p)\n", + (void *)guest_pml4_pa); + return -1; + } + + switch (pml4e64_lookup(guest_pmle, vaddr, &guest_pdpe_pa)) { + case PT_ENTRY_NOT_PRESENT: *paddr = 0; return -1; - case PDE32_ENTRY_LARGE_PAGE: - *paddr = (addr_t)host_pte; - return 0; - case PDE32_ENTRY_PTE32: - if (pte32_lookup(host_pte, vaddr, paddr) == -1) { - return -1; + case PT_ENTRY_PAGE: + { + pdpe64_t * guest_pdp = NULL; + addr_t guest_pde_pa = 0; + + if (guest_pa_to_host_va(info, guest_pdpe_pa, (addr_t *)&guest_pdp) == -1) { + PrintError("Could not get virtual address of Guest PDPE64 (PA=%p)\n", + (void *)guest_pdpe_pa); + return -1; + } + + switch (pdpe64_lookup(guest_pdp, vaddr, &guest_pde_pa)) { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_LARGE_PAGE: + *paddr = 0; + PrintError("1 Gigabyte Pages not supported\n"); + return -1; + case PT_ENTRY_PAGE: + { + pde64_t * guest_pde = NULL; + addr_t guest_pte_pa = 0; + + if (guest_pa_to_host_va(info, guest_pde_pa, (addr_t *)&guest_pde) == -1) { + PrintError("Could not get virtual address of guest PDE64 (PA=%p)\n", + (void *)guest_pde_pa); + return -1; + } + + switch (pde64_lookup(guest_pde, vaddr, &guest_pte_pa)) { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_LARGE_PAGE: + *paddr = guest_pte_pa; + return 0; + case PT_ENTRY_PAGE: + { + pte64_t * guest_pte = NULL; + + if (guest_pa_to_host_va(info, guest_pte_pa, (addr_t *)&guest_pte) == -1) { + PrintError("Could not get virtual address of guest PTE64 (PA=%p)\n", + (void *)guest_pte_pa); + return -1; + } + + if (pte64_lookup(guest_pte, vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { + return -1; + } + + return 0; + } + } + } + } } + default: + return -1; } + // should never get here return -1; } -int translate_host_pt_64(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { - pde32_t * host_pde = (pde32_t *)CR3_TO_PDE32_VA((void *)host_cr3); + +int v3_translate_host_pt_32(v3_reg_t host_cr3, addr_t vaddr, addr_t * paddr) { + pde32_t * host_pde = (pde32_t *)CR3_TO_PDE32_VA(host_cr3); pte32_t * host_pte = 0; switch (pde32_lookup(host_pde, vaddr, (addr_t *)&host_pte)) { - case PDE32_ENTRY_NOT_PRESENT: + case PT_ENTRY_NOT_PRESENT: *paddr = 0; return -1; - case PDE32_ENTRY_LARGE_PAGE: + case PT_ENTRY_LARGE_PAGE: *paddr = (addr_t)host_pte; return 0; - case PDE32_ENTRY_PTE32: - if (pte32_lookup(host_pte, vaddr, paddr) == -1) { + case PT_ENTRY_PAGE: + if (pte32_lookup(V3_VAddr(host_pte), vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { return -1; } + return 0; } - + + // should never get here return -1; } +int v3_translate_host_pt_32pae(v3_reg_t host_cr3, addr_t vaddr, addr_t * paddr) { + pdpe32pae_t * host_pdpe = (pdpe32pae_t *)CR3_TO_PDPE32PAE_VA(host_cr3); + pde32pae_t * host_pde = NULL; + pte32pae_t * host_pte = NULL; + switch (pdpe32pae_lookup(host_pdpe, vaddr, (addr_t *)&host_pde)) { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_PAGE: + switch (pde32pae_lookup(V3_VAddr(host_pde), vaddr, (addr_t *)&host_pte)) { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_LARGE_PAGE: + *paddr = (addr_t)host_pte; + return 0; + case PT_ENTRY_PAGE: + if (pte32pae_lookup(V3_VAddr(host_pte), vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { + return -1; + } + return 0; + } + default: + return -1; + } + // should never get here + return -1; +} -int pt32_lookup(pde32_t * pd, addr_t vaddr, addr_t * paddr) { - addr_t pde_entry; - pde32_entry_type_t pde_entry_type; +int v3_translate_host_pt_64(v3_reg_t host_cr3, addr_t vaddr, addr_t * paddr) { + pml4e64_t * host_pmle = (pml4e64_t *)CR3_TO_PML4E64_VA(host_cr3); + pdpe64_t * host_pdpe = NULL; + pde64_t * host_pde = NULL; + pte64_t * host_pte = NULL; - if (pd == 0) { + switch(pml4e64_lookup(host_pmle, vaddr, (addr_t *)&host_pdpe)) { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_PAGE: + switch(pdpe64_lookup(V3_VAddr(host_pdpe), vaddr, (addr_t *)&host_pde)) { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_LARGE_PAGE: + *paddr = 0; + PrintError("1 Gigabyte Pages not supported\n"); + return -1; + case PT_ENTRY_PAGE: + switch (pde64_lookup(V3_VAddr(host_pde), vaddr, (addr_t *)&host_pte)) { + case PT_ENTRY_NOT_PRESENT: + *paddr = 0; + return -1; + case PT_ENTRY_LARGE_PAGE: + *paddr = (addr_t)host_pte; + return 0; + case PT_ENTRY_PAGE: + if (pte64_lookup(V3_VAddr(host_pte), vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { + return -1; + } + return 0; + } + } + default: return -1; } - pde_entry_type = pde32_lookup(pd, vaddr, &pde_entry); - - if (pde_entry_type == PDE32_ENTRY_PTE32) { - return pte32_lookup((pte32_t *)pde_entry, vaddr, paddr); - } else if (pde_entry_type == PDE32_ENTRY_LARGE_PAGE) { - *paddr = pde_entry; - return 0; - } - + // should never get here return -1; } -/* We can't do a full lookup because we don't know what context the page tables are in... - * The entry addresses could be pointing to either guest physical memory or host physical memory - * Instead we just return the entry address, and a flag to show if it points to a pte or a large page... - */ -/* The value of entry is a return type: + + +/* + * PAGE TABLE LOOKUP FUNCTIONS + * + * + * The value of entry is a return type: * Page not present: *entry = 0 * Large Page: *entry = translated physical address (byte granularity) * PTE entry: *entry is the address of the PTE Page */ -pde32_entry_type_t pde32_lookup(pde32_t * pd, addr_t addr, addr_t * entry) { + +/** + * + * 32 bit Page Table lookup functions + * + **/ + +pt_entry_type_t pde32_lookup(pde32_t * pd, addr_t addr, addr_t * entry) { pde32_t * pde_entry = &(pd[PDE32_INDEX(addr)]); if (!pde_entry->present) { *entry = 0; - return PDE32_ENTRY_NOT_PRESENT; - } else { + return PT_ENTRY_NOT_PRESENT; + } else if (pde_entry->large_page) { + pde32_4MB_t * large_pde = (pde32_4MB_t *)pde_entry; - if (pde_entry->large_page) { - pde32_4MB_t * large_pde = (pde32_4MB_t *)pde_entry; + *entry = BASE_TO_PAGE_ADDR_4MB(large_pde->page_base_addr); + *entry += PAGE_OFFSET_4MB(addr); - *entry = BASE_TO_PAGE_ADDR_4MB(large_pde->page_base_addr); - *entry += PAGE_OFFSET_4MB(addr); - return PDE32_ENTRY_LARGE_PAGE; - } else { - *entry = BASE_TO_PAGE_ADDR(pde_entry->pt_base_addr); - return PDE32_ENTRY_PTE32; - } - } - return PDE32_ENTRY_NOT_PRESENT; + return PT_ENTRY_LARGE_PAGE; + } else { + *entry = BASE_TO_PAGE_ADDR(pde_entry->pt_base_addr); + return PT_ENTRY_PAGE; + } } /* Takes a virtual addr (addr) and returns the physical addr (entry) as defined in the page table */ -int pte32_lookup(pte32_t * pt, addr_t addr, addr_t * entry) { +pt_entry_type_t pte32_lookup(pte32_t * pt, addr_t addr, addr_t * entry) { pte32_t * pte_entry = &(pt[PTE32_INDEX(addr)]); if (!pte_entry->present) { *entry = 0; // PrintDebug("Lookup at non present page (index=%d)\n", PTE32_INDEX(addr)); - return -1; + return PT_ENTRY_NOT_PRESENT; } else { *entry = BASE_TO_PAGE_ADDR(pte_entry->page_base_addr) + PAGE_OFFSET(addr); - return 0; + return PT_ENTRY_PAGE; } - return -1; } -int pdpe32pae_lookup(pdpe32pae_t * pdp, addr_t addr, addr_t * entry) { + +/** + * + * 32 bit PAE Page Table lookup functions + * + **/ +pt_entry_type_t pdpe32pae_lookup(pdpe32pae_t * pdp, addr_t addr, addr_t * entry) { pdpe32pae_t * pdpe_entry = &(pdp[PDPE32PAE_INDEX(addr)]); if (!pdpe_entry->present) { *entry = 0; + return PT_ENTRY_NOT_PRESENT; + } else { + *entry = BASE_TO_PAGE_ADDR(pdpe_entry->pd_base_addr); + return PT_ENTRY_PAGE; + } +} + +pt_entry_type_t pde32pae_lookup(pde32pae_t * pd, addr_t addr, addr_t * entry) { + pde32pae_t * pde_entry = &(pd[PDE32PAE_INDEX(addr)]); + + if (!pde_entry->present) { + *entry = 0; + return PT_ENTRY_NOT_PRESENT; + } else if (pde_entry->large_page) { + pde32pae_2MB_t * large_pde = (pde32pae_2MB_t *)pde_entry; + + *entry = BASE_TO_PAGE_ADDR_2MB(large_pde->page_base_addr); + *entry += PAGE_OFFSET_2MB(addr); + + return PT_ENTRY_LARGE_PAGE; + } else { + *entry = BASE_TO_PAGE_ADDR(pde_entry->pt_base_addr); + return PT_ENTRY_PAGE; + } +} + +pt_entry_type_t pte32pae_lookup(pte32pae_t * pt, addr_t addr, addr_t * entry) { + pte32pae_t * pte_entry = &(pt[PTE32PAE_INDEX(addr)]); + + if (!pte_entry->present) { + *entry = 0; + return PT_ENTRY_NOT_PRESENT; + } else { + *entry = BASE_TO_PAGE_ADDR(pte_entry->page_base_addr) + PAGE_OFFSET(addr); + return PT_ENTRY_PAGE; + } +} + + + +/** + * + * 64 bit Page Table lookup functions + * + **/ +pt_entry_type_t pml4e64_lookup(pml4e64_t * pml, addr_t addr, addr_t * entry) { + pml4e64_t * pml_entry = &(pml[PML4E64_INDEX(addr)]); + + if (!pml_entry->present) { + *entry = 0; + return PT_ENTRY_NOT_PRESENT; + } else { + *entry = BASE_TO_PAGE_ADDR(pml_entry->pdp_base_addr); + return PT_ENTRY_PAGE; + } +} + +pt_entry_type_t pdpe64_lookup(pdpe64_t * pdp, addr_t addr, addr_t * entry) { + pdpe64_t * pdpe_entry = &(pdp[PDPE64_INDEX(addr)]); + + if (!pdpe_entry->present) { + *entry = 0; + return PT_ENTRY_NOT_PRESENT; + } else if (pdpe_entry->large_page) { + PrintError("1 Gigabyte pages not supported\n"); + V3_ASSERT(0); return -1; } else { - *entry = BASE_TO_PAGE_ADDR(pdpe_entry->pd_base_addr) + PAGE_OFFSET(addr); + *entry = BASE_TO_PAGE_ADDR(pdpe_entry->pd_base_addr); + return PT_ENTRY_PAGE; + } +} + +pt_entry_type_t pde64_lookup(pde64_t * pd, addr_t addr, addr_t * entry) { + pde64_t * pde_entry = &(pd[PDE64_INDEX(addr)]); + + if (!pde_entry->present) { + *entry = 0; + return PT_ENTRY_NOT_PRESENT; + } else if (pde_entry->large_page) { + pde64_2MB_t * large_pde = (pde64_2MB_t *)pde_entry; + + *entry = BASE_TO_PAGE_ADDR_2MB(large_pde->page_base_addr); + *entry += PAGE_OFFSET_2MB(addr); + + return PT_ENTRY_LARGE_PAGE; + } else { + *entry = BASE_TO_PAGE_ADDR(pde_entry->pt_base_addr); + return PT_ENTRY_PAGE; + } +} + +pt_entry_type_t pte64_lookup(pte64_t * pt, addr_t addr, addr_t * entry) { + pte64_t * pte_entry = &(pt[PTE64_INDEX(addr)]); + + if (!pte_entry->present) { + *entry = 0; + return PT_ENTRY_NOT_PRESENT; + } else { + *entry = BASE_TO_PAGE_ADDR(pte_entry->page_base_addr) + PAGE_OFFSET(addr); + return PT_ENTRY_PAGE; + } +} + + + + + + + + +/* + * + * Page Table Access Checks + * + */ + + + + + + +int v3_check_host_pt_32(v3_reg_t host_cr3, addr_t vaddr, pf_error_t access_type, pt_access_status_t * access_status) { + pde32_t * host_pde = (pde32_t *)CR3_TO_PDE32_VA(host_cr3); + pte32_t * host_pte = 0; + + int pt_level = 2; + + // Check accessibility of PDE + *access_status = v3_can_access_pde32(host_pde, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch (pde32_lookup(host_pde, vaddr, (addr_t *)&host_pte)) { + case PT_ENTRY_LARGE_PAGE: + return 0; + case PT_ENTRY_PAGE: + *access_status = v3_can_access_pte32(V3_VAddr(host_pte), vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + return 0; + default: + return -1; + } + + // should never get here + return -1; +} + +int v3_check_host_pt_32pae(v3_reg_t host_cr3, addr_t vaddr, pf_error_t access_type, pt_access_status_t * access_status) { + pdpe32pae_t * host_pdpe = (pdpe32pae_t *)CR3_TO_PDPE32PAE_VA(host_cr3); + pde32pae_t * host_pde = NULL; + pte32pae_t * host_pte = NULL; + int pt_level = 3; + + *access_status = v3_can_access_pdpe32pae(host_pdpe, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch (pdpe32pae_lookup(host_pdpe, vaddr, (addr_t *)&host_pde)) { + case PT_ENTRY_PAGE: + *access_status = v3_can_access_pde32pae(V3_VAddr(host_pde), vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch (pde32pae_lookup(V3_VAddr(host_pde), vaddr, (addr_t *)&host_pte)) { + case PT_ENTRY_LARGE_PAGE: + return 0; + case PT_ENTRY_PAGE: + *access_status = v3_can_access_pte32pae(V3_VAddr(host_pte), vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + return 0; + default: + return -1; + } + default: + return -1; + } + + // should never get here + return -1; +} + + + +int v3_check_host_pt_64(v3_reg_t host_cr3, addr_t vaddr, pf_error_t access_type, pt_access_status_t * access_status) { + pml4e64_t * host_pmle = (pml4e64_t *)CR3_TO_PML4E64_VA(host_cr3); + pdpe64_t * host_pdpe = NULL; + pde64_t * host_pde = NULL; + pte64_t * host_pte = NULL; + int pt_level = 4; + + + *access_status = v3_can_access_pml4e64(host_pmle, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch(pml4e64_lookup(host_pmle, vaddr, (addr_t *)&host_pdpe)) { + case PT_ENTRY_PAGE: + *access_status = v3_can_access_pdpe64(V3_VAddr(host_pdpe), vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch(pdpe64_lookup(V3_VAddr(host_pdpe), vaddr, (addr_t *)&host_pde)) { + case PT_ENTRY_LARGE_PAGE: + return 0; + case PT_ENTRY_PAGE: + *access_status = v3_can_access_pde64(V3_VAddr(host_pde), vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch (pde64_lookup(V3_VAddr(host_pde), vaddr, (addr_t *)&host_pte)) { + case PT_ENTRY_LARGE_PAGE: + return 0; + case PT_ENTRY_PAGE: + *access_status = v3_can_access_pte64(V3_VAddr(host_pte), vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + return 0; + default: + return -1; + } + default: + return -1; + } + default: + return -1; + } + + // should never get here + return -1; +} + + + + + +int v3_check_guest_pt_32(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, + pf_error_t access_type, pt_access_status_t * access_status) { + addr_t guest_pde_pa = CR3_TO_PDE32_PA(guest_cr3); + pde32_t * guest_pde = NULL; + addr_t guest_pte_pa = 0; + int pt_level = 2; + + if (guest_pa_to_host_va(info, guest_pde_pa, (addr_t*)&guest_pde) == -1) { + PrintError("Could not get virtual address of Guest PDE32 (PA=%p)\n", + (void *)guest_pde_pa); + return -1; + } + + + // Check accessibility of PDE + *access_status = v3_can_access_pde32(guest_pde, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch (pde32_lookup(guest_pde, vaddr, &guest_pte_pa)) { + case PT_ENTRY_LARGE_PAGE: return 0; + case PT_ENTRY_PAGE: + { + pte32_t * guest_pte = NULL; + + if (guest_pa_to_host_va(info, guest_pte_pa, (addr_t*)&guest_pte) == -1) { + PrintError("Could not get virtual address of Guest PTE32 (PA=%p)\n", + (void *)guest_pte_pa); + return -1; + } + + *access_status = v3_can_access_pte32(guest_pte, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + return 0; + } + default: + return -1; } + + // should never get here + return -1; +} + + + + +int v3_check_guest_pt_32pae(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, + pf_error_t access_type, pt_access_status_t * access_status) { + addr_t guest_pdpe_pa = CR3_TO_PDPE32PAE_PA(guest_cr3); + pdpe32pae_t * guest_pdpe = NULL; + addr_t guest_pde_pa = 0; + int pt_level = 3; + + if (guest_pa_to_host_va(info, guest_pdpe_pa, (addr_t*)&guest_pdpe) == -1) { + PrintError("Could not get virtual address of Guest PDPE32PAE (PA=%p)\n", + (void *)guest_pdpe_pa); + return -1; + } + + *access_status = v3_can_access_pdpe32pae(guest_pdpe, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch (pdpe32pae_lookup(guest_pdpe, vaddr, &guest_pde_pa)) { + case PT_ENTRY_PAGE: + { + pde32pae_t * guest_pde = NULL; + addr_t guest_pte_pa = 0; + + if (guest_pa_to_host_va(info, guest_pde_pa, (addr_t *)&guest_pde) == -1) { + PrintError("Could not get virtual Address of Guest PDE32PAE (PA=%p)\n", + (void *)guest_pde_pa); + return -1; + } + + *access_status = v3_can_access_pde32pae(guest_pde, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch (pde32pae_lookup(guest_pde, vaddr, &guest_pte_pa)) { + case PT_ENTRY_LARGE_PAGE: + return 0; + case PT_ENTRY_PAGE: + { + pte32pae_t * guest_pte = NULL; + + if (guest_pa_to_host_va(info, guest_pte_pa, (addr_t *)&guest_pte) == -1) { + PrintError("Could not get virtual Address of Guest PTE32PAE (PA=%p)\n", + (void *)guest_pte_pa); + return -1; + } + + *access_status = v3_can_access_pte32pae(guest_pte, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + return 0; + } + default: + return -1; + } + } + default: + return -1; + } + + // should never get here return -1; } + pte64_t * guest_pte = NULL; -pt_access_status_t can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type) { - pde32_t * entry = &pde[PDE32_INDEX(addr)]; +int v3_check_guest_pt_64(struct guest_info * info, v3_reg_t guest_cr3, addr_t vaddr, + pf_error_t access_type, pt_access_status_t * access_status) { + addr_t guest_pml4_pa = CR3_TO_PML4E64_PA(guest_cr3); + pml4e64_t * guest_pmle = NULL; + addr_t guest_pdpe_pa = 0; + int pt_level = 4; - if (entry->present == 0) { - return PT_ENTRY_NOT_PRESENT; - } else if ((entry->writable == 0) && (access_type.write == 1)) { - return PT_WRITE_ERROR; - } else if ((entry->user_page == 0) && (access_type.user == 1)) { - // Check CR0.WP? - return PT_USER_ERROR; + if (guest_pa_to_host_va(info, guest_pml4_pa, (addr_t*)&guest_pmle) == -1) { + PrintError("Could not get virtual address of Guest PML4E64 (PA=%p)\n", + (void *)guest_pml4_pa); + return -1; } - return PT_ACCESS_OK; + *access_status = v3_can_access_pml4e64(guest_pmle, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch(pml4e64_lookup(guest_pmle, vaddr, &guest_pdpe_pa)) { + case PT_ENTRY_PAGE: + { + pdpe64_t * guest_pdp = NULL; + addr_t guest_pde_pa = 0; + + if (guest_pa_to_host_va(info, guest_pdpe_pa, (addr_t *)&guest_pdp) == -1) { + PrintError("Could not get virtual address of Guest PDPE64 (PA=%p)\n", + (void *)guest_pdpe_pa); + return -1; + } + + *access_status = v3_can_access_pdpe64(guest_pdp, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch(pdpe64_lookup(guest_pdp, vaddr, &guest_pde_pa)) { + case PT_ENTRY_LARGE_PAGE: + return 0; + case PT_ENTRY_PAGE: + { + pde64_t * guest_pde = NULL; + addr_t guest_pte_pa = 0; + + if (guest_pa_to_host_va(info, guest_pde_pa, (addr_t *)&guest_pde) == -1) { + PrintError("Could not get virtual address of guest PDE64 (PA=%p)\n", + (void *)guest_pde_pa); + return -1; + } + + *access_status = v3_can_access_pde64(guest_pde, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + pt_level--; + + switch (pde64_lookup(guest_pde, vaddr, &guest_pte_pa)) { + case PT_ENTRY_LARGE_PAGE: + return 0; + case PT_ENTRY_PAGE: + { + pte64_t * guest_pte = NULL; + + if (guest_pa_to_host_va(info, guest_pte_pa, (addr_t *)&guest_pte) == -1) { + PrintError("Could not get virtual address of guest PTE64 (PA=%p)\n", + (void *)guest_pte_pa); + return -1; + } + + *access_status = v3_can_access_pte64(guest_pte, vaddr, access_type); + + if (*access_status != PT_ACCESS_OK) { + return pt_level; + } + + return 0; + } + default: + return -1; + } + } + default: + return -1; + } + } + default: + return -1; + } + + // should never get here + return -1; } -pt_access_status_t can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t access_type) { - pte32_t * entry = &pte[PTE32_INDEX(addr)]; - if (entry->present == 0) { - return PT_ENTRY_NOT_PRESENT; - } else if ((entry->writable == 0) && (access_type.write == 1)) { - return PT_WRITE_ERROR; - } else if ((entry->user_page == 0) && (access_type.user == 1)) { + + +static pt_access_status_t can_access_pt_entry(gen_pt_t * pt, pf_error_t access_type) { + if (pt->present == 0) { + return PT_ACCESS_NOT_PRESENT; + } else if ((pt->writable == 0) && (access_type.write == 1)) { + return PT_ACCESS_WRITE_ERROR; + } else if ((pt->user_page == 0) && (access_type.user == 1)) { // Check CR0.WP? - return PT_USER_ERROR; + return PT_ACCESS_USER_ERROR; } return PT_ACCESS_OK; @@ -288,6 +934,65 @@ pt_access_status_t can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t acces +/* + * 32 bit access checks + */ +pt_access_status_t inline v3_can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pde[PDE32_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + +pt_access_status_t inline v3_can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pte[PTE32_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + + +/* + * 32 bit PAE access checks + */ +pt_access_status_t inline v3_can_access_pdpe32pae(pdpe32pae_t * pdpe, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pdpe[PDPE32PAE_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + +pt_access_status_t inline v3_can_access_pde32pae(pde32pae_t * pde, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pde[PDE32PAE_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + +pt_access_status_t inline v3_can_access_pte32pae(pte32pae_t * pte, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pte[PTE32PAE_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + +/* + * 64 Bit access checks + */ +pt_access_status_t inline v3_can_access_pml4e64(pml4e64_t * pmle, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pmle[PML4E64_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + +pt_access_status_t inline v3_can_access_pdpe64(pdpe64_t * pdpe, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pdpe[PDPE64_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + +pt_access_status_t inline v3_can_access_pde64(pde64_t * pde, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pde[PDE32_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + +pt_access_status_t inline v3_can_access_pte64(pte64_t * pte, addr_t addr, pf_error_t access_type) { + gen_pt_t * entry = (gen_pt_t *)&pte[PTE64_INDEX(addr)]; + return can_access_pt_entry(entry, access_type); +} + + + + + /* We generate a page table to correspond to a given memory layout * pulling pages from the mem_list when necessary @@ -612,7 +1317,7 @@ pml4e64_t * create_passthrough_pts_64(struct guest_info * info) { pde[k].write_through = 0; pde[k].cache_disable = 0; pde[k].accessed = 0; - pde[k].reserved = 0; + pde[k].avail = 0; pde[k].large_page = 0; //pde[k].global_page = 0; pde[k].vmm_info = 0; @@ -624,7 +1329,7 @@ pml4e64_t * create_passthrough_pts_64(struct guest_info * info) { pde[k].write_through = 0; pde[k].cache_disable = 0; pde[k].accessed = 0; - pde[k].reserved = 0; + pde[k].avail = 0; pde[k].large_page = 0; //pde[k].global_page = 0; pde[k].vmm_info = 0; @@ -643,7 +1348,7 @@ pml4e64_t * create_passthrough_pts_64(struct guest_info * info) { pdpe[j].write_through = 0; pdpe[j].cache_disable = 0; pdpe[j].accessed = 0; - pdpe[j].reserved = 0; + pdpe[j].avail = 0; pdpe[j].large_page = 0; //pdpe[j].global_page = 0; pdpe[j].vmm_info = 0; @@ -655,7 +1360,7 @@ pml4e64_t * create_passthrough_pts_64(struct guest_info * info) { pdpe[j].write_through = 0; pdpe[j].cache_disable = 0; pdpe[j].accessed = 0; - pdpe[j].reserved = 0; + pdpe[j].avail = 0; pdpe[j].large_page = 0; //pdpe[j].global_page = 0; pdpe[j].vmm_info = 0;