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>
32 void delete_page_tables_pde32(pde32_t * pde) {
39 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
41 // We double cast, first to an addr_t to handle 64 bit issues, then to the pointer
42 PrintDebug("PTE base addr %x \n", pde[i].pt_base_addr);
43 pte32_t * pte = (pte32_t *)((addr_t)(uint_t)(pde[i].pt_base_addr << PAGE_POWER));
46 for (j = 0; (j < MAX_PTE32_ENTRIES); j++) {
47 if ((pte[j].present)) {
48 os_hooks->free_page((void *)(pte[j].page_base_addr << PAGE_POWER));
52 PrintDebug("Deleting PTE %d (%p)\n", i, pte);
57 PrintDebug("Deleting PDE (%p)\n", pde);
58 V3_FreePage(V3_PAddr(pde));
65 int pt32_lookup(pde32_t * pd, addr_t vaddr, addr_t * paddr) {
67 pde32_entry_type_t pde_entry_type;
73 pde_entry_type = pde32_lookup(pd, vaddr, &pde_entry);
75 if (pde_entry_type == PDE32_ENTRY_PTE32) {
76 return pte32_lookup((pte32_t *)pde_entry, vaddr, paddr);
77 } else if (pde_entry_type == PDE32_ENTRY_LARGE_PAGE) {
87 /* We can't do a full lookup because we don't know what context the page tables are in...
88 * The entry addresses could be pointing to either guest physical memory or host physical memory
89 * Instead we just return the entry address, and a flag to show if it points to a pte or a large page...
91 pde32_entry_type_t pde32_lookup(pde32_t * pd, addr_t addr, addr_t * entry) {
92 pde32_t * pde_entry = &(pd[PDE32_INDEX(addr)]);
94 if (!pde_entry->present) {
96 return PDE32_ENTRY_NOT_PRESENT;
99 if (pde_entry->large_page) {
100 pde32_4MB_t * large_pde = (pde32_4MB_t *)pde_entry;
102 *entry = PDE32_4MB_T_ADDR(*large_pde);
103 *entry += PD32_4MB_PAGE_OFFSET(addr);
104 return PDE32_ENTRY_LARGE_PAGE;
106 *entry = PDE32_T_ADDR(*pde_entry);
107 return PDE32_ENTRY_PTE32;
110 return PDE32_ENTRY_NOT_PRESENT;
115 /* Takes a virtual addr (addr) and returns the physical addr (entry) as defined in the page table
117 int pte32_lookup(pte32_t * pt, addr_t addr, addr_t * entry) {
118 pte32_t * pte_entry = &(pt[PTE32_INDEX(addr)]);
120 if (!pte_entry->present) {
122 PrintDebug("Lookup at non present page (index=%d)\n", PTE32_INDEX(addr));
125 *entry = PTE32_T_ADDR(*pte_entry) + PT32_PAGE_OFFSET(addr);
134 pt_access_status_t can_access_pde32(pde32_t * pde, addr_t addr, pf_error_t access_type) {
135 pde32_t * entry = &pde[PDE32_INDEX(addr)];
137 if (entry->present == 0) {
138 return PT_ENTRY_NOT_PRESENT;
139 } else if ((entry->writable == 0) && (access_type.write == 1)) {
140 return PT_WRITE_ERROR;
141 } else if ((entry->user_page == 0) && (access_type.user == 1)) {
143 return PT_USER_ERROR;
150 pt_access_status_t can_access_pte32(pte32_t * pte, addr_t addr, pf_error_t access_type) {
151 pte32_t * entry = &pte[PTE32_INDEX(addr)];
153 if (entry->present == 0) {
154 return PT_ENTRY_NOT_PRESENT;
155 } else if ((entry->writable == 0) && (access_type.write == 1)) {
156 return PT_WRITE_ERROR;
157 } else if ((entry->user_page == 0) && (access_type.user == 1)) {
159 return PT_USER_ERROR;
168 /* We generate a page table to correspond to a given memory layout
169 * pulling pages from the mem_list when necessary
170 * If there are any gaps in the layout, we add them as unmapped pages
172 pde32_t * create_passthrough_pts_32(struct guest_info * guest_info) {
173 addr_t current_page_addr = 0;
175 struct shadow_map * map = &(guest_info->mem_map);
177 pde32_t * pde = V3_VAddr(V3_AllocPages(1));
179 for (i = 0; i < MAX_PDE32_ENTRIES; i++) {
181 pte32_t * pte = V3_VAddr(V3_AllocPages(1));
184 for (j = 0; j < MAX_PTE32_ENTRIES; j++) {
185 struct shadow_region * region = get_shadow_region_by_addr(map, current_page_addr);
188 (region->host_type == HOST_REGION_HOOK) ||
189 (region->host_type == HOST_REGION_UNALLOCATED) ||
190 (region->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) ||
191 (region->host_type == HOST_REGION_REMOTE) ||
192 (region->host_type == HOST_REGION_SWAPPED)) {
195 pte[j].user_page = 0;
196 pte[j].write_through = 0;
197 pte[j].cache_disable = 0;
201 pte[j].global_page = 0;
203 pte[j].page_base_addr = 0;
208 pte[j].user_page = 1;
209 pte[j].write_through = 0;
210 pte[j].cache_disable = 0;
214 pte[j].global_page = 0;
217 if (guest_pa_to_host_pa(guest_info, current_page_addr, &host_addr) == -1) {
223 pte[j].page_base_addr = host_addr >> 12;
228 current_page_addr += PAGE_SIZE;
231 if (pte_present == 0) {
232 V3_FreePage(V3_PAddr(pte));
236 pde[i].user_page = 0;
237 pde[i].write_through = 0;
238 pde[i].cache_disable = 0;
241 pde[i].large_page = 0;
242 pde[i].global_page = 0;
244 pde[i].pt_base_addr = 0;
248 pde[i].user_page = 1;
249 pde[i].write_through = 0;
250 pde[i].cache_disable = 0;
253 pde[i].large_page = 0;
254 pde[i].global_page = 0;
256 pde[i].pt_base_addr = PAGE_ALIGNED_ADDR((addr_t)V3_PAddr(pte));
267 pml4e64_t * create_passthrough_pts_64(struct guest_info * info) {
268 addr_t current_page_addr = 0;
270 struct shadow_map * map = &(info->mem_map);
272 pml4e64_t * pml = V3_VAddr(V3_AllocPages(1));
274 for (i = 0; i < 1; i++) {
275 int pdpe_present = 0;
276 pdpe64_t * pdpe = V3_VAddr(V3_AllocPages(1));
278 for (j = 0; j < 1; j++) {
280 pde64_t * pde = V3_VAddr(V3_AllocPages(1));
282 for (k = 0; k < MAX_PDE64_ENTRIES; k++) {
284 pte64_t * pte = V3_VAddr(V3_AllocPages(1));
287 for (m = 0; m < MAX_PTE64_ENTRIES; m++) {
288 struct shadow_region * region = get_shadow_region_by_addr(map, current_page_addr);
293 (region->host_type == HOST_REGION_HOOK) ||
294 (region->host_type == HOST_REGION_UNALLOCATED) ||
295 (region->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) ||
296 (region->host_type == HOST_REGION_REMOTE) ||
297 (region->host_type == HOST_REGION_SWAPPED)) {
300 pte[m].user_page = 0;
301 pte[m].write_through = 0;
302 pte[m].cache_disable = 0;
306 pte[m].global_page = 0;
308 pte[m].page_base_addr = 0;
313 pte[m].user_page = 1;
314 pte[m].write_through = 0;
315 pte[m].cache_disable = 0;
319 pte[m].global_page = 0;
322 if (guest_pa_to_host_pa(info, current_page_addr, &host_addr) == -1) {
328 pte[m].page_base_addr = PTE64_BASE_ADDR(host_addr);
330 //PrintPTE64(current_page_addr, &(pte[m]));
338 current_page_addr += PAGE_SIZE;
341 if (pte_present == 0) {
342 V3_FreePage(V3_PAddr(pte));
346 pde[k].user_page = 0;
347 pde[k].write_through = 0;
348 pde[k].cache_disable = 0;
351 pde[k].large_page = 0;
352 //pde[k].global_page = 0;
354 pde[k].pt_base_addr = 0;
358 pde[k].user_page = 1;
359 pde[k].write_through = 0;
360 pde[k].cache_disable = 0;
363 pde[k].large_page = 0;
364 //pde[k].global_page = 0;
366 pde[k].pt_base_addr = PAGE_ALIGNED_ADDR((addr_t)V3_PAddr(pte));
372 if (pde_present == 0) {
373 V3_FreePage(V3_PAddr(pde));
376 pdpe[j].writable = 0;
377 pdpe[j].user_page = 0;
378 pdpe[j].write_through = 0;
379 pdpe[j].cache_disable = 0;
380 pdpe[j].accessed = 0;
381 pdpe[j].reserved = 0;
382 pdpe[j].large_page = 0;
383 //pdpe[j].global_page = 0;
384 pdpe[j].vmm_info = 0;
385 pdpe[j].pd_base_addr = 0;
388 pdpe[j].writable = 1;
389 pdpe[j].user_page = 1;
390 pdpe[j].write_through = 0;
391 pdpe[j].cache_disable = 0;
392 pdpe[j].accessed = 0;
393 pdpe[j].reserved = 0;
394 pdpe[j].large_page = 0;
395 //pdpe[j].global_page = 0;
396 pdpe[j].vmm_info = 0;
397 pdpe[j].pd_base_addr = PAGE_ALIGNED_ADDR((addr_t)V3_PAddr(pde));
405 PrintDebug("PML index=%d\n", i);
407 if (pdpe_present == 0) {
408 V3_FreePage(V3_PAddr(pdpe));
412 pml[i].user_page = 0;
413 pml[i].write_through = 0;
414 pml[i].cache_disable = 0;
417 //pml[i].large_page = 0;
418 //pml[i].global_page = 0;
420 pml[i].pdp_base_addr = 0;
424 pml[i].user_page = 1;
425 pml[i].write_through = 0;
426 pml[i].cache_disable = 0;
429 //pml[i].large_page = 0;
430 //pml[i].global_page = 0;
432 pml[i].pdp_base_addr = PAGE_ALIGNED_ADDR((addr_t)V3_PAddr(pdpe));
443 void PrintPDE32(addr_t virtual_address, pde32_t * pde)
445 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",
446 (void *)virtual_address,
447 (void *)(addr_t) (pde->pt_base_addr << PAGE_POWER),
461 void PrintPTE32(addr_t virtual_address, pte32_t * pte)
463 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",
464 (void *)virtual_address,
465 (void*)(addr_t)(pte->page_base_addr << PAGE_POWER),
479 void PrintPDE64(addr_t virtual_address, pde64_t * pde)
481 PrintDebug("PDE64 %p -> %p : present=%x, writable=%x, user=%x, wt=%x, cd=%x, accessed=%x, reserved=%x, largePages=%x, globalPage=%x, kernelInfo=%x\n",
482 (void *)virtual_address,
483 (void *)(addr_t) (pde->pt_base_addr << PAGE_POWER),
492 0,//pde->global_page,
497 void PrintPTE64(addr_t virtual_address, pte64_t * pte)
499 PrintDebug("PTE64 %p -> %p : present=%x, writable=%x, user=%x, wt=%x, cd=%x, accessed=%x, dirty=%x, pteAttribute=%x, globalPage=%x, vmm_info=%x\n",
500 (void *)virtual_address,
501 (void*)(addr_t)(pte->page_base_addr << PAGE_POWER),
519 void PrintPD32(pde32_t * pde)
523 PrintDebug("Page Directory at %p:\n", pde);
524 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
525 if ( pde[i].present) {
526 PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
531 void PrintPT32(addr_t starting_address, pte32_t * pte)
535 PrintDebug("Page Table at %p:\n", pte);
536 for (i = 0; (i < MAX_PTE32_ENTRIES) ; i++) {
537 if (pte[i].present) {
538 PrintPTE32(starting_address + (PAGE_SIZE * i), &(pte[i]));
547 void PrintDebugPageTables(pde32_t * pde)
551 PrintDebug("Dumping the pages starting with the pde page at %p\n", pde);
553 for (i = 0; (i < MAX_PDE32_ENTRIES); i++) {
554 if (pde[i].present) {
555 PrintPDE32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), &(pde[i]));
556 PrintPT32((addr_t)(PAGE_SIZE * MAX_PTE32_ENTRIES * i), (pte32_t *)V3_VAddr((void *)(addr_t)(pde[i].pt_base_addr << PAGE_POWER)));