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->start_addr + offset, buf_desc->length);
151 PrintDebug("Copying module to virtio buffers: SRC=%p, DST=%p, len=%d\n",
152 (void *)(module->start_addr + offset), (void *)buf, buf_desc->length);
154 if (tmp_status != 0) {
155 PrintError("Error loading module segment\n");
160 offset += buf_desc->length;
161 xfer_len += buf_desc->length;
162 desc_idx = buf_desc->next;
165 status_desc = &(q->desc[desc_idx]);
167 if (guest_pa_to_host_va(core, status_desc->addr_gpa, (addr_t *)&status_ptr) == -1) {
168 PrintError("SYMMOD Error could not translate status address\n");
172 xfer_len += status_desc->length;
173 *status_ptr = status;
175 PrintDebug("Transferred %d bytes (xfer_len)\n", xfer_len);
176 q->used->ring[q->used->index % QUEUE_SIZE].id = q->avail->ring[q->cur_avail_idx % QUEUE_SIZE];
177 q->used->ring[q->used->index % QUEUE_SIZE].length = xfer_len; // set to total inbound xfer length
184 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
185 PrintDebug("Raising IRQ %d\n", sym_state->pci_dev->config_header.intr_line);
186 v3_pci_raise_irq(sym_state->pci_bus, 0, sym_state->pci_dev);
187 sym_state->virtio_cfg.pci_isr = 1;
195 static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, uint_t length, void * private_data) {
196 struct virtio_sym_state * sym_state = (struct virtio_sym_state *)private_data;
197 int port_idx = port % sym_state->io_range_size;
200 PrintDebug("SYMMOD: VIRTIO SYMMOD Write for port %d len=%d, value=%x\n",
201 port, length, *(uint32_t *)src);
202 PrintDebug("SYMMOD: port idx=%d\n", port_idx);
206 case GUEST_FEATURES_PORT:
208 PrintError("Illegal write length for guest features\n");
212 sym_state->virtio_cfg.guest_features = *(uint32_t *)src;
215 case VRING_PG_NUM_PORT:
217 addr_t pfn = *(uint32_t *)src;
218 addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT);
220 sym_state->cur_queue->pfn = pfn;
222 sym_state->cur_queue->ring_desc_addr = page_addr ;
223 sym_state->cur_queue->ring_avail_addr = page_addr + (QUEUE_SIZE * sizeof(struct vring_desc));
224 sym_state->cur_queue->ring_used_addr = ( sym_state->cur_queue->ring_avail_addr + \
225 sizeof(struct vring_avail) + \
226 (QUEUE_SIZE * sizeof(uint16_t)));
228 // round up to next page boundary.
229 sym_state->cur_queue->ring_used_addr = (sym_state->cur_queue->ring_used_addr + 0xfff) & ~0xfff;
231 if (guest_pa_to_host_va(core, sym_state->cur_queue->ring_desc_addr, (addr_t *)&(sym_state->cur_queue->desc)) == -1) {
232 PrintError("Could not translate ring descriptor address\n");
237 if (guest_pa_to_host_va(core, sym_state->cur_queue->ring_avail_addr, (addr_t *)&(sym_state->cur_queue->avail)) == -1) {
238 PrintError("Could not translate ring available address\n");
243 if (guest_pa_to_host_va(core, sym_state->cur_queue->ring_used_addr, (addr_t *)&(sym_state->cur_queue->used)) == -1) {
244 PrintError("Could not translate ring used address\n");
248 PrintDebug("SYMMOD: RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n",
249 (void *)(sym_state->cur_queue->ring_desc_addr),
250 (void *)(sym_state->cur_queue->ring_avail_addr),
251 (void *)(sym_state->cur_queue->ring_used_addr));
253 PrintDebug("SYMMOD: RingDesc=%p, Avail=%p, Used=%p\n",
254 sym_state->cur_queue->desc, sym_state->cur_queue->avail, sym_state->cur_queue->used);
257 PrintError("Illegal write length for page frame number\n");
261 case VRING_Q_SEL_PORT:
262 sym_state->virtio_cfg.vring_queue_selector = *(uint16_t *)src;
264 if (sym_state->virtio_cfg.vring_queue_selector > NUM_QUEUES) {
265 PrintError("Virtio Symbiotic device has no qeueues. Selected %d\n",
266 sym_state->virtio_cfg.vring_queue_selector);
270 sym_state->cur_queue = &(sym_state->queue[sym_state->virtio_cfg.vring_queue_selector]);
273 case VRING_Q_NOTIFY_PORT: {
274 uint16_t queue_idx = *(uint16_t *)src;
276 PrintDebug("SYMMOD: Handling Kick\n");
278 if (queue_idx == 0) {
279 sym_state->notifier_active = 1;
281 } else if (queue_idx == 1) {
282 if (handle_xfer_kick(core, sym_state) == -1) {
283 PrintError("Could not handle Symbiotic Notification\n");
287 PrintError("Kick on invalid queue (%d)\n", queue_idx);
293 case VIRTIO_STATUS_PORT:
294 sym_state->virtio_cfg.status = *(uint8_t *)src;
296 if (sym_state->virtio_cfg.status == 0) {
297 PrintDebug("SYMMOD: Resetting device\n");
298 virtio_reset(sym_state);
303 case VIRTIO_ISR_PORT:
304 sym_state->virtio_cfg.pci_isr = *(uint8_t *)src;
315 static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, uint_t length, void * private_data) {
317 struct virtio_sym_state * sym_state = (struct virtio_sym_state *)private_data;
318 int port_idx = port % sym_state->io_range_size;
321 PrintDebug("SYMMOD: VIRTIO SYMBIOTIC Read for port %d (index =%d), length=%d\n",
322 port, port_idx, length);
325 case HOST_FEATURES_PORT:
327 PrintError("Illegal read length for host features\n");
331 *(uint32_t *)dst = sym_state->virtio_cfg.host_features;
334 case VRING_PG_NUM_PORT:
336 PrintError("Illegal read length for page frame number\n");
340 *(uint32_t *)dst = sym_state->cur_queue->pfn;
343 case VRING_SIZE_PORT:
345 PrintError("Illegal read length for vring size\n");
349 *(uint16_t *)dst = sym_state->cur_queue->queue_size;
353 case VIRTIO_STATUS_PORT:
355 PrintError("Illegal read length for status\n");
359 *(uint8_t *)dst = sym_state->virtio_cfg.status;
362 case VIRTIO_ISR_PORT:
363 *(uint8_t *)dst = sym_state->virtio_cfg.pci_isr;
364 sym_state->virtio_cfg.pci_isr = 0;
365 v3_pci_lower_irq(sym_state->pci_bus, 0, sym_state->pci_dev);
369 if ( (port_idx >= sizeof(struct virtio_config)) &&
370 (port_idx < (sizeof(struct virtio_config) + sizeof(struct sym_config))) ) {
371 int cfg_offset = port_idx - sizeof(struct virtio_config);
372 uint8_t * cfg_ptr = (uint8_t *)&(sym_state->sym_cfg);
374 memcpy(dst, cfg_ptr + cfg_offset, length);
377 PrintError("Read of Unhandled Virtio Read\n");
390 static int virtio_load_module(struct v3_vm_info * vm, char * name, int mod_size, void * priv_data) {
391 struct virtio_sym_state * virtio = (struct virtio_sym_state *)priv_data;
392 // struct virtio_queue * q = virtio->cur_queue;
393 struct virtio_queue * q = &(virtio->queue[NOTIFY_QUEUE]);
395 if (strlen(name) >= 32) {
396 PrintError("Module name is too long... (%d bytes) limit is 32\n", (uint32_t)strlen(name));
400 PrintDebug("SYMMOD: VIRTIO SYMMOD Loader: Loading Module (size=%d)\n", mod_size);
402 //queue is not set yet
403 if (q->ring_avail_addr == 0) {
404 PrintError("Queue is not set\n");
409 if (q->cur_avail_idx != q->avail->index) {
410 uint16_t notifier_idx = q->avail->ring[q->cur_avail_idx % q->queue_size];
411 struct symmod_hdr * notifier = NULL;
412 struct vring_desc * notifier_desc = NULL;
414 PrintDebug("SYMMOD: Descriptor index=%d\n", q->cur_avail_idx % q->queue_size);
416 notifier_desc = &(q->desc[notifier_idx]);
418 PrintDebug("SYMMOD: Notifier Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", notifier_desc,
419 (void *)(notifier_desc->addr_gpa), notifier_desc->length, notifier_desc->flags, notifier_desc->next);
421 if (guest_pa_to_host_va(&(vm->cores[0]), notifier_desc->addr_gpa, (addr_t *)&(notifier)) == -1) {
422 PrintError("Could not translate receive buffer address\n");
426 // clear the notifier
427 memset(notifier, 0, sizeof(struct symmod_hdr));
429 // set the module name
430 memcpy(notifier->name, name, strlen(name));
433 notifier->num_bytes = mod_size;
436 q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size];
438 q->used->ring[q->used->index % q->queue_size].length = sizeof(struct symmod_hdr);
444 if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
445 PrintDebug("SYMMOD: Raising IRQ %d\n", virtio->pci_dev->config_header.intr_line);
446 v3_pci_raise_irq(virtio->pci_bus, 0, virtio->pci_dev);
447 virtio->virtio_cfg.pci_isr = 0x1;
457 static struct v3_device_ops dev_ops = {
466 static struct v3_symmod_loader_ops loader_ops = {
467 .load_module = virtio_load_module,
471 static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
472 struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus"));
473 struct virtio_sym_state * virtio_state = NULL;
474 struct pci_device * pci_dev = NULL;
475 char * name = v3_cfg_val(cfg, "name");
477 PrintDebug("SYMMOD: Initializing VIRTIO Symbiotic Module device\n");
479 if (pci_bus == NULL) {
480 PrintError("VirtIO devices require a PCI Bus");
484 virtio_state = (struct virtio_sym_state *)V3_Malloc(sizeof(struct virtio_sym_state));
485 memset(virtio_state, 0, sizeof(struct virtio_sym_state));
487 struct vm_device * dev = v3_allocate_device(name, &dev_ops, virtio_state);
489 if (v3_attach_device(vm, dev) == -1) {
490 PrintError("Could not attach device %s\n", name);
495 // PCI initialization
497 struct v3_pci_bar bars[6];
498 int num_ports = sizeof(struct virtio_config) + sizeof(struct sym_config);
499 int tmp_ports = num_ports;
503 // This gets the number of ports, rounded up to a power of 2
504 virtio_state->io_range_size = 1; // must be a power of 2
506 while (tmp_ports > 0) {
508 virtio_state->io_range_size <<= 1;
511 // this is to account for any low order bits being set in num_ports
512 // if there are none, then num_ports was already a power of 2 so we shift right to reset it
513 if ((num_ports & ((virtio_state->io_range_size >> 1) - 1)) == 0) {
514 virtio_state->io_range_size >>= 1;
518 for (i = 0; i < 6; i++) {
519 bars[i].type = PCI_BAR_NONE;
522 bars[0].type = PCI_BAR_IO;
523 bars[0].default_base_port = -1;
524 bars[0].num_ports = virtio_state->io_range_size;
526 bars[0].io_read = virtio_io_read;
527 bars[0].io_write = virtio_io_write;
528 bars[0].private_data = virtio_state;
530 pci_dev = v3_pci_register_device(pci_bus, PCI_STD_DEVICE,
531 0, PCI_AUTO_DEV_NUM, 0,
532 "LNX_VIRTIO_SYMMOD", bars,
533 NULL, NULL, NULL, virtio_state);
536 PrintError("Could not register PCI Device\n");
540 pci_dev->config_header.vendor_id = VIRTIO_VENDOR_ID;
541 pci_dev->config_header.subsystem_vendor_id = VIRTIO_SUBVENDOR_ID;
544 pci_dev->config_header.device_id = VIRTIO_SYMMOD_DEV_ID;
545 pci_dev->config_header.class = PCI_CLASS_MEMORY;
546 pci_dev->config_header.subclass = PCI_MEM_SUBCLASS_RAM;
548 pci_dev->config_header.subsystem_id = VIRTIO_SYMMOD_SUBDEVICE_ID;
551 pci_dev->config_header.intr_pin = 1;
553 pci_dev->config_header.max_latency = 1; // ?? (qemu does it...)
556 virtio_state->pci_dev = pci_dev;
557 virtio_state->pci_bus = pci_bus;
560 virtio_reset(virtio_state);
562 v3_set_symmod_loader(vm, &loader_ops, virtio_state);
568 device_register("LNX_VIRTIO_SYMMOD", virtio_init)