From: Peter Dinda Date: Mon, 7 Jul 2014 18:43:42 +0000 (-0500) Subject: Assorted bug fixes and defensive programming X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=commitdiff_plain;h=604e1d91e2cb21ca9b0afcb9a27cabd7119d67a4 Assorted bug fixes and defensive programming - Sanity check top-level v3_ calls from host - Eliminate vm free bug when config of shadow-paging vm terminates before passthrough paging is initialized - Linux module - avoid v3_stop/free on cleanup for orphaned VMs (guest_map out of sync) --- diff --git a/linux_module/main.c b/linux_module/main.c index 2da1b22..38a2fd4 100644 --- a/linux_module/main.c +++ b/linux_module/main.c @@ -684,8 +684,13 @@ static void __exit v3_exit(void) { /* Stop and free any running VMs */ for (i = 0; i < MAX_VMS; i++) { - if (guest_map[i] != NULL) { - guest = (struct v3_guest *)guest_map[i]; + if (guest_map[i] != NULL) { + guest = (struct v3_guest *)(guest_map[i]); + + if (!guest->v3_ctx) { + ERROR("Orphan VM detected and skipped: index=%d name=%s\n", i, guest->name); + continue; + } if (v3_stop_vm(guest->v3_ctx) < 0) ERROR("Couldn't stop VM %d\n", i); diff --git a/palacios/include/palacios/vmm_paging.h b/palacios/include/palacios/vmm_paging.h index 9b95d1b..ca426af 100644 --- a/palacios/include/palacios/vmm_paging.h +++ b/palacios/include/palacios/vmm_paging.h @@ -573,6 +573,7 @@ pdpe32pae_t * create_passthrough_pts_32PAE(struct guest_info * guest_info); pml4e64_t * create_passthrough_pts_64(struct guest_info * info); +// note that these take host virtual addresses void delete_page_tables_32(pde32_t * pde); void delete_page_tables_32pae(pdpe32pae_t * pdpe); void delete_page_tables_64(pml4e64_t * pml4); diff --git a/palacios/src/palacios/vmm.c b/palacios/src/palacios/vmm.c index e16e2fa..490f106 100644 --- a/palacios/src/palacios/vmm.c +++ b/palacios/src/palacios/vmm.c @@ -345,6 +345,11 @@ 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; @@ -449,6 +454,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; @@ -526,6 +536,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) { @@ -634,6 +649,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); @@ -675,6 +695,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; @@ -690,6 +715,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; @@ -729,6 +759,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; @@ -811,10 +846,18 @@ 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; @@ -882,22 +925,38 @@ int v3_get_state_vm(struct v3_vm_info *vm, #include 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 @@ -908,6 +967,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); diff --git a/palacios/src/palacios/vmm_direct_paging.c b/palacios/src/palacios/vmm_direct_paging.c index f313e8d..2884923 100644 --- a/palacios/src/palacios/vmm_direct_paging.c +++ b/palacios/src/palacios/vmm_direct_paging.c @@ -200,7 +200,9 @@ int v3_free_passthrough_pts(struct guest_info * core) { case LONG: case LONG_32_COMPAT: // Long mode will only use 32PAE page tables... - delete_page_tables_32pae((pdpe32pae_t *)V3_VAddr((void *)(core->direct_map_pt))); + if (core->direct_map_pt) { + delete_page_tables_32pae((pdpe32pae_t *)V3_VAddr((void *)(core->direct_map_pt))); + } break; default: PrintError(core->vm_info, core, "Unknown CPU Mode\n");