X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmm_paging.c;h=f63ddf18a44c7bdc2ea43c83a8ef12b67ace9e50;hb=11b5cccde17af54f3998c38952e0eeedf85ced0a;hp=3bea238cff7c92fd48f054f4b95d7d09890bacaa;hpb=f340744762cb6a6bf0116e2ad07b50a7e890780c;p=palacios.git diff --git a/palacios/src/palacios/vmm_paging.c b/palacios/src/palacios/vmm_paging.c index 3bea238..f63ddf1 100644 --- a/palacios/src/palacios/vmm_paging.c +++ b/palacios/src/palacios/vmm_paging.c @@ -62,7 +62,7 @@ void delete_page_tables_64(pml4e64_t * pml4) { } -int v3_translate_guest_pt_32(struct guest_info * info, addr_t guest_cr3, addr_t vaddr, addr_t * paddr) { +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; @@ -98,11 +98,12 @@ int v3_translate_guest_pt_32(struct guest_info * info, addr_t guest_cr3, addr_t } } - return 0; + // should never get here + return -1; } -int v3_translate_guest_pt_32pae(struct guest_info * info, addr_t guest_cr3, addr_t vaddr, addr_t * paddr) { +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; @@ -159,10 +160,11 @@ int v3_translate_guest_pt_32pae(struct guest_info * info, addr_t guest_cr3, addr return -1; } - return 0; + // should never get here + return -1; } -int v3_translate_guest_pt_64(struct guest_info * info, addr_t guest_cr3, addr_t vaddr, addr_t * paddr) { +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; @@ -237,12 +239,14 @@ int v3_translate_guest_pt_64(struct guest_info * info, addr_t guest_cr3, addr_t default: return -1; } - return 0; + + // should never get here + return -1; } -int v3_translate_host_pt_32(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { +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; @@ -254,7 +258,7 @@ int v3_translate_host_pt_32(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { *paddr = (addr_t)host_pte; return 0; case PT_ENTRY_PAGE: - if (pte32_lookup(host_pte, vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { + if (pte32_lookup(V3_VAddr(host_pte), vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { return -1; } return 0; @@ -265,7 +269,7 @@ int v3_translate_host_pt_32(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { } -int v3_translate_host_pt_32pae(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { +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; @@ -275,7 +279,7 @@ int v3_translate_host_pt_32pae(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { *paddr = 0; return -1; case PT_ENTRY_PAGE: - switch (pde32pae_lookup(host_pde, vaddr, (addr_t *)&host_pte)) { + switch (pde32pae_lookup(V3_VAddr(host_pde), vaddr, (addr_t *)&host_pte)) { case PT_ENTRY_NOT_PRESENT: *paddr = 0; return -1; @@ -283,7 +287,7 @@ int v3_translate_host_pt_32pae(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { *paddr = (addr_t)host_pte; return 0; case PT_ENTRY_PAGE: - if (pte32pae_lookup(host_pte, vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { + if (pte32pae_lookup(V3_VAddr(host_pte), vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { return -1; } return 0; @@ -297,7 +301,7 @@ int v3_translate_host_pt_32pae(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { } -int v3_translate_host_pt_64(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { +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; @@ -308,7 +312,7 @@ int v3_translate_host_pt_64(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { *paddr = 0; return -1; case PT_ENTRY_PAGE: - switch(pdpe64_lookup(host_pdpe, vaddr, (addr_t *)&host_pde)) { + switch(pdpe64_lookup(V3_VAddr(host_pdpe), vaddr, (addr_t *)&host_pde)) { case PT_ENTRY_NOT_PRESENT: *paddr = 0; return -1; @@ -317,13 +321,15 @@ int v3_translate_host_pt_64(addr_t host_cr3, addr_t vaddr, addr_t * paddr) { PrintError("1 Gigabyte Pages not supported\n"); return -1; case PT_ENTRY_PAGE: - switch (pde64_lookup(host_pde, vaddr, (addr_t *)&host_pte)) { + 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(host_pte, vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { + if (pte64_lookup(V3_VAddr(host_pte), vaddr, paddr) == PT_ENTRY_NOT_PRESENT) { return -1; } return 0; @@ -517,39 +523,408 @@ pt_entry_type_t pte64_lookup(pte64_t * pt, addr_t addr, addr_t * entry) { +/* + * + * 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_access_status_t can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type) { - pde32_t * entry = &pde[PDE32_INDEX(addr)]; + 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; + } - if (entry->present == 0) { - return PT_ACCESS_NOT_PRESENT; - } else if ((entry->writable == 0) && (access_type.write == 1)) { - return PT_ACCESS_WRITE_ERROR; - } else if ((entry->user_page == 0) && (access_type.user == 1)) { - // Check CR0.WP? - return PT_ACCESS_USER_ERROR; + return 0; + default: + return -1; } + + // should never get here + return -1; +} - return PT_ACCESS_OK; +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; } -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) { + + +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; + +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 (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; + } + + *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; +} + + + + + +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 ((entry->writable == 0) && (access_type.write == 1)) { + } else if ((pt->writable == 0) && (access_type.write == 1)) { return PT_ACCESS_WRITE_ERROR; - } else if ((entry->user_page == 0) && (access_type.user == 1)) { + } else if ((pt->user_page == 0) && (access_type.user == 1)) { // Check CR0.WP? return PT_ACCESS_USER_ERROR; } @@ -559,6 +934,69 @@ 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 @@ -973,5 +1411,475 @@ pml4e64_t * create_passthrough_pts_64(struct guest_info * info) { } +int v3_walk_guest_pt_32(struct guest_info * info, v3_reg_t guest_cr3, + void (*callback)(page_type_t type, addr_t page_ptr, addr_t page_pa, void * private_data), + void * private_data) { + addr_t guest_pde_pa = CR3_TO_PDE32_PA(guest_cr3); + pde32_t * guest_pde = NULL; + int i, j; + + if (!callback) { + PrintError("Call back was not specified\n"); + return -1; + } + + 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; + } + + callback(PAGE_PD32, (addr_t)guest_pde, guest_pde_pa, private_data); + + for (i = 0; i < MAX_PDE32_ENTRIES; i++) { + if (guest_pde[i].present) { + if (guest_pde[i].large_page) { + pde32_4MB_t * large_pde = (pde32_4MB_t *)&(guest_pde[i]); + addr_t large_page_pa = BASE_TO_PAGE_ADDR_4MB(large_pde->page_base_addr); + addr_t large_page_va = 0; + + if (guest_pa_to_host_va(info, large_page_pa, &large_page_va) == -1) { + PrintError("Could not get virtual address of Guest 4MB Page (PA=%p)\n", + (void *)large_page_pa); + return -1; + } + + callback(PAGE_4MB, large_page_va, large_page_pa, private_data); + } else { + addr_t pte_pa = BASE_TO_PAGE_ADDR(guest_pde[i].pt_base_addr); + pte32_t * tmp_pte = NULL; + + if (guest_pa_to_host_va(info, pte_pa, (addr_t *)&tmp_pte) == -1) { + PrintError("Could not get virtual address of Guest PTE32 (PA=%p)\n", + (void *)pte_pa); + return -1; + } + + callback(PAGE_PT32, (addr_t)tmp_pte, pte_pa, private_data); + + for (j = 0; j < MAX_PTE32_ENTRIES; j++) { + if (tmp_pte[j].present) { + addr_t page_pa = BASE_TO_PAGE_ADDR(tmp_pte[j].page_base_addr); + addr_t page_va = 0; + + if (guest_pa_to_host_va(info, page_pa, &page_va) == -1) { + PrintError("Could not get virtual address of Guest 4KB Page (PA=%p)\n", + (void *)page_pa); + return -1; + } + + callback(PAGE_4KB, page_va, page_pa, private_data); + } + } + } + } + } + return 0; +} + + +int v3_walk_guest_pt_32pae(struct guest_info * info, v3_reg_t guest_cr3, + void (*callback)(page_type_t type, addr_t page_ptr, addr_t page_pa, void * private_data), + void * private_data) { + addr_t guest_pdpe_pa = CR3_TO_PDPE32PAE_PA(guest_cr3); + pdpe32pae_t * guest_pdpe = NULL; + int i, j, k; + + if (!callback) { + PrintError("Call back was not specified\n"); + return -1; + } + + 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; + } + + + + callback(PAGE_PDP32PAE, (addr_t)guest_pdpe, guest_pdpe_pa, private_data); + + for (i = 0; i < MAX_PDPE32PAE_ENTRIES; i++) { + if (guest_pdpe[i].present) { + addr_t pde_pa = BASE_TO_PAGE_ADDR(guest_pdpe[i].pd_base_addr); + pde32pae_t * tmp_pde = NULL; + + if (guest_pa_to_host_va(info, pde_pa, (addr_t *)&tmp_pde) == -1) { + PrintError("Could not get virtual address of Guest PDE32PAE (PA=%p)\n", + (void *)pde_pa); + return -1; + } + + callback(PAGE_PD32PAE, (addr_t)tmp_pde, pde_pa, private_data); + + for (j = 0; j < MAX_PDE32PAE_ENTRIES; j++) { + if (tmp_pde[j].present) { + if (tmp_pde[j].large_page) { + pde32pae_2MB_t * large_pde = (pde32pae_2MB_t *)&(tmp_pde[j]); + addr_t large_page_pa = BASE_TO_PAGE_ADDR_2MB(large_pde->page_base_addr); + addr_t large_page_va = 0; + + if (guest_pa_to_host_va(info, large_page_pa, &large_page_va) == -1) { + PrintError("Could not get virtual address of Guest 2MB Page (PA=%p)\n", + (void *)large_page_pa); + return -1; + } + + callback(PAGE_2MB, large_page_va, large_page_pa, private_data); + } else { + addr_t pte_pa = BASE_TO_PAGE_ADDR(tmp_pde[j].pt_base_addr); + pte32pae_t * tmp_pte = NULL; + + if (guest_pa_to_host_va(info, pte_pa, (addr_t *)&tmp_pte) == -1) { + PrintError("Could not get virtual address of Guest PTE32PAE (PA=%p)\n", + (void *)pte_pa); + return -1; + } + + callback(PAGE_PT32PAE, (addr_t)tmp_pte, pte_pa, private_data); + + for (k = 0; k < MAX_PTE32PAE_ENTRIES; k++) { + if (tmp_pte[k].present) { + addr_t page_pa = BASE_TO_PAGE_ADDR(tmp_pte[k].page_base_addr); + addr_t page_va = 0; + + if (guest_pa_to_host_va(info, page_pa, &page_va) == -1) { + PrintError("Could not get virtual address of Guest 4KB Page (PA=%p)\n", + (void *)page_pa); + return -1; + } + + callback(PAGE_4KB, page_va, page_pa, private_data); + } + } + } + } + } + } + } + return 0; +} + + + + +int v3_walk_guest_pt_64(struct guest_info * info, v3_reg_t guest_cr3, + void (*callback)(page_type_t type, addr_t page_ptr, addr_t page_pa, void * private_data), + void * private_data) { + addr_t guest_pml_pa = CR3_TO_PML4E64_PA(guest_cr3); + pml4e64_t * guest_pml = NULL; + int i, j, k, m; + + if (!callback) { + PrintError("Call back was not specified\n"); + return -1; + } + + if (guest_pa_to_host_va(info, guest_pml_pa, (addr_t *)&guest_pml) == -1) { + PrintError("Could not get virtual address of Guest PML464 (PA=%p)\n", + (void *)guest_pml); + return -1; + } + + + callback(PAGE_PML464, (addr_t)guest_pml, guest_pml_pa, private_data); + + for (i = 0; i < MAX_PML4E64_ENTRIES; i++) { + if (guest_pml[i].present) { + addr_t pdpe_pa = BASE_TO_PAGE_ADDR(guest_pml[i].pdp_base_addr); + pdpe64_t * tmp_pdpe = NULL; + + + if (guest_pa_to_host_va(info, pdpe_pa, (addr_t *)&tmp_pdpe) == -1) { + PrintError("Could not get virtual address of Guest PDPE64 (PA=%p)\n", + (void *)pdpe_pa); + return -1; + } + + callback(PAGE_PDP64, (addr_t)tmp_pdpe, pdpe_pa, private_data); + + for (j = 0; j < MAX_PDPE64_ENTRIES; j++) { + if (tmp_pdpe[j].present) { + if (tmp_pdpe[j].large_page) { + pdpe64_1GB_t * large_pdpe = (pdpe64_1GB_t *)&(tmp_pdpe[j]); + addr_t large_page_pa = BASE_TO_PAGE_ADDR_1GB(large_pdpe->page_base_addr); + addr_t large_page_va = 0; + + if (guest_pa_to_host_va(info, large_page_pa, &large_page_va) == -1) { + PrintError("Could not get virtual address of Guest 1GB page (PA=%p)\n", + (void *)large_page_pa); + return -1; + } + + callback(PAGE_1GB, (addr_t)large_page_va, large_page_pa, private_data); + + } else { + addr_t pde_pa = BASE_TO_PAGE_ADDR(tmp_pdpe[j].pd_base_addr); + pde64_t * tmp_pde = NULL; + + if (guest_pa_to_host_va(info, pde_pa, (addr_t *)&tmp_pde) == -1) { + PrintError("Could not get virtual address of Guest PDE64 (PA=%p)\n", + (void *)pde_pa); + return -1; + } + + callback(PAGE_PD64, (addr_t)tmp_pde, pde_pa, private_data); + + for (k = 0; k < MAX_PDE64_ENTRIES; k++) { + if (tmp_pde[k].present) { + if (tmp_pde[k].large_page) { + pde64_2MB_t * large_pde = (pde64_2MB_t *)&(tmp_pde[k]); + addr_t large_page_pa = BASE_TO_PAGE_ADDR_2MB(large_pde->page_base_addr); + addr_t large_page_va = 0; + + if (guest_pa_to_host_va(info, large_page_pa, &large_page_va) == -1) { + PrintError("Could not get virtual address of Guest 2MB page (PA=%p)\n", + (void *)large_page_pa); + return -1; + } + + callback(PAGE_2MB, large_page_va, large_page_pa, private_data); + } else { + addr_t pte_pa = BASE_TO_PAGE_ADDR(tmp_pde[k].pt_base_addr); + pte64_t * tmp_pte = NULL; + + if (guest_pa_to_host_va(info, pte_pa, (addr_t *)&tmp_pte) == -1) { + PrintError("Could not get virtual address of Guest PTE64 (PA=%p)\n", + (void *)pte_pa); + return -1; + } + + callback(PAGE_PT64, (addr_t)tmp_pte, pte_pa, private_data); + + for (m = 0; m < MAX_PTE64_ENTRIES; m++) { + if (tmp_pte[m].present) { + addr_t page_pa = BASE_TO_PAGE_ADDR(tmp_pte[m].page_base_addr); + addr_t page_va = 0; + + if (guest_pa_to_host_va(info, page_pa, &page_va) == -1) { + PrintError("Could not get virtual address of Guest 4KB Page (PA=%p)\n", + (void *)page_pa); + return -1; + } + + callback(PAGE_4KB, page_va, page_pa, private_data); + } + } + } + } + } + } + } + } + } + } + return 0; +} + +int v3_walk_host_pt_32(v3_reg_t host_cr3, + void (*callback)(page_type_t type, addr_t page_ptr, addr_t page_pa, void * private_data), + void * private_data) { + pde32_t * host_pde = (pde32_t *)CR3_TO_PDE32_VA(host_cr3); + addr_t pde_pa = CR3_TO_PDE32_PA(host_cr3); + int i, j; + + if (!callback) { + PrintError("Call back was not specified\n"); + return -1; + } + + callback(PAGE_PD32, (addr_t)host_pde, pde_pa, private_data); + + for (i = 0; i < MAX_PDE32_ENTRIES; i++) { + if (host_pde[i].present) { + if (host_pde[i].large_page) { + pde32_4MB_t * large_pde = (pde32_4MB_t *)&(host_pde[i]); + addr_t large_page_pa = BASE_TO_PAGE_ADDR_4MB(large_pde->page_base_addr); + + callback(PAGE_4MB, (addr_t)V3_VAddr((void *)large_page_pa), large_page_pa, private_data); + } else { + addr_t pte_pa = BASE_TO_PAGE_ADDR(host_pde[i].pt_base_addr); + pte32_t * tmp_pte = (pte32_t *)V3_VAddr((void *)pte_pa); + + callback(PAGE_PT32, (addr_t)tmp_pte, pte_pa, private_data); + + for (j = 0; j < MAX_PTE32_ENTRIES; j++) { + if (tmp_pte[j].present) { + addr_t page_pa = BASE_TO_PAGE_ADDR(tmp_pte[j].page_base_addr); + callback(PAGE_4KB, (addr_t)V3_VAddr((void *)page_pa), page_pa, private_data); + } + } + } + } + } + return 0; +} + + + + + +int v3_walk_host_pt_32pae(v3_reg_t host_cr3, + void (*callback)(page_type_t type, addr_t page_ptr, addr_t page_pa, void * private_data), + void * private_data) { + pdpe32pae_t * host_pdpe = (pdpe32pae_t *)CR3_TO_PDPE32PAE_VA(host_cr3); + addr_t pdpe_pa = CR3_TO_PDPE32PAE_PA(host_cr3); + int i, j, k; + + if (!callback) { + PrintError("Callback was not specified\n"); + return -1; + } + + callback(PAGE_PDP32PAE, (addr_t)host_pdpe, pdpe_pa, private_data); + + for (i = 0; i < MAX_PDPE32PAE_ENTRIES; i++) { + if (host_pdpe[i].present) { + addr_t pde_pa = BASE_TO_PAGE_ADDR(host_pdpe[i].pd_base_addr); + pde32pae_t * tmp_pde = (pde32pae_t *)V3_VAddr((void *)pde_pa); + + callback(PAGE_PD32PAE, (addr_t)tmp_pde, pde_pa, private_data); + + for (j = 0; j < MAX_PDE32PAE_ENTRIES; j++) { + if (tmp_pde[j].present) { + + if (tmp_pde[j].large_page) { + pde32pae_2MB_t * large_pde = (pde32pae_2MB_t *)&(tmp_pde[j]); + addr_t large_page_pa = BASE_TO_PAGE_ADDR_2MB(large_pde->page_base_addr); + + callback(PAGE_2MB, (addr_t)V3_VAddr((void *)large_page_pa), large_page_pa, private_data); + } else { + addr_t pte_pa = BASE_TO_PAGE_ADDR(tmp_pde[j].pt_base_addr); + pte32pae_t * tmp_pte = (pte32pae_t *)V3_VAddr((void *)pte_pa); + + callback(PAGE_PT32PAE, (addr_t)tmp_pte, pte_pa, private_data); + + for (k = 0; k < MAX_PTE32PAE_ENTRIES; k++) { + if (tmp_pte[k].present) { + addr_t page_pa = BASE_TO_PAGE_ADDR(tmp_pte[k].page_base_addr); + callback(PAGE_4KB, (addr_t)V3_VAddr((void *)page_pa), page_pa, private_data); + } + } + } + } + } + } + } + return 0; +} + + +int v3_walk_host_pt_64(v3_reg_t host_cr3, + void (*callback)(page_type_t type, addr_t page_ptr, addr_t page_pa, void * private_data), + void * private_data) { + pml4e64_t * host_pml = (pml4e64_t *)CR3_TO_PML4E64_VA(host_cr3); + addr_t pml_pa = CR3_TO_PML4E64_PA(host_cr3); + int i, j, k, m; + + if (!callback) { + PrintError("Callback was not specified\n"); + return -1; + } + + callback(PAGE_PML464, (addr_t)host_pml, pml_pa, private_data); + + for (i = 0; i < MAX_PML4E64_ENTRIES; i++) { + if (host_pml[i].present) { + addr_t pdpe_pa = BASE_TO_PAGE_ADDR(host_pml[i].pdp_base_addr); + pdpe64_t * tmp_pdpe = (pdpe64_t *)V3_VAddr((void *)pdpe_pa); + callback(PAGE_PDP64, (addr_t)tmp_pdpe, pdpe_pa, private_data); + for (j = 0; j < MAX_PDPE64_ENTRIES; j++) { + if (tmp_pdpe[j].present) { + if (tmp_pdpe[j].large_page) { + pdpe64_1GB_t * large_pdp = (pdpe64_1GB_t *)&(tmp_pdpe[j]); + addr_t large_page_pa = BASE_TO_PAGE_ADDR_1GB(large_pdp->page_base_addr); + + callback(PAGE_1GB, (addr_t)V3_VAddr((void *)large_page_pa), large_page_pa, private_data); + } else { + addr_t pde_pa = BASE_TO_PAGE_ADDR(tmp_pdpe[j].pd_base_addr); + pde64_t * tmp_pde = (pde64_t *)V3_VAddr((void *)pde_pa); + + callback(PAGE_PD64, (addr_t)tmp_pde, pde_pa, private_data); + + for (k = 0; k < MAX_PDE64_ENTRIES; k++) { + if (tmp_pde[k].present) { + if (tmp_pde[k].large_page) { + pde64_2MB_t * large_pde = (pde64_2MB_t *)&(tmp_pde[k]); + addr_t large_page_pa = BASE_TO_PAGE_ADDR_2MB(large_pde->page_base_addr); + + callback(PAGE_2MB, (addr_t)V3_VAddr((void *)large_page_pa), large_page_pa, private_data); + } else { + addr_t pte_pa = BASE_TO_PAGE_ADDR(tmp_pde[k].pt_base_addr); + pte64_t * tmp_pte = (pte64_t *)V3_VAddr((void *)pte_pa); + + callback(PAGE_PT64, (addr_t)tmp_pte, pte_pa, private_data); + + for (m = 0; m < MAX_PTE64_ENTRIES; m++) { + if (tmp_pte[m].present) { + addr_t page_pa = BASE_TO_PAGE_ADDR(tmp_pte[m].page_base_addr); + callback(PAGE_4KB, (addr_t)V3_VAddr((void *)page_pa), page_pa, private_data); + } + } + } + } + } + } + } + } + } + } + return 0; +} + + + +static const uchar_t PAGE_4KB_STR[] = "4KB_PAGE"; +static const uchar_t PAGE_2MB_STR[] = "2MB_PAGE"; +static const uchar_t PAGE_4MB_STR[] = "4MB_PAGE"; +static const uchar_t PAGE_1GB_STR[] = "1GB_PAGE"; +static const uchar_t PAGE_PT32_STR[] = "32 Bit PT"; +static const uchar_t PAGE_PD32_STR[] = "32 Bit PD"; +static const uchar_t PAGE_PDP32PAE_STR[] = "32 Bit PAE PDP"; +static const uchar_t PAGE_PD32PAE_STR[] = "32 Bit PAE PD"; +static const uchar_t PAGE_PT32PAE_STR[] = "32 Bit PAE PT"; +static const uchar_t PAGE_PML464_STR[] = "64 Bit PML4"; +static const uchar_t PAGE_PDP64_STR[] = "64 Bit PDP"; +static const uchar_t PAGE_PD64_STR[] = "64 Bit PD"; +static const uchar_t PAGE_PT64_STR[] = "64 Bit PT"; + + +const uchar_t * v3_page_type_to_str(page_type_t type) { + switch (type) { + case PAGE_4KB: + return PAGE_4KB_STR; + case PAGE_2MB: + return PAGE_2MB_STR; + case PAGE_4MB: + return PAGE_4MB_STR; + case PAGE_1GB: + return PAGE_1GB_STR; + case PAGE_PT32: + return PAGE_PT32_STR; + case PAGE_PD32: + return PAGE_PD32_STR; + case PAGE_PDP32PAE: + return PAGE_PDP32PAE_STR; + case PAGE_PD32PAE: + return PAGE_PD32PAE_STR; + case PAGE_PT32PAE: + return PAGE_PT32PAE_STR; + case PAGE_PML464: + return PAGE_PML464_STR; + case PAGE_PDP64: + return PAGE_PDP64_STR; + case PAGE_PD64: + return PAGE_PD64_STR; + case PAGE_PT64: + return PAGE_PT64_STR; + default: + return NULL; + } +}