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.


integrated new configuration system
[palacios.git] / palacios / src / palacios / vmm_mem.c
index 7480da7..4248845 100644 (file)
+/* 
+ * This file is part of the Palacios Virtual Machine Monitor developed
+ * by the V3VEE Project with funding from the United States National 
+ * Science Foundation and the Department of Energy.  
+ *
+ * The V3VEE Project is a joint project between Northwestern University
+ * and the University of New Mexico.  You can find out more at 
+ * http://www.v3vee.org
+ *
+ * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
+ * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
+ * All rights reserved.
+ *
+ * Author: Jack Lange <jarusl@cs.northwestern.edu>
+ *
+ * This is free software.  You are permitted to use,
+ * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
+ */
+
 #include <palacios/vmm_mem.h>
 #include <palacios/vmm.h>
 #include <palacios/vmm_util.h>
-#include <palacios/vmm_decoder.h>
-
-
-
-void init_shadow_region(struct shadow_region * entry,
-                       addr_t               guest_addr_start,
-                       addr_t               guest_addr_end,
-                       guest_region_type_t  guest_region_type,
-                       host_region_type_t   host_region_type)
-{
-  entry->guest_type = guest_region_type;
-  entry->guest_start = guest_addr_start;
-  entry->guest_end = guest_addr_end;
-  entry->host_type = host_region_type;
-  entry->host_addr = 0;
-  entry->next=entry->prev = NULL;
-}
-
-int add_shadow_region_passthrough( struct guest_info *  guest_info,
-                                  addr_t               guest_addr_start,
-                                  addr_t               guest_addr_end,
-                                  addr_t               host_addr)
-{
-  struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
-
-  init_shadow_region(entry, guest_addr_start, guest_addr_end, 
-                    GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_PHYSICAL_MEMORY);
-  entry->host_addr = host_addr;
-
-  return add_shadow_region(&(guest_info->mem_map), entry);
-}
-
-int hook_guest_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
-                  int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data),
-                  int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
-                  void * priv_data) {
-  
-  struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
-  struct vmm_mem_hook * hook = (struct vmm_mem_hook *)V3_Malloc(sizeof(struct vmm_mem_hook));
+#include <palacios/vmm_emulator.h>
+#include <palacios/vm_guest.h>
 
-  memset(hook, 0, sizeof(struct vmm_mem_hook));
+#include <palacios/vmm_shadow_paging.h>
+#include <palacios/vmm_direct_paging.h>
 
-  hook->read = read;
-  hook->write = write;
-  hook->region = entry;
-  hook->priv_data = priv_data;
+#define MEM_OFFSET_HCALL 0x1000
 
 
-  init_shadow_region(entry, guest_addr_start, guest_addr_end, 
-                    GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_HOOK);
+static inline
+struct v3_shadow_region * insert_shadow_region(struct guest_info * info, 
+                                              struct v3_shadow_region * region);
 
-  entry->host_addr = (addr_t)hook;
-
-  return add_shadow_region(&(info->mem_map), entry);
-}
 
