#define PAGE_SIZE 4096
-#define BALLOON_HCALL 0xba00
struct balloon_config {
uint32_t requested_pages;
/* How this works:
* A ballooning request is made by specifying the new memory size of the guest. The guest
* will then shrink the amount of of memory it uses to target. The target size is stored in the
- * Virtio PCI configuration space in the requested pages field. The device raises its irq, to notify the guest
+ * Virtio PCI configuration space in the requested pages field.
+ * The device raises its irq, to notify the guest
*
* The guest might not be able to shrink to target, so it stores the size it was able to shrink to
* into the allocate_pages field of the pci configuration space.
*
- * When the guest frees pages it writes the addresses to the deflation queue (the 2nd one), and does a kick.
- * When pages are given back to the host they are fed in via the inflation queue (the 1st one), and raises an irq.
+ * When the guest frees pages it writes the addresses to the deflation queue (the 2nd one),
+ * and does a kick.
+ * When pages are given back to the host they are fed in via the inflation queue (the 1st one),
+ * and raises an irq.
*/
#define VIRTIO_NOTIFY_HOST 0x01
-
-
-
struct virtio_balloon_state {
struct balloon_config balloon_cfg;
struct virtio_config virtio_cfg;
};
-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);
return 0;
}
+static int get_desc_count(struct virtio_queue * q, int index) {
+ struct vring_desc * tmp_desc = &(q->desc[index]);
+ int cnt = 1;
+
+ while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
+ tmp_desc = &(q->desc[tmp_desc->next]);
+ cnt++;
+ }
+ return cnt;
+}
-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 KICK: cur_index=%d, avail_index=%d\n", q->cur_avail_idx, q->avail->index);
+ PrintDebug("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) {
- struct vring_desc * hdr_desc = NULL;
- struct vring_desc * buf_desc = NULL;
- struct vring_desc * status_desc = NULL;
- uint16_t chain_idx = q->avail->ring[q->cur_avail_idx];
+ struct vring_desc * tmp_desc = NULL;
+ uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
+ int desc_cnt = get_desc_count(q, desc_idx);
+ int i = 0;
uint32_t req_len = 0;
- int chained = 1;
- PrintDebug("chained=%d, Chain Index=%d\n", chained, chain_idx);
- while (chained) {
- hdr_desc = &(q->desc[chain_idx]);
+ PrintDebug("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", hdr_desc,
- (void *)(hdr_desc->addr_gpa), hdr_desc->length, hdr_desc->flags, hdr_desc->next);
+ 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 (!(hdr_desc->flags & VIRTIO_NEXT_FLAG)) {
- PrintError("Balloon operations must chain a buffer descriptor\n");
- return -1;
- }
- buf_desc = &(q->desc[hdr_desc->next]);
-
- PrintDebug("Buffer Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", buf_desc,
- (void *)(buf_desc->addr_gpa), buf_desc->length, buf_desc->flags, buf_desc->next);
-
- if (!(buf_desc->flags & VIRTIO_NEXT_FLAG)) {
- PrintError("Balloon operatoins must chain a status descriptor\n");
+ 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;
}
-
- status_desc = &(q->desc[buf_desc->next]);
-
- // We detect whether we are chained here...
- if (status_desc->flags & VIRTIO_NEXT_FLAG) {
- chained = 1;
- chain_idx = status_desc->next;
- } else {
- chained = 0;
- }
-
- PrintDebug("Status Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", status_desc,
- (void *)(status_desc->addr_gpa), status_desc->length, status_desc->flags, status_desc->next);
-
/*
- if (handle_balloon_op(dev, hdr_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;
}
*/
PrintDebug("Guest Balloon Currently Ignored\n");
- PrintDebug("\t Requested=%d, Allocated=%d\n", virtio->balloon_cfg.requested_pages, virtio->balloon_cfg.allocated_pages);
-
+ PrintDebug("\t Requested=%d, Allocated=%d\n",
+ virtio->balloon_cfg.requested_pages,
+ virtio->balloon_cfg.allocated_pages);
- req_len += (buf_desc->length + status_desc->length);
+ req_len += tmp_desc->length;
+ desc_idx = tmp_desc->next;
}
- q->used->ring[q->used->index].id = q->avail->ring[q->cur_avail_idx];
- q->used->ring[q->used->index].length = req_len; // What do we set this to????
-
- q->used->index = (q->used->index + 1) % (QUEUE_SIZE * sizeof(struct vring_desc));;
+ q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
+ q->used->ring[q->used->index % QUEUE_SIZE].length = req_len; // What do we set this to????
-
- q->cur_avail_idx = (q->cur_avail_idx + 1) % (QUEUE_SIZE * sizeof(struct vring_desc));
+ q->used->index++;
+ q->cur_avail_idx++;
}
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);
- virtio->virtio_cfg.pci_isr = 1;
+ 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;
// 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;
}
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;
}
if (virtio->virtio_cfg.status == 0) {
PrintDebug("Resetting device\n");
- virtio_reset(dev);
+ virtio_reset(virtio);
}
break;
}
-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;
default:
if ( (port_idx >= sizeof(struct virtio_config)) &&
(port_idx < (sizeof(struct virtio_config) + sizeof(struct balloon_config))) ) {
-
+ int cfg_offset = port_idx - sizeof(struct virtio_config);
uint8_t * cfg_ptr = (uint8_t *)&(virtio->balloon_cfg);
- memcpy(dst, cfg_ptr, length);
+
+ memcpy(dst, cfg_ptr + cfg_offset, length);
} else {
PrintError("Read of Unhandled Virtio Read\n");
}
+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);
+
v3_pci_raise_irq(virtio->pci_bus, 0, virtio->pci_dev);
+ virtio->virtio_cfg.pci_isr = VIRTIO_ISR_ACTIVE | VIRTIO_ISR_CFG_CHANGED;
return 0;
}
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 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;
+
+
+ return 0;
}
-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");
}
- 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];
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, virtio_state);
if (!pci_dev) {
PrintError("Could not register PCI Device\n");
+ v3_remove_device(dev);
return -1;
}
virtio_state->pci_bus = pci_bus;
}
- virtio_reset(dev);
+ virtio_reset(virtio_state);
- v3_register_hypercall(vm, BALLOON_HCALL, handle_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;
}