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.


Merge branch 'devel' of ssh://palacios@newskysaw.cs.northwestern.edu/home/palacios...
Patrick Bridges [Wed, 11 Aug 2010 15:41:59 +0000 (09:41 -0600)]
palacios/src/palacios/vmm_direct_paging_64.h
palacios/src/palacios/vmm_mem.c

index 80dce91..d45ae17 100644 (file)
 
 // Reference: AMD Software Developer Manual Vol.2 Ch.5 "Page Translation and Protection"
 
-static int get_page_size() {
-
-    // Need to fix this....
-    return PAGE_SIZE_4KB; 
-
-
-#if 0
-   struct v3_mem_region * base_reg = &(info->vm_info->mem_map.base_region);
+static uint32_t get_page_size(struct guest_info * core, addr_t fault_addr) {
+    addr_t pg_start = 0UL, pg_end = 0UL; // 2MiB page containing the faulting address
+    struct v3_mem_region * pg_next_reg = NULL; // next immediate mem reg after page start addr
+    uint32_t page_size = PAGE_SIZE_4KB;
 
    /* If the guest has been configured for 2MiB pages, then we must check for hooked regions of
      * memory which may overlap with the 2MiB page containing the faulting address (due to
@@ -57,51 +53,48 @@ static int get_page_size() {
      * |----|----|----|----|----|page|----|----|----|     2MB pages
      *                           >>>>>>>>>>>>>>>>>>>>     search space
      */
-    addr_t pg_start = 0UL, pg_end = 0UL; // 2MiB page containing the faulting address
-    struct v3_mem_region * pg_next_reg = NULL; // next immediate mem reg after page start addr
-    bool use_large_page = false;
 
-    if (region == NULL) {
-       PrintError("%s: invalid region, addr=%p\n", __FUNCTION__, (void *)fault_addr);
-       return -1;
-    }
 
-    // set use_large_page here
-    if (info->vm_info->paging_size == PAGING_2MB) {
+    // guest page maps to a host page + offset (so when we shift, it aligns with a host page)
+    pg_start = PAGE_ADDR_2MB(fault_addr);
+    pg_end = (pg_start + PAGE_SIZE_2MB);
 
-       // guest page maps to a host page + offset (so when we shift, it aligns with a host page)
-       pg_start = PAGE_ADDR_2MB(fault_addr);
-       pg_end = (pg_start + PAGE_SIZE_2MB);
+    PrintDebug("%s: page   [%p,%p) contains address\n", __FUNCTION__, (void *)pg_start, (void *)pg_end);
 
-       PrintDebug("%s: page   [%p,%p) contains address\n", __FUNCTION__, (void *)pg_start, (void *)pg_end);
+    pg_next_reg = v3_get_next_mem_region(core->vm_info, core->cpu_id, pg_start);
 
-       pg_next_reg = v3_get_next_mem_region(info->vm_info, info->cpu_id, pg_start);
+    if (pg_next_reg == NULL) {
+       PrintError("%s: Error: address not in base region, %p\n", __FUNCTION__, (void *)fault_addr);
+       return PAGE_SIZE_4KB;
+    }
 
-       if (pg_next_reg == NULL) {
-           PrintError("%s: Error: address not in base region, %p\n", __FUNCTION__, (void *)fault_addr);
-           return -1;
+    if (pg_next_reg->flags.base == 1) {
+       page_size = PAGE_SIZE_2MB; // State A
+    } else {
+#if 0       // State B/C and D optimization
+       if ((pg_next_reg->guest_end >= pg_end) &&
+           ((pg_next_reg->guest_start >= pg_end) || (pg_next_reg->guest_start <= pg_start))) {     
+           page_size = PAGE_SIZE_2MB;
        }
 
-       if (pg_next_reg->base == 1) { // next region == base region
-           use_large_page = 1; // State A
-       } else {
-#if 0       // State B/C and D optimization
-           use_large_page = (pg_next_reg->guest_end >= pg_end) &&
-               ((pg_next_reg->guest_start >= pg_end) || (pg_next_reg->guest_start <= pg_start));
-           PrintDebug("%s: region [%p,%p) %s partial overlap with page\n", __FUNCTION__,
-                   (void *)pg_next_reg->guest_start, (void *)pg_next_reg->guest_end,
-                   (use_large_page ? "does not have" : "has"));
+       PrintDebug("%s: region [%p,%p) %s partially overlap with page\n", __FUNCTION__,
+                  (void *)pg_next_reg->guest_start, (void *)pg_next_reg->guest_end, 
+                  (page_size == PAGE_SIZE_2MB) ? "does not" : "does");
+
 #else       // State B/C
-           use_large_page = (pg_next_reg->guest_start >= pg_end);
-           PrintDebug("%s: region [%p,%p) %s overlap with page\n", __FUNCTION__,
-                   (void *)pg_next_reg->guest_start, (void *)pg_next_reg->guest_end,
-                   (use_large_page ? "does not have" : "has"));
-#endif
+       if (pg_next_reg->guest_start >= pg_end) {
+           
+           page_size = PAGE_SIZE_2MB;
        }
-    }
 
-    PrintDebug("%s: Address gets a 2MiB page? %s\n", __FUNCTION__, (use_large_page ? "yes" : "no"));
+       PrintDebug("%s: region [%p,%p) %s overlap with page\n", __FUNCTION__,
+                  (void *)pg_next_reg->guest_start, (void *)pg_next_reg->guest_end,
+                  (page_size == PAGE_SIZE_2MB) ? "does not" : "does");
+
 #endif
+    }
+
+    return page_size;
 }
 
 
@@ -121,15 +114,21 @@ static inline int handle_passthrough_pagefault_64(struct guest_info * core, addr
     struct v3_mem_region * region =  v3_get_mem_region(core->vm_info, core->cpu_id, fault_addr);
     int page_size = PAGE_SIZE_4KB;
 
+    if (region == NULL) {
+       PrintError("%s: invalid region, addr=%p\n", __FUNCTION__, (void *)fault_addr);
+       return -1;
+    }
 
     /*  Check if:
      *  1. the guest is configured to use large pages and 
      *         2. the memory regions can be referenced by a large page
      */
     if ((core->use_large_pages == 1) ) {
-       page_size = get_page_size();
+       page_size = get_page_size(core, fault_addr);
     }
 
+    PrintDebug("Using page size of %dKB\n", page_size / 1024);
+
  
     // Lookup the correct PML address based on the PAGING MODE
     if (core->shdw_pg_mode == SHADOW_PAGING) {
index 9510810..c45f18d 100644 (file)
@@ -290,30 +290,38 @@ struct v3_mem_region * v3_get_mem_region(struct v3_vm_info * vm, uint16_t core_i
 
 
 
-/* Search the "hooked" memory regions for a region that ends after the given address.  If the
- * address is invalid, return NULL. Else, return the first region found or the base region if no
- * region ends after the given address.
+/* Given an address, find the successor region. If the address is within a region, return that
+ * region. Input is an address, because the address may not have a region associated with it.
+ *
+ * Returns a region following or touching the given address. If address is invalid, NULL is
+ * returned, else the base region is returned if no region exists at or after the given address.
  */
 struct v3_mem_region * v3_get_next_mem_region( struct v3_vm_info * vm, uint16_t core_id, addr_t guest_addr) {
-    struct rb_node * n = vm->mem_map.mem_regions.rb_node;
-    struct v3_mem_region * reg = NULL;
-
-    // Keep going to the right in the tree while the address is greater than the current region's
-    // end address.
-    while (n) {
-        reg = rb_entry(n, struct v3_mem_region, tree_node);
-        if (guest_addr >= reg->guest_end) { // reg is [start,end)
-            n = n->rb_right;
-        } else {
-           if ((core_id == reg->core_id) || (reg->core_id == V3_MEM_CORE_ANY)) {
-               return reg;
-           } else {
-               n = n->rb_right;
+    struct rb_node * current_n         = vm->mem_map.mem_regions.rb_node;
+    struct rb_node * successor_n       = NULL; /* left-most node greater than guest_addr */
+    struct v3_mem_region * current_r   = NULL;
+
+    /* current_n tries to find the region containing guest_addr, going right when smaller and left when
+     * greater. Each time current_n becomes greater than guest_addr, update successor <- current_n.
+     * current_n becomes successively closer to guest_addr than the previous time it was greater
+     * than guest_addr.
+     */
+
+    /* | is address, ---- is region, + is intersection */
+    while (current_n) {
+        current_r = rb_entry(current_n, struct v3_mem_region, tree_node);
+       if (current_r->guest_start > guest_addr) { /* | ---- */
+           successor_n = current_n;
+           current_n = current_n->rb_left;
+       } else {
+           if (current_r->guest_end > guest_addr) {
+               return current_r; /* +--- or --+- */
            }
-        }
+           current_n = current_n->rb_right; /* ---- | */
+       }
     }
 
-    // There is no registered region, so we check if it's a valid address in the base region
+    /* Address does not have its own region. Check if it's a valid address in the base region */
 
     if (guest_addr >= vm->mem_map.base_region.guest_end) {
        PrintError("%s: Guest Address Exceeds Base Memory Size (ga=%p), (limit=%p)\n",