+static int mem_offset_hypercall(struct guest_info * info, uint_t hcall_id, void * private_data) {
+    info->vm_regs.rbx = info->mem_map.base_region.host_addr;
 
-struct vmm_mem_hook * get_mem_hook(struct guest_info * info, addr_t guest_addr) {
-  struct shadow_region * region = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
-
-  if (region == NULL) {
-    PrintDebug("Could not find shadow region for addr: %x\n", guest_addr);
-    return NULL;
-  }
-
-  return (struct vmm_mem_hook *)(region->host_addr);
+    return 0;
 }
 
 
-/* mem_addr is the guest physical memory address */
-static int mem_hook_dispatch(struct guest_info * info, addr_t fault_addr, addr_t guest_phys_page,  pf_error_t access_info, struct vmm_mem_hook * hook) {
+int v3_init_shadow_map(struct guest_info * info) {
+    v3_shdw_map_t * map = &(info->mem_map);
+    addr_t mem_pages = info->mem_size >> 12;
 
-  // emulate and then dispatch 
-  // or dispatch and emulate
+    map->shdw_regions.rb_node = NULL;
+    map->hook_hva = (addr_t)V3_VAddr(V3_AllocPages(1));
 
+    // There is an underlying region that contains all of the guest memory
+    // PrintDebug("Mapping %d pages of memory (%u bytes)\n", (int)mem_pages, (uint_t)info->mem_size);
 
-  if (access_info.write == 1) {
-    void * src = NULL;
-    uint_t length = 0;
+    map->base_region.guest_start = 0;
+    map->base_region.guest_end = mem_pages * PAGE_SIZE_4KB;
+    map->base_region.host_type = SHDW_REGION_ALLOCATED;
+    map->base_region.host_addr = (addr_t)V3_AllocPages(mem_pages);
 
-    PrintDebug("Memory hook write\n");
-    return -1;
 
-    if (hook->write(fault_addr, src, length, hook->priv_data) != length) {
-      return -1;
+    if ((void *)map->base_region.host_addr == NULL) {
+       PrintError("Could not allocate Guest memory\n");
+       return -1;
     }
-  } else {
-    PrintDebug("Memory hook read\n");
-    return -1;
-  }    
+       
+    //memset(V3_VAddr((void *)map->base_region.host_addr), 0xffffffff, map->base_region.guest_end);
 
-  return -1;
-}
-
-
-int handle_special_page_fault(struct guest_info * info, addr_t fault_addr, addr_t guest_phys_page, pf_error_t access_info) {
-  struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_phys_page);
-
-  switch (reg->host_type) {
-  case HOST_REGION_HOOK:
-    return mem_hook_dispatch(info, fault_addr, guest_phys_page, access_info, (struct vmm_mem_hook *)(reg->host_addr));
-  default:
-    return -1;
-  }
-
-  return 0;
-
-}
+    v3_register_hypercall(info, MEM_OFFSET_HCALL, mem_offset_hypercall, NULL);
 
-
-
-void init_shadow_map(struct guest_info * info) {
-  struct shadow_map * map = &(info->mem_map);
-
-  map->num_regions = 0;
-
-  map->head = NULL;
+    return 0;
 }
 
+void v3_delete_shadow_map(struct guest_info * info) {
+    struct rb_node * node = v3_rb_first(&(info->mem_map.shdw_regions));
+    struct v3_shadow_region * reg;
+    struct rb_node * tmp_node = NULL;
+  
+    while (node) {
+       reg = rb_entry(node, struct v3_shadow_region, tree_node);
+       tmp_node = node;
+       node = v3_rb_next(node);
 
-void free_shadow_map(struct shadow_map * map) {
-  struct shadow_region * cursor = map->head;
-  struct shadow_region * tmp = NULL;
-
-  while(cursor) {
-    tmp = cursor;
-    cursor = cursor->next;
-    V3_Free(tmp);
-  }
+       v3_delete_shadow_region(info, reg);
+    }
 
-  V3_Free(map);
+    V3_FreePage((void *)(info->mem_map.base_region.host_addr));
+    V3_FreePage(V3_PAddr((void *)(info->mem_map.hook_hva)));
 }
 
 
 
 
-int add_shadow_region(struct shadow_map * map,
-                     struct shadow_region * region) 
+int v3_add_shadow_mem( struct guest_info *  info,
+                      addr_t               guest_addr_start,
+                      addr_t               guest_addr_end,
+                      addr_t               host_addr)
 {
-  struct shadow_region * cursor = map->head;
-
-  PrintDebug("Adding Shadow Region: (0x%x-0x%x)\n", region->guest_start, region->guest_end);
-
-  if ((!cursor) || (cursor->guest_start >= region->guest_end)) {
-    region->prev = NULL;
-    region->next = cursor;
-    map->num_regions++;
-    map->head = region;
-    return 0;
-  }
-
-  while (cursor) {
-    // Check if it overlaps with the current cursor
-    if ((cursor->guest_end > region->guest_start) && (cursor->guest_start < region->guest_start)) {
-      // overlaps not allowed
-      return -1;
+    struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
+
+    entry->guest_start = guest_addr_start;
+    entry->guest_end = guest_addr_end;
+    entry->host_type = SHDW_REGION_ALLOCATED;
+    entry->host_addr = host_addr;
+    entry->write_hook = NULL;
+    entry->read_hook = NULL;
+    entry->priv_data = NULL;
+
+    if (insert_shadow_region(info, entry)) {
+       V3_Free(entry);
+       return -1;
     }
-    
-    if (!(cursor->next)) {
-      // add to the end of the list
-      cursor->next = region;
-      region->prev = cursor;
-      region->next = NULL;
-      map->num_regions++;
-      return 0;
-    } else if (cursor->next->guest_start >= region->guest_end) {
-      // add here
-      region->next = cursor->next;
-      region->prev = cursor;
-      
-      cursor->next->prev = region;
-      cursor->next = region;
-
-      map->num_regions++;
-      
-      return 0;
-    } else if (cursor->next->guest_end <= region->guest_start) {
-      cursor = cursor->next;
-    } else {
-      // This cannot happen!
-      // we should panic here
-      return -1;
-    }
-  }
-  
-  // This cannot happen
-  // We should panic here
-  return -1;
-}
-
 
-int delete_shadow_region(struct shadow_map * map,
-                        addr_t guest_start,
-                        addr_t guest_end) {
-  return -1;
+    return 0;
 }
 
 
 
-struct shadow_region *get_shadow_region_by_index(struct shadow_map *  map,
-                                                uint_t index) {
-  struct shadow_region * reg = map->head;
-  uint_t i = 0;
+int v3_hook_write_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end, 
+                     addr_t host_addr,
+                     int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
+                     void * priv_data) {
 
-  while (reg) { 
-    if (i == index) { 
-      return reg;
-    }
-    reg = reg->next;
-    i++;
-  }
-  return NULL;
-}
+    struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
 
 
-struct shadow_region * get_shadow_region_by_addr(struct shadow_map * map,
-                                                addr_t addr) {
-  struct shadow_region * reg = map->head;
+    entry->guest_start = guest_addr_start;
+    entry->guest_end = guest_addr_end;
+    entry->host_type = SHDW_REGION_WRITE_HOOK;
+    entry->host_addr = host_addr;
+    entry->write_hook = write;
+    entry->read_hook = NULL;
+    entry->priv_data = priv_data;
 
-  while (reg) {
-    if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
-      return reg;
-    } else if (reg->guest_start > addr) {
-      return NULL;
-    } else {
-      reg = reg->next;
+    if (insert_shadow_region(info, entry)) {
+       V3_Free(entry);
+       return -1;
     }
-  }
-  return NULL;
-}
-
-
-host_region_type_t get_shadow_addr_type(struct guest_info * info, addr_t guest_addr) {
-  struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
 
-  if (!reg) {
-    return HOST_REGION_INVALID;
-  } else {
-    return reg->host_type;
-  }
+    return 0;  
 }
 
-addr_t get_shadow_addr(struct guest_info * info, addr_t guest_addr) {
-  struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
+int v3_hook_full_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
+                    int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data),
+                    int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
+                    void * priv_data) {
+  
+    struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
+
+    entry->guest_start = guest_addr_start;
+    entry->guest_end = guest_addr_end;
+    entry->host_type = SHDW_REGION_FULL_HOOK;
+    entry->host_addr = (addr_t)NULL;
+    entry->write_hook = write;
+    entry->read_hook = read;
+    entry->priv_data = priv_data;
+  
+    if (insert_shadow_region(info, entry)) {
+       V3_Free(entry);
+       return -1;
+    }
 
-  if (!reg) {
     return 0;
-  } else {
-    return (guest_addr - reg->guest_start) + reg->host_addr;
-  }
 }
 
 
-host_region_type_t lookup_shadow_map_addr(struct shadow_map * map, addr_t guest_addr, addr_t * host_addr) {
-  struct shadow_region * reg = get_shadow_region_by_addr(map, guest_addr);
+// This will unhook the memory hook registered at start address
+// We do not support unhooking subregions
+int v3_unhook_mem(struct guest_info * info, addr_t guest_addr_start) {
+    struct v3_shadow_region * reg = v3_get_shadow_region(info, guest_addr_start);
 
-  if (!reg) {
-    // No mapping exists
-    return HOST_REGION_INVALID;
-  } else {
-    switch (reg->host_type) {
-    case HOST_REGION_PHYSICAL_MEMORY:
-     *host_addr = (guest_addr - reg->guest_start) + reg->host_addr;
-     return reg->host_type;
-    case HOST_REGION_MEMORY_MAPPED_DEVICE:
-    case HOST_REGION_UNALLOCATED:
-      // ... 
-    default:
-      *host_addr = 0;
-      return reg->host_type;
+    if ((reg->host_type != SHDW_REGION_FULL_HOOK) || 
+       (reg->host_type != SHDW_REGION_WRITE_HOOK)) {
+       PrintError("Trying to unhook a non hooked memory region (addr=%p)\n", (void *)guest_addr_start);
+       return -1;
     }
-  }
-}
-
-
-void print_shadow_map(struct shadow_map * map) {
-  struct shadow_region * cur = map->head;
-  int i = 0;
 
-  PrintDebug("Memory Layout (regions: %d) \n", map->num_regions);
+    v3_delete_shadow_region(info, reg);
 
-  while (cur) {
-    PrintDebug("%d:  0x%x - 0x%x (%s) -> ", i, cur->guest_start, cur->guest_end - 1,
-              cur->guest_type == GUEST_REGION_PHYSICAL_MEMORY ? "GUEST_REGION_PHYSICAL_MEMORY" :
-              cur->guest_type == GUEST_REGION_NOTHING ? "GUEST_REGION_NOTHING" :
-              cur->guest_type == GUEST_REGION_MEMORY_MAPPED_DEVICE ? "GUEST_REGION_MEMORY_MAPPED_DEVICE" :
-              "UNKNOWN");
-    if (cur->host_type == HOST_REGION_PHYSICAL_MEMORY || 
-       cur->host_type == HOST_REGION_UNALLOCATED ||
-       cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) { 
-      PrintDebug("0x%x", cur->host_addr);
-    }
-    PrintDebug("(%s)\n",
-              cur->host_type == HOST_REGION_PHYSICAL_MEMORY ? "HOST_REGION_PHYSICAL_MEMORY" :
-              cur->host_type == HOST_REGION_UNALLOCATED ? "HOST_REGION_UNALLOACTED" :
-              cur->host_type == HOST_REGION_HOOK ? "HOST_REGION_HOOK" :
-              cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE ? "HOST_REGION_MEMORY_MAPPED_DEVICE" :
-              cur->host_type == HOST_REGION_REMOTE ? "HOST_REGION_REMOTE" : 
-              cur->host_type == HOST_REGION_SWAPPED ? "HOST_REGION_SWAPPED" :
-              "UNKNOWN");
-    cur = cur->next;
-    i++;
-  }
+    return 0;
 }
 
 
 
+static inline 
+struct v3_shadow_region * __insert_shadow_region(struct guest_info * info, 
+                                                struct v3_shadow_region * region) {
+    struct rb_node ** p = &(info->mem_map.shdw_regions.rb_node);
+    struct rb_node * parent = NULL;
+    struct v3_shadow_region * tmp_region;
 
+    while (*p) {
+       parent = *p;
+       tmp_region = rb_entry(parent, struct v3_shadow_region, tree_node);
 
+       if (region->guest_end <= tmp_region->guest_start) {
+           p = &(*p)->rb_left;
+       } else if (region->guest_start >= tmp_region->guest_end) {
+           p = &(*p)->rb_right;
+       } else {
+           return tmp_region;
+       }
+    }
 
+    rb_link_node(&(region->tree_node), parent, p);
+  
+    return NULL;
+}
 
 
+static inline
+struct v3_shadow_region * insert_shadow_region(struct guest_info * info, 
+                                              struct v3_shadow_region * region) {
+    struct v3_shadow_region * ret;
 
+    if ((ret = __insert_shadow_region(info, region))) {
+       return ret;
+    }
 
-#ifdef VMM_MEM_TEST
+    v3_rb_insert_color(&(region->tree_node), &(info->mem_map.shdw_regions));
 
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdarg.h>
 
+    // flush virtual page tables 
+    // 3 cases shadow, shadow passthrough, and nested
+    if (info->shdw_pg_mode == SHADOW_PAGING) {
+       v3_mem_mode_t mem_mode = v3_get_vm_mem_mode(info);
 
+       if (mem_mode == PHYSICAL_MEM) {
+           addr_t cur_addr;
 
+           for (cur_addr = region->guest_start;
+                cur_addr < region->guest_end;
+                cur_addr += PAGE_SIZE_4KB) {
+               v3_invalidate_passthrough_addr(info, cur_addr);
+           }
+       } else {
+           v3_invalidate_shadow_pts(info);
+       }
 
+    } else if (info->shdw_pg_mode == NESTED_PAGING) {
+       addr_t cur_addr;
 
-struct vmm_os_hooks * os_hooks;
+       for (cur_addr = region->guest_start;
+            cur_addr < region->guest_end;
+            cur_addr += PAGE_SIZE_4KB) {
+           
+           v3_invalidate_nested_addr(info, cur_addr);
+       }
+    }
 
-void * TestMalloc(uint_t size) {
-  return malloc(size);
+    return NULL;
 }
+                                                
 
-void * TestAllocatePages(int size) {
-  return malloc(4096 * size);
-}
 
 
-void TestPrint(const char * fmt, ...) {
-  va_list args;
+int handle_special_page_fault(struct guest_info * info, 
+                             addr_t fault_gva, addr_t fault_gpa, 
+                             pf_error_t access_info) 
+{
+    struct v3_shadow_region * reg = v3_get_shadow_region(info, fault_gpa);
 
-  va_start(args, fmt);
-  vprintf(fmt, args);
-  va_end(args);
-}
+    PrintDebug("Handling Special Page Fault\n");
 
-int mem_list_add_test_1(  vmm_mem_list_t * list) {
+    switch (reg->host_type) {
+       case SHDW_REGION_WRITE_HOOK:
+           return v3_handle_mem_wr_hook(info, fault_gva, fault_gpa, reg, access_info);
+       case SHDW_REGION_FULL_HOOK:
+           return v3_handle_mem_full_hook(info, fault_gva, fault_gpa, reg, access_info);
+       default:
+           return -1;
+    }
 
-  uint_t offset = 0;
+    return 0;
 
-  PrintDebug("\n\nTesting Memory List\n");
+}
 
-  init_mem_list(list);
+int v3_handle_mem_wr_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa, 
+                         struct v3_shadow_region * reg, pf_error_t access_info) {
 
-  offset = PAGE_SIZE * 6;
-  PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 10));
-  add_mem_list_pages(list, offset, 10);
-  print_mem_list(list);
+    addr_t dst_addr = (addr_t)V3_VAddr((void *)v3_get_shadow_addr(reg, guest_pa));
 
+    if (v3_emulate_write_op(info, guest_va, guest_pa, dst_addr, 
+                           reg->write_hook, reg->priv_data) == -1) {
+       PrintError("Write hook emulation failed\n");
+       return -1;
+    }
 
-  offset = 0;
-  PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + PAGE_SIZE * 4);
-  add_mem_list_pages(list, offset, 4);
-  print_mem_list(list);
+    return 0;
+}
 
-  offset = PAGE_SIZE * 20;
-  PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
-  add_mem_list_pages(list, offset, 1);
-  print_mem_list(list);
+int v3_handle_mem_full_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa, 
+                           struct v3_shadow_region * reg, pf_error_t access_info) {
+  
+    addr_t op_addr = info->mem_map.hook_hva;
+
+    if (access_info.write == 1) {
+       if (v3_emulate_write_op(info, guest_va, guest_pa, op_addr, 
+                               reg->write_hook, reg->priv_data) == -1) {
+           PrintError("Write Full Hook emulation failed\n");
+           return -1;
+       }
+    } else {
+       if (v3_emulate_read_op(info, guest_va, guest_pa, op_addr, 
+                              reg->read_hook, reg->write_hook, 
+                              reg->priv_data) == -1) {
+           PrintError("Read Full Hook emulation failed\n");
+           return -1;
+       }
+    }
 
-  offset = PAGE_SIZE * 21;
-  PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 3));
-  add_mem_list_pages(list, offset, 3);
-  print_mem_list(list);
+    return 0;
+}
 
 
-  offset = PAGE_SIZE * 10;
-  PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 30));
-  add_mem_list_pages(list, offset, 30);
-  print_mem_list(list);
 
