Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


changed shadow memory map to use Red Black Tree
[palacios.git] / palacios / src / palacios / vmm_shadow_paging.c
index d58fed7..198d17e 100644 (file)
@@ -26,6 +26,8 @@
 #include <palacios/vmm_decoder.h>
 #include <palacios/vmm_ctrl_regs.h>
 
+#include <palacios/vmm_hashtable.h>
+
 #ifndef DEBUG_SHADOW_PAGING
 #undef PrintDebug
 #define PrintDebug(fmt, args...)
  ***/
 
 
+struct guest_table {
+  addr_t cr3;
+  struct list_head link;
+};
+
+
+struct backptr {
+  addr_t ptr;
+  struct list_head link;
+};
+
+
+struct shadow_page_data {
+  addr_t ptr;
+  addr_t guest_addr; 
+
+  struct list_head backptrs;
+  struct list_head guest_tables;
+};
+
 
 
 
@@ -601,7 +623,19 @@ static int handle_large_pagefault_32(struct guest_info * info,
 {
   pt_access_status_t shadow_pte_access = v3_can_access_pte32(shadow_pt, fault_addr, error_code);
   pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
-  
+  addr_t guest_fault_pa = BASE_TO_PAGE_ADDR_4MB(large_guest_pde->page_base_addr) + PAGE_OFFSET_4MB(fault_addr);  
+
+  struct v3_shadow_region * shdw_reg = v3_get_shadow_region(info, guest_fault_pa);
+
+  if ((shdw_reg == NULL) || 
+      (shdw_reg->host_type == SHDW_REGION_INVALID)) {
+    // Inject a machine check in the guest
+    PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_fault_pa);
+    v3_raise_exception(info, MC_EXCEPTION);
+    return -1;
+  }
+
   if (shadow_pte_access == PT_ACCESS_OK) {
     // Inconsistent state...
     // Guest Re-Entry will flush tables and everything should now workd
@@ -612,20 +646,11 @@ static int handle_large_pagefault_32(struct guest_info * info,
   
   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);
 
-    if (host_page_type == HOST_REGION_INVALID) {
-      // Inject a machine check in the guest
-      PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_fault_pa);
-      v3_raise_exception(info, MC_EXCEPTION);
-      return 0;
-    }
-
-    if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
+    if ((shdw_reg->host_type == SHDW_REGION_ALLOCATED) || 
+       (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK)) {
       struct shadow_page_state * state = &(info->shdw_pg_state);
-      addr_t shadow_pa = get_shadow_addr(info, guest_fault_pa);
+      addr_t shadow_pa = v3_get_shadow_addr(shdw_reg, guest_fault_pa);
 
       shadow_pte->page_base_addr = PAGE_BASE_ADDR(shadow_pa);
 
@@ -643,11 +668,12 @@ static int handle_large_pagefault_32(struct guest_info * info,
        PrintDebug("Marking page as Guest Page Table (large page)\n");
        shadow_pte->vmm_info = PT32_GUEST_PT;
        shadow_pte->writable = 0;
+      } else if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
+       shadow_pte->writable = 0;
       } else {
        shadow_pte->writable = 1;
       }
 
-
       //set according to VMM policy
       shadow_pte->write_through = 0;
       shadow_pte->cache_disable = 0;
@@ -656,18 +682,27 @@ static int handle_large_pagefault_32(struct guest_info * info,
       
     } else {
       // Handle hooked pages as well as other special pages
-      if (handle_special_page_fault(info, fault_addr, guest_fault_pa, error_code) == -1) {
+      //      if (handle_special_page_fault(info, fault_addr, guest_fault_pa, error_code) == -1) {
+
+      if (v3_handle_mem_full_hook(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
        PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
        return -1;
       }
     }
-  } else if ((shadow_pte_access == PT_ACCESS_WRITE_ERROR) && 
-            (shadow_pte->vmm_info == PT32_GUEST_PT)) {
+  } else if (shadow_pte_access == PT_ACCESS_WRITE_ERROR) {
+
+    if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
 
-    struct shadow_page_state * state = &(info->shdw_pg_state);
-    PrintDebug("Write operation on Guest PAge Table Page (large page)\n");
-    state->cached_cr3 = 0;
-    shadow_pte->writable = 1;
+      if (v3_handle_mem_wr_hook(info, fault_addr, guest_fault_pa, shdw_reg, error_code) == -1) {
+       PrintError("Special Page Fault handler returned error for address: %p\n", (void *)fault_addr);
+       return -1;
+      }
+    } else if (shadow_pte->vmm_info == PT32_GUEST_PT) {
+      struct shadow_page_state * state = &(info->shdw_pg_state);
+      PrintDebug("Write operation on Guest PAge Table Page (large page)\n");
+      state->cached_cr3 = 0;
+      shadow_pte->writable = 1;
+    }
 
   } else {
     PrintError("Error in large page fault handler...\n");
@@ -695,7 +730,17 @@ static int handle_shadow_pte32_fault(struct guest_info * info,
   pt_access_status_t shadow_pte_access;
   pte32_t * guest_pte = (pte32_t *)&(guest_pt[PTE32_INDEX(fault_addr)]);;
   pte32_t * shadow_pte = (pte32_t *)&(shadow_pt[PTE32_INDEX(fault_addr)]);
+  addr_t guest_pa = BASE_TO_PAGE_ADDR((addr_t)(guest_pte->page_base_addr)) +  PAGE_OFFSET(fault_addr);
 
+  struct v3_shadow_region * shdw_reg =  v3_get_shadow_region(info, guest_pa);
+
+  if ((shdw_reg == NULL) || 
+      (shdw_reg->host_type == SHDW_REGION_INVALID)) {
+    // Inject a machine check in the guest
+    PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_pa);
+    v3_raise_exception(info, MC_EXCEPTION);
+    return 0;
+  }
 
   // Check the guest page permissions
   guest_pte_access = v3_can_access_pte32(guest_pt, fault_addr, error_code);
@@ -728,26 +773,13 @@ static int handle_shadow_pte32_fault(struct guest_info * info,
 
 
   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);
-
     // Page Table Entry Not Present
     PrintDebug("guest_pa =%p\n", (void *)guest_pa);
 
-    host_region_type_t host_page_type = get_shadow_addr_type(info, guest_pa);
-
-    if (host_page_type == HOST_REGION_INVALID) {
-      // Inject a machine check in the guest
-      PrintDebug("Invalid Guest Address in page table (0x%p)\n", (void *)guest_pa);
-      v3_raise_exception(info, MC_EXCEPTION);
-      return 0;
-    }
-
-    // else...
-
-    if (host_page_type == HOST_REGION_PHYSICAL_MEMORY) {
+    if ((shdw_reg->host_type == SHDW_REGION_ALLOCATED) ||
+       (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK)) {
       struct shadow_page_state * state = &(info->shdw_pg_state);
-      addr_t shadow_pa = get_shadow_addr(info, guest_pa);
+      addr_t shadow_pa = v3_get_shadow_addr(shdw_reg, guest_pa);
       
       shadow_pte->page_base_addr = PAGE_BASE_ADDR(shadow_pa);
       
@@ -768,7 +800,9 @@ static int handle_shadow_pte32_fault(struct guest_info * info,
        shadow_pte->vmm_info = PT32_GUEST_PT;
       }
 
-      if (guest_pte->dirty == 1) {
+      if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
+       shadow_pte->writable = 0;
+      } else if (guest_pte->dirty == 1) {
        shadow_pte->writable = guest_pte->writable;
       } else if ((guest_pte->dirty == 0) && (error_code.write == 1)) {
        shadow_pte->writable = guest_pte->writable;
@@ -785,29 +819,37 @@ static int handle_shadow_pte32_fault(struct guest_info * info,
        shadow_pte->writable = 0;
       }
 
-
-
     } else {
       // Page fault handled by hook functions
-      if (handle_special_page_fault(info, fault_addr, guest_pa, error_code) == -1) {
+
+      if (v3_handle_mem_full_hook(info, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
        PrintError("Special Page fault handler returned error for address: %p\n",  (void *)fault_addr);
        return -1;
       }
     }
-
+    /*
   } else if ((shadow_pte_access == PT_ACCESS_WRITE_ERROR) &&
             (guest_pte->dirty == 0)) {
-
-    PrintDebug("Shadow PTE Write Error\n");
+    */
+  } else if (shadow_pte_access == PT_ACCESS_WRITE_ERROR) {
     guest_pte->dirty = 1;
-    shadow_pte->writable = guest_pte->writable;
+
+    if (shdw_reg->host_type == SHDW_REGION_WRITE_HOOK) {
+      if (v3_handle_mem_wr_hook(info, fault_addr, guest_pa, shdw_reg, error_code) == -1) {
+       PrintError("Special Page fault handler returned error for address: %p\n",  (void *)fault_addr);
+       return -1;
+      }
+    } else {
+      PrintDebug("Shadow PTE Write Error\n");
+      shadow_pte->writable = guest_pte->writable;
+    }
 
     if (shadow_pte->vmm_info == PT32_GUEST_PT) {
       struct shadow_page_state * state = &(info->shdw_pg_state);
       PrintDebug("Write operation on Guest PAge Table Page\n");
       state->cached_cr3 = 0;
     }
-    
+
     return 0;
 
   } else {