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.


Cleanup and sanity-checking of explicit null derefs (Coverity static analysis)
[palacios.git] / palacios / src / palacios / vmm.c
index 6257294..dbb1b8d 100644 (file)
@@ -44,7 +44,6 @@
 #include <palacios/vmm_checkpoint.h>
 #endif
 
-
 v3_cpu_arch_t v3_cpu_types[V3_CONFIG_MAX_CPUS];
 v3_cpu_arch_t v3_mach_type = V3_INVALID_CPU;
 
@@ -151,6 +150,10 @@ void Init_V3(struct v3_os_hooks * hooks, char * cpu_mask, int num_cpus, char *op
        v3_cpu_types[i] = V3_INVALID_CPU;
     }
 
+#ifdef V3_CONFIG_CACHEPART
+    v3_init_cachepart();
+#endif
+
     // Parse host-os defined options into an easily-accessed format.
     v3_parse_options(options);
 
@@ -162,6 +165,7 @@ void Init_V3(struct v3_os_hooks * hooks, char * cpu_mask, int num_cpus, char *op
     v3_init_hvm();
 #endif
 
+
     // Memory manager initialization
     v3_init_mem();
 
@@ -273,6 +277,9 @@ void Shutdown_V3() {
 
     v3_deinit_options();
     
+#ifdef V3_CONFIG_CACHEPART
+    v3_deinit_cachepart();
+#endif
 
 }
 
@@ -332,7 +339,9 @@ struct v3_vm_info * v3_create_vm(void * cfg, void * priv_data, char * name, unsi
     }
 
     memset(vm->name, 0, 128);
-    strncpy(vm->name, name, 127);
+    strncpy(vm->name, name, 128);
+    vm->name[127] = 0;
+    
 
     if(v3_cpu_mapper_register_vm(vm) == -1) {
 
@@ -359,12 +368,17 @@ struct v3_vm_info * v3_create_vm(void * cfg, void * priv_data, char * name, unsi
        PrintDebug(vm, VCORE_NONE, "Creating virtual core %u on logical core %u\n",
                   vcore_id, core->pcpu_id);
 
-       sprintf(core->exec_name, "%s-%u", vm->name, vcore_id);
+       sprintf(core->exec_name, "%s-%d", 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);
 
-       core->core_thread = V3_CREATE_THREAD_ON_CPU(core->pcpu_id, start_core, core, core->exec_name);
+
+       // Resource controls for cores can be independent, but
+       // currently are not, hence this copy.
+       core->resource_control = vm->resource_control;
+
+       core->core_thread = V3_CREATE_THREAD_ON_CPU(core->pcpu_id, start_core, core, core->exec_name, &core->resource_control);
 
        if (core->core_thread == NULL) {
            PrintError(vm, VCORE_NONE, "Thread creation failed\n");
@@ -484,6 +498,103 @@ int v3_reset_vm_core(struct guest_info * core, addr_t rip) {
 }
 
 
+// resets the whole VM (non-HVM) or the ROS (HVM) 
+int v3_reset_vm(struct v3_vm_info *vm)
+{
+#ifdef V3_CONFIG_HVM
+    if (vm->hvm_state.is_hvm) { 
+       return v3_reset_vm_extended(vm,V3_VM_RESET_ROS,0);
+    } else {
+       return v3_reset_vm_extended(vm,V3_VM_RESET_ALL,0);
+    }
+#else
+    return v3_reset_vm_extended(vm,V3_VM_RESET_ALL,0);
+#endif
+}
+
+int v3_reset_vm_extended(struct v3_vm_info *vm, v3_vm_reset_type t, void *data)
+{
+    uint32_t start, end, i;
+    uint32_t newcount;
+
+    if (vm->run_state != VM_RUNNING) { 
+       PrintError(vm,VCORE_NONE,"Attempt to reset VM in state %d (must be in running state)\n",vm->run_state);
+       return -1;
+    }
+       
+
+    switch (t) { 
+       case V3_VM_RESET_ALL:
+#ifdef V3_CONFIG_HVM
+           if (vm->hvm_state.is_hvm) { 
+               PrintError(vm,VCORE_NONE,"Attempt to do ALL reset of HVM (not allowed)\n");
+               return -1;
+           }
+#endif
+           start=0; end=vm->num_cores-1;
+           break;
+#ifdef V3_CONFIG_HVM
+       case V3_VM_RESET_HRT:
+       case V3_VM_RESET_ROS:
+           if (vm->hvm_state.is_hvm) { 
+               if (t==V3_VM_RESET_HRT) { 
+                   start = vm->hvm_state.first_hrt_core;
+                   end = vm->num_cores-1;
+               } else {
+                   start = 0;
+                   end = vm->hvm_state.first_hrt_core-1;
+               }
+           } else {
+               PrintError(vm,VCORE_NONE,"This is not an HVM and so HVM-specific resets do not apply\n");
+               return -1;
+           }
+#endif
+           break;
+       case V3_VM_RESET_CORE_RANGE:
+           if (data) { 
+               start = ((uint32_t*)data)[0];
+               end = ((uint32_t*)data)[1];
+           } else {
+               return -1;
+           }
+           break;
+       default:
+           PrintError(vm,VCORE_NONE,"Unsupported reset type %d for this VM\n",t);
+           return -1;
+           break;
+    }
+
+    PrintDebug(vm,VCORE_NONE,"Resetting cores %d through %d\n",start,end);
+
+    newcount = end-start+1;
+    
+    for (i=start;i<=end;i++) { 
+       if (!(vm->cores[i].core_run_state == CORE_RUNNING || vm->cores[i].core_run_state == CORE_STOPPED)) {
+           PrintError(vm,VCORE_NONE,"Cannot reset VM as core %u is in state %d (must be running or stopped)\n",i,vm->cores[i].core_run_state);
+           return -1;
+       }
+    }
+
+
+    // This had better be the only thread using the barrier at this point...
+    v3_init_counting_barrier(&vm->reset_barrier,newcount);
+
+    // OK, I am the reseter, tell the relevant cores what to do
+    // each will atomically decrement the reset countdown and then
+    // spin waiting for it to hit zero.
+
+    for (i=start;i<=end;i++) { 
+       vm->cores[i].core_run_state = CORE_RESETTING;
+       // force exit of core
+       v3_interrupt_cpu(vm, vm->cores[i].pcpu_id, 0);
+    }
+    
+    // we don't wait for reset to finish
+    // because reset could have been initiated by a core
+
+    return 0;
+}
+
 
 /* move a virtual core to different physical core */
 int v3_move_vm_core(struct v3_vm_info * vm, int vcore_id, int target_cpu) {
@@ -630,7 +741,8 @@ int v3_move_vm_mem(struct v3_vm_info * vm, void *gpa, int target_cpu) {
     new_hpa = V3_AllocPagesExtended(num_pages,
                                    PAGE_SIZE_4KB,
                                    new_node,
-                                   0, 0);  // no constraints given new shadow pager impl
+                                   vm->resource_control.pg_filter_func,
+                                   vm->resource_control.pg_filter_state); 
 
     if (!new_hpa) { 
        PrintError(vm, VCORE_NONE, "Cannot allocate memory for new base region...\n");
@@ -638,6 +750,8 @@ int v3_move_vm_mem(struct v3_vm_info * vm, void *gpa, int target_cpu) {
     }
 
     // Note, assumes virtual contiguity in the host OS... 
+    // A null deref here can only happen if the host does not provide the
+    // physical_to_virtual functions or if they do not work
     memcpy(V3_VAddr((void*)new_hpa), V3_VAddr((void*)(reg->host_addr)), num_pages*PAGE_SIZE);
 
     old_hpa = (void*)(reg->host_addr);
@@ -884,9 +998,10 @@ int v3_get_state_vm(struct v3_vm_info        *vm,
     uint32_t numcores;
     uint32_t numregions; 
     extern uint64_t v3_mem_block_size;
+    void     *cur_gpa;
 
     if (!vm || !base || !core || !mem)  { 
-        PrintError(VM_NONE, VCORE_NONE, "Invalid rquest to v3_get_state_vm\n");
+        PrintError(VM_NONE, VCORE_NONE, "Invalid request to v3_get_state_vm\n");
        return -1;
     }
 
@@ -900,14 +1015,24 @@ int v3_get_state_vm(struct v3_vm_info        *vm,
        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;
+       case VM_RESETTING: base->state = V3_VM_RESETTING; break;
        default: base->state = V3_VM_UNKNOWN; break;
     }
 
+    base->vm_type = V3_VM_GENERAL;
+
+#ifdef V3_CONFIG_HVM
+    if (vm->hvm_state.is_hvm) { 
+       base->vm_type = V3_VM_HVM;
+    } 
+#endif
+
     for (i=0;i<numcores;i++) {
        switch (vm->cores[i].core_run_state) {
            case CORE_INVALID: core->vcore[i].state = V3_VCORE_INVALID; break;
            case CORE_RUNNING: core->vcore[i].state = V3_VCORE_RUNNING; break;
            case CORE_STOPPED: core->vcore[i].state = V3_VCORE_STOPPED; break;
+           case CORE_RESETTING: core->vcore[i].state = V3_VCORE_RESETTING; break;
            default: core->vcore[i].state = V3_VCORE_UNKNOWN; break;
        }
        switch (vm->cores[i].cpu_mode) {
@@ -929,6 +1054,18 @@ int v3_get_state_vm(struct v3_vm_info        *vm,
            case VIRTUAL_MEM: core->vcore[i].mem_mode=V3_VCORE_MEM_MODE_VIRTUAL; break;
            default: core->vcore[i].mem_mode=V3_VCORE_MEM_MODE_UNKNOWN; break;
        }
+
+       core->vcore[i].vcore_type = V3_VCORE_GENERAL;
+
+#ifdef V3_CONFIG_HVM
+       if (vm->hvm_state.is_hvm) { 
+           if (v3_is_hvm_ros_core(&vm->cores[i])) { 
+               core->vcore[i].vcore_type = V3_VCORE_ROS;
+           } else {
+               core->vcore[i].vcore_type = V3_VCORE_HRT;
+           }
+       }
+#endif
        
        core->vcore[i].pcore=vm->cores[i].pcpu_id;
        core->vcore[i].last_rip=(void*)(vm->cores[i].rip);
@@ -937,7 +1074,10 @@ int v3_get_state_vm(struct v3_vm_info        *vm,
     
     core->num_vcores=numcores;
 
+    cur_gpa=0;
+
     for (i=0;i<numregions;i++) {
+       mem->region[i].guest_paddr = cur_gpa;
        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
@@ -948,9 +1088,35 @@ int v3_get_state_vm(struct v3_vm_info        *vm,
        mem->region[i].pinned = 0;
 #endif
 
+       cur_gpa += mem->region[i].size;
     }
 
     mem->num_regions=numregions;
+
+
+    mem->mem_size=vm->mem_size;
+    mem->ros_mem_size=vm->mem_size;
+
+#ifdef V3_CONFIG_HVM
+    if (vm->hvm_state.is_hvm) { 
+       mem->ros_mem_size=v3_get_hvm_ros_memsize(vm);
+    }
+#endif
+
+    return 0;
+}
+
+int v3_get_state_sizes_vm(struct v3_vm_info        *vm, 
+                         unsigned long long       *num_vcores, 
+                         unsigned long long       *num_regions)
+{
+    if (!vm || !num_vcores || !num_regions) {
+        PrintError(VM_NONE, VCORE_NONE, "Invalid request to v3_get_state_sizes\n");
+       return -1;
+    }
+
+    *num_vcores = vm->num_cores;
+    *num_regions = vm->mem_map.num_base_regions;
     
     return 0;
 }
@@ -1062,18 +1228,6 @@ v3_cpu_mode_t v3_get_host_cpu_mode() {
 
 #endif 
 
-void v3_print_cond(const char * fmt, ...) {
-    if (v3_dbg_enable == 1) {
-       char buf[2048];
-       va_list ap;
-
-       va_start(ap, fmt);
-       vsnprintf(buf, 2048, fmt, ap);
-       va_end(ap);
-
-       V3_Print(VM_NONE, VCORE_NONE,"%s", buf);
-    }    
-}