+struct v3_shadow_region * v3_get_shadow_region(struct guest_info * info, addr_t guest_addr) {
+    struct rb_node * n = info->mem_map.shdw_regions.rb_node;
+    struct v3_shadow_region * reg = NULL;
 
-  offset = PAGE_SIZE * 5;
-  PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
-  add_mem_list_pages(list, offset, 1);
-  print_mem_list(list);
+    while (n) {
+       reg = rb_entry(n, struct v3_shadow_region, tree_node);
 
+       if (guest_addr < reg->guest_start) {
+           n = n->rb_left;
+       } else if (guest_addr >= reg->guest_end) {
+           n = n->rb_right;
+       } else {
+           return reg;
+       }
+    }
 
-  return 0;
-}
 
+    // There is not registered region, so we check if its a valid address in the base region
 
-int mem_layout_add_test_1(vmm_mem_layout_t * layout) {
+    if (guest_addr > info->mem_map.base_region.guest_end) {
+       PrintError("Guest Address Exceeds Base Memory Size (ga=%p), (limit=%p)\n", 
+                  (void *)guest_addr, (void *)info->mem_map.base_region.guest_end);
+       v3_print_mem_map(info);
 
-  
-  uint_t start = 0;
-  uint_t end = 0;
+       return NULL;
+    }
+    
+    return &(info->mem_map.base_region);
+}
 
