2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <palacios/vmm_dev_mgr.h>
22 #include <devices/lnx_virtio_pci.h>
23 #include <palacios/vm_guest_mem.h>
25 #include <devices/pci.h>
28 #define BLN_REQUESTED_PORT 20
29 #define BLN_ALLOCATED_PORT 28
32 #define PAGE_SIZE 4096
34 #define BALLOON_START_HCALL 0xba00 // size in rax
35 #define BALLOON_QUERY_HCALL 0xba01 // req_pgs in rcx, alloc_pgs in rdx
37 struct balloon_config {
38 uint32_t requested_pages;
39 uint32_t allocated_pages;
40 } __attribute__((packed));
45 * A ballooning request is made by specifying the new memory size of the guest. The guest
46 * will then shrink the amount of of memory it uses to target. The target size is stored in the
47 * Virtio PCI configuration space in the requested pages field.
48 * The device raises its irq, to notify the guest
50 * The guest might not be able to shrink to target, so it stores the size it was able to shrink to
51 * into the allocate_pages field of the pci configuration space.
53 * When the guest frees pages it writes the addresses to the deflation queue (the 2nd one),
55 * When pages are given back to the host they are fed in via the inflation queue (the 1st one),
60 #define QUEUE_SIZE 128
62 /* Host Feature flags */
63 #define VIRTIO_NOTIFY_HOST 0x01
66 struct virtio_balloon_state {
67 struct balloon_config balloon_cfg;
68 struct virtio_config virtio_cfg;
70 struct vm_device * pci_bus;
71 struct pci_device * pci_dev;
73 struct virtio_queue queue[2];
76 struct virtio_queue * cur_queue;
82 static int virtio_free(struct vm_device * dev) {
86 static int virtio_reset(struct vm_device * dev) {
87 struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
89 memset(virtio->queue, 0, sizeof(struct virtio_queue) * 2);
91 virtio->cur_queue = &(virtio->queue[0]);
94 virtio->virtio_cfg.status = 0;
95 virtio->virtio_cfg.pci_isr = 0;
98 /* Balloon configuration */
99 virtio->virtio_cfg.host_features = VIRTIO_NOTIFY_HOST;
101 // Virtio Balloon uses two queues
102 virtio->queue[0].queue_size = QUEUE_SIZE;
103 virtio->queue[1].queue_size = QUEUE_SIZE;
106 memset(&(virtio->balloon_cfg), 0, sizeof(struct balloon_config));
111 static int get_desc_count(struct virtio_queue * q, int index) {
112 struct vring_desc * tmp_desc = &(q->desc[index]);
115 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
116 tmp_desc = &(q->desc[tmp_desc->next]);
124 static int handle_kick(struct vm_device * dev) {
125 struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
126 struct virtio_queue * q = virtio->cur_queue;
128 PrintDebug("VIRTIO BALLOON KICK: cur_index=%d (mod=%d), avail_index=%d\n",
129 q->cur_avail_idx, q->cur_avail_idx % QUEUE_SIZE, q->avail->index);
131 while (q->cur_avail_idx < q->avail->index) {
132 struct vring_desc * tmp_desc = NULL;
133 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
134 int desc_cnt = get_desc_count(q, desc_idx);
136 uint32_t req_len = 0;
139 PrintDebug("Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE);
141 for (i = 0; i < desc_cnt; i++) {
143 tmp_desc = &(q->desc[desc_idx]);
145 PrintDebug("Header Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n",
147 (void *)(addr_t)(tmp_desc->addr_gpa), tmp_desc->length,
148 tmp_desc->flags, tmp_desc->next);
151 if (guest_pa_to_host_va(dev->vm, tmp_desc->addr_gpa, (addr_t *)&(page_addr)) == -1) {
152 PrintError("Could not translate block header address\n");
157 if (handle_balloon_op(dev, tmp_desc, buf_desc, status_desc) == -1) {
158 PrintError("Error handling balloon operation\n");
163 PrintDebug("Guest Balloon Currently Ignored\n");
164 PrintDebug("\t Requested=%d, Allocated=%d\n",
165 virtio->balloon_cfg.requested_pages,
166 virtio->balloon_cfg.allocated_pages);
168 req_len += tmp_desc->length;
169 desc_idx = tmp_desc->next;
172 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
173 q->used->ring[q->used->index % QUEUE_SIZE].length = req_len; // What do we set this to????
179 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
180 PrintDebug("Raising IRQ %d\n", virtio->pci_dev->config_header.intr_line);
181 v3_pci_raise_irq(virtio->pci_bus, 0, virtio->pci_dev);
182 virtio->virtio_cfg.pci_isr = VIRTIO_ISR_ACTIVE;
188 static int virtio_io_write(uint16_t port, void * src, uint_t length, void * private_data) {
189 struct vm_device * dev = (struct vm_device *)private_data;
190 struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
191 int port_idx = port % virtio->io_range_size;
194 PrintDebug("VIRTIO BALLOON Write for port %d (index=%d) len=%d, value=%x\n",
195 port, port_idx, length, *(uint32_t *)src);
200 case GUEST_FEATURES_PORT:
202 PrintError("Illegal write length for guest features\n");
206 virtio->virtio_cfg.guest_features = *(uint32_t *)src;
209 case VRING_PG_NUM_PORT:
211 addr_t pfn = *(uint32_t *)src;
212 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
215 virtio->cur_queue->pfn = pfn;
217 virtio->cur_queue->ring_desc_addr = page_addr ;
218 virtio->cur_queue->ring_avail_addr = page_addr + (QUEUE_SIZE * sizeof(struct vring_desc));
219 virtio->cur_queue->ring_used_addr = ( virtio->cur_queue->ring_avail_addr + \
220 sizeof(struct vring_avail) + \
221 (QUEUE_SIZE * sizeof(uint16_t)));
223 // round up to next page boundary.
224 virtio->cur_queue->ring_used_addr = (virtio->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
226 if (guest_pa_to_host_va(dev->vm, virtio->cur_queue->ring_desc_addr, (addr_t *)&(virtio->cur_queue->desc)) == -1) {
227 PrintError("Could not translate ring descriptor address\n");
232 if (guest_pa_to_host_va(dev->vm, virtio->cur_queue->ring_avail_addr, (addr_t *)&(virtio->cur_queue->avail)) == -1) {
233 PrintError("Could not translate ring available address\n");
238 if (guest_pa_to_host_va(dev->vm, virtio->cur_queue->ring_used_addr, (addr_t *)&(virtio->cur_queue->used)) == -1) {
239 PrintError("Could not translate ring used address\n");
243 PrintDebug("RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
244 (void *)(virtio->cur_queue->ring_desc_addr),
245 (void *)(virtio->cur_queue->ring_avail_addr),
246 (void *)(virtio->cur_queue->ring_used_addr));
248 PrintDebug("RingDesc=%p, Avail=%p, Used=%p\n",
249 virtio->cur_queue->desc, virtio->cur_queue->avail, virtio->cur_queue->used);
252 PrintError("Illegal write length for page frame number\n");
256 case VRING_Q_SEL_PORT:
257 virtio->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
259 if (virtio->virtio_cfg.vring_queue_selector > 1) {
260 PrintError("Virtio Balloon device only uses 2 queue, selected %d\n",
261 virtio->virtio_cfg.vring_queue_selector);
265 virtio->cur_queue = &(virtio->queue[virtio->virtio_cfg.vring_queue_selector]);
268 case VRING_Q_NOTIFY_PORT:
269 PrintDebug("Handling Kick\n");
270 if (handle_kick(dev) == -1) {
271 PrintError("Could not handle Balloon Notification\n");
275 case VIRTIO_STATUS_PORT:
276 virtio->virtio_cfg.status = *(uint8_t *)src;
278 if (virtio->virtio_cfg.status == 0) {
279 PrintDebug("Resetting device\n");
285 case VIRTIO_ISR_PORT:
286 virtio->virtio_cfg.pci_isr = *(uint8_t *)src;
297 static int virtio_io_read(uint16_t port, void * dst, uint_t length, void * private_data) {
298 struct vm_device * dev = (struct vm_device *)private_data;
299 struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
300 int port_idx = port % virtio->io_range_size;
303 PrintDebug("VIRTIO BALLOON Read for port %d (index =%d), length=%d\n",
304 port, port_idx, length);
307 case HOST_FEATURES_PORT:
309 PrintError("Illegal read length for host features\n");
313 *(uint32_t *)dst = virtio->virtio_cfg.host_features;
316 case VRING_PG_NUM_PORT:
318 PrintError("Illegal read length for page frame number\n");
322 *(uint32_t *)dst = virtio->cur_queue->pfn;
325 case VRING_SIZE_PORT:
327 PrintError("Illegal read length for vring size\n");
331 *(uint16_t *)dst = virtio->cur_queue->queue_size;
335 case VIRTIO_STATUS_PORT:
337 PrintError("Illegal read length for status\n");
341 *(uint8_t *)dst = virtio->virtio_cfg.status;
344 case VIRTIO_ISR_PORT:
345 *(uint8_t *)dst = virtio->virtio_cfg.pci_isr;
346 virtio->virtio_cfg.pci_isr = 0;
347 v3_pci_lower_irq(virtio->pci_bus, 0, virtio->pci_dev);
351 if ( (port_idx >= sizeof(struct virtio_config)) &&
352 (port_idx < (sizeof(struct virtio_config) + sizeof(struct balloon_config))) ) {
353 int cfg_offset = port_idx - sizeof(struct virtio_config);
354 uint8_t * cfg_ptr = (uint8_t *)&(virtio->balloon_cfg);
356 memcpy(dst, cfg_ptr + cfg_offset, length);
359 PrintError("Read of Unhandled Virtio Read\n");
372 static struct v3_device_ops dev_ops = {
380 static int set_size(struct vm_device * dev, addr_t size) {
381 struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
383 virtio->balloon_cfg.requested_pages = size / PAGE_SIZE; // number of pages
385 PrintDebug("Requesting %d pages\n", virtio->balloon_cfg.requested_pages);
387 v3_pci_raise_irq(virtio->pci_bus, 0, virtio->pci_dev);
388 virtio->virtio_cfg.pci_isr = VIRTIO_ISR_ACTIVE | VIRTIO_ISR_CFG_CHANGED;
394 static int handle_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
395 struct vm_device * dev = (struct vm_device *)priv_data;
396 int tgt_size = info->vm_regs.rcx;
399 return set_size(dev, tgt_size);
404 static int handle_query_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
405 struct vm_device * dev = (struct vm_device *)priv_data;
406 struct virtio_balloon_state * virtio = (struct virtio_balloon_state *)dev->private_data;
408 info->vm_regs.rcx = virtio->balloon_cfg.requested_pages;
409 info->vm_regs.rdx = virtio->balloon_cfg.allocated_pages;
419 static int virtio_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
420 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
421 struct virtio_balloon_state * virtio_state = NULL;
422 struct pci_device * pci_dev = NULL;
423 char * name = v3_cfg_val(cfg, "name");
425 PrintDebug("Initializing VIRTIO Balloon device\n");
427 if (pci_bus == NULL) {
428 PrintError("VirtIO devices require a PCI Bus");
433 virtio_state = (struct virtio_balloon_state *)V3_Malloc(sizeof(struct virtio_balloon_state));
434 memset(virtio_state, 0, sizeof(struct virtio_balloon_state));
437 struct vm_device * dev = v3_allocate_device(name, &dev_ops, virtio_state);
438 if (v3_attach_device(vm, dev) == -1) {
439 PrintError("Could not attach device %s\n", name);
446 // PCI initialization
448 struct v3_pci_bar bars[6];
449 int num_ports = sizeof(struct virtio_config) + sizeof(struct balloon_config);
450 int tmp_ports = num_ports;
455 // This gets the number of ports, rounded up to a power of 2
456 virtio_state->io_range_size = 1; // must be a power of 2
458 while (tmp_ports > 0) {
460 virtio_state->io_range_size <<= 1;
463 // this is to account for any low order bits being set in num_ports
464 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
465 if ((num_ports & ((virtio_state->io_range_size >> 1) - 1)) == 0) {
466 virtio_state->io_range_size >>= 1;
470 for (i = 0; i < 6; i++) {
471 bars[i].type = PCI_BAR_NONE;
474 bars[0].type = PCI_BAR_IO;
475 bars[0].default_base_port = -1;
476 bars[0].num_ports = virtio_state->io_range_size;
478 bars[0].io_read = virtio_io_read;
479 bars[0].io_write = virtio_io_write;
480 bars[0].private_data = dev;
483 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
484 0, PCI_AUTO_DEV_NUM, 0,
485 "LNX_VIRTIO_BALLOON", bars,
486 NULL, NULL, NULL, dev);
489 PrintError("Could not register PCI Device\n");
493 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
494 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
497 pci_dev->config_header.device_id = VIRTIO_BALLOON_DEV_ID;
498 pci_dev->config_header.class = PCI_CLASS_MEMORY;
499 pci_dev->config_header.subclass = PCI_MEM_SUBCLASS_RAM;
501 pci_dev->config_header.subsystem_id = VIRTIO_BALLOON_SUBDEVICE_ID;
504 pci_dev->config_header.intr_pin = 1;
506 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
509 virtio_state->pci_dev = pci_dev;
510 virtio_state->pci_bus = pci_bus;
515 v3_register_hypercall(vm, BALLOON_START_HCALL, handle_hcall, dev);
516 v3_register_hypercall(vm, BALLOON_QUERY_HCALL, handle_query_hcall, dev);
522 device_register("LNX_VIRTIO_BALLOON", virtio_init)