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.


Avoid physical/virtual contiguity assumptions using new guest memory access functions
[palacios.git] / palacios / src / devices / lnx_virtio_balloon.c
index c29d900..653bc5e 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,11 +116,10 @@ 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", 
+    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) {
@@ -136,31 +130,32 @@ static int handle_kick(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", tmp_desc, 
-                      (void *)(tmp_desc->addr_gpa), tmp_desc->length, 
+           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 (guest_pa_to_host_va(dev->vm, tmp_desc->addr_gpa, (addr_t *)&(page_addr)) == -1) {
-               PrintError("Could not translate block header address\n");
+           if (v3_gpa_to_hva(core, tmp_desc->addr_gpa, (addr_t *)&(page_addr)) == -1) {
+               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);
 
@@ -176,20 +171,20 @@ static int handle_kick(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;
     }
 
     return 0;
 }
 
-static int virtio_io_write(uint16_t port, void * src, uint_t length, struct vm_device * dev) {
-    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;
 
 
-    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(uint16_t port, void * src, uint_t length, struct vm_d
     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;
            }
            
@@ -221,33 +216,33 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, struct vm_d
                // 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) {
-                   PrintError("Could not translate ring descriptor address\n");
+               if (v3_gpa_to_hva(core, virtio->cur_queue->ring_desc_addr, (addr_t *)&(virtio->cur_queue->desc)) == -1) {
+                   PrintError(core->vm_info, core, "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) {
-                   PrintError("Could not translate ring available address\n");
+               if (v3_gpa_to_hva(core, virtio->cur_queue->ring_avail_addr, (addr_t *)&(virtio->cur_queue->avail)) == -1) {
+                   PrintError(core->vm_info, core, "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) {
-                   PrintError("Could not translate ring used address\n");
+               if (v3_gpa_to_hva(core, virtio->cur_queue->ring_used_addr, (addr_t *)&(virtio->cur_queue->used)) == -1) {
+                   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(uint16_t port, void * src, uint_t length, struct vm_d
            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(uint16_t port, void * src, uint_t length, struct vm_d
 
            break;
        case VRING_Q_NOTIFY_PORT:
-           PrintDebug("Handling Kick\n");
-           if (handle_kick(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(uint16_t port, void * src, uint_t length, struct vm_d
            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;
@@ -292,18 +287,18 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, struct vm_d
 }
 
 
-static int virtio_io_read(uint16_t port, void * dst, uint_t length, struct vm_device * dev) {
-    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;
 
 
-    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;
            }
 
@@ -312,7 +307,7 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, struct vm_de
            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;
            }
 
@@ -321,7 +316,7 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, struct vm_de
            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;
            }
                
@@ -331,7 +326,7 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, struct vm_de
 
        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;
            }
 
@@ -341,7 +336,7 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, struct vm_de
        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:
@@ -353,7 +348,7 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, struct vm_de
                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;
            }
          
@@ -364,24 +359,27 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, struct vm_de
 }
 
 
+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;
@@ -389,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;
@@ -413,31 +410,37 @@ 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");
+    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("LNX_VIRTIO_BALLOON", &dev_ops, virtio_state);
-    if (v3_attach_device(vm, dev) == -1) {
-       PrintError("Could not attach device %s\n", "LNX_VIRTIO_BALLOON");
+    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
     {
@@ -473,14 +476,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 = 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;
        }
        
@@ -504,10 +510,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;
 }