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.


Updated NE2000 and RTL8139 to conform to interface changes
[palacios.git] / palacios / src / devices / lnx_virtio_balloon.c
index e7a18aa..e936f9e 100644 (file)
@@ -31,8 +31,6 @@
 
 #define PAGE_SIZE 4096
 
-#define BALLOON_START_HCALL 0xba00 // size in rax
-#define BALLOON_QUERY_HCALL 0xba01 // req_pgs in rcx, alloc_pgs in rdx
 
 struct balloon_config {
     uint32_t requested_pages;
@@ -79,12 +77,9 @@ struct virtio_balloon_state {
 };
 
 
-static int virtio_free(struct vm_device * dev) {
-    return -1;
-}
 
-static int virtio_reset(struct vm_device * dev) {
-    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
+
+static int virtio_reset(struct virtio_balloon_state * virtio) {
 
     memset(virtio->queue, 0, sizeof(struct virtio_queue) * 2);
 
@@ -121,8 +116,7 @@ static int get_desc_count(struct virtio_queue * q, int index) {
 }
 
 
-static int handle_kick(struct vm_device * dev) {
-    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;    
+static int handle_kick(struct guest_info * core, struct virtio_balloon_state * virtio) {
     struct virtio_queue * q = virtio->cur_queue;
 
     PrintDebug("VIRTIO BALLOON KICK: cur_index=%d (mod=%d), avail_index=%d\n", 
@@ -142,18 +136,19 @@ static int handle_kick(struct vm_device * dev) {
            addr_t page_addr;
            tmp_desc = &(q->desc[desc_idx]);
            
-           PrintDebug("Header Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", tmp_desc, 
-                      (void *)(tmp_desc->addr_gpa), tmp_desc->length, 
+           PrintDebug("Header Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", 
+                      tmp_desc, 
+                      (void *)(addr_t)(tmp_desc->addr_gpa), tmp_desc->length, 
                       tmp_desc->flags, tmp_desc->next);
        
 
-           if (guest_pa_to_host_va(dev->vm, tmp_desc->addr_gpa, (addr_t *)&(page_addr)) == -1) {
+           if (v3_gpa_to_hva(core, tmp_desc->addr_gpa, (addr_t *)&(page_addr)) == -1) {
                PrintError("Could not translate block header address\n");
                return -1;
            }
 
            /*      
-              if (handle_balloon_op(dev, tmp_desc, buf_desc, status_desc) == -1) {
+              if (handle_balloon_op(virtio, tmp_desc, buf_desc, status_desc) == -1) {
               PrintError("Error handling balloon operation\n");
               return -1;
               }
@@ -184,9 +179,8 @@ static int handle_kick(struct vm_device * dev) {
     return 0;
 }
 
-static int virtio_io_write(uint16_t port, void * src, uint_t length, void * private_data) {
-    struct vm_device * dev = (struct vm_device *)private_data;
-    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
+static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * private_data) {
+    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)private_data;
     int port_idx = port % virtio->io_range_size;
 
 
@@ -222,19 +216,19 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, void * priv
                // round up to next page boundary.
                virtio->cur_queue->ring_used_addr = (virtio->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
 
-               if (guest_pa_to_host_va(dev->vm, virtio->cur_queue->ring_desc_addr, (addr_t *)&(virtio->cur_queue->desc)) == -1) {
+               if (v3_gpa_to_hva(core, virtio->cur_queue->ring_desc_addr, (addr_t *)&(virtio->cur_queue->desc)) == -1) {
                    PrintError("Could not translate ring descriptor address\n");
                    return -1;
                }
 
 
-               if (guest_pa_to_host_va(dev->vm, virtio->cur_queue->ring_avail_addr, (addr_t *)&(virtio->cur_queue->avail)) == -1) {
+               if (v3_gpa_to_hva(core, virtio->cur_queue->ring_avail_addr, (addr_t *)&(virtio->cur_queue->avail)) == -1) {
                    PrintError("Could not translate ring available address\n");
                    return -1;
                }
 
 
-               if (guest_pa_to_host_va(dev->vm, virtio->cur_queue->ring_used_addr, (addr_t *)&(virtio->cur_queue->used)) == -1) {
+               if (v3_gpa_to_hva(core, virtio->cur_queue->ring_used_addr, (addr_t *)&(virtio->cur_queue->used)) == -1) {
                    PrintError("Could not translate ring used address\n");
                    return -1;
                }
@@ -266,7 +260,7 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, void * priv
            break;
        case VRING_Q_NOTIFY_PORT:
            PrintDebug("Handling Kick\n");
-           if (handle_kick(dev) == -1) {
+           if (handle_kick(core, virtio) == -1) {
                PrintError("Could not handle Balloon Notification\n");
                return -1;
            }
@@ -276,7 +270,7 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, void * priv
 
            if (virtio->virtio_cfg.status == 0) {
                PrintDebug("Resetting device\n");
-               virtio_reset(dev);
+               virtio_reset(virtio);
            }
 
            break;
@@ -293,9 +287,8 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, void * priv
 }
 
 
-static int virtio_io_read(uint16_t port, void * dst, uint_t length, void * private_data) {
-    struct vm_device * dev = (struct vm_device *)private_data;
-    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
+static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * private_data) {
+    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)private_data;
     int port_idx = port % virtio->io_range_size;
 
 
@@ -366,19 +359,22 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, void * priva
 }
 
 
+static int virtio_free(struct virtio_balloon_state * virtio) {
+
+    // unregister from PCI
+
+    V3_Free(virtio);
+    return 0;
+}
 
 
 static struct v3_device_ops dev_ops = {
-    .free = virtio_free,
-    .reset = NULL,
-    .start = NULL,
-    .stop = NULL,
-};
+    .free = (int (*)(void *))virtio_free,
 
+};
 
-static int set_size(struct vm_device * dev, addr_t size) {
-    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
 
+static int set_size(struct virtio_balloon_state * virtio, addr_t size) {
     virtio->balloon_cfg.requested_pages = size / PAGE_SIZE; // number of pages
 
     PrintDebug("Requesting %d pages\n", virtio->balloon_cfg.requested_pages);
@@ -391,18 +387,17 @@ static int set_size(struct vm_device * dev, addr_t size) {
 
 
 static int handle_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
-    struct vm_device * dev = (struct vm_device *)priv_data;
+    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)priv_data;
     int tgt_size = info->vm_regs.rcx;
 
     
-    return set_size(dev, tgt_size);
+    return set_size(virtio, tgt_size);
 }
 
 
 
 static int handle_query_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
-    struct vm_device * dev = (struct vm_device *)priv_data;
-    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
+    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)priv_data;
     
     info->vm_regs.rcx = virtio->balloon_cfg.requested_pages;
     info->vm_regs.rdx = virtio->balloon_cfg.allocated_pages;
@@ -415,10 +410,11 @@ static int handle_query_hcall(struct guest_info * info, uint_t hcall_id, void *
 
 
 
-static int virtio_init(struct guest_info * vm, void * cfg_data) {
-    struct vm_device * pci_bus = v3_find_dev(vm, (char *)cfg_data);
+static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
+    struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
     struct virtio_balloon_state * virtio_state = NULL;
     struct pci_device * pci_dev = NULL;
+    char * dev_id = v3_cfg_val(cfg, "ID");
 
     PrintDebug("Initializing VIRTIO Balloon device\n");
 
@@ -428,19 +424,18 @@ static int virtio_init(struct guest_info * vm, void * cfg_data) {
     }
 
     
-    virtio_state  = (struct virtio_balloon_state *)V3_Malloc(sizeof(struct virtio_balloon_state));
+    virtio_state = (struct virtio_balloon_state *)V3_Malloc(sizeof(struct virtio_balloon_state));
     memset(virtio_state, 0, sizeof(struct virtio_balloon_state));
 
 
-    struct vm_device * dev = v3_allocate_device("LNX_VIRTIO_BALLOON", &dev_ops, virtio_state);
-    if (v3_attach_device(vm, dev) == -1) {
-       PrintError("Could not attach device %s\n", "LNX_VIRTIO_BALLOON");
+    struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, virtio_state);
+
+    if (dev == NULL) {
+       PrintError("Could not attach device %s\n", dev_id);
+       V3_Free(virtio_state);
        return -1;
     }
 
-
-
-
     // PCI initialization
     {
        struct v3_pci_bar bars[6];
@@ -475,16 +470,17 @@ static int virtio_init(struct guest_info * vm, void * cfg_data) {
 
        bars[0].io_read = virtio_io_read;
        bars[0].io_write = virtio_io_write;
-       bars[0].private_data = dev;
+       bars[0].private_data = virtio_state;
        
 
        pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE, 
                                         0, PCI_AUTO_DEV_NUM, 0,
                                         "LNX_VIRTIO_BALLOON", bars,
-                                        NULL, NULL, NULL, dev, NULL);
+                                        NULL, NULL, NULL, virtio_state);
 
        if (!pci_dev) {
            PrintError("Could not register PCI Device\n");
+           v3_remove_device(dev);
            return -1;
        }
        
@@ -508,10 +504,10 @@ static int virtio_init(struct guest_info * vm, void * cfg_data) {
        virtio_state->pci_bus = pci_bus;
     }
 
-    virtio_reset(dev);
+    virtio_reset(virtio_state);
 
-    v3_register_hypercall(vm, BALLOON_START_HCALL, handle_hcall, dev);
-    v3_register_hypercall(vm, BALLOON_QUERY_HCALL, handle_query_hcall, dev);
+    v3_register_hypercall(vm, BALLOON_START_HCALL, handle_hcall, virtio_state);
+    v3_register_hypercall(vm, BALLOON_QUERY_HCALL, handle_query_hcall, virtio_state);
 
     return 0;
 }