X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Flnx_virtio_blk.c;h=0f32c77cd019fa8e085745cff5d134e19dbf788f;hb=60ad6a41c6d0ee08ed689e8505eb0c3df0c2a289;hp=7e26b3081566e8d51d71a2cc4dedc4376d81f422;hpb=aac189310cca0f5f14543d91413d3b5b58250a3e;p=palacios.git diff --git a/palacios/src/devices/lnx_virtio_blk.c b/palacios/src/devices/lnx_virtio_blk.c index 7e26b30..0f32c77 100644 --- a/palacios/src/devices/lnx_virtio_blk.c +++ b/palacios/src/devices/lnx_virtio_blk.c @@ -26,7 +26,7 @@ -#ifndef CONFIG_DEBUG_VIRTIO_BLK +#ifndef V3_CONFIG_DEBUG_VIRTIO_BLK #undef PrintDebug #define PrintDebug(fmt, args...) #endif @@ -59,6 +59,13 @@ struct blk_config { uint16_t cylinders; uint8_t heads; uint8_t sectors; + /* + uint32_t blk_size; + uint8_t phys_block_exp; + uint8_t alignment_offset; + uint16_t min_io_size; + uint32_t opt_io_size; + */ } __attribute__((packed)); @@ -125,7 +132,7 @@ static int blk_reset(struct virtio_blk_state * virtio) { static int handle_read_op(struct virtio_blk_state * blk_state, uint8_t * buf, uint64_t * sector, uint64_t len) { int ret = -1; - PrintDebug("Reading Disk\n"); + PrintDebug(info->vm_info, info, "Reading Disk\n"); ret = blk_state->ops->read(buf, (*sector) * SECTOR_SIZE, len, (void *)(blk_state->backend_data)); *sector += (len / SECTOR_SIZE); @@ -136,7 +143,7 @@ static int handle_read_op(struct virtio_blk_state * blk_state, uint8_t * buf, ui static int handle_write_op(struct virtio_blk_state * blk_state, uint8_t * buf, uint64_t * sector, uint64_t len) { int ret = -1; - PrintDebug("Writing Disk\n"); + PrintDebug(info->vm_info, info, "Writing Disk\n"); ret = blk_state->ops->write(buf, (*sector) * SECTOR_SIZE, len, (void *)(blk_state->backend_data)); *sector += (len / SECTOR_SIZE); @@ -151,13 +158,13 @@ static int handle_block_op(struct guest_info * core, struct virtio_blk_state * b struct vring_desc * buf_desc, uint8_t * status) { uint8_t * buf = NULL; - PrintDebug("Handling Block op\n"); + PrintDebug(core->vm_info, core, "Handling Block op\n"); if (v3_gpa_to_hva(core, buf_desc->addr_gpa, (addr_t *)&(buf)) == -1) { - PrintError("Could not translate buffer address\n"); + PrintError(core->vm_info, core, "Could not translate buffer address\n"); return -1; } - PrintDebug("Sector=%p Length=%d\n", (void *)(addr_t)(hdr->sector), buf_desc->length); + PrintDebug(core->vm_info, core, "Sector=%p Length=%d\n", (void *)(addr_t)(hdr->sector), buf_desc->length); if (hdr->type == BLK_IN_REQ) { if (handle_read_op(blk_state, buf, &(hdr->sector), buf_desc->length) == -1) { @@ -174,12 +181,12 @@ static int handle_block_op(struct guest_info * core, struct virtio_blk_state * b *status = BLK_STATUS_OK; } } else if (hdr->type == BLK_SCSI_CMD) { - PrintError("VIRTIO: SCSI Command Not supported!!!\n"); + PrintError(core->vm_info, core, "VIRTIO: SCSI Command Not supported!!!\n"); *status = BLK_STATUS_NOT_SUPPORTED; return -1; } - PrintDebug("Returning Status: %d\n", *status); + PrintDebug(core->vm_info, core, "Returning Status: %d\n", *status); return 0; } @@ -201,7 +208,7 @@ static int get_desc_count(struct virtio_queue * q, int index) { static int handle_kick(struct guest_info * core, struct virtio_blk_state * blk_state) { struct virtio_queue * q = &(blk_state->queue); - PrintDebug("VIRTIO KICK: cur_index=%d (mod=%d), avail_index=%d\n", + PrintDebug(core->vm_info, core, "VIRTIO 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) { @@ -217,28 +224,28 @@ static int handle_kick(struct guest_info * core, struct virtio_blk_state * blk_s uint8_t status = BLK_STATUS_OK; uint32_t req_len = 0; - PrintDebug("Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE); + PrintDebug(core->vm_info, core, "Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % QUEUE_SIZE); if (desc_cnt < 3) { - PrintError("Block operations must include at least 3 descriptors\n"); + PrintError(core->vm_info, core, "Block operations must include at least 3 descriptors\n"); return -1; } hdr_desc = &(q->desc[desc_idx]); - PrintDebug("Header Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", hdr_desc, + PrintDebug(core->vm_info, core, "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); if (v3_gpa_to_hva(core, hdr_desc->addr_gpa, &(hdr_addr)) == -1) { - PrintError("Could not translate block header address\n"); + PrintError(core->vm_info, core, "Could not translate block header address\n"); return -1; } // We copy the block op header out because we are going to modify its contents memcpy(&hdr, (void *)hdr_addr, sizeof(struct blk_op_hdr)); - PrintDebug("Blk Op Hdr (ptr=%p) type=%d, sector=%p\n", (void *)hdr_addr, hdr.type, (void *)hdr.sector); + PrintDebug(core->vm_info, core, "Blk Op Hdr (ptr=%p) type=%d, sector=%p\n", (void *)hdr_addr, hdr.type, (void *)hdr.sector); desc_idx = hdr_desc->next; @@ -247,11 +254,11 @@ static int handle_kick(struct guest_info * core, struct virtio_blk_state * blk_s buf_desc = &(q->desc[desc_idx]); - PrintDebug("Buffer Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", buf_desc, + PrintDebug(core->vm_info, core, "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 (handle_block_op(core, blk_state, &hdr, buf_desc, &tmp_status) == -1) { - PrintError("Error handling block operation\n"); + PrintError(core->vm_info, core, "Error handling block operation\n"); return -1; } @@ -265,11 +272,11 @@ static int handle_kick(struct guest_info * core, struct virtio_blk_state * blk_s status_desc = &(q->desc[desc_idx]); - PrintDebug("Status Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", status_desc, + PrintDebug(core->vm_info, core, "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 (v3_gpa_to_hva(core, status_desc->addr_gpa, (addr_t *)&(status_ptr)) == -1) { - PrintError("Could not translate status address\n"); + PrintError(core->vm_info, core, "Could not translate status address\n"); return -1; } @@ -284,8 +291,8 @@ static int handle_kick(struct guest_info * core, struct virtio_blk_state * blk_s } if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) { - PrintDebug("Raising IRQ %d\n", blk_state->pci_dev->config_header.intr_line); - v3_pci_raise_irq(blk_state->virtio_dev->pci_bus, 0, blk_state->pci_dev); + PrintDebug(core->vm_info, core, "Raising IRQ %d\n", blk_state->pci_dev->config_header.intr_line); + v3_pci_raise_irq(blk_state->virtio_dev->pci_bus, blk_state->pci_dev, 0); blk_state->virtio_cfg.pci_isr = 1; } @@ -297,7 +304,7 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, int port_idx = port % blk_state->io_range_size; - PrintDebug("VIRTIO BLOCK Write for port %d (index=%d) len=%d, value=%x\n", + PrintDebug(core->vm_info, core, "VIRTIO BLOCK Write for port %d (index=%d) len=%d, value=%x\n", port, port_idx, length, *(uint32_t *)src); @@ -305,12 +312,12 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, switch (port_idx) { case GUEST_FEATURES_PORT: if (length != 4) { - PrintError("Illegal write length for guest features\n"); + PrintError(core->vm_info, core, "Illegal write length for guest features\n"); return -1; } blk_state->virtio_cfg.guest_features = *(uint32_t *)src; - PrintDebug("Setting Guest Features to %x\n", blk_state->virtio_cfg.guest_features); + PrintDebug(core->vm_info, core, "Setting Guest Features to %x\n", blk_state->virtio_cfg.guest_features); break; case VRING_PG_NUM_PORT: @@ -331,32 +338,32 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, blk_state->queue.ring_used_addr = (blk_state->queue.ring_used_addr + 0xfff) & ~0xfff; if (v3_gpa_to_hva(core, blk_state->queue.ring_desc_addr, (addr_t *)&(blk_state->queue.desc)) == -1) { - PrintError("Could not translate ring descriptor address\n"); + PrintError(core->vm_info, core, "Could not translate ring descriptor address\n"); return -1; } if (v3_gpa_to_hva(core, blk_state->queue.ring_avail_addr, (addr_t *)&(blk_state->queue.avail)) == -1) { - PrintError("Could not translate ring available address\n"); + PrintError(core->vm_info, core, "Could not translate ring available address\n"); return -1; } if (v3_gpa_to_hva(core, blk_state->queue.ring_used_addr, (addr_t *)&(blk_state->queue.used)) == -1) { - PrintError("Could not translate ring used address\n"); + PrintError(core->vm_info, core, "Could not translate ring used address\n"); return -1; } - PrintDebug("RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n", + PrintDebug(core->vm_info, core, "RingDesc_addr=%p, Avail_addr=%p, Used_addr=%p\n", (void *)(blk_state->queue.ring_desc_addr), (void *)(blk_state->queue.ring_avail_addr), (void *)(blk_state->queue.ring_used_addr)); - PrintDebug("RingDesc=%p, Avail=%p, Used=%p\n", + PrintDebug(core->vm_info, core, "RingDesc=%p, Avail=%p, Used=%p\n", blk_state->queue.desc, blk_state->queue.avail, blk_state->queue.used); } else { - PrintError("Illegal write length for page frame number\n"); + PrintError(core->vm_info, core, "Illegal write length for page frame number\n"); return -1; } break; @@ -364,16 +371,16 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, blk_state->virtio_cfg.vring_queue_selector = *(uint16_t *)src; if (blk_state->virtio_cfg.vring_queue_selector != 0) { - PrintError("Virtio Block device only uses 1 queue, selected %d\n", + PrintError(core->vm_info, core, "Virtio Block device only uses 1 queue, selected %d\n", blk_state->virtio_cfg.vring_queue_selector); return -1; } break; case VRING_Q_NOTIFY_PORT: - PrintDebug("Handling Kick\n"); + PrintDebug(core->vm_info, core, "Handling Kick\n"); if (handle_kick(core, blk_state) == -1) { - PrintError("Could not handle Block Notification\n"); + PrintError(core->vm_info, core, "Could not handle Block Notification\n"); return -1; } break; @@ -381,7 +388,7 @@ static int virtio_io_write(struct guest_info * core, uint16_t port, void * src, blk_state->virtio_cfg.status = *(uint8_t *)src; if (blk_state->virtio_cfg.status == 0) { - PrintDebug("Resetting device\n"); + PrintDebug(core->vm_info, core, "Resetting device\n"); blk_reset(blk_state); } @@ -404,41 +411,47 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u int port_idx = port % blk_state->io_range_size; - PrintDebug("VIRTIO BLOCK Read for port %d (index =%d), length=%d\n", + PrintDebug(core->vm_info, core, "VIRTIO BLOCK Read for port %d (index =%d), length=%d\n", port, port_idx, length); + switch (port_idx) { case HOST_FEATURES_PORT: - if (length != 4) { - PrintError("Illegal read length for host features\n"); + case HOST_FEATURES_PORT + 1: + case HOST_FEATURES_PORT + 2: + case HOST_FEATURES_PORT + 3: + if (port_idx + length > HOST_FEATURES_PORT + 4) { + PrintError(core->vm_info, core, "Illegal read length for host features (len=%d)\n", length); return -1; } - *(uint32_t *)dst = blk_state->virtio_cfg.host_features; - + memcpy(dst, &(blk_state->virtio_cfg.host_features), length); break; case VRING_PG_NUM_PORT: - if (length != 4) { - PrintError("Illegal read length for page frame number\n"); + case VRING_PG_NUM_PORT + 1: + case VRING_PG_NUM_PORT + 2: + case VRING_PG_NUM_PORT + 3: + if (port_idx + length > VRING_PG_NUM_PORT + 4) { + PrintError(core->vm_info, core, "Illegal read length for vring pg num (len=%d)\n", length); return -1; } - *(uint32_t *)dst = blk_state->queue.pfn; - + memcpy(dst, &(blk_state->queue.pfn), length); break; case VRING_SIZE_PORT: - if (length != 2) { - PrintError("Illegal read length for vring size\n"); + case VRING_SIZE_PORT + 1: + if (length > 2) { + PrintError(core->vm_info, core, "Illegal read length for vring size (len=%d)\n", length); return -1; } - - *(uint16_t *)dst = blk_state->queue.queue_size; + + memcpy(dst, &(blk_state->queue.queue_size), length); break; case VIRTIO_STATUS_PORT: if (length != 1) { - PrintError("Illegal read length for status\n"); + PrintError(core->vm_info, core, "Illegal read length for status (len=%d)\n", length); return -1; } @@ -448,7 +461,7 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u case VIRTIO_ISR_PORT: *(uint8_t *)dst = blk_state->virtio_cfg.pci_isr; blk_state->virtio_cfg.pci_isr = 0; - v3_pci_lower_irq(blk_state->virtio_dev->pci_bus, 0, blk_state->pci_dev); + v3_pci_lower_irq(blk_state->virtio_dev->pci_bus, blk_state->pci_dev, 0); break; default: @@ -460,8 +473,16 @@ static int virtio_io_read(struct guest_info * core, uint16_t port, void * dst, u memcpy(dst, cfg_ptr + cfg_offset, length); } else { - PrintError("Read of Unhandled Virtio Read\n"); - return -1; + + PrintError(core->vm_info,core,"Read of Unhandled Virtio Read. Returning 0\n"); + if (length == 1) { + *(uint8_t *)dst = 0; + } else if (length == 2) { + *(uint16_t *)dst = 0; + } else if (length == 4) { + *(uint32_t *)dst = 0; + } + } break; @@ -529,7 +550,7 @@ static int register_dev(struct virtio_dev_state * virtio, struct virtio_blk_stat bars[i].type = PCI_BAR_NONE; } - PrintDebug("Virtio-BLK io_range_size = %d\n", blk_state->io_range_size); + PrintDebug(VM_NONE, VCORE_NONE, "Virtio-BLK io_range_size = %d\n", blk_state->io_range_size); bars[0].type = PCI_BAR_IO; bars[0].default_base_port = -1; @@ -542,10 +563,10 @@ static int register_dev(struct virtio_dev_state * virtio, struct virtio_blk_stat pci_dev = v3_pci_register_device(virtio->pci_bus, PCI_STD_DEVICE, 0, PCI_AUTO_DEV_NUM, 0, "LNX_VIRTIO_BLK", bars, - NULL, NULL, NULL, blk_state); + NULL, NULL, NULL, NULL, blk_state); if (!pci_dev) { - PrintError("Could not register PCI Device\n"); + PrintError(VM_NONE, VCORE_NONE, "Could not register PCI Device\n"); return -1; } @@ -575,6 +596,7 @@ static int register_dev(struct virtio_dev_state * virtio, struct virtio_blk_stat blk_state->virtio_cfg.host_features = VIRTIO_SEG_MAX; blk_state->block_cfg.max_seg = QUEUE_SIZE - 2; + // Virtio Block only uses one queue blk_state->queue.queue_size = QUEUE_SIZE; @@ -596,6 +618,13 @@ static int connect_fn(struct v3_vm_info * vm, struct virtio_dev_state * virtio = (struct virtio_dev_state *)frontend_data; struct virtio_blk_state * blk_state = (struct virtio_blk_state *)V3_Malloc(sizeof(struct virtio_blk_state)); + + if (!blk_state) { + PrintError(vm, VCORE_NONE, "Cannot allocate in connect\n"); + return -1; + } + + memset(blk_state, 0, sizeof(struct virtio_blk_state)); register_dev(virtio, blk_state); @@ -605,7 +634,7 @@ static int connect_fn(struct v3_vm_info * vm, blk_state->block_cfg.capacity = ops->get_capacity(private_data) / SECTOR_SIZE; - PrintDebug("Virtio Capacity = %d -- 0x%p\n", (int)(blk_state->block_cfg.capacity), + PrintDebug(vm, VCORE_NONE, "Virtio Capacity = %d -- 0x%p\n", (int)(blk_state->block_cfg.capacity), (void *)(addr_t)(blk_state->block_cfg.capacity)); return 0; @@ -617,15 +646,21 @@ static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) { struct virtio_dev_state * virtio_state = NULL; char * dev_id = v3_cfg_val(cfg, "ID"); - PrintDebug("Initializing VIRTIO Block device\n"); + PrintDebug(vm, VCORE_NONE, "Initializing VIRTIO Block device\n"); if (pci_bus == NULL) { - PrintError("VirtIO devices require a PCI Bus"); + PrintError(vm, VCORE_NONE, "VirtIO devices require a PCI Bus"); return -1; } virtio_state = (struct virtio_dev_state *)V3_Malloc(sizeof(struct virtio_dev_state)); + + if (!virtio_state) { + PrintError(vm, VCORE_NONE, "Cannot allocate in init\n"); + return -1; + } + memset(virtio_state, 0, sizeof(struct virtio_dev_state)); INIT_LIST_HEAD(&(virtio_state->dev_list)); @@ -635,13 +670,13 @@ static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) { 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); + PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id); V3_Free(virtio_state); return -1; } if (v3_dev_add_blk_frontend(vm, dev_id, connect_fn, (void *)virtio_state) == -1) { - PrintError("Could not register %s as block frontend\n", dev_id); + PrintError(vm, VCORE_NONE, "Could not register %s as block frontend\n", dev_id); v3_remove_device(dev); return -1; }