-  PrintDebug("\n\nTesting Memory Layout\n");
 
-  init_mem_layout(layout);
+void v3_delete_shadow_region(struct guest_info * info, struct v3_shadow_region * reg) {
+    if (reg == NULL) {
+       return;
+    }
 
-  start = 0x6000;
-  end = 0x10000;;
-  PrintDebug("Adding 0x%x - 0x%x\n", start, end);
-  add_guest_mem_range(layout, start, end);
-  print_mem_layout(layout);
+    // flush virtual page tables 
+    // 3 cases shadow, shadow passthrough, and nested
+    if (info->shdw_pg_mode == SHADOW_PAGING) {
+       v3_mem_mode_t mem_mode = v3_get_vm_mem_mode(info);
+           
+       if (mem_mode == PHYSICAL_MEM) {
+           addr_t cur_addr;
+               
+           for (cur_addr = reg->guest_start;
+                cur_addr < reg->guest_end;
+                cur_addr += PAGE_SIZE_4KB) {
+               v3_invalidate_passthrough_addr(info, cur_addr);
+           }
+       } else {
+           v3_invalidate_shadow_pts(info);
+       }
+
+    } else if (info->shdw_pg_mode == NESTED_PAGING) {
+       addr_t cur_addr;
+
+       for (cur_addr = reg->guest_start;
+            cur_addr < reg->guest_end;
+            cur_addr += PAGE_SIZE_4KB) {
+           
+           v3_invalidate_nested_addr(info, cur_addr);
+       }
+    }
 
 
-  start = 0x1000;
-  end = 0x3000;
-  PrintDebug("Adding 0x%x - 0x%x\n", start, end);
-  add_guest_mem_range(layout, start, end);
-  print_mem_layout(layout);
+    v3_rb_erase(&(reg->tree_node), &(info->mem_map.shdw_regions));
 
-  start = 0x2000;
-  end = 0x6000;
-  PrintDebug("Adding 0x%x - 0x%x\n", start, end);
-  add_guest_mem_range(layout, start, end);
-  print_mem_layout(layout);
+    V3_Free(reg);
 
-  start = 0x4000;
-  end = 0x5000;
-  PrintDebug("Adding 0x%x - 0x%x\n", start, end);
-  add_guest_mem_range(layout, start, end);
-  print_mem_layout(layout);
+    // flush virtual page tables 
+    // 3 cases shadow, shadow passthrough, and nested
 
+}
 
