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.


Context-based output infrastructure (V3_Print, etc) and modifications to use it
[palacios.git] / palacios / src / palacios / vmm_mem.c
index c43dcae..0f208b4 100644 (file)
@@ -22,6 +22,7 @@
 #include <palacios/vmm_util.h>
 #include <palacios/vmm_emulator.h>
 #include <palacios/vm_guest.h>
+#include <palacios/vmm_debug.h>
 
 #include <palacios/vmm_shadow_paging.h>
 #include <palacios/vmm_direct_paging.h>
@@ -30,7 +31,7 @@
 
 
 static int mem_offset_hypercall(struct guest_info * info, uint_t hcall_id, void * private_data) {
-    PrintDebug("V3Vee: Memory offset hypercall (offset=%p)\n", 
+    PrintDebug(info->vm_info, info,"V3Vee: Memory offset hypercall (offset=%p)\n", 
               (void *)(info->vm_info->mem_map.base_region.host_addr));
 
     info->vm_regs.rbx = info->vm_info->mem_map.base_region.host_addr;
@@ -41,7 +42,8 @@ static int mem_offset_hypercall(struct guest_info * info, uint_t hcall_id, void
 static int unhandled_err(struct guest_info * core, addr_t guest_va, addr_t guest_pa, 
                         struct v3_mem_region * reg, pf_error_t access_info) {
 
-    PrintError("Unhandled memory access error\n");
+    PrintError(core->vm_info, core, "Unhandled memory access error (gpa=%p, gva=%p, error_code=%d)\n",
+              (void *)guest_pa, (void *)guest_va, *(uint32_t *)&access_info);
 
     v3_print_mem_map(core->vm_info);
 
@@ -59,18 +61,27 @@ int v3_init_mem_map(struct v3_vm_info * vm) {
     map->mem_regions.rb_node = NULL;
 
     // 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);
+    // PrintDebug(info->vm_info, info, "Mapping %d pages of memory (%u bytes)\n", (int)mem_pages, (uint_t)info->mem_size);
 
     // 2MB page alignment needed for 2MB hardware nested paging
     map->base_region.guest_start = 0;
     map->base_region.guest_end = mem_pages * PAGE_SIZE_4KB;
 
-#ifdef CONFIG_ALIGNED_PG_ALLOC
+#ifdef V3_CONFIG_ALIGNED_PG_ALLOC
     map->base_region.host_addr = (addr_t)V3_AllocAlignedPages(mem_pages, vm->mem_align);
 #else
     map->base_region.host_addr = (addr_t)V3_AllocPages(mem_pages);
 #endif
 
+    if ((void*)map->base_region.host_addr == NULL) { 
+       PrintError(vm, VCORE_NONE,"Could not allocate guest memory\n");
+       return -1;
+    }
+
+    // Clear the memory...
+    memset(V3_VAddr((void *)map->base_region.host_addr), 0, mem_pages * PAGE_SIZE_4KB);
+
+
     map->base_region.flags.read = 1;
     map->base_region.flags.write = 1;
     map->base_region.flags.exec = 1;
@@ -79,13 +90,6 @@ int v3_init_mem_map(struct v3_vm_info * vm) {
     
     map->base_region.unhandled = unhandled_err;
 
-    if ((void *)map->base_region.host_addr == NULL) {
-       PrintError("Could not allocate Guest memory\n");
-       return -1;
-    }
-       
-    //memset(V3_VAddr((void *)map->base_region.host_addr), 0xffffffff, map->base_region.guest_end);
-
     v3_register_hypercall(vm, MEM_OFFSET_HCALL, mem_offset_hypercall, NULL);
 
     return 0;
@@ -112,8 +116,20 @@ void v3_delete_mem_map(struct v3_vm_info * vm) {
 
 struct v3_mem_region * v3_create_mem_region(struct v3_vm_info * vm, uint16_t core_id, 
                                               addr_t guest_addr_start, addr_t guest_addr_end) {
-    
-    struct v3_mem_region * entry = (struct v3_mem_region *)V3_Malloc(sizeof(struct v3_mem_region));
+    struct v3_mem_region * entry = NULL;
+
+    if (guest_addr_start >= guest_addr_end) {
+        PrintError(vm, VCORE_NONE, "Region start is after region end\n");
+       return NULL;
+    }
+
+    entry = (struct v3_mem_region *)V3_Malloc(sizeof(struct v3_mem_region));
+
+    if (!entry) {
+       PrintError(vm, VCORE_NONE, "Cannot allocate in creating a memory region\n");
+       return NULL;
+    }
+
     memset(entry, 0, sizeof(struct v3_mem_region));
 
     entry->guest_start = guest_addr_start;
@@ -173,7 +189,7 @@ struct v3_mem_region * __insert_mem_region(struct v3_vm_info * vm,
        } else {
            if ((region->guest_end != tmp_region->guest_end) ||
                (region->guest_start != tmp_region->guest_start)) {
-               PrintError("Trying to map a partial overlapped core specific page...\n");
+               PrintError(vm, VCORE_NONE, "Trying to map a partial overlapped core specific page...\n");
                return tmp_region; // This is ugly... 
            } else if (region->core_id == tmp_region->core_id) {
                return tmp_region;
@@ -269,7 +285,7 @@ struct v3_mem_region * v3_get_mem_region(struct v3_vm_info * vm, uint16_t core_i
                // go right, core too small
                n = n->rb_right;
            } else {
-               PrintDebug("v3_get_mem_region: Impossible!\n");
+               PrintDebug(vm, VCORE_NONE, "v3_get_mem_region: Impossible!\n");
                return NULL;
            }
        }
@@ -279,7 +295,7 @@ struct v3_mem_region * v3_get_mem_region(struct v3_vm_info * vm, uint16_t core_i
     // There is not registered region, so we check if its a valid address in the base region
 
     if (guest_addr > vm->mem_map.base_region.guest_end) {
-       PrintError("Guest Address Exceeds Base Memory Size (ga=0x%p), (limit=0x%p) (core=0x%x)\n", 
+       PrintError(vm, VCORE_NONE, "Guest Address Exceeds Base Memory Size (ga=0x%p), (limit=0x%p) (core=0x%x)\n", 
                   (void *)guest_addr, (void *)vm->mem_map.base_region.guest_end, core_id);
        v3_print_mem_map(vm);
 
@@ -301,6 +317,10 @@ static struct v3_mem_region * get_next_mem_region( struct v3_vm_info * vm, uint1
     struct v3_mem_region * reg = NULL;
     struct v3_mem_region * parent = NULL;
 
+    if (n == NULL) {
+       return NULL;
+    }
+
     while (n) {
 
        reg = rb_entry(n, struct v3_mem_region, tree_node);
@@ -323,7 +343,7 @@ static struct v3_mem_region * get_next_mem_region( struct v3_vm_info * vm, uint1
                // go right, core too small
                n = n->rb_right;
            } else {
-               PrintError("v3_get_mem_region: Impossible!\n");
+               PrintError(vm, VCORE_NONE, "v3_get_mem_region: Impossible!\n");
                return NULL;
            }
        }
@@ -369,7 +389,7 @@ static struct v3_mem_region * get_overlapping_region(struct v3_vm_info * vm, uin
     struct v3_mem_region * start_region = v3_get_mem_region(vm, core_id, start_gpa);
 
     if (start_region == NULL) {
-       PrintError("Invalid memory region\n");
+       PrintError(vm, VCORE_NONE, "Invalid memory region\n");
        return NULL;
     }
 
@@ -477,7 +497,7 @@ uint32_t v3_get_max_page_size(struct guest_info * core, addr_t page_addr, v3_cpu
                pg_start = PAGE_ADDR_4MB(page_addr);
                pg_end = (pg_start + PAGE_SIZE_4MB);
 
-               reg = get_overlapping_region(core->vm_info, core->cpu_id, pg_start, pg_end); 
+               reg = get_overlapping_region(core->vm_info, core->vcpu_id, pg_start, pg_end); 
 
                if ((reg) && ((reg->host_addr % PAGE_SIZE_4MB) == 0)) {
                    page_size = PAGE_SIZE_4MB;
@@ -489,7 +509,7 @@ uint32_t v3_get_max_page_size(struct guest_info * core, addr_t page_addr, v3_cpu
                pg_start = PAGE_ADDR_2MB(page_addr);
                pg_end = (pg_start + PAGE_SIZE_2MB);
 
-               reg = get_overlapping_region(core->vm_info, core->cpu_id, pg_start, pg_end);
+               reg = get_overlapping_region(core->vm_info, core->vcpu_id, pg_start, pg_end);
 
                if ((reg) && ((reg->host_addr % PAGE_SIZE_2MB) == 0)) {
                    page_size = PAGE_SIZE_2MB;
@@ -503,7 +523,7 @@ uint32_t v3_get_max_page_size(struct guest_info * core, addr_t page_addr, v3_cpu
                pg_start = PAGE_ADDR_1GB(page_addr);
                pg_end = (pg_start + PAGE_SIZE_1GB);
                
-               reg = get_overlapping_region(core->vm_info, core->cpu_id, pg_start, pg_end);
+               reg = get_overlapping_region(core->vm_info, core->vcpu_id, pg_start, pg_end);
                
                if ((reg) && ((reg->host_addr % PAGE_SIZE_1GB) == 0)) {
                    page_size = PAGE_SIZE_1GB;
@@ -515,7 +535,7 @@ uint32_t v3_get_max_page_size(struct guest_info * core, addr_t page_addr, v3_cpu
                pg_start = PAGE_ADDR_2MB(page_addr);
                pg_end = (pg_start + PAGE_SIZE_2MB);
 
-               reg = get_overlapping_region(core->vm_info, core->cpu_id, pg_start, pg_end);
+               reg = get_overlapping_region(core->vm_info, core->vcpu_id, pg_start, pg_end);
                
                if ((reg) && ((reg->host_addr % PAGE_SIZE_2MB) == 0)) {
                    page_size = PAGE_SIZE_2MB;
@@ -523,7 +543,7 @@ uint32_t v3_get_max_page_size(struct guest_info * core, addr_t page_addr, v3_cpu
            }
            break;
         default:
-            PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(core)));
+           PrintError(core->vm_info, core, "Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(core)));
             return -1;
     }
 
@@ -537,10 +557,10 @@ void v3_print_mem_map(struct v3_vm_info * vm) {
     struct v3_mem_region * reg = &(vm->mem_map.base_region);
     int i = 0;
 
-    V3_Print("Memory Layout (all cores):\n");
+    V3_Print(vm, VCORE_NONE, "Memory Layout (all cores):\n");
     
 
-    V3_Print("Base Region (all cores):  0x%p - 0x%p -> 0x%p\n", 
+    V3_Print(vm, VCORE_NONE, "Base Region (all cores):  0x%p - 0x%p -> 0x%p\n", 
               (void *)(reg->guest_start), 
               (void *)(reg->guest_end - 1), 
               (void *)(reg->host_addr));
@@ -554,12 +574,12 @@ void v3_print_mem_map(struct v3_vm_info * vm) {
     do {
        reg = rb_entry(node, struct v3_mem_region, tree_node);
 
-       V3_Print("%d:  0x%p - 0x%p -> 0x%p\n", i, 
+       V3_Print(vm, VCORE_NONE, "%d:  0x%p - 0x%p -> 0x%p\n", i, 
                   (void *)(reg->guest_start), 
                   (void *)(reg->guest_end - 1), 
                   (void *)(reg->host_addr));
 
-       V3_Print("\t(flags=0x%x) (core=0x%x) (unhandled = 0x%p)\n", 
+       V3_Print(vm, VCORE_NONE, "\t(flags=0x%x) (core=0x%x) (unhandled = 0x%p)\n", 
                 reg->flags.value, 
                 reg->core_id,
                 reg->unhandled);