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.


Basic HRT startup for HVM, plus assorted cleanup
[palacios.git] / palacios / src / palacios / vmm.c
index 12766b9..f45a4be 100644 (file)
@@ -31,6 +31,7 @@
 #include <palacios/vmm_cpu_mapper.h>
 #include <palacios/vmm_direct_paging.h>
 #include <interfaces/vmm_numa.h>
+#include <interfaces/vmm_file.h>
 
 #ifdef V3_CONFIG_SVM
 #include <palacios/svm.h>
@@ -153,6 +154,10 @@ void Init_V3(struct v3_os_hooks * hooks, char * cpu_mask, int num_cpus, char *op
     // Parse host-os defined options into an easily-accessed format.
     v3_parse_options(options);
 
+#ifdef V3_CONFIG_HVM
+    v3_init_hvm();
+#endif
+
     // Memory manager initialization
     v3_init_mem();
 
@@ -162,6 +167,10 @@ void Init_V3(struct v3_os_hooks * hooks, char * cpu_mask, int num_cpus, char *op
     // Register all shadow paging handlers
     V3_init_shdw_paging();
 
+#ifdef V3_CONFIG_SWAPPING
+    v3_init_swapping();
+#endif
+
     // Initialize the cpu_mapper framework (must be before extensions)
     V3_init_cpu_mapper();
 
@@ -239,6 +248,10 @@ void Shutdown_V3() {
     V3_deinit_scheduling();
     
     V3_deinit_cpu_mapper();
+
+#ifdef V3_CONFIG_SWAPPING
+    v3_deinit_swapping();
+#endif
     
     V3_deinit_shdw_paging();
     
@@ -246,6 +259,10 @@ void Shutdown_V3() {
 
     v3_deinit_mem();
     
+#ifdef V3_CONFIG_HVM
+    v3_deinit_hvm();
+#endif
+
     v3_deinit_options();
     
 
@@ -256,9 +273,42 @@ v3_cpu_arch_t v3_get_cpu_type(int cpu_id) {
     return v3_cpu_types[cpu_id];
 }
 
+static int start_core(void * p)
+{
+    struct guest_info * core = (struct guest_info *)p;
+
+    if (v3_scheduler_register_core(core) == -1){
+        PrintError(core->vm_info, core,"Error initializing scheduling in core %d\n", core->vcpu_id);
+    }
+
+    PrintDebug(core->vm_info,core,"virtual core %u (on logical core %u): in start_core (RIP=%p)\n",
+              core->vcpu_id, core->pcpu_id, (void *)(addr_t)core->rip);
 
-struct v3_vm_info * v3_create_vm(void * cfg, void * priv_data, char * name) {
+    switch (v3_mach_type) {
+#ifdef V3_CONFIG_SVM
+       case V3_SVM_CPU:
+       case V3_SVM_REV3_CPU:
+           return v3_start_svm_guest(core);
+           break;
+#endif
+#if V3_CONFIG_VMX
+       case V3_VMX_CPU:
+       case V3_VMX_EPT_CPU:
+       case V3_VMX_EPT_UG_CPU:
+           return v3_start_vmx_guest(core);
+           break;
+#endif
+       default:
+           PrintError(core->vm_info, core, "Attempting to enter a guest on an invalid CPU\n");
+           return -1;
+    }
+    // should not happen
+    return 0;
+}
+
+struct v3_vm_info * v3_create_vm(void * cfg, void * priv_data, char * name, unsigned int cpu_mask) {
     struct v3_vm_info * vm = v3_config_guest(cfg, priv_data);
+    int vcore_id = 0;
 
     if (vm == NULL) {
        PrintError(VM_NONE, VCORE_NONE, "Could not configure guest\n");
@@ -290,43 +340,32 @@ struct v3_vm_info * v3_create_vm(void * cfg, void * priv_data, char * name) {
         PrintError(vm, VCORE_NONE,"Error registering VM with scheduler\n");
     }
 
-    return vm;
-}
+    if (v3_cpu_mapper_admit_vm(vm,cpu_mask) != 0){
+        PrintError(vm, VCORE_NONE,"Error admitting VM %s for mapping", vm->name);
+    }
 
+    for (vcore_id = 0; vcore_id < vm->num_cores; vcore_id++) {
 
+        struct guest_info * core = &(vm->cores[vcore_id]);
 
+       PrintDebug(vm, VCORE_NONE, "Creating virtual core %u on logical core %u\n",
+                  vcore_id, core->pcpu_id);
 
-static int start_core(void * p)
-{
-    struct guest_info * core = (struct guest_info *)p;
+       sprintf(core->exec_name, "%s-%u", vm->name, vcore_id);
 
-    if (v3_scheduler_register_core(core) == -1){
-        PrintError(core->vm_info, core,"Error initializing scheduling in core %d\n", core->vcpu_id);
-    }
+        PrintDebug(vm, VCORE_NONE, "run: core=%u, func=0x%p, arg=0x%p, name=%s\n",
+                  core->pcpu_id, start_core, core, core->exec_name);
 
-    PrintDebug(core->vm_info,core,"virtual core %u (on logical core %u): in start_core (RIP=%p)\n", 
-              core->vcpu_id, core->pcpu_id, (void *)(addr_t)core->rip);
+       core->core_thread = V3_CREATE_THREAD_ON_CPU(core->pcpu_id, start_core, core, core->exec_name);
+
+       if (core->core_thread == NULL) {
+           PrintError(vm, VCORE_NONE, "Thread creation failed\n");
+           v3_stop_vm(vm);
+           return NULL;
+       }
 
-    switch (v3_mach_type) {
-#ifdef V3_CONFIG_SVM
-       case V3_SVM_CPU:
-       case V3_SVM_REV3_CPU:
-           return v3_start_svm_guest(core);
-           break;
-#endif
-#if V3_CONFIG_VMX
-       case V3_VMX_CPU:
-       case V3_VMX_EPT_CPU:
-       case V3_VMX_EPT_UG_CPU:
-           return v3_start_vmx_guest(core);
-           break;
-#endif
-       default:
-           PrintError(core->vm_info, core, "Attempting to enter a guest on an invalid CPU\n");
-           return -1;
     }
-    // should not happen
-    return 0;
+    return vm;
 }
 
 int v3_start_vm(struct v3_vm_info * vm, unsigned int cpu_mask) {
@@ -336,11 +375,22 @@ int v3_start_vm(struct v3_vm_info * vm, unsigned int cpu_mask) {
     uint32_t avail_cores = 0;
     int vcore_id = 0;
 
+    if (!vm) { 
+        PrintError(VM_NONE, VCORE_NONE, "Asked to start nonexistent VM\n");
+       return -1;
+    }
+
     if (vm->run_state != VM_STOPPED) {
         PrintError(vm, VCORE_NONE, "VM has already been launched (state=%d)\n", (int)vm->run_state);
         return -1;
     }
 
+#if V3_CONFIG_HVM
+    if (v3_setup_hvm_vm_for_boot(vm)) { 
+       PrintError(vm, VCORE_NONE, "HVM setup for boot failed\n");
+       return -1;
+    }
+#endif
 
     /// CHECK IF WE ARE MULTICORE ENABLED....
 
@@ -363,10 +413,6 @@ int v3_start_vm(struct v3_vm_info * vm, unsigned int cpu_mask) {
     }
 
     vm->avail_cores = avail_cores;
-    if (v3_cpu_mapper_admit_vm(vm,cpu_mask) != 0){
-        PrintError(vm, VCORE_NONE,"Error admitting VM %s for mapping", vm->name);
-    }
 
     if (v3_scheduler_admit_vm(vm) != 0){
        PrintError(vm, VCORE_NONE,"Error admitting VM %s for scheduling", vm->name);
@@ -374,27 +420,23 @@ int v3_start_vm(struct v3_vm_info * vm, unsigned int cpu_mask) {
 
     vm->run_state = VM_RUNNING;
 
-
     for (vcore_id = 0; vcore_id < vm->num_cores; vcore_id++) {
 
         struct guest_info * core = &(vm->cores[vcore_id]);
 
        PrintDebug(vm, VCORE_NONE, "Starting virtual core %u on logical core %u\n", 
                   vcore_id, core->pcpu_id);
-       
-       sprintf(core->exec_name, "%s-%u", vm->name, vcore_id);
 
-        PrintDebug(vm, VCORE_NONE, "run: core=%u, func=0x%p, arg=0x%p, name=%s\n",
-                  core->pcpu_id, start_core, core, core->exec_name);
+       if (core->core_run_state==CORE_INVALID) {
+         // launch of a fresh VM
+         core->core_run_state = CORE_STOPPED;
+         // core zero will turn itself on
+       } else {
+         // this is a resume - use whatever its current run_state is
+       }
 
-       core->core_run_state = CORE_STOPPED;  // core zero will turn itself on
-       core->core_thread = V3_CREATE_THREAD_ON_CPU(core->pcpu_id, start_core, core, core->exec_name);
+       V3_START_THREAD(core->core_thread);
 
-       if (core->core_thread == NULL) {
-           PrintError(vm, VCORE_NONE, "Thread launch failed\n");
-           v3_stop_vm(vm);
-           return -1;
-       }
     }
 
     return 0;
@@ -433,6 +475,11 @@ int v3_reset_vm_core(struct guest_info * core, addr_t rip) {
 int v3_move_vm_core(struct v3_vm_info * vm, int vcore_id, int target_cpu) {
     struct guest_info * core = NULL;
 
+    if (!vm) { 
+        PrintError(VM_NONE, VCORE_NONE, "Asked to move core of nonexistent VM\n");
+       return -1;
+    }
+
     if ((vcore_id < 0) || (vcore_id >= vm->num_cores)) {
         PrintError(vm, VCORE_NONE, "Attempted to migrate invalid virtual core (%d)\n", vcore_id);
        return -1;
@@ -510,6 +557,11 @@ int v3_move_vm_mem(struct v3_vm_info * vm, void *gpa, int target_cpu) {
     void *old_hpa;
     int i;
 
+    if (!vm) { 
+        PrintError(VM_NONE, VCORE_NONE, "Asked to move memory of nonexistent VM\n");
+       return -1;
+    }
+
     old_node = v3_numa_gpa_to_node(vm,(addr_t)gpa);
 
     if (old_node<0) { 
@@ -587,7 +639,7 @@ int v3_move_vm_mem(struct v3_vm_info * vm, void *gpa, int target_cpu) {
            v3_invalidate_shadow_pts(&(vm->cores[i]));
        } else if (vm->cores[i].shdw_pg_mode==NESTED_PAGING) { 
            // nested invalidator uses inclusive addressing [start,end], not [start,end)
-           v3_invalidate_nested_addr_range(&(vm->cores[i]),reg->guest_start,reg->guest_end-1);
+         v3_invalidate_nested_addr_range(&(vm->cores[i]),reg->guest_start,reg->guest_end-1,NULL,NULL);
        } else {
            PrintError(vm,VCORE_NONE, "Cannot determine how to invalidate paging structures! Reverting to previous region.\n");
            // We'll restore things...
@@ -618,6 +670,11 @@ int v3_stop_vm(struct v3_vm_info * vm) {
 
     struct guest_info * running_core;
 
+    if (!vm) { 
+        PrintError(VM_NONE, VCORE_NONE, "Asked to stop nonexistent VM\n");
+       return -1;
+    }
+
     if ((vm->run_state != VM_RUNNING) && 
        (vm->run_state != VM_SIMULATING)) {
         PrintError(vm, VCORE_NONE,"Tried to stop VM in invalid runstate (%d)\n", vm->run_state);
@@ -659,6 +716,11 @@ int v3_stop_vm(struct v3_vm_info * vm) {
 
 int v3_pause_vm(struct v3_vm_info * vm) {
 
+    if (!vm) { 
+        PrintError(VM_NONE, VCORE_NONE, "Asked to pause nonexistent VM\n");
+       return -1;
+    }
+
     if (vm->run_state != VM_RUNNING) {
        PrintError(vm, VCORE_NONE,"Tried to pause a VM that was not running\n");
        return -1;
@@ -674,6 +736,11 @@ int v3_pause_vm(struct v3_vm_info * vm) {
 
 int v3_continue_vm(struct v3_vm_info * vm) {
 
+    if (!vm) { 
+        PrintError(VM_NONE, VCORE_NONE, "Asked to continue nonexistent VM\n");
+       return -1;
+    }
+
     if (vm->run_state != VM_PAUSED) {
        PrintError(vm, VCORE_NONE,"Tried to continue a VM that was not paused\n");
        return -1;
@@ -713,6 +780,11 @@ int v3_simulate_vm(struct v3_vm_info * vm, unsigned int msecs) {
     uint64_t cycles = 0;
     uint64_t cpu_khz = V3_CPU_KHZ();
 
+    if (!vm) { 
+        PrintError(VM_NONE, VCORE_NONE, "Asked to simulate nonexistent VM\n");
+       return -1;
+    }
+
     if (vm->run_state != VM_PAUSED) {
        PrintError(vm, VCORE_NONE,"VM must be paused before simulation begins\n");
        return -1;
@@ -795,14 +867,23 @@ int v3_get_state_vm(struct v3_vm_info        *vm,
                    struct v3_vm_mem_state   *mem)
 {
     uint32_t i;
-    uint32_t numcores = core->num_vcores > vm->num_cores ? vm->num_cores : core->num_vcores;
-    uint32_t numregions = mem->num_regions > vm->mem_map.num_base_regions ? vm->mem_map.num_base_regions : mem->num_regions;
+    uint32_t numcores;
+    uint32_t numregions; 
     extern uint64_t v3_mem_block_size;
 
+    if (!vm || !base || !core || !mem)  { 
+        PrintError(VM_NONE, VCORE_NONE, "Invalid rquest to v3_get_state_vm\n");
+       return -1;
+    }
+
+    numcores = core->num_vcores > vm->num_cores ? vm->num_cores : core->num_vcores;
+    numregions = mem->num_regions > vm->mem_map.num_base_regions ? vm->mem_map.num_base_regions : mem->num_regions;
+
     switch (vm->run_state) { 
        case VM_INVALID: base->state = V3_VM_INVALID; break;
        case VM_RUNNING: base->state = V3_VM_RUNNING; break;
        case VM_STOPPED: base->state = V3_VM_STOPPED; break;
+       case VM_RESETTING:  base->state = V3_VM_RESETTING; break;
        case VM_PAUSED: base->state = V3_VM_PAUSED; break;
        case VM_ERROR: base->state = V3_VM_ERROR; break;
        case VM_SIMULATING: base->state = V3_VM_SIMULATING; break;
@@ -843,9 +924,17 @@ int v3_get_state_vm(struct v3_vm_info        *vm,
     
     core->num_vcores=numcores;
 
-    for (i=0;i<vm->mem_map.num_base_regions;i++) {
+    for (i=0;i<numregions;i++) {
        mem->region[i].host_paddr =  (void*)(vm->mem_map.base_regions[i].host_addr);
        mem->region[i].size = v3_mem_block_size;
+#ifdef V3_CONFIG_SWAPPING
+       mem->region[i].swapped = vm->mem_map.base_regions[i].flags.swapped;
+       mem->region[i].pinned = vm->mem_map.base_regions[i].flags.pinned;
+#else
+       mem->region[i].swapped = 0;
+       mem->region[i].pinned = 0;
+#endif
+
     }
 
     mem->num_regions=numregions;
@@ -858,22 +947,38 @@ int v3_get_state_vm(struct v3_vm_info        *vm,
 #include <palacios/vmm_checkpoint.h>
 
 int v3_save_vm(struct v3_vm_info * vm, char * store, char * url, v3_chkpt_options_t opts) {
-  return v3_chkpt_save_vm(vm, store, url, opts);
+    if (!vm || !store || !url) {
+       PrintError(VM_NONE,VCORE_NONE, "Incorrect arguemnts for v3_save_vm\n");
+       return -1;
+    }
+    return v3_chkpt_save_vm(vm, store, url, opts);
 }
 
 
 int v3_load_vm(struct v3_vm_info * vm, char * store, char * url, v3_chkpt_options_t opts) {
-  return v3_chkpt_load_vm(vm, store, url, opts);
+    if (!vm || !store || !url) {
+       PrintError(VM_NONE,VCORE_NONE, "Incorrect arguemnts for v3_load_vm\n");
+       return -1;
+    }
+    return v3_chkpt_load_vm(vm, store, url, opts);
 }
 
 #ifdef V3_CONFIG_LIVE_MIGRATION
 int v3_send_vm(struct v3_vm_info * vm, char * store, char * url, v3_chkpt_options_t opts) {
-  return v3_chkpt_send_vm(vm, store, url, opts);
+    if (!vm || !store || !url) {
+       PrintError(VM_NONE,VCORE_NONE, "Incorrect arguemnts for v3_send_vm\n");
+       return -1;
+    }
+    return v3_chkpt_send_vm(vm, store, url, opts);
 }
 
 
 int v3_receive_vm(struct v3_vm_info * vm, char * store, char * url, v3_chkpt_options_t opts) {
-  return v3_chkpt_receive_vm(vm, store, url, opts);
+    if (!vm || !store || !url) {
+       PrintError(VM_NONE,VCORE_NONE, "Incorrect arguemnts for v3_receive_vm\n");
+       return -1;
+    }
+    return v3_chkpt_receive_vm(vm, store, url, opts);
 }
 #endif
 
@@ -884,6 +989,11 @@ int v3_free_vm(struct v3_vm_info * vm) {
     int i = 0;
     // deinitialize guest (free memory, etc...)
 
+    if (!vm) { 
+        PrintError(VM_NONE, VCORE_NONE, "Asked to free nonexistent VM\n");
+       return -1;
+    }
+
     if ((vm->run_state != VM_STOPPED) &&
        (vm->run_state != VM_ERROR)) {
        PrintError(vm, VCORE_NONE,"Tried to Free VM in invalid runstate (%d)\n", vm->run_state);