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 <palacios/vm_guest_mem.h>
23 #include <devices/lnx_virtio_pci.h>
24 #include <palacios/vmm_symmod.h>
26 #include <devices/pci.h>
29 #define QUEUE_SIZE 128
33 } __attribute__((packed));
37 struct virtio_sym_state {
38 struct sym_config sym_cfg;
39 struct virtio_config virtio_cfg;
42 struct vm_device * pci_bus;
43 struct pci_device * pci_dev;
46 #define NOTIFY_QUEUE 0
47 #define LOADER_QUEUE 1
48 struct virtio_queue queue[NUM_QUEUES];
50 struct virtio_queue * cur_queue;
59 // structure of the symmod notifier ring structures
63 } __attribute__((packed));
66 static int virtio_reset(struct virtio_sym_state * virtio) {
68 memset(virtio->queue, 0, sizeof(struct virtio_queue) * 2);
70 virtio->cur_queue = &(virtio->queue[0]);
72 virtio->virtio_cfg.status = 0;
73 virtio->virtio_cfg.pci_isr = 0;
75 virtio->queue[0].queue_size = QUEUE_SIZE;
76 virtio->queue[1].queue_size = QUEUE_SIZE;
79 memset(&(virtio->sym_cfg), 0, sizeof(struct sym_config));
86 static int get_desc_count(struct virtio_queue * q, int index) {
87 struct vring_desc * tmp_desc = &(q->desc[index]);
90 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
91 tmp_desc = &(q->desc[tmp_desc->next]);
101 static int handle_xfer_kick(struct guest_info * core, struct virtio_sym_state * sym_state) {
102 struct virtio_queue * q = sym_state->cur_queue;
104 PrintDebug("SYMMOD: VIRTIO SYMMOD Kick on loader queue\n");
106 while (q->cur_avail_idx < q->avail->index) {
107 struct vring_desc * hdr_desc = NULL;
108 struct vring_desc * buf_desc = NULL;
109 struct vring_desc * status_desc = NULL;
110 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
111 uint16_t desc_cnt = get_desc_count(q, desc_idx);
112 struct symmod_hdr * hdr = NULL;
114 uint32_t xfer_len = 0;
116 uint8_t * status_ptr = NULL;
117 struct v3_sym_module * module = NULL;
121 PrintDebug("Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE);
124 PrintError("Symmod loads must include at least 3 descriptors (cnt=%d)\n", desc_cnt);
128 hdr_desc = &(q->desc[desc_idx]);
130 if (guest_pa_to_host_va(core, hdr_desc->addr_gpa, (addr_t *)&hdr) == -1) {
131 PrintError("Could not translate SYMMOD header address\n");
135 desc_idx = hdr_desc->next;
137 module = v3_get_sym_module(core->vm_info, hdr->name);
139 for (i = 0; i < desc_cnt - 2; i++) {
140 uint8_t tmp_status = 0;
141 uint8_t * buf = NULL;
143 buf_desc = &(q->desc[desc_idx]);
145 if (guest_pa_to_host_va(core, buf_desc->addr_gpa, (addr_t *)&(buf)) == -1) {
146 PrintError("Could not translate buffer address\n");
150 memcpy(buf, module->data + offset, buf_desc->length);
152 if (tmp_status != 0) {
153 PrintError("Error loading module segment\n");
158 offset += buf_desc->length;
159 xfer_len += buf_desc->length;
160 desc_idx = buf_desc->next;
163 status_desc = &(q->desc[desc_idx]);
165 if (guest_pa_to_host_va(core, status_desc->addr_gpa, (addr_t *)&status_ptr) == -1) {
166 PrintError("SYMMOD Error could not translate status address\n");
170 xfer_len += status_desc->length;
171 *status_ptr = status;
173 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
174 q->used->ring[q->used->index % QUEUE_SIZE].length = xfer_len; // set to total inbound xfer length
181 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
182 PrintDebug("Raising IRQ %d\n", sym_state->pci_dev->config_header.intr_line);
183 v3_pci_raise_irq(sym_state->pci_bus, 0, sym_state->pci_dev);
184 sym_state->virtio_cfg.pci_isr = 1;
192 static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * private_data) {
193 struct virtio_sym_state * sym_state = (struct virtio_sym_state *)private_data;
194 int port_idx = port % sym_state->io_range_size;
197 PrintDebug("SYMMOD: VIRTIO SYMMOD Write for port %d len=%d, value=%x\n",
198 port, length, *(uint32_t *)src);
199 PrintDebug("SYMMOD: port idx=%d\n", port_idx);
203 case GUEST_FEATURES_PORT:
205 PrintError("Illegal write length for guest features\n");
209 sym_state->virtio_cfg.guest_features = *(uint32_t *)src;
212 case VRING_PG_NUM_PORT:
214 addr_t pfn = *(uint32_t *)src;
215 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
217 sym_state->cur_queue->pfn = pfn;
219 sym_state->cur_queue->ring_desc_addr = page_addr ;
220 sym_state->cur_queue->ring_avail_addr = page_addr + (QUEUE_SIZE * sizeof(struct vring_desc));
221 sym_state->cur_queue->ring_used_addr = ( sym_state->cur_queue->ring_avail_addr + \
222 sizeof(struct vring_avail) + \
223 (QUEUE_SIZE * sizeof(uint16_t)));
225 // round up to next page boundary.
226 sym_state->cur_queue->ring_used_addr = (sym_state->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
228 if (guest_pa_to_host_va(core, sym_state->cur_queue->ring_desc_addr, (addr_t *)&(sym_state->cur_queue->desc)) == -1) {
229 PrintError("Could not translate ring descriptor address\n");
234 if (guest_pa_to_host_va(core, sym_state->cur_queue->ring_avail_addr, (addr_t *)&(sym_state->cur_queue->avail)) == -1) {
235 PrintError("Could not translate ring available address\n");
240 if (guest_pa_to_host_va(core, sym_state->cur_queue->ring_used_addr, (addr_t *)&(sym_state->cur_queue->used)) == -1) {
241 PrintError("Could not translate ring used address\n");
245 PrintDebug("SYMMOD: RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
246 (void *)(sym_state->cur_queue->ring_desc_addr),
247 (void *)(sym_state->cur_queue->ring_avail_addr),
248 (void *)(sym_state->cur_queue->ring_used_addr));
250 PrintDebug("SYMMOD: RingDesc=%p, Avail=%p, Used=%p\n",
251 sym_state->cur_queue->desc, sym_state->cur_queue->avail, sym_state->cur_queue->used);
254 PrintError("Illegal write length for page frame number\n");
258 case VRING_Q_SEL_PORT:
259 sym_state->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
261 if (sym_state->virtio_cfg.vring_queue_selector > NUM_QUEUES) {
262 PrintError("Virtio Symbiotic device has no qeueues. Selected %d\n",
263 sym_state->virtio_cfg.vring_queue_selector);
267 sym_state->cur_queue = &(sym_state->queue[sym_state->virtio_cfg.vring_queue_selector]);
270 case VRING_Q_NOTIFY_PORT: {
271 uint16_t queue_idx = *(uint16_t *)src;
273 PrintDebug("SYMMOD: Handling Kick\n");
275 if (queue_idx == 0) {
276 sym_state->notifier_active = 1;
278 } else if (queue_idx == 1) {
279 if (handle_xfer_kick(core, sym_state) == -1) {
280 PrintError("Could not handle Symbiotic Notification\n");
284 PrintError("Kick on invalid queue (%d)\n", queue_idx);
290 case VIRTIO_STATUS_PORT:
291 sym_state->virtio_cfg.status = *(uint8_t *)src;
293 if (sym_state->virtio_cfg.status == 0) {
294 PrintDebug("SYMMOD: Resetting device\n");
295 virtio_reset(sym_state);
300 case VIRTIO_ISR_PORT:
301 sym_state->virtio_cfg.pci_isr = *(uint8_t *)src;
312 static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * private_data) {
314 struct virtio_sym_state * sym_state = (struct virtio_sym_state *)private_data;
315 int port_idx = port % sym_state->io_range_size;
318 PrintDebug("SYMMOD: VIRTIO SYMBIOTIC Read for port %d (index =%d), length=%d\n",
319 port, port_idx, length);
322 case HOST_FEATURES_PORT:
324 PrintError("Illegal read length for host features\n");
328 *(uint32_t *)dst = sym_state->virtio_cfg.host_features;
331 case VRING_PG_NUM_PORT:
333 PrintError("Illegal read length for page frame number\n");
337 *(uint32_t *)dst = sym_state->cur_queue->pfn;
340 case VRING_SIZE_PORT:
342 PrintError("Illegal read length for vring size\n");
346 *(uint16_t *)dst = sym_state->cur_queue->queue_size;
350 case VIRTIO_STATUS_PORT:
352 PrintError("Illegal read length for status\n");
356 *(uint8_t *)dst = sym_state->virtio_cfg.status;
359 case VIRTIO_ISR_PORT:
360 *(uint8_t *)dst = sym_state->virtio_cfg.pci_isr;
361 sym_state->virtio_cfg.pci_isr = 0;
362 v3_pci_lower_irq(sym_state->pci_bus, 0, sym_state->pci_dev);
366 if ( (port_idx >= sizeof(struct virtio_config)) &&
367 (port_idx < (sizeof(struct virtio_config) + sizeof(struct sym_config))) ) {
368 int cfg_offset = port_idx - sizeof(struct virtio_config);
369 uint8_t * cfg_ptr = (uint8_t *)&(sym_state->sym_cfg);
371 memcpy(dst, cfg_ptr + cfg_offset, length);
374 PrintError("Read of Unhandled Virtio Read\n");
387 static int virtio_load_module(struct v3_vm_info * vm, char * name, int mod_size, void * priv_data) {
388 struct virtio_sym_state * virtio = (struct virtio_sym_state *)priv_data;
389 // struct virtio_queue * q = virtio->cur_queue;
390 struct virtio_queue * q = &(virtio->queue[NOTIFY_QUEUE]);
392 if (strlen(name) >= 32) {
393 PrintError("Module name is too long... (%d bytes) limit is 32\n", (uint32_t)strlen(name));
397 PrintDebug("SYMMOD: VIRTIO SYMMOD Loader: Loading Module (size=%d)\n", mod_size);
399 //queue is not set yet
400 if (q->ring_avail_addr == 0) {
401 PrintError("Queue is not set\n");
406 if (q->cur_avail_idx < q->avail->index) {
407 uint16_t notifier_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
408 struct symmod_hdr * notifier = NULL;
409 struct vring_desc * notifier_desc = NULL;
411 PrintDebug("SYMMOD: Descriptor index=%d\n", q->cur_avail_idx % q->queue_size);
413 notifier_desc = &(q->desc[notifier_idx]);
415 PrintDebug("SYMMOD: Notifier Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", notifier_desc,
416 (void *)(notifier_desc->addr_gpa), notifier_desc->length, notifier_desc->flags, notifier_desc->next);
418 if (guest_pa_to_host_va(&(vm->cores[0]), notifier_desc->addr_gpa, (addr_t *)&(notifier)) == -1) {
419 PrintError("Could not translate receive buffer address\n");
423 // clear the notifier
424 memset(notifier, 0, sizeof(struct symmod_hdr));
426 // set the module name
427 memcpy(notifier->name, name, strlen(name));
430 notifier->num_bytes = mod_size;
433 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
435 q->used->ring[q->used->index % q->queue_size].length = sizeof(struct symmod_hdr);
441 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
442 PrintDebug("SYMMOD: Raising IRQ %d\n", virtio->pci_dev->config_header.intr_line);
443 v3_pci_raise_irq(virtio->pci_bus, 0, virtio->pci_dev);
444 virtio->virtio_cfg.pci_isr = 0x1;
454 static struct v3_device_ops dev_ops = {
463 static struct v3_symmod_loader_ops loader_ops = {
464 .load_module = virtio_load_module,
468 static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
469 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
470 struct virtio_sym_state * virtio_state = NULL;
471 struct pci_device * pci_dev = NULL;
472 char * name = v3_cfg_val(cfg, "name");
474 PrintDebug("SYMMOD: Initializing VIRTIO Symbiotic Module device\n");
476 if (pci_bus == NULL) {
477 PrintError("VirtIO devices require a PCI Bus");
481 virtio_state = (struct virtio_sym_state *)V3_Malloc(sizeof(struct virtio_sym_state));
482 memset(virtio_state, 0, sizeof(struct virtio_sym_state));
484 struct vm_device * dev = v3_allocate_device(name, &dev_ops, virtio_state);
486 if (v3_attach_device(vm, dev) == -1) {
487 PrintError("Could not attach device %s\n", name);
492 // PCI initialization
494 struct v3_pci_bar bars[6];
495 int num_ports = sizeof(struct virtio_config) + sizeof(struct sym_config);
496 int tmp_ports = num_ports;
500 // This gets the number of ports, rounded up to a power of 2
501 virtio_state->io_range_size = 1; // must be a power of 2
503 while (tmp_ports > 0) {
505 virtio_state->io_range_size <<= 1;
508 // this is to account for any low order bits being set in num_ports
509 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
510 if ((num_ports & ((virtio_state->io_range_size >> 1) - 1)) == 0) {
511 virtio_state->io_range_size >>= 1;
515 for (i = 0; i < 6; i++) {
516 bars[i].type = PCI_BAR_NONE;
519 bars[0].type = PCI_BAR_IO;
520 bars[0].default_base_port = -1;
521 bars[0].num_ports = virtio_state->io_range_size;
523 bars[0].io_read = virtio_io_read;
524 bars[0].io_write = virtio_io_write;
525 bars[0].private_data = virtio_state;
527 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
528 0, PCI_AUTO_DEV_NUM, 0,
529 "LNX_VIRTIO_SYMMOD", bars,
530 NULL, NULL, NULL, virtio_state);
533 PrintError("Could not register PCI Device\n");
537 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
538 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
541 pci_dev->config_header.device_id = VIRTIO_SYMMOD_DEV_ID;
542 pci_dev->config_header.class = PCI_CLASS_MEMORY;
543 pci_dev->config_header.subclass = PCI_MEM_SUBCLASS_RAM;
545 pci_dev->config_header.subsystem_id = VIRTIO_SYMMOD_SUBDEVICE_ID;
548 pci_dev->config_header.intr_pin = 1;
550 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
553 virtio_state->pci_dev = pci_dev;
554 virtio_state->pci_bus = pci_bus;
557 virtio_reset(virtio_state);
559 v3_set_symmod_loader(vm, &loader_ops, virtio_state);
565 device_register("LNX_VIRTIO_SYMMOD", virtio_init)