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;
74 *entry = PAGE_ADDR(pde_entry->pt_base_addr);
76 if (pde_entry->large_page) {
77 *entry += PAGE_OFFSET(addr);
78 return PDE32_ENTRY_LARGE_PAGE;
80 *entry = PDE32_T_ADDR(*pde_entry);
81 return PDE32_ENTRY_PTE32;
84 return PDE32_ENTRY_NOT_PRESENT;
89 /* Takes a virtual addr (addr) and returns the physical addr (entry) as defined in the page table
91 int pte32_lookup(pte32_t * pt, addr_t addr, addr_t * entry) {
92 pte32_t * pte_entry = &(pt[PTE32_INDEX(addr)]);
94 if (!pte_entry->present) {
96 PrintDebug("Lookup at non present page (index=%d)\n", PTE32_INDEX(addr));
99 *entry = PTE32_T_ADDR(*pte_entry) + PT32_PAGE_OFFSET(addr);
108 pt_access_status_t can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type) {
109 pde32_t * entry = &pde[PDE32_INDEX(addr)];
111 if (entry->present == 0) {
112 return PT_ENTRY_NOT_PRESENT;
113 } else if ((entry->writable == 0) && (access_type.write == 1)) {
114 return PT_WRITE_ERROR;
115 } else if ((entry->user_page == 0) && (access_type.user == 1)) {
117 return PT_USER_ERROR;
124 pt_access_status_t can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t access_type) {
125 pte32_t * entry = &pte[PTE32_INDEX(addr)];
127 if (entry->present == 0) {
128 return PT_ENTRY_NOT_PRESENT;
129 } else if ((entry->writable == 0) && (access_type.write == 1)) {
130 return PT_WRITE_ERROR;
131 } else if ((entry->user_page == 0) && (access_type.user == 1)) {
133 return PT_USER_ERROR;
142 /* We generate a page table to correspond to a given memory layout
143 * pulling pages from the mem_list when necessary
144 * If there are any gaps in the layout, we add them as unmapped pages
146 pde32_t * create_passthrough_pde32_pts(struct guest_info * guest_info) {
147 ullong_t current_page_addr = 0;
149 struct shadow_map * map = &(guest_info->mem_map);
151 pde32_t * pde = os_hooks->allocate_pages(1);
153 for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
155 pte32_t * pte = os_hooks->allocate_pages(1);
158 for (j = 0; j < MAX_PTE32_ENTRIES; j++) {
159 struct shadow_region * region = get_shadow_region_by_addr(map, current_page_addr);
162 (region->host_type == HOST_REGION_HOOK) ||
163 (region->host_type == HOST_REGION_UNALLOCATED) ||
164 (region->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) ||
165 (region->host_type == HOST_REGION_REMOTE) ||
166 (region->host_type == HOST_REGION_SWAPPED)) {
169 pte[j].user_page = 0;
170 pte[j].write_through = 0;
171 pte[j].cache_disable = 0;
175 pte[j].global_page = 0;
177 pte[j].page_base_addr = 0;
182 pte[j].user_page = 1;
183 pte[j].write_through = 0;
184 pte[j].cache_disable = 0;
188 pte[j].global_page = 0;
191 if (guest_pa_to_host_pa(guest_info, current_page_addr, &host_addr) == -1) {
197 pte[j].page_base_addr = host_addr >> 12;
202 current_page_addr += PAGE_SIZE;
205 if (pte_present == 0) {
206 os_hooks->free_page(pte);
210 pde[i].user_page = 0;
211 pde[i].write_through = 0;
212 pde[i].cache_disable = 0;
215 pde[i].large_page = 0;
216 pde[i].global_page = 0;
218 pde[i].pt_base_addr = 0;
222 pde[i].user_page = 1;
223 pde[i].write_through = 0;
224 pde[i].cache_disable = 0;
227 pde[i].large_page = 0;
228 pde[i].global_page = 0;
230 pde[i].pt_base_addr = PAGE_ALIGNED_ADDR(pte);
243 void PrintPDE32(addr_t virtual_address, pde32_t * pde)
245 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",
247 (void *) (pde->pt_base_addr << PAGE_POWER),
260 void PrintPTE32(addr_t virtual_address, pte32_t * pte)
262 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",
264 (void*)(pte->page_base_addr << PAGE_POWER),
279 void PrintPD32(pde32_t * pde)
283 PrintDebug("Page Directory at %p:\n", pde);
284 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
285 if ( pde[i].present) {
286 PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
291 void PrintPT32(addr_t starting_address, pte32_t * pte)
295 PrintDebug("Page Table at %p:\n", pte);
296 for (i = 0; (i < MAX_PTE32_ENTRIES) ; i++) {
297 if (pte[i].present) {
298 PrintPTE32(starting_address + (PAGE_SIZE * i), &(pte[i]));
307 void PrintDebugPageTables(pde32_t * pde)
311 PrintDebug("Dumping the pages starting with the pde page at %p\n", pde);
313 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
314 if (pde[i].present) {
315 PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
316 PrintPT32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), (pte32_t *)(pde[i].pt_base_addr << PAGE_POWER));