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 / devices / lnx_virtio_balloon.c
index de25807..653bc5e 100644 (file)
@@ -77,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);
 
@@ -119,11 +116,10 @@ static int get_desc_count(struct virtio_queue * q, int index) {
 }
 
 
-static int handle_kick(struct guest_info * core, 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", 
+    PrintDebug(core->vm_info, core, "VIRTIO BALLOON KICK: cur_index=%d (mod=%d), avail_index=%d\n", 
               q->cur_avail_idx, q->cur_avail_idx % QUEUE_SIZE, q->avail->index);
 
     while (q->cur_avail_idx < q->avail->index) {
@@ -134,32 +130,32 @@ static int handle_kick(struct guest_info * core, struct vm_device * dev) {
        uint32_t req_len = 0;
 
 
-       PrintDebug("Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE);
+       PrintDebug(core->vm_info, core, "Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE);
 
        for (i = 0; i < desc_cnt; i++) {
            addr_t page_addr;
            tmp_desc = &(q->desc[desc_idx]);
            
-           PrintDebug("Header Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", 
+           PrintDebug(core->vm_info, core, "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 (v3_gpa_to_hva(core, tmp_desc->addr_gpa, (addr_t *)&(page_addr)) == -1) {
-               PrintError("Could not translate block header address\n");
+               PrintError(core->vm_info, core, "Could not translate block header address\n");
                return -1;
            }
 
            /*      
-              if (handle_balloon_op(dev, tmp_desc, buf_desc, status_desc) == -1) {
-              PrintError("Error handling balloon operation\n");
+              if (handle_balloon_op(virtio, tmp_desc, buf_desc, status_desc) == -1) {
+              PrintError(core->vm_info, core, "Error handling balloon operation\n");
               return -1;
               }
            */
 
-           PrintDebug("Guest Balloon Currently Ignored\n");
-           PrintDebug("\t Requested=%d, Allocated=%d\n", 
+           PrintDebug(core->vm_info, core, "Guest Balloon Currently Ignored\n");
+           PrintDebug(core->vm_info, core, "\t Requested=%d, Allocated=%d\n", 
                       virtio->balloon_cfg.requested_pages, 
                       virtio->balloon_cfg.allocated_pages);
 
@@ -175,8 +171,8 @@ static int handle_kick(struct guest_info * core, struct vm_device * dev) {
     }
 
     if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
-       PrintDebug("Raising IRQ %d\n",  virtio->pci_dev->config_header.intr_line);
-       v3_pci_raise_irq(virtio->pci_bus, 0, virtio->pci_dev);
+       PrintDebug(core->vm_info, core, "Raising IRQ %d\n",  virtio->pci_dev->config_header.intr_line);
+       v3_pci_raise_irq(virtio->pci_bus, virtio->pci_dev, 0);
        virtio->virtio_cfg.pci_isr = VIRTIO_ISR_ACTIVE;
     }
 
@@ -184,12 +180,11 @@ static int handle_kick(struct guest_info * core, struct vm_device * dev) {
 }
 
 static int virtio_io_write(struct guest_info * core, 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;
+    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)private_data;
     int port_idx = port % virtio->io_range_size;
 
 
-    PrintDebug("VIRTIO BALLOON Write for port %d (index=%d) len=%d, value=%x\n", 
+    PrintDebug(core->vm_info, core, "VIRTIO BALLOON Write for port %d (index=%d) len=%d, value=%x\n", 
               port, port_idx,  length, *(uint32_t *)src);
 
 
@@ -197,7 +192,7 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src,
     switch (port_idx) {
        case GUEST_FEATURES_PORT:
            if (length != 4) {
-               PrintError("Illegal write length for guest features\n");
+               PrintError(core->vm_info, core, "Illegal write length for guest features\n");
                return -1;
            }
            
@@ -222,32 +217,32 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src,
                virtio->cur_queue->ring_used_addr = (virtio->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
 
                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");
+                   PrintError(core->vm_info, core, "Could not translate ring descriptor address\n");
                    return -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");
+                   PrintError(core->vm_info, core, "Could not translate ring available address\n");
                    return -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");
+                   PrintError(core->vm_info, core, "Could not translate ring used address\n");
                    return -1;
                }
 
-               PrintDebug("RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
+               PrintDebug(core->vm_info, core, "RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
                           (void *)(virtio->cur_queue->ring_desc_addr),
                           (void *)(virtio->cur_queue->ring_avail_addr),
                           (void *)(virtio->cur_queue->ring_used_addr));
 
-               PrintDebug("RingDesc=%p, Avail=%p, Used=%p\n", 
+               PrintDebug(core->vm_info, core, "RingDesc=%p, Avail=%p, Used=%p\n", 
                           virtio->cur_queue->desc, virtio->cur_queue->avail, virtio->cur_queue->used);
 
            } else {
-               PrintError("Illegal write length for page frame number\n");
+               PrintError(core->vm_info, core, "Illegal write length for page frame number\n");
                return -1;
            }
            break;
@@ -255,7 +250,7 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src,
            virtio->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
 
            if (virtio->virtio_cfg.vring_queue_selector > 1) {
-               PrintError("Virtio Balloon device only uses 2 queue, selected %d\n", 
+               PrintError(core->vm_info, core, "Virtio Balloon device only uses 2 queue, selected %d\n", 
                           virtio->virtio_cfg.vring_queue_selector);
                return -1;
            }
@@ -264,9 +259,9 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src,
 
            break;
        case VRING_Q_NOTIFY_PORT:
-           PrintDebug("Handling Kick\n");
-           if (handle_kick(core, dev) == -1) {
-               PrintError("Could not handle Balloon Notification\n");
+           PrintDebug(core->vm_info, core, "Handling Kick\n");
+           if (handle_kick(core, virtio) == -1) {
+               PrintError(core->vm_info, core, "Could not handle Balloon Notification\n");
                return -1;
            }
            break;
@@ -274,8 +269,8 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src,
            virtio->virtio_cfg.status = *(uint8_t *)src;
 
            if (virtio->virtio_cfg.status == 0) {
-               PrintDebug("Resetting device\n");
-               virtio_reset(dev);
+               PrintDebug(core->vm_info, core, "Resetting device\n");
+               virtio_reset(virtio);
            }
 
            break;
@@ -293,18 +288,17 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src,
 
 
 static int virtio_io_read(struct guest_info * core, 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;
+    struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)private_data;
     int port_idx = port % virtio->io_range_size;
 
 
-    PrintDebug("VIRTIO BALLOON Read  for port %d (index =%d), length=%d\n", 
+    PrintDebug(core->vm_info, core, "VIRTIO BALLOON Read  for port %d (index =%d), length=%d\n", 
               port, port_idx, length);
 
     switch (port_idx) {
        case HOST_FEATURES_PORT:
            if (length != 4) {
-               PrintError("Illegal read length for host features\n");
+               PrintError(core->vm_info, core, "Illegal read length for host features\n");
                return -1;
            }
 
@@ -313,7 +307,7 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u
            break;
        case VRING_PG_NUM_PORT:
            if (length != 4) {
-               PrintError("Illegal read length for page frame number\n");
+               PrintError(core->vm_info, core, "Illegal read length for page frame number\n");
                return -1;
            }
 
@@ -322,7 +316,7 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u
            break;
        case VRING_SIZE_PORT:
            if (length != 2) {
-               PrintError("Illegal read length for vring size\n");
+               PrintError(core->vm_info, core, "Illegal read length for vring size\n");
                return -1;
            }
                
@@ -332,7 +326,7 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u
 
        case VIRTIO_STATUS_PORT:
            if (length != 1) {
-               PrintError("Illegal read length for status\n");
+               PrintError(core->vm_info, core, "Illegal read length for status\n");
                return -1;
            }
 
@@ -342,7 +336,7 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u
        case VIRTIO_ISR_PORT:
            *(uint8_t *)dst = virtio->virtio_cfg.pci_isr;
            virtio->virtio_cfg.pci_isr = 0;
-           v3_pci_lower_irq(virtio->pci_bus, 0, virtio->pci_dev);
+           v3_pci_lower_irq(virtio->pci_bus, virtio->pci_dev, 0);
            break;
 
        default:
@@ -354,7 +348,7 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u
                memcpy(dst, cfg_ptr + cfg_offset, length);
                
            } else {
-               PrintError("Read of Unhandled Virtio Read\n");
+               PrintError(core->vm_info, core, "Read of Unhandled Virtio Read\n");
                return -1;
            }
          
@@ -365,24 +359,27 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u
 }
 
 
+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);
+    PrintDebug(VM_NONE, VCORE_NONE, "Requesting %d pages\n", virtio->balloon_cfg.requested_pages);
 
-    v3_pci_raise_irq(virtio->pci_bus, 0, virtio->pci_dev);
+    v3_pci_raise_irq(virtio->pci_bus, virtio->pci_dev, 0);
     virtio->virtio_cfg.pci_isr = VIRTIO_ISR_ACTIVE | VIRTIO_ISR_CFG_CHANGED;
     
     return 0;
@@ -390,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;
@@ -420,26 +416,31 @@ static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     struct pci_device * pci_dev = NULL;
     char * dev_id = v3_cfg_val(cfg, "ID");
 
-    PrintDebug("Initializing VIRTIO Balloon device\n");
+    PrintDebug(vm, VCORE_NONE, "Initializing VIRTIO Balloon device\n");
 
     if (pci_bus == NULL) {
-       PrintError("VirtIO devices require a PCI Bus");
+       PrintError(vm, VCORE_NONE, "VirtIO devices require a PCI Bus");
        return -1;
     }
 
     
-    virtio_state  = (struct virtio_balloon_state *)V3_Malloc(sizeof(struct virtio_balloon_state));
-    memset(virtio_state, 0, sizeof(struct virtio_balloon_state));
+    virtio_state = (struct virtio_balloon_state *)V3_Malloc(sizeof(struct virtio_balloon_state));
 
-
-    struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, virtio_state);
-    if (v3_attach_device(vm, dev) == -1) {
-       PrintError("Could not attach device %s\n", dev_id);
+    if (!virtio_state) {
+       PrintError(vm, VCORE_NONE, "Cannot allocate in init\n");
        return -1;
     }
 
+    memset(virtio_state, 0, sizeof(struct virtio_balloon_state));
 
 
+    struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, virtio_state);
+
+    if (dev == NULL) {
+       PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id);
+       V3_Free(virtio_state);
+       return -1;
+    }
 
     // PCI initialization
     {
@@ -475,16 +476,17 @@ static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
 
        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");
+           PrintError(vm, VCORE_NONE, "Could not register PCI Device\n");
+           v3_remove_device(dev);
            return -1;
        }
        
@@ -508,10 +510,10 @@ static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
        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;
 }