-  start = 0x5000;
-  end = 0x7000;
-  PrintDebug("Adding 0x%x - 0x%x\n", start, end);
-  add_guest_mem_range(layout, start, end);
-  print_mem_layout(layout);
 
 
 
+addr_t v3_get_shadow_addr(struct v3_shadow_region * reg, addr_t guest_addr) {
+    if ( (reg) && 
+         (reg->host_type != SHDW_REGION_FULL_HOOK)) {
+        return (guest_addr - reg->guest_start) + reg->host_addr;
+    } else {
+       //  PrintError("MEM Region Invalid\n");
+        return 0;
+    }
 
-  return 0;
 }
 
 
 
-int main(int argc, char ** argv) {
-  struct vmm_os_hooks dummy_hooks;
-  os_hooks = &dummy_hooks;
+void v3_print_mem_map(struct guest_info * info) {
+    struct rb_node * node = v3_rb_first(&(info->mem_map.shdw_regions));
+    struct v3_shadow_region * reg = &(info->mem_map.base_region);
+    int i = 0;
 
-  vmm_mem_layout_t layout;
-  vmm_mem_list_t list;
+    V3_Print("Memory Layout:\n");
+    
 
-  os_hooks->malloc = &TestMalloc;
-  os_hooks->free = &free;
-  os_hooks->print_debug = &TestPrint;
-  os_hooks->allocate_pages = &TestAllocatePages;
+    V3_Print("Base Region:  0x%p - 0x%p -> 0x%p\n", 
+              (void *)(reg->guest_start), 
+              (void *)(reg->guest_end - 1), 
+              (void *)(reg->host_addr));
+    
 
+    // If the memory map is empty, don't print it
+    if (node == NULL) {
+       return;
+    }
 
+    do {
+       reg = rb_entry(node, struct v3_shadow_region, tree_node);
 
-  printf("mem_list_add_test_1: %d\n", mem_list_add_test_1(&list));
-  printf("layout_add_test_1: %d\n", mem_layout_add_test_1(&layout));
+       V3_Print("%d:  0x%p - 0x%p -> 0x%p\n", i, 
+                  (void *)(reg->guest_start), 
+                  (void *)(reg->guest_end - 1), 
+                  (void *)(reg->host_addr));
 
-  return 0;
+       V3_Print("\t(%s) (WriteHook = 0x%p) (ReadHook = 0x%p)\n", 
+                  v3_shdw_region_type_to_str(reg->host_type),
+                  (void *)(reg->write_hook), 
+                  (void *)(reg->read_hook));
+    
+       i++;
+    } while ((node = v3_rb_next(node)));
 }
-#endif
-
-
-
 
 
+static const uchar_t  SHDW_REGION_WRITE_HOOK_STR[] = "SHDW_REGION_WRITE_HOOK";
+static const uchar_t  SHDW_REGION_FULL_HOOK_STR[] = "SHDW_REGION_FULL_HOOK";
+static const uchar_t  SHDW_REGION_ALLOCATED_STR[] = "SHDW_REGION_ALLOCATED";
+
+const uchar_t * v3_shdw_region_type_to_str(v3_shdw_region_type_t type) {
+    switch (type) {
+       case SHDW_REGION_WRITE_HOOK:
+           return SHDW_REGION_WRITE_HOOK_STR;
+       case SHDW_REGION_FULL_HOOK:
+           return SHDW_REGION_FULL_HOOK_STR;
+       case SHDW_REGION_ALLOCATED:
+           return SHDW_REGION_ALLOCATED_STR;
+       default:
+           return (uchar_t *)"SHDW_REGION_INVALID";
+    }
+}