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>
25 #include <palacios/vmm_hashtable.h>
27 #include <devices/pci.h>
30 #define QUEUE_SIZE 128
36 } __attribute__((packed));
40 struct virtio_sym_state {
41 struct sym_config sym_cfg;
42 struct virtio_config virtio_cfg;
45 struct vm_device * pci_bus;
46 struct pci_device * pci_dev;
47 struct v3_vm_info * vm;
48 struct v3_symmod_state * symmod_state;
51 #define NOTIFY_QUEUE 0
52 #define LOADER_QUEUE 1
53 struct virtio_queue queue[NUM_QUEUES];
55 struct virtio_queue * cur_queue;
70 } __attribute__((packed));
73 // structure of the symmod notifier ring structures
80 #define V3_SYMMOD_INV (0x00)
81 #define V3_SYMMOD_LNX (0x01)
82 #define V3_SYMMOD_MOD (0x02)
83 #define V3_SYMMOD_SEC (0x03)
86 #define V3_SYMMOD_ARCH_INV (0x00)
87 #define V3_SYMMOD_ARCH_i386 (0x01)
88 #define V3_SYMMOD_ARCH_x86_64 (0x02)
91 #define V3_SYMMOD_ACT_INV (0x00)
92 #define V3_SYMMOD_ACT_ADVERTISE (0x01)
93 #define V3_SYMMOD_ACT_LOAD (0x02)
97 } __attribute__((packed));
98 } __attribute__((packed));
99 } __attribute__((packed));
102 static int virtio_reset(struct virtio_sym_state * virtio) {
104 memset(virtio->queue, 0, sizeof(struct virtio_queue) * 2);
106 virtio->cur_queue = &(virtio->queue[0]);
108 virtio->virtio_cfg.status = 0;
109 virtio->virtio_cfg.pci_isr = 0;
111 virtio->queue[0].queue_size = QUEUE_SIZE;
112 virtio->queue[1].queue_size = QUEUE_SIZE;
115 virtio->sym_cfg.avail_mods = virtio->symmod_state->num_avail_capsules;
116 virtio->sym_cfg.loaded_mods = virtio->symmod_state->num_loaded_capsules;
123 static int get_desc_count(struct virtio_queue * q, int index) {
124 struct vring_desc * tmp_desc = &(q->desc[index]);
127 while (tmp_desc->flags & VIRTIO_NEXT_FLAG) {
128 tmp_desc = &(q->desc[tmp_desc->next]);
138 static int handle_xfer_kick(struct guest_info * core, struct virtio_sym_state * sym_state) {
139 struct virtio_queue * q = sym_state->cur_queue;
141 PrintDebug("SYMMOD: VIRTIO SYMMOD Kick on loader queue\n");
143 while (q->cur_avail_idx != q->avail->index) {
144 struct vring_desc * cmd_desc = NULL;
145 struct symmod_cmd * cmd = NULL;
146 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
147 uint16_t desc_cnt = get_desc_count(q, desc_idx);
148 struct vring_desc * status_desc = NULL;
150 uint8_t * status_ptr = NULL;
152 uint32_t xfer_len = 0;
154 cmd_desc = &(q->desc[desc_idx]);
156 if (v3_gpa_to_hva(core, cmd_desc->addr_gpa, (addr_t *)&cmd) == -1) {
157 PrintError("Could not translate SYMMOD header address\n");
161 desc_idx = cmd_desc->next;
163 if (cmd->cmd == CMD_LOAD) {
164 struct vring_desc * name_desc = NULL;
165 struct vring_desc * buf_desc = NULL;
167 struct v3_sym_capsule * capsule = NULL;
171 PrintDebug("Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE);
174 PrintError("Symmod loads must include at least 3 descriptors (cnt=%d)\n", desc_cnt);
178 name_desc = &(q->desc[desc_idx]);
180 if (v3_gpa_to_hva(core, name_desc->addr_gpa, (addr_t *)&name) == -1) {
181 PrintError("Could not translate SYMMOD header address\n");
185 desc_idx = name_desc->next;
187 capsule = v3_get_sym_capsule(core->vm_info, name);
189 for (i = 0; i < desc_cnt - 3; i++) {
190 uint8_t tmp_status = 0;
191 uint8_t * buf = NULL;
193 buf_desc = &(q->desc[desc_idx]);
195 if (v3_gpa_to_hva(core, buf_desc->addr_gpa, (addr_t *)&(buf)) == -1) {
196 PrintError("Could not translate buffer address\n");
200 memcpy(buf, capsule->start_addr + offset, buf_desc->length);
201 PrintDebug("Copying module to virtio buffers: SRC=%p, DST=%p, len=%d\n",
202 (void *)(capsule->start_addr + offset), (void *)buf, buf_desc->length);
204 if (tmp_status != 0) {
205 PrintError("Error loading module segment\n");
210 offset += buf_desc->length;
211 xfer_len += buf_desc->length;
212 desc_idx = buf_desc->next;
215 PrintError("Invalid SYMMOD Loader command\n");
219 status_desc = &(q->desc[desc_idx]);
221 if (v3_gpa_to_hva(core, status_desc->addr_gpa, (addr_t *)&status_ptr) == -1) {
222 PrintError("SYMMOD Error could not translate status address\n");
226 xfer_len += status_desc->length;
227 *status_ptr = status;
229 PrintDebug("Transferred %d bytes (xfer_len)\n", xfer_len);
230 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
231 q->used->ring[q->used->index % QUEUE_SIZE].length = xfer_len; // set to total inbound xfer length
239 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
240 PrintDebug("Raising IRQ %d\n", sym_state->pci_dev->config_header.intr_line);
241 v3_pci_raise_irq(sym_state->pci_bus, sym_state->pci_dev, 0);
242 sym_state->virtio_cfg.pci_isr = 1;
252 static int handle_notification_kick(struct guest_info * core, struct virtio_sym_state * sym_state) {
253 // struct virtio_queue * q = sym_state->cur_queue;
254 struct virtio_queue * q = &(sym_state->queue[NOTIFY_QUEUE]);
255 struct hashtable_iter * capsule_iter = NULL;
257 PrintDebug("SYMMOD: VIRTIO SYMMOD Kick on notification queue\n");
259 capsule_iter = v3_create_htable_iter(sym_state->symmod_state->capsule_table);
262 uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
263 struct vring_desc * hdr_desc = NULL;
264 struct symmod_hdr * hdr = NULL;
265 struct v3_sym_capsule * capsule = NULL;
268 capsule = (struct v3_sym_capsule *)v3_htable_get_iter_value(capsule_iter);
271 PrintDebug("SYMMOD: Advertising Capsule %s\n", capsule->name);
273 if (capsule->type != V3_SYMMOD_LNX) {
279 if (q->cur_avail_idx == q->avail->index) {
280 PrintError("Notification Queue Too SMALL\n");
284 hdr_desc = &(q->desc[desc_idx]);
286 if (v3_gpa_to_hva(core, hdr_desc->addr_gpa, (addr_t *)&hdr) == -1) {
287 PrintError("Could not translate SYMMOD header address\n");
291 memset(hdr, 0, sizeof(struct symmod_hdr));
294 memcpy(hdr->name, capsule->name, strlen(capsule->name));
295 hdr->num_bytes = capsule->size;
296 hdr->flags = capsule->flags;
297 hdr->action = V3_SYMMOD_ACT_ADVERTISE;
299 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
300 q->used->ring[q->used->index % QUEUE_SIZE].length = sizeof(struct symmod_hdr) ; // set to total inbound xfer length
305 } while (v3_htable_iter_advance(capsule_iter));
308 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
309 PrintDebug("Raising IRQ %d\n", sym_state->pci_dev->config_header.intr_line);
310 v3_pci_raise_irq(sym_state->pci_bus, sym_state->pci_dev, 0);
311 sym_state->virtio_cfg.pci_isr = 1;
319 static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * private_data) {
320 struct virtio_sym_state * sym_state = (struct virtio_sym_state *)private_data;
321 int port_idx = port % sym_state->io_range_size;
324 PrintDebug("SYMMOD: VIRTIO SYMMOD Write for port %d len=%d, value=%x\n",
325 port, length, *(uint32_t *)src);
326 PrintDebug("SYMMOD: port idx=%d\n", port_idx);
330 case GUEST_FEATURES_PORT:
332 PrintError("Illegal write length for guest features\n");
336 sym_state->virtio_cfg.guest_features = *(uint32_t *)src;
339 case VRING_PG_NUM_PORT:
341 addr_t pfn = *(uint32_t *)src;
342 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
344 sym_state->cur_queue->pfn = pfn;
346 sym_state->cur_queue->ring_desc_addr = page_addr ;
347 sym_state->cur_queue->ring_avail_addr = page_addr + (QUEUE_SIZE * sizeof(struct vring_desc));
348 sym_state->cur_queue->ring_used_addr = ( sym_state->cur_queue->ring_avail_addr + \
349 sizeof(struct vring_avail) + \
350 (QUEUE_SIZE * sizeof(uint16_t)));
352 // round up to next page boundary.
353 sym_state->cur_queue->ring_used_addr = (sym_state->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
355 if (v3_gpa_to_hva(core, sym_state->cur_queue->ring_desc_addr, (addr_t *)&(sym_state->cur_queue->desc)) == -1) {
356 PrintError("Could not translate ring descriptor address\n");
361 if (v3_gpa_to_hva(core, sym_state->cur_queue->ring_avail_addr, (addr_t *)&(sym_state->cur_queue->avail)) == -1) {
362 PrintError("Could not translate ring available address\n");
367 if (v3_gpa_to_hva(core, sym_state->cur_queue->ring_used_addr, (addr_t *)&(sym_state->cur_queue->used)) == -1) {
368 PrintError("Could not translate ring used address\n");
372 PrintDebug("SYMMOD: RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
373 (void *)(sym_state->cur_queue->ring_desc_addr),
374 (void *)(sym_state->cur_queue->ring_avail_addr),
375 (void *)(sym_state->cur_queue->ring_used_addr));
377 PrintDebug("SYMMOD: RingDesc=%p, Avail=%p, Used=%p\n",
378 sym_state->cur_queue->desc, sym_state->cur_queue->avail, sym_state->cur_queue->used);
381 PrintError("Illegal write length for page frame number\n");
385 case VRING_Q_SEL_PORT:
386 sym_state->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
388 if (sym_state->virtio_cfg.vring_queue_selector > NUM_QUEUES) {
389 PrintError("Virtio Symbiotic device has no qeueues. Selected %d\n",
390 sym_state->virtio_cfg.vring_queue_selector);
394 sym_state->cur_queue = &(sym_state->queue[sym_state->virtio_cfg.vring_queue_selector]);
397 case VRING_Q_NOTIFY_PORT: {
398 uint16_t queue_idx = *(uint16_t *)src;
400 PrintDebug("SYMMOD: Handling Kick\n");
402 if (queue_idx == 0) {
403 if (handle_notification_kick(core, sym_state) == -1) {
404 PrintError("Could not handle Notification Kick\n");
408 sym_state->notifier_active = 1;
410 } else if (queue_idx == 1) {
411 if (handle_xfer_kick(core, sym_state) == -1) {
412 PrintError("Could not handle Symbiotic Notification\n");
416 PrintError("Kick on invalid queue (%d)\n", queue_idx);
422 case VIRTIO_STATUS_PORT:
423 sym_state->virtio_cfg.status = *(uint8_t *)src;
425 if (sym_state->virtio_cfg.status == 0) {
426 PrintDebug("SYMMOD: Resetting device\n");
427 virtio_reset(sym_state);
432 case VIRTIO_ISR_PORT:
433 sym_state->virtio_cfg.pci_isr = *(uint8_t *)src;
444 static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * private_data) {
446 struct virtio_sym_state * sym_state = (struct virtio_sym_state *)private_data;
447 int port_idx = port % sym_state->io_range_size;
450 PrintDebug("SYMMOD: VIRTIO SYMBIOTIC Read for port %d (index =%d), length=%d\n",
451 port, port_idx, length);
454 case HOST_FEATURES_PORT:
456 PrintError("Illegal read length for host features\n");
460 *(uint32_t *)dst = sym_state->virtio_cfg.host_features;
463 case VRING_PG_NUM_PORT:
465 PrintError("Illegal read length for page frame number\n");
469 *(uint32_t *)dst = sym_state->cur_queue->pfn;
472 case VRING_SIZE_PORT:
474 PrintError("Illegal read length for vring size\n");
478 *(uint16_t *)dst = sym_state->cur_queue->queue_size;
482 case VIRTIO_STATUS_PORT:
484 PrintError("Illegal read length for status\n");
488 *(uint8_t *)dst = sym_state->virtio_cfg.status;
491 case VIRTIO_ISR_PORT:
492 *(uint8_t *)dst = sym_state->virtio_cfg.pci_isr;
493 sym_state->virtio_cfg.pci_isr = 0;
494 v3_pci_lower_irq(sym_state->pci_bus, sym_state->pci_dev, 0);
498 if ( (port_idx >= sizeof(struct virtio_config)) &&
499 (port_idx < (sizeof(struct virtio_config) + sizeof(struct sym_config))) ) {
500 int cfg_offset = port_idx - sizeof(struct virtio_config);
501 uint8_t * cfg_ptr = (uint8_t *)&(sym_state->sym_cfg);
503 memcpy(dst, cfg_ptr + cfg_offset, length);
505 V3_Print("Reading SymConfig at idx %d (val=%x)\n", cfg_offset, *(uint32_t *)cfg_ptr);
508 PrintError("Read of Unhandled Virtio Read\n");
521 static int virtio_load_capsule(struct v3_vm_info * vm, struct v3_sym_capsule * mod, void * priv_data) {
522 struct virtio_sym_state * virtio = (struct virtio_sym_state *)priv_data;
523 // struct virtio_queue * q = virtio->cur_queue;
524 struct virtio_queue * q = &(virtio->queue[NOTIFY_QUEUE]);
527 if (strlen(mod->name) >= 32) {
528 PrintError("Capsule name is too long... (%d bytes) limit is 32\n", (uint32_t)strlen(mod->name));
532 PrintDebug("SYMMOD: VIRTIO SYMMOD Loader: Loading Capsule (size=%d)\n", mod->size);
534 //queue is not set yet
535 if (q->ring_avail_addr == 0) {
536 PrintError("Queue is not set\n");
541 if (q->cur_avail_idx != q->avail->index) {
542 uint16_t notifier_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
543 struct symmod_hdr * notifier = NULL;
544 struct vring_desc * notifier_desc = NULL;
546 PrintDebug("SYMMOD: Descriptor index=%d\n", q->cur_avail_idx % q->queue_size);
548 notifier_desc = &(q->desc[notifier_idx]);
550 PrintDebug("SYMMOD: Notifier Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n",
551 notifier_desc, (void *)(addr_t)(notifier_desc->addr_gpa),
552 notifier_desc->length, notifier_desc->flags,
553 notifier_desc->next);
555 if (v3_gpa_to_hva(&(vm->cores[0]), notifier_desc->addr_gpa, (addr_t *)&(notifier)) == -1) {
556 PrintError("Could not translate receive buffer address\n");
560 // clear the notifier
561 memset(notifier, 0, sizeof(struct symmod_hdr));
563 // set the capsule name
564 memcpy(notifier->name, mod->name, strlen(mod->name));
566 // set capsule length
567 notifier->num_bytes = mod->size;
568 notifier->flags = mod->flags;
569 notifier->action = V3_SYMMOD_ACT_LOAD;
572 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
574 q->used->ring[q->used->index % q->queue_size].length = sizeof(struct symmod_hdr);
580 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
581 PrintDebug("SYMMOD: Raising IRQ %d\n", virtio->pci_dev->config_header.intr_line);
582 v3_pci_raise_irq(virtio->pci_bus, virtio->pci_dev, 0);
583 virtio->virtio_cfg.pci_isr = 0x1;
591 static int virtio_free(struct virtio_sym_state * virtio_state) {
592 // unregister from PCI
594 V3_Free(virtio_state);
599 static struct v3_device_ops dev_ops = {
600 .free = (int (*)(void *))virtio_free,
605 static struct v3_symmod_loader_ops loader_ops = {
606 .load_capsule = virtio_load_capsule,
610 static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
611 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
612 struct virtio_sym_state * virtio_state = NULL;
613 struct v3_symmod_state * symmod_state = &(vm->sym_vm_state.symmod_state);
614 struct pci_device * pci_dev = NULL;
615 char * dev_id = v3_cfg_val(cfg, "ID");
617 PrintDebug("SYMMOD: Initializing VIRTIO Symbiotic Module device\n");
619 if (pci_bus == NULL) {
620 PrintError("VirtIO devices require a PCI Bus");
624 virtio_state = (struct virtio_sym_state *)V3_Malloc(sizeof(struct virtio_sym_state));
627 PrintError("Cannot allocate in init\n");
631 memset(virtio_state, 0, sizeof(struct virtio_sym_state));
633 virtio_state->vm = vm;
634 virtio_state->symmod_state = symmod_state;
639 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, virtio_state);
642 PrintError("Could not attach device %s\n", dev_id);
643 V3_Free(virtio_state);
648 // PCI initialization
650 struct v3_pci_bar bars[6];
651 int num_ports = sizeof(struct virtio_config) + sizeof(struct sym_config);
652 int tmp_ports = num_ports;
656 // This gets the number of ports, rounded up to a power of 2
657 virtio_state->io_range_size = 1; // must be a power of 2
659 while (tmp_ports > 0) {
661 virtio_state->io_range_size <<= 1;
664 // this is to account for any low order bits being set in num_ports
665 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
666 if ((num_ports & ((virtio_state->io_range_size >> 1) - 1)) == 0) {
667 virtio_state->io_range_size >>= 1;
671 for (i = 0; i < 6; i++) {
672 bars[i].type = PCI_BAR_NONE;
675 bars[0].type = PCI_BAR_IO;
676 bars[0].default_base_port = -1;
677 bars[0].num_ports = virtio_state->io_range_size;
679 bars[0].io_read = virtio_io_read;
680 bars[0].io_write = virtio_io_write;
681 bars[0].private_data = virtio_state;
683 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
684 0, PCI_AUTO_DEV_NUM, 0,
685 "LNX_VIRTIO_SYMMOD", bars,
686 NULL, NULL, NULL, NULL, virtio_state);
689 PrintError("Could not register PCI Device\n");
690 v3_remove_device(dev);
694 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
695 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
698 pci_dev->config_header.device_id = VIRTIO_SYMMOD_DEV_ID;
699 pci_dev->config_header.class = PCI_CLASS_MEMORY;
700 pci_dev->config_header.subclass = PCI_MEM_SUBCLASS_RAM;
702 pci_dev->config_header.subsystem_id = VIRTIO_SYMMOD_SUBDEVICE_ID;
705 pci_dev->config_header.intr_pin = 1;
707 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
710 virtio_state->pci_dev = pci_dev;
711 virtio_state->pci_bus = pci_bus;
715 V3_Print("SYMMOD: %d available sym modules\n", virtio_state->sym_cfg.avail_mods);
717 virtio_reset(virtio_state);
719 v3_set_symmod_loader(vm, &loader_ops, virtio_state);
725 device_register("LNX_VIRTIO_SYMMOD", virtio_init)