+/* We'll use the general form for now....
+ typedef enum {PDE32_ENTRY_NOT_PRESENT, PDE32_ENTRY_PTE32, PDE32_ENTRY_LARGE_PAGE} pde32_entry_type_t;
+ typedef enum {PTE32_ENTRY_NOT_PRESENT, PTE32_ENTRY_PAGE} pte32_entry_type_t;
+
+ typedef enum {PDPE32PAE_ENTRY_NOT_PRESENT, PDPE32PAE_ENTRY_PAGE} pdpe32pae_entry_type_t;
+ typedef enum {PDE32PAE_ENTRY_NOT_PRESENT, PDE32PAE_ENTRY_PTE32, PDE32PAE_ENTRY_LARGE_PAGE} pde32pae_entry_type_t;
+ typedef enum {PTE32PAE_ENTRY_NOT_PRESENT, PTE32PAE_ENTRY_PAGE} pte32pae_entry_type_t;
+
+ typedef enum {PML4E64_ENTRY_NOT_PRESENT, PML4E64_ENTRY_PAGE} pml4e64_entry_type_t;
+ typedef enum {PDPE64_ENTRY_NOT_PRESENT, PDPE64_ENTRY_PTE32, PDPE64_ENTRY_LARGE_PAGE} pdpe64_entry_type_t;
+ typedef enum {PDE64_ENTRY_NOT_PRESENT, PDE64_ENTRY_PTE32, PDE64_ENTRY_LARGE_PAGE} pde64_entry_type_t;
+ typedef enum {PTE64_ENTRY_NOT_PRESENT, PTE64_ENTRY_PAGE} pte64_entry_type_t;
+*/
+
-/* PDE 32 bit PAGE STRUCTURES */
-typedef enum {PDE32_ENTRY_NOT_PRESENT, PDE32_ENTRY_PTE32, PDE32_ENTRY_LARGE_PAGE} pde32_entry_type_t;
-typedef enum {PT_ACCESS_OK, PT_ENTRY_NOT_PRESENT, PT_WRITE_ERROR, PT_USER_ERROR} pt_access_status_t;
+typedef enum {PT_ENTRY_NOT_PRESENT, PT_ENTRY_LARGE_PAGE, PT_ENTRY_PAGE} pt_entry_type_t;
+typedef enum {PT_ACCESS_OK, PT_ACCESS_NOT_PRESENT, PT_ACCESS_WRITE_ERROR, PT_ACCESS_USER_ERROR} pt_access_status_t;
typedef struct pde32 {
uint_t present : 1;
uint_t cache_disable : 1;
uint_t accessed : 1;
uint_t dirty : 1;
- uint_t one : 1;
+ uint_t large_page : 1;
uint_t global_page : 1;
uint_t vmm_info : 3;
uint_t pat : 1;
uint_t rsvd : 28;
} __attribute__((packed)) pde32pae_t;
-typedef struct pde32pae_4MB {
+typedef struct pde32pae_2MB {
uint_t present : 1;
uint_t writable : 1;
uint_t user_page : 1;
uint_t global_page : 1;
uint_t vmm_info : 3;
uint_t pat : 1;
- uint_t rsvd : 9;
- uint_t page_base_addr : 14;
+ uint_t rsvd : 8;
+ uint_t page_base_addr : 15;
uint_t rsvd2 : 28;
-} __attribute__((packed)) pde32pae_4MB_t;
+} __attribute__((packed)) pde32pae_2MB_t;
typedef struct pte32pae {
uint_t present : 1;
typedef struct pdpe64 {
uint_t present : 1;
uint_t writable : 1;
- uint_t user_page : 1;
+ uint_t user_page : 1;
uint_t write_through : 1;
uint_t cache_disable : 1;
uint_t accessed : 1;
- uint_t reserved : 1;
- uint_t large_page : 1;
+ uint_t avail : 1;
+ uint_t large_page : 1;
uint_t zero : 1;
uint_t vmm_info : 3;
ullong_t pd_base_addr : 40;
} __attribute__((packed)) pdpe64_t;
+// We Don't support this
+typedef struct pdpe64_1GB {
+ uint_t present : 1;
+ uint_t writable : 1;
+ uint_t user_page : 1;
+ uint_t write_through : 1;
+ uint_t cache_disable : 1;
+ uint_t accessed : 1;
+ uint_t dirty : 1;
+ uint_t large_page : 1;
+ uint_t global_page : 1;
+ uint_t vmm_info : 3;
+ uint_t pat : 1;
+ uint_t rsvd : 17;
+ ullong_t page_base_addr : 22;
+ uint_t available : 11;
+ uint_t no_execute : 1;
+} __attribute__((packed)) pdpe64_1GB_t;
+
typedef struct pde64 {
uint_t write_through : 1;
uint_t cache_disable : 1;
uint_t accessed : 1;
- uint_t reserved : 1;
- uint_t large_page : 1;
- uint_t reserved2 : 1;
+ uint_t avail : 1;
+ uint_t large_page : 1;
+ uint_t global_page : 1;
uint_t vmm_info : 3;
ullong_t pt_base_addr : 40;
uint_t available : 11;
uint_t no_execute : 1;
} __attribute__((packed)) pde64_t;
+typedef struct pde64_2MB {
+ uint_t present : 1;
+ uint_t writable : 1;
+ uint_t user_page : 1;
+ uint_t write_through : 1;
+ uint_t cache_disable : 1;
+ uint_t accessed : 1;
+ uint_t dirty : 1;
+ uint_t large_page : 1;
+ uint_t global_page : 1;
+ uint_t vmm_info : 3;
+ uint_t pat : 1;
+ uint_t rsvd : 8;
+ ullong_t page_base_addr : 31;
+ uint_t available : 11;
+ uint_t no_execute : 1;
+} __attribute__((packed)) pde64_2MB_t;
+
+
typedef struct pte64 {
uint_t present : 1;
uint_t writable : 1;
void delete_page_tables_32PAE(pdpe32pae_t * pdpe);
void delete_page_tables_64(pml4e64_t * pml4);
-pde32_entry_type_t pde32_lookup(pde32_t * pd, addr_t addr, addr_t * entry);
-int pte32_lookup(pte32_t * pte, addr_t addr, addr_t * entry);
+pt_entry_type_t pde32_lookup(pde32_t * pd, addr_t addr, addr_t * entry);
+pt_entry_type_t pte32_lookup(pte32_t * pt, addr_t addr, addr_t * entry);
+
+pt_entry_type_t pdpe32pae_lookup(pdpe32pae_t * pdp, addr_t addr, addr_t * entry);
+pt_entry_type_t pde32pae_lookup(pde32pae_t * pd, addr_t addr, addr_t * entry);
+pt_entry_type_t pte32pae_lookup(pte32pae_t * pt, addr_t addr, addr_t * entry);
+
+pt_entry_type_t pml4e64_lookup(pml4e64_t * pml, addr_t addr, addr_t * entry);
+pt_entry_type_t pdpe64_lookup(pdpe64_t * pdp, addr_t addr, addr_t * entry);
+pt_entry_type_t pde64_lookup(pde64_t * pd, addr_t addr, addr_t * entry);
+pt_entry_type_t pte64_lookup(pte64_t * pt, addr_t addr, addr_t * entry);
+
+
-// This assumes that the page table resides in the host address space
-// IE. IT DOES NO VM ADDR TRANSLATION
-int pt32_lookup(pde32_t * pd, addr_t vaddr, addr_t * paddr);
}
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;
if (guest_pa_to_host_va(info, guest_pte_pa, (addr_t*)&guest_pte) == -1) {
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:
+ case PT_ENTRY_PAGE:
if (pte32_lookup(host_pte, vaddr, paddr) == -1) {
return -1;
}
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:
- *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;
- }
- }
+
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);
- pte32_t * host_pte = 0;
-
- switch (pde32_lookup(host_pde, vaddr, (addr_t *)&host_pte)) {
- case PDE32_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;
- }
- }
+
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;
-
- if (pd == 0) {
- 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;
- }
-
- 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);
- return 0;
+ *entry = BASE_TO_PAGE_ADDR(pdpe_entry->pd_base_addr);
+ return PT_ENTRY_PAGE;
}
+}
- return -1;
+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;
+ }
}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
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)];
if (entry->present == 0) {
- return PT_ENTRY_NOT_PRESENT;
+ return PT_ACCESS_NOT_PRESENT;
} else if ((entry->writable == 0) && (access_type.write == 1)) {
- return PT_WRITE_ERROR;
+ return PT_ACCESS_WRITE_ERROR;
} else if ((entry->user_page == 0) && (access_type.user == 1)) {
// Check CR0.WP?
- return PT_USER_ERROR;
+ return PT_ACCESS_USER_ERROR;
}
return PT_ACCESS_OK;
pte32_t * entry = &pte[PTE32_INDEX(addr)];
if (entry->present == 0) {
- return PT_ENTRY_NOT_PRESENT;
+ return PT_ACCESS_NOT_PRESENT;
} else if ((entry->writable == 0) && (access_type.write == 1)) {
- return PT_WRITE_ERROR;
+ return PT_ACCESS_WRITE_ERROR;
} else if ((entry->user_page == 0) && (access_type.user == 1)) {
// Check CR0.WP?
- return PT_USER_ERROR;
+ return PT_ACCESS_USER_ERROR;
}
return PT_ACCESS_OK;
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;
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;
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;
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;
if (guest_access != PT_ACCESS_OK) {
// Guest Access Error
- if ((shadow_access != PT_ENTRY_NOT_PRESENT) &&
- (guest_access != PT_ENTRY_NOT_PRESENT)) {
+ if ((shadow_access != PT_ACCESS_NOT_PRESENT) &&
+ (guest_access != PT_ACCESS_NOT_PRESENT)) {
// aka (guest permission error)
return 1;
}
- if ((shadow_access == PT_ENTRY_NOT_PRESENT) &&
- (guest_access == PT_ENTRY_NOT_PRESENT)) {
+ if ((shadow_access == PT_ACCESS_NOT_PRESENT) &&
+ (guest_access == PT_ACCESS_NOT_PRESENT)) {
// Page tables completely blank, handle guest first
return 1;
}
}
- if (shadow_pde_access == PT_ENTRY_NOT_PRESENT)
+ if (shadow_pde_access == PT_ACCESS_NOT_PRESENT)
{
pte32_t * shadow_pt = (pte32_t *)v3_create_new_shadow_pt();
}
}
}
- else if ((shadow_pde_access == PT_WRITE_ERROR) &&
+ else if ((shadow_pde_access == PT_ACCESS_WRITE_ERROR) &&
(guest_pde->large_page == 1) &&
(((pde32_4MB_t *)guest_pde)->dirty == 0))
{
return 0;
}
- else if (shadow_pde_access == PT_USER_ERROR)
+ else if (shadow_pde_access == PT_ACCESS_USER_ERROR)
{
//
// Page Directory Entry marked non-user
}
- if (shadow_pte_access == PT_ENTRY_NOT_PRESENT) {
+ if (shadow_pte_access == PT_ACCESS_NOT_PRESENT) {
// Get the guest physical address of the fault
addr_t guest_fault_pa = BASE_TO_PAGE_ADDR_4MB(large_guest_pde->page_base_addr) + PAGE_OFFSET_4MB(fault_addr);
host_region_type_t host_page_type = get_shadow_addr_type(info, guest_fault_pa);
return -1;
}
}
- } else if ((shadow_pte_access == PT_WRITE_ERROR) &&
+ } else if ((shadow_pte_access == PT_ACCESS_WRITE_ERROR) &&
(shadow_pte->vmm_info == PT32_GUEST_PT)) {
struct shadow_page_state * state = &(info->shdw_pg_state);
}
- if (shadow_pte_access == PT_ENTRY_NOT_PRESENT) {
+ if (shadow_pte_access == PT_ACCESS_NOT_PRESENT) {
addr_t guest_pa = BASE_TO_PAGE_ADDR((addr_t)(guest_pte->page_base_addr)) + PAGE_OFFSET(fault_addr);
}
}
- } else if ((shadow_pte_access == PT_WRITE_ERROR) &&
+ } else if ((shadow_pte_access == PT_ACCESS_WRITE_ERROR) &&
(guest_pte->dirty == 0)) {
PrintDebug("Shadow PTE Write Error\n");