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.


Deallocation bug fix in device manager to allow backing out from VM creation failure
[palacios.git] / palacios / src / palacios / vmm_dev_mgr.c
index e6fad69..0904ea1 100644 (file)
@@ -24,6 +24,9 @@
 
 #ifdef V3_CONFIG_CHECKPOINT
 #include <palacios/vmm_checkpoint.h>
+
+#define V3_MAX_DEVICE_NAME 32
+
 #endif
 
 
@@ -57,28 +60,28 @@ int V3_init_devices() {
 #ifdef V3_CONFIG_DEBUG_DEV_MGR
     {
        int num_devices = (__stop__v3_devices - __start__v3_devices) / sizeof(struct v3_device_info);
-       PrintDebug("%d Virtual devices registered with Palacios\n", num_devices);
+       PrintDebug(VM_NONE, VCORE_NONE, "%d Virtual devices registered with Palacios\n", num_devices);
     }
 #endif
 
-    PrintDebug("Start addres=%p, Stop address=%p\n", __start__v3_devices, __stop__v3_devices);
+    PrintDebug(VM_NONE, VCORE_NONE, "Start addres=%p, Stop address=%p\n", __start__v3_devices, __stop__v3_devices);
 
     master_dev_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
 
 
 
     while (tmp_dev != __stop__v3_devices) {
-       PrintDebug("Device: %s\n", tmp_dev->name);
+       V3_Print(VM_NONE, VCORE_NONE, "Registering Device: %s\n", tmp_dev->name);
 
        if (v3_htable_search(master_dev_table, (addr_t)(tmp_dev->name))) {
-           PrintError("Multiple instance of device (%s)\n", tmp_dev->name);
+           PrintError(VM_NONE, VCORE_NONE, "Multiple instance of device (%s)\n", tmp_dev->name);
            return -1;
        }
 
        if (v3_htable_insert(master_dev_table, 
                             (addr_t)(tmp_dev->name), 
                             (addr_t)(tmp_dev->init)) == 0) {
-           PrintError("Could not add device %s to master list\n", tmp_dev->name);
+           PrintError(VM_NONE, VCORE_NONE, "Could not add device %s to master list\n", tmp_dev->name);
            return -1;
        }
 
@@ -91,7 +94,10 @@ int V3_init_devices() {
 
 
 int V3_deinit_devices() {    
-    v3_free_htable(master_dev_table, 0, 0);
+    if (master_dev_table) { 
+       v3_free_htable(master_dev_table, 0, 0);
+       master_dev_table=0;
+    }
     return 0;
 }
 
@@ -113,7 +119,9 @@ int v3_init_dev_mgr(struct v3_vm_info * vm) {
     mgr->net_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
     mgr->char_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
     mgr->cons_table = v3_create_htable(0, dev_hash_fn, dev_eq_fn);
-    
+
+    mgr->inited = 1;
+
     return 0;
 }
 
@@ -123,8 +131,10 @@ int v3_free_vm_devices(struct v3_vm_info * vm) {
     struct vm_device * dev;
     struct vm_device * tmp;
 
-    list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
-       v3_remove_device(dev);
+    if (mgr && mgr->num_devs > 0) { 
+       list_for_each_entry_safe(dev, tmp, &(mgr->dev_list), dev_link) {
+           v3_remove_device(dev);
+       }
     }
 
     return 0;
@@ -138,57 +148,178 @@ int v3_save_vm_devices(struct v3_vm_info * vm, struct v3_chkpt * chkpt) {
     struct v3_chkpt_ctx * dev_mgr_ctx = NULL;
 
     uint32_t num_saved_devs = 0;
-    uint32_t table_len = mgr->num_devs * 32;
+    uint32_t table_len = mgr->num_devs * V3_MAX_DEVICE_NAME;
     char * name_table = NULL;
     uint32_t tbl_offset = 0;
     
     name_table = V3_Malloc(table_len);
 
+    if (!name_table) { 
+        PrintError(vm, VCORE_NONE, "Unable to allocate space in device manager save\n");
+       return -1;
+    }
+
     memset(name_table, 0, table_len);
     
+    dev_mgr_ctx = v3_chkpt_open_ctx(chkpt, "devices");
 
-    dev_mgr_ctx = v3_chkpt_open_ctx(chkpt, NULL, "devices");
+    if (!dev_mgr_ctx) { 
+       PrintError(vm, VCORE_NONE,"Unable to open device manager context\n");
+       V3_Free(name_table);
+       return -1;
+    }
 
     list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
+        if (dev->ops->save) {
+            strncpy(name_table + tbl_offset, dev->name, V3_MAX_DEVICE_NAME);
+            tbl_offset += V3_MAX_DEVICE_NAME;
+            num_saved_devs++;
+        }  else {
+         PrintDebug(vm, VCORE_NONE, "Skipping device %s\n", dev->name);
+       }
+    }
+
+    if (v3_chkpt_save(dev_mgr_ctx, "num_devs", 4, &num_saved_devs) == -1) {
+       PrintError(vm, VCORE_NONE,"Unable to store num_devs\n");
+       v3_chkpt_close_ctx(dev_mgr_ctx); 
+       V3_Free(name_table);
+       return -1;
+    }
 
-       if (dev->ops->save) {
-           struct v3_chkpt_ctx * dev_ctx = NULL;
+    if (v3_chkpt_save(dev_mgr_ctx, "names", num_saved_devs*V3_MAX_DEVICE_NAME, name_table) == -1) {
+       PrintError(vm, VCORE_NONE,"Unable to store names of devices\n");
+       v3_chkpt_close_ctx(dev_mgr_ctx); 
+       V3_Free(name_table);
+       return -1;
+    }
+    
+    v3_chkpt_close_ctx(dev_mgr_ctx);
 
-           
-           dev_ctx = v3_chkpt_open_ctx(chkpt, dev_mgr_ctx, dev->name);
+    V3_Free(name_table);
 
-           dev->ops->save(dev_ctx, dev->private_data);
+    list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
 
-           v3_chkpt_close_ctx(dev_ctx);
+      if (dev->ops->save_extended) { 
 
-           // Error checking?? 
+       V3_Print(vm, VCORE_NONE,"Saving state for device (%s) using extended interface\n",dev->name);
 
-           strncpy(name_table + tbl_offset, dev->name, 32);
-           tbl_offset += 32;
-           num_saved_devs++;
-       } else {
-           PrintError("Error: %s save() not implemented\n",  dev->name);
+       if (dev->ops->save_extended(chkpt,dev->name,dev->private_data)) {
+         PrintError(vm, VCORE_NONE,"Unable to save device %s\n",dev->name);
+         return -1;
        }
-    }
 
-    
-    // Specify which devices were saved
-    v3_chkpt_save(dev_mgr_ctx, "num_devs", 4, &num_saved_devs); 
-    v3_chkpt_save(dev_mgr_ctx, "names", table_len, name_table);
-    V3_Free(name_table);
+      } else if (dev->ops->save) {
 
-    v3_chkpt_close_ctx(dev_mgr_ctx);
+       struct v3_chkpt_ctx * dev_ctx = NULL;
+       
+       V3_Print(vm, VCORE_NONE,"Saving state for device (%s)\n", dev->name);
+       
+       dev_ctx = v3_chkpt_open_ctx(chkpt, dev->name);
+       
+       if (!dev_ctx) { 
+         PrintError(vm, VCORE_NONE,"Unable to open context for device %s\n",dev->name);
+         return -1;
+       }
+       
+       if (dev->ops->save(dev_ctx, dev->private_data)) {
+         PrintError(vm, VCORE_NONE,"Unable t save device %s\n",dev->name);
+         v3_chkpt_close_ctx(dev_ctx); 
+         return -1;
+       }
+       
+       v3_chkpt_close_ctx(dev_ctx);
+       
+      } else {
+       PrintError(vm, VCORE_NONE,"Error: %s save() not implemented\n",  dev->name);
+      }
+    }
     
     return 0;
 }
 
 
 int v3_load_vm_devices(struct v3_vm_info * vm, struct v3_chkpt * chkpt) {
+    struct vm_device * dev;
+    struct v3_chkpt_ctx * dev_mgr_ctx = NULL;
+    uint32_t num_devs = 0;
+    char * name_table = NULL;
+    int i = 0;
 
-    PrintError("TODO... \n");
+    dev_mgr_ctx = v3_chkpt_open_ctx(chkpt, "devices");
 
-    // Read devices from checkpoint data
-    // selectively load them
+    if (!dev_mgr_ctx) { 
+       PrintError(vm, VCORE_NONE,"Unable to open devices for load\n");
+       return -1;
+    }
+
+    if (v3_chkpt_load(dev_mgr_ctx, "num_devs", 4, &num_devs) == -1) {
+       PrintError(vm, VCORE_NONE,"Unable to load num_devs\n");
+       v3_chkpt_close_ctx(dev_mgr_ctx);
+       return -1;
+    }
+
+    V3_Print(vm, VCORE_NONE,"Loading State for %d devices\n", num_devs);
+    
+    name_table = V3_Malloc(V3_MAX_DEVICE_NAME * num_devs);
+    
+    if (!name_table) { 
+       PrintError(vm, VCORE_NONE,"Unable to allocate space for device table\n");
+       v3_chkpt_close_ctx(dev_mgr_ctx);
+       return -1;
+    }
+
+    if (v3_chkpt_load(dev_mgr_ctx, "names", V3_MAX_DEVICE_NAME * num_devs, name_table) == -1) {
+       PrintError(vm, VCORE_NONE,"Unable to load device name table\n");
+       v3_chkpt_close_ctx(dev_mgr_ctx);
+       V3_Free(name_table);
+    }
+
+    v3_chkpt_close_ctx(dev_mgr_ctx);
+
+    for (i = 0; i < num_devs; i++) {
+       char * name = &(name_table[i * V3_MAX_DEVICE_NAME]);
+       struct v3_chkpt_ctx * dev_ctx = NULL;
+       dev = v3_find_dev(vm, name);
+
+       if (!dev) {
+           PrintError(vm, VCORE_NONE,"Tried to load state into non existant device: %s\n", name);
+           return -1;
+       }
+
+       if (dev->ops->load_extended) {
+
+         V3_Print(vm, VCORE_NONE,"Loading state for device (%s) using extended interface\n",name);
+
+         if (dev->ops->load_extended(chkpt,name,dev->private_data)) { 
+           PrintError(vm, VCORE_NONE,"Load of device %s failed\n",name);
+           return -1;
+         } 
+
+       } else if (dev->ops->load) {
+         
+         dev_ctx = v3_chkpt_open_ctx(chkpt, name);
+
+         if (!dev_ctx) {
+           PrintError(vm, VCORE_NONE,"Error missing device context (%s)\n", name);
+           return -1;
+         }
+
+         if (dev->ops->load(dev_ctx, dev->private_data)) { 
+           PrintError(vm, VCORE_NONE,"Load of device %s failed\n",name);
+           v3_chkpt_close_ctx(dev_ctx);        
+           return -1;
+         }
+
+         // should close context regardless of whether load was successful
+         v3_chkpt_close_ctx(dev_ctx);
+
+       }  else {
+         PrintError(vm, VCORE_NONE,"Error Device (%s) does not support load operation\n", name);
+         // this is OK
+       }
+    }
+
+    V3_Free(name_table);
 
     return 0;
 }
@@ -199,15 +330,25 @@ int v3_load_vm_devices(struct v3_vm_info * vm, struct v3_chkpt * chkpt) {
 static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr);
 
 int v3_deinit_dev_mgr(struct v3_vm_info * vm) {
-    struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
-    
-    // clear frontend lists
+    struct vmm_dev_mgr * mgr=0;
 
-    free_frontends(vm, mgr);
+    if (vm) { 
+       mgr = &(vm->dev_mgr);
+       if (!mgr->inited) { 
+           return 0;
+       }
+    } else {
+       return 0;
+    }
 
+    // clear frontend lists
 
+    free_frontends(vm, mgr);
 
-    v3_free_htable(mgr->dev_table, 0, 0);
+    if (mgr->dev_table) { 
+       v3_free_htable(mgr->dev_table, 0, 0);
+       mgr->dev_table=0;
+    }
 
     return 0;
 }
@@ -220,13 +361,13 @@ int v3_create_device(struct v3_vm_info * vm, const char * dev_name, v3_cfg_tree_
     dev_init = (void *)v3_htable_search(master_dev_table, (addr_t)dev_name);
 
     if (dev_init == NULL) {
-       PrintError("Could not find device %s in master device table\n", dev_name);
+       PrintError(vm, VCORE_NONE,"Could not find device %s in master device table\n", dev_name);
        return -1;
     }
 
 
     if (dev_init(vm, cfg) == -1) {
-       PrintError("Could not initialize Device %s\n", dev_name);
+       PrintError(vm, VCORE_NONE,"Could not initialize Device %s\n", dev_name);
        return -1;
     }
 
@@ -268,7 +409,7 @@ static int add_resource(struct vm_device * dev, dev_rsrc_type_t type, uint64_t r
     resource = V3_Malloc(sizeof(struct dev_rsrc));
 
     if (resource == NULL) {
-       PrintError("Error: Could not allocate device resource\n");
+       PrintError(VM_NONE, VCORE_NONE,"Error: Could not allocate device resource\n");
        return -1;
     }
 
@@ -314,7 +455,7 @@ int v3_dev_hook_io(struct vm_device * dev, uint16_t port,
 
     if (add_resource(dev, DEV_IO_HOOK, port) == -1) {
        v3_unhook_io_port(dev->vm, port);
-       PrintError("Could not allocate io hook dev state\n");
+       PrintError(dev->vm, VCORE_NONE,"Could not allocate io hook dev state\n");
        return -1;
     }
     
@@ -380,7 +521,7 @@ int v3_remove_device(struct vm_device * dev) {
     if (dev->ops->free) {
        dev->ops->free(dev->private_data);
     } else {
-       PrintError("Error: %s free() not implemented\n",  dev->name);
+        PrintError(VM_NONE, VCORE_NONE,"Error: %s free() not implemented\n",  dev->name);
     }
 
     list_del(&(dev->dev_link));
@@ -400,9 +541,17 @@ struct vm_device * v3_add_device(struct v3_vm_info * vm,
     struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
     struct vm_device * dev = NULL;
 
+
+    // Check if we already registered a device of the same name
+    if (v3_htable_search(mgr->dev_table, (addr_t)name) != (addr_t)NULL) {
+        PrintError(vm, VCORE_NONE,"Device with name (%s) already registered with VM\n", name); 
+       return NULL;
+    }
+
     dev = (struct vm_device *)V3_Malloc(sizeof(struct vm_device));
 
     if (dev == NULL) {
+       PrintError(vm, VCORE_NONE,"Cannot allocate in adding a device\n");
        return NULL;
     }
 
@@ -427,10 +576,10 @@ void v3_print_dev_mgr(struct v3_vm_info * vm) {
     struct vmm_dev_mgr * mgr = &(vm->dev_mgr);
     struct vm_device * dev;
 
-    V3_Print("%d devices registered with manager\n", mgr->num_devs);
+    V3_Print(vm, VCORE_NONE,"%d devices registered with manager\n", mgr->num_devs);
 
     list_for_each_entry(dev, &(mgr->dev_list), dev_link) {
-       V3_Print("Device: %s\n", dev->name);
+       V3_Print(vm, VCORE_NONE,"Device: %s\n", dev->name);
     }
 
     return;
@@ -466,6 +615,12 @@ int v3_dev_add_blk_frontend(struct v3_vm_info * vm,
     struct blk_frontend * frontend = NULL;
 
     frontend = (struct blk_frontend *)V3_Malloc(sizeof(struct blk_frontend));
+
+    if (!frontend) {
+       PrintError(vm, VCORE_NONE,"Cannot allocate in adding a block front end\n");
+       return -1;
+    }
+
     memset(frontend, 0, sizeof(struct blk_frontend));
     
     frontend->connect = connect;
@@ -489,12 +644,12 @@ int v3_dev_connect_blk(struct v3_vm_info * vm,
                                                       (addr_t)frontend_name);
     
     if (frontend == NULL) {
-       PrintError("Could not find frontend blk device %s\n", frontend_name);
+       PrintError(vm, VCORE_NONE,"Could not find frontend blk device %s\n", frontend_name);
        return 0;
     }
 
     if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
-       PrintError("Error connecting to block frontend %s\n", frontend_name);
+       PrintError(vm, VCORE_NONE,"Error connecting to block frontend %s\n", frontend_name);
        return -1;
     }
 
@@ -529,6 +684,12 @@ int v3_dev_add_net_frontend(struct v3_vm_info * vm,
     struct net_frontend * frontend = NULL;
 
     frontend = (struct net_frontend *)V3_Malloc(sizeof(struct net_frontend));
+
+    if (!frontend) {
+       PrintError(vm, VCORE_NONE,"Cannot allocate in adding a net front end\n");
+       return -1;
+    }
+
     memset(frontend, 0, sizeof(struct net_frontend));
     
     frontend->connect = connect;
@@ -553,12 +714,12 @@ int v3_dev_connect_net(struct v3_vm_info * vm,
                                                       (addr_t)frontend_name);
     
     if (frontend == NULL) {
-       PrintError("Could not find frontend net device %s\n", frontend_name);
+       PrintError(vm, VCORE_NONE,"Could not find frontend net device %s\n", frontend_name);
        return 0;
     }
 
     if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
-       PrintError("Error connecting to net frontend %s\n", frontend_name);
+       PrintError(vm, VCORE_NONE,"Error connecting to net frontend %s\n", frontend_name);
        return -1;
     }
 
@@ -591,6 +752,12 @@ int v3_dev_add_console_frontend(struct v3_vm_info * vm,
     struct cons_frontend * frontend = NULL;
 
     frontend = (struct cons_frontend *)V3_Malloc(sizeof(struct cons_frontend));
+
+    if (!frontend) {
+       PrintError(vm, VCORE_NONE,"Cannot allocate in adding a console front end\n");
+       return -1;
+    }
+
     memset(frontend, 0, sizeof(struct cons_frontend));
     
     frontend->connect = connect;
@@ -615,12 +782,12 @@ int v3_dev_connect_console(struct v3_vm_info * vm,
                                                        (addr_t)frontend_name);
     
     if (frontend == NULL) {
-       PrintError("Could not find frontend console device %s\n", frontend_name);
+       PrintError(vm, VCORE_NONE,"Could not find frontend console device %s\n", frontend_name);
        return 0;
     }
     
     if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data) == -1) {
-       PrintError("Error connecting to console frontend %s\n", frontend_name);
+       PrintError(vm, VCORE_NONE,"Error connecting to console frontend %s\n", frontend_name);
        return -1;
     }
 
@@ -654,6 +821,12 @@ int v3_dev_add_char_frontend(struct v3_vm_info * vm,
     struct char_frontend * frontend = NULL;
 
     frontend = (struct char_frontend *)V3_Malloc(sizeof(struct char_frontend));
+
+    if (!frontend) {
+       PrintError(vm, VCORE_NONE,"Cannot allocate in adding a char front end\n");
+       return -1;
+    }
+
     memset(frontend, 0, sizeof(struct char_frontend));
     
     frontend->connect = connect;
@@ -679,12 +852,12 @@ int v3_dev_connect_char(struct v3_vm_info * vm,
                                                        (addr_t)frontend_name);
     
     if (frontend == NULL) {
-       PrintError("Could not find frontend char device %s\n", frontend_name);
+       PrintError(vm, VCORE_NONE,"Could not find frontend char device %s\n", frontend_name);
        return 0;
     }
     
     if (frontend->connect(vm, frontend->priv_data, ops, cfg, private_data, push_fn_arg) == -1) {
-       PrintError("Error connecting to char frontend %s\n", frontend_name);
+       PrintError(vm, VCORE_NONE,"Error connecting to char frontend %s\n", frontend_name);
        return -1;
     }
 
@@ -704,7 +877,7 @@ static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr) {
     struct blk_frontend * tmp_blk = NULL;
 
 
-
+    
     list_for_each_entry_safe(chr, tmp_chr, &(mgr->char_list), char_node) {
        list_del(&(chr->char_node));
        V3_Free(chr);
@@ -733,3 +906,21 @@ static int free_frontends(struct v3_vm_info * vm, struct vmm_dev_mgr * mgr) {
 
     return 0;
 }
+
+int v3_attach_device(struct v3_vm_info * vm, struct vm_device * dev)
+{
+  PrintError(vm, VCORE_NONE, "v3_attach_device is deprecated and no longer works\n");
+  return -1;
+}
+
+int v3_detach_device(struct vm_device * dev)
+{
+  PrintError(VM_NONE, VCORE_NONE, "v3_detach_device is deprecated and no longer works\n");
+  return -1;
+}
+
+struct vm_device * v3_allocate_device(char * name, struct v3_device_ops * ops, void * private_data)
+{
+  PrintError(VM_NONE, VCORE_NONE, "v3_allocate_device is deprecated and no longer works\n");
+  return 0;
+}