1 #include <palacios/vmm_paging.h>
3 #include <palacios/vmm.h>
5 #include <palacios/vm_guest_mem.h>
8 extern struct vmm_os_hooks * os_hooks;
10 void delete_page_tables_pde32(pde32_t * pde) {
17 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
19 pte32_t * pte = (pte32_t *)(pde[i].pt_base_addr << PAGE_POWER);
22 for (j = 0; (j < MAX_PTE32_ENTRIES); j++) {
23 if ((pte[j].present)) {
24 os_hooks->free_page((void *)(pte[j].page_base_addr << PAGE_POWER));
28 //PrintDebug("Deleting PTE %d (%x)\n", i, pte);
29 os_hooks->free_page(pte);
33 // PrintDebug("Deleting PDE (%x)\n", pde);
34 os_hooks->free_page(pde);
41 int pt32_lookup(pde32_t * pd, addr_t vaddr, addr_t * paddr) {
43 pde32_entry_type_t pde_entry_type;
49 pde_entry_type = pde32_lookup(pd, vaddr, &pde_entry);
51 if (pde_entry_type == PDE32_ENTRY_PTE32) {
52 return pte32_lookup((pte32_t *)pde_entry, vaddr, paddr);
53 } else if (pde_entry_type == PDE32_ENTRY_LARGE_PAGE) {
63 /* We can't do a full lookup because we don't know what context the page tables are in...
64 * The entry addresses could be pointing to either guest physical memory or host physical memory
65 * Instead we just return the entry address, and a flag to show if it points to a pte or a large page...
67 pde32_entry_type_t pde32_lookup(pde32_t * pd, addr_t addr, addr_t * entry) {
68 pde32_t * pde_entry = &(pd[PDE32_INDEX(addr)]);
70 if (!pde_entry->present) {
72 return PDE32_ENTRY_NOT_PRESENT;
75 if (pde_entry->large_page) {
76 pde32_4MB_t * large_pde = (pde32_4MB_t *)pde_entry;
78 *entry = PDE32_4MB_T_ADDR(*large_pde);
79 *entry += PD32_4MB_PAGE_OFFSET(addr);
80 return PDE32_ENTRY_LARGE_PAGE;
82 *entry = PDE32_T_ADDR(*pde_entry);
83 return PDE32_ENTRY_PTE32;
86 return PDE32_ENTRY_NOT_PRESENT;
91 /* Takes a virtual addr (addr) and returns the physical addr (entry) as defined in the page table
93 int pte32_lookup(pte32_t * pt, addr_t addr, addr_t * entry) {
94 pte32_t * pte_entry = &(pt[PTE32_INDEX(addr)]);
96 if (!pte_entry->present) {
98 PrintDebug("Lookup at non present page (index=%d)\n", PTE32_INDEX(addr));
101 *entry = PTE32_T_ADDR(*pte_entry) + PT32_PAGE_OFFSET(addr);
110 pt_access_status_t can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type) {
111 pde32_t * entry = &pde[PDE32_INDEX(addr)];
113 if (entry->present == 0) {
114 return PT_ENTRY_NOT_PRESENT;
115 } else if ((entry->writable == 0) && (access_type.write == 1)) {
116 return PT_WRITE_ERROR;
117 } else if ((entry->user_page == 0) && (access_type.user == 1)) {
119 return PT_USER_ERROR;
126 pt_access_status_t can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t access_type) {
127 pte32_t * entry = &pte[PTE32_INDEX(addr)];
129 if (entry->present == 0) {
130 return PT_ENTRY_NOT_PRESENT;
131 } else if ((entry->writable == 0) && (access_type.write == 1)) {
132 return PT_WRITE_ERROR;
133 } else if ((entry->user_page == 0) && (access_type.user == 1)) {
135 return PT_USER_ERROR;
144 /* We generate a page table to correspond to a given memory layout
145 * pulling pages from the mem_list when necessary
146 * If there are any gaps in the layout, we add them as unmapped pages
148 pde32_t * create_passthrough_pde32_pts(struct guest_info * guest_info) {
149 ullong_t current_page_addr = 0;
151 struct shadow_map * map = &(guest_info->mem_map);
153 pde32_t * pde = os_hooks->allocate_pages(1);
155 for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
157 pte32_t * pte = os_hooks->allocate_pages(1);
160 for (j = 0; j < MAX_PTE32_ENTRIES; j++) {
161 struct shadow_region * region = get_shadow_region_by_addr(map, current_page_addr);
164 (region->host_type == HOST_REGION_HOOK) ||
165 (region->host_type == HOST_REGION_UNALLOCATED) ||
166 (region->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) ||
167 (region->host_type == HOST_REGION_REMOTE) ||
168 (region->host_type == HOST_REGION_SWAPPED)) {
171 pte[j].user_page = 0;
172 pte[j].write_through = 0;
173 pte[j].cache_disable = 0;
177 pte[j].global_page = 0;
179 pte[j].page_base_addr = 0;
184 pte[j].user_page = 1;
185 pte[j].write_through = 0;
186 pte[j].cache_disable = 0;
190 pte[j].global_page = 0;
193 if (guest_pa_to_host_pa(guest_info, current_page_addr, &host_addr) == -1) {
199 pte[j].page_base_addr = host_addr >> 12;
204 current_page_addr += PAGE_SIZE;
207 if (pte_present == 0) {
208 os_hooks->free_page(pte);
212 pde[i].user_page = 0;
213 pde[i].write_through = 0;
214 pde[i].cache_disable = 0;
217 pde[i].large_page = 0;
218 pde[i].global_page = 0;
220 pde[i].pt_base_addr = 0;
224 pde[i].user_page = 1;
225 pde[i].write_through = 0;
226 pde[i].cache_disable = 0;
229 pde[i].large_page = 0;
230 pde[i].global_page = 0;
232 pde[i].pt_base_addr = PAGE_ALIGNED_ADDR(pte);
245 void PrintPDE32(addr_t virtual_address, pde32_t * pde)
247 PrintDebug("PDE %p -> %p : present=%x, writable=%x, user=%x, wt=%x, cd=%x, accessed=%x, reserved=%x, largePages=%x, globalPage=%x, kernelInfo=%x\n",
249 (void *) (pde->pt_base_addr << PAGE_POWER),
262 void PrintPTE32(addr_t virtual_address, pte32_t * pte)
264 PrintDebug("PTE %p -> %p : present=%x, writable=%x, user=%x, wt=%x, cd=%x, accessed=%x, dirty=%x, pteAttribute=%x, globalPage=%x, vmm_info=%x\n",
266 (void*)(pte->page_base_addr << PAGE_POWER),
281 void PrintPD32(pde32_t * pde)
285 PrintDebug("Page Directory at %p:\n", pde);
286 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
287 if ( pde[i].present) {
288 PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
293 void PrintPT32(addr_t starting_address, pte32_t * pte)
297 PrintDebug("Page Table at %p:\n", pte);
298 for (i = 0; (i < MAX_PTE32_ENTRIES) ; i++) {
299 if (pte[i].present) {
300 PrintPTE32(starting_address + (PAGE_SIZE * i), &(pte[i]));
309 void PrintDebugPageTables(pde32_t * pde)
313 PrintDebug("Dumping the pages starting with the pde page at %p\n", pde);
315 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
316 if (pde[i].present) {
317 PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
318 PrintPT32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), (pte32_t *)(pde[i].pt_base_addr << PAGE_POWER));