2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_paging.h>
22 #include <palacios/vmm.h>
24 #include <palacios/vm_guest_mem.h>
29 void delete_page_tables_pde32(pde32_t * pde) {
36 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
38 pte32_t * pte = (pte32_t *)(pde[i].pt_base_addr << PAGE_POWER);
41 for (j = 0; (j < MAX_PTE32_ENTRIES); j++) {
42 if ((pte[j].present)) {
43 os_hooks->free_page((void *)(pte[j].page_base_addr << PAGE_POWER));
47 //PrintDebug("Deleting PTE %d (%x)\n", i, pte);
52 // PrintDebug("Deleting PDE (%x)\n", pde);
60 int pt32_lookup(pde32_t * pd, addr_t vaddr, addr_t * paddr) {
62 pde32_entry_type_t pde_entry_type;
68 pde_entry_type = pde32_lookup(pd, vaddr, &pde_entry);
70 if (pde_entry_type == PDE32_ENTRY_PTE32) {
71 return pte32_lookup((pte32_t *)pde_entry, vaddr, paddr);
72 } else if (pde_entry_type == PDE32_ENTRY_LARGE_PAGE) {
82 /* We can't do a full lookup because we don't know what context the page tables are in...
83 * The entry addresses could be pointing to either guest physical memory or host physical memory
84 * Instead we just return the entry address, and a flag to show if it points to a pte or a large page...
86 pde32_entry_type_t pde32_lookup(pde32_t * pd, addr_t addr, addr_t * entry) {
87 pde32_t * pde_entry = &(pd[PDE32_INDEX(addr)]);
89 if (!pde_entry->present) {
91 return PDE32_ENTRY_NOT_PRESENT;
94 if (pde_entry->large_page) {
95 pde32_4MB_t * large_pde = (pde32_4MB_t *)pde_entry;
97 *entry = PDE32_4MB_T_ADDR(*large_pde);
98 *entry += PD32_4MB_PAGE_OFFSET(addr);
99 return PDE32_ENTRY_LARGE_PAGE;
101 *entry = PDE32_T_ADDR(*pde_entry);
102 return PDE32_ENTRY_PTE32;
105 return PDE32_ENTRY_NOT_PRESENT;
110 /* Takes a virtual addr (addr) and returns the physical addr (entry) as defined in the page table
112 int pte32_lookup(pte32_t * pt, addr_t addr, addr_t * entry) {
113 pte32_t * pte_entry = &(pt[PTE32_INDEX(addr)]);
115 if (!pte_entry->present) {
117 PrintDebug("Lookup at non present page (index=%d)\n", PTE32_INDEX(addr));
120 *entry = PTE32_T_ADDR(*pte_entry) + PT32_PAGE_OFFSET(addr);
129 pt_access_status_t can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type) {
130 pde32_t * entry = &pde[PDE32_INDEX(addr)];
132 if (entry->present == 0) {
133 return PT_ENTRY_NOT_PRESENT;
134 } else if ((entry->writable == 0) && (access_type.write == 1)) {
135 return PT_WRITE_ERROR;
136 } else if ((entry->user_page == 0) && (access_type.user == 1)) {
138 return PT_USER_ERROR;
145 pt_access_status_t can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t access_type) {
146 pte32_t * entry = &pte[PTE32_INDEX(addr)];
148 if (entry->present == 0) {
149 return PT_ENTRY_NOT_PRESENT;
150 } else if ((entry->writable == 0) && (access_type.write == 1)) {
151 return PT_WRITE_ERROR;
152 } else if ((entry->user_page == 0) && (access_type.user == 1)) {
154 return PT_USER_ERROR;
163 /* We generate a page table to correspond to a given memory layout
164 * pulling pages from the mem_list when necessary
165 * If there are any gaps in the layout, we add them as unmapped pages
167 pde32_t * create_passthrough_pde32_pts(struct guest_info * guest_info) {
168 ullong_t current_page_addr = 0;
170 struct shadow_map * map = &(guest_info->mem_map);
172 pde32_t * pde = V3_AllocPages(1);
174 for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
176 pte32_t * pte = V3_AllocPages(1);
179 for (j = 0; j < MAX_PTE32_ENTRIES; j++) {
180 struct shadow_region * region = get_shadow_region_by_addr(map, current_page_addr);
183 (region->host_type == HOST_REGION_HOOK) ||
184 (region->host_type == HOST_REGION_UNALLOCATED) ||
185 (region->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) ||
186 (region->host_type == HOST_REGION_REMOTE) ||
187 (region->host_type == HOST_REGION_SWAPPED)) {
190 pte[j].user_page = 0;
191 pte[j].write_through = 0;
192 pte[j].cache_disable = 0;
196 pte[j].global_page = 0;
198 pte[j].page_base_addr = 0;
203 pte[j].user_page = 1;
204 pte[j].write_through = 0;
205 pte[j].cache_disable = 0;
209 pte[j].global_page = 0;
212 if (guest_pa_to_host_pa(guest_info, current_page_addr, &host_addr) == -1) {
218 pte[j].page_base_addr = host_addr >> 12;
223 current_page_addr += PAGE_SIZE;
226 if (pte_present == 0) {
231 pde[i].user_page = 0;
232 pde[i].write_through = 0;
233 pde[i].cache_disable = 0;
236 pde[i].large_page = 0;
237 pde[i].global_page = 0;
239 pde[i].pt_base_addr = 0;
243 pde[i].user_page = 1;
244 pde[i].write_through = 0;
245 pde[i].cache_disable = 0;
248 pde[i].large_page = 0;
249 pde[i].global_page = 0;
251 pde[i].pt_base_addr = PAGE_ALIGNED_ADDR(pte);
264 void PrintPDE32(addr_t virtual_address, pde32_t * pde)
266 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",
268 (void *) (pde->pt_base_addr << PAGE_POWER),
281 void PrintPTE32(addr_t virtual_address, pte32_t * pte)
283 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",
285 (void*)(pte->page_base_addr << PAGE_POWER),
300 void PrintPD32(pde32_t * pde)
304 PrintDebug("Page Directory at %p:\n", pde);
305 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
306 if ( pde[i].present) {
307 PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
312 void PrintPT32(addr_t starting_address, pte32_t * pte)
316 PrintDebug("Page Table at %p:\n", pte);
317 for (i = 0; (i < MAX_PTE32_ENTRIES) ; i++) {
318 if (pte[i].present) {
319 PrintPTE32(starting_address + (PAGE_SIZE * i), &(pte[i]));
328 void PrintDebugPageTables(pde32_t * pde)
332 PrintDebug("Dumping the pages starting with the pde page at %p\n", pde);
334 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
335 if (pde[i].present) {
336 PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
337 PrintPT32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), (pte32_t *)(pde[i].pt_base_addr << PAGE_POWER));